32882819c13a607edb518c65003a0b9817cb9062
[ghc.git] / compiler / ghci / ByteCodeItbls.hs
1 {-# LANGUAGE CPP, MagicHash #-}
2 {-# OPTIONS_GHC -optc-DNON_POSIX_SOURCE #-}
3 --
4 -- (c) The University of Glasgow 2002-2006
5 --
6
7 -- | ByteCodeItbls: Generate infotables for interpreter-made bytecodes
8 module ByteCodeItbls ( ItblEnv, ItblPtr(..), itblCode, mkITbls, peekItbl
9 , StgInfoTable(..)
10 ) where
11
12 #include "HsVersions.h"
13
14 import DynFlags
15 import Panic
16 import Platform
17 import Name ( Name, getName )
18 import NameEnv
19 import DataCon ( DataCon, dataConRepArgTys, dataConIdentity )
20 import TyCon ( TyCon, tyConFamilySize, isDataTyCon, tyConDataCons )
21 import Type ( flattenRepType, repType, typePrimRep )
22 import StgCmmLayout ( mkVirtHeapOffsets )
23 import Util
24
25 import Control.Monad
26 import Control.Monad.Trans.Class
27 import Control.Monad.Trans.State.Strict
28 import Data.Maybe
29 import Foreign
30 import Foreign.C
31
32 import GHC.Exts ( Int(I#), addr2Int# )
33 import GHC.Ptr ( Ptr(..) )
34
35 {-
36 Manufacturing of info tables for DataCons
37 -}
38
39 newtype ItblPtr = ItblPtr (Ptr ()) deriving Show
40
41 itblCode :: DynFlags -> ItblPtr -> Ptr ()
42 itblCode dflags (ItblPtr ptr)
43 | ghciTablesNextToCode = castPtr ptr `plusPtr` conInfoTableSizeB dflags
44 | otherwise = castPtr ptr
45
46 -- XXX bogus
47 conInfoTableSizeB :: DynFlags -> Int
48 conInfoTableSizeB dflags = 3 * wORD_SIZE dflags
49
50 type ItblEnv = NameEnv (Name, ItblPtr)
51 -- We need the Name in the range so we know which
52 -- elements to filter out when unloading a module
53
54 mkItblEnv :: [(Name,ItblPtr)] -> ItblEnv
55 mkItblEnv pairs = mkNameEnv [(n, (n,p)) | (n,p) <- pairs]
56
57
58 -- Make info tables for the data decls in this module
59 mkITbls :: DynFlags -> [TyCon] -> IO ItblEnv
60 mkITbls _ [] = return emptyNameEnv
61 mkITbls dflags (tc:tcs) = do itbls <- mkITbl dflags tc
62 itbls2 <- mkITbls dflags tcs
63 return (itbls `plusNameEnv` itbls2)
64
65 mkITbl :: DynFlags -> TyCon -> IO ItblEnv
66 mkITbl dflags tc
67 | not (isDataTyCon tc)
68 = return emptyNameEnv
69 | dcs `lengthIs` n -- paranoia; this is an assertion.
70 = make_constr_itbls dflags dcs
71 where
72 dcs = tyConDataCons tc
73 n = tyConFamilySize tc
74
75 mkITbl _ _ = error "Unmatched patter in mkITbl: assertion failed!"
76
77 #include "../includes/rts/storage/ClosureTypes.h"
78 cONSTR :: Int -- Defined in ClosureTypes.h
79 cONSTR = CONSTR
80
81 -- Assumes constructors are numbered from zero, not one
82 make_constr_itbls :: DynFlags -> [DataCon] -> IO ItblEnv
83 make_constr_itbls dflags cons
84 = do is <- mapM mk_dirret_itbl (zip cons [0..])
85 return (mkItblEnv is)
86 where
87 mk_dirret_itbl (dcon, conNo)
88 = mk_itbl dcon conNo stg_interp_constr_entry
89
90 mk_itbl :: DataCon -> Int -> Ptr () -> IO (Name,ItblPtr)
91 mk_itbl dcon conNo entry_addr = do
92 let rep_args = [ (typePrimRep rep_arg,rep_arg) | arg <- dataConRepArgTys dcon, rep_arg <- flattenRepType (repType arg) ]
93 (tot_wds, ptr_wds, _) = mkVirtHeapOffsets dflags False{-not a THUNK-} rep_args
94
95 ptrs' = ptr_wds
96 nptrs' = tot_wds - ptr_wds
97 nptrs_really
98 | ptrs' + nptrs' >= mIN_PAYLOAD_SIZE dflags = nptrs'
99 | otherwise = mIN_PAYLOAD_SIZE dflags - ptrs'
100 code' = mkJumpToAddr dflags entry_addr
101 itbl = StgInfoTable {
102 entry = if ghciTablesNextToCode
103 then Nothing
104 else Just entry_addr,
105 ptrs = fromIntegral ptrs',
106 nptrs = fromIntegral nptrs_really,
107 tipe = fromIntegral cONSTR,
108 srtlen = fromIntegral conNo,
109 code = if ghciTablesNextToCode
110 then Just code'
111 else Nothing
112 }
113 qNameCString <- newArray0 0 $ dataConIdentity dcon
114 let conInfoTbl = StgConInfoTable {
115 conDesc = qNameCString,
116 infoTable = itbl
117 }
118 -- Make a piece of code to jump to "entry_label".
119 -- This is the only arch-dependent bit.
120 addrCon <- newExecConItbl dflags conInfoTbl
121 --putStrLn ("SIZE of itbl is " ++ show (sizeOf itbl))
122 --putStrLn ("# ptrs of itbl is " ++ show ptrs)
123 --putStrLn ("# nptrs of itbl is " ++ show nptrs_really)
124 return (getName dcon, ItblPtr (castFunPtrToPtr addrCon))
125
126
127 -- Make code which causes a jump to the given address. This is the
128 -- only arch-dependent bit of the itbl story.
129
130 -- For sparc_TARGET_ARCH, i386_TARGET_ARCH, etc.
131 #include "nativeGen/NCG.h"
132
133 type ItblCodes = Either [Word8] [Word32]
134
135 ptrToInt :: Ptr a -> Int
136 ptrToInt (Ptr a#) = I# (addr2Int# a#)
137
138 mkJumpToAddr :: DynFlags -> Ptr () -> ItblCodes
139 mkJumpToAddr dflags a = case platformArch (targetPlatform dflags) of
140 ArchSPARC ->
141 -- After some consideration, we'll try this, where
142 -- 0x55555555 stands in for the address to jump to.
143 -- According to includes/rts/MachRegs.h, %g3 is very
144 -- likely indeed to be baggable.
145 --
146 -- 0000 07155555 sethi %hi(0x55555555), %g3
147 -- 0004 8610E155 or %g3, %lo(0x55555555), %g3
148 -- 0008 81C0C000 jmp %g3
149 -- 000c 01000000 nop
150
151 let w32 = fromIntegral (ptrToInt a)
152
153 hi22, lo10 :: Word32 -> Word32
154 lo10 x = x .&. 0x3FF
155 hi22 x = (x `shiftR` 10) .&. 0x3FFFF
156
157 in Right [ 0x07000000 .|. (hi22 w32),
158 0x8610E000 .|. (lo10 w32),
159 0x81C0C000,
160 0x01000000 ]
161
162 ArchPPC ->
163 -- We'll use r12, for no particular reason.
164 -- 0xDEADBEEF stands for the address:
165 -- 3D80DEAD lis r12,0xDEAD
166 -- 618CBEEF ori r12,r12,0xBEEF
167 -- 7D8903A6 mtctr r12
168 -- 4E800420 bctr
169
170 let w32 = fromIntegral (ptrToInt a)
171 hi16 x = (x `shiftR` 16) .&. 0xFFFF
172 lo16 x = x .&. 0xFFFF
173 in Right [ 0x3D800000 .|. hi16 w32,
174 0x618C0000 .|. lo16 w32,
175 0x7D8903A6, 0x4E800420 ]
176
177 ArchX86 ->
178 -- Let the address to jump to be 0xWWXXYYZZ.
179 -- Generate movl $0xWWXXYYZZ,%eax ; jmp *%eax
180 -- which is
181 -- B8 ZZ YY XX WW FF E0
182
183 let w32 = fromIntegral (ptrToInt a) :: Word32
184 insnBytes :: [Word8]
185 insnBytes
186 = [0xB8, byte0 w32, byte1 w32,
187 byte2 w32, byte3 w32,
188 0xFF, 0xE0]
189 in
190 Left insnBytes
191
192 ArchX86_64 ->
193 -- Generates:
194 -- jmpq *.L1(%rip)
195 -- .align 8
196 -- .L1:
197 -- .quad <addr>
198 --
199 -- which looks like:
200 -- 8: ff 25 02 00 00 00 jmpq *0x2(%rip) # 10 <f+0x10>
201 -- with addr at 10.
202 --
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.
206
207 let w64 = fromIntegral (ptrToInt a) :: Word64
208 insnBytes :: [Word8]
209 insnBytes
210 = [0xff, 0x25, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
211 byte0 w64, byte1 w64, byte2 w64, byte3 w64,
212 byte4 w64, byte5 w64, byte6 w64, byte7 w64]
213 in
214 Left insnBytes
215
216 ArchAlpha ->
217 let w64 = fromIntegral (ptrToInt a) :: Word64
218 in Right [ 0xc3800000 -- br at, .+4
219 , 0xa79c000c -- ldq at, 12(at)
220 , 0x6bfc0000 -- jmp (at) # with zero hint -- oh well
221 , 0x47ff041f -- nop
222 , fromIntegral (w64 .&. 0x0000FFFF)
223 , fromIntegral ((w64 `shiftR` 32) .&. 0x0000FFFF) ]
224
225 ArchARM { } ->
226 -- Generates Thumb sequence,
227 -- ldr r1, [pc, #0]
228 -- bx r1
229 --
230 -- which looks like:
231 -- 00000000 <.addr-0x8>:
232 -- 0: 4900 ldr r1, [pc] ; 8 <.addr>
233 -- 4: 4708 bx r1
234 let w32 = fromIntegral (ptrToInt a) :: Word32
235 in Left [ 0x49, 0x00
236 , 0x47, 0x08
237 , byte0 w32, byte1 w32, byte2 w32, byte3 w32]
238
239 arch ->
240 panic ("mkJumpToAddr not defined for " ++ show arch)
241
242 byte0, byte1, byte2, byte3 :: (Integral w, Bits w) => w -> Word8
243 byte0 w = fromIntegral w
244 byte1 w = fromIntegral (w `shiftR` 8)
245 byte2 w = fromIntegral (w `shiftR` 16)
246 byte3 w = fromIntegral (w `shiftR` 24)
247 byte4, byte5, byte6, byte7 :: (Integral w, Bits w) => w -> Word8
248 byte4 w = fromIntegral (w `shiftR` 32)
249 byte5 w = fromIntegral (w `shiftR` 40)
250 byte6 w = fromIntegral (w `shiftR` 48)
251 byte7 w = fromIntegral (w `shiftR` 56)
252
253 -- entry point for direct returns for created constr itbls
254 foreign import ccall "&stg_interp_constr_entry" stg_interp_constr_entry :: Ptr ()
255
256
257
258
259 -- Ultra-minimalist version specially for constructors
260 #if SIZEOF_VOID_P == 8
261 type HalfWord = Word32
262 #else
263 type HalfWord = Word16
264 #endif
265
266 data StgConInfoTable = StgConInfoTable {
267 conDesc :: Ptr Word8,
268 infoTable :: StgInfoTable
269 }
270
271 sizeOfConItbl :: DynFlags -> StgConInfoTable -> Int
272 sizeOfConItbl dflags conInfoTable
273 = sum [ fieldSz conDesc conInfoTable
274 , sizeOfItbl dflags (infoTable conInfoTable) ]
275
276 pokeConItbl :: DynFlags -> Ptr StgConInfoTable -> Ptr StgConInfoTable -> StgConInfoTable
277 -> IO ()
278 pokeConItbl dflags wr_ptr ex_ptr itbl
279 = flip evalStateT (castPtr wr_ptr) $ do
280 when ghciTablesNextToCode $
281 store (conDesc itbl `minusPtr` (ex_ptr `plusPtr` conInfoTableSizeB dflags))
282 store' (sizeOfItbl dflags) (pokeItbl dflags) (infoTable itbl)
283 unless ghciTablesNextToCode $ store (conDesc itbl)
284
285 data StgInfoTable = StgInfoTable {
286 entry :: Maybe (Ptr ()), -- Just <=> not ghciTablesNextToCode
287 ptrs :: HalfWord,
288 nptrs :: HalfWord,
289 tipe :: HalfWord,
290 srtlen :: HalfWord,
291 code :: Maybe ItblCodes -- Just <=> ghciTablesNextToCode
292 }
293
294 sizeOfItbl :: DynFlags -> StgInfoTable -> Int
295 sizeOfItbl dflags itbl
296 = sum
297 [
298 if ghciTablesNextToCode then 0 else fieldSz (fromJust . entry) itbl,
299 fieldSz ptrs itbl,
300 fieldSz nptrs itbl,
301 fieldSz tipe itbl,
302 fieldSz srtlen itbl,
303 if ghciTablesNextToCode then case mkJumpToAddr dflags undefined of
304 Left xs -> sizeOf (head xs) * length xs
305 Right xs -> sizeOf (head xs) * length xs
306 else 0
307 ]
308
309 pokeItbl :: DynFlags -> Ptr StgInfoTable -> StgInfoTable -> IO ()
310 pokeItbl _ a0 itbl
311 = flip evalStateT (castPtr a0)
312 $ do
313 case entry itbl of
314 Nothing -> return ()
315 Just e -> store e
316 store (ptrs itbl)
317 store (nptrs itbl)
318 store (tipe itbl)
319 store (srtlen itbl)
320 case code itbl of
321 Nothing -> return ()
322 Just (Left xs) -> mapM_ store xs
323 Just (Right xs) -> mapM_ store xs
324
325 peekItbl :: DynFlags -> Ptr StgInfoTable -> IO StgInfoTable
326 peekItbl dflags a0
327 = flip evalStateT (castPtr a0)
328 $ do
329 entry' <- if ghciTablesNextToCode
330 then return Nothing
331 else liftM Just load
332 ptrs' <- load
333 nptrs' <- load
334 tipe' <- load
335 srtlen' <- load
336 code' <- if ghciTablesNextToCode
337 then liftM Just $ case mkJumpToAddr dflags undefined of
338 Left xs ->
339 liftM Left $ sequence (replicate (length xs) load)
340 Right xs ->
341 liftM Right $ sequence (replicate (length xs) load)
342 else return Nothing
343 return
344 StgInfoTable {
345 entry = entry',
346 ptrs = ptrs',
347 nptrs = nptrs',
348 tipe = tipe',
349 srtlen = srtlen'
350 ,code = code'
351 }
352
353 fieldSz :: Storable b => (a -> b) -> a -> Int
354 fieldSz sel x = sizeOf (sel x)
355
356 type PtrIO = StateT (Ptr Word8) IO
357
358 advance :: Storable a => PtrIO (Ptr a)
359 advance = advance' sizeOf
360
361 advance' :: (a -> Int) -> PtrIO (Ptr a)
362 advance' fSizeOf = state adv
363 where adv addr = case castPtr addr of
364 addrCast ->
365 (addrCast,
366 addr `plusPtr` sizeOfPointee fSizeOf addrCast)
367
368 sizeOfPointee :: (a -> Int) -> Ptr a -> Int
369 sizeOfPointee fSizeOf addr = fSizeOf (typeHack addr)
370 where typeHack = undefined :: Ptr a -> a
371
372 store :: Storable a => a -> PtrIO ()
373 store = store' sizeOf poke
374
375 store' :: (a -> Int) -> (Ptr a -> a -> IO ()) -> a -> PtrIO ()
376 store' fSizeOf fPoke x = do addr <- advance' fSizeOf
377 lift (fPoke addr x)
378
379 load :: Storable a => PtrIO a
380 load = do addr <- advance
381 lift (peek addr)
382
383 newExecConItbl :: DynFlags -> StgConInfoTable -> IO (FunPtr ())
384 newExecConItbl dflags obj
385 = alloca $ \pcode -> do
386 let sz = fromIntegral (sizeOfConItbl dflags obj)
387 wr_ptr <- _allocateExec sz pcode
388 ex_ptr <- peek pcode
389 pokeConItbl dflags wr_ptr ex_ptr obj
390 _flushExec sz ex_ptr -- Cache flush (if needed)
391 return (castPtrToFunPtr ex_ptr)
392
393 foreign import ccall unsafe "allocateExec"
394 _allocateExec :: CUInt -> Ptr (Ptr a) -> IO (Ptr a)
395
396 foreign import ccall unsafe "flushExec"
397 _flushExec :: CUInt -> Ptr a -> IO ()