aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeffrey Wilcke <geffobscura@gmail.com>2015-10-12 19:33:46 +0800
committerJeffrey Wilcke <geffobscura@gmail.com>2015-10-17 04:30:42 +0800
commit8c855324128fa824bb6a7ea8f4ed1224e727fad0 (patch)
treeba67d61d7e5b96546788b4784c1a8b9e77a48b07
parentb19627804400901ba003f6e401366cb7d3c4290f (diff)
downloadgo-tangerine-8c855324128fa824bb6a7ea8f4ed1224e727fad0.tar.gz
go-tangerine-8c855324128fa824bb6a7ea8f4ed1224e727fad0.tar.zst
go-tangerine-8c855324128fa824bb6a7ea8f4ed1224e727fad0.zip
core/vm: added parsing utilities
-rw-r--r--core/vm/jit_optimiser.go17
-rw-r--r--core/vm/jit_util.go68
-rw-r--r--core/vm/jit_util_test.go84
-rw-r--r--core/vm/opcodes.go14
4 files changed, 180 insertions, 3 deletions
diff --git a/core/vm/jit_optimiser.go b/core/vm/jit_optimiser.go
index 4823cc1a0..845ffbbdf 100644
--- a/core/vm/jit_optimiser.go
+++ b/core/vm/jit_optimiser.go
@@ -26,6 +26,23 @@ func optimiseProgram(program *Program) {
}()
}
+ /*
+ code := Parse(program.code)
+ for _, test := range [][]OpCode{
+ []OpCode{PUSH, PUSH, ADD},
+ []OpCode{PUSH, PUSH, SUB},
+ []OpCode{PUSH, PUSH, MUL},
+ []OpCode{PUSH, PUSH, DIV},
+ } {
+ matchCount := 0
+ MatchFn(code, test, func(i int) bool {
+ matchCount++
+ return true
+ })
+ fmt.Printf("found %d match count on: %v\n", matchCount, test)
+ }
+ */
+
for i := 0; i < len(program.instructions); i++ {
instr := program.instructions[i].(instruction)
diff --git a/core/vm/jit_util.go b/core/vm/jit_util.go
new file mode 100644
index 000000000..0d3d6d701
--- /dev/null
+++ b/core/vm/jit_util.go
@@ -0,0 +1,68 @@
+// Copyright 2014 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+// Parse parses all opcodes from the given code byte slice. This function
+// performs no error checking and may return non-existing opcodes.
+func Parse(code []byte) (opcodes []OpCode) {
+ for pc := uint64(0); pc < uint64(len(code)); pc++ {
+ op := OpCode(code[pc])
+
+ switch op {
+ 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:
+ a := uint64(op) - uint64(PUSH1) + 1
+ pc += a
+ opcodes = append(opcodes, PUSH)
+ case DUP1, DUP2, DUP3, DUP4, DUP5, DUP6, DUP7, DUP8, DUP9, DUP10, DUP11, DUP12, DUP13, DUP14, DUP15, DUP16:
+ opcodes = append(opcodes, DUP)
+ case SWAP1, SWAP2, SWAP3, SWAP4, SWAP5, SWAP6, SWAP7, SWAP8, SWAP9, SWAP10, SWAP11, SWAP12, SWAP13, SWAP14, SWAP15, SWAP16:
+ opcodes = append(opcodes, SWAP)
+ default:
+ opcodes = append(opcodes, op)
+ }
+ }
+
+ return opcodes
+}
+
+// MatchFn searcher for match in the given input and calls matcheFn if it finds
+// an appropriate match. matcherFn yields the starting position in the input.
+// MatchFn will continue to search for a match until it reacher the end of the
+// buffer or if matcherFn return false.
+func MatchFn(input, match []OpCode, matcherFn func(int) bool) {
+ // short circuit if either input or match is empty or if the match is
+ // greater than the input
+ if len(input) == 0 || len(match) == 0 || len(match) > len(input) {
+ return
+ }
+
+main:
+ for i, op := range input[:len(input)+1-len(match)] {
+ // match first opcode and continue search
+ if op == match[0] {
+ for j := 1; j < len(match); j++ {
+ if input[i+j] != match[j] {
+ continue main
+ }
+ }
+ // check for abort instruction
+ if !matcherFn(i) {
+ return
+ }
+ }
+ }
+}
diff --git a/core/vm/jit_util_test.go b/core/vm/jit_util_test.go
new file mode 100644
index 000000000..686bee0ac
--- /dev/null
+++ b/core/vm/jit_util_test.go
@@ -0,0 +1,84 @@
+// Copyright 2014 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import "testing"
+
+type matchTest struct {
+ input []OpCode
+ match []OpCode
+ matches int
+}
+
+func TestMatchFn(t *testing.T) {
+ tests := []matchTest{
+ matchTest{
+ []OpCode{PUSH1, PUSH1, MSTORE, JUMP},
+ []OpCode{PUSH1, MSTORE},
+ 1,
+ },
+ matchTest{
+ []OpCode{PUSH1, PUSH1, MSTORE, JUMP},
+ []OpCode{PUSH1, MSTORE, PUSH1},
+ 0,
+ },
+ matchTest{
+ []OpCode{},
+ []OpCode{PUSH1},
+ 0,
+ },
+ }
+
+ for i, test := range tests {
+ var matchCount int
+ MatchFn(test.input, test.match, func(i int) bool {
+ matchCount++
+ return true
+ })
+ if matchCount != test.matches {
+ t.Errorf("match count failed on test[%d]: expected %d matches, got %d", i, test.matches, matchCount)
+ }
+ }
+}
+
+type parseTest struct {
+ base OpCode
+ size int
+ output OpCode
+}
+
+func TestParser(t *testing.T) {
+ tests := []parseTest{
+ parseTest{PUSH1, 32, PUSH},
+ parseTest{DUP1, 16, DUP},
+ parseTest{SWAP1, 16, SWAP},
+ parseTest{MSTORE, 1, MSTORE},
+ }
+
+ for _, test := range tests {
+ for i := 0; i < test.size; i++ {
+ code := append([]byte{byte(byte(test.base) + byte(i))}, make([]byte, i+1)...)
+ output := Parse(code)
+ if len(output) == 0 {
+ t.Fatal("empty output")
+ }
+ if output[0] != test.output {
+ t.Error("%v failed: expected %v but got %v", test.base+OpCode(i), output[0])
+ }
+ }
+ }
+}
diff --git a/core/vm/opcodes.go b/core/vm/opcodes.go
index 46905baac..dc4139092 100644
--- a/core/vm/opcodes.go
+++ b/core/vm/opcodes.go
@@ -187,6 +187,13 @@ const (
LOG4
)
+// unofficial opcodes used for parsing
+const (
+ PUSH OpCode = 0xb0 + iota
+ DUP
+ SWAP
+)
+
const (
// 0xf0 range - closures
CREATE OpCode = 0xf0 + iota
@@ -194,7 +201,6 @@ const (
CALLCODE
RETURN
- // 0x70 range - other
SUICIDE = 0xff
)
@@ -347,9 +353,11 @@ var opCodeToString = map[OpCode]string{
CALL: "CALL",
RETURN: "RETURN",
CALLCODE: "CALLCODE",
+ SUICIDE: "SUICIDE",
- // 0x70 range - other
- SUICIDE: "SUICIDE",
+ PUSH: "PUSH",
+ DUP: "DUP",
+ SWAP: "SWAP",
}
func (o OpCode) String() string {