fix '&stg_interp_constr_entry' FFI type to be FunPtr
[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 ( FunPtr(..) )
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 -> EntryFunPtr -> 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 funPtrToInt :: FunPtr a -> Int
132 funPtrToInt (FunPtr a#) = I# (addr2Int# a#)
133
134 mkJumpToAddr :: DynFlags -> EntryFunPtr -> 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 (funPtrToInt 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 (funPtrToInt 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 (funPtrToInt 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 (funPtrToInt 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 (funPtrToInt 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 (funPtrToInt 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"
253 stg_interp_constr_entry :: EntryFunPtr
254
255
256
257
258 -- Ultra-minimalist version specially for constructors
259 #if SIZEOF_VOID_P == 8
260 type HalfWord = Word32
261 #else
262 type HalfWord = Word16
263 #endif
264
265 data StgConInfoTable = StgConInfoTable {
266 conDesc :: Ptr Word8,
267 infoTable :: StgInfoTable
268 }
269
270 sizeOfConItbl :: DynFlags -> StgConInfoTable -> Int
271 sizeOfConItbl dflags conInfoTable
272 = sum [ fieldSz conDesc conInfoTable
273 , sizeOfItbl dflags (infoTable conInfoTable) ]
274
275 pokeConItbl :: DynFlags -> Ptr StgConInfoTable -> Ptr StgConInfoTable
276 -> StgConInfoTable
277 -> IO ()
278 pokeConItbl dflags wr_ptr ex_ptr itbl
279 = flip evalStateT (castPtr wr_ptr) $ do
280 when ghciTablesNextToCode $ do
281 let con_desc = conDesc itbl `minusPtr`
282 (ex_ptr `plusPtr` conInfoTableSizeB dflags)
283 store (fromIntegral con_desc :: Word32)
284 when (wORD_SIZE dflags == 8) $
285 store (fromIntegral con_desc :: Word32)
286 store' (sizeOfItbl dflags) (pokeItbl dflags) (infoTable itbl)
287 unless ghciTablesNextToCode $ store (conDesc itbl)
288
289 type EntryFunPtr = FunPtr (Ptr () -> IO (Ptr ()))
290
291 data StgInfoTable = StgInfoTable {
292 entry :: Maybe EntryFunPtr, -- Just <=> not ghciTablesNextToCode
293 ptrs :: HalfWord,
294 nptrs :: HalfWord,
295 tipe :: HalfWord,
296 srtlen :: HalfWord,
297 code :: Maybe ItblCodes -- Just <=> ghciTablesNextToCode
298 }
299
300 sizeOfItbl :: DynFlags -> StgInfoTable -> Int
301 sizeOfItbl dflags itbl
302 = sum
303 [
304 if ghciTablesNextToCode then 0 else fieldSz (fromJust . entry) itbl,
305 fieldSz ptrs itbl,
306 fieldSz nptrs itbl,
307 fieldSz tipe itbl,
308 fieldSz srtlen itbl,
309 if ghciTablesNextToCode then case mkJumpToAddr dflags undefined of
310 Left xs -> sizeOf (head xs) * length xs
311 Right xs -> sizeOf (head xs) * length xs
312 else 0
313 ]
314
315 pokeItbl :: DynFlags -> Ptr StgInfoTable -> StgInfoTable -> IO ()
316 pokeItbl _ a0 itbl
317 = flip evalStateT (castPtr a0)
318 $ do
319 case entry itbl of
320 Nothing -> return ()
321 Just e -> store e
322 store (ptrs itbl)
323 store (nptrs itbl)
324 store (tipe itbl)
325 store (srtlen itbl)
326 case code itbl of
327 Nothing -> return ()
328 Just (Left xs) -> mapM_ store xs
329 Just (Right xs) -> mapM_ store xs
330
331 peekItbl :: DynFlags -> Ptr StgInfoTable -> IO StgInfoTable
332 peekItbl dflags a0
333 = flip evalStateT (castPtr a0)
334 $ do
335 entry' <- if ghciTablesNextToCode
336 then return Nothing
337 else liftM Just load
338 ptrs' <- load
339 nptrs' <- load
340 tipe' <- load
341 srtlen' <- load
342 code' <- if ghciTablesNextToCode
343 then liftM Just $ case mkJumpToAddr dflags undefined of
344 Left xs ->
345 liftM Left $ sequence (replicate (length xs) load)
346 Right xs ->
347 liftM Right $ sequence (replicate (length xs) load)
348 else return Nothing
349 return
350 StgInfoTable {
351 entry = entry',
352 ptrs = ptrs',
353 nptrs = nptrs',
354 tipe = tipe',
355 srtlen = srtlen'
356 ,code = code'
357 }
358
359 fieldSz :: Storable b => (a -> b) -> a -> Int
360 fieldSz sel x = sizeOf (sel x)
361
362 type PtrIO = StateT (Ptr Word8) IO
363
364 advance :: Storable a => PtrIO (Ptr a)
365 advance = advance' sizeOf
366
367 advance' :: (a -> Int) -> PtrIO (Ptr a)
368 advance' fSizeOf = state adv
369 where adv addr = case castPtr addr of
370 addrCast ->
371 (addrCast,
372 addr `plusPtr` sizeOfPointee fSizeOf addrCast)
373
374 sizeOfPointee :: (a -> Int) -> Ptr a -> Int
375 sizeOfPointee fSizeOf addr = fSizeOf (typeHack addr)
376 where typeHack = undefined :: Ptr a -> a
377
378 store :: Storable a => a -> PtrIO ()
379 store = store' sizeOf poke
380
381 store' :: (a -> Int) -> (Ptr a -> a -> IO ()) -> a -> PtrIO ()
382 store' fSizeOf fPoke x = do addr <- advance' fSizeOf
383 lift (fPoke addr x)
384
385 load :: Storable a => PtrIO a
386 load = do addr <- advance
387 lift (peek addr)
388
389 newExecConItbl :: DynFlags -> StgInfoTable -> [Word8] -> IO (FunPtr ())
390 newExecConItbl dflags obj con_desc
391 = alloca $ \pcode -> do
392 let lcon_desc = length con_desc + 1{- null terminator -}
393 dummy_cinfo = StgConInfoTable { conDesc = nullPtr, infoTable = obj }
394 sz = fromIntegral (sizeOfConItbl dflags dummy_cinfo)
395 -- Note: we need to allocate the conDesc string next to the info
396 -- table, because on a 64-bit platform we reference this string
397 -- with a 32-bit offset relative to the info table, so if we
398 -- allocated the string separately it might be out of range.
399 wr_ptr <- _allocateExec (sz + fromIntegral lcon_desc) pcode
400 ex_ptr <- peek pcode
401 let cinfo = StgConInfoTable { conDesc = ex_ptr `plusPtr` fromIntegral sz
402 , infoTable = obj }
403 pokeConItbl dflags wr_ptr ex_ptr cinfo
404 pokeArray0 0 (castPtr wr_ptr `plusPtr` fromIntegral sz) con_desc
405 _flushExec sz ex_ptr -- Cache flush (if needed)
406 return (castPtrToFunPtr ex_ptr)
407
408 foreign import ccall unsafe "allocateExec"
409 _allocateExec :: CUInt -> Ptr (Ptr a) -> IO (Ptr a)
410
411 foreign import ccall unsafe "flushExec"
412 _flushExec :: CUInt -> Ptr a -> IO ()