2 % (c) The University of Glasgow 2000
4 \section[ByteCodeItbls]{Generate infotables for interpreter-made bytecodes}
8 {-# OPTIONS -optc-DNON_POSIX_SOURCE #-}
10 module ByteCodeItbls ( ItblEnv, ItblPtr, mkITbls ) where
12 #include "HsVersions.h"
14 import Name ( Name, getName )
16 import SMRep ( typeCgRep )
17 import DataCon ( DataCon, dataConRepArgTys )
18 import TyCon ( TyCon, tyConFamilySize, isDataTyCon, tyConDataCons )
19 import Constants ( mIN_SIZE_NonUpdHeapObject )
20 import CgHeapery ( mkVirtHeapOffsets )
21 import FastString ( FastString(..) )
22 import Util ( lengthIs, listLengthCmp )
24 import Foreign ( Storable(..), Word8, Word16, Word32, Word64,
25 malloc, castPtr, plusPtr )
26 import DATA_BITS ( Bits(..), shiftR )
28 import GHC.Exts ( Int(I#), addr2Int# )
29 #if __GLASGOW_HASKELL__ < 503
30 import Ptr ( Ptr(..) )
32 import GHC.Ptr ( Ptr(..) )
36 %************************************************************************
38 \subsection{Manufacturing of info tables for DataCons}
40 %************************************************************************
43 type ItblPtr = Ptr StgInfoTable
44 type ItblEnv = NameEnv (Name, ItblPtr)
45 -- We need the Name in the range so we know which
46 -- elements to filter out when unloading a module
48 mkItblEnv :: [(Name,ItblPtr)] -> ItblEnv
49 mkItblEnv pairs = mkNameEnv [(n, (n,p)) | (n,p) <- pairs]
52 -- Make info tables for the data decls in this module
53 mkITbls :: [TyCon] -> IO ItblEnv
54 mkITbls [] = return emptyNameEnv
55 mkITbls (tc:tcs) = do itbls <- mkITbl tc
57 return (itbls `plusNameEnv` itbls2)
59 mkITbl :: TyCon -> IO ItblEnv
61 | not (isDataTyCon tc)
63 | dcs `lengthIs` n -- paranoia; this is an assertion.
64 = make_constr_itbls dcs
66 dcs = tyConDataCons tc
67 n = tyConFamilySize tc
69 #include "../includes/ClosureTypes.h"
70 cONSTR :: Int -- Defined in ClosureTypes.h
73 -- Assumes constructors are numbered from zero, not one
74 make_constr_itbls :: [DataCon] -> IO ItblEnv
75 make_constr_itbls cons
76 | listLengthCmp cons 8 /= GT -- <= 8 elements in the list
77 = do is <- mapM mk_vecret_itbl (zip cons [0..])
80 = do is <- mapM mk_dirret_itbl (zip cons [0..])
83 mk_vecret_itbl (dcon, conNo)
84 = mk_itbl dcon conNo (vecret_entry conNo)
85 mk_dirret_itbl (dcon, conNo)
86 = mk_itbl dcon conNo stg_interp_constr_entry
88 mk_itbl :: DataCon -> Int -> Ptr () -> IO (Name,ItblPtr)
89 mk_itbl dcon conNo entry_addr
90 = let rep_args = [ (typeCgRep arg,arg)
91 | arg <- dataConRepArgTys dcon ]
92 (tot_wds, ptr_wds, _) = mkVirtHeapOffsets rep_args
95 nptrs = tot_wds - ptr_wds
97 | ptrs + nptrs >= mIN_SIZE_NonUpdHeapObject = nptrs
98 | otherwise = mIN_SIZE_NonUpdHeapObject - ptrs
100 ptrs = fromIntegral ptrs,
101 nptrs = fromIntegral nptrs_really,
102 tipe = fromIntegral cONSTR,
103 srtlen = fromIntegral conNo,
106 -- Make a piece of code to jump to "entry_label".
107 -- This is the only arch-dependent bit.
108 code = mkJumpToAddr entry_addr
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 `plusPtr` 8)
118 -- Make code which causes a jump to the given address. This is the
119 -- only arch-dependent bit of the itbl story. The returned list is
120 -- itblCodeLength elements (bytes) long.
122 -- For sparc_TARGET_ARCH, i386_TARGET_ARCH, etc.
123 #include "nativeGen/NCG.h"
125 itblCodeLength :: Int
126 itblCodeLength = length (mkJumpToAddr undefined)
128 mkJumpToAddr :: Ptr () -> [ItblCode]
130 ptrToInt (Ptr a#) = I# (addr2Int# a#)
132 #if sparc_TARGET_ARCH
133 -- After some consideration, we'll try this, where
134 -- 0x55555555 stands in for the address to jump to.
135 -- According to ghc/includes/MachRegs.h, %g3 is very
136 -- likely indeed to be baggable.
138 -- 0000 07155555 sethi %hi(0x55555555), %g3
139 -- 0004 8610E155 or %g3, %lo(0x55555555), %g3
140 -- 0008 81C0C000 jmp %g3
143 type ItblCode = Word32
145 = let w32 = fromIntegral (ptrToInt a)
147 hi22, lo10 :: Word32 -> Word32
149 hi22 x = (x `shiftR` 10) .&. 0x3FFFF
151 in [ 0x07000000 .|. (hi22 w32),
152 0x8610E000 .|. (lo10 w32),
156 #elif powerpc_TARGET_ARCH
157 -- We'll use r12, for no particular reason.
158 -- 0xDEADBEEF stands for the adress:
159 -- 3D80DEAD lis r12,0xDEAD
160 -- 618CBEEF ori r12,r12,0xBEEF
161 -- 7D8903A6 mtctr r12
164 type ItblCode = Word32
166 let w32 = fromIntegral (ptrToInt a)
167 hi16 x = (x `shiftR` 16) .&. 0xFFFF
168 lo16 x = x .&. 0xFFFF
170 0x3D800000 .|. hi16 w32,
171 0x618C0000 .|. lo16 w32,
172 0x7D8903A6, 0x4E800420
175 #elif i386_TARGET_ARCH
176 -- Let the address to jump to be 0xWWXXYYZZ.
177 -- Generate movl $0xWWXXYYZZ,%eax ; jmp *%eax
179 -- B8 ZZ YY XX WW FF E0
181 type ItblCode = Word8
183 = let w32 = fromIntegral (ptrToInt a)
186 = [0xB8, byte 0 w32, byte 1 w32,
187 byte 2 w32, byte 3 w32,
192 #elif alpha_TARGET_ARCH
193 type ItblCode = Word32
195 = [ 0xc3800000 -- br at, .+4
196 , 0xa79c000c -- ldq at, 12(at)
197 , 0x6bfc0000 -- jmp (at) # with zero hint -- oh well
199 , fromIntegral (w64 .&. 0x0000FFFF)
200 , fromIntegral ((w64 `shiftR` 32) .&. 0x0000FFFF) ]
201 where w64 = fromIntegral (ptrToInt a) :: Word64
204 type ItblCode = Word32
210 byte :: Int -> Word32 -> Word8
211 byte 0 w = fromIntegral (w .&. 0xFF)
212 byte 1 w = fromIntegral ((w `shiftR` 8) .&. 0xFF)
213 byte 2 w = fromIntegral ((w `shiftR` 16) .&. 0xFF)
214 byte 3 w = fromIntegral ((w `shiftR` 24) .&. 0xFF)
217 vecret_entry 0 = stg_interp_constr1_entry
218 vecret_entry 1 = stg_interp_constr2_entry
219 vecret_entry 2 = stg_interp_constr3_entry
220 vecret_entry 3 = stg_interp_constr4_entry
221 vecret_entry 4 = stg_interp_constr5_entry
222 vecret_entry 5 = stg_interp_constr6_entry
223 vecret_entry 6 = stg_interp_constr7_entry
224 vecret_entry 7 = stg_interp_constr8_entry
226 -- entry point for direct returns for created constr itbls
227 foreign label "stg_interp_constr_entry" stg_interp_constr_entry :: Ptr ()
228 -- and the 8 vectored ones
229 foreign label "stg_interp_constr1_entry" stg_interp_constr1_entry :: Ptr ()
230 foreign label "stg_interp_constr2_entry" stg_interp_constr2_entry :: Ptr ()
231 foreign label "stg_interp_constr3_entry" stg_interp_constr3_entry :: Ptr ()
232 foreign label "stg_interp_constr4_entry" stg_interp_constr4_entry :: Ptr ()
233 foreign label "stg_interp_constr5_entry" stg_interp_constr5_entry :: Ptr ()
234 foreign label "stg_interp_constr6_entry" stg_interp_constr6_entry :: Ptr ()
235 foreign label "stg_interp_constr7_entry" stg_interp_constr7_entry :: Ptr ()
236 foreign label "stg_interp_constr8_entry" stg_interp_constr8_entry :: Ptr ()
242 -- Ultra-minimalist version specially for constructors
243 #if SIZEOF_VOID_P == 8
244 type HalfWord = Word32
246 type HalfWord = Word16
249 data StgInfoTable = StgInfoTable {
257 instance Storable StgInfoTable where
265 fieldSz (head.code) itbl * itblCodeLength]
271 = runState (castPtr a0)
272 $ do store (ptrs itbl)
276 sequence_ (map store (code itbl))
279 = runState (castPtr a0)
284 code <- sequence (replicate itblCodeLength load)
294 fieldSz :: (Storable a, Storable b) => (a -> b) -> a -> Int
295 fieldSz sel x = sizeOf (sel x)
297 newtype State s m a = State (s -> m (s, a))
299 instance Monad m => Monad (State s m) where
300 return a = State (\s -> return (s, a))
301 State m >>= k = State (\s -> m s >>= \(s', a) -> case k a of State n -> n s')
302 fail str = State (\s -> fail str)
304 class (Monad m, Monad (t m)) => MonadT t m where
307 instance Monad m => MonadT (State s) m where
308 lift m = State (\s -> m >>= \a -> return (s, a))
310 runState :: (Monad m) => s -> State s m a -> m a
311 runState s (State m) = m s >>= return . snd
313 type PtrIO = State (Ptr Word8) IO
315 advance :: Storable a => PtrIO (Ptr a)
316 advance = State adv where
317 adv addr = case castPtr addr of { addrCast -> return
318 (addr `plusPtr` sizeOfPointee addrCast, addrCast) }
320 sizeOfPointee :: (Storable a) => Ptr a -> Int
321 sizeOfPointee addr = sizeOf (typeHack addr)
322 where typeHack = undefined :: Ptr a -> a
324 store :: Storable a => a -> PtrIO ()
325 store x = do addr <- advance
328 load :: Storable a => PtrIO a
329 load = do addr <- advance