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 )
35 import ErrUtils ( dumpIfSet_dyn, showPass, ghcExit )
38 import Maybes ( firstJusts )
40 import Control.Exception
42 import System.Directory
43 import System.FilePath
47 %************************************************************************
51 %************************************************************************
54 codeOutput :: DynFlags
59 -> [RawCmm] -- Compiled C--
60 -> IO (Bool{-stub_h_exists-}, Maybe FilePath{-stub_c_exists-})
62 codeOutput dflags this_mod location foreign_stubs pkg_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 firstJusts lints of
74 Just err -> do { printDump err
80 ; showPass dflags "CodeOutput"
81 ; let filenm = hscOutName dflags
82 ; stubs_exist <- outputForeignStubs dflags this_mod location foreign_stubs
83 ; case hscTarget dflags of {
84 HscInterpreted -> return ();
85 HscAsm -> outputAsm dflags filenm flat_abstractC;
86 HscC -> outputC dflags filenm flat_abstractC pkg_deps;
87 HscLlvm -> outputLlvm dflags filenm flat_abstractC;
90 outputJava dflags filenm mod_name tycons core_binds;
92 panic "Java support not compiled into this ghc";
94 HscNothing -> panic "codeOutput: HscNothing"
99 doOutput :: String -> (Handle -> IO ()) -> IO ()
100 doOutput filenm io_action = bracket (openFile filenm WriteMode) hClose io_action
104 %************************************************************************
108 %************************************************************************
117 outputC dflags filenm flat_absC packages
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 rts = getPackageDetails (pkgState dflags) rtsPackageId
127 let cc_injects = unlines (map mk_include (includes rts))
130 '"':_{-"-} -> "#include "++h_file
131 '<':_ -> "#include "++h_file
132 _ -> "#include \""++h_file++"\""
134 pkg_configs <- getPreloadPackagesAnd dflags packages
135 let pkg_names = map (display.sourcePackageId) pkg_configs
137 doOutput filenm $ \ h -> do
138 hPutStr h ("/* GHC_PACKAGES " ++ unwords pkg_names ++ "\n*/\n")
140 writeCs dflags h flat_absC
144 %************************************************************************
146 \subsection{Assembler}
148 %************************************************************************
151 outputAsm :: DynFlags -> FilePath -> [RawCmm] -> IO ()
153 #ifndef OMIT_NATIVE_CODEGEN
155 outputAsm dflags filenm flat_absC
156 = do ncg_uniqs <- mkSplitUniqSupply 'n'
158 {-# SCC "OutputAsm" #-} doOutput filenm $
159 \f -> {-# SCC "NativeCodeGen" #-}
160 nativeCodeGen dflags f ncg_uniqs flat_absC
163 #else /* OMIT_NATIVE_CODEGEN */
166 = pprPanic "This compiler was built without a native code generator"
167 (text "Use -fvia-C instead")
173 %************************************************************************
177 %************************************************************************
180 outputLlvm :: DynFlags -> FilePath -> [RawCmm] -> IO ()
181 outputLlvm dflags filenm flat_absC
182 = do ncg_uniqs <- mkSplitUniqSupply 'n'
183 doOutput filenm $ \f -> llvmCodeGen dflags f ncg_uniqs flat_absC
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 %************************************************************************
209 \subsection{Foreign import/export}
211 %************************************************************************
214 outputForeignStubs :: DynFlags -> Module -> ModLocation -> ForeignStubs
215 -> IO (Bool, -- Header file created
216 Maybe FilePath) -- C file created
217 outputForeignStubs dflags mod location stubs
219 let stub_h = mkStubPaths dflags (moduleName mod) location
220 stub_c <- newTempName dflags "c"
224 -- When compiling External Core files, may need to use stub
225 -- files from a previous compilation
226 stub_h_exists <- doesFileExist stub_h
227 return (stub_h_exists, Nothing)
229 ForeignStubs h_code c_code -> do
231 stub_c_output_d = pprCode CStyle c_code
232 stub_c_output_w = showSDoc stub_c_output_d
234 -- Header file protos for "foreign export"ed functions.
235 stub_h_output_d = pprCode CStyle h_code
236 stub_h_output_w = showSDoc stub_h_output_d
239 createDirectoryHierarchy (takeDirectory stub_h)
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
246 let rts_pkg = getPackageDetails (pkgState dflags) rtsPackageId in
247 concatMap mk_include (includes rts_pkg)
248 mk_include i = "#include \"" ++ i ++ "\"\n"
250 -- wrapper code mentions the ffi_arg type, which comes from ffi.h
251 ffi_includes | cLibFFI = "#include \"ffi.h\"\n"
255 <- outputForeignStubs_help stub_h stub_h_output_w
256 ("#include \"HsFFI.h\"\n" ++ cplusplus_hdr) cplusplus_ftr
258 dumpIfSet_dyn dflags Opt_D_dump_foreign
259 "Foreign export stubs" stub_c_output_d
262 <- outputForeignStubs_help stub_c stub_c_output_w
263 ("#define IN_STG_CODE 0\n" ++
264 "#include \"Rts.h\"\n" ++
269 -- We're adding the default hc_header to the stub file, but this
270 -- isn't really HC code, so we need to define IN_STG_CODE==0 to
271 -- avoid the register variables etc. being enabled.
273 return (stub_h_file_exists, if stub_c_file_exists
277 cplusplus_hdr = "#ifdef __cplusplus\nextern \"C\" {\n#endif\n"
278 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 :: FilePath -> String -> String -> String -> IO Bool
285 outputForeignStubs_help _fname "" _header _footer = return False
286 outputForeignStubs_help fname doc_str header footer
287 = do writeFile fname (header ++ doc_str ++ '\n':footer ++ "\n")