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 )
15 import UniqSupply ( mkSplitUniqSupply )
16 #ifndef GHCI_TABLES_NEXT_TO_CODE
17 import qualified LlvmCodeGen ( llvmCodeGen )
21 import JavaGen ( javaGen )
22 import qualified PrintJava
23 import OccurAnal ( occurAnalyseBinds )
26 import Finder ( mkStubPaths )
27 import PprC ( writeCs )
28 import CmmLint ( cmmLint )
36 import ErrUtils ( dumpIfSet_dyn, showPass, ghcExit )
39 import Maybes ( firstJust )
41 import Control.Exception
43 import System.Directory
44 import System.FilePath
48 %************************************************************************
52 %************************************************************************
55 codeOutput :: DynFlags
60 -> [RawCmm] -- Compiled C--
61 -> IO (Bool{-stub_h_exists-}, Bool{-stub_c_exists-})
63 codeOutput dflags this_mod location foreign_stubs pkg_deps flat_abstractC
65 -- You can have C (c_output) or assembly-language (ncg_output),
66 -- but not both. [Allowing for both gives a space leak on
67 -- flat_abstractC. WDP 94/10]
69 -- Dunno if the above comment is still meaningful now. JRS 001024.
71 do { when (dopt Opt_DoCmmLinting dflags) $ do
72 { showPass dflags "CmmLint"
73 ; let lints = map cmmLint flat_abstractC
74 ; case firstJust lints of
75 Just err -> do { printDump err
81 ; showPass dflags "CodeOutput"
82 ; let filenm = hscOutName dflags
83 ; stubs_exist <- outputForeignStubs dflags this_mod location foreign_stubs
84 ; case hscTarget dflags of {
85 HscInterpreted -> return ();
86 HscAsm -> outputAsm dflags filenm flat_abstractC;
87 HscC -> outputC dflags filenm flat_abstractC pkg_deps;
88 HscLlvm -> outputLlvm dflags filenm flat_abstractC;
91 outputJava dflags filenm mod_name tycons core_binds;
93 panic "Java support not compiled into this ghc";
95 HscNothing -> panic "codeOutput: HscNothing"
100 doOutput :: String -> (Handle -> IO ()) -> IO ()
101 doOutput filenm io_action = bracket (openFile filenm WriteMode) hClose io_action
105 %************************************************************************
109 %************************************************************************
118 outputC dflags filenm flat_absC packages
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 rts = getPackageDetails (pkgState dflags) rtsPackageId
128 let cc_injects = unlines (map mk_include (includes rts))
131 '"':_{-"-} -> "#include "++h_file
132 '<':_ -> "#include "++h_file
133 _ -> "#include \""++h_file++"\""
135 pkg_configs <- getPreloadPackagesAnd dflags packages
136 let pkg_names = map (display.sourcePackageId) pkg_configs
138 doOutput filenm $ \ h -> do
139 hPutStr h ("/* GHC_PACKAGES " ++ unwords pkg_names ++ "\n*/\n")
141 writeCs dflags h flat_absC
145 %************************************************************************
147 \subsection{Assembler}
149 %************************************************************************
152 outputAsm :: DynFlags -> FilePath -> [RawCmm] -> IO ()
154 #ifndef OMIT_NATIVE_CODEGEN
156 outputAsm dflags filenm flat_absC
157 = do ncg_uniqs <- mkSplitUniqSupply 'n'
159 {-# SCC "OutputAsm" #-} doOutput filenm $
160 \f -> {-# SCC "NativeCodeGen" #-}
161 nativeCodeGen dflags f ncg_uniqs flat_absC
164 #else /* OMIT_NATIVE_CODEGEN */
167 = pprPanic "This compiler was built without a native code generator"
168 (text "Use -fvia-C instead")
174 %************************************************************************
178 %************************************************************************
181 outputLlvm :: DynFlags -> FilePath -> [RawCmm] -> IO ()
183 #ifndef GHCI_TABLES_NEXT_TO_CODE
184 outputLlvm dflags filenm flat_absC
185 = do ncg_uniqs <- mkSplitUniqSupply 'n'
186 doOutput filenm $ \f ->
187 LlvmCodeGen.llvmCodeGen dflags f ncg_uniqs flat_absC
190 = pprPanic "This compiler was built with the LLVM backend disabled"
191 (text ("This is because the TABLES_NEXT_TO_CODE optimisation is"
192 ++ " enabled, which the LLVM backend doesn't support right now.")
193 $+$ text "Use -fasm instead")
198 %************************************************************************
202 %************************************************************************
206 outputJava dflags filenm mod tycons core_binds
207 = doOutput filenm (\ f -> printForUser f alwaysQualify pp_java)
208 -- User style printing for now to keep indentation
210 occ_anal_binds = occurAnalyseBinds core_binds
211 -- Make sure we have up to date dead-var information
212 java_code = javaGen mod [{- Should be imports-}] tycons occ_anal_binds
213 pp_java = PrintJava.compilationUnit java_code
218 %************************************************************************
220 \subsection{Foreign import/export}
222 %************************************************************************
225 outputForeignStubs :: DynFlags -> Module -> ModLocation -> ForeignStubs
226 -> IO (Bool, -- Header file created
227 Bool) -- C file created
228 outputForeignStubs dflags mod location stubs
231 -- When compiling External Core files, may need to use stub
232 -- files from a previous compilation
233 stub_c_exists <- doesFileExist stub_c
234 stub_h_exists <- doesFileExist stub_h
235 return (stub_h_exists, stub_c_exists)
237 ForeignStubs h_code c_code -> do
239 stub_c_output_d = pprCode CStyle c_code
240 stub_c_output_w = showSDoc stub_c_output_d
242 -- Header file protos for "foreign export"ed functions.
243 stub_h_output_d = pprCode CStyle h_code
244 stub_h_output_w = showSDoc stub_h_output_d
247 createDirectoryHierarchy (takeDirectory stub_c)
249 dumpIfSet_dyn dflags Opt_D_dump_foreign
250 "Foreign export header file" stub_h_output_d
252 -- we need the #includes from the rts package for the stub files
254 let rts_pkg = getPackageDetails (pkgState dflags) rtsPackageId in
255 concatMap mk_include (includes rts_pkg)
256 mk_include i = "#include \"" ++ i ++ "\"\n"
258 -- wrapper code mentions the ffi_arg type, which comes from ffi.h
259 ffi_includes | cLibFFI = "#include \"ffi.h\"\n"
263 <- outputForeignStubs_help stub_h stub_h_output_w
264 ("#include \"HsFFI.h\"\n" ++ cplusplus_hdr) cplusplus_ftr
266 dumpIfSet_dyn dflags Opt_D_dump_foreign
267 "Foreign export stubs" stub_c_output_d
270 <- outputForeignStubs_help stub_c stub_c_output_w
271 ("#define IN_STG_CODE 0\n" ++
272 "#include \"Rts.h\"\n" ++
277 -- We're adding the default hc_header to the stub file, but this
278 -- isn't really HC code, so we need to define IN_STG_CODE==0 to
279 -- avoid the register variables etc. being enabled.
281 return (stub_h_file_exists, stub_c_file_exists)
283 (stub_c, stub_h, _) = mkStubPaths dflags (moduleName mod) location
285 cplusplus_hdr = "#ifdef __cplusplus\nextern \"C\" {\n#endif\n"
286 cplusplus_ftr = "#ifdef __cplusplus\n}\n#endif\n"
289 -- Don't use doOutput for dumping the f. export stubs
290 -- since it is more than likely that the stubs file will
291 -- turn out to be empty, in which case no file should be created.
292 outputForeignStubs_help :: FilePath -> String -> String -> String -> IO Bool
293 outputForeignStubs_help _fname "" _header _footer = return False
294 outputForeignStubs_help fname doc_str header footer
295 = do writeFile fname (header ++ doc_str ++ '\n':footer ++ "\n")