1e33654169b932b059c179ee8ff73e0b32c2432a
[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 \end{code}
39
40
41 %************************************************************************
42 %*                                                                      *
43 \subsection{Steering}
44 %*                                                                      *
45 %************************************************************************
46
47 \begin{code}
48 codeOutput :: DynFlags
49            -> Module
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]
62
63     -- Dunno if the above comment is still meaningful now.  JRS 001024.
64
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
71                                >> return stub_names
72              HscC           -> outputC dflags filenm flat_abstractC     
73                                >> return stub_names
74              HscJava        -> outputJava dflags filenm mod_name tycons core_binds
75                                >> return stub_names
76 #ifdef ILX
77              HscILX         -> outputIlx dflags filenm mod_name tycons stg_binds
78                                >> return stub_names
79 #endif
80         }
81
82 doOutput :: String -> (Handle -> IO ()) -> IO ()
83 doOutput filenm io_action
84   = (do handle <- openFile filenm WriteMode
85         io_action handle
86         hClose handle)
87     `catch` (\err -> pprPanic "Failed to open or write code output file" 
88                               (text filenm))
89 \end{code}
90
91
92 %************************************************************************
93 %*                                                                      *
94 \subsection{C}
95 %*                                                                      *
96 %************************************************************************
97
98 \begin{code}
99 outputC dflags filenm flat_absC
100   = do dumpIfSet_dyn dflags Opt_D_dump_realC "Real C" (dumpRealC flat_absC)
101        doOutput filenm (\ h -> writeRealC h flat_absC)
102 \end{code}
103
104
105 %************************************************************************
106 %*                                                                      *
107 \subsection{Assembler}
108 %*                                                                      *
109 %************************************************************************
110
111 \begin{code}
112 outputAsm dflags filenm flat_absC
113
114 #ifndef OMIT_NATIVE_CODEGEN
115
116   = do ncg_uniqs <- mkSplitUniqSupply 'n'
117        let (stix_final, ncg_output_d) = _scc_ "NativeCodeGen" 
118                                         nativeCodeGen flat_absC ncg_uniqs
119        dumpIfSet_dyn dflags Opt_D_dump_stix "Final stix code" stix_final
120        dumpIfSet_dyn dflags Opt_D_dump_asm "Asm code" ncg_output_d
121        _scc_ "OutputAsm" doOutput filenm ( \f -> printForAsm f ncg_output_d)
122   where
123
124 #else /* OMIT_NATIVE_CODEGEN */
125
126   = pprPanic "This compiler was built without a native code generator"
127              (text "Use -fvia-C instead")
128
129 #endif
130 \end{code}
131
132
133 %************************************************************************
134 %*                                                                      *
135 \subsection{Java}
136 %*                                                                      *
137 %************************************************************************
138
139 \begin{code}
140 outputJava dflags filenm mod tycons core_binds
141   = doOutput filenm (\ f -> printForUser f alwaysQualify pp_java)
142         -- User style printing for now to keep indentation
143   where
144     occ_anal_binds = occurAnalyseBinds core_binds
145         -- Make sure we have up to date dead-var information
146     java_code = javaGen mod [{- Should be imports-}] tycons occ_anal_binds
147     pp_java   = PrintJava.compilationUnit java_code
148 \end{code}
149
150
151 %************************************************************************
152 %*                                                                      *
153 \subsection{Ilx}
154 %*                                                                      *
155 %************************************************************************
156
157 \begin{code}
158 #ifdef ILX
159 outputIlx dflags filename mod tycons stg_binds
160   =  doOutput filename (\ f -> printForC f pp_ilx)
161   where
162     pp_ilx = ilxGen mod tycons stg_binds
163 #endif
164 \end{code}
165
166
167 %************************************************************************
168 %*                                                                      *
169 \subsection{Foreign import/export}
170 %*                                                                      *
171 %************************************************************************
172
173 \begin{code}
174 outputForeignStubs dflags c_code h_code
175   = do
176         dumpIfSet_dyn dflags Opt_D_dump_foreign
177                       "Foreign export header file" stub_h_output_d
178
179         maybe_stub_h_file
180            <- outputForeignStubs_help True{-.h output-} stub_h_output_w
181
182         dumpIfSet_dyn dflags Opt_D_dump_foreign
183                       "Foreign export stubs" stub_c_output_d
184
185         maybe_stub_c_file
186            <- outputForeignStubs_help False{-not .h-} stub_c_output_w
187
188         return (maybe_stub_h_file, maybe_stub_c_file)
189   where
190     -- C stubs for "foreign export"ed functions.
191     stub_c_output_d = pprCode CStyle c_code
192     stub_c_output_w = showSDoc stub_c_output_d
193
194     -- Header file protos for "foreign export"ed functions.
195     stub_h_output_d = pprCode CStyle h_code
196     stub_h_output_w = showSDoc stub_h_output_d
197
198
199 -- Don't use doOutput for dumping the f. export stubs
200 -- since it is more than likely that the stubs file will
201 -- turn out to be empty, in which case no file should be created.
202 outputForeignStubs_help is_header ""      = return Nothing
203 outputForeignStubs_help is_header doc_str 
204    = do fname <- newTempName suffix
205         writeFile fname (include_prefix ++ doc_str)
206         return (Just fname)
207   where
208     suffix
209        | is_header   = "h_stub"
210        | otherwise   = "c_stub"
211     include_prefix
212        | is_header   = "#include \"HsFFI.h\"\n"
213        | otherwise   = "#include \"RtsAPI.h\"\n"
214 \end{code}
215