Files
Basys3RiscV/monitor/asm.c
T
brad ArantandClaude Opus 5.5 fa4752fd7a Assembler in GNU as syntax, objdump-style disassembly, UCASE$ and LCASE$
The monitor's assembler takes GNU as directives (.org, .equ, .byte, .half,
.word, .ascii, .string, .space, .align, .balign ...), # comments, 0x/0b
numbers, '\n' constants, . for the current address, << >> ^ ~, %hi/%lo and
the FP CSR short forms; the old ORG/DB/EQU/0FFH forms remain as aliases. The
disassembler prints lower case and 0x hex as objdump does. The monitor is
built with -msave-restore and puts .rodata first, to fit its ROM.
BASIC gains UCASE$ and LCASE$. Manual, README and tests updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 18:14:19 -08:00

762 lines
30 KiB
C

/* RISC-V assembler for the monitor (included by monitor.c).
*
* GNU as syntax, as the disassembler prints it. A line is
* [line number] [label:] [instruction or directive] [# comment]
* (the line number is there when the source was typed into BASIC; ; also
* starts a comment). Case doesn't matter: lines are upper-cased outside
* quotes before they are read. Numbers: 123, 0x1F, 0b1010, 'A', '\n'; . is
* the current address. Expressions use + - * / & | ^ << >> ~ and parentheses,
* and %hi(x) and %lo(x), the LUI/AUIPC and ADDI parts of x. Registers are
* x0-x31 or their ABI names (zero ra sp gp tp t0-t6 s0-s11 fp a0-a7); memory
* operands are offset(register). CSRs by name (mstatus, mepc, cycle...) or
* number.
*
* Instructions: RV32I and M, csrrw..csrrci, ecall ebreak mret mnret wfi fence
* fence.i, and the usual short forms: nop li la mv not neg seqz snez sltz sgtz
* j jr jal target ret call beqz bnez blez bgez bltz bgtz bgt ble bgtu bleu
* csrr csrw csrs csrc csrwi csrsi csrci.
* F: flw fsw fmadd.s fmsub.s fnmsub.s fnmadd.s fadd.s fsub.s fmul.s fdiv.s
* fsqrt.s fsgnj.s fsgnjn.s fsgnjx.s fmin.s fmax.s feq.s flt.s fle.s fcvt.w.s
* fcvt.wu.s fcvt.s.w fcvt.s.wu fmv.x.w fmv.w.x fclass.s and fmv.s fneg.s
* fabs.s; float registers f0-f31 or ft0-ft11 fs0-fs11 fa0-fa7; a rounding
* mode (rne rtz rdn rup rmm dyn) may follow the operands, dyn if not. li is
* one instruction for a constant of -2048..2047 known by then, else lui and
* addi; la is auipc and addi.
* Directives: .org, .equ/.set name, value (or name = value), .byte, .half
* (.short .2byte), .word (.long .4byte), .ascii, .string/.asciz (a 0 after
* each string; \n \r \t \0 \\ \" escapes), .space/.skip/.zero n, .align n
* and .p2align n (to 2^n), .balign n (to n), .end; .text .data .section .globl
* .global .option and .type are accepted and ignored. Instructions must be on
* a 4-byte boundary. Labels are up to 8 characters (more are allowed; the
* first 8 count).
* The older forms still work: ORG, NAME EQU value, DB/DEFB/DEFM (no escapes),
* DH, DW/DEFW, DS/DEFS n, ALIGN n (to n), END; numbers 0FFH, $FF, 1010B; $
* for the current address.
*/
#define MAXSYM 200
struct sym { char name[8]; u32 val; u16 line; };
static struct sym syms[MAXSYM];
static u16 nsym;
static u32 a_pc; /* address of the line being assembled */
static u32 a_lo, a_hi; /* lowest and highest byte written */
static u8 a_pass; /* 1: sizes and labels, 2: bytes */
static u8 a_undef; /* the expression used an undefined symbol */
static u8 a_fwd; /* ... or one defined further on */
static u8 a_end; /* END seen */
static u16 a_lnum; /* source line, for forward references */
static const char *a_err; /* this line's error */
static const char *ap; /* parse pointer */
static void a_fail(const char *m) { if (!a_err) a_err = m; }
static void a_sp(void) { while (*ap == ' ' || *ap == '\t') ap++; }
static u8 a_alpha(char c) { return (c >= 'A' && c <= 'Z') || c == '_' || c == '.'; }
static u8 a_alnum(char c) { return a_alpha(c) || (c >= '0' && c <= '9'); }
/* the length of the character constant at p ('A' or '\N'), 0 if none */
static u8 a_chlen(const char *p)
{
if (p[0] != '\'') return 0;
if (p[1] == '\\' && p[2] && p[3] == '\'') return 4;
if (p[1] && p[2] == '\'') return 3;
return 0;
}
/* the character an escape stands for: \N \R \T \0 \E, else itself */
static u8 a_esc(char c)
{
switch (c) {
case 'n': case 'N': return 10;
case 'r': case 'R': return 13;
case 't': case 'T': return 9;
case '0': return 0;
case 'e': case 'E': return 27;
}
return c;
}
static void a_emit(u8 b)
{
if (a_pass == 2) {
if (!wr(a_pc, b)) a_fail("CAN'T WRITE THERE");
if (a_pc < a_lo) a_lo = a_pc;
if (a_pc > a_hi) a_hi = a_pc;
}
a_pc++;
}
static void a_emit32(u32 w) { a_emit(w); a_emit(w >> 8); a_emit(w >> 16); a_emit(w >> 24); }
static i16 sym_find(const char *n)
{
u16 i;
for (i = 0; i < nsym; i++)
if (!memcmp(syms[i].name, n, 8)) return i;
return -1;
}
static void sym_def(const char *n, u32 v)
{
i16 i = sym_find(n);
if (a_pass == 2) {
if (i >= 0 && syms[i].val != v) a_fail("PHASE ERROR");
return;
}
if (i >= 0) { a_fail("DUPLICATE LABEL"); return; }
if (nsym == MAXSYM) { a_fail("TOO MANY LABELS"); return; }
memcpy(syms[nsym].name, n, 8);
syms[nsym].val = v;
syms[nsym++].line = a_lnum;
}
/* a name at ap (up to 8 characters kept) */
static void a_name(char *n)
{
u8 k = 0;
memset(n, 0, 8);
while (a_alnum(*ap)) { if (k < 8) n[k++] = *ap; ap++; }
}
/* ---------------------------------------------------------- expressions */
static u32 a_expr(void);
/* the LUI/AUIPC part and the ADDI part of a 32-bit value */
static u32 hi20(u32 v) { return (v + 0x800) >> 12 & 0xFFFFF; }
static i32 lo12(u32 v) { return sx(v, 12); }
static u32 a_number(void)
{
const char *s = ap;
u32 v = 0;
u8 base = 10, n = 0;
while (a_alnum(s[n])) n++;
if (s[0] == '0' && s[1] == 'X') { base = 16; s += 2; }
else if (s[0] == '0' && s[1] == 'B' && s[n - 1] != 'H') { base = 2; s += 2; }
else {
if (s[n - 1] == 'H') base = 16;
else if (s[n - 1] == 'B') {
u8 k;
for (k = 0; k < n - 1 && (s[k] == '0' || s[k] == '1'); k++);
if (k == n - 1) base = 2;
}
}
for (; a_alnum(*s); s++) {
char c = *s;
u8 d = c >= '0' && c <= '9' ? c - '0' : c >= 'A' && c <= 'F' ? c - 'A' + 10 : 99;
if ((c == 'H' && base == 16 && !a_alnum(s[1])) || (c == 'B' && base == 2 && !a_alnum(s[1]))) { s++; break; }
if (d >= base) { a_fail("BAD NUMBER"); break; }
v = v * base + d;
}
ap = s;
return v;
}
static u32 a_factor(void)
{
u32 v;
a_sp();
if (*ap == '-') { ap++; return -a_factor(); }
if (*ap == '+') { ap++; return a_factor(); }
if (*ap == '~') { ap++; return ~a_factor(); }
if (*ap == '%') { /* %HI(x), %LO(x) */
u8 hi = !strncmp(ap + 1, "HI(", 3);
if (!hi && strncmp(ap + 1, "LO(", 3)) { a_fail("BAD EXPRESSION"); return 0; }
ap += 3;
v = a_factor();
return hi ? hi20(v) : (u32)lo12(v);
}
if (*ap == '.' && !a_alnum(ap[1])) { ap++; return a_pc; }
if (*ap == '(') {
ap++;
v = a_expr();
a_sp();
if (*ap == ')') ap++; else a_fail("MISSING )");
return v;
}
if (a_chlen(ap) == 4) { v = a_esc(ap[2]); ap += 4; return v; }
if (a_chlen(ap) == 3) { v = (u8)ap[1]; ap += 3; return v; }
if (*ap == '$') {
ap++;
if ((*ap >= '0' && *ap <= '9') || (*ap >= 'A' && *ap <= 'F')) {
v = 0;
while ((*ap >= '0' && *ap <= '9') || (*ap >= 'A' && *ap <= 'F'))
v = v * 16 + (*ap <= '9' ? *ap - '0' : *ap - 'A' + 10), ap++;
return v;
}
return a_pc;
}
if (*ap >= '0' && *ap <= '9') return a_number();
if (a_alpha(*ap)) {
char n[8];
i16 i;
a_name(n);
i = sym_find(n);
if (i >= 0) {
if (a_lnum && syms[i].line > a_lnum) a_fwd = 1;
return syms[i].val;
}
a_undef = 1;
if (a_pass == 2) a_fail("UNDEFINED LABEL");
return 0;
}
a_fail("BAD EXPRESSION");
return 0;
}
static u32 a_term(void)
{
u32 v = a_factor(), w;
for (;;) {
a_sp();
if (*ap == '*') { ap++; v *= a_factor(); }
else if (*ap == '/') { ap++; w = a_factor(); if (w) v /= w; else a_fail("DIVIDE BY 0"); }
else return v;
}
}
static u32 a_expr(void)
{
u32 v = a_term();
for (;;) {
a_sp();
if (*ap == '+') { ap++; v += a_term(); }
else if (*ap == '-') { ap++; v -= a_term(); }
else if (*ap == '&') { ap++; v &= a_term(); }
else if (*ap == '|') { ap++; v |= a_term(); }
else if (*ap == '^') { ap++; v ^= a_term(); }
else if (*ap == '<' && ap[1] == '<') { ap += 2; v <<= a_term(); }
else if (*ap == '>' && ap[1] == '>') { ap += 2; v >>= a_term(); }
else return v;
}
}
/* ------------------------------------------------------------- operands */
#define MAXOP 5
static char a_ops[MAXOP][40];
static u8 a_nops;
/* the operands at ap, split at commas outside brackets and quotes */
static void a_split(void)
{
a_nops = 0;
a_sp();
while (*ap) {
const char *s = ap;
u8 d = 0, q = 0, len;
while (*ap && (d || q || *ap != ',')) {
if (!q && a_chlen(ap)) ap += a_chlen(ap) - 1;
else if (q && *ap == '\\' && ap[1]) ap++;
else if (*ap == '"') q = !q;
else if (*ap == '(') d++;
else if (*ap == ')' && d) d--;
ap++;
}
len = ap - s;
while (len && (s[len - 1] == ' ' || s[len - 1] == '\t')) len--;
if (a_nops == MAXOP || len >= sizeof a_ops[0]) { a_fail("TOO MANY OPERANDS"); return; }
memcpy(a_ops[a_nops], s, len);
a_ops[a_nops++][len] = 0;
if (*ap == ',') { ap++; a_sp(); }
}
}
/* a register name, the whole of s; 0xFF if it isn't one */
static u8 a_regname(const char *s)
{
u8 i;
if (s[0] == 'X' && s[1] >= '0' && s[1] <= '9') {
u8 k = s[1] - '0';
if (s[2] >= '0' && s[2] <= '9') { k = k * 10 + s[2] - '0'; if (s[3]) return 0xFF; }
else if (s[2]) return 0xFF;
return k < 32 ? k : 0xFF;
}
if (!strcmp(s, "FP")) return 8;
for (i = 0; i < 32; i++) if (!strcmp(s, regn[i])) return i;
return 0xFF;
}
static u8 a_reg(u8 i)
{
u8 r = i < a_nops ? a_regname(a_ops[i]) : 0xFF;
if (r == 0xFF) a_fail("BAD REGISTER");
return r & 31;
}
static u32 a_val(const char *s)
{
u32 v;
ap = s;
v = a_expr();
a_sp();
if (*ap) a_fail("BAD OPERAND");
return v;
}
static u32 a_imm(u8 i)
{
if (i >= a_nops) { a_fail("MISSING OPERAND"); return 0; }
return a_val(a_ops[i]);
}
/* offset(reg) or (reg); the register number, the offset in *off */
static u8 a_mem(u8 i, i32 *off)
{
char *s, *o;
u8 n, r;
*off = 0;
if (i >= a_nops) { a_fail("MISSING OPERAND"); return 0; }
s = a_ops[i];
n = strlen(s);
if (!n || s[n - 1] != ')' || !(o = strrchr(s, '('))) { a_fail("OFFSET(REGISTER)"); return 0; }
s[n - 1] = 0;
r = a_regname(o + 1);
if (r == 0xFF) { a_fail("BAD REGISTER"); return 0; }
*o = 0;
if (o > s) *off = (i32)a_val(s);
return r;
}
static u16 a_csr(u8 i)
{
const struct csrn *c;
u32 v;
if (i >= a_nops) { a_fail("MISSING OPERAND"); return 0; }
for (c = csrs; c->s; c++) if (!strcmp(a_ops[i], c->s)) return c->n;
v = a_val(a_ops[i]);
if (v > 0xFFF) a_fail("BAD CSR");
return v;
}
/* ------------------------------------------------------------ encoding */
static i32 a_range(i32 v, i32 lo, i32 hi)
{
if (a_pass == 2 && (v < lo || v > hi)) a_fail("VALUE OUT OF RANGE");
return v;
}
static u32 enc_i(u32 base, u8 rd, u8 rs1, i32 imm)
{
imm = a_range(imm, -2048, 2047);
return base | rd << 7 | rs1 << 15 | ((u32)imm & 0xFFF) << 20;
}
static u32 enc_r(u32 base, u8 rd, u8 rs1, u8 rs2) { return base | rd << 7 | rs1 << 15 | rs2 << 20; }
static u32 enc_s(u32 base, u8 rs1, u8 rs2, i32 imm)
{
u32 v = (u32)a_range(imm, -2048, 2047);
return base | (v & 31) << 7 | rs1 << 15 | rs2 << 20 | (v >> 5 & 0x7F) << 25;
}
static u32 enc_b(u32 base, u8 rs1, u8 rs2, u32 target)
{
i32 d = (i32)(target - a_pc);
u32 v;
if (a_pass == 2 && (d & 1)) a_fail("ODD TARGET");
if (a_pass == 2 && (d < -4096 || d > 4094)) a_fail("BRANCH TOO FAR");
v = (u32)d;
return base | rs1 << 15 | rs2 << 20 | (v >> 11 & 1) << 7 | (v >> 1 & 15) << 8 |
(v >> 5 & 0x3F) << 25 | (v >> 12 & 1) << 31;
}
static u32 enc_j(u8 rd, u32 target)
{
i32 d = (i32)(target - a_pc);
u32 v;
if (a_pass == 2 && (d & 1)) a_fail("ODD TARGET");
if (a_pass == 2 && (d < -(1 << 20) || d >= (1 << 20))) a_fail("JUMP TOO FAR");
v = (u32)d;
return 0x6F | rd << 7 | (v >> 12 & 0xFF) << 12 | (v >> 11 & 1) << 20 |
(v >> 1 & 0x3FF) << 21 | (v >> 20 & 1) << 31;
}
static u32 enc_u(u32 base, u8 rd, u32 imm)
{
if (a_pass == 2 && imm > 0xFFFFF) a_fail("VALUE OUT OF RANGE");
return base | rd << 7 | (imm & 0xFFFFF) << 12;
}
/* ----------------------------------------------------------------- F */
static void a_need(u8 n);
/* a float register name, the whole of s; 0xFF if it isn't one */
static u8 a_fregname(const char *s)
{
u8 k = 0, base, lim;
const char *d;
if (s[0] != 'F') return 0xFF;
if (s[1] >= '0' && s[1] <= '9') { d = s + 1; base = 0; lim = 32; }
else if (s[1] == 'T') { d = s + 2; base = 100; lim = 12; } /* FT0-7: 0-7, FT8-11: 28-31 */
else if (s[1] == 'S') { d = s + 2; base = 200; lim = 12; } /* FS0-1: 8-9, FS2-11: 18-27 */
else if (s[1] == 'A') { d = s + 2; base = 10; lim = 8; } /* FA0-7: 10-17 */
else return 0xFF;
if (*d < '0' || *d > '9') return 0xFF;
k = *d++ - '0';
if (*d >= '0' && *d <= '9') k = k * 10 + (*d++ - '0');
if (*d || k >= lim) return 0xFF;
if (base == 100) return k < 8 ? k : k + 20;
if (base == 200) return k < 2 ? k + 8 : k + 16;
return base + k;
}
static u8 a_freg(u8 i)
{
u8 r = i < a_nops ? a_fregname(a_ops[i]) : 0xFF;
if (r == 0xFF) a_fail("BAD FLOAT REGISTER");
return r & 31;
}
/* the rounding mode in operand i, DYN (7) if there is none */
static u32 a_rm(u8 i)
{
static const char *const m[8] = { "RNE", "RTZ", "RDN", "RUP", "RMM", "", "", "DYN" };
u8 k;
if (i >= a_nops) return 7;
for (k = 0; k < 8; k++) if (m[k][0] && !strcmp(a_ops[i], m[k])) return k;
a_fail("BAD ROUNDING MODE");
return 7;
}
enum { G_LD, G_ST, G_R4, G_R3RM, G_R2RM, G_R3, G_XFF, G_XFRM, G_FXRM, G_XF, G_FX, G_P2 };
struct fins { char name[10]; u8 f; u32 base; };
static const struct fins ftab[] = {
{ "FLW", G_LD, 0x00002007 }, { "FSW", G_ST, 0x00002027 },
{ "FMADD.S", G_R4, 0x43 }, { "FMSUB.S", G_R4, 0x47 }, { "FNMSUB.S", G_R4, 0x4B },
{ "FNMADD.S", G_R4, 0x4F },
{ "FADD.S", G_R3RM, 0x00000053 }, { "FSUB.S", G_R3RM, 0x08000053 },
{ "FMUL.S", G_R3RM, 0x10000053 }, { "FDIV.S", G_R3RM, 0x18000053 },
{ "FSQRT.S", G_R2RM, 0x58000053 },
{ "FSGNJ.S", G_R3, 0x20000053 }, { "FSGNJN.S", G_R3, 0x20001053 },
{ "FSGNJX.S", G_R3, 0x20002053 }, { "FMIN.S", G_R3, 0x28000053 }, { "FMAX.S", G_R3, 0x28001053 },
{ "FEQ.S", G_XFF, 0xA0002053 }, { "FLT.S", G_XFF, 0xA0001053 }, { "FLE.S", G_XFF, 0xA0000053 },
{ "FCVT.W.S", G_XFRM, 0xC0000053 }, { "FCVT.WU.S", G_XFRM, 0xC0100053 },
{ "FCVT.S.W", G_FXRM, 0xD0000053 }, { "FCVT.S.WU", G_FXRM, 0xD0100053 },
{ "FMV.X.W", G_XF, 0xE0000053 }, { "FMV.X.S", G_XF, 0xE0000053 },
{ "FCLASS.S", G_XF, 0xE0001053 },
{ "FMV.W.X", G_FX, 0xF0000053 }, { "FMV.S.X", G_FX, 0xF0000053 },
{ "FMV.S", G_P2, 0x20000053 }, { "FNEG.S", G_P2, 0x20001053 }, { "FABS.S", G_P2, 0x20002053 },
};
#define NFINS (sizeof ftab / sizeof ftab[0])
/* the operand count of a form with an optional rounding mode */
static void a_need2(u8 n) { if (a_nops != n && a_nops != n + 1) a_need(n); }
static void a_finstr(const struct fins *t)
{
u32 b = t->base;
i32 off;
u8 r, s;
if (a_pc & 3) { a_fail("NOT ON A WORD: USE .ALIGN 2"); return; }
switch (t->f) {
case G_LD: a_need(2); r = a_freg(0); s = a_mem(1, &off); a_emit32(enc_i(b, r, s, off)); return;
case G_ST: a_need(2); r = a_freg(0); s = a_mem(1, &off); a_emit32(enc_s(b, s, r, off)); return;
case G_R4:
a_need2(4);
a_emit32(b | a_freg(0) << 7 | a_freg(1) << 15 | a_freg(2) << 20 | (u32)a_freg(3) << 27 |
a_rm(4) << 12);
return;
case G_R3RM:
a_need2(3);
a_emit32(b | a_freg(0) << 7 | a_freg(1) << 15 | a_freg(2) << 20 | a_rm(3) << 12);
return;
case G_R2RM: a_need2(2); a_emit32(b | a_freg(0) << 7 | a_freg(1) << 15 | a_rm(2) << 12); return;
case G_R3: a_need(3); a_emit32(b | a_freg(0) << 7 | a_freg(1) << 15 | a_freg(2) << 20); return;
case G_XFF: a_need(3); a_emit32(b | a_reg(0) << 7 | a_freg(1) << 15 | a_freg(2) << 20); return;
case G_XFRM: a_need2(2); a_emit32(b | a_reg(0) << 7 | a_freg(1) << 15 | a_rm(2) << 12); return;
case G_FXRM: a_need2(2); a_emit32(b | a_freg(0) << 7 | a_reg(1) << 15 | a_rm(2) << 12); return;
case G_XF: a_need(2); a_emit32(b | a_reg(0) << 7 | a_freg(1) << 15); return;
case G_FX: a_need(2); a_emit32(b | a_freg(0) << 7 | a_reg(1) << 15); return;
case G_P2: a_need(2); r = a_freg(1); a_emit32(b | a_freg(0) << 7 | r << 15 | r << 20); return;
}
}
/* --------------------------------------------------------- instructions */
enum {
F_R, F_I, F_SH, F_LD, F_ST, F_B, F_U, F_JAL, F_JALR, F_CSR, F_CSRI, F_FIX,
P_LI, P_LA, P_MV, P_NOT, P_NEG, P_SEQZ, P_SNEZ, P_SLTZ, P_SGTZ, P_J, P_JR, P_RET,
P_CALL, P_BZ, P_BZS, P_BSW, P_CSRR, P_CSRW, P_CSRWI, P_FR, P_FS
};
struct ins { char name[8]; u8 f; u32 base; };
static const struct ins itab[] = {
{ "ADD", F_R, 0x00000033 }, { "SUB", F_R, 0x40000033 }, { "SLL", F_R, 0x00001033 },
{ "SLT", F_R, 0x00002033 }, { "SLTU", F_R, 0x00003033 }, { "XOR", F_R, 0x00004033 },
{ "SRL", F_R, 0x00005033 }, { "SRA", F_R, 0x40005033 }, { "OR", F_R, 0x00006033 },
{ "AND", F_R, 0x00007033 },
{ "MUL", F_R, 0x02000033 }, { "MULH", F_R, 0x02001033 }, { "MULHSU", F_R, 0x02002033 },
{ "MULHU", F_R, 0x02003033 }, { "DIV", F_R, 0x02004033 }, { "DIVU", F_R, 0x02005033 },
{ "REM", F_R, 0x02006033 }, { "REMU", F_R, 0x02007033 },
{ "ADDI", F_I, 0x00000013 }, { "SLTI", F_I, 0x00002013 }, { "SLTIU", F_I, 0x00003013 },
{ "XORI", F_I, 0x00004013 }, { "ORI", F_I, 0x00006013 }, { "ANDI", F_I, 0x00007013 },
{ "SLLI", F_SH, 0x00001013 }, { "SRLI", F_SH, 0x00005013 }, { "SRAI", F_SH, 0x40005013 },
{ "LB", F_LD, 0x00000003 }, { "LH", F_LD, 0x00001003 }, { "LW", F_LD, 0x00002003 },
{ "LBU", F_LD, 0x00004003 }, { "LHU", F_LD, 0x00005003 },
{ "SB", F_ST, 0x00000023 }, { "SH", F_ST, 0x00001023 }, { "SW", F_ST, 0x00002023 },
{ "BEQ", F_B, 0x00000063 }, { "BNE", F_B, 0x00001063 }, { "BLT", F_B, 0x00004063 },
{ "BGE", F_B, 0x00005063 }, { "BLTU", F_B, 0x00006063 }, { "BGEU", F_B, 0x00007063 },
{ "LUI", F_U, 0x00000037 }, { "AUIPC", F_U, 0x00000017 },
{ "JAL", F_JAL, 0 }, { "JALR", F_JALR, 0x00000067 },
{ "CSRRW", F_CSR, 0x00001073 }, { "CSRRS", F_CSR, 0x00002073 }, { "CSRRC", F_CSR, 0x00003073 },
{ "CSRRWI", F_CSRI, 0x00005073 }, { "CSRRSI", F_CSRI, 0x00006073 }, { "CSRRCI", F_CSRI, 0x00007073 },
{ "ECALL", F_FIX, 0x00000073 }, { "EBREAK", F_FIX, 0x00100073 }, { "MRET", F_FIX, 0x30200073 },
{ "MNRET", F_FIX, 0x70200073 }, { "WFI", F_FIX, 0x10500073 }, { "FENCE", F_FIX, 0x0FF0000F },
{ "FENCE.I", F_FIX, 0x0000100F }, { "NOP", F_FIX, 0x00000013 },
{ "LI", P_LI, 0 }, { "LA", P_LA, 0 }, { "MV", P_MV, 0 }, { "NOT", P_NOT, 0 },
{ "NEG", P_NEG, 0 }, { "SEQZ", P_SEQZ, 0 }, { "SNEZ", P_SNEZ, 0 }, { "SLTZ", P_SLTZ, 0 },
{ "SGTZ", P_SGTZ, 0 }, { "J", P_J, 0 }, { "JR", P_JR, 0 }, { "RET", P_RET, 0 },
{ "CALL", P_CALL, 0 },
{ "BEQZ", P_BZ, 0x00000063 }, { "BNEZ", P_BZ, 0x00001063 }, { "BLTZ", P_BZ, 0x00004063 },
{ "BGEZ", P_BZ, 0x00005063 }, { "BGTZ", P_BZS, 0x00004063 }, { "BLEZ", P_BZS, 0x00005063 },
{ "BGT", P_BSW, 0x00004063 }, { "BLE", P_BSW, 0x00005063 }, { "BGTU", P_BSW, 0x00006063 },
{ "BLEU", P_BSW, 0x00007063 },
{ "CSRR", P_CSRR, 0 }, { "CSRW", P_CSRW, 0x00001073 }, { "CSRS", P_CSRW, 0x00002073 },
{ "CSRC", P_CSRW, 0x00003073 }, { "CSRWI", P_CSRWI, 0x00005073 },
{ "CSRSI", P_CSRWI, 0x00006073 }, { "CSRCI", P_CSRWI, 0x00007073 },
/* the float CSRs: read (CSRR), and write ([rd,] rs: CSRRW) */
{ "FRFLAGS", P_FR, 0x00102073 }, { "FRRM", P_FR, 0x00202073 }, { "FRCSR", P_FR, 0x00302073 },
{ "FSFLAGS", P_FS, 0x00101073 }, { "FSRM", P_FS, 0x00201073 }, { "FSCSR", P_FS, 0x00301073 },
};
#define NINS (sizeof itab / sizeof itab[0])
static void a_need(u8 n) { if (a_nops != n) a_fail(a_nops < n ? "MISSING OPERAND" : "TOO MANY OPERANDS"); }
static void a_instr(const struct ins *t)
{
u32 b = t->base, v;
i32 off;
u8 rd, rs;
if (a_pc & 3) { a_fail("NOT ON A WORD: USE .ALIGN 2"); return; }
switch (t->f) {
case F_R: a_need(3); a_emit32(enc_r(b, a_reg(0), a_reg(1), a_reg(2))); return;
case F_I: a_need(3); a_emit32(enc_i(b, a_reg(0), a_reg(1), (i32)a_imm(2))); return;
case F_SH:
a_need(3);
v = a_imm(2);
if (a_pass == 2 && v > 31) a_fail("SHIFT OVER 31");
a_emit32(enc_r(b, a_reg(0), a_reg(1), v & 31));
return;
case F_LD: a_need(2); rd = a_reg(0); rs = a_mem(1, &off); a_emit32(enc_i(b, rd, rs, off)); return;
case F_ST: a_need(2); rd = a_reg(0); rs = a_mem(1, &off); a_emit32(enc_s(b, rs, rd, off)); return;
case F_B: a_need(3); a_emit32(enc_b(b, a_reg(0), a_reg(1), a_imm(2))); return;
case F_U: a_need(2); a_emit32(enc_u(b, a_reg(0), a_imm(1))); return;
case F_JAL: /* JAL target (RA) or JAL rd,target */
if (a_nops == 1) { a_emit32(enc_j(1, a_imm(0))); return; }
a_need(2); a_emit32(enc_j(a_reg(0), a_imm(1))); return;
case F_JALR: /* JALR rs / JALR rd,off(rs) / JALR rd,rs,off */
if (a_nops == 1) { a_emit32(enc_i(b, 1, a_reg(0), 0)); return; }
if (a_nops == 2) { rd = a_reg(0); rs = a_mem(1, &off); a_emit32(enc_i(b, rd, rs, off)); return; }
a_need(3); a_emit32(enc_i(b, a_reg(0), a_reg(1), (i32)a_imm(2))); return;
case F_CSR: a_need(3); a_emit32(b | a_reg(0) << 7 | a_reg(2) << 15 | (u32)a_csr(1) << 20); return;
case F_CSRI:
a_need(3);
v = a_imm(2);
if (a_pass == 2 && v > 31) a_fail("VALUE OVER 31");
a_emit32(b | a_reg(0) << 7 | (v & 31) << 15 | (u32)a_csr(1) << 20);
return;
case F_FIX: a_need(0); a_emit32(b); return;
case P_LI:
a_need(2);
rd = a_reg(0);
a_undef = a_fwd = 0;
v = a_imm(1);
if (!a_undef && !a_fwd && (i32)v >= -2048 && (i32)v <= 2047) {
a_emit32(enc_i(0x13, rd, 0, (i32)v));
} else if (!a_undef && !a_fwd && !(v & 0xFFF)) {
a_emit32(enc_u(0x37, rd, v >> 12)); /* LUI alone, as GNU as does */
} else {
a_emit32(enc_u(0x37, rd, hi20(v)));
a_emit32(enc_i(0x13, rd, rd, lo12(v)));
}
return;
case P_LA:
a_need(2);
rd = a_reg(0);
v = a_imm(1) - a_pc;
a_emit32(enc_u(0x17, rd, hi20(v)));
a_emit32(enc_i(0x13, rd, rd, lo12(v)));
return;
case P_MV: a_need(2); a_emit32(enc_i(0x13, a_reg(0), a_reg(1), 0)); return;
case P_NOT: a_need(2); a_emit32(enc_i(0x4013, a_reg(0), a_reg(1), -1)); return;
case P_NEG: a_need(2); a_emit32(enc_r(0x40000033, a_reg(0), 0, a_reg(1))); return;
case P_SEQZ: a_need(2); a_emit32(enc_i(0x3013, a_reg(0), a_reg(1), 1)); return;
case P_SNEZ: a_need(2); a_emit32(enc_r(0x3033, a_reg(0), 0, a_reg(1))); return;
case P_SLTZ: a_need(2); a_emit32(enc_r(0x2033, a_reg(0), a_reg(1), 0)); return;
case P_SGTZ: a_need(2); a_emit32(enc_r(0x2033, a_reg(0), 0, a_reg(1))); return;
case P_J: a_need(1); a_emit32(enc_j(0, a_imm(0))); return;
case P_CALL: a_need(1); a_emit32(enc_j(1, a_imm(0))); return;
case P_JR: a_need(1); a_emit32(enc_i(0x67, 0, a_reg(0), 0)); return;
case P_RET: a_need(0); a_emit32(0x00008067); return;
case P_BZ: a_need(2); a_emit32(enc_b(b, a_reg(0), 0, a_imm(1))); return;
case P_BZS: a_need(2); a_emit32(enc_b(b, 0, a_reg(0), a_imm(1))); return;
case P_BSW: a_need(3); rs = a_reg(0); a_emit32(enc_b(b, a_reg(1), rs, a_imm(2))); return;
case P_CSRR: a_need(2); a_emit32(0x2073 | a_reg(0) << 7 | (u32)a_csr(1) << 20); return;
case P_CSRW: a_need(2); a_emit32(b | a_reg(1) << 15 | (u32)a_csr(0) << 20); return;
case P_FR: a_need(1); a_emit32(b | a_reg(0) << 7); return;
case P_FS:
if (a_nops == 1) { a_emit32(b | a_reg(0) << 15); return; }
a_need(2); a_emit32(b | a_reg(0) << 7 | a_reg(1) << 15); return;
case P_CSRWI:
a_need(2);
v = a_imm(1);
if (a_pass == 2 && v > 31) a_fail("VALUE OVER 31");
a_emit32(b | (v & 31) << 15 | (u32)a_csr(0) << 20);
return;
}
}
/* data items: numbers, and for bytes "strings". str: 1 strings with
escapes (.ascii), 2 the same with a 0 after each (.string); 0 DB's, kept
as they are. */
static void a_data(u8 size, u8 str)
{
for (;;) {
a_sp();
if (*ap == '"' && size == 1) {
ap++;
while (*ap && *ap != '"') {
if (str && *ap == '\\' && ap[1]) { a_emit(a_esc(ap[1])); ap += 2; }
else a_emit(*ap++);
}
if (*ap) ap++;
if (str == 2) a_emit(0);
} else if (str) {
a_fail("BAD STRING");
return;
} else {
u32 v = a_expr();
u8 k;
for (k = 0; k < size; k++) a_emit(v >> (8 * k));
}
a_sp();
if (*ap != ',') break;
ap++;
}
if (*ap) a_fail("BAD DATA");
}
static u8 is_w(const char *w, const char *s) { return !strncmp(w, s, 8); }
/* directives, and the old names that mean the same */
enum { D_EQU = 1, D_SET, D_ORG, D_END, D_DB, D_ASCII, D_STRING, D_DH, D_DW, D_DS, D_SPACE,
D_ALIGN, D_P2ALIGN, D_IGNORE };
static const struct { char n[8]; u8 d; } dtab[] = {
{ "EQU", D_EQU }, { ".EQU", D_SET }, { ".SET", D_SET }, { "ORG", D_ORG }, { ".ORG", D_ORG },
{ "END", D_END }, { ".END", D_END }, { "DB", D_DB }, { "DEFB", D_DB }, { "DEFM", D_DB },
{ ".BYTE", D_DB }, { ".ASCII", D_ASCII }, { ".STRING", D_STRING }, { ".ASCIZ", D_STRING },
{ "DH", D_DH }, { ".HALF", D_DH }, { ".SHORT", D_DH }, { ".2BYTE", D_DH }, { "DW", D_DW },
{ "DEFW", D_DW }, { ".WORD", D_DW }, { ".LONG", D_DW }, { ".4BYTE", D_DW }, { "DS", D_DS },
{ "DEFS", D_DS }, { ".SPACE", D_SPACE }, { ".SKIP", D_SPACE }, { ".ZERO", D_SPACE },
{ "ALIGN", D_ALIGN }, { ".BALIGN", D_ALIGN }, { ".ALIGN", D_P2ALIGN }, { ".P2ALIGN", D_P2ALIGN },
{ ".TEXT", D_IGNORE }, { ".DATA", D_IGNORE }, { ".SECTION", D_IGNORE }, { ".GLOBL", D_IGNORE },
{ ".GLOBAL", D_IGNORE }, { ".OPTION", D_IGNORE }, { ".TYPE", D_IGNORE }, { "", 0 }
};
/* Assemble one source line (uppercase except in quotes). */
static void a_line(char *s)
{
char w[8], lab[8], fm[12];
const char *mn;
u8 has_lab = 0, q = 0, d;
u16 m;
u32 n, f;
char *c;
a_err = 0;
/* cut the comment */
for (c = s; *c; c++) {
if (q && *c == '\\' && c[1]) c++;
else if (*c == '"') q = !q;
else if (!q && a_chlen(c)) c += a_chlen(c) - 1;
else if ((*c == ';' || *c == '#') && !q) { *c = 0; break; }
}
ap = s;
a_sp();
if (*ap >= '0' && *ap <= '9') { /* a BASIC line number */
const char *t = ap;
while (*t >= '0' && *t <= '9') t++;
if (*t == ' ' || !*t) ap = t;
}
a_sp();
if (!*ap) return;
if (!a_alpha(*ap)) { a_fail("SYNTAX ERROR"); return; }
mn = ap;
a_name(w);
a_sp();
if (*ap == ':') { /* LABEL: */
ap++;
memcpy(lab, w, 8); has_lab = 1;
a_sp();
if (!*ap) { sym_def(lab, a_pc); return; }
if (!a_alpha(*ap)) { a_fail("SYNTAX ERROR"); return; }
mn = ap;
a_name(w);
a_sp();
} else if ((!strncmp(ap, "EQU", 3) && !a_alnum(ap[3])) || (*ap == '=' && w[0] != '.')) {
memcpy(lab, w, 8); has_lab = 1; /* NAME EQU value, NAME = value */
ap += *ap == '=' ? 1 : 3;
memcpy(w, "EQU\0\0\0\0\0", 8);
}
for (m = 0; dtab[m].d && !is_w(w, dtab[m].n); m++);
d = dtab[m].d;
if (d == D_SET) { /* .EQU NAME, value */
if (!a_alpha(*ap)) { a_fail("EQU NEEDS A NAME"); return; }
a_name(lab); has_lab = 1;
a_sp();
if (*ap == ',') ap++;
d = D_EQU;
}
if (d == D_EQU) {
n = a_expr();
if (!has_lab) a_fail("EQU NEEDS A NAME");
else if (a_undef && a_pass == 1) a_fail("EQU BEFORE ITS VALUE");
else sym_def(lab, n);
return;
}
if (has_lab) sym_def(lab, a_pc);
switch (d) {
case D_ORG: a_pc = a_expr(); return;
case D_END: a_end = 1; return;
case D_DB: a_data(1, 0); return;
case D_ASCII: a_data(1, 1); return;
case D_STRING: a_data(1, 2); return;
case D_DH: a_data(2, 0); return;
case D_DW: a_data(4, 0); return;
case D_DS: a_pc += a_expr(); return;
case D_SPACE: /* n [, fill] */
n = a_expr(); f = 0;
a_sp();
if (*ap == ',') { ap++; f = a_expr(); }
while (n--) a_emit(f);
return;
case D_ALIGN: case D_P2ALIGN:
n = a_expr();
if (d == D_P2ALIGN) n = n < 32 ? 1u << n : 0; /* .ALIGN, .P2ALIGN: to 2^n */
a_sp();
if (!n || (n & (n - 1))) a_fail("ALIGN TO A POWER OF 2");
else if (*ap == ',') { /* n, fill */
ap++;
f = a_expr();
while (a_pc & (n - 1)) a_emit(f);
} else { /* zeros to a word, then NOPs */
while (a_pc & (n - 1) & 3) a_emit(0);
while (a_pc & (n - 1)) a_emit32(0x13);
}
return;
case D_IGNORE: return;
}
/* F instructions, whose names can be longer than 8 */
for (m = 0; m < 11 && a_alnum(mn[m]); m++) fm[m] = mn[m];
fm[m] = 0;
if (!a_alnum(mn[m]))
for (m = 0; m < NFINS; m++)
if (!strcmp(fm, ftab[m].name)) {
a_split();
if (!a_err) a_finstr(&ftab[m]);
return;
}
for (m = 0; m < NINS; m++)
if (!strncmp(w, itab[m].name, 8)) break;
if (m == NINS) { a_fail("UNKNOWN INSTRUCTION"); return; }
a_split();
if (a_err) return;
a_instr(&itab[m]);
}
/* Upper-case a source line outside quotes, in place. */
static void a_upper(char *s)
{
u8 q = 0;
for (; *s; s++) {
if (q && *s == '\\' && s[1]) s++;
else if (*s == '"') q = !q;
else if (!q && a_chlen(s)) s += a_chlen(s) - 1;
else if (!q && *s >= 'a' && *s <= 'z') *s -= 32;
if (*s == '\t') *s = ' ';
}
}