2 % (c) The University of Glasgow 2000
4 \section[ByteCodeInstrs]{Bytecode instruction definitions}
8 BCInstr(..), ProtoBCO(..), bciStackUse
11 #include "HsVersions.h"
12 #include "../includes/MachDeps.h"
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 )
26 -- ----------------------------------------------------------------------------
27 -- Bytecode instructions
31 protoBCOName :: a, -- name, in some sense
32 protoBCOInstrs :: [BCInstr], -- instrs
34 protoBCOBitmap :: [StgWord],
35 protoBCOBitmapSize :: Int,
37 -- what the BCO came from
38 protoBCOExpr :: Either [AnnAlt Id VarSet] (AnnExpr Id VarSet),
40 protoBCOPtrs :: [Ptr ()]
46 -- Messing with the stack
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-}
54 -- Push a ptr (these all map to PUSH_G really)
57 | PUSH_BCO (ProtoBCO Name)
59 -- Push an alt continuation
60 | PUSH_ALTS (ProtoBCO Name)
61 | PUSH_ALTS_UNLIFTED (ProtoBCO Name) CgRep
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.
74 -- various kinds of application
88 | SLIDE Int{-this many-} Int{-down by this much-}
90 -- To do with the heap
91 | ALLOC_AP Int -- make an AP with this many payload words
92 | ALLOC_PAP Int Int -- make a PAP with this arity / payload words
93 | MKAP Int{-ptr to AP/PAP is this far down stack-} Int{-# words-}
94 | UNPACK Int -- unpack N words from t.o.s Constr
96 -- after assembly, the DataCon is an index into the
98 -- For doing case trees
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
107 -- The Int value is a constructor number and therefore
108 -- stored in the insn stream rather than as an offset into
110 | TESTLT_P Int LocalLabel
111 | TESTEQ_P Int LocalLabel
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
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)
124 -- To Infinity And Beyond
126 | RETURN -- return a lifted value
127 | RETURN_UBX CgRep -- return an unlifted value, here's its rep
129 -- -----------------------------------------------------------------------------
130 -- Printing bytecode instructions
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))
139 Left alts -> vcat (map (pprCoreAlt.deAnnAlt) alts)
140 Right rhs -> pprCoreExpr (deAnnotate rhs)
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."
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
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 ppr PUSH_APPLY_PPPPPPP = text "PUSH_APPLY_PPPPPPP"
169 ppr (SLIDE n d) = text "SLIDE " <+> int n <+> int d
170 ppr (ALLOC_AP sz) = text "ALLOC_AP " <+> int sz
171 ppr (ALLOC_PAP arity sz) = text "ALLOC_PAP " <+> int arity <+> int sz
172 ppr (MKAP offset sz) = text "MKAP " <+> int sz <+> text "words,"
173 <+> int offset <+> text "stkoff"
174 ppr (UNPACK sz) = text "UNPACK " <+> int sz
175 ppr (PACK dcon sz) = text "PACK " <+> ppr dcon <+> ppr sz
176 ppr (LABEL lab) = text "__" <> int lab <> colon
177 ppr (TESTLT_I i lab) = text "TESTLT_I" <+> int i <+> text "__" <> int lab
178 ppr (TESTEQ_I i lab) = text "TESTEQ_I" <+> int i <+> text "__" <> int lab
179 ppr (TESTLT_F f lab) = text "TESTLT_F" <+> float f <+> text "__" <> int lab
180 ppr (TESTEQ_F f lab) = text "TESTEQ_F" <+> float f <+> text "__" <> int lab
181 ppr (TESTLT_D d lab) = text "TESTLT_D" <+> double d <+> text "__" <> int lab
182 ppr (TESTEQ_D d lab) = text "TESTEQ_D" <+> double d <+> text "__" <> int lab
183 ppr (TESTLT_P i lab) = text "TESTLT_P" <+> int i <+> text "__" <> int lab
184 ppr (TESTEQ_P i lab) = text "TESTEQ_P" <+> int i <+> text "__" <> int lab
185 ppr (JMP lab) = text "JMP" <+> int lab
186 ppr CASEFAIL = text "CASEFAIL"
187 ppr ENTER = text "ENTER"
188 ppr RETURN = text "RETURN"
189 ppr (RETURN_UBX pk) = text "RETURN_UBX " <+> ppr pk
190 ppr (CCALL off marshall_addr) = text "CCALL " <+> int off
191 <+> text "marshall code at"
192 <+> text (show marshall_addr)
193 ppr (SWIZZLE stkoff n) = text "SWIZZLE " <+> text "stkoff" <+> int stkoff
194 <+> text "by" <+> int n
196 -- -----------------------------------------------------------------------------
197 -- The stack use, in words, of each bytecode insn. These _must_ be
198 -- correct, or overestimates of reality, to be safe.
200 -- NOTE: we aggregate the stack use from case alternatives too, so that
201 -- we can do a single stack check at the beginning of a function only.
203 -- This could all be made more accurate by keeping track of a proper
204 -- stack high water mark, but it doesn't seem worth the hassle.
206 protoBCOStackUse :: ProtoBCO a -> Int
207 protoBCOStackUse bco = sum (map bciStackUse (protoBCOInstrs bco))
209 bciStackUse :: BCInstr -> Int
210 bciStackUse STKCHECK{} = 0
211 bciStackUse PUSH_L{} = 1
212 bciStackUse PUSH_LL{} = 2
213 bciStackUse PUSH_LLL{} = 3
214 bciStackUse PUSH_G{} = 1
215 bciStackUse PUSH_PRIMOP{} = 1
216 bciStackUse PUSH_BCO{} = 1
217 bciStackUse (PUSH_ALTS bco) = 2 + protoBCOStackUse bco
218 bciStackUse (PUSH_ALTS_UNLIFTED bco _) = 2 + protoBCOStackUse bco
219 bciStackUse (PUSH_UBX _ nw) = nw
220 bciStackUse PUSH_APPLY_N{} = 1
221 bciStackUse PUSH_APPLY_V{} = 1
222 bciStackUse PUSH_APPLY_F{} = 1
223 bciStackUse PUSH_APPLY_D{} = 1
224 bciStackUse PUSH_APPLY_L{} = 1
225 bciStackUse PUSH_APPLY_P{} = 1
226 bciStackUse PUSH_APPLY_PP{} = 1
227 bciStackUse PUSH_APPLY_PPP{} = 1
228 bciStackUse PUSH_APPLY_PPPP{} = 1
229 bciStackUse PUSH_APPLY_PPPPP{} = 1
230 bciStackUse PUSH_APPLY_PPPPPP{} = 1
231 bciStackUse PUSH_APPLY_PPPPPPP{} = 1
232 bciStackUse ALLOC_AP{} = 1
233 bciStackUse ALLOC_PAP{} = 1
234 bciStackUse (UNPACK sz) = sz
235 bciStackUse LABEL{} = 0
236 bciStackUse TESTLT_I{} = 0
237 bciStackUse TESTEQ_I{} = 0
238 bciStackUse TESTLT_F{} = 0
239 bciStackUse TESTEQ_F{} = 0
240 bciStackUse TESTLT_D{} = 0
241 bciStackUse TESTEQ_D{} = 0
242 bciStackUse TESTLT_P{} = 0
243 bciStackUse TESTEQ_P{} = 0
244 bciStackUse CASEFAIL{} = 0
245 bciStackUse JMP{} = 0
246 bciStackUse ENTER{} = 0
247 bciStackUse RETURN{} = 0
248 bciStackUse RETURN_UBX{} = 1
249 bciStackUse CCALL{} = 0
250 bciStackUse SWIZZLE{} = 0
252 -- These insns actually reduce stack use, but we need the high-tide level,
253 -- so can't use this info. Not that it matters much.
254 bciStackUse SLIDE{} = 0
255 bciStackUse MKAP{} = 0
256 bciStackUse PACK{} = 1 -- worst case is PACK 0 words