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 IlxGen ( ilxGen )
21 import JavaGen ( javaGen )
22 import OccurAnal ( occurAnalyseBinds )
23 import qualified PrintJava
24 import OccurAnal ( occurAnalyseBinds )
28 import DriverState ( getExplicitPackagesAnd, getPackageCIncludes )
29 import FastString ( unpackFS )
30 import AbsCSyn ( AbstractC )
31 import PprAbsC ( dumpRealC, writeRealC )
34 import ErrUtils ( dumpIfSet_dyn, showPass )
36 import Pretty ( Mode(..), printDoc )
37 import CmdLineOpts ( DynFlags, HscLang(..), dopt_OutName )
43 %************************************************************************
47 %************************************************************************
50 codeOutput :: DynFlags
52 -> AbstractC -- Compiled abstract C
53 -> IO (Bool{-stub_h_exists-}, Bool{-stub_c_exists-})
56 (ModGuts {mg_module = mod_name,
58 mg_foreign = foreign_stubs,
60 mg_binds = core_binds})
63 -- You can have C (c_output) or assembly-language (ncg_output),
64 -- but not both. [Allowing for both gives a space leak on
65 -- flat_abstractC. WDP 94/10]
67 -- Dunno if the above comment is still meaningful now. JRS 001024.
69 do { showPass dflags "CodeOutput"
70 ; let filenm = dopt_OutName dflags
71 ; stubs_exist <- outputForeignStubs dflags foreign_stubs
72 ; case dopt_HscLang dflags of {
73 HscInterpreted -> return ();
74 HscAsm -> outputAsm dflags filenm flat_abstractC;
75 HscC -> outputC dflags filenm flat_abstractC stubs_exist
79 outputJava dflags filenm mod_name tycons core_binds;
81 panic "Java support not compiled into this ghc";
85 let tycons = typeEnvTyCons type_env in
86 outputIlx dflags filenm mod_name tycons stg_binds;
88 panic "ILX support not compiled into this ghc";
94 doOutput :: String -> (Handle -> IO ()) -> IO ()
95 doOutput filenm io_action = bracket (openFile filenm WriteMode) hClose io_action
99 %************************************************************************
103 %************************************************************************
106 outputC dflags filenm flat_absC
107 (stub_h_exists, _) dependencies (ForeignStubs _ _ ffi_decl_headers _ )
108 = do dumpIfSet_dyn dflags Opt_D_dump_realC "Real C" (dumpRealC flat_absC)
110 -- figure out which header files to #include in the generated .hc file:
112 -- * extra_includes from packages
113 -- * -#include options from the cmdline and OPTIONS pragmas
114 -- * the _stub.h file, if there is one.
116 let packages = dep_pkgs dependencies
117 pkg_configs <- getExplicitPackagesAnd packages
118 let pkg_names = map name pkg_configs
120 c_includes <- getPackageCIncludes pkg_configs
121 let cmdline_includes = cmdlineHcIncludes dflags -- -#include options
123 all_headers = c_includes
124 ++ reverse cmdline_includes
125 ++ reverse (map unpackFS ffi_decl_headers)
128 let cc_injects = unlines (map mk_include all_headers)
131 '"':_{-"-} -> "#include "++h_file
132 '<':_ -> "#include "++h_file
133 _ -> "#include \""++h_file++"\""
135 doOutput filenm $ \ h -> do
136 hPutStr h ("/* GHC_PACKAGES " ++ unwords pkg_names ++ "\n*/\n")
139 hPutStrLn h ("#include \"" ++ (hscStubHOutName dflags) ++ "\"")
140 writeRealC h flat_absC
144 %************************************************************************
146 \subsection{Assembler}
148 %************************************************************************
151 outputAsm dflags filenm flat_absC
153 #ifndef OMIT_NATIVE_CODEGEN
155 = do ncg_uniqs <- mkSplitUniqSupply 'n'
156 let (stix_final, ncg_output_d) = _scc_ "NativeCodeGen"
157 nativeCodeGen flat_absC ncg_uniqs
158 dumpIfSet_dyn dflags Opt_D_dump_stix "Final stix code" stix_final
159 dumpIfSet_dyn dflags Opt_D_dump_asm "Asm code" (docToSDoc ncg_output_d)
160 _scc_ "OutputAsm" doOutput filenm $
161 \f -> printDoc LeftMode f ncg_output_d
164 #else /* OMIT_NATIVE_CODEGEN */
166 = pprPanic "This compiler was built without a native code generator"
167 (text "Use -fvia-C instead")
173 %************************************************************************
177 %************************************************************************
181 outputJava dflags filenm mod tycons core_binds
182 = doOutput filenm (\ f -> printForUser f alwaysQualify pp_java)
183 -- User style printing for now to keep indentation
185 occ_anal_binds = occurAnalyseBinds core_binds
186 -- Make sure we have up to date dead-var information
187 java_code = javaGen mod [{- Should be imports-}] tycons occ_anal_binds
188 pp_java = PrintJava.compilationUnit java_code
193 %************************************************************************
197 %************************************************************************
201 outputIlx dflags filename mod tycons stg_binds
202 = doOutput filename (\ f -> printForC f pp_ilx)
204 pp_ilx = ilxGen mod tycons stg_binds
209 %************************************************************************
211 \subsection{Foreign import/export}
213 %************************************************************************
216 outputForeignStubs :: DynFlags -> ForeignStubs
217 -> IO (Bool, -- Header file created
218 Bool) -- C file created
219 outputForeignStubs dflags NoStubs = return (False, False)
220 outputForeignStubs dflags (ForeignStubs h_code c_code _ _)
222 dumpIfSet_dyn dflags Opt_D_dump_foreign
223 "Foreign export header file" stub_h_output_d
225 -- we need the #includes from the rts package for the stub files
226 rts_pkgs <- getPackageDetails [rtsPackage]
227 let rts_includes = concatMap mk_include (concatMap c_includes rts_pkgs)
228 mk_include i = "#include \"" ++ i ++ "\"\n"
231 <- outputForeignStubs_help (hscStubHOutName dflags) stub_h_output_w
232 ("#include \"HsFFI.h\"\n" ++ cplusplus_hdr) cplusplus_ftr
234 dumpIfSet_dyn dflags Opt_D_dump_foreign
235 "Foreign export stubs" stub_c_output_d
238 <- outputForeignStubs_help (hscStubCOutName dflags) stub_c_output_w
239 ("#define IN_STG_CODE 0\n" ++
240 "#include \"RtsAPI.h\"\n" ++
244 -- We're adding the default hc_header to the stub file, but this
245 -- isn't really HC code, so we need to define IN_STG_CODE==0 to
246 -- avoid the register variables etc. being enabled.
248 return (stub_h_file_exists, stub_c_file_exists)
250 -- C stubs for "foreign export"ed functions.
251 stub_c_output_d = pprCode CStyle c_code
252 stub_c_output_w = showSDoc stub_c_output_d
254 -- Header file protos for "foreign export"ed functions.
255 stub_h_output_d = pprCode CStyle h_code
256 stub_h_output_w = showSDoc stub_h_output_d
258 cplusplus_hdr = "#ifdef __cplusplus\nextern \"C\" {\n#endif\n"
259 cplusplus_ftr = "#ifdef __cplusplus\n}\n#endif\n"
261 -- Don't use doOutput for dumping the f. export stubs
262 -- since it is more than likely that the stubs file will
263 -- turn out to be empty, in which case no file should be created.
264 outputForeignStubs_help fname "" header footer = return False
265 outputForeignStubs_help fname doc_str header footer
266 = do writeFile fname (header ++ doc_str ++ '\n':footer ++ "\n")