-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.go
236 lines (201 loc) · 6.25 KB
/
code.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
// Package code implements Opcode semantics for the virtual machine bytecode.
package code
import (
"bytes"
"encoding/binary"
"fmt"
)
// OpCode is a single byte constant specifying an instruction.
type OpCode byte
// Instructions are sequences of opcodes.
type Instructions []byte
const (
// OpConstant is a fetch and push opcode for constants
OpConstant OpCode = iota
// OpAdd pops two integers from the stack and pushes their sum
OpAdd
// OpSub pops two integers from the stack and pushes their difference
OpSub
// OpMul pops two integers from the stack and pushes their product
OpMul
// OpDiv pops two integers from the stack and pushes their quotient
OpDiv
// OpMod pops two integers from the stack and pushes their remainder
OpMod
// OpNot pops a boolean from the stack and pushes it's opposite
OpNot
// OpNeg pops an integer from the stack and pushes it's negation
OpNeg
// OpPop pops an element from the stack
OpPop
// OpTrue pushes a truth value
OpTrue
// OpFalse pushes a false value
OpFalse
// OpEqual pops two elements from the stack pushes true if equal false otherwise.
OpEqual
// OpNotEqual pops two elements from the stack pushes true if not equal false otherwise.
OpNotEqual
// OpGreaterThan pops two elements from stack push true if a > b
OpGreaterThan
// OpGreaterOrEqual pops two elements from the stack pushes true if a >= b
OpGreaterOrEqual
// OpJNE implements Jump if not equal
OpJNE
// OpJump implements jump to address
OpJump
// OpNull pushes the null value to the stack
OpNull
// OpGetGlobal fetches binding values from the globals state
OpGetGlobal
// OpSetGlobal binds a value to an identifier
OpSetGlobal
// OpArray constructs an array object by popping N objects from the stack
OpArray
// OpHashTable constructs an associative array of objects
OpHashTable
// OpIndex reads the object and index and pushes the indexed element to the stack
OpIndex
// OpCall executes function calls
OpCall
// OpReturnValue explicitly pushes function return values to the stack
OpReturnValue
// OpReturn returns from the function call to the caller
OpReturn
// OpGetLocal is used to get local bindings
OpGetLocal
// OpSetLocal is used to set local bindings
OpSetLocal
// OpGetBuiltin is used to fetch built-in function from their scope
OpGetBuiltin
// OpClosure marks closure functions
OpClosure
// OpGetFree is used to get free closure variables
OpGetFree
)
// Definition represents information about opcodes.
type Definition struct {
Name string
OperandWidths []int
}
var lookupTable = map[OpCode]Definition{
OpConstant: {"OpConstant", []int{2}},
OpAdd: {"OpAdd", []int{}},
OpSub: {"OpSub", []int{}},
OpMul: {"OpMul", []int{}},
OpDiv: {"OpDiv", []int{}},
OpMod: {"OpMod", []int{}},
OpPop: {"OpPop", []int{}},
OpTrue: {"OpTrue", []int{}},
OpFalse: {"OpFalse", []int{}},
OpEqual: {"OpEqual", []int{}},
OpNotEqual: {"OpNotEqual", []int{}},
OpGreaterThan: {"OpGreaterThan", []int{}},
OpGreaterOrEqual: {"OpGreaterThanOrEqual", []int{}},
OpNeg: {"OpNeg", []int{}},
OpNot: {"OpNot", []int{}},
OpJNE: {"OpJumpIfNotEqual", []int{2}},
OpJump: {"OpJump", []int{2}},
OpNull: {"OpNull", []int{}},
OpGetGlobal: {"OpGetGlobal", []int{2}},
OpSetGlobal: {"OpSetGlobal", []int{2}},
OpArray: {"OpArray", []int{2}},
OpHashTable: {"OpHashTable", []int{2}},
OpIndex: {"OpIndex", []int{}},
OpCall: {"OpCall", []int{1}},
OpReturnValue: {"OpReturnValue", []int{}},
OpReturn: {"OpReturn", []int{}},
OpGetLocal: {"OpGetLocal", []int{1}},
OpSetLocal: {"OpSetLocal", []int{1}},
OpGetBuiltin: {"OpGetBuiltin", []int{1}},
OpClosure: {"OpClosure", []int{2, 1}},
OpGetFree: {"OpGetFree", []int{1}},
}
// Lookup fetches the opcode definition.
func Lookup(op OpCode) (Definition, error) {
def, ok := lookupTable[op]
if !ok {
return Definition{}, fmt.Errorf("Opcode %d is undefined", op)
}
return def, nil
}
// Make creates an instruction sequence given an opcode and operands.
func Make(op OpCode, operands ...int) []byte {
def, ok := lookupTable[op]
if !ok {
return []byte{}
}
instLen := 1
for _, w := range def.OperandWidths {
instLen += w
}
inst := make([]byte, instLen)
inst[0] = byte(op)
offset := 1
for i, operand := range operands {
width := def.OperandWidths[i]
switch width {
case 2:
binary.BigEndian.PutUint16(inst[offset:], uint16(operand))
case 1:
inst[offset] = byte(operand)
}
offset += width
}
return inst
}
// FormatInstruction returns a pretty printed instruction
func (inst Instructions) FormatInstruction(def Definition, operands []int) string {
operandCount := len(def.OperandWidths)
if len(operands) != operandCount {
return fmt.Sprintf("ERROR : operand length %d does not match defined %d", len(operands), operandCount)
}
switch operandCount {
case 0:
return def.Name
case 1:
return fmt.Sprintf("%s %d", def.Name, operands[0])
case 2:
return fmt.Sprintf("%s %d %d", def.Name, operands[0], operands[1])
}
return fmt.Sprintf("ERROR: unhandled operand count for %s", def.Name)
}
// String implements the stringer interface
func (inst Instructions) String() string {
var out bytes.Buffer
i := 0
for i < len(inst) {
def, err := Lookup(OpCode(inst[i]))
if err != nil {
fmt.Fprintf(&out, "ERROR : %s\n", err)
continue
}
operands, read := ReadOperands(def, inst[i+1:])
fmt.Fprintf(&out, "%04d %s\n", i, inst.FormatInstruction(def, operands))
i += 1 + read
}
return out.String()
}
// ReadUint16 reads a big-endian encoded uint16 from an instruction slice
func ReadUint16(ins Instructions) uint16 {
return binary.BigEndian.Uint16(ins)
}
// ReadOperands parses definition operand width
func ReadOperands(def Definition, ins Instructions) ([]int, int) {
operands := make([]int, len(def.OperandWidths))
offset := 0
for i, width := range def.OperandWidths {
switch width {
case 2:
operands[i] = int(ReadUint16(ins[offset:]))
case 1:
operands[i] = int(ReadUint8(ins[offset:]))
}
offset += width
}
return operands, offset
}
// ReadUint8 reads a single byte integer
func ReadUint8(ins Instructions) uint8 {
return uint8(ins[0])
}