mirror of
https://gitlab.com/pulsechaincom/go-pulse.git
synced 2024-12-25 04:47:17 +00:00
core/vm: added parsing utilities
This commit is contained in:
parent
b196278044
commit
8c85532412
@ -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)
|
||||
|
||||
|
68
core/vm/jit_util.go
Normal file
68
core/vm/jit_util.go
Normal file
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
84
core/vm/jit_util_test.go
Normal file
84
core/vm/jit_util_test.go
Normal file
@ -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])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user