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