Statistics
| Branch: | Revision:

root / target-mips / translate.c @ c8c2227e

History | View | Annotate | Download (240.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_1i(void *func, TCGv arg)
432
{
433
    TCGv t = tcg_const_i32(arg);
434

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

    
439
static inline void tcg_gen_helper_0_2ii(void *func, TCGv arg1, TCGv arg2)
440
{
441
    TCGv t1 = tcg_const_i32(arg1);
442
    TCGv t2 = tcg_const_i32(arg2);
443

    
444
    tcg_gen_helper_0_2(func, t1, t2);
445
    tcg_temp_free(t1);
446
    tcg_temp_free(t2);
447
}
448

    
449
typedef struct DisasContext {
450
    struct TranslationBlock *tb;
451
    target_ulong pc, saved_pc;
452
    uint32_t opcode;
453
    uint32_t fp_status;
454
    /* Routine used to access memory */
455
    int mem_idx;
456
    uint32_t hflags, saved_hflags;
457
    int bstate;
458
    target_ulong btarget;
459
} DisasContext;
460

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

    
470
static const char *regnames[] =
471
    { "r0", "at", "v0", "v1", "a0", "a1", "a2", "a3",
472
      "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
473
      "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
474
      "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra", };
475

    
476
static const char *fregnames[] =
477
    { "f0",  "f1",  "f2",  "f3",  "f4",  "f5",  "f6",  "f7",
478
      "f8",  "f9",  "f10", "f11", "f12", "f13", "f14", "f15",
479
      "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
480
      "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31", };
481

    
482
#ifdef MIPS_DEBUG_DISAS
483
#define MIPS_DEBUG(fmt, args...)                                              \
484
do {                                                                          \
485
    if (loglevel & CPU_LOG_TB_IN_ASM) {                                       \
486
        fprintf(logfile, TARGET_FMT_lx ": %08x " fmt "\n",                    \
487
                ctx->pc, ctx->opcode , ##args);                               \
488
    }                                                                         \
489
} while (0)
490
#else
491
#define MIPS_DEBUG(fmt, args...) do { } while(0)
492
#endif
493

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

    
500
/* General purpose registers moves. */
501
static inline void gen_load_gpr (TCGv t, int reg)
502
{
503
    if (reg == 0)
504
        tcg_gen_movi_tl(t, 0);
505
    else
506
        tcg_gen_ld_tl(t, current_tc_gprs, sizeof(target_ulong) * reg);
507
}
508

    
509
static inline void gen_store_gpr (TCGv t, int reg)
510
{
511
    if (reg != 0)
512
        tcg_gen_st_tl(t, current_tc_gprs, sizeof(target_ulong) * reg);
513
}
514

    
515
/* Moves to/from HI and LO registers.  */
516
static inline void gen_load_LO (TCGv t, int reg)
517
{
518
    tcg_gen_ld_tl(t, current_tc_hi,
519
                  offsetof(CPUState, LO)
520
                  - offsetof(CPUState, HI)
521
                  + sizeof(target_ulong) * reg);
522
}
523

    
524
static inline void gen_store_LO (TCGv t, int reg)
525
{
526
    tcg_gen_st_tl(t, current_tc_hi,
527
                  offsetof(CPUState, LO)
528
                  - offsetof(CPUState, HI)
529
                  + sizeof(target_ulong) * reg);
530
}
531

    
532
static inline void gen_load_HI (TCGv t, int reg)
533
{
534
    tcg_gen_ld_tl(t, current_tc_hi, sizeof(target_ulong) * reg);
535
}
536

    
537
static inline void gen_store_HI (TCGv t, int reg)
538
{
539
    tcg_gen_st_tl(t, current_tc_hi, sizeof(target_ulong) * reg);
540
}
541

    
542
/* Moves to/from shadow registers. */
543
static inline void gen_load_srsgpr (TCGv t, int reg)
544
{
545
    if (reg == 0)
546
        tcg_gen_movi_tl(t, 0);
547
    else {
548
        TCGv r_tmp = tcg_temp_new(TCG_TYPE_I32);
549

    
550
        tcg_gen_ld_i32(r_tmp, cpu_env, offsetof(CPUState, CP0_SRSCtl));
551
        tcg_gen_shri_i32(r_tmp, r_tmp, CP0SRSCtl_PSS);
552
        tcg_gen_andi_i32(r_tmp, r_tmp, 0xf);
553
        tcg_gen_muli_i32(r_tmp, r_tmp, sizeof(target_ulong) * 32);
554
        tcg_gen_add_i32(r_tmp, cpu_env, r_tmp);
555

    
556
        tcg_gen_ld_tl(t, r_tmp, sizeof(target_ulong) * reg);
557
        tcg_temp_free(r_tmp);
558
    }
559
}
560

    
561
static inline void gen_store_srsgpr (TCGv t, int reg)
562
{
563
    if (reg != 0) {
564
        TCGv r_tmp = tcg_temp_new(TCG_TYPE_I32);
565

    
566
        tcg_gen_ld_i32(r_tmp, cpu_env, offsetof(CPUState, CP0_SRSCtl));
567
        tcg_gen_shri_i32(r_tmp, r_tmp, CP0SRSCtl_PSS);
568
        tcg_gen_andi_i32(r_tmp, r_tmp, 0xf);
569
        tcg_gen_muli_i32(r_tmp, r_tmp, sizeof(target_ulong) * 32);
570
        tcg_gen_add_i32(r_tmp, cpu_env, r_tmp);
571

    
572
        tcg_gen_st_tl(t, r_tmp, sizeof(target_ulong) * reg);
573
        tcg_temp_free(r_tmp);
574
    }
575
}
576

    
577
/* Floating point register moves. */
578
static inline void gen_load_fpr32 (TCGv t, int reg)
579
{
580
    tcg_gen_ld_i32(t, current_fpu, 8 * reg + 4 * FP_ENDIAN_IDX);
581
}
582

    
583
static inline void gen_store_fpr32 (TCGv t, int reg)
584
{
585
    tcg_gen_st_i32(t, current_fpu, 8 * reg + 4 * FP_ENDIAN_IDX);
586
}
587

    
588
static inline void gen_load_fpr64 (DisasContext *ctx, TCGv t, int reg)
589
{
590
    if (ctx->hflags & MIPS_HFLAG_F64) {
591
        tcg_gen_ld_i64(t, current_fpu, 8 * reg);
592
    } else {
593
        TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
594
        TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I64);
595

    
596
        tcg_gen_ld_i32(r_tmp1, current_fpu, 8 * (reg | 1) + 4 * FP_ENDIAN_IDX);
597
        tcg_gen_extu_i32_i64(t, r_tmp1);
598
        tcg_gen_shli_i64(t, t, 32);
599
        tcg_gen_ld_i32(r_tmp1, current_fpu, 8 * (reg & ~1) + 4 * FP_ENDIAN_IDX);
600
        tcg_gen_extu_i32_i64(r_tmp2, r_tmp1);
601
        tcg_gen_or_i64(t, t, r_tmp2);
602
        tcg_temp_free(r_tmp1);
603
        tcg_temp_free(r_tmp2);
604
    }
605
}
606

    
607
static inline void gen_store_fpr64 (DisasContext *ctx, TCGv t, int reg)
608
{
609
    if (ctx->hflags & MIPS_HFLAG_F64) {
610
        tcg_gen_st_i64(t, current_fpu, 8 * reg);
611
    } else {
612
        TCGv r_tmp = tcg_temp_new(TCG_TYPE_I32);
613

    
614
        tcg_gen_trunc_i64_i32(r_tmp, t);
615
        tcg_gen_st_i32(r_tmp, current_fpu, 8 * (reg & ~1) + 4 * FP_ENDIAN_IDX);
616
        tcg_gen_shri_i64(t, t, 32);
617
        tcg_gen_trunc_i64_i32(r_tmp, t);
618
        tcg_gen_st_i32(r_tmp, current_fpu, 8 * (reg | 1) + 4 * FP_ENDIAN_IDX);
619
        tcg_temp_free(r_tmp);
620
    }
621
}
622

    
623
static inline void gen_load_fpr32h (TCGv t, int reg)
624
{
625
    tcg_gen_ld_i32(t, current_fpu, 8 * reg + 4 * !FP_ENDIAN_IDX);
626
}
627

    
628
static inline void gen_store_fpr32h (TCGv t, int reg)
629
{
630
    tcg_gen_st_i32(t, current_fpu, 8 * reg + 4 * !FP_ENDIAN_IDX);
631
}
632

    
633
static inline void get_fp_cond (TCGv t)
634
{
635
    TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
636
    TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I32);
637

    
638
    tcg_gen_ld_i32(r_tmp1, current_fpu, offsetof(CPUMIPSFPUContext, fcr31));
639
    tcg_gen_shri_i32(r_tmp2, r_tmp1, 24);
640
    tcg_gen_andi_i32(r_tmp2, r_tmp2, 0xfe);
641
    tcg_gen_shri_i32(r_tmp1, r_tmp1, 23);
642
    tcg_gen_andi_i32(r_tmp1, r_tmp1, 0x1);
643
    tcg_gen_or_i32(t, r_tmp1, r_tmp2);
644
    tcg_temp_free(r_tmp1);
645
    tcg_temp_free(r_tmp2);
646
}
647

    
648
#define FOP_CONDS(type, fmt)                                              \
649
static GenOpFunc1 * fcmp ## type ## _ ## fmt ## _table[16] = {            \
650
    do_cmp ## type ## _ ## fmt ## _f,                                     \
651
    do_cmp ## type ## _ ## fmt ## _un,                                    \
652
    do_cmp ## type ## _ ## fmt ## _eq,                                    \
653
    do_cmp ## type ## _ ## fmt ## _ueq,                                   \
654
    do_cmp ## type ## _ ## fmt ## _olt,                                   \
655
    do_cmp ## type ## _ ## fmt ## _ult,                                   \
656
    do_cmp ## type ## _ ## fmt ## _ole,                                   \
657
    do_cmp ## type ## _ ## fmt ## _ule,                                   \
658
    do_cmp ## type ## _ ## fmt ## _sf,                                    \
659
    do_cmp ## type ## _ ## fmt ## _ngle,                                  \
660
    do_cmp ## type ## _ ## fmt ## _seq,                                   \
661
    do_cmp ## type ## _ ## fmt ## _ngl,                                   \
662
    do_cmp ## type ## _ ## fmt ## _lt,                                    \
663
    do_cmp ## type ## _ ## fmt ## _nge,                                   \
664
    do_cmp ## type ## _ ## fmt ## _le,                                    \
665
    do_cmp ## type ## _ ## fmt ## _ngt,                                   \
666
};                                                                        \
667
static inline void gen_cmp ## type ## _ ## fmt(int n, long cc)            \
668
{                                                                         \
669
    tcg_gen_helper_0_1i(fcmp ## type ## _ ## fmt ## _table[n], cc);       \
670
}
671

    
672
FOP_CONDS(, d)
673
FOP_CONDS(abs, d)
674
FOP_CONDS(, s)
675
FOP_CONDS(abs, s)
676
FOP_CONDS(, ps)
677
FOP_CONDS(abs, ps)
678
#undef FOP_CONDS
679

    
680
/* Tests */
681
#define OP_COND(name, cond)                                   \
682
void glue(gen_op_, name) (void)                               \
683
{                                                             \
684
    int l1 = gen_new_label();                                 \
685
    int l2 = gen_new_label();                                 \
686
                                                              \
687
    tcg_gen_brcond_tl(cond, cpu_T[0], cpu_T[1], l1);          \
688
    tcg_gen_movi_tl(cpu_T[0], 0);                             \
689
    tcg_gen_br(l2);                                           \
690
    gen_set_label(l1);                                        \
691
    tcg_gen_movi_tl(cpu_T[0], 1);                             \
692
    gen_set_label(l2);                                        \
693
}
694
OP_COND(eq, TCG_COND_EQ);
695
OP_COND(ne, TCG_COND_NE);
696
OP_COND(ge, TCG_COND_GE);
697
OP_COND(geu, TCG_COND_GEU);
698
OP_COND(lt, TCG_COND_LT);
699
OP_COND(ltu, TCG_COND_LTU);
700
#undef OP_COND
701

    
702
#define OP_CONDI(name, cond)                                  \
703
void glue(gen_op_, name) (target_ulong val)                   \
704
{                                                             \
705
    int l1 = gen_new_label();                                 \
706
    int l2 = gen_new_label();                                 \
707
                                                              \
708
    tcg_gen_brcondi_tl(cond, cpu_T[0], val, l1);              \
709
    tcg_gen_movi_tl(cpu_T[0], 0);                             \
710
    tcg_gen_br(l2);                                           \
711
    gen_set_label(l1);                                        \
712
    tcg_gen_movi_tl(cpu_T[0], 1);                             \
713
    gen_set_label(l2);                                        \
714
}
715
OP_CONDI(lti, TCG_COND_LT);
716
OP_CONDI(ltiu, TCG_COND_LTU);
717
#undef OP_CONDI
718

    
719
#define OP_CONDZ(name, cond)                                  \
720
void glue(gen_op_, name) (void)                               \
721
{                                                             \
722
    int l1 = gen_new_label();                                 \
723
    int l2 = gen_new_label();                                 \
724
                                                              \
725
    tcg_gen_brcondi_tl(cond, cpu_T[0], 0, l1);                \
726
    tcg_gen_movi_tl(cpu_T[0], 0);                             \
727
    tcg_gen_br(l2);                                           \
728
    gen_set_label(l1);                                        \
729
    tcg_gen_movi_tl(cpu_T[0], 1);                             \
730
    gen_set_label(l2);                                        \
731
}
732
OP_CONDZ(gez, TCG_COND_GE);
733
OP_CONDZ(gtz, TCG_COND_GT);
734
OP_CONDZ(lez, TCG_COND_LE);
735
OP_CONDZ(ltz, TCG_COND_LT);
736
#undef OP_CONDZ
737

    
738
static inline void gen_save_pc(target_ulong pc)
739
{
740
    TCGv r_tmp = tcg_temp_new(TCG_TYPE_TL);
741
    TCGv r_tc_off = tcg_temp_new(TCG_TYPE_I32);
742
    TCGv r_tc_off_ptr = tcg_temp_new(TCG_TYPE_PTR);
743
    TCGv r_ptr = tcg_temp_new(TCG_TYPE_PTR);
744

    
745
    tcg_gen_movi_tl(r_tmp, pc);
746
    tcg_gen_ld_i32(r_tc_off, cpu_env, offsetof(CPUState, current_tc));
747
    tcg_gen_muli_i32(r_tc_off, r_tc_off, sizeof(target_ulong));
748
    tcg_gen_ext_i32_ptr(r_tc_off_ptr, r_tc_off);
749
    tcg_gen_add_ptr(r_ptr, cpu_env, r_tc_off_ptr);
750
    tcg_gen_st_tl(r_tmp, r_ptr, offsetof(CPUState, PC));
751
    tcg_temp_free(r_tc_off);
752
    tcg_temp_free(r_tc_off_ptr);
753
    tcg_temp_free(r_ptr);
754
    tcg_temp_free(r_tmp);
755
}
756

    
757
static inline void gen_breg_pc(void)
758
{
759
    TCGv r_tmp = tcg_temp_new(TCG_TYPE_TL);
760
    TCGv r_tc_off = tcg_temp_new(TCG_TYPE_I32);
761
    TCGv r_tc_off_ptr = tcg_temp_new(TCG_TYPE_PTR);
762
    TCGv r_ptr = tcg_temp_new(TCG_TYPE_PTR);
763

    
764
    tcg_gen_ld_tl(r_tmp, cpu_env, offsetof(CPUState, btarget));
765
    tcg_gen_ld_i32(r_tc_off, cpu_env, offsetof(CPUState, current_tc));
766
    tcg_gen_muli_i32(r_tc_off, r_tc_off, sizeof(target_ulong));
767
    tcg_gen_ext_i32_ptr(r_tc_off_ptr, r_tc_off);
768
    tcg_gen_add_ptr(r_ptr, cpu_env, r_tc_off_ptr);
769
    tcg_gen_st_tl(r_tmp, r_ptr, offsetof(CPUState, PC));
770
    tcg_temp_free(r_tc_off);
771
    tcg_temp_free(r_tc_off_ptr);
772
    tcg_temp_free(r_ptr);
773
    tcg_temp_free(r_tmp);
774
}
775

    
776
static inline void gen_save_btarget(target_ulong btarget)
777
{
778
    TCGv r_tmp = tcg_temp_new(TCG_TYPE_TL);
779

    
780
    tcg_gen_movi_tl(r_tmp, btarget);
781
    tcg_gen_st_tl(r_tmp, cpu_env, offsetof(CPUState, btarget));
782
    tcg_temp_free(r_tmp);
783
}
784

    
785
static always_inline void gen_save_breg_target(int reg)
786
{
787
    TCGv r_tmp = tcg_temp_new(TCG_TYPE_TL);
788

    
789
    gen_load_gpr(r_tmp, reg);
790
    tcg_gen_st_tl(r_tmp, cpu_env, offsetof(CPUState, btarget));
791
    tcg_temp_free(r_tmp);
792
}
793

    
794
static always_inline void save_cpu_state (DisasContext *ctx, int do_save_pc)
795
{
796
#if defined MIPS_DEBUG_DISAS
797
    if (loglevel & CPU_LOG_TB_IN_ASM) {
798
            fprintf(logfile, "hflags %08x saved %08x\n",
799
                    ctx->hflags, ctx->saved_hflags);
800
    }
801
#endif
802
    if (do_save_pc && ctx->pc != ctx->saved_pc) {
803
        gen_save_pc(ctx->pc);
804
        ctx->saved_pc = ctx->pc;
805
    }
806
    if (ctx->hflags != ctx->saved_hflags) {
807
        TCGv r_tmp = tcg_temp_new(TCG_TYPE_I32);
808

    
809
        tcg_gen_movi_i32(r_tmp, ctx->hflags);
810
        tcg_gen_st_i32(r_tmp, cpu_env, offsetof(CPUState, hflags));
811
        tcg_temp_free(r_tmp);
812
        ctx->saved_hflags = ctx->hflags;
813
        switch (ctx->hflags & MIPS_HFLAG_BMASK) {
814
        case MIPS_HFLAG_BR:
815
            break;
816
        case MIPS_HFLAG_BC:
817
        case MIPS_HFLAG_BL:
818
        case MIPS_HFLAG_B:
819
            gen_save_btarget(ctx->btarget);
820
            break;
821
        }
822
    }
823
}
824

    
825
static always_inline void restore_cpu_state (CPUState *env, DisasContext *ctx)
826
{
827
    ctx->saved_hflags = ctx->hflags;
828
    switch (ctx->hflags & MIPS_HFLAG_BMASK) {
829
    case MIPS_HFLAG_BR:
830
        break;
831
    case MIPS_HFLAG_BC:
832
    case MIPS_HFLAG_BL:
833
    case MIPS_HFLAG_B:
834
        ctx->btarget = env->btarget;
835
        break;
836
    }
837
}
838

    
839
static always_inline void
840
generate_exception_err (DisasContext *ctx, int excp, int err)
841
{
842
    save_cpu_state(ctx, 1);
843
    tcg_gen_helper_0_2ii(do_raise_exception_err, excp, err);
844
    tcg_gen_helper_0_0(do_interrupt_restart);
845
    tcg_gen_exit_tb(0);
846
}
847

    
848
static always_inline void
849
generate_exception (DisasContext *ctx, int excp)
850
{
851
    save_cpu_state(ctx, 1);
852
    tcg_gen_helper_0_1i(do_raise_exception, excp);
853
    tcg_gen_helper_0_0(do_interrupt_restart);
854
    tcg_gen_exit_tb(0);
855
}
856

    
857
/* Addresses computation */
858
static inline void gen_op_addr_add (void)
859
{
860
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
861

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

    
870
        tcg_gen_ld_i32(r_tmp, cpu_env, offsetof(CPUState, hflags));
871
        tcg_gen_andi_i32(r_tmp, r_tmp, MIPS_HFLAG_KSU);
872
        tcg_gen_brcondi_i32(TCG_COND_NE, r_tmp, MIPS_HFLAG_UM, l1);
873
        tcg_gen_ld_i32(r_tmp, cpu_env, offsetof(CPUState, CP0_Status));
874
        tcg_gen_andi_i32(r_tmp, r_tmp, (1 << CP0St_UX));
875
        tcg_gen_brcondi_i32(TCG_COND_NE, r_tmp, 0, l1);
876
        tcg_temp_free(r_tmp);
877
        tcg_gen_ext32s_i64(cpu_T[0], cpu_T[0]);
878
        gen_set_label(l1);
879
    }
880
#endif
881
}
882

    
883
static always_inline void check_cp0_enabled(DisasContext *ctx)
884
{
885
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_CP0)))
886
        generate_exception_err(ctx, EXCP_CpU, 1);
887
}
888

    
889
static always_inline void check_cp1_enabled(DisasContext *ctx)
890
{
891
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_FPU)))
892
        generate_exception_err(ctx, EXCP_CpU, 1);
893
}
894

    
895
/* Verify that the processor is running with COP1X instructions enabled.
896
   This is associated with the nabla symbol in the MIPS32 and MIPS64
897
   opcode tables.  */
898

    
899
static always_inline void check_cop1x(DisasContext *ctx)
900
{
901
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_COP1X)))
902
        generate_exception(ctx, EXCP_RI);
903
}
904

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

    
908
static always_inline void check_cp1_64bitmode(DisasContext *ctx)
909
{
910
    if (unlikely(~ctx->hflags & (MIPS_HFLAG_F64 | MIPS_HFLAG_COP1X)))
911
        generate_exception(ctx, EXCP_RI);
912
}
913

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

    
931
/* This code generates a "reserved instruction" exception if the
932
   CPU does not support the instruction set corresponding to flags. */
933
static always_inline void check_insn(CPUState *env, DisasContext *ctx, int flags)
934
{
935
    if (unlikely(!(env->insn_flags & flags)))
936
        generate_exception(ctx, EXCP_RI);
937
}
938

    
939
/* This code generates a "reserved instruction" exception if 64-bit
940
   instructions are not enabled. */
941
static always_inline void check_mips_64(DisasContext *ctx)
942
{
943
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_64)))
944
        generate_exception(ctx, EXCP_RI);
945
}
946

    
947
/* load/store instructions. */
948
#define OP_LD(insn,fname)                                        \
949
void inline op_ldst_##insn(DisasContext *ctx)                    \
950
{                                                                \
951
    tcg_gen_qemu_##fname(cpu_T[0], cpu_T[0], ctx->mem_idx);      \
952
}
953
OP_LD(lb,ld8s);
954
OP_LD(lbu,ld8u);
955
OP_LD(lh,ld16s);
956
OP_LD(lhu,ld16u);
957
OP_LD(lw,ld32s);
958
#if defined(TARGET_MIPS64)
959
OP_LD(lwu,ld32u);
960
OP_LD(ld,ld64);
961
#endif
962
#undef OP_LD
963

    
964
#define OP_ST(insn,fname)                                        \
965
void inline op_ldst_##insn(DisasContext *ctx)                    \
966
{                                                                \
967
    tcg_gen_qemu_##fname(cpu_T[1], cpu_T[0], ctx->mem_idx);      \
968
}
969
OP_ST(sb,st8);
970
OP_ST(sh,st16);
971
OP_ST(sw,st32);
972
#if defined(TARGET_MIPS64)
973
OP_ST(sd,st64);
974
#endif
975
#undef OP_ST
976

    
977
#define OP_LD_ATOMIC(insn,fname)                                        \
978
void inline op_ldst_##insn(DisasContext *ctx)                           \
979
{                                                                       \
980
    tcg_gen_mov_tl(cpu_T[1], cpu_T[0]);                                 \
981
    tcg_gen_qemu_##fname(cpu_T[0], cpu_T[0], ctx->mem_idx);             \
982
    tcg_gen_st_tl(cpu_T[1], cpu_env, offsetof(CPUState, CP0_LLAddr));   \
983
}
984
OP_LD_ATOMIC(ll,ld32s);
985
#if defined(TARGET_MIPS64)
986
OP_LD_ATOMIC(lld,ld64);
987
#endif
988
#undef OP_LD_ATOMIC
989

    
990
#define OP_ST_ATOMIC(insn,fname,almask)                                 \
991
void inline op_ldst_##insn(DisasContext *ctx)                           \
992
{                                                                       \
993
    TCGv r_tmp = tcg_temp_local_new(TCG_TYPE_TL);                       \
994
    int l1 = gen_new_label();                                           \
995
    int l2 = gen_new_label();                                           \
996
    int l3 = gen_new_label();                                           \
997
                                                                        \
998
    tcg_gen_andi_tl(r_tmp, cpu_T[0], almask);                           \
999
    tcg_gen_brcondi_tl(TCG_COND_EQ, r_tmp, 0, l1);                      \
1000
    tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_BadVAddr)); \
1001
    generate_exception(ctx, EXCP_AdES);                                 \
1002
    gen_set_label(l1);                                                  \
1003
    tcg_gen_ld_tl(r_tmp, cpu_env, offsetof(CPUState, CP0_LLAddr));      \
1004
    tcg_gen_brcond_tl(TCG_COND_NE, cpu_T[0], r_tmp, l2);                \
1005
    tcg_temp_free(r_tmp);                                               \
1006
    tcg_gen_qemu_##fname(cpu_T[1], cpu_T[0], ctx->mem_idx);             \
1007
    tcg_gen_movi_tl(cpu_T[0], 1);                                       \
1008
    tcg_gen_br(l3);                                                     \
1009
    gen_set_label(l2);                                                  \
1010
    tcg_gen_movi_tl(cpu_T[0], 0);                                       \
1011
    gen_set_label(l3);                                                  \
1012
}
1013
OP_ST_ATOMIC(sc,st32,0x3);
1014
#if defined(TARGET_MIPS64)
1015
OP_ST_ATOMIC(scd,st64,0x7);
1016
#endif
1017
#undef OP_ST_ATOMIC
1018

    
1019
/* Load and store */
1020
static void gen_ldst (DisasContext *ctx, uint32_t opc, int rt,
1021
                      int base, int16_t offset)
1022
{
1023
    const char *opn = "ldst";
1024

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

    
1178
/* Load and store */
1179
static void gen_flt_ldst (DisasContext *ctx, uint32_t opc, int ft,
1180
                      int base, int16_t offset)
1181
{
1182
    const char *opn = "flt_ldst";
1183

    
1184
    if (base == 0) {
1185
        tcg_gen_movi_tl(cpu_T[0], offset);
1186
    } else if (offset == 0) {
1187
        gen_load_gpr(cpu_T[0], base);
1188
    } else {
1189
        gen_load_gpr(cpu_T[0], base);
1190
        tcg_gen_movi_tl(cpu_T[1], offset);
1191
        gen_op_addr_add();
1192
    }
1193
    /* Don't do NOP if destination is zero: we must perform the actual
1194
       memory access. */
1195
    switch (opc) {
1196
    case OPC_LWC1:
1197
        tcg_gen_qemu_ld32s(fpu32_T[0], cpu_T[0], ctx->mem_idx);
1198
        gen_store_fpr32(fpu32_T[0], ft);
1199
        opn = "lwc1";
1200
        break;
1201
    case OPC_SWC1:
1202
        gen_load_fpr32(fpu32_T[0], ft);
1203
        tcg_gen_qemu_st32(fpu32_T[0], cpu_T[0], ctx->mem_idx);
1204
        opn = "swc1";
1205
        break;
1206
    case OPC_LDC1:
1207
        tcg_gen_qemu_ld64(fpu64_T[0], cpu_T[0], ctx->mem_idx);
1208
        gen_store_fpr64(ctx, fpu64_T[0], ft);
1209
        opn = "ldc1";
1210
        break;
1211
    case OPC_SDC1:
1212
        gen_load_fpr64(ctx, fpu64_T[0], ft);
1213
        tcg_gen_qemu_st64(fpu64_T[0], cpu_T[0], ctx->mem_idx);
1214
        opn = "sdc1";
1215
        break;
1216
    default:
1217
        MIPS_INVAL(opn);
1218
        generate_exception(ctx, EXCP_RI);
1219
        return;
1220
    }
1221
    MIPS_DEBUG("%s %s, %d(%s)", opn, fregnames[ft], offset, regnames[base]);
1222
}
1223

    
1224
/* Arithmetic with immediate operand */
1225
static void gen_arith_imm (CPUState *env, DisasContext *ctx, uint32_t opc,
1226
                           int rt, int rs, int16_t imm)
1227
{
1228
    target_ulong uimm;
1229
    const char *opn = "imm arith";
1230

    
1231
    if (rt == 0 && opc != OPC_ADDI && opc != OPC_DADDI) {
1232
        /* If no destination, treat it as a NOP.
1233
           For addi, we must generate the overflow exception when needed. */
1234
        MIPS_DEBUG("NOP");
1235
        return;
1236
    }
1237
    uimm = (uint16_t)imm;
1238
    switch (opc) {
1239
    case OPC_ADDI:
1240
    case OPC_ADDIU:
1241
#if defined(TARGET_MIPS64)
1242
    case OPC_DADDI:
1243
    case OPC_DADDIU:
1244
#endif
1245
    case OPC_SLTI:
1246
    case OPC_SLTIU:
1247
        uimm = (target_long)imm; /* Sign extend to 32/64 bits */
1248
        tcg_gen_movi_tl(cpu_T[1], uimm);
1249
        /* Fall through. */
1250
    case OPC_ANDI:
1251
    case OPC_ORI:
1252
    case OPC_XORI:
1253
        gen_load_gpr(cpu_T[0], rs);
1254
        break;
1255
    case OPC_LUI:
1256
        tcg_gen_movi_tl(cpu_T[0], imm << 16);
1257
        break;
1258
    case OPC_SLL:
1259
    case OPC_SRA:
1260
    case OPC_SRL:
1261
#if defined(TARGET_MIPS64)
1262
    case OPC_DSLL:
1263
    case OPC_DSRA:
1264
    case OPC_DSRL:
1265
    case OPC_DSLL32:
1266
    case OPC_DSRA32:
1267
    case OPC_DSRL32:
1268
#endif
1269
        uimm &= 0x1f;
1270
        gen_load_gpr(cpu_T[0], rs);
1271
        break;
1272
    }
1273
    switch (opc) {
1274
    case OPC_ADDI:
1275
        {
1276
            TCGv r_tmp1 = tcg_temp_local_new(TCG_TYPE_TL);
1277
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_TL);
1278
            int l1 = gen_new_label();
1279

    
1280
            save_cpu_state(ctx, 1);
1281
            tcg_gen_ext32s_tl(r_tmp1, cpu_T[0]);
1282
            tcg_gen_addi_tl(cpu_T[0], r_tmp1, uimm);
1283

    
1284
            tcg_gen_xori_tl(r_tmp1, r_tmp1, uimm);
1285
            tcg_gen_xori_tl(r_tmp1, r_tmp1, -1);
1286
            tcg_gen_xori_tl(r_tmp2, cpu_T[0], uimm);
1287
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
1288
            tcg_temp_free(r_tmp2);
1289
            tcg_gen_shri_tl(r_tmp1, r_tmp1, 31);
1290
            tcg_gen_brcondi_tl(TCG_COND_EQ, r_tmp1, 0, l1);
1291
            tcg_temp_free(r_tmp1);
1292
            /* operands of same sign, result different sign */
1293
            generate_exception(ctx, EXCP_OVERFLOW);
1294
            gen_set_label(l1);
1295

    
1296
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1297
        }
1298
        opn = "addi";
1299
        break;
1300
    case OPC_ADDIU:
1301
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1302
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], uimm);
1303
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1304
        opn = "addiu";
1305
        break;
1306
#if defined(TARGET_MIPS64)
1307
    case OPC_DADDI:
1308
        {
1309
            TCGv r_tmp1 = tcg_temp_local_new(TCG_TYPE_TL);
1310
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_TL);
1311
            int l1 = gen_new_label();
1312

    
1313
            save_cpu_state(ctx, 1);
1314
            tcg_gen_mov_tl(r_tmp1, cpu_T[0]);
1315
            tcg_gen_addi_tl(cpu_T[0], cpu_T[0], uimm);
1316

    
1317
            tcg_gen_xori_tl(r_tmp1, r_tmp1, uimm);
1318
            tcg_gen_xori_tl(r_tmp1, r_tmp1, -1);
1319
            tcg_gen_xori_tl(r_tmp2, cpu_T[0], uimm);
1320
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
1321
            tcg_temp_free(r_tmp2);
1322
            tcg_gen_shri_tl(r_tmp1, r_tmp1, 63);
1323
            tcg_gen_brcondi_tl(TCG_COND_EQ, r_tmp1, 0, l1);
1324
            tcg_temp_free(r_tmp1);
1325
            /* operands of same sign, result different sign */
1326
            generate_exception(ctx, EXCP_OVERFLOW);
1327
            gen_set_label(l1);
1328
        }
1329
        opn = "daddi";
1330
        break;
1331
    case OPC_DADDIU:
1332
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], uimm);
1333
        opn = "daddiu";
1334
        break;
1335
#endif
1336
    case OPC_SLTI:
1337
        gen_op_lti(uimm);
1338
        opn = "slti";
1339
        break;
1340
    case OPC_SLTIU:
1341
        gen_op_ltiu(uimm);
1342
        opn = "sltiu";
1343
        break;
1344
    case OPC_ANDI:
1345
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], uimm);
1346
        opn = "andi";
1347
        break;
1348
    case OPC_ORI:
1349
        tcg_gen_ori_tl(cpu_T[0], cpu_T[0], uimm);
1350
        opn = "ori";
1351
        break;
1352
    case OPC_XORI:
1353
        tcg_gen_xori_tl(cpu_T[0], cpu_T[0], uimm);
1354
        opn = "xori";
1355
        break;
1356
    case OPC_LUI:
1357
        opn = "lui";
1358
        break;
1359
    case OPC_SLL:
1360
        tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
1361
        tcg_gen_shli_tl(cpu_T[0], cpu_T[0], uimm);
1362
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1363
        opn = "sll";
1364
        break;
1365
    case OPC_SRA:
1366
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1367
        tcg_gen_sari_tl(cpu_T[0], cpu_T[0], uimm);
1368
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1369
        opn = "sra";
1370
        break;
1371
    case OPC_SRL:
1372
        switch ((ctx->opcode >> 21) & 0x1f) {
1373
        case 0:
1374
            tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
1375
            tcg_gen_shri_tl(cpu_T[0], cpu_T[0], uimm);
1376
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1377
            opn = "srl";
1378
            break;
1379
        case 1:
1380
            /* rotr is decoded as srl on non-R2 CPUs */
1381
            if (env->insn_flags & ISA_MIPS32R2) {
1382
                if (uimm != 0) {
1383
                    TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
1384
                    TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I32);
1385

    
1386
                    tcg_gen_trunc_tl_i32(r_tmp1, cpu_T[0]);
1387
                    tcg_gen_movi_i32(r_tmp2, 0x20);
1388
                    tcg_gen_subi_i32(r_tmp2, r_tmp2, uimm);
1389
                    tcg_gen_shl_i32(r_tmp2, r_tmp1, r_tmp2);
1390
                    tcg_gen_shri_i32(r_tmp1, r_tmp1, uimm);
1391
                    tcg_gen_or_i32(r_tmp1, r_tmp1, r_tmp2);
1392
                    tcg_gen_ext_i32_tl(cpu_T[0], r_tmp1);
1393
                    tcg_temp_free(r_tmp1);
1394
                    tcg_temp_free(r_tmp2);
1395
                }
1396
                opn = "rotr";
1397
            } else {
1398
                tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
1399
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], uimm);
1400
                tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1401
                opn = "srl";
1402
            }
1403
            break;
1404
        default:
1405
            MIPS_INVAL("invalid srl flag");
1406
            generate_exception(ctx, EXCP_RI);
1407
            break;
1408
        }
1409
        break;
1410
#if defined(TARGET_MIPS64)
1411
    case OPC_DSLL:
1412
        tcg_gen_shli_tl(cpu_T[0], cpu_T[0], uimm);
1413
        opn = "dsll";
1414
        break;
1415
    case OPC_DSRA:
1416
        tcg_gen_sari_tl(cpu_T[0], cpu_T[0], uimm);
1417
        opn = "dsra";
1418
        break;
1419
    case OPC_DSRL:
1420
        switch ((ctx->opcode >> 21) & 0x1f) {
1421
        case 0:
1422
            tcg_gen_shri_tl(cpu_T[0], cpu_T[0], uimm);
1423
            opn = "dsrl";
1424
            break;
1425
        case 1:
1426
            /* drotr is decoded as dsrl on non-R2 CPUs */
1427
            if (env->insn_flags & ISA_MIPS32R2) {
1428
                if (uimm != 0) {
1429
                    TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_TL);
1430

    
1431
                    tcg_gen_movi_tl(r_tmp1, 0x40);
1432
                    tcg_gen_subi_tl(r_tmp1, r_tmp1, uimm);
1433
                    tcg_gen_shl_tl(r_tmp1, cpu_T[0], r_tmp1);
1434
                    tcg_gen_shri_tl(cpu_T[0], cpu_T[0], uimm);
1435
                    tcg_gen_or_tl(cpu_T[0], cpu_T[0], r_tmp1);
1436
                    tcg_temp_free(r_tmp1);
1437
                }
1438
                opn = "drotr";
1439
            } else {
1440
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], uimm);
1441
                opn = "dsrl";
1442
            }
1443
            break;
1444
        default:
1445
            MIPS_INVAL("invalid dsrl flag");
1446
            generate_exception(ctx, EXCP_RI);
1447
            break;
1448
        }
1449
        break;
1450
    case OPC_DSLL32:
1451
        tcg_gen_shli_tl(cpu_T[0], cpu_T[0], uimm + 32);
1452
        opn = "dsll32";
1453
        break;
1454
    case OPC_DSRA32:
1455
        tcg_gen_sari_tl(cpu_T[0], cpu_T[0], uimm + 32);
1456
        opn = "dsra32";
1457
        break;
1458
    case OPC_DSRL32:
1459
        switch ((ctx->opcode >> 21) & 0x1f) {
1460
        case 0:
1461
            tcg_gen_shri_tl(cpu_T[0], cpu_T[0], uimm + 32);
1462
            opn = "dsrl32";
1463
            break;
1464
        case 1:
1465
            /* drotr32 is decoded as dsrl32 on non-R2 CPUs */
1466
            if (env->insn_flags & ISA_MIPS32R2) {
1467
                TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_TL);
1468
                TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_TL);
1469

    
1470
                tcg_gen_movi_tl(r_tmp1, 0x40);
1471
                tcg_gen_movi_tl(r_tmp2, 32);
1472
                tcg_gen_addi_tl(r_tmp2, r_tmp2, uimm);
1473
                tcg_gen_sub_tl(r_tmp1, r_tmp1, r_tmp2);
1474
                tcg_gen_shl_tl(r_tmp1, cpu_T[0], r_tmp1);
1475
                tcg_gen_shr_tl(cpu_T[0], cpu_T[0], r_tmp2);
1476
                tcg_gen_or_tl(cpu_T[0], cpu_T[0], r_tmp1);
1477
                tcg_temp_free(r_tmp1);
1478
                tcg_temp_free(r_tmp2);
1479
                opn = "drotr32";
1480
            } else {
1481
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], uimm + 32);
1482
                opn = "dsrl32";
1483
            }
1484
            break;
1485
        default:
1486
            MIPS_INVAL("invalid dsrl32 flag");
1487
            generate_exception(ctx, EXCP_RI);
1488
            break;
1489
        }
1490
        break;
1491
#endif
1492
    default:
1493
        MIPS_INVAL(opn);
1494
        generate_exception(ctx, EXCP_RI);
1495
        return;
1496
    }
1497
    gen_store_gpr(cpu_T[0], rt);
1498
    MIPS_DEBUG("%s %s, %s, " TARGET_FMT_lx, opn, regnames[rt], regnames[rs], uimm);
1499
}
1500

    
1501
/* Arithmetic */
1502
static void gen_arith (CPUState *env, DisasContext *ctx, uint32_t opc,
1503
                       int rd, int rs, int rt)
1504
{
1505
    const char *opn = "arith";
1506

    
1507
    if (rd == 0 && opc != OPC_ADD && opc != OPC_SUB
1508
       && opc != OPC_DADD && opc != OPC_DSUB) {
1509
        /* If no destination, treat it as a NOP.
1510
           For add & sub, we must generate the overflow exception when needed. */
1511
        MIPS_DEBUG("NOP");
1512
        return;
1513
    }
1514
    gen_load_gpr(cpu_T[0], rs);
1515
    /* Specialcase the conventional move operation. */
1516
    if (rt == 0 && (opc == OPC_ADDU || opc == OPC_DADDU
1517
                    || opc == OPC_SUBU || opc == OPC_DSUBU)) {
1518
        gen_store_gpr(cpu_T[0], rd);
1519
        return;
1520
    }
1521
    gen_load_gpr(cpu_T[1], rt);
1522
    switch (opc) {
1523
    case OPC_ADD:
1524
        {
1525
            TCGv r_tmp1 = tcg_temp_local_new(TCG_TYPE_TL);
1526
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_TL);
1527
            int l1 = gen_new_label();
1528

    
1529
            save_cpu_state(ctx, 1);
1530
            tcg_gen_ext32s_tl(r_tmp1, cpu_T[0]);
1531
            tcg_gen_ext32s_tl(r_tmp2, cpu_T[1]);
1532
            tcg_gen_add_tl(cpu_T[0], r_tmp1, r_tmp2);
1533

    
1534
            tcg_gen_xor_tl(r_tmp1, r_tmp1, cpu_T[1]);
1535
            tcg_gen_xori_tl(r_tmp1, r_tmp1, -1);
1536
            tcg_gen_xor_tl(r_tmp2, cpu_T[0], cpu_T[1]);
1537
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
1538
            tcg_temp_free(r_tmp2);
1539
            tcg_gen_shri_tl(r_tmp1, r_tmp1, 31);
1540
            tcg_gen_brcondi_tl(TCG_COND_EQ, r_tmp1, 0, l1);
1541
            tcg_temp_free(r_tmp1);
1542
            /* operands of same sign, result different sign */
1543
            generate_exception(ctx, EXCP_OVERFLOW);
1544
            gen_set_label(l1);
1545

    
1546
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1547
        }
1548
        opn = "add";
1549
        break;
1550
    case OPC_ADDU:
1551
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1552
        tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
1553
        tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1554
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1555
        opn = "addu";
1556
        break;
1557
    case OPC_SUB:
1558
        {
1559
            TCGv r_tmp1 = tcg_temp_local_new(TCG_TYPE_TL);
1560
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_TL);
1561
            int l1 = gen_new_label();
1562

    
1563
            save_cpu_state(ctx, 1);
1564
            tcg_gen_ext32s_tl(r_tmp1, cpu_T[0]);
1565
            tcg_gen_ext32s_tl(r_tmp2, cpu_T[1]);
1566
            tcg_gen_sub_tl(cpu_T[0], r_tmp1, r_tmp2);
1567

    
1568
            tcg_gen_xor_tl(r_tmp2, r_tmp1, cpu_T[1]);
1569
            tcg_gen_xor_tl(r_tmp1, r_tmp1, cpu_T[0]);
1570
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
1571
            tcg_temp_free(r_tmp2);
1572
            tcg_gen_shri_tl(r_tmp1, r_tmp1, 31);
1573
            tcg_gen_brcondi_tl(TCG_COND_EQ, r_tmp1, 0, l1);
1574
            tcg_temp_free(r_tmp1);
1575
            /* operands of different sign, first operand and result different sign */
1576
            generate_exception(ctx, EXCP_OVERFLOW);
1577
            gen_set_label(l1);
1578

    
1579
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1580
        }
1581
        opn = "sub";
1582
        break;
1583
    case OPC_SUBU:
1584
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1585
        tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
1586
        tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1587
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1588
        opn = "subu";
1589
        break;
1590
#if defined(TARGET_MIPS64)
1591
    case OPC_DADD:
1592
        {
1593
            TCGv r_tmp1 = tcg_temp_local_new(TCG_TYPE_TL);
1594
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_TL);
1595
            int l1 = gen_new_label();
1596

    
1597
            save_cpu_state(ctx, 1);
1598
            tcg_gen_mov_tl(r_tmp1, cpu_T[0]);
1599
            tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1600

    
1601
            tcg_gen_xor_tl(r_tmp1, r_tmp1, cpu_T[1]);
1602
            tcg_gen_xori_tl(r_tmp1, r_tmp1, -1);
1603
            tcg_gen_xor_tl(r_tmp2, cpu_T[0], cpu_T[1]);
1604
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
1605
            tcg_temp_free(r_tmp2);
1606
            tcg_gen_shri_tl(r_tmp1, r_tmp1, 63);
1607
            tcg_gen_brcondi_tl(TCG_COND_EQ, r_tmp1, 0, l1);
1608
            tcg_temp_free(r_tmp1);
1609
            /* operands of same sign, result different sign */
1610
            generate_exception(ctx, EXCP_OVERFLOW);
1611
            gen_set_label(l1);
1612
        }
1613
        opn = "dadd";
1614
        break;
1615
    case OPC_DADDU:
1616
        tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1617
        opn = "daddu";
1618
        break;
1619
    case OPC_DSUB:
1620
        {
1621
            TCGv r_tmp1 = tcg_temp_local_new(TCG_TYPE_TL);
1622
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_TL);
1623
            int l1 = gen_new_label();
1624

    
1625
            save_cpu_state(ctx, 1);
1626
            tcg_gen_mov_tl(r_tmp1, cpu_T[0]);
1627
            tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1628

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

    
1683
            tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_T[1], 0, l1);
1684
            gen_store_gpr(cpu_T[0], rd);
1685
            gen_set_label(l1);
1686
        }
1687
        opn = "movn";
1688
        goto print;
1689
    case OPC_MOVZ:
1690
        {
1691
            int l1 = gen_new_label();
1692

    
1693
            tcg_gen_brcondi_tl(TCG_COND_NE, cpu_T[1], 0, l1);
1694
            gen_store_gpr(cpu_T[0], rd);
1695
            gen_set_label(l1);
1696
        }
1697
        opn = "movz";
1698
        goto print;
1699
    case OPC_SLLV:
1700
        tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
1701
        tcg_gen_ext32u_tl(cpu_T[1], cpu_T[1]);
1702
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0x1f);
1703
        tcg_gen_shl_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
1704
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1705
        opn = "sllv";
1706
        break;
1707
    case OPC_SRAV:
1708
        tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
1709
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0x1f);
1710
        tcg_gen_sar_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
1711
        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1712
        opn = "srav";
1713
        break;
1714
    case OPC_SRLV:
1715
        switch ((ctx->opcode >> 6) & 0x1f) {
1716
        case 0:
1717
            tcg_gen_ext32u_tl(cpu_T[1], cpu_T[1]);
1718
            tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0x1f);
1719
            tcg_gen_shr_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
1720
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1721
            opn = "srlv";
1722
            break;
1723
        case 1:
1724
            /* rotrv is decoded as srlv on non-R2 CPUs */
1725
            if (env->insn_flags & ISA_MIPS32R2) {
1726
                int l1 = gen_new_label();
1727
                int l2 = gen_new_label();
1728

    
1729
                tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0x1f);
1730
                tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_T[0], 0, l1);
1731
                {
1732
                    TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
1733
                    TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I32);
1734
                    TCGv r_tmp3 = tcg_temp_new(TCG_TYPE_I32);
1735

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

    
1791
                tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0x3f);
1792
                tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_T[0], 0, l1);
1793
                {
1794
                    TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_TL);
1795

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

    
1831
/* Arithmetic on HI/LO registers */
1832
static void gen_HILO (DisasContext *ctx, uint32_t opc, int reg)
1833
{
1834
    const char *opn = "hilo";
1835

    
1836
    if (reg == 0 && (opc == OPC_MFHI || opc == OPC_MFLO)) {
1837
        /* Treat as NOP. */
1838
        MIPS_DEBUG("NOP");
1839
        return;
1840
    }
1841
    switch (opc) {
1842
    case OPC_MFHI:
1843
        gen_load_HI(cpu_T[0], 0);
1844
        gen_store_gpr(cpu_T[0], reg);
1845
        opn = "mfhi";
1846
        break;
1847
    case OPC_MFLO:
1848
        gen_load_LO(cpu_T[0], 0);
1849
        gen_store_gpr(cpu_T[0], reg);
1850
        opn = "mflo";
1851
        break;
1852
    case OPC_MTHI:
1853
        gen_load_gpr(cpu_T[0], reg);
1854
        gen_store_HI(cpu_T[0], 0);
1855
        opn = "mthi";
1856
        break;
1857
    case OPC_MTLO:
1858
        gen_load_gpr(cpu_T[0], reg);
1859
        gen_store_LO(cpu_T[0], 0);
1860
        opn = "mtlo";
1861
        break;
1862
    default:
1863
        MIPS_INVAL(opn);
1864
        generate_exception(ctx, EXCP_RI);
1865
        return;
1866
    }
1867
    MIPS_DEBUG("%s %s", opn, regnames[reg]);
1868
}
1869

    
1870
static void gen_muldiv (DisasContext *ctx, uint32_t opc,
1871
                        int rs, int rt)
1872
{
1873
    const char *opn = "mul/div";
1874

    
1875
    gen_load_gpr(cpu_T[0], rs);
1876
    gen_load_gpr(cpu_T[1], rt);
1877
    switch (opc) {
1878
    case OPC_DIV:
1879
        {
1880
            int l1 = gen_new_label();
1881

    
1882
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1883
            tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
1884
            tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_T[1], 0, l1);
1885
            {
1886
                TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I64);
1887
                TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I64);
1888
                TCGv r_tmp3 = tcg_temp_new(TCG_TYPE_I64);
1889

    
1890
                tcg_gen_ext_tl_i64(r_tmp1, cpu_T[0]);
1891
                tcg_gen_ext_tl_i64(r_tmp2, cpu_T[1]);
1892
                tcg_gen_div_i64(r_tmp3, r_tmp1, r_tmp2);
1893
                tcg_gen_rem_i64(r_tmp2, r_tmp1, r_tmp2);
1894
                tcg_gen_trunc_i64_tl(cpu_T[0], r_tmp3);
1895
                tcg_gen_trunc_i64_tl(cpu_T[1], r_tmp2);
1896
                tcg_temp_free(r_tmp1);
1897
                tcg_temp_free(r_tmp2);
1898
                tcg_temp_free(r_tmp3);
1899
                tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1900
                tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
1901
                gen_store_LO(cpu_T[0], 0);
1902
                gen_store_HI(cpu_T[1], 0);
1903
            }
1904
            gen_set_label(l1);
1905
        }
1906
        opn = "div";
1907
        break;
1908
    case OPC_DIVU:
1909
        {
1910
            int l1 = gen_new_label();
1911

    
1912
            tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
1913
            tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_T[1], 0, l1);
1914
            {
1915
                TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
1916
                TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I32);
1917
                TCGv r_tmp3 = tcg_temp_new(TCG_TYPE_I32);
1918

    
1919
                tcg_gen_trunc_tl_i32(r_tmp1, cpu_T[0]);
1920
                tcg_gen_trunc_tl_i32(r_tmp2, cpu_T[1]);
1921
                tcg_gen_divu_i32(r_tmp3, r_tmp1, r_tmp2);
1922
                tcg_gen_remu_i32(r_tmp1, r_tmp1, r_tmp2);
1923
                tcg_gen_ext_i32_tl(cpu_T[0], r_tmp3);
1924
                tcg_gen_ext_i32_tl(cpu_T[1], r_tmp1);
1925
                tcg_temp_free(r_tmp1);
1926
                tcg_temp_free(r_tmp2);
1927
                tcg_temp_free(r_tmp3);
1928
                gen_store_LO(cpu_T[0], 0);
1929
                gen_store_HI(cpu_T[1], 0);
1930
            }
1931
            gen_set_label(l1);
1932
        }
1933
        opn = "divu";
1934
        break;
1935
    case OPC_MULT:
1936
        {
1937
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I64);
1938
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I64);
1939

    
1940
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1941
            tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
1942
            tcg_gen_ext_tl_i64(r_tmp1, cpu_T[0]);
1943
            tcg_gen_ext_tl_i64(r_tmp2, cpu_T[1]);
1944
            tcg_gen_mul_i64(r_tmp1, r_tmp1, r_tmp2);
1945
            tcg_temp_free(r_tmp2);
1946
            tcg_gen_trunc_i64_tl(cpu_T[0], r_tmp1);
1947
            tcg_gen_shri_i64(r_tmp1, r_tmp1, 32);
1948
            tcg_gen_trunc_i64_tl(cpu_T[1], r_tmp1);
1949
            tcg_temp_free(r_tmp1);
1950
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1951
            tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
1952
            gen_store_LO(cpu_T[0], 0);
1953
            gen_store_HI(cpu_T[1], 0);
1954
        }
1955
        opn = "mult";
1956
        break;
1957
    case OPC_MULTU:
1958
        {
1959
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I64);
1960
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I64);
1961

    
1962
            tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
1963
            tcg_gen_ext32u_tl(cpu_T[1], cpu_T[1]);
1964
            tcg_gen_extu_tl_i64(r_tmp1, cpu_T[0]);
1965
            tcg_gen_extu_tl_i64(r_tmp2, cpu_T[1]);
1966
            tcg_gen_mul_i64(r_tmp1, r_tmp1, r_tmp2);
1967
            tcg_temp_free(r_tmp2);
1968
            tcg_gen_trunc_i64_tl(cpu_T[0], r_tmp1);
1969
            tcg_gen_shri_i64(r_tmp1, r_tmp1, 32);
1970
            tcg_gen_trunc_i64_tl(cpu_T[1], r_tmp1);
1971
            tcg_temp_free(r_tmp1);
1972
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
1973
            tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
1974
            gen_store_LO(cpu_T[0], 0);
1975
            gen_store_HI(cpu_T[1], 0);
1976
        }
1977
        opn = "multu";
1978
        break;
1979
#if defined(TARGET_MIPS64)
1980
    case OPC_DDIV:
1981
        {
1982
            int l1 = gen_new_label();
1983

    
1984
            tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_T[1], 0, l1);
1985
            {
1986
                int l2 = gen_new_label();
1987

    
1988
                tcg_gen_brcondi_tl(TCG_COND_NE, cpu_T[0], -1LL << 63, l2);
1989
                tcg_gen_brcondi_tl(TCG_COND_NE, cpu_T[1], -1LL, l2);
1990
                {
1991
                    tcg_gen_movi_tl(cpu_T[1], 0);
1992
                    gen_store_LO(cpu_T[0], 0);
1993
                    gen_store_HI(cpu_T[1], 0);
1994
                    tcg_gen_br(l1);
1995
                }
1996
                gen_set_label(l2);
1997
                {
1998
                    TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I64);
1999
                    TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I64);
2000

    
2001
                    tcg_gen_div_i64(r_tmp1, cpu_T[0], cpu_T[1]);
2002
                    tcg_gen_rem_i64(r_tmp2, cpu_T[0], cpu_T[1]);
2003
                    gen_store_LO(r_tmp1, 0);
2004
                    gen_store_HI(r_tmp2, 0);
2005
                    tcg_temp_free(r_tmp1);
2006
                    tcg_temp_free(r_tmp2);
2007
                }
2008
            }
2009
            gen_set_label(l1);
2010
        }
2011
        opn = "ddiv";
2012
        break;
2013
    case OPC_DDIVU:
2014
        {
2015
            int l1 = gen_new_label();
2016

    
2017
            tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_T[1], 0, l1);
2018
            {
2019
                TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I64);
2020
                TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I64);
2021

    
2022
                tcg_gen_divu_i64(r_tmp1, cpu_T[0], cpu_T[1]);
2023
                tcg_gen_remu_i64(r_tmp2, cpu_T[0], cpu_T[1]);
2024
                tcg_temp_free(r_tmp1);
2025
                tcg_temp_free(r_tmp2);
2026
                gen_store_LO(r_tmp1, 0);
2027
                gen_store_HI(r_tmp2, 0);
2028
            }
2029
            gen_set_label(l1);
2030
        }
2031
        opn = "ddivu";
2032
        break;
2033
    case OPC_DMULT:
2034
        tcg_gen_helper_0_0(do_dmult);
2035
        opn = "dmult";
2036
        break;
2037
    case OPC_DMULTU:
2038
        tcg_gen_helper_0_0(do_dmultu);
2039
        opn = "dmultu";
2040
        break;
2041
#endif
2042
    case OPC_MADD:
2043
        {
2044
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I64);
2045
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I64);
2046
            TCGv r_tmp3 = tcg_temp_new(TCG_TYPE_I64);
2047

    
2048
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
2049
            tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
2050
            tcg_gen_ext_tl_i64(r_tmp1, cpu_T[0]);
2051
            tcg_gen_ext_tl_i64(r_tmp2, cpu_T[1]);
2052
            tcg_gen_mul_i64(r_tmp1, r_tmp1, r_tmp2);
2053
            gen_load_LO(cpu_T[0], 0);
2054
            gen_load_HI(cpu_T[1], 0);
2055
            tcg_gen_extu_tl_i64(r_tmp2, cpu_T[0]);
2056
            tcg_gen_extu_tl_i64(r_tmp3, cpu_T[1]);
2057
            tcg_gen_shli_i64(r_tmp3, r_tmp3, 32);
2058
            tcg_gen_or_i64(r_tmp2, r_tmp2, r_tmp3);
2059
            tcg_temp_free(r_tmp3);
2060
            tcg_gen_add_i64(r_tmp1, r_tmp1, r_tmp2);
2061
            tcg_temp_free(r_tmp2);
2062
            tcg_gen_trunc_i64_tl(cpu_T[0], r_tmp1);
2063
            tcg_gen_shri_i64(r_tmp1, r_tmp1, 32);
2064
            tcg_gen_trunc_i64_tl(cpu_T[1], r_tmp1);
2065
            tcg_temp_free(r_tmp1);
2066
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
2067
            tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
2068
            gen_store_LO(cpu_T[0], 0);
2069
            gen_store_HI(cpu_T[1], 0);
2070
        }
2071
        opn = "madd";
2072
        break;
2073
    case OPC_MADDU:
2074
       {
2075
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I64);
2076
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I64);
2077
            TCGv r_tmp3 = tcg_temp_new(TCG_TYPE_I64);
2078

    
2079
            tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
2080
            tcg_gen_ext32u_tl(cpu_T[1], cpu_T[1]);
2081
            tcg_gen_extu_tl_i64(r_tmp1, cpu_T[0]);
2082
            tcg_gen_extu_tl_i64(r_tmp2, cpu_T[1]);
2083
            tcg_gen_mul_i64(r_tmp1, r_tmp1, r_tmp2);
2084
            gen_load_LO(cpu_T[0], 0);
2085
            gen_load_HI(cpu_T[1], 0);
2086
            tcg_gen_extu_tl_i64(r_tmp2, cpu_T[0]);
2087
            tcg_gen_extu_tl_i64(r_tmp3, cpu_T[1]);
2088
            tcg_gen_shli_i64(r_tmp3, r_tmp3, 32);
2089
            tcg_gen_or_i64(r_tmp2, r_tmp2, r_tmp3);
2090
            tcg_temp_free(r_tmp3);
2091
            tcg_gen_add_i64(r_tmp1, r_tmp1, r_tmp2);
2092
            tcg_temp_free(r_tmp2);
2093
            tcg_gen_trunc_i64_tl(cpu_T[0], r_tmp1);
2094
            tcg_gen_shri_i64(r_tmp1, r_tmp1, 32);
2095
            tcg_gen_trunc_i64_tl(cpu_T[1], r_tmp1);
2096
            tcg_temp_free(r_tmp1);
2097
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
2098
            tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
2099
            gen_store_LO(cpu_T[0], 0);
2100
            gen_store_HI(cpu_T[1], 0);
2101
        }
2102
        opn = "maddu";
2103
        break;
2104
    case OPC_MSUB:
2105
        {
2106
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I64);
2107
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I64);
2108
            TCGv r_tmp3 = tcg_temp_new(TCG_TYPE_I64);
2109

    
2110
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
2111
            tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
2112
            tcg_gen_ext_tl_i64(r_tmp1, cpu_T[0]);
2113
            tcg_gen_ext_tl_i64(r_tmp2, cpu_T[1]);
2114
            tcg_gen_mul_i64(r_tmp1, r_tmp1, r_tmp2);
2115
            gen_load_LO(cpu_T[0], 0);
2116
            gen_load_HI(cpu_T[1], 0);
2117
            tcg_gen_extu_tl_i64(r_tmp2, cpu_T[0]);
2118
            tcg_gen_extu_tl_i64(r_tmp3, cpu_T[1]);
2119
            tcg_gen_shli_i64(r_tmp3, r_tmp3, 32);
2120
            tcg_gen_or_i64(r_tmp2, r_tmp2, r_tmp3);
2121
            tcg_temp_free(r_tmp3);
2122
            tcg_gen_sub_i64(r_tmp1, r_tmp1, r_tmp2);
2123
            tcg_temp_free(r_tmp2);
2124
            tcg_gen_trunc_i64_tl(cpu_T[0], r_tmp1);
2125
            tcg_gen_shri_i64(r_tmp1, r_tmp1, 32);
2126
            tcg_gen_trunc_i64_tl(cpu_T[1], r_tmp1);
2127
            tcg_temp_free(r_tmp1);
2128
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
2129
            tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
2130
            gen_store_LO(cpu_T[0], 0);
2131
            gen_store_HI(cpu_T[1], 0);
2132
        }
2133
        opn = "msub";
2134
        break;
2135
    case OPC_MSUBU:
2136
        {
2137
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I64);
2138
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I64);
2139
            TCGv r_tmp3 = tcg_temp_new(TCG_TYPE_I64);
2140

    
2141
            tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
2142
            tcg_gen_ext32u_tl(cpu_T[1], cpu_T[1]);
2143
            tcg_gen_extu_tl_i64(r_tmp1, cpu_T[0]);
2144
            tcg_gen_extu_tl_i64(r_tmp2, cpu_T[1]);
2145
            tcg_gen_mul_i64(r_tmp1, r_tmp1, r_tmp2);
2146
            gen_load_LO(cpu_T[0], 0);
2147
            gen_load_HI(cpu_T[1], 0);
2148
            tcg_gen_extu_tl_i64(r_tmp2, cpu_T[0]);
2149
            tcg_gen_extu_tl_i64(r_tmp3, cpu_T[1]);
2150
            tcg_gen_shli_i64(r_tmp3, r_tmp3, 32);
2151
            tcg_gen_or_i64(r_tmp2, r_tmp2, r_tmp3);
2152
            tcg_temp_free(r_tmp3);
2153
            tcg_gen_sub_i64(r_tmp1, r_tmp1, r_tmp2);
2154
            tcg_temp_free(r_tmp2);
2155
            tcg_gen_trunc_i64_tl(cpu_T[0], r_tmp1);
2156
            tcg_gen_shri_i64(r_tmp1, r_tmp1, 32);
2157
            tcg_gen_trunc_i64_tl(cpu_T[1], r_tmp1);
2158
            tcg_temp_free(r_tmp1);
2159
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
2160
            tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
2161
            gen_store_LO(cpu_T[0], 0);
2162
            gen_store_HI(cpu_T[1], 0);
2163
        }
2164
        opn = "msubu";
2165
        break;
2166
    default:
2167
        MIPS_INVAL(opn);
2168
        generate_exception(ctx, EXCP_RI);
2169
        return;
2170
    }
2171
    MIPS_DEBUG("%s %s %s", opn, regnames[rs], regnames[rt]);
2172
}
2173

    
2174
static void gen_mul_vr54xx (DisasContext *ctx, uint32_t opc,
2175
                            int rd, int rs, int rt)
2176
{
2177
    const char *opn = "mul vr54xx";
2178

    
2179
    gen_load_gpr(cpu_T[0], rs);
2180
    gen_load_gpr(cpu_T[1], rt);
2181

    
2182
    switch (opc) {
2183
    case OPC_VR54XX_MULS:
2184
        tcg_gen_helper_0_0(do_muls);
2185
        opn = "muls";
2186
        break;
2187
    case OPC_VR54XX_MULSU:
2188
        tcg_gen_helper_0_0(do_mulsu);
2189
        opn = "mulsu";
2190
        break;
2191
    case OPC_VR54XX_MACC:
2192
        tcg_gen_helper_0_0(do_macc);
2193
        opn = "macc";
2194
        break;
2195
    case OPC_VR54XX_MACCU:
2196
        tcg_gen_helper_0_0(do_maccu);
2197
        opn = "maccu";
2198
        break;
2199
    case OPC_VR54XX_MSAC:
2200
        tcg_gen_helper_0_0(do_msac);
2201
        opn = "msac";
2202
        break;
2203
    case OPC_VR54XX_MSACU:
2204
        tcg_gen_helper_0_0(do_msacu);
2205
        opn = "msacu";
2206
        break;
2207
    case OPC_VR54XX_MULHI:
2208
        tcg_gen_helper_0_0(do_mulhi);
2209
        opn = "mulhi";
2210
        break;
2211
    case OPC_VR54XX_MULHIU:
2212
        tcg_gen_helper_0_0(do_mulhiu);
2213
        opn = "mulhiu";
2214
        break;
2215
    case OPC_VR54XX_MULSHI:
2216
        tcg_gen_helper_0_0(do_mulshi);
2217
        opn = "mulshi";
2218
        break;
2219
    case OPC_VR54XX_MULSHIU:
2220
        tcg_gen_helper_0_0(do_mulshiu);
2221
        opn = "mulshiu";
2222
        break;
2223
    case OPC_VR54XX_MACCHI:
2224
        tcg_gen_helper_0_0(do_macchi);
2225
        opn = "macchi";
2226
        break;
2227
    case OPC_VR54XX_MACCHIU:
2228
        tcg_gen_helper_0_0(do_macchiu);
2229
        opn = "macchiu";
2230
        break;
2231
    case OPC_VR54XX_MSACHI:
2232
        tcg_gen_helper_0_0(do_msachi);
2233
        opn = "msachi";
2234
        break;
2235
    case OPC_VR54XX_MSACHIU:
2236
        tcg_gen_helper_0_0(do_msachiu);
2237
        opn = "msachiu";
2238
        break;
2239
    default:
2240
        MIPS_INVAL("mul vr54xx");
2241
        generate_exception(ctx, EXCP_RI);
2242
        return;
2243
    }
2244
    gen_store_gpr(cpu_T[0], rd);
2245
    MIPS_DEBUG("%s %s, %s, %s", opn, regnames[rd], regnames[rs], regnames[rt]);
2246
}
2247

    
2248
static void gen_cl (DisasContext *ctx, uint32_t opc,
2249
                    int rd, int rs)
2250
{
2251
    const char *opn = "CLx";
2252
    if (rd == 0) {
2253
        /* Treat as NOP. */
2254
        MIPS_DEBUG("NOP");
2255
        return;
2256
    }
2257
    gen_load_gpr(cpu_T[0], rs);
2258
    switch (opc) {
2259
    case OPC_CLO:
2260
        tcg_gen_helper_0_0(do_clo);
2261
        opn = "clo";
2262
        break;
2263
    case OPC_CLZ:
2264
        tcg_gen_helper_0_0(do_clz);
2265
        opn = "clz";
2266
        break;
2267
#if defined(TARGET_MIPS64)
2268
    case OPC_DCLO:
2269
        tcg_gen_helper_0_0(do_dclo);
2270
        opn = "dclo";
2271
        break;
2272
    case OPC_DCLZ:
2273
        tcg_gen_helper_0_0(do_dclz);
2274
        opn = "dclz";
2275
        break;
2276
#endif
2277
    default:
2278
        MIPS_INVAL(opn);
2279
        generate_exception(ctx, EXCP_RI);
2280
        return;
2281
    }
2282
    gen_store_gpr(cpu_T[0], rd);
2283
    MIPS_DEBUG("%s %s, %s", opn, regnames[rd], regnames[rs]);
2284
}
2285

    
2286
/* Traps */
2287
static void gen_trap (DisasContext *ctx, uint32_t opc,
2288
                      int rs, int rt, int16_t imm)
2289
{
2290
    int cond;
2291

    
2292
    cond = 0;
2293
    /* Load needed operands */
2294
    switch (opc) {
2295
    case OPC_TEQ:
2296
    case OPC_TGE:
2297
    case OPC_TGEU:
2298
    case OPC_TLT:
2299
    case OPC_TLTU:
2300
    case OPC_TNE:
2301
        /* Compare two registers */
2302
        if (rs != rt) {
2303
            gen_load_gpr(cpu_T[0], rs);
2304
            gen_load_gpr(cpu_T[1], rt);
2305
            cond = 1;
2306
        }
2307
        break;
2308
    case OPC_TEQI:
2309
    case OPC_TGEI:
2310
    case OPC_TGEIU:
2311
    case OPC_TLTI:
2312
    case OPC_TLTIU:
2313
    case OPC_TNEI:
2314
        /* Compare register to immediate */
2315
        if (rs != 0 || imm != 0) {
2316
            gen_load_gpr(cpu_T[0], rs);
2317
            tcg_gen_movi_tl(cpu_T[1], (int32_t)imm);
2318
            cond = 1;
2319
        }
2320
        break;
2321
    }
2322
    if (cond == 0) {
2323
        switch (opc) {
2324
        case OPC_TEQ:   /* rs == rs */
2325
        case OPC_TEQI:  /* r0 == 0  */
2326
        case OPC_TGE:   /* rs >= rs */
2327
        case OPC_TGEI:  /* r0 >= 0  */
2328
        case OPC_TGEU:  /* rs >= rs unsigned */
2329
        case OPC_TGEIU: /* r0 >= 0  unsigned */
2330
            /* Always trap */
2331
            tcg_gen_movi_tl(cpu_T[0], 1);
2332
            break;
2333
        case OPC_TLT:   /* rs < rs           */
2334
        case OPC_TLTI:  /* r0 < 0            */
2335
        case OPC_TLTU:  /* rs < rs unsigned  */
2336
        case OPC_TLTIU: /* r0 < 0  unsigned  */
2337
        case OPC_TNE:   /* rs != rs          */
2338
        case OPC_TNEI:  /* r0 != 0           */
2339
            /* Never trap: treat as NOP. */
2340
            return;
2341
        default:
2342
            MIPS_INVAL("trap");
2343
            generate_exception(ctx, EXCP_RI);
2344
            return;
2345
        }
2346
    } else {
2347
        switch (opc) {
2348
        case OPC_TEQ:
2349
        case OPC_TEQI:
2350
            gen_op_eq();
2351
            break;
2352
        case OPC_TGE:
2353
        case OPC_TGEI:
2354
            gen_op_ge();
2355
            break;
2356
        case OPC_TGEU:
2357
        case OPC_TGEIU:
2358
            gen_op_geu();
2359
            break;
2360
        case OPC_TLT:
2361
        case OPC_TLTI:
2362
            gen_op_lt();
2363
            break;
2364
        case OPC_TLTU:
2365
        case OPC_TLTIU:
2366
            gen_op_ltu();
2367
            break;
2368
        case OPC_TNE:
2369
        case OPC_TNEI:
2370
            gen_op_ne();
2371
            break;
2372
        default:
2373
            MIPS_INVAL("trap");
2374
            generate_exception(ctx, EXCP_RI);
2375
            return;
2376
        }
2377
    }
2378
    save_cpu_state(ctx, 1);
2379
    {
2380
        int l1 = gen_new_label();
2381

    
2382
        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_T[0], 0, l1);
2383
        tcg_gen_helper_0_1i(do_raise_exception, EXCP_TRAP);
2384
        gen_set_label(l1);
2385
    }
2386
    ctx->bstate = BS_STOP;
2387
}
2388

    
2389
static always_inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
2390
{
2391
    TranslationBlock *tb;
2392
    tb = ctx->tb;
2393
    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK)) {
2394
        tcg_gen_goto_tb(n);
2395
        gen_save_pc(dest);
2396
        tcg_gen_exit_tb((long)tb + n);
2397
    } else {
2398
        gen_save_pc(dest);
2399
        tcg_gen_exit_tb(0);
2400
    }
2401
}
2402

    
2403
/* Branches (before delay slot) */
2404
static void gen_compute_branch (DisasContext *ctx, uint32_t opc,
2405
                                int rs, int rt, int32_t offset)
2406
{
2407
    target_ulong btarget = -1;
2408
    int blink = 0;
2409
    int bcond = 0;
2410

    
2411
    if (ctx->hflags & MIPS_HFLAG_BMASK) {
2412
#ifdef MIPS_DEBUG_DISAS
2413
        if (loglevel & CPU_LOG_TB_IN_ASM) {
2414
            fprintf(logfile,
2415
                    "Branch in delay slot at PC 0x" TARGET_FMT_lx "\n",
2416
                    ctx->pc);
2417
        }
2418
#endif
2419
        generate_exception(ctx, EXCP_RI);
2420
        return;
2421
    }
2422

    
2423
    /* Load needed operands */
2424
    switch (opc) {
2425
    case OPC_BEQ:
2426
    case OPC_BEQL:
2427
    case OPC_BNE:
2428
    case OPC_BNEL:
2429
        /* Compare two registers */
2430
        if (rs != rt) {
2431
            gen_load_gpr(cpu_T[0], rs);
2432
            gen_load_gpr(cpu_T[1], rt);
2433
            bcond = 1;
2434
        }
2435
        btarget = ctx->pc + 4 + offset;
2436
        break;
2437
    case OPC_BGEZ:
2438
    case OPC_BGEZAL:
2439
    case OPC_BGEZALL:
2440
    case OPC_BGEZL:
2441
    case OPC_BGTZ:
2442
    case OPC_BGTZL:
2443
    case OPC_BLEZ:
2444
    case OPC_BLEZL:
2445
    case OPC_BLTZ:
2446
    case OPC_BLTZAL:
2447
    case OPC_BLTZALL:
2448
    case OPC_BLTZL:
2449
        /* Compare to zero */
2450
        if (rs != 0) {
2451
            gen_load_gpr(cpu_T[0], rs);
2452
            bcond = 1;
2453
        }
2454
        btarget = ctx->pc + 4 + offset;
2455
        break;
2456
    case OPC_J:
2457
    case OPC_JAL:
2458
        /* Jump to immediate */
2459
        btarget = ((ctx->pc + 4) & (int32_t)0xF0000000) | (uint32_t)offset;
2460
        break;
2461
    case OPC_JR:
2462
    case OPC_JALR:
2463
        /* Jump to register */
2464
        if (offset != 0 && offset != 16) {
2465
            /* Hint = 0 is JR/JALR, hint 16 is JR.HB/JALR.HB, the
2466
               others are reserved. */
2467
            MIPS_INVAL("jump hint");
2468
            generate_exception(ctx, EXCP_RI);
2469
            return;
2470
        }
2471
        gen_save_breg_target(rs);
2472
        break;
2473
    default:
2474
        MIPS_INVAL("branch/jump");
2475
        generate_exception(ctx, EXCP_RI);
2476
        return;
2477
    }
2478
    if (bcond == 0) {
2479
        /* No condition to be computed */
2480
        switch (opc) {
2481
        case OPC_BEQ:     /* rx == rx        */
2482
        case OPC_BEQL:    /* rx == rx likely */
2483
        case OPC_BGEZ:    /* 0 >= 0          */
2484
        case OPC_BGEZL:   /* 0 >= 0 likely   */
2485
        case OPC_BLEZ:    /* 0 <= 0          */
2486
        case OPC_BLEZL:   /* 0 <= 0 likely   */
2487
            /* Always take */
2488
            ctx->hflags |= MIPS_HFLAG_B;
2489
            MIPS_DEBUG("balways");
2490
            break;
2491
        case OPC_BGEZAL:  /* 0 >= 0          */
2492
        case OPC_BGEZALL: /* 0 >= 0 likely   */
2493
            /* Always take and link */
2494
            blink = 31;
2495
            ctx->hflags |= MIPS_HFLAG_B;
2496
            MIPS_DEBUG("balways and link");
2497
            break;
2498
        case OPC_BNE:     /* rx != rx        */
2499
        case OPC_BGTZ:    /* 0 > 0           */
2500
        case OPC_BLTZ:    /* 0 < 0           */
2501
            /* Treat as NOP. */
2502
            MIPS_DEBUG("bnever (NOP)");
2503
            return;
2504
        case OPC_BLTZAL:  /* 0 < 0           */
2505
            tcg_gen_movi_tl(cpu_T[0], ctx->pc + 8);
2506
            gen_store_gpr(cpu_T[0], 31);
2507
            MIPS_DEBUG("bnever and link");
2508
            return;
2509
        case OPC_BLTZALL: /* 0 < 0 likely */
2510
            tcg_gen_movi_tl(cpu_T[0], ctx->pc + 8);
2511
            gen_store_gpr(cpu_T[0], 31);
2512
            /* Skip the instruction in the delay slot */
2513
            MIPS_DEBUG("bnever, link and skip");
2514
            ctx->pc += 4;
2515
            return;
2516
        case OPC_BNEL:    /* rx != rx likely */
2517
        case OPC_BGTZL:   /* 0 > 0 likely */
2518
        case OPC_BLTZL:   /* 0 < 0 likely */
2519
            /* Skip the instruction in the delay slot */
2520
            MIPS_DEBUG("bnever and skip");
2521
            ctx->pc += 4;
2522
            return;
2523
        case OPC_J:
2524
            ctx->hflags |= MIPS_HFLAG_B;
2525
            MIPS_DEBUG("j " TARGET_FMT_lx, btarget);
2526
            break;
2527
        case OPC_JAL:
2528
            blink = 31;
2529
            ctx->hflags |= MIPS_HFLAG_B;
2530
            MIPS_DEBUG("jal " TARGET_FMT_lx, btarget);
2531
            break;
2532
        case OPC_JR:
2533
            ctx->hflags |= MIPS_HFLAG_BR;
2534
            MIPS_DEBUG("jr %s", regnames[rs]);
2535
            break;
2536
        case OPC_JALR:
2537
            blink = rt;
2538
            ctx->hflags |= MIPS_HFLAG_BR;
2539
            MIPS_DEBUG("jalr %s, %s", regnames[rt], regnames[rs]);
2540
            break;
2541
        default:
2542
            MIPS_INVAL("branch/jump");
2543
            generate_exception(ctx, EXCP_RI);
2544
            return;
2545
        }
2546
    } else {
2547
        switch (opc) {
2548
        case OPC_BEQ:
2549
            gen_op_eq();
2550
            MIPS_DEBUG("beq %s, %s, " TARGET_FMT_lx,
2551
                       regnames[rs], regnames[rt], btarget);
2552
            goto not_likely;
2553
        case OPC_BEQL:
2554
            gen_op_eq();
2555
            MIPS_DEBUG("beql %s, %s, " TARGET_FMT_lx,
2556
                       regnames[rs], regnames[rt], btarget);
2557
            goto likely;
2558
        case OPC_BNE:
2559
            gen_op_ne();
2560
            MIPS_DEBUG("bne %s, %s, " TARGET_FMT_lx,
2561
                       regnames[rs], regnames[rt], btarget);
2562
            goto not_likely;
2563
        case OPC_BNEL:
2564
            gen_op_ne();
2565
            MIPS_DEBUG("bnel %s, %s, " TARGET_FMT_lx,
2566
                       regnames[rs], regnames[rt], btarget);
2567
            goto likely;
2568
        case OPC_BGEZ:
2569
            gen_op_gez();
2570
            MIPS_DEBUG("bgez %s, " TARGET_FMT_lx, regnames[rs], btarget);
2571
            goto not_likely;
2572
        case OPC_BGEZL:
2573
            gen_op_gez();
2574
            MIPS_DEBUG("bgezl %s, " TARGET_FMT_lx, regnames[rs], btarget);
2575
            goto likely;
2576
        case OPC_BGEZAL:
2577
            gen_op_gez();
2578
            MIPS_DEBUG("bgezal %s, " TARGET_FMT_lx, regnames[rs], btarget);
2579
            blink = 31;
2580
            goto not_likely;
2581
        case OPC_BGEZALL:
2582
            gen_op_gez();
2583
            blink = 31;
2584
            MIPS_DEBUG("bgezall %s, " TARGET_FMT_lx, regnames[rs], btarget);
2585
            goto likely;
2586
        case OPC_BGTZ:
2587
            gen_op_gtz();
2588
            MIPS_DEBUG("bgtz %s, " TARGET_FMT_lx, regnames[rs], btarget);
2589
            goto not_likely;
2590
        case OPC_BGTZL:
2591
            gen_op_gtz();
2592
            MIPS_DEBUG("bgtzl %s, " TARGET_FMT_lx, regnames[rs], btarget);
2593
            goto likely;
2594
        case OPC_BLEZ:
2595
            gen_op_lez();
2596
            MIPS_DEBUG("blez %s, " TARGET_FMT_lx, regnames[rs], btarget);
2597
            goto not_likely;
2598
        case OPC_BLEZL:
2599
            gen_op_lez();
2600
            MIPS_DEBUG("blezl %s, " TARGET_FMT_lx, regnames[rs], btarget);
2601
            goto likely;
2602
        case OPC_BLTZ:
2603
            gen_op_ltz();
2604
            MIPS_DEBUG("bltz %s, " TARGET_FMT_lx, regnames[rs], btarget);
2605
            goto not_likely;
2606
        case OPC_BLTZL:
2607
            gen_op_ltz();
2608
            MIPS_DEBUG("bltzl %s, " TARGET_FMT_lx, regnames[rs], btarget);
2609
            goto likely;
2610
        case OPC_BLTZAL:
2611
            gen_op_ltz();
2612
            blink = 31;
2613
            MIPS_DEBUG("bltzal %s, " TARGET_FMT_lx, regnames[rs], btarget);
2614
        not_likely:
2615
            ctx->hflags |= MIPS_HFLAG_BC;
2616
            tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUState, bcond));
2617
            break;
2618
        case OPC_BLTZALL:
2619
            gen_op_ltz();
2620
            blink = 31;
2621
            MIPS_DEBUG("bltzall %s, " TARGET_FMT_lx, regnames[rs], btarget);
2622
        likely:
2623
            ctx->hflags |= MIPS_HFLAG_BL;
2624
            tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUState, bcond));
2625
            break;
2626
        default:
2627
            MIPS_INVAL("conditional branch/jump");
2628
            generate_exception(ctx, EXCP_RI);
2629
            return;
2630
        }
2631
    }
2632
    MIPS_DEBUG("enter ds: link %d cond %02x target " TARGET_FMT_lx,
2633
               blink, ctx->hflags, btarget);
2634

    
2635
    ctx->btarget = btarget;
2636
    if (blink > 0) {
2637
        tcg_gen_movi_tl(cpu_T[0], ctx->pc + 8);
2638
        gen_store_gpr(cpu_T[0], blink);
2639
    }
2640
}
2641

    
2642
/* special3 bitfield operations */
2643
static void gen_bitops (DisasContext *ctx, uint32_t opc, int rt,
2644
                       int rs, int lsb, int msb)
2645
{
2646
    gen_load_gpr(cpu_T[1], rs);
2647
    switch (opc) {
2648
    case OPC_EXT:
2649
        if (lsb + msb > 31)
2650
            goto fail;
2651
        tcg_gen_helper_0_2ii(do_ext, lsb, msb + 1);
2652
        break;
2653
#if defined(TARGET_MIPS64)
2654
    case OPC_DEXTM:
2655
        if (lsb + msb > 63)
2656
            goto fail;
2657
        tcg_gen_helper_0_2ii(do_dext, lsb, msb + 1 + 32);
2658
        break;
2659
    case OPC_DEXTU:
2660
        if (lsb + msb > 63)
2661
            goto fail;
2662
        tcg_gen_helper_0_2ii(do_dext, lsb + 32, msb + 1);
2663
        break;
2664
    case OPC_DEXT:
2665
        if (lsb + msb > 63)
2666
            goto fail;
2667
        tcg_gen_helper_0_2ii(do_dext, lsb, msb + 1);
2668
        break;
2669
#endif
2670
    case OPC_INS:
2671
        if (lsb > msb)
2672
            goto fail;
2673
        gen_load_gpr(cpu_T[0], rt);
2674
        tcg_gen_helper_0_2ii(do_ins, lsb, msb - lsb + 1);
2675
        break;
2676
#if defined(TARGET_MIPS64)
2677
    case OPC_DINSM:
2678
        if (lsb > msb)
2679
            goto fail;
2680
        gen_load_gpr(cpu_T[0], rt);
2681
        tcg_gen_helper_0_2ii(do_dins, lsb, msb - lsb + 1 + 32);
2682
        break;
2683
    case OPC_DINSU:
2684
        if (lsb > msb)
2685
            goto fail;
2686
        gen_load_gpr(cpu_T[0], rt);
2687
        tcg_gen_helper_0_2ii(do_dins, lsb + 32, msb - lsb + 1);
2688
        break;
2689
    case OPC_DINS:
2690
        if (lsb > msb)
2691
            goto fail;
2692
        gen_load_gpr(cpu_T[0], rt);
2693
        tcg_gen_helper_0_2ii(do_dins, lsb, msb - lsb + 1);
2694
        break;
2695
#endif
2696
    default:
2697
fail:
2698
        MIPS_INVAL("bitops");
2699
        generate_exception(ctx, EXCP_RI);
2700
        return;
2701
    }
2702
    gen_store_gpr(cpu_T[0], rt);
2703
}
2704

    
2705
/* CP0 (MMU and control) */
2706
#ifndef CONFIG_USER_ONLY
2707
static inline void gen_mfc0_load32 (TCGv t, target_ulong off)
2708
{
2709
    TCGv r_tmp = tcg_temp_new(TCG_TYPE_I32);
2710

    
2711
    tcg_gen_ld_i32(r_tmp, cpu_env, off);
2712
    tcg_gen_ext_i32_tl(t, r_tmp);
2713
    tcg_temp_free(r_tmp);
2714
}
2715

    
2716
static inline void gen_mfc0_load64 (TCGv t, target_ulong off)
2717
{
2718
    tcg_gen_ld_tl(t, cpu_env, off);
2719
    tcg_gen_ext32s_tl(t, t);
2720
}
2721

    
2722
static inline void gen_mtc0_store32 (TCGv t, target_ulong off)
2723
{
2724
    TCGv r_tmp = tcg_temp_new(TCG_TYPE_I32);
2725

    
2726
    tcg_gen_trunc_tl_i32(r_tmp, t);
2727
    tcg_gen_st_i32(r_tmp, cpu_env, off);
2728
    tcg_temp_free(r_tmp);
2729
}
2730

    
2731
static inline void gen_mtc0_store64 (TCGv t, target_ulong off)
2732
{
2733
    tcg_gen_ext32s_tl(t, t);
2734
    tcg_gen_st_tl(t, cpu_env, off);
2735
}
2736

    
2737
static void gen_mfc0 (CPUState *env, DisasContext *ctx, int reg, int sel)
2738
{
2739
    const char *rn = "invalid";
2740

    
2741
    if (sel != 0)
2742
        check_insn(env, ctx, ISA_MIPS32);
2743

    
2744
    switch (reg) {
2745
    case 0:
2746
        switch (sel) {
2747
        case 0:
2748
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Index));
2749
            rn = "Index";
2750
            break;
2751
        case 1:
2752
            check_insn(env, ctx, ASE_MT);
2753
            tcg_gen_helper_0_0(do_mfc0_mvpcontrol);
2754
            rn = "MVPControl";
2755
            break;
2756
        case 2:
2757
            check_insn(env, ctx, ASE_MT);
2758
            tcg_gen_helper_0_0(do_mfc0_mvpconf0);
2759
            rn = "MVPConf0";
2760
            break;
2761
        case 3:
2762
            check_insn(env, ctx, ASE_MT);
2763
            tcg_gen_helper_0_0(do_mfc0_mvpconf1);
2764
            rn = "MVPConf1";
2765
            break;
2766
        default:
2767
            goto die;
2768
        }
2769
        break;
2770
    case 1:
2771
        switch (sel) {
2772
        case 0:
2773
            tcg_gen_helper_0_0(do_mfc0_random);
2774
            rn = "Random";
2775
            break;
2776
        case 1:
2777
            check_insn(env, ctx, ASE_MT);
2778
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_VPEControl));
2779
            rn = "VPEControl";
2780
            break;
2781
        case 2:
2782
            check_insn(env, ctx, ASE_MT);
2783
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_VPEConf0));
2784
            rn = "VPEConf0";
2785
            break;
2786
        case 3:
2787
            check_insn(env, ctx, ASE_MT);
2788
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_VPEConf1));
2789
            rn = "VPEConf1";
2790
            break;
2791
        case 4:
2792
            check_insn(env, ctx, ASE_MT);
2793
            gen_mfc0_load64(cpu_T[0], offsetof(CPUState, CP0_YQMask));
2794
            rn = "YQMask";
2795
            break;
2796
        case 5:
2797
            check_insn(env, ctx, ASE_MT);
2798
            gen_mfc0_load64(cpu_T[0], offsetof(CPUState, CP0_VPESchedule));
2799
            rn = "VPESchedule";
2800
            break;
2801
        case 6:
2802
            check_insn(env, ctx, ASE_MT);
2803
            gen_mfc0_load64(cpu_T[0], offsetof(CPUState, CP0_VPEScheFBack));
2804
            rn = "VPEScheFBack";
2805
            break;
2806
        case 7:
2807
            check_insn(env, ctx, ASE_MT);
2808
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_VPEOpt));
2809
            rn = "VPEOpt";
2810
            break;
2811
        default:
2812
            goto die;
2813
        }
2814
        break;
2815
    case 2:
2816
        switch (sel) {
2817
        case 0:
2818
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_EntryLo0));
2819
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
2820
            rn = "EntryLo0";
2821
            break;
2822
        case 1:
2823
            check_insn(env, ctx, ASE_MT);
2824
            tcg_gen_helper_0_0(do_mfc0_tcstatus);
2825
            rn = "TCStatus";
2826
            break;
2827
        case 2:
2828
            check_insn(env, ctx, ASE_MT);
2829
            tcg_gen_helper_0_0(do_mfc0_tcbind);
2830
            rn = "TCBind";
2831
            break;
2832
        case 3:
2833
            check_insn(env, ctx, ASE_MT);
2834
            tcg_gen_helper_0_0(do_mfc0_tcrestart);
2835
            rn = "TCRestart";
2836
            break;
2837
        case 4:
2838
            check_insn(env, ctx, ASE_MT);
2839
            tcg_gen_helper_0_0(do_mfc0_tchalt);
2840
            rn = "TCHalt";
2841
            break;
2842
        case 5:
2843
            check_insn(env, ctx, ASE_MT);
2844
            tcg_gen_helper_0_0(do_mfc0_tccontext);
2845
            rn = "TCContext";
2846
            break;
2847
        case 6:
2848
            check_insn(env, ctx, ASE_MT);
2849
            tcg_gen_helper_0_0(do_mfc0_tcschedule);
2850
            rn = "TCSchedule";
2851
            break;
2852
        case 7:
2853
            check_insn(env, ctx, ASE_MT);
2854
            tcg_gen_helper_0_0(do_mfc0_tcschefback);
2855
            rn = "TCScheFBack";
2856
            break;
2857
        default:
2858
            goto die;
2859
        }
2860
        break;
2861
    case 3:
2862
        switch (sel) {
2863
        case 0:
2864
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_EntryLo1));
2865
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
2866
            rn = "EntryLo1";
2867
            break;
2868
        default:
2869
            goto die;
2870
        }
2871
        break;
2872
    case 4:
2873
        switch (sel) {
2874
        case 0:
2875
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_Context));
2876
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
2877
            rn = "Context";
2878
            break;
2879
        case 1:
2880
//            tcg_gen_helper_0_0(do_mfc0_contextconfig); /* SmartMIPS ASE */
2881
            rn = "ContextConfig";
2882
//            break;
2883
        default:
2884
            goto die;
2885
        }
2886
        break;
2887
    case 5:
2888
        switch (sel) {
2889
        case 0:
2890
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_PageMask));
2891
            rn = "PageMask";
2892
            break;
2893
        case 1:
2894
            check_insn(env, ctx, ISA_MIPS32R2);
2895
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_PageGrain));
2896
            rn = "PageGrain";
2897
            break;
2898
        default:
2899
            goto die;
2900
        }
2901
        break;
2902
    case 6:
2903
        switch (sel) {
2904
        case 0:
2905
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Wired));
2906
            rn = "Wired";
2907
            break;
2908
        case 1:
2909
            check_insn(env, ctx, ISA_MIPS32R2);
2910
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSConf0));
2911
            rn = "SRSConf0";
2912
            break;
2913
        case 2:
2914
            check_insn(env, ctx, ISA_MIPS32R2);
2915
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSConf1));
2916
            rn = "SRSConf1";
2917
            break;
2918
        case 3:
2919
            check_insn(env, ctx, ISA_MIPS32R2);
2920
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSConf2));
2921
            rn = "SRSConf2";
2922
            break;
2923
        case 4:
2924
            check_insn(env, ctx, ISA_MIPS32R2);
2925
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSConf3));
2926
            rn = "SRSConf3";
2927
            break;
2928
        case 5:
2929
            check_insn(env, ctx, ISA_MIPS32R2);
2930
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSConf4));
2931
            rn = "SRSConf4";
2932
            break;
2933
        default:
2934
            goto die;
2935
        }
2936
        break;
2937
    case 7:
2938
        switch (sel) {
2939
        case 0:
2940
            check_insn(env, ctx, ISA_MIPS32R2);
2941
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_HWREna));
2942
            rn = "HWREna";
2943
            break;
2944
        default:
2945
            goto die;
2946
        }
2947
        break;
2948
    case 8:
2949
        switch (sel) {
2950
        case 0:
2951
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_BadVAddr));
2952
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
2953
            rn = "BadVAddr";
2954
            break;
2955
        default:
2956
            goto die;
2957
       }
2958
        break;
2959
    case 9:
2960
        switch (sel) {
2961
        case 0:
2962
            tcg_gen_helper_0_0(do_mfc0_count);
2963
            rn = "Count";
2964
            break;
2965
        /* 6,7 are implementation dependent */
2966
        default:
2967
            goto die;
2968
        }
2969
        break;
2970
    case 10:
2971
        switch (sel) {
2972
        case 0:
2973
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_EntryHi));
2974
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
2975
            rn = "EntryHi";
2976
            break;
2977
        default:
2978
            goto die;
2979
        }
2980
        break;
2981
    case 11:
2982
        switch (sel) {
2983
        case 0:
2984
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Compare));
2985
            rn = "Compare";
2986
            break;
2987
        /* 6,7 are implementation dependent */
2988
        default:
2989
            goto die;
2990
        }
2991
        break;
2992
    case 12:
2993
        switch (sel) {
2994
        case 0:
2995
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Status));
2996
            rn = "Status";
2997
            break;
2998
        case 1:
2999
            check_insn(env, ctx, ISA_MIPS32R2);
3000
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_IntCtl));
3001
            rn = "IntCtl";
3002
            break;
3003
        case 2:
3004
            check_insn(env, ctx, ISA_MIPS32R2);
3005
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSCtl));
3006
            rn = "SRSCtl";
3007
            break;
3008
        case 3:
3009
            check_insn(env, ctx, ISA_MIPS32R2);
3010
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSMap));
3011
            rn = "SRSMap";
3012
            break;
3013
        default:
3014
            goto die;
3015
       }
3016
        break;
3017
    case 13:
3018
        switch (sel) {
3019
        case 0:
3020
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Cause));
3021
            rn = "Cause";
3022
            break;
3023
        default:
3024
            goto die;
3025
       }
3026
        break;
3027
    case 14:
3028
        switch (sel) {
3029
        case 0:
3030
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_EPC));
3031
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
3032
            rn = "EPC";
3033
            break;
3034
        default:
3035
            goto die;
3036
        }
3037
        break;
3038
    case 15:
3039
        switch (sel) {
3040
        case 0:
3041
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_PRid));
3042
            rn = "PRid";
3043
            break;
3044
        case 1:
3045
            check_insn(env, ctx, ISA_MIPS32R2);
3046
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_EBase));
3047
            rn = "EBase";
3048
            break;
3049
        default:
3050
            goto die;
3051
       }
3052
        break;
3053
    case 16:
3054
        switch (sel) {
3055
        case 0:
3056
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config0));
3057
            rn = "Config";
3058
            break;
3059
        case 1:
3060
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config1));
3061
            rn = "Config1";
3062
            break;
3063
        case 2:
3064
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config2));
3065
            rn = "Config2";
3066
            break;
3067
        case 3:
3068
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config3));
3069
            rn = "Config3";
3070
            break;
3071
        /* 4,5 are reserved */
3072
        /* 6,7 are implementation dependent */
3073
        case 6:
3074
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config6));
3075
            rn = "Config6";
3076
            break;
3077
        case 7:
3078
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config7));
3079
            rn = "Config7";
3080
            break;
3081
        default:
3082
            goto die;
3083
        }
3084
        break;
3085
    case 17:
3086
        switch (sel) {
3087
        case 0:
3088
            tcg_gen_helper_0_0(do_mfc0_lladdr);
3089
            rn = "LLAddr";
3090
            break;
3091
        default:
3092
            goto die;
3093
        }
3094
        break;
3095
    case 18:
3096
        switch (sel) {
3097
        case 0 ... 7:
3098
            tcg_gen_helper_0_1i(do_mfc0_watchlo, sel);
3099
            rn = "WatchLo";
3100
            break;
3101
        default:
3102
            goto die;
3103
        }
3104
        break;
3105
    case 19:
3106
        switch (sel) {
3107
        case 0 ...7:
3108
            tcg_gen_helper_0_1i(do_mfc0_watchhi, sel);
3109
            rn = "WatchHi";
3110
            break;
3111
        default:
3112
            goto die;
3113
        }
3114
        break;
3115
    case 20:
3116
        switch (sel) {
3117
        case 0:
3118
#if defined(TARGET_MIPS64)
3119
            check_insn(env, ctx, ISA_MIPS3);
3120
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_XContext));
3121
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
3122
            rn = "XContext";
3123
            break;
3124
#endif
3125
        default:
3126
            goto die;
3127
        }
3128
        break;
3129
    case 21:
3130
       /* Officially reserved, but sel 0 is used for R1x000 framemask */
3131
        switch (sel) {
3132
        case 0:
3133
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Framemask));
3134
            rn = "Framemask";
3135
            break;
3136
        default:
3137
            goto die;
3138
        }
3139
        break;
3140
    case 22:
3141
        /* ignored */
3142
        rn = "'Diagnostic"; /* implementation dependent */
3143
        break;
3144
    case 23:
3145
        switch (sel) {
3146
        case 0:
3147
            tcg_gen_helper_0_0(do_mfc0_debug); /* EJTAG support */
3148
            rn = "Debug";
3149
            break;
3150
        case 1:
3151
//            tcg_gen_helper_0_0(do_mfc0_tracecontrol); /* PDtrace support */
3152
            rn = "TraceControl";
3153
//            break;
3154
        case 2:
3155
//            tcg_gen_helper_0_0(do_mfc0_tracecontrol2); /* PDtrace support */
3156
            rn = "TraceControl2";
3157
//            break;
3158
        case 3:
3159
//            tcg_gen_helper_0_0(do_mfc0_usertracedata); /* PDtrace support */
3160
            rn = "UserTraceData";
3161
//            break;
3162
        case 4:
3163
//            tcg_gen_helper_0_0(do_mfc0_debug); /* PDtrace support */
3164
            rn = "TraceBPC";
3165
//            break;
3166
        default:
3167
            goto die;
3168
        }
3169
        break;
3170
    case 24:
3171
        switch (sel) {
3172
        case 0:
3173
            /* EJTAG support */
3174
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_DEPC));
3175
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
3176
            rn = "DEPC";
3177
            break;
3178
        default:
3179
            goto die;
3180
        }
3181
        break;
3182
    case 25:
3183
        switch (sel) {
3184
        case 0:
3185
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Performance0));
3186
            rn = "Performance0";
3187
            break;
3188
        case 1:
3189
//            tcg_gen_helper_0_0(do_mfc0_performance1);
3190
            rn = "Performance1";
3191
//            break;
3192
        case 2:
3193
//            tcg_gen_helper_0_0(do_mfc0_performance2);
3194
            rn = "Performance2";
3195
//            break;
3196
        case 3:
3197
//            tcg_gen_helper_0_0(do_mfc0_performance3);
3198
            rn = "Performance3";
3199
//            break;
3200
        case 4:
3201
//            tcg_gen_helper_0_0(do_mfc0_performance4);
3202
            rn = "Performance4";
3203
//            break;
3204
        case 5:
3205
//            tcg_gen_helper_0_0(do_mfc0_performance5);
3206
            rn = "Performance5";
3207
//            break;
3208
        case 6:
3209
//            tcg_gen_helper_0_0(do_mfc0_performance6);
3210
            rn = "Performance6";
3211
//            break;
3212
        case 7:
3213
//            tcg_gen_helper_0_0(do_mfc0_performance7);
3214
            rn = "Performance7";
3215
//            break;
3216
        default:
3217
            goto die;
3218
        }
3219
        break;
3220
    case 26:
3221
       rn = "ECC";
3222
       break;
3223
    case 27:
3224
        switch (sel) {
3225
        /* ignored */
3226
        case 0 ... 3:
3227
            rn = "CacheErr";
3228
            break;
3229
        default:
3230
            goto die;
3231
        }
3232
        break;
3233
    case 28:
3234
        switch (sel) {
3235
        case 0:
3236
        case 2:
3237
        case 4:
3238
        case 6:
3239
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_TagLo));
3240
            rn = "TagLo";
3241
            break;
3242
        case 1:
3243
        case 3:
3244
        case 5:
3245
        case 7:
3246
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_DataLo));
3247
            rn = "DataLo";
3248
            break;
3249
        default:
3250
            goto die;
3251
        }
3252
        break;
3253
    case 29:
3254
        switch (sel) {
3255
        case 0:
3256
        case 2:
3257
        case 4:
3258
        case 6:
3259
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_TagHi));
3260
            rn = "TagHi";
3261
            break;
3262
        case 1:
3263
        case 3:
3264
        case 5:
3265
        case 7:
3266
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_DataHi));
3267
            rn = "DataHi";
3268
            break;
3269
        default:
3270
            goto die;
3271
        }
3272
        break;
3273
    case 30:
3274
        switch (sel) {
3275
        case 0:
3276
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_ErrorEPC));
3277
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
3278
            rn = "ErrorEPC";
3279
            break;
3280
        default:
3281
            goto die;
3282
        }
3283
        break;
3284
    case 31:
3285
        switch (sel) {
3286
        case 0:
3287
            /* EJTAG support */
3288
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_DESAVE));
3289
            rn = "DESAVE";
3290
            break;
3291
        default:
3292
            goto die;
3293
        }
3294
        break;
3295
    default:
3296
       goto die;
3297
    }
3298
#if defined MIPS_DEBUG_DISAS
3299
    if (loglevel & CPU_LOG_TB_IN_ASM) {
3300
        fprintf(logfile, "mfc0 %s (reg %d sel %d)\n",
3301
                rn, reg, sel);
3302
    }
3303
#endif
3304
    return;
3305

    
3306
die:
3307
#if defined MIPS_DEBUG_DISAS
3308
    if (loglevel & CPU_LOG_TB_IN_ASM) {
3309
        fprintf(logfile, "mfc0 %s (reg %d sel %d)\n",
3310
                rn, reg, sel);
3311
    }
3312
#endif
3313
    generate_exception(ctx, EXCP_RI);
3314
}
3315

    
3316
static void gen_mtc0 (CPUState *env, DisasContext *ctx, int reg, int sel)
3317
{
3318
    const char *rn = "invalid";
3319

    
3320
    if (sel != 0)
3321
        check_insn(env, ctx, ISA_MIPS32);
3322

    
3323
    switch (reg) {
3324
    case 0:
3325
        switch (sel) {
3326
        case 0:
3327
            tcg_gen_helper_0_0(do_mtc0_index);
3328
            rn = "Index";
3329
            break;
3330
        case 1:
3331
            check_insn(env, ctx, ASE_MT);
3332
            tcg_gen_helper_0_0(do_mtc0_mvpcontrol);
3333
            rn = "MVPControl";
3334
            break;
3335
        case 2:
3336
            check_insn(env, ctx, ASE_MT);
3337
            /* ignored */
3338
            rn = "MVPConf0";
3339
            break;
3340
        case 3:
3341
            check_insn(env, ctx, ASE_MT);
3342
            /* ignored */
3343
            rn = "MVPConf1";
3344
            break;
3345
        default:
3346
            goto die;
3347
        }
3348
        break;
3349
    case 1:
3350
        switch (sel) {
3351
        case 0:
3352
            /* ignored */
3353
            rn = "Random";
3354
            break;
3355
        case 1:
3356
            check_insn(env, ctx, ASE_MT);
3357
            tcg_gen_helper_0_0(do_mtc0_vpecontrol);
3358
            rn = "VPEControl";
3359
            break;
3360
        case 2:
3361
            check_insn(env, ctx, ASE_MT);
3362
            tcg_gen_helper_0_0(do_mtc0_vpeconf0);
3363
            rn = "VPEConf0";
3364
            break;
3365
        case 3:
3366
            check_insn(env, ctx, ASE_MT);
3367
            tcg_gen_helper_0_0(do_mtc0_vpeconf1);
3368
            rn = "VPEConf1";
3369
            break;
3370
        case 4:
3371
            check_insn(env, ctx, ASE_MT);
3372
            tcg_gen_helper_0_0(do_mtc0_yqmask);
3373
            rn = "YQMask";
3374
            break;
3375
        case 5:
3376
            check_insn(env, ctx, ASE_MT);
3377
            gen_mtc0_store64(cpu_T[0], offsetof(CPUState, CP0_VPESchedule));
3378
            rn = "VPESchedule";
3379
            break;
3380
        case 6:
3381
            check_insn(env, ctx, ASE_MT);
3382
            gen_mtc0_store64(cpu_T[0], offsetof(CPUState, CP0_VPEScheFBack));
3383
            rn = "VPEScheFBack";
3384
            break;
3385
        case 7:
3386
            check_insn(env, ctx, ASE_MT);
3387
            tcg_gen_helper_0_0(do_mtc0_vpeopt);
3388
            rn = "VPEOpt";
3389
            break;
3390
        default:
3391
            goto die;
3392
        }
3393
        break;
3394
    case 2:
3395
        switch (sel) {
3396
        case 0:
3397
            tcg_gen_helper_0_0(do_mtc0_entrylo0);
3398
            rn = "EntryLo0";
3399
            break;
3400
        case 1:
3401
            check_insn(env, ctx, ASE_MT);
3402
            tcg_gen_helper_0_0(do_mtc0_tcstatus);
3403
            rn = "TCStatus";
3404
            break;
3405
        case 2:
3406
            check_insn(env, ctx, ASE_MT);
3407
            tcg_gen_helper_0_0(do_mtc0_tcbind);
3408
            rn = "TCBind";
3409
            break;
3410
        case 3:
3411
            check_insn(env, ctx, ASE_MT);
3412
            tcg_gen_helper_0_0(do_mtc0_tcrestart);
3413
            rn = "TCRestart";
3414
            break;
3415
        case 4:
3416
            check_insn(env, ctx, ASE_MT);
3417
            tcg_gen_helper_0_0(do_mtc0_tchalt);
3418
            rn = "TCHalt";
3419
            break;
3420
        case 5:
3421
            check_insn(env, ctx, ASE_MT);
3422
            tcg_gen_helper_0_0(do_mtc0_tccontext);
3423
            rn = "TCContext";
3424
            break;
3425
        case 6:
3426
            check_insn(env, ctx, ASE_MT);
3427
            tcg_gen_helper_0_0(do_mtc0_tcschedule);
3428
            rn = "TCSchedule";
3429
            break;
3430
        case 7:
3431
            check_insn(env, ctx, ASE_MT);
3432
            tcg_gen_helper_0_0(do_mtc0_tcschefback);
3433
            rn = "TCScheFBack";
3434
            break;
3435
        default:
3436
            goto die;
3437
        }
3438
        break;
3439
    case 3:
3440
        switch (sel) {
3441
        case 0:
3442
            tcg_gen_helper_0_0(do_mtc0_entrylo1);
3443
            rn = "EntryLo1";
3444
            break;
3445
        default:
3446
            goto die;
3447
        }
3448
        break;
3449
    case 4:
3450
        switch (sel) {
3451
        case 0:
3452
            tcg_gen_helper_0_0(do_mtc0_context);
3453
            rn = "Context";
3454
            break;
3455
        case 1:
3456
//            tcg_gen_helper_0_0(do_mtc0_contextconfig); /* SmartMIPS ASE */
3457
            rn = "ContextConfig";
3458
//            break;
3459
        default:
3460
            goto die;
3461
        }
3462
        break;
3463
    case 5:
3464
        switch (sel) {
3465
        case 0:
3466
            tcg_gen_helper_0_0(do_mtc0_pagemask);
3467
            rn = "PageMask";
3468
            break;
3469
        case 1:
3470
            check_insn(env, ctx, ISA_MIPS32R2);
3471
            tcg_gen_helper_0_0(do_mtc0_pagegrain);
3472
            rn = "PageGrain";
3473
            break;
3474
        default:
3475
            goto die;
3476
        }
3477
        break;
3478
    case 6:
3479
        switch (sel) {
3480
        case 0:
3481
            tcg_gen_helper_0_0(do_mtc0_wired);
3482
            rn = "Wired";
3483
            break;
3484
        case 1:
3485
            check_insn(env, ctx, ISA_MIPS32R2);
3486
            tcg_gen_helper_0_0(do_mtc0_srsconf0);
3487
            rn = "SRSConf0";
3488
            break;
3489
        case 2:
3490
            check_insn(env, ctx, ISA_MIPS32R2);
3491
            tcg_gen_helper_0_0(do_mtc0_srsconf1);
3492
            rn = "SRSConf1";
3493
            break;
3494
        case 3:
3495
            check_insn(env, ctx, ISA_MIPS32R2);
3496
            tcg_gen_helper_0_0(do_mtc0_srsconf2);
3497
            rn = "SRSConf2";
3498
            break;
3499
        case 4:
3500
            check_insn(env, ctx, ISA_MIPS32R2);
3501
            tcg_gen_helper_0_0(do_mtc0_srsconf3);
3502
            rn = "SRSConf3";
3503
            break;
3504
        case 5:
3505
            check_insn(env, ctx, ISA_MIPS32R2);
3506
            tcg_gen_helper_0_0(do_mtc0_srsconf4);
3507
            rn = "SRSConf4";
3508
            break;
3509
        default:
3510
            goto die;
3511
        }
3512
        break;
3513
    case 7:
3514
        switch (sel) {
3515
        case 0:
3516
            check_insn(env, ctx, ISA_MIPS32R2);
3517
            tcg_gen_helper_0_0(do_mtc0_hwrena);
3518
            rn = "HWREna";
3519
            break;
3520
        default:
3521
            goto die;
3522
        }
3523
        break;
3524
    case 8:
3525
        /* ignored */
3526
        rn = "BadVAddr";
3527
        break;
3528
    case 9:
3529
        switch (sel) {
3530
        case 0:
3531
            tcg_gen_helper_0_0(do_mtc0_count);
3532
            rn = "Count";
3533
            break;
3534
        /* 6,7 are implementation dependent */
3535
        default:
3536
            goto die;
3537
        }
3538
        /* Stop translation as we may have switched the execution mode */
3539
        ctx->bstate = BS_STOP;
3540
        break;
3541
    case 10:
3542
        switch (sel) {
3543
        case 0:
3544
            tcg_gen_helper_0_0(do_mtc0_entryhi);
3545
            rn = "EntryHi";
3546
            break;
3547
        default:
3548
            goto die;
3549
        }
3550
        break;
3551
    case 11:
3552
        switch (sel) {
3553
        case 0:
3554
            tcg_gen_helper_0_0(do_mtc0_compare);
3555
            rn = "Compare";
3556
            break;
3557
        /* 6,7 are implementation dependent */
3558
        default:
3559
            goto die;
3560
        }
3561
        /* Stop translation as we may have switched the execution mode */
3562
        ctx->bstate = BS_STOP;
3563
        break;
3564
    case 12:
3565
        switch (sel) {
3566
        case 0:
3567
            tcg_gen_helper_0_0(do_mtc0_status);
3568
            /* BS_STOP isn't good enough here, hflags may have changed. */
3569
            gen_save_pc(ctx->pc + 4);
3570
            ctx->bstate = BS_EXCP;
3571
            rn = "Status";
3572
            break;
3573
        case 1:
3574
            check_insn(env, ctx, ISA_MIPS32R2);
3575
            tcg_gen_helper_0_0(do_mtc0_intctl);
3576
            /* Stop translation as we may have switched the execution mode */
3577
            ctx->bstate = BS_STOP;
3578
            rn = "IntCtl";
3579
            break;
3580
        case 2:
3581
            check_insn(env, ctx, ISA_MIPS32R2);
3582
            tcg_gen_helper_0_0(do_mtc0_srsctl);
3583
            /* Stop translation as we may have switched the execution mode */
3584
            ctx->bstate = BS_STOP;
3585
            rn = "SRSCtl";
3586
            break;
3587
        case 3:
3588
            check_insn(env, ctx, ISA_MIPS32R2);
3589
            gen_mtc0_store32(cpu_T[0], offsetof(CPUState, CP0_SRSMap));
3590
            /* Stop translation as we may have switched the execution mode */
3591
            ctx->bstate = BS_STOP;
3592
            rn = "SRSMap";
3593
            break;
3594
        default:
3595
            goto die;
3596
        }
3597
        break;
3598
    case 13:
3599
        switch (sel) {
3600
        case 0:
3601
            tcg_gen_helper_0_0(do_mtc0_cause);
3602
            rn = "Cause";
3603
            break;
3604
        default:
3605
            goto die;
3606
        }
3607
        /* Stop translation as we may have switched the execution mode */
3608
        ctx->bstate = BS_STOP;
3609
        break;
3610
    case 14:
3611
        switch (sel) {
3612
        case 0:
3613
            gen_mtc0_store64(cpu_T[0], offsetof(CPUState, CP0_EPC));
3614
            rn = "EPC";
3615
            break;
3616
        default:
3617
            goto die;
3618
        }
3619
        break;
3620
    case 15:
3621
        switch (sel) {
3622
        case 0:
3623
            /* ignored */
3624
            rn = "PRid";
3625
            break;
3626
        case 1:
3627
            check_insn(env, ctx, ISA_MIPS32R2);
3628
            tcg_gen_helper_0_0(do_mtc0_ebase);
3629
            rn = "EBase";
3630
            break;
3631
        default:
3632
            goto die;
3633
        }
3634
        break;
3635
    case 16:
3636
        switch (sel) {
3637
        case 0:
3638
            tcg_gen_helper_0_0(do_mtc0_config0);
3639
            rn = "Config";
3640
            /* Stop translation as we may have switched the execution mode */
3641
            ctx->bstate = BS_STOP;
3642
            break;
3643
        case 1:
3644
            /* ignored, read only */
3645
            rn = "Config1";
3646
            break;
3647
        case 2:
3648
            tcg_gen_helper_0_0(do_mtc0_config2);
3649
            rn = "Config2";
3650
            /* Stop translation as we may have switched the execution mode */
3651
            ctx->bstate = BS_STOP;
3652
            break;
3653
        case 3:
3654
            /* ignored, read only */
3655
            rn = "Config3";
3656
            break;
3657
        /* 4,5 are reserved */
3658
        /* 6,7 are implementation dependent */
3659
        case 6:
3660
            /* ignored */
3661
            rn = "Config6";
3662
            break;
3663
        case 7:
3664
            /* ignored */
3665
            rn = "Config7";
3666
            break;
3667
        default:
3668
            rn = "Invalid config selector";
3669
            goto die;
3670
        }
3671
        break;
3672
    case 17:
3673
        switch (sel) {
3674
        case 0:
3675
            /* ignored */
3676
            rn = "LLAddr";
3677
            break;
3678
        default:
3679
            goto die;
3680
        }
3681
        break;
3682
    case 18:
3683
        switch (sel) {
3684
        case 0 ... 7:
3685
            tcg_gen_helper_0_1i(do_mtc0_watchlo, sel);
3686
            rn = "WatchLo";
3687
            break;
3688
        default:
3689
            goto die;
3690
        }
3691
        break;
3692
    case 19:
3693
        switch (sel) {
3694
        case 0 ... 7:
3695
            tcg_gen_helper_0_1i(do_mtc0_watchhi, sel);
3696
            rn = "WatchHi";
3697
            break;
3698
        default:
3699
            goto die;
3700
        }
3701
        break;
3702
    case 20:
3703
        switch (sel) {
3704
        case 0:
3705
#if defined(TARGET_MIPS64)
3706
            check_insn(env, ctx, ISA_MIPS3);
3707
            tcg_gen_helper_0_0(do_mtc0_xcontext);
3708
            rn = "XContext";
3709
            break;
3710
#endif
3711
        default:
3712
            goto die;
3713
        }
3714
        break;
3715
    case 21:
3716
       /* Officially reserved, but sel 0 is used for R1x000 framemask */
3717
        switch (sel) {
3718
        case 0:
3719
            tcg_gen_helper_0_0(do_mtc0_framemask);
3720
            rn = "Framemask";
3721
            break;
3722
        default:
3723
            goto die;
3724
        }
3725
        break;
3726
    case 22:
3727
        /* ignored */
3728
        rn = "Diagnostic"; /* implementation dependent */
3729
        break;
3730
    case 23:
3731
        switch (sel) {
3732
        case 0:
3733
            tcg_gen_helper_0_0(do_mtc0_debug); /* EJTAG support */
3734
            /* BS_STOP isn't good enough here, hflags may have changed. */
3735
            gen_save_pc(ctx->pc + 4);
3736
            ctx->bstate = BS_EXCP;
3737
            rn = "Debug";
3738
            break;
3739
        case 1:
3740
//            tcg_gen_helper_0_0(do_mtc0_tracecontrol); /* PDtrace support */
3741
            rn = "TraceControl";
3742
            /* Stop translation as we may have switched the execution mode */
3743
            ctx->bstate = BS_STOP;
3744
//            break;
3745
        case 2:
3746
//            tcg_gen_helper_0_0(do_mtc0_tracecontrol2); /* PDtrace support */
3747
            rn = "TraceControl2";
3748
            /* Stop translation as we may have switched the execution mode */
3749
            ctx->bstate = BS_STOP;
3750
//            break;
3751
        case 3:
3752
            /* Stop translation as we may have switched the execution mode */
3753
            ctx->bstate = BS_STOP;
3754
//            tcg_gen_helper_0_0(do_mtc0_usertracedata); /* PDtrace support */
3755
            rn = "UserTraceData";
3756
            /* Stop translation as we may have switched the execution mode */
3757
            ctx->bstate = BS_STOP;
3758
//            break;
3759
        case 4:
3760
//            tcg_gen_helper_0_0(do_mtc0_debug); /* PDtrace support */
3761
            /* Stop translation as we may have switched the execution mode */
3762
            ctx->bstate = BS_STOP;
3763
            rn = "TraceBPC";
3764
//            break;
3765
        default:
3766
            goto die;
3767
        }
3768
        break;
3769
    case 24:
3770
        switch (sel) {
3771
        case 0:
3772
            /* EJTAG support */
3773
            gen_mtc0_store64(cpu_T[0], offsetof(CPUState, CP0_DEPC));
3774
            rn = "DEPC";
3775
            break;
3776
        default:
3777
            goto die;
3778
        }
3779
        break;
3780
    case 25:
3781
        switch (sel) {
3782
        case 0:
3783
            tcg_gen_helper_0_0(do_mtc0_performance0);
3784
            rn = "Performance0";
3785
            break;
3786
        case 1:
3787
//            tcg_gen_helper_0_0(do_mtc0_performance1);
3788
            rn = "Performance1";
3789
//            break;
3790
        case 2:
3791
//            tcg_gen_helper_0_0(do_mtc0_performance2);
3792
            rn = "Performance2";
3793
//            break;
3794
        case 3:
3795
//            tcg_gen_helper_0_0(do_mtc0_performance3);
3796
            rn = "Performance3";
3797
//            break;
3798
        case 4:
3799
//            tcg_gen_helper_0_0(do_mtc0_performance4);
3800
            rn = "Performance4";
3801
//            break;
3802
        case 5:
3803
//            tcg_gen_helper_0_0(do_mtc0_performance5);
3804
            rn = "Performance5";
3805
//            break;
3806
        case 6:
3807
//            tcg_gen_helper_0_0(do_mtc0_performance6);
3808
            rn = "Performance6";
3809
//            break;
3810
        case 7:
3811
//            tcg_gen_helper_0_0(do_mtc0_performance7);
3812
            rn = "Performance7";
3813
//            break;
3814
        default:
3815
            goto die;
3816
        }
3817
       break;
3818
    case 26:
3819
        /* ignored */
3820
        rn = "ECC";
3821
        break;
3822
    case 27:
3823
        switch (sel) {
3824
        case 0 ... 3:
3825
            /* ignored */
3826
            rn = "CacheErr";
3827
            break;
3828
        default:
3829
            goto die;
3830
        }
3831
       break;
3832
    case 28:
3833
        switch (sel) {
3834
        case 0:
3835
        case 2:
3836
        case 4:
3837
        case 6:
3838
            tcg_gen_helper_0_0(do_mtc0_taglo);
3839
            rn = "TagLo";
3840
            break;
3841
        case 1:
3842
        case 3:
3843
        case 5:
3844
        case 7:
3845
            tcg_gen_helper_0_0(do_mtc0_datalo);
3846
            rn = "DataLo";
3847
            break;
3848
        default:
3849
            goto die;
3850
        }
3851
        break;
3852
    case 29:
3853
        switch (sel) {
3854
        case 0:
3855
        case 2:
3856
        case 4:
3857
        case 6:
3858
            tcg_gen_helper_0_0(do_mtc0_taghi);
3859
            rn = "TagHi";
3860
            break;
3861
        case 1:
3862
        case 3:
3863
        case 5:
3864
        case 7:
3865
            tcg_gen_helper_0_0(do_mtc0_datahi);
3866
            rn = "DataHi";
3867
            break;
3868
        default:
3869
            rn = "invalid sel";
3870
            goto die;
3871
        }
3872
       break;
3873
    case 30:
3874
        switch (sel) {
3875
        case 0:
3876
            gen_mtc0_store64(cpu_T[0], offsetof(CPUState, CP0_ErrorEPC));
3877
            rn = "ErrorEPC";
3878
            break;
3879
        default:
3880
            goto die;
3881
        }
3882
        break;
3883
    case 31:
3884
        switch (sel) {
3885
        case 0:
3886
            /* EJTAG support */
3887
            gen_mtc0_store32(cpu_T[0], offsetof(CPUState, CP0_DESAVE));
3888
            rn = "DESAVE";
3889
            break;
3890
        default:
3891
            goto die;
3892
        }
3893
        /* Stop translation as we may have switched the execution mode */
3894
        ctx->bstate = BS_STOP;
3895
        break;
3896
    default:
3897
       goto die;
3898
    }
3899
#if defined MIPS_DEBUG_DISAS
3900
    if (loglevel & CPU_LOG_TB_IN_ASM) {
3901
        fprintf(logfile, "mtc0 %s (reg %d sel %d)\n",
3902
                rn, reg, sel);
3903
    }
3904
#endif
3905
    return;
3906

    
3907
die:
3908
#if defined MIPS_DEBUG_DISAS
3909
    if (loglevel & CPU_LOG_TB_IN_ASM) {
3910
        fprintf(logfile, "mtc0 %s (reg %d sel %d)\n",
3911
                rn, reg, sel);
3912
    }
3913
#endif
3914
    generate_exception(ctx, EXCP_RI);
3915
}
3916

    
3917
#if defined(TARGET_MIPS64)
3918
static void gen_dmfc0 (CPUState *env, DisasContext *ctx, int reg, int sel)
3919
{
3920
    const char *rn = "invalid";
3921

    
3922
    if (sel != 0)
3923
        check_insn(env, ctx, ISA_MIPS64);
3924

    
3925
    switch (reg) {
3926
    case 0:
3927
        switch (sel) {
3928
        case 0:
3929
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Index));
3930
            rn = "Index";
3931
            break;
3932
        case 1:
3933
            check_insn(env, ctx, ASE_MT);
3934
            tcg_gen_helper_0_0(do_mfc0_mvpcontrol);
3935
            rn = "MVPControl";
3936
            break;
3937
        case 2:
3938
            check_insn(env, ctx, ASE_MT);
3939
            tcg_gen_helper_0_0(do_mfc0_mvpconf0);
3940
            rn = "MVPConf0";
3941
            break;
3942
        case 3:
3943
            check_insn(env, ctx, ASE_MT);
3944
            tcg_gen_helper_0_0(do_mfc0_mvpconf1);
3945
            rn = "MVPConf1";
3946
            break;
3947
        default:
3948
            goto die;
3949
        }
3950
        break;
3951
    case 1:
3952
        switch (sel) {
3953
        case 0:
3954
            tcg_gen_helper_0_0(do_mfc0_random);
3955
            rn = "Random";
3956
            break;
3957
        case 1:
3958
            check_insn(env, ctx, ASE_MT);
3959
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_VPEControl));
3960
            rn = "VPEControl";
3961
            break;
3962
        case 2:
3963
            check_insn(env, ctx, ASE_MT);
3964
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_VPEConf0));
3965
            rn = "VPEConf0";
3966
            break;
3967
        case 3:
3968
            check_insn(env, ctx, ASE_MT);
3969
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_VPEConf1));
3970
            rn = "VPEConf1";
3971
            break;
3972
        case 4:
3973
            check_insn(env, ctx, ASE_MT);
3974
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_YQMask));
3975
            rn = "YQMask";
3976
            break;
3977
        case 5:
3978
            check_insn(env, ctx, ASE_MT);
3979
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_VPESchedule));
3980
            rn = "VPESchedule";
3981
            break;
3982
        case 6:
3983
            check_insn(env, ctx, ASE_MT);
3984
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_VPEScheFBack));
3985
            rn = "VPEScheFBack";
3986
            break;
3987
        case 7:
3988
            check_insn(env, ctx, ASE_MT);
3989
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_VPEOpt));
3990
            rn = "VPEOpt";
3991
            break;
3992
        default:
3993
            goto die;
3994
        }
3995
        break;
3996
    case 2:
3997
        switch (sel) {
3998
        case 0:
3999
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_EntryLo0));
4000
            rn = "EntryLo0";
4001
            break;
4002
        case 1:
4003
            check_insn(env, ctx, ASE_MT);
4004
            tcg_gen_helper_0_0(do_mfc0_tcstatus);
4005
            rn = "TCStatus";
4006
            break;
4007
        case 2:
4008
            check_insn(env, ctx, ASE_MT);
4009
            tcg_gen_helper_0_0(do_mfc0_tcbind);
4010
            rn = "TCBind";
4011
            break;
4012
        case 3:
4013
            check_insn(env, ctx, ASE_MT);
4014
            tcg_gen_helper_0_0(do_dmfc0_tcrestart);
4015
            rn = "TCRestart";
4016
            break;
4017
        case 4:
4018
            check_insn(env, ctx, ASE_MT);
4019
            tcg_gen_helper_0_0(do_dmfc0_tchalt);
4020
            rn = "TCHalt";
4021
            break;
4022
        case 5:
4023
            check_insn(env, ctx, ASE_MT);
4024
            tcg_gen_helper_0_0(do_dmfc0_tccontext);
4025
            rn = "TCContext";
4026
            break;
4027
        case 6:
4028
            check_insn(env, ctx, ASE_MT);
4029
            tcg_gen_helper_0_0(do_dmfc0_tcschedule);
4030
            rn = "TCSchedule";
4031
            break;
4032
        case 7:
4033
            check_insn(env, ctx, ASE_MT);
4034
            tcg_gen_helper_0_0(do_dmfc0_tcschefback);
4035
            rn = "TCScheFBack";
4036
            break;
4037
        default:
4038
            goto die;
4039
        }
4040
        break;
4041
    case 3:
4042
        switch (sel) {
4043
        case 0:
4044
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_EntryLo1));
4045
            rn = "EntryLo1";
4046
            break;
4047
        default:
4048
            goto die;
4049
        }
4050
        break;
4051
    case 4:
4052
        switch (sel) {
4053
        case 0:
4054
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_Context));
4055
            rn = "Context";
4056
            break;
4057
        case 1:
4058
//            tcg_gen_helper_0_0(do_dmfc0_contextconfig); /* SmartMIPS ASE */
4059
            rn = "ContextConfig";
4060
//            break;
4061
        default:
4062
            goto die;
4063
        }
4064
        break;
4065
    case 5:
4066
        switch (sel) {
4067
        case 0:
4068
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_PageMask));
4069
            rn = "PageMask";
4070
            break;
4071
        case 1:
4072
            check_insn(env, ctx, ISA_MIPS32R2);
4073
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_PageGrain));
4074
            rn = "PageGrain";
4075
            break;
4076
        default:
4077
            goto die;
4078
        }
4079
        break;
4080
    case 6:
4081
        switch (sel) {
4082
        case 0:
4083
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Wired));
4084
            rn = "Wired";
4085
            break;
4086
        case 1:
4087
            check_insn(env, ctx, ISA_MIPS32R2);
4088
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSConf0));
4089
            rn = "SRSConf0";
4090
            break;
4091
        case 2:
4092
            check_insn(env, ctx, ISA_MIPS32R2);
4093
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSConf1));
4094
            rn = "SRSConf1";
4095
            break;
4096
        case 3:
4097
            check_insn(env, ctx, ISA_MIPS32R2);
4098
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSConf2));
4099
            rn = "SRSConf2";
4100
            break;
4101
        case 4:
4102
            check_insn(env, ctx, ISA_MIPS32R2);
4103
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSConf3));
4104
            rn = "SRSConf3";
4105
            break;
4106
        case 5:
4107
            check_insn(env, ctx, ISA_MIPS32R2);
4108
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSConf4));
4109
            rn = "SRSConf4";
4110
            break;
4111
        default:
4112
            goto die;
4113
        }
4114
        break;
4115
    case 7:
4116
        switch (sel) {
4117
        case 0:
4118
            check_insn(env, ctx, ISA_MIPS32R2);
4119
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_HWREna));
4120
            rn = "HWREna";
4121
            break;
4122
        default:
4123
            goto die;
4124
        }
4125
        break;
4126
    case 8:
4127
        switch (sel) {
4128
        case 0:
4129
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_BadVAddr));
4130
            rn = "BadVAddr";
4131
            break;
4132
        default:
4133
            goto die;
4134
        }
4135
        break;
4136
    case 9:
4137
        switch (sel) {
4138
        case 0:
4139
            tcg_gen_helper_0_0(do_mfc0_count);
4140
            rn = "Count";
4141
            break;
4142
        /* 6,7 are implementation dependent */
4143
        default:
4144
            goto die;
4145
        }
4146
        break;
4147
    case 10:
4148
        switch (sel) {
4149
        case 0:
4150
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_EntryHi));
4151
            rn = "EntryHi";
4152
            break;
4153
        default:
4154
            goto die;
4155
        }
4156
        break;
4157
    case 11:
4158
        switch (sel) {
4159
        case 0:
4160
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Compare));
4161
            rn = "Compare";
4162
            break;
4163
        /* 6,7 are implementation dependent */
4164
        default:
4165
            goto die;
4166
        }
4167
        break;
4168
    case 12:
4169
        switch (sel) {
4170
        case 0:
4171
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Status));
4172
            rn = "Status";
4173
            break;
4174
        case 1:
4175
            check_insn(env, ctx, ISA_MIPS32R2);
4176
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_IntCtl));
4177
            rn = "IntCtl";
4178
            break;
4179
        case 2:
4180
            check_insn(env, ctx, ISA_MIPS32R2);
4181
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSCtl));
4182
            rn = "SRSCtl";
4183
            break;
4184
        case 3:
4185
            check_insn(env, ctx, ISA_MIPS32R2);
4186
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_SRSMap));
4187
            rn = "SRSMap";
4188
            break;
4189
        default:
4190
            goto die;
4191
        }
4192
        break;
4193
    case 13:
4194
        switch (sel) {
4195
        case 0:
4196
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Cause));
4197
            rn = "Cause";
4198
            break;
4199
        default:
4200
            goto die;
4201
        }
4202
        break;
4203
    case 14:
4204
        switch (sel) {
4205
        case 0:
4206
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_EPC));
4207
            rn = "EPC";
4208
            break;
4209
        default:
4210
            goto die;
4211
        }
4212
        break;
4213
    case 15:
4214
        switch (sel) {
4215
        case 0:
4216
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_PRid));
4217
            rn = "PRid";
4218
            break;
4219
        case 1:
4220
            check_insn(env, ctx, ISA_MIPS32R2);
4221
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_EBase));
4222
            rn = "EBase";
4223
            break;
4224
        default:
4225
            goto die;
4226
        }
4227
        break;
4228
    case 16:
4229
        switch (sel) {
4230
        case 0:
4231
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config0));
4232
            rn = "Config";
4233
            break;
4234
        case 1:
4235
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config1));
4236
            rn = "Config1";
4237
            break;
4238
        case 2:
4239
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config2));
4240
            rn = "Config2";
4241
            break;
4242
        case 3:
4243
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config3));
4244
            rn = "Config3";
4245
            break;
4246
       /* 6,7 are implementation dependent */
4247
        case 6:
4248
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config6));
4249
            rn = "Config6";
4250
            break;
4251
        case 7:
4252
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Config7));
4253
            rn = "Config7";
4254
            break;
4255
        default:
4256
            goto die;
4257
        }
4258
        break;
4259
    case 17:
4260
        switch (sel) {
4261
        case 0:
4262
            tcg_gen_helper_0_0(do_dmfc0_lladdr);
4263
            rn = "LLAddr";
4264
            break;
4265
        default:
4266
            goto die;
4267
        }
4268
        break;
4269
    case 18:
4270
        switch (sel) {
4271
        case 0 ... 7:
4272
            tcg_gen_helper_0_1i(do_dmfc0_watchlo, sel);
4273
            rn = "WatchLo";
4274
            break;
4275
        default:
4276
            goto die;
4277
        }
4278
        break;
4279
    case 19:
4280
        switch (sel) {
4281
        case 0 ... 7:
4282
            tcg_gen_helper_0_1i(do_mfc0_watchhi, sel);
4283
            rn = "WatchHi";
4284
            break;
4285
        default:
4286
            goto die;
4287
        }
4288
        break;
4289
    case 20:
4290
        switch (sel) {
4291
        case 0:
4292
            check_insn(env, ctx, ISA_MIPS3);
4293
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_XContext));
4294
            rn = "XContext";
4295
            break;
4296
        default:
4297
            goto die;
4298
        }
4299
        break;
4300
    case 21:
4301
       /* Officially reserved, but sel 0 is used for R1x000 framemask */
4302
        switch (sel) {
4303
        case 0:
4304
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Framemask));
4305
            rn = "Framemask";
4306
            break;
4307
        default:
4308
            goto die;
4309
        }
4310
        break;
4311
    case 22:
4312
        /* ignored */
4313
        rn = "'Diagnostic"; /* implementation dependent */
4314
        break;
4315
    case 23:
4316
        switch (sel) {
4317
        case 0:
4318
            tcg_gen_helper_0_0(do_mfc0_debug); /* EJTAG support */
4319
            rn = "Debug";
4320
            break;
4321
        case 1:
4322
//            tcg_gen_helper_0_0(do_dmfc0_tracecontrol); /* PDtrace support */
4323
            rn = "TraceControl";
4324
//            break;
4325
        case 2:
4326
//            tcg_gen_helper_0_0(do_dmfc0_tracecontrol2); /* PDtrace support */
4327
            rn = "TraceControl2";
4328
//            break;
4329
        case 3:
4330
//            tcg_gen_helper_0_0(do_dmfc0_usertracedata); /* PDtrace support */
4331
            rn = "UserTraceData";
4332
//            break;
4333
        case 4:
4334
//            tcg_gen_helper_0_0(do_dmfc0_debug); /* PDtrace support */
4335
            rn = "TraceBPC";
4336
//            break;
4337
        default:
4338
            goto die;
4339
        }
4340
        break;
4341
    case 24:
4342
        switch (sel) {
4343
        case 0:
4344
            /* EJTAG support */
4345
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_DEPC));
4346
            rn = "DEPC";
4347
            break;
4348
        default:
4349
            goto die;
4350
        }
4351
        break;
4352
    case 25:
4353
        switch (sel) {
4354
        case 0:
4355
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_Performance0));
4356
            rn = "Performance0";
4357
            break;
4358
        case 1:
4359
//            tcg_gen_helper_0_0(do_dmfc0_performance1);
4360
            rn = "Performance1";
4361
//            break;
4362
        case 2:
4363
//            tcg_gen_helper_0_0(do_dmfc0_performance2);
4364
            rn = "Performance2";
4365
//            break;
4366
        case 3:
4367
//            tcg_gen_helper_0_0(do_dmfc0_performance3);
4368
            rn = "Performance3";
4369
//            break;
4370
        case 4:
4371
//            tcg_gen_helper_0_0(do_dmfc0_performance4);
4372
            rn = "Performance4";
4373
//            break;
4374
        case 5:
4375
//            tcg_gen_helper_0_0(do_dmfc0_performance5);
4376
            rn = "Performance5";
4377
//            break;
4378
        case 6:
4379
//            tcg_gen_helper_0_0(do_dmfc0_performance6);
4380
            rn = "Performance6";
4381
//            break;
4382
        case 7:
4383
//            tcg_gen_helper_0_0(do_dmfc0_performance7);
4384
            rn = "Performance7";
4385
//            break;
4386
        default:
4387
            goto die;
4388
        }
4389
        break;
4390
    case 26:
4391
       rn = "ECC";
4392
       break;
4393
    case 27:
4394
        switch (sel) {
4395
        /* ignored */
4396
        case 0 ... 3:
4397
            rn = "CacheErr";
4398
            break;
4399
        default:
4400
            goto die;
4401
        }
4402
        break;
4403
    case 28:
4404
        switch (sel) {
4405
        case 0:
4406
        case 2:
4407
        case 4:
4408
        case 6:
4409
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_TagLo));
4410
            rn = "TagLo";
4411
            break;
4412
        case 1:
4413
        case 3:
4414
        case 5:
4415
        case 7:
4416
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_DataLo));
4417
            rn = "DataLo";
4418
            break;
4419
        default:
4420
            goto die;
4421
        }
4422
        break;
4423
    case 29:
4424
        switch (sel) {
4425
        case 0:
4426
        case 2:
4427
        case 4:
4428
        case 6:
4429
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_TagHi));
4430
            rn = "TagHi";
4431
            break;
4432
        case 1:
4433
        case 3:
4434
        case 5:
4435
        case 7:
4436
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_DataHi));
4437
            rn = "DataHi";
4438
            break;
4439
        default:
4440
            goto die;
4441
        }
4442
        break;
4443
    case 30:
4444
        switch (sel) {
4445
        case 0:
4446
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_ErrorEPC));
4447
            rn = "ErrorEPC";
4448
            break;
4449
        default:
4450
            goto die;
4451
        }
4452
        break;
4453
    case 31:
4454
        switch (sel) {
4455
        case 0:
4456
            /* EJTAG support */
4457
            gen_mfc0_load32(cpu_T[0], offsetof(CPUState, CP0_DESAVE));
4458
            rn = "DESAVE";
4459
            break;
4460
        default:
4461
            goto die;
4462
        }
4463
        break;
4464
    default:
4465
        goto die;
4466
    }
4467
#if defined MIPS_DEBUG_DISAS
4468
    if (loglevel & CPU_LOG_TB_IN_ASM) {
4469
        fprintf(logfile, "dmfc0 %s (reg %d sel %d)\n",
4470
                rn, reg, sel);
4471
    }
4472
#endif
4473
    return;
4474

    
4475
die:
4476
#if defined MIPS_DEBUG_DISAS
4477
    if (loglevel & CPU_LOG_TB_IN_ASM) {
4478
        fprintf(logfile, "dmfc0 %s (reg %d sel %d)\n",
4479
                rn, reg, sel);
4480
    }
4481
#endif
4482
    generate_exception(ctx, EXCP_RI);
4483
}
4484

    
4485
static void gen_dmtc0 (CPUState *env, DisasContext *ctx, int reg, int sel)
4486
{
4487
    const char *rn = "invalid";
4488

    
4489
    if (sel != 0)
4490
        check_insn(env, ctx, ISA_MIPS64);
4491

    
4492
    switch (reg) {
4493
    case 0:
4494
        switch (sel) {
4495
        case 0:
4496
            tcg_gen_helper_0_0(do_mtc0_index);
4497
            rn = "Index";
4498
            break;
4499
        case 1:
4500
            check_insn(env, ctx, ASE_MT);
4501
            tcg_gen_helper_0_0(do_mtc0_mvpcontrol);
4502
            rn = "MVPControl";
4503
            break;
4504
        case 2:
4505
            check_insn(env, ctx, ASE_MT);
4506
            /* ignored */
4507
            rn = "MVPConf0";
4508
            break;
4509
        case 3:
4510
            check_insn(env, ctx, ASE_MT);
4511
            /* ignored */
4512
            rn = "MVPConf1";
4513
            break;
4514
        default:
4515
            goto die;
4516
        }
4517
        break;
4518
    case 1:
4519
        switch (sel) {
4520
        case 0:
4521
            /* ignored */
4522
            rn = "Random";
4523
            break;
4524
        case 1:
4525
            check_insn(env, ctx, ASE_MT);
4526
            tcg_gen_helper_0_0(do_mtc0_vpecontrol);
4527
            rn = "VPEControl";
4528
            break;
4529
        case 2:
4530
            check_insn(env, ctx, ASE_MT);
4531
            tcg_gen_helper_0_0(do_mtc0_vpeconf0);
4532
            rn = "VPEConf0";
4533
            break;
4534
        case 3:
4535
            check_insn(env, ctx, ASE_MT);
4536
            tcg_gen_helper_0_0(do_mtc0_vpeconf1);
4537
            rn = "VPEConf1";
4538
            break;
4539
        case 4:
4540
            check_insn(env, ctx, ASE_MT);
4541
            tcg_gen_helper_0_0(do_mtc0_yqmask);
4542
            rn = "YQMask";
4543
            break;
4544
        case 5:
4545
            check_insn(env, ctx, ASE_MT);
4546
            tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_VPESchedule));
4547
            rn = "VPESchedule";
4548
            break;
4549
        case 6:
4550
            check_insn(env, ctx, ASE_MT);
4551
            tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_VPEScheFBack));
4552
            rn = "VPEScheFBack";
4553
            break;
4554
        case 7:
4555
            check_insn(env, ctx, ASE_MT);
4556
            tcg_gen_helper_0_0(do_mtc0_vpeopt);
4557
            rn = "VPEOpt";
4558
            break;
4559
        default:
4560
            goto die;
4561
        }
4562
        break;
4563
    case 2:
4564
        switch (sel) {
4565
        case 0:
4566
            tcg_gen_helper_0_0(do_mtc0_entrylo0);
4567
            rn = "EntryLo0";
4568
            break;
4569
        case 1:
4570
            check_insn(env, ctx, ASE_MT);
4571
            tcg_gen_helper_0_0(do_mtc0_tcstatus);
4572
            rn = "TCStatus";
4573
            break;
4574
        case 2:
4575
            check_insn(env, ctx, ASE_MT);
4576
            tcg_gen_helper_0_0(do_mtc0_tcbind);
4577
            rn = "TCBind";
4578
            break;
4579
        case 3:
4580
            check_insn(env, ctx, ASE_MT);
4581
            tcg_gen_helper_0_0(do_mtc0_tcrestart);
4582
            rn = "TCRestart";
4583
            break;
4584
        case 4:
4585
            check_insn(env, ctx, ASE_MT);
4586
            tcg_gen_helper_0_0(do_mtc0_tchalt);
4587
            rn = "TCHalt";
4588
            break;
4589
        case 5:
4590
            check_insn(env, ctx, ASE_MT);
4591
            tcg_gen_helper_0_0(do_mtc0_tccontext);
4592
            rn = "TCContext";
4593
            break;
4594
        case 6:
4595
            check_insn(env, ctx, ASE_MT);
4596
            tcg_gen_helper_0_0(do_mtc0_tcschedule);
4597
            rn = "TCSchedule";
4598
            break;
4599
        case 7:
4600
            check_insn(env, ctx, ASE_MT);
4601
            tcg_gen_helper_0_0(do_mtc0_tcschefback);
4602
            rn = "TCScheFBack";
4603
            break;
4604
        default:
4605
            goto die;
4606
        }
4607
        break;
4608
    case 3:
4609
        switch (sel) {
4610
        case 0:
4611
            tcg_gen_helper_0_0(do_mtc0_entrylo1);
4612
            rn = "EntryLo1";
4613
            break;
4614
        default:
4615
            goto die;
4616
        }
4617
        break;
4618
    case 4:
4619
        switch (sel) {
4620
        case 0:
4621
            tcg_gen_helper_0_0(do_mtc0_context);
4622
            rn = "Context";
4623
            break;
4624
        case 1:
4625
//           tcg_gen_helper_0_0(do_mtc0_contextconfig); /* SmartMIPS ASE */
4626
            rn = "ContextConfig";
4627
//           break;
4628
        default:
4629
            goto die;
4630
        }
4631
        break;
4632
    case 5:
4633
        switch (sel) {
4634
        case 0:
4635
            tcg_gen_helper_0_0(do_mtc0_pagemask);
4636
            rn = "PageMask";
4637
            break;
4638
        case 1:
4639
            check_insn(env, ctx, ISA_MIPS32R2);
4640
            tcg_gen_helper_0_0(do_mtc0_pagegrain);
4641
            rn = "PageGrain";
4642
            break;
4643
        default:
4644
            goto die;
4645
        }
4646
        break;
4647
    case 6:
4648
        switch (sel) {
4649
        case 0:
4650
            tcg_gen_helper_0_0(do_mtc0_wired);
4651
            rn = "Wired";
4652
            break;
4653
        case 1:
4654
            check_insn(env, ctx, ISA_MIPS32R2);
4655
            tcg_gen_helper_0_0(do_mtc0_srsconf0);
4656
            rn = "SRSConf0";
4657
            break;
4658
        case 2:
4659
            check_insn(env, ctx, ISA_MIPS32R2);
4660
            tcg_gen_helper_0_0(do_mtc0_srsconf1);
4661
            rn = "SRSConf1";
4662
            break;
4663
        case 3:
4664
            check_insn(env, ctx, ISA_MIPS32R2);
4665
            tcg_gen_helper_0_0(do_mtc0_srsconf2);
4666
            rn = "SRSConf2";
4667
            break;
4668
        case 4:
4669
            check_insn(env, ctx, ISA_MIPS32R2);
4670
            tcg_gen_helper_0_0(do_mtc0_srsconf3);
4671
            rn = "SRSConf3";
4672
            break;
4673
        case 5:
4674
            check_insn(env, ctx, ISA_MIPS32R2);
4675
            tcg_gen_helper_0_0(do_mtc0_srsconf4);
4676
            rn = "SRSConf4";
4677
            break;
4678
        default:
4679
            goto die;
4680
        }
4681
        break;
4682
    case 7:
4683
        switch (sel) {
4684
        case 0:
4685
            check_insn(env, ctx, ISA_MIPS32R2);
4686
            tcg_gen_helper_0_0(do_mtc0_hwrena);
4687
            rn = "HWREna";
4688
            break;
4689
        default:
4690
            goto die;
4691
        }
4692
        break;
4693
    case 8:
4694
        /* ignored */
4695
        rn = "BadVAddr";
4696
        break;
4697
    case 9:
4698
        switch (sel) {
4699
        case 0:
4700
            tcg_gen_helper_0_0(do_mtc0_count);
4701
            rn = "Count";
4702
            break;
4703
        /* 6,7 are implementation dependent */
4704
        default:
4705
            goto die;
4706
        }
4707
        /* Stop translation as we may have switched the execution mode */
4708
        ctx->bstate = BS_STOP;
4709
        break;
4710
    case 10:
4711
        switch (sel) {
4712
        case 0:
4713
            tcg_gen_helper_0_0(do_mtc0_entryhi);
4714
            rn = "EntryHi";
4715
            break;
4716
        default:
4717
            goto die;
4718
        }
4719
        break;
4720
    case 11:
4721
        switch (sel) {
4722
        case 0:
4723
            tcg_gen_helper_0_0(do_mtc0_compare);
4724
            rn = "Compare";
4725
            break;
4726
        /* 6,7 are implementation dependent */
4727
        default:
4728
            goto die;
4729
        }
4730
        /* Stop translation as we may have switched the execution mode */
4731
        ctx->bstate = BS_STOP;
4732
        break;
4733
    case 12:
4734
        switch (sel) {
4735
        case 0:
4736
            tcg_gen_helper_0_0(do_mtc0_status);
4737
            /* BS_STOP isn't good enough here, hflags may have changed. */
4738
            gen_save_pc(ctx->pc + 4);
4739
            ctx->bstate = BS_EXCP;
4740
            rn = "Status";
4741
            break;
4742
        case 1:
4743
            check_insn(env, ctx, ISA_MIPS32R2);
4744
            tcg_gen_helper_0_0(do_mtc0_intctl);
4745
            /* Stop translation as we may have switched the execution mode */
4746
            ctx->bstate = BS_STOP;
4747
            rn = "IntCtl";
4748
            break;
4749
        case 2:
4750
            check_insn(env, ctx, ISA_MIPS32R2);
4751
            tcg_gen_helper_0_0(do_mtc0_srsctl);
4752
            /* Stop translation as we may have switched the execution mode */
4753
            ctx->bstate = BS_STOP;
4754
            rn = "SRSCtl";
4755
            break;
4756
        case 3:
4757
            check_insn(env, ctx, ISA_MIPS32R2);
4758
            gen_mtc0_store32(cpu_T[0], offsetof(CPUState, CP0_SRSMap));
4759
            /* Stop translation as we may have switched the execution mode */
4760
            ctx->bstate = BS_STOP;
4761
            rn = "SRSMap";
4762
            break;
4763
        default:
4764
            goto die;
4765
        }
4766
        break;
4767
    case 13:
4768
        switch (sel) {
4769
        case 0:
4770
            tcg_gen_helper_0_0(do_mtc0_cause);
4771
            rn = "Cause";
4772
            break;
4773
        default:
4774
            goto die;
4775
        }
4776
        /* Stop translation as we may have switched the execution mode */
4777
        ctx->bstate = BS_STOP;
4778
        break;
4779
    case 14:
4780
        switch (sel) {
4781
        case 0:
4782
            tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_EPC));
4783
            rn = "EPC";
4784
            break;
4785
        default:
4786
            goto die;
4787
        }
4788
        break;
4789
    case 15:
4790
        switch (sel) {
4791
        case 0:
4792
            /* ignored */
4793
            rn = "PRid";
4794
            break;
4795
        case 1:
4796
            check_insn(env, ctx, ISA_MIPS32R2);
4797
            tcg_gen_helper_0_0(do_mtc0_ebase);
4798
            rn = "EBase";
4799
            break;
4800
        default:
4801
            goto die;
4802
        }
4803
        break;
4804
    case 16:
4805
        switch (sel) {
4806
        case 0:
4807
            tcg_gen_helper_0_0(do_mtc0_config0);
4808
            rn = "Config";
4809
            /* Stop translation as we may have switched the execution mode */
4810
            ctx->bstate = BS_STOP;
4811
            break;
4812
        case 1:
4813
            /* ignored */
4814
            rn = "Config1";
4815
            break;
4816
        case 2:
4817
            tcg_gen_helper_0_0(do_mtc0_config2);
4818
            rn = "Config2";
4819
            /* Stop translation as we may have switched the execution mode */
4820
            ctx->bstate = BS_STOP;
4821
            break;
4822
        case 3:
4823
            /* ignored */
4824
            rn = "Config3";
4825
            break;
4826
        /* 6,7 are implementation dependent */
4827
        default:
4828
            rn = "Invalid config selector";
4829
            goto die;
4830
        }
4831
        break;
4832
    case 17:
4833
        switch (sel) {
4834
        case 0:
4835
            /* ignored */
4836
            rn = "LLAddr";
4837
            break;
4838
        default:
4839
            goto die;
4840
        }
4841
        break;
4842
    case 18:
4843
        switch (sel) {
4844
        case 0 ... 7:
4845
            tcg_gen_helper_0_1i(do_mtc0_watchlo, sel);
4846
            rn = "WatchLo";
4847
            break;
4848
        default:
4849
            goto die;
4850
        }
4851
        break;
4852
    case 19:
4853
        switch (sel) {
4854
        case 0 ... 7:
4855
            tcg_gen_helper_0_1i(do_mtc0_watchhi, sel);
4856
            rn = "WatchHi";
4857
            break;
4858
        default:
4859
            goto die;
4860
        }
4861
        break;
4862
    case 20:
4863
        switch (sel) {
4864
        case 0:
4865
            check_insn(env, ctx, ISA_MIPS3);
4866
            tcg_gen_helper_0_0(do_mtc0_xcontext);
4867
            rn = "XContext";
4868
            break;
4869
        default:
4870
            goto die;
4871
        }
4872
        break;
4873
    case 21:
4874
       /* Officially reserved, but sel 0 is used for R1x000 framemask */
4875
        switch (sel) {
4876
        case 0:
4877
            tcg_gen_helper_0_0(do_mtc0_framemask);
4878
            rn = "Framemask";
4879
            break;
4880
        default:
4881
            goto die;
4882
        }
4883
        break;
4884
    case 22:
4885
        /* ignored */
4886
        rn = "Diagnostic"; /* implementation dependent */
4887
        break;
4888
    case 23:
4889
        switch (sel) {
4890
        case 0:
4891
            tcg_gen_helper_0_0(do_mtc0_debug); /* EJTAG support */
4892
            /* BS_STOP isn't good enough here, hflags may have changed. */
4893
            gen_save_pc(ctx->pc + 4);
4894
            ctx->bstate = BS_EXCP;
4895
            rn = "Debug";
4896
            break;
4897
        case 1:
4898
//            tcg_gen_helper_0_0(do_mtc0_tracecontrol); /* PDtrace support */
4899
            /* Stop translation as we may have switched the execution mode */
4900
            ctx->bstate = BS_STOP;
4901
            rn = "TraceControl";
4902
//            break;
4903
        case 2:
4904
//            tcg_gen_helper_0_0(do_mtc0_tracecontrol2); /* PDtrace support */
4905
            /* Stop translation as we may have switched the execution mode */
4906
            ctx->bstate = BS_STOP;
4907
            rn = "TraceControl2";
4908
//            break;
4909
        case 3:
4910
//            tcg_gen_helper_0_0(do_mtc0_usertracedata); /* PDtrace support */
4911
            /* Stop translation as we may have switched the execution mode */
4912
            ctx->bstate = BS_STOP;
4913
            rn = "UserTraceData";
4914
//            break;
4915
        case 4:
4916
//            tcg_gen_helper_0_0(do_mtc0_debug); /* PDtrace support */
4917
            /* Stop translation as we may have switched the execution mode */
4918
            ctx->bstate = BS_STOP;
4919
            rn = "TraceBPC";
4920
//            break;
4921
        default:
4922
            goto die;
4923
        }
4924
        break;
4925
    case 24:
4926
        switch (sel) {
4927
        case 0:
4928
            /* EJTAG support */
4929
            tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_DEPC));
4930
            rn = "DEPC";
4931
            break;
4932
        default:
4933
            goto die;
4934
        }
4935
        break;
4936
    case 25:
4937
        switch (sel) {
4938
        case 0:
4939
            tcg_gen_helper_0_0(do_mtc0_performance0);
4940
            rn = "Performance0";
4941
            break;
4942
        case 1:
4943
//            tcg_gen_helper_0_0(do_mtc0_performance1);
4944
            rn = "Performance1";
4945
//            break;
4946
        case 2:
4947
//            tcg_gen_helper_0_0(do_mtc0_performance2);
4948
            rn = "Performance2";
4949
//            break;
4950
        case 3:
4951
//            tcg_gen_helper_0_0(do_mtc0_performance3);
4952
            rn = "Performance3";
4953
//            break;
4954
        case 4:
4955
//            tcg_gen_helper_0_0(do_mtc0_performance4);
4956
            rn = "Performance4";
4957
//            break;
4958
        case 5:
4959
//            tcg_gen_helper_0_0(do_mtc0_performance5);
4960
            rn = "Performance5";
4961
//            break;
4962
        case 6:
4963
//            tcg_gen_helper_0_0(do_mtc0_performance6);
4964
            rn = "Performance6";
4965
//            break;
4966
        case 7:
4967
//            tcg_gen_helper_0_0(do_mtc0_performance7);
4968
            rn = "Performance7";
4969
//            break;
4970
        default:
4971
            goto die;
4972
        }
4973
        break;
4974
    case 26:
4975
        /* ignored */
4976
        rn = "ECC";
4977
        break;
4978
    case 27:
4979
        switch (sel) {
4980
        case 0 ... 3:
4981
            /* ignored */
4982
            rn = "CacheErr";
4983
            break;
4984
        default:
4985
            goto die;
4986
        }
4987
        break;
4988
    case 28:
4989
        switch (sel) {
4990
        case 0:
4991
        case 2:
4992
        case 4:
4993
        case 6:
4994
            tcg_gen_helper_0_0(do_mtc0_taglo);
4995
            rn = "TagLo";
4996
            break;
4997
        case 1:
4998
        case 3:
4999
        case 5:
5000
        case 7:
5001
            tcg_gen_helper_0_0(do_mtc0_datalo);
5002
            rn = "DataLo";
5003
            break;
5004
        default:
5005
            goto die;
5006
        }
5007
        break;
5008
    case 29:
5009
        switch (sel) {
5010
        case 0:
5011
        case 2:
5012
        case 4:
5013
        case 6:
5014
            tcg_gen_helper_0_0(do_mtc0_taghi);
5015
            rn = "TagHi";
5016
            break;
5017
        case 1:
5018
        case 3:
5019
        case 5:
5020
        case 7:
5021
            tcg_gen_helper_0_0(do_mtc0_datahi);
5022
            rn = "DataHi";
5023
            break;
5024
        default:
5025
            rn = "invalid sel";
5026
            goto die;
5027
        }
5028
        break;
5029
    case 30:
5030
        switch (sel) {
5031
        case 0:
5032
            tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUState, CP0_ErrorEPC));
5033
            rn = "ErrorEPC";
5034
            break;
5035
        default:
5036
            goto die;
5037
        }
5038
        break;
5039
    case 31:
5040
        switch (sel) {
5041
        case 0:
5042
            /* EJTAG support */
5043
            gen_mtc0_store32(cpu_T[0], offsetof(CPUState, CP0_DESAVE));
5044
            rn = "DESAVE";
5045
            break;
5046
        default:
5047
            goto die;
5048
        }
5049
        /* Stop translation as we may have switched the execution mode */
5050
        ctx->bstate = BS_STOP;
5051
        break;
5052
    default:
5053
        goto die;
5054
    }
5055
#if defined MIPS_DEBUG_DISAS
5056
    if (loglevel & CPU_LOG_TB_IN_ASM) {
5057
        fprintf(logfile, "dmtc0 %s (reg %d sel %d)\n",
5058
                rn, reg, sel);
5059
    }
5060
#endif
5061
    return;
5062

    
5063
die:
5064
#if defined MIPS_DEBUG_DISAS
5065
    if (loglevel & CPU_LOG_TB_IN_ASM) {
5066
        fprintf(logfile, "dmtc0 %s (reg %d sel %d)\n",
5067
                rn, reg, sel);
5068
    }
5069
#endif
5070
    generate_exception(ctx, EXCP_RI);
5071
}
5072
#endif /* TARGET_MIPS64 */
5073

    
5074
static void gen_mftr(CPUState *env, DisasContext *ctx, int rt,
5075
                     int u, int sel, int h)
5076
{
5077
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
5078

    
5079
    if ((env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP)) == 0 &&
5080
        ((env->CP0_TCBind[other_tc] & (0xf << CP0TCBd_CurVPE)) !=
5081
         (env->CP0_TCBind[env->current_tc] & (0xf << CP0TCBd_CurVPE))))
5082
        tcg_gen_movi_tl(cpu_T[0], -1);
5083
    else if ((env->CP0_VPEControl & (0xff << CP0VPECo_TargTC)) >
5084
             (env->mvp->CP0_MVPConf0 & (0xff << CP0MVPC0_PTC)))
5085
        tcg_gen_movi_tl(cpu_T[0], -1);
5086
    else if (u == 0) {
5087
        switch (rt) {
5088
        case 2:
5089
            switch (sel) {
5090
            case 1:
5091
                tcg_gen_helper_0_0(do_mftc0_tcstatus);
5092
                break;
5093
            case 2:
5094
                tcg_gen_helper_0_0(do_mftc0_tcbind);
5095
                break;
5096
            case 3:
5097
                tcg_gen_helper_0_0(do_mftc0_tcrestart);
5098
                break;
5099
            case 4:
5100
                tcg_gen_helper_0_0(do_mftc0_tchalt);
5101
                break;
5102
            case 5:
5103
                tcg_gen_helper_0_0(do_mftc0_tccontext);
5104
                break;
5105
            case 6:
5106
                tcg_gen_helper_0_0(do_mftc0_tcschedule);
5107
                break;
5108
            case 7:
5109
                tcg_gen_helper_0_0(do_mftc0_tcschefback);
5110
                break;
5111
            default:
5112
                gen_mfc0(env, ctx, rt, sel);
5113
                break;
5114
            }
5115
            break;
5116
        case 10:
5117
            switch (sel) {
5118
            case 0:
5119
                tcg_gen_helper_0_0(do_mftc0_entryhi);
5120
                break;
5121
            default:
5122
                gen_mfc0(env, ctx, rt, sel);
5123
                break;
5124
            }
5125
        case 12:
5126
            switch (sel) {
5127
            case 0:
5128
                tcg_gen_helper_0_0(do_mftc0_status);
5129
                break;
5130
            default:
5131
                gen_mfc0(env, ctx, rt, sel);
5132
                break;
5133
            }
5134
        case 23:
5135
            switch (sel) {
5136
            case 0:
5137
                tcg_gen_helper_0_0(do_mftc0_debug);
5138
                break;
5139
            default:
5140
                gen_mfc0(env, ctx, rt, sel);
5141
                break;
5142
            }
5143
            break;
5144
        default:
5145
            gen_mfc0(env, ctx, rt, sel);
5146
        }
5147
    } else switch (sel) {
5148
    /* GPR registers. */
5149
    case 0:
5150
        tcg_gen_helper_0_1i(do_mftgpr, rt);
5151
        break;
5152
    /* Auxiliary CPU registers */
5153
    case 1:
5154
        switch (rt) {
5155
        case 0:
5156
            tcg_gen_helper_0_1i(do_mftlo, 0);
5157
            break;
5158
        case 1:
5159
            tcg_gen_helper_0_1i(do_mfthi, 0);
5160
            break;
5161
        case 2:
5162
            tcg_gen_helper_0_1i(do_mftacx, 0);
5163
            break;
5164
        case 4:
5165
            tcg_gen_helper_0_1i(do_mftlo, 1);
5166
            break;
5167
        case 5:
5168
            tcg_gen_helper_0_1i(do_mfthi, 1);
5169
            break;
5170
        case 6:
5171
            tcg_gen_helper_0_1i(do_mftacx, 1);
5172
            break;
5173
        case 8:
5174
            tcg_gen_helper_0_1i(do_mftlo, 2);
5175
            break;
5176
        case 9:
5177
            tcg_gen_helper_0_1i(do_mfthi, 2);
5178
            break;
5179
        case 10:
5180
            tcg_gen_helper_0_1i(do_mftacx, 2);
5181
            break;
5182
        case 12:
5183
            tcg_gen_helper_0_1i(do_mftlo, 3);
5184
            break;
5185
        case 13:
5186
            tcg_gen_helper_0_1i(do_mfthi, 3);
5187
            break;
5188
        case 14:
5189
            tcg_gen_helper_0_1i(do_mftacx, 3);
5190
            break;
5191
        case 16:
5192
            tcg_gen_helper_0_0(do_mftdsp);
5193
            break;
5194
        default:
5195
            goto die;
5196
        }
5197
        break;
5198
    /* Floating point (COP1). */
5199
    case 2:
5200
        /* XXX: For now we support only a single FPU context. */
5201
        if (h == 0) {
5202
            gen_load_fpr32(fpu32_T[0], rt);
5203
            tcg_gen_ext_i32_tl(cpu_T[0], fpu32_T[0]);
5204
        } else {
5205
            gen_load_fpr32h(fpu32h_T[0], rt);
5206
            tcg_gen_ext_i32_tl(cpu_T[0], fpu32h_T[0]);
5207
        }
5208
        break;
5209
    case 3:
5210
        /* XXX: For now we support only a single FPU context. */
5211
        tcg_gen_helper_0_1i(do_cfc1, rt);
5212
        break;
5213
    /* COP2: Not implemented. */
5214
    case 4:
5215
    case 5:
5216
        /* fall through */
5217
    default:
5218
        goto die;
5219
    }
5220
#if defined MIPS_DEBUG_DISAS
5221
    if (loglevel & CPU_LOG_TB_IN_ASM) {
5222
        fprintf(logfile, "mftr (reg %d u %d sel %d h %d)\n",
5223
                rt, u, sel, h);
5224
    }
5225
#endif
5226
    return;
5227

    
5228
die:
5229
#if defined MIPS_DEBUG_DISAS
5230
    if (loglevel & CPU_LOG_TB_IN_ASM) {
5231
        fprintf(logfile, "mftr (reg %d u %d sel %d h %d)\n",
5232
                rt, u, sel, h);
5233
    }
5234
#endif
5235
    generate_exception(ctx, EXCP_RI);
5236
}
5237

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

    
5243
    if ((env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP)) == 0 &&
5244
        ((env->CP0_TCBind[other_tc] & (0xf << CP0TCBd_CurVPE)) !=
5245
         (env->CP0_TCBind[env->current_tc] & (0xf << CP0TCBd_CurVPE))))
5246
        /* NOP */ ;
5247
    else if ((env->CP0_VPEControl & (0xff << CP0VPECo_TargTC)) >
5248
             (env->mvp->CP0_MVPConf0 & (0xff << CP0MVPC0_PTC)))
5249
        /* NOP */ ;
5250
    else if (u == 0) {
5251
        switch (rd) {
5252
        case 2:
5253
            switch (sel) {
5254
            case 1:
5255
                tcg_gen_helper_0_0(do_mttc0_tcstatus);
5256
                break;
5257
            case 2:
5258
                tcg_gen_helper_0_0(do_mttc0_tcbind);
5259
                break;
5260
            case 3:
5261
                tcg_gen_helper_0_0(do_mttc0_tcrestart);
5262
                break;
5263
            case 4:
5264
                tcg_gen_helper_0_0(do_mttc0_tchalt);
5265
                break;
5266
            case 5:
5267
                tcg_gen_helper_0_0(do_mttc0_tccontext);
5268
                break;
5269
            case 6:
5270
                tcg_gen_helper_0_0(do_mttc0_tcschedule);
5271
                break;
5272
            case 7:
5273
                tcg_gen_helper_0_0(do_mttc0_tcschefback);
5274
                break;
5275
            default:
5276
                gen_mtc0(env, ctx, rd, sel);
5277
                break;
5278
            }
5279
            break;
5280
        case 10:
5281
            switch (sel) {
5282
            case 0:
5283
                tcg_gen_helper_0_0(do_mttc0_entryhi);
5284
                break;
5285
            default:
5286
                gen_mtc0(env, ctx, rd, sel);
5287
                break;
5288
            }
5289
        case 12:
5290
            switch (sel) {
5291
            case 0:
5292
                tcg_gen_helper_0_0(do_mttc0_status);
5293
                break;
5294
            default:
5295
                gen_mtc0(env, ctx, rd, sel);
5296
                break;
5297
            }
5298
        case 23:
5299
            switch (sel) {
5300
            case 0:
5301
                tcg_gen_helper_0_0(do_mttc0_debug);
5302
                break;
5303
            default:
5304
                gen_mtc0(env, ctx, rd, sel);
5305
                break;
5306
            }
5307
            break;
5308
        default:
5309
            gen_mtc0(env, ctx, rd, sel);
5310
        }
5311
    } else switch (sel) {
5312
    /* GPR registers. */
5313
    case 0:
5314
        tcg_gen_helper_0_1i(do_mttgpr, rd);
5315
        break;
5316
    /* Auxiliary CPU registers */
5317
    case 1:
5318
        switch (rd) {
5319
        case 0:
5320
            tcg_gen_helper_0_1i(do_mttlo, 0);
5321
            break;
5322
        case 1:
5323
            tcg_gen_helper_0_1i(do_mtthi, 0);
5324
            break;
5325
        case 2:
5326
            tcg_gen_helper_0_1i(do_mttacx, 0);
5327
            break;
5328
        case 4:
5329
            tcg_gen_helper_0_1i(do_mttlo, 1);
5330
            break;
5331
        case 5:
5332
            tcg_gen_helper_0_1i(do_mtthi, 1);
5333
            break;
5334
        case 6:
5335
            tcg_gen_helper_0_1i(do_mttacx, 1);
5336
            break;
5337
        case 8:
5338
            tcg_gen_helper_0_1i(do_mttlo, 2);
5339
            break;
5340
        case 9:
5341
            tcg_gen_helper_0_1i(do_mtthi, 2);
5342
            break;
5343
        case 10:
5344
            tcg_gen_helper_0_1i(do_mttacx, 2);
5345
            break;
5346
        case 12:
5347
            tcg_gen_helper_0_1i(do_mttlo, 3);
5348
            break;
5349
        case 13:
5350
            tcg_gen_helper_0_1i(do_mtthi, 3);
5351
            break;
5352
        case 14:
5353
            tcg_gen_helper_0_1i(do_mttacx, 3);
5354
            break;
5355
        case 16:
5356
            tcg_gen_helper_0_0(do_mttdsp);
5357
            break;
5358
        default:
5359
            goto die;
5360
        }
5361
        break;
5362
    /* Floating point (COP1). */
5363
    case 2:
5364
        /* XXX: For now we support only a single FPU context. */
5365
        if (h == 0) {
5366
            tcg_gen_trunc_tl_i32(fpu32_T[0], cpu_T[0]);
5367
            gen_store_fpr32(fpu32_T[0], rd);
5368
        } else {
5369
            tcg_gen_trunc_tl_i32(fpu32h_T[0], cpu_T[0]);
5370
            gen_store_fpr32h(fpu32h_T[0], rd);
5371
        }
5372
        break;
5373
    case 3:
5374
        /* XXX: For now we support only a single FPU context. */
5375
        tcg_gen_helper_0_1i(do_ctc1, rd);
5376
        break;
5377
    /* COP2: Not implemented. */
5378
    case 4:
5379
    case 5:
5380
        /* fall through */
5381
    default:
5382
        goto die;
5383
    }
5384
#if defined MIPS_DEBUG_DISAS
5385
    if (loglevel & CPU_LOG_TB_IN_ASM) {
5386
        fprintf(logfile, "mttr (reg %d u %d sel %d h %d)\n",
5387
                rd, u, sel, h);
5388
    }
5389
#endif
5390
    return;
5391

    
5392
die:
5393
#if defined MIPS_DEBUG_DISAS
5394
    if (loglevel & CPU_LOG_TB_IN_ASM) {
5395
        fprintf(logfile, "mttr (reg %d u %d sel %d h %d)\n",
5396
                rd, u, sel, h);
5397
    }
5398
#endif
5399
    generate_exception(ctx, EXCP_RI);
5400
}
5401

    
5402
static void gen_cp0 (CPUState *env, DisasContext *ctx, uint32_t opc, int rt, int rd)
5403
{
5404
    const char *opn = "ldst";
5405

    
5406
    switch (opc) {
5407
    case OPC_MFC0:
5408
        if (rt == 0) {
5409
            /* Treat as NOP. */
5410
            return;
5411
        }
5412
        gen_mfc0(env, ctx, rd, ctx->opcode & 0x7);
5413
        gen_store_gpr(cpu_T[0], rt);
5414
        opn = "mfc0";
5415
        break;
5416
    case OPC_MTC0:
5417
        gen_load_gpr(cpu_T[0], rt);
5418
        save_cpu_state(ctx, 1);
5419
        gen_mtc0(env, ctx, rd, ctx->opcode & 0x7);
5420
        opn = "mtc0";
5421
        break;
5422
#if defined(TARGET_MIPS64)
5423
    case OPC_DMFC0:
5424
        check_insn(env, ctx, ISA_MIPS3);
5425
        if (rt == 0) {
5426
            /* Treat as NOP. */
5427
            return;
5428
        }
5429
        gen_dmfc0(env, ctx, rd, ctx->opcode & 0x7);
5430
        gen_store_gpr(cpu_T[0], rt);
5431
        opn = "dmfc0";
5432
        break;
5433
    case OPC_DMTC0:
5434
        check_insn(env, ctx, ISA_MIPS3);
5435
        gen_load_gpr(cpu_T[0], rt);
5436
        save_cpu_state(ctx, 1);
5437
        gen_dmtc0(env, ctx, rd, ctx->opcode & 0x7);
5438
        opn = "dmtc0";
5439
        break;
5440
#endif
5441
    case OPC_MFTR:
5442
        check_insn(env, ctx, ASE_MT);
5443
        if (rd == 0) {
5444
            /* Treat as NOP. */
5445
            return;
5446
        }
5447
        gen_mftr(env, ctx, rt, (ctx->opcode >> 5) & 1,
5448
                 ctx->opcode & 0x7, (ctx->opcode >> 4) & 1);
5449
        gen_store_gpr(cpu_T[0], rd);
5450
        opn = "mftr";
5451
        break;
5452
    case OPC_MTTR:
5453
        check_insn(env, ctx, ASE_MT);
5454
        gen_load_gpr(cpu_T[0], rt);
5455
        gen_mttr(env, ctx, rd, (ctx->opcode >> 5) & 1,
5456
                 ctx->opcode & 0x7, (ctx->opcode >> 4) & 1);
5457
        opn = "mttr";
5458
        break;
5459
    case OPC_TLBWI:
5460
        opn = "tlbwi";
5461
        if (!env->tlb->do_tlbwi)
5462
            goto die;
5463
        tcg_gen_helper_0_0(env->tlb->do_tlbwi);
5464
        break;
5465
    case OPC_TLBWR:
5466
        opn = "tlbwr";
5467
        if (!env->tlb->do_tlbwr)
5468
            goto die;
5469
        tcg_gen_helper_0_0(env->tlb->do_tlbwr);
5470
        break;
5471
    case OPC_TLBP:
5472
        opn = "tlbp";
5473
        if (!env->tlb->do_tlbp)
5474
            goto die;
5475
        tcg_gen_helper_0_0(env->tlb->do_tlbp);
5476
        break;
5477
    case OPC_TLBR:
5478
        opn = "tlbr";
5479
        if (!env->tlb->do_tlbr)
5480
            goto die;
5481
        tcg_gen_helper_0_0(env->tlb->do_tlbr);
5482
        break;
5483
    case OPC_ERET:
5484
        opn = "eret";
5485
        check_insn(env, ctx, ISA_MIPS2);
5486
        save_cpu_state(ctx, 1);
5487
        tcg_gen_helper_0_0(do_eret);
5488
        ctx->bstate = BS_EXCP;
5489
        break;
5490
    case OPC_DERET:
5491
        opn = "deret";
5492
        check_insn(env, ctx, ISA_MIPS32);
5493
        if (!(ctx->hflags & MIPS_HFLAG_DM)) {
5494
            MIPS_INVAL(opn);
5495
            generate_exception(ctx, EXCP_RI);
5496
        } else {
5497
            save_cpu_state(ctx, 1);
5498
            tcg_gen_helper_0_0(do_deret);
5499
            ctx->bstate = BS_EXCP;
5500
        }
5501
        break;
5502
    case OPC_WAIT:
5503
        opn = "wait";
5504
        check_insn(env, ctx, ISA_MIPS3 | ISA_MIPS32);
5505
        /* If we get an exception, we want to restart at next instruction */
5506
        ctx->pc += 4;
5507
        save_cpu_state(ctx, 1);
5508
        ctx->pc -= 4;
5509
        tcg_gen_helper_0_0(do_wait);
5510
        ctx->bstate = BS_EXCP;
5511
        break;
5512
    default:
5513
 die:
5514
        MIPS_INVAL(opn);
5515
        generate_exception(ctx, EXCP_RI);
5516
        return;
5517
    }
5518
    MIPS_DEBUG("%s %s %d", opn, regnames[rt], rd);
5519
}
5520
#endif /* !CONFIG_USER_ONLY */
5521

    
5522
/* CP1 Branches (before delay slot) */
5523
static void gen_compute_branch1 (CPUState *env, DisasContext *ctx, uint32_t op,
5524
                                 int32_t cc, int32_t offset)
5525
{
5526
    target_ulong btarget;
5527
    const char *opn = "cp1 cond branch";
5528

    
5529
    if (cc != 0)
5530
        check_insn(env, ctx, ISA_MIPS4 | ISA_MIPS32);
5531

    
5532
    btarget = ctx->pc + 4 + offset;
5533

    
5534
    switch (op) {
5535
    case OPC_BC1F:
5536
        {
5537
            int l1 = gen_new_label();
5538
            int l2 = gen_new_label();
5539
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
5540

    
5541
            get_fp_cond(r_tmp1);
5542
            tcg_gen_ext_i32_tl(cpu_T[0], r_tmp1);
5543
            tcg_temp_free(r_tmp1);
5544
            tcg_gen_not_tl(cpu_T[0], cpu_T[0]);
5545
            tcg_gen_movi_tl(cpu_T[1], 0x1 << cc);
5546
            tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
5547
            tcg_gen_brcondi_tl(TCG_COND_NE, cpu_T[0], 0, l1);
5548
            tcg_gen_movi_tl(cpu_T[0], 0);
5549
            tcg_gen_br(l2);
5550
            gen_set_label(l1);
5551
            tcg_gen_movi_tl(cpu_T[0], 1);
5552
            gen_set_label(l2);
5553
        }
5554
        opn = "bc1f";
5555
        goto not_likely;
5556
    case OPC_BC1FL:
5557
        {
5558
            int l1 = gen_new_label();
5559
            int l2 = gen_new_label();
5560
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
5561

    
5562
            get_fp_cond(r_tmp1);
5563
            tcg_gen_ext_i32_tl(cpu_T[0], r_tmp1);
5564
            tcg_temp_free(r_tmp1);
5565
            tcg_gen_not_tl(cpu_T[0], cpu_T[0]);
5566
            tcg_gen_movi_tl(cpu_T[1], 0x1 << cc);
5567
            tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
5568
            tcg_gen_brcondi_tl(TCG_COND_NE, cpu_T[0], 0, l1);
5569
            tcg_gen_movi_tl(cpu_T[0], 0);
5570
            tcg_gen_br(l2);
5571
            gen_set_label(l1);
5572
            tcg_gen_movi_tl(cpu_T[0], 1);
5573
            gen_set_label(l2);
5574
        }
5575
        opn = "bc1fl";
5576
        goto likely;
5577
    case OPC_BC1T:
5578
        {
5579
            int l1 = gen_new_label();
5580
            int l2 = gen_new_label();
5581
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
5582

    
5583
            get_fp_cond(r_tmp1);
5584
            tcg_gen_ext_i32_tl(cpu_T[0], r_tmp1);
5585
            tcg_temp_free(r_tmp1);
5586
            tcg_gen_movi_tl(cpu_T[1], 0x1 << cc);
5587
            tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
5588
            tcg_gen_brcondi_tl(TCG_COND_NE, cpu_T[0], 0, l1);
5589
            tcg_gen_movi_tl(cpu_T[0], 0);
5590
            tcg_gen_br(l2);
5591
            gen_set_label(l1);
5592
            tcg_gen_movi_tl(cpu_T[0], 1);
5593
            gen_set_label(l2);
5594
        }
5595
        opn = "bc1t";
5596
        goto not_likely;
5597
    case OPC_BC1TL:
5598
        {
5599
            int l1 = gen_new_label();
5600
            int l2 = gen_new_label();
5601
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
5602

    
5603
            get_fp_cond(r_tmp1);
5604
            tcg_gen_ext_i32_tl(cpu_T[0], r_tmp1);
5605
            tcg_temp_free(r_tmp1);
5606
            tcg_gen_movi_tl(cpu_T[1], 0x1 << cc);
5607
            tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
5608
            tcg_gen_brcondi_tl(TCG_COND_NE, cpu_T[0], 0, l1);
5609
            tcg_gen_movi_tl(cpu_T[0], 0);
5610
            tcg_gen_br(l2);
5611
            gen_set_label(l1);
5612
            tcg_gen_movi_tl(cpu_T[0], 1);
5613
            gen_set_label(l2);
5614
        }
5615
        opn = "bc1tl";
5616
    likely:
5617
        ctx->hflags |= MIPS_HFLAG_BL;
5618
        tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUState, bcond));
5619
        break;
5620
    case OPC_BC1FANY2:
5621
        {
5622
            int l1 = gen_new_label();
5623
            int l2 = gen_new_label();
5624
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
5625

    
5626
            get_fp_cond(r_tmp1);
5627
            tcg_gen_ext_i32_tl(cpu_T[0], r_tmp1);
5628
            tcg_temp_free(r_tmp1);
5629
            tcg_gen_not_tl(cpu_T[0], cpu_T[0]);
5630
            tcg_gen_movi_tl(cpu_T[1], 0x3 << cc);
5631
            tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
5632
            tcg_gen_brcondi_tl(TCG_COND_NE, cpu_T[0], 0, l1);
5633
            tcg_gen_movi_tl(cpu_T[0], 0);
5634
            tcg_gen_br(l2);
5635
            gen_set_label(l1);
5636
            tcg_gen_movi_tl(cpu_T[0], 1);
5637
            gen_set_label(l2);
5638
        }
5639
        opn = "bc1any2f";
5640
        goto not_likely;
5641
    case OPC_BC1TANY2:
5642
        {
5643
            int l1 = gen_new_label();
5644
            int l2 = gen_new_label();
5645
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
5646

    
5647
            get_fp_cond(r_tmp1);
5648
            tcg_gen_ext_i32_tl(cpu_T[0], r_tmp1);
5649
            tcg_temp_free(r_tmp1);
5650
            tcg_gen_movi_tl(cpu_T[1], 0x3 << cc);
5651
            tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
5652
            tcg_gen_brcondi_tl(TCG_COND_NE, cpu_T[0], 0, l1);
5653
            tcg_gen_movi_tl(cpu_T[0], 0);
5654
            tcg_gen_br(l2);
5655
            gen_set_label(l1);
5656
            tcg_gen_movi_tl(cpu_T[0], 1);
5657
            gen_set_label(l2);
5658
        }
5659
        opn = "bc1any2t";
5660
        goto not_likely;
5661
    case OPC_BC1FANY4:
5662
        {
5663
            int l1 = gen_new_label();
5664
            int l2 = gen_new_label();
5665
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
5666

    
5667
            get_fp_cond(r_tmp1);
5668
            tcg_gen_ext_i32_tl(cpu_T[0], r_tmp1);
5669
            tcg_temp_free(r_tmp1);
5670
            tcg_gen_not_tl(cpu_T[0], cpu_T[0]);
5671
            tcg_gen_movi_tl(cpu_T[1], 0xf << cc);
5672
            tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
5673
            tcg_gen_brcondi_tl(TCG_COND_NE, cpu_T[0], 0, l1);
5674
            tcg_gen_movi_tl(cpu_T[0], 0);
5675
            tcg_gen_br(l2);
5676
            gen_set_label(l1);
5677
            tcg_gen_movi_tl(cpu_T[0], 1);
5678
            gen_set_label(l2);
5679
        }
5680
        opn = "bc1any4f";
5681
        goto not_likely;
5682
    case OPC_BC1TANY4:
5683
        {
5684
            int l1 = gen_new_label();
5685
            int l2 = gen_new_label();
5686
            TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
5687

    
5688
            get_fp_cond(r_tmp1);
5689
            tcg_gen_ext_i32_tl(cpu_T[0], r_tmp1);
5690
            tcg_temp_free(r_tmp1);
5691
            tcg_gen_movi_tl(cpu_T[1], 0xf << cc);
5692
            tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
5693
            tcg_gen_brcondi_tl(TCG_COND_NE, cpu_T[0], 0, l1);
5694
            tcg_gen_movi_tl(cpu_T[0], 0);
5695
            tcg_gen_br(l2);
5696
            gen_set_label(l1);
5697
            tcg_gen_movi_tl(cpu_T[0], 1);
5698
            gen_set_label(l2);
5699
        }
5700
        opn = "bc1any4t";
5701
    not_likely:
5702
        ctx->hflags |= MIPS_HFLAG_BC;
5703
        tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUState, bcond));
5704
        break;
5705
    default:
5706
        MIPS_INVAL(opn);
5707
        generate_exception (ctx, EXCP_RI);
5708
        return;
5709
    }
5710
    MIPS_DEBUG("%s: cond %02x target " TARGET_FMT_lx, opn,
5711
               ctx->hflags, btarget);
5712
    ctx->btarget = btarget;
5713
}
5714

    
5715
/* Coprocessor 1 (FPU) */
5716

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

    
5719
static void gen_cp1 (DisasContext *ctx, uint32_t opc, int rt, int fs)
5720
{
5721
    const char *opn = "cp1 move";
5722

    
5723
    switch (opc) {
5724
    case OPC_MFC1:
5725
        gen_load_fpr32(fpu32_T[0], fs);
5726
        tcg_gen_ext_i32_tl(cpu_T[0], fpu32_T[0]);
5727
        gen_store_gpr(cpu_T[0], rt);
5728
        opn = "mfc1";
5729
        break;
5730
    case OPC_MTC1:
5731
        gen_load_gpr(cpu_T[0], rt);
5732
        tcg_gen_trunc_tl_i32(fpu32_T[0], cpu_T[0]);
5733
        gen_store_fpr32(fpu32_T[0], fs);
5734
        opn = "mtc1";
5735
        break;
5736
    case OPC_CFC1:
5737
        tcg_gen_helper_0_1i(do_cfc1, fs);
5738
        gen_store_gpr(cpu_T[0], rt);
5739
        opn = "cfc1";
5740
        break;
5741
    case OPC_CTC1:
5742
        gen_load_gpr(cpu_T[0], rt);
5743
        tcg_gen_helper_0_1i(do_ctc1, fs);
5744
        opn = "ctc1";
5745
        break;
5746
    case OPC_DMFC1:
5747
        gen_load_fpr64(ctx, fpu64_T[0], fs);
5748
        tcg_gen_mov_tl(cpu_T[0], fpu64_T[0]);
5749
        gen_store_gpr(cpu_T[0], rt);
5750
        opn = "dmfc1";
5751
        break;
5752
    case OPC_DMTC1:
5753
        gen_load_gpr(cpu_T[0], rt);
5754
        tcg_gen_mov_tl(fpu64_T[0], cpu_T[0]);
5755
        gen_store_fpr64(ctx, fpu64_T[0], fs);
5756
        opn = "dmtc1";
5757
        break;
5758
    case OPC_MFHC1:
5759
        gen_load_fpr32h(fpu32h_T[0], fs);
5760
        tcg_gen_ext_i32_tl(cpu_T[0], fpu32h_T[0]);
5761
        gen_store_gpr(cpu_T[0], rt);
5762
        opn = "mfhc1";
5763
        break;
5764
    case OPC_MTHC1:
5765
        gen_load_gpr(cpu_T[0], rt);
5766
        tcg_gen_trunc_tl_i32(fpu32h_T[0], cpu_T[0]);
5767
        gen_store_fpr32h(fpu32h_T[0], fs);
5768
        opn = "mthc1";
5769
        break;
5770
    default:
5771
        MIPS_INVAL(opn);
5772
        generate_exception (ctx, EXCP_RI);
5773
        return;
5774
    }
5775
    MIPS_DEBUG("%s %s %s", opn, regnames[rt], fregnames[fs]);
5776
}
5777

    
5778
static void gen_movci (DisasContext *ctx, int rd, int rs, int cc, int tf)
5779
{
5780
    int l1 = gen_new_label();
5781
    uint32_t ccbit;
5782
    TCGCond cond;
5783

    
5784
    if (cc)
5785
        ccbit = 1 << (24 + cc);
5786
    else
5787
        ccbit = 1 << 23;
5788
    if (tf)
5789
        cond = TCG_COND_EQ;
5790
    else
5791
        cond = TCG_COND_NE;
5792

    
5793
    gen_load_gpr(cpu_T[0], rd);
5794
    gen_load_gpr(cpu_T[1], rs);
5795
    {
5796
        TCGv r_ptr = tcg_temp_new(TCG_TYPE_PTR);
5797
        TCGv r_tmp = tcg_temp_local_new(TCG_TYPE_I32);
5798

    
5799
        tcg_gen_ld_ptr(r_ptr, cpu_env, offsetof(CPUState, fpu));
5800
        tcg_gen_ld_i32(r_tmp, r_ptr, offsetof(CPUMIPSFPUContext, fcr31));
5801
        tcg_temp_free(r_ptr);
5802
        tcg_gen_andi_i32(r_tmp, r_tmp, ccbit);
5803
        tcg_gen_brcondi_i32(cond, r_tmp, 0, l1);
5804
        tcg_temp_free(r_tmp);
5805
    }
5806
    tcg_gen_mov_tl(cpu_T[0], cpu_T[1]);
5807

    
5808
    gen_set_label(l1);
5809
    gen_store_gpr(cpu_T[0], rd);
5810
}
5811

    
5812
static inline void gen_movcf_s (int cc, int tf)
5813
{
5814
    uint32_t ccbit;
5815
    int cond;
5816
    TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
5817
    int l1 = gen_new_label();
5818

    
5819
    if (cc)
5820
        ccbit = 1 << (24 + cc);
5821
    else
5822
        ccbit = 1 << 23;
5823

    
5824
    if (tf)
5825
        cond = TCG_COND_EQ;
5826
    else
5827
        cond = TCG_COND_NE;
5828

    
5829
    tcg_gen_ld_i32(r_tmp1, current_fpu, offsetof(CPUMIPSFPUContext, fcr31));
5830
    tcg_gen_andi_i32(r_tmp1, r_tmp1, ccbit);
5831
    tcg_gen_brcondi_i32(cond, r_tmp1, 0, l1);
5832
    tcg_gen_movi_i32(fpu32_T[2], fpu32_T[0]);
5833
    gen_set_label(l1);
5834
    tcg_temp_free(r_tmp1);
5835
}
5836

    
5837
static inline void gen_movcf_d (int cc, int tf)
5838
{
5839
    uint32_t ccbit;
5840
    int cond;
5841
    TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I32);
5842
    int l1 = gen_new_label();
5843

    
5844
    if (cc)
5845
        ccbit = 1 << (24 + cc);
5846
    else
5847
        ccbit = 1 << 23;
5848

    
5849
    if (tf)
5850
        cond = TCG_COND_EQ;
5851
    else
5852
        cond = TCG_COND_NE;
5853

    
5854
    tcg_gen_ld_i32(r_tmp1, current_fpu, offsetof(CPUMIPSFPUContext, fcr31));
5855
    tcg_gen_andi_i32(r_tmp1, r_tmp1, ccbit);
5856
    tcg_gen_brcondi_i32(cond, r_tmp1, 0, l1);
5857
    tcg_gen_movi_i64(fpu64_T[2], fpu64_T[0]);
5858
    gen_set_label(l1);
5859
    tcg_temp_free(r_tmp1);
5860
}
5861

    
5862
static inline void gen_movcf_ps (int cc, int tf)
5863
{
5864
    int cond;
5865
    TCGv r_tmp1 = tcg_temp_local_new(TCG_TYPE_I32);
5866
    TCGv r_tmp2 = tcg_temp_local_new(TCG_TYPE_I32);
5867
    int l1 = gen_new_label();
5868
    int l2 = gen_new_label();
5869

    
5870
    if (tf)
5871
        cond = TCG_COND_EQ;
5872
    else
5873
        cond = TCG_COND_NE;
5874

    
5875
    get_fp_cond(r_tmp1);
5876
    tcg_gen_shri_i32(r_tmp1, r_tmp1, cc);
5877
    tcg_gen_andi_i32(r_tmp2, r_tmp1, 0x1);
5878
    tcg_gen_brcondi_i32(cond, r_tmp2, 0, l1);
5879
    tcg_gen_movi_i32(fpu32_T[2], fpu32_T[0]);
5880
    gen_set_label(l1);
5881
    tcg_gen_andi_i32(r_tmp2, r_tmp1, 0x2);
5882
    tcg_gen_brcondi_i32(cond, r_tmp2, 0, l2);
5883
    tcg_gen_movi_i32(fpu32h_T[2], fpu32h_T[0]);
5884
    gen_set_label(l2);
5885
    tcg_temp_free(r_tmp1);
5886
    tcg_temp_free(r_tmp2);
5887
}
5888

    
5889

    
5890
static void gen_farith (DisasContext *ctx, uint32_t op1,
5891
                        int ft, int fs, int fd, int cc)
5892
{
5893
    const char *opn = "farith";
5894
    const char *condnames[] = {
5895
            "c.f",
5896
            "c.un",
5897
            "c.eq",
5898
            "c.ueq",
5899
            "c.olt",
5900
            "c.ult",
5901
            "c.ole",
5902
            "c.ule",
5903
            "c.sf",
5904
            "c.ngle",
5905
            "c.seq",
5906
            "c.ngl",
5907
            "c.lt",
5908
            "c.nge",
5909
            "c.le",
5910
            "c.ngt",
5911
    };
5912
    const char *condnames_abs[] = {
5913
            "cabs.f",
5914
            "cabs.un",
5915
            "cabs.eq",
5916
            "cabs.ueq",
5917
            "cabs.olt",
5918
            "cabs.ult",
5919
            "cabs.ole",
5920
            "cabs.ule",
5921
            "cabs.sf",
5922
            "cabs.ngle",
5923
            "cabs.seq",
5924
            "cabs.ngl",
5925
            "cabs.lt",
5926
            "cabs.nge",
5927
            "cabs.le",
5928
            "cabs.ngt",
5929
    };
5930
    enum { BINOP, CMPOP, OTHEROP } optype = OTHEROP;
5931
    uint32_t func = ctx->opcode & 0x3f;
5932

    
5933
    switch (ctx->opcode & FOP(0x3f, 0x1f)) {
5934
    case FOP(0, 16):
5935
        gen_load_fpr32(fpu32_T[0], fs);
5936
        gen_load_fpr32(fpu32_T[1], ft);
5937
        tcg_gen_helper_0_0(do_float_add_s);
5938
        gen_store_fpr32(fpu32_T[2], fd);
5939
        opn = "add.s";
5940
        optype = BINOP;
5941
        break;
5942
    case FOP(1, 16):
5943
        gen_load_fpr32(fpu32_T[0], fs);
5944
        gen_load_fpr32(fpu32_T[1], ft);
5945
        tcg_gen_helper_0_0(do_float_sub_s);
5946
        gen_store_fpr32(fpu32_T[2], fd);
5947
        opn = "sub.s";
5948
        optype = BINOP;
5949
        break;
5950
    case FOP(2, 16):
5951
        gen_load_fpr32(fpu32_T[0], fs);
5952
        gen_load_fpr32(fpu32_T[1], ft);
5953
        tcg_gen_helper_0_0(do_float_mul_s);
5954
        gen_store_fpr32(fpu32_T[2], fd);
5955
        opn = "mul.s";
5956
        optype = BINOP;
5957
        break;
5958
    case FOP(3, 16):
5959
        gen_load_fpr32(fpu32_T[0], fs);
5960
        gen_load_fpr32(fpu32_T[1], ft);
5961
        tcg_gen_helper_0_0(do_float_div_s);
5962
        gen_store_fpr32(fpu32_T[2], fd);
5963
        opn = "div.s";
5964
        optype = BINOP;
5965
        break;
5966
    case FOP(4, 16):
5967
        gen_load_fpr32(fpu32_T[0], fs);
5968
        tcg_gen_helper_0_0(do_float_sqrt_s);
5969
        gen_store_fpr32(fpu32_T[2], fd);
5970
        opn = "sqrt.s";
5971
        break;
5972
    case FOP(5, 16):
5973
        gen_load_fpr32(fpu32_T[0], fs);
5974
        tcg_gen_helper_0_0(do_float_abs_s);
5975
        gen_store_fpr32(fpu32_T[2], fd);
5976
        opn = "abs.s";
5977
        break;
5978
    case FOP(6, 16):
5979
        gen_load_fpr32(fpu32_T[0], fs);
5980
        gen_store_fpr32(fpu32_T[0], fd);
5981
        opn = "mov.s";
5982
        break;
5983
    case FOP(7, 16):
5984
        gen_load_fpr32(fpu32_T[0], fs);
5985
        tcg_gen_helper_0_0(do_float_chs_s);
5986
        gen_store_fpr32(fpu32_T[2], fd);
5987
        opn = "neg.s";
5988
        break;
5989
    case FOP(8, 16):
5990
        check_cp1_64bitmode(ctx);
5991
        gen_load_fpr32(fpu32_T[0], fs);
5992
        tcg_gen_helper_0_0(do_float_roundl_s);
5993
        gen_store_fpr64(ctx, fpu64_T[2], fd);
5994
        opn = "round.l.s";
5995
        break;
5996
    case FOP(9, 16):
5997
        check_cp1_64bitmode(ctx);
5998
        gen_load_fpr32(fpu32_T[0], fs);
5999
        tcg_gen_helper_0_0(do_float_truncl_s);
6000
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6001
        opn = "trunc.l.s";
6002
        break;
6003
    case FOP(10, 16):
6004
        check_cp1_64bitmode(ctx);
6005
        gen_load_fpr32(fpu32_T[0], fs);
6006
        tcg_gen_helper_0_0(do_float_ceill_s);
6007
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6008
        opn = "ceil.l.s";
6009
        break;
6010
    case FOP(11, 16):
6011
        check_cp1_64bitmode(ctx);
6012
        gen_load_fpr32(fpu32_T[0], fs);
6013
        tcg_gen_helper_0_0(do_float_floorl_s);
6014
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6015
        opn = "floor.l.s";
6016
        break;
6017
    case FOP(12, 16):
6018
        gen_load_fpr32(fpu32_T[0], fs);
6019
        tcg_gen_helper_0_0(do_float_roundw_s);
6020
        gen_store_fpr32(fpu32_T[2], fd);
6021
        opn = "round.w.s";
6022
        break;
6023
    case FOP(13, 16):
6024
        gen_load_fpr32(fpu32_T[0], fs);
6025
        tcg_gen_helper_0_0(do_float_truncw_s);
6026
        gen_store_fpr32(fpu32_T[2], fd);
6027
        opn = "trunc.w.s";
6028
        break;
6029
    case FOP(14, 16):
6030
        gen_load_fpr32(fpu32_T[0], fs);
6031
        tcg_gen_helper_0_0(do_float_ceilw_s);
6032
        gen_store_fpr32(fpu32_T[2], fd);
6033
        opn = "ceil.w.s";
6034
        break;
6035
    case FOP(15, 16):
6036
        gen_load_fpr32(fpu32_T[0], fs);
6037
        tcg_gen_helper_0_0(do_float_floorw_s);
6038
        gen_store_fpr32(fpu32_T[2], fd);
6039
        opn = "floor.w.s";
6040
        break;
6041
    case FOP(17, 16):
6042
        gen_load_fpr32(fpu32_T[0], fs);
6043
        gen_load_fpr32(fpu32_T[2], fd);
6044
        gen_movcf_s((ft >> 2) & 0x7, ft & 0x1);
6045
        gen_store_fpr32(fpu32_T[2], fd);
6046
        opn = "movcf.s";
6047
        break;
6048
    case FOP(18, 16):
6049
        gen_load_gpr(cpu_T[0], ft);
6050
        gen_load_fpr32(fpu32_T[0], fs);
6051
        gen_load_fpr32(fpu32_T[2], fd);
6052
        {
6053
            int l1 = gen_new_label();
6054

    
6055
            tcg_gen_brcondi_tl(TCG_COND_NE, cpu_T[0], 0, l1);
6056
            tcg_gen_mov_i32(fpu32_T[2], fpu32_T[0]);
6057
            gen_set_label(l1);
6058
        }
6059
        gen_store_fpr32(fpu32_T[2], fd);
6060
        opn = "movz.s";
6061
        break;
6062
    case FOP(19, 16):
6063
        gen_load_gpr(cpu_T[0], ft);
6064
        gen_load_fpr32(fpu32_T[0], fs);
6065
        gen_load_fpr32(fpu32_T[2], fd);
6066
        {
6067
            int l1 = gen_new_label();
6068

    
6069
            tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_T[0], 0, l1);
6070
            tcg_gen_mov_i32(fpu32_T[2], fpu32_T[0]);
6071
            gen_set_label(l1);
6072
        }
6073
        gen_store_fpr32(fpu32_T[2], fd);
6074
        opn = "movn.s";
6075
        break;
6076
    case FOP(21, 16):
6077
        check_cop1x(ctx);
6078
        gen_load_fpr32(fpu32_T[0], fs);
6079
        tcg_gen_helper_0_0(do_float_recip_s);
6080
        gen_store_fpr32(fpu32_T[2], fd);
6081
        opn = "recip.s";
6082
        break;
6083
    case FOP(22, 16):
6084
        check_cop1x(ctx);
6085
        gen_load_fpr32(fpu32_T[0], fs);
6086
        tcg_gen_helper_0_0(do_float_rsqrt_s);
6087
        gen_store_fpr32(fpu32_T[2], fd);
6088
        opn = "rsqrt.s";
6089
        break;
6090
    case FOP(28, 16):
6091
        check_cp1_64bitmode(ctx);
6092
        gen_load_fpr32(fpu32_T[0], fs);
6093
        gen_load_fpr32(fpu32_T[2], fd);
6094
        tcg_gen_helper_0_0(do_float_recip2_s);
6095
        gen_store_fpr32(fpu32_T[2], fd);
6096
        opn = "recip2.s";
6097
        break;
6098
    case FOP(29, 16):
6099
        check_cp1_64bitmode(ctx);
6100
        gen_load_fpr32(fpu32_T[0], fs);
6101
        tcg_gen_helper_0_0(do_float_recip1_s);
6102
        gen_store_fpr32(fpu32_T[2], fd);
6103
        opn = "recip1.s";
6104
        break;
6105
    case FOP(30, 16):
6106
        check_cp1_64bitmode(ctx);
6107
        gen_load_fpr32(fpu32_T[0], fs);
6108
        tcg_gen_helper_0_0(do_float_rsqrt1_s);
6109
        gen_store_fpr32(fpu32_T[2], fd);
6110
        opn = "rsqrt1.s";
6111
        break;
6112
    case FOP(31, 16):
6113
        check_cp1_64bitmode(ctx);
6114
        gen_load_fpr32(fpu32_T[0], fs);
6115
        gen_load_fpr32(fpu32_T[2], ft);
6116
        tcg_gen_helper_0_0(do_float_rsqrt2_s);
6117
        gen_store_fpr32(fpu32_T[2], fd);
6118
        opn = "rsqrt2.s";
6119
        break;
6120
    case FOP(33, 16):
6121
        check_cp1_registers(ctx, fd);
6122
        gen_load_fpr32(fpu32_T[0], fs);
6123
        tcg_gen_helper_0_0(do_float_cvtd_s);
6124
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6125
        opn = "cvt.d.s";
6126
        break;
6127
    case FOP(36, 16):
6128
        gen_load_fpr32(fpu32_T[0], fs);
6129
        tcg_gen_helper_0_0(do_float_cvtw_s);
6130
        gen_store_fpr32(fpu32_T[2], fd);
6131
        opn = "cvt.w.s";
6132
        break;
6133
    case FOP(37, 16):
6134
        check_cp1_64bitmode(ctx);
6135
        gen_load_fpr32(fpu32_T[0], fs);
6136
        tcg_gen_helper_0_0(do_float_cvtl_s);
6137
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6138
        opn = "cvt.l.s";
6139
        break;
6140
    case FOP(38, 16):
6141
        check_cp1_64bitmode(ctx);
6142
        gen_load_fpr32(fpu32_T[0], fs);
6143
        gen_load_fpr32(fpu32_T[1], ft);
6144
        tcg_gen_extu_i32_i64(fpu64_T[0], fpu32_T[0]);
6145
        tcg_gen_extu_i32_i64(fpu64_T[1], fpu32_T[1]);
6146
        tcg_gen_shli_i64(fpu64_T[1], fpu64_T[1], 32);
6147
        tcg_gen_or_i64(fpu64_T[2], fpu64_T[0], fpu64_T[1]);
6148
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6149
        opn = "cvt.ps.s";
6150
        break;
6151
    case FOP(48, 16):
6152
    case FOP(49, 16):
6153
    case FOP(50, 16):
6154
    case FOP(51, 16):
6155
    case FOP(52, 16):
6156
    case FOP(53, 16):
6157
    case FOP(54, 16):
6158
    case FOP(55, 16):
6159
    case FOP(56, 16):
6160
    case FOP(57, 16):
6161
    case FOP(58, 16):
6162
    case FOP(59, 16):
6163
    case FOP(60, 16):
6164
    case FOP(61, 16):
6165
    case FOP(62, 16):
6166
    case FOP(63, 16):
6167
        gen_load_fpr32(fpu32_T[0], fs);
6168
        gen_load_fpr32(fpu32_T[1], ft);
6169
        if (ctx->opcode & (1 << 6)) {
6170
            check_cop1x(ctx);
6171
            gen_cmpabs_s(func-48, cc);
6172
            opn = condnames_abs[func-48];
6173
        } else {
6174
            gen_cmp_s(func-48, cc);
6175
            opn = condnames[func-48];
6176
        }
6177
        break;
6178
    case FOP(0, 17):
6179
        check_cp1_registers(ctx, fs | ft | fd);
6180
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6181
        gen_load_fpr64(ctx, fpu64_T[1], ft);
6182
        tcg_gen_helper_0_0(do_float_add_d);
6183
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6184
        opn = "add.d";
6185
        optype = BINOP;
6186
        break;
6187
    case FOP(1, 17):
6188
        check_cp1_registers(ctx, fs | ft | fd);
6189
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6190
        gen_load_fpr64(ctx, fpu64_T[1], ft);
6191
        tcg_gen_helper_0_0(do_float_sub_d);
6192
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6193
        opn = "sub.d";
6194
        optype = BINOP;
6195
        break;
6196
    case FOP(2, 17):
6197
        check_cp1_registers(ctx, fs | ft | fd);
6198
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6199
        gen_load_fpr64(ctx, fpu64_T[1], ft);
6200
        tcg_gen_helper_0_0(do_float_mul_d);
6201
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6202
        opn = "mul.d";
6203
        optype = BINOP;
6204
        break;
6205
    case FOP(3, 17):
6206
        check_cp1_registers(ctx, fs | ft | fd);
6207
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6208
        gen_load_fpr64(ctx, fpu64_T[1], ft);
6209
        tcg_gen_helper_0_0(do_float_div_d);
6210
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6211
        opn = "div.d";
6212
        optype = BINOP;
6213
        break;
6214
    case FOP(4, 17):
6215
        check_cp1_registers(ctx, fs | fd);
6216
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6217
        tcg_gen_helper_0_0(do_float_sqrt_d);
6218
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6219
        opn = "sqrt.d";
6220
        break;
6221
    case FOP(5, 17):
6222
        check_cp1_registers(ctx, fs | fd);
6223
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6224
        tcg_gen_helper_0_0(do_float_abs_d);
6225
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6226
        opn = "abs.d";
6227
        break;
6228
    case FOP(6, 17):
6229
        check_cp1_registers(ctx, fs | fd);
6230
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6231
        gen_store_fpr64(ctx, fpu64_T[0], fd);
6232
        opn = "mov.d";
6233
        break;
6234
    case FOP(7, 17):
6235
        check_cp1_registers(ctx, fs | fd);
6236
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6237
        tcg_gen_helper_0_0(do_float_chs_d);
6238
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6239
        opn = "neg.d";
6240
        break;
6241
    case FOP(8, 17):
6242
        check_cp1_64bitmode(ctx);
6243
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6244
        tcg_gen_helper_0_0(do_float_roundl_d);
6245
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6246
        opn = "round.l.d";
6247
        break;
6248
    case FOP(9, 17):
6249
        check_cp1_64bitmode(ctx);
6250
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6251
        tcg_gen_helper_0_0(do_float_truncl_d);
6252
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6253
        opn = "trunc.l.d";
6254
        break;
6255
    case FOP(10, 17):
6256
        check_cp1_64bitmode(ctx);
6257
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6258
        tcg_gen_helper_0_0(do_float_ceill_d);
6259
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6260
        opn = "ceil.l.d";
6261
        break;
6262
    case FOP(11, 17):
6263
        check_cp1_64bitmode(ctx);
6264
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6265
        tcg_gen_helper_0_0(do_float_floorl_d);
6266
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6267
        opn = "floor.l.d";
6268
        break;
6269
    case FOP(12, 17):
6270
        check_cp1_registers(ctx, fs);
6271
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6272
        tcg_gen_helper_0_0(do_float_roundw_d);
6273
        gen_store_fpr32(fpu32_T[2], fd);
6274
        opn = "round.w.d";
6275
        break;
6276
    case FOP(13, 17):
6277
        check_cp1_registers(ctx, fs);
6278
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6279
        tcg_gen_helper_0_0(do_float_truncw_d);
6280
        gen_store_fpr32(fpu32_T[2], fd);
6281
        opn = "trunc.w.d";
6282
        break;
6283
    case FOP(14, 17):
6284
        check_cp1_registers(ctx, fs);
6285
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6286
        tcg_gen_helper_0_0(do_float_ceilw_d);
6287
        gen_store_fpr32(fpu32_T[2], fd);
6288
        opn = "ceil.w.d";
6289
        break;
6290
    case FOP(15, 17):
6291
        check_cp1_registers(ctx, fs);
6292
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6293
        tcg_gen_helper_0_0(do_float_floorw_d);
6294
        gen_store_fpr32(fpu32_T[2], fd);
6295
        opn = "floor.w.d";
6296
        break;
6297
    case FOP(17, 17):
6298
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6299
        gen_load_fpr64(ctx, fpu64_T[2], fd);
6300
        gen_movcf_d((ft >> 2) & 0x7, ft & 0x1);
6301
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6302
        opn = "movcf.d";
6303
        break;
6304
    case FOP(18, 17):
6305
        gen_load_gpr(cpu_T[0], ft);
6306
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6307
        gen_load_fpr64(ctx, fpu64_T[2], fd);
6308
        {
6309
            int l1 = gen_new_label();
6310

    
6311
            tcg_gen_brcondi_tl(TCG_COND_NE, cpu_T[0], 0, l1);
6312
            tcg_gen_mov_i64(fpu64_T[2], fpu64_T[0]);
6313
            gen_set_label(l1);
6314
        }
6315
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6316
        opn = "movz.d";
6317
        break;
6318
    case FOP(19, 17):
6319
        gen_load_gpr(cpu_T[0], ft);
6320
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6321
        gen_load_fpr64(ctx, fpu64_T[2], fd);
6322
        {
6323
            int l1 = gen_new_label();
6324

    
6325
            tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_T[0], 0, l1);
6326
            tcg_gen_mov_i64(fpu64_T[2], fpu64_T[0]);
6327
            gen_set_label(l1);
6328
        }
6329
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6330
        opn = "movn.d";
6331
        break;
6332
    case FOP(21, 17):
6333
        check_cp1_64bitmode(ctx);
6334
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6335
        tcg_gen_helper_0_0(do_float_recip_d);
6336
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6337
        opn = "recip.d";
6338
        break;
6339
    case FOP(22, 17):
6340
        check_cp1_64bitmode(ctx);
6341
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6342
        tcg_gen_helper_0_0(do_float_rsqrt_d);
6343
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6344
        opn = "rsqrt.d";
6345
        break;
6346
    case FOP(28, 17):
6347
        check_cp1_64bitmode(ctx);
6348
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6349
        gen_load_fpr64(ctx, fpu64_T[2], ft);
6350
        tcg_gen_helper_0_0(do_float_recip2_d);
6351
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6352
        opn = "recip2.d";
6353
        break;
6354
    case FOP(29, 17):
6355
        check_cp1_64bitmode(ctx);
6356
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6357
        tcg_gen_helper_0_0(do_float_recip1_d);
6358
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6359
        opn = "recip1.d";
6360
        break;
6361
    case FOP(30, 17):
6362
        check_cp1_64bitmode(ctx);
6363
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6364
        tcg_gen_helper_0_0(do_float_rsqrt1_d);
6365
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6366
        opn = "rsqrt1.d";
6367
        break;
6368
    case FOP(31, 17):
6369
        check_cp1_64bitmode(ctx);
6370
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6371
        gen_load_fpr64(ctx, fpu64_T[2], ft);
6372
        tcg_gen_helper_0_0(do_float_rsqrt2_d);
6373
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6374
        opn = "rsqrt2.d";
6375
        break;
6376
    case FOP(48, 17):
6377
    case FOP(49, 17):
6378
    case FOP(50, 17):
6379
    case FOP(51, 17):
6380
    case FOP(52, 17):
6381
    case FOP(53, 17):
6382
    case FOP(54, 17):
6383
    case FOP(55, 17):
6384
    case FOP(56, 17):
6385
    case FOP(57, 17):
6386
    case FOP(58, 17):
6387
    case FOP(59, 17):
6388
    case FOP(60, 17):
6389
    case FOP(61, 17):
6390
    case FOP(62, 17):
6391
    case FOP(63, 17):
6392
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6393
        gen_load_fpr64(ctx, fpu64_T[1], ft);
6394
        if (ctx->opcode & (1 << 6)) {
6395
            check_cop1x(ctx);
6396
            check_cp1_registers(ctx, fs | ft);
6397
            gen_cmpabs_d(func-48, cc);
6398
            opn = condnames_abs[func-48];
6399
        } else {
6400
            check_cp1_registers(ctx, fs | ft);
6401
            gen_cmp_d(func-48, cc);
6402
            opn = condnames[func-48];
6403
        }
6404
        break;
6405
    case FOP(32, 17):
6406
        check_cp1_registers(ctx, fs);
6407
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6408
        tcg_gen_helper_0_0(do_float_cvts_d);
6409
        gen_store_fpr32(fpu32_T[2], fd);
6410
        opn = "cvt.s.d";
6411
        break;
6412
    case FOP(36, 17):
6413
        check_cp1_registers(ctx, fs);
6414
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6415
        tcg_gen_helper_0_0(do_float_cvtw_d);
6416
        gen_store_fpr32(fpu32_T[2], fd);
6417
        opn = "cvt.w.d";
6418
        break;
6419
    case FOP(37, 17):
6420
        check_cp1_64bitmode(ctx);
6421
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6422
        tcg_gen_helper_0_0(do_float_cvtl_d);
6423
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6424
        opn = "cvt.l.d";
6425
        break;
6426
    case FOP(32, 20):
6427
        gen_load_fpr32(fpu32_T[0], fs);
6428
        tcg_gen_helper_0_0(do_float_cvts_w);
6429
        gen_store_fpr32(fpu32_T[2], fd);
6430
        opn = "cvt.s.w";
6431
        break;
6432
    case FOP(33, 20):
6433
        check_cp1_registers(ctx, fd);
6434
        gen_load_fpr32(fpu32_T[0], fs);
6435
        tcg_gen_helper_0_0(do_float_cvtd_w);
6436
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6437
        opn = "cvt.d.w";
6438
        break;
6439
    case FOP(32, 21):
6440
        check_cp1_64bitmode(ctx);
6441
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6442
        tcg_gen_helper_0_0(do_float_cvts_l);
6443
        gen_store_fpr32(fpu32_T[2], fd);
6444
        opn = "cvt.s.l";
6445
        break;
6446
    case FOP(33, 21):
6447
        check_cp1_64bitmode(ctx);
6448
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6449
        tcg_gen_helper_0_0(do_float_cvtd_l);
6450
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6451
        opn = "cvt.d.l";
6452
        break;
6453
    case FOP(38, 20):
6454
        check_cp1_64bitmode(ctx);
6455
        gen_load_fpr32(fpu32_T[0], fs);
6456
        gen_load_fpr32h(fpu32h_T[0], fs);
6457
        tcg_gen_helper_0_0(do_float_cvtps_pw);
6458
        gen_store_fpr32(fpu32_T[2], fd);
6459
        gen_store_fpr32h(fpu32h_T[2], fd);
6460
        opn = "cvt.ps.pw";
6461
        break;
6462
    case FOP(0, 22):
6463
        check_cp1_64bitmode(ctx);
6464
        gen_load_fpr32(fpu32_T[0], fs);
6465
        gen_load_fpr32h(fpu32h_T[0], fs);
6466
        gen_load_fpr32(fpu32_T[1], ft);
6467
        gen_load_fpr32h(fpu32h_T[1], ft);
6468
        tcg_gen_helper_0_0(do_float_add_ps);
6469
        gen_store_fpr32(fpu32_T[2], fd);
6470
        gen_store_fpr32h(fpu32h_T[2], fd);
6471
        opn = "add.ps";
6472
        break;
6473
    case FOP(1, 22):
6474
        check_cp1_64bitmode(ctx);
6475
        gen_load_fpr32(fpu32_T[0], fs);
6476
        gen_load_fpr32h(fpu32h_T[0], fs);
6477
        gen_load_fpr32(fpu32_T[1], ft);
6478
        gen_load_fpr32h(fpu32h_T[1], ft);
6479
        tcg_gen_helper_0_0(do_float_sub_ps);
6480
        gen_store_fpr32(fpu32_T[2], fd);
6481
        gen_store_fpr32h(fpu32h_T[2], fd);
6482
        opn = "sub.ps";
6483
        break;
6484
    case FOP(2, 22):
6485
        check_cp1_64bitmode(ctx);
6486
        gen_load_fpr32(fpu32_T[0], fs);
6487
        gen_load_fpr32h(fpu32h_T[0], fs);
6488
        gen_load_fpr32(fpu32_T[1], ft);
6489
        gen_load_fpr32h(fpu32h_T[1], ft);
6490
        tcg_gen_helper_0_0(do_float_mul_ps);
6491
        gen_store_fpr32(fpu32_T[2], fd);
6492
        gen_store_fpr32h(fpu32h_T[2], fd);
6493
        opn = "mul.ps";
6494
        break;
6495
    case FOP(5, 22):
6496
        check_cp1_64bitmode(ctx);
6497
        gen_load_fpr32(fpu32_T[0], fs);
6498
        gen_load_fpr32h(fpu32h_T[0], fs);
6499
        tcg_gen_helper_0_0(do_float_abs_ps);
6500
        gen_store_fpr32(fpu32_T[2], fd);
6501
        gen_store_fpr32h(fpu32h_T[2], fd);
6502
        opn = "abs.ps";
6503
        break;
6504
    case FOP(6, 22):
6505
        check_cp1_64bitmode(ctx);
6506
        gen_load_fpr32(fpu32_T[0], fs);
6507
        gen_load_fpr32h(fpu32h_T[0], fs);
6508
        gen_store_fpr32(fpu32_T[0], fd);
6509
        gen_store_fpr32h(fpu32h_T[0], fd);
6510
        opn = "mov.ps";
6511
        break;
6512
    case FOP(7, 22):
6513
        check_cp1_64bitmode(ctx);
6514
        gen_load_fpr32(fpu32_T[0], fs);
6515
        gen_load_fpr32h(fpu32h_T[0], fs);
6516
        tcg_gen_helper_0_0(do_float_chs_ps);
6517
        gen_store_fpr32(fpu32_T[2], fd);
6518
        gen_store_fpr32h(fpu32h_T[2], fd);
6519
        opn = "neg.ps";
6520
        break;
6521
    case FOP(17, 22):
6522
        check_cp1_64bitmode(ctx);
6523
        gen_load_fpr32(fpu32_T[0], fs);
6524
        gen_load_fpr32h(fpu32h_T[0], fs);
6525
        gen_load_fpr32(fpu32_T[2], fd);
6526
        gen_load_fpr32h(fpu32h_T[2], fd);
6527
        gen_movcf_ps((ft >> 2) & 0x7, ft & 0x1);
6528
        gen_store_fpr32(fpu32_T[2], fd);
6529
        gen_store_fpr32h(fpu32h_T[2], fd);
6530
        opn = "movcf.ps";
6531
        break;
6532
    case FOP(18, 22):
6533
        check_cp1_64bitmode(ctx);
6534
        gen_load_gpr(cpu_T[0], ft);
6535
        gen_load_fpr32(fpu32_T[0], fs);
6536
        gen_load_fpr32h(fpu32h_T[0], fs);
6537
        gen_load_fpr32(fpu32_T[2], fd);
6538
        gen_load_fpr32h(fpu32h_T[2], fd);
6539
        {
6540
            int l1 = gen_new_label();
6541

    
6542
            tcg_gen_brcondi_tl(TCG_COND_NE, cpu_T[0], 0, l1);
6543
            tcg_gen_mov_i32(fpu32_T[2], fpu32_T[0]);
6544
            tcg_gen_mov_i32(fpu32h_T[2], fpu32h_T[0]);
6545
            gen_set_label(l1);
6546
        }
6547
        gen_store_fpr32(fpu32_T[2], fd);
6548
        gen_store_fpr32h(fpu32h_T[2], fd);
6549
        opn = "movz.ps";
6550
        break;
6551
    case FOP(19, 22):
6552
        check_cp1_64bitmode(ctx);
6553
        gen_load_gpr(cpu_T[0], ft);
6554
        gen_load_fpr32(fpu32_T[0], fs);
6555
        gen_load_fpr32h(fpu32h_T[0], fs);
6556
        gen_load_fpr32(fpu32_T[2], fd);
6557
        gen_load_fpr32h(fpu32h_T[2], fd);
6558
        {
6559
            int l1 = gen_new_label();
6560

    
6561
            tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_T[0], 0, l1);
6562
            tcg_gen_mov_i32(fpu32_T[2], fpu32_T[0]);
6563
            tcg_gen_mov_i32(fpu32h_T[2], fpu32h_T[0]);
6564
            gen_set_label(l1);
6565
        }
6566
        gen_store_fpr32(fpu32_T[2], fd);
6567
        gen_store_fpr32h(fpu32h_T[2], fd);
6568
        opn = "movn.ps";
6569
        break;
6570
    case FOP(24, 22):
6571
        check_cp1_64bitmode(ctx);
6572
        gen_load_fpr32(fpu32_T[0], ft);
6573
        gen_load_fpr32h(fpu32h_T[0], ft);
6574
        gen_load_fpr32(fpu32_T[1], fs);
6575
        gen_load_fpr32h(fpu32h_T[1], fs);
6576
        tcg_gen_helper_0_0(do_float_addr_ps);
6577
        gen_store_fpr32(fpu32_T[2], fd);
6578
        gen_store_fpr32h(fpu32h_T[2], fd);
6579
        opn = "addr.ps";
6580
        break;
6581
    case FOP(26, 22):
6582
        check_cp1_64bitmode(ctx);
6583
        gen_load_fpr32(fpu32_T[0], ft);
6584
        gen_load_fpr32h(fpu32h_T[0], ft);
6585
        gen_load_fpr32(fpu32_T[1], fs);
6586
        gen_load_fpr32h(fpu32h_T[1], fs);
6587
        tcg_gen_helper_0_0(do_float_mulr_ps);
6588
        gen_store_fpr32(fpu32_T[2], fd);
6589
        gen_store_fpr32h(fpu32h_T[2], fd);
6590
        opn = "mulr.ps";
6591
        break;
6592
    case FOP(28, 22):
6593
        check_cp1_64bitmode(ctx);
6594
        gen_load_fpr32(fpu32_T[0], fs);
6595
        gen_load_fpr32h(fpu32h_T[0], fs);
6596
        gen_load_fpr32(fpu32_T[2], fd);
6597
        gen_load_fpr32h(fpu32h_T[2], fd);
6598
        tcg_gen_helper_0_0(do_float_recip2_ps);
6599
        gen_store_fpr32(fpu32_T[2], fd);
6600
        gen_store_fpr32h(fpu32h_T[2], fd);
6601
        opn = "recip2.ps";
6602
        break;
6603
    case FOP(29, 22):
6604
        check_cp1_64bitmode(ctx);
6605
        gen_load_fpr32(fpu32_T[0], fs);
6606
        gen_load_fpr32h(fpu32h_T[0], fs);
6607
        tcg_gen_helper_0_0(do_float_recip1_ps);
6608
        gen_store_fpr32(fpu32_T[2], fd);
6609
        gen_store_fpr32h(fpu32h_T[2], fd);
6610
        opn = "recip1.ps";
6611
        break;
6612
    case FOP(30, 22):
6613
        check_cp1_64bitmode(ctx);
6614
        gen_load_fpr32(fpu32_T[0], fs);
6615
        gen_load_fpr32h(fpu32h_T[0], fs);
6616
        tcg_gen_helper_0_0(do_float_rsqrt1_ps);
6617
        gen_store_fpr32(fpu32_T[2], fd);
6618
        gen_store_fpr32h(fpu32h_T[2], fd);
6619
        opn = "rsqrt1.ps";
6620
        break;
6621
    case FOP(31, 22):
6622
        check_cp1_64bitmode(ctx);
6623
        gen_load_fpr32(fpu32_T[0], fs);
6624
        gen_load_fpr32h(fpu32h_T[0], fs);
6625
        gen_load_fpr32(fpu32_T[2], ft);
6626
        gen_load_fpr32h(fpu32h_T[2], ft);
6627
        tcg_gen_helper_0_0(do_float_rsqrt2_ps);
6628
        gen_store_fpr32(fpu32_T[2], fd);
6629
        gen_store_fpr32h(fpu32h_T[2], fd);
6630
        opn = "rsqrt2.ps";
6631
        break;
6632
    case FOP(32, 22):
6633
        check_cp1_64bitmode(ctx);
6634
        gen_load_fpr32h(fpu32h_T[0], fs);
6635
        tcg_gen_helper_0_0(do_float_cvts_pu);
6636
        gen_store_fpr32(fpu32_T[2], fd);
6637
        opn = "cvt.s.pu";
6638
        break;
6639
    case FOP(36, 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_cvtpw_ps);
6644
        gen_store_fpr32(fpu32_T[2], fd);
6645
        gen_store_fpr32h(fpu32h_T[2], fd);
6646
        opn = "cvt.pw.ps";
6647
        break;
6648
    case FOP(40, 22):
6649
        check_cp1_64bitmode(ctx);
6650
        gen_load_fpr32(fpu32_T[0], fs);
6651
        tcg_gen_helper_0_0(do_float_cvts_pl);
6652
        gen_store_fpr32(fpu32_T[2], fd);
6653
        opn = "cvt.s.pl";
6654
        break;
6655
    case FOP(44, 22):
6656
        check_cp1_64bitmode(ctx);
6657
        gen_load_fpr32(fpu32_T[0], fs);
6658
        gen_load_fpr32(fpu32_T[1], ft);
6659
        gen_store_fpr32h(fpu32_T[0], fd);
6660
        gen_store_fpr32(fpu32_T[1], fd);
6661
        opn = "pll.ps";
6662
        break;
6663
    case FOP(45, 22):
6664
        check_cp1_64bitmode(ctx);
6665
        gen_load_fpr32(fpu32_T[0], fs);
6666
        gen_load_fpr32h(fpu32h_T[1], ft);
6667
        gen_store_fpr32(fpu32h_T[1], fd);
6668
        gen_store_fpr32h(fpu32_T[0], fd);
6669
        opn = "plu.ps";
6670
        break;
6671
    case FOP(46, 22):
6672
        check_cp1_64bitmode(ctx);
6673
        gen_load_fpr32h(fpu32h_T[0], fs);
6674
        gen_load_fpr32(fpu32_T[1], ft);
6675
        gen_store_fpr32(fpu32_T[1], fd);
6676
        gen_store_fpr32h(fpu32h_T[0], fd);
6677
        opn = "pul.ps";
6678
        break;
6679
    case FOP(47, 22):
6680
        check_cp1_64bitmode(ctx);
6681
        gen_load_fpr32h(fpu32h_T[0], fs);
6682
        gen_load_fpr32h(fpu32h_T[1], ft);
6683
        gen_store_fpr32(fpu32h_T[1], fd);
6684
        gen_store_fpr32h(fpu32h_T[0], fd);
6685
        opn = "puu.ps";
6686
        break;
6687
    case FOP(48, 22):
6688
    case FOP(49, 22):
6689
    case FOP(50, 22):
6690
    case FOP(51, 22):
6691
    case FOP(52, 22):
6692
    case FOP(53, 22):
6693
    case FOP(54, 22):
6694
    case FOP(55, 22):
6695
    case FOP(56, 22):
6696
    case FOP(57, 22):
6697
    case FOP(58, 22):
6698
    case FOP(59, 22):
6699
    case FOP(60, 22):
6700
    case FOP(61, 22):
6701
    case FOP(62, 22):
6702
    case FOP(63, 22):
6703
        check_cp1_64bitmode(ctx);
6704
        gen_load_fpr32(fpu32_T[0], fs);
6705
        gen_load_fpr32h(fpu32h_T[0], fs);
6706
        gen_load_fpr32(fpu32_T[1], ft);
6707
        gen_load_fpr32h(fpu32h_T[1], ft);
6708
        if (ctx->opcode & (1 << 6)) {
6709
            gen_cmpabs_ps(func-48, cc);
6710
            opn = condnames_abs[func-48];
6711
        } else {
6712
            gen_cmp_ps(func-48, cc);
6713
            opn = condnames[func-48];
6714
        }
6715
        break;
6716
    default:
6717
        MIPS_INVAL(opn);
6718
        generate_exception (ctx, EXCP_RI);
6719
        return;
6720
    }
6721
    switch (optype) {
6722
    case BINOP:
6723
        MIPS_DEBUG("%s %s, %s, %s", opn, fregnames[fd], fregnames[fs], fregnames[ft]);
6724
        break;
6725
    case CMPOP:
6726
        MIPS_DEBUG("%s %s,%s", opn, fregnames[fs], fregnames[ft]);
6727
        break;
6728
    default:
6729
        MIPS_DEBUG("%s %s,%s", opn, fregnames[fd], fregnames[fs]);
6730
        break;
6731
    }
6732
}
6733

    
6734
/* Coprocessor 3 (FPU) */
6735
static void gen_flt3_ldst (DisasContext *ctx, uint32_t opc,
6736
                           int fd, int fs, int base, int index)
6737
{
6738
    const char *opn = "extended float load/store";
6739
    int store = 0;
6740

    
6741
    if (base == 0) {
6742
        gen_load_gpr(cpu_T[0], index);
6743
    } else if (index == 0) {
6744
        gen_load_gpr(cpu_T[0], base);
6745
    } else {
6746
        gen_load_gpr(cpu_T[0], base);
6747
        gen_load_gpr(cpu_T[1], index);
6748
        gen_op_addr_add();
6749
    }
6750
    /* Don't do NOP if destination is zero: we must perform the actual
6751
       memory access. */
6752
    switch (opc) {
6753
    case OPC_LWXC1:
6754
        check_cop1x(ctx);
6755
        tcg_gen_qemu_ld32s(fpu32_T[0], cpu_T[0], ctx->mem_idx);
6756
        gen_store_fpr32(fpu32_T[0], fd);
6757
        opn = "lwxc1";
6758
        break;
6759
    case OPC_LDXC1:
6760
        check_cop1x(ctx);
6761
        check_cp1_registers(ctx, fd);
6762
        tcg_gen_qemu_ld64(fpu64_T[0], cpu_T[0], ctx->mem_idx);
6763
        gen_store_fpr64(ctx, fpu64_T[0], fd);
6764
        opn = "ldxc1";
6765
        break;
6766
    case OPC_LUXC1:
6767
        check_cp1_64bitmode(ctx);
6768
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], ~0x7);
6769
        tcg_gen_qemu_ld64(fpu64_T[0], cpu_T[0], ctx->mem_idx);
6770
        gen_store_fpr64(ctx, fpu64_T[0], fd);
6771
        opn = "luxc1";
6772
        break;
6773
    case OPC_SWXC1:
6774
        check_cop1x(ctx);
6775
        gen_load_fpr32(fpu32_T[0], fs);
6776
        tcg_gen_qemu_st32(fpu32_T[0], cpu_T[0], ctx->mem_idx);
6777
        opn = "swxc1";
6778
        store = 1;
6779
        break;
6780
    case OPC_SDXC1:
6781
        check_cop1x(ctx);
6782
        check_cp1_registers(ctx, fs);
6783
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6784
        tcg_gen_qemu_st64(fpu64_T[0], cpu_T[0], ctx->mem_idx);
6785
        opn = "sdxc1";
6786
        store = 1;
6787
        break;
6788
    case OPC_SUXC1:
6789
        check_cp1_64bitmode(ctx);
6790
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6791
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], ~0x7);
6792
        tcg_gen_qemu_st64(fpu64_T[0], cpu_T[0], ctx->mem_idx);
6793
        opn = "suxc1";
6794
        store = 1;
6795
        break;
6796
    default:
6797
        MIPS_INVAL(opn);
6798
        generate_exception(ctx, EXCP_RI);
6799
        return;
6800
    }
6801
    MIPS_DEBUG("%s %s, %s(%s)", opn, fregnames[store ? fs : fd],
6802
               regnames[index], regnames[base]);
6803
}
6804

    
6805
static void gen_flt3_arith (DisasContext *ctx, uint32_t opc,
6806
                            int fd, int fr, int fs, int ft)
6807
{
6808
    const char *opn = "flt3_arith";
6809

    
6810
    switch (opc) {
6811
    case OPC_ALNV_PS:
6812
        check_cp1_64bitmode(ctx);
6813
        gen_load_gpr(cpu_T[0], fr);
6814
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0x7);
6815
        gen_load_fpr32(fpu32_T[0], fs);
6816
        gen_load_fpr32h(fpu32h_T[0], fs);
6817
        gen_load_fpr32(fpu32_T[1], ft);
6818
        gen_load_fpr32h(fpu32h_T[1], ft);
6819
        {
6820
            int l1 = gen_new_label();
6821
            int l2 = gen_new_label();
6822

    
6823
            tcg_gen_brcondi_tl(TCG_COND_NE, cpu_T[0], 0, l1);
6824
            tcg_gen_mov_i32(fpu32_T[2], fpu32_T[0]);
6825
            tcg_gen_mov_i32(fpu32h_T[2], fpu32h_T[0]);
6826
            tcg_gen_br(l2);
6827
            gen_set_label(l1);
6828
            tcg_gen_brcondi_tl(TCG_COND_NE, cpu_T[0], 4, l2);
6829
#ifdef TARGET_WORDS_BIGENDIAN
6830
            tcg_gen_mov_i32(fpu32h_T[2], fpu32_T[0]);
6831
            tcg_gen_mov_i32(fpu32_T[2], fpu32h_T[1]);
6832
#else
6833
            tcg_gen_mov_i32(fpu32h_T[2], fpu32_T[1]);
6834
            tcg_gen_mov_i32(fpu32_T[2], fpu32h_T[0]);
6835
#endif
6836
            gen_set_label(l2);
6837
        }
6838
        gen_store_fpr32(fpu32_T[2], fd);
6839
        gen_store_fpr32h(fpu32h_T[2], fd);
6840
        opn = "alnv.ps";
6841
        break;
6842
    case OPC_MADD_S:
6843
        check_cop1x(ctx);
6844
        gen_load_fpr32(fpu32_T[0], fs);
6845
        gen_load_fpr32(fpu32_T[1], ft);
6846
        gen_load_fpr32(fpu32_T[2], fr);
6847
        tcg_gen_helper_0_0(do_float_muladd_s);
6848
        gen_store_fpr32(fpu32_T[2], fd);
6849
        opn = "madd.s";
6850
        break;
6851
    case OPC_MADD_D:
6852
        check_cop1x(ctx);
6853
        check_cp1_registers(ctx, fd | fs | ft | fr);
6854
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6855
        gen_load_fpr64(ctx, fpu64_T[1], ft);
6856
        gen_load_fpr64(ctx, fpu64_T[2], fr);
6857
        tcg_gen_helper_0_0(do_float_muladd_d);
6858
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6859
        opn = "madd.d";
6860
        break;
6861
    case OPC_MADD_PS:
6862
        check_cp1_64bitmode(ctx);
6863
        gen_load_fpr32(fpu32_T[0], fs);
6864
        gen_load_fpr32h(fpu32h_T[0], fs);
6865
        gen_load_fpr32(fpu32_T[1], ft);
6866
        gen_load_fpr32h(fpu32h_T[1], ft);
6867
        gen_load_fpr32(fpu32_T[2], fr);
6868
        gen_load_fpr32h(fpu32h_T[2], fr);
6869
        tcg_gen_helper_0_0(do_float_muladd_ps);
6870
        gen_store_fpr32(fpu32_T[2], fd);
6871
        gen_store_fpr32h(fpu32h_T[2], fd);
6872
        opn = "madd.ps";
6873
        break;
6874
    case OPC_MSUB_S:
6875
        check_cop1x(ctx);
6876
        gen_load_fpr32(fpu32_T[0], fs);
6877
        gen_load_fpr32(fpu32_T[1], ft);
6878
        gen_load_fpr32(fpu32_T[2], fr);
6879
        tcg_gen_helper_0_0(do_float_mulsub_s);
6880
        gen_store_fpr32(fpu32_T[2], fd);
6881
        opn = "msub.s";
6882
        break;
6883
    case OPC_MSUB_D:
6884
        check_cop1x(ctx);
6885
        check_cp1_registers(ctx, fd | fs | ft | fr);
6886
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6887
        gen_load_fpr64(ctx, fpu64_T[1], ft);
6888
        gen_load_fpr64(ctx, fpu64_T[2], fr);
6889
        tcg_gen_helper_0_0(do_float_mulsub_d);
6890
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6891
        opn = "msub.d";
6892
        break;
6893
    case OPC_MSUB_PS:
6894
        check_cp1_64bitmode(ctx);
6895
        gen_load_fpr32(fpu32_T[0], fs);
6896
        gen_load_fpr32h(fpu32h_T[0], fs);
6897
        gen_load_fpr32(fpu32_T[1], ft);
6898
        gen_load_fpr32h(fpu32h_T[1], ft);
6899
        gen_load_fpr32(fpu32_T[2], fr);
6900
        gen_load_fpr32h(fpu32h_T[2], fr);
6901
        tcg_gen_helper_0_0(do_float_mulsub_ps);
6902
        gen_store_fpr32(fpu32_T[2], fd);
6903
        gen_store_fpr32h(fpu32h_T[2], fd);
6904
        opn = "msub.ps";
6905
        break;
6906
    case OPC_NMADD_S:
6907
        check_cop1x(ctx);
6908
        gen_load_fpr32(fpu32_T[0], fs);
6909
        gen_load_fpr32(fpu32_T[1], ft);
6910
        gen_load_fpr32(fpu32_T[2], fr);
6911
        tcg_gen_helper_0_0(do_float_nmuladd_s);
6912
        gen_store_fpr32(fpu32_T[2], fd);
6913
        opn = "nmadd.s";
6914
        break;
6915
    case OPC_NMADD_D:
6916
        check_cop1x(ctx);
6917
        check_cp1_registers(ctx, fd | fs | ft | fr);
6918
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6919
        gen_load_fpr64(ctx, fpu64_T[1], ft);
6920
        gen_load_fpr64(ctx, fpu64_T[2], fr);
6921
        tcg_gen_helper_0_0(do_float_nmuladd_d);
6922
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6923
        opn = "nmadd.d";
6924
        break;
6925
    case OPC_NMADD_PS:
6926
        check_cp1_64bitmode(ctx);
6927
        gen_load_fpr32(fpu32_T[0], fs);
6928
        gen_load_fpr32h(fpu32h_T[0], fs);
6929
        gen_load_fpr32(fpu32_T[1], ft);
6930
        gen_load_fpr32h(fpu32h_T[1], ft);
6931
        gen_load_fpr32(fpu32_T[2], fr);
6932
        gen_load_fpr32h(fpu32h_T[2], fr);
6933
        tcg_gen_helper_0_0(do_float_nmuladd_ps);
6934
        gen_store_fpr32(fpu32_T[2], fd);
6935
        gen_store_fpr32h(fpu32h_T[2], fd);
6936
        opn = "nmadd.ps";
6937
        break;
6938
    case OPC_NMSUB_S:
6939
        check_cop1x(ctx);
6940
        gen_load_fpr32(fpu32_T[0], fs);
6941
        gen_load_fpr32(fpu32_T[1], ft);
6942
        gen_load_fpr32(fpu32_T[2], fr);
6943
        tcg_gen_helper_0_0(do_float_nmulsub_s);
6944
        gen_store_fpr32(fpu32_T[2], fd);
6945
        opn = "nmsub.s";
6946
        break;
6947
    case OPC_NMSUB_D:
6948
        check_cop1x(ctx);
6949
        check_cp1_registers(ctx, fd | fs | ft | fr);
6950
        gen_load_fpr64(ctx, fpu64_T[0], fs);
6951
        gen_load_fpr64(ctx, fpu64_T[1], ft);
6952
        gen_load_fpr64(ctx, fpu64_T[2], fr);
6953
        tcg_gen_helper_0_0(do_float_nmulsub_d);
6954
        gen_store_fpr64(ctx, fpu64_T[2], fd);
6955
        opn = "nmsub.d";
6956
        break;
6957
    case OPC_NMSUB_PS:
6958
        check_cp1_64bitmode(ctx);
6959
        gen_load_fpr32(fpu32_T[0], fs);
6960
        gen_load_fpr32h(fpu32h_T[0], fs);
6961
        gen_load_fpr32(fpu32_T[1], ft);
6962
        gen_load_fpr32h(fpu32h_T[1], ft);
6963
        gen_load_fpr32(fpu32_T[2], fr);
6964
        gen_load_fpr32h(fpu32h_T[2], fr);
6965
        tcg_gen_helper_0_0(do_float_nmulsub_ps);
6966
        gen_store_fpr32(fpu32_T[2], fd);
6967
        gen_store_fpr32h(fpu32h_T[2], fd);
6968
        opn = "nmsub.ps";
6969
        break;
6970
    default:
6971
        MIPS_INVAL(opn);
6972
        generate_exception (ctx, EXCP_RI);
6973
        return;
6974
    }
6975
    MIPS_DEBUG("%s %s, %s, %s, %s", opn, fregnames[fd], fregnames[fr],
6976
               fregnames[fs], fregnames[ft]);
6977
}
6978

    
6979
/* ISA extensions (ASEs) */
6980
/* MIPS16 extension to MIPS32 */
6981
/* SmartMIPS extension to MIPS32 */
6982

    
6983
#if defined(TARGET_MIPS64)
6984

    
6985
/* MDMX extension to MIPS64 */
6986

    
6987
#endif
6988

    
6989
static void decode_opc (CPUState *env, DisasContext *ctx)
6990
{
6991
    int32_t offset;
6992
    int rs, rt, rd, sa;
6993
    uint32_t op, op1, op2;
6994
    int16_t imm;
6995

    
6996
    /* make sure instructions are on a word boundary */
6997
    if (ctx->pc & 0x3) {
6998
        env->CP0_BadVAddr = ctx->pc;
6999
        generate_exception(ctx, EXCP_AdEL);
7000
        return;
7001
    }
7002

    
7003
    /* Handle blikely not taken case */
7004
    if ((ctx->hflags & MIPS_HFLAG_BMASK) == MIPS_HFLAG_BL) {
7005
        TCGv r_tmp = tcg_temp_local_new(TCG_TYPE_TL);
7006
        int l1 = gen_new_label();
7007

    
7008
        MIPS_DEBUG("blikely condition (" TARGET_FMT_lx ")", ctx->pc + 4);
7009
        tcg_gen_ld_tl(r_tmp, cpu_env, offsetof(CPUState, bcond));
7010
        tcg_gen_brcondi_tl(TCG_COND_NE, r_tmp, 0, l1);
7011
        tcg_temp_free(r_tmp);
7012
        {
7013
            TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I32);
7014

    
7015
            tcg_gen_movi_i32(r_tmp2, ctx->hflags & ~MIPS_HFLAG_BMASK);
7016
            tcg_gen_st_i32(r_tmp2, cpu_env, offsetof(CPUState, hflags));
7017
            tcg_temp_free(r_tmp2);
7018
        }
7019
        gen_goto_tb(ctx, 1, ctx->pc + 4);
7020
        gen_set_label(l1);
7021
    }
7022
    op = MASK_OP_MAJOR(ctx->opcode);
7023
    rs = (ctx->opcode >> 21) & 0x1f;
7024
    rt = (ctx->opcode >> 16) & 0x1f;
7025
    rd = (ctx->opcode >> 11) & 0x1f;
7026
    sa = (ctx->opcode >> 6) & 0x1f;
7027
    imm = (int16_t)ctx->opcode;
7028
    switch (op) {
7029
    case OPC_SPECIAL:
7030
        op1 = MASK_SPECIAL(ctx->opcode);
7031
        switch (op1) {
7032
        case OPC_SLL:          /* Arithmetic with immediate */
7033
        case OPC_SRL ... OPC_SRA:
7034
            gen_arith_imm(env, ctx, op1, rd, rt, sa);
7035
            break;
7036
        case OPC_MOVZ ... OPC_MOVN:
7037
            check_insn(env, ctx, ISA_MIPS4 | ISA_MIPS32);
7038
        case OPC_SLLV:         /* Arithmetic */
7039
        case OPC_SRLV ... OPC_SRAV:
7040
        case OPC_ADD ... OPC_NOR:
7041
        case OPC_SLT ... OPC_SLTU:
7042
            gen_arith(env, ctx, op1, rd, rs, rt);
7043
            break;
7044
        case OPC_MULT ... OPC_DIVU:
7045
            if (sa) {
7046
                check_insn(env, ctx, INSN_VR54XX);
7047
                op1 = MASK_MUL_VR54XX(ctx->opcode);
7048
                gen_mul_vr54xx(ctx, op1, rd, rs, rt);
7049
            } else
7050
                gen_muldiv(ctx, op1, rs, rt);
7051
            break;
7052
        case OPC_JR ... OPC_JALR:
7053
            gen_compute_branch(ctx, op1, rs, rd, sa);
7054
            return;
7055
        case OPC_TGE ... OPC_TEQ: /* Traps */
7056
        case OPC_TNE:
7057
            gen_trap(ctx, op1, rs, rt, -1);
7058
            break;
7059
        case OPC_MFHI:          /* Move from HI/LO */
7060
        case OPC_MFLO:
7061
            gen_HILO(ctx, op1, rd);
7062
            break;
7063
        case OPC_MTHI:
7064
        case OPC_MTLO:          /* Move to HI/LO */
7065
            gen_HILO(ctx, op1, rs);
7066
            break;
7067
        case OPC_PMON:          /* Pmon entry point, also R4010 selsl */
7068
#ifdef MIPS_STRICT_STANDARD
7069
            MIPS_INVAL("PMON / selsl");
7070
            generate_exception(ctx, EXCP_RI);
7071
#else
7072
            tcg_gen_helper_0_1i(do_pmon, sa);
7073
#endif
7074
            break;
7075
        case OPC_SYSCALL:
7076
            generate_exception(ctx, EXCP_SYSCALL);
7077
            break;
7078
        case OPC_BREAK:
7079
            generate_exception(ctx, EXCP_BREAK);
7080
            break;
7081
        case OPC_SPIM:
7082
#ifdef MIPS_STRICT_STANDARD
7083
            MIPS_INVAL("SPIM");
7084
            generate_exception(ctx, EXCP_RI);
7085
#else
7086
           /* Implemented as RI exception for now. */
7087
            MIPS_INVAL("spim (unofficial)");
7088
            generate_exception(ctx, EXCP_RI);
7089
#endif
7090
            break;
7091
        case OPC_SYNC:
7092
            /* Treat as NOP. */
7093
            break;
7094

    
7095
        case OPC_MOVCI:
7096
            check_insn(env, ctx, ISA_MIPS4 | ISA_MIPS32);
7097
            if (env->CP0_Config1 & (1 << CP0C1_FP)) {
7098
                save_cpu_state(ctx, 1);
7099
                check_cp1_enabled(ctx);
7100
                gen_movci(ctx, rd, rs, (ctx->opcode >> 18) & 0x7,
7101
                          (ctx->opcode >> 16) & 1);
7102
            } else {
7103
                generate_exception_err(ctx, EXCP_CpU, 1);
7104
            }
7105
            break;
7106

    
7107
#if defined(TARGET_MIPS64)
7108
       /* MIPS64 specific opcodes */
7109
        case OPC_DSLL:
7110
        case OPC_DSRL ... OPC_DSRA:
7111
        case OPC_DSLL32:
7112
        case OPC_DSRL32 ... OPC_DSRA32:
7113
            check_insn(env, ctx, ISA_MIPS3);
7114
            check_mips_64(ctx);
7115
            gen_arith_imm(env, ctx, op1, rd, rt, sa);
7116
            break;
7117
        case OPC_DSLLV:
7118
        case OPC_DSRLV ... OPC_DSRAV:
7119
        case OPC_DADD ... OPC_DSUBU:
7120
            check_insn(env, ctx, ISA_MIPS3);
7121
            check_mips_64(ctx);
7122
            gen_arith(env, ctx, op1, rd, rs, rt);
7123
            break;
7124
        case OPC_DMULT ... OPC_DDIVU:
7125
            check_insn(env, ctx, ISA_MIPS3);
7126
            check_mips_64(ctx);
7127
            gen_muldiv(ctx, op1, rs, rt);
7128
            break;
7129
#endif
7130
        default:            /* Invalid */
7131
            MIPS_INVAL("special");
7132
            generate_exception(ctx, EXCP_RI);
7133
            break;
7134
        }
7135
        break;
7136
    case OPC_SPECIAL2:
7137
        op1 = MASK_SPECIAL2(ctx->opcode);
7138
        switch (op1) {
7139
        case OPC_MADD ... OPC_MADDU: /* Multiply and add/sub */
7140
        case OPC_MSUB ... OPC_MSUBU:
7141
            check_insn(env, ctx, ISA_MIPS32);
7142
            gen_muldiv(ctx, op1, rs, rt);
7143
            break;
7144
        case OPC_MUL:
7145
            gen_arith(env, ctx, op1, rd, rs, rt);
7146
            break;
7147
        case OPC_CLZ ... OPC_CLO:
7148
            check_insn(env, ctx, ISA_MIPS32);
7149
            gen_cl(ctx, op1, rd, rs);
7150
            break;
7151
        case OPC_SDBBP:
7152
            /* XXX: not clear which exception should be raised
7153
             *      when in debug mode...
7154
             */
7155
            check_insn(env, ctx, ISA_MIPS32);
7156
            if (!(ctx->hflags & MIPS_HFLAG_DM)) {
7157
                generate_exception(ctx, EXCP_DBp);
7158
            } else {
7159
                generate_exception(ctx, EXCP_DBp);
7160
            }
7161
            /* Treat as NOP. */
7162
            break;
7163
#if defined(TARGET_MIPS64)
7164
        case OPC_DCLZ ... OPC_DCLO:
7165
            check_insn(env, ctx, ISA_MIPS64);
7166
            check_mips_64(ctx);
7167
            gen_cl(ctx, op1, rd, rs);
7168
            break;
7169
#endif
7170
        default:            /* Invalid */
7171
            MIPS_INVAL("special2");
7172
            generate_exception(ctx, EXCP_RI);
7173
            break;
7174
        }
7175
        break;
7176
    case OPC_SPECIAL3:
7177
        op1 = MASK_SPECIAL3(ctx->opcode);
7178
        switch (op1) {
7179
        case OPC_EXT:
7180
        case OPC_INS:
7181
            check_insn(env, ctx, ISA_MIPS32R2);
7182
            gen_bitops(ctx, op1, rt, rs, sa, rd);
7183
            break;
7184
        case OPC_BSHFL:
7185
            check_insn(env, ctx, ISA_MIPS32R2);
7186
            op2 = MASK_BSHFL(ctx->opcode);
7187
            switch (op2) {
7188
            case OPC_WSBH:
7189
                gen_load_gpr(cpu_T[1], rt);
7190
                tcg_gen_helper_0_0(do_wsbh);
7191
                break;
7192
            case OPC_SEB:
7193
                gen_load_gpr(cpu_T[1], rt);
7194
                tcg_gen_ext8s_tl(cpu_T[0], cpu_T[1]);
7195
                break;
7196
            case OPC_SEH:
7197
                gen_load_gpr(cpu_T[1], rt);
7198
                tcg_gen_ext16s_tl(cpu_T[0], cpu_T[1]);
7199
                break;
7200
            default:            /* Invalid */
7201
                MIPS_INVAL("bshfl");
7202
                generate_exception(ctx, EXCP_RI);
7203
                break;
7204
            }
7205
            gen_store_gpr(cpu_T[0], rd);
7206
            break;
7207
        case OPC_RDHWR:
7208
            check_insn(env, ctx, ISA_MIPS32R2);
7209
            switch (rd) {
7210
            case 0:
7211
                save_cpu_state(ctx, 1);
7212
                tcg_gen_helper_0_0(do_rdhwr_cpunum);
7213
                break;
7214
            case 1:
7215
                save_cpu_state(ctx, 1);
7216
                tcg_gen_helper_0_0(do_rdhwr_synci_step);
7217
                break;
7218
            case 2:
7219
                save_cpu_state(ctx, 1);
7220
                tcg_gen_helper_0_0(do_rdhwr_cc);
7221
                break;
7222
            case 3:
7223
                save_cpu_state(ctx, 1);
7224
                tcg_gen_helper_0_0(do_rdhwr_ccres);
7225
                break;
7226
            case 29:
7227
#if defined (CONFIG_USER_ONLY)
7228
                tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUState, tls_value));
7229
                break;
7230
#else
7231
                /* XXX: Some CPUs implement this in hardware. Not supported yet. */
7232
#endif
7233
            default:            /* Invalid */
7234
                MIPS_INVAL("rdhwr");
7235
                generate_exception(ctx, EXCP_RI);
7236
                break;
7237
            }
7238
            gen_store_gpr(cpu_T[0], rt);
7239
            break;
7240
        case OPC_FORK:
7241
            check_insn(env, ctx, ASE_MT);
7242
            gen_load_gpr(cpu_T[0], rt);
7243
            gen_load_gpr(cpu_T[1], rs);
7244
            tcg_gen_helper_0_0(do_fork);
7245
            break;
7246
        case OPC_YIELD:
7247
            check_insn(env, ctx, ASE_MT);
7248
            gen_load_gpr(cpu_T[0], rs);
7249
            tcg_gen_helper_0_0(do_yield);
7250
            gen_store_gpr(cpu_T[0], rd);
7251
            break;
7252
#if defined(TARGET_MIPS64)
7253
        case OPC_DEXTM ... OPC_DEXT:
7254
        case OPC_DINSM ... OPC_DINS:
7255
            check_insn(env, ctx, ISA_MIPS64R2);
7256
            check_mips_64(ctx);
7257
            gen_bitops(ctx, op1, rt, rs, sa, rd);
7258
            break;
7259
        case OPC_DBSHFL:
7260
            check_insn(env, ctx, ISA_MIPS64R2);
7261
            check_mips_64(ctx);
7262
            op2 = MASK_DBSHFL(ctx->opcode);
7263
            switch (op2) {
7264
            case OPC_DSBH:
7265
                gen_load_gpr(cpu_T[1], rt);
7266
                tcg_gen_helper_0_0(do_dsbh);
7267
                break;
7268
            case OPC_DSHD:
7269
                gen_load_gpr(cpu_T[1], rt);
7270
                tcg_gen_helper_0_0(do_dshd);
7271
                break;
7272
            default:            /* Invalid */
7273
                MIPS_INVAL("dbshfl");
7274
                generate_exception(ctx, EXCP_RI);
7275
                break;
7276
            }
7277
            gen_store_gpr(cpu_T[0], rd);
7278
            break;
7279
#endif
7280
        default:            /* Invalid */
7281
            MIPS_INVAL("special3");
7282
            generate_exception(ctx, EXCP_RI);
7283
            break;
7284
        }
7285
        break;
7286
    case OPC_REGIMM:
7287
        op1 = MASK_REGIMM(ctx->opcode);
7288
        switch (op1) {
7289
        case OPC_BLTZ ... OPC_BGEZL: /* REGIMM branches */
7290
        case OPC_BLTZAL ... OPC_BGEZALL:
7291
            gen_compute_branch(ctx, op1, rs, -1, imm << 2);
7292
            return;
7293
        case OPC_TGEI ... OPC_TEQI: /* REGIMM traps */
7294
        case OPC_TNEI:
7295
            gen_trap(ctx, op1, rs, -1, imm);
7296
            break;
7297
        case OPC_SYNCI:
7298
            check_insn(env, ctx, ISA_MIPS32R2);
7299
            /* Treat as NOP. */
7300
            break;
7301
        default:            /* Invalid */
7302
            MIPS_INVAL("regimm");
7303
            generate_exception(ctx, EXCP_RI);
7304
            break;
7305
        }
7306
        break;
7307
    case OPC_CP0:
7308
        check_cp0_enabled(ctx);
7309
        op1 = MASK_CP0(ctx->opcode);
7310
        switch (op1) {
7311
        case OPC_MFC0:
7312
        case OPC_MTC0:
7313
        case OPC_MFTR:
7314
        case OPC_MTTR:
7315
#if defined(TARGET_MIPS64)
7316
        case OPC_DMFC0:
7317
        case OPC_DMTC0:
7318
#endif
7319
#ifndef CONFIG_USER_ONLY
7320
            gen_cp0(env, ctx, op1, rt, rd);
7321
#endif
7322
            break;
7323
        case OPC_C0_FIRST ... OPC_C0_LAST:
7324
#ifndef CONFIG_USER_ONLY
7325
            gen_cp0(env, ctx, MASK_C0(ctx->opcode), rt, rd);
7326
#endif
7327
            break;
7328
        case OPC_MFMC0:
7329
            op2 = MASK_MFMC0(ctx->opcode);
7330
            switch (op2) {
7331
            case OPC_DMT:
7332
                check_insn(env, ctx, ASE_MT);
7333
                tcg_gen_helper_0_0(do_dmt);
7334
                break;
7335
            case OPC_EMT:
7336
                check_insn(env, ctx, ASE_MT);
7337
                tcg_gen_helper_0_0(do_emt);
7338
                break;
7339
            case OPC_DVPE:
7340
                check_insn(env, ctx, ASE_MT);
7341
                tcg_gen_helper_0_0(do_dvpe);
7342
                break;
7343
            case OPC_EVPE:
7344
                check_insn(env, ctx, ASE_MT);
7345
                tcg_gen_helper_0_0(do_evpe);
7346
                break;
7347
            case OPC_DI:
7348
                check_insn(env, ctx, ISA_MIPS32R2);
7349
                save_cpu_state(ctx, 1);
7350
                tcg_gen_helper_0_0(do_di);
7351
                /* Stop translation as we may have switched the execution mode */
7352
                ctx->bstate = BS_STOP;
7353
                break;
7354
            case OPC_EI:
7355
                check_insn(env, ctx, ISA_MIPS32R2);
7356
                save_cpu_state(ctx, 1);
7357
                tcg_gen_helper_0_0(do_ei);
7358
                /* Stop translation as we may have switched the execution mode */
7359
                ctx->bstate = BS_STOP;
7360
                break;
7361
            default:            /* Invalid */
7362
                MIPS_INVAL("mfmc0");
7363
                generate_exception(ctx, EXCP_RI);
7364
                break;
7365
            }
7366
            gen_store_gpr(cpu_T[0], rt);
7367
            break;
7368
        case OPC_RDPGPR:
7369
            check_insn(env, ctx, ISA_MIPS32R2);
7370
            gen_load_srsgpr(cpu_T[0], rt);
7371
            gen_store_gpr(cpu_T[0], rd);
7372
            break;
7373
        case OPC_WRPGPR:
7374
            check_insn(env, ctx, ISA_MIPS32R2);
7375
            gen_load_gpr(cpu_T[0], rt);
7376
            gen_store_srsgpr(cpu_T[0], rd);
7377
            break;
7378
        default:
7379
            MIPS_INVAL("cp0");
7380
            generate_exception(ctx, EXCP_RI);
7381
            break;
7382
        }
7383
        break;
7384
    case OPC_ADDI ... OPC_LUI: /* Arithmetic with immediate opcode */
7385
         gen_arith_imm(env, ctx, op, rt, rs, imm);
7386
         break;
7387
    case OPC_J ... OPC_JAL: /* Jump */
7388
         offset = (int32_t)(ctx->opcode & 0x3FFFFFF) << 2;
7389
         gen_compute_branch(ctx, op, rs, rt, offset);
7390
         return;
7391
    case OPC_BEQ ... OPC_BGTZ: /* Branch */
7392
    case OPC_BEQL ... OPC_BGTZL:
7393
         gen_compute_branch(ctx, op, rs, rt, imm << 2);
7394
         return;
7395
    case OPC_LB ... OPC_LWR: /* Load and stores */
7396
    case OPC_SB ... OPC_SW:
7397
    case OPC_SWR:
7398
    case OPC_LL:
7399
    case OPC_SC:
7400
         gen_ldst(ctx, op, rt, rs, imm);
7401
         break;
7402
    case OPC_CACHE:
7403
        check_insn(env, ctx, ISA_MIPS3 | ISA_MIPS32);
7404
        /* Treat as NOP. */
7405
        break;
7406
    case OPC_PREF:
7407
        check_insn(env, ctx, ISA_MIPS4 | ISA_MIPS32);
7408
        /* Treat as NOP. */
7409
        break;
7410

    
7411
    /* Floating point (COP1). */
7412
    case OPC_LWC1:
7413
    case OPC_LDC1:
7414
    case OPC_SWC1:
7415
    case OPC_SDC1:
7416
        if (env->CP0_Config1 & (1 << CP0C1_FP)) {
7417
            save_cpu_state(ctx, 1);
7418
            check_cp1_enabled(ctx);
7419
            gen_flt_ldst(ctx, op, rt, rs, imm);
7420
        } else {
7421
            generate_exception_err(ctx, EXCP_CpU, 1);
7422
        }
7423
        break;
7424

    
7425
    case OPC_CP1:
7426
        if (env->CP0_Config1 & (1 << CP0C1_FP)) {
7427
            save_cpu_state(ctx, 1);
7428
            check_cp1_enabled(ctx);
7429
            op1 = MASK_CP1(ctx->opcode);
7430
            switch (op1) {
7431
            case OPC_MFHC1:
7432
            case OPC_MTHC1:
7433
                check_insn(env, ctx, ISA_MIPS32R2);
7434
            case OPC_MFC1:
7435
            case OPC_CFC1:
7436
            case OPC_MTC1:
7437
            case OPC_CTC1:
7438
                gen_cp1(ctx, op1, rt, rd);
7439
                break;
7440
#if defined(TARGET_MIPS64)
7441
            case OPC_DMFC1:
7442
            case OPC_DMTC1:
7443
                check_insn(env, ctx, ISA_MIPS3);
7444
                gen_cp1(ctx, op1, rt, rd);
7445
                break;
7446
#endif
7447
            case OPC_BC1ANY2:
7448
            case OPC_BC1ANY4:
7449
                check_cop1x(ctx);
7450
                check_insn(env, ctx, ASE_MIPS3D);
7451
                /* fall through */
7452
            case OPC_BC1:
7453
                gen_compute_branch1(env, ctx, MASK_BC1(ctx->opcode),
7454
                                    (rt >> 2) & 0x7, imm << 2);
7455
                return;
7456
            case OPC_S_FMT:
7457
            case OPC_D_FMT:
7458
            case OPC_W_FMT:
7459
            case OPC_L_FMT:
7460
            case OPC_PS_FMT:
7461
                gen_farith(ctx, MASK_CP1_FUNC(ctx->opcode), rt, rd, sa,
7462
                           (imm >> 8) & 0x7);
7463
                break;
7464
            default:
7465
                MIPS_INVAL("cp1");
7466
                generate_exception (ctx, EXCP_RI);
7467
                break;
7468
            }
7469
        } else {
7470
            generate_exception_err(ctx, EXCP_CpU, 1);
7471
        }
7472
        break;
7473

    
7474
    /* COP2.  */
7475
    case OPC_LWC2:
7476
    case OPC_LDC2:
7477
    case OPC_SWC2:
7478
    case OPC_SDC2:
7479
    case OPC_CP2:
7480
        /* COP2: Not implemented. */
7481
        generate_exception_err(ctx, EXCP_CpU, 2);
7482
        break;
7483

    
7484
    case OPC_CP3:
7485
        if (env->CP0_Config1 & (1 << CP0C1_FP)) {
7486
            save_cpu_state(ctx, 1);
7487
            check_cp1_enabled(ctx);
7488
            op1 = MASK_CP3(ctx->opcode);
7489
            switch (op1) {
7490
            case OPC_LWXC1:
7491
            case OPC_LDXC1:
7492
            case OPC_LUXC1:
7493
            case OPC_SWXC1:
7494
            case OPC_SDXC1:
7495
            case OPC_SUXC1:
7496
                gen_flt3_ldst(ctx, op1, sa, rd, rs, rt);
7497
                break;
7498
            case OPC_PREFX:
7499
                /* Treat as NOP. */
7500
                break;
7501
            case OPC_ALNV_PS:
7502
            case OPC_MADD_S:
7503
            case OPC_MADD_D:
7504
            case OPC_MADD_PS:
7505
            case OPC_MSUB_S:
7506
            case OPC_MSUB_D:
7507
            case OPC_MSUB_PS:
7508
            case OPC_NMADD_S:
7509
            case OPC_NMADD_D:
7510
            case OPC_NMADD_PS:
7511
            case OPC_NMSUB_S:
7512
            case OPC_NMSUB_D:
7513
            case OPC_NMSUB_PS:
7514
                gen_flt3_arith(ctx, op1, sa, rs, rd, rt);
7515
                break;
7516
            default:
7517
                MIPS_INVAL("cp3");
7518
                generate_exception (ctx, EXCP_RI);
7519
                break;
7520
            }
7521
        } else {
7522
            generate_exception_err(ctx, EXCP_CpU, 1);
7523
        }
7524
        break;
7525

    
7526
#if defined(TARGET_MIPS64)
7527
    /* MIPS64 opcodes */
7528
    case OPC_LWU:
7529
    case OPC_LDL ... OPC_LDR:
7530
    case OPC_SDL ... OPC_SDR:
7531
    case OPC_LLD:
7532
    case OPC_LD:
7533
    case OPC_SCD:
7534
    case OPC_SD:
7535
        check_insn(env, ctx, ISA_MIPS3);
7536
        check_mips_64(ctx);
7537
        gen_ldst(ctx, op, rt, rs, imm);
7538
        break;
7539
    case OPC_DADDI ... OPC_DADDIU:
7540
        check_insn(env, ctx, ISA_MIPS3);
7541
        check_mips_64(ctx);
7542
        gen_arith_imm(env, ctx, op, rt, rs, imm);
7543
        break;
7544
#endif
7545
    case OPC_JALX:
7546
        check_insn(env, ctx, ASE_MIPS16);
7547
        /* MIPS16: Not implemented. */
7548
    case OPC_MDMX:
7549
        check_insn(env, ctx, ASE_MDMX);
7550
        /* MDMX: Not implemented. */
7551
    default:            /* Invalid */
7552
        MIPS_INVAL("major opcode");
7553
        generate_exception(ctx, EXCP_RI);
7554
        break;
7555
    }
7556
    if (ctx->hflags & MIPS_HFLAG_BMASK) {
7557
        int hflags = ctx->hflags & MIPS_HFLAG_BMASK;
7558
        /* Branches completion */
7559
        ctx->hflags &= ~MIPS_HFLAG_BMASK;
7560
        ctx->bstate = BS_BRANCH;
7561
        save_cpu_state(ctx, 0);
7562
        switch (hflags) {
7563
        case MIPS_HFLAG_B:
7564
            /* unconditional branch */
7565
            MIPS_DEBUG("unconditional branch");
7566
            gen_goto_tb(ctx, 0, ctx->btarget);
7567
            break;
7568
        case MIPS_HFLAG_BL:
7569
            /* blikely taken case */
7570
            MIPS_DEBUG("blikely branch taken");
7571
            gen_goto_tb(ctx, 0, ctx->btarget);
7572
            break;
7573
        case MIPS_HFLAG_BC:
7574
            /* Conditional branch */
7575
            MIPS_DEBUG("conditional branch");
7576
            {
7577
                TCGv r_tmp = tcg_temp_local_new(TCG_TYPE_TL);
7578
                int l1 = gen_new_label();
7579

    
7580
                tcg_gen_ld_tl(r_tmp, cpu_env, offsetof(CPUState, bcond));
7581
                tcg_gen_brcondi_tl(TCG_COND_NE, r_tmp, 0, l1);
7582
                tcg_temp_free(r_tmp);
7583
                gen_goto_tb(ctx, 1, ctx->pc + 4);
7584
                gen_set_label(l1);
7585
                gen_goto_tb(ctx, 0, ctx->btarget);
7586
            }
7587
            break;
7588
        case MIPS_HFLAG_BR:
7589
            /* unconditional branch to register */
7590
            MIPS_DEBUG("branch to register");
7591
            gen_breg_pc();
7592
            tcg_gen_exit_tb(0);
7593
            break;
7594
        default:
7595
            MIPS_DEBUG("unknown branch");
7596
            break;
7597
        }
7598
    }
7599
}
7600

    
7601
static always_inline int
7602
gen_intermediate_code_internal (CPUState *env, TranslationBlock *tb,
7603
                                int search_pc)
7604
{
7605
    DisasContext ctx;
7606
    target_ulong pc_start;
7607
    uint16_t *gen_opc_end;
7608
    int j, lj = -1;
7609

    
7610
    if (search_pc && loglevel)
7611
        fprintf (logfile, "search pc %d\n", search_pc);
7612

    
7613
    pc_start = tb->pc;
7614
    /* Leave some spare opc slots for branch handling. */
7615
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE - 16;
7616
    ctx.pc = pc_start;
7617
    ctx.saved_pc = -1;
7618
    ctx.tb = tb;
7619
    ctx.bstate = BS_NONE;
7620
    /* Restore delay slot state from the tb context.  */
7621
    ctx.hflags = (uint32_t)tb->flags; /* FIXME: maybe use 64 bits here? */
7622
    restore_cpu_state(env, &ctx);
7623
#if defined(CONFIG_USER_ONLY)
7624
    ctx.mem_idx = MIPS_HFLAG_UM;
7625
#else
7626
    ctx.mem_idx = ctx.hflags & MIPS_HFLAG_KSU;
7627
#endif
7628
#ifdef DEBUG_DISAS
7629
    if (loglevel & CPU_LOG_TB_CPU) {
7630
        fprintf(logfile, "------------------------------------------------\n");
7631
        /* FIXME: This may print out stale hflags from env... */
7632
        cpu_dump_state(env, logfile, fprintf, 0);
7633
    }
7634
#endif
7635
#ifdef MIPS_DEBUG_DISAS
7636
    if (loglevel & CPU_LOG_TB_IN_ASM)
7637
        fprintf(logfile, "\ntb %p idx %d hflags %04x\n",
7638
                tb, ctx.mem_idx, ctx.hflags);
7639
#endif
7640
    while (ctx.bstate == BS_NONE) {
7641
        if (env->nb_breakpoints > 0) {
7642
            for(j = 0; j < env->nb_breakpoints; j++) {
7643
                if (env->breakpoints[j] == ctx.pc) {
7644
                    save_cpu_state(&ctx, 1);
7645
                    ctx.bstate = BS_BRANCH;
7646
                    tcg_gen_helper_0_1i(do_raise_exception, EXCP_DEBUG);
7647
                    /* Include the breakpoint location or the tb won't
7648
                     * be flushed when it must be.  */
7649
                    ctx.pc += 4;
7650
                    goto done_generating;
7651
                }
7652
            }
7653
        }
7654

    
7655
        if (search_pc) {
7656
            j = gen_opc_ptr - gen_opc_buf;
7657
            if (lj < j) {
7658
                lj++;
7659
                while (lj < j)
7660
                    gen_opc_instr_start[lj++] = 0;
7661
            }
7662
            gen_opc_pc[lj] = ctx.pc;
7663
            gen_opc_hflags[lj] = ctx.hflags & MIPS_HFLAG_BMASK;
7664
            gen_opc_instr_start[lj] = 1;
7665
        }
7666
        ctx.opcode = ldl_code(ctx.pc);
7667
        decode_opc(env, &ctx);
7668
        ctx.pc += 4;
7669

    
7670
        if (env->singlestep_enabled)
7671
            break;
7672

    
7673
        if ((ctx.pc & (TARGET_PAGE_SIZE - 1)) == 0)
7674
            break;
7675

    
7676
        if (gen_opc_ptr >= gen_opc_end)
7677
            break;
7678

    
7679
        if (gen_opc_ptr >= gen_opc_end)
7680
            break;
7681

    
7682
#if defined (MIPS_SINGLE_STEP)
7683
        break;
7684
#endif
7685
    }
7686
    if (env->singlestep_enabled) {
7687
        save_cpu_state(&ctx, ctx.bstate == BS_NONE);
7688
        tcg_gen_helper_0_1i(do_raise_exception, EXCP_DEBUG);
7689
    } else {
7690
        switch (ctx.bstate) {
7691
        case BS_STOP:
7692
            tcg_gen_helper_0_0(do_interrupt_restart);
7693
            gen_goto_tb(&ctx, 0, ctx.pc);
7694
            break;
7695
        case BS_NONE:
7696
            save_cpu_state(&ctx, 0);
7697
            gen_goto_tb(&ctx, 0, ctx.pc);
7698
            break;
7699
        case BS_EXCP:
7700
            tcg_gen_helper_0_0(do_interrupt_restart);
7701
            tcg_gen_exit_tb(0);
7702
            break;
7703
        case BS_BRANCH:
7704
        default:
7705
            break;
7706
        }
7707
    }
7708
done_generating:
7709
    *gen_opc_ptr = INDEX_op_end;
7710
    if (search_pc) {
7711
        j = gen_opc_ptr - gen_opc_buf;
7712
        lj++;
7713
        while (lj <= j)
7714
            gen_opc_instr_start[lj++] = 0;
7715
    } else {
7716
        tb->size = ctx.pc - pc_start;
7717
    }
7718
#ifdef DEBUG_DISAS
7719
#if defined MIPS_DEBUG_DISAS
7720
    if (loglevel & CPU_LOG_TB_IN_ASM)
7721
        fprintf(logfile, "\n");
7722
#endif
7723
    if (loglevel & CPU_LOG_TB_IN_ASM) {
7724
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
7725
        target_disas(logfile, pc_start, ctx.pc - pc_start, 0);
7726
        fprintf(logfile, "\n");
7727
    }
7728
    if (loglevel & CPU_LOG_TB_CPU) {
7729
        fprintf(logfile, "---------------- %d %08x\n", ctx.bstate, ctx.hflags);
7730
    }
7731
#endif
7732

    
7733
    return 0;
7734
}
7735

    
7736
int gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
7737
{
7738
    return gen_intermediate_code_internal(env, tb, 0);
7739
}
7740

    
7741
int gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
7742
{
7743
    return gen_intermediate_code_internal(env, tb, 1);
7744
}
7745

    
7746
void fpu_dump_state(CPUState *env, FILE *f,
7747
                    int (*fpu_fprintf)(FILE *f, const char *fmt, ...),
7748
                    int flags)
7749
{
7750
    int i;
7751
    int is_fpu64 = !!(env->hflags & MIPS_HFLAG_F64);
7752

    
7753
#define printfpr(fp)                                                        \
7754
    do {                                                                    \
7755
        if (is_fpu64)                                                       \
7756
            fpu_fprintf(f, "w:%08x d:%016lx fd:%13g fs:%13g psu: %13g\n",   \
7757
                        (fp)->w[FP_ENDIAN_IDX], (fp)->d, (fp)->fd,          \
7758
                        (fp)->fs[FP_ENDIAN_IDX], (fp)->fs[!FP_ENDIAN_IDX]); \
7759
        else {                                                              \
7760
            fpr_t tmp;                                                      \
7761
            tmp.w[FP_ENDIAN_IDX] = (fp)->w[FP_ENDIAN_IDX];                  \
7762
            tmp.w[!FP_ENDIAN_IDX] = ((fp) + 1)->w[FP_ENDIAN_IDX];           \
7763
            fpu_fprintf(f, "w:%08x d:%016lx fd:%13g fs:%13g psu:%13g\n",    \
7764
                        tmp.w[FP_ENDIAN_IDX], tmp.d, tmp.fd,                \
7765
                        tmp.fs[FP_ENDIAN_IDX], tmp.fs[!FP_ENDIAN_IDX]);     \
7766
        }                                                                   \
7767
    } while(0)
7768

    
7769

    
7770
    fpu_fprintf(f, "CP1 FCR0 0x%08x  FCR31 0x%08x  SR.FR %d  fp_status 0x%08x(0x%02x)\n",
7771
                env->fpu->fcr0, env->fpu->fcr31, is_fpu64, env->fpu->fp_status,
7772
                get_float_exception_flags(&env->fpu->fp_status));
7773
    fpu_fprintf(f, "FT0: "); printfpr(&env->ft0);
7774
    fpu_fprintf(f, "FT1: "); printfpr(&env->ft1);
7775
    fpu_fprintf(f, "FT2: "); printfpr(&env->ft2);
7776
    for (i = 0; i < 32; (is_fpu64) ? i++ : (i += 2)) {
7777
        fpu_fprintf(f, "%3s: ", fregnames[i]);
7778
        printfpr(&env->fpu->fpr[i]);
7779
    }
7780

    
7781
#undef printfpr
7782
}
7783

    
7784
void dump_fpu (CPUState *env)
7785
{
7786
    if (loglevel) {
7787
        fprintf(logfile,
7788
                "pc=0x" TARGET_FMT_lx " HI=0x" TARGET_FMT_lx
7789
                " LO=0x" TARGET_FMT_lx " ds %04x " TARGET_FMT_lx
7790
                " %04x\n",
7791
                env->PC[env->current_tc], env->HI[env->current_tc][0],
7792
                env->LO[env->current_tc][0], env->hflags, env->btarget,
7793
                env->bcond);
7794
       fpu_dump_state(env, logfile, fprintf, 0);
7795
    }
7796
}
7797

    
7798
#if defined(TARGET_MIPS64) && defined(MIPS_DEBUG_SIGN_EXTENSIONS)
7799
/* Debug help: The architecture requires 32bit code to maintain proper
7800
   sign-extened values on 64bit machines.  */
7801

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

    
7804
void cpu_mips_check_sign_extensions (CPUState *env, FILE *f,
7805
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
7806
                     int flags)
7807
{
7808
    int i;
7809

    
7810
    if (!SIGN_EXT_P(env->PC[env->current_tc]))
7811
        cpu_fprintf(f, "BROKEN: pc=0x" TARGET_FMT_lx "\n", env->PC[env->current_tc]);
7812
    if (!SIGN_EXT_P(env->HI[env->current_tc][0]))
7813
        cpu_fprintf(f, "BROKEN: HI=0x" TARGET_FMT_lx "\n", env->HI[env->current_tc][0]);
7814
    if (!SIGN_EXT_P(env->LO[env->current_tc][0]))
7815
        cpu_fprintf(f, "BROKEN: LO=0x" TARGET_FMT_lx "\n", env->LO[env->current_tc][0]);
7816
    if (!SIGN_EXT_P(env->btarget))
7817
        cpu_fprintf(f, "BROKEN: btarget=0x" TARGET_FMT_lx "\n", env->btarget);
7818

    
7819
    for (i = 0; i < 32; i++) {
7820
        if (!SIGN_EXT_P(env->gpr[env->current_tc][i]))
7821
            cpu_fprintf(f, "BROKEN: %s=0x" TARGET_FMT_lx "\n", regnames[i], env->gpr[env->current_tc][i]);
7822
    }
7823

    
7824
    if (!SIGN_EXT_P(env->CP0_EPC))
7825
        cpu_fprintf(f, "BROKEN: EPC=0x" TARGET_FMT_lx "\n", env->CP0_EPC);
7826
    if (!SIGN_EXT_P(env->CP0_LLAddr))
7827
        cpu_fprintf(f, "BROKEN: LLAddr=0x" TARGET_FMT_lx "\n", env->CP0_LLAddr);
7828
}
7829
#endif
7830

    
7831
void cpu_dump_state (CPUState *env, FILE *f,
7832
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
7833
                     int flags)
7834
{
7835
    int i;
7836

    
7837
    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",
7838
                env->PC[env->current_tc], env->HI[env->current_tc], env->LO[env->current_tc], env->hflags, env->btarget, env->bcond);
7839
    for (i = 0; i < 32; i++) {
7840
        if ((i & 3) == 0)
7841
            cpu_fprintf(f, "GPR%02d:", i);
7842
        cpu_fprintf(f, " %s " TARGET_FMT_lx, regnames[i], env->gpr[env->current_tc][i]);
7843
        if ((i & 3) == 3)
7844
            cpu_fprintf(f, "\n");
7845
    }
7846

    
7847
    cpu_fprintf(f, "CP0 Status  0x%08x Cause   0x%08x EPC    0x" TARGET_FMT_lx "\n",
7848
                env->CP0_Status, env->CP0_Cause, env->CP0_EPC);
7849
    cpu_fprintf(f, "    Config0 0x%08x Config1 0x%08x LLAddr 0x" TARGET_FMT_lx "\n",
7850
                env->CP0_Config0, env->CP0_Config1, env->CP0_LLAddr);
7851
    if (env->hflags & MIPS_HFLAG_FPU)
7852
        fpu_dump_state(env, f, cpu_fprintf, flags);
7853
#if defined(TARGET_MIPS64) && defined(MIPS_DEBUG_SIGN_EXTENSIONS)
7854
    cpu_mips_check_sign_extensions(env, f, cpu_fprintf, flags);
7855
#endif
7856
}
7857

    
7858
static void mips_tcg_init(void)
7859
{
7860
    static int inited;
7861

    
7862
    /* Initialize various static tables. */
7863
    if (inited)
7864
        return;
7865

    
7866
    cpu_env = tcg_global_reg_new(TCG_TYPE_PTR, TCG_AREG0, "env");
7867
    current_tc_gprs = tcg_global_mem_new(TCG_TYPE_PTR,
7868
                                         TCG_AREG0,
7869
                                         offsetof(CPUState, current_tc_gprs),
7870
                                         "current_tc_gprs");
7871
    current_tc_hi = tcg_global_mem_new(TCG_TYPE_PTR,
7872
                                       TCG_AREG0,
7873
                                       offsetof(CPUState, current_tc_hi),
7874
                                       "current_tc_hi");
7875
    current_fpu = tcg_global_mem_new(TCG_TYPE_PTR,
7876
                                     TCG_AREG0,
7877
                                     offsetof(CPUState, fpu),
7878
                                     "current_fpu");
7879
#if TARGET_LONG_BITS > HOST_LONG_BITS
7880
    cpu_T[0] = tcg_global_mem_new(TCG_TYPE_TL,
7881
                                  TCG_AREG0, offsetof(CPUState, t0), "T0");
7882
    cpu_T[1] = tcg_global_mem_new(TCG_TYPE_TL,
7883
                                  TCG_AREG0, offsetof(CPUState, t1), "T1");
7884
#else
7885
    cpu_T[0] = tcg_global_reg_new(TCG_TYPE_TL, TCG_AREG1, "T0");
7886
    cpu_T[1] = tcg_global_reg_new(TCG_TYPE_TL, TCG_AREG2, "T1");
7887
#endif
7888

    
7889
    /* register helpers */
7890
#undef DEF_HELPER
7891
#define DEF_HELPER(ret, name, params) tcg_register_helper(name, #name);
7892
#include "helper.h"
7893

    
7894
    fpu32_T[0] = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0, offsetof(CPUState, ft0.w[FP_ENDIAN_IDX]), "WT0");
7895
    fpu32_T[1] = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0, offsetof(CPUState, ft1.w[FP_ENDIAN_IDX]), "WT1");
7896
    fpu32_T[2] = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0, offsetof(CPUState, ft2.w[FP_ENDIAN_IDX]), "WT2");
7897
    fpu64_T[0] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0, offsetof(CPUState, ft0.d), "DT0");
7898
    fpu64_T[1] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0, offsetof(CPUState, ft1.d), "DT1");
7899
    fpu64_T[2] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0, offsetof(CPUState, ft2.d), "DT2");
7900
    fpu32h_T[0] = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0, offsetof(CPUState, ft0.w[!FP_ENDIAN_IDX]), "WTH0");
7901
    fpu32h_T[1] = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0, offsetof(CPUState, ft1.w[!FP_ENDIAN_IDX]), "WTH1");
7902
    fpu32h_T[2] = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0, offsetof(CPUState, ft2.w[!FP_ENDIAN_IDX]), "WTH2");
7903

    
7904
    inited = 1;
7905
}
7906

    
7907
#include "translate_init.c"
7908

    
7909
CPUMIPSState *cpu_mips_init (const char *cpu_model)
7910
{
7911
    CPUMIPSState *env;
7912
    const mips_def_t *def;
7913

    
7914
    def = cpu_mips_find_by_name(cpu_model);
7915
    if (!def)
7916
        return NULL;
7917
    env = qemu_mallocz(sizeof(CPUMIPSState));
7918
    if (!env)
7919
        return NULL;
7920
    env->cpu_model = def;
7921

    
7922
    cpu_exec_init(env);
7923
    env->cpu_model_str = cpu_model;
7924
    mips_tcg_init();
7925
    cpu_reset(env);
7926
    return env;
7927
}
7928

    
7929
void cpu_reset (CPUMIPSState *env)
7930
{
7931
    memset(env, 0, offsetof(CPUMIPSState, breakpoints));
7932

    
7933
    tlb_flush(env, 1);
7934

    
7935
    /* Minimal init */
7936
#if !defined(CONFIG_USER_ONLY)
7937
    if (env->hflags & MIPS_HFLAG_BMASK) {
7938
        /* If the exception was raised from a delay slot,
7939
         * come back to the jump.  */
7940
        env->CP0_ErrorEPC = env->PC[env->current_tc] - 4;
7941
    } else {
7942
        env->CP0_ErrorEPC = env->PC[env->current_tc];
7943
    }
7944
    env->PC[env->current_tc] = (int32_t)0xBFC00000;
7945
    env->CP0_Wired = 0;
7946
    /* SMP not implemented */
7947
    env->CP0_EBase = 0x80000000;
7948
    env->CP0_Status = (1 << CP0St_BEV) | (1 << CP0St_ERL);
7949
    /* vectored interrupts not implemented, timer on int 7,
7950
       no performance counters. */
7951
    env->CP0_IntCtl = 0xe0000000;
7952
    {
7953
        int i;
7954

    
7955
        for (i = 0; i < 7; i++) {
7956
            env->CP0_WatchLo[i] = 0;
7957
            env->CP0_WatchHi[i] = 0x80000000;
7958
        }
7959
        env->CP0_WatchLo[7] = 0;
7960
        env->CP0_WatchHi[7] = 0;
7961
    }
7962
    /* Count register increments in debug mode, EJTAG version 1 */
7963
    env->CP0_Debug = (1 << CP0DB_CNT) | (0x1 << CP0DB_VER);
7964
#endif
7965
    env->exception_index = EXCP_NONE;
7966
#if defined(CONFIG_USER_ONLY)
7967
    env->hflags = MIPS_HFLAG_UM;
7968
    env->user_mode_only = 1;
7969
#else
7970
    env->hflags = MIPS_HFLAG_CP0;
7971
#endif
7972
    cpu_mips_register(env, env->cpu_model);
7973
}
7974

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