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/Commentary/CodingStyle#Warnings
16 module ByteCodeItbls ( ItblEnv, ItblPtr(..), itblCode, mkITbls
20 #include "HsVersions.h"
22 import Name ( Name, getName )
24 import SMRep ( typeCgRep )
25 import DataCon ( DataCon, dataConRepArgTys, dataConIdentity )
26 import TyCon ( TyCon, tyConFamilySize, isDataTyCon, tyConDataCons )
27 import Constants ( mIN_PAYLOAD_SIZE, wORD_SIZE )
28 import CgHeapery ( mkVirtHeapOffsets )
29 import FastString ( FastString(..) )
35 import Foreign.C.String
36 import Data.Bits ( Bits(..), shiftR )
38 import GHC.Exts ( Int(I#), addr2Int# )
39 import GHC.Ptr ( Ptr(..) )
45 %************************************************************************
47 \subsection{Manufacturing of info tables for DataCons}
49 %************************************************************************
52 newtype ItblPtr = ItblPtr (Ptr ()) deriving Show
54 itblCode :: ItblPtr -> Ptr ()
55 itblCode (ItblPtr ptr)
56 | ghciTablesNextToCode = castPtr ptr `plusPtr` conInfoTableSizeB
57 | otherwise = castPtr ptr
60 conInfoTableSizeB = 3 * wORD_SIZE
62 type ItblEnv = NameEnv (Name, ItblPtr)
63 -- We need the Name in the range so we know which
64 -- elements to filter out when unloading a module
66 mkItblEnv :: [(Name,ItblPtr)] -> ItblEnv
67 mkItblEnv pairs = mkNameEnv [(n, (n,p)) | (n,p) <- pairs]
70 -- Make info tables for the data decls in this module
71 mkITbls :: [TyCon] -> IO ItblEnv
72 mkITbls [] = return emptyNameEnv
73 mkITbls (tc:tcs) = do itbls <- mkITbl tc
75 return (itbls `plusNameEnv` itbls2)
77 mkITbl :: TyCon -> IO ItblEnv
79 | not (isDataTyCon tc)
81 | dcs `lengthIs` n -- paranoia; this is an assertion.
82 = make_constr_itbls dcs
84 dcs = tyConDataCons tc
85 n = tyConFamilySize tc
87 #include "../includes/rts/storage/ClosureTypes.h"
88 cONSTR :: Int -- Defined in ClosureTypes.h
91 -- Assumes constructors are numbered from zero, not one
92 make_constr_itbls :: [DataCon] -> IO ItblEnv
93 make_constr_itbls cons
94 = do is <- mapM mk_dirret_itbl (zip cons [0..])
97 mk_dirret_itbl (dcon, conNo)
98 = mk_itbl dcon conNo stg_interp_constr_entry
100 mk_itbl :: DataCon -> Int -> Ptr () -> IO (Name,ItblPtr)
101 mk_itbl dcon conNo entry_addr = do
102 let rep_args = [ (typeCgRep arg,arg) | arg <- dataConRepArgTys dcon ]
103 (tot_wds, ptr_wds, _) = mkVirtHeapOffsets False{-not a THUNK-} rep_args
106 nptrs = tot_wds - ptr_wds
108 | ptrs + nptrs >= mIN_PAYLOAD_SIZE = nptrs
109 | otherwise = mIN_PAYLOAD_SIZE - ptrs
110 code = mkJumpToAddr entry_addr
111 itbl = StgInfoTable {
112 #ifndef GHCI_TABLES_NEXT_TO_CODE
115 ptrs = fromIntegral ptrs,
116 nptrs = fromIntegral nptrs_really,
117 tipe = fromIntegral cONSTR,
118 srtlen = fromIntegral conNo
119 #ifdef GHCI_TABLES_NEXT_TO_CODE
123 qNameCString <- newArray0 0 $ dataConIdentity dcon
124 let conInfoTbl = StgConInfoTable {
125 conDesc = qNameCString,
128 -- Make a piece of code to jump to "entry_label".
129 -- This is the only arch-dependent bit.
130 addrCon <- newExec pokeConItbl conInfoTbl
131 --putStrLn ("SIZE of itbl is " ++ show (sizeOf itbl))
132 --putStrLn ("# ptrs of itbl is " ++ show ptrs)
133 --putStrLn ("# nptrs of itbl is " ++ show nptrs_really)
134 return (getName dcon, ItblPtr (castFunPtrToPtr addrCon))
137 -- Make code which causes a jump to the given address. This is the
138 -- only arch-dependent bit of the itbl story. The returned list is
139 -- itblCodeLength elements (bytes) long.
141 -- For sparc_TARGET_ARCH, i386_TARGET_ARCH, etc.
142 #include "nativeGen/NCG.h"
144 itblCodeLength :: Int
145 itblCodeLength = length (mkJumpToAddr undefined)
147 mkJumpToAddr :: Ptr () -> [ItblCode]
149 ptrToInt (Ptr a#) = I# (addr2Int# a#)
151 #if sparc_TARGET_ARCH
152 -- After some consideration, we'll try this, where
153 -- 0x55555555 stands in for the address to jump to.
154 -- According to includes/rts/MachRegs.h, %g3 is very
155 -- likely indeed to be baggable.
157 -- 0000 07155555 sethi %hi(0x55555555), %g3
158 -- 0004 8610E155 or %g3, %lo(0x55555555), %g3
159 -- 0008 81C0C000 jmp %g3
162 type ItblCode = Word32
164 = let w32 = fromIntegral (ptrToInt a)
166 hi22, lo10 :: Word32 -> Word32
168 hi22 x = (x `shiftR` 10) .&. 0x3FFFF
170 in [ 0x07000000 .|. (hi22 w32),
171 0x8610E000 .|. (lo10 w32),
175 #elif powerpc_TARGET_ARCH
176 -- We'll use r12, for no particular reason.
177 -- 0xDEADBEEF stands for the adress:
178 -- 3D80DEAD lis r12,0xDEAD
179 -- 618CBEEF ori r12,r12,0xBEEF
180 -- 7D8903A6 mtctr r12
183 type ItblCode = Word32
185 let w32 = fromIntegral (ptrToInt a)
186 hi16 x = (x `shiftR` 16) .&. 0xFFFF
187 lo16 x = x .&. 0xFFFF
189 0x3D800000 .|. hi16 w32,
190 0x618C0000 .|. lo16 w32,
191 0x7D8903A6, 0x4E800420
194 #elif i386_TARGET_ARCH
195 -- Let the address to jump to be 0xWWXXYYZZ.
196 -- Generate movl $0xWWXXYYZZ,%eax ; jmp *%eax
198 -- B8 ZZ YY XX WW FF E0
200 type ItblCode = Word8
202 = let w32 = fromIntegral (ptrToInt a) :: Word32
205 = [0xB8, byte0 w32, byte1 w32,
206 byte2 w32, byte3 w32,
211 #elif x86_64_TARGET_ARCH
218 -- We need a full 64-bit pointer (we can't assume the info table is
219 -- allocated in low memory). Assuming the info pointer is aligned to
220 -- an 8-byte boundary, the addr will also be aligned.
222 type ItblCode = Word8
224 = let w64 = fromIntegral (ptrToInt a) :: Word64
227 = [0xff, 0x25, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
228 byte0 w64, byte1 w64, byte2 w64, byte3 w64,
229 byte4 w64, byte5 w64, byte6 w64, byte7 w64]
233 #elif alpha_TARGET_ARCH
234 type ItblCode = Word32
236 = [ 0xc3800000 -- br at, .+4
237 , 0xa79c000c -- ldq at, 12(at)
238 , 0x6bfc0000 -- jmp (at) # with zero hint -- oh well
240 , fromIntegral (w64 .&. 0x0000FFFF)
241 , fromIntegral ((w64 `shiftR` 32) .&. 0x0000FFFF) ]
242 where w64 = fromIntegral (ptrToInt a) :: Word64
245 type ItblCode = Word32
251 byte0, byte1, byte2, byte3, byte4, byte5, byte6, byte7
252 :: (Integral w, Bits w) => w -> Word8
253 byte0 w = fromIntegral w
254 byte1 w = fromIntegral (w `shiftR` 8)
255 byte2 w = fromIntegral (w `shiftR` 16)
256 byte3 w = fromIntegral (w `shiftR` 24)
257 byte4 w = fromIntegral (w `shiftR` 32)
258 byte5 w = fromIntegral (w `shiftR` 40)
259 byte6 w = fromIntegral (w `shiftR` 48)
260 byte7 w = fromIntegral (w `shiftR` 56)
264 -- entry point for direct returns for created constr itbls
265 foreign import ccall "&stg_interp_constr_entry" stg_interp_constr_entry :: Ptr ()
271 -- Ultra-minimalist version specially for constructors
272 #if SIZEOF_VOID_P == 8
273 type HalfWord = Word32
275 type HalfWord = Word16
278 data StgConInfoTable = StgConInfoTable {
279 conDesc :: Ptr Word8,
280 infoTable :: StgInfoTable
283 instance Storable StgConInfoTable where
285 = sum [ sizeOf (conDesc conInfoTable)
286 , sizeOf (infoTable conInfoTable) ]
287 alignment conInfoTable = SIZEOF_VOID_P
289 = runState (castPtr ptr) $ do
290 #ifdef GHCI_TABLES_NEXT_TO_CODE
294 #ifndef GHCI_TABLES_NEXT_TO_CODE
300 #ifdef GHCI_TABLES_NEXT_TO_CODE
301 conDesc = castPtr $ ptr `plusPtr` conInfoTableSizeB `plusPtr` desc
307 poke = error "poke(StgConInfoTable): use pokeConItbl instead"
310 pokeConItbl :: Ptr StgConInfoTable -> Ptr StgConInfoTable -> StgConInfoTable
312 pokeConItbl wr_ptr ex_ptr itbl
313 = runState (castPtr wr_ptr) $ do
314 #ifdef GHCI_TABLES_NEXT_TO_CODE
315 store (conDesc itbl `minusPtr` (ex_ptr `plusPtr` conInfoTableSizeB))
317 store (infoTable itbl)
318 #ifndef GHCI_TABLES_NEXT_TO_CODE
322 data StgInfoTable = StgInfoTable {
323 #ifndef GHCI_TABLES_NEXT_TO_CODE
330 #ifdef GHCI_TABLES_NEXT_TO_CODE
335 instance Storable StgInfoTable where
340 #ifndef GHCI_TABLES_NEXT_TO_CODE
347 #ifdef GHCI_TABLES_NEXT_TO_CODE
348 ,fieldSz (head.code) itbl * itblCodeLength
356 = runState (castPtr a0)
358 #ifndef GHCI_TABLES_NEXT_TO_CODE
365 #ifdef GHCI_TABLES_NEXT_TO_CODE
366 sequence_ (map store (code itbl))
370 = runState (castPtr a0)
372 #ifndef GHCI_TABLES_NEXT_TO_CODE
379 #ifdef GHCI_TABLES_NEXT_TO_CODE
380 code <- sequence (replicate itblCodeLength load)
384 #ifndef GHCI_TABLES_NEXT_TO_CODE
391 #ifdef GHCI_TABLES_NEXT_TO_CODE
396 fieldSz :: (Storable a, Storable b) => (a -> b) -> a -> Int
397 fieldSz sel x = sizeOf (sel x)
399 newtype State s m a = State (s -> m (s, a))
401 instance Monad m => Monad (State s m) where
402 return a = State (\s -> return (s, a))
403 State m >>= k = State (\s -> m s >>= \(s', a) -> case k a of State n -> n s')
404 fail str = State (\s -> fail str)
406 class (Monad m, Monad (t m)) => MonadT t m where
409 instance Monad m => MonadT (State s) m where
410 lift m = State (\s -> m >>= \a -> return (s, a))
412 runState :: (Monad m) => s -> State s m a -> m a
413 runState s (State m) = m s >>= return . snd
415 type PtrIO = State (Ptr Word8) IO
417 advance :: Storable a => PtrIO (Ptr a)
418 advance = State adv where
419 adv addr = case castPtr addr of { addrCast -> return
420 (addr `plusPtr` sizeOfPointee addrCast, addrCast) }
422 sizeOfPointee :: (Storable a) => Ptr a -> Int
423 sizeOfPointee addr = sizeOf (typeHack addr)
424 where typeHack = undefined :: Ptr a -> a
426 store :: Storable a => a -> PtrIO ()
427 store x = do addr <- advance
430 load :: Storable a => PtrIO a
431 load = do addr <- advance
435 newExec :: Storable a => (Ptr a -> Ptr a -> a -> IO ()) -> a -> IO (FunPtr ())
437 = alloca $ \pcode -> do
438 wr_ptr <- _allocateExec (fromIntegral (sizeOf obj)) pcode
440 poke_fn wr_ptr ex_ptr obj
441 return (castPtrToFunPtr ex_ptr)
443 codeSize :: Storable a => a -> [a] -> Int
444 codeSize dummy array = sizeOf(dummy) * length array
446 foreign import ccall unsafe "allocateExec"
447 _allocateExec :: CUInt -> Ptr (Ptr a) -> IO (Ptr a)