Make memory sizes parameters
This commit is contained in:
@@ -13,7 +13,10 @@ func Assemble(src []byte) (*vm.Program, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ret := &vm.Program{}
|
ret := &vm.Program{
|
||||||
|
GlobalMemorySize: parsed.GlobalMemorySize,
|
||||||
|
FunctionMemorySize: parsed.FunctionMemorySize,
|
||||||
|
}
|
||||||
|
|
||||||
for f, fnc := range parsed.Functions {
|
for f, fnc := range parsed.Functions {
|
||||||
instrs, err := assembleFunction(fnc)
|
instrs, err := assembleFunction(fnc)
|
||||||
|
|||||||
@@ -21,7 +21,14 @@ func Disassemble(prog *vm.Program) (string, error) {
|
|||||||
fncs = append(fncs, dis)
|
fncs = append(fncs, dis)
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf("functions:\n%s", strings.Join(fncs, "")), nil
|
return fmt.Sprintf(`global_memory_size: %d
|
||||||
|
function_memory_size: %d
|
||||||
|
functions:
|
||||||
|
%s`,
|
||||||
|
prog.GlobalMemorySize,
|
||||||
|
prog.FunctionMemorySize,
|
||||||
|
strings.Join(fncs, ""),
|
||||||
|
), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func disassembleFunction(fnc *vm.Function) (string, error) {
|
func disassembleFunction(fnc *vm.Function) (string, error) {
|
||||||
|
|||||||
14
asm/parse.go
14
asm/parse.go
@@ -1,9 +1,13 @@
|
|||||||
package asm
|
package asm
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
import "gopkg.in/yaml.v2"
|
import "gopkg.in/yaml.v2"
|
||||||
|
|
||||||
type program struct {
|
type program struct {
|
||||||
Functions []function `yaml:"functions"`
|
GlobalMemorySize uint64 `yaml:"global_memory_size"`
|
||||||
|
FunctionMemorySize uint64 `yaml:"function_memory_size"`
|
||||||
|
Functions []function `yaml:"functions"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type function []instruction
|
type function []instruction
|
||||||
@@ -18,5 +22,13 @@ func parse(src []byte) (*program, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if prog.GlobalMemorySize == 0 {
|
||||||
|
return nil, fmt.Errorf("global_memory_size must be set and non-zero")
|
||||||
|
}
|
||||||
|
|
||||||
|
if prog.FunctionMemorySize == 0 {
|
||||||
|
return nil, fmt.Errorf("function_memory_size must be set and non-zero")
|
||||||
|
}
|
||||||
|
|
||||||
return prog, nil
|
return prog, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,13 +2,13 @@ package gen
|
|||||||
|
|
||||||
import "github.com/firestuff/subcoding/vm"
|
import "github.com/firestuff/subcoding/vm"
|
||||||
|
|
||||||
func randInstruction() *vm.Instruction {
|
func randInstruction(prog *vm.Program) *vm.Instruction {
|
||||||
instr := &vm.Instruction{
|
instr := &vm.Instruction{
|
||||||
OpCode: randOpCode(),
|
OpCode: randOpCode(),
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, t := range vm.OperandsByOpCode[instr.OpCode] {
|
for i, t := range vm.OperandsByOpCode[instr.OpCode] {
|
||||||
instr.Operands[i] = randOperand(t)
|
instr.Operands[i] = randOperand(prog, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
return instr
|
return instr
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import "math/rand"
|
|||||||
|
|
||||||
import "github.com/firestuff/subcoding/vm"
|
import "github.com/firestuff/subcoding/vm"
|
||||||
|
|
||||||
func randOperand(t vm.OperandNumericType) *vm.Operand {
|
func randOperand(prog *vm.Program, t vm.OperandNumericType) *vm.Operand {
|
||||||
for {
|
for {
|
||||||
op := &vm.Operand{
|
op := &vm.Operand{
|
||||||
Type: randOperandType(),
|
Type: randOperandType(),
|
||||||
@@ -12,10 +12,10 @@ func randOperand(t vm.OperandNumericType) *vm.Operand {
|
|||||||
|
|
||||||
switch op.Type {
|
switch op.Type {
|
||||||
case vm.GlobalMemoryIndex:
|
case vm.GlobalMemoryIndex:
|
||||||
op.Value = RandBiasedUint64n(vm.GlobalMemorySize)
|
op.Value = RandBiasedUint64n(prog.GlobalMemorySize)
|
||||||
|
|
||||||
case vm.FunctionMemoryIndex:
|
case vm.FunctionMemoryIndex:
|
||||||
op.Value = RandBiasedUint64n(vm.FunctionMemorySize)
|
op.Value = RandBiasedUint64n(prog.FunctionMemorySize)
|
||||||
|
|
||||||
case vm.Literal:
|
case vm.Literal:
|
||||||
switch t {
|
switch t {
|
||||||
|
|||||||
@@ -2,14 +2,17 @@ package gen
|
|||||||
|
|
||||||
import "github.com/firestuff/subcoding/vm"
|
import "github.com/firestuff/subcoding/vm"
|
||||||
|
|
||||||
func RandProgram() *vm.Program {
|
func RandProgram(globalMemorySize, functionMemorySize uint64) *vm.Program {
|
||||||
return &vm.Program{
|
prog := &vm.Program{
|
||||||
Functions: []*vm.Function{
|
GlobalMemorySize: globalMemorySize,
|
||||||
&vm.Function{
|
FunctionMemorySize: functionMemorySize,
|
||||||
Instructions: []*vm.Instruction{
|
|
||||||
randInstruction(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
prog.Functions = append(prog.Functions, &vm.Function{
|
||||||
|
Instructions: []*vm.Instruction{
|
||||||
|
randInstruction(prog),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
return prog
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import "math/rand"
|
|||||||
// Generate a random uint64 with an even distribution of bits.Len64()
|
// Generate a random uint64 with an even distribution of bits.Len64()
|
||||||
func RandBiasedUint64() uint64 {
|
func RandBiasedUint64() uint64 {
|
||||||
// The shift-right by up to 64 (shifting it to 0) makes up for randomness
|
// The shift-right by up to 64 (shifting it to 0) makes up for randomness
|
||||||
// lost by setting the high bit.
|
// lost by setting the high bit.
|
||||||
return (rand.Uint64() | 0x8000000000000000) >> rand.Intn(65)
|
return (rand.Uint64() | 0x8000000000000000) >> rand.Intn(65)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,7 +29,7 @@ func RandBiasedInt64() uint64 {
|
|||||||
if shift < 64 {
|
if shift < 64 {
|
||||||
return uint64(int64((rand.Uint64() | 0x8000000000000000) >> (shift + 1)))
|
return uint64(int64((rand.Uint64() | 0x8000000000000000) >> (shift + 1)))
|
||||||
} else {
|
} else {
|
||||||
return uint64(int64((rand.Uint64() | 0x8000000000000000) >> (shift - 63)) * -1)
|
return uint64(int64((rand.Uint64()|0x8000000000000000)>>(shift-63)) * -1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import "github.com/firestuff/subcoding/asm"
|
|||||||
|
|
||||||
func TestTooManyOperands(t *testing.T) {
|
func TestTooManyOperands(t *testing.T) {
|
||||||
_, err := asm.AssembleString(`
|
_, err := asm.AssembleString(`
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [nop, 0]
|
- - [nop, 0]
|
||||||
`)
|
`)
|
||||||
@@ -16,6 +18,8 @@ functions:
|
|||||||
|
|
||||||
func TestTooFewOperands(t *testing.T) {
|
func TestTooFewOperands(t *testing.T) {
|
||||||
_, err := asm.AssembleString(`
|
_, err := asm.AssembleString(`
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [mov, g0]
|
- - [mov, g0]
|
||||||
`)
|
`)
|
||||||
@@ -26,6 +30,8 @@ functions:
|
|||||||
|
|
||||||
func TestIncorrectSigned(t *testing.T) {
|
func TestIncorrectSigned(t *testing.T) {
|
||||||
_, err := asm.AssembleString(`
|
_, err := asm.AssembleString(`
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [ltu, 0, -1]
|
- - [ltu, 0, -1]
|
||||||
`)
|
`)
|
||||||
@@ -36,6 +42,8 @@ functions:
|
|||||||
|
|
||||||
func TestHex(t *testing.T) {
|
func TestHex(t *testing.T) {
|
||||||
prog, err := asm.AssembleString(`
|
prog, err := asm.AssembleString(`
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [mov, g0, 0xfeedc0de]
|
- - [mov, g0, 0xfeedc0de]
|
||||||
`)
|
`)
|
||||||
@@ -51,6 +59,8 @@ functions:
|
|||||||
|
|
||||||
func TestBinary(t *testing.T) {
|
func TestBinary(t *testing.T) {
|
||||||
prog, err := asm.AssembleString(`
|
prog, err := asm.AssembleString(`
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [mov, g0, 0b100101]
|
- - [mov, g0, 0b100101]
|
||||||
`)
|
`)
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import "testing"
|
|||||||
|
|
||||||
func TestLoop(t *testing.T) {
|
func TestLoop(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [add, f0, 1]
|
- - [add, f0, 1]
|
||||||
- [call, +1]
|
- [call, +1]
|
||||||
|
|||||||
@@ -6,7 +6,9 @@ import "github.com/firestuff/subcoding/asm"
|
|||||||
import "github.com/firestuff/subcoding/vm"
|
import "github.com/firestuff/subcoding/vm"
|
||||||
|
|
||||||
func TestRoundTrip(t *testing.T) {
|
func TestRoundTrip(t *testing.T) {
|
||||||
src := `functions:
|
src := `global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
|
functions:
|
||||||
- - [nop]
|
- - [nop]
|
||||||
- [mov, g0, 1]
|
- [mov, g0, 1]
|
||||||
- [add, f0, 5]
|
- [add, f0, 5]
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import "github.com/firestuff/subcoding/asm"
|
|||||||
|
|
||||||
func TestRandProgram(t *testing.T) {
|
func TestRandProgram(t *testing.T) {
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < 100; i++ {
|
||||||
prog := gen.RandProgram()
|
prog := gen.RandProgram(4, 4)
|
||||||
|
|
||||||
src, err := asm.Disassemble(prog)
|
src, err := asm.Disassemble(prog)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import "testing"
|
|||||||
|
|
||||||
func TestNop(t *testing.T) {
|
func TestNop(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [nop]
|
- - [nop]
|
||||||
`)
|
`)
|
||||||
@@ -13,6 +15,8 @@ functions:
|
|||||||
|
|
||||||
func TestMov(t *testing.T) {
|
func TestMov(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [mov, g0, 1]
|
- - [mov, g0, 1]
|
||||||
- [mov, g1, g0]
|
- [mov, g1, g0]
|
||||||
@@ -27,6 +31,8 @@ functions:
|
|||||||
|
|
||||||
func TestAdd(t *testing.T) {
|
func TestAdd(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [add, g0, 5]
|
- - [add, g0, 5]
|
||||||
- [add, g0, 2]
|
- [add, g0, 2]
|
||||||
@@ -41,6 +47,8 @@ functions:
|
|||||||
|
|
||||||
func TestSub(t *testing.T) {
|
func TestSub(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [sub, g0, 2]
|
- - [sub, g0, 2]
|
||||||
- [sub, g0, -5]
|
- [sub, g0, -5]
|
||||||
@@ -53,6 +61,8 @@ functions:
|
|||||||
|
|
||||||
func TestMul(t *testing.T) {
|
func TestMul(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [mov, g0, 5]
|
- - [mov, g0, 5]
|
||||||
- [mul, g0, 3]
|
- [mul, g0, 3]
|
||||||
@@ -67,6 +77,8 @@ functions:
|
|||||||
|
|
||||||
func TestDivU(t *testing.T) {
|
func TestDivU(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [mov, g0, 15]
|
- - [mov, g0, 15]
|
||||||
- [divu, g0, 3]
|
- [divu, g0, 3]
|
||||||
@@ -80,6 +92,8 @@ functions:
|
|||||||
|
|
||||||
func TestDivS(t *testing.T) {
|
func TestDivS(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [mov, g0, 15]
|
- - [mov, g0, 15]
|
||||||
- [divs, g0, -3]
|
- [divs, g0, -3]
|
||||||
@@ -96,6 +110,8 @@ functions:
|
|||||||
|
|
||||||
func TestNot(t *testing.T) {
|
func TestNot(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [mov, g0, 8]
|
- - [mov, g0, 8]
|
||||||
- [not, g0]
|
- [not, g0]
|
||||||
@@ -107,6 +123,8 @@ functions:
|
|||||||
|
|
||||||
func TestAnd(t *testing.T) {
|
func TestAnd(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [mov, g0, 7]
|
- - [mov, g0, 7]
|
||||||
- [and, g0, 18]
|
- [and, g0, 18]
|
||||||
@@ -117,6 +135,8 @@ functions:
|
|||||||
|
|
||||||
func TestOr(t *testing.T) {
|
func TestOr(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [mov, g0, 7]
|
- - [mov, g0, 7]
|
||||||
- [or, g0, 18]
|
- [or, g0, 18]
|
||||||
@@ -127,6 +147,8 @@ functions:
|
|||||||
|
|
||||||
func TestXor(t *testing.T) {
|
func TestXor(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [mov, g0, 7]
|
- - [mov, g0, 7]
|
||||||
- [xor, g0, 18]
|
- [xor, g0, 18]
|
||||||
@@ -137,6 +159,8 @@ functions:
|
|||||||
|
|
||||||
func TestShR(t *testing.T) {
|
func TestShR(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [mov, g0, 53]
|
- - [mov, g0, 53]
|
||||||
- [shr, g0, 2]
|
- [shr, g0, 2]
|
||||||
@@ -147,6 +171,8 @@ functions:
|
|||||||
|
|
||||||
func TestShL(t *testing.T) {
|
func TestShL(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [mov, g0, 53]
|
- - [mov, g0, 53]
|
||||||
- [shl, g0, 2]
|
- [shl, g0, 2]
|
||||||
@@ -157,6 +183,8 @@ functions:
|
|||||||
|
|
||||||
func TestEq(t *testing.T) {
|
func TestEq(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [mov, g0, 5]
|
- - [mov, g0, 5]
|
||||||
- [eq, g0, 5]
|
- [eq, g0, 5]
|
||||||
@@ -172,6 +200,8 @@ functions:
|
|||||||
|
|
||||||
func TestLTU(t *testing.T) {
|
func TestLTU(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [mov, g0, 5]
|
- - [mov, g0, 5]
|
||||||
- [ltu, g0, 4]
|
- [ltu, g0, 4]
|
||||||
@@ -190,6 +220,8 @@ functions:
|
|||||||
|
|
||||||
func TestLTS(t *testing.T) {
|
func TestLTS(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [mov, g0, 0]
|
- - [mov, g0, 0]
|
||||||
- [lts, g0, -1]
|
- [lts, g0, -1]
|
||||||
@@ -208,6 +240,8 @@ functions:
|
|||||||
|
|
||||||
func TestGTU(t *testing.T) {
|
func TestGTU(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [mov, g0, 5]
|
- - [mov, g0, 5]
|
||||||
- [gtu, g0, 4]
|
- [gtu, g0, 4]
|
||||||
@@ -226,6 +260,8 @@ functions:
|
|||||||
|
|
||||||
func TestGTS(t *testing.T) {
|
func TestGTS(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [mov, g0, 0]
|
- - [mov, g0, 0]
|
||||||
- [gts, g0, -1]
|
- [gts, g0, -1]
|
||||||
@@ -244,6 +280,8 @@ functions:
|
|||||||
|
|
||||||
func TestLTEU(t *testing.T) {
|
func TestLTEU(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [mov, g0, 5]
|
- - [mov, g0, 5]
|
||||||
- [lteu, g0, 4]
|
- [lteu, g0, 4]
|
||||||
@@ -262,6 +300,8 @@ functions:
|
|||||||
|
|
||||||
func TestLTES(t *testing.T) {
|
func TestLTES(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [mov, g0, 0]
|
- - [mov, g0, 0]
|
||||||
- [ltes, g0, -1]
|
- [ltes, g0, -1]
|
||||||
@@ -280,6 +320,8 @@ functions:
|
|||||||
|
|
||||||
func TestGTEU(t *testing.T) {
|
func TestGTEU(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [mov, g0, 5]
|
- - [mov, g0, 5]
|
||||||
- [gteu, g0, 4]
|
- [gteu, g0, 4]
|
||||||
@@ -298,6 +340,8 @@ functions:
|
|||||||
|
|
||||||
func TestGTES(t *testing.T) {
|
func TestGTES(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [mov, g0, 0]
|
- - [mov, g0, 0]
|
||||||
- [gtes, g0, -1]
|
- [gtes, g0, -1]
|
||||||
@@ -316,6 +360,8 @@ functions:
|
|||||||
|
|
||||||
func TestJmp(t *testing.T) {
|
func TestJmp(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [jmp, +2]
|
- - [jmp, +2]
|
||||||
- [add, g0, 1]
|
- [add, g0, 1]
|
||||||
@@ -329,6 +375,8 @@ functions:
|
|||||||
|
|
||||||
func TestJmpT(t *testing.T) {
|
func TestJmpT(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [eq, 0, 0]
|
- - [eq, 0, 0]
|
||||||
- [jmpt, +2]
|
- [jmpt, +2]
|
||||||
@@ -344,6 +392,8 @@ functions:
|
|||||||
|
|
||||||
func TestJmpF(t *testing.T) {
|
func TestJmpF(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [eq, 0, 0]
|
- - [eq, 0, 0]
|
||||||
- [jmpf, +2]
|
- [jmpf, +2]
|
||||||
@@ -359,6 +409,8 @@ functions:
|
|||||||
|
|
||||||
func TestCal(t *testing.T) {
|
func TestCal(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [cal, +1]
|
- - [cal, +1]
|
||||||
- [add, g0, 1]
|
- [add, g0, 1]
|
||||||
@@ -373,6 +425,8 @@ functions:
|
|||||||
|
|
||||||
func TestCalT(t *testing.T) {
|
func TestCalT(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [eq, 0, 0]
|
- - [eq, 0, 0]
|
||||||
- [calt, +1]
|
- [calt, +1]
|
||||||
@@ -391,6 +445,8 @@ functions:
|
|||||||
|
|
||||||
func TestCalF(t *testing.T) {
|
func TestCalF(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [eq, 0, 0]
|
- - [eq, 0, 0]
|
||||||
- [calf, +1]
|
- [calf, +1]
|
||||||
@@ -409,6 +465,8 @@ functions:
|
|||||||
|
|
||||||
func TestRet(t *testing.T) {
|
func TestRet(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [cal, +1]
|
- - [cal, +1]
|
||||||
- [add, g0, 1]
|
- [add, g0, 1]
|
||||||
@@ -424,6 +482,8 @@ functions:
|
|||||||
|
|
||||||
func TestRetT(t *testing.T) {
|
func TestRetT(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [cal, +1]
|
- - [cal, +1]
|
||||||
- [add, g0, 1]
|
- [add, g0, 1]
|
||||||
@@ -442,6 +502,8 @@ functions:
|
|||||||
|
|
||||||
func TestRetF(t *testing.T) {
|
func TestRetF(t *testing.T) {
|
||||||
state := assembleAndExecute(t, `
|
state := assembleAndExecute(t, `
|
||||||
|
global_memory_size: 4
|
||||||
|
function_memory_size: 4
|
||||||
functions:
|
functions:
|
||||||
- - [cal, +1]
|
- - [cal, +1]
|
||||||
- [add, g0, 1]
|
- [add, g0, 1]
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package vm
|
package vm
|
||||||
|
|
||||||
type Program struct {
|
type Program struct {
|
||||||
Functions []*Function
|
GlobalMemorySize uint64
|
||||||
|
FunctionMemorySize uint64
|
||||||
|
Functions []*Function
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package vm
|
package vm
|
||||||
|
|
||||||
const FunctionMemorySize = 16
|
|
||||||
|
|
||||||
type stackFrame struct {
|
type stackFrame struct {
|
||||||
previousFunctionIndex int64
|
previousFunctionIndex int64
|
||||||
previousInstructionIndex int64
|
previousInstructionIndex int64
|
||||||
@@ -12,6 +10,6 @@ func newStackFrame(state *State) *stackFrame {
|
|||||||
return &stackFrame{
|
return &stackFrame{
|
||||||
previousFunctionIndex: state.functionIndex,
|
previousFunctionIndex: state.functionIndex,
|
||||||
previousInstructionIndex: state.instructionIndex,
|
previousInstructionIndex: state.instructionIndex,
|
||||||
functionMemory: NewMemory(FunctionMemorySize),
|
functionMemory: NewMemory(state.program.FunctionMemorySize),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ package vm
|
|||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
const GlobalMemorySize = 16
|
|
||||||
|
|
||||||
type State struct {
|
type State struct {
|
||||||
running bool
|
running bool
|
||||||
err error
|
err error
|
||||||
@@ -20,7 +18,7 @@ type State struct {
|
|||||||
func NewState(prog *Program) (*State, error) {
|
func NewState(prog *Program) (*State, error) {
|
||||||
return &State{
|
return &State{
|
||||||
program: prog,
|
program: prog,
|
||||||
globalMemory: NewMemory(GlobalMemorySize),
|
globalMemory: NewMemory(prog.GlobalMemorySize),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user