2 % (c) The University of Glasgow 2000-2006
4 ByteCodeItbls: Generate infotables for interpreter-made bytecodes
7 {-# OPTIONS -optc-DNON_POSIX_SOURCE #-}
9 module ByteCodeItbls ( ItblEnv, ItblPtr, mkITbls ) where
11 #include "HsVersions.h"
13 import Name ( Name, getName )
15 import SMRep ( typeCgRep )
16 import DataCon ( DataCon, dataConRepArgTys )
17 import TyCon ( TyCon, tyConFamilySize, isDataTyCon, tyConDataCons )
18 import Constants ( mIN_PAYLOAD_SIZE, wORD_SIZE )
19 import CgHeapery ( mkVirtHeapOffsets )
20 import FastString ( FastString(..) )
21 import Util ( lengthIs, listLengthCmp )
25 import Data.Bits ( Bits(..), shiftR )
27 import GHC.Exts ( Int(I#), addr2Int# )
28 import GHC.Ptr ( Ptr(..) )
31 %************************************************************************
33 \subsection{Manufacturing of info tables for DataCons}
35 %************************************************************************
38 type ItblPtr = Ptr StgInfoTable
39 type ItblEnv = NameEnv (Name, ItblPtr)
40 -- We need the Name in the range so we know which
41 -- elements to filter out when unloading a module
43 mkItblEnv :: [(Name,ItblPtr)] -> ItblEnv
44 mkItblEnv pairs = mkNameEnv [(n, (n,p)) | (n,p) <- pairs]
47 -- Make info tables for the data decls in this module
48 mkITbls :: [TyCon] -> IO ItblEnv
49 mkITbls [] = return emptyNameEnv
50 mkITbls (tc:tcs) = do itbls <- mkITbl tc
52 return (itbls `plusNameEnv` itbls2)
54 mkITbl :: TyCon -> IO ItblEnv
56 | not (isDataTyCon tc)
58 | dcs `lengthIs` n -- paranoia; this is an assertion.
59 = make_constr_itbls dcs
61 dcs = tyConDataCons tc
62 n = tyConFamilySize tc
64 #include "../includes/ClosureTypes.h"
65 cONSTR :: Int -- Defined in 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 -> Ptr () -> IO (Name,ItblPtr)
84 mk_itbl dcon conNo entry_addr
85 = let rep_args = [ (typeCgRep arg,arg)
86 | arg <- dataConRepArgTys dcon ]
87 (tot_wds, ptr_wds, _) = mkVirtHeapOffsets False{-not a THUNK-} rep_args
90 nptrs = tot_wds - ptr_wds
92 | ptrs + nptrs >= mIN_PAYLOAD_SIZE = nptrs
93 | otherwise = mIN_PAYLOAD_SIZE - ptrs
95 ptrs = fromIntegral ptrs,
96 nptrs = fromIntegral nptrs_really,
97 tipe = fromIntegral cONSTR,
98 srtlen = fromIntegral conNo,
101 -- Make a piece of code to jump to "entry_label".
102 -- This is the only arch-dependent bit.
103 code = mkJumpToAddr entry_addr
105 do addr <- malloc_exec (sizeOf itbl)
106 --putStrLn ("SIZE of itbl is " ++ show (sizeOf itbl))
107 --putStrLn ("# ptrs of itbl is " ++ show ptrs)
108 --putStrLn ("# nptrs of itbl is " ++ show nptrs_really)
110 return (getName dcon, addr `plusPtr` (2 * wORD_SIZE))
113 -- Make code which causes a jump to the given address. This is the
114 -- only arch-dependent bit of the itbl story. The returned list is
115 -- itblCodeLength elements (bytes) long.
117 -- For sparc_TARGET_ARCH, i386_TARGET_ARCH, etc.
118 #include "nativeGen/NCG.h"
120 itblCodeLength :: Int
121 itblCodeLength = length (mkJumpToAddr undefined)
123 mkJumpToAddr :: Ptr () -> [ItblCode]
125 ptrToInt (Ptr a#) = I# (addr2Int# a#)
127 #if sparc_TARGET_ARCH
128 -- After some consideration, we'll try this, where
129 -- 0x55555555 stands in for the address to jump to.
130 -- According to ghc/includes/MachRegs.h, %g3 is very
131 -- likely indeed to be baggable.
133 -- 0000 07155555 sethi %hi(0x55555555), %g3
134 -- 0004 8610E155 or %g3, %lo(0x55555555), %g3
135 -- 0008 81C0C000 jmp %g3
138 type ItblCode = Word32
140 = let w32 = fromIntegral (ptrToInt a)
142 hi22, lo10 :: Word32 -> Word32
144 hi22 x = (x `shiftR` 10) .&. 0x3FFFF
146 in [ 0x07000000 .|. (hi22 w32),
147 0x8610E000 .|. (lo10 w32),
151 #elif powerpc_TARGET_ARCH
152 -- We'll use r12, for no particular reason.
153 -- 0xDEADBEEF stands for the adress:
154 -- 3D80DEAD lis r12,0xDEAD
155 -- 618CBEEF ori r12,r12,0xBEEF
156 -- 7D8903A6 mtctr r12
159 type ItblCode = Word32
161 let w32 = fromIntegral (ptrToInt a)
162 hi16 x = (x `shiftR` 16) .&. 0xFFFF
163 lo16 x = x .&. 0xFFFF
165 0x3D800000 .|. hi16 w32,
166 0x618C0000 .|. lo16 w32,
167 0x7D8903A6, 0x4E800420
170 #elif i386_TARGET_ARCH
171 -- Let the address to jump to be 0xWWXXYYZZ.
172 -- Generate movl $0xWWXXYYZZ,%eax ; jmp *%eax
174 -- B8 ZZ YY XX WW FF E0
176 type ItblCode = Word8
178 = let w32 = fromIntegral (ptrToInt a) :: Word32
181 = [0xB8, byte0 w32, byte1 w32,
182 byte2 w32, byte3 w32,
187 #elif x86_64_TARGET_ARCH
194 -- We need a full 64-bit pointer (we can't assume the info table is
195 -- allocated in low memory). Assuming the info pointer is aligned to
196 -- an 8-byte boundary, the addr will also be aligned.
198 type ItblCode = Word8
200 = let w64 = fromIntegral (ptrToInt a) :: Word64
203 = [0xff, 0x25, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
204 byte0 w64, byte1 w64, byte2 w64, byte3 w64,
205 byte4 w64, byte5 w64, byte6 w64, byte7 w64]
209 #elif alpha_TARGET_ARCH
210 type ItblCode = Word32
212 = [ 0xc3800000 -- br at, .+4
213 , 0xa79c000c -- ldq at, 12(at)
214 , 0x6bfc0000 -- jmp (at) # with zero hint -- oh well
216 , fromIntegral (w64 .&. 0x0000FFFF)
217 , fromIntegral ((w64 `shiftR` 32) .&. 0x0000FFFF) ]
218 where w64 = fromIntegral (ptrToInt a) :: Word64
221 type ItblCode = Word32
227 byte0, byte1, byte2, byte3, byte4, byte5, byte6, byte7
228 :: (Integral w, Bits w) => w -> Word8
229 byte0 w = fromIntegral w
230 byte1 w = fromIntegral (w `shiftR` 8)
231 byte2 w = fromIntegral (w `shiftR` 16)
232 byte3 w = fromIntegral (w `shiftR` 24)
233 byte4 w = fromIntegral (w `shiftR` 32)
234 byte5 w = fromIntegral (w `shiftR` 40)
235 byte6 w = fromIntegral (w `shiftR` 48)
236 byte7 w = fromIntegral (w `shiftR` 56)
239 vecret_entry 0 = stg_interp_constr1_entry
240 vecret_entry 1 = stg_interp_constr2_entry
241 vecret_entry 2 = stg_interp_constr3_entry
242 vecret_entry 3 = stg_interp_constr4_entry
243 vecret_entry 4 = stg_interp_constr5_entry
244 vecret_entry 5 = stg_interp_constr6_entry
245 vecret_entry 6 = stg_interp_constr7_entry
246 vecret_entry 7 = stg_interp_constr8_entry
249 -- entry point for direct returns for created constr itbls
250 foreign import ccall "&stg_interp_constr_entry" stg_interp_constr_entry :: Ptr ()
251 -- and the 8 vectored ones
252 foreign import ccall "&stg_interp_constr1_entry" stg_interp_constr1_entry :: Ptr ()
253 foreign import ccall "&stg_interp_constr2_entry" stg_interp_constr2_entry :: Ptr ()
254 foreign import ccall "&stg_interp_constr3_entry" stg_interp_constr3_entry :: Ptr ()
255 foreign import ccall "&stg_interp_constr4_entry" stg_interp_constr4_entry :: Ptr ()
256 foreign import ccall "&stg_interp_constr5_entry" stg_interp_constr5_entry :: Ptr ()
257 foreign import ccall "&stg_interp_constr6_entry" stg_interp_constr6_entry :: Ptr ()
258 foreign import ccall "&stg_interp_constr7_entry" stg_interp_constr7_entry :: Ptr ()
259 foreign import ccall "&stg_interp_constr8_entry" stg_interp_constr8_entry :: Ptr ()
265 -- Ultra-minimalist version specially for constructors
266 #if SIZEOF_VOID_P == 8
267 type HalfWord = Word32
269 type HalfWord = Word16
272 data StgInfoTable = StgInfoTable {
280 instance Storable StgInfoTable where
288 fieldSz (head.code) itbl * itblCodeLength]
294 = runState (castPtr a0)
295 $ do store (ptrs itbl)
299 sequence_ (map store (code itbl))
302 = runState (castPtr a0)
307 code <- sequence (replicate itblCodeLength load)
317 fieldSz :: (Storable a, Storable b) => (a -> b) -> a -> Int
318 fieldSz sel x = sizeOf (sel x)
320 newtype State s m a = State (s -> m (s, a))
322 instance Monad m => Monad (State s m) where
323 return a = State (\s -> return (s, a))
324 State m >>= k = State (\s -> m s >>= \(s', a) -> case k a of State n -> n s')
325 fail str = State (\s -> fail str)
327 class (Monad m, Monad (t m)) => MonadT t m where
330 instance Monad m => MonadT (State s) m where
331 lift m = State (\s -> m >>= \a -> return (s, a))
333 runState :: (Monad m) => s -> State s m a -> m a
334 runState s (State m) = m s >>= return . snd
336 type PtrIO = State (Ptr Word8) IO
338 advance :: Storable a => PtrIO (Ptr a)
339 advance = State adv where
340 adv addr = case castPtr addr of { addrCast -> return
341 (addr `plusPtr` sizeOfPointee addrCast, addrCast) }
343 sizeOfPointee :: (Storable a) => Ptr a -> Int
344 sizeOfPointee addr = sizeOf (typeHack addr)
345 where typeHack = undefined :: Ptr a -> a
347 store :: Storable a => a -> PtrIO ()
348 store x = do addr <- advance
351 load :: Storable a => PtrIO a
352 load = do addr <- advance
355 foreign import ccall unsafe "allocateExec"
356 _allocateExec :: CUInt -> IO (Ptr a)
358 malloc_exec :: Int -> IO (Ptr a)
359 malloc_exec bytes = _allocateExec (fromIntegral bytes)