CodeOutput.lhs 8.22 KB
Newer Older
1 2 3 4 5 6
%
% (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
%
\section{Code output phase}

\begin{code}
7
module CodeOutput( codeOutput, outputForeignStubs ) where
8 9 10

#include "HsVersions.h"

11
#ifndef OMIT_NATIVE_CODEGEN
12
import UniqSupply	( mkSplitUniqSupply )
13 14
import AsmCodeGen	( nativeCodeGen )
#endif
15

16 17 18
#ifdef ILX
import IlxGen		( ilxGen )
#endif
19

20
#ifdef JAVA
21
import JavaGen		( javaGen )
22
import OccurAnal	( occurAnalyseBinds )
23
import qualified PrintJava
24
import OccurAnal	( occurAnalyseBinds )
25
#endif
26

27 28
import Packages		( PackageConfig(name), packageNameString )
import DriverState	( getExplicitPackagesAnd, getPackageCIncludes )
29
import FastString	( unpackFS )
30
import AbsCSyn		( AbstractC )
31
import PprAbsC		( dumpRealC, writeRealC )
32 33
import HscTypes		( ModGuts(..), ModGuts, ForeignStubs(..), 
			  typeEnvTyCons, Dependencies(..) )
34
import CmdLineOpts
35
import ErrUtils		( dumpIfSet_dyn, showPass )
36
import Outputable
37
import Pretty		( Mode(..), printDoc )
38
import CmdLineOpts	( DynFlags, HscLang(..), dopt_OutName )
39
import Monad		( when )
40
import IO
41 42 43
\end{code}


44 45 46 47 48 49
%************************************************************************
%*									*
\subsection{Steering}
%*									*
%************************************************************************

50
\begin{code}
51
codeOutput :: DynFlags
52 53
	   -> ModGuts
	   -> AbstractC			-- Compiled abstract C
54
	   -> IO (Bool{-stub_h_exists-}, Bool{-stub_c_exists-})
55

56 57 58 59
codeOutput dflags 
	   (ModGuts {mg_module = mod_name,
		     mg_types  = type_env,
		     mg_foreign = foreign_stubs,
60
		     mg_deps	= deps,
61
		     mg_binds   = core_binds})
62
	   flat_abstractC
63 64 65 66
  = let
	tycons = typeEnvTyCons type_env
    in
    -- You can have C (c_output) or assembly-language (ncg_output),
67 68 69
    -- but not both.  [Allowing for both gives a space leak on
    -- flat_abstractC.  WDP 94/10]

70
    -- Dunno if the above comment is still meaningful now.  JRS 001024.
71

72 73
    do	{ showPass dflags "CodeOutput"
	; let filenm = dopt_OutName dflags 
74 75 76 77 78 79
	; stubs_exist <- outputForeignStubs dflags foreign_stubs
	; case dopt_HscLang dflags of {
             HscInterpreted -> return ();
             HscAsm         -> outputAsm dflags filenm flat_abstractC;
             HscC           -> outputC dflags filenm flat_abstractC stubs_exist
					deps foreign_stubs;
80 81
             HscJava        -> 
#ifdef JAVA
82
			       outputJava dflags filenm mod_name tycons core_binds;
83
#else
84
                               panic "Java support not compiled into this ghc";
85
#endif
86
	     HscILX         -> 
rrt's avatar
rrt committed
87
#ifdef ILX
88
	                       outputIlx dflags filenm mod_name tycons stg_binds;
89
#else
90
                               panic "ILX support not compiled into this ghc";
rrt's avatar
rrt committed
91
#endif
92 93
	  }
	; return stubs_exist
94
	}
95

96
doOutput :: String -> (Handle -> IO ()) -> IO ()
97
doOutput filenm io_action = bracket (openFile filenm WriteMode) hClose io_action
98 99 100 101 102 103 104 105 106 107
\end{code}


%************************************************************************
%*									*
\subsection{C}
%*									*
%************************************************************************

\begin{code}
108 109
outputC dflags filenm flat_absC 
	(stub_h_exists, _) dependencies (ForeignStubs _ _ ffi_decl_headers _ ) 
110
  = do dumpIfSet_dyn dflags Opt_D_dump_realC "Real C" (dumpRealC flat_absC)
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

       -- figure out which header files to #include in the generated .hc file:
       --
       --   * extra_includes from packages
       --   * -#include options from the cmdline and OPTIONS pragmas
       --   * the _stub.h file, if there is one.
       --
       let packages = dep_pkgs dependencies
       pkg_configs <- getExplicitPackagesAnd packages
       let pkg_names = map name pkg_configs

       c_includes <- getPackageCIncludes pkg_configs
       let cmdline_includes = cmdlineHcIncludes dflags -- -#include options
       
           all_headers =  c_includes
		       ++ reverse cmdline_includes
		       ++ reverse (map unpackFS ffi_decl_headers)
			   -- reverse correct?

       let cc_injects = unlines (map mk_include all_headers)
       	   mk_include h_file = 
       	    case h_file of 
       	       '"':_{-"-} -> "#include "++h_file
       	       '<':_      -> "#include "++h_file
       	       _          -> "#include \""++h_file++"\""

137
       doOutput filenm $ \ h -> do
138 139
	  hPutStr h ("/* GHC_PACKAGES " ++ unwords pkg_names ++ "\n*/\n")
	  hPutStr h cc_injects
140 141
	  when stub_h_exists $ 
	     hPutStrLn h ("#include \"" ++ (hscStubHOutName dflags) ++ "\"")
142
	  writeRealC h flat_absC
143 144 145 146 147 148 149 150 151 152
\end{code}


%************************************************************************
%*									*
\subsection{Assembler}
%*									*
%************************************************************************

\begin{code}
153
outputAsm dflags filenm flat_absC
154

155
#ifndef OMIT_NATIVE_CODEGEN
156

157
  = do ncg_uniqs <- mkSplitUniqSupply 'n'
158 159
       let (stix_final, ncg_output_d) = _scc_ "NativeCodeGen" 
				        nativeCodeGen flat_absC ncg_uniqs
160
       dumpIfSet_dyn dflags Opt_D_dump_stix "Final stix code" stix_final
161 162 163
       dumpIfSet_dyn dflags Opt_D_dump_asm "Asm code" (docToSDoc ncg_output_d)
       _scc_ "OutputAsm" doOutput filenm $
	   \f -> printDoc LeftMode f ncg_output_d
164 165 166 167
  where

#else /* OMIT_NATIVE_CODEGEN */

168 169
  = pprPanic "This compiler was built without a native code generator"
	     (text "Use -fvia-C instead")
170 171 172

#endif
\end{code}
173 174


175 176 177 178 179
%************************************************************************
%*									*
\subsection{Java}
%*									*
%************************************************************************
180

181
\begin{code}
182
#ifdef JAVA
183
outputJava dflags filenm mod tycons core_binds
184
  = doOutput filenm (\ f -> printForUser f alwaysQualify pp_java)
185
	-- User style printing for now to keep indentation
186
  where
187 188 189
    occ_anal_binds = occurAnalyseBinds core_binds
	-- Make sure we have up to date dead-var information
    java_code = javaGen mod [{- Should be imports-}] tycons occ_anal_binds
190
    pp_java   = PrintJava.compilationUnit java_code
191
#endif
192
\end{code}
193

194

rrt's avatar
rrt committed
195 196 197 198 199 200 201 202
%************************************************************************
%*									*
\subsection{Ilx}
%*									*
%************************************************************************

\begin{code}
#ifdef ILX
203 204
outputIlx dflags filename mod tycons stg_binds
  =  doOutput filename (\ f -> printForC f pp_ilx)
rrt's avatar
rrt committed
205 206 207 208 209 210
  where
    pp_ilx = ilxGen mod tycons stg_binds
#endif
\end{code}


211 212 213 214 215 216 217
%************************************************************************
%*									*
\subsection{Foreign import/export}
%*									*
%************************************************************************

\begin{code}
218 219 220 221
outputForeignStubs :: DynFlags -> ForeignStubs
		   -> IO (Bool, 	-- Header file created
			  Bool)		-- C file created
outputForeignStubs dflags NoStubs = return (False, False)
222
outputForeignStubs dflags (ForeignStubs h_code c_code _ _)
223
  = do
224
	dumpIfSet_dyn dflags Opt_D_dump_foreign
225 226
                      "Foreign export header file" stub_h_output_d

227 228
	stub_h_file_exists
           <- outputForeignStubs_help (hscStubHOutName dflags) stub_h_output_w
229
		("#include \"HsFFI.h\"\n" ++ cplusplus_hdr) cplusplus_ftr
230

231
	dumpIfSet_dyn dflags Opt_D_dump_foreign
232 233
                      "Foreign export stubs" stub_c_output_d

234 235
	stub_c_file_exists
           <- outputForeignStubs_help (hscStubCOutName dflags) stub_c_output_w
236
		("#define IN_STG_CODE 0\n" ++ 
237 238 239
		 "#include \"RtsAPI.h\"\n" ++
		 cplusplus_hdr)
		 cplusplus_ftr
240
	   -- We're adding the default hc_header to the stub file, but this
241 242
	   -- isn't really HC code, so we need to define IN_STG_CODE==0 to
	   -- avoid the register variables etc. being enabled.
243 244

        return (stub_h_file_exists, stub_c_file_exists)
245
  where
246 247 248 249 250 251 252 253
    -- C stubs for "foreign export"ed functions.
    stub_c_output_d = pprCode CStyle c_code
    stub_c_output_w = showSDoc stub_c_output_d

    -- Header file protos for "foreign export"ed functions.
    stub_h_output_d = pprCode CStyle h_code
    stub_h_output_w = showSDoc stub_h_output_d

254 255
cplusplus_hdr = "#ifdef __cplusplus\nextern \"C\" {\n#endif\n"
cplusplus_ftr = "#ifdef __cplusplus\n}\n#endif\n"
256

257 258 259
-- Don't use doOutput for dumping the f. export stubs
-- since it is more than likely that the stubs file will
-- turn out to be empty, in which case no file should be created.
260 261 262
outputForeignStubs_help fname ""      header footer = return False
outputForeignStubs_help fname doc_str header footer
   = do writeFile fname (header ++ doc_str ++ '\n':footer ++ "\n")
263
        return True
264 265
\end{code}