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 )
16 import CmStaticInfo ( PackageConfigInfo )
17 import Module ( ModuleName, PackageName )
18 import InterpSyn ( UnlinkedIBind, HValue, binder )
19 import Module ( Module )
20 import Outputable ( SDoc )
21 import FiniteMap ( emptyFM )
22 import Digraph ( SCC(..), flattenSCC )
24 import Panic ( panic )
26 #include "HsVersions.h"
30 data PersistentLinkerState
31 = PersistentLinkerState {
32 -- Current global mapping from RdrNames to closure addresses
33 closure_env :: ClosureEnv,
35 -- the current global mapping from RdrNames of DataCons to
36 -- info table addresses.
37 -- When a new Unlinked is linked into the running image, or an existing
38 -- module in the image is replaced, the itbl_env must be updated
42 -- notionally here, but really lives in the C part of the linker:
43 -- object_symtab :: FiniteMap String Addr
47 = LinkOK PersistentLinkerState
48 | LinkErrs PersistentLinkerState [SDoc]
54 | Trees [UnlinkedIBind] ItblEnv -- bunch of interpretable bindings, +
55 -- a mapping from DataCons to their itbls
57 instance Outputable Unlinked where
58 ppr (DotO path) = text "DotO" <+> text path
59 ppr (DotA path) = text "DotA" <+> text path
60 ppr (DotDLL path) = text "DotDLL" <+> text path
61 ppr (Trees binds _) = text "Trees" <+> ppr (map binder binds)
64 isObject (DotO _) = True
65 isObject (DotA _) = True
66 isObject (DotDLL _) = True
69 isInterpretable (Trees _ _) = True
70 isInterpretable _ = False
73 = LM ModuleName [Unlinked]
76 instance Outputable Linkable where
77 ppr (LM mod_nm unlinkeds) = text "LinkableM" <+> ppr mod_nm <+> ppr unlinkeds
78 ppr (LP package_nm) = text "LinkableP" <+> ptext package_nm
80 emptyPLS :: IO PersistentLinkerState
81 emptyPLS = return (PersistentLinkerState { closure_env = emptyFM,
86 link :: PackageConfigInfo
88 -> PersistentLinkerState
92 --link = panic "CmLink.link: not implemented"
94 = do putStrLn "Hello from the Linker!"
95 putStrLn (showSDoc (vcat (map ppLinkableSCC groups)))
96 putStrLn "Bye-bye from the Linker!"
99 ppLinkableSCC :: SCC Linkable -> SDoc
100 ppLinkableSCC = ppr . flattenSCC
105 link pci [] pls = return (LinkOK pls)
106 link pci (groupSCC:groups) pls = do
107 let group = flattenSCC groupSCC
108 -- the group is either all objects or all interpretable, for now
109 if all isObject group
110 then do mapM loadObj [ file | DotO file <- group ]
113 else if all isInterpretable group
114 then do (new_closure_env, new_itbl_env) <-
115 linkIModules (closure_env pls)
117 [ trees | Trees trees <- group ]
118 link pci groups (PersistentLinkerState{
119 closure_env=new_closure_env,
120 itbl_env=new_itbl_env})
122 return (LinkErrs pls (ptext SLIT("linker: group must contain all objects or all interpreted modules")))
126 modname_of_linkable (LM nm _) = nm
127 modname_of_linkable (LP _) = panic "modname_of_linkable: package"
129 is_package_linkable (LP _) = True
130 is_package_linkable (LM _ _) = False
132 filterModuleLinkables :: (ModuleName -> Bool)
135 filterModuleLinkables p [] = []
136 filterModuleLinkables p (li:lis)
139 LM modnm _ -> if p modnm then retain else dump
141 dump = filterModuleLinkables p lis