2023-04-02 13:12:06 +02:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"git.defalsify.org/festive/resource"
|
|
|
|
"git.defalsify.org/festive/state"
|
|
|
|
)
|
|
|
|
|
|
|
|
//type Runner func(instruction []byte, st state.State, rs resource.Resource, ctx context.Context) (state.State, []byte, error)
|
|
|
|
|
|
|
|
// Run extracts individual op codes and arguments and executes them.
|
|
|
|
//
|
|
|
|
// Each step may update the state.
|
|
|
|
//
|
|
|
|
// On error, the remaining instructions will be returned. State will not be rolled back.
|
2023-04-02 14:59:40 +02:00
|
|
|
func Run(b []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
|
|
|
running := true
|
|
|
|
for running {
|
2023-04-03 00:53:21 +02:00
|
|
|
log.Printf("execute code %x", b)
|
2023-04-02 14:59:40 +02:00
|
|
|
op, bb, err := opSplit(b)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
2023-04-02 14:59:40 +02:00
|
|
|
b = bb
|
2023-04-02 13:12:06 +02:00
|
|
|
switch op {
|
|
|
|
case CATCH:
|
2023-04-02 14:59:40 +02:00
|
|
|
b, err = RunCatch(b, st, rs, ctx)
|
2023-04-02 13:12:06 +02:00
|
|
|
case CROAK:
|
2023-04-02 14:59:40 +02:00
|
|
|
b, err = RunCroak(b, st, rs, ctx)
|
2023-04-02 13:12:06 +02:00
|
|
|
case LOAD:
|
2023-04-02 14:59:40 +02:00
|
|
|
b, err = RunLoad(b, st, rs, ctx)
|
2023-04-02 13:12:06 +02:00
|
|
|
case RELOAD:
|
2023-04-02 14:59:40 +02:00
|
|
|
b, err = RunReload(b, st, rs, ctx)
|
2023-04-02 13:12:06 +02:00
|
|
|
case MAP:
|
2023-04-02 14:59:40 +02:00
|
|
|
b, err = RunMap(b, st, rs, ctx)
|
2023-04-02 13:12:06 +02:00
|
|
|
case MOVE:
|
2023-04-02 14:59:40 +02:00
|
|
|
b, err = RunMove(b, st, rs, ctx)
|
2023-04-02 13:12:06 +02:00
|
|
|
case INCMP:
|
2023-04-02 14:59:40 +02:00
|
|
|
b, err = RunInCmp(b, st, rs, ctx)
|
2023-04-03 00:53:21 +02:00
|
|
|
case MSIZE:
|
|
|
|
b, err = RunMSize(b, st, rs, ctx)
|
|
|
|
case MOUT:
|
|
|
|
b, err = RunMOut(b, st, rs, ctx)
|
2023-04-03 10:11:44 +02:00
|
|
|
case MNEXT:
|
|
|
|
b, err = RunMNext(b, st, rs, ctx)
|
|
|
|
case MPREV:
|
|
|
|
b, err = RunMPrev(b, st, rs, ctx)
|
2023-04-02 13:12:06 +02:00
|
|
|
case HALT:
|
2023-04-02 14:59:40 +02:00
|
|
|
b, err = RunHalt(b, st, rs, ctx)
|
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
default:
|
|
|
|
err = fmt.Errorf("Unhandled state: %v", op)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
if len(b) == 0 {
|
|
|
|
return []byte{}, nil
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, nil
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunMap executes the MAP opcode
|
2023-04-02 14:59:40 +02:00
|
|
|
func RunMap(b []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
|
|
|
sym, b, err := ParseMap(b)
|
|
|
|
err = st.Map(sym)
|
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunMap executes the CATCH opcode
|
2023-04-02 14:59:40 +02:00
|
|
|
func RunCatch(b []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
|
|
|
sym, sig, mode, b, err := ParseCatch(b)
|
2023-04-02 13:12:06 +02:00
|
|
|
if err != nil {
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
2023-04-02 14:59:40 +02:00
|
|
|
r, err := matchFlag(st, sig, mode)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
if r {
|
|
|
|
log.Printf("catch at flag %v, moving to %v", sig, sym) //bitField, d)
|
|
|
|
st.Down(sym)
|
|
|
|
b = []byte{}
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, nil
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunMap executes the CROAK opcode
|
2023-04-02 14:59:40 +02:00
|
|
|
func RunCroak(b []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
|
|
|
sig, mode, b, err := ParseCroak(b)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
r, err := matchFlag(st, sig, mode)
|
2023-04-02 13:12:06 +02:00
|
|
|
if err != nil {
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
if r {
|
|
|
|
log.Printf("croak at flag %v, purging and moving to top", sig)
|
|
|
|
st.Reset()
|
|
|
|
b = []byte{}
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunLoad executes the LOAD opcode
|
2023-04-02 14:59:40 +02:00
|
|
|
func RunLoad(b []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
|
|
|
sym, sz, b, err := ParseLoad(b)
|
2023-04-02 13:12:06 +02:00
|
|
|
if err != nil {
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
2023-04-02 14:59:40 +02:00
|
|
|
r, err := refresh(sym, rs, ctx)
|
2023-04-02 13:12:06 +02:00
|
|
|
if err != nil {
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
2023-04-02 14:59:40 +02:00
|
|
|
err = st.Add(sym, r, uint16(sz))
|
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunLoad executes the RELOAD opcode
|
2023-04-02 14:59:40 +02:00
|
|
|
func RunReload(b []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
|
|
|
sym, b, err := ParseReload(b)
|
2023-04-02 13:12:06 +02:00
|
|
|
if err != nil {
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
2023-04-02 14:59:40 +02:00
|
|
|
|
|
|
|
r, err := refresh(sym, rs, ctx)
|
2023-04-02 13:12:06 +02:00
|
|
|
if err != nil {
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
2023-04-02 14:59:40 +02:00
|
|
|
st.Update(sym, r)
|
|
|
|
return b, nil
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunLoad executes the MOVE opcode
|
2023-04-02 14:59:40 +02:00
|
|
|
func RunMove(b []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
|
|
|
sym, b, err := ParseMove(b)
|
2023-04-02 13:12:06 +02:00
|
|
|
if err != nil {
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
2023-04-02 14:59:40 +02:00
|
|
|
st.Down(sym)
|
2023-04-03 00:53:21 +02:00
|
|
|
code, err := rs.GetCode(sym)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
log.Printf("loaded additional code: %x", code)
|
|
|
|
b = append(b, code...)
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, nil
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunIncmp executes the INCMP opcode
|
2023-04-02 14:59:40 +02:00
|
|
|
func RunInCmp(b []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
|
|
|
sym, target, b, err := ParseInCmp(b)
|
2023-04-02 13:12:06 +02:00
|
|
|
if err != nil {
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
v, err := st.GetFlag(state.FLAG_INMATCH)
|
|
|
|
if err != nil {
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
if v {
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, nil
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
input, err := st.GetInput()
|
|
|
|
if err != nil {
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
2023-04-02 14:59:40 +02:00
|
|
|
if sym == string(input) {
|
2023-04-02 13:12:06 +02:00
|
|
|
log.Printf("input match for '%s'", input)
|
|
|
|
_, err = st.SetFlag(state.FLAG_INMATCH)
|
2023-04-02 14:59:40 +02:00
|
|
|
st.Down(target)
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
2023-04-03 00:53:21 +02:00
|
|
|
code, err := rs.GetCode(target)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
log.Printf("loaded additional code: %x", code)
|
|
|
|
b = append(b, code...)
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunHalt executes the HALT opcode
|
2023-04-02 14:59:40 +02:00
|
|
|
func RunHalt(b []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
|
|
|
var err error
|
|
|
|
b, err = ParseHalt(b)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
2023-04-02 13:12:06 +02:00
|
|
|
log.Printf("found HALT, stopping")
|
2023-04-02 14:59:40 +02:00
|
|
|
_, err = st.ResetFlag(state.FLAG_INMATCH)
|
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
2023-04-04 08:42:15 +02:00
|
|
|
// RunMSize executes the HALT opcode
|
2023-04-03 00:53:21 +02:00
|
|
|
func RunMSize(b []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
2023-04-03 10:11:44 +02:00
|
|
|
log.Printf("WARNING MSIZE not yet implemented")
|
2023-04-03 10:52:58 +02:00
|
|
|
_, _, b, err := ParseMSize(b)
|
|
|
|
return b, err
|
2023-04-03 00:53:21 +02:00
|
|
|
}
|
|
|
|
|
2023-04-04 08:42:15 +02:00
|
|
|
// RunMNext executes the MNEXT opcode
|
2023-04-03 10:11:44 +02:00
|
|
|
func RunMNext(b []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
|
|
|
selector, display, b, err := ParseMNext(b)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
err = rs.SetMenuBrowse(selector, display, false)
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
|
2023-04-04 08:42:15 +02:00
|
|
|
// RunMPrev executes the MPREV opcode
|
2023-04-03 10:11:44 +02:00
|
|
|
func RunMPrev(b []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
|
|
|
selector, display, b, err := ParseMPrev(b)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
err = rs.SetMenuBrowse(selector, display, false)
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
|
2023-04-04 08:42:15 +02:00
|
|
|
// RunMOut executes the MOUT opcode
|
2023-04-03 00:53:21 +02:00
|
|
|
func RunMOut(b []byte, st *state.State, rs resource.Resource, ctx context.Context) ([]byte, error) {
|
|
|
|
choice, title, b, err := ParseMOut(b)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
err = rs.PutMenu(choice, title)
|
|
|
|
return b, err
|
|
|
|
}
|
2023-04-02 13:12:06 +02:00
|
|
|
|
|
|
|
// retrieve data for key
|
|
|
|
func refresh(key string, rs resource.Resource, ctx context.Context) (string, error) {
|
|
|
|
fn, err := rs.FuncFor(key)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if fn == nil {
|
|
|
|
return "", fmt.Errorf("no retrieve function for external symbol %v", key)
|
|
|
|
}
|
|
|
|
return fn(ctx)
|
|
|
|
}
|