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