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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func emit(w *bytes.Buffer, op vm.Opcode, b []byte) error {
|
|
|
|
if err := w.WriteByte(byte(op)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := w.Write(b)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-09 15:53:58 +00:00
|
|
|
func emitOpcode(w io.ByteWriter, op vm.Opcode) 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 {
|
|
|
|
return emitOpcode(w, vm.Opusht)
|
|
|
|
}
|
|
|
|
return emitOpcode(w, vm.Opushf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func emitInt(w *bytes.Buffer, i int64) error {
|
|
|
|
if i == -1 {
|
|
|
|
return emitOpcode(w, vm.Opushm1)
|
|
|
|
}
|
|
|
|
if i == 0 {
|
|
|
|
return emitOpcode(w, vm.Opushf)
|
|
|
|
}
|
|
|
|
if i > 0 && i < 16 {
|
2019-01-25 11:20:35 +00:00
|
|
|
val := vm.Opcode(int(vm.Opush1) - 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 {
|
|
|
|
case n <= int(vm.Opushbytes75):
|
2018-02-19 09:24:28 +00:00
|
|
|
return emit(w, vm.Opcode(n), b)
|
2019-02-09 15:53:58 +00:00
|
|
|
case n < 0x100:
|
2018-02-19 09:24:28 +00:00
|
|
|
err = emit(w, vm.Opushdata1, []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))
|
|
|
|
err = emit(w, vm.Opushdata2, 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))
|
|
|
|
err = emit(w, vm.Opushdata4, buf)
|
|
|
|
}
|
|
|
|
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))
|
2018-02-24 09:06:48 +00:00
|
|
|
return emit(w, vm.Osyscall, buf)
|
|
|
|
}
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
func emitCall(w *bytes.Buffer, op vm.Opcode, label int16) error {
|
|
|
|
return emitJmp(w, op, label)
|
|
|
|
}
|
|
|
|
|
|
|
|
func emitJmp(w *bytes.Buffer, op vm.Opcode, label int16) error {
|
|
|
|
if !isOpcodeJmp(op) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isOpcodeJmp(op vm.Opcode) bool {
|
|
|
|
if op == vm.Ojmp || op == vm.Ojmpifnot || op == vm.Ojmpif || op == vm.Ocall {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|