2 % (c) The University of Glasgow, 2000
4 \section[CmLink]{Linker for GHCI}
7 module CmLink ( Linkable(..), Unlinked(..),
9 modname_of_linkable, is_package_linkable,
12 PersistentLinkerState{-abstractly!-}, emptyPLS )
15 import StgInterp ( linkIModules, ClosureEnv, ItblEnv )
16 import Linker ( loadObj, resolveObjs )
17 import CmStaticInfo ( PackageConfigInfo )
18 import Module ( ModuleName, PackageName )
19 import InterpSyn ( UnlinkedIBind, HValue, binder )
20 import Module ( Module )
21 import Outputable ( SDoc )
22 import FiniteMap ( emptyFM )
23 import Digraph ( SCC(..), flattenSCC )
25 import Panic ( panic )
27 #include "HsVersions.h"
31 data PersistentLinkerState
32 = PersistentLinkerState {
33 -- Current global mapping from RdrNames to closure addresses
34 closure_env :: ClosureEnv,
36 -- the current global mapping from RdrNames of DataCons to
37 -- info table addresses.
38 -- When a new Unlinked is linked into the running image, or an existing
39 -- module in the image is replaced, the itbl_env must be updated
43 -- notionally here, but really lives in the C part of the linker:
44 -- object_symtab :: FiniteMap String Addr
48 = LinkOK PersistentLinkerState
49 | LinkErrs PersistentLinkerState [SDoc]
55 | Trees [UnlinkedIBind] ItblEnv -- bunch of interpretable bindings, +
56 -- a mapping from DataCons to their itbls
58 instance Outputable Unlinked where
59 ppr (DotO path) = text "DotO" <+> text path
60 ppr (DotA path) = text "DotA" <+> text path
61 ppr (DotDLL path) = text "DotDLL" <+> text path
62 ppr (Trees binds _) = text "Trees" <+> ppr (map binder binds)
65 isObject (DotO _) = True
66 isObject (DotA _) = True
67 isObject (DotDLL _) = True
70 isInterpretable (Trees _ _) = True
71 isInterpretable _ = False
74 = LM ModuleName [Unlinked]
77 instance Outputable Linkable where
78 ppr (LM mod_nm unlinkeds) = text "LinkableM" <+> ppr mod_nm <+> ppr unlinkeds
79 ppr (LP package_nm) = text "LinkableP" <+> ptext package_nm
81 emptyPLS :: IO PersistentLinkerState
82 emptyPLS = return (PersistentLinkerState { closure_env = emptyFM,
87 link :: PackageConfigInfo
89 -> PersistentLinkerState
93 --link = panic "CmLink.link: not implemented"
95 = do putStrLn "Hello from the Linker!"
96 putStrLn (showSDoc (vcat (map ppLinkableSCC groups)))
97 putStrLn "Bye-bye from the Linker!"
100 ppLinkableSCC :: SCC Linkable -> SDoc
101 ppLinkableSCC = ppr . flattenSCC
106 link pci [] pls = return (LinkOK pls)
107 link pci (groupSCC:groups) pls = do
108 let group = flattenSCC groupSCC
109 -- the group is either all objects or all interpretable, for now
110 if all isObject group
111 then do mapM loadObj [ file | DotO file <- group ]
114 else if all isInterpretable group
115 then do (new_closure_env, new_itbl_env) <-
116 linkIModules (closure_env pls)
118 [ trees | Trees trees <- group ]
119 link pci groups (PersistentLinkerState{
120 closure_env=new_closure_env,
121 itbl_env=new_itbl_env})
123 return (LinkErrs pls (ptext SLIT("linker: group must contain all objects or all interpreted modules")))
127 modname_of_linkable (LM nm _) = nm
128 modname_of_linkable (LP _) = panic "modname_of_linkable: package"
130 is_package_linkable (LP _) = True
131 is_package_linkable (LM _ _) = False
133 filterModuleLinkables :: (ModuleName -> Bool)
136 filterModuleLinkables p [] = []
137 filterModuleLinkables p (li:lis)
140 LM modnm _ -> if p modnm then retain else dump
142 dump = filterModuleLinkables p lis