wcc/cgen: handle def-str field access (.ptr/.len) on Sdef ident

This commit is contained in:
2026-05-11 22:35:05 +09:00
parent 4ff63a0a40
commit 7cefedb634
4 changed files with 129 additions and 0 deletions

View File

@@ -499,6 +499,34 @@ fn cgdot(c: *cgen, n: *node) void = {
};
};
};
// `def NAME: str = "..."` field access — inline the literal.
// Sdef-backed strs aren't laid out in memory, so falling
// through to the SB-load fallback below would mis-emit
// `MOVQ <field>(SB), AX` (looking up the field name as a
// symbol). Mirrors cmd/w6c/cgen.c N_DOT off==0 / Sdef branch.
if (lhs != nil) {
if (lhs.kind == N_IDENT) {
let drhs: *node = deflookuprhs(c, lhs.str);
if (drhs != nil) {
if (drhs.kind == N_STRLIT) {
let bytes: str = drhs.str;
if (streq(fld, "ptr")) {
let lab: str = internstrlit(c, bytes);
emitline("\tLEAQ\t");
os.write(1, lab.ptr, lab.len: u64);
emitline("(SB), AX\n");
return;
};
if (streq(fld, "len")) {
emitline("\tMOVQ\t$");
emitint(bytes.len: i64);
emitline(", AX\n");
return;
};
};
};
};
};
// Module-qualified value reference: `mod.name` where `mod`
// is N_IDENT bound as SK_USE and the leaf isn't a local.
// Treat as a SB symbol — `MOVQ leaf(SB), AX`. Same fallback