frame allocs (25.2): @frame on a step list's field, @alloc_ok on a statement, and on an exported function

A field declared '@frame run: fn(...)' makes every function stored in it a frame root, as a System's
tick is. @alloc_ok("why") before a statement takes that statement out of frame_allocs and makes what
it allocates declared at run time; a function holding one keeps the fence's scope depth and puts it
back at its return, so a return inside the statement cannot leave the scope open. @alloc_ok above
'export function' was lost - export parses the declaration one call down - and is now carried to it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-28 15:56:32 +03:00
parent b35409254e
commit 1e2a6bab5b
7 changed files with 75754 additions and 74774 deletions

View file

@ -34,10 +34,13 @@ function emit_fn(d: Node) -> void {
}
let declared = fr_alloc_ok(d.s) # 25.1: what an @alloc_ok function makes, callees too, is declared
if declared { emit(" %decl.in = atomicrmw add ptr @lp_fdecl, i32 1 monotonic\n") }
let keeps = fr_has_aok(d.a) # a statement under @alloc_ok: a return inside it restores the depth
if keeps { emit(" %decl.keep = load i32, ptr @lp_fdecl\n") }
emit_block(d.a)
if not (rl == "void") and not block_ends(d.a) { perr(`function '{d.s}' can reach its end without returning a {ret_ty}`) }
if not g_term { emit(" br label %ret\n") }
emit("ret:\n")
if keeps { emit(" store i32 %decl.keep, ptr @lp_fdecl\n") }
if declared { emit(" %decl.out = atomicrmw sub ptr @lp_fdecl, i32 1 monotonic\n") }
if (rl == "void") { emit(" ret void\n") }
else { let r = emit_bind(`load {rl}, ptr %retval`); emit(" ret "); emit(rl); emit(" "); emit(r); emit("\n") }

View file

@ -7,6 +7,26 @@
var g_alloc_ok_names: []pointer = new []pointer # functions and handlers under @alloc_ok("why")
var g_frame_fields: []pointer = new []pointer # record fields declared `@frame`
var g_aok_nodes: []Node = new []Node # statements under @alloc_ok("why")
var g_aok_pending: bool = false # @alloc_ok read, its function or handler not yet
function fr_aok_stmt(n: Node) -> bool {
var i = 0
while i < len(g_aok_nodes) { if g_aok_nodes[i] == n { return true }; i += 1 }
return false
}
# does this body hold a statement under @alloc_ok? (its function then keeps the scope's depth)
function fr_has_aok(n: Node) -> bool {
if n == null or len(g_aok_nodes) == 0 { return false }
if fr_aok_stmt(n) { return true }
if fr_has_aok(n.a) or fr_has_aok(n.b) or fr_has_aok(n.c) { return true }
if n.kids != null {
var i = 0
while i < len(n.kids) { if fr_has_aok(n.kids[i]) { return true }; i += 1 }
}
return false
}
var g_fr_root: []bool = new []bool
var g_fr_from: []int = new []int # the node a reached one was first reached from
@ -76,6 +96,7 @@ function fr_alloc_kind(n: Node, text_above: bool) -> pointer {
# the allocating constructs in one declaration, each a falloc line with its chain
function fr_allocs(f: pointer, n: Node, k: int, text_above: bool) -> int {
if n == null { return 0 }
if fr_aok_stmt(n) { return 0 }
var found = 0
let kind = fr_alloc_kind(n, text_above)
if kind != null {

View file

@ -356,6 +356,24 @@ function emit_emit(st: Node) -> Val {
}
function emit_stmt(st: Node) -> void {
if fr_aok_stmt(st) { # 25.1: @alloc_ok("why") on this statement: its allocations are declared
emit(" %decl.s")
emit(itoa(ll_t))
emit(" = atomicrmw add ptr @lp_fdecl, i32 1 monotonic\n")
ll_t += 1
emit_stmt_body(st)
if not g_term {
emit(" %decl.e")
emit(itoa(ll_t))
emit(" = atomicrmw sub ptr @lp_fdecl, i32 1 monotonic\n")
ll_t += 1
}
return
}
emit_stmt_body(st)
}
function emit_stmt_body(st: Node) -> void {
g_err_file = st.file; g_err_line = st.line # so a lowering error names its statement
emit_cov_hit(st.line) # --coverage: bump this line's hit counter (no-op otherwise)
if st.kind == S_LET {