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
/
bytecode.go
122 lines (103 loc) · 2.82 KB
/
bytecode.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package acm
import (
"encoding/json"
"fmt"
"github.com/hyperledger/burrow/execution/evm/asm"
"github.com/hyperledger/burrow/execution/evm/asm/bc"
hex "github.com/tmthrgd/go-hex"
)
type Bytecode []byte
// Builds new bytecode using the Splice helper to map byte-like and byte-slice-like types to a flat bytecode slice
func NewBytecode(bytelikes ...interface{}) (Bytecode, error) {
return bc.Splice(bytelikes...)
}
func BytecodeFromHex(hexString string) (Bytecode, error) {
var bc Bytecode
err := bc.UnmarshalText([]byte(hexString))
if err != nil {
return nil, err
}
return bc, nil
}
func (bc Bytecode) Bytes() []byte {
return bc[:]
}
func (bc Bytecode) String() string {
return hex.EncodeUpperToString(bc[:])
}
func (bc Bytecode) MarshalJSON() ([]byte, error) {
text, err := bc.MarshalText()
if err != nil {
return nil, err
}
return json.Marshal(string(text))
}
func (bc *Bytecode) UnmarshalJSON(data []byte) error {
str := new(string)
err := json.Unmarshal(data, str)
if err != nil {
return err
}
err = bc.UnmarshalText([]byte(*str))
if err != nil {
return err
}
return nil
}
func (bc Bytecode) MarshalText() ([]byte, error) {
return ([]byte)(hex.EncodeUpperToString(bc)), nil
}
func (bc *Bytecode) UnmarshalText(text []byte) error {
*bc = make([]byte, hex.DecodedLen(len(text)))
_, err := hex.Decode(*bc, text)
return err
}
// Protobuf support
func (bc Bytecode) Marshal() ([]byte, error) {
return bc, nil
}
func (bc *Bytecode) Unmarshal(data []byte) error {
*bc = data
return nil
}
func (bc Bytecode) MarshalTo(data []byte) (int, error) {
return copy(data, bc), nil
}
func (bc Bytecode) Size() int {
return len(bc)
}
func (bc Bytecode) MustTokens() []string {
tokens, err := bc.Tokens()
if err != nil {
panic(err)
}
return tokens
}
// Tokenises the bytecode into opcodes and values
func (bc Bytecode) Tokens() ([]string, error) {
// Overestimate of capacity in the presence of pushes
tokens := make([]string, 0, len(bc))
for i := 0; i < len(bc); i++ {
op, ok := asm.GetOpCode(bc[i])
if !ok {
return tokens, fmt.Errorf("did not recognise byte %#x at position %v as an OpCode:\n %s",
bc[i], i, lexingPositionString(bc, i, tokens))
}
pushes := op.Pushes()
tokens = append(tokens, op.Name())
if pushes > 0 {
// This is a PUSH<N> OpCode so consume N bytes from the input, render them as hex, and skip to next OpCode
if i+pushes >= len(bc) {
return tokens, fmt.Errorf("token %v of input is %s but not enough input remains to push %v: %s",
i, op.Name(), pushes, lexingPositionString(bc, i, tokens))
}
pushedBytes := bc[i+1 : i+pushes+1]
tokens = append(tokens, fmt.Sprintf("0x%s", pushedBytes))
i += pushes
}
}
return tokens, nil
}
func lexingPositionString(bc Bytecode, position int, produced []string) string {
return fmt.Sprintf("%v_%v", produced, []byte(bc[position:]))
}