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 AsmCodeGen ( nativeCodeGen )
14 import LlvmCodeGen ( llvmCodeGen )
16 import UniqSupply ( mkSplitUniqSupply )
19 import JavaGen ( javaGen )
20 import qualified PrintJava
21 import OccurAnal ( occurAnalyseBinds )
24 import Finder ( mkStubPaths )
25 import PprC ( writeCs )
26 import CmmLint ( cmmLint )
29 import OldCmm ( RawCmm )
34 import ErrUtils ( dumpIfSet_dyn, showPass, ghcExit )
37 import Maybes ( firstJusts )
39 import Control.Exception
41 import System.Directory
42 import System.FilePath
46 %************************************************************************
50 %************************************************************************
53 codeOutput :: DynFlags
58 -> [RawCmm] -- Compiled C--
59 -> IO (Bool{-stub_h_exists-}, Bool{-stub_c_exists-})
61 codeOutput dflags this_mod location foreign_stubs pkg_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 firstJusts lints of
73 Just err -> do { printDump err
79 ; showPass dflags "CodeOutput"
80 ; let filenm = hscOutName dflags
81 ; stubs_exist <- outputForeignStubs dflags this_mod location foreign_stubs
82 ; case hscTarget dflags of {
83 HscInterpreted -> return ();
84 HscAsm -> outputAsm dflags filenm flat_abstractC;
85 HscC -> outputC dflags filenm flat_abstractC pkg_deps;
86 HscLlvm -> outputLlvm dflags filenm flat_abstractC;
89 outputJava dflags filenm mod_name tycons core_binds;
91 panic "Java support not compiled into this ghc";
93 HscNothing -> panic "codeOutput: HscNothing"
98 doOutput :: String -> (Handle -> IO ()) -> IO ()
99 doOutput filenm io_action = bracket (openFile filenm WriteMode) hClose io_action
103 %************************************************************************
107 %************************************************************************
116 outputC dflags filenm flat_absC packages
118 -- figure out which header files to #include in the generated .hc file:
120 -- * extra_includes from packages
121 -- * -#include options from the cmdline and OPTIONS pragmas
122 -- * the _stub.h file, if there is one.
124 let rts = getPackageDetails (pkgState dflags) rtsPackageId
126 let cc_injects = unlines (map mk_include (includes rts))
129 '"':_{-"-} -> "#include "++h_file
130 '<':_ -> "#include "++h_file
131 _ -> "#include \""++h_file++"\""
133 pkg_configs <- getPreloadPackagesAnd dflags packages
134 let pkg_names = map (display.sourcePackageId) pkg_configs
136 doOutput filenm $ \ h -> do
137 hPutStr h ("/* GHC_PACKAGES " ++ unwords pkg_names ++ "\n*/\n")
139 writeCs dflags h flat_absC
143 %************************************************************************
145 \subsection{Assembler}
147 %************************************************************************
150 outputAsm :: DynFlags -> FilePath -> [RawCmm] -> IO ()
152 #ifndef OMIT_NATIVE_CODEGEN
154 outputAsm dflags filenm flat_absC
155 = do ncg_uniqs <- mkSplitUniqSupply 'n'
157 {-# SCC "OutputAsm" #-} doOutput filenm $
158 \f -> {-# SCC "NativeCodeGen" #-}
159 nativeCodeGen dflags f ncg_uniqs flat_absC
162 #else /* OMIT_NATIVE_CODEGEN */
165 = pprPanic "This compiler was built without a native code generator"
166 (text "Use -fvia-C instead")
172 %************************************************************************
176 %************************************************************************
179 outputLlvm :: DynFlags -> FilePath -> [RawCmm] -> IO ()
180 outputLlvm dflags filenm flat_absC
181 = do ncg_uniqs <- mkSplitUniqSupply 'n'
182 doOutput filenm $ \f -> llvmCodeGen dflags f ncg_uniqs flat_absC
186 %************************************************************************
190 %************************************************************************
194 outputJava dflags filenm mod tycons core_binds
195 = doOutput filenm (\ f -> printForUser f alwaysQualify pp_java)
196 -- User style printing for now to keep indentation
198 occ_anal_binds = occurAnalyseBinds core_binds
199 -- Make sure we have up to date dead-var information
200 java_code = javaGen mod [{- Should be imports-}] tycons occ_anal_binds
201 pp_java = PrintJava.compilationUnit java_code
206 %************************************************************************
208 \subsection{Foreign import/export}
210 %************************************************************************
213 outputForeignStubs :: DynFlags -> Module -> ModLocation -> ForeignStubs
214 -> IO (Bool, -- Header file created
215 Bool) -- C file created
216 outputForeignStubs dflags mod location stubs
219 -- When compiling External Core files, may need to use stub
220 -- files from a previous compilation
221 stub_c_exists <- doesFileExist stub_c
222 stub_h_exists <- doesFileExist stub_h
223 return (stub_h_exists, stub_c_exists)
225 ForeignStubs h_code c_code -> do
227 stub_c_output_d = pprCode CStyle c_code
228 stub_c_output_w = showSDoc stub_c_output_d
230 -- Header file protos for "foreign export"ed functions.
231 stub_h_output_d = pprCode CStyle h_code
232 stub_h_output_w = showSDoc stub_h_output_d
235 createDirectoryHierarchy (takeDirectory stub_c)
237 dumpIfSet_dyn dflags Opt_D_dump_foreign
238 "Foreign export header file" stub_h_output_d
240 -- we need the #includes from the rts package for the stub files
242 let rts_pkg = getPackageDetails (pkgState dflags) rtsPackageId in
243 concatMap mk_include (includes rts_pkg)
244 mk_include i = "#include \"" ++ i ++ "\"\n"
246 -- wrapper code mentions the ffi_arg type, which comes from ffi.h
247 ffi_includes | cLibFFI = "#include \"ffi.h\"\n"
251 <- outputForeignStubs_help stub_h 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 stub_c stub_c_output_w
259 ("#define IN_STG_CODE 0\n" ++
260 "#include \"Rts.h\"\n" ++
265 -- We're adding the default hc_header to the stub file, but this
266 -- isn't really HC code, so we need to define IN_STG_CODE==0 to
267 -- avoid the register variables etc. being enabled.
269 return (stub_h_file_exists, stub_c_file_exists)
271 (stub_c, stub_h, _) = mkStubPaths dflags (moduleName mod) location
273 cplusplus_hdr = "#ifdef __cplusplus\nextern \"C\" {\n#endif\n"
274 cplusplus_ftr = "#ifdef __cplusplus\n}\n#endif\n"
277 -- Don't use doOutput for dumping the f. export stubs
278 -- since it is more than likely that the stubs file will
279 -- turn out to be empty, in which case no file should be created.
280 outputForeignStubs_help :: FilePath -> String -> String -> String -> IO Bool
281 outputForeignStubs_help _fname "" _header _footer = return False
282 outputForeignStubs_help fname doc_str header footer
283 = do writeFile fname (header ++ doc_str ++ '\n':footer ++ "\n")