[project @ 2001-02-06 12:00:17 by sewardj]
[ghc-hetmet.git] / ghc / compiler / ghci / ByteCodeInstr.lhs
1 %
2 % (c) The University of Glasgow 2000
3 %
4 \section[ByteCodeInstrs]{Bytecode instruction definitions}
5
6 \begin{code}
7 module ByteCodeInstr ( BCInstr(..), ProtoBCO(..), 
8                        nameOfProtoBCO, bciStackUse ) where
9
10 #include "HsVersions.h"
11
12 import Outputable
13 import Name             ( Name )
14 import Id               ( Id )
15 import CoreSyn
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
23 \end{code}
24
25 %************************************************************************
26 %*                                                                      *
27 \subsection{Bytecodes, and Outputery.}
28 %*                                                                      *
29 %************************************************************************
30
31 \begin{code}
32
33 data ProtoBCO a 
34    = ProtoBCO a                         -- name, in some sense
35               [BCInstr]                 -- instrs
36                                         -- what the BCO came from
37               (Either [AnnAlt Id VarSet]
38                       (AnnExpr Id VarSet))
39
40 nameOfProtoBCO (ProtoBCO nm insns origin) = nm
41
42 type LocalLabel = Int
43
44 data BCInstr
45    -- Messing with the stack
46    = ARGCHECK  Int
47    | STKCHECK  Int
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-}
52    -- Push a ptr
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
57    -- Pushing literals
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
62
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
68    | UPK_TAG   Int Int Int
69                         -- unpack N non-ptr words from offset M in constructor
70                         -- K words down the stack
71    | PACK      DataCon Int
72                         -- after assembly, the DataCon is an index into the
73                         -- itbl array
74    -- For doing case trees
75    | LABEL     LocalLabel
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
82
83    -- The Int value is a constructor number and therefore
84    -- stored in the insn stream rather than as an offset into
85    -- the literal pool.
86    | TESTLT_P  Int    LocalLabel
87    | TESTEQ_P  Int    LocalLabel
88
89    | CASEFAIL
90    -- To Infinity And Beyond
91    | ENTER
92    | RETURN     PrimRep
93                 -- unboxed value on TOS.  Use tag to find underlying ret itbl
94                 -- and return as per that.
95
96
97 instance Outputable a => Outputable (ProtoBCO a) where
98    ppr (ProtoBCO name instrs origin)
99       = (text "ProtoBCO" <+> ppr name <> colon)
100         $$ nest 6 (vcat (map ppr instrs))
101         $$ case origin of
102               Left alts -> vcat (map (pprCoreAlt.deAnnAlt) alts)
103               Right rhs -> pprCoreExpr (deAnnotate rhs)
104
105 instance Outputable BCInstr where
106    ppr (STKCHECK n)          = text "STKCHECK" <+> int n
107    ppr (ARGCHECK n)          = text "ARGCHECK" <+> int n
108    ppr (PUSH_L offset)       = text "PUSH_L  " <+> int offset
109    ppr (PUSH_LL o1 o2)       = text "PUSH_LL " <+> int o1 <+> int o2
110    ppr (PUSH_LLL o1 o2 o3)   = text "PUSH_LLL" <+> int o1 <+> int o2 <+> int o3
111    ppr (PUSH_G (Left nm))    = text "PUSH_G  " <+> ppr nm
112    ppr (PUSH_G (Right op))   = text "PUSH_G  " <+> text "PrelPrimopWrappers." 
113                                                <> ppr op
114    ppr (PUSH_AS nm pk)       = text "PUSH_AS " <+> ppr nm <+> ppr pk
115    ppr (PUSH_UBX lit nw)     = text "PUSH_UBX" <+> parens (int nw) <+> ppr lit
116    ppr (PUSH_TAG n)          = text "PUSH_TAG" <+> int n
117    ppr (SLIDE n d)           = text "SLIDE   " <+> int n <+> int d
118    ppr (ALLOC sz)            = text "ALLOC   " <+> int sz
119    ppr (MKAP offset sz)      = text "MKAP    " <+> int sz <+> text "words," 
120                                                <+> int offset <+> text "stkoff"
121    ppr (UNPACK sz)           = text "UNPACK  " <+> int sz
122    ppr (UPK_TAG n m k)       = text "UPK_TAG " <+> int n <> text "words" 
123                                                <+> int m <> text "conoff"
124                                                <+> int k <> text "stkoff"
125    ppr (PACK dcon sz)        = text "PACK    " <+> ppr dcon <+> ppr sz
126    ppr (LABEL     lab)       = text "__"       <> int lab <> colon
127    ppr (TESTLT_I  i lab)     = text "TESTLT_I" <+> int i <+> text "__" <> int lab
128    ppr (TESTEQ_I  i lab)     = text "TESTEQ_I" <+> int i <+> text "__" <> int lab
129    ppr (TESTLT_F  f lab)     = text "TESTLT_F" <+> float f <+> text "__" <> int lab
130    ppr (TESTEQ_F  f lab)     = text "TESTEQ_F" <+> float f <+> text "__" <> int lab
131    ppr (TESTLT_D  d lab)     = text "TESTLT_D" <+> double d <+> text "__" <> int lab
132    ppr (TESTEQ_D  d lab)     = text "TESTEQ_D" <+> double d <+> text "__" <> int lab
133    ppr (TESTLT_P  i lab)     = text "TESTLT_P" <+> int i <+> text "__" <> int lab
134    ppr (TESTEQ_P  i lab)     = text "TESTEQ_P" <+> int i <+> text "__" <> int lab
135    ppr CASEFAIL              = text "CASEFAIL"
136    ppr ENTER                 = text "ENTER"
137    ppr (RETURN pk)           = text "RETURN  " <+> ppr pk
138
139
140 -- The stack use, in words, of each bytecode insn.  These _must_ be
141 -- correct, or overestimates of reality, to be safe.
142 bciStackUse :: BCInstr -> Int
143 bciStackUse (STKCHECK n)          = 0
144 bciStackUse (ARGCHECK n)          = 0
145 bciStackUse (PUSH_L offset)       = 1
146 bciStackUse (PUSH_LL o1 o2)       = 2
147 bciStackUse (PUSH_LLL o1 o2 o3)   = 3
148 bciStackUse (PUSH_G globalish)    = 1
149 bciStackUse (PUSH_AS nm pk)       = 2
150 bciStackUse (PUSH_UBX lit nw)     = nw
151 bciStackUse (PUSH_TAG n)          = 1
152 bciStackUse (ALLOC sz)            = 1
153 bciStackUse (UNPACK sz)           = sz
154 bciStackUse (UPK_TAG n m k)       = n + 1{-tag-}
155 bciStackUse (LABEL     lab)       = 0
156 bciStackUse (TESTLT_I  i lab)     = 0
157 bciStackUse (TESTEQ_I  i lab)     = 0
158 bciStackUse (TESTLT_F  f lab)     = 0
159 bciStackUse (TESTEQ_F  f lab)     = 0
160 bciStackUse (TESTLT_D  d lab)     = 0
161 bciStackUse (TESTEQ_D  d lab)     = 0
162 bciStackUse (TESTLT_P  i lab)     = 0
163 bciStackUse (TESTEQ_P  i lab)     = 0
164 bciStackUse CASEFAIL              = 0
165 bciStackUse ENTER                 = 0
166 bciStackUse (RETURN pk)           = 0
167
168 -- These insns actually reduce stack use, but we need the high-tide level,
169 -- so can't use this info.  Not that it matters much.
170 bciStackUse (SLIDE n d)           = 0
171 bciStackUse (MKAP offset sz)      = 0
172 bciStackUse (PACK dcon sz)        = 1 -- worst case is PACK 0 words
173
174 \end{code}