2023-04-02 16:00:56 +02:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
2023-04-04 09:10:25 +02:00
|
|
|
"bytes"
|
2023-04-02 16:00:56 +02:00
|
|
|
"fmt"
|
2023-04-04 09:10:25 +02:00
|
|
|
"io"
|
|
|
|
"log"
|
2023-04-02 16:00:56 +02:00
|
|
|
)
|
|
|
|
|
2023-04-04 09:10:25 +02:00
|
|
|
// ToString verifies all instructions in bytecode and returns an assmebly code instruction for it.
|
2023-04-02 16:00:56 +02:00
|
|
|
func ToString(b []byte) (string, error) {
|
2023-04-04 09:10:25 +02:00
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
n, err := ParseAll(b, buf)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
log.Printf("Total %v bytes written to string buffer", n)
|
|
|
|
return buf.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseAll parses and verifies all instructions from bytecode.
|
|
|
|
//
|
|
|
|
// If writer is not nil, the parsed instruction as assembly code line string is written to it.
|
|
|
|
//
|
|
|
|
// Bytecode is consumed (and written) one instruction at a time.
|
|
|
|
//
|
|
|
|
// It fails on any parse error encountered before the bytecode EOF is reached.
|
|
|
|
func ParseAll(b []byte, w io.Writer) (int, error) {
|
2023-04-02 16:00:56 +02:00
|
|
|
var s string
|
2023-04-04 09:10:25 +02:00
|
|
|
var rs string
|
|
|
|
var rn int
|
2023-04-02 16:00:56 +02:00
|
|
|
running := true
|
|
|
|
for running {
|
|
|
|
op, bb, err := opSplit(b)
|
|
|
|
b = bb
|
|
|
|
if err != nil {
|
2023-04-04 09:10:25 +02:00
|
|
|
return rn, err
|
2023-04-02 16:00:56 +02:00
|
|
|
}
|
2023-04-04 09:10:25 +02:00
|
|
|
s = OpcodeString[op]
|
|
|
|
if s == "" {
|
|
|
|
return rn, fmt.Errorf("unknown opcode: %v", op)
|
2023-04-02 16:00:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch op {
|
|
|
|
case CATCH:
|
|
|
|
r, n, m, bb, err := ParseCatch(b)
|
|
|
|
b = bb
|
2023-04-04 09:10:25 +02:00
|
|
|
if err == nil {
|
|
|
|
if w != nil {
|
|
|
|
vv := 0
|
|
|
|
if m {
|
|
|
|
vv = 1
|
|
|
|
}
|
|
|
|
if w != nil {
|
2023-04-04 11:32:39 +02:00
|
|
|
//rs = fmt.Sprintf("%s %s %v %v # invertmatch=%v\n", s, r, n, vv, m)
|
|
|
|
rs = fmt.Sprintf("%s %s %v %v\n", s, r, n, vv)
|
2023-04-04 09:10:25 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-02 16:00:56 +02:00
|
|
|
}
|
|
|
|
case CROAK:
|
|
|
|
n, m, bb, err := ParseCroak(b)
|
|
|
|
b = bb
|
2023-04-04 09:10:25 +02:00
|
|
|
if err == nil {
|
|
|
|
if w != nil {
|
|
|
|
vv := 0
|
|
|
|
if m {
|
|
|
|
vv = 1
|
|
|
|
}
|
2023-04-04 11:32:39 +02:00
|
|
|
//rs = fmt.Sprintf("%s %v %v # invertmatch=%v\n", s, n, vv, m)
|
|
|
|
rs = fmt.Sprintf("%s %v %v\n", s, n, vv)
|
2023-04-04 09:10:25 +02:00
|
|
|
}
|
2023-04-02 16:00:56 +02:00
|
|
|
}
|
|
|
|
case LOAD:
|
|
|
|
r, n, bb, err := ParseLoad(b)
|
|
|
|
b = bb
|
2023-04-04 09:10:25 +02:00
|
|
|
if err == nil {
|
|
|
|
if w != nil {
|
|
|
|
rs = fmt.Sprintf("%s %s %v\n", s, r, n)
|
|
|
|
}
|
2023-04-02 16:00:56 +02:00
|
|
|
}
|
|
|
|
case RELOAD:
|
|
|
|
r, bb, err := ParseReload(b)
|
|
|
|
b = bb
|
2023-04-04 09:10:25 +02:00
|
|
|
if err == nil {
|
|
|
|
if w != nil {
|
|
|
|
rs = fmt.Sprintf("%s %s\n", s, r)
|
|
|
|
}
|
2023-04-02 16:00:56 +02:00
|
|
|
}
|
|
|
|
case MAP:
|
|
|
|
r, bb, err := ParseMap(b)
|
|
|
|
b = bb
|
2023-04-04 09:10:25 +02:00
|
|
|
if err == nil {
|
|
|
|
if w != nil {
|
|
|
|
rs = fmt.Sprintf("%s %s\n", s, r)
|
|
|
|
}
|
2023-04-02 16:00:56 +02:00
|
|
|
}
|
|
|
|
case MOVE:
|
|
|
|
r, bb, err := ParseMove(b)
|
|
|
|
b = bb
|
2023-04-04 09:10:25 +02:00
|
|
|
if err == nil {
|
|
|
|
if w != nil {
|
|
|
|
rs = fmt.Sprintf("%s %s\n", s, r)
|
|
|
|
}
|
2023-04-02 16:00:56 +02:00
|
|
|
}
|
|
|
|
case INCMP:
|
|
|
|
r, v, bb, err := ParseInCmp(b)
|
|
|
|
b = bb
|
2023-04-04 09:10:25 +02:00
|
|
|
if err == nil {
|
|
|
|
if w != nil {
|
|
|
|
rs = fmt.Sprintf("%s %s %s\n", s, r, v)
|
|
|
|
}
|
2023-04-02 16:00:56 +02:00
|
|
|
}
|
|
|
|
case HALT:
|
|
|
|
b, err = ParseHalt(b)
|
2023-04-04 09:10:25 +02:00
|
|
|
rs = "HALT\n"
|
2023-04-03 00:53:21 +02:00
|
|
|
case MSIZE:
|
2023-04-03 10:52:58 +02:00
|
|
|
r, v, bb, err := ParseMSize(b)
|
2023-04-03 00:53:21 +02:00
|
|
|
b = bb
|
2023-04-04 09:10:25 +02:00
|
|
|
if err == nil {
|
|
|
|
if w != nil {
|
|
|
|
rs = fmt.Sprintf("%s %v %v\n", s, r, v)
|
|
|
|
}
|
2023-04-03 00:53:21 +02:00
|
|
|
}
|
|
|
|
case MOUT:
|
|
|
|
r, v, bb, err := ParseMOut(b)
|
|
|
|
b = bb
|
2023-04-04 09:10:25 +02:00
|
|
|
if err == nil {
|
|
|
|
if w != nil {
|
|
|
|
rs = fmt.Sprintf("%s %s \"%s\"\n", s, r, v)
|
|
|
|
}
|
2023-04-03 00:53:21 +02:00
|
|
|
}
|
2023-04-03 10:11:44 +02:00
|
|
|
case MNEXT:
|
|
|
|
r, v, bb, err := ParseMNext(b)
|
|
|
|
b = bb
|
2023-04-04 09:10:25 +02:00
|
|
|
if err == nil {
|
|
|
|
if w != nil {
|
|
|
|
rs = fmt.Sprintf("%s %s \"%s\"\n", s, r, v)
|
|
|
|
}
|
2023-04-03 10:11:44 +02:00
|
|
|
}
|
|
|
|
case MPREV:
|
|
|
|
r, v, bb, err := ParseMPrev(b)
|
|
|
|
b = bb
|
2023-04-04 09:10:25 +02:00
|
|
|
if err == nil {
|
|
|
|
if w != nil {
|
|
|
|
rs = fmt.Sprintf("%s %s \"%s\"\n", s, r, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return rn, err
|
|
|
|
}
|
|
|
|
if w != nil {
|
|
|
|
n, err := io.WriteString(w, rs)
|
2023-04-03 10:11:44 +02:00
|
|
|
if err != nil {
|
2023-04-04 09:10:25 +02:00
|
|
|
return rn, err
|
2023-04-03 10:11:44 +02:00
|
|
|
}
|
2023-04-04 09:10:25 +02:00
|
|
|
rn += n
|
2023-04-04 11:32:39 +02:00
|
|
|
log.Printf("wrote %v bytes for instruction %v", n, s)
|
2023-04-02 16:00:56 +02:00
|
|
|
}
|
2023-04-04 09:10:25 +02:00
|
|
|
|
|
|
|
//rs += "\n"
|
2023-04-02 16:00:56 +02:00
|
|
|
if len(b) == 0 {
|
|
|
|
running = false
|
|
|
|
}
|
|
|
|
}
|
2023-04-04 09:10:25 +02:00
|
|
|
return rn, nil
|
2023-04-02 16:00:56 +02:00
|
|
|
}
|