translate.c 86.7 KB
Newer Older
J
j_mayer 已提交
1 2
/*
 *  Alpha emulation cpu translation for qemu.
3
 *
J
j_mayer 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16
 *  Copyright (c) 2007 Jocelyn Mayer
 *
 * 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
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
J
j_mayer 已提交
18 19 20
 */

#include "cpu.h"
21
#include "disas/disas.h"
22
#include "qemu/host-utils.h"
B
bellard 已提交
23
#include "tcg-op.h"
P
Paolo Bonzini 已提交
24
#include "exec/cpu_ldst.h"
J
j_mayer 已提交
25

26 27
#include "exec/helper-proto.h"
#include "exec/helper-gen.h"
P
pbrook 已提交
28

29 30 31
#include "trace-tcg.h"


32
#undef ALPHA_DEBUG_DISAS
33
#define CONFIG_SOFTFLOAT_INLINE
34 35

#ifdef ALPHA_DEBUG_DISAS
R
Richard Henderson 已提交
36
#  define LOG_DISAS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
37 38 39 40
#else
#  define LOG_DISAS(...) do { } while (0)
#endif

J
j_mayer 已提交
41 42
typedef struct DisasContext DisasContext;
struct DisasContext {
43
    struct TranslationBlock *tb;
J
j_mayer 已提交
44
    uint64_t pc;
45 46 47
#ifndef CONFIG_USER_ONLY
    uint64_t palbr;
#endif
J
j_mayer 已提交
48
    int mem_idx;
49 50 51 52 53

    /* Current rounding mode for this TB.  */
    int tb_rm;
    /* Current flush-to-zero setting for this TB.  */
    int tb_ftz;
54

55 56 57
    /* implver value for this CPU.  */
    int implver;

58 59 60
    /* The set of registers active in the current context.  */
    TCGv *ir;

61 62 63 64 65 66
    /* Temporaries for $31 and $f31 as source and destination.  */
    TCGv zero;
    TCGv sink;
    /* Temporary for immediate constants.  */
    TCGv lit;

67
    bool singlestep_enabled;
J
j_mayer 已提交
68 69
};

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
/* Return values from translate_one, indicating the state of the TB.
   Note that zero indicates that we are not exiting the TB.  */

typedef enum {
    NO_EXIT,

    /* We have emitted one or more goto_tb.  No fixup required.  */
    EXIT_GOTO_TB,

    /* We are not using a goto_tb (for whatever reason), but have updated
       the PC (for whatever reason), so there's no need to do it again on
       exiting the TB.  */
    EXIT_PC_UPDATED,

    /* We are exiting the TB, but have neither emitted a goto_tb, nor
       updated the PC for the next instruction to be executed.  */
86 87 88 89 90
    EXIT_PC_STALE,

    /* We are ending the TB with a noreturn function call, e.g. longjmp.
       No following code will be executed.  */
    EXIT_NORETURN,
91 92
} ExitStatus;

A
aurel32 已提交
93
/* global register indexes */
P
pbrook 已提交
94
static TCGv_ptr cpu_env;
95
static TCGv cpu_std_ir[31];
A
aurel32 已提交
96
static TCGv cpu_fir[31];
A
aurel32 已提交
97
static TCGv cpu_pc;
98 99 100
static TCGv cpu_lock_addr;
static TCGv cpu_lock_st_addr;
static TCGv cpu_lock_value;
A
aurel32 已提交
101

102 103 104 105
#ifndef CONFIG_USER_ONLY
static TCGv cpu_pal_ir[31];
#endif

106
#include "exec/gen-icount.h"
P
pbrook 已提交
107

108
void alpha_translate_init(void)
P
pbrook 已提交
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
#define DEF_VAR(V)  { &cpu_##V, #V, offsetof(CPUAlphaState, V) }

    typedef struct { TCGv *var; const char *name; int ofs; } GlobalVar;
    static const GlobalVar vars[] = {
        DEF_VAR(pc),
        DEF_VAR(lock_addr),
        DEF_VAR(lock_st_addr),
        DEF_VAR(lock_value),
    };

#undef DEF_VAR

    /* Use the symbolic register names that match the disassembler.  */
    static const char greg_names[31][4] = {
        "v0", "t0", "t1", "t2", "t3", "t4", "t5", "t6",
        "t7", "s0", "s1", "s2", "s3", "s4", "s5", "fp",
        "a0", "a1", "a2", "a3", "a4", "a5", "t8", "t9",
        "t10", "t11", "ra", "t12", "at", "gp", "sp"
    };
    static const char freg_names[31][4] = {
        "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
        "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
        "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
        "f24", "f25", "f26", "f27", "f28", "f29", "f30"
    };
135 136 137 138 139 140
#ifndef CONFIG_USER_ONLY
    static const char shadow_names[8][8] = {
        "pal_t7", "pal_s0", "pal_s1", "pal_s2",
        "pal_s3", "pal_s4", "pal_s5", "pal_t11"
    };
#endif
141 142

    static bool done_init = 0;
A
aurel32 已提交
143 144
    int i;

P
Paolo Bonzini 已提交
145
    if (done_init) {
P
pbrook 已提交
146
        return;
P
Paolo Bonzini 已提交
147
    }
148
    done_init = 1;
A
aurel32 已提交
149

P
pbrook 已提交
150
    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
A
aurel32 已提交
151 152

    for (i = 0; i < 31; i++) {
153 154 155
        cpu_std_ir[i] = tcg_global_mem_new_i64(TCG_AREG0,
                                               offsetof(CPUAlphaState, ir[i]),
                                               greg_names[i]);
156
    }
A
aurel32 已提交
157

158
    for (i = 0; i < 31; i++) {
P
pbrook 已提交
159
        cpu_fir[i] = tcg_global_mem_new_i64(TCG_AREG0,
160 161
                                            offsetof(CPUAlphaState, fir[i]),
                                            freg_names[i]);
A
aurel32 已提交
162 163
    }

164 165 166 167 168 169 170 171 172 173 174
#ifndef CONFIG_USER_ONLY
    memcpy(cpu_pal_ir, cpu_std_ir, sizeof(cpu_pal_ir));
    for (i = 0; i < 8; i++) {
        int r = (i == 7 ? 25 : i + 8);
        cpu_pal_ir[r] = tcg_global_mem_new_i64(TCG_AREG0,
                                               offsetof(CPUAlphaState,
                                                        shadow[i]),
                                               shadow_names[i]);
    }
#endif

175 176 177 178
    for (i = 0; i < ARRAY_SIZE(vars); ++i) {
        const GlobalVar *v = &vars[i];
        *v->var = tcg_global_mem_new_i64(TCG_AREG0, v->ofs, v->name);
    }
P
pbrook 已提交
179 180
}

181 182 183
static TCGv load_zero(DisasContext *ctx)
{
    if (TCGV_IS_UNUSED_I64(ctx->zero)) {
184
        ctx->zero = tcg_const_i64(0);
185 186 187 188 189 190 191
    }
    return ctx->zero;
}

static TCGv dest_sink(DisasContext *ctx)
{
    if (TCGV_IS_UNUSED_I64(ctx->sink)) {
192
        ctx->sink = tcg_temp_new();
193 194 195 196 197 198 199
    }
    return ctx->sink;
}

static TCGv load_gpr(DisasContext *ctx, unsigned reg)
{
    if (likely(reg < 31)) {
200
        return ctx->ir[reg];
201 202 203 204 205 206 207 208 209 210 211 212
    } else {
        return load_zero(ctx);
    }
}

static TCGv load_gpr_lit(DisasContext *ctx, unsigned reg,
                         uint8_t lit, bool islit)
{
    if (islit) {
        ctx->lit = tcg_const_i64(lit);
        return ctx->lit;
    } else if (likely(reg < 31)) {
213
        return ctx->ir[reg];
214 215 216 217 218 219 220 221
    } else {
        return load_zero(ctx);
    }
}

static TCGv dest_gpr(DisasContext *ctx, unsigned reg)
{
    if (likely(reg < 31)) {
222
        return ctx->ir[reg];
223 224 225 226 227
    } else {
        return dest_sink(ctx);
    }
}

228
static TCGv load_fpr(DisasContext *ctx, unsigned reg)
229 230 231 232 233 234 235 236
{
    if (likely(reg < 31)) {
        return cpu_fir[reg];
    } else {
        return load_zero(ctx);
    }
}

237
static TCGv dest_fpr(DisasContext *ctx, unsigned reg)
238 239 240 241 242 243 244 245
{
    if (likely(reg < 31)) {
        return cpu_fir[reg];
    } else {
        return dest_sink(ctx);
    }
}

246
static void gen_excp_1(int exception, int error_code)
J
j_mayer 已提交
247
{
P
pbrook 已提交
248
    TCGv_i32 tmp1, tmp2;
249 250 251

    tmp1 = tcg_const_i32(exception);
    tmp2 = tcg_const_i32(error_code);
252
    gen_helper_excp(cpu_env, tmp1, tmp2);
P
pbrook 已提交
253 254
    tcg_temp_free_i32(tmp2);
    tcg_temp_free_i32(tmp1);
255
}
256

257 258 259 260
static ExitStatus gen_excp(DisasContext *ctx, int exception, int error_code)
{
    tcg_gen_movi_i64(cpu_pc, ctx->pc);
    gen_excp_1(exception, error_code);
261
    return EXIT_NORETURN;
J
j_mayer 已提交
262 263
}

264
static inline ExitStatus gen_invalid(DisasContext *ctx)
J
j_mayer 已提交
265
{
266
    return gen_excp(ctx, EXCP_OPCDEC, 0);
J
j_mayer 已提交
267 268
}

B
Blue Swirl 已提交
269
static inline void gen_qemu_ldf(TCGv t0, TCGv t1, int flags)
A
aurel32 已提交
270
{
P
pbrook 已提交
271
    TCGv_i32 tmp32 = tcg_temp_new_i32();
272
    tcg_gen_qemu_ld_i32(tmp32, t1, flags, MO_LEUL);
P
pbrook 已提交
273 274
    gen_helper_memory_to_f(t0, tmp32);
    tcg_temp_free_i32(tmp32);
A
aurel32 已提交
275 276
}

B
Blue Swirl 已提交
277
static inline void gen_qemu_ldg(TCGv t0, TCGv t1, int flags)
A
aurel32 已提交
278
{
P
pbrook 已提交
279
    TCGv tmp = tcg_temp_new();
280
    tcg_gen_qemu_ld_i64(tmp, t1, flags, MO_LEQ);
P
pbrook 已提交
281
    gen_helper_memory_to_g(t0, tmp);
A
aurel32 已提交
282 283 284
    tcg_temp_free(tmp);
}

B
Blue Swirl 已提交
285
static inline void gen_qemu_lds(TCGv t0, TCGv t1, int flags)
A
aurel32 已提交
286
{
P
pbrook 已提交
287
    TCGv_i32 tmp32 = tcg_temp_new_i32();
288
    tcg_gen_qemu_ld_i32(tmp32, t1, flags, MO_LEUL);
P
pbrook 已提交
289 290
    gen_helper_memory_to_s(t0, tmp32);
    tcg_temp_free_i32(tmp32);
A
aurel32 已提交
291 292
}

B
Blue Swirl 已提交
293
static inline void gen_qemu_ldl_l(TCGv t0, TCGv t1, int flags)
294
{
295
    tcg_gen_qemu_ld_i64(t0, t1, flags, MO_LESL);
296 297
    tcg_gen_mov_i64(cpu_lock_addr, t1);
    tcg_gen_mov_i64(cpu_lock_value, t0);
298 299
}

B
Blue Swirl 已提交
300
static inline void gen_qemu_ldq_l(TCGv t0, TCGv t1, int flags)
301
{
302
    tcg_gen_qemu_ld_i64(t0, t1, flags, MO_LEQ);
303 304
    tcg_gen_mov_i64(cpu_lock_addr, t1);
    tcg_gen_mov_i64(cpu_lock_value, t0);
305 306
}

B
Blue Swirl 已提交
307 308 309
static inline void gen_load_mem(DisasContext *ctx,
                                void (*tcg_gen_qemu_load)(TCGv t0, TCGv t1,
                                                          int flags),
310 311
                                int ra, int rb, int32_t disp16, bool fp,
                                bool clear)
312
{
313
    TCGv tmp, addr, va;
314

315 316 317 318
    /* LDQ_U with ra $31 is UNOP.  Other various loads are forms of
       prefetches, which we can treat as nops.  No worries about
       missed exceptions here.  */
    if (unlikely(ra == 31)) {
319
        return;
320
    }
321

322 323 324 325 326 327 328 329 330 331
    tmp = tcg_temp_new();
    addr = load_gpr(ctx, rb);

    if (disp16) {
        tcg_gen_addi_i64(tmp, addr, disp16);
        addr = tmp;
    }
    if (clear) {
        tcg_gen_andi_i64(tmp, addr, ~0x7);
        addr = tmp;
332
    }
333

334
    va = (fp ? cpu_fir[ra] : ctx->ir[ra]);
335 336
    tcg_gen_qemu_load(va, addr, ctx->mem_idx);

337
    tcg_temp_free(tmp);
338 339
}

B
Blue Swirl 已提交
340
static inline void gen_qemu_stf(TCGv t0, TCGv t1, int flags)
A
aurel32 已提交
341
{
P
pbrook 已提交
342 343
    TCGv_i32 tmp32 = tcg_temp_new_i32();
    gen_helper_f_to_memory(tmp32, t0);
344
    tcg_gen_qemu_st_i32(tmp32, t1, flags, MO_LEUL);
P
pbrook 已提交
345
    tcg_temp_free_i32(tmp32);
A
aurel32 已提交
346 347
}

B
Blue Swirl 已提交
348
static inline void gen_qemu_stg(TCGv t0, TCGv t1, int flags)
A
aurel32 已提交
349
{
P
pbrook 已提交
350 351
    TCGv tmp = tcg_temp_new();
    gen_helper_g_to_memory(tmp, t0);
352
    tcg_gen_qemu_st_i64(tmp, t1, flags, MO_LEQ);
A
aurel32 已提交
353 354 355
    tcg_temp_free(tmp);
}

B
Blue Swirl 已提交
356
static inline void gen_qemu_sts(TCGv t0, TCGv t1, int flags)
A
aurel32 已提交
357
{
P
pbrook 已提交
358 359
    TCGv_i32 tmp32 = tcg_temp_new_i32();
    gen_helper_s_to_memory(tmp32, t0);
360
    tcg_gen_qemu_st_i32(tmp32, t1, flags, MO_LEUL);
P
pbrook 已提交
361
    tcg_temp_free_i32(tmp32);
A
aurel32 已提交
362 363
}

B
Blue Swirl 已提交
364 365 366
static inline void gen_store_mem(DisasContext *ctx,
                                 void (*tcg_gen_qemu_store)(TCGv t0, TCGv t1,
                                                            int flags),
367 368
                                 int ra, int rb, int32_t disp16, bool fp,
                                 bool clear)
369
{
370
    TCGv tmp, addr, va;
371

372 373
    tmp = tcg_temp_new();
    addr = load_gpr(ctx, rb);
374

375 376 377 378 379 380 381
    if (disp16) {
        tcg_gen_addi_i64(tmp, addr, disp16);
        addr = tmp;
    }
    if (clear) {
        tcg_gen_andi_i64(tmp, addr, ~0x7);
        addr = tmp;
382
    }
383 384

    va = (fp ? load_fpr(ctx, ra) : load_gpr(ctx, ra));
385 386
    tcg_gen_qemu_store(va, addr, ctx->mem_idx);

387
    tcg_temp_free(tmp);
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
}

static ExitStatus gen_store_conditional(DisasContext *ctx, int ra, int rb,
                                        int32_t disp16, int quad)
{
    TCGv addr;

    if (ra == 31) {
        /* ??? Don't bother storing anything.  The user can't tell
           the difference, since the zero register always reads zero.  */
        return NO_EXIT;
    }

#if defined(CONFIG_USER_ONLY)
    addr = cpu_lock_st_addr;
#else
404
    addr = tcg_temp_local_new();
405 406
#endif

407
    tcg_gen_addi_i64(addr, load_gpr(ctx, rb), disp16);
408 409 410 411 412 413 414 415 416 417

#if defined(CONFIG_USER_ONLY)
    /* ??? This is handled via a complicated version of compare-and-swap
       in the cpu_loop.  Hopefully one day we'll have a real CAS opcode
       in TCG so that this isn't necessary.  */
    return gen_excp(ctx, quad ? EXCP_STQ_C : EXCP_STL_C, ra);
#else
    /* ??? In system mode we are never multi-threaded, so CAS can be
       implemented via a non-atomic load-compare-store sequence.  */
    {
418
        TCGLabel *lab_fail, *lab_done;
419 420 421 422
        TCGv val;

        lab_fail = gen_new_label();
        lab_done = gen_new_label();
423
        tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_lock_addr, lab_fail);
424 425

        val = tcg_temp_new();
426
        tcg_gen_qemu_ld_i64(val, addr, ctx->mem_idx, quad ? MO_LEQ : MO_LESL);
427
        tcg_gen_brcond_i64(TCG_COND_NE, val, cpu_lock_value, lab_fail);
428

429
        tcg_gen_qemu_st_i64(ctx->ir[ra], addr, ctx->mem_idx,
430
                            quad ? MO_LEQ : MO_LEUL);
431
        tcg_gen_movi_i64(ctx->ir[ra], 1);
432 433 434
        tcg_gen_br(lab_done);

        gen_set_label(lab_fail);
435
        tcg_gen_movi_i64(ctx->ir[ra], 0);
436 437 438 439 440 441 442 443

        gen_set_label(lab_done);
        tcg_gen_movi_i64(cpu_lock_addr, -1);

        tcg_temp_free(addr);
        return NO_EXIT;
    }
#endif
444 445
}

446
static bool in_superpage(DisasContext *ctx, int64_t addr)
J
j_mayer 已提交
447
{
448 449 450 451 452 453 454 455 456
    return ((ctx->tb->flags & TB_FLAGS_USER_MODE) == 0
            && addr < 0
            && ((addr >> 41) & 3) == 2
            && addr >> TARGET_VIRT_ADDR_SPACE_BITS == addr >> 63);
}

static bool use_goto_tb(DisasContext *ctx, uint64_t dest)
{
    /* Suppress goto_tb in the case of single-steping and IO.  */
457 458
    if ((ctx->tb->cflags & CF_LAST_IO)
        || ctx->singlestep_enabled || singlestep) {
459 460 461 462 463 464 465 466
        return false;
    }
    /* If the destination is in the superpage, the page perms can't change.  */
    if (in_superpage(ctx, dest)) {
        return true;
    }
    /* Check for the dest on the same page as the start of the TB.  */
    return ((ctx->tb->pc ^ dest) & TARGET_PAGE_MASK) == 0;
467
}
468

469 470 471 472 473
static ExitStatus gen_bdirect(DisasContext *ctx, int ra, int32_t disp)
{
    uint64_t dest = ctx->pc + (disp << 2);

    if (ra != 31) {
474
        tcg_gen_movi_i64(ctx->ir[ra], ctx->pc);
475 476 477 478 479 480 481 482
    }

    /* Notice branch-to-next; used to initialize RA with the PC.  */
    if (disp == 0) {
        return 0;
    } else if (use_goto_tb(ctx, dest)) {
        tcg_gen_goto_tb(0);
        tcg_gen_movi_i64(cpu_pc, dest);
483
        tcg_gen_exit_tb((uintptr_t)ctx->tb);
484 485 486 487 488
        return EXIT_GOTO_TB;
    } else {
        tcg_gen_movi_i64(cpu_pc, dest);
        return EXIT_PC_UPDATED;
    }
489 490
}

491 492
static ExitStatus gen_bcond_internal(DisasContext *ctx, TCGCond cond,
                                     TCGv cmp, int32_t disp)
493
{
494
    uint64_t dest = ctx->pc + (disp << 2);
495
    TCGLabel *lab_true = gen_new_label();
A
aurel32 已提交
496

497 498 499 500 501
    if (use_goto_tb(ctx, dest)) {
        tcg_gen_brcondi_i64(cond, cmp, 0, lab_true);

        tcg_gen_goto_tb(0);
        tcg_gen_movi_i64(cpu_pc, ctx->pc);
502
        tcg_gen_exit_tb((uintptr_t)ctx->tb);
503 504 505 506

        gen_set_label(lab_true);
        tcg_gen_goto_tb(1);
        tcg_gen_movi_i64(cpu_pc, dest);
507
        tcg_gen_exit_tb((uintptr_t)ctx->tb + 1);
508 509 510

        return EXIT_GOTO_TB;
    } else {
R
Richard Henderson 已提交
511 512 513
        TCGv_i64 z = tcg_const_i64(0);
        TCGv_i64 d = tcg_const_i64(dest);
        TCGv_i64 p = tcg_const_i64(ctx->pc);
514

R
Richard Henderson 已提交
515
        tcg_gen_movcond_i64(cond, cpu_pc, cmp, z, d, p);
516

R
Richard Henderson 已提交
517 518 519
        tcg_temp_free_i64(z);
        tcg_temp_free_i64(d);
        tcg_temp_free_i64(p);
520 521 522 523 524 525 526 527 528
        return EXIT_PC_UPDATED;
    }
}

static ExitStatus gen_bcond(DisasContext *ctx, TCGCond cond, int ra,
                            int32_t disp, int mask)
{
    TCGv cmp_tmp;

529
    if (mask) {
530
        cmp_tmp = tcg_temp_new();
531 532 533
        tcg_gen_andi_i64(cmp_tmp, load_gpr(ctx, ra), 1);
    } else {
        cmp_tmp = load_gpr(ctx, ra);
A
aurel32 已提交
534
    }
535 536

    return gen_bcond_internal(ctx, cond, cmp_tmp, disp);
J
j_mayer 已提交
537 538
}

539
/* Fold -0.0 for comparison with COND.  */
540

541
static void gen_fold_mzero(TCGCond cond, TCGv dest, TCGv src)
J
j_mayer 已提交
542
{
543
    uint64_t mzero = 1ull << 63;
A
aurel32 已提交
544

545 546 547 548
    switch (cond) {
    case TCG_COND_LE:
    case TCG_COND_GT:
        /* For <= or >, the -0.0 value directly compares the way we want.  */
549
        tcg_gen_mov_i64(dest, src);
P
pbrook 已提交
550
        break;
551 552 553 554

    case TCG_COND_EQ:
    case TCG_COND_NE:
        /* For == or !=, we can simply mask off the sign bit and compare.  */
555
        tcg_gen_andi_i64(dest, src, mzero - 1);
P
pbrook 已提交
556
        break;
557 558 559

    case TCG_COND_GE:
    case TCG_COND_LT:
560 561 562 563
        /* For >= or <, map -0.0 to +0.0 via comparison and mask.  */
        tcg_gen_setcondi_i64(TCG_COND_NE, dest, src, mzero);
        tcg_gen_neg_i64(dest, dest);
        tcg_gen_and_i64(dest, dest, src);
P
pbrook 已提交
564
        break;
565

P
pbrook 已提交
566 567
    default:
        abort();
A
aurel32 已提交
568
    }
569 570
}

571 572
static ExitStatus gen_fbcond(DisasContext *ctx, TCGCond cond, int ra,
                             int32_t disp)
573
{
574 575
    TCGv cmp_tmp = tcg_temp_new();
    gen_fold_mzero(cond, cmp_tmp, load_fpr(ctx, ra));
576
    return gen_bcond_internal(ctx, cond, cmp_tmp, disp);
J
j_mayer 已提交
577 578
}

579
static void gen_fcmov(DisasContext *ctx, TCGCond cond, int ra, int rb, int rc)
580
{
581
    TCGv_i64 va, vb, z;
582

583 584 585 586
    z = load_zero(ctx);
    vb = load_fpr(ctx, rb);
    va = tcg_temp_new();
    gen_fold_mzero(cond, va, load_fpr(ctx, ra));
587

588
    tcg_gen_movcond_i64(cond, dest_fpr(ctx, rc), va, z, vb, load_fpr(ctx, rc));
589

590
    tcg_temp_free(va);
591 592
}

593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
#define QUAL_RM_N       0x080   /* Round mode nearest even */
#define QUAL_RM_C       0x000   /* Round mode chopped */
#define QUAL_RM_M       0x040   /* Round mode minus infinity */
#define QUAL_RM_D       0x0c0   /* Round mode dynamic */
#define QUAL_RM_MASK    0x0c0

#define QUAL_U          0x100   /* Underflow enable (fp output) */
#define QUAL_V          0x100   /* Overflow enable (int output) */
#define QUAL_S          0x400   /* Software completion enable */
#define QUAL_I          0x200   /* Inexact detection enable */

static void gen_qual_roundmode(DisasContext *ctx, int fn11)
{
    TCGv_i32 tmp;

    fn11 &= QUAL_RM_MASK;
    if (fn11 == ctx->tb_rm) {
        return;
    }
    ctx->tb_rm = fn11;

    tmp = tcg_temp_new_i32();
    switch (fn11) {
    case QUAL_RM_N:
        tcg_gen_movi_i32(tmp, float_round_nearest_even);
        break;
    case QUAL_RM_C:
        tcg_gen_movi_i32(tmp, float_round_to_zero);
        break;
    case QUAL_RM_M:
        tcg_gen_movi_i32(tmp, float_round_down);
        break;
    case QUAL_RM_D:
626 627
        tcg_gen_ld8u_i32(tmp, cpu_env,
                         offsetof(CPUAlphaState, fpcr_dyn_round));
628 629 630 631
        break;
    }

#if defined(CONFIG_SOFTFLOAT_INLINE)
632
    /* ??? The "fpu/softfloat.h" interface is to call set_float_rounding_mode.
633 634 635
       With CONFIG_SOFTFLOAT that expands to an out-of-line call that just
       sets the one field.  */
    tcg_gen_st8_i32(tmp, cpu_env,
636
                    offsetof(CPUAlphaState, fp_status.float_rounding_mode));
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
#else
    gen_helper_setroundmode(tmp);
#endif

    tcg_temp_free_i32(tmp);
}

static void gen_qual_flushzero(DisasContext *ctx, int fn11)
{
    TCGv_i32 tmp;

    fn11 &= QUAL_U;
    if (fn11 == ctx->tb_ftz) {
        return;
    }
    ctx->tb_ftz = fn11;

    tmp = tcg_temp_new_i32();
    if (fn11) {
        /* Underflow is enabled, use the FPCR setting.  */
657 658
        tcg_gen_ld8u_i32(tmp, cpu_env,
                         offsetof(CPUAlphaState, fpcr_flush_to_zero));
659 660 661 662 663 664 665
    } else {
        /* Underflow is disabled, force flush-to-zero.  */
        tcg_gen_movi_i32(tmp, 1);
    }

#if defined(CONFIG_SOFTFLOAT_INLINE)
    tcg_gen_st8_i32(tmp, cpu_env,
666
                    offsetof(CPUAlphaState, fp_status.flush_to_zero));
667 668 669 670 671 672 673
#else
    gen_helper_setflushzero(tmp);
#endif

    tcg_temp_free_i32(tmp);
}

674
static TCGv gen_ieee_input(DisasContext *ctx, int reg, int fn11, int is_cmp)
675
{
676
    TCGv val;
677 678 679

    if (unlikely(reg == 31)) {
        val = load_zero(ctx);
680
    } else {
681
        val = cpu_fir[reg];
682 683
        if ((fn11 & QUAL_S) == 0) {
            if (is_cmp) {
684
                gen_helper_ieee_input_cmp(cpu_env, val);
685
            } else {
686
                gen_helper_ieee_input(cpu_env, val);
687
            }
688 689 690 691 692 693 694
        } else {
#ifndef CONFIG_USER_ONLY
            /* In system mode, raise exceptions for denormals like real
               hardware.  In user mode, proceed as if the OS completion
               handler is handling the denormal as per spec.  */
            gen_helper_ieee_input_s(cpu_env, val);
#endif
695
        }
696 697 698 699
    }
    return val;
}

700
static void gen_fp_exc_raise(int rc, int fn11)
701 702 703 704 705 706
{
    /* ??? We ought to be able to do something with imprecise exceptions.
       E.g. notice we're still in the trap shadow of something within the
       TB and do not generate the code to signal the exception; end the TB
       when an exception is forced to arrive, either by consumption of a
       register value or TRAPB or EXCB.  */
707 708 709 710 711 712 713 714 715 716 717
    TCGv_i32 reg, ign;
    uint32_t ignore = 0;

    if (!(fn11 & QUAL_U)) {
        /* Note that QUAL_U == QUAL_V, so ignore either.  */
        ignore |= FPCR_UNF | FPCR_IOV;
    }
    if (!(fn11 & QUAL_I)) {
        ignore |= FPCR_INE;
    }
    ign = tcg_const_i32(ignore);
718 719 720 721 722 723 724 725

    /* ??? Pass in the regno of the destination so that the helper can
       set EXC_MASK, which contains a bitmask of destination registers
       that have caused arithmetic traps.  A simple userspace emulation
       does not require this.  We do need it for a guest kernel's entArith,
       or if we were to do something clever with imprecise exceptions.  */
    reg = tcg_const_i32(rc + 32);
    if (fn11 & QUAL_S) {
726
        gen_helper_fp_exc_raise_s(cpu_env, ign, reg);
727
    } else {
728
        gen_helper_fp_exc_raise(cpu_env, ign, reg);
729 730 731
    }

    tcg_temp_free_i32(reg);
732
    tcg_temp_free_i32(ign);
733 734
}

735
static void gen_cvtlq(TCGv vc, TCGv vb)
736
{
737
    TCGv tmp = tcg_temp_new();
738

739 740 741 742 743 744 745
    /* The arithmetic right shift here, plus the sign-extended mask below
       yields a sign-extended result without an explicit ext32s_i64.  */
    tcg_gen_sari_i64(tmp, vb, 32);
    tcg_gen_shri_i64(vc, vb, 29);
    tcg_gen_andi_i64(tmp, tmp, (int32_t)0xc0000000);
    tcg_gen_andi_i64(vc, vc, 0x3fffffff);
    tcg_gen_or_i64(vc, vc, tmp);
746

747
    tcg_temp_free(tmp);
748 749
}

750 751
static void gen_ieee_arith2(DisasContext *ctx,
                            void (*helper)(TCGv, TCGv_ptr, TCGv),
752 753 754 755 756 757 758
                            int rb, int rc, int fn11)
{
    TCGv vb;

    gen_qual_roundmode(ctx, fn11);
    gen_qual_flushzero(ctx, fn11);

759
    vb = gen_ieee_input(ctx, rb, fn11, 0);
760
    helper(dest_fpr(ctx, rc), cpu_env, vb);
761 762 763 764 765

    gen_fp_exc_raise(rc, fn11);
}

#define IEEE_ARITH2(name)                                       \
766 767
static inline void glue(gen_, name)(DisasContext *ctx,          \
                                    int rb, int rc, int fn11)   \
768 769 770 771 772 773 774 775
{                                                               \
    gen_ieee_arith2(ctx, gen_helper_##name, rb, rc, fn11);      \
}
IEEE_ARITH2(sqrts)
IEEE_ARITH2(sqrtt)
IEEE_ARITH2(cvtst)
IEEE_ARITH2(cvtts)

776
static void gen_cvttq(DisasContext *ctx, int rb, int rc, int fn11)
777
{
778
    TCGv vb, vc;
779 780

    /* No need to set flushzero, since we have an integer output.  */
781
    vb = gen_ieee_input(ctx, rb, fn11, 0);
782
    vc = dest_fpr(ctx, rc);
783

784 785 786
    /* Almost all integer conversions use cropped rounding;
       special case that.  */
    if ((fn11 & QUAL_RM_MASK) == QUAL_RM_C) {
787
        gen_helper_cvttq_c(vc, cpu_env, vb);
788
    } else {
789
        gen_qual_roundmode(ctx, fn11);
790
        gen_helper_cvttq(vc, cpu_env, vb);
791
    }
792
    gen_fp_exc_raise(rc, fn11);
J
j_mayer 已提交
793 794
}

795 796
static void gen_ieee_intcvt(DisasContext *ctx,
                            void (*helper)(TCGv, TCGv_ptr, TCGv),
797 798
			    int rb, int rc, int fn11)
{
799
    TCGv vb, vc;
800 801

    gen_qual_roundmode(ctx, fn11);
802 803
    vb = load_fpr(ctx, rb);
    vc = dest_fpr(ctx, rc);
804 805 806 807 808

    /* The only exception that can be raised by integer conversion
       is inexact.  Thus we only need to worry about exceptions when
       inexact handling is requested.  */
    if (fn11 & QUAL_I) {
809
        helper(vc, cpu_env, vb);
810 811
        gen_fp_exc_raise(rc, fn11);
    } else {
812
        helper(vc, cpu_env, vb);
813 814 815 816
    }
}

#define IEEE_INTCVT(name)                                       \
817 818
static inline void glue(gen_, name)(DisasContext *ctx,          \
                                    int rb, int rc, int fn11)   \
819 820 821 822 823 824
{                                                               \
    gen_ieee_intcvt(ctx, gen_helper_##name, rb, rc, fn11);      \
}
IEEE_INTCVT(cvtqs)
IEEE_INTCVT(cvtqt)

825
static void gen_cpy_mask(TCGv vc, TCGv va, TCGv vb, bool inv_a, uint64_t mask)
826
{
827 828
    TCGv vmask = tcg_const_i64(mask);
    TCGv tmp = tcg_temp_new_i64();
829

830 831
    if (inv_a) {
        tcg_gen_andc_i64(tmp, vmask, va);
832
    } else {
833
        tcg_gen_and_i64(tmp, va, vmask);
834 835
    }

836 837
    tcg_gen_andc_i64(vc, vb, vmask);
    tcg_gen_or_i64(vc, vc, tmp);
838 839

    tcg_temp_free(vmask);
840
    tcg_temp_free(tmp);
841 842
}

843
static void gen_ieee_arith3(DisasContext *ctx,
844
                            void (*helper)(TCGv, TCGv_ptr, TCGv, TCGv),
845 846
                            int ra, int rb, int rc, int fn11)
{
847
    TCGv va, vb, vc;
848 849 850 851

    gen_qual_roundmode(ctx, fn11);
    gen_qual_flushzero(ctx, fn11);

852 853
    va = gen_ieee_input(ctx, ra, fn11, 0);
    vb = gen_ieee_input(ctx, rb, fn11, 0);
854 855
    vc = dest_fpr(ctx, rc);
    helper(vc, cpu_env, va, vb);
856 857 858 859 860

    gen_fp_exc_raise(rc, fn11);
}

#define IEEE_ARITH3(name)                                               \
861 862
static inline void glue(gen_, name)(DisasContext *ctx,                  \
                                    int ra, int rb, int rc, int fn11)   \
863 864 865 866 867 868 869 870 871 872 873 874 875
{                                                                       \
    gen_ieee_arith3(ctx, gen_helper_##name, ra, rb, rc, fn11);          \
}
IEEE_ARITH3(adds)
IEEE_ARITH3(subs)
IEEE_ARITH3(muls)
IEEE_ARITH3(divs)
IEEE_ARITH3(addt)
IEEE_ARITH3(subt)
IEEE_ARITH3(mult)
IEEE_ARITH3(divt)

static void gen_ieee_compare(DisasContext *ctx,
876
                             void (*helper)(TCGv, TCGv_ptr, TCGv, TCGv),
877 878
                             int ra, int rb, int rc, int fn11)
{
879
    TCGv va, vb, vc;
880

881 882
    va = gen_ieee_input(ctx, ra, fn11, 1);
    vb = gen_ieee_input(ctx, rb, fn11, 1);
883 884
    vc = dest_fpr(ctx, rc);
    helper(vc, cpu_env, va, vb);
885 886 887 888 889

    gen_fp_exc_raise(rc, fn11);
}

#define IEEE_CMP3(name)                                                 \
890 891
static inline void glue(gen_, name)(DisasContext *ctx,                  \
                                    int ra, int rb, int rc, int fn11)   \
892 893 894 895 896 897 898
{                                                                       \
    gen_ieee_compare(ctx, gen_helper_##name, ra, rb, rc, fn11);         \
}
IEEE_CMP3(cmptun)
IEEE_CMP3(cmpteq)
IEEE_CMP3(cmptlt)
IEEE_CMP3(cmptle)
P
pbrook 已提交
899

900 901 902 903 904 905
static inline uint64_t zapnot_mask(uint8_t lit)
{
    uint64_t mask = 0;
    int i;

    for (i = 0; i < 8; ++i) {
P
Paolo Bonzini 已提交
906
        if ((lit >> i) & 1) {
907
            mask |= 0xffull << (i * 8);
P
Paolo Bonzini 已提交
908
        }
909 910 911 912
    }
    return mask;
}

913 914 915
/* Implement zapnot with an immediate operand, which expands to some
   form of immediate AND.  This is a basic building block in the
   definition of many of the other byte manipulation instructions.  */
916
static void gen_zapnoti(TCGv dest, TCGv src, uint8_t lit)
917 918 919
{
    switch (lit) {
    case 0x00:
920
        tcg_gen_movi_i64(dest, 0);
921 922
        break;
    case 0x01:
923
        tcg_gen_ext8u_i64(dest, src);
924 925
        break;
    case 0x03:
926
        tcg_gen_ext16u_i64(dest, src);
927 928
        break;
    case 0x0f:
929
        tcg_gen_ext32u_i64(dest, src);
930 931
        break;
    case 0xff:
932
        tcg_gen_mov_i64(dest, src);
933 934
        break;
    default:
935
        tcg_gen_andi_i64(dest, src, zapnot_mask(lit));
936 937 938 939
        break;
    }
}

940
/* EXTWH, EXTLH, EXTQH */
941
static void gen_ext_h(DisasContext *ctx, TCGv vc, TCGv va, int rb, bool islit,
942
                      uint8_t lit, uint8_t byte_mask)
943
{
944 945
    if (islit) {
        tcg_gen_shli_i64(vc, va, (64 - lit * 8) & 0x3f);
P
Paolo Bonzini 已提交
946
    } else {
947 948 949 950 951 952
        TCGv tmp = tcg_temp_new();
        tcg_gen_shli_i64(tmp, load_gpr(ctx, rb), 3);
        tcg_gen_neg_i64(tmp, tmp);
        tcg_gen_andi_i64(tmp, tmp, 0x3f);
        tcg_gen_shl_i64(vc, va, tmp);
        tcg_temp_free(tmp);
953
    }
954
    gen_zapnoti(vc, vc, byte_mask);
955 956
}

957
/* EXTBL, EXTWL, EXTLL, EXTQL */
958
static void gen_ext_l(DisasContext *ctx, TCGv vc, TCGv va, int rb, bool islit,
959
                      uint8_t lit, uint8_t byte_mask)
960
{
961 962
    if (islit) {
        tcg_gen_shri_i64(vc, va, (lit & 7) * 8);
P
Paolo Bonzini 已提交
963
    } else {
964 965 966 967 968
        TCGv tmp = tcg_temp_new();
        tcg_gen_andi_i64(tmp, load_gpr(ctx, rb), 7);
        tcg_gen_shli_i64(tmp, tmp, 3);
        tcg_gen_shr_i64(vc, va, tmp);
        tcg_temp_free(tmp);
969
    }
970
    gen_zapnoti(vc, vc, byte_mask);
971 972
}

973
/* INSWH, INSLH, INSQH */
974
static void gen_ins_h(DisasContext *ctx, TCGv vc, TCGv va, int rb, bool islit,
975 976
                      uint8_t lit, uint8_t byte_mask)
{
977
    TCGv tmp = tcg_temp_new();
978

979 980 981 982
    /* The instruction description has us left-shift the byte mask and extract
       bits <15:8> and apply that zap at the end.  This is equivalent to simply
       performing the zap first and shifting afterward.  */
    gen_zapnoti(tmp, va, byte_mask);
983

984 985 986 987
    if (islit) {
        lit &= 7;
        if (unlikely(lit == 0)) {
            tcg_gen_movi_i64(vc, 0);
988
        } else {
989
            tcg_gen_shri_i64(vc, tmp, 64 - lit * 8);
990
        }
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
    } else {
        TCGv shift = tcg_temp_new();

        /* If (B & 7) == 0, we need to shift by 64 and leave a zero.  Do this
           portably by splitting the shift into two parts: shift_count-1 and 1.
           Arrange for the -1 by using ones-complement instead of
           twos-complement in the negation: ~(B * 8) & 63.  */

        tcg_gen_shli_i64(shift, load_gpr(ctx, rb), 3);
        tcg_gen_not_i64(shift, shift);
        tcg_gen_andi_i64(shift, shift, 0x3f);

        tcg_gen_shr_i64(vc, tmp, shift);
        tcg_gen_shri_i64(vc, vc, 1);
        tcg_temp_free(shift);
1006
    }
1007
    tcg_temp_free(tmp);
1008 1009
}

1010
/* INSBL, INSWL, INSLL, INSQL */
1011
static void gen_ins_l(DisasContext *ctx, TCGv vc, TCGv va, int rb, bool islit,
1012
                      uint8_t lit, uint8_t byte_mask)
1013
{
1014
    TCGv tmp = tcg_temp_new();
1015

1016 1017 1018 1019 1020
    /* The instruction description has us left-shift the byte mask
       the same number of byte slots as the data and apply the zap
       at the end.  This is equivalent to simply performing the zap
       first and shifting afterward.  */
    gen_zapnoti(tmp, va, byte_mask);
1021

1022 1023 1024 1025 1026 1027 1028 1029
    if (islit) {
        tcg_gen_shli_i64(vc, tmp, (lit & 7) * 8);
    } else {
        TCGv shift = tcg_temp_new();
        tcg_gen_andi_i64(shift, load_gpr(ctx, rb), 7);
        tcg_gen_shli_i64(shift, shift, 3);
        tcg_gen_shl_i64(vc, tmp, shift);
        tcg_temp_free(shift);
1030
    }
1031
    tcg_temp_free(tmp);
1032 1033
}

1034
/* MSKWH, MSKLH, MSKQH */
1035
static void gen_msk_h(DisasContext *ctx, TCGv vc, TCGv va, int rb, bool islit,
1036 1037
                      uint8_t lit, uint8_t byte_mask)
{
1038 1039
    if (islit) {
        gen_zapnoti(vc, va, ~((byte_mask << (lit & 7)) >> 8));
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
    } else {
        TCGv shift = tcg_temp_new();
        TCGv mask = tcg_temp_new();

        /* The instruction description is as above, where the byte_mask
           is shifted left, and then we extract bits <15:8>.  This can be
           emulated with a right-shift on the expanded byte mask.  This
           requires extra care because for an input <2:0> == 0 we need a
           shift of 64 bits in order to generate a zero.  This is done by
           splitting the shift into two parts, the variable shift - 1
           followed by a constant 1 shift.  The code we expand below is
1051
           equivalent to ~(B * 8) & 63.  */
1052

1053
        tcg_gen_shli_i64(shift, load_gpr(ctx, rb), 3);
1054 1055 1056 1057 1058 1059
        tcg_gen_not_i64(shift, shift);
        tcg_gen_andi_i64(shift, shift, 0x3f);
        tcg_gen_movi_i64(mask, zapnot_mask (byte_mask));
        tcg_gen_shr_i64(mask, mask, shift);
        tcg_gen_shri_i64(mask, mask, 1);

1060
        tcg_gen_andc_i64(vc, va, mask);
1061 1062 1063 1064 1065 1066

        tcg_temp_free(mask);
        tcg_temp_free(shift);
    }
}

1067
/* MSKBL, MSKWL, MSKLL, MSKQL */
1068
static void gen_msk_l(DisasContext *ctx, TCGv vc, TCGv va, int rb, bool islit,
1069
                      uint8_t lit, uint8_t byte_mask)
1070
{
1071 1072
    if (islit) {
        gen_zapnoti(vc, va, ~(byte_mask << (lit & 7)));
1073 1074 1075 1076
    } else {
        TCGv shift = tcg_temp_new();
        TCGv mask = tcg_temp_new();

1077
        tcg_gen_andi_i64(shift, load_gpr(ctx, rb), 7);
1078
        tcg_gen_shli_i64(shift, shift, 3);
1079
        tcg_gen_movi_i64(mask, zapnot_mask(byte_mask));
1080 1081
        tcg_gen_shl_i64(mask, mask, shift);

1082
        tcg_gen_andc_i64(vc, va, mask);
1083 1084 1085 1086 1087 1088

        tcg_temp_free(mask);
        tcg_temp_free(shift);
    }
}

1089
static void gen_rx(DisasContext *ctx, int ra, int set)
1090 1091 1092 1093
{
    TCGv_i32 tmp;

    if (ra != 31) {
1094 1095
        tcg_gen_ld8u_i64(ctx->ir[ra], cpu_env,
                         offsetof(CPUAlphaState, intr_flag));
1096 1097 1098
    }

    tmp = tcg_const_i32(set);
1099
    tcg_gen_st8_i32(tmp, cpu_env, offsetof(CPUAlphaState, intr_flag));
1100 1101 1102
    tcg_temp_free_i32(tmp);
}

1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
static ExitStatus gen_call_pal(DisasContext *ctx, int palcode)
{
    /* We're emulating OSF/1 PALcode.  Many of these are trivial access
       to internal cpu registers.  */

    /* Unprivileged PAL call */
    if (palcode >= 0x80 && palcode < 0xC0) {
        switch (palcode) {
        case 0x86:
            /* IMB */
            /* No-op inside QEMU.  */
            break;
        case 0x9E:
            /* RDUNIQUE */
1117
            tcg_gen_ld_i64(ctx->ir[IR_V0], cpu_env,
1118
                           offsetof(CPUAlphaState, unique));
1119 1120 1121
            break;
        case 0x9F:
            /* WRUNIQUE */
1122
            tcg_gen_st_i64(ctx->ir[IR_A0], cpu_env,
1123
                           offsetof(CPUAlphaState, unique));
1124 1125
            break;
        default:
1126 1127
            palcode &= 0xbf;
            goto do_call_pal;
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
        }
        return NO_EXIT;
    }

#ifndef CONFIG_USER_ONLY
    /* Privileged PAL code */
    if (palcode < 0x40 && (ctx->tb->flags & TB_FLAGS_USER_MODE) == 0) {
        switch (palcode) {
        case 0x01:
            /* CFLUSH */
            /* No-op inside QEMU.  */
            break;
        case 0x02:
            /* DRAINA */
            /* No-op inside QEMU.  */
            break;
        case 0x2D:
            /* WRVPTPTR */
1146
            tcg_gen_st_i64(ctx->ir[IR_A0], cpu_env,
1147
                           offsetof(CPUAlphaState, vptptr));
1148 1149 1150
            break;
        case 0x31:
            /* WRVAL */
1151
            tcg_gen_st_i64(ctx->ir[IR_A0], cpu_env,
1152
                           offsetof(CPUAlphaState, sysval));
1153 1154 1155
            break;
        case 0x32:
            /* RDVAL */
1156
            tcg_gen_ld_i64(ctx->ir[IR_V0], cpu_env,
1157
                           offsetof(CPUAlphaState, sysval));
1158 1159 1160 1161 1162 1163 1164 1165
            break;

        case 0x35: {
            /* SWPIPL */
            TCGv tmp;

            /* Note that we already know we're in kernel mode, so we know
               that PS only contains the 3 IPL bits.  */
1166
            tcg_gen_ld8u_i64(ctx->ir[IR_V0], cpu_env,
1167
                             offsetof(CPUAlphaState, ps));
1168 1169 1170

            /* But make sure and store only the 3 IPL bits from the user.  */
            tmp = tcg_temp_new();
1171
            tcg_gen_andi_i64(tmp, ctx->ir[IR_A0], PS_INT_MASK);
1172
            tcg_gen_st8_i64(tmp, cpu_env, offsetof(CPUAlphaState, ps));
1173 1174 1175 1176 1177 1178
            tcg_temp_free(tmp);
            break;
        }

        case 0x36:
            /* RDPS */
1179
            tcg_gen_ld8u_i64(ctx->ir[IR_V0], cpu_env,
1180
                             offsetof(CPUAlphaState, ps));
1181 1182 1183
            break;
        case 0x38:
            /* WRUSP */
1184
            tcg_gen_st_i64(ctx->ir[IR_A0], cpu_env,
1185
                           offsetof(CPUAlphaState, usp));
1186 1187 1188
            break;
        case 0x3A:
            /* RDUSP */
1189
            tcg_gen_ld_i64(ctx->ir[IR_V0], cpu_env,
1190
                           offsetof(CPUAlphaState, usp));
1191 1192 1193
            break;
        case 0x3C:
            /* WHAMI */
1194
            tcg_gen_ld32s_i64(ctx->ir[IR_V0], cpu_env,
1195
                -offsetof(AlphaCPU, env) + offsetof(CPUState, cpu_index));
1196 1197 1198
            break;

        default:
1199 1200
            palcode &= 0x3f;
            goto do_call_pal;
1201 1202 1203 1204 1205
        }
        return NO_EXIT;
    }
#endif
    return gen_invalid(ctx);
1206 1207 1208 1209 1210 1211

 do_call_pal:
#ifdef CONFIG_USER_ONLY
    return gen_excp(ctx, EXCP_CALL_PAL, palcode);
#else
    {
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
        TCGv tmp = tcg_temp_new();
        uint64_t exc_addr = ctx->pc;
        uint64_t entry = ctx->palbr;

        if (ctx->tb->flags & TB_FLAGS_PAL_MODE) {
            exc_addr |= 1;
        } else {
            tcg_gen_movi_i64(tmp, 1);
            tcg_gen_st8_i64(tmp, cpu_env, offsetof(CPUAlphaState, pal_mode));
        }
1222

1223 1224 1225
        tcg_gen_movi_i64(tmp, exc_addr);
        tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUAlphaState, exc_addr));
        tcg_temp_free(tmp);
1226

1227 1228 1229
        entry += (palcode & 0x80
                  ? 0x2000 + (palcode - 0x80) * 64
                  : 0x1000 + palcode * 64);
1230 1231

        /* Since the destination is running in PALmode, we don't really
S
Stefan Weil 已提交
1232
           need the page permissions check.  We'll see the existence of
1233 1234 1235 1236
           the page when we create the TB, and we'll flush all TBs if
           we change the PAL base register.  */
        if (!ctx->singlestep_enabled && !(ctx->tb->cflags & CF_LAST_IO)) {
            tcg_gen_goto_tb(0);
1237
            tcg_gen_movi_i64(cpu_pc, entry);
1238
            tcg_gen_exit_tb((uintptr_t)ctx->tb);
1239
            return EXIT_GOTO_TB;
1240 1241 1242
        } else {
            tcg_gen_movi_i64(cpu_pc, entry);
            return EXIT_PC_UPDATED;
1243
        }
1244 1245
    }
#endif
1246 1247
}

1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
#ifndef CONFIG_USER_ONLY

#define PR_BYTE         0x100000
#define PR_LONG         0x200000

static int cpu_pr_data(int pr)
{
    switch (pr) {
    case  0: return offsetof(CPUAlphaState, ps) | PR_BYTE;
    case  1: return offsetof(CPUAlphaState, fen) | PR_BYTE;
    case  2: return offsetof(CPUAlphaState, pcc_ofs) | PR_LONG;
    case  3: return offsetof(CPUAlphaState, trap_arg0);
    case  4: return offsetof(CPUAlphaState, trap_arg1);
    case  5: return offsetof(CPUAlphaState, trap_arg2);
    case  6: return offsetof(CPUAlphaState, exc_addr);
    case  7: return offsetof(CPUAlphaState, palbr);
    case  8: return offsetof(CPUAlphaState, ptbr);
    case  9: return offsetof(CPUAlphaState, vptptr);
    case 10: return offsetof(CPUAlphaState, unique);
    case 11: return offsetof(CPUAlphaState, sysval);
    case 12: return offsetof(CPUAlphaState, usp);

    case 40 ... 63:
        return offsetof(CPUAlphaState, scratch[pr - 40]);
1272 1273 1274

    case 251:
        return offsetof(CPUAlphaState, alarm_expire);
1275 1276 1277 1278
    }
    return 0;
}

1279
static ExitStatus gen_mfpr(DisasContext *ctx, TCGv va, int regno)
1280
{
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
    void (*helper)(TCGv);
    int data;

    switch (regno) {
    case 32 ... 39:
        /* Accessing the "non-shadow" general registers.  */
        regno = regno == 39 ? 25 : regno - 32 + 8;
        tcg_gen_mov_i64(va, cpu_std_ir[regno]);
        break;

    case 250: /* WALLTIME */
        helper = gen_helper_get_walltime;
        goto do_helper;
    case 249: /* VMTIME */
        helper = gen_helper_get_vmtime;
    do_helper:
        if (use_icount) {
1298
            gen_io_start();
1299
            helper(va);
1300 1301 1302
            gen_io_end();
            return EXIT_PC_STALE;
        } else {
1303
            helper(va);
1304
        }
1305
        break;
1306

1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
    default:
        /* The basic registers are data only, and unknown registers
           are read-zero, write-ignore.  */
        data = cpu_pr_data(regno);
        if (data == 0) {
            tcg_gen_movi_i64(va, 0);
        } else if (data & PR_BYTE) {
            tcg_gen_ld8u_i64(va, cpu_env, data & ~PR_BYTE);
        } else if (data & PR_LONG) {
            tcg_gen_ld32s_i64(va, cpu_env, data & ~PR_LONG);
        } else {
            tcg_gen_ld_i64(va, cpu_env, data);
        }
        break;
1321
    }
1322

1323
    return NO_EXIT;
1324 1325
}

1326
static ExitStatus gen_mtpr(DisasContext *ctx, TCGv vb, int regno)
1327 1328
{
    TCGv tmp;
1329
    int data;
1330

1331 1332
    switch (regno) {
    case 255:
1333
        /* TBIA */
1334
        gen_helper_tbia(cpu_env);
1335 1336 1337
        break;

    case 254:
1338
        /* TBIS */
1339
        gen_helper_tbis(cpu_env, vb);
1340 1341 1342 1343 1344
        break;

    case 253:
        /* WAIT */
        tmp = tcg_const_i64(1);
1345 1346
        tcg_gen_st32_i64(tmp, cpu_env, -offsetof(AlphaCPU, env) +
                                       offsetof(CPUState, halted));
1347 1348
        return gen_excp(ctx, EXCP_HLT, 0);

1349 1350
    case 252:
        /* HALT */
1351
        gen_helper_halt(vb);
1352 1353
        return EXIT_PC_STALE;

1354 1355
    case 251:
        /* ALARM */
1356
        gen_helper_set_alarm(cpu_env, vb);
1357 1358
        break;

1359 1360
    case 7:
        /* PALBR */
1361
        tcg_gen_st_i64(vb, cpu_env, offsetof(CPUAlphaState, palbr));
1362 1363 1364 1365 1366 1367
        /* Changing the PAL base register implies un-chaining all of the TBs
           that ended with a CALL_PAL.  Since the base register usually only
           changes during boot, flushing everything works well.  */
        gen_helper_tb_flush(cpu_env);
        return EXIT_PC_STALE;

1368 1369 1370 1371 1372 1373
    case 32 ... 39:
        /* Accessing the "non-shadow" general registers.  */
        regno = regno == 39 ? 25 : regno - 32 + 8;
        tcg_gen_mov_i64(cpu_std_ir[regno], vb);
        break;

1374
    default:
1375 1376
        /* The basic registers are data only, and unknown registers
           are read-zero, write-ignore.  */
1377
        data = cpu_pr_data(regno);
1378 1379
        if (data != 0) {
            if (data & PR_BYTE) {
1380
                tcg_gen_st8_i64(vb, cpu_env, data & ~PR_BYTE);
1381
            } else if (data & PR_LONG) {
1382
                tcg_gen_st32_i64(vb, cpu_env, data & ~PR_LONG);
1383
            } else {
1384
                tcg_gen_st_i64(vb, cpu_env, data);
1385
            }
1386
        }
1387
        break;
1388 1389
    }

1390
    return NO_EXIT;
1391 1392 1393
}
#endif /* !USER_ONLY*/

1394 1395 1396 1397 1398 1399 1400
#define REQUIRE_NO_LIT                          \
    do {                                        \
        if (real_islit) {                       \
            goto invalid_opc;                   \
        }                                       \
    } while (0)

1401 1402 1403 1404 1405 1406 1407
#define REQUIRE_TB_FLAG(FLAG)                   \
    do {                                        \
        if ((ctx->tb->flags & (FLAG)) == 0) {   \
            goto invalid_opc;                   \
        }                                       \
    } while (0)

1408 1409 1410 1411 1412 1413 1414
#define REQUIRE_REG_31(WHICH)                   \
    do {                                        \
        if (WHICH != 31) {                      \
            goto invalid_opc;                   \
        }                                       \
    } while (0)

1415
static ExitStatus translate_one(DisasContext *ctx, uint32_t insn)
J
j_mayer 已提交
1416
{
1417
    int32_t disp21, disp16, disp12 __attribute__((unused));
1418
    uint16_t fn11;
1419
    uint8_t opc, ra, rb, rc, fpfn, fn7, lit;
1420
    bool islit, real_islit;
1421
    TCGv va, vb, vc, tmp, tmp2;
1422
    TCGv_i32 t32;
1423
    ExitStatus ret;
J
j_mayer 已提交
1424 1425

    /* Decode all instruction fields */
1426 1427 1428 1429
    opc = extract32(insn, 26, 6);
    ra = extract32(insn, 21, 5);
    rb = extract32(insn, 16, 5);
    rc = extract32(insn, 0, 5);
1430
    real_islit = islit = extract32(insn, 12, 1);
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
    lit = extract32(insn, 13, 8);

    disp21 = sextract32(insn, 0, 21);
    disp16 = sextract32(insn, 0, 16);
    disp12 = sextract32(insn, 0, 12);

    fn11 = extract32(insn, 5, 11);
    fpfn = extract32(insn, 5, 6);
    fn7 = extract32(insn, 5, 7);

1441
    if (rb == 31 && !islit) {
1442
        islit = true;
1443
        lit = 0;
1444
    }
R
Richard Henderson 已提交
1445

1446
    ret = NO_EXIT;
J
j_mayer 已提交
1447 1448 1449
    switch (opc) {
    case 0x00:
        /* CALL_PAL */
1450
        ret = gen_call_pal(ctx, insn & 0x03ffffff);
1451
        break;
J
j_mayer 已提交
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
    case 0x01:
        /* OPC01 */
        goto invalid_opc;
    case 0x02:
        /* OPC02 */
        goto invalid_opc;
    case 0x03:
        /* OPC03 */
        goto invalid_opc;
    case 0x04:
        /* OPC04 */
        goto invalid_opc;
    case 0x05:
        /* OPC05 */
        goto invalid_opc;
    case 0x06:
        /* OPC06 */
        goto invalid_opc;
    case 0x07:
        /* OPC07 */
        goto invalid_opc;
1473

J
j_mayer 已提交
1474 1475
    case 0x09:
        /* LDAH */
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
        disp16 = (uint32_t)disp16 << 16;
        /* fall through */
    case 0x08:
        /* LDA */
        va = dest_gpr(ctx, ra);
        /* It's worth special-casing immediate loads.  */
        if (rb == 31) {
            tcg_gen_movi_i64(va, disp16);
        } else {
            tcg_gen_addi_i64(va, load_gpr(ctx, rb), disp16);
A
aurel32 已提交
1486
        }
J
j_mayer 已提交
1487
        break;
1488

J
j_mayer 已提交
1489 1490
    case 0x0A:
        /* LDBU */
1491 1492 1493
        REQUIRE_TB_FLAG(TB_FLAGS_AMASK_BWX);
        gen_load_mem(ctx, &tcg_gen_qemu_ld8u, ra, rb, disp16, 0, 0);
        break;
J
j_mayer 已提交
1494 1495
    case 0x0B:
        /* LDQ_U */
A
aurel32 已提交
1496
        gen_load_mem(ctx, &tcg_gen_qemu_ld64, ra, rb, disp16, 0, 1);
J
j_mayer 已提交
1497 1498 1499
        break;
    case 0x0C:
        /* LDWU */
1500 1501 1502
        REQUIRE_TB_FLAG(TB_FLAGS_AMASK_BWX);
        gen_load_mem(ctx, &tcg_gen_qemu_ld16u, ra, rb, disp16, 0, 0);
        break;
J
j_mayer 已提交
1503 1504
    case 0x0D:
        /* STW */
1505
        REQUIRE_TB_FLAG(TB_FLAGS_AMASK_BWX);
1506
        gen_store_mem(ctx, &tcg_gen_qemu_st16, ra, rb, disp16, 0, 0);
J
j_mayer 已提交
1507 1508 1509
        break;
    case 0x0E:
        /* STB */
1510
        REQUIRE_TB_FLAG(TB_FLAGS_AMASK_BWX);
1511
        gen_store_mem(ctx, &tcg_gen_qemu_st8, ra, rb, disp16, 0, 0);
J
j_mayer 已提交
1512 1513 1514
        break;
    case 0x0F:
        /* STQ_U */
1515
        gen_store_mem(ctx, &tcg_gen_qemu_st64, ra, rb, disp16, 0, 1);
J
j_mayer 已提交
1516
        break;
1517

J
j_mayer 已提交
1518
    case 0x10:
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
        vc = dest_gpr(ctx, rc);
        vb = load_gpr_lit(ctx, rb, lit, islit);

        if (ra == 31) {
            if (fn7 == 0x00) {
                /* Special case ADDL as SEXTL.  */
                tcg_gen_ext32s_i64(vc, vb);
                break;
            }
            if (fn7 == 0x29) {
                /* Special case SUBQ as NEGQ.  */
                tcg_gen_neg_i64(vc, vb);
                break;
            }
        }

        va = load_gpr(ctx, ra);
J
j_mayer 已提交
1536 1537 1538
        switch (fn7) {
        case 0x00:
            /* ADDL */
1539 1540
            tcg_gen_add_i64(vc, va, vb);
            tcg_gen_ext32s_i64(vc, vc);
J
j_mayer 已提交
1541 1542 1543
            break;
        case 0x02:
            /* S4ADDL */
1544 1545 1546 1547 1548
            tmp = tcg_temp_new();
            tcg_gen_shli_i64(tmp, va, 2);
            tcg_gen_add_i64(tmp, tmp, vb);
            tcg_gen_ext32s_i64(vc, tmp);
            tcg_temp_free(tmp);
J
j_mayer 已提交
1549 1550 1551
            break;
        case 0x09:
            /* SUBL */
1552 1553
            tcg_gen_sub_i64(vc, va, vb);
            tcg_gen_ext32s_i64(vc, vc);
J
j_mayer 已提交
1554 1555 1556
            break;
        case 0x0B:
            /* S4SUBL */
1557 1558 1559 1560 1561
            tmp = tcg_temp_new();
            tcg_gen_shli_i64(tmp, va, 2);
            tcg_gen_sub_i64(tmp, tmp, vb);
            tcg_gen_ext32s_i64(vc, tmp);
            tcg_temp_free(tmp);
J
j_mayer 已提交
1562 1563 1564
            break;
        case 0x0F:
            /* CMPBGE */
1565
            gen_helper_cmpbge(vc, va, vb);
J
j_mayer 已提交
1566 1567 1568
            break;
        case 0x12:
            /* S8ADDL */
1569 1570 1571 1572 1573
            tmp = tcg_temp_new();
            tcg_gen_shli_i64(tmp, va, 3);
            tcg_gen_add_i64(tmp, tmp, vb);
            tcg_gen_ext32s_i64(vc, tmp);
            tcg_temp_free(tmp);
J
j_mayer 已提交
1574 1575 1576
            break;
        case 0x1B:
            /* S8SUBL */
1577 1578 1579 1580 1581
            tmp = tcg_temp_new();
            tcg_gen_shli_i64(tmp, va, 3);
            tcg_gen_sub_i64(tmp, tmp, vb);
            tcg_gen_ext32s_i64(vc, tmp);
            tcg_temp_free(tmp);
J
j_mayer 已提交
1582 1583 1584
            break;
        case 0x1D:
            /* CMPULT */
1585
            tcg_gen_setcond_i64(TCG_COND_LTU, vc, va, vb);
J
j_mayer 已提交
1586 1587 1588
            break;
        case 0x20:
            /* ADDQ */
1589
            tcg_gen_add_i64(vc, va, vb);
J
j_mayer 已提交
1590 1591 1592
            break;
        case 0x22:
            /* S4ADDQ */
1593 1594 1595 1596
            tmp = tcg_temp_new();
            tcg_gen_shli_i64(tmp, va, 2);
            tcg_gen_add_i64(vc, tmp, vb);
            tcg_temp_free(tmp);
J
j_mayer 已提交
1597 1598 1599
            break;
        case 0x29:
            /* SUBQ */
1600
            tcg_gen_sub_i64(vc, va, vb);
J
j_mayer 已提交
1601 1602 1603
            break;
        case 0x2B:
            /* S4SUBQ */
1604 1605 1606 1607
            tmp = tcg_temp_new();
            tcg_gen_shli_i64(tmp, va, 2);
            tcg_gen_sub_i64(vc, tmp, vb);
            tcg_temp_free(tmp);
J
j_mayer 已提交
1608 1609 1610
            break;
        case 0x2D:
            /* CMPEQ */
1611
            tcg_gen_setcond_i64(TCG_COND_EQ, vc, va, vb);
J
j_mayer 已提交
1612 1613 1614
            break;
        case 0x32:
            /* S8ADDQ */
1615 1616 1617 1618
            tmp = tcg_temp_new();
            tcg_gen_shli_i64(tmp, va, 3);
            tcg_gen_add_i64(vc, tmp, vb);
            tcg_temp_free(tmp);
J
j_mayer 已提交
1619 1620 1621
            break;
        case 0x3B:
            /* S8SUBQ */
1622 1623 1624 1625
            tmp = tcg_temp_new();
            tcg_gen_shli_i64(tmp, va, 3);
            tcg_gen_sub_i64(vc, tmp, vb);
            tcg_temp_free(tmp);
J
j_mayer 已提交
1626 1627 1628
            break;
        case 0x3D:
            /* CMPULE */
1629
            tcg_gen_setcond_i64(TCG_COND_LEU, vc, va, vb);
J
j_mayer 已提交
1630 1631 1632
            break;
        case 0x40:
            /* ADDL/V */
1633 1634 1635 1636 1637 1638 1639
            tmp = tcg_temp_new();
            tcg_gen_ext32s_i64(tmp, va);
            tcg_gen_ext32s_i64(vc, vb);
            tcg_gen_add_i64(tmp, tmp, vc);
            tcg_gen_ext32s_i64(vc, tmp);
            gen_helper_check_overflow(cpu_env, vc, tmp);
            tcg_temp_free(tmp);
J
j_mayer 已提交
1640 1641 1642
            break;
        case 0x49:
            /* SUBL/V */
1643 1644 1645 1646 1647 1648 1649
            tmp = tcg_temp_new();
            tcg_gen_ext32s_i64(tmp, va);
            tcg_gen_ext32s_i64(vc, vb);
            tcg_gen_sub_i64(tmp, tmp, vc);
            tcg_gen_ext32s_i64(vc, tmp);
            gen_helper_check_overflow(cpu_env, vc, tmp);
            tcg_temp_free(tmp);
J
j_mayer 已提交
1650 1651 1652
            break;
        case 0x4D:
            /* CMPLT */
1653
            tcg_gen_setcond_i64(TCG_COND_LT, vc, va, vb);
J
j_mayer 已提交
1654 1655 1656
            break;
        case 0x60:
            /* ADDQ/V */
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668
            tmp = tcg_temp_new();
            tmp2 = tcg_temp_new();
            tcg_gen_eqv_i64(tmp, va, vb);
            tcg_gen_mov_i64(tmp2, va);
            tcg_gen_add_i64(vc, va, vb);
            tcg_gen_xor_i64(tmp2, tmp2, vc);
            tcg_gen_and_i64(tmp, tmp, tmp2);
            tcg_gen_shri_i64(tmp, tmp, 63);
            tcg_gen_movi_i64(tmp2, 0);
            gen_helper_check_overflow(cpu_env, tmp, tmp2);
            tcg_temp_free(tmp);
            tcg_temp_free(tmp2);
J
j_mayer 已提交
1669 1670 1671
            break;
        case 0x69:
            /* SUBQ/V */
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
            tmp = tcg_temp_new();
            tmp2 = tcg_temp_new();
            tcg_gen_xor_i64(tmp, va, vb);
            tcg_gen_mov_i64(tmp2, va);
            tcg_gen_sub_i64(vc, va, vb);
            tcg_gen_xor_i64(tmp2, tmp2, vc);
            tcg_gen_and_i64(tmp, tmp, tmp2);
            tcg_gen_shri_i64(tmp, tmp, 63);
            tcg_gen_movi_i64(tmp2, 0);
            gen_helper_check_overflow(cpu_env, tmp, tmp2);
            tcg_temp_free(tmp);
            tcg_temp_free(tmp2);
J
j_mayer 已提交
1684 1685 1686
            break;
        case 0x6D:
            /* CMPLE */
1687
            tcg_gen_setcond_i64(TCG_COND_LE, vc, va, vb);
J
j_mayer 已提交
1688 1689 1690 1691 1692
            break;
        default:
            goto invalid_opc;
        }
        break;
1693

J
j_mayer 已提交
1694
    case 0x11:
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704
        if (fn7 == 0x20) {
            if (rc == 31) {
                /* Special case BIS as NOP.  */
                break;
            }
            if (ra == 31) {
                /* Special case BIS as MOV.  */
                vc = dest_gpr(ctx, rc);
                if (islit) {
                    tcg_gen_movi_i64(vc, lit);
P
Paolo Bonzini 已提交
1705
                } else {
1706
                    tcg_gen_mov_i64(vc, load_gpr(ctx, rb));
P
Paolo Bonzini 已提交
1707
                }
1708
                break;
1709
            }
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
        }

        vc = dest_gpr(ctx, rc);
        vb = load_gpr_lit(ctx, rb, lit, islit);

        if (fn7 == 0x28 && ra == 31) {
            /* Special case ORNOT as NOT.  */
            tcg_gen_not_i64(vc, vb);
            break;
        }

        va = load_gpr(ctx, ra);
        switch (fn7) {
        case 0x00:
            /* AND */
            tcg_gen_and_i64(vc, va, vb);
J
j_mayer 已提交
1726 1727 1728
            break;
        case 0x08:
            /* BIC */
1729
            tcg_gen_andc_i64(vc, va, vb);
J
j_mayer 已提交
1730 1731 1732
            break;
        case 0x14:
            /* CMOVLBS */
1733 1734 1735 1736 1737
            tmp = tcg_temp_new();
            tcg_gen_andi_i64(tmp, va, 1);
            tcg_gen_movcond_i64(TCG_COND_NE, vc, tmp, load_zero(ctx),
                                vb, load_gpr(ctx, rc));
            tcg_temp_free(tmp);
J
j_mayer 已提交
1738 1739 1740
            break;
        case 0x16:
            /* CMOVLBC */
1741 1742 1743 1744 1745
            tmp = tcg_temp_new();
            tcg_gen_andi_i64(tmp, va, 1);
            tcg_gen_movcond_i64(TCG_COND_EQ, vc, tmp, load_zero(ctx),
                                vb, load_gpr(ctx, rc));
            tcg_temp_free(tmp);
J
j_mayer 已提交
1746 1747 1748
            break;
        case 0x20:
            /* BIS */
1749
            tcg_gen_or_i64(vc, va, vb);
J
j_mayer 已提交
1750 1751 1752
            break;
        case 0x24:
            /* CMOVEQ */
1753 1754
            tcg_gen_movcond_i64(TCG_COND_EQ, vc, va, load_zero(ctx),
                                vb, load_gpr(ctx, rc));
J
j_mayer 已提交
1755 1756 1757
            break;
        case 0x26:
            /* CMOVNE */
1758 1759
            tcg_gen_movcond_i64(TCG_COND_NE, vc, va, load_zero(ctx),
                                vb, load_gpr(ctx, rc));
J
j_mayer 已提交
1760 1761 1762
            break;
        case 0x28:
            /* ORNOT */
1763
            tcg_gen_orc_i64(vc, va, vb);
J
j_mayer 已提交
1764 1765 1766
            break;
        case 0x40:
            /* XOR */
1767
            tcg_gen_xor_i64(vc, va, vb);
J
j_mayer 已提交
1768 1769 1770
            break;
        case 0x44:
            /* CMOVLT */
1771 1772
            tcg_gen_movcond_i64(TCG_COND_LT, vc, va, load_zero(ctx),
                                vb, load_gpr(ctx, rc));
J
j_mayer 已提交
1773 1774 1775
            break;
        case 0x46:
            /* CMOVGE */
1776 1777
            tcg_gen_movcond_i64(TCG_COND_GE, vc, va, load_zero(ctx),
                                vb, load_gpr(ctx, rc));
J
j_mayer 已提交
1778 1779 1780
            break;
        case 0x48:
            /* EQV */
1781
            tcg_gen_eqv_i64(vc, va, vb);
J
j_mayer 已提交
1782 1783 1784
            break;
        case 0x61:
            /* AMASK */
1785
            REQUIRE_REG_31(ra);
1786
            {
1787
                uint64_t amask = ctx->tb->flags >> TB_FLAGS_AMASK_SHIFT;
1788
                tcg_gen_andi_i64(vc, vb, ~amask);
1789
            }
J
j_mayer 已提交
1790 1791 1792
            break;
        case 0x64:
            /* CMOVLE */
1793 1794
            tcg_gen_movcond_i64(TCG_COND_LE, vc, va, load_zero(ctx),
                                vb, load_gpr(ctx, rc));
J
j_mayer 已提交
1795 1796 1797
            break;
        case 0x66:
            /* CMOVGT */
1798 1799
            tcg_gen_movcond_i64(TCG_COND_GT, vc, va, load_zero(ctx),
                                vb, load_gpr(ctx, rc));
J
j_mayer 已提交
1800 1801 1802
            break;
        case 0x6C:
            /* IMPLVER */
1803
            REQUIRE_REG_31(ra);
1804
            tcg_gen_movi_i64(vc, ctx->implver);
J
j_mayer 已提交
1805 1806 1807 1808 1809
            break;
        default:
            goto invalid_opc;
        }
        break;
1810

J
j_mayer 已提交
1811
    case 0x12:
1812 1813
        vc = dest_gpr(ctx, rc);
        va = load_gpr(ctx, ra);
J
j_mayer 已提交
1814 1815 1816
        switch (fn7) {
        case 0x02:
            /* MSKBL */
1817
            gen_msk_l(ctx, vc, va, rb, islit, lit, 0x01);
J
j_mayer 已提交
1818 1819 1820
            break;
        case 0x06:
            /* EXTBL */
1821
            gen_ext_l(ctx, vc, va, rb, islit, lit, 0x01);
J
j_mayer 已提交
1822 1823 1824
            break;
        case 0x0B:
            /* INSBL */
1825
            gen_ins_l(ctx, vc, va, rb, islit, lit, 0x01);
J
j_mayer 已提交
1826 1827 1828
            break;
        case 0x12:
            /* MSKWL */
1829
            gen_msk_l(ctx, vc, va, rb, islit, lit, 0x03);
J
j_mayer 已提交
1830 1831 1832
            break;
        case 0x16:
            /* EXTWL */
1833
            gen_ext_l(ctx, vc, va, rb, islit, lit, 0x03);
J
j_mayer 已提交
1834 1835 1836
            break;
        case 0x1B:
            /* INSWL */
1837
            gen_ins_l(ctx, vc, va, rb, islit, lit, 0x03);
J
j_mayer 已提交
1838 1839 1840
            break;
        case 0x22:
            /* MSKLL */
1841
            gen_msk_l(ctx, vc, va, rb, islit, lit, 0x0f);
J
j_mayer 已提交
1842 1843 1844
            break;
        case 0x26:
            /* EXTLL */
1845
            gen_ext_l(ctx, vc, va, rb, islit, lit, 0x0f);
J
j_mayer 已提交
1846 1847 1848
            break;
        case 0x2B:
            /* INSLL */
1849
            gen_ins_l(ctx, vc, va, rb, islit, lit, 0x0f);
J
j_mayer 已提交
1850 1851 1852
            break;
        case 0x30:
            /* ZAP */
1853 1854 1855 1856 1857
            if (islit) {
                gen_zapnoti(vc, va, ~lit);
            } else {
                gen_helper_zap(vc, va, load_gpr(ctx, rb));
            }
J
j_mayer 已提交
1858 1859 1860
            break;
        case 0x31:
            /* ZAPNOT */
1861 1862 1863 1864 1865
            if (islit) {
                gen_zapnoti(vc, va, lit);
            } else {
                gen_helper_zapnot(vc, va, load_gpr(ctx, rb));
            }
J
j_mayer 已提交
1866 1867 1868
            break;
        case 0x32:
            /* MSKQL */
1869
            gen_msk_l(ctx, vc, va, rb, islit, lit, 0xff);
J
j_mayer 已提交
1870 1871 1872
            break;
        case 0x34:
            /* SRL */
1873 1874 1875 1876 1877 1878 1879 1880
            if (islit) {
                tcg_gen_shri_i64(vc, va, lit & 0x3f);
            } else {
                tmp = tcg_temp_new();
                vb = load_gpr(ctx, rb);
                tcg_gen_andi_i64(tmp, vb, 0x3f);
                tcg_gen_shr_i64(vc, va, tmp);
                tcg_temp_free(tmp);
1881
            }
J
j_mayer 已提交
1882 1883 1884
            break;
        case 0x36:
            /* EXTQL */
1885
            gen_ext_l(ctx, vc, va, rb, islit, lit, 0xff);
J
j_mayer 已提交
1886 1887 1888
            break;
        case 0x39:
            /* SLL */
1889 1890 1891 1892 1893 1894 1895 1896
            if (islit) {
                tcg_gen_shli_i64(vc, va, lit & 0x3f);
            } else {
                tmp = tcg_temp_new();
                vb = load_gpr(ctx, rb);
                tcg_gen_andi_i64(tmp, vb, 0x3f);
                tcg_gen_shl_i64(vc, va, tmp);
                tcg_temp_free(tmp);
1897
            }
J
j_mayer 已提交
1898 1899 1900
            break;
        case 0x3B:
            /* INSQL */
1901
            gen_ins_l(ctx, vc, va, rb, islit, lit, 0xff);
J
j_mayer 已提交
1902 1903 1904
            break;
        case 0x3C:
            /* SRA */
1905 1906 1907 1908 1909 1910 1911 1912
            if (islit) {
                tcg_gen_sari_i64(vc, va, lit & 0x3f);
            } else {
                tmp = tcg_temp_new();
                vb = load_gpr(ctx, rb);
                tcg_gen_andi_i64(tmp, vb, 0x3f);
                tcg_gen_sar_i64(vc, va, tmp);
                tcg_temp_free(tmp);
1913
            }
J
j_mayer 已提交
1914 1915 1916
            break;
        case 0x52:
            /* MSKWH */
1917
            gen_msk_h(ctx, vc, va, rb, islit, lit, 0x03);
J
j_mayer 已提交
1918 1919 1920
            break;
        case 0x57:
            /* INSWH */
1921
            gen_ins_h(ctx, vc, va, rb, islit, lit, 0x03);
J
j_mayer 已提交
1922 1923 1924
            break;
        case 0x5A:
            /* EXTWH */
1925
            gen_ext_h(ctx, vc, va, rb, islit, lit, 0x03);
J
j_mayer 已提交
1926 1927 1928
            break;
        case 0x62:
            /* MSKLH */
1929
            gen_msk_h(ctx, vc, va, rb, islit, lit, 0x0f);
J
j_mayer 已提交
1930 1931 1932
            break;
        case 0x67:
            /* INSLH */
1933
            gen_ins_h(ctx, vc, va, rb, islit, lit, 0x0f);
J
j_mayer 已提交
1934 1935 1936
            break;
        case 0x6A:
            /* EXTLH */
1937
            gen_ext_h(ctx, vc, va, rb, islit, lit, 0x0f);
J
j_mayer 已提交
1938 1939 1940
            break;
        case 0x72:
            /* MSKQH */
1941
            gen_msk_h(ctx, vc, va, rb, islit, lit, 0xff);
J
j_mayer 已提交
1942 1943 1944
            break;
        case 0x77:
            /* INSQH */
1945
            gen_ins_h(ctx, vc, va, rb, islit, lit, 0xff);
J
j_mayer 已提交
1946 1947 1948
            break;
        case 0x7A:
            /* EXTQH */
1949
            gen_ext_h(ctx, vc, va, rb, islit, lit, 0xff);
J
j_mayer 已提交
1950 1951 1952 1953 1954
            break;
        default:
            goto invalid_opc;
        }
        break;
1955

J
j_mayer 已提交
1956
    case 0x13:
1957 1958 1959
        vc = dest_gpr(ctx, rc);
        vb = load_gpr_lit(ctx, rb, lit, islit);
        va = load_gpr(ctx, ra);
J
j_mayer 已提交
1960 1961 1962
        switch (fn7) {
        case 0x00:
            /* MULL */
1963 1964
            tcg_gen_mul_i64(vc, va, vb);
            tcg_gen_ext32s_i64(vc, vc);
J
j_mayer 已提交
1965 1966 1967
            break;
        case 0x20:
            /* MULQ */
1968
            tcg_gen_mul_i64(vc, va, vb);
J
j_mayer 已提交
1969 1970 1971
            break;
        case 0x30:
            /* UMULH */
1972 1973 1974
            tmp = tcg_temp_new();
            tcg_gen_mulu2_i64(tmp, vc, va, vb);
            tcg_temp_free(tmp);
J
j_mayer 已提交
1975 1976 1977
            break;
        case 0x40:
            /* MULL/V */
1978 1979 1980 1981 1982 1983 1984
            tmp = tcg_temp_new();
            tcg_gen_ext32s_i64(tmp, va);
            tcg_gen_ext32s_i64(vc, vb);
            tcg_gen_mul_i64(tmp, tmp, vc);
            tcg_gen_ext32s_i64(vc, tmp);
            gen_helper_check_overflow(cpu_env, vc, tmp);
            tcg_temp_free(tmp);
J
j_mayer 已提交
1985 1986 1987
            break;
        case 0x60:
            /* MULQ/V */
1988 1989 1990 1991 1992 1993 1994
            tmp = tcg_temp_new();
            tmp2 = tcg_temp_new();
            tcg_gen_muls2_i64(vc, tmp, va, vb);
            tcg_gen_sari_i64(tmp2, vc, 63);
            gen_helper_check_overflow(cpu_env, tmp, tmp2);
            tcg_temp_free(tmp);
            tcg_temp_free(tmp2);
J
j_mayer 已提交
1995 1996 1997 1998 1999
            break;
        default:
            goto invalid_opc;
        }
        break;
2000

J
j_mayer 已提交
2001
    case 0x14:
2002
        REQUIRE_TB_FLAG(TB_FLAGS_AMASK_FIX);
2003
        vc = dest_fpr(ctx, rc);
2004
        switch (fpfn) { /* fn11 & 0x3F */
J
j_mayer 已提交
2005 2006
        case 0x04:
            /* ITOFS */
2007
            REQUIRE_REG_31(rb);
2008 2009 2010 2011 2012
            t32 = tcg_temp_new_i32();
            va = load_gpr(ctx, ra);
            tcg_gen_trunc_i64_i32(t32, va);
            gen_helper_memory_to_s(vc, t32);
            tcg_temp_free_i32(t32);
J
j_mayer 已提交
2013 2014 2015
            break;
        case 0x0A:
            /* SQRTF */
2016
            REQUIRE_REG_31(ra);
2017 2018
            vb = load_fpr(ctx, rb);
            gen_helper_sqrtf(vc, cpu_env, vb);
2019
            break;
J
j_mayer 已提交
2020 2021
        case 0x0B:
            /* SQRTS */
2022
            REQUIRE_REG_31(ra);
2023
            gen_sqrts(ctx, rb, rc, fn11);
2024
            break;
J
j_mayer 已提交
2025 2026
        case 0x14:
            /* ITOFF */
2027
            REQUIRE_REG_31(rb);
2028 2029 2030 2031 2032
            t32 = tcg_temp_new_i32();
            va = load_gpr(ctx, ra);
            tcg_gen_trunc_i64_i32(t32, va);
            gen_helper_memory_to_f(vc, t32);
            tcg_temp_free_i32(t32);
J
j_mayer 已提交
2033 2034 2035
            break;
        case 0x24:
            /* ITOFT */
2036
            REQUIRE_REG_31(rb);
2037 2038
            va = load_gpr(ctx, ra);
            tcg_gen_mov_i64(vc, va);
J
j_mayer 已提交
2039 2040 2041
            break;
        case 0x2A:
            /* SQRTG */
2042
            REQUIRE_REG_31(ra);
2043 2044
            vb = load_fpr(ctx, rb);
            gen_helper_sqrtg(vc, cpu_env, vb);
2045
            break;
J
j_mayer 已提交
2046 2047
        case 0x02B:
            /* SQRTT */
2048
            REQUIRE_REG_31(ra);
2049
            gen_sqrtt(ctx, rb, rc, fn11);
2050
            break;
J
j_mayer 已提交
2051 2052 2053 2054
        default:
            goto invalid_opc;
        }
        break;
2055

J
j_mayer 已提交
2056 2057 2058
    case 0x15:
        /* VAX floating point */
        /* XXX: rounding mode and trap are ignored (!) */
2059 2060
        vc = dest_fpr(ctx, rc);
        vb = load_fpr(ctx, rb);
2061
        va = load_fpr(ctx, ra);
2062
        switch (fpfn) { /* fn11 & 0x3F */
J
j_mayer 已提交
2063 2064
        case 0x00:
            /* ADDF */
2065
            gen_helper_addf(vc, cpu_env, va, vb);
J
j_mayer 已提交
2066 2067 2068
            break;
        case 0x01:
            /* SUBF */
2069
            gen_helper_subf(vc, cpu_env, va, vb);
J
j_mayer 已提交
2070 2071 2072
            break;
        case 0x02:
            /* MULF */
2073
            gen_helper_mulf(vc, cpu_env, va, vb);
J
j_mayer 已提交
2074 2075 2076
            break;
        case 0x03:
            /* DIVF */
2077
            gen_helper_divf(vc, cpu_env, va, vb);
J
j_mayer 已提交
2078 2079
            break;
        case 0x1E:
2080 2081
            /* CVTDG -- TODO */
            REQUIRE_REG_31(ra);
J
j_mayer 已提交
2082 2083 2084
            goto invalid_opc;
        case 0x20:
            /* ADDG */
2085
            gen_helper_addg(vc, cpu_env, va, vb);
J
j_mayer 已提交
2086 2087 2088
            break;
        case 0x21:
            /* SUBG */
2089
            gen_helper_subg(vc, cpu_env, va, vb);
J
j_mayer 已提交
2090 2091 2092
            break;
        case 0x22:
            /* MULG */
2093
            gen_helper_mulg(vc, cpu_env, va, vb);
J
j_mayer 已提交
2094 2095 2096
            break;
        case 0x23:
            /* DIVG */
2097
            gen_helper_divg(vc, cpu_env, va, vb);
J
j_mayer 已提交
2098 2099 2100
            break;
        case 0x25:
            /* CMPGEQ */
2101
            gen_helper_cmpgeq(vc, cpu_env, va, vb);
J
j_mayer 已提交
2102 2103 2104
            break;
        case 0x26:
            /* CMPGLT */
2105
            gen_helper_cmpglt(vc, cpu_env, va, vb);
J
j_mayer 已提交
2106 2107 2108
            break;
        case 0x27:
            /* CMPGLE */
2109
            gen_helper_cmpgle(vc, cpu_env, va, vb);
J
j_mayer 已提交
2110 2111 2112
            break;
        case 0x2C:
            /* CVTGF */
2113
            REQUIRE_REG_31(ra);
2114
            gen_helper_cvtgf(vc, cpu_env, vb);
J
j_mayer 已提交
2115 2116
            break;
        case 0x2D:
2117 2118
            /* CVTGD -- TODO */
            REQUIRE_REG_31(ra);
J
j_mayer 已提交
2119 2120 2121
            goto invalid_opc;
        case 0x2F:
            /* CVTGQ */
2122
            REQUIRE_REG_31(ra);
2123
            gen_helper_cvtgq(vc, cpu_env, vb);
J
j_mayer 已提交
2124 2125 2126
            break;
        case 0x3C:
            /* CVTQF */
2127
            REQUIRE_REG_31(ra);
2128
            gen_helper_cvtqf(vc, cpu_env, vb);
J
j_mayer 已提交
2129 2130 2131
            break;
        case 0x3E:
            /* CVTQG */
2132
            REQUIRE_REG_31(ra);
2133
            gen_helper_cvtqg(vc, cpu_env, vb);
J
j_mayer 已提交
2134 2135 2136 2137 2138
            break;
        default:
            goto invalid_opc;
        }
        break;
2139

J
j_mayer 已提交
2140 2141
    case 0x16:
        /* IEEE floating-point */
2142
        switch (fpfn) { /* fn11 & 0x3F */
J
j_mayer 已提交
2143 2144
        case 0x00:
            /* ADDS */
2145
            gen_adds(ctx, ra, rb, rc, fn11);
J
j_mayer 已提交
2146 2147 2148
            break;
        case 0x01:
            /* SUBS */
2149
            gen_subs(ctx, ra, rb, rc, fn11);
J
j_mayer 已提交
2150 2151 2152
            break;
        case 0x02:
            /* MULS */
2153
            gen_muls(ctx, ra, rb, rc, fn11);
J
j_mayer 已提交
2154 2155 2156
            break;
        case 0x03:
            /* DIVS */
2157
            gen_divs(ctx, ra, rb, rc, fn11);
J
j_mayer 已提交
2158 2159 2160
            break;
        case 0x20:
            /* ADDT */
2161
            gen_addt(ctx, ra, rb, rc, fn11);
J
j_mayer 已提交
2162 2163 2164
            break;
        case 0x21:
            /* SUBT */
2165
            gen_subt(ctx, ra, rb, rc, fn11);
J
j_mayer 已提交
2166 2167 2168
            break;
        case 0x22:
            /* MULT */
2169
            gen_mult(ctx, ra, rb, rc, fn11);
J
j_mayer 已提交
2170 2171 2172
            break;
        case 0x23:
            /* DIVT */
2173
            gen_divt(ctx, ra, rb, rc, fn11);
J
j_mayer 已提交
2174 2175 2176
            break;
        case 0x24:
            /* CMPTUN */
2177
            gen_cmptun(ctx, ra, rb, rc, fn11);
J
j_mayer 已提交
2178 2179 2180
            break;
        case 0x25:
            /* CMPTEQ */
2181
            gen_cmpteq(ctx, ra, rb, rc, fn11);
J
j_mayer 已提交
2182 2183 2184
            break;
        case 0x26:
            /* CMPTLT */
2185
            gen_cmptlt(ctx, ra, rb, rc, fn11);
J
j_mayer 已提交
2186 2187 2188
            break;
        case 0x27:
            /* CMPTLE */
2189
            gen_cmptle(ctx, ra, rb, rc, fn11);
J
j_mayer 已提交
2190 2191
            break;
        case 0x2C:
2192
            REQUIRE_REG_31(ra);
A
aurel32 已提交
2193
            if (fn11 == 0x2AC || fn11 == 0x6AC) {
J
j_mayer 已提交
2194
                /* CVTST */
2195
                gen_cvtst(ctx, rb, rc, fn11);
J
j_mayer 已提交
2196 2197
            } else {
                /* CVTTS */
2198
                gen_cvtts(ctx, rb, rc, fn11);
J
j_mayer 已提交
2199 2200 2201 2202
            }
            break;
        case 0x2F:
            /* CVTTQ */
2203
            REQUIRE_REG_31(ra);
2204
            gen_cvttq(ctx, rb, rc, fn11);
J
j_mayer 已提交
2205 2206 2207
            break;
        case 0x3C:
            /* CVTQS */
2208
            REQUIRE_REG_31(ra);
2209
            gen_cvtqs(ctx, rb, rc, fn11);
J
j_mayer 已提交
2210 2211 2212
            break;
        case 0x3E:
            /* CVTQT */
2213
            REQUIRE_REG_31(ra);
2214
            gen_cvtqt(ctx, rb, rc, fn11);
J
j_mayer 已提交
2215 2216 2217 2218 2219
            break;
        default:
            goto invalid_opc;
        }
        break;
2220

J
j_mayer 已提交
2221 2222 2223 2224
    case 0x17:
        switch (fn11) {
        case 0x010:
            /* CVTLQ */
2225
            REQUIRE_REG_31(ra);
2226 2227
            vc = dest_fpr(ctx, rc);
            vb = load_fpr(ctx, rb);
2228
            gen_cvtlq(vc, vb);
J
j_mayer 已提交
2229 2230
            break;
        case 0x020:
2231 2232 2233
            /* CPYS */
            if (rc == 31) {
                /* Special case CPYS as FNOP.  */
2234
            } else {
2235
                vc = dest_fpr(ctx, rc);
2236 2237 2238
                va = load_fpr(ctx, ra);
                if (ra == rb) {
                    /* Special case CPYS as FMOV.  */
2239
                    tcg_gen_mov_i64(vc, va);
2240 2241 2242
                } else {
                    vb = load_fpr(ctx, rb);
                    gen_cpy_mask(vc, va, vb, 0, 0x8000000000000000ULL);
R
Richard Henderson 已提交
2243
                }
J
j_mayer 已提交
2244 2245 2246 2247
            }
            break;
        case 0x021:
            /* CPYSN */
2248 2249 2250 2251
            vc = dest_fpr(ctx, rc);
            vb = load_fpr(ctx, rb);
            va = load_fpr(ctx, ra);
            gen_cpy_mask(vc, va, vb, 1, 0x8000000000000000ULL);
J
j_mayer 已提交
2252 2253 2254
            break;
        case 0x022:
            /* CPYSE */
2255 2256 2257 2258
            vc = dest_fpr(ctx, rc);
            vb = load_fpr(ctx, rb);
            va = load_fpr(ctx, ra);
            gen_cpy_mask(vc, va, vb, 0, 0xFFF0000000000000ULL);
J
j_mayer 已提交
2259 2260 2261
            break;
        case 0x024:
            /* MT_FPCR */
2262 2263
            va = load_fpr(ctx, ra);
            gen_helper_store_fpcr(cpu_env, va);
2264 2265 2266 2267 2268
            if (ctx->tb_rm == QUAL_RM_D) {
                /* Re-do the copy of the rounding mode to fp_status
                   the next time we use dynamic rounding.  */
                ctx->tb_rm = -1;
            }
J
j_mayer 已提交
2269 2270 2271
            break;
        case 0x025:
            /* MF_FPCR */
2272 2273
            va = dest_fpr(ctx, ra);
            gen_helper_load_fpcr(va, cpu_env);
J
j_mayer 已提交
2274 2275 2276
            break;
        case 0x02A:
            /* FCMOVEQ */
2277
            gen_fcmov(ctx, TCG_COND_EQ, ra, rb, rc);
J
j_mayer 已提交
2278 2279 2280
            break;
        case 0x02B:
            /* FCMOVNE */
2281
            gen_fcmov(ctx, TCG_COND_NE, ra, rb, rc);
J
j_mayer 已提交
2282 2283 2284
            break;
        case 0x02C:
            /* FCMOVLT */
2285
            gen_fcmov(ctx, TCG_COND_LT, ra, rb, rc);
J
j_mayer 已提交
2286 2287 2288
            break;
        case 0x02D:
            /* FCMOVGE */
2289
            gen_fcmov(ctx, TCG_COND_GE, ra, rb, rc);
J
j_mayer 已提交
2290 2291 2292
            break;
        case 0x02E:
            /* FCMOVLE */
2293
            gen_fcmov(ctx, TCG_COND_LE, ra, rb, rc);
J
j_mayer 已提交
2294 2295 2296
            break;
        case 0x02F:
            /* FCMOVGT */
2297
            gen_fcmov(ctx, TCG_COND_GT, ra, rb, rc);
J
j_mayer 已提交
2298
            break;
2299 2300 2301
        case 0x030: /* CVTQL */
        case 0x130: /* CVTQL/V */
        case 0x530: /* CVTQL/SV */
2302
            REQUIRE_REG_31(ra);
2303 2304
            vc = dest_fpr(ctx, rc);
            vb = load_fpr(ctx, rb);
2305 2306
            gen_helper_cvtql(vc, cpu_env, vb);
            gen_fp_exc_raise(rc, fn11);
J
j_mayer 已提交
2307 2308 2309 2310 2311
            break;
        default:
            goto invalid_opc;
        }
        break;
2312

J
j_mayer 已提交
2313 2314 2315 2316
    case 0x18:
        switch ((uint16_t)disp16) {
        case 0x0000:
            /* TRAPB */
2317
            /* No-op.  */
J
j_mayer 已提交
2318 2319 2320
            break;
        case 0x0400:
            /* EXCB */
2321
            /* No-op.  */
J
j_mayer 已提交
2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340
            break;
        case 0x4000:
            /* MB */
            /* No-op */
            break;
        case 0x4400:
            /* WMB */
            /* No-op */
            break;
        case 0x8000:
            /* FETCH */
            /* No-op */
            break;
        case 0xA000:
            /* FETCH_M */
            /* No-op */
            break;
        case 0xC000:
            /* RPCC */
2341
            va = dest_gpr(ctx, ra);
2342
            if (ctx->tb->cflags & CF_USE_ICOUNT) {
2343 2344 2345 2346 2347 2348
                gen_io_start();
                gen_helper_load_pcc(va, cpu_env);
                gen_io_end();
                ret = EXIT_PC_STALE;
            } else {
                gen_helper_load_pcc(va, cpu_env);
2349
            }
J
j_mayer 已提交
2350 2351 2352
            break;
        case 0xE000:
            /* RC */
2353
            gen_rx(ctx, ra, 0);
J
j_mayer 已提交
2354 2355 2356 2357 2358 2359
            break;
        case 0xE800:
            /* ECB */
            break;
        case 0xF000:
            /* RS */
2360
            gen_rx(ctx, ra, 1);
J
j_mayer 已提交
2361 2362 2363 2364 2365
            break;
        case 0xF800:
            /* WH64 */
            /* No-op */
            break;
2366 2367 2368 2369
        case 0xFC00:
            /* WH64EN */
            /* No-op */
            break;
J
j_mayer 已提交
2370 2371 2372 2373
        default:
            goto invalid_opc;
        }
        break;
2374

J
j_mayer 已提交
2375 2376
    case 0x19:
        /* HW_MFPR (PALcode) */
2377
#ifndef CONFIG_USER_ONLY
2378
        REQUIRE_TB_FLAG(TB_FLAGS_PAL_MODE);
2379
        va = dest_gpr(ctx, ra);
2380
        ret = gen_mfpr(ctx, va, insn & 0xffff);
2381
        break;
2382
#else
J
j_mayer 已提交
2383
        goto invalid_opc;
2384
#endif
2385

J
j_mayer 已提交
2386
    case 0x1A:
2387 2388
        /* JMP, JSR, RET, JSR_COROUTINE.  These only differ by the branch
           prediction stack action, which of course we don't implement.  */
2389 2390
        vb = load_gpr(ctx, rb);
        tcg_gen_andi_i64(cpu_pc, vb, ~3);
2391
        if (ra != 31) {
2392
            tcg_gen_movi_i64(ctx->ir[ra], ctx->pc);
2393
        }
2394
        ret = EXIT_PC_UPDATED;
J
j_mayer 已提交
2395
        break;
2396

J
j_mayer 已提交
2397 2398
    case 0x1B:
        /* HW_LD (PALcode) */
2399
#ifndef CONFIG_USER_ONLY
2400 2401
        REQUIRE_TB_FLAG(TB_FLAGS_PAL_MODE);
        {
2402 2403 2404
            TCGv addr = tcg_temp_new();
            vb = load_gpr(ctx, rb);
            va = dest_gpr(ctx, ra);
2405

2406
            tcg_gen_addi_i64(addr, vb, disp12);
2407 2408
            switch ((insn >> 12) & 0xF) {
            case 0x0:
2409
                /* Longword physical access (hw_ldl/p) */
2410
                gen_helper_ldl_phys(va, cpu_env, addr);
2411 2412
                break;
            case 0x1:
2413
                /* Quadword physical access (hw_ldq/p) */
2414
                gen_helper_ldq_phys(va, cpu_env, addr);
2415 2416
                break;
            case 0x2:
2417
                /* Longword physical access with lock (hw_ldl_l/p) */
2418
                gen_helper_ldl_l_phys(va, cpu_env, addr);
2419 2420
                break;
            case 0x3:
2421
                /* Quadword physical access with lock (hw_ldq_l/p) */
2422
                gen_helper_ldq_l_phys(va, cpu_env, addr);
2423 2424
                break;
            case 0x4:
2425
                /* Longword virtual PTE fetch (hw_ldl/v) */
2426
                goto invalid_opc;
2427
            case 0x5:
2428
                /* Quadword virtual PTE fetch (hw_ldq/v) */
2429
                goto invalid_opc;
2430 2431
                break;
            case 0x6:
2432
                /* Invalid */
2433
                goto invalid_opc;
2434
            case 0x7:
2435
                /* Invaliid */
2436
                goto invalid_opc;
2437
            case 0x8:
2438
                /* Longword virtual access (hw_ldl) */
2439
                goto invalid_opc;
2440
            case 0x9:
2441
                /* Quadword virtual access (hw_ldq) */
2442
                goto invalid_opc;
2443
            case 0xA:
2444
                /* Longword virtual access with protection check (hw_ldl/w) */
2445
                tcg_gen_qemu_ld_i64(va, addr, MMU_KERNEL_IDX, MO_LESL);
2446 2447
                break;
            case 0xB:
2448
                /* Quadword virtual access with protection check (hw_ldq/w) */
2449
                tcg_gen_qemu_ld_i64(va, addr, MMU_KERNEL_IDX, MO_LEQ);
2450 2451
                break;
            case 0xC:
2452
                /* Longword virtual access with alt access mode (hw_ldl/a)*/
2453
                goto invalid_opc;
2454
            case 0xD:
2455
                /* Quadword virtual access with alt access mode (hw_ldq/a) */
2456
                goto invalid_opc;
2457 2458
            case 0xE:
                /* Longword virtual access with alternate access mode and
2459
                   protection checks (hw_ldl/wa) */
2460
                tcg_gen_qemu_ld_i64(va, addr, MMU_USER_IDX, MO_LESL);
2461 2462 2463
                break;
            case 0xF:
                /* Quadword virtual access with alternate access mode and
2464
                   protection checks (hw_ldq/wa) */
2465
                tcg_gen_qemu_ld_i64(va, addr, MMU_USER_IDX, MO_LEQ);
2466 2467 2468
                break;
            }
            tcg_temp_free(addr);
2469
            break;
J
j_mayer 已提交
2470
        }
2471
#else
2472
        goto invalid_opc;
2473
#endif
2474

J
j_mayer 已提交
2475
    case 0x1C:
2476
        vc = dest_gpr(ctx, rc);
2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496
        if (fn7 == 0x70) {
            /* FTOIT */
            REQUIRE_TB_FLAG(TB_FLAGS_AMASK_FIX);
            REQUIRE_REG_31(rb);
            va = load_fpr(ctx, ra);
            tcg_gen_mov_i64(vc, va);
            break;
        } else if (fn7 == 0x78) {
            /* FTOIS */
            REQUIRE_TB_FLAG(TB_FLAGS_AMASK_FIX);
            REQUIRE_REG_31(rb);
            t32 = tcg_temp_new_i32();
            va = load_fpr(ctx, ra);
            gen_helper_s_to_memory(t32, va);
            tcg_gen_ext_i32_i64(vc, t32);
            tcg_temp_free_i32(t32);
            break;
        }

        vb = load_gpr_lit(ctx, rb, lit, islit);
J
j_mayer 已提交
2497 2498 2499
        switch (fn7) {
        case 0x00:
            /* SEXTB */
2500
            REQUIRE_TB_FLAG(TB_FLAGS_AMASK_BWX);
2501
            REQUIRE_REG_31(ra);
2502
            tcg_gen_ext8s_i64(vc, vb);
J
j_mayer 已提交
2503 2504 2505
            break;
        case 0x01:
            /* SEXTW */
2506
            REQUIRE_TB_FLAG(TB_FLAGS_AMASK_BWX);
2507
            REQUIRE_REG_31(ra);
2508
            tcg_gen_ext16s_i64(vc, vb);
2509
            break;
J
j_mayer 已提交
2510 2511
        case 0x30:
            /* CTPOP */
2512
            REQUIRE_TB_FLAG(TB_FLAGS_AMASK_CIX);
2513
            REQUIRE_REG_31(ra);
2514
            REQUIRE_NO_LIT;
2515
            gen_helper_ctpop(vc, vb);
2516
            break;
J
j_mayer 已提交
2517 2518
        case 0x31:
            /* PERR */
2519
            REQUIRE_TB_FLAG(TB_FLAGS_AMASK_MVI);
2520
            REQUIRE_NO_LIT;
2521 2522
            va = load_gpr(ctx, ra);
            gen_helper_perr(vc, va, vb);
2523
            break;
J
j_mayer 已提交
2524 2525
        case 0x32:
            /* CTLZ */
2526
            REQUIRE_TB_FLAG(TB_FLAGS_AMASK_CIX);
2527
            REQUIRE_REG_31(ra);
2528
            REQUIRE_NO_LIT;
2529
            gen_helper_ctlz(vc, vb);
2530
            break;
J
j_mayer 已提交
2531 2532
        case 0x33:
            /* CTTZ */
2533
            REQUIRE_TB_FLAG(TB_FLAGS_AMASK_CIX);
2534
            REQUIRE_REG_31(ra);
2535
            REQUIRE_NO_LIT;
2536
            gen_helper_cttz(vc, vb);
2537
            break;
J
j_mayer 已提交
2538 2539
        case 0x34:
            /* UNPKBW */
2540
            REQUIRE_TB_FLAG(TB_FLAGS_AMASK_MVI);
2541
            REQUIRE_REG_31(ra);
2542
            REQUIRE_NO_LIT;
2543
            gen_helper_unpkbw(vc, vb);
2544
            break;
J
j_mayer 已提交
2545
        case 0x35:
2546
            /* UNPKBL */
2547
            REQUIRE_TB_FLAG(TB_FLAGS_AMASK_MVI);
2548
            REQUIRE_REG_31(ra);
2549
            REQUIRE_NO_LIT;
2550
            gen_helper_unpkbl(vc, vb);
2551
            break;
J
j_mayer 已提交
2552 2553
        case 0x36:
            /* PKWB */
2554
            REQUIRE_TB_FLAG(TB_FLAGS_AMASK_MVI);
2555
            REQUIRE_REG_31(ra);
2556
            REQUIRE_NO_LIT;
2557
            gen_helper_pkwb(vc, vb);
2558
            break;
J
j_mayer 已提交
2559 2560
        case 0x37:
            /* PKLB */
2561
            REQUIRE_TB_FLAG(TB_FLAGS_AMASK_MVI);
2562
            REQUIRE_REG_31(ra);
2563
            REQUIRE_NO_LIT;
2564
            gen_helper_pklb(vc, vb);
2565
            break;
J
j_mayer 已提交
2566 2567
        case 0x38:
            /* MINSB8 */
2568
            REQUIRE_TB_FLAG(TB_FLAGS_AMASK_MVI);
2569 2570
            va = load_gpr(ctx, ra);
            gen_helper_minsb8(vc, va, vb);
2571
            break;
J
j_mayer 已提交
2572 2573
        case 0x39:
            /* MINSW4 */
2574
            REQUIRE_TB_FLAG(TB_FLAGS_AMASK_MVI);
2575 2576
            va = load_gpr(ctx, ra);
            gen_helper_minsw4(vc, va, vb);
2577
            break;
J
j_mayer 已提交
2578 2579
        case 0x3A:
            /* MINUB8 */
2580
            REQUIRE_TB_FLAG(TB_FLAGS_AMASK_MVI);
2581 2582
            va = load_gpr(ctx, ra);
            gen_helper_minub8(vc, va, vb);
2583
            break;
J
j_mayer 已提交
2584 2585
        case 0x3B:
            /* MINUW4 */
2586
            REQUIRE_TB_FLAG(TB_FLAGS_AMASK_MVI);
2587 2588
            va = load_gpr(ctx, ra);
            gen_helper_minuw4(vc, va, vb);
2589
            break;
J
j_mayer 已提交
2590 2591
        case 0x3C:
            /* MAXUB8 */
2592
            REQUIRE_TB_FLAG(TB_FLAGS_AMASK_MVI);
2593 2594
            va = load_gpr(ctx, ra);
            gen_helper_maxub8(vc, va, vb);
2595
            break;
J
j_mayer 已提交
2596 2597
        case 0x3D:
            /* MAXUW4 */
2598
            REQUIRE_TB_FLAG(TB_FLAGS_AMASK_MVI);
2599 2600
            va = load_gpr(ctx, ra);
            gen_helper_maxuw4(vc, va, vb);
2601
            break;
J
j_mayer 已提交
2602 2603
        case 0x3E:
            /* MAXSB8 */
2604
            REQUIRE_TB_FLAG(TB_FLAGS_AMASK_MVI);
2605 2606
            va = load_gpr(ctx, ra);
            gen_helper_maxsb8(vc, va, vb);
2607
            break;
J
j_mayer 已提交
2608 2609
        case 0x3F:
            /* MAXSW4 */
2610
            REQUIRE_TB_FLAG(TB_FLAGS_AMASK_MVI);
2611 2612
            va = load_gpr(ctx, ra);
            gen_helper_maxsw4(vc, va, vb);
J
j_mayer 已提交
2613 2614 2615 2616 2617
            break;
        default:
            goto invalid_opc;
        }
        break;
2618

J
j_mayer 已提交
2619 2620
    case 0x1D:
        /* HW_MTPR (PALcode) */
2621
#ifndef CONFIG_USER_ONLY
2622
        REQUIRE_TB_FLAG(TB_FLAGS_PAL_MODE);
2623
        vb = load_gpr(ctx, rb);
2624 2625
        ret = gen_mtpr(ctx, vb, insn & 0xffff);
        break;
2626
#else
J
j_mayer 已提交
2627
        goto invalid_opc;
2628
#endif
2629

J
j_mayer 已提交
2630
    case 0x1E:
2631
        /* HW_RET (PALcode) */
2632
#ifndef CONFIG_USER_ONLY
2633 2634 2635 2636 2637
        REQUIRE_TB_FLAG(TB_FLAGS_PAL_MODE);
        if (rb == 31) {
            /* Pre-EV6 CPUs interpreted this as HW_REI, loading the return
               address from EXC_ADDR.  This turns out to be useful for our
               emulation PALcode, so continue to accept it.  */
R
Richard Henderson 已提交
2638 2639
            ctx->lit = vb = tcg_temp_new();
            tcg_gen_ld_i64(vb, cpu_env, offsetof(CPUAlphaState, exc_addr));
2640
        } else {
R
Richard Henderson 已提交
2641
            vb = load_gpr(ctx, rb);
J
j_mayer 已提交
2642
        }
R
Richard Henderson 已提交
2643 2644 2645 2646 2647 2648 2649
        tmp = tcg_temp_new();
        tcg_gen_movi_i64(tmp, 0);
        tcg_gen_st8_i64(tmp, cpu_env, offsetof(CPUAlphaState, intr_flag));
        tcg_gen_movi_i64(cpu_lock_addr, -1);
        tcg_gen_andi_i64(tmp, vb, 1);
        tcg_gen_st8_i64(tmp, cpu_env, offsetof(CPUAlphaState, pal_mode));
        tcg_gen_andi_i64(cpu_pc, vb, ~3);
2650 2651 2652
        ret = EXIT_PC_UPDATED;
        break;
#else
2653
        goto invalid_opc;
2654
#endif
2655

J
j_mayer 已提交
2656 2657
    case 0x1F:
        /* HW_ST (PALcode) */
2658
#ifndef CONFIG_USER_ONLY
2659 2660
        REQUIRE_TB_FLAG(TB_FLAGS_PAL_MODE);
        {
2661 2662 2663 2664 2665
            TCGv addr = tcg_temp_new();
            va = load_gpr(ctx, ra);
            vb = load_gpr(ctx, rb);

            tcg_gen_addi_i64(addr, vb, disp12);
2666 2667 2668
            switch ((insn >> 12) & 0xF) {
            case 0x0:
                /* Longword physical access */
2669
                gen_helper_stl_phys(cpu_env, addr, va);
2670 2671 2672
                break;
            case 0x1:
                /* Quadword physical access */
2673
                gen_helper_stq_phys(cpu_env, addr, va);
2674 2675 2676
                break;
            case 0x2:
                /* Longword physical access with lock */
2677
                gen_helper_stl_c_phys(dest_gpr(ctx, ra), cpu_env, addr, va);
2678 2679 2680
                break;
            case 0x3:
                /* Quadword physical access with lock */
2681
                gen_helper_stq_c_phys(dest_gpr(ctx, ra), cpu_env, addr, va);
2682 2683 2684
                break;
            case 0x4:
                /* Longword virtual access */
2685
                goto invalid_opc;
2686 2687
            case 0x5:
                /* Quadword virtual access */
2688
                goto invalid_opc;
2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708
            case 0x6:
                /* Invalid */
                goto invalid_opc;
            case 0x7:
                /* Invalid */
                goto invalid_opc;
            case 0x8:
                /* Invalid */
                goto invalid_opc;
            case 0x9:
                /* Invalid */
                goto invalid_opc;
            case 0xA:
                /* Invalid */
                goto invalid_opc;
            case 0xB:
                /* Invalid */
                goto invalid_opc;
            case 0xC:
                /* Longword virtual access with alternate access mode */
2709
                goto invalid_opc;
2710 2711
            case 0xD:
                /* Quadword virtual access with alternate access mode */
2712
                goto invalid_opc;
2713 2714 2715 2716 2717 2718 2719 2720
            case 0xE:
                /* Invalid */
                goto invalid_opc;
            case 0xF:
                /* Invalid */
                goto invalid_opc;
            }
            tcg_temp_free(addr);
2721
            break;
J
j_mayer 已提交
2722
        }
2723
#else
2724
        goto invalid_opc;
2725
#endif
J
j_mayer 已提交
2726 2727
    case 0x20:
        /* LDF */
A
aurel32 已提交
2728
        gen_load_mem(ctx, &gen_qemu_ldf, ra, rb, disp16, 1, 0);
J
j_mayer 已提交
2729 2730 2731
        break;
    case 0x21:
        /* LDG */
A
aurel32 已提交
2732
        gen_load_mem(ctx, &gen_qemu_ldg, ra, rb, disp16, 1, 0);
J
j_mayer 已提交
2733 2734 2735
        break;
    case 0x22:
        /* LDS */
A
aurel32 已提交
2736
        gen_load_mem(ctx, &gen_qemu_lds, ra, rb, disp16, 1, 0);
J
j_mayer 已提交
2737 2738 2739
        break;
    case 0x23:
        /* LDT */
A
aurel32 已提交
2740
        gen_load_mem(ctx, &tcg_gen_qemu_ld64, ra, rb, disp16, 1, 0);
J
j_mayer 已提交
2741 2742 2743
        break;
    case 0x24:
        /* STF */
2744
        gen_store_mem(ctx, &gen_qemu_stf, ra, rb, disp16, 1, 0);
J
j_mayer 已提交
2745 2746 2747
        break;
    case 0x25:
        /* STG */
2748
        gen_store_mem(ctx, &gen_qemu_stg, ra, rb, disp16, 1, 0);
J
j_mayer 已提交
2749 2750 2751
        break;
    case 0x26:
        /* STS */
2752
        gen_store_mem(ctx, &gen_qemu_sts, ra, rb, disp16, 1, 0);
J
j_mayer 已提交
2753 2754 2755
        break;
    case 0x27:
        /* STT */
2756
        gen_store_mem(ctx, &tcg_gen_qemu_st64, ra, rb, disp16, 1, 0);
J
j_mayer 已提交
2757 2758 2759
        break;
    case 0x28:
        /* LDL */
A
aurel32 已提交
2760
        gen_load_mem(ctx, &tcg_gen_qemu_ld32s, ra, rb, disp16, 0, 0);
J
j_mayer 已提交
2761 2762 2763
        break;
    case 0x29:
        /* LDQ */
A
aurel32 已提交
2764
        gen_load_mem(ctx, &tcg_gen_qemu_ld64, ra, rb, disp16, 0, 0);
J
j_mayer 已提交
2765 2766 2767
        break;
    case 0x2A:
        /* LDL_L */
2768
        gen_load_mem(ctx, &gen_qemu_ldl_l, ra, rb, disp16, 0, 0);
J
j_mayer 已提交
2769 2770 2771
        break;
    case 0x2B:
        /* LDQ_L */
2772
        gen_load_mem(ctx, &gen_qemu_ldq_l, ra, rb, disp16, 0, 0);
J
j_mayer 已提交
2773 2774 2775
        break;
    case 0x2C:
        /* STL */
2776
        gen_store_mem(ctx, &tcg_gen_qemu_st32, ra, rb, disp16, 0, 0);
J
j_mayer 已提交
2777 2778 2779
        break;
    case 0x2D:
        /* STQ */
2780
        gen_store_mem(ctx, &tcg_gen_qemu_st64, ra, rb, disp16, 0, 0);
J
j_mayer 已提交
2781 2782 2783
        break;
    case 0x2E:
        /* STL_C */
2784
        ret = gen_store_conditional(ctx, ra, rb, disp16, 0);
J
j_mayer 已提交
2785 2786 2787
        break;
    case 0x2F:
        /* STQ_C */
2788
        ret = gen_store_conditional(ctx, ra, rb, disp16, 1);
J
j_mayer 已提交
2789 2790 2791
        break;
    case 0x30:
        /* BR */
2792
        ret = gen_bdirect(ctx, ra, disp21);
J
j_mayer 已提交
2793
        break;
P
pbrook 已提交
2794
    case 0x31: /* FBEQ */
2795
        ret = gen_fbcond(ctx, TCG_COND_EQ, ra, disp21);
2796
        break;
P
pbrook 已提交
2797
    case 0x32: /* FBLT */
2798
        ret = gen_fbcond(ctx, TCG_COND_LT, ra, disp21);
2799
        break;
P
pbrook 已提交
2800
    case 0x33: /* FBLE */
2801
        ret = gen_fbcond(ctx, TCG_COND_LE, ra, disp21);
J
j_mayer 已提交
2802 2803 2804
        break;
    case 0x34:
        /* BSR */
2805
        ret = gen_bdirect(ctx, ra, disp21);
J
j_mayer 已提交
2806
        break;
P
pbrook 已提交
2807
    case 0x35: /* FBNE */
2808
        ret = gen_fbcond(ctx, TCG_COND_NE, ra, disp21);
2809
        break;
P
pbrook 已提交
2810
    case 0x36: /* FBGE */
2811
        ret = gen_fbcond(ctx, TCG_COND_GE, ra, disp21);
2812
        break;
P
pbrook 已提交
2813
    case 0x37: /* FBGT */
2814
        ret = gen_fbcond(ctx, TCG_COND_GT, ra, disp21);
J
j_mayer 已提交
2815 2816 2817
        break;
    case 0x38:
        /* BLBC */
2818
        ret = gen_bcond(ctx, TCG_COND_EQ, ra, disp21, 1);
J
j_mayer 已提交
2819 2820 2821
        break;
    case 0x39:
        /* BEQ */
2822
        ret = gen_bcond(ctx, TCG_COND_EQ, ra, disp21, 0);
J
j_mayer 已提交
2823 2824 2825
        break;
    case 0x3A:
        /* BLT */
2826
        ret = gen_bcond(ctx, TCG_COND_LT, ra, disp21, 0);
J
j_mayer 已提交
2827 2828 2829
        break;
    case 0x3B:
        /* BLE */
2830
        ret = gen_bcond(ctx, TCG_COND_LE, ra, disp21, 0);
J
j_mayer 已提交
2831 2832 2833
        break;
    case 0x3C:
        /* BLBS */
2834
        ret = gen_bcond(ctx, TCG_COND_NE, ra, disp21, 1);
J
j_mayer 已提交
2835 2836 2837
        break;
    case 0x3D:
        /* BNE */
2838
        ret = gen_bcond(ctx, TCG_COND_NE, ra, disp21, 0);
J
j_mayer 已提交
2839 2840 2841
        break;
    case 0x3E:
        /* BGE */
2842
        ret = gen_bcond(ctx, TCG_COND_GE, ra, disp21, 0);
J
j_mayer 已提交
2843 2844 2845
        break;
    case 0x3F:
        /* BGT */
2846
        ret = gen_bcond(ctx, TCG_COND_GT, ra, disp21, 0);
J
j_mayer 已提交
2847 2848
        break;
    invalid_opc:
2849
        ret = gen_invalid(ctx);
J
j_mayer 已提交
2850 2851 2852 2853 2854 2855
        break;
    }

    return ret;
}

2856
static inline void gen_intermediate_code_internal(AlphaCPU *cpu,
B
Blue Swirl 已提交
2857
                                                  TranslationBlock *tb,
2858
                                                  bool search_pc)
J
j_mayer 已提交
2859
{
2860
    CPUState *cs = CPU(cpu);
2861
    CPUAlphaState *env = &cpu->env;
J
j_mayer 已提交
2862 2863
    DisasContext ctx, *ctxp = &ctx;
    target_ulong pc_start;
2864
    target_ulong pc_mask;
J
j_mayer 已提交
2865
    uint32_t insn;
2866
    CPUBreakpoint *bp;
J
j_mayer 已提交
2867
    int j, lj = -1;
2868
    ExitStatus ret;
P
pbrook 已提交
2869 2870
    int num_insns;
    int max_insns;
J
j_mayer 已提交
2871 2872

    pc_start = tb->pc;
2873 2874

    ctx.tb = tb;
J
j_mayer 已提交
2875
    ctx.pc = pc_start;
2876
    ctx.mem_idx = cpu_mmu_index(env);
2877
    ctx.implver = env->implver;
2878
    ctx.singlestep_enabled = cs->singlestep_enabled;
2879

2880 2881 2882
#ifdef CONFIG_USER_ONLY
    ctx.ir = cpu_std_ir;
#else
2883
    ctx.palbr = env->palbr;
2884 2885 2886
    ctx.ir = (tb->flags & TB_FLAGS_PAL_MODE ? cpu_pal_ir : cpu_std_ir);
#endif

2887 2888 2889 2890 2891 2892 2893 2894 2895 2896
    /* ??? Every TB begins with unset rounding mode, to be initialized on
       the first fp insn of the TB.  Alternately we could define a proper
       default for every TB (e.g. QUAL_RM_N or QUAL_RM_D) and make sure
       to reset the FP_STATUS to that default at the end of any TB that
       changes the default.  We could even (gasp) dynamiclly figure out
       what default would be most efficient given the running program.  */
    ctx.tb_rm = -1;
    /* Similarly for flush-to-zero.  */
    ctx.tb_ftz = -1;

P
pbrook 已提交
2897 2898
    num_insns = 0;
    max_insns = tb->cflags & CF_COUNT_MASK;
2899
    if (max_insns == 0) {
P
pbrook 已提交
2900
        max_insns = CF_COUNT_MASK;
2901 2902 2903 2904 2905 2906 2907
    }

    if (in_superpage(&ctx, pc_start)) {
        pc_mask = (1ULL << 41) - 1;
    } else {
        pc_mask = ~TARGET_PAGE_MASK;
    }
P
pbrook 已提交
2908

2909
    gen_tb_start(tb);
2910
    do {
2911 2912
        if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) {
            QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
2913
                if (bp->pc == ctx.pc) {
J
j_mayer 已提交
2914 2915 2916 2917 2918 2919
                    gen_excp(&ctx, EXCP_DEBUG, 0);
                    break;
                }
            }
        }
        if (search_pc) {
2920
            j = tcg_op_buf_count();
J
j_mayer 已提交
2921 2922
            if (lj < j) {
                lj++;
2923
                while (lj < j) {
2924
                    tcg_ctx.gen_opc_instr_start[lj++] = 0;
2925
                }
J
j_mayer 已提交
2926
            }
2927
            tcg_ctx.gen_opc_pc[lj] = ctx.pc;
2928
            tcg_ctx.gen_opc_instr_start[lj] = 1;
2929
            tcg_ctx.gen_opc_icount[lj] = num_insns;
J
j_mayer 已提交
2930
        }
P
Paolo Bonzini 已提交
2931
        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO)) {
P
pbrook 已提交
2932
            gen_io_start();
P
Paolo Bonzini 已提交
2933
        }
2934
        insn = cpu_ldl_code(env, ctx.pc);
P
pbrook 已提交
2935
        num_insns++;
2936

2937
	if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
2938 2939 2940
            tcg_gen_debug_insn_start(ctx.pc);
        }

2941 2942 2943 2944
        TCGV_UNUSED_I64(ctx.zero);
        TCGV_UNUSED_I64(ctx.sink);
        TCGV_UNUSED_I64(ctx.lit);

J
j_mayer 已提交
2945 2946
        ctx.pc += 4;
        ret = translate_one(ctxp, insn);
A
aurel32 已提交
2947

2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958
        if (!TCGV_IS_UNUSED_I64(ctx.sink)) {
            tcg_gen_discard_i64(ctx.sink);
            tcg_temp_free(ctx.sink);
        }
        if (!TCGV_IS_UNUSED_I64(ctx.zero)) {
            tcg_temp_free(ctx.zero);
        }
        if (!TCGV_IS_UNUSED_I64(ctx.lit)) {
            tcg_temp_free(ctx.lit);
        }

2959 2960 2961
        /* If we reach a page boundary, are single stepping,
           or exhaust instruction count, stop generation.  */
        if (ret == NO_EXIT
2962
            && ((ctx.pc & pc_mask) == 0
2963
                || tcg_op_buf_full()
2964 2965
                || num_insns >= max_insns
                || singlestep
2966
                || ctx.singlestep_enabled)) {
2967
            ret = EXIT_PC_STALE;
2968
        }
2969 2970 2971 2972
    } while (ret == NO_EXIT);

    if (tb->cflags & CF_LAST_IO) {
        gen_io_end();
J
j_mayer 已提交
2973
    }
2974 2975 2976

    switch (ret) {
    case EXIT_GOTO_TB:
2977
    case EXIT_NORETURN:
2978 2979
        break;
    case EXIT_PC_STALE:
A
aurel32 已提交
2980
        tcg_gen_movi_i64(cpu_pc, ctx.pc);
2981 2982
        /* FALLTHRU */
    case EXIT_PC_UPDATED:
2983
        if (ctx.singlestep_enabled) {
2984 2985 2986 2987
            gen_excp_1(EXCP_DEBUG, 0);
        } else {
            tcg_gen_exit_tb(0);
        }
2988 2989 2990
        break;
    default:
        abort();
J
j_mayer 已提交
2991
    }
2992

2993
    gen_tb_end(tb, num_insns);
2994

J
j_mayer 已提交
2995
    if (search_pc) {
2996
        j = tcg_op_buf_count();
J
j_mayer 已提交
2997
        lj++;
2998
        while (lj <= j) {
2999
            tcg_ctx.gen_opc_instr_start[lj++] = 0;
3000
        }
J
j_mayer 已提交
3001 3002
    } else {
        tb->size = ctx.pc - pc_start;
P
pbrook 已提交
3003
        tb->icount = num_insns;
J
j_mayer 已提交
3004
    }
3005

R
Richard Henderson 已提交
3006
#ifdef DEBUG_DISAS
3007
    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
3008
        qemu_log("IN: %s\n", lookup_symbol(pc_start));
3009
        log_target_disas(cs, pc_start, ctx.pc - pc_start, 1);
3010
        qemu_log("\n");
J
j_mayer 已提交
3011 3012 3013 3014
    }
#endif
}

3015
void gen_intermediate_code (CPUAlphaState *env, struct TranslationBlock *tb)
J
j_mayer 已提交
3016
{
3017
    gen_intermediate_code_internal(alpha_env_get_cpu(env), tb, false);
J
j_mayer 已提交
3018 3019
}

3020
void gen_intermediate_code_pc (CPUAlphaState *env, struct TranslationBlock *tb)
J
j_mayer 已提交
3021
{
3022
    gen_intermediate_code_internal(alpha_env_get_cpu(env), tb, true);
J
j_mayer 已提交
3023 3024
}

3025
void restore_state_to_opc(CPUAlphaState *env, TranslationBlock *tb, int pc_pos)
A
aurel32 已提交
3026
{
3027
    env->pc = tcg_ctx.gen_opc_pc[pc_pos];
A
aurel32 已提交
3028
}