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 Finder ( mkStubPaths )
27 import PprC ( writeCs )
28 import CmmLint ( cmmLint )
30 import PackageConfig ( rtsPackageId )
32 import FastString ( unpackFS )
36 import ErrUtils ( dumpIfSet_dyn, showPass, ghcExit )
38 import Pretty ( Mode(..), printDoc )
39 import Module ( Module, ModLocation(..), moduleName )
41 import Maybes ( firstJust )
43 import Distribution.Package ( showPackageId )
44 import Directory ( doesFileExist )
49 %************************************************************************
53 %************************************************************************
56 codeOutput :: DynFlags
61 -> [Cmm] -- Compiled C--
62 -> IO (Bool{-stub_h_exists-}, Bool{-stub_c_exists-})
64 codeOutput dflags this_mod location foreign_stubs pkg_deps flat_abstractC
66 -- You can have C (c_output) or assembly-language (ncg_output),
67 -- but not both. [Allowing for both gives a space leak on
68 -- flat_abstractC. WDP 94/10]
70 -- Dunno if the above comment is still meaningful now. JRS 001024.
72 do { when (dopt Opt_DoCmmLinting dflags) $ do
73 { showPass dflags "CmmLint"
74 ; let lints = map cmmLint flat_abstractC
75 ; case firstJust lints of
76 Just err -> do { printDump err
82 ; showPass dflags "CodeOutput"
83 ; let filenm = hscOutName dflags
84 ; stubs_exist <- outputForeignStubs dflags this_mod location foreign_stubs
85 ; case hscTarget dflags of {
86 HscInterpreted -> return ();
87 HscAsm -> outputAsm dflags filenm flat_abstractC;
88 HscC -> outputC dflags filenm this_mod location
89 flat_abstractC stubs_exist pkg_deps
93 outputJava dflags filenm mod_name tycons core_binds;
95 panic "Java support not compiled into this ghc";
99 let tycons = typeEnvTyCons type_env in
100 outputIlx dflags filenm mod_name tycons stg_binds;
102 panic "ILX support not compiled into this ghc";
108 doOutput :: String -> (Handle -> IO ()) -> IO ()
109 doOutput filenm io_action = bracket (openFile filenm WriteMode) hClose io_action
113 %************************************************************************
117 %************************************************************************
120 outputC dflags filenm mod location flat_absC
121 (stub_h_exists, _) packages foreign_stubs
123 -- figure out which header files to #include in the generated .hc file:
125 -- * extra_includes from packages
126 -- * -#include options from the cmdline and OPTIONS pragmas
127 -- * the _stub.h file, if there is one.
129 pkg_configs <- getPreloadPackagesAnd dflags packages
130 let pkg_names = map (showPackageId.package) pkg_configs
132 c_includes <- getPackageCIncludes pkg_configs
133 let cmdline_includes = cmdlineHcIncludes dflags -- -#include options
136 = case foreign_stubs of
138 ForeignStubs _ _ fdhs _ -> map unpackFS (nub fdhs)
139 -- Remove duplicates, because distinct foreign import decls
140 -- may cite the same #include. Order doesn't matter.
142 all_headers = c_includes
143 ++ reverse cmdline_includes
146 let cc_injects = unlines (map mk_include all_headers)
149 '"':_{-"-} -> "#include "++h_file
150 '<':_ -> "#include "++h_file
151 _ -> "#include \""++h_file++"\""
153 doOutput filenm $ \ h -> do
154 hPutStr h ("/* GHC_PACKAGES " ++ unwords pkg_names ++ "\n*/\n")
157 hPutStrLn h ("#include \"" ++ (filenameOf stub_h) ++ "\"")
158 writeCs dflags h flat_absC
160 (_, stub_h) = mkStubPaths dflags (moduleName mod) location
164 %************************************************************************
166 \subsection{Assembler}
168 %************************************************************************
171 outputAsm dflags filenm flat_absC
173 #ifndef OMIT_NATIVE_CODEGEN
175 = do ncg_uniqs <- mkSplitUniqSupply 'n'
176 ncg_output_d <- _scc_ "NativeCodeGen"
177 nativeCodeGen dflags flat_absC ncg_uniqs
178 dumpIfSet_dyn dflags Opt_D_dump_asm "Asm code" (docToSDoc ncg_output_d)
179 _scc_ "OutputAsm" doOutput filenm $
180 \f -> printDoc LeftMode f ncg_output_d
183 #else /* OMIT_NATIVE_CODEGEN */
185 = pprPanic "This compiler was built without a native code generator"
186 (text "Use -fvia-C instead")
192 %************************************************************************
196 %************************************************************************
200 outputJava dflags filenm mod tycons core_binds
201 = doOutput filenm (\ f -> printForUser f alwaysQualify pp_java)
202 -- User style printing for now to keep indentation
204 occ_anal_binds = occurAnalyseBinds core_binds
205 -- Make sure we have up to date dead-var information
206 java_code = javaGen mod [{- Should be imports-}] tycons occ_anal_binds
207 pp_java = PrintJava.compilationUnit java_code
212 %************************************************************************
216 %************************************************************************
220 outputIlx dflags filename mod tycons stg_binds
221 = doOutput filename (\ f -> printForC f pp_ilx)
223 pp_ilx = ilxGen mod tycons stg_binds
228 %************************************************************************
230 \subsection{Foreign import/export}
232 %************************************************************************
235 outputForeignStubs :: DynFlags -> Module -> ModLocation -> ForeignStubs
236 -> IO (Bool, -- Header file created
237 Bool) -- C file created
238 outputForeignStubs dflags mod location stubs
239 | NoStubs <- stubs = do
240 -- When compiling External Core files, may need to use stub
241 -- files from a previous compilation
242 stub_c_exists <- doesFileExist stub_c
243 stub_h_exists <- doesFileExist stub_h
244 return (stub_h_exists, stub_c_exists)
246 | ForeignStubs h_code c_code _ _ <- stubs
249 stub_c_output_d = pprCode CStyle c_code
250 stub_c_output_w = showSDoc stub_c_output_d
252 -- Header file protos for "foreign export"ed functions.
253 stub_h_output_d = pprCode CStyle h_code
254 stub_h_output_w = showSDoc stub_h_output_d
257 createDirectoryHierarchy (directoryOf stub_c)
259 dumpIfSet_dyn dflags Opt_D_dump_foreign
260 "Foreign export header file" stub_h_output_d
262 -- we need the #includes from the rts package for the stub files
264 let rts_pkg = getPackageDetails (pkgState dflags) rtsPackageId in
265 concatMap mk_include (includes rts_pkg)
266 mk_include i = "#include \"" ++ i ++ "\"\n"
269 <- outputForeignStubs_help stub_h stub_h_output_w
270 ("#include \"HsFFI.h\"\n" ++ cplusplus_hdr) cplusplus_ftr
272 dumpIfSet_dyn dflags Opt_D_dump_foreign
273 "Foreign export stubs" stub_c_output_d
276 <- outputForeignStubs_help stub_c stub_c_output_w
277 ("#define IN_STG_CODE 0\n" ++
278 "#include \"Rts.h\"\n" ++
282 -- We're adding the default hc_header to the stub file, but this
283 -- isn't really HC code, so we need to define IN_STG_CODE==0 to
284 -- avoid the register variables etc. being enabled.
286 return (stub_h_file_exists, stub_c_file_exists)
288 (stub_c, stub_h) = mkStubPaths dflags (moduleName mod) location
290 cplusplus_hdr = "#ifdef __cplusplus\nextern \"C\" {\n#endif\n"
291 cplusplus_ftr = "#ifdef __cplusplus\n}\n#endif\n"
293 -- Don't use doOutput for dumping the f. export stubs
294 -- since it is more than likely that the stubs file will
295 -- turn out to be empty, in which case no file should be created.
296 outputForeignStubs_help fname "" header footer = return False
297 outputForeignStubs_help fname doc_str header footer
298 = do writeFile fname (header ++ doc_str ++ '\n':footer ++ "\n")