2 % (c) The University of Glasgow, 2000
4 \section[CmLink]{Linker for GHCI}
7 module CmLink ( Linkable(..), Unlinked(..),
9 findModuleLinkable_maybe,
13 PersistentLinkerState{-abstractly!-}, emptyPLS,
22 import ByteCodeLink ( linkIModules, linkIExpr )
28 import CmStaticInfo ( GhciMode(..) )
29 import Outputable ( SDoc )
30 import Digraph ( SCC(..), flattenSCC )
32 import Module ( ModuleName )
35 import ErrUtils ( showPass )
36 import CmdLineOpts ( DynFlags(..) )
37 import Panic ( panic, GhcException(..) )
44 #include "HsVersions.h"
48 data PersistentLinkerState
49 = PersistentLinkerState {
53 -- Current global mapping from RdrNames to closure addresses
54 closure_env :: ClosureEnv,
56 -- the current global mapping from RdrNames of DataCons to
57 -- info table addresses.
58 -- When a new Unlinked is linked into the running image, or an existing
59 -- module in the image is replaced, the itbl_env must be updated
63 -- list of objects we've loaded (we'll need to unload them again
64 -- before re-loading the same module), together with the ClockTime
65 -- of the linkable they were loaded from.
66 objects_loaded :: [Linkable]
68 -- notionally here, but really lives in the C part of the linker:
69 -- object_symtab :: FiniteMap String Addr
71 dummy :: () -- sigh, can't have an empty record
77 = LinkOK PersistentLinkerState
78 | LinkErrs PersistentLinkerState [SDoc]
80 findModuleLinkable_maybe :: [Linkable] -> ModuleName -> Maybe Linkable
81 findModuleLinkable_maybe lis mod
82 = case [LM time nm us | LM time nm us <- lis, nm == mod] of
85 many -> pprPanic "findModuleLinkable" (ppr mod)
88 emptyPLS :: IO PersistentLinkerState
90 emptyPLS = return (PersistentLinkerState { closure_env = emptyFM,
92 objects_loaded = [] })
94 emptyPLS = return (PersistentLinkerState {})
98 updateClosureEnv :: PersistentLinkerState -> [(Name,HValue)]
99 -> IO PersistentLinkerState
100 updateClosureEnv pls new_bindings
101 = return pls{ closure_env = addListToFM (closure_env pls) new_bindings }
104 -----------------------------------------------------------------------------
105 -- Unloading old objects ready for a new compilation sweep.
107 -- The compilation manager provides us with a list of linkables that it
108 -- considers "stable", i.e. won't be recompiled this time around. For
109 -- each of the modules current linked in memory,
111 -- * if the linkable is stable (and it's the same one - the
112 -- user may have recompiled the module on the side), we keep it,
114 -- * otherwise, we unload it.
116 -- * we also implicitly unload all temporary bindings at this point.
120 -> [Linkable] -- stable linkables
121 -> PersistentLinkerState
122 -> IO PersistentLinkerState
124 unload Batch dflags linkables pls = return pls
127 unload Interactive dflags linkables pls
128 = do new_loaded <- filterM maybeUnload (objects_loaded pls)
129 let mods_retained = map linkableModName new_loaded
130 itbl_env' = filterNameMap mods_retained (itbl_env pls)
131 closure_env' = filterNameMap mods_retained (closure_env pls)
133 let verb = verbosity dflags
134 when (verb >= 3) $ do
135 hPutStrLn stderr (showSDoc
136 (text "CmLink.unload: retaining" <+> ppr mods_retained))
138 return pls{ objects_loaded = new_loaded,
139 itbl_env = itbl_env',
140 closure_env = closure_env' }
142 maybeUnload :: Linkable -> IO Bool
143 maybeUnload (LM time mod objs) = do
144 case findModuleLinkable_maybe linkables mod of
145 Nothing -> do unloadObjs; return False
146 Just l | linkableTime l /= time -> do unloadObjs; return False
147 | otherwise -> return True
149 unloadObjs = mapM unloadObj [ f | DotO f <- objs ]
151 unload Interactive dflags linkables pls = panic "CmLink.unload: no interpreter"
153 -----------------------------------------------------------------------------
156 link :: GhciMode -- interactive or batch
157 -> DynFlags -- dynamic flags
158 -> Bool -- attempt linking in batch mode?
160 -> PersistentLinkerState
163 -- For the moment, in the batch linker, we don't bother to tell doLink
164 -- which packages to link -- it just tries all that are available.
165 -- batch_attempt_linking should only be *looked at* in batch mode. It
166 -- should only be True if the upsweep was successful and someone
167 -- exports main, i.e., we have good reason to believe that linking
170 -- There will be (ToDo: are) two lists passed to link. These
173 -- 1. The list of all linkables in the current home package. This is
174 -- used by the batch linker to link the program, and by the interactive
175 -- linker to decide which modules from the previous link it can
177 -- 2. The list of modules on which we just called "compile". This list
178 -- is used by the interactive linker to decide which modules need
179 -- to be actually linked this time around (or unlinked and re-linked
180 -- if the module was recompiled).
182 link mode dflags batch_attempt_linking linkables pls1
183 = do let verb = verbosity dflags
184 when (verb >= 3) $ do
185 hPutStrLn stderr "CmLink.link: linkables are ..."
186 hPutStrLn stderr (showSDoc (vcat (map ppr linkables)))
187 res <- link' mode dflags batch_attempt_linking linkables pls1
189 hPutStrLn stderr "CmLink.link: done"
192 link' Batch dflags batch_attempt_linking linkables pls1
193 | batch_attempt_linking
194 = do let o_files = concatMap getOfiles linkables
196 hPutStrLn stderr "ghc: linking ..."
197 -- don't showPass in Batch mode; doLink will do that for us.
199 -- doLink only returns if it succeeds
202 = do when (verb >= 3) $ do
203 hPutStrLn stderr "CmLink.link(batch): upsweep (partially) failed OR"
204 hPutStrLn stderr " Main.main not exported; not linking."
207 verb = verbosity dflags
208 getOfiles (LM _ _ us) = map nameOfObject (filter isObject us)
210 link' Interactive dflags batch_attempt_linking linkables pls
211 = do showPass dflags "Linking"
212 let (objs, bcos) = partition (isObject.head.linkableUnlinked) linkables
213 linkObjs (objs ++ bcos) pls
214 -- get the objects first
216 ppLinkableSCC :: SCC Linkable -> SDoc
217 ppLinkableSCC = ppr . flattenSCC
219 filterModuleLinkables :: (ModuleName -> Bool) -> [Linkable] -> [Linkable]
220 filterModuleLinkables p [] = []
221 filterModuleLinkables p (li:lis)
223 LM _ modnm _ -> if p modnm then retain else dump
225 dump = filterModuleLinkables p lis
228 -----------------------------------------------------------------------------
229 -- Linker for interactive mode
232 linkObjs = panic "CmLink.linkObjs: no interpreter"
234 linkObjs [] pls = linkFinish pls []
235 linkObjs (l@(LM _ m uls) : ls) pls
236 | all isObject uls = do
237 if isLoaded l pls then linkObjs ls pls else do
238 let objs = [ file | DotO file <- uls ]
240 linkObjs ls pls{objects_loaded = l : objects_loaded pls}
241 | all isInterpretable uls = linkInterpretedCode (l:ls) [] pls
242 | otherwise = invalidLinkable
244 isLoaded :: Linkable -> PersistentLinkerState -> Bool
246 case findModuleLinkable_maybe (objects_loaded pls) (linkableModName l) of
248 Just m -> linkableTime l == linkableTime m
250 linkInterpretedCode [] ul_trees pls = linkFinish pls ul_trees
251 linkInterpretedCode (l@(LM _ m uls) : ls) ul_trees pls
252 | all isInterpretable uls =
253 if isLoaded l pls then linkInterpretedCode ls ul_trees pls else
254 linkInterpretedCode ls (uls++ul_trees)
255 pls{objects_loaded = l : objects_loaded pls}
257 = throwDyn (OtherError
258 "can't link object code that depends on interpreted code")
259 | otherwise = invalidLinkable
261 invalidLinkable = panic "CmLink: linkable doesn't contain entirely objects or interpreted code"
264 -- link all the interpreted code in one go.
265 linkFinish pls ul_bcos = do
268 let stuff = [ (bcos,itbls) | BCOs bcos itbls <- ul_bcos ]
270 (ibinds, new_itbl_env, new_closure_env) <-
271 linkIModules (itbl_env pls) (closure_env pls) stuff
273 let new_pls = pls { closure_env = new_closure_env,
274 itbl_env = new_itbl_env
276 return (LinkOK new_pls)
278 linkExpr :: PersistentLinkerState -> UnlinkedBCOExpr -> IO HValue
279 linkExpr PersistentLinkerState{ itbl_env = ie, closure_env = ce } bcos
280 = linkIExpr ie ce bcos