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 Class ( Class )
25 import CoreSyn ( CoreBind )
26 import StgSyn ( StgBinding )
27 import AbsCSyn ( AbstractC )
28 import PprAbsC ( dumpRealC, writeRealC )
29 import UniqSupply ( UniqSupply )
30 import Module ( Module )
32 import ErrUtils ( dumpIfSet_dyn )
34 import CmdLineOpts ( DynFlags, HscLang(..), dopt_OutName )
35 import TmpFiles ( newTempName )
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 let filenm = dopt_OutName dflags
66 stub_names <- outputForeignStubs dflags c_code h_code
67 case dopt_HscLang dflags of
68 HscInterpreted -> return stub_names
69 HscAsm -> outputAsm dflags filenm flat_abstractC
71 HscC -> outputC dflags filenm flat_abstractC
73 HscJava -> outputJava dflags filenm mod_name tycons core_binds
76 doOutput :: String -> (Handle -> IO ()) -> IO ()
77 doOutput filenm io_action
78 = (do handle <- openFile filenm WriteMode
81 `catch` (\err -> pprPanic "Failed to open or write code output file"
86 %************************************************************************
90 %************************************************************************
93 outputC dflags filenm flat_absC
94 = do dumpIfSet_dyn dflags Opt_D_dump_realC "Real C" (dumpRealC flat_absC)
95 doOutput filenm (\ h -> writeRealC h flat_absC)
99 %************************************************************************
101 \subsection{Assembler}
103 %************************************************************************
106 outputAsm dflags filenm flat_absC
108 #ifndef OMIT_NATIVE_CODEGEN
110 = do ncg_uniqs <- mkSplitUniqSupply 'n'
112 (stix_final, ncg_output_d) = nativeCodeGen flat_absC ncg_uniqs
114 dumpIfSet_dyn dflags Opt_D_dump_stix "Final stix code" stix_final
115 dumpIfSet_dyn dflags Opt_D_dump_asm "Asm code" ncg_output_d
116 doOutput filenm ( \f -> printForAsm f ncg_output_d)
119 #else /* OMIT_NATIVE_CODEGEN */
121 = pprPanic "This compiler was built without a native code generator"
122 (text "Use -fvia-C instead")
128 %************************************************************************
132 %************************************************************************
135 outputJava dflags filenm mod tycons core_binds
136 = doOutput filenm (\ f -> printForUser f pp_java)
137 -- User style printing for now to keep indentation
139 java_code = javaGen mod [{- Should be imports-}] tycons core_binds
140 pp_java = PrintJava.compilationUnit java_code
144 %************************************************************************
146 \subsection{Foreign import/export}
148 %************************************************************************
151 outputForeignStubs dflags c_code h_code
153 dumpIfSet_dyn dflags Opt_D_dump_foreign
154 "Foreign export header file" stub_h_output_d
157 <- outputForeignStubs_help True{-.h output-} stub_h_output_w
159 dumpIfSet_dyn dflags Opt_D_dump_foreign
160 "Foreign export stubs" stub_c_output_d
163 <- outputForeignStubs_help False{-not .h-} stub_c_output_w
165 return (maybe_stub_h_file, maybe_stub_c_file)
167 -- C stubs for "foreign export"ed functions.
168 stub_c_output_d = pprCode CStyle c_code
169 stub_c_output_w = showSDoc stub_c_output_d
171 -- Header file protos for "foreign export"ed functions.
172 stub_h_output_d = pprCode CStyle h_code
173 stub_h_output_w = showSDoc stub_h_output_d
176 -- Don't use doOutput for dumping the f. export stubs
177 -- since it is more than likely that the stubs file will
178 -- turn out to be empty, in which case no file should be created.
179 outputForeignStubs_help is_header "" = return Nothing
180 outputForeignStubs_help is_header doc_str
181 = newTempName suffix >>= \ fname ->
182 writeFile fname (include_prefix ++ doc_str) >>
186 | is_header = "h_stub"
187 | otherwise = "c_stub"
189 | is_header = "#include \"Rts.h\"\n"
190 | otherwise = "#include \"RtsAPI.h\"\n"