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 )
27 import PprC ( writeCs )
28 import CmmLint ( cmmLint )
30 import DriverState ( getExplicitPackagesAnd, getPackageCIncludes )
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 )
43 import Data.List ( intersperse )
48 %************************************************************************
52 %************************************************************************
55 codeOutput :: DynFlags
59 -> [Cmm] -- Compiled C--
60 -> IO (Bool{-stub_h_exists-}, Bool{-stub_c_exists-})
62 codeOutput dflags this_mod foreign_stubs deps flat_abstractC
64 -- You can have C (c_output) or assembly-language (ncg_output),
65 -- but not both. [Allowing for both gives a space leak on
66 -- flat_abstractC. WDP 94/10]
68 -- Dunno if the above comment is still meaningful now. JRS 001024.
70 do { when (dopt Opt_DoCmmLinting dflags) $ do
71 { showPass dflags "CmmLint"
72 ; let lints = map cmmLint flat_abstractC
73 ; case firstJust lints of
74 Just err -> do { printDump err
80 ; showPass dflags "CodeOutput"
81 ; let filenm = dopt_OutName dflags
82 ; stubs_exist <- outputForeignStubs dflags foreign_stubs
83 ; case dopt_HscLang dflags of {
84 HscInterpreted -> return ();
85 HscAsm -> outputAsm dflags filenm flat_abstractC;
86 HscC -> outputC dflags filenm flat_abstractC stubs_exist
90 outputJava dflags filenm mod_name tycons core_binds;
92 panic "Java support not compiled into this ghc";
96 let tycons = typeEnvTyCons type_env in
97 outputIlx dflags filenm mod_name tycons stg_binds;
99 panic "ILX support not compiled into this ghc";
105 doOutput :: String -> (Handle -> IO ()) -> IO ()
106 doOutput filenm io_action = bracket (openFile filenm WriteMode) hClose io_action
110 %************************************************************************
114 %************************************************************************
117 outputC dflags filenm flat_absC
118 (stub_h_exists, _) dependencies foreign_stubs
120 -- figure out which header files to #include in the generated .hc file:
122 -- * extra_includes from packages
123 -- * -#include options from the cmdline and OPTIONS pragmas
124 -- * the _stub.h file, if there is one.
126 let packages = dep_pkgs dependencies
127 pkg_configs <- getExplicitPackagesAnd packages
128 let pkg_names = map name pkg_configs
130 c_includes <- getPackageCIncludes pkg_configs
131 let cmdline_includes = cmdlineHcIncludes dflags -- -#include options
134 = case foreign_stubs of
136 ForeignStubs _ _ fdhs _ -> map unpackFS (fst (removeDupsEq fdhs))
137 -- Remove duplicates, because distinct foreign import decls
138 -- may cite the same #include. Order doesn't matter.
140 all_headers = c_includes
141 ++ reverse cmdline_includes
144 let cc_injects = unlines (map mk_include all_headers)
147 '"':_{-"-} -> "#include "++h_file
148 '<':_ -> "#include "++h_file
149 _ -> "#include \""++h_file++"\""
151 doOutput filenm $ \ h -> do
152 hPutStr h ("/* GHC_PACKAGES " ++ unwords pkg_names ++ "\n*/\n")
155 hPutStrLn h ("#include \"" ++ (hscStubHOutName dflags) ++ "\"")
160 %************************************************************************
162 \subsection{Assembler}
164 %************************************************************************
167 outputAsm dflags filenm flat_absC
169 #ifndef OMIT_NATIVE_CODEGEN
171 = do ncg_uniqs <- mkSplitUniqSupply 'n'
172 ncg_output_d <- _scc_ "NativeCodeGen"
173 nativeCodeGen dflags flat_absC ncg_uniqs
174 dumpIfSet_dyn dflags Opt_D_dump_asm "Asm code" (docToSDoc ncg_output_d)
175 _scc_ "OutputAsm" doOutput filenm $
176 \f -> printDoc LeftMode f ncg_output_d
179 #else /* OMIT_NATIVE_CODEGEN */
181 = pprPanic "This compiler was built without a native code generator"
182 (text "Use -fvia-C instead")
188 %************************************************************************
192 %************************************************************************
196 outputJava dflags filenm mod tycons core_binds
197 = doOutput filenm (\ f -> printForUser f alwaysQualify pp_java)
198 -- User style printing for now to keep indentation
200 occ_anal_binds = occurAnalyseBinds core_binds
201 -- Make sure we have up to date dead-var information
202 java_code = javaGen mod [{- Should be imports-}] tycons occ_anal_binds
203 pp_java = PrintJava.compilationUnit java_code
208 %************************************************************************
212 %************************************************************************
216 outputIlx dflags filename mod tycons stg_binds
217 = doOutput filename (\ f -> printForC f pp_ilx)
219 pp_ilx = ilxGen mod tycons stg_binds
224 %************************************************************************
226 \subsection{Foreign import/export}
228 %************************************************************************
231 outputForeignStubs :: DynFlags -> ForeignStubs
232 -> IO (Bool, -- Header file created
233 Bool) -- C file created
234 outputForeignStubs dflags NoStubs = do
235 -- When compiling External Core files, may need to use stub files from a
236 -- previous compilation
237 hFileExists <- doesFileExist (hscStubHOutName dflags)
238 cFileExists <- doesFileExist (hscStubCOutName dflags)
239 return (hFileExists, cFileExists)
240 outputForeignStubs dflags (ForeignStubs h_code c_code _ _)
242 dumpIfSet_dyn dflags Opt_D_dump_foreign
243 "Foreign export header file" stub_h_output_d
245 -- we need the #includes from the rts package for the stub files
246 rts_pkgs <- getPackageDetails [rtsPackage]
247 let rts_includes = concatMap mk_include (concatMap c_includes rts_pkgs)
248 mk_include i = "#include \"" ++ i ++ "\"\n"
251 <- outputForeignStubs_help (hscStubHOutName dflags) stub_h_output_w
252 ("#include \"HsFFI.h\"\n" ++ cplusplus_hdr) cplusplus_ftr
254 dumpIfSet_dyn dflags Opt_D_dump_foreign
255 "Foreign export stubs" stub_c_output_d
258 <- outputForeignStubs_help (hscStubCOutName dflags) stub_c_output_w
259 ("#define IN_STG_CODE 0\n" ++
260 "#include \"Rts.h\"\n" ++
264 -- We're adding the default hc_header to the stub file, but this
265 -- isn't really HC code, so we need to define IN_STG_CODE==0 to
266 -- avoid the register variables etc. being enabled.
268 return (stub_h_file_exists, stub_c_file_exists)
270 -- C stubs for "foreign export"ed functions.
271 stub_c_output_d = pprCode CStyle c_code
272 stub_c_output_w = showSDoc stub_c_output_d
274 -- Header file protos for "foreign export"ed functions.
275 stub_h_output_d = pprCode CStyle h_code
276 stub_h_output_w = showSDoc stub_h_output_d
278 cplusplus_hdr = "#ifdef __cplusplus\nextern \"C\" {\n#endif\n"
279 cplusplus_ftr = "#ifdef __cplusplus\n}\n#endif\n"
281 -- Don't use doOutput for dumping the f. export stubs
282 -- since it is more than likely that the stubs file will
283 -- turn out to be empty, in which case no file should be created.
284 outputForeignStubs_help fname "" header footer = return False
285 outputForeignStubs_help fname doc_str header footer
286 = do writeFile fname (header ++ doc_str ++ '\n':footer ++ "\n")