2 % (c) The University of Glasgow, 2000
4 \section[CmLink]{Linker for GHCI}
7 module CmLink ( Linkable(..), Unlinked(..),
10 modname_of_linkable, is_package_linkable,
14 PersistentLinkerState{-abstractly!-}, emptyPLS,
22 import CmStaticInfo ( GhciMode(..) )
23 import Outputable ( SDoc )
24 import Digraph ( SCC(..), flattenSCC )
26 import Module ( ModuleName, PackageName )
30 import Panic ( panic )
35 #include "HsVersions.h"
39 data PersistentLinkerState
40 = PersistentLinkerState {
43 -- Current global mapping from RdrNames to closure addresses
44 closure_env :: ClosureEnv,
46 -- the current global mapping from RdrNames of DataCons to
47 -- info table addresses.
48 -- When a new Unlinked is linked into the running image, or an existing
49 -- module in the image is replaced, the itbl_env must be updated
53 -- notionally here, but really lives in the C part of the linker:
54 -- object_symtab :: FiniteMap String Addr
56 dummy :: () -- sigh, can't have an empty record
62 = LinkOK PersistentLinkerState
63 | LinkErrs PersistentLinkerState [SDoc]
65 findModuleLinkable :: [Linkable] -> ModuleName -> Linkable
66 findModuleLinkable lis mod
67 = case [LM nm us | LM nm us <- lis, nm == mod] of
69 other -> pprPanic "findModuleLinkable" (ppr mod)
72 emptyPLS :: IO PersistentLinkerState
74 emptyPLS = return (PersistentLinkerState { closure_env = emptyFM,
77 emptyPLS = return (PersistentLinkerState {})
82 link :: GhciMode -- interactive or batch
83 -> Bool -- attempt linking in batch mode?
84 -> [Linkable] -- only contains LMs, not LPs
85 -> PersistentLinkerState
88 -- For the moment, in the batch linker, we don't bother to tell doLink
89 -- which packages to link -- it just tries all that are available.
90 -- batch_attempt_linking should only be *looked at* in batch mode. It
91 -- should only be True if the upsweep was successful and someone
92 -- exports main, i.e., we have good reason to believe that linking
95 -- There will be (ToDo: are) two lists passed to link. These
98 -- 1. The list of all linkables in the current home package. This is
99 -- used by the batch linker to link the program, and by the interactive
100 -- linker to decide which modules from the previous link it can
102 -- 2. The list of modules on which we just called "compile". This list
103 -- is used by the interactive linker to decide which modules need
104 -- to be actually linked this time around (or unlinked and re-linked
105 -- if the module was recompiled).
107 link mode batch_attempt_linking linkables pls1
108 = do hPutStrLn stderr "CmLink.link: linkables are ..."
109 hPutStrLn stderr (showSDoc (vcat (map ppr linkables)))
110 res <- link' mode batch_attempt_linking linkables pls1
111 hPutStrLn stderr "CmLink.link: done"
114 link' Batch batch_attempt_linking linkables pls1
115 | batch_attempt_linking
116 = do let o_files = concatMap getOfiles linkables
118 -- doLink only returns if it succeeds
121 = do hPutStrLn stderr "CmLink.link(batch): upsweep (partially?) failed OR main not exported;"
122 hPutStrLn stderr " -- not doing linking"
125 getOfiles (LP _) = panic "CmLink.link(getOfiles): shouldn't get package linkables"
126 getOfiles (LM _ us) = map nameOfObject (filter isObject us)
128 link' Interactive batch_attempt_linking linkables pls1
129 = linkObjs linkables pls1
132 ppLinkableSCC :: SCC Linkable -> SDoc
133 ppLinkableSCC = ppr . flattenSCC
136 modname_of_linkable (LM nm _) = nm
137 modname_of_linkable (LP _) = panic "modname_of_linkable: package"
139 is_package_linkable (LP _) = True
140 is_package_linkable (LM _ _) = False
142 filterModuleLinkables :: (ModuleName -> Bool)
145 filterModuleLinkables p [] = []
146 filterModuleLinkables p (li:lis)
149 LM modnm _ -> if p modnm then retain else dump
151 dump = filterModuleLinkables p lis
154 -----------------------------------------------------------------------------
155 -- Linker for interactive mode
158 linkObjs = panic "CmLink.linkObjs: no interpreter"
159 unload = panic "CmLink.unload: no interpreter"
160 lookupClosure = panic "CmLink.lookupClosure: no interpreter"
162 linkObjs [] pls = linkFinish pls [] []
163 linkObjs (l@(LM _ uls) : ls) pls
164 | all isObject uls = do
165 mapM_ loadObj [ file | DotO file <- uls ]
167 | all isInterpretable uls = linkInterpretedCode (l:ls) [] [] pls
168 | otherwise = invalidLinkable
170 throwDyn (OtherError "CmLink.linkObjs: found package linkable")
173 linkInterpretedCode [] mods ul_trees pls = linkFinish pls mods ul_trees
174 linkInterpretedCode (LM m uls : ls) mods ul_trees pls
175 | all isInterpretable uls =
176 linkInterpretedCode ls (m:mods) (uls++ul_trees) pls
179 = throwDyn (OtherError "can't link object code that depends on interpreted code")
180 | otherwise = invalidLinkable
181 linkInterpretedCode _ _ _ pls =
182 throwDyn (OtherError "CmLink.linkInterpretedCode: found package linkable")
184 invalidLinkable = throwDyn (OtherError "linkable doesn't contain entirely objects interpreted code")
187 -- link all the interpreted code in one go. We first remove from the
188 -- various environments any previous versions of these modules.
189 linkFinish pls mods ul_trees = do
191 let itbl_env' = filterRdrNameEnv mods (itbl_env pls)
192 closure_env' = filterRdrNameEnv mods (closure_env pls)
193 stuff = [ (trees,itbls) | Trees trees itbls <- ul_trees ]
195 (ibinds, new_itbl_env, new_closure_env) <-
196 linkIModules itbl_env' closure_env' stuff
198 let new_pls = PersistentLinkerState {
199 closure_env = new_closure_env,
200 itbl_env = new_itbl_env
202 putStrLn (showSDoc (vcat (map ppr (keysFM new_closure_env))))
203 return (LinkOK new_pls)
205 -- purge the current "linked image"
206 unload :: PersistentLinkerState -> IO PersistentLinkerState
207 unload pls = return pls{ closure_env = emptyFM, itbl_env = emptyFM }
209 linkExpr :: PersistentLinkerState -> UnlinkedIExpr -> IO HValue
210 linkExpr PersistentLinkerState{ itbl_env = ie, closure_env = ce } expr
211 = iExprToHValue ie ce expr