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]
39 [Ptr ()] -- malloc'd; free when BCO is GCd
41 nameOfProtoBCO (ProtoBCO nm insns origin malloced) = 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 (Ptr ()))
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 (Ptr ()) -- of the glue code
103 | SWIZZLE Int Int -- to the ptr N words down the stack,
104 -- add M (interpreted as a signed 16-bit entity)
106 -- To Infinity And Beyond
109 -- unboxed value on TOS. Use tag to find underlying ret itbl
110 -- and return as per that.
113 instance Outputable a => Outputable (ProtoBCO a) where
114 ppr (ProtoBCO name instrs origin malloced)
115 = (text "ProtoBCO" <+> ppr name <+> text (show malloced) <> colon)
116 $$ nest 6 (vcat (map ppr instrs))
118 Left alts -> vcat (map (pprCoreAlt.deAnnAlt) alts)
119 Right rhs -> pprCoreExpr (deAnnotate rhs)
121 instance Outputable BCInstr where
122 ppr (STKCHECK n) = text "STKCHECK" <+> int n
123 ppr (ARGCHECK n) = text "ARGCHECK" <+> int n
124 ppr (PUSH_L offset) = text "PUSH_L " <+> int offset
125 ppr (PUSH_LL o1 o2) = text "PUSH_LL " <+> int o1 <+> int o2
126 ppr (PUSH_LLL o1 o2 o3) = text "PUSH_LLL" <+> int o1 <+> int o2 <+> int o3
127 ppr (PUSH_G (Left nm)) = text "PUSH_G " <+> ppr nm
128 ppr (PUSH_G (Right op)) = text "PUSH_G " <+> text "GHC.PrimopWrappers."
130 ppr (PUSH_AS nm pk) = text "PUSH_AS " <+> ppr nm <+> ppr pk
132 ppr (PUSH_UBX (Left lit) nw) = text "PUSH_UBX" <+> parens (int nw) <+> ppr lit
133 ppr (PUSH_UBX (Right aa) nw) = text "PUSH_UBX" <+> parens (int nw) <+> text (show aa)
135 ppr (PUSH_TAG n) = text "PUSH_TAG" <+> int n
136 ppr (SLIDE n d) = text "SLIDE " <+> int n <+> int d
137 ppr (ALLOC sz) = text "ALLOC " <+> int sz
138 ppr (MKAP offset sz) = text "MKAP " <+> int sz <+> text "words,"
139 <+> int offset <+> text "stkoff"
140 ppr (UNPACK sz) = text "UNPACK " <+> int sz
141 ppr (UPK_TAG n m k) = text "UPK_TAG " <+> int n <> text "words"
142 <+> int m <> text "conoff"
143 <+> int k <> text "stkoff"
144 ppr (PACK dcon sz) = text "PACK " <+> ppr dcon <+> ppr sz
145 ppr (LABEL lab) = text "__" <> int lab <> colon
146 ppr (TESTLT_I i lab) = text "TESTLT_I" <+> int i <+> text "__" <> int lab
147 ppr (TESTEQ_I i lab) = text "TESTEQ_I" <+> int i <+> text "__" <> int lab
148 ppr (TESTLT_F f lab) = text "TESTLT_F" <+> float f <+> text "__" <> int lab
149 ppr (TESTEQ_F f lab) = text "TESTEQ_F" <+> float f <+> text "__" <> int lab
150 ppr (TESTLT_D d lab) = text "TESTLT_D" <+> double d <+> text "__" <> int lab
151 ppr (TESTEQ_D d lab) = text "TESTEQ_D" <+> double d <+> text "__" <> int lab
152 ppr (TESTLT_P i lab) = text "TESTLT_P" <+> int i <+> text "__" <> int lab
153 ppr (TESTEQ_P i lab) = text "TESTEQ_P" <+> int i <+> text "__" <> int lab
154 ppr (JMP lab) = text "JMP" <+> int lab
155 ppr CASEFAIL = text "CASEFAIL"
156 ppr ENTER = text "ENTER"
157 ppr (RETURN pk) = text "RETURN " <+> ppr pk
158 ppr (CCALL marshall_addr) = text "CCALL " <+> text "marshall code at"
159 <+> text (show marshall_addr)
160 ppr (SWIZZLE stkoff n) = text "SWIZZLE " <+> text "stkoff" <+> int stkoff
161 <+> text "by" <+> int n
163 -- The stack use, in words, of each bytecode insn. These _must_ be
164 -- correct, or overestimates of reality, to be safe.
165 bciStackUse :: BCInstr -> Int
166 bciStackUse (STKCHECK n) = 0
167 bciStackUse (ARGCHECK n) = 0
168 bciStackUse (PUSH_L offset) = 1
169 bciStackUse (PUSH_LL o1 o2) = 2
170 bciStackUse (PUSH_LLL o1 o2 o3) = 3
171 bciStackUse (PUSH_G globalish) = 1
172 bciStackUse (PUSH_AS nm pk) = 2
173 bciStackUse (PUSH_UBX lit nw) = nw
174 bciStackUse (PUSH_TAG n) = 1
175 bciStackUse (ALLOC sz) = 1
176 bciStackUse (UNPACK sz) = sz
177 bciStackUse (UPK_TAG n m k) = n + 1{-tag-}
178 bciStackUse (LABEL lab) = 0
179 bciStackUse (TESTLT_I i lab) = 0
180 bciStackUse (TESTEQ_I i lab) = 0
181 bciStackUse (TESTLT_F f lab) = 0
182 bciStackUse (TESTEQ_F f lab) = 0
183 bciStackUse (TESTLT_D d lab) = 0
184 bciStackUse (TESTEQ_D d lab) = 0
185 bciStackUse (TESTLT_P i lab) = 0
186 bciStackUse (TESTEQ_P i lab) = 0
187 bciStackUse CASEFAIL = 0
188 bciStackUse (JMP lab) = 0
189 bciStackUse ENTER = 0
190 bciStackUse (RETURN pk) = 0
191 bciStackUse (CCALL marshall_addr) = 0
192 bciStackUse (SWIZZLE stkoff n) = 0
194 -- These insns actually reduce stack use, but we need the high-tide level,
195 -- so can't use this info. Not that it matters much.
196 bciStackUse (SLIDE n d) = 0
197 bciStackUse (MKAP offset sz) = 0
198 bciStackUse (PACK dcon sz) = 1 -- worst case is PACK 0 words