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"
47 #define DIS(x) if (loglevel & CPU_LOG_TB_IN_ASM) x
53 #define BUG() (gen_BUG(dc, __FILE__, __LINE__))
54 #define BUG_ON(x) ({if (x) BUG();})
58 /* Used by the decoder. */
59 #define EXTRACT_FIELD(src, start, end) \
60 (((src) >> start) & ((1 << (end - start + 1)) - 1))
62 #define CC_MASK_NZ 0xc
63 #define CC_MASK_NZV 0xe
64 #define CC_MASK_NZVC 0xf
65 #define CC_MASK_RNZV 0x10e
67 static TCGv_ptr cpu_env
;
68 static TCGv cpu_R
[16];
69 static TCGv cpu_PR
[16];
73 static TCGv cc_result
;
78 static TCGv env_btaken
;
79 static TCGv env_btarget
;
82 #include "gen-icount.h"
84 /* This is the state at translation time. */
85 typedef struct DisasContext
{
94 unsigned int zsize
, zzsize
;
103 int cc_size_uptodate
; /* -1 invalid or last written value. */
105 int cc_x_uptodate
; /* 1 - ccs, 2 - known | X_FLAG. 0 not uptodate. */
106 int flags_uptodate
; /* Wether or not $ccs is uptodate. */
107 int flagx_known
; /* Wether or not flags_x has the x flag known at
111 int clear_x
; /* Clear x after this insn? */
112 int cpustate_changed
;
113 unsigned int tb_flags
; /* tb dependent flags. */
118 #define JMP_INDIRECT 2
119 int jmp
; /* 0=nojmp, 1=direct, 2=indirect. */
124 struct TranslationBlock
*tb
;
125 int singlestep_enabled
;
128 static void gen_BUG(DisasContext
*dc
, const char *file
, int line
)
130 printf ("BUG: pc=%x %s %d\n", dc
->pc
, file
, line
);
131 fprintf (logfile
, "BUG: pc=%x %s %d\n", dc
->pc
, file
, line
);
132 cpu_abort(dc
->env
, "%s:%d\n", file
, line
);
135 static const char *regnames
[] =
137 "$r0", "$r1", "$r2", "$r3",
138 "$r4", "$r5", "$r6", "$r7",
139 "$r8", "$r9", "$r10", "$r11",
140 "$r12", "$r13", "$sp", "$acr",
142 static const char *pregnames
[] =
144 "$bz", "$vr", "$pid", "$srs",
145 "$wz", "$exs", "$eda", "$mof",
146 "$dz", "$ebp", "$erp", "$srp",
147 "$nrp", "$ccs", "$usp", "$spc",
150 /* We need this table to handle preg-moves with implicit width. */
151 static int preg_sizes
[] = {
162 #define t_gen_mov_TN_env(tn, member) \
163 _t_gen_mov_TN_env((tn), offsetof(CPUState, member))
164 #define t_gen_mov_env_TN(member, tn) \
165 _t_gen_mov_env_TN(offsetof(CPUState, member), (tn))
167 static inline void t_gen_mov_TN_reg(TCGv tn
, int r
)
170 fprintf(stderr
, "wrong register read $r%d\n", r
);
171 tcg_gen_mov_tl(tn
, cpu_R
[r
]);
173 static inline void t_gen_mov_reg_TN(int r
, TCGv tn
)
176 fprintf(stderr
, "wrong register write $r%d\n", r
);
177 tcg_gen_mov_tl(cpu_R
[r
], tn
);
180 static inline void _t_gen_mov_TN_env(TCGv tn
, int offset
)
182 if (offset
> sizeof (CPUState
))
183 fprintf(stderr
, "wrong load from env from off=%d\n", offset
);
184 tcg_gen_ld_tl(tn
, cpu_env
, offset
);
186 static inline void _t_gen_mov_env_TN(int offset
, TCGv tn
)
188 if (offset
> sizeof (CPUState
))
189 fprintf(stderr
, "wrong store to env at off=%d\n", offset
);
190 tcg_gen_st_tl(tn
, cpu_env
, offset
);
193 static inline void t_gen_mov_TN_preg(TCGv tn
, int r
)
196 fprintf(stderr
, "wrong register read $p%d\n", r
);
197 if (r
== PR_BZ
|| r
== PR_WZ
|| r
== PR_DZ
)
198 tcg_gen_mov_tl(tn
, tcg_const_tl(0));
200 tcg_gen_mov_tl(tn
, tcg_const_tl(32));
201 else if (r
== PR_EDA
) {
202 printf("read from EDA!\n");
203 tcg_gen_mov_tl(tn
, cpu_PR
[r
]);
206 tcg_gen_mov_tl(tn
, cpu_PR
[r
]);
208 static inline void t_gen_mov_preg_TN(DisasContext
*dc
, int r
, TCGv tn
)
211 fprintf(stderr
, "wrong register write $p%d\n", r
);
212 if (r
== PR_BZ
|| r
== PR_WZ
|| r
== PR_DZ
)
214 else if (r
== PR_SRS
)
215 tcg_gen_andi_tl(cpu_PR
[r
], tn
, 3);
218 gen_helper_tlb_flush_pid(tn
);
219 if (dc
->tb_flags
& S_FLAG
&& r
== PR_SPC
)
220 gen_helper_spc_write(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 TCGv_i32 tmp
= tcg_const_i32(index
);
230 gen_helper_raise_exception(tmp
);
231 tcg_temp_free_i32(tmp
);
234 static void t_gen_lsl(TCGv d
, TCGv a
, TCGv b
)
239 t_31
= tcg_const_tl(31);
240 tcg_gen_shl_tl(d
, a
, b
);
242 tcg_gen_sub_tl(t0
, t_31
, b
);
243 tcg_gen_sar_tl(t0
, t0
, t_31
);
244 tcg_gen_and_tl(t0
, t0
, d
);
245 tcg_gen_xor_tl(d
, d
, t0
);
250 static void t_gen_lsr(TCGv d
, TCGv a
, TCGv b
)
255 t_31
= tcg_temp_new();
256 tcg_gen_shr_tl(d
, a
, b
);
258 tcg_gen_movi_tl(t_31
, 31);
259 tcg_gen_sub_tl(t0
, t_31
, b
);
260 tcg_gen_sar_tl(t0
, t0
, t_31
);
261 tcg_gen_and_tl(t0
, t0
, d
);
262 tcg_gen_xor_tl(d
, d
, t0
);
267 static void t_gen_asr(TCGv d
, TCGv a
, TCGv b
)
272 t_31
= tcg_temp_new();
273 tcg_gen_sar_tl(d
, a
, b
);
275 tcg_gen_movi_tl(t_31
, 31);
276 tcg_gen_sub_tl(t0
, t_31
, b
);
277 tcg_gen_sar_tl(t0
, t0
, t_31
);
278 tcg_gen_or_tl(d
, d
, t0
);
283 /* 64-bit signed mul, lower result in d and upper in d2. */
284 static void t_gen_muls(TCGv d
, TCGv d2
, TCGv a
, TCGv b
)
288 t0
= tcg_temp_new_i64();
289 t1
= tcg_temp_new_i64();
291 tcg_gen_ext_i32_i64(t0
, a
);
292 tcg_gen_ext_i32_i64(t1
, b
);
293 tcg_gen_mul_i64(t0
, t0
, t1
);
295 tcg_gen_trunc_i64_i32(d
, t0
);
296 tcg_gen_shri_i64(t0
, t0
, 32);
297 tcg_gen_trunc_i64_i32(d2
, t0
);
299 tcg_temp_free_i64(t0
);
300 tcg_temp_free_i64(t1
);
303 /* 64-bit unsigned muls, lower result in d and upper in d2. */
304 static void t_gen_mulu(TCGv d
, TCGv d2
, TCGv a
, TCGv b
)
308 t0
= tcg_temp_new_i64();
309 t1
= tcg_temp_new_i64();
311 tcg_gen_extu_i32_i64(t0
, a
);
312 tcg_gen_extu_i32_i64(t1
, b
);
313 tcg_gen_mul_i64(t0
, t0
, t1
);
315 tcg_gen_trunc_i64_i32(d
, t0
);
316 tcg_gen_shri_i64(t0
, t0
, 32);
317 tcg_gen_trunc_i64_i32(d2
, t0
);
319 tcg_temp_free_i64(t0
);
320 tcg_temp_free_i64(t1
);
323 /* 32bit branch-free binary search for counting leading zeros. */
324 static void t_gen_lz_i32(TCGv d
, TCGv x
)
328 y
= tcg_temp_new_i32();
329 m
= tcg_temp_new_i32();
330 n
= tcg_temp_new_i32();
333 tcg_gen_shri_i32(y
, x
, 16);
334 tcg_gen_neg_i32(y
, y
);
336 /* m = (y >> 16) & 16 */
337 tcg_gen_sari_i32(m
, y
, 16);
338 tcg_gen_andi_i32(m
, m
, 16);
341 tcg_gen_sub_i32(n
, tcg_const_i32(16), m
);
343 tcg_gen_shr_i32(x
, x
, m
);
346 tcg_gen_subi_i32(y
, x
, 0x100);
347 /* m = (y >> 16) & 8 */
348 tcg_gen_sari_i32(m
, y
, 16);
349 tcg_gen_andi_i32(m
, m
, 8);
351 tcg_gen_add_i32(n
, n
, m
);
353 tcg_gen_shl_i32(x
, x
, m
);
356 tcg_gen_subi_i32(y
, x
, 0x1000);
357 /* m = (y >> 16) & 4 */
358 tcg_gen_sari_i32(m
, y
, 16);
359 tcg_gen_andi_i32(m
, m
, 4);
361 tcg_gen_add_i32(n
, n
, m
);
363 tcg_gen_shl_i32(x
, x
, m
);
366 tcg_gen_subi_i32(y
, x
, 0x4000);
367 /* m = (y >> 16) & 2 */
368 tcg_gen_sari_i32(m
, y
, 16);
369 tcg_gen_andi_i32(m
, m
, 2);
371 tcg_gen_add_i32(n
, n
, m
);
373 tcg_gen_shl_i32(x
, x
, m
);
376 tcg_gen_shri_i32(y
, x
, 14);
377 /* m = y & ~(y >> 1) */
378 tcg_gen_sari_i32(m
, y
, 1);
379 tcg_gen_not_i32(m
, m
);
380 tcg_gen_and_i32(m
, m
, y
);
383 tcg_gen_addi_i32(d
, n
, 2);
384 tcg_gen_sub_i32(d
, d
, m
);
391 static void t_gen_btst(TCGv d
, TCGv a
, TCGv b
)
399 The N flag is set according to the selected bit in the dest reg.
400 The Z flag is set if the selected bit and all bits to the right are
402 The X flag is cleared.
403 Other flags are left untouched.
404 The destination reg is not affected.
406 unsigned int fz, sbit, bset, mask, masked_t0;
409 bset = !!(T0 & (1 << sbit));
410 mask = sbit == 31 ? -1 : (1 << (sbit + 1)) - 1;
411 masked_t0 = T0 & mask;
412 fz = !(masked_t0 | bset);
414 // Clear the X, N and Z flags.
415 T0 = env->pregs[PR_CCS] & ~(X_FLAG | N_FLAG | Z_FLAG);
416 // Set the N and Z flags accordingly.
417 T0 |= (bset << 3) | (fz << 2);
420 l1
= gen_new_label();
421 sbit
= tcg_temp_new();
422 bset
= tcg_temp_new();
425 /* Compute bset and sbit. */
426 tcg_gen_andi_tl(sbit
, b
, 31);
427 tcg_gen_shl_tl(t0
, tcg_const_tl(1), sbit
);
428 tcg_gen_and_tl(bset
, a
, t0
);
429 tcg_gen_shr_tl(bset
, bset
, sbit
);
430 /* Displace to N_FLAG. */
431 tcg_gen_shli_tl(bset
, bset
, 3);
433 tcg_gen_shl_tl(sbit
, tcg_const_tl(2), sbit
);
434 tcg_gen_subi_tl(sbit
, sbit
, 1);
435 tcg_gen_and_tl(sbit
, a
, sbit
);
437 tcg_gen_andi_tl(d
, cpu_PR
[PR_CCS
], ~(X_FLAG
| N_FLAG
| Z_FLAG
));
438 /* or in the N_FLAG. */
439 tcg_gen_or_tl(d
, d
, bset
);
440 tcg_gen_brcondi_tl(TCG_COND_NE
, sbit
, 0, l1
);
441 /* or in the Z_FLAG. */
442 tcg_gen_ori_tl(d
, d
, Z_FLAG
);
449 static void t_gen_cris_dstep(TCGv d
, TCGv a
, TCGv b
)
453 l1
= gen_new_label();
460 tcg_gen_shli_tl(d
, a
, 1);
461 tcg_gen_brcond_tl(TCG_COND_LTU
, d
, b
, l1
);
462 tcg_gen_sub_tl(d
, d
, b
);
466 /* Extended arithmetics on CRIS. */
467 static inline void t_gen_add_flag(TCGv d
, int flag
)
472 t_gen_mov_TN_preg(c
, PR_CCS
);
473 /* Propagate carry into d. */
474 tcg_gen_andi_tl(c
, c
, 1 << flag
);
476 tcg_gen_shri_tl(c
, c
, flag
);
477 tcg_gen_add_tl(d
, d
, c
);
481 static inline void t_gen_addx_carry(DisasContext
*dc
, TCGv d
)
483 if (dc
->flagx_known
) {
488 t_gen_mov_TN_preg(c
, PR_CCS
);
489 /* C flag is already at bit 0. */
490 tcg_gen_andi_tl(c
, c
, C_FLAG
);
491 tcg_gen_add_tl(d
, d
, c
);
499 t_gen_mov_TN_preg(x
, PR_CCS
);
500 tcg_gen_mov_tl(c
, x
);
502 /* Propagate carry into d if X is set. Branch free. */
503 tcg_gen_andi_tl(c
, c
, C_FLAG
);
504 tcg_gen_andi_tl(x
, x
, X_FLAG
);
505 tcg_gen_shri_tl(x
, x
, 4);
507 tcg_gen_and_tl(x
, x
, c
);
508 tcg_gen_add_tl(d
, d
, x
);
514 static inline void t_gen_subx_carry(DisasContext
*dc
, TCGv d
)
516 if (dc
->flagx_known
) {
521 t_gen_mov_TN_preg(c
, PR_CCS
);
522 /* C flag is already at bit 0. */
523 tcg_gen_andi_tl(c
, c
, C_FLAG
);
524 tcg_gen_sub_tl(d
, d
, c
);
532 t_gen_mov_TN_preg(x
, PR_CCS
);
533 tcg_gen_mov_tl(c
, x
);
535 /* Propagate carry into d if X is set. Branch free. */
536 tcg_gen_andi_tl(c
, c
, C_FLAG
);
537 tcg_gen_andi_tl(x
, x
, X_FLAG
);
538 tcg_gen_shri_tl(x
, x
, 4);
540 tcg_gen_and_tl(x
, x
, c
);
541 tcg_gen_sub_tl(d
, d
, x
);
547 /* Swap the two bytes within each half word of the s operand.
548 T0 = ((T0 << 8) & 0xff00ff00) | ((T0 >> 8) & 0x00ff00ff) */
549 static inline void t_gen_swapb(TCGv d
, TCGv s
)
554 org_s
= tcg_temp_new();
556 /* d and s may refer to the same object. */
557 tcg_gen_mov_tl(org_s
, s
);
558 tcg_gen_shli_tl(t
, org_s
, 8);
559 tcg_gen_andi_tl(d
, t
, 0xff00ff00);
560 tcg_gen_shri_tl(t
, org_s
, 8);
561 tcg_gen_andi_tl(t
, t
, 0x00ff00ff);
562 tcg_gen_or_tl(d
, d
, t
);
564 tcg_temp_free(org_s
);
567 /* Swap the halfwords of the s operand. */
568 static inline void t_gen_swapw(TCGv d
, TCGv s
)
571 /* d and s refer the same object. */
573 tcg_gen_mov_tl(t
, s
);
574 tcg_gen_shli_tl(d
, t
, 16);
575 tcg_gen_shri_tl(t
, t
, 16);
576 tcg_gen_or_tl(d
, d
, t
);
580 /* Reverse the within each byte.
581 T0 = (((T0 << 7) & 0x80808080) |
582 ((T0 << 5) & 0x40404040) |
583 ((T0 << 3) & 0x20202020) |
584 ((T0 << 1) & 0x10101010) |
585 ((T0 >> 1) & 0x08080808) |
586 ((T0 >> 3) & 0x04040404) |
587 ((T0 >> 5) & 0x02020202) |
588 ((T0 >> 7) & 0x01010101));
590 static inline void t_gen_swapr(TCGv d
, TCGv s
)
593 int shift
; /* LSL when positive, LSR when negative. */
608 /* d and s refer the same object. */
610 org_s
= tcg_temp_new();
611 tcg_gen_mov_tl(org_s
, s
);
613 tcg_gen_shli_tl(t
, org_s
, bitrev
[0].shift
);
614 tcg_gen_andi_tl(d
, t
, bitrev
[0].mask
);
615 for (i
= 1; i
< sizeof bitrev
/ sizeof bitrev
[0]; i
++) {
616 if (bitrev
[i
].shift
>= 0) {
617 tcg_gen_shli_tl(t
, org_s
, bitrev
[i
].shift
);
619 tcg_gen_shri_tl(t
, org_s
, -bitrev
[i
].shift
);
621 tcg_gen_andi_tl(t
, t
, bitrev
[i
].mask
);
622 tcg_gen_or_tl(d
, d
, t
);
625 tcg_temp_free(org_s
);
628 static void t_gen_cc_jmp(TCGv pc_true
, TCGv pc_false
)
633 l1
= gen_new_label();
634 btaken
= tcg_temp_new();
636 /* Conditional jmp. */
637 tcg_gen_mov_tl(btaken
, env_btaken
);
638 tcg_gen_mov_tl(env_pc
, pc_false
);
639 tcg_gen_brcondi_tl(TCG_COND_EQ
, btaken
, 0, l1
);
640 tcg_gen_mov_tl(env_pc
, pc_true
);
643 tcg_temp_free(btaken
);
646 static void gen_goto_tb(DisasContext
*dc
, int n
, target_ulong dest
)
648 TranslationBlock
*tb
;
650 if ((tb
->pc
& TARGET_PAGE_MASK
) == (dest
& TARGET_PAGE_MASK
)) {
652 tcg_gen_movi_tl(env_pc
, dest
);
653 tcg_gen_exit_tb((long)tb
+ n
);
655 tcg_gen_movi_tl(env_pc
, dest
);
660 /* Sign extend at translation time. */
661 static int sign_extend(unsigned int val
, unsigned int width
)
673 static inline void cris_clear_x_flag(DisasContext
*dc
)
675 if (dc
->flagx_known
&& dc
->flags_x
)
676 dc
->flags_uptodate
= 0;
682 static void cris_flush_cc_state(DisasContext
*dc
)
684 if (dc
->cc_size_uptodate
!= dc
->cc_size
) {
685 tcg_gen_movi_tl(cc_size
, dc
->cc_size
);
686 dc
->cc_size_uptodate
= dc
->cc_size
;
688 tcg_gen_movi_tl(cc_op
, dc
->cc_op
);
689 tcg_gen_movi_tl(cc_mask
, dc
->cc_mask
);
692 static void cris_evaluate_flags(DisasContext
*dc
)
694 if (!dc
->flags_uptodate
) {
695 cris_flush_cc_state(dc
);
700 gen_helper_evaluate_flags_mcp();
703 gen_helper_evaluate_flags_muls();
706 gen_helper_evaluate_flags_mulu();
718 gen_helper_evaluate_flags_move_4();
721 gen_helper_evaluate_flags_move_2();
724 gen_helper_evaluate_flags();
736 gen_helper_evaluate_flags_alu_4();
739 gen_helper_evaluate_flags();
745 if (dc
->flagx_known
) {
747 tcg_gen_ori_tl(cpu_PR
[PR_CCS
],
748 cpu_PR
[PR_CCS
], X_FLAG
);
750 tcg_gen_andi_tl(cpu_PR
[PR_CCS
],
751 cpu_PR
[PR_CCS
], ~X_FLAG
);
754 dc
->flags_uptodate
= 1;
758 static void cris_cc_mask(DisasContext
*dc
, unsigned int mask
)
767 /* Check if we need to evaluate the condition codes due to
769 ovl
= (dc
->cc_mask
^ mask
) & ~mask
;
771 /* TODO: optimize this case. It trigs all the time. */
772 cris_evaluate_flags (dc
);
778 static void cris_update_cc_op(DisasContext
*dc
, int op
, int size
)
782 dc
->flags_uptodate
= 0;
785 static inline void cris_update_cc_x(DisasContext
*dc
)
787 /* Save the x flag state at the time of the cc snapshot. */
788 if (dc
->flagx_known
) {
789 if (dc
->cc_x_uptodate
== (2 | dc
->flags_x
))
791 tcg_gen_movi_tl(cc_x
, dc
->flags_x
);
792 dc
->cc_x_uptodate
= 2 | dc
->flags_x
;
795 tcg_gen_andi_tl(cc_x
, cpu_PR
[PR_CCS
], X_FLAG
);
796 dc
->cc_x_uptodate
= 1;
800 /* Update cc prior to executing ALU op. Needs source operands untouched. */
801 static void cris_pre_alu_update_cc(DisasContext
*dc
, int op
,
802 TCGv dst
, TCGv src
, int size
)
805 cris_update_cc_op(dc
, op
, size
);
806 tcg_gen_mov_tl(cc_src
, src
);
815 tcg_gen_mov_tl(cc_dest
, dst
);
817 cris_update_cc_x(dc
);
821 /* Update cc after executing ALU op. needs the result. */
822 static inline void cris_update_result(DisasContext
*dc
, TCGv res
)
825 if (dc
->cc_size
== 4 &&
826 (dc
->cc_op
== CC_OP_SUB
827 || dc
->cc_op
== CC_OP_ADD
))
829 tcg_gen_mov_tl(cc_result
, res
);
833 /* Returns one if the write back stage should execute. */
834 static void cris_alu_op_exec(DisasContext
*dc
, int op
,
835 TCGv dst
, TCGv a
, TCGv b
, int size
)
837 /* Emit the ALU insns. */
841 tcg_gen_add_tl(dst
, a
, b
);
842 /* Extended arithmetics. */
843 t_gen_addx_carry(dc
, dst
);
846 tcg_gen_add_tl(dst
, a
, b
);
847 t_gen_add_flag(dst
, 0); /* C_FLAG. */
850 tcg_gen_add_tl(dst
, a
, b
);
851 t_gen_add_flag(dst
, 8); /* R_FLAG. */
854 tcg_gen_sub_tl(dst
, a
, b
);
855 /* Extended arithmetics. */
856 t_gen_subx_carry(dc
, dst
);
859 tcg_gen_mov_tl(dst
, b
);
862 tcg_gen_or_tl(dst
, a
, b
);
865 tcg_gen_and_tl(dst
, a
, b
);
868 tcg_gen_xor_tl(dst
, a
, b
);
871 t_gen_lsl(dst
, a
, b
);
874 t_gen_lsr(dst
, a
, b
);
877 t_gen_asr(dst
, a
, b
);
880 tcg_gen_neg_tl(dst
, b
);
881 /* Extended arithmetics. */
882 t_gen_subx_carry(dc
, dst
);
885 t_gen_lz_i32(dst
, b
);
888 t_gen_btst(dst
, a
, b
);
891 t_gen_muls(dst
, cpu_PR
[PR_MOF
], a
, b
);
894 t_gen_mulu(dst
, cpu_PR
[PR_MOF
], a
, b
);
897 t_gen_cris_dstep(dst
, a
, b
);
902 l1
= gen_new_label();
903 tcg_gen_mov_tl(dst
, a
);
904 tcg_gen_brcond_tl(TCG_COND_LEU
, a
, b
, l1
);
905 tcg_gen_mov_tl(dst
, b
);
910 tcg_gen_sub_tl(dst
, a
, b
);
911 /* Extended arithmetics. */
912 t_gen_subx_carry(dc
, dst
);
915 fprintf (logfile
, "illegal ALU op.\n");
921 tcg_gen_andi_tl(dst
, dst
, 0xff);
923 tcg_gen_andi_tl(dst
, dst
, 0xffff);
926 static void cris_alu(DisasContext
*dc
, int op
,
927 TCGv d
, TCGv op_a
, TCGv op_b
, int size
)
934 if (op
== CC_OP_BOUND
|| op
== CC_OP_BTST
)
935 tmp
= tcg_temp_local_new();
937 if (op
== CC_OP_CMP
) {
938 tmp
= tcg_temp_new();
940 } else if (size
== 4) {
944 tmp
= tcg_temp_new();
947 cris_pre_alu_update_cc(dc
, op
, op_a
, op_b
, size
);
948 cris_alu_op_exec(dc
, op
, tmp
, op_a
, op_b
, size
);
949 cris_update_result(dc
, tmp
);
954 tcg_gen_andi_tl(d
, d
, ~0xff);
956 tcg_gen_andi_tl(d
, d
, ~0xffff);
957 tcg_gen_or_tl(d
, d
, tmp
);
959 if (!TCGV_EQUAL(tmp
, d
))
963 static int arith_cc(DisasContext
*dc
)
967 case CC_OP_ADDC
: return 1;
968 case CC_OP_ADD
: return 1;
969 case CC_OP_SUB
: return 1;
970 case CC_OP_DSTEP
: return 1;
971 case CC_OP_LSL
: return 1;
972 case CC_OP_LSR
: return 1;
973 case CC_OP_ASR
: return 1;
974 case CC_OP_CMP
: return 1;
975 case CC_OP_NEG
: return 1;
976 case CC_OP_OR
: return 1;
977 case CC_OP_XOR
: return 1;
978 case CC_OP_MULU
: return 1;
979 case CC_OP_MULS
: return 1;
987 static void gen_tst_cc (DisasContext
*dc
, TCGv cc
, int cond
)
989 int arith_opt
, move_opt
;
991 /* TODO: optimize more condition codes. */
994 * If the flags are live, we've gotta look into the bits of CCS.
995 * Otherwise, if we just did an arithmetic operation we try to
996 * evaluate the condition code faster.
998 * When this function is done, T0 should be non-zero if the condition
1001 arith_opt
= arith_cc(dc
) && !dc
->flags_uptodate
;
1002 move_opt
= (dc
->cc_op
== CC_OP_MOVE
) && dc
->flags_uptodate
;
1005 if (arith_opt
|| move_opt
) {
1006 /* If cc_result is zero, T0 should be
1007 non-zero otherwise T0 should be zero. */
1009 l1
= gen_new_label();
1010 tcg_gen_movi_tl(cc
, 0);
1011 tcg_gen_brcondi_tl(TCG_COND_NE
, cc_result
,
1013 tcg_gen_movi_tl(cc
, 1);
1017 cris_evaluate_flags(dc
);
1019 cpu_PR
[PR_CCS
], Z_FLAG
);
1023 if (arith_opt
|| move_opt
)
1024 tcg_gen_mov_tl(cc
, cc_result
);
1026 cris_evaluate_flags(dc
);
1027 tcg_gen_xori_tl(cc
, cpu_PR
[PR_CCS
],
1029 tcg_gen_andi_tl(cc
, cc
, Z_FLAG
);
1033 cris_evaluate_flags(dc
);
1034 tcg_gen_andi_tl(cc
, cpu_PR
[PR_CCS
], C_FLAG
);
1037 cris_evaluate_flags(dc
);
1038 tcg_gen_xori_tl(cc
, cpu_PR
[PR_CCS
], C_FLAG
);
1039 tcg_gen_andi_tl(cc
, cc
, C_FLAG
);
1042 cris_evaluate_flags(dc
);
1043 tcg_gen_andi_tl(cc
, cpu_PR
[PR_CCS
], V_FLAG
);
1046 cris_evaluate_flags(dc
);
1047 tcg_gen_xori_tl(cc
, cpu_PR
[PR_CCS
],
1049 tcg_gen_andi_tl(cc
, cc
, V_FLAG
);
1052 if (arith_opt
|| move_opt
) {
1055 if (dc
->cc_size
== 1)
1057 else if (dc
->cc_size
== 2)
1060 tcg_gen_shri_tl(cc
, cc_result
, bits
);
1061 tcg_gen_xori_tl(cc
, cc
, 1);
1063 cris_evaluate_flags(dc
);
1064 tcg_gen_xori_tl(cc
, cpu_PR
[PR_CCS
],
1066 tcg_gen_andi_tl(cc
, cc
, N_FLAG
);
1070 if (arith_opt
|| move_opt
) {
1073 if (dc
->cc_size
== 1)
1075 else if (dc
->cc_size
== 2)
1078 tcg_gen_shri_tl(cc
, cc_result
, 31);
1081 cris_evaluate_flags(dc
);
1082 tcg_gen_andi_tl(cc
, cpu_PR
[PR_CCS
],
1087 cris_evaluate_flags(dc
);
1088 tcg_gen_andi_tl(cc
, cpu_PR
[PR_CCS
],
1092 cris_evaluate_flags(dc
);
1096 tmp
= tcg_temp_new();
1097 tcg_gen_xori_tl(tmp
, cpu_PR
[PR_CCS
],
1099 /* Overlay the C flag on top of the Z. */
1100 tcg_gen_shli_tl(cc
, tmp
, 2);
1101 tcg_gen_and_tl(cc
, tmp
, cc
);
1102 tcg_gen_andi_tl(cc
, cc
, Z_FLAG
);
1108 cris_evaluate_flags(dc
);
1109 /* Overlay the V flag on top of the N. */
1110 tcg_gen_shli_tl(cc
, cpu_PR
[PR_CCS
], 2);
1112 cpu_PR
[PR_CCS
], cc
);
1113 tcg_gen_andi_tl(cc
, cc
, N_FLAG
);
1114 tcg_gen_xori_tl(cc
, cc
, N_FLAG
);
1117 cris_evaluate_flags(dc
);
1118 /* Overlay the V flag on top of the N. */
1119 tcg_gen_shli_tl(cc
, cpu_PR
[PR_CCS
], 2);
1121 cpu_PR
[PR_CCS
], cc
);
1122 tcg_gen_andi_tl(cc
, cc
, N_FLAG
);
1125 cris_evaluate_flags(dc
);
1132 /* To avoid a shift we overlay everything on
1134 tcg_gen_shri_tl(n
, cpu_PR
[PR_CCS
], 2);
1135 tcg_gen_shri_tl(z
, cpu_PR
[PR_CCS
], 1);
1137 tcg_gen_xori_tl(z
, z
, 2);
1139 tcg_gen_xor_tl(n
, n
, cpu_PR
[PR_CCS
]);
1140 tcg_gen_xori_tl(n
, n
, 2);
1141 tcg_gen_and_tl(cc
, z
, n
);
1142 tcg_gen_andi_tl(cc
, cc
, 2);
1149 cris_evaluate_flags(dc
);
1156 /* To avoid a shift we overlay everything on
1158 tcg_gen_shri_tl(n
, cpu_PR
[PR_CCS
], 2);
1159 tcg_gen_shri_tl(z
, cpu_PR
[PR_CCS
], 1);
1161 tcg_gen_xor_tl(n
, n
, cpu_PR
[PR_CCS
]);
1162 tcg_gen_or_tl(cc
, z
, n
);
1163 tcg_gen_andi_tl(cc
, cc
, 2);
1170 cris_evaluate_flags(dc
);
1171 tcg_gen_andi_tl(cc
, cpu_PR
[PR_CCS
], P_FLAG
);
1174 tcg_gen_movi_tl(cc
, 1);
1182 static void cris_store_direct_jmp(DisasContext
*dc
)
1184 /* Store the direct jmp state into the cpu-state. */
1185 if (dc
->jmp
== JMP_DIRECT
) {
1186 tcg_gen_movi_tl(env_btarget
, dc
->jmp_pc
);
1187 tcg_gen_movi_tl(env_btaken
, 1);
1191 static void cris_prepare_cc_branch (DisasContext
*dc
,
1192 int offset
, int cond
)
1194 /* This helps us re-schedule the micro-code to insns in delay-slots
1195 before the actual jump. */
1196 dc
->delayed_branch
= 2;
1197 dc
->jmp_pc
= dc
->pc
+ offset
;
1201 dc
->jmp
= JMP_INDIRECT
;
1202 gen_tst_cc (dc
, env_btaken
, cond
);
1203 tcg_gen_movi_tl(env_btarget
, dc
->jmp_pc
);
1205 /* Allow chaining. */
1206 dc
->jmp
= JMP_DIRECT
;
1211 /* jumps, when the dest is in a live reg for example. Direct should be set
1212 when the dest addr is constant to allow tb chaining. */
1213 static inline void cris_prepare_jmp (DisasContext
*dc
, unsigned int type
)
1215 /* This helps us re-schedule the micro-code to insns in delay-slots
1216 before the actual jump. */
1217 dc
->delayed_branch
= 2;
1219 if (type
== JMP_INDIRECT
)
1220 tcg_gen_movi_tl(env_btaken
, 1);
1223 static void gen_load64(DisasContext
*dc
, TCGv_i64 dst
, TCGv addr
)
1225 int mem_index
= cpu_mmu_index(dc
->env
);
1227 /* If we get a fault on a delayslot we must keep the jmp state in
1228 the cpu-state to be able to re-execute the jmp. */
1229 if (dc
->delayed_branch
== 1)
1230 cris_store_direct_jmp(dc
);
1232 tcg_gen_qemu_ld64(dst
, addr
, mem_index
);
1235 static void gen_load(DisasContext
*dc
, TCGv dst
, TCGv addr
,
1236 unsigned int size
, int sign
)
1238 int mem_index
= cpu_mmu_index(dc
->env
);
1240 /* If we get a fault on a delayslot we must keep the jmp state in
1241 the cpu-state to be able to re-execute the jmp. */
1242 if (dc
->delayed_branch
== 1)
1243 cris_store_direct_jmp(dc
);
1247 tcg_gen_qemu_ld8s(dst
, addr
, mem_index
);
1249 tcg_gen_qemu_ld8u(dst
, addr
, mem_index
);
1251 else if (size
== 2) {
1253 tcg_gen_qemu_ld16s(dst
, addr
, mem_index
);
1255 tcg_gen_qemu_ld16u(dst
, addr
, mem_index
);
1257 else if (size
== 4) {
1258 tcg_gen_qemu_ld32u(dst
, addr
, mem_index
);
1265 static void gen_store (DisasContext
*dc
, TCGv addr
, TCGv val
,
1268 int mem_index
= cpu_mmu_index(dc
->env
);
1270 /* If we get a fault on a delayslot we must keep the jmp state in
1271 the cpu-state to be able to re-execute the jmp. */
1272 if (dc
->delayed_branch
== 1)
1273 cris_store_direct_jmp(dc
);
1276 /* Conditional writes. We only support the kind were X and P are known
1277 at translation time. */
1278 if (dc
->flagx_known
&& dc
->flags_x
&& (dc
->tb_flags
& P_FLAG
)) {
1280 cris_evaluate_flags(dc
);
1281 tcg_gen_ori_tl(cpu_PR
[PR_CCS
], cpu_PR
[PR_CCS
], C_FLAG
);
1286 tcg_gen_qemu_st8(val
, addr
, mem_index
);
1288 tcg_gen_qemu_st16(val
, addr
, mem_index
);
1290 tcg_gen_qemu_st32(val
, addr
, mem_index
);
1292 if (dc
->flagx_known
&& dc
->flags_x
) {
1293 cris_evaluate_flags(dc
);
1294 tcg_gen_andi_tl(cpu_PR
[PR_CCS
], cpu_PR
[PR_CCS
], ~C_FLAG
);
1298 static inline void t_gen_sext(TCGv d
, TCGv s
, int size
)
1301 tcg_gen_ext8s_i32(d
, s
);
1303 tcg_gen_ext16s_i32(d
, s
);
1304 else if(!TCGV_EQUAL(d
, s
))
1305 tcg_gen_mov_tl(d
, s
);
1308 static inline void t_gen_zext(TCGv d
, TCGv s
, int size
)
1311 tcg_gen_ext8u_i32(d
, s
);
1313 tcg_gen_ext16u_i32(d
, s
);
1314 else if (!TCGV_EQUAL(d
, s
))
1315 tcg_gen_mov_tl(d
, s
);
1319 static char memsize_char(int size
)
1323 case 1: return 'b'; break;
1324 case 2: return 'w'; break;
1325 case 4: return 'd'; break;
1333 static inline unsigned int memsize_z(DisasContext
*dc
)
1335 return dc
->zsize
+ 1;
1338 static inline unsigned int memsize_zz(DisasContext
*dc
)
1349 static inline void do_postinc (DisasContext
*dc
, int size
)
1352 tcg_gen_addi_tl(cpu_R
[dc
->op1
], cpu_R
[dc
->op1
], size
);
1355 static inline void dec_prep_move_r(DisasContext
*dc
, int rs
, int rd
,
1356 int size
, int s_ext
, TCGv dst
)
1359 t_gen_sext(dst
, cpu_R
[rs
], size
);
1361 t_gen_zext(dst
, cpu_R
[rs
], size
);
1364 /* Prepare T0 and T1 for a register alu operation.
1365 s_ext decides if the operand1 should be sign-extended or zero-extended when
1367 static void dec_prep_alu_r(DisasContext
*dc
, int rs
, int rd
,
1368 int size
, int s_ext
, TCGv dst
, TCGv src
)
1370 dec_prep_move_r(dc
, rs
, rd
, size
, s_ext
, src
);
1373 t_gen_sext(dst
, cpu_R
[rd
], size
);
1375 t_gen_zext(dst
, cpu_R
[rd
], size
);
1378 static int dec_prep_move_m(DisasContext
*dc
, int s_ext
, int memsize
,
1381 unsigned int rs
, rd
;
1388 is_imm
= rs
== 15 && dc
->postinc
;
1390 /* Load [$rs] onto T1. */
1392 insn_len
= 2 + memsize
;
1399 imm
= ldsb_code(dc
->pc
+ 2);
1401 imm
= ldsw_code(dc
->pc
+ 2);
1404 imm
= ldub_code(dc
->pc
+ 2);
1406 imm
= lduw_code(dc
->pc
+ 2);
1409 imm
= ldl_code(dc
->pc
+ 2);
1411 tcg_gen_movi_tl(dst
, imm
);
1414 cris_flush_cc_state(dc
);
1415 gen_load(dc
, dst
, cpu_R
[rs
], memsize
, 0);
1417 t_gen_sext(dst
, dst
, memsize
);
1419 t_gen_zext(dst
, dst
, memsize
);
1424 /* Prepare T0 and T1 for a memory + alu operation.
1425 s_ext decides if the operand1 should be sign-extended or zero-extended when
1427 static int dec_prep_alu_m(DisasContext
*dc
, int s_ext
, int memsize
,
1432 insn_len
= dec_prep_move_m(dc
, s_ext
, memsize
, src
);
1433 tcg_gen_mov_tl(dst
, cpu_R
[dc
->op2
]);
1438 static const char *cc_name(int cc
)
1440 static const char *cc_names
[16] = {
1441 "cc", "cs", "ne", "eq", "vc", "vs", "pl", "mi",
1442 "ls", "hi", "ge", "lt", "gt", "le", "a", "p"
1445 return cc_names
[cc
];
1449 /* Start of insn decoders. */
1451 static unsigned int dec_bccq(DisasContext
*dc
)
1455 uint32_t cond
= dc
->op2
;
1458 offset
= EXTRACT_FIELD (dc
->ir
, 1, 7);
1459 sign
= EXTRACT_FIELD(dc
->ir
, 0, 0);
1462 offset
|= sign
<< 8;
1464 offset
= sign_extend(offset
, 8);
1466 DIS(fprintf (logfile
, "b%s %x\n", cc_name(cond
), dc
->pc
+ offset
));
1468 /* op2 holds the condition-code. */
1469 cris_cc_mask(dc
, 0);
1470 cris_prepare_cc_branch (dc
, offset
, cond
);
1473 static unsigned int dec_addoq(DisasContext
*dc
)
1477 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 7);
1478 imm
= sign_extend(dc
->op1
, 7);
1480 DIS(fprintf (logfile
, "addoq %d, $r%u\n", imm
, dc
->op2
));
1481 cris_cc_mask(dc
, 0);
1482 /* Fetch register operand, */
1483 tcg_gen_addi_tl(cpu_R
[R_ACR
], cpu_R
[dc
->op2
], imm
);
1487 static unsigned int dec_addq(DisasContext
*dc
)
1489 DIS(fprintf (logfile
, "addq %u, $r%u\n", dc
->op1
, dc
->op2
));
1491 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 5);
1493 cris_cc_mask(dc
, CC_MASK_NZVC
);
1495 cris_alu(dc
, CC_OP_ADD
,
1496 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], tcg_const_tl(dc
->op1
), 4);
1499 static unsigned int dec_moveq(DisasContext
*dc
)
1503 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 5);
1504 imm
= sign_extend(dc
->op1
, 5);
1505 DIS(fprintf (logfile
, "moveq %d, $r%u\n", imm
, dc
->op2
));
1507 tcg_gen_mov_tl(cpu_R
[dc
->op2
], tcg_const_tl(imm
));
1510 static unsigned int dec_subq(DisasContext
*dc
)
1512 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 5);
1514 DIS(fprintf (logfile
, "subq %u, $r%u\n", dc
->op1
, dc
->op2
));
1516 cris_cc_mask(dc
, CC_MASK_NZVC
);
1517 cris_alu(dc
, CC_OP_SUB
,
1518 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], tcg_const_tl(dc
->op1
), 4);
1521 static unsigned int dec_cmpq(DisasContext
*dc
)
1524 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 5);
1525 imm
= sign_extend(dc
->op1
, 5);
1527 DIS(fprintf (logfile
, "cmpq %d, $r%d\n", imm
, dc
->op2
));
1528 cris_cc_mask(dc
, CC_MASK_NZVC
);
1530 cris_alu(dc
, CC_OP_CMP
,
1531 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], tcg_const_tl(imm
), 4);
1534 static unsigned int dec_andq(DisasContext
*dc
)
1537 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 5);
1538 imm
= sign_extend(dc
->op1
, 5);
1540 DIS(fprintf (logfile
, "andq %d, $r%d\n", imm
, dc
->op2
));
1541 cris_cc_mask(dc
, CC_MASK_NZ
);
1543 cris_alu(dc
, CC_OP_AND
,
1544 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], tcg_const_tl(imm
), 4);
1547 static unsigned int dec_orq(DisasContext
*dc
)
1550 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 5);
1551 imm
= sign_extend(dc
->op1
, 5);
1552 DIS(fprintf (logfile
, "orq %d, $r%d\n", imm
, dc
->op2
));
1553 cris_cc_mask(dc
, CC_MASK_NZ
);
1555 cris_alu(dc
, CC_OP_OR
,
1556 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], tcg_const_tl(imm
), 4);
1559 static unsigned int dec_btstq(DisasContext
*dc
)
1562 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 4);
1563 DIS(fprintf (logfile
, "btstq %u, $r%d\n", dc
->op1
, dc
->op2
));
1565 cris_cc_mask(dc
, CC_MASK_NZ
);
1566 l0
= tcg_temp_local_new();
1567 cris_alu(dc
, CC_OP_BTST
,
1568 l0
, cpu_R
[dc
->op2
], tcg_const_tl(dc
->op1
), 4);
1569 cris_update_cc_op(dc
, CC_OP_FLAGS
, 4);
1570 t_gen_mov_preg_TN(dc
, PR_CCS
, l0
);
1571 dc
->flags_uptodate
= 1;
1575 static unsigned int dec_asrq(DisasContext
*dc
)
1577 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 4);
1578 DIS(fprintf (logfile
, "asrq %u, $r%d\n", dc
->op1
, dc
->op2
));
1579 cris_cc_mask(dc
, CC_MASK_NZ
);
1581 tcg_gen_sari_tl(cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], dc
->op1
);
1582 cris_alu(dc
, CC_OP_MOVE
,
1584 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], 4);
1587 static unsigned int dec_lslq(DisasContext
*dc
)
1589 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 4);
1590 DIS(fprintf (logfile
, "lslq %u, $r%d\n", dc
->op1
, dc
->op2
));
1592 cris_cc_mask(dc
, CC_MASK_NZ
);
1594 tcg_gen_shli_tl(cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], dc
->op1
);
1596 cris_alu(dc
, CC_OP_MOVE
,
1598 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], 4);
1601 static unsigned int dec_lsrq(DisasContext
*dc
)
1603 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 4);
1604 DIS(fprintf (logfile
, "lsrq %u, $r%d\n", dc
->op1
, dc
->op2
));
1606 cris_cc_mask(dc
, CC_MASK_NZ
);
1608 tcg_gen_shri_tl(cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], dc
->op1
);
1609 cris_alu(dc
, CC_OP_MOVE
,
1611 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], 4);
1615 static unsigned int dec_move_r(DisasContext
*dc
)
1617 int size
= memsize_zz(dc
);
1619 DIS(fprintf (logfile
, "move.%c $r%u, $r%u\n",
1620 memsize_char(size
), dc
->op1
, dc
->op2
));
1622 cris_cc_mask(dc
, CC_MASK_NZ
);
1624 dec_prep_move_r(dc
, dc
->op1
, dc
->op2
, size
, 0, cpu_R
[dc
->op2
]);
1625 cris_cc_mask(dc
, CC_MASK_NZ
);
1626 cris_update_cc_op(dc
, CC_OP_MOVE
, 4);
1627 cris_update_cc_x(dc
);
1628 cris_update_result(dc
, cpu_R
[dc
->op2
]);
1633 t0
= tcg_temp_new();
1634 dec_prep_move_r(dc
, dc
->op1
, dc
->op2
, size
, 0, t0
);
1635 cris_alu(dc
, CC_OP_MOVE
,
1637 cpu_R
[dc
->op2
], t0
, size
);
1643 static unsigned int dec_scc_r(DisasContext
*dc
)
1647 DIS(fprintf (logfile
, "s%s $r%u\n",
1648 cc_name(cond
), dc
->op1
));
1654 gen_tst_cc (dc
, cpu_R
[dc
->op1
], cond
);
1655 l1
= gen_new_label();
1656 tcg_gen_brcondi_tl(TCG_COND_EQ
, cpu_R
[dc
->op1
], 0, l1
);
1657 tcg_gen_movi_tl(cpu_R
[dc
->op1
], 1);
1661 tcg_gen_movi_tl(cpu_R
[dc
->op1
], 1);
1663 cris_cc_mask(dc
, 0);
1667 static inline void cris_alu_alloc_temps(DisasContext
*dc
, int size
, TCGv
*t
)
1670 t
[0] = cpu_R
[dc
->op2
];
1671 t
[1] = cpu_R
[dc
->op1
];
1673 t
[0] = tcg_temp_new();
1674 t
[1] = tcg_temp_new();
1678 static inline void cris_alu_free_temps(DisasContext
*dc
, int size
, TCGv
*t
)
1681 tcg_temp_free(t
[0]);
1682 tcg_temp_free(t
[1]);
1686 static unsigned int dec_and_r(DisasContext
*dc
)
1689 int size
= memsize_zz(dc
);
1691 DIS(fprintf (logfile
, "and.%c $r%u, $r%u\n",
1692 memsize_char(size
), dc
->op1
, dc
->op2
));
1694 cris_cc_mask(dc
, CC_MASK_NZ
);
1696 cris_alu_alloc_temps(dc
, size
, t
);
1697 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 0, t
[0], t
[1]);
1698 cris_alu(dc
, CC_OP_AND
, cpu_R
[dc
->op2
], t
[0], t
[1], size
);
1699 cris_alu_free_temps(dc
, size
, t
);
1703 static unsigned int dec_lz_r(DisasContext
*dc
)
1706 DIS(fprintf (logfile
, "lz $r%u, $r%u\n",
1708 cris_cc_mask(dc
, CC_MASK_NZ
);
1709 t0
= tcg_temp_new();
1710 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, 4, 0, cpu_R
[dc
->op2
], t0
);
1711 cris_alu(dc
, CC_OP_LZ
, cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], t0
, 4);
1716 static unsigned int dec_lsl_r(DisasContext
*dc
)
1719 int size
= memsize_zz(dc
);
1721 DIS(fprintf (logfile
, "lsl.%c $r%u, $r%u\n",
1722 memsize_char(size
), dc
->op1
, dc
->op2
));
1724 cris_cc_mask(dc
, CC_MASK_NZ
);
1725 cris_alu_alloc_temps(dc
, size
, t
);
1726 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 0, t
[0], t
[1]);
1727 tcg_gen_andi_tl(t
[1], t
[1], 63);
1728 cris_alu(dc
, CC_OP_LSL
, cpu_R
[dc
->op2
], t
[0], t
[1], size
);
1729 cris_alu_alloc_temps(dc
, size
, t
);
1733 static unsigned int dec_lsr_r(DisasContext
*dc
)
1736 int size
= memsize_zz(dc
);
1738 DIS(fprintf (logfile
, "lsr.%c $r%u, $r%u\n",
1739 memsize_char(size
), dc
->op1
, dc
->op2
));
1741 cris_cc_mask(dc
, CC_MASK_NZ
);
1742 cris_alu_alloc_temps(dc
, size
, t
);
1743 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 0, t
[0], t
[1]);
1744 tcg_gen_andi_tl(t
[1], t
[1], 63);
1745 cris_alu(dc
, CC_OP_LSR
, cpu_R
[dc
->op2
], t
[0], t
[1], size
);
1746 cris_alu_free_temps(dc
, size
, t
);
1750 static unsigned int dec_asr_r(DisasContext
*dc
)
1753 int size
= memsize_zz(dc
);
1755 DIS(fprintf (logfile
, "asr.%c $r%u, $r%u\n",
1756 memsize_char(size
), dc
->op1
, dc
->op2
));
1758 cris_cc_mask(dc
, CC_MASK_NZ
);
1759 cris_alu_alloc_temps(dc
, size
, t
);
1760 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 1, t
[0], t
[1]);
1761 tcg_gen_andi_tl(t
[1], t
[1], 63);
1762 cris_alu(dc
, CC_OP_ASR
, cpu_R
[dc
->op2
], t
[0], t
[1], size
);
1763 cris_alu_free_temps(dc
, size
, t
);
1767 static unsigned int dec_muls_r(DisasContext
*dc
)
1770 int size
= memsize_zz(dc
);
1772 DIS(fprintf (logfile
, "muls.%c $r%u, $r%u\n",
1773 memsize_char(size
), dc
->op1
, dc
->op2
));
1774 cris_cc_mask(dc
, CC_MASK_NZV
);
1775 cris_alu_alloc_temps(dc
, size
, t
);
1776 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 1, t
[0], t
[1]);
1778 cris_alu(dc
, CC_OP_MULS
, cpu_R
[dc
->op2
], t
[0], t
[1], 4);
1779 cris_alu_free_temps(dc
, size
, t
);
1783 static unsigned int dec_mulu_r(DisasContext
*dc
)
1786 int size
= memsize_zz(dc
);
1788 DIS(fprintf (logfile
, "mulu.%c $r%u, $r%u\n",
1789 memsize_char(size
), dc
->op1
, dc
->op2
));
1790 cris_cc_mask(dc
, CC_MASK_NZV
);
1791 cris_alu_alloc_temps(dc
, size
, t
);
1792 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 0, t
[0], t
[1]);
1794 cris_alu(dc
, CC_OP_MULU
, cpu_R
[dc
->op2
], t
[0], t
[1], 4);
1795 cris_alu_alloc_temps(dc
, size
, t
);
1800 static unsigned int dec_dstep_r(DisasContext
*dc
)
1802 DIS(fprintf (logfile
, "dstep $r%u, $r%u\n", dc
->op1
, dc
->op2
));
1803 cris_cc_mask(dc
, CC_MASK_NZ
);
1804 cris_alu(dc
, CC_OP_DSTEP
,
1805 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], cpu_R
[dc
->op1
], 4);
1809 static unsigned int dec_xor_r(DisasContext
*dc
)
1812 int size
= memsize_zz(dc
);
1813 DIS(fprintf (logfile
, "xor.%c $r%u, $r%u\n",
1814 memsize_char(size
), dc
->op1
, dc
->op2
));
1815 BUG_ON(size
!= 4); /* xor is dword. */
1816 cris_cc_mask(dc
, CC_MASK_NZ
);
1817 cris_alu_alloc_temps(dc
, size
, t
);
1818 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 0, t
[0], t
[1]);
1820 cris_alu(dc
, CC_OP_XOR
, cpu_R
[dc
->op2
], t
[0], t
[1], 4);
1821 cris_alu_free_temps(dc
, size
, t
);
1825 static unsigned int dec_bound_r(DisasContext
*dc
)
1828 int size
= memsize_zz(dc
);
1829 DIS(fprintf (logfile
, "bound.%c $r%u, $r%u\n",
1830 memsize_char(size
), dc
->op1
, dc
->op2
));
1831 cris_cc_mask(dc
, CC_MASK_NZ
);
1832 l0
= tcg_temp_local_new();
1833 dec_prep_move_r(dc
, dc
->op1
, dc
->op2
, size
, 0, l0
);
1834 cris_alu(dc
, CC_OP_BOUND
, cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], l0
, 4);
1839 static unsigned int dec_cmp_r(DisasContext
*dc
)
1842 int size
= memsize_zz(dc
);
1843 DIS(fprintf (logfile
, "cmp.%c $r%u, $r%u\n",
1844 memsize_char(size
), dc
->op1
, dc
->op2
));
1845 cris_cc_mask(dc
, CC_MASK_NZVC
);
1846 cris_alu_alloc_temps(dc
, size
, t
);
1847 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 0, t
[0], t
[1]);
1849 cris_alu(dc
, CC_OP_CMP
, cpu_R
[dc
->op2
], t
[0], t
[1], size
);
1850 cris_alu_free_temps(dc
, size
, t
);
1854 static unsigned int dec_abs_r(DisasContext
*dc
)
1858 DIS(fprintf (logfile
, "abs $r%u, $r%u\n",
1860 cris_cc_mask(dc
, CC_MASK_NZ
);
1862 t0
= tcg_temp_new();
1863 tcg_gen_sari_tl(t0
, cpu_R
[dc
->op1
], 31);
1864 tcg_gen_xor_tl(cpu_R
[dc
->op2
], cpu_R
[dc
->op1
], t0
);
1865 tcg_gen_sub_tl(cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], t0
);
1868 cris_alu(dc
, CC_OP_MOVE
,
1869 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], 4);
1873 static unsigned int dec_add_r(DisasContext
*dc
)
1876 int size
= memsize_zz(dc
);
1877 DIS(fprintf (logfile
, "add.%c $r%u, $r%u\n",
1878 memsize_char(size
), dc
->op1
, dc
->op2
));
1879 cris_cc_mask(dc
, CC_MASK_NZVC
);
1880 cris_alu_alloc_temps(dc
, size
, t
);
1881 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 0, t
[0], t
[1]);
1883 cris_alu(dc
, CC_OP_ADD
, cpu_R
[dc
->op2
], t
[0], t
[1], size
);
1884 cris_alu_free_temps(dc
, size
, t
);
1888 static unsigned int dec_addc_r(DisasContext
*dc
)
1890 DIS(fprintf (logfile
, "addc $r%u, $r%u\n",
1892 cris_evaluate_flags(dc
);
1893 cris_cc_mask(dc
, CC_MASK_NZVC
);
1894 cris_alu(dc
, CC_OP_ADDC
,
1895 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], cpu_R
[dc
->op1
], 4);
1899 static unsigned int dec_mcp_r(DisasContext
*dc
)
1901 DIS(fprintf (logfile
, "mcp $p%u, $r%u\n",
1903 cris_evaluate_flags(dc
);
1904 cris_cc_mask(dc
, CC_MASK_RNZV
);
1905 cris_alu(dc
, CC_OP_MCP
,
1906 cpu_R
[dc
->op1
], cpu_R
[dc
->op1
], cpu_PR
[dc
->op2
], 4);
1911 static char * swapmode_name(int mode
, char *modename
) {
1914 modename
[i
++] = 'n';
1916 modename
[i
++] = 'w';
1918 modename
[i
++] = 'b';
1920 modename
[i
++] = 'r';
1926 static unsigned int dec_swap_r(DisasContext
*dc
)
1932 DIS(fprintf (logfile
, "swap%s $r%u\n",
1933 swapmode_name(dc
->op2
, modename
), dc
->op1
));
1935 cris_cc_mask(dc
, CC_MASK_NZ
);
1936 t0
= tcg_temp_new();
1937 t_gen_mov_TN_reg(t0
, dc
->op1
);
1939 tcg_gen_not_tl(t0
, t0
);
1941 t_gen_swapw(t0
, t0
);
1943 t_gen_swapb(t0
, t0
);
1945 t_gen_swapr(t0
, t0
);
1946 cris_alu(dc
, CC_OP_MOVE
,
1947 cpu_R
[dc
->op1
], cpu_R
[dc
->op1
], t0
, 4);
1952 static unsigned int dec_or_r(DisasContext
*dc
)
1955 int size
= memsize_zz(dc
);
1956 DIS(fprintf (logfile
, "or.%c $r%u, $r%u\n",
1957 memsize_char(size
), dc
->op1
, dc
->op2
));
1958 cris_cc_mask(dc
, CC_MASK_NZ
);
1959 cris_alu_alloc_temps(dc
, size
, t
);
1960 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 0, t
[0], t
[1]);
1961 cris_alu(dc
, CC_OP_OR
, cpu_R
[dc
->op2
], t
[0], t
[1], size
);
1962 cris_alu_free_temps(dc
, size
, t
);
1966 static unsigned int dec_addi_r(DisasContext
*dc
)
1969 DIS(fprintf (logfile
, "addi.%c $r%u, $r%u\n",
1970 memsize_char(memsize_zz(dc
)), dc
->op2
, dc
->op1
));
1971 cris_cc_mask(dc
, 0);
1972 t0
= tcg_temp_new();
1973 tcg_gen_shl_tl(t0
, cpu_R
[dc
->op2
], tcg_const_tl(dc
->zzsize
));
1974 tcg_gen_add_tl(cpu_R
[dc
->op1
], cpu_R
[dc
->op1
], t0
);
1979 static unsigned int dec_addi_acr(DisasContext
*dc
)
1982 DIS(fprintf (logfile
, "addi.%c $r%u, $r%u, $acr\n",
1983 memsize_char(memsize_zz(dc
)), dc
->op2
, dc
->op1
));
1984 cris_cc_mask(dc
, 0);
1985 t0
= tcg_temp_new();
1986 tcg_gen_shl_tl(t0
, cpu_R
[dc
->op2
], tcg_const_tl(dc
->zzsize
));
1987 tcg_gen_add_tl(cpu_R
[R_ACR
], cpu_R
[dc
->op1
], t0
);
1992 static unsigned int dec_neg_r(DisasContext
*dc
)
1995 int size
= memsize_zz(dc
);
1996 DIS(fprintf (logfile
, "neg.%c $r%u, $r%u\n",
1997 memsize_char(size
), dc
->op1
, dc
->op2
));
1998 cris_cc_mask(dc
, CC_MASK_NZVC
);
1999 cris_alu_alloc_temps(dc
, size
, t
);
2000 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 0, t
[0], t
[1]);
2002 cris_alu(dc
, CC_OP_NEG
, cpu_R
[dc
->op2
], t
[0], t
[1], size
);
2003 cris_alu_free_temps(dc
, size
, t
);
2007 static unsigned int dec_btst_r(DisasContext
*dc
)
2010 DIS(fprintf (logfile
, "btst $r%u, $r%u\n",
2012 cris_cc_mask(dc
, CC_MASK_NZ
);
2014 l0
= tcg_temp_local_new();
2015 cris_alu(dc
, CC_OP_BTST
, l0
, cpu_R
[dc
->op2
], cpu_R
[dc
->op1
], 4);
2016 cris_update_cc_op(dc
, CC_OP_FLAGS
, 4);
2017 t_gen_mov_preg_TN(dc
, PR_CCS
, l0
);
2018 dc
->flags_uptodate
= 1;
2023 static unsigned int dec_sub_r(DisasContext
*dc
)
2026 int size
= memsize_zz(dc
);
2027 DIS(fprintf (logfile
, "sub.%c $r%u, $r%u\n",
2028 memsize_char(size
), dc
->op1
, dc
->op2
));
2029 cris_cc_mask(dc
, CC_MASK_NZVC
);
2030 cris_alu_alloc_temps(dc
, size
, t
);
2031 dec_prep_alu_r(dc
, dc
->op1
, dc
->op2
, size
, 0, t
[0], t
[1]);
2032 cris_alu(dc
, CC_OP_SUB
, cpu_R
[dc
->op2
], t
[0], t
[1], size
);
2033 cris_alu_free_temps(dc
, size
, t
);
2037 /* Zero extension. From size to dword. */
2038 static unsigned int dec_movu_r(DisasContext
*dc
)
2041 int size
= memsize_z(dc
);
2042 DIS(fprintf (logfile
, "movu.%c $r%u, $r%u\n",
2046 cris_cc_mask(dc
, CC_MASK_NZ
);
2047 t0
= tcg_temp_new();
2048 dec_prep_move_r(dc
, dc
->op1
, dc
->op2
, size
, 0, t0
);
2049 cris_alu(dc
, CC_OP_MOVE
, cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], t0
, 4);
2054 /* Sign extension. From size to dword. */
2055 static unsigned int dec_movs_r(DisasContext
*dc
)
2058 int size
= memsize_z(dc
);
2059 DIS(fprintf (logfile
, "movs.%c $r%u, $r%u\n",
2063 cris_cc_mask(dc
, CC_MASK_NZ
);
2064 t0
= tcg_temp_new();
2065 /* Size can only be qi or hi. */
2066 t_gen_sext(t0
, cpu_R
[dc
->op1
], size
);
2067 cris_alu(dc
, CC_OP_MOVE
,
2068 cpu_R
[dc
->op2
], cpu_R
[dc
->op1
], t0
, 4);
2073 /* zero extension. From size to dword. */
2074 static unsigned int dec_addu_r(DisasContext
*dc
)
2077 int size
= memsize_z(dc
);
2078 DIS(fprintf (logfile
, "addu.%c $r%u, $r%u\n",
2082 cris_cc_mask(dc
, CC_MASK_NZVC
);
2083 t0
= tcg_temp_new();
2084 /* Size can only be qi or hi. */
2085 t_gen_zext(t0
, cpu_R
[dc
->op1
], size
);
2086 cris_alu(dc
, CC_OP_ADD
,
2087 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], t0
, 4);
2092 /* Sign extension. From size to dword. */
2093 static unsigned int dec_adds_r(DisasContext
*dc
)
2096 int size
= memsize_z(dc
);
2097 DIS(fprintf (logfile
, "adds.%c $r%u, $r%u\n",
2101 cris_cc_mask(dc
, CC_MASK_NZVC
);
2102 t0
= tcg_temp_new();
2103 /* Size can only be qi or hi. */
2104 t_gen_sext(t0
, cpu_R
[dc
->op1
], size
);
2105 cris_alu(dc
, CC_OP_ADD
,
2106 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], t0
, 4);
2111 /* Zero extension. From size to dword. */
2112 static unsigned int dec_subu_r(DisasContext
*dc
)
2115 int size
= memsize_z(dc
);
2116 DIS(fprintf (logfile
, "subu.%c $r%u, $r%u\n",
2120 cris_cc_mask(dc
, CC_MASK_NZVC
);
2121 t0
= tcg_temp_new();
2122 /* Size can only be qi or hi. */
2123 t_gen_zext(t0
, cpu_R
[dc
->op1
], size
);
2124 cris_alu(dc
, CC_OP_SUB
,
2125 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], t0
, 4);
2130 /* Sign extension. From size to dword. */
2131 static unsigned int dec_subs_r(DisasContext
*dc
)
2134 int size
= memsize_z(dc
);
2135 DIS(fprintf (logfile
, "subs.%c $r%u, $r%u\n",
2139 cris_cc_mask(dc
, CC_MASK_NZVC
);
2140 t0
= tcg_temp_new();
2141 /* Size can only be qi or hi. */
2142 t_gen_sext(t0
, cpu_R
[dc
->op1
], size
);
2143 cris_alu(dc
, CC_OP_SUB
,
2144 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], t0
, 4);
2149 static unsigned int dec_setclrf(DisasContext
*dc
)
2152 int set
= (~dc
->opcode
>> 2) & 1;
2155 flags
= (EXTRACT_FIELD(dc
->ir
, 12, 15) << 4)
2156 | EXTRACT_FIELD(dc
->ir
, 0, 3);
2157 if (set
&& flags
== 0) {
2158 DIS(fprintf (logfile
, "nop\n"));
2160 } else if (!set
&& (flags
& 0x20)) {
2161 DIS(fprintf (logfile
, "di\n"));
2164 DIS(fprintf (logfile
, "%sf %x\n",
2165 set
? "set" : "clr",
2169 /* User space is not allowed to touch these. Silently ignore. */
2170 if (dc
->tb_flags
& U_FLAG
) {
2171 flags
&= ~(S_FLAG
| I_FLAG
| U_FLAG
);
2174 if (flags
& X_FLAG
) {
2175 dc
->flagx_known
= 1;
2177 dc
->flags_x
= X_FLAG
;
2182 /* Break the TB if the P flag changes. */
2183 if (flags
& P_FLAG
) {
2184 if ((set
&& !(dc
->tb_flags
& P_FLAG
))
2185 || (!set
&& (dc
->tb_flags
& P_FLAG
))) {
2186 tcg_gen_movi_tl(env_pc
, dc
->pc
+ 2);
2187 dc
->is_jmp
= DISAS_UPDATE
;
2188 dc
->cpustate_changed
= 1;
2191 if (flags
& S_FLAG
) {
2192 dc
->cpustate_changed
= 1;
2196 /* Simply decode the flags. */
2197 cris_evaluate_flags (dc
);
2198 cris_update_cc_op(dc
, CC_OP_FLAGS
, 4);
2199 cris_update_cc_x(dc
);
2200 tcg_gen_movi_tl(cc_op
, dc
->cc_op
);
2203 if (!(dc
->tb_flags
& U_FLAG
) && (flags
& U_FLAG
)) {
2204 /* Enter user mode. */
2205 t_gen_mov_env_TN(ksp
, cpu_R
[R_SP
]);
2206 tcg_gen_mov_tl(cpu_R
[R_SP
], cpu_PR
[PR_USP
]);
2207 dc
->cpustate_changed
= 1;
2209 tcg_gen_ori_tl(cpu_PR
[PR_CCS
], cpu_PR
[PR_CCS
], flags
);
2212 tcg_gen_andi_tl(cpu_PR
[PR_CCS
], cpu_PR
[PR_CCS
], ~flags
);
2214 dc
->flags_uptodate
= 1;
2219 static unsigned int dec_move_rs(DisasContext
*dc
)
2221 DIS(fprintf (logfile
, "move $r%u, $s%u\n", dc
->op1
, dc
->op2
));
2222 cris_cc_mask(dc
, 0);
2223 gen_helper_movl_sreg_reg(tcg_const_tl(dc
->op2
), tcg_const_tl(dc
->op1
));
2226 static unsigned int dec_move_sr(DisasContext
*dc
)
2228 DIS(fprintf (logfile
, "move $s%u, $r%u\n", dc
->op2
, dc
->op1
));
2229 cris_cc_mask(dc
, 0);
2230 gen_helper_movl_reg_sreg(tcg_const_tl(dc
->op1
), tcg_const_tl(dc
->op2
));
2234 static unsigned int dec_move_rp(DisasContext
*dc
)
2237 DIS(fprintf (logfile
, "move $r%u, $p%u\n", dc
->op1
, dc
->op2
));
2238 cris_cc_mask(dc
, 0);
2240 t
[0] = tcg_temp_new();
2241 if (dc
->op2
== PR_CCS
) {
2242 cris_evaluate_flags(dc
);
2243 t_gen_mov_TN_reg(t
[0], dc
->op1
);
2244 if (dc
->tb_flags
& U_FLAG
) {
2245 t
[1] = tcg_temp_new();
2246 /* User space is not allowed to touch all flags. */
2247 tcg_gen_andi_tl(t
[0], t
[0], 0x39f);
2248 tcg_gen_andi_tl(t
[1], cpu_PR
[PR_CCS
], ~0x39f);
2249 tcg_gen_or_tl(t
[0], t
[1], t
[0]);
2250 tcg_temp_free(t
[1]);
2254 t_gen_mov_TN_reg(t
[0], dc
->op1
);
2256 t_gen_mov_preg_TN(dc
, dc
->op2
, t
[0]);
2257 if (dc
->op2
== PR_CCS
) {
2258 cris_update_cc_op(dc
, CC_OP_FLAGS
, 4);
2259 dc
->flags_uptodate
= 1;
2261 tcg_temp_free(t
[0]);
2264 static unsigned int dec_move_pr(DisasContext
*dc
)
2267 DIS(fprintf (logfile
, "move $p%u, $r%u\n", dc
->op1
, dc
->op2
));
2268 cris_cc_mask(dc
, 0);
2270 if (dc
->op2
== PR_CCS
)
2271 cris_evaluate_flags(dc
);
2273 t0
= tcg_temp_new();
2274 t_gen_mov_TN_preg(t0
, dc
->op2
);
2275 cris_alu(dc
, CC_OP_MOVE
,
2276 cpu_R
[dc
->op1
], cpu_R
[dc
->op1
], t0
, preg_sizes
[dc
->op2
]);
2281 static unsigned int dec_move_mr(DisasContext
*dc
)
2283 int memsize
= memsize_zz(dc
);
2285 DIS(fprintf (logfile
, "move.%c [$r%u%s, $r%u\n",
2286 memsize_char(memsize
),
2287 dc
->op1
, dc
->postinc
? "+]" : "]",
2291 insn_len
= dec_prep_move_m(dc
, 0, 4, cpu_R
[dc
->op2
]);
2292 cris_cc_mask(dc
, CC_MASK_NZ
);
2293 cris_update_cc_op(dc
, CC_OP_MOVE
, 4);
2294 cris_update_cc_x(dc
);
2295 cris_update_result(dc
, cpu_R
[dc
->op2
]);
2300 t0
= tcg_temp_new();
2301 insn_len
= dec_prep_move_m(dc
, 0, memsize
, t0
);
2302 cris_cc_mask(dc
, CC_MASK_NZ
);
2303 cris_alu(dc
, CC_OP_MOVE
,
2304 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], t0
, memsize
);
2307 do_postinc(dc
, memsize
);
2311 static inline void cris_alu_m_alloc_temps(TCGv
*t
)
2313 t
[0] = tcg_temp_new();
2314 t
[1] = tcg_temp_new();
2317 static inline void cris_alu_m_free_temps(TCGv
*t
)
2319 tcg_temp_free(t
[0]);
2320 tcg_temp_free(t
[1]);
2323 static unsigned int dec_movs_m(DisasContext
*dc
)
2326 int memsize
= memsize_z(dc
);
2328 DIS(fprintf (logfile
, "movs.%c [$r%u%s, $r%u\n",
2329 memsize_char(memsize
),
2330 dc
->op1
, dc
->postinc
? "+]" : "]",
2333 cris_alu_m_alloc_temps(t
);
2335 insn_len
= dec_prep_alu_m(dc
, 1, memsize
, t
[0], t
[1]);
2336 cris_cc_mask(dc
, CC_MASK_NZ
);
2337 cris_alu(dc
, CC_OP_MOVE
,
2338 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], t
[1], 4);
2339 do_postinc(dc
, memsize
);
2340 cris_alu_m_free_temps(t
);
2344 static unsigned int dec_addu_m(DisasContext
*dc
)
2347 int memsize
= memsize_z(dc
);
2349 DIS(fprintf (logfile
, "addu.%c [$r%u%s, $r%u\n",
2350 memsize_char(memsize
),
2351 dc
->op1
, dc
->postinc
? "+]" : "]",
2354 cris_alu_m_alloc_temps(t
);
2356 insn_len
= dec_prep_alu_m(dc
, 0, memsize
, t
[0], t
[1]);
2357 cris_cc_mask(dc
, CC_MASK_NZVC
);
2358 cris_alu(dc
, CC_OP_ADD
,
2359 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], t
[1], 4);
2360 do_postinc(dc
, memsize
);
2361 cris_alu_m_free_temps(t
);
2365 static unsigned int dec_adds_m(DisasContext
*dc
)
2368 int memsize
= memsize_z(dc
);
2370 DIS(fprintf (logfile
, "adds.%c [$r%u%s, $r%u\n",
2371 memsize_char(memsize
),
2372 dc
->op1
, dc
->postinc
? "+]" : "]",
2375 cris_alu_m_alloc_temps(t
);
2377 insn_len
= dec_prep_alu_m(dc
, 1, memsize
, t
[0], t
[1]);
2378 cris_cc_mask(dc
, CC_MASK_NZVC
);
2379 cris_alu(dc
, CC_OP_ADD
, cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], t
[1], 4);
2380 do_postinc(dc
, memsize
);
2381 cris_alu_m_free_temps(t
);
2385 static unsigned int dec_subu_m(DisasContext
*dc
)
2388 int memsize
= memsize_z(dc
);
2390 DIS(fprintf (logfile
, "subu.%c [$r%u%s, $r%u\n",
2391 memsize_char(memsize
),
2392 dc
->op1
, dc
->postinc
? "+]" : "]",
2395 cris_alu_m_alloc_temps(t
);
2397 insn_len
= dec_prep_alu_m(dc
, 0, memsize
, t
[0], t
[1]);
2398 cris_cc_mask(dc
, CC_MASK_NZVC
);
2399 cris_alu(dc
, CC_OP_SUB
, cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], t
[1], 4);
2400 do_postinc(dc
, memsize
);
2401 cris_alu_m_free_temps(t
);
2405 static unsigned int dec_subs_m(DisasContext
*dc
)
2408 int memsize
= memsize_z(dc
);
2410 DIS(fprintf (logfile
, "subs.%c [$r%u%s, $r%u\n",
2411 memsize_char(memsize
),
2412 dc
->op1
, dc
->postinc
? "+]" : "]",
2415 cris_alu_m_alloc_temps(t
);
2417 insn_len
= dec_prep_alu_m(dc
, 1, memsize
, t
[0], t
[1]);
2418 cris_cc_mask(dc
, CC_MASK_NZVC
);
2419 cris_alu(dc
, CC_OP_SUB
, cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], t
[1], 4);
2420 do_postinc(dc
, memsize
);
2421 cris_alu_m_free_temps(t
);
2425 static unsigned int dec_movu_m(DisasContext
*dc
)
2428 int memsize
= memsize_z(dc
);
2431 DIS(fprintf (logfile
, "movu.%c [$r%u%s, $r%u\n",
2432 memsize_char(memsize
),
2433 dc
->op1
, dc
->postinc
? "+]" : "]",
2436 cris_alu_m_alloc_temps(t
);
2437 insn_len
= dec_prep_alu_m(dc
, 0, memsize
, t
[0], t
[1]);
2438 cris_cc_mask(dc
, CC_MASK_NZ
);
2439 cris_alu(dc
, CC_OP_MOVE
, cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], t
[1], 4);
2440 do_postinc(dc
, memsize
);
2441 cris_alu_m_free_temps(t
);
2445 static unsigned int dec_cmpu_m(DisasContext
*dc
)
2448 int memsize
= memsize_z(dc
);
2450 DIS(fprintf (logfile
, "cmpu.%c [$r%u%s, $r%u\n",
2451 memsize_char(memsize
),
2452 dc
->op1
, dc
->postinc
? "+]" : "]",
2455 cris_alu_m_alloc_temps(t
);
2456 insn_len
= dec_prep_alu_m(dc
, 0, memsize
, t
[0], t
[1]);
2457 cris_cc_mask(dc
, CC_MASK_NZVC
);
2458 cris_alu(dc
, CC_OP_CMP
, cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], t
[1], 4);
2459 do_postinc(dc
, memsize
);
2460 cris_alu_m_free_temps(t
);
2464 static unsigned int dec_cmps_m(DisasContext
*dc
)
2467 int memsize
= memsize_z(dc
);
2469 DIS(fprintf (logfile
, "cmps.%c [$r%u%s, $r%u\n",
2470 memsize_char(memsize
),
2471 dc
->op1
, dc
->postinc
? "+]" : "]",
2474 cris_alu_m_alloc_temps(t
);
2475 insn_len
= dec_prep_alu_m(dc
, 1, memsize
, t
[0], t
[1]);
2476 cris_cc_mask(dc
, CC_MASK_NZVC
);
2477 cris_alu(dc
, CC_OP_CMP
,
2478 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], t
[1],
2480 do_postinc(dc
, memsize
);
2481 cris_alu_m_free_temps(t
);
2485 static unsigned int dec_cmp_m(DisasContext
*dc
)
2488 int memsize
= memsize_zz(dc
);
2490 DIS(fprintf (logfile
, "cmp.%c [$r%u%s, $r%u\n",
2491 memsize_char(memsize
),
2492 dc
->op1
, dc
->postinc
? "+]" : "]",
2495 cris_alu_m_alloc_temps(t
);
2496 insn_len
= dec_prep_alu_m(dc
, 0, memsize
, t
[0], t
[1]);
2497 cris_cc_mask(dc
, CC_MASK_NZVC
);
2498 cris_alu(dc
, CC_OP_CMP
,
2499 cpu_R
[dc
->op2
], cpu_R
[dc
->op2
], t
[1],
2501 do_postinc(dc
, memsize
);
2502 cris_alu_m_free_temps(t
);
2506 static unsigned int dec_test_m(DisasContext
*dc
)
2509 int memsize
= memsize_zz(dc
);
2511 DIS(fprintf (logfile
, "test.%d [$r%u%s] op2=%x\n",
2512 memsize_char(memsize
),
2513 dc
->op1
, dc
->postinc
? "+]" : "]",
2516 cris_evaluate_flags(dc
);
2518 cris_alu_m_alloc_temps(t
);
2519 insn_len
= dec_prep_alu_m(dc
, 0, memsize
, t
[0], t
[1]);
2520 cris_cc_mask(dc
, CC_MASK_NZ
);
2521 tcg_gen_andi_tl(cpu_PR
[PR_CCS
], cpu_PR
[PR_CCS
], ~3);
2523 cris_alu(dc
, CC_OP_CMP
,
2524 cpu_R
[dc
->op2
], t
[1], tcg_const_tl(0), memsize_zz(dc
));
2525 do_postinc(dc
, memsize
);
2526 cris_alu_m_free_temps(t
);
2530 static unsigned int dec_and_m(DisasContext
*dc
)
2533 int memsize
= memsize_zz(dc
);
2535 DIS(fprintf (logfile
, "and.%d [$r%u%s, $r%u\n",
2536 memsize_char(memsize
),
2537 dc
->op1
, dc
->postinc
? "+]" : "]",
2540 cris_alu_m_alloc_temps(t
);
2541 insn_len
= dec_prep_alu_m(dc
, 0, memsize
, t
[0], t
[1]);
2542 cris_cc_mask(dc
, CC_MASK_NZ
);
2543 cris_alu(dc
, CC_OP_AND
, cpu_R
[dc
->op2
], t
[0], t
[1], memsize_zz(dc
));
2544 do_postinc(dc
, memsize
);
2545 cris_alu_m_free_temps(t
);
2549 static unsigned int dec_add_m(DisasContext
*dc
)
2552 int memsize
= memsize_zz(dc
);
2554 DIS(fprintf (logfile
, "add.%d [$r%u%s, $r%u\n",
2555 memsize_char(memsize
),
2556 dc
->op1
, dc
->postinc
? "+]" : "]",
2559 cris_alu_m_alloc_temps(t
);
2560 insn_len
= dec_prep_alu_m(dc
, 0, memsize
, t
[0], t
[1]);
2561 cris_cc_mask(dc
, CC_MASK_NZVC
);
2562 cris_alu(dc
, CC_OP_ADD
,
2563 cpu_R
[dc
->op2
], t
[0], t
[1], memsize_zz(dc
));
2564 do_postinc(dc
, memsize
);
2565 cris_alu_m_free_temps(t
);
2569 static unsigned int dec_addo_m(DisasContext
*dc
)
2572 int memsize
= memsize_zz(dc
);
2574 DIS(fprintf (logfile
, "add.%d [$r%u%s, $r%u\n",
2575 memsize_char(memsize
),
2576 dc
->op1
, dc
->postinc
? "+]" : "]",
2579 cris_alu_m_alloc_temps(t
);
2580 insn_len
= dec_prep_alu_m(dc
, 1, memsize
, t
[0], t
[1]);
2581 cris_cc_mask(dc
, 0);
2582 cris_alu(dc
, CC_OP_ADD
, cpu_R
[R_ACR
], t
[0], t
[1], 4);
2583 do_postinc(dc
, memsize
);
2584 cris_alu_m_free_temps(t
);
2588 static unsigned int dec_bound_m(DisasContext
*dc
)
2591 int memsize
= memsize_zz(dc
);
2593 DIS(fprintf (logfile
, "bound.%d [$r%u%s, $r%u\n",
2594 memsize_char(memsize
),
2595 dc
->op1
, dc
->postinc
? "+]" : "]",
2598 l
[0] = tcg_temp_local_new();
2599 l
[1] = tcg_temp_local_new();
2600 insn_len
= dec_prep_alu_m(dc
, 0, memsize
, l
[0], l
[1]);
2601 cris_cc_mask(dc
, CC_MASK_NZ
);
2602 cris_alu(dc
, CC_OP_BOUND
, cpu_R
[dc
->op2
], l
[0], l
[1], 4);
2603 do_postinc(dc
, memsize
);
2604 tcg_temp_free(l
[0]);
2605 tcg_temp_free(l
[1]);
2609 static unsigned int dec_addc_mr(DisasContext
*dc
)
2613 DIS(fprintf (logfile
, "addc [$r%u%s, $r%u\n",
2614 dc
->op1
, dc
->postinc
? "+]" : "]",
2617 cris_evaluate_flags(dc
);
2618 cris_alu_m_alloc_temps(t
);
2619 insn_len
= dec_prep_alu_m(dc
, 0, 4, t
[0], t
[1]);
2620 cris_cc_mask(dc
, CC_MASK_NZVC
);
2621 cris_alu(dc
, CC_OP_ADDC
, cpu_R
[dc
->op2
], t
[0], t
[1], 4);
2623 cris_alu_m_free_temps(t
);
2627 static unsigned int dec_sub_m(DisasContext
*dc
)
2630 int memsize
= memsize_zz(dc
);
2632 DIS(fprintf (logfile
, "sub.%c [$r%u%s, $r%u ir=%x zz=%x\n",
2633 memsize_char(memsize
),
2634 dc
->op1
, dc
->postinc
? "+]" : "]",
2635 dc
->op2
, dc
->ir
, dc
->zzsize
));
2637 cris_alu_m_alloc_temps(t
);
2638 insn_len
= dec_prep_alu_m(dc
, 0, memsize
, t
[0], t
[1]);
2639 cris_cc_mask(dc
, CC_MASK_NZVC
);
2640 cris_alu(dc
, CC_OP_SUB
, cpu_R
[dc
->op2
], t
[0], t
[1], memsize
);
2641 do_postinc(dc
, memsize
);
2642 cris_alu_m_free_temps(t
);
2646 static unsigned int dec_or_m(DisasContext
*dc
)
2649 int memsize
= memsize_zz(dc
);
2651 DIS(fprintf (logfile
, "or.%d [$r%u%s, $r%u pc=%x\n",
2652 memsize_char(memsize
),
2653 dc
->op1
, dc
->postinc
? "+]" : "]",
2656 cris_alu_m_alloc_temps(t
);
2657 insn_len
= dec_prep_alu_m(dc
, 0, memsize
, t
[0], t
[1]);
2658 cris_cc_mask(dc
, CC_MASK_NZ
);
2659 cris_alu(dc
, CC_OP_OR
,
2660 cpu_R
[dc
->op2
], t
[0], t
[1], memsize_zz(dc
));
2661 do_postinc(dc
, memsize
);
2662 cris_alu_m_free_temps(t
);
2666 static unsigned int dec_move_mp(DisasContext
*dc
)
2669 int memsize
= memsize_zz(dc
);
2672 DIS(fprintf (logfile
, "move.%c [$r%u%s, $p%u\n",
2673 memsize_char(memsize
),
2675 dc
->postinc
? "+]" : "]",
2678 cris_alu_m_alloc_temps(t
);
2679 insn_len
= dec_prep_alu_m(dc
, 0, memsize
, t
[0], t
[1]);
2680 cris_cc_mask(dc
, 0);
2681 if (dc
->op2
== PR_CCS
) {
2682 cris_evaluate_flags(dc
);
2683 if (dc
->tb_flags
& U_FLAG
) {
2684 /* User space is not allowed to touch all flags. */
2685 tcg_gen_andi_tl(t
[1], t
[1], 0x39f);
2686 tcg_gen_andi_tl(t
[0], cpu_PR
[PR_CCS
], ~0x39f);
2687 tcg_gen_or_tl(t
[1], t
[0], t
[1]);
2691 t_gen_mov_preg_TN(dc
, dc
->op2
, t
[1]);
2693 do_postinc(dc
, memsize
);
2694 cris_alu_m_free_temps(t
);
2698 static unsigned int dec_move_pm(DisasContext
*dc
)
2703 memsize
= preg_sizes
[dc
->op2
];
2705 DIS(fprintf (logfile
, "move.%c $p%u, [$r%u%s\n",
2706 memsize_char(memsize
),
2707 dc
->op2
, dc
->op1
, dc
->postinc
? "+]" : "]"));
2709 /* prepare store. Address in T0, value in T1. */
2710 if (dc
->op2
== PR_CCS
)
2711 cris_evaluate_flags(dc
);
2712 t0
= tcg_temp_new();
2713 t_gen_mov_TN_preg(t0
, dc
->op2
);
2714 cris_flush_cc_state(dc
);
2715 gen_store(dc
, cpu_R
[dc
->op1
], t0
, memsize
);
2718 cris_cc_mask(dc
, 0);
2720 tcg_gen_addi_tl(cpu_R
[dc
->op1
], cpu_R
[dc
->op1
], memsize
);
2724 static unsigned int dec_movem_mr(DisasContext
*dc
)
2730 int nr
= dc
->op2
+ 1;
2732 DIS(fprintf (logfile
, "movem [$r%u%s, $r%u\n", dc
->op1
,
2733 dc
->postinc
? "+]" : "]", dc
->op2
));
2735 addr
= tcg_temp_new();
2736 /* There are probably better ways of doing this. */
2737 cris_flush_cc_state(dc
);
2738 for (i
= 0; i
< (nr
>> 1); i
++) {
2739 tmp
[i
] = tcg_temp_new_i64();
2740 tcg_gen_addi_tl(addr
, cpu_R
[dc
->op1
], i
* 8);
2741 gen_load64(dc
, tmp
[i
], addr
);
2744 tmp32
= tcg_temp_new_i32();
2745 tcg_gen_addi_tl(addr
, cpu_R
[dc
->op1
], i
* 8);
2746 gen_load(dc
, tmp32
, addr
, 4, 0);
2748 tcg_temp_free(addr
);
2750 for (i
= 0; i
< (nr
>> 1); i
++) {
2751 tcg_gen_trunc_i64_i32(cpu_R
[i
* 2], tmp
[i
]);
2752 tcg_gen_shri_i64(tmp
[i
], tmp
[i
], 32);
2753 tcg_gen_trunc_i64_i32(cpu_R
[i
* 2 + 1], tmp
[i
]);
2754 tcg_temp_free_i64(tmp
[i
]);
2757 tcg_gen_mov_tl(cpu_R
[dc
->op2
], tmp32
);
2758 tcg_temp_free(tmp32
);
2761 /* writeback the updated pointer value. */
2763 tcg_gen_addi_tl(cpu_R
[dc
->op1
], cpu_R
[dc
->op1
], nr
* 4);
2765 /* gen_load might want to evaluate the previous insns flags. */
2766 cris_cc_mask(dc
, 0);
2770 static unsigned int dec_movem_rm(DisasContext
*dc
)
2776 DIS(fprintf (logfile
, "movem $r%u, [$r%u%s\n", dc
->op2
, dc
->op1
,
2777 dc
->postinc
? "+]" : "]"));
2779 cris_flush_cc_state(dc
);
2781 tmp
= tcg_temp_new();
2782 addr
= tcg_temp_new();
2783 tcg_gen_movi_tl(tmp
, 4);
2784 tcg_gen_mov_tl(addr
, cpu_R
[dc
->op1
]);
2785 for (i
= 0; i
<= dc
->op2
; i
++) {
2786 /* Displace addr. */
2787 /* Perform the store. */
2788 gen_store(dc
, addr
, cpu_R
[i
], 4);
2789 tcg_gen_add_tl(addr
, addr
, tmp
);
2792 tcg_gen_mov_tl(cpu_R
[dc
->op1
], addr
);
2793 cris_cc_mask(dc
, 0);
2795 tcg_temp_free(addr
);
2799 static unsigned int dec_move_rm(DisasContext
*dc
)
2803 memsize
= memsize_zz(dc
);
2805 DIS(fprintf (logfile
, "move.%d $r%u, [$r%u]\n",
2806 memsize
, dc
->op2
, dc
->op1
));
2808 /* prepare store. */
2809 cris_flush_cc_state(dc
);
2810 gen_store(dc
, cpu_R
[dc
->op1
], cpu_R
[dc
->op2
], memsize
);
2813 tcg_gen_addi_tl(cpu_R
[dc
->op1
], cpu_R
[dc
->op1
], memsize
);
2814 cris_cc_mask(dc
, 0);
2818 static unsigned int dec_lapcq(DisasContext
*dc
)
2820 DIS(fprintf (logfile
, "lapcq %x, $r%u\n",
2821 dc
->pc
+ dc
->op1
*2, dc
->op2
));
2822 cris_cc_mask(dc
, 0);
2823 tcg_gen_movi_tl(cpu_R
[dc
->op2
], dc
->pc
+ dc
->op1
* 2);
2827 static unsigned int dec_lapc_im(DisasContext
*dc
)
2835 cris_cc_mask(dc
, 0);
2836 imm
= ldl_code(dc
->pc
+ 2);
2837 DIS(fprintf (logfile
, "lapc 0x%x, $r%u\n", imm
+ dc
->pc
, dc
->op2
));
2841 t_gen_mov_reg_TN(rd
, tcg_const_tl(pc
));
2845 /* Jump to special reg. */
2846 static unsigned int dec_jump_p(DisasContext
*dc
)
2848 DIS(fprintf (logfile
, "jump $p%u\n", dc
->op2
));
2850 if (dc
->op2
== PR_CCS
)
2851 cris_evaluate_flags(dc
);
2852 t_gen_mov_TN_preg(env_btarget
, dc
->op2
);
2853 /* rete will often have low bit set to indicate delayslot. */
2854 tcg_gen_andi_tl(env_btarget
, env_btarget
, ~1);
2855 cris_cc_mask(dc
, 0);
2856 cris_prepare_jmp(dc
, JMP_INDIRECT
);
2860 /* Jump and save. */
2861 static unsigned int dec_jas_r(DisasContext
*dc
)
2863 DIS(fprintf (logfile
, "jas $r%u, $p%u\n", dc
->op1
, dc
->op2
));
2864 cris_cc_mask(dc
, 0);
2865 /* Store the return address in Pd. */
2866 tcg_gen_mov_tl(env_btarget
, cpu_R
[dc
->op1
]);
2869 t_gen_mov_preg_TN(dc
, dc
->op2
, tcg_const_tl(dc
->pc
+ 4));
2871 cris_prepare_jmp(dc
, JMP_INDIRECT
);
2875 static unsigned int dec_jas_im(DisasContext
*dc
)
2879 imm
= ldl_code(dc
->pc
+ 2);
2881 DIS(fprintf (logfile
, "jas 0x%x\n", imm
));
2882 cris_cc_mask(dc
, 0);
2883 /* Store the return address in Pd. */
2884 t_gen_mov_preg_TN(dc
, dc
->op2
, tcg_const_tl(dc
->pc
+ 8));
2887 cris_prepare_jmp(dc
, JMP_DIRECT
);
2891 static unsigned int dec_jasc_im(DisasContext
*dc
)
2895 imm
= ldl_code(dc
->pc
+ 2);
2897 DIS(fprintf (logfile
, "jasc 0x%x\n", imm
));
2898 cris_cc_mask(dc
, 0);
2899 /* Store the return address in Pd. */
2900 t_gen_mov_preg_TN(dc
, dc
->op2
, tcg_const_tl(dc
->pc
+ 8 + 4));
2903 cris_prepare_jmp(dc
, JMP_DIRECT
);
2907 static unsigned int dec_jasc_r(DisasContext
*dc
)
2909 DIS(fprintf (logfile
, "jasc_r $r%u, $p%u\n", dc
->op1
, dc
->op2
));
2910 cris_cc_mask(dc
, 0);
2911 /* Store the return address in Pd. */
2912 tcg_gen_mov_tl(env_btarget
, cpu_R
[dc
->op1
]);
2913 t_gen_mov_preg_TN(dc
, dc
->op2
, tcg_const_tl(dc
->pc
+ 4 + 4));
2914 cris_prepare_jmp(dc
, JMP_INDIRECT
);
2918 static unsigned int dec_bcc_im(DisasContext
*dc
)
2921 uint32_t cond
= dc
->op2
;
2923 offset
= ldsw_code(dc
->pc
+ 2);
2925 DIS(fprintf (logfile
, "b%s %d pc=%x dst=%x\n",
2926 cc_name(cond
), offset
,
2927 dc
->pc
, dc
->pc
+ offset
));
2929 cris_cc_mask(dc
, 0);
2930 /* op2 holds the condition-code. */
2931 cris_prepare_cc_branch (dc
, offset
, cond
);
2935 static unsigned int dec_bas_im(DisasContext
*dc
)
2940 simm
= ldl_code(dc
->pc
+ 2);
2942 DIS(fprintf (logfile
, "bas 0x%x, $p%u\n", dc
->pc
+ simm
, dc
->op2
));
2943 cris_cc_mask(dc
, 0);
2944 /* Store the return address in Pd. */
2945 t_gen_mov_preg_TN(dc
, dc
->op2
, tcg_const_tl(dc
->pc
+ 8));
2947 dc
->jmp_pc
= dc
->pc
+ simm
;
2948 cris_prepare_jmp(dc
, JMP_DIRECT
);
2952 static unsigned int dec_basc_im(DisasContext
*dc
)
2955 simm
= ldl_code(dc
->pc
+ 2);
2957 DIS(fprintf (logfile
, "basc 0x%x, $p%u\n", dc
->pc
+ simm
, dc
->op2
));
2958 cris_cc_mask(dc
, 0);
2959 /* Store the return address in Pd. */
2960 t_gen_mov_preg_TN(dc
, dc
->op2
, tcg_const_tl(dc
->pc
+ 12));
2962 dc
->jmp_pc
= dc
->pc
+ simm
;
2963 cris_prepare_jmp(dc
, JMP_DIRECT
);
2967 static unsigned int dec_rfe_etc(DisasContext
*dc
)
2969 cris_cc_mask(dc
, 0);
2971 if (dc
->op2
== 15) /* ignore halt. */
2974 switch (dc
->op2
& 7) {
2977 DIS(fprintf(logfile
, "rfe\n"));
2978 cris_evaluate_flags(dc
);
2980 dc
->is_jmp
= DISAS_UPDATE
;
2984 DIS(fprintf(logfile
, "rfn\n"));
2985 cris_evaluate_flags(dc
);
2987 dc
->is_jmp
= DISAS_UPDATE
;
2990 DIS(fprintf(logfile
, "break %d\n", dc
->op1
));
2991 cris_evaluate_flags (dc
);
2993 tcg_gen_movi_tl(env_pc
, dc
->pc
+ 2);
2995 /* Breaks start at 16 in the exception vector. */
2996 t_gen_mov_env_TN(trap_vector
,
2997 tcg_const_tl(dc
->op1
+ 16));
2998 t_gen_raise_exception(EXCP_BREAK
);
2999 dc
->is_jmp
= DISAS_UPDATE
;
3002 printf ("op2=%x\n", dc
->op2
);
3010 static unsigned int dec_ftag_fidx_d_m(DisasContext
*dc
)
3015 static unsigned int dec_ftag_fidx_i_m(DisasContext
*dc
)
3020 static unsigned int dec_null(DisasContext
*dc
)
3022 printf ("unknown insn pc=%x opc=%x op1=%x op2=%x\n",
3023 dc
->pc
, dc
->opcode
, dc
->op1
, dc
->op2
);
3029 static struct decoder_info
{
3034 unsigned int (*dec
)(DisasContext
*dc
);
3036 /* Order matters here. */
3037 {DEC_MOVEQ
, dec_moveq
},
3038 {DEC_BTSTQ
, dec_btstq
},
3039 {DEC_CMPQ
, dec_cmpq
},
3040 {DEC_ADDOQ
, dec_addoq
},
3041 {DEC_ADDQ
, dec_addq
},
3042 {DEC_SUBQ
, dec_subq
},
3043 {DEC_ANDQ
, dec_andq
},
3045 {DEC_ASRQ
, dec_asrq
},
3046 {DEC_LSLQ
, dec_lslq
},
3047 {DEC_LSRQ
, dec_lsrq
},
3048 {DEC_BCCQ
, dec_bccq
},
3050 {DEC_BCC_IM
, dec_bcc_im
},
3051 {DEC_JAS_IM
, dec_jas_im
},
3052 {DEC_JAS_R
, dec_jas_r
},
3053 {DEC_JASC_IM
, dec_jasc_im
},
3054 {DEC_JASC_R
, dec_jasc_r
},
3055 {DEC_BAS_IM
, dec_bas_im
},
3056 {DEC_BASC_IM
, dec_basc_im
},
3057 {DEC_JUMP_P
, dec_jump_p
},
3058 {DEC_LAPC_IM
, dec_lapc_im
},
3059 {DEC_LAPCQ
, dec_lapcq
},
3061 {DEC_RFE_ETC
, dec_rfe_etc
},
3062 {DEC_ADDC_MR
, dec_addc_mr
},
3064 {DEC_MOVE_MP
, dec_move_mp
},
3065 {DEC_MOVE_PM
, dec_move_pm
},
3066 {DEC_MOVEM_MR
, dec_movem_mr
},
3067 {DEC_MOVEM_RM
, dec_movem_rm
},
3068 {DEC_MOVE_PR
, dec_move_pr
},
3069 {DEC_SCC_R
, dec_scc_r
},
3070 {DEC_SETF
, dec_setclrf
},
3071 {DEC_CLEARF
, dec_setclrf
},
3073 {DEC_MOVE_SR
, dec_move_sr
},
3074 {DEC_MOVE_RP
, dec_move_rp
},
3075 {DEC_SWAP_R
, dec_swap_r
},
3076 {DEC_ABS_R
, dec_abs_r
},
3077 {DEC_LZ_R
, dec_lz_r
},
3078 {DEC_MOVE_RS
, dec_move_rs
},
3079 {DEC_BTST_R
, dec_btst_r
},
3080 {DEC_ADDC_R
, dec_addc_r
},
3082 {DEC_DSTEP_R
, dec_dstep_r
},
3083 {DEC_XOR_R
, dec_xor_r
},
3084 {DEC_MCP_R
, dec_mcp_r
},
3085 {DEC_CMP_R
, dec_cmp_r
},
3087 {DEC_ADDI_R
, dec_addi_r
},
3088 {DEC_ADDI_ACR
, dec_addi_acr
},
3090 {DEC_ADD_R
, dec_add_r
},
3091 {DEC_SUB_R
, dec_sub_r
},
3093 {DEC_ADDU_R
, dec_addu_r
},
3094 {DEC_ADDS_R
, dec_adds_r
},
3095 {DEC_SUBU_R
, dec_subu_r
},
3096 {DEC_SUBS_R
, dec_subs_r
},
3097 {DEC_LSL_R
, dec_lsl_r
},
3099 {DEC_AND_R
, dec_and_r
},
3100 {DEC_OR_R
, dec_or_r
},
3101 {DEC_BOUND_R
, dec_bound_r
},
3102 {DEC_ASR_R
, dec_asr_r
},
3103 {DEC_LSR_R
, dec_lsr_r
},
3105 {DEC_MOVU_R
, dec_movu_r
},
3106 {DEC_MOVS_R
, dec_movs_r
},
3107 {DEC_NEG_R
, dec_neg_r
},
3108 {DEC_MOVE_R
, dec_move_r
},
3110 {DEC_FTAG_FIDX_I_M
, dec_ftag_fidx_i_m
},
3111 {DEC_FTAG_FIDX_D_M
, dec_ftag_fidx_d_m
},
3113 {DEC_MULS_R
, dec_muls_r
},
3114 {DEC_MULU_R
, dec_mulu_r
},
3116 {DEC_ADDU_M
, dec_addu_m
},
3117 {DEC_ADDS_M
, dec_adds_m
},
3118 {DEC_SUBU_M
, dec_subu_m
},
3119 {DEC_SUBS_M
, dec_subs_m
},
3121 {DEC_CMPU_M
, dec_cmpu_m
},
3122 {DEC_CMPS_M
, dec_cmps_m
},
3123 {DEC_MOVU_M
, dec_movu_m
},
3124 {DEC_MOVS_M
, dec_movs_m
},
3126 {DEC_CMP_M
, dec_cmp_m
},
3127 {DEC_ADDO_M
, dec_addo_m
},
3128 {DEC_BOUND_M
, dec_bound_m
},
3129 {DEC_ADD_M
, dec_add_m
},
3130 {DEC_SUB_M
, dec_sub_m
},
3131 {DEC_AND_M
, dec_and_m
},
3132 {DEC_OR_M
, dec_or_m
},
3133 {DEC_MOVE_RM
, dec_move_rm
},
3134 {DEC_TEST_M
, dec_test_m
},
3135 {DEC_MOVE_MR
, dec_move_mr
},
3140 static inline unsigned int
3141 cris_decoder(DisasContext
*dc
)
3143 unsigned int insn_len
= 2;
3146 if (unlikely(loglevel
& CPU_LOG_TB_OP
))
3147 tcg_gen_debug_insn_start(dc
->pc
);
3149 /* Load a halfword onto the instruction register. */
3150 dc
->ir
= lduw_code(dc
->pc
);
3152 /* Now decode it. */
3153 dc
->opcode
= EXTRACT_FIELD(dc
->ir
, 4, 11);
3154 dc
->op1
= EXTRACT_FIELD(dc
->ir
, 0, 3);
3155 dc
->op2
= EXTRACT_FIELD(dc
->ir
, 12, 15);
3156 dc
->zsize
= EXTRACT_FIELD(dc
->ir
, 4, 4);
3157 dc
->zzsize
= EXTRACT_FIELD(dc
->ir
, 4, 5);
3158 dc
->postinc
= EXTRACT_FIELD(dc
->ir
, 10, 10);
3160 /* Large switch for all insns. */
3161 for (i
= 0; i
< sizeof decinfo
/ sizeof decinfo
[0]; i
++) {
3162 if ((dc
->opcode
& decinfo
[i
].mask
) == decinfo
[i
].bits
)
3164 insn_len
= decinfo
[i
].dec(dc
);
3169 #if !defined(CONFIG_USER_ONLY)
3170 /* Single-stepping ? */
3171 if (dc
->tb_flags
& S_FLAG
) {
3174 l1
= gen_new_label();
3175 tcg_gen_brcondi_tl(TCG_COND_NE
, cpu_PR
[PR_SPC
], dc
->pc
, l1
);
3176 /* We treat SPC as a break with an odd trap vector. */
3177 cris_evaluate_flags (dc
);
3178 t_gen_mov_env_TN(trap_vector
, tcg_const_tl(3));
3179 tcg_gen_movi_tl(env_pc
, dc
->pc
+ insn_len
);
3180 tcg_gen_movi_tl(cpu_PR
[PR_SPC
], dc
->pc
+ insn_len
);
3181 t_gen_raise_exception(EXCP_BREAK
);
3188 static void check_breakpoint(CPUState
*env
, DisasContext
*dc
)
3192 if (unlikely(env
->breakpoints
)) {
3193 for (bp
= env
->breakpoints
; bp
!= NULL
; bp
= bp
->next
) {
3194 if (bp
->pc
== dc
->pc
) {
3195 cris_evaluate_flags (dc
);
3196 tcg_gen_movi_tl(env_pc
, dc
->pc
);
3197 t_gen_raise_exception(EXCP_DEBUG
);
3198 dc
->is_jmp
= DISAS_UPDATE
;
3206 * Delay slots on QEMU/CRIS.
3208 * If an exception hits on a delayslot, the core will let ERP (the Exception
3209 * Return Pointer) point to the branch (the previous) insn and set the lsb to
3210 * to give SW a hint that the exception actually hit on the dslot.
3212 * CRIS expects all PC addresses to be 16-bit aligned. The lsb is ignored by
3213 * the core and any jmp to an odd addresses will mask off that lsb. It is
3214 * simply there to let sw know there was an exception on a dslot.
3216 * When the software returns from an exception, the branch will re-execute.
3217 * On QEMU care needs to be taken when a branch+delayslot sequence is broken
3218 * and the branch and delayslot dont share pages.
3220 * The TB contaning the branch insn will set up env->btarget and evaluate
3221 * env->btaken. When the translation loop exits we will note that the branch
3222 * sequence is broken and let env->dslot be the size of the branch insn (those
3225 * The TB contaning the delayslot will have the PC of its real insn (i.e no lsb
3226 * set). It will also expect to have env->dslot setup with the size of the
3227 * delay slot so that env->pc - env->dslot point to the branch insn. This TB
3228 * will execute the dslot and take the branch, either to btarget or just one
3231 * When exceptions occur, we check for env->dslot in do_interrupt to detect
3232 * broken branch sequences and setup $erp accordingly (i.e let it point to the
3233 * branch and set lsb). Then env->dslot gets cleared so that the exception
3234 * handler can enter. When returning from exceptions (jump $erp) the lsb gets
3235 * masked off and we will reexecute the branch insn.
3239 /* generate intermediate code for basic block 'tb'. */
3241 gen_intermediate_code_internal(CPUState
*env
, TranslationBlock
*tb
,
3244 uint16_t *gen_opc_end
;
3246 unsigned int insn_len
;
3248 struct DisasContext ctx
;
3249 struct DisasContext
*dc
= &ctx
;
3250 uint32_t next_page_start
;
3258 /* Odd PC indicates that branch is rexecuting due to exception in the
3259 * delayslot, like in real hw.
3261 pc_start
= tb
->pc
& ~1;
3265 gen_opc_end
= gen_opc_buf
+ OPC_MAX_SIZE
;
3267 dc
->is_jmp
= DISAS_NEXT
;
3270 dc
->singlestep_enabled
= env
->singlestep_enabled
;
3271 dc
->flags_uptodate
= 1;
3272 dc
->flagx_known
= 1;
3273 dc
->flags_x
= tb
->flags
& X_FLAG
;
3274 dc
->cc_x_uptodate
= 0;
3278 cris_update_cc_op(dc
, CC_OP_FLAGS
, 4);
3279 dc
->cc_size_uptodate
= -1;
3281 /* Decode TB flags. */
3282 dc
->tb_flags
= tb
->flags
& (S_FLAG
| P_FLAG
| U_FLAG
| X_FLAG
);
3283 dc
->delayed_branch
= !!(tb
->flags
& 7);
3284 if (dc
->delayed_branch
)
3285 dc
->jmp
= JMP_INDIRECT
;
3287 dc
->jmp
= JMP_NOJMP
;
3289 dc
->cpustate_changed
= 0;
3291 if (loglevel
& CPU_LOG_TB_IN_ASM
) {
3293 "srch=%d pc=%x %x flg=%llx bt=%x ds=%u ccs=%x\n"
3299 search_pc
, dc
->pc
, dc
->ppc
,
3300 (unsigned long long)tb
->flags
,
3301 env
->btarget
, (unsigned)tb
->flags
& 7,
3303 env
->pregs
[PR_PID
], env
->pregs
[PR_USP
],
3304 env
->regs
[0], env
->regs
[1], env
->regs
[2], env
->regs
[3],
3305 env
->regs
[4], env
->regs
[5], env
->regs
[6], env
->regs
[7],
3306 env
->regs
[8], env
->regs
[9],
3307 env
->regs
[10], env
->regs
[11],
3308 env
->regs
[12], env
->regs
[13],
3309 env
->regs
[14], env
->regs
[15]);
3310 fprintf(logfile
, "--------------\n");
3311 fprintf(logfile
, "IN: %s\n", lookup_symbol(pc_start
));
3314 next_page_start
= (pc_start
& TARGET_PAGE_MASK
) + TARGET_PAGE_SIZE
;
3317 max_insns
= tb
->cflags
& CF_COUNT_MASK
;
3319 max_insns
= CF_COUNT_MASK
;
3324 check_breakpoint(env
, dc
);
3327 j
= gen_opc_ptr
- gen_opc_buf
;
3331 gen_opc_instr_start
[lj
++] = 0;
3333 if (dc
->delayed_branch
== 1)
3334 gen_opc_pc
[lj
] = dc
->ppc
| 1;
3336 gen_opc_pc
[lj
] = dc
->pc
;
3337 gen_opc_instr_start
[lj
] = 1;
3338 gen_opc_icount
[lj
] = num_insns
;
3342 DIS(fprintf(logfile
, "%8.8x:\t", dc
->pc
));
3344 if (num_insns
+ 1 == max_insns
&& (tb
->cflags
& CF_LAST_IO
))
3348 insn_len
= cris_decoder(dc
);
3352 cris_clear_x_flag(dc
);
3355 /* Check for delayed branches here. If we do it before
3356 actually generating any host code, the simulator will just
3357 loop doing nothing for on this program location. */
3358 if (dc
->delayed_branch
) {
3359 dc
->delayed_branch
--;
3360 if (dc
->delayed_branch
== 0)
3363 t_gen_mov_env_TN(dslot
,
3365 if (dc
->jmp
== JMP_DIRECT
) {
3366 dc
->is_jmp
= DISAS_NEXT
;
3368 t_gen_cc_jmp(env_btarget
,
3369 tcg_const_tl(dc
->pc
));
3370 dc
->is_jmp
= DISAS_JUMP
;
3376 /* If we are rexecuting a branch due to exceptions on
3377 delay slots dont break. */
3378 if (!(tb
->pc
& 1) && env
->singlestep_enabled
)
3380 } while (!dc
->is_jmp
&& !dc
->cpustate_changed
3381 && gen_opc_ptr
< gen_opc_end
3382 && (dc
->pc
< next_page_start
)
3383 && num_insns
< max_insns
);
3386 if (dc
->jmp
== JMP_DIRECT
&& !dc
->delayed_branch
)
3389 if (tb
->cflags
& CF_LAST_IO
)
3391 /* Force an update if the per-tb cpu state has changed. */
3392 if (dc
->is_jmp
== DISAS_NEXT
3393 && (dc
->cpustate_changed
|| !dc
->flagx_known
3394 || (dc
->flags_x
!= (tb
->flags
& X_FLAG
)))) {
3395 dc
->is_jmp
= DISAS_UPDATE
;
3396 tcg_gen_movi_tl(env_pc
, npc
);
3398 /* Broken branch+delayslot sequence. */
3399 if (dc
->delayed_branch
== 1) {
3400 /* Set env->dslot to the size of the branch insn. */
3401 t_gen_mov_env_TN(dslot
, tcg_const_tl(dc
->pc
- dc
->ppc
));
3402 cris_store_direct_jmp(dc
);
3405 cris_evaluate_flags (dc
);
3407 if (unlikely(env
->singlestep_enabled
)) {
3408 if (dc
->is_jmp
== DISAS_NEXT
)
3409 tcg_gen_movi_tl(env_pc
, npc
);
3410 t_gen_raise_exception(EXCP_DEBUG
);
3412 switch(dc
->is_jmp
) {
3414 gen_goto_tb(dc
, 1, npc
);
3419 /* indicate that the hash table must be used
3420 to find the next TB */
3425 /* nothing more to generate */
3429 gen_icount_end(tb
, num_insns
);
3430 *gen_opc_ptr
= INDEX_op_end
;
3432 j
= gen_opc_ptr
- gen_opc_buf
;
3435 gen_opc_instr_start
[lj
++] = 0;
3437 tb
->size
= dc
->pc
- pc_start
;
3438 tb
->icount
= num_insns
;
3443 if (loglevel
& CPU_LOG_TB_IN_ASM
) {
3444 target_disas(logfile
, pc_start
, dc
->pc
- pc_start
, 0);
3445 fprintf(logfile
, "\nisize=%d osize=%zd\n",
3446 dc
->pc
- pc_start
, gen_opc_ptr
- gen_opc_buf
);
3452 void gen_intermediate_code (CPUState
*env
, struct TranslationBlock
*tb
)
3454 gen_intermediate_code_internal(env
, tb
, 0);
3457 void gen_intermediate_code_pc (CPUState
*env
, struct TranslationBlock
*tb
)
3459 gen_intermediate_code_internal(env
, tb
, 1);
3462 void cpu_dump_state (CPUState
*env
, FILE *f
,
3463 int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...),
3472 cpu_fprintf(f
, "PC=%x CCS=%x btaken=%d btarget=%x\n"
3473 "cc_op=%d cc_src=%d cc_dest=%d cc_result=%x cc_mask=%x\n",
3474 env
->pc
, env
->pregs
[PR_CCS
], env
->btaken
, env
->btarget
,
3476 env
->cc_src
, env
->cc_dest
, env
->cc_result
, env
->cc_mask
);
3479 for (i
= 0; i
< 16; i
++) {
3480 cpu_fprintf(f
, "r%2.2d=%8.8x ", i
, env
->regs
[i
]);
3481 if ((i
+ 1) % 4 == 0)
3482 cpu_fprintf(f
, "\n");
3484 cpu_fprintf(f
, "\nspecial regs:\n");
3485 for (i
= 0; i
< 16; i
++) {
3486 cpu_fprintf(f
, "p%2.2d=%8.8x ", i
, env
->pregs
[i
]);
3487 if ((i
+ 1) % 4 == 0)
3488 cpu_fprintf(f
, "\n");
3490 srs
= env
->pregs
[PR_SRS
];
3491 cpu_fprintf(f
, "\nsupport function regs bank %x:\n", srs
);
3493 for (i
= 0; i
< 16; i
++) {
3494 cpu_fprintf(f
, "s%2.2d=%8.8x ",
3495 i
, env
->sregs
[srs
][i
]);
3496 if ((i
+ 1) % 4 == 0)
3497 cpu_fprintf(f
, "\n");
3500 cpu_fprintf(f
, "\n\n");
3504 CPUCRISState
*cpu_cris_init (const char *cpu_model
)
3507 static int tcg_initialized
= 0;
3510 env
= qemu_mallocz(sizeof(CPUCRISState
));
3517 if (tcg_initialized
)
3520 tcg_initialized
= 1;
3522 cpu_env
= tcg_global_reg_new_ptr(TCG_AREG0
, "env");
3523 cc_x
= tcg_global_mem_new(TCG_AREG0
,
3524 offsetof(CPUState
, cc_x
), "cc_x");
3525 cc_src
= tcg_global_mem_new(TCG_AREG0
,
3526 offsetof(CPUState
, cc_src
), "cc_src");
3527 cc_dest
= tcg_global_mem_new(TCG_AREG0
,
3528 offsetof(CPUState
, cc_dest
),
3530 cc_result
= tcg_global_mem_new(TCG_AREG0
,
3531 offsetof(CPUState
, cc_result
),
3533 cc_op
= tcg_global_mem_new(TCG_AREG0
,
3534 offsetof(CPUState
, cc_op
), "cc_op");
3535 cc_size
= tcg_global_mem_new(TCG_AREG0
,
3536 offsetof(CPUState
, cc_size
),
3538 cc_mask
= tcg_global_mem_new(TCG_AREG0
,
3539 offsetof(CPUState
, cc_mask
),
3542 env_pc
= tcg_global_mem_new(TCG_AREG0
,
3543 offsetof(CPUState
, pc
),
3545 env_btarget
= tcg_global_mem_new(TCG_AREG0
,
3546 offsetof(CPUState
, btarget
),
3548 env_btaken
= tcg_global_mem_new(TCG_AREG0
,
3549 offsetof(CPUState
, btaken
),
3551 for (i
= 0; i
< 16; i
++) {
3552 cpu_R
[i
] = tcg_global_mem_new(TCG_AREG0
,
3553 offsetof(CPUState
, regs
[i
]),
3556 for (i
= 0; i
< 16; i
++) {
3557 cpu_PR
[i
] = tcg_global_mem_new(TCG_AREG0
,
3558 offsetof(CPUState
, pregs
[i
]),
3562 #define GEN_HELPER 2
3568 void cpu_reset (CPUCRISState
*env
)
3570 memset(env
, 0, offsetof(CPUCRISState
, breakpoints
));
3573 env
->pregs
[PR_VR
] = 32;
3574 #if defined(CONFIG_USER_ONLY)
3575 /* start in user mode with interrupts enabled. */
3576 env
->pregs
[PR_CCS
] |= U_FLAG
| I_FLAG
;
3578 env
->pregs
[PR_CCS
] = 0;
3582 void gen_pc_load(CPUState
*env
, struct TranslationBlock
*tb
,
3583 unsigned long searched_pc
, int pc_pos
, void *puc
)
3585 env
->pc
= gen_opc_pc
[pc_pos
];