2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-2000
4 \section[GHC_Main]{Main driver for Glasgow Haskell compiler}
7 module HscMain ( hscMain ) where
9 #include "HsVersions.h"
11 import Maybe ( isJust )
13 import IO ( hPutStr, hPutStrLn, hClose, stderr,
14 openFile, IOMode(..) )
17 import RdrHsSyn ( RdrNameHsModule )
18 import FastString ( unpackFS )
19 import StringBuffer ( hGetStringBuffer )
20 import Parser ( parse )
21 import Lex ( PState(..), ParseResult(..) )
22 import SrcLoc ( mkSrcLoc )
24 import Rename ( renameModule, checkOldIface, closeIfaceDecls )
26 import Rules ( emptyRuleBase )
27 import PrelInfo ( wiredInThings )
28 import PrelNames ( knownKeyNames )
29 import PrelRules ( builtinRules )
30 import MkIface ( completeIface, mkModDetailsFromIface, mkModDetails,
32 import TcModule ( TcResults(..), typecheckModule )
33 import TcEnv ( tcEnvTyCons, tcEnvClasses )
34 import InstEnv ( emptyInstEnv )
35 import Desugar ( deSugar )
36 import SimplCore ( core2core )
37 import OccurAnal ( occurAnalyseBinds )
38 import CoreUtils ( coreBindsSize )
39 import CoreTidy ( tidyCorePgm )
40 import CoreToStg ( topCoreBindsToStg )
41 import StgSyn ( collectFinalStgBinders )
42 import SimplStg ( stg2stg )
43 import CodeGen ( codeGen )
44 import CodeOutput ( codeOutput )
46 import Module ( ModuleName, moduleNameUserString,
47 moduleUserString, moduleName, emptyModuleEnv,
50 import ErrUtils ( ghcExit, doIfSet, dumpIfSet_dyn )
51 import UniqSupply ( mkSplitUniqSupply )
53 import Bag ( emptyBag )
55 import Char ( isSpace )
56 import StgInterp ( stgToInterpSyn )
57 import HscStats ( ppSourceStats )
58 import HscTypes ( ModDetails, ModIface(..), PersistentCompilerState(..),
59 PersistentRenamerState(..), WhatsImported(..),
60 HomeSymbolTable, PackageSymbolTable, ImportVersion,
61 GenAvailInfo(..), RdrAvailInfo, OrigNameEnv(..),
62 PackageRuleBase, HomeIfaceTable, PackageIfaceTable,
63 extendTypeEnv, groupTyThings, TypeEnv, TyThing,
64 typeEnvClasses, typeEnvTyCons )
65 import RnMonad ( ExportItem, ParsedIface(..) )
66 import CmSummarise ( ModSummary(..), name_of_summary, ms_get_imports,
68 import Finder ( Finder )
69 import InterpSyn ( UnlinkedIBind )
70 import StgInterp ( ItblEnv )
71 import FiniteMap ( FiniteMap, plusFM, emptyFM, addToFM )
72 import OccName ( OccName, pprOccName )
73 import Name ( Name, nameModule, emptyNameEnv, nameOccName,
74 getName, extendNameEnv_C, nameEnvElts )
75 import VarEnv ( emptyVarEnv )
76 import Module ( Module, mkModuleName, lookupModuleEnvByName )
81 %************************************************************************
83 \subsection{The main compiler pipeline}
85 %************************************************************************
89 = HscOK ModDetails -- new details (HomeSymbolTable additions)
90 (Maybe ModIface) -- new iface (if any compilation was done)
91 (Maybe String) -- generated stub_h filename (in /tmp)
92 (Maybe String) -- generated stub_c filename (in /tmp)
93 (Maybe ([UnlinkedIBind],ItblEnv)) -- interpreted code, if any
94 PersistentCompilerState -- updated PCS
96 | HscFail PersistentCompilerState -- updated PCS
97 -- no errors or warnings; the individual passes
98 -- (parse/rename/typecheck) print messages themselves
103 -> ModSummary -- summary, including source filename
104 -> Maybe ModIface -- old interface, if available
105 -> HomeSymbolTable -- for home module ModDetails
108 -> PersistentCompilerState -- IN: persistent compiler state
111 hscMain dflags finder summary maybe_old_iface hst hit pit pcs
113 -- ????? source_unchanged :: Bool -- extracted from summary?
114 let source_unchanged = trace "WARNING: source_unchanged?!" False
116 (pcs_ch, check_errs, (recomp_reqd, maybe_checked_iface))
117 <- checkOldIface dflags finder hit hst pcs (ms_mod summary)
118 source_unchanged maybe_old_iface;
120 return (HscFail pcs_ch)
123 let no_old_iface = not (isJust maybe_checked_iface)
124 what_next | recomp_reqd || no_old_iface = hscRecomp
125 | otherwise = hscNoRecomp
127 what_next dflags finder summary maybe_checked_iface
132 hscNoRecomp dflags finder summary maybe_checked_iface hst hit pit pcs_ch
134 -- we definitely expect to have the old interface available
135 let old_iface = case maybe_checked_iface of
136 Just old_if -> old_if
137 Nothing -> panic "hscNoRecomp:old_iface"
140 (pcs_cl, closure_errs, cl_hs_decls)
141 <- closeIfaceDecls dflags finder hit hst pcs_ch old_iface ;
143 return (HscFail pcs_cl)
148 <- typecheckModule dflags (ms_mod summary) pcs_cl hst hit cl_hs_decls;
149 case maybe_tc_result of {
150 Nothing -> return (HscFail pcs_cl);
151 Just tc_result -> do {
153 let pcs_tc = tc_pcs tc_result
154 env_tc = tc_env tc_result
155 binds_tc = tc_binds tc_result
156 local_insts = tc_insts tc_result
157 local_rules = tc_rules tc_result
159 -- create a new details from the closed, typechecked, old iface
160 let new_details = mkModDetailsFromIface env_tc local_insts local_rules
162 return (HscOK new_details
163 Nothing -- tells CM to use old iface and linkables
164 Nothing Nothing -- foreign export stuff
170 hscRecomp dflags finder summary maybe_checked_iface hst hit pit pcs_ch
172 -- what target are we shooting for?
173 let toInterp = dopt_HscLang dflags == HscInterpreted
174 this_mod = ms_mod summary
177 maybe_parsed <- myParseModule dflags summary;
178 case maybe_parsed of {
179 Nothing -> return (HscFail pcs_ch);
180 Just rdr_module -> do {
183 show_pass dflags "Renamer";
184 (pcs_rn, maybe_rn_result)
185 <- renameModule dflags finder hit hst pcs_ch this_mod rdr_module;
186 case maybe_rn_result of {
187 Nothing -> return (HscFail pcs_rn);
188 Just (new_iface, rn_hs_decls) -> do {
191 show_pass dflags "Typechecker";
193 <- typecheckModule dflags this_mod pcs_rn hst hit rn_hs_decls;
194 case maybe_tc_result of {
195 Nothing -> return (HscFail pcs_rn);
196 Just tc_result -> do {
198 let pcs_tc = tc_pcs tc_result
199 env_tc = tc_env tc_result
200 binds_tc = tc_binds tc_result
201 local_insts = tc_insts tc_result
203 -- DESUGAR, SIMPLIFY, TIDY-CORE
204 -- We grab the the unfoldings at this point.
205 (tidy_binds, orphan_rules, foreign_stuff)
206 <- dsThenSimplThenTidy dflags (pcs_rules pcs_tc) this_mod tc_result hst
209 (stg_binds, oa_tidy_binds, cost_centre_info, top_level_ids)
210 <- myCoreToStg dflags this_mod tidy_binds
212 -- cook up a new ModDetails now we (finally) have all the bits
213 let new_details = mkModDetails env_tc local_insts tidy_binds
214 top_level_ids orphan_rules
216 -- and possibly create a new ModIface
217 let maybe_final_iface_and_sdoc
218 = completeIface maybe_checked_iface new_iface new_details
220 = case maybe_final_iface_and_sdoc of
221 Just (fif, sdoc) -> Just fif; Nothing -> Nothing
223 -- SimonM does this, higher up
224 -- -- Write the interface file
225 -- writeIface finder maybe_final_iface
227 -- do the rest of code generation/emission
228 (maybe_stub_h_filename, maybe_stub_c_filename, maybe_ibinds)
229 <- restOfCodeGeneration dflags toInterp summary
230 cost_centre_info foreign_stuff env_tc stg_binds oa_tidy_binds
233 -- and the answer is ...
234 return (HscOK new_details maybe_final_iface
235 maybe_stub_h_filename maybe_stub_c_filename
240 myParseModule dflags summary
241 = do -------------------------- Parser ----------------
242 show_pass dflags "Parser"
245 let src_filename -- name of the preprocessed source file
246 = case ms_ppsource summary of
247 Just (filename, fingerprint) -> filename
249 "myParseModule:summary is not of a source module"
252 buf <- hGetStringBuffer True{-expand tabs-} src_filename
254 let glaexts | dopt Opt_GlasgowExts dflags = 1#
257 case parse buf PState{ bol = 0#, atbol = 1#,
258 context = [], glasgow_exts = glaexts,
259 loc = mkSrcLoc (_PK_ src_filename) 1 } of {
261 PFailed err -> do { hPutStrLn stderr (showSDoc err);
263 POk _ rdr_module@(HsModule mod_name _ _ _ _ _ _) -> do {
265 dumpIfSet_dyn dflags Opt_D_dump_parsed "Parser" (ppr rdr_module) ;
267 dumpIfSet_dyn dflags Opt_D_source_stats "Source Statistics"
268 (ppSourceStats False rdr_module) ;
270 return (Just rdr_module)
274 restOfCodeGeneration dflags toInterp summary cost_centre_info
275 foreign_stuff env_tc stg_binds oa_tidy_binds
276 hit pit -- these last two for mapping ModNames to Modules
278 = do (ibinds,itbl_env)
279 <- stgToInterpSyn (map fst stg_binds) local_tycons local_classes
280 return (Nothing, Nothing, Just (ibinds,itbl_env))
282 = do -------------------------- Code generation -------------------------------
283 show_pass dflags "CodeGen"
285 abstractC <- codeGen dflags this_mod imported_modules
286 cost_centre_info fe_binders
287 local_tycons local_classes stg_binds
289 -------------------------- Code output -------------------------------
290 show_pass dflags "CodeOutput"
291 -- _scc_ "CodeOutput"
292 ncg_uniqs <- mkSplitUniqSupply 'n'
293 (maybe_stub_h_name, maybe_stub_c_name)
294 <- codeOutput dflags this_mod local_tycons local_classes
295 oa_tidy_binds stg_binds
296 c_code h_code abstractC ncg_uniqs
298 return (maybe_stub_h_name, maybe_stub_c_name, Nothing)
300 local_tycons = typeEnvTyCons env_tc
301 local_classes = typeEnvClasses env_tc
302 this_mod = ms_mod summary
303 imported_modules = map (mod_name_to_Module.mimp_name)
304 (ms_get_imports summary)
305 (fe_binders,h_code,c_code) = foreign_stuff
307 mod_name_to_Module :: ModuleName -> Module
308 mod_name_to_Module nm
309 = let str_mi = case lookupModuleEnvByName hit nm of
311 Nothing -> case lookupModuleEnvByName pit nm of
315 barf nm = pprPanic "mod_name_to_Module: no hst or pst mapping for"
319 dsThenSimplThenTidy dflags rule_base this_mod tc_result hst
320 = do -------------------------- Desugaring ----------------
322 show_pass dflags "DeSugar"
323 ds_uniqs <- mkSplitUniqSupply 'd'
324 (desugared, rules, h_code, c_code, fe_binders)
325 <- deSugar dflags this_mod ds_uniqs hst tc_result
327 -------------------------- Main Core-language transformations ----------------
329 show_pass dflags "Core2Core"
330 (simplified, orphan_rules)
331 <- core2core dflags rule_base hst desugared rules
333 -- Do the final tidy-up
334 show_pass dflags "CoreTidy"
335 (tidy_binds, tidy_orphan_rules)
336 <- tidyCorePgm dflags this_mod simplified orphan_rules
338 return (tidy_binds, tidy_orphan_rules, (fe_binders,h_code,c_code))
341 myCoreToStg dflags this_mod tidy_binds
343 c2s_uniqs <- mkSplitUniqSupply 'c'
344 st_uniqs <- mkSplitUniqSupply 'g'
345 let occ_anal_tidy_binds = occurAnalyseBinds tidy_binds
347 () <- coreBindsSize occ_anal_tidy_binds `seq` return ()
348 -- TEMP: the above call zaps some space usage allocated by the
349 -- simplifier, which for reasons I don't understand, persists
350 -- thoroughout code generation
352 show_pass dflags "Core2Stg"
354 let stg_binds = topCoreBindsToStg c2s_uniqs occ_anal_tidy_binds
356 show_pass dflags "Stg2Stg"
358 (stg_binds2, cost_centre_info) <- stg2stg dflags this_mod st_uniqs stg_binds
359 let final_ids = collectFinalStgBinders (map fst stg_binds2)
361 return (stg_binds2, occ_anal_tidy_binds, cost_centre_info, final_ids)
364 show_pass dflags what
365 = if dopt Opt_D_show_passes dflags
366 then hPutStr stderr ("*** "++what++":\n")
371 %************************************************************************
373 \subsection{Initial persistent state}
375 %************************************************************************
378 initPersistentCompilerState :: IO PersistentCompilerState
379 initPersistentCompilerState
380 = do prs <- initPersistentRenamerState
382 PCS { pcs_PST = initPackageDetails,
383 pcs_insts = emptyInstEnv,
384 pcs_rules = emptyRuleBase,
389 initPackageDetails :: PackageSymbolTable
390 initPackageDetails = extendTypeEnv emptyModuleEnv (groupTyThings wiredInThings)
392 --initPackageDetails = panic "initPackageDetails"
394 initPersistentRenamerState :: IO PersistentRenamerState
395 = do ns <- mkSplitUniqSupply 'r'
397 PRS { prsOrig = Orig { origNames = initOrigNames,
398 origIParam = emptyFM },
399 prsDecls = emptyNameEnv,
406 initOrigNames :: FiniteMap (ModuleName,OccName) Name
408 = grab knownKeyNames `plusFM` grab (map getName wiredInThings)
410 grab names = foldl add emptyFM names
412 = addToFM env (moduleName (nameModule name), nameOccName name) name
415 initRules :: PackageRuleBase
416 initRules = emptyRuleBase
418 foldl add emptyVarEnv builtinRules
421 = extendRuleBase env name rule