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,
23 import CmStaticInfo ( GhciMode(..) )
24 import Outputable ( SDoc )
25 import Digraph ( SCC(..), flattenSCC )
26 import Module ( ModuleName )
29 import ErrUtils ( showPass )
30 import CmdLineOpts ( DynFlags(..) )
31 import Panic ( panic, GhcException(..) )
38 #include "HsVersions.h"
42 data PersistentLinkerState
43 = PersistentLinkerState {
47 -- Current global mapping from RdrNames to closure addresses
48 closure_env :: ClosureEnv,
50 -- the current global mapping from RdrNames of DataCons to
51 -- info table addresses.
52 -- When a new Unlinked is linked into the running image, or an existing
53 -- module in the image is replaced, the itbl_env must be updated
57 -- list of objects we've loaded (we'll need to unload them again
58 -- before re-loading the same module), together with the ClockTime
59 -- of the linkable they were loaded from.
60 objects_loaded :: [Linkable]
62 -- notionally here, but really lives in the C part of the linker:
63 -- object_symtab :: FiniteMap String Addr
65 dummy :: () -- sigh, can't have an empty record
71 = LinkOK PersistentLinkerState
72 | LinkErrs PersistentLinkerState [SDoc]
74 findModuleLinkable_maybe :: [Linkable] -> ModuleName -> Maybe Linkable
75 findModuleLinkable_maybe lis mod
76 = case [LM time nm us | LM time nm us <- lis, nm == mod] of
79 many -> pprPanic "findModuleLinkable" (ppr mod)
82 emptyPLS :: IO PersistentLinkerState
84 emptyPLS = return (PersistentLinkerState { closure_env = emptyFM,
86 objects_loaded = [] })
88 emptyPLS = return (PersistentLinkerState {})
91 -----------------------------------------------------------------------------
92 -- Unloading old objects ready for a new compilation sweep.
94 -- The compilation manager provides us with a list of linkables that it
95 -- considers "stable", i.e. won't be recompiled this time around. For
96 -- each of the modules current linked in memory,
98 -- * if the linkable is stable (and it's the same one - the
99 -- user may have recompiled the module on the side), we keep it,
101 -- * otherwise, we unload it.
106 -> [Linkable] -- stable linkables
107 -> PersistentLinkerState
108 -> IO PersistentLinkerState
110 unload Batch dflags linkables pls = return pls
113 unload Interactive dflags linkables pls
114 = do new_loaded <- filterM maybeUnload (objects_loaded pls)
115 let mods_retained = map linkableModName new_loaded
116 itbl_env' = filterNameMap mods_retained (itbl_env pls)
117 closure_env' = filterNameMap mods_retained (closure_env pls)
119 let verb = verbosity dflags
120 when (verb >= 3) $ do
121 hPutStrLn stderr (showSDoc
122 (text "CmLink.unload: retaining" <+> ppr mods_retained))
124 return pls{ objects_loaded = new_loaded,
125 itbl_env = itbl_env',
126 closure_env = closure_env' }
128 maybeUnload :: Linkable -> IO Bool
129 maybeUnload (LM time mod objs) = do
130 case findModuleLinkable_maybe linkables mod of
131 Nothing -> do unloadObjs; return False
132 Just l | linkableTime l /= time -> do unloadObjs; return False
133 | otherwise -> return True
135 unloadObjs = mapM unloadObj [ f | DotO f <- objs ]
137 unload Interactive dflags linkables pls = panic "CmLink.unload: no interpreter"
139 -----------------------------------------------------------------------------
142 link :: GhciMode -- interactive or batch
143 -> DynFlags -- dynamic flags
144 -> Bool -- attempt linking in batch mode?
146 -> PersistentLinkerState
149 -- For the moment, in the batch linker, we don't bother to tell doLink
150 -- which packages to link -- it just tries all that are available.
151 -- batch_attempt_linking should only be *looked at* in batch mode. It
152 -- should only be True if the upsweep was successful and someone
153 -- exports main, i.e., we have good reason to believe that linking
156 -- There will be (ToDo: are) two lists passed to link. These
159 -- 1. The list of all linkables in the current home package. This is
160 -- used by the batch linker to link the program, and by the interactive
161 -- linker to decide which modules from the previous link it can
163 -- 2. The list of modules on which we just called "compile". This list
164 -- is used by the interactive linker to decide which modules need
165 -- to be actually linked this time around (or unlinked and re-linked
166 -- if the module was recompiled).
168 link mode dflags batch_attempt_linking linkables pls1
169 = do let verb = verbosity dflags
170 when (verb >= 3) $ do
171 hPutStrLn stderr "CmLink.link: linkables are ..."
172 hPutStrLn stderr (showSDoc (vcat (map ppr linkables)))
173 res <- link' mode dflags batch_attempt_linking linkables pls1
175 hPutStrLn stderr "CmLink.link: done"
178 link' Batch dflags batch_attempt_linking linkables pls1
179 | batch_attempt_linking
180 = do let o_files = concatMap getOfiles linkables
182 hPutStrLn stderr "ghc: linking ..."
183 -- don't showPass in Batch mode; doLink will do that for us.
185 -- doLink only returns if it succeeds
188 = do when (verb >= 3) $ do
189 hPutStrLn stderr "CmLink.link(batch): upsweep (partially) failed OR"
190 hPutStrLn stderr " Main.main not exported; not linking."
193 verb = verbosity dflags
194 getOfiles (LM _ _ us) = map nameOfObject (filter isObject us)
196 link' Interactive dflags batch_attempt_linking linkables pls
197 = do showPass dflags "Linking"
198 let (objs, bcos) = partition (isObject.head.linkableUnlinked) linkables
199 linkObjs (objs ++ bcos) pls
200 -- get the objects first
202 ppLinkableSCC :: SCC Linkable -> SDoc
203 ppLinkableSCC = ppr . flattenSCC
205 filterModuleLinkables :: (ModuleName -> Bool) -> [Linkable] -> [Linkable]
206 filterModuleLinkables p [] = []
207 filterModuleLinkables p (li:lis)
209 LM _ modnm _ -> if p modnm then retain else dump
211 dump = filterModuleLinkables p lis
214 -----------------------------------------------------------------------------
215 -- Linker for interactive mode
218 linkObjs = panic "CmLink.linkObjs: no interpreter"
219 lookupClosure = panic "CmLink.lookupClosure: no interpreter"
221 linkObjs [] pls = linkFinish pls []
222 linkObjs (l@(LM _ m uls) : ls) pls
223 | all isObject uls = do
224 if isLoaded l pls then linkObjs ls pls else do
225 let objs = [ file | DotO file <- uls ]
227 linkObjs ls pls{objects_loaded = l : objects_loaded pls}
228 | all isInterpretable uls = linkInterpretedCode (l:ls) [] pls
229 | otherwise = invalidLinkable
231 isLoaded :: Linkable -> PersistentLinkerState -> Bool
233 case findModuleLinkable_maybe (objects_loaded pls) (linkableModName l) of
235 Just m -> linkableTime l == linkableTime m
237 linkInterpretedCode [] ul_trees pls = linkFinish pls ul_trees
238 linkInterpretedCode (l@(LM _ m uls) : ls) ul_trees pls
239 | all isInterpretable uls =
240 if isLoaded l pls then linkInterpretedCode ls ul_trees pls else
241 linkInterpretedCode ls (uls++ul_trees)
242 pls{objects_loaded = l : objects_loaded pls}
244 = throwDyn (OtherError
245 "can't link object code that depends on interpreted code")
246 | otherwise = invalidLinkable
248 invalidLinkable = panic "CmLink: linkable doesn't contain entirely objects or interpreted code"
251 -- link all the interpreted code in one go.
252 linkFinish pls ul_bcos = do
255 let stuff = [ (bcos,itbls) | BCOs bcos itbls <- ul_bcos ]
257 (ibinds, new_itbl_env, new_closure_env) <-
258 linkIModules (itbl_env pls) (closure_env pls) stuff
260 let new_pls = pls { closure_env = new_closure_env,
261 itbl_env = new_itbl_env
263 return (LinkOK new_pls)
265 linkExpr :: PersistentLinkerState -> UnlinkedBCOExpr -> IO HValue
266 linkExpr PersistentLinkerState{ itbl_env = ie, closure_env = ce } bcos
267 = linkIExpr ie ce bcos