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(..)))
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
26 , DsMatchContext, DsWarnFlavour
29 import SimplCore ( core2core )
30 import CoreToStg ( topCoreBindsToStg )
31 import StgSyn ( collectFinalStgBinders )
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 )
44 import Maybes ( maybeToBool, MaybeErr(..) )
45 import Specialise ( SpecialiseData(..) )
46 import StgSyn ( pprPlainStgBinding, GenStgBinding )
47 import TcInstUtil ( InstInfo )
48 import TyCon ( isDataTyCon )
49 import UniqSupply ( mkSplitUniqSupply )
51 import PprAbsC ( dumpRealC, writeRealC )
52 import PprCore ( pprCoreBinding )
53 import Outputable ( PprStyle(..), Outputable(..) )
56 import Id ( GenId ) -- instances
57 import Name ( Name ) -- instances
58 import PprType ( GenType, GenTyVar ) -- instances
59 import TyVar ( GenTyVar ) -- instances
60 import Unique ( Unique ) -- instances
66 hGetContents stdin >>= \ input_pgm ->
68 cmd_line_info = classifyOpts
70 doIt cmd_line_info input_pgm
74 doIt :: ([CoreToDo], [StgToDo]) -> String -> IO ()
76 doIt (core_cmds, stg_cmds) input_pgm
77 = doDump opt_Verbose "Glasgow Haskell Compiler, version 2.03, for Haskell 1.4" "" >>
82 rdModule >>= \ (mod_name, rdr_module) ->
84 doDump opt_D_dump_rdr "Reader:"
85 (pp_show (ppr pprStyle rdr_module)) >>
87 doDump opt_D_source_stats "\nSource Statistics:"
88 (pp_show (ppSourceStats rdr_module)) >>
90 -- UniqueSupplies for later use (these are the only lower case uniques)
92 mkSplitUniqSupply 'r' >>= \ rn_uniqs -> -- renamer
94 mkSplitUniqSupply 'a' >>= \ tc_uniqs -> -- typechecker
96 mkSplitUniqSupply 'd' >>= \ ds_uniqs -> -- desugarer
98 mkSplitUniqSupply 's' >>= \ sm_uniqs -> -- core-to-core simplifier
100 mkSplitUniqSupply 'c' >>= \ c2s_uniqs -> -- core-to-stg
102 mkSplitUniqSupply 'g' >>= \ st_uniqs -> -- stg-to-stg passes
104 mkSplitUniqSupply 'f' >>= \ fl_uniqs -> -- absC flattener
106 mkSplitUniqSupply 'n' >>= \ ncg_uniqs -> -- native-code generator
109 show_pass "Renamer" >>
112 renameModule rn_uniqs rdr_module >>=
113 \ (maybe_rn_stuff, rn_errs_bag, rn_warns_bag) ->
115 checkErrors rn_errs_bag rn_warns_bag >>
116 case maybe_rn_stuff of {
117 Nothing -> -- Hurrah! Renamer reckons that there's no need to
119 hPutStr stderr "No recompilation required!\n" >>
122 -- Oh well, we've got to recompile for real
123 Just (rn_mod, iface_file_stuff, rn_name_supply, imported_modules) ->
127 doDump opt_D_dump_rn "Renamer:"
128 (pp_show (ppr pprStyle rn_mod)) >>
130 -- Safely past renaming: we can start the interface file:
131 -- (the iface file is produced incrementally, as we have
132 -- the information that we need...; we use "iface<blah>")
133 -- "endIface" finishes the job.
134 startIface mod_name >>= \ if_handle ->
135 ifaceMain if_handle iface_file_stuff >>
138 -- ******* TYPECHECKER
139 show_pass "TypeCheck" >>
141 case (case (typecheckModule tc_uniqs {-idinfo_fm-} rn_name_supply rn_mod) of
142 Succeeded (stuff, warns)
143 -> (emptyBag, warns, stuff)
145 -> (errs, warns, error "tc_results"))
147 of { (tc_errs_bag, tc_warns_bag, tc_results) ->
149 checkErrors tc_errs_bag tc_warns_bag >>
152 of { (typechecked_quint@(recsel_binds, class_binds, inst_binds, val_binds, const_binds),
153 local_tycons, local_classes, inst_info, pragma_tycon_specs,
156 doDump opt_D_dump_tc "Typechecked:"
158 ppr pprStyle recsel_binds,
159 ppr pprStyle class_binds,
160 ppr pprStyle inst_binds,
161 ppr pprStyle const_binds,
162 ppr pprStyle val_binds])) >>
164 doDump opt_D_dump_deriv "Derived instances:"
165 (pp_show (ddump_deriv pprStyle)) >>
168 show_pass "DeSugar " >>
171 (desugared,ds_warnings)
172 = deSugar ds_uniqs mod_name typechecked_quint
174 (if isEmptyBag ds_warnings then
177 hPutStr stderr (pp_show (pprDsWarnings pprErrorsStyle ds_warnings))
178 >> hPutStr stderr "\n"
181 doDump opt_D_dump_ds "Desugared:" (pp_show (vcat
182 (map (pprCoreBinding pprStyle) desugared)))
185 -- ******* CORE-TO-CORE SIMPLIFICATION (NB: I/O op)
186 show_pass "Core2Core" >>
189 local_data_tycons = filter isDataTyCon local_tycons
191 core2core core_cmds mod_name pprStyle
192 sm_uniqs local_data_tycons pragma_tycon_specs desugared
196 SpecData _ _ _ gen_data_tycons all_tycon_specs _ _ _) ->
198 doDump opt_D_dump_simpl "Simplified:" (pp_show (vcat
199 (map (pprCoreBinding pprStyle) simplified)))
202 -- ******* STG-TO-STG SIMPLIFICATION
203 show_pass "Core2Stg" >>
206 stg_binds = topCoreBindsToStg c2s_uniqs simplified
209 show_pass "Stg2Stg" >>
211 stg2stg stg_cmds mod_name pprStyle st_uniqs stg_binds
214 \ (stg_binds2, cost_centre_info) ->
216 doDump opt_D_dump_stg "STG syntax:"
217 (pp_show (vcat (map (pprPlainStgBinding pprStyle) stg_binds2)))
220 -- Dump instance decls and type signatures into the interface file
222 final_ids = collectFinalStgBinders stg_binds2
225 ifaceDecls if_handle local_tycons local_classes inst_info final_ids simplified >>
226 endIface if_handle >>
227 -- We are definitely done w/ interface-file stuff at this point:
228 -- (See comments near call to "startIface".)
231 -- ******* "ABSTRACT", THEN "FLAT", THEN *REAL* C!
232 show_pass "CodeGen" >>
235 abstractC = codeGen mod_name -- module name for CC labelling
237 imported_modules -- import names for CC registering
238 gen_data_tycons -- type constructors generated locally
239 all_tycon_specs -- tycon specialisations
242 flat_abstractC = flattenAbsC fl_uniqs abstractC
244 doDump opt_D_dump_absC "Abstract C:"
245 (dumpRealC abstractC) >>
247 doDump opt_D_dump_flatC "Flat Abstract C:"
248 (dumpRealC flat_abstractC) >>
251 -- You can have C (c_output) or assembly-language (ncg_output),
252 -- but not both. [Allowing for both gives a space leak on
253 -- flat_abstractC. WDP 94/10]
255 (flat_absC_c, flat_absC_ncg) =
256 case (maybeToBool opt_ProduceC || opt_D_dump_realC,
257 maybeToBool opt_ProduceS || opt_D_dump_asm) of
258 (True, False) -> (flat_abstractC, absCNop)
259 (False, True) -> (absCNop, flat_abstractC)
260 (False, False) -> (absCNop, absCNop)
261 (True, True) -> error "ERROR: Can't do both .hc and .s at the same time"
263 c_output_d = dumpRealC flat_absC_c
264 c_output_w = (\ f -> writeRealC f flat_absC_c)
266 #if OMIT_NATIVE_CODEGEN
267 ncg_output_d = error "*** GHC not built with a native-code generator ***"
268 ncg_output_w = ncg_output_d
270 ncg_output_d = dumpRealAsm flat_absC_ncg ncg_uniqs
271 ncg_output_w = (\ f -> writeRealAsm f flat_absC_ncg ncg_uniqs)
275 doDump opt_D_dump_asm "" ncg_output_d >>
276 doOutput opt_ProduceS ncg_output_w >>
278 doDump opt_D_dump_realC "" c_output_d >>
279 doOutput opt_ProduceC c_output_w >>
284 -------------------------------------------------------------
285 -- ****** printing styles and column width:
288 -------------------------------------------------------------
289 -- ****** help functions:
292 = if opt_D_show_passes
293 then \ what -> hPutStr stderr ("*** "++what++":\n")
294 else \ what -> return ()
296 doOutput switch io_action
300 openFile fname WriteMode >>= \ handle ->
304 doDump switch hdr string
306 then hPutStr stderr ("\n\n--" ++ (take 80 $ repeat '=')) >>
307 hPutStr stderr ('\n': hdr) >>
308 hPutStr stderr ('\n': string) >>
313 pprCols = (80 :: Int) -- could make configurable
315 (pprStyle, pprErrorsStyle)
316 | opt_PprStyle_All = (PprShowAll, PprShowAll)
317 | opt_PprStyle_Debug = (PprDebug, PprDebug)
318 | opt_PprStyle_User = (PprQuote, PprQuote)
319 | otherwise = (PprDebug, PprQuote)
321 pp_show p = show p -- ToDo: use pprCols
323 checkErrors errs_bag warns_bag
324 | not (isEmptyBag errs_bag)
325 = hPutStr stderr (pp_show (pprBagOfErrors pprErrorsStyle errs_bag))
326 >> hPutStr stderr "\n" >>
327 hPutStr stderr (pp_show (pprBagOfErrors pprErrorsStyle warns_bag))
328 >> hPutStr stderr "\n" >>
331 | not (isEmptyBag warns_bag)
332 = hPutStr stderr (pp_show (pprBagOfErrors pprErrorsStyle warns_bag)) >>
335 | otherwise = return ()
338 ppSourceStats (HsModule name version exports imports fixities decls src_loc)
340 [("ExportAll ", export_all), -- 1 if no export list
341 ("ExportDecls ", export_ds),
342 ("ExportModules ", export_ms),
343 ("Imports ", import_no),
344 (" ImpQual ", import_qual),
345 (" ImpAs ", import_as),
346 (" ImpAll ", import_all),
347 (" ImpPartial ", import_partial),
348 (" ImpHiding ", import_hiding),
349 ("FixityDecls ", fixity_ds),
350 ("DefaultDecls ", default_ds),
351 ("TypeDecls ", type_ds),
352 ("DataDecls ", data_ds),
353 ("NewTypeDecls ", newt_ds),
354 ("DataConstrs ", data_constrs),
355 ("DataDerivings ", data_derivs),
356 ("ClassDecls ", class_ds),
357 ("ClassMethods ", class_method_ds),
358 ("DefaultMethods ", default_method_ds),
359 ("InstDecls ", inst_ds),
360 ("InstMethods ", inst_method_ds),
361 ("TypeSigs ", bind_tys),
362 ("ValBinds ", val_bind_ds),
363 ("FunBinds ", fn_bind_ds),
364 ("InlineMeths ", method_inlines),
365 ("InlineBinds ", bind_inlines),
366 -- ("SpecialisedData ", data_specs),
367 -- ("SpecialisedInsts ", inst_specs),
368 ("SpecialisedMeths ", method_specs),
369 ("SpecialisedBinds ", bind_specs)
372 pp_val (str, 0) = empty
373 pp_val (str, n) = hcat [text str, int n]
375 fixity_ds = length fixities
376 type_decls = [d | TyD d@(TySynonym _ _ _ _) <- decls]
377 data_decls = [d | TyD d@(TyData DataType _ _ _ _ _ _ _) <- decls]
378 newt_decls = [d | TyD d@(TyData NewType _ _ _ _ _ _ _) <- decls]
379 type_ds = length type_decls
380 data_ds = length data_decls
381 newt_ds = length newt_decls
382 class_decls = [d | ClD d <- decls]
383 class_ds = length class_decls
384 inst_decls = [d | InstD d <- decls]
385 inst_ds = length inst_decls
386 default_ds = length [() | DefD _ <- decls]
387 val_decls = [d | ValD d <- decls]
389 real_exports = case exports of { Nothing -> []; Just es -> es }
390 n_exports = length real_exports
391 export_ms = length [() | IEModuleContents _ <- real_exports]
392 export_ds = n_exports - export_ms
393 export_all = case exports of { Nothing -> 1; other -> 0 }
395 (val_bind_ds, fn_bind_ds, bind_tys, bind_specs, bind_inlines)
396 = count_binds (foldr ThenBinds EmptyBinds val_decls)
398 (import_no, import_qual, import_as, import_all, import_partial, import_hiding)
399 = foldr add6 (0,0,0,0,0,0) (map import_info imports)
400 (data_constrs, data_derivs)
401 = foldr add2 (0,0) (map data_info (newt_decls ++ data_decls))
402 (class_method_ds, default_method_ds)
403 = foldr add2 (0,0) (map class_info class_decls)
404 (inst_method_ds, method_specs, method_inlines)
405 = foldr add3 (0,0,0) (map inst_info inst_decls)
408 count_binds EmptyBinds = (0,0,0,0,0)
409 count_binds (ThenBinds b1 b2) = count_binds b1 `add5` count_binds b2
410 count_binds (MonoBind b sigs _) = case (count_monobinds b, count_sigs sigs) of
411 ((vs,fs),(ts,_,ss,is)) -> (vs,fs,ts,ss,is)
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})
440 class_info (ClassDecl _ _ _ meth_sigs def_meths _ _)
441 = case count_sigs meth_sigs of
443 (classops, addpr (count_monobinds def_meths))
445 inst_info (InstDecl _ inst_meths inst_sigs _ _)
446 = case count_sigs inst_sigs of
448 (addpr (count_monobinds inst_meths), ss, is)
452 add2 (x1,x2) (y1,y2) = (x1+y1,x2+y2)
453 add3 (x1,x2,x3) (y1,y2,y3) = (x1+y1,x2+y2,x3+y3)
454 add4 (x1,x2,x3,x4) (y1,y2,y3,y4) = (x1+y1,x2+y2,x3+y3,x4+y4)
455 add5 (x1,x2,x3,x4,x5) (y1,y2,y3,y4,y5) = (x1+y1,x2+y2,x3+y3,x4+y4,x5+y5)
456 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)