2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
4 \section{Code output phase}
8 -- The above warning supression flag is a temporary kludge.
9 -- While working on this module you are encouraged to remove it and fix
10 -- any warnings in the module. See
11 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
14 module CodeOutput( codeOutput, outputForeignStubs ) where
16 #include "HsVersions.h"
18 #ifndef OMIT_NATIVE_CODEGEN
19 import UniqSupply ( mkSplitUniqSupply )
20 import AsmCodeGen ( nativeCodeGen )
24 import JavaGen ( javaGen )
25 import qualified PrintJava
26 import OccurAnal ( occurAnalyseBinds )
29 import Finder ( mkStubPaths )
30 import PprC ( writeCs )
31 import CmmLint ( cmmLint )
33 import PackageConfig ( rtsPackageId )
35 import FastString ( unpackFS )
40 import ErrUtils ( dumpIfSet_dyn, showPass, ghcExit )
42 import Pretty ( Mode(..), printDoc )
43 import Module ( Module, ModLocation(..), moduleName )
45 import Maybes ( firstJust )
47 import Distribution.Package ( showPackageId )
48 import Directory ( doesFileExist )
53 %************************************************************************
57 %************************************************************************
60 codeOutput :: DynFlags
65 -> [RawCmm] -- Compiled C--
66 -> IO (Bool{-stub_h_exists-}, Bool{-stub_c_exists-})
68 codeOutput dflags this_mod location foreign_stubs pkg_deps flat_abstractC
70 -- You can have C (c_output) or assembly-language (ncg_output),
71 -- but not both. [Allowing for both gives a space leak on
72 -- flat_abstractC. WDP 94/10]
74 -- Dunno if the above comment is still meaningful now. JRS 001024.
76 do { when (dopt Opt_DoCmmLinting dflags) $ do
77 { showPass dflags "CmmLint"
78 ; let lints = map cmmLint flat_abstractC
79 ; case firstJust lints of
80 Just err -> do { printDump err
86 ; showPass dflags "CodeOutput"
87 ; let filenm = hscOutName dflags
88 ; stubs_exist <- outputForeignStubs dflags this_mod location foreign_stubs
89 ; case hscTarget dflags of {
90 HscInterpreted -> return ();
91 HscAsm -> outputAsm dflags filenm flat_abstractC;
92 HscC -> outputC dflags filenm this_mod location
93 flat_abstractC stubs_exist pkg_deps
97 outputJava dflags filenm mod_name tycons core_binds;
99 panic "Java support not compiled into this ghc";
105 doOutput :: String -> (Handle -> IO ()) -> IO ()
106 doOutput filenm io_action = bracket (openFile filenm WriteMode) hClose io_action
110 %************************************************************************
114 %************************************************************************
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 \"" ++ (filenameOf stub_h) ++ "\"")
155 writeCs dflags h flat_absC
157 (_, stub_h) = mkStubPaths dflags (moduleName mod) location
161 %************************************************************************
163 \subsection{Assembler}
165 %************************************************************************
168 outputAsm dflags filenm flat_absC
170 #ifndef OMIT_NATIVE_CODEGEN
172 = do ncg_uniqs <- mkSplitUniqSupply 'n'
174 {-# SCC "OutputAsm" #-} doOutput filenm $
175 \f -> {-# SCC "NativeCodeGen" #-}
176 nativeCodeGen dflags f ncg_uniqs flat_absC
179 #else /* OMIT_NATIVE_CODEGEN */
181 = pprPanic "This compiler was built without a native code generator"
182 (text "Use -fvia-C instead")
188 %************************************************************************
192 %************************************************************************
196 outputJava dflags filenm mod tycons core_binds
197 = doOutput filenm (\ f -> printForUser f alwaysQualify pp_java)
198 -- User style printing for now to keep indentation
200 occ_anal_binds = occurAnalyseBinds core_binds
201 -- Make sure we have up to date dead-var information
202 java_code = javaGen mod [{- Should be imports-}] tycons occ_anal_binds
203 pp_java = PrintJava.compilationUnit java_code
208 %************************************************************************
210 \subsection{Foreign import/export}
212 %************************************************************************
215 outputForeignStubs :: DynFlags -> Module -> ModLocation -> ForeignStubs
216 -> IO (Bool, -- Header file created
217 Bool) -- C file created
218 outputForeignStubs dflags mod location stubs
219 | NoStubs <- stubs = do
220 -- When compiling External Core files, may need to use stub
221 -- files from a previous compilation
222 stub_c_exists <- doesFileExist stub_c
223 stub_h_exists <- doesFileExist stub_h
224 return (stub_h_exists, stub_c_exists)
226 | ForeignStubs h_code c_code _ <- stubs
229 stub_c_output_d = pprCode CStyle c_code
230 stub_c_output_w = showSDoc stub_c_output_d
232 -- Header file protos for "foreign export"ed functions.
233 stub_h_output_d = pprCode CStyle h_code
234 stub_h_output_w = showSDoc stub_h_output_d
237 createDirectoryHierarchy (directoryOf stub_c)
239 dumpIfSet_dyn dflags Opt_D_dump_foreign
240 "Foreign export header file" stub_h_output_d
242 -- we need the #includes from the rts package for the stub files
244 let rts_pkg = getPackageDetails (pkgState dflags) rtsPackageId in
245 concatMap mk_include (includes rts_pkg)
246 mk_include i = "#include \"" ++ i ++ "\"\n"
249 <- outputForeignStubs_help stub_h stub_h_output_w
250 ("#include \"HsFFI.h\"\n" ++ cplusplus_hdr) cplusplus_ftr
252 dumpIfSet_dyn dflags Opt_D_dump_foreign
253 "Foreign export stubs" stub_c_output_d
256 <- outputForeignStubs_help stub_c stub_c_output_w
257 ("#define IN_STG_CODE 0\n" ++
258 "#include \"Rts.h\"\n" ++
262 -- We're adding the default hc_header to the stub file, but this
263 -- isn't really HC code, so we need to define IN_STG_CODE==0 to
264 -- avoid the register variables etc. being enabled.
266 return (stub_h_file_exists, stub_c_file_exists)
268 (stub_c, stub_h) = mkStubPaths dflags (moduleName mod) location
270 cplusplus_hdr = "#ifdef __cplusplus\nextern \"C\" {\n#endif\n"
271 cplusplus_ftr = "#ifdef __cplusplus\n}\n#endif\n"
273 -- Don't use doOutput for dumping the f. export stubs
274 -- since it is more than likely that the stubs file will
275 -- turn out to be empty, in which case no file should be created.
276 outputForeignStubs_help fname "" header footer = return False
277 outputForeignStubs_help fname doc_str header footer
278 = do writeFile fname (header ++ doc_str ++ '\n':footer ++ "\n")