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"
|
2018-02-19 09:24:28 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm"
|
|
|
|
)
|
|
|
|
|
2019-11-22 10:06:32 +00:00
|
|
|
// emit a VM Instruction with data to the given buffer.
|
|
|
|
func emit(w *io.BufBinWriter, instr vm.Instruction, 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.
|
|
|
|
func emitOpcode(w *io.BufBinWriter, instr vm.Instruction) {
|
|
|
|
w.WriteLE(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.
|
|
|
|
func emitBool(w *io.BufBinWriter, ok bool) {
|
2018-02-19 09:24:28 +00:00
|
|
|
if ok {
|
2019-11-22 10:06:32 +00:00
|
|
|
emitOpcode(w, vm.PUSHT)
|
|
|
|
return
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
2019-11-22 10:06:32 +00:00
|
|
|
emitOpcode(w, vm.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.
|
|
|
|
func emitInt(w *io.BufBinWriter, i int64) {
|
|
|
|
switch {
|
|
|
|
case i == -1:
|
|
|
|
emitOpcode(w, vm.PUSHM1)
|
|
|
|
return
|
|
|
|
case i == 0:
|
|
|
|
emitOpcode(w, vm.PUSHF)
|
|
|
|
return
|
|
|
|
case i > 0 && i < 16:
|
2019-08-14 12:40:31 +00:00
|
|
|
val := vm.Instruction(int(vm.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)
|
2018-03-02 15:24:09 +00:00
|
|
|
val := util.ArrayReverse(bInt.Bytes())
|
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.
|
|
|
|
func emitString(w *io.BufBinWriter, s string) {
|
|
|
|
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.
|
|
|
|
func emitBytes(w *io.BufBinWriter, b []byte) {
|
|
|
|
n := len(b)
|
2018-02-19 09:24:28 +00:00
|
|
|
|
2019-02-09 15:53:58 +00:00
|
|
|
switch {
|
2019-08-14 12:40:31 +00:00
|
|
|
case n <= int(vm.PUSHBYTES75):
|
2019-11-22 10:06:32 +00:00
|
|
|
emit(w, vm.Instruction(n), b)
|
|
|
|
return
|
2019-02-09 15:53:58 +00:00
|
|
|
case n < 0x100:
|
2019-11-22 10:06:32 +00:00
|
|
|
emit(w, vm.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-11-22 10:06:32 +00:00
|
|
|
emit(w, vm.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-11-22 10:06:32 +00:00
|
|
|
emit(w, vm.PUSHDATA4, buf)
|
|
|
|
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.
|
|
|
|
func emitSyscall(w *io.BufBinWriter, 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-11-22 10:06:32 +00:00
|
|
|
emit(w, vm.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.
|
|
|
|
func emitCall(w *io.BufBinWriter, instr vm.Instruction, label int16) {
|
|
|
|
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.
|
|
|
|
func emitJmp(w *io.BufBinWriter, instr vm.Instruction, 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
|
|
|
}
|
|
|
|
|
2018-08-20 07:07:08 +00:00
|
|
|
func isInstrJmp(instr vm.Instruction) bool {
|
|
|
|
if instr == vm.JMP || instr == vm.JMPIFNOT || instr == vm.JMPIF || instr == vm.CALL {
|
2018-02-19 09:24:28 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|