/* Writes a raw RV64 image for comparing translated and interpreted runs. * * The program makes calls through jal and through auipc and jalr, also with * a jalr that reads the register it writes. It returns through ra, saves ra * in a non-leaf function, branches around ebreak as a bounds check does, and * uses LR and SC. Recursive calls exceed the native call stack limit. * Both runs must end with the same status and instruction count. */ #include #include typedef uint32_t u32; typedef int32_t i32; enum { ZERO = 0, RA = 1, SP = 2, T0 = 5, T1 = 6, T2 = 7, S1 = 9 }; enum { A0 = 10, A1, A2, A3, A4, A5, A7 = 17, T5 = 30, T6 = 31 }; static u32 code[256]; static u32 len; /* Labels and the instructions that refer to them. */ enum label { LOOP, LEAF, LEAF_OK, NEST, RECURSE, RECURSE_DONE, LABELS }; static u32 labels[LABELS]; static struct { u32 at; enum label to; u32 kind; /* 0 jal, 1 branch, 2 jalr after auipc */ u32 ins; /* Instruction without its offset. */ } fixups[32]; static u32 nfixups; static u32 r_type(u32 op, u32 rd, u32 f3, u32 rs1, u32 rs2, u32 f7) { return f7 << 25 | rs2 << 20 | rs1 << 15 | f3 << 12 | rd << 7 | op; } static u32 i_type(u32 op, u32 rd, u32 f3, u32 rs1, i32 imm) { return ((u32)imm & 0xfff) << 20 | rs1 << 15 | f3 << 12 | rd << 7 | op; } static u32 s_type(u32 rs2, u32 rs1, u32 f3, i32 imm) { u32 v = (u32)imm; return (v >> 5 & 0x7f) << 25 | rs2 << 20 | rs1 << 15 | f3 << 12 | (v & 0x1f) << 7 | 0x23; } static void emit(u32 ins) { code[len++] = ins; } static void addi(u32 rd, u32 rs1, i32 imm) { emit(i_type(0x13, rd, 0, rs1, imm)); } static void ref(u32 kind, u32 ins, enum label to) { fixups[nfixups].at = len; fixups[nfixups].to = to; fixups[nfixups].kind = kind; fixups[nfixups].ins = ins; nfixups++; emit(0); } static void mark(enum label l) { labels[l] = len * 4; } static void resolve(void) { for (u32 i = 0; i < nfixups; i++) { u32 pc = fixups[i].at * 4; u32 off = labels[fixups[i].to] - pc; u32 ins = fixups[i].ins; if (fixups[i].kind == 0) { ins |= (off >> 20 & 1) << 31 | (off >> 1 & 0x3ff) << 21 | (off >> 11 & 1) << 20 | (off >> 12 & 0xff) << 12; } else if (fixups[i].kind == 1) { ins |= (off >> 12 & 1) << 31 | (off >> 5 & 0x3f) << 25 | (off >> 1 & 0xf) << 8 | (off >> 11 & 1) << 7; } else { ins |= ((off + 4) & 0xfff) << 20; /* Relative to the auipc. */ } code[fixups[i].at] = ins; } } int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "usage: %s \n", argv[0]); return 1; } /* main: sum calls over 200 iterations into a0. */ addi(A0, ZERO, 0); addi(A1, ZERO, 600); ref(0, RA << 7 | 0x6f, RECURSE); addi(A1, ZERO, 600); ref(0, RA << 7 | 0x6f, RECURSE); addi(S1, ZERO, 200); addi(T6, SP, -64); mark(LOOP); addi(A1, S1, 0); ref(0, RA << 7 | 0x6f, LEAF); /* jal ra, leaf */ ref(0, RA << 7 | 0x6f, NEST); /* jal ra, nest */ emit(T5 << 7 | 0x17); /* auipc t5, 0 */ ref(2, i_type(0x67, RA, 0, T5, 0), LEAF); /* jalr ra, leaf(t5) */ emit(RA << 7 | 0x17); /* auipc ra, 0 */ ref(2, i_type(0x67, RA, 0, RA, 0), LEAF); /* jalr ra, leaf(ra) */ addi(S1, S1, -1); ref(1, r_type(0x63, 0, 1, S1, ZERO, 0), LOOP); /* bnez s1, loop */ /* Add 7 to a doubleword with LR and SC, then fold it into a0. */ emit(0x1000302f | T6 << 15 | T0 << 7); /* lr.d t0, (t6) */ addi(T0, T0, 7); emit(0x1800302f | T6 << 15 | T0 << 20 | T1 << 7); /* sc.d t1, t0, (t6) */ emit(r_type(0x33, A0, 0, A0, T1, 0)); emit(i_type(0x03, T2, 3, T6, 0)); /* ld t2, 0(t6) */ emit(r_type(0x33, A0, 0, A0, T2, 0)); emit(i_type(0x13, A0, 7, A0, 0xff)); addi(A7, ZERO, 0x400); /* exit(a0) */ emit(0x73); /* leaf(a1): bounds check, then mix a1 into a0. */ mark(LEAF); addi(A2, ZERO, 1000); ref(1, r_type(0x63, 0, 6, A1, A2, 0), LEAF_OK); /* bltu a1, a2, ok */ emit(0x00100073); /* ebreak */ mark(LEAF_OK); addi(A3, A1, 0); emit(r_type(0x33, A3, 1, A3, A3, 0)); /* sll a3, a3, a3 */ addi(A4, ZERO, 3); emit(r_type(0x33, A4, 0, A1, A4, 1)); /* mul a4, a1, a4 */ emit(i_type(0x13, A5, 5, A1, 1)); /* srli a5, a1, 1 */ emit(r_type(0x33, A4, 4, A4, A5, 0)); /* xor a4, a4, a5 */ emit(r_type(0x33, A0, 0, A0, A4, 0)); emit(r_type(0x3b, A0, 0, A0, A3, 0x20)); /* subw a0, a0, a3 */ emit(i_type(0x67, ZERO, 0, RA, 0)); /* ret */ /* nest: save ra, call leaf twice, restore ra and return. */ mark(NEST); addi(SP, SP, -16); emit(s_type(RA, SP, 3, 8)); addi(A1, A1, 5); ref(0, RA << 7 | 0x6f, LEAF); addi(A1, A1, -3); ref(0, RA << 7 | 0x6f, LEAF); emit(i_type(0x03, RA, 3, SP, 8)); addi(SP, SP, 16); emit(i_type(0x67, ZERO, 0, RA, 0)); /* recurse(a1): add one to the result for each level after returning. */ mark(RECURSE); ref(1, r_type(0x63, 0, 0, A1, ZERO, 0), RECURSE_DONE); addi(SP, SP, -16); emit(s_type(RA, SP, 3, 8)); addi(A1, A1, -1); ref(0, RA << 7 | 0x6f, RECURSE); emit(i_type(0x03, RA, 3, SP, 8)); addi(SP, SP, 16); addi(A0, A0, 1); mark(RECURSE_DONE); emit(i_type(0x67, ZERO, 0, RA, 0)); resolve(); FILE *f = fopen(argv[1], "wb"); if (!f || fwrite(code, 4, len, f) != len || fclose(f)) { perror(argv[1]); return 1; } return 0; }