Skip to content

Commit 8c85532

Browse files
committed
core/vm: added parsing utilities
1 parent b196278 commit 8c85532

File tree

4 files changed

+180
-3
lines changed

4 files changed

+180
-3
lines changed

core/vm/jit_optimiser.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ func optimiseProgram(program *Program) {
2626
}()
2727
}
2828

29+
/*
30+
code := Parse(program.code)
31+
for _, test := range [][]OpCode{
32+
[]OpCode{PUSH, PUSH, ADD},
33+
[]OpCode{PUSH, PUSH, SUB},
34+
[]OpCode{PUSH, PUSH, MUL},
35+
[]OpCode{PUSH, PUSH, DIV},
36+
} {
37+
matchCount := 0
38+
MatchFn(code, test, func(i int) bool {
39+
matchCount++
40+
return true
41+
})
42+
fmt.Printf("found %d match count on: %v\n", matchCount, test)
43+
}
44+
*/
45+
2946
for i := 0; i < len(program.instructions); i++ {
3047
instr := program.instructions[i].(instruction)
3148

core/vm/jit_util.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2014 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package vm
18+
19+
// Parse parses all opcodes from the given code byte slice. This function
20+
// performs no error checking and may return non-existing opcodes.
21+
func Parse(code []byte) (opcodes []OpCode) {
22+
for pc := uint64(0); pc < uint64(len(code)); pc++ {
23+
op := OpCode(code[pc])
24+
25+
switch op {
26+
case PUSH1, PUSH2, PUSH3, PUSH4, PUSH5, PUSH6, PUSH7, PUSH8, PUSH9, PUSH10, PUSH11, PUSH12, PUSH13, PUSH14, PUSH15, PUSH16, PUSH17, PUSH18, PUSH19, PUSH20, PUSH21, PUSH22, PUSH23, PUSH24, PUSH25, PUSH26, PUSH27, PUSH28, PUSH29, PUSH30, PUSH31, PUSH32:
27+
a := uint64(op) - uint64(PUSH1) + 1
28+
pc += a
29+
opcodes = append(opcodes, PUSH)
30+
case DUP1, DUP2, DUP3, DUP4, DUP5, DUP6, DUP7, DUP8, DUP9, DUP10, DUP11, DUP12, DUP13, DUP14, DUP15, DUP16:
31+
opcodes = append(opcodes, DUP)
32+
case SWAP1, SWAP2, SWAP3, SWAP4, SWAP5, SWAP6, SWAP7, SWAP8, SWAP9, SWAP10, SWAP11, SWAP12, SWAP13, SWAP14, SWAP15, SWAP16:
33+
opcodes = append(opcodes, SWAP)
34+
default:
35+
opcodes = append(opcodes, op)
36+
}
37+
}
38+
39+
return opcodes
40+
}
41+
42+
// MatchFn searcher for match in the given input and calls matcheFn if it finds
43+
// an appropriate match. matcherFn yields the starting position in the input.
44+
// MatchFn will continue to search for a match until it reacher the end of the
45+
// buffer or if matcherFn return false.
46+
func MatchFn(input, match []OpCode, matcherFn func(int) bool) {
47+
// short circuit if either input or match is empty or if the match is
48+
// greater than the input
49+
if len(input) == 0 || len(match) == 0 || len(match) > len(input) {
50+
return
51+
}
52+
53+
main:
54+
for i, op := range input[:len(input)+1-len(match)] {
55+
// match first opcode and continue search
56+
if op == match[0] {
57+
for j := 1; j < len(match); j++ {
58+
if input[i+j] != match[j] {
59+
continue main
60+
}
61+
}
62+
// check for abort instruction
63+
if !matcherFn(i) {
64+
return
65+
}
66+
}
67+
}
68+
}

core/vm/jit_util_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2014 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package vm
18+
19+
import "testing"
20+
21+
type matchTest struct {
22+
input []OpCode
23+
match []OpCode
24+
matches int
25+
}
26+
27+
func TestMatchFn(t *testing.T) {
28+
tests := []matchTest{
29+
matchTest{
30+
[]OpCode{PUSH1, PUSH1, MSTORE, JUMP},
31+
[]OpCode{PUSH1, MSTORE},
32+
1,
33+
},
34+
matchTest{
35+
[]OpCode{PUSH1, PUSH1, MSTORE, JUMP},
36+
[]OpCode{PUSH1, MSTORE, PUSH1},
37+
0,
38+
},
39+
matchTest{
40+
[]OpCode{},
41+
[]OpCode{PUSH1},
42+
0,
43+
},
44+
}
45+
46+
for i, test := range tests {
47+
var matchCount int
48+
MatchFn(test.input, test.match, func(i int) bool {
49+
matchCount++
50+
return true
51+
})
52+
if matchCount != test.matches {
53+
t.Errorf("match count failed on test[%d]: expected %d matches, got %d", i, test.matches, matchCount)
54+
}
55+
}
56+
}
57+
58+
type parseTest struct {
59+
base OpCode
60+
size int
61+
output OpCode
62+
}
63+
64+
func TestParser(t *testing.T) {
65+
tests := []parseTest{
66+
parseTest{PUSH1, 32, PUSH},
67+
parseTest{DUP1, 16, DUP},
68+
parseTest{SWAP1, 16, SWAP},
69+
parseTest{MSTORE, 1, MSTORE},
70+
}
71+
72+
for _, test := range tests {
73+
for i := 0; i < test.size; i++ {
74+
code := append([]byte{byte(byte(test.base) + byte(i))}, make([]byte, i+1)...)
75+
output := Parse(code)
76+
if len(output) == 0 {
77+
t.Fatal("empty output")
78+
}
79+
if output[0] != test.output {
80+
t.Error("%v failed: expected %v but got %v", test.base+OpCode(i), output[0])
81+
}
82+
}
83+
}
84+
}

core/vm/opcodes.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,20 @@ const (
187187
LOG4
188188
)
189189

190+
// unofficial opcodes used for parsing
191+
const (
192+
PUSH OpCode = 0xb0 + iota
193+
DUP
194+
SWAP
195+
)
196+
190197
const (
191198
// 0xf0 range - closures
192199
CREATE OpCode = 0xf0 + iota
193200
CALL
194201
CALLCODE
195202
RETURN
196203

197-
// 0x70 range - other
198204
SUICIDE = 0xff
199205
)
200206

@@ -347,9 +353,11 @@ var opCodeToString = map[OpCode]string{
347353
CALL: "CALL",
348354
RETURN: "RETURN",
349355
CALLCODE: "CALLCODE",
356+
SUICIDE: "SUICIDE",
350357

351-
// 0x70 range - other
352-
SUICIDE: "SUICIDE",
358+
PUSH: "PUSH",
359+
DUP: "DUP",
360+
SWAP: "SWAP",
353361
}
354362

355363
func (o OpCode) String() string {

0 commit comments

Comments
 (0)