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 )
27 import FastString ( unpackFS )
32 import ErrUtils ( dumpIfSet_dyn, showPass, ghcExit )
36 import Maybes ( firstJust )
38 import Distribution.Package ( showPackageId )
39 import Directory ( doesFileExist )
44 %************************************************************************
48 %************************************************************************
51 codeOutput :: DynFlags
56 -> [RawCmm] -- Compiled C--
57 -> IO (Bool{-stub_h_exists-}, Bool{-stub_c_exists-})
59 codeOutput dflags this_mod location foreign_stubs pkg_deps flat_abstractC
61 -- You can have C (c_output) or assembly-language (ncg_output),
62 -- but not both. [Allowing for both gives a space leak on
63 -- flat_abstractC. WDP 94/10]
65 -- Dunno if the above comment is still meaningful now. JRS 001024.
67 do { when (dopt Opt_DoCmmLinting dflags) $ do
68 { showPass dflags "CmmLint"
69 ; let lints = map cmmLint flat_abstractC
70 ; case firstJust lints of
71 Just err -> do { printDump err
77 ; showPass dflags "CodeOutput"
78 ; let filenm = hscOutName dflags
79 ; stubs_exist <- outputForeignStubs dflags this_mod location foreign_stubs
80 ; case hscTarget dflags of {
81 HscInterpreted -> return ();
82 HscAsm -> outputAsm dflags filenm flat_abstractC;
83 HscC -> outputC dflags filenm this_mod location
84 flat_abstractC stubs_exist pkg_deps
88 outputJava dflags filenm mod_name tycons core_binds;
90 panic "Java support not compiled into this ghc";
92 HscNothing -> panic "codeOutput: HscNothing"
97 doOutput :: String -> (Handle -> IO ()) -> IO ()
98 doOutput filenm io_action = bracket (openFile filenm WriteMode) hClose io_action
102 %************************************************************************
106 %************************************************************************
110 -> FilePath -> Module -> ModLocation
117 outputC dflags filenm mod location flat_absC
118 (stub_h_exists, _) packages foreign_stubs
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 pkg_configs <- getPreloadPackagesAnd dflags packages
127 let pkg_names = map (showPackageId.package) pkg_configs
129 c_includes <- getPackageCIncludes pkg_configs
130 let cmdline_includes = cmdlineHcIncludes dflags -- -#include options
133 = case foreign_stubs of
135 ForeignStubs _ _ fdhs -> map unpackFS (nub fdhs)
136 -- Remove duplicates, because distinct foreign import decls
137 -- may cite the same #include. Order doesn't matter.
139 all_headers = c_includes
140 ++ reverse cmdline_includes
143 let cc_injects = unlines (map mk_include all_headers)
146 '"':_{-"-} -> "#include "++h_file
147 '<':_ -> "#include "++h_file
148 _ -> "#include \""++h_file++"\""
150 doOutput filenm $ \ h -> do
151 hPutStr h ("/* GHC_PACKAGES " ++ unwords pkg_names ++ "\n*/\n")
154 hPutStrLn h ("#include \"" ++ inc_stub_h ++ "\"")
155 writeCs dflags h flat_absC
157 (_, _, inc_stub_h) = mkStubPaths dflags (moduleName mod) location
161 %************************************************************************
163 \subsection{Assembler}
165 %************************************************************************
168 outputAsm :: DynFlags -> FilePath -> [RawCmm] -> IO ()
169 outputAsm dflags filenm flat_absC
171 #ifndef OMIT_NATIVE_CODEGEN
173 = do ncg_uniqs <- mkSplitUniqSupply 'n'
175 {-# SCC "OutputAsm" #-} doOutput filenm $
176 \f -> {-# SCC "NativeCodeGen" #-}
177 nativeCodeGen dflags f ncg_uniqs flat_absC
180 #else /* OMIT_NATIVE_CODEGEN */
182 = pprPanic "This compiler was built without a native code generator"
183 (text "Use -fvia-C instead")
189 %************************************************************************
193 %************************************************************************
197 outputJava dflags filenm mod tycons core_binds
198 = doOutput filenm (\ f -> printForUser f alwaysQualify pp_java)
199 -- User style printing for now to keep indentation
201 occ_anal_binds = occurAnalyseBinds core_binds
202 -- Make sure we have up to date dead-var information
203 java_code = javaGen mod [{- Should be imports-}] tycons occ_anal_binds
204 pp_java = PrintJava.compilationUnit java_code
209 %************************************************************************
211 \subsection{Foreign import/export}
213 %************************************************************************
216 outputForeignStubs :: DynFlags -> Module -> ModLocation -> ForeignStubs
217 -> IO (Bool, -- Header file created
218 Bool) -- C file created
219 outputForeignStubs dflags mod location stubs
222 -- When compiling External Core files, may need to use stub
223 -- files from a previous compilation
224 stub_c_exists <- doesFileExist stub_c
225 stub_h_exists <- doesFileExist stub_h
226 return (stub_h_exists, stub_c_exists)
228 ForeignStubs h_code c_code _ -> do
230 stub_c_output_d = pprCode CStyle c_code
231 stub_c_output_w = showSDoc stub_c_output_d
233 -- Header file protos for "foreign export"ed functions.
234 stub_h_output_d = pprCode CStyle h_code
235 stub_h_output_w = showSDoc stub_h_output_d
238 createDirectoryHierarchy (directoryOf stub_c)
240 dumpIfSet_dyn dflags Opt_D_dump_foreign
241 "Foreign export header file" stub_h_output_d
243 -- we need the #includes from the rts package for the stub files
245 let rts_pkg = getPackageDetails (pkgState dflags) rtsPackageId in
246 concatMap mk_include (includes rts_pkg)
247 mk_include i = "#include \"" ++ i ++ "\"\n"
250 <- outputForeignStubs_help stub_h stub_h_output_w
251 ("#include \"HsFFI.h\"\n" ++ cplusplus_hdr) cplusplus_ftr
253 dumpIfSet_dyn dflags Opt_D_dump_foreign
254 "Foreign export stubs" stub_c_output_d
257 <- outputForeignStubs_help stub_c stub_c_output_w
258 ("#define IN_STG_CODE 0\n" ++
259 "#include \"Rts.h\"\n" ++
263 -- We're adding the default hc_header to the stub file, but this
264 -- isn't really HC code, so we need to define IN_STG_CODE==0 to
265 -- avoid the register variables etc. being enabled.
267 return (stub_h_file_exists, stub_c_file_exists)
269 (stub_c, stub_h, _) = mkStubPaths dflags (moduleName mod) location
271 cplusplus_hdr = "#ifdef __cplusplus\nextern \"C\" {\n#endif\n"
272 cplusplus_ftr = "#ifdef __cplusplus\n}\n#endif\n"
275 -- Don't use doOutput for dumping the f. export stubs
276 -- since it is more than likely that the stubs file will
277 -- turn out to be empty, in which case no file should be created.
278 outputForeignStubs_help :: FilePath -> String -> String -> String -> IO Bool
279 outputForeignStubs_help _fname "" _header _footer = return False
280 outputForeignStubs_help fname doc_str header footer
281 = do writeFile fname (header ++ doc_str ++ '\n':footer ++ "\n")