Statistics
| Branch: | Revision:

root / target-mips / translate.c @ 20e1fb52

History | View | Annotate | Download (244.7 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., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 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 "tcg-op.h"
33
#include "qemu-common.h"
34

    
35
#include "helper.h"
36
#define GEN_HELPER 1
37
#include "helper.h"
38

    
39
//#define MIPS_DEBUG_DISAS
40
//#define MIPS_DEBUG_SIGN_EXTENSIONS
41
//#define MIPS_SINGLE_STEP
42

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

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

    
121
/* MIPS special opcodes */
122
#define MASK_SPECIAL(op)   MASK_OP_MAJOR(op) | (op & 0x3F)
123

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

    
187
    OPC_MOVCI    = 0x01 | OPC_SPECIAL,
188

    
189
    /* Special */
190
    OPC_PMON     = 0x05 | OPC_SPECIAL, /* inofficial */
191
    OPC_SYSCALL  = 0x0C | OPC_SPECIAL,
192
    OPC_BREAK    = 0x0D | OPC_SPECIAL,
193
    OPC_SPIM     = 0x0E | OPC_SPECIAL, /* inofficial */
194
    OPC_SYNC     = 0x0F | OPC_SPECIAL,
195

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

    
205
/* Multiplication variants of the vr54xx. */
206
#define MASK_MUL_VR54XX(op)   MASK_SPECIAL(op) | (op & (0x1F << 6))
207

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

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

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

    
246
/* Special2 opcodes */
247
#define MASK_SPECIAL2(op)  MASK_OP_MAJOR(op) | (op & 0x3F)
248

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

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

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

    
284
/* BSHFL opcodes */
285
#define MASK_BSHFL(op)     MASK_SPECIAL3(op) | (op & (0x1F << 6))
286

    
287
enum {
288
    OPC_WSBH     = (0x02 << 6) | OPC_BSHFL,
289
    OPC_SEB      = (0x10 << 6) | OPC_BSHFL,
290
    OPC_SEH      = (0x18 << 6) | OPC_BSHFL,
291
};
292

    
293
/* DBSHFL opcodes */
294
#define MASK_DBSHFL(op)    MASK_SPECIAL3(op) | (op & (0x1F << 6))
295

    
296
enum {
297
    OPC_DSBH     = (0x02 << 6) | OPC_DBSHFL,
298
    OPC_DSHD     = (0x05 << 6) | OPC_DBSHFL,
299
};
300

    
301
/* Coprocessor 0 (rs field) */
302
#define MASK_CP0(op)       MASK_OP_MAJOR(op) | (op & (0x1F << 21))
303

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

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

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

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

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

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

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

    
369
#define MASK_CP1_FUNC(op)       MASK_CP1(op) | (op & 0x3F)
370
#define MASK_BC1(op)            MASK_CP1(op) | (op & (0x3 << 16))
371

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

    
379
enum {
380
    OPC_BC1FANY2     = (0x00 << 16) | OPC_BC1ANY2,
381
    OPC_BC1TANY2     = (0x01 << 16) | OPC_BC1ANY2,
382
};
383

    
384
enum {
385
    OPC_BC1FANY4     = (0x00 << 16) | OPC_BC1ANY4,
386
    OPC_BC1TANY4     = (0x01 << 16) | OPC_BC1ANY4,
387
};
388

    
389
#define MASK_CP2(op)       MASK_OP_MAJOR(op) | (op & (0x1F << 21))
390

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

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

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

    
428
/* global register indices */
429
static TCGv_ptr cpu_env;
430
static TCGv cpu_gpr[32], cpu_PC;
431
static TCGv cpu_HI[MIPS_DSP_ACC], cpu_LO[MIPS_DSP_ACC], cpu_ACX[MIPS_DSP_ACC];
432
static TCGv cpu_dspctrl, btarget;
433
static TCGv_i32 bcond;
434
static TCGv_i32 fpu_fpr32[32], fpu_fpr32h[32];
435
static TCGv_i32 fpu_fcr0, fpu_fcr31;
436

    
437
#include "gen-icount.h"
438

    
439
#define gen_helper_0i(name, arg) do {                             \
440
    TCGv_i32 helper_tmp = tcg_const_i32(arg);                     \
441
    gen_helper_##name(helper_tmp);                                \
442
    tcg_temp_free_i32(helper_tmp);                                \
443
    } while(0)
444

    
445
#define gen_helper_1i(name, arg1, arg2) do {                      \
446
    TCGv_i32 helper_tmp = tcg_const_i32(arg2);                    \
447
    gen_helper_##name(arg1, helper_tmp);                          \
448
    tcg_temp_free_i32(helper_tmp);                                \
449
    } while(0)
450

    
451
#define gen_helper_2i(name, arg1, arg2, arg3) do {                \
452
    TCGv_i32 helper_tmp = tcg_const_i32(arg3);                    \
453
    gen_helper_##name(arg1, arg2, helper_tmp);                    \
454
    tcg_temp_free_i32(helper_tmp);                                \
455
    } while(0)
456

    
457
#define gen_helper_3i(name, arg1, arg2, arg3, arg4) do {          \
458
    TCGv_i32 helper_tmp = tcg_const_i32(arg4);                    \
459
    gen_helper_##name(arg1, arg2, arg3, helper_tmp);              \
460
    tcg_temp_free_i32(helper_tmp);                                \
461
    } while(0)
462

    
463
typedef struct DisasContext {
464
    struct TranslationBlock *tb;
465
    target_ulong pc, saved_pc;
466
    uint32_t opcode;
467
    /* Routine used to access memory */
468
    int mem_idx;
469
    uint32_t hflags, saved_hflags;
470
    int bstate;
471
    target_ulong btarget;
472
} DisasContext;
473

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

    
482
static const char *regnames[] =
483
    { "r0", "at", "v0", "v1", "a0", "a1", "a2", "a3",
484
      "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
485
      "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
486
      "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra", };
487

    
488
static const char *regnames_HI[] =
489
    { "HI0", "HI1", "HI2", "HI3", };
490

    
491
static const char *regnames_LO[] =
492
    { "LO0", "LO1", "LO2", "LO3", };
493

    
494
static const char *regnames_ACX[] =
495
    { "ACX0", "ACX1", "ACX2", "ACX3", };
496

    
497
static const char *fregnames[] =
498
    { "f0",  "f1",  "f2",  "f3",  "f4",  "f5",  "f6",  "f7",
499
      "f8",  "f9",  "f10", "f11", "f12", "f13", "f14", "f15",
500
      "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
501
      "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31", };
502

    
503
static const char *fregnames_h[] =
504
    { "h0",  "h1",  "h2",  "h3",  "h4",  "h5",  "h6",  "h7",
505
      "h8",  "h9",  "h10", "h11", "h12", "h13", "h14", "h15",
506
      "h16", "h17", "h18", "h19", "h20", "h21", "h22", "h23",
507
      "h24", "h25", "h26", "h27", "h28", "h29", "h30", "h31", };
508

    
509
#ifdef MIPS_DEBUG_DISAS
510
#define MIPS_DEBUG(fmt, args...)                         \
511
        qemu_log_mask(CPU_LOG_TB_IN_ASM,                \
512
                       TARGET_FMT_lx ": %08x " fmt "\n", \
513
                       ctx->pc, ctx->opcode , ##args)
514
#define LOG_DISAS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
515
#else
516
#define MIPS_DEBUG(fmt, args...) do { } while(0)
517
#define LOG_DISAS(...) do { } while (0)
518
#endif
519

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

    
526
/* General purpose registers moves. */
527
static inline void gen_load_gpr (TCGv t, int reg)
528
{
529
    if (reg == 0)
530
        tcg_gen_movi_tl(t, 0);
531
    else
532
        tcg_gen_mov_tl(t, cpu_gpr[reg]);
533
}
534

    
535
static inline void gen_store_gpr (TCGv t, int reg)
536
{
537
    if (reg != 0)
538
        tcg_gen_mov_tl(cpu_gpr[reg], t);
539
}
540

    
541
/* Moves to/from ACX register.  */
542
static inline void gen_load_ACX (TCGv t, int reg)
543
{
544
    tcg_gen_mov_tl(t, cpu_ACX[reg]);
545
}
546

    
547
static inline void gen_store_ACX (TCGv t, int reg)
548
{
549
    tcg_gen_mov_tl(cpu_ACX[reg], t);
550
}
551

    
552
/* Moves to/from shadow registers. */
553
static inline void gen_load_srsgpr (int from, int to)
554
{
555
    TCGv r_tmp1 = tcg_temp_new();
556

    
557
    if (from == 0)
558
        tcg_gen_movi_tl(r_tmp1, 0);
559
    else {
560
        TCGv_i32 r_tmp2 = tcg_temp_new_i32();
561
        TCGv_ptr addr = tcg_temp_new_ptr();
562

    
563
        tcg_gen_ld_i32(r_tmp2, cpu_env, offsetof(CPUState, CP0_SRSCtl));
564
        tcg_gen_shri_i32(r_tmp2, r_tmp2, CP0SRSCtl_PSS);
565
        tcg_gen_andi_i32(r_tmp2, r_tmp2, 0xf);
566
        tcg_gen_muli_i32(r_tmp2, r_tmp2, sizeof(target_ulong) * 32);
567
        tcg_gen_ext_i32_ptr(addr, r_tmp2);
568
        tcg_gen_add_ptr(addr, cpu_env, addr);
569

    
570
        tcg_gen_ld_tl(r_tmp1, addr, sizeof(target_ulong) * from);
571
        tcg_temp_free_ptr(addr);
572
        tcg_temp_free_i32(r_tmp2);
573
    }
574
    gen_store_gpr(r_tmp1, to);
575
    tcg_temp_free(r_tmp1);
576
}
577

    
578
static inline void gen_store_srsgpr (int from, int to)
579
{
580
    if (to != 0) {
581
        TCGv r_tmp1 = tcg_temp_new();
582
        TCGv_i32 r_tmp2 = tcg_temp_new_i32();
583
        TCGv_ptr addr = tcg_temp_new_ptr();
584

    
585
        gen_load_gpr(r_tmp1, from);
586
        tcg_gen_ld_i32(r_tmp2, cpu_env, offsetof(CPUState, CP0_SRSCtl));
587
        tcg_gen_shri_i32(r_tmp2, r_tmp2, CP0SRSCtl_PSS);
588
        tcg_gen_andi_i32(r_tmp2, r_tmp2, 0xf);
589
        tcg_gen_muli_i32(r_tmp2, r_tmp2, sizeof(target_ulong) * 32);
590
        tcg_gen_ext_i32_ptr(addr, r_tmp2);
591
        tcg_gen_add_ptr(addr, cpu_env, addr);
592

    
593
        tcg_gen_st_tl(r_tmp1, addr, sizeof(target_ulong) * to);
594
        tcg_temp_free_ptr(addr);
595
        tcg_temp_free_i32(r_tmp2);
596
        tcg_temp_free(r_tmp1);
597
    }
598
}
599

    
600
/* Floating point register moves. */
601
static inline void gen_load_fpr32 (TCGv_i32 t, int reg)
602
{
603
    tcg_gen_mov_i32(t, fpu_fpr32[reg]);
604
}
605

    
606
static inline void gen_store_fpr32 (TCGv_i32 t, int reg)
607
{
608
    tcg_gen_mov_i32(fpu_fpr32[reg], t);
609
}
610

    
611
static inline void gen_load_fpr64 (DisasContext *ctx, TCGv_i64 t, int reg)
612
{
613
    if (ctx->hflags & MIPS_HFLAG_F64) {
614
        tcg_gen_concat_i32_i64(t, fpu_fpr32[reg], fpu_fpr32h[reg]);
615
    } else {
616
        tcg_gen_concat_i32_i64(t, fpu_fpr32[reg & ~1], fpu_fpr32[reg | 1]);
617
    }
618
}
619

    
620
static inline void gen_store_fpr64 (DisasContext *ctx, TCGv_i64 t, int reg)
621
{
622
    if (ctx->hflags & MIPS_HFLAG_F64) {
623
        tcg_gen_trunc_i64_i32(fpu_fpr32[reg], t);
624
        tcg_gen_shri_i64(t, t, 32);
625
        tcg_gen_trunc_i64_i32(fpu_fpr32h[reg], t);
626
    } else {
627
        tcg_gen_trunc_i64_i32(fpu_fpr32[reg & ~1], t);
628
        tcg_gen_shri_i64(t, t, 32);
629
        tcg_gen_trunc_i64_i32(fpu_fpr32[reg | 1], t);
630
    }
631
}
632

    
633
static inline void gen_load_fpr32h (TCGv_i32 t, int reg)
634
{
635
    tcg_gen_mov_i32(t, fpu_fpr32h[reg]);
636
}
637

    
638
static inline void gen_store_fpr32h (TCGv_i32 t, int reg)
639
{
640
    tcg_gen_mov_i32(fpu_fpr32h[reg], t);
641
}
642

    
643
static inline void get_fp_cond (TCGv_i32 t)
644
{
645
    TCGv_i32 r_tmp1 = tcg_temp_new_i32();
646
    TCGv_i32 r_tmp2 = tcg_temp_new_i32();
647

    
648
    tcg_gen_shri_i32(r_tmp2, fpu_fcr31, 24);
649
    tcg_gen_andi_i32(r_tmp2, r_tmp2, 0xfe);
650
    tcg_gen_shri_i32(r_tmp1, fpu_fcr31, 23);
651
    tcg_gen_andi_i32(r_tmp1, r_tmp1, 0x1);
652
    tcg_gen_or_i32(t, r_tmp1, r_tmp2);
653
    tcg_temp_free_i32(r_tmp1);
654
    tcg_temp_free_i32(r_tmp2);
655
}
656

    
657
#define FOP_CONDS(type, fmt, bits)                                            \
658
static inline void gen_cmp ## type ## _ ## fmt(int n, TCGv_i##bits a,         \
659
                                               TCGv_i##bits b, int cc)        \
660
{                                                                             \
661
    switch (n) {                                                              \
662
    case  0: gen_helper_2i(cmp ## type ## _ ## fmt ## _f, a, b, cc);    break;\
663
    case  1: gen_helper_2i(cmp ## type ## _ ## fmt ## _un, a, b, cc);   break;\
664
    case  2: gen_helper_2i(cmp ## type ## _ ## fmt ## _eq, a, b, cc);   break;\
665
    case  3: gen_helper_2i(cmp ## type ## _ ## fmt ## _ueq, a, b, cc);  break;\
666
    case  4: gen_helper_2i(cmp ## type ## _ ## fmt ## _olt, a, b, cc);  break;\
667
    case  5: gen_helper_2i(cmp ## type ## _ ## fmt ## _ult, a, b, cc);  break;\
668
    case  6: gen_helper_2i(cmp ## type ## _ ## fmt ## _ole, a, b, cc);  break;\
669
    case  7: gen_helper_2i(cmp ## type ## _ ## fmt ## _ule, a, b, cc);  break;\
670
    case  8: gen_helper_2i(cmp ## type ## _ ## fmt ## _sf, a, b, cc);   break;\
671
    case  9: gen_helper_2i(cmp ## type ## _ ## fmt ## _ngle, a, b, cc); break;\
672
    case 10: gen_helper_2i(cmp ## type ## _ ## fmt ## _seq, a, b, cc);  break;\
673
    case 11: gen_helper_2i(cmp ## type ## _ ## fmt ## _ngl, a, b, cc);  break;\
674
    case 12: gen_helper_2i(cmp ## type ## _ ## fmt ## _lt, a, b, cc);   break;\
675
    case 13: gen_helper_2i(cmp ## type ## _ ## fmt ## _nge, a, b, cc);  break;\
676
    case 14: gen_helper_2i(cmp ## type ## _ ## fmt ## _le, a, b, cc);   break;\
677
    case 15: gen_helper_2i(cmp ## type ## _ ## fmt ## _ngt, a, b, cc);  break;\
678
    default: abort();                                                         \
679
    }                                                                         \
680
}
681

    
682
FOP_CONDS(, d, 64)
683
FOP_CONDS(abs, d, 64)
684
FOP_CONDS(, s, 32)
685
FOP_CONDS(abs, s, 32)
686
FOP_CONDS(, ps, 64)
687
FOP_CONDS(abs, ps, 64)
688
#undef FOP_CONDS
689

    
690
/* Tests */
691
#define OP_COND(name, cond)                                   \
692
static inline void glue(gen_op_, name) (TCGv t0, TCGv t1)     \
693
{                                                             \
694
    int l1 = gen_new_label();                                 \
695
    int l2 = gen_new_label();                                 \
696
                                                              \
697
    tcg_gen_brcond_tl(cond, t0, t1, l1);                      \
698
    tcg_gen_movi_tl(t0, 0);                                   \
699
    tcg_gen_br(l2);                                           \
700
    gen_set_label(l1);                                        \
701
    tcg_gen_movi_tl(t0, 1);                                   \
702
    gen_set_label(l2);                                        \
703
}
704
OP_COND(eq, TCG_COND_EQ);
705
OP_COND(ne, TCG_COND_NE);
706
OP_COND(ge, TCG_COND_GE);
707
OP_COND(geu, TCG_COND_GEU);
708
OP_COND(lt, TCG_COND_LT);
709
OP_COND(ltu, TCG_COND_LTU);
710
#undef OP_COND
711

    
712
#define OP_CONDI(name, cond)                                  \
713
static inline void glue(gen_op_, name) (TCGv t, target_ulong val) \
714
{                                                             \
715
    int l1 = gen_new_label();                                 \
716
    int l2 = gen_new_label();                                 \
717
                                                              \
718
    tcg_gen_brcondi_tl(cond, t, val, l1);                     \
719
    tcg_gen_movi_tl(t, 0);                                    \
720
    tcg_gen_br(l2);                                           \
721
    gen_set_label(l1);                                        \
722
    tcg_gen_movi_tl(t, 1);                                    \
723
    gen_set_label(l2);                                        \
724
}
725
OP_CONDI(lti, TCG_COND_LT);
726
OP_CONDI(ltiu, TCG_COND_LTU);
727
#undef OP_CONDI
728

    
729
#define OP_CONDZ(name, cond)                                  \
730
static inline void glue(gen_op_, name) (TCGv t)               \
731
{                                                             \
732
    int l1 = gen_new_label();                                 \
733
    int l2 = gen_new_label();                                 \
734
                                                              \
735
    tcg_gen_brcondi_tl(cond, t, 0, l1);                       \
736
    tcg_gen_movi_tl(t, 0);                                    \
737
    tcg_gen_br(l2);                                           \
738
    gen_set_label(l1);                                        \
739
    tcg_gen_movi_tl(t, 1);                                    \
740
    gen_set_label(l2);                                        \
741
}
742
OP_CONDZ(gez, TCG_COND_GE);
743
OP_CONDZ(gtz, TCG_COND_GT);
744
OP_CONDZ(lez, TCG_COND_LE);
745
OP_CONDZ(ltz, TCG_COND_LT);
746
#undef OP_CONDZ
747

    
748
static inline void gen_save_pc(target_ulong pc)
749
{
750
    tcg_gen_movi_tl(cpu_PC, pc);
751
}
752

    
753
static inline void save_cpu_state (DisasContext *ctx, int do_save_pc)
754
{
755
    LOG_DISAS("hflags %08x saved %08x\n", ctx->hflags, ctx->saved_hflags);
756
    if (do_save_pc && ctx->pc != ctx->saved_pc) {
757
        gen_save_pc(ctx->pc);
758
        ctx->saved_pc = ctx->pc;
759
    }
760
    if (ctx->hflags != ctx->saved_hflags) {
761
        TCGv_i32 r_tmp = tcg_temp_new_i32();
762

    
763
        tcg_gen_movi_i32(r_tmp, ctx->hflags);
764
        tcg_gen_st_i32(r_tmp, cpu_env, offsetof(CPUState, hflags));
765
        tcg_temp_free_i32(r_tmp);
766
        ctx->saved_hflags = ctx->hflags;
767
        switch (ctx->hflags & MIPS_HFLAG_BMASK) {
768
        case MIPS_HFLAG_BR:
769
            break;
770
        case MIPS_HFLAG_BC:
771
        case MIPS_HFLAG_BL:
772
        case MIPS_HFLAG_B:
773
            tcg_gen_movi_tl(btarget, ctx->btarget);
774
            break;
775
        }
776
    }
777
}
778

    
779
static inline void restore_cpu_state (CPUState *env, DisasContext *ctx)
780
{
781
    ctx->saved_hflags = ctx->hflags;
782
    switch (ctx->hflags & MIPS_HFLAG_BMASK) {
783
    case MIPS_HFLAG_BR:
784
        break;
785
    case MIPS_HFLAG_BC:
786
    case MIPS_HFLAG_BL:
787
    case MIPS_HFLAG_B:
788
        ctx->btarget = env->btarget;
789
        break;
790
    }
791
}
792

    
793
static inline void
794
generate_exception_err (DisasContext *ctx, int excp, int err)
795
{
796
    TCGv_i32 texcp = tcg_const_i32(excp);
797
    TCGv_i32 terr = tcg_const_i32(err);
798
    save_cpu_state(ctx, 1);
799
    gen_helper_raise_exception_err(texcp, terr);
800
    tcg_temp_free_i32(terr);
801
    tcg_temp_free_i32(texcp);
802
    gen_helper_interrupt_restart();
803
    tcg_gen_exit_tb(0);
804
}
805

    
806
static inline void
807
generate_exception (DisasContext *ctx, int excp)
808
{
809
    save_cpu_state(ctx, 1);
810
    gen_helper_0i(raise_exception, excp);
811
    gen_helper_interrupt_restart();
812
    tcg_gen_exit_tb(0);
813
}
814

    
815
/* Addresses computation */
816
static inline void gen_op_addr_add (DisasContext *ctx, TCGv t0, TCGv t1)
817
{
818
    tcg_gen_add_tl(t0, t0, t1);
819

    
820
#if defined(TARGET_MIPS64)
821
    /* For compatibility with 32-bit code, data reference in user mode
822
       with Status_UX = 0 should be casted to 32-bit and sign extended.
823
       See the MIPS64 PRA manual, section 4.10. */
824
    if (((ctx->hflags & MIPS_HFLAG_KSU) == MIPS_HFLAG_UM) &&
825
        !(ctx->hflags & MIPS_HFLAG_UX)) {
826
        tcg_gen_ext32s_i64(t0, t0);
827
    }
828
#endif
829
}
830

    
831
static inline void check_cp0_enabled(DisasContext *ctx)
832
{
833
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_CP0)))
834
        generate_exception_err(ctx, EXCP_CpU, 1);
835
}
836

    
837
static inline void check_cp1_enabled(DisasContext *ctx)
838
{
839
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_FPU)))
840
        generate_exception_err(ctx, EXCP_CpU, 1);
841
}
842

    
843
/* Verify that the processor is running with COP1X instructions enabled.
844
   This is associated with the nabla symbol in the MIPS32 and MIPS64
845
   opcode tables.  */
846

    
847
static inline void check_cop1x(DisasContext *ctx)
848
{
849
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_COP1X)))
850
        generate_exception(ctx, EXCP_RI);
851
}
852

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

    
856
static inline void check_cp1_64bitmode(DisasContext *ctx)
857
{
858
    if (unlikely(~ctx->hflags & (MIPS_HFLAG_F64 | MIPS_HFLAG_COP1X)))
859
        generate_exception(ctx, EXCP_RI);
860
}
861

    
862
/*
863
 * Verify if floating point register is valid; an operation is not defined
864
 * if bit 0 of any register specification is set and the FR bit in the
865
 * Status register equals zero, since the register numbers specify an
866
 * even-odd pair of adjacent coprocessor general registers. When the FR bit
867
 * in the Status register equals one, both even and odd register numbers
868
 * are valid. This limitation exists only for 64 bit wide (d,l,ps) registers.
869
 *
870
 * Multiple 64 bit wide registers can be checked by calling
871
 * gen_op_cp1_registers(freg1 | freg2 | ... | fregN);
872
 */
873
static inline void check_cp1_registers(DisasContext *ctx, int regs)
874
{
875
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_F64) && (regs & 1)))
876
        generate_exception(ctx, EXCP_RI);
877
}
878

    
879
/* This code generates a "reserved instruction" exception if the
880
   CPU does not support the instruction set corresponding to flags. */
881
static inline void check_insn(CPUState *env, DisasContext *ctx, int flags)
882
{
883
    if (unlikely(!(env->insn_flags & flags)))
884
        generate_exception(ctx, EXCP_RI);
885
}
886

    
887
/* This code generates a "reserved instruction" exception if 64-bit
888
   instructions are not enabled. */
889
static inline void check_mips_64(DisasContext *ctx)
890
{
891
    if (unlikely(!(ctx->hflags & MIPS_HFLAG_64)))
892
        generate_exception(ctx, EXCP_RI);
893
}
894

    
895
/* load/store instructions. */
896
#define OP_LD(insn,fname)                                        \
897
static inline void op_ldst_##insn(TCGv t0, DisasContext *ctx)    \
898
{                                                                \
899
    tcg_gen_qemu_##fname(t0, t0, ctx->mem_idx);                  \
900
}
901
OP_LD(lb,ld8s);
902
OP_LD(lbu,ld8u);
903
OP_LD(lh,ld16s);
904
OP_LD(lhu,ld16u);
905
OP_LD(lw,ld32s);
906
#if defined(TARGET_MIPS64)
907
OP_LD(lwu,ld32u);
908
OP_LD(ld,ld64);
909
#endif
910
#undef OP_LD
911

    
912
#define OP_ST(insn,fname)                                        \
913
static inline void op_ldst_##insn(TCGv t0, TCGv t1, DisasContext *ctx) \
914
{                                                                \
915
    tcg_gen_qemu_##fname(t1, t0, ctx->mem_idx);                  \
916
}
917
OP_ST(sb,st8);
918
OP_ST(sh,st16);
919
OP_ST(sw,st32);
920
#if defined(TARGET_MIPS64)
921
OP_ST(sd,st64);
922
#endif
923
#undef OP_ST
924

    
925
#define OP_LD_ATOMIC(insn,fname)                                        \
926
static inline void op_ldst_##insn(TCGv t0, TCGv t1, DisasContext *ctx)  \
927
{                                                                       \
928
    tcg_gen_mov_tl(t1, t0);                                             \
929
    tcg_gen_qemu_##fname(t0, t0, ctx->mem_idx);                         \
930
    tcg_gen_st_tl(t1, cpu_env, offsetof(CPUState, CP0_LLAddr));         \
931
}
932
OP_LD_ATOMIC(ll,ld32s);
933
#if defined(TARGET_MIPS64)
934
OP_LD_ATOMIC(lld,ld64);
935
#endif
936
#undef OP_LD_ATOMIC
937

    
938
#define OP_ST_ATOMIC(insn,fname,almask)                                 \
939
static inline void op_ldst_##insn(TCGv t0, TCGv t1, DisasContext *ctx)  \
940
{                                                                       \
941
    TCGv r_tmp = tcg_temp_local_new();                       \
942
    int l1 = gen_new_label();                                           \
943
    int l2 = gen_new_label();                                           \
944
    int l3 = gen_new_label();                                           \
945
                                                                        \
946
    tcg_gen_andi_tl(r_tmp, t0, almask);                                 \
947
    tcg_gen_brcondi_tl(TCG_COND_EQ, r_tmp, 0, l1);                      \
948
    tcg_gen_st_tl(t0, cpu_env, offsetof(CPUState, CP0_BadVAddr));       \
949
    generate_exception(ctx, EXCP_AdES);                                 \
950
    gen_set_label(l1);                                                  \
951
    tcg_gen_ld_tl(r_tmp, cpu_env, offsetof(CPUState, CP0_LLAddr));      \
952
    tcg_gen_brcond_tl(TCG_COND_NE, t0, r_tmp, l2);                      \
953
    tcg_gen_qemu_##fname(t1, t0, ctx->mem_idx);                         \
954
    tcg_gen_movi_tl(t0, 1);                                             \
955
    tcg_gen_br(l3);                                                     \
956
    gen_set_label(l2);                                                  \
957
    tcg_gen_movi_tl(t0, 0);                                             \
958
    gen_set_label(l3);                                                  \
959
    tcg_temp_free(r_tmp);                                               \
960
}
961
OP_ST_ATOMIC(sc,st32,0x3);
962
#if defined(TARGET_MIPS64)
963
OP_ST_ATOMIC(scd,st64,0x7);
964
#endif
965
#undef OP_ST_ATOMIC
966

    
967
/* Load and store */
968
static void gen_ldst (DisasContext *ctx, uint32_t opc, int rt,
969
                      int base, int16_t offset)
970
{
971
    const char *opn = "ldst";
972
    TCGv t0 = tcg_temp_local_new();
973
    TCGv t1 = tcg_temp_local_new();
974

    
975
    if (base == 0) {
976
        tcg_gen_movi_tl(t0, offset);
977
    } else if (offset == 0) {
978
        gen_load_gpr(t0, base);
979
    } else {
980
        tcg_gen_movi_tl(t0, offset);
981
        gen_op_addr_add(ctx, t0, cpu_gpr[base]);
982
    }
983
    /* Don't do NOP if destination is zero: we must perform the actual
984
       memory access. */
985
    switch (opc) {
986
#if defined(TARGET_MIPS64)
987
    case OPC_LWU:
988
        op_ldst_lwu(t0, ctx);
989
        gen_store_gpr(t0, rt);
990
        opn = "lwu";
991
        break;
992
    case OPC_LD:
993
        op_ldst_ld(t0, ctx);
994
        gen_store_gpr(t0, rt);
995
        opn = "ld";
996
        break;
997
    case OPC_LLD:
998
        op_ldst_lld(t0, t1, ctx);
999
        gen_store_gpr(t0, rt);
1000
        opn = "lld";
1001
        break;
1002
    case OPC_SD:
1003
        gen_load_gpr(t1, rt);
1004
        op_ldst_sd(t0, t1, ctx);
1005
        opn = "sd";
1006
        break;
1007
    case OPC_SCD:
1008
        save_cpu_state(ctx, 1);
1009
        gen_load_gpr(t1, rt);
1010
        op_ldst_scd(t0, t1, ctx);
1011
        gen_store_gpr(t0, rt);
1012
        opn = "scd";
1013
        break;
1014
    case OPC_LDL:
1015
        save_cpu_state(ctx, 1);
1016
        gen_load_gpr(t1, rt);
1017
        gen_helper_3i(ldl, t1, t0, t1, ctx->mem_idx);
1018
        gen_store_gpr(t1, rt);
1019
        opn = "ldl";
1020
        break;
1021
    case OPC_SDL:
1022
        save_cpu_state(ctx, 1);
1023
        gen_load_gpr(t1, rt);
1024
        gen_helper_2i(sdl, t0, t1, ctx->mem_idx);
1025
        opn = "sdl";
1026
        break;
1027
    case OPC_LDR:
1028
        save_cpu_state(ctx, 1);
1029
        gen_load_gpr(t1, rt);
1030
        gen_helper_3i(ldr, t1, t0, t1, ctx->mem_idx);
1031
        gen_store_gpr(t1, rt);
1032
        opn = "ldr";
1033
        break;
1034
    case OPC_SDR:
1035
        save_cpu_state(ctx, 1);
1036
        gen_load_gpr(t1, rt);
1037
        gen_helper_2i(sdr, t0, t1, ctx->mem_idx);
1038
        opn = "sdr";
1039
        break;
1040
#endif
1041
    case OPC_LW:
1042
        op_ldst_lw(t0, ctx);
1043
        gen_store_gpr(t0, rt);
1044
        opn = "lw";
1045
        break;
1046
    case OPC_SW:
1047
        gen_load_gpr(t1, rt);
1048
        op_ldst_sw(t0, t1, ctx);
1049
        opn = "sw";
1050
        break;
1051
    case OPC_LH:
1052
        op_ldst_lh(t0, ctx);
1053
        gen_store_gpr(t0, rt);
1054
        opn = "lh";
1055
        break;
1056
    case OPC_SH:
1057
        gen_load_gpr(t1, rt);
1058
        op_ldst_sh(t0, t1, ctx);
1059
        opn = "sh";
1060
        break;
1061
    case OPC_LHU:
1062
        op_ldst_lhu(t0, ctx);
1063
        gen_store_gpr(t0, rt);
1064
        opn = "lhu";
1065
        break;
1066
    case OPC_LB:
1067
        op_ldst_lb(t0, ctx);
1068
        gen_store_gpr(t0, rt);
1069
        opn = "lb";
1070
        break;
1071
    case OPC_SB:
1072
        gen_load_gpr(t1, rt);
1073
        op_ldst_sb(t0, t1, ctx);
1074
        opn = "sb";
1075
        break;
1076
    case OPC_LBU:
1077
        op_ldst_lbu(t0, ctx);
1078
        gen_store_gpr(t0, rt);
1079
        opn = "lbu";
1080
        break;
1081
    case OPC_LWL:
1082
        save_cpu_state(ctx, 1);
1083
        gen_load_gpr(t1, rt);
1084
        gen_helper_3i(lwl, t1, t0, t1, ctx->mem_idx);
1085
        gen_store_gpr(t1, rt);
1086
        opn = "lwl";
1087
        break;
1088
    case OPC_SWL:
1089
        save_cpu_state(ctx, 1);
1090
        gen_load_gpr(t1, rt);
1091
        gen_helper_2i(swl, t0, t1, ctx->mem_idx);
1092
        opn = "swr";
1093
        break;
1094
    case OPC_LWR:
1095
        save_cpu_state(ctx, 1);
1096
        gen_load_gpr(t1, rt);
1097
        gen_helper_3i(lwr, t1, t0, t1, ctx->mem_idx);
1098
        gen_store_gpr(t1, rt);
1099
        opn = "lwr";
1100
        break;
1101
    case OPC_SWR:
1102
        save_cpu_state(ctx, 1);
1103
        gen_load_gpr(t1, rt);
1104
        gen_helper_2i(swr, t0, t1, ctx->mem_idx);
1105
        opn = "swr";
1106
        break;
1107
    case OPC_LL:
1108
        op_ldst_ll(t0, t1, ctx);
1109
        gen_store_gpr(t0, rt);
1110
        opn = "ll";
1111
        break;
1112
    case OPC_SC:
1113
        save_cpu_state(ctx, 1);
1114
        gen_load_gpr(t1, rt);
1115
        op_ldst_sc(t0, t1, ctx);
1116
        gen_store_gpr(t0, rt);
1117
        opn = "sc";
1118
        break;
1119
    default:
1120
        MIPS_INVAL(opn);
1121
        generate_exception(ctx, EXCP_RI);
1122
        goto out;
1123
    }
1124
    MIPS_DEBUG("%s %s, %d(%s)", opn, regnames[rt], offset, regnames[base]);
1125
 out:
1126
    tcg_temp_free(t0);
1127
    tcg_temp_free(t1);
1128
}
1129

    
1130
/* Load and store */
1131
static void gen_flt_ldst (DisasContext *ctx, uint32_t opc, int ft,
1132
                          int base, int16_t offset)
1133
{
1134
    const char *opn = "flt_ldst";
1135
    TCGv t0 = tcg_temp_local_new();
1136

    
1137
    if (base == 0) {
1138
        tcg_gen_movi_tl(t0, offset);
1139
    } else if (offset == 0) {
1140
        gen_load_gpr(t0, base);
1141
    } else {
1142
        tcg_gen_movi_tl(t0, offset);
1143
        gen_op_addr_add(ctx, t0, cpu_gpr[base]);
1144
    }
1145
    /* Don't do NOP if destination is zero: we must perform the actual
1146
       memory access. */
1147
    switch (opc) {
1148
    case OPC_LWC1:
1149
        {
1150
            TCGv_i32 fp0 = tcg_temp_new_i32();
1151
            TCGv t1 = tcg_temp_new();
1152

    
1153
            tcg_gen_qemu_ld32s(t1, t0, ctx->mem_idx);
1154
            tcg_gen_trunc_tl_i32(fp0, t1);
1155
            gen_store_fpr32(fp0, ft);
1156
            tcg_temp_free(t1);
1157
            tcg_temp_free_i32(fp0);
1158
        }
1159
        opn = "lwc1";
1160
        break;
1161
    case OPC_SWC1:
1162
        {
1163
            TCGv_i32 fp0 = tcg_temp_new_i32();
1164
            TCGv t1 = tcg_temp_new();
1165

    
1166
            gen_load_fpr32(fp0, ft);
1167
            tcg_gen_extu_i32_tl(t1, fp0);
1168
            tcg_gen_qemu_st32(t1, t0, ctx->mem_idx);
1169
            tcg_temp_free(t1);
1170
            tcg_temp_free_i32(fp0);
1171
        }
1172
        opn = "swc1";
1173
        break;
1174
    case OPC_LDC1:
1175
        {
1176
            TCGv_i64 fp0 = tcg_temp_new_i64();
1177

    
1178
            tcg_gen_qemu_ld64(fp0, t0, ctx->mem_idx);
1179
            gen_store_fpr64(ctx, fp0, ft);
1180
            tcg_temp_free_i64(fp0);
1181
        }
1182
        opn = "ldc1";
1183
        break;
1184
    case OPC_SDC1:
1185
        {
1186
            TCGv_i64 fp0 = tcg_temp_new_i64();
1187

    
1188
            gen_load_fpr64(ctx, fp0, ft);
1189
            tcg_gen_qemu_st64(fp0, t0, ctx->mem_idx);
1190
            tcg_temp_free_i64(fp0);
1191
        }
1192
        opn = "sdc1";
1193
        break;
1194
    default:
1195
        MIPS_INVAL(opn);
1196
        generate_exception(ctx, EXCP_RI);
1197
        goto out;
1198
    }
1199
    MIPS_DEBUG("%s %s, %d(%s)", opn, fregnames[ft], offset, regnames[base]);
1200
 out:
1201
    tcg_temp_free(t0);
1202
}
1203

    
1204
/* Arithmetic with immediate operand */
1205
static void gen_arith_imm (CPUState *env, DisasContext *ctx, uint32_t opc,
1206
                           int rt, int rs, int16_t imm)
1207
{
1208
    target_ulong uimm;
1209
    const char *opn = "imm arith";
1210
    TCGv t0 = tcg_temp_local_new();
1211

    
1212
    if (rt == 0 && opc != OPC_ADDI && opc != OPC_DADDI) {
1213
        /* If no destination, treat it as a NOP.
1214
           For addi, we must generate the overflow exception when needed. */
1215
        MIPS_DEBUG("NOP");
1216
        goto out;
1217
    }
1218
    uimm = (uint16_t)imm;
1219
    switch (opc) {
1220
    case OPC_ADDI:
1221
    case OPC_ADDIU:
1222
#if defined(TARGET_MIPS64)
1223
    case OPC_DADDI:
1224
    case OPC_DADDIU:
1225
#endif
1226
    case OPC_SLTI:
1227
    case OPC_SLTIU:
1228
        uimm = (target_long)imm; /* Sign extend to 32/64 bits */
1229
        /* Fall through. */
1230
    case OPC_ANDI:
1231
    case OPC_ORI:
1232
    case OPC_XORI:
1233
        gen_load_gpr(t0, rs);
1234
        break;
1235
    case OPC_LUI:
1236
        tcg_gen_movi_tl(t0, imm << 16);
1237
        break;
1238
    case OPC_SLL:
1239
    case OPC_SRA:
1240
    case OPC_SRL:
1241
#if defined(TARGET_MIPS64)
1242
    case OPC_DSLL:
1243
    case OPC_DSRA:
1244
    case OPC_DSRL:
1245
    case OPC_DSLL32:
1246
    case OPC_DSRA32:
1247
    case OPC_DSRL32:
1248
#endif
1249
        uimm &= 0x1f;
1250
        gen_load_gpr(t0, rs);
1251
        break;
1252
    }
1253
    switch (opc) {
1254
    case OPC_ADDI:
1255
        {
1256
            TCGv r_tmp1 = tcg_temp_new();
1257
            TCGv r_tmp2 = tcg_temp_new();
1258
            int l1 = gen_new_label();
1259

    
1260
            save_cpu_state(ctx, 1);
1261
            tcg_gen_ext32s_tl(r_tmp1, t0);
1262
            tcg_gen_addi_tl(t0, r_tmp1, uimm);
1263

    
1264
            tcg_gen_xori_tl(r_tmp1, r_tmp1, ~uimm);
1265
            tcg_gen_xori_tl(r_tmp2, t0, uimm);
1266
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
1267
            tcg_temp_free(r_tmp2);
1268
            tcg_gen_brcondi_tl(TCG_COND_GE, r_tmp1, 0, l1);
1269
            /* operands of same sign, result different sign */
1270
            generate_exception(ctx, EXCP_OVERFLOW);
1271
            gen_set_label(l1);
1272
            tcg_temp_free(r_tmp1);
1273

    
1274
            tcg_gen_ext32s_tl(t0, t0);
1275
        }
1276
        opn = "addi";
1277
        break;
1278
    case OPC_ADDIU:
1279
        tcg_gen_addi_tl(t0, t0, uimm);
1280
        tcg_gen_ext32s_tl(t0, t0);
1281
        opn = "addiu";
1282
        break;
1283
#if defined(TARGET_MIPS64)
1284
    case OPC_DADDI:
1285
        {
1286
            TCGv r_tmp1 = tcg_temp_new();
1287
            TCGv r_tmp2 = tcg_temp_new();
1288
            int l1 = gen_new_label();
1289

    
1290
            save_cpu_state(ctx, 1);
1291
            tcg_gen_mov_tl(r_tmp1, t0);
1292
            tcg_gen_addi_tl(t0, t0, uimm);
1293

    
1294
            tcg_gen_xori_tl(r_tmp1, r_tmp1, ~uimm);
1295
            tcg_gen_xori_tl(r_tmp2, t0, uimm);
1296
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
1297
            tcg_temp_free(r_tmp2);
1298
            tcg_gen_brcondi_tl(TCG_COND_GE, r_tmp1, 0, l1);
1299
            /* operands of same sign, result different sign */
1300
            generate_exception(ctx, EXCP_OVERFLOW);
1301
            gen_set_label(l1);
1302
            tcg_temp_free(r_tmp1);
1303
        }
1304
        opn = "daddi";
1305
        break;
1306
    case OPC_DADDIU:
1307
        tcg_gen_addi_tl(t0, t0, uimm);
1308
        opn = "daddiu";
1309
        break;
1310
#endif
1311
    case OPC_SLTI:
1312
        gen_op_lti(t0, uimm);
1313
        opn = "slti";
1314
        break;
1315
    case OPC_SLTIU:
1316
        gen_op_ltiu(t0, uimm);
1317
        opn = "sltiu";
1318
        break;
1319
    case OPC_ANDI:
1320
        tcg_gen_andi_tl(t0, t0, uimm);
1321
        opn = "andi";
1322
        break;
1323
    case OPC_ORI:
1324
        tcg_gen_ori_tl(t0, t0, uimm);
1325
        opn = "ori";
1326
        break;
1327
    case OPC_XORI:
1328
        tcg_gen_xori_tl(t0, t0, uimm);
1329
        opn = "xori";
1330
        break;
1331
    case OPC_LUI:
1332
        opn = "lui";
1333
        break;
1334
    case OPC_SLL:
1335
        tcg_gen_shli_tl(t0, t0, uimm);
1336
        tcg_gen_ext32s_tl(t0, t0);
1337
        opn = "sll";
1338
        break;
1339
    case OPC_SRA:
1340
        tcg_gen_ext32s_tl(t0, t0);
1341
        tcg_gen_sari_tl(t0, t0, uimm);
1342
        opn = "sra";
1343
        break;
1344
    case OPC_SRL:
1345
        switch ((ctx->opcode >> 21) & 0x1f) {
1346
        case 0:
1347
            if (uimm != 0) {
1348
                tcg_gen_ext32u_tl(t0, t0);
1349
                tcg_gen_shri_tl(t0, t0, uimm);
1350
            } else {
1351
                tcg_gen_ext32s_tl(t0, t0);
1352
            }
1353
            opn = "srl";
1354
            break;
1355
        case 1:
1356
            /* rotr is decoded as srl on non-R2 CPUs */
1357
            if (env->insn_flags & ISA_MIPS32R2) {
1358
                if (uimm != 0) {
1359
                    TCGv_i32 r_tmp1 = tcg_temp_new_i32();
1360

    
1361
                    tcg_gen_trunc_tl_i32(r_tmp1, t0);
1362
                    tcg_gen_rotri_i32(r_tmp1, r_tmp1, uimm);
1363
                    tcg_gen_ext_i32_tl(t0, r_tmp1);
1364
                    tcg_temp_free_i32(r_tmp1);
1365
                }
1366
                opn = "rotr";
1367
            } else {
1368
                if (uimm != 0) {
1369
                    tcg_gen_ext32u_tl(t0, t0);
1370
                    tcg_gen_shri_tl(t0, t0, uimm);
1371
                } else {
1372
                    tcg_gen_ext32s_tl(t0, t0);
1373
                }
1374
                opn = "srl";
1375
            }
1376
            break;
1377
        default:
1378
            MIPS_INVAL("invalid srl flag");
1379
            generate_exception(ctx, EXCP_RI);
1380
            break;
1381
        }
1382
        break;
1383
#if defined(TARGET_MIPS64)
1384
    case OPC_DSLL:
1385
        tcg_gen_shli_tl(t0, t0, uimm);
1386
        opn = "dsll";
1387
        break;
1388
    case OPC_DSRA:
1389
        tcg_gen_sari_tl(t0, t0, uimm);
1390
        opn = "dsra";
1391
        break;
1392
    case OPC_DSRL:
1393
        switch ((ctx->opcode >> 21) & 0x1f) {
1394
        case 0:
1395
            tcg_gen_shri_tl(t0, t0, uimm);
1396
            opn = "dsrl";
1397
            break;
1398
        case 1:
1399
            /* drotr is decoded as dsrl on non-R2 CPUs */
1400
            if (env->insn_flags & ISA_MIPS32R2) {
1401
                if (uimm != 0) {
1402
                    tcg_gen_rotri_tl(t0, t0, uimm);
1403
                }
1404
                opn = "drotr";
1405
            } else {
1406
                tcg_gen_shri_tl(t0, t0, uimm);
1407
                opn = "dsrl";
1408
            }
1409
            break;
1410
        default:
1411
            MIPS_INVAL("invalid dsrl flag");
1412
            generate_exception(ctx, EXCP_RI);
1413
            break;
1414
        }
1415
        break;
1416
    case OPC_DSLL32:
1417
        tcg_gen_shli_tl(t0, t0, uimm + 32);
1418
        opn = "dsll32";
1419
        break;
1420
    case OPC_DSRA32:
1421
        tcg_gen_sari_tl(t0, t0, uimm + 32);
1422
        opn = "dsra32";
1423
        break;
1424
    case OPC_DSRL32:
1425
        switch ((ctx->opcode >> 21) & 0x1f) {
1426
        case 0:
1427
            tcg_gen_shri_tl(t0, t0, uimm + 32);
1428
            opn = "dsrl32";
1429
            break;
1430
        case 1:
1431
            /* drotr32 is decoded as dsrl32 on non-R2 CPUs */
1432
            if (env->insn_flags & ISA_MIPS32R2) {
1433
                tcg_gen_rotri_tl(t0, t0, uimm + 32);
1434
                opn = "drotr32";
1435
            } else {
1436
                tcg_gen_shri_tl(t0, t0, uimm + 32);
1437
                opn = "dsrl32";
1438
            }
1439
            break;
1440
        default:
1441
            MIPS_INVAL("invalid dsrl32 flag");
1442
            generate_exception(ctx, EXCP_RI);
1443
            break;
1444
        }
1445
        break;
1446
#endif
1447
    default:
1448
        MIPS_INVAL(opn);
1449
        generate_exception(ctx, EXCP_RI);
1450
        goto out;
1451
    }
1452
    gen_store_gpr(t0, rt);
1453
    MIPS_DEBUG("%s %s, %s, " TARGET_FMT_lx, opn, regnames[rt], regnames[rs], uimm);
1454
 out:
1455
    tcg_temp_free(t0);
1456
}
1457

    
1458
/* Arithmetic */
1459
static void gen_arith (CPUState *env, DisasContext *ctx, uint32_t opc,
1460
                       int rd, int rs, int rt)
1461
{
1462
    const char *opn = "arith";
1463
    TCGv t0 = tcg_temp_local_new();
1464
    TCGv t1 = tcg_temp_local_new();
1465

    
1466
    if (rd == 0 && opc != OPC_ADD && opc != OPC_SUB
1467
       && opc != OPC_DADD && opc != OPC_DSUB) {
1468
        /* If no destination, treat it as a NOP.
1469
           For add & sub, we must generate the overflow exception when needed. */
1470
        MIPS_DEBUG("NOP");
1471
        goto out;
1472
    }
1473
    gen_load_gpr(t0, rs);
1474
    /* Specialcase the conventional move operation. */
1475
    if (rt == 0 && (opc == OPC_ADDU || opc == OPC_DADDU
1476
                    || opc == OPC_SUBU || opc == OPC_DSUBU)) {
1477
        gen_store_gpr(t0, rd);
1478
        goto out;
1479
    }
1480
    gen_load_gpr(t1, rt);
1481
    switch (opc) {
1482
    case OPC_ADD:
1483
        {
1484
            TCGv r_tmp1 = tcg_temp_new();
1485
            TCGv r_tmp2 = tcg_temp_new();
1486
            int l1 = gen_new_label();
1487

    
1488
            save_cpu_state(ctx, 1);
1489
            tcg_gen_ext32s_tl(r_tmp1, t0);
1490
            tcg_gen_ext32s_tl(r_tmp2, t1);
1491
            tcg_gen_add_tl(t0, r_tmp1, r_tmp2);
1492

    
1493
            tcg_gen_xor_tl(r_tmp1, r_tmp1, t1);
1494
            tcg_gen_xori_tl(r_tmp1, r_tmp1, -1);
1495
            tcg_gen_xor_tl(r_tmp2, t0, t1);
1496
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
1497
            tcg_temp_free(r_tmp2);
1498
            tcg_gen_brcondi_tl(TCG_COND_GE, r_tmp1, 0, l1);
1499
            /* operands of same sign, result different sign */
1500
            generate_exception(ctx, EXCP_OVERFLOW);
1501
            gen_set_label(l1);
1502
            tcg_temp_free(r_tmp1);
1503

    
1504
            tcg_gen_ext32s_tl(t0, t0);
1505
        }
1506
        opn = "add";
1507
        break;
1508
    case OPC_ADDU:
1509
        tcg_gen_add_tl(t0, t0, t1);
1510
        tcg_gen_ext32s_tl(t0, t0);
1511
        opn = "addu";
1512
        break;
1513
    case OPC_SUB:
1514
        {
1515
            TCGv r_tmp1 = tcg_temp_new();
1516
            TCGv r_tmp2 = tcg_temp_new();
1517
            int l1 = gen_new_label();
1518

    
1519
            save_cpu_state(ctx, 1);
1520
            tcg_gen_ext32s_tl(r_tmp1, t0);
1521
            tcg_gen_ext32s_tl(r_tmp2, t1);
1522
            tcg_gen_sub_tl(t0, r_tmp1, r_tmp2);
1523

    
1524
            tcg_gen_xor_tl(r_tmp2, r_tmp1, t1);
1525
            tcg_gen_xor_tl(r_tmp1, r_tmp1, t0);
1526
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
1527
            tcg_temp_free(r_tmp2);
1528
            tcg_gen_brcondi_tl(TCG_COND_GE, r_tmp1, 0, l1);
1529
            /* operands of different sign, first operand and result different sign */
1530
            generate_exception(ctx, EXCP_OVERFLOW);
1531
            gen_set_label(l1);
1532
            tcg_temp_free(r_tmp1);
1533

    
1534
            tcg_gen_ext32s_tl(t0, t0);
1535
        }
1536
        opn = "sub";
1537
        break;
1538
    case OPC_SUBU:
1539
        tcg_gen_sub_tl(t0, t0, t1);
1540
        tcg_gen_ext32s_tl(t0, t0);
1541
        opn = "subu";
1542
        break;
1543
#if defined(TARGET_MIPS64)
1544
    case OPC_DADD:
1545
        {
1546
            TCGv r_tmp1 = tcg_temp_new();
1547
            TCGv r_tmp2 = tcg_temp_new();
1548
            int l1 = gen_new_label();
1549

    
1550
            save_cpu_state(ctx, 1);
1551
            tcg_gen_mov_tl(r_tmp1, t0);
1552
            tcg_gen_add_tl(t0, t0, t1);
1553

    
1554
            tcg_gen_xor_tl(r_tmp1, r_tmp1, t1);
1555
            tcg_gen_xori_tl(r_tmp1, r_tmp1, -1);
1556
            tcg_gen_xor_tl(r_tmp2, t0, t1);
1557
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
1558
            tcg_temp_free(r_tmp2);
1559
            tcg_gen_brcondi_tl(TCG_COND_GE, r_tmp1, 0, l1);
1560
            /* operands of same sign, result different sign */
1561
            generate_exception(ctx, EXCP_OVERFLOW);
1562
            gen_set_label(l1);
1563
            tcg_temp_free(r_tmp1);
1564
        }
1565
        opn = "dadd";
1566
        break;
1567
    case OPC_DADDU:
1568
        tcg_gen_add_tl(t0, t0, t1);
1569
        opn = "daddu";
1570
        break;
1571
    case OPC_DSUB:
1572
        {
1573
            TCGv r_tmp1 = tcg_temp_new();
1574
            TCGv r_tmp2 = tcg_temp_new();
1575
            int l1 = gen_new_label();
1576

    
1577
            save_cpu_state(ctx, 1);
1578
            tcg_gen_mov_tl(r_tmp1, t0);
1579
            tcg_gen_sub_tl(t0, t0, t1);
1580

    
1581
            tcg_gen_xor_tl(r_tmp2, r_tmp1, t1);
1582
            tcg_gen_xor_tl(r_tmp1, r_tmp1, t0);
1583
            tcg_gen_and_tl(r_tmp1, r_tmp1, r_tmp2);
1584
            tcg_temp_free(r_tmp2);
1585
            tcg_gen_brcondi_tl(TCG_COND_GE, r_tmp1, 0, l1);
1586
            /* operands of different sign, first operand and result different sign */
1587
            generate_exception(ctx, EXCP_OVERFLOW);
1588
            gen_set_label(l1);
1589
            tcg_temp_free(r_tmp1);
1590
        }
1591
        opn = "dsub";
1592
        break;
1593
    case OPC_DSUBU:
1594
        tcg_gen_sub_tl(t0, t0, t1);
1595
        opn = "dsubu";
1596
        break;
1597
#endif
1598
    case OPC_SLT:
1599
        gen_op_lt(t0, t1);
1600
        opn = "slt";
1601
        break;
1602
    case OPC_SLTU:
1603
        gen_op_ltu(t0, t1);
1604
        opn = "sltu";
1605
        break;
1606
    case OPC_AND:
1607
        tcg_gen_and_tl(t0, t0, t1);
1608
        opn = "and";
1609
        break;
1610
    case OPC_NOR:
1611
        tcg_gen_nor_tl(t0, t0, t1);
1612
        opn = "nor";
1613
        break;
1614
    case OPC_OR:
1615
        tcg_gen_or_tl(t0, t0, t1);
1616
        opn = "or";
1617
        break;
1618
    case OPC_XOR:
1619
        tcg_gen_xor_tl(t0, t0, t1);
1620
        opn = "xor";
1621
        break;
1622
    case OPC_MUL:
1623
        tcg_gen_mul_tl(t0, t0, t1);
1624
        tcg_gen_ext32s_tl(t0, t0);
1625
        opn = "mul";
1626
        break;
1627
    case OPC_MOVN:
1628
        {
1629
            int l1 = gen_new_label();
1630

    
1631
            tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
1632
            gen_store_gpr(t0, rd);
1633
            gen_set_label(l1);
1634
        }
1635
        opn = "movn";
1636
        goto print;
1637
    case OPC_MOVZ:
1638
        {
1639
            int l1 = gen_new_label();
1640

    
1641
            tcg_gen_brcondi_tl(TCG_COND_NE, t1, 0, l1);
1642
            gen_store_gpr(t0, rd);
1643
            gen_set_label(l1);
1644
        }
1645
        opn = "movz";
1646
        goto print;
1647
    case OPC_SLLV:
1648
        tcg_gen_andi_tl(t0, t0, 0x1f);
1649
        tcg_gen_shl_tl(t0, t1, t0);
1650
        tcg_gen_ext32s_tl(t0, t0);
1651
        opn = "sllv";
1652
        break;
1653
    case OPC_SRAV:
1654
        tcg_gen_ext32s_tl(t1, t1);
1655
        tcg_gen_andi_tl(t0, t0, 0x1f);
1656
        tcg_gen_sar_tl(t0, t1, t0);
1657
        opn = "srav";
1658
        break;
1659
    case OPC_SRLV:
1660
        switch ((ctx->opcode >> 6) & 0x1f) {
1661
        case 0:
1662
            tcg_gen_ext32u_tl(t1, t1);
1663
            tcg_gen_andi_tl(t0, t0, 0x1f);
1664
            tcg_gen_shr_tl(t0, t1, t0);
1665
            tcg_gen_ext32s_tl(t0, t0);
1666
            opn = "srlv";
1667
            break;
1668
        case 1:
1669
            /* rotrv is decoded as srlv on non-R2 CPUs */
1670
            if (env->insn_flags & ISA_MIPS32R2) {
1671
                int l1 = gen_new_label();
1672
                int l2 = gen_new_label();
1673

    
1674
                tcg_gen_andi_tl(t0, t0, 0x1f);
1675
                tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
1676
                {
1677
                    TCGv_i32 r_tmp1 = tcg_temp_new_i32();
1678
                    TCGv_i32 r_tmp2 = tcg_temp_new_i32();
1679

    
1680
                    tcg_gen_trunc_tl_i32(r_tmp1, t0);
1681
                    tcg_gen_trunc_tl_i32(r_tmp2, t1);
1682
                    tcg_gen_rotr_i32(r_tmp1, r_tmp1, r_tmp2);
1683
                    tcg_temp_free_i32(r_tmp1);
1684
                    tcg_temp_free_i32(r_tmp2);
1685
                    tcg_gen_br(l2);
1686
                }
1687
                gen_set_label(l1);
1688
                tcg_gen_mov_tl(t0, t1);
1689
                gen_set_label(l2);
1690
                opn = "rotrv";
1691
            } else {
1692
                tcg_gen_ext32u_tl(t1, t1);
1693
                tcg_gen_andi_tl(t0, t0, 0x1f);
1694
                tcg_gen_shr_tl(t0, t1, t0);
1695
                tcg_gen_ext32s_tl(t0, t0);
1696
                opn = "srlv";
1697
            }
1698
            break;
1699
        default:
1700
            MIPS_INVAL("invalid srlv flag");
1701
            generate_exception(ctx, EXCP_RI);
1702
            break;
1703
        }
1704
        break;
1705
#if defined(TARGET_MIPS64)
1706
    case OPC_DSLLV:
1707
        tcg_gen_andi_tl(t0, t0, 0x3f);
1708
        tcg_gen_shl_tl(t0, t1, t0);
1709
        opn = "dsllv";
1710
        break;
1711
    case OPC_DSRAV:
1712
        tcg_gen_andi_tl(t0, t0, 0x3f);
1713
        tcg_gen_sar_tl(t0, t1, t0);
1714
        opn = "dsrav";
1715
        break;
1716
    case OPC_DSRLV:
1717
        switch ((ctx->opcode >> 6) & 0x1f) {
1718
        case 0:
1719
            tcg_gen_andi_tl(t0, t0, 0x3f);
1720
            tcg_gen_shr_tl(t0, t1, t0);
1721
            opn = "dsrlv";
1722
            break;
1723
        case 1:
1724
            /* drotrv is decoded as dsrlv 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(t0, t0, 0x3f);
1730
                tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
1731
                {
1732
                    tcg_gen_rotr_tl(t0, t1, t0);
1733
                    tcg_gen_br(l2);
1734
                }
1735
                gen_set_label(l1);
1736
                tcg_gen_mov_tl(t0, t1);
1737
                gen_set_label(l2);
1738
                opn = "drotrv";
1739
            } else {
1740
                tcg_gen_andi_tl(t0, t0, 0x3f);
1741
                tcg_gen_shr_tl(t0, t1, t0);
1742
                opn = "dsrlv";
1743
            }
1744
            break;
1745
        default:
1746
            MIPS_INVAL("invalid dsrlv flag");
1747
            generate_exception(ctx, EXCP_RI);
1748
            break;
1749
        }
1750
        break;
1751
#endif
1752
    default:
1753
        MIPS_INVAL(opn);
1754
        generate_exception(ctx, EXCP_RI);
1755
        goto out;
1756
    }
1757
    gen_store_gpr(t0, rd);
1758
 print:
1759
    MIPS_DEBUG("%s %s, %s, %s", opn, regnames[rd], regnames[rs], regnames[rt]);
1760
 out:
1761
    tcg_temp_free(t0);
1762
    tcg_temp_free(t1);
1763
}
1764

    
1765
/* Arithmetic on HI/LO registers */
1766
static void gen_HILO (DisasContext *ctx, uint32_t opc, int reg)
1767
{
1768
    const char *opn = "hilo";
1769

    
1770
    if (reg == 0 && (opc == OPC_MFHI || opc == OPC_MFLO)) {
1771
        /* Treat as NOP. */
1772
        MIPS_DEBUG("NOP");
1773
        return;
1774
    }
1775
    switch (opc) {
1776
    case OPC_MFHI:
1777
        tcg_gen_mov_tl(cpu_gpr[reg], cpu_HI[0]);
1778
        opn = "mfhi";
1779
        break;
1780
    case OPC_MFLO:
1781
        tcg_gen_mov_tl(cpu_gpr[reg], cpu_LO[0]);
1782
        opn = "mflo";
1783
        break;
1784
    case OPC_MTHI:
1785
        if (reg != 0)
1786
            tcg_gen_mov_tl(cpu_HI[0], cpu_gpr[reg]);
1787
        else
1788
            tcg_gen_movi_tl(cpu_HI[0], 0);
1789
        opn = "mthi";
1790
        break;
1791
    case OPC_MTLO:
1792
        if (reg != 0)
1793
            tcg_gen_mov_tl(cpu_LO[0], cpu_gpr[reg]);
1794
        else
1795
            tcg_gen_movi_tl(cpu_LO[0], 0);
1796
        opn = "mtlo";
1797
        break;
1798
    default:
1799
        MIPS_INVAL(opn);
1800
        generate_exception(ctx, EXCP_RI);
1801
        return;
1802
    }
1803
    MIPS_DEBUG("%s %s", opn, regnames[reg]);
1804
}
1805

    
1806
static void gen_muldiv (DisasContext *ctx, uint32_t opc,
1807
                        int rs, int rt)
1808
{
1809
    const char *opn = "mul/div";
1810
    TCGv t0 = tcg_temp_local_new();
1811
    TCGv t1 = tcg_temp_local_new();
1812

    
1813
    gen_load_gpr(t0, rs);
1814
    gen_load_gpr(t1, rt);
1815
    switch (opc) {
1816
    case OPC_DIV:
1817
        {
1818
            int l1 = gen_new_label();
1819

    
1820
            tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
1821
            {
1822
                int l2 = gen_new_label();
1823
                TCGv_i32 r_tmp1 = tcg_temp_local_new_i32();
1824
                TCGv_i32 r_tmp2 = tcg_temp_local_new_i32();
1825
                TCGv_i32 r_tmp3 = tcg_temp_local_new_i32();
1826

    
1827
                tcg_gen_trunc_tl_i32(r_tmp1, t0);
1828
                tcg_gen_trunc_tl_i32(r_tmp2, t1);
1829
                tcg_gen_brcondi_i32(TCG_COND_NE, r_tmp1, -1 << 31, l2);
1830
                tcg_gen_brcondi_i32(TCG_COND_NE, r_tmp2, -1, l2);
1831
                tcg_gen_ext32s_tl(cpu_LO[0], t0);
1832
                tcg_gen_movi_tl(cpu_HI[0], 0);
1833
                tcg_gen_br(l1);
1834
                gen_set_label(l2);
1835
                tcg_gen_div_i32(r_tmp3, r_tmp1, r_tmp2);
1836
                tcg_gen_rem_i32(r_tmp2, r_tmp1, r_tmp2);
1837
                tcg_gen_ext_i32_tl(cpu_LO[0], r_tmp3);
1838
                tcg_gen_ext_i32_tl(cpu_HI[0], r_tmp2);
1839
                tcg_temp_free_i32(r_tmp1);
1840
                tcg_temp_free_i32(r_tmp2);
1841
                tcg_temp_free_i32(r_tmp3);
1842
            }
1843
            gen_set_label(l1);
1844
        }
1845
        opn = "div";
1846
        break;
1847
    case OPC_DIVU:
1848
        {
1849
            int l1 = gen_new_label();
1850

    
1851
            tcg_gen_ext32s_tl(t1, t1);
1852
            tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
1853
            {
1854
                TCGv_i32 r_tmp1 = tcg_temp_new_i32();
1855
                TCGv_i32 r_tmp2 = tcg_temp_new_i32();
1856
                TCGv_i32 r_tmp3 = tcg_temp_new_i32();
1857

    
1858
                tcg_gen_trunc_tl_i32(r_tmp1, t0);
1859
                tcg_gen_trunc_tl_i32(r_tmp2, t1);
1860
                tcg_gen_divu_i32(r_tmp3, r_tmp1, r_tmp2);
1861
                tcg_gen_remu_i32(r_tmp1, r_tmp1, r_tmp2);
1862
                tcg_gen_ext_i32_tl(cpu_LO[0], r_tmp3);
1863
                tcg_gen_ext_i32_tl(cpu_HI[0], r_tmp1);
1864
                tcg_temp_free_i32(r_tmp1);
1865
                tcg_temp_free_i32(r_tmp2);
1866
                tcg_temp_free_i32(r_tmp3);
1867
            }
1868
            gen_set_label(l1);
1869
        }
1870
        opn = "divu";
1871
        break;
1872
    case OPC_MULT:
1873
        {
1874
            TCGv_i64 r_tmp1 = tcg_temp_new_i64();
1875
            TCGv_i64 r_tmp2 = tcg_temp_new_i64();
1876

    
1877
            tcg_gen_ext_tl_i64(r_tmp1, t0);
1878
            tcg_gen_ext_tl_i64(r_tmp2, t1);
1879
            tcg_gen_mul_i64(r_tmp1, r_tmp1, r_tmp2);
1880
            tcg_temp_free_i64(r_tmp2);
1881
            tcg_gen_trunc_i64_tl(t0, r_tmp1);
1882
            tcg_gen_shri_i64(r_tmp1, r_tmp1, 32);
1883
            tcg_gen_trunc_i64_tl(t1, r_tmp1);
1884
            tcg_temp_free_i64(r_tmp1);
1885
            tcg_gen_ext32s_tl(cpu_LO[0], t0);
1886
            tcg_gen_ext32s_tl(cpu_HI[0], t1);
1887
        }
1888
        opn = "mult";
1889
        break;
1890
    case OPC_MULTU:
1891
        {
1892
            TCGv_i64 r_tmp1 = tcg_temp_new_i64();
1893
            TCGv_i64 r_tmp2 = tcg_temp_new_i64();
1894

    
1895
            tcg_gen_ext32u_tl(t0, t0);
1896
            tcg_gen_ext32u_tl(t1, t1);
1897
            tcg_gen_extu_tl_i64(r_tmp1, t0);
1898
            tcg_gen_extu_tl_i64(r_tmp2, t1);
1899
            tcg_gen_mul_i64(r_tmp1, r_tmp1, r_tmp2);
1900
            tcg_temp_free_i64(r_tmp2);
1901
            tcg_gen_trunc_i64_tl(t0, r_tmp1);
1902
            tcg_gen_shri_i64(r_tmp1, r_tmp1, 32);
1903
            tcg_gen_trunc_i64_tl(t1, r_tmp1);
1904
            tcg_temp_free_i64(r_tmp1);
1905
            tcg_gen_ext32s_tl(cpu_LO[0], t0);
1906
            tcg_gen_ext32s_tl(cpu_HI[0], t1);
1907
        }
1908
        opn = "multu";
1909
        break;
1910
#if defined(TARGET_MIPS64)
1911
    case OPC_DDIV:
1912
        {
1913
            int l1 = gen_new_label();
1914

    
1915
            tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
1916
            {
1917
                int l2 = gen_new_label();
1918

    
1919
                tcg_gen_brcondi_tl(TCG_COND_NE, t0, -1LL << 63, l2);
1920
                tcg_gen_brcondi_tl(TCG_COND_NE, t1, -1LL, l2);
1921
                tcg_gen_mov_tl(cpu_LO[0], t0);
1922
                tcg_gen_movi_tl(cpu_HI[0], 0);
1923
                tcg_gen_br(l1);
1924
                gen_set_label(l2);
1925
                tcg_gen_div_i64(cpu_LO[0], t0, t1);
1926
                tcg_gen_rem_i64(cpu_HI[0], t0, t1);
1927
            }
1928
            gen_set_label(l1);
1929
        }
1930
        opn = "ddiv";
1931
        break;
1932
    case OPC_DDIVU:
1933
        {
1934
            int l1 = gen_new_label();
1935

    
1936
            tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
1937
            tcg_gen_divu_i64(cpu_LO[0], t0, t1);
1938
            tcg_gen_remu_i64(cpu_HI[0], t0, t1);
1939
            gen_set_label(l1);
1940
        }
1941
        opn = "ddivu";
1942
        break;
1943
    case OPC_DMULT:
1944
        gen_helper_dmult(t0, t1);
1945
        opn = "dmult";
1946
        break;
1947
    case OPC_DMULTU:
1948
        gen_helper_dmultu(t0, t1);
1949
        opn = "dmultu";
1950
        break;
1951
#endif
1952
    case OPC_MADD:
1953
        {
1954
            TCGv_i64 r_tmp1 = tcg_temp_new_i64();
1955
            TCGv_i64 r_tmp2 = tcg_temp_new_i64();
1956

    
1957
            tcg_gen_ext_tl_i64(r_tmp1, t0);
1958
            tcg_gen_ext_tl_i64(r_tmp2, t1);
1959
            tcg_gen_mul_i64(r_tmp1, r_tmp1, r_tmp2);
1960
            tcg_gen_concat_tl_i64(r_tmp2, cpu_LO[0], cpu_HI[0]);
1961
            tcg_gen_add_i64(r_tmp1, r_tmp1, r_tmp2);
1962
            tcg_temp_free_i64(r_tmp2);
1963
            tcg_gen_trunc_i64_tl(t0, r_tmp1);
1964
            tcg_gen_shri_i64(r_tmp1, r_tmp1, 32);
1965
            tcg_gen_trunc_i64_tl(t1, r_tmp1);
1966
            tcg_temp_free_i64(r_tmp1);
1967
            tcg_gen_ext32s_tl(cpu_LO[0], t0);
1968
            tcg_gen_ext32s_tl(cpu_LO[1], t1);
1969
        }
1970
        opn = "madd";
1971
        break;
1972
    case OPC_MADDU:
1973
       {
1974
            TCGv_i64 r_tmp1 = tcg_temp_new_i64();
1975
            TCGv_i64 r_tmp2 = tcg_temp_new_i64();
1976

    
1977
            tcg_gen_ext32u_tl(t0, t0);
1978
            tcg_gen_ext32u_tl(t1, t1);
1979
            tcg_gen_extu_tl_i64(r_tmp1, t0);
1980
            tcg_gen_extu_tl_i64(r_tmp2, t1);
1981
            tcg_gen_mul_i64(r_tmp1, r_tmp1, r_tmp2);
1982
            tcg_gen_concat_tl_i64(r_tmp2, cpu_LO[0], cpu_HI[0]);
1983
            tcg_gen_add_i64(r_tmp1, r_tmp1, r_tmp2);
1984
            tcg_temp_free_i64(r_tmp2);
1985
            tcg_gen_trunc_i64_tl(t0, r_tmp1);
1986
            tcg_gen_shri_i64(r_tmp1, r_tmp1, 32);
1987
            tcg_gen_trunc_i64_tl(t1, r_tmp1);
1988
            tcg_temp_free_i64(r_tmp1);
1989
            tcg_gen_ext32s_tl(cpu_LO[0], t0);
1990
            tcg_gen_ext32s_tl(cpu_HI[0], t1);
1991
        }
1992
        opn = "maddu";
1993
        break;
1994
    case OPC_MSUB:
1995
        {
1996
            TCGv_i64 r_tmp1 = tcg_temp_new_i64();
1997
            TCGv_i64 r_tmp2 = tcg_temp_new_i64();
1998

    
1999
            tcg_gen_ext_tl_i64(r_tmp1, t0);
2000
            tcg_gen_ext_tl_i64(r_tmp2, t1);
2001
            tcg_gen_mul_i64(r_tmp1, r_tmp1, r_tmp2);
2002
            tcg_gen_concat_tl_i64(r_tmp2, cpu_LO[0], cpu_HI[0]);
2003
            tcg_gen_sub_i64(r_tmp1, r_tmp1, r_tmp2);
2004
            tcg_temp_free_i64(r_tmp2);
2005
            tcg_gen_trunc_i64_tl(t0, r_tmp1);
2006
            tcg_gen_shri_i64(r_tmp1, r_tmp1, 32);
2007
            tcg_gen_trunc_i64_tl(t1, r_tmp1);
2008
            tcg_temp_free_i64(r_tmp1);
2009
            tcg_gen_ext32s_tl(cpu_LO[0], t0);
2010
            tcg_gen_ext32s_tl(cpu_HI[0], t1);
2011
        }
2012
        opn = "msub";
2013
        break;
2014
    case OPC_MSUBU:
2015
        {
2016
            TCGv_i64 r_tmp1 = tcg_temp_new_i64();
2017
            TCGv_i64 r_tmp2 = tcg_temp_new_i64();
2018

    
2019
            tcg_gen_ext32u_tl(t0, t0);
2020
            tcg_gen_ext32u_tl(t1, t1);
2021
            tcg_gen_extu_tl_i64(r_tmp1, t0);
2022
            tcg_gen_extu_tl_i64(r_tmp2, t1);
2023
            tcg_gen_mul_i64(r_tmp1, r_tmp1, r_tmp2);
2024
            tcg_gen_concat_tl_i64(r_tmp2, cpu_LO[0], cpu_HI[0]);
2025
            tcg_gen_sub_i64(r_tmp1, r_tmp1, r_tmp2);
2026
            tcg_temp_free_i64(r_tmp2);
2027
            tcg_gen_trunc_i64_tl(t0, r_tmp1);
2028
            tcg_gen_shri_i64(r_tmp1, r_tmp1, 32);
2029
            tcg_gen_trunc_i64_tl(t1, r_tmp1);
2030
            tcg_temp_free_i64(r_tmp1);
2031
            tcg_gen_ext32s_tl(cpu_LO[0], t0);
2032
            tcg_gen_ext32s_tl(cpu_HI[0], t1);
2033
        }
2034
        opn = "msubu";
2035
        break;
2036
    default:
2037
        MIPS_INVAL(opn);
2038
        generate_exception(ctx, EXCP_RI);
2039
        goto out;
2040
    }
2041
    MIPS_DEBUG("%s %s %s", opn, regnames[rs], regnames[rt]);
2042
 out:
2043
    tcg_temp_free(t0);
2044
    tcg_temp_free(t1);
2045
}
2046

    
2047
static void gen_mul_vr54xx (DisasContext *ctx, uint32_t opc,
2048
                            int rd, int rs, int rt)
2049
{
2050
    const char *opn = "mul vr54xx";
2051
    TCGv t0 = tcg_temp_local_new();
2052
    TCGv t1 = tcg_temp_local_new();
2053

    
2054
    gen_load_gpr(t0, rs);
2055
    gen_load_gpr(t1, rt);
2056

    
2057
    switch (opc) {
2058
    case OPC_VR54XX_MULS:
2059
        gen_helper_muls(t0, t0, t1);
2060
        opn = "muls";
2061
        break;
2062
    case OPC_VR54XX_MULSU:
2063
        gen_helper_mulsu(t0, t0, t1);
2064
        opn = "mulsu";
2065
        break;
2066
    case OPC_VR54XX_MACC:
2067
        gen_helper_macc(t0, t0, t1);
2068
        opn = "macc";
2069
        break;
2070
    case OPC_VR54XX_MACCU:
2071
        gen_helper_maccu(t0, t0, t1);
2072
        opn = "maccu";
2073
        break;
2074
    case OPC_VR54XX_MSAC:
2075
        gen_helper_msac(t0, t0, t1);
2076
        opn = "msac";
2077
        break;
2078
    case OPC_VR54XX_MSACU:
2079
        gen_helper_msacu(t0, t0, t1);
2080
        opn = "msacu";
2081
        break;
2082
    case OPC_VR54XX_MULHI:
2083
        gen_helper_mulhi(t0, t0, t1);
2084
        opn = "mulhi";
2085
        break;
2086
    case OPC_VR54XX_MULHIU:
2087
        gen_helper_mulhiu(t0, t0, t1);
2088
        opn = "mulhiu";
2089
        break;
2090
    case OPC_VR54XX_MULSHI:
2091
        gen_helper_mulshi(t0, t0, t1);
2092
        opn = "mulshi";
2093
        break;
2094
    case OPC_VR54XX_MULSHIU:
2095
        gen_helper_mulshiu(t0, t0, t1);
2096
        opn = "mulshiu";
2097
        break;
2098
    case OPC_VR54XX_MACCHI:
2099
        gen_helper_macchi(t0, t0, t1);
2100
        opn = "macchi";
2101
        break;
2102
    case OPC_VR54XX_MACCHIU:
2103
        gen_helper_macchiu(t0, t0, t1);
2104
        opn = "macchiu";
2105
        break;
2106
    case OPC_VR54XX_MSACHI:
2107
        gen_helper_msachi(t0, t0, t1);
2108
        opn = "msachi";
2109
        break;
2110
    case OPC_VR54XX_MSACHIU:
2111
        gen_helper_msachiu(t0, t0, t1);
2112
        opn = "msachiu";
2113
        break;
2114
    default:
2115
        MIPS_INVAL("mul vr54xx");
2116
        generate_exception(ctx, EXCP_RI);
2117
        goto out;
2118
    }
2119
    gen_store_gpr(t0, rd);
2120
    MIPS_DEBUG("%s %s, %s, %s", opn, regnames[rd], regnames[rs], regnames[rt]);
2121

    
2122
 out:
2123
    tcg_temp_free(t0);
2124
    tcg_temp_free(t1);
2125
}
2126

    
2127
static void gen_cl (DisasContext *ctx, uint32_t opc,
2128
                    int rd, int rs)
2129
{
2130
    const char *opn = "CLx";
2131
    TCGv t0;
2132

    
2133
    if (rd == 0) {
2134
        /* Treat as NOP. */
2135
        MIPS_DEBUG("NOP");
2136
        return;
2137
    }
2138
    t0 = tcg_temp_new();
2139
    gen_load_gpr(t0, rs);
2140
    switch (opc) {
2141
    case OPC_CLO:
2142
        gen_helper_clo(cpu_gpr[rd], t0);
2143
        opn = "clo";
2144
        break;
2145
    case OPC_CLZ:
2146
        gen_helper_clz(cpu_gpr[rd], t0);
2147
        opn = "clz";
2148
        break;
2149
#if defined(TARGET_MIPS64)
2150
    case OPC_DCLO:
2151
        gen_helper_dclo(cpu_gpr[rd], t0);
2152
        opn = "dclo";
2153
        break;
2154
    case OPC_DCLZ:
2155
        gen_helper_dclz(cpu_gpr[rd], t0);
2156
        opn = "dclz";
2157
        break;
2158
#endif
2159
    }
2160
    MIPS_DEBUG("%s %s, %s", opn, regnames[rd], regnames[rs]);
2161
    tcg_temp_free(t0);
2162
}
2163

    
2164
/* Traps */
2165
static void gen_trap (DisasContext *ctx, uint32_t opc,
2166
                      int rs, int rt, int16_t imm)
2167
{
2168
    int cond;
2169
    TCGv t0 = tcg_temp_local_new();
2170
    TCGv t1 = tcg_temp_local_new();
2171

    
2172
    cond = 0;
2173
    /* Load needed operands */
2174
    switch (opc) {
2175
    case OPC_TEQ:
2176
    case OPC_TGE:
2177
    case OPC_TGEU:
2178
    case OPC_TLT:
2179
    case OPC_TLTU:
2180
    case OPC_TNE:
2181
        /* Compare two registers */
2182
        if (rs != rt) {
2183
            gen_load_gpr(t0, rs);
2184
            gen_load_gpr(t1, rt);
2185
            cond = 1;
2186
        }
2187
        break;
2188
    case OPC_TEQI:
2189
    case OPC_TGEI:
2190
    case OPC_TGEIU:
2191
    case OPC_TLTI:
2192
    case OPC_TLTIU:
2193
    case OPC_TNEI:
2194
        /* Compare register to immediate */
2195
        if (rs != 0 || imm != 0) {
2196
            gen_load_gpr(t0, rs);
2197
            tcg_gen_movi_tl(t1, (int32_t)imm);
2198
            cond = 1;
2199
        }
2200
        break;
2201
    }
2202
    if (cond == 0) {
2203
        switch (opc) {
2204
        case OPC_TEQ:   /* rs == rs */
2205
        case OPC_TEQI:  /* r0 == 0  */
2206
        case OPC_TGE:   /* rs >= rs */
2207
        case OPC_TGEI:  /* r0 >= 0  */
2208
        case OPC_TGEU:  /* rs >= rs unsigned */
2209
        case OPC_TGEIU: /* r0 >= 0  unsigned */
2210
            /* Always trap */
2211
            tcg_gen_movi_tl(t0, 1);
2212
            break;
2213
        case OPC_TLT:   /* rs < rs           */
2214
        case OPC_TLTI:  /* r0 < 0            */
2215
        case OPC_TLTU:  /* rs < rs unsigned  */
2216
        case OPC_TLTIU: /* r0 < 0  unsigned  */
2217
        case OPC_TNE:   /* rs != rs          */
2218
        case OPC_TNEI:  /* r0 != 0           */
2219
            /* Never trap: treat as NOP. */
2220
            goto out;
2221
        default:
2222
            MIPS_INVAL("trap");
2223
            generate_exception(ctx, EXCP_RI);
2224
            goto out;
2225
        }
2226
    } else {
2227
        switch (opc) {
2228
        case OPC_TEQ:
2229
        case OPC_TEQI:
2230
            gen_op_eq(t0, t1);
2231
            break;
2232
        case OPC_TGE:
2233
        case OPC_TGEI:
2234
            gen_op_ge(t0, t1);
2235
            break;
2236
        case OPC_TGEU:
2237
        case OPC_TGEIU:
2238
            gen_op_geu(t0, t1);
2239
            break;
2240
        case OPC_TLT:
2241
        case OPC_TLTI:
2242
            gen_op_lt(t0, t1);
2243
            break;
2244
        case OPC_TLTU:
2245
        case OPC_TLTIU:
2246
            gen_op_ltu(t0, t1);
2247
            break;
2248
        case OPC_TNE:
2249
        case OPC_TNEI:
2250
            gen_op_ne(t0, t1);
2251
            break;
2252
        default:
2253
            MIPS_INVAL("trap");
2254
            generate_exception(ctx, EXCP_RI);
2255
            goto out;
2256
        }
2257
    }
2258
    save_cpu_state(ctx, 1);
2259
    {
2260
        int l1 = gen_new_label();
2261

    
2262
        tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2263
        gen_helper_0i(raise_exception, EXCP_TRAP);
2264
        gen_set_label(l1);
2265
    }
2266
    ctx->bstate = BS_STOP;
2267
 out:
2268
    tcg_temp_free(t0);
2269
    tcg_temp_free(t1);
2270
}
2271

    
2272
static inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
2273
{
2274
    TranslationBlock *tb;
2275
    tb = ctx->tb;
2276
    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK)) {
2277
        tcg_gen_goto_tb(n);
2278
        gen_save_pc(dest);
2279
        tcg_gen_exit_tb((long)tb + n);
2280
    } else {
2281
        gen_save_pc(dest);
2282
        tcg_gen_exit_tb(0);
2283
    }
2284
}
2285

    
2286
/* Branches (before delay slot) */
2287
static void gen_compute_branch (DisasContext *ctx, uint32_t opc,
2288
                                int rs, int rt, int32_t offset)
2289
{
2290
    target_ulong btgt = -1;
2291
    int blink = 0;
2292
    int bcond_compute = 0;
2293
    TCGv t0 = tcg_temp_local_new();
2294
    TCGv t1 = tcg_temp_local_new();
2295

    
2296
    if (ctx->hflags & MIPS_HFLAG_BMASK) {
2297
#ifdef MIPS_DEBUG_DISAS
2298
        LOG_DISAS("Branch in delay slot at PC 0x" TARGET_FMT_lx "\n", ctx->pc);
2299
#endif
2300
        generate_exception(ctx, EXCP_RI);
2301
        goto out;
2302
    }
2303

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

    
2516
    ctx->btarget = btgt;
2517
    if (blink > 0) {
2518
        tcg_gen_movi_tl(t0, ctx->pc + 8);
2519
        gen_store_gpr(t0, blink);
2520
    }
2521

    
2522
 out:
2523
    tcg_temp_free(t0);
2524
    tcg_temp_free(t1);
2525
}
2526

    
2527
/* special3 bitfield operations */
2528
static void gen_bitops (DisasContext *ctx, uint32_t opc, int rt,
2529
                        int rs, int lsb, int msb)
2530
{
2531
    TCGv t0 = tcg_temp_new();
2532
    TCGv t1 = tcg_temp_new();
2533
    target_ulong mask;
2534

    
2535
    gen_load_gpr(t1, rs);
2536
    switch (opc) {
2537
    case OPC_EXT:
2538
        if (lsb + msb > 31)
2539
            goto fail;
2540
        tcg_gen_shri_tl(t0, t1, lsb);
2541
        if (msb != 31) {
2542
            tcg_gen_andi_tl(t0, t0, (1 << (msb + 1)) - 1);
2543
        } else {
2544
            tcg_gen_ext32s_tl(t0, t0);
2545
        }
2546
        break;
2547
#if defined(TARGET_MIPS64)
2548
    case OPC_DEXTM:
2549
        tcg_gen_shri_tl(t0, t1, lsb);
2550
        if (msb != 31) {
2551
            tcg_gen_andi_tl(t0, t0, (1ULL << (msb + 1 + 32)) - 1);
2552
        }
2553
        break;
2554
    case OPC_DEXTU:
2555
        tcg_gen_shri_tl(t0, t1, lsb + 32);
2556
        tcg_gen_andi_tl(t0, t0, (1ULL << (msb + 1)) - 1);
2557
        break;
2558
    case OPC_DEXT:
2559
        tcg_gen_shri_tl(t0, t1, lsb);
2560
        tcg_gen_andi_tl(t0, t0, (1ULL << (msb + 1)) - 1);
2561
        break;
2562
#endif
2563
    case OPC_INS:
2564
        if (lsb > msb)
2565
            goto fail;
2566
        mask = ((msb - lsb + 1 < 32) ? ((1 << (msb - lsb + 1)) - 1) : ~0) << lsb;
2567
        gen_load_gpr(t0, rt);
2568
        tcg_gen_andi_tl(t0, t0, ~mask);
2569
        tcg_gen_shli_tl(t1, t1, lsb);
2570
        tcg_gen_andi_tl(t1, t1, mask);
2571
        tcg_gen_or_tl(t0, t0, t1);
2572
        tcg_gen_ext32s_tl(t0, t0);
2573
        break;
2574
#if defined(TARGET_MIPS64)
2575
    case OPC_DINSM:
2576
        if (lsb > msb)
2577
            goto fail;
2578
        mask = ((msb - lsb + 1 + 32 < 64) ? ((1ULL << (msb - lsb + 1 + 32)) - 1) : ~0ULL) << lsb;
2579
        gen_load_gpr(t0, rt);
2580
        tcg_gen_andi_tl(t0, t0, ~mask);
2581
        tcg_gen_shli_tl(t1, t1, lsb);
2582
        tcg_gen_andi_tl(t1, t1, mask);
2583
        tcg_gen_or_tl(t0, t0, t1);
2584
        break;
2585
    case OPC_DINSU:
2586
        if (lsb > msb)
2587
            goto fail;
2588
        mask = ((1ULL << (msb - lsb + 1)) - 1) << lsb;
2589
        gen_load_gpr(t0, rt);
2590
        tcg_gen_andi_tl(t0, t0, ~mask);
2591
        tcg_gen_shli_tl(t1, t1, lsb + 32);
2592
        tcg_gen_andi_tl(t1, t1, mask);
2593
        tcg_gen_or_tl(t0, t0, t1);
2594
        break;
2595
    case OPC_DINS:
2596
        if (lsb > msb)
2597
            goto fail;
2598
        gen_load_gpr(t0, rt);
2599
        mask = ((1ULL << (msb - lsb + 1)) - 1) << lsb;
2600
        gen_load_gpr(t0, rt);
2601
        tcg_gen_andi_tl(t0, t0, ~mask);
2602
        tcg_gen_shli_tl(t1, t1, lsb);
2603
        tcg_gen_andi_tl(t1, t1, mask);
2604
        tcg_gen_or_tl(t0, t0, t1);
2605
        break;
2606
#endif
2607
    default:
2608
fail:
2609
        MIPS_INVAL("bitops");
2610
        generate_exception(ctx, EXCP_RI);
2611
        tcg_temp_free(t0);
2612
        tcg_temp_free(t1);
2613
        return;
2614
    }
2615
    gen_store_gpr(t0, rt);
2616
    tcg_temp_free(t0);
2617
    tcg_temp_free(t1);
2618
}
2619

    
2620
static void gen_bshfl (DisasContext *ctx, uint32_t op2, int rt, int rd)
2621
{
2622
    TCGv t0 = tcg_temp_new();
2623
    TCGv t1 = tcg_temp_new();
2624

    
2625
    gen_load_gpr(t1, rt);
2626
    switch (op2) {
2627
    case OPC_WSBH:
2628
        tcg_gen_shri_tl(t0, t1, 8);
2629
        tcg_gen_andi_tl(t0, t0, 0x00FF00FF);
2630
        tcg_gen_shli_tl(t1, t1, 8);
2631
        tcg_gen_andi_tl(t1, t1, ~0x00FF00FF);
2632
        tcg_gen_or_tl(t0, t0, t1);
2633
        tcg_gen_ext32s_tl(t0, t0);
2634
        break;
2635
    case OPC_SEB:
2636
        tcg_gen_ext8s_tl(t0, t1);
2637
        break;
2638
    case OPC_SEH:
2639
        tcg_gen_ext16s_tl(t0, t1);
2640
        break;
2641
#if defined(TARGET_MIPS64)
2642
    case OPC_DSBH:
2643
        gen_load_gpr(t1, rt);
2644
        tcg_gen_shri_tl(t0, t1, 8);
2645
        tcg_gen_andi_tl(t0, t0, 0x00FF00FF00FF00FFULL);
2646
        tcg_gen_shli_tl(t1, t1, 8);
2647
        tcg_gen_andi_tl(t1, t1, ~0x00FF00FF00FF00FFULL);
2648
        tcg_gen_or_tl(t0, t0, t1);
2649
        break;
2650
    case OPC_DSHD:
2651
        gen_load_gpr(t1, rt);
2652
        tcg_gen_shri_tl(t0, t1, 16);
2653
        tcg_gen_andi_tl(t0, t0, 0x0000FFFF0000FFFFULL);
2654
        tcg_gen_shli_tl(t1, t1, 16);
2655
        tcg_gen_andi_tl(t1, t1, ~0x0000FFFF0000FFFFULL);
2656
        tcg_gen_or_tl(t1, t0, t1);
2657
        tcg_gen_shri_tl(t0, t1, 32);
2658
        tcg_gen_shli_tl(t1, t1, 32);
2659
        tcg_gen_or_tl(t0, t0, t1);
2660
        break;
2661
#endif
2662
    default:
2663
        MIPS_INVAL("bsfhl");
2664
        generate_exception(ctx, EXCP_RI);
2665
        tcg_temp_free(t0);
2666
        tcg_temp_free(t1);
2667
        return;
2668
    }
2669
    gen_store_gpr(t0, rd);
2670
    tcg_temp_free(t0);
2671
    tcg_temp_free(t1);
2672
}
2673

    
2674
#ifndef CONFIG_USER_ONLY
2675
/* CP0 (MMU and control) */
2676
static inline void gen_mfc0_load32 (TCGv t, target_ulong off)
2677
{
2678
    TCGv_i32 r_tmp = tcg_temp_new_i32();
2679

    
2680
    tcg_gen_ld_i32(r_tmp, cpu_env, off);
2681
    tcg_gen_ext_i32_tl(t, r_tmp);
2682
    tcg_temp_free_i32(r_tmp);
2683
}
2684

    
2685
static inline void gen_mfc0_load64 (TCGv t, target_ulong off)
2686
{
2687
    tcg_gen_ld_tl(t, cpu_env, off);
2688
    tcg_gen_ext32s_tl(t, t);
2689
}
2690

    
2691
static inline void gen_mtc0_store32 (TCGv t, target_ulong off)
2692
{
2693
    TCGv_i32 r_tmp = tcg_temp_new_i32();
2694

    
2695
    tcg_gen_trunc_tl_i32(r_tmp, t);
2696
    tcg_gen_st_i32(r_tmp, cpu_env, off);
2697
    tcg_temp_free_i32(r_tmp);
2698
}
2699

    
2700
static inline void gen_mtc0_store64 (TCGv t, target_ulong off)
2701
{
2702
    tcg_gen_ext32s_tl(t, t);
2703
    tcg_gen_st_tl(t, cpu_env, off);
2704
}
2705

    
2706
static void gen_mfc0 (CPUState *env, DisasContext *ctx, TCGv t0, int reg, int sel)
2707
{
2708
    const char *rn = "invalid";
2709

    
2710
    if (sel != 0)
2711
        check_insn(env, ctx, ISA_MIPS32);
2712

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

    
3278
die:
3279
    LOG_DISAS("mfc0 %s (reg %d sel %d)\n", rn, reg, sel);
3280
    generate_exception(ctx, EXCP_RI);
3281
}
3282

    
3283
static void gen_mtc0 (CPUState *env, DisasContext *ctx, TCGv t0, int reg, int sel)
3284
{
3285
    const char *rn = "invalid";
3286

    
3287
    if (sel != 0)
3288
        check_insn(env, ctx, ISA_MIPS32);
3289

    
3290
    if (use_icount)
3291
        gen_io_start();
3292

    
3293
    switch (reg) {
3294
    case 0:
3295
        switch (sel) {
3296
        case 0:
3297
            gen_helper_mtc0_index(t0);
3298
            rn = "Index";
3299
            break;
3300
        case 1:
3301
            check_insn(env, ctx, ASE_MT);
3302
            gen_helper_mtc0_mvpcontrol(t0);
3303
            rn = "MVPControl";
3304
            break;
3305
        case 2:
3306
            check_insn(env, ctx, ASE_MT);
3307
            /* ignored */
3308
            rn = "MVPConf0";
3309
            break;
3310
        case 3:
3311
            check_insn(env, ctx, ASE_MT);
3312
            /* ignored */
3313
            rn = "MVPConf1";
3314
            break;
3315
        default:
3316
            goto die;
3317
        }
3318
        break;
3319
    case 1:
3320
        switch (sel) {
3321
        case 0:
3322
            /* ignored */
3323
            rn = "Random";
3324
            break;
3325
        case 1:
3326
            check_insn(env, ctx, ASE_MT);
3327
            gen_helper_mtc0_vpecontrol(t0);
3328
            rn = "VPEControl";
3329
            break;
3330
        case 2:
3331
            check_insn(env, ctx, ASE_MT);
3332
            gen_helper_mtc0_vpeconf0(t0);
3333
            rn = "VPEConf0";
3334
            break;
3335
        case 3:
3336
            check_insn(env, ctx, ASE_MT);
3337
            gen_helper_mtc0_vpeconf1(t0);
3338
            rn = "VPEConf1";
3339
            break;
3340
        case 4:
3341
            check_insn(env, ctx, ASE_MT);
3342
            gen_helper_mtc0_yqmask(t0);
3343
            rn = "YQMask";
3344
            break;
3345
        case 5:
3346
            check_insn(env, ctx, ASE_MT);
3347
            gen_mtc0_store64(t0, offsetof(CPUState, CP0_VPESchedule));
3348
            rn = "VPESchedule";
3349
            break;
3350
        case 6:
3351
            check_insn(env, ctx, ASE_MT);
3352
            gen_mtc0_store64(t0, offsetof(CPUState, CP0_VPEScheFBack));
3353
            rn = "VPEScheFBack";
3354
            break;
3355
        case 7:
3356
            check_insn(env, ctx, ASE_MT);
3357
            gen_helper_mtc0_vpeopt(t0);
3358
            rn = "VPEOpt";
3359
            break;
3360
        default:
3361
            goto die;
3362
        }
3363
        break;
3364
    case 2:
3365
        switch (sel) {
3366
        case 0:
3367
            gen_helper_mtc0_entrylo0(t0);
3368
            rn = "EntryLo0";
3369
            break;
3370
        case 1:
3371
            check_insn(env, ctx, ASE_MT);
3372
            gen_helper_mtc0_tcstatus(t0);
3373
            rn = "TCStatus";
3374
            break;
3375
        case 2:
3376
            check_insn(env, ctx, ASE_MT);
3377
            gen_helper_mtc0_tcbind(t0);
3378
            rn = "TCBind";
3379
            break;
3380
        case 3:
3381
            check_insn(env, ctx, ASE_MT);
3382
            gen_helper_mtc0_tcrestart(t0);
3383
            rn = "TCRestart";
3384
            break;
3385
        case 4:
3386
            check_insn(env, ctx, ASE_MT);
3387
            gen_helper_mtc0_tchalt(t0);
3388
            rn = "TCHalt";
3389
            break;
3390
        case 5:
3391
            check_insn(env, ctx, ASE_MT);
3392
            gen_helper_mtc0_tccontext(t0);
3393
            rn = "TCContext";
3394
            break;
3395
        case 6:
3396
            check_insn(env, ctx, ASE_MT);
3397
            gen_helper_mtc0_tcschedule(t0);
3398
            rn = "TCSchedule";
3399
            break;
3400
        case 7:
3401
            check_insn(env, ctx, ASE_MT);
3402
            gen_helper_mtc0_tcschefback(t0);
3403
            rn = "TCScheFBack";
3404
            break;
3405
        default:
3406
            goto die;
3407
        }
3408
        break;
3409
    case 3:
3410
        switch (sel) {
3411
        case 0:
3412
            gen_helper_mtc0_entrylo1(t0);
3413
            rn = "EntryLo1";
3414
            break;
3415
        default:
3416
            goto die;
3417
        }
3418
        break;
3419
    case 4:
3420
        switch (sel) {
3421
        case 0:
3422
            gen_helper_mtc0_context(t0);
3423
            rn = "Context";
3424
            break;
3425
        case 1:
3426
//            gen_helper_mtc0_contextconfig(t0); /* SmartMIPS ASE */
3427
            rn = "ContextConfig";
3428
//            break;
3429
        default:
3430
            goto die;
3431
        }
3432
        break;
3433
    case 5:
3434
        switch (sel) {
3435
        case 0:
3436
            gen_helper_mtc0_pagemask(t0);
3437
            rn = "PageMask";
3438
            break;
3439
        case 1:
3440
            check_insn(env, ctx, ISA_MIPS32R2);
3441
            gen_helper_mtc0_pagegrain(t0);
3442
            rn = "PageGrain";
3443
            break;
3444
        default:
3445
            goto die;
3446
        }
3447
        break;
3448
    case 6:
3449
        switch (sel) {
3450
        case 0:
3451
            gen_helper_mtc0_wired(t0);
3452
            rn = "Wired";
3453
            break;
3454
        case 1:
3455
            check_insn(env, ctx, ISA_MIPS32R2);
3456
            gen_helper_mtc0_srsconf0(t0);
3457
            rn = "SRSConf0";
3458
            break;
3459
        case 2:
3460
            check_insn(env, ctx, ISA_MIPS32R2);
3461
            gen_helper_mtc0_srsconf1(t0);
3462
            rn = "SRSConf1";
3463
            break;
3464
        case 3:
3465
            check_insn(env, ctx, ISA_MIPS32R2);
3466
            gen_helper_mtc0_srsconf2(t0);
3467
            rn = "SRSConf2";
3468
            break;
3469
        case 4:
3470
            check_insn(env, ctx, ISA_MIPS32R2);
3471
            gen_helper_mtc0_srsconf3(t0);
3472
            rn = "SRSConf3";
3473
            break;
3474
        case 5:
3475
            check_insn(env, ctx, ISA_MIPS32R2);
3476
            gen_helper_mtc0_srsconf4(t0);
3477
            rn = "SRSConf4";
3478
            break;
3479
        default:
3480
            goto die;
3481
        }
3482
        break;
3483
    case 7:
3484
        switch (sel) {
3485
        case 0:
3486
            check_insn(env, ctx, ISA_MIPS32R2);
3487
            gen_helper_mtc0_hwrena(t0);
3488
            rn = "HWREna";
3489
            break;
3490
        default:
3491
            goto die;
3492
        }
3493
        break;
3494
    case 8:
3495
        /* ignored */
3496
        rn = "BadVAddr";
3497
        break;
3498
    case 9:
3499
        switch (sel) {
3500
        case 0:
3501
            gen_helper_mtc0_count(t0);
3502
            rn = "Count";
3503
            break;
3504
        /* 6,7 are implementation dependent */
3505
        default:
3506
            goto die;
3507
        }
3508
        /* Stop translation as we may have switched the execution mode */
3509
        ctx->bstate = BS_STOP;
3510
        break;
3511
    case 10:
3512
        switch (sel) {
3513
        case 0:
3514
            gen_helper_mtc0_entryhi(t0);
3515
            rn = "EntryHi";
3516
            break;
3517
        default:
3518
            goto die;
3519
        }
3520
        break;
3521
    case 11:
3522
        switch (sel) {
3523
        case 0:
3524
            gen_helper_mtc0_compare(t0);
3525
            rn = "Compare";
3526
            break;
3527
        /* 6,7 are implementation dependent */
3528
        default:
3529
            goto die;
3530
        }
3531
        /* Stop translation as we may have switched the execution mode */
3532
        ctx->bstate = BS_STOP;
3533
        break;
3534
    case 12:
3535
        switch (sel) {
3536
        case 0:
3537
            gen_helper_mtc0_status(t0);
3538
            /* BS_STOP isn't good enough here, hflags may have changed. */
3539
            gen_save_pc(ctx->pc + 4);
3540
            ctx->bstate = BS_EXCP;
3541
            rn = "Status";
3542
            break;
3543
        case 1:
3544
            check_insn(env, ctx, ISA_MIPS32R2);
3545
            gen_helper_mtc0_intctl(t0);
3546
            /* Stop translation as we may have switched the execution mode */
3547
            ctx->bstate = BS_STOP;
3548
            rn = "IntCtl";
3549
            break;
3550
        case 2:
3551
            check_insn(env, ctx, ISA_MIPS32R2);
3552
            gen_helper_mtc0_srsctl(t0);
3553
            /* Stop translation as we may have switched the execution mode */
3554
            ctx->bstate = BS_STOP;
3555
            rn = "SRSCtl";
3556
            break;
3557
        case 3:
3558
            check_insn(env, ctx, ISA_MIPS32R2);
3559
            gen_mtc0_store32(t0, offsetof(CPUState, CP0_SRSMap));
3560
            /* Stop translation as we may have switched the execution mode */
3561
            ctx->bstate = BS_STOP;
3562
            rn = "SRSMap";
3563
            break;
3564
        default:
3565
            goto die;
3566
        }
3567
        break;
3568
    case 13:
3569
        switch (sel) {
3570
        case 0:
3571
            gen_helper_mtc0_cause(t0);
3572
            rn = "Cause";
3573
            break;
3574
        default:
3575
            goto die;
3576
        }
3577
        /* Stop translation as we may have switched the execution mode */
3578
        ctx->bstate = BS_STOP;
3579
        break;
3580
    case 14:
3581
        switch (sel) {
3582
        case 0:
3583
            gen_mtc0_store64(t0, offsetof(CPUState, CP0_EPC));
3584
            rn = "EPC";
3585
            break;
3586
        default:
3587
            goto die;
3588
        }
3589
        break;
3590
    case 15:
3591
        switch (sel) {
3592
        case 0:
3593
            /* ignored */
3594
            rn = "PRid";
3595
            break;
3596
        case 1:
3597
            check_insn(env, ctx, ISA_MIPS32R2);
3598
            gen_helper_mtc0_ebase(t0);
3599
            rn = "EBase";
3600
            break;
3601
        default:
3602
            goto die;
3603
        }
3604
        break;
3605
    case 16:
3606
        switch (sel) {
3607
        case 0:
3608
            gen_helper_mtc0_config0(t0);
3609
            rn = "Config";
3610
            /* Stop translation as we may have switched the execution mode */
3611
            ctx->bstate = BS_STOP;
3612
            break;
3613
        case 1:
3614
            /* ignored, read only */
3615
            rn = "Config1";
3616
            break;
3617
        case 2:
3618
            gen_helper_mtc0_config2(t0);
3619
            rn = "Config2";
3620
            /* Stop translation as we may have switched the execution mode */
3621
            ctx->bstate = BS_STOP;
3622
            break;
3623
        case 3:
3624
            /* ignored, read only */
3625
            rn = "Config3";
3626
            break;
3627
        /* 4,5 are reserved */
3628
        /* 6,7 are implementation dependent */
3629
        case 6:
3630
            /* ignored */
3631
            rn = "Config6";
3632
            break;
3633
        case 7:
3634
            /* ignored */
3635
            rn = "Config7";
3636
            break;
3637
        default:
3638
            rn = "Invalid config selector";
3639
            goto die;
3640
        }
3641
        break;
3642
    case 17:
3643
        switch (sel) {
3644
        case 0:
3645
            /* ignored */
3646
            rn = "LLAddr";
3647
            break;
3648
        default:
3649
            goto die;
3650
        }
3651
        break;
3652
    case 18:
3653
        switch (sel) {
3654
        case 0 ... 7:
3655
            gen_helper_1i(mtc0_watchlo, t0, sel);
3656
            rn = "WatchLo";
3657
            break;
3658
        default:
3659
            goto die;
3660
        }
3661
        break;
3662
    case 19:
3663
        switch (sel) {
3664
        case 0 ... 7:
3665
            gen_helper_1i(mtc0_watchhi, t0, sel);
3666
            rn = "WatchHi";
3667
            break;
3668
        default:
3669
            goto die;
3670
        }
3671
        break;
3672
    case 20:
3673
        switch (sel) {
3674
        case 0:
3675
#if defined(TARGET_MIPS64)
3676
            check_insn(env, ctx, ISA_MIPS3);
3677
            gen_helper_mtc0_xcontext(t0);
3678
            rn = "XContext";
3679
            break;
3680
#endif
3681
        default:
3682
            goto die;
3683
        }
3684
        break;
3685
    case 21:
3686
       /* Officially reserved, but sel 0 is used for R1x000 framemask */
3687
        switch (sel) {
3688
        case 0:
3689
            gen_helper_mtc0_framemask(t0);
3690
            rn = "Framemask";
3691
            break;
3692
        default:
3693
            goto die;
3694
        }
3695
        break;
3696
    case 22:
3697
        /* ignored */
3698
        rn = "Diagnostic"; /* implementation dependent */
3699
        break;
3700
    case 23:
3701
        switch (sel) {
3702
        case 0:
3703
            gen_helper_mtc0_debug(t0); /* EJTAG support */
3704
            /* BS_STOP isn't good enough here, hflags may have changed. */
3705
            gen_save_pc(ctx->pc + 4);
3706
            ctx->bstate = BS_EXCP;
3707
            rn = "Debug";
3708
            break;
3709
        case 1:
3710
//            gen_helper_mtc0_tracecontrol(t0); /* PDtrace support */
3711
            rn = "TraceControl";
3712
            /* Stop translation as we may have switched the execution mode */
3713
            ctx->bstate = BS_STOP;
3714
//            break;
3715
        case 2:
3716
//            gen_helper_mtc0_tracecontrol2(t0); /* PDtrace support */
3717
            rn = "TraceControl2";
3718
            /* Stop translation as we may have switched the execution mode */
3719
            ctx->bstate = BS_STOP;
3720
//            break;
3721
        case 3:
3722
            /* Stop translation as we may have switched the execution mode */
3723
            ctx->bstate = BS_STOP;
3724
//            gen_helper_mtc0_usertracedata(t0); /* PDtrace support */
3725
            rn = "UserTraceData";
3726
            /* Stop translation as we may have switched the execution mode */
3727
            ctx->bstate = BS_STOP;
3728
//            break;
3729
        case 4:
3730
//            gen_helper_mtc0_tracebpc(t0); /* PDtrace support */
3731
            /* Stop translation as we may have switched the execution mode */
3732
            ctx->bstate = BS_STOP;
3733
            rn = "TraceBPC";
3734
//            break;
3735
        default:
3736
            goto die;
3737
        }
3738
        break;
3739
    case 24:
3740
        switch (sel) {
3741
        case 0:
3742
            /* EJTAG support */
3743
            gen_mtc0_store64(t0, offsetof(CPUState, CP0_DEPC));
3744
            rn = "DEPC";
3745
            break;
3746
        default:
3747
            goto die;
3748
        }
3749
        break;
3750
    case 25:
3751
        switch (sel) {
3752
        case 0:
3753
            gen_helper_mtc0_performance0(t0);
3754
            rn = "Performance0";
3755
            break;
3756
        case 1:
3757
//            gen_helper_mtc0_performance1(t0);
3758
            rn = "Performance1";
3759
//            break;
3760
        case 2:
3761
//            gen_helper_mtc0_performance2(t0);
3762
            rn = "Performance2";
3763
//            break;
3764
        case 3:
3765
//            gen_helper_mtc0_performance3(t0);
3766
            rn = "Performance3";
3767
//            break;
3768
        case 4:
3769
//            gen_helper_mtc0_performance4(t0);
3770
            rn = "Performance4";
3771
//            break;
3772
        case 5:
3773
//            gen_helper_mtc0_performance5(t0);
3774
            rn = "Performance5";
3775
//            break;
3776
        case 6:
3777
//            gen_helper_mtc0_performance6(t0);
3778
            rn = "Performance6";
3779
//            break;
3780
        case 7:
3781
//            gen_helper_mtc0_performance7(t0);
3782
            rn = "Performance7";
3783
//            break;
3784
        default:
3785
            goto die;
3786
        }
3787
       break;
3788
    case 26:
3789
        /* ignored */
3790
        rn = "ECC";
3791
        break;
3792
    case 27:
3793
        switch (sel) {
3794
        case 0 ... 3:
3795
            /* ignored */
3796
            rn = "CacheErr";
3797
            break;
3798
        default:
3799
            goto die;
3800
        }
3801
       break;
3802
    case 28:
3803
        switch (sel) {
3804
        case 0:
3805
        case 2:
3806
        case 4:
3807
        case 6:
3808
            gen_helper_mtc0_taglo(t0);
3809
            rn = "TagLo";
3810
            break;
3811
        case 1:
3812
        case 3:
3813
        case 5:
3814
        case 7:
3815
            gen_helper_mtc0_datalo(t0);
3816
            rn = "DataLo";
3817
            break;
3818
        default:
3819
            goto die;
3820
        }
3821
        break;
3822
    case 29:
3823
        switch (sel) {
3824
        case 0:
3825
        case 2:
3826
        case 4:
3827
        case 6:
3828
            gen_helper_mtc0_taghi(t0);
3829
            rn = "TagHi";
3830
            break;
3831
        case 1:
3832
        case 3:
3833
        case 5:
3834
        case 7:
3835
            gen_helper_mtc0_datahi(t0);
3836
            rn = "DataHi";
3837
            break;
3838
        default:
3839
            rn = "invalid sel";
3840
            goto die;
3841
        }
3842
       break;
3843
    case 30:
3844
        switch (sel) {
3845
        case 0:
3846
            gen_mtc0_store64(t0, offsetof(CPUState, CP0_ErrorEPC));
3847
            rn = "ErrorEPC";
3848
            break;
3849
        default:
3850
            goto die;
3851
        }
3852
        break;
3853
    case 31:
3854
        switch (sel) {
3855
        case 0:
3856
            /* EJTAG support */
3857
            gen_mtc0_store32(t0, offsetof(CPUState, CP0_DESAVE));
3858
            rn = "DESAVE";
3859
            break;
3860
        default:
3861
            goto die;
3862
        }
3863
        /* Stop translation as we may have switched the execution mode */
3864
        ctx->bstate = BS_STOP;
3865
        break;
3866
    default:
3867
       goto die;
3868
    }
3869
    LOG_DISAS("mtc0 %s (reg %d sel %d)\n", rn, reg, sel);
3870
    /* For simplicity assume that all writes can cause interrupts.  */
3871
    if (use_icount) {
3872
        gen_io_end();
3873
        ctx->bstate = BS_STOP;
3874
    }
3875
    return;
3876

    
3877
die:
3878
    LOG_DISAS("mtc0 %s (reg %d sel %d)\n", rn, reg, sel);
3879
    generate_exception(ctx, EXCP_RI);
3880
}
3881

    
3882
#if defined(TARGET_MIPS64)
3883
static void gen_dmfc0 (CPUState *env, DisasContext *ctx, TCGv t0, int reg, int sel)
3884
{
3885
    const char *rn = "invalid";
3886

    
3887
    if (sel != 0)
3888
        check_insn(env, ctx, ISA_MIPS64);
3889

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

    
4444
die:
4445
    LOG_DISAS("dmfc0 %s (reg %d sel %d)\n", rn, reg, sel);
4446
    generate_exception(ctx, EXCP_RI);
4447
}
4448

    
4449
static void gen_dmtc0 (CPUState *env, DisasContext *ctx, TCGv t0, int reg, int sel)
4450
{
4451
    const char *rn = "invalid";
4452

    
4453
    if (sel != 0)
4454
        check_insn(env, ctx, ISA_MIPS64);
4455

    
4456
    if (use_icount)
4457
        gen_io_start();
4458

    
4459
    switch (reg) {
4460
    case 0:
4461
        switch (sel) {
4462
        case 0:
4463
            gen_helper_mtc0_index(t0);
4464
            rn = "Index";
4465
            break;
4466
        case 1:
4467
            check_insn(env, ctx, ASE_MT);
4468
            gen_helper_mtc0_mvpcontrol(t0);
4469
            rn = "MVPControl";
4470
            break;
4471
        case 2:
4472
            check_insn(env, ctx, ASE_MT);
4473
            /* ignored */
4474
            rn = "MVPConf0";
4475
            break;
4476
        case 3:
4477
            check_insn(env, ctx, ASE_MT);
4478
            /* ignored */
4479
            rn = "MVPConf1";
4480
            break;
4481
        default:
4482
            goto die;
4483
        }
4484
        break;
4485
    case 1:
4486
        switch (sel) {
4487
        case 0:
4488
            /* ignored */
4489
            rn = "Random";
4490
            break;
4491
        case 1:
4492
            check_insn(env, ctx, ASE_MT);
4493
            gen_helper_mtc0_vpecontrol(t0);
4494
            rn = "VPEControl";
4495
            break;
4496
        case 2:
4497
            check_insn(env, ctx, ASE_MT);
4498
            gen_helper_mtc0_vpeconf0(t0);
4499
            rn = "VPEConf0";
4500
            break;
4501
        case 3:
4502
            check_insn(env, ctx, ASE_MT);
4503
            gen_helper_mtc0_vpeconf1(t0);
4504
            rn = "VPEConf1";
4505
            break;
4506
        case 4:
4507
            check_insn(env, ctx, ASE_MT);
4508
            gen_helper_mtc0_yqmask(t0);
4509
            rn = "YQMask";
4510
            break;
4511
        case 5:
4512
            check_insn(env, ctx, ASE_MT);
4513
            tcg_gen_st_tl(t0, cpu_env, offsetof(CPUState, CP0_VPESchedule));
4514
            rn = "VPESchedule";
4515
            break;
4516
        case 6:
4517
            check_insn(env, ctx, ASE_MT);
4518
            tcg_gen_st_tl(t0, cpu_env, offsetof(CPUState, CP0_VPEScheFBack));
4519
            rn = "VPEScheFBack";
4520
            break;
4521
        case 7:
4522
            check_insn(env, ctx, ASE_MT);
4523
            gen_helper_mtc0_vpeopt(t0);
4524
            rn = "VPEOpt";
4525
            break;
4526
        default:
4527
            goto die;
4528
        }
4529
        break;
4530
    case 2:
4531
        switch (sel) {
4532
        case 0:
4533
            gen_helper_mtc0_entrylo0(t0);
4534
            rn = "EntryLo0";
4535
            break;
4536
        case 1:
4537
            check_insn(env, ctx, ASE_MT);
4538
            gen_helper_mtc0_tcstatus(t0);
4539
            rn = "TCStatus";
4540
            break;
4541
        case 2:
4542
            check_insn(env, ctx, ASE_MT);
4543
            gen_helper_mtc0_tcbind(t0);
4544
            rn = "TCBind";
4545
            break;
4546
        case 3:
4547
            check_insn(env, ctx, ASE_MT);
4548
            gen_helper_mtc0_tcrestart(t0);
4549
            rn = "TCRestart";
4550
            break;
4551
        case 4:
4552
            check_insn(env, ctx, ASE_MT);
4553
            gen_helper_mtc0_tchalt(t0);
4554
            rn = "TCHalt";
4555
            break;
4556
        case 5:
4557
            check_insn(env, ctx, ASE_MT);
4558
            gen_helper_mtc0_tccontext(t0);
4559
            rn = "TCContext";
4560
            break;
4561
        case 6:
4562
            check_insn(env, ctx, ASE_MT);
4563
            gen_helper_mtc0_tcschedule(t0);
4564
            rn = "TCSchedule";
4565
            break;
4566
        case 7:
4567
            check_insn(env, ctx, ASE_MT);
4568
            gen_helper_mtc0_tcschefback(t0);
4569
            rn = "TCScheFBack";
4570
            break;
4571
        default:
4572
            goto die;
4573
        }
4574
        break;
4575
    case 3:
4576
        switch (sel) {
4577
        case 0:
4578
            gen_helper_mtc0_entrylo1(t0);
4579
            rn = "EntryLo1";
4580
            break;
4581
        default:
4582
            goto die;
4583
        }
4584
        break;
4585
    case 4:
4586
        switch (sel) {
4587
        case 0:
4588
            gen_helper_mtc0_context(t0);
4589
            rn = "Context";
4590
            break;
4591
        case 1:
4592
//           gen_helper_mtc0_contextconfig(t0); /* SmartMIPS ASE */
4593
            rn = "ContextConfig";
4594
//           break;
4595
        default:
4596
            goto die;
4597
        }
4598
        break;
4599
    case 5:
4600
        switch (sel) {
4601
        case 0:
4602
            gen_helper_mtc0_pagemask(t0);
4603
            rn = "PageMask";
4604
            break;
4605
        case 1:
4606
            check_insn(env, ctx, ISA_MIPS32R2);
4607
            gen_helper_mtc0_pagegrain(t0);
4608
            rn = "PageGrain";
4609
            break;
4610
        default:
4611
            goto die;
4612
        }
4613
        break;
4614
    case 6:
4615
        switch (sel) {
4616
        case 0:
4617
            gen_helper_mtc0_wired(t0);
4618
            rn = "Wired";
4619
            break;
4620
        case 1:
4621
            check_insn(env, ctx, ISA_MIPS32R2);
4622
            gen_helper_mtc0_srsconf0(t0);
4623
            rn = "SRSConf0";
4624
            break;
4625
        case 2:
4626
            check_insn(env, ctx, ISA_MIPS32R2);
4627
            gen_helper_mtc0_srsconf1(t0);
4628
            rn = "SRSConf1";
4629
            break;
4630
        case 3:
4631
            check_insn(env, ctx, ISA_MIPS32R2);
4632
            gen_helper_mtc0_srsconf2(t0);
4633
            rn = "SRSConf2";
4634
            break;
4635
        case 4:
4636
            check_insn(env, ctx, ISA_MIPS32R2);
4637
            gen_helper_mtc0_srsconf3(t0);
4638
            rn = "SRSConf3";
4639
            break;
4640
        case 5:
4641
            check_insn(env, ctx, ISA_MIPS32R2);
4642
            gen_helper_mtc0_srsconf4(t0);
4643
            rn = "SRSConf4";
4644
            break;
4645
        default:
4646
            goto die;
4647
        }
4648
        break;
4649
    case 7:
4650
        switch (sel) {
4651
        case 0:
4652
            check_insn(env, ctx, ISA_MIPS32R2);
4653
            gen_helper_mtc0_hwrena(t0);
4654
            rn = "HWREna";
4655
            break;
4656
        default:
4657
            goto die;
4658
        }
4659
        break;
4660
    case 8:
4661
        /* ignored */
4662
        rn = "BadVAddr";
4663
        break;
4664
    case 9:
4665
        switch (sel) {
4666
        case 0:
4667
            gen_helper_mtc0_count(t0);
4668
            rn = "Count";
4669
            break;
4670
        /* 6,7 are implementation dependent */
4671
        default:
4672
            goto die;
4673
        }
4674
        /* Stop translation as we may have switched the execution mode */
4675
        ctx->bstate = BS_STOP;
4676
        break;
4677
    case 10:
4678
        switch (sel) {
4679
        case 0:
4680
            gen_helper_mtc0_entryhi(t0);
4681
            rn = "EntryHi";
4682
            break;
4683
        default:
4684
            goto die;
4685
        }
4686
        break;
4687
    case 11:
4688
        switch (sel) {
4689
        case 0:
4690
            gen_helper_mtc0_compare(t0);
4691
            rn = "Compare";
4692
            break;
4693
        /* 6,7 are implementation dependent */
4694
        default:
4695
            goto die;
4696
        }
4697
        /* Stop translation as we may have switched the execution mode */
4698
        ctx->bstate = BS_STOP;
4699
        break;
4700
    case 12:
4701
        switch (sel) {
4702
        case 0:
4703
            gen_helper_mtc0_status(t0);
4704
            /* BS_STOP isn't good enough here, hflags may have changed. */
4705
            gen_save_pc(ctx->pc + 4);
4706
            ctx->bstate = BS_EXCP;
4707
            rn = "Status";
</