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 JavaGen ( javaGen )
18 import qualified PrintJava
19 import OccurAnal ( occurAnalyseBinds )
22 import Finder ( mkStubPaths )
23 import PprC ( writeCs )
24 import CmmLint ( cmmLint )
31 import ErrUtils ( dumpIfSet_dyn, showPass, ghcExit )
34 import Maybes ( firstJust )
36 import Distribution.Text
37 import Directory ( doesFileExist )
40 import System.FilePath
43 %************************************************************************
47 %************************************************************************
50 codeOutput :: DynFlags
55 -> [RawCmm] -- Compiled C--
56 -> IO (Bool{-stub_h_exists-}, Bool{-stub_c_exists-})
58 codeOutput dflags this_mod location foreign_stubs pkg_deps flat_abstractC
60 -- You can have C (c_output) or assembly-language (ncg_output),
61 -- but not both. [Allowing for both gives a space leak on
62 -- flat_abstractC. WDP 94/10]
64 -- Dunno if the above comment is still meaningful now. JRS 001024.
66 do { when (dopt Opt_DoCmmLinting dflags) $ do
67 { showPass dflags "CmmLint"
68 ; let lints = map cmmLint flat_abstractC
69 ; case firstJust lints of
70 Just err -> do { printDump err
76 ; showPass dflags "CodeOutput"
77 ; let filenm = hscOutName dflags
78 ; stubs_exist <- outputForeignStubs dflags this_mod location foreign_stubs
79 ; case hscTarget dflags of {
80 HscInterpreted -> return ();
81 HscAsm -> outputAsm dflags filenm flat_abstractC;
82 HscC -> outputC dflags filenm flat_abstractC pkg_deps;
85 outputJava dflags filenm mod_name tycons core_binds;
87 panic "Java support not compiled into this ghc";
89 HscNothing -> panic "codeOutput: HscNothing"
94 doOutput :: String -> (Handle -> IO ()) -> IO ()
95 doOutput filenm io_action = bracket (openFile filenm WriteMode) hClose io_action
99 %************************************************************************
103 %************************************************************************
112 outputC dflags filenm flat_absC packages
114 -- figure out which header files to #include in the generated .hc file:
116 -- * extra_includes from packages
117 -- * -#include options from the cmdline and OPTIONS pragmas
118 -- * the _stub.h file, if there is one.
120 let rts = getPackageDetails (pkgState dflags) rtsPackageId
122 let cc_injects = unlines (map mk_include (includes rts))
125 '"':_{-"-} -> "#include "++h_file
126 '<':_ -> "#include "++h_file
127 _ -> "#include \""++h_file++"\""
129 pkg_configs <- getPreloadPackagesAnd dflags packages
130 let pkg_names = map (display.package) pkg_configs
132 doOutput filenm $ \ h -> do
133 hPutStr h ("/* GHC_PACKAGES " ++ unwords pkg_names ++ "\n*/\n")
135 writeCs dflags h flat_absC
139 %************************************************************************
141 \subsection{Assembler}
143 %************************************************************************
146 outputAsm :: DynFlags -> FilePath -> [RawCmm] -> IO ()
148 #ifndef OMIT_NATIVE_CODEGEN
150 outputAsm dflags filenm flat_absC
151 = do ncg_uniqs <- mkSplitUniqSupply 'n'
153 {-# SCC "OutputAsm" #-} doOutput filenm $
154 \f -> {-# SCC "NativeCodeGen" #-}
155 nativeCodeGen dflags f ncg_uniqs flat_absC
158 #else /* OMIT_NATIVE_CODEGEN */
161 = pprPanic "This compiler was built without a native code generator"
162 (text "Use -fvia-C instead")
168 %************************************************************************
172 %************************************************************************
176 outputJava dflags filenm mod tycons core_binds
177 = doOutput filenm (\ f -> printForUser f alwaysQualify pp_java)
178 -- User style printing for now to keep indentation
180 occ_anal_binds = occurAnalyseBinds core_binds
181 -- Make sure we have up to date dead-var information
182 java_code = javaGen mod [{- Should be imports-}] tycons occ_anal_binds
183 pp_java = PrintJava.compilationUnit java_code
188 %************************************************************************
190 \subsection{Foreign import/export}
192 %************************************************************************
195 outputForeignStubs :: DynFlags -> Module -> ModLocation -> ForeignStubs
196 -> IO (Bool, -- Header file created
197 Bool) -- C file created
198 outputForeignStubs dflags mod location stubs
201 -- When compiling External Core files, may need to use stub
202 -- files from a previous compilation
203 stub_c_exists <- doesFileExist stub_c
204 stub_h_exists <- doesFileExist stub_h
205 return (stub_h_exists, stub_c_exists)
207 ForeignStubs h_code c_code -> do
209 stub_c_output_d = pprCode CStyle c_code
210 stub_c_output_w = showSDoc stub_c_output_d
212 -- Header file protos for "foreign export"ed functions.
213 stub_h_output_d = pprCode CStyle h_code
214 stub_h_output_w = showSDoc stub_h_output_d
217 createDirectoryHierarchy (takeDirectory stub_c)
219 dumpIfSet_dyn dflags Opt_D_dump_foreign
220 "Foreign export header file" stub_h_output_d
222 -- we need the #includes from the rts package for the stub files
224 let rts_pkg = getPackageDetails (pkgState dflags) rtsPackageId in
225 concatMap mk_include (includes rts_pkg)
226 mk_include i = "#include \"" ++ i ++ "\"\n"
229 <- outputForeignStubs_help stub_h stub_h_output_w
230 ("#include \"HsFFI.h\"\n" ++ cplusplus_hdr) cplusplus_ftr
232 dumpIfSet_dyn dflags Opt_D_dump_foreign
233 "Foreign export stubs" stub_c_output_d
236 <- outputForeignStubs_help stub_c stub_c_output_w
237 ("#define IN_STG_CODE 0\n" ++
238 "#include \"Rts.h\"\n" ++
242 -- We're adding the default hc_header to the stub file, but this
243 -- isn't really HC code, so we need to define IN_STG_CODE==0 to
244 -- avoid the register variables etc. being enabled.
246 return (stub_h_file_exists, stub_c_file_exists)
248 (stub_c, stub_h, _) = mkStubPaths dflags (moduleName mod) location
250 cplusplus_hdr = "#ifdef __cplusplus\nextern \"C\" {\n#endif\n"
251 cplusplus_ftr = "#ifdef __cplusplus\n}\n#endif\n"
254 -- Don't use doOutput for dumping the f. export stubs
255 -- since it is more than likely that the stubs file will
256 -- turn out to be empty, in which case no file should be created.
257 outputForeignStubs_help :: FilePath -> String -> String -> String -> IO Bool
258 outputForeignStubs_help _fname "" _header _footer = return False
259 outputForeignStubs_help fname doc_str header footer
260 = do writeFile fname (header ++ doc_str ++ '\n':footer ++ "\n")