[project @ 2000-11-24 09:51:38 by simonpj]
[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) = 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)
117   where
118
119 #else /* OMIT_NATIVE_CODEGEN */
120
121   = pprPanic "This compiler was built without a native code generator"
122              (text "Use -fvia-C instead")
123
124 #endif
125 \end{code}
126
127
128 %************************************************************************
129 %*                                                                      *
130 \subsection{Java}
131 %*                                                                      *
132 %************************************************************************
133
134 \begin{code}
135 outputJava dflags filenm mod tycons core_binds
136   = doOutput filenm (\ f -> printForUser f alwaysQualify pp_java)
137         -- User style printing for now to keep indentation
138   where
139     occ_anal_binds = occurAnalyseBinds core_binds
140         -- Make sure we have up to date dead-var information
141     java_code = javaGen mod [{- Should be imports-}] tycons occ_anal_binds
142     pp_java   = PrintJava.compilationUnit java_code
143 \end{code}
144
145
146 %************************************************************************
147 %*                                                                      *
148 \subsection{Foreign import/export}
149 %*                                                                      *
150 %************************************************************************
151
152 \begin{code}
153 outputForeignStubs dflags c_code h_code
154   = do
155         dumpIfSet_dyn dflags Opt_D_dump_foreign
156                       "Foreign export header file" stub_h_output_d
157
158         maybe_stub_h_file
159            <- outputForeignStubs_help True{-.h output-} stub_h_output_w
160
161         dumpIfSet_dyn dflags Opt_D_dump_foreign
162                       "Foreign export stubs" stub_c_output_d
163
164         maybe_stub_c_file
165            <- outputForeignStubs_help False{-not .h-} stub_c_output_w
166
167         return (maybe_stub_h_file, maybe_stub_c_file)
168   where
169     -- C stubs for "foreign export"ed functions.
170     stub_c_output_d = pprCode CStyle c_code
171     stub_c_output_w = showSDoc stub_c_output_d
172
173     -- Header file protos for "foreign export"ed functions.
174     stub_h_output_d = pprCode CStyle h_code
175     stub_h_output_w = showSDoc stub_h_output_d
176
177
178 -- Don't use doOutput for dumping the f. export stubs
179 -- since it is more than likely that the stubs file will
180 -- turn out to be empty, in which case no file should be created.
181 outputForeignStubs_help is_header ""      = return Nothing
182 outputForeignStubs_help is_header doc_str 
183    = do fname <- newTempName suffix
184         writeFile fname (include_prefix ++ doc_str)
185         return (Just fname)
186   where
187     suffix
188        | is_header   = "h_stub"
189        | otherwise   = "c_stub"
190     include_prefix
191        | is_header   = "#include \"HsFFI.h\"\n"
192        | otherwise   = "#include \"RtsAPI.h\"\n"
193 \end{code}
194