Retrieving the datacon of an arbitrary closure
[ghc-hetmet.git] / compiler / ghci / ByteCodeLink.lhs
index 875f1d6..427fa1e 100644 (file)
@@ -1,37 +1,40 @@
 %
-% (c) The University of Glasgow 2000
+% (c) The University of Glasgow 2000-2006
 %
-\section[ByteCodeLink]{Bytecode assembler and linker}
+ByteCodeLink: Bytecode assembler and linker
 
 \begin{code}
-
 {-# OPTIONS -optc-DNON_POSIX_SOURCE #-}
 
 module ByteCodeLink ( 
        HValue, 
        ClosureEnv, emptyClosureEnv, extendClosureEnv,
        linkBCO, lookupStaticPtr
+       ,lookupIE
   ) where
 
 #include "HsVersions.h"
 
-import ByteCodeItbls   ( ItblEnv, ItblPtr )
-import ByteCodeAsm     ( UnlinkedBCO(..), BCOPtr(..), sizeSS, ssElts )
-import ObjLink         ( lookupSymbol )
+import ByteCodeItbls
+import ByteCodeAsm
+import ObjLink
 
-import Name            ( Name,  nameModule, nameOccName, isExternalName )
+import Name
 import NameEnv
-import OccName         ( occNameFS )
-import PrimOp          ( PrimOp, primOpOcc )
-import Module          ( moduleFS )
-import FastString      ( FastString(..), unpackFS, zEncodeFS )
+import OccName
+import PrimOp
+import Module
+import PackageConfig
+import FastString
+import Panic
+
+#ifdef DEBUG
 import Outputable
-import Panic            ( GhcException(..) )
+#endif
 
 -- Standard libraries
 import GHC.Word                ( Word(..) )
 
-import Data.Array.IArray ( listArray )
 import Data.Array.Base
 import GHC.Arr         ( STArray(..) )
 
@@ -44,7 +47,7 @@ import GHC.Exts               ( BCO#, newBCO#, unsafeCoerce#, Int#,
 
 import GHC.Arr         ( Array(..) )
 import GHC.IOBase      ( IO(..) )
-import GHC.Ptr         ( Ptr(..) )
+import GHC.Ptr         ( Ptr(..), castPtr )
 import GHC.Base                ( writeArray#, RealWorld, Int(..) )
 \end{code}
 
@@ -122,7 +125,7 @@ linkBCO' ie ce (UnlinkedBCO nm arity insns_barr bitmap literalsSS ptrsSS itblsSS
             ptrs_parr = case ptrs_arr of Array lo hi parr -> parr
 
             itbls_arr = listArray (0, n_itbls-1) linked_itbls
-                        :: UArray Int ItblPtr
+
             itbls_barr = case itbls_arr of UArray lo hi barr -> barr
 
             literals_arr = listArray (0, n_literals-1) linked_literals
@@ -153,10 +156,8 @@ mkPtrsArray ie ce n_ptrs ptrs = do
 
 newtype IOArray i e = IOArray (STArray RealWorld i e)
 
-instance HasBounds IOArray where
-    bounds (IOArray marr) = bounds marr
-
 instance MArray IOArray e IO where
+    getBounds (IOArray marr) = stToIO $ getBounds marr
     newArray lu init = stToIO $ do
         marr <- newArray lu init; return (IOArray marr)
     newArray_ lu = stToIO $ do
@@ -222,7 +223,7 @@ lookupName ce nm
 lookupIE :: ItblEnv -> Name -> IO (Ptr a)
 lookupIE ie con_nm 
    = case lookupNameEnv ie con_nm of
-        Just (_, Ptr a) -> return (Ptr a)
+        Just (_, a) -> return (castPtr (itblCode a))
         Nothing
            -> do -- try looking up in the object files.
                  let sym_to_find1 = nameToCLabel con_nm "con_info"
@@ -256,12 +257,21 @@ linkFail who what
 -- HACKS!!!  ToDo: cleaner
 nameToCLabel :: Name -> String{-suffix-} -> String
 nameToCLabel n suffix
-   = unpackFS (zEncodeFS (moduleFS (nameModule n)))
-     ++ '_': unpackFS (zEncodeFS (occNameFS (nameOccName n))) ++ '_':suffix
+   = if pkgid /= mainPackageId
+        then package_part ++ '_': qual_name
+        else qual_name
+  where
+        pkgid = modulePackageId mod
+        mod = nameModule n
+        package_part = unpackFS (zEncodeFS (packageIdFS (modulePackageId mod)))
+        module_part  = unpackFS (zEncodeFS (moduleNameFS (moduleName mod)))
+        occ_part     = unpackFS (zEncodeFS (occNameFS (nameOccName n)))
+        qual_name = module_part ++ '_':occ_part ++ '_':suffix
+
 
 primopToCLabel :: PrimOp -> String{-suffix-} -> String
 primopToCLabel primop suffix
-   = let str = "GHCziPrimopWrappers_" ++ unpackFS (zEncodeFS (occNameFS (primOpOcc primop))) ++ '_':suffix
+   = let str = "base_GHCziPrimopWrappers_" ++ unpackFS (zEncodeFS (occNameFS (primOpOcc primop))) ++ '_':suffix
      in --trace ("primopToCLabel: " ++ str)
         str
 \end{code}