Skip to content

Commit f311f78

Browse files
committed
Unflatten builtins opcodes
1 parent 9caceee commit f311f78

File tree

6 files changed

+28
-21
lines changed

6 files changed

+28
-21
lines changed

builtin/builtin.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,19 @@ var (
1414
type Function struct {
1515
Name string
1616
Func func(args ...interface{}) (interface{}, error)
17-
Opcode byte
17+
Opcode int
1818
Types []reflect.Type
1919
Validate func(args []reflect.Type) (reflect.Type, error)
2020
}
2121

2222
const (
23-
Opcode byte = iota + 100
24-
Len
23+
Len = iota + 1
2524
Abs
2625
Int
2726
Float
2827
)
2928

30-
var Builtins = map[byte]*Function{
29+
var Builtins = map[int]*Function{
3130
Len: {
3231
Name: "len",
3332
Opcode: Len,

compiler/compiler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ func (c *compiler) CallNode(node *ast.CallNode) {
528528
}
529529
if node.Func != nil {
530530
if node.Func.Opcode > 0 {
531-
c.emit(node.Func.Opcode)
531+
c.emit(OpBuiltin, node.Func.Opcode)
532532
return
533533
}
534534
switch len(node.Arguments) {

vm/opcodes.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package vm
22

3-
type Opcode = byte
3+
type Opcode byte
44

55
const (
66
OpPush Opcode = iota
@@ -54,6 +54,7 @@ const (
5454
OpCallN
5555
OpCallFast
5656
OpCallTyped
57+
OpBuiltin
5758
OpArray
5859
OpMap
5960
OpLen

vm/program.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,12 @@ func (program *Program) Disassemble() string {
6060
}
6161
out += fmt.Sprintf("%v\t%v\t%v\t%v\n", pp, label, arg, c)
6262
}
63-
64-
if op > builtin.Opcode {
65-
f, ok := builtin.Builtins[op]
63+
builtIn := func(label string) {
64+
f, ok := builtin.Builtins[arg]
6665
if !ok {
67-
panic(fmt.Sprintf("unknown builtin %v", op))
66+
panic(fmt.Sprintf("unknown builtin %v", arg))
6867
}
6968
out += fmt.Sprintf("%v\t%v\t%v\n", pp, "OpBuiltin", f.Name)
70-
continue
7169
}
7270

7371
switch op {
@@ -224,6 +222,9 @@ func (program *Program) Disassemble() string {
224222
case OpCallTyped:
225223
argument("OpCallTyped")
226224

225+
case OpBuiltin:
226+
builtIn("OpBuiltin")
227+
227228
case OpArray:
228229
code("OpArray")
229230

vm/program_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
func TestProgram_Disassemble(t *testing.T) {
1111
for op := vm.OpPush; op < vm.OpEnd; op++ {
1212
program := vm.Program{
13-
Constants: []interface{}{true},
13+
Constants: []interface{}{1, 2},
1414
Bytecode: []vm.Opcode{op},
15-
Arguments: []int{0},
15+
Arguments: []int{1},
1616
}
1717
d := program.Disassemble()
1818
if strings.Contains(d, "\t0x") {

vm/vm.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -442,17 +442,23 @@ func (vm *VM) Run(program *Program, env interface{}) (_ interface{}, err error)
442442
case OpEnd:
443443
vm.scopes = vm.scopes[:len(vm.scopes)-1]
444444

445-
case builtin.Len:
446-
vm.push(runtime.Len(vm.pop()))
445+
case OpBuiltin:
446+
switch arg {
447+
case builtin.Len:
448+
vm.push(runtime.Len(vm.pop()))
447449

448-
case builtin.Abs:
449-
vm.push(runtime.Abs(vm.pop()))
450+
case builtin.Abs:
451+
vm.push(runtime.Abs(vm.pop()))
450452

451-
case builtin.Int:
452-
vm.push(runtime.ToInt(vm.pop()))
453+
case builtin.Int:
454+
vm.push(runtime.ToInt(vm.pop()))
455+
456+
case builtin.Float:
457+
vm.push(runtime.ToFloat64(vm.pop()))
453458

454-
case builtin.Float:
455-
vm.push(runtime.ToFloat64(vm.pop()))
459+
default:
460+
panic(fmt.Sprintf("unknown builtin %v", arg))
461+
}
456462

457463
default:
458464
panic(fmt.Sprintf("unknown bytecode %#x", op))

0 commit comments

Comments
 (0)