2 % (c) The University of Glasgow 2000
4 \section[ByteCodeLink]{Bytecode assembler and linker}
8 {-# OPTIONS -optc-DNON_POSIX_SOURCE #-}
12 ClosureEnv, emptyClosureEnv, extendClosureEnv,
13 linkBCO, lookupStaticPtr
16 #include "HsVersions.h"
18 import ByteCodeItbls ( ItblEnv, ItblPtr )
19 import ByteCodeAsm ( UnlinkedBCO(..), BCOPtr(..), sizeSS, ssElts )
20 import ObjLink ( lookupSymbol )
22 import Name ( Name, nameModule, nameOccName )
24 import OccName ( occNameFS )
25 import PrimOp ( PrimOp, primOpOcc )
27 import PackageConfig ( mainPackageId, packageIdFS )
28 import FastString ( FastString(..), unpackFS, zEncodeFS )
29 import Panic ( GhcException(..) )
32 import Name ( isExternalName )
37 import GHC.Word ( Word(..) )
39 import Data.Array.Base
40 import GHC.Arr ( STArray(..) )
42 import Control.Exception ( throwDyn )
43 import Control.Monad ( zipWithM )
44 import Control.Monad.ST ( stToIO )
46 import GHC.Exts ( BCO#, newBCO#, unsafeCoerce#, Int#,
47 ByteArray#, Array#, addrToHValue#, mkApUpd0# )
49 import GHC.Arr ( Array(..) )
50 import GHC.IOBase ( IO(..) )
51 import GHC.Ptr ( Ptr(..) )
52 import GHC.Base ( writeArray#, RealWorld, Int(..) )
56 %************************************************************************
58 \subsection{Linking interpretables into something we can run}
60 %************************************************************************
63 type ClosureEnv = NameEnv (Name, HValue)
64 newtype HValue = HValue (forall a . a)
66 emptyClosureEnv = emptyNameEnv
68 extendClosureEnv :: ClosureEnv -> [(Name,HValue)] -> ClosureEnv
69 extendClosureEnv cl_env pairs
70 = extendNameEnvList cl_env [ (n, (n,v)) | (n,v) <- pairs]
74 %************************************************************************
76 \subsection{Linking interpretables into something we can run}
78 %************************************************************************
82 data BCO# = BCO# ByteArray# -- instrs :: Array Word16#
83 ByteArray# -- literals :: Array Word32#
84 PtrArray# -- ptrs :: Array HValue
85 ByteArray# -- itbls :: Array Addr#
88 linkBCO :: ItblEnv -> ClosureEnv -> UnlinkedBCO -> IO HValue
90 = do BCO bco# <- linkBCO' ie ce ul_bco
91 -- SDM: Why do we need mkApUpd0 here? I *think* it's because
92 -- otherwise top-level interpreted CAFs don't get updated
93 -- after evaluation. A top-level BCO will evaluate itself and
94 -- return its value when entered, but it won't update itself.
95 -- Wrapping the BCO in an AP_UPD thunk will take care of the
98 -- Update: the above is true, but now we also have extra invariants:
99 -- (a) An AP thunk *must* point directly to a BCO
100 -- (b) A zero-arity BCO *must* be wrapped in an AP thunk
101 -- (c) An AP is always fully saturated, so we *can't* wrap
102 -- non-zero arity BCOs in an AP thunk.
104 if (unlinkedBCOArity ul_bco > 0)
105 then return (unsafeCoerce# bco#)
106 else case mkApUpd0# bco# of { (# final_bco #) -> return final_bco }
109 linkBCO' :: ItblEnv -> ClosureEnv -> UnlinkedBCO -> IO BCO
110 linkBCO' ie ce (UnlinkedBCO nm arity insns_barr bitmap literalsSS ptrsSS itblsSS)
111 -- Raises an IO exception on failure
112 = do let literals = ssElts literalsSS
114 itbls = ssElts itblsSS
116 linked_itbls <- mapM (lookupIE ie) itbls
117 linked_literals <- mapM lookupLiteral literals
119 let n_literals = sizeSS literalsSS
120 n_ptrs = sizeSS ptrsSS
121 n_itbls = sizeSS itblsSS
123 ptrs_arr <- mkPtrsArray ie ce n_ptrs ptrs
126 ptrs_parr = case ptrs_arr of Array lo hi parr -> parr
128 itbls_arr = listArray (0, n_itbls-1) linked_itbls
129 :: UArray Int ItblPtr
130 itbls_barr = case itbls_arr of UArray lo hi barr -> barr
132 literals_arr = listArray (0, n_literals-1) linked_literals
134 literals_barr = case literals_arr of UArray lo hi barr -> barr
138 newBCO insns_barr literals_barr ptrs_parr itbls_barr arity# bitmap
141 -- we recursively link any sub-BCOs while making the ptrs array
142 mkPtrsArray :: ItblEnv -> ClosureEnv -> Int -> [BCOPtr] -> IO (Array Int HValue)
143 mkPtrsArray ie ce n_ptrs ptrs = do
144 marr <- newArray_ (0, n_ptrs-1)
146 fill (BCOPtrName n) i = do
147 ptr <- lookupName ce n
148 unsafeWrite marr i ptr
149 fill (BCOPtrPrimOp op) i = do
150 ptr <- lookupPrimOp op
151 unsafeWrite marr i ptr
152 fill (BCOPtrBCO ul_bco) i = do
153 BCO bco# <- linkBCO' ie ce ul_bco
154 writeArrayBCO marr i bco#
155 zipWithM fill ptrs [0..]
158 newtype IOArray i e = IOArray (STArray RealWorld i e)
160 instance MArray IOArray e IO where
161 getBounds (IOArray marr) = stToIO $ getBounds marr
162 newArray lu init = stToIO $ do
163 marr <- newArray lu init; return (IOArray marr)
164 newArray_ lu = stToIO $ do
165 marr <- newArray_ lu; return (IOArray marr)
166 unsafeRead (IOArray marr) i = stToIO (unsafeRead marr i)
167 unsafeWrite (IOArray marr) i e = stToIO (unsafeWrite marr i e)
169 -- XXX HACK: we should really have a new writeArray# primop that takes a BCO#.
170 writeArrayBCO :: IOArray Int a -> Int -> BCO# -> IO ()
171 writeArrayBCO (IOArray (STArray _ _ marr#)) (I# i#) bco# = IO $ \s# ->
172 case (unsafeCoerce# writeArray#) marr# i# bco# s# of { s# ->
177 newBCO :: ByteArray# -> ByteArray# -> Array# a
178 -> ByteArray# -> Int# -> ByteArray# -> IO BCO
179 newBCO instrs lits ptrs itbls arity bitmap
180 = IO $ \s -> case newBCO# instrs lits ptrs itbls arity bitmap s of
181 (# s1, bco #) -> (# s1, BCO bco #)
184 lookupLiteral :: Either Word FastString -> IO Word
185 lookupLiteral (Left lit) = return lit
186 lookupLiteral (Right sym) = do Ptr addr <- lookupStaticPtr sym
187 return (W# (unsafeCoerce# addr))
188 -- Can't be bothered to find the official way to convert Addr# to Word#;
189 -- the FFI/Foreign designers make it too damn difficult
190 -- Hence we apply the Blunt Instrument, which works correctly
191 -- on all reasonable architectures anyway
193 lookupStaticPtr :: FastString -> IO (Ptr ())
194 lookupStaticPtr addr_of_label_string
195 = do let label_to_find = unpackFS addr_of_label_string
196 m <- lookupSymbol label_to_find
198 Just ptr -> return ptr
199 Nothing -> linkFail "ByteCodeLink: can't find label"
202 lookupPrimOp :: PrimOp -> IO HValue
204 = do let sym_to_find = primopToCLabel primop "closure"
205 m <- lookupSymbol sym_to_find
207 Just (Ptr addr) -> case addrToHValue# addr of
208 (# hval #) -> return hval
209 Nothing -> linkFail "ByteCodeLink.lookupCE(primop)" sym_to_find
211 lookupName :: ClosureEnv -> Name -> IO HValue
213 = case lookupNameEnv ce nm of
214 Just (_,aa) -> return aa
216 -> ASSERT2(isExternalName nm, ppr nm)
217 do let sym_to_find = nameToCLabel nm "closure"
218 m <- lookupSymbol sym_to_find
220 Just (Ptr addr) -> case addrToHValue# addr of
221 (# hval #) -> return hval
222 Nothing -> linkFail "ByteCodeLink.lookupCE" sym_to_find
224 lookupIE :: ItblEnv -> Name -> IO (Ptr a)
226 = case lookupNameEnv ie con_nm of
227 Just (_, Ptr a) -> return (Ptr a)
229 -> do -- try looking up in the object files.
230 let sym_to_find1 = nameToCLabel con_nm "con_info"
231 m <- lookupSymbol sym_to_find1
233 Just addr -> return addr
235 -> do -- perhaps a nullary constructor?
236 let sym_to_find2 = nameToCLabel con_nm "static_info"
237 n <- lookupSymbol sym_to_find2
239 Just addr -> return addr
240 Nothing -> linkFail "ByteCodeLink.lookupIE"
241 (sym_to_find1 ++ " or " ++ sym_to_find2)
243 linkFail :: String -> String -> IO a
245 = throwDyn (ProgramError $
247 , "During interactive linking, GHCi couldn't find the following symbol:"
249 , "This may be due to you not asking GHCi to load extra object files,"
250 , "archives or DLLs needed by your current session. Restart GHCi, specifying"
251 , "the missing library using the -L/path/to/object/dir and -lmissinglibname"
252 , "flags, or simply by naming the relevant files on the GHCi command line."
253 , "Alternatively, this link failure might indicate a bug in GHCi."
254 , "If you suspect the latter, please send a bug report to:"
255 , " glasgow-haskell-bugs@haskell.org"
258 -- HACKS!!! ToDo: cleaner
259 nameToCLabel :: Name -> String{-suffix-} -> String
260 nameToCLabel n suffix
261 = if pkgid /= mainPackageId
262 then package_part ++ '_': qual_name
265 pkgid = modulePackageId mod
267 package_part = unpackFS (zEncodeFS (packageIdFS (modulePackageId mod)))
268 module_part = unpackFS (zEncodeFS (moduleNameFS (moduleName mod)))
269 occ_part = unpackFS (zEncodeFS (occNameFS (nameOccName n)))
270 qual_name = module_part ++ '_':occ_part ++ '_':suffix
273 primopToCLabel :: PrimOp -> String{-suffix-} -> String
274 primopToCLabel primop suffix
275 = let str = "base_GHCziPrimopWrappers_" ++ unpackFS (zEncodeFS (occNameFS (primOpOcc primop))) ++ '_':suffix
276 in --trace ("primopToCLabel: " ++ str)