Statistics
| Branch: | Revision:

root / target-mips / translate.c @ 78723684

History | View | Annotate | Download (246.6 kB)

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

    
23
#include <stdarg.h>
24
#include <stdlib.h>
25
#include <stdio.h>
26
#include <string.h>
27
#include <inttypes.h>
28

    
29
#include "cpu.h"
30
#include "exec-all.h"
31
#include "disas.h"
32
#include "helper.h"
33
#include "tcg-op.h"
34
#include "qemu-common.h"
35

    
36
//#define MIPS_DEBUG_DISAS
37
//#define MIPS_DEBUG_SIGN_EXTENSIONS
38
//#define MIPS_SINGLE_STEP
39

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

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

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

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

    
184
    OPC_MOVCI    = 0x01 | OPC_SPECIAL,
185

    
186
    /* Special */
187
    OPC_PMON     = 0x05 | OPC_SPECIAL, /* inofficial */
188
    OPC_SYSCALL  = 0x0C | OPC_SPECIAL,
189
    OPC_BREAK    = 0x0D | OPC_SPECIAL,
190
    OPC_SPIM     = 0x0E | OPC_SPECIAL, /* inofficial */
191
    OPC_SYNC     = 0x0F | OPC_SPECIAL,
192

    
193
    OPC_SPECIAL15_RESERVED = 0x15 | OPC_SPECIAL,
194
    OPC_SPECIAL28_RESERVED = 0x28 | OPC_SPECIAL,
195
    OPC_SPECIAL29_RESERVED = 0x29 | OPC_SPECIAL,
196
    OPC_SPECIAL35_RESERVED = 0x35 | OPC_SPECIAL,
197
    OPC_SPECIAL37_RESERVED = 0x37 | OPC_SPECIAL,
198
    OPC_SPECIAL39_RESERVED = 0x39 | OPC_SPECIAL,
199
    OPC_SPECIAL3D_RESERVED = 0x3D | OPC_SPECIAL,
200
};
201

    
202
/* Multiplication variants of the vr54xx. */
203
#define MASK_MUL_VR54XX(op)   MASK_SPECIAL(op) | (op & (0x1F << 6))
204

    
205
enum {
206
    OPC_VR54XX_MULS    = (0x03 << 6) | OPC_MULT,
207
    OPC_VR54XX_MULSU   = (0x03 << 6) | OPC_MULTU,
208
    OPC_VR54XX_MACC    = (0x05 << 6) | OPC_MULT,
209
    OPC_VR54XX_MACCU   = (0x05 << 6) | OPC_MULTU,
210
    OPC_VR54XX_MSAC    = (0x07 << 6) | OPC_MULT,
211
    OPC_VR54XX_MSACU   = (0x07 << 6) | OPC_MULTU,
212
    OPC_VR54XX_MULHI   = (0x09 << 6) | OPC_MULT,
213
    OPC_VR54XX_MULHIU  = (0x09 << 6) | OPC_MULTU,
214
    OPC_VR54XX_MULSHI  = (0x0B << 6) | OPC_MULT,
215
    OPC_VR54XX_MULSHIU = (0x0B << 6) | OPC_MULTU,
216
    OPC_VR54XX_MACCHI  = (0x0D << 6) | OPC_MULT,
217
    OPC_VR54XX_MACCHIU = (0x0D << 6) | OPC_MULTU,
218
    OPC_VR54XX_MSACHI  = (0x0F << 6) | OPC_MULT,
219
    OPC_VR54XX_MSACHIU = (0x0F << 6) | OPC_MULTU,
220
};
221

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

    
225
enum {
226
    OPC_BLTZ     = (0x00 << 16) | OPC_REGIMM,
227
    OPC_BLTZL    = (0x02 << 16) | OPC_REGIMM,
228
    OPC_BGEZ     = (0x01 << 16) | OPC_REGIMM,
229
    OPC_BGEZL    = (0x03 << 16) | OPC_REGIMM,
230
    OPC_BLTZAL   = (0x10 << 16) | OPC_REGIMM,
231
    OPC_BLTZALL  = (0x12 << 16) | OPC_REGIMM,
232
    OPC_BGEZAL   = (0x11 << 16) | OPC_REGIMM,
233
    OPC_BGEZALL  = (0x13 << 16) | OPC_REGIMM,
234
    OPC_TGEI     = (0x08 << 16) | OPC_REGIMM,
235
    OPC_TGEIU    = (0x09 << 16) | OPC_REGIMM,
236
    OPC_TLTI     = (0x0A << 16) | OPC_REGIMM,
237
    OPC_TLTIU    = (0x0B << 16) | OPC_REGIMM,
238
    OPC_TEQI     = (0x0C << 16) | OPC_REGIMM,
239
    OPC_TNEI     = (0x0E << 16) | OPC_REGIMM,
240
    OPC_SYNCI    = (0x1F << 16) | OPC_REGIMM,
241
};
242

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

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

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

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

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

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

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

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

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

    
301
enum {
302
    OPC_MFC0     = (0x00 << 21) | OPC_CP0,
303
    OPC_DMFC0    = (0x01 << 21) | OPC_CP0,
304
    OPC_MTC0     = (0x04 << 21) | OPC_CP0,
305
    OPC_DMTC0    = (0x05 << 21) | OPC_CP0,
306
    OPC_MFTR     = (0x08 << 21) | OPC_CP0,
307
    OPC_RDPGPR   = (0x0A << 21) | OPC_CP0,
308
    OPC_MFMC0    = (0x0B << 21) | OPC_CP0,
309
    OPC_MTTR     = (0x0C << 21) | OPC_CP0,
310
    OPC_WRPGPR   = (0x0E << 21) | OPC_CP0,
311
    OPC_C0       = (0x10 << 21) | OPC_CP0,
312
    OPC_C0_FIRST = (0x10 << 21) | OPC_CP0,
313
    OPC_C0_LAST  = (0x1F << 21) | OPC_CP0,
314
};
315

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

    
319
enum {
320
    OPC_DMT      = 0x01 | (0 << 5) | (0x0F << 6) | (0x01 << 11) | OPC_MFMC0,
321
    OPC_EMT      = 0x01 | (1 << 5) | (0x0F << 6) | (0x01 << 11) | OPC_MFMC0,
322
    OPC_DVPE     = 0x01 | (0 << 5) | OPC_MFMC0,
323
    OPC_EVPE     = 0x01 | (1 << 5) | OPC_MFMC0,
324
    OPC_DI       = (0 << 5) | (0x0C << 11) | OPC_MFMC0,
325
    OPC_EI       = (1 << 5) | (0x0C << 11) | OPC_MFMC0,
326
};
327

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

    
331
enum {
332
    OPC_TLBR     = 0x01 | OPC_C0,
333
    OPC_TLBWI    = 0x02 | OPC_C0,
334
    OPC_TLBWR    = 0x06 | OPC_C0,
335
    OPC_TLBP     = 0x08 | OPC_C0,
336
    OPC_RFE      = 0x10 | OPC_C0,
337
    OPC_ERET     = 0x18 | OPC_C0,
338
    OPC_DERET    = 0x1F | OPC_C0,
339
    OPC_WAIT     = 0x20 | OPC_C0,
340
};
341

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

    
345
enum {
346
    OPC_MFC1     = (0x00 << 21) | OPC_CP1,
347
    OPC_DMFC1    = (0x01 << 21) | OPC_CP1,
348
    OPC_CFC1     = (0x02 << 21) | OPC_CP1,
349
    OPC_MFHC1    = (0x03 << 21) | OPC_CP1,
350
    OPC_MTC1     = (0x04 << 21) | OPC_CP1,
351
    OPC_DMTC1    = (0x05 << 21) | OPC_CP1,
352
    OPC_CTC1     = (0x06 << 21) | OPC_CP1,
353
    OPC_MTHC1    = (0x07 << 21) | OPC_CP1,
354
    OPC_BC1      = (0x08 << 21) | OPC_CP1, /* bc */
355
    OPC_BC1ANY2  = (0x09 << 21) | OPC_CP1,
356
    OPC_BC1ANY4  = (0x0A << 21) | OPC_CP1,
357
    OPC_S_FMT    = (0x10 << 21) | OPC_CP1, /* 16: fmt=single fp */
358
    OPC_D_FMT    = (0x11 << 21) | OPC_CP1, /* 17: fmt=double fp */
359
    OPC_E_FMT    = (0x12 << 21) | OPC_CP1, /* 18: fmt=extended fp */
360
    OPC_Q_FMT    = (0x13 << 21) | OPC_CP1, /* 19: fmt=quad fp */
361
    OPC_W_FMT    = (0x14 << 21) | OPC_CP1, /* 20: fmt=32bit fixed */
362
    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
#define MASK_CP1_FUNC(op)       MASK_CP1(op) | (op & 0x3F)
367
#define MASK_BC1(op)            MASK_CP1(op) | (op & (0x3 << 16))
368

    
369
enum {
370
    OPC_BC1F     = (0x00 << 16) | OPC_BC1,
371
    OPC_BC1T     = (0x01 << 16) | OPC_BC1,
372
    OPC_BC1FL    = (0x02 << 16) | OPC_BC1,
373
    OPC_BC1TL    = (0x03 << 16) | OPC_BC1,
374
};
375

    
376
enum {
377
    OPC_BC1FANY2     = (0x00 << 16) | OPC_BC1ANY2,
378
    OPC_BC1TANY2     = (0x01 << 16) | OPC_BC1ANY2,
379
};
380

    
381
enum {
382
    OPC_BC1FANY4     = (0x00 << 16) | OPC_BC1ANY4,
383
    OPC_BC1TANY4     = (0x01 << 16) | OPC_BC1ANY4,
384
};
385

    
386
#define MASK_CP2(op)       MASK_OP_MAJOR(op) | (op & (0x1F << 21))
387

    
388
enum {
389
    OPC_MFC2    = (0x00 << 21) | OPC_CP2,
390
    OPC_DMFC2   = (0x01 << 21) | OPC_CP2,
391
    OPC_CFC2    = (0x02 << 21) | OPC_CP2,
392
    OPC_MFHC2   = (0x03 << 21) | OPC_CP2,
393
    OPC_MTC2    = (0x04 << 21) | OPC_CP2,
394
    OPC_DMTC2   = (0x05 << 21) | OPC_CP2,
395
    OPC_CTC2    = (0x06 << 21) | OPC_CP2,
396
    OPC_MTHC2   = (0x07 << 21) | OPC_CP2,
397
    OPC_BC2     = (0x08 << 21) | OPC_CP2,
398
};
399

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

    
402
enum {
403
    OPC_LWXC1   = 0x00 | OPC_CP3,
404
    OPC_LDXC1   = 0x01 | OPC_CP3,
405
    OPC_LUXC1   = 0x05 | OPC_CP3,
406
    OPC_SWXC1   = 0x08 | OPC_CP3,
407
    OPC_SDXC1   = 0x09 | OPC_CP3,
408
    OPC_SUXC1   = 0x0D | OPC_CP3,
409
    OPC_PREFX   = 0x0F | OPC_CP3,
410
    OPC_ALNV_PS = 0x1E | OPC_CP3,
411
    OPC_MADD_S  = 0x20 | OPC_CP3,
412
    OPC_MADD_D  = 0x21 | OPC_CP3,
413
    OPC_MADD_PS = 0x26 | OPC_CP3,
414
    OPC_MSUB_S  = 0x28 | OPC_CP3,
415
    OPC_MSUB_D  = 0x29 | OPC_CP3,
416
    OPC_MSUB_PS = 0x2E | OPC_CP3,
417
    OPC_NMADD_S = 0x30 | OPC_CP3,
418
    OPC_NMADD_D = 0x31 | OPC_CP3,
419
    OPC_NMADD_PS= 0x36 | OPC_CP3,
420
    OPC_NMSUB_S = 0x38 | OPC_CP3,
421
    OPC_NMSUB_D = 0x39 | OPC_CP3,
422
    OPC_NMSUB_PS= 0x3E | OPC_CP3,
423
};
424

    
425
/* global register indices */
426
static TCGv cpu_env, current_tc_gprs, current_tc_hi, current_fpu, cpu_T[2];
427

    
428
/* FPU TNs, global for now. */
429
static TCGv fpu32_T[3], fpu64_T[3], fpu32h_T[3];
430

    
431
static inline void tcg_gen_helper_0_i(void *func, TCGv arg)
432
{
433
    TCGv tmp = tcg_const_i32(arg);
434

    
435
    tcg_gen_helper_0_1(func, tmp);
436
    tcg_temp_free(tmp);
437
}
438

    
439
static inline void tcg_gen_helper_0_ii(void *func, TCGv arg1, TCGv arg2)
440
{
441
    TCGv tmp1 = tcg_const_i32(arg1);
442
    TCGv tmp2 = tcg_const_i32(arg2);
443

    
444
    tcg_gen_helper_0_2(func, tmp1, tmp2);
445
    tcg_temp_free(tmp1);
446
    tcg_temp_free(tmp2);
447
}
448

    
449
static inline void tcg_gen_helper_0_1i(void *func, TCGv arg1, TCGv arg2)
450
{
451
    TCGv tmp = tcg_const_i32(arg2);
452

    
453
    tcg_gen_helper_0_2(func, arg1, tmp);
454
    tcg_temp_free(tmp);
455
}
456

    
457
static inline void tcg_gen_helper_0_2i(void *func, TCGv arg1, TCGv arg2, TCGv arg3)
458
{
459
    TCGv tmp = tcg_const_i32(arg3);
460

    
461
    tcg_gen_helper_0_3(func, arg1, arg2, tmp);
462
    tcg_temp_free(tmp);
463
}
464

    
465
static inline void tcg_gen_helper_0_2ii(void *func, TCGv arg1, TCGv arg2, TCGv arg3, TCGv arg4)
466
{
467
    TCGv tmp1 = tcg_const_i32(arg3);
468
    TCGv tmp2 = tcg_const_i32(arg3);
469

    
470
    tcg_gen_helper_0_4(func, arg1, arg2, tmp1, tmp2);
471
    tcg_temp_free(tmp1);
472
    tcg_temp_free(tmp2);
473
}
474

    
475
static inline void tcg_gen_helper_1_i(void *func, TCGv ret, TCGv arg)
476
{
477
    TCGv tmp = tcg_const_i32(arg);
478

    
479
    tcg_gen_helper_1_1(func, ret, tmp);
480
    tcg_temp_free(tmp);
481
}
482

    
483
static inline void tcg_gen_helper_1_1i(void *func, TCGv ret, TCGv arg1, TCGv arg2)
484
{
485
    TCGv tmp = tcg_const_i32(arg2);
486

    
487
    tcg_gen_helper_1_2(func, ret, arg1, tmp);
488
    tcg_temp_free(tmp);
489
}
490

    
491
static inline void tcg_gen_helper_1_2i(void *func, TCGv ret, TCGv arg1, TCGv arg2, TCGv arg3)
492
{
493
    TCGv tmp = tcg_const_i32(arg3);
494

    
495
    tcg_gen_helper_1_3(func, ret, arg1, arg2, tmp);
496
    tcg_temp_free(tmp);
497
}
498

    
499
static inline void tcg_gen_helper_1_2ii(void *func, TCGv ret, TCGv arg1, TCGv arg2, TCGv arg3, TCGv arg4)
500
{
501
    TCGv tmp1 = tcg_const_i32(arg3);
502
    TCGv tmp2 = tcg_const_i32(arg3);
503

    
504
    tcg_gen_helper_1_4(func, ret, arg1, arg2, tmp1, tmp2);
505
    tcg_temp_free(tmp1);
506
    tcg_temp_free(tmp2);
507
}
508

    
509
typedef struct DisasContext {
510
    struct TranslationBlock *tb;
511
    target_ulong pc, saved_pc;
512
    uint32_t opcode;
513
    uint32_t fp_status;
514
    /* Routine used to access memory */
515
    int mem_idx;
516
    uint32_t hflags, saved_hflags;
517
    int bstate;
518
    target_ulong btarget;
519
} DisasContext;
520

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

    
530
static const char *regnames[] =
531
    { "r0", "at", "v0", "v1", "a0", "a1", "a2", "a3",
532
      "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
533
      "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
534
      "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra", };
535

    
536
static const char *fregnames[] =
537
    { "f0",  "f1",  "f2",  "f3",  "f4",  "f5",  "f6",  "f7",
538
      "f8",  "f9",  "f10", "f11", "f12", "f13", "f14", "f15",
539
      "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
540
      "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31", };
541

    
542
#ifdef MIPS_DEBUG_DISAS
543
#define MIPS_DEBUG(fmt, args...)                                              \
544
do {                                                                          \
545
    if (loglevel & CPU_LOG_TB_IN_ASM) {                                       \
546
        fprintf(logfile, TARGET_FMT_lx ": %08x " fmt "\n",                    \
547
                ctx->pc, ctx->opcode , ##args);                               \
548
    }                                                                         \
549
} while (0)
550
#else
551
#define MIPS_DEBUG(fmt, args...) do { } while(0)
552
#endif
553

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

    
560
/* General purpose registers moves. */
561
static inline void gen_load_gpr (TCGv t, int reg)
562
{
563
    if (reg == 0)
564
        tcg_gen_movi_tl(t, 0);
565
    else
566
        tcg_gen_ld_tl(t, current_tc_gprs, sizeof(target_ulong) * reg);
567
}
568

    
569
static inline void gen_store_gpr (TCGv t, int reg)
570
{
571
    if (reg != 0)
572
        tcg_gen_st_tl(t, current_tc_gprs, sizeof(target_ulong) * reg);
573
}
574

    
575
/* Moves to/from HI and LO registers.  */
576
static inline void gen_load_LO (TCGv t, int reg)
577
{
578
    tcg_gen_ld_tl(t, current_tc_hi,
579
                  offsetof(CPUState, LO)
580
                  - offsetof(CPUState, HI)
581
                  + sizeof(target_ulong) * reg);
582
}
583

    
584
static inline void gen_store_LO (TCGv t, int reg)
585
{
586
    tcg_gen_st_tl(t, current_tc_hi,
587
                  offsetof(CPUState, LO)
588
                  - offsetof(CPUState, HI)
589
                  + sizeof(target_ulong) * reg);
590
}
591

    
592
static inline void gen_load_HI (TCGv t, int reg)
593
{
594
    tcg_gen_ld_tl(t, current_tc_hi, sizeof(target_ulong) * reg);
595
}
596

    
597
static inline void gen_store_HI (TCGv t, int reg)
598
{
599
    tcg_gen_st_tl(t, current_tc_hi, sizeof(target_ulong) * reg);
600
}
601

    
602
/* Moves to/from shadow registers. */
603
static inline void gen_load_srsgpr (int from, int to)
604
{
605
    TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_TL);
606

    
607
    if (from == 0)
608
        tcg_gen_movi_tl(r_tmp1, 0);
609
    else {
610
        TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I32);
611

    
612
        tcg_gen_ld_i32(r_tmp2, cpu_env, offsetof(CPUState, CP0_SRSCtl));
613
        tcg_gen_shri_i32(r_tmp2, r_tmp2, CP0SRSCtl_PSS);
614
        tcg_gen_andi_i32(r_tmp2, r_tmp2, 0xf);
615
        tcg_gen_muli_i32(r_tmp2, r_tmp2, sizeof(target_ulong) * 32);
616
        tcg_gen_add_i32(r_tmp2, cpu_env, r_tmp2);
617

    
618
        tcg_gen_ld_tl(r_tmp1, r_tmp2, sizeof(target_ulong) * from);
619
        tcg_temp_free(r_tmp2);
620
    }
621
    gen_store_gpr(r_tmp1, to);
622
    tcg_temp_free(r_tmp1);
623
}
624

    
625
static inline void gen_store_srsgpr (int from, int to)
626
{
627
    if (to != 0) {
628
        TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_TL);
629
        TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I32);
630

    
631
        gen_load_gpr(r_tmp1, from);
632
        tcg_gen_ld_i32(r_tmp2, cpu_env, offsetof(CPUState, CP0_SRSCtl));
633
        tcg_gen_shri_i32(r_tmp2, r_tmp2, CP0SRSCtl_PSS);
634
        tcg_gen_andi_i32(r_tmp2, r_tmp2, 0xf);
635
        tcg_gen_muli_i32(r_tmp2, r_tmp2, sizeof(target_ulong) * 32);
636
        tcg_gen_add_i32(r_tmp2, cpu_env, r_tmp2);
637

    
638
        tcg_gen_st_tl(r_tmp1, r_tmp2, sizeof(target_ulong) * to);
639
        tcg_temp_free(r_tmp1);
640
        tcg_temp_free(r_tmp2);
641
    }
642
}
643

    
644
/* Floating point register moves. */
645
static inline void gen_load_fpr32 (TCGv t, int reg)
646
{
647
    tcg_gen_ld_i32(t, current_fpu, 8 * reg + 4 * FP_ENDIAN_IDX);
648
}
649

    
650
static inline void gen_store_fpr32 (TCGv t, int reg)
651
{
652
    tcg_gen_st_i32(t, current_fpu, 8 * reg + 4 * FP_ENDIAN_IDX);
653
}
654

    
655
static inline void gen_load_fpr64 (DisasContext *ctx, TCGv t, int reg)
656
{
657
    if (ctx->hflags & MIPS_HFLAG_F64) {
658
        tcg_gen_ld_i64(t, current_fpu, 8 * reg);
659
    } else {
660
        TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
661
        TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I64);
662

    
663
        tcg_gen_ld_i32(r_tmp1, current_fpu, 8 * (reg | 1) + 4 * FP_ENDIAN_IDX);
664
        tcg_gen_extu_i32_i64(t, r_tmp1);
665
        tcg_gen_shli_i64(t, t, 32);
666
        tcg_gen_ld_i32(r_tmp1, current_fpu, 8 * (reg & ~1) + 4 * FP_ENDIAN_IDX);
667
        tcg_gen_extu_i32_i64(r_tmp2, r_tmp1);
668
        tcg_gen_or_i64(t, t, r_tmp2);
669
        tcg_temp_free(r_tmp1);
670
        tcg_temp_free(r_tmp2);
671
    }
672
}
673

    
674
static inline void gen_store_fpr64 (DisasContext *ctx, TCGv t, int reg)
675
{
676
    if (ctx->hflags & MIPS_HFLAG_F64) {
677
        tcg_gen_st_i64(t, current_fpu, 8 * reg);
678
    } else {
679
        TCGv r_tmp = tcg_temp_new(TCG_TYPE_I32);
680

    
681
        tcg_gen_trunc_i64_i32(r_tmp, t);
682
        tcg_gen_st_i32(r_tmp, current_fpu, 8 * (reg & ~1) + 4 * FP_ENDIAN_IDX);
683
        tcg_gen_shri_i64(t, t, 32);
684
        tcg_gen_trunc_i64_i32(r_tmp, t);
685
        tcg_gen_st_i32(r_tmp, current_fpu, 8 * (reg | 1) + 4 * FP_ENDIAN_IDX);
686
        tcg_temp_free(r_tmp);
687
    }
688
}
689

    
690
static inline void gen_load_fpr32h (TCGv t, int reg)
691
{
692
    tcg_gen_ld_i32(t, current_fpu, 8 * reg + 4 * !FP_ENDIAN_IDX);
693
}
694

    
695
static inline void gen_store_fpr32h (TCGv t, int reg)
696
{
697
    tcg_gen_st_i32(t, current_fpu, 8 * reg + 4 * !FP_ENDIAN_IDX);
698
}
699

    
700
static inline void get_fp_cond (TCGv t)
701
{
702
    TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
703
    TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I32);
704

    
705
    tcg_gen_ld_i32(r_tmp1, current_fpu, offsetof(CPUMIPSFPUContext, fcr31));
706
    tcg_gen_shri_i32(r_tmp2, r_tmp1, 24);
707
    tcg_gen_andi_i32(r_tmp2, r_tmp2, 0xfe);
708
    tcg_gen_shri_i32(r_tmp1, r_tmp1, 23);
709
    tcg_gen_andi_i32(r_tmp1, r_tmp1, 0x1);
710
    tcg_gen_or_i32(t, r_tmp1, r_tmp2);
711
    tcg_temp_free(r_tmp1);
712
    tcg_temp_free(r_tmp2);
713
}
714

    
715
#define FOP_CONDS(type, fmt)                                              \
716
static GenOpFunc1 * fcmp ## type ## _ ## fmt ## _table[16] = {            \
717
    do_cmp ## type ## _ ## fmt ## _f,                                     \
718
    do_cmp ## type ## _ ## fmt ## _un,                                    \
719
    do_cmp ## type ## _ ## fmt ## _eq,                                    \
720
    do_cmp ## type ## _ ## fmt ## _ueq,                                   \
721
    do_cmp ## type ## _ ## fmt ## _olt,                                   \
722
    do_cmp ## type ## _ ## fmt ## _ult,                                   \
723
    do_cmp ## type ## _ ## fmt ## _ole,                                   \
724
    do_cmp ## type ## _ ## fmt ## _ule,                                   \
725
    do_cmp ## type ## _ ## fmt ## _sf,                                    \
726
    do_cmp ## type ## _ ## fmt ## _ngle,                                  \
727
    do_cmp ## type ## _ ## fmt ## _seq,                                   \
728
    do_cmp ## type ## _ ## fmt ## _ngl,                                   \
729
    do_cmp ## type ## _ ## fmt ## _lt,                                    \
730
    do_cmp ## type ## _ ## fmt ## _nge,                                   \
731
    do_cmp ## type ## _ ## fmt ## _le,                                    \
732
    do_cmp ## type ## _ ## fmt ## _ngt,                                   \
733
};                                                                        \
734
static inline void gen_cmp ## type ## _ ## fmt(int n, long cc)            \
735
{                                                                         \
736
    tcg_gen_helper_0_i(fcmp ## type ## _ ## fmt ## _table[n], cc);        \
737
}
738

    
739
FOP_CONDS(, d)
740
FOP_CONDS(abs, d)
741
FOP_CONDS(, s)
742
FOP_CONDS(abs, s)
743
FOP_CONDS(, ps)
744
FOP_CONDS(abs, ps)
745
#undef FOP_CONDS
746

    
747
/* Tests */
748
#define OP_COND(name, cond)                                   \
749
void glue(gen_op_, name) (TCGv t0, TCGv t1)                   \
750
{                                                             \
751
    int l1 = gen_new_label();                                 \
752
    int l2 = gen_new_label();                                 \
753
                                                              \
754
    tcg_gen_brcond_tl(cond, t0, t1, l1);                      \
755
    tcg_gen_movi_tl(t0, 0);                                   \
756
    tcg_gen_br(l2);                                           \
757
    gen_set_label(l1);                                        \
758
    tcg_gen_movi_tl(t0, 1);                                   \
759
    gen_set_label(l2);                                        \
760
}
761
OP_COND(eq, TCG_COND_EQ);
762
OP_COND(ne, TCG_COND_NE);
763
OP_COND(ge, TCG_COND_GE);
764
OP_COND(geu, TCG_COND_GEU);
765
OP_COND(lt, TCG_COND_LT);
766
OP_COND(ltu, TCG_COND_LTU);
767
#undef OP_COND
768

    
769
#define OP_CONDI(name, cond)                                  \
770
void glue(gen_op_, name) (TCGv t, target_ulong val)           \
771
{                                                             \
772
    int l1 = gen_new_label();                                 \
773
    int l2 = gen_new_label();                                 \
774
                                                              \
775
    tcg_gen_brcondi_tl(cond, t, val, l1);                     \
776
    tcg_gen_movi_tl(t, 0);                                    \
777
    tcg_gen_br(l2);                                           \
778
    gen_set_label(l1);                                        \
779
    tcg_gen_movi_tl(t, 1);                                    \
780
    gen_set_label(l2);                                        \
781
}
782
OP_CONDI(lti, TCG_COND_LT);
783
OP_CONDI(ltiu, TCG_COND_LTU);
784
#undef OP_CONDI
785

    
786
#define OP_CONDZ(name, cond)                                  \
787
void glue(gen_op_, name) (TCGv t)                             \
788
{                                                             \
789
    int l1 = gen_new_label();                                 \
790
    int l2 = gen_new_label();                                 \
791
                                                              \
792
    tcg_gen_brcondi_tl(cond, t, 0, l1);                       \
793
    tcg_gen_movi_tl(t, 0);                                    \
794
    tcg_gen_br(l2);                                           \
795
    gen_set_label(l1);                                        \
796
    tcg_gen_movi_tl(t, 1);                                    \
797
    gen_set_label(l2);                                        \
798
}
799
OP_CONDZ(gez, TCG_COND_GE);
800
OP_CONDZ(gtz, TCG_COND_GT);
801
OP_CONDZ(lez, TCG_COND_LE);
802
OP_CONDZ(ltz, TCG_COND_LT);
803
#undef OP_CONDZ
804

    
805
static inline void gen_save_pc(target_ulong pc)
806
{
807
    TCGv r_tmp = tcg_temp_new(TCG_TYPE_TL);
808
    TCGv r_tc_off = tcg_temp_new(TCG_TYPE_I32);
809
    TCGv r_tc_off_ptr = tcg_temp_new(TCG_TYPE_PTR);
810
    TCGv r_ptr = tcg_temp_new(TCG_TYPE_PTR);
811

    
812
    tcg_gen_movi_tl(r_tmp, pc);
813
    tcg_gen_ld_i32(r_tc_off, cpu_env, offsetof(CPUState, current_tc));
814
    tcg_gen_muli_i32(r_tc_off, r_tc_off, sizeof(target_ulong));
815
    tcg_gen_ext_i32_ptr(r_tc_off_ptr, r_tc_off);
816
    tcg_gen_add_ptr(r_ptr, cpu_env, r_tc_off_ptr);
817
    tcg_gen_st_tl(r_tmp, r_ptr, offsetof(CPUState, PC));
818
    tcg_temp_free(r_tc_off);
819
    tcg_temp_free(r_tc_off_ptr);
820
    tcg_temp_free(r_ptr);
821
    tcg_temp_free(r_tmp);
822
}
823

    
824
static inline void gen_breg_pc(void)
825
{
826
    TCGv r_tmp = tcg_temp_new(TCG_TYPE_TL);
827
    TCGv r_tc_off = tcg_temp_new(TCG_TYPE_I32);
828
    TCGv r_tc_off_ptr = tcg_temp_new(TCG_TYPE_PTR);
829
    TCGv r_ptr = tcg_temp_new(TCG_TYPE_PTR);
830

    
831
    tcg_gen_ld_tl(r_tmp, cpu_env, offsetof(CPUState, btarget));
832
    tcg_gen_ld_i32(r_tc_off, cpu_env, offsetof(CPUState, current_tc));
833
    tcg_gen_muli_i32(r_tc_off, r_tc_off, sizeof(target_ulong));
834
    tcg_gen_ext_i32_ptr(r_tc_off_ptr, r_tc_off);
835
    tcg_gen_add_ptr(r_ptr, cpu_env, r_tc_off_ptr);
836
    tcg_gen_st_tl(r_tmp, r_ptr, offsetof(CPUState, PC));
837
    tcg_temp_free(r_tc_off);
838
    tcg_temp_free(r_tc_off_ptr);
839
    tcg_temp_free(r_ptr);
840
    tcg_temp_free(r_tmp);
841
}
842

    
843
static inline void gen_save_btarget(target_ulong btarget)
844
{
845
    TCGv r_tmp = tcg_temp_new(TCG_TYPE_TL);
846

    
847
    tcg_gen_movi_tl(r_tmp, btarget);
848
    tcg_gen_st_tl(r_tmp, cpu_env, offsetof(CPUState, btarget));
849
    tcg_temp_free(r_tmp);
850
}
851

    
852
static always_inline void gen_save_breg_target(int reg)
853
{
854
    TCGv r_tmp = tcg_temp_new(TCG_TYPE_TL);
855

    
856
    gen_load_gpr(r_tmp, reg);
857
    tcg_gen_st_tl(r_tmp, cpu_env, offsetof(CPUState, btarget));
858
    tcg_temp_free(r_tmp);
859
}
860

    
861
static always_inline void save_cpu_state (DisasContext *ctx, int do_save_pc)
862
{
863
#if defined MIPS_DEBUG_DISAS
864
    if (loglevel & CPU_LOG_TB_IN_ASM) {
865
            fprintf(logfile, "hflags %08x saved %08x\n",
866
                    ctx->hflags, ctx->saved_hflags);
867
    }
868
#endif
869
    if (do_save_pc && ctx->pc != ctx->saved_pc) {
870
        gen_save_pc(ctx->pc);
871
        ctx->saved_pc = ctx->pc;
872
    }
873
    if (ctx->hflags != ctx->saved_hflags) {
874
        TCGv r_tmp = tcg_temp_new(TCG_TYPE_I32);
875

    
876
        tcg_gen_movi_i32(r_tmp, ctx->hflags);
877
        tcg_gen_st_i32(r_tmp, cpu_env, offsetof(CPUState, hflags));
878
        tcg_temp_free(r_tmp);
879
        ctx->saved_hflags = ctx->hflags;
880
        switch (ctx->hflags & MIPS_HFLAG_BMASK) {
881
        case MIPS_HFLAG_BR:
882
            break;
883
        case MIPS_HFLAG_BC:
884
        case MIPS_HFLAG_BL:
885
        case MIPS_HFLAG_B:
886
            gen_save_btarget(ctx->btarget);
887
            break;
888
        }
889
    }
890
}
891

    
892
static always_inline void restore_cpu_state (CPUState *env, DisasContext *ctx)
893
{
894
    ctx->saved_hflags = ctx->hflags;
895
    switch (ctx->hflags & MIPS_HFLAG_BMASK) {
896
    case MIPS_HFLAG_BR:
897
        break;
898
    case MIPS_HFLAG_BC:
899
    case MIPS_HFLAG_BL:
900
    case MIPS_HFLAG_B:
901
        ctx->btarget = env->btarget;
902
        break;
903
    }
904
}
905

    
906
static always_inline void
907
generate_exception_err (DisasContext *ctx, int excp, int err)
908
{
909
    save_cpu_state(ctx, 1);
910
    tcg_gen_helper_0_ii(do_raise_exception_err, excp, err);
911
    tcg_gen_helper_0_0(do_interrupt_restart);
912
    tcg_gen_exit_tb(0);
913
}
914

    
915
static always_inline void
916
generate_exception (DisasContext *ctx, int excp)
917
{
918
    save_cpu_state(ctx, 1);
919
    tcg_gen_helper_0_i(do_raise_exception, excp);
920
    tcg_gen_helper_0_0(do_interrupt_restart);
921
    tcg_gen_exit_tb(0);
922
}
923

    
924
/* Addresses computation */
925
static inline void gen_op_addr_add (TCGv t0, TCGv t1)
926
{
927
    tcg_gen_add_tl(t0, t0, t1);
928

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

    
937
        tcg_gen_ld_i32(r_tmp, cpu_env, offsetof(CPUState, hflags));
938
        tcg_gen_andi_i32(r_tmp, r_tmp, MIPS_HFLAG_KSU);
939
        tcg_gen_brcondi_i32(TCG_COND_NE, r_tmp, MIPS_HFLAG_UM, l1);
940
        tcg_gen_ld_i32(r_tmp, cpu_env, offsetof(CPUState, CP0_Status));
941
        tcg_gen_andi_i32(r_tmp, r_tmp, (1 << CP0St_UX));
942
        tcg_gen_brcondi_i32(TCG_COND_NE, r_tmp, 0, l1);
943
        tcg_temp_free(r_tmp);
944
        tcg_gen_ext32s_i64(t0, t0);
945
        gen_set_label(l1);
946
    }
947
#endif
948
}
949

    
950
static always_inline void check_cp0_enabled(DisasContext *ctx)
951
{
952
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_CP0)))
953
        generate_exception_err(ctx, EXCP_CpU, 1);
954
}
955

    
956
static always_inline void check_cp1_enabled(DisasContext *ctx)
957
{
958
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_FPU)))
959
        generate_exception_err(ctx, EXCP_CpU, 1);
960
}
961

    
962
/* Verify that the processor is running with COP1X instructions enabled.
963
   This is associated with the nabla symbol in the MIPS32 and MIPS64
964
   opcode tables.  */
965

    
966
static always_inline void check_cop1x(DisasContext *ctx)
967
{
968
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_COP1X)))
969
        generate_exception(ctx, EXCP_RI);
970
}
971

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

    
975
static always_inline void check_cp1_64bitmode(DisasContext *ctx)
976
{
977
    if (unlikely(~ctx->hflags & (MIPS_HFLAG_F64 | MIPS_HFLAG_COP1X)))
978
        generate_exception(ctx, EXCP_RI);
979
}
980

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

    
998
/* This code generates a "reserved instruction" exception if the
999
   CPU does not support the instruction set corresponding to flags. */
1000
static always_inline void check_insn(CPUState *env, DisasContext *ctx, int flags)
1001
{
1002
    if (unlikely(!(env->insn_flags & flags)))
1003
        generate_exception(ctx, EXCP_RI);
1004
}
1005

    
1006
/* This code generates a "reserved instruction" exception if 64-bit
1007
   instructions are not enabled. */
1008
static always_inline void check_mips_64(DisasContext *ctx)
1009
{
1010
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_64)))
1011
        generate_exception(ctx, EXCP_RI);
1012
}
1013

    
1014
/* load/store instructions. */
1015
#define OP_LD(insn,fname)                                        \
1016
void inline op_ldst_##insn(TCGv t0, DisasContext *ctx)           \
1017
{                                                                \
1018
    tcg_gen_qemu_##fname(t0, t0, ctx->mem_idx);                  \
1019
}
1020
OP_LD(lb,ld8s);
1021
OP_LD(lbu,ld8u);
1022
OP_LD(lh,ld16s);
1023
OP_LD(lhu,ld16u);
1024
OP_LD(lw,ld32s);
1025
#if defined(TARGET_MIPS64)
1026
OP_LD(lwu,ld32u);
1027
OP_LD(ld,ld64);
1028
#endif
1029
#undef OP_LD
1030

    
1031
#define OP_ST(insn,fname)                                        \
1032
void inline op_ldst_##insn(TCGv t0, TCGv t1, DisasContext *ctx)  \
1033
{                                                                \
1034
    tcg_gen_qemu_##fname(t1, t0, ctx->mem_idx);                  \
1035
}
1036
OP_ST(sb,st8);
1037
OP_ST(sh,st16);
1038
OP_ST(sw,st32);
1039
#if defined(TARGET_MIPS64)
1040
OP_ST(sd,st64);
1041
#endif
1042
#undef OP_ST
1043

    
1044
#define OP_LD_ATOMIC(insn,fname)                                        \
1045
void inline op_ldst_##insn(TCGv t0, TCGv t1, DisasContext *ctx)         \
1046
{                                                                       \
1047
    tcg_gen_mov_tl(t1, t0);                                             \
1048
    tcg_gen_qemu_##fname(t0, t0, ctx->mem_idx);                         \
1049
    tcg_gen_st_tl(t1, cpu_env, offsetof(CPUState, CP0_LLAddr));         \
1050
}
1051
OP_LD_ATOMIC(ll,ld32s);
1052
#if defined(TARGET_MIPS64)
1053
OP_LD_ATOMIC(lld,ld64);
1054
#endif
1055
#undef OP_LD_ATOMIC
1056

    
1057
#define OP_ST_ATOMIC(insn,fname,almask)                                 \
1058
void inline op_ldst_##insn(TCGv t0, TCGv t1, DisasContext *ctx)         \
1059
{                                                                       \
1060
    TCGv r_tmp = tcg_temp_local_new(TCG_TYPE_TL);                       \
1061
    int l1 = gen_new_label();                                           \
1062
    int l2 = gen_new_label();                                           \
1063
    int l3 = gen_new_label();                                           \
1064
                                                                        \
1065
    tcg_gen_andi_tl(r_tmp, t0, almask);                                 \
1066
    tcg_gen_brcondi_tl(TCG_COND_EQ, r_tmp, 0, l1);                      \
1067
    tcg_gen_st_tl(t0, cpu_env, offsetof(CPUState, CP0_BadVAddr));       \
1068
    generate_exception(ctx, EXCP_AdES);                                 \
1069
    gen_set_label(l1);                                                  \
1070
    tcg_gen_ld_tl(r_tmp, cpu_env, offsetof(CPUState, CP0_LLAddr));      \
1071
    tcg_gen_brcond_tl(TCG_COND_NE, t0, r_tmp, l2);                      \
1072
    tcg_temp_free(r_tmp);                                               \
1073
    tcg_gen_qemu_##fname(t1, t0, ctx->mem_idx);                         \
1074
    tcg_gen_movi_tl(t0, 1);                                             \
1075
    tcg_gen_br(l3);                                                     \
1076
    gen_set_label(l2);                                                  \
1077
    tcg_gen_movi_tl(t0, 0);                                             \
1078
    gen_set_label(l3);                                                  \
1079
}
1080
OP_ST_ATOMIC(sc,st32,0x3);
1081
#if defined(TARGET_MIPS64)
1082
OP_ST_ATOMIC(scd,st64,0x7);
1083
#endif
1084
#undef OP_ST_ATOMIC
1085

    
1086
/* Load and store */
1087
static void gen_ldst (DisasContext *ctx, uint32_t opc, int rt,
1088
                      int base, int16_t offset)
1089
{
1090
    const char *opn = "ldst";
1091
    TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
1092
    TCGv t1 = tcg_temp_local_new(TCG_TYPE_TL);
1093

    
1094
    if (base == 0) {
1095
        tcg_gen_movi_tl(t0, offset);
1096
    } else if (offset == 0) {
1097
        gen_load_gpr(t0, base);
1098
    } else {
1099
        gen_load_gpr(t0, base);
1100
        tcg_gen_movi_tl(t1, offset);
1101
        gen_op_addr_add(t0, t1);
1102
    }
1103
    /* Don't do NOP if destination is zero: we must perform the actual
1104
       memory access. */
1105
    switch (opc) {
1106
#if defined(TARGET_MIPS64)
1107
    case OPC_LWU:
1108
        op_ldst_lwu(t0, ctx);
1109
        gen_store_gpr(t0, rt);
1110
        opn = "lwu";
1111
        break;
1112
    case OPC_LD:
1113
        op_ldst_ld(t0, ctx);
1114
        gen_store_gpr(t0, rt);
1115
        opn = "ld";
1116
        break;
1117
    case OPC_LLD:
1118
        op_ldst_lld(t0, t1, ctx);
1119
        gen_store_gpr(t0, rt);
1120
        opn = "lld";
1121
        break;
1122
    case OPC_SD:
1123
        gen_load_gpr(t1, rt);
1124
        op_ldst_sd(t0, t1, ctx);
1125
        opn = "sd";
1126
        break;
1127
    case OPC_SCD:
1128
        save_cpu_state(ctx, 1);
1129
        gen_load_gpr(t1, rt);
1130
        op_ldst_scd(t0, t1, ctx);
1131
        gen_store_gpr(t0, rt);
1132
        opn = "scd";
1133
        break;
1134
    case OPC_LDL:
1135
        save_cpu_state(ctx, 1);
1136
        gen_load_gpr(t1, rt);
1137
        tcg_gen_helper_1_2i(do_ldl, t1, t0, t1, ctx->mem_idx);
1138
        gen_store_gpr(t1, rt);
1139
        opn = "ldl";
1140
        break;
1141
    case OPC_SDL:
1142
        save_cpu_state(ctx, 1);
1143
        gen_load_gpr(t1, rt);
1144
        tcg_gen_helper_0_2i(do_sdl, t0, t1, ctx->mem_idx);
1145
        opn = "sdl";
1146
        break;
1147
    case OPC_LDR:
1148
        save_cpu_state(ctx, 1);
1149
        gen_load_gpr(t1, rt);
1150
        tcg_gen_helper_1_2i(do_ldr, t1, t0, t1, ctx->mem_idx);
1151
        gen_store_gpr(t1, rt);
1152
        opn = "ldr";
1153
        break;
1154
    case OPC_SDR:
1155
        save_cpu_state(ctx, 1);
1156
        gen_load_gpr(t1, rt);
1157
        tcg_gen_helper_0_2i(do_sdr, t0, t1, ctx->mem_idx);
1158
        opn = "sdr";
1159
        break;
1160
#endif
1161
    case OPC_LW:
1162
        op_ldst_lw(t0, ctx);
1163
        gen_store_gpr(t0, rt);
1164
        opn = "lw";
1165
        break;
1166
    case OPC_SW:
1167
        gen_load_gpr(t1, rt);
1168
        op_ldst_sw(t0, t1, ctx);
1169
        opn = "sw";
1170
        break;
1171
    case OPC_LH:
1172
        op_ldst_lh(t0, ctx);
1173
        gen_store_gpr(t0, rt);
1174
        opn = "lh";
1175
        break;
1176
    case OPC_SH:
1177
        gen_load_gpr(t1, rt);
1178
        op_ldst_sh(t0, t1, ctx);
1179
        opn = "sh";
1180
        break;
1181
    case OPC_LHU:
1182
        op_ldst_lhu(t0, ctx);
1183
        gen_store_gpr(t0, rt);
1184
        opn = "lhu";
1185
        break;
1186
    case OPC_LB:
1187
        op_ldst_lb(t0, ctx);
1188
        gen_store_gpr(t0, rt);
1189
        opn = "lb";
1190
        break;
1191
    case OPC_SB:
1192
        gen_load_gpr(t1, rt);
1193
        op_ldst_sb(t0, t1, ctx);
1194
        opn = "sb";
1195
        break;
1196
    case OPC_LBU:
1197
        op_ldst_lbu(t0, ctx);
1198
        gen_store_gpr(t0, rt);
1199
        opn = "lbu";
1200
        break;
1201
    case OPC_LWL:
1202
        save_cpu_state(ctx, 1);
1203
        gen_load_gpr(t1, rt);
1204
        tcg_gen_helper_1_2i(do_lwl, t1, t0, t1, ctx->mem_idx);
1205
        gen_store_gpr(t1, rt);
1206
        opn = "lwl";
1207
        break;
1208
    case OPC_SWL:
1209
        save_cpu_state(ctx, 1);
1210
        gen_load_gpr(t1, rt);
1211
        tcg_gen_helper_0_2i(do_swl, t0, t1, ctx->mem_idx);
1212
        opn = "swr";
1213
        break;
1214
    case OPC_LWR:
1215
        save_cpu_state(ctx, 1);
1216
        gen_load_gpr(t1, rt);
1217
        tcg_gen_helper_1_2i(do_lwr, t1, t0, t1, ctx->mem_idx);
1218
        gen_store_gpr(t1, rt);
1219
        opn = "lwr";
1220
        break;
1221
    case OPC_SWR:
1222
        save_cpu_state(ctx, 1);
1223
        gen_load_gpr(t1, rt);
1224
        tcg_gen_helper_0_2i(do_swr, t0, t1, ctx->mem_idx);
1225
        opn = "swr";
1226
        break;
1227
    case OPC_LL:
1228
        op_ldst_ll(t0, t1, ctx);
1229
        gen_store_gpr(t0, rt);
1230
        opn = "ll";
1231
        break;
1232
    case OPC_SC:
1233
        save_cpu_state(ctx, 1);
1234
        gen_load_gpr(t1, rt);
1235
        op_ldst_sc(t0, t1, ctx);
1236
        gen_store_gpr(t0, rt);
1237
        opn = "sc";
1238
        break;
1239
    default:
1240
        MIPS_INVAL(opn);
1241
        generate_exception(ctx, EXCP_RI);
1242
        goto out;
1243
    }
1244
    MIPS_DEBUG("%s %s, %d(%s)", opn, regnames[rt], offset, regnames[base]);
1245
 out:
1246
    tcg_temp_free(t0);
1247
    tcg_temp_free(t1);
1248
}
1249

    
1250
/* Load and store */
1251
static void gen_flt_ldst (DisasContext *ctx, uint32_t opc, int ft,
1252
                      int base, int16_t offset)
1253
{
1254
    const char *opn = "flt_ldst";
1255
    TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
1256

    
1257
    if (base == 0) {
1258
        tcg_gen_movi_tl(t0, offset);
1259
    } else if (offset == 0) {
1260
        gen_load_gpr(t0, base);
1261
    } else {
1262
        TCGv t1 = tcg_temp_local_new(TCG_TYPE_TL);
1263

    
1264
        gen_load_gpr(t0, base);
1265
        tcg_gen_movi_tl(t1, offset);
1266
        gen_op_addr_add(t0, t1);
1267
        tcg_temp_free(t1);
1268
    }
1269
    /* Don't do NOP if destination is zero: we must perform the actual
1270
       memory access. */
1271
    switch (opc) {
1272
    case OPC_LWC1:
1273
        tcg_gen_qemu_ld32s(fpu32_T[0], t0, ctx->mem_idx);
1274
        gen_store_fpr32(fpu32_T[0], ft);
1275
        opn = "lwc1";
1276
        break;
1277
    case OPC_SWC1:
1278
        gen_load_fpr32(fpu32_T[0], ft);
1279
        tcg_gen_qemu_st32(fpu32_T[0], t0, ctx->mem_idx);
1280
        opn = "swc1";
1281
        break;
1282
    case OPC_LDC1:
1283
        tcg_gen_qemu_ld64(fpu64_T[0], t0, ctx->mem_idx);
1284
        gen_store_fpr64(ctx, fpu64_T[0], ft);
1285
        opn = "ldc1";
1286
        break;
1287
    case OPC_SDC1:
1288
        gen_load_fpr64(ctx, fpu64_T[0], ft);
1289
        tcg_gen_qemu_st64(fpu64_T[0], t0, ctx->mem_idx);
1290
        opn = "sdc1";
1291
        break;
1292
    default:
1293
        MIPS_INVAL(opn);
1294
        generate_exception(ctx, EXCP_RI);
1295
        goto out;
1296
    }
1297
    MIPS_DEBUG("%s %s, %d(%s)", opn, fregnames[ft], offset, regnames[base]);
1298
 out:
1299
    tcg_temp_free(t0);
1300
}
1301

    
1302
/* Arithmetic with immediate operand */
1303
static void gen_arith_imm (CPUState *env, DisasContext *ctx, uint32_t opc,
1304
                           int rt, int rs, int16_t imm)
1305
{
1306
    target_ulong uimm;
1307
    const char *opn = "imm arith";
1308
    TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
1309

    
1310
    if (rt == 0 && opc != OPC_ADDI && opc != OPC_DADDI) {
1311
        /* If no destination, treat it as a NOP.
1312
           For addi, we must generate the overflow exception when needed. */
1313
        MIPS_DEBUG("NOP");
1314
        goto out;
1315
    }
1316
    uimm = (uint16_t)imm;
1317
    switch (opc) {
1318
    case OPC_ADDI:
1319
    case OPC_ADDIU:
1320
#if defined(TARGET_MIPS64)
1321
    case OPC_DADDI:
1322
    case OPC_DADDIU:
1323
#endif
1324
    case OPC_SLTI:
1325
    case OPC_SLTIU:
1326
        uimm = (target_long)imm; /* Sign extend to 32/64 bits */
1327
        /* Fall through. */
1328
    case OPC_ANDI:
1329
    case OPC_ORI:
1330
    case OPC_XORI:
1331
        gen_load_gpr(t0, rs);
1332
        break;
1333
    case OPC_LUI:
1334
        tcg_gen_movi_tl(t0, imm << 16);
1335
        break;
1336
    case OPC_SLL:
1337
    case OPC_SRA:
1338
    case OPC_SRL:
1339
#if defined(TARGET_MIPS64)
1340
    case OPC_DSLL:
1341
    case OPC_DSRA:
1342
    case OPC_DSRL:
1343
    case OPC_DSLL32:
1344
    case OPC_DSRA32:
1345
    case OPC_DSRL32:
1346
#endif
1347
        uimm &= 0x1f;
1348
        gen_load_gpr(t0, rs);
1349
        break;
1350
    }
1351
    switch (opc) {
1352
    case OPC_ADDI:
1353
        {
1354
            TCGv r_tmp1 = tcg_temp_local_new(TCG_TYPE_TL);
1355
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_TL);
1356
            int l1 = gen_new_label();
1357

    
1358
            save_cpu_state(ctx, 1);
1359
            tcg_gen_ext32s_tl(r_tmp1, t0);
1360
            tcg_gen_addi_tl(t0, r_tmp1, uimm);
1361

    
1362
            tcg_gen_xori_tl(r_tmp1, r_tmp1, uimm);
1363
            tcg_gen_xori_tl(r_tmp1, r_tmp1, -1);
1364
            tcg_gen_xori_tl(r_tmp2, t0, uimm);
1365
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
1366
            tcg_temp_free(r_tmp2);
1367
            tcg_gen_shri_tl(r_tmp1, r_tmp1, 31);
1368
            tcg_gen_brcondi_tl(TCG_COND_EQ, r_tmp1, 0, l1);
1369
            tcg_temp_free(r_tmp1);
1370
            /* operands of same sign, result different sign */
1371
            generate_exception(ctx, EXCP_OVERFLOW);
1372
            gen_set_label(l1);
1373

    
1374
            tcg_gen_ext32s_tl(t0, t0);
1375
        }
1376
        opn = "addi";
1377
        break;
1378
    case OPC_ADDIU:
1379
        tcg_gen_ext32s_tl(t0, t0);
1380
        tcg_gen_addi_tl(t0, t0, uimm);
1381
        tcg_gen_ext32s_tl(t0, t0);
1382
        opn = "addiu";
1383
        break;
1384
#if defined(TARGET_MIPS64)
1385
    case OPC_DADDI:
1386
        {
1387
            TCGv r_tmp1 = tcg_temp_local_new(TCG_TYPE_TL);
1388
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_TL);
1389
            int l1 = gen_new_label();
1390

    
1391
            save_cpu_state(ctx, 1);
1392
            tcg_gen_mov_tl(r_tmp1, t0);
1393
            tcg_gen_addi_tl(t0, t0, uimm);
1394

    
1395
            tcg_gen_xori_tl(r_tmp1, r_tmp1, uimm);
1396
            tcg_gen_xori_tl(r_tmp1, r_tmp1, -1);
1397
            tcg_gen_xori_tl(r_tmp2, t0, uimm);
1398
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
1399
            tcg_temp_free(r_tmp2);
1400
            tcg_gen_shri_tl(r_tmp1, r_tmp1, 63);
1401
            tcg_gen_brcondi_tl(TCG_COND_EQ, r_tmp1, 0, l1);
1402
            tcg_temp_free(r_tmp1);
1403
            /* operands of same sign, result different sign */
1404
            generate_exception(ctx, EXCP_OVERFLOW);
1405
            gen_set_label(l1);
1406
        }
1407
        opn = "daddi";
1408
        break;
1409
    case OPC_DADDIU:
1410
        tcg_gen_addi_tl(t0, t0, uimm);
1411
        opn = "daddiu";
1412
        break;
1413
#endif
1414
    case OPC_SLTI:
1415
        gen_op_lti(t0, uimm);
1416
        opn = "slti";
1417
        break;
1418
    case OPC_SLTIU:
1419
        gen_op_ltiu(t0, uimm);
1420
        opn = "sltiu";
1421
        break;
1422
    case OPC_ANDI:
1423
        tcg_gen_andi_tl(t0, t0, uimm);
1424
        opn = "andi";
1425
        break;
1426
    case OPC_ORI:
1427
        tcg_gen_ori_tl(t0, t0, uimm);
1428
        opn = "ori";
1429
        break;
1430
    case OPC_XORI:
1431
        tcg_gen_xori_tl(t0, t0, uimm);
1432
        opn = "xori";
1433
        break;
1434
    case OPC_LUI:
1435
        opn = "lui";
1436
        break;
1437
    case OPC_SLL:
1438
        tcg_gen_ext32u_tl(t0, t0);
1439
        tcg_gen_shli_tl(t0, t0, uimm);
1440
        tcg_gen_ext32s_tl(t0, t0);
1441
        opn = "sll";
1442
        break;
1443
    case OPC_SRA:
1444
        tcg_gen_ext32s_tl(t0, t0);
1445
        tcg_gen_sari_tl(t0, t0, uimm);
1446
        tcg_gen_ext32s_tl(t0, t0);
1447
        opn = "sra";
1448
        break;
1449
    case OPC_SRL:
1450
        switch ((ctx->opcode >> 21) & 0x1f) {
1451
        case 0:
1452
            tcg_gen_ext32u_tl(t0, t0);
1453
            tcg_gen_shri_tl(t0, t0, uimm);
1454
            tcg_gen_ext32s_tl(t0, t0);
1455
            opn = "srl";
1456
            break;
1457
        case 1:
1458
            /* rotr is decoded as srl on non-R2 CPUs */
1459
            if (env->insn_flags & ISA_MIPS32R2) {
1460
                if (uimm != 0) {
1461
                    TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
1462
                    TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I32);
1463

    
1464
                    tcg_gen_trunc_tl_i32(r_tmp1, t0);
1465
                    tcg_gen_movi_i32(r_tmp2, 0x20);
1466
                    tcg_gen_subi_i32(r_tmp2, r_tmp2, uimm);
1467
                    tcg_gen_shl_i32(r_tmp2, r_tmp1, r_tmp2);
1468
                    tcg_gen_shri_i32(r_tmp1, r_tmp1, uimm);
1469
                    tcg_gen_or_i32(r_tmp1, r_tmp1, r_tmp2);
1470
                    tcg_gen_ext_i32_tl(t0, r_tmp1);
1471
                    tcg_temp_free(r_tmp1);
1472
                    tcg_temp_free(r_tmp2);
1473
                }
1474
                opn = "rotr";
1475
            } else {
1476
                tcg_gen_ext32u_tl(t0, t0);
1477
                tcg_gen_shri_tl(t0, t0, uimm);
1478
                tcg_gen_ext32s_tl(t0, t0);
1479
                opn = "srl";
1480
            }
1481
            break;
1482
        default:
1483
            MIPS_INVAL("invalid srl flag");
1484
            generate_exception(ctx, EXCP_RI);
1485
            break;
1486
        }
1487
        break;
1488
#if defined(TARGET_MIPS64)
1489
    case OPC_DSLL:
1490
        tcg_gen_shli_tl(t0, t0, uimm);
1491
        opn = "dsll";
1492
        break;
1493
    case OPC_DSRA:
1494
        tcg_gen_sari_tl(t0, t0, uimm);
1495
        opn = "dsra";
1496
        break;
1497
    case OPC_DSRL:
1498
        switch ((ctx->opcode >> 21) & 0x1f) {
1499
        case 0:
1500
            tcg_gen_shri_tl(t0, t0, uimm);
1501
            opn = "dsrl";
1502
            break;
1503
        case 1:
1504
            /* drotr is decoded as dsrl on non-R2 CPUs */
1505
            if (env->insn_flags & ISA_MIPS32R2) {
1506
                if (uimm != 0) {
1507
                    TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_TL);
1508

    
1509
                    tcg_gen_movi_tl(r_tmp1, 0x40);
1510
                    tcg_gen_subi_tl(r_tmp1, r_tmp1, uimm);
1511
                    tcg_gen_shl_tl(r_tmp1, t0, r_tmp1);
1512
                    tcg_gen_shri_tl(t0, t0, uimm);
1513
                    tcg_gen_or_tl(t0, t0, r_tmp1);
1514
                    tcg_temp_free(r_tmp1);
1515
                }
1516
                opn = "drotr";
1517
            } else {
1518
                tcg_gen_shri_tl(t0, t0, uimm);
1519
                opn = "dsrl";
1520
            }
1521
            break;
1522
        default:
1523
            MIPS_INVAL("invalid dsrl flag");
1524
            generate_exception(ctx, EXCP_RI);
1525
            break;
1526
        }
1527
        break;
1528
    case OPC_DSLL32:
1529
        tcg_gen_shli_tl(t0, t0, uimm + 32);
1530
        opn = "dsll32";
1531
        break;
1532
    case OPC_DSRA32:
1533
        tcg_gen_sari_tl(t0, t0, uimm + 32);
1534
        opn = "dsra32";
1535
        break;
1536
    case OPC_DSRL32:
1537
        switch ((ctx->opcode >> 21) & 0x1f) {
1538
        case 0:
1539
            tcg_gen_shri_tl(t0, t0, uimm + 32);
1540
            opn = "dsrl32";
1541
            break;
1542
        case 1:
1543
            /* drotr32 is decoded as dsrl32 on non-R2 CPUs */
1544
            if (env->insn_flags & ISA_MIPS32R2) {
1545
                TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_TL);
1546
                TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_TL);
1547

    
1548
                tcg_gen_movi_tl(r_tmp1, 0x40);
1549
                tcg_gen_movi_tl(r_tmp2, 32);
1550
                tcg_gen_addi_tl(r_tmp2, r_tmp2, uimm);
1551
                tcg_gen_sub_tl(r_tmp1, r_tmp1, r_tmp2);
1552
                tcg_gen_shl_tl(r_tmp1, t0, r_tmp1);
1553
                tcg_gen_shr_tl(t0, t0, r_tmp2);
1554
                tcg_gen_or_tl(t0, t0, r_tmp1);
1555
                tcg_temp_free(r_tmp1);
1556
                tcg_temp_free(r_tmp2);
1557
                opn = "drotr32";
1558
            } else {
1559
                tcg_gen_shri_tl(t0, t0, uimm + 32);
1560
                opn = "dsrl32";
1561
            }
1562
            break;
1563
        default:
1564
            MIPS_INVAL("invalid dsrl32 flag");
1565
            generate_exception(ctx, EXCP_RI);
1566
            break;
1567
        }
1568
        break;
1569
#endif
1570
    default:
1571
        MIPS_INVAL(opn);
1572
        generate_exception(ctx, EXCP_RI);
1573
        goto out;
1574
    }
1575
    gen_store_gpr(t0, rt);
1576
    MIPS_DEBUG("%s %s, %s, " TARGET_FMT_lx, opn, regnames[rt], regnames[rs], uimm);
1577
 out:
1578
    tcg_temp_free(t0);
1579
}
1580

    
1581
/* Arithmetic */
1582
static void gen_arith (CPUState *env, DisasContext *ctx, uint32_t opc,
1583
                       int rd, int rs, int rt)
1584
{
1585
    const char *opn = "arith";
1586
    TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
1587
    TCGv t1 = tcg_temp_local_new(TCG_TYPE_TL);
1588

    
1589
    if (rd == 0 && opc != OPC_ADD && opc != OPC_SUB
1590
       && opc != OPC_DADD && opc != OPC_DSUB) {
1591
        /* If no destination, treat it as a NOP.
1592
           For add & sub, we must generate the overflow exception when needed. */
1593
        MIPS_DEBUG("NOP");
1594
        goto out;
1595
    }
1596
    gen_load_gpr(t0, rs);
1597
    /* Specialcase the conventional move operation. */
1598
    if (rt == 0 && (opc == OPC_ADDU || opc == OPC_DADDU
1599
                    || opc == OPC_SUBU || opc == OPC_DSUBU)) {
1600
        gen_store_gpr(t0, rd);
1601
        goto out;
1602
    }
1603
    gen_load_gpr(t1, rt);
1604
    switch (opc) {
1605
    case OPC_ADD:
1606
        {
1607
            TCGv r_tmp1 = tcg_temp_local_new(TCG_TYPE_TL);
1608
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_TL);
1609
            int l1 = gen_new_label();
1610

    
1611
            save_cpu_state(ctx, 1);
1612
            tcg_gen_ext32s_tl(r_tmp1, t0);
1613
            tcg_gen_ext32s_tl(r_tmp2, t1);
1614
            tcg_gen_add_tl(t0, r_tmp1, r_tmp2);
1615

    
1616
            tcg_gen_xor_tl(r_tmp1, r_tmp1, t1);
1617
            tcg_gen_xori_tl(r_tmp1, r_tmp1, -1);
1618
            tcg_gen_xor_tl(r_tmp2, t0, t1);
1619
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
1620
            tcg_temp_free(r_tmp2);
1621
            tcg_gen_shri_tl(r_tmp1, r_tmp1, 31);
1622
            tcg_gen_brcondi_tl(TCG_COND_EQ, r_tmp1, 0, l1);
1623
            tcg_temp_free(r_tmp1);
1624
            /* operands of same sign, result different sign */
1625
            generate_exception(ctx, EXCP_OVERFLOW);
1626
            gen_set_label(l1);
1627

    
1628
            tcg_gen_ext32s_tl(t0, t0);
1629
        }
1630
        opn = "add";
1631
        break;
1632
    case OPC_ADDU:
1633
        tcg_gen_ext32s_tl(t0, t0);
1634
        tcg_gen_ext32s_tl(t1, t1);
1635
        tcg_gen_add_tl(t0, t0, t1);
1636
        tcg_gen_ext32s_tl(t0, t0);
1637
        opn = "addu";
1638
        break;
1639
    case OPC_SUB:
1640
        {
1641
            TCGv r_tmp1 = tcg_temp_local_new(TCG_TYPE_TL);
1642
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_TL);
1643
            int l1 = gen_new_label();
1644

    
1645
            save_cpu_state(ctx, 1);
1646
            tcg_gen_ext32s_tl(r_tmp1, t0);
1647
            tcg_gen_ext32s_tl(r_tmp2, t1);
1648
            tcg_gen_sub_tl(t0, r_tmp1, r_tmp2);
1649

    
1650
            tcg_gen_xor_tl(r_tmp2, r_tmp1, t1);
1651
            tcg_gen_xor_tl(r_tmp1, r_tmp1, t0);
1652
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
1653
            tcg_temp_free(r_tmp2);
1654
            tcg_gen_shri_tl(r_tmp1, r_tmp1, 31);
1655
            tcg_gen_brcondi_tl(TCG_COND_EQ, r_tmp1, 0, l1);
1656
            tcg_temp_free(r_tmp1);
1657
            /* operands of different sign, first operand and result different sign */
1658
            generate_exception(ctx, EXCP_OVERFLOW);
1659
            gen_set_label(l1);
1660

    
1661
            tcg_gen_ext32s_tl(t0, t0);
1662
        }
1663
        opn = "sub";
1664
        break;
1665
    case OPC_SUBU:
1666
        tcg_gen_ext32s_tl(t0, t0);
1667
        tcg_gen_ext32s_tl(t1, t1);
1668
        tcg_gen_sub_tl(t0, t0, t1);
1669
        tcg_gen_ext32s_tl(t0, t0);
1670
        opn = "subu";
1671
        break;
1672
#if defined(TARGET_MIPS64)
1673
    case OPC_DADD:
1674
        {
1675
            TCGv r_tmp1 = tcg_temp_local_new(TCG_TYPE_TL);
1676
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_TL);
1677
            int l1 = gen_new_label();
1678

    
1679
            save_cpu_state(ctx, 1);
1680
            tcg_gen_mov_tl(r_tmp1, t0);
1681
            tcg_gen_add_tl(t0, t0, t1);
1682

    
1683
            tcg_gen_xor_tl(r_tmp1, r_tmp1, t1);
1684
            tcg_gen_xori_tl(r_tmp1, r_tmp1, -1);
1685
            tcg_gen_xor_tl(r_tmp2, t0, t1);
1686
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
1687
            tcg_temp_free(r_tmp2);
1688
            tcg_gen_shri_tl(r_tmp1, r_tmp1, 63);
1689
            tcg_gen_brcondi_tl(TCG_COND_EQ, r_tmp1, 0, l1);
1690
            tcg_temp_free(r_tmp1);
1691
            /* operands of same sign, result different sign */
1692
            generate_exception(ctx, EXCP_OVERFLOW);
1693
            gen_set_label(l1);
1694
        }
1695
        opn = "dadd";
1696
        break;
1697
    case OPC_DADDU:
1698
        tcg_gen_add_tl(t0, t0, t1);
1699
        opn = "daddu";
1700
        break;
1701
    case OPC_DSUB:
1702
        {
1703
            TCGv r_tmp1 = tcg_temp_local_new(TCG_TYPE_TL);
1704
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_TL);
1705
            int l1 = gen_new_label();
1706

    
1707
            save_cpu_state(ctx, 1);
1708
            tcg_gen_mov_tl(r_tmp1, t0);
1709
            tcg_gen_sub_tl(t0, t0, t1);
1710

    
1711
            tcg_gen_xor_tl(r_tmp2, r_tmp1, t1);
1712
            tcg_gen_xor_tl(r_tmp1, r_tmp1, t0);
1713
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
1714
            tcg_temp_free(r_tmp2);
1715
            tcg_gen_shri_tl(r_tmp1, r_tmp1, 63);
1716
            tcg_gen_brcondi_tl(TCG_COND_EQ, r_tmp1, 0, l1);
1717
            tcg_temp_free(r_tmp1);
1718
            /* operands of different sign, first operand and result different sign */
1719
            generate_exception(ctx, EXCP_OVERFLOW);
1720
            gen_set_label(l1);
1721
        }
1722
        opn = "dsub";
1723
        break;
1724
    case OPC_DSUBU:
1725
        tcg_gen_sub_tl(t0, t0, t1);
1726
        opn = "dsubu";
1727
        break;
1728
#endif
1729
    case OPC_SLT:
1730
        gen_op_lt(t0, t1);
1731
        opn = "slt";
1732
        break;
1733
    case OPC_SLTU:
1734
        gen_op_ltu(t0, t1);
1735
        opn = "sltu";
1736
        break;
1737
    case OPC_AND:
1738
        tcg_gen_and_tl(t0, t0, t1);
1739
        opn = "and";
1740
        break;
1741
    case OPC_NOR:
1742
        tcg_gen_or_tl(t0, t0, t1);
1743
        tcg_gen_not_tl(t0, t0);
1744
        opn = "nor";
1745
        break;
1746
    case OPC_OR:
1747
        tcg_gen_or_tl(t0, t0, t1);
1748
        opn = "or";
1749
        break;
1750
    case OPC_XOR:
1751
        tcg_gen_xor_tl(t0, t0, t1);
1752
        opn = "xor";
1753
        break;
1754
    case OPC_MUL:
1755
        tcg_gen_ext32s_tl(t0, t0);
1756
        tcg_gen_ext32s_tl(t1, t1);
1757
        tcg_gen_mul_tl(t0, t0, t1);
1758
        tcg_gen_ext32s_tl(t0, t0);
1759
        opn = "mul";
1760
        break;
1761
    case OPC_MOVN:
1762
        {
1763
            int l1 = gen_new_label();
1764

    
1765
            tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
1766
            gen_store_gpr(t0, rd);
1767
            gen_set_label(l1);
1768
        }
1769
        opn = "movn";
1770
        goto print;
1771
    case OPC_MOVZ:
1772
        {
1773
            int l1 = gen_new_label();
1774

    
1775
            tcg_gen_brcondi_tl(TCG_COND_NE, t1, 0, l1);
1776
            gen_store_gpr(t0, rd);
1777
            gen_set_label(l1);
1778
        }
1779
        opn = "movz";
1780
        goto print;
1781
    case OPC_SLLV:
1782
        tcg_gen_ext32u_tl(t0, t0);
1783
        tcg_gen_ext32u_tl(t1, t1);
1784
        tcg_gen_andi_tl(t0, t0, 0x1f);
1785
        tcg_gen_shl_tl(t0, t1, t0);
1786
        tcg_gen_ext32s_tl(t0, t0);
1787
        opn = "sllv";
1788
        break;
1789
    case OPC_SRAV:
1790
        tcg_gen_ext32s_tl(t1, t1);
1791
        tcg_gen_andi_tl(t0, t0, 0x1f);
1792
        tcg_gen_sar_tl(t0, t1, t0);
1793
        tcg_gen_ext32s_tl(t0, t0);
1794
        opn = "srav";
1795
        break;
1796
    case OPC_SRLV:
1797
        switch ((ctx->opcode >> 6) & 0x1f) {
1798
        case 0:
1799
            tcg_gen_ext32u_tl(t1, t1);
1800
            tcg_gen_andi_tl(t0, t0, 0x1f);
1801
            tcg_gen_shr_tl(t0, t1, t0);
1802
            tcg_gen_ext32s_tl(t0, t0);
1803
            opn = "srlv";
1804
            break;
1805
        case 1:
1806
            /* rotrv is decoded as srlv on non-R2 CPUs */
1807
            if (env->insn_flags & ISA_MIPS32R2) {
1808
                int l1 = gen_new_label();
1809
                int l2 = gen_new_label();
1810

    
1811
                tcg_gen_andi_tl(t0, t0, 0x1f);
1812
                tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
1813
                {
1814
                    TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
1815
                    TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I32);
1816
                    TCGv r_tmp3 = tcg_temp_new(TCG_TYPE_I32);
1817

    
1818
                    tcg_gen_trunc_tl_i32(r_tmp1, t0);
1819
                    tcg_gen_trunc_tl_i32(r_tmp2, t1);
1820
                    tcg_gen_movi_i32(r_tmp3, 0x20);
1821
                    tcg_gen_sub_i32(r_tmp3, r_tmp3, r_tmp1);
1822
                    tcg_gen_shl_i32(r_tmp3, r_tmp2, r_tmp3);
1823
                    tcg_gen_shr_i32(r_tmp1, r_tmp2, r_tmp1);
1824
                    tcg_gen_or_i32(r_tmp1, r_tmp1, r_tmp3);
1825
                    tcg_gen_ext_i32_tl(t0, r_tmp1);
1826
                    tcg_temp_free(r_tmp1);
1827
                    tcg_temp_free(r_tmp2);
1828
                    tcg_temp_free(r_tmp3);
1829
                    tcg_gen_br(l2);
1830
                }
1831
                gen_set_label(l1);
1832
                tcg_gen_mov_tl(t0, t1);
1833
                gen_set_label(l2);
1834
                opn = "rotrv";
1835
            } else {
1836
                tcg_gen_ext32u_tl(t1, t1);
1837
                tcg_gen_andi_tl(t0, t0, 0x1f);
1838
                tcg_gen_shr_tl(t0, t1, t0);
1839
                tcg_gen_ext32s_tl(t0, t0);
1840
                opn = "srlv";
1841
            }
1842
            break;
1843
        default:
1844
            MIPS_INVAL("invalid srlv flag");
1845
            generate_exception(ctx, EXCP_RI);
1846
            break;
1847
        }
1848
        break;
1849
#if defined(TARGET_MIPS64)
1850
    case OPC_DSLLV:
1851
        tcg_gen_andi_tl(t0, t0, 0x3f);
1852
        tcg_gen_shl_tl(t0, t1, t0);
1853
        opn = "dsllv";
1854
        break;
1855
    case OPC_DSRAV:
1856
        tcg_gen_andi_tl(t0, t0, 0x3f);
1857
        tcg_gen_sar_tl(t0, t1, t0);
1858
        opn = "dsrav";
1859
        break;
1860
    case OPC_DSRLV:
1861
        switch ((ctx->opcode >> 6) & 0x1f) {
1862
        case 0:
1863
            tcg_gen_andi_tl(t0, t0, 0x3f);
1864
            tcg_gen_shr_tl(t0, t1, t0);
1865
            opn = "dsrlv";
1866
            break;
1867
        case 1:
1868
            /* drotrv is decoded as dsrlv on non-R2 CPUs */
1869
            if (env->insn_flags & ISA_MIPS32R2) {
1870
                int l1 = gen_new_label();
1871
                int l2 = gen_new_label();
1872

    
1873
                tcg_gen_andi_tl(t0, t0, 0x3f);
1874
                tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
1875
                {
1876
                    TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_TL);
1877

    
1878
                    tcg_gen_movi_tl(r_tmp1, 0x40);
1879
                    tcg_gen_sub_tl(r_tmp1, r_tmp1, t0);
1880
                    tcg_gen_shl_tl(r_tmp1, t1, r_tmp1);
1881
                    tcg_gen_shr_tl(t0, t1, t0);
1882
                    tcg_gen_or_tl(t0, t0, r_tmp1);
1883
                    tcg_temp_free(r_tmp1);
1884
                    tcg_gen_br(l2);
1885
                }
1886
                gen_set_label(l1);
1887
                tcg_gen_mov_tl(t0, t1);
1888
                gen_set_label(l2);
1889
                opn = "drotrv";
1890
            } else {
1891
                tcg_gen_andi_tl(t0, t0, 0x3f);
1892
                tcg_gen_shr_tl(t0, t1, t0);
1893
                opn = "dsrlv";
1894
            }
1895
            break;
1896
        default:
1897
            MIPS_INVAL("invalid dsrlv flag");
1898
            generate_exception(ctx, EXCP_RI);
1899
            break;
1900
        }
1901
        break;
1902
#endif
1903
    default:
1904
        MIPS_INVAL(opn);
1905
        generate_exception(ctx, EXCP_RI);
1906
        goto out;
1907
    }
1908
    gen_store_gpr(t0, rd);
1909
 print:
1910
    MIPS_DEBUG("%s %s, %s, %s", opn, regnames[rd], regnames[rs], regnames[rt]);
1911
 out:
1912
    tcg_temp_free(t0);
1913
    tcg_temp_free(t1);
1914
}
1915

    
1916
/* Arithmetic on HI/LO registers */
1917
static void gen_HILO (DisasContext *ctx, uint32_t opc, int reg)
1918
{
1919
    const char *opn = "hilo";
1920
    TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
1921

    
1922
    if (reg == 0 && (opc == OPC_MFHI || opc == OPC_MFLO)) {
1923
        /* Treat as NOP. */
1924
        MIPS_DEBUG("NOP");
1925
        goto out;
1926
    }
1927
    switch (opc) {
1928
    case OPC_MFHI:
1929
        gen_load_HI(t0, 0);
1930
        gen_store_gpr(t0, reg);
1931
        opn = "mfhi";
1932
        break;
1933
    case OPC_MFLO:
1934
        gen_load_LO(t0, 0);
1935
        gen_store_gpr(t0, reg);
1936
        opn = "mflo";
1937
        break;
1938
    case OPC_MTHI:
1939
        gen_load_gpr(t0, reg);
1940
        gen_store_HI(t0, 0);
1941
        opn = "mthi";
1942
        break;
1943
    case OPC_MTLO:
1944
        gen_load_gpr(t0, reg);
1945
        gen_store_LO(t0, 0);
1946
        opn = "mtlo";
1947
        break;
1948
    default:
1949
        MIPS_INVAL(opn);
1950
        generate_exception(ctx, EXCP_RI);
1951
        goto out;
1952
    }
1953
    MIPS_DEBUG("%s %s", opn, regnames[reg]);
1954
 out:
1955
    tcg_temp_free(t0);
1956
}
1957

    
1958
static void gen_muldiv (DisasContext *ctx, uint32_t opc,
1959
                        int rs, int rt)
1960
{
1961
    const char *opn = "mul/div";
1962
    TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
1963
    TCGv t1 = tcg_temp_local_new(TCG_TYPE_TL);
1964

    
1965
    gen_load_gpr(t0, rs);
1966
    gen_load_gpr(t1, rt);
1967
    switch (opc) {
1968
    case OPC_DIV:
1969
        {
1970
            int l1 = gen_new_label();
1971

    
1972
            tcg_gen_ext32s_tl(t0, t0);
1973
            tcg_gen_ext32s_tl(t1, t1);
1974
            tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
1975
            {
1976
                TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I64);
1977
                TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I64);
1978
                TCGv r_tmp3 = tcg_temp_new(TCG_TYPE_I64);
1979

    
1980
                tcg_gen_ext_tl_i64(r_tmp1, t0);
1981
                tcg_gen_ext_tl_i64(r_tmp2, t1);
1982
                tcg_gen_div_i64(r_tmp3, r_tmp1, r_tmp2);
1983
                tcg_gen_rem_i64(r_tmp2, r_tmp1, r_tmp2);
1984
                tcg_gen_trunc_i64_tl(t0, r_tmp3);
1985
                tcg_gen_trunc_i64_tl(t1, r_tmp2);
1986
                tcg_temp_free(r_tmp1);
1987
                tcg_temp_free(r_tmp2);
1988
                tcg_temp_free(r_tmp3);
1989
                tcg_gen_ext32s_tl(t0, t0);
1990
                tcg_gen_ext32s_tl(t1, t1);
1991
                gen_store_LO(t0, 0);
1992
                gen_store_HI(t1, 0);
1993
            }
1994
            gen_set_label(l1);
1995
        }
1996
        opn = "div";
1997
        break;
1998
    case OPC_DIVU:
1999
        {
2000
            int l1 = gen_new_label();
2001

    
2002
            tcg_gen_ext32s_tl(t1, t1);
2003
            tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
2004
            {
2005
                TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
2006
                TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I32);
2007
                TCGv r_tmp3 = tcg_temp_new(TCG_TYPE_I32);
2008

    
2009
                tcg_gen_trunc_tl_i32(r_tmp1, t0);
2010
                tcg_gen_trunc_tl_i32(r_tmp2, t1);
2011
                tcg_gen_divu_i32(r_tmp3, r_tmp1, r_tmp2);
2012
                tcg_gen_remu_i32(r_tmp1, r_tmp1, r_tmp2);
2013
                tcg_gen_ext_i32_tl(t0, r_tmp3);
2014
                tcg_gen_ext_i32_tl(t1, r_tmp1);
2015
                tcg_temp_free(r_tmp1);
2016
                tcg_temp_free(r_tmp2);
2017
                tcg_temp_free(r_tmp3);
2018
                gen_store_LO(t0, 0);
2019
                gen_store_HI(t1, 0);
2020
            }
2021
            gen_set_label(l1);
2022
        }
2023
        opn = "divu";
2024
        break;
2025
    case OPC_MULT:
2026
        {
2027
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I64);
2028
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I64);
2029

    
2030
            tcg_gen_ext32s_tl(t0, t0);
2031
            tcg_gen_ext32s_tl(t1, t1);
2032
            tcg_gen_ext_tl_i64(r_tmp1, t0);
2033
            tcg_gen_ext_tl_i64(r_tmp2, t1);
2034
            tcg_gen_mul_i64(r_tmp1, r_tmp1, r_tmp2);
2035
            tcg_temp_free(r_tmp2);
2036
            tcg_gen_trunc_i64_tl(t0, r_tmp1);
2037
            tcg_gen_shri_i64(r_tmp1, r_tmp1, 32);
2038
            tcg_gen_trunc_i64_tl(t1, r_tmp1);
2039
            tcg_temp_free(r_tmp1);
2040
            tcg_gen_ext32s_tl(t0, t0);
2041
            tcg_gen_ext32s_tl(t1, t1);
2042
            gen_store_LO(t0, 0);
2043
            gen_store_HI(t1, 0);
2044
        }
2045
        opn = "mult";
2046
        break;
2047
    case OPC_MULTU:
2048
        {
2049
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I64);
2050
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I64);
2051

    
2052
            tcg_gen_ext32u_tl(t0, t0);
2053
            tcg_gen_ext32u_tl(t1, t1);
2054
            tcg_gen_extu_tl_i64(r_tmp1, t0);
2055
            tcg_gen_extu_tl_i64(r_tmp2, t1);
2056
            tcg_gen_mul_i64(r_tmp1, r_tmp1, r_tmp2);
2057
            tcg_temp_free(r_tmp2);
2058
            tcg_gen_trunc_i64_tl(t0, r_tmp1);
2059
            tcg_gen_shri_i64(r_tmp1, r_tmp1, 32);
2060
            tcg_gen_trunc_i64_tl(t1, r_tmp1);
2061
            tcg_temp_free(r_tmp1);
2062
            tcg_gen_ext32s_tl(t0, t0);
2063
            tcg_gen_ext32s_tl(t1, t1);
2064
            gen_store_LO(t0, 0);
2065
            gen_store_HI(t1, 0);
2066
        }
2067
        opn = "multu";
2068
        break;
2069
#if defined(TARGET_MIPS64)
2070
    case OPC_DDIV:
2071
        {
2072
            int l1 = gen_new_label();
2073

    
2074
            tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
2075
            {
2076
                int l2 = gen_new_label();
2077

    
2078
                tcg_gen_brcondi_tl(TCG_COND_NE, t0, -1LL << 63, l2);
2079
                tcg_gen_brcondi_tl(TCG_COND_NE, t1, -1LL, l2);
2080
                {
2081
                    tcg_gen_movi_tl(t1, 0);
2082
                    gen_store_LO(t0, 0);
2083
                    gen_store_HI(t1, 0);
2084
                    tcg_gen_br(l1);
2085
                }
2086
                gen_set_label(l2);
2087
                {
2088
                    TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I64);
2089
                    TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I64);
2090

    
2091
                    tcg_gen_div_i64(r_tmp1, t0, t1);
2092
                    tcg_gen_rem_i64(r_tmp2, t0, t1);
2093
                    gen_store_LO(r_tmp1, 0);
2094
                    gen_store_HI(r_tmp2, 0);
2095
                    tcg_temp_free(r_tmp1);
2096
                    tcg_temp_free(r_tmp2);
2097
                }
2098
            }
2099
            gen_set_label(l1);
2100
        }
2101
        opn = "ddiv";
2102
        break;
2103
    case OPC_DDIVU:
2104
        {
2105
            int l1 = gen_new_label();
2106

    
2107
            tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
2108
            {
2109
                TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I64);
2110
                TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I64);
2111

    
2112
                tcg_gen_divu_i64(r_tmp1, t0, t1);
2113
                tcg_gen_remu_i64(r_tmp2, t0, t1);
2114
                tcg_temp_free(r_tmp1);
2115
                tcg_temp_free(r_tmp2);
2116
                gen_store_LO(r_tmp1, 0);
2117
                gen_store_HI(r_tmp2, 0);
2118
            }
2119
            gen_set_label(l1);
2120
        }
2121
        opn = "ddivu";
2122
        break;
2123
    case OPC_DMULT:
2124
        tcg_gen_helper_0_2(do_dmult, t0, t1);
2125
        opn = "dmult";
2126
        break;
2127
    case OPC_DMULTU:
2128
        tcg_gen_helper_0_2(do_dmultu, t0, t1);
2129
        opn = "dmultu";
2130
        break;
2131
#endif
2132
    case OPC_MADD:
2133
        {
2134
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I64);
2135
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I64);
2136
            TCGv r_tmp3 = tcg_temp_new(TCG_TYPE_I64);
2137

    
2138
            tcg_gen_ext32s_tl(t0, t0);
2139
            tcg_gen_ext32s_tl(t1, t1);
2140
            tcg_gen_ext_tl_i64(r_tmp1, t0);
2141
            tcg_gen_ext_tl_i64(r_tmp2, t1);
2142
            tcg_gen_mul_i64(r_tmp1, r_tmp1, r_tmp2);
2143
            gen_load_LO(t0, 0);
2144
            gen_load_HI(t1, 0);
2145
            tcg_gen_extu_tl_i64(r_tmp2, t0);
2146
            tcg_gen_extu_tl_i64(r_tmp3, t1);
2147
            tcg_gen_shli_i64(r_tmp3, r_tmp3, 32);
2148
            tcg_gen_or_i64(r_tmp2, r_tmp2, r_tmp3);
2149
            tcg_temp_free(r_tmp3);
2150
            tcg_gen_add_i64(r_tmp1, r_tmp1, r_tmp2);
2151
            tcg_temp_free(r_tmp2);
2152
            tcg_gen_trunc_i64_tl(t0, r_tmp1);
2153
            tcg_gen_shri_i64(r_tmp1, r_tmp1, 32);
2154
            tcg_gen_trunc_i64_tl(t1, r_tmp1);
2155
            tcg_temp_free(r_tmp1);
2156
            tcg_gen_ext32s_tl(t0, t0);
2157
            tcg_gen_ext32s_tl(t1, t1);
2158
            gen_store_LO(t0, 0);
2159
            gen_store_HI(t1, 0);
2160
        }
2161
        opn = "madd";
2162
        break;
2163
    case OPC_MADDU:
2164
       {
2165
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I64);
2166
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I64);
2167
            TCGv r_tmp3 = tcg_temp_new(TCG_TYPE_I64);
2168

    
2169
            tcg_gen_ext32u_tl(t0, t0);
2170
            tcg_gen_ext32u_tl(t1, t1);
2171
            tcg_gen_extu_tl_i64(r_tmp1, t0);
2172
            tcg_gen_extu_tl_i64(r_tmp2, t1);
2173
            tcg_gen_mul_i64(r_tmp1, r_tmp1, r_tmp2);
2174
            gen_load_LO(t0, 0);
2175
            gen_load_HI(t1, 0);
2176
            tcg_gen_extu_tl_i64(r_tmp2, t0);
2177
            tcg_gen_extu_tl_i64(r_tmp3, t1);
2178
            tcg_gen_shli_i64(r_tmp3, r_tmp3, 32);
2179
            tcg_gen_or_i64(r_tmp2, r_tmp2, r_tmp3);
2180
            tcg_temp_free(r_tmp3);
2181
            tcg_gen_add_i64(r_tmp1, r_tmp1, r_tmp2);
2182
            tcg_temp_free(r_tmp2);
2183
            tcg_gen_trunc_i64_tl(t0, r_tmp1);
2184
            tcg_gen_shri_i64(r_tmp1, r_tmp1, 32);
2185
            tcg_gen_trunc_i64_tl(t1, r_tmp1);
2186
            tcg_temp_free(r_tmp1);
2187
            tcg_gen_ext32s_tl(t0, t0);
2188
            tcg_gen_ext32s_tl(t1, t1);
2189
            gen_store_LO(t0, 0);
2190
            gen_store_HI(t1, 0);
2191
        }
2192
        opn = "maddu";
2193
        break;
2194
    case OPC_MSUB:
2195
        {
2196
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I64);
2197
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I64);
2198
            TCGv r_tmp3 = tcg_temp_new(TCG_TYPE_I64);
2199

    
2200
            tcg_gen_ext32s_tl(t0, t0);
2201
            tcg_gen_ext32s_tl(t1, t1);
2202
            tcg_gen_ext_tl_i64(r_tmp1, t0);
2203
            tcg_gen_ext_tl_i64(r_tmp2, t1);
2204
            tcg_gen_mul_i64(r_tmp1, r_tmp1, r_tmp2);
2205
            gen_load_LO(t0, 0);
2206
            gen_load_HI(t1, 0);
2207
            tcg_gen_extu_tl_i64(r_tmp2, t0);
2208
            tcg_gen_extu_tl_i64(r_tmp3, t1);
2209
            tcg_gen_shli_i64(r_tmp3, r_tmp3, 32);
2210
            tcg_gen_or_i64(r_tmp2, r_tmp2, r_tmp3);
2211
            tcg_temp_free(r_tmp3);
2212
            tcg_gen_sub_i64(r_tmp1, r_tmp1, r_tmp2);
2213
            tcg_temp_free(r_tmp2);
2214
            tcg_gen_trunc_i64_tl(t0, r_tmp1);
2215
            tcg_gen_shri_i64(r_tmp1, r_tmp1, 32);
2216
            tcg_gen_trunc_i64_tl(t1, r_tmp1);
2217
            tcg_temp_free(r_tmp1);
2218
            tcg_gen_ext32s_tl(t0, t0);
2219
            tcg_gen_ext32s_tl(t1, t1);
2220
            gen_store_LO(t0, 0);
2221
            gen_store_HI(t1, 0);
2222
        }
2223
        opn = "msub";
2224
        break;
2225
    case OPC_MSUBU:
2226
        {
2227
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I64);
2228
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I64);
2229
            TCGv r_tmp3 = tcg_temp_new(TCG_TYPE_I64);
2230

    
2231
            tcg_gen_ext32u_tl(t0, t0);
2232
            tcg_gen_ext32u_tl(t1, t1);
2233
            tcg_gen_extu_tl_i64(r_tmp1, t0);
2234
            tcg_gen_extu_tl_i64(r_tmp2, t1);
2235
            tcg_gen_mul_i64(r_tmp1, r_tmp1, r_tmp2);
2236
            gen_load_LO(t0, 0);
2237
            gen_load_HI(t1, 0);
2238
            tcg_gen_extu_tl_i64(r_tmp2, t0);
2239
            tcg_gen_extu_tl_i64(r_tmp3, t1);
2240
            tcg_gen_shli_i64(r_tmp3, r_tmp3, 32);
2241
            tcg_gen_or_i64(r_tmp2, r_tmp2, r_tmp3);
2242
            tcg_temp_free(r_tmp3);
2243
            tcg_gen_sub_i64(r_tmp1, r_tmp1, r_tmp2);
2244
            tcg_temp_free(r_tmp2);
2245
            tcg_gen_trunc_i64_tl(t0, r_tmp1);
2246
            tcg_gen_shri_i64(r_tmp1, r_tmp1, 32);
2247
            tcg_gen_trunc_i64_tl(t1, r_tmp1);
2248
            tcg_temp_free(r_tmp1);
2249
            tcg_gen_ext32s_tl(t0, t0);
2250
            tcg_gen_ext32s_tl(t1, t1);
2251
            gen_store_LO(t0, 0);
2252
            gen_store_HI(t1, 0);
2253
        }
2254
        opn = "msubu";
2255
        break;
2256
    default:
2257
        MIPS_INVAL(opn);
2258
        generate_exception(ctx, EXCP_RI);
2259
        goto out;
2260
    }
2261
    MIPS_DEBUG("%s %s %s", opn, regnames[rs], regnames[rt]);
2262
 out:
2263
    tcg_temp_free(t0);
2264
    tcg_temp_free(t1);
2265
}
2266

    
2267
static void gen_mul_vr54xx (DisasContext *ctx, uint32_t opc,
2268
                            int rd, int rs, int rt)
2269
{
2270
    const char *opn = "mul vr54xx";
2271
    TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
2272
    TCGv t1 = tcg_temp_local_new(TCG_TYPE_TL);
2273

    
2274
    gen_load_gpr(t0, rs);
2275
    gen_load_gpr(t1, rt);
2276

    
2277
    switch (opc) {
2278
    case OPC_VR54XX_MULS:
2279
        tcg_gen_helper_1_2(do_muls, t0, t0, t1);
2280
        opn = "muls";
2281
        break;
2282
    case OPC_VR54XX_MULSU:
2283
        tcg_gen_helper_1_2(do_mulsu, t0, t0, t1);
2284
        opn = "mulsu";
2285
        break;
2286
    case OPC_VR54XX_MACC:
2287
        tcg_gen_helper_1_2(do_macc, t0, t0, t1);
2288
        opn = "macc";
2289
        break;
2290
    case OPC_VR54XX_MACCU:
2291
        tcg_gen_helper_1_2(do_maccu, t0, t0, t1);
2292
        opn = "maccu";
2293
        break;
2294
    case OPC_VR54XX_MSAC:
2295
        tcg_gen_helper_1_2(do_msac, t0, t0, t1);
2296
        opn = "msac";
2297
        break;
2298
    case OPC_VR54XX_MSACU:
2299
        tcg_gen_helper_1_2(do_msacu, t0, t0, t1);
2300
        opn = "msacu";
2301
        break;
2302
    case OPC_VR54XX_MULHI:
2303
        tcg_gen_helper_1_2(do_mulhi, t0, t0, t1);
2304
        opn = "mulhi";
2305
        break;
2306
    case OPC_VR54XX_MULHIU:
2307
        tcg_gen_helper_1_2(do_mulhiu, t0, t0, t1);
2308
        opn = "mulhiu";
2309
        break;
2310
    case OPC_VR54XX_MULSHI:
2311
        tcg_gen_helper_1_2(do_mulshi, t0, t0, t1);
2312
        opn = "mulshi";
2313
        break;
2314
    case OPC_VR54XX_MULSHIU:
2315
        tcg_gen_helper_1_2(do_mulshiu, t0, t0, t1);
2316
        opn = "mulshiu";
2317
        break;
2318
    case OPC_VR54XX_MACCHI:
2319
        tcg_gen_helper_1_2(do_macchi, t0, t0, t1);
2320
        opn = "macchi";
2321
        break;
2322
    case OPC_VR54XX_MACCHIU:
2323
        tcg_gen_helper_1_2(do_macchiu, t0, t0, t1);
2324
        opn = "macchiu";
2325
        break;
2326
    case OPC_VR54XX_MSACHI:
2327
        tcg_gen_helper_1_2(do_msachi, t0, t0, t1);
2328
        opn = "msachi";
2329
        break;
2330
    case OPC_VR54XX_MSACHIU:
2331
        tcg_gen_helper_1_2(do_msachiu, t0, t0, t1);
2332
        opn = "msachiu";
2333
        break;
2334
    default:
2335
        MIPS_INVAL("mul vr54xx");
2336
        generate_exception(ctx, EXCP_RI);
2337
        goto out;
2338
    }
2339
    gen_store_gpr(t0, rd);
2340
    MIPS_DEBUG("%s %s, %s, %s", opn, regnames[rd], regnames[rs], regnames[rt]);
2341

    
2342
 out:
2343
    tcg_temp_free(t0);
2344
    tcg_temp_free(t1);
2345
}
2346

    
2347
static void gen_cl (DisasContext *ctx, uint32_t opc,
2348
                    int rd, int rs)
2349
{
2350
    const char *opn = "CLx";
2351
    TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
2352

    
2353
    if (rd == 0) {
2354
        /* Treat as NOP. */
2355
        MIPS_DEBUG("NOP");
2356
        goto out;
2357
    }
2358
    gen_load_gpr(t0, rs);
2359
    switch (opc) {
2360
    case OPC_CLO:
2361
        tcg_gen_helper_1_1(do_clo, t0, t0);
2362
        opn = "clo";
2363
        break;
2364
    case OPC_CLZ:
2365
        tcg_gen_helper_1_1(do_clz, t0, t0);
2366
        opn = "clz";
2367
        break;
2368
#if defined(TARGET_MIPS64)
2369
    case OPC_DCLO:
2370
        tcg_gen_helper_1_1(do_dclo, t0, t0);
2371
        opn = "dclo";
2372
        break;
2373
    case OPC_DCLZ:
2374
        tcg_gen_helper_1_1(do_dclz, t0, t0);
2375
        opn = "dclz";
2376
        break;
2377
#endif
2378
    default:
2379
        MIPS_INVAL(opn);
2380
        generate_exception(ctx, EXCP_RI);
2381
        goto out;
2382
    }
2383
    gen_store_gpr(t0, rd);
2384
    MIPS_DEBUG("%s %s, %s", opn, regnames[rd], regnames[rs]);
2385

    
2386
 out:
2387
    tcg_temp_free(t0);
2388
}
2389

    
2390
/* Traps */
2391
static void gen_trap (DisasContext *ctx, uint32_t opc,
2392
                      int rs, int rt, int16_t imm)
2393
{
2394
    int cond;
2395
    TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
2396
    TCGv t1 = tcg_temp_local_new(TCG_TYPE_TL);
2397

    
2398
    cond = 0;
2399
    /* Load needed operands */
2400
    switch (opc) {
2401
    case OPC_TEQ:
2402
    case OPC_TGE:
2403
    case OPC_TGEU:
2404
    case OPC_TLT:
2405
    case OPC_TLTU:
2406
    case OPC_TNE:
2407
        /* Compare two registers */
2408
        if (rs != rt) {
2409
            gen_load_gpr(t0, rs);
2410
            gen_load_gpr(t1, rt);
2411
            cond = 1;
2412
        }
2413
        break;
2414
    case OPC_TEQI:
2415
    case OPC_TGEI:
2416
    case OPC_TGEIU:
2417
    case OPC_TLTI:
2418
    case OPC_TLTIU:
2419
    case OPC_TNEI:
2420
        /* Compare register to immediate */
2421
        if (rs != 0 || imm != 0) {
2422
            gen_load_gpr(t0, rs);
2423
            tcg_gen_movi_tl(t1, (int32_t)imm);
2424
            cond = 1;
2425
        }
2426
        break;
2427
    }
2428
    if (cond == 0) {
2429
        switch (opc) {
2430
        case OPC_TEQ:   /* rs == rs */
2431
        case OPC_TEQI:  /* r0 == 0  */
2432
        case OPC_TGE:   /* rs >= rs */
2433
        case OPC_TGEI:  /* r0 >= 0  */
2434
        case OPC_TGEU:  /* rs >= rs unsigned */
2435
        case OPC_TGEIU: /* r0 >= 0  unsigned */
2436
            /* Always trap */
2437
            tcg_gen_movi_tl(t0, 1);
2438
            break;
2439
        case OPC_TLT:   /* rs < rs           */
2440
        case OPC_TLTI:  /* r0 < 0            */
2441
        case OPC_TLTU:  /* rs < rs unsigned  */
2442
        case OPC_TLTIU: /* r0 < 0  unsigned  */
2443
        case OPC_TNE:   /* rs != rs          */
2444
        case OPC_TNEI:  /* r0 != 0           */
2445
            /* Never trap: treat as NOP. */
2446
            goto out;
2447
        default:
2448
            MIPS_INVAL("trap");
2449
            generate_exception(ctx, EXCP_RI);
2450
            goto out;
2451
        }
2452
    } else {
2453
        switch (opc) {
2454
        case OPC_TEQ:
2455
        case OPC_TEQI:
2456
            gen_op_eq(t0, t1);
2457
            break;
2458
        case OPC_TGE:
2459
        case OPC_TGEI:
2460
            gen_op_ge(t0, t1);
2461
            break;
2462
        case OPC_TGEU:
2463
        case OPC_TGEIU:
2464
            gen_op_geu(t0, t1);
2465
            break;
2466
        case OPC_TLT:
2467
        case OPC_TLTI:
2468
            gen_op_lt(t0, t1);
2469
            break;
2470
        case OPC_TLTU:
2471
        case OPC_TLTIU:
2472
            gen_op_ltu(t0, t1);
2473
            break;
2474
        case OPC_TNE:
2475
        case OPC_TNEI:
2476
            gen_op_ne(t0, t1);
2477
            break;
2478
        default:
2479
            MIPS_INVAL("trap");
2480
            generate_exception(ctx, EXCP_RI);
2481
            goto out;
2482
        }
2483
    }
2484
    save_cpu_state(ctx, 1);
2485
    {
2486
        int l1 = gen_new_label();
2487

    
2488
        tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2489
        tcg_gen_helper_0_i(do_raise_exception, EXCP_TRAP);
2490
        gen_set_label(l1);
2491
    }
2492
    ctx->bstate = BS_STOP;
2493
 out:
2494
    tcg_temp_free(t0);
2495
    tcg_temp_free(t1);
2496
}
2497

    
2498
static always_inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
2499
{
2500
    TranslationBlock *tb;
2501
    tb = ctx->tb;
2502
    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK)) {
2503
        tcg_gen_goto_tb(n);
2504
        gen_save_pc(dest);
2505
        tcg_gen_exit_tb((long)tb + n);
2506
    } else {
2507
        gen_save_pc(dest);
2508
        tcg_gen_exit_tb(0);
2509
    }
2510
}
2511

    
2512
/* Branches (before delay slot) */
2513
static void gen_compute_branch (DisasContext *ctx, uint32_t opc,
2514
                                int rs, int rt, int32_t offset)
2515
{
2516
    target_ulong btarget = -1;
2517
    int blink = 0;
2518
    int bcond = 0;
2519
    TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
2520
    TCGv t1 = tcg_temp_local_new(TCG_TYPE_TL);
2521

    
2522
    if (ctx->hflags & MIPS_HFLAG_BMASK) {
2523
#ifdef MIPS_DEBUG_DISAS
2524
        if (loglevel & CPU_LOG_TB_IN_ASM) {
2525
            fprintf(logfile,
2526
                    "Branch in delay slot at PC 0x" TARGET_FMT_lx "\n",
2527
                    ctx->pc);
2528
        }
2529
#endif
2530
        generate_exception(ctx, EXCP_RI);
2531
        goto out;
2532
    }
2533

    
2534
    /* Load needed operands */
2535
    switch (opc) {
2536
    case OPC_BEQ:
2537
    case OPC_BEQL:
2538
    case OPC_BNE:
2539
    case OPC_BNEL:
2540
        /* Compare two registers */
2541
        if (rs != rt) {
2542
            gen_load_gpr(t0, rs);
2543
            gen_load_gpr(t1, rt);
2544
            bcond = 1;
2545
        }
2546
        btarget = ctx->pc + 4 + offset;
2547
        break;
2548
    case OPC_BGEZ:
2549
    case OPC_BGEZAL:
2550
    case OPC_BGEZALL:
2551
    case OPC_BGEZL:
2552
    case OPC_BGTZ:
2553
    case OPC_BGTZL:
2554
    case OPC_BLEZ:
2555
    case OPC_BLEZL:
2556
    case OPC_BLTZ:
2557
    case OPC_BLTZAL:
2558
    case OPC_BLTZALL:
2559
    case OPC_BLTZL:
2560
        /* Compare to zero */
2561
        if (rs != 0) {
2562
            gen_load_gpr(t0, rs);
2563
            bcond = 1;
2564
        }
2565
        btarget = ctx->pc + 4 + offset;
2566
        break;
2567
    case OPC_J:
2568
    case OPC_JAL:
2569
        /* Jump to immediate */
2570
        btarget = ((ctx->pc + 4) & (int32_t)0xF0000000) | (uint32_t)offset;
2571
        break;
2572
    case OPC_JR:
2573
    case OPC_JALR:
2574
        /* Jump to register */
2575
        if (offset != 0 && offset != 16) {
2576
            /* Hint = 0 is JR/JALR, hint 16 is JR.HB/JALR.HB, the
2577
               others are reserved. */
2578
            MIPS_INVAL("jump hint");
2579
            generate_exception(ctx, EXCP_RI);
2580
            goto out;
2581
        }
2582
        gen_save_breg_target(rs);
2583
        break;
2584
    default:
2585
        MIPS_INVAL("branch/jump");
2586
        generate_exception(ctx, EXCP_RI);
2587
        goto out;
2588
    }
2589
    if (bcond == 0) {
2590
        /* No condition to be computed */
2591
        switch (opc) {
2592
        case OPC_BEQ:     /* rx == rx        */
2593
        case OPC_BEQL:    /* rx == rx likely */
2594
        case OPC_BGEZ:    /* 0 >= 0          */
2595
        case OPC_BGEZL:   /* 0 >= 0 likely   */
2596
        case OPC_BLEZ:    /* 0 <= 0          */
2597
        case OPC_BLEZL:   /* 0 <= 0 likely   */
2598
            /* Always take */
2599
            ctx->hflags |= MIPS_HFLAG_B;
2600
            MIPS_DEBUG("balways");
2601
            break;
2602
        case OPC_BGEZAL:  /* 0 >= 0          */
2603
        case OPC_BGEZALL: /* 0 >= 0 likely   */
2604
            /* Always take and link */
2605
            blink = 31;
2606
            ctx->hflags |= MIPS_HFLAG_B;
2607
            MIPS_DEBUG("balways and link");
2608
            break;
2609
        case OPC_BNE:     /* rx != rx        */
2610
        case OPC_BGTZ:    /* 0 > 0           */
2611
        case OPC_BLTZ:    /* 0 < 0           */
2612
            /* Treat as NOP. */
2613
            MIPS_DEBUG("bnever (NOP)");
2614
            goto out;
2615
        case OPC_BLTZAL:  /* 0 < 0           */
2616
            tcg_gen_movi_tl(t0, ctx->pc + 8);
2617
            gen_store_gpr(t0, 31);
2618
            MIPS_DEBUG("bnever and link");
2619
            goto out;
2620
        case OPC_BLTZALL: /* 0 < 0 likely */
2621
            tcg_gen_movi_tl(t0, ctx->pc + 8);
2622
            gen_store_gpr(t0, 31);
2623
            /* Skip the instruction in the delay slot */
2624
            MIPS_DEBUG("bnever, link and skip");
2625
            ctx->pc += 4;
2626
            goto out;
2627
        case OPC_BNEL:    /* rx != rx likely */
2628
        case OPC_BGTZL:   /* 0 > 0 likely */
2629
        case OPC_BLTZL:   /* 0 < 0 likely */
2630
            /* Skip the instruction in the delay slot */
2631
            MIPS_DEBUG("bnever and skip");
2632
            ctx->pc += 4;
2633
            goto out;
2634
        case OPC_J:
2635
            ctx->hflags |= MIPS_HFLAG_B;
2636
            MIPS_DEBUG("j " TARGET_FMT_lx, btarget);
2637
            break;
2638
        case OPC_JAL:
2639
            blink = 31;
2640
            ctx->hflags |= MIPS_HFLAG_B;
2641
            MIPS_DEBUG("jal " TARGET_FMT_lx, btarget);
2642
            break;
2643
        case OPC_JR:
2644
            ctx->hflags |= MIPS_HFLAG_BR;
2645
            MIPS_DEBUG("jr %s", regnames[rs]);
2646
            break;
2647
        case OPC_JALR:
2648
            blink = rt;
2649
            ctx->hflags |= MIPS_HFLAG_BR;
2650
            MIPS_DEBUG("jalr %s, %s", regnames[rt], regnames[rs]);
2651
            break;
2652
        default:
2653
            MIPS_INVAL("branch/jump");
2654
            generate_exception(ctx, EXCP_RI);
2655
            goto out;
2656
        }
2657
    } else {
2658
        switch (opc) {
2659
        case OPC_BEQ:
2660
            gen_op_eq(t0, t1);
2661
            MIPS_DEBUG("beq %s, %s, " TARGET_FMT_lx,
2662
                       regnames[rs], regnames[rt], btarget);
2663
            goto not_likely;
2664
        case OPC_BEQL:
2665
            gen_op_eq(t0, t1);
2666
            MIPS_DEBUG("beql %s, %s, " TARGET_FMT_lx,
2667
                       regnames[rs], regnames[rt], btarget);
2668
            goto likely;
2669
        case OPC_BNE:
2670
            gen_op_ne(t0, t1);
2671
            MIPS_DEBUG("bne %s, %s, " TARGET_FMT_lx,
2672
                       regnames[rs], regnames[rt], btarget);
2673
            goto not_likely;
2674
        case OPC_BNEL:
2675
            gen_op_ne(t0, t1);
2676
            MIPS_DEBUG("bnel %s, %s, " TARGET_FMT_lx,
2677
                       regnames[rs], regnames[rt], btarget);
2678
            goto likely;
2679
        case OPC_BGEZ:
2680
            gen_op_gez(t0);
2681
            MIPS_DEBUG("bgez %s, " TARGET_FMT_lx, regnames[rs], btarget);
2682
            goto not_likely;
2683
        case OPC_BGEZL:
2684
            gen_op_gez(t0);
2685
            MIPS_DEBUG("bgezl %s, " TARGET_FMT_lx, regnames[rs], btarget);
2686
            goto likely;
2687
        case OPC_BGEZAL:
2688
            gen_op_gez(t0);
2689
            MIPS_DEBUG("bgezal %s, " TARGET_FMT_lx, regnames[rs], btarget);
2690
            blink = 31;
2691
            goto not_likely;
2692
        case OPC_BGEZALL:
2693
            gen_op_gez(t0);
2694
            blink = 31;
2695
            MIPS_DEBUG("bgezall %s, " TARGET_FMT_lx, regnames[rs], btarget);
2696
            goto likely;
2697
        case OPC_BGTZ:
2698
            gen_op_gtz(t0);
2699
            MIPS_DEBUG("bgtz %s, " TARGET_FMT_lx, regnames[rs], btarget);
2700
            goto not_likely;
2701
        case OPC_BGTZL:
2702
            gen_op_gtz(t0);
2703
            MIPS_DEBUG("bgtzl %s, " TARGET_FMT_lx, regnames[rs], btarget);
2704
            goto likely;
2705
        case OPC_BLEZ:
2706
            gen_op_lez(t0);
2707
            MIPS_DEBUG("blez %s, " TARGET_FMT_lx, regnames[rs], btarget);
2708
            goto not_likely;
2709
        case OPC_BLEZL:
2710
            gen_op_lez(t0);
2711
            MIPS_DEBUG("blezl %s, " TARGET_FMT_lx, regnames[rs], btarget);
2712
            goto likely;
2713
        case OPC_BLTZ:
2714
            gen_op_ltz(t0);
2715
            MIPS_DEBUG("bltz %s, " TARGET_FMT_lx, regnames[rs], btarget);
2716
            goto not_likely;
2717
        case OPC_BLTZL:
2718
            gen_op_ltz(t0);
2719
            MIPS_DEBUG("bltzl %s, " TARGET_FMT_lx, regnames[rs], btarget);
2720
            goto likely;
2721
        case OPC_BLTZAL:
2722
            gen_op_ltz(t0);
2723
            blink = 31;
2724
            MIPS_DEBUG("bltzal %s, " TARGET_FMT_lx, regnames[rs], btarget);
2725
        not_likely:
2726
            ctx->hflags |= MIPS_HFLAG_BC;
2727
            tcg_gen_st_tl(t0, cpu_env, offsetof(CPUState, bcond));
2728
            break;
2729
        case OPC_BLTZALL:
2730
            gen_op_ltz(t0);
2731
            blink = 31;
2732
            MIPS_DEBUG("bltzall %s, " TARGET_FMT_lx, regnames[rs], btarget);
2733
        likely:
2734
            ctx->hflags |= MIPS_HFLAG_BL;
2735
            tcg_gen_st_tl(t0, cpu_env, offsetof(CPUState, bcond));
2736
            break;
2737
        default:
2738
            MIPS_INVAL("conditional branch/jump");
2739
            generate_exception(ctx, EXCP_RI);
2740
            goto out;
2741
        }
2742
    }
2743
    MIPS_DEBUG("enter ds: link %d cond %02x target " TARGET_FMT_lx,
2744
               blink, ctx->hflags, btarget);
2745

    
2746
    ctx->btarget = btarget;
2747
    if (blink > 0) {
2748
        tcg_gen_movi_tl(t0, ctx->pc + 8);
2749
        gen_store_gpr(t0, blink);
2750
    }
2751

    
2752
 out:
2753
    tcg_temp_free(t0);
2754
    tcg_temp_free(t1);
2755
}
2756

    
2757
/* special3 bitfield operations */
2758
static void gen_bitops (DisasContext *ctx, uint32_t opc, int rt,
2759
                       int rs, int lsb, int msb)
2760
{
2761
    TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
2762
    TCGv t1 = tcg_temp_local_new(TCG_TYPE_TL);
2763

    
2764
    gen_load_gpr(t1, rs);
2765
    switch (opc) {
2766
    case OPC_EXT:
2767
        if (lsb + msb > 31)
2768
            goto fail;
2769
        tcg_gen_helper_1_2ii(do_ext, t0, t0, t1, lsb, msb + 1);
2770
        break;
2771
#if defined(TARGET_MIPS64)
2772
    case OPC_DEXTM:
2773
        if (lsb + msb > 63)
2774
            goto fail;
2775
        tcg_gen_helper_1_2ii(do_dext, t0, t0, t1, lsb, msb + 1 + 32);
2776
        break;
2777
    case OPC_DEXTU:
2778
        if (lsb + msb > 63)
2779
            goto fail;
2780
        tcg_gen_helper_1_2ii(do_dext, t0, t0, t1, lsb + 32, msb + 1);
2781
        break;
2782
    case OPC_DEXT:
2783
        if (lsb + msb > 63)
2784
            goto fail;
2785
        tcg_gen_helper_1_2ii(do_dext, t0, t0, t1, lsb, msb + 1);
2786
        break;
2787
#endif
2788
    case OPC_INS:
2789
        if (lsb > msb)
2790
            goto fail;
2791
        gen_load_gpr(t0, rt);
2792
        tcg_gen_helper_1_2ii(do_ins, t0, t0, t1, lsb, msb - lsb + 1);
2793
        break;
2794
#if defined(TARGET_MIPS64)
2795
    case OPC_DINSM:
2796
        if (lsb > msb)
2797
            goto fail;
2798
        gen_load_gpr(t0, rt);
2799
        tcg_gen_helper_1_2ii(do_dins, t0, t0, t1, lsb, msb - lsb + 1 + 32);
2800
        break;
2801
    case OPC_DINSU:
2802
        if (lsb > msb)
2803
            goto fail;
2804
        gen_load_gpr(t0, rt);
2805
        tcg_gen_helper_1_2ii(do_dins, t0, t0, t1, lsb + 32, msb - lsb + 1);
2806
        break;
2807
    case OPC_DINS:
2808
        if (lsb > msb)
2809
            goto fail;
2810
        gen_load_gpr(t0, rt);
2811
        tcg_gen_helper_1_2ii(do_dins, t0, t0, t1, lsb, msb - lsb + 1);
2812
        break;
2813
#endif
2814
    default:
2815
fail:
2816
        MIPS_INVAL("bitops");
2817
        generate_exception(ctx, EXCP_RI);
2818
        tcg_temp_free(t0);
2819
        tcg_temp_free(t1);
2820
        return;
2821
    }
2822
    gen_store_gpr(t0, rt);
2823
    tcg_temp_free(t0);
2824
    tcg_temp_free(t1);
2825
}
2826

    
2827
/* CP0 (MMU and control) */
2828
#ifndef CONFIG_USER_ONLY
2829
static inline void gen_mfc0_load32 (TCGv t, target_ulong off)
2830
{
2831
    TCGv r_tmp = tcg_temp_new(TCG_TYPE_I32);
2832

    
2833
    tcg_gen_ld_i32(r_tmp, cpu_env, off);
2834
    tcg_gen_ext_i32_tl(t, r_tmp);
2835
    tcg_temp_free(r_tmp);
2836
}
2837

    
2838
static inline void gen_mfc0_load64 (TCGv t, target_ulong off)
2839
{
2840
    tcg_gen_ld_tl(t, cpu_env, off);
2841
    tcg_gen_ext32s_tl(t, t);
2842
}
2843

    
2844
static inline void gen_mtc0_store32 (TCGv t, target_ulong off)
2845
{
2846
    TCGv r_tmp = tcg_temp_new(TCG_TYPE_I32);
2847

    
2848
    tcg_gen_trunc_tl_i32(r_tmp, t);
2849
    tcg_gen_st_i32(r_tmp, cpu_env, off);
2850
    tcg_temp_free(r_tmp);
2851
}
2852

    
2853
static inline void gen_mtc0_store64 (TCGv t, target_ulong off)
2854
{
2855
    tcg_gen_ext32s_tl(t, t);
2856
    tcg_gen_st_tl(t, cpu_env, off);
2857
}
2858

    
2859
static void gen_mfc0 (CPUState *env, DisasContext *ctx, int reg, int sel)
2860
{
2861
    const char *rn = "invalid";
2862

    
2863
    if (sel != 0)
2864
        check_insn(env, ctx, ISA_MIPS32);
2865

    
2866
    switch (reg) {
2867
    case 0:
2868
        switch (sel) {
2869
        case 0:
2870
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Index));
2871
            rn = "Index";
2872
            break;
2873
        case 1:
2874
            check_insn(env, ctx, ASE_MT);
2875
            tcg_gen_helper_1_1(do_mfc0_mvpcontrol, cpu_T[0], cpu_T[0]);
2876
            rn = "MVPControl";
2877
            break;
2878
        case 2:
2879
            check_insn(env, ctx, ASE_MT);
2880
            tcg_gen_helper_1_1(do_mfc0_mvpconf0, cpu_T[0], cpu_T[0]);
2881
            rn = "MVPConf0";
2882
            break;
2883
        case 3:
2884
            check_insn(env, ctx, ASE_MT);
2885
            tcg_gen_helper_1_1(do_mfc0_mvpconf1, cpu_T[0], cpu_T[0]);
2886
            rn = "MVPConf1";
2887
            break;
2888
        default:
2889
            goto die;
2890
        }
2891
        break;
2892
    case 1:
2893
        switch (sel) {
2894
        case 0:
2895
            tcg_gen_helper_1_1(do_mfc0_random, cpu_T[0], cpu_T[0]);
2896
            rn = "Random";
2897
            break;
2898
        case 1:
2899
            check_insn(env, ctx, ASE_MT);
2900
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_VPEControl));
2901
            rn = "VPEControl";
2902
            break;
2903
        case 2:
2904
            check_insn(env, ctx, ASE_MT);
2905
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_VPEConf0));
2906
            rn = "VPEConf0";
2907
            break;
2908
        case 3:
2909
            check_insn(env, ctx, ASE_MT);
2910
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_VPEConf1));
2911
            rn = "VPEConf1";
2912
            break;
2913
        case 4:
2914
            check_insn(env, ctx, ASE_MT);
2915
            gen_mfc0_load64(cpu_T[0], offsetof(CPUState, CP0_YQMask));
2916
            rn = "YQMask";
2917
            break;
2918
        case 5:
2919
            check_insn(env, ctx, ASE_MT);
2920
            gen_mfc0_load64(cpu_T[0], offsetof(CPUState, CP0_VPESchedule));
2921
            rn = "VPESchedule";
2922
            break;
2923
        case 6:
2924
            check_insn(env, ctx, ASE_MT);
2925
            gen_mfc0_load64(cpu_T[0], offsetof(CPUState, CP0_VPEScheFBack));
2926
            rn = "VPEScheFBack";
2927
            break;
2928
        case 7:
2929
            check_insn(env, ctx, ASE_MT);
2930
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_VPEOpt));
2931
            rn = "VPEOpt";
2932
            break;
2933
        default:
2934
            goto die;
2935
        }
2936
        break;
2937
    case 2:
2938
        switch (sel) {
2939
        case 0:
2940
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_EntryLo0));
2941
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
2942
            rn = "EntryLo0";
2943
            break;
2944
        case 1:
2945
            check_insn(env, ctx, ASE_MT);
2946
            tcg_gen_helper_1_1(do_mfc0_tcstatus, cpu_T[0], cpu_T[0]);
2947
            rn = "TCStatus";
2948
            break;
2949
        case 2:
2950
            check_insn(env, ctx, ASE_MT);
2951
            tcg_gen_helper_1_1(do_mfc0_tcbind, cpu_T[0], cpu_T[0]);
2952
            rn = "TCBind";
2953
            break;
2954
        case 3:
2955
            check_insn(env, ctx, ASE_MT);
2956
            tcg_gen_helper_1_1(do_mfc0_tcrestart, cpu_T[0], cpu_T[0]);
2957
            rn = "TCRestart";
2958
            break;
2959
        case 4:
2960
            check_insn(env, ctx, ASE_MT);
2961
            tcg_gen_helper_1_1(do_mfc0_tchalt, cpu_T[0], cpu_T[0]);
2962
            rn = "TCHalt";
2963
            break;
2964
        case 5:
2965
            check_insn(env, ctx, ASE_MT);
2966
            tcg_gen_helper_1_1(do_mfc0_tccontext, cpu_T[0], cpu_T[0]);
2967
            rn = "TCContext";
2968
            break;
2969
        case 6:
2970
            check_insn(env, ctx, ASE_MT);
2971
            tcg_gen_helper_1_1(do_mfc0_tcschedule, cpu_T[0], cpu_T[0]);
2972
            rn = "TCSchedule";
2973
            break;
2974
        case 7:
2975
            check_insn(env, ctx, ASE_MT);
2976
            tcg_gen_helper_1_1(do_mfc0_tcschefback, cpu_T[0], cpu_T[0]);
2977
            rn = "TCScheFBack";
2978
            break;
2979
        default:
2980
            goto die;
2981
        }
2982
        break;
2983
    case 3:
2984
        switch (sel) {
2985
        case 0:
2986
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_EntryLo1));
2987
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
2988
            rn = "EntryLo1";
2989
            break;
2990
        default:
2991
            goto die;
2992
        }
2993
        break;
2994
    case 4:
2995
        switch (sel) {
2996
        case 0:
2997
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_Context));
2998
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
2999
            rn = "Context";
3000
            break;
3001
        case 1:
3002
//            tcg_gen_helper_1_1(do_mfc0_contextconfig, cpu_T[0], cpu_T[0]); /* SmartMIPS ASE */
3003
            rn = "ContextConfig";
3004
//            break;
3005
        default:
3006
            goto die;
3007
        }
3008
        break;
3009
    case 5:
3010
        switch (sel) {
3011
        case 0:
3012
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_PageMask));
3013
            rn = "PageMask";
3014
            break;
3015
        case 1:
3016
            check_insn(env, ctx, ISA_MIPS32R2);
3017
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_PageGrain));
3018
            rn = "PageGrain";
3019
            break;
3020
        default:
3021
            goto die;
3022
        }
3023
        break;
3024
    case 6:
3025
        switch (sel) {
3026
        case 0:
3027
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Wired));
3028
            rn = "Wired";
3029
            break;
3030
        case 1:
3031
            check_insn(env, ctx, ISA_MIPS32R2);
3032
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSConf0));
3033
            rn = "SRSConf0";
3034
            break;
3035
        case 2:
3036
            check_insn(env, ctx, ISA_MIPS32R2);
3037
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSConf1));
3038
            rn = "SRSConf1";
3039
            break;
3040
        case 3:
3041
            check_insn(env, ctx, ISA_MIPS32R2);
3042
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSConf2));
3043
            rn = "SRSConf2";
3044
            break;
3045
        case 4:
3046
            check_insn(env, ctx, ISA_MIPS32R2);
3047
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSConf3));
3048
            rn = "SRSConf3";
3049
            break;
3050
        case 5:
3051
            check_insn(env, ctx, ISA_MIPS32R2);
3052
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSConf4));
3053
            rn = "SRSConf4";
3054
            break;
3055
        default:
3056
            goto die;
3057
        }
3058
        break;
3059
    case 7:
3060
        switch (sel) {
3061
        case 0:
3062
            check_insn(env, ctx, ISA_MIPS32R2);
3063
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_HWREna));
3064
            rn = "HWREna";
3065
            break;
3066
        default:
3067
            goto die;
3068
        }
3069
        break;
3070
    case 8:
3071
        switch (sel) {
3072
        case 0:
3073
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_BadVAddr));
3074
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
3075
            rn = "BadVAddr";
3076
            break;
3077
        default:
3078
            goto die;
3079
       }
3080
        break;
3081
    case 9:
3082
        switch (sel) {
3083
        case 0:
3084
            tcg_gen_helper_1_1(do_mfc0_count, cpu_T[0], cpu_T[0]);
3085
            rn = "Count";
3086
            break;
3087
        /* 6,7 are implementation dependent */
3088
        default:
3089
            goto die;
3090
        }
3091
        break;
3092
    case 10:
3093
        switch (sel) {
3094
        case 0:
3095
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_EntryHi));
3096
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
3097
            rn = "EntryHi";
3098
            break;
3099
        default:
3100
            goto die;
3101
        }
3102
        break;
3103
    case 11:
3104
        switch (sel) {
3105
        case 0:
3106
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Compare));
3107
            rn = "Compare";
3108
            break;
3109
        /* 6,7 are implementation dependent */
3110
        default:
3111
            goto die;
3112
        }
3113
        break;
3114
    case 12:
3115
        switch (sel) {
3116
        case 0:
3117
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Status));
3118
            rn = "Status";
3119
            break;
3120
        case 1:
3121
            check_insn(env, ctx, ISA_MIPS32R2);
3122
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_IntCtl));
3123
            rn = "IntCtl";
3124
            break;
3125
        case 2:
3126
            check_insn(env, ctx, ISA_MIPS32R2);
3127
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSCtl));
3128
            rn = "SRSCtl";
3129
            break;
3130
        case 3:
3131
            check_insn(env, ctx, ISA_MIPS32R2);
3132
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSMap));
3133
            rn = "SRSMap";
3134
            break;
3135
        default:
3136
            goto die;
3137
       }
3138
        break;
3139
    case 13:
3140
        switch (sel) {
3141
        case 0:
3142
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Cause));
3143
            rn = "Cause";
3144
            break;
3145
        default:
3146
            goto die;
3147
       }
3148
        break;
3149
    case 14:
3150
        switch (sel) {
3151
        case 0:
3152
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_EPC));
3153
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
3154
            rn = "EPC";
3155
            break;
3156
        default:
3157
            goto die;
3158
        }
3159
        break;
3160
    case 15:
3161
        switch (sel) {
3162
        case 0:
3163
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_PRid));
3164
            rn = "PRid";
3165
            break;
3166
        case 1:
3167
            check_insn(env, ctx, ISA_MIPS32R2);
3168
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_EBase));
3169
            rn = "EBase";
3170
            break;
3171
        default:
3172
            goto die;
3173
       }
3174
        break;
3175
    case 16:
3176
        switch (sel) {
3177
        case 0:
3178
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config0));
3179
            rn = "Config";
3180
            break;
3181
        case 1:
3182
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config1));
3183
            rn = "Config1";
3184
            break;
3185
        case 2:
3186
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config2));
3187
            rn = "Config2";
3188
            break;
3189
        case 3:
3190
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config3));
3191
            rn = "Config3";
3192
            break;
3193
        /* 4,5 are reserved */
3194
        /* 6,7 are implementation dependent */
3195
        case 6:
3196
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config6));
3197
            rn = "Config6";
3198
            break;
3199
        case 7:
3200
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config7));
3201
            rn = "Config7";
3202
            break;
3203
        default:
3204
            goto die;
3205
        }
3206
        break;
3207
    case 17:
3208
        switch (sel) {
3209
        case 0:
3210
            tcg_gen_helper_1_1(do_mfc0_lladdr, cpu_T[0], cpu_T[0]);
3211
            rn = "LLAddr";
3212
            break;
3213
        default:
3214
            goto die;
3215
        }
3216
        break;
3217
    case 18:
3218
        switch (sel) {
3219
        case 0 ... 7:
3220
            tcg_gen_helper_1_1i(do_mfc0_watchlo, cpu_T[0], cpu_T[0], sel);
3221
            rn = "WatchLo";
3222
            break;
3223
        default:
3224
            goto die;
3225
        }
3226
        break;
3227
    case 19:
3228
        switch (sel) {
3229
        case 0 ...7:
3230
            tcg_gen_helper_1_1i(do_mfc0_watchhi, cpu_T[0], cpu_T[0], sel);
3231
            rn = "WatchHi";
3232
            break;
3233
        default:
3234
            goto die;
3235
        }
3236
        break;
3237
    case 20:
3238
        switch (sel) {
3239
        case 0:
3240
#if defined(TARGET_MIPS64)
3241
            check_insn(env, ctx, ISA_MIPS3);
3242
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_XContext));
3243
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
3244
            rn = "XContext";
3245
            break;
3246
#endif
3247
        default:
3248
            goto die;
3249
        }
3250
        break;
3251
    case 21:
3252
       /* Officially reserved, but sel 0 is used for R1x000 framemask */
3253
        switch (sel) {
3254
        case 0:
3255
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Framemask));
3256
            rn = "Framemask";
3257
            break;
3258
        default:
3259
            goto die;
3260
        }
3261
        break;
3262
    case 22:
3263
        /* ignored */
3264
        rn = "'Diagnostic"; /* implementation dependent */
3265
        break;
3266
    case 23:
3267
        switch (sel) {
3268
        case 0:
3269
            tcg_gen_helper_1_1(do_mfc0_debug, cpu_T[0], cpu_T[0]); /* EJTAG support */
3270
            rn = "Debug";
3271
            break;
3272
        case 1:
3273
//            tcg_gen_helper_1_1(do_mfc0_tracecontrol, cpu_T[0], cpu_T[0]); /* PDtrace support */
3274
            rn = "TraceControl";
3275
//            break;
3276
        case 2:
3277
//            tcg_gen_helper_1_1(do_mfc0_tracecontrol2, cpu_T[0], cpu_T[0]); /* PDtrace support */
3278
            rn = "TraceControl2";
3279
//            break;
3280
        case 3:
3281
//            tcg_gen_helper_1_1(do_mfc0_usertracedata, cpu_T[0], cpu_T[0]); /* PDtrace support */
3282
            rn = "UserTraceData";
3283
//            break;
3284
        case 4:
3285
//            tcg_gen_helper_1_1(do_mfc0_tracebpc, cpu_T[0], cpu_T[0]); /* PDtrace support */
3286
            rn = "TraceBPC";
3287
//            break;
3288
        default:
3289
            goto die;
3290
        }
3291
        break;
3292
    case 24:
3293
        switch (sel) {
3294
        case 0:
3295
            /* EJTAG support */
3296
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_DEPC));
3297
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
3298
            rn = "DEPC";
3299
            break;
3300
        default:
3301
            goto die;
3302
        }
3303
        break;
3304
    case 25:
3305
        switch (sel) {
3306
        case 0:
3307
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Performance0));
3308
            rn = "Performance0";
3309
            break;
3310
        case 1:
3311
//            tcg_gen_helper_1_1(do_mfc0_performance1, cpu_T[0], cpu_T[0]);
3312
            rn = "Performance1";
3313
//            break;
3314
        case 2:
3315
//            tcg_gen_helper_1_1(do_mfc0_performance2, cpu_T[0], cpu_T[0]);
3316
            rn = "Performance2";
3317
//            break;
3318
        case 3:
3319
//            tcg_gen_helper_1_1(do_mfc0_performance3, cpu_T[0], cpu_T[0]);
3320
            rn = "Performance3";
3321
//            break;
3322
        case 4:
3323
//            tcg_gen_helper_1_1(do_mfc0_performance4, cpu_T[0], cpu_T[0]);
3324
            rn = "Performance4";
3325
//            break;
3326
        case 5:
3327
//            tcg_gen_helper_1_1(do_mfc0_performance5, cpu_T[0], cpu_T[0]);
3328
            rn = "Performance5";
3329
//            break;
3330
        case 6:
3331
//            tcg_gen_helper_1_1(do_mfc0_performance6, cpu_T[0], cpu_T[0]);
3332
            rn = "Performance6";
3333
//            break;
3334
        case 7:
3335
//            tcg_gen_helper_1_1(do_mfc0_performance7, cpu_T[0], cpu_T[0]);
3336
            rn = "Performance7";
3337
//            break;
3338
        default:
3339
            goto die;
3340
        }
3341
        break;
3342
    case 26:
3343
       rn = "ECC";
3344
       break;
3345
    case 27:
3346
        switch (sel) {
3347
        /* ignored */
3348
        case 0 ... 3:
3349
            rn = "CacheErr";
3350
            break;
3351
        default:
3352
            goto die;
3353
        }
3354
        break;
3355
    case 28:
3356
        switch (sel) {
3357
        case 0:
3358
        case 2:
3359
        case 4:
3360
        case 6:
3361
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_TagLo));
3362
            rn = "TagLo";
3363
            break;
3364
        case 1:
3365
        case 3:
3366
        case 5:
3367
        case 7:
3368
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_DataLo));
3369
            rn = "DataLo";
3370
            break;
3371
        default:
3372
            goto die;
3373
        }
3374
        break;
3375
    case 29:
3376
        switch (sel) {
3377
        case 0:
3378
        case 2:
3379
        case 4:
3380
        case 6:
3381
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_TagHi));
3382
            rn = "TagHi";
3383
            break;
3384
        case 1:
3385
        case 3:
3386
        case 5:
3387
        case 7:
3388
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_DataHi));
3389
            rn = "DataHi";
3390
            break;
3391
        default:
3392
            goto die;
3393
        }
3394
        break;
3395
    case 30:
3396
        switch (sel) {
3397
        case 0:
3398
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_ErrorEPC));
3399
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
3400
            rn = "ErrorEPC";
3401
            break;
3402
        default:
3403
            goto die;
3404
        }
3405
        break;
3406
    case 31:
3407
        switch (sel) {
3408
        case 0:
3409
            /* EJTAG support */
3410
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_DESAVE));
3411
            rn = "DESAVE";
3412
            break;
3413
        default:
3414
            goto die;
3415
        }
3416
        break;
3417
    default:
3418
       goto die;
3419
    }
3420
#if defined MIPS_DEBUG_DISAS
3421
    if (loglevel & CPU_LOG_TB_IN_ASM) {
3422
        fprintf(logfile, "mfc0 %s (reg %d sel %d)\n",
3423
                rn, reg, sel);
3424
    }
3425
#endif
3426
    return;
3427

    
3428
die:
3429
#if defined MIPS_DEBUG_DISAS
3430
    if (loglevel & CPU_LOG_TB_IN_ASM) {
3431
        fprintf(logfile, "mfc0 %s (reg %d sel %d)\n",
3432
                rn, reg, sel);
3433
    }
3434
#endif
3435
    generate_exception(ctx, EXCP_RI);
3436
}
3437

    
3438
static void gen_mtc0 (CPUState *env, DisasContext *ctx, int reg, int sel)
3439
{
3440
    const char *rn = "invalid";
3441

    
3442
    if (sel != 0)
3443
        check_insn(env, ctx, ISA_MIPS32);
3444

    
3445
    switch (reg) {
3446
    case 0:
3447
        switch (sel) {
3448
        case 0:
3449
            tcg_gen_helper_0_1(do_mtc0_index, cpu_T[0]);
3450
            rn = "Index";
3451
            break;
3452
        case 1:
3453
            check_insn(env, ctx, ASE_MT);
3454
            tcg_gen_helper_0_1(do_mtc0_mvpcontrol, cpu_T[0]);
3455
            rn = "MVPControl";
3456
            break;
3457
        case 2:
3458
            check_insn(env, ctx, ASE_MT);
3459
            /* ignored */
3460
            rn = "MVPConf0";
3461
            break;
3462
        case 3:
3463
            check_insn(env, ctx, ASE_MT);
3464
            /* ignored */
3465
            rn = "MVPConf1";
3466
            break;
3467
        default:
3468
            goto die;
3469
        }
3470
        break;
3471
    case 1:
3472
        switch (sel) {
3473
        case 0:
3474
            /* ignored */
3475
            rn = "Random";
3476
            break;
3477
        case 1:
3478
            check_insn(env, ctx, ASE_MT);
3479
            tcg_gen_helper_0_1(do_mtc0_vpecontrol, cpu_T[0]);
3480
            rn = "VPEControl";
3481
            break;
3482
        case 2:
3483
            check_insn(env, ctx, ASE_MT);
3484
            tcg_gen_helper_0_1(do_mtc0_vpeconf0, cpu_T[0]);
3485
            rn = "VPEConf0";
3486
            break;
3487
        case 3:
3488
            check_insn(env, ctx, ASE_MT);
3489
            tcg_gen_helper_0_1(do_mtc0_vpeconf1, cpu_T[0]);
3490
            rn = "VPEConf1";
3491
            break;
3492
        case 4:
3493
            check_insn(env, ctx, ASE_MT);
3494
            tcg_gen_helper_0_1(do_mtc0_yqmask, cpu_T[0]);
3495
            rn = "YQMask";
3496
            break;
3497
        case 5:
3498
            check_insn(env, ctx, ASE_MT);
3499
            gen_mtc0_store64(cpu_T[0], offsetof(CPUState, CP0_VPESchedule));
3500
            rn = "VPESchedule";
3501
            break;
3502
        case 6:
3503
            check_insn(env, ctx, ASE_MT);
3504
            gen_mtc0_store64(cpu_T[0], offsetof(CPUState, CP0_VPEScheFBack));
3505
            rn = "VPEScheFBack";
3506
            break;
3507
        case 7:
3508
            check_insn(env, ctx, ASE_MT);
3509
            tcg_gen_helper_0_1(do_mtc0_vpeopt, cpu_T[0]);
3510
            rn = "VPEOpt";
3511
            break;
3512
        default:
3513
            goto die;
3514
        }
3515
        break;
3516
    case 2:
3517
        switch (sel) {
3518
        case 0:
3519
            tcg_gen_helper_0_1(do_mtc0_entrylo0, cpu_T[0]);
3520
            rn = "EntryLo0";
3521
            break;
3522
        case 1:
3523
            check_insn(env, ctx, ASE_MT);
3524
            tcg_gen_helper_0_1(do_mtc0_tcstatus, cpu_T[0]);
3525
            rn = "TCStatus";
3526
            break;
3527
        case 2:
3528
            check_insn(env, ctx, ASE_MT);
3529
            tcg_gen_helper_0_1(do_mtc0_tcbind, cpu_T[0]);
3530
            rn = "TCBind";
3531
            break;
3532
        case 3:
3533
            check_insn(env, ctx, ASE_MT);
3534
            tcg_gen_helper_0_1(do_mtc0_tcrestart, cpu_T[0]);
3535
            rn = "TCRestart";
3536
            break;
3537
        case 4:
3538
            check_insn(env, ctx, ASE_MT);
3539
            tcg_gen_helper_0_1(do_mtc0_tchalt, cpu_T[0]);
3540
            rn = "TCHalt";
3541
            break;
3542
        case 5:
3543
            check_insn(env, ctx, ASE_MT);
3544
            tcg_gen_helper_0_1(do_mtc0_tccontext, cpu_T[0]);
3545
            rn = "TCContext";
3546
            break;
3547
        case 6:
3548
            check_insn(env, ctx, ASE_MT);
3549
            tcg_gen_helper_0_1(do_mtc0_tcschedule, cpu_T[0]);
3550
            rn = "TCSchedule";
3551
            break;
3552
        case 7:
3553
            check_insn(env, ctx, ASE_MT);
3554
            tcg_gen_helper_0_1(do_mtc0_tcschefback, cpu_T[0]);
3555
            rn = "TCScheFBack";
3556
            break;
3557
        default:
3558
            goto die;
3559
        }
3560
        break;
3561
    case 3:
3562
        switch (sel) {
3563
        case 0:
3564
            tcg_gen_helper_0_1(do_mtc0_entrylo1, cpu_T[0]);
3565
            rn = "EntryLo1";
3566
            break;
3567
        default:
3568
            goto die;
3569
        }
3570
        break;
3571
    case 4:
3572
        switch (sel) {
3573
        case 0:
3574
            tcg_gen_helper_0_1(do_mtc0_context, cpu_T[0]);
3575
            rn = "Context";
3576
            break;
3577
        case 1:
3578
//            tcg_gen_helper_0_1(do_mtc0_contextconfig, cpu_T[0]); /* SmartMIPS ASE */
3579
            rn = "ContextConfig";
3580
//            break;
3581
        default:
3582
            goto die;
3583
        }
3584
        break;
3585
    case 5:
3586
        switch (sel) {
3587
        case 0:
3588
            tcg_gen_helper_0_1(do_mtc0_pagemask, cpu_T[0]);
3589
            rn = "PageMask";
3590
            break;
3591
        case 1:
3592
            check_insn(env, ctx, ISA_MIPS32R2);
3593
            tcg_gen_helper_0_1(do_mtc0_pagegrain, cpu_T[0]);
3594
            rn = "PageGrain";
3595
            break;
3596
        default:
3597
            goto die;
3598
        }
3599
        break;
3600
    case 6:
3601
        switch (sel) {
3602
        case 0:
3603
            tcg_gen_helper_0_1(do_mtc0_wired, cpu_T[0]);
3604
            rn = "Wired";
3605
            break;
3606
        case 1:
3607
            check_insn(env, ctx, ISA_MIPS32R2);
3608
            tcg_gen_helper_0_1(do_mtc0_srsconf0, cpu_T[0]);
3609
            rn = "SRSConf0";
3610
            break;
3611
        case 2:
3612
            check_insn(env, ctx, ISA_MIPS32R2);
3613
            tcg_gen_helper_0_1(do_mtc0_srsconf1, cpu_T[0]);
3614
            rn = "SRSConf1";
3615
            break;
3616
        case 3:
3617
            check_insn(env, ctx, ISA_MIPS32R2);
3618
            tcg_gen_helper_0_1(do_mtc0_srsconf2, cpu_T[0]);
3619
            rn = "SRSConf2";
3620
            break;
3621
        case 4:
3622
            check_insn(env, ctx, ISA_MIPS32R2);
3623
            tcg_gen_helper_0_1(do_mtc0_srsconf3, cpu_T[0]);
3624
            rn = "SRSConf3";
3625
            break;
3626
        case 5:
3627
            check_insn(env, ctx, ISA_MIPS32R2);
3628
            tcg_gen_helper_0_1(do_mtc0_srsconf4, cpu_T[0]);
3629
            rn = "SRSConf4";
3630
            break;
3631
        default:
3632
            goto die;
3633
        }
3634
        break;
3635
    case 7:
3636
        switch (sel) {
3637
        case 0:
3638
            check_insn(env, ctx, ISA_MIPS32R2);
3639
            tcg_gen_helper_0_1(do_mtc0_hwrena, cpu_T[0]);
3640
            rn = "HWREna";
3641
            break;
3642
        default:
3643
            goto die;
3644
        }
3645
        break;
3646
    case 8:
3647
        /* ignored */
3648
        rn = "BadVAddr";
3649
        break;
3650
    case 9:
3651
        switch (sel) {
3652
        case 0:
3653
            tcg_gen_helper_0_1(do_mtc0_count, cpu_T[0]);
3654
            rn = "Count";
3655
            break;
3656
        /* 6,7 are implementation dependent */
3657
        default:
3658
            goto die;
3659
        }
3660
        /* Stop translation as we may have switched the execution mode */
3661
        ctx->bstate = BS_STOP;
3662
        break;
3663
    case 10:
3664
        switch (sel) {
3665
        case 0:
3666
            tcg_gen_helper_0_1(do_mtc0_entryhi, cpu_T[0]);
3667
            rn = "EntryHi";
3668
            break;
3669
        default:
3670
            goto die;
3671
        }
3672
        break;
3673
    case 11:
3674
        switch (sel) {
3675
        case 0:
3676
            tcg_gen_helper_0_1(do_mtc0_compare, cpu_T[0]);
3677
            rn = "Compare";
3678
            break;
3679
        /* 6,7 are implementation dependent */
3680
        default:
3681
            goto die;
3682
        }
3683
        /* Stop translation as we may have switched the execution mode */
3684
        ctx->bstate = BS_STOP;
3685
        break;
3686
    case 12:
3687
        switch (sel) {
3688
        case 0:
3689
            tcg_gen_helper_0_1(do_mtc0_status, cpu_T[0]);
3690
            /* BS_STOP isn't good enough here, hflags may have changed. */
3691
            gen_save_pc(ctx->pc + 4);
3692
            ctx->bstate = BS_EXCP;
3693
            rn = "Status";
3694
            break;
3695
        case 1:
3696
            check_insn(env, ctx, ISA_MIPS32R2);
3697
            tcg_gen_helper_0_1(do_mtc0_intctl, cpu_T[0]);
3698
            /* Stop translation as we may have switched the execution mode */
3699
            ctx->bstate = BS_STOP;
3700
            rn = "IntCtl";
3701
            break;
3702
        case 2:
3703
            check_insn(env, ctx, ISA_MIPS32R2);
3704
            tcg_gen_helper_0_1(do_mtc0_srsctl, cpu_T[0]);
3705
            /* Stop translation as we may have switched the execution mode */
3706
            ctx->bstate = BS_STOP;
3707
            rn = "SRSCtl";
3708
            break;
3709
        case 3:
3710
            check_insn(env, ctx, ISA_MIPS32R2);
3711
            gen_mtc0_store32(cpu_T[0], offsetof(CPUState, CP0_SRSMap));
3712
            /* Stop translation as we may have switched the execution mode */
3713
            ctx->bstate = BS_STOP;
3714
            rn = "SRSMap";
3715
            break;
3716
        default:
3717
            goto die;
3718
        }
3719
        break;
3720
    case 13:
3721
        switch (sel) {
3722
        case 0:
3723
            tcg_gen_helper_0_1(do_mtc0_cause, cpu_T[0]);
3724
            rn = "Cause";
3725
            break;
3726
        default:
3727
            goto die;
3728
        }
3729
        /* Stop translation as we may have switched the execution mode */
3730
        ctx->bstate = BS_STOP;
3731
        break;
3732
    case 14:
3733
        switch (sel) {
3734
        case 0:
3735
            gen_mtc0_store64(cpu_T[0], offsetof(CPUState, CP0_EPC));
3736
            rn = "EPC";
3737
            break;
3738
        default:
3739
            goto die;
3740
        }
3741
        break;
3742
    case 15:
3743
        switch (sel) {
3744
        case 0:
3745
            /* ignored */
3746
            rn = "PRid";
3747
            break;
3748
        case 1:
3749
            check_insn(env, ctx, ISA_MIPS32R2);
3750
            tcg_gen_helper_0_1(do_mtc0_ebase, cpu_T[0]);
3751
            rn = "EBase";
3752
            break;
3753
        default:
3754
            goto die;
3755
        }
3756
        break;
3757
    case 16:
3758
        switch (sel) {
3759
        case 0:
3760
            tcg_gen_helper_0_1(do_mtc0_config0, cpu_T[0]);
3761
            rn = "Config";
3762
            /* Stop translation as we may have switched the execution mode */
3763
            ctx->bstate = BS_STOP;
3764
            break;
3765
        case 1:
3766
            /* ignored, read only */
3767
            rn = "Config1";
3768
            break;
3769
        case 2:
3770
            tcg_gen_helper_0_1(do_mtc0_config2, cpu_T[0]);
3771
            rn = "Config2";
3772
            /* Stop translation as we may have switched the execution mode */
3773
            ctx->bstate = BS_STOP;
3774
            break;
3775
        case 3:
3776
            /* ignored, read only */
3777
            rn = "Config3";
3778
            break;
3779
        /* 4,5 are reserved */
3780
        /* 6,7 are implementation dependent */
3781
        case 6:
3782
            /* ignored */
3783
            rn = "Config6";
3784
            break;
3785
        case 7:
3786
            /* ignored */
3787
            rn = "Config7";
3788
            break;
3789
        default:
3790
            rn = "Invalid config selector";
3791
            goto die;
3792
        }
3793
        break;
3794
    case 17:
3795
        switch (sel) {
3796
        case 0:
3797
            /* ignored */
3798
            rn = "LLAddr";
3799
            break;
3800
        default:
3801
            goto die;
3802
        }
3803
        break;
3804
    case 18:
3805
        switch (sel) {
3806
        case 0 ... 7:
3807
            tcg_gen_helper_0_1i(do_mtc0_watchlo, cpu_T[0], sel);
3808
            rn = "WatchLo";
3809
            break;
3810
        default:
3811
            goto die;
3812
        }
3813
        break;
3814
    case 19:
3815
        switch (sel) {
3816
        case 0 ... 7:
3817
            tcg_gen_helper_0_1i(do_mtc0_watchhi, cpu_T[0], sel);
3818
            rn = "WatchHi";
3819
            break;
3820
        default:
3821
            goto die;
3822
        }
3823
        break;
3824
    case 20:
3825
        switch (sel) {
3826
        case 0:
3827
#if defined(TARGET_MIPS64)
3828
            check_insn(env, ctx, ISA_MIPS3);
3829
            tcg_gen_helper_0_1(do_mtc0_xcontext, cpu_T[0]);
3830
            rn = "XContext";
3831
            break;
3832
#endif
3833
        default:
3834
            goto die;
3835
        }
3836
        break;
3837
    case 21:
3838
       /* Officially reserved, but sel 0 is used for R1x000 framemask */
3839
        switch (sel) {
3840
        case 0:
3841
            tcg_gen_helper_0_1(do_mtc0_framemask, cpu_T[0]);
3842
            rn = "Framemask";
3843
            break;
3844
        default:
3845
            goto die;
3846
        }
3847
        break;
3848
    case 22:
3849
        /* ignored */
3850
        rn = "Diagnostic"; /* implementation dependent */
3851
        break;
3852
    case 23:
3853
        switch (sel) {
3854
        case 0:
3855
            tcg_gen_helper_0_1(do_mtc0_debug, cpu_T[0]); /* EJTAG support */
3856
            /* BS_STOP isn't good enough here, hflags may have changed. */
3857
            gen_save_pc(ctx->pc + 4);
3858
            ctx->bstate = BS_EXCP;
3859
            rn = "Debug";
3860
            break;
3861
        case 1:
3862
//            tcg_gen_helper_0_1(do_mtc0_tracecontrol, cpu_T[0]); /* PDtrace support */
3863
            rn = "TraceControl";
3864
            /* Stop translation as we may have switched the execution mode */
3865
            ctx->bstate = BS_STOP;
3866
//            break;
3867
        case 2:
3868
//            tcg_gen_helper_0_1(do_mtc0_tracecontrol2, cpu_T[0]); /* PDtrace support */
3869
            rn = "TraceControl2";
3870
            /* Stop translation as we may have switched the execution mode */
3871
            ctx->bstate = BS_STOP;
3872
//            break;
3873
        case 3:
3874
            /* Stop translation as we may have switched the execution mode */
3875
            ctx->bstate = BS_STOP;
3876
//            tcg_gen_helper_0_1(do_mtc0_usertracedata, cpu_T[0]); /* PDtrace support */
3877
            rn = "UserTraceData";
3878
            /* Stop translation as we may have switched the execution mode */
3879
            ctx->bstate = BS_STOP;
3880
//            break;
3881
        case 4:
3882
//            tcg_gen_helper_0_1(do_mtc0_tracebpc, cpu_T[0]); /* PDtrace support */
3883
            /* Stop translation as we may have switched the execution mode */
3884
            ctx->bstate = BS_STOP;
3885
            rn = "TraceBPC";
3886
//            break;
3887
        default:
3888
            goto die;
3889
        }
3890
        break;
3891
    case 24:
3892
        switch (sel) {
3893
        case 0:
3894
            /* EJTAG support */
3895
            gen_mtc0_store64(cpu_T[0], offsetof(CPUState, CP0_DEPC));
3896
            rn = "DEPC";
3897
            break;
3898
        default:
3899
            goto die;
3900
        }
3901
        break;
3902
    case 25:
3903
        switch (sel) {
3904
        case 0:
3905
            tcg_gen_helper_0_1(do_mtc0_performance0, cpu_T[0]);
3906
            rn = "Performance0";
3907
            break;
3908
        case 1:
3909
//            tcg_gen_helper_0_1(do_mtc0_performance1, cpu_T[0]);
3910
            rn = "Performance1";
3911
//            break;
3912
        case 2:
3913
//            tcg_gen_helper_0_1(do_mtc0_performance2, cpu_T[0]);
3914
            rn = "Performance2";
3915
//            break;
3916
        case 3:
3917
//            tcg_gen_helper_0_1(do_mtc0_performance3, cpu_T[0]);
3918
            rn = "Performance3";
3919
//            break;
3920
        case 4:
3921
//            tcg_gen_helper_0_1(do_mtc0_performance4, cpu_T[0]);
3922
            rn = "Performance4";
3923
//            break;
3924
        case 5:
3925
//            tcg_gen_helper_0_1(do_mtc0_performance5, cpu_T[0]);
3926
            rn = "Performance5";
3927
//            break;
3928
        case 6:
3929
//            tcg_gen_helper_0_1(do_mtc0_performance6, cpu_T[0]);
3930
            rn = "Performance6";
3931
//            break;
3932
        case 7:
3933
//            tcg_gen_helper_0_1(do_mtc0_performance7, cpu_T[0]);
3934
            rn = "Performance7";
3935
//            break;
3936
        default:
3937
            goto die;
3938
        }
3939
       break;
3940
    case 26:
3941
        /* ignored */
3942
        rn = "ECC";
3943
        break;
3944
    case 27:
3945
        switch (sel) {
3946
        case 0 ... 3:
3947
            /* ignored */
3948
            rn = "CacheErr";
3949
            break;
3950
        default:
3951
            goto die;
3952
        }
3953
       break;
3954
    case 28:
3955
        switch (sel) {
3956
        case 0:
3957
        case 2:
3958
        case 4:
3959
        case 6:
3960
            tcg_gen_helper_0_1(do_mtc0_taglo, cpu_T[0]);
3961
            rn = "TagLo";
3962
            break;
3963
        case 1:
3964
        case 3:
3965
        case 5:
3966
        case 7:
3967
            tcg_gen_helper_0_1(do_mtc0_datalo, cpu_T[0]);
3968
            rn = "DataLo";
3969
            break;
3970
        default:
3971
            goto die;
3972
        }
3973
        break;
3974
    case 29:
3975
        switch (sel) {
3976
        case 0:
3977
        case 2:
3978
        case 4:
3979
        case 6:
3980
            tcg_gen_helper_0_1(do_mtc0_taghi, cpu_T[0]);
3981
            rn = "TagHi";
3982
            break;
3983
        case 1:
3984
        case 3:
3985
        case 5:
3986
        case 7:
3987
            tcg_gen_helper_0_1(do_mtc0_datahi, cpu_T[0]);
3988
            rn = "DataHi";
3989
            break;
3990
        default:
3991
            rn = "invalid sel";
3992
            goto die;
3993
        }
3994
       break;
3995
    case 30:
3996
        switch (sel) {
3997
        case 0:
3998
            gen_mtc0_store64(cpu_T[0], offsetof(CPUState, CP0_ErrorEPC));
3999
            rn = "ErrorEPC";
4000
            break;
4001
        default:
4002
            goto die;
4003
        }
4004
        break;
4005
    case 31:
4006
        switch (sel) {
4007
        case 0:
4008
            /* EJTAG support */
4009
            gen_mtc0_store32(cpu_T[0], offsetof(CPUState, CP0_DESAVE));
4010
            rn = "DESAVE";
4011
            break;
4012
        default:
4013
            goto die;
4014
        }
4015
        /* Stop translation as we may have switched the execution mode */
4016
        ctx->bstate = BS_STOP;
4017
        break;
4018
    default:
4019
       goto die;
4020
    }
4021
#if defined MIPS_DEBUG_DISAS
4022
    if (loglevel & CPU_LOG_TB_IN_ASM) {
4023
        fprintf(logfile, "mtc0 %s (reg %d sel %d)\n",
4024
                rn, reg, sel);
4025
    }
4026
#endif
4027
    return;
4028

    
4029
die:
4030
#if defined MIPS_DEBUG_DISAS
4031
    if (loglevel & CPU_LOG_TB_IN_ASM) {
4032
        fprintf(logfile, "mtc0 %s (reg %d sel %d)\n",
4033
                rn, reg, sel);
4034
    }
4035
#endif
4036
    generate_exception(ctx, EXCP_RI);
4037
}
4038

    
4039
#if defined(TARGET_MIPS64)
4040
static void gen_dmfc0 (CPUState *env, DisasContext *ctx, int reg, int sel)
4041
{
4042
    const char *rn = "invalid";
4043

    
4044
    if (sel != 0)
4045
        check_insn(env, ctx, ISA_MIPS64);
4046

    
4047
    switch (reg) {
4048
    case 0:
4049
        switch (sel) {
4050
        case 0:
4051
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Index));
4052
            rn = "Index";
4053
            break;
4054
        case 1:
4055
            check_insn(env, ctx, ASE_MT);
4056
            tcg_gen_helper_1_1(do_mfc0_mvpcontrol, cpu_T[0], cpu_T[0]);
4057
            rn = "MVPControl";
4058
            break;
4059
        case 2:
4060
            check_insn(env, ctx, ASE_MT);
4061
            tcg_gen_helper_1_1(do_mfc0_mvpconf0, cpu_T[0], cpu_T[0]);
4062
            rn = "MVPConf0";
4063
            break;
4064
        case 3:
4065
            check_insn(env, ctx, ASE_MT);
4066
            tcg_gen_helper_1_1(do_mfc0_mvpconf1, cpu_T[0], cpu_T[0]);
4067
            rn = "MVPConf1";
4068
            break;
4069
        default:
4070
            goto die;
4071
        }
4072
        break;
4073
    case 1:
4074
        switch (sel) {
4075
        case 0:
4076
            tcg_gen_helper_1_1(do_mfc0_random, cpu_T[0], cpu_T[0]);
4077
            rn = "Random";
4078
            break;
4079
        case 1:
4080
            check_insn(env, ctx, ASE_MT);
4081
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_VPEControl));
4082
            rn = "VPEControl";
4083
            break;
4084
        case 2:
4085
            check_insn(env, ctx, ASE_MT);
4086
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_VPEConf0));
4087
            rn = "VPEConf0";
4088
            break;
4089
        case 3:
4090
            check_insn(env, ctx, ASE_MT);
4091
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_VPEConf1));
4092
            rn = "VPEConf1";
4093
            break;
4094
        case 4:
4095
            check_insn(env, ctx, ASE_MT);
4096
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_YQMask));
4097
            rn = "YQMask";
4098
            break;
4099
        case 5:
4100
            check_insn(env, ctx, ASE_MT);
4101
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_VPESchedule));
4102
            rn = "VPESchedule";
4103
            break;
4104
        case 6:
4105
            check_insn(env, ctx, ASE_MT);
4106
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_VPEScheFBack));
4107
            rn = "VPEScheFBack";
4108
            break;
4109
        case 7:
4110
            check_insn(env, ctx, ASE_MT);
4111
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_VPEOpt));
4112
            rn = "VPEOpt";
4113
            break;
4114
        default:
4115
            goto die;
4116
        }
4117
        break;
4118
    case 2:
4119
        switch (sel) {
4120
        case 0:
4121
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_EntryLo0));
4122
            rn = "EntryLo0";
4123
            break;
4124
        case 1:
4125
            check_insn(env, ctx, ASE_MT);
4126
            tcg_gen_helper_1_1(do_mfc0_tcstatus, cpu_T[0], cpu_T[0]);
4127
            rn = "TCStatus";
4128
            break;
4129
        case 2:
4130
            check_insn(env, ctx, ASE_MT);
4131
            tcg_gen_helper_1_1(do_mfc0_tcbind, cpu_T[0], cpu_T[0]);
4132
            rn = "TCBind";
4133
            break;
4134
        case 3:
4135
            check_insn(env, ctx, ASE_MT);
4136
            tcg_gen_helper_1_1(do_dmfc0_tcrestart, cpu_T[0], cpu_T[0]);
4137
            rn = "TCRestart";
4138
            break;
4139
        case 4:
4140
            check_insn(env, ctx, ASE_MT);
4141
            tcg_gen_helper_1_1(do_dmfc0_tchalt, cpu_T[0], cpu_T[0]);
4142
            rn = "TCHalt";
4143
            break;
4144
        case 5:
4145
            check_insn(env, ctx, ASE_MT);
4146
            tcg_gen_helper_1_1(do_dmfc0_tccontext, cpu_T[0], cpu_T[0]);
4147
            rn = "TCContext";
4148
            break;
4149
        case 6:
4150
            check_insn(env, ctx, ASE_MT);
4151
            tcg_gen_helper_1_1(do_dmfc0_tcschedule, cpu_T[0], cpu_T[0]);
4152
            rn = "TCSchedule";
4153
            break;
4154
        case 7:
4155
            check_insn(env, ctx, ASE_MT);
4156
            tcg_gen_helper_1_1(do_dmfc0_tcschefback, cpu_T[0], cpu_T[0]);
4157
            rn = "TCScheFBack";
4158
            break;
4159
        default:
4160
            goto die;
4161
        }
4162
        break;
4163
    case 3:
4164
        switch (sel) {
4165
        case 0:
4166
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_EntryLo1));
4167
            rn = "EntryLo1";
4168
            break;
4169
        default:
4170
            goto die;
4171
        }
4172
        break;
4173
    case 4:
4174
        switch (sel) {
4175
        case 0:
4176
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_Context));
4177
            rn = "Context";
4178
            break;
4179
        case 1:
4180
//            tcg_gen_helper_1_1(do_dmfc0_contextconfig, cpu_T[0], cpu_T[0]); /* SmartMIPS ASE */
4181
            rn = "ContextConfig";
4182
//            break;
4183
        default:
4184
            goto die;
4185
        }
4186
        break;
4187
    case 5:
4188
        switch (sel) {
4189
        case 0:
4190
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_PageMask));
4191
            rn = "PageMask";
4192
            break;
4193
        case 1:
4194
            check_insn(env, ctx, ISA_MIPS32R2);
4195
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_PageGrain));
4196
            rn = "PageGrain";
4197
            break;
4198
        default:
4199
            goto die;
4200
        }
4201
        break;
4202
    case 6:
4203
        switch (sel) {
4204
        case 0:
4205
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Wired));
4206
            rn = "Wired";
4207
            break;
4208
        case 1:
4209
            check_insn(env, ctx, ISA_MIPS32R2);
4210
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSConf0));
4211
            rn = "SRSConf0";
4212
            break;
4213
        case 2:
4214
            check_insn(env, ctx, ISA_MIPS32R2);
4215
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSConf1));
4216
            rn = "SRSConf1";
4217
            break;
4218
        case 3:
4219
            check_insn(env, ctx, ISA_MIPS32R2);
4220
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSConf2));
4221
            rn = "SRSConf2";
4222
            break;
4223
        case 4:
4224
            check_insn(env, ctx, ISA_MIPS32R2);
4225
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSConf3));
4226
            rn = "SRSConf3";
4227
            break;
4228
        case 5:
4229
            check_insn(env, ctx, ISA_MIPS32R2);
4230
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSConf4));
4231
            rn = "SRSConf4";
4232
            break;
4233
        default:
4234
            goto die;
4235
        }
4236
        break;
4237
    case 7:
4238
        switch (sel) {
4239
        case 0:
4240
            check_insn(env, ctx, ISA_MIPS32R2);
4241
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_HWREna));
4242
            rn = "HWREna";
4243
            break;
4244
        default:
4245
            goto die;
4246
        }
4247
        break;
4248
    case 8:
4249
        switch (sel) {
4250
        case 0:
4251
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_BadVAddr));
4252
            rn = "BadVAddr";
4253
            break;
4254
        default:
4255
            goto die;
4256
        }
4257
        break;
4258
    case 9:
4259
        switch (sel) {
4260
        case 0:
4261
            tcg_gen_helper_1_1(do_mfc0_count, cpu_T[0], cpu_T[0]);
4262
            rn = "Count";
4263
            break;
4264
        /* 6,7 are implementation dependent */
4265
        default:
4266
            goto die;
4267
        }
4268
        break;
4269
    case 10:
4270
        switch (sel) {
4271
        case 0:
4272
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_EntryHi));
4273
            rn = "EntryHi";
4274
            break;
4275
        default:
4276
            goto die;
4277
        }
4278
        break;
4279
    case 11:
4280
        switch (sel) {
4281
        case 0:
4282
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Compare));
4283
            rn = "Compare";
4284
            break;
4285
        /* 6,7 are implementation dependent */
4286
        default:
4287
            goto die;
4288
        }
4289
        break;
4290
    case 12:
4291
        switch (sel) {
4292
        case 0:
4293
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Status));
4294
            rn = "Status";
4295
            break;
4296
        case 1:
4297
            check_insn(env, ctx, ISA_MIPS32R2);
4298
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_IntCtl));
4299
            rn = "IntCtl";
4300
            break;
4301
        case 2:
4302
            check_insn(env, ctx, ISA_MIPS32R2);
4303
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSCtl));
4304
            rn = "SRSCtl";
4305
            break;
4306
        case 3:
4307
            check_insn(env, ctx, ISA_MIPS32R2);
4308
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSMap));
4309
            rn = "SRSMap";
4310
            break;
4311
        default:
4312
            goto die;
4313
        }
4314
        break;
4315
    case 13:
4316
        switch (sel) {
4317
        case 0:
4318
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Cause));
4319
            rn = "Cause";
4320
            break;
4321
        default:
4322
            goto die;
4323
        }
4324
        break;
4325
    case 14:
4326
        switch (sel) {
4327
        case 0:
4328
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_EPC));
4329
            rn = "EPC";
4330
            break;
4331
        default:
4332
            goto die;
4333
        }
4334
        break;
4335
    case 15:
4336
        switch (sel) {
4337
        case 0:
4338
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_PRid));
4339
            rn = "PRid";
4340
            break;
4341
        case 1:
4342
            check_insn(env, ctx, ISA_MIPS32R2);
4343
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_EBase));
4344
            rn = "EBase";
4345
            break;
4346
        default:
4347
            goto die;
4348
        }
4349
        break;
4350
    case 16:
4351
        switch (sel) {
4352
        case 0:
4353
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config0));
4354
            rn = "Config";
4355
            break;
4356
        case 1:
4357
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config1));
4358
            rn = "Config1";
4359
            break;
4360
        case 2:
4361
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config2));
4362
            rn = "Config2";
4363
            break;
4364
        case 3:
4365
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config3));
4366
            rn = "Config3";
4367
            break;
4368
       /* 6,7 are implementation dependent */
4369
        case 6:
4370
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config6));
4371
            rn = "Config6";
4372
            break;
4373
        case 7:
4374
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config7));
4375
            rn = "Config7";
4376
            break;
4377
        default:
4378
            goto die;
4379
        }
4380
        break;
4381
    case 17:
4382
        switch (sel) {
4383
        case 0:
4384
            tcg_gen_helper_1_1(do_dmfc0_lladdr, cpu_T[0], cpu_T[0]);
4385
            rn = "LLAddr";
4386
            break;
4387
        default:
4388
            goto die;
4389
        }
4390
        break;
4391
    case 18:
4392
        switch (sel) {
4393
        case 0 ... 7:
4394
            tcg_gen_helper_1_1i(do_dmfc0_watchlo, cpu_T[0], cpu_T[0], sel);
4395
            rn = "WatchLo";
4396
            break;
4397
        default:
4398
            goto die;
4399
        }
4400
        break;
4401
    case 19:
4402
        switch (sel) {
4403
        case 0 ... 7:
4404
            tcg_gen_helper_1_1i(do_mfc0_watchhi, cpu_T[0], cpu_T[0], sel);
4405
            rn = "WatchHi";
4406
            break;
4407
        default:
4408
            goto die;
4409
        }
4410
        break;
4411
    case 20:
4412
        switch (sel) {
4413
        case 0:
4414
            check_insn(env, ctx, ISA_MIPS3);
4415
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_XContext));
4416
            rn = "XContext";
4417
            break;
4418
        default:
4419
            goto die;
4420
        }
4421
        break;
4422
    case 21:
4423
       /* Officially reserved, but sel 0 is used for R1x000 framemask */
4424
        switch (sel) {
4425
        case 0:
4426
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Framemask));
4427
            rn = "Framemask";
4428
            break;
4429
        default:
4430
            goto die;
4431
        }
4432
        break;
4433
    case 22:
4434
        /* ignored */
4435
        rn = "'Diagnostic"; /* implementation dependent */
4436
        break;
4437
    case 23:
4438
        switch (sel) {
4439
        case 0:
4440
            tcg_gen_helper_1_1(do_mfc0_debug, cpu_T[0], cpu_T[0]); /* EJTAG support */
4441
            rn = "Debug";
4442
            break;
4443
        case 1:
4444
//            tcg_gen_helper_1_1(do_dmfc0_tracecontrol, cpu_T[0], cpu_T[0]); /* PDtrace support */
4445
            rn = "TraceControl";
4446
//            break;
4447
        case 2:
4448
//            tcg_gen_helper_1_1(do_dmfc0_tracecontrol2, cpu_T[0], cpu_T[0]); /* PDtrace support */
4449
            rn = "TraceControl2";
4450
//            break;
4451
        case 3:
4452
//            tcg_gen_helper_1_1(do_dmfc0_usertracedata, cpu_T[0], cpu_T[0]); /* PDtrace support */
4453
            rn = "UserTraceData";
4454
//            break;
4455
        case 4:
4456
//            tcg_gen_helper_1_1(do_dmfc0_tracebpc, cpu_T[0], cpu_T[0]); /* PDtrace support */
4457
            rn = "TraceBPC";
4458
//            break;
4459
        default:
4460
            goto die;
4461
        }
4462
        break;
4463
    case 24:
4464
        switch (sel) {
4465
        case 0:
4466
            /* EJTAG support */
4467
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_DEPC));
4468
            rn = "DEPC";
4469
            break;
4470
        default:
4471
            goto die;
4472
        }
4473
        break;
4474
    case 25:
4475
        switch (sel) {
4476
        case 0:
4477
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Performance0));
4478
            rn = "Performance0";
4479
            break;
4480
        case 1:
4481
//            tcg_gen_helper_1_1(do_dmfc0_performance1, cpu_T[0], cpu_T[0]);
4482
            rn = "Performance1";
4483
//            break;
4484
        case 2:
4485
//            tcg_gen_helper_1_1(do_dmfc0_performance2, cpu_T[0], cpu_T[0]);
4486
            rn = "Performance2";
4487
//            break;
4488
        case 3:
4489
//            tcg_gen_helper_1_1(do_dmfc0_performance3, cpu_T[0], cpu_T[0]);
4490
            rn = "Performance3";
4491
//            break;
4492
        case 4:
4493
//            tcg_gen_helper_1_1(do_dmfc0_performance4, cpu_T[0], cpu_T[0]);
4494
            rn = "Performance4";
4495
//            break;
4496
        case 5:
4497
//            tcg_gen_helper_1_1(do_dmfc0_performance5, cpu_T[0], cpu_T[0]);
4498
            rn = "Performance5";
4499
//            break;
4500
        case 6:
4501
//            tcg_gen_helper_1_1(do_dmfc0_performance6, cpu_T[0], cpu_T[0]);
4502
            rn = "Performance6";
4503
//            break;
4504
        case 7:
4505
//            tcg_gen_helper_1_1(do_dmfc0_performance7, cpu_T[0], cpu_T[0]);
4506
            rn = "Performance7";
4507
//            break;
4508
        default:
4509
            goto die;
4510
        }
4511
        break;
4512
    case 26:
4513
       rn = "ECC";
4514
       break;
4515
    case 27:
4516
        switch (sel) {
4517
        /* ignored */
4518
        case 0 ... 3:
4519
            rn = "CacheErr";
4520
            break;
4521
        default:
4522
            goto die;
4523
        }
4524
        break;
4525
    case 28:
4526
        switch (sel) {
4527
        case 0:
4528
        case 2:
4529
        case 4:
4530
        case 6:
4531
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_TagLo));
4532
            rn = "TagLo";
4533
            break;
4534
        case 1:
4535
        case 3:
4536
        case 5:
4537
        case 7:
4538
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_DataLo));
4539
            rn = "DataLo";
4540
            break;
4541
        default:
4542
            goto die;
4543
        }
4544
        break;
4545
    case 29:
4546
        switch (sel) {
4547
        case 0:
4548
        case 2:
4549
        case 4:
4550
        case 6:
4551
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_TagHi));
4552
            rn = "TagHi";
4553
            break;
4554
        case 1:
4555
        case 3:
4556
        case 5:
4557
        case 7:
4558
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_DataHi));
4559
            rn = "DataHi";
4560
            break;
4561
        default:
4562
            goto die;
4563
        }
4564
        break;
4565
    case 30:
4566
        switch (sel) {
4567
        case 0:
4568
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_ErrorEPC));
4569
            rn = "ErrorEPC";
4570
            break;
4571
        default:
4572
            goto die;
4573
        }
4574
        break;
4575
    case 31:
4576
        switch (sel) {
4577
        case 0:
4578
            /* EJTAG support */
4579
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_DESAVE));
4580
            rn = "DESAVE";
4581
            break;
4582
        default:
4583
            goto die;
4584
        }
4585
        break;
4586
    default:
4587
        goto die;
4588
    }
4589
#if defined MIPS_DEBUG_DISAS
4590
    if (loglevel & CPU_LOG_TB_IN_ASM) {
4591
        fprintf(logfile, "dmfc0 %s (reg %d sel %d)\n",
4592
                rn, reg, sel);
4593
    }
4594
#endif
4595
    return;
4596

    
4597
die:
4598
#if defined MIPS_DEBUG_DISAS
4599
    if (loglevel & CPU_LOG_TB_IN_ASM) {
4600
        fprintf(logfile, "dmfc0 %s (reg %d sel %d)\n",
4601
                rn, reg, sel);
4602
    }
4603
#endif
4604
    generate_exception(ctx, EXCP_RI);
4605
}
4606

    
4607
static void gen_dmtc0 (CPUState *env, DisasContext *ctx, int reg, int sel)
4608
{
4609
    const char *rn = "invalid";
4610

    
4611
    if (sel != 0)
4612
        check_insn(env, ctx, ISA_MIPS64);
4613

    
4614
    switch (reg) {
4615
    case 0:
4616
        switch (sel) {
4617
        case 0:
4618
            tcg_gen_helper_0_1(do_mtc0_index, cpu_T[0]);
4619
            rn = "Index";
4620
            break;
4621
        case 1:
4622
            check_insn(env, ctx, ASE_MT);
4623
            tcg_gen_helper_0_1(do_mtc0_mvpcontrol, cpu_T[0]);
4624
            rn = "MVPControl";
4625
            break;
4626
        case 2:
4627
            check_insn(env, ctx, ASE_MT);
4628
            /* ignored */
4629
            rn = "MVPConf0";
4630
            break;
4631
        case 3:
4632
            check_insn(env, ctx, ASE_MT);
4633
            /* ignored */
4634
            rn = "MVPConf1";
4635
            break;
4636
        default:
4637
            goto die;
4638
        }
4639
        break;
4640
    case 1:
4641
        switch (sel) {
4642
        case 0:
4643
            /* ignored */
4644
            rn = "Random";
4645
            break;
4646
        case 1:
4647
            check_insn(env, ctx, ASE_MT);
4648
            tcg_gen_helper_0_1(do_mtc0_vpecontrol, cpu_T[0]);
4649
            rn = "VPEControl";
4650
            break;
4651
        case 2:
4652
            check_insn(env, ctx, ASE_MT);
4653
            tcg_gen_helper_0_1(do_mtc0_vpeconf0, cpu_T[0]);
4654
            rn = "VPEConf0";
4655
            break;
4656
        case 3:
4657
            check_insn(env, ctx, ASE_MT);
4658
            tcg_gen_helper_0_1(do_mtc0_vpeconf1, cpu_T[0]);
4659
            rn = "VPEConf1";
4660
            break;
4661
        case 4:
4662
            check_insn(env, ctx, ASE_MT);
4663
            tcg_gen_helper_0_1(do_mtc0_yqmask, cpu_T[0]);
4664
            rn = "YQMask";
4665
            break;
4666
        case 5:
4667
            check_insn(env, ctx, ASE_MT);
4668
            tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_VPESchedule));
4669
            rn = "VPESchedule";
4670
            break;
4671
        case 6:
4672
            check_insn(env, ctx, ASE_MT);
4673
            tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_VPEScheFBack));
4674
            rn = "VPEScheFBack";
4675
            break;
4676
        case 7:
4677
            check_insn(env, ctx, ASE_MT);
4678
            tcg_gen_helper_0_1(do_mtc0_vpeopt, cpu_T[0]);
4679
            rn = "VPEOpt";
4680
            break;
4681
        default:
4682
            goto die;
4683
        }
4684
        break;
4685
    case 2:
4686
        switch (sel) {
4687
        case 0:
4688
            tcg_gen_helper_0_1(do_mtc0_entrylo0, cpu_T[0]);
4689
            rn = "EntryLo0";
4690
            break;
4691
        case 1:
4692
            check_insn(env, ctx, ASE_MT);
4693
            tcg_gen_helper_0_1(do_mtc0_tcstatus, cpu_T[0]);
4694
            rn = "TCStatus";
4695
            break;
4696
        case 2:
4697
            check_insn(env, ctx, ASE_MT);
4698
            tcg_gen_helper_0_1(do_mtc0_tcbind, cpu_T[0]);
4699
            rn = "TCBind";
4700
            break;
4701
        case 3:
4702
            check_insn(env, ctx, ASE_MT);
4703
            tcg_gen_helper_0_1(do_mtc0_tcrestart, cpu_T[0]);
4704
            rn = "TCRestart";
4705
            break;
4706
        case 4:
4707
            check_insn(env, ctx, ASE_MT);
4708
            tcg_gen_helper_0_1(do_mtc0_tchalt, cpu_T[0]);
4709
            rn = "TCHalt";
4710
            break;
4711
        case 5:
4712
            check_insn(env, ctx, ASE_MT);
4713
            tcg_gen_helper_0_1(do_mtc0_tccontext, cpu_T[0]);
4714
            rn = "TCContext";
4715
            break;
4716
        case 6:
4717
            check_insn(env, ctx, ASE_MT);
4718
            tcg_gen_helper_0_1(do_mtc0_tcschedule, cpu_T[0]);
4719
            rn = "TCSchedule";
4720
            break;
4721
        case 7:
4722
            check_insn(env, ctx, ASE_MT);
4723
            tcg_gen_helper_0_1(do_mtc0_tcschefback, cpu_T[0]);
4724
            rn = "TCScheFBack";
4725
            break;
4726
        default:
4727
            goto die;
4728
        }
4729
        break;
4730
    case 3:
4731
        switch (sel) {
4732
        case 0:
4733
            tcg_gen_helper_0_1(do_mtc0_entrylo1, cpu_T[0]);
4734
            rn = "EntryLo1";
4735
            break;
4736
        default:
4737
            goto die;
4738
        }
4739
        break;
4740
    case 4:
4741
        switch (sel) {
4742
        case 0:
4743
            tcg_gen_helper_0_1(do_mtc0_context, cpu_T[0]);
4744
            rn = "Context";
4745
            break;
4746
        case 1:
4747
//           tcg_gen_helper_0_1(do_mtc0_contextconfig, cpu_T[0]); /* SmartMIPS ASE */
4748
            rn = "ContextConfig";
4749
//           break;
4750
        default:
4751
            goto die;
4752
        }
4753
        break;
4754
    case 5:
4755
        switch (sel) {
4756
        case 0:
4757
            tcg_gen_helper_0_1(do_mtc0_pagemask, cpu_T[0]);
4758
            rn = "PageMask";
4759
            break;
4760
        case 1:
4761
            check_insn(env, ctx, ISA_MIPS32R2);
4762
            tcg_gen_helper_0_1(do_mtc0_pagegrain, cpu_T[0]);
4763
            rn = "PageGrain";
4764
            break;
4765
        default:
4766
            goto die;
4767
        }
4768
        break;
4769
    case 6:
4770
        switch (sel) {
4771
        case 0:
4772
            tcg_gen_helper_0_1(do_mtc0_wired, cpu_T[0]);
4773
            rn = "Wired";
4774
            break;
4775
        case 1:
4776
            check_insn(env, ctx, ISA_MIPS32R2);
4777
            tcg_gen_helper_0_1(do_mtc0_srsconf0, cpu_T[0]);
4778
            rn = "SRSConf0";
4779
            break;
4780
        case 2:
4781
            check_insn(env, ctx, ISA_MIPS32R2);
4782
            tcg_gen_helper_0_1(do_mtc0_srsconf1, cpu_T[0]);
4783
            rn = "SRSConf1";
4784
            break;
4785
        case 3:
4786
            check_insn(env, ctx, ISA_MIPS32R2);
4787
            tcg_gen_helper_0_1(do_mtc0_srsconf2, cpu_T[0]);
4788
            rn = "SRSConf2";
4789
            break;
4790
        case 4:
4791
            check_insn(env, ctx, ISA_MIPS32R2);
4792
            tcg_gen_helper_0_1(do_mtc0_srsconf3, cpu_T[0]);
4793
            rn = "SRSConf3";
4794
            break;
4795
        case 5:
4796
            check_insn(env, ctx, ISA_MIPS32R2);
4797
            tcg_gen_helper_0_1(do_mtc0_srsconf4, cpu_T[0]);
4798
            rn = "SRSConf4";
4799
            break;
4800
        default:
4801
            goto die;
4802
        }
4803
        break;
4804
    case 7:
4805
        switch (sel) {
4806
        case 0:
4807
            check_insn(env, ctx, ISA_MIPS32R2);
4808
            tcg_gen_helper_0_1(do_mtc0_hwrena, cpu_T[0]);
4809
            rn = "HWREna";
4810
            break;
4811
        default:
4812
            goto die;
4813
        }
4814
        break;
4815
    case 8:
4816
        /* ignored */
4817
        rn = "BadVAddr";
4818
        break;
4819
    case 9:
4820
        switch (sel) {
4821
        case 0:
4822
            tcg_gen_helper_0_1(do_mtc0_count, cpu_T[0]);
4823
            rn = "Count";
4824
            break;
4825
        /* 6,7 are implementation dependent */
4826
        default:
4827
            goto die;
4828
        }
4829
        /* Stop translation as we may have switched the execution mode */
4830
        ctx->bstate = BS_STOP;
4831
        break;
4832
    case 10:
4833
        switch (sel) {
4834
        case 0:
4835
            tcg_gen_helper_0_1(do_mtc0_entryhi, cpu_T[0]);
4836
            rn = "EntryHi";
4837
            break;
4838
        default:
4839
            goto die;
4840
        }
4841
        break;
4842
    case 11:
4843
        switch (sel) {
4844
        case 0:
4845
            tcg_gen_helper_0_1(do_mtc0_compare, cpu_T[0]);
4846
            rn = "Compare";
4847
            break;
4848
        /* 6,7 are implementation dependent */
4849
        default:
4850
            goto die;
4851
        }
4852
        /* Stop translation as we may have switched the execution mode */
4853
        ctx->bstate = BS_STOP;
4854
        break;
4855
    case 12:
4856
        switch (sel) {
4857
        case 0:
4858
            tcg_gen_helper_0_1(do_mtc0_status, cpu_T[0]);
4859
            /* BS_STOP isn't good enough here, hflags may have changed. */
4860
            gen_save_pc(ctx->pc + 4);
4861
            ctx->bstate = BS_EXCP;
4862
            rn = "Status";
4863
            break;
4864
        case 1:
4865
            check_insn(env, ctx, ISA_MIPS32R2);
4866
            tcg_gen_helper_0_1(do_mtc0_intctl, cpu_T[0]);
4867
            /* Stop translation as we may have switched the execution mode */
4868
            ctx->bstate = BS_STOP;
4869
            rn = "IntCtl";
4870
            break;
4871
        case 2:
4872
            check_insn(env, ctx, ISA_MIPS32R2);
4873
            tcg_gen_helper_0_1(do_mtc0_srsctl, cpu_T[0]);
4874
            /* Stop translation as we may have switched the execution mode */
4875
            ctx->bstate = BS_STOP;
4876
            rn = "SRSCtl";
4877
            break;
4878
        case 3:
4879
            check_insn(env, ctx, ISA_MIPS32R2);
4880
            gen_mtc0_store32(cpu_T[0], offsetof(CPUState, CP0_SRSMap));
4881
            /* Stop translation as we may have switched the execution mode */
4882
            ctx->bstate = BS_STOP;
4883
            rn = "SRSMap";
4884
            break;
4885
        default:
4886
            goto die;
4887
        }
4888
        break;
4889
    case 13:
4890
        switch (sel) {
4891
        case 0:
4892
            tcg_gen_helper_0_1(do_mtc0_cause, cpu_T[0]);
4893
            rn = "Cause";
4894
            break;
4895
        default:
4896
            goto die;
4897
        }
4898
        /* Stop translation as we may have switched the execution mode */
4899
        ctx->bstate = BS_STOP;
4900
        break;
4901
    case 14:
4902
        switch (sel) {
4903
        case 0:
4904
            tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_EPC));
4905
            rn = "EPC";
4906
            break;
4907
        default:
4908
            goto die;
4909
        }
4910
        break;
4911
    case 15:
4912
        switch (sel) {
4913
        case 0:
4914
            /* ignored */
4915
            rn = "PRid";
4916
            break;
4917
        case 1:
4918
            check_insn(env, ctx, ISA_MIPS32R2);
4919
            tcg_gen_helper_0_1(do_mtc0_ebase, cpu_T[0]);
4920
            rn = "EBase";
4921
            break;
4922
        default:
4923
            goto die;
4924
        }
4925
        break;
4926
    case 16:
4927
        switch (sel) {
4928
        case 0:
4929
            tcg_gen_helper_0_1(do_mtc0_config0, cpu_T[0]);
4930
            rn = "Config";
4931
            /* Stop translation as we may have switched the execution mode */
4932
            ctx->bstate = BS_STOP;
4933
            break;
4934
        case 1:
4935
            /* ignored */
4936
            rn = "Config1";
4937
            break;
4938
        case 2:
4939
            tcg_gen_helper_0_1(do_mtc0_config2, cpu_T[0]);
4940
            rn = "Config2";
4941
            /* Stop translation as we may have switched the execution mode */
4942
            ctx->bstate = BS_STOP;
4943
            break;
4944
        case 3:
4945
            /* ignored */
4946
            rn = "Config3";
4947
            break;
4948
        /* 6,7 are implementation dependent */
4949
        default:
4950
            rn = "Invalid config selector";
4951
            goto die;
4952
        }
4953
        break;
4954
    case 17:
4955
        switch (sel) {
4956
        case 0:
4957
            /* ignored */
4958
            rn = "LLAddr";
4959
            break;
4960
        default:
4961
            goto die;
4962
        }
4963
        break;
4964
    case 18:
4965
        switch (sel) {
4966
        case 0 ... 7:
4967
            tcg_gen_helper_0_1i(do_mtc0_watchlo, cpu_T[0], sel);
4968
            rn = "WatchLo";
4969
            break;
4970
        default:
4971
            goto die;
4972
        }
4973
        break;
4974
    case 19:
4975
        switch (sel) {
4976
        case 0 ... 7:
4977
            tcg_gen_helper_0_1i(do_mtc0_watchhi, cpu_T[0], sel);
4978
            rn = "WatchHi";
4979
            break;
4980
        default:
4981
            goto die;
4982
        }
4983
        break;
4984
    case 20:
4985
        switch (sel) {
4986
        case 0:
4987
            check_insn(env, ctx, ISA_MIPS3);
4988
            tcg_gen_helper_0_1(do_mtc0_xcontext, cpu_T[0]);
4989
            rn = "XContext";
4990
            break;
4991
        default:
4992
            goto die;
4993
        }
4994
        break;
4995
    case 21:
4996
       /* Officially reserved, but sel 0 is used for R1x000 framemask */
4997
        switch (sel) {
4998
        case 0:
4999
            tcg_gen_helper_0_1(do_mtc0_framemask, cpu_T[0]);
5000
            rn = "Framemask";
5001
            break;
5002
        default:
5003
            goto die;
5004
        }
5005
        break;
5006
    case 22:
5007
        /* ignored */
5008
        rn = "Diagnostic"; /* implementation dependent */
5009
        break;
5010
    case 23:
5011
        switch (sel) {
5012
        case 0:
5013
            tcg_gen_helper_0_1(do_mtc0_debug, cpu_T[0]); /* EJTAG support */
5014
            /* BS_STOP isn't good enough here, hflags may have changed. */
5015
            gen_save_pc(ctx->pc + 4);
5016
            ctx->bstate = BS_EXCP;
5017
            rn = "Debug";
5018
            break;
5019
        case 1:
5020
//            tcg_gen_helper_0_1(do_mtc0_tracecontrol, cpu_T[0]); /* PDtrace support */
5021
            /* Stop translation as we may have switched the execution mode */
5022
            ctx->bstate = BS_STOP;
5023
            rn = "TraceControl";
5024
//            break;
5025
        case 2:
5026
//            tcg_gen_helper_0_1(do_mtc0_tracecontrol2, cpu_T[0]); /* PDtrace support */
5027
            /* Stop translation as we may have switched the execution mode */
5028
            ctx->bstate = BS_STOP;
5029
            rn = "TraceControl2";
5030
//            break;
5031
        case 3:
5032
//            tcg_gen_helper_0_1(do_mtc0_usertracedata, cpu_T[0]); /* PDtrace support */
5033
            /* Stop translation as we may have switched the execution mode */
5034
            ctx->bstate = BS_STOP;
5035
            rn = "UserTraceData";
5036
//            break;
5037
        case 4:
5038
//            tcg_gen_helper_0_1(do_mtc0_tracebpc, cpu_T[0]); /* PDtrace support */
5039
            /* Stop translation as we may have switched the execution mode */
5040
            ctx->bstate = BS_STOP;
5041
            rn = "TraceBPC";
5042
//            break;
5043
        default:
5044
            goto die;
5045
        }
5046
        break;
5047
    case 24:
5048
        switch (sel) {
5049
        case 0:
5050
            /* EJTAG support */
5051
            tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_DEPC));
5052
            rn = "DEPC";
5053
            break;
5054
        default:
5055
            goto die;
5056
        }
5057
        break;
5058
    case 25:
5059
        switch (sel) {
5060
        case 0:
5061
            tcg_gen_helper_0_1(do_mtc0_performance0, cpu_T[0]);
5062
            rn = "Performance0";
5063
            break;
5064
        case 1:
5065
//            tcg_gen_helper_0_1(do_mtc0_performance1, cpu_T[0]);
5066
            rn = "Performance1";
5067
//            break;
5068
        case 2:
5069
//            tcg_gen_helper_0_1(do_mtc0_performance2, cpu_T[0]);
5070
            rn = "Performance2";
5071
//            break;
5072
        case 3:
5073
//            tcg_gen_helper_0_1(do_mtc0_performance3, cpu_T[0]);
5074
            rn = "Performance3";
5075
//            break;
5076
        case 4:
5077
//            tcg_gen_helper_0_1(do_mtc0_performance4, cpu_T[0]);
5078
            rn = "Performance4";
5079
//            break;
5080
        case 5:
5081
//            tcg_gen_helper_0_1(do_mtc0_performance5, cpu_T[0]);
5082
            rn = "Performance5";
5083
//            break;
5084
        case 6:
5085
//            tcg_gen_helper_0_1(do_mtc0_performance6, cpu_T[0]);
5086
            rn = "Performance6";
5087
//            break;
5088
        case 7:
5089
//            tcg_gen_helper_0_1(do_mtc0_performance7, cpu_T[0]);
5090
            rn = "Performance7";
5091
//            break;
5092
        default:
5093
            goto die;
5094
        }
5095
        break;
5096
    case 26:
5097
        /* ignored */
5098
        rn = "ECC";
5099
        break;
5100
    case 27:
5101
        switch (sel) {
5102
        case 0 ... 3:
5103
            /* ignored */
5104
            rn = "CacheErr";
5105
            break;
5106
        default:
5107
            goto die;
5108
        }
5109
        break;
5110
    case 28:
5111
        switch (sel) {
5112
        case 0:
5113
        case 2:
5114
        case 4:
5115
        case 6:
5116
            tcg_gen_helper_0_1(do_mtc0_taglo, cpu_T[0]);
5117
            rn = "TagLo";
5118
            break;
5119
        case 1:
5120
        case 3:
5121
        case 5:
5122
        case 7:
5123
            tcg_gen_helper_0_1(do_mtc0_datalo, cpu_T[0]);
5124
            rn = "DataLo";
5125
            break;
5126
        default:
5127
            goto die;
5128
        }
5129
        break;
5130
    case 29:
5131
        switch (sel) {
5132
        case 0:
5133
        case 2:
5134
        case 4:
5135
        case 6:
5136
            tcg_gen_helper_0_1(do_mtc0_taghi, cpu_T[0]);
5137
            rn = "TagHi";
5138
            break;
5139
        case 1:
5140
        case 3:
5141
        case 5:
5142
        case 7:
5143
            tcg_gen_helper_0_1(do_mtc0_datahi, cpu_T[0]);
5144
            rn = "DataHi";
5145
            break;
5146
        default:
5147
            rn = "invalid sel";
5148
            goto die;
5149
        }
5150
        break;
5151
    case 30:
5152
        switch (sel) {
5153
        case 0:
5154
            tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_ErrorEPC));
5155
            rn = "ErrorEPC";
5156
            break;
5157
        default:
5158
            goto die;
5159
        }
5160
        break;
5161
    case 31:
5162
        switch (sel) {
5163
        case 0:
5164
            /* EJTAG support */
5165
            gen_mtc0_store32(cpu_T[0], offsetof(CPUState, CP0_DESAVE));
5166
            rn = "DESAVE";
5167
            break;
5168
        default:
5169
            goto die;
5170
        }
5171
        /* Stop translation as we may have switched the execution mode */
5172
        ctx->bstate = BS_STOP;
5173
        break;
5174
    default:
5175
        goto die;
5176
    }
5177
#if defined MIPS_DEBUG_DISAS
5178
    if (loglevel & CPU_LOG_TB_IN_ASM) {
5179
        fprintf(logfile, "dmtc0 %s (reg %d sel %d)\n",
5180
                rn, reg, sel);
5181
    }
5182
#endif
5183
    return;
5184

    
5185
die:
5186
#if defined MIPS_DEBUG_DISAS
5187
    if (loglevel & CPU_LOG_TB_IN_ASM) {
5188
        fprintf(logfile, "dmtc0 %s (reg %d sel %d)\n",
5189
                rn, reg, sel);
5190
    }
5191
#endif
5192
    generate_exception(ctx, EXCP_RI);
5193
}
5194
#endif /* TARGET_MIPS64 */
5195

    
5196
static void gen_mftr(CPUState *env, DisasContext *ctx, int rt, int rd,
5197
                     int u, int sel, int h)
5198
{
5199
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
5200

    
5201
    if ((env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP)) == 0 &&
5202
        ((env->CP0_TCBind[other_tc] & (0xf << CP0TCBd_CurVPE)) !=
5203
         (env->CP0_TCBind[env->current_tc] & (0xf << CP0TCBd_CurVPE))))
5204
        tcg_gen_movi_tl(cpu_T[0], -1);
5205
    else if ((env->CP0_VPEControl & (0xff << CP0VPECo_TargTC)) >
5206
             (env->mvp->CP0_MVPConf0 & (0xff << CP0MVPC0_PTC)))
5207
        tcg_gen_movi_tl(cpu_T[0], -1);
5208
    else if (u == 0) {
5209
        switch (rt) {
5210
        case 2:
5211
            switch (sel) {
5212
            case 1:
5213
                tcg_gen_helper_1_1(do_mftc0_tcstatus, cpu_T[0], cpu_T[0]);
5214
                break;
5215
            case 2:
5216
                tcg_gen_helper_1_1(do_mftc0_tcbind, cpu_T[0], cpu_T[0]);
5217
                break;
5218
            case 3:
5219
                tcg_gen_helper_1_1(do_mftc0_tcrestart, cpu_T[0], cpu_T[0]);
5220
                break;
5221
            case 4:
5222
                tcg_gen_helper_1_1(do_mftc0_tchalt, cpu_T[0], cpu_T[0]);
5223
                break;
5224
            case 5:
5225
                tcg_gen_helper_1_1(do_mftc0_tccontext, cpu_T[0], cpu_T[0]);
5226
                break;
5227
            case 6:
5228
                tcg_gen_helper_1_1(do_mftc0_tcschedule, cpu_T[0], cpu_T[0]);
5229
                break;
5230
            case 7:
5231
                tcg_gen_helper_1_1(do_mftc0_tcschefback, cpu_T[0], cpu_T[0]);
5232
                break;
5233
            default:
5234
                gen_mfc0(env, ctx, rt, sel);
5235
                break;
5236
            }
5237
            break;
5238
        case 10:
5239
            switch (sel) {
5240
            case 0:
5241
                tcg_gen_helper_1_1(do_mftc0_entryhi, cpu_T[0], cpu_T[0]);
5242
                break;
5243
            default:
5244
                gen_mfc0(env, ctx, rt, sel);
5245
                break;
5246
            }
5247
        case 12:
5248
            switch (sel) {
5249
            case 0:
5250
                tcg_gen_helper_1_1(do_mftc0_status, cpu_T[0], cpu_T[0]);
5251
                break;
5252
            default:
5253
                gen_mfc0(env, ctx, rt, sel);
5254
                break;
5255
            }
5256
        case 23:
5257
            switch (sel) {
5258
            case 0:
5259
                tcg_gen_helper_1_1(do_mftc0_debug, cpu_T[0], cpu_T[0]);
5260
                break;
5261
            default:
5262
                gen_mfc0(env, ctx, rt, sel);
5263
                break;
5264
            }
5265
            break;
5266
        default:
5267
            gen_mfc0(env, ctx, rt, sel);
5268
        }
5269
    } else switch (sel) {
5270
    /* GPR registers. */
5271
    case 0:
5272
        tcg_gen_helper_1_1i(do_mftgpr, cpu_T[0], cpu_T[0], rt);
5273
        break;
5274
    /* Auxiliary CPU registers */
5275
    case 1:
5276
        switch (rt) {
5277
        case 0:
5278
            tcg_gen_helper_1_1i(do_mftlo, cpu_T[0], cpu_T[0], 0);
5279
            break;
5280
        case 1:
5281
            tcg_gen_helper_1_1i(do_mfthi, cpu_T[0], cpu_T[0], 0);
5282
            break;
5283
        case 2:
5284
            tcg_gen_helper_1_1i(do_mftacx, cpu_T[0], cpu_T[0], 0);
5285
            break;
5286
        case 4:
5287
            tcg_gen_helper_1_1i(do_mftlo, cpu_T[0], cpu_T[0], 1);
5288
            break;
5289
        case 5:
5290
            tcg_gen_helper_1_1i(do_mfthi, cpu_T[0], cpu_T[0], 1);
5291
            break;
5292
        case 6:
5293
            tcg_gen_helper_1_1i(do_mftacx, cpu_T[0], cpu_T[0], 1);
5294
            break;
5295
        case 8:
5296
            tcg_gen_helper_1_1i(do_mftlo, cpu_T[0], cpu_T[0], 2);
5297
            break;
5298
        case 9:
5299
            tcg_gen_helper_1_1i(do_mfthi, cpu_T[0], cpu_T[0], 2);
5300
            break;
5301
        case 10:
5302
            tcg_gen_helper_1_1i(do_mftacx, cpu_T[0], cpu_T[0], 2);
5303
            break;
5304
        case 12:
5305
            tcg_gen_helper_1_1i(do_mftlo, cpu_T[0], cpu_T[0], 3);
5306
            break;
5307
        case 13:
5308
            tcg_gen_helper_1_1i(do_mfthi, cpu_T[0], cpu_T[0], 3);
5309
            break;
5310
        case 14:
5311
            tcg_gen_helper_1_1i(do_mftacx, cpu_T[0], cpu_T[0], 3);
5312
            break;
5313
        case 16:
5314
            tcg_gen_helper_1_1(do_mftdsp, cpu_T[0], cpu_T[0]);
5315
            break;
5316
        default:
5317
            goto die;
5318
        }
5319
        break;
5320
    /* Floating point (COP1). */
5321
    case 2:
5322
        /* XXX: For now we support only a single FPU context. */
5323
        if (h == 0) {
5324
            gen_load_fpr32(fpu32_T[0], rt);
5325
            tcg_gen_ext_i32_tl(cpu_T[0], fpu32_T[0]);
5326
        } else {
5327
            gen_load_fpr32h(fpu32h_T[0], rt);
5328
            tcg_gen_ext_i32_tl(cpu_T[0], fpu32h_T[0]);
5329
        }
5330
        break;
5331
    case 3:
5332
        /* XXX: For now we support only a single FPU context. */
5333
        tcg_gen_helper_1_1i(do_cfc1, cpu_T[0], cpu_T[0], rt);
5334
        break;
5335
    /* COP2: Not implemented. */
5336
    case 4:
5337
    case 5:
5338
        /* fall through */
5339
    default:
5340
        goto die;
5341
    }
5342
#if defined MIPS_DEBUG_DISAS
5343
    if (loglevel & CPU_LOG_TB_IN_ASM) {
5344
        fprintf(logfile, "mftr (reg %d u %d sel %d h %d)\n",
5345
                rt, u, sel, h);
5346
    }
5347
#endif
5348
    gen_store_gpr(cpu_T[0], rd);
5349
    return;
5350

    
5351
die:
5352
#if defined MIPS_DEBUG_DISAS
5353
    if (loglevel & CPU_LOG_TB_IN_ASM) {
5354
        fprintf(logfile, "mftr (reg %d u %d sel %d h %d)\n",
5355
                rt, u, sel, h);
5356
    }
5357
#endif
5358
    generate_exception(ctx, EXCP_RI);
5359
}
5360

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

    
5366
    gen_load_gpr(cpu_T[0], rt);
5367
    if ((env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP)) == 0 &&
5368
        ((env->CP0_TCBind[other_tc] & (0xf << CP0TCBd_CurVPE)) !=
5369
         (env->CP0_TCBind[env->current_tc] & (0xf << CP0TCBd_CurVPE))))
5370
        /* NOP */ ;
5371
    else if ((env->CP0_VPEControl & (0xff << CP0VPECo_TargTC)) >
5372
             (env->mvp->CP0_MVPConf0 & (0xff << CP0MVPC0_PTC)))
5373
        /* NOP */ ;
5374
    else if (u == 0) {
5375
        switch (rd) {
5376
        case 2:
5377
            switch (sel) {
5378
            case 1:
5379
                tcg_gen_helper_0_1(do_mttc0_tcstatus, cpu_T[0]);
5380
                break;
5381
            case 2:
5382
                tcg_gen_helper_0_1(do_mttc0_tcbind, cpu_T[0]);
5383
                break;
5384
            case 3:
5385
                tcg_gen_helper_0_1(do_mttc0_tcrestart, cpu_T[0]);
5386
                break;
5387
            case 4:
5388
                tcg_gen_helper_0_1(do_mttc0_tchalt, cpu_T[0]);
5389
                break;
5390
            case 5:
5391
                tcg_gen_helper_0_1(do_mttc0_tccontext, cpu_T[0]);
5392
                break;
5393
            case 6:
5394
                tcg_gen_helper_0_1(do_mttc0_tcschedule, cpu_T[0]);
5395
                break;
5396
            case 7:
5397
                tcg_gen_helper_0_1(do_mttc0_tcschefback, cpu_T[0]);
5398
                break;
5399
            default:
5400
                gen_mtc0(env, ctx, rd, sel);
5401
                break;
5402
            }
5403
            break;
5404
        case 10:
5405
            switch (sel) {
5406
            case 0:
5407
                tcg_gen_helper_0_1(do_mttc0_entryhi, cpu_T[0]);
5408
                break;
5409
            default:
5410
                gen_mtc0(env, ctx, rd, sel);
5411
                break;
5412
            }
5413
        case 12:
5414
            switch (sel) {
5415
            case 0:
5416
                tcg_gen_helper_0_1(do_mttc0_status, cpu_T[0]);
5417
                break;
5418
            default:
5419
                gen_mtc0(env, ctx, rd, sel);
5420
                break;
5421
            }
5422
        case 23:
5423
            switch (sel) {
5424
            case 0:
5425
                tcg_gen_helper_0_1(do_mttc0_debug, cpu_T[0]);
5426
                break;
5427
            default:
5428
                gen_mtc0(env, ctx, rd, sel);
5429
                break;
5430
            }
5431
            break;
5432
        default:
5433
            gen_mtc0(env, ctx, rd, sel);
5434
        }
5435
    } else switch (sel) {
5436
    /* GPR registers. */
5437
    case 0:
5438
        tcg_gen_helper_0_1i(do_mttgpr, cpu_T[0], rd);
5439
        break;
5440
    /* Auxiliary CPU registers */
5441
    case 1:
5442
        switch (rd) {
5443
        case 0:
5444
            tcg_gen_helper_0_1i(do_mttlo, cpu_T[0], 0);
5445
            break;
5446
        case 1:
5447
            tcg_gen_helper_0_1i(do_mtthi, cpu_T[0], 0);
5448
            break;
5449
        case 2:
5450
            tcg_gen_helper_0_1i(do_mttacx, cpu_T[0], 0);
5451
            break;
5452
        case 4:
5453
            tcg_gen_helper_0_1i(do_mttlo, cpu_T[0], 1);
5454
            break;
5455
        case 5:
5456
            tcg_gen_helper_0_1i(do_mtthi, cpu_T[0], 1);
5457
            break;
5458
        case 6:
5459
            tcg_gen_helper_0_1i(do_mttacx, cpu_T[0], 1);
5460
            break;
5461
        case 8:
5462
            tcg_gen_helper_0_1i(do_mttlo, cpu_T[0], 2);
5463
            break;
5464
        case 9:
5465
            tcg_gen_helper_0_1i(do_mtthi, cpu_T[0], 2);
5466
            break;
5467
        case 10:
5468
            tcg_gen_helper_0_1i(do_mttacx, cpu_T[0], 2);
5469
            break;
5470
        case 12:
5471
            tcg_gen_helper_0_1i(do_mttlo, cpu_T[0], 3);
5472
            break;
5473
        case 13:
5474
            tcg_gen_helper_0_1i(do_mtthi, cpu_T[0], 3);
5475
            break;
5476
        case 14:
5477
            tcg_gen_helper_0_1i(do_mttacx, cpu_T[0], 3);
5478
            break;
5479
        case 16:
5480
            tcg_gen_helper_0_1(do_mttdsp, cpu_T[0]);
5481
            break;
5482
        default:
5483
            goto die;
5484
        }
5485
        break;
5486
    /* Floating point (COP1). */
5487
    case 2:
5488
        /* XXX: For now we support only a single FPU context. */
5489
        if (h == 0) {
5490
            tcg_gen_trunc_tl_i32(fpu32_T[0], cpu_T[0]);
5491
            gen_store_fpr32(fpu32_T[0], rd);
5492
        } else {
5493
            tcg_gen_trunc_tl_i32(fpu32h_T[0], cpu_T[0]);
5494
            gen_store_fpr32h(fpu32h_T[0], rd);
5495
        }
5496
        break;
5497
    case 3:
5498
        /* XXX: For now we support only a single FPU context. */
5499
        tcg_gen_helper_0_1i(do_ctc1, cpu_T[0], rd);
5500
        break;
5501
    /* COP2: Not implemented. */
5502
    case 4:
5503
    case 5:
5504
        /* fall through */
5505
    default:
5506
        goto die;
5507
    }
5508
#if defined MIPS_DEBUG_DISAS
5509
    if (loglevel & CPU_LOG_TB_IN_ASM) {
5510
        fprintf(logfile, "mttr (reg %d u %d sel %d h %d)\n",
5511
                rd, u, sel, h);
5512
    }
5513
#endif
5514
    return;
5515

    
5516
die:
5517
#if defined MIPS_DEBUG_DISAS
5518
    if (loglevel & CPU_LOG_TB_IN_ASM) {
5519
        fprintf(logfile, "mttr (reg %d u %d sel %d h %d)\n",
5520
                rd, u, sel, h);
5521
    }
5522
#endif
5523
    generate_exception(ctx, EXCP_RI);
5524
}
5525

    
5526
static void gen_cp0 (CPUState *env, DisasContext *ctx, uint32_t opc, int rt, int rd)
5527
{
5528
    const char *opn = "ldst";
5529

    
5530
    switch (opc) {
5531
    case OPC_MFC0:
5532
        if (rt == 0) {
5533
            /* Treat as NOP. */
5534
            return;
5535
        }
5536
        gen_mfc0(env, ctx, rd, ctx->opcode & 0x7);
5537
        gen_store_gpr(cpu_T[0], rt);
5538
        opn = "mfc0";
5539
        break;
5540
    case OPC_MTC0:
5541
        gen_load_gpr(cpu_T[0], rt);
5542
        save_cpu_state(ctx, 1);
5543
        gen_mtc0(env, ctx, rd, ctx->opcode & 0x7);
5544
        opn = "mtc0";
5545
        break;
5546
#if defined(TARGET_MIPS64)
5547
    case OPC_DMFC0:
5548
        check_insn(env, ctx, ISA_MIPS3);
5549
        if (rt == 0) {
5550
            /* Treat as NOP. */
5551
            return;
5552
        }
5553
        gen_dmfc0(env, ctx, rd, ctx->opcode & 0x7);
5554
        gen_store_gpr(cpu_T[0], rt);
5555
        opn = "dmfc0";
5556
        break;
5557
    case OPC_DMTC0:
5558
        check_insn(env, ctx, ISA_MIPS3);
5559
        gen_load_gpr(cpu_T[0], rt);
5560
        save_cpu_state(ctx, 1);
5561
        gen_dmtc0(env, ctx, rd, ctx->opcode & 0x7);
5562
        opn = "dmtc0";
5563
        break;
5564
#endif
5565
    case OPC_MFTR:
5566
        check_insn(env, ctx, ASE_MT);
5567
        if (rd == 0) {
5568
            /* Treat as NOP. */
5569
            return;
5570
        }
5571
        gen_mftr(env, ctx, rt, rd, (ctx->opcode >> 5) & 1,
5572
                 ctx->opcode & 0x7, (ctx->opcode >> 4) & 1);
5573
        opn = "mftr";
5574
        break;
5575
    case OPC_MTTR:
5576
        check_insn(env, ctx, ASE_MT);
5577
        gen_mttr(env, ctx, rd, rt, (ctx->opcode >> 5) & 1,
5578
                 ctx->opcode & 0x7, (ctx->opcode >> 4) & 1);
5579
        opn = "mttr";
5580
        break;
5581
    case OPC_TLBWI:
5582
        opn = "tlbwi";
5583
        if (!env->tlb->do_tlbwi)
5584
            goto die;
5585
        tcg_gen_helper_0_0(env->tlb->do_tlbwi);
5586
        break;
5587
    case OPC_TLBWR:
5588
        opn = "tlbwr";
5589
        if (!env->tlb->do_tlbwr)
5590
            goto die;
5591
        tcg_gen_helper_0_0(env->tlb->do_tlbwr);
5592
        break;
5593
    case OPC_TLBP:
5594
        opn = "tlbp";
5595
        if (!env->tlb->do_tlbp)
5596
            goto die;
5597
        tcg_gen_helper_0_0(env->tlb->do_tlbp);
5598
        break;
5599
    case OPC_TLBR:
5600
        opn = "tlbr";
5601
        if (!env->tlb->do_tlbr)
5602
            goto die;
5603
        tcg_gen_helper_0_0(env->tlb->do_tlbr);
5604
        break;
5605
    case OPC_ERET:
5606
        opn = "eret";
5607
        check_insn(env, ctx, ISA_MIPS2);
5608
        save_cpu_state(ctx, 1);
5609
        tcg_gen_helper_0_0(do_eret);
5610
        ctx->bstate = BS_EXCP;
5611
        break;
5612
    case OPC_DERET:
5613
        opn = "deret";
5614
        check_insn(env, ctx, ISA_MIPS32);
5615
        if (!(ctx->hflags & MIPS_HFLAG_DM)) {
5616
            MIPS_INVAL(opn);
5617
            generate_exception(ctx, EXCP_RI);
5618
        } else {
5619
            save_cpu_state(ctx, 1);
5620
            tcg_gen_helper_0_0(do_deret);
5621
            ctx->bstate = BS_EXCP;
5622
        }
5623
        break;
5624
    case OPC_WAIT:
5625
        opn = "wait";
5626
        check_insn(env, ctx, ISA_MIPS3 | ISA_MIPS32);
5627
        /* If we get an exception, we want to restart at next instruction */
5628
        ctx->pc += 4;
5629
        save_cpu_state(ctx, 1);
5630
        ctx->pc -= 4;
5631
        tcg_gen_helper_0_0(do_wait);
5632
        ctx->bstate = BS_EXCP;
5633
        break;
5634
    default:
5635
 die:
5636
        MIPS_INVAL(opn);
5637
        generate_exception(ctx, EXCP_RI);
5638
        return;
5639
    }
5640
    MIPS_DEBUG("%s %s %d", opn, regnames[rt], rd);
5641
}
5642
#endif /* !CONFIG_USER_ONLY */
5643

    
5644
/* CP1 Branches (before delay slot) */
5645
static void gen_compute_branch1 (CPUState *env, DisasContext *ctx, uint32_t op,
5646
                                 int32_t cc, int32_t offset)
5647
{
5648
    target_ulong btarget;
5649
    const char *opn = "cp1 cond branch";
5650
    TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
5651
    TCGv t1 = tcg_temp_local_new(TCG_TYPE_TL);
5652

    
5653
    if (cc != 0)
5654
        check_insn(env, ctx, ISA_MIPS4 | ISA_MIPS32);
5655

    
5656
    btarget = ctx->pc + 4 + offset;
5657

    
5658
    switch (op) {
5659
    case OPC_BC1F:
5660
        {
5661
            int l1 = gen_new_label();
5662
            int l2 = gen_new_label();
5663
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
5664

    
5665
            get_fp_cond(r_tmp1);
5666
            tcg_gen_ext_i32_tl(t0, r_tmp1);
5667
            tcg_temp_free(r_tmp1);
5668
            tcg_gen_not_tl(t0, t0);
5669
            tcg_gen_movi_tl(t1, 0x1 << cc);
5670
            tcg_gen_and_tl(t0, t0, t1);
5671
            tcg_gen_brcondi_tl(TCG_COND_NE, t0, 0, l1);
5672
            tcg_gen_movi_tl(t0, 0);
5673
            tcg_gen_br(l2);
5674
            gen_set_label(l1);
5675
            tcg_gen_movi_tl(t0, 1);
5676
            gen_set_label(l2);
5677
        }
5678
        opn = "bc1f";
5679
        goto not_likely;
5680
    case OPC_BC1FL:
5681
        {
5682
            int l1 = gen_new_label();
5683
            int l2 = gen_new_label();
5684
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
5685

    
5686
            get_fp_cond(r_tmp1);
5687
            tcg_gen_ext_i32_tl(t0, r_tmp1);
5688
            tcg_temp_free(r_tmp1);
5689
            tcg_gen_not_tl(t0, t0);
5690
            tcg_gen_movi_tl(t1, 0x1 << cc);
5691
            tcg_gen_and_tl(t0, t0, t1);
5692
            tcg_gen_brcondi_tl(TCG_COND_NE, t0, 0, l1);
5693
            tcg_gen_movi_tl(t0, 0);
5694
            tcg_gen_br(l2);
5695
            gen_set_label(l1);
5696
            tcg_gen_movi_tl(t0, 1);
5697
            gen_set_label(l2);
5698
        }
5699
        opn = "bc1fl";
5700
        goto likely;
5701
    case OPC_BC1T:
5702
        {
5703
            int l1 = gen_new_label();
5704
            int l2 = gen_new_label();
5705
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
5706

    
5707
            get_fp_cond(r_tmp1);
5708
            tcg_gen_ext_i32_tl(t0, r_tmp1);
5709
            tcg_temp_free(r_tmp1);
5710
            tcg_gen_movi_tl(t1, 0x1 << cc);
5711
            tcg_gen_and_tl(t0, t0, t1);
5712
            tcg_gen_brcondi_tl(TCG_COND_NE, t0, 0, l1);
5713
            tcg_gen_movi_tl(t0, 0);
5714
            tcg_gen_br(l2);
5715
            gen_set_label(l1);
5716
            tcg_gen_movi_tl(t0, 1);
5717
            gen_set_label(l2);
5718
        }
5719
        opn = "bc1t";
5720
        goto not_likely;
5721
    case OPC_BC1TL:
5722
        {
5723
            int l1 = gen_new_label();
5724
            int l2 = gen_new_label();
5725
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
5726

    
5727
            get_fp_cond(r_tmp1);
5728
            tcg_gen_ext_i32_tl(t0, r_tmp1);
5729
            tcg_temp_free(r_tmp1);
5730
            tcg_gen_movi_tl(t1, 0x1 << cc);
5731
            tcg_gen_and_tl(t0, t0, t1);
5732
            tcg_gen_brcondi_tl(TCG_COND_NE, t0, 0, l1);
5733
            tcg_gen_movi_tl(t0, 0);
5734
            tcg_gen_br(l2);
5735
            gen_set_label(l1);
5736
            tcg_gen_movi_tl(t0, 1);
5737
            gen_set_label(l2);
5738
        }
5739
        opn = "bc1tl";
5740
    likely:
5741
        ctx->hflags |= MIPS_HFLAG_BL;
5742
        tcg_gen_st_tl(t0, cpu_env, offsetof(CPUState, bcond));
5743
        break;
5744
    case OPC_BC1FANY2:
5745
        {
5746
            int l1 = gen_new_label();
5747
            int l2 = gen_new_label();
5748
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
5749

    
5750
            get_fp_cond(r_tmp1);
5751
            tcg_gen_ext_i32_tl(t0, r_tmp1);
5752
            tcg_temp_free(r_tmp1);
5753
            tcg_gen_not_tl(t0, t0);
5754
            tcg_gen_movi_tl(t1, 0x3 << cc);
5755
            tcg_gen_and_tl(t0, t0, t1);
5756
            tcg_gen_brcondi_tl(TCG_COND_NE, t0, 0, l1);
5757
            tcg_gen_movi_tl(t0, 0);
5758
            tcg_gen_br(l2);
5759
            gen_set_label(l1);
5760
            tcg_gen_movi_tl(t0, 1);
5761
            gen_set_label(l2);
5762
        }
5763
        opn = "bc1any2f";
5764
        goto not_likely;
5765
    case OPC_BC1TANY2:
5766
        {
5767
            int l1 = gen_new_label();
5768
            int l2 = gen_new_label();
5769
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
5770

    
5771
            get_fp_cond(r_tmp1);
5772
            tcg_gen_ext_i32_tl(t0, r_tmp1);
5773
            tcg_temp_free(r_tmp1);
5774
            tcg_gen_movi_tl(t1, 0x3 << cc);
5775
            tcg_gen_and_tl(t0, t0, t1);
5776
            tcg_gen_brcondi_tl(TCG_COND_NE, t0, 0, l1);
5777
            tcg_gen_movi_tl(t0, 0);
5778
            tcg_gen_br(l2);
5779
            gen_set_label(l1);
5780
            tcg_gen_movi_tl(t0, 1);
5781
            gen_set_label(l2);
5782
        }
5783
        opn = "bc1any2t";
5784
        goto not_likely;
5785
    case OPC_BC1FANY4:
5786
        {
5787
            int l1 = gen_new_label();
5788
            int l2 = gen_new_label();
5789
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
5790

    
5791
            get_fp_cond(r_tmp1);
5792
            tcg_gen_ext_i32_tl(t0, r_tmp1);
5793
            tcg_temp_free(r_tmp1);
5794
            tcg_gen_not_tl(t0, t0);
5795
            tcg_gen_movi_tl(t1, 0xf << cc);
5796
            tcg_gen_and_tl(t0, t0, t1);
5797
            tcg_gen_brcondi_tl(TCG_COND_NE, t0, 0, l1);
5798
            tcg_gen_movi_tl(t0, 0);
5799
            tcg_gen_br(l2);
5800
            gen_set_label(l1);
5801
            tcg_gen_movi_tl(t0, 1);
5802
            gen_set_label(l2);
5803
        }
5804
        opn = "bc1any4f";
5805
        goto not_likely;
5806
    case OPC_BC1TANY4:
5807
        {
5808
            int l1 = gen_new_label();
5809
            int l2 = gen_new_label();
5810
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
5811

    
5812
            get_fp_cond(r_tmp1);
5813
            tcg_gen_ext_i32_tl(t0, r_tmp1);
5814
            tcg_temp_free(r_tmp1);
5815
            tcg_gen_movi_tl(t1, 0xf << cc);
5816
            tcg_gen_and_tl(t0, t0, t1);
5817
            tcg_gen_brcondi_tl(TCG_COND_NE, t0, 0, l1);
5818
            tcg_gen_movi_tl(t0, 0);
5819
            tcg_gen_br(l2);
5820
            gen_set_label(l1);
5821
            tcg_gen_movi_tl(t0, 1);
5822
            gen_set_label(l2);
5823
        }
5824
        opn = "bc1any4t";
5825
    not_likely:
5826
        ctx->hflags |= MIPS_HFLAG_BC;
5827
        tcg_gen_st_tl(t0, cpu_env, offsetof(CPUState, bcond));
5828
        break;
5829
    default:
5830
        MIPS_INVAL(opn);
5831
        generate_exception (ctx, EXCP_RI);
5832
        goto out;
5833
    }
5834
    MIPS_DEBUG("%s: cond %02x target " TARGET_FMT_lx, opn,
5835
               ctx->hflags, btarget);
5836
    ctx->btarget = btarget;
5837

    
5838
 out:
5839
    tcg_temp_free(t0);
5840
    tcg_temp_free(t1);
5841
}
5842

    
5843
/* Coprocessor 1 (FPU) */
5844

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

    
5847
static void gen_cp1 (DisasContext *ctx, uint32_t opc, int rt, int fs)
5848
{
5849
    const char *opn = "cp1 move";
5850
    TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
5851

    
5852
    switch (opc) {
5853
    case OPC_MFC1:
5854
        gen_load_fpr32(fpu32_T[0], fs);
5855
        tcg_gen_ext_i32_tl(t0, fpu32_T[0]);
5856
        gen_store_gpr(t0, rt);
5857
        opn = "mfc1";
5858
        break;
5859
    case OPC_MTC1:
5860
        gen_load_gpr(t0, rt);
5861
        tcg_gen_trunc_tl_i32(fpu32_T[0], t0);
5862
        gen_store_fpr32(fpu32_T[0], fs);
5863
        opn = "mtc1";
5864
        break;
5865
    case OPC_CFC1:
5866
        tcg_gen_helper_1_i(do_cfc1, t0, fs);
5867
        gen_store_gpr(t0, rt);
5868
        opn = "cfc1";
5869
        break;
5870
    case OPC_CTC1:
5871
        gen_load_gpr(t0, rt);
5872
        tcg_gen_helper_0_1i(do_ctc1, t0, fs);
5873
        opn = "ctc1";
5874
        break;
5875
    case OPC_DMFC1:
5876
        gen_load_fpr64(ctx, fpu64_T[0], fs);
5877
        tcg_gen_mov_tl(t0, fpu64_T[0]);
5878
        gen_store_gpr(t0, rt);
5879
        opn = "dmfc1";
5880
        break;
5881
    case OPC_DMTC1:
5882
        gen_load_gpr(t0, rt);
5883
        tcg_gen_mov_tl(fpu64_T[0], t0);
5884
        gen_store_fpr64(ctx, fpu64_T[0], fs);
5885
        opn = "dmtc1";
5886
        break;
5887
    case OPC_MFHC1:
5888
        gen_load_fpr32h(fpu32h_T[0], fs);
5889
        tcg_gen_ext_i32_tl(t0, fpu32h_T[0]);
5890
        gen_store_gpr(t0, rt);
5891
        opn = "mfhc1";
5892
        break;
5893
    case OPC_MTHC1:
5894
        gen_load_gpr(t0, rt);
5895
        tcg_gen_trunc_tl_i32(fpu32h_T[0], t0);
5896
        gen_store_fpr32h(fpu32h_T[0], fs);
5897
        opn = "mthc1";
5898
        break;
5899
    default:
5900
        MIPS_INVAL(opn);
5901
        generate_exception (ctx, EXCP_RI);
5902
        goto out;
5903
    }
5904
    MIPS_DEBUG("%s %s %s", opn, regnames[rt], fregnames[fs]);
5905

    
5906
 out:
5907
    tcg_temp_free(t0);
5908
}
5909

    
5910
static void gen_movci (DisasContext *ctx, int rd, int rs, int cc, int tf)
5911
{
5912
    int l1 = gen_new_label();
5913
    uint32_t ccbit;
5914
    TCGCond cond;
5915
    TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
5916
    TCGv t1 = tcg_temp_local_new(TCG_TYPE_TL);
5917

    
5918
    if (cc)
5919
        ccbit = 1 << (24 + cc);
5920
    else
5921
        ccbit = 1 << 23;
5922
    if (tf)
5923
        cond = TCG_COND_EQ;
5924
    else
5925
        cond = TCG_COND_NE;
5926

    
5927
    gen_load_gpr(t0, rd);
5928
    gen_load_gpr(t1, rs);
5929
    {
5930
        TCGv r_ptr = tcg_temp_new(TCG_TYPE_PTR);
5931
        TCGv r_tmp = tcg_temp_local_new(TCG_TYPE_I32);
5932

    
5933
        tcg_gen_ld_ptr(r_ptr, cpu_env, offsetof(CPUState, fpu));
5934
        tcg_gen_ld_i32(r_tmp, r_ptr, offsetof(CPUMIPSFPUContext, fcr31));
5935
        tcg_temp_free(r_ptr);
5936
        tcg_gen_andi_i32(r_tmp, r_tmp, ccbit);
5937
        tcg_gen_brcondi_i32(cond, r_tmp, 0, l1);
5938
        tcg_temp_free(r_tmp);
5939
    }
5940
    tcg_gen_mov_tl(t0, t1);
5941
    tcg_temp_free(t1);
5942

    
5943
    gen_set_label(l1);
5944
    gen_store_gpr(t0, rd);
5945
    tcg_temp_free(t0);
5946
}
5947

    
5948
static inline void gen_movcf_s (int cc, int tf)
5949
{
5950
    uint32_t ccbit;
5951
    int cond;
5952
    TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
5953
    int l1 = gen_new_label();
5954

    
5955
    if (cc)
5956
        ccbit = 1 << (24 + cc);
5957
    else
5958
        ccbit = 1 << 23;
5959

    
5960
    if (tf)
5961
        cond = TCG_COND_EQ;
5962
    else
5963
        cond = TCG_COND_NE;
5964

    
5965
    tcg_gen_ld_i32(r_tmp1, current_fpu, offsetof(CPUMIPSFPUContext, fcr31));
5966
    tcg_gen_andi_i32(r_tmp1, r_tmp1, ccbit);
5967
    tcg_gen_brcondi_i32(cond, r_tmp1, 0, l1);
5968
    tcg_gen_movi_i32(fpu32_T[2], fpu32_T[0]);
5969
    gen_set_label(l1);
5970
    tcg_temp_free(r_tmp1);
5971
}
5972

    
5973
static inline void gen_movcf_d (int cc, int tf)
5974
{
5975
    uint32_t ccbit;
5976
    int cond;
5977
    TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
5978
    int l1 = gen_new_label();
5979

    
5980
    if (cc)
5981
        ccbit = 1 << (24 + cc);
5982
    else
5983
        ccbit = 1 << 23;
5984

    
5985
    if (tf)
5986
        cond = TCG_COND_EQ;
5987
    else
5988
        cond = TCG_COND_NE;
5989

    
5990
    tcg_gen_ld_i32(r_tmp1, current_fpu, offsetof(CPUMIPSFPUContext, fcr31));
5991
    tcg_gen_andi_i32(r_tmp1, r_tmp1, ccbit);
5992
    tcg_gen_brcondi_i32(cond, r_tmp1, 0, l1);
5993
    tcg_gen_movi_i64(fpu64_T[2], fpu64_T[0]);
5994
    gen_set_label(l1);
5995
    tcg_temp_free(r_tmp1);
5996
}
5997

    
5998
static inline void gen_movcf_ps (int cc, int tf)
5999
{
6000
    int cond;
6001
    TCGv r_tmp1 = tcg_temp_local_new(TCG_TYPE_I32);
6002
    TCGv r_tmp2 = tcg_temp_local_new(TCG_TYPE_I32);
6003
    int l1 = gen_new_label();
6004
    int l2 = gen_new_label();
6005

    
6006
    if (tf)
6007
        cond = TCG_COND_EQ;
6008
    else
6009
        cond = TCG_COND_NE;
6010

    
6011
    get_fp_cond(r_tmp1);
6012
    tcg_gen_shri_i32(r_tmp1, r_tmp1, cc);
6013
    tcg_gen_andi_i32(r_tmp2, r_tmp1, 0x1);
6014
    tcg_gen_brcondi_i32(cond, r_tmp2, 0, l1);
6015
    tcg_gen_movi_i32(fpu32_T[2], fpu32_T[0]);
6016
    gen_set_label(l1);
6017
    tcg_gen_andi_i32(r_tmp2, r_tmp1, 0x2);
6018
    tcg_gen_brcondi_i32(cond, r_tmp2, 0, l2);
6019
    tcg_gen_movi_i32(fpu32h_T[2], fpu32h_T[0]);
6020
    gen_set_label(l2);
6021
    tcg_temp_free(r_tmp1);
6022
    tcg_temp_free(r_tmp2);
6023
}
6024

    
6025

    
6026
static void gen_farith (DisasContext *ctx, uint32_t op1,
6027
                        int ft, int fs, int fd, int cc)
6028
{
6029
    const char *opn = "farith";
6030
    const char *condnames[] = {
6031
            "c.f",
6032
            "c.un",
6033
            "c.eq",
6034
            "c.ueq",
6035
            "c.olt",
6036
            "c.ult",
6037
            "c.ole",
6038
            "c.ule",
6039
            "c.sf",
6040
            "c.ngle",
6041
            "c.seq",
6042
            "c.ngl",
6043
            "c.lt",
6044
            "c.nge",
6045
            "c.le",
6046
            "c.ngt",
6047
    };
6048
    const char *condnames_abs[] = {
6049
            "cabs.f",
6050
            "cabs.un",
6051
            "cabs.eq",
6052
            "cabs.ueq",
6053
            "cabs.olt",
6054
            "cabs.ult",
6055
            "cabs.ole",
6056
            "cabs.ule",
6057
            "cabs.sf",
6058
            "cabs.ngle",
6059
            "cabs.seq",
6060
            "cabs.ngl",
6061
            "cabs.lt",
6062
            "cabs.nge",
6063
            "cabs.le",
6064
            "cabs.ngt",
6065
    };
6066
    enum { BINOP, CMPOP, OTHEROP } optype = OTHEROP;
6067
    uint32_t func = ctx->opcode & 0x3f;
6068

    
6069
    switch (ctx->opcode & FOP(0x3f, 0x1f)) {
6070
    case FOP(0, 16):
6071
        gen_load_fpr32(fpu32_T[0], fs);
6072
        gen_load_fpr32(fpu32_T[1], ft);
6073
        tcg_gen_helper_0_0(do_float_add_s);
6074
        gen_store_fpr32(fpu32_T[2], fd);
6075
        opn = "add.s";
6076
        optype = BINOP;
6077
        break;
6078
    case FOP(1, 16):
6079
        gen_load_fpr32(fpu32_T[0], fs);
6080
        gen_load_fpr32(fpu32_T[1], ft);
6081
        tcg_gen_helper_0_0(do_float_sub_s);
6082
        gen_store_fpr32(fpu32_T[2], fd);
6083
        opn = "sub.s";
6084
        optype = BINOP;
6085
        break;
6086
    case FOP(2, 16):
6087
        gen_load_fpr32(fpu32_T[0], fs);
6088
        gen_load_fpr32(fpu32_T[1], ft);
6089
        tcg_gen_helper_0_0(do_float_mul_s);
6090
        gen_store_fpr32(fpu32_T[2], fd);
6091
        opn = "mul.s";
6092
        optype = BINOP;
6093
        break;
6094
    case FOP(3, 16):
6095
        gen_load_fpr32(fpu32_T[0], fs);
6096
        gen_load_fpr32(fpu32_T[1], ft);
6097
        tcg_gen_helper_0_0(do_float_div_s);
6098
        gen_store_fpr32(fpu32_T[2], fd);
6099
        opn = "div.s";
6100
        optype = BINOP;
6101
        break;
6102
    case FOP(4, 16):
6103
        gen_load_fpr32(fpu32_T[0], fs);
6104
        tcg_gen_helper_0_0(do_float_sqrt_s);
6105
        gen_store_fpr32(fpu32_T[2], fd);
6106
        opn = "sqrt.s";
6107
        break;
6108
    case FOP(5, 16):
6109
        gen_load_fpr32(fpu32_T[0], fs);
6110
        tcg_gen_helper_0_0(do_float_abs_s);
6111
        gen_store_fpr32(fpu32_T[2], fd);
6112
        opn = "abs.s";
6113
        break;
6114
    case FOP(6, 16):
6115
        gen_load_fpr32(fpu32_T[0], fs);
6116
        gen_store_fpr32(fpu32_T[0], fd);
6117
        opn = "mov.s";
6118
        break;
6119
    case FOP(7, 16):
6120
        gen_load_fpr32(fpu32_T[0], fs);
6121
        tcg_gen_helper_0_0(do_float_chs_s);
6122
        gen_store_fpr32(fpu32_T[2], fd);
6123
        opn = "neg.s";
6124
        break;
6125
    case FOP(8, 16):
6126
        check_cp1_64bitmode(ctx);
6127
        gen_load_fpr32(fpu32_T[0], fs);
6128
        tcg_gen_helper_0_0(do_float_roundl_s);
6129
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6130
        opn = "round.l.s";
6131
        break;
6132
    case FOP(9, 16):
6133
        check_cp1_64bitmode(ctx);
6134
        gen_load_fpr32(fpu32_T[0], fs);
6135
        tcg_gen_helper_0_0(do_float_truncl_s);
6136
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6137
        opn = "trunc.l.s";
6138
        break;
6139
    case FOP(10, 16):
6140
        check_cp1_64bitmode(ctx);
6141
        gen_load_fpr32(fpu32_T[0], fs);
6142
        tcg_gen_helper_0_0(do_float_ceill_s);
6143
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6144
        opn = "ceil.l.s";
6145
        break;
6146
    case FOP(11, 16):
6147
        check_cp1_64bitmode(ctx);
6148
        gen_load_fpr32(fpu32_T[0], fs);
6149
        tcg_gen_helper_0_0(do_float_floorl_s);
6150
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6151
        opn = "floor.l.s";
6152
        break;
6153
    case FOP(12, 16):
6154
        gen_load_fpr32(fpu32_T[0], fs);
6155
        tcg_gen_helper_0_0(do_float_roundw_s);
6156
        gen_store_fpr32(fpu32_T[2], fd);
6157
        opn = "round.w.s";
6158
        break;
6159
    case FOP(13, 16):
6160
        gen_load_fpr32(fpu32_T[0], fs);
6161
        tcg_gen_helper_0_0(do_float_truncw_s);
6162
        gen_store_fpr32(fpu32_T[2], fd);
6163
        opn = "trunc.w.s";
6164
        break;
6165
    case FOP(14, 16):
6166
        gen_load_fpr32(fpu32_T[0], fs);
6167
        tcg_gen_helper_0_0(do_float_ceilw_s);
6168
        gen_store_fpr32(fpu32_T[2], fd);
6169
        opn = "ceil.w.s";
6170
        break;
6171
    case FOP(15, 16):
6172
        gen_load_fpr32(fpu32_T[0], fs);
6173
        tcg_gen_helper_0_0(do_float_floorw_s);
6174
        gen_store_fpr32(fpu32_T[2], fd);
6175
        opn = "floor.w.s";
6176
        break;
6177
    case FOP(17, 16):
6178
        gen_load_fpr32(fpu32_T[0], fs);
6179
        gen_load_fpr32(fpu32_T[2], fd);
6180
        gen_movcf_s((ft >> 2) & 0x7, ft & 0x1);
6181
        gen_store_fpr32(fpu32_T[2], fd);
6182
        opn = "movcf.s";
6183
        break;
6184
    case FOP(18, 16):
6185
        gen_load_fpr32(fpu32_T[0], fs);
6186
        gen_load_fpr32(fpu32_T[2], fd);
6187
        {
6188
            int l1 = gen_new_label();
6189
            TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
6190

    
6191
            gen_load_gpr(t0, ft);
6192
            tcg_gen_brcondi_tl(TCG_COND_NE, t0, 0, l1);
6193
            tcg_temp_free(t0);
6194
            tcg_gen_mov_i32(fpu32_T[2], fpu32_T[0]);
6195
            gen_set_label(l1);
6196
        }
6197
        gen_store_fpr32(fpu32_T[2], fd);
6198
        opn = "movz.s";
6199
        break;
6200
    case FOP(19, 16):
6201
        gen_load_fpr32(fpu32_T[0], fs);
6202
        gen_load_fpr32(fpu32_T[2], fd);
6203
        {
6204
            int l1 = gen_new_label();
6205
            TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
6206

    
6207
            gen_load_gpr(t0, ft);
6208
            tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
6209
            tcg_temp_free(t0);
6210
            tcg_gen_mov_i32(fpu32_T[2], fpu32_T[0]);
6211
            gen_set_label(l1);
6212
        }
6213
        gen_store_fpr32(fpu32_T[2], fd);
6214
        opn = "movn.s";
6215
        break;
6216
    case FOP(21, 16):
6217
        check_cop1x(ctx);
6218
        gen_load_fpr32(fpu32_T[0], fs);
6219
        tcg_gen_helper_0_0(do_float_recip_s);
6220
        gen_store_fpr32(fpu32_T[2], fd);
6221
        opn = "recip.s";
6222
        break;
6223
    case FOP(22, 16):
6224
        check_cop1x(ctx);
6225
        gen_load_fpr32(fpu32_T[0], fs);
6226
        tcg_gen_helper_0_0(do_float_rsqrt_s);
6227
        gen_store_fpr32(fpu32_T[2], fd);
6228
        opn = "rsqrt.s";
6229
        break;
6230
    case FOP(28, 16):
6231
        check_cp1_64bitmode(ctx);
6232
        gen_load_fpr32(fpu32_T[0], fs);
6233
        gen_load_fpr32(fpu32_T[2], fd);
6234
        tcg_gen_helper_0_0(do_float_recip2_s);
6235
        gen_store_fpr32(fpu32_T[2], fd);
6236
        opn = "recip2.s";
6237
        break;
6238
    case FOP(29, 16):
6239
        check_cp1_64bitmode(ctx);
6240
        gen_load_fpr32(fpu32_T[0], fs);
6241
        tcg_gen_helper_0_0(do_float_recip1_s);
6242
        gen_store_fpr32(fpu32_T[2], fd);
6243
        opn = "recip1.s";
6244
        break;
6245
    case FOP(30, 16):
6246
        check_cp1_64bitmode(ctx);
6247
        gen_load_fpr32(fpu32_T[0], fs);
6248
        tcg_gen_helper_0_0(do_float_rsqrt1_s);
6249
        gen_store_fpr32(fpu32_T[2], fd);
6250
        opn = "rsqrt1.s";
6251
        break;
6252
    case FOP(31, 16):
6253
        check_cp1_64bitmode(ctx);
6254
        gen_load_fpr32(fpu32_T[0], fs);
6255
        gen_load_fpr32(fpu32_T[2], ft);
6256
        tcg_gen_helper_0_0(do_float_rsqrt2_s);
6257
        gen_store_fpr32(fpu32_T[2], fd);
6258
        opn = "rsqrt2.s";
6259
        break;
6260
    case FOP(33, 16):
6261
        check_cp1_registers(ctx, fd);
6262
        gen_load_fpr32(fpu32_T[0], fs);
6263
        tcg_gen_helper_0_0(do_float_cvtd_s);
6264
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6265
        opn = "cvt.d.s";
6266
        break;
6267
    case FOP(36, 16):
6268
        gen_load_fpr32(fpu32_T[0], fs);
6269
        tcg_gen_helper_0_0(do_float_cvtw_s);
6270
        gen_store_fpr32(fpu32_T[2], fd);
6271
        opn = "cvt.w.s";
6272
        break;
6273
    case FOP(37, 16):
6274
        check_cp1_64bitmode(ctx);
6275
        gen_load_fpr32(fpu32_T[0], fs);
6276
        tcg_gen_helper_0_0(do_float_cvtl_s);
6277
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6278
        opn = "cvt.l.s";
6279
        break;
6280
    case FOP(38, 16):
6281
        check_cp1_64bitmode(ctx);
6282
        gen_load_fpr32(fpu32_T[0], fs);
6283
        gen_load_fpr32(fpu32_T[1], ft);
6284
        tcg_gen_extu_i32_i64(fpu64_T[0], fpu32_T[0]);
6285
        tcg_gen_extu_i32_i64(fpu64_T[1], fpu32_T[1]);
6286
        tcg_gen_shli_i64(fpu64_T[1], fpu64_T[1], 32);
6287
        tcg_gen_or_i64(fpu64_T[2], fpu64_T[0], fpu64_T[1]);
6288
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6289
        opn = "cvt.ps.s";
6290
        break;
6291
    case FOP(48, 16):
6292
    case FOP(49, 16):
6293
    case FOP(50, 16):
6294
    case FOP(51, 16):
6295
    case FOP(52, 16):
6296
    case FOP(53, 16):
6297
    case FOP(54, 16):
6298
    case FOP(55, 16):
6299
    case FOP(56, 16):
6300
    case FOP(57, 16):
6301
    case FOP(58, 16):
6302
    case FOP(59, 16):
6303
    case FOP(60, 16):
6304
    case FOP(61, 16):
6305
    case FOP(62, 16):
6306
    case FOP(63, 16):
6307
        gen_load_fpr32(fpu32_T[0], fs);
6308
        gen_load_fpr32(fpu32_T[1], ft);
6309
        if (ctx->opcode & (1 << 6)) {
6310
            check_cop1x(ctx);
6311
            gen_cmpabs_s(func-48, cc);
6312
            opn = condnames_abs[func-48];
6313
        } else {
6314
            gen_cmp_s(func-48, cc);
6315
            opn = condnames[func-48];
6316
        }
6317
        break;
6318
    case FOP(0, 17):
6319
        check_cp1_registers(ctx, fs | ft | fd);
6320
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6321
        gen_load_fpr64(ctx, fpu64_T[1], ft);
6322
        tcg_gen_helper_0_0(do_float_add_d);
6323
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6324
        opn = "add.d";
6325
        optype = BINOP;
6326
        break;
6327
    case FOP(1, 17):
6328
        check_cp1_registers(ctx, fs | ft | fd);
6329
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6330
        gen_load_fpr64(ctx, fpu64_T[1], ft);
6331
        tcg_gen_helper_0_0(do_float_sub_d);
6332
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6333
        opn = "sub.d";
6334
        optype = BINOP;
6335
        break;
6336
    case FOP(2, 17):
6337
        check_cp1_registers(ctx, fs | ft | fd);
6338
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6339
        gen_load_fpr64(ctx, fpu64_T[1], ft);
6340
        tcg_gen_helper_0_0(do_float_mul_d);
6341
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6342
        opn = "mul.d";
6343
        optype = BINOP;
6344
        break;
6345
    case FOP(3, 17):
6346
        check_cp1_registers(ctx, fs | ft | fd);
6347
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6348
        gen_load_fpr64(ctx, fpu64_T[1], ft);
6349
        tcg_gen_helper_0_0(do_float_div_d);
6350
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6351
        opn = "div.d";
6352
        optype = BINOP;
6353
        break;
6354
    case FOP(4, 17):
6355
        check_cp1_registers(ctx, fs | fd);
6356
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6357
        tcg_gen_helper_0_0(do_float_sqrt_d);
6358
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6359
        opn = "sqrt.d";
6360
        break;
6361
    case FOP(5, 17):
6362
        check_cp1_registers(ctx, fs | fd);
6363
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6364
        tcg_gen_helper_0_0(do_float_abs_d);
6365
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6366
        opn = "abs.d";
6367
        break;
6368
    case FOP(6, 17):
6369
        check_cp1_registers(ctx, fs | fd);
6370
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6371
        gen_store_fpr64(ctx, fpu64_T[0], fd);
6372
        opn = "mov.d";
6373
        break;
6374
    case FOP(7, 17):
6375
        check_cp1_registers(ctx, fs | fd);
6376
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6377
        tcg_gen_helper_0_0(do_float_chs_d);
6378
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6379
        opn = "neg.d";
6380
        break;
6381
    case FOP(8, 17):
6382
        check_cp1_64bitmode(ctx);
6383
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6384
        tcg_gen_helper_0_0(do_float_roundl_d);
6385
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6386
        opn = "round.l.d";
6387
        break;
6388
    case FOP(9, 17):
6389
        check_cp1_64bitmode(ctx);
6390
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6391
        tcg_gen_helper_0_0(do_float_truncl_d);
6392
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6393
        opn = "trunc.l.d";
6394
        break;
6395
    case FOP(10, 17):
6396
        check_cp1_64bitmode(ctx);
6397
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6398
        tcg_gen_helper_0_0(do_float_ceill_d);
6399
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6400
        opn = "ceil.l.d";
6401
        break;
6402
    case FOP(11, 17):
6403
        check_cp1_64bitmode(ctx);
6404
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6405
        tcg_gen_helper_0_0(do_float_floorl_d);
6406
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6407
        opn = "floor.l.d";
6408
        break;
6409
    case FOP(12, 17):
6410
        check_cp1_registers(ctx, fs);
6411
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6412
        tcg_gen_helper_0_0(do_float_roundw_d);
6413
        gen_store_fpr32(fpu32_T[2], fd);
6414
        opn = "round.w.d";
6415
        break;
6416
    case FOP(13, 17):
6417
        check_cp1_registers(ctx, fs);
6418
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6419
        tcg_gen_helper_0_0(do_float_truncw_d);
6420
        gen_store_fpr32(fpu32_T[2], fd);
6421
        opn = "trunc.w.d";
6422
        break;
6423
    case FOP(14, 17):
6424
        check_cp1_registers(ctx, fs);
6425
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6426
        tcg_gen_helper_0_0(do_float_ceilw_d);
6427
        gen_store_fpr32(fpu32_T[2], fd);
6428
        opn = "ceil.w.d";
6429
        break;
6430
    case FOP(15, 17):
6431
        check_cp1_registers(ctx, fs);
6432
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6433
        tcg_gen_helper_0_0(do_float_floorw_d);
6434
        gen_store_fpr32(fpu32_T[2], fd);
6435
        opn = "floor.w.d";
6436
        break;
6437
    case FOP(17, 17):
6438
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6439
        gen_load_fpr64(ctx, fpu64_T[2], fd);
6440
        gen_movcf_d((ft >> 2) & 0x7, ft & 0x1);
6441
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6442
        opn = "movcf.d";
6443
        break;
6444
    case FOP(18, 17):
6445
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6446
        gen_load_fpr64(ctx, fpu64_T[2], fd);
6447
        {
6448
            int l1 = gen_new_label();
6449
            TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
6450

    
6451
            gen_load_gpr(t0, ft);
6452
            tcg_gen_brcondi_tl(TCG_COND_NE, t0, 0, l1);
6453
            tcg_temp_free(t0);
6454
            tcg_gen_mov_i64(fpu64_T[2], fpu64_T[0]);
6455
            gen_set_label(l1);
6456
        }
6457
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6458
        opn = "movz.d";
6459
        break;
6460
    case FOP(19, 17):
6461
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6462
        gen_load_fpr64(ctx, fpu64_T[2], fd);
6463
        {
6464
            int l1 = gen_new_label();
6465
            TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
6466

    
6467
            gen_load_gpr(t0, ft);
6468
            tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
6469
            tcg_temp_free(t0);
6470
            tcg_gen_mov_i64(fpu64_T[2], fpu64_T[0]);
6471
            gen_set_label(l1);
6472
        }
6473
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6474
        opn = "movn.d";
6475
        break;
6476
    case FOP(21, 17):
6477
        check_cp1_64bitmode(ctx);
6478
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6479
        tcg_gen_helper_0_0(do_float_recip_d);
6480
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6481
        opn = "recip.d";
6482
        break;
6483
    case FOP(22, 17):
6484
        check_cp1_64bitmode(ctx);
6485
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6486
        tcg_gen_helper_0_0(do_float_rsqrt_d);
6487
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6488
        opn = "rsqrt.d";
6489
        break;
6490
    case FOP(28, 17):
6491
        check_cp1_64bitmode(ctx);
6492
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6493
        gen_load_fpr64(ctx, fpu64_T[2], ft);
6494
        tcg_gen_helper_0_0(do_float_recip2_d);
6495
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6496
        opn = "recip2.d";
6497
        break;
6498
    case FOP(29, 17):
6499
        check_cp1_64bitmode(ctx);
6500
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6501
        tcg_gen_helper_0_0(do_float_recip1_d);
6502
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6503
        opn = "recip1.d";
6504
        break;
6505
    case FOP(30, 17):
6506
        check_cp1_64bitmode(ctx);
6507
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6508
        tcg_gen_helper_0_0(do_float_rsqrt1_d);
6509
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6510
        opn = "rsqrt1.d";
6511
        break;
6512
    case FOP(31, 17):
6513
        check_cp1_64bitmode(ctx);
6514
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6515
        gen_load_fpr64(ctx, fpu64_T[2], ft);
6516
        tcg_gen_helper_0_0(do_float_rsqrt2_d);
6517
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6518
        opn = "rsqrt2.d";
6519
        break;
6520
    case FOP(48, 17):
6521
    case FOP(49, 17):
6522
    case FOP(50, 17):
6523
    case FOP(51, 17):
6524
    case FOP(52, 17):
6525
    case FOP(53, 17):
6526
    case FOP(54, 17):
6527
    case FOP(55, 17):
6528
    case FOP(56, 17):
6529
    case FOP(57, 17):
6530
    case FOP(58, 17):
6531
    case FOP(59, 17):
6532
    case FOP(60, 17):
6533
    case FOP(61, 17):
6534
    case FOP(62, 17):
6535
    case FOP(63, 17):
6536
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6537
        gen_load_fpr64(ctx, fpu64_T[1], ft);
6538
        if (ctx->opcode & (1 << 6)) {
6539
            check_cop1x(ctx);
6540
            check_cp1_registers(ctx, fs | ft);
6541
            gen_cmpabs_d(func-48, cc);
6542
            opn = condnames_abs[func-48];
6543
        } else {
6544
            check_cp1_registers(ctx, fs | ft);
6545
            gen_cmp_d(func-48, cc);
6546
            opn = condnames[func-48];
6547
        }
6548
        break;
6549
    case FOP(32, 17):
6550
        check_cp1_registers(ctx, fs);
6551
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6552
        tcg_gen_helper_0_0(do_float_cvts_d);
6553
        gen_store_fpr32(fpu32_T[2], fd);
6554
        opn = "cvt.s.d";
6555
        break;
6556
    case FOP(36, 17):
6557
        check_cp1_registers(ctx, fs);
6558
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6559
        tcg_gen_helper_0_0(do_float_cvtw_d);
6560
        gen_store_fpr32(fpu32_T[2], fd);
6561
        opn = "cvt.w.d";
6562
        break;
6563
    case FOP(37, 17):
6564
        check_cp1_64bitmode(ctx);
6565
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6566
        tcg_gen_helper_0_0(do_float_cvtl_d);
6567
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6568
        opn = "cvt.l.d";
6569
        break;
6570
    case FOP(32, 20):
6571
        gen_load_fpr32(fpu32_T[0], fs);
6572
        tcg_gen_helper_0_0(do_float_cvts_w);
6573
        gen_store_fpr32(fpu32_T[2], fd);
6574
        opn = "cvt.s.w";
6575
        break;
6576
    case FOP(33, 20):
6577
        check_cp1_registers(ctx, fd);
6578
        gen_load_fpr32(fpu32_T[0], fs);
6579
        tcg_gen_helper_0_0(do_float_cvtd_w);
6580
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6581
        opn = "cvt.d.w";
6582
        break;
6583
    case FOP(32, 21):
6584
        check_cp1_64bitmode(ctx);
6585
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6586
        tcg_gen_helper_0_0(do_float_cvts_l);
6587
        gen_store_fpr32(fpu32_T[2], fd);
6588
        opn = "cvt.s.l";
6589
        break;
6590
    case FOP(33, 21):
6591
        check_cp1_64bitmode(ctx);
6592
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6593
        tcg_gen_helper_0_0(do_float_cvtd_l);
6594
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6595
        opn = "cvt.d.l";
6596
        break;
6597
    case FOP(38, 20):
6598
        check_cp1_64bitmode(ctx);
6599
        gen_load_fpr32(fpu32_T[0], fs);
6600
        gen_load_fpr32h(fpu32h_T[0], fs);
6601
        tcg_gen_helper_0_0(do_float_cvtps_pw);
6602
        gen_store_fpr32(fpu32_T[2], fd);
6603
        gen_store_fpr32h(fpu32h_T[2], fd);
6604
        opn = "cvt.ps.pw";
6605
        break;
6606
    case FOP(0, 22):
6607
        check_cp1_64bitmode(ctx);
6608
        gen_load_fpr32(fpu32_T[0], fs);
6609
        gen_load_fpr32h(fpu32h_T[0], fs);
6610
        gen_load_fpr32(fpu32_T[1], ft);
6611
        gen_load_fpr32h(fpu32h_T[1], ft);
6612
        tcg_gen_helper_0_0(do_float_add_ps);
6613
        gen_store_fpr32(fpu32_T[2], fd);
6614
        gen_store_fpr32h(fpu32h_T[2], fd);
6615
        opn = "add.ps";
6616
        break;
6617
    case FOP(1, 22):
6618
        check_cp1_64bitmode(ctx);
6619
        gen_load_fpr32(fpu32_T[0], fs);
6620
        gen_load_fpr32h(fpu32h_T[0], fs);
6621
        gen_load_fpr32(fpu32_T[1], ft);
6622
        gen_load_fpr32h(fpu32h_T[1], ft);
6623
        tcg_gen_helper_0_0(do_float_sub_ps);
6624
        gen_store_fpr32(fpu32_T[2], fd);
6625
        gen_store_fpr32h(fpu32h_T[2], fd);
6626
        opn = "sub.ps";
6627
        break;
6628
    case FOP(2, 22):
6629
        check_cp1_64bitmode(ctx);
6630
        gen_load_fpr32(fpu32_T[0], fs);
6631
        gen_load_fpr32h(fpu32h_T[0], fs);
6632
        gen_load_fpr32(fpu32_T[1], ft);
6633
        gen_load_fpr32h(fpu32h_T[1], ft);
6634
        tcg_gen_helper_0_0(do_float_mul_ps);
6635
        gen_store_fpr32(fpu32_T[2], fd);
6636
        gen_store_fpr32h(fpu32h_T[2], fd);
6637
        opn = "mul.ps";
6638
        break;
6639
    case FOP(5, 22):
6640
        check_cp1_64bitmode(ctx);
6641
        gen_load_fpr32(fpu32_T[0], fs);
6642
        gen_load_fpr32h(fpu32h_T[0], fs);
6643
        tcg_gen_helper_0_0(do_float_abs_ps);
6644
        gen_store_fpr32(fpu32_T[2], fd);
6645
        gen_store_fpr32h(fpu32h_T[2], fd);
6646
        opn = "abs.ps";
6647
        break;
6648
    case FOP(6, 22):
6649
        check_cp1_64bitmode(ctx);
6650
        gen_load_fpr32(fpu32_T[0], fs);
6651
        gen_load_fpr32h(fpu32h_T[0], fs);
6652
        gen_store_fpr32(fpu32_T[0], fd);
6653
        gen_store_fpr32h(fpu32h_T[0], fd);
6654
        opn = "mov.ps";
6655
        break;
6656
    case FOP(7, 22):
6657
        check_cp1_64bitmode(ctx);
6658
        gen_load_fpr32(fpu32_T[0], fs);
6659
        gen_load_fpr32h(fpu32h_T[0], fs);
6660
        tcg_gen_helper_0_0(do_float_chs_ps);
6661
        gen_store_fpr32(fpu32_T[2], fd);
6662
        gen_store_fpr32h(fpu32h_T[2], fd);
6663
        opn = "neg.ps";
6664
        break;
6665
    case FOP(17, 22):
6666
        check_cp1_64bitmode(ctx);
6667
        gen_load_fpr32(fpu32_T[0], fs);
6668
        gen_load_fpr32h(fpu32h_T[0], fs);
6669
        gen_load_fpr32(fpu32_T[2], fd);
6670
        gen_load_fpr32h(fpu32h_T[2], fd);
6671
        gen_movcf_ps((ft >> 2) & 0x7, ft & 0x1);
6672
        gen_store_fpr32(fpu32_T[2], fd);
6673
        gen_store_fpr32h(fpu32h_T[2], fd);
6674
        opn = "movcf.ps";
6675
        break;
6676
    case FOP(18, 22):
6677
        check_cp1_64bitmode(ctx);
6678
        gen_load_fpr32(fpu32_T[0], fs);
6679
        gen_load_fpr32h(fpu32h_T[0], fs);
6680
        gen_load_fpr32(fpu32_T[2], fd);
6681
        gen_load_fpr32h(fpu32h_T[2], fd);
6682
        {
6683
            int l1 = gen_new_label();
6684
            TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
6685

    
6686
            gen_load_gpr(t0, ft);
6687
            tcg_gen_brcondi_tl(TCG_COND_NE, t0, 0, l1);
6688
            tcg_temp_free(t0);
6689
            tcg_gen_mov_i32(fpu32_T[2], fpu32_T[0]);
6690
            tcg_gen_mov_i32(fpu32h_T[2], fpu32h_T[0]);
6691
            gen_set_label(l1);
6692
        }
6693
        gen_store_fpr32(fpu32_T[2], fd);
6694
        gen_store_fpr32h(fpu32h_T[2], fd);
6695
        opn = "movz.ps";
6696
        break;
6697
    case FOP(19, 22):
6698
        check_cp1_64bitmode(ctx);
6699
        gen_load_fpr32(fpu32_T[0], fs);
6700
        gen_load_fpr32h(fpu32h_T[0], fs);
6701
        gen_load_fpr32(fpu32_T[2], fd);
6702
        gen_load_fpr32h(fpu32h_T[2], fd);
6703
        {
6704
            int l1 = gen_new_label();
6705
            TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
6706

    
6707
            gen_load_gpr(t0, ft);
6708
            tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
6709
            tcg_temp_free(t0);
6710
            tcg_gen_mov_i32(fpu32_T[2], fpu32_T[0]);
6711
            tcg_gen_mov_i32(fpu32h_T[2], fpu32h_T[0]);
6712
            gen_set_label(l1);
6713
        }
6714
        gen_store_fpr32(fpu32_T[2], fd);
6715
        gen_store_fpr32h(fpu32h_T[2], fd);
6716
        opn = "movn.ps";
6717
        break;
6718
    case FOP(24, 22):
6719
        check_cp1_64bitmode(ctx);
6720
        gen_load_fpr32(fpu32_T[0], ft);
6721
        gen_load_fpr32h(fpu32h_T[0], ft);
6722
        gen_load_fpr32(fpu32_T[1], fs);
6723
        gen_load_fpr32h(fpu32h_T[1], fs);
6724
        tcg_gen_helper_0_0(do_float_addr_ps);
6725
        gen_store_fpr32(fpu32_T[2], fd);
6726
        gen_store_fpr32h(fpu32h_T[2], fd);
6727
        opn = "addr.ps";
6728
        break;
6729
    case FOP(26, 22):
6730
        check_cp1_64bitmode(ctx);
6731
        gen_load_fpr32(fpu32_T[0], ft);
6732
        gen_load_fpr32h(fpu32h_T[0], ft);
6733
        gen_load_fpr32(fpu32_T[1], fs);
6734
        gen_load_fpr32h(fpu32h_T[1], fs);
6735
        tcg_gen_helper_0_0(do_float_mulr_ps);
6736
        gen_store_fpr32(fpu32_T[2], fd);
6737
        gen_store_fpr32h(fpu32h_T[2], fd);
6738
        opn = "mulr.ps";
6739
        break;
6740
    case FOP(28, 22):
6741
        check_cp1_64bitmode(ctx);
6742
        gen_load_fpr32(fpu32_T[0], fs);
6743
        gen_load_fpr32h(fpu32h_T[0], fs);
6744
        gen_load_fpr32(fpu32_T[2], fd);
6745
        gen_load_fpr32h(fpu32h_T[2], fd);
6746
        tcg_gen_helper_0_0(do_float_recip2_ps);
6747
        gen_store_fpr32(fpu32_T[2], fd);
6748
        gen_store_fpr32h(fpu32h_T[2], fd);
6749
        opn = "recip2.ps";
6750
        break;
6751
    case FOP(29, 22):
6752
        check_cp1_64bitmode(ctx);
6753
        gen_load_fpr32(fpu32_T[0], fs);
6754
        gen_load_fpr32h(fpu32h_T[0], fs);
6755
        tcg_gen_helper_0_0(do_float_recip1_ps);
6756
        gen_store_fpr32(fpu32_T[2], fd);
6757
        gen_store_fpr32h(fpu32h_T[2], fd);
6758
        opn = "recip1.ps";
6759
        break;
6760
    case FOP(30, 22):
6761
        check_cp1_64bitmode(ctx);
6762
        gen_load_fpr32(fpu32_T[0], fs);
6763
        gen_load_fpr32h(fpu32h_T[0], fs);
6764
        tcg_gen_helper_0_0(do_float_rsqrt1_ps);
6765
        gen_store_fpr32(fpu32_T[2], fd);
6766
        gen_store_fpr32h(fpu32h_T[2], fd);
6767
        opn = "rsqrt1.ps";
6768
        break;
6769
    case FOP(31, 22):
6770
        check_cp1_64bitmode(ctx);
6771
        gen_load_fpr32(fpu32_T[0], fs);
6772
        gen_load_fpr32h(fpu32h_T[0], fs);
6773
        gen_load_fpr32(fpu32_T[2], ft);
6774
        gen_load_fpr32h(fpu32h_T[2], ft);
6775
        tcg_gen_helper_0_0(do_float_rsqrt2_ps);
6776
        gen_store_fpr32(fpu32_T[2], fd);
6777
        gen_store_fpr32h(fpu32h_T[2], fd);
6778
        opn = "rsqrt2.ps";
6779
        break;
6780
    case FOP(32, 22):
6781
        check_cp1_64bitmode(ctx);
6782
        gen_load_fpr32h(fpu32h_T[0], fs);
6783
        tcg_gen_helper_0_0(do_float_cvts_pu);
6784
        gen_store_fpr32(fpu32_T[2], fd);
6785
        opn = "cvt.s.pu";
6786
        break;
6787
    case FOP(36, 22):
6788
        check_cp1_64bitmode(ctx);
6789
        gen_load_fpr32(fpu32_T[0], fs);
6790
        gen_load_fpr32h(fpu32h_T[0], fs);
6791
        tcg_gen_helper_0_0(do_float_cvtpw_ps);
6792
        gen_store_fpr32(fpu32_T[2], fd);
6793
        gen_store_fpr32h(fpu32h_T[2], fd);
6794
        opn = "cvt.pw.ps";
6795
        break;
6796
    case FOP(40, 22):
6797
        check_cp1_64bitmode(ctx);
6798
        gen_load_fpr32(fpu32_T[0], fs);
6799
        tcg_gen_helper_0_0(do_float_cvts_pl);
6800
        gen_store_fpr32(fpu32_T[2], fd);
6801
        opn = "cvt.s.pl";
6802
        break;
6803
    case FOP(44, 22):
6804
        check_cp1_64bitmode(ctx);
6805
        gen_load_fpr32(fpu32_T[0], fs);
6806
        gen_load_fpr32(fpu32_T[1], ft);
6807
        gen_store_fpr32h(fpu32_T[0], fd);
6808
        gen_store_fpr32(fpu32_T[1], fd);
6809
        opn = "pll.ps";
6810
        break;
6811
    case FOP(45, 22):
6812
        check_cp1_64bitmode(ctx);
6813
        gen_load_fpr32(fpu32_T[0], fs);
6814
        gen_load_fpr32h(fpu32h_T[1], ft);
6815
        gen_store_fpr32(fpu32h_T[1], fd);
6816
        gen_store_fpr32h(fpu32_T[0], fd);
6817
        opn = "plu.ps";
6818
        break;
6819
    case FOP(46, 22):
6820
        check_cp1_64bitmode(ctx);
6821
        gen_load_fpr32h(fpu32h_T[0], fs);
6822
        gen_load_fpr32(fpu32_T[1], ft);
6823
        gen_store_fpr32(fpu32_T[1], fd);
6824
        gen_store_fpr32h(fpu32h_T[0], fd);
6825
        opn = "pul.ps";
6826
        break;
6827
    case FOP(47, 22):
6828
        check_cp1_64bitmode(ctx);
6829
        gen_load_fpr32h(fpu32h_T[0], fs);
6830
        gen_load_fpr32h(fpu32h_T[1], ft);
6831
        gen_store_fpr32(fpu32h_T[1], fd);
6832
        gen_store_fpr32h(fpu32h_T[0], fd);
6833
        opn = "puu.ps";
6834
        break;
6835
    case FOP(48, 22):
6836
    case FOP(49, 22):
6837
    case FOP(50, 22):
6838
    case FOP(51, 22):
6839
    case FOP(52, 22):
6840
    case FOP(53, 22):
6841
    case FOP(54, 22):
6842
    case FOP(55, 22):
6843
    case FOP(56, 22):
6844
    case FOP(57, 22):
6845
    case FOP(58, 22):
6846
    case FOP(59, 22):
6847
    case FOP(60, 22):
6848
    case FOP(61, 22):
6849
    case FOP(62, 22):
6850
    case FOP(63, 22):
6851
        check_cp1_64bitmode(ctx);
6852
        gen_load_fpr32(fpu32_T[0], fs);
6853
        gen_load_fpr32h(fpu32h_T[0], fs);
6854
        gen_load_fpr32(fpu32_T[1], ft);
6855
        gen_load_fpr32h(fpu32h_T[1], ft);
6856
        if (ctx->opcode & (1 << 6)) {
6857
            gen_cmpabs_ps(func-48, cc);
6858
            opn = condnames_abs[func-48];
6859
        } else {
6860
            gen_cmp_ps(func-48, cc);
6861
            opn = condnames[func-48];
6862
        }
6863
        break;
6864
    default:
6865
        MIPS_INVAL(opn);
6866
        generate_exception (ctx, EXCP_RI);
6867
        return;
6868
    }
6869
    switch (optype) {
6870
    case BINOP:
6871
        MIPS_DEBUG("%s %s, %s, %s", opn, fregnames[fd], fregnames[fs], fregnames[ft]);
6872
        break;
6873
    case CMPOP:
6874
        MIPS_DEBUG("%s %s,%s", opn, fregnames[fs], fregnames[ft]);
6875
        break;
6876
    default:
6877
        MIPS_DEBUG("%s %s,%s", opn, fregnames[fd], fregnames[fs]);
6878
        break;
6879
    }
6880
}
6881

    
6882
/* Coprocessor 3 (FPU) */
6883
static void gen_flt3_ldst (DisasContext *ctx, uint32_t opc,
6884
                           int fd, int fs, int base, int index)
6885
{
6886
    const char *opn = "extended float load/store";
6887
    int store = 0;
6888
    TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
6889
    TCGv t1 = tcg_temp_local_new(TCG_TYPE_TL);
6890

    
6891
    if (base == 0) {
6892
        gen_load_gpr(t0, index);
6893
    } else if (index == 0) {
6894
        gen_load_gpr(t0, base);
6895
    } else {
6896
        gen_load_gpr(t0, base);
6897
        gen_load_gpr(t1, index);
6898
        gen_op_addr_add(t0, t1);
6899
    }
6900
    /* Don't do NOP if destination is zero: we must perform the actual
6901
       memory access. */
6902
    switch (opc) {
6903
    case OPC_LWXC1:
6904
        check_cop1x(ctx);
6905
        tcg_gen_qemu_ld32s(fpu32_T[0], t0, ctx->mem_idx);
6906
        gen_store_fpr32(fpu32_T[0], fd);
6907
        opn = "lwxc1";
6908
        break;
6909
    case OPC_LDXC1:
6910
        check_cop1x(ctx);
6911
        check_cp1_registers(ctx, fd);
6912
        tcg_gen_qemu_ld64(fpu64_T[0], t0, ctx->mem_idx);
6913
        gen_store_fpr64(ctx, fpu64_T[0], fd);
6914
        opn = "ldxc1";
6915
        break;
6916
    case OPC_LUXC1:
6917
        check_cp1_64bitmode(ctx);
6918
        tcg_gen_andi_tl(t0, t0, ~0x7);
6919
        tcg_gen_qemu_ld64(fpu64_T[0], t0, ctx->mem_idx);
6920
        gen_store_fpr64(ctx, fpu64_T[0], fd);
6921
        opn = "luxc1";
6922
        break;
6923
    case OPC_SWXC1:
6924
        check_cop1x(ctx);
6925
        gen_load_fpr32(fpu32_T[0], fs);
6926
        tcg_gen_qemu_st32(fpu32_T[0], t0, ctx->mem_idx);
6927
        opn = "swxc1";
6928
        store = 1;
6929
        break;
6930
    case OPC_SDXC1:
6931
        check_cop1x(ctx);
6932
        check_cp1_registers(ctx, fs);
6933
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6934
        tcg_gen_qemu_st64(fpu64_T[0], t0, ctx->mem_idx);
6935
        opn = "sdxc1";
6936
        store = 1;
6937
        break;
6938
    case OPC_SUXC1:
6939
        check_cp1_64bitmode(ctx);
6940
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6941
        tcg_gen_andi_tl(t0, t0, ~0x7);
6942
        tcg_gen_qemu_st64(fpu64_T[0], t0, ctx->mem_idx);
6943
        opn = "suxc1";
6944
        store = 1;
6945
        break;
6946
    default:
6947
        MIPS_INVAL(opn);
6948
        generate_exception(ctx, EXCP_RI);
6949
        tcg_temp_free(t0);
6950
        tcg_temp_free(t1);
6951
        return;
6952
    }
6953
    tcg_temp_free(t0);
6954
    tcg_temp_free(t1);
6955
    MIPS_DEBUG("%s %s, %s(%s)", opn, fregnames[store ? fs : fd],
6956
               regnames[index], regnames[base]);
6957
}
6958

    
6959
static void gen_flt3_arith (DisasContext *ctx, uint32_t opc,
6960
                            int fd, int fr, int fs, int ft)
6961
{
6962
    const char *opn = "flt3_arith";
6963

    
6964
    switch (opc) {
6965
    case OPC_ALNV_PS:
6966
        check_cp1_64bitmode(ctx);
6967
        {
6968
            TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
6969
            int l1 = gen_new_label();
6970
            int l2 = gen_new_label();
6971

    
6972
            gen_load_gpr(t0, fr);
6973
            tcg_gen_andi_tl(t0, t0, 0x7);
6974
            gen_load_fpr32(fpu32_T[0], fs);
6975
            gen_load_fpr32h(fpu32h_T[0], fs);
6976
            gen_load_fpr32(fpu32_T[1], ft);
6977
            gen_load_fpr32h(fpu32h_T[1], ft);
6978

    
6979
            tcg_gen_brcondi_tl(TCG_COND_NE, t0, 0, l1);
6980
            tcg_gen_mov_i32(fpu32_T[2], fpu32_T[0]);
6981
            tcg_gen_mov_i32(fpu32h_T[2], fpu32h_T[0]);
6982
            tcg_gen_br(l2);
6983
            gen_set_label(l1);
6984
            tcg_gen_brcondi_tl(TCG_COND_NE, t0, 4, l2);
6985
            tcg_temp_free(t0);
6986
#ifdef TARGET_WORDS_BIGENDIAN
6987
            tcg_gen_mov_i32(fpu32h_T[2], fpu32_T[0]);
6988
            tcg_gen_mov_i32(fpu32_T[2], fpu32h_T[1]);
6989
#else
6990
            tcg_gen_mov_i32(fpu32h_T[2], fpu32_T[1]);
6991
            tcg_gen_mov_i32(fpu32_T[2], fpu32h_T[0]);
6992
#endif
6993
            gen_set_label(l2);
6994
        }
6995
        gen_store_fpr32(fpu32_T[2], fd);
6996
        gen_store_fpr32h(fpu32h_T[2], fd);
6997
        opn = "alnv.ps";
6998
        break;
6999
    case OPC_MADD_S:
7000
        check_cop1x(ctx);
7001
        gen_load_fpr32(fpu32_T[0], fs);
7002
        gen_load_fpr32(fpu32_T[1], ft);
7003
        gen_load_fpr32(fpu32_T[2], fr);
7004
        tcg_gen_helper_0_0(do_float_muladd_s);
7005
        gen_store_fpr32(fpu32_T[2], fd);
7006
        opn = "madd.s";
7007
        break;
7008
    case OPC_MADD_D:
7009
        check_cop1x(ctx);
7010
        check_cp1_registers(ctx, fd | fs | ft | fr);
7011
        gen_load_fpr64(ctx, fpu64_T[0], fs);
7012
        gen_load_fpr64(ctx, fpu64_T[1], ft);
7013
        gen_load_fpr64(ctx, fpu64_T[2], fr);
7014
        tcg_gen_helper_0_0(do_float_muladd_d);
7015
        gen_store_fpr64(ctx, fpu64_T[2], fd);
7016
        opn = "madd.d";
7017
        break;
7018
    case OPC_MADD_PS:
7019
        check_cp1_64bitmode(ctx);
7020
        gen_load_fpr32(fpu32_T[0], fs);
7021
        gen_load_fpr32h(fpu32h_T[0], fs);
7022
        gen_load_fpr32(fpu32_T[1], ft);
7023
        gen_load_fpr32h(fpu32h_T[1], ft);
7024
        gen_load_fpr32(fpu32_T[2], fr);
7025
        gen_load_fpr32h(fpu32h_T[2], fr);
7026
        tcg_gen_helper_0_0(do_float_muladd_ps);
7027
        gen_store_fpr32(fpu32_T[2], fd);
7028
        gen_store_fpr32h(fpu32h_T[2], fd);
7029
        opn = "madd.ps";
7030
        break;
7031
    case OPC_MSUB_S:
7032
        check_cop1x(ctx);
7033
        gen_load_fpr32(fpu32_T[0], fs);
7034
        gen_load_fpr32(fpu32_T[1], ft);
7035
        gen_load_fpr32(fpu32_T[2], fr);
7036
        tcg_gen_helper_0_0(do_float_mulsub_s);
7037
        gen_store_fpr32(fpu32_T[2], fd);
7038
        opn = "msub.s";
7039
        break;
7040
    case OPC_MSUB_D:
7041
        check_cop1x(ctx);
7042
        check_cp1_registers(ctx, fd | fs | ft | fr);
7043
        gen_load_fpr64(ctx, fpu64_T[0], fs);
7044
        gen_load_fpr64(ctx, fpu64_T[1], ft);
7045
        gen_load_fpr64(ctx, fpu64_T[2], fr);
7046
        tcg_gen_helper_0_0(do_float_mulsub_d);
7047
        gen_store_fpr64(ctx, fpu64_T[2], fd);
7048
        opn = "msub.d";
7049
        break;
7050
    case OPC_MSUB_PS:
7051
        check_cp1_64bitmode(ctx);
7052
        gen_load_fpr32(fpu32_T[0], fs);
7053
        gen_load_fpr32h(fpu32h_T[0], fs);
7054
        gen_load_fpr32(fpu32_T[1], ft);
7055
        gen_load_fpr32h(fpu32h_T[1], ft);
7056
        gen_load_fpr32(fpu32_T[2], fr);
7057
        gen_load_fpr32h(fpu32h_T[2], fr);
7058
        tcg_gen_helper_0_0(do_float_mulsub_ps);
7059
        gen_store_fpr32(fpu32_T[2], fd);
7060
        gen_store_fpr32h(fpu32h_T[2], fd);
7061
        opn = "msub.ps";
7062
        break;
7063
    case OPC_NMADD_S:
7064
        check_cop1x(ctx);
7065
        gen_load_fpr32(fpu32_T[0], fs);
7066
        gen_load_fpr32(fpu32_T[1], ft);
7067
        gen_load_fpr32(fpu32_T[2], fr);
7068
        tcg_gen_helper_0_0(do_float_nmuladd_s);
7069
        gen_store_fpr32(fpu32_T[2], fd);
7070
        opn = "nmadd.s";
7071
        break;
7072
    case OPC_NMADD_D:
7073
        check_cop1x(ctx);
7074
        check_cp1_registers(ctx, fd | fs | ft | fr);
7075
        gen_load_fpr64(ctx, fpu64_T[0], fs);
7076
        gen_load_fpr64(ctx, fpu64_T[1], ft);
7077
        gen_load_fpr64(ctx, fpu64_T[2], fr);
7078
        tcg_gen_helper_0_0(do_float_nmuladd_d);
7079
        gen_store_fpr64(ctx, fpu64_T[2], fd);
7080
        opn = "nmadd.d";
7081
        break;
7082
    case OPC_NMADD_PS:
7083
        check_cp1_64bitmode(ctx);
7084
        gen_load_fpr32(fpu32_T[0], fs);
7085
        gen_load_fpr32h(fpu32h_T[0], fs);
7086
        gen_load_fpr32(fpu32_T[1], ft);
7087
        gen_load_fpr32h(fpu32h_T[1], ft);
7088
        gen_load_fpr32(fpu32_T[2], fr);
7089
        gen_load_fpr32h(fpu32h_T[2], fr);
7090
        tcg_gen_helper_0_0(do_float_nmuladd_ps);
7091
        gen_store_fpr32(fpu32_T[2], fd);
7092
        gen_store_fpr32h(fpu32h_T[2], fd);
7093
        opn = "nmadd.ps";
7094
        break;
7095
    case OPC_NMSUB_S:
7096
        check_cop1x(ctx);
7097
        gen_load_fpr32(fpu32_T[0], fs);
7098
        gen_load_fpr32(fpu32_T[1], ft);
7099
        gen_load_fpr32(fpu32_T[2], fr);
7100
        tcg_gen_helper_0_0(do_float_nmulsub_s);
7101
        gen_store_fpr32(fpu32_T[2], fd);
7102
        opn = "nmsub.s";
7103
        break;
7104
    case OPC_NMSUB_D:
7105
        check_cop1x(ctx);
7106
        check_cp1_registers(ctx, fd | fs | ft | fr);
7107
        gen_load_fpr64(ctx, fpu64_T[0], fs);
7108
        gen_load_fpr64(ctx, fpu64_T[1], ft);
7109
        gen_load_fpr64(ctx, fpu64_T[2], fr);
7110
        tcg_gen_helper_0_0(do_float_nmulsub_d);
7111
        gen_store_fpr64(ctx, fpu64_T[2], fd);
7112
        opn = "nmsub.d";
7113
        break;
7114
    case OPC_NMSUB_PS:
7115
        check_cp1_64bitmode(ctx);
7116
        gen_load_fpr32(fpu32_T[0], fs);
7117
        gen_load_fpr32h(fpu32h_T[0], fs);
7118
        gen_load_fpr32(fpu32_T[1], ft);
7119
        gen_load_fpr32h(fpu32h_T[1], ft);
7120
        gen_load_fpr32(fpu32_T[2], fr);
7121
        gen_load_fpr32h(fpu32h_T[2], fr);
7122
        tcg_gen_helper_0_0(do_float_nmulsub_ps);
7123
        gen_store_fpr32(fpu32_T[2], fd);
7124
        gen_store_fpr32h(fpu32h_T[2], fd);
7125
        opn = "nmsub.ps";
7126
        break;
7127
    default:
7128
        MIPS_INVAL(opn);
7129
        generate_exception (ctx, EXCP_RI);
7130
        return;
7131
    }
7132
    MIPS_DEBUG("%s %s, %s, %s, %s", opn, fregnames[fd], fregnames[fr],
7133
               fregnames[fs], fregnames[ft]);
7134
}
7135

    
7136
/* ISA extensions (ASEs) */
7137
/* MIPS16 extension to MIPS32 */
7138
/* SmartMIPS extension to MIPS32 */
7139

    
7140
#if defined(TARGET_MIPS64)
7141

    
7142
/* MDMX extension to MIPS64 */
7143

    
7144
#endif
7145

    
7146
static void decode_opc (CPUState *env, DisasContext *ctx)
7147
{
7148
    int32_t offset;
7149
    int rs, rt, rd, sa;
7150
    uint32_t op, op1, op2;
7151
    int16_t imm;
7152

    
7153
    /* make sure instructions are on a word boundary */
7154
    if (ctx->pc & 0x3) {
7155
        env->CP0_BadVAddr = ctx->pc;
7156
        generate_exception(ctx, EXCP_AdEL);
7157
        return;
7158
    }
7159

    
7160
    /* Handle blikely not taken case */
7161
    if ((ctx->hflags & MIPS_HFLAG_BMASK) == MIPS_HFLAG_BL) {
7162
        TCGv r_tmp = tcg_temp_local_new(TCG_TYPE_TL);
7163
        int l1 = gen_new_label();
7164

    
7165
        MIPS_DEBUG("blikely condition (" TARGET_FMT_lx ")", ctx->pc + 4);
7166
        tcg_gen_ld_tl(r_tmp, cpu_env, offsetof(CPUState, bcond));
7167
        tcg_gen_brcondi_tl(TCG_COND_NE, r_tmp, 0, l1);
7168
        tcg_temp_free(r_tmp);
7169
        {
7170
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I32);
7171

    
7172
            tcg_gen_movi_i32(r_tmp2, ctx->hflags & ~MIPS_HFLAG_BMASK);
7173
            tcg_gen_st_i32(r_tmp2, cpu_env, offsetof(CPUState, hflags));
7174
            tcg_temp_free(r_tmp2);
7175
        }
7176
        gen_goto_tb(ctx, 1, ctx->pc + 4);
7177
        gen_set_label(l1);
7178
    }
7179
    op = MASK_OP_MAJOR(ctx->opcode);
7180
    rs = (ctx->opcode >> 21) & 0x1f;
7181
    rt = (ctx->opcode >> 16) & 0x1f;
7182
    rd = (ctx->opcode >> 11) & 0x1f;
7183
    sa = (ctx->opcode >> 6) & 0x1f;
7184
    imm = (int16_t)ctx->opcode;
7185
    switch (op) {
7186
    case OPC_SPECIAL:
7187
        op1 = MASK_SPECIAL(ctx->opcode);
7188
        switch (op1) {
7189
        case OPC_SLL:          /* Arithmetic with immediate */
7190
        case OPC_SRL ... OPC_SRA:
7191
            gen_arith_imm(env, ctx, op1, rd, rt, sa);
7192
            break;
7193
        case OPC_MOVZ ... OPC_MOVN:
7194
            check_insn(env, ctx, ISA_MIPS4 | ISA_MIPS32);
7195
        case OPC_SLLV:         /* Arithmetic */
7196
        case OPC_SRLV ... OPC_SRAV:
7197
        case OPC_ADD ... OPC_NOR:
7198
        case OPC_SLT ... OPC_SLTU:
7199
            gen_arith(env, ctx, op1, rd, rs, rt);
7200
            break;
7201
        case OPC_MULT ... OPC_DIVU:
7202
            if (sa) {
7203
                check_insn(env, ctx, INSN_VR54XX);
7204
                op1 = MASK_MUL_VR54XX(ctx->opcode);
7205
                gen_mul_vr54xx(ctx, op1, rd, rs, rt);
7206
            } else
7207
                gen_muldiv(ctx, op1, rs, rt);
7208
            break;
7209
        case OPC_JR ... OPC_JALR:
7210
            gen_compute_branch(ctx, op1, rs, rd, sa);
7211
            return;
7212
        case OPC_TGE ... OPC_TEQ: /* Traps */
7213
        case OPC_TNE:
7214
            gen_trap(ctx, op1, rs, rt, -1);
7215
            break;
7216
        case OPC_MFHI:          /* Move from HI/LO */
7217
        case OPC_MFLO:
7218
            gen_HILO(ctx, op1, rd);
7219
            break;
7220
        case OPC_MTHI:
7221
        case OPC_MTLO:          /* Move to HI/LO */
7222
            gen_HILO(ctx, op1, rs);
7223
            break;
7224
        case OPC_PMON:          /* Pmon entry point, also R4010 selsl */
7225
#ifdef MIPS_STRICT_STANDARD
7226
            MIPS_INVAL("PMON / selsl");
7227
            generate_exception(ctx, EXCP_RI);
7228
#else
7229
            tcg_gen_helper_0_i(do_pmon, sa);
7230
#endif
7231
            break;
7232
        case OPC_SYSCALL:
7233
            generate_exception(ctx, EXCP_SYSCALL);
7234
            break;
7235
        case OPC_BREAK:
7236
            generate_exception(ctx, EXCP_BREAK);
7237
            break;
7238
        case OPC_SPIM:
7239
#ifdef MIPS_STRICT_STANDARD
7240
            MIPS_INVAL("SPIM");
7241
            generate_exception(ctx, EXCP_RI);
7242
#else
7243
           /* Implemented as RI exception for now. */
7244
            MIPS_INVAL("spim (unofficial)");
7245
            generate_exception(ctx, EXCP_RI);
7246
#endif
7247
            break;
7248
        case OPC_SYNC:
7249
            /* Treat as NOP. */
7250
            break;
7251

    
7252
        case OPC_MOVCI:
7253
            check_insn(env, ctx, ISA_MIPS4 | ISA_MIPS32);
7254
            if (env->CP0_Config1 & (1 << CP0C1_FP)) {
7255
                save_cpu_state(ctx, 1);
7256
                check_cp1_enabled(ctx);
7257
                gen_movci(ctx, rd, rs, (ctx->opcode >> 18) & 0x7,
7258
                          (ctx->opcode >> 16) & 1);
7259
            } else {
7260
                generate_exception_err(ctx, EXCP_CpU, 1);
7261
            }
7262
            break;
7263

    
7264
#if defined(TARGET_MIPS64)
7265
       /* MIPS64 specific opcodes */
7266
        case OPC_DSLL:
7267
        case OPC_DSRL ... OPC_DSRA:
7268
        case OPC_DSLL32:
7269
        case OPC_DSRL32 ... OPC_DSRA32:
7270
            check_insn(env, ctx, ISA_MIPS3);
7271
            check_mips_64(ctx);
7272
            gen_arith_imm(env, ctx, op1, rd, rt, sa);
7273
            break;
7274
        case OPC_DSLLV:
7275
        case OPC_DSRLV ... OPC_DSRAV:
7276
        case OPC_DADD ... OPC_DSUBU:
7277
            check_insn(env, ctx, ISA_MIPS3);
7278
            check_mips_64(ctx);
7279
            gen_arith(env, ctx, op1, rd, rs, rt);
7280
            break;
7281
        case OPC_DMULT ... OPC_DDIVU:
7282
            check_insn(env, ctx, ISA_MIPS3);
7283
            check_mips_64(ctx);
7284
            gen_muldiv(ctx, op1, rs, rt);
7285
            break;
7286
#endif
7287
        default:            /* Invalid */
7288
            MIPS_INVAL("special");
7289
            generate_exception(ctx, EXCP_RI);
7290
            break;
7291
        }
7292
        break;
7293
    case OPC_SPECIAL2:
7294
        op1 = MASK_SPECIAL2(ctx->opcode);
7295
        switch (op1) {
7296
        case OPC_MADD ... OPC_MADDU: /* Multiply and add/sub */
7297
        case OPC_MSUB ... OPC_MSUBU:
7298
            check_insn(env, ctx, ISA_MIPS32);
7299
            gen_muldiv(ctx, op1, rs, rt);
7300
            break;
7301
        case OPC_MUL:
7302
            gen_arith(env, ctx, op1, rd, rs, rt);
7303
            break;
7304
        case OPC_CLZ ... OPC_CLO:
7305
            check_insn(env, ctx, ISA_MIPS32);
7306
            gen_cl(ctx, op1, rd, rs);
7307
            break;
7308
        case OPC_SDBBP:
7309
            /* XXX: not clear which exception should be raised
7310
             *      when in debug mode...
7311
             */
7312
            check_insn(env, ctx, ISA_MIPS32);
7313
            if (!(ctx->hflags & MIPS_HFLAG_DM)) {
7314
                generate_exception(ctx, EXCP_DBp);
7315
            } else {
7316
                generate_exception(ctx, EXCP_DBp);
7317
            }
7318
            /* Treat as NOP. */
7319
            break;
7320
#if defined(TARGET_MIPS64)
7321
        case OPC_DCLZ ... OPC_DCLO:
7322
            check_insn(env, ctx, ISA_MIPS64);
7323
            check_mips_64(ctx);
7324
            gen_cl(ctx, op1, rd, rs);
7325
            break;
7326
#endif
7327
        default:            /* Invalid */
7328
            MIPS_INVAL("special2");
7329
            generate_exception(ctx, EXCP_RI);
7330
            break;
7331
        }
7332
        break;
7333
    case OPC_SPECIAL3:
7334
        op1 = MASK_SPECIAL3(ctx->opcode);
7335
        switch (op1) {
7336
        case OPC_EXT:
7337
        case OPC_INS:
7338
            check_insn(env, ctx, ISA_MIPS32R2);
7339
            gen_bitops(ctx, op1, rt, rs, sa, rd);
7340
            break;
7341
        case OPC_BSHFL:
7342
            check_insn(env, ctx, ISA_MIPS32R2);
7343
            op2 = MASK_BSHFL(ctx->opcode);
7344
            {
7345
                TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
7346
                TCGv t1 = tcg_temp_local_new(TCG_TYPE_TL);
7347

    
7348
                switch (op2) {
7349
                case OPC_WSBH:
7350
                    gen_load_gpr(t1, rt);
7351
                    tcg_gen_helper_1_2(do_wsbh, t0, t0, t1);
7352
                    gen_store_gpr(t0, rd);
7353
                    break;
7354
                case OPC_SEB:
7355
                    gen_load_gpr(t1, rt);
7356
                    tcg_gen_ext8s_tl(t0, t1);
7357
                    gen_store_gpr(t0, rd);
7358
                    break;
7359
                case OPC_SEH:
7360
                    gen_load_gpr(t1, rt);
7361
                    tcg_gen_ext16s_tl(t0, t1);
7362
                    gen_store_gpr(t0, rd);
7363
                    break;
7364
                default:            /* Invalid */
7365
                    MIPS_INVAL("bshfl");
7366
                    generate_exception(ctx, EXCP_RI);
7367
                    break;
7368
                }
7369
                tcg_temp_free(t0);
7370
                tcg_temp_free(t1);
7371
            }
7372
            break;
7373
        case OPC_RDHWR:
7374
            check_insn(env, ctx, ISA_MIPS32R2);
7375
            {
7376
                TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
7377

    
7378
                switch (rd) {
7379
                case 0:
7380
                    save_cpu_state(ctx, 1);
7381
                    tcg_gen_helper_1_1(do_rdhwr_cpunum, t0, t0);
7382
                    break;
7383
                case 1:
7384
                    save_cpu_state(ctx, 1);
7385
                    tcg_gen_helper_1_1(do_rdhwr_synci_step, t0, t0);
7386
                    break;
7387
                case 2:
7388
                    save_cpu_state(ctx, 1);
7389
                    tcg_gen_helper_1_1(do_rdhwr_cc, t0, t0);
7390
                    break;
7391
                case 3:
7392
                    save_cpu_state(ctx, 1);
7393
                    tcg_gen_helper_1_1(do_rdhwr_ccres, t0, t0);
7394
                    break;
7395
                case 29:
7396
#if defined (CONFIG_USER_ONLY)
7397
                    tcg_gen_ld_tl(t0, cpu_env, offsetof(CPUState, tls_value));
7398
                    break;
7399
#else
7400
                    /* XXX: Some CPUs implement this in hardware. Not supported yet. */
7401
#endif
7402
                default:            /* Invalid */
7403
                    MIPS_INVAL("rdhwr");
7404
                    generate_exception(ctx, EXCP_RI);
7405
                    break;
7406
                }
7407
                gen_store_gpr(t0, rt);
7408
                tcg_temp_free(t0);
7409
            }
7410
            break;
7411
        case OPC_FORK:
7412
            check_insn(env, ctx, ASE_MT);
7413
            {
7414
                TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
7415
                TCGv t1 = tcg_temp_local_new(TCG_TYPE_TL);
7416

    
7417
                gen_load_gpr(t0, rt);
7418
                gen_load_gpr(t1, rs);
7419
                tcg_gen_helper_0_2(do_fork, t0, t1);
7420
                tcg_temp_free(t0);
7421
                tcg_temp_free(t1);
7422
            }
7423
            break;
7424
        case OPC_YIELD:
7425
            check_insn(env, ctx, ASE_MT);
7426
            {
7427
                TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
7428

    
7429
                gen_load_gpr(t0, rs);
7430
                tcg_gen_helper_1_1(do_yield, t0, t0);
7431
                gen_store_gpr(t0, rd);
7432
                tcg_temp_free(t0);
7433
            }
7434
            break;
7435
#if defined(TARGET_MIPS64)
7436
        case OPC_DEXTM ... OPC_DEXT:
7437
        case OPC_DINSM ... OPC_DINS:
7438
            check_insn(env, ctx, ISA_MIPS64R2);
7439
            check_mips_64(ctx);
7440
            gen_bitops(ctx, op1, rt, rs, sa, rd);
7441
            break;
7442
        case OPC_DBSHFL:
7443
            check_insn(env, ctx, ISA_MIPS64R2);
7444
            check_mips_64(ctx);
7445
            op2 = MASK_DBSHFL(ctx->opcode);
7446
            {
7447
                TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
7448
                TCGv t1 = tcg_temp_local_new(TCG_TYPE_TL);
7449

    
7450
                switch (op2) {
7451
                case OPC_DSBH:
7452
                    gen_load_gpr(t1, rt);
7453
                    tcg_gen_helper_1_2(do_dsbh, t0, t0, t1);
7454
                    break;
7455
                case OPC_DSHD:
7456
                    gen_load_gpr(t1, rt);
7457
                    tcg_gen_helper_1_2(do_dshd, t0, t0, t1);
7458
                    break;
7459
                default:            /* Invalid */
7460
                    MIPS_INVAL("dbshfl");
7461
                    generate_exception(ctx, EXCP_RI);
7462
                    break;
7463
                }
7464
                gen_store_gpr(t0, rd);
7465
                tcg_temp_free(t0);
7466
                tcg_temp_free(t1);
7467
            }
7468
            break;
7469
#endif
7470
        default:            /* Invalid */
7471
            MIPS_INVAL("special3");
7472
            generate_exception(ctx, EXCP_RI);
7473
            break;
7474
        }
7475
        break;
7476
    case OPC_REGIMM:
7477
        op1 = MASK_REGIMM(ctx->opcode);
7478
        switch (op1) {
7479
        case OPC_BLTZ ... OPC_BGEZL: /* REGIMM branches */
7480
        case OPC_BLTZAL ... OPC_BGEZALL:
7481
            gen_compute_branch(ctx, op1, rs, -1, imm << 2);
7482
            return;
7483
        case OPC_TGEI ... OPC_TEQI: /* REGIMM traps */
7484
        case OPC_TNEI:
7485
            gen_trap(ctx, op1, rs, -1, imm);
7486
            break;
7487
        case OPC_SYNCI:
7488
            check_insn(env, ctx, ISA_MIPS32R2);
7489
            /* Treat as NOP. */
7490
            break;
7491
        default:            /* Invalid */
7492
            MIPS_INVAL("regimm");
7493
            generate_exception(ctx, EXCP_RI);
7494
            break;
7495
        }
7496
        break;
7497
    case OPC_CP0:
7498
        check_cp0_enabled(ctx);
7499
        op1 = MASK_CP0(ctx->opcode);
7500
        switch (op1) {
7501
        case OPC_MFC0:
7502
        case OPC_MTC0:
7503
        case OPC_MFTR:
7504
        case OPC_MTTR:
7505
#if defined(TARGET_MIPS64)
7506
        case OPC_DMFC0:
7507
        case OPC_DMTC0:
7508
#endif
7509
#ifndef CONFIG_USER_ONLY
7510
            gen_cp0(env, ctx, op1, rt, rd);
7511
#endif
7512
            break;
7513
        case OPC_C0_FIRST ... OPC_C0_LAST:
7514
#ifndef CONFIG_USER_ONLY
7515
            gen_cp0(env, ctx, MASK_C0(ctx->opcode), rt, rd);
7516
#endif
7517
            break;
7518
        case OPC_MFMC0:
7519
            op2 = MASK_MFMC0(ctx->opcode);
7520
            {
7521
                TCGv t0 = tcg_temp_local_new(TCG_TYPE_TL);
7522

    
7523
                switch (op2) {
7524
                case OPC_DMT:
7525
                    check_insn(env, ctx, ASE_MT);
7526
                    tcg_gen_helper_1_1(do_dmt, t0, t0);
7527
                    break;
7528
                case OPC_EMT:
7529
                    check_insn(env, ctx, ASE_MT);
7530
                    tcg_gen_helper_1_1(do_emt, t0, t0);
7531
                     break;
7532
                case OPC_DVPE:
7533
                    check_insn(env, ctx, ASE_MT);
7534
                    tcg_gen_helper_1_1(do_dvpe, t0, t0);
7535
                    break;
7536
                case OPC_EVPE:
7537
                    check_insn(env, ctx, ASE_MT);
7538
                    tcg_gen_helper_1_1(do_evpe, t0, t0);
7539
                    break;
7540
                case OPC_DI:
7541
                    check_insn(env, ctx, ISA_MIPS32R2);
7542
                    save_cpu_state(ctx, 1);
7543
                    tcg_gen_helper_1_1(do_di, t0, t0);
7544
                    /* Stop translation as we may have switched the execution mode */
7545
                    ctx->bstate = BS_STOP;
7546
                    break;
7547
                case OPC_EI:
7548
                    check_insn(env, ctx, ISA_MIPS32R2);
7549
                    save_cpu_state(ctx, 1);
7550
                    tcg_gen_helper_1_1(do_ei, t0, t0);
7551
                    /* Stop translation as we may have switched the execution mode */
7552
                    ctx->bstate = BS_STOP;
7553
                    break;
7554
                default:            /* Invalid */
7555
                    MIPS_INVAL("mfmc0");
7556
                    generate_exception(ctx, EXCP_RI);
7557
                    break;
7558
                }
7559
                gen_store_gpr(t0, rt);
7560
                tcg_temp_free(t0);
7561
            }
7562
            break;
7563
        case OPC_RDPGPR:
7564
            check_insn(env, ctx, ISA_MIPS32R2);
7565
            gen_load_srsgpr(rt, rd);
7566
            break;
7567
        case OPC_WRPGPR:
7568
            check_insn(env, ctx, ISA_MIPS32R2);
7569
            gen_store_srsgpr(rt, rd);
7570
            break;
7571
        default:
7572
            MIPS_INVAL("cp0");
7573
            generate_exception(ctx, EXCP_RI);
7574
            break;
7575
        }
7576
        break;
7577
    case OPC_ADDI ... OPC_LUI: /* Arithmetic with immediate opcode */
7578
         gen_arith_imm(env, ctx, op, rt, rs, imm);
7579
         break;
7580
    case OPC_J ... OPC_JAL: /* Jump */
7581
         offset = (int32_t)(ctx->opcode & 0x3FFFFFF) << 2;
7582
         gen_compute_branch(ctx, op, rs, rt, offset);
7583
         return;
7584
    case OPC_BEQ ... OPC_BGTZ: /* Branch */
7585
    case OPC_BEQL ... OPC_BGTZL:
7586
         gen_compute_branch(ctx, op, rs, rt, imm << 2);
7587
         return;
7588
    case OPC_LB ... OPC_LWR: /* Load and stores */
7589
    case OPC_SB ... OPC_SW:
7590
    case OPC_SWR:
7591
    case OPC_LL:
7592
    case OPC_SC:
7593
         gen_ldst(ctx, op, rt, rs, imm);
7594
         break;
7595
    case OPC_CACHE:
7596
        check_insn(env, ctx, ISA_MIPS3 | ISA_MIPS32);
7597
        /* Treat as NOP. */
7598
        break;
7599
    case OPC_PREF:
7600
        check_insn(env, ctx, ISA_MIPS4 | ISA_MIPS32);
7601
        /* Treat as NOP. */
7602
        break;
7603

    
7604
    /* Floating point (COP1). */
7605
    case OPC_LWC1:
7606
    case OPC_LDC1:
7607
    case OPC_SWC1:
7608
    case OPC_SDC1:
7609
        if (env->CP0_Config1 & (1 << CP0C1_FP)) {
7610
            save_cpu_state(ctx, 1);
7611
            check_cp1_enabled(ctx);
7612
            gen_flt_ldst(ctx, op, rt, rs, imm);
7613
        } else {
7614
            generate_exception_err(ctx, EXCP_CpU, 1);
7615
        }
7616
        break;
7617

    
7618
    case OPC_CP1:
7619
        if (env->CP0_Config1 & (1 << CP0C1_FP)) {
7620
            save_cpu_state(ctx, 1);
7621
            check_cp1_enabled(ctx);
7622
            op1 = MASK_CP1(ctx->opcode);
7623
            switch (op1) {
7624
            case OPC_MFHC1:
7625
            case OPC_MTHC1:
7626
                check_insn(env, ctx, ISA_MIPS32R2);
7627
            case OPC_MFC1:
7628
            case OPC_CFC1:
7629
            case OPC_MTC1:
7630
            case OPC_CTC1:
7631
                gen_cp1(ctx, op1, rt, rd);
7632
                break;
7633
#if defined(TARGET_MIPS64)
7634
            case OPC_DMFC1:
7635
            case OPC_DMTC1:
7636
                check_insn(env, ctx, ISA_MIPS3);
7637
                gen_cp1(ctx, op1, rt, rd);
7638
                break;
7639
#endif
7640
            case OPC_BC1ANY2:
7641
            case OPC_BC1ANY4:
7642
                check_cop1x(ctx);
7643
                check_insn(env, ctx, ASE_MIPS3D);
7644
                /* fall through */
7645
            case OPC_BC1:
7646
                gen_compute_branch1(env, ctx, MASK_BC1(ctx->opcode),
7647
                                    (rt >> 2) & 0x7, imm << 2);
7648
                return;
7649
            case OPC_S_FMT:
7650
            case OPC_D_FMT:
7651
            case OPC_W_FMT:
7652
            case OPC_L_FMT:
7653
            case OPC_PS_FMT:
7654
                gen_farith(ctx, MASK_CP1_FUNC(ctx->opcode), rt, rd, sa,
7655
                           (imm >> 8) & 0x7);
7656
                break;
7657
            default:
7658
                MIPS_INVAL("cp1");
7659
                generate_exception (ctx, EXCP_RI);
7660
                break;
7661
            }
7662
        } else {
7663
            generate_exception_err(ctx, EXCP_CpU, 1);
7664
        }
7665
        break;
7666

    
7667
    /* COP2.  */
7668
    case OPC_LWC2:
7669
    case OPC_LDC2:
7670
    case OPC_SWC2:
7671
    case OPC_SDC2:
7672
    case OPC_CP2:
7673
        /* COP2: Not implemented. */
7674
        generate_exception_err(ctx, EXCP_CpU, 2);
7675
        break;
7676

    
7677
    case OPC_CP3:
7678
        if (env->CP0_Config1 & (1 << CP0C1_FP)) {
7679
            save_cpu_state(ctx, 1);
7680
            check_cp1_enabled(ctx);
7681
            op1 = MASK_CP3(ctx->opcode);
7682
            switch (op1) {
7683
            case OPC_LWXC1:
7684
            case OPC_LDXC1:
7685
            case OPC_LUXC1:
7686
            case OPC_SWXC1:
7687
            case OPC_SDXC1:
7688
            case OPC_SUXC1:
7689
                gen_flt3_ldst(ctx, op1, sa, rd, rs, rt);
7690
                break;
7691
            case OPC_PREFX:
7692
                /* Treat as NOP. */
7693
                break;
7694
            case OPC_ALNV_PS:
7695
            case OPC_MADD_S:
7696
            case OPC_MADD_D:
7697
            case OPC_MADD_PS:
7698
            case OPC_MSUB_S:
7699
            case OPC_MSUB_D:
7700
            case OPC_MSUB_PS:
7701
            case OPC_NMADD_S:
7702
            case OPC_NMADD_D:
7703
            case OPC_NMADD_PS:
7704
            case OPC_NMSUB_S:
7705
            case OPC_NMSUB_D:
7706
            case OPC_NMSUB_PS:
7707
                gen_flt3_arith(ctx, op1, sa, rs, rd, rt);
7708
                break;
7709
            default:
7710
                MIPS_INVAL("cp3");
7711
                generate_exception (ctx, EXCP_RI);
7712
                break;
7713
            }
7714
        } else {
7715
            generate_exception_err(ctx, EXCP_CpU, 1);
7716
        }
7717
        break;
7718

    
7719
#if defined(TARGET_MIPS64)
7720
    /* MIPS64 opcodes */
7721
    case OPC_LWU:
7722
    case OPC_LDL ... OPC_LDR:
7723
    case OPC_SDL ... OPC_SDR:
7724
    case OPC_LLD:
7725
    case OPC_LD:
7726
    case OPC_SCD:
7727
    case OPC_SD:
7728
        check_insn(env, ctx, ISA_MIPS3);
7729
        check_mips_64(ctx);
7730
        gen_ldst(ctx, op, rt, rs, imm);
7731
        break;
7732
    case OPC_DADDI ... OPC_DADDIU:
7733
        check_insn(env, ctx, ISA_MIPS3);
7734
        check_mips_64(ctx);
7735
        gen_arith_imm(env, ctx, op, rt, rs, imm);
7736
        break;
7737
#endif
7738
    case OPC_JALX:
7739
        check_insn(env, ctx, ASE_MIPS16);
7740
        /* MIPS16: Not implemented. */
7741
    case OPC_MDMX:
7742
        check_insn(env, ctx, ASE_MDMX);
7743
        /* MDMX: Not implemented. */
7744
    default:            /* Invalid */
7745
        MIPS_INVAL("major opcode");
7746
        generate_exception(ctx, EXCP_RI);
7747
        break;
7748
    }
7749
    if (ctx->hflags & MIPS_HFLAG_BMASK) {
7750
        int hflags = ctx->hflags & MIPS_HFLAG_BMASK;
7751
        /* Branches completion */
7752
        ctx->hflags &= ~MIPS_HFLAG_BMASK;
7753
        ctx->bstate = BS_BRANCH;
7754
        save_cpu_state(ctx, 0);
7755
        switch (hflags) {
7756
        case MIPS_HFLAG_B:
7757
            /* unconditional branch */
7758
            MIPS_DEBUG("unconditional branch");
7759
            gen_goto_tb(ctx, 0, ctx->btarget);
7760
            break;
7761
        case MIPS_HFLAG_BL:
7762
            /* blikely taken case */
7763
            MIPS_DEBUG("blikely branch taken");
7764
            gen_goto_tb(ctx, 0, ctx->btarget);
7765
            break;
7766
        case MIPS_HFLAG_BC:
7767
            /* Conditional branch */
7768
            MIPS_DEBUG("conditional branch");
7769
            {
7770
                TCGv r_tmp = tcg_temp_local_new(TCG_TYPE_TL);
7771
                int l1 = gen_new_label();
7772

    
7773
                tcg_gen_ld_tl(r_tmp, cpu_env, offsetof(CPUState, bcond));
7774
                tcg_gen_brcondi_tl(TCG_COND_NE, r_tmp, 0, l1);
7775
                tcg_temp_free(r_tmp);
7776
                gen_goto_tb(ctx, 1, ctx->pc + 4);
7777
                gen_set_label(l1);
7778
                gen_goto_tb(ctx, 0, ctx->btarget);
7779
            }
7780
            break;
7781
        case MIPS_HFLAG_BR:
7782
            /* unconditional branch to register */
7783
            MIPS_DEBUG("branch to register");
7784
            gen_breg_pc();
7785
            tcg_gen_exit_tb(0);
7786
            break;
7787
        default:
7788
            MIPS_DEBUG("unknown branch");
7789
            break;
7790
        }
7791
    }
7792
}
7793

    
7794
static always_inline int
7795
gen_intermediate_code_internal (CPUState *env, TranslationBlock *tb,
7796
                                int search_pc)
7797
{
7798
    DisasContext ctx;
7799
    target_ulong pc_start;
7800
    uint16_t *gen_opc_end;
7801
    int j, lj = -1;
7802

    
7803
    if (search_pc && loglevel)
7804
        fprintf (logfile, "search pc %d\n", search_pc);
7805

    
7806
    pc_start = tb->pc;
7807
    /* Leave some spare opc slots for branch handling. */
7808
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE - 16;
7809
    ctx.pc = pc_start;
7810
    ctx.saved_pc = -1;
7811
    ctx.tb = tb;
7812
    ctx.bstate = BS_NONE;
7813
    /* Restore delay slot state from the tb context.  */
7814
    ctx.hflags = (uint32_t)tb->flags; /* FIXME: maybe use 64 bits here? */
7815
    restore_cpu_state(env, &ctx);
7816
#if defined(CONFIG_USER_ONLY)
7817
    ctx.mem_idx = MIPS_HFLAG_UM;
7818
#else
7819
    ctx.mem_idx = ctx.hflags & MIPS_HFLAG_KSU;
7820
#endif
7821
#ifdef DEBUG_DISAS
7822
    if (loglevel & CPU_LOG_TB_CPU) {
7823
        fprintf(logfile, "------------------------------------------------\n");
7824
        /* FIXME: This may print out stale hflags from env... */
7825
        cpu_dump_state(env, logfile, fprintf, 0);
7826
    }
7827
#endif
7828
#ifdef MIPS_DEBUG_DISAS
7829
    if (loglevel & CPU_LOG_TB_IN_ASM)
7830
        fprintf(logfile, "\ntb %p idx %d hflags %04x\n",
7831
                tb, ctx.mem_idx, ctx.hflags);
7832
#endif
7833
    while (ctx.bstate == BS_NONE) {
7834
        if (env->nb_breakpoints > 0) {
7835
            for(j = 0; j < env->nb_breakpoints; j++) {
7836
                if (env->breakpoints[j] == ctx.pc) {
7837
                    save_cpu_state(&ctx, 1);
7838
                    ctx.bstate = BS_BRANCH;
7839
                    tcg_gen_helper_0_i(do_raise_exception, EXCP_DEBUG);
7840
                    /* Include the breakpoint location or the tb won't
7841
                     * be flushed when it must be.  */
7842
                    ctx.pc += 4;
7843
                    goto done_generating;
7844
                }
7845
            }
7846
        }
7847

    
7848
        if (search_pc) {
7849
            j = gen_opc_ptr - gen_opc_buf;
7850
            if (lj < j) {
7851
                lj++;
7852
                while (lj < j)
7853
                    gen_opc_instr_start[lj++] = 0;
7854
            }
7855
            gen_opc_pc[lj] = ctx.pc;
7856
            gen_opc_hflags[lj] = ctx.hflags & MIPS_HFLAG_BMASK;
7857
            gen_opc_instr_start[lj] = 1;
7858
        }
7859
        ctx.opcode = ldl_code(ctx.pc);
7860
        decode_opc(env, &ctx);
7861
        ctx.pc += 4;
7862

    
7863
        if (env->singlestep_enabled)
7864
            break;
7865

    
7866
        if ((ctx.pc & (TARGET_PAGE_SIZE - 1)) == 0)
7867
            break;
7868

    
7869
        if (gen_opc_ptr >= gen_opc_end)
7870
            break;
7871

    
7872
        if (gen_opc_ptr >= gen_opc_end)
7873
            break;
7874

    
7875
#if defined (MIPS_SINGLE_STEP)
7876
        break;
7877
#endif
7878
    }
7879
    if (env->singlestep_enabled) {
7880
        save_cpu_state(&ctx, ctx.bstate == BS_NONE);
7881
        tcg_gen_helper_0_i(do_raise_exception, EXCP_DEBUG);
7882
    } else {
7883
        switch (ctx.bstate) {
7884
        case BS_STOP:
7885
            tcg_gen_helper_0_0(do_interrupt_restart);
7886
            gen_goto_tb(&ctx, 0, ctx.pc);
7887
            break;
7888
        case BS_NONE:
7889
            save_cpu_state(&ctx, 0);
7890
            gen_goto_tb(&ctx, 0, ctx.pc);
7891
            break;
7892
        case BS_EXCP:
7893
            tcg_gen_helper_0_0(do_interrupt_restart);
7894
            tcg_gen_exit_tb(0);
7895
            break;
7896
        case BS_BRANCH:
7897
        default:
7898
            break;
7899
        }
7900
    }
7901
done_generating:
7902
    *gen_opc_ptr = INDEX_op_end;
7903
    if (search_pc) {
7904
        j = gen_opc_ptr - gen_opc_buf;
7905
        lj++;
7906
        while (lj <= j)
7907
            gen_opc_instr_start[lj++] = 0;
7908
    } else {
7909
        tb->size = ctx.pc - pc_start;
7910
    }
7911
#ifdef DEBUG_DISAS
7912
#if defined MIPS_DEBUG_DISAS
7913
    if (loglevel & CPU_LOG_TB_IN_ASM)
7914
        fprintf(logfile, "\n");
7915
#endif
7916
    if (loglevel & CPU_LOG_TB_IN_ASM) {
7917
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
7918
        target_disas(logfile, pc_start, ctx.pc - pc_start, 0);
7919
        fprintf(logfile, "\n");
7920
    }
7921
    if (loglevel & CPU_LOG_TB_CPU) {
7922
        fprintf(logfile, "---------------- %d %08x\n", ctx.bstate, ctx.hflags);
7923
    }
7924
#endif
7925

    
7926
    return 0;
7927
}
7928

    
7929
int gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
7930
{
7931
    return gen_intermediate_code_internal(env, tb, 0);
7932
}
7933

    
7934
int gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
7935
{
7936
    return gen_intermediate_code_internal(env, tb, 1);
7937
}
7938

    
7939
void fpu_dump_state(CPUState *env, FILE *f,
7940
                    int (*fpu_fprintf)(FILE *f, const char *fmt, ...),
7941
                    int flags)
7942
{
7943
    int i;
7944
    int is_fpu64 = !!(env->hflags & MIPS_HFLAG_F64);
7945

    
7946
#define printfpr(fp)                                                        \
7947
    do {                                                                    \
7948
        if (is_fpu64)                                                       \
7949
            fpu_fprintf(f, "w:%08x d:%016lx fd:%13g fs:%13g psu: %13g\n",   \
7950
                        (fp)->w[FP_ENDIAN_IDX], (fp)->d, (fp)->fd,          \
7951
                        (fp)->fs[FP_ENDIAN_IDX], (fp)->fs[!FP_ENDIAN_IDX]); \
7952
        else {                                                              \
7953
            fpr_t tmp;                                                      \
7954
            tmp.w[FP_ENDIAN_IDX] = (fp)->w[FP_ENDIAN_IDX];                  \
7955
            tmp.w[!FP_ENDIAN_IDX] = ((fp) + 1)->w[FP_ENDIAN_IDX];           \
7956
            fpu_fprintf(f, "w:%08x d:%016lx fd:%13g fs:%13g psu:%13g\n",    \
7957
                        tmp.w[FP_ENDIAN_IDX], tmp.d, tmp.fd,                \
7958
                        tmp.fs[FP_ENDIAN_IDX], tmp.fs[!FP_ENDIAN_IDX]);     \
7959
        }                                                                   \
7960
    } while(0)
7961

    
7962

    
7963
    fpu_fprintf(f, "CP1 FCR0 0x%08x  FCR31 0x%08x  SR.FR %d  fp_status 0x%08x(0x%02x)\n",
7964
                env->fpu->fcr0, env->fpu->fcr31, is_fpu64, env->fpu->fp_status,
7965
                get_float_exception_flags(&env->fpu->fp_status));
7966
    fpu_fprintf(f, "FT0: "); printfpr(&env->ft0);
7967
    fpu_fprintf(f, "FT1: "); printfpr(&env->ft1);
7968
    fpu_fprintf(f, "FT2: "); printfpr(&env->ft2);
7969
    for (i = 0; i < 32; (is_fpu64) ? i++ : (i += 2)) {
7970
        fpu_fprintf(f, "%3s: ", fregnames[i]);
7971
        printfpr(&env->fpu->fpr[i]);
7972
    }
7973

    
7974
#undef printfpr
7975
}
7976

    
7977
void dump_fpu (CPUState *env)
7978
{
7979
    if (loglevel) {
7980
        fprintf(logfile,
7981
                "pc=0x" TARGET_FMT_lx " HI=0x" TARGET_FMT_lx
7982
                " LO=0x" TARGET_FMT_lx " ds %04x " TARGET_FMT_lx
7983
                " %04x\n",
7984
                env->PC[env->current_tc], env->HI[env->current_tc][0],
7985
                env->LO[env->current_tc][0], env->hflags, env->btarget,
7986
                env->bcond);
7987
       fpu_dump_state(env, logfile, fprintf, 0);
7988
    }
7989
}
7990

    
7991
#if defined(TARGET_MIPS64) && defined(MIPS_DEBUG_SIGN_EXTENSIONS)
7992
/* Debug help: The architecture requires 32bit code to maintain proper
7993
   sign-extened values on 64bit machines.  */
7994

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

    
7997
void cpu_mips_check_sign_extensions (CPUState *env, FILE *f,
7998
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
7999
                     int flags)
8000
{
8001
    int i;
8002

    
8003
    if (!SIGN_EXT_P(env->PC[env->current_tc]))
8004
        cpu_fprintf(f, "BROKEN: pc=0x" TARGET_FMT_lx "\n", env->PC[env->current_tc]);
8005
    if (!SIGN_EXT_P(env->HI[env->current_tc][0]))
8006
        cpu_fprintf(f, "BROKEN: HI=0x" TARGET_FMT_lx "\n", env->HI[env->current_tc][0]);
8007
    if (!SIGN_EXT_P(env->LO[env->current_tc][0]))
8008
        cpu_fprintf(f, "BROKEN: LO=0x" TARGET_FMT_lx "\n", env->LO[env->current_tc][0]);
8009
    if (!SIGN_EXT_P(env->btarget))
8010
        cpu_fprintf(f, "BROKEN: btarget=0x" TARGET_FMT_lx "\n", env->btarget);
8011

    
8012
    for (i = 0; i < 32; i++) {
8013
        if (!SIGN_EXT_P(env->gpr[env->current_tc][i]))
8014
            cpu_fprintf(f, "BROKEN: %s=0x" TARGET_FMT_lx "\n", regnames[i], env->gpr[env->current_tc][i]);
8015
    }
8016

    
8017
    if (!SIGN_EXT_P(env->CP0_EPC))
8018
        cpu_fprintf(f, "BROKEN: EPC=0x" TARGET_FMT_lx "\n", env->CP0_EPC);
8019
    if (!SIGN_EXT_P(env->CP0_LLAddr))
8020
        cpu_fprintf(f, "BROKEN: LLAddr=0x" TARGET_FMT_lx "\n", env->CP0_LLAddr);
8021
}
8022
#endif
8023

    
8024
void cpu_dump_state (CPUState *env, FILE *f,
8025
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
8026
                     int flags)
8027
{
8028
    int i;
8029

    
8030
    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",
8031
                env->PC[env->current_tc], env->HI[env->current_tc], env->LO[env->current_tc], env->hflags, env->btarget, env->bcond);
8032
    for (i = 0; i < 32; i++) {
8033
        if ((i & 3) == 0)
8034
            cpu_fprintf(f, "GPR%02d:", i);
8035
        cpu_fprintf(f, " %s " TARGET_FMT_lx, regnames[i], env->gpr[env->current_tc][i]);
8036
        if ((i & 3) == 3)
8037
            cpu_fprintf(f, "\n");
8038
    }
8039

    
8040
    cpu_fprintf(f, "CP0 Status  0x%08x Cause   0x%08x EPC    0x" TARGET_FMT_lx "\n",
8041
                env->CP0_Status, env->CP0_Cause, env->CP0_EPC);
8042
    cpu_fprintf(f, "    Config0 0x%08x Config1 0x%08x LLAddr 0x" TARGET_FMT_lx "\n",
8043
                env->CP0_Config0, env->CP0_Config1, env->CP0_LLAddr);
8044
    if (env->hflags & MIPS_HFLAG_FPU)
8045
        fpu_dump_state(env, f, cpu_fprintf, flags);
8046
#if defined(TARGET_MIPS64) && defined(MIPS_DEBUG_SIGN_EXTENSIONS)
8047
    cpu_mips_check_sign_extensions(env, f, cpu_fprintf, flags);
8048
#endif
8049
}
8050

    
8051
static void mips_tcg_init(void)
8052
{
8053
    static int inited;
8054

    
8055
    /* Initialize various static tables. */
8056
    if (inited)
8057
        return;
8058

    
8059
    cpu_env = tcg_global_reg_new(TCG_TYPE_PTR, TCG_AREG0, "env");
8060
    current_tc_gprs = tcg_global_mem_new(TCG_TYPE_PTR,
8061
                                         TCG_AREG0,
8062
                                         offsetof(CPUState, current_tc_gprs),
8063
                                         "current_tc_gprs");
8064
    current_tc_hi = tcg_global_mem_new(TCG_TYPE_PTR,
8065
                                       TCG_AREG0,
8066
                                       offsetof(CPUState, current_tc_hi),
8067
                                       "current_tc_hi");
8068
    current_fpu = tcg_global_mem_new(TCG_TYPE_PTR,
8069
                                     TCG_AREG0,
8070
                                     offsetof(CPUState, fpu),
8071
                                     "current_fpu");
8072
#if TARGET_LONG_BITS > HOST_LONG_BITS
8073
    cpu_T[0] = tcg_global_mem_new(TCG_TYPE_TL,
8074
                                  TCG_AREG0, offsetof(CPUState, t0), "T0");
8075
    cpu_T[1] = tcg_global_mem_new(TCG_TYPE_TL,
8076
                                  TCG_AREG0, offsetof(CPUState, t1), "T1");
8077
#else
8078
    cpu_T[0] = tcg_global_reg_new(TCG_TYPE_TL, TCG_AREG1, "T0");
8079
    cpu_T[1] = tcg_global_reg_new(TCG_TYPE_TL, TCG_AREG2, "T1");
8080
#endif
8081

    
8082
    /* register helpers */
8083
#undef DEF_HELPER
8084
#define DEF_HELPER(ret, name, params) tcg_register_helper(name, #name);
8085
#include "helper.h"
8086

    
8087
    fpu32_T[0] = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0, offsetof(CPUState, ft0.w[FP_ENDIAN_IDX]), "WT0");
8088
    fpu32_T[1] = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0, offsetof(CPUState, ft1.w[FP_ENDIAN_IDX]), "WT1");
8089
    fpu32_T[2] = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0, offsetof(CPUState, ft2.w[FP_ENDIAN_IDX]), "WT2");
8090
    fpu64_T[0] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0, offsetof(CPUState, ft0.d), "DT0");
8091
    fpu64_T[1] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0, offsetof(CPUState, ft1.d), "DT1");
8092
    fpu64_T[2] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0, offsetof(CPUState, ft2.d), "DT2");
8093
    fpu32h_T[0] = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0, offsetof(CPUState, ft0.w[!FP_ENDIAN_IDX]), "WTH0");
8094
    fpu32h_T[1] = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0, offsetof(CPUState, ft1.w[!FP_ENDIAN_IDX]), "WTH1");
8095
    fpu32h_T[2] = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0, offsetof(CPUState, ft2.w[!FP_ENDIAN_IDX]), "WTH2");
8096

    
8097
    inited = 1;
8098
}
8099

    
8100
#include "translate_init.c"
8101

    
8102
CPUMIPSState *cpu_mips_init (const char *cpu_model)
8103
{
8104
    CPUMIPSState *env;
8105
    const mips_def_t *def;
8106

    
8107
    def = cpu_mips_find_by_name(cpu_model);
8108
    if (!def)
8109
        return NULL;
8110
    env = qemu_mallocz(sizeof(CPUMIPSState));
8111
    if (!env)
8112
        return NULL;
8113
    env->cpu_model = def;
8114

    
8115
    cpu_exec_init(env);
8116
    env->cpu_model_str = cpu_model;
8117
    mips_tcg_init();
8118
    cpu_reset(env);
8119
    return env;
8120
}
8121

    
8122
void cpu_reset (CPUMIPSState *env)
8123
{
8124
    memset(env, 0, offsetof(CPUMIPSState, breakpoints));
8125

    
8126
    tlb_flush(env, 1);
8127

    
8128
    /* Minimal init */
8129
#if !defined(CONFIG_USER_ONLY)
8130
    if (env->hflags & MIPS_HFLAG_BMASK) {
8131
        /* If the exception was raised from a delay slot,
8132
         * come back to the jump.  */
8133
        env->CP0_ErrorEPC = env->PC[env->current_tc] - 4;
8134
    } else {
8135
        env->CP0_ErrorEPC = env->PC[env->current_tc];
8136
    }
8137
    env->PC[env->current_tc] = (int32_t)0xBFC00000;
8138
    env->CP0_Wired = 0;
8139
    /* SMP not implemented */
8140
    env->CP0_EBase = 0x80000000;
8141
    env->CP0_Status = (1 << CP0St_BEV) | (1 << CP0St_ERL);
8142
    /* vectored interrupts not implemented, timer on int 7,
8143
       no performance counters. */
8144
    env->CP0_IntCtl = 0xe0000000;
8145
    {
8146
        int i;
8147

    
8148
        for (i = 0; i < 7; i++) {
8149
            env->CP0_WatchLo[i] = 0;
8150
            env->CP0_WatchHi[i] = 0x80000000;
8151
        }
8152
        env->CP0_WatchLo[7] = 0;
8153
        env->CP0_WatchHi[7] = 0;
8154
    }
8155
    /* Count register increments in debug mode, EJTAG version 1 */
8156
    env->CP0_Debug = (1 << CP0DB_CNT) | (0x1 << CP0DB_VER);
8157
#endif
8158
    env->exception_index = EXCP_NONE;
8159
#if defined(CONFIG_USER_ONLY)
8160
    env->hflags = MIPS_HFLAG_UM;
8161
    env->user_mode_only = 1;
8162
#else
8163
    env->hflags = MIPS_HFLAG_CP0;
8164
#endif
8165
    cpu_mips_register(env, env->cpu_model);
8166
}
8167

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