Make memory sizes parameters

This commit is contained in:
Ian Gulliver
2021-11-20 18:27:06 -10:00
parent bd84a5b969
commit 5af90aa6ff
15 changed files with 127 additions and 28 deletions

View File

@@ -2,13 +2,13 @@ package gen
import "github.com/firestuff/subcoding/vm"
func randInstruction() *vm.Instruction {
func randInstruction(prog *vm.Program) *vm.Instruction {
instr := &vm.Instruction{
OpCode: randOpCode(),
}
for i, t := range vm.OperandsByOpCode[instr.OpCode] {
instr.Operands[i] = randOperand(t)
instr.Operands[i] = randOperand(prog, t)
}
return instr

View File

@@ -4,7 +4,7 @@ import "math/rand"
import "github.com/firestuff/subcoding/vm"
func randOperand(t vm.OperandNumericType) *vm.Operand {
func randOperand(prog *vm.Program, t vm.OperandNumericType) *vm.Operand {
for {
op := &vm.Operand{
Type: randOperandType(),
@@ -12,10 +12,10 @@ func randOperand(t vm.OperandNumericType) *vm.Operand {
switch op.Type {
case vm.GlobalMemoryIndex:
op.Value = RandBiasedUint64n(vm.GlobalMemorySize)
op.Value = RandBiasedUint64n(prog.GlobalMemorySize)
case vm.FunctionMemoryIndex:
op.Value = RandBiasedUint64n(vm.FunctionMemorySize)
op.Value = RandBiasedUint64n(prog.FunctionMemorySize)
case vm.Literal:
switch t {

View File

@@ -2,14 +2,17 @@ package gen
import "github.com/firestuff/subcoding/vm"
func RandProgram() *vm.Program {
return &vm.Program{
Functions: []*vm.Function{
&vm.Function{
Instructions: []*vm.Instruction{
randInstruction(),
},
},
},
func RandProgram(globalMemorySize, functionMemorySize uint64) *vm.Program {
prog := &vm.Program{
GlobalMemorySize: globalMemorySize,
FunctionMemorySize: functionMemorySize,
}
prog.Functions = append(prog.Functions, &vm.Function{
Instructions: []*vm.Instruction{
randInstruction(prog),
},
})
return prog
}

View File

@@ -5,7 +5,7 @@ import "math/rand"
// Generate a random uint64 with an even distribution of bits.Len64()
func RandBiasedUint64() uint64 {
// 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)
}
@@ -29,7 +29,7 @@ func RandBiasedInt64() uint64 {
if shift < 64 {
return uint64(int64((rand.Uint64() | 0x8000000000000000) >> (shift + 1)))
} else {
return uint64(int64((rand.Uint64() | 0x8000000000000000) >> (shift - 63)) * -1)
return uint64(int64((rand.Uint64()|0x8000000000000000)>>(shift-63)) * -1)
}
}