- `[a, b, c]` list literals (E_LIST → emit_list); static_type learns slice-element, `new T`, list, string and literal kinds - `x op= y` lowers through the same path as `x = x op y` (emit_bin_vals): fixed `*=`/`/=` use the Q16.16 64-bit paths, string `+=` concatenates, int→long widens; unary `-` keeps a fixed operand's type (arith_ty) - one `unescape()` table for "strings", 'chars' and `interpolation`; `'\''`, `'\\'`, `'\"'` no longer read as 0; unterminated char literals and unexpected characters are errors instead of silently skipped - every diagnostic is `file:line: error: msg` (g_parse_file / g_err_file, Node.file + Node.line set by node()); tok_desc() in expectation errors; duplicate `function` names and unknown `phase` names are reported in source terms (phase_id used to default unknown phases to Overlay) - interpolation holes skip braces inside string literals - hand-IR preludes move from the user `@fn_` prefix to `@lp_` so a user `is_ws` / `str_eq` / `path_join` no longer collides at link time - `@ClearColor(expr)` accepts any constant expression; `Os.pid()` added (docs page + inventory); `str_starts()` in support/str - main.ludic: `else if` flag ladder, char literals, stale script comments - examples/lang/operators.ludic covers all of the above; os.ludic covers Os.pid; docs pages for Os.pid and the Overlay phase; ten changesets - reseeded: selfhost/ludicc.seed.ll is the new compiler's own fixpoint Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
127 lines
5.9 KiB
Text
127 lines
5.9 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"
|
|
let me = itoa(MAX_ENT)
|
|
emit_io(fn2, "@L_entc", "4")
|
|
emit_io(fn2, "@L_freen", "4")
|
|
emit(" %nalive = mul i64 "); emit(me); emit(", 4\n")
|
|
emit_io(fn2, "@L_alive", "%nalive")
|
|
emit_io(fn2, "@L_freelist", "%nalive")
|
|
emit_io(fn2, "@L_kind", "%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, "@L_owner_arr", "%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 csz = `%csz{itoa(ci)}`
|
|
emit(" "); emit(csz); emit(" = ptrtoint ptr getelementptr (%Cmp_"); emit(c); emit(", ptr null, i32 "); emit(me); emit(") to i64\n")
|
|
emit_io(fn2, `@S_{c}`, csz)
|
|
emit_io(fn2, `@H_{c}`, me)
|
|
ci += 1
|
|
}
|
|
i += 1
|
|
}
|
|
}
|
|
|
|
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_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_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"
|
|
}
|