2 % (c) The University of Glasgow, 2001
4 \section[CmLink]{The compilation manager's linker}
8 LinkResult(..), link, unload,
10 filterModuleLinkables,
11 findModuleLinkable_maybe,
13 PersistentLinkerState{-abstractly!-}, emptyPLS,
16 delListFromClosureEnv,
24 import ByteCodeLink ( linkIModules, linkIExpr )
30 import HscTypes ( GhciMode(..) )
31 import Outputable ( SDoc )
33 import Module ( ModuleName )
36 import ErrUtils ( showPass )
37 import CmdLineOpts ( DynFlags(..) )
41 import Exception ( block )
49 #include "HsVersions.h"
51 -- ---------------------------------------------------------------------------
54 -- The PersistentLinkerState maps Names to actual closures (for
55 -- interpreted code only), for use during linking.
57 data PersistentLinkerState
58 = PersistentLinkerState {
61 -- Current global mapping from RdrNames to closure addresses
62 closure_env :: ClosureEnv,
64 -- the current global mapping from RdrNames of DataCons to
65 -- info table addresses.
66 -- When a new Unlinked is linked into the running image, or an existing
67 -- module in the image is replaced, the itbl_env must be updated
71 -- the currently loaded interpreted modules
72 bcos_loaded :: [Linkable]
75 dummy :: () -- sigh, can't have an empty record
80 emptyPLS :: IO PersistentLinkerState
82 emptyPLS = return (PersistentLinkerState { closure_env = emptyFM,
86 emptyPLS = return (PersistentLinkerState {})
89 -- We also keep track of which object modules are currently loaded
90 -- into the dynamic linker, so that we can unload them again later.
92 -- This state *must* match the actual state of the dyanmic linker at
93 -- all times, which is why we keep it private here and don't
94 -- put it in the PersistentLinkerState.
96 GLOBAL_VAR(v_ObjectsLoaded, [], [Linkable])
99 -- ---------------------------------------------------------------------------
102 findModuleLinkable_maybe :: [Linkable] -> ModuleName -> Maybe Linkable
103 findModuleLinkable_maybe lis mod
104 = case [LM time nm us | LM time nm us <- lis, nm == mod] of
107 many -> pprPanic "findModuleLinkable" (ppr mod)
109 filterModuleLinkables :: (ModuleName -> Bool) -> [Linkable] -> [Linkable]
110 filterModuleLinkables p [] = []
111 filterModuleLinkables p (li:lis)
113 LM _ modnm _ -> if p modnm then retain else dump
115 dump = filterModuleLinkables p lis
118 linkableInSet :: Linkable -> [Linkable] -> Bool
119 linkableInSet l objs_loaded =
120 case findModuleLinkable_maybe objs_loaded (linkableModName l) of
122 Just m -> linkableTime l == linkableTime m
124 -- These two are used to add/remove entries from the closure env for
125 -- new bindings made at the prompt.
127 delListFromClosureEnv :: PersistentLinkerState -> [Name]
128 -> IO PersistentLinkerState
129 delListFromClosureEnv pls names
130 = return pls{ closure_env = delListFromFM (closure_env pls) names }
132 addListToClosureEnv :: PersistentLinkerState -> [(Name,HValue)]
133 -> IO PersistentLinkerState
134 addListToClosureEnv pls new_bindings
135 = return pls{ closure_env = addListToFM (closure_env pls) new_bindings }
138 -- ---------------------------------------------------------------------------
139 -- Unloading old objects ready for a new compilation sweep.
141 -- The compilation manager provides us with a list of linkables that it
142 -- considers "stable", i.e. won't be recompiled this time around. For
143 -- each of the modules current linked in memory,
145 -- * if the linkable is stable (and it's the same one - the
146 -- user may have recompiled the module on the side), we keep it,
148 -- * otherwise, we unload it.
150 -- * we also implicitly unload all temporary bindings at this point.
154 -> [Linkable] -- stable linkables
155 -> PersistentLinkerState
156 -> IO PersistentLinkerState
158 unload Batch dflags linkables pls = return pls
161 unload Interactive dflags linkables pls
162 = block $ do -- block, so we're safe from Ctrl-C in here
163 objs_loaded <- readIORef v_ObjectsLoaded
164 objs_loaded' <- filterM (maybeUnload objs_to_keep) objs_loaded
165 writeIORef v_ObjectsLoaded objs_loaded'
167 bcos_loaded' <- filterM (maybeUnload bcos_to_keep) (bcos_loaded pls)
169 let objs_retained = map linkableModName objs_loaded'
170 bcos_retained = map linkableModName bcos_loaded'
171 itbl_env' = filterNameMap bcos_retained (itbl_env pls)
172 closure_env' = filterNameMap bcos_retained (closure_env pls)
174 let verb = verbosity dflags
175 when (verb >= 3) $ do
176 hPutStrLn stderr (showSDoc
177 (text "CmLink.unload: retaining objs" <+> ppr objs_retained))
178 hPutStrLn stderr (showSDoc
179 (text "CmLink.unload: retaining bcos" <+> ppr bcos_retained))
181 return pls{ itbl_env = itbl_env',
182 closure_env = closure_env',
183 bcos_loaded = bcos_loaded' }
185 (objs_to_keep, bcos_to_keep) = partition isObjectLinkable linkables
187 maybeUnload :: [Linkable] -> Linkable -> IO Bool
188 maybeUnload keep_linkables l@(LM time mod objs)
189 | linkableInSet l linkables
192 = do mapM unloadObj [ f | DotO f <- objs ]
195 unload Interactive dflags linkables pls = panic "CmLink.unload: no interpreter"
198 -----------------------------------------------------------------------------
202 = LinkOK PersistentLinkerState
203 | LinkErrs PersistentLinkerState [SDoc]
205 link :: GhciMode -- interactive or batch
206 -> DynFlags -- dynamic flags
207 -> Bool -- attempt linking in batch mode?
209 -> PersistentLinkerState
212 -- For the moment, in the batch linker, we don't bother to tell doLink
213 -- which packages to link -- it just tries all that are available.
214 -- batch_attempt_linking should only be *looked at* in batch mode. It
215 -- should only be True if the upsweep was successful and someone
216 -- exports main, i.e., we have good reason to believe that linking
219 -- There will be (ToDo: are) two lists passed to link. These
222 -- 1. The list of all linkables in the current home package. This is
223 -- used by the batch linker to link the program, and by the interactive
224 -- linker to decide which modules from the previous link it can
226 -- 2. The list of modules on which we just called "compile". This list
227 -- is used by the interactive linker to decide which modules need
228 -- to be actually linked this time around (or unlinked and re-linked
229 -- if the module was recompiled).
231 link mode dflags batch_attempt_linking linkables pls1
232 = do let verb = verbosity dflags
233 when (verb >= 3) $ do
234 hPutStrLn stderr "CmLink.link: linkables are ..."
235 hPutStrLn stderr (showSDoc (vcat (map ppr linkables)))
236 res <- link' mode dflags batch_attempt_linking linkables pls1
238 hPutStrLn stderr "CmLink.link: done"
241 link' Batch dflags batch_attempt_linking linkables pls1
242 | batch_attempt_linking
243 = do let o_files = concatMap getOfiles linkables
245 hPutStrLn stderr "ghc: linking ..."
246 -- don't showPass in Batch mode; doLink will do that for us.
248 -- doLink only returns if it succeeds
251 = do when (verb >= 3) $ do
252 hPutStrLn stderr "CmLink.link(batch): upsweep (partially) failed OR"
253 hPutStrLn stderr " Main.main not exported; not linking."
256 verb = verbosity dflags
257 getOfiles (LM _ _ us) = map nameOfObject (filter isObject us)
260 link' Interactive dflags batch_attempt_linking linkables pls
261 = do showPass dflags "Linking"
262 block $ do -- don't want to be interrupted by ^C in here
264 -- Always load objects first. Objects aren't allowed to
266 let (objs, bcos) = partition isObjectLinkable linkables
268 objs_loaded <- readIORef v_ObjectsLoaded
269 objs_loaded' <- linkObjs objs objs_loaded
270 writeIORef v_ObjectsLoaded objs_loaded'
272 -- resolve symbols within the object files
275 -- finally link the interpreted linkables
279 -----------------------------------------------------------------------------
280 -- Linker for interactive mode
283 linkObjs [] objs_loaded = return objs_loaded
284 linkObjs (l@(LM _ m uls) : ls) objs_loaded
285 | linkableInSet l objs_loaded = linkObjs ls objs_loaded -- already loaded
286 | otherwise = do mapM_ loadObj [ file | DotO file <- uls ]
287 linkObjs ls (l:objs_loaded)
289 linkBCOs [] ul_trees pls = linkFinish pls ul_trees
290 linkBCOs (l@(LM _ m uls) : ls) ul_trees pls
291 | linkableInSet l (bcos_loaded pls)
292 = linkBCOs ls ul_trees pls
294 = linkBCOs ls (uls++ul_trees) pls{bcos_loaded = l : bcos_loaded pls}
296 -- link all the interpreted code in one go.
297 linkFinish pls ul_bcos = do
299 let stuff = [ (bcos,itbls) | BCOs bcos itbls <- ul_bcos ]
301 (ibinds, new_itbl_env, new_closure_env) <-
302 linkIModules (itbl_env pls) (closure_env pls) stuff
304 let new_pls = pls { closure_env = new_closure_env,
305 itbl_env = new_itbl_env
307 return (LinkOK new_pls)
310 -- ---------------------------------------------------------------------------
311 -- Link a single expression
314 linkExpr :: PersistentLinkerState -> UnlinkedBCOExpr -> IO HValue
315 linkExpr PersistentLinkerState{ itbl_env = ie, closure_env = ce } bcos
316 = linkIExpr ie ce bcos