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(hGetContents,stdin,stderr,hPutStr,hClose,openFile,IOMode(..)))
16 import ReadPrefix ( rdModule )
17 import Rename ( renameModule )
18 import MkIface -- several functions
19 import TcModule ( typecheckModule )
20 import Desugar ( deSugar, DsMatchContext, pprDsWarnings )
21 import SimplCore ( core2core )
22 import CoreToStg ( topCoreBindsToStg )
23 import SimplStg ( stg2stg )
24 import CodeGen ( codeGen )
25 #if ! OMIT_NATIVE_CODEGEN
26 import AsmCodeGen ( dumpRealAsm, writeRealAsm )
29 import AbsCSyn ( absCNop, AbstractC )
30 import AbsCUtils ( flattenAbsC )
31 import CoreUnfold ( Unfolding )
32 import Bag ( emptyBag, isEmptyBag )
34 import ErrUtils ( pprBagOfErrors, ghcExit )
35 import Maybes ( maybeToBool, MaybeErr(..) )
36 import RdrHsSyn ( getRawExportees )
37 import Specialise ( SpecialiseData(..) )
38 import StgSyn ( pprPlainStgBinding, GenStgBinding )
39 import TcInstUtil ( InstInfo )
40 import TyCon ( isDataTyCon )
41 import UniqSupply ( mkSplitUniqSupply )
43 import PprAbsC ( dumpRealC, writeRealC )
44 import PprCore ( pprCoreBinding )
45 import PprStyle ( PprStyle(..) )
48 import Id ( GenId ) -- instances
49 import Name ( Name, RdrName ) -- instances
50 import PprType ( GenType, GenTyVar ) -- instances
51 import RnHsSyn ( RnName ) -- instances
52 import TyVar ( GenTyVar ) -- instances
53 import Unique ( Unique ) -- instances
58 = hGetContents stdin >>= \ input_pgm ->
60 cmd_line_info = classifyOpts
62 doIt cmd_line_info input_pgm
66 doIt :: ([CoreToDo], [StgToDo]) -> String -> IO ()
68 doIt (core_cmds, stg_cmds) input_pgm
69 = doDump opt_Verbose "Glasgow Haskell Compiler, version 2.01, for Haskell 1.3" "" >>
74 rdModule >>= \ (mod_name, rdr_module) ->
76 doDump opt_D_dump_rdr "Reader:"
77 (pp_show (ppr pprStyle rdr_module)) >>
79 doDump opt_D_source_stats "\nSource Statistics:"
80 (pp_show (ppSourceStats rdr_module)) >>
82 -- UniqueSupplies for later use (these are the only lower case uniques)
83 mkSplitUniqSupply 'r' >>= \ rn_uniqs -> -- renamer
84 mkSplitUniqSupply 'a' >>= \ tc_uniqs -> -- typechecker
85 mkSplitUniqSupply 'd' >>= \ ds_uniqs -> -- desugarer
86 mkSplitUniqSupply 's' >>= \ sm_uniqs -> -- core-to-core simplifier
87 mkSplitUniqSupply 'c' >>= \ c2s_uniqs -> -- core-to-stg
88 mkSplitUniqSupply 'g' >>= \ st_uniqs -> -- stg-to-stg passes
89 mkSplitUniqSupply 'f' >>= \ fl_uniqs -> -- absC flattener
90 mkSplitUniqSupply 'n' >>= \ ncg_uniqs -> -- native-code generator
93 show_pass "Renamer" >>
96 renameModule rn_uniqs rdr_module >>=
97 \ (rn_mod, rn_env, import_names,
98 export_stuff, usage_stuff,
99 rn_errs_bag, rn_warns_bag) ->
101 if (not (isEmptyBag rn_errs_bag)) then
102 hPutStr stderr (ppShow pprCols (pprBagOfErrors pprErrorsStyle rn_errs_bag))
103 >> hPutStr stderr "\n" >>
104 hPutStr stderr (ppShow pprCols (pprBagOfErrors pprErrorsStyle rn_warns_bag))
105 >> hPutStr stderr "\n" >>
108 else -- No renaming errors ...
110 (if (isEmptyBag rn_warns_bag) then
113 hPutStr stderr (ppShow pprCols (pprBagOfErrors pprErrorsStyle rn_warns_bag))
114 >> hPutStr stderr "\n"
117 doDump opt_D_dump_rn "Renamer:"
118 (pp_show (ppr pprStyle rn_mod)) >>
120 -- Safely past renaming: we can start the interface file:
121 -- (the iface file is produced incrementally, as we have
122 -- the information that we need...; we use "iface<blah>")
123 -- "endIface" finishes the job.
125 (usages_map, version_info, instance_modules) = usage_stuff
127 startIface mod_name >>= \ if_handle ->
128 ifaceUsages if_handle usages_map >>
129 ifaceVersions if_handle version_info >>
130 ifaceExportList if_handle export_stuff rn_env >>
131 ifaceFixities if_handle rn_mod >>
132 ifaceInstanceModules if_handle instance_modules >>
134 -- ******* TYPECHECKER
135 show_pass "TypeCheck" >>
137 case (case (typecheckModule tc_uniqs {-idinfo_fm-} rn_env rn_mod) of
138 Succeeded (stuff, warns)
139 -> (emptyBag, warns, stuff)
141 -> (errs, warns, error "tc_results"))
143 of { (tc_errs_bag, tc_warns_bag, tc_results) ->
145 if (not (isEmptyBag tc_errs_bag)) then
146 hPutStr stderr (ppShow pprCols (pprBagOfErrors pprErrorsStyle tc_errs_bag))
147 >> hPutStr stderr "\n" >>
148 hPutStr stderr (ppShow pprCols (pprBagOfErrors pprErrorsStyle tc_warns_bag))
149 >> hPutStr stderr "\n" >>
152 else ( -- No typechecking errors ...
154 (if (isEmptyBag tc_warns_bag) then
157 hPutStr stderr (ppShow pprCols (pprBagOfErrors pprErrorsStyle tc_warns_bag))
158 >> hPutStr stderr "\n"
162 of { (typechecked_quint@(recsel_binds, class_binds, inst_binds, val_binds, const_binds),
163 interface_stuff@(_,local_tycons,_,_),
164 pragma_tycon_specs, ddump_deriv) ->
166 doDump opt_D_dump_tc "Typechecked:"
168 ppr pprStyle recsel_binds,
169 ppr pprStyle class_binds,
170 ppr pprStyle inst_binds,
171 ppAboves (map (\ (i,e) -> ppr pprStyle (VarMonoBind i e)) const_binds),
172 ppr pprStyle val_binds])) >>
174 doDump opt_D_dump_deriv "Derived instances:"
175 (pp_show (ddump_deriv pprStyle)) >>
177 -- OK, now do the interface stuff that relies on typechecker output:
178 ifaceDecls if_handle interface_stuff >>
179 ifaceInstances if_handle interface_stuff >>
182 show_pass "DeSugar" >>
185 (desugared,ds_warnings)
186 = deSugar ds_uniqs mod_name typechecked_quint
188 (if isEmptyBag ds_warnings then
191 hPutStr stderr (ppShow pprCols (pprDsWarnings pprErrorsStyle ds_warnings))
192 >> hPutStr stderr "\n"
195 doDump opt_D_dump_ds "Desugared:" (pp_show (ppAboves
196 (map (pprCoreBinding pprStyle) desugared)))
199 -- ******* CORE-TO-CORE SIMPLIFICATION (NB: I/O op)
200 show_pass "Core2Core" >>
203 local_data_tycons = filter isDataTyCon local_tycons
205 core2core core_cmds mod_name pprStyle
206 sm_uniqs local_data_tycons pragma_tycon_specs desugared
209 \ (simplified, inlinings_env,
210 SpecData _ _ _ gen_tycons all_tycon_specs _ _ _) ->
212 doDump opt_D_dump_simpl "Simplified:" (pp_show (ppAboves
213 (map (pprCoreBinding pprStyle) simplified)))
216 -- ******* STG-TO-STG SIMPLIFICATION
217 show_pass "Core2Stg" >>
220 stg_binds = topCoreBindsToStg c2s_uniqs simplified
223 show_pass "Stg2Stg" >>
225 stg2stg stg_cmds mod_name pprStyle st_uniqs stg_binds
228 \ (stg_binds2, cost_centre_info) ->
230 doDump opt_D_dump_stg "STG syntax:"
231 (pp_show (ppAboves (map (pprPlainStgBinding pprStyle) stg_binds2)))
234 -- We are definitely done w/ interface-file stuff at this point:
235 -- (See comments near call to "startIface".)
236 endIface if_handle >>
238 -- ******* "ABSTRACT", THEN "FLAT", THEN *REAL* C!
239 show_pass "CodeGen" >>
242 abstractC = codeGen mod_name -- module name for CC labelling
244 import_names -- import names for CC registering
245 gen_tycons -- type constructors generated locally
246 all_tycon_specs -- tycon specialisations
249 flat_abstractC = flattenAbsC fl_uniqs abstractC
251 doDump opt_D_dump_absC "Abstract C:"
252 (dumpRealC abstractC) >>
254 doDump opt_D_dump_flatC "Flat Abstract C:"
255 (dumpRealC flat_abstractC) >>
257 -- You can have C (c_output) or assembly-language (ncg_output),
258 -- but not both. [Allowing for both gives a space leak on
259 -- flat_abstractC. WDP 94/10]
261 (flat_absC_c, flat_absC_ncg) =
262 case (maybeToBool opt_ProduceC || opt_D_dump_realC,
263 maybeToBool opt_ProduceS || opt_D_dump_asm) of
264 (True, False) -> (flat_abstractC, absCNop)
265 (False, True) -> (absCNop, flat_abstractC)
266 (False, False) -> (absCNop, absCNop)
267 (True, True) -> error "ERROR: Can't do both .hc and .s at the same time"
269 c_output_d = dumpRealC flat_absC_c
270 c_output_w = (\ f -> writeRealC f flat_absC_c)
272 #if OMIT_NATIVE_CODEGEN
273 ncg_output_d = error "*** GHC not built with a native-code generator ***"
274 ncg_output_w = ncg_output_d
276 ncg_output_d = dumpRealAsm flat_absC_ncg ncg_uniqs
277 ncg_output_w = (\ f -> writeRealAsm f flat_absC_ncg ncg_uniqs)
281 doDump opt_D_dump_asm "" ncg_output_d >>
282 doOutput opt_ProduceS ncg_output_w >>
284 doDump opt_D_dump_realC "" c_output_d >>
285 doOutput opt_ProduceC c_output_w >>
290 -------------------------------------------------------------
291 -- ****** printing styles and column width:
293 pprCols = (80 :: Int) -- could make configurable
295 (pprStyle, pprErrorsStyle)
296 = if opt_PprStyle_All then
297 (PprShowAll, PprShowAll)
298 else if opt_PprStyle_Debug then
300 else if opt_PprStyle_User then
301 (PprForUser, PprForUser)
303 (PprDebug, PprForUser)
305 pp_show p = ppShow {-WAS:pprCols-}10000{-random-} p
307 -------------------------------------------------------------
308 -- ****** help functions:
311 = if opt_D_show_passes
312 then \ what -> hPutStr stderr ("*** "++what++":\n")
313 else \ what -> return ()
315 doOutput switch io_action
319 openFile fname WriteMode >>= \ handle ->
323 doDump switch hdr string
325 then hPutStr stderr hdr >>
326 hPutStr stderr ('\n': string) >>
331 ppSourceStats (HsModule name version exports imports fixities typedecls typesigs
332 classdecls instdecls instsigs defdecls binds
333 [{-no sigs-}] src_loc)
334 = ppAboves (map pp_val
335 [("ExportAll ", export_all), -- 1 if no export list
336 ("ExportDecls ", export_ds),
337 ("ExportModules ", export_ms),
338 ("Imports ", import_no),
339 (" ImpQual ", import_qual),
340 (" ImpAs ", import_as),
341 (" ImpAll ", import_all),
342 (" ImpPartial ", import_partial),
343 (" ImpHiding ", import_hiding),
344 ("FixityDecls ", fixity_ds),
345 ("DefaultDecls ", defalut_ds),
346 ("TypeDecls ", type_ds),
347 ("DataDecls ", data_ds),
348 ("NewTypeDecls ", newt_ds),
349 ("DataConstrs ", data_constrs),
350 ("DataDerivings ", data_derivs),
351 ("ClassDecls ", class_ds),
352 ("ClassMethods ", class_method_ds),
353 ("DefaultMethods ", default_method_ds),
354 ("InstDecls ", inst_ds),
355 ("InstMethods ", inst_method_ds),
356 ("TypeSigs ", bind_tys),
357 ("ValBinds ", val_bind_ds),
358 ("FunBinds ", fn_bind_ds),
359 ("InlineMeths ", method_inlines),
360 ("InlineBinds ", bind_inlines),
361 ("SpecialisedData ", data_specs),
362 ("SpecialisedInsts ", inst_specs),
363 ("SpecialisedMeths ", method_specs),
364 ("SpecialisedBinds ", bind_specs)
367 pp_val (str, 0) = ppNil
368 pp_val (str, n) = ppBesides [ppStr str, ppInt n]
370 (export_decls, export_mods) = getRawExportees exports
371 type_decls = filter is_type_decl typedecls
372 data_decls = filter is_data_decl typedecls
373 newt_decls = filter is_newt_decl typedecls
375 export_ds = length export_decls
376 export_ms = length export_mods
377 export_all = if export_ds == 0 && export_ms == 0 then 1 else 0
379 fixity_ds = length fixities
380 defalut_ds = length defdecls
381 type_ds = length type_decls
382 data_ds = length data_decls
383 newt_ds = length newt_decls
384 class_ds = length classdecls
385 inst_ds = length instdecls
387 (val_bind_ds, fn_bind_ds, bind_tys, bind_specs, bind_inlines)
390 (import_no, import_qual, import_as, import_all, import_partial, import_hiding)
391 = foldr add6 (0,0,0,0,0,0) (map import_info imports)
392 (data_constrs, data_derivs)
393 = foldr add2 (0,0) (map data_info (newt_decls ++ data_decls))
394 (class_method_ds, default_method_ds)
395 = foldr add2 (0,0) (map class_info classdecls)
396 (inst_method_ds, method_specs, method_inlines)
397 = foldr add3 (0,0,0) (map inst_info instdecls)
399 data_specs = length typesigs
400 inst_specs = length instsigs
402 count_binds EmptyBinds = (0,0,0,0,0)
403 count_binds (ThenBinds b1 b2) = count_binds b1 `add5` count_binds b2
404 count_binds (SingleBind b) = case count_bind b of
405 (vs,fs) -> (vs,fs,0,0,0)
406 count_binds (BindWith b sigs) = case (count_bind b, count_sigs sigs) of
407 ((vs,fs),(ts,_,ss,is)) -> (vs,fs,ts,ss,is)
409 count_bind EmptyBind = (0,0)
410 count_bind (NonRecBind b) = count_monobinds b
411 count_bind (RecBind b) = count_monobinds b
413 count_monobinds EmptyMonoBinds = (0,0)
414 count_monobinds (AndMonoBinds b1 b2) = count_monobinds b1 `add2` count_monobinds b2
415 count_monobinds (PatMonoBind (VarPatIn n) r _) = (1,0)
416 count_monobinds (PatMonoBind p r _) = (0,1)
417 count_monobinds (FunMonoBind f _ m _) = (0,1)
419 count_sigs sigs = foldr add4 (0,0,0,0) (map sig_info sigs)
421 sig_info (Sig _ _ _ _) = (1,0,0,0)
422 sig_info (ClassOpSig _ _ _ _) = (0,1,0,0)
423 sig_info (SpecSig _ _ _ _) = (0,0,1,0)
424 sig_info (InlineSig _ _) = (0,0,0,1)
425 sig_info _ = (0,0,0,0)
427 import_info (ImportDecl _ qual as spec _)
428 = add6 (1, qual_info qual, as_info as, 0,0,0) (spec_info spec)
433 spec_info Nothing = (0,0,0,1,0,0)
434 spec_info (Just (False, _)) = (0,0,0,0,1,0)
435 spec_info (Just (True, _)) = (0,0,0,0,0,1)
437 data_info (TyData _ _ _ constrs derivs _ _)
438 = (length constrs, case derivs of {Nothing -> 0; Just ds -> length ds})
439 data_info (TyNew _ _ _ constr derivs _ _)
440 = (length constr, case derivs of {Nothing -> 0; Just ds -> length ds})
442 class_info (ClassDecl _ _ _ meth_sigs def_meths _ _)
443 = case count_sigs meth_sigs of
445 (classops, addpr (count_monobinds def_meths))
447 inst_info (InstDecl _ _ inst_meths _ _ inst_sigs _ _)
448 = case count_sigs inst_sigs of
450 (addpr (count_monobinds inst_meths), ss, is)
452 is_type_decl (TySynonym _ _ _ _) = True
453 is_type_decl _ = False
454 is_data_decl (TyData _ _ _ _ _ _ _) = True
455 is_data_decl _ = False
456 is_newt_decl (TyNew _ _ _ _ _ _ _) = True
457 is_newt_decl _ = False
461 add2 (x1,x2) (y1,y2) = (x1+y1,x2+y2)
462 add3 (x1,x2,x3) (y1,y2,y3) = (x1+y1,x2+y2,x3+y3)
463 add4 (x1,x2,x3,x4) (y1,y2,y3,y4) = (x1+y1,x2+y2,x3+y3,x4+y4)
464 add5 (x1,x2,x3,x4,x5) (y1,y2,y3,y4,y5) = (x1+y1,x2+y2,x3+y3,x4+y4,x5+y5)
465 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)