Disassembler skeleton

This commit is contained in:
Ian Gulliver
2021-11-19 20:38:56 -10:00
parent 5ccf6832c8
commit 579ddab2de
7 changed files with 111 additions and 6 deletions

View File

@@ -21,7 +21,11 @@ func Assemble(src []byte) (*vm.Program, error) {
return nil, errors.Wrapf(err, "At function index %d\n", f)
}
ret.Functions = append(ret.Functions, instrs)
newFunc := &vm.Function{
Instructions: instrs,
}
ret.Functions = append(ret.Functions, newFunc)
}
return ret, nil

34
asm/disassemble.go Normal file
View File

@@ -0,0 +1,34 @@
package asm
import "fmt"
import "github.com/firestuff/subcoding/vm"
import "github.com/pkg/errors"
func Disassemble(prog *vm.Program) ([]byte, error) {
for f, fnc := range prog.Functions {
_, err := disassembleFunction(fnc)
if err != nil {
return nil, errors.Wrapf(err, "At function index %d", f)
}
}
return nil, nil
}
func disassembleFunction(fnc *vm.Function) ([]byte, error) {
for i, instr := range fnc.Instructions {
_, err := disassembleInstruction(instr)
if err != nil {
return nil, errors.Wrapf(err, "At instruction index %d", i)
}
}
return nil, nil
}
func disassembleInstruction(in *vm.Instruction) ([]byte, error) {
fmt.Printf("%s\n", nameByOpCode[in.OpCode])
return nil, nil
}

View File

@@ -70,6 +70,48 @@ var opCodeByName = map[string]vm.OpCodeType{
"retf": vm.OpRetF,
}
var nameByOpCode = map[vm.OpCodeType]string{
vm.OpNop: "nop",
vm.OpMov: "mov",
vm.OpAdd: "add",
vm.OpSub: "sub",
vm.OpMul: "mul",
vm.OpDivU: "divu",
vm.OpDivS: "divs",
vm.OpNot: "not",
vm.OpAnd: "and",
vm.OpOr: "or",
vm.OpXor: "xor",
vm.OpShR: "shr",
vm.OpShL: "shl",
vm.OpEq: "eq",
vm.OpLTU: "ltu",
vm.OpLTS: "lts",
vm.OpGTU: "gtu",
vm.OpGTS: "gts",
vm.OpLTEU: "lteu",
vm.OpLTES: "ltes",
vm.OpGTEU: "gteu",
vm.OpGTES: "gtes",
vm.OpJmp: "jmp",
vm.OpJmpT: "jmpt",
vm.OpJmpF: "jmpf",
vm.OpCal: "cal",
vm.OpCalT: "calt",
vm.OpCalF: "calf",
vm.OpRet: "ret",
vm.OpRetT: "rett",
vm.OpRetF: "retf",
}
type operandType int
const (