2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-2000
4 \section[GHC_Main]{Main driver for Glasgow Haskell compiler}
7 module HscMain ( HscResult(..), hscMain,
8 initPersistentCompilerState ) where
10 #include "HsVersions.h"
12 import Maybe ( isJust )
13 import IO ( hPutStr, hPutStrLn, stderr )
16 import StringBuffer ( hGetStringBuffer )
17 import Parser ( parse )
18 import Lex ( PState(..), ParseResult(..) )
19 import SrcLoc ( mkSrcLoc )
21 import Rename ( renameModule, checkOldIface, closeIfaceDecls )
22 import Rules ( emptyRuleBase )
23 import PrelInfo ( wiredInThingEnv, wiredInThings )
24 import PrelNames ( knownKeyNames )
25 import PrelRules ( builtinRules )
26 import MkIface ( completeIface, mkModDetailsFromIface, mkModDetails,
28 import TcModule ( TcResults(..), typecheckModule )
29 import InstEnv ( emptyInstEnv )
30 import Desugar ( deSugar )
31 import SimplCore ( core2core )
32 import OccurAnal ( occurAnalyseBinds )
33 import CoreUtils ( coreBindsSize )
34 import CoreTidy ( tidyCorePgm )
35 import CoreToStg ( topCoreBindsToStg )
36 import StgSyn ( collectFinalStgBinders )
37 import SimplStg ( stg2stg )
38 import CodeGen ( codeGen )
39 import CodeOutput ( codeOutput )
41 import Module ( ModuleName, moduleName, mkModuleInThisPackage )
43 import ErrUtils ( dumpIfSet_dyn )
44 import UniqSupply ( mkSplitUniqSupply )
46 import Bag ( emptyBag )
48 import StgInterp ( stgToInterpSyn )
49 import HscStats ( ppSourceStats )
50 import HscTypes ( ModDetails, ModIface(..), PersistentCompilerState(..),
51 PersistentRenamerState(..), ModuleLocation(..),
53 OrigNameEnv(..), PackageRuleBase, HomeIfaceTable,
54 typeEnvClasses, typeEnvTyCons, emptyIfaceTable )
55 import InterpSyn ( UnlinkedIBind )
56 import StgInterp ( ItblEnv )
57 import FiniteMap ( FiniteMap, plusFM, emptyFM, addToFM )
58 import OccName ( OccName )
59 import Name ( Name, nameModule, emptyNameEnv, nameOccName, getName )
60 import Module ( Module, lookupModuleEnvByName )
65 %************************************************************************
67 \subsection{The main compiler pipeline}
69 %************************************************************************
73 = HscOK ModDetails -- new details (HomeSymbolTable additions)
74 (Maybe ModIface) -- new iface (if any compilation was done)
75 (Maybe String) -- generated stub_h filename (in /tmp)
76 (Maybe String) -- generated stub_c filename (in /tmp)
77 (Maybe ([UnlinkedIBind],ItblEnv)) -- interpreted code, if any
78 PersistentCompilerState -- updated PCS
80 | HscFail PersistentCompilerState -- updated PCS
81 -- no errors or warnings; the individual passes
82 -- (parse/rename/typecheck) print messages themselves
86 -> Bool -- source unchanged?
87 -> ModuleLocation -- location info
88 -> Maybe ModIface -- old interface, if available
89 -> HomeSymbolTable -- for home module ModDetails
91 -> PersistentCompilerState -- IN: persistent compiler state
94 hscMain dflags source_unchanged location maybe_old_iface hst hit pcs
96 putStrLn "checking old iface ...";
97 (pcs_ch, check_errs, (recomp_reqd, maybe_checked_iface))
98 <- checkOldIface dflags hit hst pcs (hi_file location)
99 source_unchanged maybe_old_iface;
101 return (HscFail pcs_ch)
104 let no_old_iface = not (isJust maybe_checked_iface)
105 what_next | recomp_reqd || no_old_iface = hscRecomp
106 | otherwise = hscNoRecomp
108 putStrLn "doing what_next ...";
109 what_next dflags location maybe_checked_iface
114 hscNoRecomp dflags location maybe_checked_iface hst hit pcs_ch
116 -- we definitely expect to have the old interface available
117 let old_iface = case maybe_checked_iface of
118 Just old_if -> old_if
119 Nothing -> panic "hscNoRecomp:old_iface"
120 this_mod = mi_module old_iface
123 (pcs_cl, closure_errs, cl_hs_decls)
124 <- closeIfaceDecls dflags hit hst pcs_ch old_iface ;
126 return (HscFail pcs_cl)
131 <- typecheckModule dflags this_mod pcs_cl hst hit cl_hs_decls;
132 case maybe_tc_result of {
133 Nothing -> return (HscFail pcs_cl);
134 Just tc_result -> do {
136 let pcs_tc = tc_pcs tc_result
137 env_tc = tc_env tc_result
138 local_insts = tc_insts tc_result
139 local_rules = tc_rules tc_result
141 -- create a new details from the closed, typechecked, old iface
142 let new_details = mkModDetailsFromIface env_tc local_insts local_rules
144 return (HscOK new_details
145 Nothing -- tells CM to use old iface and linkables
146 Nothing Nothing -- foreign export stuff
152 hscRecomp dflags location maybe_checked_iface hst hit pcs_ch
154 -- what target are we shooting for?
155 let toInterp = dopt_HscLang dflags == HscInterpreted
157 -- putStrLn ("toInterp = " ++ show toInterp);
159 maybe_parsed <- myParseModule dflags (hs_preprocd_file location);
160 case maybe_parsed of {
161 Nothing -> return (HscFail pcs_ch);
162 Just rdr_module -> do {
165 let this_mod = mkModuleInThisPackage (hsModuleName rdr_module)
167 show_pass dflags "Renamer";
168 (pcs_rn, maybe_rn_result)
169 <- renameModule dflags hit hst pcs_ch this_mod rdr_module;
170 case maybe_rn_result of {
171 Nothing -> return (HscFail pcs_rn);
172 Just (new_iface, rn_hs_decls) -> do {
175 show_pass dflags "Typechecker";
177 <- typecheckModule dflags this_mod pcs_rn hst hit rn_hs_decls;
178 case maybe_tc_result of {
179 Nothing -> do { hPutStrLn stderr "Typechecked failed"
180 ; return (HscFail pcs_rn) } ;
181 Just tc_result -> do {
183 let pcs_tc = tc_pcs tc_result
184 env_tc = tc_env tc_result
185 local_insts = tc_insts tc_result
187 -- DESUGAR, SIMPLIFY, TIDY-CORE
188 -- We grab the the unfoldings at this point.
189 (tidy_binds, orphan_rules, foreign_stuff)
190 <- dsThenSimplThenTidy dflags (pcs_rules pcs_tc) this_mod tc_result hst
193 (stg_binds, oa_tidy_binds, cost_centre_info, top_level_ids)
194 <- myCoreToStg dflags this_mod tidy_binds
196 -- cook up a new ModDetails now we (finally) have all the bits
197 let new_details = mkModDetails env_tc local_insts tidy_binds
198 top_level_ids orphan_rules
200 -- and possibly create a new ModIface
201 let maybe_final_iface_and_sdoc
202 = completeIface maybe_checked_iface new_iface new_details
204 = case maybe_final_iface_and_sdoc of
205 Just (fif, sdoc) -> Just fif; Nothing -> Nothing
207 -- Write the interface file
208 writeIface maybe_final_iface
210 -- do the rest of code generation/emission
211 (maybe_stub_h_filename, maybe_stub_c_filename, maybe_ibinds)
212 <- restOfCodeGeneration dflags toInterp this_mod
213 (map ideclName (hsModuleImports rdr_module))
214 cost_centre_info foreign_stuff env_tc stg_binds oa_tidy_binds
217 -- and the answer is ...
218 return (HscOK new_details maybe_final_iface
219 maybe_stub_h_filename maybe_stub_c_filename
224 myParseModule dflags src_filename
225 = do -------------------------- Parser ----------------
226 show_pass dflags "Parser"
229 buf <- hGetStringBuffer True{-expand tabs-} src_filename
231 let glaexts | dopt Opt_GlasgowExts dflags = 1#
234 case parse buf PState{ bol = 0#, atbol = 1#,
235 context = [], glasgow_exts = glaexts,
236 loc = mkSrcLoc (_PK_ src_filename) 1 } of {
238 PFailed err -> do { hPutStrLn stderr (showSDoc err);
240 POk _ rdr_module@(HsModule mod_name _ _ _ _ _ _) -> do {
242 dumpIfSet_dyn dflags Opt_D_dump_parsed "Parser" (ppr rdr_module) ;
244 dumpIfSet_dyn dflags Opt_D_source_stats "Source Statistics"
245 (ppSourceStats False rdr_module) ;
247 return (Just rdr_module)
251 restOfCodeGeneration dflags toInterp this_mod imported_module_names cost_centre_info
252 foreign_stuff env_tc stg_binds oa_tidy_binds
253 hit pit -- these last two for mapping ModNames to Modules
255 = do (ibinds,itbl_env)
256 <- stgToInterpSyn (map fst stg_binds) local_tycons local_classes
257 return (Nothing, Nothing, Just (ibinds,itbl_env))
259 = do -------------------------- Code generation -------------------------------
260 show_pass dflags "CodeGen"
262 abstractC <- codeGen dflags this_mod imported_modules
263 cost_centre_info fe_binders
264 local_tycons local_classes stg_binds
266 -------------------------- Code output -------------------------------
267 show_pass dflags "CodeOutput"
268 -- _scc_ "CodeOutput"
269 ncg_uniqs <- mkSplitUniqSupply 'n'
270 (maybe_stub_h_name, maybe_stub_c_name)
271 <- codeOutput dflags this_mod local_tycons local_classes
272 oa_tidy_binds stg_binds
273 c_code h_code abstractC ncg_uniqs
275 return (maybe_stub_h_name, maybe_stub_c_name, Nothing)
277 local_tycons = typeEnvTyCons env_tc
278 local_classes = typeEnvClasses env_tc
279 imported_modules = map mod_name_to_Module imported_module_names
280 (fe_binders,h_code,c_code) = foreign_stuff
282 mod_name_to_Module :: ModuleName -> Module
283 mod_name_to_Module nm
284 = let str_mi = case lookupModuleEnvByName hit nm of
286 Nothing -> case lookupModuleEnvByName pit nm of
290 barf nm = pprPanic "mod_name_to_Module: no hst or pst mapping for"
294 dsThenSimplThenTidy dflags rule_base this_mod tc_result hst
295 = do -------------------------- Desugaring ----------------
297 show_pass dflags "DeSugar"
298 ds_uniqs <- mkSplitUniqSupply 'd'
299 (desugared, rules, h_code, c_code, fe_binders)
300 <- deSugar dflags this_mod ds_uniqs hst tc_result
302 -------------------------- Main Core-language transformations ----------------
304 show_pass dflags "Core2Core"
305 (simplified, orphan_rules)
306 <- core2core dflags rule_base hst desugared rules
308 -- Do the final tidy-up
309 show_pass dflags "CoreTidy"
310 (tidy_binds, tidy_orphan_rules)
311 <- tidyCorePgm dflags this_mod simplified orphan_rules
313 return (tidy_binds, tidy_orphan_rules, (fe_binders,h_code,c_code))
316 myCoreToStg dflags this_mod tidy_binds
318 c2s_uniqs <- mkSplitUniqSupply 'c'
319 st_uniqs <- mkSplitUniqSupply 'g'
320 let occ_anal_tidy_binds = occurAnalyseBinds tidy_binds
322 () <- coreBindsSize occ_anal_tidy_binds `seq` return ()
323 -- TEMP: the above call zaps some space usage allocated by the
324 -- simplifier, which for reasons I don't understand, persists
325 -- thoroughout code generation
327 show_pass dflags "Core2Stg"
329 let stg_binds = topCoreBindsToStg c2s_uniqs occ_anal_tidy_binds
331 show_pass dflags "Stg2Stg"
333 (stg_binds2, cost_centre_info) <- stg2stg dflags this_mod st_uniqs stg_binds
334 let final_ids = collectFinalStgBinders (map fst stg_binds2)
336 return (stg_binds2, occ_anal_tidy_binds, cost_centre_info, final_ids)
339 show_pass dflags what
340 = if dopt Opt_D_show_passes dflags
341 then hPutStr stderr ("*** "++what++":\n")
346 %************************************************************************
348 \subsection{Initial persistent state}
350 %************************************************************************
353 initPersistentCompilerState :: IO PersistentCompilerState
354 initPersistentCompilerState
355 = do prs <- initPersistentRenamerState
357 PCS { pcs_PIT = emptyIfaceTable,
358 pcs_PTE = wiredInThingEnv,
359 pcs_insts = emptyInstEnv,
360 pcs_rules = emptyRuleBase,
365 initPersistentRenamerState :: IO PersistentRenamerState
366 = do ns <- mkSplitUniqSupply 'r'
368 PRS { prsOrig = Orig { origNames = initOrigNames,
369 origIParam = emptyFM },
370 prsDecls = emptyNameEnv,
377 initOrigNames :: FiniteMap (ModuleName,OccName) Name
379 = grab knownKeyNames `plusFM` grab (map getName wiredInThings)
381 grab names = foldl add emptyFM names
383 = addToFM env (moduleName (nameModule name), nameOccName name) name
386 initRules :: PackageRuleBase
387 initRules = emptyRuleBase
389 foldl add emptyVarEnv builtinRules
392 = extendRuleBase env name rule