The parser now requires a newline or ';' between statements (block() in selfhost/parse.ludic); two statements may no longer sit adjacent with only spaces. Also fixed if-without-else swallowing its trailing separator. Migration: tools/ludic-tools/migrate_separators.c inserts ';' at statement boundaries corpus-wide (examples, runtime, 25 self-host fragments, ~1100 boundaries). Verified semantically identical — the migrated compiler compiles itself to IR byte-identical to the pre-migration seed, and every golden game renders identically. Reseeded to the strict compiler; C-free fixpoint holds; test.sh 14/14. Docs: Rule B documented in LANGUAGE.md; BOOTSTRAP.md R1 + stale fences updated; check-docs green across all docs. Fixed a multi-line string literal in emit_expr.ludic (byte-identical \n escape) that the C toolchain lexer mis-lexes. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
92 lines
3.7 KiB
Text
92 lines
3.7 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_ecs() -> bool {
|
|
let i = 0
|
|
while i < len(prog) { let k = prog[i].kind; if k == N_COMP or k == N_SYS { return true }; i = i + 1 }
|
|
return false
|
|
}
|
|
fn has_systems() -> bool {
|
|
let i = 0
|
|
while i < len(prog) { if prog[i].kind == N_SYS { return true }; i = i + 1 }
|
|
return false
|
|
}
|
|
|
|
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(sconcat("@L_alive = internal global [", sconcat(me, " x i32] zeroinitializer\n")))
|
|
emith(sconcat("@L_kind = internal global [", sconcat(me, " x i32] zeroinitializer\n")))
|
|
emith(sconcat("@L_freelist = internal global [", sconcat(me, " x i32] zeroinitializer\n")))
|
|
emith("@L_freen = internal global i32 0\n")
|
|
let i = 0
|
|
while i < len(prog) {
|
|
let c = prog[i]
|
|
if c.kind == N_COMP {
|
|
emith(sconcat("%Cmp_", sconcat(c.s, " = type { ")))
|
|
if len(c.kids) == 0 { emith("i32") }
|
|
let f = 0
|
|
while f < len(c.kids) { if f > 0 { emith(", ") }; emith(llty(c.kids[f].ty)); f = f + 1 }
|
|
emith(" }\n")
|
|
emith(sconcat("@S_", sconcat(c.s, sconcat(" = internal global [", sconcat(me, sconcat(" x %Cmp_", sconcat(c.s, "] zeroinitializer\n")))))))
|
|
emith(sconcat("@H_", sconcat(c.s, sconcat(" = internal global [", sconcat(me, " x i8] zeroinitializer\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")
|
|
let i = 0
|
|
while i < len(prog) {
|
|
if prog[i].kind == N_COMP {
|
|
let hn = sconcat("%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")
|
|
}
|