ludic/selfhost/backend/emit_mem.ludic
Orkuncakilkaya 23726afa90
All checks were successful
bootstrap / cfree-fixpoint (push) Successful in 12s
ci / build-and-test (push) Successful in 50s
commit-lint / conventional-commits (push) Successful in 3s
docs / build-and-deploy (push) Successful in 2s
refactor(selfhost): reorganise into concern-based subdirectories
Split the flat 38-file selfhost/ into concern-based subdirectories:

  frontend/        lex, parse, parse_game, ast
  support/         str, buf, io
  backend/         core IR + expression/statement lowering
  backend/game/    ECS/scene/event/world lowering
  backend/stdlib/  the namespaced Math.*/Text.*/Crypto.*/… intrinsics

and split the three oversized emitters at responsibility boundaries so
no file mixes concerns:

  emit_game.ludic  -> + emit_world.ludic         (reflection world table,
                                                  tick helpers, @main synthesis)
  emit_expr.ludic  -> + emit_call.ludic          (namespaced builtins, call
                                                  lowering, expr dispatch)
  emit_text.ludic  -> + emit_text_prelude.ludic  (emitted string-builder runtime)

FRAGS in tools/x/selfhost.ludic is updated to the new paths with the link
order preserved, and the Python doc/vocabulary tooling is updated to walk
the new layout. Because the build is a plain in-order concatenation and
every split lands on a blank-line boundary, the regenerated seed is
byte-identical: `x reseed` leaves selfhost/ludicc.seed.ll unchanged,
`x bootstrap-cfree` still reaches its fixed point, and both `x test` (56)
and `x selfhost-test` (29, incl. golden renders) stay green.

Closes #29

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-31 00:26:02 +03:00

48 lines
2.5 KiB
Text

# emit_mem.ludic — the Memory.* namespace: raw buffers and byte pokes. bytes/words
# allocate (as the bare builtins do); copy/fill wrap memcpy/memset; peek/poke
# read and write one byte. The low-level escape hatch, PICO-8's memcpy/memset/
# peek/poke by another name.
function is_mem_ns(meth: pointer) -> bool {
if (meth == "bytes") or (meth == "words") { return true }
if (meth == "copy") or (meth == "fill") or (meth == "peek") or (meth == "poke") { return true }
return false
}
function emit_mem_ns(meth: pointer, e: Node) -> Val {
if (meth == "bytes") { # allocate n bytes -> a byte buffer
let n = emit_expr(e.kids[0])
let w = emit_bind(`zext i32 {n.code} to i64`)
return val(emit_bind(`call ptr @malloc(i64 {w})`), "pointer")
}
if (meth == "words") { # allocate n 32-bit words
let n = emit_expr(e.kids[0])
let by = emit_bind(`mul i32 {n.code}, 4`)
let w = emit_bind(`zext i32 {by} to i64`)
return val(emit_bind(`call ptr @malloc(i64 {w})`), "words")
}
if (meth == "copy") { # copy n bytes src -> dst
let dst = emit_expr(e.kids[0]); let src = emit_expr(e.kids[1]); let n = emit_expr(e.kids[2])
let w = emit_bind(`zext i32 {n.code} to i64`)
emit(" call ptr @memcpy(ptr "); emit(dst.code); emit(", ptr "); emit(src.code); emit(", i64 "); emit(w); emit(")\n")
return val("0", "void")
}
if (meth == "fill") { # set n bytes of buf to value v
let buf = emit_expr(e.kids[0]); let v = emit_expr(e.kids[1]); let n = emit_expr(e.kids[2])
let w = emit_bind(`zext i32 {n.code} to i64`)
emit(" call ptr @memset(ptr "); emit(buf.code); emit(", i32 "); emit(v.code); emit(", i64 "); emit(w); emit(")\n")
return val("0", "void")
}
if (meth == "peek") { # read one byte at buf[i], 0..255
let buf = emit_expr(e.kids[0]); let i = emit_expr(e.kids[1])
let p = emit_bind(`getelementptr inbounds i8, ptr {buf.code}, i32 {i.code}`)
let c = emit_bind(`load i8, ptr {p}`)
return val(emit_bind(`zext i8 {c} to i32`), "int")
}
# poke: write the low byte of v at buf[i]
let buf = emit_expr(e.kids[0]); let i = emit_expr(e.kids[1]); let v = emit_expr(e.kids[2])
let p = emit_bind(`getelementptr inbounds i8, ptr {buf.code}, i32 {i.code}`)
let b = emit_bind(`trunc i32 {v.code} to i8`)
emit(" store i8 "); emit(b); emit(", ptr "); emit(p); emit("\n")
return val("0", "void")
}