2 % (c) The University of Glasgow 2000
4 \section[ByteCodeInstrs]{Bytecode instruction definitions}
7 module ByteCodeInstr ( BCInstr(..), ProtoBCO(..), nameOfProtoBCO ) where
9 #include "HsVersions.h"
15 import PprCore ( pprCoreExpr, pprCoreAlt )
16 import Literal ( Literal )
17 import PrimRep ( PrimRep )
18 import DataCon ( DataCon )
19 import VarSet ( VarSet )
20 import PrimOp ( PrimOp )
24 %************************************************************************
26 \subsection{Bytecodes, and Outputery.}
28 %************************************************************************
33 = ProtoBCO a -- name, in some sense
35 -- what the BCO came from
36 (Either [AnnAlt Id VarSet]
39 nameOfProtoBCO (ProtoBCO nm insns origin) = nm
44 -- Messing with the stack
46 -- Push locals (existing bits of the stack)
47 | PUSH_L Int{-offset-}
48 | PUSH_LL Int Int{-2 offsets-}
49 | PUSH_LLL Int Int Int{-3 offsets-}
51 | PUSH_G (Either Name PrimOp)
52 -- Push an alt continuation
53 | PUSH_AS Name PrimRep -- push alts and BCO_ptr_ret_info
54 -- PrimRep so we know which itbl
56 | PUSH_UBX Literal Int
57 -- push this int/float/double, NO TAG, on the stack
58 -- Int is # of words to copy from literal pool
59 | PUSH_TAG Int -- push this tag on the stack
61 | SLIDE Int{-this many-} Int{-down by this much-}
62 -- To do with the heap
63 | ALLOC Int -- make an AP_UPD with this many payload words, zeroed
64 | MKAP Int{-ptr to AP_UPD is this far down stack-} Int{-# words-}
65 | UNPACK Int -- unpack N ptr words from t.o.s Constr
67 -- unpack N non-ptr words from offset M in constructor
68 -- K words down the stack
70 -- after assembly, the DataCon is an index into the
72 -- For doing case trees
74 | TESTLT_I Int LocalLabel
75 | TESTEQ_I Int LocalLabel
76 | TESTLT_F Float LocalLabel
77 | TESTEQ_F Float LocalLabel
78 | TESTLT_D Double LocalLabel
79 | TESTEQ_D Double LocalLabel
81 -- The Int value is a constructor number and therefore
82 -- stored in the insn stream rather than as an offset into
84 | TESTLT_P Int LocalLabel
85 | TESTEQ_P Int LocalLabel
88 -- To Infinity And Beyond
91 -- unboxed value on TOS. Use tag to find underlying ret itbl
92 -- and return as per that.
95 instance Outputable BCInstr where
96 ppr (ARGCHECK n) = text "ARGCHECK" <+> int n
97 ppr (PUSH_L offset) = text "PUSH_L " <+> int offset
98 ppr (PUSH_LL o1 o2) = text "PUSH_LL " <+> int o1 <+> int o2
99 ppr (PUSH_LLL o1 o2 o3) = text "PUSH_LLL" <+> int o1 <+> int o2 <+> int o3
100 ppr (PUSH_G (Left nm)) = text "PUSH_G " <+> ppr nm
101 ppr (PUSH_G (Right op)) = text "PUSH_G " <+> text "PrelPrimopWrappers."
103 ppr (PUSH_AS nm pk) = text "PUSH_AS " <+> ppr nm <+> ppr pk
104 ppr (PUSH_UBX lit nw) = text "PUSH_UBX" <+> parens (int nw) <+> ppr lit
105 ppr (PUSH_TAG n) = text "PUSH_TAG" <+> int n
106 ppr (SLIDE n d) = text "SLIDE " <+> int n <+> int d
107 ppr (ALLOC sz) = text "ALLOC " <+> int sz
108 ppr (MKAP offset sz) = text "MKAP " <+> int sz <+> text "words,"
109 <+> int offset <+> text "stkoff"
110 ppr (UNPACK sz) = text "UNPACK " <+> int sz
111 ppr (UPK_TAG n m k) = text "UPK_TAG " <+> int n <> text "words"
112 <+> int m <> text "conoff"
113 <+> int k <> text "stkoff"
114 ppr (PACK dcon sz) = text "PACK " <+> ppr dcon <+> ppr sz
115 ppr (LABEL lab) = text "__" <> int lab <> colon
116 ppr (TESTLT_I i lab) = text "TESTLT_I" <+> int i <+> text "__" <> int lab
117 ppr (TESTEQ_I i lab) = text "TESTEQ_I" <+> int i <+> text "__" <> int lab
118 ppr (TESTLT_F f lab) = text "TESTLT_F" <+> float f <+> text "__" <> int lab
119 ppr (TESTEQ_F f lab) = text "TESTEQ_F" <+> float f <+> text "__" <> int lab
120 ppr (TESTLT_D d lab) = text "TESTLT_D" <+> double d <+> text "__" <> int lab
121 ppr (TESTEQ_D d lab) = text "TESTEQ_D" <+> double d <+> text "__" <> int lab
122 ppr (TESTLT_P i lab) = text "TESTLT_P" <+> int i <+> text "__" <> int lab
123 ppr (TESTEQ_P i lab) = text "TESTEQ_P" <+> int i <+> text "__" <> int lab
124 ppr CASEFAIL = text "CASEFAIL"
125 ppr ENTER = text "ENTER"
126 ppr (RETURN pk) = text "RETURN " <+> ppr pk
128 instance Outputable a => Outputable (ProtoBCO a) where
129 ppr (ProtoBCO name instrs origin)
130 = (text "ProtoBCO" <+> ppr name <> colon)
131 $$ nest 6 (vcat (map ppr instrs))
133 Left alts -> vcat (map (pprCoreAlt.deAnnAlt) alts)
134 Right rhs -> pprCoreExpr (deAnnotate rhs)