selfhost: drop snake_case locals in dyn/dynout/obj + w6a + cgen + ww driver

This commit is contained in:
2026-05-11 16:33:02 +09:00
parent c64e96cbd6
commit 97ca76d2bb
18 changed files with 1795 additions and 1795 deletions

View File

@@ -115,7 +115,7 @@ type fieldinfo = struct {
type structinfo = struct {
sname: str,
fields: *fieldinfo,
tot_size: i32,
totsize: i32,
sinext: *structinfo,
};
@@ -150,9 +150,9 @@ type cgen = struct {
a: *arena,
locals: *local,
frame: i32,
last_was_return: i32,
lastwasreturn: i32,
labelseq: i32,
strlit_seq: i32,
strlitseq: i32,
strlits: *strlit,
ffis: *ffi,
defs: *defent,
@@ -160,25 +160,25 @@ type cgen = struct {
aliases: *aliasent,
structs: *structinfo,
mods: *modent, // non-exported decls → originating module
fn_name: str,
fn_ret: *node, // declared return type of current fn (or nil)
loop_top: i32,
loop_end_buf: *str, // stack of end labels for break
loop_cont_buf: *str, // stack of cont labels for continue
fnname: str,
fnret: *node, // declared return type of current fn (or nil)
looptop: i32,
loopendbuf: *str, // stack of end labels for break
loopcontbuf: *str, // stack of cont labels for continue
};
fn cgeninit(c: *cgen, a: *arena) void = {
c.a = a;
c.locals = nil;
c.frame = 0;
c.last_was_return = 0;
c.lastwasreturn = 0;
c.labelseq = 0;
// Note: strlit_seq, strlits, ffis are *not* reset here; they
// persist across cgfn calls within one file. cgfile resets them
// at the start of each compilation unit.
c.loop_top = 0;
c.loop_end_buf = amalloc(a, (LOOP_MAX: u64) * 16u64): *str;
c.loop_cont_buf = amalloc(a, (LOOP_MAX: u64) * 16u64): *str;
c.looptop = 0;
c.loopendbuf = amalloc(a, (LOOP_MAX: u64) * 16u64): *str;
c.loopcontbuf = amalloc(a, (LOOP_MAX: u64) * 16u64): *str;
};
// localalloc — append a slot for `name` without dedup. Used for
@@ -310,7 +310,7 @@ fn emitoff(v: i64) void = {
fn mklabel(c: *cgen, base: str) str = {
let buf: [128]u8;
let i: i32 = 0;
let fname: str = c.fn_name;
let fname: str = c.fnname;
let j: i32 = 0;
for (j < fname.len) {
buf[i] = fname[j];
@@ -362,8 +362,8 @@ fn internstrlit(c: *cgen, bytes: str) str = {
// New label "_S_<seq>".
let buf: [32]u8;
buf[0] = 95u8; buf[1] = 83u8; buf[2] = 95u8; // "_S_"
let n: i32 = strconv.i64toa(buf[3:32], c.strlit_seq: i64);
c.strlit_seq += 1;
let n: i32 = strconv.i64toa(buf[3:32], c.strlitseq: i64);
c.strlitseq += 1;
let total: i32 = 3 + n;
let p: *u8 = amalloc(c.a, (total: u64) + 1u64): *u8;
let i: i32 = 0;

View File

@@ -154,8 +154,8 @@ fn cgfnparams(c: *cgen, params: *node) void = {
fn cgfn(c: *cgen, fn_: *node) void = {
cgeninit(c, c.a);
c.fn_name = fn_.str;
c.fn_ret = fn_.lhs;
c.fnname = fn_.str;
c.fnret = fn_.lhs;
emitline("TEXT ");
if (fn_.exported == 0) {
@@ -212,10 +212,10 @@ fn cgfn(c: *cgen, fn_: *node) void = {
emitline(", SP\n");
cgfnparams(c, fn_.list);
c.last_was_return = 0;
c.lastwasreturn = 0;
if (fn_.body != nil) { cgstmt(c, fn_.body); };
if (c.last_was_return == 0) {
if (c.lastwasreturn == 0) {
// Zero AX before the fall-through return — matches C cgen,
// which always emits this so void-returning fns don't leak
// a stale callee value to their caller.
@@ -231,7 +231,7 @@ fn cgfn(c: *cgen, fn_: *node) void = {
export fn cgfile(c: *cgen, file: *node) void = {
if (file == nil) { return; };
c.strlits = nil;
c.strlit_seq = 0;
c.strlitseq = 0;
collectaliases(c, file);
collectstructs(c, file);
collectdefs(c, file);

View File

@@ -6,7 +6,7 @@
// (N_INTLIT, N_RUNELIT, N_TRUE/FALSE/NIL, N_CAST) stay inline.
//
// The remainder of cgen lives in cgen.ww (foundation: types, emit
// primitives, the collect* tables, FFI/module maps) and cgen_stmt.ww
// primitives, the collect* tables, FFI/module maps) and cgenstmt.ww
// (cgstmt).
//
// `use cgenexpr;` is unnecessary at consumer sites — cgen.ww imports

View File

@@ -4,7 +4,7 @@
// per-kind helper: cgblock, cgreturn, cgexprstmt, cglet, cgif, cgfor,
// cgmassign, cgbreak, cgcontinue.
//
// The expression generator (cgexpr) lives in cgen_expr.ww; the
// The expression generator (cgexpr) lives in cgenexpr.ww; the
// foundation (types, emit primitives, collect* tables, FFI/module
// maps) lives in cgen.ww.
@@ -39,7 +39,7 @@ fn cgstmt(c: *cgen, n: *node) void = {
if (k == N_BREAK) { cgbreak(c, n); return; };
if (k == N_CONTINUE) { cgcontinue(c, n); return; };
c.last_was_return = 0;
c.lastwasreturn = 0;
};
fn cgblock(c: *cgen, n: *node) void = {
@@ -73,16 +73,16 @@ fn cgreturn(c: *cgen, n: *node) void = {
emitline("\tMOVQ\tBP, SP\n");
emitline("\tPOPQ\tBP\n");
emitline("\tRET\n");
c.last_was_return = 1;
c.lastwasreturn = 1;
return;
};
// Tagged-union return: pack as (AX=tag, DX=value0, CX=value1).
// For str variant, cgexpr leaves (AX=ptr, BX=len), so we
// shuffle DX←AX (ptr) and CX←BX (len), then load tag.
// For other variants, cgexpr leaves AX, shuffle DX←AX.
if (istaggedtype(c.fn_ret)) {
if (istaggedtype(c.fnret)) {
cgexpr(c, rhs);
let idx: i32 = taggedvariantindex(c, c.fn_ret, rhs);
let idx: i32 = taggedvariantindex(c, c.fnret, rhs);
if (nodeisstr(c, rhs)) {
emitline("\tMOVQ\tBX, CX\n");
emitline("\tMOVQ\tAX, DX\n");
@@ -96,7 +96,7 @@ fn cgreturn(c: *cgen, n: *node) void = {
emitline("\tMOVQ\tBP, SP\n");
emitline("\tPOPQ\tBP\n");
emitline("\tRET\n");
c.last_was_return = 1;
c.lastwasreturn = 1;
return;
};
cgexpr(c, rhs);
@@ -108,19 +108,19 @@ fn cgreturn(c: *cgen, n: *node) void = {
};
// SysV: 16-byte aggregates (str, 2-tuple) return in (AX, DX).
// cgexpr leaves str in (AX, BX); shuffle BX→DX.
if (isstrtype(c, c.fn_ret)) {
if (isstrtype(c, c.fnret)) {
emitline("\tMOVQ\tBX, DX\n");
};
emitline("\tMOVQ\tBP, SP\n");
emitline("\tPOPQ\tBP\n");
emitline("\tRET\n");
c.last_was_return = 1;
c.lastwasreturn = 1;
return;
};
fn cgexprstmt(c: *cgen, n: *node) void = {
if (n.lhs != nil) { cgexpr(c, n.lhs); };
c.last_was_return = 0;
c.lastwasreturn = 0;
return;
};
@@ -162,7 +162,7 @@ fn cglet(c: *cgen, n: *node) void = {
emitline("\tMOVQ\tCX, ");
emitoff((off + 16): i64);
emitline("(BP)\n");
c.last_was_return = 0;
c.lastwasreturn = 0;
return;
};
let tagidx: i32 = taggedvariantindex(c, n.lhs, rhs);
@@ -184,7 +184,7 @@ fn cglet(c: *cgen, n: *node) void = {
emitline(", ");
emitoff(off: i64);
emitline("(BP)\n");
c.last_was_return = 0;
c.lastwasreturn = 0;
return;
};
// Struct literal init: `let p: point = point{x=..., y=...};`.
@@ -224,7 +224,7 @@ fn cglet(c: *cgen, n: *node) void = {
};
fieldnode = fieldnode.next;
};
c.last_was_return = 0;
c.lastwasreturn = 0;
return;
};
};
@@ -262,7 +262,7 @@ fn cglet(c: *cgen, n: *node) void = {
emitline("(BP)\n");
};
};
c.last_was_return = 0;
c.lastwasreturn = 0;
return;
};
@@ -282,7 +282,7 @@ fn cgif(c: *cgen, n: *node) void = {
cgstmt(c, n.els);
};
emitlabel(endl);
c.last_was_return = 0;
c.lastwasreturn = 0;
return;
};
@@ -302,18 +302,18 @@ fn cgfor(c: *cgen, n: *node) void = {
emitline("\tJE\t"); emitline(endl); emitline("\n");
};
c.loop_end_buf[c.loop_top] = endl;
c.loop_cont_buf[c.loop_top] = topl;
c.loop_top += 1;
c.loopendbuf[c.looptop] = endl;
c.loopcontbuf[c.looptop] = topl;
c.looptop += 1;
if (n.body != nil) { cgstmt(c, n.body); };
c.loop_top -= 1;
c.looptop -= 1;
if (n.rhs != nil) { cgexpr(c, n.rhs); };
emitline("\tJMP\t"); emitline(topl); emitline("\n");
emitlabel(endl);
c.last_was_return = 0;
c.lastwasreturn = 0;
return;
};
@@ -349,25 +349,25 @@ fn cgmassign(c: *cgen, n: *node) void = {
};
};
};
c.last_was_return = 0;
c.lastwasreturn = 0;
return;
};
fn cgbreak(c: *cgen, n: *node) void = {
if (c.loop_top > 0) {
let lbl: str = c.loop_end_buf[c.loop_top - 1];
if (c.looptop > 0) {
let lbl: str = c.loopendbuf[c.looptop - 1];
emitline("\tJMP\t"); emitline(lbl); emitline("\n");
};
c.last_was_return = 0;
c.lastwasreturn = 0;
return;
};
fn cgcontinue(c: *cgen, n: *node) void = {
if (c.loop_top > 0) {
let lbl: str = c.loop_cont_buf[c.loop_top - 1];
if (c.looptop > 0) {
let lbl: str = c.loopcontbuf[c.looptop - 1];
emitline("\tJMP\t"); emitline(lbl); emitline("\n");
};
c.last_was_return = 0;
c.lastwasreturn = 0;
return;
};

View File

@@ -1,6 +1,6 @@
// selfhost/cmd/wcc/cgenutil.ww — split out of cgen.ww.
//
// General helpers used across cgen_expr / cgen_stmt / cgen_decl:
// General helpers used across cgenexpr / cgenstmt / cgendecl:
// - pushargsrev: per-call arg pushing
// - type predicates: isstr*/isslice*/istagged*/nodeis* families
// - field ops: fieldloadop, fieldstoreop
@@ -619,9 +619,9 @@ fn primsize(name: str) i32 = {
return 0;
};
fn slotsize(c: *cgen, typ_n: *node) i32 = {
if (typ_n == nil) { return 8; };
let k: i32 = typ_n.kind;
fn slotsize(c: *cgen, typn: *node) i32 = {
if (typn == nil) { return 8; };
let k: i32 = typn.kind;
if (k == N_TPTR) { return 8; };
if (k == N_TFN) { return 8; };
if (k == N_TCHAN) { return 8; };
@@ -629,7 +629,7 @@ fn slotsize(c: *cgen, typ_n: *node) i32 = {
if (k == N_TTUPLE) { return 16; };
if (k == N_TTAGGED){ return 24; };
if (k == N_TNAME) {
let nm: str = typ_n.str;
let nm: str = typn.str;
if (streq(nm, "str")) { return 16; };
let ps: i32 = primsize(nm);
if (ps > 0) {
@@ -639,12 +639,12 @@ fn slotsize(c: *cgen, typ_n: *node) i32 = {
};
// Named struct lookup.
let si: *structinfo = structlookup(c, nm);
if (si != nil) { return si.tot_size; };
if (si != nil) { return si.totsize; };
return 8;
};
if (k == N_TARRAY) {
let lenn: *node = typ_n.rhs;
let elemn: *node = typ_n.lhs;
let lenn: *node = typn.rhs;
let elemn: *node = typn.lhs;
let elen: i64 = 1i64;
if (lenn != nil) {
if (lenn.kind == N_INTLIT) { elen = lenn.uval: i64; };
@@ -661,7 +661,7 @@ fn slotsize(c: *cgen, typ_n: *node) i32 = {
};
if (k == N_TSTRUCT) {
// Inline anonymous struct — sum of field sizes.
let f: *node = typ_n.list;
let f: *node = typn.list;
let total: i32 = 0;
for (f != nil) {
if (f.kind == N_TFIELD) {
@@ -687,7 +687,7 @@ fn fieldsize(c: *cgen, tnode: *node) i32 = {
let ps: i32 = primsize(nm);
if (ps > 0) { return ps; };
let si: *structinfo = structlookup(c, nm);
if (si != nil) { return si.tot_size; };
if (si != nil) { return si.totsize; };
return 8;
};
if (k == N_TPTR) { return 8; };
@@ -710,7 +710,7 @@ fn registerstruct(c: *cgen, name: str, tstruct: *node) void = {
let si: *structinfo = amalloc(c.a, 64u64): *structinfo;
si.sname = name;
si.fields = nil;
si.tot_size = 0;
si.totsize = 0;
let head: *fieldinfo = nil;
let tail: *fieldinfo = nil;
let off: i32 = 0;
@@ -742,7 +742,7 @@ fn registerstruct(c: *cgen, name: str, tstruct: *node) void = {
// Round total to 8 for stack-slot use.
if ((off & 7) != 0) { off = (off + 7) & ~7; };
si.fields = head;
si.tot_size = off;
si.totsize = off;
si.sinext = c.structs;
c.structs = si;
};