forked from moisespsena-go/ugo
/
opcodes.go
243 lines (233 loc) · 5.74 KB
/
opcodes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright (c) 2020-2023 Ozan Hacıbekiroğlu.
// Use of this source code is governed by a MIT License
// that can be found in the LICENSE file.
package gad
// Opcode represents a single byte operation code.
type Opcode byte
func (o Opcode) String() string {
if int(o) < len(OpcodeNames) {
return OpcodeNames[o]
}
return ""
}
const (
OpCallFlagVarArgs OpCallFlag = 1 << iota
OpCallFlagNamedArgs
OpCallFlagVarNamedArgs
)
type OpCallFlag byte
func (f OpCallFlag) Has(other OpCallFlag) bool {
return (f & other) != 0
}
// List of opcodes
const (
OpNoOp Opcode = iota
OpConstant
OpCall
OpGetGlobal
OpSetGlobal
OpGetLocal
OpSetLocal
OpGetBuiltin
OpBinaryOp
OpUnary
OpEqual
OpNotEqual
OpJump
OpJumpFalsy
OpAndJump
OpOrJump
OpMap
OpArray
OpSliceIndex
OpGetIndex
OpSetIndex
OpNull
OpStdIn
OpStdOut
OpStdErr
OpDotName
OpDotFile
OpIsModule
OpPop
OpGetFree
OpSetFree
OpGetLocalPtr
OpGetFreePtr
OpClosure
OpIterInit
OpIterNext
OpIterNextElse
OpIterKey
OpIterValue
OpLoadModule
OpStoreModule
OpSetupTry
OpSetupCatch
OpSetupFinally
OpThrow
OpFinalizer
OpReturn
OpDefineLocal
OpTrue
OpFalse
OpYes
OpNo
OpCallName
OpJumpNil
OpJumpNotNil
OpKeyValueArray
OpKeyValue
OpCallee
OpArgs
OpNamedArgs
OpTextWriter
OpIsNil
OpNotIsNil
)
// OpcodeNames are string representation of opcodes.
var OpcodeNames = [...]string{
OpNoOp: "NOOP",
OpConstant: "CONSTANT",
OpCall: "CALL",
OpGetGlobal: "GETGLOBAL",
OpSetGlobal: "SETGLOBAL",
OpGetLocal: "GETLOCAL",
OpSetLocal: "SETLOCAL",
OpGetBuiltin: "GETBUILTIN",
OpBinaryOp: "BINARYOP",
OpUnary: "UNARY",
OpEqual: "EQUAL",
OpNotEqual: "NOTEQUAL",
OpJump: "JUMP",
OpJumpFalsy: "JUMPFALSY",
OpAndJump: "ANDJUMP",
OpOrJump: "ORJUMP",
OpMap: "MAP",
OpArray: "ARRAY",
OpSliceIndex: "SLICEINDEX",
OpGetIndex: "GETINDEX",
OpSetIndex: "SETINDEX",
OpNull: "NULL",
OpStdIn: "STDIN",
OpStdOut: "STDOUT",
OpStdErr: "STDERR",
OpDotName: "DOTNAME",
OpDotFile: "DOTFILE",
OpIsModule: "ISMODULE",
OpPop: "POP",
OpGetFree: "GETFREE",
OpSetFree: "SETFREE",
OpGetLocalPtr: "GETLOCALPTR",
OpGetFreePtr: "GETFREEPTR",
OpClosure: "CLOSURE",
OpIterInit: "ITERINIT",
OpIterNext: "ITERNEXT",
OpIterNextElse: "ITERNEXTELSE",
OpIterKey: "ITERKEY",
OpIterValue: "ITERVALUE",
OpLoadModule: "LOADMODULE",
OpStoreModule: "STOREMODULE",
OpReturn: "RETURN",
OpSetupTry: "SETUPTRY",
OpSetupCatch: "SETUPCATCH",
OpSetupFinally: "SETUPFINALLY",
OpThrow: "THROW",
OpFinalizer: "FINALIZER",
OpDefineLocal: "DEFINELOCAL",
OpTrue: "TRUE",
OpFalse: "FALSE",
OpYes: "YES",
OpNo: "NO",
OpCallName: "CALLNAME",
OpJumpNil: "JUMPNULL",
OpJumpNotNil: "JUMPNOTNULL",
OpKeyValueArray: "KVARRAY",
OpKeyValue: "KV",
OpCallee: "CALLEE",
OpArgs: "ARGS",
OpNamedArgs: "NAMEDARGS",
OpIsNil: "ISNIL",
OpNotIsNil: "NOTISNIL",
}
// OpcodeOperands is the number of operands.
var OpcodeOperands = [...][]int{
OpNoOp: {},
OpConstant: {2}, // constant index
OpCall: {1, 1}, // number of arguments, flags
OpGetGlobal: {2}, // constant index
OpSetGlobal: {2}, // constant index
OpGetLocal: {1}, // local variable index
OpSetLocal: {1}, // local variable index
OpGetBuiltin: {2}, // builtin index
OpBinaryOp: {1}, // operator
OpUnary: {1}, // operator
OpEqual: {},
OpNotEqual: {},
OpIsNil: {},
OpNotIsNil: {},
OpJump: {2}, // position
OpJumpFalsy: {2}, // position
OpAndJump: {2}, // position
OpOrJump: {2}, // position
OpMap: {2}, // number of keys and values
OpArray: {2}, // number of items
OpSliceIndex: {},
OpGetIndex: {1}, // number of selectors
OpSetIndex: {},
OpNull: {},
OpStdIn: {},
OpStdOut: {},
OpStdErr: {},
OpDotName: {},
OpDotFile: {},
OpIsModule: {},
OpPop: {},
OpGetFree: {1}, // index
OpSetFree: {1}, // index
OpGetLocalPtr: {1}, // index
OpGetFreePtr: {1}, // index
OpClosure: {2, 1}, // constant index, item count
OpIterInit: {},
OpIterNext: {},
OpIterNextElse: {2, 2}, // true pos, false pos
OpIterKey: {},
OpIterValue: {},
OpLoadModule: {2, 2}, // constant index, module index
OpStoreModule: {2}, // module index
OpReturn: {1}, // number of items (0 or 1)
OpSetupTry: {2, 2},
OpSetupCatch: {},
OpSetupFinally: {},
OpThrow: {1}, // 0:re-throw (system), 1:throw <expression>
OpFinalizer: {1}, // up to error handler index
OpDefineLocal: {1},
OpTrue: {},
OpFalse: {},
OpYes: {},
OpNo: {},
OpCallName: {1, 1}, // number of arguments, flags
OpJumpNil: {2}, // position
OpJumpNotNil: {2}, // position
OpKeyValueArray: {2}, // number of keys and values
OpCallee: {},
OpArgs: {},
OpNamedArgs: {},
OpKeyValue: {1}, // 0: whitout value, 1: with value
}
// ReadOperands reads operands from the bytecode. Given operands slice is used to
// fill operands and is returned to allocate less.
func ReadOperands(numOperands []int, ins []byte, operands []int) ([]int, int) {
operands = operands[:0]
var offset int
for _, width := range numOperands {
switch width {
case 1:
operands = append(operands, int(ins[offset]))
case 2:
operands = append(operands, int(ins[offset+1])|int(ins[offset])<<8)
}
offset += width
}
return operands, offset
}