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>
This commit is contained in:
parent
f55216af50
commit
23726afa90
51 changed files with 780 additions and 771 deletions
388
selfhost/backend/game/emit_game.ludic
Normal file
388
selfhost/backend/game/emit_game.ludic
Normal file
|
|
@ -0,0 +1,388 @@
|
|||
# emit_game.ludic — system functions and the frame loop. A system compiles to a
|
||||
# void function; main() boots (Start systems), then runs the per-frame phases in
|
||||
# order until the game stops. Runtime hooks (rt_init/poll/running/shutdown) are
|
||||
# called only when the runtime defines them.
|
||||
|
||||
function emit_system_fn(sys: Node) -> void {
|
||||
g_cur_scene = sys.c # scene owning this handler (null if global) — for `become`
|
||||
ll_t = 0; ll_lbl = 0; g_term = false; loc_reset(); nloop = 0; nself = 0
|
||||
ret_ty = "void"
|
||||
let fbody = buf_new()
|
||||
falloc = buf_new()
|
||||
let saved = code
|
||||
code = fbody
|
||||
emit_block(sys.a)
|
||||
if not g_term { emit(" br label %ret\n") }
|
||||
emit("ret:\n ret void\n")
|
||||
code = saved
|
||||
emit("define void @sys_"); emit(sys.s); emit("() {\nentry:\n")
|
||||
emit(buf_str(falloc))
|
||||
emit(buf_str(fbody))
|
||||
emit("}\n\n")
|
||||
}
|
||||
|
||||
# one enable-gated call to @sys_<d.s> (skipped while the handler is disabled).
|
||||
function emit_call_one(d: Node) -> void {
|
||||
let he = emit_bind(`load i32, ptr @HE_{d.s}`)
|
||||
var hc = emit_bind(`icmp ne i32 {he}, 0`)
|
||||
# a handler in a toggled layer also gates on its layer's @LE_ flag (byte-identical
|
||||
# for layers that are never enabled/disabled, since d.b is only read when managed)
|
||||
if (d.b != null) and is_toggled_layer(d.b.s) {
|
||||
let le = emit_bind(`load i32, ptr @LE_{d.b.s}`)
|
||||
let lc = emit_bind(`icmp ne i32 {le}, 0`)
|
||||
hc = emit_bind(`and i1 {hc}, {lc}`)
|
||||
}
|
||||
# N5: an @Server handler (d.ival==1) runs only on the authority (@L_role==1).
|
||||
# Unmarked and @Predicted handlers run on every peer. Offline @L_role defaults to
|
||||
# 1, so the guard collapses to "run here" and a non-networked build is unchanged.
|
||||
if d.ival == 1 {
|
||||
let rv = emit_bind("load i32, ptr @L_role")
|
||||
let rc = emit_bind(`icmp eq i32 {rv}, 1`)
|
||||
hc = emit_bind(`and i1 {hc}, {rc}`)
|
||||
}
|
||||
let run = lbl("hrun"); let skip = lbl("hskip")
|
||||
emit(" br i1 "); emit(hc); emit(", label %"); emit(run); emit(", label %"); emit(skip); emit("\n")
|
||||
emit(run); emit(":\n call void @sys_"); emit(d.s); emit("()\n")
|
||||
emit(" br label %"); emit(skip); emit("\n"); emit(skip); emit(":\n")
|
||||
}
|
||||
|
||||
# Global handlers run first, then the active scene's layer handlers in
|
||||
# declaration (draw) order. The active scene is snapshotted once per phase, so a
|
||||
# `become` mid-phase takes effect at the next phase boundary — exactly one scene
|
||||
# is active within any single phase.
|
||||
function emit_calls_for_phase(phase: pointer) -> void {
|
||||
var i = 0
|
||||
while i < len(prog) {
|
||||
let d = prog[i]
|
||||
if d.kind == N_SYS and (d.ty == phase) and (d.c == null) { emit_call_one(d) }
|
||||
i = i + 1
|
||||
}
|
||||
# any scene-owned handlers in this phase? gate them on one @L_scene snapshot.
|
||||
var has_sc = false
|
||||
i = 0
|
||||
while i < len(prog) { let d = prog[i]; if d.kind == N_SYS and (d.ty == phase) and (d.c != null) { has_sc = true }; i = i + 1 }
|
||||
if not has_sc { return }
|
||||
let cs = emit_bind("load i32, ptr @L_scene")
|
||||
i = 0
|
||||
while i < len(prog) {
|
||||
let d = prog[i]
|
||||
if d.kind == N_SYS and (d.ty == phase) and (d.c != null) {
|
||||
let ce = emit_bind(`icmp eq i32 {cs}, {itoa(d.c.ival)}`)
|
||||
let run = lbl("scrun"); let skip = lbl("scskip")
|
||||
emit(" br i1 "); emit(ce); emit(", label %"); emit(run); emit(", label %"); emit(skip); emit("\n")
|
||||
emit(run); emit(":\n")
|
||||
emit_call_one(d)
|
||||
emit(" br label %"); emit(skip); emit("\n"); emit(skip); emit(":\n")
|
||||
}
|
||||
i = i + 1
|
||||
}
|
||||
}
|
||||
|
||||
# on enter / on exit compile to void functions @scene_enter_<Name> /
|
||||
# @scene_exit_<Name>, called at the transition point (and enter at boot for the
|
||||
# start scene). Emitted for every scene, empty body when the hook is absent.
|
||||
function emit_scene_fn(name: pointer, kind: pointer, body: Node) -> void {
|
||||
ll_t = 0; ll_lbl = 0; g_term = false; loc_reset(); nloop = 0; nself = 0
|
||||
ret_ty = "void"
|
||||
let fbody = buf_new()
|
||||
falloc = buf_new()
|
||||
let saved = code
|
||||
code = fbody
|
||||
if (body != null) { emit_block(body) }
|
||||
# EV1: a `public` scene fires scene_<S>_enter / scene_<S>_exit after its block
|
||||
let sev = `scene_{name}_{kind}`
|
||||
if (not g_term) and (find_event(sev) != null) { emit(" call void @ev_"); emit(sev); emit("()\n") }
|
||||
if not g_term { emit(" br label %ret\n") }
|
||||
emit("ret:\n ret void\n")
|
||||
code = saved
|
||||
emit("define void @scene_"); emit(kind); emit("_"); emit(name); emit("() {\nentry:\n")
|
||||
emit(buf_str(falloc))
|
||||
emit(buf_str(fbody))
|
||||
emit("}\n\n")
|
||||
}
|
||||
|
||||
function emit_scene_hooks() -> void {
|
||||
var i = 0
|
||||
while i < len(g_scenes) {
|
||||
let sc = g_scenes[i]
|
||||
g_cur_scene = sc
|
||||
emit_scene_fn(sc.s, "enter", sc.a)
|
||||
emit_scene_fn(sc.s, "exit", sc.b)
|
||||
i = i + 1
|
||||
}
|
||||
}
|
||||
|
||||
# @OnDespawn(Model) hooks compile to `@on_despawn_<Model>(entity, reason)`
|
||||
# functions that bind the model's properties and run the body — dispatched by
|
||||
# kind at `despawn`. LC1: the `i32 %reason` parameter carries an EndReason; when
|
||||
# the hook declared `reason: r`, `r` is bound as an int local reading it.
|
||||
function emit_despawn_hooks() -> void {
|
||||
var i = 0
|
||||
while i < len(g_ondespawn) {
|
||||
let hk = g_ondespawn[i]
|
||||
let model = find_arch(hk.s)
|
||||
ll_t = 0; ll_lbl = 0; g_term = false; loc_reset(); nloop = 0; nself = 0
|
||||
ret_ty = "void"
|
||||
let fbody = buf_new()
|
||||
falloc = buf_new()
|
||||
let saved = code
|
||||
code = fbody
|
||||
if (hk.ty != null) { # bind the reason: r name to %reason
|
||||
let rslot = emit_alloca("i32")
|
||||
emit(" store i32 %reason, ptr "); emit(rslot); emit("\n")
|
||||
loc_push(hk.ty, rslot, "int")
|
||||
}
|
||||
emit_bind_props(model, "%e")
|
||||
emit_block(hk.a)
|
||||
if not g_term { emit(" br label %ret\n") }
|
||||
emit("ret:\n ret void\n")
|
||||
code = saved
|
||||
emit("define void @on_despawn_"); emit(hk.s); emit("(i32 %e, i32 %reason) {\nentry:\n")
|
||||
emit(buf_str(falloc))
|
||||
emit(buf_str(fbody))
|
||||
emit("}\n\n")
|
||||
i = i + 1
|
||||
}
|
||||
emit_despawn_all_fn()
|
||||
}
|
||||
|
||||
# LC1 "no silent deaths": at program shutdown every still-live entity's despawn
|
||||
# hook fires with reason Quit, so teardown that must run on exit is not skipped.
|
||||
# @L_despawn_all(reason) walks the live set and dispatches each entity by kind —
|
||||
# the same per-model dispatch as `despawn`, but without freeing (the process is
|
||||
# ending). Emitted only when the program has @OnDespawn hooks, so despawn-free
|
||||
# programs are byte-for-byte unchanged.
|
||||
function emit_despawn_all_fn() -> void {
|
||||
if len(g_ondespawn) == 0 { return }
|
||||
let me = itoa(MAX_ENT)
|
||||
emit("define void @L_despawn_all(i32 %reason) {\nentry:\n br label %loop\n")
|
||||
emit("loop:\n %i = phi i32 [ 0, %entry ], [ %i1, %cont ]\n")
|
||||
emit(" %n = load i32, ptr @L_entc\n %go = icmp slt i32 %i, %n\n")
|
||||
emit(" br i1 %go, label %body, label %fin\n")
|
||||
emit("body:\n %ap = getelementptr inbounds ["); emit(me); emit(" x i32], ptr @L_alive, i32 0, i32 %i\n")
|
||||
emit(" %al = load i32, ptr %ap\n %isa = icmp ne i32 %al, 0\n")
|
||||
emit(" br i1 %isa, label %do, label %cont\n")
|
||||
emit("do:\n %kp = getelementptr inbounds ["); emit(me); emit(" x i32], ptr @L_kind, i32 0, i32 %i\n")
|
||||
emit(" %k = load i32, ptr %kp\n")
|
||||
var i = 0
|
||||
while i < len(g_ondespawn) {
|
||||
let mname = g_ondespawn[i].s
|
||||
let si = itoa(i)
|
||||
emit(" %c"); emit(si); emit(" = icmp eq i32 %k, "); emit(itoa(find_arch_id(mname))); emit("\n")
|
||||
emit(" br i1 %c"); emit(si); emit(", label %hit"); emit(si); emit(", label %next"); emit(si); emit("\n")
|
||||
emit("hit"); emit(si); emit(":\n call void @on_despawn_"); emit(mname); emit("(i32 %i, i32 %reason)\n")
|
||||
let dev = `model_{mname}_despawn` # EV1: @Public despawn event at shutdown
|
||||
if (find_event(dev) != null) { emit(" call void @ev_"); emit(dev); emit("(i32 %i, i32 %reason)\n") }
|
||||
emit(" br label %next"); emit(si); emit("\n")
|
||||
emit("next"); emit(si); emit(":\n")
|
||||
i = i + 1
|
||||
}
|
||||
emit(" br label %cont\n")
|
||||
emit("cont:\n %i1 = add i32 %i, 1\n br label %loop\n")
|
||||
emit("fin:\n ret void\n}\n\n")
|
||||
}
|
||||
|
||||
# EV0: each declared `event E` compiles to a `@ev_<E>(payload…)` function whose
|
||||
# body is (1) its `@On(E)` listeners concatenated in declaration order — the
|
||||
# closed, compile-time half — then (2) a loop over a runtime listener array, the
|
||||
# open half a mod in another language joins through the C ABI. The payload fields
|
||||
# arrive as params (%p0, %p1, …), bound by name so a listener body reads them bare
|
||||
# (like a query/hook binding). Emitted only when g_events is non-empty, so an
|
||||
# event-free program is byte-for-byte unchanged.
|
||||
#
|
||||
# The runtime half is the deliberate opt-in exception to "no dispatch tables":
|
||||
# %Ev_<E> — the POD payload struct passed by pointer to foreign listeners
|
||||
# @evL_<E> — a fixed-capacity [16 x ptr] array of foreign callbacks
|
||||
# @evN_<E> — how many are registered (registration order = dispatch order)
|
||||
# @ludic_on_<E>(ptr cb) -> i32 — the C ABI: a mod appends its callback
|
||||
# A native Ludic listener costs a direct call; a foreign one costs one indirect
|
||||
# call. With no foreign listeners registered the loop runs zero times (one branch).
|
||||
const EV_CAP: int = 16
|
||||
# EV6 — re-entrant emit is bounded: a listener may `emit` another event, but the
|
||||
# nesting is capped so an event cycle traps as an early return instead of hanging
|
||||
# the frame. @ev_depth counts the live dispatch nesting; past the cap a dispatch
|
||||
# returns immediately (a cancellable event returns "not cancelled").
|
||||
const EV_DEPTH_CAP: int = 32
|
||||
|
||||
function emit_event_fns() -> void {
|
||||
emith("@ev_depth = global i32 0\n") # EV6 re-entrancy counter
|
||||
var e = 0
|
||||
while e < len(g_events) {
|
||||
let ev = g_events[e]
|
||||
let en = ev.s
|
||||
let cap = itoa(EV_CAP)
|
||||
|
||||
# --- module-level: payload struct + the foreign listener registry (into head)
|
||||
# A cancellable event's payload carries a trailing i32 `cancelled` flag that a
|
||||
# listener (native or foreign) can set; the caller reads it back.
|
||||
emith("%Ev_"); emith(en); emith(" = type { ")
|
||||
var t = 0
|
||||
while t < len(ev.kids) {
|
||||
if t > 0 { emith(", ") }
|
||||
emith(llty(ev.kids[t].ty))
|
||||
t = t + 1
|
||||
}
|
||||
if ev.ival == 1 { if len(ev.kids) > 0 { emith(", ") }; emith("i32") }
|
||||
emith(" }\n")
|
||||
emith("@evL_"); emith(en); emith(" = global ["); emith(cap); emith(" x ptr] zeroinitializer\n")
|
||||
emith("@evN_"); emith(en); emith(" = global i32 0\n")
|
||||
# EV5: a parallel owner array — -1 = program-scoped (never swept), >=0 = the
|
||||
# entity that owns the listener (swept when that entity despawns).
|
||||
emith("@evO_"); emith(en); emith(" = global ["); emith(cap); emith(" x i32] zeroinitializer\n")
|
||||
|
||||
# --- @ludic_on_<E>(cb): append a program-scoped callback, return a token
|
||||
emit("define i32 @ludic_on_"); emit(en); emit("(ptr %cb) {\nentry:\n")
|
||||
emit(" %n = load i32, ptr @evN_"); emit(en); emit("\n")
|
||||
emit(" %full = icmp slt i32 %n, "); emit(cap); emit("\n")
|
||||
emit(" br i1 %full, label %add, label %drop\n")
|
||||
emit("add:\n %slot = getelementptr inbounds ["); emit(cap); emit(" x ptr], ptr @evL_"); emit(en); emit(", i32 0, i32 %n\n")
|
||||
emit(" store ptr %cb, ptr %slot\n")
|
||||
emit(" %oslot = getelementptr inbounds ["); emit(cap); emit(" x i32], ptr @evO_"); emit(en); emit(", i32 0, i32 %n\n")
|
||||
emit(" store i32 -1, ptr %oslot\n")
|
||||
emit(" %n1 = add i32 %n, 1\n store i32 %n1, ptr @evN_"); emit(en); emit("\n ret i32 %n\n")
|
||||
emit("drop:\n ret i32 -1\n}\n\n") # registry full: reject (token -1)
|
||||
|
||||
# --- @ludic_on_entity_<E>(owner, cb): append an entity-scoped callback
|
||||
emit("define i32 @ludic_on_entity_"); emit(en); emit("(i32 %owner, ptr %cb) {\nentry:\n")
|
||||
emit(" %n = load i32, ptr @evN_"); emit(en); emit("\n")
|
||||
emit(" %full = icmp slt i32 %n, "); emit(cap); emit("\n")
|
||||
emit(" br i1 %full, label %add, label %drop\n")
|
||||
emit("add:\n %slot = getelementptr inbounds ["); emit(cap); emit(" x ptr], ptr @evL_"); emit(en); emit(", i32 0, i32 %n\n")
|
||||
emit(" store ptr %cb, ptr %slot\n")
|
||||
emit(" %oslot = getelementptr inbounds ["); emit(cap); emit(" x i32], ptr @evO_"); emit(en); emit(", i32 0, i32 %n\n")
|
||||
emit(" store i32 %owner, ptr %oslot\n")
|
||||
emit(" %n1 = add i32 %n, 1\n store i32 %n1, ptr @evN_"); emit(en); emit("\n ret i32 %n\n")
|
||||
emit("drop:\n ret i32 -1\n}\n\n")
|
||||
|
||||
# --- @ludic_off_<E>(token): remove a listener (tombstone the slot to null)
|
||||
emit("define void @ludic_off_"); emit(en); emit("(i32 %tok) {\nentry:\n")
|
||||
emit(" %n = load i32, ptr @evN_"); emit(en); emit("\n")
|
||||
emit(" %lo = icmp sge i32 %tok, 0\n %hi = icmp slt i32 %tok, %n\n %ok = and i1 %lo, %hi\n")
|
||||
emit(" br i1 %ok, label %do, label %skip\n")
|
||||
emit("do:\n %slot = getelementptr inbounds ["); emit(cap); emit(" x ptr], ptr @evL_"); emit(en); emit(", i32 0, i32 %tok\n")
|
||||
emit(" store ptr null, ptr %slot\n br label %skip\n")
|
||||
emit("skip:\n ret void\n}\n\n")
|
||||
|
||||
# --- @ev_<E>(payload): fire compile-time listeners, then foreign ones
|
||||
ll_t = 0; ll_lbl = 0; g_term = false; loc_reset(); nloop = 0; nself = 0
|
||||
ret_ty = "void"
|
||||
let fbody = buf_new()
|
||||
falloc = buf_new()
|
||||
let saved = code
|
||||
code = fbody
|
||||
# EV6: bound re-entrant emit — past EV_DEPTH_CAP, return without dispatching
|
||||
emit(" %evd = load i32, ptr @ev_depth\n")
|
||||
emit(" %evover = icmp sge i32 %evd, "); emit(itoa(EV_DEPTH_CAP)); emit("\n")
|
||||
emit(" br i1 %evover, label %evcap, label %evgo\n")
|
||||
emit("evcap:\n")
|
||||
if ev.ival == 1 { emit(" ret i32 0\n") } else { emit(" ret void\n") }
|
||||
emit("evgo:\n")
|
||||
emit(" %evd1 = add i32 %evd, 1\n store i32 %evd1, ptr @ev_depth\n")
|
||||
# a stack copy of the payload, passed by pointer to every foreign listener
|
||||
let pl = emit_alloca(`%Ev_{en}`)
|
||||
# bind each field: store the param into the payload struct AND a name slot the
|
||||
# compile-time listener bodies read bare.
|
||||
var f = 0
|
||||
while f < len(ev.kids) {
|
||||
let fd = ev.kids[f]
|
||||
let lt = llty(fd.ty)
|
||||
let pa = nreg(); emit(" "); emit(pa); emit(" = getelementptr inbounds %Ev_"); emit(en); emit(", ptr "); emit(pl); emit(", i32 0, i32 "); emit(itoa(f)); emit("\n")
|
||||
emit(" store "); emit(lt); emit(" %p"); emit(itoa(f)); emit(", ptr "); emit(pa); emit("\n")
|
||||
let slot = emit_alloca(lt)
|
||||
emit(" store "); emit(lt); emit(" %p"); emit(itoa(f)); emit(", ptr "); emit(slot); emit("\n")
|
||||
loc_push(fd.s, slot, fd.ty)
|
||||
f = f + 1
|
||||
}
|
||||
# cancellable: zero the flag and expose its address to `cancel` in the listeners
|
||||
var caddr = null
|
||||
if ev.ival == 1 {
|
||||
caddr = nreg(); emit(" "); emit(caddr); emit(" = getelementptr inbounds %Ev_"); emit(en); emit(", ptr "); emit(pl); emit(", i32 0, i32 "); emit(itoa(len(ev.kids))); emit("\n")
|
||||
emit(" store i32 0, ptr "); emit(caddr); emit("\n")
|
||||
g_cancel_addr = caddr
|
||||
}
|
||||
let base = nloc # listeners share the params but not each other's locals
|
||||
var i = 0
|
||||
while i < len(g_onlisten) {
|
||||
if (g_onlisten[i].s == en) { nloc = base; g_term = false; emit_block(g_onlisten[i].a) }
|
||||
i = i + 1
|
||||
}
|
||||
# the open half: walk the foreign callback array in registration order
|
||||
if not g_term {
|
||||
let ci = emit_alloca("i32"); emit(" store i32 0, ptr "); emit(ci); emit("\n")
|
||||
let L = lbl("evl"); let B = lbl("evb"); let D = lbl("evd")
|
||||
emit(" br label %"); emit(L); emit("\n")
|
||||
emit(L); emit(":\n")
|
||||
let iv = emit_bind(`load i32, ptr {ci}`)
|
||||
let nn = emit_bind(`load i32, ptr @evN_{en}`)
|
||||
let go = emit_bind(`icmp slt i32 {iv}, {nn}`)
|
||||
emit(" br i1 "); emit(go); emit(", label %"); emit(B); emit(", label %"); emit(D); emit("\n")
|
||||
emit(B); emit(":\n")
|
||||
let sp = nreg(); emit(" "); emit(sp); emit(" = getelementptr inbounds ["); emit(cap); emit(" x ptr], ptr @evL_"); emit(en); emit(", i32 0, i32 "); emit(iv); emit("\n")
|
||||
let cb = emit_bind(`load ptr, ptr {sp}`)
|
||||
let cbn = emit_bind(`icmp eq ptr {cb}, null`) # EV5: a removed (off) listener is null — skip it
|
||||
let doc = lbl("evdo"); let skp = lbl("evsk")
|
||||
emit(" br i1 "); emit(cbn); emit(", label %"); emit(skp); emit(", label %"); emit(doc); emit("\n")
|
||||
emit(doc); emit(":\n")
|
||||
emit(" call void "); emit(cb); emit("(ptr "); emit(pl); emit(")\n")
|
||||
emit(" br label %"); emit(skp); emit("\n")
|
||||
emit(skp); emit(":\n")
|
||||
let i2 = emit_bind(`add i32 {iv}, 1`)
|
||||
emit(" store i32 "); emit(i2); emit(", ptr "); emit(ci); emit("\n")
|
||||
emit(" br label %"); emit(L); emit("\n")
|
||||
emit(D); emit(":\n")
|
||||
emit(" br label %ret\n")
|
||||
}
|
||||
emit("ret:\n")
|
||||
emit(" %evdd = load i32, ptr @ev_depth\n %evdd1 = sub i32 %evdd, 1\n store i32 %evdd1, ptr @ev_depth\n") # EV6: leave one nesting level
|
||||
if ev.ival == 1 { # return the (possibly set) cancelled flag
|
||||
let cv = emit_bind(`load i32, ptr {caddr}`)
|
||||
emit(" ret i32 "); emit(cv); emit("\n")
|
||||
} else { emit(" ret void\n") }
|
||||
g_cancel_addr = null # leaves listener scope
|
||||
code = saved
|
||||
var rt = "void"; if ev.ival == 1 { rt = "i32" }
|
||||
emit("define "); emit(rt); emit(" @ev_"); emit(en); emit("(")
|
||||
var g = 0
|
||||
while g < len(ev.kids) {
|
||||
if g > 0 { emit(", ") }
|
||||
emit(llty(ev.kids[g].ty)); emit(" %p"); emit(itoa(g))
|
||||
g = g + 1
|
||||
}
|
||||
emit(") {\nentry:\n")
|
||||
emit(buf_str(falloc))
|
||||
emit(buf_str(fbody))
|
||||
emit("}\n\n")
|
||||
e = e + 1
|
||||
}
|
||||
|
||||
# EV5: @ludic_sweep_entity(owner) — remove every entity-scoped listener owned by
|
||||
# a despawning entity, across all events. Called from `despawn`, so a listener
|
||||
# bound to an entity cannot outlive it (the Node listener-leak footgun, gone).
|
||||
let capS = itoa(EV_CAP)
|
||||
emit("define void @ludic_sweep_entity(i32 %owner) {\nentry:\n %ci = alloca i32\n store i32 0, ptr %ci\n br label %sw0\n")
|
||||
var e2 = 0
|
||||
while e2 < len(g_events) {
|
||||
let en2 = g_events[e2].s; let sk = itoa(e2)
|
||||
emit("sw"); emit(sk); emit(":\n")
|
||||
emit(" %swi"); emit(sk); emit(" = load i32, ptr %ci\n")
|
||||
emit(" %swn"); emit(sk); emit(" = load i32, ptr @evN_"); emit(en2); emit("\n")
|
||||
emit(" %swg"); emit(sk); emit(" = icmp slt i32 %swi"); emit(sk); emit(", %swn"); emit(sk); emit("\n")
|
||||
emit(" br i1 %swg"); emit(sk); emit(", label %swb"); emit(sk); emit(", label %swd"); emit(sk); emit("\n")
|
||||
emit("swb"); emit(sk); emit(":\n")
|
||||
emit(" %swop"); emit(sk); emit(" = getelementptr inbounds ["); emit(capS); emit(" x i32], ptr @evO_"); emit(en2); emit(", i32 0, i32 %swi"); emit(sk); emit("\n")
|
||||
emit(" %swov"); emit(sk); emit(" = load i32, ptr %swop"); emit(sk); emit("\n")
|
||||
emit(" %swm"); emit(sk); emit(" = icmp eq i32 %swov"); emit(sk); emit(", %owner\n")
|
||||
emit(" br i1 %swm"); emit(sk); emit(", label %swh"); emit(sk); emit(", label %swx"); emit(sk); emit("\n")
|
||||
emit("swh"); emit(sk); emit(":\n")
|
||||
emit(" %swlp"); emit(sk); emit(" = getelementptr inbounds ["); emit(capS); emit(" x ptr], ptr @evL_"); emit(en2); emit(", i32 0, i32 %swi"); emit(sk); emit("\n")
|
||||
emit(" store ptr null, ptr %swlp"); emit(sk); emit("\n br label %swx"); emit(sk); emit("\n")
|
||||
emit("swx"); emit(sk); emit(":\n")
|
||||
emit(" %swi1"); emit(sk); emit(" = add i32 %swi"); emit(sk); emit(", 1\n store i32 %swi1"); emit(sk); emit(", ptr %ci\n br label %sw"); emit(sk); emit("\n")
|
||||
emit("swd"); emit(sk); emit(":\n store i32 0, ptr %ci\n")
|
||||
if (e2 + 1) < len(g_events) { emit(" br label %sw"); emit(itoa(e2 + 1)); emit("\n") }
|
||||
else { emit(" ret void\n") }
|
||||
e2 = e2 + 1
|
||||
}
|
||||
emit("}\n\n")
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue