wip(0.S3): calls into the runtime get its states supplied; the emitter's own calls to runtime functions reach their thunks; the migration names the runtime's states per file

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-25 15:44:50 +03:00
parent a476ec7976
commit d70b00f30d
15 changed files with 54328 additions and 53309 deletions

View file

@ -958,6 +958,7 @@ function emit_call(e: Node) -> Val {
}
vis_check(fn2, name)
state_inject_generated(e, fn2) # 0.S: a call the compiler wrote gets its states
state_inject_runtime(e, fn2) # and a call into the runtime
call_fill_defaults(e, fn2) # L11 - for code the checker does not walk
reorder_named(e, param_labels(fn2))
# evaluate args first (their IR is emitted before the call instruction), coercing

View file

@ -60,7 +60,7 @@ function emit_main(d: Node) -> void {
# game that renders (drives tick_render, draws, uses the engine light pass) has
# its framebuffer allocated. Headless it only allocates — no window, no output —
# so a non-rendering entry game is unchanged.
if (find_fn("rt_init") != null) { emit(" call void @fn_rt_init()\n") }
if (find_fn("rt_init") != null) { emit(" call void " + fn_sym("rt_init") + "()\n") }
emit(" call void @L_init_globals()\n")
emit_block(d.a)
if not g_term { emit(" br label %ret\n") }

View file

@ -75,12 +75,12 @@ function emit_engine_systems_for_phase(phase: pointer) -> void {
if (phase == "Update") {
# Tween.* fluent handles (#48): advanced each Update tick when the game uses
# them (gated on g_uses_tween_rt rather than a declared component).
if g_uses_tween_rt and (find_fn("esys_tween") != null) { emit(" call void @fn_esys_tween()\n") }
if g_uses_tween_rt and (find_fn("esys_tween") != null) { emit(" call void " + fn_sym("esys_tween") + "()\n") }
if has_countdowns() { emit(" call void @L_countdowns()\n") } # `countdown` fields tick toward 0
if g_uses_fx and (find_fn("fx_tick") != null) { emit(" call void @fn_fx_tick()\n") } # Fx.* age and move
if g_uses_fx and (find_fn("fx_tick") != null) { emit(" call void " + fn_sym("fx_tick") + "()\n") } # Fx.* age and move
}
if (phase == "Render") {
if g_uses_fx and (find_fn("fx_draw") != null) { emit(" call void @fn_fx_draw()\n") } # Fx.* draw after the sprites
if g_uses_fx and (find_fn("fx_draw") != null) { emit(" call void " + fn_sym("fx_draw") + "()\n") } # Fx.* draw after the sprites
}
}
@ -361,7 +361,7 @@ function emit_scene_menu_render() -> void {
let ce = emit_bind(`icmp eq i32 {cs}, {itoa(sc.ival)}`)
let run = lbl("mrun"); let skip = lbl("mskip")
emit(" br i1 "); emit(ce); emit(", label %"); emit(run); emit(", label %"); emit(skip); emit("\n")
emit(run); emit(":\n call void @fn_rt_ui_render()\n")
emit(run); emit(":\n call void " + fn_sym("rt_ui_render") + "()\n")
emit(" br label %"); emit(skip); emit("\n"); emit(skip); emit(":\n")
}
i += 1
@ -380,10 +380,10 @@ function emit_scene_fn(name: pointer, kind: pointer, body: Node, menu: pointer)
code = fbody
# `scene X shows Menu`: a free cursor and the menu open on enter, closed on exit
if (menu != null) and (kind == "enter") {
if (find_fn("input_cursor_mode") != null) { emit(" call void @fn_input_cursor_mode(i32 0)\n") }
emit(" call void @fn_rt_ui_open(i32 "); emit(itoa(ui_index_of("UI_" + menu))); emit(")\n")
if (find_fn("input_cursor_mode") != null) { emit(" call void " + fn_sym("input_cursor_mode") + "(i32 0)\n") }
emit(" call void " + fn_sym("rt_ui_open") + "(i32 "); emit(itoa(ui_index_of("UI_" + menu))); emit(")\n")
}
if (menu != null) and (kind == "exit") { emit(" call void @fn_rt_ui_close()\n") }
if (menu != null) and (kind == "exit") { emit(" call void " + fn_sym("rt_ui_close") + "()\n") }
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}`

View file

@ -56,7 +56,7 @@ function emit_machine(st: Node) -> void {
if (addr != null) { s = emit_bind(`load i32, ptr {addr}`) }
else {
let regv = emit_expr(st.a)
s = emit_bind(`call i32 @fn_rt_reg(i32 {regv.code})`)
s = emit_bind(`call i32 {fn_sym("rt_reg")}(i32 {regv.code})`)
}
if nmach < len(mach_stk) { mach_stk[nmach] = st } else { push(mach_stk, st) }
nmach += 1
@ -96,7 +96,7 @@ function emit_become(st: Node) -> void {
emit(` store i32 {sv}, ptr {addr}\n`)
} else {
let regv = emit_expr(m.a)
emit(" call void @fn_rt_set_reg(i32 "); emit(regv.code); emit(", i32 "); emit(sv); emit(")\n")
emit(" call void " + fn_sym("rt_set_reg") + "(i32 "); emit(regv.code); emit(", i32 "); emit(sv); emit(")\n")
}
return
}

View file

@ -86,7 +86,7 @@ function emit_snapshot() -> void {
emit(" %bad = icmp eq ptr %f, null\n")
emit(" br i1 %bad, label %out, label %go\ngo:\n")
emit_snapshot_blocks("fwrite")
if has_save { emit(" call void @fn_rt_save_state(ptr %f)\n") }
if has_save { emit(" call void " + fn_sym("rt_save_state") + "(ptr %f)\n") }
emit(" %c = call i32 @fclose(ptr %f)\n br label %out\nout:\n ret void\n}\n\n")
emit("define i32 @L_load() {\nentry:\n")
@ -94,7 +94,7 @@ function emit_snapshot() -> void {
emit(" %bad = icmp eq ptr %f, null\n")
emit(" br i1 %bad, label %miss, label %go\nmiss:\n ret i32 0\ngo:\n")
emit_snapshot_blocks("fread")
if has_load { emit(" call void @fn_rt_load_state(ptr %f)\n") }
if has_load { emit(" call void " + fn_sym("rt_load_state") + "(ptr %f)\n") }
emit(" %c = call i32 @fclose(ptr %f)\n ret i32 1\n}\n\n")
# world_save(buf) -> int / world_load(buf, len): the same whole-world snapshot,

View file

@ -85,7 +85,7 @@ function is_ui_ident(nm: pointer) -> bool {
}
function ll_ui_set(idx: int, key: int, val: pointer) -> void {
emit(" call void @fn_rt_ui_set(i32 "); emit(itoa(idx)); emit(", i32 "); emit(itoa(key)); emit(", i32 "); emit(val); emit(")\n")
emit(" call void " + fn_sym("rt_ui_set") + "(i32 "); emit(itoa(idx)); emit(", i32 "); emit(itoa(key)); emit(", i32 "); emit(val); emit(")\n")
}
function ui_prop_key(k: pointer) -> int {
if (k == "w") { return 2 }; if (k == "h") { return 3 }; if (k == "x") { return 4 }; if (k == "y") { return 5 }
@ -102,7 +102,7 @@ function emit_ui_build() -> void {
falloc = buf_new()
let saved = code
code = fbody
emit(" call void @fn_rt_ui_reset(i32 "); emit(itoa(len(uiw))); emit(")\n")
emit(" call void " + fn_sym("rt_ui_reset") + "(i32 "); emit(itoa(len(uiw))); emit(")\n")
var i = 0
while i < len(uiw) {
let w = uiw[i]
@ -119,10 +119,10 @@ function emit_ui_build() -> void {
if (k == "id") or (k == "goto") { p += 1; continue }
if (k == "text") {
let sv = emit_expr(v)
emit(" call void @fn_rt_ui_static_text(i32 "); emit(itoa(i)); emit(", ptr "); emit(sv.code); emit(")\n")
emit(" call void " + fn_sym("rt_ui_static_text") + "(i32 "); emit(itoa(i)); emit(", ptr "); emit(sv.code); emit(")\n")
} else { if (k == "skin") or (k == "image") {
let sv = emit_expr(v)
let r = emit_bind(`call i32 @fn_rt_image_load(ptr {sv.code})`)
let r = emit_bind(`call i32 {fn_sym("rt_image_load")}(ptr {sv.code})`)
if (k == "skin") { ll_ui_set(i, 20, r) } else { ll_ui_set(i, 19, r) }
} else { if (k == "align") {
if v.kind == E_ID {

View file

@ -445,7 +445,7 @@ function emit_game_main() -> void {
emit("define i32 @main(i32 %argc, ptr %argv) {\nentry:\n")
emit(" store i32 %argc, ptr @L_argc\n")
emit(" store ptr %argv, ptr @L_argv\n")
if (find_fn("rt_init") != null) { emit(" call void @fn_rt_init()\n") }
if (find_fn("rt_init") != null) { emit(" call void " + fn_sym("rt_init") + "()\n") }
emit(" call void @L_init_globals()\n")
emit_calls_for_phase("Start")
# enter the start scene once, after boot: store its id and run its on-enter.
@ -463,7 +463,7 @@ function emit_game_main() -> void {
let r = emit_bind("load i32, ptr @L_running")
let rc = emit_bind(`icmp ne i32 {r}, 0`)
if (find_fn("rt_running") != null) {
let pr = emit_bind("call i32 @fn_rt_running()")
let pr = emit_bind("call i32 " + fn_sym("rt_running") + "()")
let pc = emit_bind(`icmp ne i32 {pr}, 0`)
let go = emit_bind(`and i1 {rc}, {pc}`)
emit(" br i1 "); emit(go); emit(", label %body, label %done\n")
@ -478,11 +478,11 @@ function emit_game_main() -> void {
# and the game no longer has to call Input.poll by hand. A game that uses no Input
# runtime keeps the plain rt_poll path, byte-identical.
if g_uses_input and (find_fn("input_drive") != null) {
let k = emit_bind("call i32 @fn_input_drive()")
let k = emit_bind("call i32 " + fn_sym("input_drive") + "()")
emit(" store i32 "); emit(k); emit(", ptr @L_key\n")
} else {
if (find_fn("rt_poll") != null) {
let k = emit_bind("call i32 @fn_rt_poll()")
let k = emit_bind("call i32 " + fn_sym("rt_poll") + "()")
emit(" store i32 "); emit(k); emit(", ptr @L_key\n")
}
}
@ -490,7 +490,7 @@ function emit_game_main() -> void {
# UiClicked on activation — so a scene never polls ui_tick by hand.
if has_ui() and (find_fn("rt_ui_tick") != null) {
let uk = emit_bind("load i32, ptr @L_key")
emit(" call void @fn_rt_ui_tick(i32 "); emit(uk); emit(")\n")
emit(" call void " + fn_sym("rt_ui_tick") + "(i32 "); emit(uk); emit(")\n")
}
emit_calls_for_phase("Input")
emit_calls_for_phase("FixedUpdate")
@ -503,14 +503,14 @@ function emit_game_main() -> void {
# system owns the present).
if g_has_clear_color and (find_fn("rt_clear") != null) {
let colour = emit_expr(g_clear_color) # a literal, a const, or a Color.Name
emit(" call void @fn_rt_clear(i32 "); emit(colour.code); emit(")\n")
emit(" call void " + fn_sym("rt_clear") + "(i32 "); emit(colour.code); emit(")\n")
}
emit_calls_for_phase("Render")
# Overlay: HUD / menus drawn after every engine Render system (sprites, lights), so
# game UI is never painted under an actor. Presented together with the frame.
emit_calls_for_phase("Overlay")
if g_has_clear_color and (find_fn("rt_present") != null) {
emit(" call void @fn_rt_present()\n")
emit(" call void " + fn_sym("rt_present") + "()\n")
}
let fcur = emit_bind("load i32, ptr @L_frame") # Time.frame(): count completed frames
let fnext = emit_bind(`add i32 {fcur}, 1`)
@ -520,6 +520,6 @@ function emit_game_main() -> void {
if len(g_ondespawn) > 0 { emit(" call void @L_despawn_all(i32 2)\n") } # LC1: every survivor's @OnDespawn fires with reason Quit
emit_calls_for_phase("OnQuit") # @OnQuit shutdown hooks run once, before teardown
if (find_event("program_quit") != null) { emit(" call void @ev_program_quit()\n") } # EV1: @Public @OnQuit
if (find_fn("rt_shutdown") != null) { emit(" call void @fn_rt_shutdown()\n") }
if (find_fn("rt_shutdown") != null) { emit(" call void " + fn_sym("rt_shutdown") + "()\n") }
emit(" ret i32 0\n}\n")
}

View file

@ -137,7 +137,7 @@ function emit_tween_ns(meth: pointer, e: Node) -> Val {
# a call into tween.ludic; the 2-arg Tween.done(timer, dur) is the pure form.
if (len(e.kids) == 1) {
let h = emit_expr(e.kids[0])
return val(emit_bind(`call i32 @fn_tween_done(i32 {h.code})`), "bool")
return val(emit_bind(`call i32 {fn_sym("tween_done")}(i32 {h.code})`), "bool")
}
let timer = emit_expr(e.kids[0]); let dur = emit_expr(e.kids[1]) # timer >= duration -> bool
let c = emit_bind(`icmp sge i32 {timer.code}, {dur.code}`)

View file

@ -56,17 +56,17 @@ function emit_ivec_ns(meth: pointer, e: Node) -> Val {
if (meth == "heading") { # degrees from a to b (0 = +x, 90 = +y)
if find_fn("rt_ivec_heading") == null { perr("IVec2.heading needs the engine runtime") }
let a = emit_expr(e.kids[0]); let b = emit_expr(e.kids[1])
return val(emit_bind(`call i32 @fn_rt_ivec_heading(i64 {a.code}, i64 {b.code})`), "int")
return val(emit_bind(`call i32 {fn_sym("rt_ivec_heading")}(i64 {a.code}, i64 {b.code})`), "int")
}
if (meth == "along") { # the point `distance` along `degrees` from origin
if find_fn("rt_ivec_along") == null { perr("IVec2.along needs the engine runtime") }
let o = emit_expr(e.kids[0]); let d = emit_expr(e.kids[1]); let n = emit_expr(e.kids[2])
return val(emit_bind(`call i64 @fn_rt_ivec_along(i64 {o.code}, i32 {d.code}, i32 {n.code})`), "IVec2")
return val(emit_bind(`call i64 {fn_sym("rt_ivec_along")}(i64 {o.code}, i32 {d.code}, i32 {n.code})`), "IVec2")
}
if (meth == "step") { # a -1/0/1 unit step along `degrees`
if find_fn("rt_ivec_step") == null { perr("IVec2.step needs the engine runtime") }
let d = emit_expr(e.kids[0])
return val(emit_bind(`call i64 @fn_rt_ivec_step(i32 {d.code})`), "IVec2")
return val(emit_bind(`call i64 {fn_sym("rt_ivec_step")}(i32 {d.code})`), "IVec2")
}
if (meth == "add") { # component-wise a + b
let a = emit_expr(e.kids[0]); let b = emit_expr(e.kids[1])

View file

@ -70,11 +70,11 @@ function emit_takeright(elt: pointer, mode: int, fnsym: pointer, keyll: pointer,
return emit_bind(`icmp slt {elt} {bv}, {av}`) # right strictly less than left
}
if (mode == SORT_WITH) {
let c = emit_bind(`call i32 @fn_{fnsym}({elt} {av}, {elt} {bv})`)
let c = emit_bind(`call i32 {fn_sym(fnsym)}({elt} {av}, {elt} {bv})`)
return emit_bind(`icmp sgt i32 {c}, 0`) # cmp(left, right) > 0 -> left comes after
}
let kl = emit_bind(`call {keyll} @fn_{fnsym}({elt} {av})`)
let kr = emit_bind(`call {keyll} @fn_{fnsym}({elt} {bv})`)
let kl = emit_bind(`call {keyll} {fn_sym(fnsym)}({elt} {av})`)
let kr = emit_bind(`call {keyll} {fn_sym(fnsym)}({elt} {bv})`)
if (mode == SORT_KEY_DESC) {
return emit_bind(`icmp slt {keyll} {kl}, {kr}`) # desc: right first when its key is larger
}
@ -232,7 +232,7 @@ function emit_list_ns(meth: pointer, e: Node) -> Val {
let pool = emit_expr(e.kids[0])
let cnt = emit_expr(e.kids[1])
if find_fn("rt_list_sample") == null { perr("List.sample needs the engine runtime (a game, or import ludic.core)") }
return val(emit_bind(`call ptr @fn_rt_list_sample(ptr {pool.code}, i32 {cnt.code})`), "[]int")
return val(emit_bind(`call ptr {fn_sym("rt_list_sample")}(ptr {pool.code}, i32 {cnt.code})`), "[]int")
}
let s = emit_expr(e.kids[0])
if not is_slice_ty(s.ty) { perr(`List.{meth} needs a slice`) }

View file

@ -14,6 +14,7 @@ function ck_params(f: Node, labels: []pointer, tys: []pointer) -> void {
}
function ck_call_fn(e: Node, name: pointer, f: Node) -> pointer {
state_inject_generated(e, f)
state_inject_runtime(e, f)
mg_call(e, f)
call_fill_defaults(e, f)
ck_state_args(e, f)

View file

@ -81,6 +81,16 @@ var g_mg_entry: pointer = "" # the program's own file: its vars are t
# the state a var's file moves into: its module's, or - a file in no module - the program's for the
# program's own file, else one named for the file's directory (a test's shared fakes)
function mg_state_of_file(f: pointer) -> int {
if is_runtime_file(f) { # the runtime: a state per file (rt_input: RtInputState)
var b = f
var i = 0
while i < len(f) {
if f[i] == '/' { b = f[i + 1 .. len(f)] }
i += 1
}
if len(b) > 6 and (b[len(b) - 6 .. len(b)] == ".ludic") { b = b[0 .. len(b) - 6] }
return mg_state_of_mod(`@{f}`, `rt_{b}`)
}
let m = module_for_uses(f)
if not (m == "") { return mg_state_of_mod(m, mg_short(m)) }
if (f == g_mg_entry) { # the program's own: SceneDemo is SceneDemoState, scene_demo_st
@ -301,6 +311,7 @@ var g_mc_passed: []int = new []int # 1: the call already hands the callee a sta
function mg_call(e: Node, f: Node) -> void {
if not g_migrate or f == null or g_mg_cur < 0 { return }
if e.pos < 0 { return } # a generated call gets its states when compiled
if is_runtime_file(f.file) and not is_runtime_file(e.file) { return } # the runtime's are supplied
push(g_mc_call, e)
push(g_mc_unit, g_mg_cur)
push(g_mc_callee, mg_unit(f, 0))

View file

@ -18,6 +18,11 @@ var g_state_names: []pointer = new []pointer
var g_allow_globals: bool = false
var g_gen_nodes: bool = false # the finishing passes are writing code: its nodes are generated
# a runtime function called from outside the runtime: a built-in, whose states are the runtime's
function is_runtime_file(f: pointer) -> bool { return f != null and has_sub(f, "runtime/native/") }
function state_inject_runtime(e: Node, f: Node) -> void {
if e != null and f != null and is_runtime_file(f.file) and not is_runtime_file(e.file) { state_inject(e, f) }
}
# a call the compiler wrote itself (a view's model, a component's glue): its callee's states are
# supplied - the runtime's side of an entry point
function state_inject_generated(e: Node, f: Node) -> void {
@ -117,6 +122,12 @@ function state_inject(e: Node, f: Node) -> void {
i += 1
}
if k == 0 or len(e.kids) + k > total { return }
var named = false # `f(x: 1)`: the states go in by name too
i = 0
while i < len(e.kids) {
if e.kids[i].kind == E_FINIT { named = true }
i += 1
}
let args = new []Node
i = 0
while i < k {
@ -124,7 +135,12 @@ function state_inject(e: Node, f: Node) -> void {
id.s = state_global(f.kids[i].ty)
id.file = e.file
id.line = e.line
push(args, id)
if named {
let fi = node(E_FINIT)
fi.s = f.kids[i].s
fi.a = id
push(args, fi)
} else { push(args, id) }
i += 1
}
i = 0

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff