-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytecode.go
195 lines (162 loc) · 3.88 KB
/
bytecode.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
package core
import (
"bytes"
"encoding/binary"
"fmt"
)
type Instructions []byte
type Opcode byte
func (ins Instructions) String() string {
var out bytes.Buffer
i := 0
for i < len(ins) {
def, err := lookupOpcode(ins[i])
if err != nil {
fmt.Fprintf(&out, "ERROR: %s\n", err)
continue
}
operands, read := readOperands(def, ins[i+1:])
fmt.Fprintf(&out, "\x1b[33m%04d\x1b[0m %s\n", i, ins.fmtInstruction(def, operands))
i += 1 + read
}
return out.String()
}
func (ins Instructions) fmtInstruction(def *Definition, operands []int) string {
operandCount := len(def.operandWidths)
if len(operands) != operandCount {
return fmt.Sprintf("ERROR: operand len %d does not match defined %d\n",
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 operandCount for %s\n", def.name)
}
type Definition struct {
name string
operandWidths []int
}
const (
OpConstant Opcode = iota
OpPop
OpAdd
OpSub
OpMul
OpDiv
OpNegate
OpNot
OpClosure
OpJump
OpLoop // negative jump
OpJumpNotTruthy
OpIterate
OpIterateNext
OpTrue
OpFalse
OpNull
OpArray
OpHash
OpCall
OpReturnValue
OpReturn
OpIndex
OpGetGlobal
OpSetGlobal
OpGetLocal
OpSetLocal
OpGetBuiltin
OpGetFree
OpEq
OpNotEq
OpGreater
OpLess
OpGeq
OpLeq
)
var definitions = map[Opcode]*Definition{
OpConstant: {"OpConstant", []int{2}},
OpPop: {"OpPop", []int{}},
OpAdd: {"OpAdd", []int{}},
OpSub: {"OpSub", []int{}},
OpMul: {"OpMul", []int{}},
OpDiv: {"OpDiv", []int{}},
OpNegate: {"OpNegate", []int{}},
OpNot: {"OpNot", []int{}},
OpTrue: {"OpTrue", []int{}},
OpFalse: {"OpFalse", []int{}},
OpNull: {"OpNull", []int{}},
OpArray: {"OpArray", []int{2}},
OpHash: {"OpHash", []int{2}},
OpCall: {"OpCall", []int{1}},
OpReturnValue: {"OpReturnValue", []int{}},
OpReturn: {"OpReturn", []int{}},
OpClosure: {"OpClosure", []int{2, 1}},
OpJump: {"OpJump", []int{2}},
OpLoop: {"OpLoop", []int{2}},
OpJumpNotTruthy: {"OpJumpNotTruthy", []int{2}},
OpIterate: {"OpIterate", []int{}},
OpIterateNext: {"OpIterateNext", []int{}},
OpIndex: {"OpIndex", []int{}},
OpGetGlobal: {"OpGetGlobal", []int{2}},
OpSetGlobal: {"OpSetGlobal", []int{2}},
OpGetLocal: {"OpGetLocal", []int{1}},
OpSetLocal: {"OpSetLocal", []int{1}},
OpGetBuiltin: {"OpGetBuiltin", []int{1}},
OpGetFree: {"OpGetFree", []int{1}},
OpEq: {"OpEq", []int{}},
OpNotEq: {"OpNotEq", []int{}},
OpGreater: {"OpGreater", []int{}},
OpLess: {"OpLess", []int{}},
OpGeq: {"OpGeq", []int{}},
OpLeq: {"OpLeq", []int{}},
}
func lookupOpcode(op byte) (*Definition, error) {
def, ok := definitions[Opcode(op)]
if !ok {
return nil, fmt.Errorf("opcode %d undefined", op)
}
return def, nil
}
func readOperands(def *Definition, instructions Instructions) ([]int, int) {
operands := make([]int, len(def.operandWidths))
offset := 0
for i, width := range def.operandWidths {
switch width {
case 1:
operands[i] = int(instructions[offset])
case 2:
operands[i] = int(binary.BigEndian.Uint16(instructions[offset:]))
}
offset += width
}
return operands, offset
}
func makeOpcode(op Opcode, operands ...int) []byte {
def, ok := definitions[op]
if !ok {
return []byte{}
}
instructionLen := 1
for _, w := range def.operandWidths {
instructionLen += w
}
instruction := make([]byte, instructionLen)
instruction[0] = byte(op)
offset := 1
for i, o := range operands {
width := def.operandWidths[i]
switch width {
case 1:
instruction[offset] = byte(o)
case 2:
binary.BigEndian.PutUint16(instruction[offset:], uint16(o))
}
offset += width
}
return instruction
}