Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
8c855324
Commit
8c855324
authored
Oct 12, 2015
by
Jeffrey Wilcke
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core/vm: added parsing utilities
parent
b1962780
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
180 additions
and
3 deletions
+180
-3
jit_optimiser.go
core/vm/jit_optimiser.go
+17
-0
jit_util.go
core/vm/jit_util.go
+68
-0
jit_util_test.go
core/vm/jit_util_test.go
+84
-0
opcodes.go
core/vm/opcodes.go
+11
-3
No files found.
core/vm/jit_optimiser.go
View file @
8c855324
...
@@ -26,6 +26,23 @@ func optimiseProgram(program *Program) {
...
@@ -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
++
{
for
i
:=
0
;
i
<
len
(
program
.
instructions
);
i
++
{
instr
:=
program
.
instructions
[
i
]
.
(
instruction
)
instr
:=
program
.
instructions
[
i
]
.
(
instruction
)
...
...
core/vm/jit_util.go
0 → 100644
View file @
8c855324
// 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
}
}
}
}
core/vm/jit_util_test.go
0 → 100644
View file @
8c855324
// 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
])
}
}
}
}
core/vm/opcodes.go
View file @
8c855324
...
@@ -187,6 +187,13 @@ const (
...
@@ -187,6 +187,13 @@ const (
LOG4
LOG4
)
)
// unofficial opcodes used for parsing
const
(
PUSH
OpCode
=
0xb0
+
iota
DUP
SWAP
)
const
(
const
(
// 0xf0 range - closures
// 0xf0 range - closures
CREATE
OpCode
=
0xf0
+
iota
CREATE
OpCode
=
0xf0
+
iota
...
@@ -194,7 +201,6 @@ const (
...
@@ -194,7 +201,6 @@ const (
CALLCODE
CALLCODE
RETURN
RETURN
// 0x70 range - other
SUICIDE
=
0xff
SUICIDE
=
0xff
)
)
...
@@ -347,9 +353,11 @@ var opCodeToString = map[OpCode]string{
...
@@ -347,9 +353,11 @@ var opCodeToString = map[OpCode]string{
CALL
:
"CALL"
,
CALL
:
"CALL"
,
RETURN
:
"RETURN"
,
RETURN
:
"RETURN"
,
CALLCODE
:
"CALLCODE"
,
CALLCODE
:
"CALLCODE"
,
SUICIDE
:
"SUICIDE"
,
// 0x70 range - other
PUSH
:
"PUSH"
,
SUICIDE
:
"SUICIDE"
,
DUP
:
"DUP"
,
SWAP
:
"SWAP"
,
}
}
func
(
o
OpCode
)
String
()
string
{
func
(
o
OpCode
)
String
()
string
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment