2 % (c) The University of Glasgow, 2000
4 \section[CmLink]{Linker for GHCI}
7 module CmLink ( Linkable(..), Unlinked(..),
9 findModuleLinkable_maybe,
10 modname_of_linkable, is_package_linkable,
14 PersistentLinkerState{-abstractly!-}, emptyPLS,
24 import CmStaticInfo ( GhciMode(..) )
25 import Outputable ( SDoc )
26 import Digraph ( SCC(..), flattenSCC )
28 import Module ( ModuleName, PackageName )
32 import Panic ( panic )
37 #include "HsVersions.h"
41 data PersistentLinkerState
42 = PersistentLinkerState {
45 -- Current global mapping from RdrNames to closure addresses
46 closure_env :: ClosureEnv,
48 -- the current global mapping from RdrNames of DataCons to
49 -- info table addresses.
50 -- When a new Unlinked is linked into the running image, or an existing
51 -- module in the image is replaced, the itbl_env must be updated
55 -- notionally here, but really lives in the C part of the linker:
56 -- object_symtab :: FiniteMap String Addr
58 dummy :: () -- sigh, can't have an empty record
64 = LinkOK PersistentLinkerState
65 | LinkErrs PersistentLinkerState [SDoc]
67 findModuleLinkable_maybe :: [Linkable] -> ModuleName -> Maybe Linkable
68 findModuleLinkable_maybe lis mod
69 = case [LM time nm us | LM time nm us <- lis, nm == mod] of
72 many -> pprPanic "findModuleLinkable" (ppr mod)
75 emptyPLS :: IO PersistentLinkerState
77 emptyPLS = return (PersistentLinkerState { closure_env = emptyFM,
80 emptyPLS = return (PersistentLinkerState {})
85 link :: GhciMode -- interactive or batch
86 -> Bool -- attempt linking in batch mode?
87 -> [Linkable] -- only contains LMs, not LPs
88 -> PersistentLinkerState
91 -- For the moment, in the batch linker, we don't bother to tell doLink
92 -- which packages to link -- it just tries all that are available.
93 -- batch_attempt_linking should only be *looked at* in batch mode. It
94 -- should only be True if the upsweep was successful and someone
95 -- exports main, i.e., we have good reason to believe that linking
98 -- There will be (ToDo: are) two lists passed to link. These
101 -- 1. The list of all linkables in the current home package. This is
102 -- used by the batch linker to link the program, and by the interactive
103 -- linker to decide which modules from the previous link it can
105 -- 2. The list of modules on which we just called "compile". This list
106 -- is used by the interactive linker to decide which modules need
107 -- to be actually linked this time around (or unlinked and re-linked
108 -- if the module was recompiled).
110 link mode batch_attempt_linking linkables pls1
111 = do hPutStrLn stderr "CmLink.link: linkables are ..."
112 hPutStrLn stderr (showSDoc (vcat (map ppr linkables)))
113 res <- link' mode batch_attempt_linking linkables pls1
114 hPutStrLn stderr "CmLink.link: done"
117 link' Batch batch_attempt_linking linkables pls1
118 | batch_attempt_linking
119 = do let o_files = concatMap getOfiles linkables
121 -- doLink only returns if it succeeds
124 = do hPutStrLn stderr "CmLink.link(batch): upsweep (partially?) failed OR main not exported;"
125 hPutStrLn stderr " -- not doing linking"
128 getOfiles (LP _) = panic "CmLink.link(getOfiles): shouldn't get package linkables"
129 getOfiles (LM _ _ us) = map nameOfObject (filter isObject us)
131 link' Interactive batch_attempt_linking linkables pls1
132 = linkObjs linkables pls1
135 ppLinkableSCC :: SCC Linkable -> SDoc
136 ppLinkableSCC = ppr . flattenSCC
139 modname_of_linkable (LM _ nm _) = nm
140 modname_of_linkable (LP _) = panic "modname_of_linkable: package"
142 is_package_linkable (LP _) = True
143 is_package_linkable (LM _ _ _) = False
145 filterModuleLinkables :: (ModuleName -> Bool)
148 filterModuleLinkables p [] = []
149 filterModuleLinkables p (li:lis)
152 LM _ modnm _ -> if p modnm then retain else dump
154 dump = filterModuleLinkables p lis
157 -----------------------------------------------------------------------------
158 -- Linker for interactive mode
161 linkObjs = panic "CmLink.linkObjs: no interpreter"
162 unload = panic "CmLink.unload: no interpreter"
163 lookupClosure = panic "CmLink.lookupClosure: no interpreter"
165 linkObjs [] pls = linkFinish pls [] []
166 linkObjs (l@(LM _ _ uls) : ls) pls
167 | all isObject uls = do
168 mapM_ loadObj [ file | DotO file <- uls ]
170 | all isInterpretable uls = linkInterpretedCode (l:ls) [] [] pls
171 | otherwise = invalidLinkable
173 throwDyn (OtherError "CmLink.linkObjs: found package linkable")
176 linkInterpretedCode [] mods ul_trees pls = linkFinish pls mods ul_trees
177 linkInterpretedCode (LM _ m uls : ls) mods ul_trees pls
178 | all isInterpretable uls =
179 linkInterpretedCode ls (m:mods) (uls++ul_trees) pls
182 = throwDyn (OtherError "can't link object code that depends on interpreted code")
183 | otherwise = invalidLinkable
184 linkInterpretedCode _ _ _ pls =
185 throwDyn (OtherError "CmLink.linkInterpretedCode: found package linkable")
187 invalidLinkable = throwDyn (OtherError "linkable doesn't contain entirely objects interpreted code")
190 -- link all the interpreted code in one go. We first remove from the
191 -- various environments any previous versions of these modules.
192 linkFinish pls mods ul_trees = do
194 let itbl_env' = filterRdrNameEnv mods (itbl_env pls)
195 closure_env' = filterRdrNameEnv mods (closure_env pls)
196 stuff = [ (trees,itbls) | Trees trees itbls <- ul_trees ]
198 (ibinds, new_itbl_env, new_closure_env) <-
199 linkIModules itbl_env' closure_env' stuff
201 let new_pls = PersistentLinkerState {
202 closure_env = new_closure_env,
203 itbl_env = new_itbl_env
205 putStrLn (showSDoc (vcat (map ppr (keysFM new_closure_env))))
206 return (LinkOK new_pls)
208 -- purge the current "linked image"
209 unload :: PersistentLinkerState -> IO PersistentLinkerState
210 unload pls = return pls{ closure_env = emptyFM, itbl_env = emptyFM }
212 linkExpr :: PersistentLinkerState -> UnlinkedIExpr -> IO HValue
213 linkExpr PersistentLinkerState{ itbl_env = ie, closure_env = ce } expr
214 = iExprToHValue ie ce expr