w6c+w6a+selfhost+lib: cgen+asm bugs surfaced by hash modules

Seven fixes across the toolchain, plus three new lib/hash modules
(adler32, crc16, crc32) that surfaced them.

  1. `~x` on u8/u16/u32 left the upper bits set: NOTQ inverts the
     whole 64-bit register and nothing trimmed it back to type
     width, so a returned `u16` would compare 64-bit against a
     typed literal and disagree. Both stages now mask after NOTQ
     for narrow unsigned: AND $0xFF/0xFFFF for u8/u16, MOVL r,r for
     u32 (ANDQ $0xFFFFFFFF sign-extends imm32 and is a no-op).
     Signed narrows stay sign-extended and need no fix-up. See
     cmd/w6c/cgen.c N_UN TK_TILDE and selfhost cgenexpr.ww cgun
     TK_TILDE with new nodeprimwidth helper.

  2. w6a had no D_CONST immediate path for ANDQ / ORQ. cgen would
     emit `ANDQ $65535, AX` and the rr encoder silently wrote
     `21 /r` with garbage reg fields — the mask never happened.
     Added `81 /4` (AND) and `81 /1` (OR) imm32 paths in both
     cstage and selfhost w6a. The ~width fix above depends on this.

  3. `s: []u8` cast as a direct fn argument produced a 0-length
     slice. cgexpr for N_CAST left (AX=ptr, BX=len) from the str
     source but never set CX (cap), and the arg-push fallback only
     pushed AX. cgcast now synthesises CX=BX when target is slice
     and source is str; node_isslice / arg-push recognise
     cast-to-slice and emit the full (cap, len, ptr) triple. Both
     stages.

  4. `*[N]T` element-store used 8-byte stride + MOVQ regardless of
     T's width. Indexing `buf: *[4]u16` would step 8 bytes and
     write 8 bytes per element. Added idx_eff (drills *[N]T → T)
     in cstage and the matching pointer-array drill in selfhost
     elemsizeof. Also added MOVW / MOVZWQ / MOVSWQ to w6c, w6a,
     and selfhost mirrors so 2-byte element stores/loads use the
     right opcode (was falling through to MOVQ and trailing 6 bytes
     into the next slot).

  5. Slicing a top-level fixed array (`g[0:n]` where `g: [N]T` is
     a global) computed the base from BP instead of the symbol —
     localfind returned 0 and the cgen treated it as a local at
     offset 0. Both N_SLICE-as-expression (cgslice) and N_SLICE-
     as-call-arg paths now check let_islet / letvartnode and emit
     LEAQ name(SB) when the base is a global array (or MOVQ
     name(SB) for a global slice/pointer base). Both stages.

  6. Top-level `let arr: [N]T = [v0, v1, ...]` link-failed on
     cstage — emit_lets bailed when it saw N_ARRLIT init on an
     array type, and the sz==8 scalar path then misemitted any
     8-byte-sized array (e.g. [4]u16, [8]u8) as a single quad.
     emit_lets now walks N_ARRLIT, evaluates each element as an
     int/rune/bool/nil literal, packs per-element bytes
     little-endian, and honours the trailing `...` repeat marker.
     Selfhost already handled the literal-init path; fixed the
     parallel sz==8 duplicate-DATAW emit on its side (the array
     and the scalar paths both fired, last write winning at link
     but the duplicate broke cross-stage byte-identicality on user
     code with this shape).

  7. w6a's per-line input buffer was a 1KB stack `char buf[1024]`.
     A `DATAW` for a [256]u16 emits ~2080 bytes on one line, which
     truncated mid-escape; the assembler then re-parsed the
     remaining tail as garbage opcodes ("unknown opcode"). Bumped
     cstage w6a to a 32K static buffer (selfhost w6a already
     allocated per-line via amalloc).

  lib: lib/hash/adler32, lib/hash/crc16, lib/hash/crc32 — pure
  buffer-subset shape (matching lib/hash/fnv), with per-module
  *_test.ww runnable via `ww test lib/hash/<name>`. Adler-32 plus
  CRC-16 (CCITT/CMDA2000/DECT/ANSI) and CRC-32 (IEEE/Castagnoli/
  Koopman) cover Hare's reference vectors bit-for-bit. Wired into
  test/wcc/900_stdlib.c. .gitignore: lib/**/*.s,*.o so `ww test`
  droppings stay untracked.

`make test` (26/26), `make bootstrap` (ww2≡ww3≡ww4), and per-module
`ww test` all pass. cgen output is byte-identical across cstage and
selfhost for every repro that previously diverged.
This commit is contained in:
2026-05-13 14:26:18 +09:00
parent b05968c7f4
commit cbcc0167ae
22 changed files with 1242 additions and 59 deletions

3
.gitignore vendored
View File

@@ -21,7 +21,10 @@ selfhost/test/*.combined.ww
# `ww build` on a lib/ module leaves a .combined.ww next to the
# source. None of these are bootstrap inputs (those are under
# selfhost/cmd/*/main.combined.ww) — they're just transient.
# `ww test lib/foo` also drops the .s/.o triplet for *_test.ww.
lib/**/*.combined.ww
lib/**/*.s
lib/**/*.o
# examples/ build outputs. Each example has its own Makefile that
# leaves the .s/.o/.combined.ww plus a stripped binary behind.

View File

@@ -453,6 +453,57 @@ a_encode(Asm *a)
a->errs++;
}
break;
case A_MOVW:
/* 16-bit MOV: prefix 0x66 selects 16-bit operand size.
* MOV r/m16, r16 — 66 89 /r; MOV r16, r/m16 — 66 8B /r.
* No REX.W (operand-size prefix beats REX.W). */
if (p->from.type >= D_AX && p->from.type <= D_R15
&& p->to.type == D_INDIR) {
a_emit_byte(a, 0x66);
emit_rex(a, rhi(p->from.type), rhi(p->to.reg), 0);
a_emit_byte(a, 0x89);
emit_modrm_mem(a, rcode(p->from.type),
p->to.reg, p->to.offset);
} else if (p->from.type == D_INDIR
&& p->to.type >= D_AX && p->to.type <= D_R15) {
a_emit_byte(a, 0x66);
emit_rex(a, rhi(p->to.type), rhi(p->from.reg), 0);
a_emit_byte(a, 0x8B);
emit_modrm_mem(a, rcode(p->to.type),
p->from.reg, p->from.offset);
} else {
fprintf(stderr, "w6a: line %d: unsupported MOVW shape\n", p->line);
a->errs++;
}
break;
case A_MOVZWQ:
/* MOVZX r64, r/m16 — 0F B7 /r with REX.W */
if (p->from.type == D_INDIR
&& p->to.type >= D_AX && p->to.type <= D_R15) {
emit_rex(a, rhi(p->to.type), rhi(p->from.reg), 1);
a_emit_byte(a, 0x0F);
a_emit_byte(a, 0xB7);
emit_modrm_mem(a, rcode(p->to.type),
p->from.reg, p->from.offset);
} else {
fprintf(stderr, "w6a: line %d: unsupported MOVZWQ shape\n", p->line);
a->errs++;
}
break;
case A_MOVSWQ:
/* MOVSX r64, r/m16 — 0F BF /r with REX.W */
if (p->from.type == D_INDIR
&& p->to.type >= D_AX && p->to.type <= D_R15) {
emit_rex(a, rhi(p->to.type), rhi(p->from.reg), 1);
a_emit_byte(a, 0x0F);
a_emit_byte(a, 0xBF);
emit_modrm_mem(a, rcode(p->to.type),
p->from.reg, p->from.offset);
} else {
fprintf(stderr, "w6a: line %d: unsupported MOVSWQ shape\n", p->line);
a->errs++;
}
break;
case A_MOVB:
/* MOV r/m8, r8 — 88 /r. No REX.W. We always emit REX
* to allow access to SIL/DIL/BPL/SPL. */
@@ -638,8 +689,24 @@ a_encode(Asm *a)
else
encode_rr(a, 0x29, p->from.type, p->to.type);
break;
case A_ANDQ: encode_rr(a, 0x21, p->from.type, p->to.type); break;
case A_ORQ: encode_rr(a, 0x09, p->from.type, p->to.type); break;
case A_ANDQ:
/* AND r/m64, imm32 — 81 /4 (REX.W). Without the
* D_CONST path the rr encoder would silently emit
* a 0x21 with garbage reg fields. */
if (p->from.type == D_CONST
&& p->to.type >= D_AX && p->to.type <= D_R15)
encode_ri_imm32(a, 0x81, 4, p->to.type, (i32)p->from.offset);
else
encode_rr(a, 0x21, p->from.type, p->to.type);
break;
case A_ORQ:
/* OR r/m64, imm32 — 81 /1 (REX.W). Mirrors ANDQ. */
if (p->from.type == D_CONST
&& p->to.type >= D_AX && p->to.type <= D_R15)
encode_ri_imm32(a, 0x81, 1, p->to.type, (i32)p->from.offset);
else
encode_rr(a, 0x09, p->from.type, p->to.type);
break;
case A_XORQ:
if (p->from.type == D_CONST
&& p->to.type >= D_AX && p->to.type <= D_R15)

View File

@@ -83,8 +83,9 @@ opcode_lookup(const char *m)
{
struct { const char *m; int op; } tab[] = {
{ "MOVQ", A_MOVQ }, { "MOVL", A_MOVL },
{ "MOVB", A_MOVB }, { "MOVZBQ", A_MOVZBQ },
{ "MOVSXD", A_MOVSXD },
{ "MOVW", A_MOVW }, { "MOVB", A_MOVB },
{ "MOVZBQ", A_MOVZBQ }, { "MOVZWQ", A_MOVZWQ },
{ "MOVSXD", A_MOVSXD }, { "MOVSWQ", A_MOVSWQ },
{ "MOVSD", A_MOVSD },
{ "ADDSD", A_ADDSD },{ "SUBSD", A_SUBSD },
{ "MULSD", A_MULSD },{ "DIVSD", A_DIVSD },
@@ -263,7 +264,10 @@ parse_operand(Asm *a, const char *s, Aoperand *out)
int
a_parse(Asm *a)
{
char buf[1024];
/* Big enough for a DATAW emitting a [256]u32 table (1024 bytes
* → ~4100 chars of `\xNN` escapes plus directive boilerplate).
* Selfhost w6a allocates per-line; this is the cstage equivalent. */
static char buf[32768];
char *line;
size_t len;
const char *pending_label = NULL;

View File

@@ -49,9 +49,12 @@ enum {
A_MOVQ,
A_MOVL,
A_MOVW,
A_MOVB,
A_MOVZBQ, /* movzx r64, r/m8 — load byte zero-extended */
A_MOVZWQ, /* movzx r64, r/m16 — load word zero-extended */
A_MOVSXD, /* movsxd r64, r/m32 — load i32 sign-extended */
A_MOVSWQ, /* movsx r64, r/m16 — load word sign-extended */
/* SSE2 scalar double-precision float */
A_MOVSD, /* xmm/m → xmm and xmm → m */

View File

@@ -521,6 +521,21 @@ type_unwrap(Type *t)
return (t->kind == TY_NAMED) ? t->under : t;
}
/* Element-effective type for indexing. For `*[N]T` we drill through
* the pointer to the underlying array so esz/esub reflect T, not the
* whole-array pointee. For everything else returns t unchanged. */
static Type *
idx_eff(Type *t)
{
if (t == NULL) return NULL;
Type *u = type_unwrap(t);
if (u && u->kind == TY_PTR && u->sub) {
Type *p = type_unwrap(u->sub);
if (p && p->kind == TY_ARRAY) return p;
}
return u;
}
static int
decl_has_ffisym(Node *d)
{
@@ -1322,7 +1337,27 @@ cgexpr(Cg *c, Node *n, Local *locals)
ins1(c, A_NEGQ, areg(D_AX));
}
break;
case TK_TILDE: ins1(c, A_NOTQ, areg(D_AX)); break;
case TK_TILDE:
/* NOTQ inverts the whole 64-bit register. For unsigned
* narrow types we clamp to the type width so the
* upper bits are 0, matching how zero-extended loads
* leave the register. Signed narrow types already
* end up sign-extended (NOTQ on a sign-extended
* positive becomes sign-extended negative), so they
* need no fix-up. u32 uses MOVL r,r (zero-extends
* upper 32) because ANDQ $0xFFFFFFFF would sign-extend
* the imm32 to all-ones and act as a no-op. */
ins1(c, A_NOTQ, areg(D_AX));
if (n->type && type_isunsigned(n->type)
&& n->type->size < 8) {
if (n->type->size == 4) {
ins2(c, A_MOVL, areg(D_AX), areg(D_AX));
} else {
u64 mask = ((u64)1 << (n->type->size * 8)) - 1;
ins2(c, A_ANDQ, aimm((i64)mask), areg(D_AX));
}
}
break;
case TK_NOT: {
ins2(c, A_CMPQ, aimm(0), areg(D_AX));
char *t = mklabel(c, "tt");
@@ -1969,9 +2004,13 @@ cgexpr(Cg *c, Node *n, Local *locals)
int is_arr = u && u->kind == TY_ARRAY;
int is_sl = u && u->kind == TY_SLICE;
int is_ptr = u && u->kind == TY_PTR;
int esz = (u && u->sub) ? (int)u->sub->size : 1;
int elem_is_str = u && u->sub && type_isstr(u->sub);
Type *esub = u ? u->sub : NULL;
/* For `*[N]T` drill through to the array so esz reflects
* T, not sizeof(array). Base load still uses u (MOVQ
* because is_ptr stays true). */
Type *eff = idx_eff(bt);
int esz = (eff && eff->sub) ? (int)eff->sub->size : 1;
int elem_is_str = eff && eff->sub && type_isstr(eff->sub);
Type *esub = eff ? eff->sub : NULL;
Type *esubu = (esub && esub->kind == TY_NAMED)
? esub->under : esub;
int elem_tagged = esubu && esubu->kind == TY_TAGGED;
@@ -2074,6 +2113,7 @@ cgexpr(Cg *c, Node *n, Local *locals)
}
int store_op = A_MOVQ;
if (esz == 1) store_op = A_MOVB;
else if (esz == 2) store_op = A_MOVW;
else if (esz == 4) store_op = A_MOVL;
ins2(c, store_op, areg(D_AX), amem(D_BX, 0));
break;
@@ -2755,7 +2795,17 @@ cgexpr(Cg *c, Node *n, Local *locals)
/* base addr → push */
if (base->kind == N_IDENT) {
int boff = localfind(locals, base->str);
if (bu && bu->kind == TY_ARRAY) {
int isglobal = (boff == 0) &&
let_islet(base->str);
if (isglobal && bu && bu->kind == TY_ARRAY) {
ins2(c, A_LEAQ,
masym(c, base->str),
areg(D_AX));
} else if (isglobal) {
ins2(c, A_MOVQ,
masym(c, base->str),
areg(D_AX));
} else if (bu && bu->kind == TY_ARRAY) {
ins2(c, A_LEAQ, amem(D_BP, boff), areg(D_AX));
} else {
ins2(c, A_MOVQ, amem(D_BP, boff), areg(D_AX));
@@ -2771,7 +2821,17 @@ cgexpr(Cg *c, Node *n, Local *locals)
else if (base->kind == N_IDENT && bu &&
(bu->kind == TY_SLICE || bu->kind == TY_STR)) {
int boff = localfind(locals, base->str);
ins2(c, A_MOVQ, amem(D_BP, boff + 8), areg(D_AX));
int isglobal = (boff == 0) &&
let_islet(base->str);
if (isglobal) {
ins2(c, A_LEAQ,
masym(c, base->str),
areg(D_CX));
ins2(c, A_MOVQ,
amem(D_CX, 8), areg(D_AX));
} else {
ins2(c, A_MOVQ, amem(D_BP, boff + 8), areg(D_AX));
}
} else {
cgexpr_int(c, 0);
}
@@ -2849,6 +2909,13 @@ cgexpr(Cg *c, Node *n, Local *locals)
} else if (node_isstr(args[i])) {
ins1(c, A_PUSHQ, areg(D_BX)); /* len */
ins1(c, A_PUSHQ, areg(D_AX)); /* ptr — top */
} else if (node_isslice(args[i])) {
/* Slice-typed arg without a fast path above
* (e.g. `s: []u8` cast): cgexpr left
* (AX=ptr, BX=len, CX=cap). Push the triple. */
ins1(c, A_PUSHQ, areg(D_CX)); /* cap */
ins1(c, A_PUSHQ, areg(D_BX)); /* len */
ins1(c, A_PUSHQ, areg(D_AX)); /* ptr — top */
} else if (node_istaggedarg(args[i])) {
/* Tagged-return ABI: AX=tag, DX=val0,
* CX=val1, R8=val2. Push high-to-low so pop
@@ -3391,6 +3458,22 @@ cgexpr(Cg *c, Node *n, Local *locals)
int op = to_f32 ? A_CVTSD2SS : A_CVTSS2SD;
ins2(c, op, areg(D_X0), areg(D_X0));
}
/* str → []u8 (or any []T): cgexpr left (AX=ptr, BX=len).
* Slice register convention is (AX=ptr, BX=len, CX=cap);
* synthesise cap = len so downstream arg-push / let-init
* paths see the canonical triple. Without this, the cap
* register stays whatever cgexpr happened to leave there
* and the receiver reads a stale value. */
{
Type *tt = n->type;
Type *tu = (tt && tt->kind == TY_NAMED) ? tt->under : tt;
Type *ft = n->lhs ? n->lhs->type : NULL;
Type *fu = (ft && ft->kind == TY_NAMED) ? ft->under : ft;
if (tu && tu->kind == TY_SLICE
&& fu && fu->kind == TY_STR) {
ins2(c, A_MOVQ, areg(D_BX), areg(D_CX));
}
}
break;
}
case N_DOT: {
@@ -3748,13 +3831,16 @@ cgexpr(Cg *c, Node *n, Local *locals)
}
case N_INDEX: {
/* Scaled indexing for slice/array/str/ptr-to-T.
* Element size is 1 for u8/str, otherwise type's natural size. */
* Element size is 1 for u8/str, otherwise type's natural size.
* For `*[N]T` drill through to the array so esz/esub reflect
* T, not sizeof(array). */
Type *bt = n->lhs ? n->lhs->type : NULL;
Type *u = (bt && bt->kind == TY_NAMED) ? bt->under : bt;
Type *eff = idx_eff(bt);
int esz = 1;
if (u && u->sub) esz = (int)u->sub->size;
if (eff && eff->sub) esz = (int)eff->sub->size;
if (u && u->kind == TY_STR) esz = 1;
Type *esub = u ? u->sub : NULL;
Type *esub = eff ? eff->sub : NULL;
Type *esubu = (esub && esub->kind == TY_NAMED)
? esub->under : esub;
int elem_tagged = esubu && esubu->kind == TY_TAGGED;
@@ -3811,11 +3897,12 @@ cgexpr(Cg *c, Node *n, Local *locals)
ins2(c, A_MOVQ, amem(D_BX, 0), areg(D_AX));
break;
}
int signed_elem = u && u->sub && (
u->sub->kind == TY_I8 || u->sub->kind == TY_I16 ||
u->sub->kind == TY_I32);
int signed_elem = esub && (
esub->kind == TY_I8 || esub->kind == TY_I16 ||
esub->kind == TY_I32);
int load_op = A_MOVQ;
if (esz == 1) load_op = A_MOVZBQ;
else if (esz == 2) load_op = signed_elem ? A_MOVSWQ : A_MOVZWQ;
else if (esz == 4) load_op = signed_elem ? A_MOVSXD : A_MOVL;
ins2(c, load_op, amem(D_BX, 0), areg(D_AX));
break;
@@ -3858,11 +3945,12 @@ cgexpr(Cg *c, Node *n, Local *locals)
break;
}
{
int signed_elem = u && u->sub && (
u->sub->kind == TY_I8 || u->sub->kind == TY_I16 ||
u->sub->kind == TY_I32);
int signed_elem = esub && (
esub->kind == TY_I8 || esub->kind == TY_I16 ||
esub->kind == TY_I32);
int load_op = A_MOVQ;
if (esz == 1) load_op = A_MOVZBQ;
else if (esz == 2) load_op = signed_elem ? A_MOVSWQ : A_MOVZWQ;
else if (esz == 4)
load_op = signed_elem ? A_MOVSXD : A_MOVL;
ins2(c, load_op, amem(D_AX, 0), areg(D_AX));
@@ -3884,7 +3972,14 @@ cgexpr(Cg *c, Node *n, Local *locals)
Type *bu = (bt && bt->kind == TY_NAMED) ? bt->under : bt;
if (base && base->kind == N_IDENT) {
int boff = localfind(locals, base->str);
if (bu && bu->kind == TY_ARRAY) {
int isglobal = (boff == 0) && let_islet(base->str);
if (isglobal && bu && bu->kind == TY_ARRAY) {
ins2(c, A_LEAQ, masym(c, base->str),
areg(D_AX));
} else if (isglobal) {
ins2(c, A_MOVQ, masym(c, base->str),
areg(D_AX));
} else if (bu && bu->kind == TY_ARRAY) {
ins2(c, A_LEAQ, amem(D_BP, boff), areg(D_AX));
} else {
ins2(c, A_MOVQ, amem(D_BP, boff), areg(D_AX));
@@ -3903,7 +3998,15 @@ cgexpr(Cg *c, Node *n, Local *locals)
} else if (base && base->kind == N_IDENT && bu &&
(bu->kind == TY_SLICE || bu->kind == TY_STR)) {
int boff = localfind(locals, base->str);
ins2(c, A_MOVQ, amem(D_BP, boff + 8), areg(D_AX));
int isglobal = (boff == 0) && let_islet(base->str);
if (isglobal) {
ins2(c, A_LEAQ, masym(c, base->str),
areg(D_CX));
ins2(c, A_MOVQ, amem(D_CX, 8), areg(D_AX));
} else {
ins2(c, A_MOVQ, amem(D_BP, boff + 8),
areg(D_AX));
}
} else {
cgexpr_int(c, 0);
}
@@ -4933,7 +5036,7 @@ emit_lets(Cg *c, FILE *out, Node *file)
fputs("\"\n", out);
continue;
}
if (sz == 8) {
if (sz == 8 && !let_isarray(d->type)) {
u64 v = 0;
if (d->rhs != NULL) {
Node *r = d->rhs;
@@ -4974,9 +5077,67 @@ emit_lets(Cg *c, FILE *out, Node *file)
fprintf(out, "DATAR %s+0(SB),%s(SB)\n", sym, lab);
continue;
}
/* Array literal init: `let xs: [N]T = [v0, v1, ...];`. Walk
* elements in declaration order; each must reduce to an
* integer literal (casts are stripped). The trailing `...`
* repeat marker fills remaining slots with the last value.
* Falls through to zero-init if any element isn't a
* constant we can evaluate at emit time. */
if (r != NULL && r->kind == N_ARRLIT && let_isarray(d->type)) {
Type *u = type_unwrap(d->type);
int esz = (u && u->sub) ? (int)u->sub->size : 1;
int alen = (u) ? (int)u->alen : 0;
u64 *vals = amalloc(c->a, sizeof(u64) * (size_t)alen);
int idx = 0;
int ok = 1;
u64 last = 0;
int repeat = 0;
for (Node *e = r->list; e && idx < alen; e = e->next) {
if (e->kind == N_FIELD && e->str &&
strcmp(e->str, "...") == 0) {
repeat = 1;
break;
}
Node *ev = e;
while (ev && ev->kind == N_CAST) ev = ev->lhs;
if (ev == NULL) { ok = 0; break; }
if (ev->kind == N_INTLIT || ev->kind == N_RUNELIT) {
last = ev->uval;
} else if (ev->kind == N_TRUE) {
last = 1;
} else if (ev->kind == N_FALSE) {
last = 0;
} else if (ev->kind == N_NIL) {
last = 0;
} else {
ok = 0;
break;
}
vals[idx++] = last;
}
if (ok) {
if (repeat) {
while (idx < alen) vals[idx++] = last;
} else {
while (idx < alen) vals[idx++] = 0;
}
fprintf(out, "DATAW %s(SB),\"",
mod_mangle(c, d->str));
for (int i = 0; i < alen; i++) {
u64 v = vals[i];
for (int b = 0; b < esz; b++) {
emit_data_byte(out,
(u8)((v >> (b * 8)) & 0xff));
}
}
fputs("\"\n", out);
continue;
}
/* fall through to zero-init */
}
/* Otherwise: zero-init. str accepts nil / ""; struct
* accepts no rhs at all; slice accepts nil; array accepts
* no rhs (literal-array init isn't wired). */
* accepts no rhs at all; slice accepts nil; array with no
* literal init (or a non-constant one) zero-fills. */
if (r != NULL) {
int is_struct = let_isstruct(d->type);
int is_array = let_isarray(d->type);

View File

@@ -31,9 +31,12 @@ anames(int op)
case A_END: return "END";
case A_MOVQ: return "MOVQ";
case A_MOVL: return "MOVL";
case A_MOVW: return "MOVW";
case A_MOVB: return "MOVB";
case A_MOVZBQ: return "MOVZBQ";
case A_MOVZWQ: return "MOVZWQ";
case A_MOVSXD: return "MOVSXD";
case A_MOVSWQ: return "MOVSWQ";
case A_MOVSD: return "MOVSD";
case A_ADDSD: return "ADDSD";
case A_SUBSD: return "SUBSD";

View File

@@ -0,0 +1,20 @@
// hash/adler32 — Adler-32 checksum (RFC 1950). Pure ww.
//
// Hare ships a hash::hash-shaped streaming type backed by io::stream;
// we ship the pure-buffer subset here, same shape as lib/hash/fnv.
// `sum32(buf)` matches Hare's adler32::sum32 contract for a single
// write-then-sum: a = 1, b = 0, fold each byte, return b<<16 | a.
def MOD: u32 = 65521u32;
export fn sum32(buf: []u8) u32 = {
let a: u32 = 1u32;
let b: u32 = 0u32;
let i: i32 = 0;
for (i < buf.len) {
a = (a + (buf[i]: u32)) % MOD;
b = (b + a) % MOD;
i += 1;
};
return (b << 16u32) | a;
};

View File

@@ -0,0 +1,52 @@
use adler32;
fn putstr(s: str, into: []u8, off: i32) i32 = {
let i: i32 = 0;
for (i < s.len) {
into[off + i] = s[i];
i += 1;
};
return off + s.len;
};
fn check(s: str, want: u32) void = {
let arr: [256]u8;
let n: i32 = putstr(s, arr[0:256], 0);
let buf: []u8 = arr[0:n];
let got: u32 = adler32.sum32(buf);
if (got != want) { let _: i32 = 1/0; };
};
@test fn vec_empty() void = {
let arr: [1]u8;
let buf: []u8 = arr[0:0];
let h: u32 = adler32.sum32(buf);
if (h != 1u32) { let _: i32 = 1/0; };
};
@test fn vec_helloworld() void = {
check("hello world", 436929629u32);
};
@test fn vec_hareiscool() void = {
check("Hare is a cool language", 1578567727u32);
};
@test fn vec_bdale() void = {
check("'Life is too short to run proprietary software' - Bdale Garbee",
3135706652u32);
};
@test fn vec_geer() void = {
check("'The central enemy of reliability is complexity.' - Geer et al",
3170309588u32);
};
export fn main() i32 = {
vec_empty();
vec_helloworld();
vec_hareiscool();
vec_bdale();
vec_geer();
return 0;
};

42
lib/hash/crc16/crc16.ww Normal file
View File

@@ -0,0 +1,42 @@
// hash/crc16 — CRC-16 checksum. Pure ww.
//
// Inline polynomial-shift per byte (no precomputed tables). Slower
// than a table-driven CRC by ~8x per byte but matches the
// table-driven answer bit-for-bit.
//
// Polynomials are given in reversed form, matching Hare.
def CCITT: u16 = 0x8408u16; // X.25, Bluetooth, XMODEM
def CMDA2000: u16 = 0xE613u16; // CDMA2000 infra
def DECT: u16 = 0x91A0u16; // DECT cordless
def ANSI: u16 = 0xA001u16; // Modbus, USB, ANSI X3.28
// sum16 — fold `buf` under `poly` and return ~cval. Initial value is
// ~0u16, matching the streaming CRC-16 contract for a single
// write-then-sum. Per byte: XOR low byte of cval with msg byte to form
// an 8-bit index, run 8 polynomial shifts on that index, XOR the
// result with the high byte of cval shifted down.
export fn sum16(buf: []u8, poly: u16) u16 = {
let c: u16 = 0xFFFFu16;
let i: i32 = 0;
for (i < buf.len) {
let t: u16 = (c & 0xFFu16) ^ (buf[i]: u16);
let z: i32 = 0;
for (z < 8) {
if ((t & 1u16) == 1u16) {
t = (t >> 1u16) ^ poly;
} else {
t = t >> 1u16;
};
z += 1;
};
c = t ^ (c >> 8u16);
i += 1;
};
return ~c;
};
export fn sum16ccitt(buf: []u8) u16 = { return sum16(buf, CCITT); };
export fn sum16cmda2000(buf: []u8) u16 = { return sum16(buf, CMDA2000); };
export fn sum16dect(buf: []u8) u16 = { return sum16(buf, DECT); };
export fn sum16ansi(buf: []u8) u16 = { return sum16(buf, ANSI); };

View File

@@ -0,0 +1,58 @@
use crc16;
fn putstr(s: str, into: []u8, off: i32) i32 = {
let i: i32 = 0;
for (i < s.len) {
into[off + i] = s[i];
i += 1;
};
return off + s.len;
};
fn check(s: str, ccitt: u16, cmda: u16, dect: u16, ansi: u16) void = {
let arr: [256]u8;
let n: i32 = putstr(s, arr[0:256], 0);
let buf: []u8 = arr[0:n];
if (crc16.sum16ccitt(buf) != ccitt) { let _: i32 = 1/0; };
if (crc16.sum16cmda2000(buf) != cmda) { let _: i32 = 1/0; };
if (crc16.sum16dect(buf) != dect) { let _: i32 = 1/0; };
if (crc16.sum16ansi(buf) != ansi) { let _: i32 = 1/0; };
};
@test fn vec_empty() void = {
let arr: [1]u8;
let buf: []u8 = arr[0:0];
if (crc16.sum16ccitt(buf) != 0u16) { let _: i32 = 1/0; };
if (crc16.sum16cmda2000(buf) != 0u16) { let _: i32 = 1/0; };
if (crc16.sum16dect(buf) != 0u16) { let _: i32 = 1/0; };
if (crc16.sum16ansi(buf) != 0u16) { let _: i32 = 1/0; };
};
@test fn vec_truman() void = {
check("Always be sincere, even if you don't mean it. -- Harry Truman",
0x38DFu16, 0x2441u16, 0x8E44u16, 0xEF5Eu16);
};
@test fn vec_animals() void = {
check("You get along very well with everyone except animals and people.",
0xB6AEu16, 0xFED0u16, 0x8739u16, 0xCF56u16);
};
@test fn vec_twain() void = {
check("All generalizations are false, including this one. -- Mark Twain",
0xCA68u16, 0x65ECu16, 0x098Au16, 0x45B4u16);
};
@test fn vec_peace() void = {
check("I want peace and I'm willing to fight for it. -- Harry Truman",
0xB0E8u16, 0xFE1Fu16, 0x4659u16, 0x5062u16);
};
export fn main() i32 = {
vec_empty();
vec_truman();
vec_animals();
vec_twain();
vec_peace();
return 0;
};

36
lib/hash/crc32/crc32.ww Normal file
View File

@@ -0,0 +1,36 @@
// hash/crc32 — CRC-32 checksum. Pure ww.
//
// Same shape as lib/hash/crc16: per-byte inline polynomial shift,
// no precomputed table. Slower than Hare's table-driven path by ~8x
// per byte but produces identical answers.
def IEEE: u32 = 0xEDB88320u32; // gzip, PNG, zip, Ethernet
def CASTAGNOLI: u32 = 0x82F63B78u32; // iSCSI, SCTP, SSE4.2
def KOOPMAN: u32 = 0xEB31D82Eu32; // small datasets
// sum32 — fold `buf` under `poly` (reversed form). Initial cval is
// ~0u32; per byte we mix in the low byte via 8 polynomial shifts and
// XOR with the high three bytes shifted down.
export fn sum32(buf: []u8, poly: u32) u32 = {
let c: u32 = 0xFFFFFFFFu32;
let i: i32 = 0;
for (i < buf.len) {
let t: u32 = (c & 0xFFu32) ^ (buf[i]: u32);
let z: i32 = 0;
for (z < 8) {
if ((t & 1u32) == 1u32) {
t = (t >> 1u32) ^ poly;
} else {
t = t >> 1u32;
};
z += 1;
};
c = t ^ (c >> 8u32);
i += 1;
};
return ~c;
};
export fn sum32ieee(buf: []u8) u32 = { return sum32(buf, IEEE); };
export fn sum32castagnoli(buf: []u8) u32 = { return sum32(buf, CASTAGNOLI); };
export fn sum32koopman(buf: []u8) u32 = { return sum32(buf, KOOPMAN); };

View File

@@ -0,0 +1,62 @@
use crc32;
fn putstr(s: str, into: []u8, off: i32) i32 = {
let i: i32 = 0;
for (i < s.len) {
into[off + i] = s[i];
i += 1;
};
return off + s.len;
};
fn check(s: str, ieee: u32, cast: u32, koop: u32) void = {
let arr: [128]u8;
let n: i32 = putstr(s, arr[0:128], 0);
let buf: []u8 = arr[0:n];
if (crc32.sum32ieee(buf) != ieee) { let _: i32 = 1/0; };
if (crc32.sum32castagnoli(buf) != cast) { let _: i32 = 1/0; };
if (crc32.sum32koopman(buf) != koop) { let _: i32 = 1/0; };
};
@test fn vec_empty() void = {
let arr: [1]u8;
let buf: []u8 = arr[0:0];
if (crc32.sum32ieee(buf) != 0u32) { let _: i32 = 1/0; };
if (crc32.sum32castagnoli(buf) != 0u32) { let _: i32 = 1/0; };
if (crc32.sum32koopman(buf) != 0u32) { let _: i32 = 1/0; };
};
@test fn vec_fire() void = {
check("Give a man a fire, they be warm for a day",
4026734998u32, 2112273292u32, 1263149916u32);
};
@test fn vec_setfire() void = {
check("Set a man on fire, they be warm for the rest of their life",
3931092334u32, 4172943610u32, 3071632577u32);
};
@test fn vec_blm() void = {
check("Black lives matter",
2370964079u32, 2068416418u32, 4151357773u32);
};
@test fn vec_unix() void = {
check("UNIX is simple and coherent",
3254252081u32, 1650777601u32, 340189632u32);
};
@test fn vec_gnu() void = {
check("GNU's not UNIX",
224734747u32, 200511816u32, 332335539u32);
};
export fn main() i32 = {
vec_empty();
vec_fire();
vec_setfire();
vec_blm();
vec_unix();
vec_gnu();
return 0;
};

View File

@@ -443,6 +443,62 @@ export fn encode(a: *asm_) i32 = {
p = p.link; continue;
};
if (op == A_MOVW) {
// 16-bit MOV: 0x66 operand-size prefix + the 32-bit
// MOV opcodes 0x89 / 0x8B. No REX.W.
let ft: i32 = p.from.atype;
let tt: i32 = p.to.atype;
if (isgpr(ft)) { if (tt == D_INDIR) {
emitbyte(a, 102u8); // 0x66
emitrex(a, rhi(ft), rhi(p.to.reg), 0);
emitbyte(a, 137u8); // 0x89
emitmodrmmem(a, rcode(ft), p.to.reg, p.to.offset);
p = p.link; continue;
};};
if (ft == D_INDIR) { if (isgpr(tt)) {
emitbyte(a, 102u8); // 0x66
emitrex(a, rhi(tt), rhi(p.from.reg), 0);
emitbyte(a, 139u8); // 0x8B
emitmodrmmem(a, rcode(tt), p.from.reg, p.from.offset);
p = p.link; continue;
};};
os.write(2, "w6a: unsupported MOVW shape\n".ptr, 27u64);
a.errs += 1;
p = p.link; continue;
};
if (op == A_MOVZWQ) {
// MOVZX r64, r/m16 — 0F B7 /r with REX.W.
let ft: i32 = p.from.atype;
let tt: i32 = p.to.atype;
if (ft == D_INDIR) { if (isgpr(tt)) {
emitrex(a, rhi(tt), rhi(p.from.reg), 1);
emitbyte(a, 15u8);
emitbyte(a, 183u8); // 0xB7
emitmodrmmem(a, rcode(tt), p.from.reg, p.from.offset);
p = p.link; continue;
};};
os.write(2, "w6a: unsupported MOVZWQ shape\n".ptr, 29u64);
a.errs += 1;
p = p.link; continue;
};
if (op == A_MOVSWQ) {
// MOVSX r64, r/m16 — 0F BF /r with REX.W.
let ft: i32 = p.from.atype;
let tt: i32 = p.to.atype;
if (ft == D_INDIR) { if (isgpr(tt)) {
emitrex(a, rhi(tt), rhi(p.from.reg), 1);
emitbyte(a, 15u8);
emitbyte(a, 191u8); // 0xBF
emitmodrmmem(a, rcode(tt), p.from.reg, p.from.offset);
p = p.link; continue;
};};
os.write(2, "w6a: unsupported MOVSWQ shape\n".ptr, 29u64);
a.errs += 1;
p = p.link; continue;
};
if (op == A_MOVZBQ) {
let ft: i32 = p.from.atype;
let tt: i32 = p.to.atype;
@@ -607,8 +663,30 @@ export fn encode(a: *asm_) i32 = {
p = p.link; continue;
};
if (op == A_ANDQ) { encoderr(a, 33u8, p.from.atype, p.to.atype); p = p.link; continue; }; // 0x21
if (op == A_ORQ) { encoderr(a, 9u8, p.from.atype, p.to.atype); p = p.link; continue; }; // 0x09
if (op == A_ANDQ) {
// AND r/m64, imm32 — 0x81 /4 (REX.W). Without the
// D_CONST path encoderr would silently emit 0x21
// with garbage reg fields.
let ft: i32 = p.from.atype;
let tt: i32 = p.to.atype;
if (ft == D_CONST) { if (isgpr(tt)) {
encoderiimm32(a, 129u8, 4, tt, p.from.offset: i32);
p = p.link; continue;
};};
encoderr(a, 33u8, ft, tt); // 0x21
p = p.link; continue;
};
if (op == A_ORQ) {
// OR r/m64, imm32 — 0x81 /1 (REX.W). Mirrors ANDQ.
let ft: i32 = p.from.atype;
let tt: i32 = p.to.atype;
if (ft == D_CONST) { if (isgpr(tt)) {
encoderiimm32(a, 129u8, 1, tt, p.from.offset: i32);
p = p.link; continue;
};};
encoderr(a, 9u8, ft, tt); // 0x09
p = p.link; continue;
};
if (op == A_XORQ) {
let ft: i32 = p.from.atype;
let tt: i32 = p.to.atype;

View File

@@ -402,6 +402,9 @@ def A_MOVL: i32 = 6;
def A_MOVB: i32 = 7;
def A_MOVZBQ: i32 = 8;
def A_MOVSXD: i32 = 9;
def A_MOVW: i32 = 62;
def A_MOVZWQ: i32 = 63;
def A_MOVSWQ: i32 = 64;
def A_MOVSD: i32 = 10;
def A_ADDSD: i32 = 11;
@@ -648,9 +651,12 @@ fn streqlit(p: *u8, n: u64, lit: str) bool = {
fn opcodelookup(p: *u8, n: u64) i32 = {
if (streqlit(p, n, "MOVQ")) { return A_MOVQ; };
if (streqlit(p, n, "MOVL")) { return A_MOVL; };
if (streqlit(p, n, "MOVW")) { return A_MOVW; };
if (streqlit(p, n, "MOVB")) { return A_MOVB; };
if (streqlit(p, n, "MOVZBQ")) { return A_MOVZBQ; };
if (streqlit(p, n, "MOVZWQ")) { return A_MOVZWQ; };
if (streqlit(p, n, "MOVSXD")) { return A_MOVSXD; };
if (streqlit(p, n, "MOVSWQ")) { return A_MOVSWQ; };
if (streqlit(p, n, "MOVSD")) { return A_MOVSD; };
if (streqlit(p, n, "ADDSD")) { return A_ADDSD; };
if (streqlit(p, n, "SUBSD")) { return A_SUBSD; };
@@ -1647,6 +1653,62 @@ export fn encode(a: *asm_) i32 = {
p = p.link; continue;
};
if (op == A_MOVW) {
// 16-bit MOV: 0x66 operand-size prefix + the 32-bit
// MOV opcodes 0x89 / 0x8B. No REX.W.
let ft: i32 = p.from.atype;
let tt: i32 = p.to.atype;
if (isgpr(ft)) { if (tt == D_INDIR) {
emitbyte(a, 102u8); // 0x66
emitrex(a, rhi(ft), rhi(p.to.reg), 0);
emitbyte(a, 137u8); // 0x89
emitmodrmmem(a, rcode(ft), p.to.reg, p.to.offset);
p = p.link; continue;
};};
if (ft == D_INDIR) { if (isgpr(tt)) {
emitbyte(a, 102u8); // 0x66
emitrex(a, rhi(tt), rhi(p.from.reg), 0);
emitbyte(a, 139u8); // 0x8B
emitmodrmmem(a, rcode(tt), p.from.reg, p.from.offset);
p = p.link; continue;
};};
os.write(2, "w6a: unsupported MOVW shape\n".ptr, 27u64);
a.errs += 1;
p = p.link; continue;
};
if (op == A_MOVZWQ) {
// MOVZX r64, r/m16 — 0F B7 /r with REX.W.
let ft: i32 = p.from.atype;
let tt: i32 = p.to.atype;
if (ft == D_INDIR) { if (isgpr(tt)) {
emitrex(a, rhi(tt), rhi(p.from.reg), 1);
emitbyte(a, 15u8);
emitbyte(a, 183u8); // 0xB7
emitmodrmmem(a, rcode(tt), p.from.reg, p.from.offset);
p = p.link; continue;
};};
os.write(2, "w6a: unsupported MOVZWQ shape\n".ptr, 29u64);
a.errs += 1;
p = p.link; continue;
};
if (op == A_MOVSWQ) {
// MOVSX r64, r/m16 — 0F BF /r with REX.W.
let ft: i32 = p.from.atype;
let tt: i32 = p.to.atype;
if (ft == D_INDIR) { if (isgpr(tt)) {
emitrex(a, rhi(tt), rhi(p.from.reg), 1);
emitbyte(a, 15u8);
emitbyte(a, 191u8); // 0xBF
emitmodrmmem(a, rcode(tt), p.from.reg, p.from.offset);
p = p.link; continue;
};};
os.write(2, "w6a: unsupported MOVSWQ shape\n".ptr, 29u64);
a.errs += 1;
p = p.link; continue;
};
if (op == A_MOVZBQ) {
let ft: i32 = p.from.atype;
let tt: i32 = p.to.atype;
@@ -1811,8 +1873,30 @@ export fn encode(a: *asm_) i32 = {
p = p.link; continue;
};
if (op == A_ANDQ) { encoderr(a, 33u8, p.from.atype, p.to.atype); p = p.link; continue; }; // 0x21
if (op == A_ORQ) { encoderr(a, 9u8, p.from.atype, p.to.atype); p = p.link; continue; }; // 0x09
if (op == A_ANDQ) {
// AND r/m64, imm32 — 0x81 /4 (REX.W). Without the
// D_CONST path encoderr would silently emit 0x21
// with garbage reg fields.
let ft: i32 = p.from.atype;
let tt: i32 = p.to.atype;
if (ft == D_CONST) { if (isgpr(tt)) {
encoderiimm32(a, 129u8, 4, tt, p.from.offset: i32);
p = p.link; continue;
};};
encoderr(a, 33u8, ft, tt); // 0x21
p = p.link; continue;
};
if (op == A_ORQ) {
// OR r/m64, imm32 — 0x81 /1 (REX.W). Mirrors ANDQ.
let ft: i32 = p.from.atype;
let tt: i32 = p.to.atype;
if (ft == D_CONST) { if (isgpr(tt)) {
encoderiimm32(a, 129u8, 1, tt, p.from.offset: i32);
p = p.link; continue;
};};
encoderr(a, 9u8, ft, tt); // 0x09
p = p.link; continue;
};
if (op == A_XORQ) {
let ft: i32 = p.from.atype;
let tt: i32 = p.to.atype;

View File

@@ -31,9 +31,12 @@ fn streqlit(p: *u8, n: u64, lit: str) bool = {
fn opcodelookup(p: *u8, n: u64) i32 = {
if (streqlit(p, n, "MOVQ")) { return A_MOVQ; };
if (streqlit(p, n, "MOVL")) { return A_MOVL; };
if (streqlit(p, n, "MOVW")) { return A_MOVW; };
if (streqlit(p, n, "MOVB")) { return A_MOVB; };
if (streqlit(p, n, "MOVZBQ")) { return A_MOVZBQ; };
if (streqlit(p, n, "MOVZWQ")) { return A_MOVZWQ; };
if (streqlit(p, n, "MOVSXD")) { return A_MOVSXD; };
if (streqlit(p, n, "MOVSWQ")) { return A_MOVSWQ; };
if (streqlit(p, n, "MOVSD")) { return A_MOVSD; };
if (streqlit(p, n, "ADDSD")) { return A_ADDSD; };
if (streqlit(p, n, "SUBSD")) { return A_SUBSD; };

View File

@@ -64,6 +64,9 @@ def A_MOVL: i32 = 6;
def A_MOVB: i32 = 7;
def A_MOVZBQ: i32 = 8;
def A_MOVSXD: i32 = 9;
def A_MOVW: i32 = 62;
def A_MOVZWQ: i32 = 63;
def A_MOVSWQ: i32 = 64;
def A_MOVSD: i32 = 10;
def A_ADDSD: i32 = 11;

View File

@@ -5789,10 +5789,20 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = {
let lo: *node = arg.rhs;
let hi: *node = arg.cond;
let baselocal: *local = nil;
let globaltn: *node = nil;
let globalname: str;
globalname.ptr = nil; globalname.len = 0;
if (base != nil) {
if (base.kind == nkind.N_IDENT) {
let bn: str = base.str;
baselocal = localfindnode(c, bn);
if (baselocal == nil) {
let gt: *node = letvartnode(c, bn);
if (gt != nil) {
globaltn = gt;
globalname = bn;
};
};
};
};
// base address → push
@@ -5813,9 +5823,19 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = {
emitoff(baselocal.off: i64);
emitline("(BP), AX\n");
};
} else { if (globaltn != nil) {
if (globaltn.kind == nkind.N_TARRAY) {
emitline("\tLEAQ\t");
emitsymname(c, globalname);
emitline("(SB), AX\n");
} else {
emitline("\tMOVQ\t");
emitsymname(c, globalname);
emitline("(SB), AX\n");
};
} else {
cgexpr(c, base);
};
};};
emitline("\tPUSHQ\tAX\n");
// hi (default base length) → push
if (hi != nil) {
@@ -5844,9 +5864,25 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = {
};
};};};
};
} else { if (globaltn != nil) {
if (globaltn.kind == nkind.N_TARRAY) {
let lenn: *node = globaltn.rhs;
if (lenn != nil) {
if (lenn.kind == nkind.N_INTLIT) {
emitline("\tMOVQ\t$");
emituint(lenn.uval);
emitline(", AX\n");
};
};
} else { if (globaltn.kind == nkind.N_TSLICE) {
emitline("\tLEAQ\t");
emitsymname(c, globalname);
emitline("(SB), CX\n");
emitline("\tMOVQ\t8(CX), AX\n");
};};
} else {
emitline("\tMOVQ\t$0, AX\n");
};};
};};};
emitline("\tPUSHQ\tAX\n");
// lo (default 0) → AX
if (lo != nil) { cgexpr(c, lo); }
@@ -5930,6 +5966,7 @@ fn nodeisslice(c: *cgen, n: *node) bool = {
return false;
};
if (k == nkind.N_SLICE) { return true; };
if (k == nkind.N_CAST) { return isslicetype(c, n.rhs); };
return false;
};
@@ -6287,6 +6324,11 @@ fn elemsizeof(t: *node) i32 = {
return 1;
};
if (elem == nil) { return 1; };
// `*[N]T`: drill through the pointer into the array's element so
// indexing scales by T's width, not the whole-array byte size.
if (elem.kind == nkind.N_TARRAY) {
if (elem.lhs != nil) { elem = elem.lhs; };
};
if (elem.kind == nkind.N_TNAME) {
let nm: str = elem.str;
// str element is 16B (ptr+len). primsize returns 0 for it.
@@ -6409,6 +6451,35 @@ fn nodeisunsigned(c: *cgen, n: *node) bool = {
};
return false;
};
// nodeprimwidth — primitive byte width of an expression, or 0 if not
// statically determinable. Mirrors nodeisunsigned's structural walk.
// Used by cgun TK_TILDE to clamp narrow unsigned ~ results to type
// width (NOTQ inverts the full 64-bit register).
fn nodeprimwidth(c: *cgen, n: *node) i32 = {
if (n == nil) { return 0; };
let k: nkind = n.kind;
if (k == nkind.N_IDENT) {
let lc: *local = localfindnode(c, n.str);
if (lc != nil) {
let tn: *node = lc.tnode;
if (tn != nil) {
if (tn.kind == nkind.N_TNAME) { return primsize(tn.str); };
};
};
return 0;
};
if (k == nkind.N_CAST) {
let tn: *node = n.rhs;
if (tn != nil) {
if (tn.kind == nkind.N_TNAME) { return primsize(tn.str); };
};
return 0;
};
if (k == nkind.N_UN) { return nodeprimwidth(c, n.lhs); };
return 0;
};
// ---- type-driven slot sizing ----------------------------------------
fn structlookup(c: *cgen, name: str) *structinfo = {
@@ -8051,6 +8122,23 @@ fn cgcast(c: *cgen, n: *node) void = {
if (dstf32) { dstfk = 1; }
else { if (dstf64) { dstfk = 2; }; };
cgexpr(c, n.lhs);
// str → []T: cgexpr left (AX=ptr, BX=len). Slice register
// convention is (AX=ptr, BX=len, CX=cap); synthesise cap = len
// so downstream arg-push / let-init paths see the canonical
// triple. Detect via dst-is-slice + src-ident's local-tnode
// being str (the common shape; non-ident sources rare).
if (isslicetype(c, n.rhs)) {
let srcstr: bool = false;
if (n.lhs != nil) {
if (n.lhs.kind == nkind.N_IDENT) {
let lc: *local = localfindnode(c, n.lhs.str);
if (lc != nil) {
if (isstrtype(c, lc.tnode)) { srcstr = true; };
};
};
};
if (srcstr) { emitline("\tMOVQ\tBX, CX\n"); };
};
// 0=int, 1=f32, 2=f64. CVT picks one direction per combo;
// same-kind casts (int↔int with widening differences,
// f64→f64 etc.) stay no-ops at the asm level, matching the
@@ -8326,11 +8414,15 @@ fn cgindex(c: *cgen, n: *node) void = {
return;
};
if (esz == 1) { emitline("\tMOVZBQ\t(BX), AX\n"); }
else { if (esz == 2) {
if (signed_elem) { emitline("\tMOVSWQ\t(BX), AX\n"); }
else { emitline("\tMOVZWQ\t(BX), AX\n"); };
}
else { if (esz == 4) {
if (signed_elem) { emitline("\tMOVSXD\t(BX), AX\n"); }
else { emitline("\tMOVL\t(BX), AX\n"); };
}
else { emitline("\tMOVQ\t(BX), AX\n"); };};
else { emitline("\tMOVQ\t(BX), AX\n"); };};};
return;
};
if (baselocal != nil) {
@@ -8369,11 +8461,15 @@ fn cgindex(c: *cgen, n: *node) void = {
return;
};
if (esz == 1) { emitline("\tMOVZBQ\t(BX), AX\n"); }
else { if (esz == 2) {
if (signed_elem) { emitline("\tMOVSWQ\t(BX), AX\n"); }
else { emitline("\tMOVZWQ\t(BX), AX\n"); };
}
else { if (esz == 4) {
if (signed_elem) { emitline("\tMOVSXD\t(BX), AX\n"); }
else { emitline("\tMOVL\t(BX), AX\n"); };
}
else { emitline("\tMOVQ\t(BX), AX\n"); };};
else { emitline("\tMOVQ\t(BX), AX\n"); };};};
return;
};
// Generic fallback when base isn't a plain ident.
@@ -8400,11 +8496,15 @@ fn cgindex(c: *cgen, n: *node) void = {
return;
};
if (esz == 1) { emitline("\tMOVZBQ\t(AX), AX\n"); }
else { if (esz == 2) {
if (signed_elem) { emitline("\tMOVSWQ\t(AX), AX\n"); }
else { emitline("\tMOVZWQ\t(AX), AX\n"); };
}
else { if (esz == 4) {
if (signed_elem) { emitline("\tMOVSXD\t(AX), AX\n"); }
else { emitline("\tMOVL\t(AX), AX\n"); };
}
else { emitline("\tMOVQ\t(AX), AX\n"); };};
else { emitline("\tMOVQ\t(AX), AX\n"); };};};
return;
};
@@ -8418,9 +8518,19 @@ fn cgslice(c: *cgen, n: *node) void = {
let lo: *node = n.rhs;
let hi: *node = n.cond;
let baselocal: *local = nil;
let globaltn: *node = nil;
let globalname: str;
globalname.ptr = nil; globalname.len = 0;
if (base != nil) {
if (base.kind == nkind.N_IDENT) {
baselocal = localfindnode(c, base.str);
if (baselocal == nil) {
let gt: *node = letvartnode(c, base.str);
if (gt != nil) {
globaltn = gt;
globalname = base.str;
};
};
};
};
// base address
@@ -8439,9 +8549,22 @@ fn cgslice(c: *cgen, n: *node) void = {
emitoff(baselocal.off: i64);
emitline("(BP), AX\n");
};
} else { if (globaltn != nil) {
// Top-level let: [N]T → LEAQ name(SB); pointer/slice/str
// → MOVQ name(SB) (the symbol holds the {ptr,len,cap} or
// {ptr,len} or pointer value).
if (globaltn.kind == nkind.N_TARRAY) {
emitline("\tLEAQ\t");
emitsymname(c, globalname);
emitline("(SB), AX\n");
} else {
emitline("\tMOVQ\t");
emitsymname(c, globalname);
emitline("(SB), AX\n");
};
} else { if (base != nil) {
cgexpr(c, base);
};};
};};};
emitline("\tPUSHQ\tAX\n");
// lo (default 0)
if (lo != nil) { cgexpr(c, lo); }
@@ -8479,9 +8602,29 @@ fn cgslice(c: *cgen, n: *node) void = {
};};};
};
if (!handled) { emitline("\tMOVQ\t$0, AX\n"); };
} else { if (globaltn != nil) {
let handled: bool = false;
if (globaltn.kind == nkind.N_TARRAY) {
let lenn: *node = globaltn.rhs;
if (lenn != nil) {
if (lenn.kind == nkind.N_INTLIT) {
emitline("\tMOVQ\t$");
emituint(lenn.uval);
emitline(", AX\n");
handled = true;
};
};
} else { if (globaltn.kind == nkind.N_TSLICE) {
emitline("\tLEAQ\t");
emitsymname(c, globalname);
emitline("(SB), CX\n");
emitline("\tMOVQ\t8(CX), AX\n");
handled = true;
};};
if (!handled) { emitline("\tMOVQ\t$0, AX\n"); };
} else {
emitline("\tMOVQ\t$0, AX\n");
};};
};};};
emitline("\tMOVQ\tAX, BX\n");
emitline("\tPOPQ\tCX\n");
emitline("\tPOPQ\tAX\n");
@@ -9439,7 +9582,21 @@ fn cgun(c: *cgen, n: *node) void = {
};
cgexpr(c, n.lhs);
if (n.op == tkind.TK_MINUS) { emitline("\tNEGQ\tAX\n"); return; };
if (n.op == tkind.TK_TILDE) { emitline("\tNOTQ\tAX\n"); return; };
if (n.op == tkind.TK_TILDE) {
emitline("\tNOTQ\tAX\n");
// NOTQ inverts the whole 64-bit register; clamp narrow
// unsigned results to type width so subsequent 64-bit
// compares against typed literals agree. u32 uses MOVL r,r
// (zero-extends upper 32) because ANDQ $0xFFFFFFFF would
// sign-extend imm32 to all-ones and act as a no-op.
if (nodeisunsigned(c, n.lhs)) {
let w: i32 = nodeprimwidth(c, n.lhs);
if (w == 1) { emitline("\tANDQ\t$255, AX\n"); };
if (w == 2) { emitline("\tANDQ\t$65535, AX\n"); };
if (w == 4) { emitline("\tMOVL\tAX, AX\n"); };
};
return;
};
if (n.op == tkind.TK_STAR) { emitline("\tMOVQ\t(AX), AX\n"); return; };
if (n.op == tkind.TK_NOT) {
let t: str = mklabel(c, "tt");
@@ -10506,8 +10663,9 @@ fn cgassign(c: *cgen, n: *node) void = {
return;
};
if (esz == 1) { emitline("\tMOVB\tAX, (BX)\n"); }
else { if (esz == 2) { emitline("\tMOVW\tAX, (BX)\n"); }
else { if (esz == 4) { emitline("\tMOVL\tAX, (BX)\n"); }
else { emitline("\tMOVQ\tAX, (BX)\n"); };};
else { emitline("\tMOVQ\tAX, (BX)\n"); };};};
return;
};
};
@@ -14085,7 +14243,16 @@ fn emitletdataw(c: *cgen, file: *node) void = {
emitline("\"\n");
};
};
if (sz == 8 && !issg && fsz == 0) {
// Skip the scalar 8B path when the global is a
// fixed-size array that just happens to sum to 8
// bytes (e.g. [4]u16, [8]u8) — the array path
// below handles it and the duplicate DATAW would
// otherwise differ across stages on user code.
let isarr8: bool = false;
if (d.lhs != nil) {
if (d.lhs.kind == nkind.N_TARRAY) { isarr8 = true; };
};
if (sz == 8 && !issg && fsz == 0 && !isarr8) {
let v: u64 = 0u64;
let ok: bool = true;
if (d.rhs != nil) {

View File

@@ -988,7 +988,16 @@ fn emitletdataw(c: *cgen, file: *node) void = {
emitline("\"\n");
};
};
if (sz == 8 && !issg && fsz == 0) {
// Skip the scalar 8B path when the global is a
// fixed-size array that just happens to sum to 8
// bytes (e.g. [4]u16, [8]u8) — the array path
// below handles it and the duplicate DATAW would
// otherwise differ across stages on user code.
let isarr8: bool = false;
if (d.lhs != nil) {
if (d.lhs.kind == nkind.N_TARRAY) { isarr8 = true; };
};
if (sz == 8 && !issg && fsz == 0 && !isarr8) {
let v: u64 = 0u64;
let ok: bool = true;
if (d.rhs != nil) {

View File

@@ -357,6 +357,23 @@ fn cgcast(c: *cgen, n: *node) void = {
if (dstf32) { dstfk = 1; }
else { if (dstf64) { dstfk = 2; }; };
cgexpr(c, n.lhs);
// str → []T: cgexpr left (AX=ptr, BX=len). Slice register
// convention is (AX=ptr, BX=len, CX=cap); synthesise cap = len
// so downstream arg-push / let-init paths see the canonical
// triple. Detect via dst-is-slice + src-ident's local-tnode
// being str (the common shape; non-ident sources rare).
if (isslicetype(c, n.rhs)) {
let srcstr: bool = false;
if (n.lhs != nil) {
if (n.lhs.kind == nkind.N_IDENT) {
let lc: *local = localfindnode(c, n.lhs.str);
if (lc != nil) {
if (isstrtype(c, lc.tnode)) { srcstr = true; };
};
};
};
if (srcstr) { emitline("\tMOVQ\tBX, CX\n"); };
};
// 0=int, 1=f32, 2=f64. CVT picks one direction per combo;
// same-kind casts (int↔int with widening differences,
// f64→f64 etc.) stay no-ops at the asm level, matching the
@@ -632,11 +649,15 @@ fn cgindex(c: *cgen, n: *node) void = {
return;
};
if (esz == 1) { emitline("\tMOVZBQ\t(BX), AX\n"); }
else { if (esz == 2) {
if (signed_elem) { emitline("\tMOVSWQ\t(BX), AX\n"); }
else { emitline("\tMOVZWQ\t(BX), AX\n"); };
}
else { if (esz == 4) {
if (signed_elem) { emitline("\tMOVSXD\t(BX), AX\n"); }
else { emitline("\tMOVL\t(BX), AX\n"); };
}
else { emitline("\tMOVQ\t(BX), AX\n"); };};
else { emitline("\tMOVQ\t(BX), AX\n"); };};};
return;
};
if (baselocal != nil) {
@@ -675,11 +696,15 @@ fn cgindex(c: *cgen, n: *node) void = {
return;
};
if (esz == 1) { emitline("\tMOVZBQ\t(BX), AX\n"); }
else { if (esz == 2) {
if (signed_elem) { emitline("\tMOVSWQ\t(BX), AX\n"); }
else { emitline("\tMOVZWQ\t(BX), AX\n"); };
}
else { if (esz == 4) {
if (signed_elem) { emitline("\tMOVSXD\t(BX), AX\n"); }
else { emitline("\tMOVL\t(BX), AX\n"); };
}
else { emitline("\tMOVQ\t(BX), AX\n"); };};
else { emitline("\tMOVQ\t(BX), AX\n"); };};};
return;
};
// Generic fallback when base isn't a plain ident.
@@ -706,11 +731,15 @@ fn cgindex(c: *cgen, n: *node) void = {
return;
};
if (esz == 1) { emitline("\tMOVZBQ\t(AX), AX\n"); }
else { if (esz == 2) {
if (signed_elem) { emitline("\tMOVSWQ\t(AX), AX\n"); }
else { emitline("\tMOVZWQ\t(AX), AX\n"); };
}
else { if (esz == 4) {
if (signed_elem) { emitline("\tMOVSXD\t(AX), AX\n"); }
else { emitline("\tMOVL\t(AX), AX\n"); };
}
else { emitline("\tMOVQ\t(AX), AX\n"); };};
else { emitline("\tMOVQ\t(AX), AX\n"); };};};
return;
};
@@ -724,9 +753,19 @@ fn cgslice(c: *cgen, n: *node) void = {
let lo: *node = n.rhs;
let hi: *node = n.cond;
let baselocal: *local = nil;
let globaltn: *node = nil;
let globalname: str;
globalname.ptr = nil; globalname.len = 0;
if (base != nil) {
if (base.kind == nkind.N_IDENT) {
baselocal = localfindnode(c, base.str);
if (baselocal == nil) {
let gt: *node = letvartnode(c, base.str);
if (gt != nil) {
globaltn = gt;
globalname = base.str;
};
};
};
};
// base address
@@ -745,9 +784,22 @@ fn cgslice(c: *cgen, n: *node) void = {
emitoff(baselocal.off: i64);
emitline("(BP), AX\n");
};
} else { if (globaltn != nil) {
// Top-level let: [N]T → LEAQ name(SB); pointer/slice/str
// → MOVQ name(SB) (the symbol holds the {ptr,len,cap} or
// {ptr,len} or pointer value).
if (globaltn.kind == nkind.N_TARRAY) {
emitline("\tLEAQ\t");
emitsymname(c, globalname);
emitline("(SB), AX\n");
} else {
emitline("\tMOVQ\t");
emitsymname(c, globalname);
emitline("(SB), AX\n");
};
} else { if (base != nil) {
cgexpr(c, base);
};};
};};};
emitline("\tPUSHQ\tAX\n");
// lo (default 0)
if (lo != nil) { cgexpr(c, lo); }
@@ -785,9 +837,29 @@ fn cgslice(c: *cgen, n: *node) void = {
};};};
};
if (!handled) { emitline("\tMOVQ\t$0, AX\n"); };
} else { if (globaltn != nil) {
let handled: bool = false;
if (globaltn.kind == nkind.N_TARRAY) {
let lenn: *node = globaltn.rhs;
if (lenn != nil) {
if (lenn.kind == nkind.N_INTLIT) {
emitline("\tMOVQ\t$");
emituint(lenn.uval);
emitline(", AX\n");
handled = true;
};
};
} else { if (globaltn.kind == nkind.N_TSLICE) {
emitline("\tLEAQ\t");
emitsymname(c, globalname);
emitline("(SB), CX\n");
emitline("\tMOVQ\t8(CX), AX\n");
handled = true;
};};
if (!handled) { emitline("\tMOVQ\t$0, AX\n"); };
} else {
emitline("\tMOVQ\t$0, AX\n");
};};
};};};
emitline("\tMOVQ\tAX, BX\n");
emitline("\tPOPQ\tCX\n");
emitline("\tPOPQ\tAX\n");
@@ -1745,7 +1817,21 @@ fn cgun(c: *cgen, n: *node) void = {
};
cgexpr(c, n.lhs);
if (n.op == tkind.TK_MINUS) { emitline("\tNEGQ\tAX\n"); return; };
if (n.op == tkind.TK_TILDE) { emitline("\tNOTQ\tAX\n"); return; };
if (n.op == tkind.TK_TILDE) {
emitline("\tNOTQ\tAX\n");
// NOTQ inverts the whole 64-bit register; clamp narrow
// unsigned results to type width so subsequent 64-bit
// compares against typed literals agree. u32 uses MOVL r,r
// (zero-extends upper 32) because ANDQ $0xFFFFFFFF would
// sign-extend imm32 to all-ones and act as a no-op.
if (nodeisunsigned(c, n.lhs)) {
let w: i32 = nodeprimwidth(c, n.lhs);
if (w == 1) { emitline("\tANDQ\t$255, AX\n"); };
if (w == 2) { emitline("\tANDQ\t$65535, AX\n"); };
if (w == 4) { emitline("\tMOVL\tAX, AX\n"); };
};
return;
};
if (n.op == tkind.TK_STAR) { emitline("\tMOVQ\t(AX), AX\n"); return; };
if (n.op == tkind.TK_NOT) {
let t: str = mklabel(c, "tt");
@@ -2812,8 +2898,9 @@ fn cgassign(c: *cgen, n: *node) void = {
return;
};
if (esz == 1) { emitline("\tMOVB\tAX, (BX)\n"); }
else { if (esz == 2) { emitline("\tMOVW\tAX, (BX)\n"); }
else { if (esz == 4) { emitline("\tMOVL\tAX, (BX)\n"); }
else { emitline("\tMOVQ\tAX, (BX)\n"); };};
else { emitline("\tMOVQ\tAX, (BX)\n"); };};};
return;
};
};

View File

@@ -235,10 +235,20 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = {
let lo: *node = arg.rhs;
let hi: *node = arg.cond;
let baselocal: *local = nil;
let globaltn: *node = nil;
let globalname: str;
globalname.ptr = nil; globalname.len = 0;
if (base != nil) {
if (base.kind == nkind.N_IDENT) {
let bn: str = base.str;
baselocal = localfindnode(c, bn);
if (baselocal == nil) {
let gt: *node = letvartnode(c, bn);
if (gt != nil) {
globaltn = gt;
globalname = bn;
};
};
};
};
// base address → push
@@ -259,9 +269,19 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = {
emitoff(baselocal.off: i64);
emitline("(BP), AX\n");
};
} else { if (globaltn != nil) {
if (globaltn.kind == nkind.N_TARRAY) {
emitline("\tLEAQ\t");
emitsymname(c, globalname);
emitline("(SB), AX\n");
} else {
emitline("\tMOVQ\t");
emitsymname(c, globalname);
emitline("(SB), AX\n");
};
} else {
cgexpr(c, base);
};
};};
emitline("\tPUSHQ\tAX\n");
// hi (default base length) → push
if (hi != nil) {
@@ -290,9 +310,25 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = {
};
};};};
};
} else { if (globaltn != nil) {
if (globaltn.kind == nkind.N_TARRAY) {
let lenn: *node = globaltn.rhs;
if (lenn != nil) {
if (lenn.kind == nkind.N_INTLIT) {
emitline("\tMOVQ\t$");
emituint(lenn.uval);
emitline(", AX\n");
};
};
} else { if (globaltn.kind == nkind.N_TSLICE) {
emitline("\tLEAQ\t");
emitsymname(c, globalname);
emitline("(SB), CX\n");
emitline("\tMOVQ\t8(CX), AX\n");
};};
} else {
emitline("\tMOVQ\t$0, AX\n");
};};
};};};
emitline("\tPUSHQ\tAX\n");
// lo (default 0) → AX
if (lo != nil) { cgexpr(c, lo); }
@@ -376,6 +412,7 @@ fn nodeisslice(c: *cgen, n: *node) bool = {
return false;
};
if (k == nkind.N_SLICE) { return true; };
if (k == nkind.N_CAST) { return isslicetype(c, n.rhs); };
return false;
};
@@ -733,6 +770,11 @@ fn elemsizeof(t: *node) i32 = {
return 1;
};
if (elem == nil) { return 1; };
// `*[N]T`: drill through the pointer into the array's element so
// indexing scales by T's width, not the whole-array byte size.
if (elem.kind == nkind.N_TARRAY) {
if (elem.lhs != nil) { elem = elem.lhs; };
};
if (elem.kind == nkind.N_TNAME) {
let nm: str = elem.str;
// str element is 16B (ptr+len). primsize returns 0 for it.
@@ -855,6 +897,35 @@ fn nodeisunsigned(c: *cgen, n: *node) bool = {
};
return false;
};
// nodeprimwidth — primitive byte width of an expression, or 0 if not
// statically determinable. Mirrors nodeisunsigned's structural walk.
// Used by cgun TK_TILDE to clamp narrow unsigned ~ results to type
// width (NOTQ inverts the full 64-bit register).
fn nodeprimwidth(c: *cgen, n: *node) i32 = {
if (n == nil) { return 0; };
let k: nkind = n.kind;
if (k == nkind.N_IDENT) {
let lc: *local = localfindnode(c, n.str);
if (lc != nil) {
let tn: *node = lc.tnode;
if (tn != nil) {
if (tn.kind == nkind.N_TNAME) { return primsize(tn.str); };
};
};
return 0;
};
if (k == nkind.N_CAST) {
let tn: *node = n.rhs;
if (tn != nil) {
if (tn.kind == nkind.N_TNAME) { return primsize(tn.str); };
};
return 0;
};
if (k == nkind.N_UN) { return nodeprimwidth(c, n.lhs); };
return 0;
};
// ---- type-driven slot sizing ----------------------------------------
fn structlookup(c: *cgen, name: str) *structinfo = {

View File

@@ -5789,10 +5789,20 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = {
let lo: *node = arg.rhs;
let hi: *node = arg.cond;
let baselocal: *local = nil;
let globaltn: *node = nil;
let globalname: str;
globalname.ptr = nil; globalname.len = 0;
if (base != nil) {
if (base.kind == nkind.N_IDENT) {
let bn: str = base.str;
baselocal = localfindnode(c, bn);
if (baselocal == nil) {
let gt: *node = letvartnode(c, bn);
if (gt != nil) {
globaltn = gt;
globalname = bn;
};
};
};
};
// base address → push
@@ -5813,9 +5823,19 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = {
emitoff(baselocal.off: i64);
emitline("(BP), AX\n");
};
} else { if (globaltn != nil) {
if (globaltn.kind == nkind.N_TARRAY) {
emitline("\tLEAQ\t");
emitsymname(c, globalname);
emitline("(SB), AX\n");
} else {
emitline("\tMOVQ\t");
emitsymname(c, globalname);
emitline("(SB), AX\n");
};
} else {
cgexpr(c, base);
};
};};
emitline("\tPUSHQ\tAX\n");
// hi (default base length) → push
if (hi != nil) {
@@ -5844,9 +5864,25 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = {
};
};};};
};
} else { if (globaltn != nil) {
if (globaltn.kind == nkind.N_TARRAY) {
let lenn: *node = globaltn.rhs;
if (lenn != nil) {
if (lenn.kind == nkind.N_INTLIT) {
emitline("\tMOVQ\t$");
emituint(lenn.uval);
emitline(", AX\n");
};
};
} else { if (globaltn.kind == nkind.N_TSLICE) {
emitline("\tLEAQ\t");
emitsymname(c, globalname);
emitline("(SB), CX\n");
emitline("\tMOVQ\t8(CX), AX\n");
};};
} else {
emitline("\tMOVQ\t$0, AX\n");
};};
};};};
emitline("\tPUSHQ\tAX\n");
// lo (default 0) → AX
if (lo != nil) { cgexpr(c, lo); }
@@ -5930,6 +5966,7 @@ fn nodeisslice(c: *cgen, n: *node) bool = {
return false;
};
if (k == nkind.N_SLICE) { return true; };
if (k == nkind.N_CAST) { return isslicetype(c, n.rhs); };
return false;
};
@@ -6287,6 +6324,11 @@ fn elemsizeof(t: *node) i32 = {
return 1;
};
if (elem == nil) { return 1; };
// `*[N]T`: drill through the pointer into the array's element so
// indexing scales by T's width, not the whole-array byte size.
if (elem.kind == nkind.N_TARRAY) {
if (elem.lhs != nil) { elem = elem.lhs; };
};
if (elem.kind == nkind.N_TNAME) {
let nm: str = elem.str;
// str element is 16B (ptr+len). primsize returns 0 for it.
@@ -6409,6 +6451,35 @@ fn nodeisunsigned(c: *cgen, n: *node) bool = {
};
return false;
};
// nodeprimwidth — primitive byte width of an expression, or 0 if not
// statically determinable. Mirrors nodeisunsigned's structural walk.
// Used by cgun TK_TILDE to clamp narrow unsigned ~ results to type
// width (NOTQ inverts the full 64-bit register).
fn nodeprimwidth(c: *cgen, n: *node) i32 = {
if (n == nil) { return 0; };
let k: nkind = n.kind;
if (k == nkind.N_IDENT) {
let lc: *local = localfindnode(c, n.str);
if (lc != nil) {
let tn: *node = lc.tnode;
if (tn != nil) {
if (tn.kind == nkind.N_TNAME) { return primsize(tn.str); };
};
};
return 0;
};
if (k == nkind.N_CAST) {
let tn: *node = n.rhs;
if (tn != nil) {
if (tn.kind == nkind.N_TNAME) { return primsize(tn.str); };
};
return 0;
};
if (k == nkind.N_UN) { return nodeprimwidth(c, n.lhs); };
return 0;
};
// ---- type-driven slot sizing ----------------------------------------
fn structlookup(c: *cgen, name: str) *structinfo = {
@@ -8051,6 +8122,23 @@ fn cgcast(c: *cgen, n: *node) void = {
if (dstf32) { dstfk = 1; }
else { if (dstf64) { dstfk = 2; }; };
cgexpr(c, n.lhs);
// str → []T: cgexpr left (AX=ptr, BX=len). Slice register
// convention is (AX=ptr, BX=len, CX=cap); synthesise cap = len
// so downstream arg-push / let-init paths see the canonical
// triple. Detect via dst-is-slice + src-ident's local-tnode
// being str (the common shape; non-ident sources rare).
if (isslicetype(c, n.rhs)) {
let srcstr: bool = false;
if (n.lhs != nil) {
if (n.lhs.kind == nkind.N_IDENT) {
let lc: *local = localfindnode(c, n.lhs.str);
if (lc != nil) {
if (isstrtype(c, lc.tnode)) { srcstr = true; };
};
};
};
if (srcstr) { emitline("\tMOVQ\tBX, CX\n"); };
};
// 0=int, 1=f32, 2=f64. CVT picks one direction per combo;
// same-kind casts (int↔int with widening differences,
// f64→f64 etc.) stay no-ops at the asm level, matching the
@@ -8326,11 +8414,15 @@ fn cgindex(c: *cgen, n: *node) void = {
return;
};
if (esz == 1) { emitline("\tMOVZBQ\t(BX), AX\n"); }
else { if (esz == 2) {
if (signed_elem) { emitline("\tMOVSWQ\t(BX), AX\n"); }
else { emitline("\tMOVZWQ\t(BX), AX\n"); };
}
else { if (esz == 4) {
if (signed_elem) { emitline("\tMOVSXD\t(BX), AX\n"); }
else { emitline("\tMOVL\t(BX), AX\n"); };
}
else { emitline("\tMOVQ\t(BX), AX\n"); };};
else { emitline("\tMOVQ\t(BX), AX\n"); };};};
return;
};
if (baselocal != nil) {
@@ -8369,11 +8461,15 @@ fn cgindex(c: *cgen, n: *node) void = {
return;
};
if (esz == 1) { emitline("\tMOVZBQ\t(BX), AX\n"); }
else { if (esz == 2) {
if (signed_elem) { emitline("\tMOVSWQ\t(BX), AX\n"); }
else { emitline("\tMOVZWQ\t(BX), AX\n"); };
}
else { if (esz == 4) {
if (signed_elem) { emitline("\tMOVSXD\t(BX), AX\n"); }
else { emitline("\tMOVL\t(BX), AX\n"); };
}
else { emitline("\tMOVQ\t(BX), AX\n"); };};
else { emitline("\tMOVQ\t(BX), AX\n"); };};};
return;
};
// Generic fallback when base isn't a plain ident.
@@ -8400,11 +8496,15 @@ fn cgindex(c: *cgen, n: *node) void = {
return;
};
if (esz == 1) { emitline("\tMOVZBQ\t(AX), AX\n"); }
else { if (esz == 2) {
if (signed_elem) { emitline("\tMOVSWQ\t(AX), AX\n"); }
else { emitline("\tMOVZWQ\t(AX), AX\n"); };
}
else { if (esz == 4) {
if (signed_elem) { emitline("\tMOVSXD\t(AX), AX\n"); }
else { emitline("\tMOVL\t(AX), AX\n"); };
}
else { emitline("\tMOVQ\t(AX), AX\n"); };};
else { emitline("\tMOVQ\t(AX), AX\n"); };};};
return;
};
@@ -8418,9 +8518,19 @@ fn cgslice(c: *cgen, n: *node) void = {
let lo: *node = n.rhs;
let hi: *node = n.cond;
let baselocal: *local = nil;
let globaltn: *node = nil;
let globalname: str;
globalname.ptr = nil; globalname.len = 0;
if (base != nil) {
if (base.kind == nkind.N_IDENT) {
baselocal = localfindnode(c, base.str);
if (baselocal == nil) {
let gt: *node = letvartnode(c, base.str);
if (gt != nil) {
globaltn = gt;
globalname = base.str;
};
};
};
};
// base address
@@ -8439,9 +8549,22 @@ fn cgslice(c: *cgen, n: *node) void = {
emitoff(baselocal.off: i64);
emitline("(BP), AX\n");
};
} else { if (globaltn != nil) {
// Top-level let: [N]T → LEAQ name(SB); pointer/slice/str
// → MOVQ name(SB) (the symbol holds the {ptr,len,cap} or
// {ptr,len} or pointer value).
if (globaltn.kind == nkind.N_TARRAY) {
emitline("\tLEAQ\t");
emitsymname(c, globalname);
emitline("(SB), AX\n");
} else {
emitline("\tMOVQ\t");
emitsymname(c, globalname);
emitline("(SB), AX\n");
};
} else { if (base != nil) {
cgexpr(c, base);
};};
};};};
emitline("\tPUSHQ\tAX\n");
// lo (default 0)
if (lo != nil) { cgexpr(c, lo); }
@@ -8479,9 +8602,29 @@ fn cgslice(c: *cgen, n: *node) void = {
};};};
};
if (!handled) { emitline("\tMOVQ\t$0, AX\n"); };
} else { if (globaltn != nil) {
let handled: bool = false;
if (globaltn.kind == nkind.N_TARRAY) {
let lenn: *node = globaltn.rhs;
if (lenn != nil) {
if (lenn.kind == nkind.N_INTLIT) {
emitline("\tMOVQ\t$");
emituint(lenn.uval);
emitline(", AX\n");
handled = true;
};
};
} else { if (globaltn.kind == nkind.N_TSLICE) {
emitline("\tLEAQ\t");
emitsymname(c, globalname);
emitline("(SB), CX\n");
emitline("\tMOVQ\t8(CX), AX\n");
handled = true;
};};
if (!handled) { emitline("\tMOVQ\t$0, AX\n"); };
} else {
emitline("\tMOVQ\t$0, AX\n");
};};
};};};
emitline("\tMOVQ\tAX, BX\n");
emitline("\tPOPQ\tCX\n");
emitline("\tPOPQ\tAX\n");
@@ -9439,7 +9582,21 @@ fn cgun(c: *cgen, n: *node) void = {
};
cgexpr(c, n.lhs);
if (n.op == tkind.TK_MINUS) { emitline("\tNEGQ\tAX\n"); return; };
if (n.op == tkind.TK_TILDE) { emitline("\tNOTQ\tAX\n"); return; };
if (n.op == tkind.TK_TILDE) {
emitline("\tNOTQ\tAX\n");
// NOTQ inverts the whole 64-bit register; clamp narrow
// unsigned results to type width so subsequent 64-bit
// compares against typed literals agree. u32 uses MOVL r,r
// (zero-extends upper 32) because ANDQ $0xFFFFFFFF would
// sign-extend imm32 to all-ones and act as a no-op.
if (nodeisunsigned(c, n.lhs)) {
let w: i32 = nodeprimwidth(c, n.lhs);
if (w == 1) { emitline("\tANDQ\t$255, AX\n"); };
if (w == 2) { emitline("\tANDQ\t$65535, AX\n"); };
if (w == 4) { emitline("\tMOVL\tAX, AX\n"); };
};
return;
};
if (n.op == tkind.TK_STAR) { emitline("\tMOVQ\t(AX), AX\n"); return; };
if (n.op == tkind.TK_NOT) {
let t: str = mklabel(c, "tt");
@@ -10506,8 +10663,9 @@ fn cgassign(c: *cgen, n: *node) void = {
return;
};
if (esz == 1) { emitline("\tMOVB\tAX, (BX)\n"); }
else { if (esz == 2) { emitline("\tMOVW\tAX, (BX)\n"); }
else { if (esz == 4) { emitline("\tMOVL\tAX, (BX)\n"); }
else { emitline("\tMOVQ\tAX, (BX)\n"); };};
else { emitline("\tMOVQ\tAX, (BX)\n"); };};};
return;
};
};
@@ -14085,7 +14243,16 @@ fn emitletdataw(c: *cgen, file: *node) void = {
emitline("\"\n");
};
};
if (sz == 8 && !issg && fsz == 0) {
// Skip the scalar 8B path when the global is a
// fixed-size array that just happens to sum to 8
// bytes (e.g. [4]u16, [8]u8) — the array path
// below handles it and the duplicate DATAW would
// otherwise differ across stages on user code.
let isarr8: bool = false;
if (d.lhs != nil) {
if (d.lhs.kind == nkind.N_TARRAY) { isarr8 = true; };
};
if (sz == 8 && !issg && fsz == 0 && !isarr8) {
let v: u64 = 0u64;
let ok: bool = true;
if (d.rhs != nil) {

View File

@@ -23,6 +23,9 @@ static const char *modules[] = {
"lib/encoding/utf8/utf8.ww",
"lib/encoding/hex/hex.ww",
"lib/hash/fnv/fnv.ww",
"lib/hash/adler32/adler32.ww",
"lib/hash/crc16/crc16.ww",
"lib/hash/crc32/crc32.ww",
"lib/time/time.ww",
"lib/c/libc/libc.ww",
"lib/bufio/bufio.ww",