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
78 doOutput :: String -> (Handle -> IO ()) -> IO ()
79 doOutput filenm io_action
80 = (do handle <- openFile filenm WriteMode
83 `catch` (\err -> pprPanic "Failed to open or write code output file"
88 %************************************************************************
92 %************************************************************************
95 outputC dflags filenm flat_absC
96 = do dumpIfSet_dyn dflags Opt_D_dump_realC "Real C" (dumpRealC flat_absC)
97 doOutput filenm (\ h -> writeRealC h flat_absC)
101 %************************************************************************
103 \subsection{Assembler}
105 %************************************************************************
108 outputAsm dflags filenm flat_absC
110 #ifndef OMIT_NATIVE_CODEGEN
112 = do ncg_uniqs <- mkSplitUniqSupply 'n'
113 let (stix_final, ncg_output_d) = _scc_ "NativeCodeGen"
114 nativeCodeGen flat_absC ncg_uniqs
115 dumpIfSet_dyn dflags Opt_D_dump_stix "Final stix code" stix_final
116 dumpIfSet_dyn dflags Opt_D_dump_asm "Asm code" ncg_output_d
117 _scc_ "OutputAsm" doOutput filenm ( \f -> printForAsm f ncg_output_d)
120 #else /* OMIT_NATIVE_CODEGEN */
122 = pprPanic "This compiler was built without a native code generator"
123 (text "Use -fvia-C instead")
129 %************************************************************************
133 %************************************************************************
136 outputJava dflags filenm mod tycons core_binds
137 = doOutput filenm (\ f -> printForUser f alwaysQualify pp_java)
138 -- User style printing for now to keep indentation
140 occ_anal_binds = occurAnalyseBinds core_binds
141 -- Make sure we have up to date dead-var information
142 java_code = javaGen mod [{- Should be imports-}] tycons occ_anal_binds
143 pp_java = PrintJava.compilationUnit java_code
147 %************************************************************************
149 \subsection{Foreign import/export}
151 %************************************************************************
154 outputForeignStubs dflags c_code h_code
156 dumpIfSet_dyn dflags Opt_D_dump_foreign
157 "Foreign export header file" stub_h_output_d
160 <- outputForeignStubs_help True{-.h output-} stub_h_output_w
162 dumpIfSet_dyn dflags Opt_D_dump_foreign
163 "Foreign export stubs" stub_c_output_d
166 <- outputForeignStubs_help False{-not .h-} stub_c_output_w
168 return (maybe_stub_h_file, maybe_stub_c_file)
170 -- C stubs for "foreign export"ed functions.
171 stub_c_output_d = pprCode CStyle c_code
172 stub_c_output_w = showSDoc stub_c_output_d
174 -- Header file protos for "foreign export"ed functions.
175 stub_h_output_d = pprCode CStyle h_code
176 stub_h_output_w = showSDoc stub_h_output_d
179 -- Don't use doOutput for dumping the f. export stubs
180 -- since it is more than likely that the stubs file will
181 -- turn out to be empty, in which case no file should be created.
182 outputForeignStubs_help is_header "" = return Nothing
183 outputForeignStubs_help is_header doc_str
184 = do fname <- newTempName suffix
185 writeFile fname (include_prefix ++ doc_str)
189 | is_header = "h_stub"
190 | otherwise = "c_stub"
192 | is_header = "#include \"HsFFI.h\"\n"
193 | otherwise = "#include \"RtsAPI.h\"\n"