remove empty dir
[ghc-hetmet.git] / ghc / compiler / ghci / ByteCodeItbls.lhs
index 280cd35..74346c6 100644 (file)
@@ -4,27 +4,33 @@
 \section[ByteCodeItbls]{Generate infotables for interpreter-made bytecodes}
 
 \begin{code}
+
+{-# OPTIONS -optc-DNON_POSIX_SOURCE #-}
+
 module ByteCodeItbls ( ItblEnv, ItblPtr, mkITbls ) where
 
 #include "HsVersions.h"
 
 import Name            ( Name, getName )
-import FiniteMap       ( FiniteMap, listToFM, emptyFM, plusFM )
-import Type            ( typePrimRep )
+import NameEnv
+import SMRep           ( typeCgRep )
 import DataCon         ( DataCon, dataConRepArgTys )
 import TyCon           ( TyCon, tyConFamilySize, isDataTyCon, tyConDataCons )
-import Constants       ( mIN_SIZE_NonUpdHeapObject )
-import ClosureInfo     ( mkVirtHeapOffsets )
+import Constants       ( mIN_PAYLOAD_SIZE, wORD_SIZE )
+import CgHeapery       ( mkVirtHeapOffsets )
 import FastString      ( FastString(..) )
+import Util             ( lengthIs, listLengthCmp )
 
-import Foreign         ( Storable(..), Word8, Word16, Word32, Ptr(..), 
-                         malloc, castPtr, plusPtr, Addr )
-import Addr            ( addrToInt )
-import Bits            ( Bits(..), shiftR )
-
-import PrelBase                ( Int(..) )
-import PrelIOBase      ( IO(..) )
+import Foreign
+import Foreign.C
+import DATA_BITS       ( Bits(..), shiftR )
 
+import GHC.Exts                ( Int(I#), addr2Int# )
+#if __GLASGOW_HASKELL__ < 503
+import Ptr             ( Ptr(..) )
+#else
+import GHC.Ptr         ( Ptr(..) )
+#endif
 \end{code}
 
 %************************************************************************
@@ -34,91 +40,94 @@ import PrelIOBase   ( IO(..) )
 %************************************************************************
 
 \begin{code}
-
 type ItblPtr = Ptr StgInfoTable
-type ItblEnv = FiniteMap Name ItblPtr
+type ItblEnv = NameEnv (Name, ItblPtr)
+       -- We need the Name in the range so we know which
+       -- elements to filter out when unloading a module
+
+mkItblEnv :: [(Name,ItblPtr)] -> ItblEnv
+mkItblEnv pairs = mkNameEnv [(n, (n,p)) | (n,p) <- pairs]
 
 
 -- Make info tables for the data decls in this module
 mkITbls :: [TyCon] -> IO ItblEnv
-mkITbls [] = return emptyFM
+mkITbls [] = return emptyNameEnv
 mkITbls (tc:tcs) = do itbls  <- mkITbl tc
                       itbls2 <- mkITbls tcs
-                      return (itbls `plusFM` itbls2)
+                      return (itbls `plusNameEnv` itbls2)
 
 mkITbl :: TyCon -> IO ItblEnv
 mkITbl tc
    | not (isDataTyCon tc) 
-   = return emptyFM
-   | n == length dcs  -- paranoia; this is an assertion.
+   = return emptyNameEnv
+   | dcs `lengthIs` n -- paranoia; this is an assertion.
    = make_constr_itbls dcs
      where
         dcs = tyConDataCons tc
         n   = tyConFamilySize tc
 
-cONSTR :: Int
-cONSTR = 1  -- as defined in ghc/includes/ClosureTypes.h
+#include "../includes/ClosureTypes.h"
+cONSTR :: Int  -- Defined in ClosureTypes.h
+cONSTR = CONSTR 
 
 -- Assumes constructors are numbered from zero, not one
 make_constr_itbls :: [DataCon] -> IO ItblEnv
 make_constr_itbls cons
-   | length cons <= 8
+   | listLengthCmp cons 8 /= GT -- <= 8 elements in the list
    = do is <- mapM mk_vecret_itbl (zip cons [0..])
-       return (listToFM is)
+       return (mkItblEnv is)
    | otherwise
    = do is <- mapM mk_dirret_itbl (zip cons [0..])
-       return (listToFM is)
+       return (mkItblEnv is)
      where
         mk_vecret_itbl (dcon, conNo)
            = mk_itbl dcon conNo (vecret_entry conNo)
         mk_dirret_itbl (dcon, conNo)
            = mk_itbl dcon conNo stg_interp_constr_entry
 
-        mk_itbl :: DataCon -> Int -> Addr -> IO (Name,ItblPtr)
+        mk_itbl :: DataCon -> Int -> Ptr () -> IO (Name,ItblPtr)
         mk_itbl dcon conNo entry_addr
-           = let (tot_wds, ptr_wds, _) 
-                    = mkVirtHeapOffsets typePrimRep (dataConRepArgTys dcon)
+           = let rep_args = [ (typeCgRep arg,arg) 
+                           | arg <- dataConRepArgTys dcon ]
+                (tot_wds, ptr_wds, _) = mkVirtHeapOffsets False{-not a THUNK-} rep_args
+
                  ptrs  = ptr_wds
                  nptrs = tot_wds - ptr_wds
                  nptrs_really
-                    | ptrs + nptrs >= mIN_SIZE_NonUpdHeapObject = nptrs
-                    | otherwise = mIN_SIZE_NonUpdHeapObject - ptrs
+                    | ptrs + nptrs >= mIN_PAYLOAD_SIZE = nptrs
+                    | otherwise = mIN_PAYLOAD_SIZE - ptrs
                  itbl  = StgInfoTable {
                            ptrs  = fromIntegral ptrs, 
                            nptrs = fromIntegral nptrs_really,
                            tipe  = fromIntegral cONSTR,
                            srtlen = fromIntegral conNo,
-                           code0 = fromIntegral code0, code1 = fromIntegral code1,
-                           code2 = fromIntegral code2, code3 = fromIntegral code3,
-                           code4 = fromIntegral code4, code5 = fromIntegral code5,
-                           code6 = fromIntegral code6, code7 = fromIntegral code7,
-                           code8 = fromIntegral code8, code9 = fromIntegral code9,
-                           codeA = fromIntegral codeA, codeB = fromIntegral codeB,
-                           codeC = fromIntegral codeC, codeD = fromIntegral codeD,
-                           codeE = fromIntegral codeE, codeF = fromIntegral codeF 
+                           code  = code
                         }
                  -- Make a piece of code to jump to "entry_label".
                  -- This is the only arch-dependent bit.
-                 [code0,code1,code2,code3,code4,code5,code6,code7,
-                  code8,code9,codeA,codeB,codeC,codeD,codeE,codeF]
-                    = mkJumpToAddr entry_addr
+                 code = mkJumpToAddr entry_addr
              in
-                 do addr <- malloc
+                 do addr <- malloc_exec (sizeOf itbl)
                     --putStrLn ("SIZE of itbl is " ++ show (sizeOf itbl))
                     --putStrLn ("# ptrs  of itbl is " ++ show ptrs)
                     --putStrLn ("# nptrs of itbl is " ++ show nptrs_really)
                     poke addr itbl
-                    return (getName dcon, addr `plusPtr` 8)
+                    return (getName dcon, addr `plusPtr` (2 * wORD_SIZE))
 
 
 -- Make code which causes a jump to the given address.  This is the
 -- only arch-dependent bit of the itbl story.  The returned list is
--- 16 elements long, since on sparc 4 words (i.e. 4 insns) are needed.
+-- itblCodeLength elements (bytes) long.
 
 -- For sparc_TARGET_ARCH, i386_TARGET_ARCH, etc.
 #include "nativeGen/NCG.h"
 
-mkJumpToAddr :: Addr -> [Word8]
+itblCodeLength :: Int
+itblCodeLength = length (mkJumpToAddr undefined)
+
+mkJumpToAddr :: Ptr () -> [ItblCode]
+
+ptrToInt (Ptr a#) = I# (addr2Int# a#)
 
 #if sparc_TARGET_ARCH
 -- After some consideration, we'll try this, where
@@ -131,53 +140,105 @@ mkJumpToAddr :: Addr -> [Word8]
 --   0008 81C0C000              jmp     %g3
 --   000c 01000000              nop
 
+type ItblCode = Word32
 mkJumpToAddr a
-   = let w32 = fromIntegral (addrToInt a)
-         insn1 = 0x07000000 .|. (hi22 w32)
-         insn2 = 0x8610E000 .|. (lo10 w32)
-         insn3 = 0x81C0C000
-         insn4 = 0x01000000 
-
-         -- big-endianly ...
-         w2bytes :: Word32 -> [Word8]
-         w2bytes w
-            = map fromIntegral [byte 3 w, byte 2 w, byte 1 w, byte 0 w]
+   = let w32 = fromIntegral (ptrToInt a)
 
          hi22, lo10 :: Word32 -> Word32
          lo10 x = x .&. 0x3FF
          hi22 x = (x `shiftR` 10) .&. 0x3FFFF
-      
-         insnBytes
-            = concat (map w2bytes [insn1, insn2, insn3, insn4])
-     in
-         insnBytes
-#endif
 
-#if i386_TARGET_ARCH
+     in  [ 0x07000000 .|. (hi22 w32),
+           0x8610E000 .|. (lo10 w32),
+           0x81C0C000,
+           0x01000000 ]
+
+#elif powerpc_TARGET_ARCH
+-- We'll use r12, for no particular reason.
+-- 0xDEADBEEF stands for the adress:
+-- 3D80DEAD lis r12,0xDEAD
+-- 618CBEEF ori r12,r12,0xBEEF
+-- 7D8903A6 mtctr r12
+-- 4E800420 bctr
+
+type ItblCode = Word32
+mkJumpToAddr a =
+    let w32 = fromIntegral (ptrToInt a)
+       hi16 x = (x `shiftR` 16) .&. 0xFFFF
+       lo16 x = x .&. 0xFFFF
+    in [
+       0x3D800000 .|. hi16 w32,
+       0x618C0000 .|. lo16 w32,
+       0x7D8903A6, 0x4E800420
+       ]
+
+#elif i386_TARGET_ARCH
 -- Let the address to jump to be 0xWWXXYYZZ.
 -- Generate   movl $0xWWXXYYZZ,%eax  ;  jmp *%eax
 -- which is
 -- B8 ZZ YY XX WW FF E0
+
+type ItblCode = Word8
+mkJumpToAddr a
+   = let w32 = fromIntegral (ptrToInt a) :: Word32
+         insnBytes :: [Word8]
+         insnBytes
+            = [0xB8, byte0 w32, byte1 w32, 
+                     byte2 w32, byte3 w32, 
+               0xFF, 0xE0]
+     in
+         insnBytes
+
+#elif x86_64_TARGET_ARCH
+-- Generates:
+--     jmpq *.L1(%rip)
+--     .align 8
+-- .L1:        
+--     .quad <addr>
+--
+-- We need a full 64-bit pointer (we can't assume the info table is
+-- allocated in low memory).  Assuming the info pointer is aligned to
+-- an 8-byte boundary, the addr will also be aligned.
+
+type ItblCode = Word8
 mkJumpToAddr a
-   = let w32 = fromIntegral (addrToInt a)
+   = let w64 = fromIntegral (ptrToInt a) :: Word64
          insnBytes :: [Word8]
          insnBytes
-            = map fromIntegral (take 16 (
-                 [0xB8, byte 0 w32, byte 1 w32, 
-                        byte 2 w32, byte 3 w32, 
-                  0xFF, 0xE0]
-                 ++ let nops = 0x90 : nops in nops
-              ))
+            = [0xff, 0x25, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
+              byte0 w64, byte1 w64, byte2 w64, byte3 w64,
+              byte4 w64, byte5 w64, byte6 w64, byte7 w64]
      in
          insnBytes
+
+#elif alpha_TARGET_ARCH
+type ItblCode = Word32
+mkJumpToAddr a
+    = [ 0xc3800000      -- br   at, .+4
+      , 0xa79c000c      -- ldq  at, 12(at)
+      , 0x6bfc0000      -- jmp  (at)    # with zero hint -- oh well
+      , 0x47ff041f      -- nop
+      , fromIntegral (w64 .&. 0x0000FFFF)
+      , fromIntegral ((w64 `shiftR` 32) .&. 0x0000FFFF) ]
+    where w64 = fromIntegral (ptrToInt a) :: Word64
+
+#else
+type ItblCode = Word32
+mkJumpToAddr a
+    = undefined
 #endif
 
 
-byte :: Int -> Word32 -> Word32
-byte 0 w = w .&. 0xFF
-byte 1 w = (w `shiftR` 8) .&. 0xFF
-byte 2 w = (w `shiftR` 16) .&. 0xFF
-byte 3 w = (w `shiftR` 24) .&. 0xFF
+byte0, byte1, byte2, byte3, byte4, byte5, byte6, byte7
+   :: (Integral w, Bits w) => w -> Word8
+byte0 w = fromIntegral w
+byte1 w = fromIntegral (w `shiftR` 8)
+byte2 w = fromIntegral (w `shiftR` 16)
+byte3 w = fromIntegral (w `shiftR` 24)
+byte4 w = fromIntegral (w `shiftR` 32)
+byte5 w = fromIntegral (w `shiftR` 40)
+byte6 w = fromIntegral (w `shiftR` 48)
+byte7 w = fromIntegral (w `shiftR` 56)
 
 
 vecret_entry 0 = stg_interp_constr1_entry
@@ -189,117 +250,117 @@ vecret_entry 5 = stg_interp_constr6_entry
 vecret_entry 6 = stg_interp_constr7_entry
 vecret_entry 7 = stg_interp_constr8_entry
 
+#ifndef __HADDOCK__
 -- entry point for direct returns for created constr itbls
-foreign label "stg_interp_constr_entry" stg_interp_constr_entry :: Addr
+foreign import ccall "&stg_interp_constr_entry" stg_interp_constr_entry :: Ptr ()
 -- and the 8 vectored ones
-foreign label "stg_interp_constr1_entry" stg_interp_constr1_entry :: Addr
-foreign label "stg_interp_constr2_entry" stg_interp_constr2_entry :: Addr
-foreign label "stg_interp_constr3_entry" stg_interp_constr3_entry :: Addr
-foreign label "stg_interp_constr4_entry" stg_interp_constr4_entry :: Addr
-foreign label "stg_interp_constr5_entry" stg_interp_constr5_entry :: Addr
-foreign label "stg_interp_constr6_entry" stg_interp_constr6_entry :: Addr
-foreign label "stg_interp_constr7_entry" stg_interp_constr7_entry :: Addr
-foreign label "stg_interp_constr8_entry" stg_interp_constr8_entry :: Addr
-
+foreign import ccall "&stg_interp_constr1_entry" stg_interp_constr1_entry :: Ptr ()
+foreign import ccall "&stg_interp_constr2_entry" stg_interp_constr2_entry :: Ptr ()
+foreign import ccall "&stg_interp_constr3_entry" stg_interp_constr3_entry :: Ptr ()
+foreign import ccall "&stg_interp_constr4_entry" stg_interp_constr4_entry :: Ptr ()
+foreign import ccall "&stg_interp_constr5_entry" stg_interp_constr5_entry :: Ptr ()
+foreign import ccall "&stg_interp_constr6_entry" stg_interp_constr6_entry :: Ptr ()
+foreign import ccall "&stg_interp_constr7_entry" stg_interp_constr7_entry :: Ptr ()
+foreign import ccall "&stg_interp_constr8_entry" stg_interp_constr8_entry :: Ptr ()
+#endif
 
 
 
 
 -- Ultra-minimalist version specially for constructors
+#if SIZEOF_VOID_P == 8
+type HalfWord = Word32
+#else
+type HalfWord = Word16
+#endif
+
 data StgInfoTable = StgInfoTable {
-   ptrs   :: Word16,
-   nptrs  :: Word16,
-   srtlen :: Word16,
-   tipe   :: Word16,
-   code0, code1, code2, code3, code4, code5, code6, code7,
-   code8, code9, codeA, codeB, codeC, codeD, codeE, codeF :: Word8
+   ptrs   :: HalfWord,
+   nptrs  :: HalfWord,
+   tipe   :: HalfWord,
+   srtlen :: HalfWord,
+   code   :: [ItblCode]
 }
 
-
 instance Storable StgInfoTable where
 
    sizeOf itbl 
-      = (sum . map (\f -> f itbl))
-        [fieldSz ptrs, fieldSz nptrs, fieldSz srtlen, fieldSz tipe,
-         fieldSz code0, fieldSz code1, fieldSz code2, fieldSz code3, 
-         fieldSz code4, fieldSz code5, fieldSz code6, fieldSz code7,
-         fieldSz code8, fieldSz code9, fieldSz codeA, fieldSz codeB, 
-         fieldSz codeC, fieldSz codeD, fieldSz codeE, fieldSz codeF]
+      = sum
+        [fieldSz ptrs itbl,
+         fieldSz nptrs itbl,
+         fieldSz tipe itbl,
+         fieldSz srtlen itbl,
+         fieldSz (head.code) itbl * itblCodeLength]
 
    alignment itbl 
-      = (sum . map (\f -> f itbl))
-        [fieldAl ptrs, fieldAl nptrs, fieldAl srtlen, fieldAl tipe,
-         fieldAl code0, fieldAl code1, fieldAl code2, fieldAl code3, 
-         fieldAl code4, fieldAl code5, fieldAl code6, fieldAl code7,
-         fieldAl code8, fieldAl code9, fieldAl codeA, fieldAl codeB, 
-         fieldAl codeC, fieldAl codeD, fieldAl codeE, fieldAl codeF]
+      = SIZEOF_VOID_P
 
    poke a0 itbl
-      = do a1  <- store (ptrs   itbl) (castPtr a0)
-           a2  <- store (nptrs  itbl) a1
-           a3  <- store (tipe   itbl) a2
-           a4  <- store (srtlen itbl) a3
-           a5  <- store (code0  itbl) a4
-           a6  <- store (code1  itbl) a5
-           a7  <- store (code2  itbl) a6
-           a8  <- store (code3  itbl) a7
-           a9  <- store (code4  itbl) a8
-           aA  <- store (code5  itbl) a9
-           aB  <- store (code6  itbl) aA
-           aC  <- store (code7  itbl) aB
-           aD  <- store (code8  itbl) aC
-           aE  <- store (code9  itbl) aD
-           aF  <- store (codeA  itbl) aE
-           a10 <- store (codeB  itbl) aF
-           a11 <- store (codeC  itbl) a10
-           a12 <- store (codeD  itbl) a11
-           a13 <- store (codeE  itbl) a12
-           a14 <- store (codeF  itbl) a13
-           return ()
+      = runState (castPtr a0)
+      $ do store (ptrs   itbl)
+           store (nptrs  itbl)
+           store (tipe   itbl)
+           store (srtlen itbl)
+           sequence_ (map store (code itbl))
 
    peek a0
-      = do (a1, ptrs)   <- load (castPtr a0)
-           (a2, nptrs)  <- load a1
-           (a3, tipe)   <- load a2
-           (a4, srtlen) <- load a3
-           (a5, code0)  <- load a4
-           (a6, code1)  <- load a5
-           (a7, code2)  <- load a6
-           (a8, code3)  <- load a7
-           (a9, code4)  <- load a8
-           (aA, code5)  <- load a9
-           (aB, code6)  <- load aA
-           (aC, code7)  <- load aB
-           (aD, code8)  <- load aC
-           (aE, code9)  <- load aD
-           (aF, codeA)  <- load aE
-           (a10,codeB)  <- load aF
-           (a11,codeC)  <- load a10
-           (a12,codeD)  <- load a11
-           (a13,codeE)  <- load a12
-           (a14,codeF)  <- load a13
+      = runState (castPtr a0)
+      $ do ptrs   <- load
+           nptrs  <- load
+           tipe   <- load
+           srtlen <- load
+           code   <- sequence (replicate itblCodeLength load)
            return 
               StgInfoTable { 
-                 ptrs = ptrs, nptrs = nptrs, 
-                 srtlen = srtlen, tipe = tipe,
-                 code0 = code0, code1 = code1, code2 = code2, code3 = code3,
-                 code4 = code4, code5 = code5, code6 = code6, code7 = code7,
-                 code8 = code8, code9 = code9, codeA = codeA, codeB = codeB,
-                 codeC = codeC, codeD = codeD, codeE = codeE, codeF = codeF
+                 ptrs   = ptrs,
+                 nptrs  = nptrs, 
+                 tipe   = tipe,
+                 srtlen = srtlen,
+                 code   = code
               }
 
 fieldSz :: (Storable a, Storable b) => (a -> b) -> a -> Int
 fieldSz sel x = sizeOf (sel x)
 
-fieldAl :: (Storable a, Storable b) => (a -> b) -> a -> Int
-fieldAl sel x = alignment (sel x)
+newtype State s m a = State (s -> m (s, a))
+
+instance Monad m => Monad (State s m) where
+  return a      = State (\s -> return (s, a))
+  State m >>= k = State (\s -> m s >>= \(s', a) -> case k a of State n -> n s')
+  fail str      = State (\s -> fail str)
+
+class (Monad m, Monad (t m)) => MonadT t m where
+  lift :: m a -> t m a
+
+instance Monad m => MonadT (State s) m where
+  lift m        = State (\s -> m >>= \a -> return (s, a))
+
+runState :: (Monad m) => s -> State s m a -> m a
+runState s (State m) = m s >>= return . snd
+
+type PtrIO = State (Ptr Word8) IO
+
+advance :: Storable a => PtrIO (Ptr a)
+advance = State adv where
+    adv addr = case castPtr addr of { addrCast -> return
+        (addr `plusPtr` sizeOfPointee addrCast, addrCast) }
+
+sizeOfPointee :: (Storable a) => Ptr a -> Int
+sizeOfPointee addr = sizeOf (typeHack addr)
+    where typeHack = undefined :: Ptr a -> a
+
+store :: Storable a => a -> PtrIO ()
+store x = do addr <- advance
+             lift (poke addr x)
+
+load :: Storable a => PtrIO a
+load = do addr <- advance
+          lift (peek addr)
 
-store :: Storable a => a -> Ptr a -> IO (Ptr b)
-store x addr = do poke addr x
-                  return (castPtr (addr `plusPtr` sizeOf x))
+foreign import ccall unsafe "stgMallocBytesRWX"
+  _stgMallocBytesRWX :: CInt -> IO (Ptr a)
 
-load :: Storable a => Ptr a -> IO (Ptr b, a)
-load addr = do x <- peek addr
-               return (castPtr (addr `plusPtr` sizeOf x), x)
+malloc_exec :: Int -> IO (Ptr a)
+malloc_exec bytes = _stgMallocBytesRWX (fromIntegral bytes)
 
 \end{code}