2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1996
4 \section[GHC_Main]{Main driver for Glasgow Haskell compiler}
7 #include "HsVersions.h"
9 module Main ( main ) where
12 IMPORT_1_3(IO(stderr,hPutStr,hClose,openFile,IOMode(..)))
15 import RdrHsSyn ( RdrName )
16 import BasicTypes ( NewOrData(..) )
18 import ReadPrefix ( rdModule )
19 import Rename ( renameModule )
20 import RnMonad ( ExportEnv )
22 import MkIface -- several functions
23 import TcModule ( typecheckModule )
24 import Desugar ( deSugar, pprDsWarnings
25 #if __GLASGOW_HASKELL__ <= 200
29 import SimplCore ( core2core )
30 import CoreToStg ( topCoreBindsToStg )
31 import StgSyn ( collectFinalStgBinders, pprStgBindings )
32 import SimplStg ( stg2stg )
33 import CodeGen ( codeGen )
34 #if ! OMIT_NATIVE_CODEGEN
35 import AsmCodeGen ( dumpRealAsm, writeRealAsm )
38 import AbsCSyn ( absCNop, AbstractC )
39 import AbsCUtils ( flattenAbsC )
40 import CoreUnfold ( Unfolding )
41 import Bag ( emptyBag, isEmptyBag )
43 import ErrUtils ( pprBagOfErrors, ghcExit, doIfSet, dumpIfSet )
44 import Maybes ( maybeToBool, MaybeErr(..) )
45 import Specialise ( SpecialiseData(..) )
46 import StgSyn ( GenStgBinding )
47 import TcInstUtil ( InstInfo )
48 import TyCon ( isDataTyCon )
49 import UniqSupply ( mkSplitUniqSupply )
51 import PprAbsC ( dumpRealC, writeRealC )
52 import PprCore ( pprCoreBinding )
55 import Id ( GenId ) -- instances
56 import Name ( Name ) -- instances
57 import PprType ( GenType, GenTyVar ) -- instances
58 import TyVar ( GenTyVar ) -- instances
59 import Unique ( Unique ) -- instances
61 import Outputable ( PprStyle(..), Outputable(..), pprDumpStyle, pprErrorsStyle )
69 cmd_line_info = classifyOpts
75 doIt :: ([CoreToDo], [StgToDo]) -> IO ()
77 doIt (core_cmds, stg_cmds)
79 (hPutStr stderr ("Glasgow Haskell Compiler, version " ++
80 show PROJECTVERSION ++
81 ", for Haskell 1.4\n")) >>
86 rdModule >>= \ (mod_name, rdr_module) ->
88 dumpIfSet opt_D_dump_rdr "Reader"
89 (ppr pprDumpStyle rdr_module) >>
91 dumpIfSet opt_D_source_stats "Source Statistics"
92 (ppSourceStats rdr_module) >>
94 -- UniqueSupplies for later use (these are the only lower case uniques)
96 mkSplitUniqSupply 'r' >>= \ rn_uniqs -> -- renamer
98 mkSplitUniqSupply 'a' >>= \ tc_uniqs -> -- typechecker
100 mkSplitUniqSupply 'd' >>= \ ds_uniqs -> -- desugarer
102 mkSplitUniqSupply 's' >>= \ sm_uniqs -> -- core-to-core simplifier
104 mkSplitUniqSupply 'c' >>= \ c2s_uniqs -> -- core-to-stg
106 mkSplitUniqSupply 'g' >>= \ st_uniqs -> -- stg-to-stg passes
108 mkSplitUniqSupply 'f' >>= \ fl_uniqs -> -- absC flattener
110 mkSplitUniqSupply 'n' >>= \ ncg_uniqs -> -- native-code generator
113 show_pass "Renamer" >>
116 renameModule rn_uniqs rdr_module >>=
118 case maybe_rn_stuff of {
119 Nothing -> -- Hurrah! Renamer reckons that there's no need to
123 Just (rn_mod, iface_file_stuff, rn_name_supply, imported_modules) ->
124 -- Oh well, we've got to recompile for real
127 -- Safely past renaming: we can start the interface file:
128 -- (the iface file is produced incrementally, as we have
129 -- the information that we need...; we use "iface<blah>")
130 -- "endIface" finishes the job.
131 startIface mod_name >>= \ if_handle ->
132 ifaceMain if_handle iface_file_stuff >>
135 -- ******* TYPECHECKER
136 show_pass "TypeCheck" >>
138 typecheckModule tc_uniqs rn_name_supply rn_mod >>= \ maybe_tc_stuff ->
139 case maybe_tc_stuff of {
140 Nothing -> ghcExit 1; -- Type checker failed
143 local_tycons, local_classes, inst_info, pragma_tycon_specs,
148 show_pass "DeSugar" >>
150 deSugar ds_uniqs mod_name all_binds >>= \ desugared ->
153 -- ******* CORE-TO-CORE SIMPLIFICATION
154 show_pass "Core2Core" >>
157 local_data_tycons = filter isDataTyCon local_tycons
159 core2core core_cmds mod_name
160 sm_uniqs local_data_tycons pragma_tycon_specs desugared
163 SpecData _ _ _ gen_data_tycons all_tycon_specs _ _ _) ->
166 -- ******* STG-TO-STG SIMPLIFICATION
167 show_pass "Core2Stg" >>
170 stg_binds = topCoreBindsToStg c2s_uniqs simplified
173 show_pass "Stg2Stg" >>
175 stg2stg stg_cmds mod_name st_uniqs stg_binds
177 \ (stg_binds2, cost_centre_info) ->
179 dumpIfSet opt_D_dump_stg "STG syntax:"
180 (pprStgBindings pprDumpStyle stg_binds2)
183 -- Dump instance decls and type signatures into the interface file
185 final_ids = collectFinalStgBinders stg_binds2
188 ifaceDecls if_handle local_tycons local_classes inst_info final_ids simplified >>
189 endIface if_handle >>
190 -- We are definitely done w/ interface-file stuff at this point:
191 -- (See comments near call to "startIface".)
194 -- ******* "ABSTRACT", THEN "FLAT", THEN *REAL* C!
195 show_pass "CodeGen" >>
198 abstractC = codeGen mod_name -- module name for CC labelling
200 imported_modules -- import names for CC registering
201 gen_data_tycons -- type constructors generated locally
202 all_tycon_specs -- tycon specialisations
205 flat_abstractC = flattenAbsC fl_uniqs abstractC
207 dumpIfSet opt_D_dump_absC "Abstract C"
208 (dumpRealC abstractC) >>
210 dumpIfSet opt_D_dump_flatC "Flat Abstract C"
211 (dumpRealC flat_abstractC) >>
213 show_pass "CodeOutput" >>
215 -- You can have C (c_output) or assembly-language (ncg_output),
216 -- but not both. [Allowing for both gives a space leak on
217 -- flat_abstractC. WDP 94/10]
219 (flat_absC_c, flat_absC_ncg) =
220 case (maybeToBool opt_ProduceC || opt_D_dump_realC,
221 maybeToBool opt_ProduceS || opt_D_dump_asm) of
222 (True, False) -> (flat_abstractC, absCNop)
223 (False, True) -> (absCNop, flat_abstractC)
224 (False, False) -> (absCNop, absCNop)
225 (True, True) -> error "ERROR: Can't do both .hc and .s at the same time"
227 c_output_d = dumpRealC flat_absC_c
228 c_output_w = (\ f -> writeRealC f flat_absC_c)
230 #if OMIT_NATIVE_CODEGEN
231 ncg_output_d = error "*** GHC not built with a native-code generator ***"
232 ncg_output_w = ncg_output_d
234 ncg_output_d = dumpRealAsm flat_absC_ncg ncg_uniqs
235 ncg_output_w = (\ f -> writeRealAsm f flat_absC_ncg ncg_uniqs)
239 dumpIfSet opt_D_dump_asm "Asm code" ncg_output_d >>
240 doOutput opt_ProduceS ncg_output_w >>
242 dumpIfSet opt_D_dump_realC "Real C" c_output_d >>
243 doOutput opt_ProduceC c_output_w >>
248 -------------------------------------------------------------
249 -- ****** help functions:
252 = if opt_D_show_passes
253 then \ what -> hPutStr stderr ("*** "++what++":\n")
254 else \ what -> return ()
256 doOutput switch io_action
260 openFile fname WriteMode >>= \ handle ->
265 ppSourceStats (HsModule name version exports imports fixities decls src_loc)
267 [("ExportAll ", export_all), -- 1 if no export list
268 ("ExportDecls ", export_ds),
269 ("ExportModules ", export_ms),
270 ("Imports ", import_no),
271 (" ImpQual ", import_qual),
272 (" ImpAs ", import_as),
273 (" ImpAll ", import_all),
274 (" ImpPartial ", import_partial),
275 (" ImpHiding ", import_hiding),
276 ("FixityDecls ", fixity_ds),
277 ("DefaultDecls ", default_ds),
278 ("TypeDecls ", type_ds),
279 ("DataDecls ", data_ds),
280 ("NewTypeDecls ", newt_ds),
281 ("DataConstrs ", data_constrs),
282 ("DataDerivings ", data_derivs),
283 ("ClassDecls ", class_ds),
284 ("ClassMethods ", class_method_ds),
285 ("DefaultMethods ", default_method_ds),
286 ("InstDecls ", inst_ds),
287 ("InstMethods ", inst_method_ds),
288 ("TypeSigs ", bind_tys),
289 ("ValBinds ", val_bind_ds),
290 ("FunBinds ", fn_bind_ds),
291 ("InlineMeths ", method_inlines),
292 ("InlineBinds ", bind_inlines),
293 -- ("SpecialisedData ", data_specs),
294 -- ("SpecialisedInsts ", inst_specs),
295 ("SpecialisedMeths ", method_specs),
296 ("SpecialisedBinds ", bind_specs)
299 pp_val (str, 0) = empty
300 pp_val (str, n) = hcat [text str, int n]
302 fixity_ds = length fixities
303 type_decls = [d | TyD d@(TySynonym _ _ _ _) <- decls]
304 data_decls = [d | TyD d@(TyData DataType _ _ _ _ _ _ _) <- decls]
305 newt_decls = [d | TyD d@(TyData NewType _ _ _ _ _ _ _) <- decls]
306 type_ds = length type_decls
307 data_ds = length data_decls
308 newt_ds = length newt_decls
309 class_decls = [d | ClD d <- decls]
310 class_ds = length class_decls
311 inst_decls = [d | InstD d <- decls]
312 inst_ds = length inst_decls
313 default_ds = length [() | DefD _ <- decls]
314 val_decls = [d | ValD d <- decls]
316 real_exports = case exports of { Nothing -> []; Just es -> es }
317 n_exports = length real_exports
318 export_ms = length [() | IEModuleContents _ <- real_exports]
319 export_ds = n_exports - export_ms
320 export_all = case exports of { Nothing -> 1; other -> 0 }
322 (val_bind_ds, fn_bind_ds, bind_tys, bind_specs, bind_inlines)
323 = count_binds (foldr ThenBinds EmptyBinds val_decls)
325 (import_no, import_qual, import_as, import_all, import_partial, import_hiding)
326 = foldr add6 (0,0,0,0,0,0) (map import_info imports)
327 (data_constrs, data_derivs)
328 = foldr add2 (0,0) (map data_info (newt_decls ++ data_decls))
329 (class_method_ds, default_method_ds)
330 = foldr add2 (0,0) (map class_info class_decls)
331 (inst_method_ds, method_specs, method_inlines)
332 = foldr add3 (0,0,0) (map inst_info inst_decls)
335 count_binds EmptyBinds = (0,0,0,0,0)
336 count_binds (ThenBinds b1 b2) = count_binds b1 `add5` count_binds b2
337 count_binds (MonoBind b sigs _) = case (count_monobinds b, count_sigs sigs) of
338 ((vs,fs),(ts,_,ss,is)) -> (vs,fs,ts,ss,is)
340 count_monobinds EmptyMonoBinds = (0,0)
341 count_monobinds (AndMonoBinds b1 b2) = count_monobinds b1 `add2` count_monobinds b2
342 count_monobinds (PatMonoBind (VarPatIn n) r _) = (1,0)
343 count_monobinds (PatMonoBind p r _) = (0,1)
344 count_monobinds (FunMonoBind f _ m _) = (0,1)
346 count_sigs sigs = foldr add4 (0,0,0,0) (map sig_info sigs)
348 sig_info (Sig _ _ _) = (1,0,0,0)
349 sig_info (ClassOpSig _ _ _ _) = (0,1,0,0)
350 sig_info (SpecSig _ _ _ _) = (0,0,1,0)
351 sig_info (InlineSig _ _) = (0,0,0,1)
352 sig_info _ = (0,0,0,0)
354 import_info (ImportDecl _ qual _ as spec _)
355 = add6 (1, qual_info qual, as_info as, 0,0,0) (spec_info spec)
360 spec_info Nothing = (0,0,0,1,0,0)
361 spec_info (Just (False, _)) = (0,0,0,0,1,0)
362 spec_info (Just (True, _)) = (0,0,0,0,0,1)
364 data_info (TyData _ _ _ _ constrs derivs _ _)
365 = (length constrs, case derivs of {Nothing -> 0; Just ds -> length ds})
367 class_info (ClassDecl _ _ _ meth_sigs def_meths _ _)
368 = case count_sigs meth_sigs of
370 (classops, addpr (count_monobinds def_meths))
372 inst_info (InstDecl _ inst_meths inst_sigs _ _)
373 = case count_sigs inst_sigs of
375 (addpr (count_monobinds inst_meths), ss, is)
379 add2 (x1,x2) (y1,y2) = (x1+y1,x2+y2)
380 add3 (x1,x2,x3) (y1,y2,y3) = (x1+y1,x2+y2,x3+y3)
381 add4 (x1,x2,x3,x4) (y1,y2,y3,y4) = (x1+y1,x2+y2,x3+y3,x4+y4)
382 add5 (x1,x2,x3,x4,x5) (y1,y2,y3,y4,y5) = (x1+y1,x2+y2,x3+y3,x4+y4,x5+y5)
383 add6 (x1,x2,x3,x4,x5,x6) (y1,y2,y3,y4,y5,y6) = (x1+y1,x2+y2,x3+y3,x4+y4,x5+y5,x6+y6)