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.
195 lines
4.8 KiB
C
195 lines
4.8 KiB
C
/*
|
|
* txt.c — print a Prog list as Plan 9-flavoured amd64 asm text.
|
|
*
|
|
* Format we emit (and that w6a expects):
|
|
* TEXT name<framesize>
|
|
* MOVQ $1, AX
|
|
* MOVQ AX, name(SB) ; extern symbol
|
|
* MOVQ off(BP), AX ; local
|
|
* CMPQ AX, $0
|
|
* JE label
|
|
* RET
|
|
* label:
|
|
*
|
|
* Operand order is Plan 9-ish: source first, dest second. (Same as
|
|
* AT&T; the convention diverges from Plan 9 only on a few items we
|
|
* don't yet emit.)
|
|
*/
|
|
#include "gc.h"
|
|
#include <string.h>
|
|
|
|
const char *
|
|
anames(int op)
|
|
{
|
|
switch (op) {
|
|
case A_NOP: return "NOP";
|
|
case A_TEXT: return "TEXT";
|
|
case A_DATA: return "DATA";
|
|
case A_DATAW: return "DATAW";
|
|
case A_DATAR: return "DATAR";
|
|
case A_GLOBL: return "GLOBL";
|
|
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";
|
|
case A_MULSD: return "MULSD";
|
|
case A_DIVSD: return "DIVSD";
|
|
case A_UCOMISD: return "UCOMISD";
|
|
case A_CVTTSD2SI: return "CVTTSD2SI";
|
|
case A_CVTSI2SD:return "CVTSI2SD";
|
|
case A_MOVSS: return "MOVSS";
|
|
case A_ADDSS: return "ADDSS";
|
|
case A_SUBSS: return "SUBSS";
|
|
case A_MULSS: return "MULSS";
|
|
case A_DIVSS: return "DIVSS";
|
|
case A_UCOMISS: return "UCOMISS";
|
|
case A_CVTTSS2SI: return "CVTTSS2SI";
|
|
case A_CVTSI2SS:return "CVTSI2SS";
|
|
case A_CVTSD2SS:return "CVTSD2SS";
|
|
case A_CVTSS2SD:return "CVTSS2SD";
|
|
case A_ADDQ: return "ADDQ";
|
|
case A_SUBQ: return "SUBQ";
|
|
case A_IMULQ: return "IMULQ";
|
|
case A_IDIVQ: return "IDIVQ";
|
|
case A_DIVQ: return "DIVQ";
|
|
case A_NEGQ: return "NEGQ";
|
|
case A_NOTQ: return "NOTQ";
|
|
case A_ANDQ: return "ANDQ";
|
|
case A_ORQ: return "ORQ";
|
|
case A_XORQ: return "XORQ";
|
|
case A_SHLQ: return "SHLQ";
|
|
case A_SHRQ: return "SHRQ";
|
|
case A_CMPQ: return "CMPQ";
|
|
case A_PUSHQ: return "PUSHQ";
|
|
case A_POPQ: return "POPQ";
|
|
case A_LEAQ: return "LEAQ";
|
|
case A_CALL: return "CALL";
|
|
case A_RET: return "RET";
|
|
case A_JMP: return "JMP";
|
|
case A_JE: return "JE";
|
|
case A_JNE: return "JNE";
|
|
case A_JL: return "JL";
|
|
case A_JLE: return "JLE";
|
|
case A_JG: return "JG";
|
|
case A_JGE: return "JGE";
|
|
case A_JB: return "JB";
|
|
case A_JBE: return "JBE";
|
|
case A_JA: return "JA";
|
|
case A_JAE: return "JAE";
|
|
case A_JZ: return "JZ";
|
|
case A_JNZ: return "JNZ";
|
|
case A_SYSCALL: return "SYSCALL";
|
|
}
|
|
return "??";
|
|
}
|
|
|
|
const char *
|
|
rnames(int r)
|
|
{
|
|
switch (r) {
|
|
case D_AX: return "AX"; case D_CX: return "CX";
|
|
case D_DX: return "DX"; case D_BX: return "BX";
|
|
case D_SP: return "SP"; case D_BP: return "BP";
|
|
case D_SI: return "SI"; case D_DI: return "DI";
|
|
case D_R8: return "R8"; case D_R9: return "R9";
|
|
case D_R10: return "R10"; case D_R11: return "R11";
|
|
case D_R12: return "R12"; case D_R13: return "R13";
|
|
case D_R14: return "R14"; case D_R15: return "R15";
|
|
case D_X0: return "X0"; case D_X1: return "X1";
|
|
case D_X2: return "X2"; case D_X3: return "X3";
|
|
case D_X4: return "X4"; case D_X5: return "X5";
|
|
case D_X6: return "X6"; case D_X7: return "X7";
|
|
case D_X8: return "X8"; case D_X9: return "X9";
|
|
case D_X10: return "X10"; case D_X11: return "X11";
|
|
case D_X12: return "X12"; case D_X13: return "X13";
|
|
case D_X14: return "X14"; case D_X15: return "X15";
|
|
case D_PSP: return "SP"; case D_PFP: return "FP"; case D_PSB: return "SB";
|
|
}
|
|
return "?";
|
|
}
|
|
|
|
static void
|
|
prAdr(FILE *f, Adr a)
|
|
{
|
|
switch (a.type) {
|
|
case D_NONE: fputs("?", f); break;
|
|
case D_CONST:
|
|
fprintf(f, "$%lld", a.offset);
|
|
break;
|
|
case D_INDIR:
|
|
if (a.offset)
|
|
fprintf(f, "%lld(%s)", a.offset, rnames(a.reg));
|
|
else
|
|
fprintf(f, "(%s)", rnames(a.reg));
|
|
break;
|
|
case D_BRANCH:
|
|
fputs(a.sym ? a.sym : "?", f);
|
|
break;
|
|
case D_EXTERN:
|
|
fprintf(f, "%s(SB)", a.sym ? a.sym : "?");
|
|
break;
|
|
default:
|
|
fputs(rnames(a.type), f);
|
|
}
|
|
}
|
|
|
|
void
|
|
txt_emit(FILE *f, Prog *head)
|
|
{
|
|
for (Prog *p = head; p; p = p->link) {
|
|
if (p->label) {
|
|
fprintf(f, "%s:\n", p->label);
|
|
if (p->as == A_NOP) continue;
|
|
}
|
|
switch (p->as) {
|
|
case A_NOP:
|
|
break;
|
|
case A_TEXT:
|
|
fprintf(f, "TEXT %s,$%lld\n",
|
|
p->to.sym ? p->to.sym : "?",
|
|
p->from.offset);
|
|
break;
|
|
case A_RET:
|
|
fputs("\tRET\n", f);
|
|
break;
|
|
case A_SYSCALL:
|
|
fputs("\tSYSCALL\n", f);
|
|
break;
|
|
case A_NEGQ:
|
|
case A_NOTQ:
|
|
case A_PUSHQ:
|
|
case A_POPQ:
|
|
case A_IDIVQ:
|
|
case A_DIVQ:
|
|
fprintf(f, "\t%s\t", anames(p->as));
|
|
prAdr(f, p->to);
|
|
fputc('\n', f);
|
|
break;
|
|
case A_CALL:
|
|
case A_JMP:
|
|
case A_JE: case A_JNE:
|
|
case A_JL: case A_JLE: case A_JG: case A_JGE:
|
|
case A_JB: case A_JBE: case A_JA: case A_JAE:
|
|
case A_JZ: case A_JNZ:
|
|
fprintf(f, "\t%s\t", anames(p->as));
|
|
prAdr(f, p->to);
|
|
fputc('\n', f);
|
|
break;
|
|
default:
|
|
fprintf(f, "\t%s\t", anames(p->as));
|
|
prAdr(f, p->from);
|
|
fputs(", ", f);
|
|
prAdr(f, p->to);
|
|
fputc('\n', f);
|
|
}
|
|
}
|
|
}
|