+hscMain ghci_mode dflags source_unchanged location maybe_old_iface hst hit pcs
+ = do {
+ showPass dflags ("Checking old interface for hs = "
+ ++ show (ml_hs_file location)
+ ++ ", hspp = " ++ show (ml_hspp_file location));
+
+ (pcs_ch, errs_found, (recomp_reqd, maybe_checked_iface))
+ <- checkOldIface ghci_mode dflags hit hst pcs
+ (unJust "hscMain" (ml_hi_file location))
+ source_unchanged maybe_old_iface;
+
+ if errs_found then
+ return (HscFail pcs_ch)
+ else do {
+
+ let no_old_iface = not (isJust maybe_checked_iface)
+ what_next | recomp_reqd || no_old_iface = hscRecomp
+ | otherwise = hscNoRecomp
+ ;
+ what_next ghci_mode dflags location maybe_checked_iface
+ hst hit pcs_ch
+ }}
+
+
+-- we definitely expect to have the old interface available
+hscNoRecomp ghci_mode dflags location (Just old_iface) hst hit pcs_ch
+ | ghci_mode == OneShot
+ = do {
+ hPutStrLn stderr "compilation IS NOT required";
+ let { bomb = panic "hscNoRecomp:OneShot" };
+ return (HscNoRecomp pcs_ch bomb bomb)
+ }
+ | otherwise
+ = do {
+ hPutStr stderr "compilation IS NOT required";
+ when (verbosity dflags /= 1) $ hPutStrLn stderr "";
+
+ -- CLOSURE
+ (pcs_cl, closure_errs, cl_hs_decls)
+ <- closeIfaceDecls dflags hit hst pcs_ch old_iface ;
+ if closure_errs then
+ return (HscFail pcs_cl)
+ else do {
+
+ -- TYPECHECK
+ maybe_tc_result <- typecheckModule dflags pcs_cl hst
+ old_iface alwaysQualify cl_hs_decls;
+ case maybe_tc_result of {
+ Nothing -> return (HscFail pcs_cl);
+ Just (pcs_tc, tc_result) -> do {
+
+ let env_tc = tc_env tc_result
+ local_rules = tc_rules tc_result
+ ;
+ -- create a new details from the closed, typechecked, old iface
+ let new_details = mkModDetailsFromIface env_tc local_rules
+ ;
+ return (HscNoRecomp pcs_tc new_details old_iface)
+ }}}}
+
+
+hscRecomp ghci_mode dflags location maybe_checked_iface hst hit pcs_ch
+ = do {
+ ; when (verbosity dflags >= 1) $
+ hPutStr stderr "compilation IS required";
+ -- mode -v1 tries to keep everything on one line
+ when (verbosity dflags > 1) $
+ hPutStrLn stderr "";
+
+ -- what target are we shooting for?
+ ; let toInterp = dopt_HscLang dflags == HscInterpreted
+
+ -------------------
+ -- PARSE
+ -------------------
+ ; maybe_parsed <- myParseModule dflags
+ (unJust "hscRecomp:hspp" (ml_hspp_file location))
+ ; case maybe_parsed of {
+ Nothing -> return (HscFail pcs_ch);
+ Just rdr_module -> do {
+ ; let this_mod = mkHomeModule (hsModuleName rdr_module)
+
+ -------------------
+ -- RENAME
+ -------------------
+ ; (pcs_rn, maybe_rn_result)
+ <- renameModule dflags hit hst pcs_ch this_mod rdr_module
+ ; case maybe_rn_result of {
+ Nothing -> return (HscFail pcs_rn);
+ Just (print_unqualified, (is_exported, new_iface, rn_hs_decls)) -> do {
+
+ -------------------
+ -- TYPECHECK
+ -------------------
+ ; maybe_tc_result <- typecheckModule dflags pcs_rn hst new_iface
+ print_unqualified rn_hs_decls
+ ; case maybe_tc_result of {
+ Nothing -> return (HscFail pcs_rn);
+ Just (pcs_tc, tc_result) -> do {
+
+ ; let env_tc = tc_env tc_result
+
+ -------------------
+ -- DESUGAR
+ -------------------
+ ; (ds_binds, ds_rules, foreign_stuff)
+ <- deSugar dflags pcs_tc hst this_mod print_unqualified tc_result
+
+ -------------------
+ -- SIMPLIFY, TIDY-CORE
+ -------------------
+ -- We grab the the unfoldings at this point.
+ ; (pcs_simpl, tidy_binds, orphan_rules)
+ <- simplThenTidy dflags pcs_tc hst this_mod is_exported ds_binds ds_rules
+
+ -------------------
+ -- BUILD THE NEW ModDetails AND ModIface
+ -------------------
+ ; let new_details = mkModDetails env_tc tidy_binds orphan_rules
+ ; final_iface <- mkFinalIface ghci_mode dflags location
+ maybe_checked_iface new_iface new_details
+
+ -------------------
+ -- CONVERT TO STG
+ -------------------
+ ; (stg_binds, cost_centre_info)
+ <- myCoreToStg dflags this_mod tidy_binds
+
+ -------------------
+ -- COMPLETE CODE GENERATION
+ -------------------
+ ; (maybe_stub_h_filename, maybe_stub_c_filename, maybe_ibinds)
+ <- restOfCodeGeneration dflags toInterp this_mod
+ (map ideclName (hsModuleImports rdr_module))
+ cost_centre_info foreign_stuff env_tc stg_binds tidy_binds
+ hit (pcs_PIT pcs_simpl)
+
+ -- and the answer is ...
+ ; return (HscRecomp pcs_simpl new_details final_iface
+ maybe_stub_h_filename maybe_stub_c_filename
+ maybe_ibinds)
+ }}}}}}}
+
+
+
+mkFinalIface ghci_mode dflags location maybe_old_iface new_iface new_details
+ = case completeIface maybe_old_iface new_iface new_details of
+ (new_iface, Nothing) -- no change in the interfacfe
+ -> do when (dopt Opt_D_dump_hi_diffs dflags)
+ (printDump (text "INTERFACE UNCHANGED"))
+ dumpIfSet_dyn dflags Opt_D_dump_hi
+ "UNCHANGED FINAL INTERFACE" (pprIface new_iface)
+ return new_iface
+ (new_iface, Just sdoc_diffs)
+ -> do dumpIfSet_dyn dflags Opt_D_dump_hi_diffs "INTERFACE HAS CHANGED"
+ sdoc_diffs
+ dumpIfSet_dyn dflags Opt_D_dump_hi "NEW FINAL INTERFACE"
+ (pprIface new_iface)
+ -- Write the interface file
+ when (ghci_mode /= Interactive)
+ (writeIface (unJust "hscRecomp:hi" (ml_hi_file location))
+ new_iface)
+ return new_iface
+
+
+myParseModule dflags src_filename
+ = do -------------------------- Parser ----------------
+ showPass dflags "Parser"
+ -- _scc_ "Parser"
+
+ buf <- hGetStringBuffer True{-expand tabs-} src_filename
+
+ let glaexts | dopt Opt_GlasgowExts dflags = 1#
+ | otherwise = 0#
+
+ case parse buf PState{ bol = 0#, atbol = 1#,
+ context = [], glasgow_exts = glaexts,
+ loc = mkSrcLoc (_PK_ src_filename) 1 } of {
+
+ PFailed err -> do { hPutStrLn stderr (showSDoc err);
+ return Nothing };
+
+ POk _ (PModule rdr_module@(HsModule mod_name _ _ _ _ _ _)) -> do {
+
+ dumpIfSet_dyn dflags Opt_D_dump_parsed "Parser" (ppr rdr_module) ;
+
+ dumpIfSet_dyn dflags Opt_D_source_stats "Source Statistics"
+ (ppSourceStats False rdr_module) ;
+
+ return (Just rdr_module)
+ }}
+
+
+simplThenTidy dflags pcs hst this_mod is_exported binds rules
+ = do -- Do main Core-language transformations ---------
+ -- _scc_ "Core2Core"
+ (simplified, orphan_rules)
+ <- core2core dflags pcs hst is_exported binds rules
+
+ -- Do saturation and convert to A-normal form
+ -- NOTE: future passes cannot transform the syntax, only annotate it
+ saturated <- coreSatPgm dflags simplified
+
+ -- Do the final tidy-up
+ (pcs', tidy_binds, tidy_orphan_rules)
+ <- tidyCorePgm dflags this_mod pcs saturated orphan_rules
+
+ return (pcs', tidy_binds, tidy_orphan_rules)
+
+
+restOfCodeGeneration dflags toInterp this_mod imported_module_names cost_centre_info
+ foreign_stuff env_tc stg_binds tidy_binds
+ hit pit -- these last two for mapping ModNames to Modules
+ | toInterp
+ = do (ibinds,itbl_env)
+ <- stgBindsToInterpSyn dflags (map fst stg_binds)
+ local_tycons local_classes
+ return (Nothing, Nothing, Just (ibinds,itbl_env))
+
+ | otherwise
+ = do -------------------------- Code generation -------------------------------
+ -- _scc_ "CodeGen"
+ abstractC <- codeGen dflags this_mod imported_modules
+ cost_centre_info fe_binders
+ local_tycons stg_binds
+
+ -------------------------- Code output -------------------------------
+ -- _scc_ "CodeOutput"
+ (maybe_stub_h_name, maybe_stub_c_name)
+ <- codeOutput dflags this_mod local_tycons
+ tidy_binds stg_binds
+ c_code h_code abstractC
+
+ return (maybe_stub_h_name, maybe_stub_c_name, Nothing)
+ where
+ local_tycons = typeEnvTyCons env_tc
+ local_classes = typeEnvClasses env_tc
+ imported_modules = map mod_name_to_Module imported_module_names
+ (h_code,c_code,fe_binders) = foreign_stuff
+
+ mod_name_to_Module :: ModuleName -> Module
+ mod_name_to_Module nm
+ = let str_mi = case lookupModuleEnvByName hit nm of
+ Just mi -> mi
+ Nothing -> case lookupModuleEnvByName pit nm of
+ Just mi -> mi
+ Nothing -> barf nm
+ in mi_module str_mi
+ barf nm = pprPanic "mod_name_to_Module: no hst or pst mapping for"
+ (ppr nm)
+
+
+myCoreToStg dflags this_mod tidy_binds
+ = do
+ () <- coreBindsSize tidy_binds `seq` return ()
+ -- TEMP: the above call zaps some space usage allocated by the
+ -- simplifier, which for reasons I don't understand, persists
+ -- thoroughout code generation
+
+ --let bcos = byteCodeGen tidy_binds
+ --putStrLn (showSDoc (vcat (map ppr bcos)))
+
+ -- _scc_ "Core2Stg"
+ stg_binds <- coreToStg dflags this_mod tidy_binds
+
+ -- _scc_ "Stg2Stg"
+ (stg_binds2, cost_centre_info) <- stg2stg dflags this_mod stg_binds
+
+ return (stg_binds2, cost_centre_info)
+\end{code}