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(..), itblCode, mkITbls
13 #include "HsVersions.h"
15 import ByteCodeFFI ( newExec )
16 import Name ( Name, getName )
18 import SMRep ( typeCgRep )
19 import DataCon ( DataCon, dataConRepArgTys, dataConIdentity )
20 import TyCon ( TyCon, tyConFamilySize, isDataTyCon, tyConDataCons )
21 import Constants ( mIN_PAYLOAD_SIZE, wORD_SIZE )
22 import CgHeapery ( mkVirtHeapOffsets )
23 import FastString ( FastString(..) )
24 import Util ( lengthIs, listLengthCmp )
28 import Foreign.C.String
29 import Data.Bits ( Bits(..), shiftR )
31 import GHC.Exts ( Int(I#), addr2Int# )
32 import GHC.Ptr ( Ptr(..) )
38 %************************************************************************
40 \subsection{Manufacturing of info tables for DataCons}
42 %************************************************************************
45 newtype ItblPtr = ItblPtr (Ptr ()) deriving Show
47 itblCode :: ItblPtr -> Ptr ()
48 itblCode (ItblPtr ptr)
50 #ifdef GHCI_TABLES_NEXT_TO_CODE
51 `plusPtr` (3 * wORD_SIZE)
54 type ItblEnv = NameEnv (Name, ItblPtr)
55 -- We need the Name in the range so we know which
56 -- elements to filter out when unloading a module
58 mkItblEnv :: [(Name,ItblPtr)] -> ItblEnv
59 mkItblEnv pairs = mkNameEnv [(n, (n,p)) | (n,p) <- pairs]
62 -- Make info tables for the data decls in this module
63 mkITbls :: [TyCon] -> IO ItblEnv
64 mkITbls [] = return emptyNameEnv
65 mkITbls (tc:tcs) = do itbls <- mkITbl tc
67 return (itbls `plusNameEnv` itbls2)
69 mkITbl :: TyCon -> IO ItblEnv
71 | not (isDataTyCon tc)
73 | dcs `lengthIs` n -- paranoia; this is an assertion.
74 = make_constr_itbls dcs
76 dcs = tyConDataCons tc
77 n = tyConFamilySize tc
79 #include "../includes/ClosureTypes.h"
80 cONSTR :: Int -- Defined in ClosureTypes.h
83 -- Assumes constructors are numbered from zero, not one
84 make_constr_itbls :: [DataCon] -> IO ItblEnv
85 make_constr_itbls cons
86 | listLengthCmp cons 8 /= GT -- <= 8 elements in the list
87 = do is <- mapM mk_vecret_itbl (zip cons [0..])
90 = do is <- mapM mk_dirret_itbl (zip cons [0..])
93 mk_vecret_itbl (dcon, conNo)
94 = mk_itbl dcon conNo (vecret_entry conNo)
95 mk_dirret_itbl (dcon, conNo)
96 = mk_itbl dcon conNo stg_interp_constr_entry
98 mk_itbl :: DataCon -> Int -> Ptr () -> IO (Name,ItblPtr)
99 mk_itbl dcon conNo entry_addr = do
100 let rep_args = [ (typeCgRep arg,arg) | arg <- dataConRepArgTys dcon ]
101 (tot_wds, ptr_wds, _) = mkVirtHeapOffsets False{-not a THUNK-} rep_args
104 nptrs = tot_wds - ptr_wds
106 | ptrs + nptrs >= mIN_PAYLOAD_SIZE = nptrs
107 | otherwise = mIN_PAYLOAD_SIZE - ptrs
108 code = mkJumpToAddr entry_addr
109 itbl = StgInfoTable {
110 #ifndef GHCI_TABLES_NEXT_TO_CODE
113 ptrs = fromIntegral ptrs,
114 nptrs = fromIntegral nptrs_really,
115 tipe = fromIntegral cONSTR,
116 srtlen = fromIntegral conNo
117 #ifdef GHCI_TABLES_NEXT_TO_CODE
121 qNameCString <- newCString $ dataConIdentity dcon
122 let conInfoTbl = StgConInfoTable {
123 conDesc = qNameCString,
126 -- Make a piece of code to jump to "entry_label".
127 -- This is the only arch-dependent bit.
128 addrCon <- newExec [conInfoTbl]
129 --putStrLn ("SIZE of itbl is " ++ show (sizeOf itbl))
130 --putStrLn ("# ptrs of itbl is " ++ show ptrs)
131 --putStrLn ("# nptrs of itbl is " ++ show nptrs_really)
132 return (getName dcon, ItblPtr (castFunPtrToPtr addrCon))
135 -- Make code which causes a jump to the given address. This is the
136 -- only arch-dependent bit of the itbl story. The returned list is
137 -- itblCodeLength elements (bytes) long.
139 -- For sparc_TARGET_ARCH, i386_TARGET_ARCH, etc.
140 #include "nativeGen/NCG.h"
142 itblCodeLength :: Int
143 itblCodeLength = length (mkJumpToAddr undefined)
145 mkJumpToAddr :: Ptr () -> [ItblCode]
147 ptrToInt (Ptr a#) = I# (addr2Int# a#)
149 #if sparc_TARGET_ARCH
150 -- After some consideration, we'll try this, where
151 -- 0x55555555 stands in for the address to jump to.
152 -- According to ghc/includes/MachRegs.h, %g3 is very
153 -- likely indeed to be baggable.
155 -- 0000 07155555 sethi %hi(0x55555555), %g3
156 -- 0004 8610E155 or %g3, %lo(0x55555555), %g3
157 -- 0008 81C0C000 jmp %g3
160 type ItblCode = Word32
162 = let w32 = fromIntegral (ptrToInt a)
164 hi22, lo10 :: Word32 -> Word32
166 hi22 x = (x `shiftR` 10) .&. 0x3FFFF
168 in [ 0x07000000 .|. (hi22 w32),
169 0x8610E000 .|. (lo10 w32),
173 #elif powerpc_TARGET_ARCH
174 -- We'll use r12, for no particular reason.
175 -- 0xDEADBEEF stands for the adress:
176 -- 3D80DEAD lis r12,0xDEAD
177 -- 618CBEEF ori r12,r12,0xBEEF
178 -- 7D8903A6 mtctr r12
181 type ItblCode = Word32
183 let w32 = fromIntegral (ptrToInt a)
184 hi16 x = (x `shiftR` 16) .&. 0xFFFF
185 lo16 x = x .&. 0xFFFF
187 0x3D800000 .|. hi16 w32,
188 0x618C0000 .|. lo16 w32,
189 0x7D8903A6, 0x4E800420
192 #elif i386_TARGET_ARCH
193 -- Let the address to jump to be 0xWWXXYYZZ.
194 -- Generate movl $0xWWXXYYZZ,%eax ; jmp *%eax
196 -- B8 ZZ YY XX WW FF E0
198 type ItblCode = Word8
200 = let w32 = fromIntegral (ptrToInt a) :: Word32
203 = [0xB8, byte0 w32, byte1 w32,
204 byte2 w32, byte3 w32,
209 #elif x86_64_TARGET_ARCH
216 -- We need a full 64-bit pointer (we can't assume the info table is
217 -- allocated in low memory). Assuming the info pointer is aligned to
218 -- an 8-byte boundary, the addr will also be aligned.
220 type ItblCode = Word8
222 = let w64 = fromIntegral (ptrToInt a) :: Word64
225 = [0xff, 0x25, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
226 byte0 w64, byte1 w64, byte2 w64, byte3 w64,
227 byte4 w64, byte5 w64, byte6 w64, byte7 w64]
231 #elif alpha_TARGET_ARCH
232 type ItblCode = Word32
234 = [ 0xc3800000 -- br at, .+4
235 , 0xa79c000c -- ldq at, 12(at)
236 , 0x6bfc0000 -- jmp (at) # with zero hint -- oh well
238 , fromIntegral (w64 .&. 0x0000FFFF)
239 , fromIntegral ((w64 `shiftR` 32) .&. 0x0000FFFF) ]
240 where w64 = fromIntegral (ptrToInt a) :: Word64
243 type ItblCode = Word32
249 byte0, byte1, byte2, byte3, byte4, byte5, byte6, byte7
250 :: (Integral w, Bits w) => w -> Word8
251 byte0 w = fromIntegral w
252 byte1 w = fromIntegral (w `shiftR` 8)
253 byte2 w = fromIntegral (w `shiftR` 16)
254 byte3 w = fromIntegral (w `shiftR` 24)
255 byte4 w = fromIntegral (w `shiftR` 32)
256 byte5 w = fromIntegral (w `shiftR` 40)
257 byte6 w = fromIntegral (w `shiftR` 48)
258 byte7 w = fromIntegral (w `shiftR` 56)
261 vecret_entry 0 = stg_interp_constr1_entry
262 vecret_entry 1 = stg_interp_constr2_entry
263 vecret_entry 2 = stg_interp_constr3_entry
264 vecret_entry 3 = stg_interp_constr4_entry
265 vecret_entry 4 = stg_interp_constr5_entry
266 vecret_entry 5 = stg_interp_constr6_entry
267 vecret_entry 6 = stg_interp_constr7_entry
268 vecret_entry 7 = stg_interp_constr8_entry
271 -- entry point for direct returns for created constr itbls
272 foreign import ccall "&stg_interp_constr_entry" stg_interp_constr_entry :: Ptr ()
273 -- and the 8 vectored ones
274 foreign import ccall "&stg_interp_constr1_entry" stg_interp_constr1_entry :: Ptr ()
275 foreign import ccall "&stg_interp_constr2_entry" stg_interp_constr2_entry :: Ptr ()
276 foreign import ccall "&stg_interp_constr3_entry" stg_interp_constr3_entry :: Ptr ()
277 foreign import ccall "&stg_interp_constr4_entry" stg_interp_constr4_entry :: Ptr ()
278 foreign import ccall "&stg_interp_constr5_entry" stg_interp_constr5_entry :: Ptr ()
279 foreign import ccall "&stg_interp_constr6_entry" stg_interp_constr6_entry :: Ptr ()
280 foreign import ccall "&stg_interp_constr7_entry" stg_interp_constr7_entry :: Ptr ()
281 foreign import ccall "&stg_interp_constr8_entry" stg_interp_constr8_entry :: Ptr ()
287 -- Ultra-minimalist version specially for constructors
288 #if SIZEOF_VOID_P == 8
289 type HalfWord = Word32
291 type HalfWord = Word16
294 data StgConInfoTable = StgConInfoTable {
296 infoTable :: StgInfoTable
299 instance Storable StgConInfoTable where
301 = sum [ sizeOf (conDesc conInfoTable)
302 , sizeOf (infoTable conInfoTable) ]
303 alignment conInfoTable = SIZEOF_VOID_P
305 = runState (castPtr ptr) $ do
314 = runState (castPtr ptr) $ do
316 store (infoTable itbl)
318 data StgInfoTable = StgInfoTable {
319 #ifndef GHCI_TABLES_NEXT_TO_CODE
326 #ifdef GHCI_TABLES_NEXT_TO_CODE
331 instance Storable StgInfoTable where
336 #ifndef GHCI_TABLES_NEXT_TO_CODE
343 #ifdef GHCI_TABLES_NEXT_TO_CODE
344 ,fieldSz (head.code) itbl * itblCodeLength
352 = runState (castPtr a0)
354 #ifndef GHCI_TABLES_NEXT_TO_CODE
361 #ifdef GHCI_TABLES_NEXT_TO_CODE
362 sequence_ (map store (code itbl))
366 = runState (castPtr a0)
368 #ifndef GHCI_TABLES_NEXT_TO_CODE
375 #ifdef GHCI_TABLES_NEXT_TO_CODE
376 code <- sequence (replicate itblCodeLength load)
380 #ifndef GHCI_TABLES_NEXT_TO_CODE
387 #ifdef GHCI_TABLES_NEXT_TO_CODE
392 fieldSz :: (Storable a, Storable b) => (a -> b) -> a -> Int
393 fieldSz sel x = sizeOf (sel x)
395 newtype State s m a = State (s -> m (s, a))
397 instance Monad m => Monad (State s m) where
398 return a = State (\s -> return (s, a))
399 State m >>= k = State (\s -> m s >>= \(s', a) -> case k a of State n -> n s')
400 fail str = State (\s -> fail str)
402 class (Monad m, Monad (t m)) => MonadT t m where
405 instance Monad m => MonadT (State s) m where
406 lift m = State (\s -> m >>= \a -> return (s, a))
408 runState :: (Monad m) => s -> State s m a -> m a
409 runState s (State m) = m s >>= return . snd
411 type PtrIO = State (Ptr Word8) IO
413 advance :: Storable a => PtrIO (Ptr a)
414 advance = State adv where
415 adv addr = case castPtr addr of { addrCast -> return
416 (addr `plusPtr` sizeOfPointee addrCast, addrCast) }
418 sizeOfPointee :: (Storable a) => Ptr a -> Int
419 sizeOfPointee addr = sizeOf (typeHack addr)
420 where typeHack = undefined :: Ptr a -> a
422 store :: Storable a => a -> PtrIO ()
423 store x = do addr <- advance
426 load :: Storable a => PtrIO a
427 load = do addr <- advance