w6c+wcc+selfhost+lib: int-cast truncate + use_alias, 5 new modules

Two cgen/check bugs surfaced by new lib modules, plus the modules
themselves (crc64, siphash, random, base64, base32).

  1. `(big_u64): u32` (and `: u16`, `: u8`, `: bool`) didn't truncate.
     N_CAST emitted nothing for int↔int; the value stayed in AX with
     its upper bits intact and downstream CMPQ/DIVQ misread the slot.
     The TK_TILDE path already had clamp logic for the same reason —
     N_CAST was the missing case. Both stages now MOVL r,r for u32 and
     ANDQ $mask for u8/u16/bool. Signed-narrow (i8/i16/i32) stays
     no-op until w6a grows reg-reg MOVSBQ/MOVSWQ/MOVSXD. selfhost
     cgcast walks alias chains via aliaslookup before checking
     primsize/typenameisunsigned so `(u: random)` where
     `type random = u64` still bypasses the clamp.
     See cmd/w6c/cgen.c N_CAST and selfhost/cmd/wcc/cgenexpr.ww cgcast.

  2. `mod.mod` type refs (`random.random` when the imported module
     declares `export type random = u64;`) failed with "unknown type".
     The driver concatenates imports into one flat scope, so SK_USE
     `random` collided with SK_TYPE `random` and scope_define silently
     dropped the use. resolve_typename's leaf lookup required
     `kind == SK_USE` and gave up. Adds a `use_alias` flag to Sym; the
     pass-1 decl scan now marks colliding syms in both directions
     (use-after-type and type-after-use). resolve_typename and the
     N_DOT cexpr branch treat `use_alias` like SK_USE for qualified
     lookup. selfhost check.ww was already lenient on this path so no
     ww-side change was needed; bootstrap fixed point (990-995) holds.
     See cmd/wcc/check.c installdecl pass + N_DOT/resolve_typename and
     cmd/wcc/ww.h Sym.use_alias.

New modules under lib/, each with @test vectors in *_test.ww and wired
into test/wcc/900_stdlib.c (26 modules → all compile):

  - lib/hash/crc64       ECMA, ISO  (mirror of crc32 shape)
  - lib/hash/siphash     SipHash-2-4, buffer-based sum/sum24
  - lib/math/random      SplitMix64 (init, next, u32n, u64n)
  - lib/encoding/base64  RFC 4648 std + url-safe encode/decode + sizes
  - lib/encoding/base32  RFC 4648 std + base32hex encode/decode + sizes
This commit is contained in:
2026-05-13 14:58:36 +09:00
parent cbcc0167ae
commit 7a4f60b041
17 changed files with 1302 additions and 23 deletions

View File

@@ -3474,6 +3474,39 @@ cgexpr(Cg *c, Node *n, Local *locals)
ins2(c, A_MOVQ, areg(D_BX), areg(D_CX));
}
}
/* Narrowing integer cast: clamp AX to the target width so
* downstream 64-bit ops see a value within the declared
* range. Hare semantics: `expr: T` truncates to T's bit
* width (mod 2^n). Without this, `(big_u64): u32` left the
* upper 32 bits intact and CMPQ/DIVQ misread the value.
*
* Unsigned targets only here. Signed-narrow targets (i8/
* i16/i32) need MOVSBQ / MOVSWQ / MOVSXD in their reg-reg
* form which the assembler doesn't expose yet; callers
* that need a clean signed-narrow value either keep the
* value in range before the cast (as strconv does with
* an explicit bounds check) or AND the low bits manually.
* Tracking this gap is part of the same TODO. */
if (!from_f && !to_f && n->type) {
Type *tt = n->type;
Type *tu = (tt && tt->kind == TY_NAMED) ? tt->under : tt;
if (tu && type_isint(tu) && tu->size > 0
&& tu->size < 8 && type_isunsigned(tu)) {
if (tu->size == 4) {
ins2(c, A_MOVL, areg(D_AX), areg(D_AX));
} else {
u64 mask = ((u64)1 << (tu->size * 8)) - 1;
ins2(c, A_ANDQ, aimm((i64)mask),
areg(D_AX));
}
}
/* TY_BOOL is size 1 too; clamp to a single byte so
* `(u32_val): bool` produces 0 or a low-byte value
* instead of leaking the upper bits. */
if (tu && tu->kind == TY_BOOL) {
ins2(c, A_ANDQ, aimm(0xFF), areg(D_AX));
}
}
break;
}
case N_DOT: {

View File

@@ -63,14 +63,17 @@ resolve_typename(Checker *c, Node *n)
Sym *s = scope_lookup(c->cur, nm);
if (s == NULL && nm) {
/* module-qualified: io.stream → strip the last dot prefix
* and look up the leaf if `io` is a `use`-imported name. */
* and look up the leaf if `io` is a `use`-imported name.
* `m->use_alias` covers the self-import case where the
* imported module declares a type with the same name as
* the module itself (e.g. `random.random`). */
const char *dot = strrchr(nm, '.');
if (dot) {
char head[128] = {0};
size_t hl = (size_t)(dot - nm);
if (hl < sizeof head) memcpy(head, nm, hl);
Sym *m = scope_lookup(c->cur, head);
if (m && m->kind == SK_USE)
if (m && (m->kind == SK_USE || m->use_alias))
s = scope_lookup(c->cur, dot + 1);
}
}
@@ -684,16 +687,28 @@ cexpr(Checker *c, Node *n)
* scope, so we lookup `n->str` directly. */
if (n->lhs && n->lhs->kind == N_IDENT) {
Sym *ms = scope_lookup(c->cur, n->lhs->str);
if (ms && ms->kind == SK_USE) {
if (ms && (ms->kind == SK_USE || ms->use_alias)) {
/* Module-qualified ref. `use_alias` covers
* the self-import case where the module's
* type name shadowed the SK_USE; the leaf
* still resolves through the flat scope.
* fs == ms is the `mod.mod` case (the
* imported module's leaf type is named after
* the module itself — both names point at
* the same SK_TYPE sym in the flat scope). */
Sym *fs = scope_lookup(c->cur, n->str);
if (fs)
return n->type = fs->type;
/* Leaf isn't in scope here — treat as an
* external declaration. The codegen will
* still emit CALL/MOVQ by the leaf name; the
* linker fails if the symbol is truly
* missing. */
return n->type = ty_err;
if (ms->kind == SK_USE) {
/* Pure SK_USE with missing leaf:
* external declaration. Codegen
* emits CALL/MOVQ by the leaf name
* and the linker resolves it. */
return n->type = ty_err;
}
/* SK_TYPE with use_alias=1 and no leaf
* found: fall through so the enum / type-
* member paths below get a shot. */
}
/* enum member access: TypeName.MEMBER → fold to
* the member's integer literal value. Type is the
@@ -1590,13 +1605,34 @@ check_file(Checker *c, Node *file)
* walked in the next pass. */
for (Node *d = file->list; d; d = d->next) {
if (d->kind == N_USE) {
scope_define(c->cur, d->str, SK_USE, NULL, d);
Sym *prev = scope_lookup_local(c->cur, d->str);
if (prev != NULL) {
/* Self-import: the driver concatenates the
* imported module's source into the flat
* scope, so its top-level decls (types, fns,
* defs) shadow a same-named SK_USE. Mark
* the colliding sym as also-a-use so dotted
* qualifiers (`mod.x`) still resolve. */
prev->use_alias = 1;
} else {
scope_define(c->cur, d->str, SK_USE, NULL, d);
}
continue;
}
if (d->kind != N_TYPEDECL) continue;
Type *named = type_named(c->a, d->str, NULL);
if (!scope_define(c->cur, d->str, SK_TYPE, named, d))
Sym *prev = scope_lookup_local(c->cur, d->str);
if (prev && prev->kind == SK_USE) {
/* `use mod; ... type mod = ...;` — promote the
* SK_USE to the type symbol but remember it was
* also a module name. */
prev->kind = SK_TYPE;
prev->type = named;
prev->decl = d;
prev->use_alias = 1;
} else if (!scope_define(c->cur, d->str, SK_TYPE, named, d)) {
err(c, d->pos, "duplicate type %s", d->str);
}
d->type = named;
}
for (Node *d = file->list; d; d = d->next) {

View File

@@ -476,6 +476,12 @@ struct Sym {
Node *decl;
int exported;
int is_const; /* const-bound (assignment rejected) */
int use_alias; /* also bound as a `use` module name. Set
* when a `use foo;` directive collides
* with a same-named SK_TYPE/SK_FN/etc.
* Lets resolve_typename treat `foo.x`
* as module-qualified even though the
* primary kind isn't SK_USE. */
Sym *next; /* iteration */
Sym *hashnext; /* bucket chain */
Scope *scope;