test/jit_calls.c 5.5 KiB raw
1
/* Writes a raw RV64 image for comparing translated and interpreted runs.
2
 *
3
 * The program makes calls through jal and through auipc and jalr, also with
4
 * a jalr that reads the register it writes.  It returns through ra, saves ra
5
 * in a non-leaf function, branches around ebreak as a bounds check does, and
6
 * uses LR and SC.  Recursive calls exceed the native call stack limit.
7
 * Both runs must end with the same status and instruction count. */
8
#include <stdint.h>
9
#include <stdio.h>
10
11
typedef uint32_t u32;
12
typedef int32_t  i32;
13
14
enum { ZERO = 0, RA = 1, SP = 2, T0 = 5, T1 = 6, T2 = 7, S1 = 9 };
15
enum { A0 = 10, A1, A2, A3, A4, A5, A7 = 17, T5 = 30, T6 = 31 };
16
17
static u32 code[256];
18
static u32 len;
19
20
/* Labels and the instructions that refer to them. */
21
enum label { LOOP, LEAF, LEAF_OK, NEST, RECURSE, RECURSE_DONE, LABELS };
22
static u32 labels[LABELS];
23
static struct {
24
    u32        at;
25
    enum label to;
26
    u32        kind; /* 0 jal, 1 branch, 2 jalr after auipc */
27
    u32        ins;  /* Instruction without its offset. */
28
} fixups[32];
29
static u32 nfixups;
30
31
static u32 r_type(u32 op, u32 rd, u32 f3, u32 rs1, u32 rs2, u32 f7) {
32
    return f7 << 25 | rs2 << 20 | rs1 << 15 | f3 << 12 | rd << 7 | op;
33
}
34
35
static u32 i_type(u32 op, u32 rd, u32 f3, u32 rs1, i32 imm) {
36
    return ((u32)imm & 0xfff) << 20 | rs1 << 15 | f3 << 12 | rd << 7 | op;
37
}
38
39
static u32 s_type(u32 rs2, u32 rs1, u32 f3, i32 imm) {
40
    u32 v = (u32)imm;
41
    return (v >> 5 & 0x7f) << 25 | rs2 << 20 | rs1 << 15 | f3 << 12 |
42
           (v & 0x1f) << 7 | 0x23;
43
}
44
45
static void emit(u32 ins) {
46
    code[len++] = ins;
47
}
48
49
static void addi(u32 rd, u32 rs1, i32 imm) {
50
    emit(i_type(0x13, rd, 0, rs1, imm));
51
}
52
53
static void ref(u32 kind, u32 ins, enum label to) {
54
    fixups[nfixups].at   = len;
55
    fixups[nfixups].to   = to;
56
    fixups[nfixups].kind = kind;
57
    fixups[nfixups].ins  = ins;
58
    nfixups++;
59
    emit(0);
60
}
61
62
static void mark(enum label l) {
63
    labels[l] = len * 4;
64
}
65
66
static void resolve(void) {
67
    for (u32 i = 0; i < nfixups; i++) {
68
        u32 pc  = fixups[i].at * 4;
69
        u32 off = labels[fixups[i].to] - pc;
70
        u32 ins = fixups[i].ins;
71
        if (fixups[i].kind == 0) {
72
            ins |= (off >> 20 & 1) << 31 | (off >> 1 & 0x3ff) << 21 |
73
                   (off >> 11 & 1) << 20 | (off >> 12 & 0xff) << 12;
74
        } else if (fixups[i].kind == 1) {
75
            ins |= (off >> 12 & 1) << 31 | (off >> 5 & 0x3f) << 25 |
76
                   (off >> 1 & 0xf) << 8 | (off >> 11 & 1) << 7;
77
        } else {
78
            ins |= ((off + 4) & 0xfff) << 20; /* Relative to the auipc. */
79
        }
80
        code[fixups[i].at] = ins;
81
    }
82
}
83
84
int main(int argc, char **argv) {
85
    if (argc != 2) {
86
        fprintf(stderr, "usage: %s <output>\n", argv[0]);
87
        return 1;
88
    }
89
    /* main: sum calls over 200 iterations into a0. */
90
    addi(A0, ZERO, 0);
91
    addi(A1, ZERO, 600);
92
    ref(0, RA << 7 | 0x6f, RECURSE);
93
    addi(A1, ZERO, 600);
94
    ref(0, RA << 7 | 0x6f, RECURSE);
95
    addi(S1, ZERO, 200);
96
    addi(T6, SP, -64);
97
    mark(LOOP);
98
    addi(A1, S1, 0);
99
    ref(0, RA << 7 | 0x6f, LEAF);             /* jal ra, leaf */
100
    ref(0, RA << 7 | 0x6f, NEST);             /* jal ra, nest */
101
    emit(T5 << 7 | 0x17);                     /* auipc t5, 0 */
102
    ref(2, i_type(0x67, RA, 0, T5, 0), LEAF); /* jalr ra, leaf(t5) */
103
    emit(RA << 7 | 0x17);                     /* auipc ra, 0 */
104
    ref(2, i_type(0x67, RA, 0, RA, 0), LEAF); /* jalr ra, leaf(ra) */
105
    addi(S1, S1, -1);
106
    ref(1, r_type(0x63, 0, 1, S1, ZERO, 0), LOOP); /* bnez s1, loop */
107
108
    /* Add 7 to a doubleword with LR and SC, then fold it into a0. */
109
    emit(0x1000302f | T6 << 15 | T0 << 7); /* lr.d t0, (t6) */
110
    addi(T0, T0, 7);
111
    emit(0x1800302f | T6 << 15 | T0 << 20 | T1 << 7); /* sc.d t1, t0, (t6) */
112
    emit(r_type(0x33, A0, 0, A0, T1, 0));
113
    emit(i_type(0x03, T2, 3, T6, 0)); /* ld t2, 0(t6) */
114
    emit(r_type(0x33, A0, 0, A0, T2, 0));
115
    emit(i_type(0x13, A0, 7, A0, 0xff));
116
    addi(A7, ZERO, 0x400); /* exit(a0) */
117
    emit(0x73);
118
119
    /* leaf(a1): bounds check, then mix a1 into a0. */
120
    mark(LEAF);
121
    addi(A2, ZERO, 1000);
122
    ref(1, r_type(0x63, 0, 6, A1, A2, 0), LEAF_OK); /* bltu a1, a2, ok */
123
    emit(0x00100073);                               /* ebreak */
124
    mark(LEAF_OK);
125
    addi(A3, A1, 0);
126
    emit(r_type(0x33, A3, 1, A3, A3, 0)); /* sll a3, a3, a3 */
127
    addi(A4, ZERO, 3);
128
    emit(r_type(0x33, A4, 0, A1, A4, 1)); /* mul a4, a1, a4 */
129
    emit(i_type(0x13, A5, 5, A1, 1));     /* srli a5, a1, 1 */
130
    emit(r_type(0x33, A4, 4, A4, A5, 0)); /* xor a4, a4, a5 */
131
    emit(r_type(0x33, A0, 0, A0, A4, 0));
132
    emit(r_type(0x3b, A0, 0, A0, A3, 0x20)); /* subw a0, a0, a3 */
133
    emit(i_type(0x67, ZERO, 0, RA, 0));      /* ret */
134
135
    /* nest: save ra, call leaf twice, restore ra and return. */
136
    mark(NEST);
137
    addi(SP, SP, -16);
138
    emit(s_type(RA, SP, 3, 8));
139
    addi(A1, A1, 5);
140
    ref(0, RA << 7 | 0x6f, LEAF);
141
    addi(A1, A1, -3);
142
    ref(0, RA << 7 | 0x6f, LEAF);
143
    emit(i_type(0x03, RA, 3, SP, 8));
144
    addi(SP, SP, 16);
145
    emit(i_type(0x67, ZERO, 0, RA, 0));
146
147
    /* recurse(a1): add one to the result for each level after returning. */
148
    mark(RECURSE);
149
    ref(1, r_type(0x63, 0, 0, A1, ZERO, 0), RECURSE_DONE);
150
    addi(SP, SP, -16);
151
    emit(s_type(RA, SP, 3, 8));
152
    addi(A1, A1, -1);
153
    ref(0, RA << 7 | 0x6f, RECURSE);
154
    emit(i_type(0x03, RA, 3, SP, 8));
155
    addi(SP, SP, 16);
156
    addi(A0, A0, 1);
157
    mark(RECURSE_DONE);
158
    emit(i_type(0x67, ZERO, 0, RA, 0));
159
160
    resolve();
161
    FILE *f = fopen(argv[1], "wb");
162
    if (!f || fwrite(code, 4, len, f) != len || fclose(f)) {
163
        perror(argv[1]);
164
        return 1;
165
    }
166
    return 0;
167
}