This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
code.go
66 lines (57 loc) · 1.44 KB
/
code.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
package evm
import (
"github.com/hyperledger/burrow/acm"
"github.com/hyperledger/burrow/execution/evm/asm"
"github.com/tmthrgd/go-bitset"
)
type Code struct {
Bytecode acm.Bytecode
OpcodeBitset bitset.Bitset
}
// Build a Code object that includes analysis of which symbols are opcodes versus push data
func NewCode(code []byte) *Code {
return &Code{
Bytecode: code,
OpcodeBitset: opcodeBitset(code),
}
}
func (code *Code) Length() uint64 {
if code == nil {
return 0
}
return uint64(len(code.Bytecode))
}
func (code *Code) GetBytecode() acm.Bytecode {
if code == nil {
return nil
}
return code.Bytecode
}
func (code *Code) IsOpcode(indexOfSymbolInCode uint64) bool {
if code == nil || indexOfSymbolInCode >= uint64(code.OpcodeBitset.Len()) {
return false
}
return code.OpcodeBitset.IsSet(uint(indexOfSymbolInCode))
}
func (code *Code) IsPushData(indexOfSymbolInCode uint64) bool {
return !code.IsOpcode(indexOfSymbolInCode)
}
func (code *Code) GetSymbol(n uint64) asm.OpCode {
if code.Length() <= n {
return asm.STOP
} else {
return asm.OpCode(code.Bytecode[n])
}
}
// If code[i] is an opcode (rather than PUSH data) then bitset.IsSet(i) will be true
func opcodeBitset(code []byte) bitset.Bitset {
bs := bitset.New(uint(len(code)))
for i := 0; i < len(code); i++ {
bs.Set(uint(i))
symbol := asm.OpCode(code[i])
if symbol >= asm.PUSH1 && symbol <= asm.PUSH32 {
i += int(symbol - asm.PUSH1 + 1)
}
}
return bs
}