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