selfhost/cmd/w6a: align parsenum to strtoll(base 0) semantics (#62)

w6a's parsenum diverged from the C twin's strtoll(s,end,0)
(cmd/w6a/lex.c:30) on three hand-written-asm edge shapes (all
gate-blind — w6c emits the canonical $5/$8/-8(BP), never these):
  (a) `$ 5`  — leading whitespace: strtoll skips it (->5); ww had no
              skip and silently encoded imm 0.
  (b) `$08`  — strtoll base-0 reads a leading 0 as octal, stops at '8'
              (->0); ww parsed it as decimal 8.
  (c) `-(BP)` — strtoll/cstage require a digit after the sign, so a bare
              `-(` is unrecognised operand; ww silently took it as 0(BP).
Add the whitespace skip + octal base-0 detection to parsenum, and the
digit-after-sign guard to the operand scanner — both assemblers now
agree byte-for-byte (a/b) and both reject (c).

Not a Hare item (w6a is ww's plan9-lineage assembler); reference is the
C strtoll twin. w6a embeds into its own combined.ww snapshot; regen'd.
530_w6a_parsenum pins the byte-identity + both-reject matrix.
This commit is contained in:
2026-06-13 11:03:42 +09:00
parent a9dcea70ed
commit 559b77db40
5 changed files with 222 additions and 17 deletions

View File

@@ -25,20 +25,29 @@ export fn isidcont(c: i32) bool = {
// Plain Plan 9-style: $123 / $0x1f / $-7. Decimal default; 0x prefix
// for hex; 0 prefix for octal when followed by a digit (else just 0).
export fn parsenum(p: *u8, n: u64) (i64, u64) = {
// strtoll(s, end, 0) semantics, matching the C twin cmd/w6a/lex.c:30:
// skip leading whitespace, optional sign, base-0 prefix detection
// (0x -> hex, leading 0 -> octal, else decimal). w6c never emits the
// `$ 5` / `$08` edge shapes; this aligns the hand-written-asm path
// with cstage so the two assemblers agree byte-for-byte (#62).
let i: u64 = 0u64;
for (i < n) {
if (p[i] != 32u8) { if (p[i] != 9u8) { break; }; };
i += 1u64;
};
let neg: bool = false;
if (i < n) {
if (p[i] == 45u8) { neg = true; i += 1u64; }
else { if (p[i] == 43u8) { i += 1u64; }; };
};
let base: i64 = 10i64;
if (i + 1u64 < n) {
if (p[i] == 48u8) {
if (p[i + 1u64] == 120u8) { base = 16i64; i += 2u64; }
else { if (p[i + 1u64] == 88u8) { base = 16i64; i += 2u64; }
else { if (p[i + 1u64] >= 48u8) { if (p[i + 1u64] <= 55u8) {
base = 8i64; i += 1u64;
};};};};
if (i < n) {
if (p[i] == 48u8) { // leading '0' -> octal, unless '0x'/'0X'
if (i + 1u64 < n) {
if (p[i + 1u64] == 120u8) { base = 16i64; i += 2u64; }
else { if (p[i + 1u64] == 88u8) { base = 16i64; i += 2u64; }
else { base = 8i64; i += 1u64; }; };
} else { base = 8i64; i += 1u64; };
};
};
let v: i64 = 0i64;

View File

@@ -3113,20 +3113,29 @@ export fn isidcont(c: i32) bool = {
// Plain Plan 9-style: $123 / $0x1f / $-7. Decimal default; 0x prefix
// for hex; 0 prefix for octal when followed by a digit (else just 0).
export fn parsenum(p: *u8, n: u64) (i64, u64) = {
// strtoll(s, end, 0) semantics, matching the C twin cmd/w6a/lex.c:30:
// skip leading whitespace, optional sign, base-0 prefix detection
// (0x -> hex, leading 0 -> octal, else decimal). w6c never emits the
// `$ 5` / `$08` edge shapes; this aligns the hand-written-asm path
// with cstage so the two assemblers agree byte-for-byte (#62).
let i: u64 = 0u64;
for (i < n) {
if (p[i] != 32u8) { if (p[i] != 9u8) { break; }; };
i += 1u64;
};
let neg: bool = false;
if (i < n) {
if (p[i] == 45u8) { neg = true; i += 1u64; }
else { if (p[i] == 43u8) { i += 1u64; }; };
};
let base: i64 = 10i64;
if (i + 1u64 < n) {
if (p[i] == 48u8) {
if (p[i + 1u64] == 120u8) { base = 16i64; i += 2u64; }
else { if (p[i + 1u64] == 88u8) { base = 16i64; i += 2u64; }
else { if (p[i + 1u64] >= 48u8) { if (p[i + 1u64] <= 55u8) {
base = 8i64; i += 1u64;
};};};};
if (i < n) {
if (p[i] == 48u8) { // leading '0' -> octal, unless '0x'/'0X'
if (i + 1u64 < n) {
if (p[i + 1u64] == 120u8) { base = 16i64; i += 2u64; }
else { if (p[i + 1u64] == 88u8) { base = 16i64; i += 2u64; }
else { base = 8i64; i += 1u64; }; };
} else { base = 8i64; i += 1u64; };
};
};
let v: i64 = 0i64;
@@ -3430,7 +3439,17 @@ fn parseoperand(a: *asm_, p: *u8, offin: u64, n: u64, out: *aoperand) u64 = {
let cur: u64 = off;
let isnum: bool = false;
if (cur < n) {
if (p[cur] == '-') { isnum = true; }
// cstage parse.c:189-190 strips '-' then requires isdigit(*p);
// a bare `-(BP)` is NOT a number operand and falls through to a
// loud reject. ww treated '-' alone as a number → silent 0(BP).
// Require a following digit to match (#62).
if (p[cur] == '-') {
if (cur + 1u64 < n) {
if (p[cur + 1u64] >= '0') { if (p[cur + 1u64] <= '9') {
isnum = true;
}; };
};
}
else { if (p[cur] >= '0') { if (p[cur] <= '9') { isnum = true; }; }; };
};
if (isnum) {

View File

@@ -273,7 +273,17 @@ fn parseoperand(a: *asm_, p: *u8, offin: u64, n: u64, out: *aoperand) u64 = {
let cur: u64 = off;
let isnum: bool = false;
if (cur < n) {
if (p[cur] == '-') { isnum = true; }
// cstage parse.c:189-190 strips '-' then requires isdigit(*p);
// a bare `-(BP)` is NOT a number operand and falls through to a
// loud reject. ww treated '-' alone as a number → silent 0(BP).
// Require a following digit to match (#62).
if (p[cur] == '-') {
if (cur + 1u64 < n) {
if (p[cur + 1u64] >= '0') { if (p[cur + 1u64] <= '9') {
isnum = true;
}; };
};
}
else { if (p[cur] >= '0') { if (p[cur] <= '9') { isnum = true; }; }; };
};
if (isnum) {