2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
4 \section{Code output phase}
7 module CodeOutput( codeOutput, outputForeignStubs ) where
9 #include "HsVersions.h"
11 #ifndef OMIT_NATIVE_CODEGEN
12 import UniqSupply ( mkSplitUniqSupply )
13 import AsmCodeGen ( nativeCodeGen )
17 import IlxGen ( ilxGen )
21 import JavaGen ( javaGen )
22 import OccurAnal ( occurAnalyseBinds )
23 import qualified PrintJava
24 import OccurAnal ( occurAnalyseBinds )
28 import DriverState ( getExplicitPackagesAnd, getPackageCIncludes )
29 import FastString ( unpackFS )
30 import AbsCSyn ( AbstractC )
31 import PprAbsC ( dumpRealC, writeRealC )
34 import ErrUtils ( dumpIfSet_dyn, showPass )
36 import Pretty ( Mode(..), printDoc )
37 import Module ( Module )
44 %************************************************************************
48 %************************************************************************
51 codeOutput :: DynFlags
55 -> AbstractC -- Compiled abstract C
56 -> IO (Bool{-stub_h_exists-}, Bool{-stub_c_exists-})
58 codeOutput dflags this_mod foreign_stubs deps flat_abstractC
60 -- You can have C (c_output) or assembly-language (ncg_output),
61 -- but not both. [Allowing for both gives a space leak on
62 -- flat_abstractC. WDP 94/10]
64 -- Dunno if the above comment is still meaningful now. JRS 001024.
66 do { showPass dflags "CodeOutput"
67 ; let filenm = dopt_OutName dflags
68 ; stubs_exist <- outputForeignStubs dflags foreign_stubs
69 ; case dopt_HscLang dflags of {
70 HscInterpreted -> return ();
71 HscAsm -> outputAsm dflags filenm flat_abstractC;
72 HscC -> outputC dflags filenm flat_abstractC stubs_exist
76 outputJava dflags filenm mod_name tycons core_binds;
78 panic "Java support not compiled into this ghc";
82 let tycons = typeEnvTyCons type_env in
83 outputIlx dflags filenm mod_name tycons stg_binds;
85 panic "ILX support not compiled into this ghc";
91 doOutput :: String -> (Handle -> IO ()) -> IO ()
92 doOutput filenm io_action = bracket (openFile filenm WriteMode) hClose io_action
96 %************************************************************************
100 %************************************************************************
103 outputC dflags filenm flat_absC
104 (stub_h_exists, _) dependencies foreign_stubs
105 = do dumpIfSet_dyn dflags Opt_D_dump_realC "Real C" (dumpRealC flat_absC)
107 -- figure out which header files to #include in the generated .hc file:
109 -- * extra_includes from packages
110 -- * -#include options from the cmdline and OPTIONS pragmas
111 -- * the _stub.h file, if there is one.
113 let packages = dep_pkgs dependencies
114 pkg_configs <- getExplicitPackagesAnd packages
115 let pkg_names = map name pkg_configs
117 c_includes <- getPackageCIncludes pkg_configs
118 let cmdline_includes = cmdlineHcIncludes dflags -- -#include options
120 ffi_decl_headers = case foreign_stubs of
122 ForeignStubs _ _ fdhs _ -> fdhs
124 all_headers = c_includes
125 ++ reverse cmdline_includes
126 ++ reverse (map unpackFS ffi_decl_headers)
129 let cc_injects = unlines (map mk_include all_headers)
132 '"':_{-"-} -> "#include "++h_file
133 '<':_ -> "#include "++h_file
134 _ -> "#include \""++h_file++"\""
136 doOutput filenm $ \ h -> do
137 hPutStr h ("/* GHC_PACKAGES " ++ unwords pkg_names ++ "\n*/\n")
140 hPutStrLn h ("#include \"" ++ (hscStubHOutName dflags) ++ "\"")
141 writeRealC h flat_absC
145 %************************************************************************
147 \subsection{Assembler}
149 %************************************************************************
152 outputAsm dflags filenm flat_absC
154 #ifndef OMIT_NATIVE_CODEGEN
156 = do ncg_uniqs <- mkSplitUniqSupply 'n'
157 let (stix_final, ncg_output_d) = _scc_ "NativeCodeGen"
158 nativeCodeGen flat_absC ncg_uniqs
159 dumpIfSet_dyn dflags Opt_D_dump_stix "Final stix code" stix_final
160 dumpIfSet_dyn dflags Opt_D_dump_asm "Asm code" (docToSDoc ncg_output_d)
161 _scc_ "OutputAsm" doOutput filenm $
162 \f -> printDoc LeftMode f ncg_output_d
165 #else /* OMIT_NATIVE_CODEGEN */
167 = pprPanic "This compiler was built without a native code generator"
168 (text "Use -fvia-C instead")
174 %************************************************************************
178 %************************************************************************
182 outputJava dflags filenm mod tycons core_binds
183 = doOutput filenm (\ f -> printForUser f alwaysQualify pp_java)
184 -- User style printing for now to keep indentation
186 occ_anal_binds = occurAnalyseBinds core_binds
187 -- Make sure we have up to date dead-var information
188 java_code = javaGen mod [{- Should be imports-}] tycons occ_anal_binds
189 pp_java = PrintJava.compilationUnit java_code
194 %************************************************************************
198 %************************************************************************
202 outputIlx dflags filename mod tycons stg_binds
203 = doOutput filename (\ f -> printForC f pp_ilx)
205 pp_ilx = ilxGen mod tycons stg_binds
210 %************************************************************************
212 \subsection{Foreign import/export}
214 %************************************************************************
217 outputForeignStubs :: DynFlags -> ForeignStubs
218 -> IO (Bool, -- Header file created
219 Bool) -- C file created
220 outputForeignStubs dflags NoStubs = return (False, False)
221 outputForeignStubs dflags (ForeignStubs h_code c_code _ _)
223 dumpIfSet_dyn dflags Opt_D_dump_foreign
224 "Foreign export header file" stub_h_output_d
226 -- we need the #includes from the rts package for the stub files
227 rts_pkgs <- getPackageDetails [rtsPackage]
228 let rts_includes = concatMap mk_include (concatMap c_includes rts_pkgs)
229 mk_include i = "#include \"" ++ i ++ "\"\n"
232 <- outputForeignStubs_help (hscStubHOutName dflags) stub_h_output_w
233 ("#include \"HsFFI.h\"\n" ++ cplusplus_hdr) cplusplus_ftr
235 dumpIfSet_dyn dflags Opt_D_dump_foreign
236 "Foreign export stubs" stub_c_output_d
239 <- outputForeignStubs_help (hscStubCOutName dflags) stub_c_output_w
240 ("#define IN_STG_CODE 0\n" ++
241 "#include \"RtsAPI.h\"\n" ++
245 -- We're adding the default hc_header to the stub file, but this
246 -- isn't really HC code, so we need to define IN_STG_CODE==0 to
247 -- avoid the register variables etc. being enabled.
249 return (stub_h_file_exists, stub_c_file_exists)
251 -- C stubs for "foreign export"ed functions.
252 stub_c_output_d = pprCode CStyle c_code
253 stub_c_output_w = showSDoc stub_c_output_d
255 -- Header file protos for "foreign export"ed functions.
256 stub_h_output_d = pprCode CStyle h_code
257 stub_h_output_w = showSDoc stub_h_output_d
259 cplusplus_hdr = "#ifdef __cplusplus\nextern \"C\" {\n#endif\n"
260 cplusplus_ftr = "#ifdef __cplusplus\n}\n#endif\n"
262 -- Don't use doOutput for dumping the f. export stubs
263 -- since it is more than likely that the stubs file will
264 -- turn out to be empty, in which case no file should be created.
265 outputForeignStubs_help fname "" header footer = return False
266 outputForeignStubs_help fname doc_str header footer
267 = do writeFile fname (header ++ doc_str ++ '\n':footer ++ "\n")