-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopcode.go
More file actions
141 lines (135 loc) · 1.98 KB
/
opcode.go
File metadata and controls
141 lines (135 loc) · 1.98 KB
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
package main
import (
"strconv"
)
// opcode is a two-digit operation code, like 99(END) or 01(ADD). See const
// declaration for concrete values.
type opcode uint8
// newOpcode extracts an opcode from an instruction value (Such as 01 from 12201).
func newOpcode(val int64) opcode {
return opcode(val % 1e2)
}
type opcodeInfo struct {
Name string
ArgNum int
Fn func(p *Program, argIndexes []int)
}
var opcodes = [...]opcodeInfo{
1: {
Name: "Add",
ArgNum: 3,
Fn: Add,
},
2: {
Name: "Multiply",
ArgNum: 3,
Fn: Multiply,
},
3: {
Name: "Input",
ArgNum: 1,
Fn: Input,
},
4: {
Name: "Output",
ArgNum: 1,
Fn: Output,
},
5: {
Name: "Jump non-zero",
ArgNum: 2,
Fn: JumpNonZero,
},
6: {
Name: "Jump zero",
ArgNum: 2,
Fn: JumpZero,
},
7: {
Name: "Less than",
ArgNum: 3,
Fn: LessThan,
},
8: {
Name: "Equal",
ArgNum: 3,
Fn: Equal,
},
9: {
Name: "Add relative base",
ArgNum: 1,
Fn: AddRelativeBase,
},
10: {
Name: "Bitwise And",
ArgNum: 3,
Fn: BitAnd,
},
11: {
Name: "Bitwise Or",
ArgNum: 3,
Fn: BitOr,
},
12: {
Name: "Bitwise Xor",
ArgNum: 3,
Fn: BitXor,
},
13: {
Name: "Division",
ArgNum: 3,
Fn: Division,
},
14: {
Name: "Modulo",
ArgNum: 3,
Fn: Modulo,
},
15: {
Name: "Left Shift",
ArgNum: 3,
Fn: LeftShift,
},
16: {
Name: "Right shift",
ArgNum: 3,
Fn: RightShift,
},
17: {
Name: "Negate",
ArgNum: 2,
Fn: Negate,
},
18: {
Name: "Timestamp",
ArgNum: 1,
Fn: Timestamp,
},
19: {
Name: "Random",
ArgNum: 1,
Fn: Random,
},
20: {
Name: "Absolute",
ArgNum: 2,
Fn: Absolute,
},
80: {
Name: "Syscall",
ArgNum: 3,
Fn: Syscall,
},
99: {
Name: "End",
ArgNum: 0,
Fn: End,
},
}
func (o opcode) String() string {
name := "(" + strconv.Itoa(int(o)) + ")"
if int(o) < len(opcodes) {
name = opcodes[o].Name + name
}
return name
}