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 import AsmCodeGen ( nativeCodeGen )
12 import LlvmCodeGen ( llvmCodeGen )
14 import UniqSupply ( mkSplitUniqSupply )
16 import Finder ( mkStubPaths )
17 import PprC ( writeCs )
18 import CmmLint ( cmmLint )
21 import OldCmm ( RawCmm )
27 import ErrUtils ( dumpIfSet_dyn, showPass, ghcExit )
30 import Maybes ( firstJusts )
32 import Control.Exception
34 import System.Directory
35 import System.FilePath
39 %************************************************************************
43 %************************************************************************
46 codeOutput :: DynFlags
51 -> [RawCmm] -- Compiled C--
52 -> IO (Bool{-stub_h_exists-}, Maybe FilePath{-stub_c_exists-})
54 codeOutput dflags this_mod location foreign_stubs pkg_deps flat_abstractC
56 -- You can have C (c_output) or assembly-language (ncg_output),
57 -- but not both. [Allowing for both gives a space leak on
58 -- flat_abstractC. WDP 94/10]
60 -- Dunno if the above comment is still meaningful now. JRS 001024.
62 do { when (dopt Opt_DoCmmLinting dflags) $ do
63 { showPass dflags "CmmLint"
64 ; let lints = map cmmLint flat_abstractC
65 ; case firstJusts lints of
66 Just err -> do { printDump err
72 ; showPass dflags "CodeOutput"
73 ; let filenm = hscOutName dflags
74 ; stubs_exist <- outputForeignStubs dflags this_mod location foreign_stubs
75 ; case hscTarget dflags of {
76 HscInterpreted -> return ();
77 HscAsm -> outputAsm dflags filenm flat_abstractC;
78 HscC -> outputC dflags filenm flat_abstractC pkg_deps;
79 HscLlvm -> outputLlvm dflags filenm flat_abstractC;
80 HscNothing -> panic "codeOutput: HscNothing"
85 doOutput :: String -> (Handle -> IO ()) -> IO ()
86 doOutput filenm io_action = bracket (openFile filenm WriteMode) hClose io_action
90 %************************************************************************
94 %************************************************************************
103 outputC dflags filenm flat_absC packages
105 -- figure out which header files to #include in the generated .hc file:
107 -- * extra_includes from packages
108 -- * -#include options from the cmdline and OPTIONS pragmas
109 -- * the _stub.h file, if there is one.
111 let rts = getPackageDetails (pkgState dflags) rtsPackageId
113 let cc_injects = unlines (map mk_include (includes rts))
116 '"':_{-"-} -> "#include "++h_file
117 '<':_ -> "#include "++h_file
118 _ -> "#include \""++h_file++"\""
120 pkg_configs <- getPreloadPackagesAnd dflags packages
121 let pkg_names = map (display.sourcePackageId) pkg_configs
123 doOutput filenm $ \ h -> do
124 hPutStr h ("/* GHC_PACKAGES " ++ unwords pkg_names ++ "\n*/\n")
126 writeCs dflags h flat_absC
130 %************************************************************************
132 \subsection{Assembler}
134 %************************************************************************
137 outputAsm :: DynFlags -> FilePath -> [RawCmm] -> IO ()
138 outputAsm dflags filenm flat_absC
139 | cGhcWithNativeCodeGen == "YES"
140 = do ncg_uniqs <- mkSplitUniqSupply 'n'
142 {-# SCC "OutputAsm" #-} doOutput filenm $
143 \f -> {-# SCC "NativeCodeGen" #-}
144 nativeCodeGen dflags f ncg_uniqs flat_absC
147 = panic "This compiler was built without a native code generator"
151 %************************************************************************
155 %************************************************************************
158 outputLlvm :: DynFlags -> FilePath -> [RawCmm] -> IO ()
159 outputLlvm dflags filenm flat_absC
160 = do ncg_uniqs <- mkSplitUniqSupply 'n'
161 doOutput filenm $ \f -> llvmCodeGen dflags f ncg_uniqs flat_absC
165 %************************************************************************
167 \subsection{Foreign import/export}
169 %************************************************************************
172 outputForeignStubs :: DynFlags -> Module -> ModLocation -> ForeignStubs
173 -> IO (Bool, -- Header file created
174 Maybe FilePath) -- C file created
175 outputForeignStubs dflags mod location stubs
177 let stub_h = mkStubPaths dflags (moduleName mod) location
178 stub_c <- newTempName dflags "c"
182 -- When compiling External Core files, may need to use stub
183 -- files from a previous compilation
184 stub_h_exists <- doesFileExist stub_h
185 return (stub_h_exists, Nothing)
187 ForeignStubs h_code c_code -> do
189 stub_c_output_d = pprCode CStyle c_code
190 stub_c_output_w = showSDoc stub_c_output_d
192 -- Header file protos for "foreign export"ed functions.
193 stub_h_output_d = pprCode CStyle h_code
194 stub_h_output_w = showSDoc stub_h_output_d
197 createDirectoryHierarchy (takeDirectory stub_h)
199 dumpIfSet_dyn dflags Opt_D_dump_foreign
200 "Foreign export header file" stub_h_output_d
202 -- we need the #includes from the rts package for the stub files
204 let rts_pkg = getPackageDetails (pkgState dflags) rtsPackageId in
205 concatMap mk_include (includes rts_pkg)
206 mk_include i = "#include \"" ++ i ++ "\"\n"
208 -- wrapper code mentions the ffi_arg type, which comes from ffi.h
209 ffi_includes | cLibFFI = "#include \"ffi.h\"\n"
213 <- outputForeignStubs_help stub_h stub_h_output_w
214 ("#include \"HsFFI.h\"\n" ++ cplusplus_hdr) cplusplus_ftr
216 dumpIfSet_dyn dflags Opt_D_dump_foreign
217 "Foreign export stubs" stub_c_output_d
220 <- outputForeignStubs_help stub_c stub_c_output_w
221 ("#define IN_STG_CODE 0\n" ++
222 "#include \"Rts.h\"\n" ++
227 -- We're adding the default hc_header to the stub file, but this
228 -- isn't really HC code, so we need to define IN_STG_CODE==0 to
229 -- avoid the register variables etc. being enabled.
231 return (stub_h_file_exists, if stub_c_file_exists
235 cplusplus_hdr = "#ifdef __cplusplus\nextern \"C\" {\n#endif\n"
236 cplusplus_ftr = "#ifdef __cplusplus\n}\n#endif\n"
239 -- Don't use doOutput for dumping the f. export stubs
240 -- since it is more than likely that the stubs file will
241 -- turn out to be empty, in which case no file should be created.
242 outputForeignStubs_help :: FilePath -> String -> String -> String -> IO Bool
243 outputForeignStubs_help _fname "" _header _footer = return False
244 outputForeignStubs_help fname doc_str header footer
245 = do writeFile fname (header ++ doc_str ++ '\n':footer ++ "\n")