2018-03-30 16:15:06 +00:00
|
|
|
package vm
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
)
|
2018-03-30 16:15:06 +00:00
|
|
|
|
|
|
|
// Context represent the current execution context of the VM.
|
|
|
|
type Context struct {
|
|
|
|
// Instruction pointer.
|
|
|
|
ip int
|
|
|
|
|
|
|
|
// The raw program script.
|
|
|
|
prog []byte
|
|
|
|
|
|
|
|
// Breakpoints
|
|
|
|
breakPoints []int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewContext return a new Context object.
|
|
|
|
func NewContext(b []byte) *Context {
|
|
|
|
return &Context{
|
|
|
|
ip: -1,
|
|
|
|
prog: b,
|
|
|
|
breakPoints: []int{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next return the next instruction to execute.
|
2019-08-14 12:40:31 +00:00
|
|
|
func (c *Context) Next() Instruction {
|
2018-03-30 16:15:06 +00:00
|
|
|
c.ip++
|
2018-04-04 19:41:19 +00:00
|
|
|
if c.ip >= len(c.prog) {
|
2019-08-14 12:40:31 +00:00
|
|
|
return RET
|
2018-04-04 19:41:19 +00:00
|
|
|
}
|
2019-08-14 12:40:31 +00:00
|
|
|
return Instruction(c.prog[c.ip])
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 19:41:19 +00:00
|
|
|
// IP returns the absolute instruction without taking 0 into account.
|
2018-03-30 16:15:06 +00:00
|
|
|
// If that program starts the ip = 0 but IP() will return 1, cause its
|
|
|
|
// the first instruction.
|
|
|
|
func (c *Context) IP() int {
|
|
|
|
return c.ip + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// LenInstr returns the number of instructions loaded.
|
|
|
|
func (c *Context) LenInstr() int {
|
|
|
|
return len(c.prog)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CurrInstr returns the current instruction and opcode.
|
2019-08-14 12:40:31 +00:00
|
|
|
func (c *Context) CurrInstr() (int, Instruction) {
|
2018-03-30 16:15:06 +00:00
|
|
|
if c.ip < 0 {
|
2019-09-10 16:17:33 +00:00
|
|
|
return c.ip, NOP
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
2019-08-14 12:40:31 +00:00
|
|
|
return c.ip, Instruction(c.prog[c.ip])
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy returns an new exact copy of c.
|
|
|
|
func (c *Context) Copy() *Context {
|
|
|
|
return &Context{
|
|
|
|
ip: c.ip,
|
|
|
|
prog: c.prog,
|
|
|
|
breakPoints: c.breakPoints,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Program returns the loaded program.
|
|
|
|
func (c *Context) Program() []byte {
|
|
|
|
return c.prog
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value implements StackItem interface.
|
|
|
|
func (c *Context) Value() interface{} {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) atBreakPoint() bool {
|
|
|
|
for _, n := range c.breakPoints {
|
|
|
|
if n == c.ip {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) String() string {
|
|
|
|
return "execution context"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) readUint32() uint32 {
|
|
|
|
start, end := c.IP(), c.IP()+4
|
|
|
|
if end > len(c.prog) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
val := binary.LittleEndian.Uint32(c.prog[start:end])
|
|
|
|
c.ip += 4
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) readUint16() uint16 {
|
|
|
|
start, end := c.IP(), c.IP()+2
|
|
|
|
if end > len(c.prog) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
val := binary.LittleEndian.Uint16(c.prog[start:end])
|
|
|
|
c.ip += 2
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) readByte() byte {
|
|
|
|
return c.readBytes(1)[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) readBytes(n int) []byte {
|
|
|
|
start, end := c.IP(), c.IP()+n
|
|
|
|
if end > len(c.prog) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make([]byte, n)
|
|
|
|
copy(out, c.prog[start:end])
|
|
|
|
c.ip += n
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) readVarBytes() []byte {
|
|
|
|
n := c.readByte()
|
|
|
|
return c.readBytes(int(n))
|
|
|
|
}
|