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:
20
lib/hash/adler32/adler32.ww
Normal file
20
lib/hash/adler32/adler32.ww
Normal 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;
|
||||
};
|
||||
52
lib/hash/adler32/adler32_test.ww
Normal file
52
lib/hash/adler32/adler32_test.ww
Normal 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
42
lib/hash/crc16/crc16.ww
Normal 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); };
|
||||
58
lib/hash/crc16/crc16_test.ww
Normal file
58
lib/hash/crc16/crc16_test.ww
Normal 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
36
lib/hash/crc32/crc32.ww
Normal 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); };
|
||||
62
lib/hash/crc32/crc32_test.ww
Normal file
62
lib/hash/crc32/crc32_test.ww
Normal 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;
|
||||
};
|
||||
Reference in New Issue
Block a user