remove empty dir
[ghc-hetmet.git] / compiler / ghci / ByteCodeInstr.lhs
1 %
2 % (c) The University of Glasgow 2000
3 %
4 \section[ByteCodeInstrs]{Bytecode instruction definitions}
5
6 \begin{code}
7 module ByteCodeInstr ( 
8         BCInstr(..), ProtoBCO(..), bciStackUse
9   ) where
10
11 #include "HsVersions.h"
12 #include "../includes/MachDeps.h"
13
14 import Outputable
15 import Name             ( Name )
16 import Id               ( Id )
17 import CoreSyn
18 import PprCore          ( pprCoreExpr, pprCoreAlt )
19 import Literal          ( Literal )
20 import DataCon          ( DataCon )
21 import VarSet           ( VarSet )
22 import PrimOp           ( PrimOp )
23 import SMRep            ( StgWord, CgRep )
24 import GHC.Ptr
25
26 -- ----------------------------------------------------------------------------
27 -- Bytecode instructions
28
29 data ProtoBCO a 
30    = ProtoBCO { 
31         protoBCOName       :: a,          -- name, in some sense
32         protoBCOInstrs     :: [BCInstr],  -- instrs
33         -- arity and GC info
34         protoBCOBitmap     :: [StgWord],
35         protoBCOBitmapSize :: Int,
36         protoBCOArity      :: Int,
37         -- what the BCO came from
38         protoBCOExpr       :: Either  [AnnAlt Id VarSet] (AnnExpr Id VarSet),
39         -- malloc'd pointers
40         protoBCOPtrs       :: [Ptr ()]
41    }
42
43 type LocalLabel = Int
44
45 data BCInstr
46    -- Messing with the stack
47    = STKCHECK  Int
48
49    -- Push locals (existing bits of the stack)
50    | PUSH_L    Int{-offset-}
51    | PUSH_LL   Int Int{-2 offsets-}
52    | PUSH_LLL  Int Int Int{-3 offsets-}
53
54    -- Push a ptr  (these all map to PUSH_G really)
55    | PUSH_G       Name
56    | PUSH_PRIMOP  PrimOp
57    | PUSH_BCO     (ProtoBCO Name)
58
59    -- Push an alt continuation
60    | PUSH_ALTS          (ProtoBCO Name)
61    | PUSH_ALTS_UNLIFTED (ProtoBCO Name) CgRep
62
63    -- Pushing literals
64    | PUSH_UBX  (Either Literal (Ptr ())) Int
65         -- push this int/float/double/addr, on the stack.  Int
66         -- is # of words to copy from literal pool.  Eitherness reflects
67         -- the difficulty of dealing with MachAddr here, mostly due to
68         -- the excessive (and unnecessary) restrictions imposed by the
69         -- designers of the new Foreign library.  In particular it is
70         -- quite impossible to convert an Addr to any other integral
71         -- type, and it appears impossible to get hold of the bits of
72         -- an addr, even though we need to to assemble BCOs.
73
74    -- various kinds of application
75    | PUSH_APPLY_N
76    | PUSH_APPLY_V
77    | PUSH_APPLY_F
78    | PUSH_APPLY_D
79    | PUSH_APPLY_L
80    | PUSH_APPLY_P
81    | PUSH_APPLY_PP
82    | PUSH_APPLY_PPP
83    | PUSH_APPLY_PPPP
84    | PUSH_APPLY_PPPPP
85    | PUSH_APPLY_PPPPPP
86
87    | SLIDE     Int{-this many-} Int{-down by this much-}
88
89    -- To do with the heap
90    | ALLOC_AP  Int      -- make an AP with this many payload words
91    | ALLOC_PAP Int Int  -- make a PAP with this arity / payload words
92    | MKAP      Int{-ptr to AP is this far down stack-} Int{-# words-}
93    | MKPAP     Int{-ptr to PAP is this far down stack-} Int{-# words-}
94    | UNPACK    Int      -- unpack N words from t.o.s Constr
95    | PACK      DataCon Int
96                         -- after assembly, the DataCon is an index into the
97                         -- itbl array
98    -- For doing case trees
99    | LABEL     LocalLabel
100    | TESTLT_I  Int    LocalLabel
101    | TESTEQ_I  Int    LocalLabel
102    | TESTLT_F  Float  LocalLabel
103    | TESTEQ_F  Float  LocalLabel
104    | TESTLT_D  Double LocalLabel
105    | TESTEQ_D  Double LocalLabel
106
107    -- The Int value is a constructor number and therefore
108    -- stored in the insn stream rather than as an offset into
109    -- the literal pool.
110    | TESTLT_P  Int    LocalLabel
111    | TESTEQ_P  Int    LocalLabel
112
113    | CASEFAIL
114    | JMP              LocalLabel
115
116    -- For doing calls to C (via glue code generated by ByteCodeFFI)
117    | CCALL            Int       -- stack frame size
118                       (Ptr ())  -- addr of the glue code
119
120    -- For doing magic ByteArray passing to foreign calls
121    | SWIZZLE          Int       -- to the ptr N words down the stack,
122                       Int       -- add M (interpreted as a signed 16-bit entity)
123
124    -- To Infinity And Beyond
125    | ENTER
126    | RETURN             -- return a lifted value
127    | RETURN_UBX CgRep -- return an unlifted value, here's its rep
128
129 -- -----------------------------------------------------------------------------
130 -- Printing bytecode instructions
131
132 instance Outputable a => Outputable (ProtoBCO a) where
133    ppr (ProtoBCO name instrs bitmap bsize arity origin malloced)
134       = (text "ProtoBCO" <+> ppr name <> char '#' <> int arity 
135                 <+> text (show malloced) <> colon)
136         $$ nest 6 (text "bitmap: " <+> text (show bsize) <+> text (show bitmap))
137         $$ nest 6 (vcat (map ppr instrs))
138         $$ case origin of
139               Left alts -> vcat (map (pprCoreAlt.deAnnAlt) alts)
140               Right rhs -> pprCoreExpr (deAnnotate rhs)
141
142 instance Outputable BCInstr where
143    ppr (STKCHECK n)          = text "STKCHECK" <+> int n
144    ppr (PUSH_L offset)       = text "PUSH_L  " <+> int offset
145    ppr (PUSH_LL o1 o2)       = text "PUSH_LL " <+> int o1 <+> int o2
146    ppr (PUSH_LLL o1 o2 o3)   = text "PUSH_LLL" <+> int o1 <+> int o2 <+> int o3
147    ppr (PUSH_G nm)           = text "PUSH_G  " <+> ppr nm
148    ppr (PUSH_PRIMOP op)      = text "PUSH_G  " <+> text "GHC.PrimopWrappers." 
149                                                <> ppr op
150    ppr (PUSH_BCO bco)        = text "PUSH_BCO" <+> nest 3 (ppr bco)
151    ppr (PUSH_ALTS bco)       = text "PUSH_ALTS " <+> ppr bco
152    ppr (PUSH_ALTS_UNLIFTED bco pk) = text "PUSH_ALTS_UNLIFTED " <+> ppr pk <+> ppr bco
153
154    ppr (PUSH_UBX (Left lit) nw) = text "PUSH_UBX" <+> parens (int nw) <+> ppr lit
155    ppr (PUSH_UBX (Right aa) nw) = text "PUSH_UBX" <+> parens (int nw) <+> text (show aa)
156    ppr PUSH_APPLY_N             = text "PUSH_APPLY_N"
157    ppr PUSH_APPLY_V             = text "PUSH_APPLY_V"
158    ppr PUSH_APPLY_F             = text "PUSH_APPLY_F"
159    ppr PUSH_APPLY_D             = text "PUSH_APPLY_D"
160    ppr PUSH_APPLY_L             = text "PUSH_APPLY_L"
161    ppr PUSH_APPLY_P             = text "PUSH_APPLY_P"
162    ppr PUSH_APPLY_PP            = text "PUSH_APPLY_PP"
163    ppr PUSH_APPLY_PPP           = text "PUSH_APPLY_PPP"
164    ppr PUSH_APPLY_PPPP          = text "PUSH_APPLY_PPPP"
165    ppr PUSH_APPLY_PPPPP         = text "PUSH_APPLY_PPPPP"
166    ppr PUSH_APPLY_PPPPPP        = text "PUSH_APPLY_PPPPPP"
167
168    ppr (SLIDE n d)           = text "SLIDE   " <+> int n <+> int d
169    ppr (ALLOC_AP sz)         = text "ALLOC_AP   " <+> int sz
170    ppr (ALLOC_PAP arity sz)  = text "ALLOC_PAP   " <+> int arity <+> int sz
171    ppr (MKAP offset sz)      = text "MKAP    " <+> int sz <+> text "words," 
172                                                <+> int offset <+> text "stkoff"
173    ppr (UNPACK sz)           = text "UNPACK  " <+> int sz
174    ppr (PACK dcon sz)        = text "PACK    " <+> ppr dcon <+> ppr sz
175    ppr (LABEL     lab)       = text "__"       <> int lab <> colon
176    ppr (TESTLT_I  i lab)     = text "TESTLT_I" <+> int i <+> text "__" <> int lab
177    ppr (TESTEQ_I  i lab)     = text "TESTEQ_I" <+> int i <+> text "__" <> int lab
178    ppr (TESTLT_F  f lab)     = text "TESTLT_F" <+> float f <+> text "__" <> int lab
179    ppr (TESTEQ_F  f lab)     = text "TESTEQ_F" <+> float f <+> text "__" <> int lab
180    ppr (TESTLT_D  d lab)     = text "TESTLT_D" <+> double d <+> text "__" <> int lab
181    ppr (TESTEQ_D  d lab)     = text "TESTEQ_D" <+> double d <+> text "__" <> int lab
182    ppr (TESTLT_P  i lab)     = text "TESTLT_P" <+> int i <+> text "__" <> int lab
183    ppr (TESTEQ_P  i lab)     = text "TESTEQ_P" <+> int i <+> text "__" <> int lab
184    ppr (JMP lab)             = text "JMP"      <+> int lab
185    ppr CASEFAIL              = text "CASEFAIL"
186    ppr ENTER                 = text "ENTER"
187    ppr RETURN                = text "RETURN"
188    ppr (RETURN_UBX pk)       = text "RETURN_UBX  " <+> ppr pk
189    ppr (CCALL off marshall_addr) = text "CCALL   " <+> int off 
190                                                 <+> text "marshall code at" 
191                                                <+> text (show marshall_addr)
192    ppr (SWIZZLE stkoff n)    = text "SWIZZLE " <+> text "stkoff" <+> int stkoff 
193                                                <+> text "by" <+> int n 
194
195 -- -----------------------------------------------------------------------------
196 -- The stack use, in words, of each bytecode insn.  These _must_ be
197 -- correct, or overestimates of reality, to be safe.
198
199 -- NOTE: we aggregate the stack use from case alternatives too, so that
200 -- we can do a single stack check at the beginning of a function only.
201
202 -- This could all be made more accurate by keeping track of a proper
203 -- stack high water mark, but it doesn't seem worth the hassle.
204
205 protoBCOStackUse :: ProtoBCO a -> Int
206 protoBCOStackUse bco = sum (map bciStackUse (protoBCOInstrs bco))
207
208 bciStackUse :: BCInstr -> Int
209 bciStackUse STKCHECK{}            = 0
210 bciStackUse PUSH_L{}              = 1
211 bciStackUse PUSH_LL{}             = 2
212 bciStackUse PUSH_LLL{}            = 3
213 bciStackUse PUSH_G{}              = 1
214 bciStackUse PUSH_PRIMOP{}         = 1
215 bciStackUse PUSH_BCO{}            = 1
216 bciStackUse (PUSH_ALTS bco)       = 2 + protoBCOStackUse bco
217 bciStackUse (PUSH_ALTS_UNLIFTED bco _) = 2 + protoBCOStackUse bco
218 bciStackUse (PUSH_UBX _ nw)       = nw
219 bciStackUse PUSH_APPLY_N{}        = 1
220 bciStackUse PUSH_APPLY_V{}        = 1
221 bciStackUse PUSH_APPLY_F{}        = 1
222 bciStackUse PUSH_APPLY_D{}        = 1
223 bciStackUse PUSH_APPLY_L{}        = 1
224 bciStackUse PUSH_APPLY_P{}        = 1
225 bciStackUse PUSH_APPLY_PP{}       = 1
226 bciStackUse PUSH_APPLY_PPP{}      = 1
227 bciStackUse PUSH_APPLY_PPPP{}     = 1
228 bciStackUse PUSH_APPLY_PPPPP{}    = 1
229 bciStackUse PUSH_APPLY_PPPPPP{}   = 1
230 bciStackUse ALLOC_AP{}            = 1
231 bciStackUse ALLOC_PAP{}           = 1
232 bciStackUse (UNPACK sz)           = sz
233 bciStackUse LABEL{}               = 0
234 bciStackUse TESTLT_I{}            = 0
235 bciStackUse TESTEQ_I{}            = 0
236 bciStackUse TESTLT_F{}            = 0
237 bciStackUse TESTEQ_F{}            = 0
238 bciStackUse TESTLT_D{}            = 0
239 bciStackUse TESTEQ_D{}            = 0
240 bciStackUse TESTLT_P{}            = 0
241 bciStackUse TESTEQ_P{}            = 0
242 bciStackUse CASEFAIL{}            = 0
243 bciStackUse JMP{}                 = 0
244 bciStackUse ENTER{}               = 0
245 bciStackUse RETURN{}              = 0
246 bciStackUse RETURN_UBX{}          = 1
247 bciStackUse CCALL{}               = 0
248 bciStackUse SWIZZLE{}             = 0
249
250 -- These insns actually reduce stack use, but we need the high-tide level,
251 -- so can't use this info.  Not that it matters much.
252 bciStackUse SLIDE{}               = 0
253 bciStackUse MKAP{}                = 0
254 bciStackUse MKPAP{}               = 0
255 bciStackUse PACK{}                = 1 -- worst case is PACK 0 words
256 \end{code}