asm support for all opcodes

This commit is contained in:
Ian Gulliver
2021-11-18 19:48:46 -10:00
parent 8b502f940c
commit 941939da83
2 changed files with 80 additions and 15 deletions

View File

@@ -7,20 +7,6 @@ import "strings"
import "github.com/firestuff/subcoding/vm" import "github.com/firestuff/subcoding/vm"
import "github.com/pkg/errors" import "github.com/pkg/errors"
var opCodeByName = map[string]vm.OpCodeType{
"add": vm.OpAdd,
"call": vm.OpCall,
"ltu": vm.OpLTU,
"jmpt": vm.OpJmpT,
}
var operandsByOpCode = map[vm.OpCodeType]int{
vm.OpAdd: 2,
vm.OpCall: 1,
vm.OpLTU: 2,
vm.OpJmpT: 1,
}
func Assemble(src []byte) ([][]*vm.Instruction, error) { func Assemble(src []byte) ([][]*vm.Instruction, error) {
prog, err := parse(src) prog, err := parse(src)
if err != nil { if err != nil {
@@ -67,7 +53,7 @@ func assembleInstruction(in instruction) (*vm.Instruction, error) {
instr := &vm.Instruction{} instr := &vm.Instruction{}
opCode, found := opCodeByName[in[0]] opCode, found := opCodeByName[strings.ToLower(in[0])]
if !found { if !found {
return nil, fmt.Errorf("Invalid op name: %s\n", in[0]) return nil, fmt.Errorf("Invalid op name: %s\n", in[0])
} }
@@ -101,6 +87,7 @@ func assembleInstruction(in instruction) (*vm.Instruction, error) {
} }
func assembleOperand(op string) (vm.Operand, error) { func assembleOperand(op string) (vm.Operand, error) {
op = strings.ToLower(op)
ret := vm.Operand{} ret := vm.Operand{}
numStr := "" numStr := ""

78
asm/opcodes.go Normal file
View File

@@ -0,0 +1,78 @@
package asm
import "github.com/firestuff/subcoding/vm"
var opCodeByName = map[string]vm.OpCodeType{
"noop": vm.OpNoOp,
"nop": vm.OpNop,
"call": vm.OpCall,
"cal": vm.OpCal,
"return": vm.OpReturn,
"ret": vm.OpRet,
"move": vm.OpMove,
"mov": vm.OpMov,
"add": vm.OpAdd,
"subtract": vm.OpSubtract,
"sub": vm.OpSub,
"multiply": vm.OpMultiply,
"mul": vm.OpMul,
"divideunsigned": vm.OpDivideUnsigned,
"divu": vm.OpDivU,
"dividesigned": vm.OpDivideSigned,
"divs": vm.OpDivS,
"isequal": vm.OpIsEqual,
"eq": vm.OpEq,
"islessthanunsigned": vm.OpIsLessThanUnsigned,
"ltu": vm.OpLTU,
"islessthansigned": vm.OpIsLessThanSigned,
"lts": vm.OpLTS,
"isgreaterthanunsigned": vm.OpIsGreaterThanUnsigned,
"gtu": vm.OpGTU,
"isgreaterthansigned": vm.OpIsGreaterThanSigned,
"gts": vm.OpGTS,
"islessthanorequalunsigned": vm.OpIsLessThanOrEqualUnsigned,
"lteu": vm.OpLTEU,
"islessthanorequalsigned": vm.OpIsLessThanOrEqualSigned,
"ltes": vm.OpLTES,
"isgreaterthanorequalunsigned": vm.OpIsGreaterThanOrEqualUnsigned,
"gteu": vm.OpGTEU,
"isgreaterthanorequalsigned": vm.OpIsGreaterThanOrEqualSigned,
"gtes": vm.OpGTES,
"jump": vm.OpJump,
"jmp": vm.OpJmp,
"jumpiftrue": vm.OpJumpIfTrue,
"jmpt": vm.OpJmpT,
"jumpiffalse": vm.OpJumpIfFalse,
"jmpf": vm.OpJmpF,
}
var operandsByOpCode = map[vm.OpCodeType]int{
vm.OpNop: 0,
vm.OpCal: 1,
vm.OpRet: 0,
vm.OpMov: 2,
vm.OpAdd: 2,
vm.OpSub: 2,
vm.OpMul: 2,
vm.OpDivU: 2,
vm.OpDivS: 2,
vm.OpEq: 2,
vm.OpLTU: 2,
vm.OpLTS: 2,
vm.OpGTU: 2,
vm.OpGTS: 2,
vm.OpLTEU: 2,
vm.OpLTES: 2,
vm.OpGTEU: 2,
vm.OpGTES: 2,
vm.OpJmp: 1,
vm.OpJmpT: 1,
vm.OpJmpF: 1,
}