2 % (c) The University of Glasgow 2000-2006
4 ByteCodeItbls: Generate infotables for interpreter-made bytecodes
7 {-# OPTIONS -optc-DNON_POSIX_SOURCE #-}
10 -- The above warning supression flag is a temporary kludge.
11 -- While working on this module you are encouraged to remove it and fix
12 -- any warnings in the module. See
13 -- http://hackage.haskell.org/trac/ghc/wiki/CodingStyle#Warnings
16 module ByteCodeItbls ( ItblEnv, ItblPtr(..), itblCode, mkITbls
20 #include "HsVersions.h"
22 import ByteCodeFFI ( newExec )
23 import Name ( Name, getName )
25 import SMRep ( typeCgRep )
26 import DataCon ( DataCon, dataConRepArgTys, dataConIdentity )
27 import TyCon ( TyCon, tyConFamilySize, isDataTyCon, tyConDataCons )
28 import Constants ( mIN_PAYLOAD_SIZE, wORD_SIZE )
29 import CgHeapery ( mkVirtHeapOffsets )
30 import FastString ( FastString(..) )
31 import Util ( lengthIs, listLengthCmp )
36 import Foreign.C.String
37 import Data.Bits ( Bits(..), shiftR )
39 import GHC.Exts ( Int(I#), addr2Int# )
40 import GHC.Ptr ( Ptr(..) )
47 %************************************************************************
49 \subsection{Manufacturing of info tables for DataCons}
51 %************************************************************************
54 newtype ItblPtr = ItblPtr (Ptr ()) deriving Show
56 itblCode :: ItblPtr -> Ptr ()
57 itblCode (ItblPtr ptr)
59 #ifdef GHCI_TABLES_NEXT_TO_CODE
60 `plusPtr` conInfoTableSizeB
64 conInfoTableSizeB = 3 * wORD_SIZE
66 type ItblEnv = NameEnv (Name, ItblPtr)
67 -- We need the Name in the range so we know which
68 -- elements to filter out when unloading a module
70 mkItblEnv :: [(Name,ItblPtr)] -> ItblEnv
71 mkItblEnv pairs = mkNameEnv [(n, (n,p)) | (n,p) <- pairs]
74 -- Make info tables for the data decls in this module
75 mkITbls :: [TyCon] -> IO ItblEnv
76 mkITbls [] = return emptyNameEnv
77 mkITbls (tc:tcs) = do itbls <- mkITbl tc
79 return (itbls `plusNameEnv` itbls2)
81 mkITbl :: TyCon -> IO ItblEnv
83 | not (isDataTyCon tc)
85 | dcs `lengthIs` n -- paranoia; this is an assertion.
86 = make_constr_itbls dcs
88 dcs = tyConDataCons tc
89 n = tyConFamilySize tc
91 #include "../includes/ClosureTypes.h"
92 cONSTR :: Int -- Defined in ClosureTypes.h
95 -- Assumes constructors are numbered from zero, not one
96 make_constr_itbls :: [DataCon] -> IO ItblEnv
97 make_constr_itbls cons
98 = do is <- mapM mk_dirret_itbl (zip cons [0..])
101 mk_dirret_itbl (dcon, conNo)
102 = mk_itbl dcon conNo stg_interp_constr_entry
104 mk_itbl :: DataCon -> Int -> Ptr () -> IO (Name,ItblPtr)
105 mk_itbl dcon conNo entry_addr = do
106 let rep_args = [ (typeCgRep arg,arg) | arg <- dataConRepArgTys dcon ]
107 (tot_wds, ptr_wds, _) = mkVirtHeapOffsets False{-not a THUNK-} rep_args
110 nptrs = tot_wds - ptr_wds
112 | ptrs + nptrs >= mIN_PAYLOAD_SIZE = nptrs
113 | otherwise = mIN_PAYLOAD_SIZE - ptrs
114 code = mkJumpToAddr entry_addr
115 itbl = StgInfoTable {
116 #ifndef GHCI_TABLES_NEXT_TO_CODE
119 ptrs = fromIntegral ptrs,
120 nptrs = fromIntegral nptrs_really,
121 tipe = fromIntegral cONSTR,
122 srtlen = fromIntegral conNo
123 #ifdef GHCI_TABLES_NEXT_TO_CODE
127 qNameCString <- newArray0 0 $ dataConIdentity dcon
128 let conInfoTbl = StgConInfoTable {
129 conDesc = qNameCString,
132 -- Make a piece of code to jump to "entry_label".
133 -- This is the only arch-dependent bit.
134 addrCon <- newExec [conInfoTbl]
135 --putStrLn ("SIZE of itbl is " ++ show (sizeOf itbl))
136 --putStrLn ("# ptrs of itbl is " ++ show ptrs)
137 --putStrLn ("# nptrs of itbl is " ++ show nptrs_really)
138 return (getName dcon, ItblPtr (castFunPtrToPtr addrCon))
141 -- Make code which causes a jump to the given address. This is the
142 -- only arch-dependent bit of the itbl story. The returned list is
143 -- itblCodeLength elements (bytes) long.
145 -- For sparc_TARGET_ARCH, i386_TARGET_ARCH, etc.
146 #include "nativeGen/NCG.h"
148 itblCodeLength :: Int
149 itblCodeLength = length (mkJumpToAddr undefined)
151 mkJumpToAddr :: Ptr () -> [ItblCode]
153 ptrToInt (Ptr a#) = I# (addr2Int# a#)
155 #if sparc_TARGET_ARCH
156 -- After some consideration, we'll try this, where
157 -- 0x55555555 stands in for the address to jump to.
158 -- According to ghc/includes/MachRegs.h, %g3 is very
159 -- likely indeed to be baggable.
161 -- 0000 07155555 sethi %hi(0x55555555), %g3
162 -- 0004 8610E155 or %g3, %lo(0x55555555), %g3
163 -- 0008 81C0C000 jmp %g3
166 type ItblCode = Word32
168 = let w32 = fromIntegral (ptrToInt a)
170 hi22, lo10 :: Word32 -> Word32
172 hi22 x = (x `shiftR` 10) .&. 0x3FFFF
174 in [ 0x07000000 .|. (hi22 w32),
175 0x8610E000 .|. (lo10 w32),
179 #elif powerpc_TARGET_ARCH
180 -- We'll use r12, for no particular reason.
181 -- 0xDEADBEEF stands for the adress:
182 -- 3D80DEAD lis r12,0xDEAD
183 -- 618CBEEF ori r12,r12,0xBEEF
184 -- 7D8903A6 mtctr r12
187 type ItblCode = Word32
189 let w32 = fromIntegral (ptrToInt a)
190 hi16 x = (x `shiftR` 16) .&. 0xFFFF
191 lo16 x = x .&. 0xFFFF
193 0x3D800000 .|. hi16 w32,
194 0x618C0000 .|. lo16 w32,
195 0x7D8903A6, 0x4E800420
198 #elif i386_TARGET_ARCH
199 -- Let the address to jump to be 0xWWXXYYZZ.
200 -- Generate movl $0xWWXXYYZZ,%eax ; jmp *%eax
202 -- B8 ZZ YY XX WW FF E0
204 type ItblCode = Word8
206 = let w32 = fromIntegral (ptrToInt a) :: Word32
209 = [0xB8, byte0 w32, byte1 w32,
210 byte2 w32, byte3 w32,
215 #elif x86_64_TARGET_ARCH
222 -- We need a full 64-bit pointer (we can't assume the info table is
223 -- allocated in low memory). Assuming the info pointer is aligned to
224 -- an 8-byte boundary, the addr will also be aligned.
226 type ItblCode = Word8
228 = let w64 = fromIntegral (ptrToInt a) :: Word64
231 = [0xff, 0x25, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
232 byte0 w64, byte1 w64, byte2 w64, byte3 w64,
233 byte4 w64, byte5 w64, byte6 w64, byte7 w64]
237 #elif alpha_TARGET_ARCH
238 type ItblCode = Word32
240 = [ 0xc3800000 -- br at, .+4
241 , 0xa79c000c -- ldq at, 12(at)
242 , 0x6bfc0000 -- jmp (at) # with zero hint -- oh well
244 , fromIntegral (w64 .&. 0x0000FFFF)
245 , fromIntegral ((w64 `shiftR` 32) .&. 0x0000FFFF) ]
246 where w64 = fromIntegral (ptrToInt a) :: Word64
249 type ItblCode = Word32
255 byte0, byte1, byte2, byte3, byte4, byte5, byte6, byte7
256 :: (Integral w, Bits w) => w -> Word8
257 byte0 w = fromIntegral w
258 byte1 w = fromIntegral (w `shiftR` 8)
259 byte2 w = fromIntegral (w `shiftR` 16)
260 byte3 w = fromIntegral (w `shiftR` 24)
261 byte4 w = fromIntegral (w `shiftR` 32)
262 byte5 w = fromIntegral (w `shiftR` 40)
263 byte6 w = fromIntegral (w `shiftR` 48)
264 byte7 w = fromIntegral (w `shiftR` 56)
268 -- entry point for direct returns for created constr itbls
269 foreign import ccall "&stg_interp_constr_entry" stg_interp_constr_entry :: Ptr ()
275 -- Ultra-minimalist version specially for constructors
276 #if SIZEOF_VOID_P == 8
277 type HalfWord = Word32
279 type HalfWord = Word16
282 data StgConInfoTable = StgConInfoTable {
283 conDesc :: Ptr Word8,
284 infoTable :: StgInfoTable
287 instance Storable StgConInfoTable where
289 = sum [ sizeOf (conDesc conInfoTable)
290 , sizeOf (infoTable conInfoTable) ]
291 alignment conInfoTable = SIZEOF_VOID_P
293 = runState (castPtr ptr) $ do
294 #ifdef GHCI_TABLES_NEXT_TO_CODE
298 #ifndef GHCI_TABLES_NEXT_TO_CODE
304 #ifdef GHCI_TABLES_NEXT_TO_CODE
305 conDesc = castPtr $ ptr `plusPtr` conInfoTableSizeB `plusPtr` desc
312 = runState (castPtr ptr) $ do
313 #ifdef GHCI_TABLES_NEXT_TO_CODE
314 store (conDesc itbl `minusPtr` (ptr `plusPtr` conInfoTableSizeB))
316 store (infoTable itbl)
317 #ifndef GHCI_TABLES_NEXT_TO_CODE
321 data StgInfoTable = StgInfoTable {
322 #ifndef GHCI_TABLES_NEXT_TO_CODE
329 #ifdef GHCI_TABLES_NEXT_TO_CODE
334 instance Storable StgInfoTable where
339 #ifndef GHCI_TABLES_NEXT_TO_CODE
346 #ifdef GHCI_TABLES_NEXT_TO_CODE
347 ,fieldSz (head.code) itbl * itblCodeLength
355 = runState (castPtr a0)
357 #ifndef GHCI_TABLES_NEXT_TO_CODE
364 #ifdef GHCI_TABLES_NEXT_TO_CODE
365 sequence_ (map store (code itbl))
369 = runState (castPtr a0)
371 #ifndef GHCI_TABLES_NEXT_TO_CODE
378 #ifdef GHCI_TABLES_NEXT_TO_CODE
379 code <- sequence (replicate itblCodeLength load)
383 #ifndef GHCI_TABLES_NEXT_TO_CODE
390 #ifdef GHCI_TABLES_NEXT_TO_CODE
395 fieldSz :: (Storable a, Storable b) => (a -> b) -> a -> Int
396 fieldSz sel x = sizeOf (sel x)
398 newtype State s m a = State (s -> m (s, a))
400 instance Monad m => Monad (State s m) where
401 return a = State (\s -> return (s, a))
402 State m >>= k = State (\s -> m s >>= \(s', a) -> case k a of State n -> n s')
403 fail str = State (\s -> fail str)
405 class (Monad m, Monad (t m)) => MonadT t m where
408 instance Monad m => MonadT (State s) m where
409 lift m = State (\s -> m >>= \a -> return (s, a))
411 runState :: (Monad m) => s -> State s m a -> m a
412 runState s (State m) = m s >>= return . snd
414 type PtrIO = State (Ptr Word8) IO
416 advance :: Storable a => PtrIO (Ptr a)
417 advance = State adv where
418 adv addr = case castPtr addr of { addrCast -> return
419 (addr `plusPtr` sizeOfPointee addrCast, addrCast) }
421 sizeOfPointee :: (Storable a) => Ptr a -> Int
422 sizeOfPointee addr = sizeOf (typeHack addr)
423 where typeHack = undefined :: Ptr a -> a
425 store :: Storable a => a -> PtrIO ()
426 store x = do addr <- advance
429 load :: Storable a => PtrIO a
430 load = do addr <- advance