2 * MIPS32 emulation for qemu: main translation routines.
4 * Copyright (c) 2004-2005 Jocelyn Mayer
5 * Copyright (c) 2006 Marius Groeger (FPU operations)
6 * Copyright (c) 2006 Thiemo Seufer (MIPS32R2 support)
7 * Copyright (c) 2009 CodeSourcery (MIPS16 support)
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
33 #include "qemu-common.h"
39 //#define MIPS_DEBUG_DISAS
40 //#define MIPS_DEBUG_SIGN_EXTENSIONS
42 /* MIPS major opcodes */
43 #define MASK_OP_MAJOR(op) (op & (0x3F << 26))
46 /* indirect opcode tables */
47 OPC_SPECIAL = (0x00 << 26),
48 OPC_REGIMM = (0x01 << 26),
49 OPC_CP0 = (0x10 << 26),
50 OPC_CP1 = (0x11 << 26),
51 OPC_CP2 = (0x12 << 26),
52 OPC_CP3 = (0x13 << 26),
53 OPC_SPECIAL2 = (0x1C << 26),
54 OPC_SPECIAL3 = (0x1F << 26),
55 /* arithmetic with immediate */
56 OPC_ADDI = (0x08 << 26),
57 OPC_ADDIU = (0x09 << 26),
58 OPC_SLTI = (0x0A << 26),
59 OPC_SLTIU = (0x0B << 26),
60 /* logic with immediate */
61 OPC_ANDI = (0x0C << 26),
62 OPC_ORI = (0x0D << 26),
63 OPC_XORI = (0x0E << 26),
64 OPC_LUI = (0x0F << 26),
65 /* arithmetic with immediate */
66 OPC_DADDI = (0x18 << 26),
67 OPC_DADDIU = (0x19 << 26),
68 /* Jump and branches */
70 OPC_JAL = (0x03 << 26),
71 OPC_BEQ = (0x04 << 26), /* Unconditional if rs = rt = 0 (B) */
72 OPC_BEQL = (0x14 << 26),
73 OPC_BNE = (0x05 << 26),
74 OPC_BNEL = (0x15 << 26),
75 OPC_BLEZ = (0x06 << 26),
76 OPC_BLEZL = (0x16 << 26),
77 OPC_BGTZ = (0x07 << 26),
78 OPC_BGTZL = (0x17 << 26),
79 OPC_JALX = (0x1D << 26), /* MIPS 16 only */
81 OPC_LDL = (0x1A << 26),
82 OPC_LDR = (0x1B << 26),
83 OPC_LB = (0x20 << 26),
84 OPC_LH = (0x21 << 26),
85 OPC_LWL = (0x22 << 26),
86 OPC_LW = (0x23 << 26),
87 OPC_LWPC = OPC_LW | 0x5,
88 OPC_LBU = (0x24 << 26),
89 OPC_LHU = (0x25 << 26),
90 OPC_LWR = (0x26 << 26),
91 OPC_LWU = (0x27 << 26),
92 OPC_SB = (0x28 << 26),
93 OPC_SH = (0x29 << 26),
94 OPC_SWL = (0x2A << 26),
95 OPC_SW = (0x2B << 26),
96 OPC_SDL = (0x2C << 26),
97 OPC_SDR = (0x2D << 26),
98 OPC_SWR = (0x2E << 26),
99 OPC_LL = (0x30 << 26),
100 OPC_LLD = (0x34 << 26),
101 OPC_LD = (0x37 << 26),
102 OPC_LDPC = OPC_LD | 0x5,
103 OPC_SC = (0x38 << 26),
104 OPC_SCD = (0x3C << 26),
105 OPC_SD = (0x3F << 26),
106 /* Floating point load/store */
107 OPC_LWC1 = (0x31 << 26),
108 OPC_LWC2 = (0x32 << 26),
109 OPC_LDC1 = (0x35 << 26),
110 OPC_LDC2 = (0x36 << 26),
111 OPC_SWC1 = (0x39 << 26),
112 OPC_SWC2 = (0x3A << 26),
113 OPC_SDC1 = (0x3D << 26),
114 OPC_SDC2 = (0x3E << 26),
115 /* MDMX ASE specific */
116 OPC_MDMX = (0x1E << 26),
117 /* Cache and prefetch */
118 OPC_CACHE = (0x2F << 26),
119 OPC_PREF = (0x33 << 26),
120 /* Reserved major opcode */
121 OPC_MAJOR3B_RESERVED = (0x3B << 26),
124 /* MIPS special opcodes */
125 #define MASK_SPECIAL(op) MASK_OP_MAJOR(op) | (op & 0x3F)
129 OPC_SLL = 0x00 | OPC_SPECIAL,
130 /* NOP is SLL r0, r0, 0 */
131 /* SSNOP is SLL r0, r0, 1 */
132 /* EHB is SLL r0, r0, 3 */
133 OPC_SRL = 0x02 | OPC_SPECIAL, /* also ROTR */
134 OPC_ROTR = OPC_SRL | (1 << 21),
135 OPC_SRA = 0x03 | OPC_SPECIAL,
136 OPC_SLLV = 0x04 | OPC_SPECIAL,
137 OPC_SRLV = 0x06 | OPC_SPECIAL, /* also ROTRV */
138 OPC_ROTRV = OPC_SRLV | (1 << 6),
139 OPC_SRAV = 0x07 | OPC_SPECIAL,
140 OPC_DSLLV = 0x14 | OPC_SPECIAL,
141 OPC_DSRLV = 0x16 | OPC_SPECIAL, /* also DROTRV */
142 OPC_DROTRV = OPC_DSRLV | (1 << 6),
143 OPC_DSRAV = 0x17 | OPC_SPECIAL,
144 OPC_DSLL = 0x38 | OPC_SPECIAL,
145 OPC_DSRL = 0x3A | OPC_SPECIAL, /* also DROTR */
146 OPC_DROTR = OPC_DSRL | (1 << 21),
147 OPC_DSRA = 0x3B | OPC_SPECIAL,
148 OPC_DSLL32 = 0x3C | OPC_SPECIAL,
149 OPC_DSRL32 = 0x3E | OPC_SPECIAL, /* also DROTR32 */
150 OPC_DROTR32 = OPC_DSRL32 | (1 << 21),
151 OPC_DSRA32 = 0x3F | OPC_SPECIAL,
152 /* Multiplication / division */
153 OPC_MULT = 0x18 | OPC_SPECIAL,
154 OPC_MULTU = 0x19 | OPC_SPECIAL,
155 OPC_DIV = 0x1A | OPC_SPECIAL,
156 OPC_DIVU = 0x1B | OPC_SPECIAL,
157 OPC_DMULT = 0x1C | OPC_SPECIAL,
158 OPC_DMULTU = 0x1D | OPC_SPECIAL,
159 OPC_DDIV = 0x1E | OPC_SPECIAL,
160 OPC_DDIVU = 0x1F | OPC_SPECIAL,
161 /* 2 registers arithmetic / logic */
162 OPC_ADD = 0x20 | OPC_SPECIAL,
163 OPC_ADDU = 0x21 | OPC_SPECIAL,
164 OPC_SUB = 0x22 | OPC_SPECIAL,
165 OPC_SUBU = 0x23 | OPC_SPECIAL,
166 OPC_AND = 0x24 | OPC_SPECIAL,
167 OPC_OR = 0x25 | OPC_SPECIAL,
168 OPC_XOR = 0x26 | OPC_SPECIAL,
169 OPC_NOR = 0x27 | OPC_SPECIAL,
170 OPC_SLT = 0x2A | OPC_SPECIAL,
171 OPC_SLTU = 0x2B | OPC_SPECIAL,
172 OPC_DADD = 0x2C | OPC_SPECIAL,
173 OPC_DADDU = 0x2D | OPC_SPECIAL,
174 OPC_DSUB = 0x2E | OPC_SPECIAL,
175 OPC_DSUBU = 0x2F | OPC_SPECIAL,
177 OPC_JR = 0x08 | OPC_SPECIAL, /* Also JR.HB */
178 OPC_JALR = 0x09 | OPC_SPECIAL, /* Also JALR.HB */
179 OPC_JALRC = OPC_JALR | (0x5 << 6),
181 OPC_TGE = 0x30 | OPC_SPECIAL,
182 OPC_TGEU = 0x31 | OPC_SPECIAL,
183 OPC_TLT = 0x32 | OPC_SPECIAL,
184 OPC_TLTU = 0x33 | OPC_SPECIAL,
185 OPC_TEQ = 0x34 | OPC_SPECIAL,
186 OPC_TNE = 0x36 | OPC_SPECIAL,
187 /* HI / LO registers load & stores */
188 OPC_MFHI = 0x10 | OPC_SPECIAL,
189 OPC_MTHI = 0x11 | OPC_SPECIAL,
190 OPC_MFLO = 0x12 | OPC_SPECIAL,
191 OPC_MTLO = 0x13 | OPC_SPECIAL,
192 /* Conditional moves */
193 OPC_MOVZ = 0x0A | OPC_SPECIAL,
194 OPC_MOVN = 0x0B | OPC_SPECIAL,
196 OPC_MOVCI = 0x01 | OPC_SPECIAL,
199 OPC_PMON = 0x05 | OPC_SPECIAL, /* unofficial */
200 OPC_SYSCALL = 0x0C | OPC_SPECIAL,
201 OPC_BREAK = 0x0D | OPC_SPECIAL,
202 OPC_SPIM = 0x0E | OPC_SPECIAL, /* unofficial */
203 OPC_SYNC = 0x0F | OPC_SPECIAL,
205 OPC_SPECIAL15_RESERVED = 0x15 | OPC_SPECIAL,
206 OPC_SPECIAL28_RESERVED = 0x28 | OPC_SPECIAL,
207 OPC_SPECIAL29_RESERVED = 0x29 | OPC_SPECIAL,
208 OPC_SPECIAL35_RESERVED = 0x35 | OPC_SPECIAL,
209 OPC_SPECIAL37_RESERVED = 0x37 | OPC_SPECIAL,
210 OPC_SPECIAL39_RESERVED = 0x39 | OPC_SPECIAL,
211 OPC_SPECIAL3D_RESERVED = 0x3D | OPC_SPECIAL,
214 /* Multiplication variants of the vr54xx. */
215 #define MASK_MUL_VR54XX(op) MASK_SPECIAL(op) | (op & (0x1F << 6))
218 OPC_VR54XX_MULS = (0x03 << 6) | OPC_MULT,
219 OPC_VR54XX_MULSU = (0x03 << 6) | OPC_MULTU,
220 OPC_VR54XX_MACC = (0x05 << 6) | OPC_MULT,
221 OPC_VR54XX_MACCU = (0x05 << 6) | OPC_MULTU,
222 OPC_VR54XX_MSAC = (0x07 << 6) | OPC_MULT,
223 OPC_VR54XX_MSACU = (0x07 << 6) | OPC_MULTU,
224 OPC_VR54XX_MULHI = (0x09 << 6) | OPC_MULT,
225 OPC_VR54XX_MULHIU = (0x09 << 6) | OPC_MULTU,
226 OPC_VR54XX_MULSHI = (0x0B << 6) | OPC_MULT,
227 OPC_VR54XX_MULSHIU = (0x0B << 6) | OPC_MULTU,
228 OPC_VR54XX_MACCHI = (0x0D << 6) | OPC_MULT,
229 OPC_VR54XX_MACCHIU = (0x0D << 6) | OPC_MULTU,
230 OPC_VR54XX_MSACHI = (0x0F << 6) | OPC_MULT,
231 OPC_VR54XX_MSACHIU = (0x0F << 6) | OPC_MULTU,
234 /* REGIMM (rt field) opcodes */
235 #define MASK_REGIMM(op) MASK_OP_MAJOR(op) | (op & (0x1F << 16))
238 OPC_BLTZ = (0x00 << 16) | OPC_REGIMM,
239 OPC_BLTZL = (0x02 << 16) | OPC_REGIMM,
240 OPC_BGEZ = (0x01 << 16) | OPC_REGIMM,
241 OPC_BGEZL = (0x03 << 16) | OPC_REGIMM,
242 OPC_BLTZAL = (0x10 << 16) | OPC_REGIMM,
243 OPC_BLTZALL = (0x12 << 16) | OPC_REGIMM,
244 OPC_BGEZAL = (0x11 << 16) | OPC_REGIMM,
245 OPC_BGEZALL = (0x13 << 16) | OPC_REGIMM,
246 OPC_TGEI = (0x08 << 16) | OPC_REGIMM,
247 OPC_TGEIU = (0x09 << 16) | OPC_REGIMM,
248 OPC_TLTI = (0x0A << 16) | OPC_REGIMM,
249 OPC_TLTIU = (0x0B << 16) | OPC_REGIMM,
250 OPC_TEQI = (0x0C << 16) | OPC_REGIMM,
251 OPC_TNEI = (0x0E << 16) | OPC_REGIMM,
252 OPC_SYNCI = (0x1F << 16) | OPC_REGIMM,
255 /* Special2 opcodes */
256 #define MASK_SPECIAL2(op) MASK_OP_MAJOR(op) | (op & 0x3F)
259 /* Multiply & xxx operations */
260 OPC_MADD = 0x00 | OPC_SPECIAL2,
261 OPC_MADDU = 0x01 | OPC_SPECIAL2,
262 OPC_MUL = 0x02 | OPC_SPECIAL2,
263 OPC_MSUB = 0x04 | OPC_SPECIAL2,
264 OPC_MSUBU = 0x05 | OPC_SPECIAL2,
266 OPC_CLZ = 0x20 | OPC_SPECIAL2,
267 OPC_CLO = 0x21 | OPC_SPECIAL2,
268 OPC_DCLZ = 0x24 | OPC_SPECIAL2,
269 OPC_DCLO = 0x25 | OPC_SPECIAL2,
271 OPC_SDBBP = 0x3F | OPC_SPECIAL2,
274 /* Special3 opcodes */
275 #define MASK_SPECIAL3(op) MASK_OP_MAJOR(op) | (op & 0x3F)
278 OPC_EXT = 0x00 | OPC_SPECIAL3,
279 OPC_DEXTM = 0x01 | OPC_SPECIAL3,
280 OPC_DEXTU = 0x02 | OPC_SPECIAL3,
281 OPC_DEXT = 0x03 | OPC_SPECIAL3,
282 OPC_INS = 0x04 | OPC_SPECIAL3,
283 OPC_DINSM = 0x05 | OPC_SPECIAL3,
284 OPC_DINSU = 0x06 | OPC_SPECIAL3,
285 OPC_DINS = 0x07 | OPC_SPECIAL3,
286 OPC_FORK = 0x08 | OPC_SPECIAL3,
287 OPC_YIELD = 0x09 | OPC_SPECIAL3,
288 OPC_BSHFL = 0x20 | OPC_SPECIAL3,
289 OPC_DBSHFL = 0x24 | OPC_SPECIAL3,
290 OPC_RDHWR = 0x3B | OPC_SPECIAL3,
294 #define MASK_BSHFL(op) MASK_SPECIAL3(op) | (op & (0x1F << 6))
297 OPC_WSBH = (0x02 << 6) | OPC_BSHFL,
298 OPC_SEB = (0x10 << 6) | OPC_BSHFL,
299 OPC_SEH = (0x18 << 6) | OPC_BSHFL,
303 #define MASK_DBSHFL(op) MASK_SPECIAL3(op) | (op & (0x1F << 6))
306 OPC_DSBH = (0x02 << 6) | OPC_DBSHFL,
307 OPC_DSHD = (0x05 << 6) | OPC_DBSHFL,
310 /* Coprocessor 0 (rs field) */
311 #define MASK_CP0(op) MASK_OP_MAJOR(op) | (op & (0x1F << 21))
314 OPC_MFC0 = (0x00 << 21) | OPC_CP0,
315 OPC_DMFC0 = (0x01 << 21) | OPC_CP0,
316 OPC_MTC0 = (0x04 << 21) | OPC_CP0,
317 OPC_DMTC0 = (0x05 << 21) | OPC_CP0,
318 OPC_MFTR = (0x08 << 21) | OPC_CP0,
319 OPC_RDPGPR = (0x0A << 21) | OPC_CP0,
320 OPC_MFMC0 = (0x0B << 21) | OPC_CP0,
321 OPC_MTTR = (0x0C << 21) | OPC_CP0,
322 OPC_WRPGPR = (0x0E << 21) | OPC_CP0,
323 OPC_C0 = (0x10 << 21) | OPC_CP0,
324 OPC_C0_FIRST = (0x10 << 21) | OPC_CP0,
325 OPC_C0_LAST = (0x1F << 21) | OPC_CP0,
329 #define MASK_MFMC0(op) MASK_CP0(op) | (op & 0xFFFF)
332 OPC_DMT = 0x01 | (0 << 5) | (0x0F << 6) | (0x01 << 11) | OPC_MFMC0,
333 OPC_EMT = 0x01 | (1 << 5) | (0x0F << 6) | (0x01 << 11) | OPC_MFMC0,
334 OPC_DVPE = 0x01 | (0 << 5) | OPC_MFMC0,
335 OPC_EVPE = 0x01 | (1 << 5) | OPC_MFMC0,
336 OPC_DI = (0 << 5) | (0x0C << 11) | OPC_MFMC0,
337 OPC_EI = (1 << 5) | (0x0C << 11) | OPC_MFMC0,
340 /* Coprocessor 0 (with rs == C0) */
341 #define MASK_C0(op) MASK_CP0(op) | (op & 0x3F)
344 OPC_TLBR = 0x01 | OPC_C0,
345 OPC_TLBWI = 0x02 | OPC_C0,
346 OPC_TLBWR = 0x06 | OPC_C0,
347 OPC_TLBP = 0x08 | OPC_C0,
348 OPC_RFE = 0x10 | OPC_C0,
349 OPC_ERET = 0x18 | OPC_C0,
350 OPC_DERET = 0x1F | OPC_C0,
351 OPC_WAIT = 0x20 | OPC_C0,
354 /* Coprocessor 1 (rs field) */
355 #define MASK_CP1(op) MASK_OP_MAJOR(op) | (op & (0x1F << 21))
358 OPC_MFC1 = (0x00 << 21) | OPC_CP1,
359 OPC_DMFC1 = (0x01 << 21) | OPC_CP1,
360 OPC_CFC1 = (0x02 << 21) | OPC_CP1,
361 OPC_MFHC1 = (0x03 << 21) | OPC_CP1,
362 OPC_MTC1 = (0x04 << 21) | OPC_CP1,
363 OPC_DMTC1 = (0x05 << 21) | OPC_CP1,
364 OPC_CTC1 = (0x06 << 21) | OPC_CP1,
365 OPC_MTHC1 = (0x07 << 21) | OPC_CP1,
366 OPC_BC1 = (0x08 << 21) | OPC_CP1, /* bc */
367 OPC_BC1ANY2 = (0x09 << 21) | OPC_CP1,
368 OPC_BC1ANY4 = (0x0A << 21) | OPC_CP1,
369 OPC_S_FMT = (0x10 << 21) | OPC_CP1, /* 16: fmt=single fp */
370 OPC_D_FMT = (0x11 << 21) | OPC_CP1, /* 17: fmt=double fp */
371 OPC_E_FMT = (0x12 << 21) | OPC_CP1, /* 18: fmt=extended fp */
372 OPC_Q_FMT = (0x13 << 21) | OPC_CP1, /* 19: fmt=quad fp */
373 OPC_W_FMT = (0x14 << 21) | OPC_CP1, /* 20: fmt=32bit fixed */
374 OPC_L_FMT = (0x15 << 21) | OPC_CP1, /* 21: fmt=64bit fixed */
375 OPC_PS_FMT = (0x16 << 21) | OPC_CP1, /* 22: fmt=paired single fp */
378 #define MASK_CP1_FUNC(op) MASK_CP1(op) | (op & 0x3F)
379 #define MASK_BC1(op) MASK_CP1(op) | (op & (0x3 << 16))
382 OPC_BC1F = (0x00 << 16) | OPC_BC1,
383 OPC_BC1T = (0x01 << 16) | OPC_BC1,
384 OPC_BC1FL = (0x02 << 16) | OPC_BC1,
385 OPC_BC1TL = (0x03 << 16) | OPC_BC1,
389 OPC_BC1FANY2 = (0x00 << 16) | OPC_BC1ANY2,
390 OPC_BC1TANY2 = (0x01 << 16) | OPC_BC1ANY2,
394 OPC_BC1FANY4 = (0x00 << 16) | OPC_BC1ANY4,
395 OPC_BC1TANY4 = (0x01 << 16) | OPC_BC1ANY4,
398 #define MASK_CP2(op) MASK_OP_MAJOR(op) | (op & (0x1F << 21))
401 OPC_MFC2 = (0x00 << 21) | OPC_CP2,
402 OPC_DMFC2 = (0x01 << 21) | OPC_CP2,
403 OPC_CFC2 = (0x02 << 21) | OPC_CP2,
404 OPC_MFHC2 = (0x03 << 21) | OPC_CP2,
405 OPC_MTC2 = (0x04 << 21) | OPC_CP2,
406 OPC_DMTC2 = (0x05 << 21) | OPC_CP2,
407 OPC_CTC2 = (0x06 << 21) | OPC_CP2,
408 OPC_MTHC2 = (0x07 << 21) | OPC_CP2,
409 OPC_BC2 = (0x08 << 21) | OPC_CP2,
412 #define MASK_CP3(op) MASK_OP_MAJOR(op) | (op & 0x3F)
415 OPC_LWXC1 = 0x00 | OPC_CP3,
416 OPC_LDXC1 = 0x01 | OPC_CP3,
417 OPC_LUXC1 = 0x05 | OPC_CP3,
418 OPC_SWXC1 = 0x08 | OPC_CP3,
419 OPC_SDXC1 = 0x09 | OPC_CP3,
420 OPC_SUXC1 = 0x0D | OPC_CP3,
421 OPC_PREFX = 0x0F | OPC_CP3,
422 OPC_ALNV_PS = 0x1E | OPC_CP3,
423 OPC_MADD_S = 0x20 | OPC_CP3,
424 OPC_MADD_D = 0x21 | OPC_CP3,
425 OPC_MADD_PS = 0x26 | OPC_CP3,
426 OPC_MSUB_S = 0x28 | OPC_CP3,
427 OPC_MSUB_D = 0x29 | OPC_CP3,
428 OPC_MSUB_PS = 0x2E | OPC_CP3,
429 OPC_NMADD_S = 0x30 | OPC_CP3,
430 OPC_NMADD_D = 0x31 | OPC_CP3,
431 OPC_NMADD_PS= 0x36 | OPC_CP3,
432 OPC_NMSUB_S = 0x38 | OPC_CP3,
433 OPC_NMSUB_D = 0x39 | OPC_CP3,
434 OPC_NMSUB_PS= 0x3E | OPC_CP3,
437 /* global register indices */
438 static TCGv_ptr cpu_env;
439 static TCGv cpu_gpr[32], cpu_PC;
440 static TCGv cpu_HI[MIPS_DSP_ACC], cpu_LO[MIPS_DSP_ACC], cpu_ACX[MIPS_DSP_ACC];
441 static TCGv cpu_dspctrl, btarget, bcond;
442 static TCGv_i32 hflags;
443 static TCGv_i32 fpu_fcr0, fpu_fcr31;
445 #include "gen-icount.h"
447 #define gen_helper_0i(name, arg) do { \
448 TCGv_i32 helper_tmp = tcg_const_i32(arg); \
449 gen_helper_##name(helper_tmp); \
450 tcg_temp_free_i32(helper_tmp); \
453 #define gen_helper_1i(name, arg1, arg2) do { \
454 TCGv_i32 helper_tmp = tcg_const_i32(arg2); \
455 gen_helper_##name(arg1, helper_tmp); \
456 tcg_temp_free_i32(helper_tmp); \
459 #define gen_helper_2i(name, arg1, arg2, arg3) do { \
460 TCGv_i32 helper_tmp = tcg_const_i32(arg3); \
461 gen_helper_##name(arg1, arg2, helper_tmp); \
462 tcg_temp_free_i32(helper_tmp); \
465 #define gen_helper_3i(name, arg1, arg2, arg3, arg4) do { \
466 TCGv_i32 helper_tmp = tcg_const_i32(arg4); \
467 gen_helper_##name(arg1, arg2, arg3, helper_tmp); \
468 tcg_temp_free_i32(helper_tmp); \
471 typedef struct DisasContext {
472 struct TranslationBlock *tb;
473 target_ulong pc, saved_pc;
475 int singlestep_enabled;
476 /* Routine used to access memory */
478 uint32_t hflags, saved_hflags;
480 target_ulong btarget;
484 BS_NONE = 0, /* We go out of the TB without reaching a branch or an
485 * exception condition */
486 BS_STOP = 1, /* We want to stop translation for any reason */
487 BS_BRANCH = 2, /* We reached a branch condition */
488 BS_EXCP = 3, /* We reached an exception condition */
491 static const char *regnames[] =
492 { "r0", "at", "v0", "v1", "a0", "a1", "a2", "a3",
493 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
494 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
495 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra", };
497 static const char *regnames_HI[] =
498 { "HI0", "HI1", "HI2", "HI3", };
500 static const char *regnames_LO[] =
501 { "LO0", "LO1", "LO2", "LO3", };
503 static const char *regnames_ACX[] =
504 { "ACX0", "ACX1", "ACX2", "ACX3", };
506 static const char *fregnames[] =
507 { "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
508 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
509 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
510 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31", };
512 #ifdef MIPS_DEBUG_DISAS
513 #define MIPS_DEBUG(fmt, ...) \
514 qemu_log_mask(CPU_LOG_TB_IN_ASM, \
515 TARGET_FMT_lx ": %08x " fmt "\n", \
516 ctx->pc, ctx->opcode , ## __VA_ARGS__)
517 #define LOG_DISAS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
519 #define MIPS_DEBUG(fmt, ...) do { } while(0)
520 #define LOG_DISAS(...) do { } while (0)
523 #define MIPS_INVAL(op) \
525 MIPS_DEBUG("Invalid %s %03x %03x %03x", op, ctx->opcode >> 26, \
526 ctx->opcode & 0x3F, ((ctx->opcode >> 16) & 0x1F)); \
529 /* General purpose registers moves. */
530 static inline void gen_load_gpr (TCGv t, int reg)
533 tcg_gen_movi_tl(t, 0);
535 tcg_gen_mov_tl(t, cpu_gpr[reg]);
538 static inline void gen_store_gpr (TCGv t, int reg)
541 tcg_gen_mov_tl(cpu_gpr[reg], t);
544 /* Moves to/from ACX register. */
545 static inline void gen_load_ACX (TCGv t, int reg)
547 tcg_gen_mov_tl(t, cpu_ACX[reg]);
550 static inline void gen_store_ACX (TCGv t, int reg)
552 tcg_gen_mov_tl(cpu_ACX[reg], t);
555 /* Moves to/from shadow registers. */
556 static inline void gen_load_srsgpr (int from, int to)
558 TCGv t0 = tcg_temp_new();
561 tcg_gen_movi_tl(t0, 0);
563 TCGv_i32 t2 = tcg_temp_new_i32();
564 TCGv_ptr addr = tcg_temp_new_ptr();
566 tcg_gen_ld_i32(t2, cpu_env, offsetof(CPUState, CP0_SRSCtl));
567 tcg_gen_shri_i32(t2, t2, CP0SRSCtl_PSS);
568 tcg_gen_andi_i32(t2, t2, 0xf);
569 tcg_gen_muli_i32(t2, t2, sizeof(target_ulong) * 32);
570 tcg_gen_ext_i32_ptr(addr, t2);
571 tcg_gen_add_ptr(addr, cpu_env, addr);
573 tcg_gen_ld_tl(t0, addr, sizeof(target_ulong) * from);
574 tcg_temp_free_ptr(addr);
575 tcg_temp_free_i32(t2);
577 gen_store_gpr(t0, to);
581 static inline void gen_store_srsgpr (int from, int to)
584 TCGv t0 = tcg_temp_new();
585 TCGv_i32 t2 = tcg_temp_new_i32();
586 TCGv_ptr addr = tcg_temp_new_ptr();
588 gen_load_gpr(t0, from);
589 tcg_gen_ld_i32(t2, cpu_env, offsetof(CPUState, CP0_SRSCtl));
590 tcg_gen_shri_i32(t2, t2, CP0SRSCtl_PSS);
591 tcg_gen_andi_i32(t2, t2, 0xf);
592 tcg_gen_muli_i32(t2, t2, sizeof(target_ulong) * 32);
593 tcg_gen_ext_i32_ptr(addr, t2);
594 tcg_gen_add_ptr(addr, cpu_env, addr);
596 tcg_gen_st_tl(t0, addr, sizeof(target_ulong) * to);
597 tcg_temp_free_ptr(addr);
598 tcg_temp_free_i32(t2);
603 /* Floating point register moves. */
604 static inline void gen_load_fpr32 (TCGv_i32 t, int reg)
606 tcg_gen_ld_i32(t, cpu_env, offsetof(CPUState, active_fpu.fpr[reg].w[FP_ENDIAN_IDX]));
609 static inline void gen_store_fpr32 (TCGv_i32 t, int reg)
611 tcg_gen_st_i32(t, cpu_env, offsetof(CPUState, active_fpu.fpr[reg].w[FP_ENDIAN_IDX]));
614 static inline void gen_load_fpr32h (TCGv_i32 t, int reg)
616 tcg_gen_ld_i32(t, cpu_env, offsetof(CPUState, active_fpu.fpr[reg].w[!FP_ENDIAN_IDX]));
619 static inline void gen_store_fpr32h (TCGv_i32 t, int reg)
621 tcg_gen_st_i32(t, cpu_env, offsetof(CPUState, active_fpu.fpr[reg].w[!FP_ENDIAN_IDX]));
624 static inline void gen_load_fpr64 (DisasContext *ctx, TCGv_i64 t, int reg)
626 if (ctx->hflags & MIPS_HFLAG_F64) {
627 tcg_gen_ld_i64(t, cpu_env, offsetof(CPUState, active_fpu.fpr[reg].d));
629 TCGv_i32 t0 = tcg_temp_new_i32();
630 TCGv_i32 t1 = tcg_temp_new_i32();
631 gen_load_fpr32(t0, reg & ~1);
632 gen_load_fpr32(t1, reg | 1);
633 tcg_gen_concat_i32_i64(t, t0, t1);
634 tcg_temp_free_i32(t0);
635 tcg_temp_free_i32(t1);
639 static inline void gen_store_fpr64 (DisasContext *ctx, TCGv_i64 t, int reg)
641 if (ctx->hflags & MIPS_HFLAG_F64) {
642 tcg_gen_st_i64(t, cpu_env, offsetof(CPUState, active_fpu.fpr[reg].d));
644 TCGv_i64 t0 = tcg_temp_new_i64();
645 TCGv_i32 t1 = tcg_temp_new_i32();
646 tcg_gen_trunc_i64_i32(t1, t);
647 gen_store_fpr32(t1, reg & ~1);
648 tcg_gen_shri_i64(t0, t, 32);
649 tcg_gen_trunc_i64_i32(t1, t0);
650 gen_store_fpr32(t1, reg | 1);
651 tcg_temp_free_i32(t1);
652 tcg_temp_free_i64(t0);
656 static inline int get_fp_bit (int cc)
664 #define FOP_CONDS(type, fmt, bits) \
665 static inline void gen_cmp ## type ## _ ## fmt(int n, TCGv_i##bits a, \
666 TCGv_i##bits b, int cc) \
669 case 0: gen_helper_2i(cmp ## type ## _ ## fmt ## _f, a, b, cc); break;\
670 case 1: gen_helper_2i(cmp ## type ## _ ## fmt ## _un, a, b, cc); break;\
671 case 2: gen_helper_2i(cmp ## type ## _ ## fmt ## _eq, a, b, cc); break;\
672 case 3: gen_helper_2i(cmp ## type ## _ ## fmt ## _ueq, a, b, cc); break;\
673 case 4: gen_helper_2i(cmp ## type ## _ ## fmt ## _olt, a, b, cc); break;\
674 case 5: gen_helper_2i(cmp ## type ## _ ## fmt ## _ult, a, b, cc); break;\
675 case 6: gen_helper_2i(cmp ## type ## _ ## fmt ## _ole, a, b, cc); break;\
676 case 7: gen_helper_2i(cmp ## type ## _ ## fmt ## _ule, a, b, cc); break;\
677 case 8: gen_helper_2i(cmp ## type ## _ ## fmt ## _sf, a, b, cc); break;\
678 case 9: gen_helper_2i(cmp ## type ## _ ## fmt ## _ngle, a, b, cc); break;\
679 case 10: gen_helper_2i(cmp ## type ## _ ## fmt ## _seq, a, b, cc); break;\
680 case 11: gen_helper_2i(cmp ## type ## _ ## fmt ## _ngl, a, b, cc); break;\
681 case 12: gen_helper_2i(cmp ## type ## _ ## fmt ## _lt, a, b, cc); break;\
682 case 13: gen_helper_2i(cmp ## type ## _ ## fmt ## _nge, a, b, cc); break;\
683 case 14: gen_helper_2i(cmp ## type ## _ ## fmt ## _le, a, b, cc); break;\
684 case 15: gen_helper_2i(cmp ## type ## _ ## fmt ## _ngt, a, b, cc); break;\
690 FOP_CONDS(abs, d, 64)
692 FOP_CONDS(abs, s, 32)
694 FOP_CONDS(abs, ps, 64)
698 #define OP_COND(name, cond) \
699 static inline void glue(gen_op_, name) (TCGv ret, TCGv t0, TCGv t1) \
701 int l1 = gen_new_label(); \
702 int l2 = gen_new_label(); \
704 tcg_gen_brcond_tl(cond, t0, t1, l1); \
705 tcg_gen_movi_tl(ret, 0); \
708 tcg_gen_movi_tl(ret, 1); \
711 OP_COND(eq, TCG_COND_EQ);
712 OP_COND(ne, TCG_COND_NE);
713 OP_COND(ge, TCG_COND_GE);
714 OP_COND(geu, TCG_COND_GEU);
715 OP_COND(lt, TCG_COND_LT);
716 OP_COND(ltu, TCG_COND_LTU);
719 #define OP_CONDI(name, cond) \
720 static inline void glue(gen_op_, name) (TCGv ret, TCGv t0, target_ulong val) \
722 int l1 = gen_new_label(); \
723 int l2 = gen_new_label(); \
725 tcg_gen_brcondi_tl(cond, t0, val, l1); \
726 tcg_gen_movi_tl(ret, 0); \
729 tcg_gen_movi_tl(ret, 1); \
732 OP_CONDI(lti, TCG_COND_LT);
733 OP_CONDI(ltiu, TCG_COND_LTU);
736 #define OP_CONDZ(name, cond) \
737 static inline void glue(gen_op_, name) (TCGv ret, TCGv t0) \
739 int l1 = gen_new_label(); \
740 int l2 = gen_new_label(); \
742 tcg_gen_brcondi_tl(cond, t0, 0, l1); \
743 tcg_gen_movi_tl(ret, 0); \
746 tcg_gen_movi_tl(ret, 1); \
749 OP_CONDZ(gez, TCG_COND_GE);
750 OP_CONDZ(gtz, TCG_COND_GT);
751 OP_CONDZ(lez, TCG_COND_LE);
752 OP_CONDZ(ltz, TCG_COND_LT);
755 static inline void gen_save_pc(target_ulong pc)
757 tcg_gen_movi_tl(cpu_PC, pc);
760 static inline void save_cpu_state (DisasContext *ctx, int do_save_pc)
762 LOG_DISAS("hflags %08x saved %08x\n", ctx->hflags, ctx->saved_hflags);
763 if (do_save_pc && ctx->pc != ctx->saved_pc) {
764 gen_save_pc(ctx->pc);
765 ctx->saved_pc = ctx->pc;
767 if (ctx->hflags != ctx->saved_hflags) {
768 tcg_gen_movi_i32(hflags, ctx->hflags);
769 ctx->saved_hflags = ctx->hflags;
770 switch (ctx->hflags & MIPS_HFLAG_BMASK_BASE) {
776 tcg_gen_movi_tl(btarget, ctx->btarget);
782 static inline void restore_cpu_state (CPUState *env, DisasContext *ctx)
784 ctx->saved_hflags = ctx->hflags;
785 switch (ctx->hflags & MIPS_HFLAG_BMASK_BASE) {
791 ctx->btarget = env->btarget;
797 generate_exception_err (DisasContext *ctx, int excp, int err)
799 TCGv_i32 texcp = tcg_const_i32(excp);
800 TCGv_i32 terr = tcg_const_i32(err);
801 save_cpu_state(ctx, 1);
802 gen_helper_raise_exception_err(texcp, terr);
803 tcg_temp_free_i32(terr);
804 tcg_temp_free_i32(texcp);
808 generate_exception (DisasContext *ctx, int excp)
810 save_cpu_state(ctx, 1);
811 gen_helper_0i(raise_exception, excp);
814 /* Addresses computation */
815 static inline void gen_op_addr_add (DisasContext *ctx, TCGv ret, TCGv arg0, TCGv arg1)
817 tcg_gen_add_tl(ret, arg0, arg1);
819 #if defined(TARGET_MIPS64)
820 /* For compatibility with 32-bit code, data reference in user mode
821 with Status_UX = 0 should be casted to 32-bit and sign extended.
822 See the MIPS64 PRA manual, section 4.10. */
823 if (((ctx->hflags & MIPS_HFLAG_KSU) == MIPS_HFLAG_UM) &&
824 !(ctx->hflags & MIPS_HFLAG_UX)) {
825 tcg_gen_ext32s_i64(ret, ret);
830 static inline void check_cp0_enabled(DisasContext *ctx)
832 if (unlikely(!(ctx->hflags & MIPS_HFLAG_CP0)))
833 generate_exception_err(ctx, EXCP_CpU, 1);
836 static inline void check_cp1_enabled(DisasContext *ctx)
838 if (unlikely(!(ctx->hflags & MIPS_HFLAG_FPU)))
839 generate_exception_err(ctx, EXCP_CpU, 1);
842 /* Verify that the processor is running with COP1X instructions enabled.
843 This is associated with the nabla symbol in the MIPS32 and MIPS64
846 static inline void check_cop1x(DisasContext *ctx)
848 if (unlikely(!(ctx->hflags & MIPS_HFLAG_COP1X)))
849 generate_exception(ctx, EXCP_RI);
852 /* Verify that the processor is running with 64-bit floating-point
853 operations enabled. */
855 static inline void check_cp1_64bitmode(DisasContext *ctx)
857 if (unlikely(~ctx->hflags & (MIPS_HFLAG_F64 | MIPS_HFLAG_COP1X)))
858 generate_exception(ctx, EXCP_RI);
862 * Verify if floating point register is valid; an operation is not defined
863 * if bit 0 of any register specification is set and the FR bit in the
864 * Status register equals zero, since the register numbers specify an
865 * even-odd pair of adjacent coprocessor general registers. When the FR bit
866 * in the Status register equals one, both even and odd register numbers
867 * are valid. This limitation exists only for 64 bit wide (d,l,ps) registers.
869 * Multiple 64 bit wide registers can be checked by calling
870 * gen_op_cp1_registers(freg1 | freg2 | ... | fregN);
872 static inline void check_cp1_registers(DisasContext *ctx, int regs)
874 if (unlikely(!(ctx->hflags & MIPS_HFLAG_F64) && (regs & 1)))
875 generate_exception(ctx, EXCP_RI);
878 /* This code generates a "reserved instruction" exception if the
879 CPU does not support the instruction set corresponding to flags. */
880 static inline void check_insn(CPUState *env, DisasContext *ctx, int flags)
882 if (unlikely(!(env->insn_flags & flags)))
883 generate_exception(ctx, EXCP_RI);
886 /* This code generates a "reserved instruction" exception if 64-bit
887 instructions are not enabled. */
888 static inline void check_mips_64(DisasContext *ctx)
890 if (unlikely(!(ctx->hflags & MIPS_HFLAG_64)))
891 generate_exception(ctx, EXCP_RI);
894 /* load/store instructions. */
895 #define OP_LD(insn,fname) \
896 static inline void op_ldst_##insn(TCGv ret, TCGv arg1, DisasContext *ctx) \
898 tcg_gen_qemu_##fname(ret, arg1, ctx->mem_idx); \
905 #if defined(TARGET_MIPS64)
911 #define OP_ST(insn,fname) \
912 static inline void op_ldst_##insn(TCGv arg1, TCGv arg2, DisasContext *ctx) \
914 tcg_gen_qemu_##fname(arg1, arg2, ctx->mem_idx); \
919 #if defined(TARGET_MIPS64)
924 #ifdef CONFIG_USER_ONLY
925 #define OP_LD_ATOMIC(insn,fname) \
926 static inline void op_ldst_##insn(TCGv ret, TCGv arg1, DisasContext *ctx) \
928 TCGv t0 = tcg_temp_new(); \
929 tcg_gen_mov_tl(t0, arg1); \
930 tcg_gen_qemu_##fname(ret, arg1, ctx->mem_idx); \
931 tcg_gen_st_tl(t0, cpu_env, offsetof(CPUState, lladdr)); \
932 tcg_gen_st_tl(ret, cpu_env, offsetof(CPUState, llval)); \
936 #define OP_LD_ATOMIC(insn,fname) \
937 static inline void op_ldst_##insn(TCGv ret, TCGv arg1, DisasContext *ctx) \
939 gen_helper_2i(insn, ret, arg1, ctx->mem_idx); \
942 OP_LD_ATOMIC(ll,ld32s);
943 #if defined(TARGET_MIPS64)
944 OP_LD_ATOMIC(lld,ld64);
948 #ifdef CONFIG_USER_ONLY
949 #define OP_ST_ATOMIC(insn,fname,ldname,almask) \
950 static inline void op_ldst_##insn(TCGv arg1, TCGv arg2, int rt, DisasContext *ctx) \
952 TCGv t0 = tcg_temp_new(); \
953 int l1 = gen_new_label(); \
954 int l2 = gen_new_label(); \
956 tcg_gen_andi_tl(t0, arg2, almask); \
957 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1); \
958 tcg_gen_st_tl(arg2, cpu_env, offsetof(CPUState, CP0_BadVAddr)); \
959 generate_exception(ctx, EXCP_AdES); \
961 tcg_gen_ld_tl(t0, cpu_env, offsetof(CPUState, lladdr)); \
962 tcg_gen_brcond_tl(TCG_COND_NE, arg2, t0, l2); \
963 tcg_gen_movi_tl(t0, rt | ((almask << 3) & 0x20)); \
964 tcg_gen_st_tl(t0, cpu_env, offsetof(CPUState, llreg)); \
965 tcg_gen_st_tl(arg1, cpu_env, offsetof(CPUState, llnewval)); \
966 gen_helper_0i(raise_exception, EXCP_SC); \
968 tcg_gen_movi_tl(t0, 0); \
969 gen_store_gpr(t0, rt); \
973 #define OP_ST_ATOMIC(insn,fname,ldname,almask) \
974 static inline void op_ldst_##insn(TCGv arg1, TCGv arg2, int rt, DisasContext *ctx) \
976 TCGv t0 = tcg_temp_new(); \
977 gen_helper_3i(insn, t0, arg1, arg2, ctx->mem_idx); \
978 gen_store_gpr(t0, rt); \
982 OP_ST_ATOMIC(sc,st32,ld32s,0x3);
983 #if defined(TARGET_MIPS64)
984 OP_ST_ATOMIC(scd,st64,ld64,0x7);
988 static void gen_base_offset_addr (DisasContext *ctx, TCGv addr,
989 int base, int16_t offset)
992 tcg_gen_movi_tl(addr, offset);
993 } else if (offset == 0) {
994 gen_load_gpr(addr, base);
996 tcg_gen_movi_tl(addr, offset);
997 gen_op_addr_add(ctx, addr, cpu_gpr[base], addr);
1001 static target_ulong pc_relative_pc (DisasContext *ctx)
1003 target_ulong pc = ctx->pc;
1005 if (ctx->hflags & MIPS_HFLAG_BMASK) {
1006 int branch_bytes = ctx->hflags & MIPS_HFLAG_BDS16 ? 2 : 4;
1011 pc &= ~(target_ulong)3;
1015 /* Load and store */
1016 static void gen_ldst (DisasContext *ctx, uint32_t opc, int rt,
1017 int base, int16_t offset)
1019 const char *opn = "ldst";
1020 TCGv t0 = tcg_temp_new();
1021 TCGv t1 = tcg_temp_new();
1023 gen_base_offset_addr(ctx, t0, base, offset);
1024 /* Don't do NOP if destination is zero: we must perform the actual
1027 #if defined(TARGET_MIPS64)
1029 save_cpu_state(ctx, 0);
1030 op_ldst_lwu(t0, t0, ctx);
1031 gen_store_gpr(t0, rt);
1035 save_cpu_state(ctx, 0);
1036 op_ldst_ld(t0, t0, ctx);
1037 gen_store_gpr(t0, rt);
1041 save_cpu_state(ctx, 0);
1042 op_ldst_lld(t0, t0, ctx);
1043 gen_store_gpr(t0, rt);
1047 save_cpu_state(ctx, 0);
1048 gen_load_gpr(t1, rt);
1049 op_ldst_sd(t1, t0, ctx);
1053 save_cpu_state(ctx, 1);
1054 gen_load_gpr(t1, rt);
1055 gen_helper_3i(ldl, t1, t1, t0, ctx->mem_idx);
1056 gen_store_gpr(t1, rt);
1060 save_cpu_state(ctx, 1);
1061 gen_load_gpr(t1, rt);
1062 gen_helper_2i(sdl, t1, t0, ctx->mem_idx);
1066 save_cpu_state(ctx, 1);
1067 gen_load_gpr(t1, rt);
1068 gen_helper_3i(ldr, t1, t1, t0, ctx->mem_idx);
1069 gen_store_gpr(t1, rt);
1073 save_cpu_state(ctx, 1);
1074 gen_load_gpr(t1, rt);
1075 gen_helper_2i(sdr, t1, t0, ctx->mem_idx);
1079 save_cpu_state(ctx, 1);
1080 tcg_gen_movi_tl(t1, pc_relative_pc(ctx));
1081 gen_op_addr_add(ctx, t0, t0, t1);
1082 op_ldst_ld(t0, t0, ctx);
1083 gen_store_gpr(t0, rt);
1087 save_cpu_state(ctx, 1);
1088 tcg_gen_movi_tl(t1, pc_relative_pc(ctx));
1089 gen_op_addr_add(ctx, t0, t0, t1);
1090 op_ldst_lw(t0, t0, ctx);
1091 gen_store_gpr(t0, rt);
1094 save_cpu_state(ctx, 0);
1095 op_ldst_lw(t0, t0, ctx);
1096 gen_store_gpr(t0, rt);
1100 save_cpu_state(ctx, 0);
1101 gen_load_gpr(t1, rt);
1102 op_ldst_sw(t1, t0, ctx);
1106 save_cpu_state(ctx, 0);
1107 op_ldst_lh(t0, t0, ctx);
1108 gen_store_gpr(t0, rt);
1112 save_cpu_state(ctx, 0);
1113 gen_load_gpr(t1, rt);
1114 op_ldst_sh(t1, t0, ctx);
1118 save_cpu_state(ctx, 0);
1119 op_ldst_lhu(t0, t0, ctx);
1120 gen_store_gpr(t0, rt);
1124 save_cpu_state(ctx, 0);
1125 op_ldst_lb(t0, t0, ctx);
1126 gen_store_gpr(t0, rt);
1130 save_cpu_state(ctx, 0);
1131 gen_load_gpr(t1, rt);
1132 op_ldst_sb(t1, t0, ctx);
1136 save_cpu_state(ctx, 0);
1137 op_ldst_lbu(t0, t0, ctx);
1138 gen_store_gpr(t0, rt);
1142 save_cpu_state(ctx, 1);
1143 gen_load_gpr(t1, rt);
1144 gen_helper_3i(lwl, t1, t1, t0, ctx->mem_idx);
1145 gen_store_gpr(t1, rt);
1149 save_cpu_state(ctx, 1);
1150 gen_load_gpr(t1, rt);
1151 gen_helper_2i(swl, t1, t0, ctx->mem_idx);
1155 save_cpu_state(ctx, 1);
1156 gen_load_gpr(t1, rt);
1157 gen_helper_3i(lwr, t1, t1, t0, ctx->mem_idx);
1158 gen_store_gpr(t1, rt);
1162 save_cpu_state(ctx, 1);
1163 gen_load_gpr(t1, rt);
1164 gen_helper_2i(swr, t1, t0, ctx->mem_idx);
1168 save_cpu_state(ctx, 1);
1169 op_ldst_ll(t0, t0, ctx);
1170 gen_store_gpr(t0, rt);
1174 MIPS_DEBUG("%s %s, %d(%s)", opn, regnames[rt], offset, regnames[base]);
1179 /* Store conditional */
1180 static void gen_st_cond (DisasContext *ctx, uint32_t opc, int rt,
1181 int base, int16_t offset)
1183 const char *opn = "st_cond";
1186 t0 = tcg_temp_local_new();
1188 gen_base_offset_addr(ctx, t0, base, offset);
1189 /* Don't do NOP if destination is zero: we must perform the actual
1192 t1 = tcg_temp_local_new();
1193 gen_load_gpr(t1, rt);
1195 #if defined(TARGET_MIPS64)
1197 save_cpu_state(ctx, 0);
1198 op_ldst_scd(t1, t0, rt, ctx);
1203 save_cpu_state(ctx, 1);
1204 op_ldst_sc(t1, t0, rt, ctx);
1208 MIPS_DEBUG("%s %s, %d(%s)", opn, regnames[rt], offset, regnames[base]);
1213 /* Load and store */
1214 static void gen_flt_ldst (DisasContext *ctx, uint32_t opc, int ft,
1215 int base, int16_t offset)
1217 const char *opn = "flt_ldst";
1218 TCGv t0 = tcg_temp_new();
1220 gen_base_offset_addr(ctx, t0, base, offset);
1221 /* Don't do NOP if destination is zero: we must perform the actual
1226 TCGv_i32 fp0 = tcg_temp_new_i32();
1228 tcg_gen_qemu_ld32s(t0, t0, ctx->mem_idx);
1229 tcg_gen_trunc_tl_i32(fp0, t0);
1230 gen_store_fpr32(fp0, ft);
1231 tcg_temp_free_i32(fp0);
1237 TCGv_i32 fp0 = tcg_temp_new_i32();
1238 TCGv t1 = tcg_temp_new();
1240 gen_load_fpr32(fp0, ft);
1241 tcg_gen_extu_i32_tl(t1, fp0);
1242 tcg_gen_qemu_st32(t1, t0, ctx->mem_idx);
1244 tcg_temp_free_i32(fp0);
1250 TCGv_i64 fp0 = tcg_temp_new_i64();
1252 tcg_gen_qemu_ld64(fp0, t0, ctx->mem_idx);
1253 gen_store_fpr64(ctx, fp0, ft);
1254 tcg_temp_free_i64(fp0);
1260 TCGv_i64 fp0 = tcg_temp_new_i64();
1262 gen_load_fpr64(ctx, fp0, ft);
1263 tcg_gen_qemu_st64(fp0, t0, ctx->mem_idx);
1264 tcg_temp_free_i64(fp0);
1270 generate_exception(ctx, EXCP_RI);
1273 MIPS_DEBUG("%s %s, %d(%s)", opn, fregnames[ft], offset, regnames[base]);
1278 /* Arithmetic with immediate operand */
1279 static void gen_arith_imm (CPUState *env, DisasContext *ctx, uint32_t opc,
1280 int rt, int rs, int16_t imm)
1282 target_ulong uimm = (target_long)imm; /* Sign extend to 32/64 bits */
1283 const char *opn = "imm arith";
1285 if (rt == 0 && opc != OPC_ADDI && opc != OPC_DADDI) {
1286 /* If no destination, treat it as a NOP.
1287 For addi, we must generate the overflow exception when needed. */
1294 TCGv t0 = tcg_temp_local_new();
1295 TCGv t1 = tcg_temp_new();
1296 TCGv t2 = tcg_temp_new();
1297 int l1 = gen_new_label();
1299 gen_load_gpr(t1, rs);
1300 tcg_gen_addi_tl(t0, t1, uimm);
1301 tcg_gen_ext32s_tl(t0, t0);
1303 tcg_gen_xori_tl(t1, t1, ~uimm);
1304 tcg_gen_xori_tl(t2, t0, uimm);
1305 tcg_gen_and_tl(t1, t1, t2);
1307 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
1309 /* operands of same sign, result different sign */
1310 generate_exception(ctx, EXCP_OVERFLOW);
1312 tcg_gen_ext32s_tl(t0, t0);
1313 gen_store_gpr(t0, rt);
1320 tcg_gen_addi_tl(cpu_gpr[rt], cpu_gpr[rs], uimm);
1321 tcg_gen_ext32s_tl(cpu_gpr[rt], cpu_gpr[rt]);
1323 tcg_gen_movi_tl(cpu_gpr[rt], uimm);
1327 #if defined(TARGET_MIPS64)
1330 TCGv t0 = tcg_temp_local_new();
1331 TCGv t1 = tcg_temp_new();
1332 TCGv t2 = tcg_temp_new();
1333 int l1 = gen_new_label();
1335 gen_load_gpr(t1, rs);
1336 tcg_gen_addi_tl(t0, t1, uimm);
1338 tcg_gen_xori_tl(t1, t1, ~uimm);
1339 tcg_gen_xori_tl(t2, t0, uimm);
1340 tcg_gen_and_tl(t1, t1, t2);
1342 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
1344 /* operands of same sign, result different sign */
1345 generate_exception(ctx, EXCP_OVERFLOW);
1347 gen_store_gpr(t0, rt);
1354 tcg_gen_addi_tl(cpu_gpr[rt], cpu_gpr[rs], uimm);
1356 tcg_gen_movi_tl(cpu_gpr[rt], uimm);
1362 MIPS_DEBUG("%s %s, %s, " TARGET_FMT_lx, opn, regnames[rt], regnames[rs], uimm);
1365 /* Logic with immediate operand */
1366 static void gen_logic_imm (CPUState *env, uint32_t opc, int rt, int rs, int16_t imm)
1369 const char *opn = "imm logic";
1372 /* If no destination, treat it as a NOP. */
1376 uimm = (uint16_t)imm;
1379 if (likely(rs != 0))
1380 tcg_gen_andi_tl(cpu_gpr[rt], cpu_gpr[rs], uimm);
1382 tcg_gen_movi_tl(cpu_gpr[rt], 0);
1387 tcg_gen_ori_tl(cpu_gpr[rt], cpu_gpr[rs], uimm);
1389 tcg_gen_movi_tl(cpu_gpr[rt], uimm);
1393 if (likely(rs != 0))
1394 tcg_gen_xori_tl(cpu_gpr[rt], cpu_gpr[rs], uimm);
1396 tcg_gen_movi_tl(cpu_gpr[rt], uimm);
1400 tcg_gen_movi_tl(cpu_gpr[rt], imm << 16);
1404 MIPS_DEBUG("%s %s, %s, " TARGET_FMT_lx, opn, regnames[rt], regnames[rs], uimm);
1407 /* Set on less than with immediate operand */
1408 static void gen_slt_imm (CPUState *env, uint32_t opc, int rt, int rs, int16_t imm)
1410 target_ulong uimm = (target_long)imm; /* Sign extend to 32/64 bits */
1411 const char *opn = "imm arith";
1415 /* If no destination, treat it as a NOP. */
1419 t0 = tcg_temp_new();
1420 gen_load_gpr(t0, rs);
1423 gen_op_lti(cpu_gpr[rt], t0, uimm);
1427 gen_op_ltiu(cpu_gpr[rt], t0, uimm);
1431 MIPS_DEBUG("%s %s, %s, " TARGET_FMT_lx, opn, regnames[rt], regnames[rs], uimm);
1435 /* Shifts with immediate operand */
1436 static void gen_shift_imm(CPUState *env, DisasContext *ctx, uint32_t opc,
1437 int rt, int rs, int16_t imm)
1439 target_ulong uimm = ((uint16_t)imm) & 0x1f;
1440 const char *opn = "imm shift";
1444 /* If no destination, treat it as a NOP. */
1449 t0 = tcg_temp_new();
1450 gen_load_gpr(t0, rs);
1453 tcg_gen_shli_tl(t0, t0, uimm);
1454 tcg_gen_ext32s_tl(cpu_gpr[rt], t0);
1458 tcg_gen_ext32s_tl(t0, t0);
1459 tcg_gen_sari_tl(cpu_gpr[rt], t0, uimm);
1464 tcg_gen_ext32u_tl(t0, t0);
1465 tcg_gen_shri_tl(cpu_gpr[rt], t0, uimm);
1467 tcg_gen_ext32s_tl(cpu_gpr[rt], t0);
1473 TCGv_i32 t1 = tcg_temp_new_i32();
1475 tcg_gen_trunc_tl_i32(t1, t0);
1476 tcg_gen_rotri_i32(t1, t1, uimm);
1477 tcg_gen_ext_i32_tl(cpu_gpr[rt], t1);
1478 tcg_temp_free_i32(t1);
1482 #if defined(TARGET_MIPS64)
1484 tcg_gen_shli_tl(cpu_gpr[rt], t0, uimm);
1488 tcg_gen_sari_tl(cpu_gpr[rt], t0, uimm);
1492 tcg_gen_shri_tl(cpu_gpr[rt], t0, uimm);
1497 tcg_gen_rotri_tl(cpu_gpr[rt], t0, uimm);
1502 tcg_gen_shli_tl(cpu_gpr[rt], t0, uimm + 32);
1506 tcg_gen_sari_tl(cpu_gpr[rt], t0, uimm + 32);
1510 tcg_gen_shri_tl(cpu_gpr[rt], t0, uimm + 32);
1514 tcg_gen_rotri_tl(cpu_gpr[rt], t0, uimm + 32);
1519 MIPS_DEBUG("%s %s, %s, " TARGET_FMT_lx, opn, regnames[rt], regnames[rs], uimm);
1524 static void gen_arith (CPUState *env, DisasContext *ctx, uint32_t opc,
1525 int rd, int rs, int rt)
1527 const char *opn = "arith";
1529 if (rd == 0 && opc != OPC_ADD && opc != OPC_SUB
1530 && opc != OPC_DADD && opc != OPC_DSUB) {
1531 /* If no destination, treat it as a NOP.
1532 For add & sub, we must generate the overflow exception when needed. */
1540 TCGv t0 = tcg_temp_local_new();
1541 TCGv t1 = tcg_temp_new();
1542 TCGv t2 = tcg_temp_new();
1543 int l1 = gen_new_label();
1545 gen_load_gpr(t1, rs);
1546 gen_load_gpr(t2, rt);
1547 tcg_gen_add_tl(t0, t1, t2);
1548 tcg_gen_ext32s_tl(t0, t0);
1549 tcg_gen_xor_tl(t1, t1, t2);
1550 tcg_gen_not_tl(t1, t1);
1551 tcg_gen_xor_tl(t2, t0, t2);
1552 tcg_gen_and_tl(t1, t1, t2);
1554 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
1556 /* operands of same sign, result different sign */
1557 generate_exception(ctx, EXCP_OVERFLOW);
1559 gen_store_gpr(t0, rd);
1565 if (rs != 0 && rt != 0) {
1566 tcg_gen_add_tl(cpu_gpr[rd], cpu_gpr[rs], cpu_gpr[rt]);
1567 tcg_gen_ext32s_tl(cpu_gpr[rd], cpu_gpr[rd]);
1568 } else if (rs == 0 && rt != 0) {
1569 tcg_gen_mov_tl(cpu_gpr[rd], cpu_gpr[rt]);
1570 } else if (rs != 0 && rt == 0) {
1571 tcg_gen_mov_tl(cpu_gpr[rd], cpu_gpr[rs]);
1573 tcg_gen_movi_tl(cpu_gpr[rd], 0);
1579 TCGv t0 = tcg_temp_local_new();
1580 TCGv t1 = tcg_temp_new();
1581 TCGv t2 = tcg_temp_new();
1582 int l1 = gen_new_label();
1584 gen_load_gpr(t1, rs);
1585 gen_load_gpr(t2, rt);
1586 tcg_gen_sub_tl(t0, t1, t2);
1587 tcg_gen_ext32s_tl(t0, t0);
1588 tcg_gen_xor_tl(t2, t1, t2);
1589 tcg_gen_xor_tl(t1, t0, t1);
1590 tcg_gen_and_tl(t1, t1, t2);
1592 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
1594 /* operands of different sign, first operand and result different sign */
1595 generate_exception(ctx, EXCP_OVERFLOW);
1597 gen_store_gpr(t0, rd);
1603 if (rs != 0 && rt != 0) {
1604 tcg_gen_sub_tl(cpu_gpr[rd], cpu_gpr[rs], cpu_gpr[rt]);
1605 tcg_gen_ext32s_tl(cpu_gpr[rd], cpu_gpr[rd]);
1606 } else if (rs == 0 && rt != 0) {
1607 tcg_gen_neg_tl(cpu_gpr[rd], cpu_gpr[rt]);
1608 tcg_gen_ext32s_tl(cpu_gpr[rd], cpu_gpr[rd]);
1609 } else if (rs != 0 && rt == 0) {
1610 tcg_gen_mov_tl(cpu_gpr[rd], cpu_gpr[rs]);
1612 tcg_gen_movi_tl(cpu_gpr[rd], 0);
1616 #if defined(TARGET_MIPS64)
1619 TCGv t0 = tcg_temp_local_new();
1620 TCGv t1 = tcg_temp_new();
1621 TCGv t2 = tcg_temp_new();
1622 int l1 = gen_new_label();
1624 gen_load_gpr(t1, rs);
1625 gen_load_gpr(t2, rt);
1626 tcg_gen_add_tl(t0, t1, t2);
1627 tcg_gen_xor_tl(t1, t1, t2);
1628 tcg_gen_not_tl(t1, t1);
1629 tcg_gen_xor_tl(t2, t0, t2);
1630 tcg_gen_and_tl(t1, t1, t2);
1632 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
1634 /* operands of same sign, result different sign */
1635 generate_exception(ctx, EXCP_OVERFLOW);
1637 gen_store_gpr(t0, rd);
1643 if (rs != 0 && rt != 0) {
1644 tcg_gen_add_tl(cpu_gpr[rd], cpu_gpr[rs], cpu_gpr[rt]);
1645 } else if (rs == 0 && rt != 0) {
1646 tcg_gen_mov_tl(cpu_gpr[rd], cpu_gpr[rt]);
1647 } else if (rs != 0 && rt == 0) {
1648 tcg_gen_mov_tl(cpu_gpr[rd], cpu_gpr[rs]);
1650 tcg_gen_movi_tl(cpu_gpr[rd], 0);
1656 TCGv t0 = tcg_temp_local_new();
1657 TCGv t1 = tcg_temp_new();
1658 TCGv t2 = tcg_temp_new();
1659 int l1 = gen_new_label();
1661 gen_load_gpr(t1, rs);
1662 gen_load_gpr(t2, rt);
1663 tcg_gen_sub_tl(t0, t1, t2);
1664 tcg_gen_xor_tl(t2, t1, t2);
1665 tcg_gen_xor_tl(t1, t0, t1);
1666 tcg_gen_and_tl(t1, t1, t2);
1668 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
1670 /* operands of different sign, first operand and result different sign */
1671 generate_exception(ctx, EXCP_OVERFLOW);
1673 gen_store_gpr(t0, rd);
1679 if (rs != 0 && rt != 0) {
1680 tcg_gen_sub_tl(cpu_gpr[rd], cpu_gpr[rs], cpu_gpr[rt]);
1681 } else if (rs == 0 && rt != 0) {
1682 tcg_gen_neg_tl(cpu_gpr[rd], cpu_gpr[rt]);
1683 } else if (rs != 0 && rt == 0) {
1684 tcg_gen_mov_tl(cpu_gpr[rd], cpu_gpr[rs]);
1686 tcg_gen_movi_tl(cpu_gpr[rd], 0);
1692 if (likely(rs != 0 && rt != 0)) {
1693 tcg_gen_mul_tl(cpu_gpr[rd], cpu_gpr[rs], cpu_gpr[rt]);
1694 tcg_gen_ext32s_tl(cpu_gpr[rd], cpu_gpr[rd]);
1696 tcg_gen_movi_tl(cpu_gpr[rd], 0);
1701 MIPS_DEBUG("%s %s, %s, %s", opn, regnames[rd], regnames[rs], regnames[rt]);
1704 /* Conditional move */
1705 static void gen_cond_move (CPUState *env, uint32_t opc, int rd, int rs, int rt)
1707 const char *opn = "cond move";
1711 /* If no destination, treat it as a NOP.
1712 For add & sub, we must generate the overflow exception when needed. */
1717 l1 = gen_new_label();
1720 if (likely(rt != 0))
1721 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rt], 0, l1);
1727 if (likely(rt != 0))
1728 tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[rt], 0, l1);
1733 tcg_gen_mov_tl(cpu_gpr[rd], cpu_gpr[rs]);
1735 tcg_gen_movi_tl(cpu_gpr[rd], 0);
1738 MIPS_DEBUG("%s %s, %s, %s", opn, regnames[rd], regnames[rs], regnames[rt]);
1742 static void gen_logic (CPUState *env, uint32_t opc, int rd, int rs, int rt)
1744 const char *opn = "logic";
1747 /* If no destination, treat it as a NOP. */
1754 if (likely(rs != 0 && rt != 0)) {
1755 tcg_gen_and_tl(cpu_gpr[rd], cpu_gpr[rs], cpu_gpr[rt]);
1757 tcg_gen_movi_tl(cpu_gpr[rd], 0);
1762 if (rs != 0 && rt != 0) {
1763 tcg_gen_nor_tl(cpu_gpr[rd], cpu_gpr[rs], cpu_gpr[rt]);
1764 } else if (rs == 0 && rt != 0) {
1765 tcg_gen_not_tl(cpu_gpr[rd], cpu_gpr[rt]);
1766 } else if (rs != 0 && rt == 0) {
1767 tcg_gen_not_tl(cpu_gpr[rd], cpu_gpr[rs]);
1769 tcg_gen_movi_tl(cpu_gpr[rd], ~((target_ulong)0));
1774 if (likely(rs != 0 && rt != 0)) {
1775 tcg_gen_or_tl(cpu_gpr[rd], cpu_gpr[rs], cpu_gpr[rt]);
1776 } else if (rs == 0 && rt != 0) {
1777 tcg_gen_mov_tl(cpu_gpr[rd], cpu_gpr[rt]);
1778 } else if (rs != 0 && rt == 0) {
1779 tcg_gen_mov_tl(cpu_gpr[rd], cpu_gpr[rs]);
1781 tcg_gen_movi_tl(cpu_gpr[rd], 0);
1786 if (likely(rs != 0 && rt != 0)) {
1787 tcg_gen_xor_tl(cpu_gpr[rd], cpu_gpr[rs], cpu_gpr[rt]);
1788 } else if (rs == 0 && rt != 0) {
1789 tcg_gen_mov_tl(cpu_gpr[rd], cpu_gpr[rt]);
1790 } else if (rs != 0 && rt == 0) {
1791 tcg_gen_mov_tl(cpu_gpr[rd], cpu_gpr[rs]);
1793 tcg_gen_movi_tl(cpu_gpr[rd], 0);
1798 MIPS_DEBUG("%s %s, %s, %s", opn, regnames[rd], regnames[rs], regnames[rt]);
1801 /* Set on lower than */
1802 static void gen_slt (CPUState *env, uint32_t opc, int rd, int rs, int rt)
1804 const char *opn = "slt";
1808 /* If no destination, treat it as a NOP. */
1813 t0 = tcg_temp_new();
1814 t1 = tcg_temp_new();
1815 gen_load_gpr(t0, rs);
1816 gen_load_gpr(t1, rt);
1819 gen_op_lt(cpu_gpr[rd], t0, t1);
1823 gen_op_ltu(cpu_gpr[rd], t0, t1);
1827 MIPS_DEBUG("%s %s, %s, %s", opn, regnames[rd], regnames[rs], regnames[rt]);
1833 static void gen_shift (CPUState *env, DisasContext *ctx, uint32_t opc,
1834 int rd, int rs, int rt)
1836 const char *opn = "shifts";
1840 /* If no destination, treat it as a NOP.
1841 For add & sub, we must generate the overflow exception when needed. */
1846 t0 = tcg_temp_new();
1847 t1 = tcg_temp_new();
1848 gen_load_gpr(t0, rs);
1849 gen_load_gpr(t1, rt);
1852 tcg_gen_andi_tl(t0, t0, 0x1f);
1853 tcg_gen_shl_tl(t0, t1, t0);
1854 tcg_gen_ext32s_tl(cpu_gpr[rd], t0);
1858 tcg_gen_ext32s_tl(t1, t1);
1859 tcg_gen_andi_tl(t0, t0, 0x1f);
1860 tcg_gen_sar_tl(cpu_gpr[rd], t1, t0);
1864 tcg_gen_ext32u_tl(t1, t1);
1865 tcg_gen_andi_tl(t0, t0, 0x1f);
1866 tcg_gen_shr_tl(t0, t1, t0);
1867 tcg_gen_ext32s_tl(cpu_gpr[rd], t0);
1872 TCGv_i32 t2 = tcg_temp_new_i32();
1873 TCGv_i32 t3 = tcg_temp_new_i32();
1875 tcg_gen_trunc_tl_i32(t2, t0);
1876 tcg_gen_trunc_tl_i32(t3, t1);
1877 tcg_gen_andi_i32(t2, t2, 0x1f);
1878 tcg_gen_rotr_i32(t2, t3, t2);
1879 tcg_gen_ext_i32_tl(cpu_gpr[rd], t2);
1880 tcg_temp_free_i32(t2);
1881 tcg_temp_free_i32(t3);
1885 #if defined(TARGET_MIPS64)
1887 tcg_gen_andi_tl(t0, t0, 0x3f);
1888 tcg_gen_shl_tl(cpu_gpr[rd], t1, t0);
1892 tcg_gen_andi_tl(t0, t0, 0x3f);
1893 tcg_gen_sar_tl(cpu_gpr[rd], t1, t0);
1897 tcg_gen_andi_tl(t0, t0, 0x3f);
1898 tcg_gen_shr_tl(cpu_gpr[rd], t1, t0);
1902 tcg_gen_andi_tl(t0, t0, 0x3f);
1903 tcg_gen_rotr_tl(cpu_gpr[rd], t1, t0);
1908 MIPS_DEBUG("%s %s, %s, %s", opn, regnames[rd], regnames[rs], regnames[rt]);
1913 /* Arithmetic on HI/LO registers */
1914 static void gen_HILO (DisasContext *ctx, uint32_t opc, int reg)
1916 const char *opn = "hilo";
1918 if (reg == 0 && (opc == OPC_MFHI || opc == OPC_MFLO)) {
1925 tcg_gen_mov_tl(cpu_gpr[reg], cpu_HI[0]);
1929 tcg_gen_mov_tl(cpu_gpr[reg], cpu_LO[0]);
1934 tcg_gen_mov_tl(cpu_HI[0], cpu_gpr[reg]);
1936 tcg_gen_movi_tl(cpu_HI[0], 0);
1941 tcg_gen_mov_tl(cpu_LO[0], cpu_gpr[reg]);
1943 tcg_gen_movi_tl(cpu_LO[0], 0);
1947 MIPS_DEBUG("%s %s", opn, regnames[reg]);
1950 static void gen_muldiv (DisasContext *ctx, uint32_t opc,
1953 const char *opn = "mul/div";
1959 #if defined(TARGET_MIPS64)
1963 t0 = tcg_temp_local_new();
1964 t1 = tcg_temp_local_new();
1967 t0 = tcg_temp_new();
1968 t1 = tcg_temp_new();
1972 gen_load_gpr(t0, rs);
1973 gen_load_gpr(t1, rt);
1977 int l1 = gen_new_label();
1978 int l2 = gen_new_label();
1980 tcg_gen_ext32s_tl(t0, t0);
1981 tcg_gen_ext32s_tl(t1, t1);
1982 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
1983 tcg_gen_brcondi_tl(TCG_COND_NE, t0, INT_MIN, l2);
1984 tcg_gen_brcondi_tl(TCG_COND_NE, t1, -1, l2);
1986 tcg_gen_mov_tl(cpu_LO[0], t0);
1987 tcg_gen_movi_tl(cpu_HI[0], 0);
1990 tcg_gen_div_tl(cpu_LO[0], t0, t1);
1991 tcg_gen_rem_tl(cpu_HI[0], t0, t1);
1992 tcg_gen_ext32s_tl(cpu_LO[0], cpu_LO[0]);
1993 tcg_gen_ext32s_tl(cpu_HI[0], cpu_HI[0]);
2000 int l1 = gen_new_label();
2002 tcg_gen_ext32u_tl(t0, t0);
2003 tcg_gen_ext32u_tl(t1, t1);
2004 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
2005 tcg_gen_divu_tl(cpu_LO[0], t0, t1);
2006 tcg_gen_remu_tl(cpu_HI[0], t0, t1);
2007 tcg_gen_ext32s_tl(cpu_LO[0], cpu_LO[0]);
2008 tcg_gen_ext32s_tl(cpu_HI[0], cpu_HI[0]);
2015 TCGv_i64 t2 = tcg_temp_new_i64();
2016 TCGv_i64 t3 = tcg_temp_new_i64();
2018 tcg_gen_ext_tl_i64(t2, t0);
2019 tcg_gen_ext_tl_i64(t3, t1);
2020 tcg_gen_mul_i64(t2, t2, t3);
2021 tcg_temp_free_i64(t3);
2022 tcg_gen_trunc_i64_tl(t0, t2);
2023 tcg_gen_shri_i64(t2, t2, 32);
2024 tcg_gen_trunc_i64_tl(t1, t2);
2025 tcg_temp_free_i64(t2);
2026 tcg_gen_ext32s_tl(cpu_LO[0], t0);
2027 tcg_gen_ext32s_tl(cpu_HI[0], t1);
2033 TCGv_i64 t2 = tcg_temp_new_i64();
2034 TCGv_i64 t3 = tcg_temp_new_i64();
2036 tcg_gen_ext32u_tl(t0, t0);
2037 tcg_gen_ext32u_tl(t1, t1);
2038 tcg_gen_extu_tl_i64(t2, t0);
2039 tcg_gen_extu_tl_i64(t3, t1);
2040 tcg_gen_mul_i64(t2, t2, t3);
2041 tcg_temp_free_i64(t3);
2042 tcg_gen_trunc_i64_tl(t0, t2);
2043 tcg_gen_shri_i64(t2, t2, 32);
2044 tcg_gen_trunc_i64_tl(t1, t2);
2045 tcg_temp_free_i64(t2);
2046 tcg_gen_ext32s_tl(cpu_LO[0], t0);
2047 tcg_gen_ext32s_tl(cpu_HI[0], t1);
2051 #if defined(TARGET_MIPS64)
2054 int l1 = gen_new_label();
2055 int l2 = gen_new_label();
2057 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
2058 tcg_gen_brcondi_tl(TCG_COND_NE, t0, -1LL << 63, l2);
2059 tcg_gen_brcondi_tl(TCG_COND_NE, t1, -1LL, l2);
2060 tcg_gen_mov_tl(cpu_LO[0], t0);
2061 tcg_gen_movi_tl(cpu_HI[0], 0);
2064 tcg_gen_div_i64(cpu_LO[0], t0, t1);
2065 tcg_gen_rem_i64(cpu_HI[0], t0, t1);
2072 int l1 = gen_new_label();
2074 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
2075 tcg_gen_divu_i64(cpu_LO[0], t0, t1);
2076 tcg_gen_remu_i64(cpu_HI[0], t0, t1);
2082 gen_helper_dmult(t0, t1);
2086 gen_helper_dmultu(t0, t1);
2092 TCGv_i64 t2 = tcg_temp_new_i64();
2093 TCGv_i64 t3 = tcg_temp_new_i64();
2095 tcg_gen_ext_tl_i64(t2, t0);
2096 tcg_gen_ext_tl_i64(t3, t1);
2097 tcg_gen_mul_i64(t2, t2, t3);
2098 tcg_gen_concat_tl_i64(t3, cpu_LO[0], cpu_HI[0]);
2099 tcg_gen_add_i64(t2, t2, t3);
2100 tcg_temp_free_i64(t3);
2101 tcg_gen_trunc_i64_tl(t0, t2);
2102 tcg_gen_shri_i64(t2, t2, 32);
2103 tcg_gen_trunc_i64_tl(t1, t2);
2104 tcg_temp_free_i64(t2);
2105 tcg_gen_ext32s_tl(cpu_LO[0], t0);
2106 tcg_gen_ext32s_tl(cpu_HI[0], t1);
2112 TCGv_i64 t2 = tcg_temp_new_i64();
2113 TCGv_i64 t3 = tcg_temp_new_i64();
2115 tcg_gen_ext32u_tl(t0, t0);
2116 tcg_gen_ext32u_tl(t1, t1);
2117 tcg_gen_extu_tl_i64(t2, t0);
2118 tcg_gen_extu_tl_i64(t3, t1);
2119 tcg_gen_mul_i64(t2, t2, t3);
2120 tcg_gen_concat_tl_i64(t3, cpu_LO[0], cpu_HI[0]);
2121 tcg_gen_add_i64(t2, t2, t3);
2122 tcg_temp_free_i64(t3);
2123 tcg_gen_trunc_i64_tl(t0, t2);
2124 tcg_gen_shri_i64(t2, t2, 32);
2125 tcg_gen_trunc_i64_tl(t1, t2);
2126 tcg_temp_free_i64(t2);
2127 tcg_gen_ext32s_tl(cpu_LO[0], t0);
2128 tcg_gen_ext32s_tl(cpu_HI[0], t1);
2134 TCGv_i64 t2 = tcg_temp_new_i64();
2135 TCGv_i64 t3 = tcg_temp_new_i64();
2137 tcg_gen_ext_tl_i64(t2, t0);
2138 tcg_gen_ext_tl_i64(t3, t1);
2139 tcg_gen_mul_i64(t2, t2, t3);
2140 tcg_gen_concat_tl_i64(t3, cpu_LO[0], cpu_HI[0]);
2141 tcg_gen_sub_i64(t2, t3, t2);
2142 tcg_temp_free_i64(t3);
2143 tcg_gen_trunc_i64_tl(t0, t2);
2144 tcg_gen_shri_i64(t2, t2, 32);
2145 tcg_gen_trunc_i64_tl(t1, t2);
2146 tcg_temp_free_i64(t2);
2147 tcg_gen_ext32s_tl(cpu_LO[0], t0);
2148 tcg_gen_ext32s_tl(cpu_HI[0], t1);
2154 TCGv_i64 t2 = tcg_temp_new_i64();
2155 TCGv_i64 t3 = tcg_temp_new_i64();
2157 tcg_gen_ext32u_tl(t0, t0);
2158 tcg_gen_ext32u_tl(t1, t1);
2159 tcg_gen_extu_tl_i64(t2, t0);
2160 tcg_gen_extu_tl_i64(t3, t1);
2161 tcg_gen_mul_i64(t2, t2, t3);
2162 tcg_gen_concat_tl_i64(t3, cpu_LO[0], cpu_HI[0]);
2163 tcg_gen_sub_i64(t2, t3, t2);
2164 tcg_temp_free_i64(t3);
2165 tcg_gen_trunc_i64_tl(t0, t2);
2166 tcg_gen_shri_i64(t2, t2, 32);
2167 tcg_gen_trunc_i64_tl(t1, t2);
2168 tcg_temp_free_i64(t2);
2169 tcg_gen_ext32s_tl(cpu_LO[0], t0);
2170 tcg_gen_ext32s_tl(cpu_HI[0], t1);
2176 generate_exception(ctx, EXCP_RI);
2179 MIPS_DEBUG("%s %s %s", opn, regnames[rs], regnames[rt]);
2185 static void gen_mul_vr54xx (DisasContext *ctx, uint32_t opc,
2186 int rd, int rs, int rt)
2188 const char *opn = "mul vr54xx";
2189 TCGv t0 = tcg_temp_new();
2190 TCGv t1 = tcg_temp_new();
2192 gen_load_gpr(t0, rs);
2193 gen_load_gpr(t1, rt);
2196 case OPC_VR54XX_MULS:
2197 gen_helper_muls(t0, t0, t1);
2200 case OPC_VR54XX_MULSU:
2201 gen_helper_mulsu(t0, t0, t1);
2204 case OPC_VR54XX_MACC:
2205 gen_helper_macc(t0, t0, t1);
2208 case OPC_VR54XX_MACCU:
2209 gen_helper_maccu(t0, t0, t1);
2212 case OPC_VR54XX_MSAC:
2213 gen_helper_msac(t0, t0, t1);
2216 case OPC_VR54XX_MSACU:
2217 gen_helper_msacu(t0, t0, t1);
2220 case OPC_VR54XX_MULHI:
2221 gen_helper_mulhi(t0, t0, t1);
2224 case OPC_VR54XX_MULHIU:
2225 gen_helper_mulhiu(t0, t0, t1);
2228 case OPC_VR54XX_MULSHI:
2229 gen_helper_mulshi(t0, t0, t1);
2232 case OPC_VR54XX_MULSHIU:
2233 gen_helper_mulshiu(t0, t0, t1);
2236 case OPC_VR54XX_MACCHI:
2237 gen_helper_macchi(t0, t0, t1);
2240 case OPC_VR54XX_MACCHIU:
2241 gen_helper_macchiu(t0, t0, t1);
2244 case OPC_VR54XX_MSACHI:
2245 gen_helper_msachi(t0, t0, t1);
2248 case OPC_VR54XX_MSACHIU:
2249 gen_helper_msachiu(t0, t0, t1);
2253 MIPS_INVAL("mul vr54xx");
2254 generate_exception(ctx, EXCP_RI);
2257 gen_store_gpr(t0, rd);
2258 MIPS_DEBUG("%s %s, %s, %s", opn, regnames[rd], regnames[rs], regnames[rt]);
2265 static void gen_cl (DisasContext *ctx, uint32_t opc,
2268 const char *opn = "CLx";
2276 t0 = tcg_temp_new();
2277 gen_load_gpr(t0, rs);
2280 gen_helper_clo(cpu_gpr[rd], t0);
2284 gen_helper_clz(cpu_gpr[rd], t0);
2287 #if defined(TARGET_MIPS64)
2289 gen_helper_dclo(cpu_gpr[rd], t0);
2293 gen_helper_dclz(cpu_gpr[rd], t0);
2298 MIPS_DEBUG("%s %s, %s", opn, regnames[rd], regnames[rs]);
2303 static void gen_trap (DisasContext *ctx, uint32_t opc,
2304 int rs, int rt, int16_t imm)
2307 TCGv t0 = tcg_temp_new();
2308 TCGv t1 = tcg_temp_new();
2311 /* Load needed operands */
2319 /* Compare two registers */
2321 gen_load_gpr(t0, rs);
2322 gen_load_gpr(t1, rt);
2332 /* Compare register to immediate */
2333 if (rs != 0 || imm != 0) {
2334 gen_load_gpr(t0, rs);
2335 tcg_gen_movi_tl(t1, (int32_t)imm);
2342 case OPC_TEQ: /* rs == rs */
2343 case OPC_TEQI: /* r0 == 0 */
2344 case OPC_TGE: /* rs >= rs */
2345 case OPC_TGEI: /* r0 >= 0 */
2346 case OPC_TGEU: /* rs >= rs unsigned */
2347 case OPC_TGEIU: /* r0 >= 0 unsigned */
2349 generate_exception(ctx, EXCP_TRAP);
2351 case OPC_TLT: /* rs < rs */
2352 case OPC_TLTI: /* r0 < 0 */
2353 case OPC_TLTU: /* rs < rs unsigned */
2354 case OPC_TLTIU: /* r0 < 0 unsigned */
2355 case OPC_TNE: /* rs != rs */
2356 case OPC_TNEI: /* r0 != 0 */
2357 /* Never trap: treat as NOP. */
2361 int l1 = gen_new_label();
2366 tcg_gen_brcond_tl(TCG_COND_NE, t0, t1, l1);
2370 tcg_gen_brcond_tl(TCG_COND_LT, t0, t1, l1);
2374 tcg_gen_brcond_tl(TCG_COND_LTU, t0, t1, l1);
2378 tcg_gen_brcond_tl(TCG_COND_GE, t0, t1, l1);
2382 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
2386 tcg_gen_brcond_tl(TCG_COND_EQ, t0, t1, l1);
2389 generate_exception(ctx, EXCP_TRAP);
2396 static inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
2398 TranslationBlock *tb;
2400 if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
2401 likely(!ctx->singlestep_enabled)) {
2404 tcg_gen_exit_tb((long)tb + n);
2407 if (ctx->singlestep_enabled) {
2408 save_cpu_state(ctx, 0);
2409 gen_helper_0i(raise_exception, EXCP_DEBUG);
2415 /* Branches (before delay slot) */
2416 static void gen_compute_branch (DisasContext *ctx, uint32_t opc,
2418 int rs, int rt, int32_t offset)
2420 target_ulong btgt = -1;
2422 int bcond_compute = 0;
2423 TCGv t0 = tcg_temp_new();
2424 TCGv t1 = tcg_temp_new();
2426 if (ctx->hflags & MIPS_HFLAG_BMASK) {
2427 #ifdef MIPS_DEBUG_DISAS
2428 LOG_DISAS("Branch in delay slot at PC 0x" TARGET_FMT_lx "\n", ctx->pc);
2430 generate_exception(ctx, EXCP_RI);
2434 /* Load needed operands */
2440 /* Compare two registers */
2442 gen_load_gpr(t0, rs);
2443 gen_load_gpr(t1, rt);
2446 btgt = ctx->pc + insn_bytes + offset;
2460 /* Compare to zero */
2462 gen_load_gpr(t0, rs);
2465 btgt = ctx->pc + insn_bytes + offset;
2470 /* Jump to immediate */
2471 btgt = ((ctx->pc + insn_bytes) & (int32_t)0xF0000000) | (uint32_t)offset;
2476 /* Jump to register */
2477 if (offset != 0 && offset != 16) {
2478 /* Hint = 0 is JR/JALR, hint 16 is JR.HB/JALR.HB, the
2479 others are reserved. */
2480 MIPS_INVAL("jump hint");
2481 generate_exception(ctx, EXCP_RI);
2484 gen_load_gpr(btarget, rs);
2487 MIPS_INVAL("branch/jump");
2488 generate_exception(ctx, EXCP_RI);
2491 if (bcond_compute == 0) {
2492 /* No condition to be computed */
2494 case OPC_BEQ: /* rx == rx */
2495 case OPC_BEQL: /* rx == rx likely */
2496 case OPC_BGEZ: /* 0 >= 0 */
2497 case OPC_BGEZL: /* 0 >= 0 likely */
2498 case OPC_BLEZ: /* 0 <= 0 */
2499 case OPC_BLEZL: /* 0 <= 0 likely */
2501 ctx->hflags |= MIPS_HFLAG_B;
2502 MIPS_DEBUG("balways");
2504 case OPC_BGEZAL: /* 0 >= 0 */
2505 case OPC_BGEZALL: /* 0 >= 0 likely */
2506 /* Always take and link */
2508 ctx->hflags |= MIPS_HFLAG_B;
2509 MIPS_DEBUG("balways and link");
2511 case OPC_BNE: /* rx != rx */
2512 case OPC_BGTZ: /* 0 > 0 */
2513 case OPC_BLTZ: /* 0 < 0 */
2515 MIPS_DEBUG("bnever (NOP)");
2517 case OPC_BLTZAL: /* 0 < 0 */
2518 tcg_gen_movi_tl(cpu_gpr[31], ctx->pc + 8);
2519 MIPS_DEBUG("bnever and link");
2521 case OPC_BLTZALL: /* 0 < 0 likely */
2522 tcg_gen_movi_tl(cpu_gpr[31], ctx->pc + 8);
2523 /* Skip the instruction in the delay slot */
2524 MIPS_DEBUG("bnever, link and skip");
2527 case OPC_BNEL: /* rx != rx likely */
2528 case OPC_BGTZL: /* 0 > 0 likely */
2529 case OPC_BLTZL: /* 0 < 0 likely */
2530 /* Skip the instruction in the delay slot */
2531 MIPS_DEBUG("bnever and skip");
2535 ctx->hflags |= MIPS_HFLAG_B;
2536 MIPS_DEBUG("j " TARGET_FMT_lx, btgt);
2539 ctx->hflags |= MIPS_HFLAG_BX;
2543 ctx->hflags |= MIPS_HFLAG_B;
2544 ctx->hflags |= (ctx->hflags & MIPS_HFLAG_M16
2546 : MIPS_HFLAG_BDS32);
2547 MIPS_DEBUG("jal " TARGET_FMT_lx, btgt);
2550 ctx->hflags |= MIPS_HFLAG_BR;
2551 if (ctx->hflags & MIPS_HFLAG_M16)
2552 ctx->hflags |= MIPS_HFLAG_BDS16;
2553 MIPS_DEBUG("jr %s", regnames[rs]);
2558 ctx->hflags |= MIPS_HFLAG_BR;
2559 if (ctx->hflags & MIPS_HFLAG_M16)
2560 ctx->hflags |= MIPS_HFLAG_BDS16;
2561 MIPS_DEBUG("jalr %s, %s", regnames[rt], regnames[rs]);
2564 MIPS_INVAL("branch/jump");
2565 generate_exception(ctx, EXCP_RI);
2571 gen_op_eq(bcond, t0, t1);
2572 MIPS_DEBUG("beq %s, %s, " TARGET_FMT_lx,
2573 regnames[rs], regnames[rt], btgt);
2576 gen_op_eq(bcond, t0, t1);
2577 MIPS_DEBUG("beql %s, %s, " TARGET_FMT_lx,
2578 regnames[rs], regnames[rt], btgt);
2581 gen_op_ne(bcond, t0, t1);
2582 MIPS_DEBUG("bne %s, %s, " TARGET_FMT_lx,
2583 regnames[rs], regnames[rt], btgt);
2586 gen_op_ne(bcond, t0, t1);
2587 MIPS_DEBUG("bnel %s, %s, " TARGET_FMT_lx,
2588 regnames[rs], regnames[rt], btgt);
2591 gen_op_gez(bcond, t0);
2592 MIPS_DEBUG("bgez %s, " TARGET_FMT_lx, regnames[rs], btgt);
2595 gen_op_gez(bcond, t0);
2596 MIPS_DEBUG("bgezl %s, " TARGET_FMT_lx, regnames[rs], btgt);
2599 gen_op_gez(bcond, t0);
2600 MIPS_DEBUG("bgezal %s, " TARGET_FMT_lx, regnames[rs], btgt);
2604 gen_op_gez(bcond, t0);
2606 MIPS_DEBUG("bgezall %s, " TARGET_FMT_lx, regnames[rs], btgt);
2609 gen_op_gtz(bcond, t0);
2610 MIPS_DEBUG("bgtz %s, " TARGET_FMT_lx, regnames[rs], btgt);
2613 gen_op_gtz(bcond, t0);
2614 MIPS_DEBUG("bgtzl %s, " TARGET_FMT_lx, regnames[rs], btgt);
2617 gen_op_lez(bcond, t0);
2618 MIPS_DEBUG("blez %s, " TARGET_FMT_lx, regnames[rs], btgt);
2621 gen_op_lez(bcond, t0);
2622 MIPS_DEBUG("blezl %s, " TARGET_FMT_lx, regnames[rs], btgt);
2625 gen_op_ltz(bcond, t0);
2626 MIPS_DEBUG("bltz %s, " TARGET_FMT_lx, regnames[rs], btgt);
2629 gen_op_ltz(bcond, t0);
2630 MIPS_DEBUG("bltzl %s, " TARGET_FMT_lx, regnames[rs], btgt);
2633 gen_op_ltz(bcond, t0);
2635 MIPS_DEBUG("bltzal %s, " TARGET_FMT_lx, regnames[rs], btgt);
2637 ctx->hflags |= MIPS_HFLAG_BC;
2640 gen_op_ltz(bcond, t0);
2642 MIPS_DEBUG("bltzall %s, " TARGET_FMT_lx, regnames[rs], btgt);
2644 ctx->hflags |= MIPS_HFLAG_BL;
2647 MIPS_INVAL("conditional branch/jump");
2648 generate_exception(ctx, EXCP_RI);
2652 MIPS_DEBUG("enter ds: link %d cond %02x target " TARGET_FMT_lx,
2653 blink, ctx->hflags, btgt);
2655 ctx->btarget = btgt;
2657 int post_delay = insn_bytes;
2658 int lowbit = !!(ctx->hflags & MIPS_HFLAG_M16);
2660 if (opc != OPC_JALRC)
2661 post_delay += ((ctx->hflags & MIPS_HFLAG_BDS16) ? 2 : 4);
2663 tcg_gen_movi_tl(cpu_gpr[blink], ctx->pc + post_delay + lowbit);
2667 if (insn_bytes == 2)
2668 ctx->hflags |= MIPS_HFLAG_B16;
2673 /* special3 bitfield operations */
2674 static void gen_bitops (DisasContext *ctx, uint32_t opc, int rt,
2675 int rs, int lsb, int msb)
2677 TCGv t0 = tcg_temp_new();
2678 TCGv t1 = tcg_temp_new();
2681 gen_load_gpr(t1, rs);
2686 tcg_gen_shri_tl(t0, t1, lsb);
2688 tcg_gen_andi_tl(t0, t0, (1 << (msb + 1)) - 1);
2690 tcg_gen_ext32s_tl(t0, t0);
2693 #if defined(TARGET_MIPS64)
2695 tcg_gen_shri_tl(t0, t1, lsb);
2697 tcg_gen_andi_tl(t0, t0, (1ULL << (msb + 1 + 32)) - 1);
2701 tcg_gen_shri_tl(t0, t1, lsb + 32);
2702 tcg_gen_andi_tl(t0, t0, (1ULL << (msb + 1)) - 1);
2705 tcg_gen_shri_tl(t0, t1, lsb);
2706 tcg_gen_andi_tl(t0, t0, (1ULL << (msb + 1)) - 1);
2712 mask = ((msb - lsb + 1 < 32) ? ((1 << (msb - lsb + 1)) - 1) : ~0) << lsb;
2713 gen_load_gpr(t0, rt);
2714 tcg_gen_andi_tl(t0, t0, ~mask);
2715 tcg_gen_shli_tl(t1, t1, lsb);
2716 tcg_gen_andi_tl(t1, t1, mask);
2717 tcg_gen_or_tl(t0, t0, t1);
2718 tcg_gen_ext32s_tl(t0, t0);
2720 #if defined(TARGET_MIPS64)
2724 mask = ((msb - lsb + 1 + 32 < 64) ? ((1ULL << (msb - lsb + 1 + 32)) - 1) : ~0ULL) << lsb;
2725 gen_load_gpr(t0, rt);
2726 tcg_gen_andi_tl(t0, t0, ~mask);
2727 tcg_gen_shli_tl(t1, t1, lsb);
2728 tcg_gen_andi_tl(t1, t1, mask);
2729 tcg_gen_or_tl(t0, t0, t1);
2734 mask = ((1ULL << (msb - lsb + 1)) - 1) << lsb;
2735 gen_load_gpr(t0, rt);
2736 tcg_gen_andi_tl(t0, t0, ~mask);
2737 tcg_gen_shli_tl(t1, t1, lsb + 32);
2738 tcg_gen_andi_tl(t1, t1, mask);
2739 tcg_gen_or_tl(t0, t0, t1);
2744 gen_load_gpr(t0, rt);
2745 mask = ((1ULL << (msb - lsb + 1)) - 1) << lsb;
2746 gen_load_gpr(t0, rt);
2747 tcg_gen_andi_tl(t0, t0, ~mask);
2748 tcg_gen_shli_tl(t1, t1, lsb);
2749 tcg_gen_andi_tl(t1, t1, mask);
2750 tcg_gen_or_tl(t0, t0, t1);
2755 MIPS_INVAL("bitops");
2756 generate_exception(ctx, EXCP_RI);
2761 gen_store_gpr(t0, rt);
2766 static void gen_bshfl (DisasContext *ctx, uint32_t op2, int rt, int rd)
2771 /* If no destination, treat it as a NOP. */
2776 t0 = tcg_temp_new();
2777 gen_load_gpr(t0, rt);
2781 TCGv t1 = tcg_temp_new();
2783 tcg_gen_shri_tl(t1, t0, 8);
2784 tcg_gen_andi_tl(t1, t1, 0x00FF00FF);
2785 tcg_gen_shli_tl(t0, t0, 8);
2786 tcg_gen_andi_tl(t0, t0, ~0x00FF00FF);
2787 tcg_gen_or_tl(t0, t0, t1);
2789 tcg_gen_ext32s_tl(cpu_gpr[rd], t0);
2793 tcg_gen_ext8s_tl(cpu_gpr[rd], t0);
2796 tcg_gen_ext16s_tl(cpu_gpr[rd], t0);
2798 #if defined(TARGET_MIPS64)
2801 TCGv t1 = tcg_temp_new();
2803 tcg_gen_shri_tl(t1, t0, 8);
2804 tcg_gen_andi_tl(t1, t1, 0x00FF00FF00FF00FFULL);
2805 tcg_gen_shli_tl(t0, t0, 8);
2806 tcg_gen_andi_tl(t0, t0, ~0x00FF00FF00FF00FFULL);
2807 tcg_gen_or_tl(cpu_gpr[rd], t0, t1);
2813 TCGv t1 = tcg_temp_new();
2815 tcg_gen_shri_tl(t1, t0, 16);
2816 tcg_gen_andi_tl(t1, t1, 0x0000FFFF0000FFFFULL);
2817 tcg_gen_shli_tl(t0, t0, 16);
2818 tcg_gen_andi_tl(t0, t0, ~0x0000FFFF0000FFFFULL);
2819 tcg_gen_or_tl(t0, t0, t1);
2820 tcg_gen_shri_tl(t1, t0, 32);
2821 tcg_gen_shli_tl(t0, t0, 32);
2822 tcg_gen_or_tl(cpu_gpr[rd], t0, t1);
2828 MIPS_INVAL("bsfhl");
2829 generate_exception(ctx, EXCP_RI);
2836 #ifndef CONFIG_USER_ONLY
2837 /* CP0 (MMU and control) */
2838 static inline void gen_mfc0_load32 (TCGv arg, target_ulong off)
2840 TCGv_i32 t0 = tcg_temp_new_i32();
2842 tcg_gen_ld_i32(t0, cpu_env, off);
2843 tcg_gen_ext_i32_tl(arg, t0);
2844 tcg_temp_free_i32(t0);
2847 static inline void gen_mfc0_load64 (TCGv arg, target_ulong off)
2849 tcg_gen_ld_tl(arg, cpu_env, off);
2850 tcg_gen_ext32s_tl(arg, arg);
2853 static inline void gen_mtc0_store32 (TCGv arg, target_ulong off)
2855 TCGv_i32 t0 = tcg_temp_new_i32();
2857 tcg_gen_trunc_tl_i32(t0, arg);
2858 tcg_gen_st_i32(t0, cpu_env, off);
2859 tcg_temp_free_i32(t0);
2862 static inline void gen_mtc0_store64 (TCGv arg, target_ulong off)
2864 tcg_gen_ext32s_tl(arg, arg);
2865 tcg_gen_st_tl(arg, cpu_env, off);
2868 static void gen_mfc0 (CPUState *env, DisasContext *ctx, TCGv arg, int reg, int sel)
2870 const char *rn = "invalid";
2873 check_insn(env, ctx, ISA_MIPS32);
2879 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Index));
2883 check_insn(env, ctx, ASE_MT);
2884 gen_helper_mfc0_mvpcontrol(arg);
2888 check_insn(env, ctx, ASE_MT);
2889 gen_helper_mfc0_mvpconf0(arg);
2893 check_insn(env, ctx, ASE_MT);
2894 gen_helper_mfc0_mvpconf1(arg);
2904 gen_helper_mfc0_random(arg);
2908 check_insn(env, ctx, ASE_MT);
2909 gen_mfc0_load32(arg, offsetof(CPUState, CP0_VPEControl));
2913 check_insn(env, ctx, ASE_MT);
2914 gen_mfc0_load32(arg, offsetof(CPUState, CP0_VPEConf0));
2918 check_insn(env, ctx, ASE_MT);
2919 gen_mfc0_load32(arg, offsetof(CPUState, CP0_VPEConf1));
2923 check_insn(env, ctx, ASE_MT);
2924 gen_mfc0_load64(arg, offsetof(CPUState, CP0_YQMask));
2928 check_insn(env, ctx, ASE_MT);
2929 gen_mfc0_load64(arg, offsetof(CPUState, CP0_VPESchedule));
2933 check_insn(env, ctx, ASE_MT);
2934 gen_mfc0_load64(arg, offsetof(CPUState, CP0_VPEScheFBack));
2935 rn = "VPEScheFBack";
2938 check_insn(env, ctx, ASE_MT);
2939 gen_mfc0_load32(arg, offsetof(CPUState, CP0_VPEOpt));
2949 tcg_gen_ld_tl(arg, cpu_env, offsetof(CPUState, CP0_EntryLo0));
2950 tcg_gen_ext32s_tl(arg, arg);
2954 check_insn(env, ctx, ASE_MT);
2955 gen_helper_mfc0_tcstatus(arg);
2959 check_insn(env, ctx, ASE_MT);
2960 gen_helper_mfc0_tcbind(arg);
2964 check_insn(env, ctx, ASE_MT);
2965 gen_helper_mfc0_tcrestart(arg);
2969 check_insn(env, ctx, ASE_MT);
2970 gen_helper_mfc0_tchalt(arg);
2974 check_insn(env, ctx, ASE_MT);
2975 gen_helper_mfc0_tccontext(arg);
2979 check_insn(env, ctx, ASE_MT);
2980 gen_helper_mfc0_tcschedule(arg);
2984 check_insn(env, ctx, ASE_MT);
2985 gen_helper_mfc0_tcschefback(arg);
2995 tcg_gen_ld_tl(arg, cpu_env, offsetof(CPUState, CP0_EntryLo1));
2996 tcg_gen_ext32s_tl(arg, arg);
3006 tcg_gen_ld_tl(arg, cpu_env, offsetof(CPUState, CP0_Context));
3007 tcg_gen_ext32s_tl(arg, arg);
3011 // gen_helper_mfc0_contextconfig(arg); /* SmartMIPS ASE */
3012 rn = "ContextConfig";
3021 gen_mfc0_load32(arg, offsetof(CPUState, CP0_PageMask));
3025 check_insn(env, ctx, ISA_MIPS32R2);
3026 gen_mfc0_load32(arg, offsetof(CPUState, CP0_PageGrain));
3036 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Wired));
3040 check_insn(env, ctx, ISA_MIPS32R2);
3041 gen_mfc0_load32(arg, offsetof(CPUState, CP0_SRSConf0));
3045 check_insn(env, ctx, ISA_MIPS32R2);
3046 gen_mfc0_load32(arg, offsetof(CPUState, CP0_SRSConf1));
3050 check_insn(env, ctx, ISA_MIPS32R2);
3051 gen_mfc0_load32(arg, offsetof(CPUState, CP0_SRSConf2));
3055 check_insn(env, ctx, ISA_MIPS32R2);
3056 gen_mfc0_load32(arg, offsetof(CPUState, CP0_SRSConf3));
3060 check_insn(env, ctx, ISA_MIPS32R2);
3061 gen_mfc0_load32(arg, offsetof(CPUState, CP0_SRSConf4));
3071 check_insn(env, ctx, ISA_MIPS32R2);
3072 gen_mfc0_load32(arg, offsetof(CPUState, CP0_HWREna));
3082 tcg_gen_ld_tl(arg, cpu_env, offsetof(CPUState, CP0_BadVAddr));
3083 tcg_gen_ext32s_tl(arg, arg);
3093 /* Mark as an IO operation because we read the time. */
3096 gen_helper_mfc0_count(arg);
3099 ctx->bstate = BS_STOP;
3103 /* 6,7 are implementation dependent */
3111 tcg_gen_ld_tl(arg, cpu_env, offsetof(CPUState, CP0_EntryHi));
3112 tcg_gen_ext32s_tl(arg, arg);
3122 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Compare));
3125 /* 6,7 are implementation dependent */
3133 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Status));
3137 check_insn(env, ctx, ISA_MIPS32R2);
3138 gen_mfc0_load32(arg, offsetof(CPUState, CP0_IntCtl));
3142 check_insn(env, ctx, ISA_MIPS32R2);
3143 gen_mfc0_load32(arg, offsetof(CPUState, CP0_SRSCtl));
3147 check_insn(env, ctx, ISA_MIPS32R2);
3148 gen_mfc0_load32(arg, offsetof(CPUState, CP0_SRSMap));
3158 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Cause));
3168 tcg_gen_ld_tl(arg, cpu_env, offsetof(CPUState, CP0_EPC));
3169 tcg_gen_ext32s_tl(arg, arg);
3179 gen_mfc0_load32(arg, offsetof(CPUState, CP0_PRid));
3183 check_insn(env, ctx, ISA_MIPS32R2);
3184 gen_mfc0_load32(arg, offsetof(CPUState, CP0_EBase));
3194 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Config0));
3198 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Config1));
3202 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Config2));
3206 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Config3));
3209 /* 4,5 are reserved */
3210 /* 6,7 are implementation dependent */
3212 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Config6));
3216 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Config7));
3226 gen_helper_mfc0_lladdr(arg);
3236 gen_helper_1i(mfc0_watchlo, arg, sel);
3246 gen_helper_1i(mfc0_watchhi, arg, sel);
3256 #if defined(TARGET_MIPS64)
3257 check_insn(env, ctx, ISA_MIPS3);
3258 tcg_gen_ld_tl(arg, cpu_env, offsetof(CPUState, CP0_XContext));
3259 tcg_gen_ext32s_tl(arg, arg);
3268 /* Officially reserved, but sel 0 is used for R1x000 framemask */
3271 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Framemask));
3279 tcg_gen_movi_tl(arg, 0); /* unimplemented */
3280 rn = "'Diagnostic"; /* implementation dependent */
3285 gen_helper_mfc0_debug(arg); /* EJTAG support */
3289 // gen_helper_mfc0_tracecontrol(arg); /* PDtrace support */
3290 rn = "TraceControl";
3293 // gen_helper_mfc0_tracecontrol2(arg); /* PDtrace support */
3294 rn = "TraceControl2";
3297 // gen_helper_mfc0_usertracedata(arg); /* PDtrace support */
3298 rn = "UserTraceData";
3301 // gen_helper_mfc0_tracebpc(arg); /* PDtrace support */
3312 tcg_gen_ld_tl(arg, cpu_env, offsetof(CPUState, CP0_DEPC));
3313 tcg_gen_ext32s_tl(arg, arg);
3323 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Performance0));
3324 rn = "Performance0";
3327 // gen_helper_mfc0_performance1(arg);
3328 rn = "Performance1";
3331 // gen_helper_mfc0_performance2(arg);
3332 rn = "Performance2";
3335 // gen_helper_mfc0_performance3(arg);
3336 rn = "Performance3";
3339 // gen_helper_mfc0_performance4(arg);
3340 rn = "Performance4";
3343 // gen_helper_mfc0_performance5(arg);
3344 rn = "Performance5";
3347 // gen_helper_mfc0_performance6(arg);
3348 rn = "Performance6";
3351 // gen_helper_mfc0_performance7(arg);
3352 rn = "Performance7";
3359 tcg_gen_movi_tl(arg, 0); /* unimplemented */
3365 tcg_gen_movi_tl(arg, 0); /* unimplemented */
3378 gen_mfc0_load32(arg, offsetof(CPUState, CP0_TagLo));
3385 gen_mfc0_load32(arg, offsetof(CPUState, CP0_DataLo));
3398 gen_mfc0_load32(arg, offsetof(CPUState, CP0_TagHi));
3405 gen_mfc0_load32(arg, offsetof(CPUState, CP0_DataHi));
3415 tcg_gen_ld_tl(arg, cpu_env, offsetof(CPUState, CP0_ErrorEPC));
3416 tcg_gen_ext32s_tl(arg, arg);
3427 gen_mfc0_load32(arg, offsetof(CPUState, CP0_DESAVE));
3437 LOG_DISAS("mfc0 %s (reg %d sel %d)\n", rn, reg, sel);
3441 LOG_DISAS("mfc0 %s (reg %d sel %d)\n", rn, reg, sel);
3442 generate_exception(ctx, EXCP_RI);
3445 static void gen_mtc0 (CPUState *env, DisasContext *ctx, TCGv arg, int reg, int sel)
3447 const char *rn = "invalid";
3450 check_insn(env, ctx, ISA_MIPS32);
3459 gen_helper_mtc0_index(arg);
3463 check_insn(env, ctx, ASE_MT);
3464 gen_helper_mtc0_mvpcontrol(arg);
3468 check_insn(env, ctx, ASE_MT);
3473 check_insn(env, ctx, ASE_MT);
3488 check_insn(env, ctx, ASE_MT);
3489 gen_helper_mtc0_vpecontrol(arg);
3493 check_insn(env, ctx, ASE_MT);
3494 gen_helper_mtc0_vpeconf0(arg);
3498 check_insn(env, ctx, ASE_MT);
3499 gen_helper_mtc0_vpeconf1(arg);
3503 check_insn(env, ctx, ASE_MT);
3504 gen_helper_mtc0_yqmask(arg);
3508 check_insn(env, ctx, ASE_MT);
3509 gen_mtc0_store64(arg, offsetof(CPUState, CP0_VPESchedule));
3513 check_insn(env, ctx, ASE_MT);
3514 gen_mtc0_store64(arg, offsetof(CPUState, CP0_VPEScheFBack));
3515 rn = "VPEScheFBack";
3518 check_insn(env, ctx, ASE_MT);
3519 gen_helper_mtc0_vpeopt(arg);
3529 gen_helper_mtc0_entrylo0(arg);
3533 check_insn(env, ctx, ASE_MT);
3534 gen_helper_mtc0_tcstatus(arg);
3538 check_insn(env, ctx, ASE_MT);
3539 gen_helper_mtc0_tcbind(arg);
3543 check_insn(env, ctx, ASE_MT);
3544 gen_helper_mtc0_tcrestart(arg);
3548 check_insn(env, ctx, ASE_MT);
3549 gen_helper_mtc0_tchalt(arg);
3553 check_insn(env, ctx, ASE_MT);
3554 gen_helper_mtc0_tccontext(arg);
3558 check_insn(env, ctx, ASE_MT);
3559 gen_helper_mtc0_tcschedule(arg);
3563 check_insn(env, ctx, ASE_MT);
3564 gen_helper_mtc0_tcschefback(arg);
3574 gen_helper_mtc0_entrylo1(arg);
3584 gen_helper_mtc0_context(arg);
3588 // gen_helper_mtc0_contextconfig(arg); /* SmartMIPS ASE */
3589 rn = "ContextConfig";
3598 gen_helper_mtc0_pagemask(arg);
3602 check_insn(env, ctx, ISA_MIPS32R2);
3603 gen_helper_mtc0_pagegrain(arg);
3613 gen_helper_mtc0_wired(arg);
3617 check_insn(env, ctx, ISA_MIPS32R2);
3618 gen_helper_mtc0_srsconf0(arg);
3622 check_insn(env, ctx, ISA_MIPS32R2);
3623 gen_helper_mtc0_srsconf1(arg);
3627 check_insn(env, ctx, ISA_MIPS32R2);
3628 gen_helper_mtc0_srsconf2(arg);
3632 check_insn(env, ctx, ISA_MIPS32R2);
3633 gen_helper_mtc0_srsconf3(arg);
3637 check_insn(env, ctx, ISA_MIPS32R2);
3638 gen_helper_mtc0_srsconf4(arg);
3648 check_insn(env, ctx, ISA_MIPS32R2);
3649 gen_helper_mtc0_hwrena(arg);
3663 gen_helper_mtc0_count(arg);
3666 /* 6,7 are implementation dependent */
3674 gen_helper_mtc0_entryhi(arg);
3684 gen_helper_mtc0_compare(arg);
3687 /* 6,7 are implementation dependent */
3695 save_cpu_state(ctx, 1);
3696 gen_helper_mtc0_status(arg);
3697 /* BS_STOP isn't good enough here, hflags may have changed. */
3698 gen_save_pc(ctx->pc + 4);
3699 ctx->bstate = BS_EXCP;
3703 check_insn(env, ctx, ISA_MIPS32R2);
3704 gen_helper_mtc0_intctl(arg);
3705 /* Stop translation as we may have switched the execution mode */
3706 ctx->bstate = BS_STOP;
3710 check_insn(env, ctx, ISA_MIPS32R2);
3711 gen_helper_mtc0_srsctl(arg);
3712 /* Stop translation as we may have switched the execution mode */
3713 ctx->bstate = BS_STOP;
3717 check_insn(env, ctx, ISA_MIPS32R2);
3718 gen_mtc0_store32(arg, offsetof(CPUState, CP0_SRSMap));
3719 /* Stop translation as we may have switched the execution mode */
3720 ctx->bstate = BS_STOP;
3730 save_cpu_state(ctx, 1);
3731 gen_helper_mtc0_cause(arg);
3741 gen_mtc0_store64(arg, offsetof(CPUState, CP0_EPC));
3755 check_insn(env, ctx, ISA_MIPS32R2);
3756 gen_helper_mtc0_ebase(arg);
3766 gen_helper_mtc0_config0(arg);
3768 /* Stop translation as we may have switched the execution mode */
3769 ctx->bstate = BS_STOP;
3772 /* ignored, read only */
3776 gen_helper_mtc0_config2(arg);
3778 /* Stop translation as we may have switched the execution mode */
3779 ctx->bstate = BS_STOP;
3782 /* ignored, read only */
3785 /* 4,5 are reserved */
3786 /* 6,7 are implementation dependent */
3796 rn = "Invalid config selector";
3803 gen_helper_mtc0_lladdr(arg);
3813 gen_helper_1i(mtc0_watchlo, arg, sel);
3823 gen_helper_1i(mtc0_watchhi, arg, sel);
3833 #if defined(TARGET_MIPS64)
3834 check_insn(env, ctx, ISA_MIPS3);
3835 gen_helper_mtc0_xcontext(arg);
3844 /* Officially reserved, but sel 0 is used for R1x000 framemask */
3847 gen_helper_mtc0_framemask(arg);
3856 rn = "Diagnostic"; /* implementation dependent */
3861 gen_helper_mtc0_debug(arg); /* EJTAG support */
3862 /* BS_STOP isn't good enough here, hflags may have changed. */
3863 gen_save_pc(ctx->pc + 4);
3864 ctx->bstate = BS_EXCP;
3868 // gen_helper_mtc0_tracecontrol(arg); /* PDtrace support */
3869 rn = "TraceControl";
3870 /* Stop translation as we may have switched the execution mode */
3871 ctx->bstate = BS_STOP;
3874 // gen_helper_mtc0_tracecontrol2(arg); /* PDtrace support */
3875 rn = "TraceControl2";
3876 /* Stop translation as we may have switched the execution mode */
3877 ctx->bstate = BS_STOP;
3880 /* Stop translation as we may have switched the execution mode */
3881 ctx->bstate = BS_STOP;
3882 // gen_helper_mtc0_usertracedata(arg); /* PDtrace support */
3883 rn = "UserTraceData";
3884 /* Stop translation as we may have switched the execution mode */
3885 ctx->bstate = BS_STOP;
3888 // gen_helper_mtc0_tracebpc(arg); /* PDtrace support */
3889 /* Stop translation as we may have switched the execution mode */
3890 ctx->bstate = BS_STOP;
3901 gen_mtc0_store64(arg, offsetof(CPUState, CP0_DEPC));
3911 gen_helper_mtc0_performance0(arg);
3912 rn = "Performance0";
3915 // gen_helper_mtc0_performance1(arg);
3916 rn = "Performance1";
3919 // gen_helper_mtc0_performance2(arg);
3920 rn = "Performance2";
3923 // gen_helper_mtc0_performance3(arg);
3924 rn = "Performance3";
3927 // gen_helper_mtc0_performance4(arg);
3928 rn = "Performance4";
3931 // gen_helper_mtc0_performance5(arg);
3932 rn = "Performance5";
3935 // gen_helper_mtc0_performance6(arg);
3936 rn = "Performance6";
3939 // gen_helper_mtc0_performance7(arg);
3940 rn = "Performance7";
3966 gen_helper_mtc0_taglo(arg);
3973 gen_helper_mtc0_datalo(arg);
3986 gen_helper_mtc0_taghi(arg);
3993 gen_helper_mtc0_datahi(arg);
4004 gen_mtc0_store64(arg, offsetof(CPUState, CP0_ErrorEPC));
4015 gen_mtc0_store32(arg, offsetof(CPUState, CP0_DESAVE));
4021 /* Stop translation as we may have switched the execution mode */
4022 ctx->bstate = BS_STOP;
4027 LOG_DISAS("mtc0 %s (reg %d sel %d)\n", rn, reg, sel);
4028 /* For simplicity assume that all writes can cause interrupts. */
4031 ctx->bstate = BS_STOP;
4036 LOG_DISAS("mtc0 %s (reg %d sel %d)\n", rn, reg, sel);
4037 generate_exception(ctx, EXCP_RI);
4040 #if defined(TARGET_MIPS64)
4041 static void gen_dmfc0 (CPUState *env, DisasContext *ctx, TCGv arg, int reg, int sel)
4043 const char *rn = "invalid";
4046 check_insn(env, ctx, ISA_MIPS64);
4052 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Index));
4056 check_insn(env, ctx, ASE_MT);
4057 gen_helper_mfc0_mvpcontrol(arg);
4061 check_insn(env, ctx, ASE_MT);
4062 gen_helper_mfc0_mvpconf0(arg);
4066 check_insn(env, ctx, ASE_MT);
4067 gen_helper_mfc0_mvpconf1(arg);
4077 gen_helper_mfc0_random(arg);
4081 check_insn(env, ctx, ASE_MT);
4082 gen_mfc0_load32(arg, offsetof(CPUState, CP0_VPEControl));
4086 check_insn(env, ctx, ASE_MT);
4087 gen_mfc0_load32(arg, offsetof(CPUState, CP0_VPEConf0));
4091 check_insn(env, ctx, ASE_MT);
4092 gen_mfc0_load32(arg, offsetof(CPUState, CP0_VPEConf1));
4096 check_insn(env, ctx, ASE_MT);
4097 tcg_gen_ld_tl(arg, cpu_env, offsetof(CPUState, CP0_YQMask));
4101 check_insn(env, ctx, ASE_MT);
4102 tcg_gen_ld_tl(arg, cpu_env, offsetof(CPUState, CP0_VPESchedule));
4106 check_insn(env, ctx, ASE_MT);
4107 tcg_gen_ld_tl(arg, cpu_env, offsetof(CPUState, CP0_VPEScheFBack));
4108 rn = "VPEScheFBack";
4111 check_insn(env, ctx, ASE_MT);
4112 gen_mfc0_load32(arg, offsetof(CPUState, CP0_VPEOpt));
4122 tcg_gen_ld_tl(arg, cpu_env, offsetof(CPUState, CP0_EntryLo0));
4126 check_insn(env, ctx, ASE_MT);
4127 gen_helper_mfc0_tcstatus(arg);
4131 check_insn(env, ctx, ASE_MT);
4132 gen_helper_mfc0_tcbind(arg);
4136 check_insn(env, ctx, ASE_MT);
4137 gen_helper_dmfc0_tcrestart(arg);
4141 check_insn(env, ctx, ASE_MT);
4142 gen_helper_dmfc0_tchalt(arg);
4146 check_insn(env, ctx, ASE_MT);
4147 gen_helper_dmfc0_tccontext(arg);
4151 check_insn(env, ctx, ASE_MT);
4152 gen_helper_dmfc0_tcschedule(arg);
4156 check_insn(env, ctx, ASE_MT);
4157 gen_helper_dmfc0_tcschefback(arg);
4167 tcg_gen_ld_tl(arg, cpu_env, offsetof(CPUState, CP0_EntryLo1));
4177 tcg_gen_ld_tl(arg, cpu_env, offsetof(CPUState, CP0_Context));
4181 // gen_helper_dmfc0_contextconfig(arg); /* SmartMIPS ASE */
4182 rn = "ContextConfig";
4191 gen_mfc0_load32(arg, offsetof(CPUState, CP0_PageMask));
4195 check_insn(env, ctx, ISA_MIPS32R2);
4196 gen_mfc0_load32(arg, offsetof(CPUState, CP0_PageGrain));
4206 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Wired));
4210 check_insn(env, ctx, ISA_MIPS32R2);
4211 gen_mfc0_load32(arg, offsetof(CPUState, CP0_SRSConf0));
4215 check_insn(env, ctx, ISA_MIPS32R2);
4216 gen_mfc0_load32(arg, offsetof(CPUState, CP0_SRSConf1));
4220 check_insn(env, ctx, ISA_MIPS32R2);
4221 gen_mfc0_load32(arg, offsetof(CPUState, CP0_SRSConf2));
4225 check_insn(env, ctx, ISA_MIPS32R2);
4226 gen_mfc0_load32(arg, offsetof(CPUState, CP0_SRSConf3));
4230 check_insn(env, ctx, ISA_MIPS32R2);
4231 gen_mfc0_load32(arg, offsetof(CPUState, CP0_SRSConf4));
4241 check_insn(env, ctx, ISA_MIPS32R2);
4242 gen_mfc0_load32(arg, offsetof(CPUState, CP0_HWREna));
4252 tcg_gen_ld_tl(arg, cpu_env, offsetof(CPUState, CP0_BadVAddr));
4262 /* Mark as an IO operation because we read the time. */
4265 gen_helper_mfc0_count(arg);
4268 ctx->bstate = BS_STOP;
4272 /* 6,7 are implementation dependent */
4280 tcg_gen_ld_tl(arg, cpu_env, offsetof(CPUState, CP0_EntryHi));
4290 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Compare));
4293 /* 6,7 are implementation dependent */
4301 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Status));
4305 check_insn(env, ctx, ISA_MIPS32R2);
4306 gen_mfc0_load32(arg, offsetof(CPUState, CP0_IntCtl));
4310 check_insn(env, ctx, ISA_MIPS32R2);
4311 gen_mfc0_load32(arg, offsetof(CPUState, CP0_SRSCtl));
4315 check_insn(env, ctx, ISA_MIPS32R2);
4316 gen_mfc0_load32(arg, offsetof(CPUState, CP0_SRSMap));
4326 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Cause));
4336 tcg_gen_ld_tl(arg, cpu_env, offsetof(CPUState, CP0_EPC));
4346 gen_mfc0_load32(arg, offsetof(CPUState, CP0_PRid));
4350 check_insn(env, ctx, ISA_MIPS32R2);
4351 gen_mfc0_load32(arg, offsetof(CPUState, CP0_EBase));
4361 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Config0));
4365 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Config1));
4369 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Config2));
4373 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Config3));
4376 /* 6,7 are implementation dependent */
4378 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Config6));
4382 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Config7));
4392 gen_helper_dmfc0_lladdr(arg);
4402 gen_helper_1i(dmfc0_watchlo, arg, sel);
4412 gen_helper_1i(mfc0_watchhi, arg, sel);
4422 check_insn(env, ctx, ISA_MIPS3);
4423 tcg_gen_ld_tl(arg, cpu_env, offsetof(CPUState, CP0_XContext));
4431 /* Officially reserved, but sel 0 is used for R1x000 framemask */
4434 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Framemask));
4442 tcg_gen_movi_tl(arg, 0); /* unimplemented */
4443 rn = "'Diagnostic"; /* implementation dependent */
4448 gen_helper_mfc0_debug(arg); /* EJTAG support */
4452 // gen_helper_dmfc0_tracecontrol(arg); /* PDtrace support */
4453 rn = "TraceControl";
4456 // gen_helper_dmfc0_tracecontrol2(arg); /* PDtrace support */
4457 rn = "TraceControl2";
4460 // gen_helper_dmfc0_usertracedata(arg); /* PDtrace support */
4461 rn = "UserTraceData";
4464 // gen_helper_dmfc0_tracebpc(arg); /* PDtrace support */
4475 tcg_gen_ld_tl(arg, cpu_env, offsetof(CPUState, CP0_DEPC));
4485 gen_mfc0_load32(arg, offsetof(CPUState, CP0_Performance0));
4486 rn = "Performance0";
4489 // gen_helper_dmfc0_performance1(arg);
4490 rn = "Performance1";
4493 // gen_helper_dmfc0_performance2(arg);
4494 rn = "Performance2";
4497 // gen_helper_dmfc0_performance3(arg);
4498 rn = "Performance3";
4501 // gen_helper_dmfc0_performance4(arg);
4502 rn = "Performance4";
4505 // gen_helper_dmfc0_performance5(arg);
4506 rn = "Performance5";
4509 // gen_helper_dmfc0_performance6(arg);
4510 rn = "Performance6";
4513 // gen_helper_dmfc0_performance7(arg);
4514 rn = "Performance7";
4521 tcg_gen_movi_tl(arg, 0); /* unimplemented */
4528 tcg_gen_movi_tl(arg, 0); /* unimplemented */
4541 gen_mfc0_load32(arg, offsetof(CPUState, CP0_TagLo));
4548 gen_mfc0_load32(arg, offsetof(CPUState, CP0_DataLo));
4561 gen_mfc0_load32(arg, offsetof(CPUState, CP0_TagHi));
4568 gen_mfc0_load32(arg, offsetof(CPUState, CP0_DataHi));
4578 tcg_gen_ld_tl(arg, cpu_env, offsetof(CPUState, CP0_ErrorEPC));
4589 gen_mfc0_load32(arg, offsetof(CPUState, CP0_DESAVE));
4599 LOG_DISAS("dmfc0 %s (reg %d sel %d)\n", rn, reg, sel);
4603 LOG_DISAS("dmfc0 %s (reg %d sel %d)\n", rn, reg, sel);
4604 generate_exception(ctx, EXCP_RI);
4607 static void gen_dmtc0 (CPUState *env, DisasContext *ctx, TCGv arg, int reg, int sel)
4609 const char *rn = "invalid";
4612 check_insn(env, ctx, ISA_MIPS64);
4621 gen_helper_mtc0_index(arg);
4625 check_insn(env, ctx, ASE_MT);
4626 gen_helper_mtc0_mvpcontrol(arg);
4630 check_insn(env, ctx, ASE_MT);
4635 check_insn(env, ctx, ASE_MT);
4650 check_insn(env, ctx, ASE_MT);
4651 gen_helper_mtc0_vpecontrol(arg);
4655 check_insn(env, ctx, ASE_MT);
4656 gen_helper_mtc0_vpeconf0(arg);
4660 check_insn(env, ctx, ASE_MT);
4661 gen_helper_mtc0_vpeconf1(arg);
4665 check_insn(env, ctx, ASE_MT);
4666 gen_helper_mtc0_yqmask(arg);
4670 check_insn(env, ctx, ASE_MT);
4671 tcg_gen_st_tl(arg, cpu_env, offsetof(CPUState, CP0_VPESchedule));
4675 check_insn(env, ctx, ASE_MT);
4676 tcg_gen_st_tl(arg, cpu_env, offsetof(CPUState, CP0_VPEScheFBack));
4677 rn = "VPEScheFBack";
4680 check_insn(env, ctx, ASE_MT);
4681 gen_helper_mtc0_vpeopt(arg);
4691 gen_helper_mtc0_entrylo0(arg);
4695 check_insn(env, ctx, ASE_MT);
4696 gen_helper_mtc0_tcstatus(arg);
4700 check_insn(env, ctx, ASE_MT);
4701 gen_helper_mtc0_tcbind(arg);
4705 check_insn(env, ctx, ASE_MT);
4706 gen_helper_mtc0_tcrestart(arg);
4710 check_insn(env, ctx, ASE_MT);
4711 gen_helper_mtc0_tchalt(arg);
4715 check_insn(env, ctx, ASE_MT);
4716 gen_helper_mtc0_tccontext(arg);
4720 check_insn(env, ctx, ASE_MT);
4721 gen_helper_mtc0_tcschedule(arg);
4725 check_insn(env, ctx, ASE_MT);
4726 gen_helper_mtc0_tcschefback(arg);
4736 gen_helper_mtc0_entrylo1(arg);
4746 gen_helper_mtc0_context(arg);
4750 // gen_helper_mtc0_contextconfig(arg); /* SmartMIPS ASE */
4751 rn = "ContextConfig";
4760 gen_helper_mtc0_pagemask(arg);
4764 check_insn(env, ctx, ISA_MIPS32R2);
4765 gen_helper_mtc0_pagegrain(arg);
4775 gen_helper_mtc0_wired(arg);
4779 check_insn(env, ctx, ISA_MIPS32R2);
4780 gen_helper_mtc0_srsconf0(arg);
4784 check_insn(env, ctx, ISA_MIPS32R2);
4785 gen_helper_mtc0_srsconf1(arg);
4789 check_insn(env, ctx, ISA_MIPS32R2);
4790 gen_helper_mtc0_srsconf2(arg);
4794 check_insn(env, ctx, ISA_MIPS32R2);
4795 gen_helper_mtc0_srsconf3(arg);
4799 check_insn(env, ctx, ISA_MIPS32R2);
4800 gen_helper_mtc0_srsconf4(arg);
4810 check_insn(env, ctx, ISA_MIPS32R2);
4811 gen_helper_mtc0_hwrena(arg);
4825 gen_helper_mtc0_count(arg);
4828 /* 6,7 are implementation dependent */
4832 /* Stop translation as we may have switched the execution mode */
4833 ctx->bstate = BS_STOP;
4838 gen_helper_mtc0_entryhi(arg);
4848 gen_helper_mtc0_compare(arg);
4851 /* 6,7 are implementation dependent */
4855 /* Stop translation as we may have switched the execution mode */
4856 ctx->bstate = BS_STOP;
4861 save_cpu_state(ctx, 1);
4862 gen_helper_mtc0_status(arg);
4863 /* BS_STOP isn't good enough here, hflags may have changed. */
4864 gen_save_pc(ctx->pc + 4);
4865 ctx->bstate = BS_EXCP;
4869 check_insn(env, ctx, ISA_MIPS32R2);
4870 gen_helper_mtc0_intctl(arg);
4871 /* Stop translation as we may have switched the execution mode */
4872 ctx->bstate = BS_STOP;
4876 check_insn(env, ctx, ISA_MIPS32R2);
4877 gen_helper_mtc0_srsctl(arg);
4878 /* Stop translation as we may have switched the execution mode */
4879 ctx->bstate = BS_STOP;
4883 check_insn(env, ctx, ISA_MIPS32R2);
4884 gen_mtc0_store32(arg, offsetof(CPUState, CP0_SRSMap));
4885 /* Stop translation as we may have switched the execution mode */
4886 ctx->bstate = BS_STOP;
4896 save_cpu_state(ctx, 1);
4897 gen_helper_mtc0_cause(arg);
4907 tcg_gen_st_tl(arg, cpu_env, offsetof(CPUState, CP0_EPC));
4921 check_insn(env, ctx, ISA_MIPS32R2);
4922 gen_helper_mtc0_ebase(arg);
4932 gen_helper_mtc0_config0(arg);
4934 /* Stop translation as we may have switched the execution mode */
4935 ctx->bstate = BS_STOP;
4938 /* ignored, read only */
4942 gen_helper_mtc0_config2(arg);
4944 /* Stop translation as we may have switched the execution mode */
4945 ctx->bstate = BS_STOP;
4951 /* 6,7 are implementation dependent */
4953 rn = "Invalid config selector";
4960 gen_helper_mtc0_lladdr(arg);
4970 gen_helper_1i(mtc0_watchlo, arg, sel);
4980 gen_helper_1i(mtc0_watchhi, arg, sel);
4990 check_insn(env, ctx, ISA_MIPS3);
4991 gen_helper_mtc0_xcontext(arg);
4999 /* Officially reserved, but sel 0 is used for R1x000 framemask */
5002 gen_helper_mtc0_framemask(arg);
5011 rn = "Diagnostic"; /* implementation dependent */
5016 gen_helper_mtc0_debug(arg); /* EJTAG support */
5017 /* BS_STOP isn't good enough here, hflags may have changed. */
5018 gen_save_pc(ctx->pc + 4);
5019 ctx->bstate = BS_EXCP;
5023 // gen_helper_mtc0_tracecontrol(arg); /* PDtrace support */
5024 /* Stop translation as we may have switched the execution mode */
5025 ctx->bstate = BS_STOP;
5026 rn = "TraceControl";
5029 // gen_helper_mtc0_tracecontrol2(arg); /* PDtrace support */
5030 /* Stop translation as we may have switched the execution mode */
5031 ctx->bstate = BS_STOP;
5032 rn = "TraceControl2";
5035 // gen_helper_mtc0_usertracedata(arg); /* PDtrace support */
5036 /* Stop translation as we may have switched the execution mode */
5037 ctx->bstate = BS_STOP;
5038 rn = "UserTraceData";
5041 // gen_helper_mtc0_tracebpc(arg); /* PDtrace support */
5042 /* Stop translation as we may have switched the execution mode */
5043 ctx->bstate = BS_STOP;
5054 tcg_gen_st_tl(arg, cpu_env, offsetof(CPUState, CP0_DEPC));
5064 gen_helper_mtc0_performance0(arg);
5065 rn = "Performance0";
5068 // gen_helper_mtc0_performance1(arg);
5069 rn = "Performance1";
5072 // gen_helper_mtc0_performance2(arg);
5073 rn = "Performance2";
5076 // gen_helper_mtc0_performance3(arg);
5077 rn = "Performance3";
5080 // gen_helper_mtc0_performance4(arg);
5081 rn = "Performance4";
5084 // gen_helper_mtc0_performance5(arg);
5085 rn = "Performance5";
5088 // gen_helper_mtc0_performance6(arg);
5089 rn = "Performance6";
5092 // gen_helper_mtc0_performance7(arg);
5093 rn = "Performance7";
5119 gen_helper_mtc0_taglo(arg);
5126 gen_helper_mtc0_datalo(arg);
5139 gen_helper_mtc0_taghi(arg);
5146 gen_helper_mtc0_datahi(arg);
5157 tcg_gen_st_tl(arg, cpu_env, offsetof(CPUState, CP0_ErrorEPC));
5168 gen_mtc0_store32(arg, offsetof(CPUState, CP0_DESAVE));
5174 /* Stop translation as we may have switched the execution mode */
5175 ctx->bstate = BS_STOP;
5180 LOG_DISAS("dmtc0 %s (reg %d sel %d)\n", rn, reg, sel);
5181 /* For simplicity assume that all writes can cause interrupts. */
5184 ctx->bstate = BS_STOP;
5189 LOG_DISAS("dmtc0 %s (reg %d sel %d)\n", rn, reg, sel);
5190 generate_exception(ctx, EXCP_RI);
5192 #endif /* TARGET_MIPS64 */
5194 static void gen_mftr(CPUState *env, DisasContext *ctx, int rt, int rd,
5195 int u, int sel, int h)
5197 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
5198 TCGv t0 = tcg_temp_local_new();
5200 if ((env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP)) == 0 &&
5201 ((env->tcs[other_tc].CP0_TCBind & (0xf << CP0TCBd_CurVPE)) !=
5202 (env->active_tc.CP0_TCBind & (0xf << CP0TCBd_CurVPE))))
5203 tcg_gen_movi_tl(t0, -1);
5204 else if ((env->CP0_VPEControl & (0xff << CP0VPECo_TargTC)) >
5205 (env->mvp->CP0_MVPConf0 & (0xff << CP0MVPC0_PTC)))
5206 tcg_gen_movi_tl(t0, -1);
5212 gen_helper_mftc0_tcstatus(t0);
5215 gen_helper_mftc0_tcbind(t0);
5218 gen_helper_mftc0_tcrestart(t0);
5221 gen_helper_mftc0_tchalt(t0);
5224 gen_helper_mftc0_tccontext(t0);
5227 gen_helper_mftc0_tcschedule(t0);
5230 gen_helper_mftc0_tcschefback(t0);
5233 gen_mfc0(env, ctx, t0, rt, sel);
5240 gen_helper_mftc0_entryhi(t0);
5243 gen_mfc0(env, ctx, t0, rt, sel);
5249 gen_helper_mftc0_status(t0);
5252 gen_mfc0(env, ctx, t0, rt, sel);
5258 gen_helper_mftc0_debug(t0);
5261 gen_mfc0(env, ctx, t0, rt, sel);
5266 gen_mfc0(env, ctx, t0, rt, sel);
5268 } else switch (sel) {
5269 /* GPR registers. */
5271 gen_helper_1i(mftgpr, t0, rt);
5273 /* Auxiliary CPU registers */
5277 gen_helper_1i(mftlo, t0, 0);
5280 gen_helper_1i(mfthi, t0, 0);
5283 gen_helper_1i(mftacx, t0, 0);
5286 gen_helper_1i(mftlo, t0, 1);
5289 gen_helper_1i(mfthi, t0, 1);
5292 gen_helper_1i(mftacx, t0, 1);
5295 gen_helper_1i(mftlo, t0, 2);
5298 gen_helper_1i(mfthi, t0, 2);
5301 gen_helper_1i(mftacx, t0, 2);
5304 gen_helper_1i(mftlo, t0, 3);
5307 gen_helper_1i(mfthi, t0, 3);
5310 gen_helper_1i(mftacx, t0, 3);
5313 gen_helper_mftdsp(t0);
5319 /* Floating point (COP1). */
5321 /* XXX: For now we support only a single FPU context. */
5323 TCGv_i32 fp0 = tcg_temp_new_i32();
5325 gen_load_fpr32(fp0, rt);
5326 tcg_gen_ext_i32_tl(t0, fp0);
5327 tcg_temp_free_i32(fp0);
5329 TCGv_i32 fp0 = tcg_temp_new_i32();
5331 gen_load_fpr32h(fp0, rt);
5332 tcg_gen_ext_i32_tl(t0, fp0);
5333 tcg_temp_free_i32(fp0);
5337 /* XXX: For now we support only a single FPU context. */
5338 gen_helper_1i(cfc1, t0, rt);
5340 /* COP2: Not implemented. */
5347 LOG_DISAS("mftr (reg %d u %d sel %d h %d)\n", rt, u, sel, h);
5348 gen_store_gpr(t0, rd);
5354 LOG_DISAS("mftr (reg %d u %d sel %d h %d)\n", rt, u, sel, h);
5355 generate_exception(ctx, EXCP_RI);
5358 static void gen_mttr(CPUState *env, DisasContext *ctx, int rd, int rt,
5359 int u, int sel, int h)
5361 int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
5362 TCGv t0 = tcg_temp_local_new();
5364 gen_load_gpr(t0, rt);
5365 if ((env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP)) == 0 &&
5366 ((env->tcs[other_tc].CP0_TCBind & (0xf << CP0TCBd_CurVPE)) !=
5367 (env->active_tc.CP0_TCBind & (0xf << CP0TCBd_CurVPE))))
5369 else if ((env->CP0_VPEControl & (0xff << CP0VPECo_TargTC)) >
5370 (env->mvp->CP0_MVPConf0 & (0xff << CP0MVPC0_PTC)))
5377 gen_helper_mttc0_tcstatus(t0);
5380 gen_helper_mttc0_tcbind(t0);
5383 gen_helper_mttc0_tcrestart(t0);
5386 gen_helper_mttc0_tchalt(t0);
5389 gen_helper_mttc0_tccontext(t0);
5392 gen_helper_mttc0_tcschedule(t0);
5395 gen_helper_mttc0_tcschefback(t0);
5398 gen_mtc0(env, ctx, t0, rd, sel);
5405 gen_helper_mttc0_entryhi(t0);
5408 gen_mtc0(env, ctx, t0, rd, sel);
5414 gen_helper_mttc0_status(t0);
5417 gen_mtc0(env, ctx, t0, rd, sel);
5423 gen_helper_mttc0_debug(t0);
5426 gen_mtc0(env, ctx, t0, rd, sel);
5431 gen_mtc0(env, ctx, t0, rd, sel);
5433 } else switch (sel) {
5434 /* GPR registers. */
5436 gen_helper_1i(mttgpr, t0, rd);
5438 /* Auxiliary CPU registers */
5442 gen_helper_1i(mttlo, t0, 0);
5445 gen_helper_1i(mtthi, t0, 0);
5448 gen_helper_1i(mttacx, t0, 0);
5451 gen_helper_1i(mttlo, t0, 1);
5454 gen_helper_1i(mtthi, t0, 1);
5457 gen_helper_1i(mttacx, t0, 1);
5460 gen_helper_1i(mttlo, t0, 2);
5463 gen_helper_1i(mtthi, t0, 2);
5466 gen_helper_1i(mttacx, t0, 2);
5469 gen_helper_1i(mttlo, t0, 3);
5472 gen_helper_1i(mtthi, t0, 3);
5475 gen_helper_1i(mttacx, t0, 3);
5478 gen_helper_mttdsp(t0);
5484 /* Floating point (COP1). */
5486 /* XXX: For now we support only a single FPU context. */
5488 TCGv_i32 fp0 = tcg_temp_new_i32();
5490 tcg_gen_trunc_tl_i32(fp0, t0);
5491 gen_store_fpr32(fp0, rd);
5492 tcg_temp_free_i32(fp0);
5494 TCGv_i32 fp0 = tcg_temp_new_i32();
5496 tcg_gen_trunc_tl_i32(fp0, t0);
5497 gen_store_fpr32h(fp0, rd);
5498 tcg_temp_free_i32(fp0);
5502 /* XXX: For now we support only a single FPU context. */
5503 gen_helper_1i(ctc1, t0, rd);
5505 /* COP2: Not implemented. */
5512 LOG_DISAS("mttr (reg %d u %d sel %d h %d)\n", rd, u, sel, h);
5518 LOG_DISAS("mttr (reg %d u %d sel %d h %d)\n", rd, u, sel, h);
5519 generate_exception(ctx, EXCP_RI);
5522 static void gen_cp0 (CPUState *env, DisasContext *ctx, uint32_t opc, int rt, int rd)
5524 const char *opn = "ldst";
5532 gen_mfc0(env, ctx, cpu_gpr[rt], rd, ctx->opcode & 0x7);
5537 TCGv t0 = tcg_temp_new();
5539 gen_load_gpr(t0, rt);
5540 gen_mtc0(env, ctx, t0, rd, ctx->opcode & 0x7);
5545 #if defined(TARGET_MIPS64)
5547 check_insn(env, ctx, ISA_MIPS3);
5552 gen_dmfc0(env, ctx, cpu_gpr[rt], rd, ctx->opcode & 0x7);
5556 check_insn(env, ctx, ISA_MIPS3);
5558 TCGv t0 = tcg_temp_new();
5560 gen_load_gpr(t0, rt);
5561 gen_dmtc0(env, ctx, t0, rd, ctx->opcode & 0x7);
5568 check_insn(env, ctx, ASE_MT);
5573 gen_mftr(env, ctx, rt, rd, (ctx->opcode >> 5) & 1,
5574 ctx->opcode & 0x7, (ctx->opcode >> 4) & 1);
5578 check_insn(env, ctx, ASE_MT);
5579 gen_mttr(env, ctx, rd, rt, (ctx->opcode >> 5) & 1,
5580 ctx->opcode & 0x7, (ctx->opcode >> 4) & 1);
5585 if (!env->tlb->helper_tlbwi)
5591 if (!env->tlb->helper_tlbwr)
5597 if (!env->tlb->helper_tlbp)
5603 if (!env->tlb->helper_tlbr)
5609 check_insn(env, ctx, ISA_MIPS2);
5611 ctx->bstate = BS_EXCP;
5615 check_insn(env, ctx, ISA_MIPS32);
5616 if (!(ctx->hflags & MIPS_HFLAG_DM)) {
5618 generate_exception(ctx, EXCP_RI);
5621 ctx->bstate = BS_EXCP;
5626 check_insn(env, ctx, ISA_MIPS3 | ISA_MIPS32);
5627 /* If we get an exception, we want to restart at next instruction */
5629 save_cpu_state(ctx, 1);
5632 ctx->bstate = BS_EXCP;
5637 generate_exception(ctx, EXCP_RI);
5640 MIPS_DEBUG("%s %s %d", opn, regnames[rt], rd);
5642 #endif /* !CONFIG_USER_ONLY */
5644 /* CP1 Branches (before delay slot) */
5645 static void gen_compute_branch1 (CPUState *env, DisasContext *ctx, uint32_t op,
5646 int32_t cc, int32_t offset)
5648 target_ulong btarget;
5649 const char *opn = "cp1 cond branch";
5650 TCGv_i32 t0 = tcg_temp_new_i32();
5653 check_insn(env, ctx, ISA_MIPS4 | ISA_MIPS32);
5655 btarget = ctx->pc + 4 + offset;
5659 tcg_gen_shri_i32(t0, fpu_fcr31, get_fp_bit(cc));
5660 tcg_gen_not_i32(t0, t0);
5661 tcg_gen_andi_i32(t0, t0, 1);
5662 tcg_gen_extu_i32_tl(bcond, t0);
5666 tcg_gen_shri_i32(t0, fpu_fcr31, get_fp_bit(cc));
5667 tcg_gen_not_i32(t0, t0);
5668 tcg_gen_andi_i32(t0, t0, 1);
5669 tcg_gen_extu_i32_tl(bcond, t0);
5673 tcg_gen_shri_i32(t0, fpu_fcr31, get_fp_bit(cc));
5674 tcg_gen_andi_i32(t0, t0, 1);
5675 tcg_gen_extu_i32_tl(bcond, t0);
5679 tcg_gen_shri_i32(t0, fpu_fcr31, get_fp_bit(cc));
5680 tcg_gen_andi_i32(t0, t0, 1);
5681 tcg_gen_extu_i32_tl(bcond, t0);
5684 ctx->hflags |= MIPS_HFLAG_BL;
5688 TCGv_i32 t1 = tcg_temp_new_i32();
5689 tcg_gen_shri_i32(t0, fpu_fcr31, get_fp_bit(cc));
5690 tcg_gen_shri_i32(t1, fpu_fcr31, get_fp_bit(cc+1));
5691 tcg_gen_or_i32(t0, t0, t1);
5692 tcg_temp_free_i32(t1);
5693 tcg_gen_not_i32(t0, t0);
5694 tcg_gen_andi_i32(t0, t0, 1);
5695 tcg_gen_extu_i32_tl(bcond, t0);
5701 TCGv_i32 t1 = tcg_temp_new_i32();
5702 tcg_gen_shri_i32(t0, fpu_fcr31, get_fp_bit(cc));
5703 tcg_gen_shri_i32(t1, fpu_fcr31, get_fp_bit(cc+1));
5704 tcg_gen_or_i32(t0, t0, t1);
5705 tcg_temp_free_i32(t1);
5706 tcg_gen_andi_i32(t0, t0, 1);
5707 tcg_gen_extu_i32_tl(bcond, t0);
5713 TCGv_i32 t1 = tcg_temp_new_i32();
5714 tcg_gen_shri_i32(t0, fpu_fcr31, get_fp_bit(cc));
5715 tcg_gen_shri_i32(t1, fpu_fcr31, get_fp_bit(cc+1));
5716 tcg_gen_or_i32(t0, t0, t1);
5717 tcg_gen_shri_i32(t1, fpu_fcr31, get_fp_bit(cc+2));
5718 tcg_gen_or_i32(t0, t0, t1);
5719 tcg_gen_shri_i32(t1, fpu_fcr31, get_fp_bit(cc+3));
5720 tcg_gen_or_i32(t0, t0, t1);
5721 tcg_temp_free_i32(t1);
5722 tcg_gen_not_i32(t0, t0);
5723 tcg_gen_andi_i32(t0, t0, 1);
5724 tcg_gen_extu_i32_tl(bcond, t0);
5730 TCGv_i32 t1 = tcg_temp_new_i32();
5731 tcg_gen_shri_i32(t0, fpu_fcr31, get_fp_bit(cc));
5732 tcg_gen_shri_i32(t1, fpu_fcr31, get_fp_bit(cc+1));
5733 tcg_gen_or_i32(t0, t0, t1);
5734 tcg_gen_shri_i32(t1, fpu_fcr31, get_fp_bit(cc+2));
5735 tcg_gen_or_i32(t0, t0, t1);
5736 tcg_gen_shri_i32(t1, fpu_fcr31, get_fp_bit(cc+3));
5737 tcg_gen_or_i32(t0, t0, t1);
5738 tcg_temp_free_i32(t1);
5739 tcg_gen_andi_i32(t0, t0, 1);
5740 tcg_gen_extu_i32_tl(bcond, t0);
5744 ctx->hflags |= MIPS_HFLAG_BC;
5748 generate_exception (ctx, EXCP_RI);
5751 MIPS_DEBUG("%s: cond %02x target " TARGET_FMT_lx, opn,
5752 ctx->hflags, btarget);
5753 ctx->btarget = btarget;
5756 tcg_temp_free_i32(t0);
5759 /* Coprocessor 1 (FPU) */
5761 #define FOP(func, fmt) (((fmt) << 21) | (func))
5763 static void gen_cp1 (DisasContext *ctx, uint32_t opc, int rt, int fs)
5765 const char *opn = "cp1 move";
5766 TCGv t0 = tcg_temp_new();
5771 TCGv_i32 fp0 = tcg_temp_new_i32();
5773 gen_load_fpr32(fp0, fs);
5774 tcg_gen_ext_i32_tl(t0, fp0);
5775 tcg_temp_free_i32(fp0);
5777 gen_store_gpr(t0, rt);
5781 gen_load_gpr(t0, rt);
5783 TCGv_i32 fp0 = tcg_temp_new_i32();
5785 tcg_gen_trunc_tl_i32(fp0, t0);
5786 gen_store_fpr32(fp0, fs);
5787 tcg_temp_free_i32(fp0);
5792 gen_helper_1i(cfc1, t0, fs);
5793 gen_store_gpr(t0, rt);
5797 gen_load_gpr(t0, rt);
5798 gen_helper_1i(ctc1, t0, fs);
5801 #if defined(TARGET_MIPS64)
5803 gen_load_fpr64(ctx, t0, fs);
5804 gen_store_gpr(t0, rt);
5808 gen_load_gpr(t0, rt);
5809 gen_store_fpr64(ctx, t0, fs);
5815 TCGv_i32 fp0 = tcg_temp_new_i32();
5817 gen_load_fpr32h(fp0, fs);
5818 tcg_gen_ext_i32_tl(t0, fp0);
5819 tcg_temp_free_i32(fp0);
5821 gen_store_gpr(t0, rt);
5825 gen_load_gpr(t0, rt);
5827 TCGv_i32 fp0 = tcg_temp_new_i32();
5829 tcg_gen_trunc_tl_i32(fp0, t0);
5830 gen_store_fpr32h(fp0, fs);
5831 tcg_temp_free_i32(fp0);
5837 generate_exception (ctx, EXCP_RI);
5840 MIPS_DEBUG("%s %s %s", opn, regnames[rt], fregnames[fs]);
5846 static void gen_movci (DisasContext *ctx, int rd, int rs, int cc, int tf)
5862 l1 = gen_new_label();
5863 t0 = tcg_temp_new_i32();
5864 tcg_gen_andi_i32(t0, fpu_fcr31, 1 << get_fp_bit(cc));
5865 tcg_gen_brcondi_i32(cond, t0, 0, l1);
5866 tcg_temp_free_i32(t0);
5868 tcg_gen_movi_tl(cpu_gpr[rd], 0);
5870 tcg_gen_mov_tl(cpu_gpr[rd], cpu_gpr[rs]);
5875 static inline void gen_movcf_s (int fs, int fd, int cc, int tf)
5878 TCGv_i32 t0 = tcg_temp_new_i32();
5879 int l1 = gen_new_label();
5886 tcg_gen_andi_i32(t0, fpu_fcr31, 1 << get_fp_bit(cc));
5887 tcg_gen_brcondi_i32(cond, t0, 0, l1);
5888 gen_load_fpr32(t0, fs);
5889 gen_store_fpr32(t0, fd);
5891 tcg_temp_free_i32(t0);
5894 static inline void gen_movcf_d (DisasContext *ctx, int fs, int fd, int cc, int tf)
5897 TCGv_i32 t0 = tcg_temp_new_i32();
5899 int l1 = gen_new_label();
5906 tcg_gen_andi_i32(t0, fpu_fcr31, 1 << get_fp_bit(cc));
5907 tcg_gen_brcondi_i32(cond, t0, 0, l1);
5908 tcg_temp_free_i32(t0);
5909 fp0 = tcg_temp_new_i64();
5910 gen_load_fpr64(ctx, fp0, fs);
5911 gen_store_fpr64(ctx, fp0, fd);
5912 tcg_temp_free_i64(fp0);
5916 static inline void gen_movcf_ps (int fs, int fd, int cc, int tf)
5919 TCGv_i32 t0 = tcg_temp_new_i32();
5920 int l1 = gen_new_label();
5921 int l2 = gen_new_label();
5928 tcg_gen_andi_i32(t0, fpu_fcr31, 1 << get_fp_bit(cc));
5929 tcg_gen_brcondi_i32(cond, t0, 0, l1);
5930 gen_load_fpr32(t0, fs);
5931 gen_store_fpr32(t0, fd);
5934 tcg_gen_andi_i32(t0, fpu_fcr31, 1 << get_fp_bit(cc+1));
5935 tcg_gen_brcondi_i32(cond, t0, 0, l2);
5936 gen_load_fpr32h(t0, fs);
5937 gen_store_fpr32h(t0, fd);
5938 tcg_temp_free_i32(t0);
5943 static void gen_farith (DisasContext *ctx, uint32_t op1,
5944 int ft, int fs, int fd, int cc)
5946 const char *opn = "farith";
5947 const char *condnames[] = {
5965 const char *condnames_abs[] = {
5983 enum { BINOP, CMPOP, OTHEROP } optype = OTHEROP;
5984 uint32_t func = ctx->opcode & 0x3f;
5986 switch (ctx->opcode & FOP(0x3f, 0x1f)) {
5989 TCGv_i32 fp0 = tcg_temp_new_i32();
5990 TCGv_i32 fp1 = tcg_temp_new_i32();
5992 gen_load_fpr32(fp0, fs);
5993 gen_load_fpr32(fp1, ft);
5994 gen_helper_float_add_s(fp0, fp0, fp1);
5995 tcg_temp_free_i32(fp1);
5996 gen_store_fpr32(fp0, fd);
5997 tcg_temp_free_i32(fp0);
6004 TCGv_i32 fp0 = tcg_temp_new_i32();
6005 TCGv_i32 fp1 = tcg_temp_new_i32();
6007 gen_load_fpr32(fp0, fs);
6008 gen_load_fpr32(fp1, ft);
6009 gen_helper_float_sub_s(fp0, fp0, fp1);
6010 tcg_temp_free_i32(fp1);
6011 gen_store_fpr32(fp0, fd);
6012 tcg_temp_free_i32(fp0);
6019 TCGv_i32 fp0 = tcg_temp_new_i32();
6020 TCGv_i32 fp1 = tcg_temp_new_i32();
6022 gen_load_fpr32(fp0, fs);
6023 gen_load_fpr32(fp1, ft);
6024 gen_helper_float_mul_s(fp0, fp0, fp1);
6025 tcg_temp_free_i32(fp1);
6026 gen_store_fpr32(fp0, fd);
6027 tcg_temp_free_i32(fp0);
6034 TCGv_i32 fp0 = tcg_temp_new_i32();
6035 TCGv_i32 fp1 = tcg_temp_new_i32();
6037 gen_load_fpr32(fp0, fs);
6038 gen_load_fpr32(fp1, ft);
6039 gen_helper_float_div_s(fp0, fp0, fp1);
6040 tcg_temp_free_i32(fp1);
6041 gen_store_fpr32(fp0, fd);
6042 tcg_temp_free_i32(fp0);
6049 TCGv_i32 fp0 = tcg_temp_new_i32();
6051 gen_load_fpr32(fp0, fs);
6052 gen_helper_float_sqrt_s(fp0, fp0);
6053 gen_store_fpr32(fp0, fd);
6054 tcg_temp_free_i32(fp0);
6060 TCGv_i32 fp0 = tcg_temp_new_i32();
6062 gen_load_fpr32(fp0, fs);
6063 gen_helper_float_abs_s(fp0, fp0);
6064 gen_store_fpr32(fp0, fd);
6065 tcg_temp_free_i32(fp0);
6071 TCGv_i32 fp0 = tcg_temp_new_i32();
6073 gen_load_fpr32(fp0, fs);
6074 gen_store_fpr32(fp0, fd);
6075 tcg_temp_free_i32(fp0);
6081 TCGv_i32 fp0 = tcg_temp_new_i32();
6083 gen_load_fpr32(fp0, fs);
6084 gen_helper_float_chs_s(fp0, fp0);
6085 gen_store_fpr32(fp0, fd);
6086 tcg_temp_free_i32(fp0);
6091 check_cp1_64bitmode(ctx);
6093 TCGv_i32 fp32 = tcg_temp_new_i32();
6094 TCGv_i64 fp64 = tcg_temp_new_i64();
6096 gen_load_fpr32(fp32, fs);
6097 gen_helper_float_roundl_s(fp64, fp32);
6098 tcg_temp_free_i32(fp32);
6099 gen_store_fpr64(ctx, fp64, fd);
6100 tcg_temp_free_i64(fp64);
6105 check_cp1_64bitmode(ctx);
6107 TCGv_i32 fp32 = tcg_temp_new_i32();
6108 TCGv_i64 fp64 = tcg_temp_new_i64();
6110 gen_load_fpr32(fp32, fs);
6111 gen_helper_float_truncl_s(fp64, fp32);
6112 tcg_temp_free_i32(fp32);
6113 gen_store_fpr64(ctx, fp64, fd);
6114 tcg_temp_free_i64(fp64);
6119 check_cp1_64bitmode(ctx);
6121 TCGv_i32 fp32 = tcg_temp_new_i32();
6122 TCGv_i64 fp64 = tcg_temp_new_i64();
6124 gen_load_fpr32(fp32, fs);
6125 gen_helper_float_ceill_s(fp64, fp32);
6126 tcg_temp_free_i32(fp32);
6127 gen_store_fpr64(ctx, fp64, fd);
6128 tcg_temp_free_i64(fp64);
6133 check_cp1_64bitmode(ctx);
6135 TCGv_i32 fp32 = tcg_temp_new_i32();
6136 TCGv_i64 fp64 = tcg_temp_new_i64();
6138 gen_load_fpr32(fp32, fs);
6139 gen_helper_float_floorl_s(fp64, fp32);
6140 tcg_temp_free_i32(fp32);
6141 gen_store_fpr64(ctx, fp64, fd);
6142 tcg_temp_free_i64(fp64);
6148 TCGv_i32 fp0 = tcg_temp_new_i32();
6150 gen_load_fpr32(fp0, fs);
6151 gen_helper_float_roundw_s(fp0, fp0);
6152 gen_store_fpr32(fp0, fd);
6153 tcg_temp_free_i32(fp0);
6159 TCGv_i32 fp0 = tcg_temp_new_i32();
6161 gen_load_fpr32(fp0, fs);
6162 gen_helper_float_truncw_s(fp0, fp0);
6163 gen_store_fpr32(fp0, fd);
6164 tcg_temp_free_i32(fp0);
6170 TCGv_i32 fp0 = tcg_temp_new_i32();
6172 gen_load_fpr32(fp0, fs);
6173 gen_helper_float_ceilw_s(fp0, fp0);
6174 gen_store_fpr32(fp0, fd);
6175 tcg_temp_free_i32(fp0);
6181 TCGv_i32 fp0 = tcg_temp_new_i32();
6183 gen_load_fpr32(fp0, fs);
6184 gen_helper_float_floorw_s(fp0, fp0);
6185 gen_store_fpr32(fp0, fd);
6186 tcg_temp_free_i32(fp0);
6191 gen_movcf_s(fs, fd, (ft >> 2) & 0x7, ft & 0x1);
6196 int l1 = gen_new_label();
6200 tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[ft], 0, l1);
6202 fp0 = tcg_temp_new_i32();
6203 gen_load_fpr32(fp0, fs);
6204 gen_store_fpr32(fp0, fd);
6205 tcg_temp_free_i32(fp0);
6212 int l1 = gen_new_label();
6216 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[ft], 0, l1);
6217 fp0 = tcg_temp_new_i32();
6218 gen_load_fpr32(fp0, fs);
6219 gen_store_fpr32(fp0, fd);
6220 tcg_temp_free_i32(fp0);
6229 TCGv_i32 fp0 = tcg_temp_new_i32();
6231 gen_load_fpr32(fp0, fs);
6232 gen_helper_float_recip_s(fp0, fp0);
6233 gen_store_fpr32(fp0, fd);
6234 tcg_temp_free_i32(fp0);
6241 TCGv_i32 fp0 = tcg_temp_new_i32();
6243 gen_load_fpr32(fp0, fs);
6244 gen_helper_float_rsqrt_s(fp0, fp0);
6245 gen_store_fpr32(fp0, fd);
6246 tcg_temp_free_i32(fp0);
6251 check_cp1_64bitmode(ctx);
6253 TCGv_i32 fp0 = tcg_temp_new_i32();
6254 TCGv_i32 fp1 = tcg_temp_new_i32();
6256 gen_load_fpr32(fp0, fs);
6257 gen_load_fpr32(fp1, fd);
6258 gen_helper_float_recip2_s(fp0, fp0, fp1);
6259 tcg_temp_free_i32(fp1);
6260 gen_store_fpr32(fp0, fd);
6261 tcg_temp_free_i32(fp0);
6266 check_cp1_64bitmode(ctx);
6268 TCGv_i32 fp0 = tcg_temp_new_i32();
6270 gen_load_fpr32(fp0, fs);
6271 gen_helper_float_recip1_s(fp0, fp0);
6272 gen_store_fpr32(fp0, fd);
6273 tcg_temp_free_i32(fp0);
6278 check_cp1_64bitmode(ctx);
6280 TCGv_i32 fp0 = tcg_temp_new_i32();
6282 gen_load_fpr32(fp0, fs);
6283 gen_helper_float_rsqrt1_s(fp0, fp0);
6284 gen_store_fpr32(fp0, fd);
6285 tcg_temp_free_i32(fp0);
6290 check_cp1_64bitmode(ctx);
6292 TCGv_i32 fp0 = tcg_temp_new_i32();
6293 TCGv_i32 fp1 = tcg_temp_new_i32();
6295 gen_load_fpr32(fp0, fs);
6296 gen_load_fpr32(fp1, ft);
6297 gen_helper_float_rsqrt2_s(fp0, fp0, fp1);
6298 tcg_temp_free_i32(fp1);
6299 gen_store_fpr32(fp0, fd);
6300 tcg_temp_free_i32(fp0);
6305 check_cp1_registers(ctx, fd);
6307 TCGv_i32 fp32 = tcg_temp_new_i32();
6308 TCGv_i64 fp64 = tcg_temp_new_i64();
6310 gen_load_fpr32(fp32, fs);
6311 gen_helper_float_cvtd_s(fp64, fp32);
6312 tcg_temp_free_i32(fp32);
6313 gen_store_fpr64(ctx, fp64, fd);
6314 tcg_temp_free_i64(fp64);
6320 TCGv_i32 fp0 = tcg_temp_new_i32();
6322 gen_load_fpr32(fp0, fs);
6323 gen_helper_float_cvtw_s(fp0, fp0);
6324 gen_store_fpr32(fp0, fd);
6325 tcg_temp_free_i32(fp0);
6330 check_cp1_64bitmode(ctx);
6332 TCGv_i32 fp32 = tcg_temp_new_i32();
6333 TCGv_i64 fp64 = tcg_temp_new_i64();
6335 gen_load_fpr32(fp32, fs);
6336 gen_helper_float_cvtl_s(fp64, fp32);
6337 tcg_temp_free_i32(fp32);
6338 gen_store_fpr64(ctx, fp64, fd);
6339 tcg_temp_free_i64(fp64);
6344 check_cp1_64bitmode(ctx);
6346 TCGv_i64 fp64 = tcg_temp_new_i64();
6347 TCGv_i32 fp32_0 = tcg_temp_new_i32();
6348 TCGv_i32 fp32_1 = tcg_temp_new_i32();
6350 gen_load_fpr32(fp32_0, fs);
6351 gen_load_fpr32(fp32_1, ft);
6352 tcg_gen_concat_i32_i64(fp64, fp32_0, fp32_1);
6353 tcg_temp_free_i32(fp32_1);
6354 tcg_temp_free_i32(fp32_0);
6355 gen_store_fpr64(ctx, fp64, fd);
6356 tcg_temp_free_i64(fp64);
6377 TCGv_i32 fp0 = tcg_temp_new_i32();
6378 TCGv_i32 fp1 = tcg_temp_new_i32();
6380 gen_load_fpr32(fp0, fs);
6381 gen_load_fpr32(fp1, ft);
6382 if (ctx->opcode & (1 << 6)) {
6384 gen_cmpabs_s(func-48, fp0, fp1, cc);
6385 opn = condnames_abs[func-48];
6387 gen_cmp_s(func-48, fp0, fp1, cc);
6388 opn = condnames[func-48];
6390 tcg_temp_free_i32(fp0);
6391 tcg_temp_free_i32(fp1);
6395 check_cp1_registers(ctx, fs | ft | fd);
6397 TCGv_i64 fp0 = tcg_temp_new_i64();
6398 TCGv_i64 fp1 = tcg_temp_new_i64();
6400 gen_load_fpr64(ctx, fp0, fs);
6401 gen_load_fpr64(ctx, fp1, ft);
6402 gen_helper_float_add_d(fp0, fp0, fp1);
6403 tcg_temp_free_i64(fp1);
6404 gen_store_fpr64(ctx, fp0, fd);
6405 tcg_temp_free_i64(fp0);
6411 check_cp1_registers(ctx, fs | ft | fd);
6413 TCGv_i64 fp0 = tcg_temp_new_i64();
6414 TCGv_i64 fp1 = tcg_temp_new_i64();
6416 gen_load_fpr64(ctx, fp0, fs);
6417 gen_load_fpr64(ctx, fp1, ft);
6418 gen_helper_float_sub_d(fp0, fp0, fp1);
6419 tcg_temp_free_i64(fp1);
6420 gen_store_fpr64(ctx, fp0, fd);
6421 tcg_temp_free_i64(fp0);
6427 check_cp1_registers(ctx, fs | ft | fd);
6429 TCGv_i64 fp0 = tcg_temp_new_i64();
6430 TCGv_i64 fp1 = tcg_temp_new_i64();
6432 gen_load_fpr64(ctx, fp0, fs);
6433 gen_load_fpr64(ctx, fp1, ft);
6434 gen_helper_float_mul_d(fp0, fp0, fp1);
6435 tcg_temp_free_i64(fp1);
6436 gen_store_fpr64(ctx, fp0, fd);
6437 tcg_temp_free_i64(fp0);
6443 check_cp1_registers(ctx, fs | ft | fd);
6445 TCGv_i64 fp0 = tcg_temp_new_i64();
6446 TCGv_i64 fp1 = tcg_temp_new_i64();
6448 gen_load_fpr64(ctx, fp0, fs);
6449 gen_load_fpr64(ctx, fp1, ft);
6450 gen_helper_float_div_d(fp0, fp0, fp1);
6451 tcg_temp_free_i64(fp1);
6452 gen_store_fpr64(ctx, fp0, fd);
6453 tcg_temp_free_i64(fp0);
6459 check_cp1_registers(ctx, fs | fd);
6461 TCGv_i64 fp0 = tcg_temp_new_i64();
6463 gen_load_fpr64(ctx, fp0, fs);
6464 gen_helper_float_sqrt_d(fp0, fp0);
6465 gen_store_fpr64(ctx, fp0, fd);
6466 tcg_temp_free_i64(fp0);
6471 check_cp1_registers(ctx, fs | fd);
6473 TCGv_i64 fp0 = tcg_temp_new_i64();
6475 gen_load_fpr64(ctx, fp0, fs);
6476 gen_helper_float_abs_d(fp0, fp0);
6477 gen_store_fpr64(ctx, fp0, fd);
6478 tcg_temp_free_i64(fp0);
6483 check_cp1_registers(ctx, fs | fd);
6485 TCGv_i64 fp0 = tcg_temp_new_i64();
6487 gen_load_fpr64(ctx, fp0, fs);
6488 gen_store_fpr64(ctx, fp0, fd);
6489 tcg_temp_free_i64(fp0);
6494 check_cp1_registers(ctx, fs | fd);
6496 TCGv_i64 fp0 = tcg_temp_new_i64();
6498 gen_load_fpr64(ctx, fp0, fs);
6499 gen_helper_float_chs_d(fp0, fp0);
6500 gen_store_fpr64(ctx, fp0, fd);
6501 tcg_temp_free_i64(fp0);
6506 check_cp1_64bitmode(ctx);
6508 TCGv_i64 fp0 = tcg_temp_new_i64();
6510 gen_load_fpr64(ctx, fp0, fs);
6511 gen_helper_float_roundl_d(fp0, fp0);
6512 gen_store_fpr64(ctx, fp0, fd);
6513 tcg_temp_free_i64(fp0);
6518 check_cp1_64bitmode(ctx);
6520 TCGv_i64 fp0 = tcg_temp_new_i64();
6522 gen_load_fpr64(ctx, fp0, fs);
6523 gen_helper_float_truncl_d(fp0, fp0);
6524 gen_store_fpr64(ctx, fp0, fd);
6525 tcg_temp_free_i64(fp0);
6530 check_cp1_64bitmode(ctx);
6532 TCGv_i64 fp0 = tcg_temp_new_i64();
6534 gen_load_fpr64(ctx, fp0, fs);
6535 gen_helper_float_ceill_d(fp0, fp0);
6536 gen_store_fpr64(ctx, fp0, fd);
6537 tcg_temp_free_i64(fp0);
6542 check_cp1_64bitmode(ctx);
6544 TCGv_i64 fp0 = tcg_temp_new_i64();
6546 gen_load_fpr64(ctx, fp0, fs);
6547 gen_helper_float_floorl_d(fp0, fp0);
6548 gen_store_fpr64(ctx, fp0, fd);
6549 tcg_temp_free_i64(fp0);
6554 check_cp1_registers(ctx, fs);
6556 TCGv_i32 fp32 = tcg_temp_new_i32();
6557 TCGv_i64 fp64 = tcg_temp_new_i64();
6559 gen_load_fpr64(ctx, fp64, fs);
6560 gen_helper_float_roundw_d(fp32, fp64);
6561 tcg_temp_free_i64(fp64);
6562 gen_store_fpr32(fp32, fd);
6563 tcg_temp_free_i32(fp32);
6568 check_cp1_registers(ctx, fs);
6570 TCGv_i32 fp32 = tcg_temp_new_i32();
6571 TCGv_i64 fp64 = tcg_temp_new_i64();
6573 gen_load_fpr64(ctx, fp64, fs);
6574 gen_helper_float_truncw_d(fp32, fp64);
6575 tcg_temp_free_i64(fp64);
6576 gen_store_fpr32(fp32, fd);
6577 tcg_temp_free_i32(fp32);
6582 check_cp1_registers(ctx, fs);
6584 TCGv_i32 fp32 = tcg_temp_new_i32();
6585 TCGv_i64 fp64 = tcg_temp_new_i64();
6587 gen_load_fpr64(ctx, fp64, fs);
6588 gen_helper_float_ceilw_d(fp32, fp64);
6589 tcg_temp_free_i64(fp64);
6590 gen_store_fpr32(fp32, fd);
6591 tcg_temp_free_i32(fp32);
6596 check_cp1_registers(ctx, fs);
6598 TCGv_i32 fp32 = tcg_temp_new_i32();
6599 TCGv_i64 fp64 = tcg_temp_new_i64();
6601 gen_load_fpr64(ctx, fp64, fs);
6602 gen_helper_float_floorw_d(fp32, fp64);
6603 tcg_temp_free_i64(fp64);
6604 gen_store_fpr32(fp32, fd);
6605 tcg_temp_free_i32(fp32);
6610 gen_movcf_d(ctx, fs, fd, (ft >> 2) & 0x7, ft & 0x1);
6615 int l1 = gen_new_label();
6619 tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[ft], 0, l1);
6621 fp0 = tcg_temp_new_i64();
6622 gen_load_fpr64(ctx, fp0, fs);
6623 gen_store_fpr64(ctx, fp0, fd);
6624 tcg_temp_free_i64(fp0);
6631 int l1 = gen_new_label();
6635 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[ft], 0, l1);
6636 fp0 = tcg_temp_new_i64();
6637 gen_load_fpr64(ctx, fp0, fs);
6638 gen_store_fpr64(ctx, fp0, fd);
6639 tcg_temp_free_i64(fp0);
6646 check_cp1_64bitmode(ctx);
6648 TCGv_i64 fp0 = tcg_temp_new_i64();
6650 gen_load_fpr64(ctx, fp0, fs);
6651 gen_helper_float_recip_d(fp0, fp0);
6652 gen_store_fpr64(ctx, fp0, fd);
6653 tcg_temp_free_i64(fp0);
6658 check_cp1_64bitmode(ctx);
6660 TCGv_i64 fp0 = tcg_temp_new_i64();
6662 gen_load_fpr64(ctx, fp0, fs);
6663 gen_helper_float_rsqrt_d(fp0, fp0);
6664 gen_store_fpr64(ctx, fp0, fd);
6665 tcg_temp_free_i64(fp0);
6670 check_cp1_64bitmode(ctx);
6672 TCGv_i64 fp0 = tcg_temp_new_i64();
6673 TCGv_i64 fp1 = tcg_temp_new_i64();
6675 gen_load_fpr64(ctx, fp0, fs);
6676 gen_load_fpr64(ctx, fp1, ft);
6677 gen_helper_float_recip2_d(fp0, fp0, fp1);
6678 tcg_temp_free_i64(fp1);
6679 gen_store_fpr64(ctx, fp0, fd);
6680 tcg_temp_free_i64(fp0);
6685 check_cp1_64bitmode(ctx);
6687 TCGv_i64 fp0 = tcg_temp_new_i64();
6689 gen_load_fpr64(ctx, fp0, fs);
6690 gen_helper_float_recip1_d(fp0, fp0);
6691 gen_store_fpr64(ctx, fp0, fd);
6692 tcg_temp_free_i64(fp0);
6697 check_cp1_64bitmode(ctx);
6699 TCGv_i64 fp0 = tcg_temp_new_i64();
6701 gen_load_fpr64(ctx, fp0, fs);
6702 gen_helper_float_rsqrt1_d(fp0, fp0);
6703 gen_store_fpr64(ctx, fp0, fd);
6704 tcg_temp_free_i64(fp0);
6709 check_cp1_64bitmode(ctx);
6711 TCGv_i64 fp0 = tcg_temp_new_i64();
6712 TCGv_i64 fp1 = tcg_temp_new_i64();
6714 gen_load_fpr64(ctx, fp0, fs);
6715 gen_load_fpr64(ctx, fp1, ft);
6716 gen_helper_float_rsqrt2_d(fp0, fp0, fp1);
6717 tcg_temp_free_i64(fp1);
6718 gen_store_fpr64(ctx, fp0, fd);
6719 tcg_temp_free_i64(fp0);
6740 TCGv_i64 fp0 = tcg_temp_new_i64();
6741 TCGv_i64 fp1 = tcg_temp_new_i64();
6743 gen_load_fpr64(ctx, fp0, fs);
6744 gen_load_fpr64(ctx, fp1, ft);
6745 if (ctx->opcode & (1 << 6)) {
6747 check_cp1_registers(ctx, fs | ft);
6748 gen_cmpabs_d(func-48, fp0, fp1, cc);
6749 opn = condnames_abs[func-48];
6751 check_cp1_registers(ctx, fs | ft);
6752 gen_cmp_d(func-48, fp0, fp1, cc);
6753 opn = condnames[func-48];
6755 tcg_temp_free_i64(fp0);
6756 tcg_temp_free_i64(fp1);
6760 check_cp1_registers(ctx, fs);
6762 TCGv_i32 fp32 = tcg_temp_new_i32();
6763 TCGv_i64 fp64 = tcg_temp_new_i64();
6765 gen_load_fpr64(ctx, fp64, fs);
6766 gen_helper_float_cvts_d(fp32, fp64);
6767 tcg_temp_free_i64(fp64);
6768 gen_store_fpr32(fp32, fd);
6769 tcg_temp_free_i32(fp32);
6774 check_cp1_registers(ctx, fs);
6776 TCGv_i32 fp32 = tcg_temp_new_i32();
6777 TCGv_i64 fp64 = tcg_temp_new_i64();
6779 gen_load_fpr64(ctx, fp64, fs);
6780 gen_helper_float_cvtw_d(fp32, fp64);
6781 tcg_temp_free_i64(fp64);
6782 gen_store_fpr32(fp32, fd);
6783 tcg_temp_free_i32(fp32);
6788 check_cp1_64bitmode(ctx);
6790 TCGv_i64 fp0 = tcg_temp_new_i64();
6792 gen_load_fpr64(ctx, fp0, fs);
6793 gen_helper_float_cvtl_d(fp0, fp0);
6794 gen_store_fpr64(ctx, fp0, fd);
6795 tcg_temp_free_i64(fp0);
6801 TCGv_i32 fp0 = tcg_temp_new_i32();
6803 gen_load_fpr32(fp0, fs);
6804 gen_helper_float_cvts_w(fp0, fp0);
6805 gen_store_fpr32(fp0, fd);
6806 tcg_temp_free_i32(fp0);
6811 check_cp1_registers(ctx, fd);
6813 TCGv_i32 fp32 = tcg_temp_new_i32();
6814 TCGv_i64 fp64 = tcg_temp_new_i64();
6816 gen_load_fpr32(fp32, fs);
6817 gen_helper_float_cvtd_w(fp64, fp32);
6818 tcg_temp_free_i32(fp32);
6819 gen_store_fpr64(ctx, fp64, fd);
6820 tcg_temp_free_i64(fp64);
6825 check_cp1_64bitmode(ctx);
6827 TCGv_i32 fp32 = tcg_temp_new_i32();
6828 TCGv_i64 fp64 = tcg_temp_new_i64();
6830 gen_load_fpr64(ctx, fp64, fs);
6831 gen_helper_float_cvts_l(fp32, fp64);
6832 tcg_temp_free_i64(fp64);
6833 gen_store_fpr32(fp32, fd);
6834 tcg_temp_free_i32(fp32);
6839 check_cp1_64bitmode(ctx);
6841 TCGv_i64 fp0 = tcg_temp_new_i64();
6843 gen_load_fpr64(ctx, fp0, fs);
6844 gen_helper_float_cvtd_l(fp0, fp0);
6845 gen_store_fpr64(ctx, fp0, fd);
6846 tcg_temp_free_i64(fp0);
6851 check_cp1_64bitmode(ctx);
6853 TCGv_i64 fp0 = tcg_temp_new_i64();
6855 gen_load_fpr64(ctx, fp0, fs);
6856 gen_helper_float_cvtps_pw(fp0, fp0);
6857 gen_store_fpr64(ctx, fp0, fd);
6858 tcg_temp_free_i64(fp0);
6863 check_cp1_64bitmode(ctx);
6865 TCGv_i64 fp0 = tcg_temp_new_i64();
6866 TCGv_i64 fp1 = tcg_temp_new_i64();
6868 gen_load_fpr64(ctx, fp0, fs);
6869 gen_load_fpr64(ctx, fp1, ft);
6870 gen_helper_float_add_ps(fp0, fp0, fp1);
6871 tcg_temp_free_i64(fp1);
6872 gen_store_fpr64(ctx, fp0, fd);
6873 tcg_temp_free_i64(fp0);
6878 check_cp1_64bitmode(ctx);
6880 TCGv_i64 fp0 = tcg_temp_new_i64();
6881 TCGv_i64 fp1 = tcg_temp_new_i64();
6883 gen_load_fpr64(ctx, fp0, fs);
6884 gen_load_fpr64(ctx, fp1, ft);
6885 gen_helper_float_sub_ps(fp0, fp0, fp1);
6886 tcg_temp_free_i64(fp1);
6887 gen_store_fpr64(ctx, fp0, fd);
6888 tcg_temp_free_i64(fp0);
6893 check_cp1_64bitmode(ctx);
6895 TCGv_i64 fp0 = tcg_temp_new_i64();
6896 TCGv_i64 fp1 = tcg_temp_new_i64();
6898 gen_load_fpr64(ctx, fp0, fs);
6899 gen_load_fpr64(ctx, fp1, ft);
6900 gen_helper_float_mul_ps(fp0, fp0, fp1);
6901 tcg_temp_free_i64(fp1);
6902 gen_store_fpr64(ctx, fp0, fd);
6903 tcg_temp_free_i64(fp0);
6908 check_cp1_64bitmode(ctx);
6910 TCGv_i64 fp0 = tcg_temp_new_i64();
6912 gen_load_fpr64(ctx, fp0, fs);
6913 gen_helper_float_abs_ps(fp0, fp0);
6914 gen_store_fpr64(ctx, fp0, fd);
6915 tcg_temp_free_i64(fp0);
6920 check_cp1_64bitmode(ctx);
6922 TCGv_i64 fp0 = tcg_temp_new_i64();
6924 gen_load_fpr64(ctx, fp0, fs);
6925 gen_store_fpr64(ctx, fp0, fd);
6926 tcg_temp_free_i64(fp0);
6931 check_cp1_64bitmode(ctx);
6933 TCGv_i64 fp0 = tcg_temp_new_i64();
6935 gen_load_fpr64(ctx, fp0, fs);
6936 gen_helper_float_chs_ps(fp0, fp0);
6937 gen_store_fpr64(ctx, fp0, fd);
6938 tcg_temp_free_i64(fp0);
6943 check_cp1_64bitmode(ctx);
6944 gen_movcf_ps(fs, fd, (ft >> 2) & 0x7, ft & 0x1);
6948 check_cp1_64bitmode(ctx);
6950 int l1 = gen_new_label();
6954 tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[ft], 0, l1);
6955 fp0 = tcg_temp_new_i64();
6956 gen_load_fpr64(ctx, fp0, fs);
6957 gen_store_fpr64(ctx, fp0, fd);
6958 tcg_temp_free_i64(fp0);
6964 check_cp1_64bitmode(ctx);
6966 int l1 = gen_new_label();
6970 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[ft], 0, l1);
6971 fp0 = tcg_temp_new_i64();
6972 gen_load_fpr64(ctx, fp0, fs);
6973 gen_store_fpr64(ctx, fp0, fd);
6974 tcg_temp_free_i64(fp0);
6981 check_cp1_64bitmode(ctx);
6983 TCGv_i64 fp0 = tcg_temp_new_i64();
6984 TCGv_i64 fp1 = tcg_temp_new_i64();
6986 gen_load_fpr64(ctx, fp0, ft);
6987 gen_load_fpr64(ctx, fp1, fs);
6988 gen_helper_float_addr_ps(fp0, fp0, fp1);
6989 tcg_temp_free_i64(fp1);
6990 gen_store_fpr64(ctx, fp0, fd);
6991 tcg_temp_free_i64(fp0);
6996 check_cp1_64bitmode(ctx);
6998 TCGv_i64 fp0 = tcg_temp_new_i64();
6999 TCGv_i64 fp1 = tcg_temp_new_i64();
7001 gen_load_fpr64(ctx, fp0, ft);
7002 gen_load_fpr64(ctx, fp1, fs);
7003 gen_helper_float_mulr_ps(fp0, fp0, fp1);
7004 tcg_temp_free_i64(fp1);
7005 gen_store_fpr64(ctx, fp0, fd);
7006 tcg_temp_free_i64(fp0);
7011 check_cp1_64bitmode(ctx);
7013 TCGv_i64 fp0 = tcg_temp_new_i64();
7014 TCGv_i64 fp1 = tcg_temp_new_i64();
7016 gen_load_fpr64(ctx, fp0, fs);
7017 gen_load_fpr64(ctx, fp1, fd);
7018 gen_helper_float_recip2_ps(fp0, fp0, fp1);
7019 tcg_temp_free_i64(fp1);
7020 gen_store_fpr64(ctx, fp0, fd);
7021 tcg_temp_free_i64(fp0);
7026 check_cp1_64bitmode(ctx);
7028 TCGv_i64 fp0 = tcg_temp_new_i64();
7030 gen_load_fpr64(ctx, fp0, fs);
7031 gen_helper_float_recip1_ps(fp0, fp0);
7032 gen_store_fpr64(ctx, fp0, fd);
7033 tcg_temp_free_i64(fp0);
7038 check_cp1_64bitmode(ctx);
7040 TCGv_i64 fp0 = tcg_temp_new_i64();
7042 gen_load_fpr64(ctx, fp0, fs);
7043 gen_helper_float_rsqrt1_ps(fp0, fp0);
7044 gen_store_fpr64(ctx, fp0, fd);
7045 tcg_temp_free_i64(fp0);
7050 check_cp1_64bitmode(ctx);
7052 TCGv_i64 fp0 = tcg_temp_new_i64();
7053 TCGv_i64 fp1 = tcg_temp_new_i64();
7055 gen_load_fpr64(ctx, fp0, fs);
7056 gen_load_fpr64(ctx, fp1, ft);
7057 gen_helper_float_rsqrt2_ps(fp0, fp0, fp1);
7058 tcg_temp_free_i64(fp1);
7059 gen_store_fpr64(ctx, fp0, fd);
7060 tcg_temp_free_i64(fp0);
7065 check_cp1_64bitmode(ctx);
7067 TCGv_i32 fp0 = tcg_temp_new_i32();
7069 gen_load_fpr32h(fp0, fs);
7070 gen_helper_float_cvts_pu(fp0, fp0);
7071 gen_store_fpr32(fp0, fd);
7072 tcg_temp_free_i32(fp0);
7077 check_cp1_64bitmode(ctx);
7079 TCGv_i64 fp0 = tcg_temp_new_i64();
7081 gen_load_fpr64(ctx, fp0, fs);
7082 gen_helper_float_cvtpw_ps(fp0, fp0);
7083 gen_store_fpr64(ctx, fp0, fd);
7084 tcg_temp_free_i64(fp0);
7089 check_cp1_64bitmode(ctx);
7091 TCGv_i32 fp0 = tcg_temp_new_i32();
7093 gen_load_fpr32(fp0, fs);
7094 gen_helper_float_cvts_pl(fp0, fp0);
7095 gen_store_fpr32(fp0, fd);
7096 tcg_temp_free_i32(fp0);
7101 check_cp1_64bitmode(ctx);
7103 TCGv_i32 fp0 = tcg_temp_new_i32();
7104 TCGv_i32 fp1 = tcg_temp_new_i32();
7106 gen_load_fpr32(fp0, fs);
7107 gen_load_fpr32(fp1, ft);
7108 gen_store_fpr32h(fp0, fd);
7109 gen_store_fpr32(fp1, fd);
7110 tcg_temp_free_i32(fp0);
7111 tcg_temp_free_i32(fp1);
7116 check_cp1_64bitmode(ctx);
7118 TCGv_i32 fp0 = tcg_temp_new_i32();
7119 TCGv_i32 fp1 = tcg_temp_new_i32();
7121 gen_load_fpr32(fp0, fs);
7122 gen_load_fpr32h(fp1, ft);
7123 gen_store_fpr32(fp1, fd);
7124 gen_store_fpr32h(fp0, fd);
7125 tcg_temp_free_i32(fp0);
7126 tcg_temp_free_i32(fp1);
7131 check_cp1_64bitmode(ctx);
7133 TCGv_i32 fp0 = tcg_temp_new_i32();
7134 TCGv_i32 fp1 = tcg_temp_new_i32();
7136 gen_load_fpr32h(fp0, fs);
7137 gen_load_fpr32(fp1, ft);
7138 gen_store_fpr32(fp1, fd);
7139 gen_store_fpr32h(fp0, fd);
7140 tcg_temp_free_i32(fp0);
7141 tcg_temp_free_i32(fp1);
7146 check_cp1_64bitmode(ctx);
7148 TCGv_i32 fp0 = tcg_temp_new_i32();
7149 TCGv_i32 fp1 = tcg_temp_new_i32();
7151 gen_load_fpr32h(fp0, fs);
7152 gen_load_fpr32h(fp1, ft);
7153 gen_store_fpr32(fp1, fd);
7154 gen_store_fpr32h(fp0, fd);
7155 tcg_temp_free_i32(fp0);
7156 tcg_temp_free_i32(fp1);
7176 check_cp1_64bitmode(ctx);
7178 TCGv_i64 fp0 = tcg_temp_new_i64();
7179 TCGv_i64 fp1 = tcg_temp_new_i64();
7181 gen_load_fpr64(ctx, fp0, fs);
7182 gen_load_fpr64(ctx, fp1, ft);
7183 if (ctx->opcode & (1 << 6)) {
7184 gen_cmpabs_ps(func-48, fp0, fp1, cc);
7185 opn = condnames_abs[func-48];
7187 gen_cmp_ps(func-48, fp0, fp1, cc);
7188 opn = condnames[func-48];
7190 tcg_temp_free_i64(fp0);
7191 tcg_temp_free_i64(fp1);
7196 generate_exception (ctx, EXCP_RI);
7201 MIPS_DEBUG("%s %s, %s, %s", opn, fregnames[fd], fregnames[fs], fregnames[ft]);
7204 MIPS_DEBUG("%s %s,%s", opn, fregnames[fs], fregnames[ft]);
7207 MIPS_DEBUG("%s %s,%s", opn, fregnames[fd], fregnames[fs]);
7212 /* Coprocessor 3 (FPU) */
7213 static void gen_flt3_ldst (DisasContext *ctx, uint32_t opc,
7214 int fd, int fs, int base, int index)
7216 const char *opn = "extended float load/store";
7218 TCGv t0 = tcg_temp_new();
7221 gen_load_gpr(t0, index);
7222 } else if (index == 0) {
7223 gen_load_gpr(t0, base);
7225 gen_load_gpr(t0, index);
7226 gen_op_addr_add(ctx, t0, cpu_gpr[base], t0);
7228 /* Don't do NOP if destination is zero: we must perform the actual
7230 save_cpu_state(ctx, 0);
7235 TCGv_i32 fp0 = tcg_temp_new_i32();
7237 tcg_gen_qemu_ld32s(t0, t0, ctx->mem_idx);
7238 tcg_gen_trunc_tl_i32(fp0, t0);
7239 gen_store_fpr32(fp0, fd);
7240 tcg_temp_free_i32(fp0);
7246 check_cp1_registers(ctx, fd);
7248 TCGv_i64 fp0 = tcg_temp_new_i64();
7250 tcg_gen_qemu_ld64(fp0, t0, ctx->mem_idx);
7251 gen_store_fpr64(ctx, fp0, fd);
7252 tcg_temp_free_i64(fp0);
7257 check_cp1_64bitmode(ctx);
7258 tcg_gen_andi_tl(t0, t0, ~0x7);
7260 TCGv_i64 fp0 = tcg_temp_new_i64();
7262 tcg_gen_qemu_ld64(fp0, t0, ctx->mem_idx);
7263 gen_store_fpr64(ctx, fp0, fd);
7264 tcg_temp_free_i64(fp0);
7271 TCGv_i32 fp0 = tcg_temp_new_i32();
7272 TCGv t1 = tcg_temp_new();
7274 gen_load_fpr32(fp0, fs);
7275 tcg_gen_extu_i32_tl(t1, fp0);
7276 tcg_gen_qemu_st32(t1, t0, ctx->mem_idx);
7277 tcg_temp_free_i32(fp0);
7285 check_cp1_registers(ctx, fs);
7287 TCGv_i64 fp0 = tcg_temp_new_i64();
7289 gen_load_fpr64(ctx, fp0, fs);
7290 tcg_gen_qemu_st64(fp0, t0, ctx->mem_idx);
7291 tcg_temp_free_i64(fp0);
7297 check_cp1_64bitmode(ctx);
7298 tcg_gen_andi_tl(t0, t0, ~0x7);
7300 TCGv_i64 fp0 = tcg_temp_new_i64();
7302 gen_load_fpr64(ctx, fp0, fs);
7303 tcg_gen_qemu_st64(fp0, t0, ctx->mem_idx);
7304 tcg_temp_free_i64(fp0);
7311 MIPS_DEBUG("%s %s, %s(%s)", opn, fregnames[store ? fs : fd],
7312 regnames[index], regnames[base]);
7315 static void gen_flt3_arith (DisasContext *ctx, uint32_t opc,
7316 int fd, int fr, int fs, int ft)
7318 const char *opn = "flt3_arith";
7322 check_cp1_64bitmode(ctx);
7324 TCGv t0 = tcg_temp_local_new();
7325 TCGv_i32 fp = tcg_temp_new_i32();
7326 TCGv_i32 fph = tcg_temp_new_i32();
7327 int l1 = gen_new_label();
7328 int l2 = gen_new_label();
7330 gen_load_gpr(t0, fr);
7331 tcg_gen_andi_tl(t0, t0, 0x7);
7333 tcg_gen_brcondi_tl(TCG_COND_NE, t0, 0, l1);
7334 gen_load_fpr32(fp, fs);
7335 gen_load_fpr32h(fph, fs);
7336 gen_store_fpr32(fp, fd);
7337 gen_store_fpr32h(fph, fd);
7340 tcg_gen_brcondi_tl(TCG_COND_NE, t0, 4, l2);
7342 #ifdef TARGET_WORDS_BIGENDIAN
7343 gen_load_fpr32(fp, fs);
7344 gen_load_fpr32h(fph, ft);
7345 gen_store_fpr32h(fp, fd);
7346 gen_store_fpr32(fph, fd);
7348 gen_load_fpr32h(fph, fs);
7349 gen_load_fpr32(fp, ft);
7350 gen_store_fpr32(fph, fd);
7351 gen_store_fpr32h(fp, fd);
7354 tcg_temp_free_i32(fp);
7355 tcg_temp_free_i32(fph);
7362 TCGv_i32 fp0 = tcg_temp_new_i32();
7363 TCGv_i32 fp1 = tcg_temp_new_i32();
7364 TCGv_i32 fp2 = tcg_temp_new_i32();
7366 gen_load_fpr32(fp0, fs);
7367 gen_load_fpr32(fp1, ft);
7368 gen_load_fpr32(fp2, fr);
7369 gen_helper_float_muladd_s(fp2, fp0, fp1, fp2);
7370 tcg_temp_free_i32(fp0);
7371 tcg_temp_free_i32(fp1);
7372 gen_store_fpr32(fp2, fd);
7373 tcg_temp_free_i32(fp2);
7379 check_cp1_registers(ctx, fd | fs | ft | fr);
7381 TCGv_i64 fp0 = tcg_temp_new_i64();
7382 TCGv_i64 fp1 = tcg_temp_new_i64();
7383 TCGv_i64 fp2 = tcg_temp_new_i64();
7385 gen_load_fpr64(ctx, fp0, fs);
7386 gen_load_fpr64(ctx, fp1, ft);
7387 gen_load_fpr64(ctx, fp2, fr);
7388 gen_helper_float_muladd_d(fp2, fp0, fp1, fp2);
7389 tcg_temp_free_i64(fp0);
7390 tcg_temp_free_i64(fp1);
7391 gen_store_fpr64(ctx, fp2, fd);
7392 tcg_temp_free_i64(fp2);
7397 check_cp1_64bitmode(ctx);
7399 TCGv_i64 fp0 = tcg_temp_new_i64();
7400 TCGv_i64 fp1 = tcg_temp_new_i64();
7401 TCGv_i64 fp2 = tcg_temp_new_i64();
7403 gen_load_fpr64(ctx, fp0, fs);
7404 gen_load_fpr64(ctx, fp1, ft);
7405 gen_load_fpr64(ctx, fp2, fr);
7406 gen_helper_float_muladd_ps(fp2, fp0, fp1, fp2);
7407 tcg_temp_free_i64(fp0);
7408 tcg_temp_free_i64(fp1);
7409 gen_store_fpr64(ctx, fp2, fd);
7410 tcg_temp_free_i64(fp2);
7417 TCGv_i32 fp0 = tcg_temp_new_i32();
7418 TCGv_i32 fp1 = tcg_temp_new_i32();
7419 TCGv_i32 fp2 = tcg_temp_new_i32();
7421 gen_load_fpr32(fp0, fs);
7422 gen_load_fpr32(fp1, ft);
7423 gen_load_fpr32(fp2, fr);
7424 gen_helper_float_mulsub_s(fp2, fp0, fp1, fp2);
7425 tcg_temp_free_i32(fp0);
7426 tcg_temp_free_i32(fp1);
7427 gen_store_fpr32(fp2, fd);
7428 tcg_temp_free_i32(fp2);
7434 check_cp1_registers(ctx, fd | fs | ft | fr);
7436 TCGv_i64 fp0 = tcg_temp_new_i64();
7437 TCGv_i64 fp1 = tcg_temp_new_i64();
7438 TCGv_i64 fp2 = tcg_temp_new_i64();
7440 gen_load_fpr64(ctx, fp0, fs);
7441 gen_load_fpr64(ctx, fp1, ft);
7442 gen_load_fpr64(ctx, fp2, fr);
7443 gen_helper_float_mulsub_d(fp2, fp0, fp1, fp2);
7444 tcg_temp_free_i64(fp0);
7445 tcg_temp_free_i64(fp1);
7446 gen_store_fpr64(ctx, fp2, fd);
7447 tcg_temp_free_i64(fp2);
7452 check_cp1_64bitmode(ctx);
7454 TCGv_i64 fp0 = tcg_temp_new_i64();
7455 TCGv_i64 fp1 = tcg_temp_new_i64();
7456 TCGv_i64 fp2 = tcg_temp_new_i64();
7458 gen_load_fpr64(ctx, fp0, fs);
7459 gen_load_fpr64(ctx, fp1, ft);
7460 gen_load_fpr64(ctx, fp2, fr);
7461 gen_helper_float_mulsub_ps(fp2, fp0, fp1, fp2);
7462 tcg_temp_free_i64(fp0);
7463 tcg_temp_free_i64(fp1);
7464 gen_store_fpr64(ctx, fp2, fd);
7465 tcg_temp_free_i64(fp2);
7472 TCGv_i32 fp0 = tcg_temp_new_i32();
7473 TCGv_i32 fp1 = tcg_temp_new_i32();
7474 TCGv_i32 fp2 = tcg_temp_new_i32();
7476 gen_load_fpr32(fp0, fs);
7477 gen_load_fpr32(fp1, ft);
7478 gen_load_fpr32(fp2, fr);
7479 gen_helper_float_nmuladd_s(fp2, fp0, fp1, fp2);
7480 tcg_temp_free_i32(fp0);
7481 tcg_temp_free_i32(fp1);
7482 gen_store_fpr32(fp2, fd);
7483 tcg_temp_free_i32(fp2);
7489 check_cp1_registers(ctx, fd | fs | ft | fr);
7491 TCGv_i64 fp0 = tcg_temp_new_i64();
7492 TCGv_i64 fp1 = tcg_temp_new_i64();
7493 TCGv_i64 fp2 = tcg_temp_new_i64();
7495 gen_load_fpr64(ctx, fp0, fs);
7496 gen_load_fpr64(ctx, fp1, ft);
7497 gen_load_fpr64(ctx, fp2, fr);
7498 gen_helper_float_nmuladd_d(fp2, fp0, fp1, fp2);
7499 tcg_temp_free_i64(fp0);
7500 tcg_temp_free_i64(fp1);
7501 gen_store_fpr64(ctx, fp2, fd);
7502 tcg_temp_free_i64(fp2);
7507 check_cp1_64bitmode(ctx);
7509 TCGv_i64 fp0 = tcg_temp_new_i64();
7510 TCGv_i64 fp1 = tcg_temp_new_i64();
7511 TCGv_i64 fp2 = tcg_temp_new_i64();
7513 gen_load_fpr64(ctx, fp0, fs);
7514 gen_load_fpr64(ctx, fp1, ft);
7515 gen_load_fpr64(ctx, fp2, fr);
7516 gen_helper_float_nmuladd_ps(fp2, fp0, fp1, fp2);
7517 tcg_temp_free_i64(fp0);
7518 tcg_temp_free_i64(fp1);
7519 gen_store_fpr64(ctx, fp2, fd);
7520 tcg_temp_free_i64(fp2);
7527 TCGv_i32 fp0 = tcg_temp_new_i32();
7528 TCGv_i32 fp1 = tcg_temp_new_i32();
7529 TCGv_i32 fp2 = tcg_temp_new_i32();
7531 gen_load_fpr32(fp0, fs);
7532 gen_load_fpr32(fp1, ft);
7533 gen_load_fpr32(fp2, fr);
7534 gen_helper_float_nmulsub_s(fp2, fp0, fp1, fp2);
7535 tcg_temp_free_i32(fp0);
7536 tcg_temp_free_i32(fp1);
7537 gen_store_fpr32(fp2, fd);
7538 tcg_temp_free_i32(fp2);
7544 check_cp1_registers(ctx, fd | fs | ft | fr);
7546 TCGv_i64 fp0 = tcg_temp_new_i64();
7547 TCGv_i64 fp1 = tcg_temp_new_i64();
7548 TCGv_i64 fp2 = tcg_temp_new_i64();
7550 gen_load_fpr64(ctx, fp0, fs);
7551 gen_load_fpr64(ctx, fp1, ft);
7552 gen_load_fpr64(ctx, fp2, fr);
7553 gen_helper_float_nmulsub_d(fp2, fp0, fp1, fp2);
7554 tcg_temp_free_i64(fp0);
7555 tcg_temp_free_i64(fp1);
7556 gen_store_fpr64(ctx, fp2, fd);
7557 tcg_temp_free_i64(fp2);
7562 check_cp1_64bitmode(ctx);
7564 TCGv_i64 fp0 = tcg_temp_new_i64();
7565 TCGv_i64 fp1 = tcg_temp_new_i64();
7566 TCGv_i64 fp2 = tcg_temp_new_i64();
7568 gen_load_fpr64(ctx, fp0, fs);
7569 gen_load_fpr64(ctx, fp1, ft);
7570 gen_load_fpr64(ctx, fp2, fr);
7571 gen_helper_float_nmulsub_ps(fp2, fp0, fp1, fp2);
7572 tcg_temp_free_i64(fp0);
7573 tcg_temp_free_i64(fp1);
7574 gen_store_fpr64(ctx, fp2, fd);
7575 tcg_temp_free_i64(fp2);
7581 generate_exception (ctx, EXCP_RI);
7584 MIPS_DEBUG("%s %s, %s, %s, %s", opn, fregnames[fd], fregnames[fr],
7585 fregnames[fs], fregnames[ft]);
7588 static void handle_delay_slot (CPUState *env, DisasContext *ctx,
7591 if (ctx->hflags & MIPS_HFLAG_BMASK) {
7592 int proc_hflags = ctx->hflags & MIPS_HFLAG_BMASK;
7593 /* Branches completion */
7594 ctx->hflags &= ~MIPS_HFLAG_BMASK;
7595 ctx->bstate = BS_BRANCH;
7596 save_cpu_state(ctx, 0);
7597 /* FIXME: Need to clear can_do_io. */
7598 switch (proc_hflags & MIPS_HFLAG_BMASK_BASE) {
7600 /* unconditional branch */
7601 MIPS_DEBUG("unconditional branch");
7602 if (proc_hflags & MIPS_HFLAG_BX) {
7603 tcg_gen_xori_i32(hflags, hflags, MIPS_HFLAG_M16);
7605 gen_goto_tb(ctx, 0, ctx->btarget);
7608 /* blikely taken case */
7609 MIPS_DEBUG("blikely branch taken");
7610 gen_goto_tb(ctx, 0, ctx->btarget);
7613 /* Conditional branch */
7614 MIPS_DEBUG("conditional branch");
7616 int l1 = gen_new_label();
7618 tcg_gen_brcondi_tl(TCG_COND_NE, bcond, 0, l1);
7619 gen_goto_tb(ctx, 1, ctx->pc + insn_bytes);
7621 gen_goto_tb(ctx, 0, ctx->btarget);
7625 /* unconditional branch to register */
7626 MIPS_DEBUG("branch to register");
7627 if (env->insn_flags & ASE_MIPS16) {
7628 TCGv t0 = tcg_temp_new();
7629 TCGv_i32 t1 = tcg_temp_new_i32();
7631 tcg_gen_andi_tl(t0, btarget, 0x1);
7632 tcg_gen_trunc_tl_i32(t1, t0);
7634 tcg_gen_andi_i32(hflags, hflags, ~(uint32_t)MIPS_HFLAG_M16);
7635 tcg_gen_shli_i32(t1, t1, MIPS_HFLAG_M16_SHIFT);
7636 tcg_gen_or_i32(hflags, hflags, t1);
7637 tcg_temp_free_i32(t1);
7639 tcg_gen_andi_tl(cpu_PC, btarget, ~(target_ulong)0x1);
7641 tcg_gen_mov_tl(cpu_PC, btarget);
7643 if (ctx->singlestep_enabled) {
7644 save_cpu_state(ctx, 0);
7645 gen_helper_0i(raise_exception, EXCP_DEBUG);
7650 MIPS_DEBUG("unknown branch");
7656 /* ISA extensions (ASEs) */
7657 /* MIPS16 extension to MIPS32 */
7659 /* MIPS16 major opcodes */
7661 M16_OPC_ADDIUSP = 0x00,
7662 M16_OPC_ADDIUPC = 0x01,
7665 M16_OPC_BEQZ = 0x04,
7666 M16_OPC_BNEQZ = 0x05,
7667 M16_OPC_SHIFT = 0x06,
7669 M16_OPC_RRIA = 0x08,
7670 M16_OPC_ADDIU8 = 0x09,
7671 M16_OPC_SLTI = 0x0a,
7672 M16_OPC_SLTIU = 0x0b,
7675 M16_OPC_CMPI = 0x0e,
7679 M16_OPC_LWSP = 0x12,
7683 M16_OPC_LWPC = 0x16,
7687 M16_OPC_SWSP = 0x1a,
7691 M16_OPC_EXTEND = 0x1e,
7695 /* I8 funct field */
7714 /* RR funct field */
7748 /* I64 funct field */
7760 /* RR ry field for CNVT */
7762 RR_RY_CNVT_ZEB = 0x0,
7763 RR_RY_CNVT_ZEH = 0x1,
7764 RR_RY_CNVT_ZEW = 0x2,
7765 RR_RY_CNVT_SEB = 0x4,
7766 RR_RY_CNVT_SEH = 0x5,
7767 RR_RY_CNVT_SEW = 0x6,
7770 static int xlat (int r)
7772 static int map[] = { 16, 17, 2, 3, 4, 5, 6, 7 };
7777 static void gen_mips16_save (DisasContext *ctx,
7778 int xsregs, int aregs,
7779 int do_ra, int do_s0, int do_s1,
7782 TCGv t0 = tcg_temp_new();
7783 TCGv t1 = tcg_temp_new();
7813 generate_exception(ctx, EXCP_RI);
7819 gen_base_offset_addr(ctx, t0, 29, 12);
7820 gen_load_gpr(t1, 7);
7821 op_ldst_sw(t1, t0, ctx);
7824 gen_base_offset_addr(ctx, t0, 29, 8);
7825 gen_load_gpr(t1, 6);
7826 op_ldst_sw(t1, t0, ctx);
7829 gen_base_offset_addr(ctx, t0, 29, 4);
7830 gen_load_gpr(t1, 5);
7831 op_ldst_sw(t1, t0, ctx);
7834 gen_base_offset_addr(ctx, t0, 29, 0);
7835 gen_load_gpr(t1, 4);
7836 op_ldst_sw(t1, t0, ctx);
7839 gen_load_gpr(t0, 29);
7841 #define DECR_AND_STORE(reg) do { \
7842 tcg_gen_subi_tl(t0, t0, 4); \
7843 gen_load_gpr(t1, reg); \
7844 op_ldst_sw(t1, t0, ctx); \
7908 generate_exception(ctx, EXCP_RI);
7924 #undef DECR_AND_STORE
7926 tcg_gen_subi_tl(cpu_gpr[29], cpu_gpr[29], framesize);
7931 static void gen_mips16_restore (DisasContext *ctx,
7932 int xsregs, int aregs,
7933 int do_ra, int do_s0, int do_s1,
7937 TCGv t0 = tcg_temp_new();
7938 TCGv t1 = tcg_temp_new();
7940 tcg_gen_addi_tl(t0, cpu_gpr[29], framesize);
7942 #define DECR_AND_LOAD(reg) do { \
7943 tcg_gen_subi_tl(t0, t0, 4); \
7944 op_ldst_lw(t1, t0, ctx); \
7945 gen_store_gpr(t1, reg); \
8009 generate_exception(ctx, EXCP_RI);
8025 #undef DECR_AND_LOAD
8027 tcg_gen_addi_tl(cpu_gpr[29], cpu_gpr[29], framesize);
8032 static void gen_addiupc (DisasContext *ctx, int rx, int imm,
8033 int is_64_bit, int extended)
8037 if (extended && (ctx->hflags & MIPS_HFLAG_BMASK)) {
8038 generate_exception(ctx, EXCP_RI);
8042 t0 = tcg_temp_new();
8044 tcg_gen_movi_tl(t0, pc_relative_pc(ctx));
8045 tcg_gen_addi_tl(cpu_gpr[rx], t0, imm);
8047 tcg_gen_ext32s_tl(cpu_gpr[rx], cpu_gpr[rx]);
8053 #if defined(TARGET_MIPS64)
8054 static void decode_i64_mips16 (CPUState *env, DisasContext *ctx,
8055 int ry, int funct, int16_t offset,
8061 offset = extended ? offset : offset << 3;
8062 gen_ldst(ctx, OPC_LD, ry, 29, offset);
8066 offset = extended ? offset : offset << 3;
8067 gen_ldst(ctx, OPC_SD, ry, 29, offset);
8071 offset = extended ? offset : (ctx->opcode & 0xff) << 3;
8072 gen_ldst(ctx, OPC_SD, 31, 29, offset);
8076 offset = extended ? offset : ((int8_t)ctx->opcode) << 3;
8077 gen_arith_imm(env, ctx, OPC_DADDIU, 29, 29, offset);
8080 if (extended && (ctx->hflags & MIPS_HFLAG_BMASK)) {
8081 generate_exception(ctx, EXCP_RI);
8083 offset = extended ? offset : offset << 3;
8084 gen_ldst(ctx, OPC_LDPC, ry, 0, offset);
8089 offset = extended ? offset : ((int8_t)(offset << 3)) >> 3;
8090 gen_arith_imm(env, ctx, OPC_DADDIU, ry, ry, offset);
8094 offset = extended ? offset : offset << 2;
8095 gen_addiupc(ctx, ry, offset, 1, extended);
8099 offset = extended ? offset : offset << 2;
8100 gen_arith_imm(env, ctx, OPC_DADDIU, ry, 29, offset);
8106 static int decode_extended_mips16_opc (CPUState *env, DisasContext *ctx,
8109 int extend = lduw_code(ctx->pc + 2);
8110 int op, rx, ry, funct, sa;
8111 int16_t imm, offset;
8113 ctx->opcode = (ctx->opcode << 16) | extend;
8114 op = (ctx->opcode >> 11) & 0x1f;
8115 sa = (ctx->opcode >> 22) & 0x1f;
8116 funct = (ctx->opcode >> 8) & 0x7;
8117 rx = xlat((ctx->opcode >> 8) & 0x7);
8118 ry = xlat((ctx->opcode >> 5) & 0x7);
8119 offset = imm = (int16_t) (((ctx->opcode >> 16) & 0x1f) << 11
8120 | ((ctx->opcode >> 21) & 0x3f) << 5
8121 | (ctx->opcode & 0x1f));
8123 /* The extended opcodes cleverly reuse the opcodes from their 16-bit
8126 case M16_OPC_ADDIUSP:
8127 gen_arith_imm(env, ctx, OPC_ADDIU, rx, 29, imm);
8129 case M16_OPC_ADDIUPC:
8130 gen_addiupc(ctx, rx, imm, 0, 1);
8133 gen_compute_branch(ctx, OPC_BEQ, 4, 0, 0, offset << 1);
8134 /* No delay slot, so just process as a normal instruction */
8137 gen_compute_branch(ctx, OPC_BEQ, 4, rx, 0, offset << 1);
8138 /* No delay slot, so just process as a normal instruction */
8141 gen_compute_branch(ctx, OPC_BNE, 4, rx, 0, offset << 1);
8142 /* No delay slot, so just process as a normal instruction */
8145 switch (ctx->opcode & 0x3) {
8147 gen_shift_imm(env, ctx, OPC_SLL, rx, ry, sa);
8150 #if defined(TARGET_MIPS64)
8152 gen_shift_imm(env, ctx, OPC_DSLL, rx, ry, sa);
8154 generate_exception(ctx, EXCP_RI);
8158 gen_shift_imm(env, ctx, OPC_SRL, rx, ry, sa);
8161 gen_shift_imm(env, ctx, OPC_SRA, rx, ry, sa);
8165 #if defined(TARGET_MIPS64)
8168 gen_ldst(ctx, OPC_LD, ry, rx, offset);
8172 imm = ctx->opcode & 0xf;
8173 imm = imm | ((ctx->opcode >> 20) & 0x7f) << 4;
8174 imm = imm | ((ctx->opcode >> 16) & 0xf) << 11;
8175 imm = (int16_t) (imm << 1) >> 1;
8176 if ((ctx->opcode >> 4) & 0x1) {
8177 #if defined(TARGET_MIPS64)
8179 gen_arith_imm(env, ctx, OPC_DADDIU, ry, rx, imm);
8181 generate_exception(ctx, EXCP_RI);
8184 gen_arith_imm(env, ctx, OPC_ADDIU, ry, rx, imm);
8187 case M16_OPC_ADDIU8:
8188 gen_arith_imm(env, ctx, OPC_ADDIU, rx, rx, imm);
8191 gen_slt_imm(env, OPC_SLTI, 24, rx, imm);
8194 gen_slt_imm(env, OPC_SLTIU, 24, rx, imm);
8199 gen_compute_branch(ctx, OPC_BEQ, 4, 24, 0, offset << 1);
8202 gen_compute_branch(ctx, OPC_BNE, 4, 24, 0, offset << 1);
8205 gen_ldst(ctx, OPC_SW, 31, 29, imm);
8208 gen_arith_imm(env, ctx, OPC_ADDIU, 29, 29, imm);
8212 int xsregs = (ctx->opcode >> 24) & 0x7;
8213 int aregs = (ctx->opcode >> 16) & 0xf;
8214 int do_ra = (ctx->opcode >> 6) & 0x1;
8215 int do_s0 = (ctx->opcode >> 5) & 0x1;
8216 int do_s1 = (ctx->opcode >> 4) & 0x1;
8217 int framesize = (((ctx->opcode >> 20) & 0xf) << 4
8218 | (ctx->opcode & 0xf)) << 3;
8220 if (ctx->opcode & (1 << 7)) {
8221 gen_mips16_save(ctx, xsregs, aregs,
8222 do_ra, do_s0, do_s1,
8225 gen_mips16_restore(ctx, xsregs, aregs,
8226 do_ra, do_s0, do_s1,
8232 generate_exception(ctx, EXCP_RI);
8237 tcg_gen_movi_tl(cpu_gpr[rx], (uint16_t) imm);
8240 tcg_gen_xori_tl(cpu_gpr[24], cpu_gpr[rx], (uint16_t) imm);
8242 #if defined(TARGET_MIPS64)
8244 gen_ldst(ctx, OPC_SD, ry, rx, offset);
8248 gen_ldst(ctx, OPC_LB, ry, rx, offset);
8251 gen_ldst(ctx, OPC_LH, ry, rx, offset);
8254 gen_ldst(ctx, OPC_LW, rx, 29, offset);
8257 gen_ldst(ctx, OPC_LW, ry, rx, offset);
8260 gen_ldst(ctx, OPC_LBU, ry, rx, offset);
8263 gen_ldst(ctx, OPC_LHU, ry, rx, offset);
8266 gen_ldst(ctx, OPC_LWPC, rx, 0, offset);
8268 #if defined(TARGET_MIPS64)
8270 gen_ldst(ctx, OPC_LWU, ry, rx, offset);
8274 gen_ldst(ctx, OPC_SB, ry, rx, offset);
8277 gen_ldst(ctx, OPC_SH, ry, rx, offset);
8280 gen_ldst(ctx, OPC_SW, rx, 29, offset);
8283 gen_ldst(ctx, OPC_SW, ry, rx, offset);
8285 #if defined(TARGET_MIPS64)
8287 decode_i64_mips16(env, ctx, ry, funct, offset, 1);
8291 generate_exception(ctx, EXCP_RI);
8298 static int decode_mips16_opc (CPUState *env, DisasContext *ctx,
8303 int op, cnvt_op, op1, offset;
8307 op = (ctx->opcode >> 11) & 0x1f;
8308 sa = (ctx->opcode >> 2) & 0x7;
8309 sa = sa == 0 ? 8 : sa;
8310 rx = xlat((ctx->opcode >> 8) & 0x7);
8311 cnvt_op = (ctx->opcode >> 5) & 0x7;
8312 ry = xlat((ctx->opcode >> 5) & 0x7);
8313 op1 = offset = ctx->opcode & 0x1f;
8318 case M16_OPC_ADDIUSP:
8320 int16_t imm = ((uint8_t) ctx->opcode) << 2;
8322 gen_arith_imm(env, ctx, OPC_ADDIU, rx, 29, imm);
8325 case M16_OPC_ADDIUPC:
8326 gen_addiupc(ctx, rx, ((uint8_t) ctx->opcode) << 2, 0, 0);
8329 offset = (ctx->opcode & 0x7ff) << 1;
8330 offset = (int16_t)(offset << 4) >> 4;
8331 gen_compute_branch(ctx, OPC_BEQ, 2, 0, 0, offset);
8332 /* No delay slot, so just process as a normal instruction */
8335 offset = lduw_code(ctx->pc + 2);
8336 offset = (((ctx->opcode & 0x1f) << 21)
8337 | ((ctx->opcode >> 5) & 0x1f) << 16
8339 op = ((ctx->opcode >> 10) & 0x1) ? OPC_JALX : OPC_JAL;
8340 gen_compute_branch(ctx, op, 4, rx, ry, offset);
8345 gen_compute_branch(ctx, OPC_BEQ, 2, rx, 0, ((int8_t)ctx->opcode) << 1);
8346 /* No delay slot, so just process as a normal instruction */
8349 gen_compute_branch(ctx, OPC_BNE, 2, rx, 0, ((int8_t)ctx->opcode) << 1);
8350 /* No delay slot, so just process as a normal instruction */
8353 switch (ctx->opcode & 0x3) {
8355 gen_shift_imm(env, ctx, OPC_SLL, rx, ry, sa);
8358 #if defined(TARGET_MIPS64)
8360 gen_shift_imm(env, ctx, OPC_DSLL, rx, ry, sa);
8362 generate_exception(ctx, EXCP_RI);
8366 gen_shift_imm(env, ctx, OPC_SRL, rx, ry, sa);
8369 gen_shift_imm(env, ctx, OPC_SRA, rx, ry, sa);
8373 #if defined(TARGET_MIPS64)
8376 gen_ldst(ctx, OPC_LD, ry, rx, offset << 3);
8381 int16_t imm = (int8_t)((ctx->opcode & 0xf) << 4) >> 4;
8383 if ((ctx->opcode >> 4) & 1) {
8384 #if defined(TARGET_MIPS64)
8386 gen_arith_imm(env, ctx, OPC_DADDIU, ry, rx, imm);
8388 generate_exception(ctx, EXCP_RI);
8391 gen_arith_imm(env, ctx, OPC_ADDIU, ry, rx, imm);
8395 case M16_OPC_ADDIU8:
8397 int16_t imm = (int8_t) ctx->opcode;
8399 gen_arith_imm(env, ctx, OPC_ADDIU, rx, rx, imm);
8404 int16_t imm = (uint8_t) ctx->opcode;
8406 gen_slt_imm(env, OPC_SLTI, 24, rx, imm);
8411 int16_t imm = (uint8_t) ctx->opcode;
8413 gen_slt_imm(env, OPC_SLTIU, 24, rx, imm);
8420 funct = (ctx->opcode >> 8) & 0x7;
8423 gen_compute_branch(ctx, OPC_BEQ, 2, 24, 0,
8424 ((int8_t)ctx->opcode) << 1);
8427 gen_compute_branch(ctx, OPC_BNE, 2, 24, 0,
8428 ((int8_t)ctx->opcode) << 1);
8431 gen_ldst(ctx, OPC_SW, 31, 29, (ctx->opcode & 0xff) << 2);
8434 gen_arith_imm(env, ctx, OPC_ADDIU, 29, 29,
8435 ((int8_t)ctx->opcode) << 3);
8439 int do_ra = ctx->opcode & (1 << 6);
8440 int do_s0 = ctx->opcode & (1 << 5);
8441 int do_s1 = ctx->opcode & (1 << 4);
8442 int framesize = ctx->opcode & 0xf;
8444 if (framesize == 0) {
8447 framesize = framesize << 3;
8450 if (ctx->opcode & (1 << 7)) {
8451 gen_mips16_save(ctx, 0, 0,
8452 do_ra, do_s0, do_s1, framesize);
8454 gen_mips16_restore(ctx, 0, 0,
8455 do_ra, do_s0, do_s1, framesize);
8461 int rz = xlat(ctx->opcode & 0x7);
8463 reg32 = (((ctx->opcode >> 3) & 0x3) << 3) |
8464 ((ctx->opcode >> 5) & 0x7);
8465 gen_arith(env, ctx, OPC_ADDU, reg32, rz, 0);
8469 reg32 = ctx->opcode & 0x1f;
8470 gen_arith(env, ctx, OPC_ADDU, ry, reg32, 0);
8473 generate_exception(ctx, EXCP_RI);
8480 int16_t imm = (uint8_t) ctx->opcode;
8482 gen_arith_imm(env, ctx, OPC_ADDIU, rx, 0, imm);
8487 int16_t imm = (uint8_t) ctx->opcode;
8489 gen_logic_imm(env, OPC_XORI, 24, rx, imm);
8492 #if defined(TARGET_MIPS64)
8495 gen_ldst(ctx, OPC_SD, ry, rx, offset << 3);
8499 gen_ldst(ctx, OPC_LB, ry, rx, offset);
8502 gen_ldst(ctx, OPC_LH, ry, rx, offset << 1);
8505 gen_ldst(ctx, OPC_LW, rx, 29, ((uint8_t)ctx->opcode) << 2);
8508 gen_ldst(ctx, OPC_LW, ry, rx, offset << 2);
8511 gen_ldst(ctx, OPC_LBU, ry, rx, offset);
8514 gen_ldst(ctx, OPC_LHU, ry, rx, offset << 1);
8517 gen_ldst(ctx, OPC_LWPC, rx, 0, ((uint8_t)ctx->opcode) << 2);
8519 #if defined (TARGET_MIPS64)
8522 gen_ldst(ctx, OPC_LWU, ry, rx, offset << 2);
8526 gen_ldst(ctx, OPC_SB, ry, rx, offset);
8529 gen_ldst(ctx, OPC_SH, ry, rx, offset << 1);
8532 gen_ldst(ctx, OPC_SW, rx, 29, ((uint8_t)ctx->opcode) << 2);
8535 gen_ldst(ctx, OPC_SW, ry, rx, offset << 2);
8539 int rz = xlat((ctx->opcode >> 2) & 0x7);
8542 switch (ctx->opcode & 0x3) {
8544 mips32_op = OPC_ADDU;
8547 mips32_op = OPC_SUBU;
8549 #if defined(TARGET_MIPS64)
8551 mips32_op = OPC_DADDU;
8555 mips32_op = OPC_DSUBU;
8560 generate_exception(ctx, EXCP_RI);
8564 gen_arith(env, ctx, mips32_op, rz, rx, ry);
8573 int nd = (ctx->opcode >> 7) & 0x1;
8574 int link = (ctx->opcode >> 6) & 0x1;
8575 int ra = (ctx->opcode >> 5) & 0x1;
8578 op = nd ? OPC_JALRC : OPC_JALR;
8583 gen_compute_branch(ctx, op, 2, ra ? 31 : rx, 31, 0);
8590 /* XXX: not clear which exception should be raised
8591 * when in debug mode...
8593 check_insn(env, ctx, ISA_MIPS32);
8594 if (!(ctx->hflags & MIPS_HFLAG_DM)) {
8595 generate_exception(ctx, EXCP_DBp);
8597 generate_exception(ctx, EXCP_DBp);
8601 gen_slt(env, OPC_SLT, 24, rx, ry);
8604 gen_slt(env, OPC_SLTU, 24, rx, ry);
8607 generate_exception(ctx, EXCP_BREAK);
8610 gen_shift(env, ctx, OPC_SLLV, ry, rx, ry);
8613 gen_shift(env, ctx, OPC_SRLV, ry, rx, ry);
8616 gen_shift(env, ctx, OPC_SRAV, ry, rx, ry);
8618 #if defined (TARGET_MIPS64)
8621 gen_shift_imm(env, ctx, OPC_DSRL, ry, ry, sa);
8625 gen_logic(env, OPC_XOR, 24, rx, ry);
8628 gen_arith(env, ctx, OPC_SUBU, rx, 0, ry);
8631 gen_logic(env, OPC_AND, rx, rx, ry);
8634 gen_logic(env, OPC_OR, rx, rx, ry);
8637 gen_logic(env, OPC_XOR, rx, rx, ry);
8640 gen_logic(env, OPC_NOR, rx, ry, 0);
8643 gen_HILO(ctx, OPC_MFHI, rx);
8647 case RR_RY_CNVT_ZEB:
8648 tcg_gen_ext8u_tl(cpu_gpr[rx], cpu_gpr[rx]);
8650 case RR_RY_CNVT_ZEH:
8651 tcg_gen_ext16u_tl(cpu_gpr[rx], cpu_gpr[rx]);
8653 case RR_RY_CNVT_SEB:
8654 tcg_gen_ext8s_tl(cpu_gpr[rx], cpu_gpr[rx]);
8656 case RR_RY_CNVT_SEH:
8657 tcg_gen_ext16s_tl(cpu_gpr[rx], cpu_gpr[rx]);
8659 #if defined (TARGET_MIPS64)
8660 case RR_RY_CNVT_ZEW:
8662 tcg_gen_ext32u_tl(cpu_gpr[rx], cpu_gpr[rx]);
8664 case RR_RY_CNVT_SEW:
8666 tcg_gen_ext32s_tl(cpu_gpr[rx], cpu_gpr[rx]);
8670 generate_exception(ctx, EXCP_RI);
8675 gen_HILO(ctx, OPC_MFLO, rx);
8677 #if defined (TARGET_MIPS64)
8680 gen_shift_imm(env, ctx, OPC_DSRA, ry, ry, sa);
8684 gen_shift(env, ctx, OPC_DSLLV, ry, rx, ry);
8688 gen_shift(env, ctx, OPC_DSRLV, ry, rx, ry);
8692 gen_shift(env, ctx, OPC_DSRAV, ry, rx, ry);
8696 gen_muldiv(ctx, OPC_MULT, rx, ry);
8699 gen_muldiv(ctx, OPC_MULTU, rx, ry);
8702 gen_muldiv(ctx, OPC_DIV, rx, ry);
8705 gen_muldiv(ctx, OPC_DIVU, rx, ry);
8707 #if defined (TARGET_MIPS64)
8710 gen_muldiv(ctx, OPC_DMULT, rx, ry);
8714 gen_muldiv(ctx, OPC_DMULTU, rx, ry);
8718 gen_muldiv(ctx, OPC_DDIV, rx, ry);
8722 gen_muldiv(ctx, OPC_DDIVU, rx, ry);
8726 generate_exception(ctx, EXCP_RI);
8730 case M16_OPC_EXTEND:
8731 decode_extended_mips16_opc(env, ctx, is_branch);
8734 #if defined(TARGET_MIPS64)
8736 funct = (ctx->opcode >> 8) & 0x7;
8737 decode_i64_mips16(env, ctx, ry, funct, offset, 0);
8741 generate_exception(ctx, EXCP_RI);
8748 /* SmartMIPS extension to MIPS32 */
8750 #if defined(TARGET_MIPS64)
8752 /* MDMX extension to MIPS64 */
8756 static void decode_opc (CPUState *env, DisasContext *ctx, int *is_branch)
8760 uint32_t op, op1, op2;
8763 /* make sure instructions are on a word boundary */
8764 if (ctx->pc & 0x3) {
8765 env->CP0_BadVAddr = ctx->pc;
8766 generate_exception(ctx, EXCP_AdEL);
8770 /* Handle blikely not taken case */
8771 if ((ctx->hflags & MIPS_HFLAG_BMASK_BASE) == MIPS_HFLAG_BL) {
8772 int l1 = gen_new_label();
8774 MIPS_DEBUG("blikely condition (" TARGET_FMT_lx ")", ctx->pc + 4);
8775 tcg_gen_brcondi_tl(TCG_COND_NE, bcond, 0, l1);
8776 tcg_gen_movi_i32(hflags, ctx->hflags & ~MIPS_HFLAG_BMASK);
8777 gen_goto_tb(ctx, 1, ctx->pc + 4);
8781 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP)))
8782 tcg_gen_debug_insn_start(ctx->pc);
8784 op = MASK_OP_MAJOR(ctx->opcode);
8785 rs = (ctx->opcode >> 21) & 0x1f;
8786 rt = (ctx->opcode >> 16) & 0x1f;
8787 rd = (ctx->opcode >> 11) & 0x1f;
8788 sa = (ctx->opcode >> 6) & 0x1f;
8789 imm = (int16_t)ctx->opcode;
8792 op1 = MASK_SPECIAL(ctx->opcode);
8794 case OPC_SLL: /* Shift with immediate */
8796 gen_shift_imm(env, ctx, op1, rd, rt, sa);
8799 switch ((ctx->opcode >> 21) & 0x1f) {
8801 /* rotr is decoded as srl on non-R2 CPUs */
8802 if (env->insn_flags & ISA_MIPS32R2) {
8807 gen_shift_imm(env, ctx, op1, rd, rt, sa);
8810 generate_exception(ctx, EXCP_RI);
8814 case OPC_MOVN: /* Conditional move */
8816 check_insn(env, ctx, ISA_MIPS4 | ISA_MIPS32);
8817 gen_cond_move(env, op1, rd, rs, rt);
8819 case OPC_ADD ... OPC_SUBU:
8820 gen_arith(env, ctx, op1, rd, rs, rt);
8822 case OPC_SLLV: /* Shifts */
8824 gen_shift(env, ctx, op1, rd, rs, rt);
8827 switch ((ctx->opcode >> 6) & 0x1f) {
8829 /* rotrv is decoded as srlv on non-R2 CPUs */
8830 if (env->insn_flags & ISA_MIPS32R2) {
8835 gen_shift(env, ctx, op1, rd, rs, rt);
8838 generate_exception(ctx, EXCP_RI);
8842 case OPC_SLT: /* Set on less than */
8844 gen_slt(env, op1, rd, rs, rt);
8846 case OPC_AND: /* Logic*/
8850 gen_logic(env, op1, rd, rs, rt);
8852 case OPC_MULT ... OPC_DIVU:
8854 check_insn(env, ctx, INSN_VR54XX);
8855 op1 = MASK_MUL_VR54XX(ctx->opcode);
8856 gen_mul_vr54xx(ctx, op1, rd, rs, rt);
8858 gen_muldiv(ctx, op1, rs, rt);
8860 case OPC_JR ... OPC_JALR:
8861 gen_compute_branch(ctx, op1, 4, rs, rd, sa);
8864 case OPC_TGE ... OPC_TEQ: /* Traps */
8866 gen_trap(ctx, op1, rs, rt, -1);
8868 case OPC_MFHI: /* Move from HI/LO */
8870 gen_HILO(ctx, op1, rd);
8873 case OPC_MTLO: /* Move to HI/LO */
8874 gen_HILO(ctx, op1, rs);
8876 case OPC_PMON: /* Pmon entry point, also R4010 selsl */
8877 #ifdef MIPS_STRICT_STANDARD
8878 MIPS_INVAL("PMON / selsl");
8879 generate_exception(ctx, EXCP_RI);
8881 gen_helper_0i(pmon, sa);
8885 generate_exception(ctx, EXCP_SYSCALL);
8886 ctx->bstate = BS_STOP;
8889 generate_exception(ctx, EXCP_BREAK);
8892 #ifdef MIPS_STRICT_STANDARD
8894 generate_exception(ctx, EXCP_RI);
8896 /* Implemented as RI exception for now. */
8897 MIPS_INVAL("spim (unofficial)");
8898 generate_exception(ctx, EXCP_RI);
8906 check_insn(env, ctx, ISA_MIPS4 | ISA_MIPS32);
8907 if (env->CP0_Config1 & (1 << CP0C1_FP)) {
8908 check_cp1_enabled(ctx);
8909 gen_movci(ctx, rd, rs, (ctx->opcode >> 18) & 0x7,
8910 (ctx->opcode >> 16) & 1);
8912 generate_exception_err(ctx, EXCP_CpU, 1);
8916 #if defined(TARGET_MIPS64)
8917 /* MIPS64 specific opcodes */
8922 check_insn(env, ctx, ISA_MIPS3);
8924 gen_shift_imm(env, ctx, op1, rd, rt, sa);
8927 switch ((ctx->opcode >> 21) & 0x1f) {
8929 /* drotr is decoded as dsrl on non-R2 CPUs */
8930 if (env->insn_flags & ISA_MIPS32R2) {
8935 check_insn(env, ctx, ISA_MIPS3);
8937 gen_shift_imm(env, ctx, op1, rd, rt, sa);
8940 generate_exception(ctx, EXCP_RI);
8945 switch ((ctx->opcode >> 21) & 0x1f) {
8947 /* drotr32 is decoded as dsrl32 on non-R2 CPUs */
8948 if (env->insn_flags & ISA_MIPS32R2) {
8953 check_insn(env, ctx, ISA_MIPS3);
8955 gen_shift_imm(env, ctx, op1, rd, rt, sa);
8958 generate_exception(ctx, EXCP_RI);
8962 case OPC_DADD ... OPC_DSUBU:
8963 check_insn(env, ctx, ISA_MIPS3);
8965 gen_arith(env, ctx, op1, rd, rs, rt);
8969 check_insn(env, ctx, ISA_MIPS3);
8971 gen_shift(env, ctx, op1, rd, rs, rt);
8974 switch ((ctx->opcode >> 6) & 0x1f) {
8976 /* drotrv is decoded as dsrlv on non-R2 CPUs */
8977 if (env->insn_flags & ISA_MIPS32R2) {
8982 check_insn(env, ctx, ISA_MIPS3);
8984 gen_shift(env, ctx, op1, rd, rs, rt);
8987 generate_exception(ctx, EXCP_RI);
8991 case OPC_DMULT ... OPC_DDIVU:
8992 check_insn(env, ctx, ISA_MIPS3);
8994 gen_muldiv(ctx, op1, rs, rt);
8997 default: /* Invalid */
8998 MIPS_INVAL("special");
8999 generate_exception(ctx, EXCP_RI);
9004 op1 = MASK_SPECIAL2(ctx->opcode);
9006 case OPC_MADD ... OPC_MADDU: /* Multiply and add/sub */
9007 case OPC_MSUB ... OPC_MSUBU:
9008 check_insn(env, ctx, ISA_MIPS32);
9009 gen_muldiv(ctx, op1, rs, rt);
9012 gen_arith(env, ctx, op1, rd, rs, rt);
9016 check_insn(env, ctx, ISA_MIPS32);
9017 gen_cl(ctx, op1, rd, rs);
9020 /* XXX: not clear which exception should be raised
9021 * when in debug mode...
9023 check_insn(env, ctx, ISA_MIPS32);
9024 if (!(ctx->hflags & MIPS_HFLAG_DM)) {
9025 generate_exception(ctx, EXCP_DBp);
9027 generate_exception(ctx, EXCP_DBp);
9031 #if defined(TARGET_MIPS64)
9034 check_insn(env, ctx, ISA_MIPS64);
9036 gen_cl(ctx, op1, rd, rs);
9039 default: /* Invalid */
9040 MIPS_INVAL("special2");
9041 generate_exception(ctx, EXCP_RI);
9046 op1 = MASK_SPECIAL3(ctx->opcode);
9050 check_insn(env, ctx, ISA_MIPS32R2);
9051 gen_bitops(ctx, op1, rt, rs, sa, rd);
9054 check_insn(env, ctx, ISA_MIPS32R2);
9055 op2 = MASK_BSHFL(ctx->opcode);
9056 gen_bshfl(ctx, op2, rt, rd);
9059 check_insn(env, ctx, ISA_MIPS32R2);
9061 TCGv t0 = tcg_temp_new();
9065 save_cpu_state(ctx, 1);
9066 gen_helper_rdhwr_cpunum(t0);
9067 gen_store_gpr(t0, rt);
9070 save_cpu_state(ctx, 1);
9071 gen_helper_rdhwr_synci_step(t0);
9072 gen_store_gpr(t0, rt);
9075 save_cpu_state(ctx, 1);
9076 gen_helper_rdhwr_cc(t0);
9077 gen_store_gpr(t0, rt);
9080 save_cpu_state(ctx, 1);
9081 gen_helper_rdhwr_ccres(t0);
9082 gen_store_gpr(t0, rt);
9085 #if defined(CONFIG_USER_ONLY)
9086 tcg_gen_ld_tl(t0, cpu_env, offsetof(CPUState, tls_value));
9087 gen_store_gpr(t0, rt);
9090 /* XXX: Some CPUs implement this in hardware.
9091 Not supported yet. */
9093 default: /* Invalid */
9094 MIPS_INVAL("rdhwr");
9095 generate_exception(ctx, EXCP_RI);
9102 check_insn(env, ctx, ASE_MT);
9104 TCGv t0 = tcg_temp_new();
9105 TCGv t1 = tcg_temp_new();
9107 gen_load_gpr(t0, rt);
9108 gen_load_gpr(t1, rs);
9109 gen_helper_fork(t0, t1);
9115 check_insn(env, ctx, ASE_MT);
9117 TCGv t0 = tcg_temp_new();
9119 save_cpu_state(ctx, 1);
9120 gen_load_gpr(t0, rs);
9121 gen_helper_yield(t0, t0);
9122 gen_store_gpr(t0, rd);
9126 #if defined(TARGET_MIPS64)
9127 case OPC_DEXTM ... OPC_DEXT:
9128 case OPC_DINSM ... OPC_DINS:
9129 check_insn(env, ctx, ISA_MIPS64R2);
9131 gen_bitops(ctx, op1, rt, rs, sa, rd);
9134 check_insn(env, ctx, ISA_MIPS64R2);
9136 op2 = MASK_DBSHFL(ctx->opcode);
9137 gen_bshfl(ctx, op2, rt, rd);
9140 default: /* Invalid */
9141 MIPS_INVAL("special3");
9142 generate_exception(ctx, EXCP_RI);
9147 op1 = MASK_REGIMM(ctx->opcode);
9149 case OPC_BLTZ ... OPC_BGEZL: /* REGIMM branches */
9150 case OPC_BLTZAL ... OPC_BGEZALL:
9151 gen_compute_branch(ctx, op1, 4, rs, -1, imm << 2);
9154 case OPC_TGEI ... OPC_TEQI: /* REGIMM traps */
9156 gen_trap(ctx, op1, rs, -1, imm);
9159 check_insn(env, ctx, ISA_MIPS32R2);
9162 default: /* Invalid */
9163 MIPS_INVAL("regimm");
9164 generate_exception(ctx, EXCP_RI);
9169 check_cp0_enabled(ctx);
9170 op1 = MASK_CP0(ctx->opcode);
9176 #if defined(TARGET_MIPS64)
9180 #ifndef CONFIG_USER_ONLY
9181 gen_cp0(env, ctx, op1, rt, rd);
9182 #endif /* !CONFIG_USER_ONLY */
9184 case OPC_C0_FIRST ... OPC_C0_LAST:
9185 #ifndef CONFIG_USER_ONLY
9186 gen_cp0(env, ctx, MASK_C0(ctx->opcode), rt, rd);
9187 #endif /* !CONFIG_USER_ONLY */
9190 #ifndef CONFIG_USER_ONLY
9192 TCGv t0 = tcg_temp_new();
9194 op2 = MASK_MFMC0(ctx->opcode);
9197 check_insn(env, ctx, ASE_MT);
9198 gen_helper_dmt(t0, t0);
9199 gen_store_gpr(t0, rt);
9202 check_insn(env, ctx, ASE_MT);
9203 gen_helper_emt(t0, t0);
9204 gen_store_gpr(t0, rt);
9207 check_insn(env, ctx, ASE_MT);
9208 gen_helper_dvpe(t0, t0);
9209 gen_store_gpr(t0, rt);
9212 check_insn(env, ctx, ASE_MT);
9213 gen_helper_evpe(t0, t0);
9214 gen_store_gpr(t0, rt);
9217 check_insn(env, ctx, ISA_MIPS32R2);
9218 save_cpu_state(ctx, 1);
9220 gen_store_gpr(t0, rt);
9221 /* Stop translation as we may have switched the execution mode */
9222 ctx->bstate = BS_STOP;
9225 check_insn(env, ctx, ISA_MIPS32R2);
9226 save_cpu_state(ctx, 1);
9228 gen_store_gpr(t0, rt);
9229 /* Stop translation as we may have switched the execution mode */
9230 ctx->bstate = BS_STOP;
9232 default: /* Invalid */
9233 MIPS_INVAL("mfmc0");
9234 generate_exception(ctx, EXCP_RI);
9239 #endif /* !CONFIG_USER_ONLY */
9242 check_insn(env, ctx, ISA_MIPS32R2);
9243 gen_load_srsgpr(rt, rd);
9246 check_insn(env, ctx, ISA_MIPS32R2);
9247 gen_store_srsgpr(rt, rd);
9251 generate_exception(ctx, EXCP_RI);
9255 case OPC_ADDI: /* Arithmetic with immediate opcode */
9257 gen_arith_imm(env, ctx, op, rt, rs, imm);
9259 case OPC_SLTI: /* Set on less than with immediate opcode */
9261 gen_slt_imm(env, op, rt, rs, imm);
9263 case OPC_ANDI: /* Arithmetic with immediate opcode */
9267 gen_logic_imm(env, op, rt, rs, imm);
9269 case OPC_J ... OPC_JAL: /* Jump */
9270 offset = (int32_t)(ctx->opcode & 0x3FFFFFF) << 2;
9271 gen_compute_branch(ctx, op, 4, rs, rt, offset);
9274 case OPC_BEQ ... OPC_BGTZ: /* Branch */
9275 case OPC_BEQL ... OPC_BGTZL:
9276 gen_compute_branch(ctx, op, 4, rs, rt, imm << 2);
9279 case OPC_LB ... OPC_LWR: /* Load and stores */
9280 case OPC_SB ... OPC_SW:
9283 gen_ldst(ctx, op, rt, rs, imm);
9286 gen_st_cond(ctx, op, rt, rs, imm);
9289 check_insn(env, ctx, ISA_MIPS3 | ISA_MIPS32);
9293 check_insn(env, ctx, ISA_MIPS4 | ISA_MIPS32);
9297 /* Floating point (COP1). */
9302 if (env->CP0_Config1 & (1 << CP0C1_FP)) {
9303 check_cp1_enabled(ctx);
9304 gen_flt_ldst(ctx, op, rt, rs, imm);
9306 generate_exception_err(ctx, EXCP_CpU, 1);
9311 if (env->CP0_Config1 & (1 << CP0C1_FP)) {
9312 check_cp1_enabled(ctx);
9313 op1 = MASK_CP1(ctx->opcode);
9317 check_insn(env, ctx, ISA_MIPS32R2);
9322 gen_cp1(ctx, op1, rt, rd);
9324 #if defined(TARGET_MIPS64)
9327 check_insn(env, ctx, ISA_MIPS3);
9328 gen_cp1(ctx, op1, rt, rd);
9334 check_insn(env, ctx, ASE_MIPS3D);
9337 gen_compute_branch1(env, ctx, MASK_BC1(ctx->opcode),
9338 (rt >> 2) & 0x7, imm << 2);
9346 gen_farith(ctx, MASK_CP1_FUNC(ctx->opcode), rt, rd, sa,
9351 generate_exception (ctx, EXCP_RI);
9355 generate_exception_err(ctx, EXCP_CpU, 1);
9365 /* COP2: Not implemented. */
9366 generate_exception_err(ctx, EXCP_CpU, 2);
9370 if (env->CP0_Config1 & (1 << CP0C1_FP)) {
9371 check_cp1_enabled(ctx);
9372 op1 = MASK_CP3(ctx->opcode);
9380 gen_flt3_ldst(ctx, op1, sa, rd, rs, rt);
9398 gen_flt3_arith(ctx, op1, sa, rs, rd, rt);
9402 generate_exception (ctx, EXCP_RI);
9406 generate_exception_err(ctx, EXCP_CpU, 1);
9410 #if defined(TARGET_MIPS64)
9411 /* MIPS64 opcodes */
9413 case OPC_LDL ... OPC_LDR:
9414 case OPC_SDL ... OPC_SDR:
9418 check_insn(env, ctx, ISA_MIPS3);
9420 gen_ldst(ctx, op, rt, rs, imm);
9423 check_insn(env, ctx, ISA_MIPS3);
9425 gen_st_cond(ctx, op, rt, rs, imm);
9429 check_insn(env, ctx, ISA_MIPS3);
9431 gen_arith_imm(env, ctx, op, rt, rs, imm);
9435 check_insn(env, ctx, ASE_MIPS16);
9436 offset = (int32_t)(ctx->opcode & 0x3FFFFFF) << 2;
9437 gen_compute_branch(ctx, op, 4, rs, rt, offset);
9441 check_insn(env, ctx, ASE_MDMX);
9442 /* MDMX: Not implemented. */
9443 default: /* Invalid */
9444 MIPS_INVAL("major opcode");
9445 generate_exception(ctx, EXCP_RI);
9451 gen_intermediate_code_internal (CPUState *env, TranslationBlock *tb,
9455 target_ulong pc_start;
9456 uint16_t *gen_opc_end;
9465 qemu_log("search pc %d\n", search_pc);
9468 gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
9471 ctx.singlestep_enabled = env->singlestep_enabled;
9473 ctx.bstate = BS_NONE;
9474 /* Restore delay slot state from the tb context. */
9475 ctx.hflags = (uint32_t)tb->flags; /* FIXME: maybe use 64 bits here? */
9476 restore_cpu_state(env, &ctx);
9477 #ifdef CONFIG_USER_ONLY
9478 ctx.mem_idx = MIPS_HFLAG_UM;
9480 ctx.mem_idx = ctx.hflags & MIPS_HFLAG_KSU;
9483 max_insns = tb->cflags & CF_COUNT_MASK;
9485 max_insns = CF_COUNT_MASK;
9487 qemu_log_mask(CPU_LOG_TB_CPU, "------------------------------------------------\n");
9488 /* FIXME: This may print out stale hflags from env... */
9489 log_cpu_state_mask(CPU_LOG_TB_CPU, env, 0);
9491 LOG_DISAS("\ntb %p idx %d hflags %04x\n", tb, ctx.mem_idx, ctx.hflags);
9493 while (ctx.bstate == BS_NONE) {
9494 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
9495 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
9496 if (bp->pc == ctx.pc) {
9497 save_cpu_state(&ctx, 1);
9498 ctx.bstate = BS_BRANCH;
9499 gen_helper_0i(raise_exception, EXCP_DEBUG);
9500 /* Include the breakpoint location or the tb won't
9501 * be flushed when it must be. */
9503 goto done_generating;
9509 j = gen_opc_ptr - gen_opc_buf;
9513 gen_opc_instr_start[lj++] = 0;
9515 gen_opc_pc[lj] = ctx.pc;
9516 gen_opc_hflags[lj] = ctx.hflags & MIPS_HFLAG_BMASK;
9517 gen_opc_instr_start[lj] = 1;
9518 gen_opc_icount[lj] = num_insns;
9520 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
9524 if (!(ctx.hflags & MIPS_HFLAG_M16)) {
9525 ctx.opcode = ldl_code(ctx.pc);
9527 decode_opc(env, &ctx, &is_branch);
9528 } else if (env->insn_flags & ASE_MIPS16) {
9529 ctx.opcode = lduw_code(ctx.pc);
9530 insn_bytes = decode_mips16_opc(env, &ctx, &is_branch);
9532 generate_exception(&ctx, EXCP_RI);
9536 handle_delay_slot(env, &ctx, insn_bytes);
9538 ctx.pc += insn_bytes;
9542 /* Execute a branch and its delay slot as a single instruction.
9543 This is what GDB expects and is consistent with what the
9544 hardware does (e.g. if a delay slot instruction faults, the
9545 reported PC is the PC of the branch). */
9546 if (env->singlestep_enabled && (ctx.hflags & MIPS_HFLAG_BMASK) == 0)
9549 if ((ctx.pc & (TARGET_PAGE_SIZE - 1)) == 0)
9552 if (gen_opc_ptr >= gen_opc_end)
9555 if (num_insns >= max_insns)
9561 if (tb->cflags & CF_LAST_IO)
9563 if (env->singlestep_enabled && ctx.bstate != BS_BRANCH) {
9564 save_cpu_state(&ctx, ctx.bstate == BS_NONE);
9565 gen_helper_0i(raise_exception, EXCP_DEBUG);
9567 switch (ctx.bstate) {
9569 gen_helper_interrupt_restart();
9570 gen_goto_tb(&ctx, 0, ctx.pc);
9573 save_cpu_state(&ctx, 0);
9574 gen_goto_tb(&ctx, 0, ctx.pc);
9577 gen_helper_interrupt_restart();
9586 gen_icount_end(tb, num_insns);
9587 *gen_opc_ptr = INDEX_op_end;
9589 j = gen_opc_ptr - gen_opc_buf;
9592 gen_opc_instr_start[lj++] = 0;
9594 tb->size = ctx.pc - pc_start;
9595 tb->icount = num_insns;
9599 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
9600 qemu_log("IN: %s\n", lookup_symbol(pc_start));
9601 log_target_disas(pc_start, ctx.pc - pc_start, 0);
9604 qemu_log_mask(CPU_LOG_TB_CPU, "---------------- %d %08x\n", ctx.bstate, ctx.hflags);
9608 void gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
9610 gen_intermediate_code_internal(env, tb, 0);
9613 void gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
9615 gen_intermediate_code_internal(env, tb, 1);
9618 static void fpu_dump_state(CPUState *env, FILE *f,
9619 int (*fpu_fprintf)(FILE *f, const char *fmt, ...),
9623 int is_fpu64 = !!(env->hflags & MIPS_HFLAG_F64);
9625 #define printfpr(fp) \
9628 fpu_fprintf(f, "w:%08x d:%016lx fd:%13g fs:%13g psu: %13g\n", \
9629 (fp)->w[FP_ENDIAN_IDX], (fp)->d, (fp)->fd, \
9630 (fp)->fs[FP_ENDIAN_IDX], (fp)->fs[!FP_ENDIAN_IDX]); \
9633 tmp.w[FP_ENDIAN_IDX] = (fp)->w[FP_ENDIAN_IDX]; \
9634 tmp.w[!FP_ENDIAN_IDX] = ((fp) + 1)->w[FP_ENDIAN_IDX]; \
9635 fpu_fprintf(f, "w:%08x d:%016lx fd:%13g fs:%13g psu:%13g\n", \
9636 tmp.w[FP_ENDIAN_IDX], tmp.d, tmp.fd, \
9637 tmp.fs[FP_ENDIAN_IDX], tmp.fs[!FP_ENDIAN_IDX]); \
9642 fpu_fprintf(f, "CP1 FCR0 0x%08x FCR31 0x%08x SR.FR %d fp_status 0x%08x(0x%02x)\n",
9643 env->active_fpu.fcr0, env->active_fpu.fcr31, is_fpu64, env->active_fpu.fp_status,
9644 get_float_exception_flags(&env->active_fpu.fp_status));
9645 for (i = 0; i < 32; (is_fpu64) ? i++ : (i += 2)) {
9646 fpu_fprintf(f, "%3s: ", fregnames[i]);
9647 printfpr(&env->active_fpu.fpr[i]);
9653 #if defined(TARGET_MIPS64) && defined(MIPS_DEBUG_SIGN_EXTENSIONS)
9654 /* Debug help: The architecture requires 32bit code to maintain proper
9655 sign-extended values on 64bit machines. */
9657 #define SIGN_EXT_P(val) ((((val) & ~0x7fffffff) == 0) || (((val) & ~0x7fffffff) == ~0x7fffffff))
9660 cpu_mips_check_sign_extensions (CPUState *env, FILE *f,
9661 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
9666 if (!SIGN_EXT_P(env->active_tc.PC))
9667 cpu_fprintf(f, "BROKEN: pc=0x" TARGET_FMT_lx "\n", env->active_tc.PC);
9668 if (!SIGN_EXT_P(env->active_tc.HI[0]))
9669 cpu_fprintf(f, "BROKEN: HI=0x" TARGET_FMT_lx "\n", env->active_tc.HI[0]);
9670 if (!SIGN_EXT_P(env->active_tc.LO[0]))
9671 cpu_fprintf(f, "BROKEN: LO=0x" TARGET_FMT_lx "\n", env->active_tc.LO[0]);
9672 if (!SIGN_EXT_P(env->btarget))
9673 cpu_fprintf(f, "BROKEN: btarget=0x" TARGET_FMT_lx "\n", env->btarget);
9675 for (i = 0; i < 32; i++) {
9676 if (!SIGN_EXT_P(env->active_tc.gpr[i]))
9677 cpu_fprintf(f, "BROKEN: %s=0x" TARGET_FMT_lx "\n", regnames[i], env->active_tc.gpr[i]);
9680 if (!SIGN_EXT_P(env->CP0_EPC))
9681 cpu_fprintf(f, "BROKEN: EPC=0x" TARGET_FMT_lx "\n", env->CP0_EPC);
9682 if (!SIGN_EXT_P(env->lladdr))
9683 cpu_fprintf(f, "BROKEN: LLAddr=0x" TARGET_FMT_lx "\n", env->lladdr);
9687 void cpu_dump_state (CPUState *env, FILE *f,
9688 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
9693 cpu_fprintf(f, "pc=0x" TARGET_FMT_lx " HI=0x" TARGET_FMT_lx " LO=0x" TARGET_FMT_lx " ds %04x " TARGET_FMT_lx " %d\n",
9694 env->active_tc.PC, env->active_tc.HI[0], env->active_tc.LO[0],
9695 env->hflags, env->btarget, env->bcond);
9696 for (i = 0; i < 32; i++) {
9698 cpu_fprintf(f, "GPR%02d:", i);
9699 cpu_fprintf(f, " %s " TARGET_FMT_lx, regnames[i], env->active_tc.gpr[i]);
9701 cpu_fprintf(f, "\n");
9704 cpu_fprintf(f, "CP0 Status 0x%08x Cause 0x%08x EPC 0x" TARGET_FMT_lx "\n",
9705 env->CP0_Status, env->CP0_Cause, env->CP0_EPC);
9706 cpu_fprintf(f, " Config0 0x%08x Config1 0x%08x LLAddr 0x" TARGET_FMT_lx "\n",
9707 env->CP0_Config0, env->CP0_Config1, env->lladdr);
9708 if (env->hflags & MIPS_HFLAG_FPU)
9709 fpu_dump_state(env, f, cpu_fprintf, flags);
9710 #if defined(TARGET_MIPS64) && defined(MIPS_DEBUG_SIGN_EXTENSIONS)
9711 cpu_mips_check_sign_extensions(env, f, cpu_fprintf, flags);
9715 static void mips_tcg_init(void)
9720 /* Initialize various static tables. */
9724 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
9725 TCGV_UNUSED(cpu_gpr[0]);
9726 for (i = 1; i < 32; i++)
9727 cpu_gpr[i] = tcg_global_mem_new(TCG_AREG0,
9728 offsetof(CPUState, active_tc.gpr[i]),
9730 cpu_PC = tcg_global_mem_new(TCG_AREG0,
9731 offsetof(CPUState, active_tc.PC), "PC");
9732 for (i = 0; i < MIPS_DSP_ACC; i++) {
9733 cpu_HI[i] = tcg_global_mem_new(TCG_AREG0,
9734 offsetof(CPUState, active_tc.HI[i]),
9736 cpu_LO[i] = tcg_global_mem_new(TCG_AREG0,
9737 offsetof(CPUState, active_tc.LO[i]),
9739 cpu_ACX[i] = tcg_global_mem_new(TCG_AREG0,
9740 offsetof(CPUState, active_tc.ACX[i]),
9743 cpu_dspctrl = tcg_global_mem_new(TCG_AREG0,
9744 offsetof(CPUState, active_tc.DSPControl),
9746 bcond = tcg_global_mem_new(TCG_AREG0,
9747 offsetof(CPUState, bcond), "bcond");
9748 btarget = tcg_global_mem_new(TCG_AREG0,
9749 offsetof(CPUState, btarget), "btarget");
9750 hflags = tcg_global_mem_new_i32(TCG_AREG0,
9751 offsetof(CPUState, hflags), "hflags");
9753 fpu_fcr0 = tcg_global_mem_new_i32(TCG_AREG0,
9754 offsetof(CPUState, active_fpu.fcr0),
9756 fpu_fcr31 = tcg_global_mem_new_i32(TCG_AREG0,
9757 offsetof(CPUState, active_fpu.fcr31),
9760 /* register helpers */
9761 #define GEN_HELPER 2
9767 #include "translate_init.c"
9769 CPUMIPSState *cpu_mips_init (const char *cpu_model)
9772 const mips_def_t *def;
9774 def = cpu_mips_find_by_name(cpu_model);
9777 env = qemu_mallocz(sizeof(CPUMIPSState));
9778 env->cpu_model = def;
9779 env->cpu_model_str = cpu_model;
9782 #ifndef CONFIG_USER_ONLY
9789 qemu_init_vcpu(env);
9793 void cpu_reset (CPUMIPSState *env)
9795 if (qemu_loglevel_mask(CPU_LOG_RESET)) {
9796 qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
9797 log_cpu_state(env, 0);
9800 memset(env, 0, offsetof(CPUMIPSState, breakpoints));
9803 /* Reset registers to their default values */
9804 env->CP0_PRid = env->cpu_model->CP0_PRid;
9805 env->CP0_Config0 = env->cpu_model->CP0_Config0;
9806 #ifdef TARGET_WORDS_BIGENDIAN
9807 env->CP0_Config0 |= (1 << CP0C0_BE);
9809 env->CP0_Config1 = env->cpu_model->CP0_Config1;
9810 env->CP0_Config2 = env->cpu_model->CP0_Config2;
9811 env->CP0_Config3 = env->cpu_model->CP0_Config3;
9812 env->CP0_Config6 = env->cpu_model->CP0_Config6;
9813 env->CP0_Config7 = env->cpu_model->CP0_Config7;
9814 env->CP0_LLAddr_rw_bitmask = env->cpu_model->CP0_LLAddr_rw_bitmask
9815 << env->cpu_model->CP0_LLAddr_shift;
9816 env->CP0_LLAddr_shift = env->cpu_model->CP0_LLAddr_shift;
9817 env->SYNCI_Step = env->cpu_model->SYNCI_Step;
9818 env->CCRes = env->cpu_model->CCRes;
9819 env->CP0_Status_rw_bitmask = env->cpu_model->CP0_Status_rw_bitmask;
9820 env->CP0_TCStatus_rw_bitmask = env->cpu_model->CP0_TCStatus_rw_bitmask;
9821 env->CP0_SRSCtl = env->cpu_model->CP0_SRSCtl;
9822 env->current_tc = 0;
9823 env->SEGBITS = env->cpu_model->SEGBITS;
9824 env->SEGMask = (target_ulong)((1ULL << env->cpu_model->SEGBITS) - 1);
9825 #if defined(TARGET_MIPS64)
9826 if (env->cpu_model->insn_flags & ISA_MIPS3) {
9827 env->SEGMask |= 3ULL << 62;
9830 env->PABITS = env->cpu_model->PABITS;
9831 env->PAMask = (target_ulong)((1ULL << env->cpu_model->PABITS) - 1);
9832 env->CP0_SRSConf0_rw_bitmask = env->cpu_model->CP0_SRSConf0_rw_bitmask;
9833 env->CP0_SRSConf0 = env->cpu_model->CP0_SRSConf0;
9834 env->CP0_SRSConf1_rw_bitmask = env->cpu_model->CP0_SRSConf1_rw_bitmask;
9835 env->CP0_SRSConf1 = env->cpu_model->CP0_SRSConf1;
9836 env->CP0_SRSConf2_rw_bitmask = env->cpu_model->CP0_SRSConf2_rw_bitmask;
9837 env->CP0_SRSConf2 = env->cpu_model->CP0_SRSConf2;
9838 env->CP0_SRSConf3_rw_bitmask = env->cpu_model->CP0_SRSConf3_rw_bitmask;
9839 env->CP0_SRSConf3 = env->cpu_model->CP0_SRSConf3;
9840 env->CP0_SRSConf4_rw_bitmask = env->cpu_model->CP0_SRSConf4_rw_bitmask;
9841 env->CP0_SRSConf4 = env->cpu_model->CP0_SRSConf4;
9842 env->insn_flags = env->cpu_model->insn_flags;
9844 #if defined(CONFIG_USER_ONLY)
9845 env->hflags = MIPS_HFLAG_UM;
9846 /* Enable access to the SYNCI_Step register. */
9847 env->CP0_HWREna |= (1 << 1);
9848 if (env->CP0_Config1 & (1 << CP0C1_FP)) {
9849 env->hflags |= MIPS_HFLAG_FPU;
9851 #ifdef TARGET_MIPS64
9852 if (env->active_fpu.fcr0 & (1 << FCR0_F64)) {
9853 env->hflags |= MIPS_HFLAG_F64;
9857 if (env->hflags & MIPS_HFLAG_BMASK) {
9858 /* If the exception was raised from a delay slot,
9859 come back to the jump. */
9860 env->CP0_ErrorEPC = env->active_tc.PC - 4;
9862 env->CP0_ErrorEPC = env->active_tc.PC;
9864 env->active_tc.PC = (int32_t)0xBFC00000;
9865 env->CP0_Random = env->tlb->nb_tlb - 1;
9866 env->tlb->tlb_in_use = env->tlb->nb_tlb;
9868 /* SMP not implemented */
9869 env->CP0_EBase = 0x80000000;
9870 env->CP0_Status = (1 << CP0St_BEV) | (1 << CP0St_ERL);
9871 /* vectored interrupts not implemented, timer on int 7,
9872 no performance counters. */
9873 env->CP0_IntCtl = 0xe0000000;
9877 for (i = 0; i < 7; i++) {
9878 env->CP0_WatchLo[i] = 0;
9879 env->CP0_WatchHi[i] = 0x80000000;
9881 env->CP0_WatchLo[7] = 0;
9882 env->CP0_WatchHi[7] = 0;
9884 /* Count register increments in debug mode, EJTAG version 1 */
9885 env->CP0_Debug = (1 << CP0DB_CNT) | (0x1 << CP0DB_VER);
9886 env->hflags = MIPS_HFLAG_CP0;
9888 #if defined(TARGET_MIPS64)
9889 if (env->cpu_model->insn_flags & ISA_MIPS3) {
9890 env->hflags |= MIPS_HFLAG_64;
9893 env->exception_index = EXCP_NONE;
9896 void gen_pc_load(CPUState *env, TranslationBlock *tb,
9897 unsigned long searched_pc, int pc_pos, void *puc)
9899 env->active_tc.PC = gen_opc_pc[pc_pos];
9900 env->hflags &= ~MIPS_HFLAG_BMASK;
9901 env->hflags |= gen_opc_hflags[pc_pos];