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
13 import PreludeGlaST ( thenPrimIO, _FILE{-instances-} ) -- ToDo: STOP using this...
18 import ReadPrefix ( rdModule )
19 import Rename ( renameModule )
20 import Typecheck ( typecheckModule, InstInfo )
21 import Desugar ( deSugar, DsMatchContext, pprDsWarnings )
22 import SimplCore ( core2core )
23 import CoreToStg ( topCoreBindsToStg )
24 import SimplStg ( stg2stg )
25 import CodeGen ( codeGen )
26 #if ! OMIT_NATIVE_CODEGEN
27 import AsmCodeGen ( dumpRealAsm, writeRealAsm )
30 import AbsCSyn ( absCNop, AbstractC )
31 import AbsCUtils ( flattenAbsC )
32 import Bag ( emptyBag, isEmptyBag )
34 import ErrUtils ( pprBagOfErrors )
35 import Maybes ( maybeToBool, MaybeErr(..) )
36 import PrelInfo ( builtinNameInfo )
37 import RdrHsSyn ( getRawExportees )
38 import Specialise ( SpecialiseData(..) )
39 import StgSyn ( pprPlainStgBinding, GenStgBinding )
41 import PprAbsC ( dumpRealC, writeRealC )
42 import PprCore ( pprCoreBinding )
43 import PprStyle ( PprStyle(..) )
46 import Id ( GenId ) -- instances
47 import Name ( Name ) -- instances
48 import ProtoName ( ProtoName ) -- instances
49 import PprType ( GenType, GenTyVar ) -- instances
50 import TyVar ( GenTyVar ) -- instances
51 import Unique ( Unique) -- instances
54 --import MkIface ( mkInterface )
62 = readMn stdin `thenMn` \ input_pgm ->
64 cmd_line_info = classifyOpts
66 doIt cmd_line_info input_pgm
70 doIt :: ([CoreToDo], [StgToDo]) -> String -> MainIO ()
72 doIt (core_cmds, stg_cmds) input_pgm
73 = doDump opt_Verbose "Glasgow Haskell Compiler, version 1.3-xx" ""
77 show_pass "Reader" `thenMn_`
80 \ (mod_name, export_list_fns, absyn_tree) ->
83 -- reader things used much later
84 ds_mod_name = mod_name
85 if_mod_name = mod_name
86 co_mod_name = mod_name
87 st_mod_name = mod_name
88 cc_mod_name = mod_name
90 doDump opt_D_dump_rdr "Reader:"
91 (pp_show (ppr pprStyle absyn_tree)) `thenMn_`
93 doDump opt_D_source_stats "\nSource Statistics:"
94 (pp_show (ppSourceStats absyn_tree)) `thenMn_`
96 -- UniqueSupplies for later use (these are the only lower case uniques)
97 getSplitUniqSupplyMn 'r' `thenMn` \ rn_uniqs -> -- renamer
98 getSplitUniqSupplyMn 't' `thenMn` \ tc_uniqs -> -- typechecker
99 getSplitUniqSupplyMn 'd' `thenMn` \ ds_uniqs -> -- desugarer
100 getSplitUniqSupplyMn 's' `thenMn` \ sm_uniqs -> -- core-to-core simplifier
101 getSplitUniqSupplyMn 'c' `thenMn` \ c2s_uniqs -> -- core-to-stg
102 getSplitUniqSupplyMn 'g' `thenMn` \ st_uniqs -> -- stg-to-stg passes
103 getSplitUniqSupplyMn 'f' `thenMn` \ fl_uniqs -> -- absC flattener
104 getSplitUniqSupplyMn 'n' `thenMn` \ ncg_uniqs -> -- native-code generator
107 show_pass "Renamer" `thenMn_`
110 of { (init_val_lookup_fn, init_tc_lookup_fn) ->
112 case (renameModule (init_val_lookup_fn, init_tc_lookup_fn)
115 of { (mod4, import_names, final_name_funs, rn_errs_bag) ->
117 -- renamer things used much later
118 cc_import_names = import_names
121 if (not (isEmptyBag rn_errs_bag)) then
122 writeMn stderr (ppShow pprCols (pprBagOfErrors pprErrorsStyle rn_errs_bag))
123 `thenMn_` writeMn stderr "\n"
126 else -- No renaming errors ...
128 doDump opt_D_dump_rn "Renamer:"
129 (pp_show (ppr pprStyle mod4)) `thenMn_`
131 -- ******* TYPECHECKER
132 show_pass "TypeCheck" `thenMn_`
133 case (case (typecheckModule tc_uniqs final_name_funs mod4) of
134 Succeeded (stuff, warns)
135 -> (emptyBag, warns, stuff)
137 -> (errs, warns, error "tc_results"))
139 of { (tc_errs_bag, tc_warns_bag, tc_results) ->
141 (if (isEmptyBag tc_warns_bag) then
144 writeMn stderr (ppShow pprCols (pprBagOfErrors pprErrorsStyle tc_errs_bag))
145 `thenMn_` writeMn stderr "\n"
148 if (not (isEmptyBag tc_errs_bag)) then
149 writeMn stderr (ppShow pprCols (pprBagOfErrors pprErrorsStyle tc_errs_bag))
150 `thenMn_` writeMn stderr "\n"
153 else ( -- No typechecking errors ...
156 of { (typechecked_quint@(recsel_binds, class_binds, inst_binds, val_binds, const_binds),
157 interface_stuff@(_,_,_,_,_), -- @-pat just for strictness...
158 (local_tycons,local_classes), pragma_tycon_specs, ddump_deriv) ->
160 doDump opt_D_dump_tc "Typechecked:"
162 ppr pprStyle recsel_binds,
163 ppr pprStyle class_binds,
164 ppr pprStyle inst_binds,
165 ppAboves (map (\ (i,e) -> ppr pprStyle (VarMonoBind i e)) const_binds),
166 ppr pprStyle val_binds])) `thenMn_`
168 doDump opt_D_dump_deriv "Derived instances:"
169 (pp_show (ddump_deriv pprStyle)) `thenMn_`
172 show_pass "DeSugar" `thenMn_`
174 (desugared,ds_warnings)
175 = deSugar ds_uniqs ds_mod_name typechecked_quint
177 (if isEmptyBag ds_warnings then
180 writeMn stderr (ppShow pprCols (pprDsWarnings pprErrorsStyle ds_warnings))
181 `thenMn_` writeMn stderr "\n"
184 doDump opt_D_dump_ds "Desugared:" (pp_show (ppAboves
185 (map (pprCoreBinding pprStyle) desugared)))
188 -- ******* CORE-TO-CORE SIMPLIFICATION (NB: I/O op)
189 core2core core_cmds co_mod_name pprStyle
190 sm_uniqs local_tycons pragma_tycon_specs desugared
193 \ (simplified, inlinings_env,
194 SpecData _ _ _ gen_tycons all_tycon_specs _ _ _) ->
196 doDump opt_D_dump_simpl "Simplified:" (pp_show (ppAboves
197 (map (pprCoreBinding pprStyle) simplified)))
200 -- ******* STG-TO-STG SIMPLIFICATION
201 show_pass "Core2Stg" `thenMn_`
203 stg_binds = topCoreBindsToStg c2s_uniqs simplified
206 show_pass "Stg2Stg" `thenMn_`
207 stg2stg stg_cmds st_mod_name pprStyle st_uniqs stg_binds
210 \ (stg_binds2, cost_centre_info) ->
212 doDump opt_D_dump_stg "STG syntax:"
213 (pp_show (ppAboves (map (pprPlainStgBinding pprStyle) stg_binds2)))
217 -- ******* INTERFACE GENERATION (needs STG output)
219 mod_name = "_TestName_"
220 export_list_fns = (\ x -> False, \ x -> False)
221 inlinings_env = nullIdEnv
226 if_inst_info = emptyBag
230 show_pass "Interface" `thenMn_`
233 = mkInterface if_mod_name export_list_fns
234 inlinings_env all_tycon_specs
238 doOutput opt_ProduceHi ( \ file ->
239 ppAppendFile file 1000{-pprCols-} mod_interface )
243 -- ******* "ABSTRACT", THEN "FLAT", THEN *REAL* C!
244 show_pass "CodeGen" `thenMn_`
246 abstractC = codeGen cc_mod_name -- module name for CC labelling
248 cc_import_names -- import names for CC registering
249 gen_tycons -- type constructors generated locally
250 all_tycon_specs -- tycon specialisations
253 flat_abstractC = flattenAbsC fl_uniqs abstractC
255 doDump opt_D_dump_absC "Abstract C:"
256 (dumpRealC abstractC) `thenMn_`
258 doDump opt_D_dump_flatC "Flat Abstract C:"
259 (dumpRealC flat_abstractC) `thenMn_`
261 -- You can have C (c_output) or assembly-language (ncg_output),
262 -- but not both. [Allowing for both gives a space leak on
263 -- flat_abstractC. WDP 94/10]
265 (flat_absC_c, flat_absC_ncg) =
266 case (maybeToBool opt_ProduceC || opt_D_dump_realC,
267 maybeToBool opt_ProduceS || opt_D_dump_asm) of
268 (True, False) -> (flat_abstractC, absCNop)
269 (False, True) -> (absCNop, flat_abstractC)
270 (False, False) -> (absCNop, absCNop)
271 (True, True) -> error "ERROR: Can't do both .hc and .s at the same time"
273 c_output_d = dumpRealC flat_absC_c
274 c_output_w = (\ f -> writeRealC f flat_absC_c)
276 #if OMIT_NATIVE_CODEGEN
277 ncg_output_d = error "*** GHC not built with a native-code generator ***"
278 ncg_output_w = ncg_output_d
280 ncg_output_d = dumpRealAsm flat_absC_ncg ncg_uniqs
281 ncg_output_w = (\ f -> writeRealAsm f flat_absC_ncg ncg_uniqs)
285 doDump opt_D_dump_asm "" ncg_output_d `thenMn_`
286 doOutput opt_ProduceS ncg_output_w `thenMn_`
288 doDump opt_D_dump_realC "" c_output_d `thenMn_`
289 doOutput opt_ProduceC c_output_w `thenMn_`
293 -------------------------------------------------------------
294 -- ****** printing styles and column width:
296 pprCols = (80 :: Int) -- could make configurable
298 (pprStyle, pprErrorsStyle)
299 = if opt_PprStyle_All then
300 (PprShowAll, PprShowAll)
301 else if opt_PprStyle_Debug then
303 else if opt_PprStyle_User then
304 (PprForUser, PprForUser)
306 (PprDebug, PprForUser)
308 pp_show p = ppShow {-WAS:pprCols-}10000{-random-} p
310 -------------------------------------------------------------
311 -- ****** help functions:
314 = if opt_D_show_passes
315 then \ what -> writeMn stderr ("*** "++what++":\n")
316 else \ what -> returnMn ()
318 doOutput switch io_action
320 Nothing -> returnMn ()
321 Just fn -> let fname = _UNPK_ fn in
322 fopen fname "a+" `thenPrimIO` \ file ->
323 if (file == ``NULL'') then
324 error ("doOutput: failed to open:"++fname)
326 io_action file `thenMn` \ () ->
327 fclose file `thenPrimIO` \ status ->
330 else error ("doOutput: closed failed: "{-++show status++" "-}++fname)
332 doDump switch hdr string
334 then writeMn stderr hdr `thenMn_`
335 writeMn stderr ('\n': string) `thenMn_`
340 ppSourceStats (HsModule name exports imports fixities typedecls typesigs
341 classdecls instdecls instsigs defdecls binds
342 [{-no sigs-}] src_loc)
343 = ppAboves (map pp_val
344 [("ExportAll ", export_all), -- 1 if no export list
345 ("ExportDecls ", export_ds),
346 ("ExportModules ", export_ms),
347 ("Imports ", import_no),
348 (" ImpQual ", import_qual),
349 (" ImpAs ", import_as),
350 (" ImpAll ", import_all),
351 (" ImpPartial ", import_partial),
352 (" ImpHiding ", import_hiding),
353 ("FixityDecls ", fixity_ds),
354 ("DefaultDecls ", defalut_ds),
355 ("TypeDecls ", type_ds),
356 ("DataDecls ", data_ds),
357 ("NewTypeDecls ", newt_ds),
358 ("DataConstrs ", data_constrs),
359 ("DataDerivings ", data_derivs),
360 ("ClassDecls ", class_ds),
361 ("ClassMethods ", class_method_ds),
362 ("DefaultMethods ", default_method_ds),
363 ("InstDecls ", inst_ds),
364 ("InstMethods ", inst_method_ds),
365 ("TypeSigs ", bind_tys),
366 ("ValBinds ", val_bind_ds),
367 ("FunBinds ", fn_bind_ds),
368 ("InlineMeths ", method_inlines),
369 ("InlineBinds ", bind_inlines),
370 ("SpecialisedData ", data_specs),
371 ("SpecialisedInsts ", inst_specs),
372 ("SpecialisedMeths ", method_specs),
373 ("SpecialisedBinds ", bind_specs)
376 pp_val (str, 0) = ppNil
377 pp_val (str, n) = ppBesides [ppStr str, ppInt n]
379 (export_decls, export_mods) = getRawExportees exports
380 type_decls = filter is_type_decl typedecls
381 data_decls = filter is_data_decl typedecls
382 newt_decls = filter is_newt_decl typedecls
384 export_ds = length export_decls
385 export_ms = length export_mods
386 export_all = if export_ds == 0 && export_ms == 0 then 1 else 0
388 fixity_ds = length fixities
389 defalut_ds = length defdecls
390 type_ds = length type_decls
391 data_ds = length data_decls
392 newt_ds = length newt_decls
393 class_ds = length classdecls
394 inst_ds = length instdecls
396 (val_bind_ds, fn_bind_ds, bind_tys, bind_specs, bind_inlines)
399 (import_no, import_qual, import_as, import_all, import_partial, import_hiding)
400 = foldr add6 (0,0,0,0,0,0) (map import_info imports)
401 (data_constrs, data_derivs)
402 = foldr add2 (0,0) (map data_info (newt_decls ++ data_decls))
403 (class_method_ds, default_method_ds)
404 = foldr add2 (0,0) (map class_info classdecls)
405 (inst_method_ds, method_specs, method_inlines)
406 = foldr add3 (0,0,0) (map inst_info instdecls)
408 data_specs = length typesigs
409 inst_specs = length instsigs
411 count_binds EmptyBinds = (0,0,0,0,0)
412 count_binds (ThenBinds b1 b2) = count_binds b1 `add5` count_binds b2
413 count_binds (SingleBind b) = case count_bind b of
414 (vs,fs) -> (vs,fs,0,0,0)
415 count_binds (BindWith b sigs) = case (count_bind b, count_sigs sigs) of
416 ((vs,fs),(ts,_,ss,is)) -> (vs,fs,ts,ss,is)
418 count_bind EmptyBind = (0,0)
419 count_bind (NonRecBind b) = count_monobinds b
420 count_bind (RecBind b) = count_monobinds b
422 count_monobinds EmptyMonoBinds = (0,0)
423 count_monobinds (AndMonoBinds b1 b2) = count_monobinds b1 `add2` count_monobinds b2
424 count_monobinds (PatMonoBind (VarPatIn n) r _) = (1,0)
425 count_monobinds (PatMonoBind p r _) = (0,1)
426 count_monobinds (FunMonoBind f m _) = (0,1)
428 count_sigs sigs = foldr add4 (0,0,0,0) (map sig_info sigs)
430 sig_info (Sig _ _ _ _) = (1,0,0,0)
431 sig_info (ClassOpSig _ _ _ _) = (0,1,0,0)
432 sig_info (SpecSig _ _ _ _) = (0,0,1,0)
433 sig_info (InlineSig _ _) = (0,0,0,1)
434 sig_info _ = (0,0,0,0)
436 import_info (ImportMod _ qual as spec)
437 = add6 (1, qual_info qual, as_info as, 0,0,0) (spec_info spec)
442 spec_info Nothing = (0,0,0,1,0,0)
443 spec_info (Just (False, _)) = (0,0,0,0,1,0)
444 spec_info (Just (True, _)) = (0,0,0,0,0,1)
446 data_info (TyData _ _ _ constrs derivs _ _)
447 = (length constrs, case derivs of {Nothing -> 0; Just ds -> length ds})
448 data_info (TyNew _ _ _ constr derivs _ _)
449 = (length constr, case derivs of {Nothing -> 0; Just ds -> length ds})
451 class_info (ClassDecl _ _ _ meth_sigs def_meths _ _)
452 = case count_sigs meth_sigs of
454 (classops, addpr (count_monobinds def_meths))
456 inst_info (InstDecl _ _ inst_meths _ _ inst_sigs _ _)
457 = case count_sigs inst_sigs of
459 (addpr (count_monobinds inst_meths), ss, is)
461 is_type_decl (TySynonym _ _ _ _) = True
462 is_type_decl _ = False
463 is_data_decl (TyData _ _ _ _ _ _ _) = True
464 is_data_decl _ = False
465 is_newt_decl (TyNew _ _ _ _ _ _ _) = True
466 is_newt_decl _ = False
470 add2 (x1,x2) (y1,y2) = (x1+y1,x2+y2)
471 add3 (x1,x2,x3) (y1,y2,y3) = (x1+y1,x2+y2,x3+y3)
472 add4 (x1,x2,x3,x4) (y1,y2,y3,y4) = (x1+y1,x2+y2,x3+y3,x4+y4)
473 add5 (x1,x2,x3,x4,x5) (y1,y2,y3,y4,y5) = (x1+y1,x2+y2,x3+y3,x4+y4,x5+y5)
474 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)