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
87 | SLIDE Int{-this many-} Int{-down by this much-}
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/PAP is this far down stack-} Int{-# words-}
93 | UNPACK Int -- unpack N words from t.o.s Constr
95 -- after assembly, the DataCon is an index into the
97 -- For doing case trees
99 | TESTLT_I Int LocalLabel
100 | TESTEQ_I Int LocalLabel
101 | TESTLT_F Float LocalLabel
102 | TESTEQ_F Float LocalLabel
103 | TESTLT_D Double LocalLabel
104 | TESTEQ_D Double LocalLabel
106 -- The Int value is a constructor number and therefore
107 -- stored in the insn stream rather than as an offset into
109 | TESTLT_P Int LocalLabel
110 | TESTEQ_P Int LocalLabel
115 -- For doing calls to C (via glue code generated by ByteCodeFFI)
116 | CCALL Int -- stack frame size
117 (Ptr ()) -- addr of the glue code
119 -- For doing magic ByteArray passing to foreign calls
120 | SWIZZLE Int -- to the ptr N words down the stack,
121 Int -- add M (interpreted as a signed 16-bit entity)
123 -- To Infinity And Beyond
125 | RETURN -- return a lifted value
126 | RETURN_UBX CgRep -- return an unlifted value, here's its rep
128 -- -----------------------------------------------------------------------------
129 -- Printing bytecode instructions
131 instance Outputable a => Outputable (ProtoBCO a) where
132 ppr (ProtoBCO name instrs bitmap bsize arity origin malloced)
133 = (text "ProtoBCO" <+> ppr name <> char '#' <> int arity
134 <+> text (show malloced) <> colon)
135 $$ nest 6 (text "bitmap: " <+> text (show bsize) <+> text (show bitmap))
136 $$ nest 6 (vcat (map ppr instrs))
138 Left alts -> vcat (map (pprCoreAlt.deAnnAlt) alts)
139 Right rhs -> pprCoreExpr (deAnnotate rhs)
141 instance Outputable BCInstr where
142 ppr (STKCHECK n) = text "STKCHECK" <+> int n
143 ppr (PUSH_L offset) = text "PUSH_L " <+> int offset
144 ppr (PUSH_LL o1 o2) = text "PUSH_LL " <+> int o1 <+> int o2
145 ppr (PUSH_LLL o1 o2 o3) = text "PUSH_LLL" <+> int o1 <+> int o2 <+> int o3
146 ppr (PUSH_G nm) = text "PUSH_G " <+> ppr nm
147 ppr (PUSH_PRIMOP op) = text "PUSH_G " <+> text "GHC.PrimopWrappers."
149 ppr (PUSH_BCO bco) = text "PUSH_BCO" <+> nest 3 (ppr bco)
150 ppr (PUSH_ALTS bco) = text "PUSH_ALTS " <+> ppr bco
151 ppr (PUSH_ALTS_UNLIFTED bco pk) = text "PUSH_ALTS_UNLIFTED " <+> ppr pk <+> ppr bco
153 ppr (PUSH_UBX (Left lit) nw) = text "PUSH_UBX" <+> parens (int nw) <+> ppr lit
154 ppr (PUSH_UBX (Right aa) nw) = text "PUSH_UBX" <+> parens (int nw) <+> text (show aa)
155 ppr PUSH_APPLY_N = text "PUSH_APPLY_N"
156 ppr PUSH_APPLY_V = text "PUSH_APPLY_V"
157 ppr PUSH_APPLY_F = text "PUSH_APPLY_F"
158 ppr PUSH_APPLY_D = text "PUSH_APPLY_D"
159 ppr PUSH_APPLY_L = text "PUSH_APPLY_L"
160 ppr PUSH_APPLY_P = text "PUSH_APPLY_P"
161 ppr PUSH_APPLY_PP = text "PUSH_APPLY_PP"
162 ppr PUSH_APPLY_PPP = text "PUSH_APPLY_PPP"
163 ppr PUSH_APPLY_PPPP = text "PUSH_APPLY_PPPP"
164 ppr PUSH_APPLY_PPPPP = text "PUSH_APPLY_PPPPP"
165 ppr PUSH_APPLY_PPPPPP = text "PUSH_APPLY_PPPPPP"
167 ppr (SLIDE n d) = text "SLIDE " <+> int n <+> int d
168 ppr (ALLOC_AP sz) = text "ALLOC_AP " <+> int sz
169 ppr (ALLOC_PAP arity sz) = text "ALLOC_PAP " <+> int arity <+> int sz
170 ppr (MKAP offset sz) = text "MKAP " <+> int sz <+> text "words,"
171 <+> int offset <+> text "stkoff"
172 ppr (UNPACK sz) = text "UNPACK " <+> int sz
173 ppr (PACK dcon sz) = text "PACK " <+> ppr dcon <+> ppr sz
174 ppr (LABEL lab) = text "__" <> int lab <> colon
175 ppr (TESTLT_I i lab) = text "TESTLT_I" <+> int i <+> text "__" <> int lab
176 ppr (TESTEQ_I i lab) = text "TESTEQ_I" <+> int i <+> text "__" <> int lab
177 ppr (TESTLT_F f lab) = text "TESTLT_F" <+> float f <+> text "__" <> int lab
178 ppr (TESTEQ_F f lab) = text "TESTEQ_F" <+> float f <+> text "__" <> int lab
179 ppr (TESTLT_D d lab) = text "TESTLT_D" <+> double d <+> text "__" <> int lab
180 ppr (TESTEQ_D d lab) = text "TESTEQ_D" <+> double d <+> text "__" <> int lab
181 ppr (TESTLT_P i lab) = text "TESTLT_P" <+> int i <+> text "__" <> int lab
182 ppr (TESTEQ_P i lab) = text "TESTEQ_P" <+> int i <+> text "__" <> int lab
183 ppr (JMP lab) = text "JMP" <+> int lab
184 ppr CASEFAIL = text "CASEFAIL"
185 ppr ENTER = text "ENTER"
186 ppr RETURN = text "RETURN"
187 ppr (RETURN_UBX pk) = text "RETURN_UBX " <+> ppr pk
188 ppr (CCALL off marshall_addr) = text "CCALL " <+> int off
189 <+> text "marshall code at"
190 <+> text (show marshall_addr)
191 ppr (SWIZZLE stkoff n) = text "SWIZZLE " <+> text "stkoff" <+> int stkoff
192 <+> text "by" <+> int n
194 -- -----------------------------------------------------------------------------
195 -- The stack use, in words, of each bytecode insn. These _must_ be
196 -- correct, or overestimates of reality, to be safe.
198 -- NOTE: we aggregate the stack use from case alternatives too, so that
199 -- we can do a single stack check at the beginning of a function only.
201 -- This could all be made more accurate by keeping track of a proper
202 -- stack high water mark, but it doesn't seem worth the hassle.
204 protoBCOStackUse :: ProtoBCO a -> Int
205 protoBCOStackUse bco = sum (map bciStackUse (protoBCOInstrs bco))
207 bciStackUse :: BCInstr -> Int
208 bciStackUse STKCHECK{} = 0
209 bciStackUse PUSH_L{} = 1
210 bciStackUse PUSH_LL{} = 2
211 bciStackUse PUSH_LLL{} = 3
212 bciStackUse PUSH_G{} = 1
213 bciStackUse PUSH_PRIMOP{} = 1
214 bciStackUse PUSH_BCO{} = 1
215 bciStackUse (PUSH_ALTS bco) = 2 + protoBCOStackUse bco
216 bciStackUse (PUSH_ALTS_UNLIFTED bco _) = 2 + protoBCOStackUse bco
217 bciStackUse (PUSH_UBX _ nw) = nw
218 bciStackUse PUSH_APPLY_N{} = 1
219 bciStackUse PUSH_APPLY_V{} = 1
220 bciStackUse PUSH_APPLY_F{} = 1
221 bciStackUse PUSH_APPLY_D{} = 1
222 bciStackUse PUSH_APPLY_L{} = 1
223 bciStackUse PUSH_APPLY_P{} = 1
224 bciStackUse PUSH_APPLY_PP{} = 1
225 bciStackUse PUSH_APPLY_PPP{} = 1
226 bciStackUse PUSH_APPLY_PPPP{} = 1
227 bciStackUse PUSH_APPLY_PPPPP{} = 1
228 bciStackUse PUSH_APPLY_PPPPPP{} = 1
229 bciStackUse ALLOC_AP{} = 1
230 bciStackUse ALLOC_PAP{} = 1
231 bciStackUse (UNPACK sz) = sz
232 bciStackUse LABEL{} = 0
233 bciStackUse TESTLT_I{} = 0
234 bciStackUse TESTEQ_I{} = 0
235 bciStackUse TESTLT_F{} = 0
236 bciStackUse TESTEQ_F{} = 0
237 bciStackUse TESTLT_D{} = 0
238 bciStackUse TESTEQ_D{} = 0
239 bciStackUse TESTLT_P{} = 0
240 bciStackUse TESTEQ_P{} = 0
241 bciStackUse CASEFAIL{} = 0
242 bciStackUse JMP{} = 0
243 bciStackUse ENTER{} = 0
244 bciStackUse RETURN{} = 0
245 bciStackUse RETURN_UBX{} = 1
246 bciStackUse CCALL{} = 0
247 bciStackUse SWIZZLE{} = 0
249 -- These insns actually reduce stack use, but we need the high-tide level,
250 -- so can't use this info. Not that it matters much.
251 bciStackUse SLIDE{} = 0
252 bciStackUse MKAP{} = 0
253 bciStackUse PACK{} = 1 -- worst case is PACK 0 words