Files
subcoding/gen/operand.go

51 lines
959 B
Go
Raw Normal View History

2021-11-20 17:59:22 -10:00
package gen
import "math/rand"
import "github.com/firestuff/subcoding/vm"
2021-11-20 18:27:06 -10:00
func randOperand(prog *vm.Program, t vm.OperandNumericType) *vm.Operand {
2021-11-20 17:59:22 -10:00
for {
op := &vm.Operand{
Type: randOperandType(),
}
switch op.Type {
case vm.GlobalMemoryIndex:
2021-11-20 18:27:06 -10:00
op.Value = RandBiasedUint64n(prog.GlobalMemorySize)
2021-11-20 17:59:22 -10:00
case vm.FunctionMemoryIndex:
2021-11-20 18:27:06 -10:00
op.Value = RandBiasedUint64n(prog.FunctionMemorySize)
2021-11-20 17:59:22 -10:00
case vm.Literal:
switch t {
case vm.OperandUnsigned:
op.Value = RandBiasedUint64()
case vm.OperandSigned:
op.Value = RandBiasedInt64()
case vm.OperandSignedOrUnsigned:
op.Value = RandBiasedUSint64()
case vm.OperandReference:
// Invalid. Roll the dice again.
continue
}
}
return op
}
}
var operandTypes = []vm.OperandType{
vm.Literal,
vm.FunctionMemoryIndex,
vm.GlobalMemoryIndex,
}
func randOperandType() vm.OperandType {
// Uniform distribution
return operandTypes[rand.Intn(len(operandTypes))]
}