translate.c 185.1 KB
Newer Older
B
bellard 已提交
1 2
/*
 *  MIPS32 emulation for qemu: main translation routines.
3
 *
B
bellard 已提交
4
 *  Copyright (c) 2004-2005 Jocelyn Mayer
B
bellard 已提交
5
 *  Copyright (c) 2006 Marius Groeger (FPU operations)
T
ths 已提交
6
 *  Copyright (c) 2006 Thiemo Seufer (MIPS32R2 support)
B
bellard 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>

#include "cpu.h"
#include "exec-all.h"
#include "disas.h"

33
//#define MIPS_DEBUG_DISAS
34
//#define MIPS_DEBUG_SIGN_EXTENSIONS
B
bellard 已提交
35 36
//#define MIPS_SINGLE_STEP

B
bellard 已提交
37 38 39 40 41 42
#ifdef USE_DIRECT_JUMP
#define TBPARAM(x)
#else
#define TBPARAM(x) (long)(x)
#endif

B
bellard 已提交
43 44 45 46 47 48 49 50 51 52 53 54
enum {
#define DEF(s, n, copy_size) INDEX_op_ ## s,
#include "opc.h"
#undef DEF
    NB_OPS,
};

static uint16_t *gen_opc_ptr;
static uint32_t *gen_opparam_ptr;

#include "gen-op.h"

55 56
/* MIPS major opcodes */
#define MASK_OP_MAJOR(op)  (op & (0x3F << 26))
57 58 59

enum {
    /* indirect opcode tables */
60 61 62 63 64 65 66 67
    OPC_SPECIAL  = (0x00 << 26),
    OPC_REGIMM   = (0x01 << 26),
    OPC_CP0      = (0x10 << 26),
    OPC_CP1      = (0x11 << 26),
    OPC_CP2      = (0x12 << 26),
    OPC_CP3      = (0x13 << 26),
    OPC_SPECIAL2 = (0x1C << 26),
    OPC_SPECIAL3 = (0x1F << 26),
68
    /* arithmetic with immediate */
69 70 71 72 73 74 75 76 77 78
    OPC_ADDI     = (0x08 << 26),
    OPC_ADDIU    = (0x09 << 26),
    OPC_SLTI     = (0x0A << 26),
    OPC_SLTIU    = (0x0B << 26),
    OPC_ANDI     = (0x0C << 26),
    OPC_ORI      = (0x0D << 26),
    OPC_XORI     = (0x0E << 26),
    OPC_LUI      = (0x0F << 26),
    OPC_DADDI    = (0x18 << 26),
    OPC_DADDIU   = (0x19 << 26),
79
    /* Jump and branches */
80 81 82 83 84 85 86 87 88 89 90
    OPC_J        = (0x02 << 26),
    OPC_JAL      = (0x03 << 26),
    OPC_BEQ      = (0x04 << 26),  /* Unconditional if rs = rt = 0 (B) */
    OPC_BEQL     = (0x14 << 26),
    OPC_BNE      = (0x05 << 26),
    OPC_BNEL     = (0x15 << 26),
    OPC_BLEZ     = (0x06 << 26),
    OPC_BLEZL    = (0x16 << 26),
    OPC_BGTZ     = (0x07 << 26),
    OPC_BGTZL    = (0x17 << 26),
    OPC_JALX     = (0x1D << 26),  /* MIPS 16 only */
91
    /* Load and stores */
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    OPC_LDL      = (0x1A << 26),
    OPC_LDR      = (0x1B << 26),
    OPC_LB       = (0x20 << 26),
    OPC_LH       = (0x21 << 26),
    OPC_LWL      = (0x22 << 26),
    OPC_LW       = (0x23 << 26),
    OPC_LBU      = (0x24 << 26),
    OPC_LHU      = (0x25 << 26),
    OPC_LWR      = (0x26 << 26),
    OPC_LWU      = (0x27 << 26),
    OPC_SB       = (0x28 << 26),
    OPC_SH       = (0x29 << 26),
    OPC_SWL      = (0x2A << 26),
    OPC_SW       = (0x2B << 26),
    OPC_SDL      = (0x2C << 26),
    OPC_SDR      = (0x2D << 26),
    OPC_SWR      = (0x2E << 26),
    OPC_LL       = (0x30 << 26),
    OPC_LLD      = (0x34 << 26),
    OPC_LD       = (0x37 << 26),
    OPC_SC       = (0x38 << 26),
    OPC_SCD      = (0x3C << 26),
    OPC_SD       = (0x3F << 26),
115
    /* Floating point load/store */
116 117 118 119 120 121 122 123 124 125
    OPC_LWC1     = (0x31 << 26),
    OPC_LWC2     = (0x32 << 26),
    OPC_LDC1     = (0x35 << 26),
    OPC_LDC2     = (0x36 << 26),
    OPC_SWC1     = (0x39 << 26),
    OPC_SWC2     = (0x3A << 26),
    OPC_SDC1     = (0x3D << 26),
    OPC_SDC2     = (0x3E << 26),
    /* MDMX ASE specific */
    OPC_MDMX     = (0x1E << 26),
126
    /* Cache and prefetch */
127 128 129 130
    OPC_CACHE    = (0x2F << 26),
    OPC_PREF     = (0x33 << 26),
    /* Reserved major opcode */
    OPC_MAJOR3B_RESERVED = (0x3B << 26),
131 132 133
};

/* MIPS special opcodes */
134 135
#define MASK_SPECIAL(op)   MASK_OP_MAJOR(op) | (op & 0x3F)

136 137
enum {
    /* Shifts */
138
    OPC_SLL      = 0x00 | OPC_SPECIAL,
139 140
    /* NOP is SLL r0, r0, 0   */
    /* SSNOP is SLL r0, r0, 1 */
141 142 143 144
    /* EHB is SLL r0, r0, 3 */
    OPC_SRL      = 0x02 | OPC_SPECIAL, /* also ROTR */
    OPC_SRA      = 0x03 | OPC_SPECIAL,
    OPC_SLLV     = 0x04 | OPC_SPECIAL,
145
    OPC_SRLV     = 0x06 | OPC_SPECIAL, /* also ROTRV */
146 147 148 149 150 151 152 153 154 155
    OPC_SRAV     = 0x07 | OPC_SPECIAL,
    OPC_DSLLV    = 0x14 | OPC_SPECIAL,
    OPC_DSRLV    = 0x16 | OPC_SPECIAL, /* also DROTRV */
    OPC_DSRAV    = 0x17 | OPC_SPECIAL,
    OPC_DSLL     = 0x38 | OPC_SPECIAL,
    OPC_DSRL     = 0x3A | OPC_SPECIAL, /* also DROTR */
    OPC_DSRA     = 0x3B | OPC_SPECIAL,
    OPC_DSLL32   = 0x3C | OPC_SPECIAL,
    OPC_DSRL32   = 0x3E | OPC_SPECIAL, /* also DROTR32 */
    OPC_DSRA32   = 0x3F | OPC_SPECIAL,
156
    /* Multiplication / division */
157 158 159 160 161 162 163 164
    OPC_MULT     = 0x18 | OPC_SPECIAL,
    OPC_MULTU    = 0x19 | OPC_SPECIAL,
    OPC_DIV      = 0x1A | OPC_SPECIAL,
    OPC_DIVU     = 0x1B | OPC_SPECIAL,
    OPC_DMULT    = 0x1C | OPC_SPECIAL,
    OPC_DMULTU   = 0x1D | OPC_SPECIAL,
    OPC_DDIV     = 0x1E | OPC_SPECIAL,
    OPC_DDIVU    = 0x1F | OPC_SPECIAL,
165
    /* 2 registers arithmetic / logic */
166 167 168 169 170 171 172 173 174 175 176 177 178 179
    OPC_ADD      = 0x20 | OPC_SPECIAL,
    OPC_ADDU     = 0x21 | OPC_SPECIAL,
    OPC_SUB      = 0x22 | OPC_SPECIAL,
    OPC_SUBU     = 0x23 | OPC_SPECIAL,
    OPC_AND      = 0x24 | OPC_SPECIAL,
    OPC_OR       = 0x25 | OPC_SPECIAL,
    OPC_XOR      = 0x26 | OPC_SPECIAL,
    OPC_NOR      = 0x27 | OPC_SPECIAL,
    OPC_SLT      = 0x2A | OPC_SPECIAL,
    OPC_SLTU     = 0x2B | OPC_SPECIAL,
    OPC_DADD     = 0x2C | OPC_SPECIAL,
    OPC_DADDU    = 0x2D | OPC_SPECIAL,
    OPC_DSUB     = 0x2E | OPC_SPECIAL,
    OPC_DSUBU    = 0x2F | OPC_SPECIAL,
180
    /* Jumps */
181 182
    OPC_JR       = 0x08 | OPC_SPECIAL, /* Also JR.HB */
    OPC_JALR     = 0x09 | OPC_SPECIAL, /* Also JALR.HB */
183
    /* Traps */
184 185 186 187 188 189
    OPC_TGE      = 0x30 | OPC_SPECIAL,
    OPC_TGEU     = 0x31 | OPC_SPECIAL,
    OPC_TLT      = 0x32 | OPC_SPECIAL,
    OPC_TLTU     = 0x33 | OPC_SPECIAL,
    OPC_TEQ      = 0x34 | OPC_SPECIAL,
    OPC_TNE      = 0x36 | OPC_SPECIAL,
190
    /* HI / LO registers load & stores */
191 192 193 194
    OPC_MFHI     = 0x10 | OPC_SPECIAL,
    OPC_MTHI     = 0x11 | OPC_SPECIAL,
    OPC_MFLO     = 0x12 | OPC_SPECIAL,
    OPC_MTLO     = 0x13 | OPC_SPECIAL,
195
    /* Conditional moves */
196 197
    OPC_MOVZ     = 0x0A | OPC_SPECIAL,
    OPC_MOVN     = 0x0B | OPC_SPECIAL,
198

199
    OPC_MOVCI    = 0x01 | OPC_SPECIAL,
200 201

    /* Special */
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    OPC_PMON     = 0x05 | OPC_SPECIAL, /* inofficial */
    OPC_SYSCALL  = 0x0C | OPC_SPECIAL,
    OPC_BREAK    = 0x0D | OPC_SPECIAL,
    OPC_SPIM     = 0x0E | OPC_SPECIAL, /* inofficial */
    OPC_SYNC     = 0x0F | OPC_SPECIAL,

    OPC_SPECIAL15_RESERVED = 0x15 | OPC_SPECIAL,
    OPC_SPECIAL28_RESERVED = 0x28 | OPC_SPECIAL,
    OPC_SPECIAL29_RESERVED = 0x29 | OPC_SPECIAL,
    OPC_SPECIAL35_RESERVED = 0x35 | OPC_SPECIAL,
    OPC_SPECIAL37_RESERVED = 0x37 | OPC_SPECIAL,
    OPC_SPECIAL39_RESERVED = 0x39 | OPC_SPECIAL,
    OPC_SPECIAL3D_RESERVED = 0x3D | OPC_SPECIAL,
};

/* REGIMM (rt field) opcodes */
#define MASK_REGIMM(op)    MASK_OP_MAJOR(op) | (op & (0x1F << 16))

enum {
    OPC_BLTZ     = (0x00 << 16) | OPC_REGIMM,
    OPC_BLTZL    = (0x02 << 16) | OPC_REGIMM,
    OPC_BGEZ     = (0x01 << 16) | OPC_REGIMM,
    OPC_BGEZL    = (0x03 << 16) | OPC_REGIMM,
    OPC_BLTZAL   = (0x10 << 16) | OPC_REGIMM,
    OPC_BLTZALL  = (0x12 << 16) | OPC_REGIMM,
    OPC_BGEZAL   = (0x11 << 16) | OPC_REGIMM,
    OPC_BGEZALL  = (0x13 << 16) | OPC_REGIMM,
    OPC_TGEI     = (0x08 << 16) | OPC_REGIMM,
    OPC_TGEIU    = (0x09 << 16) | OPC_REGIMM,
    OPC_TLTI     = (0x0A << 16) | OPC_REGIMM,
    OPC_TLTIU    = (0x0B << 16) | OPC_REGIMM,
    OPC_TEQI     = (0x0C << 16) | OPC_REGIMM,
    OPC_TNEI     = (0x0E << 16) | OPC_REGIMM,
    OPC_SYNCI    = (0x1F << 16) | OPC_REGIMM,
236 237
};

238 239 240
/* Special2 opcodes */
#define MASK_SPECIAL2(op)  MASK_OP_MAJOR(op) | (op & 0x3F)

241
enum {
242 243 244 245 246 247
    /* Multiply & xxx operations */
    OPC_MADD     = 0x00 | OPC_SPECIAL2,
    OPC_MADDU    = 0x01 | OPC_SPECIAL2,
    OPC_MUL      = 0x02 | OPC_SPECIAL2,
    OPC_MSUB     = 0x04 | OPC_SPECIAL2,
    OPC_MSUBU    = 0x05 | OPC_SPECIAL2,
248
    /* Misc */
249 250 251 252
    OPC_CLZ      = 0x20 | OPC_SPECIAL2,
    OPC_CLO      = 0x21 | OPC_SPECIAL2,
    OPC_DCLZ     = 0x24 | OPC_SPECIAL2,
    OPC_DCLO     = 0x25 | OPC_SPECIAL2,
253
    /* Special */
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    OPC_SDBBP    = 0x3F | OPC_SPECIAL2,
};

/* Special3 opcodes */
#define MASK_SPECIAL3(op)  MASK_OP_MAJOR(op) | (op & 0x3F)

enum {
    OPC_EXT      = 0x00 | OPC_SPECIAL3,
    OPC_DEXTM    = 0x01 | OPC_SPECIAL3,
    OPC_DEXTU    = 0x02 | OPC_SPECIAL3,
    OPC_DEXT     = 0x03 | OPC_SPECIAL3,
    OPC_INS      = 0x04 | OPC_SPECIAL3,
    OPC_DINSM    = 0x05 | OPC_SPECIAL3,
    OPC_DINSU    = 0x06 | OPC_SPECIAL3,
    OPC_DINS     = 0x07 | OPC_SPECIAL3,
269 270
    OPC_FORK     = 0x08 | OPC_SPECIAL3,
    OPC_YIELD    = 0x09 | OPC_SPECIAL3,
271 272 273
    OPC_BSHFL    = 0x20 | OPC_SPECIAL3,
    OPC_DBSHFL   = 0x24 | OPC_SPECIAL3,
    OPC_RDHWR    = 0x3B | OPC_SPECIAL3,
274 275
};

276 277 278
/* BSHFL opcodes */
#define MASK_BSHFL(op)     MASK_SPECIAL3(op) | (op & (0x1F << 6))

279
enum {
280 281 282
    OPC_WSBH     = (0x02 << 6) | OPC_BSHFL,
    OPC_SEB      = (0x10 << 6) | OPC_BSHFL,
    OPC_SEH      = (0x18 << 6) | OPC_BSHFL,
283 284
};

285 286 287
/* DBSHFL opcodes */
#define MASK_DBSHFL(op)    MASK_SPECIAL3(op) | (op & (0x1F << 6))

288
enum {
289 290
    OPC_DSBH     = (0x02 << 6) | OPC_DBSHFL,
    OPC_DSHD     = (0x05 << 6) | OPC_DBSHFL,
291 292
};

293 294 295
/* Coprocessor 0 (rs field) */
#define MASK_CP0(op)       MASK_OP_MAJOR(op) | (op & (0x1F << 21))

B
bellard 已提交
296
enum {
297 298 299 300
    OPC_MFC0     = (0x00 << 21) | OPC_CP0,
    OPC_DMFC0    = (0x01 << 21) | OPC_CP0,
    OPC_MTC0     = (0x04 << 21) | OPC_CP0,
    OPC_DMTC0    = (0x05 << 21) | OPC_CP0,
301
    OPC_MFTR     = (0x08 << 21) | OPC_CP0,
302 303
    OPC_RDPGPR   = (0x0A << 21) | OPC_CP0,
    OPC_MFMC0    = (0x0B << 21) | OPC_CP0,
304
    OPC_MTTR     = (0x0C << 21) | OPC_CP0,
305 306 307 308
    OPC_WRPGPR   = (0x0E << 21) | OPC_CP0,
    OPC_C0       = (0x10 << 21) | OPC_CP0,
    OPC_C0_FIRST = (0x10 << 21) | OPC_CP0,
    OPC_C0_LAST  = (0x1F << 21) | OPC_CP0,
B
bellard 已提交
309
};
310 311

/* MFMC0 opcodes */
312
#define MASK_MFMC0(op)     MASK_CP0(op) | (op & 0xFFFF)
313 314

enum {
315 316 317 318
    OPC_DMT      = 0x01 | (0 << 5) | (0x0F << 6) | (0x01 << 11) | OPC_MFMC0,
    OPC_EMT      = 0x01 | (1 << 5) | (0x0F << 6) | (0x01 << 11) | OPC_MFMC0,
    OPC_DVPE     = 0x01 | (0 << 5) | OPC_MFMC0,
    OPC_EVPE     = 0x01 | (1 << 5) | OPC_MFMC0,
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    OPC_DI       = (0 << 5) | (0x0C << 11) | OPC_MFMC0,
    OPC_EI       = (1 << 5) | (0x0C << 11) | OPC_MFMC0,
};

/* Coprocessor 0 (with rs == C0) */
#define MASK_C0(op)        MASK_CP0(op) | (op & 0x3F)

enum {
    OPC_TLBR     = 0x01 | OPC_C0,
    OPC_TLBWI    = 0x02 | OPC_C0,
    OPC_TLBWR    = 0x06 | OPC_C0,
    OPC_TLBP     = 0x08 | OPC_C0,
    OPC_RFE      = 0x10 | OPC_C0,
    OPC_ERET     = 0x18 | OPC_C0,
    OPC_DERET    = 0x1F | OPC_C0,
    OPC_WAIT     = 0x20 | OPC_C0,
};

/* Coprocessor 1 (rs field) */
#define MASK_CP1(op)       MASK_OP_MAJOR(op) | (op & (0x1F << 21))

enum {
    OPC_MFC1     = (0x00 << 21) | OPC_CP1,
    OPC_DMFC1    = (0x01 << 21) | OPC_CP1,
    OPC_CFC1     = (0x02 << 21) | OPC_CP1,
344
    OPC_MFHC1    = (0x03 << 21) | OPC_CP1,
345 346 347
    OPC_MTC1     = (0x04 << 21) | OPC_CP1,
    OPC_DMTC1    = (0x05 << 21) | OPC_CP1,
    OPC_CTC1     = (0x06 << 21) | OPC_CP1,
348
    OPC_MTHC1    = (0x07 << 21) | OPC_CP1,
349
    OPC_BC1      = (0x08 << 21) | OPC_CP1, /* bc */
350 351
    OPC_BC1ANY2  = (0x09 << 21) | OPC_CP1,
    OPC_BC1ANY4  = (0x0A << 21) | OPC_CP1,
352 353 354 355 356 357
    OPC_S_FMT    = (0x10 << 21) | OPC_CP1, /* 16: fmt=single fp */
    OPC_D_FMT    = (0x11 << 21) | OPC_CP1, /* 17: fmt=double fp */
    OPC_E_FMT    = (0x12 << 21) | OPC_CP1, /* 18: fmt=extended fp */
    OPC_Q_FMT    = (0x13 << 21) | OPC_CP1, /* 19: fmt=quad fp */
    OPC_W_FMT    = (0x14 << 21) | OPC_CP1, /* 20: fmt=32bit fixed */
    OPC_L_FMT    = (0x15 << 21) | OPC_CP1, /* 21: fmt=64bit fixed */
358
    OPC_PS_FMT   = (0x16 << 21) | OPC_CP1, /* 22: fmt=paired single fp */
359 360
};

361 362 363
#define MASK_CP1_FUNC(op)       MASK_CP1(op) | (op & 0x3F)
#define MASK_BC1(op)            MASK_CP1(op) | (op & (0x3 << 16))

364 365 366 367 368 369 370
enum {
    OPC_BC1F     = (0x00 << 16) | OPC_BC1,
    OPC_BC1T     = (0x01 << 16) | OPC_BC1,
    OPC_BC1FL    = (0x02 << 16) | OPC_BC1,
    OPC_BC1TL    = (0x03 << 16) | OPC_BC1,
};

371 372 373 374 375 376 377 378 379
enum {
    OPC_BC1FANY2     = (0x00 << 16) | OPC_BC1ANY2,
    OPC_BC1TANY2     = (0x01 << 16) | OPC_BC1ANY2,
};

enum {
    OPC_BC1FANY4     = (0x00 << 16) | OPC_BC1ANY4,
    OPC_BC1TANY4     = (0x01 << 16) | OPC_BC1ANY4,
};
380 381

#define MASK_CP2(op)       MASK_OP_MAJOR(op) | (op & (0x1F << 21))
T
ths 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412

enum {
    OPC_MFC2    = (0x00 << 21) | OPC_CP2,
    OPC_DMFC2   = (0x01 << 21) | OPC_CP2,
    OPC_CFC2    = (0x02 << 21) | OPC_CP2,
    OPC_MFHC2   = (0x03 << 21) | OPC_CP2,
    OPC_MTC2    = (0x04 << 21) | OPC_CP2,
    OPC_DMTC2   = (0x05 << 21) | OPC_CP2,
    OPC_CTC2    = (0x06 << 21) | OPC_CP2,
    OPC_MTHC2   = (0x07 << 21) | OPC_CP2,
    OPC_BC2     = (0x08 << 21) | OPC_CP2,
};

#define MASK_CP3(op)       MASK_OP_MAJOR(op) | (op & 0x3F)

enum {
    OPC_LWXC1   = 0x00 | OPC_CP3,
    OPC_LDXC1   = 0x01 | OPC_CP3,
    OPC_LUXC1   = 0x05 | OPC_CP3,
    OPC_SWXC1   = 0x08 | OPC_CP3,
    OPC_SDXC1   = 0x09 | OPC_CP3,
    OPC_SUXC1   = 0x0D | OPC_CP3,
    OPC_PREFX   = 0x0F | OPC_CP3,
    OPC_ALNV_PS = 0x1E | OPC_CP3,
    OPC_MADD_S  = 0x20 | OPC_CP3,
    OPC_MADD_D  = 0x21 | OPC_CP3,
    OPC_MADD_PS = 0x26 | OPC_CP3,
    OPC_MSUB_S  = 0x28 | OPC_CP3,
    OPC_MSUB_D  = 0x29 | OPC_CP3,
    OPC_MSUB_PS = 0x2E | OPC_CP3,
    OPC_NMADD_S = 0x30 | OPC_CP3,
413
    OPC_NMADD_D = 0x31 | OPC_CP3,
T
ths 已提交
414 415 416 417 418 419
    OPC_NMADD_PS= 0x36 | OPC_CP3,
    OPC_NMSUB_S = 0x38 | OPC_CP3,
    OPC_NMSUB_D = 0x39 | OPC_CP3,
    OPC_NMSUB_PS= 0x3E | OPC_CP3,
};

B
bellard 已提交
420

B
bellard 已提交
421 422 423 424 425 426 427
const unsigned char *regnames[] =
    { "r0", "at", "v0", "v1", "a0", "a1", "a2", "a3",
      "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
      "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
      "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra", };

/* Warning: no function for r0 register (hard wired to zero) */
428 429 430 431 432 433 434 435 436 437 438
#define GEN32(func, NAME)                        \
static GenOpFunc *NAME ## _table [32] = {        \
NULL,       NAME ## 1, NAME ## 2, NAME ## 3,     \
NAME ## 4,  NAME ## 5, NAME ## 6, NAME ## 7,     \
NAME ## 8,  NAME ## 9, NAME ## 10, NAME ## 11,   \
NAME ## 12, NAME ## 13, NAME ## 14, NAME ## 15,  \
NAME ## 16, NAME ## 17, NAME ## 18, NAME ## 19,  \
NAME ## 20, NAME ## 21, NAME ## 22, NAME ## 23,  \
NAME ## 24, NAME ## 25, NAME ## 26, NAME ## 27,  \
NAME ## 28, NAME ## 29, NAME ## 30, NAME ## 31,  \
};                                               \
439
static always_inline void func(int n)            \
440 441
{                                                \
    NAME ## _table[n]();                         \
B
bellard 已提交
442 443 444 445 446 447 448 449 450 451
}

/* General purpose registers moves */
GEN32(gen_op_load_gpr_T0, gen_op_load_gpr_T0_gpr);
GEN32(gen_op_load_gpr_T1, gen_op_load_gpr_T1_gpr);
GEN32(gen_op_load_gpr_T2, gen_op_load_gpr_T2_gpr);

GEN32(gen_op_store_T0_gpr, gen_op_store_T0_gpr_gpr);
GEN32(gen_op_store_T1_gpr, gen_op_store_T1_gpr_gpr);

452 453 454 455
/* Moves to/from shadow registers */
GEN32(gen_op_load_srsgpr_T0, gen_op_load_srsgpr_T0_gpr);
GEN32(gen_op_store_T0_srsgpr, gen_op_store_T0_srsgpr_gpr);

456
static const char *fregnames[] =
B
bellard 已提交
457 458 459 460 461
    { "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", "f31", };

462 463 464 465 466 467 468 469 470 471 472
#define FGEN32(func, NAME)                       \
static GenOpFunc *NAME ## _table [32] = {        \
NAME ## 0,  NAME ## 1,  NAME ## 2,  NAME ## 3,   \
NAME ## 4,  NAME ## 5,  NAME ## 6,  NAME ## 7,   \
NAME ## 8,  NAME ## 9,  NAME ## 10, NAME ## 11,  \
NAME ## 12, NAME ## 13, NAME ## 14, NAME ## 15,  \
NAME ## 16, NAME ## 17, NAME ## 18, NAME ## 19,  \
NAME ## 20, NAME ## 21, NAME ## 22, NAME ## 23,  \
NAME ## 24, NAME ## 25, NAME ## 26, NAME ## 27,  \
NAME ## 28, NAME ## 29, NAME ## 30, NAME ## 31,  \
};                                               \
473
static always_inline void func(int n)            \
474 475
{                                                \
    NAME ## _table[n]();                         \
B
bellard 已提交
476 477
}

478 479 480 481 482
FGEN32(gen_op_load_fpr_WT0,  gen_op_load_fpr_WT0_fpr);
FGEN32(gen_op_store_fpr_WT0, gen_op_store_fpr_WT0_fpr);

FGEN32(gen_op_load_fpr_WT1,  gen_op_load_fpr_WT1_fpr);
FGEN32(gen_op_store_fpr_WT1, gen_op_store_fpr_WT1_fpr);
B
bellard 已提交
483

484 485
FGEN32(gen_op_load_fpr_WT2,  gen_op_load_fpr_WT2_fpr);
FGEN32(gen_op_store_fpr_WT2, gen_op_store_fpr_WT2_fpr);
B
bellard 已提交
486

487 488
FGEN32(gen_op_load_fpr_DT0,  gen_op_load_fpr_DT0_fpr);
FGEN32(gen_op_store_fpr_DT0, gen_op_store_fpr_DT0_fpr);
B
bellard 已提交
489

490 491
FGEN32(gen_op_load_fpr_DT1,  gen_op_load_fpr_DT1_fpr);
FGEN32(gen_op_store_fpr_DT1, gen_op_store_fpr_DT1_fpr);
B
bellard 已提交
492

493 494
FGEN32(gen_op_load_fpr_DT2,  gen_op_load_fpr_DT2_fpr);
FGEN32(gen_op_store_fpr_DT2, gen_op_store_fpr_DT2_fpr);
B
bellard 已提交
495

496 497
FGEN32(gen_op_load_fpr_WTH0,  gen_op_load_fpr_WTH0_fpr);
FGEN32(gen_op_store_fpr_WTH0, gen_op_store_fpr_WTH0_fpr);
B
bellard 已提交
498

499 500 501 502 503
FGEN32(gen_op_load_fpr_WTH1,  gen_op_load_fpr_WTH1_fpr);
FGEN32(gen_op_store_fpr_WTH1, gen_op_store_fpr_WTH1_fpr);

FGEN32(gen_op_load_fpr_WTH2,  gen_op_load_fpr_WTH2_fpr);
FGEN32(gen_op_store_fpr_WTH2, gen_op_store_fpr_WTH2_fpr);
B
bellard 已提交
504

505
#define FOP_CONDS(type, fmt)                                            \
506
static GenOpFunc1 * gen_op_cmp ## type ## _ ## fmt ## _table[16] = {    \
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
    gen_op_cmp ## type ## _ ## fmt ## _f,                               \
    gen_op_cmp ## type ## _ ## fmt ## _un,                              \
    gen_op_cmp ## type ## _ ## fmt ## _eq,                              \
    gen_op_cmp ## type ## _ ## fmt ## _ueq,                             \
    gen_op_cmp ## type ## _ ## fmt ## _olt,                             \
    gen_op_cmp ## type ## _ ## fmt ## _ult,                             \
    gen_op_cmp ## type ## _ ## fmt ## _ole,                             \
    gen_op_cmp ## type ## _ ## fmt ## _ule,                             \
    gen_op_cmp ## type ## _ ## fmt ## _sf,                              \
    gen_op_cmp ## type ## _ ## fmt ## _ngle,                            \
    gen_op_cmp ## type ## _ ## fmt ## _seq,                             \
    gen_op_cmp ## type ## _ ## fmt ## _ngl,                             \
    gen_op_cmp ## type ## _ ## fmt ## _lt,                              \
    gen_op_cmp ## type ## _ ## fmt ## _nge,                             \
    gen_op_cmp ## type ## _ ## fmt ## _le,                              \
    gen_op_cmp ## type ## _ ## fmt ## _ngt,                             \
B
bellard 已提交
523
};                                                                      \
524
static always_inline void gen_cmp ## type ## _ ## fmt(int n, long cc)   \
B
bellard 已提交
525
{                                                                       \
526
    gen_op_cmp ## type ## _ ## fmt ## _table[n](cc);                    \
B
bellard 已提交
527 528
}

529 530 531 532 533 534
FOP_CONDS(, d)
FOP_CONDS(abs, d)
FOP_CONDS(, s)
FOP_CONDS(abs, s)
FOP_CONDS(, ps)
FOP_CONDS(abs, ps)
B
bellard 已提交
535

B
bellard 已提交
536 537 538 539
typedef struct DisasContext {
    struct TranslationBlock *tb;
    target_ulong pc, saved_pc;
    uint32_t opcode;
540
    uint32_t fp_status;
B
bellard 已提交
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
    /* Routine used to access memory */
    int mem_idx;
    uint32_t hflags, saved_hflags;
    int bstate;
    target_ulong btarget;
} DisasContext;

enum {
    BS_NONE     = 0, /* We go out of the TB without reaching a branch or an
                      * exception condition
                      */
    BS_STOP     = 1, /* We want to stop translation for any reason */
    BS_BRANCH   = 2, /* We reached a branch condition     */
    BS_EXCP     = 3, /* We reached an exception condition */
};

557
#ifdef MIPS_DEBUG_DISAS
B
bellard 已提交
558 559 560
#define MIPS_DEBUG(fmt, args...)                                              \
do {                                                                          \
    if (loglevel & CPU_LOG_TB_IN_ASM) {                                       \
T
ths 已提交
561
        fprintf(logfile, TARGET_FMT_lx ": %08x " fmt "\n",                    \
B
bellard 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
                ctx->pc, ctx->opcode , ##args);                               \
    }                                                                         \
} while (0)
#else
#define MIPS_DEBUG(fmt, args...) do { } while(0)
#endif

#define MIPS_INVAL(op)                                                        \
do {                                                                          \
    MIPS_DEBUG("Invalid %s %03x %03x %03x", op, ctx->opcode >> 26,            \
               ctx->opcode & 0x3F, ((ctx->opcode >> 16) & 0x1F));             \
} while (0)

#define GEN_LOAD_REG_TN(Tn, Rn)                                               \
do {                                                                          \
    if (Rn == 0) {                                                            \
        glue(gen_op_reset_, Tn)();                                            \
    } else {                                                                  \
        glue(gen_op_load_gpr_, Tn)(Rn);                                       \
    }                                                                         \
} while (0)

584 585 586 587 588 589 590 591 592
#define GEN_LOAD_SRSREG_TN(Tn, Rn)                                            \
do {                                                                          \
    if (Rn == 0) {                                                            \
        glue(gen_op_reset_, Tn)();                                            \
    } else {                                                                  \
        glue(gen_op_load_srsgpr_, Tn)(Rn);                                    \
    }                                                                         \
} while (0)

593
#if defined(TARGET_MIPS64)
594 595 596 597 598 599 600 601 602 603 604
#define GEN_LOAD_IMM_TN(Tn, Imm)                                              \
do {                                                                          \
    if (Imm == 0) {                                                           \
        glue(gen_op_reset_, Tn)();                                            \
    } else if ((int32_t)Imm == Imm) {                                         \
        glue(gen_op_set_, Tn)(Imm);                                           \
    } else {                                                                  \
        glue(gen_op_set64_, Tn)(((uint64_t)Imm) >> 32, (uint32_t)Imm);        \
    }                                                                         \
} while (0)
#else
B
bellard 已提交
605 606 607 608 609 610 611 612
#define GEN_LOAD_IMM_TN(Tn, Imm)                                              \
do {                                                                          \
    if (Imm == 0) {                                                           \
        glue(gen_op_reset_, Tn)();                                            \
    } else {                                                                  \
        glue(gen_op_set_, Tn)(Imm);                                           \
    }                                                                         \
} while (0)
613
#endif
B
bellard 已提交
614 615 616 617 618 619 620 621

#define GEN_STORE_TN_REG(Rn, Tn)                                              \
do {                                                                          \
    if (Rn != 0) {                                                            \
        glue(glue(gen_op_store_, Tn),_gpr)(Rn);                               \
    }                                                                         \
} while (0)

622 623 624 625 626 627 628
#define GEN_STORE_TN_SRSREG(Rn, Tn)                                           \
do {                                                                          \
    if (Rn != 0) {                                                            \
        glue(glue(gen_op_store_, Tn),_srsgpr)(Rn);                            \
    }                                                                         \
} while (0)

629
#define GEN_LOAD_FREG_FTN(FTn, Fn)                                            \
B
bellard 已提交
630 631 632 633 634 635 636 637 638
do {                                                                          \
    glue(gen_op_load_fpr_, FTn)(Fn);                                          \
} while (0)

#define GEN_STORE_FTN_FREG(Fn, FTn)                                           \
do {                                                                          \
    glue(gen_op_store_fpr_, FTn)(Fn);                                         \
} while (0)

639
static always_inline void gen_save_pc(target_ulong pc)
640
{
641
#if defined(TARGET_MIPS64)
642 643 644 645 646 647 648 649 650 651
    if (pc == (int32_t)pc) {
        gen_op_save_pc(pc);
    } else {
        gen_op_save_pc64(pc >> 32, (uint32_t)pc);
    }
#else
    gen_op_save_pc(pc);
#endif
}

652
static always_inline void gen_save_btarget(target_ulong btarget)
653
{
654
#if defined(TARGET_MIPS64)
655 656 657 658 659 660 661 662 663 664
    if (btarget == (int32_t)btarget) {
        gen_op_save_btarget(btarget);
    } else {
        gen_op_save_btarget64(btarget >> 32, (uint32_t)btarget);
    }
#else
    gen_op_save_btarget(btarget);
#endif
}

665
static always_inline void save_cpu_state (DisasContext *ctx, int do_save_pc)
B
bellard 已提交
666 667 668 669 670 671 672 673
{
#if defined MIPS_DEBUG_DISAS
    if (loglevel & CPU_LOG_TB_IN_ASM) {
            fprintf(logfile, "hflags %08x saved %08x\n",
                    ctx->hflags, ctx->saved_hflags);
    }
#endif
    if (do_save_pc && ctx->pc != ctx->saved_pc) {
674
        gen_save_pc(ctx->pc);
B
bellard 已提交
675 676 677 678 679
        ctx->saved_pc = ctx->pc;
    }
    if (ctx->hflags != ctx->saved_hflags) {
        gen_op_save_state(ctx->hflags);
        ctx->saved_hflags = ctx->hflags;
680 681
        switch (ctx->hflags & MIPS_HFLAG_BMASK) {
        case MIPS_HFLAG_BR:
B
bellard 已提交
682
            gen_op_save_breg_target();
683 684
            break;
        case MIPS_HFLAG_BC:
B
bellard 已提交
685
            gen_op_save_bcond();
686 687 688 689 690
            /* fall through */
        case MIPS_HFLAG_BL:
            /* bcond was already saved by the BL insn */
            /* fall through */
        case MIPS_HFLAG_B:
691
            gen_save_btarget(ctx->btarget);
692
            break;
B
bellard 已提交
693 694 695 696
        }
    }
}

697
static always_inline void restore_cpu_state (CPUState *env, DisasContext *ctx)
698
{
699 700 701 702 703 704 705 706 707 708 709 710 711
    ctx->saved_hflags = ctx->hflags;
    switch (ctx->hflags & MIPS_HFLAG_BMASK) {
    case MIPS_HFLAG_BR:
        gen_op_restore_breg_target();
        break;
    case MIPS_HFLAG_B:
        ctx->btarget = env->btarget;
        break;
    case MIPS_HFLAG_BC:
    case MIPS_HFLAG_BL:
        ctx->btarget = env->btarget;
        gen_op_restore_bcond();
        break;
712 713 714
    }
}

715
static always_inline void generate_exception_err (DisasContext *ctx, int excp, int err)
B
bellard 已提交
716 717 718 719 720 721
{
#if defined MIPS_DEBUG_DISAS
    if (loglevel & CPU_LOG_TB_IN_ASM)
            fprintf(logfile, "%s: raise exception %d\n", __func__, excp);
#endif
    save_cpu_state(ctx, 1);
B
bellard 已提交
722 723 724 725
    if (err == 0)
        gen_op_raise_exception(excp);
    else
        gen_op_raise_exception_err(excp, err);
B
bellard 已提交
726 727 728
    ctx->bstate = BS_EXCP;
}

729
static always_inline void generate_exception (DisasContext *ctx, int excp)
B
bellard 已提交
730 731 732 733
{
    generate_exception_err (ctx, excp, 0);
}

734
static always_inline void check_cp0_enabled(DisasContext *ctx)
735
{
736
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_CP0)))
737 738 739
        generate_exception_err(ctx, EXCP_CpU, 1);
}

740
static always_inline void check_cp1_enabled(DisasContext *ctx)
741
{
742
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_FPU)))
743 744 745
        generate_exception_err(ctx, EXCP_CpU, 1);
}

746
static always_inline void check_cp1_64bitmode(DisasContext *ctx)
747
{
748
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_F64)))
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
        generate_exception(ctx, EXCP_RI);
}

/*
 * Verify if floating point register is valid; an operation is not defined
 * if bit 0 of any register specification is set and the FR bit in the
 * Status register equals zero, since the register numbers specify an
 * even-odd pair of adjacent coprocessor general registers. When the FR bit
 * in the Status register equals one, both even and odd register numbers
 * are valid. This limitation exists only for 64 bit wide (d,l,ps) registers.
 *
 * Multiple 64 bit wide registers can be checked by calling
 * gen_op_cp1_registers(freg1 | freg2 | ... | fregN);
 */
void check_cp1_registers(DisasContext *ctx, int regs)
{
765
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_F64) && (regs & 1)))
766 767 768
        generate_exception(ctx, EXCP_RI);
}

769
/* This code generates a "reserved instruction" exception if the
770
   CPU does not support the instruction set corresponding to flags. */
771
static always_inline void check_insn(CPUState *env, DisasContext *ctx, int flags)
772
{
773
    if (unlikely(!(env->insn_flags & flags)))
774 775 776
        generate_exception(ctx, EXCP_RI);
}

777 778
/* This code generates a "reserved instruction" exception if 64-bit
   instructions are not enabled. */
779
static always_inline void check_mips_64(DisasContext *ctx)
780
{
781
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_64)))
782 783 784
        generate_exception(ctx, EXCP_RI);
}

B
bellard 已提交
785 786 787 788 789 790 791 792 793
#if defined(CONFIG_USER_ONLY)
#define op_ldst(name)        gen_op_##name##_raw()
#define OP_LD_TABLE(width)
#define OP_ST_TABLE(width)
#else
#define op_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
#define OP_LD_TABLE(width)                                                    \
static GenOpFunc *gen_op_l##width[] = {                                       \
    &gen_op_l##width##_kernel,                                                \
T
ths 已提交
794 795
    &gen_op_l##width##_super,                                                 \
    &gen_op_l##width##_user,                                                  \
B
bellard 已提交
796 797 798 799
}
#define OP_ST_TABLE(width)                                                    \
static GenOpFunc *gen_op_s##width[] = {                                       \
    &gen_op_s##width##_kernel,                                                \
T
ths 已提交
800 801
    &gen_op_s##width##_super,                                                 \
    &gen_op_s##width##_user,                                                  \
B
bellard 已提交
802 803 804
}
#endif

805
#if defined(TARGET_MIPS64)
B
bellard 已提交
806 807 808 809 810 811
OP_LD_TABLE(d);
OP_LD_TABLE(dl);
OP_LD_TABLE(dr);
OP_ST_TABLE(d);
OP_ST_TABLE(dl);
OP_ST_TABLE(dr);
812 813
OP_LD_TABLE(ld);
OP_ST_TABLE(cd);
814
OP_LD_TABLE(wu);
B
bellard 已提交
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
#endif
OP_LD_TABLE(w);
OP_LD_TABLE(wl);
OP_LD_TABLE(wr);
OP_ST_TABLE(w);
OP_ST_TABLE(wl);
OP_ST_TABLE(wr);
OP_LD_TABLE(h);
OP_LD_TABLE(hu);
OP_ST_TABLE(h);
OP_LD_TABLE(b);
OP_LD_TABLE(bu);
OP_ST_TABLE(b);
OP_LD_TABLE(l);
OP_ST_TABLE(c);
B
bellard 已提交
830 831 832 833
OP_LD_TABLE(wc1);
OP_ST_TABLE(wc1);
OP_LD_TABLE(dc1);
OP_ST_TABLE(dc1);
834 835
OP_LD_TABLE(uxc1);
OP_ST_TABLE(uxc1);
B
bellard 已提交
836 837

/* Load and store */
838
static void gen_ldst (DisasContext *ctx, uint32_t opc, int rt,
B
bellard 已提交
839 840
                      int base, int16_t offset)
{
841
    const char *opn = "ldst";
B
bellard 已提交
842 843 844 845 846 847 848 849

    if (base == 0) {
        GEN_LOAD_IMM_TN(T0, offset);
    } else if (offset == 0) {
        gen_op_load_gpr_T0(base);
    } else {
        gen_op_load_gpr_T0(base);
        gen_op_set_T1(offset);
850
        gen_op_addr_add();
B
bellard 已提交
851 852
    }
    /* Don't do NOP if destination is zero: we must perform the actual
853
       memory access. */
B
bellard 已提交
854
    switch (opc) {
855
#if defined(TARGET_MIPS64)
856 857 858 859 860
    case OPC_LWU:
        op_ldst(lwu);
        GEN_STORE_TN_REG(rt, T0);
        opn = "lwu";
        break;
B
bellard 已提交
861 862 863 864 865
    case OPC_LD:
        op_ldst(ld);
        GEN_STORE_TN_REG(rt, T0);
        opn = "ld";
        break;
866 867 868 869 870
    case OPC_LLD:
        op_ldst(lld);
        GEN_STORE_TN_REG(rt, T0);
        opn = "lld";
        break;
B
bellard 已提交
871 872 873 874 875
    case OPC_SD:
        GEN_LOAD_REG_TN(T1, rt);
        op_ldst(sd);
        opn = "sd";
        break;
876
    case OPC_SCD:
T
ths 已提交
877
        save_cpu_state(ctx, 1);
878 879
        GEN_LOAD_REG_TN(T1, rt);
        op_ldst(scd);
880
        GEN_STORE_TN_REG(rt, T0);
881 882
        opn = "scd";
        break;
B
bellard 已提交
883
    case OPC_LDL:
884
        GEN_LOAD_REG_TN(T1, rt);
B
bellard 已提交
885
        op_ldst(ldl);
886
        GEN_STORE_TN_REG(rt, T1);
B
bellard 已提交
887 888 889 890 891 892 893 894
        opn = "ldl";
        break;
    case OPC_SDL:
        GEN_LOAD_REG_TN(T1, rt);
        op_ldst(sdl);
        opn = "sdl";
        break;
    case OPC_LDR:
895
        GEN_LOAD_REG_TN(T1, rt);
B
bellard 已提交
896
        op_ldst(ldr);
897
        GEN_STORE_TN_REG(rt, T1);
B
bellard 已提交
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
        opn = "ldr";
        break;
    case OPC_SDR:
        GEN_LOAD_REG_TN(T1, rt);
        op_ldst(sdr);
        opn = "sdr";
        break;
#endif
    case OPC_LW:
        op_ldst(lw);
        GEN_STORE_TN_REG(rt, T0);
        opn = "lw";
        break;
    case OPC_SW:
        GEN_LOAD_REG_TN(T1, rt);
        op_ldst(sw);
        opn = "sw";
        break;
    case OPC_LH:
        op_ldst(lh);
        GEN_STORE_TN_REG(rt, T0);
        opn = "lh";
        break;
    case OPC_SH:
        GEN_LOAD_REG_TN(T1, rt);
        op_ldst(sh);
        opn = "sh";
        break;
    case OPC_LHU:
        op_ldst(lhu);
        GEN_STORE_TN_REG(rt, T0);
        opn = "lhu";
        break;
    case OPC_LB:
        op_ldst(lb);
        GEN_STORE_TN_REG(rt, T0);
        opn = "lb";
        break;
    case OPC_SB:
        GEN_LOAD_REG_TN(T1, rt);
        op_ldst(sb);
        opn = "sb";
        break;
    case OPC_LBU:
        op_ldst(lbu);
        GEN_STORE_TN_REG(rt, T0);
        opn = "lbu";
        break;
    case OPC_LWL:
B
bellard 已提交
947
	GEN_LOAD_REG_TN(T1, rt);
B
bellard 已提交
948
        op_ldst(lwl);
949
        GEN_STORE_TN_REG(rt, T1);
B
bellard 已提交
950 951 952 953 954 955 956 957
        opn = "lwl";
        break;
    case OPC_SWL:
        GEN_LOAD_REG_TN(T1, rt);
        op_ldst(swl);
        opn = "swr";
        break;
    case OPC_LWR:
B
bellard 已提交
958
	GEN_LOAD_REG_TN(T1, rt);
B
bellard 已提交
959
        op_ldst(lwr);
960
        GEN_STORE_TN_REG(rt, T1);
B
bellard 已提交
961 962 963 964 965 966 967 968 969 970 971 972 973
        opn = "lwr";
        break;
    case OPC_SWR:
        GEN_LOAD_REG_TN(T1, rt);
        op_ldst(swr);
        opn = "swr";
        break;
    case OPC_LL:
        op_ldst(ll);
        GEN_STORE_TN_REG(rt, T0);
        opn = "ll";
        break;
    case OPC_SC:
T
ths 已提交
974
        save_cpu_state(ctx, 1);
B
bellard 已提交
975 976 977 978 979 980
        GEN_LOAD_REG_TN(T1, rt);
        op_ldst(sc);
        GEN_STORE_TN_REG(rt, T0);
        opn = "sc";
        break;
    default:
981
        MIPS_INVAL(opn);
B
bellard 已提交
982 983 984 985 986 987
        generate_exception(ctx, EXCP_RI);
        return;
    }
    MIPS_DEBUG("%s %s, %d(%s)", opn, regnames[rt], offset, regnames[base]);
}

B
bellard 已提交
988
/* Load and store */
989
static void gen_flt_ldst (DisasContext *ctx, uint32_t opc, int ft,
B
bellard 已提交
990 991
                      int base, int16_t offset)
{
992
    const char *opn = "flt_ldst";
B
bellard 已提交
993 994 995 996 997 998 999 1000

    if (base == 0) {
        GEN_LOAD_IMM_TN(T0, offset);
    } else if (offset == 0) {
        gen_op_load_gpr_T0(base);
    } else {
        gen_op_load_gpr_T0(base);
        gen_op_set_T1(offset);
1001
        gen_op_addr_add();
B
bellard 已提交
1002 1003
    }
    /* Don't do NOP if destination is zero: we must perform the actual
1004
       memory access. */
B
bellard 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
    switch (opc) {
    case OPC_LWC1:
        op_ldst(lwc1);
        GEN_STORE_FTN_FREG(ft, WT0);
        opn = "lwc1";
        break;
    case OPC_SWC1:
        GEN_LOAD_FREG_FTN(WT0, ft);
        op_ldst(swc1);
        opn = "swc1";
        break;
    case OPC_LDC1:
        op_ldst(ldc1);
        GEN_STORE_FTN_FREG(ft, DT0);
        opn = "ldc1";
        break;
    case OPC_SDC1:
        GEN_LOAD_FREG_FTN(DT0, ft);
        op_ldst(sdc1);
        opn = "sdc1";
        break;
    default:
1027
        MIPS_INVAL(opn);
1028
        generate_exception(ctx, EXCP_RI);
B
bellard 已提交
1029 1030 1031 1032 1033
        return;
    }
    MIPS_DEBUG("%s %s, %d(%s)", opn, fregnames[ft], offset, regnames[base]);
}

B
bellard 已提交
1034
/* Arithmetic with immediate operand */
1035 1036
static void gen_arith_imm (CPUState *env, DisasContext *ctx, uint32_t opc,
                           int rt, int rs, int16_t imm)
B
bellard 已提交
1037
{
1038
    target_ulong uimm;
1039
    const char *opn = "imm arith";
B
bellard 已提交
1040

1041
    if (rt == 0 && opc != OPC_ADDI && opc != OPC_DADDI) {
1042 1043
        /* If no destination, treat it as a NOP.
           For addi, we must generate the overflow exception when needed. */
B
bellard 已提交
1044 1045 1046
        MIPS_DEBUG("NOP");
        return;
    }
1047 1048 1049 1050
    uimm = (uint16_t)imm;
    switch (opc) {
    case OPC_ADDI:
    case OPC_ADDIU:
1051
#if defined(TARGET_MIPS64)
1052 1053 1054 1055 1056
    case OPC_DADDI:
    case OPC_DADDIU:
#endif
    case OPC_SLTI:
    case OPC_SLTIU:
1057
        uimm = (target_long)imm; /* Sign extend to 32/64 bits */
1058 1059 1060 1061
        /* Fall through. */
    case OPC_ANDI:
    case OPC_ORI:
    case OPC_XORI:
B
bellard 已提交
1062 1063
        GEN_LOAD_REG_TN(T0, rs);
        GEN_LOAD_IMM_TN(T1, uimm);
1064 1065
        break;
    case OPC_LUI:
1066
        GEN_LOAD_IMM_TN(T0, imm << 16);
1067 1068 1069 1070
        break;
    case OPC_SLL:
    case OPC_SRA:
    case OPC_SRL:
1071
#if defined(TARGET_MIPS64)
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
    case OPC_DSLL:
    case OPC_DSRA:
    case OPC_DSRL:
    case OPC_DSLL32:
    case OPC_DSRA32:
    case OPC_DSRL32:
#endif
        uimm &= 0x1f;
        GEN_LOAD_REG_TN(T0, rs);
        GEN_LOAD_IMM_TN(T1, uimm);
        break;
B
bellard 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
    }
    switch (opc) {
    case OPC_ADDI:
        save_cpu_state(ctx, 1);
        gen_op_addo();
        opn = "addi";
        break;
    case OPC_ADDIU:
        gen_op_add();
        opn = "addiu";
        break;
1094
#if defined(TARGET_MIPS64)
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
    case OPC_DADDI:
        save_cpu_state(ctx, 1);
        gen_op_daddo();
        opn = "daddi";
        break;
    case OPC_DADDIU:
        gen_op_dadd();
        opn = "daddiu";
        break;
#endif
B
bellard 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
    case OPC_SLTI:
        gen_op_lt();
        opn = "slti";
        break;
    case OPC_SLTIU:
        gen_op_ltu();
        opn = "sltiu";
        break;
    case OPC_ANDI:
        gen_op_and();
        opn = "andi";
        break;
    case OPC_ORI:
        gen_op_or();
        opn = "ori";
        break;
    case OPC_XORI:
        gen_op_xor();
        opn = "xori";
        break;
    case OPC_LUI:
        opn = "lui";
        break;
    case OPC_SLL:
        gen_op_sll();
        opn = "sll";
        break;
    case OPC_SRA:
        gen_op_sra();
        opn = "sra";
        break;
    case OPC_SRL:
1137 1138
        switch ((ctx->opcode >> 21) & 0x1f) {
        case 0:
1139 1140
            gen_op_srl();
            opn = "srl";
1141 1142
            break;
        case 1:
1143 1144 1145 1146 1147 1148 1149 1150
            /* rotr is decoded as srl on non-R2 CPUs */
            if (env->insn_flags & ISA_MIPS32R2) {
                gen_op_rotr();
                opn = "rotr";
            } else {
                gen_op_srl();
                opn = "srl";
            }
1151 1152 1153 1154 1155 1156
            break;
        default:
            MIPS_INVAL("invalid srl flag");
            generate_exception(ctx, EXCP_RI);
            break;
        }
1157
        break;
1158
#if defined(TARGET_MIPS64)
1159 1160 1161 1162 1163 1164 1165 1166 1167
    case OPC_DSLL:
        gen_op_dsll();
        opn = "dsll";
        break;
    case OPC_DSRA:
        gen_op_dsra();
        opn = "dsra";
        break;
    case OPC_DSRL:
1168 1169
        switch ((ctx->opcode >> 21) & 0x1f) {
        case 0:
1170 1171
            gen_op_dsrl();
            opn = "dsrl";
1172 1173
            break;
        case 1:
1174 1175 1176 1177 1178 1179 1180 1181
            /* drotr is decoded as dsrl on non-R2 CPUs */
            if (env->insn_flags & ISA_MIPS32R2) {
                gen_op_drotr();
                opn = "drotr";
            } else {
                gen_op_dsrl();
                opn = "dsrl";
            }
1182 1183 1184 1185 1186 1187
            break;
        default:
            MIPS_INVAL("invalid dsrl flag");
            generate_exception(ctx, EXCP_RI);
            break;
        }
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
        break;
    case OPC_DSLL32:
        gen_op_dsll32();
        opn = "dsll32";
        break;
    case OPC_DSRA32:
        gen_op_dsra32();
        opn = "dsra32";
        break;
    case OPC_DSRL32:
1198 1199
        switch ((ctx->opcode >> 21) & 0x1f) {
        case 0:
1200 1201
            gen_op_dsrl32();
            opn = "dsrl32";
1202 1203
            break;
        case 1:
1204 1205 1206 1207 1208 1209 1210 1211
            /* drotr32 is decoded as dsrl32 on non-R2 CPUs */
            if (env->insn_flags & ISA_MIPS32R2) {
                gen_op_drotr32();
                opn = "drotr32";
            } else {
                gen_op_dsrl32();
                opn = "dsrl32";
            }
1212 1213 1214 1215 1216 1217
            break;
        default:
            MIPS_INVAL("invalid dsrl32 flag");
            generate_exception(ctx, EXCP_RI);
            break;
        }
B
bellard 已提交
1218
        break;
1219
#endif
B
bellard 已提交
1220
    default:
1221
        MIPS_INVAL(opn);
B
bellard 已提交
1222 1223 1224 1225
        generate_exception(ctx, EXCP_RI);
        return;
    }
    GEN_STORE_TN_REG(rt, T0);
T
ths 已提交
1226
    MIPS_DEBUG("%s %s, %s, " TARGET_FMT_lx, opn, regnames[rt], regnames[rs], uimm);
B
bellard 已提交
1227 1228 1229
}

/* Arithmetic */
1230
static void gen_arith (CPUState *env, DisasContext *ctx, uint32_t opc,
B
bellard 已提交
1231 1232
                       int rd, int rs, int rt)
{
1233
    const char *opn = "arith";
B
bellard 已提交
1234

1235 1236
    if (rd == 0 && opc != OPC_ADD && opc != OPC_SUB
       && opc != OPC_DADD && opc != OPC_DSUB) {
1237 1238
        /* If no destination, treat it as a NOP.
           For add & sub, we must generate the overflow exception when needed. */
B
bellard 已提交
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
        MIPS_DEBUG("NOP");
        return;
    }
    GEN_LOAD_REG_TN(T0, rs);
    GEN_LOAD_REG_TN(T1, rt);
    switch (opc) {
    case OPC_ADD:
        save_cpu_state(ctx, 1);
        gen_op_addo();
        opn = "add";
        break;
    case OPC_ADDU:
        gen_op_add();
        opn = "addu";
        break;
    case OPC_SUB:
        save_cpu_state(ctx, 1);
        gen_op_subo();
        opn = "sub";
        break;
    case OPC_SUBU:
        gen_op_sub();
        opn = "subu";
        break;
1263
#if defined(TARGET_MIPS64)
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
    case OPC_DADD:
        save_cpu_state(ctx, 1);
        gen_op_daddo();
        opn = "dadd";
        break;
    case OPC_DADDU:
        gen_op_dadd();
        opn = "daddu";
        break;
    case OPC_DSUB:
        save_cpu_state(ctx, 1);
        gen_op_dsubo();
        opn = "dsub";
        break;
    case OPC_DSUBU:
        gen_op_dsub();
        opn = "dsubu";
        break;
#endif
B
bellard 已提交
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
    case OPC_SLT:
        gen_op_lt();
        opn = "slt";
        break;
    case OPC_SLTU:
        gen_op_ltu();
        opn = "sltu";
        break;
    case OPC_AND:
        gen_op_and();
        opn = "and";
        break;
    case OPC_NOR:
        gen_op_nor();
        opn = "nor";
        break;
    case OPC_OR:
        gen_op_or();
        opn = "or";
        break;
    case OPC_XOR:
        gen_op_xor();
        opn = "xor";
        break;
    case OPC_MUL:
        gen_op_mul();
        opn = "mul";
        break;
    case OPC_MOVN:
        gen_op_movn(rd);
        opn = "movn";
        goto print;
    case OPC_MOVZ:
        gen_op_movz(rd);
        opn = "movz";
        goto print;
    case OPC_SLLV:
        gen_op_sllv();
        opn = "sllv";
        break;
    case OPC_SRAV:
        gen_op_srav();
        opn = "srav";
        break;
    case OPC_SRLV:
1328 1329
        switch ((ctx->opcode >> 6) & 0x1f) {
        case 0:
1330 1331
            gen_op_srlv();
            opn = "srlv";
1332 1333
            break;
        case 1:
1334 1335 1336 1337 1338 1339 1340 1341
            /* rotrv is decoded as srlv on non-R2 CPUs */
            if (env->insn_flags & ISA_MIPS32R2) {
                gen_op_rotrv();
                opn = "rotrv";
            } else {
                gen_op_srlv();
                opn = "srlv";
            }
1342 1343 1344 1345 1346 1347
            break;
        default:
            MIPS_INVAL("invalid srlv flag");
            generate_exception(ctx, EXCP_RI);
            break;
        }
1348
        break;
1349
#if defined(TARGET_MIPS64)
1350 1351 1352 1353 1354 1355 1356 1357 1358
    case OPC_DSLLV:
        gen_op_dsllv();
        opn = "dsllv";
        break;
    case OPC_DSRAV:
        gen_op_dsrav();
        opn = "dsrav";
        break;
    case OPC_DSRLV:
1359 1360
        switch ((ctx->opcode >> 6) & 0x1f) {
        case 0:
1361 1362
            gen_op_dsrlv();
            opn = "dsrlv";
1363 1364
            break;
        case 1:
1365 1366 1367 1368 1369 1370 1371 1372
            /* drotrv is decoded as dsrlv on non-R2 CPUs */
            if (env->insn_flags & ISA_MIPS32R2) {
                gen_op_drotrv();
                opn = "drotrv";
            } else {
                gen_op_dsrlv();
                opn = "dsrlv";
            }
1373 1374 1375 1376 1377 1378
            break;
        default:
            MIPS_INVAL("invalid dsrlv flag");
            generate_exception(ctx, EXCP_RI);
            break;
        }
B
bellard 已提交
1379
        break;
1380
#endif
B
bellard 已提交
1381
    default:
1382
        MIPS_INVAL(opn);
B
bellard 已提交
1383 1384 1385 1386 1387 1388 1389 1390 1391
        generate_exception(ctx, EXCP_RI);
        return;
    }
    GEN_STORE_TN_REG(rd, T0);
 print:
    MIPS_DEBUG("%s %s, %s, %s", opn, regnames[rd], regnames[rs], regnames[rt]);
}

/* Arithmetic on HI/LO registers */
1392
static void gen_HILO (DisasContext *ctx, uint32_t opc, int reg)
B
bellard 已提交
1393
{
1394
    const char *opn = "hilo";
B
bellard 已提交
1395 1396

    if (reg == 0 && (opc == OPC_MFHI || opc == OPC_MFLO)) {
1397
        /* Treat as NOP. */
B
bellard 已提交
1398 1399 1400 1401 1402
        MIPS_DEBUG("NOP");
        return;
    }
    switch (opc) {
    case OPC_MFHI:
1403
        gen_op_load_HI(0);
B
bellard 已提交
1404 1405 1406 1407
        GEN_STORE_TN_REG(reg, T0);
        opn = "mfhi";
        break;
    case OPC_MFLO:
1408
        gen_op_load_LO(0);
B
bellard 已提交
1409 1410 1411 1412 1413
        GEN_STORE_TN_REG(reg, T0);
        opn = "mflo";
        break;
    case OPC_MTHI:
        GEN_LOAD_REG_TN(T0, reg);
1414
        gen_op_store_HI(0);
B
bellard 已提交
1415 1416 1417 1418
        opn = "mthi";
        break;
    case OPC_MTLO:
        GEN_LOAD_REG_TN(T0, reg);
1419
        gen_op_store_LO(0);
B
bellard 已提交
1420 1421 1422
        opn = "mtlo";
        break;
    default:
1423
        MIPS_INVAL(opn);
B
bellard 已提交
1424 1425 1426 1427 1428 1429
        generate_exception(ctx, EXCP_RI);
        return;
    }
    MIPS_DEBUG("%s %s", opn, regnames[reg]);
}

1430
static void gen_muldiv (DisasContext *ctx, uint32_t opc,
B
bellard 已提交
1431 1432
                        int rs, int rt)
{
1433
    const char *opn = "mul/div";
B
bellard 已提交
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453

    GEN_LOAD_REG_TN(T0, rs);
    GEN_LOAD_REG_TN(T1, rt);
    switch (opc) {
    case OPC_DIV:
        gen_op_div();
        opn = "div";
        break;
    case OPC_DIVU:
        gen_op_divu();
        opn = "divu";
        break;
    case OPC_MULT:
        gen_op_mult();
        opn = "mult";
        break;
    case OPC_MULTU:
        gen_op_multu();
        opn = "multu";
        break;
1454
#if defined(TARGET_MIPS64)
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
    case OPC_DDIV:
        gen_op_ddiv();
        opn = "ddiv";
        break;
    case OPC_DDIVU:
        gen_op_ddivu();
        opn = "ddivu";
        break;
    case OPC_DMULT:
        gen_op_dmult();
        opn = "dmult";
        break;
    case OPC_DMULTU:
        gen_op_dmultu();
        opn = "dmultu";
        break;
#endif
B
bellard 已提交
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
    case OPC_MADD:
        gen_op_madd();
        opn = "madd";
        break;
    case OPC_MADDU:
        gen_op_maddu();
        opn = "maddu";
        break;
    case OPC_MSUB:
        gen_op_msub();
        opn = "msub";
        break;
    case OPC_MSUBU:
        gen_op_msubu();
        opn = "msubu";
        break;
    default:
1489
        MIPS_INVAL(opn);
B
bellard 已提交
1490 1491 1492 1493 1494 1495
        generate_exception(ctx, EXCP_RI);
        return;
    }
    MIPS_DEBUG("%s %s %s", opn, regnames[rs], regnames[rt]);
}

1496
static void gen_cl (DisasContext *ctx, uint32_t opc,
B
bellard 已提交
1497 1498
                    int rd, int rs)
{
1499
    const char *opn = "CLx";
B
bellard 已提交
1500
    if (rd == 0) {
1501
        /* Treat as NOP. */
B
bellard 已提交
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
        MIPS_DEBUG("NOP");
        return;
    }
    GEN_LOAD_REG_TN(T0, rs);
    switch (opc) {
    case OPC_CLO:
        gen_op_clo();
        opn = "clo";
        break;
    case OPC_CLZ:
        gen_op_clz();
        opn = "clz";
        break;
1515
#if defined(TARGET_MIPS64)
1516 1517 1518 1519 1520 1521 1522 1523 1524
    case OPC_DCLO:
        gen_op_dclo();
        opn = "dclo";
        break;
    case OPC_DCLZ:
        gen_op_dclz();
        opn = "dclz";
        break;
#endif
B
bellard 已提交
1525
    default:
1526
        MIPS_INVAL(opn);
B
bellard 已提交
1527 1528 1529 1530 1531 1532 1533 1534
        generate_exception(ctx, EXCP_RI);
        return;
    }
    gen_op_store_T0_gpr(rd);
    MIPS_DEBUG("%s %s, %s", opn, regnames[rd], regnames[rs]);
}

/* Traps */
1535
static void gen_trap (DisasContext *ctx, uint32_t opc,
B
bellard 已提交
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
                      int rs, int rt, int16_t imm)
{
    int cond;

    cond = 0;
    /* Load needed operands */
    switch (opc) {
    case OPC_TEQ:
    case OPC_TGE:
    case OPC_TGEU:
    case OPC_TLT:
    case OPC_TLTU:
    case OPC_TNE:
        /* Compare two registers */
        if (rs != rt) {
            GEN_LOAD_REG_TN(T0, rs);
            GEN_LOAD_REG_TN(T1, rt);
            cond = 1;
        }
1555
        break;
B
bellard 已提交
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
    case OPC_TEQI:
    case OPC_TGEI:
    case OPC_TGEIU:
    case OPC_TLTI:
    case OPC_TLTIU:
    case OPC_TNEI:
        /* Compare register to immediate */
        if (rs != 0 || imm != 0) {
            GEN_LOAD_REG_TN(T0, rs);
            GEN_LOAD_IMM_TN(T1, (int32_t)imm);
            cond = 1;
        }
        break;
    }
    if (cond == 0) {
        switch (opc) {
        case OPC_TEQ:   /* rs == rs */
        case OPC_TEQI:  /* r0 == 0  */
        case OPC_TGE:   /* rs >= rs */
        case OPC_TGEI:  /* r0 >= 0  */
        case OPC_TGEU:  /* rs >= rs unsigned */
        case OPC_TGEIU: /* r0 >= 0  unsigned */
            /* Always trap */
            gen_op_set_T0(1);
            break;
        case OPC_TLT:   /* rs < rs           */
        case OPC_TLTI:  /* r0 < 0            */
        case OPC_TLTU:  /* rs < rs unsigned  */
        case OPC_TLTIU: /* r0 < 0  unsigned  */
        case OPC_TNE:   /* rs != rs          */
        case OPC_TNEI:  /* r0 != 0           */
1587
            /* Never trap: treat as NOP. */
B
bellard 已提交
1588 1589
            return;
        default:
1590
            MIPS_INVAL("trap");
B
bellard 已提交
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
            generate_exception(ctx, EXCP_RI);
            return;
        }
    } else {
        switch (opc) {
        case OPC_TEQ:
        case OPC_TEQI:
            gen_op_eq();
            break;
        case OPC_TGE:
        case OPC_TGEI:
            gen_op_ge();
            break;
        case OPC_TGEU:
        case OPC_TGEIU:
            gen_op_geu();
            break;
        case OPC_TLT:
        case OPC_TLTI:
            gen_op_lt();
            break;
        case OPC_TLTU:
        case OPC_TLTIU:
            gen_op_ltu();
            break;
        case OPC_TNE:
        case OPC_TNEI:
            gen_op_ne();
            break;
        default:
1621
            MIPS_INVAL("trap");
B
bellard 已提交
1622 1623 1624 1625 1626 1627 1628 1629 1630
            generate_exception(ctx, EXCP_RI);
            return;
        }
    }
    save_cpu_state(ctx, 1);
    gen_op_trap();
    ctx->bstate = BS_STOP;
}

1631
static always_inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
B
bellard 已提交
1632
{
1633 1634 1635 1636 1637 1638 1639
    TranslationBlock *tb;
    tb = ctx->tb;
    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK)) {
        if (n == 0)
            gen_op_goto_tb0(TBPARAM(tb));
        else
            gen_op_goto_tb1(TBPARAM(tb));
1640
        gen_save_pc(dest);
1641 1642
        gen_op_set_T0((long)tb + n);
    } else {
1643
        gen_save_pc(dest);
T
ths 已提交
1644
        gen_op_reset_T0();
1645
    }
T
ths 已提交
1646
    gen_op_exit_tb();
B
bellard 已提交
1647 1648
}

B
bellard 已提交
1649
/* Branches (before delay slot) */
1650
static void gen_compute_branch (DisasContext *ctx, uint32_t opc,
B
bellard 已提交
1651 1652
                                int rs, int rt, int32_t offset)
{
1653 1654 1655 1656 1657
    target_ulong btarget = -1;
    int blink = 0;
    int bcond = 0;

    if (ctx->hflags & MIPS_HFLAG_BMASK) {
1658
#ifdef MIPS_DEBUG_DISAS
1659 1660
        if (loglevel & CPU_LOG_TB_IN_ASM) {
            fprintf(logfile,
1661
                    "Branch in delay slot at PC 0x" TARGET_FMT_lx "\n",
1662
                    ctx->pc);
1663
	}
1664
#endif
1665 1666 1667
        generate_exception(ctx, EXCP_RI);
        return;
    }
B
bellard 已提交
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704

    /* Load needed operands */
    switch (opc) {
    case OPC_BEQ:
    case OPC_BEQL:
    case OPC_BNE:
    case OPC_BNEL:
        /* Compare two registers */
        if (rs != rt) {
            GEN_LOAD_REG_TN(T0, rs);
            GEN_LOAD_REG_TN(T1, rt);
            bcond = 1;
        }
        btarget = ctx->pc + 4 + offset;
        break;
    case OPC_BGEZ:
    case OPC_BGEZAL:
    case OPC_BGEZALL:
    case OPC_BGEZL:
    case OPC_BGTZ:
    case OPC_BGTZL:
    case OPC_BLEZ:
    case OPC_BLEZL:
    case OPC_BLTZ:
    case OPC_BLTZAL:
    case OPC_BLTZALL:
    case OPC_BLTZL:
        /* Compare to zero */
        if (rs != 0) {
            gen_op_load_gpr_T0(rs);
            bcond = 1;
        }
        btarget = ctx->pc + 4 + offset;
        break;
    case OPC_J:
    case OPC_JAL:
        /* Jump to immediate */
1705
        btarget = ((ctx->pc + 4) & (int32_t)0xF0000000) | (uint32_t)offset;
B
bellard 已提交
1706 1707 1708 1709
        break;
    case OPC_JR:
    case OPC_JALR:
        /* Jump to register */
1710 1711
        if (offset != 0 && offset != 16) {
            /* Hint = 0 is JR/JALR, hint 16 is JR.HB/JALR.HB, the
1712
               others are reserved. */
1713
            MIPS_INVAL("jump hint");
B
bellard 已提交
1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
            generate_exception(ctx, EXCP_RI);
            return;
        }
        GEN_LOAD_REG_TN(T2, rs);
        break;
    default:
        MIPS_INVAL("branch/jump");
        generate_exception(ctx, EXCP_RI);
        return;
    }
    if (bcond == 0) {
        /* No condition to be computed */
        switch (opc) {
        case OPC_BEQ:     /* rx == rx        */
        case OPC_BEQL:    /* rx == rx likely */
        case OPC_BGEZ:    /* 0 >= 0          */
        case OPC_BGEZL:   /* 0 >= 0 likely   */
        case OPC_BLEZ:    /* 0 <= 0          */
        case OPC_BLEZL:   /* 0 <= 0 likely   */
            /* Always take */
B
bellard 已提交
1734
            ctx->hflags |= MIPS_HFLAG_B;
B
bellard 已提交
1735 1736 1737 1738 1739 1740
            MIPS_DEBUG("balways");
            break;
        case OPC_BGEZAL:  /* 0 >= 0          */
        case OPC_BGEZALL: /* 0 >= 0 likely   */
            /* Always take and link */
            blink = 31;
B
bellard 已提交
1741
            ctx->hflags |= MIPS_HFLAG_B;
B
bellard 已提交
1742 1743 1744 1745 1746
            MIPS_DEBUG("balways and link");
            break;
        case OPC_BNE:     /* rx != rx        */
        case OPC_BGTZ:    /* 0 > 0           */
        case OPC_BLTZ:    /* 0 < 0           */
1747
            /* Treat as NOP. */
B
bellard 已提交
1748 1749
            MIPS_DEBUG("bnever (NOP)");
            return;
1750
        case OPC_BLTZAL:  /* 0 < 0           */
1751
            GEN_LOAD_IMM_TN(T0, ctx->pc + 8);
1752
            gen_op_store_T0_gpr(31);
T
ths 已提交
1753
            MIPS_DEBUG("bnever and link");
1754 1755
            return;
        case OPC_BLTZALL: /* 0 < 0 likely */
1756
            GEN_LOAD_IMM_TN(T0, ctx->pc + 8);
1757
            gen_op_store_T0_gpr(31);
T
ths 已提交
1758 1759 1760
            /* Skip the instruction in the delay slot */
            MIPS_DEBUG("bnever, link and skip");
            ctx->pc += 4;
1761
            return;
B
bellard 已提交
1762 1763 1764 1765 1766
        case OPC_BNEL:    /* rx != rx likely */
        case OPC_BGTZL:   /* 0 > 0 likely */
        case OPC_BLTZL:   /* 0 < 0 likely */
            /* Skip the instruction in the delay slot */
            MIPS_DEBUG("bnever and skip");
T
ths 已提交
1767
            ctx->pc += 4;
B
bellard 已提交
1768 1769
            return;
        case OPC_J:
B
bellard 已提交
1770
            ctx->hflags |= MIPS_HFLAG_B;
1771
            MIPS_DEBUG("j " TARGET_FMT_lx, btarget);
B
bellard 已提交
1772 1773 1774
            break;
        case OPC_JAL:
            blink = 31;
B
bellard 已提交
1775
            ctx->hflags |= MIPS_HFLAG_B;
1776
            MIPS_DEBUG("jal " TARGET_FMT_lx, btarget);
B
bellard 已提交
1777 1778
            break;
        case OPC_JR:
B
bellard 已提交
1779
            ctx->hflags |= MIPS_HFLAG_BR;
B
bellard 已提交
1780 1781 1782 1783
            MIPS_DEBUG("jr %s", regnames[rs]);
            break;
        case OPC_JALR:
            blink = rt;
B
bellard 已提交
1784
            ctx->hflags |= MIPS_HFLAG_BR;
B
bellard 已提交
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
            MIPS_DEBUG("jalr %s, %s", regnames[rt], regnames[rs]);
            break;
        default:
            MIPS_INVAL("branch/jump");
            generate_exception(ctx, EXCP_RI);
            return;
        }
    } else {
        switch (opc) {
        case OPC_BEQ:
            gen_op_eq();
1796
            MIPS_DEBUG("beq %s, %s, " TARGET_FMT_lx,
B
bellard 已提交
1797 1798 1799 1800
                       regnames[rs], regnames[rt], btarget);
            goto not_likely;
        case OPC_BEQL:
            gen_op_eq();
1801
            MIPS_DEBUG("beql %s, %s, " TARGET_FMT_lx,
B
bellard 已提交
1802 1803 1804 1805
                       regnames[rs], regnames[rt], btarget);
            goto likely;
        case OPC_BNE:
            gen_op_ne();
1806
            MIPS_DEBUG("bne %s, %s, " TARGET_FMT_lx,
B
bellard 已提交
1807 1808 1809 1810
                       regnames[rs], regnames[rt], btarget);
            goto not_likely;
        case OPC_BNEL:
            gen_op_ne();
1811
            MIPS_DEBUG("bnel %s, %s, " TARGET_FMT_lx,
B
bellard 已提交
1812 1813 1814 1815
                       regnames[rs], regnames[rt], btarget);
            goto likely;
        case OPC_BGEZ:
            gen_op_gez();
1816
            MIPS_DEBUG("bgez %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
1817 1818 1819
            goto not_likely;
        case OPC_BGEZL:
            gen_op_gez();
1820
            MIPS_DEBUG("bgezl %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
1821 1822 1823
            goto likely;
        case OPC_BGEZAL:
            gen_op_gez();
1824
            MIPS_DEBUG("bgezal %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
1825 1826 1827 1828 1829
            blink = 31;
            goto not_likely;
        case OPC_BGEZALL:
            gen_op_gez();
            blink = 31;
1830
            MIPS_DEBUG("bgezall %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
1831 1832 1833
            goto likely;
        case OPC_BGTZ:
            gen_op_gtz();
1834
            MIPS_DEBUG("bgtz %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
1835 1836 1837
            goto not_likely;
        case OPC_BGTZL:
            gen_op_gtz();
1838
            MIPS_DEBUG("bgtzl %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
1839 1840 1841
            goto likely;
        case OPC_BLEZ:
            gen_op_lez();
1842
            MIPS_DEBUG("blez %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
1843 1844 1845
            goto not_likely;
        case OPC_BLEZL:
            gen_op_lez();
1846
            MIPS_DEBUG("blezl %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
1847 1848 1849
            goto likely;
        case OPC_BLTZ:
            gen_op_ltz();
1850
            MIPS_DEBUG("bltz %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
1851 1852 1853
            goto not_likely;
        case OPC_BLTZL:
            gen_op_ltz();
1854
            MIPS_DEBUG("bltzl %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
1855 1856 1857 1858
            goto likely;
        case OPC_BLTZAL:
            gen_op_ltz();
            blink = 31;
1859
            MIPS_DEBUG("bltzal %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
1860
        not_likely:
B
bellard 已提交
1861
            ctx->hflags |= MIPS_HFLAG_BC;
1862
            gen_op_set_bcond();
B
bellard 已提交
1863 1864 1865 1866
            break;
        case OPC_BLTZALL:
            gen_op_ltz();
            blink = 31;
1867
            MIPS_DEBUG("bltzall %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
1868
        likely:
B
bellard 已提交
1869
            ctx->hflags |= MIPS_HFLAG_BL;
1870 1871
            gen_op_set_bcond();
            gen_op_save_bcond();
B
bellard 已提交
1872
            break;
T
ths 已提交
1873 1874 1875 1876
        default:
            MIPS_INVAL("conditional branch/jump");
            generate_exception(ctx, EXCP_RI);
            return;
B
bellard 已提交
1877 1878
        }
    }
1879
    MIPS_DEBUG("enter ds: link %d cond %02x target " TARGET_FMT_lx,
B
bellard 已提交
1880
               blink, ctx->hflags, btarget);
1881

B
bellard 已提交
1882 1883
    ctx->btarget = btarget;
    if (blink > 0) {
1884
        GEN_LOAD_IMM_TN(T0, ctx->pc + 8);
B
bellard 已提交
1885 1886 1887 1888
        gen_op_store_T0_gpr(blink);
    }
}

1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915
/* special3 bitfield operations */
static void gen_bitops (DisasContext *ctx, uint32_t opc, int rt,
                       int rs, int lsb, int msb)
{
    GEN_LOAD_REG_TN(T1, rs);
    switch (opc) {
    case OPC_EXT:
        if (lsb + msb > 31)
            goto fail;
        gen_op_ext(lsb, msb + 1);
        break;
    case OPC_DEXTM:
        if (lsb + msb > 63)
            goto fail;
        gen_op_ext(lsb, msb + 1 + 32);
        break;
    case OPC_DEXTU:
        if (lsb + msb > 63)
            goto fail;
        gen_op_ext(lsb + 32, msb + 1);
        break;
    case OPC_DEXT:
        gen_op_ext(lsb, msb + 1);
        break;
    case OPC_INS:
        if (lsb > msb)
            goto fail;
1916
        GEN_LOAD_REG_TN(T0, rt);
1917 1918 1919 1920 1921
        gen_op_ins(lsb, msb - lsb + 1);
        break;
    case OPC_DINSM:
        if (lsb > msb)
            goto fail;
1922
        GEN_LOAD_REG_TN(T0, rt);
1923 1924 1925 1926 1927
        gen_op_ins(lsb, msb - lsb + 1 + 32);
        break;
    case OPC_DINSU:
        if (lsb > msb)
            goto fail;
1928
        GEN_LOAD_REG_TN(T0, rt);
1929 1930 1931 1932 1933
        gen_op_ins(lsb + 32, msb - lsb + 1);
        break;
    case OPC_DINS:
        if (lsb > msb)
            goto fail;
1934
        GEN_LOAD_REG_TN(T0, rt);
1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
        gen_op_ins(lsb, msb - lsb + 1);
        break;
    default:
fail:
        MIPS_INVAL("bitops");
        generate_exception(ctx, EXCP_RI);
        return;
    }
    GEN_STORE_TN_REG(rt, T0);
}

B
bellard 已提交
1946
/* CP0 (MMU and control) */
1947
static void gen_mfc0 (CPUState *env, DisasContext *ctx, int reg, int sel)
1948
{
1949
    const char *rn = "invalid";
1950

1951 1952 1953
    if (sel != 0)
        check_insn(env, ctx, ISA_MIPS32);

1954 1955
    switch (reg) {
    case 0:
1956 1957
        switch (sel) {
        case 0:
T
ths 已提交
1958
            gen_op_mfc0_index();
1959 1960 1961
            rn = "Index";
            break;
        case 1:
1962
            check_insn(env, ctx, ASE_MT);
1963
            gen_op_mfc0_mvpcontrol();
1964
            rn = "MVPControl";
1965
            break;
1966
        case 2:
1967
            check_insn(env, ctx, ASE_MT);
1968
            gen_op_mfc0_mvpconf0();
1969
            rn = "MVPConf0";
1970
            break;
1971
        case 3:
1972
            check_insn(env, ctx, ASE_MT);
1973
            gen_op_mfc0_mvpconf1();
1974
            rn = "MVPConf1";
1975
            break;
1976 1977 1978
        default:
            goto die;
        }
1979 1980
        break;
    case 1:
1981 1982 1983 1984
        switch (sel) {
        case 0:
            gen_op_mfc0_random();
            rn = "Random";
T
ths 已提交
1985
            break;
1986
        case 1:
1987
            check_insn(env, ctx, ASE_MT);
1988
            gen_op_mfc0_vpecontrol();
1989
            rn = "VPEControl";
1990
            break;
1991
        case 2:
1992
            check_insn(env, ctx, ASE_MT);
1993
            gen_op_mfc0_vpeconf0();
1994
            rn = "VPEConf0";
1995
            break;
1996
        case 3:
1997
            check_insn(env, ctx, ASE_MT);
1998
            gen_op_mfc0_vpeconf1();
1999
            rn = "VPEConf1";
2000
            break;
2001
        case 4:
2002
            check_insn(env, ctx, ASE_MT);
2003
            gen_op_mfc0_yqmask();
2004
            rn = "YQMask";
2005
            break;
2006
        case 5:
2007
            check_insn(env, ctx, ASE_MT);
2008
            gen_op_mfc0_vpeschedule();
2009
            rn = "VPESchedule";
2010
            break;
2011
        case 6:
2012
            check_insn(env, ctx, ASE_MT);
2013
            gen_op_mfc0_vpeschefback();
2014
            rn = "VPEScheFBack";
2015
            break;
2016
        case 7:
2017
            check_insn(env, ctx, ASE_MT);
2018
            gen_op_mfc0_vpeopt();
2019
            rn = "VPEOpt";
2020
            break;
2021 2022 2023
        default:
            goto die;
        }
2024 2025
        break;
    case 2:
2026 2027
        switch (sel) {
        case 0:
T
ths 已提交
2028 2029 2030
            gen_op_mfc0_entrylo0();
            rn = "EntryLo0";
            break;
2031
        case 1:
2032
            check_insn(env, ctx, ASE_MT);
2033
            gen_op_mfc0_tcstatus();
T
ths 已提交
2034
            rn = "TCStatus";
2035
            break;
2036
        case 2:
2037
            check_insn(env, ctx, ASE_MT);
2038
            gen_op_mfc0_tcbind();
T
ths 已提交
2039
            rn = "TCBind";
2040
            break;
2041
        case 3:
2042
            check_insn(env, ctx, ASE_MT);
2043
            gen_op_mfc0_tcrestart();
T
ths 已提交
2044
            rn = "TCRestart";
2045
            break;
2046
        case 4:
2047
            check_insn(env, ctx, ASE_MT);
2048
            gen_op_mfc0_tchalt();
T
ths 已提交
2049
            rn = "TCHalt";
2050
            break;
2051
        case 5:
2052
            check_insn(env, ctx, ASE_MT);
2053
            gen_op_mfc0_tccontext();
T
ths 已提交
2054
            rn = "TCContext";
2055
            break;
2056
        case 6:
2057
            check_insn(env, ctx, ASE_MT);
2058
            gen_op_mfc0_tcschedule();
T
ths 已提交
2059
            rn = "TCSchedule";
2060
            break;
2061
        case 7:
2062
            check_insn(env, ctx, ASE_MT);
2063
            gen_op_mfc0_tcschefback();
T
ths 已提交
2064
            rn = "TCScheFBack";
2065
            break;
2066 2067 2068
        default:
            goto die;
        }
2069 2070
        break;
    case 3:
2071 2072
        switch (sel) {
        case 0:
T
ths 已提交
2073 2074 2075
            gen_op_mfc0_entrylo1();
            rn = "EntryLo1";
            break;
2076 2077
        default:
            goto die;
2078
        }
2079 2080
        break;
    case 4:
2081 2082
        switch (sel) {
        case 0:
T
ths 已提交
2083 2084 2085
            gen_op_mfc0_context();
            rn = "Context";
            break;
2086
        case 1:
T
ths 已提交
2087 2088 2089
//            gen_op_mfc0_contextconfig(); /* SmartMIPS ASE */
            rn = "ContextConfig";
//            break;
2090 2091
        default:
            goto die;
2092
        }
2093 2094
        break;
    case 5:
2095 2096
        switch (sel) {
        case 0:
T
ths 已提交
2097 2098 2099
            gen_op_mfc0_pagemask();
            rn = "PageMask";
            break;
2100
        case 1:
2101
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
2102 2103 2104
            gen_op_mfc0_pagegrain();
            rn = "PageGrain";
            break;
2105 2106
        default:
            goto die;
2107
        }
2108 2109
        break;
    case 6:
2110 2111
        switch (sel) {
        case 0:
T
ths 已提交
2112 2113 2114
            gen_op_mfc0_wired();
            rn = "Wired";
            break;
2115
        case 1:
2116
            check_insn(env, ctx, ISA_MIPS32R2);
2117
            gen_op_mfc0_srsconf0();
T
ths 已提交
2118
            rn = "SRSConf0";
2119
            break;
2120
        case 2:
2121
            check_insn(env, ctx, ISA_MIPS32R2);
2122
            gen_op_mfc0_srsconf1();
T
ths 已提交
2123
            rn = "SRSConf1";
2124
            break;
2125
        case 3:
2126
            check_insn(env, ctx, ISA_MIPS32R2);
2127
            gen_op_mfc0_srsconf2();
T
ths 已提交
2128
            rn = "SRSConf2";
2129
            break;
2130
        case 4:
2131
            check_insn(env, ctx, ISA_MIPS32R2);
2132
            gen_op_mfc0_srsconf3();
T
ths 已提交
2133
            rn = "SRSConf3";
2134
            break;
2135
        case 5:
2136
            check_insn(env, ctx, ISA_MIPS32R2);
2137
            gen_op_mfc0_srsconf4();
T
ths 已提交
2138
            rn = "SRSConf4";
2139
            break;
2140 2141
        default:
            goto die;
2142
        }
2143
        break;
2144
    case 7:
2145 2146
        switch (sel) {
        case 0:
2147
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
2148 2149 2150
            gen_op_mfc0_hwrena();
            rn = "HWREna";
            break;
2151 2152
        default:
            goto die;
2153
        }
2154
        break;
2155
    case 8:
2156 2157
        switch (sel) {
        case 0:
T
ths 已提交
2158 2159 2160
            gen_op_mfc0_badvaddr();
            rn = "BadVaddr";
            break;
2161 2162 2163
        default:
            goto die;
       }
2164 2165
        break;
    case 9:
2166 2167
        switch (sel) {
        case 0:
T
ths 已提交
2168 2169 2170 2171
            gen_op_mfc0_count();
            rn = "Count";
            break;
        /* 6,7 are implementation dependent */
2172 2173
        default:
            goto die;
T
ths 已提交
2174
        }
2175 2176
        break;
    case 10:
2177 2178
        switch (sel) {
        case 0:
T
ths 已提交
2179 2180 2181
            gen_op_mfc0_entryhi();
            rn = "EntryHi";
            break;
2182 2183
        default:
            goto die;
2184
        }
2185 2186
        break;
    case 11:
2187 2188
        switch (sel) {
        case 0:
T
ths 已提交
2189 2190 2191 2192
            gen_op_mfc0_compare();
            rn = "Compare";
            break;
        /* 6,7 are implementation dependent */
2193 2194
        default:
            goto die;
T
ths 已提交
2195
        }
2196 2197
        break;
    case 12:
2198 2199
        switch (sel) {
        case 0:
T
ths 已提交
2200 2201 2202
            gen_op_mfc0_status();
            rn = "Status";
            break;
2203
        case 1:
2204
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
2205 2206 2207
            gen_op_mfc0_intctl();
            rn = "IntCtl";
            break;
2208
        case 2:
2209
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
2210 2211 2212
            gen_op_mfc0_srsctl();
            rn = "SRSCtl";
            break;
2213
        case 3:
2214
            check_insn(env, ctx, ISA_MIPS32R2);
2215
            gen_op_mfc0_srsmap();
T
ths 已提交
2216
            rn = "SRSMap";
2217
            break;
2218 2219 2220
        default:
            goto die;
       }
2221 2222
        break;
    case 13:
2223 2224
        switch (sel) {
        case 0:
T
ths 已提交
2225 2226 2227
            gen_op_mfc0_cause();
            rn = "Cause";
            break;
2228 2229 2230
        default:
            goto die;
       }
2231 2232
        break;
    case 14:
2233 2234
        switch (sel) {
        case 0:
T
ths 已提交
2235 2236 2237
            gen_op_mfc0_epc();
            rn = "EPC";
            break;
2238 2239
        default:
            goto die;
2240
        }
2241 2242
        break;
    case 15:
2243 2244
        switch (sel) {
        case 0:
T
ths 已提交
2245 2246 2247
            gen_op_mfc0_prid();
            rn = "PRid";
            break;
2248
        case 1:
2249
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
2250 2251 2252
            gen_op_mfc0_ebase();
            rn = "EBase";
            break;
2253 2254 2255
        default:
            goto die;
       }
2256 2257 2258 2259
        break;
    case 16:
        switch (sel) {
        case 0:
2260
            gen_op_mfc0_config0();
2261 2262 2263
            rn = "Config";
            break;
        case 1:
2264
            gen_op_mfc0_config1();
2265 2266
            rn = "Config1";
            break;
2267
        case 2:
2268
            gen_op_mfc0_config2();
2269 2270 2271
            rn = "Config2";
            break;
        case 3:
2272
            gen_op_mfc0_config3();
2273 2274
            rn = "Config3";
            break;
2275 2276 2277 2278 2279 2280 2281 2282 2283 2284
        /* 4,5 are reserved */
        /* 6,7 are implementation dependent */
        case 6:
            gen_op_mfc0_config6();
            rn = "Config6";
            break;
        case 7:
            gen_op_mfc0_config7();
            rn = "Config7";
            break;
2285 2286 2287 2288 2289
        default:
            goto die;
        }
        break;
    case 17:
2290 2291
        switch (sel) {
        case 0:
T
ths 已提交
2292 2293 2294
            gen_op_mfc0_lladdr();
            rn = "LLAddr";
            break;
2295 2296 2297
        default:
            goto die;
        }
2298 2299
        break;
    case 18:
2300
        switch (sel) {
2301 2302
        case 0 ... 7:
            gen_op_mfc0_watchlo(sel);
T
ths 已提交
2303 2304
            rn = "WatchLo";
            break;
2305 2306 2307
        default:
            goto die;
        }
2308 2309
        break;
    case 19:
2310
        switch (sel) {
2311 2312
        case 0 ...7:
            gen_op_mfc0_watchhi(sel);
T
ths 已提交
2313 2314
            rn = "WatchHi";
            break;
2315 2316 2317
        default:
            goto die;
        }
2318
        break;
2319
    case 20:
2320 2321
        switch (sel) {
        case 0:
2322
#if defined(TARGET_MIPS64)
2323
            check_insn(env, ctx, ISA_MIPS3);
T
ths 已提交
2324 2325 2326
            gen_op_mfc0_xcontext();
            rn = "XContext";
            break;
T
ths 已提交
2327
#endif
2328 2329 2330
        default:
            goto die;
        }
2331 2332
        break;
    case 21:
2333 2334 2335
       /* Officially reserved, but sel 0 is used for R1x000 framemask */
        switch (sel) {
        case 0:
T
ths 已提交
2336 2337 2338
            gen_op_mfc0_framemask();
            rn = "Framemask";
            break;
2339 2340 2341
        default:
            goto die;
        }
2342 2343
        break;
    case 22:
T
ths 已提交
2344 2345 2346
        /* ignored */
        rn = "'Diagnostic"; /* implementation dependent */
        break;
2347
    case 23:
2348 2349
        switch (sel) {
        case 0:
T
ths 已提交
2350 2351 2352
            gen_op_mfc0_debug(); /* EJTAG support */
            rn = "Debug";
            break;
2353
        case 1:
T
ths 已提交
2354 2355 2356
//            gen_op_mfc0_tracecontrol(); /* PDtrace support */
            rn = "TraceControl";
//            break;
2357
        case 2:
T
ths 已提交
2358 2359 2360
//            gen_op_mfc0_tracecontrol2(); /* PDtrace support */
            rn = "TraceControl2";
//            break;
2361
        case 3:
T
ths 已提交
2362 2363 2364
//            gen_op_mfc0_usertracedata(); /* PDtrace support */
            rn = "UserTraceData";
//            break;
2365
        case 4:
T
ths 已提交
2366 2367 2368
//            gen_op_mfc0_debug(); /* PDtrace support */
            rn = "TraceBPC";
//            break;
2369 2370 2371
        default:
            goto die;
        }
2372 2373
        break;
    case 24:
2374 2375
        switch (sel) {
        case 0:
T
ths 已提交
2376 2377 2378
            gen_op_mfc0_depc(); /* EJTAG support */
            rn = "DEPC";
            break;
2379 2380 2381
        default:
            goto die;
        }
2382
        break;
2383
    case 25:
2384 2385
        switch (sel) {
        case 0:
T
ths 已提交
2386 2387
            gen_op_mfc0_performance0();
            rn = "Performance0";
2388 2389
            break;
        case 1:
T
ths 已提交
2390 2391 2392
//            gen_op_mfc0_performance1();
            rn = "Performance1";
//            break;
2393
        case 2:
T
ths 已提交
2394 2395 2396
//            gen_op_mfc0_performance2();
            rn = "Performance2";
//            break;
2397
        case 3:
T
ths 已提交
2398 2399 2400
//            gen_op_mfc0_performance3();
            rn = "Performance3";
//            break;
2401
        case 4:
T
ths 已提交
2402 2403 2404
//            gen_op_mfc0_performance4();
            rn = "Performance4";
//            break;
2405
        case 5:
T
ths 已提交
2406 2407 2408
//            gen_op_mfc0_performance5();
            rn = "Performance5";
//            break;
2409
        case 6:
T
ths 已提交
2410 2411 2412
//            gen_op_mfc0_performance6();
            rn = "Performance6";
//            break;
2413
        case 7:
T
ths 已提交
2414 2415 2416
//            gen_op_mfc0_performance7();
            rn = "Performance7";
//            break;
2417 2418 2419
        default:
            goto die;
        }
2420 2421
        break;
    case 26:
2422 2423
       rn = "ECC";
       break;
2424
    case 27:
2425 2426 2427
        switch (sel) {
        /* ignored */
        case 0 ... 3:
T
ths 已提交
2428 2429
            rn = "CacheErr";
            break;
2430 2431 2432
        default:
            goto die;
        }
2433
        break;
2434 2435 2436
    case 28:
        switch (sel) {
        case 0:
2437 2438 2439
        case 2:
        case 4:
        case 6:
2440 2441 2442 2443
            gen_op_mfc0_taglo();
            rn = "TagLo";
            break;
        case 1:
2444 2445 2446
        case 3:
        case 5:
        case 7:
2447 2448 2449 2450 2451 2452 2453
            gen_op_mfc0_datalo();
            rn = "DataLo";
            break;
        default:
            goto die;
        }
        break;
2454
    case 29:
2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472
        switch (sel) {
        case 0:
        case 2:
        case 4:
        case 6:
            gen_op_mfc0_taghi();
            rn = "TagHi";
            break;
        case 1:
        case 3:
        case 5:
        case 7:
            gen_op_mfc0_datahi();
            rn = "DataHi";
            break;
        default:
            goto die;
        }
2473
        break;
2474
    case 30:
2475 2476
        switch (sel) {
        case 0:
T
ths 已提交
2477 2478 2479
            gen_op_mfc0_errorepc();
            rn = "ErrorEPC";
            break;
2480 2481 2482
        default:
            goto die;
        }
2483 2484
        break;
    case 31:
2485 2486
        switch (sel) {
        case 0:
T
ths 已提交
2487 2488 2489
            gen_op_mfc0_desave(); /* EJTAG support */
            rn = "DESAVE";
            break;
2490 2491 2492
        default:
            goto die;
        }
2493 2494 2495 2496 2497 2498
        break;
    default:
       goto die;
    }
#if defined MIPS_DEBUG_DISAS
    if (loglevel & CPU_LOG_TB_IN_ASM) {
2499 2500
        fprintf(logfile, "mfc0 %s (reg %d sel %d)\n",
                rn, reg, sel);
2501 2502 2503 2504 2505 2506 2507
    }
#endif
    return;

die:
#if defined MIPS_DEBUG_DISAS
    if (loglevel & CPU_LOG_TB_IN_ASM) {
2508 2509
        fprintf(logfile, "mfc0 %s (reg %d sel %d)\n",
                rn, reg, sel);
2510 2511 2512 2513 2514
    }
#endif
    generate_exception(ctx, EXCP_RI);
}

2515
static void gen_mtc0 (CPUState *env, DisasContext *ctx, int reg, int sel)
2516
{
2517 2518
    const char *rn = "invalid";

2519 2520 2521
    if (sel != 0)
        check_insn(env, ctx, ISA_MIPS32);

2522 2523
    switch (reg) {
    case 0:
2524 2525
        switch (sel) {
        case 0:
T
ths 已提交
2526
            gen_op_mtc0_index();
2527 2528 2529
            rn = "Index";
            break;
        case 1:
2530
            check_insn(env, ctx, ASE_MT);
2531
            gen_op_mtc0_mvpcontrol();
2532
            rn = "MVPControl";
2533
            break;
2534
        case 2:
2535
            check_insn(env, ctx, ASE_MT);
2536
            /* ignored */
2537
            rn = "MVPConf0";
2538
            break;
2539
        case 3:
2540
            check_insn(env, ctx, ASE_MT);
2541
            /* ignored */
2542
            rn = "MVPConf1";
2543
            break;
2544 2545 2546
        default:
            goto die;
        }
2547 2548
        break;
    case 1:
2549 2550
        switch (sel) {
        case 0:
T
ths 已提交
2551
            /* ignored */
2552
            rn = "Random";
T
ths 已提交
2553
            break;
2554
        case 1:
2555
            check_insn(env, ctx, ASE_MT);
2556
            gen_op_mtc0_vpecontrol();
2557
            rn = "VPEControl";
2558
            break;
2559
        case 2:
2560
            check_insn(env, ctx, ASE_MT);
2561
            gen_op_mtc0_vpeconf0();
2562
            rn = "VPEConf0";
2563
            break;
2564
        case 3:
2565
            check_insn(env, ctx, ASE_MT);
2566
            gen_op_mtc0_vpeconf1();
2567
            rn = "VPEConf1";
2568
            break;
2569
        case 4:
2570
            check_insn(env, ctx, ASE_MT);
2571
            gen_op_mtc0_yqmask();
2572
            rn = "YQMask";
2573
            break;
2574
        case 5:
2575
            check_insn(env, ctx, ASE_MT);
2576
            gen_op_mtc0_vpeschedule();
2577
            rn = "VPESchedule";
2578
            break;
2579
        case 6:
2580
            check_insn(env, ctx, ASE_MT);
2581
            gen_op_mtc0_vpeschefback();
2582
            rn = "VPEScheFBack";
2583
            break;
2584
        case 7:
2585
            check_insn(env, ctx, ASE_MT);
2586
            gen_op_mtc0_vpeopt();
2587
            rn = "VPEOpt";
2588
            break;
2589 2590 2591
        default:
            goto die;
        }
2592 2593
        break;
    case 2:
2594 2595
        switch (sel) {
        case 0:
T
ths 已提交
2596 2597 2598
            gen_op_mtc0_entrylo0();
            rn = "EntryLo0";
            break;
2599
        case 1:
2600
            check_insn(env, ctx, ASE_MT);
2601
            gen_op_mtc0_tcstatus();
T
ths 已提交
2602
            rn = "TCStatus";
2603
            break;
2604
        case 2:
2605
            check_insn(env, ctx, ASE_MT);
2606
            gen_op_mtc0_tcbind();
T
ths 已提交
2607
            rn = "TCBind";
2608
            break;
2609
        case 3:
2610
            check_insn(env, ctx, ASE_MT);
2611
            gen_op_mtc0_tcrestart();
T
ths 已提交
2612
            rn = "TCRestart";
2613
            break;
2614
        case 4:
2615
            check_insn(env, ctx, ASE_MT);
2616
            gen_op_mtc0_tchalt();
T
ths 已提交
2617
            rn = "TCHalt";
2618
            break;
2619
        case 5:
2620
            check_insn(env, ctx, ASE_MT);
2621
            gen_op_mtc0_tccontext();
T
ths 已提交
2622
            rn = "TCContext";
2623
            break;
2624
        case 6:
2625
            check_insn(env, ctx, ASE_MT);
2626
            gen_op_mtc0_tcschedule();
T
ths 已提交
2627
            rn = "TCSchedule";
2628
            break;
2629
        case 7:
2630
            check_insn(env, ctx, ASE_MT);
2631
            gen_op_mtc0_tcschefback();
T
ths 已提交
2632
            rn = "TCScheFBack";
2633
            break;
2634 2635 2636
        default:
            goto die;
        }
2637 2638
        break;
    case 3:
2639 2640
        switch (sel) {
        case 0:
T
ths 已提交
2641 2642 2643
            gen_op_mtc0_entrylo1();
            rn = "EntryLo1";
            break;
2644 2645
        default:
            goto die;
T
ths 已提交
2646
        }
2647 2648
        break;
    case 4:
2649 2650
        switch (sel) {
        case 0:
T
ths 已提交
2651 2652 2653
            gen_op_mtc0_context();
            rn = "Context";
            break;
2654
        case 1:
T
ths 已提交
2655 2656 2657
//            gen_op_mtc0_contextconfig(); /* SmartMIPS ASE */
            rn = "ContextConfig";
//            break;
2658 2659
        default:
            goto die;
T
ths 已提交
2660
        }
2661 2662
        break;
    case 5:
2663 2664
        switch (sel) {
        case 0:
T
ths 已提交
2665 2666 2667
            gen_op_mtc0_pagemask();
            rn = "PageMask";
            break;
2668
        case 1:
2669
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
2670 2671 2672
            gen_op_mtc0_pagegrain();
            rn = "PageGrain";
            break;
2673 2674
        default:
            goto die;
T
ths 已提交
2675
        }
2676 2677
        break;
    case 6:
2678 2679
        switch (sel) {
        case 0:
T
ths 已提交
2680 2681 2682
            gen_op_mtc0_wired();
            rn = "Wired";
            break;
2683
        case 1:
2684
            check_insn(env, ctx, ISA_MIPS32R2);
2685
            gen_op_mtc0_srsconf0();
T
ths 已提交
2686
            rn = "SRSConf0";
2687
            break;
2688
        case 2:
2689
            check_insn(env, ctx, ISA_MIPS32R2);
2690
            gen_op_mtc0_srsconf1();
T
ths 已提交
2691
            rn = "SRSConf1";
2692
            break;
2693
        case 3:
2694
            check_insn(env, ctx, ISA_MIPS32R2);
2695
            gen_op_mtc0_srsconf2();
T
ths 已提交
2696
            rn = "SRSConf2";
2697
            break;
2698
        case 4:
2699
            check_insn(env, ctx, ISA_MIPS32R2);
2700
            gen_op_mtc0_srsconf3();
T
ths 已提交
2701
            rn = "SRSConf3";
2702
            break;
2703
        case 5:
2704
            check_insn(env, ctx, ISA_MIPS32R2);
2705
            gen_op_mtc0_srsconf4();
T
ths 已提交
2706
            rn = "SRSConf4";
2707
            break;
2708 2709
        default:
            goto die;
T
ths 已提交
2710
        }
2711 2712
        break;
    case 7:
2713 2714
        switch (sel) {
        case 0:
2715
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
2716 2717 2718
            gen_op_mtc0_hwrena();
            rn = "HWREna";
            break;
2719 2720
        default:
            goto die;
T
ths 已提交
2721
        }
2722 2723
        break;
    case 8:
2724
        /* ignored */
2725 2726 2727
        rn = "BadVaddr";
        break;
    case 9:
2728 2729
        switch (sel) {
        case 0:
T
ths 已提交
2730 2731 2732
            gen_op_mtc0_count();
            rn = "Count";
            break;
T
ths 已提交
2733
        /* 6,7 are implementation dependent */
2734 2735
        default:
            goto die;
T
ths 已提交
2736 2737 2738
        }
        /* Stop translation as we may have switched the execution mode */
        ctx->bstate = BS_STOP;
2739 2740
        break;
    case 10:
2741 2742
        switch (sel) {
        case 0:
T
ths 已提交
2743 2744 2745
            gen_op_mtc0_entryhi();
            rn = "EntryHi";
            break;
2746 2747
        default:
            goto die;
T
ths 已提交
2748
        }
2749 2750
        break;
    case 11:
2751 2752
        switch (sel) {
        case 0:
T
ths 已提交
2753 2754 2755 2756
            gen_op_mtc0_compare();
            rn = "Compare";
            break;
        /* 6,7 are implementation dependent */
2757 2758
        default:
            goto die;
T
ths 已提交
2759 2760 2761
        }
        /* Stop translation as we may have switched the execution mode */
        ctx->bstate = BS_STOP;
2762 2763
        break;
    case 12:
2764 2765
        switch (sel) {
        case 0:
T
ths 已提交
2766
            gen_op_mtc0_status();
2767 2768 2769
            /* BS_STOP isn't good enough here, hflags may have changed. */
            gen_save_pc(ctx->pc + 4);
            ctx->bstate = BS_EXCP;
T
ths 已提交
2770 2771
            rn = "Status";
            break;
2772
        case 1:
2773
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
2774
            gen_op_mtc0_intctl();
2775 2776
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
2777 2778
            rn = "IntCtl";
            break;
2779
        case 2:
2780
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
2781
            gen_op_mtc0_srsctl();
2782 2783
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
2784 2785
            rn = "SRSCtl";
            break;
2786
        case 3:
2787
            check_insn(env, ctx, ISA_MIPS32R2);
2788
            gen_op_mtc0_srsmap();
2789 2790
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
2791
            rn = "SRSMap";
2792
            break;
2793 2794
        default:
            goto die;
T
ths 已提交
2795
        }
2796 2797
        break;
    case 13:
2798 2799
        switch (sel) {
        case 0:
T
ths 已提交
2800 2801 2802
            gen_op_mtc0_cause();
            rn = "Cause";
            break;
2803 2804
        default:
            goto die;
T
ths 已提交
2805 2806 2807
        }
        /* Stop translation as we may have switched the execution mode */
        ctx->bstate = BS_STOP;
2808 2809
        break;
    case 14:
2810 2811
        switch (sel) {
        case 0:
T
ths 已提交
2812 2813 2814
            gen_op_mtc0_epc();
            rn = "EPC";
            break;
2815 2816
        default:
            goto die;
T
ths 已提交
2817
        }
2818 2819
        break;
    case 15:
2820 2821
        switch (sel) {
        case 0:
T
ths 已提交
2822 2823 2824
            /* ignored */
            rn = "PRid";
            break;
2825
        case 1:
2826
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
2827 2828 2829
            gen_op_mtc0_ebase();
            rn = "EBase";
            break;
2830 2831
        default:
            goto die;
2832
        }
2833 2834 2835 2836
        break;
    case 16:
        switch (sel) {
        case 0:
2837
            gen_op_mtc0_config0();
2838
            rn = "Config";
T
ths 已提交
2839 2840
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
2841 2842
            break;
        case 1:
2843
            /* ignored, read only */
2844 2845 2846
            rn = "Config1";
            break;
        case 2:
2847
            gen_op_mtc0_config2();
2848
            rn = "Config2";
T
ths 已提交
2849 2850
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
2851
            break;
2852
        case 3:
2853
            /* ignored, read only */
2854 2855
            rn = "Config3";
            break;
2856 2857 2858 2859 2860 2861 2862 2863 2864 2865
        /* 4,5 are reserved */
        /* 6,7 are implementation dependent */
        case 6:
            /* ignored */
            rn = "Config6";
            break;
        case 7:
            /* ignored */
            rn = "Config7";
            break;
2866 2867 2868 2869 2870 2871
        default:
            rn = "Invalid config selector";
            goto die;
        }
        break;
    case 17:
2872 2873
        switch (sel) {
        case 0:
T
ths 已提交
2874 2875 2876
            /* ignored */
            rn = "LLAddr";
            break;
2877 2878 2879
        default:
            goto die;
        }
2880 2881
        break;
    case 18:
2882
        switch (sel) {
2883 2884
        case 0 ... 7:
            gen_op_mtc0_watchlo(sel);
T
ths 已提交
2885 2886
            rn = "WatchLo";
            break;
2887 2888 2889
        default:
            goto die;
        }
2890 2891
        break;
    case 19:
2892
        switch (sel) {
2893 2894
        case 0 ... 7:
            gen_op_mtc0_watchhi(sel);
T
ths 已提交
2895 2896
            rn = "WatchHi";
            break;
2897 2898 2899
        default:
            goto die;
        }
2900 2901
        break;
    case 20:
2902 2903
        switch (sel) {
        case 0:
2904
#if defined(TARGET_MIPS64)
2905
            check_insn(env, ctx, ISA_MIPS3);
2906
            gen_op_mtc0_xcontext();
T
ths 已提交
2907 2908
            rn = "XContext";
            break;
T
ths 已提交
2909
#endif
2910 2911 2912
        default:
            goto die;
        }
2913 2914
        break;
    case 21:
2915 2916 2917
       /* Officially reserved, but sel 0 is used for R1x000 framemask */
        switch (sel) {
        case 0:
T
ths 已提交
2918 2919 2920
            gen_op_mtc0_framemask();
            rn = "Framemask";
            break;
2921 2922 2923 2924
        default:
            goto die;
        }
        break;
2925
    case 22:
2926 2927
        /* ignored */
        rn = "Diagnostic"; /* implementation dependent */
T
ths 已提交
2928
        break;
2929
    case 23:
2930 2931
        switch (sel) {
        case 0:
T
ths 已提交
2932
            gen_op_mtc0_debug(); /* EJTAG support */
2933 2934 2935
            /* BS_STOP isn't good enough here, hflags may have changed. */
            gen_save_pc(ctx->pc + 4);
            ctx->bstate = BS_EXCP;
T
ths 已提交
2936 2937
            rn = "Debug";
            break;
2938
        case 1:
T
ths 已提交
2939 2940
//            gen_op_mtc0_tracecontrol(); /* PDtrace support */
            rn = "TraceControl";
2941 2942
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
2943
//            break;
2944
        case 2:
T
ths 已提交
2945 2946
//            gen_op_mtc0_tracecontrol2(); /* PDtrace support */
            rn = "TraceControl2";
2947 2948
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
2949
//            break;
2950
        case 3:
2951 2952
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
2953 2954
//            gen_op_mtc0_usertracedata(); /* PDtrace support */
            rn = "UserTraceData";
2955 2956
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
2957
//            break;
2958
        case 4:
T
ths 已提交
2959
//            gen_op_mtc0_debug(); /* PDtrace support */
2960 2961
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
2962 2963
            rn = "TraceBPC";
//            break;
2964 2965 2966
        default:
            goto die;
        }
2967 2968
        break;
    case 24:
2969 2970
        switch (sel) {
        case 0:
T
ths 已提交
2971 2972 2973
            gen_op_mtc0_depc(); /* EJTAG support */
            rn = "DEPC";
            break;
2974 2975 2976
        default:
            goto die;
        }
2977 2978
        break;
    case 25:
2979 2980
        switch (sel) {
        case 0:
T
ths 已提交
2981 2982 2983
            gen_op_mtc0_performance0();
            rn = "Performance0";
            break;
2984
        case 1:
T
ths 已提交
2985 2986 2987
//            gen_op_mtc0_performance1();
            rn = "Performance1";
//            break;
2988
        case 2:
T
ths 已提交
2989 2990 2991
//            gen_op_mtc0_performance2();
            rn = "Performance2";
//            break;
2992
        case 3:
T
ths 已提交
2993 2994 2995
//            gen_op_mtc0_performance3();
            rn = "Performance3";
//            break;
2996
        case 4:
T
ths 已提交
2997 2998 2999
//            gen_op_mtc0_performance4();
            rn = "Performance4";
//            break;
3000
        case 5:
T
ths 已提交
3001 3002 3003
//            gen_op_mtc0_performance5();
            rn = "Performance5";
//            break;
3004
        case 6:
T
ths 已提交
3005 3006 3007
//            gen_op_mtc0_performance6();
            rn = "Performance6";
//            break;
3008
        case 7:
T
ths 已提交
3009 3010 3011
//            gen_op_mtc0_performance7();
            rn = "Performance7";
//            break;
3012 3013 3014
        default:
            goto die;
        }
3015 3016
       break;
    case 26:
T
ths 已提交
3017
        /* ignored */
3018
        rn = "ECC";
T
ths 已提交
3019
        break;
3020
    case 27:
3021 3022
        switch (sel) {
        case 0 ... 3:
T
ths 已提交
3023 3024 3025
            /* ignored */
            rn = "CacheErr";
            break;
3026 3027 3028
        default:
            goto die;
        }
3029 3030 3031 3032
       break;
    case 28:
        switch (sel) {
        case 0:
3033 3034 3035
        case 2:
        case 4:
        case 6:
3036 3037 3038
            gen_op_mtc0_taglo();
            rn = "TagLo";
            break;
3039 3040 3041 3042
        case 1:
        case 3:
        case 5:
        case 7:
T
ths 已提交
3043
            gen_op_mtc0_datalo();
3044 3045
            rn = "DataLo";
            break;
3046 3047 3048 3049 3050
        default:
            goto die;
        }
        break;
    case 29:
3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062
        switch (sel) {
        case 0:
        case 2:
        case 4:
        case 6:
            gen_op_mtc0_taghi();
            rn = "TagHi";
            break;
        case 1:
        case 3:
        case 5:
        case 7:
T
ths 已提交
3063
            gen_op_mtc0_datahi();
3064 3065 3066 3067 3068 3069
            rn = "DataHi";
            break;
        default:
            rn = "invalid sel";
            goto die;
        }
3070 3071
       break;
    case 30:
3072 3073
        switch (sel) {
        case 0:
T
ths 已提交
3074 3075 3076
            gen_op_mtc0_errorepc();
            rn = "ErrorEPC";
            break;
3077 3078 3079
        default:
            goto die;
        }
3080 3081
        break;
    case 31:
3082 3083
        switch (sel) {
        case 0:
T
ths 已提交
3084 3085 3086
            gen_op_mtc0_desave(); /* EJTAG support */
            rn = "DESAVE";
            break;
3087 3088 3089
        default:
            goto die;
        }
T
ths 已提交
3090 3091
        /* Stop translation as we may have switched the execution mode */
        ctx->bstate = BS_STOP;
3092 3093 3094 3095 3096 3097
        break;
    default:
       goto die;
    }
#if defined MIPS_DEBUG_DISAS
    if (loglevel & CPU_LOG_TB_IN_ASM) {
3098 3099
        fprintf(logfile, "mtc0 %s (reg %d sel %d)\n",
                rn, reg, sel);
3100 3101 3102 3103 3104 3105 3106
    }
#endif
    return;

die:
#if defined MIPS_DEBUG_DISAS
    if (loglevel & CPU_LOG_TB_IN_ASM) {
3107 3108
        fprintf(logfile, "mtc0 %s (reg %d sel %d)\n",
                rn, reg, sel);
3109 3110 3111 3112 3113
    }
#endif
    generate_exception(ctx, EXCP_RI);
}

3114
#if defined(TARGET_MIPS64)
3115
static void gen_dmfc0 (CPUState *env, DisasContext *ctx, int reg, int sel)
T
ths 已提交
3116 3117 3118
{
    const char *rn = "invalid";

3119 3120 3121
    if (sel != 0)
        check_insn(env, ctx, ISA_MIPS64);

T
ths 已提交
3122 3123 3124 3125
    switch (reg) {
    case 0:
        switch (sel) {
        case 0:
T
ths 已提交
3126
            gen_op_mfc0_index();
T
ths 已提交
3127 3128 3129
            rn = "Index";
            break;
        case 1:
3130
            check_insn(env, ctx, ASE_MT);
3131
            gen_op_mfc0_mvpcontrol();
T
ths 已提交
3132
            rn = "MVPControl";
3133
            break;
T
ths 已提交
3134
        case 2:
3135
            check_insn(env, ctx, ASE_MT);
3136
            gen_op_mfc0_mvpconf0();
T
ths 已提交
3137
            rn = "MVPConf0";
3138
            break;
T
ths 已提交
3139
        case 3:
3140
            check_insn(env, ctx, ASE_MT);
3141
            gen_op_mfc0_mvpconf1();
T
ths 已提交
3142
            rn = "MVPConf1";
3143
            break;
T
ths 已提交
3144 3145 3146 3147 3148 3149 3150 3151 3152
        default:
            goto die;
        }
        break;
    case 1:
        switch (sel) {
        case 0:
            gen_op_mfc0_random();
            rn = "Random";
T
ths 已提交
3153
            break;
T
ths 已提交
3154
        case 1:
3155
            check_insn(env, ctx, ASE_MT);
3156
            gen_op_mfc0_vpecontrol();
T
ths 已提交
3157
            rn = "VPEControl";
3158
            break;
T
ths 已提交
3159
        case 2:
3160
            check_insn(env, ctx, ASE_MT);
3161
            gen_op_mfc0_vpeconf0();
T
ths 已提交
3162
            rn = "VPEConf0";
3163
            break;
T
ths 已提交
3164
        case 3:
3165
            check_insn(env, ctx, ASE_MT);
3166
            gen_op_mfc0_vpeconf1();
T
ths 已提交
3167
            rn = "VPEConf1";
3168
            break;
T
ths 已提交
3169
        case 4:
3170
            check_insn(env, ctx, ASE_MT);
3171
            gen_op_dmfc0_yqmask();
T
ths 已提交
3172
            rn = "YQMask";
3173
            break;
T
ths 已提交
3174
        case 5:
3175
            check_insn(env, ctx, ASE_MT);
3176
            gen_op_dmfc0_vpeschedule();
T
ths 已提交
3177
            rn = "VPESchedule";
3178
            break;
T
ths 已提交
3179
        case 6:
3180
            check_insn(env, ctx, ASE_MT);
3181
            gen_op_dmfc0_vpeschefback();
T
ths 已提交
3182
            rn = "VPEScheFBack";
3183
            break;
T
ths 已提交
3184
        case 7:
3185
            check_insn(env, ctx, ASE_MT);
3186
            gen_op_mfc0_vpeopt();
T
ths 已提交
3187
            rn = "VPEOpt";
3188
            break;
T
ths 已提交
3189 3190 3191 3192 3193 3194 3195
        default:
            goto die;
        }
        break;
    case 2:
        switch (sel) {
        case 0:
T
ths 已提交
3196 3197 3198
            gen_op_dmfc0_entrylo0();
            rn = "EntryLo0";
            break;
T
ths 已提交
3199
        case 1:
3200
            check_insn(env, ctx, ASE_MT);
3201
            gen_op_mfc0_tcstatus();
T
ths 已提交
3202
            rn = "TCStatus";
3203
            break;
T
ths 已提交
3204
        case 2:
3205
            check_insn(env, ctx, ASE_MT);
3206
            gen_op_mfc0_tcbind();
T
ths 已提交
3207
            rn = "TCBind";
3208
            break;
T
ths 已提交
3209
        case 3:
3210
            check_insn(env, ctx, ASE_MT);
3211
            gen_op_dmfc0_tcrestart();
T
ths 已提交
3212
            rn = "TCRestart";
3213
            break;
T
ths 已提交
3214
        case 4:
3215
            check_insn(env, ctx, ASE_MT);
3216
            gen_op_dmfc0_tchalt();
T
ths 已提交
3217
            rn = "TCHalt";
3218
            break;
T
ths 已提交
3219
        case 5:
3220
            check_insn(env, ctx, ASE_MT);
3221
            gen_op_dmfc0_tccontext();
T
ths 已提交
3222
            rn = "TCContext";
3223
            break;
T
ths 已提交
3224
        case 6:
3225
            check_insn(env, ctx, ASE_MT);
3226
            gen_op_dmfc0_tcschedule();
T
ths 已提交
3227
            rn = "TCSchedule";
3228
            break;
T
ths 已提交
3229
        case 7:
3230
            check_insn(env, ctx, ASE_MT);
3231
            gen_op_dmfc0_tcschefback();
T
ths 已提交
3232
            rn = "TCScheFBack";
3233
            break;
T
ths 已提交
3234 3235 3236 3237 3238 3239 3240
        default:
            goto die;
        }
        break;
    case 3:
        switch (sel) {
        case 0:
T
ths 已提交
3241 3242 3243
            gen_op_dmfc0_entrylo1();
            rn = "EntryLo1";
            break;
T
ths 已提交
3244 3245
        default:
            goto die;
3246
        }
T
ths 已提交
3247 3248 3249 3250
        break;
    case 4:
        switch (sel) {
        case 0:
T
ths 已提交
3251 3252 3253
            gen_op_dmfc0_context();
            rn = "Context";
            break;
T
ths 已提交
3254
        case 1:
T
ths 已提交
3255 3256 3257
//            gen_op_dmfc0_contextconfig(); /* SmartMIPS ASE */
            rn = "ContextConfig";
//            break;
T
ths 已提交
3258 3259
        default:
            goto die;
T
ths 已提交
3260
        }
T
ths 已提交
3261 3262 3263 3264
        break;
    case 5:
        switch (sel) {
        case 0:
T
ths 已提交
3265 3266 3267
            gen_op_mfc0_pagemask();
            rn = "PageMask";
            break;
T
ths 已提交
3268
        case 1:
3269
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
3270 3271 3272
            gen_op_mfc0_pagegrain();
            rn = "PageGrain";
            break;
T
ths 已提交
3273 3274
        default:
            goto die;
T
ths 已提交
3275
        }
T
ths 已提交
3276 3277 3278 3279
        break;
    case 6:
        switch (sel) {
        case 0:
T
ths 已提交
3280 3281 3282
            gen_op_mfc0_wired();
            rn = "Wired";
            break;
T
ths 已提交
3283
        case 1:
3284
            check_insn(env, ctx, ISA_MIPS32R2);
3285
            gen_op_mfc0_srsconf0();
T
ths 已提交
3286
            rn = "SRSConf0";
3287
            break;
T
ths 已提交
3288
        case 2:
3289
            check_insn(env, ctx, ISA_MIPS32R2);
3290
            gen_op_mfc0_srsconf1();
T
ths 已提交
3291
            rn = "SRSConf1";
3292
            break;
T
ths 已提交
3293
        case 3:
3294
            check_insn(env, ctx, ISA_MIPS32R2);
3295
            gen_op_mfc0_srsconf2();
T
ths 已提交
3296
            rn = "SRSConf2";
3297
            break;
T
ths 已提交
3298
        case 4:
3299
            check_insn(env, ctx, ISA_MIPS32R2);
3300
            gen_op_mfc0_srsconf3();
T
ths 已提交
3301
            rn = "SRSConf3";
3302
            break;
T
ths 已提交
3303
        case 5:
3304
            check_insn(env, ctx, ISA_MIPS32R2);
3305
            gen_op_mfc0_srsconf4();
T
ths 已提交
3306
            rn = "SRSConf4";
3307
            break;
T
ths 已提交
3308 3309
        default:
            goto die;
T
ths 已提交
3310
        }
T
ths 已提交
3311 3312 3313 3314
        break;
    case 7:
        switch (sel) {
        case 0:
3315
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
3316 3317 3318
            gen_op_mfc0_hwrena();
            rn = "HWREna";
            break;
T
ths 已提交
3319 3320
        default:
            goto die;
T
ths 已提交
3321
        }
T
ths 已提交
3322 3323 3324 3325
        break;
    case 8:
        switch (sel) {
        case 0:
T
ths 已提交
3326 3327 3328
            gen_op_dmfc0_badvaddr();
            rn = "BadVaddr";
            break;
T
ths 已提交
3329 3330
        default:
            goto die;
T
ths 已提交
3331
        }
T
ths 已提交
3332 3333 3334 3335
        break;
    case 9:
        switch (sel) {
        case 0:
T
ths 已提交
3336 3337 3338 3339
            gen_op_mfc0_count();
            rn = "Count";
            break;
        /* 6,7 are implementation dependent */
T
ths 已提交
3340 3341
        default:
            goto die;
T
ths 已提交
3342
        }
T
ths 已提交
3343 3344 3345 3346
        break;
    case 10:
        switch (sel) {
        case 0:
T
ths 已提交
3347 3348 3349
            gen_op_dmfc0_entryhi();
            rn = "EntryHi";
            break;
T
ths 已提交
3350 3351
        default:
            goto die;
T
ths 已提交
3352
        }
T
ths 已提交
3353 3354 3355 3356
        break;
    case 11:
        switch (sel) {
        case 0:
T
ths 已提交
3357 3358 3359
            gen_op_mfc0_compare();
            rn = "Compare";
            break;
T
ths 已提交
3360
        /* 6,7 are implementation dependent */
T
ths 已提交
3361 3362
        default:
            goto die;
T
ths 已提交
3363
        }
T
ths 已提交
3364 3365 3366 3367
        break;
    case 12:
        switch (sel) {
        case 0:
T
ths 已提交
3368 3369 3370
            gen_op_mfc0_status();
            rn = "Status";
            break;
T
ths 已提交
3371
        case 1:
3372
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
3373 3374 3375
            gen_op_mfc0_intctl();
            rn = "IntCtl";
            break;
T
ths 已提交
3376
        case 2:
3377
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
3378 3379 3380
            gen_op_mfc0_srsctl();
            rn = "SRSCtl";
            break;
T
ths 已提交
3381
        case 3:
3382
            check_insn(env, ctx, ISA_MIPS32R2);
3383
            gen_op_mfc0_srsmap();
T
ths 已提交
3384 3385
            rn = "SRSMap";
            break;
T
ths 已提交
3386 3387
        default:
            goto die;
T
ths 已提交
3388
        }
T
ths 已提交
3389 3390 3391 3392
        break;
    case 13:
        switch (sel) {
        case 0:
T
ths 已提交
3393 3394 3395
            gen_op_mfc0_cause();
            rn = "Cause";
            break;
T
ths 已提交
3396 3397
        default:
            goto die;
T
ths 已提交
3398
        }
T
ths 已提交
3399 3400 3401 3402
        break;
    case 14:
        switch (sel) {
        case 0:
T
ths 已提交
3403 3404 3405
            gen_op_dmfc0_epc();
            rn = "EPC";
            break;
T
ths 已提交
3406 3407
        default:
            goto die;
T
ths 已提交
3408
        }
T
ths 已提交
3409 3410 3411 3412
        break;
    case 15:
        switch (sel) {
        case 0:
T
ths 已提交
3413 3414 3415
            gen_op_mfc0_prid();
            rn = "PRid";
            break;
T
ths 已提交
3416
        case 1:
3417
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
3418 3419 3420
            gen_op_mfc0_ebase();
            rn = "EBase";
            break;
T
ths 已提交
3421 3422
        default:
            goto die;
T
ths 已提交
3423
        }
T
ths 已提交
3424 3425 3426 3427
        break;
    case 16:
        switch (sel) {
        case 0:
T
ths 已提交
3428
            gen_op_mfc0_config0();
T
ths 已提交
3429 3430 3431
            rn = "Config";
            break;
        case 1:
T
ths 已提交
3432
            gen_op_mfc0_config1();
T
ths 已提交
3433 3434 3435
            rn = "Config1";
            break;
        case 2:
T
ths 已提交
3436
            gen_op_mfc0_config2();
T
ths 已提交
3437 3438 3439
            rn = "Config2";
            break;
        case 3:
T
ths 已提交
3440
            gen_op_mfc0_config3();
T
ths 已提交
3441 3442 3443 3444 3445 3446 3447 3448 3449 3450
            rn = "Config3";
            break;
       /* 6,7 are implementation dependent */
        default:
            goto die;
        }
        break;
    case 17:
        switch (sel) {
        case 0:
T
ths 已提交
3451 3452 3453
            gen_op_dmfc0_lladdr();
            rn = "LLAddr";
            break;
T
ths 已提交
3454 3455 3456 3457 3458 3459
        default:
            goto die;
        }
        break;
    case 18:
        switch (sel) {
3460 3461
        case 0 ... 7:
            gen_op_dmfc0_watchlo(sel);
T
ths 已提交
3462 3463
            rn = "WatchLo";
            break;
T
ths 已提交
3464 3465 3466 3467 3468 3469
        default:
            goto die;
        }
        break;
    case 19:
        switch (sel) {
3470 3471
        case 0 ... 7:
            gen_op_mfc0_watchhi(sel);
T
ths 已提交
3472 3473
            rn = "WatchHi";
            break;
T
ths 已提交
3474 3475 3476 3477 3478 3479 3480
        default:
            goto die;
        }
        break;
    case 20:
        switch (sel) {
        case 0:
3481
            check_insn(env, ctx, ISA_MIPS3);
T
ths 已提交
3482 3483 3484
            gen_op_dmfc0_xcontext();
            rn = "XContext";
            break;
T
ths 已提交
3485 3486 3487 3488 3489 3490 3491 3492
        default:
            goto die;
        }
        break;
    case 21:
       /* Officially reserved, but sel 0 is used for R1x000 framemask */
        switch (sel) {
        case 0:
T
ths 已提交
3493 3494 3495
            gen_op_mfc0_framemask();
            rn = "Framemask";
            break;
T
ths 已提交
3496 3497 3498 3499 3500
        default:
            goto die;
        }
        break;
    case 22:
T
ths 已提交
3501 3502 3503
        /* ignored */
        rn = "'Diagnostic"; /* implementation dependent */
        break;
T
ths 已提交
3504 3505 3506
    case 23:
        switch (sel) {
        case 0:
T
ths 已提交
3507 3508 3509
            gen_op_mfc0_debug(); /* EJTAG support */
            rn = "Debug";
            break;
T
ths 已提交
3510
        case 1:
T
ths 已提交
3511 3512 3513
//            gen_op_dmfc0_tracecontrol(); /* PDtrace support */
            rn = "TraceControl";
//            break;
T
ths 已提交
3514
        case 2:
T
ths 已提交
3515 3516 3517
//            gen_op_dmfc0_tracecontrol2(); /* PDtrace support */
            rn = "TraceControl2";
//            break;
T
ths 已提交
3518
        case 3:
T
ths 已提交
3519 3520 3521
//            gen_op_dmfc0_usertracedata(); /* PDtrace support */
            rn = "UserTraceData";
//            break;
T
ths 已提交
3522
        case 4:
T
ths 已提交
3523 3524 3525
//            gen_op_dmfc0_debug(); /* PDtrace support */
            rn = "TraceBPC";
//            break;
T
ths 已提交
3526 3527 3528 3529 3530 3531 3532
        default:
            goto die;
        }
        break;
    case 24:
        switch (sel) {
        case 0:
T
ths 已提交
3533 3534 3535
            gen_op_dmfc0_depc(); /* EJTAG support */
            rn = "DEPC";
            break;
T
ths 已提交
3536 3537 3538 3539 3540 3541 3542
        default:
            goto die;
        }
        break;
    case 25:
        switch (sel) {
        case 0:
T
ths 已提交
3543 3544
            gen_op_mfc0_performance0();
            rn = "Performance0";
T
ths 已提交
3545 3546
            break;
        case 1:
T
ths 已提交
3547 3548 3549
//            gen_op_dmfc0_performance1();
            rn = "Performance1";
//            break;
T
ths 已提交
3550
        case 2:
T
ths 已提交
3551 3552 3553
//            gen_op_dmfc0_performance2();
            rn = "Performance2";
//            break;
T
ths 已提交
3554
        case 3:
T
ths 已提交
3555 3556 3557
//            gen_op_dmfc0_performance3();
            rn = "Performance3";
//            break;
T
ths 已提交
3558
        case 4:
T
ths 已提交
3559 3560 3561
//            gen_op_dmfc0_performance4();
            rn = "Performance4";
//            break;
T
ths 已提交
3562
        case 5:
T
ths 已提交
3563 3564 3565
//            gen_op_dmfc0_performance5();
            rn = "Performance5";
//            break;
T
ths 已提交
3566
        case 6:
T
ths 已提交
3567 3568 3569
//            gen_op_dmfc0_performance6();
            rn = "Performance6";
//            break;
T
ths 已提交
3570
        case 7:
T
ths 已提交
3571 3572 3573
//            gen_op_dmfc0_performance7();
            rn = "Performance7";
//            break;
T
ths 已提交
3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584
        default:
            goto die;
        }
        break;
    case 26:
       rn = "ECC";
       break;
    case 27:
        switch (sel) {
        /* ignored */
        case 0 ... 3:
T
ths 已提交
3585 3586
            rn = "CacheErr";
            break;
T
ths 已提交
3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633
        default:
            goto die;
        }
        break;
    case 28:
        switch (sel) {
        case 0:
        case 2:
        case 4:
        case 6:
            gen_op_mfc0_taglo();
            rn = "TagLo";
            break;
        case 1:
        case 3:
        case 5:
        case 7:
            gen_op_mfc0_datalo();
            rn = "DataLo";
            break;
        default:
            goto die;
        }
        break;
    case 29:
        switch (sel) {
        case 0:
        case 2:
        case 4:
        case 6:
            gen_op_mfc0_taghi();
            rn = "TagHi";
            break;
        case 1:
        case 3:
        case 5:
        case 7:
            gen_op_mfc0_datahi();
            rn = "DataHi";
            break;
        default:
            goto die;
        }
        break;
    case 30:
        switch (sel) {
        case 0:
T
ths 已提交
3634 3635 3636
            gen_op_dmfc0_errorepc();
            rn = "ErrorEPC";
            break;
T
ths 已提交
3637 3638 3639 3640 3641 3642 3643
        default:
            goto die;
        }
        break;
    case 31:
        switch (sel) {
        case 0:
T
ths 已提交
3644 3645 3646
            gen_op_mfc0_desave(); /* EJTAG support */
            rn = "DESAVE";
            break;
T
ths 已提交
3647 3648 3649 3650 3651
        default:
            goto die;
        }
        break;
    default:
T
ths 已提交
3652
        goto die;
T
ths 已提交
3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671
    }
#if defined MIPS_DEBUG_DISAS
    if (loglevel & CPU_LOG_TB_IN_ASM) {
        fprintf(logfile, "dmfc0 %s (reg %d sel %d)\n",
                rn, reg, sel);
    }
#endif
    return;

die:
#if defined MIPS_DEBUG_DISAS
    if (loglevel & CPU_LOG_TB_IN_ASM) {
        fprintf(logfile, "dmfc0 %s (reg %d sel %d)\n",
                rn, reg, sel);
    }
#endif
    generate_exception(ctx, EXCP_RI);
}

3672
static void gen_dmtc0 (CPUState *env, DisasContext *ctx, int reg, int sel)
T
ths 已提交
3673 3674 3675
{
    const char *rn = "invalid";

3676 3677 3678
    if (sel != 0)
        check_insn(env, ctx, ISA_MIPS64);

T
ths 已提交
3679 3680 3681 3682 3683 3684 3685 3686
    switch (reg) {
    case 0:
        switch (sel) {
        case 0:
            gen_op_mtc0_index();
            rn = "Index";
            break;
        case 1:
3687
            check_insn(env, ctx, ASE_MT);
3688
            gen_op_mtc0_mvpcontrol();
T
ths 已提交
3689
            rn = "MVPControl";
3690
            break;
T
ths 已提交
3691
        case 2:
3692
            check_insn(env, ctx, ASE_MT);
3693
            /* ignored */
T
ths 已提交
3694
            rn = "MVPConf0";
3695
            break;
T
ths 已提交
3696
        case 3:
3697
            check_insn(env, ctx, ASE_MT);
3698
            /* ignored */
T
ths 已提交
3699
            rn = "MVPConf1";
3700
            break;
T
ths 已提交
3701 3702 3703 3704 3705 3706 3707
        default:
            goto die;
        }
        break;
    case 1:
        switch (sel) {
        case 0:
T
ths 已提交
3708
            /* ignored */
T
ths 已提交
3709
            rn = "Random";
T
ths 已提交
3710
            break;
T
ths 已提交
3711
        case 1:
3712
            check_insn(env, ctx, ASE_MT);
3713
            gen_op_mtc0_vpecontrol();
T
ths 已提交
3714
            rn = "VPEControl";
3715
            break;
T
ths 已提交
3716
        case 2:
3717
            check_insn(env, ctx, ASE_MT);
3718
            gen_op_mtc0_vpeconf0();
T
ths 已提交
3719
            rn = "VPEConf0";
3720
            break;
T
ths 已提交
3721
        case 3:
3722
            check_insn(env, ctx, ASE_MT);
3723
            gen_op_mtc0_vpeconf1();
T
ths 已提交
3724
            rn = "VPEConf1";
3725
            break;
T
ths 已提交
3726
        case 4:
3727
            check_insn(env, ctx, ASE_MT);
3728
            gen_op_mtc0_yqmask();
T
ths 已提交
3729
            rn = "YQMask";
3730
            break;
T
ths 已提交
3731
        case 5:
3732
            check_insn(env, ctx, ASE_MT);
3733
            gen_op_mtc0_vpeschedule();
T
ths 已提交
3734
            rn = "VPESchedule";
3735
            break;
T
ths 已提交
3736
        case 6:
3737
            check_insn(env, ctx, ASE_MT);
3738
            gen_op_mtc0_vpeschefback();
T
ths 已提交
3739
            rn = "VPEScheFBack";
3740
            break;
T
ths 已提交
3741
        case 7:
3742
            check_insn(env, ctx, ASE_MT);
3743
            gen_op_mtc0_vpeopt();
T
ths 已提交
3744
            rn = "VPEOpt";
3745
            break;
T
ths 已提交
3746 3747 3748 3749 3750 3751 3752
        default:
            goto die;
        }
        break;
    case 2:
        switch (sel) {
        case 0:
3753
            gen_op_mtc0_entrylo0();
T
ths 已提交
3754 3755
            rn = "EntryLo0";
            break;
T
ths 已提交
3756
        case 1:
3757
            check_insn(env, ctx, ASE_MT);
3758
            gen_op_mtc0_tcstatus();
T
ths 已提交
3759
            rn = "TCStatus";
3760
            break;
T
ths 已提交
3761
        case 2:
3762
            check_insn(env, ctx, ASE_MT);
3763
            gen_op_mtc0_tcbind();
T
ths 已提交
3764
            rn = "TCBind";
3765
            break;
T
ths 已提交
3766
        case 3:
3767
            check_insn(env, ctx, ASE_MT);
3768
            gen_op_mtc0_tcrestart();
T
ths 已提交
3769
            rn = "TCRestart";
3770
            break;
T
ths 已提交
3771
        case 4:
3772
            check_insn(env, ctx, ASE_MT);
3773
            gen_op_mtc0_tchalt();
T
ths 已提交
3774
            rn = "TCHalt";
3775
            break;
T
ths 已提交
3776
        case 5:
3777
            check_insn(env, ctx, ASE_MT);
3778
            gen_op_mtc0_tccontext();
T
ths 已提交
3779
            rn = "TCContext";
3780
            break;
T
ths 已提交
3781
        case 6:
3782
            check_insn(env, ctx, ASE_MT);
3783
            gen_op_mtc0_tcschedule();
T
ths 已提交
3784
            rn = "TCSchedule";
3785
            break;
T
ths 已提交
3786
        case 7:
3787
            check_insn(env, ctx, ASE_MT);
3788
            gen_op_mtc0_tcschefback();
T
ths 已提交
3789
            rn = "TCScheFBack";
3790
            break;
T
ths 已提交
3791 3792 3793 3794 3795 3796 3797
        default:
            goto die;
        }
        break;
    case 3:
        switch (sel) {
        case 0:
3798
            gen_op_mtc0_entrylo1();
T
ths 已提交
3799 3800
            rn = "EntryLo1";
            break;
T
ths 已提交
3801 3802
        default:
            goto die;
T
ths 已提交
3803
        }
T
ths 已提交
3804 3805 3806 3807
        break;
    case 4:
        switch (sel) {
        case 0:
3808
            gen_op_mtc0_context();
T
ths 已提交
3809 3810
            rn = "Context";
            break;
T
ths 已提交
3811
        case 1:
3812
//           gen_op_mtc0_contextconfig(); /* SmartMIPS ASE */
T
ths 已提交
3813 3814
            rn = "ContextConfig";
//           break;
T
ths 已提交
3815 3816
        default:
            goto die;
T
ths 已提交
3817
        }
T
ths 已提交
3818 3819 3820 3821
        break;
    case 5:
        switch (sel) {
        case 0:
T
ths 已提交
3822 3823 3824
            gen_op_mtc0_pagemask();
            rn = "PageMask";
            break;
T
ths 已提交
3825
        case 1:
3826
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
3827 3828 3829
            gen_op_mtc0_pagegrain();
            rn = "PageGrain";
            break;
T
ths 已提交
3830 3831
        default:
            goto die;
T
ths 已提交
3832
        }
T
ths 已提交
3833 3834 3835 3836
        break;
    case 6:
        switch (sel) {
        case 0:
T
ths 已提交
3837 3838 3839
            gen_op_mtc0_wired();
            rn = "Wired";
            break;
T
ths 已提交
3840
        case 1:
3841
            check_insn(env, ctx, ISA_MIPS32R2);
3842
            gen_op_mtc0_srsconf0();
T
ths 已提交
3843
            rn = "SRSConf0";
3844
            break;
T
ths 已提交
3845
        case 2:
3846
            check_insn(env, ctx, ISA_MIPS32R2);
3847
            gen_op_mtc0_srsconf1();
T
ths 已提交
3848
            rn = "SRSConf1";
3849
            break;
T
ths 已提交
3850
        case 3:
3851
            check_insn(env, ctx, ISA_MIPS32R2);
3852
            gen_op_mtc0_srsconf2();
T
ths 已提交
3853
            rn = "SRSConf2";
3854
            break;
T
ths 已提交
3855
        case 4:
3856
            check_insn(env, ctx, ISA_MIPS32R2);
3857
            gen_op_mtc0_srsconf3();
T
ths 已提交
3858
            rn = "SRSConf3";
3859
            break;
T
ths 已提交
3860
        case 5:
3861
            check_insn(env, ctx, ISA_MIPS32R2);
3862
            gen_op_mtc0_srsconf4();
T
ths 已提交
3863
            rn = "SRSConf4";
3864
            break;
T
ths 已提交
3865 3866
        default:
            goto die;
T
ths 已提交
3867
        }
T
ths 已提交
3868 3869 3870 3871
        break;
    case 7:
        switch (sel) {
        case 0:
3872
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
3873 3874 3875
            gen_op_mtc0_hwrena();
            rn = "HWREna";
            break;
T
ths 已提交
3876 3877
        default:
            goto die;
T
ths 已提交
3878
        }
T
ths 已提交
3879 3880 3881 3882 3883 3884 3885 3886
        break;
    case 8:
        /* ignored */
        rn = "BadVaddr";
        break;
    case 9:
        switch (sel) {
        case 0:
T
ths 已提交
3887 3888 3889
            gen_op_mtc0_count();
            rn = "Count";
            break;
T
ths 已提交
3890
        /* 6,7 are implementation dependent */
T
ths 已提交
3891 3892
        default:
            goto die;
T
ths 已提交
3893 3894 3895
        }
        /* Stop translation as we may have switched the execution mode */
        ctx->bstate = BS_STOP;
T
ths 已提交
3896 3897 3898 3899
        break;
    case 10:
        switch (sel) {
        case 0:
T
ths 已提交
3900 3901 3902
            gen_op_mtc0_entryhi();
            rn = "EntryHi";
            break;
T
ths 已提交
3903 3904
        default:
            goto die;
T
ths 已提交
3905
        }
T
ths 已提交
3906 3907 3908 3909
        break;
    case 11:
        switch (sel) {
        case 0:
T
ths 已提交
3910 3911 3912
            gen_op_mtc0_compare();
            rn = "Compare";
            break;
T
ths 已提交
3913
        /* 6,7 are implementation dependent */
T
ths 已提交
3914 3915
        default:
            goto die;
T
ths 已提交
3916 3917 3918
        }
        /* Stop translation as we may have switched the execution mode */
        ctx->bstate = BS_STOP;
T
ths 已提交
3919 3920 3921 3922
        break;
    case 12:
        switch (sel) {
        case 0:
T
ths 已提交
3923
            gen_op_mtc0_status();
3924 3925 3926
            /* BS_STOP isn't good enough here, hflags may have changed. */
            gen_save_pc(ctx->pc + 4);
            ctx->bstate = BS_EXCP;
T
ths 已提交
3927 3928
            rn = "Status";
            break;
T
ths 已提交
3929
        case 1:
3930
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
3931
            gen_op_mtc0_intctl();
3932 3933
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
3934 3935
            rn = "IntCtl";
            break;
T
ths 已提交
3936
        case 2:
3937
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
3938
            gen_op_mtc0_srsctl();
3939 3940
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
3941 3942
            rn = "SRSCtl";
            break;
T
ths 已提交
3943
        case 3:
3944
            check_insn(env, ctx, ISA_MIPS32R2);
3945
            gen_op_mtc0_srsmap();
3946 3947
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
3948 3949 3950
            rn = "SRSMap";
            break;
        default:
T
ths 已提交
3951
            goto die;
T
ths 已提交
3952
        }
T
ths 已提交
3953 3954 3955 3956
        break;
    case 13:
        switch (sel) {
        case 0:
T
ths 已提交
3957 3958 3959
            gen_op_mtc0_cause();
            rn = "Cause";
            break;
T
ths 已提交
3960 3961
        default:
            goto die;
T
ths 已提交
3962 3963 3964
        }
        /* Stop translation as we may have switched the execution mode */
        ctx->bstate = BS_STOP;
T
ths 已提交
3965 3966 3967 3968
        break;
    case 14:
        switch (sel) {
        case 0:
3969
            gen_op_mtc0_epc();
T
ths 已提交
3970 3971
            rn = "EPC";
            break;
T
ths 已提交
3972 3973
        default:
            goto die;
T
ths 已提交
3974
        }
T
ths 已提交
3975 3976 3977 3978
        break;
    case 15:
        switch (sel) {
        case 0:
T
ths 已提交
3979 3980 3981
            /* ignored */
            rn = "PRid";
            break;
T
ths 已提交
3982
        case 1:
3983
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
3984 3985 3986
            gen_op_mtc0_ebase();
            rn = "EBase";
            break;
T
ths 已提交
3987 3988
        default:
            goto die;
T
ths 已提交
3989
        }
T
ths 已提交
3990 3991 3992 3993 3994 3995
        break;
    case 16:
        switch (sel) {
        case 0:
            gen_op_mtc0_config0();
            rn = "Config";
T
ths 已提交
3996 3997
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
3998 3999
            break;
        case 1:
T
ths 已提交
4000
            /* ignored */
T
ths 已提交
4001 4002 4003 4004 4005
            rn = "Config1";
            break;
        case 2:
            gen_op_mtc0_config2();
            rn = "Config2";
T
ths 已提交
4006 4007
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
4008 4009
            break;
        case 3:
T
ths 已提交
4010
            /* ignored */
T
ths 已提交
4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021
            rn = "Config3";
            break;
        /* 6,7 are implementation dependent */
        default:
            rn = "Invalid config selector";
            goto die;
        }
        break;
    case 17:
        switch (sel) {
        case 0:
T
ths 已提交
4022 4023 4024
            /* ignored */
            rn = "LLAddr";
            break;
T
ths 已提交
4025 4026 4027 4028 4029 4030
        default:
            goto die;
        }
        break;
    case 18:
        switch (sel) {
4031 4032
        case 0 ... 7:
            gen_op_mtc0_watchlo(sel);
T
ths 已提交
4033 4034
            rn = "WatchLo";
            break;
T
ths 已提交
4035 4036 4037 4038 4039 4040
        default:
            goto die;
        }
        break;
    case 19:
        switch (sel) {
4041 4042
        case 0 ... 7:
            gen_op_mtc0_watchhi(sel);
T
ths 已提交
4043 4044
            rn = "WatchHi";
            break;
T
ths 已提交
4045 4046 4047 4048 4049 4050 4051
        default:
            goto die;
        }
        break;
    case 20:
        switch (sel) {
        case 0:
4052
            check_insn(env, ctx, ISA_MIPS3);
4053
            gen_op_mtc0_xcontext();
T
ths 已提交
4054 4055
            rn = "XContext";
            break;
T
ths 已提交
4056 4057 4058 4059 4060 4061 4062 4063
        default:
            goto die;
        }
        break;
    case 21:
       /* Officially reserved, but sel 0 is used for R1x000 framemask */
        switch (sel) {
        case 0:
T
ths 已提交
4064 4065 4066
            gen_op_mtc0_framemask();
            rn = "Framemask";
            break;
T
ths 已提交
4067 4068 4069 4070 4071 4072 4073
        default:
            goto die;
        }
        break;
    case 22:
        /* ignored */
        rn = "Diagnostic"; /* implementation dependent */
T
ths 已提交
4074
        break;
T
ths 已提交
4075 4076 4077
    case 23:
        switch (sel) {
        case 0:
T
ths 已提交
4078
            gen_op_mtc0_debug(); /* EJTAG support */
4079 4080 4081
            /* BS_STOP isn't good enough here, hflags may have changed. */
            gen_save_pc(ctx->pc + 4);
            ctx->bstate = BS_EXCP;
T
ths 已提交
4082 4083
            rn = "Debug";
            break;
T
ths 已提交
4084
        case 1:
4085
//            gen_op_mtc0_tracecontrol(); /* PDtrace support */
4086 4087
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
4088 4089
            rn = "TraceControl";
//            break;
T
ths 已提交
4090
        case 2:
4091
//            gen_op_mtc0_tracecontrol2(); /* PDtrace support */
4092 4093
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
4094 4095
            rn = "TraceControl2";
//            break;
T
ths 已提交
4096
        case 3:
4097
//            gen_op_mtc0_usertracedata(); /* PDtrace support */
4098 4099
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
4100 4101
            rn = "UserTraceData";
//            break;
T
ths 已提交
4102
        case 4:
4103
//            gen_op_mtc0_debug(); /* PDtrace support */
4104 4105
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
4106 4107
            rn = "TraceBPC";
//            break;
T
ths 已提交
4108 4109 4110 4111 4112 4113 4114
        default:
            goto die;
        }
        break;
    case 24:
        switch (sel) {
        case 0:
4115
            gen_op_mtc0_depc(); /* EJTAG support */
T
ths 已提交
4116 4117
            rn = "DEPC";
            break;
T
ths 已提交
4118 4119 4120 4121 4122 4123 4124
        default:
            goto die;
        }
        break;
    case 25:
        switch (sel) {
        case 0:
T
ths 已提交
4125 4126 4127
            gen_op_mtc0_performance0();
            rn = "Performance0";
            break;
T
ths 已提交
4128
        case 1:
4129
//            gen_op_mtc0_performance1();
T
ths 已提交
4130 4131
            rn = "Performance1";
//            break;
T
ths 已提交
4132
        case 2:
4133
//            gen_op_mtc0_performance2();
T
ths 已提交
4134 4135
            rn = "Performance2";
//            break;
T
ths 已提交
4136
        case 3:
4137
//            gen_op_mtc0_performance3();
T
ths 已提交
4138 4139
            rn = "Performance3";
//            break;
T
ths 已提交
4140
        case 4:
4141
//            gen_op_mtc0_performance4();
T
ths 已提交
4142 4143
            rn = "Performance4";
//            break;
T
ths 已提交
4144
        case 5:
4145
//            gen_op_mtc0_performance5();
T
ths 已提交
4146 4147
            rn = "Performance5";
//            break;
T
ths 已提交
4148
        case 6:
4149
//            gen_op_mtc0_performance6();
T
ths 已提交
4150 4151
            rn = "Performance6";
//            break;
T
ths 已提交
4152
        case 7:
4153
//            gen_op_mtc0_performance7();
T
ths 已提交
4154 4155
            rn = "Performance7";
//            break;
T
ths 已提交
4156 4157 4158
        default:
            goto die;
        }
T
ths 已提交
4159
        break;
T
ths 已提交
4160
    case 26:
T
ths 已提交
4161
        /* ignored */
T
ths 已提交
4162
        rn = "ECC";
T
ths 已提交
4163
        break;
T
ths 已提交
4164 4165 4166
    case 27:
        switch (sel) {
        case 0 ... 3:
T
ths 已提交
4167 4168 4169
            /* ignored */
            rn = "CacheErr";
            break;
T
ths 已提交
4170 4171 4172
        default:
            goto die;
        }
T
ths 已提交
4173
        break;
T
ths 已提交
4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186
    case 28:
        switch (sel) {
        case 0:
        case 2:
        case 4:
        case 6:
            gen_op_mtc0_taglo();
            rn = "TagLo";
            break;
        case 1:
        case 3:
        case 5:
        case 7:
T
ths 已提交
4187
            gen_op_mtc0_datalo();
T
ths 已提交
4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206
            rn = "DataLo";
            break;
        default:
            goto die;
        }
        break;
    case 29:
        switch (sel) {
        case 0:
        case 2:
        case 4:
        case 6:
            gen_op_mtc0_taghi();
            rn = "TagHi";
            break;
        case 1:
        case 3:
        case 5:
        case 7:
T
ths 已提交
4207
            gen_op_mtc0_datahi();
T
ths 已提交
4208 4209 4210 4211 4212 4213
            rn = "DataHi";
            break;
        default:
            rn = "invalid sel";
            goto die;
        }
T
ths 已提交
4214
        break;
T
ths 已提交
4215 4216 4217
    case 30:
        switch (sel) {
        case 0:
4218
            gen_op_mtc0_errorepc();
T
ths 已提交
4219 4220
            rn = "ErrorEPC";
            break;
T
ths 已提交
4221 4222 4223 4224 4225 4226 4227
        default:
            goto die;
        }
        break;
    case 31:
        switch (sel) {
        case 0:
T
ths 已提交
4228 4229 4230
            gen_op_mtc0_desave(); /* EJTAG support */
            rn = "DESAVE";
            break;
T
ths 已提交
4231 4232 4233
        default:
            goto die;
        }
T
ths 已提交
4234 4235
        /* Stop translation as we may have switched the execution mode */
        ctx->bstate = BS_STOP;
T
ths 已提交
4236 4237
        break;
    default:
T
ths 已提交
4238
        goto die;
T
ths 已提交
4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256
    }
#if defined MIPS_DEBUG_DISAS
    if (loglevel & CPU_LOG_TB_IN_ASM) {
        fprintf(logfile, "dmtc0 %s (reg %d sel %d)\n",
                rn, reg, sel);
    }
#endif
    return;

die:
#if defined MIPS_DEBUG_DISAS
    if (loglevel & CPU_LOG_TB_IN_ASM) {
        fprintf(logfile, "dmtc0 %s (reg %d sel %d)\n",
                rn, reg, sel);
    }
#endif
    generate_exception(ctx, EXCP_RI);
}
4257
#endif /* TARGET_MIPS64 */
T
ths 已提交
4258

4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586
static void gen_mftr(CPUState *env, DisasContext *ctx, int rt,
                     int u, int sel, int h)
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

    if ((env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP)) == 0 &&
        ((env->CP0_TCBind[other_tc] & (0xf << CP0TCBd_CurVPE)) !=
         (env->CP0_TCBind[env->current_tc] & (0xf << CP0TCBd_CurVPE))))
        gen_op_set_T0(-1);
    else if ((env->CP0_VPEControl & (0xff << CP0VPECo_TargTC)) >
             (env->mvp->CP0_MVPConf0 & (0xff << CP0MVPC0_PTC)))
        gen_op_set_T0(-1);
    else if (u == 0) {
        switch (rt) {
        case 2:
            switch (sel) {
            case 1:
                gen_op_mftc0_tcstatus();
                break;
            case 2:
                gen_op_mftc0_tcbind();
                break;
            case 3:
                gen_op_mftc0_tcrestart();
                break;
            case 4:
                gen_op_mftc0_tchalt();
                break;
            case 5:
                gen_op_mftc0_tccontext();
                break;
            case 6:
                gen_op_mftc0_tcschedule();
                break;
            case 7:
                gen_op_mftc0_tcschefback();
                break;
            default:
                gen_mfc0(env, ctx, rt, sel);
                break;
            }
            break;
        case 10:
            switch (sel) {
            case 0:
                gen_op_mftc0_entryhi();
                break;
            default:
                gen_mfc0(env, ctx, rt, sel);
                break;
            }
        case 12:
            switch (sel) {
            case 0:
                gen_op_mftc0_status();
                break;
            default:
                gen_mfc0(env, ctx, rt, sel);
                break;
            }
        case 23:
            switch (sel) {
            case 0:
                gen_op_mftc0_debug();
                break;
            default:
                gen_mfc0(env, ctx, rt, sel);
                break;
            }
            break;
        default:
            gen_mfc0(env, ctx, rt, sel);
        }
    } else switch (sel) {
    /* GPR registers. */
    case 0:
        gen_op_mftgpr(rt);
        break;
    /* Auxiliary CPU registers */
    case 1:
        switch (rt) {
        case 0:
            gen_op_mftlo(0);
            break;
        case 1:
            gen_op_mfthi(0);
            break;
        case 2:
            gen_op_mftacx(0);
            break;
        case 4:
            gen_op_mftlo(1);
            break;
        case 5:
            gen_op_mfthi(1);
            break;
        case 6:
            gen_op_mftacx(1);
            break;
        case 8:
            gen_op_mftlo(2);
            break;
        case 9:
            gen_op_mfthi(2);
            break;
        case 10:
            gen_op_mftacx(2);
            break;
        case 12:
            gen_op_mftlo(3);
            break;
        case 13:
            gen_op_mfthi(3);
            break;
        case 14:
            gen_op_mftacx(3);
            break;
        case 16:
            gen_op_mftdsp();
            break;
        default:
            goto die;
        }
        break;
    /* Floating point (COP1). */
    case 2:
        /* XXX: For now we support only a single FPU context. */
        if (h == 0) {
            GEN_LOAD_FREG_FTN(WT0, rt);
            gen_op_mfc1();
        } else {
            GEN_LOAD_FREG_FTN(WTH0, rt);
            gen_op_mfhc1();
        }
        break;
    case 3:
        /* XXX: For now we support only a single FPU context. */
        gen_op_cfc1(rt);
        break;
    /* COP2: Not implemented. */
    case 4:
    case 5:
        /* fall through */
    default:
        goto die;
    }
#if defined MIPS_DEBUG_DISAS
    if (loglevel & CPU_LOG_TB_IN_ASM) {
        fprintf(logfile, "mftr (reg %d u %d sel %d h %d)\n",
                rt, u, sel, h);
    }
#endif
    return;

die:
#if defined MIPS_DEBUG_DISAS
    if (loglevel & CPU_LOG_TB_IN_ASM) {
        fprintf(logfile, "mftr (reg %d u %d sel %d h %d)\n",
                rt, u, sel, h);
    }
#endif
    generate_exception(ctx, EXCP_RI);
}

static void gen_mttr(CPUState *env, DisasContext *ctx, int rd,
                     int u, int sel, int h)
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

    if ((env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP)) == 0 &&
        ((env->CP0_TCBind[other_tc] & (0xf << CP0TCBd_CurVPE)) !=
         (env->CP0_TCBind[env->current_tc] & (0xf << CP0TCBd_CurVPE))))
        /* NOP */ ;
    else if ((env->CP0_VPEControl & (0xff << CP0VPECo_TargTC)) >
             (env->mvp->CP0_MVPConf0 & (0xff << CP0MVPC0_PTC)))
        /* NOP */ ;
    else if (u == 0) {
        switch (rd) {
        case 2:
            switch (sel) {
            case 1:
                gen_op_mttc0_tcstatus();
                break;
            case 2:
                gen_op_mttc0_tcbind();
                break;
            case 3:
                gen_op_mttc0_tcrestart();
                break;
            case 4:
                gen_op_mttc0_tchalt();
                break;
            case 5:
                gen_op_mttc0_tccontext();
                break;
            case 6:
                gen_op_mttc0_tcschedule();
                break;
            case 7:
                gen_op_mttc0_tcschefback();
                break;
            default:
                gen_mtc0(env, ctx, rd, sel);
                break;
            }
            break;
        case 10:
            switch (sel) {
            case 0:
                gen_op_mttc0_entryhi();
                break;
            default:
                gen_mtc0(env, ctx, rd, sel);
                break;
            }
        case 12:
            switch (sel) {
            case 0:
                gen_op_mttc0_status();
                break;
            default:
                gen_mtc0(env, ctx, rd, sel);
                break;
            }
        case 23:
            switch (sel) {
            case 0:
                gen_op_mttc0_debug();
                break;
            default:
                gen_mtc0(env, ctx, rd, sel);
                break;
            }
            break;
        default:
            gen_mtc0(env, ctx, rd, sel);
        }
    } else switch (sel) {
    /* GPR registers. */
    case 0:
        gen_op_mttgpr(rd);
        break;
    /* Auxiliary CPU registers */
    case 1:
        switch (rd) {
        case 0:
            gen_op_mttlo(0);
            break;
        case 1:
            gen_op_mtthi(0);
            break;
        case 2:
            gen_op_mttacx(0);
            break;
        case 4:
            gen_op_mttlo(1);
            break;
        case 5:
            gen_op_mtthi(1);
            break;
        case 6:
            gen_op_mttacx(1);
            break;
        case 8:
            gen_op_mttlo(2);
            break;
        case 9:
            gen_op_mtthi(2);
            break;
        case 10:
            gen_op_mttacx(2);
            break;
        case 12:
            gen_op_mttlo(3);
            break;
        case 13:
            gen_op_mtthi(3);
            break;
        case 14:
            gen_op_mttacx(3);
            break;
        case 16:
            gen_op_mttdsp();
            break;
        default:
            goto die;
        }
        break;
    /* Floating point (COP1). */
    case 2:
        /* XXX: For now we support only a single FPU context. */
        if (h == 0) {
            gen_op_mtc1();
            GEN_STORE_FTN_FREG(rd, WT0);
        } else {
            gen_op_mthc1();
            GEN_STORE_FTN_FREG(rd, WTH0);
        }
        break;
    case 3:
        /* XXX: For now we support only a single FPU context. */
        gen_op_ctc1(rd);
        break;
    /* COP2: Not implemented. */
    case 4:
    case 5:
        /* fall through */
    default:
        goto die;
    }
#if defined MIPS_DEBUG_DISAS
    if (loglevel & CPU_LOG_TB_IN_ASM) {
        fprintf(logfile, "mttr (reg %d u %d sel %d h %d)\n",
                rd, u, sel, h);
    }
#endif
    return;

die:
#if defined MIPS_DEBUG_DISAS
    if (loglevel & CPU_LOG_TB_IN_ASM) {
        fprintf(logfile, "mttr (reg %d u %d sel %d h %d)\n",
                rd, u, sel, h);
    }
#endif
    generate_exception(ctx, EXCP_RI);
}

4587
static void gen_cp0 (CPUState *env, DisasContext *ctx, uint32_t opc, int rt, int rd)
B
bellard 已提交
4588
{
T
ths 已提交
4589
    const char *opn = "ldst";
B
bellard 已提交
4590 4591 4592 4593

    switch (opc) {
    case OPC_MFC0:
        if (rt == 0) {
4594
            /* Treat as NOP. */
B
bellard 已提交
4595 4596
            return;
        }
4597
        gen_mfc0(env, ctx, rd, ctx->opcode & 0x7);
B
bellard 已提交
4598 4599 4600 4601 4602
        gen_op_store_T0_gpr(rt);
        opn = "mfc0";
        break;
    case OPC_MTC0:
        GEN_LOAD_REG_TN(T0, rt);
4603
        save_cpu_state(ctx, 1);
4604
        gen_mtc0(env, ctx, rd, ctx->opcode & 0x7);
B
bellard 已提交
4605 4606
        opn = "mtc0";
        break;
4607
#if defined(TARGET_MIPS64)
T
ths 已提交
4608
    case OPC_DMFC0:
4609
        check_insn(env, ctx, ISA_MIPS3);
T
ths 已提交
4610
        if (rt == 0) {
4611
            /* Treat as NOP. */
T
ths 已提交
4612 4613
            return;
        }
4614
        gen_dmfc0(env, ctx, rd, ctx->opcode & 0x7);
T
ths 已提交
4615 4616 4617 4618
        gen_op_store_T0_gpr(rt);
        opn = "dmfc0";
        break;
    case OPC_DMTC0:
4619
        check_insn(env, ctx, ISA_MIPS3);
T
ths 已提交
4620
        GEN_LOAD_REG_TN(T0, rt);
4621
        save_cpu_state(ctx, 1);
4622
        gen_dmtc0(env, ctx, rd, ctx->opcode & 0x7);
T
ths 已提交
4623 4624
        opn = "dmtc0";
        break;
4625
#endif
4626
    case OPC_MFTR:
4627
        check_insn(env, ctx, ASE_MT);
4628 4629 4630 4631 4632 4633 4634 4635 4636 4637
        if (rd == 0) {
            /* Treat as NOP. */
            return;
        }
        gen_mftr(env, ctx, rt, (ctx->opcode >> 5) & 1,
                 ctx->opcode & 0x7, (ctx->opcode >> 4) & 1);
        gen_op_store_T0_gpr(rd);
        opn = "mftr";
        break;
    case OPC_MTTR:
4638
        check_insn(env, ctx, ASE_MT);
4639 4640 4641 4642 4643
        GEN_LOAD_REG_TN(T0, rt);
        gen_mttr(env, ctx, rd, (ctx->opcode >> 5) & 1,
                 ctx->opcode & 0x7, (ctx->opcode >> 4) & 1);
        opn = "mttr";
        break;
B
bellard 已提交
4644 4645
    case OPC_TLBWI:
        opn = "tlbwi";
4646
        if (!env->tlb->do_tlbwi)
4647 4648
            goto die;
        gen_op_tlbwi();
B
bellard 已提交
4649 4650 4651
        break;
    case OPC_TLBWR:
        opn = "tlbwr";
4652
        if (!env->tlb->do_tlbwr)
4653 4654
            goto die;
        gen_op_tlbwr();
B
bellard 已提交
4655 4656 4657
        break;
    case OPC_TLBP:
        opn = "tlbp";
4658
        if (!env->tlb->do_tlbp)
4659 4660
            goto die;
        gen_op_tlbp();
B
bellard 已提交
4661 4662 4663
        break;
    case OPC_TLBR:
        opn = "tlbr";
4664
        if (!env->tlb->do_tlbr)
4665 4666
            goto die;
        gen_op_tlbr();
B
bellard 已提交
4667 4668 4669
        break;
    case OPC_ERET:
        opn = "eret";
4670
        check_insn(env, ctx, ISA_MIPS2);
4671
        save_cpu_state(ctx, 1);
B
bellard 已提交
4672 4673 4674 4675 4676
        gen_op_eret();
        ctx->bstate = BS_EXCP;
        break;
    case OPC_DERET:
        opn = "deret";
4677
        check_insn(env, ctx, ISA_MIPS32);
B
bellard 已提交
4678
        if (!(ctx->hflags & MIPS_HFLAG_DM)) {
4679
            MIPS_INVAL(opn);
B
bellard 已提交
4680 4681
            generate_exception(ctx, EXCP_RI);
        } else {
4682
            save_cpu_state(ctx, 1);
B
bellard 已提交
4683 4684 4685 4686
            gen_op_deret();
            ctx->bstate = BS_EXCP;
        }
        break;
B
bellard 已提交
4687 4688
    case OPC_WAIT:
        opn = "wait";
4689
        check_insn(env, ctx, ISA_MIPS3 | ISA_MIPS32);
B
bellard 已提交
4690 4691 4692 4693 4694 4695 4696
        /* If we get an exception, we want to restart at next instruction */
        ctx->pc += 4;
        save_cpu_state(ctx, 1);
        ctx->pc -= 4;
        gen_op_wait();
        ctx->bstate = BS_EXCP;
        break;
B
bellard 已提交
4697
    default:
4698
 die:
4699
        MIPS_INVAL(opn);
B
bellard 已提交
4700 4701 4702 4703 4704 4705
        generate_exception(ctx, EXCP_RI);
        return;
    }
    MIPS_DEBUG("%s %s %d", opn, regnames[rt], rd);
}

B
bellard 已提交
4706
/* CP1 Branches (before delay slot) */
4707
static void gen_compute_branch1 (CPUState *env, DisasContext *ctx, uint32_t op,
4708
                                 int32_t cc, int32_t offset)
B
bellard 已提交
4709 4710
{
    target_ulong btarget;
4711
    const char *opn = "cp1 cond branch";
B
bellard 已提交
4712

4713 4714 4715
    if (cc != 0)
        check_insn(env, ctx, ISA_MIPS4 | ISA_MIPS32);

B
bellard 已提交
4716 4717
    btarget = ctx->pc + 4 + offset;

4718 4719
    switch (op) {
    case OPC_BC1F:
4720
        gen_op_bc1f(cc);
4721
        opn = "bc1f";
B
bellard 已提交
4722
        goto not_likely;
4723
    case OPC_BC1FL:
4724
        gen_op_bc1f(cc);
4725
        opn = "bc1fl";
B
bellard 已提交
4726
        goto likely;
4727
    case OPC_BC1T:
4728
        gen_op_bc1t(cc);
4729
        opn = "bc1t";
4730
        goto not_likely;
4731
    case OPC_BC1TL:
4732
        gen_op_bc1t(cc);
4733
        opn = "bc1tl";
B
bellard 已提交
4734 4735
    likely:
        ctx->hflags |= MIPS_HFLAG_BL;
4736 4737
        gen_op_set_bcond();
        gen_op_save_bcond();
B
bellard 已提交
4738
        break;
4739
    case OPC_BC1FANY2:
4740 4741
        gen_op_bc1any2f(cc);
        opn = "bc1any2f";
4742 4743
        goto not_likely;
    case OPC_BC1TANY2:
4744 4745
        gen_op_bc1any2t(cc);
        opn = "bc1any2t";
4746 4747
        goto not_likely;
    case OPC_BC1FANY4:
4748 4749
        gen_op_bc1any4f(cc);
        opn = "bc1any4f";
4750 4751
        goto not_likely;
    case OPC_BC1TANY4:
4752 4753
        gen_op_bc1any4t(cc);
        opn = "bc1any4t";
4754 4755 4756 4757 4758
    not_likely:
        ctx->hflags |= MIPS_HFLAG_BC;
        gen_op_set_bcond();
        break;
    default:
4759
        MIPS_INVAL(opn);
4760
        generate_exception (ctx, EXCP_RI);
B
bellard 已提交
4761 4762
        return;
    }
4763
    MIPS_DEBUG("%s: cond %02x target " TARGET_FMT_lx, opn,
B
bellard 已提交
4764 4765 4766 4767
               ctx->hflags, btarget);
    ctx->btarget = btarget;
}

B
bellard 已提交
4768
/* Coprocessor 1 (FPU) */
4769 4770 4771

#define FOP(func, fmt) (((fmt) << 21) | (func))

4772
static void gen_cp1 (DisasContext *ctx, uint32_t opc, int rt, int fs)
B
bellard 已提交
4773
{
4774
    const char *opn = "cp1 move";
B
bellard 已提交
4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789

    switch (opc) {
    case OPC_MFC1:
        GEN_LOAD_FREG_FTN(WT0, fs);
        gen_op_mfc1();
        GEN_STORE_TN_REG(rt, T0);
        opn = "mfc1";
        break;
    case OPC_MTC1:
        GEN_LOAD_REG_TN(T0, rt);
        gen_op_mtc1();
        GEN_STORE_FTN_FREG(fs, WT0);
        opn = "mtc1";
        break;
    case OPC_CFC1:
4790
        gen_op_cfc1(fs);
B
bellard 已提交
4791 4792 4793 4794 4795
        GEN_STORE_TN_REG(rt, T0);
        opn = "cfc1";
        break;
    case OPC_CTC1:
        GEN_LOAD_REG_TN(T0, rt);
4796
        gen_op_ctc1(fs);
B
bellard 已提交
4797 4798
        opn = "ctc1";
        break;
T
ths 已提交
4799
    case OPC_DMFC1:
4800 4801 4802 4803 4804
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_dmfc1();
        GEN_STORE_TN_REG(rt, T0);
        opn = "dmfc1";
        break;
T
ths 已提交
4805
    case OPC_DMTC1:
4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822
        GEN_LOAD_REG_TN(T0, rt);
        gen_op_dmtc1();
        GEN_STORE_FTN_FREG(fs, DT0);
        opn = "dmtc1";
        break;
    case OPC_MFHC1:
        GEN_LOAD_FREG_FTN(WTH0, fs);
        gen_op_mfhc1();
        GEN_STORE_TN_REG(rt, T0);
        opn = "mfhc1";
        break;
    case OPC_MTHC1:
        GEN_LOAD_REG_TN(T0, rt);
        gen_op_mthc1();
        GEN_STORE_FTN_FREG(fs, WTH0);
        opn = "mthc1";
        break;
B
bellard 已提交
4823
    default:
4824
        MIPS_INVAL(opn);
4825
        generate_exception (ctx, EXCP_RI);
B
bellard 已提交
4826 4827 4828 4829 4830
        return;
    }
    MIPS_DEBUG("%s %s %s", opn, regnames[rt], fregnames[fs]);
}

4831 4832 4833
static void gen_movci (DisasContext *ctx, int rd, int rs, int cc, int tf)
{
    uint32_t ccbit;
B
bellard 已提交
4834

4835 4836
    GEN_LOAD_REG_TN(T0, rd);
    GEN_LOAD_REG_TN(T1, rs);
T
ths 已提交
4837
    if (cc) {
4838
        ccbit = 1 << (24 + cc);
T
ths 已提交
4839
    } else
4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852
        ccbit = 1 << 23;
    if (!tf)
        gen_op_movf(ccbit);
    else
        gen_op_movt(ccbit);
    GEN_STORE_TN_REG(rd, T0);
}

#define GEN_MOVCF(fmt)                                                \
static void glue(gen_movcf_, fmt) (DisasContext *ctx, int cc, int tf) \
{                                                                     \
    uint32_t ccbit;                                                   \
                                                                      \
T
ths 已提交
4853
    if (cc) {                                                         \
4854
        ccbit = 1 << (24 + cc);                                       \
T
ths 已提交
4855
    } else                                                            \
4856 4857 4858 4859 4860 4861 4862 4863 4864 4865
        ccbit = 1 << 23;                                              \
    if (!tf)                                                          \
        glue(gen_op_float_movf_, fmt)(ccbit);                         \
    else                                                              \
        glue(gen_op_float_movt_, fmt)(ccbit);                         \
}
GEN_MOVCF(d);
GEN_MOVCF(s);
GEN_MOVCF(ps);
#undef GEN_MOVCF
B
bellard 已提交
4866

4867 4868
static void gen_farith (DisasContext *ctx, uint32_t op1,
                        int ft, int fs, int fd, int cc)
B
bellard 已提交
4869
{
4870
    const char *opn = "farith";
B
bellard 已提交
4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888
    const char *condnames[] = {
            "c.f",
            "c.un",
            "c.eq",
            "c.ueq",
            "c.olt",
            "c.ult",
            "c.ole",
            "c.ule",
            "c.sf",
            "c.ngle",
            "c.seq",
            "c.ngl",
            "c.lt",
            "c.nge",
            "c.le",
            "c.ngt",
    };
4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907
    const char *condnames_abs[] = {
            "cabs.f",
            "cabs.un",
            "cabs.eq",
            "cabs.ueq",
            "cabs.olt",
            "cabs.ult",
            "cabs.ole",
            "cabs.ule",
            "cabs.sf",
            "cabs.ngle",
            "cabs.seq",
            "cabs.ngl",
            "cabs.lt",
            "cabs.nge",
            "cabs.le",
            "cabs.ngt",
    };
    enum { BINOP, CMPOP, OTHEROP } optype = OTHEROP;
4908 4909
    uint32_t func = ctx->opcode & 0x3f;

B
bellard 已提交
4910
    switch (ctx->opcode & FOP(0x3f, 0x1f)) {
4911 4912 4913 4914 4915 4916
    case FOP(0, 16):
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WT1, ft);
        gen_op_float_add_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "add.s";
4917
        optype = BINOP;
4918 4919 4920 4921 4922 4923 4924
        break;
    case FOP(1, 16):
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WT1, ft);
        gen_op_float_sub_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "sub.s";
4925
        optype = BINOP;
4926 4927 4928 4929 4930 4931 4932
        break;
    case FOP(2, 16):
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WT1, ft);
        gen_op_float_mul_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "mul.s";
4933
        optype = BINOP;
4934 4935 4936 4937 4938 4939 4940
        break;
    case FOP(3, 16):
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WT1, ft);
        gen_op_float_div_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "div.s";
4941
        optype = BINOP;
4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967
        break;
    case FOP(4, 16):
        GEN_LOAD_FREG_FTN(WT0, fs);
        gen_op_float_sqrt_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "sqrt.s";
        break;
    case FOP(5, 16):
        GEN_LOAD_FREG_FTN(WT0, fs);
        gen_op_float_abs_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "abs.s";
        break;
    case FOP(6, 16):
        GEN_LOAD_FREG_FTN(WT0, fs);
        gen_op_float_mov_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "mov.s";
        break;
    case FOP(7, 16):
        GEN_LOAD_FREG_FTN(WT0, fs);
        gen_op_float_chs_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "neg.s";
        break;
    case FOP(8, 16):
4968
        check_cp1_64bitmode(ctx);
4969 4970 4971 4972 4973 4974
        GEN_LOAD_FREG_FTN(WT0, fs);
        gen_op_float_roundl_s();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "round.l.s";
        break;
    case FOP(9, 16):
4975
        check_cp1_64bitmode(ctx);
4976 4977 4978 4979 4980 4981
        GEN_LOAD_FREG_FTN(WT0, fs);
        gen_op_float_truncl_s();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "trunc.l.s";
        break;
    case FOP(10, 16):
4982
        check_cp1_64bitmode(ctx);
4983 4984 4985 4986 4987 4988
        GEN_LOAD_FREG_FTN(WT0, fs);
        gen_op_float_ceill_s();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "ceil.l.s";
        break;
    case FOP(11, 16):
4989
        check_cp1_64bitmode(ctx);
4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042
        GEN_LOAD_FREG_FTN(WT0, fs);
        gen_op_float_floorl_s();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "floor.l.s";
        break;
    case FOP(12, 16):
        GEN_LOAD_FREG_FTN(WT0, fs);
        gen_op_float_roundw_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "round.w.s";
        break;
    case FOP(13, 16):
        GEN_LOAD_FREG_FTN(WT0, fs);
        gen_op_float_truncw_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "trunc.w.s";
        break;
    case FOP(14, 16):
        GEN_LOAD_FREG_FTN(WT0, fs);
        gen_op_float_ceilw_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "ceil.w.s";
        break;
    case FOP(15, 16):
        GEN_LOAD_FREG_FTN(WT0, fs);
        gen_op_float_floorw_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "floor.w.s";
        break;
    case FOP(17, 16):
        GEN_LOAD_REG_TN(T0, ft);
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WT2, fd);
        gen_movcf_s(ctx, (ft >> 2) & 0x7, ft & 0x1);
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "movcf.s";
        break;
    case FOP(18, 16):
        GEN_LOAD_REG_TN(T0, ft);
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WT2, fd);
        gen_op_float_movz_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "movz.s";
        break;
    case FOP(19, 16):
        GEN_LOAD_REG_TN(T0, ft);
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WT2, fd);
        gen_op_float_movn_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "movn.s";
        break;
T
ths 已提交
5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055
    case FOP(21, 16):
        GEN_LOAD_FREG_FTN(WT0, fs);
        gen_op_float_recip_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "recip.s";
        break;
    case FOP(22, 16):
        GEN_LOAD_FREG_FTN(WT0, fs);
        gen_op_float_rsqrt_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "rsqrt.s";
        break;
    case FOP(28, 16):
5056
        check_cp1_64bitmode(ctx);
T
ths 已提交
5057 5058 5059 5060 5061 5062 5063
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WT2, fd);
        gen_op_float_recip2_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "recip2.s";
        break;
    case FOP(29, 16):
5064
        check_cp1_64bitmode(ctx);
T
ths 已提交
5065 5066 5067 5068 5069 5070
        GEN_LOAD_FREG_FTN(WT0, fs);
        gen_op_float_recip1_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "recip1.s";
        break;
    case FOP(30, 16):
5071
        check_cp1_64bitmode(ctx);
T
ths 已提交
5072 5073 5074 5075 5076 5077
        GEN_LOAD_FREG_FTN(WT0, fs);
        gen_op_float_rsqrt1_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "rsqrt1.s";
        break;
    case FOP(31, 16):
5078
        check_cp1_64bitmode(ctx);
T
ths 已提交
5079
        GEN_LOAD_FREG_FTN(WT0, fs);
T
ths 已提交
5080
        GEN_LOAD_FREG_FTN(WT2, ft);
T
ths 已提交
5081 5082 5083 5084
        gen_op_float_rsqrt2_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "rsqrt2.s";
        break;
5085
    case FOP(33, 16):
5086
        check_cp1_registers(ctx, fd);
5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098
        GEN_LOAD_FREG_FTN(WT0, fs);
        gen_op_float_cvtd_s();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "cvt.d.s";
        break;
    case FOP(36, 16):
        GEN_LOAD_FREG_FTN(WT0, fs);
        gen_op_float_cvtw_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "cvt.w.s";
        break;
    case FOP(37, 16):
5099
        check_cp1_64bitmode(ctx);
5100 5101 5102 5103 5104 5105
        GEN_LOAD_FREG_FTN(WT0, fs);
        gen_op_float_cvtl_s();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "cvt.l.s";
        break;
    case FOP(38, 16):
5106
        check_cp1_64bitmode(ctx);
5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130
        GEN_LOAD_FREG_FTN(WT1, fs);
        GEN_LOAD_FREG_FTN(WT0, ft);
        gen_op_float_cvtps_s();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "cvt.ps.s";
        break;
    case FOP(48, 16):
    case FOP(49, 16):
    case FOP(50, 16):
    case FOP(51, 16):
    case FOP(52, 16):
    case FOP(53, 16):
    case FOP(54, 16):
    case FOP(55, 16):
    case FOP(56, 16):
    case FOP(57, 16):
    case FOP(58, 16):
    case FOP(59, 16):
    case FOP(60, 16):
    case FOP(61, 16):
    case FOP(62, 16):
    case FOP(63, 16):
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WT1, ft);
5131
        if (ctx->opcode & (1 << 6)) {
5132
            check_cp1_64bitmode(ctx);
5133 5134 5135 5136 5137 5138
            gen_cmpabs_s(func-48, cc);
            opn = condnames_abs[func-48];
        } else {
            gen_cmp_s(func-48, cc);
            opn = condnames[func-48];
        }
5139
        break;
B
bellard 已提交
5140
    case FOP(0, 17):
5141
        check_cp1_registers(ctx, fs | ft | fd);
B
bellard 已提交
5142 5143 5144 5145 5146
        GEN_LOAD_FREG_FTN(DT0, fs);
        GEN_LOAD_FREG_FTN(DT1, ft);
        gen_op_float_add_d();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "add.d";
5147
        optype = BINOP;
B
bellard 已提交
5148 5149
        break;
    case FOP(1, 17):
5150
        check_cp1_registers(ctx, fs | ft | fd);
B
bellard 已提交
5151 5152 5153 5154 5155
        GEN_LOAD_FREG_FTN(DT0, fs);
        GEN_LOAD_FREG_FTN(DT1, ft);
        gen_op_float_sub_d();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "sub.d";
5156
        optype = BINOP;
B
bellard 已提交
5157 5158
        break;
    case FOP(2, 17):
5159
        check_cp1_registers(ctx, fs | ft | fd);
B
bellard 已提交
5160 5161 5162 5163 5164
        GEN_LOAD_FREG_FTN(DT0, fs);
        GEN_LOAD_FREG_FTN(DT1, ft);
        gen_op_float_mul_d();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "mul.d";
5165
        optype = BINOP;
B
bellard 已提交
5166 5167
        break;
    case FOP(3, 17):
5168
        check_cp1_registers(ctx, fs | ft | fd);
B
bellard 已提交
5169 5170 5171 5172 5173
        GEN_LOAD_FREG_FTN(DT0, fs);
        GEN_LOAD_FREG_FTN(DT1, ft);
        gen_op_float_div_d();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "div.d";
5174
        optype = BINOP;
B
bellard 已提交
5175 5176
        break;
    case FOP(4, 17):
5177
        check_cp1_registers(ctx, fs | fd);
B
bellard 已提交
5178 5179 5180 5181 5182 5183
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_sqrt_d();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "sqrt.d";
        break;
    case FOP(5, 17):
5184
        check_cp1_registers(ctx, fs | fd);
B
bellard 已提交
5185 5186 5187 5188 5189 5190
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_abs_d();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "abs.d";
        break;
    case FOP(6, 17):
5191
        check_cp1_registers(ctx, fs | fd);
B
bellard 已提交
5192 5193 5194 5195 5196 5197
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_mov_d();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "mov.d";
        break;
    case FOP(7, 17):
5198
        check_cp1_registers(ctx, fs | fd);
B
bellard 已提交
5199 5200 5201 5202 5203
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_chs_d();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "neg.d";
        break;
5204
    case FOP(8, 17):
5205
        check_cp1_64bitmode(ctx);
5206 5207 5208 5209 5210 5211
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_roundl_d();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "round.l.d";
        break;
    case FOP(9, 17):
5212
        check_cp1_64bitmode(ctx);
5213 5214 5215 5216 5217 5218
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_truncl_d();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "trunc.l.d";
        break;
    case FOP(10, 17):
5219
        check_cp1_64bitmode(ctx);
5220 5221 5222 5223 5224 5225
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_ceill_d();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "ceil.l.d";
        break;
    case FOP(11, 17):
5226
        check_cp1_64bitmode(ctx);
5227 5228 5229 5230 5231
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_floorl_d();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "floor.l.d";
        break;
B
bellard 已提交
5232
    case FOP(12, 17):
5233
        check_cp1_registers(ctx, fs);
B
bellard 已提交
5234 5235 5236 5237 5238 5239
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_roundw_d();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "round.w.d";
        break;
    case FOP(13, 17):
5240
        check_cp1_registers(ctx, fs);
B
bellard 已提交
5241 5242 5243 5244 5245 5246
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_truncw_d();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "trunc.w.d";
        break;
    case FOP(14, 17):
5247
        check_cp1_registers(ctx, fs);
B
bellard 已提交
5248 5249 5250 5251 5252 5253
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_ceilw_d();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "ceil.w.d";
        break;
    case FOP(15, 17):
5254
        check_cp1_registers(ctx, fs);
B
bellard 已提交
5255 5256 5257
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_floorw_d();
        GEN_STORE_FTN_FREG(fd, WT2);
5258
        opn = "floor.w.d";
B
bellard 已提交
5259
        break;
5260 5261 5262 5263 5264
    case FOP(17, 17):
        GEN_LOAD_REG_TN(T0, ft);
        GEN_LOAD_FREG_FTN(DT0, fs);
        GEN_LOAD_FREG_FTN(DT2, fd);
        gen_movcf_d(ctx, (ft >> 2) & 0x7, ft & 0x1);
5265
        GEN_STORE_FTN_FREG(fd, DT2);
5266
        opn = "movcf.d";
5267
        break;
5268 5269 5270 5271 5272
    case FOP(18, 17):
        GEN_LOAD_REG_TN(T0, ft);
        GEN_LOAD_FREG_FTN(DT0, fs);
        GEN_LOAD_FREG_FTN(DT2, fd);
        gen_op_float_movz_d();
B
bellard 已提交
5273
        GEN_STORE_FTN_FREG(fd, DT2);
5274 5275 5276 5277 5278 5279 5280 5281 5282
        opn = "movz.d";
        break;
    case FOP(19, 17):
        GEN_LOAD_REG_TN(T0, ft);
        GEN_LOAD_FREG_FTN(DT0, fs);
        GEN_LOAD_FREG_FTN(DT2, fd);
        gen_op_float_movn_d();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "movn.d";
B
bellard 已提交
5283
        break;
T
ths 已提交
5284
    case FOP(21, 17):
5285
        check_cp1_registers(ctx, fs | fd);
T
ths 已提交
5286 5287 5288 5289 5290 5291
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_recip_d();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "recip.d";
        break;
    case FOP(22, 17):
5292
        check_cp1_registers(ctx, fs | fd);
T
ths 已提交
5293 5294 5295 5296 5297 5298
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_rsqrt_d();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "rsqrt.d";
        break;
    case FOP(28, 17):
5299
        check_cp1_64bitmode(ctx);
T
ths 已提交
5300 5301 5302 5303 5304 5305 5306
        GEN_LOAD_FREG_FTN(DT0, fs);
        GEN_LOAD_FREG_FTN(DT2, ft);
        gen_op_float_recip2_d();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "recip2.d";
        break;
    case FOP(29, 17):
5307
        check_cp1_64bitmode(ctx);
T
ths 已提交
5308 5309 5310 5311 5312 5313
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_recip1_d();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "recip1.d";
        break;
    case FOP(30, 17):
5314
        check_cp1_64bitmode(ctx);
T
ths 已提交
5315 5316 5317 5318 5319 5320
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_rsqrt1_d();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "rsqrt1.d";
        break;
    case FOP(31, 17):
5321
        check_cp1_64bitmode(ctx);
T
ths 已提交
5322 5323 5324 5325 5326 5327
        GEN_LOAD_FREG_FTN(DT0, fs);
        GEN_LOAD_FREG_FTN(DT2, ft);
        gen_op_float_rsqrt2_d();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "rsqrt2.d";
        break;
B
bellard 已提交
5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345
    case FOP(48, 17):
    case FOP(49, 17):
    case FOP(50, 17):
    case FOP(51, 17):
    case FOP(52, 17):
    case FOP(53, 17):
    case FOP(54, 17):
    case FOP(55, 17):
    case FOP(56, 17):
    case FOP(57, 17):
    case FOP(58, 17):
    case FOP(59, 17):
    case FOP(60, 17):
    case FOP(61, 17):
    case FOP(62, 17):
    case FOP(63, 17):
        GEN_LOAD_FREG_FTN(DT0, fs);
        GEN_LOAD_FREG_FTN(DT1, ft);
5346
        if (ctx->opcode & (1 << 6)) {
5347
            check_cp1_64bitmode(ctx);
5348 5349 5350
            gen_cmpabs_d(func-48, cc);
            opn = condnames_abs[func-48];
        } else {
5351
            check_cp1_registers(ctx, fs | ft);
5352 5353 5354
            gen_cmp_d(func-48, cc);
            opn = condnames[func-48];
        }
B
bellard 已提交
5355
        break;
5356
    case FOP(32, 17):
5357
        check_cp1_registers(ctx, fs);
5358 5359 5360 5361 5362 5363
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_cvts_d();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "cvt.s.d";
        break;
    case FOP(36, 17):
5364
        check_cp1_registers(ctx, fs);
5365 5366 5367 5368 5369 5370
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_cvtw_d();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "cvt.w.d";
        break;
    case FOP(37, 17):
5371
        check_cp1_64bitmode(ctx);
5372 5373 5374 5375 5376 5377
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_cvtl_d();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "cvt.l.d";
        break;
    case FOP(32, 20):
B
bellard 已提交
5378
        GEN_LOAD_FREG_FTN(WT0, fs);
5379
        gen_op_float_cvts_w();
B
bellard 已提交
5380
        GEN_STORE_FTN_FREG(fd, WT2);
5381
        opn = "cvt.s.w";
B
bellard 已提交
5382
        break;
5383
    case FOP(33, 20):
5384
        check_cp1_registers(ctx, fd);
B
bellard 已提交
5385
        GEN_LOAD_FREG_FTN(WT0, fs);
5386 5387 5388 5389 5390
        gen_op_float_cvtd_w();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "cvt.d.w";
        break;
    case FOP(32, 21):
5391
        check_cp1_64bitmode(ctx);
5392 5393 5394 5395 5396 5397
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_cvts_l();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "cvt.s.l";
        break;
    case FOP(33, 21):
5398
        check_cp1_64bitmode(ctx);
5399 5400 5401 5402 5403 5404
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_cvtd_l();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "cvt.d.l";
        break;
    case FOP(38, 20):
5405
        check_cp1_64bitmode(ctx);
5406 5407 5408 5409 5410 5411 5412 5413
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WTH0, fs);
        gen_op_float_cvtps_pw();
        GEN_STORE_FTN_FREG(fd, WT2);
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "cvt.ps.pw";
        break;
    case FOP(0, 22):
5414
        check_cp1_64bitmode(ctx);
5415 5416
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WTH0, fs);
B
bellard 已提交
5417
        GEN_LOAD_FREG_FTN(WT1, ft);
5418 5419
        GEN_LOAD_FREG_FTN(WTH1, ft);
        gen_op_float_add_ps();
B
bellard 已提交
5420
        GEN_STORE_FTN_FREG(fd, WT2);
5421 5422
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "add.ps";
B
bellard 已提交
5423
        break;
5424
    case FOP(1, 22):
5425
        check_cp1_64bitmode(ctx);
B
bellard 已提交
5426
        GEN_LOAD_FREG_FTN(WT0, fs);
5427
        GEN_LOAD_FREG_FTN(WTH0, fs);
B
bellard 已提交
5428
        GEN_LOAD_FREG_FTN(WT1, ft);
5429 5430
        GEN_LOAD_FREG_FTN(WTH1, ft);
        gen_op_float_sub_ps();
B
bellard 已提交
5431
        GEN_STORE_FTN_FREG(fd, WT2);
5432 5433
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "sub.ps";
B
bellard 已提交
5434
        break;
5435
    case FOP(2, 22):
5436
        check_cp1_64bitmode(ctx);
B
bellard 已提交
5437
        GEN_LOAD_FREG_FTN(WT0, fs);
5438
        GEN_LOAD_FREG_FTN(WTH0, fs);
B
bellard 已提交
5439
        GEN_LOAD_FREG_FTN(WT1, ft);
5440 5441
        GEN_LOAD_FREG_FTN(WTH1, ft);
        gen_op_float_mul_ps();
B
bellard 已提交
5442
        GEN_STORE_FTN_FREG(fd, WT2);
5443 5444
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "mul.ps";
B
bellard 已提交
5445
        break;
5446
    case FOP(5, 22):
5447
        check_cp1_64bitmode(ctx);
B
bellard 已提交
5448
        GEN_LOAD_FREG_FTN(WT0, fs);
5449 5450
        GEN_LOAD_FREG_FTN(WTH0, fs);
        gen_op_float_abs_ps();
B
bellard 已提交
5451
        GEN_STORE_FTN_FREG(fd, WT2);
5452 5453
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "abs.ps";
B
bellard 已提交
5454
        break;
5455
    case FOP(6, 22):
5456
        check_cp1_64bitmode(ctx);
B
bellard 已提交
5457
        GEN_LOAD_FREG_FTN(WT0, fs);
5458 5459
        GEN_LOAD_FREG_FTN(WTH0, fs);
        gen_op_float_mov_ps();
B
bellard 已提交
5460
        GEN_STORE_FTN_FREG(fd, WT2);
5461 5462
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "mov.ps";
B
bellard 已提交
5463
        break;
5464
    case FOP(7, 22):
5465
        check_cp1_64bitmode(ctx);
B
bellard 已提交
5466
        GEN_LOAD_FREG_FTN(WT0, fs);
5467 5468
        GEN_LOAD_FREG_FTN(WTH0, fs);
        gen_op_float_chs_ps();
B
bellard 已提交
5469
        GEN_STORE_FTN_FREG(fd, WT2);
5470 5471
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "neg.ps";
B
bellard 已提交
5472
        break;
5473
    case FOP(17, 22):
5474
        check_cp1_64bitmode(ctx);
5475
        GEN_LOAD_REG_TN(T0, ft);
B
bellard 已提交
5476
        GEN_LOAD_FREG_FTN(WT0, fs);
5477 5478 5479 5480
        GEN_LOAD_FREG_FTN(WTH0, fs);
        GEN_LOAD_FREG_FTN(WT2, fd);
        GEN_LOAD_FREG_FTN(WTH2, fd);
        gen_movcf_ps(ctx, (ft >> 2) & 0x7, ft & 0x1);
B
bellard 已提交
5481
        GEN_STORE_FTN_FREG(fd, WT2);
5482 5483
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "movcf.ps";
B
bellard 已提交
5484
        break;
5485
    case FOP(18, 22):
5486
        check_cp1_64bitmode(ctx);
5487
        GEN_LOAD_REG_TN(T0, ft);
B
bellard 已提交
5488
        GEN_LOAD_FREG_FTN(WT0, fs);
5489 5490 5491 5492
        GEN_LOAD_FREG_FTN(WTH0, fs);
        GEN_LOAD_FREG_FTN(WT2, fd);
        GEN_LOAD_FREG_FTN(WTH2, fd);
        gen_op_float_movz_ps();
B
bellard 已提交
5493
        GEN_STORE_FTN_FREG(fd, WT2);
5494 5495
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "movz.ps";
B
bellard 已提交
5496
        break;
5497
    case FOP(19, 22):
5498
        check_cp1_64bitmode(ctx);
5499
        GEN_LOAD_REG_TN(T0, ft);
B
bellard 已提交
5500
        GEN_LOAD_FREG_FTN(WT0, fs);
5501 5502 5503 5504
        GEN_LOAD_FREG_FTN(WTH0, fs);
        GEN_LOAD_FREG_FTN(WT2, fd);
        GEN_LOAD_FREG_FTN(WTH2, fd);
        gen_op_float_movn_ps();
B
bellard 已提交
5505
        GEN_STORE_FTN_FREG(fd, WT2);
5506 5507
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "movn.ps";
B
bellard 已提交
5508
        break;
5509
    case FOP(24, 22):
5510
        check_cp1_64bitmode(ctx);
5511 5512 5513 5514
        GEN_LOAD_FREG_FTN(WT0, ft);
        GEN_LOAD_FREG_FTN(WTH0, ft);
        GEN_LOAD_FREG_FTN(WT1, fs);
        GEN_LOAD_FREG_FTN(WTH1, fs);
5515 5516 5517 5518 5519
        gen_op_float_addr_ps();
        GEN_STORE_FTN_FREG(fd, WT2);
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "addr.ps";
        break;
T
ths 已提交
5520
    case FOP(26, 22):
5521
        check_cp1_64bitmode(ctx);
5522 5523 5524 5525
        GEN_LOAD_FREG_FTN(WT0, ft);
        GEN_LOAD_FREG_FTN(WTH0, ft);
        GEN_LOAD_FREG_FTN(WT1, fs);
        GEN_LOAD_FREG_FTN(WTH1, fs);
T
ths 已提交
5526 5527 5528 5529 5530 5531
        gen_op_float_mulr_ps();
        GEN_STORE_FTN_FREG(fd, WT2);
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "mulr.ps";
        break;
    case FOP(28, 22):
5532
        check_cp1_64bitmode(ctx);
T
ths 已提交
5533 5534 5535 5536 5537 5538 5539 5540 5541 5542
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WTH0, fs);
        GEN_LOAD_FREG_FTN(WT2, fd);
        GEN_LOAD_FREG_FTN(WTH2, fd);
        gen_op_float_recip2_ps();
        GEN_STORE_FTN_FREG(fd, WT2);
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "recip2.ps";
        break;
    case FOP(29, 22):
5543
        check_cp1_64bitmode(ctx);
T
ths 已提交
5544 5545 5546 5547 5548 5549 5550 5551
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WTH0, fs);
        gen_op_float_recip1_ps();
        GEN_STORE_FTN_FREG(fd, WT2);
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "recip1.ps";
        break;
    case FOP(30, 22):
5552
        check_cp1_64bitmode(ctx);
T
ths 已提交
5553 5554 5555 5556 5557 5558 5559 5560
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WTH0, fs);
        gen_op_float_rsqrt1_ps();
        GEN_STORE_FTN_FREG(fd, WT2);
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "rsqrt1.ps";
        break;
    case FOP(31, 22):
5561
        check_cp1_64bitmode(ctx);
T
ths 已提交
5562 5563
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WTH0, fs);
T
ths 已提交
5564 5565
        GEN_LOAD_FREG_FTN(WT2, ft);
        GEN_LOAD_FREG_FTN(WTH2, ft);
T
ths 已提交
5566 5567 5568 5569 5570
        gen_op_float_rsqrt2_ps();
        GEN_STORE_FTN_FREG(fd, WT2);
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "rsqrt2.ps";
        break;
5571
    case FOP(32, 22):
5572
        check_cp1_64bitmode(ctx);
5573 5574
        GEN_LOAD_FREG_FTN(WTH0, fs);
        gen_op_float_cvts_pu();
5575
        GEN_STORE_FTN_FREG(fd, WT2);
5576
        opn = "cvt.s.pu";
5577
        break;
5578
    case FOP(36, 22):
5579
        check_cp1_64bitmode(ctx);
B
bellard 已提交
5580
        GEN_LOAD_FREG_FTN(WT0, fs);
5581 5582
        GEN_LOAD_FREG_FTN(WTH0, fs);
        gen_op_float_cvtpw_ps();
B
bellard 已提交
5583
        GEN_STORE_FTN_FREG(fd, WT2);
5584 5585
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "cvt.pw.ps";
B
bellard 已提交
5586
        break;
5587
    case FOP(40, 22):
5588
        check_cp1_64bitmode(ctx);
B
bellard 已提交
5589
        GEN_LOAD_FREG_FTN(WT0, fs);
5590
        gen_op_float_cvts_pl();
B
bellard 已提交
5591
        GEN_STORE_FTN_FREG(fd, WT2);
5592
        opn = "cvt.s.pl";
B
bellard 已提交
5593
        break;
5594
    case FOP(44, 22):
5595
        check_cp1_64bitmode(ctx);
5596 5597 5598 5599 5600
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WT1, ft);
        gen_op_float_pll_ps();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "pll.ps";
B
bellard 已提交
5601
        break;
5602
    case FOP(45, 22):
5603
        check_cp1_64bitmode(ctx);
B
bellard 已提交
5604
        GEN_LOAD_FREG_FTN(WT0, fs);
5605 5606 5607 5608 5609 5610
        GEN_LOAD_FREG_FTN(WTH1, ft);
        gen_op_float_plu_ps();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "plu.ps";
        break;
    case FOP(46, 22):
5611
        check_cp1_64bitmode(ctx);
5612
        GEN_LOAD_FREG_FTN(WTH0, fs);
B
bellard 已提交
5613
        GEN_LOAD_FREG_FTN(WT1, ft);
5614 5615 5616 5617 5618
        gen_op_float_pul_ps();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "pul.ps";
        break;
    case FOP(47, 22):
5619
        check_cp1_64bitmode(ctx);
5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641
        GEN_LOAD_FREG_FTN(WTH0, fs);
        GEN_LOAD_FREG_FTN(WTH1, ft);
        gen_op_float_puu_ps();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "puu.ps";
        break;
    case FOP(48, 22):
    case FOP(49, 22):
    case FOP(50, 22):
    case FOP(51, 22):
    case FOP(52, 22):
    case FOP(53, 22):
    case FOP(54, 22):
    case FOP(55, 22):
    case FOP(56, 22):
    case FOP(57, 22):
    case FOP(58, 22):
    case FOP(59, 22):
    case FOP(60, 22):
    case FOP(61, 22):
    case FOP(62, 22):
    case FOP(63, 22):
5642
        check_cp1_64bitmode(ctx);
5643 5644 5645 5646
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WTH0, fs);
        GEN_LOAD_FREG_FTN(WT1, ft);
        GEN_LOAD_FREG_FTN(WTH1, ft);
5647 5648 5649 5650 5651 5652 5653
        if (ctx->opcode & (1 << 6)) {
            gen_cmpabs_ps(func-48, cc);
            opn = condnames_abs[func-48];
        } else {
            gen_cmp_ps(func-48, cc);
            opn = condnames[func-48];
        }
B
bellard 已提交
5654
        break;
5655
    default:
5656
        MIPS_INVAL(opn);
5657
        generate_exception (ctx, EXCP_RI);
B
bellard 已提交
5658 5659
        return;
    }
5660 5661
    switch (optype) {
    case BINOP:
B
bellard 已提交
5662
        MIPS_DEBUG("%s %s, %s, %s", opn, fregnames[fd], fregnames[fs], fregnames[ft]);
5663 5664 5665 5666 5667
        break;
    case CMPOP:
        MIPS_DEBUG("%s %s,%s", opn, fregnames[fs], fregnames[ft]);
        break;
    default:
B
bellard 已提交
5668
        MIPS_DEBUG("%s %s,%s", opn, fregnames[fd], fregnames[fs]);
5669 5670
        break;
    }
B
bellard 已提交
5671
}
B
bellard 已提交
5672

5673
/* Coprocessor 3 (FPU) */
5674 5675
static void gen_flt3_ldst (DisasContext *ctx, uint32_t opc,
                           int fd, int fs, int base, int index)
5676
{
5677
    const char *opn = "extended float load/store";
T
ths 已提交
5678
    int store = 0;
5679

T
ths 已提交
5680
    /* All of those work only on 64bit FPUs. */
5681
    check_cp1_64bitmode(ctx);
T
ths 已提交
5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693
    if (base == 0) {
        if (index == 0)
            gen_op_reset_T0();
        else
            GEN_LOAD_REG_TN(T0, index);
    } else if (index == 0) {
        GEN_LOAD_REG_TN(T0, base);
    } else {
        GEN_LOAD_REG_TN(T0, base);
        GEN_LOAD_REG_TN(T1, index);
        gen_op_addr_add();
    }
5694
    /* Don't do NOP if destination is zero: we must perform the actual
5695
       memory access. */
5696 5697
    switch (opc) {
    case OPC_LWXC1:
T
ths 已提交
5698
        op_ldst(lwc1);
5699 5700 5701 5702
        GEN_STORE_FTN_FREG(fd, WT0);
        opn = "lwxc1";
        break;
    case OPC_LDXC1:
T
ths 已提交
5703
        op_ldst(ldc1);
5704 5705 5706 5707 5708 5709 5710 5711 5712
        GEN_STORE_FTN_FREG(fd, DT0);
        opn = "ldxc1";
        break;
    case OPC_LUXC1:
        op_ldst(luxc1);
        GEN_STORE_FTN_FREG(fd, DT0);
        opn = "luxc1";
        break;
    case OPC_SWXC1:
T
ths 已提交
5713 5714
        GEN_LOAD_FREG_FTN(WT0, fs);
        op_ldst(swc1);
5715
        opn = "swxc1";
T
ths 已提交
5716
        store = 1;
5717 5718
        break;
    case OPC_SDXC1:
T
ths 已提交
5719 5720
        GEN_LOAD_FREG_FTN(DT0, fs);
        op_ldst(sdc1);
5721
        opn = "sdxc1";
T
ths 已提交
5722
        store = 1;
5723 5724
        break;
    case OPC_SUXC1:
T
ths 已提交
5725
        GEN_LOAD_FREG_FTN(DT0, fs);
5726 5727
        op_ldst(suxc1);
        opn = "suxc1";
T
ths 已提交
5728
        store = 1;
5729 5730
        break;
    default:
5731
        MIPS_INVAL(opn);
5732 5733 5734
        generate_exception(ctx, EXCP_RI);
        return;
    }
T
ths 已提交
5735 5736
    MIPS_DEBUG("%s %s, %s(%s)", opn, fregnames[store ? fs : fd],
               regnames[index], regnames[base]);
5737 5738
}

5739 5740
static void gen_flt3_arith (DisasContext *ctx, uint32_t opc,
                            int fd, int fr, int fs, int ft)
5741
{
5742
    const char *opn = "flt3_arith";
5743 5744

    /* All of those work only on 64bit FPUs. */
5745
    check_cp1_64bitmode(ctx);
5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763
    switch (opc) {
    case OPC_ALNV_PS:
        GEN_LOAD_REG_TN(T0, fr);
        GEN_LOAD_FREG_FTN(DT0, fs);
        GEN_LOAD_FREG_FTN(DT1, ft);
        gen_op_float_alnv_ps();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "alnv.ps";
        break;
    case OPC_MADD_S:
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WT1, ft);
        GEN_LOAD_FREG_FTN(WT2, fr);
        gen_op_float_muladd_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "madd.s";
        break;
    case OPC_MADD_D:
5764 5765 5766 5767 5768
        GEN_LOAD_FREG_FTN(DT0, fs);
        GEN_LOAD_FREG_FTN(DT1, ft);
        GEN_LOAD_FREG_FTN(DT2, fr);
        gen_op_float_muladd_d();
        GEN_STORE_FTN_FREG(fd, DT2);
5769 5770 5771
        opn = "madd.d";
        break;
    case OPC_MADD_PS:
5772 5773 5774 5775 5776 5777 5778 5779 5780
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WTH0, fs);
        GEN_LOAD_FREG_FTN(WT1, ft);
        GEN_LOAD_FREG_FTN(WTH1, ft);
        GEN_LOAD_FREG_FTN(WT2, fr);
        GEN_LOAD_FREG_FTN(WTH2, fr);
        gen_op_float_muladd_ps();
        GEN_STORE_FTN_FREG(fd, WT2);
        GEN_STORE_FTN_FREG(fd, WTH2);
5781 5782 5783
        opn = "madd.ps";
        break;
    case OPC_MSUB_S:
5784 5785 5786 5787 5788
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WT1, ft);
        GEN_LOAD_FREG_FTN(WT2, fr);
        gen_op_float_mulsub_s();
        GEN_STORE_FTN_FREG(fd, WT2);
5789 5790 5791
        opn = "msub.s";
        break;
    case OPC_MSUB_D:
5792 5793 5794 5795 5796
        GEN_LOAD_FREG_FTN(DT0, fs);
        GEN_LOAD_FREG_FTN(DT1, ft);
        GEN_LOAD_FREG_FTN(DT2, fr);
        gen_op_float_mulsub_d();
        GEN_STORE_FTN_FREG(fd, DT2);
5797 5798 5799
        opn = "msub.d";
        break;
    case OPC_MSUB_PS:
5800 5801 5802 5803 5804 5805 5806 5807 5808
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WTH0, fs);
        GEN_LOAD_FREG_FTN(WT1, ft);
        GEN_LOAD_FREG_FTN(WTH1, ft);
        GEN_LOAD_FREG_FTN(WT2, fr);
        GEN_LOAD_FREG_FTN(WTH2, fr);
        gen_op_float_mulsub_ps();
        GEN_STORE_FTN_FREG(fd, WT2);
        GEN_STORE_FTN_FREG(fd, WTH2);
5809 5810 5811
        opn = "msub.ps";
        break;
    case OPC_NMADD_S:
5812 5813 5814 5815 5816
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WT1, ft);
        GEN_LOAD_FREG_FTN(WT2, fr);
        gen_op_float_nmuladd_s();
        GEN_STORE_FTN_FREG(fd, WT2);
5817 5818 5819
        opn = "nmadd.s";
        break;
    case OPC_NMADD_D:
5820 5821 5822 5823 5824
        GEN_LOAD_FREG_FTN(DT0, fs);
        GEN_LOAD_FREG_FTN(DT1, ft);
        GEN_LOAD_FREG_FTN(DT2, fr);
        gen_op_float_nmuladd_d();
        GEN_STORE_FTN_FREG(fd, DT2);
5825 5826 5827
        opn = "nmadd.d";
        break;
    case OPC_NMADD_PS:
5828 5829 5830 5831 5832 5833 5834 5835 5836
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WTH0, fs);
        GEN_LOAD_FREG_FTN(WT1, ft);
        GEN_LOAD_FREG_FTN(WTH1, ft);
        GEN_LOAD_FREG_FTN(WT2, fr);
        GEN_LOAD_FREG_FTN(WTH2, fr);
        gen_op_float_nmuladd_ps();
        GEN_STORE_FTN_FREG(fd, WT2);
        GEN_STORE_FTN_FREG(fd, WTH2);
5837 5838 5839
        opn = "nmadd.ps";
        break;
    case OPC_NMSUB_S:
5840 5841 5842 5843 5844
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WT1, ft);
        GEN_LOAD_FREG_FTN(WT2, fr);
        gen_op_float_nmulsub_s();
        GEN_STORE_FTN_FREG(fd, WT2);
5845 5846 5847
        opn = "nmsub.s";
        break;
    case OPC_NMSUB_D:
5848 5849 5850 5851 5852
        GEN_LOAD_FREG_FTN(DT0, fs);
        GEN_LOAD_FREG_FTN(DT1, ft);
        GEN_LOAD_FREG_FTN(DT2, fr);
        gen_op_float_nmulsub_d();
        GEN_STORE_FTN_FREG(fd, DT2);
5853 5854 5855
        opn = "nmsub.d";
        break;
    case OPC_NMSUB_PS:
5856 5857 5858 5859 5860 5861 5862 5863 5864
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WTH0, fs);
        GEN_LOAD_FREG_FTN(WT1, ft);
        GEN_LOAD_FREG_FTN(WTH1, ft);
        GEN_LOAD_FREG_FTN(WT2, fr);
        GEN_LOAD_FREG_FTN(WTH2, fr);
        gen_op_float_nmulsub_ps();
        GEN_STORE_FTN_FREG(fd, WT2);
        GEN_STORE_FTN_FREG(fd, WTH2);
5865 5866
        opn = "nmsub.ps";
        break;
5867 5868
    default:
        MIPS_INVAL(opn);
5869 5870 5871 5872 5873
        generate_exception (ctx, EXCP_RI);
        return;
    }
    MIPS_DEBUG("%s %s, %s, %s, %s", opn, fregnames[fd], fregnames[fr],
               fregnames[fs], fregnames[ft]);
5874 5875 5876
}

/* ISA extensions (ASEs) */
B
bellard 已提交
5877 5878 5879
/* MIPS16 extension to MIPS32 */
/* SmartMIPS extension to MIPS32 */

5880
#if defined(TARGET_MIPS64)
B
bellard 已提交
5881 5882 5883 5884 5885

/* MDMX extension to MIPS64 */

#endif

5886
static void decode_opc (CPUState *env, DisasContext *ctx)
B
bellard 已提交
5887 5888 5889
{
    int32_t offset;
    int rs, rt, rd, sa;
5890
    uint32_t op, op1, op2;
B
bellard 已提交
5891 5892
    int16_t imm;

5893 5894
    /* make sure instructions are on a word boundary */
    if (ctx->pc & 0x3) {
5895
        env->CP0_BadVAddr = ctx->pc;
5896 5897 5898 5899
        generate_exception(ctx, EXCP_AdEL);
        return;
    }

B
bellard 已提交
5900
    if ((ctx->hflags & MIPS_HFLAG_BMASK) == MIPS_HFLAG_BL) {
5901
        int l1;
B
bellard 已提交
5902
        /* Handle blikely not taken case */
T
ths 已提交
5903
        MIPS_DEBUG("blikely condition (" TARGET_FMT_lx ")", ctx->pc + 4);
5904 5905 5906 5907 5908
        l1 = gen_new_label();
        gen_op_jnz_T2(l1);
        gen_op_save_state(ctx->hflags & ~MIPS_HFLAG_BMASK);
        gen_goto_tb(ctx, 1, ctx->pc + 4);
        gen_set_label(l1);
B
bellard 已提交
5909
    }
5910 5911 5912 5913 5914
    op = MASK_OP_MAJOR(ctx->opcode);
    rs = (ctx->opcode >> 21) & 0x1f;
    rt = (ctx->opcode >> 16) & 0x1f;
    rd = (ctx->opcode >> 11) & 0x1f;
    sa = (ctx->opcode >> 6) & 0x1f;
B
bellard 已提交
5915 5916
    imm = (int16_t)ctx->opcode;
    switch (op) {
5917 5918
    case OPC_SPECIAL:
        op1 = MASK_SPECIAL(ctx->opcode);
B
bellard 已提交
5919
        switch (op1) {
5920 5921
        case OPC_SLL:          /* Arithmetic with immediate */
        case OPC_SRL ... OPC_SRA:
5922
            gen_arith_imm(env, ctx, op1, rd, rt, sa);
5923
            break;
5924 5925
        case OPC_MOVZ ... OPC_MOVN:
            check_insn(env, ctx, ISA_MIPS4 | ISA_MIPS32);
5926 5927 5928 5929
        case OPC_SLLV:         /* Arithmetic */
        case OPC_SRLV ... OPC_SRAV:
        case OPC_ADD ... OPC_NOR:
        case OPC_SLT ... OPC_SLTU:
5930
            gen_arith(env, ctx, op1, rd, rs, rt);
5931 5932 5933 5934 5935 5936
            break;
        case OPC_MULT ... OPC_DIVU:
            gen_muldiv(ctx, op1, rs, rt);
            break;
        case OPC_JR ... OPC_JALR:
            gen_compute_branch(ctx, op1, rs, rd, sa);
B
bellard 已提交
5937
            return;
5938 5939 5940
        case OPC_TGE ... OPC_TEQ: /* Traps */
        case OPC_TNE:
            gen_trap(ctx, op1, rs, rt, -1);
B
bellard 已提交
5941
            break;
5942 5943 5944
        case OPC_MFHI:          /* Move from HI/LO */
        case OPC_MFLO:
            gen_HILO(ctx, op1, rd);
B
bellard 已提交
5945
            break;
5946 5947 5948
        case OPC_MTHI:
        case OPC_MTLO:          /* Move to HI/LO */
            gen_HILO(ctx, op1, rs);
B
bellard 已提交
5949
            break;
5950 5951 5952 5953 5954
        case OPC_PMON:          /* Pmon entry point, also R4010 selsl */
#ifdef MIPS_STRICT_STANDARD
            MIPS_INVAL("PMON / selsl");
            generate_exception(ctx, EXCP_RI);
#else
5955
            gen_op_pmon(sa);
5956
#endif
5957 5958
            break;
        case OPC_SYSCALL:
B
bellard 已提交
5959 5960
            generate_exception(ctx, EXCP_SYSCALL);
            break;
5961
        case OPC_BREAK:
B
bellard 已提交
5962 5963
            generate_exception(ctx, EXCP_BREAK);
            break;
5964 5965 5966 5967 5968
        case OPC_SPIM:
#ifdef MIPS_STRICT_STANDARD
            MIPS_INVAL("SPIM");
            generate_exception(ctx, EXCP_RI);
#else
5969 5970 5971
           /* Implemented as RI exception for now. */
            MIPS_INVAL("spim (unofficial)");
            generate_exception(ctx, EXCP_RI);
5972
#endif
B
bellard 已提交
5973
            break;
5974
        case OPC_SYNC:
5975
            /* Treat as NOP. */
B
bellard 已提交
5976
            break;
B
bellard 已提交
5977

5978
        case OPC_MOVCI:
5979
            check_insn(env, ctx, ISA_MIPS4 | ISA_MIPS32);
5980
            if (env->CP0_Config1 & (1 << CP0C1_FP)) {
5981
                save_cpu_state(ctx, 1);
5982
                check_cp1_enabled(ctx);
5983 5984 5985
                gen_movci(ctx, rd, rs, (ctx->opcode >> 18) & 0x7,
                          (ctx->opcode >> 16) & 1);
            } else {
5986
                generate_exception_err(ctx, EXCP_CpU, 1);
5987
            }
B
bellard 已提交
5988 5989
            break;

5990
#if defined(TARGET_MIPS64)
5991 5992 5993 5994 5995
       /* MIPS64 specific opcodes */
        case OPC_DSLL:
        case OPC_DSRL ... OPC_DSRA:
        case OPC_DSLL32:
        case OPC_DSRL32 ... OPC_DSRA32:
5996 5997 5998
            check_insn(env, ctx, ISA_MIPS3);
            check_mips_64(ctx);
            gen_arith_imm(env, ctx, op1, rd, rt, sa);
5999 6000 6001 6002
            break;
        case OPC_DSLLV:
        case OPC_DSRLV ... OPC_DSRAV:
        case OPC_DADD ... OPC_DSUBU:
6003 6004 6005
            check_insn(env, ctx, ISA_MIPS3);
            check_mips_64(ctx);
            gen_arith(env, ctx, op1, rd, rs, rt);
6006 6007
            break;
        case OPC_DMULT ... OPC_DDIVU:
6008 6009
            check_insn(env, ctx, ISA_MIPS3);
            check_mips_64(ctx);
6010 6011
            gen_muldiv(ctx, op1, rs, rt);
            break;
B
bellard 已提交
6012 6013 6014 6015 6016 6017 6018
#endif
        default:            /* Invalid */
            MIPS_INVAL("special");
            generate_exception(ctx, EXCP_RI);
            break;
        }
        break;
6019 6020
    case OPC_SPECIAL2:
        op1 = MASK_SPECIAL2(ctx->opcode);
B
bellard 已提交
6021
        switch (op1) {
6022 6023
        case OPC_MADD ... OPC_MADDU: /* Multiply and add/sub */
        case OPC_MSUB ... OPC_MSUBU:
6024
            check_insn(env, ctx, ISA_MIPS32);
6025
            gen_muldiv(ctx, op1, rs, rt);
B
bellard 已提交
6026
            break;
6027
        case OPC_MUL:
6028
            gen_arith(env, ctx, op1, rd, rs, rt);
B
bellard 已提交
6029
            break;
6030
        case OPC_CLZ ... OPC_CLO:
6031
            check_insn(env, ctx, ISA_MIPS32);
6032
            gen_cl(ctx, op1, rd, rs);
B
bellard 已提交
6033
            break;
6034
        case OPC_SDBBP:
B
bellard 已提交
6035 6036 6037
            /* XXX: not clear which exception should be raised
             *      when in debug mode...
             */
6038
            check_insn(env, ctx, ISA_MIPS32);
B
bellard 已提交
6039 6040 6041 6042 6043
            if (!(ctx->hflags & MIPS_HFLAG_DM)) {
                generate_exception(ctx, EXCP_DBp);
            } else {
                generate_exception(ctx, EXCP_DBp);
            }
6044
            /* Treat as NOP. */
B
bellard 已提交
6045
            break;
6046
#if defined(TARGET_MIPS64)
6047
        case OPC_DCLZ ... OPC_DCLO:
6048 6049
            check_insn(env, ctx, ISA_MIPS64);
            check_mips_64(ctx);
6050 6051 6052
            gen_cl(ctx, op1, rd, rs);
            break;
#endif
B
bellard 已提交
6053 6054 6055 6056 6057 6058
        default:            /* Invalid */
            MIPS_INVAL("special2");
            generate_exception(ctx, EXCP_RI);
            break;
        }
        break;
6059
    case OPC_SPECIAL3:
6060 6061 6062 6063
         op1 = MASK_SPECIAL3(ctx->opcode);
         switch (op1) {
         case OPC_EXT:
         case OPC_INS:
6064
             check_insn(env, ctx, ISA_MIPS32R2);
6065 6066 6067
             gen_bitops(ctx, op1, rt, rs, sa, rd);
             break;
         case OPC_BSHFL:
6068
             check_insn(env, ctx, ISA_MIPS32R2);
6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088
             op2 = MASK_BSHFL(ctx->opcode);
             switch (op2) {
             case OPC_WSBH:
                 GEN_LOAD_REG_TN(T1, rt);
                 gen_op_wsbh();
                 break;
             case OPC_SEB:
                 GEN_LOAD_REG_TN(T1, rt);
                 gen_op_seb();
                 break;
             case OPC_SEH:
                 GEN_LOAD_REG_TN(T1, rt);
                 gen_op_seh();
                 break;
             default:            /* Invalid */
                 MIPS_INVAL("bshfl");
                 generate_exception(ctx, EXCP_RI);
                 break;
            }
            GEN_STORE_TN_REG(rd, T0);
6089
            break;
6090
        case OPC_RDHWR:
6091
            check_insn(env, ctx, ISA_MIPS32R2);
6092 6093
            switch (rd) {
            case 0:
6094
                save_cpu_state(ctx, 1);
6095
                gen_op_rdhwr_cpunum();
6096
                break;
6097
            case 1:
6098
                save_cpu_state(ctx, 1);
6099
                gen_op_rdhwr_synci_step();
6100
                break;
6101
            case 2:
6102
                save_cpu_state(ctx, 1);
6103
                gen_op_rdhwr_cc();
6104
                break;
6105
            case 3:
6106
                save_cpu_state(ctx, 1);
6107
                gen_op_rdhwr_ccres();
6108
                break;
6109
            case 29:
6110
#if defined (CONFIG_USER_ONLY)
6111
                gen_op_tls_value();
6112
                break;
6113
#endif
6114 6115 6116 6117 6118 6119 6120
            default:            /* Invalid */
                MIPS_INVAL("rdhwr");
                generate_exception(ctx, EXCP_RI);
                break;
            }
            GEN_STORE_TN_REG(rt, T0);
            break;
6121
        case OPC_FORK:
6122
            check_insn(env, ctx, ASE_MT);
6123 6124 6125 6126 6127
            GEN_LOAD_REG_TN(T0, rt);
            GEN_LOAD_REG_TN(T1, rs);
            gen_op_fork();
            break;
        case OPC_YIELD:
6128
            check_insn(env, ctx, ASE_MT);
6129 6130 6131 6132
            GEN_LOAD_REG_TN(T0, rs);
            gen_op_yield();
            GEN_STORE_TN_REG(rd, T0);
            break;
6133
#if defined(TARGET_MIPS64)
6134 6135
        case OPC_DEXTM ... OPC_DEXT:
        case OPC_DINSM ... OPC_DINS:
6136 6137
            check_insn(env, ctx, ISA_MIPS64R2);
            check_mips_64(ctx);
6138
            gen_bitops(ctx, op1, rt, rs, sa, rd);
6139
            break;
6140
        case OPC_DBSHFL:
6141 6142
            check_insn(env, ctx, ISA_MIPS64R2);
            check_mips_64(ctx);
6143 6144 6145 6146 6147 6148 6149 6150 6151 6152
            op2 = MASK_DBSHFL(ctx->opcode);
            switch (op2) {
            case OPC_DSBH:
                GEN_LOAD_REG_TN(T1, rt);
                gen_op_dsbh();
                break;
            case OPC_DSHD:
                GEN_LOAD_REG_TN(T1, rt);
                gen_op_dshd();
                break;
6153 6154 6155 6156
            default:            /* Invalid */
                MIPS_INVAL("dbshfl");
                generate_exception(ctx, EXCP_RI);
                break;
6157 6158
            }
            GEN_STORE_TN_REG(rd, T0);
6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171
#endif
        default:            /* Invalid */
            MIPS_INVAL("special3");
            generate_exception(ctx, EXCP_RI);
            break;
        }
        break;
    case OPC_REGIMM:
        op1 = MASK_REGIMM(ctx->opcode);
        switch (op1) {
        case OPC_BLTZ ... OPC_BGEZL: /* REGIMM branches */
        case OPC_BLTZAL ... OPC_BGEZALL:
            gen_compute_branch(ctx, op1, rs, -1, imm << 2);
B
bellard 已提交
6172
            return;
6173 6174 6175 6176 6177
        case OPC_TGEI ... OPC_TEQI: /* REGIMM traps */
        case OPC_TNEI:
            gen_trap(ctx, op1, rs, -1, imm);
            break;
        case OPC_SYNCI:
6178
            check_insn(env, ctx, ISA_MIPS32R2);
6179
            /* Treat as NOP. */
B
bellard 已提交
6180 6181
            break;
        default:            /* Invalid */
6182
            MIPS_INVAL("regimm");
B
bellard 已提交
6183 6184 6185 6186
            generate_exception(ctx, EXCP_RI);
            break;
        }
        break;
6187
    case OPC_CP0:
6188
        check_cp0_enabled(ctx);
6189
        op1 = MASK_CP0(ctx->opcode);
B
bellard 已提交
6190
        switch (op1) {
6191 6192
        case OPC_MFC0:
        case OPC_MTC0:
6193 6194
        case OPC_MFTR:
        case OPC_MTTR:
6195
#if defined(TARGET_MIPS64)
6196 6197 6198
        case OPC_DMFC0:
        case OPC_DMTC0:
#endif
6199
            gen_cp0(env, ctx, op1, rt, rd);
6200 6201
            break;
        case OPC_C0_FIRST ... OPC_C0_LAST:
6202
            gen_cp0(env, ctx, MASK_C0(ctx->opcode), rt, rd);
6203 6204 6205 6206
            break;
        case OPC_MFMC0:
            op2 = MASK_MFMC0(ctx->opcode);
            switch (op2) {
6207
            case OPC_DMT:
6208
                check_insn(env, ctx, ASE_MT);
6209 6210 6211
                gen_op_dmt();
                break;
            case OPC_EMT:
6212
                check_insn(env, ctx, ASE_MT);
6213 6214 6215
                gen_op_emt();
                break;
            case OPC_DVPE:
6216
                check_insn(env, ctx, ASE_MT);
6217 6218 6219
                gen_op_dvpe();
                break;
            case OPC_EVPE:
6220
                check_insn(env, ctx, ASE_MT);
6221 6222
                gen_op_evpe();
                break;
6223
            case OPC_DI:
6224
                check_insn(env, ctx, ISA_MIPS32R2);
6225
                save_cpu_state(ctx, 1);
6226 6227 6228 6229 6230
                gen_op_di();
                /* Stop translation as we may have switched the execution mode */
                ctx->bstate = BS_STOP;
                break;
            case OPC_EI:
6231
                check_insn(env, ctx, ISA_MIPS32R2);
6232
                save_cpu_state(ctx, 1);
6233 6234 6235 6236 6237
                gen_op_ei();
                /* Stop translation as we may have switched the execution mode */
                ctx->bstate = BS_STOP;
                break;
            default:            /* Invalid */
6238
                MIPS_INVAL("mfmc0");
6239 6240 6241 6242
                generate_exception(ctx, EXCP_RI);
                break;
            }
            GEN_STORE_TN_REG(rt, T0);
B
bellard 已提交
6243
            break;
6244
        case OPC_RDPGPR:
6245
            check_insn(env, ctx, ISA_MIPS32R2);
6246 6247 6248
            GEN_LOAD_SRSREG_TN(T0, rt);
            GEN_STORE_TN_REG(rd, T0);
            break;
6249
        case OPC_WRPGPR:
6250
            check_insn(env, ctx, ISA_MIPS32R2);
6251
            GEN_LOAD_REG_TN(T0, rt);
6252
            GEN_STORE_TN_SRSREG(rd, T0);
6253
            break;
B
bellard 已提交
6254
        default:
6255
            MIPS_INVAL("cp0");
6256
            generate_exception(ctx, EXCP_RI);
B
bellard 已提交
6257 6258 6259
            break;
        }
        break;
6260
    case OPC_ADDI ... OPC_LUI: /* Arithmetic with immediate opcode */
6261
         gen_arith_imm(env, ctx, op, rt, rs, imm);
6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278
         break;
    case OPC_J ... OPC_JAL: /* Jump */
         offset = (int32_t)(ctx->opcode & 0x3FFFFFF) << 2;
         gen_compute_branch(ctx, op, rs, rt, offset);
         return;
    case OPC_BEQ ... OPC_BGTZ: /* Branch */
    case OPC_BEQL ... OPC_BGTZL:
         gen_compute_branch(ctx, op, rs, rt, imm << 2);
         return;
    case OPC_LB ... OPC_LWR: /* Load and stores */
    case OPC_SB ... OPC_SW:
    case OPC_SWR:
    case OPC_LL:
    case OPC_SC:
         gen_ldst(ctx, op, rt, rs, imm);
         break;
    case OPC_CACHE:
6279
        check_insn(env, ctx, ISA_MIPS3 | ISA_MIPS32);
6280
        /* Treat as NOP. */
6281
        break;
6282
    case OPC_PREF:
6283
        check_insn(env, ctx, ISA_MIPS4 | ISA_MIPS32);
6284
        /* Treat as NOP. */
B
bellard 已提交
6285
        break;
B
bellard 已提交
6286

6287
    /* Floating point (COP1). */
6288 6289 6290 6291
    case OPC_LWC1:
    case OPC_LDC1:
    case OPC_SWC1:
    case OPC_SDC1:
6292 6293
        if (env->CP0_Config1 & (1 << CP0C1_FP)) {
            save_cpu_state(ctx, 1);
6294
            check_cp1_enabled(ctx);
6295 6296 6297 6298
            gen_flt_ldst(ctx, op, rt, rs, imm);
        } else {
            generate_exception_err(ctx, EXCP_CpU, 1);
        }
B
bellard 已提交
6299 6300
        break;

6301
    case OPC_CP1:
6302 6303
        if (env->CP0_Config1 & (1 << CP0C1_FP)) {
            save_cpu_state(ctx, 1);
6304
            check_cp1_enabled(ctx);
6305 6306
            op1 = MASK_CP1(ctx->opcode);
            switch (op1) {
6307 6308
            case OPC_MFHC1:
            case OPC_MTHC1:
6309
                check_insn(env, ctx, ISA_MIPS32R2);
6310 6311 6312 6313
            case OPC_MFC1:
            case OPC_CFC1:
            case OPC_MTC1:
            case OPC_CTC1:
6314 6315
                gen_cp1(ctx, op1, rt, rd);
                break;
6316
#if defined(TARGET_MIPS64)
6317 6318
            case OPC_DMFC1:
            case OPC_DMTC1:
6319
                check_insn(env, ctx, ISA_MIPS3);
6320 6321
                gen_cp1(ctx, op1, rt, rd);
                break;
6322
#endif
6323 6324
            case OPC_BC1ANY2:
            case OPC_BC1ANY4:
6325
                check_insn(env, ctx, ASE_MIPS3D);
6326 6327
                /* fall through */
            case OPC_BC1:
6328
                gen_compute_branch1(env, ctx, MASK_BC1(ctx->opcode),
6329
                                    (rt >> 2) & 0x7, imm << 2);
6330 6331 6332 6333 6334
                return;
            case OPC_S_FMT:
            case OPC_D_FMT:
            case OPC_W_FMT:
            case OPC_L_FMT:
6335 6336 6337
            case OPC_PS_FMT:
                gen_farith(ctx, MASK_CP1_FUNC(ctx->opcode), rt, rd, sa,
                           (imm >> 8) & 0x7);
6338 6339
                break;
            default:
6340
                MIPS_INVAL("cp1");
6341
                generate_exception (ctx, EXCP_RI);
6342 6343 6344 6345
                break;
            }
        } else {
            generate_exception_err(ctx, EXCP_CpU, 1);
B
bellard 已提交
6346
        }
B
bellard 已提交
6347 6348 6349
        break;

    /* COP2.  */
6350 6351 6352 6353 6354 6355
    case OPC_LWC2:
    case OPC_LDC2:
    case OPC_SWC2:
    case OPC_SDC2:
    case OPC_CP2:
        /* COP2: Not implemented. */
B
bellard 已提交
6356 6357 6358
        generate_exception_err(ctx, EXCP_CpU, 2);
        break;

6359
    case OPC_CP3:
6360
        if (env->CP0_Config1 & (1 << CP0C1_FP)) {
6361
            save_cpu_state(ctx, 1);
6362
            check_cp1_enabled(ctx);
6363 6364
            op1 = MASK_CP3(ctx->opcode);
            switch (op1) {
6365 6366 6367 6368 6369 6370
            case OPC_LWXC1:
            case OPC_LDXC1:
            case OPC_LUXC1:
            case OPC_SWXC1:
            case OPC_SDXC1:
            case OPC_SUXC1:
T
ths 已提交
6371
                gen_flt3_ldst(ctx, op1, sa, rd, rs, rt);
6372
                break;
T
ths 已提交
6373
            case OPC_PREFX:
6374
                /* Treat as NOP. */
T
ths 已提交
6375
                break;
6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390
            case OPC_ALNV_PS:
            case OPC_MADD_S:
            case OPC_MADD_D:
            case OPC_MADD_PS:
            case OPC_MSUB_S:
            case OPC_MSUB_D:
            case OPC_MSUB_PS:
            case OPC_NMADD_S:
            case OPC_NMADD_D:
            case OPC_NMADD_PS:
            case OPC_NMSUB_S:
            case OPC_NMSUB_D:
            case OPC_NMSUB_PS:
                gen_flt3_arith(ctx, op1, sa, rs, rd, rt);
                break;
6391
            default:
6392
                MIPS_INVAL("cp3");
6393
                generate_exception (ctx, EXCP_RI);
6394 6395 6396
                break;
            }
        } else {
6397
            generate_exception_err(ctx, EXCP_CpU, 1);
6398
        }
B
bellard 已提交
6399 6400
        break;

6401
#if defined(TARGET_MIPS64)
6402 6403 6404 6405 6406 6407 6408 6409
    /* MIPS64 opcodes */
    case OPC_LWU:
    case OPC_LDL ... OPC_LDR:
    case OPC_SDL ... OPC_SDR:
    case OPC_LLD:
    case OPC_LD:
    case OPC_SCD:
    case OPC_SD:
6410 6411
        check_insn(env, ctx, ISA_MIPS3);
        check_mips_64(ctx);
6412 6413 6414
        gen_ldst(ctx, op, rt, rs, imm);
        break;
    case OPC_DADDI ... OPC_DADDIU:
6415 6416 6417
        check_insn(env, ctx, ISA_MIPS3);
        check_mips_64(ctx);
        gen_arith_imm(env, ctx, op, rt, rs, imm);
6418
        break;
B
bellard 已提交
6419
#endif
6420
    case OPC_JALX:
6421
        check_insn(env, ctx, ASE_MIPS16);
6422 6423
        /* MIPS16: Not implemented. */
    case OPC_MDMX:
6424
        check_insn(env, ctx, ASE_MDMX);
6425
        /* MDMX: Not implemented. */
B
bellard 已提交
6426
    default:            /* Invalid */
6427
        MIPS_INVAL("major opcode");
B
bellard 已提交
6428 6429 6430
        generate_exception(ctx, EXCP_RI);
        break;
    }
B
bellard 已提交
6431
    if (ctx->hflags & MIPS_HFLAG_BMASK) {
T
ths 已提交
6432
        int hflags = ctx->hflags & MIPS_HFLAG_BMASK;
B
bellard 已提交
6433
        /* Branches completion */
B
bellard 已提交
6434
        ctx->hflags &= ~MIPS_HFLAG_BMASK;
B
bellard 已提交
6435 6436
        ctx->bstate = BS_BRANCH;
        save_cpu_state(ctx, 0);
6437
        switch (hflags) {
B
bellard 已提交
6438 6439 6440
        case MIPS_HFLAG_B:
            /* unconditional branch */
            MIPS_DEBUG("unconditional branch");
6441
            gen_goto_tb(ctx, 0, ctx->btarget);
B
bellard 已提交
6442 6443 6444 6445
            break;
        case MIPS_HFLAG_BL:
            /* blikely taken case */
            MIPS_DEBUG("blikely branch taken");
6446
            gen_goto_tb(ctx, 0, ctx->btarget);
B
bellard 已提交
6447 6448 6449 6450
            break;
        case MIPS_HFLAG_BC:
            /* Conditional branch */
            MIPS_DEBUG("conditional branch");
B
bellard 已提交
6451 6452 6453 6454
            {
              int l1;
              l1 = gen_new_label();
              gen_op_jnz_T2(l1);
6455
              gen_goto_tb(ctx, 1, ctx->pc + 4);
6456 6457
              gen_set_label(l1);
              gen_goto_tb(ctx, 0, ctx->btarget);
B
bellard 已提交
6458
            }
B
bellard 已提交
6459 6460 6461 6462 6463
            break;
        case MIPS_HFLAG_BR:
            /* unconditional branch to register */
            MIPS_DEBUG("branch to register");
            gen_op_breg();
6464 6465
            gen_op_reset_T0();
            gen_op_exit_tb();
B
bellard 已提交
6466 6467 6468 6469 6470 6471 6472 6473
            break;
        default:
            MIPS_DEBUG("unknown branch");
            break;
        }
    }
}

6474
static always_inline int
6475 6476
gen_intermediate_code_internal (CPUState *env, TranslationBlock *tb,
                                int search_pc)
B
bellard 已提交
6477
{
T
ths 已提交
6478
    DisasContext ctx;
B
bellard 已提交
6479 6480 6481 6482
    target_ulong pc_start;
    uint16_t *gen_opc_end;
    int j, lj = -1;

B
bellard 已提交
6483
    if (search_pc && loglevel)
B
bellard 已提交
6484
        fprintf (logfile, "search pc %d\n", search_pc);
B
bellard 已提交
6485

B
bellard 已提交
6486 6487 6488 6489
    pc_start = tb->pc;
    gen_opc_ptr = gen_opc_buf;
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
    gen_opparam_ptr = gen_opparam_buf;
B
bellard 已提交
6490
    nb_gen_labels = 0;
B
bellard 已提交
6491
    ctx.pc = pc_start;
B
bellard 已提交
6492
    ctx.saved_pc = -1;
B
bellard 已提交
6493 6494
    ctx.tb = tb;
    ctx.bstate = BS_NONE;
B
bellard 已提交
6495
    /* Restore delay slot state from the tb context.  */
6496
    ctx.hflags = (uint32_t)tb->flags; /* FIXME: maybe use 64 bits here? */
6497
    restore_cpu_state(env, &ctx);
B
bellard 已提交
6498
#if defined(CONFIG_USER_ONLY)
T
ths 已提交
6499
    ctx.mem_idx = MIPS_HFLAG_UM;
B
bellard 已提交
6500
#else
T
ths 已提交
6501
    ctx.mem_idx = ctx.hflags & MIPS_HFLAG_KSU;
B
bellard 已提交
6502 6503 6504 6505
#endif
#ifdef DEBUG_DISAS
    if (loglevel & CPU_LOG_TB_CPU) {
        fprintf(logfile, "------------------------------------------------\n");
B
bellard 已提交
6506
        /* FIXME: This may print out stale hflags from env... */
B
bellard 已提交
6507 6508 6509
        cpu_dump_state(env, logfile, fprintf, 0);
    }
#endif
6510
#ifdef MIPS_DEBUG_DISAS
B
bellard 已提交
6511
    if (loglevel & CPU_LOG_TB_IN_ASM)
T
ths 已提交
6512
        fprintf(logfile, "\ntb %p idx %d hflags %04x\n",
B
bellard 已提交
6513
                tb, ctx.mem_idx, ctx.hflags);
B
bellard 已提交
6514 6515
#endif
    while (ctx.bstate == BS_NONE && gen_opc_ptr < gen_opc_end) {
B
bellard 已提交
6516 6517 6518
        if (env->nb_breakpoints > 0) {
            for(j = 0; j < env->nb_breakpoints; j++) {
                if (env->breakpoints[j] == ctx.pc) {
T
ths 已提交
6519
                    save_cpu_state(&ctx, 1);
B
bellard 已提交
6520 6521
                    ctx.bstate = BS_BRANCH;
                    gen_op_debug();
6522 6523 6524
                    /* Include the breakpoint location or the tb won't
                     * be flushed when it must be.  */
                    ctx.pc += 4;
B
bellard 已提交
6525 6526 6527 6528 6529
                    goto done_generating;
                }
            }
        }

B
bellard 已提交
6530 6531 6532 6533 6534 6535 6536
        if (search_pc) {
            j = gen_opc_ptr - gen_opc_buf;
            if (lj < j) {
                lj++;
                while (lj < j)
                    gen_opc_instr_start[lj++] = 0;
            }
B
bellard 已提交
6537 6538 6539
            gen_opc_pc[lj] = ctx.pc;
            gen_opc_hflags[lj] = ctx.hflags & MIPS_HFLAG_BMASK;
            gen_opc_instr_start[lj] = 1;
B
bellard 已提交
6540 6541
        }
        ctx.opcode = ldl_code(ctx.pc);
6542
        decode_opc(env, &ctx);
B
bellard 已提交
6543
        ctx.pc += 4;
B
bellard 已提交
6544 6545 6546 6547

        if (env->singlestep_enabled)
            break;

B
bellard 已提交
6548 6549
        if ((ctx.pc & (TARGET_PAGE_SIZE - 1)) == 0)
            break;
B
bellard 已提交
6550

B
bellard 已提交
6551 6552 6553 6554
#if defined (MIPS_SINGLE_STEP)
        break;
#endif
    }
B
bellard 已提交
6555
    if (env->singlestep_enabled) {
T
ths 已提交
6556
        save_cpu_state(&ctx, ctx.bstate == BS_NONE);
B
bellard 已提交
6557
        gen_op_debug();
T
ths 已提交
6558 6559 6560 6561
    } else {
	switch (ctx.bstate) {
        case BS_STOP:
            gen_op_interrupt_restart();
6562 6563
            gen_goto_tb(&ctx, 0, ctx.pc);
            break;
T
ths 已提交
6564
        case BS_NONE:
T
ths 已提交
6565
            save_cpu_state(&ctx, 0);
T
ths 已提交
6566 6567
            gen_goto_tb(&ctx, 0, ctx.pc);
            break;
6568 6569
        case BS_EXCP:
            gen_op_interrupt_restart();
T
ths 已提交
6570 6571
            gen_op_reset_T0();
            gen_op_exit_tb();
T
ths 已提交
6572
            break;
6573 6574 6575
        case BS_BRANCH:
        default:
            break;
T
ths 已提交
6576
	}
B
bellard 已提交
6577
    }
B
bellard 已提交
6578
done_generating:
B
bellard 已提交
6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594
    *gen_opc_ptr = INDEX_op_end;
    if (search_pc) {
        j = gen_opc_ptr - gen_opc_buf;
        lj++;
        while (lj <= j)
            gen_opc_instr_start[lj++] = 0;
    } else {
        tb->size = ctx.pc - pc_start;
    }
#ifdef DEBUG_DISAS
#if defined MIPS_DEBUG_DISAS
    if (loglevel & CPU_LOG_TB_IN_ASM)
        fprintf(logfile, "\n");
#endif
    if (loglevel & CPU_LOG_TB_IN_ASM) {
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
T
ths 已提交
6595
        target_disas(logfile, pc_start, ctx.pc - pc_start, 0);
B
bellard 已提交
6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606
        fprintf(logfile, "\n");
    }
    if (loglevel & CPU_LOG_TB_OP) {
        fprintf(logfile, "OP:\n");
        dump_ops(gen_opc_buf, gen_opparam_buf);
        fprintf(logfile, "\n");
    }
    if (loglevel & CPU_LOG_TB_CPU) {
        fprintf(logfile, "---------------- %d %08x\n", ctx.bstate, ctx.hflags);
    }
#endif
6607

B
bellard 已提交
6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620
    return 0;
}

int gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
{
    return gen_intermediate_code_internal(env, tb, 0);
}

int gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
{
    return gen_intermediate_code_internal(env, tb, 1);
}

6621
void fpu_dump_state(CPUState *env, FILE *f,
B
bellard 已提交
6622 6623 6624 6625
                    int (*fpu_fprintf)(FILE *f, const char *fmt, ...),
                    int flags)
{
    int i;
6626
    int is_fpu64 = !!(env->hflags & MIPS_HFLAG_F64);
6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641

#define printfpr(fp)                                                        \
    do {                                                                    \
        if (is_fpu64)                                                       \
            fpu_fprintf(f, "w:%08x d:%016lx fd:%13g fs:%13g psu: %13g\n",   \
                        (fp)->w[FP_ENDIAN_IDX], (fp)->d, (fp)->fd,          \
                        (fp)->fs[FP_ENDIAN_IDX], (fp)->fs[!FP_ENDIAN_IDX]); \
        else {                                                              \
            fpr_t tmp;                                                      \
            tmp.w[FP_ENDIAN_IDX] = (fp)->w[FP_ENDIAN_IDX];                  \
            tmp.w[!FP_ENDIAN_IDX] = ((fp) + 1)->w[FP_ENDIAN_IDX];           \
            fpu_fprintf(f, "w:%08x d:%016lx fd:%13g fs:%13g psu:%13g\n",    \
                        tmp.w[FP_ENDIAN_IDX], tmp.d, tmp.fd,                \
                        tmp.fs[FP_ENDIAN_IDX], tmp.fs[!FP_ENDIAN_IDX]);     \
        }                                                                   \
B
bellard 已提交
6642 6643
    } while(0)

6644 6645

    fpu_fprintf(f, "CP1 FCR0 0x%08x  FCR31 0x%08x  SR.FR %d  fp_status 0x%08x(0x%02x)\n",
6646 6647 6648 6649 6650
                env->fpu->fcr0, env->fpu->fcr31, is_fpu64, env->fpu->fp_status,
                get_float_exception_flags(&env->fpu->fp_status));
    fpu_fprintf(f, "FT0: "); printfpr(&env->fpu->ft0);
    fpu_fprintf(f, "FT1: "); printfpr(&env->fpu->ft1);
    fpu_fprintf(f, "FT2: "); printfpr(&env->fpu->ft2);
6651 6652
    for (i = 0; i < 32; (is_fpu64) ? i++ : (i += 2)) {
        fpu_fprintf(f, "%3s: ", fregnames[i]);
6653
        printfpr(&env->fpu->fpr[i]);
B
bellard 已提交
6654 6655 6656 6657 6658
    }

#undef printfpr
}

6659
void dump_fpu (CPUState *env)
B
bellard 已提交
6660
{
6661
    if (loglevel) {
T
ths 已提交
6662
       fprintf(logfile, "pc=0x" TARGET_FMT_lx " HI=0x" TARGET_FMT_lx " LO=0x" TARGET_FMT_lx " ds %04x " TARGET_FMT_lx " %d\n",
6663
               env->PC[env->current_tc], env->HI[0][env->current_tc], env->LO[0][env->current_tc], env->hflags, env->btarget, env->bcond);
B
bellard 已提交
6664 6665 6666 6667
       fpu_dump_state(env, logfile, fprintf, 0);
    }
}

6668
#if defined(TARGET_MIPS64) && defined(MIPS_DEBUG_SIGN_EXTENSIONS)
6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679
/* Debug help: The architecture requires 32bit code to maintain proper
   sign-extened values on 64bit machines.  */

#define SIGN_EXT_P(val) ((((val) & ~0x7fffffff) == 0) || (((val) & ~0x7fffffff) == ~0x7fffffff))

void cpu_mips_check_sign_extensions (CPUState *env, FILE *f,
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                     int flags)
{
    int i;

6680 6681 6682 6683 6684 6685
    if (!SIGN_EXT_P(env->PC[env->current_tc]))
        cpu_fprintf(f, "BROKEN: pc=0x" TARGET_FMT_lx "\n", env->PC[env->current_tc]);
    if (!SIGN_EXT_P(env->HI[env->current_tc]))
        cpu_fprintf(f, "BROKEN: HI=0x" TARGET_FMT_lx "\n", env->HI[env->current_tc]);
    if (!SIGN_EXT_P(env->LO[env->current_tc]))
        cpu_fprintf(f, "BROKEN: LO=0x" TARGET_FMT_lx "\n", env->LO[env->current_tc]);
6686
    if (!SIGN_EXT_P(env->btarget))
T
ths 已提交
6687
        cpu_fprintf(f, "BROKEN: btarget=0x" TARGET_FMT_lx "\n", env->btarget);
6688 6689

    for (i = 0; i < 32; i++) {
6690 6691
        if (!SIGN_EXT_P(env->gpr[i][env->current_tc]))
            cpu_fprintf(f, "BROKEN: %s=0x" TARGET_FMT_lx "\n", regnames[i], env->gpr[i][env->current_tc]);
6692 6693 6694
    }

    if (!SIGN_EXT_P(env->CP0_EPC))
T
ths 已提交
6695
        cpu_fprintf(f, "BROKEN: EPC=0x" TARGET_FMT_lx "\n", env->CP0_EPC);
6696
    if (!SIGN_EXT_P(env->CP0_LLAddr))
T
ths 已提交
6697
        cpu_fprintf(f, "BROKEN: LLAddr=0x" TARGET_FMT_lx "\n", env->CP0_LLAddr);
6698 6699 6700
}
#endif

6701
void cpu_dump_state (CPUState *env, FILE *f,
B
bellard 已提交
6702 6703 6704 6705
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                     int flags)
{
    int i;
6706

T
ths 已提交
6707
    cpu_fprintf(f, "pc=0x" TARGET_FMT_lx " HI=0x" TARGET_FMT_lx " LO=0x" TARGET_FMT_lx " ds %04x " TARGET_FMT_lx " %d\n",
6708
                env->PC[env->current_tc], env->HI[env->current_tc], env->LO[env->current_tc], env->hflags, env->btarget, env->bcond);
B
bellard 已提交
6709 6710 6711
    for (i = 0; i < 32; i++) {
        if ((i & 3) == 0)
            cpu_fprintf(f, "GPR%02d:", i);
6712
        cpu_fprintf(f, " %s " TARGET_FMT_lx, regnames[i], env->gpr[i][env->current_tc]);
B
bellard 已提交
6713 6714 6715
        if ((i & 3) == 3)
            cpu_fprintf(f, "\n");
    }
6716

T
ths 已提交
6717
    cpu_fprintf(f, "CP0 Status  0x%08x Cause   0x%08x EPC    0x" TARGET_FMT_lx "\n",
6718
                env->CP0_Status, env->CP0_Cause, env->CP0_EPC);
T
ths 已提交
6719
    cpu_fprintf(f, "    Config0 0x%08x Config1 0x%08x LLAddr 0x" TARGET_FMT_lx "\n",
B
bellard 已提交
6720
                env->CP0_Config0, env->CP0_Config1, env->CP0_LLAddr);
6721
    if (env->hflags & MIPS_HFLAG_FPU)
6722
        fpu_dump_state(env, f, cpu_fprintf, flags);
6723
#if defined(TARGET_MIPS64) && defined(MIPS_DEBUG_SIGN_EXTENSIONS)
6724 6725
    cpu_mips_check_sign_extensions(env, f, cpu_fprintf, flags);
#endif
B
bellard 已提交
6726 6727 6728 6729 6730 6731 6732 6733 6734
}

CPUMIPSState *cpu_mips_init (void)
{
    CPUMIPSState *env;

    env = qemu_mallocz(sizeof(CPUMIPSState));
    if (!env)
        return NULL;
B
bellard 已提交
6735
    cpu_exec_init(env);
6736 6737 6738 6739 6740 6741 6742 6743
    cpu_reset(env);
    return env;
}

void cpu_reset (CPUMIPSState *env)
{
    memset(env, 0, offsetof(CPUMIPSState, breakpoints));

B
bellard 已提交
6744
    tlb_flush(env, 1);
6745

B
bellard 已提交
6746
    /* Minimal init */
6747
#if !defined(CONFIG_USER_ONLY)
6748 6749 6750
    if (env->hflags & MIPS_HFLAG_BMASK) {
        /* If the exception was raised from a delay slot,
         * come back to the jump.  */
6751
        env->CP0_ErrorEPC = env->PC[env->current_tc] - 4;
6752
    } else {
6753
        env->CP0_ErrorEPC = env->PC[env->current_tc];
6754
    }
6755
    env->PC[env->current_tc] = (int32_t)0xBFC00000;
B
bellard 已提交
6756
    env->CP0_Wired = 0;
6757
    /* SMP not implemented */
6758
    env->CP0_EBase = 0x80000000;
6759
    env->CP0_Status = (1 << CP0St_BEV) | (1 << CP0St_ERL);
T
ths 已提交
6760 6761 6762
    /* vectored interrupts not implemented, timer on int 7,
       no performance counters. */
    env->CP0_IntCtl = 0xe0000000;
6763 6764 6765 6766 6767 6768 6769 6770 6771 6772
    {
        int i;

        for (i = 0; i < 7; i++) {
            env->CP0_WatchLo[i] = 0;
            env->CP0_WatchHi[i] = 0x80000000;
        }
        env->CP0_WatchLo[7] = 0;
        env->CP0_WatchHi[7] = 0;
    }
B
bellard 已提交
6773 6774
    /* Count register increments in debug mode, EJTAG version 1 */
    env->CP0_Debug = (1 << CP0DB_CNT) | (0x1 << CP0DB_VER);
6775
#endif
B
bellard 已提交
6776
    env->exception_index = EXCP_NONE;
6777
#if defined(CONFIG_USER_ONLY)
6778
    env->hflags = MIPS_HFLAG_UM;
6779
    env->user_mode_only = 1;
6780 6781
#else
    env->hflags = MIPS_HFLAG_CP0;
B
bellard 已提交
6782
#endif
B
bellard 已提交
6783
}
6784 6785

#include "translate_init.c"