Generate random single instructions
This commit is contained in:
50
gen/operand.go
Normal file
50
gen/operand.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package gen
|
||||
|
||||
import "math/rand"
|
||||
|
||||
import "github.com/firestuff/subcoding/vm"
|
||||
|
||||
func randOperand(t vm.OperandNumericType) *vm.Operand {
|
||||
for {
|
||||
op := &vm.Operand{
|
||||
Type: randOperandType(),
|
||||
}
|
||||
|
||||
switch op.Type {
|
||||
case vm.GlobalMemoryIndex:
|
||||
op.Value = RandBiasedUint64()
|
||||
|
||||
case vm.FunctionMemoryIndex:
|
||||
op.Value = RandBiasedUint64()
|
||||
|
||||
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))]
|
||||
}
|
||||
Reference in New Issue
Block a user