2018-02-19 09:24:28 +00:00
|
|
|
package compiler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
|
2019-11-22 10:06:32 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2020-01-28 08:59:57 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm"
|
2019-12-03 14:05:06 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
|
2018-02-19 09:24:28 +00:00
|
|
|
)
|
|
|
|
|
2019-11-22 10:06:32 +00:00
|
|
|
// emit a VM Instruction with data to the given buffer.
|
2019-12-03 14:05:06 +00:00
|
|
|
func emit(w *io.BinWriter, instr opcode.Opcode, b []byte) {
|
2019-11-22 10:34:06 +00:00
|
|
|
emitOpcode(w, instr)
|
|
|
|
w.WriteBytes(b)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 10:06:32 +00:00
|
|
|
// emitOpcode emits a single VM Instruction the given buffer.
|
2019-12-03 14:05:06 +00:00
|
|
|
func emitOpcode(w *io.BinWriter, instr opcode.Opcode) {
|
2019-12-06 15:22:21 +00:00
|
|
|
w.WriteBytes([]byte{byte(instr)})
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 10:06:32 +00:00
|
|
|
// emitBool emits a bool type the given buffer.
|
2019-12-02 10:20:16 +00:00
|
|
|
func emitBool(w *io.BinWriter, ok bool) {
|
2018-02-19 09:24:28 +00:00
|
|
|
if ok {
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(w, opcode.PUSHT)
|
2019-11-22 10:06:32 +00:00
|
|
|
return
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(w, opcode.PUSHF)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 10:06:32 +00:00
|
|
|
// emitInt emits a int type to the given buffer.
|
2019-12-02 10:20:16 +00:00
|
|
|
func emitInt(w *io.BinWriter, i int64) {
|
2019-11-22 10:06:32 +00:00
|
|
|
switch {
|
|
|
|
case i == -1:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(w, opcode.PUSHM1)
|
2019-11-22 10:06:32 +00:00
|
|
|
return
|
|
|
|
case i == 0:
|
2019-12-03 14:05:06 +00:00
|
|
|
emitOpcode(w, opcode.PUSHF)
|
2019-11-22 10:06:32 +00:00
|
|
|
return
|
|
|
|
case i > 0 && i < 16:
|
2019-12-03 14:05:06 +00:00
|
|
|
val := opcode.Opcode(int(opcode.PUSH1) - 1 + int(i))
|
2019-11-22 10:06:32 +00:00
|
|
|
emitOpcode(w, val)
|
|
|
|
return
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bInt := big.NewInt(i)
|
2020-01-28 08:59:57 +00:00
|
|
|
val := vm.IntToBytes(bInt)
|
2019-11-22 10:06:32 +00:00
|
|
|
emitBytes(w, val)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 10:06:32 +00:00
|
|
|
// emitString emits a string to the given buffer.
|
2019-12-02 10:20:16 +00:00
|
|
|
func emitString(w *io.BinWriter, s string) {
|
2019-11-22 10:06:32 +00:00
|
|
|
emitBytes(w, []byte(s))
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 10:06:32 +00:00
|
|
|
// emitBytes emits a byte array to the given buffer.
|
2019-12-02 10:20:16 +00:00
|
|
|
func emitBytes(w *io.BinWriter, b []byte) {
|
2019-11-22 10:06:32 +00:00
|
|
|
n := len(b)
|
2018-02-19 09:24:28 +00:00
|
|
|
|
2019-02-09 15:53:58 +00:00
|
|
|
switch {
|
2019-12-03 14:05:06 +00:00
|
|
|
case n <= int(opcode.PUSHBYTES75):
|
|
|
|
emit(w, opcode.Opcode(n), b)
|
2019-11-22 10:06:32 +00:00
|
|
|
return
|
2019-02-09 15:53:58 +00:00
|
|
|
case n < 0x100:
|
2019-12-03 14:05:06 +00:00
|
|
|
emit(w, opcode.PUSHDATA1, []byte{byte(n)})
|
2019-02-09 15:53:58 +00:00
|
|
|
case n < 0x10000:
|
2018-02-19 09:24:28 +00:00
|
|
|
buf := make([]byte, 2)
|
|
|
|
binary.LittleEndian.PutUint16(buf, uint16(n))
|
2019-12-03 14:05:06 +00:00
|
|
|
emit(w, opcode.PUSHDATA2, buf)
|
2019-02-09 15:53:58 +00:00
|
|
|
default:
|
2018-02-19 09:24:28 +00:00
|
|
|
buf := make([]byte, 4)
|
|
|
|
binary.LittleEndian.PutUint32(buf, uint32(n))
|
2019-12-03 14:05:06 +00:00
|
|
|
emit(w, opcode.PUSHDATA4, buf)
|
2019-11-22 10:06:32 +00:00
|
|
|
if w.Err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
2019-11-22 10:06:32 +00:00
|
|
|
|
|
|
|
w.WriteBytes(b)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 10:06:32 +00:00
|
|
|
// emitSyscall emits the syscall API to the given buffer.
|
|
|
|
// Syscall API string cannot be 0.
|
2019-12-02 10:20:16 +00:00
|
|
|
func emitSyscall(w *io.BinWriter, api string) {
|
2018-02-24 09:06:48 +00:00
|
|
|
if len(api) == 0 {
|
2019-11-22 10:06:32 +00:00
|
|
|
w.Err = errors.New("syscall api cannot be of length 0")
|
|
|
|
return
|
2018-02-24 09:06:48 +00:00
|
|
|
}
|
|
|
|
buf := make([]byte, len(api)+1)
|
|
|
|
buf[0] = byte(len(api))
|
2019-10-18 15:39:31 +00:00
|
|
|
copy(buf[1:], api)
|
2019-12-03 14:05:06 +00:00
|
|
|
emit(w, opcode.SYSCALL, buf)
|
2018-02-24 09:06:48 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 10:06:32 +00:00
|
|
|
// emitCall emits a call Instruction with label to the given buffer.
|
2019-12-03 14:05:06 +00:00
|
|
|
func emitCall(w *io.BinWriter, instr opcode.Opcode, label int16) {
|
2019-11-22 10:06:32 +00:00
|
|
|
emitJmp(w, instr, label)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 10:06:32 +00:00
|
|
|
// emitJmp emits a jump Instruction along with label to the given buffer.
|
2019-12-03 14:05:06 +00:00
|
|
|
func emitJmp(w *io.BinWriter, instr opcode.Opcode, label int16) {
|
2018-08-20 07:07:08 +00:00
|
|
|
if !isInstrJmp(instr) {
|
2019-11-22 10:06:32 +00:00
|
|
|
w.Err = fmt.Errorf("opcode %s is not a jump or call type", instr)
|
|
|
|
return
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
buf := make([]byte, 2)
|
|
|
|
binary.LittleEndian.PutUint16(buf, uint16(label))
|
2019-11-22 10:06:32 +00:00
|
|
|
emit(w, instr, buf)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
func isInstrJmp(instr opcode.Opcode) bool {
|
|
|
|
if instr == opcode.JMP || instr == opcode.JMPIFNOT || instr == opcode.JMPIF || instr == opcode.CALL {
|
2018-02-19 09:24:28 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|