This commit is contained in:
2026-05-31 11:23:07 +01:00
parent 1d51b29b92
commit 5ea458fb06
18 changed files with 1367 additions and 220 deletions
+32
View File
@@ -1,7 +1,9 @@
package vm
import (
"crypto/rand"
"fmt"
"math/big"
)
type Instruction struct {
@@ -15,7 +17,15 @@ type LabelledInstr struct {
Target string
}
type CompileOpts struct {
NoJunk bool
}
func Compile(program []LabelledInstr) []byte {
return CompileWith(program, CompileOpts{})
}
func CompileWith(program []LabelledInstr, opts CompileOpts) []byte {
labelAddrs := make(map[string]int)
type fixup struct {
@@ -26,11 +36,18 @@ func Compile(program []LabelledInstr) []byte {
var fixups []fixup
var bytecode []byte
junkOpcodes := []Opcode{opJunkA, opJunkB, opJunkC, opJunkD}
for _, entry := range program {
if entry.Label != "" {
labelAddrs[entry.Label] = len(bytecode)
}
if !opts.NoJunk && shouldInsertJunk() {
junkOp := junkOpcodes[randIntn(len(junkOpcodes))]
bytecode = append(bytecode, encodeOp(junkOp))
}
instr := entry.Instr
isJump := instr.Op == OpJmp || instr.Op == OpJz || instr.Op == OpJnz
@@ -64,3 +81,18 @@ func Compile(program []LabelledInstr) []byte {
return bytecode
}
func shouldInsertJunk() bool {
return randIntn(3) == 0
}
func randIntn(n int) int {
if n <= 0 {
return 0
}
b, err := rand.Int(rand.Reader, big.NewInt(int64(n)))
if err != nil {
return 0
}
return int(b.Int64())
}