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/WorkingConventions#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'
173 ncg_output_d <- {-# SCC "NativeCodeGen" #-}
174 nativeCodeGen dflags flat_absC ncg_uniqs
175 dumpIfSet_dyn dflags Opt_D_dump_asm "Asm code" (docToSDoc ncg_output_d)
176 {-# SCC "OutputAsm" #-} doOutput filenm $
177 \f -> printDoc LeftMode f ncg_output_d
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
220 | NoStubs <- stubs = do
221 -- When compiling External Core files, may need to use stub
222 -- files from a previous compilation
223 stub_c_exists <- doesFileExist stub_c
224 stub_h_exists <- doesFileExist stub_h
225 return (stub_h_exists, stub_c_exists)
227 | ForeignStubs h_code c_code _ <- stubs
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"
274 -- Don't use doOutput for dumping the f. export stubs
275 -- since it is more than likely that the stubs file will
276 -- turn out to be empty, in which case no file should be created.
277 outputForeignStubs_help fname "" header footer = return False
278 outputForeignStubs_help fname doc_str header footer
279 = do writeFile fname (header ++ doc_str ++ '\n':footer ++ "\n")