872d7289920771142ef4b6e6f764338768c3fb43
[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
114 -- Make a piece of code to jump to "entry_label".
115 -- This is the only arch-dependent bit.
116 addrCon <- newExecConItbl dflags itbl (dataConIdentity dcon)
117 --putStrLn ("SIZE of itbl is " ++ show (sizeOf itbl))
118 --putStrLn ("# ptrs of itbl is " ++ show ptrs)
119 --putStrLn ("# nptrs of itbl is " ++ show nptrs_really)
120 return (getName dcon, ItblPtr (castFunPtrToPtr addrCon))
121
122
123 -- Make code which causes a jump to the given address. This is the
124 -- only arch-dependent bit of the itbl story.
125
126 -- For sparc_TARGET_ARCH, i386_TARGET_ARCH, etc.
127 #include "nativeGen/NCG.h"
128
129 type ItblCodes = Either [Word8] [Word32]
130
131 ptrToInt :: Ptr a -> Int
132 ptrToInt (Ptr a#) = I# (addr2Int# a#)
133
134 mkJumpToAddr :: DynFlags -> Ptr () -> ItblCodes
135 mkJumpToAddr dflags a = case platformArch (targetPlatform dflags) of
136 ArchSPARC ->
137 -- After some consideration, we'll try this, where
138 -- 0x55555555 stands in for the address to jump to.
139 -- According to includes/rts/MachRegs.h, %g3 is very
140 -- likely indeed to be baggable.
141 --
142 -- 0000 07155555 sethi %hi(0x55555555), %g3
143 -- 0004 8610E155 or %g3, %lo(0x55555555), %g3
144 -- 0008 81C0C000 jmp %g3
145 -- 000c 01000000 nop
146
147 let w32 = fromIntegral (ptrToInt a)
148
149 hi22, lo10 :: Word32 -> Word32
150 lo10 x = x .&. 0x3FF
151 hi22 x = (x `shiftR` 10) .&. 0x3FFFF
152
153 in Right [ 0x07000000 .|. (hi22 w32),
154 0x8610E000 .|. (lo10 w32),
155 0x81C0C000,
156 0x01000000 ]
157
158 ArchPPC ->
159 -- We'll use r12, for no particular reason.
160 -- 0xDEADBEEF stands for the address:
161 -- 3D80DEAD lis r12,0xDEAD
162 -- 618CBEEF ori r12,r12,0xBEEF
163 -- 7D8903A6 mtctr r12
164 -- 4E800420 bctr
165
166 let w32 = fromIntegral (ptrToInt a)
167 hi16 x = (x `shiftR` 16) .&. 0xFFFF
168 lo16 x = x .&. 0xFFFF
169 in Right [ 0x3D800000 .|. hi16 w32,
170 0x618C0000 .|. lo16 w32,
171 0x7D8903A6, 0x4E800420 ]
172
173 ArchX86 ->
174 -- Let the address to jump to be 0xWWXXYYZZ.
175 -- Generate movl $0xWWXXYYZZ,%eax ; jmp *%eax
176 -- which is
177 -- B8 ZZ YY XX WW FF E0
178
179 let w32 = fromIntegral (ptrToInt a) :: Word32
180 insnBytes :: [Word8]
181 insnBytes
182 = [0xB8, byte0 w32, byte1 w32,
183 byte2 w32, byte3 w32,
184 0xFF, 0xE0]
185 in
186 Left insnBytes
187
188 ArchX86_64 ->
189 -- Generates:
190 -- jmpq *.L1(%rip)
191 -- .align 8
192 -- .L1:
193 -- .quad <addr>
194 --
195 -- which looks like:
196 -- 8: ff 25 02 00 00 00 jmpq *0x2(%rip) # 10 <f+0x10>
197 -- with addr at 10.
198 --
199 -- We need a full 64-bit pointer (we can't assume the info table is
200 -- allocated in low memory). Assuming the info pointer is aligned to
201 -- an 8-byte boundary, the addr will also be aligned.
202
203 let w64 = fromIntegral (ptrToInt a) :: Word64
204 insnBytes :: [Word8]
205 insnBytes
206 = [0xff, 0x25, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
207 byte0 w64, byte1 w64, byte2 w64, byte3 w64,
208 byte4 w64, byte5 w64, byte6 w64, byte7 w64]
209 in
210 Left insnBytes
211
212 ArchAlpha ->
213 let w64 = fromIntegral (ptrToInt a) :: Word64
214 in Right [ 0xc3800000 -- br at, .+4
215 , 0xa79c000c -- ldq at, 12(at)
216 , 0x6bfc0000 -- jmp (at) # with zero hint -- oh well
217 , 0x47ff041f -- nop
218 , fromIntegral (w64 .&. 0x0000FFFF)
219 , fromIntegral ((w64 `shiftR` 32) .&. 0x0000FFFF) ]
220
221 ArchARM { } ->
222 -- Generates Thumb sequence,
223 -- ldr r1, [pc, #0]
224 -- bx r1
225 --
226 -- which looks like:
227 -- 00000000 <.addr-0x8>:
228 -- 0: 4900 ldr r1, [pc] ; 8 <.addr>
229 -- 4: 4708 bx r1
230 let w32 = fromIntegral (ptrToInt a) :: Word32
231 in Left [ 0x49, 0x00
232 , 0x47, 0x08
233 , byte0 w32, byte1 w32, byte2 w32, byte3 w32]
234
235 arch ->
236 panic ("mkJumpToAddr not defined for " ++ show arch)
237
238 byte0 :: (Integral w) => w -> Word8
239 byte0 w = fromIntegral w
240
241 byte1, byte2, byte3, byte4, byte5, byte6, byte7
242 :: (Integral w, Bits w) => w -> Word8
243 byte1 w = fromIntegral (w `shiftR` 8)
244 byte2 w = fromIntegral (w `shiftR` 16)
245 byte3 w = fromIntegral (w `shiftR` 24)
246 byte4 w = fromIntegral (w `shiftR` 32)
247 byte5 w = fromIntegral (w `shiftR` 40)
248 byte6 w = fromIntegral (w `shiftR` 48)
249 byte7 w = fromIntegral (w `shiftR` 56)
250
251 -- entry point for direct returns for created constr itbls
252 foreign import ccall "&stg_interp_constr_entry" stg_interp_constr_entry :: Ptr ()
253
254
255
256
257 -- Ultra-minimalist version specially for constructors
258 #if SIZEOF_VOID_P == 8
259 type HalfWord = Word32
260 #else
261 type HalfWord = Word16
262 #endif
263
264 data StgConInfoTable = StgConInfoTable {
265 conDesc :: Ptr Word8,
266 infoTable :: StgInfoTable
267 }
268
269 sizeOfConItbl :: DynFlags -> StgConInfoTable -> Int
270 sizeOfConItbl dflags conInfoTable
271 = sum [ fieldSz conDesc conInfoTable
272 , sizeOfItbl dflags (infoTable conInfoTable) ]
273
274 pokeConItbl :: DynFlags -> Ptr StgConInfoTable -> Ptr StgConInfoTable
275 -> StgConInfoTable
276 -> IO ()
277 pokeConItbl dflags wr_ptr ex_ptr itbl
278 = flip evalStateT (castPtr wr_ptr) $ do
279 when ghciTablesNextToCode $ do
280 let con_desc = conDesc itbl `minusPtr`
281 (ex_ptr `plusPtr` conInfoTableSizeB dflags)
282 store (fromIntegral con_desc :: Word32)
283 when (wORD_SIZE dflags == 8) $
284 store (fromIntegral con_desc :: Word32)
285 store' (sizeOfItbl dflags) (pokeItbl dflags) (infoTable itbl)
286 unless ghciTablesNextToCode $ store (conDesc itbl)
287
288 data StgInfoTable = StgInfoTable {
289 entry :: Maybe (Ptr ()), -- Just <=> not ghciTablesNextToCode
290 ptrs :: HalfWord,
291 nptrs :: HalfWord,
292 tipe :: HalfWord,
293 srtlen :: HalfWord,
294 code :: Maybe ItblCodes -- Just <=> ghciTablesNextToCode
295 }
296
297 sizeOfItbl :: DynFlags -> StgInfoTable -> Int
298 sizeOfItbl dflags itbl
299 = sum
300 [
301 if ghciTablesNextToCode then 0 else fieldSz (fromJust . entry) itbl,
302 fieldSz ptrs itbl,
303 fieldSz nptrs itbl,
304 fieldSz tipe itbl,
305 fieldSz srtlen itbl,
306 if ghciTablesNextToCode then case mkJumpToAddr dflags undefined of
307 Left xs -> sizeOf (head xs) * length xs
308 Right xs -> sizeOf (head xs) * length xs
309 else 0
310 ]
311
312 pokeItbl :: DynFlags -> Ptr StgInfoTable -> StgInfoTable -> IO ()
313 pokeItbl _ a0 itbl
314 = flip evalStateT (castPtr a0)
315 $ do
316 case entry itbl of
317 Nothing -> return ()
318 Just e -> store e
319 store (ptrs itbl)
320 store (nptrs itbl)
321 store (tipe itbl)
322 store (srtlen itbl)
323 case code itbl of
324 Nothing -> return ()
325 Just (Left xs) -> mapM_ store xs
326 Just (Right xs) -> mapM_ store xs
327
328 peekItbl :: DynFlags -> Ptr StgInfoTable -> IO StgInfoTable
329 peekItbl dflags a0
330 = flip evalStateT (castPtr a0)
331 $ do
332 entry' <- if ghciTablesNextToCode
333 then return Nothing
334 else liftM Just load
335 ptrs' <- load
336 nptrs' <- load
337 tipe' <- load
338 srtlen' <- load
339 code' <- if ghciTablesNextToCode
340 then liftM Just $ case mkJumpToAddr dflags undefined of
341 Left xs ->
342 liftM Left $ sequence (replicate (length xs) load)
343 Right xs ->
344 liftM Right $ sequence (replicate (length xs) load)
345 else return Nothing
346 return
347 StgInfoTable {
348 entry = entry',
349 ptrs = ptrs',
350 nptrs = nptrs',
351 tipe = tipe',
352 srtlen = srtlen'
353 ,code = code'
354 }
355
356 fieldSz :: Storable b => (a -> b) -> a -> Int
357 fieldSz sel x = sizeOf (sel x)
358
359 type PtrIO = StateT (Ptr Word8) IO
360
361 advance :: Storable a => PtrIO (Ptr a)
362 advance = advance' sizeOf
363
364 advance' :: (a -> Int) -> PtrIO (Ptr a)
365 advance' fSizeOf = state adv
366 where adv addr = case castPtr addr of
367 addrCast ->
368 (addrCast,
369 addr `plusPtr` sizeOfPointee fSizeOf addrCast)
370
371 sizeOfPointee :: (a -> Int) -> Ptr a -> Int
372 sizeOfPointee fSizeOf addr = fSizeOf (typeHack addr)
373 where typeHack = undefined :: Ptr a -> a
374
375 store :: Storable a => a -> PtrIO ()
376 store = store' sizeOf poke
377
378 store' :: (a -> Int) -> (Ptr a -> a -> IO ()) -> a -> PtrIO ()
379 store' fSizeOf fPoke x = do addr <- advance' fSizeOf
380 lift (fPoke addr x)
381
382 load :: Storable a => PtrIO a
383 load = do addr <- advance
384 lift (peek addr)
385
386 newExecConItbl :: DynFlags -> StgInfoTable -> [Word8] -> IO (FunPtr ())
387 newExecConItbl dflags obj con_desc
388 = alloca $ \pcode -> do
389 let lcon_desc = length con_desc + 1{- null terminator -}
390 dummy_cinfo = StgConInfoTable { conDesc = nullPtr, infoTable = obj }
391 sz = fromIntegral (sizeOfConItbl dflags dummy_cinfo)
392 -- Note: we need to allocate the conDesc string next to the info
393 -- table, because on a 64-bit platform we reference this string
394 -- with a 32-bit offset relative to the info table, so if we
395 -- allocated the string separately it might be out of range.
396 wr_ptr <- _allocateExec (sz + fromIntegral lcon_desc) pcode
397 ex_ptr <- peek pcode
398 let cinfo = StgConInfoTable { conDesc = ex_ptr `plusPtr` fromIntegral sz
399 , infoTable = obj }
400 pokeConItbl dflags wr_ptr ex_ptr cinfo
401 pokeArray0 0 (castPtr wr_ptr `plusPtr` fromIntegral sz) con_desc
402 _flushExec sz ex_ptr -- Cache flush (if needed)
403 return (castPtrToFunPtr ex_ptr)
404
405 foreign import ccall unsafe "allocateExec"
406 _allocateExec :: CUInt -> Ptr (Ptr a) -> IO (Ptr a)
407
408 foreign import ccall unsafe "flushExec"
409 _flushExec :: CUInt -> Ptr a -> IO ()