-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbytecode.go
155 lines (137 loc) · 2.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
package vmgen
import (
"encoding/binary"
"fmt"
)
// Bytecode is composed of commands
type Bytecode struct {
commands []*Command
}
// Command ...
type Command struct {
mnemonic string
parameters []byte
isMarker bool
offset int
}
type CostFunc func(interface{}) int
type Instruction struct {
Opcode uint
Cost CostFunc
}
type InstructionMap map[string]Instruction
func (im InstructionMap) AddAll(m InstructionMap) {
if im == nil {
im = make(InstructionMap)
}
for k, v := range m {
im[k] = v
}
}
// Length ...
func (b *Bytecode) Length() int {
return len(b.commands)
}
// AddCommand to the current bytecode
func (b *Bytecode) AddCommand(c *Command) {
if b.commands == nil {
b.commands = make([]*Command, 0)
}
b.commands = append(b.commands, c)
}
// Add ...
func (b *Bytecode) Add(mnemonic string, parameters ...byte) {
c := Command{
mnemonic: mnemonic,
parameters: parameters,
isMarker: false,
}
b.AddCommand(&c)
}
// AddMarker ...
func (b *Bytecode) AddMarker(mnemonic string, offset int) {
c := Command{
mnemonic: mnemonic,
offset: offset,
isMarker: true,
}
b.AddCommand(&c)
}
// Concat another bytecode struct onto ours
func (b *Bytecode) Concat(other Bytecode) {
if other.commands == nil {
return
}
b.commands = append(b.commands, other.commands...)
}
// Compare to another bytecode
func (b *Bytecode) Compare(other Bytecode) bool {
if b.commands == nil && other.commands == nil {
return true
}
if other.commands == nil || b.commands == nil {
return false
}
if len(b.commands) != len(other.commands) {
return false
}
for i, c := range b.commands {
o := other.commands[i]
if c.mnemonic != o.mnemonic {
return false
}
for j, p := range c.parameters {
if p != o.parameters[j] {
return false
}
}
}
return true
}
// Format bytecode for output
func (b *Bytecode) Format() string {
if b.commands == nil {
return "No commands"
}
s := fmt.Sprintf("%d commands\n", len(b.commands))
for i, c := range b.commands {
// have to do this manually
s += fmt.Sprintf("%d | %s %v\n", i+1, c.mnemonic, c.parameters)
}
return s
}
// Finalise ...
func (b *Bytecode) Finalise() {
for i, c := range b.commands {
if c.isMarker {
c.parameters = make([]byte, 8)
binary.LittleEndian.PutUint64(c.parameters, uint64(c.offset+i))
}
}
}
// CompareMnemonics ...
func (b *Bytecode) CompareMnemonics(test []string) bool {
if len(test) != len(b.commands) {
return false
}
for i, t := range test {
if t != b.commands[i].mnemonic {
return false
}
}
return true
}
type Generator interface {
Instructions() InstructionMap
}
func (b *Bytecode) Generate(g Generator) []byte {
bytes := make([]byte, 0)
is := g.Instructions()
for _, c := range b.commands {
bs := make([]byte, 4)
binary.LittleEndian.PutUint32(bs, uint32(is[c.mnemonic].Opcode))
bytes = append(bytes, bs...)
bytes = append(bytes, c.parameters...)
}
return bytes
}