2 % (c) The University of Glasgow 2000
4 \section[ByteCodeItbls]{Generate infotables for interpreter-made bytecodes}
8 {-# OPTIONS -optc-DNON_POSIX_SOURCE #-}
10 module ByteCodeItbls ( ItblEnv, ItblPtr, mkITbls ) where
12 #include "HsVersions.h"
14 import Name ( Name, getName )
15 import FiniteMap ( FiniteMap, listToFM, emptyFM, plusFM )
16 import Type ( typePrimRep )
17 import DataCon ( DataCon, dataConRepArgTys )
18 import TyCon ( TyCon, tyConFamilySize, isDataTyCon, tyConDataCons )
19 import Constants ( mIN_SIZE_NonUpdHeapObject )
20 import ClosureInfo ( mkVirtHeapOffsets )
21 import FastString ( FastString(..) )
22 import Util ( lengthIs, listLengthCmp )
24 import Foreign ( Storable(..), Word8, Word16, Word32, Word64, Ptr(..),
25 malloc, castPtr, plusPtr, Addr )
26 import Addr ( addrToInt )
27 import Bits ( Bits(..), shiftR )
29 import PrelBase ( Int(..) )
30 import PrelIOBase ( IO(..) )
32 import Monad ( liftM )
36 %************************************************************************
38 \subsection{Manufacturing of info tables for DataCons}
40 %************************************************************************
44 type ItblPtr = Ptr StgInfoTable
45 type ItblEnv = FiniteMap Name ItblPtr
48 -- Make info tables for the data decls in this module
49 mkITbls :: [TyCon] -> IO ItblEnv
50 mkITbls [] = return emptyFM
51 mkITbls (tc:tcs) = do itbls <- mkITbl tc
53 return (itbls `plusFM` itbls2)
55 mkITbl :: TyCon -> IO ItblEnv
57 | not (isDataTyCon tc)
59 | dcs `lengthIs` n -- paranoia; this is an assertion.
60 = make_constr_itbls dcs
62 dcs = tyConDataCons tc
63 n = tyConFamilySize tc
66 cONSTR = 1 -- as defined in ghc/includes/ClosureTypes.h
68 -- Assumes constructors are numbered from zero, not one
69 make_constr_itbls :: [DataCon] -> IO ItblEnv
70 make_constr_itbls cons
71 | listLengthCmp cons 8 /= GT -- <= 8 elements in the list
72 = do is <- mapM mk_vecret_itbl (zip cons [0..])
75 = do is <- mapM mk_dirret_itbl (zip cons [0..])
78 mk_vecret_itbl (dcon, conNo)
79 = mk_itbl dcon conNo (vecret_entry conNo)
80 mk_dirret_itbl (dcon, conNo)
81 = mk_itbl dcon conNo stg_interp_constr_entry
83 mk_itbl :: DataCon -> Int -> Addr -> IO (Name,ItblPtr)
84 mk_itbl dcon conNo entry_addr
85 = let (tot_wds, ptr_wds, _)
86 = mkVirtHeapOffsets typePrimRep (dataConRepArgTys dcon)
88 nptrs = tot_wds - ptr_wds
90 | ptrs + nptrs >= mIN_SIZE_NonUpdHeapObject = nptrs
91 | otherwise = mIN_SIZE_NonUpdHeapObject - ptrs
93 ptrs = fromIntegral ptrs,
94 nptrs = fromIntegral nptrs_really,
95 tipe = fromIntegral cONSTR,
96 srtlen = fromIntegral conNo,
99 -- Make a piece of code to jump to "entry_label".
100 -- This is the only arch-dependent bit.
101 code = mkJumpToAddr entry_addr
104 --putStrLn ("SIZE of itbl is " ++ show (sizeOf itbl))
105 --putStrLn ("# ptrs of itbl is " ++ show ptrs)
106 --putStrLn ("# nptrs of itbl is " ++ show nptrs_really)
108 return (getName dcon, addr `plusPtr` 8)
111 -- Make code which causes a jump to the given address. This is the
112 -- only arch-dependent bit of the itbl story. The returned list is
113 -- itblCodeLength elements (bytes) long.
115 -- For sparc_TARGET_ARCH, i386_TARGET_ARCH, etc.
116 #include "nativeGen/NCG.h"
118 itblCodeLength :: Int
119 itblCodeLength = length (mkJumpToAddr undefined)
121 mkJumpToAddr :: Addr -> [ItblCode]
123 #if sparc_TARGET_ARCH
124 -- After some consideration, we'll try this, where
125 -- 0x55555555 stands in for the address to jump to.
126 -- According to ghc/includes/MachRegs.h, %g3 is very
127 -- likely indeed to be baggable.
129 -- 0000 07155555 sethi %hi(0x55555555), %g3
130 -- 0004 8610E155 or %g3, %lo(0x55555555), %g3
131 -- 0008 81C0C000 jmp %g3
134 type ItblCode = Word32
136 = let w32 = fromIntegral (addrToInt a)
138 hi22, lo10 :: Word32 -> Word32
140 hi22 x = (x `shiftR` 10) .&. 0x3FFFF
142 in [ 0x07000000 .|. (hi22 w32),
143 0x8610E000 .|. (lo10 w32),
149 -- Let the address to jump to be 0xWWXXYYZZ.
150 -- Generate movl $0xWWXXYYZZ,%eax ; jmp *%eax
152 -- B8 ZZ YY XX WW FF E0
154 type ItblCode = Word8
156 = let w32 = fromIntegral (addrToInt a)
159 = [0xB8, byte 0 w32, byte 1 w32,
160 byte 2 w32, byte 3 w32,
166 #if alpha_TARGET_ARCH
167 type ItblCode = Word32
169 = [ 0xc3800000 -- br at, .+4
170 , 0xa79c000c -- ldq at, 12(at)
171 , 0x6bfc0000 -- jmp (at) # with zero hint -- oh well
173 , fromIntegral (w64 .&. 0x0000FFFF)
174 , fromIntegral ((w64 `shiftR` 32) .&. 0x0000FFFF) ]
175 where w64 = fromIntegral (addrToInt a) :: Word64
179 byte :: Int -> Word32 -> Word8
180 byte 0 w = fromIntegral (w .&. 0xFF)
181 byte 1 w = fromIntegral ((w `shiftR` 8) .&. 0xFF)
182 byte 2 w = fromIntegral ((w `shiftR` 16) .&. 0xFF)
183 byte 3 w = fromIntegral ((w `shiftR` 24) .&. 0xFF)
186 vecret_entry 0 = stg_interp_constr1_entry
187 vecret_entry 1 = stg_interp_constr2_entry
188 vecret_entry 2 = stg_interp_constr3_entry
189 vecret_entry 3 = stg_interp_constr4_entry
190 vecret_entry 4 = stg_interp_constr5_entry
191 vecret_entry 5 = stg_interp_constr6_entry
192 vecret_entry 6 = stg_interp_constr7_entry
193 vecret_entry 7 = stg_interp_constr8_entry
195 -- entry point for direct returns for created constr itbls
196 foreign label "stg_interp_constr_entry" stg_interp_constr_entry :: Addr
197 -- and the 8 vectored ones
198 foreign label "stg_interp_constr1_entry" stg_interp_constr1_entry :: Addr
199 foreign label "stg_interp_constr2_entry" stg_interp_constr2_entry :: Addr
200 foreign label "stg_interp_constr3_entry" stg_interp_constr3_entry :: Addr
201 foreign label "stg_interp_constr4_entry" stg_interp_constr4_entry :: Addr
202 foreign label "stg_interp_constr5_entry" stg_interp_constr5_entry :: Addr
203 foreign label "stg_interp_constr6_entry" stg_interp_constr6_entry :: Addr
204 foreign label "stg_interp_constr7_entry" stg_interp_constr7_entry :: Addr
205 foreign label "stg_interp_constr8_entry" stg_interp_constr8_entry :: Addr
211 -- Ultra-minimalist version specially for constructors
212 #if SIZEOF_VOID_P == 8
213 type HalfWord = Word32
215 type HalfWord = Word16
218 data StgInfoTable = StgInfoTable {
226 instance Storable StgInfoTable where
234 fieldSz (head.code) itbl * itblCodeLength]
240 = runState (castPtr a0)
241 $ do store (ptrs itbl)
245 sequence_ (map store (code itbl))
248 = runState (castPtr a0)
253 code <- sequence (replicate itblCodeLength load)
263 fieldSz :: (Storable a, Storable b) => (a -> b) -> a -> Int
264 fieldSz sel x = sizeOf (sel x)
266 newtype State s m a = State (s -> m (s, a))
268 instance Monad m => Monad (State s m) where
269 return a = State (\s -> return (s, a))
270 State m >>= k = State (\s -> m s >>= \(s', a) -> case k a of State n -> n s')
271 fail str = State (\s -> fail str)
273 class (Monad m, Monad (t m)) => MonadT t m where
276 instance Monad m => MonadT (State s) m where
277 lift m = State (\s -> m >>= \a -> return (s, a))
279 runState :: (Monad m) => s -> State s m a -> m a
280 runState s (State m) = m s >>= return . snd
282 type PtrIO = State (Ptr Word8) IO
284 advance :: Storable a => PtrIO (Ptr a)
285 advance = State adv where
286 adv addr = case castPtr addr of { addrCast -> return
287 (addr `plusPtr` sizeOfPointee addrCast, addrCast) }
289 sizeOfPointee :: (Storable a) => Ptr a -> Int
290 sizeOfPointee addr = sizeOf (typeHack addr)
291 where typeHack = undefined :: Ptr a -> a
293 store :: Storable a => a -> PtrIO ()
294 store x = do addr <- advance
297 load :: Storable a => PtrIO a
298 load = do addr <- advance