1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <errno.h>
#include "debug.h"
#include "box64context.h"
#include "dynarec.h"
#include "emu/x64emu_private.h"
#include "emu/x64run_private.h"
#include "x64run.h"
#include "x64emu.h"
#include "box64stack.h"
#include "callback.h"
#include "emu/x64run_private.h"
#include "x64trace.h"
#include "dynarec_native.h"
#include "rv64_printer.h"
#include "dynarec_rv64_private.h"
#include "dynarec_rv64_functions.h"
#include "dynarec_rv64_helper.h"
// emit CMP8 instruction, from cmp s1, s2, using s3, s4, s5 and s6 as scratch
void emit_cmp8(dynarec_rv64_t* dyn, int ninst, int s1, int s2, int s3, int s4, int s5, int s6)
{
CLEAR_FLAGS();
IFX_PENDOR0 {
SB(s1, xEmu, offsetof(x64emu_t, op1));
SB(s2, xEmu, offsetof(x64emu_t, op2));
SET_DF(s4, d_cmp8);
} else {
SET_DFNONE();
}
IFX(X_AF | X_CF | X_OF) {
// for later flag calculation
NOT(s5, s1);
}
// It's a cmp, we can't store the result back to s1.
SUB(s6, s1, s2);
ANDI(s6, s6, 0xff);
IFX_PENDOR0 {
SB(s6, xEmu, offsetof(x64emu_t, res));
}
IFX(X_SF) {
SRLI(s3, s6, 7);
BEQZ(s3, 8);
ORI(xFlags, xFlags, 1 << F_SF);
}
CALC_SUB_FLAGS(s5, s2, s6, s3, s4, 8);
IFX(X_ZF) {
BNEZ(s6, 8);
ORI(xFlags, xFlags, 1 << F_ZF);
}
IFX(X_PF) {
emit_pf(dyn, ninst, s6, s3, s4);
}
}
// emit CMP8 instruction, from cmp s1 , 0, using s3 and s4 as scratch
void emit_cmp8_0(dynarec_rv64_t* dyn, int ninst, int s1, int s3, int s4)
{
CLEAR_FLAGS();
IFX_PENDOR0 {
SB(s1, xEmu, offsetof(x64emu_t, op1));
SB(xZR, xEmu, offsetof(x64emu_t, op2));
SB(s1, xEmu, offsetof(x64emu_t, res));
SET_DF(s3, d_cmp8);
} else {
SET_DFNONE();
}
IFX(X_SF) {
SRLI(s3, s1, 7);
BEQZ(s3, 8);
ORI(xFlags, xFlags, 1 << F_SF);
}
IFX(X_ZF) {
BNEZ(s1, 8);
ORI(xFlags, xFlags, 1 << F_ZF);
}
IFX(X_PF) {
emit_pf(dyn, ninst, s1, s3, s4);
}
}
// emit CMP16 instruction, from cmp s1, s2, using s3 and s4 as scratch
void emit_cmp16(dynarec_rv64_t* dyn, int ninst, int s1, int s2, int s3, int s4, int s5, int s6)
{
CLEAR_FLAGS();
IFX_PENDOR0 {
SH(s1, xEmu, offsetof(x64emu_t, op1));
SH(s2, xEmu, offsetof(x64emu_t, op2));
SET_DF(s4, d_cmp16);
} else {
SET_DFNONE();
}
IFX(X_AF | X_CF | X_OF) {
// for later flag calculation
NOT(s5, s1);
}
// It's a cmp, we can't store the result back to s1.
SUB(s6, s1, s2);
IFX(X_ALL) {
ZEXTH(s6, s6);
}
IFX_PENDOR0 {
SH(s6, xEmu, offsetof(x64emu_t, res));
}
IFX(X_SF) {
SRLI(s3, s6, 15);
BEQZ(s3, 8);
ORI(xFlags, xFlags, 1 << F_SF);
}
CALC_SUB_FLAGS(s5, s2, s6, s3, s4, 16);
IFX(X_ZF) {
BNEZ(s6, 8);
ORI(xFlags, xFlags, 1 << F_ZF);
}
IFX(X_PF) {
emit_pf(dyn, ninst, s6, s3, s4);
}
}
// emit CMP16 instruction, from cmp s1 , #0, using s3 and s4 as scratch
void emit_cmp16_0(dynarec_rv64_t* dyn, int ninst, int s1, int s3, int s4)
{
CLEAR_FLAGS();
IFX_PENDOR0 {
SH(s1, xEmu, offsetof(x64emu_t, op1));
SH(xZR, xEmu, offsetof(x64emu_t, op2));
SH(s1, xEmu, offsetof(x64emu_t, res));
SET_DF(s3, d_cmp16);
} else {
SET_DFNONE();
}
IFX(X_SF) {
SRLI(s3, s1, 15);
BEQZ(s3, 8);
ORI(xFlags, xFlags, 1 << F_SF);
}
IFX(X_ZF) {
BNEZ(s1, 8);
ORI(xFlags, xFlags, 1 << F_ZF);
}
IFX(X_PF) {
emit_pf(dyn, ninst, s1, s3, s4);
}
}
// emit CMP32 instruction, from cmp s1, s2, using s3 and s4 as scratch
void emit_cmp32(dynarec_rv64_t* dyn, int ninst, rex_t rex, int s1, int s2, int s3, int s4, int s5, int s6)
{
CLEAR_FLAGS();
IFX_PENDOR0 {
SDxw(s1, xEmu, offsetof(x64emu_t, op1));
SDxw(s2, xEmu, offsetof(x64emu_t, op2));
SET_DF(s4, rex.w?d_cmp64:d_cmp32);
} else {
SET_DFNONE();
}
IFX(X_AF | X_CF | X_OF) {
// for later flag calculation
NOT(s5, s1);
}
// It's a cmp, we can't store the result back to s1.
SUBxw(s6, s1, s2);
IFX_PENDOR0 {
SDxw(s6, xEmu, offsetof(x64emu_t, res));
}
IFX(X_SF) {
BGE(s6, xZR, 8);
ORI(xFlags, xFlags, 1 << F_SF);
}
if (!rex.w) {
ZEROUP(s6);
}
CALC_SUB_FLAGS(s5, s2, s6, s3, s4, rex.w?64:32);
IFX(X_ZF) {
BNEZ(s6, 8);
ORI(xFlags, xFlags, 1 << F_ZF);
}
IFX(X_PF) {
emit_pf(dyn, ninst, s6, s3, s4);
}
}
// emit CMP32 instruction, from cmp s1, 0, using s3 and s4 as scratch
void emit_cmp32_0(dynarec_rv64_t* dyn, int ninst, rex_t rex, int s1, int s3, int s4)
{
CLEAR_FLAGS();
IFX_PENDOR0 {
SD(s1, xEmu, offsetof(x64emu_t, op1));
SD(xZR, xEmu, offsetof(x64emu_t, op2));
SD(s1, xEmu, offsetof(x64emu_t, res));
SET_DF(s4, rex.w?d_cmp64:d_cmp32);
} else {
SET_DFNONE();
}
IFX(X_SF) {
if (rex.w) {
BGE(s1, xZR, 8);
} else {
SRLI(s3, s1, 31);
BEQZ(s3, 8);
}
ORI(xFlags, xFlags, 1 << F_SF);
}
IFX(X_ZF) {
BNEZ(s1, 8);
ORI(xFlags, xFlags, 1 << F_ZF);
}
IFX(X_PF) {
emit_pf(dyn, ninst, s1, s3, s4);
}
}
// emit TEST8 instruction, from test s1, s2, using s3, s4 and s5 as scratch
void emit_test8(dynarec_rv64_t* dyn, int ninst, int s1, int s2, int s3, int s4, int s5) {
CLEAR_FLAGS();
IFX_PENDOR0 {
SET_DF(s3, d_tst8);
} else {
SET_DFNONE();
}
AND(s3, s1, s2); // res = s1 & s2
IFX_PENDOR0 {
SD(s3, xEmu, offsetof(x64emu_t, res));
}
IFX(X_SF) {
SRLI(s4, s3, 7);
BEQZ(s4, 8);
ORI(xFlags, xFlags, 1 << F_SF);
}
IFX(X_ZF) {
BNEZ(s3, 8);
ORI(xFlags, xFlags, 1 << F_ZF);
}
IFX(X_PF) {
emit_pf(dyn, ninst, s3, s4, s5);
}
}
// emit TEST16 instruction, from test s1, s2, using s3, s4 and s5 as scratch
void emit_test16(dynarec_rv64_t* dyn, int ninst, int s1, int s2, int s3, int s4, int s5)
{
CLEAR_FLAGS();
IFX_PENDOR0 {
SET_DF(s3, d_tst16);
} else {
SET_DFNONE();
}
AND(s3, s1, s2); // res = s1 & s2
IFX_PENDOR0 {
SH(s3, xEmu, offsetof(x64emu_t, res));
}
IFX(X_SF) {
SRLI(s4, s3, 15);
BEQZ(s4, 8);
ORI(xFlags, xFlags, 1 << F_SF);
}
IFX(X_ZF) {
BNEZ(s3, 8);
ORI(xFlags, xFlags, 1 << F_ZF);
}
IFX(X_PF) {
emit_pf(dyn, ninst, s3, s4, s5);
}
}
// emit TEST32 instruction, from test s1, s2, using s3 and s4 as scratch
void emit_test32(dynarec_rv64_t* dyn, int ninst, rex_t rex, int s1, int s2, int s3, int s4, int s5)
{
CLEAR_FLAGS();
IFX_PENDOR0 {
SET_DF(s3, rex.w?d_tst64:d_tst32);
} else {
SET_DFNONE();
}
AND(s3, s1, s2); // res = s1 & s2
IFX_PENDOR0 {
SDxw(s3, xEmu, offsetof(x64emu_t, res));
}
IFX(X_SF|X_ZF) {
if (!rex.w) ZEROUP(s3);
}
IFX(X_SF) {
SRLI(s4, s3, rex.w?63:31);
BEQZ(s4, 8);
ORI(xFlags, xFlags, 1 << F_SF);
}
IFX(X_ZF) {
BNEZ(s3, 8);
ORI(xFlags, xFlags, 1 << F_ZF);
}
IFX(X_PF) {
emit_pf(dyn, ninst, s3, s4, s5);
}
}
// emit TEST32 instruction, from test s1, s2, using s3 and s4 as scratch
void emit_test32c(dynarec_rv64_t* dyn, int ninst, rex_t rex, int s1, int64_t c, int s3, int s4, int s5)
{
CLEAR_FLAGS();
IFX_PENDOR0 {
SET_DF(s3, rex.w?d_tst64:d_tst32);
} else {
SET_DFNONE();
}
if(c>=-2048 && c<=2047) {
ANDI(s3, s1, c);
IFX(X_SF|X_ZF) {
if (!rex.w && c<0) ZEROUP(s3);
}
} else {
MOV64xw(s3, c);
AND(s3, s1, s3); // res = s1 & s2
}
IFX_PENDOR0 {
SDxw(s3, xEmu, offsetof(x64emu_t, res));
}
IFX(X_SF) {
SRLI(s4, s3, rex.w?63:31);
BEQZ(s4, 8);
ORI(xFlags, xFlags, 1 << F_SF);
}
IFX(X_ZF) {
BNEZ(s3, 8);
ORI(xFlags, xFlags, 1 << F_ZF);
}
IFX(X_PF) {
emit_pf(dyn, ninst, s3, s4, s5);
}
}
|