5325f8f29e91e439eb22e67f4bbbeff299830842
[ghc-hetmet.git] / ghc / compiler / ghci / ByteCodeItbls.lhs
1 %
2 % (c) The University of Glasgow 2000
3 %
4 \section[ByteCodeItbls]{Generate infotables for interpreter-made bytecodes}
5
6 \begin{code}
7
8 {-# OPTIONS -optc-DNON_POSIX_SOURCE #-}
9
10 module ByteCodeItbls ( ItblEnv, ItblPtr, mkITbls ) where
11
12 #include "HsVersions.h"
13
14 import Name             ( Name, getName )
15 import NameEnv
16 import Type             ( typePrimRep )
17 import DataCon          ( DataCon, dataConRepArgTys )
18 import TyCon            ( TyCon, tyConFamilySize, isDataTyCon, tyConDataCons )
19 import Constants        ( mIN_SIZE_NonUpdHeapObject )
20 import ClosureInfo      ( mkVirtHeapOffsets )
21 import FastString       ( FastString(..) )
22 import Util             ( lengthIs, listLengthCmp )
23
24 import Foreign          ( Storable(..), Word8, Word16, Word32, Word64,
25                           malloc, castPtr, plusPtr )
26 import DATA_BITS        ( Bits(..), shiftR )
27
28 import GHC.Exts         ( Int(I#), addr2Int# )
29 #if __GLASGOW_HASKELL__ < 503
30 import Ptr              ( Ptr(..) )
31 #else
32 import GHC.Ptr          ( Ptr(..) )
33 #endif
34 \end{code}
35
36 %************************************************************************
37 %*                                                                      *
38 \subsection{Manufacturing of info tables for DataCons}
39 %*                                                                      *
40 %************************************************************************
41
42 \begin{code}
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
47
48 mkItblEnv :: [(Name,ItblPtr)] -> ItblEnv
49 mkItblEnv pairs = mkNameEnv [(n, (n,p)) | (n,p) <- pairs]
50
51
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
56                       itbls2 <- mkITbls tcs
57                       return (itbls `plusNameEnv` itbls2)
58
59 mkITbl :: TyCon -> IO ItblEnv
60 mkITbl tc
61    | not (isDataTyCon tc) 
62    = return emptyNameEnv
63    | dcs `lengthIs` n -- paranoia; this is an assertion.
64    = make_constr_itbls dcs
65      where
66         dcs = tyConDataCons tc
67         n   = tyConFamilySize tc
68
69 #include "../includes/ClosureTypes.h"
70 cONSTR :: Int   -- Defined in ClosureTypes.h
71 cONSTR = CONSTR 
72
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..])
78         return (mkItblEnv is)
79    | otherwise
80    = do is <- mapM mk_dirret_itbl (zip cons [0..])
81         return (mkItblEnv is)
82      where
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
87
88         mk_itbl :: DataCon -> Int -> Ptr () -> IO (Name,ItblPtr)
89         mk_itbl dcon conNo entry_addr
90            = let (tot_wds, ptr_wds, _) 
91                     = mkVirtHeapOffsets typePrimRep (dataConRepArgTys dcon)
92                  ptrs  = ptr_wds
93                  nptrs = tot_wds - ptr_wds
94                  nptrs_really
95                     | ptrs + nptrs >= mIN_SIZE_NonUpdHeapObject = nptrs
96                     | otherwise = mIN_SIZE_NonUpdHeapObject - ptrs
97                  itbl  = StgInfoTable {
98                            ptrs  = fromIntegral ptrs, 
99                            nptrs = fromIntegral nptrs_really,
100                            tipe  = fromIntegral cONSTR,
101                            srtlen = fromIntegral conNo,
102                            code  = code
103                         }
104                  -- Make a piece of code to jump to "entry_label".
105                  -- This is the only arch-dependent bit.
106                  code = mkJumpToAddr entry_addr
107              in
108                  do addr <- malloc
109                     --putStrLn ("SIZE of itbl is " ++ show (sizeOf itbl))
110                     --putStrLn ("# ptrs  of itbl is " ++ show ptrs)
111                     --putStrLn ("# nptrs of itbl is " ++ show nptrs_really)
112                     poke addr itbl
113                     return (getName dcon, addr `plusPtr` 8)
114
115
116 -- Make code which causes a jump to the given address.  This is the
117 -- only arch-dependent bit of the itbl story.  The returned list is
118 -- itblCodeLength elements (bytes) long.
119
120 -- For sparc_TARGET_ARCH, i386_TARGET_ARCH, etc.
121 #include "nativeGen/NCG.h"
122
123 itblCodeLength :: Int
124 itblCodeLength = length (mkJumpToAddr undefined)
125
126 mkJumpToAddr :: Ptr () -> [ItblCode]
127
128 ptrToInt (Ptr a#) = I# (addr2Int# a#)
129
130 #if sparc_TARGET_ARCH
131 -- After some consideration, we'll try this, where
132 -- 0x55555555 stands in for the address to jump to.
133 -- According to ghc/includes/MachRegs.h, %g3 is very
134 -- likely indeed to be baggable.
135 --
136 --   0000 07155555              sethi   %hi(0x55555555), %g3
137 --   0004 8610E155              or      %g3, %lo(0x55555555), %g3
138 --   0008 81C0C000              jmp     %g3
139 --   000c 01000000              nop
140
141 type ItblCode = Word32
142 mkJumpToAddr a
143    = let w32 = fromIntegral (ptrToInt a)
144
145          hi22, lo10 :: Word32 -> Word32
146          lo10 x = x .&. 0x3FF
147          hi22 x = (x `shiftR` 10) .&. 0x3FFFF
148
149      in  [ 0x07000000 .|. (hi22 w32),
150            0x8610E000 .|. (lo10 w32),
151            0x81C0C000,
152            0x01000000 ]
153
154 #elif powerpc_TARGET_ARCH
155 -- We'll use r12, for no particular reason.
156 -- 0xDEADBEEF stands for the adress:
157 -- 3D80DEAD lis r12,0xDEAD
158 -- 618CBEEF ori r12,r12,0xBEEF
159 -- 7D8903A6 mtctr r12
160 -- 4E800420 bctr
161
162 type ItblCode = Word32
163 mkJumpToAddr a =
164     let w32 = fromIntegral (ptrToInt a)
165         hi16 x = (x `shiftR` 16) .&. 0xFFFF
166         lo16 x = x .&. 0xFFFF
167     in  [
168         0x3D800000 .|. hi16 w32,
169         0x618C0000 .|. lo16 w32,
170         0x7D8903A6, 0x4E800420
171         ]
172
173 #elif i386_TARGET_ARCH
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 type ItblCode = Word8
180 mkJumpToAddr a
181    = let w32 = fromIntegral (ptrToInt a)
182          insnBytes :: [Word8]
183          insnBytes
184             = [0xB8, byte 0 w32, byte 1 w32, 
185                      byte 2 w32, byte 3 w32, 
186                0xFF, 0xE0]
187      in
188          insnBytes
189
190 #elif alpha_TARGET_ARCH
191 type ItblCode = Word32
192 mkJumpToAddr a
193     = [ 0xc3800000      -- br   at, .+4
194       , 0xa79c000c      -- ldq  at, 12(at)
195       , 0x6bfc0000      -- jmp  (at)    # with zero hint -- oh well
196       , 0x47ff041f      -- nop
197       , fromIntegral (w64 .&. 0x0000FFFF)
198       , fromIntegral ((w64 `shiftR` 32) .&. 0x0000FFFF) ]
199     where w64 = fromIntegral (ptrToInt a) :: Word64
200
201 #else
202 type ItblCode = Word32
203 mkJumpToAddr a
204     = undefined
205 #endif
206
207
208 byte :: Int -> Word32 -> Word8
209 byte 0 w = fromIntegral (w .&. 0xFF)
210 byte 1 w = fromIntegral ((w `shiftR` 8) .&. 0xFF)
211 byte 2 w = fromIntegral ((w `shiftR` 16) .&. 0xFF)
212 byte 3 w = fromIntegral ((w `shiftR` 24) .&. 0xFF)
213
214
215 vecret_entry 0 = stg_interp_constr1_entry
216 vecret_entry 1 = stg_interp_constr2_entry
217 vecret_entry 2 = stg_interp_constr3_entry
218 vecret_entry 3 = stg_interp_constr4_entry
219 vecret_entry 4 = stg_interp_constr5_entry
220 vecret_entry 5 = stg_interp_constr6_entry
221 vecret_entry 6 = stg_interp_constr7_entry
222 vecret_entry 7 = stg_interp_constr8_entry
223
224 -- entry point for direct returns for created constr itbls
225 foreign label "stg_interp_constr_entry" stg_interp_constr_entry :: Ptr ()
226 -- and the 8 vectored ones
227 foreign label "stg_interp_constr1_entry" stg_interp_constr1_entry :: Ptr ()
228 foreign label "stg_interp_constr2_entry" stg_interp_constr2_entry :: Ptr ()
229 foreign label "stg_interp_constr3_entry" stg_interp_constr3_entry :: Ptr ()
230 foreign label "stg_interp_constr4_entry" stg_interp_constr4_entry :: Ptr ()
231 foreign label "stg_interp_constr5_entry" stg_interp_constr5_entry :: Ptr ()
232 foreign label "stg_interp_constr6_entry" stg_interp_constr6_entry :: Ptr ()
233 foreign label "stg_interp_constr7_entry" stg_interp_constr7_entry :: Ptr ()
234 foreign label "stg_interp_constr8_entry" stg_interp_constr8_entry :: Ptr ()
235
236
237
238
239
240 -- Ultra-minimalist version specially for constructors
241 #if SIZEOF_VOID_P == 8
242 type HalfWord = Word32
243 #else
244 type HalfWord = Word16
245 #endif
246
247 data StgInfoTable = StgInfoTable {
248    ptrs   :: HalfWord,
249    nptrs  :: HalfWord,
250    tipe   :: HalfWord,
251    srtlen :: HalfWord,
252    code   :: [ItblCode]
253 }
254
255 instance Storable StgInfoTable where
256
257    sizeOf itbl 
258       = sum
259         [fieldSz ptrs itbl,
260          fieldSz nptrs itbl,
261          fieldSz tipe itbl,
262          fieldSz srtlen itbl,
263          fieldSz (head.code) itbl * itblCodeLength]
264
265    alignment itbl 
266       = SIZEOF_VOID_P
267
268    poke a0 itbl
269       = runState (castPtr a0)
270       $ do store (ptrs   itbl)
271            store (nptrs  itbl)
272            store (tipe   itbl)
273            store (srtlen itbl)
274            sequence_ (map store (code itbl))
275
276    peek a0
277       = runState (castPtr a0)
278       $ do ptrs   <- load
279            nptrs  <- load
280            tipe   <- load
281            srtlen <- load
282            code   <- sequence (replicate itblCodeLength load)
283            return 
284               StgInfoTable { 
285                  ptrs   = ptrs,
286                  nptrs  = nptrs, 
287                  tipe   = tipe,
288                  srtlen = srtlen,
289                  code   = code
290               }
291
292 fieldSz :: (Storable a, Storable b) => (a -> b) -> a -> Int
293 fieldSz sel x = sizeOf (sel x)
294
295 newtype State s m a = State (s -> m (s, a))
296
297 instance Monad m => Monad (State s m) where
298   return a      = State (\s -> return (s, a))
299   State m >>= k = State (\s -> m s >>= \(s', a) -> case k a of State n -> n s')
300   fail str      = State (\s -> fail str)
301
302 class (Monad m, Monad (t m)) => MonadT t m where
303   lift :: m a -> t m a
304
305 instance Monad m => MonadT (State s) m where
306   lift m        = State (\s -> m >>= \a -> return (s, a))
307
308 runState :: (Monad m) => s -> State s m a -> m a
309 runState s (State m) = m s >>= return . snd
310
311 type PtrIO = State (Ptr Word8) IO
312
313 advance :: Storable a => PtrIO (Ptr a)
314 advance = State adv where
315     adv addr = case castPtr addr of { addrCast -> return
316         (addr `plusPtr` sizeOfPointee addrCast, addrCast) }
317
318 sizeOfPointee :: (Storable a) => Ptr a -> Int
319 sizeOfPointee addr = sizeOf (typeHack addr)
320     where typeHack = undefined :: Ptr a -> a
321
322 store :: Storable a => a -> PtrIO ()
323 store x = do addr <- advance
324              lift (poke addr x)
325
326 load :: Storable a => PtrIO a
327 load = do addr <- advance
328           lift (peek addr)
329
330 \end{code}