2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
4 \section{Code output phase}
7 module CodeOutput( codeOutput ) where
9 #include "HsVersions.h"
11 #ifndef OMIT_NATIVE_CODEGEN
12 import AsmCodeGen ( nativeCodeGen )
16 import IlxGen ( ilxGen )
19 import JavaGen ( javaGen )
20 import qualified PrintJava
22 import TyCon ( TyCon )
24 import CoreSyn ( CoreBind )
25 import OccurAnal ( occurAnalyseBinds )
26 import StgSyn ( StgBinding )
27 import AbsCSyn ( AbstractC )
28 import PprAbsC ( dumpRealC, writeRealC )
29 import Module ( Module )
31 import ErrUtils ( dumpIfSet_dyn, showPass )
33 import CmdLineOpts ( DynFlags, HscLang(..), dopt_OutName )
34 import TmpFiles ( newTempName )
35 import UniqSupply ( mkSplitUniqSupply )
37 import IO ( IOMode(..), hClose, openFile, Handle )
41 %************************************************************************
45 %************************************************************************
48 codeOutput :: DynFlags
50 -> [TyCon] -- Local tycons
51 -> [CoreBind] -- Core bindings
52 -> [(StgBinding,[Id])] -- The STG program with SRTs
53 -> SDoc -- C stubs for foreign exported functions
54 -> SDoc -- Header file prototype for foreign exported functions
55 -> AbstractC -- Compiled abstract C
56 -> IO (Maybe FilePath, Maybe FilePath)
57 codeOutput dflags mod_name tycons core_binds stg_binds
58 c_code h_code flat_abstractC
59 = -- You can have C (c_output) or assembly-language (ncg_output),
60 -- but not both. [Allowing for both gives a space leak on
61 -- flat_abstractC. WDP 94/10]
63 -- Dunno if the above comment is still meaningful now. JRS 001024.
65 do { showPass dflags "CodeOutput"
66 ; let filenm = dopt_OutName dflags
67 ; stub_names <- outputForeignStubs dflags c_code h_code
68 ; case dopt_HscLang dflags of
69 HscInterpreted -> return stub_names
70 HscAsm -> outputAsm dflags filenm flat_abstractC
72 HscC -> outputC dflags filenm flat_abstractC
74 HscJava -> outputJava dflags filenm mod_name tycons core_binds
77 HscILX -> outputIlx mod_name tycons stg_binds
81 doOutput :: String -> (Handle -> IO ()) -> IO ()
82 doOutput filenm io_action
83 = (do handle <- openFile filenm WriteMode
86 `catch` (\err -> pprPanic "Failed to open or write code output file"
91 %************************************************************************
95 %************************************************************************
98 outputC dflags filenm flat_absC
99 = do dumpIfSet_dyn dflags Opt_D_dump_realC "Real C" (dumpRealC flat_absC)
100 doOutput filenm (\ h -> writeRealC h flat_absC)
104 %************************************************************************
106 \subsection{Assembler}
108 %************************************************************************
111 outputAsm dflags filenm flat_absC
113 #ifndef OMIT_NATIVE_CODEGEN
115 = do ncg_uniqs <- mkSplitUniqSupply 'n'
116 let (stix_final, ncg_output_d) = _scc_ "NativeCodeGen"
117 nativeCodeGen flat_absC ncg_uniqs
118 dumpIfSet_dyn dflags Opt_D_dump_stix "Final stix code" stix_final
119 dumpIfSet_dyn dflags Opt_D_dump_asm "Asm code" ncg_output_d
120 _scc_ "OutputAsm" doOutput filenm ( \f -> printForAsm f ncg_output_d)
123 #else /* OMIT_NATIVE_CODEGEN */
125 = pprPanic "This compiler was built without a native code generator"
126 (text "Use -fvia-C instead")
132 %************************************************************************
136 %************************************************************************
139 outputJava dflags filenm mod tycons core_binds
140 = doOutput filenm (\ f -> printForUser f alwaysQualify pp_java)
141 -- User style printing for now to keep indentation
143 occ_anal_binds = occurAnalyseBinds core_binds
144 -- Make sure we have up to date dead-var information
145 java_code = javaGen mod [{- Should be imports-}] tycons occ_anal_binds
146 pp_java = PrintJava.compilationUnit java_code
150 %************************************************************************
154 %************************************************************************
158 outputIlx mod tycons stg_binds
159 = doOutput (\ f -> printForC f pp_ilx)
161 pp_ilx = ilxGen mod tycons stg_binds
166 %************************************************************************
168 \subsection{Foreign import/export}
170 %************************************************************************
173 outputForeignStubs dflags c_code h_code
175 dumpIfSet_dyn dflags Opt_D_dump_foreign
176 "Foreign export header file" stub_h_output_d
179 <- outputForeignStubs_help True{-.h output-} stub_h_output_w
181 dumpIfSet_dyn dflags Opt_D_dump_foreign
182 "Foreign export stubs" stub_c_output_d
185 <- outputForeignStubs_help False{-not .h-} stub_c_output_w
187 return (maybe_stub_h_file, maybe_stub_c_file)
189 -- C stubs for "foreign export"ed functions.
190 stub_c_output_d = pprCode CStyle c_code
191 stub_c_output_w = showSDoc stub_c_output_d
193 -- Header file protos for "foreign export"ed functions.
194 stub_h_output_d = pprCode CStyle h_code
195 stub_h_output_w = showSDoc stub_h_output_d
198 -- Don't use doOutput for dumping the f. export stubs
199 -- since it is more than likely that the stubs file will
200 -- turn out to be empty, in which case no file should be created.
201 outputForeignStubs_help is_header "" = return Nothing
202 outputForeignStubs_help is_header doc_str
203 = do fname <- newTempName suffix
204 writeFile fname (include_prefix ++ doc_str)
208 | is_header = "h_stub"
209 | otherwise = "c_stub"
211 | is_header = "#include \"HsFFI.h\"\n"
212 | otherwise = "#include \"RtsAPI.h\"\n"