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 #ifndef GHCI_TABLES_NEXT_TO_CODE
98 ptrs = fromIntegral ptrs,
99 nptrs = fromIntegral nptrs_really,
100 tipe = fromIntegral cONSTR,
101 srtlen = fromIntegral conNo
102 #ifdef GHCI_TABLES_NEXT_TO_CODE
106 -- Make a piece of code to jump to "entry_label".
107 -- This is the only arch-dependent bit.
108 code = mkJumpToAddr entry_addr
110 do addr <- malloc_exec (sizeOf itbl)
111 --putStrLn ("SIZE of itbl is " ++ show (sizeOf itbl))
112 --putStrLn ("# ptrs of itbl is " ++ show ptrs)
113 --putStrLn ("# nptrs of itbl is " ++ show nptrs_really)
115 return (getName dcon, addr
116 #ifdef GHCI_TABLES_NEXT_TO_CODE
117 `plusPtr` (2 * wORD_SIZE)
122 -- Make code which causes a jump to the given address. This is the
123 -- only arch-dependent bit of the itbl story. The returned list is
124 -- itblCodeLength elements (bytes) long.
126 -- For sparc_TARGET_ARCH, i386_TARGET_ARCH, etc.
127 #include "nativeGen/NCG.h"
129 itblCodeLength :: Int
130 itblCodeLength = length (mkJumpToAddr undefined)
132 mkJumpToAddr :: Ptr () -> [ItblCode]
134 ptrToInt (Ptr a#) = I# (addr2Int# a#)
136 #if sparc_TARGET_ARCH
137 -- After some consideration, we'll try this, where
138 -- 0x55555555 stands in for the address to jump to.
139 -- According to ghc/includes/MachRegs.h, %g3 is very
140 -- likely indeed to be baggable.
142 -- 0000 07155555 sethi %hi(0x55555555), %g3
143 -- 0004 8610E155 or %g3, %lo(0x55555555), %g3
144 -- 0008 81C0C000 jmp %g3
147 type ItblCode = Word32
149 = let w32 = fromIntegral (ptrToInt a)
151 hi22, lo10 :: Word32 -> Word32
153 hi22 x = (x `shiftR` 10) .&. 0x3FFFF
155 in [ 0x07000000 .|. (hi22 w32),
156 0x8610E000 .|. (lo10 w32),
160 #elif powerpc_TARGET_ARCH
161 -- We'll use r12, for no particular reason.
162 -- 0xDEADBEEF stands for the adress:
163 -- 3D80DEAD lis r12,0xDEAD
164 -- 618CBEEF ori r12,r12,0xBEEF
165 -- 7D8903A6 mtctr r12
168 type ItblCode = Word32
170 let w32 = fromIntegral (ptrToInt a)
171 hi16 x = (x `shiftR` 16) .&. 0xFFFF
172 lo16 x = x .&. 0xFFFF
174 0x3D800000 .|. hi16 w32,
175 0x618C0000 .|. lo16 w32,
176 0x7D8903A6, 0x4E800420
179 #elif i386_TARGET_ARCH
180 -- Let the address to jump to be 0xWWXXYYZZ.
181 -- Generate movl $0xWWXXYYZZ,%eax ; jmp *%eax
183 -- B8 ZZ YY XX WW FF E0
185 type ItblCode = Word8
187 = let w32 = fromIntegral (ptrToInt a) :: Word32
190 = [0xB8, byte0 w32, byte1 w32,
191 byte2 w32, byte3 w32,
196 #elif x86_64_TARGET_ARCH
203 -- We need a full 64-bit pointer (we can't assume the info table is
204 -- allocated in low memory). Assuming the info pointer is aligned to
205 -- an 8-byte boundary, the addr will also be aligned.
207 type ItblCode = Word8
209 = let w64 = fromIntegral (ptrToInt a) :: Word64
212 = [0xff, 0x25, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
213 byte0 w64, byte1 w64, byte2 w64, byte3 w64,
214 byte4 w64, byte5 w64, byte6 w64, byte7 w64]
218 #elif alpha_TARGET_ARCH
219 type ItblCode = Word32
221 = [ 0xc3800000 -- br at, .+4
222 , 0xa79c000c -- ldq at, 12(at)
223 , 0x6bfc0000 -- jmp (at) # with zero hint -- oh well
225 , fromIntegral (w64 .&. 0x0000FFFF)
226 , fromIntegral ((w64 `shiftR` 32) .&. 0x0000FFFF) ]
227 where w64 = fromIntegral (ptrToInt a) :: Word64
230 type ItblCode = Word32
236 byte0, byte1, byte2, byte3, byte4, byte5, byte6, byte7
237 :: (Integral w, Bits w) => w -> Word8
238 byte0 w = fromIntegral w
239 byte1 w = fromIntegral (w `shiftR` 8)
240 byte2 w = fromIntegral (w `shiftR` 16)
241 byte3 w = fromIntegral (w `shiftR` 24)
242 byte4 w = fromIntegral (w `shiftR` 32)
243 byte5 w = fromIntegral (w `shiftR` 40)
244 byte6 w = fromIntegral (w `shiftR` 48)
245 byte7 w = fromIntegral (w `shiftR` 56)
248 vecret_entry 0 = stg_interp_constr1_entry
249 vecret_entry 1 = stg_interp_constr2_entry
250 vecret_entry 2 = stg_interp_constr3_entry
251 vecret_entry 3 = stg_interp_constr4_entry
252 vecret_entry 4 = stg_interp_constr5_entry
253 vecret_entry 5 = stg_interp_constr6_entry
254 vecret_entry 6 = stg_interp_constr7_entry
255 vecret_entry 7 = stg_interp_constr8_entry
258 -- entry point for direct returns for created constr itbls
259 foreign import ccall "&stg_interp_constr_entry" stg_interp_constr_entry :: Ptr ()
260 -- and the 8 vectored ones
261 foreign import ccall "&stg_interp_constr1_entry" stg_interp_constr1_entry :: Ptr ()
262 foreign import ccall "&stg_interp_constr2_entry" stg_interp_constr2_entry :: Ptr ()
263 foreign import ccall "&stg_interp_constr3_entry" stg_interp_constr3_entry :: Ptr ()
264 foreign import ccall "&stg_interp_constr4_entry" stg_interp_constr4_entry :: Ptr ()
265 foreign import ccall "&stg_interp_constr5_entry" stg_interp_constr5_entry :: Ptr ()
266 foreign import ccall "&stg_interp_constr6_entry" stg_interp_constr6_entry :: Ptr ()
267 foreign import ccall "&stg_interp_constr7_entry" stg_interp_constr7_entry :: Ptr ()
268 foreign import ccall "&stg_interp_constr8_entry" stg_interp_constr8_entry :: Ptr ()
274 -- Ultra-minimalist version specially for constructors
275 #if SIZEOF_VOID_P == 8
276 type HalfWord = Word32
278 type HalfWord = Word16
281 data StgInfoTable = StgInfoTable {
282 #ifndef GHCI_TABLES_NEXT_TO_CODE
289 #ifdef GHCI_TABLES_NEXT_TO_CODE
294 instance Storable StgInfoTable where
299 #ifndef GHCI_TABLES_NEXT_TO_CODE
306 #ifdef GHCI_TABLES_NEXT_TO_CODE
307 ,fieldSz (head.code) itbl * itblCodeLength
315 = runState (castPtr a0)
317 #ifndef GHCI_TABLES_NEXT_TO_CODE
324 #ifdef GHCI_TABLES_NEXT_TO_CODE
325 sequence_ (map store (code itbl))
329 = runState (castPtr a0)
331 #ifndef GHCI_TABLES_NEXT_TO_CODE
338 #ifdef GHCI_TABLES_NEXT_TO_CODE
339 code <- sequence (replicate itblCodeLength load)
343 #ifndef GHCI_TABLES_NEXT_TO_CODE
350 #ifdef GHCI_TABLES_NEXT_TO_CODE
355 fieldSz :: (Storable a, Storable b) => (a -> b) -> a -> Int
356 fieldSz sel x = sizeOf (sel x)
358 newtype State s m a = State (s -> m (s, a))
360 instance Monad m => Monad (State s m) where
361 return a = State (\s -> return (s, a))
362 State m >>= k = State (\s -> m s >>= \(s', a) -> case k a of State n -> n s')
363 fail str = State (\s -> fail str)
365 class (Monad m, Monad (t m)) => MonadT t m where
368 instance Monad m => MonadT (State s) m where
369 lift m = State (\s -> m >>= \a -> return (s, a))
371 runState :: (Monad m) => s -> State s m a -> m a
372 runState s (State m) = m s >>= return . snd
374 type PtrIO = State (Ptr Word8) IO
376 advance :: Storable a => PtrIO (Ptr a)
377 advance = State adv where
378 adv addr = case castPtr addr of { addrCast -> return
379 (addr `plusPtr` sizeOfPointee addrCast, addrCast) }
381 sizeOfPointee :: (Storable a) => Ptr a -> Int
382 sizeOfPointee addr = sizeOf (typeHack addr)
383 where typeHack = undefined :: Ptr a -> a
385 store :: Storable a => a -> PtrIO ()
386 store x = do addr <- advance
389 load :: Storable a => PtrIO a
390 load = do addr <- advance
393 foreign import ccall unsafe "allocateExec"
394 _allocateExec :: CUInt -> IO (Ptr a)
396 malloc_exec :: Int -> IO (Ptr a)
397 malloc_exec bytes = _allocateExec (fromIntegral bytes)