2020-02-03 14:24:57 +00:00
|
|
|
package emit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2020-02-03 14:24:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Instruction emits a VM Instruction with data to the given buffer.
|
2020-02-03 14:46:51 +00:00
|
|
|
func Instruction(w *io.BinWriter, op opcode.Opcode, b []byte) {
|
|
|
|
w.WriteB(byte(op))
|
|
|
|
w.WriteBytes(b)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Opcode emits a single VM Instruction without arguments to the given buffer.
|
2020-02-03 14:46:51 +00:00
|
|
|
func Opcode(w *io.BinWriter, op opcode.Opcode) {
|
|
|
|
w.WriteB(byte(op))
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bool emits a bool type the given buffer.
|
2020-02-03 14:46:51 +00:00
|
|
|
func Bool(w *io.BinWriter, ok bool) {
|
2020-02-03 14:24:57 +00:00
|
|
|
if ok {
|
2020-02-03 14:46:51 +00:00
|
|
|
Opcode(w, opcode.PUSHT)
|
|
|
|
return
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
Opcode(w, opcode.PUSHF)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Int emits a int type to the given buffer.
|
2020-02-03 14:46:51 +00:00
|
|
|
func Int(w *io.BinWriter, i int64) {
|
|
|
|
switch {
|
|
|
|
case i == -1:
|
|
|
|
Opcode(w, opcode.PUSHM1)
|
|
|
|
case i == 0:
|
|
|
|
Opcode(w, opcode.PUSHF)
|
|
|
|
case i > 0 && i < 16:
|
2020-02-03 14:24:57 +00:00
|
|
|
val := opcode.Opcode(int(opcode.PUSH1) - 1 + int(i))
|
2020-02-03 14:46:51 +00:00
|
|
|
Opcode(w, val)
|
|
|
|
default:
|
|
|
|
bInt := big.NewInt(i)
|
|
|
|
val := IntToBytes(bInt)
|
|
|
|
Bytes(w, val)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String emits a string to the given buffer.
|
2020-02-03 14:46:51 +00:00
|
|
|
func String(w *io.BinWriter, s string) {
|
|
|
|
Bytes(w, []byte(s))
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes emits a byte array to the given buffer.
|
2020-02-03 14:46:51 +00:00
|
|
|
func Bytes(w *io.BinWriter, b []byte) {
|
|
|
|
var n = len(b)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case n <= int(opcode.PUSHBYTES75):
|
|
|
|
Instruction(w, opcode.Opcode(n), b)
|
|
|
|
return
|
|
|
|
case n < 0x100:
|
|
|
|
Instruction(w, opcode.PUSHDATA1, []byte{byte(n)})
|
|
|
|
case n < 0x10000:
|
2020-02-03 14:24:57 +00:00
|
|
|
buf := make([]byte, 2)
|
|
|
|
binary.LittleEndian.PutUint16(buf, uint16(n))
|
2020-02-03 14:46:51 +00:00
|
|
|
Instruction(w, opcode.PUSHDATA2, buf)
|
|
|
|
default:
|
2020-02-03 14:24:57 +00:00
|
|
|
buf := make([]byte, 4)
|
|
|
|
binary.LittleEndian.PutUint32(buf, uint32(n))
|
2020-02-03 14:46:51 +00:00
|
|
|
Instruction(w, opcode.PUSHDATA4, buf)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
w.WriteBytes(b)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Syscall emits the syscall API to the given buffer.
|
|
|
|
// Syscall API string cannot be 0.
|
2020-02-03 14:46:51 +00:00
|
|
|
func Syscall(w *io.BinWriter, api string) {
|
|
|
|
if w.Err != nil {
|
|
|
|
return
|
|
|
|
} else if len(api) == 0 {
|
|
|
|
w.Err = errors.New("syscall api cannot be of length 0")
|
|
|
|
return
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
buf := make([]byte, len(api)+1)
|
|
|
|
buf[0] = byte(len(api))
|
|
|
|
copy(buf[1:], api)
|
2020-02-03 14:46:51 +00:00
|
|
|
Instruction(w, opcode.SYSCALL, buf)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call emits a call Instruction with label to the given buffer.
|
2020-02-21 08:50:03 +00:00
|
|
|
func Call(w *io.BinWriter, op opcode.Opcode, label uint16) {
|
2020-02-03 14:46:51 +00:00
|
|
|
Jmp(w, op, label)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Jmp emits a jump Instruction along with label to the given buffer.
|
2020-02-21 08:50:03 +00:00
|
|
|
func Jmp(w *io.BinWriter, op opcode.Opcode, label uint16) {
|
2020-02-03 14:46:51 +00:00
|
|
|
if w.Err != nil {
|
|
|
|
return
|
|
|
|
} else if !isInstructionJmp(op) {
|
|
|
|
w.Err = fmt.Errorf("opcode %s is not a jump or call type", op.String())
|
|
|
|
return
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
buf := make([]byte, 2)
|
2020-02-21 08:50:03 +00:00
|
|
|
binary.LittleEndian.PutUint16(buf, label)
|
2020-02-03 14:46:51 +00:00
|
|
|
Instruction(w, op, buf)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AppCall emits an appcall, if tailCall is true, tailCall opcode will be
|
|
|
|
// emitted instead.
|
2020-02-03 14:46:51 +00:00
|
|
|
func AppCall(w *io.BinWriter, scriptHash util.Uint160, tailCall bool) {
|
2020-02-03 14:24:57 +00:00
|
|
|
op := opcode.APPCALL
|
|
|
|
if tailCall {
|
|
|
|
op = opcode.TAILCALL
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
Instruction(w, op, scriptHash.BytesBE())
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AppCallWithOperationAndData emits an appcall with the given operation and data.
|
2020-02-03 14:46:51 +00:00
|
|
|
func AppCallWithOperationAndData(w *io.BinWriter, scriptHash util.Uint160, operation string, data []byte) {
|
|
|
|
Bytes(w, data)
|
|
|
|
String(w, operation)
|
|
|
|
AppCall(w, scriptHash, false)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AppCallWithOperation emits an appcall with the given operation.
|
2020-02-03 14:46:51 +00:00
|
|
|
func AppCallWithOperation(w *io.BinWriter, scriptHash util.Uint160, operation string) {
|
|
|
|
Bool(w, false)
|
|
|
|
String(w, operation)
|
|
|
|
AppCall(w, scriptHash, false)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func isInstructionJmp(op opcode.Opcode) bool {
|
|
|
|
if op == opcode.JMP || op == opcode.JMPIFNOT || op == opcode.JMPIF || op == opcode.CALL {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|