[project @ 2000-10-12 13:44:59 by simonpj]
[ghc-hetmet.git] / ghc / compiler / ghci / 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(..), 
8                 filterModuleLinkables, 
9                 modname_of_linkable, is_package_linkable,
10                 LinkResult(..),
11                 link, 
12                 PLS{-abstractly!-}, emptyPLS )
13 where
14
15 import StgInterp        ( linkIModules, ClosureEnv, ItblEnv )
16 import Linker
17
18 import CmStaticInfo     ( PCI )
19 import CmFind           ( Path, PkgName )
20 import InterpSyn        ( UnlinkedIBind, HValue, binder )
21 import Module           ( Module )
22 import Outputable       ( SDoc )
23 import FiniteMap        ( FiniteMap, emptyFM )
24 import RdrName          ( RdrName )
25 import Digraph          ( SCC(..) )
26 import Addr             ( Addr )
27 import Outputable
28 import Panic            ( panic )
29
30 #include "HsVersions.h"
31 \end{code}
32
33 \begin{code}
34 data PLS 
35    = MkPLS {
36         closure_env :: ClosureEnv,
37         itbl_env    :: ItblEnv
38         -- notionally here, but really lives in the C part of the linker:
39         --            object_symtab :: FiniteMap String Addr
40      }
41
42 data LinkResult 
43    = LinkOK   PLS
44    | LinkErrs PLS [SDoc]
45
46 data Unlinked
47    = DotO Path
48    | DotA Path
49    | DotDLL Path
50    | Trees [UnlinkedIBind]      -- bunch of interpretable bindings
51
52 instance Outputable Unlinked where
53    ppr (DotO path)   = text "DotO" <+> text path
54    ppr (DotA path)   = text "DotA" <+> text path
55    ppr (DotDLL path) = text "DotDLL" <+> text path
56    ppr (Trees binds) = text "Trees" <+> ppr (map binder binds)
57
58
59 isObject (DotO _) = True
60 isObject (DotA _) = True
61 isObject (DotDLL _) = True
62 isObject _ = False
63
64 isInterpretable (Trees _) = True
65 isInterpretable _ = False
66
67 data Linkable
68    = LM {-should be:Module-} String{- == ModName-} [Unlinked]
69    | LP PkgName
70
71 instance Outputable Linkable where
72    ppr (LM mod_nm unlinkeds) = text "LinkableM" <+> text mod_nm <+> ppr unlinkeds
73    ppr (LP package_nm)       = text "LinkableP" <+> text package_nm
74
75 emptyPLS :: IO PLS
76 emptyPLS = return (MkPLS { closure_env = emptyFM, 
77                            itbl_env    = emptyFM })
78 \end{code}
79
80 \begin{code}
81 link :: PCI -> [SCC Linkable] -> PLS -> IO LinkResult
82
83 #ifndef GHCI_NOTYET
84 --link = panic "CmLink.link: not implemented"
85 link pci groups pls1
86    = do putStrLn "Hello from the Linker!"
87         putStrLn (showSDoc (vcat (map ppLinkableSCC groups)))
88         putStrLn "Bye-bye from the Linker!"
89         return (LinkOK pls1)
90
91 ppLinkableSCC :: SCC Linkable -> SDoc
92 ppLinkableSCC (CyclicSCC xs) = ppr xs
93 ppLinkableSCC (AcyclicSCC x) = ppr [x]
94
95
96 #else
97 link pci [] pls = return (LinkOK pls)
98 link pci (group:groups) pls = do
99    -- the group is either all objects or all interpretable, for now
100    if all isObject group
101         then do mapM loadObj [ file | DotO file <- group ]
102                 resolveObjs
103                 link pci groups pls
104     else if all isInterpretable group
105         then do (new_closure_env, new_itbl_env) <-
106                    linkIModules (closure_env pls)
107                                 (itbl_env pls)
108                                 [ trees | Trees trees <- group ]
109                 link pci groups MkPLS{closure_env=new_closure_env,
110                                       itbl_env=new_itbl_env}
111     else
112         return (LinkErrs pls (ptext SLIT("linker: group must contain all objects or all interpreted modules")))
113 #endif
114
115 modname_of_linkable (LM nm _) = nm
116 modname_of_linkable (LP _)    = panic "modname_of_linkable: package"
117
118 is_package_linkable (LP _)   = True
119 is_package_linkable (LM _ _) = False
120
121 filterModuleLinkables :: (String{- ==ModName-} -> Bool) 
122                       -> [Linkable] 
123                       -> [Linkable]
124 filterModuleLinkables p [] = []
125 filterModuleLinkables p (li:lis)
126    = case li of
127         LP _       -> retain
128         LM modnm _ -> if p modnm then retain else dump
129      where
130         dump   = filterModuleLinkables p lis
131         retain = li : dump
132 \end{code}