2 * CRIS emulation for qemu: main translation routines.
4 * Copyright (c) 2008 AXIS Communications AB
5 * Written by Edgar E. Iglesias.
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
24 * The condition code translation is in need of attention.
39 #include "crisv32-decode.h"
40 #include "qemu-common.h"
50 #define BUG() (gen_BUG(dc, __FILE__, __LINE__))
51 #define BUG_ON(x) ({if (x) BUG();})
55 /* Used by the decoder. */
56 #define EXTRACT_FIELD(src, start, end) \
57 (((src) >> start) & ((1 << (end - start + 1)) - 1))
59 #define CC_MASK_NZ 0xc
60 #define CC_MASK_NZV 0xe
61 #define CC_MASK_NZVC 0xf
62 #define CC_MASK_RNZV 0x10e
80 #include "gen-icount.h"
82 /* This is the state at translation time. */
83 typedef struct DisasContext
{
92 unsigned int zsize
, zzsize
;
101 int cc_size_uptodate
; /* -1 invalid or last written value. */
103 int cc_x_uptodate
; /* 1 - ccs, 2 - known | X_FLAG. 0 not uptodate. */
104 int flags_uptodate
; /* Wether or not $ccs is uptodate. */
105 int flagx_known
; /* Wether or not flags_x has the x flag known at
109 int clear_x
; /* Clear x after this insn? */
110 int cpustate_changed
;
111 unsigned int tb_flags
; /* tb dependent flags. */
116 #define JMP_INDIRECT 2
117 int jmp
; /* 0=nojmp, 1=direct, 2=indirect. */
122 struct TranslationBlock
*tb
;
123 int singlestep_enabled
;
126 static void gen_BUG(DisasContext
*dc
, const char *file
, int line
)
128 printf ("BUG: pc=%x %s %d\n", dc
->pc
, file
, line
);
129 fprintf (logfile
, "BUG: pc=%x %s %d\n", dc
->pc
, file
, line
);
130 cpu_abort(dc
->env
, "%s:%d\n", file
, line
);
133 const char *regnames
[] =
135 "$r0", "$r1", "$r2", "$r3",
136 "$r4", "$r5", "$r6", "$r7",
137 "$r8", "$r9", "$r10", "$r11",
138 "$r12", "$r13", "$sp", "$acr",
140 const char *pregnames
[] =
142 "$bz", "$vr", "$pid", "$srs",
143 "$wz", "$exs", "$eda", "$mof",
144 "$dz", "$ebp", "$erp", "$srp",
145 "$nrp", "$ccs", "$usp", "$spc",
148 /* We need this table to handle preg-moves with implicit width. */
160 #define t_gen_mov_TN_env(tn, member) \
161 _t_gen_mov_TN_env((tn), offsetof(CPUState, member))
162 #define t_gen_mov_env_TN(member, tn) \
163 _t_gen_mov_env_TN(offsetof(CPUState, member), (tn))
165 static inline void t_gen_mov_TN_reg(TCGv tn
, int r
)
168 fprintf(stderr
, "wrong register read $r%d\n", r
);
169 tcg_gen_mov_tl(tn
, cpu_R
[r
]);
171 static inline void t_gen_mov_reg_TN(int r
, TCGv tn
)
174 fprintf(stderr
, "wrong register write $r%d\n", r
);
175 tcg_gen_mov_tl(cpu_R
[r
], tn
);
178 static inline void _t_gen_mov_TN_env(TCGv tn
, int offset
)
180 if (offset
> sizeof (CPUState
))
181 fprintf(stderr
, "wrong load from env from off=%d\n", offset
);
182 tcg_gen_ld_tl(tn
, cpu_env
, offset
);
184 static inline void _t_gen_mov_env_TN(int offset
, TCGv tn
)
186 if (offset
> sizeof (CPUState
))
187 fprintf(stderr
, "wrong store to env at off=%d\n", offset
);
188 tcg_gen_st_tl(tn
, cpu_env
, offset
);
191 static inline void t_gen_mov_TN_preg(TCGv tn
, int r
)
194 fprintf(stderr
, "wrong register read $p%d\n", r
);
195 if (r
== PR_BZ
|| r
== PR_WZ
|| r
== PR_DZ
)
196 tcg_gen_mov_tl(tn
, tcg_const_tl(0));
198 tcg_gen_mov_tl(tn
, tcg_const_tl(32));
199 else if (r
== PR_EXS
) {
200 printf("read from EXS!\n");
201 tcg_gen_mov_tl(tn
, cpu_PR
[r
]);
203 else if (r
== PR_EDA
) {
204 printf("read from EDA!\n");
205 tcg_gen_mov_tl(tn
, cpu_PR
[r
]);
208 tcg_gen_mov_tl(tn
, cpu_PR
[r
]);
210 static inline void t_gen_mov_preg_TN(DisasContext
*dc
, int r
, TCGv tn
)
213 fprintf(stderr
, "wrong register write $p%d\n", r
);
214 if (r
== PR_BZ
|| r
== PR_WZ
|| r
== PR_DZ
)
216 else if (r
== PR_SRS
)
217 tcg_gen_andi_tl(cpu_PR
[r
], tn
, 3);
220 tcg_gen_helper_0_1(helper_tlb_flush_pid
, tn
);
221 else if (r
== PR_CCS
)
222 dc
->cpustate_changed
= 1;
223 tcg_gen_mov_tl(cpu_PR
[r
], tn
);
227 static inline void t_gen_raise_exception(uint32_t index
)
229 tcg_gen_helper_0_1(helper_raise_exception
, tcg_const_tl(index
));
232 static void t_gen_lsl(TCGv d
, TCGv a
, TCGv b
)
236 l1
= gen_new_label();
237 /* Speculative shift. */
238 tcg_gen_shl_tl(d
, a
, b
);
239 tcg_gen_brcondi_tl(TCG_COND_LEU
, b
, 31, l1
);
240 /* Clear dst if shift operands were to large. */
241 tcg_gen_movi_tl(d
, 0);
245 static void t_gen_lsr(TCGv d
, TCGv a
, TCGv b
)
249 l1
= gen_new_label();
250 /* Speculative shift. */
251 tcg_gen_shr_tl(d
, a
, b
);
252 tcg_gen_brcondi_tl(TCG_COND_LEU
, b
, 31, l1
);
253 /* Clear dst if shift operands were to large. */
254 tcg_gen_movi_tl(d
, 0);
258 static void t_gen_asr(TCGv d
, TCGv a
, TCGv b
)
262 l1
= gen_new_label();
263 /* Speculative shift. */
264 tcg_gen_sar_tl(d
, a
, b
);
265 tcg_gen_brcondi_tl(TCG_COND_LEU
, b
, 31, l1
);
266 /* Clear dst if shift operands were to large. */
267 tcg_gen_sar_tl(d
, a
, tcg_const_tl(30));
271 /* 64-bit signed mul, lower result in d and upper in d2. */
272 static void t_gen_muls(TCGv d
, TCGv d2
, TCGv a
, TCGv b
)
276 t0
= tcg_temp_new(TCG_TYPE_I64
);
277 t1
= tcg_temp_new(TCG_TYPE_I64
);
279 tcg_gen_ext32s_i64(t0
, a
);
280 tcg_gen_ext32s_i64(t1
, b
);
281 tcg_gen_mul_i64(t0
, t0
, t1
);
283 tcg_gen_trunc_i64_i32(d
, t0
);
284 tcg_gen_shri_i64(t0
, t0
, 32);
285 tcg_gen_trunc_i64_i32(d2
, t0
);
291 /* 64-bit unsigned muls, lower result in d and upper in d2. */
292 static void t_gen_mulu(TCGv d
, TCGv d2
, TCGv a
, TCGv b
)
296 t0
= tcg_temp_new(TCG_TYPE_I64
);
297 t1
= tcg_temp_new(TCG_TYPE_I64
);
299 tcg_gen_extu_i32_i64(t0
, a
);
300 tcg_gen_extu_i32_i64(t1
, b
);
301 tcg_gen_mul_i64(t0
, t0
, t1
);
303 tcg_gen_trunc_i64_i32(d
, t0
);
304 tcg_gen_shri_i64(t0
, t0
, 32);
305 tcg_gen_trunc_i64_i32(d2
, t0
);
311 /* 32bit branch-free binary search for counting leading zeros. */
312 static void t_gen_lz_i32(TCGv d
, TCGv x
)
316 y
= tcg_temp_new(TCG_TYPE_I32
);
317 m
= tcg_temp_new(TCG_TYPE_I32
);
318 n
= tcg_temp_new(TCG_TYPE_I32
);
321 tcg_gen_shri_i32(y
, x
, 16);
322 tcg_gen_neg_i32(y
, y
);
324 /* m = (y >> 16) & 16 */
325 tcg_gen_sari_i32(m
, y
, 16);
326 tcg_gen_andi_i32(m
, m
, 16);
329 tcg_gen_sub_i32(n
, tcg_const_i32(16), m
);
331 tcg_gen_shr_i32(x
, x
, m
);
334 tcg_gen_subi_i32(y
, x
, 0x100);
335 /* m = (y >> 16) & 8 */
336 tcg_gen_sari_i32(m
, y
, 16);
337 tcg_gen_andi_i32(m
, m
, 8);
339 tcg_gen_add_i32(n
, n
, m
);
341 tcg_gen_shl_i32(x
, x
, m
);
344 tcg_gen_subi_i32(y
, x
, 0x1000);
345 /* m = (y >> 16) & 4 */
346 tcg_gen_sari_i32(m
, y
, 16);
347 tcg_gen_andi_i32(m
, m
, 4);
349 tcg_gen_add_i32(n
, n
, m
);
351 tcg_gen_shl_i32(x
, x
, m
);
354 tcg_gen_subi_i32(y
, x
, 0x4000);
355 /* m = (y >> 16) & 2 */
356 tcg_gen_sari_i32(m
, y
, 16);
357 tcg_gen_andi_i32(m
, m
, 2);
359 tcg_gen_add_i32(n
, n
, m
);
361 tcg_gen_shl_i32(x
, x
, m
);
364 tcg_gen_shri_i32(y
, x
, 14);
365 /* m = y & ~(y >> 1) */
366 tcg_gen_sari_i32(m
, y
, 1);
367 tcg_gen_not_i32(m
, m
);
368 tcg_gen_and_i32(m
, m
, y
);
371 tcg_gen_addi_i32(d
, n
, 2);
372 tcg_gen_sub_i32(d
, d
, m
);
379 static void t_gen_btst(TCGv d
, TCGv a
, TCGv b
)
387 The N flag is set according to the selected bit in the dest reg.
388 The Z flag is set if the selected bit and all bits to the right are
390 The X flag is cleared.
391 Other flags are left untouched.
392 The destination reg is not affected.
394 unsigned int fz, sbit, bset, mask, masked_t0;
397 bset = !!(T0 & (1 << sbit));
398 mask = sbit == 31 ? -1 : (1 << (sbit + 1)) - 1;
399 masked_t0 = T0 & mask;
400 fz = !(masked_t0 | bset);
402 // Clear the X, N and Z flags.
403 T0 = env->pregs[PR_CCS] & ~(X_FLAG | N_FLAG | Z_FLAG);
404 // Set the N and Z flags accordingly.
405 T0 |= (bset << 3) | (fz << 2);
408 l1
= gen_new_label();
409 sbit
= tcg_temp_new(TCG_TYPE_TL
);
410 bset
= tcg_temp_new(TCG_TYPE_TL
);
411 t0
= tcg_temp_new(TCG_TYPE_TL
);
413 /* Compute bset and sbit. */
414 tcg_gen_andi_tl(sbit
, b
, 31);
415 tcg_gen_shl_tl(t0
, tcg_const_tl(1), sbit
);
416 tcg_gen_and_tl(bset
, a
, t0
);
417 tcg_gen_shr_tl(bset
, bset
, sbit
);
418 /* Displace to N_FLAG. */
419 tcg_gen_shli_tl(bset
, bset
, 3);
421 tcg_gen_shl_tl(sbit
, tcg_const_tl(2), sbit
);
422 tcg_gen_subi_tl(sbit
, sbit
, 1);
423 tcg_gen_and_tl(sbit
, a
, sbit
);
425 tcg_gen_andi_tl(d
, cpu_PR
[PR_CCS
], ~(X_FLAG
| N_FLAG
| Z_FLAG
));
426 /* or in the N_FLAG. */
427 tcg_gen_or_tl(d
, d
, bset
);
428 tcg_gen_brcondi_tl(TCG_COND_NE
, sbit
, 0, l1
);
429 /* or in the Z_FLAG. */
430 tcg_gen_ori_tl(d
, d
, Z_FLAG
);
437 static void t_gen_cris_dstep(TCGv d
, TCGv a
, TCGv b
)
441 l1
= gen_new_label();
448 tcg_gen_shli_tl(d
, a
, 1);
449 tcg_gen_brcond_tl(TCG_COND_LTU
, d
, b
, l1
);
450 tcg_gen_sub_tl(d
, d
, b
);
454 /* Extended arithmetics on CRIS. */
455 static inline void t_gen_add_flag(TCGv d
, int flag
)
459 c
= tcg_temp_new(TCG_TYPE_TL
);
460 t_gen_mov_TN_preg(c
, PR_CCS
);
461 /* Propagate carry into d. */
462 tcg_gen_andi_tl(c
, c
, 1 << flag
);
464 tcg_gen_shri_tl(c
, c
, flag
);
465 tcg_gen_add_tl(d
, d
, c
);
469 static inline void t_gen_addx_carry(DisasContext
*dc
, TCGv d
)
471 if (dc
->flagx_known
) {
475 c
= tcg_temp_new(TCG_TYPE_TL
);
476 t_gen_mov_TN_preg(c
, PR_CCS
);
477 /* C flag is already at bit 0. */
478 tcg_gen_andi_tl(c
, c
, C_FLAG
);
479 tcg_gen_add_tl(d
, d
, c
);
485 x
= tcg_temp_new(TCG_TYPE_TL
);
486 c
= tcg_temp_new(TCG_TYPE_TL
);
487 t_gen_mov_TN_preg(x
, PR_CCS
);
488 tcg_gen_mov_tl(c
, x
);
490 /* Propagate carry into d if X is set. Branch free. */
491 tcg_gen_andi_tl(c
, c
, C_FLAG
);
492 tcg_gen_andi_tl(x
, x
, X_FLAG
);
493 tcg_gen_shri_tl(x
, x
, 4);
495 tcg_gen_and_tl(x
, x
, c
);
496 tcg_gen_add_tl(d
, d
, x
);
502 static inline void t_gen_subx_carry(DisasContext
*dc
, TCGv d
)
504 if (dc
->flagx_known
) {
508 c
= tcg_temp_new(TCG_TYPE_TL
);
509 t_gen_mov_TN_preg(c
, PR_CCS
);
510 /* C flag is already at bit 0. */
511 tcg_gen_andi_tl(c
, c
, C_FLAG
);
512 tcg_gen_sub_tl(d
, d
, c
);
518 x
= tcg_temp_new(TCG_TYPE_TL
);
519 c
= tcg_temp_new(TCG_TYPE_TL
);
520 t_gen_mov_TN_preg(x
, PR_CCS
);
521 tcg_gen_mov_tl(c
, x
);
523 /* Propagate carry into d if X is set. Branch free. */
524 tcg_gen_andi_tl(c
, c
, C_FLAG
);
525 tcg_gen_andi_tl(x
, x
, X_FLAG
);
526 tcg_gen_shri_tl(x
, x
, 4);
528 tcg_gen_and_tl(x
, x
, c
);
529 tcg_gen_sub_tl(d
, d
, x
);
535 /* Swap the two bytes within each half word of the s operand.
536 T0 = ((T0 << 8) & 0xff00ff00) | ((T0 >> 8) & 0x00ff00ff) */
537 static inline void t_gen_swapb(TCGv d
, TCGv s
)
541 t
= tcg_temp_new(TCG_TYPE_TL
);
542 org_s
= tcg_temp_new(TCG_TYPE_TL
);
544 /* d and s may refer to the same object. */
545 tcg_gen_mov_tl(org_s
, s
);
546 tcg_gen_shli_tl(t
, org_s
, 8);
547 tcg_gen_andi_tl(d
, t
, 0xff00ff00);
548 tcg_gen_shri_tl(t
, org_s
, 8);
549 tcg_gen_andi_tl(t
, t
, 0x00ff00ff);
550 tcg_gen_or_tl(d
, d
, t
);
552 tcg_temp_free(org_s
);
555 /* Swap the halfwords of the s operand. */
556 static inline void t_gen_swapw(TCGv d
, TCGv s
)
559 /* d and s refer the same object. */
560 t
= tcg_temp_new(TCG_TYPE_TL
);
561 tcg_gen_mov_tl(t
, s
);
562 tcg_gen_shli_tl(d
, t
, 16);
563 tcg_gen_shri_tl(t
, t
, 16);
564 tcg_gen_or_tl(d
, d
, t
);
568 /* Reverse the within each byte.
569 T0 = (((T0 << 7) & 0x80808080) |
570 ((T0 << 5) & 0x40404040) |
571 ((T0 << 3) & 0x20202020) |
572 ((T0 << 1) & 0x10101010) |
573 ((T0 >> 1) & 0x08080808) |
574 ((T0 >> 3) & 0x04040404) |
575 ((T0 >> 5) & 0x02020202) |
576 ((T0 >> 7) & 0x01010101));
578 static inline void t_gen_swapr(TCGv d
, TCGv s
)
581 int shift
; /* LSL when positive, LSR when negative. */
596 /* d and s refer the same object. */
597 t
= tcg_temp_new(TCG_TYPE_TL
);
598 org_s
= tcg_temp_new(TCG_TYPE_TL
);
599 tcg_gen_mov_tl(org_s
, s
);
601 tcg_gen_shli_tl(t
, org_s
, bitrev
[0].shift
);
602 tcg_gen_andi_tl(d
, t
, bitrev
[0].mask
);
603 for (i
= 1; i
< sizeof bitrev
/ sizeof bitrev
[0]; i
++) {
604 if (bitrev
[i
].shift
>= 0) {
605 tcg_gen_shli_tl(t
, org_s
, bitrev
[i
].shift
);
607 tcg_gen_shri_tl(t
, org_s
, -bitrev
[i
].shift
);
609 tcg_gen_andi_tl(t
, t
, bitrev
[i
].mask
);
610 tcg_gen_or_tl(d
, d
, t
);
613 tcg_temp_free(org_s
);
616 static void t_gen_cc_jmp(TCGv pc_true
, TCGv pc_false
)
621 l1
= gen_new_label();
622 btaken
= tcg_temp_new(TCG_TYPE_TL
);
624 /* Conditional jmp. */
625 tcg_gen_mov_tl(btaken
, env_btaken
);
626 tcg_gen_mov_tl(env_pc
, pc_false
);
627 tcg_gen_brcondi_tl(TCG_COND_EQ
, btaken
, 0, l1
);
628 tcg_gen_mov_tl(env_pc
, pc_true
);
631 tcg_temp_free(btaken
);
634 static void gen_goto_tb(DisasContext
*dc
, int n
, target_ulong dest
)
636 TranslationBlock
*tb
;
638 if ((tb
->pc
& TARGET_PAGE_MASK
) == (dest
& TARGET_PAGE_MASK
)) {
640 tcg_gen_movi_tl(env_pc
, dest
);
641 tcg_gen_exit_tb((long)tb
+ n
);
643 tcg_gen_movi_tl(env_pc
, dest
);
648 /* Sign extend at translation time. */
649 static int sign_extend(unsigned int val
, unsigned int width
)
661 static inline void cris_clear_x_flag(DisasContext
*dc
)
663 if (dc
->flagx_known
&& dc
->flags_x
)
664 dc
->flags_uptodate
= 0;
670 static void cris_flush_cc_state(DisasContext
*dc
)
672 if (dc
->cc_size_uptodate
!= dc
->cc_size
) {
673 tcg_gen_movi_tl(cc_size
, dc
->cc_size
);
674 dc
->cc_size_uptodate
= dc
->cc_size
;
676 tcg_gen_movi_tl(cc_op
, dc
->cc_op
);
677 tcg_gen_movi_tl(cc_mask
, dc
->cc_mask
);
680 static void cris_evaluate_flags(DisasContext
*dc
)
682 if (!dc
->flags_uptodate
) {
683 cris_flush_cc_state(dc
);
688 tcg_gen_helper_0_0(helper_evaluate_flags_mcp
);
691 tcg_gen_helper_0_0(helper_evaluate_flags_muls
);
694 tcg_gen_helper_0_0(helper_evaluate_flags_mulu
);
706 tcg_gen_helper_0_0(helper_evaluate_flags_move_4
);
709 tcg_gen_helper_0_0(helper_evaluate_flags_move_2
);
712 tcg_gen_helper_0_0(helper_evaluate_flags
);
724 tcg_gen_helper_0_0(helper_evaluate_flags_alu_4
);
727 tcg_gen_helper_0_0(helper_evaluate_flags
);
733 if (dc
->flagx_known
) {
735 tcg_gen_ori_tl(cpu_PR
[PR_CCS
],
736 cpu_PR
[PR_CCS
], X_FLAG
);
738 tcg_gen_andi_tl(cpu_PR
[PR_CCS
],
739 cpu_PR
[PR_CCS
], ~X_FLAG
);
742 dc
->flags_uptodate
= 1;
746 static void cris_cc_mask(DisasContext
*dc
, unsigned int mask
)
755 /* Check if we need to evaluate the condition codes due to
757 ovl
= (dc
->cc_mask
^ mask
) & ~mask
;
759 /* TODO: optimize this case. It trigs all the time. */
760 cris_evaluate_flags (dc
);
766 static void cris_update_cc_op(DisasContext
*dc
, int op
, int size
)
770 dc
->flags_uptodate
= 0;
773 static inline void cris_update_cc_x(DisasContext
*dc
)
775 /* Save the x flag state at the time of the cc snapshot. */
776 if (dc
->flagx_known
) {
777 if (dc
->cc_x_uptodate
== (2 | dc
->flags_x
))
779 tcg_gen_movi_tl(cc_x
, dc
->flags_x
);
780 dc
->cc_x_uptodate
= 2 | dc
->flags_x
;
783 tcg_gen_andi_tl(cc_x
, cpu_PR
[PR_CCS
], X_FLAG
);
784 dc
->cc_x_uptodate
= 1;
788 /* Update cc prior to executing ALU op. Needs source operands untouched. */
789 static void cris_pre_alu_update_cc(DisasContext
*dc
, int op
,
790 TCGv dst
, TCGv src
, int size
)
793 cris_update_cc_op(dc
, op
, size
);
794 tcg_gen_mov_tl(cc_src
, src
);
803 tcg_gen_mov_tl(cc_dest
, dst
);
805 cris_update_cc_x(dc
);
809 /* Update cc after executing ALU op. needs the result. */
810 static inline void cris_update_result(DisasContext
*dc
, TCGv res
)
813 if (dc
->cc_size
== 4 &&
814 (dc
->cc_op
== CC_OP_SUB
815 || dc
->cc_op
== CC_OP_ADD
))
817 tcg_gen_mov_tl(cc_result
, res
);
821 /* Returns one if the write back stage should execute. */
822 static void cris_alu_op_exec(DisasContext
*dc
, int op
,
823 TCGv dst
, TCGv a
, TCGv b
, int size
)
825 /* Emit the ALU insns. */
829 tcg_gen_add_tl(dst
, a
, b
);
830 /* Extended arithmetics. */
831 t_gen_addx_carry(dc
, dst
);
834 tcg_gen_add_tl(dst
, a
, b
);
835 t_gen_add_flag(dst
, 0); /* C_FLAG. */
838 tcg_gen_add_tl(dst
, a
, b
);
839 t_gen_add_flag(dst
, 8); /* R_FLAG. */
842 tcg_gen_sub_tl(dst
, a
, b
);
843 /* Extended arithmetics. */
844 t_gen_subx_carry(dc
, dst
);
847 tcg_gen_mov_tl(dst
, b
);
850 tcg_gen_or_tl(dst
, a
, b
);
853 tcg_gen_and_tl(dst
, a
, b
);
856 tcg_gen_xor_tl(dst
, a
, b
);
859 t_gen_lsl(dst
, a
, b
);
862 t_gen_lsr(dst
, a
, b
);
865 t_gen_asr(dst
, a
, b
);
868 tcg_gen_neg_tl(dst
, b
);
869 /* Extended arithmetics. */
870 t_gen_subx_carry(dc
, dst
);
873 t_gen_lz_i32(dst
, b
);
876 t_gen_btst(dst
, a
, b
);
879 t_gen_muls(dst
, cpu_PR
[PR_MOF
], a
, b
);
882 t_gen_mulu(dst
, cpu_PR
[PR_MOF
], a
, b
);
885 t_gen_cris_dstep(dst
, a
, b
);
890 l1
= gen_new_label();
891 tcg_gen_mov_tl(dst
, a
);
892 tcg_gen_brcond_tl(TCG_COND_LEU
, a
, b
, l1
);
893 tcg_gen_mov_tl(dst
, b
);
898 tcg_gen_sub_tl(dst
, a
, b
);
899 /* Extended arithmetics. */
900 t_gen_subx_carry(dc
, dst
);
903 fprintf (logfile
, "illegal ALU op.\n");
909 tcg_gen_andi_tl(dst
, dst
, 0xff);
911 tcg_gen_andi_tl(dst
, dst
, 0xffff);
914 static void cris_alu(DisasContext
*dc
, int op
,
915 TCGv d
, TCGv op_a
, TCGv op_b
, int size
)
924 else if (size
== 4) {
929 cris_pre_alu_update_cc(dc
, op
, op_a
, op_b
, size
);
930 cris_alu_op_exec(dc
, op
, tmp
, op_a
, op_b
, size
);
931 cris_update_result(dc
, tmp
);
936 tcg_gen_andi_tl(d
, d
, ~0xff);
938 tcg_gen_andi_tl(d
, d
, ~0xffff);
939 tcg_gen_or_tl(d
, d
, tmp
);
943 static int arith_cc(DisasContext
*dc
)
947 case CC_OP_ADDC
: return 1;
948 case CC_OP_ADD
: return 1;
949 case CC_OP_SUB
: return 1;
950 case CC_OP_DSTEP
: return 1;
951 case CC_OP_LSL
: return 1;
952 case CC_OP_LSR
: return 1;
953 case CC_OP_ASR
: return 1;
954 case CC_OP_CMP
: return 1;
955 case CC_OP_NEG
: return 1;
956 case CC_OP_OR
: return 1;
957 case CC_OP_XOR
: return 1;
958 case CC_OP_MULU
: return 1;
959 case CC_OP_MULS
: return 1;
967 static void gen_tst_cc (DisasContext
*dc
, int cond
)
969 int arith_opt
, move_opt
;
971 /* TODO: optimize more condition codes. */
974 * If the flags are live, we've gotta look into the bits of CCS.
975 * Otherwise, if we just did an arithmetic operation we try to
976 * evaluate the condition code faster.
978 * When this function is done, T0 should be non-zero if the condition
981 arith_opt
= arith_cc(dc
) && !dc
->flags_uptodate
;
982 move_opt
= (dc
->cc_op
== CC_OP_MOVE
) && dc
->flags_uptodate
;
985 if (arith_opt
|| move_opt
) {
986 /* If cc_result is zero, T0 should be
987 non-zero otherwise T0 should be zero. */
989 l1
= gen_new_label();
990 tcg_gen_movi_tl(cpu_T
[0], 0);
991 tcg_gen_brcondi_tl(TCG_COND_NE
, cc_result
,
993 tcg_gen_movi_tl(cpu_T
[0], 1);
997 cris_evaluate_flags(dc
);
998 tcg_gen_andi_tl(cpu_T
[0],
999 cpu_PR
[PR_CCS
], Z_FLAG
);
1003 if (arith_opt
|| move_opt
)
1004 tcg_gen_mov_tl(cpu_T
[0], cc_result
);
1006 cris_evaluate_flags(dc
);
1007 tcg_gen_xori_tl(cpu_T
[0], cpu_PR
[PR_CCS
],
1009 tcg_gen_andi_tl(cpu_T
[0], cpu_T
[0], Z_FLAG
);
1013 cris_evaluate_flags(dc
);
1014 tcg_gen_andi_tl(cpu_T
[0], cpu_PR
[PR_CCS
], C_FLAG
);
1017 cris_evaluate_flags(dc
);
1018 tcg_gen_xori_tl(cpu_T
[0], cpu_PR
[PR_CCS
], C_FLAG
);
1019 tcg_gen_andi_tl(cpu_T
[0], cpu_T
[0], C_FLAG
);
1022 cris_evaluate_flags(dc
);
1023 tcg_gen_andi_tl(cpu_T
[0], cpu_PR
[PR_CCS
], V_FLAG
);
1026 cris_evaluate_flags(dc
);
1027 tcg_gen_xori_tl(cpu_T
[0], cpu_PR
[PR_CCS
],
1029 tcg_gen_andi_tl(cpu_T
[0], cpu_T
[0], V_FLAG
);
1032 if (arith_opt
|| move_opt
) {
1035 if (dc
->cc_size
== 1)
1037 else if (dc
->cc_size
== 2)
1040 tcg_gen_shri_tl(cpu_T
[0], cc_result
, bits
);
1041 tcg_gen_xori_tl(cpu_T
[0], cpu_T
[0], 1);
1043 cris_evaluate_flags(dc
);
1044 tcg_gen_xori_tl(cpu_T
[0], cpu_PR
[PR_CCS
],
1046 tcg_gen_andi_tl(cpu_T
[0], cpu_T
[0], N_FLAG
);
1050 if (arith_opt
|| move_opt
) {
1053 if (dc
->cc_size
== 1)
1055 else if (dc
->cc_size
== 2)
1058 tcg_gen_shri_tl(cpu_T
[0], cc_result
, 31);
1061 cris_evaluate_flags(dc
);
1062 tcg_gen_andi_tl(cpu_T
[0], cpu_PR
[PR_CCS
],
1067 cris_evaluate_flags(dc
);
1068 tcg_gen_andi_tl(cpu_T
[0], cpu_PR
[PR_CCS
],
1072 cris_evaluate_flags(dc
);
1076 tmp
= tcg_temp_new(TCG_TYPE_TL
);
1077 tcg_gen_xori_tl(tmp
, cpu_PR
[PR_CCS
],
1079 /* Overlay the C flag on top of the Z. */
1080 tcg_gen_shli_tl(cpu_T
[0], tmp
, 2);
1081 tcg_gen_and_tl(cpu_T
[0], tmp
, cpu_T
[0]);
1082 tcg_gen_andi_tl(cpu_T
[0], cpu_T
[0], Z_FLAG
);
1088 cris_evaluate_flags(dc
);
1089 /* Overlay the V flag on top of the N. */
1090 tcg_gen_shli_tl(cpu_T
[0], cpu_PR
[PR_CCS
], 2);
1091 tcg_gen_xor_tl(cpu_T
[0],
1092 cpu_PR
[PR_CCS
], cpu_T
[0]);
1093 tcg_gen_andi_tl(cpu_T
[0], cpu_T
[0], N_FLAG
);
1094 tcg_gen_xori_tl(cpu_T
[0], cpu_T
[0], N_FLAG
);
1097 cris_evaluate_flags(dc
);
1098 /* Overlay the V flag on top of the N. */
1099 tcg_gen_shli_tl(cpu_T
[0], cpu_PR
[PR_CCS
], 2);
1100 tcg_gen_xor_tl(cpu_T
[0],
1101 cpu_PR
[PR_CCS
], cpu_T
[0]);
1102 tcg_gen_andi_tl(cpu_T
[0], cpu_T
[0], N_FLAG
);
1105 cris_evaluate_flags(dc
);
1109 n
= tcg_temp_new(TCG_TYPE_TL
);
1110 z
= tcg_temp_new(TCG_TYPE_TL
);
1112 /* To avoid a shift we overlay everything on
1114 tcg_gen_shri_tl(n
, cpu_PR
[PR_CCS
], 2);
1115 tcg_gen_shri_tl(z
, cpu_PR
[PR_CCS
], 1);
1117 tcg_gen_xori_tl(z
, z
, 2);
1119 tcg_gen_xor_tl(n
, n
, cpu_PR
[PR_CCS
]);
1120 tcg_gen_xori_tl(n
, n
, 2);
1121 tcg_gen_and_tl(cpu_T
[0], z
, n
);
1122 tcg_gen_andi_tl(cpu_T
[0], cpu_T
[0], 2);
1129 cris_evaluate_flags(dc
);
1133 n
= tcg_temp_new(TCG_TYPE_TL
);
1134 z
= tcg_temp_new(TCG_TYPE_TL
);
1136 /* To avoid a shift we overlay everything on
1138 tcg_gen_shri_tl(n
, cpu_PR
[PR_CCS
], 2);
1139 tcg_gen_shri_tl(z
, cpu_PR
[PR_CCS
], 1);
1141 tcg_gen_xor_tl(n
, n
, cpu_PR
[PR_CCS
]);
1142 tcg_gen_or_tl(cpu_T
[0], z
, n
);
1143 tcg_gen_andi_tl(cpu_T
[0], cpu_T
[0], 2);
1150 cris_evaluate_flags(dc
);
1151 tcg_gen_andi_tl(cpu_T
[0], cpu_PR
[PR_CCS
], P_FLAG
);
1154 tcg_gen_movi_tl(cpu_T
[0], 1);
1162 static void cris_store_direct_jmp(DisasContext
*dc
)
1164 /* Store the direct jmp state into the cpu-state. */
1165 if (dc
->jmp
== JMP_DIRECT
) {
1166 tcg_gen_movi_tl(env_btarget
, dc
->jmp_pc
);
1167 tcg_gen_movi_tl(env_btaken
, 1);
1171 static void cris_prepare_cc_branch (DisasContext
*dc
,
1172 int offset
, int cond
)
1174 /* This helps us re-schedule the micro-code to insns in delay-slots
1175 before the actual jump. */
1176 dc
->delayed_branch
= 2;
1177 dc
->jmp_pc
= dc
->pc
+ offset
;
1181 dc
->jmp
= JMP_INDIRECT
;
1182 gen_tst_cc (dc
, cond
);
1183 tcg_gen_mov_tl(env_btaken
, cpu_T
[0]);
1184 tcg_gen_movi_tl(env_btarget
, dc
->jmp_pc
);
1186 /* Allow chaining. */
1187 dc
->jmp
= JMP_DIRECT
;
1192 /* jumps, when the dest is in a live reg for example. Direct should be set
1193 when the dest addr is constant to allow tb chaining. */
1194 static inline void cris_prepare_jmp (DisasContext
*dc
, unsigned int type
)
1196 /* This helps us re-schedule the micro-code to insns in delay-slots
1197 before the actual jump. */
1198 dc
->delayed_branch
= 2;
1200 if (type
== JMP_INDIRECT
)
1201 tcg_gen_movi_tl(env_btaken
, 1);
1204 void gen_load(DisasContext
*dc
, TCGv dst
, TCGv addr
,
1205 unsigned int size
, int sign
)
1207 int mem_index
= cpu_mmu_index(dc
->env
);
1209 /* If we get a fault on a delayslot we must keep the jmp state in
1210 the cpu-state to be able to re-execute the jmp. */
1211 if (dc
->delayed_branch
== 1)
1212 cris_store_direct_jmp(dc
);
1216 tcg_gen_qemu_ld8s(dst
, addr
, mem_index
);
1218 tcg_gen_qemu_ld8u(dst
, addr
, mem_index
);
1220 else if (size
== 2) {
1222 tcg_gen_qemu_ld16s(dst
, addr
, mem_index
);
1224 tcg_gen_qemu_ld16u(dst
, addr
, mem_index
);
1226 else if (size
== 4) {
1227 tcg_gen_qemu_ld32u(dst
, addr
, mem_index
);
1229 else if (size
== 8) {
1230 tcg_gen_qemu_ld64(dst
, addr
, mem_index
);
1234 void gen_store (DisasContext
*dc
, TCGv addr
, TCGv val
,
1237 int mem_index
= cpu_mmu_index(dc
->env
);
1239 /* If we get a fault on a delayslot we must keep the jmp state in
1240 the cpu-state to be able to re-execute the jmp. */
1241 if (dc
->delayed_branch
== 1)
1242 cris_store_direct_jmp(dc
);
1245 /* Conditional writes. We only support the kind were X and P are known
1246 at translation time. */
1247 if (dc
->flagx_known
&& dc
->flags_x
&& (dc
->tb_flags
& P_FLAG
)) {
1249 cris_evaluate_flags(dc
);
1250 tcg_gen_ori_tl(cpu_PR
[PR_CCS
], cpu_PR
[PR_CCS
], C_FLAG
);
1255 tcg_gen_qemu_st8(val
, addr
, mem_index
);
1257 tcg_gen_qemu_st16(val
, addr
, mem_index
);
1259 tcg_gen_qemu_st32(val
, addr
, mem_index
);
1261 if (dc
->flagx_known
&& dc
->flags_x
) {
1262 cris_evaluate_flags(dc
);
1263 tcg_gen_andi_tl(cpu_PR
[PR_CCS
], cpu_PR
[PR_CCS
], ~C_FLAG
);
1267 static inline void t_gen_sext(TCGv d
, TCGv s
, int size
)
1270 tcg_gen_ext8s_i32(d
, s
);
1272 tcg_gen_ext16s_i32(d
, s
);
1273 else if(GET_TCGV(d
) != GET_TCGV(s
))
1274 tcg_gen_mov_tl(d
, s
);
1277 static inline void t_gen_zext(TCGv d
, TCGv s
, int size
)
1280 tcg_gen_ext8u_i32(d
, s
);
1282 tcg_gen_ext16u_i32(d
, s
);
1283 else if (GET_TCGV(d
) != GET_TCGV(s
))
1284 tcg_gen_mov_tl(d
, s
);
1288 static char memsize_char(int size
)
1292 case 1: return 'b'; break;
1293 case 2: return 'w'; break;
1294 case 4: return 'd'; break;
1302 static inline unsigned int memsize_z(DisasContext
*dc
)
1304 return dc
->zsize
+ 1;
1307 static inline unsigned int memsize_zz(DisasContext
*dc
)
1318 static inline void do_postinc (DisasContext
*dc
, int size
)
1321 tcg_gen_addi_tl(cpu_R
[dc
->op1
], cpu_R
[dc
->op1
], size
);
1324 static inline void dec_prep_move_r(DisasContext
*dc
, int rs
, int rd
,
1325 int size
, int s_ext
, TCGv dst
)
1328 t_gen_sext(dst
, cpu_R
[rs
], size
);
1330 t_gen_zext(dst
, cpu_R
[rs
], size
);
1333 /* Prepare T0 and T1 for a register alu operation.
1334 s_ext decides if the operand1 should be sign-extended or zero-extended when
1336 static void dec_prep_alu_r(DisasContext
*dc
, int rs
, int rd
,
1337 int size
, int s_ext
)
1339 dec_prep_move_r(dc
, rs
, rd
, size
, s_ext
, cpu_T
[1]);
1342 t_gen_sext(cpu_T
[0], cpu_R
[rd
], size
);
1344 t_gen_zext(cpu_T
[0], cpu_R
[rd
], size
);
1347 static int dec_prep_move_m(DisasContext
*dc
, int s_ext
, int memsize
,
1350 unsigned int rs
, rd
;
1357 is_imm
= rs
== 15 && dc
->postinc
;
1359 /* Load [$rs] onto T1. */
1361 insn_len
= 2 + memsize
;
1368 imm
= ldsb_code(dc
->pc
+ 2);
1370 imm
= ldsw_code(dc
->pc
+ 2);
1373 imm
= ldub_code(dc
->pc
+ 2);
1375 imm
= lduw_code(dc
->pc
+ 2);
1378 imm
= ldl_code(dc
->pc
+ 2);
1380 DIS(fprintf (logfile
, "imm=%x rd=%d sext=%d ms=%d\n",
1381 imm
, rd
, s_ext
, memsize
));
1382 tcg_gen_movi_tl(dst
, imm
);
1385 cris_flush_cc_state(dc
);
1386 gen_load(dc
, dst
, cpu_R
[rs
], memsize
, 0);
1388 t_gen_sext(dst
, dst
, memsize
);
1390 t_gen_zext(dst
, dst
, memsize
);
1395 /* Prepare T0 and T1 for a memory + alu operation.
1396 s_ext decides if the operand1 should be sign-extended or zero-extended when
1398 static int dec_prep_alu_m(DisasContext
*dc
, int s_ext
, int memsize
)
1402 insn_len
= dec_prep_move_m(dc
, s_ext
, memsize
, cpu_T
[1]);
1404 /* put dest in T0. */
1405 tcg_gen_mov_tl(cpu_T
[0], cpu_R
[dc
->op2
]);
1410 static const char *cc_name(int cc
)
1412 static char *cc_names
[16] = {
1413 "cc", "cs", "ne", "eq", "vc", "vs", "pl", "mi",
1414 "ls", "hi", "ge", "lt", "gt", "le", "a", "p"
1417 return cc_names
[cc
];
1421 /* Start of insn decoders. */
1423 static unsigned int dec_bccq(DisasContext
*dc
)
1427 uint32_t cond
= dc
->op2
;
1430 offset
= EXTRACT_FIELD (dc
->ir
, 1, 7);
1431 sign
= EXTRACT_FIELD(dc
->ir
, 0, 0);
1434 offset
|= sign
<< 8;
1436 offset
= sign_extend(offset
, 8);
1438 DIS(fprintf (logfile
, "b%s %x\n", cc_name(cond
), dc
->pc
+ offset
));
1440 /* op2 holds the condition-code. */
1441 cris_cc_mask(dc
, 0);
1442 cris_prepare_cc_branch (dc
, offset
, cond
);
1445 static unsigned int dec_addoq(DisasContext
*dc
)
1449 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 7);
1450 imm
= sign_extend(dc
->op1
, 7);
1452 DIS(fprintf (logfile
, "addoq %d, $r%u\n", imm
, dc
->op2
));
1453 cris_cc_mask(dc
, 0);
1454 /* Fetch register operand, */
1455 tcg_gen_addi_tl(cpu_R
[R_ACR
], cpu_R
[dc
->op2
], imm
);
1458 static unsigned int dec_addq(DisasContext
*dc
)
1460 DIS(fprintf (logfile
, "addq %u, $r%u\n", dc
->op1
, dc
->op2
));
1462 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 5);
1464 cris_cc_mask(dc
, CC_MASK_NZVC
);
1466 cris_alu(dc
, CC_OP_ADD
,
1467 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], tcg_const_tl(dc
->op1
), 4);
1470 static unsigned int dec_moveq(DisasContext
*dc
)
1474 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 5);
1475 imm
= sign_extend(dc
->op1
, 5);
1476 DIS(fprintf (logfile
, "moveq %d, $r%u\n", imm
, dc
->op2
));
1478 tcg_gen_mov_tl(cpu_R
[dc
->op2
], tcg_const_tl(imm
));
1481 static unsigned int dec_subq(DisasContext
*dc
)
1483 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 5);
1485 DIS(fprintf (logfile
, "subq %u, $r%u\n", dc
->op1
, dc
->op2
));
1487 cris_cc_mask(dc
, CC_MASK_NZVC
);
1488 cris_alu(dc
, CC_OP_SUB
,
1489 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], tcg_const_tl(dc
->op1
), 4);
1492 static unsigned int dec_cmpq(DisasContext
*dc
)
1495 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 5);
1496 imm
= sign_extend(dc
->op1
, 5);
1498 DIS(fprintf (logfile
, "cmpq %d, $r%d\n", imm
, dc
->op2
));
1499 cris_cc_mask(dc
, CC_MASK_NZVC
);
1501 cris_alu(dc
, CC_OP_CMP
,
1502 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], tcg_const_tl(imm
), 4);
1505 static unsigned int dec_andq(DisasContext
*dc
)
1508 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 5);
1509 imm
= sign_extend(dc
->op1
, 5);
1511 DIS(fprintf (logfile
, "andq %d, $r%d\n", imm
, dc
->op2
));
1512 cris_cc_mask(dc
, CC_MASK_NZ
);
1514 cris_alu(dc
, CC_OP_AND
,
1515 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], tcg_const_tl(imm
), 4);
1518 static unsigned int dec_orq(DisasContext
*dc
)
1521 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 5);
1522 imm
= sign_extend(dc
->op1
, 5);
1523 DIS(fprintf (logfile
, "orq %d, $r%d\n", imm
, dc
->op2
));
1524 cris_cc_mask(dc
, CC_MASK_NZ
);
1526 cris_alu(dc
, CC_OP_OR
,
1527 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], tcg_const_tl(imm
), 4);
1530 static unsigned int dec_btstq(DisasContext
*dc
)
1532 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 4);
1533 DIS(fprintf (logfile
, "btstq %u, $r%d\n", dc
->op1
, dc
->op2
));
1535 cris_cc_mask(dc
, CC_MASK_NZ
);
1537 cris_alu(dc
, CC_OP_BTST
,
1538 cpu_T
[0], cpu_R
[dc
->op2
], tcg_const_tl(dc
->op1
), 4);
1539 cris_update_cc_op(dc
, CC_OP_FLAGS
, 4);
1540 t_gen_mov_preg_TN(dc
, PR_CCS
, cpu_T
[0]);
1541 dc
->flags_uptodate
= 1;
1544 static unsigned int dec_asrq(DisasContext
*dc
)
1546 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 4);
1547 DIS(fprintf (logfile
, "asrq %u, $r%d\n", dc
->op1
, dc
->op2
));
1548 cris_cc_mask(dc
, CC_MASK_NZ
);
1550 tcg_gen_sari_tl(cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], dc
->op1
);
1551 cris_alu(dc
, CC_OP_MOVE
,
1553 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], 4);
1556 static unsigned int dec_lslq(DisasContext
*dc
)
1558 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 4);
1559 DIS(fprintf (logfile
, "lslq %u, $r%d\n", dc
->op1
, dc
->op2
));
1561 cris_cc_mask(dc
, CC_MASK_NZ
);
1563 tcg_gen_shli_tl(cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], dc
->op1
);
1565 cris_alu(dc
, CC_OP_MOVE
,
1567 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], 4);
1570 static unsigned int dec_lsrq(DisasContext
*dc
)
1572 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 4);
1573 DIS(fprintf (logfile
, "lsrq %u, $r%d\n", dc
->op1
, dc
->op2
));
1575 cris_cc_mask(dc
, CC_MASK_NZ
);
1577 tcg_gen_shri_tl(cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], dc
->op1
);
1578 cris_alu(dc
, CC_OP_MOVE
,
1580 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], 4);
1584 static unsigned int dec_move_r(DisasContext
*dc
)
1586 int size
= memsize_zz(dc
);
1588 DIS(fprintf (logfile
, "move.%c $r%u, $r%u\n",
1589 memsize_char(size
), dc
->op1
, dc
->op2
));
1591 cris_cc_mask(dc
, CC_MASK_NZ
);
1593 dec_prep_move_r(dc
, dc
->op1
, dc
->op2
, size
, 0, cpu_R
[dc
->op2
]);
1594 cris_cc_mask(dc
, CC_MASK_NZ
);
1595 cris_update_cc_op(dc
, CC_OP_MOVE
, 4);
1596 cris_update_cc_x(dc
);
1597 cris_update_result(dc
, cpu_R
[dc
->op2
]);
1600 dec_prep_move_r(dc
, dc
->op1
, dc
->op2
, size
, 0, cpu_T
[1]);
1601 cris_alu(dc
, CC_OP_MOVE
,
1603 cpu_R
[dc
->op2
], cpu_T
[1], size
);
1608 static unsigned int dec_scc_r(DisasContext
*dc
)
1612 DIS(fprintf (logfile
, "s%s $r%u\n",
1613 cc_name(cond
), dc
->op1
));
1619 gen_tst_cc (dc
, cond
);
1621 l1
= gen_new_label();
1622 tcg_gen_movi_tl(cpu_R
[dc
->op1
], 0);
1623 tcg_gen_brcondi_tl(TCG_COND_EQ
, cpu_T
[0], 0, l1
);
1624 tcg_gen_movi_tl(cpu_R
[dc
->op1
], 1);
1628 tcg_gen_movi_tl(cpu_R
[dc
->op1
], 1);
1630 cris_cc_mask(dc
, 0);
1634 static unsigned int dec_and_r(DisasContext
*dc
)
1636 int size
= memsize_zz(dc
);
1638 DIS(fprintf (logfile
, "and.%c $r%u, $r%u\n",
1639 memsize_char(size
), dc
->op1
, dc
->op2
));
1640 cris_cc_mask(dc
, CC_MASK_NZ
);
1641 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 0);
1643 cris_alu(dc
, CC_OP_AND
,
1645 cpu_R
[dc
->op2
], cpu_T
[1], size
);
1649 static unsigned int dec_lz_r(DisasContext
*dc
)
1651 DIS(fprintf (logfile
, "lz $r%u, $r%u\n",
1653 cris_cc_mask(dc
, CC_MASK_NZ
);
1654 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, 4, 0);
1655 cris_alu(dc
, CC_OP_LZ
,
1656 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], cpu_T
[1], 4);
1660 static unsigned int dec_lsl_r(DisasContext
*dc
)
1662 int size
= memsize_zz(dc
);
1664 DIS(fprintf (logfile
, "lsl.%c $r%u, $r%u\n",
1665 memsize_char(size
), dc
->op1
, dc
->op2
));
1666 cris_cc_mask(dc
, CC_MASK_NZ
);
1667 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 0);
1668 tcg_gen_andi_tl(cpu_T
[1], cpu_T
[1], 63);
1670 cris_alu(dc
, CC_OP_LSL
,
1671 cpu_R
[dc
->op2
], cpu_T
[0], cpu_T
[1], size
);
1675 static unsigned int dec_lsr_r(DisasContext
*dc
)
1677 int size
= memsize_zz(dc
);
1679 DIS(fprintf (logfile
, "lsr.%c $r%u, $r%u\n",
1680 memsize_char(size
), dc
->op1
, dc
->op2
));
1681 cris_cc_mask(dc
, CC_MASK_NZ
);
1682 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 0);
1683 tcg_gen_andi_tl(cpu_T
[1], cpu_T
[1], 63);
1685 cris_alu(dc
, CC_OP_LSR
,
1686 cpu_R
[dc
->op2
], cpu_T
[0], cpu_T
[1], size
);
1690 static unsigned int dec_asr_r(DisasContext
*dc
)
1692 int size
= memsize_zz(dc
);
1694 DIS(fprintf (logfile
, "asr.%c $r%u, $r%u\n",
1695 memsize_char(size
), dc
->op1
, dc
->op2
));
1696 cris_cc_mask(dc
, CC_MASK_NZ
);
1697 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 1);
1698 tcg_gen_andi_tl(cpu_T
[1], cpu_T
[1], 63);
1700 cris_alu(dc
, CC_OP_ASR
,
1701 cpu_R
[dc
->op2
], cpu_T
[0], cpu_T
[1], size
);
1705 static unsigned int dec_muls_r(DisasContext
*dc
)
1707 int size
= memsize_zz(dc
);
1709 DIS(fprintf (logfile
, "muls.%c $r%u, $r%u\n",
1710 memsize_char(size
), dc
->op1
, dc
->op2
));
1711 cris_cc_mask(dc
, CC_MASK_NZV
);
1712 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 1);
1714 cris_alu(dc
, CC_OP_MULS
,
1715 cpu_R
[dc
->op2
], cpu_T
[0], cpu_T
[1], 4);
1719 static unsigned int dec_mulu_r(DisasContext
*dc
)
1721 int size
= memsize_zz(dc
);
1723 DIS(fprintf (logfile
, "mulu.%c $r%u, $r%u\n",
1724 memsize_char(size
), dc
->op1
, dc
->op2
));
1725 cris_cc_mask(dc
, CC_MASK_NZV
);
1726 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 0);
1728 cris_alu(dc
, CC_OP_MULU
,
1729 cpu_R
[dc
->op2
], cpu_T
[0], cpu_T
[1], 4);
1734 static unsigned int dec_dstep_r(DisasContext
*dc
)
1736 DIS(fprintf (logfile
, "dstep $r%u, $r%u\n", dc
->op1
, dc
->op2
));
1737 cris_cc_mask(dc
, CC_MASK_NZ
);
1738 cris_alu(dc
, CC_OP_DSTEP
,
1739 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], cpu_R
[dc
->op1
], 4);
1743 static unsigned int dec_xor_r(DisasContext
*dc
)
1745 int size
= memsize_zz(dc
);
1746 DIS(fprintf (logfile
, "xor.%c $r%u, $r%u\n",
1747 memsize_char(size
), dc
->op1
, dc
->op2
));
1748 BUG_ON(size
!= 4); /* xor is dword. */
1749 cris_cc_mask(dc
, CC_MASK_NZ
);
1750 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 0);
1752 cris_alu(dc
, CC_OP_XOR
,
1753 cpu_R
[dc
->op2
], cpu_T
[0], cpu_T
[1], 4);
1757 static unsigned int dec_bound_r(DisasContext
*dc
)
1759 int size
= memsize_zz(dc
);
1760 DIS(fprintf (logfile
, "bound.%c $r%u, $r%u\n",
1761 memsize_char(size
), dc
->op1
, dc
->op2
));
1762 cris_cc_mask(dc
, CC_MASK_NZ
);
1763 dec_prep_move_r(dc
, dc
->op1
, dc
->op2
, size
, 0, cpu_T
[1]);
1764 cris_alu(dc
, CC_OP_BOUND
,
1765 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], cpu_T
[1], 4);
1769 static unsigned int dec_cmp_r(DisasContext
*dc
)
1771 int size
= memsize_zz(dc
);
1772 DIS(fprintf (logfile
, "cmp.%c $r%u, $r%u\n",
1773 memsize_char(size
), dc
->op1
, dc
->op2
));
1774 cris_cc_mask(dc
, CC_MASK_NZVC
);
1775 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 0);
1777 cris_alu(dc
, CC_OP_CMP
,
1778 cpu_R
[dc
->op2
], cpu_T
[0], cpu_T
[1], size
);
1782 static unsigned int dec_abs_r(DisasContext
*dc
)
1786 DIS(fprintf (logfile
, "abs $r%u, $r%u\n",
1788 cris_cc_mask(dc
, CC_MASK_NZ
);
1789 dec_prep_move_r(dc
, dc
->op1
, dc
->op2
, 4, 0, cpu_T
[1]);
1791 /* TODO: consider a branch free approach. */
1792 l1
= gen_new_label();
1793 tcg_gen_brcondi_tl(TCG_COND_GE
, cpu_T
[1], 0, l1
);
1794 tcg_gen_neg_tl(cpu_T
[1], cpu_T
[1]);
1796 cris_alu(dc
, CC_OP_MOVE
,
1797 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], cpu_T
[1], 4);
1801 static unsigned int dec_add_r(DisasContext
*dc
)
1803 int size
= memsize_zz(dc
);
1804 DIS(fprintf (logfile
, "add.%c $r%u, $r%u\n",
1805 memsize_char(size
), dc
->op1
, dc
->op2
));
1806 cris_cc_mask(dc
, CC_MASK_NZVC
);
1807 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 0);
1809 cris_alu(dc
, CC_OP_ADD
,
1810 cpu_R
[dc
->op2
], cpu_T
[0], cpu_T
[1], size
);
1814 static unsigned int dec_addc_r(DisasContext
*dc
)
1816 DIS(fprintf (logfile
, "addc $r%u, $r%u\n",
1818 cris_evaluate_flags(dc
);
1819 cris_cc_mask(dc
, CC_MASK_NZVC
);
1820 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, 4, 0);
1821 cris_alu(dc
, CC_OP_ADDC
,
1822 cpu_R
[dc
->op2
], cpu_T
[0], cpu_T
[1], 4);
1826 static unsigned int dec_mcp_r(DisasContext
*dc
)
1828 DIS(fprintf (logfile
, "mcp $p%u, $r%u\n",
1830 cris_evaluate_flags(dc
);
1831 cris_cc_mask(dc
, CC_MASK_RNZV
);
1832 cris_alu(dc
, CC_OP_MCP
,
1833 cpu_R
[dc
->op1
], cpu_R
[dc
->op1
], cpu_PR
[dc
->op2
], 4);
1838 static char * swapmode_name(int mode
, char *modename
) {
1841 modename
[i
++] = 'n';
1843 modename
[i
++] = 'w';
1845 modename
[i
++] = 'b';
1847 modename
[i
++] = 'r';
1853 static unsigned int dec_swap_r(DisasContext
*dc
)
1858 DIS(fprintf (logfile
, "swap%s $r%u\n",
1859 swapmode_name(dc
->op2
, modename
), dc
->op1
));
1861 cris_cc_mask(dc
, CC_MASK_NZ
);
1862 t_gen_mov_TN_reg(cpu_T
[0], dc
->op1
);
1864 tcg_gen_not_tl(cpu_T
[0], cpu_T
[0]);
1866 t_gen_swapw(cpu_T
[0], cpu_T
[0]);
1868 t_gen_swapb(cpu_T
[0], cpu_T
[0]);
1870 t_gen_swapr(cpu_T
[0], cpu_T
[0]);
1871 cris_alu(dc
, CC_OP_MOVE
,
1872 cpu_R
[dc
->op1
], cpu_R
[dc
->op1
], cpu_T
[0], 4);
1877 static unsigned int dec_or_r(DisasContext
*dc
)
1879 int size
= memsize_zz(dc
);
1880 DIS(fprintf (logfile
, "or.%c $r%u, $r%u\n",
1881 memsize_char(size
), dc
->op1
, dc
->op2
));
1882 cris_cc_mask(dc
, CC_MASK_NZ
);
1883 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 0);
1885 cris_alu(dc
, CC_OP_OR
,
1886 cpu_R
[dc
->op2
], cpu_T
[0], cpu_T
[1], size
);
1890 static unsigned int dec_addi_r(DisasContext
*dc
)
1892 DIS(fprintf (logfile
, "addi.%c $r%u, $r%u\n",
1893 memsize_char(memsize_zz(dc
)), dc
->op2
, dc
->op1
));
1894 cris_cc_mask(dc
, 0);
1895 tcg_gen_shl_tl(cpu_T
[0], cpu_R
[dc
->op2
], tcg_const_tl(dc
->zzsize
));
1896 tcg_gen_add_tl(cpu_R
[dc
->op1
], cpu_R
[dc
->op1
], cpu_T
[0]);
1900 static unsigned int dec_addi_acr(DisasContext
*dc
)
1902 DIS(fprintf (logfile
, "addi.%c $r%u, $r%u, $acr\n",
1903 memsize_char(memsize_zz(dc
)), dc
->op2
, dc
->op1
));
1904 cris_cc_mask(dc
, 0);
1905 tcg_gen_shl_tl(cpu_T
[0], cpu_R
[dc
->op2
], tcg_const_tl(dc
->zzsize
));
1906 tcg_gen_add_tl(cpu_R
[R_ACR
], cpu_R
[dc
->op1
], cpu_T
[0]);
1910 static unsigned int dec_neg_r(DisasContext
*dc
)
1912 int size
= memsize_zz(dc
);
1913 DIS(fprintf (logfile
, "neg.%c $r%u, $r%u\n",
1914 memsize_char(size
), dc
->op1
, dc
->op2
));
1915 cris_cc_mask(dc
, CC_MASK_NZVC
);
1916 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 0);
1918 cris_alu(dc
, CC_OP_NEG
,
1919 cpu_R
[dc
->op2
], cpu_T
[0], cpu_T
[1], size
);
1923 static unsigned int dec_btst_r(DisasContext
*dc
)
1925 DIS(fprintf (logfile
, "btst $r%u, $r%u\n",
1927 cris_cc_mask(dc
, CC_MASK_NZ
);
1928 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, 4, 0);
1930 cris_alu(dc
, CC_OP_BTST
,
1931 cpu_T
[0], cpu_T
[0], cpu_T
[1], 4);
1932 cris_update_cc_op(dc
, CC_OP_FLAGS
, 4);
1933 t_gen_mov_preg_TN(dc
, PR_CCS
, cpu_T
[0]);
1934 dc
->flags_uptodate
= 1;
1938 static unsigned int dec_sub_r(DisasContext
*dc
)
1940 int size
= memsize_zz(dc
);
1941 DIS(fprintf (logfile
, "sub.%c $r%u, $r%u\n",
1942 memsize_char(size
), dc
->op1
, dc
->op2
));
1943 cris_cc_mask(dc
, CC_MASK_NZVC
);
1944 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 0);
1945 cris_alu(dc
, CC_OP_SUB
,
1946 cpu_R
[dc
->op2
], cpu_T
[0], cpu_T
[1], size
);
1950 /* Zero extension. From size to dword. */
1951 static unsigned int dec_movu_r(DisasContext
*dc
)
1953 int size
= memsize_z(dc
);
1954 DIS(fprintf (logfile
, "movu.%c $r%u, $r%u\n",
1958 cris_cc_mask(dc
, CC_MASK_NZ
);
1959 dec_prep_move_r(dc
, dc
->op1
, dc
->op2
, size
, 0, cpu_T
[1]);
1960 cris_alu(dc
, CC_OP_MOVE
,
1961 cpu_R
[dc
->op2
], cpu_T
[0], cpu_T
[1], 4);
1965 /* Sign extension. From size to dword. */
1966 static unsigned int dec_movs_r(DisasContext
*dc
)
1968 int size
= memsize_z(dc
);
1969 DIS(fprintf (logfile
, "movs.%c $r%u, $r%u\n",
1973 cris_cc_mask(dc
, CC_MASK_NZ
);
1974 /* Size can only be qi or hi. */
1975 t_gen_sext(cpu_T
[1], cpu_R
[dc
->op1
], size
);
1976 cris_alu(dc
, CC_OP_MOVE
,
1977 cpu_R
[dc
->op2
], cpu_R
[dc
->op1
], cpu_T
[1], 4);
1981 /* zero extension. From size to dword. */
1982 static unsigned int dec_addu_r(DisasContext
*dc
)
1984 int size
= memsize_z(dc
);
1985 DIS(fprintf (logfile
, "addu.%c $r%u, $r%u\n",
1989 cris_cc_mask(dc
, CC_MASK_NZVC
);
1990 /* Size can only be qi or hi. */
1991 t_gen_zext(cpu_T
[1], cpu_R
[dc
->op1
], size
);
1992 cris_alu(dc
, CC_OP_ADD
,
1993 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], cpu_T
[1], 4);
1997 /* Sign extension. From size to dword. */
1998 static unsigned int dec_adds_r(DisasContext
*dc
)
2000 int size
= memsize_z(dc
);
2001 DIS(fprintf (logfile
, "adds.%c $r%u, $r%u\n",
2005 cris_cc_mask(dc
, CC_MASK_NZVC
);
2006 /* Size can only be qi or hi. */
2007 t_gen_sext(cpu_T
[1], cpu_R
[dc
->op1
], size
);
2008 cris_alu(dc
, CC_OP_ADD
,
2009 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], cpu_T
[1], 4);
2013 /* Zero extension. From size to dword. */
2014 static unsigned int dec_subu_r(DisasContext
*dc
)
2016 int size
= memsize_z(dc
);
2017 DIS(fprintf (logfile
, "subu.%c $r%u, $r%u\n",
2021 cris_cc_mask(dc
, CC_MASK_NZVC
);
2022 /* Size can only be qi or hi. */
2023 t_gen_zext(cpu_T
[1], cpu_R
[dc
->op1
], size
);
2024 cris_alu(dc
, CC_OP_SUB
,
2025 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], cpu_T
[1], 4);
2029 /* Sign extension. From size to dword. */
2030 static unsigned int dec_subs_r(DisasContext
*dc
)
2032 int size
= memsize_z(dc
);
2033 DIS(fprintf (logfile
, "subs.%c $r%u, $r%u\n",
2037 cris_cc_mask(dc
, CC_MASK_NZVC
);
2038 /* Size can only be qi or hi. */
2039 t_gen_sext(cpu_T
[1], cpu_R
[dc
->op1
], size
);
2040 cris_alu(dc
, CC_OP_SUB
,
2041 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], cpu_T
[1], 4);
2045 static unsigned int dec_setclrf(DisasContext
*dc
)
2048 int set
= (~dc
->opcode
>> 2) & 1;
2050 flags
= (EXTRACT_FIELD(dc
->ir
, 12, 15) << 4)
2051 | EXTRACT_FIELD(dc
->ir
, 0, 3);
2052 if (set
&& flags
== 0) {
2053 DIS(fprintf (logfile
, "nop\n"));
2055 } else if (!set
&& (flags
& 0x20)) {
2056 DIS(fprintf (logfile
, "di\n"));
2059 DIS(fprintf (logfile
, "%sf %x\n",
2060 set
? "set" : "clr",
2064 /* User space is not allowed to touch these. Silently ignore. */
2065 if (dc
->tb_flags
& U_FLAG
) {
2066 flags
&= ~(I_FLAG
| U_FLAG
);
2069 if (flags
& X_FLAG
) {
2070 dc
->flagx_known
= 1;
2072 dc
->flags_x
= X_FLAG
;
2077 /* Break the TB if the P flag changes. */
2078 if (flags
& P_FLAG
) {
2079 if ((set
&& !(dc
->tb_flags
& P_FLAG
))
2080 || (!set
&& (dc
->tb_flags
& P_FLAG
))) {
2081 tcg_gen_movi_tl(env_pc
, dc
->pc
+ 2);
2082 dc
->is_jmp
= DISAS_UPDATE
;
2083 dc
->cpustate_changed
= 1;
2088 /* Simply decode the flags. */
2089 cris_evaluate_flags (dc
);
2090 cris_update_cc_op(dc
, CC_OP_FLAGS
, 4);
2091 cris_update_cc_x(dc
);
2092 tcg_gen_movi_tl(cc_op
, dc
->cc_op
);
2095 if (!(dc
->tb_flags
& U_FLAG
) && (flags
& U_FLAG
)) {
2096 /* Enter user mode. */
2097 t_gen_mov_env_TN(ksp
, cpu_R
[R_SP
]);
2098 tcg_gen_mov_tl(cpu_R
[R_SP
], cpu_PR
[PR_USP
]);
2099 dc
->cpustate_changed
= 1;
2101 tcg_gen_ori_tl(cpu_PR
[PR_CCS
], cpu_PR
[PR_CCS
], flags
);
2104 tcg_gen_andi_tl(cpu_PR
[PR_CCS
], cpu_PR
[PR_CCS
], ~flags
);
2106 dc
->flags_uptodate
= 1;
2111 static unsigned int dec_move_rs(DisasContext
*dc
)
2113 DIS(fprintf (logfile
, "move $r%u, $s%u\n", dc
->op1
, dc
->op2
));
2114 cris_cc_mask(dc
, 0);
2115 tcg_gen_helper_0_2(helper_movl_sreg_reg
,
2116 tcg_const_tl(dc
->op2
), tcg_const_tl(dc
->op1
));
2119 static unsigned int dec_move_sr(DisasContext
*dc
)
2121 DIS(fprintf (logfile
, "move $s%u, $r%u\n", dc
->op2
, dc
->op1
));
2122 cris_cc_mask(dc
, 0);
2123 tcg_gen_helper_0_2(helper_movl_reg_sreg
,
2124 tcg_const_tl(dc
->op1
), tcg_const_tl(dc
->op2
));
2128 static unsigned int dec_move_rp(DisasContext
*dc
)
2130 DIS(fprintf (logfile
, "move $r%u, $p%u\n", dc
->op1
, dc
->op2
));
2131 cris_cc_mask(dc
, 0);
2133 if (dc
->op2
== PR_CCS
) {
2134 cris_evaluate_flags(dc
);
2135 t_gen_mov_TN_reg(cpu_T
[0], dc
->op1
);
2136 if (dc
->tb_flags
& U_FLAG
) {
2137 /* User space is not allowed to touch all flags. */
2138 tcg_gen_andi_tl(cpu_T
[0], cpu_T
[0], 0x39f);
2139 tcg_gen_andi_tl(cpu_T
[1], cpu_PR
[PR_CCS
], ~0x39f);
2140 tcg_gen_or_tl(cpu_T
[0], cpu_T
[1], cpu_T
[0]);
2144 t_gen_mov_TN_reg(cpu_T
[0], dc
->op1
);
2146 t_gen_mov_preg_TN(dc
, dc
->op2
, cpu_T
[0]);
2147 if (dc
->op2
== PR_CCS
) {
2148 cris_update_cc_op(dc
, CC_OP_FLAGS
, 4);
2149 dc
->flags_uptodate
= 1;
2153 static unsigned int dec_move_pr(DisasContext
*dc
)
2155 DIS(fprintf (logfile
, "move $p%u, $r%u\n", dc
->op1
, dc
->op2
));
2156 cris_cc_mask(dc
, 0);
2158 if (dc
->op2
== PR_CCS
)
2159 cris_evaluate_flags(dc
);
2161 t_gen_mov_TN_preg(cpu_T
[1], dc
->op2
);
2162 cris_alu(dc
, CC_OP_MOVE
,
2163 cpu_R
[dc
->op1
], cpu_R
[dc
->op1
], cpu_T
[1],
2164 preg_sizes
[dc
->op2
]);
2168 static unsigned int dec_move_mr(DisasContext
*dc
)
2170 int memsize
= memsize_zz(dc
);
2172 DIS(fprintf (logfile
, "move.%c [$r%u%s, $r%u\n",
2173 memsize_char(memsize
),
2174 dc
->op1
, dc
->postinc
? "+]" : "]",
2178 insn_len
= dec_prep_move_m(dc
, 0, 4, cpu_R
[dc
->op2
]);
2179 cris_cc_mask(dc
, CC_MASK_NZ
);
2180 cris_update_cc_op(dc
, CC_OP_MOVE
, 4);
2181 cris_update_cc_x(dc
);
2182 cris_update_result(dc
, cpu_R
[dc
->op2
]);
2185 insn_len
= dec_prep_move_m(dc
, 0, memsize
, cpu_T
[1]);
2186 cris_cc_mask(dc
, CC_MASK_NZ
);
2187 cris_alu(dc
, CC_OP_MOVE
,
2188 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], cpu_T
[1], memsize
);
2190 do_postinc(dc
, memsize
);
2194 static unsigned int dec_movs_m(DisasContext
*dc
)
2196 int memsize
= memsize_z(dc
);
2198 DIS(fprintf (logfile
, "movs.%c [$r%u%s, $r%u\n",
2199 memsize_char(memsize
),
2200 dc
->op1
, dc
->postinc
? "+]" : "]",
2204 insn_len
= dec_prep_alu_m(dc
, 1, memsize
);
2205 cris_cc_mask(dc
, CC_MASK_NZ
);
2206 cris_alu(dc
, CC_OP_MOVE
,
2207 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], cpu_T
[1], 4);
2208 do_postinc(dc
, memsize
);
2212 static unsigned int dec_addu_m(DisasContext
*dc
)
2214 int memsize
= memsize_z(dc
);
2216 DIS(fprintf (logfile
, "addu.%c [$r%u%s, $r%u\n",
2217 memsize_char(memsize
),
2218 dc
->op1
, dc
->postinc
? "+]" : "]",
2222 insn_len
= dec_prep_alu_m(dc
, 0, memsize
);
2223 cris_cc_mask(dc
, CC_MASK_NZVC
);
2224 cris_alu(dc
, CC_OP_ADD
,
2225 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], cpu_T
[1], 4);
2226 do_postinc(dc
, memsize
);
2230 static unsigned int dec_adds_m(DisasContext
*dc
)
2232 int memsize
= memsize_z(dc
);
2234 DIS(fprintf (logfile
, "adds.%c [$r%u%s, $r%u\n",
2235 memsize_char(memsize
),
2236 dc
->op1
, dc
->postinc
? "+]" : "]",
2240 insn_len
= dec_prep_alu_m(dc
, 1, memsize
);
2241 cris_cc_mask(dc
, CC_MASK_NZVC
);
2242 cris_alu(dc
, CC_OP_ADD
,
2243 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], cpu_T
[1], 4);
2244 do_postinc(dc
, memsize
);
2248 static unsigned int dec_subu_m(DisasContext
*dc
)
2250 int memsize
= memsize_z(dc
);
2252 DIS(fprintf (logfile
, "subu.%c [$r%u%s, $r%u\n",
2253 memsize_char(memsize
),
2254 dc
->op1
, dc
->postinc
? "+]" : "]",
2258 insn_len
= dec_prep_alu_m(dc
, 0, memsize
);
2259 cris_cc_mask(dc
, CC_MASK_NZVC
);
2260 cris_alu(dc
, CC_OP_SUB
,
2261 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], cpu_T
[1], 4);
2262 do_postinc(dc
, memsize
);
2266 static unsigned int dec_subs_m(DisasContext
*dc
)
2268 int memsize
= memsize_z(dc
);
2270 DIS(fprintf (logfile
, "subs.%c [$r%u%s, $r%u\n",
2271 memsize_char(memsize
),
2272 dc
->op1
, dc
->postinc
? "+]" : "]",
2276 insn_len
= dec_prep_alu_m(dc
, 1, memsize
);
2277 cris_cc_mask(dc
, CC_MASK_NZVC
);
2278 cris_alu(dc
, CC_OP_SUB
,
2279 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], cpu_T
[1], 4);
2280 do_postinc(dc
, memsize
);
2284 static unsigned int dec_movu_m(DisasContext
*dc
)
2286 int memsize
= memsize_z(dc
);
2289 DIS(fprintf (logfile
, "movu.%c [$r%u%s, $r%u\n",
2290 memsize_char(memsize
),
2291 dc
->op1
, dc
->postinc
? "+]" : "]",
2294 insn_len
= dec_prep_alu_m(dc
, 0, memsize
);
2295 cris_cc_mask(dc
, CC_MASK_NZ
);
2296 cris_alu(dc
, CC_OP_MOVE
,
2297 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], cpu_T
[1], 4);
2298 do_postinc(dc
, memsize
);
2302 static unsigned int dec_cmpu_m(DisasContext
*dc
)
2304 int memsize
= memsize_z(dc
);
2306 DIS(fprintf (logfile
, "cmpu.%c [$r%u%s, $r%u\n",
2307 memsize_char(memsize
),
2308 dc
->op1
, dc
->postinc
? "+]" : "]",
2311 insn_len
= dec_prep_alu_m(dc
, 0, memsize
);
2312 cris_cc_mask(dc
, CC_MASK_NZVC
);
2313 cris_alu(dc
, CC_OP_CMP
,
2314 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], cpu_T
[1], 4);
2315 do_postinc(dc
, memsize
);
2319 static unsigned int dec_cmps_m(DisasContext
*dc
)
2321 int memsize
= memsize_z(dc
);
2323 DIS(fprintf (logfile
, "cmps.%c [$r%u%s, $r%u\n",
2324 memsize_char(memsize
),
2325 dc
->op1
, dc
->postinc
? "+]" : "]",
2328 insn_len
= dec_prep_alu_m(dc
, 1, memsize
);
2329 cris_cc_mask(dc
, CC_MASK_NZVC
);
2330 cris_alu(dc
, CC_OP_CMP
,
2331 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], cpu_T
[1],
2333 do_postinc(dc
, memsize
);
2337 static unsigned int dec_cmp_m(DisasContext
*dc
)
2339 int memsize
= memsize_zz(dc
);
2341 DIS(fprintf (logfile
, "cmp.%c [$r%u%s, $r%u\n",
2342 memsize_char(memsize
),
2343 dc
->op1
, dc
->postinc
? "+]" : "]",
2346 insn_len
= dec_prep_alu_m(dc
, 0, memsize
);
2347 cris_cc_mask(dc
, CC_MASK_NZVC
);
2348 cris_alu(dc
, CC_OP_CMP
,
2349 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], cpu_T
[1],
2351 do_postinc(dc
, memsize
);
2355 static unsigned int dec_test_m(DisasContext
*dc
)
2357 int memsize
= memsize_zz(dc
);
2359 DIS(fprintf (logfile
, "test.%d [$r%u%s] op2=%x\n",
2360 memsize_char(memsize
),
2361 dc
->op1
, dc
->postinc
? "+]" : "]",
2364 cris_evaluate_flags(dc
);
2366 insn_len
= dec_prep_alu_m(dc
, 0, memsize
);
2367 cris_cc_mask(dc
, CC_MASK_NZ
);
2368 tcg_gen_andi_tl(cpu_PR
[PR_CCS
], cpu_PR
[PR_CCS
], ~3);
2370 cris_alu(dc
, CC_OP_CMP
,
2371 cpu_R
[dc
->op2
], cpu_T
[1], tcg_const_tl(0),
2373 do_postinc(dc
, memsize
);
2377 static unsigned int dec_and_m(DisasContext
*dc
)
2379 int memsize
= memsize_zz(dc
);
2381 DIS(fprintf (logfile
, "and.%d [$r%u%s, $r%u\n",
2382 memsize_char(memsize
),
2383 dc
->op1
, dc
->postinc
? "+]" : "]",
2386 insn_len
= dec_prep_alu_m(dc
, 0, memsize
);
2387 cris_cc_mask(dc
, CC_MASK_NZ
);
2388 cris_alu(dc
, CC_OP_AND
,
2389 cpu_R
[dc
->op2
], cpu_T
[0], cpu_T
[1],
2391 do_postinc(dc
, memsize
);
2395 static unsigned int dec_add_m(DisasContext
*dc
)
2397 int memsize
= memsize_zz(dc
);
2399 DIS(fprintf (logfile
, "add.%d [$r%u%s, $r%u\n",
2400 memsize_char(memsize
),
2401 dc
->op1
, dc
->postinc
? "+]" : "]",
2404 insn_len
= dec_prep_alu_m(dc
, 0, memsize
);
2405 cris_cc_mask(dc
, CC_MASK_NZVC
);
2406 cris_alu(dc
, CC_OP_ADD
,
2407 cpu_R
[dc
->op2
], cpu_T
[0], cpu_T
[1],
2409 do_postinc(dc
, memsize
);
2413 static unsigned int dec_addo_m(DisasContext
*dc
)
2415 int memsize
= memsize_zz(dc
);
2417 DIS(fprintf (logfile
, "add.%d [$r%u%s, $r%u\n",
2418 memsize_char(memsize
),
2419 dc
->op1
, dc
->postinc
? "+]" : "]",
2422 insn_len
= dec_prep_alu_m(dc
, 1, memsize
);
2423 cris_cc_mask(dc
, 0);
2424 cris_alu(dc
, CC_OP_ADD
,
2425 cpu_R
[R_ACR
], cpu_T
[0], cpu_T
[1], 4);
2426 do_postinc(dc
, memsize
);
2430 static unsigned int dec_bound_m(DisasContext
*dc
)
2432 int memsize
= memsize_zz(dc
);
2434 DIS(fprintf (logfile
, "bound.%d [$r%u%s, $r%u\n",
2435 memsize_char(memsize
),
2436 dc
->op1
, dc
->postinc
? "+]" : "]",
2439 insn_len
= dec_prep_alu_m(dc
, 0, memsize
);
2440 cris_cc_mask(dc
, CC_MASK_NZ
);
2441 cris_alu(dc
, CC_OP_BOUND
,
2442 cpu_R
[dc
->op2
], cpu_T
[0], cpu_T
[1], 4);
2443 do_postinc(dc
, memsize
);
2447 static unsigned int dec_addc_mr(DisasContext
*dc
)
2450 DIS(fprintf (logfile
, "addc [$r%u%s, $r%u\n",
2451 dc
->op1
, dc
->postinc
? "+]" : "]",
2454 cris_evaluate_flags(dc
);
2455 insn_len
= dec_prep_alu_m(dc
, 0, 4);
2456 cris_cc_mask(dc
, CC_MASK_NZVC
);
2457 cris_alu(dc
, CC_OP_ADDC
,
2458 cpu_R
[dc
->op2
], cpu_T
[0], cpu_T
[1], 4);
2463 static unsigned int dec_sub_m(DisasContext
*dc
)
2465 int memsize
= memsize_zz(dc
);
2467 DIS(fprintf (logfile
, "sub.%c [$r%u%s, $r%u ir=%x zz=%x\n",
2468 memsize_char(memsize
),
2469 dc
->op1
, dc
->postinc
? "+]" : "]",
2470 dc
->op2
, dc
->ir
, dc
->zzsize
));
2472 insn_len
= dec_prep_alu_m(dc
, 0, memsize
);
2473 cris_cc_mask(dc
, CC_MASK_NZVC
);
2474 cris_alu(dc
, CC_OP_SUB
,
2475 cpu_R
[dc
->op2
], cpu_T
[0], cpu_T
[1], memsize
);
2476 do_postinc(dc
, memsize
);
2480 static unsigned int dec_or_m(DisasContext
*dc
)
2482 int memsize
= memsize_zz(dc
);
2484 DIS(fprintf (logfile
, "or.%d [$r%u%s, $r%u pc=%x\n",
2485 memsize_char(memsize
),
2486 dc
->op1
, dc
->postinc
? "+]" : "]",
2489 insn_len
= dec_prep_alu_m(dc
, 0, memsize
);
2490 cris_cc_mask(dc
, CC_MASK_NZ
);
2491 cris_alu(dc
, CC_OP_OR
,
2492 cpu_R
[dc
->op2
], cpu_T
[0], cpu_T
[1], memsize_zz(dc
));
2493 do_postinc(dc
, memsize
);
2497 static unsigned int dec_move_mp(DisasContext
*dc
)
2499 int memsize
= memsize_zz(dc
);
2502 DIS(fprintf (logfile
, "move.%c [$r%u%s, $p%u\n",
2503 memsize_char(memsize
),
2505 dc
->postinc
? "+]" : "]",
2508 insn_len
= dec_prep_alu_m(dc
, 0, memsize
);
2509 cris_cc_mask(dc
, 0);
2510 if (dc
->op2
== PR_CCS
) {
2511 cris_evaluate_flags(dc
);
2512 if (dc
->tb_flags
& U_FLAG
) {
2513 /* User space is not allowed to touch all flags. */
2514 tcg_gen_andi_tl(cpu_T
[1], cpu_T
[1], 0x39f);
2515 tcg_gen_andi_tl(cpu_T
[0], cpu_PR
[PR_CCS
], ~0x39f);
2516 tcg_gen_or_tl(cpu_T
[1], cpu_T
[0], cpu_T
[1]);
2520 t_gen_mov_preg_TN(dc
, dc
->op2
, cpu_T
[1]);
2522 do_postinc(dc
, memsize
);
2526 static unsigned int dec_move_pm(DisasContext
*dc
)
2530 memsize
= preg_sizes
[dc
->op2
];
2532 DIS(fprintf (logfile
, "move.%c $p%u, [$r%u%s\n",
2533 memsize_char(memsize
),
2534 dc
->op2
, dc
->op1
, dc
->postinc
? "+]" : "]"));
2536 /* prepare store. Address in T0, value in T1. */
2537 if (dc
->op2
== PR_CCS
)
2538 cris_evaluate_flags(dc
);
2539 t_gen_mov_TN_preg(cpu_T
[1], dc
->op2
);
2540 cris_flush_cc_state(dc
);
2541 gen_store(dc
, cpu_R
[dc
->op1
], cpu_T
[1], memsize
);
2543 cris_cc_mask(dc
, 0);
2545 tcg_gen_addi_tl(cpu_R
[dc
->op1
], cpu_R
[dc
->op1
], memsize
);
2549 static unsigned int dec_movem_mr(DisasContext
*dc
)
2553 int nr
= dc
->op2
+ 1;
2555 DIS(fprintf (logfile
, "movem [$r%u%s, $r%u\n", dc
->op1
,
2556 dc
->postinc
? "+]" : "]", dc
->op2
));
2558 /* There are probably better ways of doing this. */
2559 cris_flush_cc_state(dc
);
2560 for (i
= 0; i
< (nr
>> 1); i
++) {
2561 tmp
[i
] = tcg_temp_new(TCG_TYPE_I64
);
2562 tcg_gen_addi_tl(cpu_T
[0], cpu_R
[dc
->op1
], i
* 8);
2563 gen_load(dc
, tmp
[i
], cpu_T
[0], 8, 0);
2566 tmp
[i
] = tcg_temp_new(TCG_TYPE_I32
);
2567 tcg_gen_addi_tl(cpu_T
[0], cpu_R
[dc
->op1
], i
* 8);
2568 gen_load(dc
, tmp
[i
], cpu_T
[0], 4, 0);
2571 for (i
= 0; i
< (nr
>> 1); i
++) {
2572 tcg_gen_trunc_i64_i32(cpu_R
[i
* 2], tmp
[i
]);
2573 tcg_gen_shri_i64(tmp
[i
], tmp
[i
], 32);
2574 tcg_gen_trunc_i64_i32(cpu_R
[i
* 2 + 1], tmp
[i
]);
2575 tcg_temp_free(tmp
[i
]);
2578 tcg_gen_mov_tl(cpu_R
[dc
->op2
], tmp
[i
]);
2579 tcg_temp_free(tmp
[i
]);
2582 /* writeback the updated pointer value. */
2584 tcg_gen_addi_tl(cpu_R
[dc
->op1
], cpu_R
[dc
->op1
], nr
* 4);
2586 /* gen_load might want to evaluate the previous insns flags. */
2587 cris_cc_mask(dc
, 0);
2591 static unsigned int dec_movem_rm(DisasContext
*dc
)
2596 DIS(fprintf (logfile
, "movem $r%u, [$r%u%s\n", dc
->op2
, dc
->op1
,
2597 dc
->postinc
? "+]" : "]"));
2599 cris_flush_cc_state(dc
);
2601 tmp
= tcg_temp_new(TCG_TYPE_TL
);
2602 tcg_gen_movi_tl(tmp
, 4);
2603 tcg_gen_mov_tl(cpu_T
[0], cpu_R
[dc
->op1
]);
2604 for (i
= 0; i
<= dc
->op2
; i
++) {
2605 /* Displace addr. */
2606 /* Perform the store. */
2607 gen_store(dc
, cpu_T
[0], cpu_R
[i
], 4);
2608 tcg_gen_add_tl(cpu_T
[0], cpu_T
[0], tmp
);
2611 tcg_gen_mov_tl(cpu_R
[dc
->op1
], cpu_T
[0]);
2612 cris_cc_mask(dc
, 0);
2617 static unsigned int dec_move_rm(DisasContext
*dc
)
2621 memsize
= memsize_zz(dc
);
2623 DIS(fprintf (logfile
, "move.%d $r%u, [$r%u]\n",
2624 memsize
, dc
->op2
, dc
->op1
));
2626 /* prepare store. */
2627 cris_flush_cc_state(dc
);
2628 gen_store(dc
, cpu_R
[dc
->op1
], cpu_R
[dc
->op2
], memsize
);
2631 tcg_gen_addi_tl(cpu_R
[dc
->op1
], cpu_R
[dc
->op1
], memsize
);
2632 cris_cc_mask(dc
, 0);
2636 static unsigned int dec_lapcq(DisasContext
*dc
)
2638 DIS(fprintf (logfile
, "lapcq %x, $r%u\n",
2639 dc
->pc
+ dc
->op1
*2, dc
->op2
));
2640 cris_cc_mask(dc
, 0);
2641 tcg_gen_movi_tl(cpu_R
[dc
->op2
], dc
->pc
+ dc
->op1
* 2);
2645 static unsigned int dec_lapc_im(DisasContext
*dc
)
2653 cris_cc_mask(dc
, 0);
2654 imm
= ldl_code(dc
->pc
+ 2);
2655 DIS(fprintf (logfile
, "lapc 0x%x, $r%u\n", imm
+ dc
->pc
, dc
->op2
));
2659 t_gen_mov_reg_TN(rd
, tcg_const_tl(pc
));
2663 /* Jump to special reg. */
2664 static unsigned int dec_jump_p(DisasContext
*dc
)
2666 DIS(fprintf (logfile
, "jump $p%u\n", dc
->op2
));
2668 if (dc
->op2
== PR_CCS
)
2669 cris_evaluate_flags(dc
);
2670 t_gen_mov_TN_preg(cpu_T
[0], dc
->op2
);
2671 /* rete will often have low bit set to indicate delayslot. */
2672 tcg_gen_andi_tl(env_btarget
, cpu_T
[0], ~1);
2673 cris_cc_mask(dc
, 0);
2674 cris_prepare_jmp(dc
, JMP_INDIRECT
);
2678 /* Jump and save. */
2679 static unsigned int dec_jas_r(DisasContext
*dc
)
2681 DIS(fprintf (logfile
, "jas $r%u, $p%u\n", dc
->op1
, dc
->op2
));
2682 cris_cc_mask(dc
, 0);
2683 /* Store the return address in Pd. */
2684 tcg_gen_mov_tl(env_btarget
, cpu_R
[dc
->op1
]);
2687 t_gen_mov_preg_TN(dc
, dc
->op2
, tcg_const_tl(dc
->pc
+ 4));
2689 cris_prepare_jmp(dc
, JMP_INDIRECT
);
2693 static unsigned int dec_jas_im(DisasContext
*dc
)
2697 imm
= ldl_code(dc
->pc
+ 2);
2699 DIS(fprintf (logfile
, "jas 0x%x\n", imm
));
2700 cris_cc_mask(dc
, 0);
2701 /* Store the return address in Pd. */
2702 t_gen_mov_preg_TN(dc
, dc
->op2
, tcg_const_tl(dc
->pc
+ 8));
2705 cris_prepare_jmp(dc
, JMP_DIRECT
);
2709 static unsigned int dec_jasc_im(DisasContext
*dc
)
2713 imm
= ldl_code(dc
->pc
+ 2);
2715 DIS(fprintf (logfile
, "jasc 0x%x\n", imm
));
2716 cris_cc_mask(dc
, 0);
2717 /* Store the return address in Pd. */
2718 t_gen_mov_preg_TN(dc
, dc
->op2
, tcg_const_tl(dc
->pc
+ 8 + 4));
2721 cris_prepare_jmp(dc
, JMP_DIRECT
);
2725 static unsigned int dec_jasc_r(DisasContext
*dc
)
2727 DIS(fprintf (logfile
, "jasc_r $r%u, $p%u\n", dc
->op1
, dc
->op2
));
2728 cris_cc_mask(dc
, 0);
2729 /* Store the return address in Pd. */
2730 tcg_gen_mov_tl(env_btarget
, cpu_R
[dc
->op1
]);
2731 t_gen_mov_preg_TN(dc
, dc
->op2
, tcg_const_tl(dc
->pc
+ 4 + 4));
2732 cris_prepare_jmp(dc
, JMP_INDIRECT
);
2736 static unsigned int dec_bcc_im(DisasContext
*dc
)
2739 uint32_t cond
= dc
->op2
;
2741 offset
= ldsw_code(dc
->pc
+ 2);
2743 DIS(fprintf (logfile
, "b%s %d pc=%x dst=%x\n",
2744 cc_name(cond
), offset
,
2745 dc
->pc
, dc
->pc
+ offset
));
2747 cris_cc_mask(dc
, 0);
2748 /* op2 holds the condition-code. */
2749 cris_prepare_cc_branch (dc
, offset
, cond
);
2753 static unsigned int dec_bas_im(DisasContext
*dc
)
2758 simm
= ldl_code(dc
->pc
+ 2);
2760 DIS(fprintf (logfile
, "bas 0x%x, $p%u\n", dc
->pc
+ simm
, dc
->op2
));
2761 cris_cc_mask(dc
, 0);
2762 /* Store the return address in Pd. */
2763 t_gen_mov_preg_TN(dc
, dc
->op2
, tcg_const_tl(dc
->pc
+ 8));
2765 dc
->jmp_pc
= dc
->pc
+ simm
;
2766 cris_prepare_jmp(dc
, JMP_DIRECT
);
2770 static unsigned int dec_basc_im(DisasContext
*dc
)
2773 simm
= ldl_code(dc
->pc
+ 2);
2775 DIS(fprintf (logfile
, "basc 0x%x, $p%u\n", dc
->pc
+ simm
, dc
->op2
));
2776 cris_cc_mask(dc
, 0);
2777 /* Store the return address in Pd. */
2778 t_gen_mov_preg_TN(dc
, dc
->op2
, tcg_const_tl(dc
->pc
+ 12));
2780 dc
->jmp_pc
= dc
->pc
+ simm
;
2781 cris_prepare_jmp(dc
, JMP_DIRECT
);
2785 static unsigned int dec_rfe_etc(DisasContext
*dc
)
2787 DIS(fprintf (logfile
, "rfe_etc opc=%x pc=0x%x op1=%d op2=%d\n",
2788 dc
->opcode
, dc
->pc
, dc
->op1
, dc
->op2
));
2790 cris_cc_mask(dc
, 0);
2792 if (dc
->op2
== 15) /* ignore halt. */
2795 switch (dc
->op2
& 7) {
2798 cris_evaluate_flags(dc
);
2799 tcg_gen_helper_0_0(helper_rfe
);
2800 dc
->is_jmp
= DISAS_UPDATE
;
2804 cris_evaluate_flags(dc
);
2805 tcg_gen_helper_0_0(helper_rfn
);
2806 dc
->is_jmp
= DISAS_UPDATE
;
2810 tcg_gen_movi_tl(env_pc
, dc
->pc
);
2811 /* Breaks start at 16 in the exception vector. */
2812 t_gen_mov_env_TN(trap_vector
,
2813 tcg_const_tl(dc
->op1
+ 16));
2814 t_gen_raise_exception(EXCP_BREAK
);
2815 dc
->is_jmp
= DISAS_UPDATE
;
2818 printf ("op2=%x\n", dc
->op2
);
2826 static unsigned int dec_ftag_fidx_d_m(DisasContext
*dc
)
2828 /* Ignore D-cache flushes. */
2832 static unsigned int dec_ftag_fidx_i_m(DisasContext
*dc
)
2834 /* Ignore I-cache flushes. */
2838 static unsigned int dec_null(DisasContext
*dc
)
2840 printf ("unknown insn pc=%x opc=%x op1=%x op2=%x\n",
2841 dc
->pc
, dc
->opcode
, dc
->op1
, dc
->op2
);
2847 struct decoder_info
{
2852 unsigned int (*dec
)(DisasContext
*dc
);
2854 /* Order matters here. */
2855 {DEC_MOVEQ
, dec_moveq
},
2856 {DEC_BTSTQ
, dec_btstq
},
2857 {DEC_CMPQ
, dec_cmpq
},
2858 {DEC_ADDOQ
, dec_addoq
},
2859 {DEC_ADDQ
, dec_addq
},
2860 {DEC_SUBQ
, dec_subq
},
2861 {DEC_ANDQ
, dec_andq
},
2863 {DEC_ASRQ
, dec_asrq
},
2864 {DEC_LSLQ
, dec_lslq
},
2865 {DEC_LSRQ
, dec_lsrq
},
2866 {DEC_BCCQ
, dec_bccq
},
2868 {DEC_BCC_IM
, dec_bcc_im
},
2869 {DEC_JAS_IM
, dec_jas_im
},
2870 {DEC_JAS_R
, dec_jas_r
},
2871 {DEC_JASC_IM
, dec_jasc_im
},
2872 {DEC_JASC_R
, dec_jasc_r
},
2873 {DEC_BAS_IM
, dec_bas_im
},
2874 {DEC_BASC_IM
, dec_basc_im
},
2875 {DEC_JUMP_P
, dec_jump_p
},
2876 {DEC_LAPC_IM
, dec_lapc_im
},
2877 {DEC_LAPCQ
, dec_lapcq
},
2879 {DEC_RFE_ETC
, dec_rfe_etc
},
2880 {DEC_ADDC_MR
, dec_addc_mr
},
2882 {DEC_MOVE_MP
, dec_move_mp
},
2883 {DEC_MOVE_PM
, dec_move_pm
},
2884 {DEC_MOVEM_MR
, dec_movem_mr
},
2885 {DEC_MOVEM_RM
, dec_movem_rm
},
2886 {DEC_MOVE_PR
, dec_move_pr
},
2887 {DEC_SCC_R
, dec_scc_r
},
2888 {DEC_SETF
, dec_setclrf
},
2889 {DEC_CLEARF
, dec_setclrf
},
2891 {DEC_MOVE_SR
, dec_move_sr
},
2892 {DEC_MOVE_RP
, dec_move_rp
},
2893 {DEC_SWAP_R
, dec_swap_r
},
2894 {DEC_ABS_R
, dec_abs_r
},
2895 {DEC_LZ_R
, dec_lz_r
},
2896 {DEC_MOVE_RS
, dec_move_rs
},
2897 {DEC_BTST_R
, dec_btst_r
},
2898 {DEC_ADDC_R
, dec_addc_r
},
2900 {DEC_DSTEP_R
, dec_dstep_r
},
2901 {DEC_XOR_R
, dec_xor_r
},
2902 {DEC_MCP_R
, dec_mcp_r
},
2903 {DEC_CMP_R
, dec_cmp_r
},
2905 {DEC_ADDI_R
, dec_addi_r
},
2906 {DEC_ADDI_ACR
, dec_addi_acr
},
2908 {DEC_ADD_R
, dec_add_r
},
2909 {DEC_SUB_R
, dec_sub_r
},
2911 {DEC_ADDU_R
, dec_addu_r
},
2912 {DEC_ADDS_R
, dec_adds_r
},
2913 {DEC_SUBU_R
, dec_subu_r
},
2914 {DEC_SUBS_R
, dec_subs_r
},
2915 {DEC_LSL_R
, dec_lsl_r
},
2917 {DEC_AND_R
, dec_and_r
},
2918 {DEC_OR_R
, dec_or_r
},
2919 {DEC_BOUND_R
, dec_bound_r
},
2920 {DEC_ASR_R
, dec_asr_r
},
2921 {DEC_LSR_R
, dec_lsr_r
},
2923 {DEC_MOVU_R
, dec_movu_r
},
2924 {DEC_MOVS_R
, dec_movs_r
},
2925 {DEC_NEG_R
, dec_neg_r
},
2926 {DEC_MOVE_R
, dec_move_r
},
2928 {DEC_FTAG_FIDX_I_M
, dec_ftag_fidx_i_m
},
2929 {DEC_FTAG_FIDX_D_M
, dec_ftag_fidx_d_m
},
2931 {DEC_MULS_R
, dec_muls_r
},
2932 {DEC_MULU_R
, dec_mulu_r
},
2934 {DEC_ADDU_M
, dec_addu_m
},
2935 {DEC_ADDS_M
, dec_adds_m
},
2936 {DEC_SUBU_M
, dec_subu_m
},
2937 {DEC_SUBS_M
, dec_subs_m
},
2939 {DEC_CMPU_M
, dec_cmpu_m
},
2940 {DEC_CMPS_M
, dec_cmps_m
},
2941 {DEC_MOVU_M
, dec_movu_m
},
2942 {DEC_MOVS_M
, dec_movs_m
},
2944 {DEC_CMP_M
, dec_cmp_m
},
2945 {DEC_ADDO_M
, dec_addo_m
},
2946 {DEC_BOUND_M
, dec_bound_m
},
2947 {DEC_ADD_M
, dec_add_m
},
2948 {DEC_SUB_M
, dec_sub_m
},
2949 {DEC_AND_M
, dec_and_m
},
2950 {DEC_OR_M
, dec_or_m
},
2951 {DEC_MOVE_RM
, dec_move_rm
},
2952 {DEC_TEST_M
, dec_test_m
},
2953 {DEC_MOVE_MR
, dec_move_mr
},
2958 static inline unsigned int
2959 cris_decoder(DisasContext
*dc
)
2961 unsigned int insn_len
= 2;
2964 if (unlikely(loglevel
& CPU_LOG_TB_OP
))
2965 tcg_gen_debug_insn_start(dc
->pc
);
2967 /* Load a halfword onto the instruction register. */
2968 dc
->ir
= lduw_code(dc
->pc
);
2970 /* Now decode it. */
2971 dc
->opcode
= EXTRACT_FIELD(dc
->ir
, 4, 11);
2972 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 3);
2973 dc
->op2
= EXTRACT_FIELD(dc
->ir
, 12, 15);
2974 dc
->zsize
= EXTRACT_FIELD(dc
->ir
, 4, 4);
2975 dc
->zzsize
= EXTRACT_FIELD(dc
->ir
, 4, 5);
2976 dc
->postinc
= EXTRACT_FIELD(dc
->ir
, 10, 10);
2978 /* Large switch for all insns. */
2979 for (i
= 0; i
< sizeof decinfo
/ sizeof decinfo
[0]; i
++) {
2980 if ((dc
->opcode
& decinfo
[i
].mask
) == decinfo
[i
].bits
)
2982 insn_len
= decinfo
[i
].dec(dc
);
2990 static void check_breakpoint(CPUState
*env
, DisasContext
*dc
)
2993 if (env
->nb_breakpoints
> 0) {
2994 for(j
= 0; j
< env
->nb_breakpoints
; j
++) {
2995 if (env
->breakpoints
[j
] == dc
->pc
) {
2996 cris_evaluate_flags (dc
);
2997 tcg_gen_movi_tl(env_pc
, dc
->pc
);
2998 t_gen_raise_exception(EXCP_DEBUG
);
2999 dc
->is_jmp
= DISAS_UPDATE
;
3007 * Delay slots on QEMU/CRIS.
3009 * If an exception hits on a delayslot, the core will let ERP (the Exception
3010 * Return Pointer) point to the branch (the previous) insn and set the lsb to
3011 * to give SW a hint that the exception actually hit on the dslot.
3013 * CRIS expects all PC addresses to be 16-bit aligned. The lsb is ignored by
3014 * the core and any jmp to an odd addresses will mask off that lsb. It is
3015 * simply there to let sw know there was an exception on a dslot.
3017 * When the software returns from an exception, the branch will re-execute.
3018 * On QEMU care needs to be taken when a branch+delayslot sequence is broken
3019 * and the branch and delayslot dont share pages.
3021 * The TB contaning the branch insn will set up env->btarget and evaluate
3022 * env->btaken. When the translation loop exits we will note that the branch
3023 * sequence is broken and let env->dslot be the size of the branch insn (those
3026 * The TB contaning the delayslot will have the PC of its real insn (i.e no lsb
3027 * set). It will also expect to have env->dslot setup with the size of the
3028 * delay slot so that env->pc - env->dslot point to the branch insn. This TB
3029 * will execute the dslot and take the branch, either to btarget or just one
3032 * When exceptions occur, we check for env->dslot in do_interrupt to detect
3033 * broken branch sequences and setup $erp accordingly (i.e let it point to the
3034 * branch and set lsb). Then env->dslot gets cleared so that the exception
3035 * handler can enter. When returning from exceptions (jump $erp) the lsb gets
3036 * masked off and we will reexecute the branch insn.
3040 /* generate intermediate code for basic block 'tb'. */
3042 gen_intermediate_code_internal(CPUState
*env
, TranslationBlock
*tb
,
3045 uint16_t *gen_opc_end
;
3047 unsigned int insn_len
;
3049 struct DisasContext ctx
;
3050 struct DisasContext
*dc
= &ctx
;
3051 uint32_t next_page_start
;
3059 /* Odd PC indicates that branch is rexecuting due to exception in the
3060 * delayslot, like in real hw.
3062 pc_start
= tb
->pc
& ~1;
3066 gen_opc_end
= gen_opc_buf
+ OPC_MAX_SIZE
;
3068 dc
->is_jmp
= DISAS_NEXT
;
3071 dc
->singlestep_enabled
= env
->singlestep_enabled
;
3072 dc
->flags_uptodate
= 1;
3073 dc
->flagx_known
= 1;
3074 dc
->flags_x
= tb
->flags
& X_FLAG
;
3075 dc
->cc_x_uptodate
= 0;
3079 cris_update_cc_op(dc
, CC_OP_FLAGS
, 4);
3080 dc
->cc_size_uptodate
= -1;
3082 /* Decode TB flags. */
3083 dc
->tb_flags
= tb
->flags
& (P_FLAG
| U_FLAG
| X_FLAG
);
3084 dc
->delayed_branch
= !!(tb
->flags
& 7);
3085 if (dc
->delayed_branch
)
3086 dc
->jmp
= JMP_INDIRECT
;
3088 dc
->jmp
= JMP_NOJMP
;
3090 dc
->cpustate_changed
= 0;
3092 if (loglevel
& CPU_LOG_TB_IN_ASM
) {
3094 "srch=%d pc=%x %x flg=%llx bt=%x ds=%u ccs=%x\n"
3100 search_pc
, dc
->pc
, dc
->ppc
,
3101 (unsigned long long)tb
->flags
,
3102 env
->btarget
, (unsigned)tb
->flags
& 7,
3104 env
->pregs
[PR_PID
], env
->pregs
[PR_USP
],
3105 env
->regs
[0], env
->regs
[1], env
->regs
[2], env
->regs
[3],
3106 env
->regs
[4], env
->regs
[5], env
->regs
[6], env
->regs
[7],
3107 env
->regs
[8], env
->regs
[9],
3108 env
->regs
[10], env
->regs
[11],
3109 env
->regs
[12], env
->regs
[13],
3110 env
->regs
[14], env
->regs
[15]);
3114 next_page_start
= (pc_start
& TARGET_PAGE_MASK
) + TARGET_PAGE_SIZE
;
3117 max_insns
= tb
->cflags
& CF_COUNT_MASK
;
3119 max_insns
= CF_COUNT_MASK
;
3124 check_breakpoint(env
, dc
);
3127 j
= gen_opc_ptr
- gen_opc_buf
;
3131 gen_opc_instr_start
[lj
++] = 0;
3133 if (dc
->delayed_branch
== 1)
3134 gen_opc_pc
[lj
] = dc
->ppc
| 1;
3136 gen_opc_pc
[lj
] = dc
->pc
;
3137 gen_opc_instr_start
[lj
] = 1;
3138 gen_opc_icount
[lj
] = num_insns
;
3142 DIS(fprintf(logfile
, "%x ", dc
->pc
));
3144 DIS(fprintf(logfile
, "%x ", dc
->pc
));
3147 if (num_insns
+ 1 == max_insns
&& (tb
->cflags
& CF_LAST_IO
))
3151 insn_len
= cris_decoder(dc
);
3155 cris_clear_x_flag(dc
);
3158 /* Check for delayed branches here. If we do it before
3159 actually generating any host code, the simulator will just
3160 loop doing nothing for on this program location. */
3161 if (dc
->delayed_branch
) {
3162 dc
->delayed_branch
--;
3163 if (dc
->delayed_branch
== 0)
3166 t_gen_mov_env_TN(dslot
,
3168 if (dc
->jmp
== JMP_DIRECT
) {
3169 dc
->is_jmp
= DISAS_NEXT
;
3171 t_gen_cc_jmp(env_btarget
,
3172 tcg_const_tl(dc
->pc
));
3173 dc
->is_jmp
= DISAS_JUMP
;
3179 /* If we are rexecuting a branch due to exceptions on
3180 delay slots dont break. */
3181 if (!(tb
->pc
& 1) && env
->singlestep_enabled
)
3183 } while (!dc
->is_jmp
&& gen_opc_ptr
< gen_opc_end
3184 && (dc
->pc
< next_page_start
)
3185 && num_insns
< max_insns
);
3188 if (dc
->jmp
== JMP_DIRECT
&& !dc
->delayed_branch
)
3191 if (tb
->cflags
& CF_LAST_IO
)
3193 /* Force an update if the per-tb cpu state has changed. */
3194 if (dc
->is_jmp
== DISAS_NEXT
3195 && (dc
->cpustate_changed
|| !dc
->flagx_known
3196 || (dc
->flags_x
!= (tb
->flags
& X_FLAG
)))) {
3197 dc
->is_jmp
= DISAS_UPDATE
;
3198 tcg_gen_movi_tl(env_pc
, npc
);
3200 /* Broken branch+delayslot sequence. */
3201 if (dc
->delayed_branch
== 1) {
3202 /* Set env->dslot to the size of the branch insn. */
3203 t_gen_mov_env_TN(dslot
, tcg_const_tl(dc
->pc
- dc
->ppc
));
3204 cris_store_direct_jmp(dc
);
3207 cris_evaluate_flags (dc
);
3209 if (unlikely(env
->singlestep_enabled
)) {
3210 if (dc
->is_jmp
== DISAS_NEXT
)
3211 tcg_gen_movi_tl(env_pc
, npc
);
3212 t_gen_raise_exception(EXCP_DEBUG
);
3214 switch(dc
->is_jmp
) {
3216 gen_goto_tb(dc
, 1, npc
);
3221 /* indicate that the hash table must be used
3222 to find the next TB */
3227 /* nothing more to generate */
3231 gen_icount_end(tb
, num_insns
);
3232 *gen_opc_ptr
= INDEX_op_end
;
3234 j
= gen_opc_ptr
- gen_opc_buf
;
3237 gen_opc_instr_start
[lj
++] = 0;
3239 tb
->size
= dc
->pc
- pc_start
;
3240 tb
->icount
= num_insns
;
3244 if (loglevel
& CPU_LOG_TB_IN_ASM
) {
3245 fprintf(logfile
, "--------------\n");
3246 fprintf(logfile
, "IN: %s\n", lookup_symbol(pc_start
));
3247 target_disas(logfile
, pc_start
, dc
->pc
- pc_start
, 0);
3248 fprintf(logfile
, "\nisize=%d osize=%zd\n",
3249 dc
->pc
- pc_start
, gen_opc_ptr
- gen_opc_buf
);
3254 void gen_intermediate_code (CPUState
*env
, struct TranslationBlock
*tb
)
3256 gen_intermediate_code_internal(env
, tb
, 0);
3259 void gen_intermediate_code_pc (CPUState
*env
, struct TranslationBlock
*tb
)
3261 gen_intermediate_code_internal(env
, tb
, 1);
3264 void cpu_dump_state (CPUState
*env
, FILE *f
,
3265 int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...),
3274 cpu_fprintf(f
, "PC=%x CCS=%x btaken=%d btarget=%x\n"
3275 "cc_op=%d cc_src=%d cc_dest=%d cc_result=%x cc_mask=%x\n",
3276 env
->pc
, env
->pregs
[PR_CCS
], env
->btaken
, env
->btarget
,
3278 env
->cc_src
, env
->cc_dest
, env
->cc_result
, env
->cc_mask
);
3281 for (i
= 0; i
< 16; i
++) {
3282 cpu_fprintf(f
, "r%2.2d=%8.8x ", i
, env
->regs
[i
]);
3283 if ((i
+ 1) % 4 == 0)
3284 cpu_fprintf(f
, "\n");
3286 cpu_fprintf(f
, "\nspecial regs:\n");
3287 for (i
= 0; i
< 16; i
++) {
3288 cpu_fprintf(f
, "p%2.2d=%8.8x ", i
, env
->pregs
[i
]);
3289 if ((i
+ 1) % 4 == 0)
3290 cpu_fprintf(f
, "\n");
3292 srs
= env
->pregs
[PR_SRS
];
3293 cpu_fprintf(f
, "\nsupport function regs bank %x:\n", srs
);
3295 for (i
= 0; i
< 16; i
++) {
3296 cpu_fprintf(f
, "s%2.2d=%8.8x ",
3297 i
, env
->sregs
[srs
][i
]);
3298 if ((i
+ 1) % 4 == 0)
3299 cpu_fprintf(f
, "\n");
3302 cpu_fprintf(f
, "\n\n");
3306 CPUCRISState
*cpu_cris_init (const char *cpu_model
)
3309 static int tcg_initialized
= 0;
3312 env
= qemu_mallocz(sizeof(CPUCRISState
));
3319 if (tcg_initialized
)
3322 tcg_initialized
= 1;
3324 cpu_env
= tcg_global_reg_new(TCG_TYPE_PTR
, TCG_AREG0
, "env");
3325 #if TARGET_LONG_BITS > HOST_LONG_BITS
3326 cpu_T
[0] = tcg_global_mem_new(TCG_TYPE_TL
,
3327 TCG_AREG0
, offsetof(CPUState
, t0
), "T0");
3328 cpu_T
[1] = tcg_global_mem_new(TCG_TYPE_TL
,
3329 TCG_AREG0
, offsetof(CPUState
, t1
), "T1");
3331 cpu_T
[0] = tcg_global_reg_new(TCG_TYPE_TL
, TCG_AREG1
, "T0");
3332 cpu_T
[1] = tcg_global_reg_new(TCG_TYPE_TL
, TCG_AREG2
, "T1");
3335 cc_x
= tcg_global_mem_new(TCG_TYPE_PTR
, TCG_AREG0
,
3336 offsetof(CPUState
, cc_x
), "cc_x");
3337 cc_src
= tcg_global_mem_new(TCG_TYPE_PTR
, TCG_AREG0
,
3338 offsetof(CPUState
, cc_src
), "cc_src");
3339 cc_dest
= tcg_global_mem_new(TCG_TYPE_PTR
, TCG_AREG0
,
3340 offsetof(CPUState
, cc_dest
),
3342 cc_result
= tcg_global_mem_new(TCG_TYPE_PTR
, TCG_AREG0
,
3343 offsetof(CPUState
, cc_result
),
3345 cc_op
= tcg_global_mem_new(TCG_TYPE_PTR
, TCG_AREG0
,
3346 offsetof(CPUState
, cc_op
), "cc_op");
3347 cc_size
= tcg_global_mem_new(TCG_TYPE_PTR
, TCG_AREG0
,
3348 offsetof(CPUState
, cc_size
),
3350 cc_mask
= tcg_global_mem_new(TCG_TYPE_PTR
, TCG_AREG0
,
3351 offsetof(CPUState
, cc_mask
),
3354 env_pc
= tcg_global_mem_new(TCG_TYPE_PTR
, TCG_AREG0
,
3355 offsetof(CPUState
, pc
),
3357 env_btarget
= tcg_global_mem_new(TCG_TYPE_PTR
, TCG_AREG0
,
3358 offsetof(CPUState
, btarget
),
3360 env_btaken
= tcg_global_mem_new(TCG_TYPE_PTR
, TCG_AREG0
,
3361 offsetof(CPUState
, btaken
),
3363 for (i
= 0; i
< 16; i
++) {
3364 cpu_R
[i
] = tcg_global_mem_new(TCG_TYPE_PTR
, TCG_AREG0
,
3365 offsetof(CPUState
, regs
[i
]),
3368 for (i
= 0; i
< 16; i
++) {
3369 cpu_PR
[i
] = tcg_global_mem_new(TCG_TYPE_PTR
, TCG_AREG0
,
3370 offsetof(CPUState
, pregs
[i
]),
3374 TCG_HELPER(helper_raise_exception
);
3375 TCG_HELPER(helper_dump
);
3377 TCG_HELPER(helper_tlb_flush_pid
);
3378 TCG_HELPER(helper_movl_sreg_reg
);
3379 TCG_HELPER(helper_movl_reg_sreg
);
3380 TCG_HELPER(helper_rfe
);
3381 TCG_HELPER(helper_rfn
);
3383 TCG_HELPER(helper_evaluate_flags_muls
);
3384 TCG_HELPER(helper_evaluate_flags_mulu
);
3385 TCG_HELPER(helper_evaluate_flags_mcp
);
3386 TCG_HELPER(helper_evaluate_flags_alu_4
);
3387 TCG_HELPER(helper_evaluate_flags_move_4
);
3388 TCG_HELPER(helper_evaluate_flags_move_2
);
3389 TCG_HELPER(helper_evaluate_flags
);
3390 TCG_HELPER(helper_top_evaluate_flags
);
3394 void cpu_reset (CPUCRISState
*env
)
3396 memset(env
, 0, offsetof(CPUCRISState
, breakpoints
));
3399 #if defined(CONFIG_USER_ONLY)
3400 /* start in user mode with interrupts enabled. */
3401 env
->pregs
[PR_CCS
] |= U_FLAG
| I_FLAG
;
3403 env
->pregs
[PR_CCS
] = 0;
3407 void gen_pc_load(CPUState
*env
, struct TranslationBlock
*tb
,
3408 unsigned long searched_pc
, int pc_pos
, void *puc
)
3410 env
->pc
= gen_opc_pc
[pc_pos
];