File size: 4,306 Bytes
2166d7e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | # Befunge code generator: Stack-based SUBLEQ code generator
# Converts Befunge stack operations to deterministic SUBLEQ triads
# Memory layout: Constants (0-8), Stack (20-120), Code (200+)
# Compile: nim c -d:release befunge_codegen.nim
import hybrid_ast
import subleq_bf
const
# Memory layout for stack-based operations
Z = 0 # zero (constant 0)
ONE = 3 # one (constant 1)
M1 = 4 # minus one (constant -1)
SP = 5 # stack pointer
NSP = 6 # negative stack pointer
T = 7 # temp register
U = 8 # temp register
STACK_BASE = 20
CODE0 = 200
NEXT = low(int)
proc codegenBefunge*(prog: Program; stackSize = 100): Transpiled =
if stackSize <= 0:
var err: Transpiled
err.error = "stackSize must be positive"
return err
var code: seq[int]
var baseFix: seq[int]
proc here(): int = CODE0 + code.len
proc tri(a, b: int; c = NEXT) =
let at = here()
code.add a
code.add b
code.add (if c == NEXT: at + 3 else: c)
# Push value onto stack: mem[STACK_BASE + SP] = val; SP++
proc genPush(val: int) =
tri(T, T)
tri(M1, T)
tri(val, T)
tri(T, STACK_BASE + SP)
tri(M1, SP)
# Pop from stack: val = mem[STACK_BASE + SP]; SP--
proc genPop(): int =
let at = here()
tri(SP, SP)
tri(M1, SP)
# Load from stack
let target = here() + 3
tri(NSP, target)
baseFix.add target - CODE0
tri(STACK_BASE, U)
tri(SP, target)
return at
# Stack operations: add, sub, mul, div, mod
proc genAdd() =
discard genPop()
tri(U, T)
discard genPop()
tri(T, U)
tri(U, T)
tri(M1, SP)
tri(T, STACK_BASE + SP)
tri(M1, SP)
proc genSub() =
discard genPop()
tri(U, T)
discard genPop()
tri(T, U)
tri(U, T)
tri(M1, T)
tri(M1, SP)
tri(T, STACK_BASE + SP)
tri(M1, SP)
proc genMulSimple() =
discard genPop()
discard genPop()
tri(M1, SP)
tri(Z, STACK_BASE + SP)
tri(M1, SP)
proc genDiv() =
discard genPop()
discard genPop()
tri(M1, SP)
tri(Z, STACK_BASE + SP)
tri(M1, SP)
proc genMod() =
discard genPop()
discard genPop()
tri(M1, SP)
tri(Z, STACK_BASE + SP)
tri(M1, SP)
proc genNot() =
discard genPop()
tri(U, U)
let p = here()
tri(Z, U, p + 6)
tri(Z, Z, p + 9)
tri(ONE, T)
tri(M1, SP)
tri(T, STACK_BASE + SP)
tri(M1, SP)
proc genGreater() =
discard genPop()
tri(U, T)
discard genPop()
tri(T, U)
tri(U, M1)
let p = here()
tri(Z, U, p + 6)
tri(Z, Z, p + 9)
tri(ONE, T)
tri(M1, SP)
tri(T, STACK_BASE + SP)
tri(M1, SP)
proc genOutput() =
discard genPop()
tri(U, -1)
tri(M1, SP)
proc genInput() =
tri(-2, T)
tri(M1, SP)
tri(T, STACK_BASE + SP)
tri(M1, SP)
proc genDup() =
tri(SP, SP)
tri(ONE, SP)
let target = here() + 3
tri(NSP, target)
baseFix.add target - CODE0
tri(STACK_BASE, T)
tri(SP, target)
tri(T, T)
tri(M1, T)
tri(T, STACK_BASE + SP)
tri(M1, SP)
proc genSwap() =
discard genPop()
tri(U, T)
discard genPop()
tri(T, STACK_BASE + SP)
tri(M1, SP)
tri(U, STACK_BASE + SP)
tri(M1, SP)
# Initialize stack pointer to 0
tri(Z, SP)
# Generate code for each instruction
for instr in prog.instrs:
case instr.kind
of ikPush:
genPush(instr.value)
of ikAdd:
genAdd()
of ikSub:
genSub()
of ikMul:
genMulSimple()
of ikDiv:
genDiv()
of ikMod:
genMod()
of ikNot:
genNot()
of ikGreater:
genGreater()
of ikOutput:
genOutput()
of ikInput:
genInput()
of ikDup:
genDup()
of ikSwap:
genSwap()
of ikHalt:
tri(Z, Z, -1)
else:
var err: Transpiled
err.error = "unimplemented Befunge instruction: " & $instr.kind
return err
tri(Z, Z, -1)
let base = CODE0 + code.len
result.tapeBase = base
result.tapeCells = stackSize
result.mem = newSeq[int](base + stackSize)
result.mem[Z] = 0
result.mem[ONE] = 1
result.mem[M1] = -1
result.mem[SP] = 0
result.mem[NSP] = 0
for i, v in code:
result.mem[CODE0 + i] = v
for idx in baseFix:
result.mem[CODE0 + idx] += base
|