translate.c 143.0 KB
Newer Older
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
/*
 * HPPA emulation cpu translation for qemu.
 *
 * Copyright (c) 2016 Richard Henderson <rth@twiddle.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include "qemu/osdep.h"
#include "cpu.h"
#include "disas/disas.h"
#include "qemu/host-utils.h"
#include "exec/exec-all.h"
#include "tcg-op.h"
#include "exec/cpu_ldst.h"
#include "exec/helper-proto.h"
#include "exec/helper-gen.h"
29
#include "exec/translator.h"
30 31 32
#include "trace-tcg.h"
#include "exec/log.h"

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
/* Since we have a distinction between register size and address size,
   we need to redefine all of these.  */

#undef TCGv
#undef tcg_temp_new
#undef tcg_global_reg_new
#undef tcg_global_mem_new
#undef tcg_temp_local_new
#undef tcg_temp_free

#if TARGET_LONG_BITS == 64
#define TCGv_tl              TCGv_i64
#define tcg_temp_new_tl      tcg_temp_new_i64
#define tcg_temp_free_tl     tcg_temp_free_i64
#if TARGET_REGISTER_BITS == 64
#define tcg_gen_extu_reg_tl  tcg_gen_mov_i64
#else
#define tcg_gen_extu_reg_tl  tcg_gen_extu_i32_i64
#endif
#else
#define TCGv_tl              TCGv_i32
#define tcg_temp_new_tl      tcg_temp_new_i32
#define tcg_temp_free_tl     tcg_temp_free_i32
#define tcg_gen_extu_reg_tl  tcg_gen_mov_i32
#endif

#if TARGET_REGISTER_BITS == 64
#define TCGv_reg             TCGv_i64

#define tcg_temp_new         tcg_temp_new_i64
#define tcg_global_reg_new   tcg_global_reg_new_i64
#define tcg_global_mem_new   tcg_global_mem_new_i64
#define tcg_temp_local_new   tcg_temp_local_new_i64
#define tcg_temp_free        tcg_temp_free_i64

#define tcg_gen_movi_reg     tcg_gen_movi_i64
#define tcg_gen_mov_reg      tcg_gen_mov_i64
#define tcg_gen_ld8u_reg     tcg_gen_ld8u_i64
#define tcg_gen_ld8s_reg     tcg_gen_ld8s_i64
#define tcg_gen_ld16u_reg    tcg_gen_ld16u_i64
#define tcg_gen_ld16s_reg    tcg_gen_ld16s_i64
#define tcg_gen_ld32u_reg    tcg_gen_ld32u_i64
#define tcg_gen_ld32s_reg    tcg_gen_ld32s_i64
#define tcg_gen_ld_reg       tcg_gen_ld_i64
#define tcg_gen_st8_reg      tcg_gen_st8_i64
#define tcg_gen_st16_reg     tcg_gen_st16_i64
#define tcg_gen_st32_reg     tcg_gen_st32_i64
#define tcg_gen_st_reg       tcg_gen_st_i64
#define tcg_gen_add_reg      tcg_gen_add_i64
#define tcg_gen_addi_reg     tcg_gen_addi_i64
#define tcg_gen_sub_reg      tcg_gen_sub_i64
#define tcg_gen_neg_reg      tcg_gen_neg_i64
#define tcg_gen_subfi_reg    tcg_gen_subfi_i64
#define tcg_gen_subi_reg     tcg_gen_subi_i64
#define tcg_gen_and_reg      tcg_gen_and_i64
#define tcg_gen_andi_reg     tcg_gen_andi_i64
#define tcg_gen_or_reg       tcg_gen_or_i64
#define tcg_gen_ori_reg      tcg_gen_ori_i64
#define tcg_gen_xor_reg      tcg_gen_xor_i64
#define tcg_gen_xori_reg     tcg_gen_xori_i64
#define tcg_gen_not_reg      tcg_gen_not_i64
#define tcg_gen_shl_reg      tcg_gen_shl_i64
#define tcg_gen_shli_reg     tcg_gen_shli_i64
#define tcg_gen_shr_reg      tcg_gen_shr_i64
#define tcg_gen_shri_reg     tcg_gen_shri_i64
#define tcg_gen_sar_reg      tcg_gen_sar_i64
#define tcg_gen_sari_reg     tcg_gen_sari_i64
#define tcg_gen_brcond_reg   tcg_gen_brcond_i64
#define tcg_gen_brcondi_reg  tcg_gen_brcondi_i64
#define tcg_gen_setcond_reg  tcg_gen_setcond_i64
#define tcg_gen_setcondi_reg tcg_gen_setcondi_i64
#define tcg_gen_mul_reg      tcg_gen_mul_i64
#define tcg_gen_muli_reg     tcg_gen_muli_i64
#define tcg_gen_div_reg      tcg_gen_div_i64
#define tcg_gen_rem_reg      tcg_gen_rem_i64
#define tcg_gen_divu_reg     tcg_gen_divu_i64
#define tcg_gen_remu_reg     tcg_gen_remu_i64
#define tcg_gen_discard_reg  tcg_gen_discard_i64
#define tcg_gen_trunc_reg_i32 tcg_gen_extrl_i64_i32
#define tcg_gen_trunc_i64_reg tcg_gen_mov_i64
#define tcg_gen_extu_i32_reg tcg_gen_extu_i32_i64
#define tcg_gen_ext_i32_reg  tcg_gen_ext_i32_i64
#define tcg_gen_extu_reg_i64 tcg_gen_mov_i64
#define tcg_gen_ext_reg_i64  tcg_gen_mov_i64
#define tcg_gen_ext8u_reg    tcg_gen_ext8u_i64
#define tcg_gen_ext8s_reg    tcg_gen_ext8s_i64
#define tcg_gen_ext16u_reg   tcg_gen_ext16u_i64
#define tcg_gen_ext16s_reg   tcg_gen_ext16s_i64
#define tcg_gen_ext32u_reg   tcg_gen_ext32u_i64
#define tcg_gen_ext32s_reg   tcg_gen_ext32s_i64
#define tcg_gen_bswap16_reg  tcg_gen_bswap16_i64
#define tcg_gen_bswap32_reg  tcg_gen_bswap32_i64
#define tcg_gen_bswap64_reg  tcg_gen_bswap64_i64
#define tcg_gen_concat_reg_i64 tcg_gen_concat32_i64
#define tcg_gen_andc_reg     tcg_gen_andc_i64
#define tcg_gen_eqv_reg      tcg_gen_eqv_i64
#define tcg_gen_nand_reg     tcg_gen_nand_i64
#define tcg_gen_nor_reg      tcg_gen_nor_i64
#define tcg_gen_orc_reg      tcg_gen_orc_i64
#define tcg_gen_clz_reg      tcg_gen_clz_i64
#define tcg_gen_ctz_reg      tcg_gen_ctz_i64
#define tcg_gen_clzi_reg     tcg_gen_clzi_i64
#define tcg_gen_ctzi_reg     tcg_gen_ctzi_i64
#define tcg_gen_clrsb_reg    tcg_gen_clrsb_i64
#define tcg_gen_ctpop_reg    tcg_gen_ctpop_i64
#define tcg_gen_rotl_reg     tcg_gen_rotl_i64
#define tcg_gen_rotli_reg    tcg_gen_rotli_i64
#define tcg_gen_rotr_reg     tcg_gen_rotr_i64
#define tcg_gen_rotri_reg    tcg_gen_rotri_i64
#define tcg_gen_deposit_reg  tcg_gen_deposit_i64
#define tcg_gen_deposit_z_reg tcg_gen_deposit_z_i64
#define tcg_gen_extract_reg  tcg_gen_extract_i64
#define tcg_gen_sextract_reg tcg_gen_sextract_i64
#define tcg_const_reg        tcg_const_i64
#define tcg_const_local_reg  tcg_const_local_i64
#define tcg_gen_movcond_reg  tcg_gen_movcond_i64
#define tcg_gen_add2_reg     tcg_gen_add2_i64
#define tcg_gen_sub2_reg     tcg_gen_sub2_i64
#define tcg_gen_qemu_ld_reg  tcg_gen_qemu_ld_i64
#define tcg_gen_qemu_st_reg  tcg_gen_qemu_st_i64
#define tcg_gen_atomic_xchg_reg tcg_gen_atomic_xchg_i64
#if UINTPTR_MAX == UINT32_MAX
# define tcg_gen_trunc_reg_ptr(p, r) \
    tcg_gen_trunc_i64_i32(TCGV_PTR_TO_NAT(p), r)
#else
# define tcg_gen_trunc_reg_ptr(p, r) \
    tcg_gen_mov_i64(TCGV_PTR_TO_NAT(p), r)
#endif
#else
#define TCGv_reg             TCGv_i32
#define tcg_temp_new         tcg_temp_new_i32
#define tcg_global_reg_new   tcg_global_reg_new_i32
#define tcg_global_mem_new   tcg_global_mem_new_i32
#define tcg_temp_local_new   tcg_temp_local_new_i32
#define tcg_temp_free        tcg_temp_free_i32

#define tcg_gen_movi_reg     tcg_gen_movi_i32
#define tcg_gen_mov_reg      tcg_gen_mov_i32
#define tcg_gen_ld8u_reg     tcg_gen_ld8u_i32
#define tcg_gen_ld8s_reg     tcg_gen_ld8s_i32
#define tcg_gen_ld16u_reg    tcg_gen_ld16u_i32
#define tcg_gen_ld16s_reg    tcg_gen_ld16s_i32
#define tcg_gen_ld32u_reg    tcg_gen_ld_i32
#define tcg_gen_ld32s_reg    tcg_gen_ld_i32
#define tcg_gen_ld_reg       tcg_gen_ld_i32
#define tcg_gen_st8_reg      tcg_gen_st8_i32
#define tcg_gen_st16_reg     tcg_gen_st16_i32
#define tcg_gen_st32_reg     tcg_gen_st32_i32
#define tcg_gen_st_reg       tcg_gen_st_i32
#define tcg_gen_add_reg      tcg_gen_add_i32
#define tcg_gen_addi_reg     tcg_gen_addi_i32
#define tcg_gen_sub_reg      tcg_gen_sub_i32
#define tcg_gen_neg_reg      tcg_gen_neg_i32
#define tcg_gen_subfi_reg    tcg_gen_subfi_i32
#define tcg_gen_subi_reg     tcg_gen_subi_i32
#define tcg_gen_and_reg      tcg_gen_and_i32
#define tcg_gen_andi_reg     tcg_gen_andi_i32
#define tcg_gen_or_reg       tcg_gen_or_i32
#define tcg_gen_ori_reg      tcg_gen_ori_i32
#define tcg_gen_xor_reg      tcg_gen_xor_i32
#define tcg_gen_xori_reg     tcg_gen_xori_i32
#define tcg_gen_not_reg      tcg_gen_not_i32
#define tcg_gen_shl_reg      tcg_gen_shl_i32
#define tcg_gen_shli_reg     tcg_gen_shli_i32
#define tcg_gen_shr_reg      tcg_gen_shr_i32
#define tcg_gen_shri_reg     tcg_gen_shri_i32
#define tcg_gen_sar_reg      tcg_gen_sar_i32
#define tcg_gen_sari_reg     tcg_gen_sari_i32
#define tcg_gen_brcond_reg   tcg_gen_brcond_i32
#define tcg_gen_brcondi_reg  tcg_gen_brcondi_i32
#define tcg_gen_setcond_reg  tcg_gen_setcond_i32
#define tcg_gen_setcondi_reg tcg_gen_setcondi_i32
#define tcg_gen_mul_reg      tcg_gen_mul_i32
#define tcg_gen_muli_reg     tcg_gen_muli_i32
#define tcg_gen_div_reg      tcg_gen_div_i32
#define tcg_gen_rem_reg      tcg_gen_rem_i32
#define tcg_gen_divu_reg     tcg_gen_divu_i32
#define tcg_gen_remu_reg     tcg_gen_remu_i32
#define tcg_gen_discard_reg  tcg_gen_discard_i32
#define tcg_gen_trunc_reg_i32 tcg_gen_mov_i32
#define tcg_gen_trunc_i64_reg tcg_gen_extrl_i64_i32
#define tcg_gen_extu_i32_reg tcg_gen_mov_i32
#define tcg_gen_ext_i32_reg  tcg_gen_mov_i32
#define tcg_gen_extu_reg_i64 tcg_gen_extu_i32_i64
#define tcg_gen_ext_reg_i64  tcg_gen_ext_i32_i64
#define tcg_gen_ext8u_reg    tcg_gen_ext8u_i32
#define tcg_gen_ext8s_reg    tcg_gen_ext8s_i32
#define tcg_gen_ext16u_reg   tcg_gen_ext16u_i32
#define tcg_gen_ext16s_reg   tcg_gen_ext16s_i32
#define tcg_gen_ext32u_reg   tcg_gen_mov_i32
#define tcg_gen_ext32s_reg   tcg_gen_mov_i32
#define tcg_gen_bswap16_reg  tcg_gen_bswap16_i32
#define tcg_gen_bswap32_reg  tcg_gen_bswap32_i32
#define tcg_gen_concat_reg_i64 tcg_gen_concat_i32_i64
#define tcg_gen_andc_reg     tcg_gen_andc_i32
#define tcg_gen_eqv_reg      tcg_gen_eqv_i32
#define tcg_gen_nand_reg     tcg_gen_nand_i32
#define tcg_gen_nor_reg      tcg_gen_nor_i32
#define tcg_gen_orc_reg      tcg_gen_orc_i32
#define tcg_gen_clz_reg      tcg_gen_clz_i32
#define tcg_gen_ctz_reg      tcg_gen_ctz_i32
#define tcg_gen_clzi_reg     tcg_gen_clzi_i32
#define tcg_gen_ctzi_reg     tcg_gen_ctzi_i32
#define tcg_gen_clrsb_reg    tcg_gen_clrsb_i32
#define tcg_gen_ctpop_reg    tcg_gen_ctpop_i32
#define tcg_gen_rotl_reg     tcg_gen_rotl_i32
#define tcg_gen_rotli_reg    tcg_gen_rotli_i32
#define tcg_gen_rotr_reg     tcg_gen_rotr_i32
#define tcg_gen_rotri_reg    tcg_gen_rotri_i32
#define tcg_gen_deposit_reg  tcg_gen_deposit_i32
#define tcg_gen_deposit_z_reg tcg_gen_deposit_z_i32
#define tcg_gen_extract_reg  tcg_gen_extract_i32
#define tcg_gen_sextract_reg tcg_gen_sextract_i32
#define tcg_const_reg        tcg_const_i32
#define tcg_const_local_reg  tcg_const_local_i32
#define tcg_gen_movcond_reg  tcg_gen_movcond_i32
#define tcg_gen_add2_reg     tcg_gen_add2_i32
#define tcg_gen_sub2_reg     tcg_gen_sub2_i32
#define tcg_gen_qemu_ld_reg  tcg_gen_qemu_ld_i32
#define tcg_gen_qemu_st_reg  tcg_gen_qemu_st_i32
#define tcg_gen_atomic_xchg_reg tcg_gen_atomic_xchg_i32
#if UINTPTR_MAX == UINT32_MAX
# define tcg_gen_trunc_reg_ptr(p, r) \
    tcg_gen_mov_i32(TCGV_PTR_TO_NAT(p), r)
#else
# define tcg_gen_trunc_reg_ptr(p, r) \
    tcg_gen_extu_i32_i64(TCGV_PTR_TO_NAT(p), r)
#endif
#endif /* TARGET_REGISTER_BITS */

263 264
typedef struct DisasCond {
    TCGCond c;
265
    TCGv_reg a0, a1;
266 267 268 269 270
    bool a0_is_n;
    bool a1_is_0;
} DisasCond;

typedef struct DisasContext {
271
    DisasContextBase base;
272 273
    CPUState *cs;

274 275 276 277
    target_ureg iaoq_f;
    target_ureg iaoq_b;
    target_ureg iaoq_n;
    TCGv_reg iaoq_n_var;
278

279 280 281
    int ntempr, ntempl;
    TCGv_reg tempr[4];
    TCGv_tl  templ[4];
282 283 284 285

    DisasCond null_cond;
    TCGLabel *null_lab;

286
    uint32_t insn;
287
    uint32_t tb_flags;
288 289
    int mmu_idx;
    int privilege;
290 291 292
    bool psw_n_nonzero;
} DisasContext;

293 294 295
/* Target-specific return values from translate_one, indicating the
   state of the TB.  Note that DISAS_NEXT indicates that we are not
   exiting the TB.  */
296

297 298 299
/* We are not using a goto_tb (for whatever reason), but have updated
   the iaq (for whatever reason), so don't do it again on exit.  */
#define DISAS_IAQ_N_UPDATED  DISAS_TARGET_0
300

301 302 303
/* We are exiting the TB, but have neither emitted a goto_tb, nor
   updated the iaq for the next instruction to be executed.  */
#define DISAS_IAQ_N_STALE    DISAS_TARGET_1
304

305 306 307 308
/* Similarly, but we want to return to the main loop immediately
   to recognize unmasked interrupts.  */
#define DISAS_IAQ_N_STALE_EXIT      DISAS_TARGET_2

309 310
typedef struct DisasInsn {
    uint32_t insn, mask;
311 312
    DisasJumpType (*trans)(DisasContext *ctx, uint32_t insn,
                           const struct DisasInsn *f);
313
    union {
314
        void (*ttt)(TCGv_reg, TCGv_reg, TCGv_reg);
315 316 317 318 319 320 321
        void (*weww)(TCGv_i32, TCGv_env, TCGv_i32, TCGv_i32);
        void (*dedd)(TCGv_i64, TCGv_env, TCGv_i64, TCGv_i64);
        void (*wew)(TCGv_i32, TCGv_env, TCGv_i32);
        void (*ded)(TCGv_i64, TCGv_env, TCGv_i64);
        void (*wed)(TCGv_i32, TCGv_env, TCGv_i64);
        void (*dew)(TCGv_i64, TCGv_env, TCGv_i32);
    } f;
322 323 324
} DisasInsn;

/* global register indexes */
325
static TCGv_reg cpu_gr[32];
326
static TCGv_i64 cpu_sr[4];
327
static TCGv_i64 cpu_srH;
328 329
static TCGv_reg cpu_iaoq_f;
static TCGv_reg cpu_iaoq_b;
R
Richard Henderson 已提交
330 331
static TCGv_i64 cpu_iasq_f;
static TCGv_i64 cpu_iasq_b;
332 333 334 335 336
static TCGv_reg cpu_sar;
static TCGv_reg cpu_psw_n;
static TCGv_reg cpu_psw_v;
static TCGv_reg cpu_psw_cb;
static TCGv_reg cpu_psw_cb_msb;
337 338 339 340 341 342 343

#include "exec/gen-icount.h"

void hppa_translate_init(void)
{
#define DEF_VAR(V)  { &cpu_##V, #V, offsetof(CPUHPPAState, V) }

344
    typedef struct { TCGv_reg *var; const char *name; int ofs; } GlobalVar;
345
    static const GlobalVar vars[] = {
346
        { &cpu_sar, "sar", offsetof(CPUHPPAState, cr[CR_SAR]) },
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
        DEF_VAR(psw_n),
        DEF_VAR(psw_v),
        DEF_VAR(psw_cb),
        DEF_VAR(psw_cb_msb),
        DEF_VAR(iaoq_f),
        DEF_VAR(iaoq_b),
    };

#undef DEF_VAR

    /* Use the symbolic register names that match the disassembler.  */
    static const char gr_names[32][4] = {
        "r0",  "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
        "r8",  "r9",  "r10", "r11", "r12", "r13", "r14", "r15",
        "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
        "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
    };
364
    /* SR[4-7] are not global registers so that we can index them.  */
365 366
    static const char sr_names[5][4] = {
        "sr0", "sr1", "sr2", "sr3", "srH"
367
    };
368 369 370

    int i;

371
    cpu_gr[0] = NULL;
372 373 374 375 376
    for (i = 1; i < 32; i++) {
        cpu_gr[i] = tcg_global_mem_new(cpu_env,
                                       offsetof(CPUHPPAState, gr[i]),
                                       gr_names[i]);
    }
377 378 379 380 381
    for (i = 0; i < 4; i++) {
        cpu_sr[i] = tcg_global_mem_new_i64(cpu_env,
                                           offsetof(CPUHPPAState, sr[i]),
                                           sr_names[i]);
    }
382 383 384
    cpu_srH = tcg_global_mem_new_i64(cpu_env,
                                     offsetof(CPUHPPAState, sr[4]),
                                     sr_names[4]);
385 386 387 388 389

    for (i = 0; i < ARRAY_SIZE(vars); ++i) {
        const GlobalVar *v = &vars[i];
        *v->var = tcg_global_mem_new(cpu_env, v->ofs, v->name);
    }
R
Richard Henderson 已提交
390 391 392 393 394 395 396

    cpu_iasq_f = tcg_global_mem_new_i64(cpu_env,
                                        offsetof(CPUHPPAState, iasq_f),
                                        "iasq_f");
    cpu_iasq_b = tcg_global_mem_new_i64(cpu_env,
                                        offsetof(CPUHPPAState, iasq_b),
                                        "iasq_b");
397 398
}

399 400
static DisasCond cond_make_f(void)
{
401 402 403 404 405
    return (DisasCond){
        .c = TCG_COND_NEVER,
        .a0 = NULL,
        .a1 = NULL,
    };
406 407 408 409
}

static DisasCond cond_make_n(void)
{
410 411 412 413 414 415 416
    return (DisasCond){
        .c = TCG_COND_NE,
        .a0 = cpu_psw_n,
        .a0_is_n = true,
        .a1 = NULL,
        .a1_is_0 = true
    };
417 418
}

419
static DisasCond cond_make_0(TCGCond c, TCGv_reg a0)
420
{
421
    DisasCond r = { .c = c, .a1 = NULL, .a1_is_0 = true };
422 423 424

    assert (c != TCG_COND_NEVER && c != TCG_COND_ALWAYS);
    r.a0 = tcg_temp_new();
425
    tcg_gen_mov_reg(r.a0, a0);
426 427 428 429

    return r;
}

430
static DisasCond cond_make(TCGCond c, TCGv_reg a0, TCGv_reg a1)
431 432 433 434 435
{
    DisasCond r = { .c = c };

    assert (c != TCG_COND_NEVER && c != TCG_COND_ALWAYS);
    r.a0 = tcg_temp_new();
436
    tcg_gen_mov_reg(r.a0, a0);
437
    r.a1 = tcg_temp_new();
438
    tcg_gen_mov_reg(r.a1, a1);
439 440 441 442 443 444 445 446

    return r;
}

static void cond_prep(DisasCond *cond)
{
    if (cond->a1_is_0) {
        cond->a1_is_0 = false;
447
        cond->a1 = tcg_const_reg(0);
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
    }
}

static void cond_free(DisasCond *cond)
{
    switch (cond->c) {
    default:
        if (!cond->a0_is_n) {
            tcg_temp_free(cond->a0);
        }
        if (!cond->a1_is_0) {
            tcg_temp_free(cond->a1);
        }
        cond->a0_is_n = false;
        cond->a1_is_0 = false;
463 464
        cond->a0 = NULL;
        cond->a1 = NULL;
465 466 467 468 469 470 471 472 473
        /* fallthru */
    case TCG_COND_ALWAYS:
        cond->c = TCG_COND_NEVER;
        break;
    case TCG_COND_NEVER:
        break;
    }
}

474
static TCGv_reg get_temp(DisasContext *ctx)
475
{
476 477 478
    unsigned i = ctx->ntempr++;
    g_assert(i < ARRAY_SIZE(ctx->tempr));
    return ctx->tempr[i] = tcg_temp_new();
479 480
}

481 482 483 484 485 486 487 488 489
#ifndef CONFIG_USER_ONLY
static TCGv_tl get_temp_tl(DisasContext *ctx)
{
    unsigned i = ctx->ntempl++;
    g_assert(i < ARRAY_SIZE(ctx->templ));
    return ctx->templ[i] = tcg_temp_new_tl();
}
#endif

490
static TCGv_reg load_const(DisasContext *ctx, target_sreg v)
491
{
492 493
    TCGv_reg t = get_temp(ctx);
    tcg_gen_movi_reg(t, v);
494 495 496
    return t;
}

497
static TCGv_reg load_gpr(DisasContext *ctx, unsigned reg)
498 499
{
    if (reg == 0) {
500 501
        TCGv_reg t = get_temp(ctx);
        tcg_gen_movi_reg(t, 0);
502 503 504 505 506 507
        return t;
    } else {
        return cpu_gr[reg];
    }
}

508
static TCGv_reg dest_gpr(DisasContext *ctx, unsigned reg)
509
{
510
    if (reg == 0 || ctx->null_cond.c != TCG_COND_NEVER) {
511 512 513 514 515 516
        return get_temp(ctx);
    } else {
        return cpu_gr[reg];
    }
}

517
static void save_or_nullify(DisasContext *ctx, TCGv_reg dest, TCGv_reg t)
518 519 520
{
    if (ctx->null_cond.c != TCG_COND_NEVER) {
        cond_prep(&ctx->null_cond);
521
        tcg_gen_movcond_reg(ctx->null_cond.c, dest, ctx->null_cond.a0,
522 523
                           ctx->null_cond.a1, dest, t);
    } else {
524
        tcg_gen_mov_reg(dest, t);
525 526 527
    }
}

528
static void save_gpr(DisasContext *ctx, unsigned reg, TCGv_reg t)
529 530 531 532 533 534
{
    if (reg != 0) {
        save_or_nullify(ctx, cpu_gr[reg], t);
    }
}

535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
#ifdef HOST_WORDS_BIGENDIAN
# define HI_OFS  0
# define LO_OFS  4
#else
# define HI_OFS  4
# define LO_OFS  0
#endif

static TCGv_i32 load_frw_i32(unsigned rt)
{
    TCGv_i32 ret = tcg_temp_new_i32();
    tcg_gen_ld_i32(ret, cpu_env,
                   offsetof(CPUHPPAState, fr[rt & 31])
                   + (rt & 32 ? LO_OFS : HI_OFS));
    return ret;
}

552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
static TCGv_i32 load_frw0_i32(unsigned rt)
{
    if (rt == 0) {
        return tcg_const_i32(0);
    } else {
        return load_frw_i32(rt);
    }
}

static TCGv_i64 load_frw0_i64(unsigned rt)
{
    if (rt == 0) {
        return tcg_const_i64(0);
    } else {
        TCGv_i64 ret = tcg_temp_new_i64();
        tcg_gen_ld32u_i64(ret, cpu_env,
                          offsetof(CPUHPPAState, fr[rt & 31])
                          + (rt & 32 ? LO_OFS : HI_OFS));
        return ret;
    }
}

574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
static void save_frw_i32(unsigned rt, TCGv_i32 val)
{
    tcg_gen_st_i32(val, cpu_env,
                   offsetof(CPUHPPAState, fr[rt & 31])
                   + (rt & 32 ? LO_OFS : HI_OFS));
}

#undef HI_OFS
#undef LO_OFS

static TCGv_i64 load_frd(unsigned rt)
{
    TCGv_i64 ret = tcg_temp_new_i64();
    tcg_gen_ld_i64(ret, cpu_env, offsetof(CPUHPPAState, fr[rt]));
    return ret;
}

591 592 593 594 595 596 597 598 599
static TCGv_i64 load_frd0(unsigned rt)
{
    if (rt == 0) {
        return tcg_const_i64(0);
    } else {
        return load_frd(rt);
    }
}

600 601 602 603 604
static void save_frd(unsigned rt, TCGv_i64 val)
{
    tcg_gen_st_i64(val, cpu_env, offsetof(CPUHPPAState, fr[rt]));
}

605 606 607 608 609 610 611
static void load_spr(DisasContext *ctx, TCGv_i64 dest, unsigned reg)
{
#ifdef CONFIG_USER_ONLY
    tcg_gen_movi_i64(dest, 0);
#else
    if (reg < 4) {
        tcg_gen_mov_i64(dest, cpu_sr[reg]);
612 613
    } else if (ctx->tb_flags & TB_FLAG_SR_SAME) {
        tcg_gen_mov_i64(dest, cpu_srH);
614 615 616 617 618 619
    } else {
        tcg_gen_ld_i64(dest, cpu_env, offsetof(CPUHPPAState, sr[reg]));
    }
#endif
}

620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
/* Skip over the implementation of an insn that has been nullified.
   Use this when the insn is too complex for a conditional move.  */
static void nullify_over(DisasContext *ctx)
{
    if (ctx->null_cond.c != TCG_COND_NEVER) {
        /* The always condition should have been handled in the main loop.  */
        assert(ctx->null_cond.c != TCG_COND_ALWAYS);

        ctx->null_lab = gen_new_label();
        cond_prep(&ctx->null_cond);

        /* If we're using PSW[N], copy it to a temp because... */
        if (ctx->null_cond.a0_is_n) {
            ctx->null_cond.a0_is_n = false;
            ctx->null_cond.a0 = tcg_temp_new();
635
            tcg_gen_mov_reg(ctx->null_cond.a0, cpu_psw_n);
636 637 638 639 640 641
        }
        /* ... we clear it before branching over the implementation,
           so that (1) it's clear after nullifying this insn and
           (2) if this insn nullifies the next, PSW[N] is valid.  */
        if (ctx->psw_n_nonzero) {
            ctx->psw_n_nonzero = false;
642
            tcg_gen_movi_reg(cpu_psw_n, 0);
643 644
        }

645
        tcg_gen_brcond_reg(ctx->null_cond.c, ctx->null_cond.a0,
646 647 648 649 650 651 652 653 654 655
                          ctx->null_cond.a1, ctx->null_lab);
        cond_free(&ctx->null_cond);
    }
}

/* Save the current nullification state to PSW[N].  */
static void nullify_save(DisasContext *ctx)
{
    if (ctx->null_cond.c == TCG_COND_NEVER) {
        if (ctx->psw_n_nonzero) {
656
            tcg_gen_movi_reg(cpu_psw_n, 0);
657 658 659 660 661
        }
        return;
    }
    if (!ctx->null_cond.a0_is_n) {
        cond_prep(&ctx->null_cond);
662
        tcg_gen_setcond_reg(ctx->null_cond.c, cpu_psw_n,
663 664 665 666 667 668 669 670 671 672 673 674
                           ctx->null_cond.a0, ctx->null_cond.a1);
        ctx->psw_n_nonzero = true;
    }
    cond_free(&ctx->null_cond);
}

/* Set a PSW[N] to X.  The intention is that this is used immediately
   before a goto_tb/exit_tb, so that there is no fallthru path to other
   code within the TB.  Therefore we do not update psw_n_nonzero.  */
static void nullify_set(DisasContext *ctx, bool x)
{
    if (ctx->psw_n_nonzero || x) {
675
        tcg_gen_movi_reg(cpu_psw_n, x);
676 677 678 679 680
    }
}

/* Mark the end of an instruction that may have been nullified.
   This is the pair to nullify_over.  */
681
static DisasJumpType nullify_end(DisasContext *ctx, DisasJumpType status)
682 683 684
{
    TCGLabel *null_lab = ctx->null_lab;

R
Richard Henderson 已提交
685 686 687 688
    /* For NEXT, NORETURN, STALE, we can easily continue (or exit).
       For UPDATED, we cannot update on the nullified path.  */
    assert(status != DISAS_IAQ_N_UPDATED);

689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
    if (likely(null_lab == NULL)) {
        /* The current insn wasn't conditional or handled the condition
           applied to it without a branch, so the (new) setting of
           NULL_COND can be applied directly to the next insn.  */
        return status;
    }
    ctx->null_lab = NULL;

    if (likely(ctx->null_cond.c == TCG_COND_NEVER)) {
        /* The next instruction will be unconditional,
           and NULL_COND already reflects that.  */
        gen_set_label(null_lab);
    } else {
        /* The insn that we just executed is itself nullifying the next
           instruction.  Store the condition in the PSW[N] global.
           We asserted PSW[N] = 0 in nullify_over, so that after the
           label we have the proper value in place.  */
        nullify_save(ctx);
        gen_set_label(null_lab);
        ctx->null_cond = cond_make_n();
    }
710 711
    if (status == DISAS_NORETURN) {
        status = DISAS_NEXT;
712 713 714 715
    }
    return status;
}

716
static void copy_iaoq_entry(TCGv_reg dest, target_ureg ival, TCGv_reg vval)
717 718
{
    if (unlikely(ival == -1)) {
719
        tcg_gen_mov_reg(dest, vval);
720
    } else {
721
        tcg_gen_movi_reg(dest, ival);
722 723 724
    }
}

725
static inline target_ureg iaoq_dest(DisasContext *ctx, target_sreg disp)
726 727 728 729 730 731 732 733 734 735 736
{
    return ctx->iaoq_f + disp + 8;
}

static void gen_excp_1(int exception)
{
    TCGv_i32 t = tcg_const_i32(exception);
    gen_helper_excp(cpu_env, t);
    tcg_temp_free_i32(t);
}

737
static DisasJumpType gen_excp(DisasContext *ctx, int exception)
738 739 740
{
    copy_iaoq_entry(cpu_iaoq_f, ctx->iaoq_f, cpu_iaoq_f);
    copy_iaoq_entry(cpu_iaoq_b, ctx->iaoq_b, cpu_iaoq_b);
741
    nullify_save(ctx);
742
    gen_excp_1(exception);
743
    return DISAS_NORETURN;
744 745
}

746 747 748 749 750 751 752 753
static DisasJumpType gen_excp_iir(DisasContext *ctx, int exc)
{
    TCGv_reg tmp = tcg_const_reg(ctx->insn);
    tcg_gen_st_reg(tmp, cpu_env, offsetof(CPUHPPAState, cr[CR_IIR]));
    tcg_temp_free(tmp);
    return gen_excp(ctx, exc);
}

754
static DisasJumpType gen_illegal(DisasContext *ctx)
755
{
756
    nullify_over(ctx);
757
    return nullify_end(ctx, gen_excp_iir(ctx, EXCP_ILL));
758 759
}

760 761 762 763
#define CHECK_MOST_PRIVILEGED(EXCP)                               \
    do {                                                          \
        if (ctx->privilege != 0) {                                \
            nullify_over(ctx);                                    \
764
            return nullify_end(ctx, gen_excp_iir(ctx, EXCP));     \
765 766 767
        }                                                         \
    } while (0)

768
static bool use_goto_tb(DisasContext *ctx, target_ureg dest)
769 770
{
    /* Suppress goto_tb in the case of single-steping and IO.  */
771
    if ((tb_cflags(ctx->base.tb) & CF_LAST_IO) || ctx->base.singlestep_enabled) {
772 773 774 775 776
        return false;
    }
    return true;
}

777 778 779 780 781 782 783 784 785 786
/* If the next insn is to be nullified, and it's on the same page,
   and we're not attempting to set a breakpoint on it, then we can
   totally skip the nullified insn.  This avoids creating and
   executing a TB that merely branches to the next TB.  */
static bool use_nullify_skip(DisasContext *ctx)
{
    return (((ctx->iaoq_b ^ ctx->iaoq_f) & TARGET_PAGE_MASK) == 0
            && !cpu_breakpoint_test(ctx->cs, ctx->iaoq_b, BP_ANY));
}

787
static void gen_goto_tb(DisasContext *ctx, int which,
788
                        target_ureg f, target_ureg b)
789 790 791
{
    if (f != -1 && b != -1 && use_goto_tb(ctx, f)) {
        tcg_gen_goto_tb(which);
792 793
        tcg_gen_movi_reg(cpu_iaoq_f, f);
        tcg_gen_movi_reg(cpu_iaoq_b, b);
794
        tcg_gen_exit_tb((uintptr_t)ctx->base.tb + which);
795 796 797
    } else {
        copy_iaoq_entry(cpu_iaoq_f, f, cpu_iaoq_b);
        copy_iaoq_entry(cpu_iaoq_b, b, ctx->iaoq_n_var);
798
        if (ctx->base.singlestep_enabled) {
799 800
            gen_excp_1(EXCP_DEBUG);
        } else {
801
            tcg_gen_lookup_and_goto_ptr();
802 803 804 805
        }
    }
}

806 807
/* PA has a habit of taking the LSB of a field and using that as the sign,
   with the rest of the field becoming the least significant bits.  */
808
static target_sreg low_sextract(uint32_t val, int pos, int len)
809
{
810
    target_ureg x = -(target_ureg)extract32(val, pos, 1);
811 812 813 814
    x = (x << (len - 1)) | extract32(val, pos + 1, len - 1);
    return x;
}

815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
static unsigned assemble_rt64(uint32_t insn)
{
    unsigned r1 = extract32(insn, 6, 1);
    unsigned r0 = extract32(insn, 0, 5);
    return r1 * 32 + r0;
}

static unsigned assemble_ra64(uint32_t insn)
{
    unsigned r1 = extract32(insn, 7, 1);
    unsigned r0 = extract32(insn, 21, 5);
    return r1 * 32 + r0;
}

static unsigned assemble_rb64(uint32_t insn)
{
    unsigned r1 = extract32(insn, 12, 1);
    unsigned r0 = extract32(insn, 16, 5);
    return r1 * 32 + r0;
}

static unsigned assemble_rc64(uint32_t insn)
{
    unsigned r2 = extract32(insn, 8, 1);
    unsigned r1 = extract32(insn, 13, 3);
    unsigned r0 = extract32(insn, 9, 2);
    return r2 * 32 + r1 * 4 + r0;
}

844 845 846 847 848 849 850
static unsigned assemble_sr3(uint32_t insn)
{
    unsigned s2 = extract32(insn, 13, 1);
    unsigned s0 = extract32(insn, 14, 2);
    return s2 * 4 + s0;
}

851
static target_sreg assemble_12(uint32_t insn)
852
{
853
    target_ureg x = -(target_ureg)(insn & 1);
854 855 856 857 858
    x = (x <<  1) | extract32(insn, 2, 1);
    x = (x << 10) | extract32(insn, 3, 10);
    return x;
}

859
static target_sreg assemble_16(uint32_t insn)
860 861 862 863 864 865 866
{
    /* Take the name from PA2.0, which produces a 16-bit number
       only with wide mode; otherwise a 14-bit number.  Since we don't
       implement wide mode, this is always the 14-bit number.  */
    return low_sextract(insn, 0, 14);
}

867
static target_sreg assemble_16a(uint32_t insn)
868 869 870 871
{
    /* Take the name from PA2.0, which produces a 14-bit shifted number
       only with wide mode; otherwise a 12-bit shifted number.  Since we
       don't implement wide mode, this is always the 12-bit number.  */
872
    target_ureg x = -(target_ureg)(insn & 1);
873 874 875 876
    x = (x << 11) | extract32(insn, 2, 11);
    return x << 2;
}

877
static target_sreg assemble_17(uint32_t insn)
878
{
879
    target_ureg x = -(target_ureg)(insn & 1);
880 881 882 883 884 885
    x = (x <<  5) | extract32(insn, 16, 5);
    x = (x <<  1) | extract32(insn, 2, 1);
    x = (x << 10) | extract32(insn, 3, 10);
    return x << 2;
}

886
static target_sreg assemble_21(uint32_t insn)
887
{
888
    target_ureg x = -(target_ureg)(insn & 1);
889 890 891 892 893 894 895
    x = (x << 11) | extract32(insn, 1, 11);
    x = (x <<  2) | extract32(insn, 14, 2);
    x = (x <<  5) | extract32(insn, 16, 5);
    x = (x <<  2) | extract32(insn, 12, 2);
    return x << 11;
}

896
static target_sreg assemble_22(uint32_t insn)
897
{
898
    target_ureg x = -(target_ureg)(insn & 1);
899 900 901 902 903 904
    x = (x << 10) | extract32(insn, 16, 10);
    x = (x <<  1) | extract32(insn, 2, 1);
    x = (x << 10) | extract32(insn, 3, 10);
    return x << 2;
}

905 906 907 908 909 910 911
/* The parisc documentation describes only the general interpretation of
   the conditions, without describing their exact implementation.  The
   interpretations do not stand up well when considering ADD,C and SUB,B.
   However, considering the Addition, Subtraction and Logical conditions
   as a whole it would appear that these relations are similar to what
   a traditional NZCV set of flags would produce.  */

912 913
static DisasCond do_cond(unsigned cf, TCGv_reg res,
                         TCGv_reg cb_msb, TCGv_reg sv)
914 915
{
    DisasCond cond;
916
    TCGv_reg tmp;
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935

    switch (cf >> 1) {
    case 0: /* Never / TR */
        cond = cond_make_f();
        break;
    case 1: /* = / <>        (Z / !Z) */
        cond = cond_make_0(TCG_COND_EQ, res);
        break;
    case 2: /* < / >=        (N / !N) */
        cond = cond_make_0(TCG_COND_LT, res);
        break;
    case 3: /* <= / >        (N | Z / !N & !Z) */
        cond = cond_make_0(TCG_COND_LE, res);
        break;
    case 4: /* NUV / UV      (!C / C) */
        cond = cond_make_0(TCG_COND_EQ, cb_msb);
        break;
    case 5: /* ZNV / VNZ     (!C | Z / C & !Z) */
        tmp = tcg_temp_new();
936 937
        tcg_gen_neg_reg(tmp, cb_msb);
        tcg_gen_and_reg(tmp, tmp, res);
938 939 940 941 942 943 944 945
        cond = cond_make_0(TCG_COND_EQ, tmp);
        tcg_temp_free(tmp);
        break;
    case 6: /* SV / NSV      (V / !V) */
        cond = cond_make_0(TCG_COND_LT, sv);
        break;
    case 7: /* OD / EV */
        tmp = tcg_temp_new();
946
        tcg_gen_andi_reg(tmp, res, 1);
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
        cond = cond_make_0(TCG_COND_NE, tmp);
        tcg_temp_free(tmp);
        break;
    default:
        g_assert_not_reached();
    }
    if (cf & 1) {
        cond.c = tcg_invert_cond(cond.c);
    }

    return cond;
}

/* Similar, but for the special case of subtraction without borrow, we
   can use the inputs directly.  This can allow other computation to be
   deleted as unused.  */

964 965
static DisasCond do_sub_cond(unsigned cf, TCGv_reg res,
                             TCGv_reg in1, TCGv_reg in2, TCGv_reg sv)
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
{
    DisasCond cond;

    switch (cf >> 1) {
    case 1: /* = / <> */
        cond = cond_make(TCG_COND_EQ, in1, in2);
        break;
    case 2: /* < / >= */
        cond = cond_make(TCG_COND_LT, in1, in2);
        break;
    case 3: /* <= / > */
        cond = cond_make(TCG_COND_LE, in1, in2);
        break;
    case 4: /* << / >>= */
        cond = cond_make(TCG_COND_LTU, in1, in2);
        break;
    case 5: /* <<= / >> */
        cond = cond_make(TCG_COND_LEU, in1, in2);
        break;
    default:
        return do_cond(cf, res, sv, sv);
    }
    if (cf & 1) {
        cond.c = tcg_invert_cond(cond.c);
    }

    return cond;
}

/* Similar, but for logicals, where the carry and overflow bits are not
   computed, and use of them is undefined.  */

998
static DisasCond do_log_cond(unsigned cf, TCGv_reg res)
999 1000 1001 1002 1003 1004 1005 1006 1007
{
    switch (cf >> 1) {
    case 4: case 5: case 6:
        cf &= 1;
        break;
    }
    return do_cond(cf, res, res, res);
}

1008 1009
/* Similar, but for shift/extract/deposit conditions.  */

1010
static DisasCond do_sed_cond(unsigned orig, TCGv_reg res)
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
{
    unsigned c, f;

    /* Convert the compressed condition codes to standard.
       0-2 are the same as logicals (nv,<,<=), while 3 is OD.
       4-7 are the reverse of 0-3.  */
    c = orig & 3;
    if (c == 3) {
        c = 7;
    }
    f = (orig & 4) / 4;

    return do_log_cond(c * 2 + f, res);
}

1026 1027
/* Similar, but for unit conditions.  */

1028 1029
static DisasCond do_unit_cond(unsigned cf, TCGv_reg res,
                              TCGv_reg in1, TCGv_reg in2)
1030 1031
{
    DisasCond cond;
1032
    TCGv_reg tmp, cb = NULL;
1033 1034 1035 1036 1037 1038 1039 1040

    if (cf & 8) {
        /* Since we want to test lots of carry-out bits all at once, do not
         * do our normal thing and compute carry-in of bit B+1 since that
         * leaves us with carry bits spread across two words.
         */
        cb = tcg_temp_new();
        tmp = tcg_temp_new();
1041 1042 1043 1044
        tcg_gen_or_reg(cb, in1, in2);
        tcg_gen_and_reg(tmp, in1, in2);
        tcg_gen_andc_reg(cb, cb, res);
        tcg_gen_or_reg(cb, cb, tmp);
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
        tcg_temp_free(tmp);
    }

    switch (cf >> 1) {
    case 0: /* never / TR */
    case 1: /* undefined */
    case 5: /* undefined */
        cond = cond_make_f();
        break;

    case 2: /* SBZ / NBZ */
        /* See hasless(v,1) from
         * https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
         */
        tmp = tcg_temp_new();
1060 1061 1062
        tcg_gen_subi_reg(tmp, res, 0x01010101u);
        tcg_gen_andc_reg(tmp, tmp, res);
        tcg_gen_andi_reg(tmp, tmp, 0x80808080u);
1063 1064 1065 1066 1067 1068
        cond = cond_make_0(TCG_COND_NE, tmp);
        tcg_temp_free(tmp);
        break;

    case 3: /* SHZ / NHZ */
        tmp = tcg_temp_new();
1069 1070 1071
        tcg_gen_subi_reg(tmp, res, 0x00010001u);
        tcg_gen_andc_reg(tmp, tmp, res);
        tcg_gen_andi_reg(tmp, tmp, 0x80008000u);
1072 1073 1074 1075 1076
        cond = cond_make_0(TCG_COND_NE, tmp);
        tcg_temp_free(tmp);
        break;

    case 4: /* SDC / NDC */
1077
        tcg_gen_andi_reg(cb, cb, 0x88888888u);
1078 1079 1080 1081
        cond = cond_make_0(TCG_COND_NE, cb);
        break;

    case 6: /* SBC / NBC */
1082
        tcg_gen_andi_reg(cb, cb, 0x80808080u);
1083 1084 1085 1086
        cond = cond_make_0(TCG_COND_NE, cb);
        break;

    case 7: /* SHC / NHC */
1087
        tcg_gen_andi_reg(cb, cb, 0x80008000u);
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
        cond = cond_make_0(TCG_COND_NE, cb);
        break;

    default:
        g_assert_not_reached();
    }
    if (cf & 8) {
        tcg_temp_free(cb);
    }
    if (cf & 1) {
        cond.c = tcg_invert_cond(cond.c);
    }

    return cond;
}

/* Compute signed overflow for addition.  */
1105 1106
static TCGv_reg do_add_sv(DisasContext *ctx, TCGv_reg res,
                          TCGv_reg in1, TCGv_reg in2)
1107
{
1108 1109
    TCGv_reg sv = get_temp(ctx);
    TCGv_reg tmp = tcg_temp_new();
1110

1111 1112 1113
    tcg_gen_xor_reg(sv, res, in1);
    tcg_gen_xor_reg(tmp, in1, in2);
    tcg_gen_andc_reg(sv, sv, tmp);
1114 1115 1116 1117 1118 1119
    tcg_temp_free(tmp);

    return sv;
}

/* Compute signed overflow for subtraction.  */
1120 1121
static TCGv_reg do_sub_sv(DisasContext *ctx, TCGv_reg res,
                          TCGv_reg in1, TCGv_reg in2)
1122
{
1123 1124
    TCGv_reg sv = get_temp(ctx);
    TCGv_reg tmp = tcg_temp_new();
1125

1126 1127 1128
    tcg_gen_xor_reg(sv, res, in1);
    tcg_gen_xor_reg(tmp, in1, in2);
    tcg_gen_and_reg(sv, sv, tmp);
1129 1130 1131 1132 1133
    tcg_temp_free(tmp);

    return sv;
}

1134 1135 1136
static DisasJumpType do_add(DisasContext *ctx, unsigned rt, TCGv_reg in1,
                            TCGv_reg in2, unsigned shift, bool is_l,
                            bool is_tsv, bool is_tc, bool is_c, unsigned cf)
1137
{
1138
    TCGv_reg dest, cb, cb_msb, sv, tmp;
1139 1140 1141 1142
    unsigned c = cf >> 1;
    DisasCond cond;

    dest = tcg_temp_new();
1143 1144
    cb = NULL;
    cb_msb = NULL;
1145 1146 1147

    if (shift) {
        tmp = get_temp(ctx);
1148
        tcg_gen_shli_reg(tmp, in1, shift);
1149 1150 1151 1152
        in1 = tmp;
    }

    if (!is_l || c == 4 || c == 5) {
1153
        TCGv_reg zero = tcg_const_reg(0);
1154
        cb_msb = get_temp(ctx);
1155
        tcg_gen_add2_reg(dest, cb_msb, in1, zero, in2, zero);
1156
        if (is_c) {
1157
            tcg_gen_add2_reg(dest, cb_msb, dest, cb_msb, cpu_psw_cb_msb, zero);
1158 1159 1160 1161
        }
        tcg_temp_free(zero);
        if (!is_l) {
            cb = get_temp(ctx);
1162 1163
            tcg_gen_xor_reg(cb, in1, in2);
            tcg_gen_xor_reg(cb, cb, dest);
1164 1165
        }
    } else {
1166
        tcg_gen_add_reg(dest, in1, in2);
1167
        if (is_c) {
1168
            tcg_gen_add_reg(dest, dest, cpu_psw_cb_msb);
1169 1170 1171 1172
        }
    }

    /* Compute signed overflow if required.  */
1173
    sv = NULL;
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
    if (is_tsv || c == 6) {
        sv = do_add_sv(ctx, dest, in1, in2);
        if (is_tsv) {
            /* ??? Need to include overflow from shift.  */
            gen_helper_tsv(cpu_env, sv);
        }
    }

    /* Emit any conditional trap before any writeback.  */
    cond = do_cond(cf, dest, cb_msb, sv);
    if (is_tc) {
        cond_prep(&cond);
        tmp = tcg_temp_new();
1187
        tcg_gen_setcond_reg(cond.c, tmp, cond.a0, cond.a1);
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
        gen_helper_tcond(cpu_env, tmp);
        tcg_temp_free(tmp);
    }

    /* Write back the result.  */
    if (!is_l) {
        save_or_nullify(ctx, cpu_psw_cb, cb);
        save_or_nullify(ctx, cpu_psw_cb_msb, cb_msb);
    }
    save_gpr(ctx, rt, dest);
    tcg_temp_free(dest);

    /* Install the new nullification.  */
    cond_free(&ctx->null_cond);
    ctx->null_cond = cond;
1203
    return DISAS_NEXT;
1204 1205
}

1206 1207 1208
static DisasJumpType do_sub(DisasContext *ctx, unsigned rt, TCGv_reg in1,
                            TCGv_reg in2, bool is_tsv, bool is_b,
                            bool is_tc, unsigned cf)
1209
{
1210
    TCGv_reg dest, sv, cb, cb_msb, zero, tmp;
1211 1212 1213 1214 1215 1216 1217
    unsigned c = cf >> 1;
    DisasCond cond;

    dest = tcg_temp_new();
    cb = tcg_temp_new();
    cb_msb = tcg_temp_new();

1218
    zero = tcg_const_reg(0);
1219 1220
    if (is_b) {
        /* DEST,C = IN1 + ~IN2 + C.  */
1221 1222 1223 1224 1225
        tcg_gen_not_reg(cb, in2);
        tcg_gen_add2_reg(dest, cb_msb, in1, zero, cpu_psw_cb_msb, zero);
        tcg_gen_add2_reg(dest, cb_msb, dest, cb_msb, cb, zero);
        tcg_gen_xor_reg(cb, cb, in1);
        tcg_gen_xor_reg(cb, cb, dest);
1226 1227 1228
    } else {
        /* DEST,C = IN1 + ~IN2 + 1.  We can produce the same result in fewer
           operations by seeding the high word with 1 and subtracting.  */
1229 1230 1231 1232
        tcg_gen_movi_reg(cb_msb, 1);
        tcg_gen_sub2_reg(dest, cb_msb, in1, cb_msb, in2, zero);
        tcg_gen_eqv_reg(cb, in1, in2);
        tcg_gen_xor_reg(cb, cb, dest);
1233 1234 1235 1236
    }
    tcg_temp_free(zero);

    /* Compute signed overflow if required.  */
1237
    sv = NULL;
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
    if (is_tsv || c == 6) {
        sv = do_sub_sv(ctx, dest, in1, in2);
        if (is_tsv) {
            gen_helper_tsv(cpu_env, sv);
        }
    }

    /* Compute the condition.  We cannot use the special case for borrow.  */
    if (!is_b) {
        cond = do_sub_cond(cf, dest, in1, in2, sv);
    } else {
        cond = do_cond(cf, dest, cb_msb, sv);
    }

    /* Emit any conditional trap before any writeback.  */
    if (is_tc) {
        cond_prep(&cond);
        tmp = tcg_temp_new();
1256
        tcg_gen_setcond_reg(cond.c, tmp, cond.a0, cond.a1);
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
        gen_helper_tcond(cpu_env, tmp);
        tcg_temp_free(tmp);
    }

    /* Write back the result.  */
    save_or_nullify(ctx, cpu_psw_cb, cb);
    save_or_nullify(ctx, cpu_psw_cb_msb, cb_msb);
    save_gpr(ctx, rt, dest);
    tcg_temp_free(dest);

    /* Install the new nullification.  */
    cond_free(&ctx->null_cond);
    ctx->null_cond = cond;
1270
    return DISAS_NEXT;
1271 1272
}

1273 1274
static DisasJumpType do_cmpclr(DisasContext *ctx, unsigned rt, TCGv_reg in1,
                               TCGv_reg in2, unsigned cf)
1275
{
1276
    TCGv_reg dest, sv;
1277 1278 1279
    DisasCond cond;

    dest = tcg_temp_new();
1280
    tcg_gen_sub_reg(dest, in1, in2);
1281 1282

    /* Compute signed overflow if required.  */
1283
    sv = NULL;
1284 1285 1286 1287 1288 1289 1290 1291
    if ((cf >> 1) == 6) {
        sv = do_sub_sv(ctx, dest, in1, in2);
    }

    /* Form the condition for the compare.  */
    cond = do_sub_cond(cf, dest, in1, in2, sv);

    /* Clear.  */
1292
    tcg_gen_movi_reg(dest, 0);
1293 1294 1295 1296 1297 1298
    save_gpr(ctx, rt, dest);
    tcg_temp_free(dest);

    /* Install the new nullification.  */
    cond_free(&ctx->null_cond);
    ctx->null_cond = cond;
1299
    return DISAS_NEXT;
1300 1301
}

1302 1303 1304
static DisasJumpType do_log(DisasContext *ctx, unsigned rt, TCGv_reg in1,
                            TCGv_reg in2, unsigned cf,
                            void (*fn)(TCGv_reg, TCGv_reg, TCGv_reg))
1305
{
1306
    TCGv_reg dest = dest_gpr(ctx, rt);
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316

    /* Perform the operation, and writeback.  */
    fn(dest, in1, in2);
    save_gpr(ctx, rt, dest);

    /* Install the new nullification.  */
    cond_free(&ctx->null_cond);
    if (cf) {
        ctx->null_cond = do_log_cond(cf, dest);
    }
1317
    return DISAS_NEXT;
1318 1319
}

1320 1321 1322
static DisasJumpType do_unit(DisasContext *ctx, unsigned rt, TCGv_reg in1,
                             TCGv_reg in2, unsigned cf, bool is_tc,
                             void (*fn)(TCGv_reg, TCGv_reg, TCGv_reg))
1323
{
1324
    TCGv_reg dest;
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
    DisasCond cond;

    if (cf == 0) {
        dest = dest_gpr(ctx, rt);
        fn(dest, in1, in2);
        save_gpr(ctx, rt, dest);
        cond_free(&ctx->null_cond);
    } else {
        dest = tcg_temp_new();
        fn(dest, in1, in2);

        cond = do_unit_cond(cf, dest, in1, in2);

        if (is_tc) {
1339
            TCGv_reg tmp = tcg_temp_new();
1340
            cond_prep(&cond);
1341
            tcg_gen_setcond_reg(cond.c, tmp, cond.a0, cond.a1);
1342 1343 1344 1345 1346 1347 1348 1349
            gen_helper_tcond(cpu_env, tmp);
            tcg_temp_free(tmp);
        }
        save_gpr(ctx, rt, dest);

        cond_free(&ctx->null_cond);
        ctx->null_cond = cond;
    }
1350
    return DISAS_NEXT;
1351 1352
}

1353
#ifndef CONFIG_USER_ONLY
1354 1355 1356 1357
/* The "normal" usage is SP >= 0, wherein SP == 0 selects the space
   from the top 2 bits of the base register.  There are a few system
   instructions that have a 3-bit space specifier, for which SR0 is
   not special.  To handle this, pass ~SP.  */
1358 1359 1360 1361 1362 1363 1364
static TCGv_i64 space_select(DisasContext *ctx, int sp, TCGv_reg base)
{
    TCGv_ptr ptr;
    TCGv_reg tmp;
    TCGv_i64 spc;

    if (sp != 0) {
1365 1366 1367 1368 1369 1370
        if (sp < 0) {
            sp = ~sp;
        }
        spc = get_temp_tl(ctx);
        load_spr(ctx, spc, sp);
        return spc;
1371
    }
1372 1373 1374
    if (ctx->tb_flags & TB_FLAG_SR_SAME) {
        return cpu_srH;
    }
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417

    ptr = tcg_temp_new_ptr();
    tmp = tcg_temp_new();
    spc = get_temp_tl(ctx);

    tcg_gen_shri_reg(tmp, base, TARGET_REGISTER_BITS - 5);
    tcg_gen_andi_reg(tmp, tmp, 030);
    tcg_gen_trunc_reg_ptr(ptr, tmp);
    tcg_temp_free(tmp);

    tcg_gen_add_ptr(ptr, ptr, cpu_env);
    tcg_gen_ld_i64(spc, ptr, offsetof(CPUHPPAState, sr[4]));
    tcg_temp_free_ptr(ptr);

    return spc;
}
#endif

static void form_gva(DisasContext *ctx, TCGv_tl *pgva, TCGv_reg *pofs,
                     unsigned rb, unsigned rx, int scale, target_sreg disp,
                     unsigned sp, int modify, bool is_phys)
{
    TCGv_reg base = load_gpr(ctx, rb);
    TCGv_reg ofs;

    /* Note that RX is mutually exclusive with DISP.  */
    if (rx) {
        ofs = get_temp(ctx);
        tcg_gen_shli_reg(ofs, cpu_gr[rx], scale);
        tcg_gen_add_reg(ofs, ofs, base);
    } else if (disp || modify) {
        ofs = get_temp(ctx);
        tcg_gen_addi_reg(ofs, base, disp);
    } else {
        ofs = base;
    }

    *pofs = ofs;
#ifdef CONFIG_USER_ONLY
    *pgva = (modify <= 0 ? ofs : base);
#else
    TCGv_tl addr = get_temp_tl(ctx);
    tcg_gen_extu_reg_tl(addr, modify <= 0 ? ofs : base);
1418
    if (ctx->tb_flags & PSW_W) {
1419 1420 1421 1422 1423 1424 1425 1426 1427
        tcg_gen_andi_tl(addr, addr, 0x3fffffffffffffffull);
    }
    if (!is_phys) {
        tcg_gen_or_tl(addr, addr, space_select(ctx, sp, base));
    }
    *pgva = addr;
#endif
}

1428 1429 1430 1431 1432 1433
/* Emit a memory load.  The modify parameter should be
 * < 0 for pre-modify,
 * > 0 for post-modify,
 * = 0 for no base register update.
 */
static void do_load_32(DisasContext *ctx, TCGv_i32 dest, unsigned rb,
1434
                       unsigned rx, int scale, target_sreg disp,
1435
                       unsigned sp, int modify, TCGMemOp mop)
1436
{
1437 1438
    TCGv_reg ofs;
    TCGv_tl addr;
1439 1440 1441 1442

    /* Caller uses nullify_over/nullify_end.  */
    assert(ctx->null_cond.c == TCG_COND_NEVER);

1443 1444 1445 1446 1447
    form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
             ctx->mmu_idx == MMU_PHYS_IDX);
    tcg_gen_qemu_ld_reg(dest, addr, ctx->mmu_idx, mop);
    if (modify) {
        save_gpr(ctx, rb, ofs);
1448 1449 1450 1451
    }
}

static void do_load_64(DisasContext *ctx, TCGv_i64 dest, unsigned rb,
1452
                       unsigned rx, int scale, target_sreg disp,
1453
                       unsigned sp, int modify, TCGMemOp mop)
1454
{
1455 1456
    TCGv_reg ofs;
    TCGv_tl addr;
1457 1458 1459 1460

    /* Caller uses nullify_over/nullify_end.  */
    assert(ctx->null_cond.c == TCG_COND_NEVER);

1461 1462 1463 1464 1465
    form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
             ctx->mmu_idx == MMU_PHYS_IDX);
    tcg_gen_qemu_ld_i64(dest, addr, ctx->mmu_idx, mop);
    if (modify) {
        save_gpr(ctx, rb, ofs);
1466 1467 1468 1469
    }
}

static void do_store_32(DisasContext *ctx, TCGv_i32 src, unsigned rb,
1470
                        unsigned rx, int scale, target_sreg disp,
1471
                        unsigned sp, int modify, TCGMemOp mop)
1472
{
1473 1474
    TCGv_reg ofs;
    TCGv_tl addr;
1475 1476 1477 1478

    /* Caller uses nullify_over/nullify_end.  */
    assert(ctx->null_cond.c == TCG_COND_NEVER);

1479 1480 1481 1482 1483
    form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
             ctx->mmu_idx == MMU_PHYS_IDX);
    tcg_gen_qemu_st_i32(src, addr, ctx->mmu_idx, mop);
    if (modify) {
        save_gpr(ctx, rb, ofs);
1484 1485 1486 1487
    }
}

static void do_store_64(DisasContext *ctx, TCGv_i64 src, unsigned rb,
1488
                        unsigned rx, int scale, target_sreg disp,
1489
                        unsigned sp, int modify, TCGMemOp mop)
1490
{
1491 1492
    TCGv_reg ofs;
    TCGv_tl addr;
1493 1494 1495 1496

    /* Caller uses nullify_over/nullify_end.  */
    assert(ctx->null_cond.c == TCG_COND_NEVER);

1497 1498 1499 1500 1501
    form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
             ctx->mmu_idx == MMU_PHYS_IDX);
    tcg_gen_qemu_st_i64(src, addr, ctx->mmu_idx, mop);
    if (modify) {
        save_gpr(ctx, rb, ofs);
1502 1503 1504
    }
}

1505 1506 1507
#if TARGET_REGISTER_BITS == 64
#define do_load_reg   do_load_64
#define do_store_reg  do_store_64
1508
#else
1509 1510
#define do_load_reg   do_load_32
#define do_store_reg  do_store_32
1511 1512
#endif

1513
static DisasJumpType do_load(DisasContext *ctx, unsigned rt, unsigned rb,
1514
                             unsigned rx, int scale, target_sreg disp,
1515
                             unsigned sp, int modify, TCGMemOp mop)
1516
{
1517
    TCGv_reg dest;
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527

    nullify_over(ctx);

    if (modify == 0) {
        /* No base register update.  */
        dest = dest_gpr(ctx, rt);
    } else {
        /* Make sure if RT == RB, we see the result of the load.  */
        dest = get_temp(ctx);
    }
1528
    do_load_reg(ctx, dest, rb, rx, scale, disp, sp, modify, mop);
1529 1530
    save_gpr(ctx, rt, dest);

1531
    return nullify_end(ctx, DISAS_NEXT);
1532 1533
}

1534
static DisasJumpType do_floadw(DisasContext *ctx, unsigned rt, unsigned rb,
1535
                               unsigned rx, int scale, target_sreg disp,
1536
                               unsigned sp, int modify)
1537 1538 1539 1540 1541 1542
{
    TCGv_i32 tmp;

    nullify_over(ctx);

    tmp = tcg_temp_new_i32();
1543
    do_load_32(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEUL);
1544 1545 1546 1547 1548 1549 1550
    save_frw_i32(rt, tmp);
    tcg_temp_free_i32(tmp);

    if (rt == 0) {
        gen_helper_loaded_fr0(cpu_env);
    }

1551
    return nullify_end(ctx, DISAS_NEXT);
1552 1553
}

1554
static DisasJumpType do_floadd(DisasContext *ctx, unsigned rt, unsigned rb,
1555
                               unsigned rx, int scale, target_sreg disp,
1556
                               unsigned sp, int modify)
1557 1558 1559 1560 1561 1562
{
    TCGv_i64 tmp;

    nullify_over(ctx);

    tmp = tcg_temp_new_i64();
1563
    do_load_64(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEQ);
1564 1565 1566 1567 1568 1569 1570
    save_frd(rt, tmp);
    tcg_temp_free_i64(tmp);

    if (rt == 0) {
        gen_helper_loaded_fr0(cpu_env);
    }

1571
    return nullify_end(ctx, DISAS_NEXT);
1572 1573
}

1574
static DisasJumpType do_store(DisasContext *ctx, unsigned rt, unsigned rb,
1575 1576
                              target_sreg disp, unsigned sp,
                              int modify, TCGMemOp mop)
1577 1578
{
    nullify_over(ctx);
1579
    do_store_reg(ctx, load_gpr(ctx, rt), rb, 0, 0, disp, sp, modify, mop);
1580
    return nullify_end(ctx, DISAS_NEXT);
1581 1582
}

1583
static DisasJumpType do_fstorew(DisasContext *ctx, unsigned rt, unsigned rb,
1584
                                unsigned rx, int scale, target_sreg disp,
1585
                                unsigned sp, int modify)
1586 1587 1588 1589 1590 1591
{
    TCGv_i32 tmp;

    nullify_over(ctx);

    tmp = load_frw_i32(rt);
1592
    do_store_32(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEUL);
1593 1594
    tcg_temp_free_i32(tmp);

1595
    return nullify_end(ctx, DISAS_NEXT);
1596 1597
}

1598
static DisasJumpType do_fstored(DisasContext *ctx, unsigned rt, unsigned rb,
1599
                                unsigned rx, int scale, target_sreg disp,
1600
                                unsigned sp, int modify)
1601 1602 1603 1604 1605 1606
{
    TCGv_i64 tmp;

    nullify_over(ctx);

    tmp = load_frd(rt);
1607
    do_store_64(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEQ);
1608 1609
    tcg_temp_free_i64(tmp);

1610
    return nullify_end(ctx, DISAS_NEXT);
1611 1612
}

1613 1614
static DisasJumpType do_fop_wew(DisasContext *ctx, unsigned rt, unsigned ra,
                                void (*func)(TCGv_i32, TCGv_env, TCGv_i32))
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
{
    TCGv_i32 tmp;

    nullify_over(ctx);
    tmp = load_frw0_i32(ra);

    func(tmp, cpu_env, tmp);

    save_frw_i32(rt, tmp);
    tcg_temp_free_i32(tmp);
1625
    return nullify_end(ctx, DISAS_NEXT);
1626 1627
}

1628 1629
static DisasJumpType do_fop_wed(DisasContext *ctx, unsigned rt, unsigned ra,
                                void (*func)(TCGv_i32, TCGv_env, TCGv_i64))
1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
{
    TCGv_i32 dst;
    TCGv_i64 src;

    nullify_over(ctx);
    src = load_frd(ra);
    dst = tcg_temp_new_i32();

    func(dst, cpu_env, src);

    tcg_temp_free_i64(src);
    save_frw_i32(rt, dst);
    tcg_temp_free_i32(dst);
1643
    return nullify_end(ctx, DISAS_NEXT);
1644 1645
}

1646 1647
static DisasJumpType do_fop_ded(DisasContext *ctx, unsigned rt, unsigned ra,
                                void (*func)(TCGv_i64, TCGv_env, TCGv_i64))
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
{
    TCGv_i64 tmp;

    nullify_over(ctx);
    tmp = load_frd0(ra);

    func(tmp, cpu_env, tmp);

    save_frd(rt, tmp);
    tcg_temp_free_i64(tmp);
1658
    return nullify_end(ctx, DISAS_NEXT);
1659 1660
}

1661 1662
static DisasJumpType do_fop_dew(DisasContext *ctx, unsigned rt, unsigned ra,
                                void (*func)(TCGv_i64, TCGv_env, TCGv_i32))
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
{
    TCGv_i32 src;
    TCGv_i64 dst;

    nullify_over(ctx);
    src = load_frw0_i32(ra);
    dst = tcg_temp_new_i64();

    func(dst, cpu_env, src);

    tcg_temp_free_i32(src);
    save_frd(rt, dst);
    tcg_temp_free_i64(dst);
1676
    return nullify_end(ctx, DISAS_NEXT);
1677 1678
}

1679 1680 1681 1682
static DisasJumpType do_fop_weww(DisasContext *ctx, unsigned rt,
                                 unsigned ra, unsigned rb,
                                 void (*func)(TCGv_i32, TCGv_env,
                                              TCGv_i32, TCGv_i32))
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
{
    TCGv_i32 a, b;

    nullify_over(ctx);
    a = load_frw0_i32(ra);
    b = load_frw0_i32(rb);

    func(a, cpu_env, a, b);

    tcg_temp_free_i32(b);
    save_frw_i32(rt, a);
    tcg_temp_free_i32(a);
1695
    return nullify_end(ctx, DISAS_NEXT);
1696 1697
}

1698 1699 1700 1701
static DisasJumpType do_fop_dedd(DisasContext *ctx, unsigned rt,
                                 unsigned ra, unsigned rb,
                                 void (*func)(TCGv_i64, TCGv_env,
                                              TCGv_i64, TCGv_i64))
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713
{
    TCGv_i64 a, b;

    nullify_over(ctx);
    a = load_frd0(ra);
    b = load_frd0(rb);

    func(a, cpu_env, a, b);

    tcg_temp_free_i64(b);
    save_frd(rt, a);
    tcg_temp_free_i64(a);
1714
    return nullify_end(ctx, DISAS_NEXT);
1715 1716
}

1717 1718
/* Emit an unconditional branch to a direct target, which may or may not
   have already had nullification handled.  */
1719
static DisasJumpType do_dbranch(DisasContext *ctx, target_ureg dest,
1720
                                unsigned link, bool is_n)
1721 1722 1723 1724 1725 1726 1727 1728 1729
{
    if (ctx->null_cond.c == TCG_COND_NEVER && ctx->null_lab == NULL) {
        if (link != 0) {
            copy_iaoq_entry(cpu_gr[link], ctx->iaoq_n, ctx->iaoq_n_var);
        }
        ctx->iaoq_n = dest;
        if (is_n) {
            ctx->null_cond.c = TCG_COND_ALWAYS;
        }
1730
        return DISAS_NEXT;
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
    } else {
        nullify_over(ctx);

        if (link != 0) {
            copy_iaoq_entry(cpu_gr[link], ctx->iaoq_n, ctx->iaoq_n_var);
        }

        if (is_n && use_nullify_skip(ctx)) {
            nullify_set(ctx, 0);
            gen_goto_tb(ctx, 0, dest, dest + 4);
        } else {
            nullify_set(ctx, is_n);
            gen_goto_tb(ctx, 0, ctx->iaoq_b, dest);
        }

1746
        nullify_end(ctx, DISAS_NEXT);
1747 1748 1749

        nullify_set(ctx, 0);
        gen_goto_tb(ctx, 1, ctx->iaoq_b, ctx->iaoq_n);
1750
        return DISAS_NORETURN;
1751 1752 1753 1754 1755
    }
}

/* Emit a conditional branch to a direct target.  If the branch itself
   is nullified, we should have already used nullify_over.  */
1756
static DisasJumpType do_cbranch(DisasContext *ctx, target_sreg disp, bool is_n,
1757
                                DisasCond *cond)
1758
{
1759
    target_ureg dest = iaoq_dest(ctx, disp);
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
    TCGLabel *taken = NULL;
    TCGCond c = cond->c;
    bool n;

    assert(ctx->null_cond.c == TCG_COND_NEVER);

    /* Handle TRUE and NEVER as direct branches.  */
    if (c == TCG_COND_ALWAYS) {
        return do_dbranch(ctx, dest, 0, is_n && disp >= 0);
    }
    if (c == TCG_COND_NEVER) {
        return do_dbranch(ctx, ctx->iaoq_n, 0, is_n && disp < 0);
    }

    taken = gen_new_label();
    cond_prep(cond);
1776
    tcg_gen_brcond_reg(c, cond->a0, cond->a1, taken);
1777 1778 1779 1780 1781 1782
    cond_free(cond);

    /* Not taken: Condition not satisfied; nullify on backward branches. */
    n = is_n && disp < 0;
    if (n && use_nullify_skip(ctx)) {
        nullify_set(ctx, 0);
R
Richard Henderson 已提交
1783
        gen_goto_tb(ctx, 0, ctx->iaoq_n, ctx->iaoq_n + 4);
1784 1785 1786 1787 1788 1789
    } else {
        if (!n && ctx->null_lab) {
            gen_set_label(ctx->null_lab);
            ctx->null_lab = NULL;
        }
        nullify_set(ctx, n);
R
Richard Henderson 已提交
1790 1791 1792 1793 1794
        if (ctx->iaoq_n == -1) {
            /* The temporary iaoq_n_var died at the branch above.
               Regenerate it here instead of saving it.  */
            tcg_gen_addi_reg(ctx->iaoq_n_var, cpu_iaoq_b, 4);
        }
R
Richard Henderson 已提交
1795
        gen_goto_tb(ctx, 0, ctx->iaoq_b, ctx->iaoq_n);
1796 1797 1798 1799 1800 1801 1802 1803
    }

    gen_set_label(taken);

    /* Taken: Condition satisfied; nullify on forward branches.  */
    n = is_n && disp >= 0;
    if (n && use_nullify_skip(ctx)) {
        nullify_set(ctx, 0);
R
Richard Henderson 已提交
1804
        gen_goto_tb(ctx, 1, dest, dest + 4);
1805 1806
    } else {
        nullify_set(ctx, n);
R
Richard Henderson 已提交
1807
        gen_goto_tb(ctx, 1, ctx->iaoq_b, dest);
1808 1809 1810 1811 1812 1813
    }

    /* Not taken: the branch itself was nullified.  */
    if (ctx->null_lab) {
        gen_set_label(ctx->null_lab);
        ctx->null_lab = NULL;
1814
        return DISAS_IAQ_N_STALE;
1815
    } else {
1816
        return DISAS_NORETURN;
1817 1818 1819 1820 1821
    }
}

/* Emit an unconditional branch to an indirect target.  This handles
   nullification of the branch itself.  */
1822
static DisasJumpType do_ibranch(DisasContext *ctx, TCGv_reg dest,
1823
                                unsigned link, bool is_n)
1824
{
1825
    TCGv_reg a0, a1, next, tmp;
1826 1827 1828 1829 1830 1831 1832 1833 1834
    TCGCond c;

    assert(ctx->null_lab == NULL);

    if (ctx->null_cond.c == TCG_COND_NEVER) {
        if (link != 0) {
            copy_iaoq_entry(cpu_gr[link], ctx->iaoq_n, ctx->iaoq_n_var);
        }
        next = get_temp(ctx);
1835
        tcg_gen_mov_reg(next, dest);
1836
        if (is_n) {
R
Richard Henderson 已提交
1837 1838 1839 1840 1841 1842
            if (use_nullify_skip(ctx)) {
                tcg_gen_mov_reg(cpu_iaoq_f, next);
                tcg_gen_addi_reg(cpu_iaoq_b, next, 4);
                nullify_set(ctx, 0);
                return DISAS_IAQ_N_UPDATED;
            }
1843 1844
            ctx->null_cond.c = TCG_COND_ALWAYS;
        }
R
Richard Henderson 已提交
1845 1846
        ctx->iaoq_n = -1;
        ctx->iaoq_n_var = next;
1847 1848 1849
    } else if (is_n && use_nullify_skip(ctx)) {
        /* The (conditional) branch, B, nullifies the next insn, N,
           and we're allowed to skip execution N (no single-step or
1850
           tracepoint in effect).  Since the goto_ptr that we must use
1851 1852 1853 1854 1855 1856 1857 1858 1859
           for the indirect branch consumes no special resources, we
           can (conditionally) skip B and continue execution.  */
        /* The use_nullify_skip test implies we have a known control path.  */
        tcg_debug_assert(ctx->iaoq_b != -1);
        tcg_debug_assert(ctx->iaoq_n != -1);

        /* We do have to handle the non-local temporary, DEST, before
           branching.  Since IOAQ_F is not really live at this point, we
           can simply store DEST optimistically.  Similarly with IAOQ_B.  */
1860 1861
        tcg_gen_mov_reg(cpu_iaoq_f, dest);
        tcg_gen_addi_reg(cpu_iaoq_b, dest, 4);
1862 1863 1864

        nullify_over(ctx);
        if (link != 0) {
1865
            tcg_gen_movi_reg(cpu_gr[link], ctx->iaoq_n);
1866
        }
1867
        tcg_gen_lookup_and_goto_ptr();
1868
        return nullify_end(ctx, DISAS_NEXT);
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
    } else {
        cond_prep(&ctx->null_cond);
        c = ctx->null_cond.c;
        a0 = ctx->null_cond.a0;
        a1 = ctx->null_cond.a1;

        tmp = tcg_temp_new();
        next = get_temp(ctx);

        copy_iaoq_entry(tmp, ctx->iaoq_n, ctx->iaoq_n_var);
1879
        tcg_gen_movcond_reg(c, next, a0, a1, tmp, dest);
1880 1881 1882 1883
        ctx->iaoq_n = -1;
        ctx->iaoq_n_var = next;

        if (link != 0) {
1884
            tcg_gen_movcond_reg(c, cpu_gr[link], a0, a1, cpu_gr[link], tmp);
1885 1886 1887 1888 1889 1890
        }

        if (is_n) {
            /* The branch nullifies the next insn, which means the state of N
               after the branch is the inverse of the state of N that applied
               to the branch.  */
1891
            tcg_gen_setcond_reg(tcg_invert_cond(c), cpu_psw_n, a0, a1);
1892 1893 1894 1895 1896 1897 1898 1899
            cond_free(&ctx->null_cond);
            ctx->null_cond = cond_make_n();
            ctx->psw_n_nonzero = true;
        } else {
            cond_free(&ctx->null_cond);
        }
    }

1900
    return DISAS_NEXT;
1901 1902
}

1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936
/* Implement
 *    if (IAOQ_Front{30..31} < GR[b]{30..31})
 *      IAOQ_Next{30..31} ← GR[b]{30..31};
 *    else
 *      IAOQ_Next{30..31} ← IAOQ_Front{30..31};
 * which keeps the privilege level from being increased.
 */
static TCGv_reg do_ibranch_priv(DisasContext *ctx, TCGv_reg offset)
{
#ifdef CONFIG_USER_ONLY
    return offset;
#else
    TCGv_reg dest;
    switch (ctx->privilege) {
    case 0:
        /* Privilege 0 is maximum and is allowed to decrease.  */
        return offset;
    case 3:
        /* Privilege 3 is minimum and is never allowed increase.  */
        dest = get_temp(ctx);
        tcg_gen_ori_reg(dest, offset, 3);
        break;
    default:
        dest = tcg_temp_new();
        tcg_gen_andi_reg(dest, offset, -4);
        tcg_gen_ori_reg(dest, dest, ctx->privilege);
        tcg_gen_movcond_reg(TCG_COND_GTU, dest, dest, offset, dest, offset);
        tcg_temp_free(dest);
        break;
    }
    return dest;
#endif
}

1937
#ifdef CONFIG_USER_ONLY
1938 1939 1940 1941 1942 1943 1944
/* On Linux, page zero is normally marked execute only + gateway.
   Therefore normal read or write is supposed to fail, but specific
   offsets have kernel code mapped to raise permissions to implement
   system calls.  Handling this via an explicit check here, rather
   in than the "be disp(sr2,r0)" instruction that probably sent us
   here, is the easiest way to handle the branch delay slot on the
   aforementioned BE.  */
1945
static DisasJumpType do_page_zero(DisasContext *ctx)
1946 1947 1948 1949 1950 1951 1952 1953
{
    /* If by some means we get here with PSW[N]=1, that implies that
       the B,GATE instruction would be skipped, and we'd fault on the
       next insn within the privilaged page.  */
    switch (ctx->null_cond.c) {
    case TCG_COND_NEVER:
        break;
    case TCG_COND_ALWAYS:
1954
        tcg_gen_movi_reg(cpu_psw_n, 0);
1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
        goto do_sigill;
    default:
        /* Since this is always the first (and only) insn within the
           TB, we should know the state of PSW[N] from TB->FLAGS.  */
        g_assert_not_reached();
    }

    /* Check that we didn't arrive here via some means that allowed
       non-sequential instruction execution.  Normally the PSW[B] bit
       detects this by disallowing the B,GATE instruction to execute
       under such conditions.  */
    if (ctx->iaoq_b != ctx->iaoq_f + 4) {
        goto do_sigill;
    }

    switch (ctx->iaoq_f) {
    case 0x00: /* Null pointer call */
1972
        gen_excp_1(EXCP_IMP);
1973
        return DISAS_NORETURN;
1974 1975 1976

    case 0xb0: /* LWS */
        gen_excp_1(EXCP_SYSCALL_LWS);
1977
        return DISAS_NORETURN;
1978 1979

    case 0xe0: /* SET_THREAD_POINTER */
1980
        tcg_gen_st_reg(cpu_gr[26], cpu_env, offsetof(CPUHPPAState, cr[27]));
1981 1982
        tcg_gen_mov_reg(cpu_iaoq_f, cpu_gr[31]);
        tcg_gen_addi_reg(cpu_iaoq_b, cpu_iaoq_f, 4);
1983
        return DISAS_IAQ_N_UPDATED;
1984 1985 1986

    case 0x100: /* SYSCALL */
        gen_excp_1(EXCP_SYSCALL);
1987
        return DISAS_NORETURN;
1988 1989 1990

    default:
    do_sigill:
1991
        gen_excp_1(EXCP_ILL);
1992
        return DISAS_NORETURN;
1993 1994
    }
}
1995
#endif
1996

1997 1998
static DisasJumpType trans_nop(DisasContext *ctx, uint32_t insn,
                               const DisasInsn *di)
1999 2000
{
    cond_free(&ctx->null_cond);
2001
    return DISAS_NEXT;
2002 2003
}

2004 2005
static DisasJumpType trans_break(DisasContext *ctx, uint32_t insn,
                                 const DisasInsn *di)
2006 2007
{
    nullify_over(ctx);
2008
    return nullify_end(ctx, gen_excp_iir(ctx, EXCP_BREAK));
2009 2010
}

2011 2012
static DisasJumpType trans_sync(DisasContext *ctx, uint32_t insn,
                                const DisasInsn *di)
2013 2014 2015 2016 2017
{
    /* No point in nullifying the memory barrier.  */
    tcg_gen_mb(TCG_BAR_SC | TCG_MO_ALL);

    cond_free(&ctx->null_cond);
2018
    return DISAS_NEXT;
2019 2020
}

2021 2022
static DisasJumpType trans_mfia(DisasContext *ctx, uint32_t insn,
                                const DisasInsn *di)
2023 2024
{
    unsigned rt = extract32(insn, 0, 5);
2025 2026
    TCGv_reg tmp = dest_gpr(ctx, rt);
    tcg_gen_movi_reg(tmp, ctx->iaoq_f);
2027 2028 2029
    save_gpr(ctx, rt, tmp);

    cond_free(&ctx->null_cond);
2030
    return DISAS_NEXT;
2031 2032
}

2033 2034
static DisasJumpType trans_mfsp(DisasContext *ctx, uint32_t insn,
                                const DisasInsn *di)
2035 2036
{
    unsigned rt = extract32(insn, 0, 5);
2037 2038 2039
    unsigned rs = assemble_sr3(insn);
    TCGv_i64 t0 = tcg_temp_new_i64();
    TCGv_reg t1 = tcg_temp_new();
2040

2041 2042 2043 2044 2045 2046 2047
    load_spr(ctx, t0, rs);
    tcg_gen_shri_i64(t0, t0, 32);
    tcg_gen_trunc_i64_reg(t1, t0);

    save_gpr(ctx, rt, t1);
    tcg_temp_free(t1);
    tcg_temp_free_i64(t0);
2048 2049

    cond_free(&ctx->null_cond);
2050
    return DISAS_NEXT;
2051 2052
}

2053 2054
static DisasJumpType trans_mfctl(DisasContext *ctx, uint32_t insn,
                                 const DisasInsn *di)
2055 2056 2057
{
    unsigned rt = extract32(insn, 0, 5);
    unsigned ctl = extract32(insn, 21, 5);
2058
    TCGv_reg tmp;
2059
    DisasJumpType ret;
2060 2061

    switch (ctl) {
2062
    case CR_SAR:
2063 2064 2065 2066
#ifdef TARGET_HPPA64
        if (extract32(insn, 14, 1) == 0) {
            /* MFSAR without ,W masks low 5 bits.  */
            tmp = dest_gpr(ctx, rt);
2067
            tcg_gen_andi_reg(tmp, cpu_sar, 31);
2068
            save_gpr(ctx, rt, tmp);
2069
            goto done;
2070 2071 2072
        }
#endif
        save_gpr(ctx, rt, cpu_sar);
2073 2074 2075 2076
        goto done;
    case CR_IT: /* Interval Timer */
        /* FIXME: Respect PSW_S bit.  */
        nullify_over(ctx);
2077
        tmp = dest_gpr(ctx, rt);
2078 2079 2080 2081 2082 2083 2084 2085 2086
        if (ctx->base.tb->cflags & CF_USE_ICOUNT) {
            gen_io_start();
            gen_helper_read_interval_timer(tmp);
            gen_io_end();
            ret = DISAS_IAQ_N_STALE;
        } else {
            gen_helper_read_interval_timer(tmp);
            ret = DISAS_NEXT;
        }
2087
        save_gpr(ctx, rt, tmp);
2088
        return nullify_end(ctx, ret);
2089 2090 2091 2092 2093
    case 26:
    case 27:
        break;
    default:
        /* All other control registers are privileged.  */
2094 2095
        CHECK_MOST_PRIVILEGED(EXCP_PRIV_REG);
        break;
2096 2097
    }

2098 2099 2100 2101 2102
    tmp = get_temp(ctx);
    tcg_gen_ld_reg(tmp, cpu_env, offsetof(CPUHPPAState, cr[ctl]));
    save_gpr(ctx, rt, tmp);

 done:
2103
    cond_free(&ctx->null_cond);
2104
    return DISAS_NEXT;
2105 2106
}

2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124
static DisasJumpType trans_mtsp(DisasContext *ctx, uint32_t insn,
                                const DisasInsn *di)
{
    unsigned rr = extract32(insn, 16, 5);
    unsigned rs = assemble_sr3(insn);
    TCGv_i64 t64;

    if (rs >= 5) {
        CHECK_MOST_PRIVILEGED(EXCP_PRIV_REG);
    }
    nullify_over(ctx);

    t64 = tcg_temp_new_i64();
    tcg_gen_extu_reg_i64(t64, load_gpr(ctx, rr));
    tcg_gen_shli_i64(t64, t64, 32);

    if (rs >= 4) {
        tcg_gen_st_i64(t64, cpu_env, offsetof(CPUHPPAState, sr[rs]));
2125
        ctx->tb_flags &= ~TB_FLAG_SR_SAME;
2126 2127 2128 2129 2130 2131 2132 2133
    } else {
        tcg_gen_mov_i64(cpu_sr[rs], t64);
    }
    tcg_temp_free_i64(t64);

    return nullify_end(ctx, DISAS_NEXT);
}

2134 2135
static DisasJumpType trans_mtctl(DisasContext *ctx, uint32_t insn,
                                 const DisasInsn *di)
2136 2137 2138
{
    unsigned rin = extract32(insn, 16, 5);
    unsigned ctl = extract32(insn, 21, 5);
2139
    TCGv_reg reg = load_gpr(ctx, rin);
2140
    TCGv_reg tmp;
2141

2142
    if (ctl == CR_SAR) {
2143
        tmp = tcg_temp_new();
2144
        tcg_gen_andi_reg(tmp, reg, TARGET_REGISTER_BITS - 1);
2145 2146
        save_or_nullify(ctx, cpu_sar, tmp);
        tcg_temp_free(tmp);
2147 2148 2149

        cond_free(&ctx->null_cond);
        return DISAS_NEXT;
2150 2151
    }

2152 2153 2154
    /* All other control registers are privileged or read-only.  */
    CHECK_MOST_PRIVILEGED(EXCP_PRIV_REG);

2155 2156 2157 2158 2159
#ifdef CONFIG_USER_ONLY
    g_assert_not_reached();
#else
    DisasJumpType ret = DISAS_NEXT;

2160 2161 2162
    nullify_over(ctx);
    switch (ctl) {
    case CR_IT:
2163
        gen_helper_write_interval_timer(cpu_env, reg);
2164
        break;
2165 2166 2167 2168 2169 2170 2171 2172
    case CR_EIRR:
        gen_helper_write_eirr(cpu_env, reg);
        break;
    case CR_EIEM:
        gen_helper_write_eiem(cpu_env, reg);
        ret = DISAS_IAQ_N_STALE_EXIT;
        break;

2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188
    case CR_IIASQ:
    case CR_IIAOQ:
        /* FIXME: Respect PSW_Q bit */
        /* The write advances the queue and stores to the back element.  */
        tmp = get_temp(ctx);
        tcg_gen_ld_reg(tmp, cpu_env,
                       offsetof(CPUHPPAState, cr_back[ctl - CR_IIASQ]));
        tcg_gen_st_reg(tmp, cpu_env, offsetof(CPUHPPAState, cr[ctl]));
        tcg_gen_st_reg(reg, cpu_env,
                       offsetof(CPUHPPAState, cr_back[ctl - CR_IIASQ]));
        break;

    default:
        tcg_gen_st_reg(reg, cpu_env, offsetof(CPUHPPAState, cr[ctl]));
        break;
    }
2189 2190
    return nullify_end(ctx, ret);
#endif
2191 2192
}

2193 2194
static DisasJumpType trans_mtsarcm(DisasContext *ctx, uint32_t insn,
                                   const DisasInsn *di)
2195 2196
{
    unsigned rin = extract32(insn, 16, 5);
2197
    TCGv_reg tmp = tcg_temp_new();
2198

2199 2200
    tcg_gen_not_reg(tmp, load_gpr(ctx, rin));
    tcg_gen_andi_reg(tmp, tmp, TARGET_REGISTER_BITS - 1);
2201 2202 2203 2204
    save_or_nullify(ctx, cpu_sar, tmp);
    tcg_temp_free(tmp);

    cond_free(&ctx->null_cond);
2205
    return DISAS_NEXT;
2206 2207
}

2208 2209
static DisasJumpType trans_ldsid(DisasContext *ctx, uint32_t insn,
                                 const DisasInsn *di)
2210 2211
{
    unsigned rt = extract32(insn, 0, 5);
2212
    TCGv_reg dest = dest_gpr(ctx, rt);
2213 2214

    /* Since we don't implement space registers, this returns zero.  */
2215
    tcg_gen_movi_reg(dest, 0);
2216 2217 2218
    save_gpr(ctx, rt, dest);

    cond_free(&ctx->null_cond);
2219
    return DISAS_NEXT;
2220 2221
}

2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292
#ifndef CONFIG_USER_ONLY
/* Note that ssm/rsm instructions number PSW_W and PSW_E differently.  */
static target_ureg extract_sm_imm(uint32_t insn)
{
    target_ureg val = extract32(insn, 16, 10);

    if (val & PSW_SM_E) {
        val = (val & ~PSW_SM_E) | PSW_E;
    }
    if (val & PSW_SM_W) {
        val = (val & ~PSW_SM_W) | PSW_W;
    }
    return val;
}

static DisasJumpType trans_rsm(DisasContext *ctx, uint32_t insn,
                               const DisasInsn *di)
{
    unsigned rt = extract32(insn, 0, 5);
    target_ureg sm = extract_sm_imm(insn);
    TCGv_reg tmp;

    CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
    nullify_over(ctx);

    tmp = get_temp(ctx);
    tcg_gen_ld_reg(tmp, cpu_env, offsetof(CPUHPPAState, psw));
    tcg_gen_andi_reg(tmp, tmp, ~sm);
    gen_helper_swap_system_mask(tmp, cpu_env, tmp);
    save_gpr(ctx, rt, tmp);

    /* Exit the TB to recognize new interrupts, e.g. PSW_M.  */
    return nullify_end(ctx, DISAS_IAQ_N_STALE_EXIT);
}

static DisasJumpType trans_ssm(DisasContext *ctx, uint32_t insn,
                               const DisasInsn *di)
{
    unsigned rt = extract32(insn, 0, 5);
    target_ureg sm = extract_sm_imm(insn);
    TCGv_reg tmp;

    CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
    nullify_over(ctx);

    tmp = get_temp(ctx);
    tcg_gen_ld_reg(tmp, cpu_env, offsetof(CPUHPPAState, psw));
    tcg_gen_ori_reg(tmp, tmp, sm);
    gen_helper_swap_system_mask(tmp, cpu_env, tmp);
    save_gpr(ctx, rt, tmp);

    /* Exit the TB to recognize new interrupts, e.g. PSW_I.  */
    return nullify_end(ctx, DISAS_IAQ_N_STALE_EXIT);
}

static DisasJumpType trans_mtsm(DisasContext *ctx, uint32_t insn,
                                const DisasInsn *di)
{
    unsigned rr = extract32(insn, 16, 5);
    TCGv_reg tmp, reg;

    CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
    nullify_over(ctx);

    reg = load_gpr(ctx, rr);
    tmp = get_temp(ctx);
    gen_helper_swap_system_mask(tmp, cpu_env, reg);

    /* Exit the TB to recognize new interrupts.  */
    return nullify_end(ctx, DISAS_IAQ_N_STALE_EXIT);
}
R
Richard Henderson 已提交
2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315

static DisasJumpType trans_rfi(DisasContext *ctx, uint32_t insn,
                               const DisasInsn *di)
{
    unsigned comp = extract32(insn, 5, 4);

    CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
    nullify_over(ctx);

    if (comp == 5) {
        gen_helper_rfi_r(cpu_env);
    } else {
        gen_helper_rfi(cpu_env);
    }
    if (ctx->base.singlestep_enabled) {
        gen_excp_1(EXCP_DEBUG);
    } else {
        tcg_gen_exit_tb(0);
    }

    /* Exit the TB to recognize new interrupts.  */
    return nullify_end(ctx, DISAS_NORETURN);
}
2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327

static DisasJumpType gen_hlt(DisasContext *ctx, int reset)
{
    CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
    nullify_over(ctx);
    if (reset) {
        gen_helper_reset(cpu_env);
    } else {
        gen_helper_halt(cpu_env);
    }
    return nullify_end(ctx, DISAS_NORETURN);
}
2328 2329
#endif /* !CONFIG_USER_ONLY */

2330 2331
static const DisasInsn table_system[] = {
    { 0x00000000u, 0xfc001fe0u, trans_break },
2332
    { 0x00001820u, 0xffe01fffu, trans_mtsp },
2333 2334 2335 2336
    { 0x00001840u, 0xfc00ffffu, trans_mtctl },
    { 0x016018c0u, 0xffe0ffffu, trans_mtsarcm },
    { 0x000014a0u, 0xffffffe0u, trans_mfia },
    { 0x000004a0u, 0xffff1fe0u, trans_mfsp },
2337
    { 0x000008a0u, 0xfc1fbfe0u, trans_mfctl },
2338 2339
    { 0x00000400u, 0xffffffffu, trans_sync },  /* sync */
    { 0x00100400u, 0xffffffffu, trans_sync },  /* syncdma */
2340
    { 0x000010a0u, 0xfc1f3fe0u, trans_ldsid },
2341 2342 2343 2344
#ifndef CONFIG_USER_ONLY
    { 0x00000e60u, 0xfc00ffe0u, trans_rsm },
    { 0x00000d60u, 0xfc00ffe0u, trans_ssm },
    { 0x00001860u, 0xffe0ffffu, trans_mtsm },
R
Richard Henderson 已提交
2345
    { 0x00000c00u, 0xfffffe1fu, trans_rfi },
2346
#endif
2347 2348
};

2349 2350
static DisasJumpType trans_base_idx_mod(DisasContext *ctx, uint32_t insn,
                                        const DisasInsn *di)
2351 2352 2353
{
    unsigned rb = extract32(insn, 21, 5);
    unsigned rx = extract32(insn, 16, 5);
2354 2355 2356
    TCGv_reg dest = dest_gpr(ctx, rb);
    TCGv_reg src1 = load_gpr(ctx, rb);
    TCGv_reg src2 = load_gpr(ctx, rx);
2357 2358

    /* The only thing we need to do is the base register modification.  */
2359
    tcg_gen_add_reg(dest, src1, src2);
2360 2361 2362
    save_gpr(ctx, rb, dest);

    cond_free(&ctx->null_cond);
2363
    return DISAS_NEXT;
2364 2365
}

2366 2367
static DisasJumpType trans_probe(DisasContext *ctx, uint32_t insn,
                                 const DisasInsn *di)
2368 2369
{
    unsigned rt = extract32(insn, 0, 5);
2370
    unsigned sp = extract32(insn, 14, 2);
2371 2372
    unsigned rb = extract32(insn, 21, 5);
    unsigned is_write = extract32(insn, 6, 1);
2373 2374
    TCGv_reg dest, ofs;
    TCGv_tl addr;
2375 2376 2377 2378 2379

    nullify_over(ctx);

    /* ??? Do something with priv level operand.  */
    dest = dest_gpr(ctx, rt);
2380
    form_gva(ctx, &addr, &ofs, rb, 0, 0, 0, sp, 0, false);
2381
    if (is_write) {
2382
        gen_helper_probe_w(dest, addr);
2383
    } else {
2384
        gen_helper_probe_r(dest, addr);
2385 2386
    }
    save_gpr(ctx, rt, dest);
2387
    return nullify_end(ctx, DISAS_NEXT);
2388 2389
}

2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420
#ifndef CONFIG_USER_ONLY
static DisasJumpType trans_ixtlbx(DisasContext *ctx, uint32_t insn,
                                  const DisasInsn *di)
{
    unsigned sp;
    unsigned rr = extract32(insn, 16, 5);
    unsigned rb = extract32(insn, 21, 5);
    unsigned is_data = insn & 0x1000;
    unsigned is_addr = insn & 0x40;
    TCGv_tl addr;
    TCGv_reg ofs, reg;

    if (is_data) {
        sp = extract32(insn, 14, 2);
    } else {
        sp = ~assemble_sr3(insn);
    }

    CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
    nullify_over(ctx);

    form_gva(ctx, &addr, &ofs, rb, 0, 0, 0, sp, 0, false);
    reg = load_gpr(ctx, rr);
    if (is_addr) {
        gen_helper_itlba(cpu_env, addr, reg);
    } else {
        gen_helper_itlbp(cpu_env, addr, reg);
    }

    /* Exit TB for ITLB change if mmu is enabled.  This *should* not be
       the case, since the OS TLB fill handler runs with mmu disabled.  */
2421
    return nullify_end(ctx, !is_data && (ctx->tb_flags & PSW_C)
2422 2423
                       ? DISAS_IAQ_N_STALE : DISAS_NEXT);
}
2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456

static DisasJumpType trans_pxtlbx(DisasContext *ctx, uint32_t insn,
                                  const DisasInsn *di)
{
    unsigned m = extract32(insn, 5, 1);
    unsigned sp;
    unsigned rx = extract32(insn, 16, 5);
    unsigned rb = extract32(insn, 21, 5);
    unsigned is_data = insn & 0x1000;
    unsigned is_local = insn & 0x40;
    TCGv_tl addr;
    TCGv_reg ofs;

    if (is_data) {
        sp = extract32(insn, 14, 2);
    } else {
        sp = ~assemble_sr3(insn);
    }

    CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
    nullify_over(ctx);

    form_gva(ctx, &addr, &ofs, rb, rx, 0, 0, sp, m, false);
    if (m) {
        save_gpr(ctx, rb, ofs);
    }
    if (is_local) {
        gen_helper_ptlbe(cpu_env);
    } else {
        gen_helper_ptlb(cpu_env, addr);
    }

    /* Exit TB for TLB change if mmu is enabled.  */
2457
    return nullify_end(ctx, !is_data && (ctx->tb_flags & PSW_C)
2458 2459
                       ? DISAS_IAQ_N_STALE : DISAS_NEXT);
}
R
Richard Henderson 已提交
2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488

static DisasJumpType trans_lpa(DisasContext *ctx, uint32_t insn,
                               const DisasInsn *di)
{
    unsigned rt = extract32(insn, 0, 5);
    unsigned m = extract32(insn, 5, 1);
    unsigned sp = extract32(insn, 14, 2);
    unsigned rx = extract32(insn, 16, 5);
    unsigned rb = extract32(insn, 21, 5);
    TCGv_tl vaddr;
    TCGv_reg ofs, paddr;

    CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
    nullify_over(ctx);

    form_gva(ctx, &vaddr, &ofs, rb, rx, 0, 0, sp, m, false);

    paddr = tcg_temp_new();
    gen_helper_lpa(paddr, cpu_env, vaddr);

    /* Note that physical address result overrides base modification.  */
    if (m) {
        save_gpr(ctx, rb, ofs);
    }
    save_gpr(ctx, rt, paddr);
    tcg_temp_free(paddr);

    return nullify_end(ctx, DISAS_NEXT);
}
R
Richard Henderson 已提交
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507

static DisasJumpType trans_lci(DisasContext *ctx, uint32_t insn,
                               const DisasInsn *di)
{
    unsigned rt = extract32(insn, 0, 5);
    TCGv_reg ci;

    CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);

    /* The Coherence Index is an implementation-defined function of the
       physical address.  Two addresses with the same CI have a coherent
       view of the cache.  Our implementation is to return 0 for all,
       since the entire address space is coherent.  */
    ci = tcg_const_reg(0);
    save_gpr(ctx, rt, ci);
    tcg_temp_free(ci);

    return DISAS_NEXT;
}
2508 2509
#endif /* !CONFIG_USER_ONLY */

2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525
static const DisasInsn table_mem_mgmt[] = {
    { 0x04003280u, 0xfc003fffu, trans_nop },          /* fdc, disp */
    { 0x04001280u, 0xfc003fffu, trans_nop },          /* fdc, index */
    { 0x040012a0u, 0xfc003fffu, trans_base_idx_mod }, /* fdc, index, base mod */
    { 0x040012c0u, 0xfc003fffu, trans_nop },          /* fdce */
    { 0x040012e0u, 0xfc003fffu, trans_base_idx_mod }, /* fdce, base mod */
    { 0x04000280u, 0xfc001fffu, trans_nop },          /* fic 0a */
    { 0x040002a0u, 0xfc001fffu, trans_base_idx_mod }, /* fic 0a, base mod */
    { 0x040013c0u, 0xfc003fffu, trans_nop },          /* fic 4f */
    { 0x040013e0u, 0xfc003fffu, trans_base_idx_mod }, /* fic 4f, base mod */
    { 0x040002c0u, 0xfc001fffu, trans_nop },          /* fice */
    { 0x040002e0u, 0xfc001fffu, trans_base_idx_mod }, /* fice, base mod */
    { 0x04002700u, 0xfc003fffu, trans_nop },          /* pdc */
    { 0x04002720u, 0xfc003fffu, trans_base_idx_mod }, /* pdc, base mod */
    { 0x04001180u, 0xfc003fa0u, trans_probe },        /* probe */
    { 0x04003180u, 0xfc003fa0u, trans_probe },        /* probei */
2526 2527 2528 2529 2530
#ifndef CONFIG_USER_ONLY
    { 0x04000000u, 0xfc001fffu, trans_ixtlbx },       /* iitlbp */
    { 0x04000040u, 0xfc001fffu, trans_ixtlbx },       /* iitlba */
    { 0x04001000u, 0xfc001fffu, trans_ixtlbx },       /* idtlbp */
    { 0x04001040u, 0xfc001fffu, trans_ixtlbx },       /* idtlba */
2531 2532 2533 2534
    { 0x04000200u, 0xfc001fdfu, trans_pxtlbx },       /* pitlb */
    { 0x04000240u, 0xfc001fdfu, trans_pxtlbx },       /* pitlbe */
    { 0x04001200u, 0xfc001fdfu, trans_pxtlbx },       /* pdtlb */
    { 0x04001240u, 0xfc001fdfu, trans_pxtlbx },       /* pdtlbe */
R
Richard Henderson 已提交
2535
    { 0x04001340u, 0xfc003fc0u, trans_lpa },
R
Richard Henderson 已提交
2536
    { 0x04001300u, 0xfc003fe0u, trans_lci },
2537
#endif
2538 2539
};

2540 2541
static DisasJumpType trans_add(DisasContext *ctx, uint32_t insn,
                               const DisasInsn *di)
2542 2543 2544 2545 2546 2547 2548
{
    unsigned r2 = extract32(insn, 21, 5);
    unsigned r1 = extract32(insn, 16, 5);
    unsigned cf = extract32(insn, 12, 4);
    unsigned ext = extract32(insn, 8, 4);
    unsigned shift = extract32(insn, 6, 2);
    unsigned rt = extract32(insn,  0, 5);
2549
    TCGv_reg tcg_r1, tcg_r2;
2550 2551 2552 2553
    bool is_c = false;
    bool is_l = false;
    bool is_tc = false;
    bool is_tsv = false;
2554
    DisasJumpType ret;
2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583

    switch (ext) {
    case 0x6: /* ADD, SHLADD */
        break;
    case 0xa: /* ADD,L, SHLADD,L */
        is_l = true;
        break;
    case 0xe: /* ADD,TSV, SHLADD,TSV (1) */
        is_tsv = true;
        break;
    case 0x7: /* ADD,C */
        is_c = true;
        break;
    case 0xf: /* ADD,C,TSV */
        is_c = is_tsv = true;
        break;
    default:
        return gen_illegal(ctx);
    }

    if (cf) {
        nullify_over(ctx);
    }
    tcg_r1 = load_gpr(ctx, r1);
    tcg_r2 = load_gpr(ctx, r2);
    ret = do_add(ctx, rt, tcg_r1, tcg_r2, shift, is_l, is_tsv, is_tc, is_c, cf);
    return nullify_end(ctx, ret);
}

2584 2585
static DisasJumpType trans_sub(DisasContext *ctx, uint32_t insn,
                               const DisasInsn *di)
2586 2587 2588 2589 2590 2591
{
    unsigned r2 = extract32(insn, 21, 5);
    unsigned r1 = extract32(insn, 16, 5);
    unsigned cf = extract32(insn, 12, 4);
    unsigned ext = extract32(insn, 6, 6);
    unsigned rt = extract32(insn,  0, 5);
2592
    TCGv_reg tcg_r1, tcg_r2;
2593 2594 2595
    bool is_b = false;
    bool is_tc = false;
    bool is_tsv = false;
2596
    DisasJumpType ret;
2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628

    switch (ext) {
    case 0x10: /* SUB */
        break;
    case 0x30: /* SUB,TSV */
        is_tsv = true;
        break;
    case 0x14: /* SUB,B */
        is_b = true;
        break;
    case 0x34: /* SUB,B,TSV */
        is_b = is_tsv = true;
        break;
    case 0x13: /* SUB,TC */
        is_tc = true;
        break;
    case 0x33: /* SUB,TSV,TC */
        is_tc = is_tsv = true;
        break;
    default:
        return gen_illegal(ctx);
    }

    if (cf) {
        nullify_over(ctx);
    }
    tcg_r1 = load_gpr(ctx, r1);
    tcg_r2 = load_gpr(ctx, r2);
    ret = do_sub(ctx, rt, tcg_r1, tcg_r2, is_tsv, is_b, is_tc, cf);
    return nullify_end(ctx, ret);
}

2629 2630
static DisasJumpType trans_log(DisasContext *ctx, uint32_t insn,
                               const DisasInsn *di)
2631 2632 2633 2634 2635
{
    unsigned r2 = extract32(insn, 21, 5);
    unsigned r1 = extract32(insn, 16, 5);
    unsigned cf = extract32(insn, 12, 4);
    unsigned rt = extract32(insn,  0, 5);
2636
    TCGv_reg tcg_r1, tcg_r2;
2637
    DisasJumpType ret;
2638 2639 2640 2641 2642 2643

    if (cf) {
        nullify_over(ctx);
    }
    tcg_r1 = load_gpr(ctx, r1);
    tcg_r2 = load_gpr(ctx, r2);
2644
    ret = do_log(ctx, rt, tcg_r1, tcg_r2, cf, di->f.ttt);
2645 2646 2647 2648
    return nullify_end(ctx, ret);
}

/* OR r,0,t -> COPY (according to gas) */
2649 2650
static DisasJumpType trans_copy(DisasContext *ctx, uint32_t insn,
                                const DisasInsn *di)
2651 2652 2653 2654 2655
{
    unsigned r1 = extract32(insn, 16, 5);
    unsigned rt = extract32(insn,  0, 5);

    if (r1 == 0) {
2656 2657
        TCGv_reg dest = dest_gpr(ctx, rt);
        tcg_gen_movi_reg(dest, 0);
2658 2659 2660 2661 2662
        save_gpr(ctx, rt, dest);
    } else {
        save_gpr(ctx, rt, cpu_gr[r1]);
    }
    cond_free(&ctx->null_cond);
2663
    return DISAS_NEXT;
2664 2665
}

2666 2667
static DisasJumpType trans_cmpclr(DisasContext *ctx, uint32_t insn,
                                  const DisasInsn *di)
2668 2669 2670 2671 2672
{
    unsigned r2 = extract32(insn, 21, 5);
    unsigned r1 = extract32(insn, 16, 5);
    unsigned cf = extract32(insn, 12, 4);
    unsigned rt = extract32(insn,  0, 5);
2673
    TCGv_reg tcg_r1, tcg_r2;
2674
    DisasJumpType ret;
2675 2676 2677 2678 2679 2680 2681 2682 2683 2684

    if (cf) {
        nullify_over(ctx);
    }
    tcg_r1 = load_gpr(ctx, r1);
    tcg_r2 = load_gpr(ctx, r2);
    ret = do_cmpclr(ctx, rt, tcg_r1, tcg_r2, cf);
    return nullify_end(ctx, ret);
}

2685 2686
static DisasJumpType trans_uxor(DisasContext *ctx, uint32_t insn,
                                const DisasInsn *di)
2687 2688 2689 2690 2691
{
    unsigned r2 = extract32(insn, 21, 5);
    unsigned r1 = extract32(insn, 16, 5);
    unsigned cf = extract32(insn, 12, 4);
    unsigned rt = extract32(insn,  0, 5);
2692
    TCGv_reg tcg_r1, tcg_r2;
2693
    DisasJumpType ret;
2694 2695 2696 2697 2698 2699

    if (cf) {
        nullify_over(ctx);
    }
    tcg_r1 = load_gpr(ctx, r1);
    tcg_r2 = load_gpr(ctx, r2);
2700
    ret = do_unit(ctx, rt, tcg_r1, tcg_r2, cf, false, tcg_gen_xor_reg);
2701 2702 2703
    return nullify_end(ctx, ret);
}

2704 2705
static DisasJumpType trans_uaddcm(DisasContext *ctx, uint32_t insn,
                                  const DisasInsn *di)
2706 2707 2708 2709 2710 2711
{
    unsigned r2 = extract32(insn, 21, 5);
    unsigned r1 = extract32(insn, 16, 5);
    unsigned cf = extract32(insn, 12, 4);
    unsigned is_tc = extract32(insn, 6, 1);
    unsigned rt = extract32(insn,  0, 5);
2712
    TCGv_reg tcg_r1, tcg_r2, tmp;
2713
    DisasJumpType ret;
2714 2715 2716 2717 2718 2719 2720

    if (cf) {
        nullify_over(ctx);
    }
    tcg_r1 = load_gpr(ctx, r1);
    tcg_r2 = load_gpr(ctx, r2);
    tmp = get_temp(ctx);
2721 2722
    tcg_gen_not_reg(tmp, tcg_r2);
    ret = do_unit(ctx, rt, tcg_r1, tmp, cf, is_tc, tcg_gen_add_reg);
2723 2724 2725
    return nullify_end(ctx, ret);
}

2726 2727
static DisasJumpType trans_dcor(DisasContext *ctx, uint32_t insn,
                                const DisasInsn *di)
2728 2729 2730 2731 2732
{
    unsigned r2 = extract32(insn, 21, 5);
    unsigned cf = extract32(insn, 12, 4);
    unsigned is_i = extract32(insn, 6, 1);
    unsigned rt = extract32(insn,  0, 5);
2733
    TCGv_reg tmp;
2734
    DisasJumpType ret;
2735 2736 2737 2738

    nullify_over(ctx);

    tmp = get_temp(ctx);
2739
    tcg_gen_shri_reg(tmp, cpu_psw_cb, 3);
2740
    if (!is_i) {
2741
        tcg_gen_not_reg(tmp, tmp);
2742
    }
2743 2744
    tcg_gen_andi_reg(tmp, tmp, 0x11111111);
    tcg_gen_muli_reg(tmp, tmp, 6);
2745
    ret = do_unit(ctx, rt, tmp, load_gpr(ctx, r2), cf, false,
2746
                  is_i ? tcg_gen_add_reg : tcg_gen_sub_reg);
2747 2748 2749 2750

    return nullify_end(ctx, ret);
}

2751 2752
static DisasJumpType trans_ds(DisasContext *ctx, uint32_t insn,
                              const DisasInsn *di)
2753 2754 2755 2756 2757
{
    unsigned r2 = extract32(insn, 21, 5);
    unsigned r1 = extract32(insn, 16, 5);
    unsigned cf = extract32(insn, 12, 4);
    unsigned rt = extract32(insn,  0, 5);
2758
    TCGv_reg dest, add1, add2, addc, zero, in1, in2;
2759 2760 2761 2762 2763 2764 2765 2766 2767 2768

    nullify_over(ctx);

    in1 = load_gpr(ctx, r1);
    in2 = load_gpr(ctx, r2);

    add1 = tcg_temp_new();
    add2 = tcg_temp_new();
    addc = tcg_temp_new();
    dest = tcg_temp_new();
2769
    zero = tcg_const_reg(0);
2770 2771

    /* Form R1 << 1 | PSW[CB]{8}.  */
2772 2773
    tcg_gen_add_reg(add1, in1, in1);
    tcg_gen_add_reg(add1, add1, cpu_psw_cb_msb);
2774 2775 2776 2777 2778

    /* Add or subtract R2, depending on PSW[V].  Proper computation of
       carry{8} requires that we subtract via + ~R2 + 1, as described in
       the manual.  By extracting and masking V, we can produce the
       proper inputs to the addition without movcond.  */
2779 2780 2781
    tcg_gen_sari_reg(addc, cpu_psw_v, TARGET_REGISTER_BITS - 1);
    tcg_gen_xor_reg(add2, in2, addc);
    tcg_gen_andi_reg(addc, addc, 1);
2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792
    /* ??? This is only correct for 32-bit.  */
    tcg_gen_add2_i32(dest, cpu_psw_cb_msb, add1, zero, add2, zero);
    tcg_gen_add2_i32(dest, cpu_psw_cb_msb, dest, cpu_psw_cb_msb, addc, zero);

    tcg_temp_free(addc);
    tcg_temp_free(zero);

    /* Write back the result register.  */
    save_gpr(ctx, rt, dest);

    /* Write back PSW[CB].  */
2793 2794
    tcg_gen_xor_reg(cpu_psw_cb, add1, add2);
    tcg_gen_xor_reg(cpu_psw_cb, cpu_psw_cb, dest);
2795 2796

    /* Write back PSW[V] for the division step.  */
2797 2798
    tcg_gen_neg_reg(cpu_psw_v, cpu_psw_cb_msb);
    tcg_gen_xor_reg(cpu_psw_v, cpu_psw_v, in2);
2799 2800 2801

    /* Install the new nullification.  */
    if (cf) {
2802
        TCGv_reg sv = NULL;
2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813
        if (cf >> 1 == 6) {
            /* ??? The lshift is supposed to contribute to overflow.  */
            sv = do_add_sv(ctx, dest, add1, add2);
        }
        ctx->null_cond = do_cond(cf, dest, cpu_psw_cb_msb, sv);
    }

    tcg_temp_free(add1);
    tcg_temp_free(add2);
    tcg_temp_free(dest);

2814
    return nullify_end(ctx, DISAS_NEXT);
2815 2816 2817 2818 2819
}

static const DisasInsn table_arith_log[] = {
    { 0x08000240u, 0xfc00ffffu, trans_nop },  /* or x,y,0 */
    { 0x08000240u, 0xffe0ffe0u, trans_copy }, /* or x,0,t */
2820 2821 2822 2823
    { 0x08000000u, 0xfc000fe0u, trans_log, .f.ttt = tcg_gen_andc_reg },
    { 0x08000200u, 0xfc000fe0u, trans_log, .f.ttt = tcg_gen_and_reg },
    { 0x08000240u, 0xfc000fe0u, trans_log, .f.ttt = tcg_gen_or_reg },
    { 0x08000280u, 0xfc000fe0u, trans_log, .f.ttt = tcg_gen_xor_reg },
2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834
    { 0x08000880u, 0xfc000fe0u, trans_cmpclr },
    { 0x08000380u, 0xfc000fe0u, trans_uxor },
    { 0x08000980u, 0xfc000fa0u, trans_uaddcm },
    { 0x08000b80u, 0xfc1f0fa0u, trans_dcor },
    { 0x08000440u, 0xfc000fe0u, trans_ds },
    { 0x08000700u, 0xfc0007e0u, trans_add }, /* add */
    { 0x08000400u, 0xfc0006e0u, trans_sub }, /* sub; sub,b; sub,tsv */
    { 0x080004c0u, 0xfc0007e0u, trans_sub }, /* sub,tc; sub,tsv,tc */
    { 0x08000200u, 0xfc000320u, trans_add }, /* shladd */
};

2835
static DisasJumpType trans_addi(DisasContext *ctx, uint32_t insn)
2836
{
2837
    target_sreg im = low_sextract(insn, 0, 11);
2838 2839 2840 2841 2842
    unsigned e1 = extract32(insn, 11, 1);
    unsigned cf = extract32(insn, 12, 4);
    unsigned rt = extract32(insn, 16, 5);
    unsigned r2 = extract32(insn, 21, 5);
    unsigned o1 = extract32(insn, 26, 1);
2843
    TCGv_reg tcg_im, tcg_r2;
2844
    DisasJumpType ret;
2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856

    if (cf) {
        nullify_over(ctx);
    }

    tcg_im = load_const(ctx, im);
    tcg_r2 = load_gpr(ctx, r2);
    ret = do_add(ctx, rt, tcg_im, tcg_r2, 0, false, e1, !o1, false, cf);

    return nullify_end(ctx, ret);
}

2857
static DisasJumpType trans_subi(DisasContext *ctx, uint32_t insn)
2858
{
2859
    target_sreg im = low_sextract(insn, 0, 11);
2860 2861 2862 2863
    unsigned e1 = extract32(insn, 11, 1);
    unsigned cf = extract32(insn, 12, 4);
    unsigned rt = extract32(insn, 16, 5);
    unsigned r2 = extract32(insn, 21, 5);
2864
    TCGv_reg tcg_im, tcg_r2;
2865
    DisasJumpType ret;
2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877

    if (cf) {
        nullify_over(ctx);
    }

    tcg_im = load_const(ctx, im);
    tcg_r2 = load_gpr(ctx, r2);
    ret = do_sub(ctx, rt, tcg_im, tcg_r2, e1, false, false, cf);

    return nullify_end(ctx, ret);
}

2878
static DisasJumpType trans_cmpiclr(DisasContext *ctx, uint32_t insn)
2879
{
2880
    target_sreg im = low_sextract(insn, 0, 11);
2881 2882 2883
    unsigned cf = extract32(insn, 12, 4);
    unsigned rt = extract32(insn, 16, 5);
    unsigned r2 = extract32(insn, 21, 5);
2884
    TCGv_reg tcg_im, tcg_r2;
2885
    DisasJumpType ret;
2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897

    if (cf) {
        nullify_over(ctx);
    }

    tcg_im = load_const(ctx, im);
    tcg_r2 = load_gpr(ctx, r2);
    ret = do_cmpclr(ctx, rt, tcg_im, tcg_r2, cf);

    return nullify_end(ctx, ret);
}

2898 2899
static DisasJumpType trans_ld_idx_i(DisasContext *ctx, uint32_t insn,
                                    const DisasInsn *di)
2900 2901 2902 2903 2904
{
    unsigned rt = extract32(insn, 0, 5);
    unsigned m = extract32(insn, 5, 1);
    unsigned sz = extract32(insn, 6, 2);
    unsigned a = extract32(insn, 13, 1);
2905
    unsigned sp = extract32(insn, 14, 2);
2906 2907 2908 2909 2910
    int disp = low_sextract(insn, 16, 5);
    unsigned rb = extract32(insn, 21, 5);
    int modify = (m ? (a ? -1 : 1) : 0);
    TCGMemOp mop = MO_TE | sz;

2911
    return do_load(ctx, rt, rb, 0, 0, disp, sp, modify, mop);
2912 2913
}

2914 2915
static DisasJumpType trans_ld_idx_x(DisasContext *ctx, uint32_t insn,
                                    const DisasInsn *di)
2916 2917 2918 2919 2920
{
    unsigned rt = extract32(insn, 0, 5);
    unsigned m = extract32(insn, 5, 1);
    unsigned sz = extract32(insn, 6, 2);
    unsigned u = extract32(insn, 13, 1);
2921
    unsigned sp = extract32(insn, 14, 2);
2922 2923 2924 2925
    unsigned rx = extract32(insn, 16, 5);
    unsigned rb = extract32(insn, 21, 5);
    TCGMemOp mop = MO_TE | sz;

2926
    return do_load(ctx, rt, rb, rx, u ? sz : 0, 0, sp, m, mop);
2927 2928
}

2929 2930
static DisasJumpType trans_st_idx_i(DisasContext *ctx, uint32_t insn,
                                    const DisasInsn *di)
2931 2932 2933 2934 2935
{
    int disp = low_sextract(insn, 0, 5);
    unsigned m = extract32(insn, 5, 1);
    unsigned sz = extract32(insn, 6, 2);
    unsigned a = extract32(insn, 13, 1);
2936
    unsigned sp = extract32(insn, 14, 2);
2937 2938 2939 2940 2941
    unsigned rr = extract32(insn, 16, 5);
    unsigned rb = extract32(insn, 21, 5);
    int modify = (m ? (a ? -1 : 1) : 0);
    TCGMemOp mop = MO_TE | sz;

2942
    return do_store(ctx, rr, rb, disp, sp, modify, mop);
2943 2944
}

2945 2946
static DisasJumpType trans_ldcw(DisasContext *ctx, uint32_t insn,
                                const DisasInsn *di)
2947 2948 2949 2950 2951
{
    unsigned rt = extract32(insn, 0, 5);
    unsigned m = extract32(insn, 5, 1);
    unsigned i = extract32(insn, 12, 1);
    unsigned au = extract32(insn, 13, 1);
2952
    unsigned sp = extract32(insn, 14, 2);
2953 2954 2955
    unsigned rx = extract32(insn, 16, 5);
    unsigned rb = extract32(insn, 21, 5);
    TCGMemOp mop = MO_TEUL | MO_ALIGN_16;
2956 2957
    TCGv_reg zero, dest, ofs;
    TCGv_tl addr;
2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972
    int modify, disp = 0, scale = 0;

    nullify_over(ctx);

    if (i) {
        modify = (m ? (au ? -1 : 1) : 0);
        disp = low_sextract(rx, 0, 5);
        rx = 0;
    } else {
        modify = m;
        if (au) {
            scale = mop & MO_SIZE;
        }
    }
    if (modify) {
2973 2974
        /* Base register modification.  Make sure if RT == RB,
           we see the result of the load.  */
2975 2976 2977 2978 2979
        dest = get_temp(ctx);
    } else {
        dest = dest_gpr(ctx, rt);
    }

2980 2981
    form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
             ctx->mmu_idx == MMU_PHYS_IDX);
2982
    zero = tcg_const_reg(0);
2983
    tcg_gen_atomic_xchg_reg(dest, addr, zero, ctx->mmu_idx, mop);
2984
    if (modify) {
2985
        save_gpr(ctx, rb, ofs);
2986 2987 2988
    }
    save_gpr(ctx, rt, dest);

2989
    return nullify_end(ctx, DISAS_NEXT);
2990 2991
}

2992 2993
static DisasJumpType trans_stby(DisasContext *ctx, uint32_t insn,
                                const DisasInsn *di)
2994
{
2995
    target_sreg disp = low_sextract(insn, 0, 5);
2996 2997
    unsigned m = extract32(insn, 5, 1);
    unsigned a = extract32(insn, 13, 1);
2998
    unsigned sp = extract32(insn, 14, 2);
2999 3000
    unsigned rt = extract32(insn, 16, 5);
    unsigned rb = extract32(insn, 21, 5);
3001 3002
    TCGv_reg ofs, val;
    TCGv_tl addr;
3003 3004 3005

    nullify_over(ctx);

3006 3007
    form_gva(ctx, &addr, &ofs, rb, 0, 0, disp, sp, m,
             ctx->mmu_idx == MMU_PHYS_IDX);
3008 3009
    val = load_gpr(ctx, rt);
    if (a) {
3010 3011 3012 3013 3014
        if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
            gen_helper_stby_e_parallel(cpu_env, addr, val);
        } else {
            gen_helper_stby_e(cpu_env, addr, val);
        }
3015
    } else {
3016 3017 3018 3019 3020
        if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
            gen_helper_stby_b_parallel(cpu_env, addr, val);
        } else {
            gen_helper_stby_b(cpu_env, addr, val);
        }
3021 3022 3023
    }

    if (m) {
3024 3025
        tcg_gen_andi_reg(ofs, ofs, ~3);
        save_gpr(ctx, rb, ofs);
3026 3027
    }

3028
    return nullify_end(ctx, DISAS_NEXT);
3029 3030
}

R
Richard Henderson 已提交
3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064
#ifndef CONFIG_USER_ONLY
static DisasJumpType trans_ldwa_idx_i(DisasContext *ctx, uint32_t insn,
                                      const DisasInsn *di)
{
    int hold_mmu_idx = ctx->mmu_idx;
    DisasJumpType ret;

    CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);

    /* ??? needs fixing for hppa64 -- ldda does not follow the same
       format wrt the sub-opcode in bits 6:9.  */
    ctx->mmu_idx = MMU_PHYS_IDX;
    ret = trans_ld_idx_i(ctx, insn, di);
    ctx->mmu_idx = hold_mmu_idx;
    return ret;
}

static DisasJumpType trans_ldwa_idx_x(DisasContext *ctx, uint32_t insn,
                                      const DisasInsn *di)
{
    int hold_mmu_idx = ctx->mmu_idx;
    DisasJumpType ret;

    CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);

    /* ??? needs fixing for hppa64 -- ldda does not follow the same
       format wrt the sub-opcode in bits 6:9.  */
    ctx->mmu_idx = MMU_PHYS_IDX;
    ret = trans_ld_idx_x(ctx, insn, di);
    ctx->mmu_idx = hold_mmu_idx;
    return ret;
}
#endif

3065 3066 3067 3068 3069 3070
static const DisasInsn table_index_mem[] = {
    { 0x0c001000u, 0xfc001300, trans_ld_idx_i }, /* LD[BHWD], im */
    { 0x0c000000u, 0xfc001300, trans_ld_idx_x }, /* LD[BHWD], rx */
    { 0x0c001200u, 0xfc001300, trans_st_idx_i }, /* ST[BHWD] */
    { 0x0c0001c0u, 0xfc0003c0, trans_ldcw },
    { 0x0c001300u, 0xfc0013c0, trans_stby },
R
Richard Henderson 已提交
3071 3072 3073 3074
#ifndef CONFIG_USER_ONLY
    { 0x0c001180u, 0xfc00d3c0, trans_ldwa_idx_i }, /* LDWA, im */
    { 0x0c000180u, 0xfc00d3c0, trans_ldwa_idx_x }, /* LDWA, rx */
#endif
3075 3076
};

3077
static DisasJumpType trans_ldil(DisasContext *ctx, uint32_t insn)
3078 3079
{
    unsigned rt = extract32(insn, 21, 5);
3080 3081
    target_sreg i = assemble_21(insn);
    TCGv_reg tcg_rt = dest_gpr(ctx, rt);
3082

3083
    tcg_gen_movi_reg(tcg_rt, i);
3084 3085 3086
    save_gpr(ctx, rt, tcg_rt);
    cond_free(&ctx->null_cond);

3087
    return DISAS_NEXT;
3088 3089
}

3090
static DisasJumpType trans_addil(DisasContext *ctx, uint32_t insn)
3091 3092
{
    unsigned rt = extract32(insn, 21, 5);
3093 3094 3095
    target_sreg i = assemble_21(insn);
    TCGv_reg tcg_rt = load_gpr(ctx, rt);
    TCGv_reg tcg_r1 = dest_gpr(ctx, 1);
3096

3097
    tcg_gen_addi_reg(tcg_r1, tcg_rt, i);
3098 3099 3100
    save_gpr(ctx, 1, tcg_r1);
    cond_free(&ctx->null_cond);

3101
    return DISAS_NEXT;
3102 3103
}

3104
static DisasJumpType trans_ldo(DisasContext *ctx, uint32_t insn)
3105 3106 3107
{
    unsigned rb = extract32(insn, 21, 5);
    unsigned rt = extract32(insn, 16, 5);
3108 3109
    target_sreg i = assemble_16(insn);
    TCGv_reg tcg_rt = dest_gpr(ctx, rt);
3110 3111 3112 3113

    /* Special case rb == 0, for the LDI pseudo-op.
       The COPY pseudo-op is handled for free within tcg_gen_addi_tl.  */
    if (rb == 0) {
3114
        tcg_gen_movi_reg(tcg_rt, i);
3115
    } else {
3116
        tcg_gen_addi_reg(tcg_rt, cpu_gr[rb], i);
3117 3118 3119 3120
    }
    save_gpr(ctx, rt, tcg_rt);
    cond_free(&ctx->null_cond);

3121
    return DISAS_NEXT;
3122 3123
}

3124 3125
static DisasJumpType trans_load(DisasContext *ctx, uint32_t insn,
                                bool is_mod, TCGMemOp mop)
3126 3127 3128
{
    unsigned rb = extract32(insn, 21, 5);
    unsigned rt = extract32(insn, 16, 5);
3129
    unsigned sp = extract32(insn, 14, 2);
3130
    target_sreg i = assemble_16(insn);
3131

3132 3133
    return do_load(ctx, rt, rb, 0, 0, i, sp,
                   is_mod ? (i < 0 ? -1 : 1) : 0, mop);
3134 3135
}

3136
static DisasJumpType trans_load_w(DisasContext *ctx, uint32_t insn)
3137 3138 3139
{
    unsigned rb = extract32(insn, 21, 5);
    unsigned rt = extract32(insn, 16, 5);
3140
    unsigned sp = extract32(insn, 14, 2);
3141
    target_sreg i = assemble_16a(insn);
3142 3143 3144 3145 3146 3147
    unsigned ext2 = extract32(insn, 1, 2);

    switch (ext2) {
    case 0:
    case 1:
        /* FLDW without modification.  */
3148
        return do_floadw(ctx, ext2 * 32 + rt, rb, 0, 0, i, sp, 0);
3149 3150 3151
    case 2:
        /* LDW with modification.  Note that the sign of I selects
           post-dec vs pre-inc.  */
3152
        return do_load(ctx, rt, rb, 0, 0, i, sp, (i < 0 ? 1 : -1), MO_TEUL);
3153 3154 3155 3156 3157
    default:
        return gen_illegal(ctx);
    }
}

3158
static DisasJumpType trans_fload_mod(DisasContext *ctx, uint32_t insn)
3159
{
3160
    target_sreg i = assemble_16a(insn);
3161 3162
    unsigned t1 = extract32(insn, 1, 1);
    unsigned a = extract32(insn, 2, 1);
3163
    unsigned sp = extract32(insn, 14, 2);
3164 3165 3166 3167
    unsigned t0 = extract32(insn, 16, 5);
    unsigned rb = extract32(insn, 21, 5);

    /* FLDW with modification.  */
3168
    return do_floadw(ctx, t1 * 32 + t0, rb, 0, 0, i, sp, (a ? -1 : 1));
3169 3170
}

3171 3172
static DisasJumpType trans_store(DisasContext *ctx, uint32_t insn,
                                 bool is_mod, TCGMemOp mop)
3173 3174 3175
{
    unsigned rb = extract32(insn, 21, 5);
    unsigned rt = extract32(insn, 16, 5);
3176
    unsigned sp = extract32(insn, 14, 2);
3177
    target_sreg i = assemble_16(insn);
3178

3179
    return do_store(ctx, rt, rb, i, sp, is_mod ? (i < 0 ? -1 : 1) : 0, mop);
3180 3181
}

3182
static DisasJumpType trans_store_w(DisasContext *ctx, uint32_t insn)
3183 3184 3185
{
    unsigned rb = extract32(insn, 21, 5);
    unsigned rt = extract32(insn, 16, 5);
3186
    unsigned sp = extract32(insn, 14, 2);
3187
    target_sreg i = assemble_16a(insn);
3188 3189 3190 3191 3192 3193
    unsigned ext2 = extract32(insn, 1, 2);

    switch (ext2) {
    case 0:
    case 1:
        /* FSTW without modification.  */
3194
        return do_fstorew(ctx, ext2 * 32 + rt, rb, 0, 0, i, sp, 0);
3195 3196
    case 2:
        /* LDW with modification.  */
3197
        return do_store(ctx, rt, rb, i, sp, (i < 0 ? 1 : -1), MO_TEUL);
3198 3199 3200 3201 3202
    default:
        return gen_illegal(ctx);
    }
}

3203
static DisasJumpType trans_fstore_mod(DisasContext *ctx, uint32_t insn)
3204
{
3205
    target_sreg i = assemble_16a(insn);
3206 3207
    unsigned t1 = extract32(insn, 1, 1);
    unsigned a = extract32(insn, 2, 1);
3208
    unsigned sp = extract32(insn, 14, 2);
3209 3210 3211 3212
    unsigned t0 = extract32(insn, 16, 5);
    unsigned rb = extract32(insn, 21, 5);

    /* FSTW with modification.  */
3213
    return do_fstorew(ctx, t1 * 32 + t0, rb, 0, 0, i, sp, (a ? -1 : 1));
3214 3215
}

3216
static DisasJumpType trans_copr_w(DisasContext *ctx, uint32_t insn)
3217 3218 3219 3220 3221 3222 3223 3224
{
    unsigned t0 = extract32(insn, 0, 5);
    unsigned m = extract32(insn, 5, 1);
    unsigned t1 = extract32(insn, 6, 1);
    unsigned ext3 = extract32(insn, 7, 3);
    /* unsigned cc = extract32(insn, 10, 2); */
    unsigned i = extract32(insn, 12, 1);
    unsigned ua = extract32(insn, 13, 1);
3225
    unsigned sp = extract32(insn, 14, 2);
3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244
    unsigned rx = extract32(insn, 16, 5);
    unsigned rb = extract32(insn, 21, 5);
    unsigned rt = t1 * 32 + t0;
    int modify = (m ? (ua ? -1 : 1) : 0);
    int disp, scale;

    if (i == 0) {
        scale = (ua ? 2 : 0);
        disp = 0;
        modify = m;
    } else {
        disp = low_sextract(rx, 0, 5);
        scale = 0;
        rx = 0;
        modify = (m ? (ua ? -1 : 1) : 0);
    }

    switch (ext3) {
    case 0: /* FLDW */
3245
        return do_floadw(ctx, rt, rb, rx, scale, disp, sp, modify);
3246
    case 4: /* FSTW */
3247
        return do_fstorew(ctx, rt, rb, rx, scale, disp, sp, modify);
3248 3249 3250 3251
    }
    return gen_illegal(ctx);
}

3252
static DisasJumpType trans_copr_dw(DisasContext *ctx, uint32_t insn)
3253 3254 3255 3256 3257 3258 3259
{
    unsigned rt = extract32(insn, 0, 5);
    unsigned m = extract32(insn, 5, 1);
    unsigned ext4 = extract32(insn, 6, 4);
    /* unsigned cc = extract32(insn, 10, 2); */
    unsigned i = extract32(insn, 12, 1);
    unsigned ua = extract32(insn, 13, 1);
3260
    unsigned sp = extract32(insn, 14, 2);
3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278
    unsigned rx = extract32(insn, 16, 5);
    unsigned rb = extract32(insn, 21, 5);
    int modify = (m ? (ua ? -1 : 1) : 0);
    int disp, scale;

    if (i == 0) {
        scale = (ua ? 3 : 0);
        disp = 0;
        modify = m;
    } else {
        disp = low_sextract(rx, 0, 5);
        scale = 0;
        rx = 0;
        modify = (m ? (ua ? -1 : 1) : 0);
    }

    switch (ext4) {
    case 0: /* FLDD */
3279
        return do_floadd(ctx, rt, rb, rx, scale, disp, sp, modify);
3280
    case 8: /* FSTD */
3281
        return do_fstored(ctx, rt, rb, rx, scale, disp, sp, modify);
3282 3283 3284 3285 3286
    default:
        return gen_illegal(ctx);
    }
}

3287 3288
static DisasJumpType trans_cmpb(DisasContext *ctx, uint32_t insn,
                                bool is_true, bool is_imm, bool is_dw)
3289
{
3290
    target_sreg disp = assemble_12(insn) * 4;
3291 3292 3293 3294
    unsigned n = extract32(insn, 1, 1);
    unsigned c = extract32(insn, 13, 3);
    unsigned r = extract32(insn, 21, 5);
    unsigned cf = c * 2 + !is_true;
3295
    TCGv_reg dest, in1, in2, sv;
3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307
    DisasCond cond;

    nullify_over(ctx);

    if (is_imm) {
        in1 = load_const(ctx, low_sextract(insn, 16, 5));
    } else {
        in1 = load_gpr(ctx, extract32(insn, 16, 5));
    }
    in2 = load_gpr(ctx, r);
    dest = get_temp(ctx);

3308
    tcg_gen_sub_reg(dest, in1, in2);
3309

3310
    sv = NULL;
3311 3312 3313 3314 3315 3316 3317 3318
    if (c == 6) {
        sv = do_sub_sv(ctx, dest, in1, in2);
    }

    cond = do_sub_cond(cf, dest, in1, in2, sv);
    return do_cbranch(ctx, disp, n, &cond);
}

3319 3320
static DisasJumpType trans_addb(DisasContext *ctx, uint32_t insn,
                                bool is_true, bool is_imm)
3321
{
3322
    target_sreg disp = assemble_12(insn) * 4;
3323 3324 3325 3326
    unsigned n = extract32(insn, 1, 1);
    unsigned c = extract32(insn, 13, 3);
    unsigned r = extract32(insn, 21, 5);
    unsigned cf = c * 2 + !is_true;
3327
    TCGv_reg dest, in1, in2, sv, cb_msb;
3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338
    DisasCond cond;

    nullify_over(ctx);

    if (is_imm) {
        in1 = load_const(ctx, low_sextract(insn, 16, 5));
    } else {
        in1 = load_gpr(ctx, extract32(insn, 16, 5));
    }
    in2 = load_gpr(ctx, r);
    dest = dest_gpr(ctx, r);
3339 3340
    sv = NULL;
    cb_msb = NULL;
3341 3342 3343

    switch (c) {
    default:
3344
        tcg_gen_add_reg(dest, in1, in2);
3345 3346 3347
        break;
    case 4: case 5:
        cb_msb = get_temp(ctx);
3348 3349
        tcg_gen_movi_reg(cb_msb, 0);
        tcg_gen_add2_reg(dest, cb_msb, in1, cb_msb, in2, cb_msb);
3350 3351
        break;
    case 6:
3352
        tcg_gen_add_reg(dest, in1, in2);
3353 3354 3355 3356 3357 3358 3359 3360
        sv = do_add_sv(ctx, dest, in1, in2);
        break;
    }

    cond = do_cond(cf, dest, cb_msb, sv);
    return do_cbranch(ctx, disp, n, &cond);
}

3361
static DisasJumpType trans_bb(DisasContext *ctx, uint32_t insn)
3362
{
3363
    target_sreg disp = assemble_12(insn) * 4;
3364 3365 3366 3367 3368
    unsigned n = extract32(insn, 1, 1);
    unsigned c = extract32(insn, 15, 1);
    unsigned r = extract32(insn, 16, 5);
    unsigned p = extract32(insn, 21, 5);
    unsigned i = extract32(insn, 26, 1);
3369
    TCGv_reg tmp, tcg_r;
3370 3371 3372 3373 3374 3375 3376
    DisasCond cond;

    nullify_over(ctx);

    tmp = tcg_temp_new();
    tcg_r = load_gpr(ctx, r);
    if (i) {
3377
        tcg_gen_shli_reg(tmp, tcg_r, p);
3378
    } else {
3379
        tcg_gen_shl_reg(tmp, tcg_r, cpu_sar);
3380 3381 3382 3383 3384 3385 3386
    }

    cond = cond_make_0(c ? TCG_COND_GE : TCG_COND_LT, tmp);
    tcg_temp_free(tmp);
    return do_cbranch(ctx, disp, n, &cond);
}

3387
static DisasJumpType trans_movb(DisasContext *ctx, uint32_t insn, bool is_imm)
3388
{
3389
    target_sreg disp = assemble_12(insn) * 4;
3390 3391 3392 3393
    unsigned n = extract32(insn, 1, 1);
    unsigned c = extract32(insn, 13, 3);
    unsigned t = extract32(insn, 16, 5);
    unsigned r = extract32(insn, 21, 5);
3394
    TCGv_reg dest;
3395 3396 3397 3398 3399 3400
    DisasCond cond;

    nullify_over(ctx);

    dest = dest_gpr(ctx, r);
    if (is_imm) {
3401
        tcg_gen_movi_reg(dest, low_sextract(t, 0, 5));
3402
    } else if (t == 0) {
3403
        tcg_gen_movi_reg(dest, 0);
3404
    } else {
3405
        tcg_gen_mov_reg(dest, cpu_gr[t]);
3406 3407 3408 3409 3410 3411
    }

    cond = do_sed_cond(c, dest);
    return do_cbranch(ctx, disp, n, &cond);
}

3412 3413
static DisasJumpType trans_shrpw_sar(DisasContext *ctx, uint32_t insn,
                                    const DisasInsn *di)
3414 3415 3416 3417 3418
{
    unsigned rt = extract32(insn, 0, 5);
    unsigned c = extract32(insn, 13, 3);
    unsigned r1 = extract32(insn, 16, 5);
    unsigned r2 = extract32(insn, 21, 5);
3419
    TCGv_reg dest;
3420 3421 3422 3423 3424 3425 3426

    if (c) {
        nullify_over(ctx);
    }

    dest = dest_gpr(ctx, rt);
    if (r1 == 0) {
3427 3428
        tcg_gen_ext32u_reg(dest, load_gpr(ctx, r2));
        tcg_gen_shr_reg(dest, dest, cpu_sar);
3429 3430
    } else if (r1 == r2) {
        TCGv_i32 t32 = tcg_temp_new_i32();
3431
        tcg_gen_trunc_reg_i32(t32, load_gpr(ctx, r2));
3432
        tcg_gen_rotr_i32(t32, t32, cpu_sar);
3433
        tcg_gen_extu_i32_reg(dest, t32);
3434 3435 3436 3437 3438
        tcg_temp_free_i32(t32);
    } else {
        TCGv_i64 t = tcg_temp_new_i64();
        TCGv_i64 s = tcg_temp_new_i64();

3439 3440
        tcg_gen_concat_reg_i64(t, load_gpr(ctx, r2), load_gpr(ctx, r1));
        tcg_gen_extu_reg_i64(s, cpu_sar);
3441
        tcg_gen_shr_i64(t, t, s);
3442
        tcg_gen_trunc_i64_reg(dest, t);
3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453

        tcg_temp_free_i64(t);
        tcg_temp_free_i64(s);
    }
    save_gpr(ctx, rt, dest);

    /* Install the new nullification.  */
    cond_free(&ctx->null_cond);
    if (c) {
        ctx->null_cond = do_sed_cond(c, dest);
    }
3454
    return nullify_end(ctx, DISAS_NEXT);
3455 3456
}

3457 3458
static DisasJumpType trans_shrpw_imm(DisasContext *ctx, uint32_t insn,
                                     const DisasInsn *di)
3459 3460 3461 3462 3463 3464 3465
{
    unsigned rt = extract32(insn, 0, 5);
    unsigned cpos = extract32(insn, 5, 5);
    unsigned c = extract32(insn, 13, 3);
    unsigned r1 = extract32(insn, 16, 5);
    unsigned r2 = extract32(insn, 21, 5);
    unsigned sa = 31 - cpos;
3466
    TCGv_reg dest, t2;
3467 3468 3469 3470 3471 3472 3473 3474 3475

    if (c) {
        nullify_over(ctx);
    }

    dest = dest_gpr(ctx, rt);
    t2 = load_gpr(ctx, r2);
    if (r1 == r2) {
        TCGv_i32 t32 = tcg_temp_new_i32();
3476
        tcg_gen_trunc_reg_i32(t32, t2);
3477
        tcg_gen_rotri_i32(t32, t32, sa);
3478
        tcg_gen_extu_i32_reg(dest, t32);
3479 3480
        tcg_temp_free_i32(t32);
    } else if (r1 == 0) {
3481
        tcg_gen_extract_reg(dest, t2, sa, 32 - sa);
3482
    } else {
3483 3484 3485
        TCGv_reg t0 = tcg_temp_new();
        tcg_gen_extract_reg(t0, t2, sa, 32 - sa);
        tcg_gen_deposit_reg(dest, t0, cpu_gr[r1], 32 - sa, sa);
3486 3487 3488 3489 3490 3491 3492 3493 3494
        tcg_temp_free(t0);
    }
    save_gpr(ctx, rt, dest);

    /* Install the new nullification.  */
    cond_free(&ctx->null_cond);
    if (c) {
        ctx->null_cond = do_sed_cond(c, dest);
    }
3495
    return nullify_end(ctx, DISAS_NEXT);
3496 3497
}

3498 3499
static DisasJumpType trans_extrw_sar(DisasContext *ctx, uint32_t insn,
                                     const DisasInsn *di)
3500 3501 3502 3503 3504 3505 3506
{
    unsigned clen = extract32(insn, 0, 5);
    unsigned is_se = extract32(insn, 10, 1);
    unsigned c = extract32(insn, 13, 3);
    unsigned rt = extract32(insn, 16, 5);
    unsigned rr = extract32(insn, 21, 5);
    unsigned len = 32 - clen;
3507
    TCGv_reg dest, src, tmp;
3508 3509 3510 3511 3512 3513 3514 3515 3516 3517

    if (c) {
        nullify_over(ctx);
    }

    dest = dest_gpr(ctx, rt);
    src = load_gpr(ctx, rr);
    tmp = tcg_temp_new();

    /* Recall that SAR is using big-endian bit numbering.  */
3518
    tcg_gen_xori_reg(tmp, cpu_sar, TARGET_REGISTER_BITS - 1);
3519
    if (is_se) {
3520 3521
        tcg_gen_sar_reg(dest, src, tmp);
        tcg_gen_sextract_reg(dest, dest, 0, len);
3522
    } else {
3523 3524
        tcg_gen_shr_reg(dest, src, tmp);
        tcg_gen_extract_reg(dest, dest, 0, len);
3525 3526 3527 3528 3529 3530 3531 3532 3533
    }
    tcg_temp_free(tmp);
    save_gpr(ctx, rt, dest);

    /* Install the new nullification.  */
    cond_free(&ctx->null_cond);
    if (c) {
        ctx->null_cond = do_sed_cond(c, dest);
    }
3534
    return nullify_end(ctx, DISAS_NEXT);
3535 3536
}

3537 3538
static DisasJumpType trans_extrw_imm(DisasContext *ctx, uint32_t insn,
                                     const DisasInsn *di)
3539 3540 3541 3542 3543 3544 3545 3546 3547
{
    unsigned clen = extract32(insn, 0, 5);
    unsigned pos = extract32(insn, 5, 5);
    unsigned is_se = extract32(insn, 10, 1);
    unsigned c = extract32(insn, 13, 3);
    unsigned rt = extract32(insn, 16, 5);
    unsigned rr = extract32(insn, 21, 5);
    unsigned len = 32 - clen;
    unsigned cpos = 31 - pos;
3548
    TCGv_reg dest, src;
3549 3550 3551 3552 3553 3554 3555 3556

    if (c) {
        nullify_over(ctx);
    }

    dest = dest_gpr(ctx, rt);
    src = load_gpr(ctx, rr);
    if (is_se) {
3557
        tcg_gen_sextract_reg(dest, src, cpos, len);
3558
    } else {
3559
        tcg_gen_extract_reg(dest, src, cpos, len);
3560 3561 3562 3563 3564 3565 3566 3567
    }
    save_gpr(ctx, rt, dest);

    /* Install the new nullification.  */
    cond_free(&ctx->null_cond);
    if (c) {
        ctx->null_cond = do_sed_cond(c, dest);
    }
3568
    return nullify_end(ctx, DISAS_NEXT);
3569 3570 3571 3572 3573 3574 3575 3576 3577
}

static const DisasInsn table_sh_ex[] = {
    { 0xd0000000u, 0xfc001fe0u, trans_shrpw_sar },
    { 0xd0000800u, 0xfc001c00u, trans_shrpw_imm },
    { 0xd0001000u, 0xfc001be0u, trans_extrw_sar },
    { 0xd0001800u, 0xfc001800u, trans_extrw_imm },
};

3578 3579
static DisasJumpType trans_depw_imm_c(DisasContext *ctx, uint32_t insn,
                                      const DisasInsn *di)
3580 3581 3582 3583 3584
{
    unsigned clen = extract32(insn, 0, 5);
    unsigned cpos = extract32(insn, 5, 5);
    unsigned nz = extract32(insn, 10, 1);
    unsigned c = extract32(insn, 13, 3);
3585
    target_sreg val = low_sextract(insn, 16, 5);
3586 3587
    unsigned rt = extract32(insn, 21, 5);
    unsigned len = 32 - clen;
3588 3589
    target_sreg mask0, mask1;
    TCGv_reg dest;
3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602

    if (c) {
        nullify_over(ctx);
    }
    if (cpos + len > 32) {
        len = 32 - cpos;
    }

    dest = dest_gpr(ctx, rt);
    mask0 = deposit64(0, cpos, len, val);
    mask1 = deposit64(-1, cpos, len, val);

    if (nz) {
3603
        TCGv_reg src = load_gpr(ctx, rt);
3604
        if (mask1 != -1) {
3605
            tcg_gen_andi_reg(dest, src, mask1);
3606 3607
            src = dest;
        }
3608
        tcg_gen_ori_reg(dest, src, mask0);
3609
    } else {
3610
        tcg_gen_movi_reg(dest, mask0);
3611 3612 3613 3614 3615 3616 3617 3618
    }
    save_gpr(ctx, rt, dest);

    /* Install the new nullification.  */
    cond_free(&ctx->null_cond);
    if (c) {
        ctx->null_cond = do_sed_cond(c, dest);
    }
3619
    return nullify_end(ctx, DISAS_NEXT);
3620 3621
}

3622 3623
static DisasJumpType trans_depw_imm(DisasContext *ctx, uint32_t insn,
                                    const DisasInsn *di)
3624 3625 3626 3627 3628 3629 3630 3631 3632
{
    unsigned clen = extract32(insn, 0, 5);
    unsigned cpos = extract32(insn, 5, 5);
    unsigned nz = extract32(insn, 10, 1);
    unsigned c = extract32(insn, 13, 3);
    unsigned rr = extract32(insn, 16, 5);
    unsigned rt = extract32(insn, 21, 5);
    unsigned rs = nz ? rt : 0;
    unsigned len = 32 - clen;
3633
    TCGv_reg dest, val;
3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644

    if (c) {
        nullify_over(ctx);
    }
    if (cpos + len > 32) {
        len = 32 - cpos;
    }

    dest = dest_gpr(ctx, rt);
    val = load_gpr(ctx, rr);
    if (rs == 0) {
3645
        tcg_gen_deposit_z_reg(dest, val, cpos, len);
3646
    } else {
3647
        tcg_gen_deposit_reg(dest, cpu_gr[rs], val, cpos, len);
3648 3649 3650 3651 3652 3653 3654 3655
    }
    save_gpr(ctx, rt, dest);

    /* Install the new nullification.  */
    cond_free(&ctx->null_cond);
    if (c) {
        ctx->null_cond = do_sed_cond(c, dest);
    }
3656
    return nullify_end(ctx, DISAS_NEXT);
3657 3658
}

3659 3660
static DisasJumpType trans_depw_sar(DisasContext *ctx, uint32_t insn,
                                    const DisasInsn *di)
3661 3662 3663 3664 3665 3666 3667 3668
{
    unsigned clen = extract32(insn, 0, 5);
    unsigned nz = extract32(insn, 10, 1);
    unsigned i = extract32(insn, 12, 1);
    unsigned c = extract32(insn, 13, 3);
    unsigned rt = extract32(insn, 21, 5);
    unsigned rs = nz ? rt : 0;
    unsigned len = 32 - clen;
3669
    TCGv_reg val, mask, tmp, shift, dest;
3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685
    unsigned msb = 1U << (len - 1);

    if (c) {
        nullify_over(ctx);
    }

    if (i) {
        val = load_const(ctx, low_sextract(insn, 16, 5));
    } else {
        val = load_gpr(ctx, extract32(insn, 16, 5));
    }
    dest = dest_gpr(ctx, rt);
    shift = tcg_temp_new();
    tmp = tcg_temp_new();

    /* Convert big-endian bit numbering in SAR to left-shift.  */
3686
    tcg_gen_xori_reg(shift, cpu_sar, TARGET_REGISTER_BITS - 1);
3687

3688 3689
    mask = tcg_const_reg(msb + (msb - 1));
    tcg_gen_and_reg(tmp, val, mask);
3690
    if (rs) {
3691 3692 3693 3694
        tcg_gen_shl_reg(mask, mask, shift);
        tcg_gen_shl_reg(tmp, tmp, shift);
        tcg_gen_andc_reg(dest, cpu_gr[rs], mask);
        tcg_gen_or_reg(dest, dest, tmp);
3695
    } else {
3696
        tcg_gen_shl_reg(dest, tmp, shift);
3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707
    }
    tcg_temp_free(shift);
    tcg_temp_free(mask);
    tcg_temp_free(tmp);
    save_gpr(ctx, rt, dest);

    /* Install the new nullification.  */
    cond_free(&ctx->null_cond);
    if (c) {
        ctx->null_cond = do_sed_cond(c, dest);
    }
3708
    return nullify_end(ctx, DISAS_NEXT);
3709 3710 3711 3712 3713 3714 3715 3716
}

static const DisasInsn table_depw[] = {
    { 0xd4000000u, 0xfc000be0u, trans_depw_sar },
    { 0xd4000800u, 0xfc001800u, trans_depw_imm },
    { 0xd4001800u, 0xfc001800u, trans_depw_imm_c },
};

3717
static DisasJumpType trans_be(DisasContext *ctx, uint32_t insn, bool is_l)
3718 3719 3720
{
    unsigned n = extract32(insn, 1, 1);
    unsigned b = extract32(insn, 21, 5);
3721
    target_sreg disp = assemble_17(insn);
3722
    TCGv_reg tmp;
3723

R
Richard Henderson 已提交
3724
#ifdef CONFIG_USER_ONLY
3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735
    /* ??? It seems like there should be a good way of using
       "be disp(sr2, r0)", the canonical gateway entry mechanism
       to our advantage.  But that appears to be inconvenient to
       manage along side branch delay slots.  Therefore we handle
       entry into the gateway page via absolute address.  */
    /* Since we don't implement spaces, just branch.  Do notice the special
       case of "be disp(*,r0)" using a direct branch to disp, so that we can
       goto_tb to the TB containing the syscall.  */
    if (b == 0) {
        return do_dbranch(ctx, disp, is_l ? 31 : 0, n);
    }
R
Richard Henderson 已提交
3736 3737 3738
#else
    int sp = assemble_sr3(insn);
    nullify_over(ctx);
3739 3740 3741 3742 3743
#endif

    tmp = get_temp(ctx);
    tcg_gen_addi_reg(tmp, load_gpr(ctx, b), disp);
    tmp = do_ibranch_priv(ctx, tmp);
R
Richard Henderson 已提交
3744 3745

#ifdef CONFIG_USER_ONLY
3746
    return do_ibranch(ctx, tmp, is_l ? 31 : 0, n);
R
Richard Henderson 已提交
3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772
#else
    TCGv_i64 new_spc = tcg_temp_new_i64();

    load_spr(ctx, new_spc, sp);
    if (is_l) {
        copy_iaoq_entry(cpu_gr[31], ctx->iaoq_n, ctx->iaoq_n_var);
        tcg_gen_mov_i64(cpu_sr[0], cpu_iasq_f);
    }
    if (n && use_nullify_skip(ctx)) {
        tcg_gen_mov_reg(cpu_iaoq_f, tmp);
        tcg_gen_addi_reg(cpu_iaoq_b, cpu_iaoq_f, 4);
        tcg_gen_mov_i64(cpu_iasq_f, new_spc);
        tcg_gen_mov_i64(cpu_iasq_b, cpu_iasq_f);
    } else {
        copy_iaoq_entry(cpu_iaoq_f, ctx->iaoq_b, cpu_iaoq_b);
        if (ctx->iaoq_b == -1) {
            tcg_gen_mov_i64(cpu_iasq_f, cpu_iasq_b);
        }
        tcg_gen_mov_reg(cpu_iaoq_b, tmp);
        tcg_gen_mov_i64(cpu_iasq_b, new_spc);
        nullify_set(ctx, n);
    }
    tcg_temp_free_i64(new_spc);
    tcg_gen_lookup_and_goto_ptr();
    return nullify_end(ctx, DISAS_NORETURN);
#endif
3773 3774
}

3775 3776
static DisasJumpType trans_bl(DisasContext *ctx, uint32_t insn,
                              const DisasInsn *di)
3777 3778 3779
{
    unsigned n = extract32(insn, 1, 1);
    unsigned link = extract32(insn, 21, 5);
3780
    target_sreg disp = assemble_17(insn);
3781 3782 3783 3784

    return do_dbranch(ctx, iaoq_dest(ctx, disp), link, n);
}

3785 3786
static DisasJumpType trans_bl_long(DisasContext *ctx, uint32_t insn,
                                   const DisasInsn *di)
3787 3788
{
    unsigned n = extract32(insn, 1, 1);
3789
    target_sreg disp = assemble_22(insn);
3790 3791 3792 3793

    return do_dbranch(ctx, iaoq_dest(ctx, disp), 2, n);
}

3794 3795
static DisasJumpType trans_blr(DisasContext *ctx, uint32_t insn,
                               const DisasInsn *di)
3796 3797 3798 3799
{
    unsigned n = extract32(insn, 1, 1);
    unsigned rx = extract32(insn, 16, 5);
    unsigned link = extract32(insn, 21, 5);
3800
    TCGv_reg tmp = get_temp(ctx);
3801

3802 3803
    tcg_gen_shli_reg(tmp, load_gpr(ctx, rx), 3);
    tcg_gen_addi_reg(tmp, tmp, ctx->iaoq_f + 8);
3804
    /* The computation here never changes privilege level.  */
3805 3806 3807
    return do_ibranch(ctx, tmp, link, n);
}

3808 3809
static DisasJumpType trans_bv(DisasContext *ctx, uint32_t insn,
                              const DisasInsn *di)
3810 3811 3812 3813
{
    unsigned n = extract32(insn, 1, 1);
    unsigned rx = extract32(insn, 16, 5);
    unsigned rb = extract32(insn, 21, 5);
3814
    TCGv_reg dest;
3815 3816 3817 3818 3819

    if (rx == 0) {
        dest = load_gpr(ctx, rb);
    } else {
        dest = get_temp(ctx);
3820 3821
        tcg_gen_shli_reg(dest, load_gpr(ctx, rx), 3);
        tcg_gen_add_reg(dest, dest, load_gpr(ctx, rb));
3822
    }
3823
    dest = do_ibranch_priv(ctx, dest);
3824 3825 3826
    return do_ibranch(ctx, dest, 0, n);
}

3827 3828
static DisasJumpType trans_bve(DisasContext *ctx, uint32_t insn,
                               const DisasInsn *di)
3829 3830 3831 3832
{
    unsigned n = extract32(insn, 1, 1);
    unsigned rb = extract32(insn, 21, 5);
    unsigned link = extract32(insn, 13, 1) ? 2 : 0;
3833
    TCGv_reg dest;
3834

R
Richard Henderson 已提交
3835
#ifdef CONFIG_USER_ONLY
3836 3837
    dest = do_ibranch_priv(ctx, load_gpr(ctx, rb));
    return do_ibranch(ctx, dest, link, n);
R
Richard Henderson 已提交
3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854
#else
    nullify_over(ctx);
    dest = do_ibranch_priv(ctx, load_gpr(ctx, rb));

    copy_iaoq_entry(cpu_iaoq_f, ctx->iaoq_b, cpu_iaoq_b);
    if (ctx->iaoq_b == -1) {
        tcg_gen_mov_i64(cpu_iasq_f, cpu_iasq_b);
    }
    copy_iaoq_entry(cpu_iaoq_b, -1, dest);
    tcg_gen_mov_i64(cpu_iasq_b, space_select(ctx, 0, dest));
    if (link) {
        copy_iaoq_entry(cpu_gr[link], ctx->iaoq_n, ctx->iaoq_n_var);
    }
    nullify_set(ctx, n);
    tcg_gen_lookup_and_goto_ptr();
    return nullify_end(ctx, DISAS_NORETURN);
#endif
3855 3856 3857 3858 3859 3860 3861 3862 3863 3864
}

static const DisasInsn table_branch[] = {
    { 0xe8000000u, 0xfc006000u, trans_bl }, /* B,L and B,L,PUSH */
    { 0xe800a000u, 0xfc00e000u, trans_bl_long },
    { 0xe8004000u, 0xfc00fffdu, trans_blr },
    { 0xe800c000u, 0xfc00fffdu, trans_bv },
    { 0xe800d000u, 0xfc00dffcu, trans_bve },
};

3865 3866
static DisasJumpType trans_fop_wew_0c(DisasContext *ctx, uint32_t insn,
                                      const DisasInsn *di)
3867 3868 3869
{
    unsigned rt = extract32(insn, 0, 5);
    unsigned ra = extract32(insn, 21, 5);
3870
    return do_fop_wew(ctx, rt, ra, di->f.wew);
3871 3872
}

3873 3874
static DisasJumpType trans_fop_wew_0e(DisasContext *ctx, uint32_t insn,
                                      const DisasInsn *di)
3875 3876 3877
{
    unsigned rt = assemble_rt64(insn);
    unsigned ra = assemble_ra64(insn);
3878
    return do_fop_wew(ctx, rt, ra, di->f.wew);
3879 3880
}

3881 3882
static DisasJumpType trans_fop_ded(DisasContext *ctx, uint32_t insn,
                                   const DisasInsn *di)
3883 3884 3885
{
    unsigned rt = extract32(insn, 0, 5);
    unsigned ra = extract32(insn, 21, 5);
3886
    return do_fop_ded(ctx, rt, ra, di->f.ded);
3887 3888
}

3889 3890
static DisasJumpType trans_fop_wed_0c(DisasContext *ctx, uint32_t insn,
                                      const DisasInsn *di)
3891 3892 3893
{
    unsigned rt = extract32(insn, 0, 5);
    unsigned ra = extract32(insn, 21, 5);
3894
    return do_fop_wed(ctx, rt, ra, di->f.wed);
3895 3896
}

3897 3898
static DisasJumpType trans_fop_wed_0e(DisasContext *ctx, uint32_t insn,
                                      const DisasInsn *di)
3899 3900 3901
{
    unsigned rt = assemble_rt64(insn);
    unsigned ra = extract32(insn, 21, 5);
3902
    return do_fop_wed(ctx, rt, ra, di->f.wed);
3903 3904
}

3905 3906
static DisasJumpType trans_fop_dew_0c(DisasContext *ctx, uint32_t insn,
                                      const DisasInsn *di)
3907 3908 3909
{
    unsigned rt = extract32(insn, 0, 5);
    unsigned ra = extract32(insn, 21, 5);
3910
    return do_fop_dew(ctx, rt, ra, di->f.dew);
3911 3912
}

3913 3914
static DisasJumpType trans_fop_dew_0e(DisasContext *ctx, uint32_t insn,
                                      const DisasInsn *di)
3915 3916 3917
{
    unsigned rt = extract32(insn, 0, 5);
    unsigned ra = assemble_ra64(insn);
3918
    return do_fop_dew(ctx, rt, ra, di->f.dew);
3919 3920
}

3921 3922
static DisasJumpType trans_fop_weww_0c(DisasContext *ctx, uint32_t insn,
                                       const DisasInsn *di)
3923 3924 3925 3926
{
    unsigned rt = extract32(insn, 0, 5);
    unsigned rb = extract32(insn, 16, 5);
    unsigned ra = extract32(insn, 21, 5);
3927
    return do_fop_weww(ctx, rt, ra, rb, di->f.weww);
3928 3929
}

3930 3931
static DisasJumpType trans_fop_weww_0e(DisasContext *ctx, uint32_t insn,
                                       const DisasInsn *di)
3932 3933 3934 3935
{
    unsigned rt = assemble_rt64(insn);
    unsigned rb = assemble_rb64(insn);
    unsigned ra = assemble_ra64(insn);
3936
    return do_fop_weww(ctx, rt, ra, rb, di->f.weww);
3937 3938
}

3939 3940
static DisasJumpType trans_fop_dedd(DisasContext *ctx, uint32_t insn,
                                    const DisasInsn *di)
3941 3942 3943 3944
{
    unsigned rt = extract32(insn, 0, 5);
    unsigned rb = extract32(insn, 16, 5);
    unsigned ra = extract32(insn, 21, 5);
3945
    return do_fop_dedd(ctx, rt, ra, rb, di->f.dedd);
3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987
}

static void gen_fcpy_s(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
{
    tcg_gen_mov_i32(dst, src);
}

static void gen_fcpy_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
{
    tcg_gen_mov_i64(dst, src);
}

static void gen_fabs_s(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
{
    tcg_gen_andi_i32(dst, src, INT32_MAX);
}

static void gen_fabs_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
{
    tcg_gen_andi_i64(dst, src, INT64_MAX);
}

static void gen_fneg_s(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
{
    tcg_gen_xori_i32(dst, src, INT32_MIN);
}

static void gen_fneg_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
{
    tcg_gen_xori_i64(dst, src, INT64_MIN);
}

static void gen_fnegabs_s(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
{
    tcg_gen_ori_i32(dst, src, INT32_MIN);
}

static void gen_fnegabs_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
{
    tcg_gen_ori_i64(dst, src, INT64_MIN);
}

3988 3989
static DisasJumpType do_fcmp_s(DisasContext *ctx, unsigned ra, unsigned rb,
                               unsigned y, unsigned c)
3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006
{
    TCGv_i32 ta, tb, tc, ty;

    nullify_over(ctx);

    ta = load_frw0_i32(ra);
    tb = load_frw0_i32(rb);
    ty = tcg_const_i32(y);
    tc = tcg_const_i32(c);

    gen_helper_fcmp_s(cpu_env, ta, tb, ty, tc);

    tcg_temp_free_i32(ta);
    tcg_temp_free_i32(tb);
    tcg_temp_free_i32(ty);
    tcg_temp_free_i32(tc);

4007
    return nullify_end(ctx, DISAS_NEXT);
4008 4009
}

4010 4011
static DisasJumpType trans_fcmp_s_0c(DisasContext *ctx, uint32_t insn,
                                     const DisasInsn *di)
4012 4013 4014 4015 4016 4017 4018 4019
{
    unsigned c = extract32(insn, 0, 5);
    unsigned y = extract32(insn, 13, 3);
    unsigned rb = extract32(insn, 16, 5);
    unsigned ra = extract32(insn, 21, 5);
    return do_fcmp_s(ctx, ra, rb, y, c);
}

4020 4021
static DisasJumpType trans_fcmp_s_0e(DisasContext *ctx, uint32_t insn,
                                     const DisasInsn *di)
4022 4023 4024 4025 4026 4027 4028 4029
{
    unsigned c = extract32(insn, 0, 5);
    unsigned y = extract32(insn, 13, 3);
    unsigned rb = assemble_rb64(insn);
    unsigned ra = assemble_ra64(insn);
    return do_fcmp_s(ctx, ra, rb, y, c);
}

4030 4031
static DisasJumpType trans_fcmp_d(DisasContext *ctx, uint32_t insn,
                                  const DisasInsn *di)
4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053
{
    unsigned c = extract32(insn, 0, 5);
    unsigned y = extract32(insn, 13, 3);
    unsigned rb = extract32(insn, 16, 5);
    unsigned ra = extract32(insn, 21, 5);
    TCGv_i64 ta, tb;
    TCGv_i32 tc, ty;

    nullify_over(ctx);

    ta = load_frd0(ra);
    tb = load_frd0(rb);
    ty = tcg_const_i32(y);
    tc = tcg_const_i32(c);

    gen_helper_fcmp_d(cpu_env, ta, tb, ty, tc);

    tcg_temp_free_i64(ta);
    tcg_temp_free_i64(tb);
    tcg_temp_free_i32(ty);
    tcg_temp_free_i32(tc);

4054
    return nullify_end(ctx, DISAS_NEXT);
4055 4056
}

4057 4058
static DisasJumpType trans_ftest_t(DisasContext *ctx, uint32_t insn,
                                   const DisasInsn *di)
4059 4060 4061
{
    unsigned y = extract32(insn, 13, 3);
    unsigned cbit = (y ^ 1) - 1;
4062
    TCGv_reg t;
4063 4064 4065 4066

    nullify_over(ctx);

    t = tcg_temp_new();
4067 4068
    tcg_gen_ld32u_reg(t, cpu_env, offsetof(CPUHPPAState, fr0_shadow));
    tcg_gen_extract_reg(t, t, 21 - cbit, 1);
4069 4070 4071
    ctx->null_cond = cond_make_0(TCG_COND_NE, t);
    tcg_temp_free(t);

4072
    return nullify_end(ctx, DISAS_NEXT);
4073 4074
}

4075 4076
static DisasJumpType trans_ftest_q(DisasContext *ctx, uint32_t insn,
                                   const DisasInsn *di)
4077 4078 4079 4080
{
    unsigned c = extract32(insn, 0, 5);
    int mask;
    bool inv = false;
4081
    TCGv_reg t;
4082 4083 4084 4085

    nullify_over(ctx);

    t = tcg_temp_new();
4086
    tcg_gen_ld32u_reg(t, cpu_env, offsetof(CPUHPPAState, fr0_shadow));
4087 4088 4089

    switch (c) {
    case 0: /* simple */
4090
        tcg_gen_andi_reg(t, t, 0x4000000);
4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117
        ctx->null_cond = cond_make_0(TCG_COND_NE, t);
        goto done;
    case 2: /* rej */
        inv = true;
        /* fallthru */
    case 1: /* acc */
        mask = 0x43ff800;
        break;
    case 6: /* rej8 */
        inv = true;
        /* fallthru */
    case 5: /* acc8 */
        mask = 0x43f8000;
        break;
    case 9: /* acc6 */
        mask = 0x43e0000;
        break;
    case 13: /* acc4 */
        mask = 0x4380000;
        break;
    case 17: /* acc2 */
        mask = 0x4200000;
        break;
    default:
        return gen_illegal(ctx);
    }
    if (inv) {
4118 4119
        TCGv_reg c = load_const(ctx, mask);
        tcg_gen_or_reg(t, t, c);
4120 4121
        ctx->null_cond = cond_make(TCG_COND_EQ, t, c);
    } else {
4122
        tcg_gen_andi_reg(t, t, mask);
4123 4124 4125
        ctx->null_cond = cond_make_0(TCG_COND_EQ, t);
    }
 done:
4126
    return nullify_end(ctx, DISAS_NEXT);
4127 4128
}

4129 4130
static DisasJumpType trans_xmpyu(DisasContext *ctx, uint32_t insn,
                                 const DisasInsn *di)
4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145
{
    unsigned rt = extract32(insn, 0, 5);
    unsigned rb = assemble_rb64(insn);
    unsigned ra = assemble_ra64(insn);
    TCGv_i64 a, b;

    nullify_over(ctx);

    a = load_frw0_i64(ra);
    b = load_frw0_i64(rb);
    tcg_gen_mul_i64(a, a, b);
    save_frd(rt, a);
    tcg_temp_free_i64(a);
    tcg_temp_free_i64(b);

4146
    return nullify_end(ctx, DISAS_NEXT);
4147 4148
}

4149 4150
#define FOP_DED  trans_fop_ded, .f.ded
#define FOP_DEDD trans_fop_dedd, .f.dedd
4151

4152 4153 4154 4155
#define FOP_WEW  trans_fop_wew_0c, .f.wew
#define FOP_DEW  trans_fop_dew_0c, .f.dew
#define FOP_WED  trans_fop_wed_0c, .f.wed
#define FOP_WEWW trans_fop_weww_0c, .f.weww
4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233

static const DisasInsn table_float_0c[] = {
    /* floating point class zero */
    { 0x30004000, 0xfc1fffe0, FOP_WEW = gen_fcpy_s },
    { 0x30006000, 0xfc1fffe0, FOP_WEW = gen_fabs_s },
    { 0x30008000, 0xfc1fffe0, FOP_WEW = gen_helper_fsqrt_s },
    { 0x3000a000, 0xfc1fffe0, FOP_WEW = gen_helper_frnd_s },
    { 0x3000c000, 0xfc1fffe0, FOP_WEW = gen_fneg_s },
    { 0x3000e000, 0xfc1fffe0, FOP_WEW = gen_fnegabs_s },

    { 0x30004800, 0xfc1fffe0, FOP_DED = gen_fcpy_d },
    { 0x30006800, 0xfc1fffe0, FOP_DED = gen_fabs_d },
    { 0x30008800, 0xfc1fffe0, FOP_DED = gen_helper_fsqrt_d },
    { 0x3000a800, 0xfc1fffe0, FOP_DED = gen_helper_frnd_d },
    { 0x3000c800, 0xfc1fffe0, FOP_DED = gen_fneg_d },
    { 0x3000e800, 0xfc1fffe0, FOP_DED = gen_fnegabs_d },

    /* floating point class three */
    { 0x30000600, 0xfc00ffe0, FOP_WEWW = gen_helper_fadd_s },
    { 0x30002600, 0xfc00ffe0, FOP_WEWW = gen_helper_fsub_s },
    { 0x30004600, 0xfc00ffe0, FOP_WEWW = gen_helper_fmpy_s },
    { 0x30006600, 0xfc00ffe0, FOP_WEWW = gen_helper_fdiv_s },

    { 0x30000e00, 0xfc00ffe0, FOP_DEDD = gen_helper_fadd_d },
    { 0x30002e00, 0xfc00ffe0, FOP_DEDD = gen_helper_fsub_d },
    { 0x30004e00, 0xfc00ffe0, FOP_DEDD = gen_helper_fmpy_d },
    { 0x30006e00, 0xfc00ffe0, FOP_DEDD = gen_helper_fdiv_d },

    /* floating point class one */
    /* float/float */
    { 0x30000a00, 0xfc1fffe0, FOP_WED = gen_helper_fcnv_d_s },
    { 0x30002200, 0xfc1fffe0, FOP_DEW = gen_helper_fcnv_s_d },
    /* int/float */
    { 0x30008200, 0xfc1fffe0, FOP_WEW = gen_helper_fcnv_w_s },
    { 0x30008a00, 0xfc1fffe0, FOP_WED = gen_helper_fcnv_dw_s },
    { 0x3000a200, 0xfc1fffe0, FOP_DEW = gen_helper_fcnv_w_d },
    { 0x3000aa00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_dw_d },
    /* float/int */
    { 0x30010200, 0xfc1fffe0, FOP_WEW = gen_helper_fcnv_s_w },
    { 0x30010a00, 0xfc1fffe0, FOP_WED = gen_helper_fcnv_d_w },
    { 0x30012200, 0xfc1fffe0, FOP_DEW = gen_helper_fcnv_s_dw },
    { 0x30012a00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_d_dw },
    /* float/int truncate */
    { 0x30018200, 0xfc1fffe0, FOP_WEW = gen_helper_fcnv_t_s_w },
    { 0x30018a00, 0xfc1fffe0, FOP_WED = gen_helper_fcnv_t_d_w },
    { 0x3001a200, 0xfc1fffe0, FOP_DEW = gen_helper_fcnv_t_s_dw },
    { 0x3001aa00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_t_d_dw },
    /* uint/float */
    { 0x30028200, 0xfc1fffe0, FOP_WEW = gen_helper_fcnv_uw_s },
    { 0x30028a00, 0xfc1fffe0, FOP_WED = gen_helper_fcnv_udw_s },
    { 0x3002a200, 0xfc1fffe0, FOP_DEW = gen_helper_fcnv_uw_d },
    { 0x3002aa00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_udw_d },
    /* float/uint */
    { 0x30030200, 0xfc1fffe0, FOP_WEW = gen_helper_fcnv_s_uw },
    { 0x30030a00, 0xfc1fffe0, FOP_WED = gen_helper_fcnv_d_uw },
    { 0x30032200, 0xfc1fffe0, FOP_DEW = gen_helper_fcnv_s_udw },
    { 0x30032a00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_d_udw },
    /* float/uint truncate */
    { 0x30038200, 0xfc1fffe0, FOP_WEW = gen_helper_fcnv_t_s_uw },
    { 0x30038a00, 0xfc1fffe0, FOP_WED = gen_helper_fcnv_t_d_uw },
    { 0x3003a200, 0xfc1fffe0, FOP_DEW = gen_helper_fcnv_t_s_udw },
    { 0x3003aa00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_t_d_udw },

    /* floating point class two */
    { 0x30000400, 0xfc001fe0, trans_fcmp_s_0c },
    { 0x30000c00, 0xfc001fe0, trans_fcmp_d },
    { 0x30002420, 0xffffffe0, trans_ftest_q },
    { 0x30000420, 0xffff1fff, trans_ftest_t },

    /* FID.  Note that ra == rt == 0, which via fcpy puts 0 into fr0.
       This is machine/revision == 0, which is reserved for simulator.  */
    { 0x30000000, 0xffffffff, FOP_WEW = gen_fcpy_s },
};

#undef FOP_WEW
#undef FOP_DEW
#undef FOP_WED
#undef FOP_WEWW
4234 4235 4236 4237
#define FOP_WEW  trans_fop_wew_0e, .f.wew
#define FOP_DEW  trans_fop_dew_0e, .f.dew
#define FOP_WED  trans_fop_wed_0e, .f.wed
#define FOP_WEWW trans_fop_weww_0e, .f.weww
4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320

static const DisasInsn table_float_0e[] = {
    /* floating point class zero */
    { 0x38004000, 0xfc1fff20, FOP_WEW = gen_fcpy_s },
    { 0x38006000, 0xfc1fff20, FOP_WEW = gen_fabs_s },
    { 0x38008000, 0xfc1fff20, FOP_WEW = gen_helper_fsqrt_s },
    { 0x3800a000, 0xfc1fff20, FOP_WEW = gen_helper_frnd_s },
    { 0x3800c000, 0xfc1fff20, FOP_WEW = gen_fneg_s },
    { 0x3800e000, 0xfc1fff20, FOP_WEW = gen_fnegabs_s },

    { 0x38004800, 0xfc1fffe0, FOP_DED = gen_fcpy_d },
    { 0x38006800, 0xfc1fffe0, FOP_DED = gen_fabs_d },
    { 0x38008800, 0xfc1fffe0, FOP_DED = gen_helper_fsqrt_d },
    { 0x3800a800, 0xfc1fffe0, FOP_DED = gen_helper_frnd_d },
    { 0x3800c800, 0xfc1fffe0, FOP_DED = gen_fneg_d },
    { 0x3800e800, 0xfc1fffe0, FOP_DED = gen_fnegabs_d },

    /* floating point class three */
    { 0x38000600, 0xfc00ef20, FOP_WEWW = gen_helper_fadd_s },
    { 0x38002600, 0xfc00ef20, FOP_WEWW = gen_helper_fsub_s },
    { 0x38004600, 0xfc00ef20, FOP_WEWW = gen_helper_fmpy_s },
    { 0x38006600, 0xfc00ef20, FOP_WEWW = gen_helper_fdiv_s },

    { 0x38000e00, 0xfc00ffe0, FOP_DEDD = gen_helper_fadd_d },
    { 0x38002e00, 0xfc00ffe0, FOP_DEDD = gen_helper_fsub_d },
    { 0x38004e00, 0xfc00ffe0, FOP_DEDD = gen_helper_fmpy_d },
    { 0x38006e00, 0xfc00ffe0, FOP_DEDD = gen_helper_fdiv_d },

    { 0x38004700, 0xfc00ef60, trans_xmpyu },

    /* floating point class one */
    /* float/float */
    { 0x38000a00, 0xfc1fffa0, FOP_WED = gen_helper_fcnv_d_s },
    { 0x38002200, 0xfc1fffc0, FOP_DEW = gen_helper_fcnv_s_d },
    /* int/float */
    { 0x38008200, 0xfc1ffe60, FOP_WEW = gen_helper_fcnv_w_s },
    { 0x38008a00, 0xfc1fffa0, FOP_WED = gen_helper_fcnv_dw_s },
    { 0x3800a200, 0xfc1fff60, FOP_DEW = gen_helper_fcnv_w_d },
    { 0x3800aa00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_dw_d },
    /* float/int */
    { 0x38010200, 0xfc1ffe60, FOP_WEW = gen_helper_fcnv_s_w },
    { 0x38010a00, 0xfc1fffa0, FOP_WED = gen_helper_fcnv_d_w },
    { 0x38012200, 0xfc1fff60, FOP_DEW = gen_helper_fcnv_s_dw },
    { 0x38012a00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_d_dw },
    /* float/int truncate */
    { 0x38018200, 0xfc1ffe60, FOP_WEW = gen_helper_fcnv_t_s_w },
    { 0x38018a00, 0xfc1fffa0, FOP_WED = gen_helper_fcnv_t_d_w },
    { 0x3801a200, 0xfc1fff60, FOP_DEW = gen_helper_fcnv_t_s_dw },
    { 0x3801aa00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_t_d_dw },
    /* uint/float */
    { 0x38028200, 0xfc1ffe60, FOP_WEW = gen_helper_fcnv_uw_s },
    { 0x38028a00, 0xfc1fffa0, FOP_WED = gen_helper_fcnv_udw_s },
    { 0x3802a200, 0xfc1fff60, FOP_DEW = gen_helper_fcnv_uw_d },
    { 0x3802aa00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_udw_d },
    /* float/uint */
    { 0x38030200, 0xfc1ffe60, FOP_WEW = gen_helper_fcnv_s_uw },
    { 0x38030a00, 0xfc1fffa0, FOP_WED = gen_helper_fcnv_d_uw },
    { 0x38032200, 0xfc1fff60, FOP_DEW = gen_helper_fcnv_s_udw },
    { 0x38032a00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_d_udw },
    /* float/uint truncate */
    { 0x38038200, 0xfc1ffe60, FOP_WEW = gen_helper_fcnv_t_s_uw },
    { 0x38038a00, 0xfc1fffa0, FOP_WED = gen_helper_fcnv_t_d_uw },
    { 0x3803a200, 0xfc1fff60, FOP_DEW = gen_helper_fcnv_t_s_udw },
    { 0x3803aa00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_t_d_udw },

    /* floating point class two */
    { 0x38000400, 0xfc000f60, trans_fcmp_s_0e },
    { 0x38000c00, 0xfc001fe0, trans_fcmp_d },
};

#undef FOP_WEW
#undef FOP_DEW
#undef FOP_WED
#undef FOP_WEWW
#undef FOP_DED
#undef FOP_DEDD

/* Convert the fmpyadd single-precision register encodings to standard.  */
static inline int fmpyadd_s_reg(unsigned r)
{
    return (r & 16) * 2 + 16 + (r & 15);
}

4321 4322
static DisasJumpType trans_fmpyadd(DisasContext *ctx,
                                   uint32_t insn, bool is_sub)
4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349
{
    unsigned tm = extract32(insn, 0, 5);
    unsigned f = extract32(insn, 5, 1);
    unsigned ra = extract32(insn, 6, 5);
    unsigned ta = extract32(insn, 11, 5);
    unsigned rm2 = extract32(insn, 16, 5);
    unsigned rm1 = extract32(insn, 21, 5);

    nullify_over(ctx);

    /* Independent multiply & add/sub, with undefined behaviour
       if outputs overlap inputs.  */
    if (f == 0) {
        tm = fmpyadd_s_reg(tm);
        ra = fmpyadd_s_reg(ra);
        ta = fmpyadd_s_reg(ta);
        rm2 = fmpyadd_s_reg(rm2);
        rm1 = fmpyadd_s_reg(rm1);
        do_fop_weww(ctx, tm, rm1, rm2, gen_helper_fmpy_s);
        do_fop_weww(ctx, ta, ta, ra,
                    is_sub ? gen_helper_fsub_s : gen_helper_fadd_s);
    } else {
        do_fop_dedd(ctx, tm, rm1, rm2, gen_helper_fmpy_d);
        do_fop_dedd(ctx, ta, ta, ra,
                    is_sub ? gen_helper_fsub_d : gen_helper_fadd_d);
    }

4350
    return nullify_end(ctx, DISAS_NEXT);
4351 4352
}

4353 4354
static DisasJumpType trans_fmpyfadd_s(DisasContext *ctx, uint32_t insn,
                                      const DisasInsn *di)
4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377
{
    unsigned rt = assemble_rt64(insn);
    unsigned neg = extract32(insn, 5, 1);
    unsigned rm1 = assemble_ra64(insn);
    unsigned rm2 = assemble_rb64(insn);
    unsigned ra3 = assemble_rc64(insn);
    TCGv_i32 a, b, c;

    nullify_over(ctx);
    a = load_frw0_i32(rm1);
    b = load_frw0_i32(rm2);
    c = load_frw0_i32(ra3);

    if (neg) {
        gen_helper_fmpynfadd_s(a, cpu_env, a, b, c);
    } else {
        gen_helper_fmpyfadd_s(a, cpu_env, a, b, c);
    }

    tcg_temp_free_i32(b);
    tcg_temp_free_i32(c);
    save_frw_i32(rt, a);
    tcg_temp_free_i32(a);
4378
    return nullify_end(ctx, DISAS_NEXT);
4379 4380
}

4381 4382
static DisasJumpType trans_fmpyfadd_d(DisasContext *ctx, uint32_t insn,
                                      const DisasInsn *di)
4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405
{
    unsigned rt = extract32(insn, 0, 5);
    unsigned neg = extract32(insn, 5, 1);
    unsigned rm1 = extract32(insn, 21, 5);
    unsigned rm2 = extract32(insn, 16, 5);
    unsigned ra3 = assemble_rc64(insn);
    TCGv_i64 a, b, c;

    nullify_over(ctx);
    a = load_frd0(rm1);
    b = load_frd0(rm2);
    c = load_frd0(ra3);

    if (neg) {
        gen_helper_fmpynfadd_d(a, cpu_env, a, b, c);
    } else {
        gen_helper_fmpyfadd_d(a, cpu_env, a, b, c);
    }

    tcg_temp_free_i64(b);
    tcg_temp_free_i64(c);
    save_frd(rt, a);
    tcg_temp_free_i64(a);
4406
    return nullify_end(ctx, DISAS_NEXT);
4407 4408 4409 4410 4411 4412 4413
}

static const DisasInsn table_fp_fused[] = {
    { 0xb8000000u, 0xfc000800u, trans_fmpyfadd_s },
    { 0xb8000800u, 0xfc0019c0u, trans_fmpyfadd_d }
};

4414 4415
static DisasJumpType translate_table_int(DisasContext *ctx, uint32_t insn,
                                         const DisasInsn table[], size_t n)
4416 4417 4418 4419 4420 4421 4422
{
    size_t i;
    for (i = 0; i < n; ++i) {
        if ((insn & table[i].mask) == table[i].insn) {
            return table[i].trans(ctx, insn, &table[i]);
        }
    }
4423 4424
    qemu_log_mask(LOG_UNIMP, "UNIMP insn %08x @ " TARGET_FMT_lx "\n",
                  insn, ctx->base.pc_next);
4425 4426 4427 4428 4429 4430
    return gen_illegal(ctx);
}

#define translate_table(ctx, insn, table) \
    translate_table_int(ctx, insn, table, ARRAY_SIZE(table))

4431
static DisasJumpType translate_one(DisasContext *ctx, uint32_t insn)
4432 4433 4434 4435
{
    uint32_t opc = extract32(insn, 26, 6);

    switch (opc) {
4436 4437 4438 4439
    case 0x00: /* system op */
        return translate_table(ctx, insn, table_system);
    case 0x01:
        return translate_table(ctx, insn, table_mem_mgmt);
4440 4441
    case 0x02:
        return translate_table(ctx, insn, table_arith_log);
4442 4443
    case 0x03:
        return translate_table(ctx, insn, table_index_mem);
4444 4445
    case 0x06:
        return trans_fmpyadd(ctx, insn, false);
4446 4447
    case 0x08:
        return trans_ldil(ctx, insn);
4448 4449
    case 0x09:
        return trans_copr_w(ctx, insn);
4450 4451
    case 0x0A:
        return trans_addil(ctx, insn);
4452 4453
    case 0x0B:
        return trans_copr_dw(ctx, insn);
4454 4455
    case 0x0C:
        return translate_table(ctx, insn, table_float_0c);
4456 4457
    case 0x0D:
        return trans_ldo(ctx, insn);
4458 4459
    case 0x0E:
        return translate_table(ctx, insn, table_float_0e);
4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485

    case 0x10:
        return trans_load(ctx, insn, false, MO_UB);
    case 0x11:
        return trans_load(ctx, insn, false, MO_TEUW);
    case 0x12:
        return trans_load(ctx, insn, false, MO_TEUL);
    case 0x13:
        return trans_load(ctx, insn, true, MO_TEUL);
    case 0x16:
        return trans_fload_mod(ctx, insn);
    case 0x17:
        return trans_load_w(ctx, insn);
    case 0x18:
        return trans_store(ctx, insn, false, MO_UB);
    case 0x19:
        return trans_store(ctx, insn, false, MO_TEUW);
    case 0x1A:
        return trans_store(ctx, insn, false, MO_TEUL);
    case 0x1B:
        return trans_store(ctx, insn, true, MO_TEUL);
    case 0x1E:
        return trans_fstore_mod(ctx, insn);
    case 0x1F:
        return trans_store_w(ctx, insn);

4486 4487 4488 4489 4490 4491 4492 4493
    case 0x20:
        return trans_cmpb(ctx, insn, true, false, false);
    case 0x21:
        return trans_cmpb(ctx, insn, true, true, false);
    case 0x22:
        return trans_cmpb(ctx, insn, false, false, false);
    case 0x23:
        return trans_cmpb(ctx, insn, false, true, false);
4494 4495 4496 4497
    case 0x24:
        return trans_cmpiclr(ctx, insn);
    case 0x25:
        return trans_subi(ctx, insn);
4498 4499
    case 0x26:
        return trans_fmpyadd(ctx, insn, true);
4500 4501 4502 4503 4504 4505 4506 4507 4508 4509
    case 0x27:
        return trans_cmpb(ctx, insn, true, false, true);
    case 0x28:
        return trans_addb(ctx, insn, true, false);
    case 0x29:
        return trans_addb(ctx, insn, true, true);
    case 0x2A:
        return trans_addb(ctx, insn, false, false);
    case 0x2B:
        return trans_addb(ctx, insn, false, true);
4510 4511 4512
    case 0x2C:
    case 0x2D:
        return trans_addi(ctx, insn);
4513 4514
    case 0x2E:
        return translate_table(ctx, insn, table_fp_fused);
4515 4516
    case 0x2F:
        return trans_cmpb(ctx, insn, false, false, true);
4517

4518 4519 4520 4521 4522 4523 4524
    case 0x30:
    case 0x31:
        return trans_bb(ctx, insn);
    case 0x32:
        return trans_movb(ctx, insn, false);
    case 0x33:
        return trans_movb(ctx, insn, true);
4525 4526 4527 4528
    case 0x34:
        return translate_table(ctx, insn, table_sh_ex);
    case 0x35:
        return translate_table(ctx, insn, table_depw);
4529 4530 4531 4532 4533 4534
    case 0x38:
        return trans_be(ctx, insn, false);
    case 0x39:
        return trans_be(ctx, insn, true);
    case 0x3A:
        return translate_table(ctx, insn, table_branch);
4535 4536 4537 4538 4539 4540 4541 4542 4543 4544

    case 0x04: /* spopn */
    case 0x05: /* diag */
    case 0x0F: /* product specific */
        break;

    case 0x07: /* unassigned */
    case 0x15: /* unassigned */
    case 0x1D: /* unassigned */
    case 0x37: /* unassigned */
4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556
        break;
    case 0x3F:
#ifndef CONFIG_USER_ONLY
        /* Unassigned, but use as system-halt.  */
        if (insn == 0xfffdead0) {
            return gen_hlt(ctx, 0); /* halt system */
        }
        if (insn == 0xfffdead1) {
            return gen_hlt(ctx, 1); /* reset system */
        }
#endif
        break;
4557 4558 4559 4560 4561 4562
    default:
        break;
    }
    return gen_illegal(ctx);
}

4563 4564
static int hppa_tr_init_disas_context(DisasContextBase *dcbase,
                                      CPUState *cs, int max_insns)
4565
{
4566
    DisasContext *ctx = container_of(dcbase, DisasContext, base);
4567
    int bound;
4568

4569
    ctx->cs = cs;
4570
    ctx->tb_flags = ctx->base.tb->flags;
4571 4572 4573 4574

#ifdef CONFIG_USER_ONLY
    ctx->privilege = MMU_USER_IDX;
    ctx->mmu_idx = MMU_USER_IDX;
R
Richard Henderson 已提交
4575 4576
    ctx->iaoq_f = ctx->base.pc_first;
    ctx->iaoq_b = ctx->base.tb->cs_base;
4577
#else
4578 4579
    ctx->privilege = (ctx->tb_flags >> TB_FLAG_PRIV_SHIFT) & 3;
    ctx->mmu_idx = (ctx->tb_flags & PSW_D ? ctx->privilege : MMU_PHYS_IDX);
4580

R
Richard Henderson 已提交
4581 4582 4583 4584 4585 4586 4587 4588
    /* Recover the IAOQ values from the GVA + PRIV.  */
    uint64_t cs_base = ctx->base.tb->cs_base;
    uint64_t iasq_f = cs_base & ~0xffffffffull;
    int32_t diff = cs_base;

    ctx->iaoq_f = (ctx->base.pc_first & ~iasq_f) + ctx->privilege;
    ctx->iaoq_b = (diff ? ctx->iaoq_f + diff : -1);
#endif
4589
    ctx->iaoq_n = -1;
4590
    ctx->iaoq_n_var = NULL;
4591

4592 4593 4594 4595
    /* Bound the number of instructions by those left on the page.  */
    bound = -(ctx->base.pc_first | TARGET_PAGE_MASK) / 4;
    bound = MIN(max_insns, bound);

4596 4597 4598 4599
    ctx->ntempr = 0;
    ctx->ntempl = 0;
    memset(ctx->tempr, 0, sizeof(ctx->tempr));
    memset(ctx->templ, 0, sizeof(ctx->templ));
4600

4601
    return bound;
4602
}
4603

4604 4605 4606
static void hppa_tr_tb_start(DisasContextBase *dcbase, CPUState *cs)
{
    DisasContext *ctx = container_of(dcbase, DisasContext, base);
4607

4608
    /* Seed the nullification status from PSW[N], as saved in TB->FLAGS.  */
4609 4610
    ctx->null_cond = cond_make_f();
    ctx->psw_n_nonzero = false;
4611
    if (ctx->tb_flags & PSW_N) {
4612 4613
        ctx->null_cond.c = TCG_COND_ALWAYS;
        ctx->psw_n_nonzero = true;
4614
    }
4615 4616
    ctx->null_lab = NULL;
}
4617

4618 4619 4620
static void hppa_tr_insn_start(DisasContextBase *dcbase, CPUState *cs)
{
    DisasContext *ctx = container_of(dcbase, DisasContext, base);
4621

4622 4623 4624 4625 4626 4627 4628
    tcg_gen_insn_start(ctx->iaoq_f, ctx->iaoq_b);
}

static bool hppa_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cs,
                                      const CPUBreakpoint *bp)
{
    DisasContext *ctx = container_of(dcbase, DisasContext, base);
4629

4630
    ctx->base.is_jmp = gen_excp(ctx, EXCP_DEBUG);
R
Richard Henderson 已提交
4631
    ctx->base.pc_next += 4;
4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642
    return true;
}

static void hppa_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
{
    DisasContext *ctx = container_of(dcbase, DisasContext, base);
    CPUHPPAState *env = cs->env_ptr;
    DisasJumpType ret;
    int i, n;

    /* Execute one insn.  */
4643
#ifdef CONFIG_USER_ONLY
R
Richard Henderson 已提交
4644
    if (ctx->base.pc_next < TARGET_PAGE_SIZE) {
4645 4646
        ret = do_page_zero(ctx);
        assert(ret != DISAS_NEXT);
4647 4648 4649
    } else
#endif
    {
4650 4651
        /* Always fetch the insn, even if nullified, so that we check
           the page permissions for execute.  */
R
Richard Henderson 已提交
4652
        uint32_t insn = cpu_ldl_code(env, ctx->base.pc_next);
4653 4654 4655 4656 4657 4658

        /* Set up the IA queue for the next insn.
           This will be overwritten by a branch.  */
        if (ctx->iaoq_b == -1) {
            ctx->iaoq_n = -1;
            ctx->iaoq_n_var = get_temp(ctx);
4659
            tcg_gen_addi_reg(ctx->iaoq_n_var, cpu_iaoq_b, 4);
4660
        } else {
4661
            ctx->iaoq_n = ctx->iaoq_b + 4;
4662
            ctx->iaoq_n_var = NULL;
4663 4664
        }

4665 4666 4667 4668
        if (unlikely(ctx->null_cond.c == TCG_COND_ALWAYS)) {
            ctx->null_cond.c = TCG_COND_NEVER;
            ret = DISAS_NEXT;
        } else {
4669
            ctx->insn = insn;
4670 4671
            ret = translate_one(ctx, insn);
            assert(ctx->null_lab == NULL);
4672
        }
4673
    }
4674

4675
    /* Free any temporaries allocated.  */
4676 4677 4678 4679 4680 4681 4682
    for (i = 0, n = ctx->ntempr; i < n; ++i) {
        tcg_temp_free(ctx->tempr[i]);
        ctx->tempr[i] = NULL;
    }
    for (i = 0, n = ctx->ntempl; i < n; ++i) {
        tcg_temp_free_tl(ctx->templ[i]);
        ctx->templ[i] = NULL;
4683
    }
4684 4685
    ctx->ntempr = 0;
    ctx->ntempl = 0;
4686

4687 4688
    /* Advance the insn queue.  Note that this check also detects
       a priority change within the instruction queue.  */
4689
    if (ret == DISAS_NEXT && ctx->iaoq_b != ctx->iaoq_f + 4) {
R
Richard Henderson 已提交
4690 4691 4692 4693
        if (ctx->iaoq_b != -1 && ctx->iaoq_n != -1
            && use_goto_tb(ctx, ctx->iaoq_b)
            && (ctx->null_cond.c == TCG_COND_NEVER
                || ctx->null_cond.c == TCG_COND_ALWAYS)) {
4694 4695 4696 4697 4698
            nullify_set(ctx, ctx->null_cond.c == TCG_COND_ALWAYS);
            gen_goto_tb(ctx, 0, ctx->iaoq_b, ctx->iaoq_n);
            ret = DISAS_NORETURN;
        } else {
            ret = DISAS_IAQ_N_STALE;
R
Richard Henderson 已提交
4699
        }
4700
    }
4701 4702 4703
    ctx->iaoq_f = ctx->iaoq_b;
    ctx->iaoq_b = ctx->iaoq_n;
    ctx->base.is_jmp = ret;
R
Richard Henderson 已提交
4704
    ctx->base.pc_next += 4;
4705 4706 4707 4708 4709

    if (ret == DISAS_NORETURN || ret == DISAS_IAQ_N_UPDATED) {
        return;
    }
    if (ctx->iaoq_f == -1) {
4710
        tcg_gen_mov_reg(cpu_iaoq_f, cpu_iaoq_b);
4711
        copy_iaoq_entry(cpu_iaoq_b, ctx->iaoq_n, ctx->iaoq_n_var);
R
Richard Henderson 已提交
4712 4713 4714
#ifndef CONFIG_USER_ONLY
        tcg_gen_mov_i64(cpu_iasq_f, cpu_iasq_b);
#endif
4715 4716 4717
        nullify_save(ctx);
        ctx->base.is_jmp = DISAS_IAQ_N_UPDATED;
    } else if (ctx->iaoq_b == -1) {
4718
        tcg_gen_mov_reg(cpu_iaoq_b, ctx->iaoq_n_var);
4719 4720 4721 4722 4723 4724
    }
}

static void hppa_tr_tb_stop(DisasContextBase *dcbase, CPUState *cs)
{
    DisasContext *ctx = container_of(dcbase, DisasContext, base);
4725
    DisasJumpType is_jmp = ctx->base.is_jmp;
4726

4727
    switch (is_jmp) {
4728
    case DISAS_NORETURN:
4729
        break;
4730
    case DISAS_TOO_MANY:
4731
    case DISAS_IAQ_N_STALE:
4732
    case DISAS_IAQ_N_STALE_EXIT:
4733 4734 4735
        copy_iaoq_entry(cpu_iaoq_f, ctx->iaoq_f, cpu_iaoq_f);
        copy_iaoq_entry(cpu_iaoq_b, ctx->iaoq_b, cpu_iaoq_b);
        nullify_save(ctx);
4736
        /* FALLTHRU */
4737
    case DISAS_IAQ_N_UPDATED:
4738
        if (ctx->base.singlestep_enabled) {
4739
            gen_excp_1(EXCP_DEBUG);
4740 4741
        } else if (is_jmp == DISAS_IAQ_N_STALE_EXIT) {
            tcg_gen_exit_tb(0);
4742
        } else {
4743
            tcg_gen_lookup_and_goto_ptr();
4744 4745 4746
        }
        break;
    default:
4747
        g_assert_not_reached();
4748
    }
4749
}
4750

4751 4752
static void hppa_tr_disas_log(const DisasContextBase *dcbase, CPUState *cs)
{
R
Richard Henderson 已提交
4753
    target_ulong pc = dcbase->pc_first;
4754

4755 4756
#ifdef CONFIG_USER_ONLY
    switch (pc) {
4757 4758
    case 0x00:
        qemu_log("IN:\n0x00000000:  (null)\n");
4759
        return;
4760 4761
    case 0xb0:
        qemu_log("IN:\n0x000000b0:  light-weight-syscall\n");
4762
        return;
4763 4764
    case 0xe0:
        qemu_log("IN:\n0x000000e0:  set-thread-pointer-syscall\n");
4765
        return;
4766 4767
    case 0x100:
        qemu_log("IN:\n0x00000100:  syscall\n");
4768
        return;
4769
    }
4770 4771 4772
#endif

    qemu_log("IN: %s\n", lookup_symbol(pc));
4773
    log_target_disas(cs, pc, dcbase->tb->size);
4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790
}

static const TranslatorOps hppa_tr_ops = {
    .init_disas_context = hppa_tr_init_disas_context,
    .tb_start           = hppa_tr_tb_start,
    .insn_start         = hppa_tr_insn_start,
    .breakpoint_check   = hppa_tr_breakpoint_check,
    .translate_insn     = hppa_tr_translate_insn,
    .tb_stop            = hppa_tr_tb_stop,
    .disas_log          = hppa_tr_disas_log,
};

void gen_intermediate_code(CPUState *cs, struct TranslationBlock *tb)

{
    DisasContext ctx;
    translator_loop(&hppa_tr_ops, &ctx.base, cs, tb);
4791 4792 4793 4794 4795 4796
}

void restore_state_to_opc(CPUHPPAState *env, TranslationBlock *tb,
                          target_ulong *data)
{
    env->iaoq_f = data[0];
4797
    if (data[1] != (target_ureg)-1) {
4798 4799 4800 4801 4802 4803 4804
        env->iaoq_b = data[1];
    }
    /* Since we were executing the instruction at IAOQ_F, and took some
       sort of action that provoked the cpu_restore_state, we can infer
       that the instruction was not nullified.  */
    env->psw_n = 0;
}