ludic/selfhost/backend/emit_intrin.ludic
Orkuncakilkaya 6ab98292d2 fence: every runtime call is its line's site, and site 0 comes back when it returns
A block took whatever @lp_site held when it was made. Only malloc, calloc, realloc, concat, the number
texts, the float text, a substring and Text.* set one, so every other runtime helper - intern,
Text.repeat and the string builders, the Fs, Os, unicode, uuid and crypto helpers - was charged to
whichever line had allocated last (walk 9 blamed gvk_tex_storage, m4_new, kept_push$int and
survey_op_reward for intern's 16 B copies).

- Any `call ptr @lp_*` now takes a site of its own, its kind the callee's name when no better one is
  known (intern, str_repeat, fs_list, ...). What the helper makes, in however many blocks and
  through whichever helpers it calls in turn, is that line's.
- When the call returns, @lp_site goes back to site 0, now named "(runtime) (no site) unsited": a
  block made with no site of its own says so instead of borrowing the last one.
- The ECS stores' grows and a mod's registered stores - the only allocations emitted outside
  emit_bind - take a site each.

Golden alloc_fence_sites: two lines take turns keeping memory, Text.repeat and a record, every frame
judged. Each report names its own line (25 x +32 B str_repeat at :11, 25 x +16 B new Box at :12);
the toolchain before this charges 23 of the 32 B texts to the new Box line. The other fence and arena
goldens are unchanged; ludic.base's tests pass; the game's frame ratchets are 0 on main 7cb2b164.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 00:40:28 +03:00

118 lines
5.4 KiB
Text

# emit_intrin.ludic — the low-level intrinsics the self-host source uses, each
# lowered to the same libc/inline IR the C backend emits. Returns a Val via
# g_intrin_val and sets g_intrin_ok when `name` was an intrinsic.
var g_intrin_ok: bool = false
# is `name` a low-level intrinsic? A pure name check, so it can gate dispatch
# without evaluating arguments (which could clobber shared state).
function is_intrinsic(name: pointer) -> bool {
if (name == "resize") { return true }
if (name == "file_open") or (name == "file_read") or (name == "file_write") { return true }
if (name == "file_seek") or (name == "file_tell") or (name == "file_close") { return true }
if (name == "arg_count") or (name == "arg") or (name == "exit") { return true }
if (name == "file_stderr") or (name == "file_stdout") { return true }
if (name == "run") or (name == "getenv") { return true }
if (name == "world_despawn") { return true } # #84 — despawn an entity by id from a system
return false
}
# emit " <r> = <rest>\n" and return r
function emit_bind(rest: pointer) -> pointer {
let want = fence_bind(rest) # 25.3: a LOCAL site's allocation asks for the frame's scratch
let r = nreg()
emit(" "); emit(r); emit(" = "); emit(rest); emit("\n")
if want { emit(" store i8 0, ptr @lp_want\n") }
if g_site_reset { emit(" store i32 0, ptr @lp_site\n"); g_site_reset = false }
return r
}
function arg_code(e: Node, i: int) -> pointer { let v = emit_expr(e.kids[i]); return v.code }
# a buffer handed to the platform layer: a slice's elements, never its header (as an extern's are)
function arg_buf(e: Node, i: int) -> pointer {
let v = emit_expr(e.kids[i])
if is_slice_ty(v.ty) { return emit_bind(`load ptr, ptr {slice_field(v.code, 0)}`) }
return v.code
}
function emit_intrinsic(name: pointer, e: Node) -> Val {
g_intrin_ok = true
# ptr_null / ptr_is_null are the `null` literal and `x == null` now.
# mem_alloc is bytes(n) / words(n) now (see emit_call).
if (name == "resize") {
let p = arg_code(e, 0); let n = arg_code(e, 1)
let w = emit_bind(`zext i32 {n} to i64`)
return val(emit_bind(`call ptr @lp_realloc(ptr {p}, i64 {w})`), "pointer")
}
# Every asset a Ludic program loads arrives through here — gltf_load, tex_load,
# Audio.load, Fs.read_text and the renderer's own shader loads all bottom out at
# file_open — so this one call is where a shipped game's asset pack is spliced
# in. @lp_pak_open serves the bytes from a mounted pack when the path is in one
# and falls through to @fopen when it is not, which is every open during
# development. See selfhost/backend/stdlib/emit_pak.ludic.
if (name == "file_open") {
use_pak()
let p = arg_code(e, 0); let m = arg_code(e, 1)
return val(emit_bind(`call ptr @lp_pak_open(ptr {p}, ptr {m})`), "pointer")
}
if (name == "file_read") or (name == "file_write") {
let f = arg_code(e, 0); let b = raw_expr(e.kids[1]).code; let n = arg_code(e, 2)
let w = emit_bind(`zext i32 {n} to i64`)
var fn2 = "@fread"
if (name == "file_write") { fn2 = "@fwrite" }
let r = emit_bind(`call i64 {fn2}(ptr {b}, i64 1, i64 {w}, ptr {f})`)
return val(emit_bind(`trunc i64 {r} to i32`), "int")
}
if (name == "file_seek") {
let f = arg_code(e, 0); let off = arg_code(e, 1); let wh = arg_code(e, 2)
let o = emit_bind(`sext i32 {off} to i64`)
return val(emit_bind(`call i32 @fseek(ptr {f}, i64 {o}, i32 {wh})`), "int")
}
if (name == "file_tell") {
let f = arg_code(e, 0)
let r = emit_bind(`call i64 @ftell(ptr {f})`)
return val(emit_bind(`trunc i64 {r} to i32`), "int")
}
if (name == "file_close") {
let f = arg_code(e, 0)
emit(" call i32 @fclose(ptr "); emit(f); emit(")\n")
return val("0", "void")
}
# #84 — despawn an entity by id (runs its @OnDespawn hooks + frees the slot),
# callable from a system (esys_bounds kill) or as World.despawn(e). The helper
# @fn_world_despawn is emitted with the ECS defs when g_uses_world_despawn.
if (name == "world_despawn") {
g_uses_world_despawn = true # emit @fn_world_despawn in the tail (after all uses seen)
let ent = arg_code(e, 0)
emit(" call void @fn_world_despawn(i32 "); emit(ent); emit(")\n")
return val("0", "void")
}
# print_str / print_int are the polymorphic `print(x)` builtin now (emit_call).
if (name == "arg_count") { return val(emit_bind("load i32, ptr @L_argc"), "int") }
if (name == "arg") {
let i = arg_code(e, 0)
let v = emit_bind("load ptr, ptr @L_argv")
let q = emit_bind(`getelementptr ptr, ptr {v}, i32 {i}`)
return val(emit_bind(`load ptr, ptr {q}`), "string")
}
if (name == "exit") {
let n = arg_code(e, 0)
emit(" call void @exit(i32 "); emit(n); emit(")\n")
emit(" unreachable\n")
g_term = true
return val("0", "void")
}
if (name == "file_stderr") { return val(emit_bind(stdstream_rhs(2)), "pointer") }
if (name == "file_stdout") { return val(emit_bind(stdstream_rhs(1)), "pointer") }
if (name == "run") {
let c = arg_code(e, 0)
return val(emit_bind(`call i32 @system(ptr {c})`), "int")
}
if (name == "getenv") {
let n = arg_code(e, 0)
return val(emit_bind(`call ptr @getenv(ptr {n})`), "string")
}
# bitwise ops are the operators & | ^ << >> ~ now (see emit_bin / p_mul).
# peek32 / poke32 are `words` indexing now: w[i] and w[i] = v (see emit_index_addr).
g_intrin_ok = false
return val("0", "void")
}