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

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

@@ -0,0 +1,36 @@
// hash/crc64 — CRC-64 checksum. Pure ww.
//
// Same shape as lib/hash/crc32: per-byte inline polynomial shift, no
// precomputed table. Slower than Hare's table-driven path by ~8x per
// byte but produces identical answers for the documented polynomials.
//
// Polynomials are given in reversed form, matching Hare.
def ECMA: u64 = 0xC96C5795D7870F42u64; // ECMA-182, xz-utils
def ISO: u64 = 0xD800000000000000u64; // ISO 3309 HDLC
// sum64 — fold `buf` under `poly` (reversed form). Initial cval is
// ~0u64; per byte we mix in the low byte via 8 polynomial shifts and
// XOR with the high seven bytes shifted down.
export fn sum64(buf: []u8, poly: u64) u64 = {
let c: u64 = 0xFFFFFFFFFFFFFFFFu64;
let i: i32 = 0;
for (i < buf.len) {
let t: u64 = (c & 0xFFu64) ^ (buf[i]: u64);
let z: i32 = 0;
for (z < 8) {
if ((t & 1u64) == 1u64) {
t = (t >> 1u64) ^ poly;
} else {
t = t >> 1u64;
};
z += 1;
};
c = t ^ (c >> 8u64);
i += 1;
};
return ~c;
};
export fn sum64ecma(buf: []u8) u64 = { return sum64(buf, ECMA); };
export fn sum64iso(buf: []u8) u64 = { return sum64(buf, ISO); };

View File

@@ -0,0 +1,66 @@
use crc64;
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, ecma: u64, iso: u64) void = {
let arr: [128]u8;
let n: i32 = putstr(s, arr[0:128], 0);
let buf: []u8 = arr[0:n];
if (crc64.sum64ecma(buf) != ecma) { let _: i32 = 1/0; };
if (crc64.sum64iso(buf) != iso) { let _: i32 = 1/0; };
};
@test fn vec_empty() void = {
let arr: [1]u8;
let buf: []u8 = arr[0:0];
if (crc64.sum64ecma(buf) != 0u64) { let _: i32 = 1/0; };
if (crc64.sum64iso(buf) != 0u64) { let _: i32 = 1/0; };
};
@test fn vec_oscar() void = {
check("Man can believe the impossible, but can never believe the improbable. -- Oscar Wilde",
0x17F71EF3BB851DC7u64, 0xD0E8ED57865D6AC6u64);
};
@test fn vec_hegel() void = {
check("We learn from history that we do not learn from history. -- Georg Hegel",
0xAA3C335BB49ABE9Du64, 0x5FD192CC516BBEC3u64);
};
@test fn vec_chapelain() void = {
check("The final delusion is the belief that one has lost all delusions. -- Maurice Chapelain",
0x7DFC4F7CE9552D23u64, 0xF71B429C925D99AEu64);
};
@test fn vec_unix() void = {
check("UNIX is simple and coherent",
0x9C1938E72C1D8619u64, 0x372ABFFD392FF27Du64);
};
@test fn vec_gnu() void = {
check("GNU's not UNIX",
0x2C0C97B1CB17FCBAu64, 0x9BD1FA95A419A43Du64);
};
@test fn vec_blm() void = {
check("Black lives matter",
0x159BB7B6086BF47Eu64, 0xC218CBB390CF44EBu64);
};
export fn main() i32 = {
vec_empty();
vec_oscar();
vec_hegel();
vec_chapelain();
vec_unix();
vec_gnu();
vec_blm();
return 0;
};