2020-02-03 14:24:57 +00:00
|
|
|
package emit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2020-04-21 13:45:48 +00:00
|
|
|
"math/bits"
|
2020-02-03 14:24:57 +00:00
|
|
|
|
2020-08-13 07:41:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
|
2020-06-04 18:11:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
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-06-03 18:16:19 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
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-06-03 18:16:19 +00:00
|
|
|
Instruction(w, opcode.CONVERT, []byte{byte(stackitem.BooleanT)})
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 13:45:48 +00:00
|
|
|
func padRight(s int, buf []byte) []byte {
|
|
|
|
l := len(buf)
|
|
|
|
buf = buf[:s]
|
|
|
|
if buf[l-1]&0x80 != 0 {
|
|
|
|
for i := l; i < s; i++ {
|
|
|
|
buf[i] = 0xFF
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
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)
|
2020-04-21 13:45:48 +00:00
|
|
|
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:
|
2020-06-04 18:11:27 +00:00
|
|
|
buf := bigint.ToPreallocatedBytes(big.NewInt(i), make([]byte, 0, 32))
|
2020-04-21 13:45:48 +00:00
|
|
|
// l != 0 becase of switch
|
|
|
|
padSize := byte(8 - bits.LeadingZeros8(byte(len(buf)-1)))
|
|
|
|
Opcode(w, opcode.PUSHINT8+opcode.Opcode(padSize))
|
|
|
|
w.WriteBytes(padRight(1<<padSize, buf))
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 08:00:06 +00:00
|
|
|
// Array emits array of elements to the given buffer.
|
|
|
|
func Array(w *io.BinWriter, es ...interface{}) {
|
|
|
|
for i := len(es) - 1; i >= 0; i-- {
|
|
|
|
switch e := es[i].(type) {
|
|
|
|
case int64:
|
|
|
|
Int(w, e)
|
|
|
|
case string:
|
|
|
|
String(w, e)
|
|
|
|
case util.Uint160:
|
|
|
|
Bytes(w, e.BytesBE())
|
|
|
|
case []byte:
|
|
|
|
Bytes(w, e)
|
|
|
|
case bool:
|
|
|
|
Bool(w, e)
|
|
|
|
default:
|
2020-08-04 09:03:31 +00:00
|
|
|
if es[i] != nil {
|
|
|
|
w.Err = errors.New("unsupported type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
Opcode(w, opcode.PUSHNULL)
|
2020-03-27 08:00:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Int(w, int64(len(es)))
|
|
|
|
Opcode(w, opcode.PACK)
|
|
|
|
}
|
|
|
|
|
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 < 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
|
|
|
}
|
2020-04-15 14:40:05 +00:00
|
|
|
buf := make([]byte, 4)
|
2020-08-13 07:41:33 +00:00
|
|
|
binary.LittleEndian.PutUint32(buf, interopnames.ToID([]byte(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
|
|
|
}
|
2020-04-23 08:56:36 +00:00
|
|
|
buf := make([]byte, 4)
|
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
|
|
|
}
|
|
|
|
|
2020-05-07 11:58:59 +00:00
|
|
|
// AppCall emits call to provided contract.
|
|
|
|
func AppCall(w *io.BinWriter, scriptHash util.Uint160) {
|
2020-05-07 11:38:19 +00:00
|
|
|
Bytes(w, scriptHash.BytesBE())
|
2020-08-14 10:50:52 +00:00
|
|
|
Syscall(w, interopnames.SystemContractCall)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 08:00:06 +00:00
|
|
|
// AppCallWithOperationAndArgs emits an APPCALL with the given operation and arguments.
|
|
|
|
func AppCallWithOperationAndArgs(w *io.BinWriter, scriptHash util.Uint160, operation string, args ...interface{}) {
|
|
|
|
Array(w, args...)
|
|
|
|
String(w, operation)
|
2020-05-07 11:58:59 +00:00
|
|
|
AppCall(w, scriptHash)
|
2020-03-27 08:00:06 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 14:24:57 +00:00
|
|
|
func isInstructionJmp(op opcode.Opcode) bool {
|
2020-04-23 08:56:36 +00:00
|
|
|
return opcode.JMP <= op && op <= opcode.CALLL
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|