2 * S/390 condition code helper routines
4 * Copyright (c) 2009 Ulrich Hecht
5 * Copyright (c) 2009 Alexander Graf
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 #include "dyngen-exec.h"
25 /* #define DEBUG_HELPER */
27 #define HELPER_LOG(x...) qemu_log(x)
29 #define HELPER_LOG(x...)
32 static inline uint32_t cc_calc_ltgt_32(CPUS390XState *env, int32_t src,
37 } else if (src < dst) {
44 static inline uint32_t cc_calc_ltgt0_32(CPUS390XState *env, int32_t dst)
46 return cc_calc_ltgt_32(env, dst, 0);
49 static inline uint32_t cc_calc_ltgt_64(CPUS390XState *env, int64_t src,
54 } else if (src < dst) {
61 static inline uint32_t cc_calc_ltgt0_64(CPUS390XState *env, int64_t dst)
63 return cc_calc_ltgt_64(env, dst, 0);
66 static inline uint32_t cc_calc_ltugtu_32(CPUS390XState *env, uint32_t src,
71 } else if (src < dst) {
78 static inline uint32_t cc_calc_ltugtu_64(CPUS390XState *env, uint64_t src,
83 } else if (src < dst) {
90 static inline uint32_t cc_calc_tm_32(CPUS390XState *env, uint32_t val,
93 uint16_t r = val & mask;
95 HELPER_LOG("%s: val 0x%x mask 0x%x\n", __func__, val, mask);
96 if (r == 0 || mask == 0) {
98 } else if (r == mask) {
105 /* set condition code for test under mask */
106 static inline uint32_t cc_calc_tm_64(CPUS390XState *env, uint64_t val,
109 uint16_t r = val & mask;
111 HELPER_LOG("%s: val 0x%lx mask 0x%x r 0x%x\n", __func__, val, mask, r);
112 if (r == 0 || mask == 0) {
114 } else if (r == mask) {
117 while (!(mask & 0x8000)) {
129 static inline uint32_t cc_calc_nz(CPUS390XState *env, uint64_t dst)
134 static inline uint32_t cc_calc_add_64(CPUS390XState *env, int64_t a1,
135 int64_t a2, int64_t ar)
137 if ((a1 > 0 && a2 > 0 && ar < 0) || (a1 < 0 && a2 < 0 && ar > 0)) {
138 return 3; /* overflow */
150 static inline uint32_t cc_calc_addu_64(CPUS390XState *env, uint64_t a1,
151 uint64_t a2, uint64_t ar)
160 if (ar < a1 || ar < a2) {
168 static inline uint32_t cc_calc_sub_64(CPUS390XState *env, int64_t a1,
169 int64_t a2, int64_t ar)
171 if ((a1 > 0 && a2 < 0 && ar < 0) || (a1 < 0 && a2 > 0 && ar > 0)) {
172 return 3; /* overflow */
184 static inline uint32_t cc_calc_subu_64(CPUS390XState *env, uint64_t a1,
185 uint64_t a2, uint64_t ar)
198 static inline uint32_t cc_calc_abs_64(CPUS390XState *env, int64_t dst)
200 if ((uint64_t)dst == 0x8000000000000000ULL) {
209 static inline uint32_t cc_calc_nabs_64(CPUS390XState *env, int64_t dst)
214 static inline uint32_t cc_calc_comp_64(CPUS390XState *env, int64_t dst)
216 if ((uint64_t)dst == 0x8000000000000000ULL) {
218 } else if (dst < 0) {
220 } else if (dst > 0) {
228 static inline uint32_t cc_calc_add_32(CPUS390XState *env, int32_t a1,
229 int32_t a2, int32_t ar)
231 if ((a1 > 0 && a2 > 0 && ar < 0) || (a1 < 0 && a2 < 0 && ar > 0)) {
232 return 3; /* overflow */
244 static inline uint32_t cc_calc_addu_32(CPUS390XState *env, uint32_t a1,
245 uint32_t a2, uint32_t ar)
254 if (ar < a1 || ar < a2) {
262 static inline uint32_t cc_calc_sub_32(CPUS390XState *env, int32_t a1,
263 int32_t a2, int32_t ar)
265 if ((a1 > 0 && a2 < 0 && ar < 0) || (a1 < 0 && a2 > 0 && ar > 0)) {
266 return 3; /* overflow */
278 static inline uint32_t cc_calc_subu_32(CPUS390XState *env, uint32_t a1,
279 uint32_t a2, uint32_t ar)
292 static inline uint32_t cc_calc_abs_32(CPUS390XState *env, int32_t dst)
294 if ((uint32_t)dst == 0x80000000UL) {
303 static inline uint32_t cc_calc_nabs_32(CPUS390XState *env, int32_t dst)
308 static inline uint32_t cc_calc_comp_32(CPUS390XState *env, int32_t dst)
310 if ((uint32_t)dst == 0x80000000UL) {
312 } else if (dst < 0) {
314 } else if (dst > 0) {
321 /* calculate condition code for insert character under mask insn */
322 static inline uint32_t cc_calc_icm_32(CPUS390XState *env, uint32_t mask,
327 HELPER_LOG("%s: mask 0x%x val %d\n", __func__, mask, val);
331 } else if (val & 0x80000000) {
354 static inline uint32_t cc_calc_slag(CPUS390XState *env, uint64_t src,
357 uint64_t mask = ((1ULL << shift) - 1ULL) << (64 - shift);
360 /* check if the sign bit stays the same */
361 if (src & (1ULL << 63)) {
367 if ((src & mask) != match) {
372 r = ((src << shift) & ((1ULL << 63) - 1)) | (src & (1ULL << 63));
374 if ((int64_t)r == 0) {
376 } else if ((int64_t)r < 0) {
384 static inline uint32_t do_calc_cc(CPUS390XState *env, uint32_t cc_op,
385 uint64_t src, uint64_t dst, uint64_t vr)
394 /* cc_op value _is_ cc */
398 r = cc_calc_ltgt0_32(env, dst);
401 r = cc_calc_ltgt0_64(env, dst);
404 r = cc_calc_ltgt_32(env, src, dst);
407 r = cc_calc_ltgt_64(env, src, dst);
409 case CC_OP_LTUGTU_32:
410 r = cc_calc_ltugtu_32(env, src, dst);
412 case CC_OP_LTUGTU_64:
413 r = cc_calc_ltugtu_64(env, src, dst);
416 r = cc_calc_tm_32(env, src, dst);
419 r = cc_calc_tm_64(env, src, dst);
422 r = cc_calc_nz(env, dst);
425 r = cc_calc_add_64(env, src, dst, vr);
428 r = cc_calc_addu_64(env, src, dst, vr);
431 r = cc_calc_sub_64(env, src, dst, vr);
434 r = cc_calc_subu_64(env, src, dst, vr);
437 r = cc_calc_abs_64(env, dst);
440 r = cc_calc_nabs_64(env, dst);
443 r = cc_calc_comp_64(env, dst);
447 r = cc_calc_add_32(env, src, dst, vr);
450 r = cc_calc_addu_32(env, src, dst, vr);
453 r = cc_calc_sub_32(env, src, dst, vr);
456 r = cc_calc_subu_32(env, src, dst, vr);
459 r = cc_calc_abs_64(env, dst);
462 r = cc_calc_nabs_64(env, dst);
465 r = cc_calc_comp_32(env, dst);
469 r = cc_calc_icm_32(env, src, dst);
472 r = cc_calc_slag(env, src, dst);
476 r = set_cc_f32(env, src, dst);
479 r = set_cc_f64(env, src, dst);
482 r = set_cc_nz_f32(dst);
485 r = set_cc_nz_f64(dst);
489 cpu_abort(env, "Unknown CC operation: %s\n", cc_name(cc_op));
492 HELPER_LOG("%s: %15s 0x%016lx 0x%016lx 0x%016lx = %d\n", __func__,
493 cc_name(cc_op), src, dst, vr, r);
497 uint32_t calc_cc(CPUS390XState *env, uint32_t cc_op, uint64_t src, uint64_t dst,
500 return do_calc_cc(env, cc_op, src, dst, vr);
503 uint32_t HELPER(calc_cc)(uint32_t cc_op, uint64_t src, uint64_t dst,
506 return do_calc_cc(env, cc_op, src, dst, vr);
509 /* insert psw mask and condition code into r1 */
510 void HELPER(ipm)(uint32_t cc, uint32_t r1)
512 uint64_t r = env->regs[r1];
514 r &= 0xffffffff00ffffffULL;
515 r |= (cc << 28) | ((env->psw.mask >> 40) & 0xf);
517 HELPER_LOG("%s: cc %d psw.mask 0x%lx r1 0x%lx\n", __func__,
518 cc, env->psw.mask, r);
521 #ifndef CONFIG_USER_ONLY
522 void HELPER(load_psw)(uint64_t mask, uint64_t addr)
524 load_psw(env, mask, addr);
528 void HELPER(sacf)(uint64_t a1)
530 HELPER_LOG("%s: %16" PRIx64 "\n", __func__, a1);
532 switch (a1 & 0xf00) {
534 env->psw.mask &= ~PSW_MASK_ASC;
535 env->psw.mask |= PSW_ASC_PRIMARY;
538 env->psw.mask &= ~PSW_MASK_ASC;
539 env->psw.mask |= PSW_ASC_SECONDARY;
542 env->psw.mask &= ~PSW_MASK_ASC;
543 env->psw.mask |= PSW_ASC_HOME;
546 qemu_log("unknown sacf mode: %" PRIx64 "\n", a1);
547 program_interrupt(env, PGM_SPECIFICATION, 2);