2018-02-19 09:24:28 +00:00
|
|
|
package compiler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-02-09 15:53:58 +00:00
|
|
|
"io"
|
2018-02-19 09:24:28 +00:00
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm"
|
|
|
|
)
|
|
|
|
|
2019-08-14 12:40:31 +00:00
|
|
|
func emit(w *bytes.Buffer, op vm.Instruction, b []byte) error {
|
2018-02-19 09:24:28 +00:00
|
|
|
if err := w.WriteByte(byte(op)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := w.Write(b)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-14 12:40:31 +00:00
|
|
|
func emitOpcode(w io.ByteWriter, op vm.Instruction) error {
|
2018-02-19 09:24:28 +00:00
|
|
|
return w.WriteByte(byte(op))
|
|
|
|
}
|
|
|
|
|
2019-02-09 15:53:58 +00:00
|
|
|
func emitBool(w io.ByteWriter, ok bool) error {
|
2018-02-19 09:24:28 +00:00
|
|
|
if ok {
|
2019-08-14 12:40:31 +00:00
|
|
|
return emitOpcode(w, vm.PUSHT)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
2019-08-14 12:40:31 +00:00
|
|
|
return emitOpcode(w, vm.PUSHF)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func emitInt(w *bytes.Buffer, i int64) error {
|
|
|
|
if i == -1 {
|
2019-08-14 12:40:31 +00:00
|
|
|
return emitOpcode(w, vm.PUSHM1)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
if i == 0 {
|
2019-08-14 12:40:31 +00:00
|
|
|
return emitOpcode(w, vm.PUSHF)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
if i > 0 && i < 16 {
|
2019-08-14 12:40:31 +00:00
|
|
|
val := vm.Instruction(int(vm.PUSH1) - 1 + int(i))
|
2018-02-19 09:24:28 +00:00
|
|
|
return emitOpcode(w, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
bInt := big.NewInt(i)
|
2018-03-02 15:24:09 +00:00
|
|
|
val := util.ArrayReverse(bInt.Bytes())
|
2018-02-19 09:24:28 +00:00
|
|
|
return emitBytes(w, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func emitString(w *bytes.Buffer, s string) error {
|
|
|
|
return emitBytes(w, []byte(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
func emitBytes(w *bytes.Buffer, b []byte) error {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
n = len(b)
|
|
|
|
)
|
|
|
|
|
2019-02-09 15:53:58 +00:00
|
|
|
switch {
|
2019-08-14 12:40:31 +00:00
|
|
|
case n <= int(vm.PUSHBYTES75):
|
|
|
|
return emit(w, vm.Instruction(n), b)
|
2019-02-09 15:53:58 +00:00
|
|
|
case n < 0x100:
|
2019-08-14 12:40:31 +00:00
|
|
|
err = 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-08-14 12:40:31 +00:00
|
|
|
err = 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-08-14 12:40:31 +00:00
|
|
|
err = emit(w, vm.PUSHDATA4, buf)
|
2018-02-19 09:24:28 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = w.Write(b)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-24 09:06:48 +00:00
|
|
|
func emitSyscall(w *bytes.Buffer, api string) error {
|
|
|
|
if len(api) == 0 {
|
|
|
|
return errors.New("syscall api cannot be of length 0")
|
|
|
|
}
|
|
|
|
buf := make([]byte, len(api)+1)
|
|
|
|
buf[0] = byte(len(api))
|
2019-01-25 11:20:35 +00:00
|
|
|
copy(buf[1:], []byte(api))
|
2019-08-14 12:40:31 +00:00
|
|
|
return emit(w, vm.SYSCALL, buf)
|
2018-02-24 09:06:48 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 12:40:31 +00:00
|
|
|
func emitCall(w *bytes.Buffer, op vm.Instruction, label int16) error {
|
2018-02-19 09:24:28 +00:00
|
|
|
return emitJmp(w, op, label)
|
|
|
|
}
|
|
|
|
|
2019-08-14 12:40:31 +00:00
|
|
|
func emitJmp(w *bytes.Buffer, op vm.Instruction, label int16) error {
|
|
|
|
if !isInstructionJmp(op) {
|
2018-02-19 09:24:28 +00:00
|
|
|
return fmt.Errorf("opcode %s is not a jump or call type", op)
|
|
|
|
}
|
|
|
|
buf := make([]byte, 2)
|
|
|
|
binary.LittleEndian.PutUint16(buf, uint16(label))
|
|
|
|
return emit(w, op, buf)
|
|
|
|
}
|
|
|
|
|
2019-08-14 12:40:31 +00:00
|
|
|
func isInstructionJmp(op vm.Instruction) bool {
|
|
|
|
if op == vm.JMP || op == vm.JMPIFNOT || op == vm.JMPIF || op == vm.CALL {
|
2018-02-19 09:24:28 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|