/* RISC-V assembler for the monitor (included by monitor.c). * * The syntax the disassembler prints. A line is * [line number] [label:] [instruction or directive] [; comment] * (the line number is there when the source was typed into BASIC). Numbers: * 123, 0FFH, $FF, 0x1F, 1010B, 'A'; $ alone is the current address. * Expressions use + - * / & | and parentheses. 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 (NAME EQU value), DB/DEFB/DEFM (numbers and "strings"), * DH (16 bits), DW/DEFW (32 bits), DS/DEFS n, ALIGN n, END. Instructions must * be on a 4-byte boundary. Labels are up to 8 characters. */ #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'); } 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); static u32 a_number(void) { const char *s = ap; u32 v = 0; u8 base = 10, n = 0; if (s[0] == '0' && s[1] == 'X') { base = 16; s += 2; } else { while (a_alnum(s[n])) n++; 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) || (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++; v = a_expr(); a_sp(); if (*ap == ')') ap++; else a_fail("MISSING )"); return v; } if (*ap == '\'' && ap[1] && ap[2] == '\'') { 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 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 (*ap == '\'' && !q && ap[1] && ap[2] == '\'') ap += 2; 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; } /* 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); } /* ----------------------------------------------------------------- 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 4"); 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 }; 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 }, }; #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 4"); 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 { 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_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; } } /* DB/DH/DW items: numbers and (for DB) strings */ static void a_data(u8 size) { for (;;) { a_sp(); if (*ap == '"' && size == 1) { ap++; while (*ap && *ap != '"') a_emit(*ap++); if (*ap) ap++; } 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); } /* 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; u16 m; char *c; a_err = 0; /* cut the comment */ for (c = s; *c; c++) { if (*c == '"') q = !q; else if (*c == '\'' && !q && c[1] && c[2] == '\'') c += 2; else if (*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])) { memcpy(lab, w, 8); has_lab = 1; memcpy(w, "EQU\0\0\0\0\0", 8); ap += 3; } if (is_w(w, "EQU")) { u32 v = 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, v); return; } if (has_lab) sym_def(lab, a_pc); if (is_w(w, "ORG")) { a_pc = a_expr(); return; } if (is_w(w, "END")) { a_end = 1; return; } if (is_w(w, "DB") || is_w(w, "DEFB") || is_w(w, "DEFM")) { a_data(1); return; } if (is_w(w, "DH")) { a_data(2); return; } if (is_w(w, "DW") || is_w(w, "DEFW")) { a_data(4); return; } if (is_w(w, "DS") || is_w(w, "DEFS")) { a_pc += a_expr(); return; } if (is_w(w, "ALIGN")) { u32 n = a_expr(); if (!n || (n & (n - 1))) a_fail("ALIGN TO A POWER OF 2"); else while (a_pc & (n - 1)) a_emit(0); 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 (*s == '"') q = !q; else if (*s == '\'' && !q && s[1] && s[2] == '\'') s += 2; else if (!q && *s >= 'a' && *s <= 'z') *s -= 32; if (*s == '\t') *s = ' '; } }