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, absCNop )
28 import PprAbsC ( dumpRealC, writeRealC )
29 import UniqSupply ( UniqSupply )
30 import Module ( Module, moduleString )
32 import Maybes ( maybeToBool )
33 import ErrUtils ( doIfSet, dumpIfSet )
35 import IO ( IOMode(..), hClose, openFile )
39 %************************************************************************
43 %************************************************************************
47 -> [TyCon] -> [Class] -- Local tycons and classes
48 -> [CoreBind] -- Core bindings
49 -> [(StgBinding,[Id])] -- The STG program with SRTs
50 -> SDoc -- C stubs for foreign exported functions
51 -> SDoc -- Header file prototype for foreign exported functions
52 -> AbstractC -- Compiled abstract C
55 codeOutput mod_name tycons classes core_binds stg_binds
56 c_code h_code flat_abstractC ncg_uniqs
57 = -- You can have C (c_output) or assembly-language (ncg_output),
58 -- but not both. [Allowing for both gives a space leak on
59 -- flat_abstractC. WDP 94/10]
62 outputForeignStubs c_code h_code ;
63 case opt_OutputLanguage of {
64 Nothing -> return () -- No -olang=xxx flag; so no-op
65 ; Just "asm" -> outputAsm flat_abstractC ncg_uniqs
66 ; Just "C" -> outputC flat_abstractC
67 ; Just "java" -> outputJava mod_name tycons core_binds
68 ; Just foo -> pprPanic "Don't understand output language" (quotes (text foo))
73 = (do handle <- openFile opt_OutputFile WriteMode
76 `catch` (\err -> pprPanic "Failed to open or write code output file" (text opt_OutputFile))
80 %************************************************************************
84 %************************************************************************
89 dumpIfSet opt_D_dump_realC "Real C" (dumpRealC flat_absC)
90 doOutput (\ h -> writeRealC h flat_absC)
94 %************************************************************************
96 \subsection{Assembler}
98 %************************************************************************
101 outputAsm flat_absC ncg_uniqs
102 #ifndef OMIT_NATIVE_CODEGEN
104 = do dumpIfSet opt_D_dump_stix "Final stix code" stix_final
105 dumpIfSet opt_D_dump_asm "Asm code" ncg_output_d
106 doOutput (\ f -> printForAsm f ncg_output_d)
108 (stix_final, ncg_output_d) = nativeCodeGen flat_absC ncg_uniqs
110 #else /* OMIT_NATIVE_CODEGEN */
112 = pprPanic "This compiler was built without a native code generator"
113 (text "Use -fvia-C instead")
119 %************************************************************************
123 %************************************************************************
126 outputJava mod tycons core_binds
127 = doOutput (\ f -> printForUser f pp_java)
128 -- User style printing for now to keep indentation
130 java_code = javaGen mod [{- Should be imports-}] tycons core_binds
131 pp_java = PrintJava.compilationUnit java_code
135 %************************************************************************
137 \subsection{Foreign import/export}
139 %************************************************************************
142 outputForeignStubs c_code h_code
144 dumpIfSet opt_D_dump_foreign "Foreign export header file" stub_h_output_d
145 outputForeignStubs_help True{-.h output-} opt_ProduceExportHStubs stub_h_output_w
147 dumpIfSet opt_D_dump_foreign "Foreign export stubs" stub_c_output_d
148 outputForeignStubs_help False{-not .h-} opt_ProduceExportCStubs stub_c_output_w
150 -- C stubs for "foreign export"ed functions.
151 stub_c_output_d = pprCode CStyle c_code
152 stub_c_output_w = showSDoc stub_c_output_d
154 -- Header file protos for "foreign export"ed functions.
155 stub_h_output_d = pprCode CStyle h_code
156 stub_h_output_w = showSDoc stub_h_output_d
159 -- Don't use doOutput for dumping the f. export stubs
160 -- since it is more than likely that the stubs file will
161 -- turn out to be empty, in which case no file should be created.
162 outputForeignStubs_help is_header switch "" = return ()
163 outputForeignStubs_help is_header switch doc_str =
166 Just fname -> writeFile fname (include_prefix ++ doc_str)
169 | is_header = "#include \"Rts.h\"\n"
170 | otherwise = "#include \"RtsAPI.h\"\n"