Strings are values now: `a + b` concatenates and `a == b` / `a != b` compare by
content, replacing the 605 `sconcat(...)` / `streq(...)` calls that made the
compiler read like C.
streq(name, "let") -> name == "let"
sconcat("load ", reg) -> "load " + reg
sconcat(a, sconcat(b, c)) -> a + b + c
Implementation: emit_bin gains a string path. Strings are pointer-typed, so any
`+` with a pointer operand concatenates and `==`/`!=` between pointers compares
content — except when one side is the `null` literal, which stays a pointer
identity test (the only two kinds of pointer `==` in the codebase). Both call a
small hand-written IR prelude, @fn_str_eq / @fn_str_concat, emitted once into any
program that uses string ops (so it works for tools, games and the compiler with
no runtime-splice dependency and no duplicate symbols).
Delivered as two reseeds: (A) add the operators + prelude with the full
pointer-aware dispatch, keeping streq/sconcat; (B) migrate every call site
(354 lines, via a string-literal-safe balanced-paren script that leaves the
function definitions alone) and delete streq/sconcat. examples/strings.ludic +
a test.sh smoke (prints 1 2 3 4 5) guard it.
Reseeded (21890 lines); C-free fixpoint holds; goldens byte-identical; 18/18;
vocab + doc-fences clean.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
99 lines
4.3 KiB
Text
99 lines
4.3 KiB
Text
# emit_ecs.ludic — ECS storage and the entity allocator. For each component:
|
|
# a %Cmp_ layout, a dense @S_ store, and an @H_ "has this component" array.
|
|
# Entities are integer handles; L_alloc reuses freed slots. Mirrors the ECS
|
|
# parts of compiler/back/ir_decl.c.
|
|
|
|
const MAX_ENT: int = 1024
|
|
|
|
fn has_systems() -> bool {
|
|
var i = 0
|
|
while i < len(prog) { if prog[i].kind == N_SYS { return true }; i = i + 1 }
|
|
return false
|
|
}
|
|
fn has_models() -> bool {
|
|
var i = 0
|
|
while i < len(prog) { if prog[i].kind == N_ARCH { return true }; i = i + 1 }
|
|
return false
|
|
}
|
|
# Does this program run the ECS? A property alone no longer answers that — the
|
|
# same `property` keyword also declares plain `new`-allocated records (the merged
|
|
# `struct`). A program uses the ECS when it has a handler or a model; a tool that
|
|
# only declares record types and functions does not, and gets no entity storage,
|
|
# allocator, snapshot or runtime splice.
|
|
fn has_ecs() -> bool { return has_systems() or has_models() }
|
|
|
|
fn emit_ecs_storage() -> void {
|
|
emith("@L_running = internal global i32 1\n")
|
|
emith("@L_key = internal global i32 0\n")
|
|
emith("@L_entc = internal global i32 0\n")
|
|
let me = itoa(MAX_ENT)
|
|
emith(("@L_alive = internal global [" + (me + " x i32] zeroinitializer\n")))
|
|
emith(("@L_kind = internal global [" + (me + " x i32] zeroinitializer\n")))
|
|
emith(("@L_freelist = internal global [" + (me + " x i32] zeroinitializer\n")))
|
|
emith("@L_freen = internal global i32 0\n")
|
|
var i = 0
|
|
while i < len(prog) {
|
|
let c = prog[i]
|
|
# per-entity storage for a property (its %Cmp_ layout is emitted in the
|
|
# header). Every property in an ECS program is a component today; a property
|
|
# used only via `new` would not need these, but no such program mixes the two.
|
|
if c.kind == N_COMP {
|
|
emith(("@S_" + (c.s + (" = internal global [" + (me + ((" x %Cmp_") + (c.s + "] zeroinitializer\n")))))))
|
|
emith(("@H_" + (c.s + (" = internal global [" + (me + " x i8] zeroinitializer\n")))))
|
|
}
|
|
# one enabled-flag global per model and per handler (default enabled)
|
|
if c.kind == N_ARCH { emith(("@ME_" + (c.s + " = internal global i32 1\n"))) }
|
|
if c.kind == N_SYS { emith(("@HE_" + (c.s + " = internal global i32 1\n"))) }
|
|
i = i + 1
|
|
}
|
|
}
|
|
|
|
# L_reset(e): clear every has-flag and the archetype kind for entity e
|
|
fn emit_ecs_allocator() -> void {
|
|
let me = itoa(MAX_ENT)
|
|
emit("define void @L_reset(i32 %e) {\nentry:\n")
|
|
var i = 0
|
|
while i < len(prog) {
|
|
if prog[i].kind == N_COMP {
|
|
let hn = (("%h") + itoa(i))
|
|
emit(" "); emit(hn); emit(" = getelementptr inbounds ["); emit(me); emit(" x i8], ptr @H_"); emit(prog[i].s); emit(", i32 0, i32 %e\n")
|
|
emit(" store i8 0, ptr "); emit(hn); emit("\n")
|
|
}
|
|
i = i + 1
|
|
}
|
|
emit(" %k = getelementptr inbounds ["); emit(me); emit(" x i32], ptr @L_kind, i32 0, i32 %e\n")
|
|
emit(" store i32 0, ptr %k\n ret void\n}\n\n")
|
|
|
|
emit("define i32 @L_alloc() {\nentry:\n")
|
|
emit(" %fn = load i32, ptr @L_freen\n")
|
|
emit(" %has = icmp sgt i32 %fn, 0\n")
|
|
emit(" br i1 %has, label %reuse, label %fresh\n")
|
|
emit("reuse:\n")
|
|
emit(" %fn1 = sub i32 %fn, 1\n")
|
|
emit(" store i32 %fn1, ptr @L_freen\n")
|
|
emit(" %fp = getelementptr inbounds ["); emit(me); emit(" x i32], ptr @L_freelist, i32 0, i32 %fn1\n")
|
|
emit(" %re = load i32, ptr %fp\n")
|
|
emit(" br label %done\n")
|
|
emit("fresh:\n")
|
|
emit(" %ec = load i32, ptr @L_entc\n")
|
|
emit(" %ec1 = add i32 %ec, 1\n")
|
|
emit(" store i32 %ec1, ptr @L_entc\n")
|
|
emit(" br label %done\n")
|
|
emit("done:\n")
|
|
emit(" %e = phi i32 [ %re, %reuse ], [ %ec, %fresh ]\n")
|
|
emit(" call void @L_reset(i32 %e)\n")
|
|
emit(" %ap = getelementptr inbounds ["); emit(me); emit(" x i32], ptr @L_alive, i32 0, i32 %e\n")
|
|
emit(" store i32 1, ptr %ap\n")
|
|
emit(" ret i32 %e\n}\n\n")
|
|
|
|
emit("define void @L_free_entity(i32 %e) {\nentry:\n")
|
|
emit(" %ap = getelementptr inbounds ["); emit(me); emit(" x i32], ptr @L_alive, i32 0, i32 %e\n")
|
|
emit(" store i32 0, ptr %ap\n")
|
|
emit(" call void @L_reset(i32 %e)\n")
|
|
emit(" %fn = load i32, ptr @L_freen\n")
|
|
emit(" %fp = getelementptr inbounds ["); emit(me); emit(" x i32], ptr @L_freelist, i32 0, i32 %fn\n")
|
|
emit(" store i32 %e, ptr %fp\n")
|
|
emit(" %fn1 = add i32 %fn, 1\n")
|
|
emit(" store i32 %fn1, ptr @L_freen\n")
|
|
emit(" ret void\n}\n\n")
|
|
}
|