[project @ 2000-11-08 14:52:06 by simonpj]
[ghc-hetmet.git] / ghc / compiler / compMan / CmLink.lhs
1 %
2 % (c) The University of Glasgow, 2000
3 %
4 \section[CmLink]{Linker for GHCI}
5
6 \begin{code}
7 module CmLink ( Linkable(..),  Unlinked(..),
8                 filterModuleLinkables, 
9                 modname_of_linkable, is_package_linkable,
10                 LinkResult(..),
11                 link, 
12                 PersistentLinkerState{-abstractly!-}, emptyPLS )
13 where
14
15
16 import Interpreter
17 import CmStaticInfo     ( PackageConfigInfo )
18 import Module           ( ModuleName, PackageName )
19 import Outputable       ( SDoc )
20 import Digraph          ( SCC(..), flattenSCC )
21 import Outputable
22 import Panic            ( panic )
23
24 #include "HsVersions.h"
25 \end{code}
26
27 \begin{code}
28 data PersistentLinkerState 
29    = PersistentLinkerState {
30
31 #ifdef GHCI
32         -- Current global mapping from RdrNames to closure addresses
33         closure_env :: ClosureEnv,
34
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
39         -- appropriately.
40         itbl_env    :: ItblEnv
41
42         -- notionally here, but really lives in the C part of the linker:
43         --            object_symtab :: FiniteMap String Addr
44 #else
45         dummy :: ()     --  sigh, can't have an empty record
46 #endif
47
48      }
49
50 data LinkResult 
51    = LinkOK   PersistentLinkerState
52    | LinkErrs PersistentLinkerState [SDoc]
53
54 data Unlinked
55    = DotO FilePath
56    | DotA FilePath
57    | DotDLL FilePath
58    | Trees [UnlinkedIBind] ItblEnv  -- bunch of interpretable bindings, +
59                                     -- a mapping from DataCons to their itbls
60
61 instance Outputable Unlinked where
62    ppr (DotO path)   = text "DotO" <+> text path
63    ppr (DotA path)   = text "DotA" <+> text path
64    ppr (DotDLL path) = text "DotDLL" <+> text path
65    ppr (Trees binds _) = text "Trees" <+> ppr binds
66
67
68 isObject (DotO _) = True
69 isObject (DotA _) = True
70 isObject (DotDLL _) = True
71 isObject _ = False
72
73 isInterpretable (Trees _ _) = True
74 isInterpretable _ = False
75
76 data Linkable
77    = LM ModuleName [Unlinked]
78    | LP PackageName
79
80 instance Outputable Linkable where
81    ppr (LM mod_nm unlinkeds) = text "LinkableM" <+> ppr mod_nm <+> ppr unlinkeds
82    ppr (LP package_nm)       = text "LinkableP" <+> ptext package_nm
83
84 emptyPLS :: IO PersistentLinkerState
85 emptyPLS = return (PersistentLinkerState { closure_env = emptyClosureEnv, 
86                                            itbl_env    = emptyItblEnv })
87 \end{code}
88
89 \begin{code}
90 link :: PackageConfigInfo 
91      -> [SCC Linkable] 
92      -> PersistentLinkerState 
93      -> IO LinkResult
94
95 #ifndef GHCI_NOTYET
96 --link = panic "CmLink.link: not implemented"
97 link pci groups pls1
98    = do putStrLn "Hello from the Linker!"
99         putStrLn (showSDoc (vcat (map ppLinkableSCC groups)))
100         putStrLn "Bye-bye from the Linker!"
101         return (LinkOK pls1)
102
103 ppLinkableSCC :: SCC Linkable -> SDoc
104 ppLinkableSCC = ppr . flattenSCC
105
106 #else
107
108
109 link pci [] pls = return (LinkOK pls)
110 link pci (groupSCC:groups) pls = do
111    let group = flattenSCC groupSCC
112    -- the group is either all objects or all interpretable, for now
113    if all isObject group
114         then do mapM loadObj [ file | DotO file <- group ]
115                 resolveObjs
116                 link pci groups pls
117     else if all isInterpretable group
118         then do (new_closure_env, new_itbl_env) <-
119                    linkIModules (closure_env pls)
120                                 (itbl_env pls)
121                                 [ trees | Trees trees <- group ]
122                 link pci groups (PersistentLinkerState{
123                                    closure_env=new_closure_env,
124                                    itbl_env=new_itbl_env})
125     else
126         return (LinkErrs pls (ptext SLIT("linker: group must contain all objects or all interpreted modules")))
127 #endif
128
129
130 modname_of_linkable (LM nm _) = nm
131 modname_of_linkable (LP _)    = panic "modname_of_linkable: package"
132
133 is_package_linkable (LP _)   = True
134 is_package_linkable (LM _ _) = False
135
136 filterModuleLinkables :: (ModuleName -> Bool) 
137                       -> [Linkable] 
138                       -> [Linkable]
139 filterModuleLinkables p [] = []
140 filterModuleLinkables p (li:lis)
141    = case li of
142         LP _       -> retain
143         LM modnm _ -> if p modnm then retain else dump
144      where
145         dump   = filterModuleLinkables p lis
146         retain = li : dump
147 \end{code}