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 DriverState ( v_HCHeader )
24 import TyCon ( TyCon )
26 import CoreSyn ( CoreBind )
27 import OccurAnal ( occurAnalyseBinds )
28 import StgSyn ( StgBinding )
29 import AbsCSyn ( AbstractC )
30 import PprAbsC ( dumpRealC, writeRealC )
31 import Module ( Module )
33 import ErrUtils ( dumpIfSet_dyn, showPass )
35 import CmdLineOpts ( DynFlags, HscLang(..), dopt_OutName )
36 import TmpFiles ( newTempName )
43 %************************************************************************
47 %************************************************************************
50 codeOutput :: DynFlags
52 -> [TyCon] -- Local tycons
53 -> [CoreBind] -- Core bindings
54 -> [(StgBinding,[Id])] -- The STG program with SRTs
55 -> SDoc -- C stubs for foreign exported functions
56 -> SDoc -- Header file prototype for foreign exported functions
57 -> AbstractC -- Compiled abstract C
58 -> IO (Maybe FilePath, Maybe FilePath)
59 codeOutput dflags mod_name tycons core_binds stg_binds
60 c_code h_code flat_abstractC
61 = -- You can have C (c_output) or assembly-language (ncg_output),
62 -- but not both. [Allowing for both gives a space leak on
63 -- flat_abstractC. WDP 94/10]
65 -- Dunno if the above comment is still meaningful now. JRS 001024.
67 do { showPass dflags "CodeOutput"
68 ; let filenm = dopt_OutName dflags
69 ; stub_names <- outputForeignStubs dflags c_code h_code
70 ; case dopt_HscLang dflags of
71 HscInterpreted -> return stub_names
72 HscAsm -> outputAsm dflags filenm flat_abstractC
74 HscC -> outputC dflags filenm flat_abstractC stub_names
76 HscJava -> outputJava dflags filenm mod_name tycons core_binds
79 HscILX -> outputIlx dflags filenm mod_name tycons stg_binds
84 doOutput :: String -> (Handle -> IO ()) -> IO ()
85 doOutput filenm io_action
86 = (do handle <- openFile filenm WriteMode
89 `catch` (\err -> pprPanic "Failed to open or write code output file"
94 %************************************************************************
98 %************************************************************************
101 outputC dflags filenm flat_absC (maybe_stub_h, _)
102 = do dumpIfSet_dyn dflags Opt_D_dump_realC "Real C" (dumpRealC flat_absC)
103 header <- readIORef v_HCHeader
104 doOutput filenm $ \ h -> do
108 Just filename -> hPutStrLn h ("#include \"" ++ filename ++ "\"")
109 writeRealC h flat_absC
113 %************************************************************************
115 \subsection{Assembler}
117 %************************************************************************
120 outputAsm dflags filenm flat_absC
122 #ifndef OMIT_NATIVE_CODEGEN
124 = do ncg_uniqs <- mkSplitUniqSupply 'n'
125 let (stix_final, ncg_output_d) = _scc_ "NativeCodeGen"
126 nativeCodeGen flat_absC ncg_uniqs
127 dumpIfSet_dyn dflags Opt_D_dump_stix "Final stix code" stix_final
128 dumpIfSet_dyn dflags Opt_D_dump_asm "Asm code" ncg_output_d
129 _scc_ "OutputAsm" doOutput filenm ( \f -> printForAsm f ncg_output_d)
132 #else /* OMIT_NATIVE_CODEGEN */
134 = pprPanic "This compiler was built without a native code generator"
135 (text "Use -fvia-C instead")
141 %************************************************************************
145 %************************************************************************
148 outputJava dflags filenm mod tycons core_binds
149 = doOutput filenm (\ f -> printForUser f alwaysQualify pp_java)
150 -- User style printing for now to keep indentation
152 occ_anal_binds = occurAnalyseBinds core_binds
153 -- Make sure we have up to date dead-var information
154 java_code = javaGen mod [{- Should be imports-}] tycons occ_anal_binds
155 pp_java = PrintJava.compilationUnit java_code
159 %************************************************************************
163 %************************************************************************
167 outputIlx dflags filename mod tycons stg_binds
168 = doOutput filename (\ f -> printForC f pp_ilx)
170 pp_ilx = ilxGen mod tycons stg_binds
175 %************************************************************************
177 \subsection{Foreign import/export}
179 %************************************************************************
182 outputForeignStubs dflags c_code h_code
184 dumpIfSet_dyn dflags Opt_D_dump_foreign
185 "Foreign export header file" stub_h_output_d
188 <- outputForeignStubs_help True{-.h output-} stub_h_output_w
190 dumpIfSet_dyn dflags Opt_D_dump_foreign
191 "Foreign export stubs" stub_c_output_d
194 <- outputForeignStubs_help False{-not .h-} stub_c_output_w
196 return (maybe_stub_h_file, maybe_stub_c_file)
198 -- C stubs for "foreign export"ed functions.
199 stub_c_output_d = pprCode CStyle c_code
200 stub_c_output_w = showSDoc stub_c_output_d
202 -- Header file protos for "foreign export"ed functions.
203 stub_h_output_d = pprCode CStyle h_code
204 stub_h_output_w = showSDoc stub_h_output_d
207 -- Don't use doOutput for dumping the f. export stubs
208 -- since it is more than likely that the stubs file will
209 -- turn out to be empty, in which case no file should be created.
210 outputForeignStubs_help is_header "" = return Nothing
211 outputForeignStubs_help is_header doc_str
212 = do fname <- newTempName suffix
213 writeFile fname (include_prefix ++ doc_str)
217 | is_header = "h_stub"
218 | otherwise = "c_stub"
220 | is_header = "#include \"HsFFI.h\"\n"
221 | otherwise = "#include \"RtsAPI.h\"\n"