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 )
38 import ListSetOps ( removeDupsEq )
40 import Directory ( doesFileExist )
46 %************************************************************************
50 %************************************************************************
53 codeOutput :: DynFlags
57 -> AbstractC -- Compiled abstract C
58 -> IO (Bool{-stub_h_exists-}, Bool{-stub_c_exists-})
60 codeOutput dflags this_mod foreign_stubs deps flat_abstractC
62 -- You can have C (c_output) or assembly-language (ncg_output),
63 -- but not both. [Allowing for both gives a space leak on
64 -- flat_abstractC. WDP 94/10]
66 -- Dunno if the above comment is still meaningful now. JRS 001024.
68 do { showPass dflags "CodeOutput"
69 ; let filenm = dopt_OutName dflags
70 ; stubs_exist <- outputForeignStubs dflags foreign_stubs
71 ; case dopt_HscLang dflags of {
72 HscInterpreted -> return ();
73 HscAsm -> outputAsm dflags filenm flat_abstractC;
74 HscC -> outputC dflags filenm flat_abstractC stubs_exist
78 outputJava dflags filenm mod_name tycons core_binds;
80 panic "Java support not compiled into this ghc";
84 let tycons = typeEnvTyCons type_env in
85 outputIlx dflags filenm mod_name tycons stg_binds;
87 panic "ILX support not compiled into this ghc";
93 doOutput :: String -> (Handle -> IO ()) -> IO ()
94 doOutput filenm io_action = bracket (openFile filenm WriteMode) hClose io_action
98 %************************************************************************
102 %************************************************************************
105 outputC dflags filenm flat_absC
106 (stub_h_exists, _) dependencies foreign_stubs
107 = do dumpIfSet_dyn dflags Opt_D_dump_realC "Real C" (dumpRealC flat_absC)
109 -- figure out which header files to #include in the generated .hc file:
111 -- * extra_includes from packages
112 -- * -#include options from the cmdline and OPTIONS pragmas
113 -- * the _stub.h file, if there is one.
115 let packages = dep_pkgs dependencies
116 pkg_configs <- getExplicitPackagesAnd packages
117 let pkg_names = map name pkg_configs
119 c_includes <- getPackageCIncludes pkg_configs
120 let cmdline_includes = cmdlineHcIncludes dflags -- -#include options
123 = case foreign_stubs of
125 ForeignStubs _ _ fdhs _ -> map unpackFS (fst (removeDupsEq fdhs))
126 -- Remove duplicates, because distinct foreign import decls
127 -- may cite the same #include. Order doesn't matter.
129 all_headers = c_includes
130 ++ reverse cmdline_includes
133 let cc_injects = unlines (map mk_include all_headers)
136 '"':_{-"-} -> "#include "++h_file
137 '<':_ -> "#include "++h_file
138 _ -> "#include \""++h_file++"\""
140 doOutput filenm $ \ h -> do
141 hPutStr h ("/* GHC_PACKAGES " ++ unwords pkg_names ++ "\n*/\n")
144 hPutStrLn h ("#include \"" ++ (hscStubHOutName dflags) ++ "\"")
145 writeRealC h flat_absC
149 %************************************************************************
151 \subsection{Assembler}
153 %************************************************************************
156 outputAsm dflags filenm flat_absC
158 #ifndef OMIT_NATIVE_CODEGEN
160 = do ncg_uniqs <- mkSplitUniqSupply 'n'
161 let (stix_final, ncg_output_d) = _scc_ "NativeCodeGen"
162 nativeCodeGen flat_absC ncg_uniqs
163 dumpIfSet_dyn dflags Opt_D_dump_stix "Final stix code" stix_final
164 dumpIfSet_dyn dflags Opt_D_dump_asm "Asm code" (docToSDoc ncg_output_d)
165 _scc_ "OutputAsm" doOutput filenm $
166 \f -> printDoc LeftMode f ncg_output_d
169 #else /* OMIT_NATIVE_CODEGEN */
171 = pprPanic "This compiler was built without a native code generator"
172 (text "Use -fvia-C instead")
178 %************************************************************************
182 %************************************************************************
186 outputJava dflags filenm mod tycons core_binds
187 = doOutput filenm (\ f -> printForUser f alwaysQualify pp_java)
188 -- User style printing for now to keep indentation
190 occ_anal_binds = occurAnalyseBinds core_binds
191 -- Make sure we have up to date dead-var information
192 java_code = javaGen mod [{- Should be imports-}] tycons occ_anal_binds
193 pp_java = PrintJava.compilationUnit java_code
198 %************************************************************************
202 %************************************************************************
206 outputIlx dflags filename mod tycons stg_binds
207 = doOutput filename (\ f -> printForC f pp_ilx)
209 pp_ilx = ilxGen mod tycons stg_binds
214 %************************************************************************
216 \subsection{Foreign import/export}
218 %************************************************************************
221 outputForeignStubs :: DynFlags -> ForeignStubs
222 -> IO (Bool, -- Header file created
223 Bool) -- C file created
224 outputForeignStubs dflags NoStubs = do
225 -- When compiling External Core files, may need to use stub files from a
226 -- previous compilation
227 hFileExists <- doesFileExist (hscStubHOutName dflags)
228 cFileExists <- doesFileExist (hscStubCOutName dflags)
229 return (hFileExists, cFileExists)
230 outputForeignStubs dflags (ForeignStubs h_code c_code _ _)
232 dumpIfSet_dyn dflags Opt_D_dump_foreign
233 "Foreign export header file" stub_h_output_d
235 -- we need the #includes from the rts package for the stub files
236 rts_pkgs <- getPackageDetails [rtsPackage]
237 let rts_includes = concatMap mk_include (concatMap c_includes rts_pkgs)
238 mk_include i = "#include \"" ++ i ++ "\"\n"
241 <- outputForeignStubs_help (hscStubHOutName dflags) stub_h_output_w
242 ("#include \"HsFFI.h\"\n" ++ cplusplus_hdr) cplusplus_ftr
244 dumpIfSet_dyn dflags Opt_D_dump_foreign
245 "Foreign export stubs" stub_c_output_d
248 <- outputForeignStubs_help (hscStubCOutName dflags) stub_c_output_w
249 ("#define IN_STG_CODE 0\n" ++
250 "#include \"RtsAPI.h\"\n" ++
254 -- We're adding the default hc_header to the stub file, but this
255 -- isn't really HC code, so we need to define IN_STG_CODE==0 to
256 -- avoid the register variables etc. being enabled.
258 return (stub_h_file_exists, stub_c_file_exists)
260 -- C stubs for "foreign export"ed functions.
261 stub_c_output_d = pprCode CStyle c_code
262 stub_c_output_w = showSDoc stub_c_output_d
264 -- Header file protos for "foreign export"ed functions.
265 stub_h_output_d = pprCode CStyle h_code
266 stub_h_output_w = showSDoc stub_h_output_d
268 cplusplus_hdr = "#ifdef __cplusplus\nextern \"C\" {\n#endif\n"
269 cplusplus_ftr = "#ifdef __cplusplus\n}\n#endif\n"
271 -- Don't use doOutput for dumping the f. export stubs
272 -- since it is more than likely that the stubs file will
273 -- turn out to be empty, in which case no file should be created.
274 outputForeignStubs_help fname "" header footer = return False
275 outputForeignStubs_help fname doc_str header footer
276 = do writeFile fname (header ++ doc_str ++ '\n':footer ++ "\n")