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 )
22 import Foreign ( Addr )
26 %************************************************************************
28 \subsection{Bytecodes, and Outputery.}
30 %************************************************************************
35 = ProtoBCO a -- name, in some sense
37 -- what the BCO came from
38 (Either [AnnAlt Id VarSet]
41 nameOfProtoBCO (ProtoBCO nm insns origin) = nm
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_G (Either Name PrimOp)
55 -- Push an alt continuation
56 | PUSH_AS Name PrimRep -- push alts and BCO_ptr_ret_info
57 -- PrimRep so we know which itbl
59 | PUSH_UBX (Either Literal Addr)
60 Int -- push this int/float/double/addr, NO TAG, on the stack
61 -- Int is # of words to copy from literal pool
62 -- Eitherness reflects the difficulty of dealing with
63 -- MachAddr here, mostly due to the excessive
64 -- (and unnecessary) restrictions imposed by the designers
65 -- of the new Foreign library. In particular it is quite
66 -- impossible to convert an Addr to any other integral type,
67 -- and it appears impossible to get hold of the bits of an
68 -- addr, even though we need to to assemble BCOs.
70 | PUSH_TAG Int -- push this tag on the stack
72 | SLIDE Int{-this many-} Int{-down by this much-}
73 -- To do with the heap
74 | ALLOC Int -- make an AP_UPD with this many payload words, zeroed
75 | MKAP Int{-ptr to AP_UPD is this far down stack-} Int{-# words-}
76 | UNPACK Int -- unpack N ptr words from t.o.s Constr
78 -- unpack N non-ptr words from offset M in constructor
79 -- K words down the stack
81 -- after assembly, the DataCon is an index into the
83 -- For doing case trees
85 | TESTLT_I Int LocalLabel
86 | TESTEQ_I Int LocalLabel
87 | TESTLT_F Float LocalLabel
88 | TESTEQ_F Float LocalLabel
89 | TESTLT_D Double LocalLabel
90 | TESTEQ_D Double LocalLabel
92 -- The Int value is a constructor number and therefore
93 -- stored in the insn stream rather than as an offset into
95 | TESTLT_P Int LocalLabel
96 | TESTEQ_P Int LocalLabel
101 -- For doing calls to C (via glue code generated by ByteCodeFFI)
102 | CCALL Addr -- of the glue code
104 -- To Infinity And Beyond
107 -- unboxed value on TOS. Use tag to find underlying ret itbl
108 -- and return as per that.
111 instance Outputable a => Outputable (ProtoBCO a) where
112 ppr (ProtoBCO name instrs origin)
113 = (text "ProtoBCO" <+> ppr name <> colon)
114 $$ nest 6 (vcat (map ppr instrs))
116 Left alts -> vcat (map (pprCoreAlt.deAnnAlt) alts)
117 Right rhs -> pprCoreExpr (deAnnotate rhs)
119 instance Outputable BCInstr where
120 ppr (STKCHECK n) = text "STKCHECK" <+> int n
121 ppr (ARGCHECK n) = text "ARGCHECK" <+> int n
122 ppr (PUSH_L offset) = text "PUSH_L " <+> int offset
123 ppr (PUSH_LL o1 o2) = text "PUSH_LL " <+> int o1 <+> int o2
124 ppr (PUSH_LLL o1 o2 o3) = text "PUSH_LLL" <+> int o1 <+> int o2 <+> int o3
125 ppr (PUSH_G (Left nm)) = text "PUSH_G " <+> ppr nm
126 ppr (PUSH_G (Right op)) = text "PUSH_G " <+> text "PrelPrimopWrappers."
128 ppr (PUSH_AS nm pk) = text "PUSH_AS " <+> ppr nm <+> ppr pk
130 ppr (PUSH_UBX (Left lit) nw) = text "PUSH_UBX" <+> parens (int nw) <+> ppr lit
131 ppr (PUSH_UBX (Right aa) nw) = text "PUSH_UBX" <+> parens (int nw) <+> text (show aa)
133 ppr (PUSH_TAG n) = text "PUSH_TAG" <+> int n
134 ppr (SLIDE n d) = text "SLIDE " <+> int n <+> int d
135 ppr (ALLOC sz) = text "ALLOC " <+> int sz
136 ppr (MKAP offset sz) = text "MKAP " <+> int sz <+> text "words,"
137 <+> int offset <+> text "stkoff"
138 ppr (UNPACK sz) = text "UNPACK " <+> int sz
139 ppr (UPK_TAG n m k) = text "UPK_TAG " <+> int n <> text "words"
140 <+> int m <> text "conoff"
141 <+> int k <> text "stkoff"
142 ppr (PACK dcon sz) = text "PACK " <+> ppr dcon <+> ppr sz
143 ppr (LABEL lab) = text "__" <> int lab <> colon
144 ppr (TESTLT_I i lab) = text "TESTLT_I" <+> int i <+> text "__" <> int lab
145 ppr (TESTEQ_I i lab) = text "TESTEQ_I" <+> int i <+> text "__" <> int lab
146 ppr (TESTLT_F f lab) = text "TESTLT_F" <+> float f <+> text "__" <> int lab
147 ppr (TESTEQ_F f lab) = text "TESTEQ_F" <+> float f <+> text "__" <> int lab
148 ppr (TESTLT_D d lab) = text "TESTLT_D" <+> double d <+> text "__" <> int lab
149 ppr (TESTEQ_D d lab) = text "TESTEQ_D" <+> double d <+> text "__" <> int lab
150 ppr (TESTLT_P i lab) = text "TESTLT_P" <+> int i <+> text "__" <> int lab
151 ppr (TESTEQ_P i lab) = text "TESTEQ_P" <+> int i <+> text "__" <> int lab
152 ppr (JMP lab) = text "JMP" <+> int lab
153 ppr CASEFAIL = text "CASEFAIL"
154 ppr ENTER = text "ENTER"
155 ppr (RETURN pk) = text "RETURN " <+> ppr pk
156 ppr (CCALL marshall_addr) = text "CCALL " <+> text "marshall code at"
157 <+> text (show marshall_addr)
159 -- The stack use, in words, of each bytecode insn. These _must_ be
160 -- correct, or overestimates of reality, to be safe.
161 bciStackUse :: BCInstr -> Int
162 bciStackUse (STKCHECK n) = 0
163 bciStackUse (ARGCHECK n) = 0
164 bciStackUse (PUSH_L offset) = 1
165 bciStackUse (PUSH_LL o1 o2) = 2
166 bciStackUse (PUSH_LLL o1 o2 o3) = 3
167 bciStackUse (PUSH_G globalish) = 1
168 bciStackUse (PUSH_AS nm pk) = 2
169 bciStackUse (PUSH_UBX lit nw) = nw
170 bciStackUse (PUSH_TAG n) = 1
171 bciStackUse (ALLOC sz) = 1
172 bciStackUse (UNPACK sz) = sz
173 bciStackUse (UPK_TAG n m k) = n + 1{-tag-}
174 bciStackUse (LABEL lab) = 0
175 bciStackUse (TESTLT_I i lab) = 0
176 bciStackUse (TESTEQ_I i lab) = 0
177 bciStackUse (TESTLT_F f lab) = 0
178 bciStackUse (TESTEQ_F f lab) = 0
179 bciStackUse (TESTLT_D d lab) = 0
180 bciStackUse (TESTEQ_D d lab) = 0
181 bciStackUse (TESTLT_P i lab) = 0
182 bciStackUse (TESTEQ_P i lab) = 0
183 bciStackUse CASEFAIL = 0
184 bciStackUse (JMP lab) = 0
185 bciStackUse ENTER = 0
186 bciStackUse (RETURN pk) = 0
187 bciStackUse (CCALL marshall_addr) = 0
189 -- These insns actually reduce stack use, but we need the high-tide level,
190 -- so can't use this info. Not that it matters much.
191 bciStackUse (SLIDE n d) = 0
192 bciStackUse (MKAP offset sz) = 0
193 bciStackUse (PACK dcon sz) = 1 -- worst case is PACK 0 words