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.
600 lines
18 KiB
Plaintext
600 lines
18 KiB
Plaintext
// selfhost/cmd/w6a/parse.ww — port of cmd/w6a/parse.c.
|
|
//
|
|
// Line-oriented parser for the asm subset emitted by w6c.
|
|
// Grammar:
|
|
// line := blank | comment | label | text | instr
|
|
// blank := /^\s*$/
|
|
// comment := /^\s*\/\/.*$/
|
|
// label := /^IDENT:$/
|
|
// text := TEXT name,$framesize
|
|
// instr := \tMNEM\t[OP1[, OP2]]
|
|
// OP := $NUM | REG | NUM(REG) | (REG) | name(SB) | label
|
|
|
|
package w6a;
|
|
|
|
import os;
|
|
import strings;
|
|
import lex;
|
|
import opcodes;
|
|
|
|
fn streqlit(p: *u8, n: u64, lit: str) bool = {
|
|
if (n != lit.len: u64) { return false; };
|
|
let i: u64 = 0u64;
|
|
for (i < n) {
|
|
let li: i32 = i: i32;
|
|
if (p[i] != lit[li]) { return false; };
|
|
i += 1u64;
|
|
};
|
|
return true;
|
|
};
|
|
|
|
// opcodelookup — name (length-bounded *u8) → A_*. Returns 0 (A_NOP)
|
|
// if not found.
|
|
fn opcodelookup(p: *u8, n: u64) i32 = {
|
|
if (streqlit(p, n, "MOVQ")) { return A_MOVQ; };
|
|
if (streqlit(p, n, "MOVL")) { return A_MOVL; };
|
|
if (streqlit(p, n, "MOVW")) { return A_MOVW; };
|
|
if (streqlit(p, n, "MOVB")) { return A_MOVB; };
|
|
if (streqlit(p, n, "MOVZBQ")) { return A_MOVZBQ; };
|
|
if (streqlit(p, n, "MOVZWQ")) { return A_MOVZWQ; };
|
|
if (streqlit(p, n, "MOVSXD")) { return A_MOVSXD; };
|
|
if (streqlit(p, n, "MOVSWQ")) { return A_MOVSWQ; };
|
|
if (streqlit(p, n, "MOVSBQ")) { return A_MOVSBQ; };
|
|
if (streqlit(p, n, "MOVSD")) { return A_MOVSD; };
|
|
if (streqlit(p, n, "ADDSD")) { return A_ADDSD; };
|
|
if (streqlit(p, n, "SUBSD")) { return A_SUBSD; };
|
|
if (streqlit(p, n, "MULSD")) { return A_MULSD; };
|
|
if (streqlit(p, n, "DIVSD")) { return A_DIVSD; };
|
|
if (streqlit(p, n, "UCOMISD")) { return A_UCOMISD; };
|
|
if (streqlit(p, n, "CVTTSD2SI")) { return A_CVTTSD2SI; };
|
|
if (streqlit(p, n, "CVTSI2SD")) { return A_CVTSI2SD; };
|
|
if (streqlit(p, n, "MOVSS")) { return A_MOVSS; };
|
|
if (streqlit(p, n, "ADDSS")) { return A_ADDSS; };
|
|
if (streqlit(p, n, "SUBSS")) { return A_SUBSS; };
|
|
if (streqlit(p, n, "MULSS")) { return A_MULSS; };
|
|
if (streqlit(p, n, "DIVSS")) { return A_DIVSS; };
|
|
if (streqlit(p, n, "UCOMISS")) { return A_UCOMISS; };
|
|
if (streqlit(p, n, "CVTTSS2SI")) { return A_CVTTSS2SI; };
|
|
if (streqlit(p, n, "CVTSI2SS")) { return A_CVTSI2SS; };
|
|
if (streqlit(p, n, "CVTSD2SS")) { return A_CVTSD2SS; };
|
|
if (streqlit(p, n, "CVTSS2SD")) { return A_CVTSS2SD; };
|
|
if (streqlit(p, n, "ADDQ")) { return A_ADDQ; };
|
|
if (streqlit(p, n, "SUBQ")) { return A_SUBQ; };
|
|
if (streqlit(p, n, "IMULQ")) { return A_IMULQ; };
|
|
if (streqlit(p, n, "IDIVQ")) { return A_IDIVQ; };
|
|
if (streqlit(p, n, "DIVQ")) { return A_DIVQ; };
|
|
if (streqlit(p, n, "CQO")) { return A_CQO; };
|
|
if (streqlit(p, n, "NEGQ")) { return A_NEGQ; };
|
|
if (streqlit(p, n, "NOTQ")) { return A_NOTQ; };
|
|
if (streqlit(p, n, "ANDQ")) { return A_ANDQ; };
|
|
if (streqlit(p, n, "ORQ")) { return A_ORQ; };
|
|
if (streqlit(p, n, "XORQ")) { return A_XORQ; };
|
|
if (streqlit(p, n, "SHLQ")) { return A_SHLQ; };
|
|
if (streqlit(p, n, "SHRQ")) { return A_SHRQ; };
|
|
if (streqlit(p, n, "SARQ")) { return A_SARQ; };
|
|
if (streqlit(p, n, "CMPQ")) { return A_CMPQ; };
|
|
if (streqlit(p, n, "PUSHQ")) { return A_PUSHQ; };
|
|
if (streqlit(p, n, "POPQ")) { return A_POPQ; };
|
|
if (streqlit(p, n, "LEAQ")) { return A_LEAQ; };
|
|
if (streqlit(p, n, "CALL")) { return A_CALL; };
|
|
if (streqlit(p, n, "RET")) { return A_RET; };
|
|
if (streqlit(p, n, "JMP")) { return A_JMP; };
|
|
if (streqlit(p, n, "JE")) { return A_JE; };
|
|
if (streqlit(p, n, "JNE")) { return A_JNE; };
|
|
if (streqlit(p, n, "JL")) { return A_JL; };
|
|
if (streqlit(p, n, "JLE")) { return A_JLE; };
|
|
if (streqlit(p, n, "JG")) { return A_JG; };
|
|
if (streqlit(p, n, "JGE")) { return A_JGE; };
|
|
if (streqlit(p, n, "JB")) { return A_JB; };
|
|
if (streqlit(p, n, "JBE")) { return A_JBE; };
|
|
if (streqlit(p, n, "JA")) { return A_JA; };
|
|
if (streqlit(p, n, "JAE")) { return A_JAE; };
|
|
if (streqlit(p, n, "JZ")) { return A_JZ; };
|
|
if (streqlit(p, n, "JNZ")) { return A_JNZ; };
|
|
if (streqlit(p, n, "JP")) { return A_JP; };
|
|
if (streqlit(p, n, "SYSCALL")) { return A_SYSCALL; };
|
|
if (streqlit(p, n, "TEXT")) { return A_TEXT; };
|
|
if (streqlit(p, n, "DATA")) { return A_DATA; };
|
|
if (streqlit(p, n, "DATAW")) { return A_DATAW; };
|
|
if (streqlit(p, n, "DATAR")) { return A_DATAR; };
|
|
return A_NOP;
|
|
};
|
|
|
|
// reglookup — name → D_*. Returns D_NONE if not found.
|
|
fn reglookup(p: *u8, n: u64) i32 = {
|
|
if (streqlit(p, n, "AX")) { return D_AX; };
|
|
if (streqlit(p, n, "BX")) { return D_BX; };
|
|
if (streqlit(p, n, "CX")) { return D_CX; };
|
|
if (streqlit(p, n, "DX")) { return D_DX; };
|
|
if (streqlit(p, n, "SP")) { return D_SP; };
|
|
if (streqlit(p, n, "BP")) { return D_BP; };
|
|
if (streqlit(p, n, "SI")) { return D_SI; };
|
|
if (streqlit(p, n, "DI")) { return D_DI; };
|
|
if (streqlit(p, n, "R8")) { return D_R8; };
|
|
if (streqlit(p, n, "R9")) { return D_R9; };
|
|
if (streqlit(p, n, "R10")) { return D_R10; };
|
|
if (streqlit(p, n, "R11")) { return D_R11; };
|
|
if (streqlit(p, n, "R12")) { return D_R12; };
|
|
if (streqlit(p, n, "R13")) { return D_R13; };
|
|
if (streqlit(p, n, "R14")) { return D_R14; };
|
|
if (streqlit(p, n, "R15")) { return D_R15; };
|
|
if (streqlit(p, n, "X0")) { return D_X0; };
|
|
if (streqlit(p, n, "X1")) { return D_X1; };
|
|
if (streqlit(p, n, "X2")) { return D_X2; };
|
|
if (streqlit(p, n, "X3")) { return D_X3; };
|
|
if (streqlit(p, n, "X4")) { return D_X4; };
|
|
if (streqlit(p, n, "X5")) { return D_X5; };
|
|
if (streqlit(p, n, "X6")) { return D_X6; };
|
|
if (streqlit(p, n, "X7")) { return D_X7; };
|
|
if (streqlit(p, n, "X8")) { return D_X8; };
|
|
if (streqlit(p, n, "X9")) { return D_X9; };
|
|
if (streqlit(p, n, "X10")) { return D_X10; };
|
|
if (streqlit(p, n, "X11")) { return D_X11; };
|
|
if (streqlit(p, n, "X12")) { return D_X12; };
|
|
if (streqlit(p, n, "X13")) { return D_X13; };
|
|
if (streqlit(p, n, "X14")) { return D_X14; };
|
|
if (streqlit(p, n, "X15")) { return D_X15; };
|
|
if (streqlit(p, n, "SB")) { return D_PSB; };
|
|
if (streqlit(p, n, "FP")) { return D_PFP; };
|
|
return D_NONE;
|
|
};
|
|
|
|
export fn init(a: *asm_, file: str, src: *u8, len: u64) void = {
|
|
a.file = file;
|
|
a.src = src;
|
|
a.srclen = len;
|
|
a.pos = 0u64;
|
|
a.line = 1;
|
|
a.head = nil;
|
|
a.tail = nil;
|
|
a.text = nil;
|
|
a.textcap = 0u64;
|
|
a.textlen = 0u64;
|
|
a.syms = nil;
|
|
a.relocs = nil;
|
|
a.fixups = nil;
|
|
a.errs = 0;
|
|
};
|
|
|
|
export fn intern(a: *asm_, name: str) *asym = {
|
|
let s: *asym = a.syms;
|
|
for (s != nil) {
|
|
if (strings.compare(s.name, name) == 0) { return s; };
|
|
s = s.snext;
|
|
};
|
|
let n: *asym = alloc(asym { name = name, snext = a.syms })!;
|
|
a.syms = n;
|
|
return n;
|
|
};
|
|
|
|
fn perr(a: *asm_, msg: str) void = {
|
|
let pfx: str = "w6a: ";
|
|
os.write(2, pfx.ptr, pfx.len: u64);
|
|
let f: str = a.file;
|
|
os.write(2, f.ptr, f.len: u64);
|
|
let sep: str = ": ";
|
|
os.write(2, sep.ptr, sep.len: u64);
|
|
os.write(2, msg.ptr, msg.len: u64);
|
|
let nl: str = "\n";
|
|
os.write(2, nl.ptr, nl.len: u64);
|
|
a.errs += 1;
|
|
};
|
|
|
|
// dupstr — copy n bytes from p into a fresh heap str.
|
|
fn dupstr(p: *u8, n: u64) str = {
|
|
let view: str;
|
|
view.ptr = p;
|
|
view.len = n: i32;
|
|
return strings.dup(view);
|
|
};
|
|
|
|
// ---- line iteration & whitespace --------------------------------------
|
|
|
|
// Read next line into a fresh heap buffer; returns (ptr, len) or (nil,0)
|
|
// at EOF. Advances a.pos past the newline.
|
|
fn nextline(a: *asm_) (*u8, u64) = {
|
|
if (a.pos >= a.srclen) { return nil, 0u64; };
|
|
let start: u64 = a.pos;
|
|
for (a.pos < a.srclen) {
|
|
if (a.src[a.pos] == '\n') { a.pos = a.pos; a.pos += 0u64; } // no-op; explicit break via condition
|
|
else { a.pos += 1u64; continue; };
|
|
// hit newline
|
|
let n: u64 = a.pos - start;
|
|
let buf: []u8 = alloc([], n + 1u64)!;
|
|
let i: u64 = 0u64;
|
|
for (i < n) { buf[i] = a.src[start + i]; i += 1u64; };
|
|
buf[n] = 0u8;
|
|
a.pos += 1u64; // skip newline
|
|
return buf.ptr, n;
|
|
};
|
|
// EOF without trailing newline
|
|
let n: u64 = a.pos - start;
|
|
if (n == 0u64) { return nil, 0u64; };
|
|
let buf: []u8 = alloc([], n + 1u64)!;
|
|
let i: u64 = 0u64;
|
|
for (i < n) { buf[i] = a.src[start + i]; i += 1u64; };
|
|
buf[n] = 0u8;
|
|
return buf.ptr, n;
|
|
};
|
|
|
|
fn skipws(p: *u8, off: u64, n: u64) u64 = {
|
|
let i: u64 = off;
|
|
for (i < n) {
|
|
if (p[i] != ' ') { if (p[i] != '\t') { return i; }; };
|
|
i += 1u64;
|
|
};
|
|
return i;
|
|
};
|
|
|
|
// parseoperand — parse one operand from p[off..n), populate out.
|
|
// Returns new offset (clamped to n on error).
|
|
fn parseoperand(a: *asm_, p: *u8, offin: u64, n: u64, out: *aoperand) u64 = {
|
|
let off: u64 = skipws(p, offin, n);
|
|
out.atype = D_NONE;
|
|
out.reg = 0;
|
|
out.offset = 0i64;
|
|
let empty: str;
|
|
empty.ptr = nil; empty.len = 0;
|
|
out.asym = empty;
|
|
if (off >= n) { return off; };
|
|
let c0: u8 = p[off];
|
|
|
|
// $NUM
|
|
if (c0 == '$') {
|
|
off += 1u64;
|
|
let v: i64;
|
|
let used: u64;
|
|
v, used = parsenum(p + off, n - off);
|
|
out.atype = D_CONST;
|
|
out.offset = v;
|
|
return off + used;
|
|
};
|
|
|
|
// (REG)
|
|
if (c0 == '(') {
|
|
off += 1u64;
|
|
let rstart: u64 = off;
|
|
for (off < n) {
|
|
if (p[off] == ')') { off = off; off += 0u64; } // no-op marker
|
|
else { off += 1u64; continue; };
|
|
let rn: u64 = off - rstart;
|
|
let r: i32 = reglookup(p + rstart, rn);
|
|
if (r == 0) { perr(a, "bad register in indirect"); return n; };
|
|
out.atype = D_INDIR;
|
|
out.reg = r;
|
|
out.offset = 0i64;
|
|
return off + 1u64; // past ')'
|
|
};
|
|
perr(a, "missing ')' in indirect");
|
|
return n;
|
|
};
|
|
|
|
// number(REG) — possibly signed — or bare $NUM-less constant
|
|
let cur: u64 = off;
|
|
let isnum: bool = false;
|
|
if (cur < n) {
|
|
// 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) {
|
|
let v: i64;
|
|
let used: u64;
|
|
v, used = parsenum(p + off, n - off);
|
|
let after: u64 = off + used;
|
|
if (after < n) { if (p[after] == '(') {
|
|
let rstart: u64 = after + 1u64;
|
|
let cur2: u64 = rstart;
|
|
for (cur2 < n) {
|
|
if (p[cur2] == ')') { cur2 = cur2; cur2 += 0u64; }
|
|
else { cur2 += 1u64; continue; };
|
|
let rn: u64 = cur2 - rstart;
|
|
let r: i32 = reglookup(p + rstart, rn);
|
|
if (r == 0) { perr(a, "bad register"); return n; };
|
|
out.atype = D_INDIR;
|
|
out.reg = r;
|
|
out.offset = v;
|
|
return cur2 + 1u64;
|
|
};
|
|
perr(a, "missing ')'");
|
|
return n;
|
|
};};
|
|
out.atype = D_CONST;
|
|
out.offset = v;
|
|
return after;
|
|
};
|
|
|
|
// IDENT — register, symbol(SB), symbol+disp(SB), or branch label
|
|
if (isidstart(c0: i32)) {
|
|
let istart: u64 = off;
|
|
for (off < n) {
|
|
if (isidcont(p[off]: i32)) { off += 1u64; continue; };
|
|
off = off; off += 0u64; // loop break
|
|
let in_: u64 = off - istart;
|
|
// Optional `+disp` between the ident and `(SB)`. Used
|
|
// by DATAR to address bytes within a previously-defined
|
|
// .data slot (e.g. `DATAR s+8(SB),...`).
|
|
let symdisp: i64 = 0i64;
|
|
if (off < n) { if (p[off] == '+') {
|
|
off += 1u64;
|
|
let v: i64;
|
|
let used: u64;
|
|
v, used = parsenum(p + off, n - off);
|
|
symdisp = v;
|
|
off += used;
|
|
};};
|
|
// IDENT(SB) — external
|
|
if (off < n) { if (p[off] == '(') {
|
|
let rstart: u64 = off + 1u64;
|
|
let cur2: u64 = rstart;
|
|
for (cur2 < n) {
|
|
if (p[cur2] == ')') { cur2 = cur2; cur2 += 0u64; }
|
|
else { cur2 += 1u64; continue; };
|
|
let rn: u64 = cur2 - rstart;
|
|
let r: i32 = reglookup(p + rstart, rn);
|
|
if (r == D_PSB) {
|
|
out.atype = D_EXTERN;
|
|
out.asym = dupstr(p + istart, in_);
|
|
out.offset = symdisp;
|
|
} else {
|
|
out.atype = D_INDIR;
|
|
out.reg = r;
|
|
out.offset = 0i64;
|
|
};
|
|
return cur2 + 1u64;
|
|
};
|
|
perr(a, "missing ')'");
|
|
return n;
|
|
};};
|
|
let r: i32 = reglookup(p + istart, in_);
|
|
if (r != D_NONE) {
|
|
out.atype = r;
|
|
return off;
|
|
};
|
|
out.atype = D_BRANCH;
|
|
out.asym = dupstr(p + istart, in_);
|
|
return off;
|
|
};
|
|
// EOF inside ident
|
|
let in_: u64 = off - istart;
|
|
let r: i32 = reglookup(p + istart, in_);
|
|
if (r != D_NONE) { out.atype = r; return off; };
|
|
out.atype = D_BRANCH;
|
|
out.asym = dupstr(p + istart, in_);
|
|
return off;
|
|
};
|
|
|
|
perr(a, "unrecognised operand");
|
|
return n;
|
|
};
|
|
|
|
// Append a fresh aprog to the list with given opcode and label.
|
|
fn addprog(a: *asm_, opc: i32, lbl: str) *aprog = {
|
|
let pr: *aprog = alloc(aprog { as_ = opc, line = a.line, label = lbl })!;
|
|
pr.from = alloc(aoperand { })!;
|
|
pr.to = alloc(aoperand { })!;
|
|
if (a.head == nil) { a.head = pr; }
|
|
else { a.tail.link = pr; };
|
|
a.tail = pr;
|
|
return pr;
|
|
};
|
|
|
|
export fn parse(a: *asm_) i32 = {
|
|
let pending: str;
|
|
pending.ptr = nil; pending.len = 0;
|
|
|
|
for (true) {
|
|
let line: *u8;
|
|
let n: u64;
|
|
line, n = nextline(a);
|
|
if (line == nil) { return a.errs; };
|
|
|
|
// skip leading ws
|
|
let i: u64 = skipws(line, 0u64, n);
|
|
// blank or //-comment
|
|
if (i >= n) { a.line += 1; continue; };
|
|
if (i + 1u64 < n) {
|
|
if (line[i] == '/') { if (line[i + 1u64] == '/') {
|
|
a.line += 1; continue;
|
|
};};
|
|
};
|
|
|
|
// Label? IDENT: starting at column 0 (no leading tab).
|
|
// Only if the identifier is followed by ':'. Otherwise, fall
|
|
// through to mnemonic parsing so e.g. `TEXT foo,$0` (which
|
|
// also starts with an idchar in column 0) gets parsed.
|
|
if (line[0u64] != '\t') {
|
|
if (isidstart(line[i]: i32)) {
|
|
let q: u64 = i;
|
|
let scanid: bool = true;
|
|
for (scanid) {
|
|
if (q >= n) { scanid = false; }
|
|
else { if (isidcont(line[q]: i32)) { q += 1u64; }
|
|
else { scanid = false; }; };
|
|
};
|
|
if (q < n) { if (line[q] == ':') {
|
|
let nm: str = dupstr(line + i, q - i);
|
|
// Pending label gets a NOP prog so addresses pin.
|
|
if (pending.len > 0) {
|
|
let np: *aprog = addprog(a, A_NOP, pending);
|
|
};
|
|
pending = nm;
|
|
a.line += 1;
|
|
continue;
|
|
};};
|
|
// not a label — fall through to mnemonic parse
|
|
};
|
|
};
|
|
|
|
// MNEMONIC at the start of the rest. Scan to first ws/EOL.
|
|
let mstart: u64 = i;
|
|
let m: u64 = mstart;
|
|
let scan: bool = true;
|
|
for (scan) {
|
|
if (m >= n) { scan = false; }
|
|
else { if (line[m] == ' ') { scan = false; }
|
|
else { if (line[m] == '\t') { scan = false; }
|
|
else { m += 1u64; }; }; };
|
|
};
|
|
let mlen: u64 = m - mstart;
|
|
let opc: i32 = opcodelookup(line + mstart, mlen);
|
|
if (opc == 0) {
|
|
if (mlen > 0u64) {
|
|
perr(a, "unknown opcode");
|
|
};
|
|
pending.ptr = nil; pending.len = 0;
|
|
a.line += 1; continue;
|
|
};
|
|
|
|
let pr: *aprog = addprog(a, opc, pending);
|
|
pending.ptr = nil; pending.len = 0;
|
|
|
|
// Skip ws after mnemonic
|
|
let r0: u64 = skipws(line, m, n);
|
|
|
|
if (opc == A_TEXT) {
|
|
// TEXT name,$framesize — find first ',' as the end of name.
|
|
let q: u64 = r0;
|
|
let commapos: u64 = n;
|
|
let scant: bool = true;
|
|
for (scant) {
|
|
if (q >= n) { scant = false; }
|
|
else { if (line[q] == ',') { commapos = q; scant = false; }
|
|
else { q += 1u64; }; };
|
|
};
|
|
let toop: *aoperand = pr.to;
|
|
toop.atype = D_EXTERN;
|
|
toop.asym = dupstr(line + r0, commapos - r0);
|
|
if (commapos < n) {
|
|
let p2: u64 = commapos + 1u64;
|
|
p2 = skipws(line, p2, n);
|
|
if (p2 < n) { if (line[p2] == '$') { p2 += 1u64; }; };
|
|
let v: i64;
|
|
let used: u64;
|
|
v, used = parsenum(line + p2, n - p2);
|
|
let fromop: *aoperand = pr.from;
|
|
fromop.atype = D_CONST;
|
|
fromop.offset = v;
|
|
};
|
|
a.line += 1; continue;
|
|
};
|
|
|
|
if (opc == A_DATA || opc == A_DATAW) {
|
|
// DATA / DATAW name(SB),"escaped bytes" — same syntax,
|
|
// different destination section (.text vs .data).
|
|
let q: u64 = r0;
|
|
let lparen: u64 = n;
|
|
let scand: bool = true;
|
|
for (scand) {
|
|
if (q >= n) { scand = false; }
|
|
else { if (line[q] == '(') { lparen = q; scand = false; }
|
|
else { q += 1u64; }; };
|
|
};
|
|
let toop: *aoperand = pr.to;
|
|
toop.atype = D_EXTERN;
|
|
toop.asym = dupstr(line + r0, lparen - r0);
|
|
// Skip past `(SB)` to land just after ')'.
|
|
let p2: u64 = lparen;
|
|
let scand2: bool = true;
|
|
for (scand2) {
|
|
if (p2 >= n) { scand2 = false; }
|
|
else { if (line[p2] == ')') { p2 += 1u64; scand2 = false; }
|
|
else { p2 += 1u64; }; };
|
|
};
|
|
// Skip ws / ',' / tab between `)` and the `"`.
|
|
let scand3: bool = true;
|
|
for (scand3) {
|
|
if (p2 >= n) { scand3 = false; }
|
|
else { if (line[p2] == ' ') { p2 += 1u64; }
|
|
else { if (line[p2] == ',') { p2 += 1u64; }
|
|
else { if (line[p2] == '\t') { p2 += 1u64; }
|
|
else { scand3 = false; }; }; }; };
|
|
};
|
|
if (p2 >= n) { perr(a, "DATA missing payload"); a.line += 1; continue; };
|
|
if (line[p2] != '"') { perr(a, "DATA expects \"...\""); a.line += 1; continue; };
|
|
p2 += 1u64; // past opening "
|
|
// Parse escape sequence into a fresh growable buffer.
|
|
let cap: u64 = 32u64;
|
|
let blen: u64 = 0u64;
|
|
let dbuf: []u8 = alloc([], cap)!;
|
|
for (p2 < n) {
|
|
if (line[p2] == '"') { p2 = p2; p2 += 0u64; p2 = n + 1u64; }
|
|
else {
|
|
let ch: u8 = line[p2];
|
|
p2 += 1u64;
|
|
if (ch == '\\') {
|
|
if (p2 < n) {
|
|
let e: u8 = line[p2];
|
|
p2 += 1u64;
|
|
if (e == 'n') { ch = '\n'; }
|
|
else { if (e == 't') { ch = '\t'; }
|
|
else { if (e == 'r') { ch = '\r'; }
|
|
else { if (e == '\\') { ch = '\\'; }
|
|
else { if (e == '"') { ch = '"'; }
|
|
else { if (e == '0') { ch = '\0'; }
|
|
else { if (e == 'x') {
|
|
if (p2 + 1u64 < n) {
|
|
let hi: u8 = line[p2];
|
|
let lo: u8 = line[p2 + 1u64];
|
|
p2 += 2u64;
|
|
let h: u8 = 0u8;
|
|
let l: u8 = 0u8;
|
|
if (hi <= 57u8) { h = hi - 48u8; }
|
|
else { h = (hi | 32u8) - 97u8 + 10u8; };
|
|
if (lo <= 57u8) { l = lo - 48u8; }
|
|
else { l = (lo | 32u8) - 97u8 + 10u8; };
|
|
ch = (h << 4u8) | l;
|
|
};
|
|
}
|
|
else { ch = e; };};};};};};};
|
|
};
|
|
};
|
|
if (blen + 1u64 > cap) {
|
|
let ncap: u64 = cap * 2u64;
|
|
let nb: []u8 = alloc([], ncap)!;
|
|
let bi: u64 = 0u64;
|
|
for (bi < blen) { nb[bi] = dbuf[bi]; bi += 1u64; };
|
|
dbuf = nb;
|
|
cap = ncap;
|
|
};
|
|
dbuf[blen] = ch;
|
|
blen += 1u64;
|
|
};
|
|
};
|
|
pr.bytes = dbuf.ptr;
|
|
pr.nbytes = blen;
|
|
a.line += 1; continue;
|
|
};
|
|
|
|
// Generic instruction: 0/1/2 operands separated by ','.
|
|
// Find top-level comma.
|
|
let comma: i64 = -1i64;
|
|
let q: u64 = r0;
|
|
for (q < n) {
|
|
if (line[q] == ',') {
|
|
if (comma < 0i64) { comma = q: i64; };
|
|
};
|
|
q += 1u64;
|
|
};
|
|
if (comma >= 0i64) {
|
|
let cu: u64 = comma: u64;
|
|
parseoperand(a, line, r0, cu, pr.from);
|
|
parseoperand(a, line + (cu + 1u64), 0u64, n - (cu + 1u64), pr.to);
|
|
} else { if (r0 < n) {
|
|
parseoperand(a, line, r0, n, pr.to);
|
|
};};
|
|
|
|
a.line += 1;
|
|
};
|
|
return a.errs;
|
|
};
|