Every per-entity store (@S_ components, @H_ flags, alive, kind, freelist, owners) is a heap block
L_grow doubles from 1024 as L_alloc hands out a slot past it, the new slots zeroed; each site loads
the store's base where it indexes it (ecs_base, its registers %ecsb* so a raw function's t0 labels
cannot collide). Prop.has bounds against @L_cap, Pool.capacity answers it, a mod's registered
stores grow with the rest, every main grows the stores once before anything reads them. A snapshot
records its slot count first and a load grows to it before reading back. The overflow stop of
1c7ce84 is gone with the wall. ludic-dev test 305 passed, selfhost-test 33 passed; 1000 / 5000 /
100000 entities spawn and count.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
143 lines
6.7 KiB
Text
143 lines
6.7 KiB
Text
# emit_save.ludic — save() / load() snapshot of the whole ECS world. The
|
|
# compiler writes the entity/component half itself (it knows the shape) and
|
|
# hands the open file to rt_save_state/rt_load_state for the runtime's own
|
|
# state. Mirrors compiler/back/ir_save.c. save()->@L_save, load()->@L_load.
|
|
|
|
var g_iok: int = 0
|
|
|
|
# The snapshot is a fixed sequence of (region, byte-length) blocks; the same list
|
|
# feeds two targets — a file (save/load via fwrite/fread) and a memory buffer
|
|
# (world_save/world_load via memcpy, NETWORKING-DESIGN §5 N1). g_snap_mode picks
|
|
# which; buffer modes thread a running i64 offset (@g_off) so world_save returns
|
|
# the total byte count and world_load reads the identical layout back.
|
|
# NOTE: a string initializer on a module `ptr` var lowers to null (global_init),
|
|
# so these are seeded at runtime in emit_snapshot before first use — never read
|
|
# them uninitialized (a null string `==` would deref and crash the compiler).
|
|
var g_snap_mode: pointer = null # "file" | "save" | "load" | "size"
|
|
var g_off: pointer = null # current byte-offset register, buffer modes
|
|
|
|
function emit_io(fn2: pointer, p: pointer, bytes: pointer) -> void {
|
|
if (g_snap_mode == "file") {
|
|
let r = `%io{itoa(g_iok)}`; g_iok += 1
|
|
emit(" "); emit(r); emit(" = call i64 @"); emit(fn2); emit("(ptr "); emit(p); emit(", i64 1, i64 "); emit(bytes); emit(", ptr %f)\n")
|
|
return
|
|
}
|
|
if (g_snap_mode == "size") { # accumulate the offset only, no copy
|
|
let noff = `%ioff{itoa(g_iok)}`
|
|
emit(" "); emit(noff); emit(" = add i64 "); emit(g_off); emit(", "); emit(bytes); emit("\n")
|
|
g_off = noff
|
|
g_iok += 1
|
|
return
|
|
}
|
|
# buffer mode: dst/src is %buf + g_off, copy `bytes`, then advance the cursor
|
|
let addr = `%ioa{itoa(g_iok)}`
|
|
emit(" "); emit(addr); emit(" = getelementptr inbounds i8, ptr %buf, i64 "); emit(g_off); emit("\n")
|
|
if (g_snap_mode == "save") {
|
|
emit(" call ptr @memcpy(ptr "); emit(addr); emit(", ptr "); emit(p); emit(", i64 "); emit(bytes); emit(")\n")
|
|
} else {
|
|
emit(" call ptr @memcpy(ptr "); emit(p); emit(", ptr "); emit(addr); emit(", i64 "); emit(bytes); emit(")\n")
|
|
}
|
|
let noff = `%ioff{itoa(g_iok)}`
|
|
emit(" "); emit(noff); emit(" = add i64 "); emit(g_off); emit(", "); emit(bytes); emit("\n")
|
|
g_off = noff
|
|
g_iok += 1
|
|
}
|
|
|
|
function emit_snapshot_blocks(fn2: pointer) -> void {
|
|
g_iok = 0
|
|
g_off = "0"
|
|
# the stores grow, so a snapshot says how many slots it holds first; a load grows to it before
|
|
# reading them back
|
|
let reading = (fn2 == "fread") or (g_snap_mode == "load")
|
|
if not reading { emit(" %sncap0 = load i32, ptr @L_cap\n store i32 %sncap0, ptr @L_snapcap\n") }
|
|
emit_io(fn2, "@L_snapcap", "4")
|
|
emit(" %sncap = load i32, ptr @L_snapcap\n")
|
|
if reading { emit(" call void @L_grow(i32 %sncap)\n") }
|
|
emit(" %sncap64 = sext i32 %sncap to i64\n")
|
|
emit_io(fn2, "@L_entc", "4")
|
|
emit_io(fn2, "@L_freen", "4")
|
|
emit(" %nalive = mul i64 %sncap64, 4\n")
|
|
emit_io(fn2, snap_base("L_alive", 0), "%nalive")
|
|
emit_io(fn2, snap_base("L_freelist", 1), "%nalive")
|
|
emit_io(fn2, snap_base("L_kind", 2), "%nalive")
|
|
# N3: an @Owned world snapshots its per-entity owners too, so rollback/replication
|
|
# round-trips ownership (like @L_kind). Gated, so non-@Owned snapshots are unchanged.
|
|
if net_has_owned() { emit_io(fn2, snap_base("L_owner_arr", 3), "%nalive") }
|
|
var i = 0
|
|
while i < len(prog) { if prog[i].kind == N_VAR { emit_io(fn2, `@g_{prog[i].s}`, "4") }; i += 1 }
|
|
var ci = 0
|
|
i = 0
|
|
while i < len(prog) {
|
|
if prog[i].kind == N_COMP {
|
|
let c = prog[i].s
|
|
let csz1 = `%csz1_{itoa(ci)}`
|
|
let csz = `%csz{itoa(ci)}`
|
|
emit(" "); emit(csz1); emit(" = ptrtoint ptr getelementptr (%Cmp_"); emit(c); emit(", ptr null, i32 1) to i64\n")
|
|
emit(" "); emit(csz); emit(" = mul i64 %sncap64, "); emit(csz1); emit("\n")
|
|
emit_io(fn2, snap_base(`S_{c}`, 4 + ci * 2), csz)
|
|
emit_io(fn2, snap_base(`H_{c}`, 5 + ci * 2), "%sncap64")
|
|
ci += 1
|
|
}
|
|
i += 1
|
|
}
|
|
}
|
|
|
|
# a store's base, loaded after any grow the snapshot made
|
|
function snap_base(store: pointer, k: int) -> pointer {
|
|
let r = `%snb{itoa(k)}`
|
|
emit(` {r} = load ptr, ptr @{store}\n`)
|
|
return r
|
|
}
|
|
|
|
function emit_snapshot() -> void {
|
|
g_snap_mode = "file" # seed (module ptr inits are null)
|
|
emith("@.sav_path = private unnamed_addr constant [10 x i8] c\"ludic.sav\\00\"\n")
|
|
emith("@.sav_wb = private unnamed_addr constant [3 x i8] c\"wb\\00\"\n")
|
|
emith("@.sav_rb = private unnamed_addr constant [3 x i8] c\"rb\\00\"\n")
|
|
let has_save = (find_fn("rt_save_state") != null)
|
|
let has_load = (find_fn("rt_load_state") != null)
|
|
|
|
emit("define void @L_save() {\nentry:\n")
|
|
emit(" %f = call ptr @fopen(ptr @.sav_path, ptr @.sav_wb)\n")
|
|
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_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")
|
|
emit(" %f = call ptr @fopen(ptr @.sav_path, ptr @.sav_rb)\n")
|
|
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_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,
|
|
# to a caller-owned memory buffer instead of a file (NETWORKING-DESIGN §5 N1) —
|
|
# the rollback/replication substrate. No rt_ hook: this is the ECS world only
|
|
# (entities, components, vars), which is what a peer replicates or a rollback
|
|
# restores; the runtime's windowing state stays local. world_save returns the
|
|
# byte count written; the caller sizes the buffer with world_size().
|
|
emit("define i32 @L_world_save(ptr %buf) {\nentry:\n")
|
|
g_snap_mode = "save"
|
|
emit_snapshot_blocks("")
|
|
let sret = `%wsn{itoa(g_iok)}`
|
|
emit(" "); emit(sret); emit(" = trunc i64 "); emit(g_off); emit(" to i32\n")
|
|
emit(" ret i32 "); emit(sret); emit("\n}\n\n")
|
|
|
|
emit("define void @L_world_load(ptr %buf, i32 %len) {\nentry:\n")
|
|
g_snap_mode = "load"
|
|
emit_snapshot_blocks("")
|
|
emit(" ret void\n}\n\n")
|
|
|
|
# world_size() -> int: the exact byte count a full snapshot needs, so a caller
|
|
# can size the buffer before world_save. Same block walk, offset-only.
|
|
emit("define i32 @L_world_size() {\nentry:\n")
|
|
g_snap_mode = "size"
|
|
emit_snapshot_blocks("")
|
|
let zret = `%wzn{itoa(g_iok)}`
|
|
emit(" "); emit(zret); emit(" = trunc i64 "); emit(g_off); emit(" to i32\n")
|
|
emit(" ret i32 "); emit(zret); emit("\n}\n\n")
|
|
g_snap_mode = "file"
|
|
}
|