]>
git.proxmox.com Git - mirror_qemu.git/blob - target-mips/translate.c
2 * MIPS32 emulation for qemu: main translation routines.
4 * Copyright (c) 2004-2005 Jocelyn Mayer
5 * Copyright (c) 2006 Marius Groeger (FPU operations)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 //#define MIPS_DEBUG_DISAS
33 //#define MIPS_SINGLE_STEP
35 #ifdef USE_DIRECT_JUMP
38 #define TBPARAM(x) (long)(x)
42 #define DEF(s, n, copy_size) INDEX_op_ ## s,
48 static uint16_t *gen_opc_ptr
;
49 static uint32_t *gen_opparam_ptr
;
54 #define EXT_SPECIAL 0x100
55 #define EXT_SPECIAL2 0x200
56 #define EXT_REGIMM 0x300
63 /* indirect opcode tables */
71 /* arithmetic with immediate */
80 /* Jump and branches */
83 OPC_BEQ
= 0x04, /* Unconditional if rs = rt = 0 (B) */
91 OPC_JALX
= 0x1D, /* MIPS 16 only */
108 /* Floating point load/store */
117 /* Cache and prefetch */
122 /* MIPS special opcodes */
125 OPC_SLL
= 0x00 | EXT_SPECIAL
,
126 /* NOP is SLL r0, r0, 0 */
127 /* SSNOP is SLL r0, r0, 1 */
128 OPC_SRL
= 0x02 | EXT_SPECIAL
,
129 OPC_SRA
= 0x03 | EXT_SPECIAL
,
130 OPC_SLLV
= 0x04 | EXT_SPECIAL
,
131 OPC_SRLV
= 0x06 | EXT_SPECIAL
,
132 OPC_SRAV
= 0x07 | EXT_SPECIAL
,
133 /* Multiplication / division */
134 OPC_MULT
= 0x18 | EXT_SPECIAL
,
135 OPC_MULTU
= 0x19 | EXT_SPECIAL
,
136 OPC_DIV
= 0x1A | EXT_SPECIAL
,
137 OPC_DIVU
= 0x1B | EXT_SPECIAL
,
138 /* 2 registers arithmetic / logic */
139 OPC_ADD
= 0x20 | EXT_SPECIAL
,
140 OPC_ADDU
= 0x21 | EXT_SPECIAL
,
141 OPC_SUB
= 0x22 | EXT_SPECIAL
,
142 OPC_SUBU
= 0x23 | EXT_SPECIAL
,
143 OPC_AND
= 0x24 | EXT_SPECIAL
,
144 OPC_OR
= 0x25 | EXT_SPECIAL
,
145 OPC_XOR
= 0x26 | EXT_SPECIAL
,
146 OPC_NOR
= 0x27 | EXT_SPECIAL
,
147 OPC_SLT
= 0x2A | EXT_SPECIAL
,
148 OPC_SLTU
= 0x2B | EXT_SPECIAL
,
150 OPC_JR
= 0x08 | EXT_SPECIAL
,
151 OPC_JALR
= 0x09 | EXT_SPECIAL
,
153 OPC_TGE
= 0x30 | EXT_SPECIAL
,
154 OPC_TGEU
= 0x31 | EXT_SPECIAL
,
155 OPC_TLT
= 0x32 | EXT_SPECIAL
,
156 OPC_TLTU
= 0x33 | EXT_SPECIAL
,
157 OPC_TEQ
= 0x34 | EXT_SPECIAL
,
158 OPC_TNE
= 0x36 | EXT_SPECIAL
,
159 /* HI / LO registers load & stores */
160 OPC_MFHI
= 0x10 | EXT_SPECIAL
,
161 OPC_MTHI
= 0x11 | EXT_SPECIAL
,
162 OPC_MFLO
= 0x12 | EXT_SPECIAL
,
163 OPC_MTLO
= 0x13 | EXT_SPECIAL
,
164 /* Conditional moves */
165 OPC_MOVZ
= 0x0A | EXT_SPECIAL
,
166 OPC_MOVN
= 0x0B | EXT_SPECIAL
,
168 OPC_MOVCI
= 0x01 | EXT_SPECIAL
,
171 OPC_PMON
= 0x05 | EXT_SPECIAL
,
172 OPC_SYSCALL
= 0x0C | EXT_SPECIAL
,
173 OPC_BREAK
= 0x0D | EXT_SPECIAL
,
174 OPC_SYNC
= 0x0F | EXT_SPECIAL
,
178 /* Mutiply & xxx operations */
179 OPC_MADD
= 0x00 | EXT_SPECIAL2
,
180 OPC_MADDU
= 0x01 | EXT_SPECIAL2
,
181 OPC_MUL
= 0x02 | EXT_SPECIAL2
,
182 OPC_MSUB
= 0x04 | EXT_SPECIAL2
,
183 OPC_MSUBU
= 0x05 | EXT_SPECIAL2
,
185 OPC_CLZ
= 0x20 | EXT_SPECIAL2
,
186 OPC_CLO
= 0x21 | EXT_SPECIAL2
,
188 OPC_SDBBP
= 0x3F | EXT_SPECIAL2
,
193 OPC_BLTZ
= 0x00 | EXT_REGIMM
,
194 OPC_BLTZL
= 0x02 | EXT_REGIMM
,
195 OPC_BGEZ
= 0x01 | EXT_REGIMM
,
196 OPC_BGEZL
= 0x03 | EXT_REGIMM
,
197 OPC_BLTZAL
= 0x10 | EXT_REGIMM
,
198 OPC_BLTZALL
= 0x12 | EXT_REGIMM
,
199 OPC_BGEZAL
= 0x11 | EXT_REGIMM
,
200 OPC_BGEZALL
= 0x13 | EXT_REGIMM
,
201 OPC_TGEI
= 0x08 | EXT_REGIMM
,
202 OPC_TGEIU
= 0x09 | EXT_REGIMM
,
203 OPC_TLTI
= 0x0A | EXT_REGIMM
,
204 OPC_TLTIU
= 0x0B | EXT_REGIMM
,
205 OPC_TEQI
= 0x0C | EXT_REGIMM
,
206 OPC_TNEI
= 0x0E | EXT_REGIMM
,
210 /* Coprocessor 0 (MMU) */
211 OPC_MFC0
= 0x00 | EXT_CP0
,
212 OPC_MTC0
= 0x04 | EXT_CP0
,
213 OPC_TLBR
= 0x01 | EXT_CP0
,
214 OPC_TLBWI
= 0x02 | EXT_CP0
,
215 OPC_TLBWR
= 0x06 | EXT_CP0
,
216 OPC_TLBP
= 0x08 | EXT_CP0
,
217 OPC_ERET
= 0x18 | EXT_CP0
,
218 OPC_DERET
= 0x1F | EXT_CP0
,
219 OPC_WAIT
= 0x20 | EXT_CP0
,
224 /* Coprocessor 1 (FPU) */
225 OPC_MFC1
= 0x00 | EXT_CP1
,
226 OPC_MTC1
= 0x04 | EXT_CP1
,
227 OPC_CFC1
= 0x02 | EXT_CP1
,
228 OPC_CTC1
= 0x06 | EXT_CP1
,
232 const unsigned char *regnames
[] =
233 { "r0", "at", "v0", "v1", "a0", "a1", "a2", "a3",
234 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
235 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
236 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra", };
238 /* Warning: no function for r0 register (hard wired to zero) */
239 #define GEN32(func, NAME) \
240 static GenOpFunc *NAME ## _table [32] = { \
241 NULL, NAME ## 1, NAME ## 2, NAME ## 3, \
242 NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7, \
243 NAME ## 8, NAME ## 9, NAME ## 10, NAME ## 11, \
244 NAME ## 12, NAME ## 13, NAME ## 14, NAME ## 15, \
245 NAME ## 16, NAME ## 17, NAME ## 18, NAME ## 19, \
246 NAME ## 20, NAME ## 21, NAME ## 22, NAME ## 23, \
247 NAME ## 24, NAME ## 25, NAME ## 26, NAME ## 27, \
248 NAME ## 28, NAME ## 29, NAME ## 30, NAME ## 31, \
250 static inline void func(int n) \
252 NAME ## _table[n](); \
255 /* General purpose registers moves */
256 GEN32(gen_op_load_gpr_T0
, gen_op_load_gpr_T0_gpr
);
257 GEN32(gen_op_load_gpr_T1
, gen_op_load_gpr_T1_gpr
);
258 GEN32(gen_op_load_gpr_T2
, gen_op_load_gpr_T2_gpr
);
260 GEN32(gen_op_store_T0_gpr
, gen_op_store_T0_gpr_gpr
);
261 GEN32(gen_op_store_T1_gpr
, gen_op_store_T1_gpr_gpr
);
264 const unsigned char *fregnames
[] =
265 { "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
266 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
267 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
268 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31", };
270 # define SFGEN32(func, NAME) \
271 static GenOpFunc *NAME ## _table [32] = { \
272 NAME ## 0, NAME ## 1, NAME ## 2, NAME ## 3, \
273 NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7, \
274 NAME ## 8, NAME ## 9, NAME ## 10, NAME ## 11, \
275 NAME ## 12, NAME ## 13, NAME ## 14, NAME ## 15, \
276 NAME ## 16, NAME ## 17, NAME ## 18, NAME ## 19, \
277 NAME ## 20, NAME ## 21, NAME ## 22, NAME ## 23, \
278 NAME ## 24, NAME ## 25, NAME ## 26, NAME ## 27, \
279 NAME ## 28, NAME ## 29, NAME ## 30, NAME ## 31, \
281 static inline void func(int n) \
283 NAME ## _table[n](); \
286 # define DFGEN32(func, NAME) \
287 static GenOpFunc *NAME ## _table [32] = { \
288 NAME ## 0, 0, NAME ## 2, 0, \
289 NAME ## 4, 0, NAME ## 6, 0, \
290 NAME ## 8, 0, NAME ## 10, 0, \
291 NAME ## 12, 0, NAME ## 14, 0, \
292 NAME ## 16, 0, NAME ## 18, 0, \
293 NAME ## 20, 0, NAME ## 22, 0, \
294 NAME ## 24, 0, NAME ## 26, 0, \
295 NAME ## 28, 0, NAME ## 30, 0, \
297 static inline void func(int n) \
299 NAME ## _table[n](); \
302 SFGEN32(gen_op_load_fpr_WT0
, gen_op_load_fpr_WT0_fpr
);
303 SFGEN32(gen_op_store_fpr_WT0
, gen_op_store_fpr_WT0_fpr
);
305 SFGEN32(gen_op_load_fpr_WT1
, gen_op_load_fpr_WT1_fpr
);
306 SFGEN32(gen_op_store_fpr_WT1
, gen_op_store_fpr_WT1_fpr
);
308 SFGEN32(gen_op_load_fpr_WT2
, gen_op_load_fpr_WT2_fpr
);
309 SFGEN32(gen_op_store_fpr_WT2
, gen_op_store_fpr_WT2_fpr
);
311 DFGEN32(gen_op_load_fpr_DT0
, gen_op_load_fpr_DT0_fpr
);
312 DFGEN32(gen_op_store_fpr_DT0
, gen_op_store_fpr_DT0_fpr
);
314 DFGEN32(gen_op_load_fpr_DT1
, gen_op_load_fpr_DT1_fpr
);
315 DFGEN32(gen_op_store_fpr_DT1
, gen_op_store_fpr_DT1_fpr
);
317 DFGEN32(gen_op_load_fpr_DT2
, gen_op_load_fpr_DT2_fpr
);
318 DFGEN32(gen_op_store_fpr_DT2
, gen_op_store_fpr_DT2_fpr
);
320 #define FOP_CONDS(fmt) \
321 static GenOpFunc * cond_ ## fmt ## _table[16] = { \
322 gen_op_cmp_ ## fmt ## _f, \
323 gen_op_cmp_ ## fmt ## _un, \
324 gen_op_cmp_ ## fmt ## _eq, \
325 gen_op_cmp_ ## fmt ## _ueq, \
326 gen_op_cmp_ ## fmt ## _olt, \
327 gen_op_cmp_ ## fmt ## _ult, \
328 gen_op_cmp_ ## fmt ## _ole, \
329 gen_op_cmp_ ## fmt ## _ule, \
330 gen_op_cmp_ ## fmt ## _sf, \
331 gen_op_cmp_ ## fmt ## _ngle, \
332 gen_op_cmp_ ## fmt ## _seq, \
333 gen_op_cmp_ ## fmt ## _ngl, \
334 gen_op_cmp_ ## fmt ## _lt, \
335 gen_op_cmp_ ## fmt ## _nge, \
336 gen_op_cmp_ ## fmt ## _le, \
337 gen_op_cmp_ ## fmt ## _ngt, \
339 static inline void gen_cmp_ ## fmt(int n) \
341 cond_ ## fmt ## _table[n](); \
349 typedef struct DisasContext
{
350 struct TranslationBlock
*tb
;
351 target_ulong pc
, saved_pc
;
353 /* Routine used to access memory */
355 uint32_t hflags
, saved_hflags
;
358 target_ulong btarget
;
362 BS_NONE
= 0, /* We go out of the TB without reaching a branch or an
363 * exception condition
365 BS_STOP
= 1, /* We want to stop translation for any reason */
366 BS_BRANCH
= 2, /* We reached a branch condition */
367 BS_EXCP
= 3, /* We reached an exception condition */
370 #if defined MIPS_DEBUG_DISAS
371 #define MIPS_DEBUG(fmt, args...) \
373 if (loglevel & CPU_LOG_TB_IN_ASM) { \
374 fprintf(logfile, "%08x: %08x " fmt "\n", \
375 ctx->pc, ctx->opcode , ##args); \
379 #define MIPS_DEBUG(fmt, args...) do { } while(0)
382 #define MIPS_INVAL(op) \
384 MIPS_DEBUG("Invalid %s %03x %03x %03x", op, ctx->opcode >> 26, \
385 ctx->opcode & 0x3F, ((ctx->opcode >> 16) & 0x1F)); \
388 #define GEN_LOAD_REG_TN(Tn, Rn) \
391 glue(gen_op_reset_, Tn)(); \
393 glue(gen_op_load_gpr_, Tn)(Rn); \
397 #define GEN_LOAD_IMM_TN(Tn, Imm) \
400 glue(gen_op_reset_, Tn)(); \
402 glue(gen_op_set_, Tn)(Imm); \
406 #define GEN_STORE_TN_REG(Rn, Tn) \
409 glue(glue(gen_op_store_, Tn),_gpr)(Rn); \
415 # define GEN_LOAD_FREG_FTN(FTn, Fn) \
417 glue(gen_op_load_fpr_, FTn)(Fn); \
420 #define GEN_STORE_FTN_FREG(Fn, FTn) \
422 glue(gen_op_store_fpr_, FTn)(Fn); \
427 static inline void save_cpu_state (DisasContext
*ctx
, int do_save_pc
)
429 #if defined MIPS_DEBUG_DISAS
430 if (loglevel
& CPU_LOG_TB_IN_ASM
) {
431 fprintf(logfile
, "hflags %08x saved %08x\n",
432 ctx
->hflags
, ctx
->saved_hflags
);
435 if (do_save_pc
&& ctx
->pc
!= ctx
->saved_pc
) {
436 gen_op_save_pc(ctx
->pc
);
437 ctx
->saved_pc
= ctx
->pc
;
439 if (ctx
->hflags
!= ctx
->saved_hflags
) {
440 gen_op_save_state(ctx
->hflags
);
441 ctx
->saved_hflags
= ctx
->hflags
;
442 if (ctx
->hflags
& MIPS_HFLAG_BR
) {
443 gen_op_save_breg_target();
444 } else if (ctx
->hflags
& MIPS_HFLAG_B
) {
445 gen_op_save_btarget(ctx
->btarget
);
446 } else if (ctx
->hflags
& MIPS_HFLAG_BMASK
) {
448 gen_op_save_btarget(ctx
->btarget
);
453 static inline void generate_exception_err (DisasContext
*ctx
, int excp
, int err
)
455 #if defined MIPS_DEBUG_DISAS
456 if (loglevel
& CPU_LOG_TB_IN_ASM
)
457 fprintf(logfile
, "%s: raise exception %d\n", __func__
, excp
);
459 save_cpu_state(ctx
, 1);
461 gen_op_raise_exception(excp
);
463 gen_op_raise_exception_err(excp
, err
);
464 ctx
->bstate
= BS_EXCP
;
467 static inline void generate_exception (DisasContext
*ctx
, int excp
)
469 generate_exception_err (ctx
, excp
, 0);
472 #if defined(CONFIG_USER_ONLY)
473 #define op_ldst(name) gen_op_##name##_raw()
474 #define OP_LD_TABLE(width)
475 #define OP_ST_TABLE(width)
477 #define op_ldst(name) (*gen_op_##name[ctx->mem_idx])()
478 #define OP_LD_TABLE(width) \
479 static GenOpFunc *gen_op_l##width[] = { \
480 &gen_op_l##width##_user, \
481 &gen_op_l##width##_kernel, \
483 #define OP_ST_TABLE(width) \
484 static GenOpFunc *gen_op_s##width[] = { \
485 &gen_op_s##width##_user, \
486 &gen_op_s##width##_kernel, \
521 static void gen_ldst (DisasContext
*ctx
, uint16_t opc
, int rt
,
522 int base
, int16_t offset
)
524 const unsigned char *opn
= "unk";
527 GEN_LOAD_IMM_TN(T0
, offset
);
528 } else if (offset
== 0) {
529 gen_op_load_gpr_T0(base
);
531 gen_op_load_gpr_T0(base
);
532 gen_op_set_T1(offset
);
535 /* Don't do NOP if destination is zero: we must perform the actual
539 #if defined(TARGET_MIPS64)
541 #if defined (MIPS_HAS_UNALIGNED_LS)
545 GEN_STORE_TN_REG(rt
, T0
);
549 #if defined (MIPS_HAS_UNALIGNED_LS)
552 GEN_LOAD_REG_TN(T1
, rt
);
558 GEN_STORE_TN_REG(rt
, T0
);
562 GEN_LOAD_REG_TN(T1
, rt
);
568 GEN_STORE_TN_REG(rt
, T0
);
572 GEN_LOAD_REG_TN(T1
, rt
);
578 #if defined (MIPS_HAS_UNALIGNED_LS)
582 GEN_STORE_TN_REG(rt
, T0
);
587 GEN_STORE_TN_REG(rt
, T0
);
591 #if defined (MIPS_HAS_UNALIGNED_LS)
594 GEN_LOAD_REG_TN(T1
, rt
);
599 #if defined (MIPS_HAS_UNALIGNED_LS)
603 GEN_STORE_TN_REG(rt
, T0
);
607 #if defined (MIPS_HAS_UNALIGNED_LS)
610 GEN_LOAD_REG_TN(T1
, rt
);
615 #if defined (MIPS_HAS_UNALIGNED_LS)
619 GEN_STORE_TN_REG(rt
, T0
);
624 GEN_STORE_TN_REG(rt
, T0
);
628 GEN_LOAD_REG_TN(T1
, rt
);
634 GEN_STORE_TN_REG(rt
, T0
);
638 GEN_LOAD_REG_TN(T1
, rt
);
640 GEN_STORE_TN_REG(rt
, T0
);
644 GEN_LOAD_REG_TN(T1
, rt
);
649 GEN_LOAD_REG_TN(T1
, rt
);
651 GEN_STORE_TN_REG(rt
, T0
);
655 GEN_LOAD_REG_TN(T1
, rt
);
661 GEN_STORE_TN_REG(rt
, T0
);
665 GEN_LOAD_REG_TN(T1
, rt
);
667 GEN_STORE_TN_REG(rt
, T0
);
671 MIPS_INVAL("load/store");
672 generate_exception(ctx
, EXCP_RI
);
675 MIPS_DEBUG("%s %s, %d(%s)", opn
, regnames
[rt
], offset
, regnames
[base
]);
681 static void gen_flt_ldst (DisasContext
*ctx
, uint16_t opc
, int ft
,
682 int base
, int16_t offset
)
684 const unsigned char *opn
= "unk";
687 GEN_LOAD_IMM_TN(T0
, offset
);
688 } else if (offset
== 0) {
689 gen_op_load_gpr_T0(base
);
691 gen_op_load_gpr_T0(base
);
692 gen_op_set_T1(offset
);
695 /* Don't do NOP if destination is zero: we must perform the actual
701 GEN_STORE_FTN_FREG(ft
, WT0
);
705 GEN_LOAD_FREG_FTN(WT0
, ft
);
711 GEN_STORE_FTN_FREG(ft
, DT0
);
715 GEN_LOAD_FREG_FTN(DT0
, ft
);
720 MIPS_INVAL("float load/store");
721 generate_exception(ctx
, EXCP_CpU
);
724 MIPS_DEBUG("%s %s, %d(%s)", opn
, fregnames
[ft
], offset
, regnames
[base
]);
728 /* Arithmetic with immediate operand */
729 static void gen_arith_imm (DisasContext
*ctx
, uint16_t opc
, int rt
,
733 const unsigned char *opn
= "unk";
735 if (rt
== 0 && opc
!= OPC_ADDI
) {
736 /* if no destination, treat it as a NOP
737 * For addi, we must generate the overflow exception when needed.
742 if (opc
== OPC_ADDI
|| opc
== OPC_ADDIU
||
743 opc
== OPC_SLTI
|| opc
== OPC_SLTIU
)
744 uimm
= (int32_t)imm
; /* Sign extent to 32 bits */
746 uimm
= (uint16_t)imm
;
747 if (opc
!= OPC_LUI
) {
748 GEN_LOAD_REG_TN(T0
, rs
);
749 GEN_LOAD_IMM_TN(T1
, uimm
);
752 GEN_LOAD_IMM_TN(T0
, uimm
);
756 save_cpu_state(ctx
, 1);
800 MIPS_INVAL("imm arith");
801 generate_exception(ctx
, EXCP_RI
);
804 GEN_STORE_TN_REG(rt
, T0
);
805 MIPS_DEBUG("%s %s, %s, %x", opn
, regnames
[rt
], regnames
[rs
], uimm
);
809 static void gen_arith (DisasContext
*ctx
, uint16_t opc
,
810 int rd
, int rs
, int rt
)
812 const unsigned char *opn
= "unk";
814 if (rd
== 0 && opc
!= OPC_ADD
&& opc
!= OPC_SUB
) {
815 /* if no destination, treat it as a NOP
816 * For add & sub, we must generate the overflow exception when needed.
821 GEN_LOAD_REG_TN(T0
, rs
);
822 GEN_LOAD_REG_TN(T1
, rt
);
825 save_cpu_state(ctx
, 1);
834 save_cpu_state(ctx
, 1);
892 generate_exception(ctx
, EXCP_RI
);
895 GEN_STORE_TN_REG(rd
, T0
);
897 MIPS_DEBUG("%s %s, %s, %s", opn
, regnames
[rd
], regnames
[rs
], regnames
[rt
]);
900 /* Arithmetic on HI/LO registers */
901 static void gen_HILO (DisasContext
*ctx
, uint16_t opc
, int reg
)
903 const unsigned char *opn
= "unk";
905 if (reg
== 0 && (opc
== OPC_MFHI
|| opc
== OPC_MFLO
)) {
913 GEN_STORE_TN_REG(reg
, T0
);
918 GEN_STORE_TN_REG(reg
, T0
);
922 GEN_LOAD_REG_TN(T0
, reg
);
927 GEN_LOAD_REG_TN(T0
, reg
);
933 generate_exception(ctx
, EXCP_RI
);
936 MIPS_DEBUG("%s %s", opn
, regnames
[reg
]);
939 static void gen_muldiv (DisasContext
*ctx
, uint16_t opc
,
942 const unsigned char *opn
= "unk";
944 GEN_LOAD_REG_TN(T0
, rs
);
945 GEN_LOAD_REG_TN(T1
, rt
);
980 MIPS_INVAL("mul/div");
981 generate_exception(ctx
, EXCP_RI
);
984 MIPS_DEBUG("%s %s %s", opn
, regnames
[rs
], regnames
[rt
]);
987 static void gen_cl (DisasContext
*ctx
, uint16_t opc
,
990 const unsigned char *opn
= "unk";
996 GEN_LOAD_REG_TN(T0
, rs
);
1010 generate_exception(ctx
, EXCP_RI
);
1013 gen_op_store_T0_gpr(rd
);
1014 MIPS_DEBUG("%s %s, %s", opn
, regnames
[rd
], regnames
[rs
]);
1018 static void gen_trap (DisasContext
*ctx
, uint16_t opc
,
1019 int rs
, int rt
, int16_t imm
)
1024 /* Load needed operands */
1032 /* Compare two registers */
1034 GEN_LOAD_REG_TN(T0
, rs
);
1035 GEN_LOAD_REG_TN(T1
, rt
);
1044 /* Compare register to immediate */
1045 if (rs
!= 0 || imm
!= 0) {
1046 GEN_LOAD_REG_TN(T0
, rs
);
1047 GEN_LOAD_IMM_TN(T1
, (int32_t)imm
);
1054 case OPC_TEQ
: /* rs == rs */
1055 case OPC_TEQI
: /* r0 == 0 */
1056 case OPC_TGE
: /* rs >= rs */
1057 case OPC_TGEI
: /* r0 >= 0 */
1058 case OPC_TGEU
: /* rs >= rs unsigned */
1059 case OPC_TGEIU
: /* r0 >= 0 unsigned */
1063 case OPC_TLT
: /* rs < rs */
1064 case OPC_TLTI
: /* r0 < 0 */
1065 case OPC_TLTU
: /* rs < rs unsigned */
1066 case OPC_TLTIU
: /* r0 < 0 unsigned */
1067 case OPC_TNE
: /* rs != rs */
1068 case OPC_TNEI
: /* r0 != 0 */
1069 /* Never trap: treat as NOP */
1073 generate_exception(ctx
, EXCP_RI
);
1104 generate_exception(ctx
, EXCP_RI
);
1108 save_cpu_state(ctx
, 1);
1110 ctx
->bstate
= BS_STOP
;
1113 static inline void gen_goto_tb(DisasContext
*ctx
, int n
, target_ulong dest
)
1115 TranslationBlock
*tb
;
1117 if ((tb
->pc
& TARGET_PAGE_MASK
) == (dest
& TARGET_PAGE_MASK
)) {
1119 gen_op_goto_tb0(TBPARAM(tb
));
1121 gen_op_goto_tb1(TBPARAM(tb
));
1122 gen_op_save_pc(dest
);
1123 gen_op_set_T0((long)tb
+ n
);
1126 gen_op_save_pc(dest
);
1132 /* Branches (before delay slot) */
1133 static void gen_compute_branch (DisasContext
*ctx
, uint16_t opc
,
1134 int rs
, int rt
, int32_t offset
)
1136 target_ulong btarget
;
1142 /* Load needed operands */
1148 /* Compare two registers */
1150 GEN_LOAD_REG_TN(T0
, rs
);
1151 GEN_LOAD_REG_TN(T1
, rt
);
1154 btarget
= ctx
->pc
+ 4 + offset
;
1168 /* Compare to zero */
1170 gen_op_load_gpr_T0(rs
);
1173 btarget
= ctx
->pc
+ 4 + offset
;
1177 /* Jump to immediate */
1178 btarget
= ((ctx
->pc
+ 4) & 0xF0000000) | offset
;
1182 /* Jump to register */
1184 /* Only hint = 0 is valid */
1185 generate_exception(ctx
, EXCP_RI
);
1188 GEN_LOAD_REG_TN(T2
, rs
);
1191 MIPS_INVAL("branch/jump");
1192 generate_exception(ctx
, EXCP_RI
);
1196 /* No condition to be computed */
1198 case OPC_BEQ
: /* rx == rx */
1199 case OPC_BEQL
: /* rx == rx likely */
1200 case OPC_BGEZ
: /* 0 >= 0 */
1201 case OPC_BGEZL
: /* 0 >= 0 likely */
1202 case OPC_BLEZ
: /* 0 <= 0 */
1203 case OPC_BLEZL
: /* 0 <= 0 likely */
1205 ctx
->hflags
|= MIPS_HFLAG_B
;
1206 MIPS_DEBUG("balways");
1208 case OPC_BGEZAL
: /* 0 >= 0 */
1209 case OPC_BGEZALL
: /* 0 >= 0 likely */
1210 /* Always take and link */
1212 ctx
->hflags
|= MIPS_HFLAG_B
;
1213 MIPS_DEBUG("balways and link");
1215 case OPC_BNE
: /* rx != rx */
1216 case OPC_BGTZ
: /* 0 > 0 */
1217 case OPC_BLTZ
: /* 0 < 0 */
1218 /* Treated as NOP */
1219 MIPS_DEBUG("bnever (NOP)");
1221 case OPC_BLTZAL
: /* 0 < 0 */
1222 gen_op_set_T0(ctx
->pc
+ 8);
1223 gen_op_store_T0_gpr(31);
1225 case OPC_BLTZALL
: /* 0 < 0 likely */
1226 gen_op_set_T0(ctx
->pc
+ 8);
1227 gen_op_store_T0_gpr(31);
1228 gen_goto_tb(ctx
, 0, ctx
->pc
+ 4);
1230 case OPC_BNEL
: /* rx != rx likely */
1231 case OPC_BGTZL
: /* 0 > 0 likely */
1232 case OPC_BLTZL
: /* 0 < 0 likely */
1233 /* Skip the instruction in the delay slot */
1234 MIPS_DEBUG("bnever and skip");
1235 gen_goto_tb(ctx
, 0, ctx
->pc
+ 4);
1238 ctx
->hflags
|= MIPS_HFLAG_B
;
1239 MIPS_DEBUG("j %08x", btarget
);
1243 ctx
->hflags
|= MIPS_HFLAG_B
;
1244 MIPS_DEBUG("jal %08x", btarget
);
1247 ctx
->hflags
|= MIPS_HFLAG_BR
;
1248 MIPS_DEBUG("jr %s", regnames
[rs
]);
1252 ctx
->hflags
|= MIPS_HFLAG_BR
;
1253 MIPS_DEBUG("jalr %s, %s", regnames
[rt
], regnames
[rs
]);
1256 MIPS_INVAL("branch/jump");
1257 generate_exception(ctx
, EXCP_RI
);
1264 MIPS_DEBUG("beq %s, %s, %08x",
1265 regnames
[rs
], regnames
[rt
], btarget
);
1269 MIPS_DEBUG("beql %s, %s, %08x",
1270 regnames
[rs
], regnames
[rt
], btarget
);
1274 MIPS_DEBUG("bne %s, %s, %08x",
1275 regnames
[rs
], regnames
[rt
], btarget
);
1279 MIPS_DEBUG("bnel %s, %s, %08x",
1280 regnames
[rs
], regnames
[rt
], btarget
);
1284 MIPS_DEBUG("bgez %s, %08x", regnames
[rs
], btarget
);
1288 MIPS_DEBUG("bgezl %s, %08x", regnames
[rs
], btarget
);
1292 MIPS_DEBUG("bgezal %s, %08x", regnames
[rs
], btarget
);
1298 MIPS_DEBUG("bgezall %s, %08x", regnames
[rs
], btarget
);
1302 MIPS_DEBUG("bgtz %s, %08x", regnames
[rs
], btarget
);
1306 MIPS_DEBUG("bgtzl %s, %08x", regnames
[rs
], btarget
);
1310 MIPS_DEBUG("blez %s, %08x", regnames
[rs
], btarget
);
1314 MIPS_DEBUG("blezl %s, %08x", regnames
[rs
], btarget
);
1318 MIPS_DEBUG("bltz %s, %08x", regnames
[rs
], btarget
);
1322 MIPS_DEBUG("bltzl %s, %08x", regnames
[rs
], btarget
);
1327 MIPS_DEBUG("bltzal %s, %08x", regnames
[rs
], btarget
);
1329 ctx
->hflags
|= MIPS_HFLAG_BC
;
1334 MIPS_DEBUG("bltzall %s, %08x", regnames
[rs
], btarget
);
1336 ctx
->hflags
|= MIPS_HFLAG_BL
;
1341 MIPS_DEBUG("enter ds: link %d cond %02x target %08x",
1342 blink
, ctx
->hflags
, btarget
);
1343 ctx
->btarget
= btarget
;
1345 gen_op_set_T0(ctx
->pc
+ 8);
1346 gen_op_store_T0_gpr(blink
);
1351 /* CP0 (MMU and control) */
1352 static void gen_mfc0 (DisasContext
*ctx
, int reg
, int sel
)
1354 const unsigned char *rn
;
1356 if (sel
!= 0 && reg
!= 16 && reg
!= 28) {
1362 gen_op_mfc0_index();
1366 gen_op_mfc0_random();
1370 gen_op_mfc0_entrylo0();
1374 gen_op_mfc0_entrylo1();
1378 gen_op_mfc0_context();
1382 gen_op_mfc0_pagemask();
1386 gen_op_mfc0_wired();
1390 gen_op_mfc0_badvaddr();
1394 gen_op_mfc0_count();
1398 gen_op_mfc0_entryhi();
1402 gen_op_mfc0_compare();
1406 gen_op_mfc0_status();
1410 gen_op_mfc0_cause();
1424 gen_op_mfc0_config0();
1428 gen_op_mfc0_config1();
1432 rn
= "Unknown config register";
1437 gen_op_mfc0_lladdr();
1441 gen_op_mfc0_watchlo();
1445 gen_op_mfc0_watchhi();
1449 gen_op_mfc0_debug();
1459 gen_op_mfc0_taglo();
1463 gen_op_mfc0_datalo();
1472 gen_op_mfc0_errorepc();
1476 gen_op_mfc0_desave();
1483 #if defined MIPS_DEBUG_DISAS
1484 if (loglevel
& CPU_LOG_TB_IN_ASM
) {
1485 fprintf(logfile
, "%08x mfc0 %s => %08x (%d %d)\n",
1486 env
->PC
, rn
, T0
, reg
, sel
);
1492 #if defined MIPS_DEBUG_DISAS
1493 if (loglevel
& CPU_LOG_TB_IN_ASM
) {
1494 fprintf(logfile
, "%08x mfc0 %s => %08x (%d %d)\n",
1495 env
->PC
, rn
, T0
, reg
, sel
);
1498 generate_exception(ctx
, EXCP_RI
);
1501 static void gen_cp0 (DisasContext
*ctx
, uint16_t opc
, int rt
, int rd
)
1503 const unsigned char *opn
= "unk";
1505 if (!(ctx
->CP0_Status
& (1 << CP0St_CU0
)) &&
1506 (ctx
->hflags
& MIPS_HFLAG_UM
) &&
1507 !(ctx
->hflags
& MIPS_HFLAG_ERL
) &&
1508 !(ctx
->hflags
& MIPS_HFLAG_EXL
)) {
1509 if (loglevel
& CPU_LOG_TB_IN_ASM
) {
1510 fprintf(logfile
, "CP0 is not usable\n");
1512 generate_exception_err (ctx
, EXCP_CpU
, 0);
1522 gen_mfc0(ctx
, rd
, ctx
->opcode
& 0x7);
1523 gen_op_store_T0_gpr(rt
);
1527 /* If we get an exception, we want to restart at next instruction */
1529 save_cpu_state(ctx
, 1);
1531 GEN_LOAD_REG_TN(T0
, rt
);
1532 gen_op_mtc0(rd
, ctx
->opcode
& 0x7);
1533 /* Stop translation as we may have switched the execution mode */
1534 ctx
->bstate
= BS_STOP
;
1537 #if defined(MIPS_USES_R4K_TLB)
1557 save_cpu_state(ctx
, 0);
1559 ctx
->bstate
= BS_EXCP
;
1563 if (!(ctx
->hflags
& MIPS_HFLAG_DM
)) {
1564 generate_exception(ctx
, EXCP_RI
);
1566 save_cpu_state(ctx
, 0);
1568 ctx
->bstate
= BS_EXCP
;
1573 /* If we get an exception, we want to restart at next instruction */
1575 save_cpu_state(ctx
, 1);
1578 ctx
->bstate
= BS_EXCP
;
1581 if (loglevel
& CPU_LOG_TB_IN_ASM
) {
1582 fprintf(logfile
, "Invalid CP0 opcode: %08x %03x %03x %03x\n",
1583 ctx
->opcode
, ctx
->opcode
>> 26, ctx
->opcode
& 0x3F,
1584 ((ctx
->opcode
>> 16) & 0x1F));
1586 generate_exception(ctx
, EXCP_RI
);
1589 MIPS_DEBUG("%s %s %d", opn
, regnames
[rt
], rd
);
1592 #ifdef MIPS_USES_FPU
1593 /* CP1 Branches (before delay slot) */
1594 static void gen_compute_branch1 (DisasContext
*ctx
, uint16_t cond
,
1597 target_ulong btarget
;
1599 btarget
= ctx
->pc
+ 4 + offset
;
1602 case 0x0000: /* bc1f */
1604 MIPS_DEBUG("bc1f %08x", btarget
);
1606 case 0x0002: /* bc1fl */
1608 MIPS_DEBUG("bc1fl %08x", btarget
);
1610 case 0x0001: /* bc1t */
1612 MIPS_DEBUG("bc1t %08x", btarget
);
1614 ctx
->hflags
|= MIPS_HFLAG_BC
;
1616 case 0x0003: /* bc1tl */
1618 MIPS_DEBUG("bc1tl %08x", btarget
);
1620 ctx
->hflags
|= MIPS_HFLAG_BL
;
1623 MIPS_INVAL("cp1 branch/jump");
1624 generate_exception(ctx
, EXCP_RI
);
1629 MIPS_DEBUG("enter ds: cond %02x target %08x",
1630 ctx
->hflags
, btarget
);
1631 ctx
->btarget
= btarget
;
1636 /* Coprocessor 1 (FPU) */
1637 static void gen_cp1 (DisasContext
*ctx
, uint16_t opc
, int rt
, int fs
)
1639 const unsigned char *opn
= "unk";
1643 GEN_LOAD_FREG_FTN(WT0
, fs
);
1645 GEN_STORE_TN_REG(rt
, T0
);
1649 GEN_LOAD_REG_TN(T0
, rt
);
1651 GEN_STORE_FTN_FREG(fs
, WT0
);
1655 if (fs
!= 0 && fs
!= 31) {
1656 MIPS_INVAL("cfc1 freg");
1657 generate_exception(ctx
, EXCP_RI
);
1660 GEN_LOAD_IMM_TN(T1
, fs
);
1662 GEN_STORE_TN_REG(rt
, T0
);
1666 if (fs
!= 0 && fs
!= 31) {
1667 MIPS_INVAL("ctc1 freg");
1668 generate_exception(ctx
, EXCP_RI
);
1671 GEN_LOAD_IMM_TN(T1
, fs
);
1672 GEN_LOAD_REG_TN(T0
, rt
);
1677 if (loglevel
& CPU_LOG_TB_IN_ASM
) {
1678 fprintf(logfile
, "Invalid CP1 opcode: %08x %03x %03x %03x\n",
1679 ctx
->opcode
, ctx
->opcode
>> 26, ctx
->opcode
& 0x3F,
1680 ((ctx
->opcode
>> 16) & 0x1F));
1682 generate_exception(ctx
, EXCP_RI
);
1685 MIPS_DEBUG("%s %s %s", opn
, regnames
[rt
], fregnames
[fs
]);
1688 /* verify if floating point register is valid; an operation is not defined
1689 * if bit 0 of any register specification is set and the FR bit in the
1690 * Status register equals zero, since the register numbers specify an
1691 * even-odd pair of adjacent coprocessor general registers. When the FR bit
1692 * in the Status register equals one, both even and odd register numbers
1695 * Multiple float registers can be checked by calling
1696 * CHECK_FR(ctx, freg1 | freg2 | ... | fregN);
1698 #define CHECK_FR(ctx, freg) do { \
1699 if (!((ctx)->CP0_Status & (1<<CP0St_FR)) && ((freg) & 1)) { \
1700 generate_exception(ctx, EXCP_RI); \
1705 #define FOP(func, fmt) (((fmt) << 21) | (func))
1707 static void gen_farith (DisasContext
*ctx
, int fmt
, int ft
, int fs
, int fd
, int func
)
1709 const unsigned char *opn
= "unk";
1710 const char *condnames
[] = {
1730 switch (ctx
->opcode
& FOP(0x3f, 0x1f)) {
1732 CHECK_FR(ctx
, fs
| ft
| fd
);
1733 GEN_LOAD_FREG_FTN(DT0
, fs
);
1734 GEN_LOAD_FREG_FTN(DT1
, ft
);
1735 gen_op_float_add_d();
1736 GEN_STORE_FTN_FREG(fd
, DT2
);
1741 CHECK_FR(ctx
, fs
| ft
| fd
);
1742 GEN_LOAD_FREG_FTN(DT0
, fs
);
1743 GEN_LOAD_FREG_FTN(DT1
, ft
);
1744 gen_op_float_sub_d();
1745 GEN_STORE_FTN_FREG(fd
, DT2
);
1750 CHECK_FR(ctx
, fs
| ft
| fd
);
1751 GEN_LOAD_FREG_FTN(DT0
, fs
);
1752 GEN_LOAD_FREG_FTN(DT1
, ft
);
1753 gen_op_float_mul_d();
1754 GEN_STORE_FTN_FREG(fd
, DT2
);
1759 CHECK_FR(ctx
, fs
| ft
| fd
);
1760 GEN_LOAD_FREG_FTN(DT0
, fs
);
1761 GEN_LOAD_FREG_FTN(DT1
, ft
);
1762 gen_op_float_div_d();
1763 GEN_STORE_FTN_FREG(fd
, DT2
);
1768 CHECK_FR(ctx
, fs
| fd
);
1769 GEN_LOAD_FREG_FTN(DT0
, fs
);
1770 gen_op_float_sqrt_d();
1771 GEN_STORE_FTN_FREG(fd
, DT2
);
1775 CHECK_FR(ctx
, fs
| fd
);
1776 GEN_LOAD_FREG_FTN(DT0
, fs
);
1777 gen_op_float_abs_d();
1778 GEN_STORE_FTN_FREG(fd
, DT2
);
1782 CHECK_FR(ctx
, fs
| fd
);
1783 GEN_LOAD_FREG_FTN(DT0
, fs
);
1784 gen_op_float_mov_d();
1785 GEN_STORE_FTN_FREG(fd
, DT2
);
1789 CHECK_FR(ctx
, fs
| fd
);
1790 GEN_LOAD_FREG_FTN(DT0
, fs
);
1791 gen_op_float_chs_d();
1792 GEN_STORE_FTN_FREG(fd
, DT2
);
1800 CHECK_FR(ctx
, fs
| fd
);
1801 GEN_LOAD_FREG_FTN(DT0
, fs
);
1802 gen_op_float_roundw_d();
1803 GEN_STORE_FTN_FREG(fd
, WT2
);
1807 CHECK_FR(ctx
, fs
| fd
);
1808 GEN_LOAD_FREG_FTN(DT0
, fs
);
1809 gen_op_float_truncw_d();
1810 GEN_STORE_FTN_FREG(fd
, WT2
);
1814 CHECK_FR(ctx
, fs
| fd
);
1815 GEN_LOAD_FREG_FTN(DT0
, fs
);
1816 gen_op_float_ceilw_d();
1817 GEN_STORE_FTN_FREG(fd
, WT2
);
1821 CHECK_FR(ctx
, fs
| fd
);
1822 GEN_LOAD_FREG_FTN(DT0
, fs
);
1823 gen_op_float_floorw_d();
1824 GEN_STORE_FTN_FREG(fd
, WT2
);
1827 case FOP(33, 16): /* cvt.d.s */
1828 CHECK_FR(ctx
, fs
| fd
);
1829 GEN_LOAD_FREG_FTN(WT0
, fs
);
1830 gen_op_float_cvtd_s();
1831 GEN_STORE_FTN_FREG(fd
, DT2
);
1834 case FOP(33, 20): /* cvt.d.w */
1835 CHECK_FR(ctx
, fs
| fd
);
1836 GEN_LOAD_FREG_FTN(WT0
, fs
);
1837 gen_op_float_cvtd_w();
1838 GEN_STORE_FTN_FREG(fd
, DT2
);
1857 CHECK_FR(ctx
, fs
| ft
);
1858 GEN_LOAD_FREG_FTN(DT0
, fs
);
1859 GEN_LOAD_FREG_FTN(DT1
, ft
);
1861 opn
= condnames
[func
-48];
1864 CHECK_FR(ctx
, fs
| ft
| fd
);
1865 GEN_LOAD_FREG_FTN(WT0
, fs
);
1866 GEN_LOAD_FREG_FTN(WT1
, ft
);
1867 gen_op_float_add_s();
1868 GEN_STORE_FTN_FREG(fd
, WT2
);
1873 CHECK_FR(ctx
, fs
| ft
| fd
);
1874 GEN_LOAD_FREG_FTN(WT0
, fs
);
1875 GEN_LOAD_FREG_FTN(WT1
, ft
);
1876 gen_op_float_sub_s();
1877 GEN_STORE_FTN_FREG(fd
, WT2
);
1882 CHECK_FR(ctx
, fs
| ft
| fd
);
1883 GEN_LOAD_FREG_FTN(WT0
, fs
);
1884 GEN_LOAD_FREG_FTN(WT1
, ft
);
1885 gen_op_float_mul_s();
1886 GEN_STORE_FTN_FREG(fd
, WT2
);
1891 CHECK_FR(ctx
, fs
| ft
| fd
);
1892 GEN_LOAD_FREG_FTN(WT0
, fs
);
1893 GEN_LOAD_FREG_FTN(WT1
, ft
);
1894 gen_op_float_div_s();
1895 GEN_STORE_FTN_FREG(fd
, WT2
);
1900 CHECK_FR(ctx
, fs
| fd
);
1901 GEN_LOAD_FREG_FTN(WT0
, fs
);
1902 gen_op_float_sqrt_s();
1903 GEN_STORE_FTN_FREG(fd
, WT2
);
1907 CHECK_FR(ctx
, fs
| fd
);
1908 GEN_LOAD_FREG_FTN(WT0
, fs
);
1909 gen_op_float_abs_s();
1910 GEN_STORE_FTN_FREG(fd
, WT2
);
1914 CHECK_FR(ctx
, fs
| fd
);
1915 GEN_LOAD_FREG_FTN(WT0
, fs
);
1916 gen_op_float_mov_s();
1917 GEN_STORE_FTN_FREG(fd
, WT2
);
1921 CHECK_FR(ctx
, fs
| fd
);
1922 GEN_LOAD_FREG_FTN(WT0
, fs
);
1923 gen_op_float_chs_s();
1924 GEN_STORE_FTN_FREG(fd
, WT2
);
1928 CHECK_FR(ctx
, fs
| fd
);
1929 GEN_LOAD_FREG_FTN(WT0
, fs
);
1930 gen_op_float_roundw_s();
1931 GEN_STORE_FTN_FREG(fd
, WT2
);
1935 CHECK_FR(ctx
, fs
| fd
);
1936 GEN_LOAD_FREG_FTN(WT0
, fs
);
1937 gen_op_float_truncw_s();
1938 GEN_STORE_FTN_FREG(fd
, WT2
);
1941 case FOP(32, 17): /* cvt.s.d */
1942 CHECK_FR(ctx
, fs
| fd
);
1943 GEN_LOAD_FREG_FTN(DT0
, fs
);
1944 gen_op_float_cvts_d();
1945 GEN_STORE_FTN_FREG(fd
, WT2
);
1948 case FOP(32, 20): /* cvt.s.w */
1949 CHECK_FR(ctx
, fs
| fd
);
1950 GEN_LOAD_FREG_FTN(WT0
, fs
);
1951 gen_op_float_cvts_w();
1952 GEN_STORE_FTN_FREG(fd
, WT2
);
1955 case FOP(36, 16): /* cvt.w.s */
1956 CHECK_FR(ctx
, fs
| fd
);
1957 GEN_LOAD_FREG_FTN(WT0
, fs
);
1958 gen_op_float_cvtw_s();
1959 GEN_STORE_FTN_FREG(fd
, WT2
);
1962 case FOP(36, 17): /* cvt.w.d */
1963 CHECK_FR(ctx
, fs
| fd
);
1964 GEN_LOAD_FREG_FTN(DT0
, fs
);
1965 gen_op_float_cvtw_d();
1966 GEN_STORE_FTN_FREG(fd
, WT2
);
1985 CHECK_FR(ctx
, fs
| ft
);
1986 GEN_LOAD_FREG_FTN(WT0
, fs
);
1987 GEN_LOAD_FREG_FTN(WT1
, ft
);
1989 opn
= condnames
[func
-48];
1992 if (loglevel
& CPU_LOG_TB_IN_ASM
) {
1993 fprintf(logfile
, "Invalid arith function: %08x %03x %03x %03x\n",
1994 ctx
->opcode
, ctx
->opcode
>> 26, ctx
->opcode
& 0x3F,
1995 ((ctx
->opcode
>> 16) & 0x1F));
1997 generate_exception(ctx
, EXCP_RI
);
2001 MIPS_DEBUG("%s %s, %s, %s", opn
, fregnames
[fd
], fregnames
[fs
], fregnames
[ft
]);
2003 MIPS_DEBUG("%s %s,%s", opn
, fregnames
[fd
], fregnames
[fs
]);
2007 /* ISA extensions */
2008 /* MIPS16 extension to MIPS32 */
2009 /* SmartMIPS extension to MIPS32 */
2011 #ifdef TARGET_MIPS64
2012 static void gen_arith64 (DisasContext
*ctx
, uint16_t opc
)
2014 if (func
== 0x02 && rd
== 0) {
2018 if (rs
== 0 || rt
== 0) {
2022 gen_op_load_gpr_T0(rs
);
2023 gen_op_load_gpr_T1(rt
);
2036 /* Coprocessor 3 (FPU) */
2038 /* MDMX extension to MIPS64 */
2039 /* MIPS-3D extension to MIPS64 */
2043 static void gen_blikely(DisasContext
*ctx
)
2046 l1
= gen_new_label();
2048 gen_op_save_state(ctx
->hflags
& ~MIPS_HFLAG_BMASK
);
2049 gen_goto_tb(ctx
, 1, ctx
->pc
+ 4);
2053 static void decode_opc (DisasContext
*ctx
)
2060 /* make sure instructions are on a word boundary */
2061 if (ctx
->pc
& 0x3) {
2062 generate_exception(ctx
, EXCP_AdEL
);
2066 if ((ctx
->hflags
& MIPS_HFLAG_BMASK
) == MIPS_HFLAG_BL
) {
2067 /* Handle blikely not taken case */
2068 MIPS_DEBUG("blikely condition (%08x)", ctx
->pc
+ 4);
2071 op
= ctx
->opcode
>> 26;
2072 rs
= ((ctx
->opcode
>> 21) & 0x1F);
2073 rt
= ((ctx
->opcode
>> 16) & 0x1F);
2074 rd
= ((ctx
->opcode
>> 11) & 0x1F);
2075 sa
= ((ctx
->opcode
>> 6) & 0x1F);
2076 imm
= (int16_t)ctx
->opcode
;
2078 case 0x00: /* Special opcode */
2079 op1
= ctx
->opcode
& 0x3F;
2081 case 0x00: /* Arithmetic with immediate */
2083 gen_arith_imm(ctx
, op1
| EXT_SPECIAL
, rd
, rt
, sa
);
2085 case 0x04: /* Arithmetic */
2090 gen_arith(ctx
, op1
| EXT_SPECIAL
, rd
, rs
, rt
);
2092 case 0x18 ... 0x1B: /* MULT / DIV */
2093 gen_muldiv(ctx
, op1
| EXT_SPECIAL
, rs
, rt
);
2095 case 0x08 ... 0x09: /* Jumps */
2096 gen_compute_branch(ctx
, op1
| EXT_SPECIAL
, rs
, rd
, sa
);
2098 case 0x30 ... 0x34: /* Traps */
2100 gen_trap(ctx
, op1
| EXT_SPECIAL
, rs
, rt
, -1);
2102 case 0x10: /* Move from HI/LO */
2104 gen_HILO(ctx
, op1
| EXT_SPECIAL
, rd
);
2107 case 0x13: /* Move to HI/LO */
2108 gen_HILO(ctx
, op1
| EXT_SPECIAL
, rs
);
2110 case 0x0C: /* SYSCALL */
2111 generate_exception(ctx
, EXCP_SYSCALL
);
2113 case 0x0D: /* BREAK */
2114 generate_exception(ctx
, EXCP_BREAK
);
2116 case 0x0F: /* SYNC */
2117 /* Treat as a noop */
2119 case 0x05: /* Pmon entry point */
2120 gen_op_pmon((ctx
->opcode
>> 6) & 0x1F);
2123 case 0x01: /* MOVCI */
2124 #if defined (MIPS_HAS_MOVCI)
2127 /* Not implemented */
2128 generate_exception_err (ctx
, EXCP_CpU
, 1);
2132 #if defined (TARGET_MIPS64)
2133 case 0x14: /* MIPS64 specific opcodes */
2142 default: /* Invalid */
2143 MIPS_INVAL("special");
2144 generate_exception(ctx
, EXCP_RI
);
2148 case 0x1C: /* Special2 opcode */
2149 op1
= ctx
->opcode
& 0x3F;
2151 #if defined (MIPS_USES_R4K_EXT)
2152 /* Those instructions are not part of MIPS32 core */
2153 case 0x00 ... 0x01: /* Multiply and add/sub */
2155 gen_muldiv(ctx
, op1
| EXT_SPECIAL2
, rs
, rt
);
2157 case 0x02: /* MUL */
2158 gen_arith(ctx
, op1
| EXT_SPECIAL2
, rd
, rs
, rt
);
2160 case 0x20 ... 0x21: /* CLO / CLZ */
2161 gen_cl(ctx
, op1
| EXT_SPECIAL2
, rd
, rs
);
2164 case 0x3F: /* SDBBP */
2165 /* XXX: not clear which exception should be raised
2166 * when in debug mode...
2168 if (!(ctx
->hflags
& MIPS_HFLAG_DM
)) {
2169 generate_exception(ctx
, EXCP_DBp
);
2171 generate_exception(ctx
, EXCP_DBp
);
2173 /* Treat as a noop */
2175 default: /* Invalid */
2176 MIPS_INVAL("special2");
2177 generate_exception(ctx
, EXCP_RI
);
2181 case 0x01: /* B REGIMM opcode */
2182 op1
= ((ctx
->opcode
>> 16) & 0x1F);
2184 case 0x00 ... 0x03: /* REGIMM branches */
2186 gen_compute_branch(ctx
, op1
| EXT_REGIMM
, rs
, -1, imm
<< 2);
2188 case 0x08 ... 0x0C: /* Traps */
2190 gen_trap(ctx
, op1
| EXT_REGIMM
, rs
, -1, imm
);
2192 default: /* Invalid */
2193 MIPS_INVAL("REGIMM");
2194 generate_exception(ctx
, EXCP_RI
);
2198 case 0x10: /* CP0 opcode */
2199 op1
= ((ctx
->opcode
>> 21) & 0x1F);
2203 gen_cp0(ctx
, op1
| EXT_CP0
, rt
, rd
);
2206 gen_cp0(ctx
, (ctx
->opcode
& 0x3F) | EXT_CP0
, rt
, rd
);
2210 case 0x08 ... 0x0F: /* Arithmetic with immediate opcode */
2211 gen_arith_imm(ctx
, op
, rt
, rs
, imm
);
2213 case 0x02 ... 0x03: /* Jump */
2214 offset
= (int32_t)(ctx
->opcode
& 0x03FFFFFF) << 2;
2215 gen_compute_branch(ctx
, op
, rs
, rt
, offset
);
2217 case 0x04 ... 0x07: /* Branch */
2219 gen_compute_branch(ctx
, op
, rs
, rt
, imm
<< 2);
2221 case 0x20 ... 0x2E: /* Load and stores */
2224 gen_ldst(ctx
, op
, rt
, rs
, imm
);
2226 case 0x2F: /* Cache operation */
2227 /* Treat as a noop */
2229 case 0x33: /* Prefetch */
2230 /* Treat as a noop */
2232 case 0x3F: /* HACK */
2235 /* Floating point. */
2236 case 0x31: /* LWC1 */
2237 case 0x35: /* LDC1 */
2238 case 0x39: /* SWC1 */
2239 case 0x3D: /* SDC1 */
2240 #if defined(MIPS_USES_FPU)
2241 save_cpu_state(ctx
, 1);
2242 gen_op_cp1_enabled();
2243 gen_flt_ldst(ctx
, op
, rt
, rs
, imm
);
2245 generate_exception_err(ctx
, EXCP_CpU
, 1);
2249 case 0x11: /* CP1 opcode */
2250 #if defined(MIPS_USES_FPU)
2251 save_cpu_state(ctx
, 1);
2252 gen_op_cp1_enabled();
2253 op1
= ((ctx
->opcode
>> 21) & 0x1F);
2255 case 0x00: /* mfc1 */
2256 case 0x02: /* cfc1 */
2257 case 0x04: /* mtc1 */
2258 case 0x06: /* ctc1 */
2259 gen_cp1(ctx
, op1
| EXT_CP1
, rt
, rd
);
2262 gen_compute_branch1(ctx
, rt
, imm
<< 2);
2264 case 0x10: /* 16: fmt=single fp */
2265 case 0x11: /* 17: fmt=double fp */
2266 case 0x14: /* 20: fmt=32bit fixed */
2267 case 0x15: /* 21: fmt=64bit fixed */
2268 gen_farith(ctx
, op1
, rt
, rd
, sa
, ctx
->opcode
& 0x3f);
2271 generate_exception_err(ctx
, EXCP_RI
, 1);
2276 generate_exception_err(ctx
, EXCP_CpU
, 1);
2281 case 0x32: /* LWC2 */
2282 case 0x36: /* LDC2 */
2283 case 0x3A: /* SWC2 */
2284 case 0x3E: /* SDC2 */
2285 case 0x12: /* CP2 opcode */
2286 /* Not implemented */
2287 generate_exception_err(ctx
, EXCP_CpU
, 2);
2290 case 0x13: /* CP3 opcode */
2291 /* Not implemented */
2292 generate_exception_err(ctx
, EXCP_CpU
, 3);
2295 #if defined (TARGET_MIPS64)
2300 /* MIPS64 opcodes */
2302 #if defined (MIPS_HAS_JALX)
2304 /* JALX: not implemented */
2308 default: /* Invalid */
2310 generate_exception(ctx
, EXCP_RI
);
2313 if (ctx
->hflags
& MIPS_HFLAG_BMASK
) {
2314 int hflags
= ctx
->hflags
;
2315 /* Branches completion */
2316 ctx
->hflags
&= ~MIPS_HFLAG_BMASK
;
2317 ctx
->bstate
= BS_BRANCH
;
2318 save_cpu_state(ctx
, 0);
2319 switch (hflags
& MIPS_HFLAG_BMASK
) {
2321 /* unconditional branch */
2322 MIPS_DEBUG("unconditional branch");
2323 gen_goto_tb(ctx
, 0, ctx
->btarget
);
2326 /* blikely taken case */
2327 MIPS_DEBUG("blikely branch taken");
2328 gen_goto_tb(ctx
, 0, ctx
->btarget
);
2331 /* Conditional branch */
2332 MIPS_DEBUG("conditional branch");
2335 l1
= gen_new_label();
2337 gen_goto_tb(ctx
, 1, ctx
->pc
+ 4);
2339 gen_goto_tb(ctx
, 0, ctx
->btarget
);
2343 /* unconditional branch to register */
2344 MIPS_DEBUG("branch to register");
2348 MIPS_DEBUG("unknown branch");
2354 int gen_intermediate_code_internal (CPUState
*env
, TranslationBlock
*tb
,
2357 DisasContext ctx
, *ctxp
= &ctx
;
2358 target_ulong pc_start
;
2359 uint16_t *gen_opc_end
;
2362 if (search_pc
&& loglevel
)
2363 fprintf (logfile
, "search pc %d\n", search_pc
);
2366 gen_opc_ptr
= gen_opc_buf
;
2367 gen_opc_end
= gen_opc_buf
+ OPC_MAX_SIZE
;
2368 gen_opparam_ptr
= gen_opparam_buf
;
2373 ctx
.bstate
= BS_NONE
;
2374 /* Restore delay slot state from the tb context. */
2375 ctx
.hflags
= tb
->flags
;
2376 ctx
.saved_hflags
= ctx
.hflags
;
2377 if (ctx
.hflags
& MIPS_HFLAG_BR
) {
2378 gen_op_restore_breg_target();
2379 } else if (ctx
.hflags
& MIPS_HFLAG_B
) {
2380 ctx
.btarget
= env
->btarget
;
2381 } else if (ctx
.hflags
& MIPS_HFLAG_BMASK
) {
2382 /* If we are in the delay slot of a conditional branch,
2383 * restore the branch condition from env->bcond to T2
2385 ctx
.btarget
= env
->btarget
;
2386 gen_op_restore_bcond();
2388 #if defined(CONFIG_USER_ONLY)
2391 ctx
.mem_idx
= !((ctx
.hflags
& MIPS_HFLAG_MODE
) == MIPS_HFLAG_UM
);
2393 ctx
.CP0_Status
= env
->CP0_Status
;
2395 if (loglevel
& CPU_LOG_TB_CPU
) {
2396 fprintf(logfile
, "------------------------------------------------\n");
2397 /* FIXME: This may print out stale hflags from env... */
2398 cpu_dump_state(env
, logfile
, fprintf
, 0);
2401 #if defined MIPS_DEBUG_DISAS
2402 if (loglevel
& CPU_LOG_TB_IN_ASM
)
2403 fprintf(logfile
, "\ntb %p super %d cond %04x\n",
2404 tb
, ctx
.mem_idx
, ctx
.hflags
);
2406 while (ctx
.bstate
== BS_NONE
&& gen_opc_ptr
< gen_opc_end
) {
2407 if (env
->nb_breakpoints
> 0) {
2408 for(j
= 0; j
< env
->nb_breakpoints
; j
++) {
2409 if (env
->breakpoints
[j
] == ctx
.pc
) {
2410 save_cpu_state(ctxp
, 1);
2411 ctx
.bstate
= BS_BRANCH
;
2413 goto done_generating
;
2419 j
= gen_opc_ptr
- gen_opc_buf
;
2423 gen_opc_instr_start
[lj
++] = 0;
2425 gen_opc_pc
[lj
] = ctx
.pc
;
2426 gen_opc_hflags
[lj
] = ctx
.hflags
& MIPS_HFLAG_BMASK
;
2427 gen_opc_instr_start
[lj
] = 1;
2429 ctx
.opcode
= ldl_code(ctx
.pc
);
2433 if (env
->singlestep_enabled
)
2436 if ((ctx
.pc
& (TARGET_PAGE_SIZE
- 1)) == 0)
2439 #if defined (MIPS_SINGLE_STEP)
2443 if (env
->singlestep_enabled
) {
2444 save_cpu_state(ctxp
, ctx
.bstate
== BS_NONE
);
2446 goto done_generating
;
2448 else if (ctx
.bstate
!= BS_BRANCH
&& ctx
.bstate
!= BS_EXCP
) {
2449 save_cpu_state(ctxp
, 0);
2450 gen_goto_tb(&ctx
, 0, ctx
.pc
);
2453 /* Generate the return instruction */
2456 *gen_opc_ptr
= INDEX_op_end
;
2458 j
= gen_opc_ptr
- gen_opc_buf
;
2461 gen_opc_instr_start
[lj
++] = 0;
2464 tb
->size
= ctx
.pc
- pc_start
;
2467 #if defined MIPS_DEBUG_DISAS
2468 if (loglevel
& CPU_LOG_TB_IN_ASM
)
2469 fprintf(logfile
, "\n");
2471 if (loglevel
& CPU_LOG_TB_IN_ASM
) {
2472 fprintf(logfile
, "IN: %s\n", lookup_symbol(pc_start
));
2473 target_disas(logfile
, pc_start
, ctx
.pc
- pc_start
, 0);
2474 fprintf(logfile
, "\n");
2476 if (loglevel
& CPU_LOG_TB_OP
) {
2477 fprintf(logfile
, "OP:\n");
2478 dump_ops(gen_opc_buf
, gen_opparam_buf
);
2479 fprintf(logfile
, "\n");
2481 if (loglevel
& CPU_LOG_TB_CPU
) {
2482 fprintf(logfile
, "---------------- %d %08x\n", ctx
.bstate
, ctx
.hflags
);
2489 int gen_intermediate_code (CPUState
*env
, struct TranslationBlock
*tb
)
2491 return gen_intermediate_code_internal(env
, tb
, 0);
2494 int gen_intermediate_code_pc (CPUState
*env
, struct TranslationBlock
*tb
)
2496 return gen_intermediate_code_internal(env
, tb
, 1);
2499 #ifdef MIPS_USES_FPU
2500 void fpu_dump_state(CPUState
*env
, FILE *f
,
2501 int (*fpu_fprintf
)(FILE *f
, const char *fmt
, ...),
2506 # define printfpr(fp) do { \
2507 fpu_fprintf(f, "w:%08x d:%08lx%08lx fd:%g fs:%g\n", \
2508 (fp)->w[FP_ENDIAN_IDX], (fp)->w[0], (fp)->w[1], (fp)->fd, (fp)->fs[FP_ENDIAN_IDX]); \
2511 fpu_fprintf(f
, "CP1 FCR0 0x%08x FCR31 0x%08x SR.FR %d\n",
2512 env
->fcr0
, env
->fcr31
,
2513 (env
->CP0_Status
& (1<<CP0St_FR
)) != 0);
2514 fpu_fprintf(f
, "FT0: "); printfpr(&env
->ft0
);
2515 fpu_fprintf(f
, "FT1: "); printfpr(&env
->ft1
);
2516 fpu_fprintf(f
, "FT2: "); printfpr(&env
->ft2
);
2517 for(i
=0; i
< 32; i
+=2) {
2518 fpu_fprintf(f
, "f%02d: ", i
);
2519 printfpr(FPR(env
, i
));
2525 void dump_fpu(CPUState
*env
)
2528 fprintf(logfile
, "pc=0x%08x HI=0x%08x LO=0x%08x ds %04x %08x %d\n",
2529 env
->PC
, env
->HI
, env
->LO
, env
->hflags
, env
->btarget
, env
->bcond
);
2530 fpu_dump_state(env
, logfile
, fprintf
, 0);
2533 #endif /* MIPS_USES_FPU */
2535 void cpu_dump_state (CPUState
*env
, FILE *f
,
2536 int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...),
2542 cpu_fprintf(f
, "pc=0x%08x HI=0x%08x LO=0x%08x ds %04x %08x %d\n",
2543 env
->PC
, env
->HI
, env
->LO
, env
->hflags
, env
->btarget
, env
->bcond
);
2544 for (i
= 0; i
< 32; i
++) {
2546 cpu_fprintf(f
, "GPR%02d:", i
);
2547 cpu_fprintf(f
, " %s %08x", regnames
[i
], env
->gpr
[i
]);
2549 cpu_fprintf(f
, "\n");
2552 c0_status
= env
->CP0_Status
;
2553 if (env
->hflags
& MIPS_HFLAG_UM
)
2554 c0_status
|= (1 << CP0St_UM
);
2555 if (env
->hflags
& MIPS_HFLAG_ERL
)
2556 c0_status
|= (1 << CP0St_ERL
);
2557 if (env
->hflags
& MIPS_HFLAG_EXL
)
2558 c0_status
|= (1 << CP0St_EXL
);
2560 cpu_fprintf(f
, "CP0 Status 0x%08x Cause 0x%08x EPC 0x%08x\n",
2561 c0_status
, env
->CP0_Cause
, env
->CP0_EPC
);
2562 cpu_fprintf(f
, " Config0 0x%08x Config1 0x%08x LLAddr 0x%08x\n",
2563 env
->CP0_Config0
, env
->CP0_Config1
, env
->CP0_LLAddr
);
2564 #ifdef MIPS_USES_FPU
2565 fpu_dump_state(env
, f
, cpu_fprintf
, flags
);
2569 CPUMIPSState
*cpu_mips_init (void)
2573 env
= qemu_mallocz(sizeof(CPUMIPSState
));
2581 void cpu_reset (CPUMIPSState
*env
)
2583 memset(env
, 0, offsetof(CPUMIPSState
, breakpoints
));
2588 env
->PC
= 0xBFC00000;
2589 #if defined (MIPS_USES_R4K_TLB)
2590 env
->CP0_random
= MIPS_TLB_NB
- 1;
2591 env
->tlb_in_use
= MIPS_TLB_NB
;
2594 env
->CP0_Config0
= MIPS_CONFIG0
;
2595 #if defined (MIPS_CONFIG1)
2596 env
->CP0_Config1
= MIPS_CONFIG1
;
2598 #if defined (MIPS_CONFIG2)
2599 env
->CP0_Config2
= MIPS_CONFIG2
;
2601 #if defined (MIPS_CONFIG3)
2602 env
->CP0_Config3
= MIPS_CONFIG3
;
2604 env
->CP0_Status
= (1 << CP0St_CU0
) | (1 << CP0St_BEV
);
2605 env
->CP0_WatchLo
= 0;
2606 env
->hflags
= MIPS_HFLAG_ERL
;
2607 /* Count register increments in debug mode, EJTAG version 1 */
2608 env
->CP0_Debug
= (1 << CP0DB_CNT
) | (0x1 << CP0DB_VER
);
2609 env
->CP0_PRid
= MIPS_CPU
;
2610 env
->exception_index
= EXCP_NONE
;
2611 #if defined(CONFIG_USER_ONLY)
2612 env
->hflags
|= MIPS_HFLAG_UM
;
2614 #ifdef MIPS_USES_FPU
2615 env
->fcr0
= MIPS_FCR0
;