Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/target/i386/tcg/cc_helper.c
Line
Count
Source
1
/*
2
 *  x86 condition code helpers
3
 *
4
 *  Copyright (c) 2003 Fabrice Bellard
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
#include "qemu/osdep.h"
21
#include "cpu.h"
22
#include "exec/helper-proto.h"
23
#include "helper-tcg.h"
24
25
0
#define SHIFT 0
26
#include "cc_helper_template.h.inc"
27
#undef SHIFT
28
29
0
#define SHIFT 1
30
#include "cc_helper_template.h.inc"
31
#undef SHIFT
32
33
0
#define SHIFT 2
34
#include "cc_helper_template.h.inc"
35
#undef SHIFT
36
37
#ifdef TARGET_X86_64
38
39
#define SHIFT 3
40
#include "cc_helper_template.h.inc"
41
#undef SHIFT
42
43
#endif
44
45
static target_ulong compute_all_adcx(target_ulong dst, target_ulong src1,
46
                                     target_ulong src2)
47
0
{
48
0
    return (src1 & ~CC_C) | (dst * CC_C);
49
0
}
50
51
static target_ulong compute_all_adox(target_ulong dst, target_ulong src1,
52
                                     target_ulong src2)
53
0
{
54
0
    return (src1 & ~CC_O) | (src2 * CC_O);
55
0
}
56
57
static target_ulong compute_all_adcox(target_ulong dst, target_ulong src1,
58
                                      target_ulong src2)
59
0
{
60
0
    return (src1 & ~(CC_C | CC_O)) | (dst * CC_C) | (src2 * CC_O);
61
0
}
62
63
target_ulong HELPER(cc_compute_nz)(target_ulong dst, target_ulong src1,
64
                                  int op)
65
0
{
66
0
    if (CC_OP_HAS_EFLAGS(op)) {
67
0
        return ~src1 & CC_Z;
68
0
    } else {
69
0
        MemOp size = cc_op_size(op);
70
0
        target_ulong mask = MAKE_64BIT_MASK(0, 8 << size);
71
72
0
        return dst & mask;
73
0
    }
74
0
}
75
76
/* NOTE: we compute the flags like the P4. On olders CPUs, only OF and
77
   CF are modified and it is slower to do that.  Note as well that we
78
   don't truncate SRC1 for computing carry to DATA_TYPE.  */
79
static inline uint32_t compute_aco_mul(target_long src1)
80
0
{
81
0
    uint32_t cf, af, of;
82
83
0
    cf = (src1 != 0);
84
0
    af = 0; /* undefined */
85
0
    of = cf * CC_O;
86
0
    return cf + af + of;
87
0
}
88
89
target_ulong HELPER(cc_compute_all)(target_ulong dst, target_ulong src1,
90
                                   target_ulong src2, int op)
91
0
{
92
0
    uint32_t flags = 0;
93
0
    int shift = 0;
94
95
0
    switch (op) {
96
0
    default: /* should never happen */
97
0
        return 0;
98
99
0
    case CC_OP_EFLAGS:
100
0
        return src1;
101
0
    case CC_OP_POPCNT:
102
0
        return dst ? 0 : CC_Z;
103
0
    case CC_OP_SBB_SELF:
104
  /* dst is either all zeros (--Z-P-) or all ones (-S-APC) */
105
0
        return (dst & (CC_Z|CC_A|CC_C|CC_S)) ^ (CC_P | CC_Z);
106
107
0
    case CC_OP_ADCX:
108
0
        return compute_all_adcx(dst, src1, src2);
109
0
    case CC_OP_ADOX:
110
0
        return compute_all_adox(dst, src1, src2);
111
0
    case CC_OP_ADCOX:
112
0
        return compute_all_adcox(dst, src1, src2);
113
114
0
    case CC_OP_CCMPB:
115
0
        return compute_all_ccmpb(dst, src1, src2);
116
0
    case CC_OP_CCMPW:
117
0
        return compute_all_ccmpw(dst, src1, src2);
118
0
    case CC_OP_CCMPL:
119
0
        return compute_all_ccmpl(dst, src1, src2);
120
121
0
    case CC_OP_MULB:
122
0
        flags = compute_aco_mul(src1);
123
0
        goto psz_b;
124
0
    case CC_OP_MULW:
125
0
        flags = compute_aco_mul(src1);
126
0
        goto psz_w;
127
0
    case CC_OP_MULL:
128
0
        flags = compute_aco_mul(src1);
129
0
        goto psz_l;
130
131
0
    case CC_OP_ADDB:
132
0
        flags = compute_aco_addb(dst, src1);
133
0
        goto psz_b;
134
0
    case CC_OP_ADDW:
135
0
        flags = compute_aco_addw(dst, src1);
136
0
        goto psz_w;
137
0
    case CC_OP_ADDL:
138
0
        flags = compute_aco_addl(dst, src1);
139
0
        goto psz_l;
140
141
0
    case CC_OP_ADCB:
142
0
        flags = compute_aco_adcb(dst, src1, src2);
143
0
        goto psz_b;
144
0
    case CC_OP_ADCW:
145
0
        flags = compute_aco_adcw(dst, src1, src2);
146
0
        goto psz_w;
147
0
    case CC_OP_ADCL:
148
0
        flags = compute_aco_adcl(dst, src1, src2);
149
0
        goto psz_l;
150
151
0
    case CC_OP_SUBB:
152
0
        flags = compute_aco_subb(dst, src1);
153
0
        goto psz_b;
154
0
    case CC_OP_SUBW:
155
0
        flags = compute_aco_subw(dst, src1);
156
0
        goto psz_w;
157
0
    case CC_OP_SUBL:
158
0
        flags = compute_aco_subl(dst, src1);
159
0
        goto psz_l;
160
161
0
    case CC_OP_SBBB:
162
0
        flags = compute_aco_sbbb(dst, src1, src2);
163
0
        goto psz_b;
164
0
    case CC_OP_SBBW:
165
0
        flags = compute_aco_sbbw(dst, src1, src2);
166
0
        goto psz_w;
167
0
    case CC_OP_SBBL:
168
0
        flags = compute_aco_sbbl(dst, src1, src2);
169
0
        goto psz_l;
170
171
0
    case CC_OP_LOGICB:
172
0
        flags = 0;
173
0
        goto psz_b;
174
0
    case CC_OP_LOGICW:
175
0
        flags = 0;
176
0
        goto psz_w;
177
0
    case CC_OP_LOGICL:
178
0
        flags = 0;
179
0
        goto psz_l;
180
181
0
    case CC_OP_INCB:
182
0
        flags = compute_aco_incb(dst, src1);
183
0
        goto psz_b;
184
0
    case CC_OP_INCW:
185
0
        flags = compute_aco_incw(dst, src1);
186
0
        goto psz_w;
187
0
    case CC_OP_INCL:
188
0
        flags = compute_aco_incl(dst, src1);
189
0
        goto psz_l;
190
191
0
    case CC_OP_DECB:
192
0
        flags = compute_aco_decb(dst, src1);
193
0
        goto psz_b;
194
0
    case CC_OP_DECW:
195
0
        flags = compute_aco_decw(dst, src1);
196
0
        goto psz_w;
197
0
    case CC_OP_DECL:
198
0
        flags = compute_aco_decl(dst, src1);
199
0
        goto psz_l;
200
201
0
    case CC_OP_SHLB:
202
0
        flags = compute_aco_shlb(dst, src1);
203
0
        goto psz_b;
204
0
    case CC_OP_SHLW:
205
0
        flags = compute_aco_shlw(dst, src1);
206
0
        goto psz_w;
207
0
    case CC_OP_SHLL:
208
0
        flags = compute_aco_shll(dst, src1);
209
0
        goto psz_l;
210
211
0
    case CC_OP_SARB:
212
0
        flags = compute_aco_sarb(dst, src1);
213
0
        goto psz_b;
214
0
    case CC_OP_SARW:
215
0
        flags = compute_aco_sarw(dst, src1);
216
0
        goto psz_w;
217
0
    case CC_OP_SARL:
218
0
        flags = compute_aco_sarl(dst, src1);
219
0
        goto psz_l;
220
221
0
    case CC_OP_BMILGB:
222
0
        flags = compute_aco_bmilgb(dst, src1);
223
0
        goto psz_b;
224
0
    case CC_OP_BMILGW:
225
0
        flags = compute_aco_bmilgw(dst, src1);
226
0
        goto psz_w;
227
0
    case CC_OP_BMILGL:
228
0
        flags = compute_aco_bmilgl(dst, src1);
229
0
        goto psz_l;
230
231
0
    case CC_OP_BLSIB:
232
0
        flags = compute_aco_blsib(dst, src1);
233
0
        goto psz_b;
234
0
    case CC_OP_BLSIW:
235
0
        flags = compute_aco_blsiw(dst, src1);
236
0
        goto psz_w;
237
0
    case CC_OP_BLSIL:
238
0
        flags = compute_aco_blsil(dst, src1);
239
0
        goto psz_l;
240
241
#ifdef TARGET_X86_64
242
    case CC_OP_CCMPQ:
243
        return compute_all_ccmpq(dst, src1, src2);
244
245
    case CC_OP_MULQ:
246
        flags = compute_aco_mul(src1);
247
        goto psz_q;
248
    case CC_OP_ADDQ:
249
        flags = compute_aco_addq(dst, src1);
250
        goto psz_q;
251
    case CC_OP_ADCQ:
252
        flags = compute_aco_adcq(dst, src1, src2);
253
        goto psz_q;
254
    case CC_OP_SUBQ:
255
        flags = compute_aco_subq(dst, src1);
256
        goto psz_q;
257
    case CC_OP_SBBQ:
258
        flags = compute_aco_sbbq(dst, src1, src2);
259
        goto psz_q;
260
    case CC_OP_INCQ:
261
        flags = compute_aco_incq(dst, src1);
262
        goto psz_q;
263
    case CC_OP_DECQ:
264
        flags = compute_aco_decq(dst, src1);
265
        goto psz_q;
266
    case CC_OP_LOGICQ:
267
        flags = 0;
268
        goto psz_q;
269
    case CC_OP_SHLQ:
270
        flags = compute_aco_shlq(dst, src1);
271
        goto psz_q;
272
    case CC_OP_SARQ:
273
        flags = compute_aco_sarq(dst, src1);
274
        goto psz_q;
275
    case CC_OP_BMILGQ:
276
        flags = compute_aco_bmilgq(dst, src1);
277
        goto psz_q;
278
    case CC_OP_BLSIQ:
279
        flags = compute_aco_blsiq(dst, src1);
280
        goto psz_q;
281
#endif
282
0
    }
283
284
0
psz_b:
285
0
    shift += 8;
286
0
psz_w:
287
0
    shift += 16;
288
0
psz_l:
289
#ifdef TARGET_X86_64
290
    shift += 32;
291
psz_q:
292
#endif
293
294
0
    flags += compute_pf(dst);
295
0
    dst <<= shift;
296
0
    flags += dst == 0 ? CC_Z : 0;
297
0
    flags += (target_long)dst < 0 ? CC_S : 0;
298
0
    return flags;
299
0
}
300
301
uint32_t cpu_cc_compute_all(CPUX86State *env)
302
0
{
303
0
    return HELPER(cc_compute_all)(CC_DST, CC_SRC, CC_SRC2, CC_OP);
304
0
}
305
306
target_ulong HELPER(cc_compute_c)(target_ulong dst, target_ulong src1,
307
                                 target_ulong src2, int op)
308
0
{
309
0
    switch (op) {
310
0
    default: /* should never happen */
311
0
    case CC_OP_LOGICB:
312
0
    case CC_OP_LOGICW:
313
0
    case CC_OP_LOGICL:
314
0
    case CC_OP_LOGICQ:
315
0
    case CC_OP_POPCNT:
316
0
        return 0;
317
318
0
    case CC_OP_EFLAGS:
319
0
    case CC_OP_SARB:
320
0
    case CC_OP_SARW:
321
0
    case CC_OP_SARL:
322
0
    case CC_OP_SARQ:
323
0
    case CC_OP_ADOX:
324
0
        return src1 & 1;
325
326
0
    case CC_OP_INCB:
327
0
    case CC_OP_INCW:
328
0
    case CC_OP_INCL:
329
0
    case CC_OP_INCQ:
330
0
    case CC_OP_DECB:
331
0
    case CC_OP_DECW:
332
0
    case CC_OP_DECL:
333
0
    case CC_OP_DECQ:
334
0
        return src1;
335
336
0
    case CC_OP_MULB:
337
0
    case CC_OP_MULW:
338
0
    case CC_OP_MULL:
339
0
    case CC_OP_MULQ:
340
0
        return src1 != 0;
341
342
0
    case CC_OP_SBB_SELF:
343
0
        return dst & 1;
344
345
0
    case CC_OP_ADCX:
346
0
    case CC_OP_ADCOX:
347
0
        return dst;
348
349
0
    case CC_OP_ADDB:
350
0
        return compute_c_addb(dst, src1);
351
0
    case CC_OP_ADDW:
352
0
        return compute_c_addw(dst, src1);
353
0
    case CC_OP_ADDL:
354
0
        return compute_c_addl(dst, src1);
355
356
0
    case CC_OP_ADCB:
357
0
        return compute_c_adcb(dst, src1, src2);
358
0
    case CC_OP_ADCW:
359
0
        return compute_c_adcw(dst, src1, src2);
360
0
    case CC_OP_ADCL:
361
0
        return compute_c_adcl(dst, src1, src2);
362
363
0
    case CC_OP_SUBB:
364
0
        return compute_c_subb(dst, src1);
365
0
    case CC_OP_SUBW:
366
0
        return compute_c_subw(dst, src1);
367
0
    case CC_OP_SUBL:
368
0
        return compute_c_subl(dst, src1);
369
370
0
    case CC_OP_SBBB:
371
0
        return compute_c_sbbb(dst, src1, src2);
372
0
    case CC_OP_SBBW:
373
0
        return compute_c_sbbw(dst, src1, src2);
374
0
    case CC_OP_SBBL:
375
0
        return compute_c_sbbl(dst, src1, src2);
376
377
0
    case CC_OP_SHLB:
378
0
    case CC_OP_CCMPB:
379
0
        return compute_c_shlb(dst, src1);
380
0
    case CC_OP_SHLW:
381
0
    case CC_OP_CCMPW:
382
0
        return compute_c_shlw(dst, src1);
383
0
    case CC_OP_SHLL:
384
0
    case CC_OP_CCMPL:
385
0
        return compute_c_shll(dst, src1);
386
387
0
    case CC_OP_BMILGB:
388
0
        return compute_c_bmilgb(dst, src1);
389
0
    case CC_OP_BMILGW:
390
0
        return compute_c_bmilgw(dst, src1);
391
0
    case CC_OP_BMILGL:
392
0
        return compute_c_bmilgl(dst, src1);
393
394
0
    case CC_OP_BLSIB:
395
0
        return compute_c_blsib(dst, src1);
396
0
    case CC_OP_BLSIW:
397
0
        return compute_c_blsiw(dst, src1);
398
0
    case CC_OP_BLSIL:
399
0
        return compute_c_blsil(dst, src1);
400
401
#ifdef TARGET_X86_64
402
    case CC_OP_ADDQ:
403
        return compute_c_addq(dst, src1);
404
    case CC_OP_ADCQ:
405
        return compute_c_adcq(dst, src1, src2);
406
    case CC_OP_SUBQ:
407
        return compute_c_subq(dst, src1);
408
    case CC_OP_SBBQ:
409
        return compute_c_sbbq(dst, src1, src2);
410
    case CC_OP_SHLQ:
411
    case CC_OP_CCMPQ:
412
        return compute_c_shlq(dst, src1);
413
    case CC_OP_BMILGQ:
414
        return compute_c_bmilgq(dst, src1);
415
    case CC_OP_BLSIQ:
416
        return compute_c_blsiq(dst, src1);
417
#endif
418
0
    }
419
0
}
420
421
void HELPER(write_eflags)(CPUX86State *env, target_ulong t0,
422
                         uint32_t update_mask)
423
0
{
424
0
    cpu_load_eflags(env, t0, update_mask);
425
0
}
426
427
target_ulong HELPER(read_eflags)(CPUX86State *env)
428
0
{
429
0
    uint32_t eflags;
430
431
0
    CC_SRC = eflags = cpu_cc_compute_all(env);
432
0
    CC_OP = CC_OP_EFLAGS;
433
434
0
    eflags |= (env->df & DF_MASK);
435
0
    eflags |= env->eflags & ~(VM_MASK | RF_MASK);
436
0
    return eflags;
437
0
}
438
439
void HELPER(clts)(CPUX86State *env)
440
0
{
441
0
    env->cr[0] &= ~CR0_TS_MASK;
442
0
    env->hflags &= ~HF_TS_MASK;
443
0
}