33357fcbc2ddd16fb491601c034c8003d289d8c6
[ghc-hetmet.git] / ghc / compiler / main / CodeOutput.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
3 %
4 \section{Code output phase}
5
6 \begin{code}
7 module CodeOutput( codeOutput ) where
8
9 #include "HsVersions.h"
10
11 #ifndef OMIT_NATIVE_CODEGEN
12 import UniqSupply       ( mkSplitUniqSupply )
13 import AsmCodeGen       ( nativeCodeGen )
14 #endif
15
16 #ifdef ILX
17 import IlxGen           ( ilxGen )
18 #endif
19
20 import JavaGen          ( javaGen )
21 import qualified PrintJava
22
23 import TyCon            ( TyCon )
24 import Id               ( Id )
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 )
31 import CmdLineOpts
32 import ErrUtils         ( dumpIfSet_dyn, showPass )
33 import Outputable
34 import CmdLineOpts      ( DynFlags, HscLang(..), dopt_OutName )
35 import TmpFiles         ( newTempName )
36
37 import IO               ( IOMode(..), hClose, openFile, Handle )
38 import IO               ( hPutStr, stderr)      -- Debugging
39 \end{code}
40
41
42 %************************************************************************
43 %*                                                                      *
44 \subsection{Steering}
45 %*                                                                      *
46 %************************************************************************
47
48 \begin{code}
49 codeOutput :: DynFlags
50            -> Module
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]
63
64     -- Dunno if the above comment is still meaningful now.  JRS 001024.
65
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
72                                >> return stub_names
73              HscC           -> outputC dflags filenm flat_abstractC     
74                                >> return stub_names
75              HscJava        -> outputJava dflags filenm mod_name tycons core_binds
76                                >> return stub_names
77 #ifdef ILX
78              HscILX         -> outputIlx dflags filenm mod_name tycons stg_binds
79                                >> return stub_names
80 #endif
81         }
82
83 doOutput :: String -> (Handle -> IO ()) -> IO ()
84 doOutput filenm io_action
85   = (do hPutStr stderr ("Writing to" ++ filenm)
86         handle <- openFile filenm WriteMode
87         io_action handle
88         hClose handle)
89     `catch` (\err -> pprPanic "Failed to open or write code output file" 
90                               (text filenm))
91 \end{code}
92
93
94 %************************************************************************
95 %*                                                                      *
96 \subsection{C}
97 %*                                                                      *
98 %************************************************************************
99
100 \begin{code}
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)
104 \end{code}
105
106
107 %************************************************************************
108 %*                                                                      *
109 \subsection{Assembler}
110 %*                                                                      *
111 %************************************************************************
112
113 \begin{code}
114 outputAsm dflags filenm flat_absC
115
116 #ifndef OMIT_NATIVE_CODEGEN
117
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)
124   where
125
126 #else /* OMIT_NATIVE_CODEGEN */
127
128   = pprPanic "This compiler was built without a native code generator"
129              (text "Use -fvia-C instead")
130
131 #endif
132 \end{code}
133
134
135 %************************************************************************
136 %*                                                                      *
137 \subsection{Java}
138 %*                                                                      *
139 %************************************************************************
140
141 \begin{code}
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
145   where
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
150 \end{code}
151
152
153 %************************************************************************
154 %*                                                                      *
155 \subsection{Ilx}
156 %*                                                                      *
157 %************************************************************************
158
159 \begin{code}
160 #ifdef ILX
161 outputIlx dflags filename mod tycons stg_binds
162   =  doOutput filename (\ f -> printForC f pp_ilx)
163   where
164     pp_ilx = ilxGen mod tycons stg_binds
165 #endif
166 \end{code}
167
168
169 %************************************************************************
170 %*                                                                      *
171 \subsection{Foreign import/export}
172 %*                                                                      *
173 %************************************************************************
174
175 \begin{code}
176 outputForeignStubs dflags c_code h_code
177   = do
178         dumpIfSet_dyn dflags Opt_D_dump_foreign
179                       "Foreign export header file" stub_h_output_d
180
181         maybe_stub_h_file
182            <- outputForeignStubs_help True{-.h output-} stub_h_output_w
183
184         dumpIfSet_dyn dflags Opt_D_dump_foreign
185                       "Foreign export stubs" stub_c_output_d
186
187         maybe_stub_c_file
188            <- outputForeignStubs_help False{-not .h-} stub_c_output_w
189
190         return (maybe_stub_h_file, maybe_stub_c_file)
191   where
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
195
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
199
200
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)
208         return (Just fname)
209   where
210     suffix
211        | is_header   = "h_stub"
212        | otherwise   = "c_stub"
213     include_prefix
214        | is_header   = "#include \"HsFFI.h\"\n"
215        | otherwise   = "#include \"RtsAPI.h\"\n"
216 \end{code}
217