[project @ 2001-07-24 04:52:49 by ken]
[ghc-hetmet.git] / ghc / compiler / main / HscMain.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-2000
3 %
4
5 \section[GHC_Main]{Main driver for Glasgow Haskell compiler}
6
7 \begin{code}
8 module HscMain ( HscResult(..), hscMain, 
9 #ifdef GHCI
10                  hscStmt, hscThing,
11 #endif
12                  initPersistentCompilerState ) where
13
14 #include "HsVersions.h"
15
16 #ifdef GHCI
17 import ByteCodeGen      ( byteCodeGen )
18 import CoreTidy         ( tidyCoreExpr )
19 import CorePrep         ( corePrepExpr )
20 import SrcLoc           ( noSrcLoc )
21 import Rename           ( renameStmt )
22 import RdrName          ( mkUnqual )
23 import RdrHsSyn         ( RdrNameStmt )
24 import OccName          ( dataName )
25 import Type             ( Type )
26 import Id               ( Id, idName, setGlobalIdDetails )
27 import IdInfo           ( GlobalIdDetails(VanillaGlobal) )
28 import HscTypes         ( InteractiveContext(..) )
29 import PrelNames        ( iNTERACTIVE )
30 import StringBuffer     ( stringToStringBuffer )
31 import FastString       ( mkFastString )
32 #endif
33
34 import HsSyn
35
36 import Id               ( idName )
37 import IdInfo           ( CafInfo(..), CgInfoEnv, CgInfo(..) )
38 import StringBuffer     ( hGetStringBuffer, freeStringBuffer )
39 import Parser
40 import Lex              ( PState(..), ParseResult(..) )
41 import SrcLoc           ( mkSrcLoc )
42 import Finder           ( findModule )
43 import Rename           ( checkOldIface, renameModule, closeIfaceDecls )
44 import Rules            ( emptyRuleBase )
45 import PrelInfo         ( wiredInThingEnv, wiredInThings )
46 import PrelNames        ( knownKeyNames )
47 import MkIface          ( mkFinalIface )
48 import TcModule
49 import InstEnv          ( emptyInstEnv )
50 import Desugar
51 import SimplCore
52 import CoreUtils        ( coreBindsSize )
53 import CoreTidy         ( tidyCorePgm )
54 import CorePrep         ( corePrepPgm )
55 import StgSyn
56 import CoreToStg        ( coreToStg )
57 import SimplStg         ( stg2stg )
58 import CodeGen          ( codeGen )
59 import CodeOutput       ( codeOutput )
60
61 import Module           ( ModuleName, moduleName, mkHomeModule, 
62                           moduleUserString )
63 import CmdLineOpts
64 import ErrUtils         ( dumpIfSet_dyn, showPass, printError )
65 import Util             ( unJust )
66 import UniqSupply       ( mkSplitUniqSupply )
67
68 import Bag              ( emptyBag )
69 import Outputable
70 import Interpreter
71 import HscStats         ( ppSourceStats )
72 import HscTypes
73 import FiniteMap        ( FiniteMap, plusFM, emptyFM, addToFM )
74 import OccName          ( OccName )
75 import Name             ( Name, nameModule, nameOccName, getName, isGlobalName )
76 import NameEnv          ( emptyNameEnv, mkNameEnv )
77 import Module           ( Module )
78
79 import IOExts           ( newIORef, readIORef, writeIORef, unsafePerformIO )
80
81 import Monad            ( when )
82 import Maybe            ( isJust, fromJust )
83 import IO
84
85 import MkExternalCore   ( emitExternalCore )
86 \end{code}
87
88
89 %************************************************************************
90 %*                                                                      *
91 \subsection{The main compiler pipeline}
92 %*                                                                      *
93 %************************************************************************
94
95 \begin{code}
96 data HscResult
97    -- compilation failed
98    = HscFail     PersistentCompilerState -- updated PCS
99    -- concluded that it wasn't necessary
100    | HscNoRecomp PersistentCompilerState -- updated PCS
101                  ModDetails              -- new details (HomeSymbolTable additions)
102                  ModIface                -- new iface (if any compilation was done)
103    -- did recompilation
104    | HscRecomp   PersistentCompilerState -- updated PCS
105                  ModDetails              -- new details (HomeSymbolTable additions)
106                  ModIface                -- new iface (if any compilation was done)
107                  Bool                   -- stub_h exists
108                  Bool                   -- stub_c exists
109 #ifdef GHCI
110                  (Maybe ([UnlinkedBCO],ItblEnv)) -- interpreted code, if any
111 #else
112                  (Maybe ())                      -- no interpreted code whatsoever
113 #endif
114
115         -- no errors or warnings; the individual passes
116         -- (parse/rename/typecheck) print messages themselves
117
118 hscMain
119   :: GhciMode
120   -> DynFlags
121   -> Module
122   -> ModuleLocation             -- location info
123   -> Bool                       -- True <=> source unchanged
124   -> Bool                       -- True <=> have an object file (for msgs only)
125   -> Maybe ModIface             -- old interface, if available
126   -> HomeSymbolTable            -- for home module ModDetails
127   -> HomeIfaceTable
128   -> PersistentCompilerState    -- IN: persistent compiler state
129   -> IO HscResult
130
131 hscMain ghci_mode dflags mod location source_unchanged have_object 
132         maybe_old_iface hst hit pcs
133  = do {
134       showPass dflags ("Checking old interface for hs = " 
135                         ++ show (ml_hs_file location)
136                         ++ ", hspp = " ++ show (ml_hspp_file location));
137
138       (pcs_ch, errs_found, (recomp_reqd, maybe_checked_iface))
139          <- _scc_ "checkOldIface"
140             checkOldIface ghci_mode dflags hit hst pcs (ml_hi_file location)
141                 source_unchanged maybe_old_iface;
142
143       if errs_found then
144          return (HscFail pcs_ch)
145       else do {
146
147       let no_old_iface = not (isJust maybe_checked_iface)
148           what_next | recomp_reqd || no_old_iface = hscRecomp 
149                     | otherwise                   = hscNoRecomp
150       ;
151       what_next ghci_mode dflags have_object mod location 
152                 maybe_checked_iface hst hit pcs_ch
153       }}
154
155
156 -- we definitely expect to have the old interface available
157 hscNoRecomp ghci_mode dflags have_object 
158             mod location (Just old_iface) hst hit pcs_ch
159  | ghci_mode == OneShot
160  = do {
161       hPutStrLn stderr "compilation IS NOT required";
162       let { bomb = panic "hscNoRecomp:OneShot" };
163       return (HscNoRecomp pcs_ch bomb bomb)
164       }
165  | otherwise
166  = do {
167       when (verbosity dflags >= 1) $
168                 hPutStrLn stderr ("Skipping  " ++ 
169                         compMsg have_object mod location);
170
171       -- CLOSURE
172       (pcs_cl, closure_errs, cl_hs_decls) 
173          <- closeIfaceDecls dflags hit hst pcs_ch old_iface ;
174       if closure_errs then 
175          return (HscFail pcs_cl) 
176       else do {
177
178       -- TYPECHECK
179       maybe_tc_result 
180         <- typecheckIface dflags pcs_cl hst old_iface cl_hs_decls;
181
182       case maybe_tc_result of {
183          Nothing -> return (HscFail pcs_cl);
184          Just (pcs_tc, new_details) ->
185
186       return (HscNoRecomp pcs_tc new_details old_iface)
187       }}}
188
189 compMsg use_object mod location =
190     mod_str ++ take (max 0 (16 - length mod_str)) (repeat ' ')
191     ++" ( " ++ unJust "hscRecomp" (ml_hs_file location) ++ ", "
192     ++ (if use_object
193           then unJust "hscRecomp" (ml_obj_file location)
194           else "interpreted")
195     ++ " )"
196  where mod_str = moduleUserString mod
197
198
199 hscRecomp ghci_mode dflags have_object 
200           mod location maybe_checked_iface hst hit pcs_ch
201  = do   {
202           -- what target are we shooting for?
203         ; let toInterp = dopt_HscLang dflags == HscInterpreted
204
205         ; when (verbosity dflags >= 1) $
206                 hPutStrLn stderr ("Compiling " ++ 
207                         compMsg (not toInterp) mod location);
208
209             -------------------
210             -- PARSE
211             -------------------
212         ; maybe_parsed <- myParseModule dflags 
213                              (unJust "hscRecomp:hspp" (ml_hspp_file location))
214         ; case maybe_parsed of {
215              Nothing -> return (HscFail pcs_ch);
216              Just rdr_module -> do {
217         ; let this_mod = mkHomeModule (hsModuleName rdr_module)
218     
219             -------------------
220             -- RENAME
221             -------------------
222         ; (pcs_rn, print_unqualified, maybe_rn_result) 
223              <- _scc_ "Rename" 
224                  renameModule dflags hit hst pcs_ch this_mod rdr_module
225         ; case maybe_rn_result of {
226              Nothing -> return (HscFail pcs_ch{-was: pcs_rn-});
227              Just (is_exported, new_iface, rn_hs_decls) -> do {
228     
229             -- In interactive mode, we don't want to discard any top-level entities at
230             -- all (eg. do not inline them away during simplification), and retain them
231             -- all in the TypeEnv so they are available from the command line.
232             --
233             -- isGlobalName separates the user-defined top-level names from those
234             -- introduced by the type checker.
235         ; let dont_discard | ghci_mode == Interactive = isGlobalName
236                            | otherwise = is_exported
237
238             -------------------
239             -- TYPECHECK
240             -------------------
241         ; maybe_tc_result 
242             <- _scc_ "TypeCheck" typecheckModule dflags pcs_rn hst new_iface 
243                                              print_unqualified rn_hs_decls 
244         ; case maybe_tc_result of {
245              Nothing -> return (HscFail pcs_ch{-was: pcs_rn-});
246              Just (pcs_tc, tc_result) -> do {
247     
248             -------------------
249             -- DESUGAR
250             -------------------
251         ; (ds_details, foreign_stuff) 
252              <- _scc_ "DeSugar" 
253                 deSugar dflags pcs_tc hst this_mod print_unqualified tc_result
254
255         ; pcs_middle
256             <- if ghci_mode == OneShot 
257                   then do init_pcs <- initPersistentCompilerState
258                           init_prs <- initPersistentRenamerState
259                           let 
260                               rules   = pcs_rules pcs_tc        
261                               orig_tc = prsOrig (pcs_PRS pcs_tc)
262                               new_prs = init_prs{ prsOrig=orig_tc }
263
264                           orig_tc `seq` rules `seq` new_prs `seq`
265                             return init_pcs{ pcs_PRS = new_prs,
266                                              pcs_rules = rules }
267                   else return pcs_tc
268
269             -------------------
270             -- SIMPLIFY
271             -------------------
272         ; simpl_details
273              <- _scc_     "Core2Core"
274                 core2core dflags pcs_middle hst dont_discard ds_details
275
276             -------------------
277             -- TIDY
278             -------------------
279         ; cg_info_ref <- newIORef Nothing ;
280         ; let cg_info :: CgInfoEnv
281               cg_info = unsafePerformIO $ do {
282                            maybe_cg_env <- readIORef cg_info_ref ;
283                            case maybe_cg_env of
284                              Just env -> return env
285                              Nothing  -> do { printError "Urk! Looked at CgInfo too early!";
286                                               return emptyNameEnv } }
287                 -- cg_info_ref will be filled in just after restOfCodeGeneration
288                 -- Meanwhile, tidyCorePgm is careful not to look at cg_info!
289
290         ; (pcs_simpl, tidy_details) 
291              <- _scc_ "CoreTidy"
292                 tidyCorePgm dflags this_mod pcs_middle cg_info simpl_details
293       
294         ; pcs_final <- if ghci_mode == OneShot then initPersistentCompilerState
295                                                else return pcs_simpl
296
297         -- alive at this point:  
298         --      tidy_details
299         --      new_iface               
300
301         ; emitExternalCore dflags new_iface tidy_details 
302             -------------------
303             -- PREPARE FOR CODE GENERATION
304             -------------------
305               -- Do saturation and convert to A-normal form
306         ; prepd_details <- _scc_ "CorePrep" corePrepPgm dflags tidy_details
307
308             -------------------
309             -- CONVERT TO STG and COMPLETE CODE GENERATION
310             -------------------
311         ; let
312             ModDetails{md_binds=binds, md_types=env_tc} = prepd_details
313
314             local_tycons     = typeEnvTyCons  env_tc
315             local_classes    = typeEnvClasses env_tc
316
317             imported_module_names = map ideclName (hsModuleImports rdr_module)
318
319             mod_name_to_Module nm
320                  = do m <- findModule nm ; return (fst (fromJust m))
321
322             (h_code,c_code,fe_binders) = foreign_stuff
323
324         ; imported_modules <- mapM mod_name_to_Module imported_module_names
325
326         ; (stub_h_exists, stub_c_exists, maybe_bcos, final_iface )
327            <- if toInterp
328 #ifdef GHCI
329                 then do 
330                     -----------------  Generate byte code ------------------
331                     (bcos,itbl_env) <- byteCodeGen dflags binds 
332                                         local_tycons local_classes
333
334                     -- Fill in the code-gen info
335                     writeIORef cg_info_ref (Just emptyNameEnv)
336
337                     ------------------ BUILD THE NEW ModIface ------------
338                     final_iface <- _scc_ "MkFinalIface" 
339                           mkFinalIface ghci_mode dflags location 
340                                    maybe_checked_iface new_iface tidy_details
341
342                     return ( False, False, Just (bcos,itbl_env), final_iface )
343 #else
344                 then error "GHC not compiled with interpreter"
345 #endif
346
347                 else do
348                     -----------------  Convert to STG ------------------
349                     (stg_binds, cost_centre_info, stg_back_end_info) 
350                               <- _scc_ "CoreToStg"
351                                   myCoreToStg dflags this_mod binds
352                     
353                     -- Fill in the code-gen info for the earlier tidyCorePgm
354                     writeIORef cg_info_ref (Just stg_back_end_info)
355
356                     ------------------ BUILD THE NEW ModIface ------------
357                     final_iface <- _scc_ "MkFinalIface" 
358                           mkFinalIface ghci_mode dflags location 
359                                    maybe_checked_iface new_iface tidy_details
360
361                     ------------------  Code generation ------------------
362                     abstractC <- _scc_ "CodeGen"
363                                   codeGen dflags this_mod imported_modules
364                                          cost_centre_info fe_binders
365                                          local_tycons stg_binds
366                     
367                     ------------------  Code output -----------------------
368                     (stub_h_exists, stub_c_exists)
369                        <- codeOutput dflags this_mod local_tycons
370                              binds stg_binds
371                              c_code h_code abstractC
372                         
373                     return (stub_h_exists, stub_c_exists, Nothing, final_iface)
374
375         ; let final_details = tidy_details {md_binds = []} 
376
377
378           -- and the answer is ...
379         ; return (HscRecomp pcs_final
380                             final_details
381                             final_iface
382                             stub_h_exists stub_c_exists
383                             maybe_bcos)
384           }}}}}}}
385
386 myParseModule dflags src_filename
387  = do --------------------------  Parser  ----------------
388       showPass dflags "Parser"
389       _scc_  "Parser" do
390
391       buf <- hGetStringBuffer True{-expand tabs-} src_filename
392
393       let glaexts | dopt Opt_GlasgowExts dflags = 1#
394                   | otherwise                   = 0#
395
396       case parseModule buf PState{ bol = 0#, atbol = 1#,
397                                    context = [], glasgow_exts = glaexts,
398                                    loc = mkSrcLoc (_PK_ src_filename) 1 } of {
399
400         PFailed err -> do { hPutStrLn stderr (showSDoc err);
401                             freeStringBuffer buf;
402                             return Nothing };
403
404         POk _ rdr_module@(HsModule mod_name _ _ _ _ _ _) -> do {
405
406       dumpIfSet_dyn dflags Opt_D_dump_parsed "Parser" (ppr rdr_module) ;
407       
408       dumpIfSet_dyn dflags Opt_D_source_stats "Source Statistics"
409                            (ppSourceStats False rdr_module) ;
410       
411       return (Just rdr_module)
412         -- ToDo: free the string buffer later.
413       }}
414
415
416 myCoreToStg dflags this_mod tidy_binds
417  = do 
418       () <- coreBindsSize tidy_binds `seq` return ()
419       -- TEMP: the above call zaps some space usage allocated by the
420       -- simplifier, which for reasons I don't understand, persists
421       -- thoroughout code generation
422
423       stg_binds <- _scc_ "Core2Stg" coreToStg dflags tidy_binds
424
425       (stg_binds2, cost_centre_info)
426            <- _scc_ "Core2Stg" stg2stg dflags this_mod stg_binds
427
428       let env_rhs :: CgInfoEnv
429           env_rhs = mkNameEnv [ (idName bndr, CgInfo (stgRhsArity rhs) caf_info)
430                               | (bind,_) <- stg_binds2, 
431                                 let caf_info 
432                                      | stgBindHasCafRefs bind = MayHaveCafRefs
433                                      | otherwise = NoCafRefs,
434                                 (bndr,rhs) <- stgBindPairs bind ]
435
436       return (stg_binds2, cost_centre_info, env_rhs)
437    where
438       stgBindPairs (StgNonRec _ b r) = [(b,r)]
439       stgBindPairs (StgRec    _ prs) = prs
440
441
442 \end{code}
443
444
445 %************************************************************************
446 %*                                                                      *
447 \subsection{Compiling a do-statement}
448 %*                                                                      *
449 %************************************************************************
450
451 \begin{code}
452 #ifdef GHCI
453 hscStmt
454   :: DynFlags
455   -> HomeSymbolTable    
456   -> HomeIfaceTable
457   -> PersistentCompilerState    -- IN: persistent compiler state
458   -> InteractiveContext         -- Context for compiling
459   -> String                     -- The statement
460   -> Bool                       -- just treat it as an expression
461   -> IO ( PersistentCompilerState, 
462           Maybe ( [Id], 
463                   Type, 
464                   UnlinkedBCOExpr) )
465 \end{code}
466
467 When the UnlinkedBCOExpr is linked you get an HValue of type
468         IO [HValue]
469 When you run it you get a list of HValues that should be 
470 the same length as the list of names; add them to the ClosureEnv.
471
472 A naked expression returns a singleton Name [it].
473
474         What you type                   The IO [HValue] that hscStmt returns
475         -------------                   ------------------------------------
476         let pat = expr          ==>     let pat = expr in return [coerce HVal x, coerce HVal y, ...]
477                                         bindings: [x,y,...]
478
479         pat <- expr             ==>     expr >>= \ pat -> return [coerce HVal x, coerce HVal y, ...]
480                                         bindings: [x,y,...]
481
482         expr (of IO type)       ==>     expr >>= \ v -> return [v]
483           [NB: result not printed]      bindings: [it]
484           
485
486         expr (of non-IO type, 
487           result showable)      ==>     let v = expr in print v >> return [v]
488                                         bindings: [it]
489
490         expr (of non-IO type, 
491           result not showable)  ==>     error
492
493 \begin{code}
494 hscStmt dflags hst hit pcs0 icontext stmt just_expr
495    = let 
496         InteractiveContext { 
497              ic_rn_env   = rn_env, 
498              ic_type_env = type_env,
499              ic_module   = scope_mod } = icontext
500      in
501      do { maybe_stmt <- hscParseStmt dflags stmt
502         ; case maybe_stmt of
503              Nothing -> return (pcs0, Nothing)
504              Just parsed_stmt -> do {
505
506            let { notExprStmt (ExprStmt _ _ _) = False;
507                  notExprStmt _                = True 
508                };
509
510            if (just_expr && notExprStmt parsed_stmt)
511                 then do hPutStrLn stderr ("not an expression: `" ++ stmt ++ "'")
512                         return (pcs0, Nothing)
513                 else do {
514
515                 -- Rename it
516           (pcs1, print_unqual, maybe_renamed_stmt)
517                  <- renameStmt dflags hit hst pcs0 scope_mod 
518                                 iNTERACTIVE rn_env parsed_stmt
519
520         ; case maybe_renamed_stmt of
521                 Nothing -> return (pcs0, Nothing)
522                 Just (bound_names, rn_stmt) -> do {
523
524                 -- Typecheck it
525           maybe_tc_return <- 
526             if just_expr 
527                 then case rn_stmt of { (ExprStmt e _ _, decls) -> 
528                      typecheckExpr dflags pcs1 hst type_env
529                            print_unqual iNTERACTIVE (e,decls) }
530                 else typecheckStmt dflags pcs1 hst type_env
531                            print_unqual iNTERACTIVE bound_names rn_stmt
532
533         ; case maybe_tc_return of
534                 Nothing -> return (pcs0, Nothing)
535                 Just (pcs2, tc_expr, bound_ids, ty) ->  do {
536
537                 -- Desugar it
538           ds_expr <- deSugarExpr dflags pcs2 hst iNTERACTIVE print_unqual tc_expr
539         
540                 -- Simplify it
541         ; simpl_expr <- simplifyExpr dflags pcs2 hst ds_expr
542
543                 -- Tidy it (temporary, until coreSat does cloning)
544         ; tidy_expr <- tidyCoreExpr simpl_expr
545
546                 -- Prepare for codegen
547         ; prepd_expr <- corePrepExpr dflags tidy_expr
548
549                 -- Convert to BCOs
550         ; bcos <- coreExprToBCOs dflags prepd_expr
551
552         ; let
553                 -- Make all the bound ids "global" ids, now that
554                 -- they're notionally top-level bindings.  This is
555                 -- important: otherwise when we come to compile an expression
556                 -- using these ids later, the byte code generator will consider
557                 -- the occurrences to be free rather than global.
558              global_bound_ids = map globaliseId bound_ids;
559              globaliseId id   = setGlobalIdDetails id VanillaGlobal
560
561         ; return (pcs2, Just (global_bound_ids, ty, bcos))
562
563      }}}}}
564
565 hscThing -- like hscStmt, but deals with a single identifier
566   :: DynFlags
567   -> HomeSymbolTable    
568   -> HomeIfaceTable
569   -> PersistentCompilerState    -- IN: persistent compiler state
570   -> InteractiveContext         -- Context for compiling
571   -> String                     -- The identifier
572   -> IO ( PersistentCompilerState, 
573           Maybe TyThing )
574 hscThing dflags hst hit pcs0 icontext id
575    = let 
576         InteractiveContext { 
577              ic_rn_env   = rn_env, 
578              ic_type_env = type_env,
579              ic_module   = scope_mod } = icontext
580         fname = mkFastString id
581         rn = mkUnqual dataName fname -- need to guess correct namespace
582         stmt = ResultStmt (HsVar rn) noSrcLoc
583      in
584      do { (pcs, err, maybe_stmt) <- renameStmt dflags hit hst pcs0 scope_mod scope_mod rn_env stmt
585         ; case maybe_stmt of
586              Nothing -> return (pcs, Nothing)
587              Just (n:ns, _) -> return (pcs, lookupType hst type_env n)
588         }
589
590 hscParseStmt :: DynFlags -> String -> IO (Maybe RdrNameStmt)
591 hscParseStmt dflags str
592  = do --------------------------  Parser  ----------------
593       showPass dflags "Parser"
594       _scc_ "Parser" do
595
596       buf <- stringToStringBuffer str
597
598       let glaexts | dopt Opt_GlasgowExts dflags = 1#
599                   | otherwise                   = 0#
600
601       case parseStmt buf PState{ bol = 0#, atbol = 1#,
602                                  context = [], glasgow_exts = glaexts,
603                                  loc = mkSrcLoc SLIT("<no file>") 0 } of {
604
605         PFailed err -> do { hPutStrLn stderr (showSDoc err);
606 --      Not yet implemented in <4.11    freeStringBuffer buf;
607                             return Nothing };
608
609         -- no stmt: the line consisted of just space or comments
610         POk _ Nothing -> return Nothing;
611
612         POk _ (Just rdr_stmt) -> do {
613
614       --ToDo: can't free the string buffer until we've finished this
615       -- compilation sweep and all the identifiers have gone away.
616       --freeStringBuffer buf;
617       dumpIfSet_dyn dflags Opt_D_dump_parsed "Parser" (ppr rdr_stmt);
618       return (Just rdr_stmt)
619       }}
620 #endif
621 \end{code}
622
623 %************************************************************************
624 %*                                                                      *
625 \subsection{Initial persistent state}
626 %*                                                                      *
627 %************************************************************************
628
629 \begin{code}
630 initPersistentCompilerState :: IO PersistentCompilerState
631 initPersistentCompilerState 
632   = do prs <- initPersistentRenamerState
633        return (
634         PCS { pcs_PIT   = emptyIfaceTable,
635               pcs_PTE   = wiredInThingEnv,
636               pcs_insts = emptyInstEnv,
637               pcs_rules = emptyRuleBase,
638               pcs_PRS   = prs
639             }
640         )
641
642 initPersistentRenamerState :: IO PersistentRenamerState
643   = do us <- mkSplitUniqSupply 'r'
644        return (
645         PRS { prsOrig  = NameSupply { nsUniqs = us,
646                                       nsNames = initOrigNames,
647                                       nsIPs   = emptyFM },
648               prsDecls   = (emptyNameEnv, 0),
649               prsInsts   = (emptyBag, 0),
650               prsRules   = (emptyBag, 0),
651               prsImpMods = emptyFM
652             }
653         )
654
655 initOrigNames :: FiniteMap (ModuleName,OccName) Name
656 initOrigNames 
657    = grab knownKeyNames `plusFM` grab (map getName wiredInThings)
658      where
659         grab names = foldl add emptyFM names
660         add env name 
661            = addToFM env (moduleName (nameModule name), nameOccName name) name
662 \end{code}