51 lines
959 B
Go
51 lines
959 B
Go
package gen
|
|
|
|
import "math/rand"
|
|
|
|
import "github.com/firestuff/subcoding/vm"
|
|
|
|
func RandOperand(prog *vm.Program, t vm.OperandNumericType) *vm.Operand {
|
|
for {
|
|
op := &vm.Operand{
|
|
Type: randOperandType(),
|
|
}
|
|
|
|
switch op.Type {
|
|
case vm.GlobalMemoryIndex:
|
|
op.Value = RandBiasedUint64n(prog.GlobalMemorySize)
|
|
|
|
case vm.FunctionMemoryIndex:
|
|
op.Value = RandBiasedUint64n(prog.FunctionMemorySize)
|
|
|
|
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))]
|
|
}
|