selfhost: port switch — N_SWITCH parser + cgen + scratch slot

This commit is contained in:
2026-05-12 15:10:00 +09:00
parent ab0976571b
commit f67c07cbae
8 changed files with 502 additions and 0 deletions

View File

@@ -524,6 +524,37 @@ fn emitlabel(s: str) void = {
emitline(":\n");
};
// mkscratchname — fresh local-slot name ".<base>_<labelseq>". Used for
// compiler-synthesised slots (switch scrutinee, forrange index/len)
// that need to be unique per use site but are never referenced by user
// code. Increments labelseq so the same source position lines up with
// C cgen's labelseq stream.
fn mkscratchname(c: *cgen, base: str) str = {
let buf: [128]u8;
let i: i32 = 0;
buf[i] = 46u8; i += 1; // '.'
let j: i32 = 0;
for (j < base.len) {
buf[i] = base[j];
i += 1; j += 1;
};
buf[i] = 95u8; i += 1; // '_'
let n: i32 = strconv.i64tos(buf[i:128], c.labelseq: i64);
c.labelseq += 1;
let total: i32 = i + n;
let p: *u8 = amalloc(c.a, (total: u64) + 1u64): *u8;
let k: i32 = 0;
for (k < total) {
p[k] = buf[k];
k += 1;
};
p[total] = 0u8;
let r: str;
r.ptr = p;
r.len = total;
return r;
};
// ---- string interning ------------------------------------------------
//
// streq is provided by sym.ww and reused here.