[project @ 2000-10-31 12:07:43 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 Class            ( Class )
25 import CoreSyn          ( CoreBind )
26 import StgSyn           ( StgBinding )
27 import AbsCSyn          ( AbstractC )
28 import PprAbsC          ( dumpRealC, writeRealC )
29 import UniqSupply       ( UniqSupply )
30 import Module           ( Module )
31 import CmdLineOpts
32 import ErrUtils         ( dumpIfSet_dyn )
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 let filenm = dopt_OutName dflags 
66        stub_names <- outputForeignStubs dflags c_code h_code
67        case dopt_HscLang dflags of
68           HscInterpreted -> return stub_names
69           HscAsm         -> outputAsm dflags filenm flat_abstractC
70                             >> return stub_names
71           HscC           -> outputC dflags filenm flat_abstractC        
72                             >> return stub_names
73           HscJava        -> outputJava dflags filenm mod_name tycons core_binds
74                             >> return stub_names
75
76 doOutput :: String -> (Handle -> IO ()) -> IO ()
77 doOutput filenm io_action
78   = (do handle <- openFile filenm WriteMode
79         io_action handle
80         hClose handle)
81     `catch` (\err -> pprPanic "Failed to open or write code output file" 
82                               (text filenm))
83 \end{code}
84
85
86 %************************************************************************
87 %*                                                                      *
88 \subsection{C}
89 %*                                                                      *
90 %************************************************************************
91
92 \begin{code}
93 outputC dflags filenm flat_absC
94   = do dumpIfSet_dyn dflags Opt_D_dump_realC "Real C" (dumpRealC flat_absC)
95        doOutput filenm (\ h -> writeRealC h flat_absC)
96 \end{code}
97
98
99 %************************************************************************
100 %*                                                                      *
101 \subsection{Assembler}
102 %*                                                                      *
103 %************************************************************************
104
105 \begin{code}
106 outputAsm dflags filenm flat_absC
107
108 #ifndef OMIT_NATIVE_CODEGEN
109
110   = do ncg_uniqs <- mkSplitUniqSupply 'n'
111        let
112             (stix_final, ncg_output_d) = nativeCodeGen flat_absC ncg_uniqs
113        in
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 pp_java)
137         -- User style printing for now to keep indentation
138   where
139     java_code = javaGen mod [{- Should be imports-}] tycons core_binds
140     pp_java   = PrintJava.compilationUnit java_code
141 \end{code}
142
143
144 %************************************************************************
145 %*                                                                      *
146 \subsection{Foreign import/export}
147 %*                                                                      *
148 %************************************************************************
149
150 \begin{code}
151 outputForeignStubs dflags c_code h_code
152   = do
153         dumpIfSet_dyn dflags Opt_D_dump_foreign
154                       "Foreign export header file" stub_h_output_d
155
156         maybe_stub_h_file
157            <- outputForeignStubs_help True{-.h output-} stub_h_output_w
158
159         dumpIfSet_dyn dflags Opt_D_dump_foreign
160                       "Foreign export stubs" stub_c_output_d
161
162         maybe_stub_c_file
163            <- outputForeignStubs_help False{-not .h-} stub_c_output_w
164
165         return (maybe_stub_h_file, maybe_stub_c_file)
166   where
167     -- C stubs for "foreign export"ed functions.
168     stub_c_output_d = pprCode CStyle c_code
169     stub_c_output_w = showSDoc stub_c_output_d
170
171     -- Header file protos for "foreign export"ed functions.
172     stub_h_output_d = pprCode CStyle h_code
173     stub_h_output_w = showSDoc stub_h_output_d
174
175
176 -- Don't use doOutput for dumping the f. export stubs
177 -- since it is more than likely that the stubs file will
178 -- turn out to be empty, in which case no file should be created.
179 outputForeignStubs_help is_header ""      = return Nothing
180 outputForeignStubs_help is_header doc_str 
181    = newTempName suffix >>= \ fname ->
182      writeFile fname (include_prefix ++ doc_str) >>
183      return (Just suffix)
184   where
185     suffix
186        | is_header   = "h_stub"
187        | otherwise   = "c_stub"
188     include_prefix
189        | is_header   = "#include \"Rts.h\"\n"
190        | otherwise   = "#include \"RtsAPI.h\"\n"
191 \end{code}
192