[project @ 2001-01-18 17:19:26 by simonmar]
[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 AsmCodeGen       ( nativeCodeGen )
13 #endif
14
15 #ifdef ILX
16 import IlxGen           ( ilxGen )
17 #endif
18
19 import JavaGen          ( javaGen )
20 import qualified PrintJava
21
22 import TyCon            ( TyCon )
23 import Id               ( Id )
24 import CoreSyn          ( CoreBind )
25 import OccurAnal        ( occurAnalyseBinds )
26 import StgSyn           ( StgBinding )
27 import AbsCSyn          ( AbstractC )
28 import PprAbsC          ( dumpRealC, writeRealC )
29 import Module           ( Module )
30 import CmdLineOpts
31 import ErrUtils         ( dumpIfSet_dyn, showPass )
32 import Outputable
33 import CmdLineOpts      ( DynFlags, HscLang(..), dopt_OutName )
34 import TmpFiles         ( newTempName )
35 import UniqSupply       ( mkSplitUniqSupply )
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         }
77
78 doOutput :: String -> (Handle -> IO ()) -> IO ()
79 doOutput filenm io_action
80   = (do handle <- openFile filenm WriteMode
81         io_action handle
82         hClose handle)
83     `catch` (\err -> pprPanic "Failed to open or write code output file" 
84                               (text filenm))
85 \end{code}
86
87
88 %************************************************************************
89 %*                                                                      *
90 \subsection{C}
91 %*                                                                      *
92 %************************************************************************
93
94 \begin{code}
95 outputC dflags filenm flat_absC
96   = do dumpIfSet_dyn dflags Opt_D_dump_realC "Real C" (dumpRealC flat_absC)
97        doOutput filenm (\ h -> writeRealC h flat_absC)
98 \end{code}
99
100
101 %************************************************************************
102 %*                                                                      *
103 \subsection{Assembler}
104 %*                                                                      *
105 %************************************************************************
106
107 \begin{code}
108 outputAsm dflags filenm flat_absC
109
110 #ifndef OMIT_NATIVE_CODEGEN
111
112   = do ncg_uniqs <- mkSplitUniqSupply 'n'
113        let (stix_final, ncg_output_d) = _scc_ "NativeCodeGen" 
114                                         nativeCodeGen flat_absC ncg_uniqs
115        dumpIfSet_dyn dflags Opt_D_dump_stix "Final stix code" stix_final
116        dumpIfSet_dyn dflags Opt_D_dump_asm "Asm code" ncg_output_d
117        _scc_ "OutputAsm" doOutput filenm ( \f -> printForAsm f ncg_output_d)
118   where
119
120 #else /* OMIT_NATIVE_CODEGEN */
121
122   = pprPanic "This compiler was built without a native code generator"
123              (text "Use -fvia-C instead")
124
125 #endif
126 \end{code}
127
128
129 %************************************************************************
130 %*                                                                      *
131 \subsection{Java}
132 %*                                                                      *
133 %************************************************************************
134
135 \begin{code}
136 outputJava dflags filenm mod tycons core_binds
137   = doOutput filenm (\ f -> printForUser f alwaysQualify pp_java)
138         -- User style printing for now to keep indentation
139   where
140     occ_anal_binds = occurAnalyseBinds core_binds
141         -- Make sure we have up to date dead-var information
142     java_code = javaGen mod [{- Should be imports-}] tycons occ_anal_binds
143     pp_java   = PrintJava.compilationUnit java_code
144 \end{code}
145
146
147 %************************************************************************
148 %*                                                                      *
149 \subsection{Foreign import/export}
150 %*                                                                      *
151 %************************************************************************
152
153 \begin{code}
154 outputForeignStubs dflags c_code h_code
155   = do
156         dumpIfSet_dyn dflags Opt_D_dump_foreign
157                       "Foreign export header file" stub_h_output_d
158
159         maybe_stub_h_file
160            <- outputForeignStubs_help True{-.h output-} stub_h_output_w
161
162         dumpIfSet_dyn dflags Opt_D_dump_foreign
163                       "Foreign export stubs" stub_c_output_d
164
165         maybe_stub_c_file
166            <- outputForeignStubs_help False{-not .h-} stub_c_output_w
167
168         return (maybe_stub_h_file, maybe_stub_c_file)
169   where
170     -- C stubs for "foreign export"ed functions.
171     stub_c_output_d = pprCode CStyle c_code
172     stub_c_output_w = showSDoc stub_c_output_d
173
174     -- Header file protos for "foreign export"ed functions.
175     stub_h_output_d = pprCode CStyle h_code
176     stub_h_output_w = showSDoc stub_h_output_d
177
178
179 -- Don't use doOutput for dumping the f. export stubs
180 -- since it is more than likely that the stubs file will
181 -- turn out to be empty, in which case no file should be created.
182 outputForeignStubs_help is_header ""      = return Nothing
183 outputForeignStubs_help is_header doc_str 
184    = do fname <- newTempName suffix
185         writeFile fname (include_prefix ++ doc_str)
186         return (Just fname)
187   where
188     suffix
189        | is_header   = "h_stub"
190        | otherwise   = "c_stub"
191     include_prefix
192        | is_header   = "#include \"HsFFI.h\"\n"
193        | otherwise   = "#include \"RtsAPI.h\"\n"
194 \end{code}
195