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 qualified PrintJava
23 import OccurAnal ( occurAnalyseBinds )
26 import Distribution.Package ( showPackageId )
27 import PprC ( writeCs )
28 import CmmLint ( cmmLint )
30 import Util ( filenameOf )
31 import FastString ( unpackFS )
35 import ErrUtils ( dumpIfSet_dyn, showPass, ghcExit )
37 import Pretty ( Mode(..), printDoc )
38 import Module ( Module )
39 import ListSetOps ( removeDupsEq )
40 import Maybes ( firstJust )
42 import Directory ( doesFileExist )
47 %************************************************************************
51 %************************************************************************
54 codeOutput :: DynFlags
58 -> [Cmm] -- Compiled C--
59 -> IO (Bool{-stub_h_exists-}, Bool{-stub_c_exists-})
61 codeOutput dflags this_mod foreign_stubs deps flat_abstractC
63 -- You can have C (c_output) or assembly-language (ncg_output),
64 -- but not both. [Allowing for both gives a space leak on
65 -- flat_abstractC. WDP 94/10]
67 -- Dunno if the above comment is still meaningful now. JRS 001024.
69 do { when (dopt Opt_DoCmmLinting dflags) $ do
70 { showPass dflags "CmmLint"
71 ; let lints = map cmmLint flat_abstractC
72 ; case firstJust lints of
73 Just err -> do { printDump err
79 ; showPass dflags "CodeOutput"
80 ; let filenm = hscOutName dflags
81 ; stubs_exist <- outputForeignStubs dflags foreign_stubs
82 ; case hscTarget dflags of {
83 HscInterpreted -> return ();
84 HscAsm -> outputAsm dflags filenm flat_abstractC;
85 HscC -> outputC dflags filenm flat_abstractC stubs_exist
89 outputJava dflags filenm mod_name tycons core_binds;
91 panic "Java support not compiled into this ghc";
95 let tycons = typeEnvTyCons type_env in
96 outputIlx dflags filenm mod_name tycons stg_binds;
98 panic "ILX support not compiled into this ghc";
104 doOutput :: String -> (Handle -> IO ()) -> IO ()
105 doOutput filenm io_action = bracket (openFile filenm WriteMode) hClose io_action
109 %************************************************************************
113 %************************************************************************
116 outputC dflags filenm flat_absC
117 (stub_h_exists, _) dependencies foreign_stubs
119 -- figure out which header files to #include in the generated .hc file:
121 -- * extra_includes from packages
122 -- * -#include options from the cmdline and OPTIONS pragmas
123 -- * the _stub.h file, if there is one.
125 let packages = dep_pkgs dependencies
126 pkg_configs <- getExplicitPackagesAnd dflags packages
127 let pkg_names = map (showPackageId.package) pkg_configs
129 c_includes <- getPackageCIncludes pkg_configs
130 let cmdline_includes = cmdlineHcIncludes dflags -- -#include options
133 = case foreign_stubs of
135 ForeignStubs _ _ fdhs _ -> map unpackFS (fst (removeDupsEq fdhs))
136 -- Remove duplicates, because distinct foreign import decls
137 -- may cite the same #include. Order doesn't matter.
139 all_headers = c_includes
140 ++ reverse cmdline_includes
143 let cc_injects = unlines (map mk_include all_headers)
146 '"':_{-"-} -> "#include "++h_file
147 '<':_ -> "#include "++h_file
148 _ -> "#include \""++h_file++"\""
150 doOutput filenm $ \ h -> do
151 hPutStr h ("/* GHC_PACKAGES " ++ unwords pkg_names ++ "\n*/\n")
154 hPutStrLn h ("#include \"" ++ (filenameOf (hscStubHOutName dflags)) ++ "\"")
159 %************************************************************************
161 \subsection{Assembler}
163 %************************************************************************
166 outputAsm dflags filenm flat_absC
168 #ifndef OMIT_NATIVE_CODEGEN
170 = do ncg_uniqs <- mkSplitUniqSupply 'n'
171 ncg_output_d <- _scc_ "NativeCodeGen"
172 nativeCodeGen dflags flat_absC ncg_uniqs
173 dumpIfSet_dyn dflags Opt_D_dump_asm "Asm code" (docToSDoc ncg_output_d)
174 _scc_ "OutputAsm" doOutput filenm $
175 \f -> printDoc LeftMode f ncg_output_d
178 #else /* OMIT_NATIVE_CODEGEN */
180 = pprPanic "This compiler was built without a native code generator"
181 (text "Use -fvia-C instead")
187 %************************************************************************
191 %************************************************************************
195 outputJava dflags filenm mod tycons core_binds
196 = doOutput filenm (\ f -> printForUser f alwaysQualify pp_java)
197 -- User style printing for now to keep indentation
199 occ_anal_binds = occurAnalyseBinds core_binds
200 -- Make sure we have up to date dead-var information
201 java_code = javaGen mod [{- Should be imports-}] tycons occ_anal_binds
202 pp_java = PrintJava.compilationUnit java_code
207 %************************************************************************
211 %************************************************************************
215 outputIlx dflags filename mod tycons stg_binds
216 = doOutput filename (\ f -> printForC f pp_ilx)
218 pp_ilx = ilxGen mod tycons stg_binds
223 %************************************************************************
225 \subsection{Foreign import/export}
227 %************************************************************************
230 outputForeignStubs :: DynFlags -> ForeignStubs
231 -> IO (Bool, -- Header file created
232 Bool) -- C file created
233 outputForeignStubs dflags NoStubs = do
234 -- When compiling External Core files, may need to use stub files from a
235 -- previous compilation
236 hFileExists <- doesFileExist (hscStubHOutName dflags)
237 cFileExists <- doesFileExist (hscStubCOutName dflags)
238 return (hFileExists, cFileExists)
239 outputForeignStubs dflags (ForeignStubs h_code c_code _ _)
241 dumpIfSet_dyn dflags Opt_D_dump_foreign
242 "Foreign export header file" stub_h_output_d
244 -- we need the #includes from the rts package for the stub files
245 let rtsid = rtsPackageId (pkgState dflags)
247 | ExtPackage pid <- rtsid =
248 let rts_pkg = getPackageDetails (pkgState dflags) pid in
249 concatMap mk_include (includes rts_pkg)
251 mk_include i = "#include \"" ++ i ++ "\"\n"
254 <- outputForeignStubs_help (hscStubHOutName dflags) stub_h_output_w
255 ("#include \"HsFFI.h\"\n" ++ cplusplus_hdr) cplusplus_ftr
257 dumpIfSet_dyn dflags Opt_D_dump_foreign
258 "Foreign export stubs" stub_c_output_d
261 <- outputForeignStubs_help (hscStubCOutName dflags) stub_c_output_w
262 ("#define IN_STG_CODE 0\n" ++
263 "#include \"Rts.h\"\n" ++
267 -- We're adding the default hc_header to the stub file, but this
268 -- isn't really HC code, so we need to define IN_STG_CODE==0 to
269 -- avoid the register variables etc. being enabled.
271 return (stub_h_file_exists, stub_c_file_exists)
273 -- C stubs for "foreign export"ed functions.
274 stub_c_output_d = pprCode CStyle c_code
275 stub_c_output_w = showSDoc stub_c_output_d
277 -- Header file protos for "foreign export"ed functions.
278 stub_h_output_d = pprCode CStyle h_code
279 stub_h_output_w = showSDoc stub_h_output_d
281 cplusplus_hdr = "#ifdef __cplusplus\nextern \"C\" {\n#endif\n"
282 cplusplus_ftr = "#ifdef __cplusplus\n}\n#endif\n"
284 -- Don't use doOutput for dumping the f. export stubs
285 -- since it is more than likely that the stubs file will
286 -- turn out to be empty, in which case no file should be created.
287 outputForeignStubs_help fname "" header footer = return False
288 outputForeignStubs_help fname doc_str header footer
289 = do writeFile fname (header ++ doc_str ++ '\n':footer ++ "\n")