2 % (c) The University of Glasgow 2000
4 \section[ByteCodeInstrs]{Bytecode instruction definitions}
7 module ByteCodeInstr ( BCInstr(..), ProtoBCO(..),
8 nameOfProtoBCO, bciStackUse ) where
10 #include "HsVersions.h"
16 import PprCore ( pprCoreExpr, pprCoreAlt )
17 import Literal ( Literal )
18 import PrimRep ( PrimRep )
19 import DataCon ( DataCon )
20 import VarSet ( VarSet )
21 import PrimOp ( PrimOp )
25 %************************************************************************
27 \subsection{Bytecodes, and Outputery.}
29 %************************************************************************
34 = ProtoBCO a -- name, in some sense
36 -- what the BCO came from
37 (Either [AnnAlt Id VarSet]
40 nameOfProtoBCO (ProtoBCO nm insns origin) = nm
45 -- Messing with the stack
48 -- Push locals (existing bits of the stack)
49 | PUSH_L Int{-offset-}
50 | PUSH_LL Int Int{-2 offsets-}
51 | PUSH_LLL Int Int Int{-3 offsets-}
53 | PUSH_G (Either Name PrimOp)
54 -- Push an alt continuation
55 | PUSH_AS Name PrimRep -- push alts and BCO_ptr_ret_info
56 -- PrimRep so we know which itbl
58 | PUSH_UBX Literal Int
59 -- push this int/float/double, NO TAG, on the stack
60 -- Int is # of words to copy from literal pool
61 | PUSH_TAG Int -- push this tag on the stack
63 | SLIDE Int{-this many-} Int{-down by this much-}
64 -- To do with the heap
65 | ALLOC Int -- make an AP_UPD with this many payload words, zeroed
66 | MKAP Int{-ptr to AP_UPD is this far down stack-} Int{-# words-}
67 | UNPACK Int -- unpack N ptr words from t.o.s Constr
69 -- unpack N non-ptr words from offset M in constructor
70 -- K words down the stack
72 -- after assembly, the DataCon is an index into the
74 -- For doing case trees
76 | TESTLT_I Int LocalLabel
77 | TESTEQ_I Int LocalLabel
78 | TESTLT_F Float LocalLabel
79 | TESTEQ_F Float LocalLabel
80 | TESTLT_D Double LocalLabel
81 | TESTEQ_D Double LocalLabel
83 -- The Int value is a constructor number and therefore
84 -- stored in the insn stream rather than as an offset into
86 | TESTLT_P Int LocalLabel
87 | TESTEQ_P Int LocalLabel
92 -- To Infinity And Beyond
95 -- unboxed value on TOS. Use tag to find underlying ret itbl
96 -- and return as per that.
99 instance Outputable a => Outputable (ProtoBCO a) where
100 ppr (ProtoBCO name instrs origin)
101 = (text "ProtoBCO" <+> ppr name <> colon)
102 $$ nest 6 (vcat (map ppr instrs))
104 Left alts -> vcat (map (pprCoreAlt.deAnnAlt) alts)
105 Right rhs -> pprCoreExpr (deAnnotate rhs)
107 instance Outputable BCInstr where
108 ppr (STKCHECK n) = text "STKCHECK" <+> int n
109 ppr (ARGCHECK n) = text "ARGCHECK" <+> int n
110 ppr (PUSH_L offset) = text "PUSH_L " <+> int offset
111 ppr (PUSH_LL o1 o2) = text "PUSH_LL " <+> int o1 <+> int o2
112 ppr (PUSH_LLL o1 o2 o3) = text "PUSH_LLL" <+> int o1 <+> int o2 <+> int o3
113 ppr (PUSH_G (Left nm)) = text "PUSH_G " <+> ppr nm
114 ppr (PUSH_G (Right op)) = text "PUSH_G " <+> text "PrelPrimopWrappers."
116 ppr (PUSH_AS nm pk) = text "PUSH_AS " <+> ppr nm <+> ppr pk
117 ppr (PUSH_UBX lit nw) = text "PUSH_UBX" <+> parens (int nw) <+> ppr lit
118 ppr (PUSH_TAG n) = text "PUSH_TAG" <+> int n
119 ppr (SLIDE n d) = text "SLIDE " <+> int n <+> int d
120 ppr (ALLOC sz) = text "ALLOC " <+> int sz
121 ppr (MKAP offset sz) = text "MKAP " <+> int sz <+> text "words,"
122 <+> int offset <+> text "stkoff"
123 ppr (UNPACK sz) = text "UNPACK " <+> int sz
124 ppr (UPK_TAG n m k) = text "UPK_TAG " <+> int n <> text "words"
125 <+> int m <> text "conoff"
126 <+> int k <> text "stkoff"
127 ppr (PACK dcon sz) = text "PACK " <+> ppr dcon <+> ppr sz
128 ppr (LABEL lab) = text "__" <> int lab <> colon
129 ppr (TESTLT_I i lab) = text "TESTLT_I" <+> int i <+> text "__" <> int lab
130 ppr (TESTEQ_I i lab) = text "TESTEQ_I" <+> int i <+> text "__" <> int lab
131 ppr (TESTLT_F f lab) = text "TESTLT_F" <+> float f <+> text "__" <> int lab
132 ppr (TESTEQ_F f lab) = text "TESTEQ_F" <+> float f <+> text "__" <> int lab
133 ppr (TESTLT_D d lab) = text "TESTLT_D" <+> double d <+> text "__" <> int lab
134 ppr (TESTEQ_D d lab) = text "TESTEQ_D" <+> double d <+> text "__" <> int lab
135 ppr (TESTLT_P i lab) = text "TESTLT_P" <+> int i <+> text "__" <> int lab
136 ppr (TESTEQ_P i lab) = text "TESTEQ_P" <+> int i <+> text "__" <> int lab
137 ppr (JMP lab) = text "JMP" <+> int lab
138 ppr CASEFAIL = text "CASEFAIL"
139 ppr ENTER = text "ENTER"
140 ppr (RETURN pk) = text "RETURN " <+> ppr pk
143 -- The stack use, in words, of each bytecode insn. These _must_ be
144 -- correct, or overestimates of reality, to be safe.
145 bciStackUse :: BCInstr -> Int
146 bciStackUse (STKCHECK n) = 0
147 bciStackUse (ARGCHECK n) = 0
148 bciStackUse (PUSH_L offset) = 1
149 bciStackUse (PUSH_LL o1 o2) = 2
150 bciStackUse (PUSH_LLL o1 o2 o3) = 3
151 bciStackUse (PUSH_G globalish) = 1
152 bciStackUse (PUSH_AS nm pk) = 2
153 bciStackUse (PUSH_UBX lit nw) = nw
154 bciStackUse (PUSH_TAG n) = 1
155 bciStackUse (ALLOC sz) = 1
156 bciStackUse (UNPACK sz) = sz
157 bciStackUse (UPK_TAG n m k) = n + 1{-tag-}
158 bciStackUse (LABEL lab) = 0
159 bciStackUse (TESTLT_I i lab) = 0
160 bciStackUse (TESTEQ_I i lab) = 0
161 bciStackUse (TESTLT_F f lab) = 0
162 bciStackUse (TESTEQ_F f lab) = 0
163 bciStackUse (TESTLT_D d lab) = 0
164 bciStackUse (TESTEQ_D d lab) = 0
165 bciStackUse (TESTLT_P i lab) = 0
166 bciStackUse (TESTEQ_P i lab) = 0
167 bciStackUse CASEFAIL = 0
168 bciStackUse (JMP lab) = 0
169 bciStackUse ENTER = 0
170 bciStackUse (RETURN pk) = 0
172 -- These insns actually reduce stack use, but we need the high-tide level,
173 -- so can't use this info. Not that it matters much.
174 bciStackUse (SLIDE n d) = 0
175 bciStackUse (MKAP offset sz) = 0
176 bciStackUse (PACK dcon sz) = 1 -- worst case is PACK 0 words