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 UniqSupply ( mkSplitUniqSupply )
13 import AsmCodeGen ( nativeCodeGen )
17 import IlxGen ( ilxGen )
20 import JavaGen ( javaGen )
21 import qualified PrintJava
23 import TyCon ( TyCon )
25 import CoreSyn ( CoreBind )
26 import OccurAnal ( occurAnalyseBinds )
27 import StgSyn ( StgBinding )
28 import AbsCSyn ( AbstractC )
29 import PprAbsC ( dumpRealC, writeRealC )
30 import Module ( Module )
32 import ErrUtils ( dumpIfSet_dyn, showPass )
34 import CmdLineOpts ( DynFlags, HscLang(..), dopt_OutName )
35 import TmpFiles ( newTempName )
37 import IO ( IOMode(..), hClose, openFile, Handle )
38 import IO ( hPutStr, stderr) -- Debugging
42 %************************************************************************
46 %************************************************************************
49 codeOutput :: DynFlags
51 -> [TyCon] -- Local tycons
52 -> [CoreBind] -- Core bindings
53 -> [(StgBinding,[Id])] -- The STG program with SRTs
54 -> SDoc -- C stubs for foreign exported functions
55 -> SDoc -- Header file prototype for foreign exported functions
56 -> AbstractC -- Compiled abstract C
57 -> IO (Maybe FilePath, Maybe FilePath)
58 codeOutput dflags mod_name tycons core_binds stg_binds
59 c_code h_code flat_abstractC
60 = -- You can have C (c_output) or assembly-language (ncg_output),
61 -- but not both. [Allowing for both gives a space leak on
62 -- flat_abstractC. WDP 94/10]
64 -- Dunno if the above comment is still meaningful now. JRS 001024.
66 do { showPass dflags "CodeOutput"
67 ; let filenm = dopt_OutName dflags
68 ; stub_names <- outputForeignStubs dflags c_code h_code
69 ; case dopt_HscLang dflags of
70 HscInterpreted -> return stub_names
71 HscAsm -> outputAsm dflags filenm flat_abstractC
73 HscC -> outputC dflags filenm flat_abstractC
75 HscJava -> outputJava dflags filenm mod_name tycons core_binds
78 HscILX -> outputIlx dflags filenm mod_name tycons stg_binds
83 doOutput :: String -> (Handle -> IO ()) -> IO ()
84 doOutput filenm io_action
85 = (do hPutStr stderr ("Writing to" ++ filenm)
86 handle <- openFile filenm WriteMode
89 `catch` (\err -> pprPanic "Failed to open or write code output file"
94 %************************************************************************
98 %************************************************************************
101 outputC dflags filenm flat_absC
102 = do dumpIfSet_dyn dflags Opt_D_dump_realC "Real C" (dumpRealC flat_absC)
103 doOutput filenm (\ h -> writeRealC h flat_absC)
107 %************************************************************************
109 \subsection{Assembler}
111 %************************************************************************
114 outputAsm dflags filenm flat_absC
116 #ifndef OMIT_NATIVE_CODEGEN
118 = do ncg_uniqs <- mkSplitUniqSupply 'n'
119 let (stix_final, ncg_output_d) = _scc_ "NativeCodeGen"
120 nativeCodeGen flat_absC ncg_uniqs
121 dumpIfSet_dyn dflags Opt_D_dump_stix "Final stix code" stix_final
122 dumpIfSet_dyn dflags Opt_D_dump_asm "Asm code" ncg_output_d
123 _scc_ "OutputAsm" doOutput filenm ( \f -> printForAsm f ncg_output_d)
126 #else /* OMIT_NATIVE_CODEGEN */
128 = pprPanic "This compiler was built without a native code generator"
129 (text "Use -fvia-C instead")
135 %************************************************************************
139 %************************************************************************
142 outputJava dflags filenm mod tycons core_binds
143 = doOutput filenm (\ f -> printForUser f alwaysQualify pp_java)
144 -- User style printing for now to keep indentation
146 occ_anal_binds = occurAnalyseBinds core_binds
147 -- Make sure we have up to date dead-var information
148 java_code = javaGen mod [{- Should be imports-}] tycons occ_anal_binds
149 pp_java = PrintJava.compilationUnit java_code
153 %************************************************************************
157 %************************************************************************
161 outputIlx dflags filename mod tycons stg_binds
162 = doOutput filename (\ f -> printForC f pp_ilx)
164 pp_ilx = ilxGen mod tycons stg_binds
169 %************************************************************************
171 \subsection{Foreign import/export}
173 %************************************************************************
176 outputForeignStubs dflags c_code h_code
178 dumpIfSet_dyn dflags Opt_D_dump_foreign
179 "Foreign export header file" stub_h_output_d
182 <- outputForeignStubs_help True{-.h output-} stub_h_output_w
184 dumpIfSet_dyn dflags Opt_D_dump_foreign
185 "Foreign export stubs" stub_c_output_d
188 <- outputForeignStubs_help False{-not .h-} stub_c_output_w
190 return (maybe_stub_h_file, maybe_stub_c_file)
192 -- C stubs for "foreign export"ed functions.
193 stub_c_output_d = pprCode CStyle c_code
194 stub_c_output_w = showSDoc stub_c_output_d
196 -- Header file protos for "foreign export"ed functions.
197 stub_h_output_d = pprCode CStyle h_code
198 stub_h_output_w = showSDoc stub_h_output_d
201 -- Don't use doOutput for dumping the f. export stubs
202 -- since it is more than likely that the stubs file will
203 -- turn out to be empty, in which case no file should be created.
204 outputForeignStubs_help is_header "" = return Nothing
205 outputForeignStubs_help is_header doc_str
206 = do fname <- newTempName suffix
207 writeFile fname (include_prefix ++ doc_str)
211 | is_header = "h_stub"
212 | otherwise = "c_stub"
214 | is_header = "#include \"HsFFI.h\"\n"
215 | otherwise = "#include \"RtsAPI.h\"\n"