translate.c 212.9 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
 *
 * 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"
32
#include "helper.h"
B
bellard 已提交
33
#include "tcg-op.h"
34
#include "qemu-common.h"
B
bellard 已提交
35

36
//#define MIPS_DEBUG_DISAS
37
//#define MIPS_DEBUG_SIGN_EXTENSIONS
B
bellard 已提交
38 39
//#define MIPS_SINGLE_STEP

40 41
/* MIPS major opcodes */
#define MASK_OP_MAJOR(op)  (op & (0x3F << 26))
42 43 44

enum {
    /* indirect opcode tables */
45 46 47 48 49 50 51 52
    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),
53
    /* arithmetic with immediate */
54 55 56 57 58 59 60 61 62 63
    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),
64
    /* Jump and branches */
65 66 67 68 69 70 71 72 73 74 75
    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 */
76
    /* Load and stores */
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    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),
100
    /* Floating point load/store */
101 102 103 104 105 106 107 108 109 110
    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),
111
    /* Cache and prefetch */
112 113 114 115
    OPC_CACHE    = (0x2F << 26),
    OPC_PREF     = (0x33 << 26),
    /* Reserved major opcode */
    OPC_MAJOR3B_RESERVED = (0x3B << 26),
116 117 118
};

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

121 122
enum {
    /* Shifts */
123
    OPC_SLL      = 0x00 | OPC_SPECIAL,
124 125
    /* NOP is SLL r0, r0, 0   */
    /* SSNOP is SLL r0, r0, 1 */
126 127 128 129
    /* EHB is SLL r0, r0, 3 */
    OPC_SRL      = 0x02 | OPC_SPECIAL, /* also ROTR */
    OPC_SRA      = 0x03 | OPC_SPECIAL,
    OPC_SLLV     = 0x04 | OPC_SPECIAL,
130
    OPC_SRLV     = 0x06 | OPC_SPECIAL, /* also ROTRV */
131 132 133 134 135 136 137 138 139 140
    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,
141
    /* Multiplication / division */
142 143 144 145 146 147 148 149
    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,
150
    /* 2 registers arithmetic / logic */
151 152 153 154 155 156 157 158 159 160 161 162 163 164
    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,
165
    /* Jumps */
166 167
    OPC_JR       = 0x08 | OPC_SPECIAL, /* Also JR.HB */
    OPC_JALR     = 0x09 | OPC_SPECIAL, /* Also JALR.HB */
168
    /* Traps */
169 170 171 172 173 174
    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,
175
    /* HI / LO registers load & stores */
176 177 178 179
    OPC_MFHI     = 0x10 | OPC_SPECIAL,
    OPC_MTHI     = 0x11 | OPC_SPECIAL,
    OPC_MFLO     = 0x12 | OPC_SPECIAL,
    OPC_MTLO     = 0x13 | OPC_SPECIAL,
180
    /* Conditional moves */
181 182
    OPC_MOVZ     = 0x0A | OPC_SPECIAL,
    OPC_MOVN     = 0x0B | OPC_SPECIAL,
183

184
    OPC_MOVCI    = 0x01 | OPC_SPECIAL,
185 186

    /* Special */
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    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,
};

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
/* Multiplication variants of the vr54xx. */
#define MASK_MUL_VR54XX(op)   MASK_SPECIAL(op) | (op & (0x1F << 6))

enum {
    OPC_VR54XX_MULS    = (0x03 << 6) | OPC_MULT,
    OPC_VR54XX_MULSU   = (0x03 << 6) | OPC_MULTU,
    OPC_VR54XX_MACC    = (0x05 << 6) | OPC_MULT,
    OPC_VR54XX_MACCU   = (0x05 << 6) | OPC_MULTU,
    OPC_VR54XX_MSAC    = (0x07 << 6) | OPC_MULT,
    OPC_VR54XX_MSACU   = (0x07 << 6) | OPC_MULTU,
    OPC_VR54XX_MULHI   = (0x09 << 6) | OPC_MULT,
    OPC_VR54XX_MULHIU  = (0x09 << 6) | OPC_MULTU,
    OPC_VR54XX_MULSHI  = (0x0B << 6) | OPC_MULT,
    OPC_VR54XX_MULSHIU = (0x0B << 6) | OPC_MULTU,
    OPC_VR54XX_MACCHI  = (0x0D << 6) | OPC_MULT,
    OPC_VR54XX_MACCHIU = (0x0D << 6) | OPC_MULTU,
    OPC_VR54XX_MSACHI  = (0x0F << 6) | OPC_MULT,
    OPC_VR54XX_MSACHIU = (0x0F << 6) | OPC_MULTU,
};

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
/* 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,
241 242
};

243 244 245
/* Special2 opcodes */
#define MASK_SPECIAL2(op)  MASK_OP_MAJOR(op) | (op & 0x3F)

246
enum {
247 248 249 250 251 252
    /* 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,
253
    /* Misc */
254 255 256 257
    OPC_CLZ      = 0x20 | OPC_SPECIAL2,
    OPC_CLO      = 0x21 | OPC_SPECIAL2,
    OPC_DCLZ     = 0x24 | OPC_SPECIAL2,
    OPC_DCLO     = 0x25 | OPC_SPECIAL2,
258
    /* Special */
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    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,
274 275
    OPC_FORK     = 0x08 | OPC_SPECIAL3,
    OPC_YIELD    = 0x09 | OPC_SPECIAL3,
276 277 278
    OPC_BSHFL    = 0x20 | OPC_SPECIAL3,
    OPC_DBSHFL   = 0x24 | OPC_SPECIAL3,
    OPC_RDHWR    = 0x3B | OPC_SPECIAL3,
279 280
};

281 282 283
/* BSHFL opcodes */
#define MASK_BSHFL(op)     MASK_SPECIAL3(op) | (op & (0x1F << 6))

284
enum {
285 286 287
    OPC_WSBH     = (0x02 << 6) | OPC_BSHFL,
    OPC_SEB      = (0x10 << 6) | OPC_BSHFL,
    OPC_SEH      = (0x18 << 6) | OPC_BSHFL,
288 289
};

290 291 292
/* DBSHFL opcodes */
#define MASK_DBSHFL(op)    MASK_SPECIAL3(op) | (op & (0x1F << 6))

293
enum {
294 295
    OPC_DSBH     = (0x02 << 6) | OPC_DBSHFL,
    OPC_DSHD     = (0x05 << 6) | OPC_DBSHFL,
296 297
};

298 299 300
/* Coprocessor 0 (rs field) */
#define MASK_CP0(op)       MASK_OP_MAJOR(op) | (op & (0x1F << 21))

B
bellard 已提交
301
enum {
302 303 304 305
    OPC_MFC0     = (0x00 << 21) | OPC_CP0,
    OPC_DMFC0    = (0x01 << 21) | OPC_CP0,
    OPC_MTC0     = (0x04 << 21) | OPC_CP0,
    OPC_DMTC0    = (0x05 << 21) | OPC_CP0,
306
    OPC_MFTR     = (0x08 << 21) | OPC_CP0,
307 308
    OPC_RDPGPR   = (0x0A << 21) | OPC_CP0,
    OPC_MFMC0    = (0x0B << 21) | OPC_CP0,
309
    OPC_MTTR     = (0x0C << 21) | OPC_CP0,
310 311 312 313
    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 已提交
314
};
315 316

/* MFMC0 opcodes */
317
#define MASK_MFMC0(op)     MASK_CP0(op) | (op & 0xFFFF)
318 319

enum {
320 321 322 323
    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,
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
    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,
349
    OPC_MFHC1    = (0x03 << 21) | OPC_CP1,
350 351 352
    OPC_MTC1     = (0x04 << 21) | OPC_CP1,
    OPC_DMTC1    = (0x05 << 21) | OPC_CP1,
    OPC_CTC1     = (0x06 << 21) | OPC_CP1,
353
    OPC_MTHC1    = (0x07 << 21) | OPC_CP1,
354
    OPC_BC1      = (0x08 << 21) | OPC_CP1, /* bc */
355 356
    OPC_BC1ANY2  = (0x09 << 21) | OPC_CP1,
    OPC_BC1ANY4  = (0x0A << 21) | OPC_CP1,
357 358 359 360 361 362
    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 */
363
    OPC_PS_FMT   = (0x16 << 21) | OPC_CP1, /* 22: fmt=paired single fp */
364 365
};

366 367 368
#define MASK_CP1_FUNC(op)       MASK_CP1(op) | (op & 0x3F)
#define MASK_BC1(op)            MASK_CP1(op) | (op & (0x3 << 16))

369 370 371 372 373 374 375
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,
};

376 377 378 379 380 381 382 383 384
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,
};
385 386

#define MASK_CP2(op)       MASK_OP_MAJOR(op) | (op & (0x1F << 21))
T
ths 已提交
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 413 414 415 416 417

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,
418
    OPC_NMADD_D = 0x31 | OPC_CP3,
T
ths 已提交
419 420 421 422 423 424
    OPC_NMADD_PS= 0x36 | OPC_CP3,
    OPC_NMSUB_S = 0x38 | OPC_CP3,
    OPC_NMSUB_D = 0x39 | OPC_CP3,
    OPC_NMSUB_PS= 0x3E | OPC_CP3,
};

425
/* global register indices */
T
ths 已提交
426
static TCGv cpu_env, current_tc_gprs, cpu_T[2];
B
bellard 已提交
427

T
ths 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
/* The code generator doesn't like lots of temporaries, so maintain our own
   cache for reuse within a function.  */
#define MAX_TEMPS 4
static int num_temps;
static TCGv temps[MAX_TEMPS];

/* Allocate a temporary variable.  */
static TCGv new_tmp(void)
{
    TCGv tmp;
    if (num_temps == MAX_TEMPS)
        abort();

    if (GET_TCGV(temps[num_temps]))
      return temps[num_temps++];

    tmp = tcg_temp_new(TCG_TYPE_I32);
    temps[num_temps++] = tmp;
    return tmp;
}

/* Release a temporary variable.  */
static void dead_tmp(TCGv tmp)
{
    int i;
    num_temps--;
    i = num_temps;
    if (GET_TCGV(temps[i]) == GET_TCGV(tmp))
        return;

    /* Shuffle this temp to the last slot.  */
    while (GET_TCGV(temps[i]) != GET_TCGV(tmp))
        i--;
    while (i < num_temps) {
        temps[i] = temps[i + 1];
        i++;
    }
    temps[i] = tmp;
}

468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
typedef struct DisasContext {
    struct TranslationBlock *tb;
    target_ulong pc, saved_pc;
    uint32_t opcode;
    uint32_t fp_status;
    /* 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 */
};

static const char *regnames[] =
B
bellard 已提交
490 491 492 493 494
    { "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", };

495 496 497 498 499
static const char *fregnames[] =
    { "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", };
T
ths 已提交
500

501 502 503 504 505 506 507 508 509 510 511
#ifdef MIPS_DEBUG_DISAS
#define MIPS_DEBUG(fmt, args...)                                              \
do {                                                                          \
    if (loglevel & CPU_LOG_TB_IN_ASM) {                                       \
        fprintf(logfile, TARGET_FMT_lx ": %08x " fmt "\n",                    \
                ctx->pc, ctx->opcode , ##args);                               \
    }                                                                         \
} while (0)
#else
#define MIPS_DEBUG(fmt, args...) do { } while(0)
#endif
T
ths 已提交
512

513 514 515 516 517
#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)
518

519 520
/* General purpose registers moves. */
static inline void gen_load_gpr (TCGv t, int reg)
521
{
522 523 524 525
    if (reg == 0)
        tcg_gen_movi_tl(t, 0);
    else
        tcg_gen_ld_tl(t, current_tc_gprs, sizeof(target_ulong) * reg);
526 527
}

528
static inline void gen_store_gpr (TCGv t, int reg)
529
{
530 531
    if (reg != 0)
        tcg_gen_st_tl(t, current_tc_gprs, sizeof(target_ulong) * reg);
532 533
}

534 535
/* Moves to/from shadow registers. */
static inline void gen_load_srsgpr (TCGv t, int reg)
536
{
537 538 539 540
    if (reg == 0)
        tcg_gen_movi_tl(t, 0);
    else {
        TCGv r_tmp = new_tmp();
541

542 543 544 545 546
        tcg_gen_ld_i32(r_tmp, cpu_env, offsetof(CPUState, CP0_SRSCtl));
        tcg_gen_shri_i32(r_tmp, r_tmp, CP0SRSCtl_PSS);
        tcg_gen_andi_i32(r_tmp, r_tmp, 0xf);
        tcg_gen_muli_i32(r_tmp, r_tmp, sizeof(target_ulong) * 32);
        tcg_gen_add_i32(r_tmp, cpu_env, r_tmp);
547

548 549 550
        tcg_gen_ld_tl(t, r_tmp, sizeof(target_ulong) * reg);
        dead_tmp(r_tmp);
    }
551 552
}

553
static inline void gen_store_srsgpr (TCGv t, int reg)
554
{
555 556
    if (reg != 0) {
        TCGv r_tmp = new_tmp();
557

558 559 560 561 562
        tcg_gen_ld_i32(r_tmp, cpu_env, offsetof(CPUState, CP0_SRSCtl));
        tcg_gen_shri_i32(r_tmp, r_tmp, CP0SRSCtl_PSS);
        tcg_gen_andi_i32(r_tmp, r_tmp, 0xf);
        tcg_gen_muli_i32(r_tmp, r_tmp, sizeof(target_ulong) * 32);
        tcg_gen_add_i32(r_tmp, cpu_env, r_tmp);
563

564 565 566
        tcg_gen_st_tl(t, r_tmp, sizeof(target_ulong) * reg);
        dead_tmp(r_tmp);
    }
567 568 569
}

/* Floating point register moves. */
570 571 572 573 574 575 576 577 578 579 580
#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,  \
};                                               \
581
static always_inline void func(int n)            \
582 583
{                                                \
    NAME ## _table[n]();                         \
B
bellard 已提交
584 585
}

586 587 588 589 590
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 已提交
591

592 593
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 已提交
594

595 596
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 已提交
597

598 599
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 已提交
600

601 602
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 已提交
603

604 605
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 已提交
606

607 608 609 610 611
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 已提交
612

613 614 615 616 617 618 619 620 621 622
#define GEN_LOAD_FREG_FTN(FTn, Fn)                                            \
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)

623
#define FOP_CONDS(type, fmt)                                            \
624
static GenOpFunc1 * gen_op_cmp ## type ## _ ## fmt ## _table[16] = {    \
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
    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 已提交
641
};                                                                      \
642
static always_inline void gen_cmp ## type ## _ ## fmt(int n, long cc)   \
B
bellard 已提交
643
{                                                                       \
644
    gen_op_cmp ## type ## _ ## fmt ## _table[n](cc);                    \
B
bellard 已提交
645 646
}

647 648 649 650 651 652
FOP_CONDS(, d)
FOP_CONDS(abs, d)
FOP_CONDS(, s)
FOP_CONDS(abs, s)
FOP_CONDS(, ps)
FOP_CONDS(abs, ps)
B
bellard 已提交
653

654 655 656 657 658 659 660 661
/* Tests */
#define OP_COND(name, cond)                                   \
void glue(gen_op_, name) (void)                               \
{                                                             \
    int l1 = gen_new_label();                                 \
    int l2 = gen_new_label();                                 \
                                                              \
    tcg_gen_brcond_tl(cond, cpu_T[0], cpu_T[1], l1);          \
662
    tcg_gen_movi_tl(cpu_T[0], 0);                             \
663 664
    tcg_gen_br(l2);                                           \
    gen_set_label(l1);                                        \
665
    tcg_gen_movi_tl(cpu_T[0], 1);                             \
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
    gen_set_label(l2);                                        \
}
OP_COND(eq, TCG_COND_EQ);
OP_COND(ne, TCG_COND_NE);
OP_COND(ge, TCG_COND_GE);
OP_COND(geu, TCG_COND_GEU);
OP_COND(lt, TCG_COND_LT);
OP_COND(ltu, TCG_COND_LTU);
#undef OP_COND

#define OP_CONDI(name, cond)                                  \
void glue(gen_op_, name) (target_ulong val)                   \
{                                                             \
    int l1 = gen_new_label();                                 \
    int l2 = gen_new_label();                                 \
                                                              \
    tcg_gen_brcond_tl(cond, cpu_T[0], tcg_const_tl(val), l1); \
683
    tcg_gen_movi_tl(cpu_T[0], 0);                             \
684 685
    tcg_gen_br(l2);                                           \
    gen_set_label(l1);                                        \
686
    tcg_gen_movi_tl(cpu_T[0], 1);                             \
687 688 689 690 691 692 693 694 695 696 697 698 699
    gen_set_label(l2);                                        \
}
OP_CONDI(lti, TCG_COND_LT);
OP_CONDI(ltiu, TCG_COND_LTU);
#undef OP_CONDI

#define OP_CONDZ(name, cond)                                  \
void glue(gen_op_, name) (void)                               \
{                                                             \
    int l1 = gen_new_label();                                 \
    int l2 = gen_new_label();                                 \
                                                              \
    tcg_gen_brcond_tl(cond, cpu_T[0], tcg_const_tl(0), l1);   \
700
    tcg_gen_movi_tl(cpu_T[0], 0);                             \
701 702
    tcg_gen_br(l2);                                           \
    gen_set_label(l1);                                        \
703
    tcg_gen_movi_tl(cpu_T[0], 1);                             \
704 705 706 707 708 709 710 711
    gen_set_label(l2);                                        \
}
OP_CONDZ(gez, TCG_COND_GE);
OP_CONDZ(gtz, TCG_COND_GT);
OP_CONDZ(lez, TCG_COND_LE);
OP_CONDZ(ltz, TCG_COND_LT);
#undef OP_CONDZ

712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
static inline void gen_save_pc(target_ulong pc)
{
    TCGv r_tmp = tcg_temp_new(TCG_TYPE_TL);
    TCGv r_tc_off = new_tmp();
    TCGv r_tc_off_tl = tcg_temp_new(TCG_TYPE_TL);
    TCGv r_ptr = tcg_temp_new(TCG_TYPE_PTR);

    tcg_gen_movi_tl(r_tmp, pc);
    tcg_gen_ld_i32(r_tc_off, cpu_env, offsetof(CPUState, current_tc));
    tcg_gen_muli_i32(r_tc_off, r_tc_off, sizeof(target_ulong));
    tcg_gen_ext_i32_ptr(r_tc_off_tl, r_tc_off);
    tcg_gen_add_ptr(r_ptr, cpu_env, r_tc_off_tl);
    tcg_gen_st_tl(r_tmp, r_ptr, offsetof(CPUState, PC));
    dead_tmp(r_tc_off);
}
727

728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
static inline void gen_breg_pc(void)
{
    TCGv r_tmp = tcg_temp_new(TCG_TYPE_TL);
    TCGv r_tc_off = new_tmp();
    TCGv r_tc_off_tl = tcg_temp_new(TCG_TYPE_TL);
    TCGv r_ptr = tcg_temp_new(TCG_TYPE_PTR);

    tcg_gen_ld_tl(r_tmp, cpu_env, offsetof(CPUState, btarget));
    tcg_gen_ld_i32(r_tc_off, cpu_env, offsetof(CPUState, current_tc));
    tcg_gen_muli_i32(r_tc_off, r_tc_off, sizeof(target_ulong));
    tcg_gen_ext_i32_ptr(r_tc_off_tl, r_tc_off);
    tcg_gen_add_ptr(r_ptr, cpu_env, r_tc_off_tl);
    tcg_gen_st_tl(r_tmp, r_ptr, offsetof(CPUState, PC));
    dead_tmp(r_tc_off);
}
B
bellard 已提交
743

744
static inline void gen_save_btarget(target_ulong btarget)
745
{
746 747 748 749
    TCGv r_tmp = tcg_temp_new(TCG_TYPE_TL);

    tcg_gen_movi_tl(r_tmp, btarget);
    tcg_gen_st_tl(r_tmp, cpu_env, offsetof(CPUState, btarget));
750 751
}

752
static always_inline void gen_save_breg_target(int reg)
753
{
754 755 756 757
    TCGv r_tmp = tcg_temp_new(TCG_TYPE_TL);

    gen_load_gpr(r_tmp, reg);
    tcg_gen_st_tl(r_tmp, cpu_env, offsetof(CPUState, btarget));
758 759
}

760
static always_inline void save_cpu_state (DisasContext *ctx, int do_save_pc)
B
bellard 已提交
761 762 763 764 765 766 767 768
{
#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) {
769
        gen_save_pc(ctx->pc);
B
bellard 已提交
770 771 772 773 774
        ctx->saved_pc = ctx->pc;
    }
    if (ctx->hflags != ctx->saved_hflags) {
        gen_op_save_state(ctx->hflags);
        ctx->saved_hflags = ctx->hflags;
775 776 777 778 779 780
        switch (ctx->hflags & MIPS_HFLAG_BMASK) {
        case MIPS_HFLAG_BR:
            break;
        case MIPS_HFLAG_BC:
        case MIPS_HFLAG_BL:
        case MIPS_HFLAG_B:
781
            gen_save_btarget(ctx->btarget);
782
            break;
B
bellard 已提交
783 784 785 786
        }
    }
}

787
static always_inline void restore_cpu_state (CPUState *env, DisasContext *ctx)
788
{
789 790 791 792 793 794
    ctx->saved_hflags = ctx->hflags;
    switch (ctx->hflags & MIPS_HFLAG_BMASK) {
    case MIPS_HFLAG_BR:
        break;
    case MIPS_HFLAG_BC:
    case MIPS_HFLAG_BL:
795
    case MIPS_HFLAG_B:
796 797
        ctx->btarget = env->btarget;
        break;
798 799 800
    }
}

801
static always_inline void
802
generate_exception_err (DisasContext *ctx, int excp, int err)
803 804
{
    save_cpu_state(ctx, 1);
805 806
    tcg_gen_helper_0_2(do_raise_exception_err, tcg_const_i32(excp), tcg_const_i32(err));
    tcg_gen_helper_0_0(do_interrupt_restart);
807 808 809 810
    tcg_gen_exit_tb(0);
}

static always_inline void
811
generate_exception (DisasContext *ctx, int excp)
812
{
B
bellard 已提交
813
    save_cpu_state(ctx, 1);
814 815 816
    tcg_gen_helper_0_1(do_raise_exception, tcg_const_i32(excp));
    tcg_gen_helper_0_0(do_interrupt_restart);
    tcg_gen_exit_tb(0);
B
bellard 已提交
817 818
}

819 820
/* Addresses computation */
static inline void gen_op_addr_add (void)
B
bellard 已提交
821
{
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);

#if defined(TARGET_MIPS64)
    /* For compatibility with 32-bit code, data reference in user mode
       with Status_UX = 0 should be casted to 32-bit and sign extended.
       See the MIPS64 PRA manual, section 4.10. */
    {
        TCGv r_tmp = new_tmp();
        int l1 = gen_new_label();

        tcg_gen_ld_i32(r_tmp, cpu_env, offsetof(CPUState, hflags));
        tcg_gen_andi_i32(r_tmp, r_tmp, MIPS_HFLAG_KSU);
        tcg_gen_brcond_i32(TCG_COND_NE, r_tmp, tcg_const_i32(MIPS_HFLAG_UM), l1);
        tcg_gen_ld_i32(r_tmp, cpu_env, offsetof(CPUState, CP0_Status));
        tcg_gen_andi_i32(r_tmp, r_tmp, (1 << CP0St_UX));
        tcg_gen_brcond_i32(TCG_COND_NE, r_tmp, tcg_const_i32(0), l1);
        tcg_gen_ext32s_i64(cpu_T[0], cpu_T[0]);
        gen_set_label(l1);
        dead_tmp(r_tmp);
    }
#endif
B
bellard 已提交
843 844
}

845
static always_inline void check_cp0_enabled(DisasContext *ctx)
846
{
847
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_CP0)))
848 849 850
        generate_exception_err(ctx, EXCP_CpU, 1);
}

851
static always_inline void check_cp1_enabled(DisasContext *ctx)
852
{
853
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_FPU)))
854 855 856
        generate_exception_err(ctx, EXCP_CpU, 1);
}

857 858 859 860 861 862 863 864 865 866 867 868 869
/* Verify that the processor is running with COP1X instructions enabled.
   This is associated with the nabla symbol in the MIPS32 and MIPS64
   opcode tables.  */

static always_inline void check_cop1x(DisasContext *ctx)
{
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_COP1X)))
        generate_exception(ctx, EXCP_RI);
}

/* Verify that the processor is running with 64-bit floating-point
   operations enabled.  */

870
static always_inline void check_cp1_64bitmode(DisasContext *ctx)
871
{
872
    if (unlikely(~ctx->hflags & (MIPS_HFLAG_F64 | MIPS_HFLAG_COP1X)))
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
        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)
{
889
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_F64) && (regs & 1)))
890 891 892
        generate_exception(ctx, EXCP_RI);
}

893
/* This code generates a "reserved instruction" exception if the
894
   CPU does not support the instruction set corresponding to flags. */
895
static always_inline void check_insn(CPUState *env, DisasContext *ctx, int flags)
896
{
897
    if (unlikely(!(env->insn_flags & flags)))
898 899 900
        generate_exception(ctx, EXCP_RI);
}

901 902
/* This code generates a "reserved instruction" exception if 64-bit
   instructions are not enabled. */
903
static always_inline void check_mips_64(DisasContext *ctx)
904
{
905
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_64)))
906 907 908
        generate_exception(ctx, EXCP_RI);
}

T
ths 已提交
909
/* load/store instructions. */
B
bellard 已提交
910 911 912 913 914 915 916 917 918
#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 已提交
919 920
    &gen_op_l##width##_super,                                                 \
    &gen_op_l##width##_user,                                                  \
B
bellard 已提交
921 922 923 924
}
#define OP_ST_TABLE(width)                                                    \
static GenOpFunc *gen_op_s##width[] = {                                       \
    &gen_op_s##width##_kernel,                                                \
T
ths 已提交
925 926
    &gen_op_s##width##_super,                                                 \
    &gen_op_s##width##_user,                                                  \
B
bellard 已提交
927 928 929
}
#endif

930
#if defined(TARGET_MIPS64)
B
bellard 已提交
931 932 933 934 935 936 937 938 939
OP_LD_TABLE(dl);
OP_LD_TABLE(dr);
OP_ST_TABLE(dl);
OP_ST_TABLE(dr);
#endif
OP_LD_TABLE(wl);
OP_LD_TABLE(wr);
OP_ST_TABLE(wl);
OP_ST_TABLE(wr);
B
bellard 已提交
940 941 942 943
OP_LD_TABLE(wc1);
OP_ST_TABLE(wc1);
OP_LD_TABLE(dc1);
OP_ST_TABLE(dc1);
944 945
OP_LD_TABLE(uxc1);
OP_ST_TABLE(uxc1);
B
bellard 已提交
946

947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
#define OP_LD(insn,fname)                                        \
void inline op_ldst_##insn(DisasContext *ctx)                    \
{                                                                \
    tcg_gen_qemu_##fname(cpu_T[0], cpu_T[0], ctx->mem_idx);      \
}
OP_LD(lb,ld8s);
OP_LD(lbu,ld8u);
OP_LD(lh,ld16s);
OP_LD(lhu,ld16u);
OP_LD(lw,ld32s);
#if defined(TARGET_MIPS64)
OP_LD(lwu,ld32u);
OP_LD(ld,ld64);
#endif
#undef OP_LD

#define OP_ST(insn,fname)                                        \
void inline op_ldst_##insn(DisasContext *ctx)                    \
{                                                                \
    tcg_gen_qemu_##fname(cpu_T[1], cpu_T[0], ctx->mem_idx);      \
}
OP_ST(sb,st8);
OP_ST(sh,st16);
OP_ST(sw,st32);
#if defined(TARGET_MIPS64)
OP_ST(sd,st64);
#endif
#undef OP_ST

#define OP_LD_ATOMIC(insn,fname)                                        \
void inline op_ldst_##insn(DisasContext *ctx)                           \
{                                                                       \
    tcg_gen_mov_tl(cpu_T[1], cpu_T[0]);                                 \
    tcg_gen_qemu_##fname(cpu_T[0], cpu_T[0], ctx->mem_idx);             \
    tcg_gen_st_tl(cpu_T[1], cpu_env, offsetof(CPUState, CP0_LLAddr));   \
}
OP_LD_ATOMIC(ll,ld32s);
#if defined(TARGET_MIPS64)
OP_LD_ATOMIC(lld,ld64);
#endif
#undef OP_LD_ATOMIC

#define OP_ST_ATOMIC(insn,fname,almask)                                 \
void inline op_ldst_##insn(DisasContext *ctx)                           \
{                                                                       \
T
ths 已提交
992
    TCGv r_tmp = tcg_temp_new(TCG_TYPE_TL);                             \
993 994 995 996 997 998 999
    int l1 = gen_new_label();                                           \
    int l2 = gen_new_label();                                           \
    int l3 = gen_new_label();                                           \
                                                                        \
    tcg_gen_andi_tl(r_tmp, cpu_T[0], almask);                           \
    tcg_gen_brcond_tl(TCG_COND_EQ, r_tmp, tcg_const_tl(0), l1);         \
    tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_BadVAddr)); \
1000
    generate_exception(ctx, EXCP_AdES);                             \
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
    gen_set_label(l1);                                                  \
    tcg_gen_ld_tl(r_tmp, cpu_env, offsetof(CPUState, CP0_LLAddr));      \
    tcg_gen_brcond_tl(TCG_COND_NE, cpu_T[0], r_tmp, l2);                \
    tcg_gen_qemu_##fname(cpu_T[1], cpu_T[0], ctx->mem_idx);             \
    tcg_gen_movi_tl(cpu_T[0], 1);                                       \
    tcg_gen_br(l3);                                                     \
    gen_set_label(l2);                                                  \
    tcg_gen_movi_tl(cpu_T[0], 0);                                       \
    gen_set_label(l3);                                                  \
}
OP_ST_ATOMIC(sc,st32,0x3);
#if defined(TARGET_MIPS64)
OP_ST_ATOMIC(scd,st64,0x7);
#endif
#undef OP_ST_ATOMIC

void inline op_ldst_lwc1(DisasContext *ctx)
{
    op_ldst(lwc1);
}

void inline op_ldst_ldc1(DisasContext *ctx)
{
    op_ldst(ldc1);
}

void inline op_ldst_swc1(DisasContext *ctx)
{
    op_ldst(swc1);
}

void inline op_ldst_sdc1(DisasContext *ctx)
{
    op_ldst(sdc1);
}

B
bellard 已提交
1037
/* Load and store */
1038
static void gen_ldst (DisasContext *ctx, uint32_t opc, int rt,
B
bellard 已提交
1039 1040
                      int base, int16_t offset)
{
1041
    const char *opn = "ldst";
B
bellard 已提交
1042 1043

    if (base == 0) {
1044
        tcg_gen_movi_tl(cpu_T[0], offset);
B
bellard 已提交
1045
    } else if (offset == 0) {
1046
        gen_load_gpr(cpu_T[0], base);
B
bellard 已提交
1047
    } else {
1048 1049
        gen_load_gpr(cpu_T[0], base);
        tcg_gen_movi_tl(cpu_T[1], offset);
1050
        gen_op_addr_add();
B
bellard 已提交
1051 1052
    }
    /* Don't do NOP if destination is zero: we must perform the actual
1053
       memory access. */
B
bellard 已提交
1054
    switch (opc) {
1055
#if defined(TARGET_MIPS64)
1056
    case OPC_LWU:
1057
        op_ldst_lwu(ctx);
1058
        gen_store_gpr(cpu_T[0], rt);
1059 1060
        opn = "lwu";
        break;
B
bellard 已提交
1061
    case OPC_LD:
1062
        op_ldst_ld(ctx);
1063
        gen_store_gpr(cpu_T[0], rt);
B
bellard 已提交
1064 1065
        opn = "ld";
        break;
1066
    case OPC_LLD:
1067
        op_ldst_lld(ctx);
1068
        gen_store_gpr(cpu_T[0], rt);
1069 1070
        opn = "lld";
        break;
B
bellard 已提交
1071
    case OPC_SD:
1072
        gen_load_gpr(cpu_T[1], rt);
1073
        op_ldst_sd(ctx);
B
bellard 已提交
1074 1075
        opn = "sd";
        break;
1076
    case OPC_SCD:
T
ths 已提交
1077
        save_cpu_state(ctx, 1);
1078
        gen_load_gpr(cpu_T[1], rt);
1079
        op_ldst_scd(ctx);
1080
        gen_store_gpr(cpu_T[0], rt);
1081 1082
        opn = "scd";
        break;
B
bellard 已提交
1083
    case OPC_LDL:
1084
        gen_load_gpr(cpu_T[1], rt);
B
bellard 已提交
1085
        op_ldst(ldl);
1086
        gen_store_gpr(cpu_T[1], rt);
B
bellard 已提交
1087 1088 1089
        opn = "ldl";
        break;
    case OPC_SDL:
1090
        gen_load_gpr(cpu_T[1], rt);
B
bellard 已提交
1091 1092 1093 1094
        op_ldst(sdl);
        opn = "sdl";
        break;
    case OPC_LDR:
1095
        gen_load_gpr(cpu_T[1], rt);
B
bellard 已提交
1096
        op_ldst(ldr);
1097
        gen_store_gpr(cpu_T[1], rt);
B
bellard 已提交
1098 1099 1100
        opn = "ldr";
        break;
    case OPC_SDR:
1101
        gen_load_gpr(cpu_T[1], rt);
B
bellard 已提交
1102 1103 1104 1105 1106
        op_ldst(sdr);
        opn = "sdr";
        break;
#endif
    case OPC_LW:
1107
        op_ldst_lw(ctx);
1108
        gen_store_gpr(cpu_T[0], rt);
B
bellard 已提交
1109 1110 1111
        opn = "lw";
        break;
    case OPC_SW:
1112
        gen_load_gpr(cpu_T[1], rt);
1113
        op_ldst_sw(ctx);
B
bellard 已提交
1114 1115 1116
        opn = "sw";
        break;
    case OPC_LH:
1117
        op_ldst_lh(ctx);
1118
        gen_store_gpr(cpu_T[0], rt);
B
bellard 已提交
1119 1120 1121
        opn = "lh";
        break;
    case OPC_SH:
1122
        gen_load_gpr(cpu_T[1], rt);
1123
        op_ldst_sh(ctx);
B
bellard 已提交
1124 1125 1126
        opn = "sh";
        break;
    case OPC_LHU:
1127
        op_ldst_lhu(ctx);
1128
        gen_store_gpr(cpu_T[0], rt);
B
bellard 已提交
1129 1130 1131
        opn = "lhu";
        break;
    case OPC_LB:
1132
        op_ldst_lb(ctx);
1133
        gen_store_gpr(cpu_T[0], rt);
B
bellard 已提交
1134 1135 1136
        opn = "lb";
        break;
    case OPC_SB:
1137
        gen_load_gpr(cpu_T[1], rt);
1138
        op_ldst_sb(ctx);
B
bellard 已提交
1139 1140 1141
        opn = "sb";
        break;
    case OPC_LBU:
1142
        op_ldst_lbu(ctx);
1143
        gen_store_gpr(cpu_T[0], rt);
B
bellard 已提交
1144 1145 1146
        opn = "lbu";
        break;
    case OPC_LWL:
1147
	gen_load_gpr(cpu_T[1], rt);
B
bellard 已提交
1148
        op_ldst(lwl);
1149
        gen_store_gpr(cpu_T[1], rt);
B
bellard 已提交
1150 1151 1152
        opn = "lwl";
        break;
    case OPC_SWL:
1153
        gen_load_gpr(cpu_T[1], rt);
B
bellard 已提交
1154 1155 1156 1157
        op_ldst(swl);
        opn = "swr";
        break;
    case OPC_LWR:
1158
	gen_load_gpr(cpu_T[1], rt);
B
bellard 已提交
1159
        op_ldst(lwr);
1160
        gen_store_gpr(cpu_T[1], rt);
B
bellard 已提交
1161 1162 1163
        opn = "lwr";
        break;
    case OPC_SWR:
1164
        gen_load_gpr(cpu_T[1], rt);
B
bellard 已提交
1165 1166 1167 1168
        op_ldst(swr);
        opn = "swr";
        break;
    case OPC_LL:
1169
        op_ldst_ll(ctx);
1170
        gen_store_gpr(cpu_T[0], rt);
B
bellard 已提交
1171 1172 1173
        opn = "ll";
        break;
    case OPC_SC:
T
ths 已提交
1174
        save_cpu_state(ctx, 1);
1175
        gen_load_gpr(cpu_T[1], rt);
1176
        op_ldst_sc(ctx);
1177
        gen_store_gpr(cpu_T[0], rt);
B
bellard 已提交
1178 1179 1180
        opn = "sc";
        break;
    default:
1181
        MIPS_INVAL(opn);
B
bellard 已提交
1182 1183 1184 1185 1186 1187
        generate_exception(ctx, EXCP_RI);
        return;
    }
    MIPS_DEBUG("%s %s, %d(%s)", opn, regnames[rt], offset, regnames[base]);
}

B
bellard 已提交
1188
/* Load and store */
1189
static void gen_flt_ldst (DisasContext *ctx, uint32_t opc, int ft,
B
bellard 已提交
1190 1191
                      int base, int16_t offset)
{
1192
    const char *opn = "flt_ldst";
B
bellard 已提交
1193 1194

    if (base == 0) {
1195
        tcg_gen_movi_tl(cpu_T[0], offset);
B
bellard 已提交
1196
    } else if (offset == 0) {
1197
        gen_load_gpr(cpu_T[0], base);
B
bellard 已提交
1198
    } else {
1199 1200
        gen_load_gpr(cpu_T[0], base);
        tcg_gen_movi_tl(cpu_T[1], offset);
1201
        gen_op_addr_add();
B
bellard 已提交
1202 1203
    }
    /* Don't do NOP if destination is zero: we must perform the actual
1204
       memory access. */
B
bellard 已提交
1205 1206
    switch (opc) {
    case OPC_LWC1:
1207
        op_ldst_lwc1(ctx);
B
bellard 已提交
1208 1209 1210 1211 1212
        GEN_STORE_FTN_FREG(ft, WT0);
        opn = "lwc1";
        break;
    case OPC_SWC1:
        GEN_LOAD_FREG_FTN(WT0, ft);
1213
        op_ldst_swc1(ctx);
B
bellard 已提交
1214 1215 1216
        opn = "swc1";
        break;
    case OPC_LDC1:
1217
        op_ldst_ldc1(ctx);
B
bellard 已提交
1218 1219 1220 1221 1222
        GEN_STORE_FTN_FREG(ft, DT0);
        opn = "ldc1";
        break;
    case OPC_SDC1:
        GEN_LOAD_FREG_FTN(DT0, ft);
1223
        op_ldst_sdc1(ctx);
B
bellard 已提交
1224 1225 1226
        opn = "sdc1";
        break;
    default:
1227
        MIPS_INVAL(opn);
1228
        generate_exception(ctx, EXCP_RI);
B
bellard 已提交
1229 1230 1231 1232 1233
        return;
    }
    MIPS_DEBUG("%s %s, %d(%s)", opn, fregnames[ft], offset, regnames[base]);
}

B
bellard 已提交
1234
/* Arithmetic with immediate operand */
1235 1236
static void gen_arith_imm (CPUState *env, DisasContext *ctx, uint32_t opc,
                           int rt, int rs, int16_t imm)
B
bellard 已提交
1237
{
1238
    target_ulong uimm;
1239
    const char *opn = "imm arith";
B
bellard 已提交
1240

1241
    if (rt == 0 && opc != OPC_ADDI && opc != OPC_DADDI) {
1242 1243
        /* If no destination, treat it as a NOP.
           For addi, we must generate the overflow exception when needed. */
B
bellard 已提交
1244 1245 1246
        MIPS_DEBUG("NOP");
        return;
    }
1247 1248 1249 1250
    uimm = (uint16_t)imm;
    switch (opc) {
    case OPC_ADDI:
    case OPC_ADDIU:
1251
#if defined(TARGET_MIPS64)
1252 1253 1254 1255 1256
    case OPC_DADDI:
    case OPC_DADDIU:
#endif
    case OPC_SLTI:
    case OPC_SLTIU:
1257
        uimm = (target_long)imm; /* Sign extend to 32/64 bits */
1258
        tcg_gen_movi_tl(cpu_T[1], uimm);
1259 1260 1261 1262
        /* Fall through. */
    case OPC_ANDI:
    case OPC_ORI:
    case OPC_XORI:
1263
        gen_load_gpr(cpu_T[0], rs);
1264 1265
        break;
    case OPC_LUI:
1266
        tcg_gen_movi_tl(cpu_T[0], imm << 16);
1267 1268 1269 1270
        break;
    case OPC_SLL:
    case OPC_SRA:
    case OPC_SRL:
1271
#if defined(TARGET_MIPS64)
1272 1273 1274 1275 1276 1277 1278 1279
    case OPC_DSLL:
    case OPC_DSRA:
    case OPC_DSRL:
    case OPC_DSLL32:
    case OPC_DSRA32:
    case OPC_DSRL32:
#endif
        uimm &= 0x1f;
1280
        gen_load_gpr(cpu_T[0], rs);
1281
        break;
B
bellard 已提交
1282 1283 1284
    }
    switch (opc) {
    case OPC_ADDI:
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
        {
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_TL);
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_TL);
            int l1 = gen_new_label();

            save_cpu_state(ctx, 1);
            tcg_gen_ext32s_tl(r_tmp1, cpu_T[0]);
            tcg_gen_addi_tl(cpu_T[0], r_tmp1, uimm);

            tcg_gen_xori_tl(r_tmp1, r_tmp1, uimm);
            tcg_gen_xori_tl(r_tmp1, r_tmp1, -1);
            tcg_gen_xori_tl(r_tmp2, cpu_T[0], uimm);
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
            tcg_gen_shri_tl(r_tmp1, r_tmp1, 31);
            tcg_gen_brcond_tl(TCG_COND_EQ, r_tmp1, tcg_const_tl(0), l1);
            /* operands of same sign, result different sign */
            generate_exception(ctx, EXCP_OVERFLOW);
            gen_set_label(l1);

            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
        }
B
bellard 已提交
1306 1307 1308
        opn = "addi";
        break;
    case OPC_ADDIU:
1309 1310 1311
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], uimm);
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
1312 1313
        opn = "addiu";
        break;
1314
#if defined(TARGET_MIPS64)
1315
    case OPC_DADDI:
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
        {
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_TL);
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_TL);
            int l1 = gen_new_label();

            save_cpu_state(ctx, 1);
            tcg_gen_mov_tl(r_tmp1, cpu_T[0]);
            tcg_gen_addi_tl(cpu_T[0], cpu_T[0], uimm);

            tcg_gen_xori_tl(r_tmp1, r_tmp1, uimm);
            tcg_gen_xori_tl(r_tmp1, r_tmp1, -1);
            tcg_gen_xori_tl(r_tmp2, cpu_T[0], uimm);
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
            tcg_gen_shri_tl(r_tmp1, r_tmp1, 63);
            tcg_gen_brcond_tl(TCG_COND_EQ, r_tmp1, tcg_const_tl(0), l1);
            /* operands of same sign, result different sign */
            generate_exception(ctx, EXCP_OVERFLOW);
            gen_set_label(l1);
        }
1335 1336 1337
        opn = "daddi";
        break;
    case OPC_DADDIU:
1338
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], uimm);
1339 1340 1341
        opn = "daddiu";
        break;
#endif
B
bellard 已提交
1342
    case OPC_SLTI:
1343
        gen_op_lti(uimm);
B
bellard 已提交
1344 1345 1346
        opn = "slti";
        break;
    case OPC_SLTIU:
1347
        gen_op_ltiu(uimm);
B
bellard 已提交
1348 1349 1350
        opn = "sltiu";
        break;
    case OPC_ANDI:
1351
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], uimm);
B
bellard 已提交
1352 1353 1354
        opn = "andi";
        break;
    case OPC_ORI:
1355
        tcg_gen_ori_tl(cpu_T[0], cpu_T[0], uimm);
B
bellard 已提交
1356 1357 1358
        opn = "ori";
        break;
    case OPC_XORI:
1359
        tcg_gen_xori_tl(cpu_T[0], cpu_T[0], uimm);
B
bellard 已提交
1360 1361 1362 1363 1364 1365
        opn = "xori";
        break;
    case OPC_LUI:
        opn = "lui";
        break;
    case OPC_SLL:
1366 1367 1368
        tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
        tcg_gen_shli_tl(cpu_T[0], cpu_T[0], uimm);
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
1369 1370 1371
        opn = "sll";
        break;
    case OPC_SRA:
1372 1373 1374
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
        tcg_gen_sari_tl(cpu_T[0], cpu_T[0], uimm);
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
1375 1376 1377
        opn = "sra";
        break;
    case OPC_SRL:
1378 1379
        switch ((ctx->opcode >> 21) & 0x1f) {
        case 0:
1380 1381 1382
            tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_shri_tl(cpu_T[0], cpu_T[0], uimm);
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1383
            opn = "srl";
1384 1385
            break;
        case 1:
1386 1387
            /* rotr is decoded as srl on non-R2 CPUs */
            if (env->insn_flags & ISA_MIPS32R2) {
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
                if (uimm != 0) {
                    TCGv r_tmp1 = new_tmp();
                    TCGv r_tmp2 = new_tmp();

                    tcg_gen_trunc_tl_i32(r_tmp1, cpu_T[0]);
                    tcg_gen_movi_i32(r_tmp2, 0x20);
                    tcg_gen_subi_i32(r_tmp2, r_tmp2, uimm);
                    tcg_gen_shl_i32(r_tmp2, r_tmp1, r_tmp2);
                    tcg_gen_shri_i32(r_tmp1, r_tmp1, uimm);
                    tcg_gen_or_i32(r_tmp1, r_tmp1, r_tmp2);
                    tcg_gen_ext_i32_tl(r_tmp1, cpu_T[0]);
                    dead_tmp(r_tmp1);
                    dead_tmp(r_tmp2);
                }
1402 1403
                opn = "rotr";
            } else {
1404 1405 1406
                tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], uimm);
                tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1407 1408
                opn = "srl";
            }
1409 1410 1411 1412 1413 1414
            break;
        default:
            MIPS_INVAL("invalid srl flag");
            generate_exception(ctx, EXCP_RI);
            break;
        }
1415
        break;
1416
#if defined(TARGET_MIPS64)
1417
    case OPC_DSLL:
1418
        tcg_gen_shli_tl(cpu_T[0], cpu_T[0], uimm);
1419 1420 1421
        opn = "dsll";
        break;
    case OPC_DSRA:
1422
        tcg_gen_sari_tl(cpu_T[0], cpu_T[0], uimm);
1423 1424 1425
        opn = "dsra";
        break;
    case OPC_DSRL:
1426 1427
        switch ((ctx->opcode >> 21) & 0x1f) {
        case 0:
1428
            tcg_gen_shri_tl(cpu_T[0], cpu_T[0], uimm);
1429
            opn = "dsrl";
1430 1431
            break;
        case 1:
1432 1433
            /* drotr is decoded as dsrl on non-R2 CPUs */
            if (env->insn_flags & ISA_MIPS32R2) {
1434 1435 1436 1437 1438 1439 1440 1441 1442
                if (uimm != 0) {
                    TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_TL);

                    tcg_gen_movi_tl(r_tmp1, 0x40);
                    tcg_gen_subi_tl(r_tmp1, r_tmp1, uimm);
                    tcg_gen_shl_tl(r_tmp1, cpu_T[0], r_tmp1);
                    tcg_gen_shri_tl(cpu_T[0], cpu_T[0], uimm);
                    tcg_gen_or_tl(cpu_T[0], cpu_T[0], r_tmp1);
                }
1443 1444
                opn = "drotr";
            } else {
1445
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], uimm);
1446 1447
                opn = "dsrl";
            }
1448 1449 1450 1451 1452 1453
            break;
        default:
            MIPS_INVAL("invalid dsrl flag");
            generate_exception(ctx, EXCP_RI);
            break;
        }
1454 1455
        break;
    case OPC_DSLL32:
1456
        tcg_gen_shli_tl(cpu_T[0], cpu_T[0], uimm + 32);
1457 1458 1459
        opn = "dsll32";
        break;
    case OPC_DSRA32:
1460
        tcg_gen_sari_tl(cpu_T[0], cpu_T[0], uimm + 32);
1461 1462 1463
        opn = "dsra32";
        break;
    case OPC_DSRL32:
1464 1465
        switch ((ctx->opcode >> 21) & 0x1f) {
        case 0:
1466
            tcg_gen_shri_tl(cpu_T[0], cpu_T[0], uimm + 32);
1467
            opn = "dsrl32";
1468 1469
            break;
        case 1:
1470 1471
            /* drotr32 is decoded as dsrl32 on non-R2 CPUs */
            if (env->insn_flags & ISA_MIPS32R2) {
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
                TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_TL);
                TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_TL);

                tcg_gen_movi_tl(r_tmp1, 0x40);
                tcg_gen_movi_tl(r_tmp2, 32);
                tcg_gen_addi_tl(r_tmp2, r_tmp2, uimm);
                tcg_gen_sub_tl(r_tmp1, r_tmp1, r_tmp2);
                tcg_gen_shl_tl(r_tmp1, cpu_T[0], r_tmp1);
                tcg_gen_shr_tl(cpu_T[0], cpu_T[0], r_tmp2);
                tcg_gen_or_tl(cpu_T[0], cpu_T[0], r_tmp1);
1482 1483
                opn = "drotr32";
            } else {
1484
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], uimm + 32);
1485 1486
                opn = "dsrl32";
            }
1487 1488 1489 1490 1491 1492
            break;
        default:
            MIPS_INVAL("invalid dsrl32 flag");
            generate_exception(ctx, EXCP_RI);
            break;
        }
B
bellard 已提交
1493
        break;
1494
#endif
B
bellard 已提交
1495
    default:
1496
        MIPS_INVAL(opn);
B
bellard 已提交
1497 1498 1499
        generate_exception(ctx, EXCP_RI);
        return;
    }
1500
    gen_store_gpr(cpu_T[0], rt);
T
ths 已提交
1501
    MIPS_DEBUG("%s %s, %s, " TARGET_FMT_lx, opn, regnames[rt], regnames[rs], uimm);
B
bellard 已提交
1502 1503 1504
}

/* Arithmetic */
1505
static void gen_arith (CPUState *env, DisasContext *ctx, uint32_t opc,
B
bellard 已提交
1506 1507
                       int rd, int rs, int rt)
{
1508
    const char *opn = "arith";
B
bellard 已提交
1509

1510 1511
    if (rd == 0 && opc != OPC_ADD && opc != OPC_SUB
       && opc != OPC_DADD && opc != OPC_DSUB) {
1512 1513
        /* If no destination, treat it as a NOP.
           For add & sub, we must generate the overflow exception when needed. */
B
bellard 已提交
1514 1515 1516
        MIPS_DEBUG("NOP");
        return;
    }
1517
    gen_load_gpr(cpu_T[0], rs);
T
ths 已提交
1518 1519 1520
    /* Specialcase the conventional move operation. */
    if (rt == 0 && (opc == OPC_ADDU || opc == OPC_DADDU
                    || opc == OPC_SUBU || opc == OPC_DSUBU)) {
1521
        gen_store_gpr(cpu_T[0], rd);
T
ths 已提交
1522 1523
        return;
    }
1524
    gen_load_gpr(cpu_T[1], rt);
B
bellard 已提交
1525 1526
    switch (opc) {
    case OPC_ADD:
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
        {
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_TL);
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_TL);
            int l1 = gen_new_label();

            save_cpu_state(ctx, 1);
            tcg_gen_ext32s_tl(r_tmp1, cpu_T[0]);
            tcg_gen_ext32s_tl(r_tmp2, cpu_T[1]);
            tcg_gen_add_tl(cpu_T[0], r_tmp1, r_tmp2);

            tcg_gen_xor_tl(r_tmp1, r_tmp1, cpu_T[1]);
            tcg_gen_xori_tl(r_tmp1, r_tmp1, -1);
            tcg_gen_xor_tl(r_tmp2, cpu_T[0], cpu_T[1]);
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
            tcg_gen_shri_tl(r_tmp1, r_tmp1, 31);
            tcg_gen_brcond_tl(TCG_COND_EQ, r_tmp1, tcg_const_tl(0), l1);
            /* operands of same sign, result different sign */
            generate_exception(ctx, EXCP_OVERFLOW);
            gen_set_label(l1);

            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
        }
B
bellard 已提交
1549 1550 1551
        opn = "add";
        break;
    case OPC_ADDU:
1552 1553 1554 1555
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
        tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
        tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
1556 1557 1558
        opn = "addu";
        break;
    case OPC_SUB:
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
        {
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_TL);
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_TL);
            int l1 = gen_new_label();

            save_cpu_state(ctx, 1);
            tcg_gen_ext32s_tl(r_tmp1, cpu_T[0]);
            tcg_gen_ext32s_tl(r_tmp2, cpu_T[1]);
            tcg_gen_sub_tl(cpu_T[0], r_tmp1, r_tmp2);

            tcg_gen_xor_tl(r_tmp2, r_tmp1, cpu_T[1]);
            tcg_gen_xor_tl(r_tmp1, r_tmp1, cpu_T[0]);
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
            tcg_gen_shri_tl(r_tmp1, r_tmp1, 31);
            tcg_gen_brcond_tl(TCG_COND_EQ, r_tmp1, tcg_const_tl(0), l1);
            /* operands of different sign, first operand and result different sign */
            generate_exception(ctx, EXCP_OVERFLOW);
            gen_set_label(l1);

            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
        }
B
bellard 已提交
1580 1581 1582
        opn = "sub";
        break;
    case OPC_SUBU:
1583 1584 1585 1586
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
        tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
        tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
1587 1588
        opn = "subu";
        break;
1589
#if defined(TARGET_MIPS64)
1590
    case OPC_DADD:
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
        {
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_TL);
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_TL);
            int l1 = gen_new_label();

            save_cpu_state(ctx, 1);
            tcg_gen_mov_tl(r_tmp1, cpu_T[0]);
            tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);

            tcg_gen_xor_tl(r_tmp1, r_tmp1, cpu_T[1]);
            tcg_gen_xori_tl(r_tmp1, r_tmp1, -1);
            tcg_gen_xor_tl(r_tmp2, cpu_T[0], cpu_T[1]);
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
            tcg_gen_shri_tl(r_tmp1, r_tmp1, 63);
            tcg_gen_brcond_tl(TCG_COND_EQ, r_tmp1, tcg_const_tl(0), l1);
            /* operands of same sign, result different sign */
            generate_exception(ctx, EXCP_OVERFLOW);
            gen_set_label(l1);
        }
1610 1611 1612
        opn = "dadd";
        break;
    case OPC_DADDU:
1613
        tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1614 1615 1616
        opn = "daddu";
        break;
    case OPC_DSUB:
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
        {
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_TL);
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_TL);
            int l1 = gen_new_label();

            save_cpu_state(ctx, 1);
            tcg_gen_mov_tl(r_tmp1, cpu_T[0]);
            tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_T[1]);

            tcg_gen_xor_tl(r_tmp2, r_tmp1, cpu_T[1]);
            tcg_gen_xor_tl(r_tmp1, r_tmp1, cpu_T[0]);
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
            tcg_gen_shri_tl(r_tmp1, r_tmp1, 63);
            tcg_gen_brcond_tl(TCG_COND_EQ, r_tmp1, tcg_const_tl(0), l1);
            /* operands of different sign, first operand and result different sign */
            generate_exception(ctx, EXCP_OVERFLOW);
            gen_set_label(l1);
        }
1635 1636 1637
        opn = "dsub";
        break;
    case OPC_DSUBU:
1638
        tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1639 1640 1641
        opn = "dsubu";
        break;
#endif
B
bellard 已提交
1642 1643 1644 1645 1646 1647 1648 1649 1650
    case OPC_SLT:
        gen_op_lt();
        opn = "slt";
        break;
    case OPC_SLTU:
        gen_op_ltu();
        opn = "sltu";
        break;
    case OPC_AND:
1651
        tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
B
bellard 已提交
1652 1653 1654
        opn = "and";
        break;
    case OPC_NOR:
1655 1656
        tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
        tcg_gen_not_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
1657 1658 1659
        opn = "nor";
        break;
    case OPC_OR:
1660
        tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
B
bellard 已提交
1661 1662 1663
        opn = "or";
        break;
    case OPC_XOR:
1664
        tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
B
bellard 已提交
1665 1666 1667
        opn = "xor";
        break;
    case OPC_MUL:
1668 1669 1670 1671
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
        tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
        tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
1672 1673 1674
        opn = "mul";
        break;
    case OPC_MOVN:
T
ths 已提交
1675 1676 1677 1678
        {
            int l1 = gen_new_label();

            tcg_gen_brcond_tl(TCG_COND_EQ, cpu_T[1], tcg_const_tl(0), l1);
1679
            gen_store_gpr(cpu_T[0], rd);
T
ths 已提交
1680 1681
            gen_set_label(l1);
        }
B
bellard 已提交
1682 1683 1684
        opn = "movn";
        goto print;
    case OPC_MOVZ:
T
ths 已提交
1685 1686 1687 1688
        {
            int l1 = gen_new_label();

            tcg_gen_brcond_tl(TCG_COND_NE, cpu_T[1], tcg_const_tl(0), l1);
1689
            gen_store_gpr(cpu_T[0], rd);
T
ths 已提交
1690 1691
            gen_set_label(l1);
        }
B
bellard 已提交
1692 1693 1694
        opn = "movz";
        goto print;
    case OPC_SLLV:
1695 1696 1697 1698 1699
        tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
        tcg_gen_ext32u_tl(cpu_T[1], cpu_T[1]);
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0x1f);
        tcg_gen_shl_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
1700 1701 1702
        opn = "sllv";
        break;
    case OPC_SRAV:
1703 1704 1705 1706
        tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0x1f);
        tcg_gen_sar_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
1707 1708 1709
        opn = "srav";
        break;
    case OPC_SRLV:
1710 1711
        switch ((ctx->opcode >> 6) & 0x1f) {
        case 0:
1712 1713 1714 1715
            tcg_gen_ext32u_tl(cpu_T[1], cpu_T[1]);
            tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0x1f);
            tcg_gen_shr_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1716
            opn = "srlv";
1717 1718
            break;
        case 1:
1719 1720
            /* rotrv is decoded as srlv on non-R2 CPUs */
            if (env->insn_flags & ISA_MIPS32R2) {
1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746
                int l1 = gen_new_label();
                int l2 = gen_new_label();

                tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0x1f);
                tcg_gen_brcond_tl(TCG_COND_EQ, cpu_T[0], tcg_const_tl(0), l1);
                {
                    TCGv r_tmp1 = new_tmp();
                    TCGv r_tmp2 = new_tmp();
                    TCGv r_tmp3 = new_tmp();

                    tcg_gen_trunc_tl_i32(r_tmp1, cpu_T[0]);
                    tcg_gen_trunc_tl_i32(r_tmp2, cpu_T[1]);
                    tcg_gen_movi_i32(r_tmp3, 0x20);
                    tcg_gen_sub_i32(r_tmp3, r_tmp3, r_tmp1);
                    tcg_gen_shl_i32(r_tmp3, r_tmp2, r_tmp3);
                    tcg_gen_shr_i32(r_tmp1, r_tmp2, r_tmp1);
                    tcg_gen_or_i32(r_tmp1, r_tmp1, r_tmp3);
                    tcg_gen_ext_i32_tl(r_tmp1, cpu_T[0]);
                    dead_tmp(r_tmp1);
                    dead_tmp(r_tmp2);
                    dead_tmp(r_tmp3);
                    tcg_gen_br(l2);
                }
                gen_set_label(l1);
                tcg_gen_mov_tl(cpu_T[0], cpu_T[1]);
                gen_set_label(l2);
1747 1748
                opn = "rotrv";
            } else {
1749 1750 1751 1752
                tcg_gen_ext32u_tl(cpu_T[1], cpu_T[1]);
                tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0x1f);
                tcg_gen_shr_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
                tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1753 1754
                opn = "srlv";
            }
1755 1756 1757 1758 1759 1760
            break;
        default:
            MIPS_INVAL("invalid srlv flag");
            generate_exception(ctx, EXCP_RI);
            break;
        }
1761
        break;
1762
#if defined(TARGET_MIPS64)
1763
    case OPC_DSLLV:
1764 1765
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0x3f);
        tcg_gen_shl_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
1766 1767 1768
        opn = "dsllv";
        break;
    case OPC_DSRAV:
1769 1770
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0x3f);
        tcg_gen_sar_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
1771 1772 1773
        opn = "dsrav";
        break;
    case OPC_DSRLV:
1774 1775
        switch ((ctx->opcode >> 6) & 0x1f) {
        case 0:
1776 1777
            tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0x3f);
            tcg_gen_shr_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
1778
            opn = "dsrlv";
1779 1780
            break;
        case 1:
1781 1782
            /* drotrv is decoded as dsrlv on non-R2 CPUs */
            if (env->insn_flags & ISA_MIPS32R2) {
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
                int l1 = gen_new_label();
                int l2 = gen_new_label();

                tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0x3f);
                tcg_gen_brcond_tl(TCG_COND_EQ, cpu_T[0], tcg_const_tl(0), l1);
                {
                    TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_TL);

                    tcg_gen_movi_tl(r_tmp1, 0x40);
                    tcg_gen_sub_tl(r_tmp1, r_tmp1, cpu_T[0]);
                    tcg_gen_shl_tl(r_tmp1, cpu_T[1], r_tmp1);
                    tcg_gen_shr_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
                    tcg_gen_or_tl(cpu_T[0], cpu_T[0], r_tmp1);
                    tcg_gen_br(l2);
                }
                gen_set_label(l1);
                tcg_gen_mov_tl(cpu_T[0], cpu_T[1]);
                gen_set_label(l2);
1801 1802
                opn = "drotrv";
            } else {
1803 1804
                tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0x3f);
                tcg_gen_shr_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
1805 1806
                opn = "dsrlv";
            }
1807 1808 1809 1810 1811 1812
            break;
        default:
            MIPS_INVAL("invalid dsrlv flag");
            generate_exception(ctx, EXCP_RI);
            break;
        }
B
bellard 已提交
1813
        break;
1814
#endif
B
bellard 已提交
1815
    default:
1816
        MIPS_INVAL(opn);
B
bellard 已提交
1817 1818 1819
        generate_exception(ctx, EXCP_RI);
        return;
    }
1820
    gen_store_gpr(cpu_T[0], rd);
B
bellard 已提交
1821 1822 1823 1824 1825
 print:
    MIPS_DEBUG("%s %s, %s, %s", opn, regnames[rd], regnames[rs], regnames[rt]);
}

/* Arithmetic on HI/LO registers */
1826
static void gen_HILO (DisasContext *ctx, uint32_t opc, int reg)
B
bellard 已提交
1827
{
1828
    const char *opn = "hilo";
B
bellard 已提交
1829 1830

    if (reg == 0 && (opc == OPC_MFHI || opc == OPC_MFLO)) {
1831
        /* Treat as NOP. */
B
bellard 已提交
1832 1833 1834 1835 1836
        MIPS_DEBUG("NOP");
        return;
    }
    switch (opc) {
    case OPC_MFHI:
1837 1838
        tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, HI[0]));
        gen_store_gpr(cpu_T[0], reg);
B
bellard 已提交
1839 1840 1841
        opn = "mfhi";
        break;
    case OPC_MFLO:
1842 1843
        tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, LO[0]));
        gen_store_gpr(cpu_T[0], reg);
B
bellard 已提交
1844 1845 1846
        opn = "mflo";
        break;
    case OPC_MTHI:
1847 1848
        gen_load_gpr(cpu_T[0], reg);
        tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUState, HI[0]));
B
bellard 已提交
1849 1850 1851
        opn = "mthi";
        break;
    case OPC_MTLO:
1852 1853
        gen_load_gpr(cpu_T[0], reg);
        tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUState, LO[0]));
B
bellard 已提交
1854 1855 1856
        opn = "mtlo";
        break;
    default:
1857
        MIPS_INVAL(opn);
B
bellard 已提交
1858 1859 1860 1861 1862 1863
        generate_exception(ctx, EXCP_RI);
        return;
    }
    MIPS_DEBUG("%s %s", opn, regnames[reg]);
}

1864
static void gen_muldiv (DisasContext *ctx, uint32_t opc,
B
bellard 已提交
1865 1866
                        int rs, int rt)
{
1867
    const char *opn = "mul/div";
B
bellard 已提交
1868

1869 1870
    gen_load_gpr(cpu_T[0], rs);
    gen_load_gpr(cpu_T[1], rt);
B
bellard 已提交
1871 1872
    switch (opc) {
    case OPC_DIV:
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
        {
            int l1 = gen_new_label();

            tcg_gen_brcond_tl(TCG_COND_EQ, cpu_T[1], tcg_const_tl(0), l1);
            {
                TCGv r_tmp1 = new_tmp();
                TCGv r_tmp2 = new_tmp();
                TCGv r_tmp3 = new_tmp();
                TCGv r_tc_off = new_tmp();
                TCGv r_tc_off_tl = tcg_temp_new(TCG_TYPE_TL);
                TCGv r_ptr = tcg_temp_new(TCG_TYPE_PTR);

                tcg_gen_ext_i32_tl(r_tmp1, cpu_T[0]);
                tcg_gen_ext_i32_tl(r_tmp2, cpu_T[1]);
                tcg_gen_div_i32(r_tmp3, r_tmp1, r_tmp2);
                tcg_gen_rem_i32(r_tmp1, r_tmp1, r_tmp2);
                tcg_gen_trunc_tl_i32(cpu_T[0], r_tmp3);
                tcg_gen_trunc_tl_i32(cpu_T[1], r_tmp1);
                dead_tmp(r_tmp1);
                dead_tmp(r_tmp2);
                dead_tmp(r_tmp3);
                tcg_gen_ld_i32(r_tc_off, cpu_env, offsetof(CPUState, current_tc));
                tcg_gen_muli_i32(r_tc_off, r_tc_off, sizeof(target_ulong));
                tcg_gen_ext_i32_ptr(r_tc_off_tl, r_tc_off);
                tcg_gen_add_ptr(r_ptr, cpu_env, r_tc_off_tl);
                tcg_gen_st_tl(cpu_T[0], r_ptr, offsetof(CPUState, LO));
                tcg_gen_st_tl(cpu_T[1], r_ptr, offsetof(CPUState, HI));
                dead_tmp(r_tc_off);
            }
            gen_set_label(l1);
        }
B
bellard 已提交
1904 1905 1906
        opn = "div";
        break;
    case OPC_DIVU:
1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
        {
            int l1 = gen_new_label();

            tcg_gen_brcond_tl(TCG_COND_EQ, cpu_T[1], tcg_const_tl(0), l1);
            {
                TCGv r_tmp1 = new_tmp();
                TCGv r_tmp2 = new_tmp();
                TCGv r_tmp3 = new_tmp();
                TCGv r_tc_off = new_tmp();
                TCGv r_tc_off_tl = tcg_temp_new(TCG_TYPE_TL);
                TCGv r_ptr = tcg_temp_new(TCG_TYPE_PTR);

                tcg_gen_ext_i32_tl(r_tmp1, cpu_T[0]);
                tcg_gen_ext_i32_tl(r_tmp2, cpu_T[1]);
                tcg_gen_divu_i32(r_tmp3, r_tmp1, r_tmp2);
                tcg_gen_remu_i32(r_tmp1, r_tmp1, r_tmp2);
                tcg_gen_trunc_tl_i32(cpu_T[0], r_tmp3);
                tcg_gen_trunc_tl_i32(cpu_T[1], r_tmp1);
                dead_tmp(r_tmp1);
                dead_tmp(r_tmp2);
                dead_tmp(r_tmp3);
                tcg_gen_ld_i32(r_tc_off, cpu_env, offsetof(CPUState, current_tc));
                tcg_gen_muli_i32(r_tc_off, r_tc_off, sizeof(target_ulong));
                tcg_gen_ext_i32_ptr(r_tc_off_tl, r_tc_off);
                tcg_gen_add_ptr(r_ptr, cpu_env, r_tc_off_tl);
                tcg_gen_st_tl(cpu_T[0], r_ptr, offsetof(CPUState, LO));
                tcg_gen_st_tl(cpu_T[1], r_ptr, offsetof(CPUState, HI));
                dead_tmp(r_tc_off);
            }
            gen_set_label(l1);
        }
B
bellard 已提交
1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
        opn = "divu";
        break;
    case OPC_MULT:
        gen_op_mult();
        opn = "mult";
        break;
    case OPC_MULTU:
        gen_op_multu();
        opn = "multu";
        break;
1948
#if defined(TARGET_MIPS64)
1949
    case OPC_DDIV:
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
        {
            int l1 = gen_new_label();

            tcg_gen_brcond_tl(TCG_COND_EQ, cpu_T[1], tcg_const_tl(0), l1);
            {
                TCGv r_tc_off = new_tmp();
                TCGv r_tc_off_tl = tcg_temp_new(TCG_TYPE_TL);
                TCGv r_ptr = tcg_temp_new(TCG_TYPE_PTR);
                int l2 = gen_new_label();
                int l3 = gen_new_label();

                tcg_gen_brcond_tl(TCG_COND_NE, cpu_T[0], tcg_const_tl(1ULL << 63), l2);
                tcg_gen_brcond_tl(TCG_COND_NE, cpu_T[1], tcg_const_tl(-1ULL), l2);
                tcg_gen_div_i64(cpu_T[0], cpu_T[0], cpu_T[1]);
                tcg_gen_movi_tl(cpu_T[1], 0);
                tcg_gen_br(l3);
                gen_set_label(l2);
                tcg_gen_div_i64(cpu_T[0], cpu_T[0], cpu_T[1]);
                tcg_gen_rem_i64(cpu_T[1], cpu_T[0], cpu_T[1]);
                gen_set_label(l3);

                tcg_gen_ld_i32(r_tc_off, cpu_env, offsetof(CPUState, current_tc));
                tcg_gen_muli_i32(r_tc_off, r_tc_off, sizeof(target_ulong));
                tcg_gen_ext_i32_ptr(r_tc_off_tl, r_tc_off);
                tcg_gen_add_ptr(r_ptr, cpu_env, r_tc_off_tl);
                tcg_gen_st_tl(cpu_T[0], r_ptr, offsetof(CPUState, LO));
                tcg_gen_st_tl(cpu_T[1], r_ptr, offsetof(CPUState, HI));
                dead_tmp(r_tc_off);
            }
            gen_set_label(l1);
        }
1981 1982 1983
        opn = "ddiv";
        break;
    case OPC_DDIVU:
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
        {
            int l1 = gen_new_label();

            tcg_gen_brcond_tl(TCG_COND_EQ, cpu_T[1], tcg_const_tl(0), l1);
            {
                TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I64);
                TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I64);
                TCGv r_tc_off = new_tmp();
                TCGv r_tc_off_tl = tcg_temp_new(TCG_TYPE_TL);
                TCGv r_ptr = tcg_temp_new(TCG_TYPE_PTR);

                tcg_gen_divu_i64(r_tmp1, cpu_T[0], cpu_T[1]);
                tcg_gen_remu_i64(r_tmp2, cpu_T[0], cpu_T[1]);
                tcg_gen_ld_i32(r_tc_off, cpu_env, offsetof(CPUState, current_tc));
                tcg_gen_muli_i32(r_tc_off, r_tc_off, sizeof(target_ulong));
                tcg_gen_ext_i32_ptr(r_tc_off_tl, r_tc_off);
                tcg_gen_add_ptr(r_ptr, cpu_env, r_tc_off_tl);
                tcg_gen_st_tl(r_tmp1, r_ptr, offsetof(CPUState, LO));
                tcg_gen_st_tl(r_tmp2, r_ptr, offsetof(CPUState, HI));
                dead_tmp(r_tc_off);
            }
            gen_set_label(l1);
        }
2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017
        opn = "ddivu";
        break;
    case OPC_DMULT:
        gen_op_dmult();
        opn = "dmult";
        break;
    case OPC_DMULTU:
        gen_op_dmultu();
        opn = "dmultu";
        break;
#endif
B
bellard 已提交
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
    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:
2035
        MIPS_INVAL(opn);
B
bellard 已提交
2036 2037 2038 2039 2040 2041
        generate_exception(ctx, EXCP_RI);
        return;
    }
    MIPS_DEBUG("%s %s %s", opn, regnames[rs], regnames[rt]);
}

2042 2043 2044 2045 2046
static void gen_mul_vr54xx (DisasContext *ctx, uint32_t opc,
                            int rd, int rs, int rt)
{
    const char *opn = "mul vr54xx";

2047 2048
    gen_load_gpr(cpu_T[0], rs);
    gen_load_gpr(cpu_T[1], rt);
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111

    switch (opc) {
    case OPC_VR54XX_MULS:
        gen_op_muls();
        opn = "muls";
	break;
    case OPC_VR54XX_MULSU:
        gen_op_mulsu();
        opn = "mulsu";
	break;
    case OPC_VR54XX_MACC:
        gen_op_macc();
        opn = "macc";
	break;
    case OPC_VR54XX_MACCU:
        gen_op_maccu();
        opn = "maccu";
	break;
    case OPC_VR54XX_MSAC:
        gen_op_msac();
        opn = "msac";
	break;
    case OPC_VR54XX_MSACU:
        gen_op_msacu();
        opn = "msacu";
	break;
    case OPC_VR54XX_MULHI:
        gen_op_mulhi();
        opn = "mulhi";
	break;
    case OPC_VR54XX_MULHIU:
        gen_op_mulhiu();
        opn = "mulhiu";
	break;
    case OPC_VR54XX_MULSHI:
        gen_op_mulshi();
        opn = "mulshi";
	break;
    case OPC_VR54XX_MULSHIU:
        gen_op_mulshiu();
        opn = "mulshiu";
	break;
    case OPC_VR54XX_MACCHI:
        gen_op_macchi();
        opn = "macchi";
	break;
    case OPC_VR54XX_MACCHIU:
        gen_op_macchiu();
        opn = "macchiu";
	break;
    case OPC_VR54XX_MSACHI:
        gen_op_msachi();
        opn = "msachi";
	break;
    case OPC_VR54XX_MSACHIU:
        gen_op_msachiu();
        opn = "msachiu";
	break;
    default:
        MIPS_INVAL("mul vr54xx");
        generate_exception(ctx, EXCP_RI);
        return;
    }
2112
    gen_store_gpr(cpu_T[0], rd);
2113 2114 2115
    MIPS_DEBUG("%s %s, %s, %s", opn, regnames[rd], regnames[rs], regnames[rt]);
}

2116
static void gen_cl (DisasContext *ctx, uint32_t opc,
B
bellard 已提交
2117 2118
                    int rd, int rs)
{
2119
    const char *opn = "CLx";
B
bellard 已提交
2120
    if (rd == 0) {
2121
        /* Treat as NOP. */
B
bellard 已提交
2122 2123 2124
        MIPS_DEBUG("NOP");
        return;
    }
2125
    gen_load_gpr(cpu_T[0], rs);
B
bellard 已提交
2126 2127
    switch (opc) {
    case OPC_CLO:
2128
        tcg_gen_helper_0_0(do_clo);
B
bellard 已提交
2129 2130 2131
        opn = "clo";
        break;
    case OPC_CLZ:
2132
        tcg_gen_helper_0_0(do_clz);
B
bellard 已提交
2133 2134
        opn = "clz";
        break;
2135
#if defined(TARGET_MIPS64)
2136
    case OPC_DCLO:
2137
        tcg_gen_helper_0_0(do_dclo);
2138 2139 2140
        opn = "dclo";
        break;
    case OPC_DCLZ:
2141
        tcg_gen_helper_0_0(do_dclz);
2142 2143 2144
        opn = "dclz";
        break;
#endif
B
bellard 已提交
2145
    default:
2146
        MIPS_INVAL(opn);
B
bellard 已提交
2147 2148 2149
        generate_exception(ctx, EXCP_RI);
        return;
    }
2150
    gen_store_gpr(cpu_T[0], rd);
B
bellard 已提交
2151 2152 2153 2154
    MIPS_DEBUG("%s %s, %s", opn, regnames[rd], regnames[rs]);
}

/* Traps */
2155
static void gen_trap (DisasContext *ctx, uint32_t opc,
B
bellard 已提交
2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170
                      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) {
2171 2172
            gen_load_gpr(cpu_T[0], rs);
            gen_load_gpr(cpu_T[1], rt);
B
bellard 已提交
2173 2174
            cond = 1;
        }
2175
        break;
B
bellard 已提交
2176 2177 2178 2179 2180 2181 2182 2183
    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) {
2184 2185
            gen_load_gpr(cpu_T[0], rs);
            tcg_gen_movi_tl(cpu_T[1], (int32_t)imm);
B
bellard 已提交
2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198
            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 */
2199
            tcg_gen_movi_tl(cpu_T[0], 1);
B
bellard 已提交
2200 2201 2202 2203 2204 2205 2206
            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           */
2207
            /* Never trap: treat as NOP. */
B
bellard 已提交
2208 2209
            return;
        default:
2210
            MIPS_INVAL("trap");
B
bellard 已提交
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
            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:
2241
            MIPS_INVAL("trap");
B
bellard 已提交
2242 2243 2244 2245 2246 2247 2248 2249 2250
            generate_exception(ctx, EXCP_RI);
            return;
        }
    }
    save_cpu_state(ctx, 1);
    gen_op_trap();
    ctx->bstate = BS_STOP;
}

2251
static always_inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
B
bellard 已提交
2252
{
2253 2254 2255
    TranslationBlock *tb;
    tb = ctx->tb;
    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK)) {
B
bellard 已提交
2256
        tcg_gen_goto_tb(n);
2257
        gen_save_pc(dest);
B
bellard 已提交
2258
        tcg_gen_exit_tb((long)tb + n);
2259
    } else {
2260
        gen_save_pc(dest);
B
bellard 已提交
2261
        tcg_gen_exit_tb(0);
2262
    }
B
bellard 已提交
2263 2264
}

B
bellard 已提交
2265
/* Branches (before delay slot) */
2266
static void gen_compute_branch (DisasContext *ctx, uint32_t opc,
B
bellard 已提交
2267 2268
                                int rs, int rt, int32_t offset)
{
2269 2270 2271 2272 2273
    target_ulong btarget = -1;
    int blink = 0;
    int bcond = 0;

    if (ctx->hflags & MIPS_HFLAG_BMASK) {
2274
#ifdef MIPS_DEBUG_DISAS
2275 2276
        if (loglevel & CPU_LOG_TB_IN_ASM) {
            fprintf(logfile,
2277
                    "Branch in delay slot at PC 0x" TARGET_FMT_lx "\n",
2278
                    ctx->pc);
2279
	}
2280
#endif
2281 2282 2283
        generate_exception(ctx, EXCP_RI);
        return;
    }
B
bellard 已提交
2284 2285 2286 2287 2288 2289 2290 2291 2292

    /* Load needed operands */
    switch (opc) {
    case OPC_BEQ:
    case OPC_BEQL:
    case OPC_BNE:
    case OPC_BNEL:
        /* Compare two registers */
        if (rs != rt) {
2293 2294
            gen_load_gpr(cpu_T[0], rs);
            gen_load_gpr(cpu_T[1], rt);
B
bellard 已提交
2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312
            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) {
2313
            gen_load_gpr(cpu_T[0], rs);
B
bellard 已提交
2314 2315 2316 2317 2318 2319 2320
            bcond = 1;
        }
        btarget = ctx->pc + 4 + offset;
        break;
    case OPC_J:
    case OPC_JAL:
        /* Jump to immediate */
2321
        btarget = ((ctx->pc + 4) & (int32_t)0xF0000000) | (uint32_t)offset;
B
bellard 已提交
2322 2323 2324 2325
        break;
    case OPC_JR:
    case OPC_JALR:
        /* Jump to register */
2326 2327
        if (offset != 0 && offset != 16) {
            /* Hint = 0 is JR/JALR, hint 16 is JR.HB/JALR.HB, the
2328
               others are reserved. */
2329
            MIPS_INVAL("jump hint");
B
bellard 已提交
2330 2331 2332
            generate_exception(ctx, EXCP_RI);
            return;
        }
2333
        gen_save_breg_target(rs);
B
bellard 已提交
2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349
        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 已提交
2350
            ctx->hflags |= MIPS_HFLAG_B;
B
bellard 已提交
2351 2352 2353 2354 2355 2356
            MIPS_DEBUG("balways");
            break;
        case OPC_BGEZAL:  /* 0 >= 0          */
        case OPC_BGEZALL: /* 0 >= 0 likely   */
            /* Always take and link */
            blink = 31;
B
bellard 已提交
2357
            ctx->hflags |= MIPS_HFLAG_B;
B
bellard 已提交
2358 2359 2360 2361 2362
            MIPS_DEBUG("balways and link");
            break;
        case OPC_BNE:     /* rx != rx        */
        case OPC_BGTZ:    /* 0 > 0           */
        case OPC_BLTZ:    /* 0 < 0           */
2363
            /* Treat as NOP. */
B
bellard 已提交
2364 2365
            MIPS_DEBUG("bnever (NOP)");
            return;
2366
        case OPC_BLTZAL:  /* 0 < 0           */
2367 2368
            tcg_gen_movi_tl(cpu_T[0], ctx->pc + 8);
            gen_store_gpr(cpu_T[0], 31);
T
ths 已提交
2369
            MIPS_DEBUG("bnever and link");
2370 2371
            return;
        case OPC_BLTZALL: /* 0 < 0 likely */
2372 2373
            tcg_gen_movi_tl(cpu_T[0], ctx->pc + 8);
            gen_store_gpr(cpu_T[0], 31);
T
ths 已提交
2374 2375 2376
            /* Skip the instruction in the delay slot */
            MIPS_DEBUG("bnever, link and skip");
            ctx->pc += 4;
2377
            return;
B
bellard 已提交
2378 2379 2380 2381 2382
        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 已提交
2383
            ctx->pc += 4;
B
bellard 已提交
2384 2385
            return;
        case OPC_J:
B
bellard 已提交
2386
            ctx->hflags |= MIPS_HFLAG_B;
2387
            MIPS_DEBUG("j " TARGET_FMT_lx, btarget);
B
bellard 已提交
2388 2389 2390
            break;
        case OPC_JAL:
            blink = 31;
B
bellard 已提交
2391
            ctx->hflags |= MIPS_HFLAG_B;
2392
            MIPS_DEBUG("jal " TARGET_FMT_lx, btarget);
B
bellard 已提交
2393 2394
            break;
        case OPC_JR:
B
bellard 已提交
2395
            ctx->hflags |= MIPS_HFLAG_BR;
B
bellard 已提交
2396 2397 2398 2399
            MIPS_DEBUG("jr %s", regnames[rs]);
            break;
        case OPC_JALR:
            blink = rt;
B
bellard 已提交
2400
            ctx->hflags |= MIPS_HFLAG_BR;
B
bellard 已提交
2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411
            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();
2412
            MIPS_DEBUG("beq %s, %s, " TARGET_FMT_lx,
B
bellard 已提交
2413 2414 2415 2416
                       regnames[rs], regnames[rt], btarget);
            goto not_likely;
        case OPC_BEQL:
            gen_op_eq();
2417
            MIPS_DEBUG("beql %s, %s, " TARGET_FMT_lx,
B
bellard 已提交
2418 2419 2420 2421
                       regnames[rs], regnames[rt], btarget);
            goto likely;
        case OPC_BNE:
            gen_op_ne();
2422
            MIPS_DEBUG("bne %s, %s, " TARGET_FMT_lx,
B
bellard 已提交
2423 2424 2425 2426
                       regnames[rs], regnames[rt], btarget);
            goto not_likely;
        case OPC_BNEL:
            gen_op_ne();
2427
            MIPS_DEBUG("bnel %s, %s, " TARGET_FMT_lx,
B
bellard 已提交
2428 2429 2430 2431
                       regnames[rs], regnames[rt], btarget);
            goto likely;
        case OPC_BGEZ:
            gen_op_gez();
2432
            MIPS_DEBUG("bgez %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
2433 2434 2435
            goto not_likely;
        case OPC_BGEZL:
            gen_op_gez();
2436
            MIPS_DEBUG("bgezl %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
2437 2438 2439
            goto likely;
        case OPC_BGEZAL:
            gen_op_gez();
2440
            MIPS_DEBUG("bgezal %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
2441 2442 2443 2444 2445
            blink = 31;
            goto not_likely;
        case OPC_BGEZALL:
            gen_op_gez();
            blink = 31;
2446
            MIPS_DEBUG("bgezall %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
2447 2448 2449
            goto likely;
        case OPC_BGTZ:
            gen_op_gtz();
2450
            MIPS_DEBUG("bgtz %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
2451 2452 2453
            goto not_likely;
        case OPC_BGTZL:
            gen_op_gtz();
2454
            MIPS_DEBUG("bgtzl %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
2455 2456 2457
            goto likely;
        case OPC_BLEZ:
            gen_op_lez();
2458
            MIPS_DEBUG("blez %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
2459 2460 2461
            goto not_likely;
        case OPC_BLEZL:
            gen_op_lez();
2462
            MIPS_DEBUG("blezl %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
2463 2464 2465
            goto likely;
        case OPC_BLTZ:
            gen_op_ltz();
2466
            MIPS_DEBUG("bltz %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
2467 2468 2469
            goto not_likely;
        case OPC_BLTZL:
            gen_op_ltz();
2470
            MIPS_DEBUG("bltzl %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
2471 2472 2473 2474
            goto likely;
        case OPC_BLTZAL:
            gen_op_ltz();
            blink = 31;
2475
            MIPS_DEBUG("bltzal %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
2476
        not_likely:
B
bellard 已提交
2477
            ctx->hflags |= MIPS_HFLAG_BC;
2478
            tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUState, bcond));
B
bellard 已提交
2479 2480 2481 2482
            break;
        case OPC_BLTZALL:
            gen_op_ltz();
            blink = 31;
2483
            MIPS_DEBUG("bltzall %s, " TARGET_FMT_lx, regnames[rs], btarget);
B
bellard 已提交
2484
        likely:
B
bellard 已提交
2485
            ctx->hflags |= MIPS_HFLAG_BL;
2486
            tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUState, bcond));
B
bellard 已提交
2487
            break;
T
ths 已提交
2488 2489 2490 2491
        default:
            MIPS_INVAL("conditional branch/jump");
            generate_exception(ctx, EXCP_RI);
            return;
B
bellard 已提交
2492 2493
        }
    }
2494
    MIPS_DEBUG("enter ds: link %d cond %02x target " TARGET_FMT_lx,
B
bellard 已提交
2495
               blink, ctx->hflags, btarget);
2496

B
bellard 已提交
2497 2498
    ctx->btarget = btarget;
    if (blink > 0) {
2499 2500
        tcg_gen_movi_tl(cpu_T[0], ctx->pc + 8);
        gen_store_gpr(cpu_T[0], blink);
B
bellard 已提交
2501 2502 2503
    }
}

2504 2505 2506 2507
/* special3 bitfield operations */
static void gen_bitops (DisasContext *ctx, uint32_t opc, int rt,
                       int rs, int lsb, int msb)
{
2508
    gen_load_gpr(cpu_T[1], rs);
2509 2510 2511 2512 2513 2514
    switch (opc) {
    case OPC_EXT:
        if (lsb + msb > 31)
            goto fail;
        gen_op_ext(lsb, msb + 1);
        break;
T
ths 已提交
2515
#if defined(TARGET_MIPS64)
2516 2517 2518
    case OPC_DEXTM:
        if (lsb + msb > 63)
            goto fail;
T
ths 已提交
2519
        gen_op_dext(lsb, msb + 1 + 32);
2520 2521 2522 2523
        break;
    case OPC_DEXTU:
        if (lsb + msb > 63)
            goto fail;
T
ths 已提交
2524
        gen_op_dext(lsb + 32, msb + 1);
2525 2526
        break;
    case OPC_DEXT:
T
ths 已提交
2527 2528 2529
        if (lsb + msb > 63)
            goto fail;
        gen_op_dext(lsb, msb + 1);
2530
        break;
T
ths 已提交
2531
#endif
2532 2533 2534
    case OPC_INS:
        if (lsb > msb)
            goto fail;
2535
        gen_load_gpr(cpu_T[0], rt);
2536 2537
        gen_op_ins(lsb, msb - lsb + 1);
        break;
T
ths 已提交
2538
#if defined(TARGET_MIPS64)
2539 2540 2541
    case OPC_DINSM:
        if (lsb > msb)
            goto fail;
2542
        gen_load_gpr(cpu_T[0], rt);
T
ths 已提交
2543
        gen_op_dins(lsb, msb - lsb + 1 + 32);
2544 2545 2546 2547
        break;
    case OPC_DINSU:
        if (lsb > msb)
            goto fail;
2548
        gen_load_gpr(cpu_T[0], rt);
T
ths 已提交
2549
        gen_op_dins(lsb + 32, msb - lsb + 1);
2550 2551 2552 2553
        break;
    case OPC_DINS:
        if (lsb > msb)
            goto fail;
2554
        gen_load_gpr(cpu_T[0], rt);
T
ths 已提交
2555
        gen_op_dins(lsb, msb - lsb + 1);
2556
        break;
T
ths 已提交
2557
#endif
2558 2559 2560 2561 2562 2563
    default:
fail:
        MIPS_INVAL("bitops");
        generate_exception(ctx, EXCP_RI);
        return;
    }
2564
    gen_store_gpr(cpu_T[0], rt);
2565 2566
}

B
bellard 已提交
2567
/* CP0 (MMU and control) */
2568
static void gen_mfc0 (CPUState *env, DisasContext *ctx, int reg, int sel)
2569
{
2570
    const char *rn = "invalid";
2571

2572 2573 2574
    if (sel != 0)
        check_insn(env, ctx, ISA_MIPS32);

2575 2576
    switch (reg) {
    case 0:
2577 2578
        switch (sel) {
        case 0:
T
ths 已提交
2579
            gen_op_mfc0_index();
2580 2581 2582
            rn = "Index";
            break;
        case 1:
2583
            check_insn(env, ctx, ASE_MT);
2584
            gen_op_mfc0_mvpcontrol();
2585
            rn = "MVPControl";
2586
            break;
2587
        case 2:
2588
            check_insn(env, ctx, ASE_MT);
2589
            gen_op_mfc0_mvpconf0();
2590
            rn = "MVPConf0";
2591
            break;
2592
        case 3:
2593
            check_insn(env, ctx, ASE_MT);
2594
            gen_op_mfc0_mvpconf1();
2595
            rn = "MVPConf1";
2596
            break;
2597 2598 2599
        default:
            goto die;
        }
2600 2601
        break;
    case 1:
2602 2603 2604 2605
        switch (sel) {
        case 0:
            gen_op_mfc0_random();
            rn = "Random";
T
ths 已提交
2606
            break;
2607
        case 1:
2608
            check_insn(env, ctx, ASE_MT);
2609
            gen_op_mfc0_vpecontrol();
2610
            rn = "VPEControl";
2611
            break;
2612
        case 2:
2613
            check_insn(env, ctx, ASE_MT);
2614
            gen_op_mfc0_vpeconf0();
2615
            rn = "VPEConf0";
2616
            break;
2617
        case 3:
2618
            check_insn(env, ctx, ASE_MT);
2619
            gen_op_mfc0_vpeconf1();
2620
            rn = "VPEConf1";
2621
            break;
2622
        case 4:
2623
            check_insn(env, ctx, ASE_MT);
2624
            gen_op_mfc0_yqmask();
2625
            rn = "YQMask";
2626
            break;
2627
        case 5:
2628
            check_insn(env, ctx, ASE_MT);
2629
            gen_op_mfc0_vpeschedule();
2630
            rn = "VPESchedule";
2631
            break;
2632
        case 6:
2633
            check_insn(env, ctx, ASE_MT);
2634
            gen_op_mfc0_vpeschefback();
2635
            rn = "VPEScheFBack";
2636
            break;
2637
        case 7:
2638
            check_insn(env, ctx, ASE_MT);
2639
            gen_op_mfc0_vpeopt();
2640
            rn = "VPEOpt";
2641
            break;
2642 2643 2644
        default:
            goto die;
        }
2645 2646
        break;
    case 2:
2647 2648
        switch (sel) {
        case 0:
T
ths 已提交
2649 2650 2651
            gen_op_mfc0_entrylo0();
            rn = "EntryLo0";
            break;
2652
        case 1:
2653
            check_insn(env, ctx, ASE_MT);
2654
            gen_op_mfc0_tcstatus();
T
ths 已提交
2655
            rn = "TCStatus";
2656
            break;
2657
        case 2:
2658
            check_insn(env, ctx, ASE_MT);
2659
            gen_op_mfc0_tcbind();
T
ths 已提交
2660
            rn = "TCBind";
2661
            break;
2662
        case 3:
2663
            check_insn(env, ctx, ASE_MT);
2664
            gen_op_mfc0_tcrestart();
T
ths 已提交
2665
            rn = "TCRestart";
2666
            break;
2667
        case 4:
2668
            check_insn(env, ctx, ASE_MT);
2669
            gen_op_mfc0_tchalt();
T
ths 已提交
2670
            rn = "TCHalt";
2671
            break;
2672
        case 5:
2673
            check_insn(env, ctx, ASE_MT);
2674
            gen_op_mfc0_tccontext();
T
ths 已提交
2675
            rn = "TCContext";
2676
            break;
2677
        case 6:
2678
            check_insn(env, ctx, ASE_MT);
2679
            gen_op_mfc0_tcschedule();
T
ths 已提交
2680
            rn = "TCSchedule";
2681
            break;
2682
        case 7:
2683
            check_insn(env, ctx, ASE_MT);
2684
            gen_op_mfc0_tcschefback();
T
ths 已提交
2685
            rn = "TCScheFBack";
2686
            break;
2687 2688 2689
        default:
            goto die;
        }
2690 2691
        break;
    case 3:
2692 2693
        switch (sel) {
        case 0:
T
ths 已提交
2694 2695 2696
            gen_op_mfc0_entrylo1();
            rn = "EntryLo1";
            break;
2697 2698
        default:
            goto die;
2699
        }
2700 2701
        break;
    case 4:
2702 2703
        switch (sel) {
        case 0:
T
ths 已提交
2704 2705 2706
            gen_op_mfc0_context();
            rn = "Context";
            break;
2707
        case 1:
T
ths 已提交
2708 2709 2710
//            gen_op_mfc0_contextconfig(); /* SmartMIPS ASE */
            rn = "ContextConfig";
//            break;
2711 2712
        default:
            goto die;
2713
        }
2714 2715
        break;
    case 5:
2716 2717
        switch (sel) {
        case 0:
T
ths 已提交
2718 2719 2720
            gen_op_mfc0_pagemask();
            rn = "PageMask";
            break;
2721
        case 1:
2722
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
2723 2724 2725
            gen_op_mfc0_pagegrain();
            rn = "PageGrain";
            break;
2726 2727
        default:
            goto die;
2728
        }
2729 2730
        break;
    case 6:
2731 2732
        switch (sel) {
        case 0:
T
ths 已提交
2733 2734 2735
            gen_op_mfc0_wired();
            rn = "Wired";
            break;
2736
        case 1:
2737
            check_insn(env, ctx, ISA_MIPS32R2);
2738
            gen_op_mfc0_srsconf0();
T
ths 已提交
2739
            rn = "SRSConf0";
2740
            break;
2741
        case 2:
2742
            check_insn(env, ctx, ISA_MIPS32R2);
2743
            gen_op_mfc0_srsconf1();
T
ths 已提交
2744
            rn = "SRSConf1";
2745
            break;
2746
        case 3:
2747
            check_insn(env, ctx, ISA_MIPS32R2);
2748
            gen_op_mfc0_srsconf2();
T
ths 已提交
2749
            rn = "SRSConf2";
2750
            break;
2751
        case 4:
2752
            check_insn(env, ctx, ISA_MIPS32R2);
2753
            gen_op_mfc0_srsconf3();
T
ths 已提交
2754
            rn = "SRSConf3";
2755
            break;
2756
        case 5:
2757
            check_insn(env, ctx, ISA_MIPS32R2);
2758
            gen_op_mfc0_srsconf4();
T
ths 已提交
2759
            rn = "SRSConf4";
2760
            break;
2761 2762
        default:
            goto die;
2763
        }
2764
        break;
2765
    case 7:
2766 2767
        switch (sel) {
        case 0:
2768
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
2769 2770 2771
            gen_op_mfc0_hwrena();
            rn = "HWREna";
            break;
2772 2773
        default:
            goto die;
2774
        }
2775
        break;
2776
    case 8:
2777 2778
        switch (sel) {
        case 0:
T
ths 已提交
2779 2780 2781
            gen_op_mfc0_badvaddr();
            rn = "BadVaddr";
            break;
2782 2783 2784
        default:
            goto die;
       }
2785 2786
        break;
    case 9:
2787 2788
        switch (sel) {
        case 0:
T
ths 已提交
2789 2790 2791 2792
            gen_op_mfc0_count();
            rn = "Count";
            break;
        /* 6,7 are implementation dependent */
2793 2794
        default:
            goto die;
T
ths 已提交
2795
        }
2796 2797
        break;
    case 10:
2798 2799
        switch (sel) {
        case 0:
T
ths 已提交
2800 2801 2802
            gen_op_mfc0_entryhi();
            rn = "EntryHi";
            break;
2803 2804
        default:
            goto die;
2805
        }
2806 2807
        break;
    case 11:
2808 2809
        switch (sel) {
        case 0:
T
ths 已提交
2810 2811 2812 2813
            gen_op_mfc0_compare();
            rn = "Compare";
            break;
        /* 6,7 are implementation dependent */
2814 2815
        default:
            goto die;
T
ths 已提交
2816
        }
2817 2818
        break;
    case 12:
2819 2820
        switch (sel) {
        case 0:
T
ths 已提交
2821 2822 2823
            gen_op_mfc0_status();
            rn = "Status";
            break;
2824
        case 1:
2825
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
2826 2827 2828
            gen_op_mfc0_intctl();
            rn = "IntCtl";
            break;
2829
        case 2:
2830
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
2831 2832 2833
            gen_op_mfc0_srsctl();
            rn = "SRSCtl";
            break;
2834
        case 3:
2835
            check_insn(env, ctx, ISA_MIPS32R2);
2836
            gen_op_mfc0_srsmap();
T
ths 已提交
2837
            rn = "SRSMap";
2838
            break;
2839 2840 2841
        default:
            goto die;
       }
2842 2843
        break;
    case 13:
2844 2845
        switch (sel) {
        case 0:
T
ths 已提交
2846 2847 2848
            gen_op_mfc0_cause();
            rn = "Cause";
            break;
2849 2850 2851
        default:
            goto die;
       }
2852 2853
        break;
    case 14:
2854 2855
        switch (sel) {
        case 0:
T
ths 已提交
2856 2857 2858
            gen_op_mfc0_epc();
            rn = "EPC";
            break;
2859 2860
        default:
            goto die;
2861
        }
2862 2863
        break;
    case 15:
2864 2865
        switch (sel) {
        case 0:
T
ths 已提交
2866 2867 2868
            gen_op_mfc0_prid();
            rn = "PRid";
            break;
2869
        case 1:
2870
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
2871 2872 2873
            gen_op_mfc0_ebase();
            rn = "EBase";
            break;
2874 2875 2876
        default:
            goto die;
       }
2877 2878 2879 2880
        break;
    case 16:
        switch (sel) {
        case 0:
2881
            gen_op_mfc0_config0();
2882 2883 2884
            rn = "Config";
            break;
        case 1:
2885
            gen_op_mfc0_config1();
2886 2887
            rn = "Config1";
            break;
2888
        case 2:
2889
            gen_op_mfc0_config2();
2890 2891 2892
            rn = "Config2";
            break;
        case 3:
2893
            gen_op_mfc0_config3();
2894 2895
            rn = "Config3";
            break;
2896 2897 2898 2899 2900 2901 2902 2903 2904 2905
        /* 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;
2906 2907 2908 2909 2910
        default:
            goto die;
        }
        break;
    case 17:
2911 2912
        switch (sel) {
        case 0:
T
ths 已提交
2913 2914 2915
            gen_op_mfc0_lladdr();
            rn = "LLAddr";
            break;
2916 2917 2918
        default:
            goto die;
        }
2919 2920
        break;
    case 18:
2921
        switch (sel) {
2922 2923
        case 0 ... 7:
            gen_op_mfc0_watchlo(sel);
T
ths 已提交
2924 2925
            rn = "WatchLo";
            break;
2926 2927 2928
        default:
            goto die;
        }
2929 2930
        break;
    case 19:
2931
        switch (sel) {
2932 2933
        case 0 ...7:
            gen_op_mfc0_watchhi(sel);
T
ths 已提交
2934 2935
            rn = "WatchHi";
            break;
2936 2937 2938
        default:
            goto die;
        }
2939
        break;
2940
    case 20:
2941 2942
        switch (sel) {
        case 0:
2943
#if defined(TARGET_MIPS64)
2944
            check_insn(env, ctx, ISA_MIPS3);
T
ths 已提交
2945 2946 2947
            gen_op_mfc0_xcontext();
            rn = "XContext";
            break;
T
ths 已提交
2948
#endif
2949 2950 2951
        default:
            goto die;
        }
2952 2953
        break;
    case 21:
2954 2955 2956
       /* Officially reserved, but sel 0 is used for R1x000 framemask */
        switch (sel) {
        case 0:
T
ths 已提交
2957 2958 2959
            gen_op_mfc0_framemask();
            rn = "Framemask";
            break;
2960 2961 2962
        default:
            goto die;
        }
2963 2964
        break;
    case 22:
T
ths 已提交
2965 2966 2967
        /* ignored */
        rn = "'Diagnostic"; /* implementation dependent */
        break;
2968
    case 23:
2969 2970
        switch (sel) {
        case 0:
T
ths 已提交
2971 2972 2973
            gen_op_mfc0_debug(); /* EJTAG support */
            rn = "Debug";
            break;
2974
        case 1:
T
ths 已提交
2975 2976 2977
//            gen_op_mfc0_tracecontrol(); /* PDtrace support */
            rn = "TraceControl";
//            break;
2978
        case 2:
T
ths 已提交
2979 2980 2981
//            gen_op_mfc0_tracecontrol2(); /* PDtrace support */
            rn = "TraceControl2";
//            break;
2982
        case 3:
T
ths 已提交
2983 2984 2985
//            gen_op_mfc0_usertracedata(); /* PDtrace support */
            rn = "UserTraceData";
//            break;
2986
        case 4:
T
ths 已提交
2987 2988 2989
//            gen_op_mfc0_debug(); /* PDtrace support */
            rn = "TraceBPC";
//            break;
2990 2991 2992
        default:
            goto die;
        }
2993 2994
        break;
    case 24:
2995 2996
        switch (sel) {
        case 0:
T
ths 已提交
2997 2998 2999
            gen_op_mfc0_depc(); /* EJTAG support */
            rn = "DEPC";
            break;
3000 3001 3002
        default:
            goto die;
        }
3003
        break;
3004
    case 25:
3005 3006
        switch (sel) {
        case 0:
T
ths 已提交
3007 3008
            gen_op_mfc0_performance0();
            rn = "Performance0";
3009 3010
            break;
        case 1:
T
ths 已提交
3011 3012 3013
//            gen_op_mfc0_performance1();
            rn = "Performance1";
//            break;
3014
        case 2:
T
ths 已提交
3015 3016 3017
//            gen_op_mfc0_performance2();
            rn = "Performance2";
//            break;
3018
        case 3:
T
ths 已提交
3019 3020 3021
//            gen_op_mfc0_performance3();
            rn = "Performance3";
//            break;
3022
        case 4:
T
ths 已提交
3023 3024 3025
//            gen_op_mfc0_performance4();
            rn = "Performance4";
//            break;
3026
        case 5:
T
ths 已提交
3027 3028 3029
//            gen_op_mfc0_performance5();
            rn = "Performance5";
//            break;
3030
        case 6:
T
ths 已提交
3031 3032 3033
//            gen_op_mfc0_performance6();
            rn = "Performance6";
//            break;
3034
        case 7:
T
ths 已提交
3035 3036 3037
//            gen_op_mfc0_performance7();
            rn = "Performance7";
//            break;
3038 3039 3040
        default:
            goto die;
        }
3041 3042
        break;
    case 26:
3043 3044
       rn = "ECC";
       break;
3045
    case 27:
3046 3047 3048
        switch (sel) {
        /* ignored */
        case 0 ... 3:
T
ths 已提交
3049 3050
            rn = "CacheErr";
            break;
3051 3052 3053
        default:
            goto die;
        }
3054
        break;
3055 3056 3057
    case 28:
        switch (sel) {
        case 0:
3058 3059 3060
        case 2:
        case 4:
        case 6:
3061 3062 3063 3064
            gen_op_mfc0_taglo();
            rn = "TagLo";
            break;
        case 1:
3065 3066 3067
        case 3:
        case 5:
        case 7:
3068 3069 3070 3071 3072 3073 3074
            gen_op_mfc0_datalo();
            rn = "DataLo";
            break;
        default:
            goto die;
        }
        break;
3075
    case 29:
3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093
        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;
        }
3094
        break;
3095
    case 30:
3096 3097
        switch (sel) {
        case 0:
T
ths 已提交
3098 3099 3100
            gen_op_mfc0_errorepc();
            rn = "ErrorEPC";
            break;
3101 3102 3103
        default:
            goto die;
        }
3104 3105
        break;
    case 31:
3106 3107
        switch (sel) {
        case 0:
T
ths 已提交
3108 3109 3110
            gen_op_mfc0_desave(); /* EJTAG support */
            rn = "DESAVE";
            break;
3111 3112 3113
        default:
            goto die;
        }
3114 3115 3116 3117 3118 3119
        break;
    default:
       goto die;
    }
#if defined MIPS_DEBUG_DISAS
    if (loglevel & CPU_LOG_TB_IN_ASM) {
3120 3121
        fprintf(logfile, "mfc0 %s (reg %d sel %d)\n",
                rn, reg, sel);
3122 3123 3124 3125 3126 3127 3128
    }
#endif
    return;

die:
#if defined MIPS_DEBUG_DISAS
    if (loglevel & CPU_LOG_TB_IN_ASM) {
3129 3130
        fprintf(logfile, "mfc0 %s (reg %d sel %d)\n",
                rn, reg, sel);
3131 3132 3133 3134 3135
    }
#endif
    generate_exception(ctx, EXCP_RI);
}

3136
static void gen_mtc0 (CPUState *env, DisasContext *ctx, int reg, int sel)
3137
{
3138 3139
    const char *rn = "invalid";

3140 3141 3142
    if (sel != 0)
        check_insn(env, ctx, ISA_MIPS32);

3143 3144
    switch (reg) {
    case 0:
3145 3146
        switch (sel) {
        case 0:
T
ths 已提交
3147
            gen_op_mtc0_index();
3148 3149 3150
            rn = "Index";
            break;
        case 1:
3151
            check_insn(env, ctx, ASE_MT);
3152
            gen_op_mtc0_mvpcontrol();
3153
            rn = "MVPControl";
3154
            break;
3155
        case 2:
3156
            check_insn(env, ctx, ASE_MT);
3157
            /* ignored */
3158
            rn = "MVPConf0";
3159
            break;
3160
        case 3:
3161
            check_insn(env, ctx, ASE_MT);
3162
            /* ignored */
3163
            rn = "MVPConf1";
3164
            break;
3165 3166 3167
        default:
            goto die;
        }
3168 3169
        break;
    case 1:
3170 3171
        switch (sel) {
        case 0:
T
ths 已提交
3172
            /* ignored */
3173
            rn = "Random";
T
ths 已提交
3174
            break;
3175
        case 1:
3176
            check_insn(env, ctx, ASE_MT);
3177
            gen_op_mtc0_vpecontrol();
3178
            rn = "VPEControl";
3179
            break;
3180
        case 2:
3181
            check_insn(env, ctx, ASE_MT);
3182
            gen_op_mtc0_vpeconf0();
3183
            rn = "VPEConf0";
3184
            break;
3185
        case 3:
3186
            check_insn(env, ctx, ASE_MT);
3187
            gen_op_mtc0_vpeconf1();
3188
            rn = "VPEConf1";
3189
            break;
3190
        case 4:
3191
            check_insn(env, ctx, ASE_MT);
3192
            gen_op_mtc0_yqmask();
3193
            rn = "YQMask";
3194
            break;
3195
        case 5:
3196
            check_insn(env, ctx, ASE_MT);
3197
            gen_op_mtc0_vpeschedule();
3198
            rn = "VPESchedule";
3199
            break;
3200
        case 6:
3201
            check_insn(env, ctx, ASE_MT);
3202
            gen_op_mtc0_vpeschefback();
3203
            rn = "VPEScheFBack";
3204
            break;
3205
        case 7:
3206
            check_insn(env, ctx, ASE_MT);
3207
            gen_op_mtc0_vpeopt();
3208
            rn = "VPEOpt";
3209
            break;
3210 3211 3212
        default:
            goto die;
        }
3213 3214
        break;
    case 2:
3215 3216
        switch (sel) {
        case 0:
T
ths 已提交
3217 3218 3219
            gen_op_mtc0_entrylo0();
            rn = "EntryLo0";
            break;
3220
        case 1:
3221
            check_insn(env, ctx, ASE_MT);
3222
            gen_op_mtc0_tcstatus();
T
ths 已提交
3223
            rn = "TCStatus";
3224
            break;
3225
        case 2:
3226
            check_insn(env, ctx, ASE_MT);
3227
            gen_op_mtc0_tcbind();
T
ths 已提交
3228
            rn = "TCBind";
3229
            break;
3230
        case 3:
3231
            check_insn(env, ctx, ASE_MT);
3232
            gen_op_mtc0_tcrestart();
T
ths 已提交
3233
            rn = "TCRestart";
3234
            break;
3235
        case 4:
3236
            check_insn(env, ctx, ASE_MT);
3237
            gen_op_mtc0_tchalt();
T
ths 已提交
3238
            rn = "TCHalt";
3239
            break;
3240
        case 5:
3241
            check_insn(env, ctx, ASE_MT);
3242
            gen_op_mtc0_tccontext();
T
ths 已提交
3243
            rn = "TCContext";
3244
            break;
3245
        case 6:
3246
            check_insn(env, ctx, ASE_MT);
3247
            gen_op_mtc0_tcschedule();
T
ths 已提交
3248
            rn = "TCSchedule";
3249
            break;
3250
        case 7:
3251
            check_insn(env, ctx, ASE_MT);
3252
            gen_op_mtc0_tcschefback();
T
ths 已提交
3253
            rn = "TCScheFBack";
3254
            break;
3255 3256 3257
        default:
            goto die;
        }
3258 3259
        break;
    case 3:
3260 3261
        switch (sel) {
        case 0:
T
ths 已提交
3262 3263 3264
            gen_op_mtc0_entrylo1();
            rn = "EntryLo1";
            break;
3265 3266
        default:
            goto die;
T
ths 已提交
3267
        }
3268 3269
        break;
    case 4:
3270 3271
        switch (sel) {
        case 0:
T
ths 已提交
3272 3273 3274
            gen_op_mtc0_context();
            rn = "Context";
            break;
3275
        case 1:
T
ths 已提交
3276 3277 3278
//            gen_op_mtc0_contextconfig(); /* SmartMIPS ASE */
            rn = "ContextConfig";
//            break;
3279 3280
        default:
            goto die;
T
ths 已提交
3281
        }
3282 3283
        break;
    case 5:
3284 3285
        switch (sel) {
        case 0:
T
ths 已提交
3286 3287 3288
            gen_op_mtc0_pagemask();
            rn = "PageMask";
            break;
3289
        case 1:
3290
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
3291 3292 3293
            gen_op_mtc0_pagegrain();
            rn = "PageGrain";
            break;
3294 3295
        default:
            goto die;
T
ths 已提交
3296
        }
3297 3298
        break;
    case 6:
3299 3300
        switch (sel) {
        case 0:
T
ths 已提交
3301 3302 3303
            gen_op_mtc0_wired();
            rn = "Wired";
            break;
3304
        case 1:
3305
            check_insn(env, ctx, ISA_MIPS32R2);
3306
            gen_op_mtc0_srsconf0();
T
ths 已提交
3307
            rn = "SRSConf0";
3308
            break;
3309
        case 2:
3310
            check_insn(env, ctx, ISA_MIPS32R2);
3311
            gen_op_mtc0_srsconf1();
T
ths 已提交
3312
            rn = "SRSConf1";
3313
            break;
3314
        case 3:
3315
            check_insn(env, ctx, ISA_MIPS32R2);
3316
            gen_op_mtc0_srsconf2();
T
ths 已提交
3317
            rn = "SRSConf2";
3318
            break;
3319
        case 4:
3320
            check_insn(env, ctx, ISA_MIPS32R2);
3321
            gen_op_mtc0_srsconf3();
T
ths 已提交
3322
            rn = "SRSConf3";
3323
            break;
3324
        case 5:
3325
            check_insn(env, ctx, ISA_MIPS32R2);
3326
            gen_op_mtc0_srsconf4();
T
ths 已提交
3327
            rn = "SRSConf4";
3328
            break;
3329 3330
        default:
            goto die;
T
ths 已提交
3331
        }
3332 3333
        break;
    case 7:
3334 3335
        switch (sel) {
        case 0:
3336
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
3337 3338 3339
            gen_op_mtc0_hwrena();
            rn = "HWREna";
            break;
3340 3341
        default:
            goto die;
T
ths 已提交
3342
        }
3343 3344
        break;
    case 8:
3345
        /* ignored */
3346 3347 3348
        rn = "BadVaddr";
        break;
    case 9:
3349 3350
        switch (sel) {
        case 0:
T
ths 已提交
3351 3352 3353
            gen_op_mtc0_count();
            rn = "Count";
            break;
T
ths 已提交
3354
        /* 6,7 are implementation dependent */
3355 3356
        default:
            goto die;
T
ths 已提交
3357 3358 3359
        }
        /* Stop translation as we may have switched the execution mode */
        ctx->bstate = BS_STOP;
3360 3361
        break;
    case 10:
3362 3363
        switch (sel) {
        case 0:
T
ths 已提交
3364 3365 3366
            gen_op_mtc0_entryhi();
            rn = "EntryHi";
            break;
3367 3368
        default:
            goto die;
T
ths 已提交
3369
        }
3370 3371
        break;
    case 11:
3372 3373
        switch (sel) {
        case 0:
T
ths 已提交
3374 3375 3376 3377
            gen_op_mtc0_compare();
            rn = "Compare";
            break;
        /* 6,7 are implementation dependent */
3378 3379
        default:
            goto die;
T
ths 已提交
3380
        }
A
aurel32 已提交
3381 3382
        /* Stop translation as we may have switched the execution mode */
        ctx->bstate = BS_STOP;
3383 3384
        break;
    case 12:
3385 3386
        switch (sel) {
        case 0:
T
ths 已提交
3387
            gen_op_mtc0_status();
3388 3389 3390
            /* BS_STOP isn't good enough here, hflags may have changed. */
            gen_save_pc(ctx->pc + 4);
            ctx->bstate = BS_EXCP;
T
ths 已提交
3391 3392
            rn = "Status";
            break;
3393
        case 1:
3394
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
3395
            gen_op_mtc0_intctl();
3396 3397
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
3398 3399
            rn = "IntCtl";
            break;
3400
        case 2:
3401
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
3402
            gen_op_mtc0_srsctl();
3403 3404
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
3405 3406
            rn = "SRSCtl";
            break;
3407
        case 3:
3408
            check_insn(env, ctx, ISA_MIPS32R2);
3409
            gen_op_mtc0_srsmap();
3410 3411
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
3412
            rn = "SRSMap";
3413
            break;
3414 3415
        default:
            goto die;
T
ths 已提交
3416
        }
3417 3418
        break;
    case 13:
3419 3420
        switch (sel) {
        case 0:
T
ths 已提交
3421 3422 3423
            gen_op_mtc0_cause();
            rn = "Cause";
            break;
3424 3425
        default:
            goto die;
T
ths 已提交
3426 3427 3428
        }
        /* Stop translation as we may have switched the execution mode */
        ctx->bstate = BS_STOP;
3429 3430
        break;
    case 14:
3431 3432
        switch (sel) {
        case 0:
T
ths 已提交
3433 3434 3435
            gen_op_mtc0_epc();
            rn = "EPC";
            break;
3436 3437
        default:
            goto die;
T
ths 已提交
3438
        }
3439 3440
        break;
    case 15:
3441 3442
        switch (sel) {
        case 0:
T
ths 已提交
3443 3444 3445
            /* ignored */
            rn = "PRid";
            break;
3446
        case 1:
3447
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
3448 3449 3450
            gen_op_mtc0_ebase();
            rn = "EBase";
            break;
3451 3452
        default:
            goto die;
3453
        }
3454 3455 3456 3457
        break;
    case 16:
        switch (sel) {
        case 0:
3458
            gen_op_mtc0_config0();
3459
            rn = "Config";
T
ths 已提交
3460 3461
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
3462 3463
            break;
        case 1:
3464
            /* ignored, read only */
3465 3466 3467
            rn = "Config1";
            break;
        case 2:
3468
            gen_op_mtc0_config2();
3469
            rn = "Config2";
T
ths 已提交
3470 3471
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
3472
            break;
3473
        case 3:
3474
            /* ignored, read only */
3475 3476
            rn = "Config3";
            break;
3477 3478 3479 3480 3481 3482 3483 3484 3485 3486
        /* 4,5 are reserved */
        /* 6,7 are implementation dependent */
        case 6:
            /* ignored */
            rn = "Config6";
            break;
        case 7:
            /* ignored */
            rn = "Config7";
            break;
3487 3488 3489 3490 3491 3492
        default:
            rn = "Invalid config selector";
            goto die;
        }
        break;
    case 17:
3493 3494
        switch (sel) {
        case 0:
T
ths 已提交
3495 3496 3497
            /* ignored */
            rn = "LLAddr";
            break;
3498 3499 3500
        default:
            goto die;
        }
3501 3502
        break;
    case 18:
3503
        switch (sel) {
3504 3505
        case 0 ... 7:
            gen_op_mtc0_watchlo(sel);
T
ths 已提交
3506 3507
            rn = "WatchLo";
            break;
3508 3509 3510
        default:
            goto die;
        }
3511 3512
        break;
    case 19:
3513
        switch (sel) {
3514 3515
        case 0 ... 7:
            gen_op_mtc0_watchhi(sel);
T
ths 已提交
3516 3517
            rn = "WatchHi";
            break;
3518 3519 3520
        default:
            goto die;
        }
3521 3522
        break;
    case 20:
3523 3524
        switch (sel) {
        case 0:
3525
#if defined(TARGET_MIPS64)
3526
            check_insn(env, ctx, ISA_MIPS3);
3527
            gen_op_mtc0_xcontext();
T
ths 已提交
3528 3529
            rn = "XContext";
            break;
T
ths 已提交
3530
#endif
3531 3532 3533
        default:
            goto die;
        }
3534 3535
        break;
    case 21:
3536 3537 3538
       /* Officially reserved, but sel 0 is used for R1x000 framemask */
        switch (sel) {
        case 0:
T
ths 已提交
3539 3540 3541
            gen_op_mtc0_framemask();
            rn = "Framemask";
            break;
3542 3543 3544 3545
        default:
            goto die;
        }
        break;
3546
    case 22:
3547 3548
        /* ignored */
        rn = "Diagnostic"; /* implementation dependent */
T
ths 已提交
3549
        break;
3550
    case 23:
3551 3552
        switch (sel) {
        case 0:
T
ths 已提交
3553
            gen_op_mtc0_debug(); /* EJTAG support */
3554 3555 3556
            /* BS_STOP isn't good enough here, hflags may have changed. */
            gen_save_pc(ctx->pc + 4);
            ctx->bstate = BS_EXCP;
T
ths 已提交
3557 3558
            rn = "Debug";
            break;
3559
        case 1:
T
ths 已提交
3560 3561
//            gen_op_mtc0_tracecontrol(); /* PDtrace support */
            rn = "TraceControl";
3562 3563
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
3564
//            break;
3565
        case 2:
T
ths 已提交
3566 3567
//            gen_op_mtc0_tracecontrol2(); /* PDtrace support */
            rn = "TraceControl2";
3568 3569
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
3570
//            break;
3571
        case 3:
3572 3573
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
3574 3575
//            gen_op_mtc0_usertracedata(); /* PDtrace support */
            rn = "UserTraceData";
3576 3577
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
3578
//            break;
3579
        case 4:
T
ths 已提交
3580
//            gen_op_mtc0_debug(); /* PDtrace support */
3581 3582
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
3583 3584
            rn = "TraceBPC";
//            break;
3585 3586 3587
        default:
            goto die;
        }
3588 3589
        break;
    case 24:
3590 3591
        switch (sel) {
        case 0:
T
ths 已提交
3592 3593 3594
            gen_op_mtc0_depc(); /* EJTAG support */
            rn = "DEPC";
            break;
3595 3596 3597
        default:
            goto die;
        }
3598 3599
        break;
    case 25:
3600 3601
        switch (sel) {
        case 0:
T
ths 已提交
3602 3603 3604
            gen_op_mtc0_performance0();
            rn = "Performance0";
            break;
3605
        case 1:
T
ths 已提交
3606 3607 3608
//            gen_op_mtc0_performance1();
            rn = "Performance1";
//            break;
3609
        case 2:
T
ths 已提交
3610 3611 3612
//            gen_op_mtc0_performance2();
            rn = "Performance2";
//            break;
3613
        case 3:
T
ths 已提交
3614 3615 3616
//            gen_op_mtc0_performance3();
            rn = "Performance3";
//            break;
3617
        case 4:
T
ths 已提交
3618 3619 3620
//            gen_op_mtc0_performance4();
            rn = "Performance4";
//            break;
3621
        case 5:
T
ths 已提交
3622 3623 3624
//            gen_op_mtc0_performance5();
            rn = "Performance5";
//            break;
3625
        case 6:
T
ths 已提交
3626 3627 3628
//            gen_op_mtc0_performance6();
            rn = "Performance6";
//            break;
3629
        case 7:
T
ths 已提交
3630 3631 3632
//            gen_op_mtc0_performance7();
            rn = "Performance7";
//            break;
3633 3634 3635
        default:
            goto die;
        }
3636 3637
       break;
    case 26:
T
ths 已提交
3638
        /* ignored */
3639
        rn = "ECC";
T
ths 已提交
3640
        break;
3641
    case 27:
3642 3643
        switch (sel) {
        case 0 ... 3:
T
ths 已提交
3644 3645 3646
            /* ignored */
            rn = "CacheErr";
            break;
3647 3648 3649
        default:
            goto die;
        }
3650 3651 3652 3653
       break;
    case 28:
        switch (sel) {
        case 0:
3654 3655 3656
        case 2:
        case 4:
        case 6:
3657 3658 3659
            gen_op_mtc0_taglo();
            rn = "TagLo";
            break;
3660 3661 3662 3663
        case 1:
        case 3:
        case 5:
        case 7:
T
ths 已提交
3664
            gen_op_mtc0_datalo();
3665 3666
            rn = "DataLo";
            break;
3667 3668 3669 3670 3671
        default:
            goto die;
        }
        break;
    case 29:
3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683
        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 已提交
3684
            gen_op_mtc0_datahi();
3685 3686 3687 3688 3689 3690
            rn = "DataHi";
            break;
        default:
            rn = "invalid sel";
            goto die;
        }
3691 3692
       break;
    case 30:
3693 3694
        switch (sel) {
        case 0:
T
ths 已提交
3695 3696 3697
            gen_op_mtc0_errorepc();
            rn = "ErrorEPC";
            break;
3698 3699 3700
        default:
            goto die;
        }
3701 3702
        break;
    case 31:
3703 3704
        switch (sel) {
        case 0:
T
ths 已提交
3705 3706 3707
            gen_op_mtc0_desave(); /* EJTAG support */
            rn = "DESAVE";
            break;
3708 3709 3710
        default:
            goto die;
        }
T
ths 已提交
3711 3712
        /* Stop translation as we may have switched the execution mode */
        ctx->bstate = BS_STOP;
3713 3714 3715 3716 3717 3718
        break;
    default:
       goto die;
    }
#if defined MIPS_DEBUG_DISAS
    if (loglevel & CPU_LOG_TB_IN_ASM) {
3719 3720
        fprintf(logfile, "mtc0 %s (reg %d sel %d)\n",
                rn, reg, sel);
3721 3722 3723 3724 3725 3726 3727
    }
#endif
    return;

die:
#if defined MIPS_DEBUG_DISAS
    if (loglevel & CPU_LOG_TB_IN_ASM) {
3728 3729
        fprintf(logfile, "mtc0 %s (reg %d sel %d)\n",
                rn, reg, sel);
3730 3731 3732 3733 3734
    }
#endif
    generate_exception(ctx, EXCP_RI);
}

3735
#if defined(TARGET_MIPS64)
3736
static void gen_dmfc0 (CPUState *env, DisasContext *ctx, int reg, int sel)
T
ths 已提交
3737 3738 3739
{
    const char *rn = "invalid";

3740 3741 3742
    if (sel != 0)
        check_insn(env, ctx, ISA_MIPS64);

T
ths 已提交
3743 3744 3745 3746
    switch (reg) {
    case 0:
        switch (sel) {
        case 0:
T
ths 已提交
3747
            gen_op_mfc0_index();
T
ths 已提交
3748 3749 3750
            rn = "Index";
            break;
        case 1:
3751
            check_insn(env, ctx, ASE_MT);
3752
            gen_op_mfc0_mvpcontrol();
T
ths 已提交
3753
            rn = "MVPControl";
3754
            break;
T
ths 已提交
3755
        case 2:
3756
            check_insn(env, ctx, ASE_MT);
3757
            gen_op_mfc0_mvpconf0();
T
ths 已提交
3758
            rn = "MVPConf0";
3759
            break;
T
ths 已提交
3760
        case 3:
3761
            check_insn(env, ctx, ASE_MT);
3762
            gen_op_mfc0_mvpconf1();
T
ths 已提交
3763
            rn = "MVPConf1";
3764
            break;
T
ths 已提交
3765 3766 3767 3768 3769 3770 3771 3772 3773
        default:
            goto die;
        }
        break;
    case 1:
        switch (sel) {
        case 0:
            gen_op_mfc0_random();
            rn = "Random";
T
ths 已提交
3774
            break;
T
ths 已提交
3775
        case 1:
3776
            check_insn(env, ctx, ASE_MT);
3777
            gen_op_mfc0_vpecontrol();
T
ths 已提交
3778
            rn = "VPEControl";
3779
            break;
T
ths 已提交
3780
        case 2:
3781
            check_insn(env, ctx, ASE_MT);
3782
            gen_op_mfc0_vpeconf0();
T
ths 已提交
3783
            rn = "VPEConf0";
3784
            break;
T
ths 已提交
3785
        case 3:
3786
            check_insn(env, ctx, ASE_MT);
3787
            gen_op_mfc0_vpeconf1();
T
ths 已提交
3788
            rn = "VPEConf1";
3789
            break;
T
ths 已提交
3790
        case 4:
3791
            check_insn(env, ctx, ASE_MT);
3792
            gen_op_dmfc0_yqmask();
T
ths 已提交
3793
            rn = "YQMask";
3794
            break;
T
ths 已提交
3795
        case 5:
3796
            check_insn(env, ctx, ASE_MT);
3797
            gen_op_dmfc0_vpeschedule();
T
ths 已提交
3798
            rn = "VPESchedule";
3799
            break;
T
ths 已提交
3800
        case 6:
3801
            check_insn(env, ctx, ASE_MT);
3802
            gen_op_dmfc0_vpeschefback();
T
ths 已提交
3803
            rn = "VPEScheFBack";
3804
            break;
T
ths 已提交
3805
        case 7:
3806
            check_insn(env, ctx, ASE_MT);
3807
            gen_op_mfc0_vpeopt();
T
ths 已提交
3808
            rn = "VPEOpt";
3809
            break;
T
ths 已提交
3810 3811 3812 3813 3814 3815 3816
        default:
            goto die;
        }
        break;
    case 2:
        switch (sel) {
        case 0:
T
ths 已提交
3817 3818 3819
            gen_op_dmfc0_entrylo0();
            rn = "EntryLo0";
            break;
T
ths 已提交
3820
        case 1:
3821
            check_insn(env, ctx, ASE_MT);
3822
            gen_op_mfc0_tcstatus();
T
ths 已提交
3823
            rn = "TCStatus";
3824
            break;
T
ths 已提交
3825
        case 2:
3826
            check_insn(env, ctx, ASE_MT);
3827
            gen_op_mfc0_tcbind();
T
ths 已提交
3828
            rn = "TCBind";
3829
            break;
T
ths 已提交
3830
        case 3:
3831
            check_insn(env, ctx, ASE_MT);
3832
            gen_op_dmfc0_tcrestart();
T
ths 已提交
3833
            rn = "TCRestart";
3834
            break;
T
ths 已提交
3835
        case 4:
3836
            check_insn(env, ctx, ASE_MT);
3837
            gen_op_dmfc0_tchalt();
T
ths 已提交
3838
            rn = "TCHalt";
3839
            break;
T
ths 已提交
3840
        case 5:
3841
            check_insn(env, ctx, ASE_MT);
3842
            gen_op_dmfc0_tccontext();
T
ths 已提交
3843
            rn = "TCContext";
3844
            break;
T
ths 已提交
3845
        case 6:
3846
            check_insn(env, ctx, ASE_MT);
3847
            gen_op_dmfc0_tcschedule();
T
ths 已提交
3848
            rn = "TCSchedule";
3849
            break;
T
ths 已提交
3850
        case 7:
3851
            check_insn(env, ctx, ASE_MT);
3852
            gen_op_dmfc0_tcschefback();
T
ths 已提交
3853
            rn = "TCScheFBack";
3854
            break;
T
ths 已提交
3855 3856 3857 3858 3859 3860 3861
        default:
            goto die;
        }
        break;
    case 3:
        switch (sel) {
        case 0:
T
ths 已提交
3862 3863 3864
            gen_op_dmfc0_entrylo1();
            rn = "EntryLo1";
            break;
T
ths 已提交
3865 3866
        default:
            goto die;
3867
        }
T
ths 已提交
3868 3869 3870 3871
        break;
    case 4:
        switch (sel) {
        case 0:
T
ths 已提交
3872 3873 3874
            gen_op_dmfc0_context();
            rn = "Context";
            break;
T
ths 已提交
3875
        case 1:
T
ths 已提交
3876 3877 3878
//            gen_op_dmfc0_contextconfig(); /* SmartMIPS ASE */
            rn = "ContextConfig";
//            break;
T
ths 已提交
3879 3880
        default:
            goto die;
T
ths 已提交
3881
        }
T
ths 已提交
3882 3883 3884 3885
        break;
    case 5:
        switch (sel) {
        case 0:
T
ths 已提交
3886 3887 3888
            gen_op_mfc0_pagemask();
            rn = "PageMask";
            break;
T
ths 已提交
3889
        case 1:
3890
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
3891 3892 3893
            gen_op_mfc0_pagegrain();
            rn = "PageGrain";
            break;
T
ths 已提交
3894 3895
        default:
            goto die;
T
ths 已提交
3896
        }
T
ths 已提交
3897 3898 3899 3900
        break;
    case 6:
        switch (sel) {
        case 0:
T
ths 已提交
3901 3902 3903
            gen_op_mfc0_wired();
            rn = "Wired";
            break;
T
ths 已提交
3904
        case 1:
3905
            check_insn(env, ctx, ISA_MIPS32R2);
3906
            gen_op_mfc0_srsconf0();
T
ths 已提交
3907
            rn = "SRSConf0";
3908
            break;
T
ths 已提交
3909
        case 2:
3910
            check_insn(env, ctx, ISA_MIPS32R2);
3911
            gen_op_mfc0_srsconf1();
T
ths 已提交
3912
            rn = "SRSConf1";
3913
            break;
T
ths 已提交
3914
        case 3:
3915
            check_insn(env, ctx, ISA_MIPS32R2);
3916
            gen_op_mfc0_srsconf2();
T
ths 已提交
3917
            rn = "SRSConf2";
3918
            break;
T
ths 已提交
3919
        case 4:
3920
            check_insn(env, ctx, ISA_MIPS32R2);
3921
            gen_op_mfc0_srsconf3();
T
ths 已提交
3922
            rn = "SRSConf3";
3923
            break;
T
ths 已提交
3924
        case 5:
3925
            check_insn(env, ctx, ISA_MIPS32R2);
3926
            gen_op_mfc0_srsconf4();
T
ths 已提交
3927
            rn = "SRSConf4";
3928
            break;
T
ths 已提交
3929 3930
        default:
            goto die;
T
ths 已提交
3931
        }
T
ths 已提交
3932 3933 3934 3935
        break;
    case 7:
        switch (sel) {
        case 0:
3936
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
3937 3938 3939
            gen_op_mfc0_hwrena();
            rn = "HWREna";
            break;
T
ths 已提交
3940 3941
        default:
            goto die;
T
ths 已提交
3942
        }
T
ths 已提交
3943 3944 3945 3946
        break;
    case 8:
        switch (sel) {
        case 0:
T
ths 已提交
3947 3948 3949
            gen_op_dmfc0_badvaddr();
            rn = "BadVaddr";
            break;
T
ths 已提交
3950 3951
        default:
            goto die;
T
ths 已提交
3952
        }
T
ths 已提交
3953 3954 3955 3956
        break;
    case 9:
        switch (sel) {
        case 0:
T
ths 已提交
3957 3958 3959 3960
            gen_op_mfc0_count();
            rn = "Count";
            break;
        /* 6,7 are implementation dependent */
T
ths 已提交
3961 3962
        default:
            goto die;
T
ths 已提交
3963
        }
T
ths 已提交
3964 3965 3966 3967
        break;
    case 10:
        switch (sel) {
        case 0:
T
ths 已提交
3968 3969 3970
            gen_op_dmfc0_entryhi();
            rn = "EntryHi";
            break;
T
ths 已提交
3971 3972
        default:
            goto die;
T
ths 已提交
3973
        }
T
ths 已提交
3974 3975 3976 3977
        break;
    case 11:
        switch (sel) {
        case 0:
T
ths 已提交
3978 3979 3980
            gen_op_mfc0_compare();
            rn = "Compare";
            break;
T
ths 已提交
3981
        /* 6,7 are implementation dependent */
T
ths 已提交
3982 3983
        default:
            goto die;
T
ths 已提交
3984
        }
T
ths 已提交
3985 3986 3987 3988
        break;
    case 12:
        switch (sel) {
        case 0:
T
ths 已提交
3989 3990 3991
            gen_op_mfc0_status();
            rn = "Status";
            break;
T
ths 已提交
3992
        case 1:
3993
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
3994 3995 3996
            gen_op_mfc0_intctl();
            rn = "IntCtl";
            break;
T
ths 已提交
3997
        case 2:
3998
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
3999 4000 4001
            gen_op_mfc0_srsctl();
            rn = "SRSCtl";
            break;
T
ths 已提交
4002
        case 3:
4003
            check_insn(env, ctx, ISA_MIPS32R2);
4004
            gen_op_mfc0_srsmap();
T
ths 已提交
4005 4006
            rn = "SRSMap";
            break;
T
ths 已提交
4007 4008
        default:
            goto die;
T
ths 已提交
4009
        }
T
ths 已提交
4010 4011 4012 4013
        break;
    case 13:
        switch (sel) {
        case 0:
T
ths 已提交
4014 4015 4016
            gen_op_mfc0_cause();
            rn = "Cause";
            break;
T
ths 已提交
4017 4018
        default:
            goto die;
T
ths 已提交
4019
        }
T
ths 已提交
4020 4021 4022 4023
        break;
    case 14:
        switch (sel) {
        case 0:
T
ths 已提交
4024 4025 4026
            gen_op_dmfc0_epc();
            rn = "EPC";
            break;
T
ths 已提交
4027 4028
        default:
            goto die;
T
ths 已提交
4029
        }
T
ths 已提交
4030 4031 4032 4033
        break;
    case 15:
        switch (sel) {
        case 0:
T
ths 已提交
4034 4035 4036
            gen_op_mfc0_prid();
            rn = "PRid";
            break;
T
ths 已提交
4037
        case 1:
4038
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
4039 4040 4041
            gen_op_mfc0_ebase();
            rn = "EBase";
            break;
T
ths 已提交
4042 4043
        default:
            goto die;
T
ths 已提交
4044
        }
T
ths 已提交
4045 4046 4047 4048
        break;
    case 16:
        switch (sel) {
        case 0:
T
ths 已提交
4049
            gen_op_mfc0_config0();
T
ths 已提交
4050 4051 4052
            rn = "Config";
            break;
        case 1:
T
ths 已提交
4053
            gen_op_mfc0_config1();
T
ths 已提交
4054 4055 4056
            rn = "Config1";
            break;
        case 2:
T
ths 已提交
4057
            gen_op_mfc0_config2();
T
ths 已提交
4058 4059 4060
            rn = "Config2";
            break;
        case 3:
T
ths 已提交
4061
            gen_op_mfc0_config3();
T
ths 已提交
4062 4063 4064 4065 4066 4067 4068 4069 4070 4071
            rn = "Config3";
            break;
       /* 6,7 are implementation dependent */
        default:
            goto die;
        }
        break;
    case 17:
        switch (sel) {
        case 0:
T
ths 已提交
4072 4073 4074
            gen_op_dmfc0_lladdr();
            rn = "LLAddr";
            break;
T
ths 已提交
4075 4076 4077 4078 4079 4080
        default:
            goto die;
        }
        break;
    case 18:
        switch (sel) {
4081 4082
        case 0 ... 7:
            gen_op_dmfc0_watchlo(sel);
T
ths 已提交
4083 4084
            rn = "WatchLo";
            break;
T
ths 已提交
4085 4086 4087 4088 4089 4090
        default:
            goto die;
        }
        break;
    case 19:
        switch (sel) {
4091 4092
        case 0 ... 7:
            gen_op_mfc0_watchhi(sel);
T
ths 已提交
4093 4094
            rn = "WatchHi";
            break;
T
ths 已提交
4095 4096 4097 4098 4099 4100 4101
        default:
            goto die;
        }
        break;
    case 20:
        switch (sel) {
        case 0:
4102
            check_insn(env, ctx, ISA_MIPS3);
T
ths 已提交
4103 4104 4105
            gen_op_dmfc0_xcontext();
            rn = "XContext";
            break;
T
ths 已提交
4106 4107 4108 4109 4110 4111 4112 4113
        default:
            goto die;
        }
        break;
    case 21:
       /* Officially reserved, but sel 0 is used for R1x000 framemask */
        switch (sel) {
        case 0:
T
ths 已提交
4114 4115 4116
            gen_op_mfc0_framemask();
            rn = "Framemask";
            break;
T
ths 已提交
4117 4118 4119 4120 4121
        default:
            goto die;
        }
        break;
    case 22:
T
ths 已提交
4122 4123 4124
        /* ignored */
        rn = "'Diagnostic"; /* implementation dependent */
        break;
T
ths 已提交
4125 4126 4127
    case 23:
        switch (sel) {
        case 0:
T
ths 已提交
4128 4129 4130
            gen_op_mfc0_debug(); /* EJTAG support */
            rn = "Debug";
            break;
T
ths 已提交
4131
        case 1:
T
ths 已提交
4132 4133 4134
//            gen_op_dmfc0_tracecontrol(); /* PDtrace support */
            rn = "TraceControl";
//            break;
T
ths 已提交
4135
        case 2:
T
ths 已提交
4136 4137 4138
//            gen_op_dmfc0_tracecontrol2(); /* PDtrace support */
            rn = "TraceControl2";
//            break;
T
ths 已提交
4139
        case 3:
T
ths 已提交
4140 4141 4142
//            gen_op_dmfc0_usertracedata(); /* PDtrace support */
            rn = "UserTraceData";
//            break;
T
ths 已提交
4143
        case 4:
T
ths 已提交
4144 4145 4146
//            gen_op_dmfc0_debug(); /* PDtrace support */
            rn = "TraceBPC";
//            break;
T
ths 已提交
4147 4148 4149 4150 4151 4152 4153
        default:
            goto die;
        }
        break;
    case 24:
        switch (sel) {
        case 0:
T
ths 已提交
4154 4155 4156
            gen_op_dmfc0_depc(); /* EJTAG support */
            rn = "DEPC";
            break;
T
ths 已提交
4157 4158 4159 4160 4161 4162 4163
        default:
            goto die;
        }
        break;
    case 25:
        switch (sel) {
        case 0:
T
ths 已提交
4164 4165
            gen_op_mfc0_performance0();
            rn = "Performance0";
T
ths 已提交
4166 4167
            break;
        case 1:
T
ths 已提交
4168 4169 4170
//            gen_op_dmfc0_performance1();
            rn = "Performance1";
//            break;
T
ths 已提交
4171
        case 2:
T
ths 已提交
4172 4173 4174
//            gen_op_dmfc0_performance2();
            rn = "Performance2";
//            break;
T
ths 已提交
4175
        case 3:
T
ths 已提交
4176 4177 4178
//            gen_op_dmfc0_performance3();
            rn = "Performance3";
//            break;
T
ths 已提交
4179
        case 4:
T
ths 已提交
4180 4181 4182
//            gen_op_dmfc0_performance4();
            rn = "Performance4";
//            break;
T
ths 已提交
4183
        case 5:
T
ths 已提交
4184 4185 4186
//            gen_op_dmfc0_performance5();
            rn = "Performance5";
//            break;
T
ths 已提交
4187
        case 6:
T
ths 已提交
4188 4189 4190
//            gen_op_dmfc0_performance6();
            rn = "Performance6";
//            break;
T
ths 已提交
4191
        case 7:
T
ths 已提交
4192 4193 4194
//            gen_op_dmfc0_performance7();
            rn = "Performance7";
//            break;
T
ths 已提交
4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205
        default:
            goto die;
        }
        break;
    case 26:
       rn = "ECC";
       break;
    case 27:
        switch (sel) {
        /* ignored */
        case 0 ... 3:
T
ths 已提交
4206 4207
            rn = "CacheErr";
            break;
T
ths 已提交
4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254
        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 已提交
4255 4256 4257
            gen_op_dmfc0_errorepc();
            rn = "ErrorEPC";
            break;
T
ths 已提交
4258 4259 4260 4261 4262 4263 4264
        default:
            goto die;
        }
        break;
    case 31:
        switch (sel) {
        case 0:
T
ths 已提交
4265 4266 4267
            gen_op_mfc0_desave(); /* EJTAG support */
            rn = "DESAVE";
            break;
T
ths 已提交
4268 4269 4270 4271 4272
        default:
            goto die;
        }
        break;
    default:
T
ths 已提交
4273
        goto die;
T
ths 已提交
4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292
    }
#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);
}

4293
static void gen_dmtc0 (CPUState *env, DisasContext *ctx, int reg, int sel)
T
ths 已提交
4294 4295 4296
{
    const char *rn = "invalid";

4297 4298 4299
    if (sel != 0)
        check_insn(env, ctx, ISA_MIPS64);

T
ths 已提交
4300 4301 4302 4303 4304 4305 4306 4307
    switch (reg) {
    case 0:
        switch (sel) {
        case 0:
            gen_op_mtc0_index();
            rn = "Index";
            break;
        case 1:
4308
            check_insn(env, ctx, ASE_MT);
4309
            gen_op_mtc0_mvpcontrol();
T
ths 已提交
4310
            rn = "MVPControl";
4311
            break;
T
ths 已提交
4312
        case 2:
4313
            check_insn(env, ctx, ASE_MT);
4314
            /* ignored */
T
ths 已提交
4315
            rn = "MVPConf0";
4316
            break;
T
ths 已提交
4317
        case 3:
4318
            check_insn(env, ctx, ASE_MT);
4319
            /* ignored */
T
ths 已提交
4320
            rn = "MVPConf1";
4321
            break;
T
ths 已提交
4322 4323 4324 4325 4326 4327 4328
        default:
            goto die;
        }
        break;
    case 1:
        switch (sel) {
        case 0:
T
ths 已提交
4329
            /* ignored */
T
ths 已提交
4330
            rn = "Random";
T
ths 已提交
4331
            break;
T
ths 已提交
4332
        case 1:
4333
            check_insn(env, ctx, ASE_MT);
4334
            gen_op_mtc0_vpecontrol();
T
ths 已提交
4335
            rn = "VPEControl";
4336
            break;
T
ths 已提交
4337
        case 2:
4338
            check_insn(env, ctx, ASE_MT);
4339
            gen_op_mtc0_vpeconf0();
T
ths 已提交
4340
            rn = "VPEConf0";
4341
            break;
T
ths 已提交
4342
        case 3:
4343
            check_insn(env, ctx, ASE_MT);
4344
            gen_op_mtc0_vpeconf1();
T
ths 已提交
4345
            rn = "VPEConf1";
4346
            break;
T
ths 已提交
4347
        case 4:
4348
            check_insn(env, ctx, ASE_MT);
4349
            gen_op_mtc0_yqmask();
T
ths 已提交
4350
            rn = "YQMask";
4351
            break;
T
ths 已提交
4352
        case 5:
4353
            check_insn(env, ctx, ASE_MT);
4354
            gen_op_mtc0_vpeschedule();
T
ths 已提交
4355
            rn = "VPESchedule";
4356
            break;
T
ths 已提交
4357
        case 6:
4358
            check_insn(env, ctx, ASE_MT);
4359
            gen_op_mtc0_vpeschefback();
T
ths 已提交
4360
            rn = "VPEScheFBack";
4361
            break;
T
ths 已提交
4362
        case 7:
4363
            check_insn(env, ctx, ASE_MT);
4364
            gen_op_mtc0_vpeopt();
T
ths 已提交
4365
            rn = "VPEOpt";
4366
            break;
T
ths 已提交
4367 4368 4369 4370 4371 4372 4373
        default:
            goto die;
        }
        break;
    case 2:
        switch (sel) {
        case 0:
4374
            gen_op_mtc0_entrylo0();
T
ths 已提交
4375 4376
            rn = "EntryLo0";
            break;
T
ths 已提交
4377
        case 1:
4378
            check_insn(env, ctx, ASE_MT);
4379
            gen_op_mtc0_tcstatus();
T
ths 已提交
4380
            rn = "TCStatus";
4381
            break;
T
ths 已提交
4382
        case 2:
4383
            check_insn(env, ctx, ASE_MT);
4384
            gen_op_mtc0_tcbind();
T
ths 已提交
4385
            rn = "TCBind";
4386
            break;
T
ths 已提交
4387
        case 3:
4388
            check_insn(env, ctx, ASE_MT);
4389
            gen_op_mtc0_tcrestart();
T
ths 已提交
4390
            rn = "TCRestart";
4391
            break;
T
ths 已提交
4392
        case 4:
4393
            check_insn(env, ctx, ASE_MT);
4394
            gen_op_mtc0_tchalt();
T
ths 已提交
4395
            rn = "TCHalt";
4396
            break;
T
ths 已提交
4397
        case 5:
4398
            check_insn(env, ctx, ASE_MT);
4399
            gen_op_mtc0_tccontext();
T
ths 已提交
4400
            rn = "TCContext";
4401
            break;
T
ths 已提交
4402
        case 6:
4403
            check_insn(env, ctx, ASE_MT);
4404
            gen_op_mtc0_tcschedule();
T
ths 已提交
4405
            rn = "TCSchedule";
4406
            break;
T
ths 已提交
4407
        case 7:
4408
            check_insn(env, ctx, ASE_MT);
4409
            gen_op_mtc0_tcschefback();
T
ths 已提交
4410
            rn = "TCScheFBack";
4411
            break;
T
ths 已提交
4412 4413 4414 4415 4416 4417 4418
        default:
            goto die;
        }
        break;
    case 3:
        switch (sel) {
        case 0:
4419
            gen_op_mtc0_entrylo1();
T
ths 已提交
4420 4421
            rn = "EntryLo1";
            break;
T
ths 已提交
4422 4423
        default:
            goto die;
T
ths 已提交
4424
        }
T
ths 已提交
4425 4426 4427 4428
        break;
    case 4:
        switch (sel) {
        case 0:
4429
            gen_op_mtc0_context();
T
ths 已提交
4430 4431
            rn = "Context";
            break;
T
ths 已提交
4432
        case 1:
4433
//           gen_op_mtc0_contextconfig(); /* SmartMIPS ASE */
T
ths 已提交
4434 4435
            rn = "ContextConfig";
//           break;
T
ths 已提交
4436 4437
        default:
            goto die;
T
ths 已提交
4438
        }
T
ths 已提交
4439 4440 4441 4442
        break;
    case 5:
        switch (sel) {
        case 0:
T
ths 已提交
4443 4444 4445
            gen_op_mtc0_pagemask();
            rn = "PageMask";
            break;
T
ths 已提交
4446
        case 1:
4447
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
4448 4449 4450
            gen_op_mtc0_pagegrain();
            rn = "PageGrain";
            break;
T
ths 已提交
4451 4452
        default:
            goto die;
T
ths 已提交
4453
        }
T
ths 已提交
4454 4455 4456 4457
        break;
    case 6:
        switch (sel) {
        case 0:
T
ths 已提交
4458 4459 4460
            gen_op_mtc0_wired();
            rn = "Wired";
            break;
T
ths 已提交
4461
        case 1:
4462
            check_insn(env, ctx, ISA_MIPS32R2);
4463
            gen_op_mtc0_srsconf0();
T
ths 已提交
4464
            rn = "SRSConf0";
4465
            break;
T
ths 已提交
4466
        case 2:
4467
            check_insn(env, ctx, ISA_MIPS32R2);
4468
            gen_op_mtc0_srsconf1();
T
ths 已提交
4469
            rn = "SRSConf1";
4470
            break;
T
ths 已提交
4471
        case 3:
4472
            check_insn(env, ctx, ISA_MIPS32R2);
4473
            gen_op_mtc0_srsconf2();
T
ths 已提交
4474
            rn = "SRSConf2";
4475
            break;
T
ths 已提交
4476
        case 4:
4477
            check_insn(env, ctx, ISA_MIPS32R2);
4478
            gen_op_mtc0_srsconf3();
T
ths 已提交
4479
            rn = "SRSConf3";
4480
            break;
T
ths 已提交
4481
        case 5:
4482
            check_insn(env, ctx, ISA_MIPS32R2);
4483
            gen_op_mtc0_srsconf4();
T
ths 已提交
4484
            rn = "SRSConf4";
4485
            break;
T
ths 已提交
4486 4487
        default:
            goto die;
T
ths 已提交
4488
        }
T
ths 已提交
4489 4490 4491 4492
        break;
    case 7:
        switch (sel) {
        case 0:
4493
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
4494 4495 4496
            gen_op_mtc0_hwrena();
            rn = "HWREna";
            break;
T
ths 已提交
4497 4498
        default:
            goto die;
T
ths 已提交
4499
        }
T
ths 已提交
4500 4501 4502 4503 4504 4505 4506 4507
        break;
    case 8:
        /* ignored */
        rn = "BadVaddr";
        break;
    case 9:
        switch (sel) {
        case 0:
T
ths 已提交
4508 4509 4510
            gen_op_mtc0_count();
            rn = "Count";
            break;
T
ths 已提交
4511
        /* 6,7 are implementation dependent */
T
ths 已提交
4512 4513
        default:
            goto die;
T
ths 已提交
4514 4515 4516
        }
        /* Stop translation as we may have switched the execution mode */
        ctx->bstate = BS_STOP;
T
ths 已提交
4517 4518 4519 4520
        break;
    case 10:
        switch (sel) {
        case 0:
T
ths 已提交
4521 4522 4523
            gen_op_mtc0_entryhi();
            rn = "EntryHi";
            break;
T
ths 已提交
4524 4525
        default:
            goto die;
T
ths 已提交
4526
        }
T
ths 已提交
4527 4528 4529 4530
        break;
    case 11:
        switch (sel) {
        case 0:
T
ths 已提交
4531 4532 4533
            gen_op_mtc0_compare();
            rn = "Compare";
            break;
T
ths 已提交
4534
        /* 6,7 are implementation dependent */
T
ths 已提交
4535 4536
        default:
            goto die;
T
ths 已提交
4537
        }
A
aurel32 已提交
4538 4539
        /* Stop translation as we may have switched the execution mode */
        ctx->bstate = BS_STOP;
T
ths 已提交
4540 4541 4542 4543
        break;
    case 12:
        switch (sel) {
        case 0:
T
ths 已提交
4544
            gen_op_mtc0_status();
4545 4546 4547
            /* BS_STOP isn't good enough here, hflags may have changed. */
            gen_save_pc(ctx->pc + 4);
            ctx->bstate = BS_EXCP;
T
ths 已提交
4548 4549
            rn = "Status";
            break;
T
ths 已提交
4550
        case 1:
4551
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
4552
            gen_op_mtc0_intctl();
4553 4554
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
4555 4556
            rn = "IntCtl";
            break;
T
ths 已提交
4557
        case 2:
4558
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
4559
            gen_op_mtc0_srsctl();
4560 4561
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
4562 4563
            rn = "SRSCtl";
            break;
T
ths 已提交
4564
        case 3:
4565
            check_insn(env, ctx, ISA_MIPS32R2);
4566
            gen_op_mtc0_srsmap();
4567 4568
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
4569 4570 4571
            rn = "SRSMap";
            break;
        default:
T
ths 已提交
4572
            goto die;
T
ths 已提交
4573
        }
T
ths 已提交
4574 4575 4576 4577
        break;
    case 13:
        switch (sel) {
        case 0:
T
ths 已提交
4578 4579 4580
            gen_op_mtc0_cause();
            rn = "Cause";
            break;
T
ths 已提交
4581 4582
        default:
            goto die;
T
ths 已提交
4583 4584 4585
        }
        /* Stop translation as we may have switched the execution mode */
        ctx->bstate = BS_STOP;
T
ths 已提交
4586 4587 4588 4589
        break;
    case 14:
        switch (sel) {
        case 0:
4590
            gen_op_mtc0_epc();
T
ths 已提交
4591 4592
            rn = "EPC";
            break;
T
ths 已提交
4593 4594
        default:
            goto die;
T
ths 已提交
4595
        }
T
ths 已提交
4596 4597 4598 4599
        break;
    case 15:
        switch (sel) {
        case 0:
T
ths 已提交
4600 4601 4602
            /* ignored */
            rn = "PRid";
            break;
T
ths 已提交
4603
        case 1:
4604
            check_insn(env, ctx, ISA_MIPS32R2);
T
ths 已提交
4605 4606 4607
            gen_op_mtc0_ebase();
            rn = "EBase";
            break;
T
ths 已提交
4608 4609
        default:
            goto die;
T
ths 已提交
4610
        }
T
ths 已提交
4611 4612 4613 4614 4615 4616
        break;
    case 16:
        switch (sel) {
        case 0:
            gen_op_mtc0_config0();
            rn = "Config";
T
ths 已提交
4617 4618
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
4619 4620
            break;
        case 1:
T
ths 已提交
4621
            /* ignored */
T
ths 已提交
4622 4623 4624 4625 4626
            rn = "Config1";
            break;
        case 2:
            gen_op_mtc0_config2();
            rn = "Config2";
T
ths 已提交
4627 4628
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
4629 4630
            break;
        case 3:
T
ths 已提交
4631
            /* ignored */
T
ths 已提交
4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642
            rn = "Config3";
            break;
        /* 6,7 are implementation dependent */
        default:
            rn = "Invalid config selector";
            goto die;
        }
        break;
    case 17:
        switch (sel) {
        case 0:
T
ths 已提交
4643 4644 4645
            /* ignored */
            rn = "LLAddr";
            break;
T
ths 已提交
4646 4647 4648 4649 4650 4651
        default:
            goto die;
        }
        break;
    case 18:
        switch (sel) {
4652 4653
        case 0 ... 7:
            gen_op_mtc0_watchlo(sel);
T
ths 已提交
4654 4655
            rn = "WatchLo";
            break;
T
ths 已提交
4656 4657 4658 4659 4660 4661
        default:
            goto die;
        }
        break;
    case 19:
        switch (sel) {
4662 4663
        case 0 ... 7:
            gen_op_mtc0_watchhi(sel);
T
ths 已提交
4664 4665
            rn = "WatchHi";
            break;
T
ths 已提交
4666 4667 4668 4669 4670 4671 4672
        default:
            goto die;
        }
        break;
    case 20:
        switch (sel) {
        case 0:
4673
            check_insn(env, ctx, ISA_MIPS3);
4674
            gen_op_mtc0_xcontext();
T
ths 已提交
4675 4676
            rn = "XContext";
            break;
T
ths 已提交
4677 4678 4679 4680 4681 4682 4683 4684
        default:
            goto die;
        }
        break;
    case 21:
       /* Officially reserved, but sel 0 is used for R1x000 framemask */
        switch (sel) {
        case 0:
T
ths 已提交
4685 4686 4687
            gen_op_mtc0_framemask();
            rn = "Framemask";
            break;
T
ths 已提交
4688 4689 4690 4691 4692 4693 4694
        default:
            goto die;
        }
        break;
    case 22:
        /* ignored */
        rn = "Diagnostic"; /* implementation dependent */
T
ths 已提交
4695
        break;
T
ths 已提交
4696 4697 4698
    case 23:
        switch (sel) {
        case 0:
T
ths 已提交
4699
            gen_op_mtc0_debug(); /* EJTAG support */
4700 4701 4702
            /* BS_STOP isn't good enough here, hflags may have changed. */
            gen_save_pc(ctx->pc + 4);
            ctx->bstate = BS_EXCP;
T
ths 已提交
4703 4704
            rn = "Debug";
            break;
T
ths 已提交
4705
        case 1:
4706
//            gen_op_mtc0_tracecontrol(); /* PDtrace support */
4707 4708
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
4709 4710
            rn = "TraceControl";
//            break;
T
ths 已提交
4711
        case 2:
4712
//            gen_op_mtc0_tracecontrol2(); /* PDtrace support */
4713 4714
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
4715 4716
            rn = "TraceControl2";
//            break;
T
ths 已提交
4717
        case 3:
4718
//            gen_op_mtc0_usertracedata(); /* PDtrace support */
4719 4720
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
4721 4722
            rn = "UserTraceData";
//            break;
T
ths 已提交
4723
        case 4:
4724
//            gen_op_mtc0_debug(); /* PDtrace support */
4725 4726
            /* Stop translation as we may have switched the execution mode */
            ctx->bstate = BS_STOP;
T
ths 已提交
4727 4728
            rn = "TraceBPC";
//            break;
T
ths 已提交
4729 4730 4731 4732 4733 4734 4735
        default:
            goto die;
        }
        break;
    case 24:
        switch (sel) {
        case 0:
4736
            gen_op_mtc0_depc(); /* EJTAG support */
T
ths 已提交
4737 4738
            rn = "DEPC";
            break;
T
ths 已提交
4739 4740 4741 4742 4743 4744 4745
        default:
            goto die;
        }
        break;
    case 25:
        switch (sel) {
        case 0:
T
ths 已提交
4746 4747 4748
            gen_op_mtc0_performance0();
            rn = "Performance0";
            break;
T
ths 已提交
4749
        case 1:
4750
//            gen_op_mtc0_performance1();
T
ths 已提交
4751 4752
            rn = "Performance1";
//            break;
T
ths 已提交
4753
        case 2:
4754
//            gen_op_mtc0_performance2();
T
ths 已提交
4755 4756
            rn = "Performance2";
//            break;
T
ths 已提交
4757
        case 3:
4758
//            gen_op_mtc0_performance3();
T
ths 已提交
4759 4760
            rn = "Performance3";
//            break;
T
ths 已提交
4761
        case 4:
4762
//            gen_op_mtc0_performance4();
T
ths 已提交
4763 4764
            rn = "Performance4";
//            break;
T
ths 已提交
4765
        case 5:
4766
//            gen_op_mtc0_performance5();
T
ths 已提交
4767 4768
            rn = "Performance5";
//            break;
T
ths 已提交
4769
        case 6:
4770
//            gen_op_mtc0_performance6();
T
ths 已提交
4771 4772
            rn = "Performance6";
//            break;
T
ths 已提交
4773
        case 7:
4774
//            gen_op_mtc0_performance7();
T
ths 已提交
4775 4776
            rn = "Performance7";
//            break;
T
ths 已提交
4777 4778 4779
        default:
            goto die;
        }
T
ths 已提交
4780
        break;
T
ths 已提交
4781
    case 26:
T
ths 已提交
4782
        /* ignored */
T
ths 已提交
4783
        rn = "ECC";
T
ths 已提交
4784
        break;
T
ths 已提交
4785 4786 4787
    case 27:
        switch (sel) {
        case 0 ... 3:
T
ths 已提交
4788 4789 4790
            /* ignored */
            rn = "CacheErr";
            break;
T
ths 已提交
4791 4792 4793
        default:
            goto die;
        }
T
ths 已提交
4794
        break;
T
ths 已提交
4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807
    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 已提交
4808
            gen_op_mtc0_datalo();
T
ths 已提交
4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827
            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 已提交
4828
            gen_op_mtc0_datahi();
T
ths 已提交
4829 4830 4831 4832 4833 4834
            rn = "DataHi";
            break;
        default:
            rn = "invalid sel";
            goto die;
        }
T
ths 已提交
4835
        break;
T
ths 已提交
4836 4837 4838
    case 30:
        switch (sel) {
        case 0:
4839
            gen_op_mtc0_errorepc();
T
ths 已提交
4840 4841
            rn = "ErrorEPC";
            break;
T
ths 已提交
4842 4843 4844 4845 4846 4847 4848
        default:
            goto die;
        }
        break;
    case 31:
        switch (sel) {
        case 0:
T
ths 已提交
4849 4850 4851
            gen_op_mtc0_desave(); /* EJTAG support */
            rn = "DESAVE";
            break;
T
ths 已提交
4852 4853 4854
        default:
            goto die;
        }
T
ths 已提交
4855 4856
        /* Stop translation as we may have switched the execution mode */
        ctx->bstate = BS_STOP;
T
ths 已提交
4857 4858
        break;
    default:
T
ths 已提交
4859
        goto die;
T
ths 已提交
4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877
    }
#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);
}
4878
#endif /* TARGET_MIPS64 */
T
ths 已提交
4879

4880 4881 4882 4883 4884 4885 4886 4887
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))))
4888
        tcg_gen_movi_tl(cpu_T[0], -1);
4889 4890
    else if ((env->CP0_VPEControl & (0xff << CP0VPECo_TargTC)) >
             (env->mvp->CP0_MVPConf0 & (0xff << CP0MVPC0_PTC)))
4891
        tcg_gen_movi_tl(cpu_T[0], -1);
4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 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 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 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 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207
    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);
}

5208
static void gen_cp0 (CPUState *env, DisasContext *ctx, uint32_t opc, int rt, int rd)
B
bellard 已提交
5209
{
T
ths 已提交
5210
    const char *opn = "ldst";
B
bellard 已提交
5211 5212 5213 5214

    switch (opc) {
    case OPC_MFC0:
        if (rt == 0) {
5215
            /* Treat as NOP. */
B
bellard 已提交
5216 5217
            return;
        }
5218
        gen_mfc0(env, ctx, rd, ctx->opcode & 0x7);
5219
        gen_store_gpr(cpu_T[0], rt);
B
bellard 已提交
5220 5221 5222
        opn = "mfc0";
        break;
    case OPC_MTC0:
5223
        gen_load_gpr(cpu_T[0], rt);
5224
        save_cpu_state(ctx, 1);
5225
        gen_mtc0(env, ctx, rd, ctx->opcode & 0x7);
B
bellard 已提交
5226 5227
        opn = "mtc0";
        break;
5228
#if defined(TARGET_MIPS64)
T
ths 已提交
5229
    case OPC_DMFC0:
5230
        check_insn(env, ctx, ISA_MIPS3);
T
ths 已提交
5231
        if (rt == 0) {
5232
            /* Treat as NOP. */
T
ths 已提交
5233 5234
            return;
        }
5235
        gen_dmfc0(env, ctx, rd, ctx->opcode & 0x7);
5236
        gen_store_gpr(cpu_T[0], rt);
T
ths 已提交
5237 5238 5239
        opn = "dmfc0";
        break;
    case OPC_DMTC0:
5240
        check_insn(env, ctx, ISA_MIPS3);
5241
        gen_load_gpr(cpu_T[0], rt);
5242
        save_cpu_state(ctx, 1);
5243
        gen_dmtc0(env, ctx, rd, ctx->opcode & 0x7);
T
ths 已提交
5244 5245
        opn = "dmtc0";
        break;
5246
#endif
5247
    case OPC_MFTR:
5248
        check_insn(env, ctx, ASE_MT);
5249 5250 5251 5252 5253 5254
        if (rd == 0) {
            /* Treat as NOP. */
            return;
        }
        gen_mftr(env, ctx, rt, (ctx->opcode >> 5) & 1,
                 ctx->opcode & 0x7, (ctx->opcode >> 4) & 1);
5255
        gen_store_gpr(cpu_T[0], rd);
5256 5257 5258
        opn = "mftr";
        break;
    case OPC_MTTR:
5259
        check_insn(env, ctx, ASE_MT);
5260
        gen_load_gpr(cpu_T[0], rt);
5261 5262 5263 5264
        gen_mttr(env, ctx, rd, (ctx->opcode >> 5) & 1,
                 ctx->opcode & 0x7, (ctx->opcode >> 4) & 1);
        opn = "mttr";
        break;
B
bellard 已提交
5265 5266
    case OPC_TLBWI:
        opn = "tlbwi";
5267
        if (!env->tlb->do_tlbwi)
5268 5269
            goto die;
        gen_op_tlbwi();
B
bellard 已提交
5270 5271 5272
        break;
    case OPC_TLBWR:
        opn = "tlbwr";
5273
        if (!env->tlb->do_tlbwr)
5274 5275
            goto die;
        gen_op_tlbwr();
B
bellard 已提交
5276 5277 5278
        break;
    case OPC_TLBP:
        opn = "tlbp";
5279
        if (!env->tlb->do_tlbp)
5280 5281
            goto die;
        gen_op_tlbp();
B
bellard 已提交
5282 5283 5284
        break;
    case OPC_TLBR:
        opn = "tlbr";
5285
        if (!env->tlb->do_tlbr)
5286 5287
            goto die;
        gen_op_tlbr();
B
bellard 已提交
5288 5289 5290
        break;
    case OPC_ERET:
        opn = "eret";
5291
        check_insn(env, ctx, ISA_MIPS2);
5292
        save_cpu_state(ctx, 1);
B
bellard 已提交
5293 5294 5295 5296 5297
        gen_op_eret();
        ctx->bstate = BS_EXCP;
        break;
    case OPC_DERET:
        opn = "deret";
5298
        check_insn(env, ctx, ISA_MIPS32);
B
bellard 已提交
5299
        if (!(ctx->hflags & MIPS_HFLAG_DM)) {
5300
            MIPS_INVAL(opn);
B
bellard 已提交
5301 5302
            generate_exception(ctx, EXCP_RI);
        } else {
5303
            save_cpu_state(ctx, 1);
B
bellard 已提交
5304 5305 5306 5307
            gen_op_deret();
            ctx->bstate = BS_EXCP;
        }
        break;
B
bellard 已提交
5308 5309
    case OPC_WAIT:
        opn = "wait";
5310
        check_insn(env, ctx, ISA_MIPS3 | ISA_MIPS32);
B
bellard 已提交
5311 5312 5313 5314 5315 5316 5317
        /* 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 已提交
5318
    default:
5319
 die:
5320
        MIPS_INVAL(opn);
B
bellard 已提交
5321 5322 5323 5324 5325 5326
        generate_exception(ctx, EXCP_RI);
        return;
    }
    MIPS_DEBUG("%s %s %d", opn, regnames[rt], rd);
}

B
bellard 已提交
5327
/* CP1 Branches (before delay slot) */
5328
static void gen_compute_branch1 (CPUState *env, DisasContext *ctx, uint32_t op,
5329
                                 int32_t cc, int32_t offset)
B
bellard 已提交
5330 5331
{
    target_ulong btarget;
5332
    const char *opn = "cp1 cond branch";
B
bellard 已提交
5333

5334 5335 5336
    if (cc != 0)
        check_insn(env, ctx, ISA_MIPS4 | ISA_MIPS32);

B
bellard 已提交
5337 5338
    btarget = ctx->pc + 4 + offset;

5339 5340
    switch (op) {
    case OPC_BC1F:
5341
        gen_op_bc1f(cc);
5342
        opn = "bc1f";
B
bellard 已提交
5343
        goto not_likely;
5344
    case OPC_BC1FL:
5345
        gen_op_bc1f(cc);
5346
        opn = "bc1fl";
B
bellard 已提交
5347
        goto likely;
5348
    case OPC_BC1T:
5349
        gen_op_bc1t(cc);
5350
        opn = "bc1t";
5351
        goto not_likely;
5352
    case OPC_BC1TL:
5353
        gen_op_bc1t(cc);
5354
        opn = "bc1tl";
B
bellard 已提交
5355 5356
    likely:
        ctx->hflags |= MIPS_HFLAG_BL;
5357
        tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUState, bcond));
B
bellard 已提交
5358
        break;
5359
    case OPC_BC1FANY2:
5360 5361
        gen_op_bc1any2f(cc);
        opn = "bc1any2f";
5362 5363
        goto not_likely;
    case OPC_BC1TANY2:
5364 5365
        gen_op_bc1any2t(cc);
        opn = "bc1any2t";
5366 5367
        goto not_likely;
    case OPC_BC1FANY4:
5368 5369
        gen_op_bc1any4f(cc);
        opn = "bc1any4f";
5370 5371
        goto not_likely;
    case OPC_BC1TANY4:
5372 5373
        gen_op_bc1any4t(cc);
        opn = "bc1any4t";
5374 5375
    not_likely:
        ctx->hflags |= MIPS_HFLAG_BC;
5376
        tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUState, bcond));
5377 5378
        break;
    default:
5379
        MIPS_INVAL(opn);
5380
        generate_exception (ctx, EXCP_RI);
B
bellard 已提交
5381 5382
        return;
    }
5383
    MIPS_DEBUG("%s: cond %02x target " TARGET_FMT_lx, opn,
B
bellard 已提交
5384 5385 5386 5387
               ctx->hflags, btarget);
    ctx->btarget = btarget;
}

B
bellard 已提交
5388
/* Coprocessor 1 (FPU) */
5389 5390 5391

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

5392
static void gen_cp1 (DisasContext *ctx, uint32_t opc, int rt, int fs)
B
bellard 已提交
5393
{
5394
    const char *opn = "cp1 move";
B
bellard 已提交
5395 5396 5397 5398 5399

    switch (opc) {
    case OPC_MFC1:
        GEN_LOAD_FREG_FTN(WT0, fs);
        gen_op_mfc1();
5400
        gen_store_gpr(cpu_T[0], rt);
B
bellard 已提交
5401 5402 5403
        opn = "mfc1";
        break;
    case OPC_MTC1:
5404
        gen_load_gpr(cpu_T[0], rt);
B
bellard 已提交
5405 5406 5407 5408 5409
        gen_op_mtc1();
        GEN_STORE_FTN_FREG(fs, WT0);
        opn = "mtc1";
        break;
    case OPC_CFC1:
5410
        gen_op_cfc1(fs);
5411
        gen_store_gpr(cpu_T[0], rt);
B
bellard 已提交
5412 5413 5414
        opn = "cfc1";
        break;
    case OPC_CTC1:
5415
        gen_load_gpr(cpu_T[0], rt);
5416
        gen_op_ctc1(fs);
B
bellard 已提交
5417 5418
        opn = "ctc1";
        break;
T
ths 已提交
5419
    case OPC_DMFC1:
5420 5421
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_dmfc1();
5422
        gen_store_gpr(cpu_T[0], rt);
5423 5424
        opn = "dmfc1";
        break;
T
ths 已提交
5425
    case OPC_DMTC1:
5426
        gen_load_gpr(cpu_T[0], rt);
5427 5428 5429 5430 5431 5432 5433
        gen_op_dmtc1();
        GEN_STORE_FTN_FREG(fs, DT0);
        opn = "dmtc1";
        break;
    case OPC_MFHC1:
        GEN_LOAD_FREG_FTN(WTH0, fs);
        gen_op_mfhc1();
5434
        gen_store_gpr(cpu_T[0], rt);
5435 5436 5437
        opn = "mfhc1";
        break;
    case OPC_MTHC1:
5438
        gen_load_gpr(cpu_T[0], rt);
5439 5440 5441 5442
        gen_op_mthc1();
        GEN_STORE_FTN_FREG(fs, WTH0);
        opn = "mthc1";
        break;
B
bellard 已提交
5443
    default:
5444
        MIPS_INVAL(opn);
5445
        generate_exception (ctx, EXCP_RI);
B
bellard 已提交
5446 5447 5448 5449 5450
        return;
    }
    MIPS_DEBUG("%s %s %s", opn, regnames[rt], fregnames[fs]);
}

5451 5452
static void gen_movci (DisasContext *ctx, int rd, int rs, int cc, int tf)
{
T
ths 已提交
5453 5454 5455 5456 5457
    TCGv r_ptr = tcg_temp_new(TCG_TYPE_PTR);
    TCGv r_tmp = new_tmp();
    TCGv t0 = tcg_temp_new(TCG_TYPE_TL);
    TCGv t1 = tcg_temp_new(TCG_TYPE_TL);
    int l1 = gen_new_label();
5458
    uint32_t ccbit;
T
ths 已提交
5459
    TCGCond cond;
B
bellard 已提交
5460

T
ths 已提交
5461
    if (cc)
5462
        ccbit = 1 << (24 + cc);
T
ths 已提交
5463
    else
5464
        ccbit = 1 << 23;
T
ths 已提交
5465 5466
    if (tf)
        cond = TCG_COND_NE;
5467
    else
T
ths 已提交
5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479
        cond = TCG_COND_EQ;

    gen_load_gpr(t0, rd);
    gen_load_gpr(t1, rs);
    tcg_gen_ld_ptr(r_ptr, cpu_env, offsetof(CPUState, fpu));
    tcg_gen_ld_i32(r_tmp, r_ptr, offsetof(CPUMIPSFPUContext, fcr31));
    tcg_gen_andi_i32(r_tmp, r_tmp, ccbit);
    tcg_gen_brcond_i32(cond, r_tmp, tcg_const_i32(0), l1);
    tcg_gen_mov_tl(t0, t1);
    gen_set_label(l1);
    dead_tmp(r_tmp);
    gen_store_gpr(t0, rd);
5480 5481 5482 5483 5484 5485 5486
}

#define GEN_MOVCF(fmt)                                                \
static void glue(gen_movcf_, fmt) (DisasContext *ctx, int cc, int tf) \
{                                                                     \
    uint32_t ccbit;                                                   \
                                                                      \
T
ths 已提交
5487
    if (cc) {                                                         \
5488
        ccbit = 1 << (24 + cc);                                       \
T
ths 已提交
5489
    } else                                                            \
5490 5491 5492 5493 5494 5495 5496 5497 5498 5499
        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 已提交
5500

5501 5502
static void gen_farith (DisasContext *ctx, uint32_t op1,
                        int ft, int fs, int fd, int cc)
B
bellard 已提交
5503
{
5504
    const char *opn = "farith";
B
bellard 已提交
5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522
    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",
    };
5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541
    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;
5542 5543
    uint32_t func = ctx->opcode & 0x3f;

B
bellard 已提交
5544
    switch (ctx->opcode & FOP(0x3f, 0x1f)) {
5545 5546 5547 5548 5549 5550
    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";
5551
        optype = BINOP;
5552 5553 5554 5555 5556 5557 5558
        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";
5559
        optype = BINOP;
5560 5561 5562 5563 5564 5565 5566
        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";
5567
        optype = BINOP;
5568 5569 5570 5571 5572 5573 5574
        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";
5575
        optype = BINOP;
5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601
        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):
5602
        check_cp1_64bitmode(ctx);
5603 5604 5605 5606 5607 5608
        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):
5609
        check_cp1_64bitmode(ctx);
5610 5611 5612 5613 5614 5615
        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):
5616
        check_cp1_64bitmode(ctx);
5617 5618 5619 5620 5621 5622
        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):
5623
        check_cp1_64bitmode(ctx);
5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653
        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):
5654
        gen_load_gpr(cpu_T[0], ft);
5655 5656 5657 5658 5659 5660 5661
        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):
5662
        gen_load_gpr(cpu_T[0], ft);
5663 5664 5665 5666 5667 5668 5669
        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):
5670
        gen_load_gpr(cpu_T[0], ft);
5671 5672 5673 5674 5675 5676
        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 已提交
5677
    case FOP(21, 16):
5678
        check_cop1x(ctx);
T
ths 已提交
5679 5680 5681 5682 5683 5684
        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):
5685
        check_cop1x(ctx);
T
ths 已提交
5686 5687 5688 5689 5690 5691
        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):
5692
        check_cp1_64bitmode(ctx);
T
ths 已提交
5693 5694 5695 5696 5697 5698 5699
        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):
5700
        check_cp1_64bitmode(ctx);
T
ths 已提交
5701 5702 5703 5704 5705 5706
        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):
5707
        check_cp1_64bitmode(ctx);
T
ths 已提交
5708 5709 5710 5711 5712 5713
        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):
5714
        check_cp1_64bitmode(ctx);
T
ths 已提交
5715
        GEN_LOAD_FREG_FTN(WT0, fs);
T
ths 已提交
5716
        GEN_LOAD_FREG_FTN(WT2, ft);
T
ths 已提交
5717 5718 5719 5720
        gen_op_float_rsqrt2_s();
        GEN_STORE_FTN_FREG(fd, WT2);
        opn = "rsqrt2.s";
        break;
5721
    case FOP(33, 16):
5722
        check_cp1_registers(ctx, fd);
5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734
        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):
5735
        check_cp1_64bitmode(ctx);
5736 5737 5738 5739 5740 5741
        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):
5742
        check_cp1_64bitmode(ctx);
5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766
        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);
5767
        if (ctx->opcode & (1 << 6)) {
5768
            check_cop1x(ctx);
5769 5770 5771 5772 5773 5774
            gen_cmpabs_s(func-48, cc);
            opn = condnames_abs[func-48];
        } else {
            gen_cmp_s(func-48, cc);
            opn = condnames[func-48];
        }
5775
        break;
B
bellard 已提交
5776
    case FOP(0, 17):
5777
        check_cp1_registers(ctx, fs | ft | fd);
B
bellard 已提交
5778 5779 5780 5781 5782
        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";
5783
        optype = BINOP;
B
bellard 已提交
5784 5785
        break;
    case FOP(1, 17):
5786
        check_cp1_registers(ctx, fs | ft | fd);
B
bellard 已提交
5787 5788 5789 5790 5791
        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";
5792
        optype = BINOP;
B
bellard 已提交
5793 5794
        break;
    case FOP(2, 17):
5795
        check_cp1_registers(ctx, fs | ft | fd);
B
bellard 已提交
5796 5797 5798 5799 5800
        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";
5801
        optype = BINOP;
B
bellard 已提交
5802 5803
        break;
    case FOP(3, 17):
5804
        check_cp1_registers(ctx, fs | ft | fd);
B
bellard 已提交
5805 5806 5807 5808 5809
        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";
5810
        optype = BINOP;
B
bellard 已提交
5811 5812
        break;
    case FOP(4, 17):
5813
        check_cp1_registers(ctx, fs | fd);
B
bellard 已提交
5814 5815 5816 5817 5818 5819
        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):
5820
        check_cp1_registers(ctx, fs | fd);
B
bellard 已提交
5821 5822 5823 5824 5825 5826
        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):
5827
        check_cp1_registers(ctx, fs | fd);
B
bellard 已提交
5828 5829 5830 5831 5832 5833
        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):
5834
        check_cp1_registers(ctx, fs | fd);
B
bellard 已提交
5835 5836 5837 5838 5839
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_chs_d();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "neg.d";
        break;
5840
    case FOP(8, 17):
5841
        check_cp1_64bitmode(ctx);
5842 5843 5844 5845 5846 5847
        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):
5848
        check_cp1_64bitmode(ctx);
5849 5850 5851 5852 5853 5854
        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):
5855
        check_cp1_64bitmode(ctx);
5856 5857 5858 5859 5860 5861
        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):
5862
        check_cp1_64bitmode(ctx);
5863 5864 5865 5866 5867
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_floorl_d();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "floor.l.d";
        break;
B
bellard 已提交
5868
    case FOP(12, 17):
5869
        check_cp1_registers(ctx, fs);
B
bellard 已提交
5870 5871 5872 5873 5874 5875
        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):
5876
        check_cp1_registers(ctx, fs);
B
bellard 已提交
5877 5878 5879 5880 5881 5882
        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):
5883
        check_cp1_registers(ctx, fs);
B
bellard 已提交
5884 5885 5886 5887 5888 5889
        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):
5890
        check_cp1_registers(ctx, fs);
B
bellard 已提交
5891 5892 5893
        GEN_LOAD_FREG_FTN(DT0, fs);
        gen_op_float_floorw_d();
        GEN_STORE_FTN_FREG(fd, WT2);
5894
        opn = "floor.w.d";
B
bellard 已提交
5895
        break;
5896
    case FOP(17, 17):
5897
        gen_load_gpr(cpu_T[0], ft);
5898 5899 5900
        GEN_LOAD_FREG_FTN(DT0, fs);
        GEN_LOAD_FREG_FTN(DT2, fd);
        gen_movcf_d(ctx, (ft >> 2) & 0x7, ft & 0x1);
5901
        GEN_STORE_FTN_FREG(fd, DT2);
5902
        opn = "movcf.d";
5903
        break;
5904
    case FOP(18, 17):
5905
        gen_load_gpr(cpu_T[0], ft);
5906 5907 5908
        GEN_LOAD_FREG_FTN(DT0, fs);
        GEN_LOAD_FREG_FTN(DT2, fd);
        gen_op_float_movz_d();
B
bellard 已提交
5909
        GEN_STORE_FTN_FREG(fd, DT2);
5910 5911 5912
        opn = "movz.d";
        break;
    case FOP(19, 17):
5913
        gen_load_gpr(cpu_T[0], ft);
5914 5915 5916 5917 5918
        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 已提交
5919
        break;
T
ths 已提交
5920
    case FOP(21, 17):
5921
        check_cp1_64bitmode(ctx);
T
ths 已提交
5922 5923 5924 5925 5926 5927
        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):
5928
        check_cp1_64bitmode(ctx);
T
ths 已提交
5929 5930 5931 5932 5933 5934
        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):
5935
        check_cp1_64bitmode(ctx);
T
ths 已提交
5936 5937 5938 5939 5940 5941 5942
        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):
5943
        check_cp1_64bitmode(ctx);
T
ths 已提交
5944 5945 5946 5947 5948 5949
        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):
5950
        check_cp1_64bitmode(ctx);
T
ths 已提交
5951 5952 5953 5954 5955 5956
        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):
5957
        check_cp1_64bitmode(ctx);
T
ths 已提交
5958 5959 5960 5961 5962 5963
        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 已提交
5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981
    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);
5982
        if (ctx->opcode & (1 << 6)) {
5983 5984
            check_cop1x(ctx);
            check_cp1_registers(ctx, fs | ft);
5985 5986 5987
            gen_cmpabs_d(func-48, cc);
            opn = condnames_abs[func-48];
        } else {
5988
            check_cp1_registers(ctx, fs | ft);
5989 5990 5991
            gen_cmp_d(func-48, cc);
            opn = condnames[func-48];
        }
B
bellard 已提交
5992
        break;
5993
    case FOP(32, 17):
5994
        check_cp1_registers(ctx, fs);
5995 5996 5997 5998 5999 6000
        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):
6001
        check_cp1_registers(ctx, fs);
6002 6003 6004 6005 6006 6007
        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):
6008
        check_cp1_64bitmode(ctx);
6009 6010 6011 6012 6013 6014
        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 已提交
6015
        GEN_LOAD_FREG_FTN(WT0, fs);
6016
        gen_op_float_cvts_w();
B
bellard 已提交
6017
        GEN_STORE_FTN_FREG(fd, WT2);
6018
        opn = "cvt.s.w";
B
bellard 已提交
6019
        break;
6020
    case FOP(33, 20):
6021
        check_cp1_registers(ctx, fd);
B
bellard 已提交
6022
        GEN_LOAD_FREG_FTN(WT0, fs);
6023 6024 6025 6026 6027
        gen_op_float_cvtd_w();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "cvt.d.w";
        break;
    case FOP(32, 21):
6028
        check_cp1_64bitmode(ctx);
6029 6030 6031 6032 6033 6034
        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):
6035
        check_cp1_64bitmode(ctx);
6036 6037 6038 6039 6040 6041
        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):
6042
        check_cp1_64bitmode(ctx);
6043 6044 6045 6046 6047 6048 6049 6050
        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):
6051
        check_cp1_64bitmode(ctx);
6052 6053
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WTH0, fs);
B
bellard 已提交
6054
        GEN_LOAD_FREG_FTN(WT1, ft);
6055 6056
        GEN_LOAD_FREG_FTN(WTH1, ft);
        gen_op_float_add_ps();
B
bellard 已提交
6057
        GEN_STORE_FTN_FREG(fd, WT2);
6058 6059
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "add.ps";
B
bellard 已提交
6060
        break;
6061
    case FOP(1, 22):
6062
        check_cp1_64bitmode(ctx);
B
bellard 已提交
6063
        GEN_LOAD_FREG_FTN(WT0, fs);
6064
        GEN_LOAD_FREG_FTN(WTH0, fs);
B
bellard 已提交
6065
        GEN_LOAD_FREG_FTN(WT1, ft);
6066 6067
        GEN_LOAD_FREG_FTN(WTH1, ft);
        gen_op_float_sub_ps();
B
bellard 已提交
6068
        GEN_STORE_FTN_FREG(fd, WT2);
6069 6070
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "sub.ps";
B
bellard 已提交
6071
        break;
6072
    case FOP(2, 22):
6073
        check_cp1_64bitmode(ctx);
B
bellard 已提交
6074
        GEN_LOAD_FREG_FTN(WT0, fs);
6075
        GEN_LOAD_FREG_FTN(WTH0, fs);
B
bellard 已提交
6076
        GEN_LOAD_FREG_FTN(WT1, ft);
6077 6078
        GEN_LOAD_FREG_FTN(WTH1, ft);
        gen_op_float_mul_ps();
B
bellard 已提交
6079
        GEN_STORE_FTN_FREG(fd, WT2);
6080 6081
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "mul.ps";
B
bellard 已提交
6082
        break;
6083
    case FOP(5, 22):
6084
        check_cp1_64bitmode(ctx);
B
bellard 已提交
6085
        GEN_LOAD_FREG_FTN(WT0, fs);
6086 6087
        GEN_LOAD_FREG_FTN(WTH0, fs);
        gen_op_float_abs_ps();
B
bellard 已提交
6088
        GEN_STORE_FTN_FREG(fd, WT2);
6089 6090
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "abs.ps";
B
bellard 已提交
6091
        break;
6092
    case FOP(6, 22):
6093
        check_cp1_64bitmode(ctx);
B
bellard 已提交
6094
        GEN_LOAD_FREG_FTN(WT0, fs);
6095 6096
        GEN_LOAD_FREG_FTN(WTH0, fs);
        gen_op_float_mov_ps();
B
bellard 已提交
6097
        GEN_STORE_FTN_FREG(fd, WT2);
6098 6099
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "mov.ps";
B
bellard 已提交
6100
        break;
6101
    case FOP(7, 22):
6102
        check_cp1_64bitmode(ctx);
B
bellard 已提交
6103
        GEN_LOAD_FREG_FTN(WT0, fs);
6104 6105
        GEN_LOAD_FREG_FTN(WTH0, fs);
        gen_op_float_chs_ps();
B
bellard 已提交
6106
        GEN_STORE_FTN_FREG(fd, WT2);
6107 6108
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "neg.ps";
B
bellard 已提交
6109
        break;
6110
    case FOP(17, 22):
6111
        check_cp1_64bitmode(ctx);
6112
        gen_load_gpr(cpu_T[0], ft);
B
bellard 已提交
6113
        GEN_LOAD_FREG_FTN(WT0, fs);
6114 6115 6116 6117
        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 已提交
6118
        GEN_STORE_FTN_FREG(fd, WT2);
6119 6120
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "movcf.ps";
B
bellard 已提交
6121
        break;
6122
    case FOP(18, 22):
6123
        check_cp1_64bitmode(ctx);
6124
        gen_load_gpr(cpu_T[0], ft);
B
bellard 已提交
6125
        GEN_LOAD_FREG_FTN(WT0, fs);
6126 6127 6128 6129
        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 已提交
6130
        GEN_STORE_FTN_FREG(fd, WT2);
6131 6132
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "movz.ps";
B
bellard 已提交
6133
        break;
6134
    case FOP(19, 22):
6135
        check_cp1_64bitmode(ctx);
6136
        gen_load_gpr(cpu_T[0], ft);
B
bellard 已提交
6137
        GEN_LOAD_FREG_FTN(WT0, fs);
6138 6139 6140 6141
        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 已提交
6142
        GEN_STORE_FTN_FREG(fd, WT2);
6143 6144
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "movn.ps";
B
bellard 已提交
6145
        break;
6146
    case FOP(24, 22):
6147
        check_cp1_64bitmode(ctx);
6148 6149 6150 6151
        GEN_LOAD_FREG_FTN(WT0, ft);
        GEN_LOAD_FREG_FTN(WTH0, ft);
        GEN_LOAD_FREG_FTN(WT1, fs);
        GEN_LOAD_FREG_FTN(WTH1, fs);
6152 6153 6154 6155 6156
        gen_op_float_addr_ps();
        GEN_STORE_FTN_FREG(fd, WT2);
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "addr.ps";
        break;
T
ths 已提交
6157
    case FOP(26, 22):
6158
        check_cp1_64bitmode(ctx);
6159 6160 6161 6162
        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 已提交
6163 6164 6165 6166 6167 6168
        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):
6169
        check_cp1_64bitmode(ctx);
T
ths 已提交
6170 6171 6172 6173 6174 6175 6176 6177 6178 6179
        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):
6180
        check_cp1_64bitmode(ctx);
T
ths 已提交
6181 6182 6183 6184 6185 6186 6187 6188
        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):
6189
        check_cp1_64bitmode(ctx);
T
ths 已提交
6190 6191 6192 6193 6194 6195 6196 6197
        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):
6198
        check_cp1_64bitmode(ctx);
T
ths 已提交
6199 6200
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WTH0, fs);
T
ths 已提交
6201 6202
        GEN_LOAD_FREG_FTN(WT2, ft);
        GEN_LOAD_FREG_FTN(WTH2, ft);
T
ths 已提交
6203 6204 6205 6206 6207
        gen_op_float_rsqrt2_ps();
        GEN_STORE_FTN_FREG(fd, WT2);
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "rsqrt2.ps";
        break;
6208
    case FOP(32, 22):
6209
        check_cp1_64bitmode(ctx);
6210 6211
        GEN_LOAD_FREG_FTN(WTH0, fs);
        gen_op_float_cvts_pu();
6212
        GEN_STORE_FTN_FREG(fd, WT2);
6213
        opn = "cvt.s.pu";
6214
        break;
6215
    case FOP(36, 22):
6216
        check_cp1_64bitmode(ctx);
B
bellard 已提交
6217
        GEN_LOAD_FREG_FTN(WT0, fs);
6218 6219
        GEN_LOAD_FREG_FTN(WTH0, fs);
        gen_op_float_cvtpw_ps();
B
bellard 已提交
6220
        GEN_STORE_FTN_FREG(fd, WT2);
6221 6222
        GEN_STORE_FTN_FREG(fd, WTH2);
        opn = "cvt.pw.ps";
B
bellard 已提交
6223
        break;
6224
    case FOP(40, 22):
6225
        check_cp1_64bitmode(ctx);
B
bellard 已提交
6226
        GEN_LOAD_FREG_FTN(WT0, fs);
6227
        gen_op_float_cvts_pl();
B
bellard 已提交
6228
        GEN_STORE_FTN_FREG(fd, WT2);
6229
        opn = "cvt.s.pl";
B
bellard 已提交
6230
        break;
6231
    case FOP(44, 22):
6232
        check_cp1_64bitmode(ctx);
6233 6234 6235 6236 6237
        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 已提交
6238
        break;
6239
    case FOP(45, 22):
6240
        check_cp1_64bitmode(ctx);
B
bellard 已提交
6241
        GEN_LOAD_FREG_FTN(WT0, fs);
6242 6243 6244 6245 6246 6247
        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):
6248
        check_cp1_64bitmode(ctx);
6249
        GEN_LOAD_FREG_FTN(WTH0, fs);
B
bellard 已提交
6250
        GEN_LOAD_FREG_FTN(WT1, ft);
6251 6252 6253 6254 6255
        gen_op_float_pul_ps();
        GEN_STORE_FTN_FREG(fd, DT2);
        opn = "pul.ps";
        break;
    case FOP(47, 22):
6256
        check_cp1_64bitmode(ctx);
6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278
        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):
6279
        check_cp1_64bitmode(ctx);
6280 6281 6282 6283
        GEN_LOAD_FREG_FTN(WT0, fs);
        GEN_LOAD_FREG_FTN(WTH0, fs);
        GEN_LOAD_FREG_FTN(WT1, ft);
        GEN_LOAD_FREG_FTN(WTH1, ft);
6284 6285 6286 6287 6288 6289 6290
        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 已提交
6291
        break;
6292
    default:
6293
        MIPS_INVAL(opn);
6294
        generate_exception (ctx, EXCP_RI);
B
bellard 已提交
6295 6296
        return;
    }
6297 6298
    switch (optype) {
    case BINOP:
B
bellard 已提交
6299
        MIPS_DEBUG("%s %s, %s, %s", opn, fregnames[fd], fregnames[fs], fregnames[ft]);
6300 6301 6302 6303 6304
        break;
    case CMPOP:
        MIPS_DEBUG("%s %s,%s", opn, fregnames[fs], fregnames[ft]);
        break;
    default:
B
bellard 已提交
6305
        MIPS_DEBUG("%s %s,%s", opn, fregnames[fd], fregnames[fs]);
6306 6307
        break;
    }
B
bellard 已提交
6308
}
B
bellard 已提交
6309

6310
/* Coprocessor 3 (FPU) */
6311 6312
static void gen_flt3_ldst (DisasContext *ctx, uint32_t opc,
                           int fd, int fs, int base, int index)
6313
{
6314
    const char *opn = "extended float load/store";
T
ths 已提交
6315
    int store = 0;
6316

T
ths 已提交
6317
    if (base == 0) {
6318
        gen_load_gpr(cpu_T[0], index);
T
ths 已提交
6319
    } else if (index == 0) {
6320
        gen_load_gpr(cpu_T[0], base);
T
ths 已提交
6321
    } else {
6322 6323
        gen_load_gpr(cpu_T[0], base);
        gen_load_gpr(cpu_T[1], index);
T
ths 已提交
6324 6325
        gen_op_addr_add();
    }
6326
    /* Don't do NOP if destination is zero: we must perform the actual
6327
       memory access. */
6328 6329
    switch (opc) {
    case OPC_LWXC1:
6330
        check_cop1x(ctx);
6331
        op_ldst_lwc1(ctx);
6332 6333 6334 6335
        GEN_STORE_FTN_FREG(fd, WT0);
        opn = "lwxc1";
        break;
    case OPC_LDXC1:
6336 6337
        check_cop1x(ctx);
        check_cp1_registers(ctx, fd);
6338
        op_ldst_ldc1(ctx);
6339 6340 6341 6342
        GEN_STORE_FTN_FREG(fd, DT0);
        opn = "ldxc1";
        break;
    case OPC_LUXC1:
6343
        check_cp1_64bitmode(ctx);
6344 6345 6346 6347 6348
        op_ldst(luxc1);
        GEN_STORE_FTN_FREG(fd, DT0);
        opn = "luxc1";
        break;
    case OPC_SWXC1:
6349
        check_cop1x(ctx);
T
ths 已提交
6350
        GEN_LOAD_FREG_FTN(WT0, fs);
6351
        op_ldst_swc1(ctx);
6352
        opn = "swxc1";
T
ths 已提交
6353
        store = 1;
6354 6355
        break;
    case OPC_SDXC1:
6356 6357
        check_cop1x(ctx);
        check_cp1_registers(ctx, fs);
T
ths 已提交
6358
        GEN_LOAD_FREG_FTN(DT0, fs);
6359
        op_ldst_sdc1(ctx);
6360
        opn = "sdxc1";
T
ths 已提交
6361
        store = 1;
6362 6363
        break;
    case OPC_SUXC1:
6364
        check_cp1_64bitmode(ctx);
T
ths 已提交
6365
        GEN_LOAD_FREG_FTN(DT0, fs);
6366 6367
        op_ldst(suxc1);
        opn = "suxc1";
T
ths 已提交
6368
        store = 1;
6369 6370
        break;
    default:
6371
        MIPS_INVAL(opn);
6372 6373 6374
        generate_exception(ctx, EXCP_RI);
        return;
    }
T
ths 已提交
6375 6376
    MIPS_DEBUG("%s %s, %s(%s)", opn, fregnames[store ? fs : fd],
               regnames[index], regnames[base]);
6377 6378
}

6379 6380
static void gen_flt3_arith (DisasContext *ctx, uint32_t opc,
                            int fd, int fr, int fs, int ft)
6381
{
6382
    const char *opn = "flt3_arith";
6383 6384 6385

    switch (opc) {
    case OPC_ALNV_PS:
6386
        check_cp1_64bitmode(ctx);
6387
        gen_load_gpr(cpu_T[0], fr);
6388 6389 6390 6391 6392 6393 6394
        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:
6395
        check_cop1x(ctx);
6396 6397 6398 6399 6400 6401 6402 6403
        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:
6404 6405
        check_cop1x(ctx);
        check_cp1_registers(ctx, fd | fs | ft | fr);
6406 6407 6408 6409 6410
        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);
6411 6412 6413
        opn = "madd.d";
        break;
    case OPC_MADD_PS:
6414
        check_cp1_64bitmode(ctx);
6415 6416 6417 6418 6419 6420 6421 6422 6423
        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);
6424 6425 6426
        opn = "madd.ps";
        break;
    case OPC_MSUB_S:
6427
        check_cop1x(ctx);
6428 6429 6430 6431 6432
        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);
6433 6434 6435
        opn = "msub.s";
        break;
    case OPC_MSUB_D:
6436 6437
        check_cop1x(ctx);
        check_cp1_registers(ctx, fd | fs | ft | fr);
6438 6439 6440 6441 6442
        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);
6443 6444 6445
        opn = "msub.d";
        break;
    case OPC_MSUB_PS:
6446
        check_cp1_64bitmode(ctx);
6447 6448 6449 6450 6451 6452 6453 6454 6455
        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);
6456 6457 6458
        opn = "msub.ps";
        break;
    case OPC_NMADD_S:
6459
        check_cop1x(ctx);
6460 6461 6462 6463 6464
        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);
6465 6466 6467
        opn = "nmadd.s";
        break;
    case OPC_NMADD_D:
6468 6469
        check_cop1x(ctx);
        check_cp1_registers(ctx, fd | fs | ft | fr);
6470 6471 6472 6473 6474
        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);
6475 6476 6477
        opn = "nmadd.d";
        break;
    case OPC_NMADD_PS:
6478
        check_cp1_64bitmode(ctx);
6479 6480 6481 6482 6483 6484 6485 6486 6487
        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);
6488 6489 6490
        opn = "nmadd.ps";
        break;
    case OPC_NMSUB_S:
6491
        check_cop1x(ctx);
6492 6493 6494 6495 6496
        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);
6497 6498 6499
        opn = "nmsub.s";
        break;
    case OPC_NMSUB_D:
6500 6501
        check_cop1x(ctx);
        check_cp1_registers(ctx, fd | fs | ft | fr);
6502 6503 6504 6505 6506
        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);
6507 6508 6509
        opn = "nmsub.d";
        break;
    case OPC_NMSUB_PS:
6510
        check_cp1_64bitmode(ctx);
6511 6512 6513 6514 6515 6516 6517 6518 6519
        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);
6520 6521
        opn = "nmsub.ps";
        break;
6522 6523
    default:
        MIPS_INVAL(opn);
6524 6525 6526 6527 6528
        generate_exception (ctx, EXCP_RI);
        return;
    }
    MIPS_DEBUG("%s %s, %s, %s, %s", opn, fregnames[fd], fregnames[fr],
               fregnames[fs], fregnames[ft]);
6529 6530 6531
}

/* ISA extensions (ASEs) */
B
bellard 已提交
6532 6533 6534
/* MIPS16 extension to MIPS32 */
/* SmartMIPS extension to MIPS32 */

6535
#if defined(TARGET_MIPS64)
B
bellard 已提交
6536 6537 6538 6539 6540

/* MDMX extension to MIPS64 */

#endif

6541
static void decode_opc (CPUState *env, DisasContext *ctx)
B
bellard 已提交
6542 6543 6544
{
    int32_t offset;
    int rs, rt, rd, sa;
6545
    uint32_t op, op1, op2;
B
bellard 已提交
6546 6547
    int16_t imm;

6548 6549
    /* make sure instructions are on a word boundary */
    if (ctx->pc & 0x3) {
6550
        env->CP0_BadVAddr = ctx->pc;
6551 6552 6553 6554
        generate_exception(ctx, EXCP_AdEL);
        return;
    }

6555
    /* Handle blikely not taken case */
B
bellard 已提交
6556
    if ((ctx->hflags & MIPS_HFLAG_BMASK) == MIPS_HFLAG_BL) {
6557 6558 6559
        TCGv r_tmp = tcg_temp_new(TCG_TYPE_TL);
        int l1 = gen_new_label();

T
ths 已提交
6560
        MIPS_DEBUG("blikely condition (" TARGET_FMT_lx ")", ctx->pc + 4);
6561 6562
        tcg_gen_ld_tl(r_tmp, cpu_env, offsetof(CPUState, bcond));
        tcg_gen_brcond_tl(TCG_COND_NE, r_tmp, tcg_const_tl(0), l1);
6563 6564 6565
        gen_op_save_state(ctx->hflags & ~MIPS_HFLAG_BMASK);
        gen_goto_tb(ctx, 1, ctx->pc + 4);
        gen_set_label(l1);
B
bellard 已提交
6566
    }
6567 6568 6569 6570 6571
    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 已提交
6572 6573
    imm = (int16_t)ctx->opcode;
    switch (op) {
6574 6575
    case OPC_SPECIAL:
        op1 = MASK_SPECIAL(ctx->opcode);
B
bellard 已提交
6576
        switch (op1) {
6577 6578
        case OPC_SLL:          /* Arithmetic with immediate */
        case OPC_SRL ... OPC_SRA:
6579
            gen_arith_imm(env, ctx, op1, rd, rt, sa);
6580
            break;
6581 6582
        case OPC_MOVZ ... OPC_MOVN:
            check_insn(env, ctx, ISA_MIPS4 | ISA_MIPS32);
6583 6584 6585 6586
        case OPC_SLLV:         /* Arithmetic */
        case OPC_SRLV ... OPC_SRAV:
        case OPC_ADD ... OPC_NOR:
        case OPC_SLT ... OPC_SLTU:
6587
            gen_arith(env, ctx, op1, rd, rs, rt);
6588 6589
            break;
        case OPC_MULT ... OPC_DIVU:
6590 6591 6592 6593 6594 6595
            if (sa) {
                check_insn(env, ctx, INSN_VR54XX);
                op1 = MASK_MUL_VR54XX(ctx->opcode);
                gen_mul_vr54xx(ctx, op1, rd, rs, rt);
            } else
                gen_muldiv(ctx, op1, rs, rt);
6596 6597 6598
            break;
        case OPC_JR ... OPC_JALR:
            gen_compute_branch(ctx, op1, rs, rd, sa);
B
bellard 已提交
6599
            return;
6600 6601 6602
        case OPC_TGE ... OPC_TEQ: /* Traps */
        case OPC_TNE:
            gen_trap(ctx, op1, rs, rt, -1);
B
bellard 已提交
6603
            break;
6604 6605 6606
        case OPC_MFHI:          /* Move from HI/LO */
        case OPC_MFLO:
            gen_HILO(ctx, op1, rd);
B
bellard 已提交
6607
            break;
6608 6609 6610
        case OPC_MTHI:
        case OPC_MTLO:          /* Move to HI/LO */
            gen_HILO(ctx, op1, rs);
B
bellard 已提交
6611
            break;
6612 6613 6614 6615 6616
        case OPC_PMON:          /* Pmon entry point, also R4010 selsl */
#ifdef MIPS_STRICT_STANDARD
            MIPS_INVAL("PMON / selsl");
            generate_exception(ctx, EXCP_RI);
#else
6617
            gen_op_pmon(sa);
6618
#endif
6619 6620
            break;
        case OPC_SYSCALL:
B
bellard 已提交
6621 6622
            generate_exception(ctx, EXCP_SYSCALL);
            break;
6623
        case OPC_BREAK:
B
bellard 已提交
6624 6625
            generate_exception(ctx, EXCP_BREAK);
            break;
6626 6627 6628 6629 6630
        case OPC_SPIM:
#ifdef MIPS_STRICT_STANDARD
            MIPS_INVAL("SPIM");
            generate_exception(ctx, EXCP_RI);
#else
6631 6632 6633
           /* Implemented as RI exception for now. */
            MIPS_INVAL("spim (unofficial)");
            generate_exception(ctx, EXCP_RI);
6634
#endif
B
bellard 已提交
6635
            break;
6636
        case OPC_SYNC:
6637
            /* Treat as NOP. */
B
bellard 已提交
6638
            break;
B
bellard 已提交
6639

6640
        case OPC_MOVCI:
6641
            check_insn(env, ctx, ISA_MIPS4 | ISA_MIPS32);
6642
            if (env->CP0_Config1 & (1 << CP0C1_FP)) {
6643
                save_cpu_state(ctx, 1);
6644
                check_cp1_enabled(ctx);
6645 6646 6647
                gen_movci(ctx, rd, rs, (ctx->opcode >> 18) & 0x7,
                          (ctx->opcode >> 16) & 1);
            } else {
6648
                generate_exception_err(ctx, EXCP_CpU, 1);
6649
            }
B
bellard 已提交
6650 6651
            break;

6652
#if defined(TARGET_MIPS64)
6653 6654 6655 6656 6657
       /* MIPS64 specific opcodes */
        case OPC_DSLL:
        case OPC_DSRL ... OPC_DSRA:
        case OPC_DSLL32:
        case OPC_DSRL32 ... OPC_DSRA32:
6658 6659 6660
            check_insn(env, ctx, ISA_MIPS3);
            check_mips_64(ctx);
            gen_arith_imm(env, ctx, op1, rd, rt, sa);
6661 6662 6663 6664
            break;
        case OPC_DSLLV:
        case OPC_DSRLV ... OPC_DSRAV:
        case OPC_DADD ... OPC_DSUBU:
6665 6666 6667
            check_insn(env, ctx, ISA_MIPS3);
            check_mips_64(ctx);
            gen_arith(env, ctx, op1, rd, rs, rt);
6668 6669
            break;
        case OPC_DMULT ... OPC_DDIVU:
6670 6671
            check_insn(env, ctx, ISA_MIPS3);
            check_mips_64(ctx);
6672 6673
            gen_muldiv(ctx, op1, rs, rt);
            break;
B
bellard 已提交
6674 6675 6676 6677 6678 6679 6680
#endif
        default:            /* Invalid */
            MIPS_INVAL("special");
            generate_exception(ctx, EXCP_RI);
            break;
        }
        break;
6681 6682
    case OPC_SPECIAL2:
        op1 = MASK_SPECIAL2(ctx->opcode);
B
bellard 已提交
6683
        switch (op1) {
6684 6685
        case OPC_MADD ... OPC_MADDU: /* Multiply and add/sub */
        case OPC_MSUB ... OPC_MSUBU:
6686
            check_insn(env, ctx, ISA_MIPS32);
6687
            gen_muldiv(ctx, op1, rs, rt);
B
bellard 已提交
6688
            break;
6689
        case OPC_MUL:
6690
            gen_arith(env, ctx, op1, rd, rs, rt);
B
bellard 已提交
6691
            break;
6692
        case OPC_CLZ ... OPC_CLO:
6693
            check_insn(env, ctx, ISA_MIPS32);
6694
            gen_cl(ctx, op1, rd, rs);
B
bellard 已提交
6695
            break;
6696
        case OPC_SDBBP:
B
bellard 已提交
6697 6698 6699
            /* XXX: not clear which exception should be raised
             *      when in debug mode...
             */
6700
            check_insn(env, ctx, ISA_MIPS32);
B
bellard 已提交
6701 6702 6703 6704 6705
            if (!(ctx->hflags & MIPS_HFLAG_DM)) {
                generate_exception(ctx, EXCP_DBp);
            } else {
                generate_exception(ctx, EXCP_DBp);
            }
6706
            /* Treat as NOP. */
B
bellard 已提交
6707
            break;
6708
#if defined(TARGET_MIPS64)
6709
        case OPC_DCLZ ... OPC_DCLO:
6710 6711
            check_insn(env, ctx, ISA_MIPS64);
            check_mips_64(ctx);
6712 6713 6714
            gen_cl(ctx, op1, rd, rs);
            break;
#endif
B
bellard 已提交
6715 6716 6717 6718 6719 6720
        default:            /* Invalid */
            MIPS_INVAL("special2");
            generate_exception(ctx, EXCP_RI);
            break;
        }
        break;
6721
    case OPC_SPECIAL3:
6722 6723 6724 6725
         op1 = MASK_SPECIAL3(ctx->opcode);
         switch (op1) {
         case OPC_EXT:
         case OPC_INS:
6726
             check_insn(env, ctx, ISA_MIPS32R2);
6727 6728 6729
             gen_bitops(ctx, op1, rt, rs, sa, rd);
             break;
         case OPC_BSHFL:
6730
             check_insn(env, ctx, ISA_MIPS32R2);
6731 6732 6733
             op2 = MASK_BSHFL(ctx->opcode);
             switch (op2) {
             case OPC_WSBH:
6734
                 gen_load_gpr(cpu_T[1], rt);
6735 6736 6737
                 gen_op_wsbh();
                 break;
             case OPC_SEB:
6738
                 gen_load_gpr(cpu_T[1], rt);
6739
                 tcg_gen_ext8s_tl(cpu_T[0], cpu_T[1]);
6740 6741
                 break;
             case OPC_SEH:
6742
                 gen_load_gpr(cpu_T[1], rt);
6743
                 tcg_gen_ext16s_tl(cpu_T[0], cpu_T[1]);
6744 6745 6746 6747 6748 6749
                 break;
             default:            /* Invalid */
                 MIPS_INVAL("bshfl");
                 generate_exception(ctx, EXCP_RI);
                 break;
            }
6750
            gen_store_gpr(cpu_T[0], rd);
6751
            break;
6752
        case OPC_RDHWR:
6753
            check_insn(env, ctx, ISA_MIPS32R2);
6754 6755
            switch (rd) {
            case 0:
6756
                save_cpu_state(ctx, 1);
6757
                gen_op_rdhwr_cpunum();
6758
                break;
6759
            case 1:
6760
                save_cpu_state(ctx, 1);
6761
                gen_op_rdhwr_synci_step();
6762
                break;
6763
            case 2:
6764
                save_cpu_state(ctx, 1);
6765
                gen_op_rdhwr_cc();
6766
                break;
6767
            case 3:
6768
                save_cpu_state(ctx, 1);
6769
                gen_op_rdhwr_ccres();
6770
                break;
6771
            case 29:
6772
#if defined (CONFIG_USER_ONLY)
6773
                gen_op_tls_value();
6774
                break;
6775
#endif
6776 6777 6778 6779 6780
            default:            /* Invalid */
                MIPS_INVAL("rdhwr");
                generate_exception(ctx, EXCP_RI);
                break;
            }
6781
            gen_store_gpr(cpu_T[0], rt);
6782
            break;
6783
        case OPC_FORK:
6784
            check_insn(env, ctx, ASE_MT);
6785 6786
            gen_load_gpr(cpu_T[0], rt);
            gen_load_gpr(cpu_T[1], rs);
6787 6788 6789
            gen_op_fork();
            break;
        case OPC_YIELD:
6790
            check_insn(env, ctx, ASE_MT);
6791
            gen_load_gpr(cpu_T[0], rs);
6792
            gen_op_yield();
6793
            gen_store_gpr(cpu_T[0], rd);
6794
            break;
6795
#if defined(TARGET_MIPS64)
6796 6797
        case OPC_DEXTM ... OPC_DEXT:
        case OPC_DINSM ... OPC_DINS:
6798 6799
            check_insn(env, ctx, ISA_MIPS64R2);
            check_mips_64(ctx);
6800
            gen_bitops(ctx, op1, rt, rs, sa, rd);
6801
            break;
6802
        case OPC_DBSHFL:
6803 6804
            check_insn(env, ctx, ISA_MIPS64R2);
            check_mips_64(ctx);
6805 6806 6807
            op2 = MASK_DBSHFL(ctx->opcode);
            switch (op2) {
            case OPC_DSBH:
6808
                gen_load_gpr(cpu_T[1], rt);
6809 6810 6811
                gen_op_dsbh();
                break;
            case OPC_DSHD:
6812
                gen_load_gpr(cpu_T[1], rt);
6813 6814
                gen_op_dshd();
                break;
6815 6816 6817 6818
            default:            /* Invalid */
                MIPS_INVAL("dbshfl");
                generate_exception(ctx, EXCP_RI);
                break;
6819
            }
6820
            gen_store_gpr(cpu_T[0], rd);
T
ths 已提交
6821
            break;
6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834
#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 已提交
6835
            return;
6836 6837 6838 6839 6840
        case OPC_TGEI ... OPC_TEQI: /* REGIMM traps */
        case OPC_TNEI:
            gen_trap(ctx, op1, rs, -1, imm);
            break;
        case OPC_SYNCI:
6841
            check_insn(env, ctx, ISA_MIPS32R2);
6842
            /* Treat as NOP. */
B
bellard 已提交
6843 6844
            break;
        default:            /* Invalid */
6845
            MIPS_INVAL("regimm");
B
bellard 已提交
6846 6847 6848 6849
            generate_exception(ctx, EXCP_RI);
            break;
        }
        break;
6850
    case OPC_CP0:
6851
        check_cp0_enabled(ctx);
6852
        op1 = MASK_CP0(ctx->opcode);
B
bellard 已提交
6853
        switch (op1) {
6854 6855
        case OPC_MFC0:
        case OPC_MTC0:
6856 6857
        case OPC_MFTR:
        case OPC_MTTR:
6858
#if defined(TARGET_MIPS64)
6859 6860 6861
        case OPC_DMFC0:
        case OPC_DMTC0:
#endif
6862
            gen_cp0(env, ctx, op1, rt, rd);
6863 6864
            break;
        case OPC_C0_FIRST ... OPC_C0_LAST:
6865
            gen_cp0(env, ctx, MASK_C0(ctx->opcode), rt, rd);
6866 6867 6868 6869
            break;
        case OPC_MFMC0:
            op2 = MASK_MFMC0(ctx->opcode);
            switch (op2) {
6870
            case OPC_DMT:
6871
                check_insn(env, ctx, ASE_MT);
6872 6873 6874
                gen_op_dmt();
                break;
            case OPC_EMT:
6875
                check_insn(env, ctx, ASE_MT);
6876 6877 6878
                gen_op_emt();
                break;
            case OPC_DVPE:
6879
                check_insn(env, ctx, ASE_MT);
6880 6881 6882
                gen_op_dvpe();
                break;
            case OPC_EVPE:
6883
                check_insn(env, ctx, ASE_MT);
6884 6885
                gen_op_evpe();
                break;
6886
            case OPC_DI:
6887
                check_insn(env, ctx, ISA_MIPS32R2);
6888
                save_cpu_state(ctx, 1);
6889 6890 6891 6892 6893
                gen_op_di();
                /* Stop translation as we may have switched the execution mode */
                ctx->bstate = BS_STOP;
                break;
            case OPC_EI:
6894
                check_insn(env, ctx, ISA_MIPS32R2);
6895
                save_cpu_state(ctx, 1);
6896 6897 6898 6899 6900
                gen_op_ei();
                /* Stop translation as we may have switched the execution mode */
                ctx->bstate = BS_STOP;
                break;
            default:            /* Invalid */
6901
                MIPS_INVAL("mfmc0");
6902 6903 6904
                generate_exception(ctx, EXCP_RI);
                break;
            }
6905
            gen_store_gpr(cpu_T[0], rt);
B
bellard 已提交
6906
            break;
6907
        case OPC_RDPGPR:
6908
            check_insn(env, ctx, ISA_MIPS32R2);
6909 6910
            gen_load_srsgpr(cpu_T[0], rt);
            gen_store_gpr(cpu_T[0], rd);
6911
            break;
6912
        case OPC_WRPGPR:
6913
            check_insn(env, ctx, ISA_MIPS32R2);
6914 6915
            gen_load_gpr(cpu_T[0], rt);
            gen_store_srsgpr(cpu_T[0], rd);
6916
            break;
B
bellard 已提交
6917
        default:
6918
            MIPS_INVAL("cp0");
6919
            generate_exception(ctx, EXCP_RI);
B
bellard 已提交
6920 6921 6922
            break;
        }
        break;
6923
    case OPC_ADDI ... OPC_LUI: /* Arithmetic with immediate opcode */
6924
         gen_arith_imm(env, ctx, op, rt, rs, imm);
6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941
         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:
6942
        check_insn(env, ctx, ISA_MIPS3 | ISA_MIPS32);
6943
        /* Treat as NOP. */
6944
        break;
6945
    case OPC_PREF:
6946
        check_insn(env, ctx, ISA_MIPS4 | ISA_MIPS32);
6947
        /* Treat as NOP. */
B
bellard 已提交
6948
        break;
B
bellard 已提交
6949

6950
    /* Floating point (COP1). */
6951 6952 6953 6954
    case OPC_LWC1:
    case OPC_LDC1:
    case OPC_SWC1:
    case OPC_SDC1:
6955 6956
        if (env->CP0_Config1 & (1 << CP0C1_FP)) {
            save_cpu_state(ctx, 1);
6957
            check_cp1_enabled(ctx);
6958 6959 6960 6961
            gen_flt_ldst(ctx, op, rt, rs, imm);
        } else {
            generate_exception_err(ctx, EXCP_CpU, 1);
        }
B
bellard 已提交
6962 6963
        break;

6964
    case OPC_CP1:
6965 6966
        if (env->CP0_Config1 & (1 << CP0C1_FP)) {
            save_cpu_state(ctx, 1);
6967
            check_cp1_enabled(ctx);
6968 6969
            op1 = MASK_CP1(ctx->opcode);
            switch (op1) {
6970 6971
            case OPC_MFHC1:
            case OPC_MTHC1:
6972
                check_insn(env, ctx, ISA_MIPS32R2);
6973 6974 6975 6976
            case OPC_MFC1:
            case OPC_CFC1:
            case OPC_MTC1:
            case OPC_CTC1:
6977 6978
                gen_cp1(ctx, op1, rt, rd);
                break;
6979
#if defined(TARGET_MIPS64)
6980 6981
            case OPC_DMFC1:
            case OPC_DMTC1:
6982
                check_insn(env, ctx, ISA_MIPS3);
6983 6984
                gen_cp1(ctx, op1, rt, rd);
                break;
6985
#endif
6986 6987
            case OPC_BC1ANY2:
            case OPC_BC1ANY4:
6988
                check_cop1x(ctx);
6989
                check_insn(env, ctx, ASE_MIPS3D);
6990 6991
                /* fall through */
            case OPC_BC1:
6992
                gen_compute_branch1(env, ctx, MASK_BC1(ctx->opcode),
6993
                                    (rt >> 2) & 0x7, imm << 2);
6994 6995 6996 6997 6998
                return;
            case OPC_S_FMT:
            case OPC_D_FMT:
            case OPC_W_FMT:
            case OPC_L_FMT:
6999 7000 7001
            case OPC_PS_FMT:
                gen_farith(ctx, MASK_CP1_FUNC(ctx->opcode), rt, rd, sa,
                           (imm >> 8) & 0x7);
7002 7003
                break;
            default:
7004
                MIPS_INVAL("cp1");
7005
                generate_exception (ctx, EXCP_RI);
7006 7007 7008 7009
                break;
            }
        } else {
            generate_exception_err(ctx, EXCP_CpU, 1);
B
bellard 已提交
7010
        }
B
bellard 已提交
7011 7012 7013
        break;

    /* COP2.  */
7014 7015 7016 7017 7018 7019
    case OPC_LWC2:
    case OPC_LDC2:
    case OPC_SWC2:
    case OPC_SDC2:
    case OPC_CP2:
        /* COP2: Not implemented. */
B
bellard 已提交
7020 7021 7022
        generate_exception_err(ctx, EXCP_CpU, 2);
        break;

7023
    case OPC_CP3:
7024
        if (env->CP0_Config1 & (1 << CP0C1_FP)) {
7025
            save_cpu_state(ctx, 1);
7026
            check_cp1_enabled(ctx);
7027 7028
            op1 = MASK_CP3(ctx->opcode);
            switch (op1) {
7029 7030 7031 7032 7033 7034
            case OPC_LWXC1:
            case OPC_LDXC1:
            case OPC_LUXC1:
            case OPC_SWXC1:
            case OPC_SDXC1:
            case OPC_SUXC1:
T
ths 已提交
7035
                gen_flt3_ldst(ctx, op1, sa, rd, rs, rt);
7036
                break;
T
ths 已提交
7037
            case OPC_PREFX:
7038
                /* Treat as NOP. */
T
ths 已提交
7039
                break;
7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054
            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;
7055
            default:
7056
                MIPS_INVAL("cp3");
7057
                generate_exception (ctx, EXCP_RI);
7058 7059 7060
                break;
            }
        } else {
7061
            generate_exception_err(ctx, EXCP_CpU, 1);
7062
        }
B
bellard 已提交
7063 7064
        break;

7065
#if defined(TARGET_MIPS64)
7066 7067 7068 7069 7070 7071 7072 7073
    /* 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:
7074 7075
        check_insn(env, ctx, ISA_MIPS3);
        check_mips_64(ctx);
7076 7077 7078
        gen_ldst(ctx, op, rt, rs, imm);
        break;
    case OPC_DADDI ... OPC_DADDIU:
7079 7080 7081
        check_insn(env, ctx, ISA_MIPS3);
        check_mips_64(ctx);
        gen_arith_imm(env, ctx, op, rt, rs, imm);
7082
        break;
B
bellard 已提交
7083
#endif
7084
    case OPC_JALX:
7085
        check_insn(env, ctx, ASE_MIPS16);
7086 7087
        /* MIPS16: Not implemented. */
    case OPC_MDMX:
7088
        check_insn(env, ctx, ASE_MDMX);
7089
        /* MDMX: Not implemented. */
B
bellard 已提交
7090
    default:            /* Invalid */
7091
        MIPS_INVAL("major opcode");
B
bellard 已提交
7092 7093 7094
        generate_exception(ctx, EXCP_RI);
        break;
    }
B
bellard 已提交
7095
    if (ctx->hflags & MIPS_HFLAG_BMASK) {
T
ths 已提交
7096
        int hflags = ctx->hflags & MIPS_HFLAG_BMASK;
B
bellard 已提交
7097
        /* Branches completion */
B
bellard 已提交
7098
        ctx->hflags &= ~MIPS_HFLAG_BMASK;
B
bellard 已提交
7099 7100
        ctx->bstate = BS_BRANCH;
        save_cpu_state(ctx, 0);
7101
        switch (hflags) {
B
bellard 已提交
7102 7103 7104
        case MIPS_HFLAG_B:
            /* unconditional branch */
            MIPS_DEBUG("unconditional branch");
7105
            gen_goto_tb(ctx, 0, ctx->btarget);
B
bellard 已提交
7106 7107 7108 7109
            break;
        case MIPS_HFLAG_BL:
            /* blikely taken case */
            MIPS_DEBUG("blikely branch taken");
7110
            gen_goto_tb(ctx, 0, ctx->btarget);
B
bellard 已提交
7111 7112 7113 7114
            break;
        case MIPS_HFLAG_BC:
            /* Conditional branch */
            MIPS_DEBUG("conditional branch");
B
bellard 已提交
7115
            {
7116 7117 7118 7119 7120 7121 7122 7123
                TCGv r_tmp = tcg_temp_new(TCG_TYPE_TL);
                int l1 = gen_new_label();

                tcg_gen_ld_tl(r_tmp, cpu_env, offsetof(CPUState, bcond));
                tcg_gen_brcond_tl(TCG_COND_NE, r_tmp, tcg_const_tl(0), l1);
                gen_goto_tb(ctx, 1, ctx->pc + 4);
                gen_set_label(l1);
                gen_goto_tb(ctx, 0, ctx->btarget);
B
bellard 已提交
7124
            }
B
bellard 已提交
7125 7126 7127 7128
            break;
        case MIPS_HFLAG_BR:
            /* unconditional branch to register */
            MIPS_DEBUG("branch to register");
7129
            gen_breg_pc();
B
bellard 已提交
7130
            tcg_gen_exit_tb(0);
B
bellard 已提交
7131 7132 7133 7134 7135 7136 7137 7138
            break;
        default:
            MIPS_DEBUG("unknown branch");
            break;
        }
    }
}

7139
static always_inline int
7140 7141
gen_intermediate_code_internal (CPUState *env, TranslationBlock *tb,
                                int search_pc)
B
bellard 已提交
7142
{
T
ths 已提交
7143
    DisasContext ctx;
B
bellard 已提交
7144 7145 7146 7147
    target_ulong pc_start;
    uint16_t *gen_opc_end;
    int j, lj = -1;

B
bellard 已提交
7148
    if (search_pc && loglevel)
B
bellard 已提交
7149
        fprintf (logfile, "search pc %d\n", search_pc);
B
bellard 已提交
7150

T
ths 已提交
7151 7152 7153
    num_temps = 0;
    memset(temps, 0, sizeof(temps));

7154 7155 7156
    num_temps = 0;
    memset(temps, 0, sizeof(temps));

B
bellard 已提交
7157 7158 7159
    pc_start = tb->pc;
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
    ctx.pc = pc_start;
B
bellard 已提交
7160
    ctx.saved_pc = -1;
B
bellard 已提交
7161 7162
    ctx.tb = tb;
    ctx.bstate = BS_NONE;
B
bellard 已提交
7163
    /* Restore delay slot state from the tb context.  */
7164
    ctx.hflags = (uint32_t)tb->flags; /* FIXME: maybe use 64 bits here? */
7165
    restore_cpu_state(env, &ctx);
B
bellard 已提交
7166
#if defined(CONFIG_USER_ONLY)
T
ths 已提交
7167
    ctx.mem_idx = MIPS_HFLAG_UM;
B
bellard 已提交
7168
#else
T
ths 已提交
7169
    ctx.mem_idx = ctx.hflags & MIPS_HFLAG_KSU;
B
bellard 已提交
7170 7171 7172 7173
#endif
#ifdef DEBUG_DISAS
    if (loglevel & CPU_LOG_TB_CPU) {
        fprintf(logfile, "------------------------------------------------\n");
B
bellard 已提交
7174
        /* FIXME: This may print out stale hflags from env... */
B
bellard 已提交
7175 7176 7177
        cpu_dump_state(env, logfile, fprintf, 0);
    }
#endif
7178
#ifdef MIPS_DEBUG_DISAS
B
bellard 已提交
7179
    if (loglevel & CPU_LOG_TB_IN_ASM)
T
ths 已提交
7180
        fprintf(logfile, "\ntb %p idx %d hflags %04x\n",
B
bellard 已提交
7181
                tb, ctx.mem_idx, ctx.hflags);
B
bellard 已提交
7182 7183
#endif
    while (ctx.bstate == BS_NONE && gen_opc_ptr < gen_opc_end) {
B
bellard 已提交
7184 7185 7186
        if (env->nb_breakpoints > 0) {
            for(j = 0; j < env->nb_breakpoints; j++) {
                if (env->breakpoints[j] == ctx.pc) {
T
ths 已提交
7187
                    save_cpu_state(&ctx, 1);
B
bellard 已提交
7188 7189
                    ctx.bstate = BS_BRANCH;
                    gen_op_debug();
7190 7191 7192
                    /* Include the breakpoint location or the tb won't
                     * be flushed when it must be.  */
                    ctx.pc += 4;
B
bellard 已提交
7193 7194 7195 7196 7197
                    goto done_generating;
                }
            }
        }

B
bellard 已提交
7198 7199 7200 7201 7202 7203 7204
        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 已提交
7205 7206 7207
            gen_opc_pc[lj] = ctx.pc;
            gen_opc_hflags[lj] = ctx.hflags & MIPS_HFLAG_BMASK;
            gen_opc_instr_start[lj] = 1;
B
bellard 已提交
7208 7209
        }
        ctx.opcode = ldl_code(ctx.pc);
7210
        decode_opc(env, &ctx);
T
ths 已提交
7211 7212 7213 7214 7215 7216
        if (num_temps) {
            fprintf(stderr,
                    "Internal resource leak before " TARGET_FMT_lx "\n",
                    ctx.pc);
            num_temps = 0;
        }
B
bellard 已提交
7217
        ctx.pc += 4;
B
bellard 已提交
7218 7219 7220 7221

        if (env->singlestep_enabled)
            break;

B
bellard 已提交
7222 7223
        if ((ctx.pc & (TARGET_PAGE_SIZE - 1)) == 0)
            break;
B
bellard 已提交
7224

B
bellard 已提交
7225 7226 7227 7228
#if defined (MIPS_SINGLE_STEP)
        break;
#endif
    }
B
bellard 已提交
7229
    if (env->singlestep_enabled) {
T
ths 已提交
7230
        save_cpu_state(&ctx, ctx.bstate == BS_NONE);
B
bellard 已提交
7231
        gen_op_debug();
T
ths 已提交
7232 7233 7234
    } else {
	switch (ctx.bstate) {
        case BS_STOP:
7235
            tcg_gen_helper_0_0(do_interrupt_restart);
7236 7237
            gen_goto_tb(&ctx, 0, ctx.pc);
            break;
T
ths 已提交
7238
        case BS_NONE:
T
ths 已提交
7239
            save_cpu_state(&ctx, 0);
T
ths 已提交
7240 7241
            gen_goto_tb(&ctx, 0, ctx.pc);
            break;
7242
        case BS_EXCP:
7243
            tcg_gen_helper_0_0(do_interrupt_restart);
B
bellard 已提交
7244
            tcg_gen_exit_tb(0);
T
ths 已提交
7245
            break;
7246 7247 7248
        case BS_BRANCH:
        default:
            break;
T
ths 已提交
7249
	}
B
bellard 已提交
7250
    }
B
bellard 已提交
7251
done_generating:
B
bellard 已提交
7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267
    *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 已提交
7268
        target_disas(logfile, pc_start, ctx.pc - pc_start, 0);
B
bellard 已提交
7269 7270 7271 7272 7273 7274
        fprintf(logfile, "\n");
    }
    if (loglevel & CPU_LOG_TB_CPU) {
        fprintf(logfile, "---------------- %d %08x\n", ctx.bstate, ctx.hflags);
    }
#endif
7275

B
bellard 已提交
7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288
    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);
}

7289
void fpu_dump_state(CPUState *env, FILE *f,
B
bellard 已提交
7290 7291 7292 7293
                    int (*fpu_fprintf)(FILE *f, const char *fmt, ...),
                    int flags)
{
    int i;
7294
    int is_fpu64 = !!(env->hflags & MIPS_HFLAG_F64);
7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309

#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 已提交
7310 7311
    } while(0)

7312 7313

    fpu_fprintf(f, "CP1 FCR0 0x%08x  FCR31 0x%08x  SR.FR %d  fp_status 0x%08x(0x%02x)\n",
7314 7315 7316 7317 7318
                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);
7319 7320
    for (i = 0; i < 32; (is_fpu64) ? i++ : (i += 2)) {
        fpu_fprintf(f, "%3s: ", fregnames[i]);
7321
        printfpr(&env->fpu->fpr[i]);
B
bellard 已提交
7322 7323 7324 7325 7326
    }

#undef printfpr
}

7327
void dump_fpu (CPUState *env)
B
bellard 已提交
7328
{
7329
    if (loglevel) {
T
ths 已提交
7330 7331 7332 7333 7334 7335 7336
        fprintf(logfile,
                "pc=0x" TARGET_FMT_lx " HI=0x" TARGET_FMT_lx
                " LO=0x" TARGET_FMT_lx " ds %04x " TARGET_FMT_lx
                " %04x\n",
                env->PC[env->current_tc], env->HI[env->current_tc][0],
                env->LO[env->current_tc][0], env->hflags, env->btarget,
                env->bcond);
B
bellard 已提交
7337 7338 7339 7340
       fpu_dump_state(env, logfile, fprintf, 0);
    }
}

7341
#if defined(TARGET_MIPS64) && defined(MIPS_DEBUG_SIGN_EXTENSIONS)
7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352
/* 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;

7353 7354
    if (!SIGN_EXT_P(env->PC[env->current_tc]))
        cpu_fprintf(f, "BROKEN: pc=0x" TARGET_FMT_lx "\n", env->PC[env->current_tc]);
7355 7356 7357 7358
    if (!SIGN_EXT_P(env->HI[env->current_tc][0]))
        cpu_fprintf(f, "BROKEN: HI=0x" TARGET_FMT_lx "\n", env->HI[env->current_tc][0]);
    if (!SIGN_EXT_P(env->LO[env->current_tc][0]))
        cpu_fprintf(f, "BROKEN: LO=0x" TARGET_FMT_lx "\n", env->LO[env->current_tc][0]);
7359
    if (!SIGN_EXT_P(env->btarget))
T
ths 已提交
7360
        cpu_fprintf(f, "BROKEN: btarget=0x" TARGET_FMT_lx "\n", env->btarget);
7361 7362

    for (i = 0; i < 32; i++) {
7363 7364
        if (!SIGN_EXT_P(env->gpr[env->current_tc][i]))
            cpu_fprintf(f, "BROKEN: %s=0x" TARGET_FMT_lx "\n", regnames[i], env->gpr[env->current_tc][i]);
7365 7366 7367
    }

    if (!SIGN_EXT_P(env->CP0_EPC))
T
ths 已提交
7368
        cpu_fprintf(f, "BROKEN: EPC=0x" TARGET_FMT_lx "\n", env->CP0_EPC);
7369
    if (!SIGN_EXT_P(env->CP0_LLAddr))
T
ths 已提交
7370
        cpu_fprintf(f, "BROKEN: LLAddr=0x" TARGET_FMT_lx "\n", env->CP0_LLAddr);
7371 7372 7373
}
#endif

7374
void cpu_dump_state (CPUState *env, FILE *f,
B
bellard 已提交
7375 7376 7377 7378
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                     int flags)
{
    int i;
7379

T
ths 已提交
7380
    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",
7381
                env->PC[env->current_tc], env->HI[env->current_tc], env->LO[env->current_tc], env->hflags, env->btarget, env->bcond);
B
bellard 已提交
7382 7383 7384
    for (i = 0; i < 32; i++) {
        if ((i & 3) == 0)
            cpu_fprintf(f, "GPR%02d:", i);
7385
        cpu_fprintf(f, " %s " TARGET_FMT_lx, regnames[i], env->gpr[env->current_tc][i]);
B
bellard 已提交
7386 7387 7388
        if ((i & 3) == 3)
            cpu_fprintf(f, "\n");
    }
7389

T
ths 已提交
7390
    cpu_fprintf(f, "CP0 Status  0x%08x Cause   0x%08x EPC    0x" TARGET_FMT_lx "\n",
7391
                env->CP0_Status, env->CP0_Cause, env->CP0_EPC);
T
ths 已提交
7392
    cpu_fprintf(f, "    Config0 0x%08x Config1 0x%08x LLAddr 0x" TARGET_FMT_lx "\n",
B
bellard 已提交
7393
                env->CP0_Config0, env->CP0_Config1, env->CP0_LLAddr);
7394
    if (env->hflags & MIPS_HFLAG_FPU)
7395
        fpu_dump_state(env, f, cpu_fprintf, flags);
7396
#if defined(TARGET_MIPS64) && defined(MIPS_DEBUG_SIGN_EXTENSIONS)
7397 7398
    cpu_mips_check_sign_extensions(env, f, cpu_fprintf, flags);
#endif
B
bellard 已提交
7399 7400
}

7401 7402 7403 7404 7405 7406 7407 7408 7409
static void mips_tcg_init(void)
{
    static int inited;

    /* Initialize various static tables. */
    if (inited)
	return;

    cpu_env = tcg_global_reg_new(TCG_TYPE_PTR, TCG_AREG0, "env");
T
ths 已提交
7410 7411 7412 7413
    current_tc_gprs = tcg_global_mem_new(TCG_TYPE_PTR,
                                         TCG_AREG0,
                                         offsetof(CPUState, current_tc_gprs),
                                         "current_tc_gprs");
7414 7415 7416 7417 7418 7419
#if TARGET_LONG_BITS > HOST_LONG_BITS
    cpu_T[0] = tcg_global_mem_new(TCG_TYPE_TL,
                                  TCG_AREG0, offsetof(CPUState, t0), "T0");
    cpu_T[1] = tcg_global_mem_new(TCG_TYPE_TL,
                                  TCG_AREG0, offsetof(CPUState, t1), "T1");
#else
T
ths 已提交
7420 7421
    cpu_T[0] = tcg_global_reg_new(TCG_TYPE_TL, TCG_AREG1, "T0");
    cpu_T[1] = tcg_global_reg_new(TCG_TYPE_TL, TCG_AREG2, "T1");
7422 7423 7424 7425 7426
#endif

    inited = 1;
}

B
bellard 已提交
7427 7428 7429
#include "translate_init.c"

CPUMIPSState *cpu_mips_init (const char *cpu_model)
B
bellard 已提交
7430 7431
{
    CPUMIPSState *env;
B
bellard 已提交
7432
    const mips_def_t *def;
B
bellard 已提交
7433

B
bellard 已提交
7434 7435 7436
    def = cpu_mips_find_by_name(cpu_model);
    if (!def)
        return NULL;
B
bellard 已提交
7437 7438 7439
    env = qemu_mallocz(sizeof(CPUMIPSState));
    if (!env)
        return NULL;
B
bellard 已提交
7440 7441
    env->cpu_model = def;

B
bellard 已提交
7442
    cpu_exec_init(env);
7443
    env->cpu_model_str = cpu_model;
7444
    mips_tcg_init();
7445 7446 7447 7448 7449 7450 7451 7452
    cpu_reset(env);
    return env;
}

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

B
bellard 已提交
7453
    tlb_flush(env, 1);
7454

B
bellard 已提交
7455
    /* Minimal init */
7456
#if !defined(CONFIG_USER_ONLY)
7457 7458 7459
    if (env->hflags & MIPS_HFLAG_BMASK) {
        /* If the exception was raised from a delay slot,
         * come back to the jump.  */
7460
        env->CP0_ErrorEPC = env->PC[env->current_tc] - 4;
7461
    } else {
7462
        env->CP0_ErrorEPC = env->PC[env->current_tc];
7463
    }
7464
    env->PC[env->current_tc] = (int32_t)0xBFC00000;
B
bellard 已提交
7465
    env->CP0_Wired = 0;
7466
    /* SMP not implemented */
7467
    env->CP0_EBase = 0x80000000;
7468
    env->CP0_Status = (1 << CP0St_BEV) | (1 << CP0St_ERL);
T
ths 已提交
7469 7470 7471
    /* vectored interrupts not implemented, timer on int 7,
       no performance counters. */
    env->CP0_IntCtl = 0xe0000000;
7472 7473 7474 7475 7476 7477 7478 7479 7480 7481
    {
        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 已提交
7482 7483
    /* Count register increments in debug mode, EJTAG version 1 */
    env->CP0_Debug = (1 << CP0DB_CNT) | (0x1 << CP0DB_VER);
7484
#endif
B
bellard 已提交
7485
    env->exception_index = EXCP_NONE;
7486
#if defined(CONFIG_USER_ONLY)
7487
    env->hflags = MIPS_HFLAG_UM;
7488
    env->user_mode_only = 1;
7489 7490
#else
    env->hflags = MIPS_HFLAG_CP0;
B
bellard 已提交
7491
#endif
B
bellard 已提交
7492
    cpu_mips_register(env, env->cpu_model);
B
bellard 已提交
7493
}
A
aurel32 已提交
7494 7495 7496 7497 7498 7499 7500 7501

void gen_pc_load(CPUState *env, TranslationBlock *tb,
                unsigned long searched_pc, int pc_pos, void *puc)
{
    env->PC[env->current_tc] = gen_opc_pc[pc_pos];
    env->hflags &= ~MIPS_HFLAG_BMASK;
    env->hflags |= gen_opc_hflags[pc_pos];
}