selfhost/cmd/w6a: drop snake_case from lex/parse/asm/obj exports
This commit is contained in:
@@ -15,7 +15,7 @@ use mem;
|
||||
use lex;
|
||||
use types;
|
||||
|
||||
fn streq_lit(p: *u8, n: u64, lit: str) bool = {
|
||||
fn streqlit(p: *u8, n: u64, lit: str) bool = {
|
||||
if (n != lit.len: u64) { return false; };
|
||||
let i: u64 = 0u64;
|
||||
for (i < n) {
|
||||
@@ -26,109 +26,109 @@ fn streq_lit(p: *u8, n: u64, lit: str) bool = {
|
||||
return true;
|
||||
};
|
||||
|
||||
// opcode_lookup — name (length-bounded *u8) → A_*. Returns 0 (A_NOP)
|
||||
// opcodelookup — name (length-bounded *u8) → A_*. Returns 0 (A_NOP)
|
||||
// if not found.
|
||||
fn opcode_lookup(p: *u8, n: u64) i32 = {
|
||||
if (streq_lit(p, n, "MOVQ")) { return A_MOVQ; };
|
||||
if (streq_lit(p, n, "MOVL")) { return A_MOVL; };
|
||||
if (streq_lit(p, n, "MOVB")) { return A_MOVB; };
|
||||
if (streq_lit(p, n, "MOVZBQ")) { return A_MOVZBQ; };
|
||||
if (streq_lit(p, n, "MOVSXD")) { return A_MOVSXD; };
|
||||
if (streq_lit(p, n, "MOVSD")) { return A_MOVSD; };
|
||||
if (streq_lit(p, n, "ADDSD")) { return A_ADDSD; };
|
||||
if (streq_lit(p, n, "SUBSD")) { return A_SUBSD; };
|
||||
if (streq_lit(p, n, "MULSD")) { return A_MULSD; };
|
||||
if (streq_lit(p, n, "DIVSD")) { return A_DIVSD; };
|
||||
if (streq_lit(p, n, "UCOMISD")) { return A_UCOMISD; };
|
||||
if (streq_lit(p, n, "CVTTSD2SI")) { return A_CVTTSD2SI; };
|
||||
if (streq_lit(p, n, "CVTSI2SD")) { return A_CVTSI2SD; };
|
||||
if (streq_lit(p, n, "MOVSS")) { return A_MOVSS; };
|
||||
if (streq_lit(p, n, "ADDSS")) { return A_ADDSS; };
|
||||
if (streq_lit(p, n, "SUBSS")) { return A_SUBSS; };
|
||||
if (streq_lit(p, n, "MULSS")) { return A_MULSS; };
|
||||
if (streq_lit(p, n, "DIVSS")) { return A_DIVSS; };
|
||||
if (streq_lit(p, n, "UCOMISS")) { return A_UCOMISS; };
|
||||
if (streq_lit(p, n, "CVTTSS2SI")) { return A_CVTTSS2SI; };
|
||||
if (streq_lit(p, n, "CVTSI2SS")) { return A_CVTSI2SS; };
|
||||
if (streq_lit(p, n, "CVTSD2SS")) { return A_CVTSD2SS; };
|
||||
if (streq_lit(p, n, "CVTSS2SD")) { return A_CVTSS2SD; };
|
||||
if (streq_lit(p, n, "ADDQ")) { return A_ADDQ; };
|
||||
if (streq_lit(p, n, "SUBQ")) { return A_SUBQ; };
|
||||
if (streq_lit(p, n, "IMULQ")) { return A_IMULQ; };
|
||||
if (streq_lit(p, n, "IDIVQ")) { return A_IDIVQ; };
|
||||
if (streq_lit(p, n, "DIVQ")) { return A_DIVQ; };
|
||||
if (streq_lit(p, n, "NEGQ")) { return A_NEGQ; };
|
||||
if (streq_lit(p, n, "NOTQ")) { return A_NOTQ; };
|
||||
if (streq_lit(p, n, "ANDQ")) { return A_ANDQ; };
|
||||
if (streq_lit(p, n, "ORQ")) { return A_ORQ; };
|
||||
if (streq_lit(p, n, "XORQ")) { return A_XORQ; };
|
||||
if (streq_lit(p, n, "SHLQ")) { return A_SHLQ; };
|
||||
if (streq_lit(p, n, "SHRQ")) { return A_SHRQ; };
|
||||
if (streq_lit(p, n, "CMPQ")) { return A_CMPQ; };
|
||||
if (streq_lit(p, n, "PUSHQ")) { return A_PUSHQ; };
|
||||
if (streq_lit(p, n, "POPQ")) { return A_POPQ; };
|
||||
if (streq_lit(p, n, "LEAQ")) { return A_LEAQ; };
|
||||
if (streq_lit(p, n, "CALL")) { return A_CALL; };
|
||||
if (streq_lit(p, n, "RET")) { return A_RET; };
|
||||
if (streq_lit(p, n, "JMP")) { return A_JMP; };
|
||||
if (streq_lit(p, n, "JE")) { return A_JE; };
|
||||
if (streq_lit(p, n, "JNE")) { return A_JNE; };
|
||||
if (streq_lit(p, n, "JL")) { return A_JL; };
|
||||
if (streq_lit(p, n, "JLE")) { return A_JLE; };
|
||||
if (streq_lit(p, n, "JG")) { return A_JG; };
|
||||
if (streq_lit(p, n, "JGE")) { return A_JGE; };
|
||||
if (streq_lit(p, n, "JB")) { return A_JB; };
|
||||
if (streq_lit(p, n, "JBE")) { return A_JBE; };
|
||||
if (streq_lit(p, n, "JA")) { return A_JA; };
|
||||
if (streq_lit(p, n, "JAE")) { return A_JAE; };
|
||||
if (streq_lit(p, n, "JZ")) { return A_JZ; };
|
||||
if (streq_lit(p, n, "JNZ")) { return A_JNZ; };
|
||||
if (streq_lit(p, n, "SYSCALL")) { return A_SYSCALL; };
|
||||
if (streq_lit(p, n, "TEXT")) { return A_TEXT; };
|
||||
if (streq_lit(p, n, "DATA")) { return A_DATA; };
|
||||
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, "MOVB")) { return A_MOVB; };
|
||||
if (streqlit(p, n, "MOVZBQ")) { return A_MOVZBQ; };
|
||||
if (streqlit(p, n, "MOVSXD")) { return A_MOVSXD; };
|
||||
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, "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, "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, "SYSCALL")) { return A_SYSCALL; };
|
||||
if (streqlit(p, n, "TEXT")) { return A_TEXT; };
|
||||
if (streqlit(p, n, "DATA")) { return A_DATA; };
|
||||
return A_NOP;
|
||||
};
|
||||
|
||||
// reg_lookup — name → D_*. Returns D_NONE if not found.
|
||||
fn reg_lookup(p: *u8, n: u64) i32 = {
|
||||
if (streq_lit(p, n, "AX")) { return D_AX; };
|
||||
if (streq_lit(p, n, "BX")) { return D_BX; };
|
||||
if (streq_lit(p, n, "CX")) { return D_CX; };
|
||||
if (streq_lit(p, n, "DX")) { return D_DX; };
|
||||
if (streq_lit(p, n, "SP")) { return D_SP; };
|
||||
if (streq_lit(p, n, "BP")) { return D_BP; };
|
||||
if (streq_lit(p, n, "SI")) { return D_SI; };
|
||||
if (streq_lit(p, n, "DI")) { return D_DI; };
|
||||
if (streq_lit(p, n, "R8")) { return D_R8; };
|
||||
if (streq_lit(p, n, "R9")) { return D_R9; };
|
||||
if (streq_lit(p, n, "R10")) { return D_R10; };
|
||||
if (streq_lit(p, n, "R11")) { return D_R11; };
|
||||
if (streq_lit(p, n, "R12")) { return D_R12; };
|
||||
if (streq_lit(p, n, "R13")) { return D_R13; };
|
||||
if (streq_lit(p, n, "R14")) { return D_R14; };
|
||||
if (streq_lit(p, n, "R15")) { return D_R15; };
|
||||
if (streq_lit(p, n, "X0")) { return D_X0; };
|
||||
if (streq_lit(p, n, "X1")) { return D_X1; };
|
||||
if (streq_lit(p, n, "X2")) { return D_X2; };
|
||||
if (streq_lit(p, n, "X3")) { return D_X3; };
|
||||
if (streq_lit(p, n, "X4")) { return D_X4; };
|
||||
if (streq_lit(p, n, "X5")) { return D_X5; };
|
||||
if (streq_lit(p, n, "X6")) { return D_X6; };
|
||||
if (streq_lit(p, n, "X7")) { return D_X7; };
|
||||
if (streq_lit(p, n, "X8")) { return D_X8; };
|
||||
if (streq_lit(p, n, "X9")) { return D_X9; };
|
||||
if (streq_lit(p, n, "X10")) { return D_X10; };
|
||||
if (streq_lit(p, n, "X11")) { return D_X11; };
|
||||
if (streq_lit(p, n, "X12")) { return D_X12; };
|
||||
if (streq_lit(p, n, "X13")) { return D_X13; };
|
||||
if (streq_lit(p, n, "X14")) { return D_X14; };
|
||||
if (streq_lit(p, n, "X15")) { return D_X15; };
|
||||
if (streq_lit(p, n, "SB")) { return D_PSB; };
|
||||
if (streq_lit(p, n, "FP")) { return D_PFP; };
|
||||
// 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 a_init(a: *asm_, ar: *arena, file: str, src: *u8, len: u64) void = {
|
||||
export fn init(a: *asm_, ar: *arena, file: str, src: *u8, len: u64) void = {
|
||||
a.a = ar;
|
||||
a.file = file;
|
||||
a.src = src;
|
||||
@@ -146,20 +146,12 @@ export fn a_init(a: *asm_, ar: *arena, file: str, src: *u8, len: u64) void = {
|
||||
a.errs = 0;
|
||||
};
|
||||
|
||||
fn streq_str(a: str, b: str) bool = {
|
||||
if (a.len != b.len) { return false; };
|
||||
let i: i32 = 0;
|
||||
for (i < a.len) {
|
||||
if (a[i] != b[i]) { return false; };
|
||||
i += 1;
|
||||
};
|
||||
return true;
|
||||
};
|
||||
// `streq(str,str)` lives in asm.ww — same bundle, single definition.
|
||||
|
||||
export fn a_intern(a: *asm_, name: str) *asym = {
|
||||
export fn intern(a: *asm_, name: str) *asym = {
|
||||
let s: *asym = a.syms;
|
||||
for (s != nil) {
|
||||
if (streq_str(s.name, name)) { return s; };
|
||||
if (streq(s.name, name)) { return s; };
|
||||
s = s.snext;
|
||||
};
|
||||
let n: *asym = amalloc(a.a, 64u64): *asym;
|
||||
@@ -179,8 +171,8 @@ fn perr(a: *asm_, msg: str) void = {
|
||||
a.errs += 1;
|
||||
};
|
||||
|
||||
// dup_str — copy n bytes from p into a fresh heap str.
|
||||
fn dup_str(a: *arena, p: *u8, n: u64) str = {
|
||||
// dupstr — copy n bytes from p into a fresh heap str.
|
||||
fn dupstr(a: *arena, p: *u8, n: u64) str = {
|
||||
return astrndup(a, p, n);
|
||||
};
|
||||
|
||||
@@ -188,7 +180,7 @@ fn dup_str(a: *arena, p: *u8, n: u64) str = {
|
||||
|
||||
// Read next line into a fresh heap buffer; returns (ptr, len) or (nil,0)
|
||||
// at EOF. Advances a.pos past the newline.
|
||||
fn next_line(a: *asm_) (*u8, u64) = {
|
||||
fn nextline(a: *asm_) (*u8, u64) = {
|
||||
if (a.pos >= a.srclen) { return nil, 0u64; };
|
||||
let start: u64 = a.pos;
|
||||
for (a.pos < a.srclen) {
|
||||
@@ -213,7 +205,7 @@ fn next_line(a: *asm_) (*u8, u64) = {
|
||||
return buf, n;
|
||||
};
|
||||
|
||||
fn skip_ws(p: *u8, off: u64, n: u64) u64 = {
|
||||
fn skipws(p: *u8, off: u64, n: u64) u64 = {
|
||||
let i: u64 = off;
|
||||
for (i < n) {
|
||||
if (p[i] != 32u8) { if (p[i] != 9u8) { return i; }; };
|
||||
@@ -222,16 +214,16 @@ fn skip_ws(p: *u8, off: u64, n: u64) u64 = {
|
||||
return i;
|
||||
};
|
||||
|
||||
// parse_operand — parse one operand from p[off..n), populate out.
|
||||
// parseoperand — parse one operand from p[off..n), populate out.
|
||||
// Returns new offset (clamped to n on error).
|
||||
fn parse_operand(a: *asm_, p: *u8, off_in: u64, n: u64, out: *aoperand) u64 = {
|
||||
let off: u64 = skip_ws(p, off_in, n);
|
||||
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: str;
|
||||
empty_str.ptr = nil; empty_str.len = 0;
|
||||
out.asym = empty_str;
|
||||
let empty: str;
|
||||
empty.ptr = nil; empty.len = 0;
|
||||
out.asym = empty;
|
||||
if (off >= n) { return off; };
|
||||
let c0: u8 = p[off];
|
||||
|
||||
@@ -240,7 +232,7 @@ fn parse_operand(a: *asm_, p: *u8, off_in: u64, n: u64, out: *aoperand) u64 = {
|
||||
off += 1u64;
|
||||
let v: i64;
|
||||
let used: u64;
|
||||
v, used = a_parsenum(p + off, n - off);
|
||||
v, used = parsenum(p + off, n - off);
|
||||
out.atype = D_CONST;
|
||||
out.offset = v;
|
||||
return off + used;
|
||||
@@ -254,7 +246,7 @@ fn parse_operand(a: *asm_, p: *u8, off_in: u64, n: u64, out: *aoperand) u64 = {
|
||||
if (p[off] == 41u8) { off = off; off += 0u64; } // no-op marker
|
||||
else { off += 1u64; continue; };
|
||||
let rn: u64 = off - rstart;
|
||||
let r: i32 = reg_lookup(p + rstart, rn);
|
||||
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;
|
||||
@@ -275,7 +267,7 @@ fn parse_operand(a: *asm_, p: *u8, off_in: u64, n: u64, out: *aoperand) u64 = {
|
||||
if (is_num) {
|
||||
let v: i64;
|
||||
let used: u64;
|
||||
v, used = a_parsenum(p + off, n - off);
|
||||
v, used = parsenum(p + off, n - off);
|
||||
let after: u64 = off + used;
|
||||
if (after < n) { if (p[after] == 40u8) { // '('
|
||||
let rstart: u64 = after + 1u64;
|
||||
@@ -284,7 +276,7 @@ fn parse_operand(a: *asm_, p: *u8, off_in: u64, n: u64, out: *aoperand) u64 = {
|
||||
if (p[cur2] == 41u8) { cur2 = cur2; cur2 += 0u64; }
|
||||
else { cur2 += 1u64; continue; };
|
||||
let rn: u64 = cur2 - rstart;
|
||||
let r: i32 = reg_lookup(p + rstart, rn);
|
||||
let r: i32 = reglookup(p + rstart, rn);
|
||||
if (r == 0) { perr(a, "bad register"); return n; };
|
||||
out.atype = D_INDIR;
|
||||
out.reg = r;
|
||||
@@ -300,10 +292,10 @@ fn parse_operand(a: *asm_, p: *u8, off_in: u64, n: u64, out: *aoperand) u64 = {
|
||||
};
|
||||
|
||||
// IDENT — register, symbol(SB), or branch label
|
||||
if (a_isidstart(c0: i32)) {
|
||||
if (isidstart(c0: i32)) {
|
||||
let istart: u64 = off;
|
||||
for (off < n) {
|
||||
if (a_isidcont(p[off]: i32)) { off += 1u64; continue; };
|
||||
if (isidcont(p[off]: i32)) { off += 1u64; continue; };
|
||||
off = off; off += 0u64; // loop break
|
||||
let in_: u64 = off - istart;
|
||||
// IDENT(SB) — external
|
||||
@@ -314,10 +306,10 @@ fn parse_operand(a: *asm_, p: *u8, off_in: u64, n: u64, out: *aoperand) u64 = {
|
||||
if (p[cur2] == 41u8) { cur2 = cur2; cur2 += 0u64; }
|
||||
else { cur2 += 1u64; continue; };
|
||||
let rn: u64 = cur2 - rstart;
|
||||
let r: i32 = reg_lookup(p + rstart, rn);
|
||||
let r: i32 = reglookup(p + rstart, rn);
|
||||
if (r == D_PSB) {
|
||||
out.atype = D_EXTERN;
|
||||
out.asym = dup_str(a.a, p + istart, in_);
|
||||
out.asym = dupstr(a.a, p + istart, in_);
|
||||
} else {
|
||||
out.atype = D_INDIR;
|
||||
out.reg = r;
|
||||
@@ -328,21 +320,21 @@ fn parse_operand(a: *asm_, p: *u8, off_in: u64, n: u64, out: *aoperand) u64 = {
|
||||
perr(a, "missing ')'");
|
||||
return n;
|
||||
};};
|
||||
let r: i32 = reg_lookup(p + istart, in_);
|
||||
let r: i32 = reglookup(p + istart, in_);
|
||||
if (r != D_NONE) {
|
||||
out.atype = r;
|
||||
return off;
|
||||
};
|
||||
out.atype = D_BRANCH;
|
||||
out.asym = dup_str(a.a, p + istart, in_);
|
||||
out.asym = dupstr(a.a, p + istart, in_);
|
||||
return off;
|
||||
};
|
||||
// EOF inside ident
|
||||
let in_: u64 = off - istart;
|
||||
let r: i32 = reg_lookup(p + istart, in_);
|
||||
let r: i32 = reglookup(p + istart, in_);
|
||||
if (r != D_NONE) { out.atype = r; return off; };
|
||||
out.atype = D_BRANCH;
|
||||
out.asym = dup_str(a.a, p + istart, in_);
|
||||
out.asym = dupstr(a.a, p + istart, in_);
|
||||
return off;
|
||||
};
|
||||
|
||||
@@ -351,7 +343,7 @@ fn parse_operand(a: *asm_, p: *u8, off_in: u64, n: u64, out: *aoperand) u64 = {
|
||||
};
|
||||
|
||||
// Append a fresh aprog to the list with given opcode and label.
|
||||
fn add_prog(a: *asm_, opc: i32, lbl: str) *aprog = {
|
||||
fn addprog(a: *asm_, opc: i32, lbl: str) *aprog = {
|
||||
let pr: *aprog = amalloc(a.a, 96u64): *aprog;
|
||||
pr.as_ = opc;
|
||||
pr.line = a.line;
|
||||
@@ -373,18 +365,18 @@ fn add_prog(a: *asm_, opc: i32, lbl: str) *aprog = {
|
||||
return pr;
|
||||
};
|
||||
|
||||
export fn a_parse(a: *asm_) i32 = {
|
||||
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 = next_line(a);
|
||||
line, n = nextline(a);
|
||||
if (line == nil) { return a.errs; };
|
||||
|
||||
// skip leading ws
|
||||
let i: u64 = skip_ws(line, 0u64, n);
|
||||
let i: u64 = skipws(line, 0u64, n);
|
||||
// blank or //-comment
|
||||
if (i >= n) { a.line += 1; continue; };
|
||||
if (i + 1u64 < n) {
|
||||
@@ -398,19 +390,19 @@ export fn a_parse(a: *asm_) i32 = {
|
||||
// through to mnemonic parsing so e.g. `TEXT foo,$0` (which
|
||||
// also starts with an idchar in column 0) gets parsed.
|
||||
if (line[0u64] != 9u8) {
|
||||
if (a_isidstart(line[i]: i32)) {
|
||||
if (isidstart(line[i]: i32)) {
|
||||
let q: u64 = i;
|
||||
let scan_id: bool = true;
|
||||
for (scan_id) {
|
||||
if (q >= n) { scan_id = false; }
|
||||
else { if (a_isidcont(line[q]: i32)) { q += 1u64; }
|
||||
else { if (isidcont(line[q]: i32)) { q += 1u64; }
|
||||
else { scan_id = false; }; };
|
||||
};
|
||||
if (q < n) { if (line[q] == 58u8) { // ':'
|
||||
let nm: str = dup_str(a.a, line + i, q - i);
|
||||
let nm: str = dupstr(a.a, line + i, q - i);
|
||||
// Pending label gets a NOP prog so addresses pin.
|
||||
if (pending.len > 0) {
|
||||
let np: *aprog = add_prog(a, A_NOP, pending);
|
||||
let np: *aprog = addprog(a, A_NOP, pending);
|
||||
};
|
||||
pending = nm;
|
||||
a.line += 1;
|
||||
@@ -431,7 +423,7 @@ export fn a_parse(a: *asm_) i32 = {
|
||||
else { m += 1u64; }; }; };
|
||||
};
|
||||
let mlen: u64 = m - mstart;
|
||||
let opc: i32 = opcode_lookup(line + mstart, mlen);
|
||||
let opc: i32 = opcodelookup(line + mstart, mlen);
|
||||
if (opc == 0) {
|
||||
if (mlen > 0u64) {
|
||||
perr(a, "unknown opcode");
|
||||
@@ -440,11 +432,11 @@ export fn a_parse(a: *asm_) i32 = {
|
||||
a.line += 1; continue;
|
||||
};
|
||||
|
||||
let pr: *aprog = add_prog(a, opc, pending);
|
||||
let pr: *aprog = addprog(a, opc, pending);
|
||||
pending.ptr = nil; pending.len = 0;
|
||||
|
||||
// Skip ws after mnemonic
|
||||
let r0: u64 = skip_ws(line, m, n);
|
||||
let r0: u64 = skipws(line, m, n);
|
||||
|
||||
if (opc == A_TEXT) {
|
||||
// TEXT name,$framesize — find first ',' as the end of name.
|
||||
@@ -458,14 +450,14 @@ export fn a_parse(a: *asm_) i32 = {
|
||||
};
|
||||
let to_op: *aoperand = pr.to;
|
||||
to_op.atype = D_EXTERN;
|
||||
to_op.asym = dup_str(a.a, line + r0, comma_pos - r0);
|
||||
to_op.asym = dupstr(a.a, line + r0, comma_pos - r0);
|
||||
if (comma_pos < n) {
|
||||
let p2: u64 = comma_pos + 1u64;
|
||||
p2 = skip_ws(line, p2, n);
|
||||
p2 = skipws(line, p2, n);
|
||||
if (p2 < n) { if (line[p2] == 36u8) { p2 += 1u64; }; };
|
||||
let v: i64;
|
||||
let used: u64;
|
||||
v, used = a_parsenum(line + p2, n - p2);
|
||||
v, used = parsenum(line + p2, n - p2);
|
||||
let from_op: *aoperand = pr.from;
|
||||
from_op.atype = D_CONST;
|
||||
from_op.offset = v;
|
||||
@@ -485,7 +477,7 @@ export fn a_parse(a: *asm_) i32 = {
|
||||
};
|
||||
let to_op: *aoperand = pr.to;
|
||||
to_op.atype = D_EXTERN;
|
||||
to_op.asym = dup_str(a.a, line + r0, lparen - r0);
|
||||
to_op.asym = dupstr(a.a, line + r0, lparen - r0);
|
||||
// Skip past `(SB)` to land just after ')'.
|
||||
let p2: u64 = lparen;
|
||||
let scan_d2: bool = true;
|
||||
@@ -571,10 +563,10 @@ export fn a_parse(a: *asm_) i32 = {
|
||||
};
|
||||
if (comma >= 0i64) {
|
||||
let cu: u64 = comma: u64;
|
||||
parse_operand(a, line, r0, cu, pr.from);
|
||||
parse_operand(a, line + (cu + 1u64), 0u64, n - (cu + 1u64), pr.to);
|
||||
parseoperand(a, line, r0, cu, pr.from);
|
||||
parseoperand(a, line + (cu + 1u64), 0u64, n - (cu + 1u64), pr.to);
|
||||
} else { if (r0 < n) {
|
||||
parse_operand(a, line, r0, n, pr.to);
|
||||
parseoperand(a, line, r0, n, pr.to);
|
||||
};};
|
||||
|
||||
a.line += 1;
|
||||
|
||||
Reference in New Issue
Block a user