-
Notifications
You must be signed in to change notification settings - Fork 13
/
asm.go
276 lines (245 loc) · 4.91 KB
/
asm.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package frontend
import (
"math/big"
"github.com/ethereum/go-ethereum/core/vm"
)
var OpCodes = map[string]byte{
// 0x0 range - arithmetic ops
"stop": 0x00,
"add": 0x01,
"mul": 0x02,
"sub": 0x03,
"div": 0x04,
"sdiv": 0x05,
"mod": 0x06,
"smod": 0x07,
"exp": 0x08,
"neg": 0x09,
"lt": 0x0a,
"gt": 0x0b,
"slt": 0x0c,
"sgt": 0x0d,
"eq": 0x0e,
"not": 0x0f,
// 0x10 range - bit ops
"and": 0x10,
"or": 0x11,
"xor": 0x12,
"byte": 0x13,
"addmod": 0x14,
"mulmod": 0x15,
// 0x20 range - crypto
"sha3": 0x20,
// 0x30 range - closure state
"address": 0x30,
"balance": 0x31,
"origin": 0x32,
"caller": 0x33,
"callvalue": 0x34,
"calldataload": 0x35,
"calldatasize": 0x36,
"calldatacopy": 0x37,
"codesize": 0x38,
"codecopy": 0x39,
"gasprice": 0x3a,
// 0x40 range - block operations
"prevhash": 0x40,
"coinbase": 0x41,
"timestamp": 0x42,
"number": 0x43,
"difficulty": 0x44,
"gaslimit": 0x45,
// 0x50 range - 'storage' and execution
"pop": 0x50,
"mload": 0x53,
"mstore": 0x54,
"mstore8": 0x55,
"sload": 0x56,
"sstore": 0x57,
"jump": 0x58,
"jumpi": 0x59,
"pc": 0x5a,
"msize": 0x5b,
"gas": 0x5c,
"dest": 0x5d,
// 0x60 range
"push1": 0x60,
"push2": 0x61,
"push3": 0x62,
"push4": 0x63,
"push5": 0x64,
"push6": 0x65,
"push7": 0x66,
"push8": 0x67,
"push9": 0x68,
"push10": 0x69,
"push11": 0x6a,
"push12": 0x6b,
"push13": 0x6c,
"push14": 0x6d,
"push15": 0x6e,
"push16": 0x6f,
"push17": 0x70,
"push18": 0x71,
"push19": 0x72,
"push20": 0x73,
"push21": 0x74,
"push22": 0x75,
"push23": 0x76,
"push24": 0x77,
"push25": 0x78,
"push26": 0x79,
"push27": 0x7a,
"push28": 0x7b,
"push29": 0x7c,
"push30": 0x7d,
"push31": 0x7e,
"push32": 0x7f,
"dup1": 0x80,
"dup2": 0x81,
"dup3": 0x82,
"dup4": 0x83,
"dup5": 0x84,
"dup6": 0x85,
"dup7": 0x86,
"dup8": 0x87,
"dup9": 0x88,
"dup10": 0x89,
"dup11": 0x8a,
"dup12": 0x8b,
"dup13": 0x8c,
"dup14": 0x8d,
"dup15": 0x8e,
"dup16": 0x8f,
"swap1": 0x90,
"swap2": 0x91,
"swap3": 0x92,
"swap4": 0x93,
"swap5": 0x94,
"swap6": 0x95,
"swap7": 0x96,
"swap8": 0x97,
"swap9": 0x98,
"swap10": 0x99,
"swap11": 0x9a,
"swap12": 0x9b,
"swap13": 0x9c,
"swap14": 0x9d,
"swap15": 0x9e,
"swap16": 0x9f,
// 0xf0 range - closures
"create": 0xf0,
"call": 0xf1,
"return": 0xf2,
// 0x70 range - other
"suicide": 0xff,
}
// Big to bytes
//
// Returns the bytes of a big integer with the size specified by
// **base**
// Attempts to pad the byte array with zeros.
func bigToBytes(num *big.Int, base int) []byte {
ret := make([]byte, base/8)
return append(ret[:len(ret)-len(num.Bytes())], num.Bytes()...)
}
// Is op code
//
// Check whether the given string matches anything in
// the OpCode list
func IsOpCode(s string) bool {
for key, _ := range OpCodes {
if key == s {
return true
}
}
return false
}
// Compile instruction
//
// Attempts to compile and parse the given instruction in "s"
// and returns the byte sequence
func CompileInstr(s interface{}) ([]byte, error) {
switch s := s.(type) {
case vm.OpCode:
return []byte{byte(s)}, nil
case string:
str := s
// Check for pre formatted byte array
// Jumps are preformatted
if []byte(str)[0] == 0 {
return []byte(str), nil
}
num := new(big.Int)
_, success := num.SetString(str, 0)
// Assume regular bytes during compilation
if !success {
num.SetBytes([]byte(str))
}
return num.Bytes(), nil
case int:
return big.NewInt(int64(s)).Bytes(), nil
case []byte:
return new(big.Int).SetBytes(s).Bytes(), nil
}
return nil, nil
}
// Assemble
//
// Assembles the given instructions and returns EVM byte code
func Assemble(instructions ...interface{}) (script []byte, err error) {
for _, val := range instructions {
instr, err := CompileInstr(val)
if err != nil {
return nil, err
}
if len(instr) == 0 {
instr = []byte{0}
}
script = append(script, instr...)
}
return
}
// Pre parse script
//
// Take data apart and attempt to find the "init" section and
// "main" section. `main { } init { }`
func PreParse(data string) (mainInput, initInput string) {
mainInput = getCodeSectionFor("main", data)
if mainInput == "" {
mainInput = data
}
initInput = getCodeSectionFor("init", data)
return
}
// Very, very dumb parser. Heed no attention :-)
func getCodeSectionFor(blockMatcher, input string) string {
curCount := -1
length := len(blockMatcher)
matchfst := rune(blockMatcher[0])
var currStr string
for i, run := range input {
// Find init
if curCount == -1 && run == matchfst && input[i:i+length] == blockMatcher {
curCount = 0
} else if curCount > -1 {
if run == '{' {
curCount++
if curCount == 1 {
continue
}
} else if run == '}' {
curCount--
if curCount == 0 {
// we are done
curCount = -1
break
}
}
if curCount > 0 {
currStr += string(run)
}
}
}
return currStr
}