Some more updates for the g3bw -> g3beige change
[qemu/mini2440.git] / target-cris / translate.c
blob03ac7ea74ba51825e3136bbbdb9ed08aac2ad004
1 /*
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
23 * FIXME:
24 * The condition code translation is in need of attention.
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <inttypes.h>
32 #include <assert.h>
34 #include "cpu.h"
35 #include "exec-all.h"
36 #include "disas.h"
37 #include "tcg-op.h"
38 #include "helper.h"
39 #include "crisv32-decode.h"
40 #include "qemu-common.h"
42 #define GEN_HELPER 1
43 #include "helper.h"
45 #define DISAS_CRIS 0
46 #if DISAS_CRIS
47 #define DIS(x) if (loglevel & CPU_LOG_TB_IN_ASM) x
48 #else
49 #define DIS(x)
50 #endif
52 #define D(x)
53 #define BUG() (gen_BUG(dc, __FILE__, __LINE__))
54 #define BUG_ON(x) ({if (x) BUG();})
56 #define DISAS_SWI 5
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];
70 static TCGv cc_x;
71 static TCGv cc_src;
72 static TCGv cc_dest;
73 static TCGv cc_result;
74 static TCGv cc_op;
75 static TCGv cc_size;
76 static TCGv cc_mask;
78 static TCGv env_btaken;
79 static TCGv env_btarget;
80 static TCGv env_pc;
82 #include "gen-icount.h"
84 /* This is the state at translation time. */
85 typedef struct DisasContext {
86 CPUState *env;
87 target_ulong pc, ppc;
89 /* Decoder. */
90 uint32_t ir;
91 uint32_t opcode;
92 unsigned int op1;
93 unsigned int op2;
94 unsigned int zsize, zzsize;
95 unsigned int mode;
96 unsigned int postinc;
98 int update_cc;
99 int cc_op;
100 int cc_size;
101 uint32_t cc_mask;
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
108 translation time. */
109 int flags_x;
111 int clear_x; /* Clear x after this insn? */
112 int cpustate_changed;
113 unsigned int tb_flags; /* tb dependent flags. */
114 int is_jmp;
116 #define JMP_NOJMP 0
117 #define JMP_DIRECT 1
118 #define JMP_INDIRECT 2
119 int jmp; /* 0=nojmp, 1=direct, 2=indirect. */
120 uint32_t jmp_pc;
122 int delayed_branch;
124 struct TranslationBlock *tb;
125 int singlestep_enabled;
126 } DisasContext;
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[] = {
152 1, /* bz. */
153 1, /* vr. */
154 4, /* pid. */
155 1, /* srs. */
156 2, /* wz. */
157 4, 4, 4,
158 4, 4, 4, 4,
159 4, 4, 4, 4,
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)
169 if (r < 0 || r > 15)
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)
175 if (r < 0 || r > 15)
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)
195 if (r < 0 || r > 15)
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));
199 else if (r == PR_VR)
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]);
205 else
206 tcg_gen_mov_tl(tn, cpu_PR[r]);
208 static inline void t_gen_mov_preg_TN(DisasContext *dc, int r, TCGv tn)
210 if (r < 0 || r > 15)
211 fprintf(stderr, "wrong register write $p%d\n", r);
212 if (r == PR_BZ || r == PR_WZ || r == PR_DZ)
213 return;
214 else if (r == PR_SRS)
215 tcg_gen_andi_tl(cpu_PR[r], tn, 3);
216 else {
217 if (r == PR_PID)
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)
236 TCGv t0, t_31;
238 t0 = tcg_temp_new();
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);
246 tcg_temp_free(t0);
247 tcg_temp_free(t_31);
250 static void t_gen_lsr(TCGv d, TCGv a, TCGv b)
252 TCGv t0, t_31;
254 t0 = tcg_temp_new();
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);
263 tcg_temp_free(t0);
264 tcg_temp_free(t_31);
267 static void t_gen_asr(TCGv d, TCGv a, TCGv b)
269 TCGv t0, t_31;
271 t0 = tcg_temp_new();
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);
279 tcg_temp_free(t0);
280 tcg_temp_free(t_31);
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)
286 TCGv_i64 t0, t1;
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)
306 TCGv_i64 t0, t1;
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 static void t_gen_cris_dstep(TCGv d, TCGv a, TCGv b)
325 int l1;
327 l1 = gen_new_label();
330 * d <<= 1
331 * if (d >= s)
332 * d -= s;
334 tcg_gen_shli_tl(d, a, 1);
335 tcg_gen_brcond_tl(TCG_COND_LTU, d, b, l1);
336 tcg_gen_sub_tl(d, d, b);
337 gen_set_label(l1);
340 /* Extended arithmetics on CRIS. */
341 static inline void t_gen_add_flag(TCGv d, int flag)
343 TCGv c;
345 c = tcg_temp_new();
346 t_gen_mov_TN_preg(c, PR_CCS);
347 /* Propagate carry into d. */
348 tcg_gen_andi_tl(c, c, 1 << flag);
349 if (flag)
350 tcg_gen_shri_tl(c, c, flag);
351 tcg_gen_add_tl(d, d, c);
352 tcg_temp_free(c);
355 static inline void t_gen_addx_carry(DisasContext *dc, TCGv d)
357 if (dc->flagx_known) {
358 if (dc->flags_x) {
359 TCGv c;
361 c = tcg_temp_new();
362 t_gen_mov_TN_preg(c, PR_CCS);
363 /* C flag is already at bit 0. */
364 tcg_gen_andi_tl(c, c, C_FLAG);
365 tcg_gen_add_tl(d, d, c);
366 tcg_temp_free(c);
368 } else {
369 TCGv x, c;
371 x = tcg_temp_new();
372 c = tcg_temp_new();
373 t_gen_mov_TN_preg(x, PR_CCS);
374 tcg_gen_mov_tl(c, x);
376 /* Propagate carry into d if X is set. Branch free. */
377 tcg_gen_andi_tl(c, c, C_FLAG);
378 tcg_gen_andi_tl(x, x, X_FLAG);
379 tcg_gen_shri_tl(x, x, 4);
381 tcg_gen_and_tl(x, x, c);
382 tcg_gen_add_tl(d, d, x);
383 tcg_temp_free(x);
384 tcg_temp_free(c);
388 static inline void t_gen_subx_carry(DisasContext *dc, TCGv d)
390 if (dc->flagx_known) {
391 if (dc->flags_x) {
392 TCGv c;
394 c = tcg_temp_new();
395 t_gen_mov_TN_preg(c, PR_CCS);
396 /* C flag is already at bit 0. */
397 tcg_gen_andi_tl(c, c, C_FLAG);
398 tcg_gen_sub_tl(d, d, c);
399 tcg_temp_free(c);
401 } else {
402 TCGv x, c;
404 x = tcg_temp_new();
405 c = tcg_temp_new();
406 t_gen_mov_TN_preg(x, PR_CCS);
407 tcg_gen_mov_tl(c, x);
409 /* Propagate carry into d if X is set. Branch free. */
410 tcg_gen_andi_tl(c, c, C_FLAG);
411 tcg_gen_andi_tl(x, x, X_FLAG);
412 tcg_gen_shri_tl(x, x, 4);
414 tcg_gen_and_tl(x, x, c);
415 tcg_gen_sub_tl(d, d, x);
416 tcg_temp_free(x);
417 tcg_temp_free(c);
421 /* Swap the two bytes within each half word of the s operand.
422 T0 = ((T0 << 8) & 0xff00ff00) | ((T0 >> 8) & 0x00ff00ff) */
423 static inline void t_gen_swapb(TCGv d, TCGv s)
425 TCGv t, org_s;
427 t = tcg_temp_new();
428 org_s = tcg_temp_new();
430 /* d and s may refer to the same object. */
431 tcg_gen_mov_tl(org_s, s);
432 tcg_gen_shli_tl(t, org_s, 8);
433 tcg_gen_andi_tl(d, t, 0xff00ff00);
434 tcg_gen_shri_tl(t, org_s, 8);
435 tcg_gen_andi_tl(t, t, 0x00ff00ff);
436 tcg_gen_or_tl(d, d, t);
437 tcg_temp_free(t);
438 tcg_temp_free(org_s);
441 /* Swap the halfwords of the s operand. */
442 static inline void t_gen_swapw(TCGv d, TCGv s)
444 TCGv t;
445 /* d and s refer the same object. */
446 t = tcg_temp_new();
447 tcg_gen_mov_tl(t, s);
448 tcg_gen_shli_tl(d, t, 16);
449 tcg_gen_shri_tl(t, t, 16);
450 tcg_gen_or_tl(d, d, t);
451 tcg_temp_free(t);
454 /* Reverse the within each byte.
455 T0 = (((T0 << 7) & 0x80808080) |
456 ((T0 << 5) & 0x40404040) |
457 ((T0 << 3) & 0x20202020) |
458 ((T0 << 1) & 0x10101010) |
459 ((T0 >> 1) & 0x08080808) |
460 ((T0 >> 3) & 0x04040404) |
461 ((T0 >> 5) & 0x02020202) |
462 ((T0 >> 7) & 0x01010101));
464 static inline void t_gen_swapr(TCGv d, TCGv s)
466 struct {
467 int shift; /* LSL when positive, LSR when negative. */
468 uint32_t mask;
469 } bitrev [] = {
470 {7, 0x80808080},
471 {5, 0x40404040},
472 {3, 0x20202020},
473 {1, 0x10101010},
474 {-1, 0x08080808},
475 {-3, 0x04040404},
476 {-5, 0x02020202},
477 {-7, 0x01010101}
479 int i;
480 TCGv t, org_s;
482 /* d and s refer the same object. */
483 t = tcg_temp_new();
484 org_s = tcg_temp_new();
485 tcg_gen_mov_tl(org_s, s);
487 tcg_gen_shli_tl(t, org_s, bitrev[0].shift);
488 tcg_gen_andi_tl(d, t, bitrev[0].mask);
489 for (i = 1; i < ARRAY_SIZE(bitrev); i++) {
490 if (bitrev[i].shift >= 0) {
491 tcg_gen_shli_tl(t, org_s, bitrev[i].shift);
492 } else {
493 tcg_gen_shri_tl(t, org_s, -bitrev[i].shift);
495 tcg_gen_andi_tl(t, t, bitrev[i].mask);
496 tcg_gen_or_tl(d, d, t);
498 tcg_temp_free(t);
499 tcg_temp_free(org_s);
502 static void t_gen_cc_jmp(TCGv pc_true, TCGv pc_false)
504 TCGv btaken;
505 int l1;
507 l1 = gen_new_label();
508 btaken = tcg_temp_new();
510 /* Conditional jmp. */
511 tcg_gen_mov_tl(btaken, env_btaken);
512 tcg_gen_mov_tl(env_pc, pc_false);
513 tcg_gen_brcondi_tl(TCG_COND_EQ, btaken, 0, l1);
514 tcg_gen_mov_tl(env_pc, pc_true);
515 gen_set_label(l1);
517 tcg_temp_free(btaken);
520 static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
522 TranslationBlock *tb;
523 tb = dc->tb;
524 if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK)) {
525 tcg_gen_goto_tb(n);
526 tcg_gen_movi_tl(env_pc, dest);
527 tcg_gen_exit_tb((long)tb + n);
528 } else {
529 tcg_gen_movi_tl(env_pc, dest);
530 tcg_gen_exit_tb(0);
534 /* Sign extend at translation time. */
535 static int sign_extend(unsigned int val, unsigned int width)
537 int sval;
539 /* LSL. */
540 val <<= 31 - width;
541 sval = val;
542 /* ASR. */
543 sval >>= 31 - width;
544 return sval;
547 static inline void cris_clear_x_flag(DisasContext *dc)
549 if (dc->flagx_known && dc->flags_x)
550 dc->flags_uptodate = 0;
552 dc->flagx_known = 1;
553 dc->flags_x = 0;
556 static void cris_flush_cc_state(DisasContext *dc)
558 if (dc->cc_size_uptodate != dc->cc_size) {
559 tcg_gen_movi_tl(cc_size, dc->cc_size);
560 dc->cc_size_uptodate = dc->cc_size;
562 tcg_gen_movi_tl(cc_op, dc->cc_op);
563 tcg_gen_movi_tl(cc_mask, dc->cc_mask);
566 static void cris_evaluate_flags(DisasContext *dc)
568 if (dc->flags_uptodate)
569 return;
571 cris_flush_cc_state(dc);
573 switch (dc->cc_op)
575 case CC_OP_MCP:
576 gen_helper_evaluate_flags_mcp(cpu_PR[PR_CCS],
577 cpu_PR[PR_CCS], cc_src,
578 cc_dest, cc_result);
579 break;
580 case CC_OP_MULS:
581 gen_helper_evaluate_flags_muls(cpu_PR[PR_CCS],
582 cpu_PR[PR_CCS], cc_result,
583 cpu_PR[PR_MOF]);
584 break;
585 case CC_OP_MULU:
586 gen_helper_evaluate_flags_mulu(cpu_PR[PR_CCS],
587 cpu_PR[PR_CCS], cc_result,
588 cpu_PR[PR_MOF]);
589 break;
590 case CC_OP_MOVE:
591 case CC_OP_AND:
592 case CC_OP_OR:
593 case CC_OP_XOR:
594 case CC_OP_ASR:
595 case CC_OP_LSR:
596 case CC_OP_LSL:
597 switch (dc->cc_size)
599 case 4:
600 gen_helper_evaluate_flags_move_4(cpu_PR[PR_CCS],
601 cpu_PR[PR_CCS], cc_result);
602 break;
603 case 2:
604 gen_helper_evaluate_flags_move_2(cpu_PR[PR_CCS],
605 cpu_PR[PR_CCS], cc_result);
606 break;
607 default:
608 gen_helper_evaluate_flags();
609 break;
611 break;
612 case CC_OP_FLAGS:
613 /* live. */
614 break;
615 case CC_OP_SUB:
616 case CC_OP_CMP:
617 if (dc->cc_size == 4)
618 gen_helper_evaluate_flags_sub_4(cpu_PR[PR_CCS],
619 cpu_PR[PR_CCS], cc_src, cc_dest, cc_result);
620 else
621 gen_helper_evaluate_flags();
623 break;
624 default:
625 switch (dc->cc_size)
627 case 4:
628 gen_helper_evaluate_flags_alu_4(cpu_PR[PR_CCS],
629 cpu_PR[PR_CCS], cc_src, cc_dest, cc_result);
630 break;
631 default:
632 gen_helper_evaluate_flags();
633 break;
635 break;
638 if (dc->flagx_known) {
639 if (dc->flags_x)
640 tcg_gen_ori_tl(cpu_PR[PR_CCS],
641 cpu_PR[PR_CCS], X_FLAG);
642 else
643 tcg_gen_andi_tl(cpu_PR[PR_CCS],
644 cpu_PR[PR_CCS], ~X_FLAG);
646 dc->flags_uptodate = 1;
649 static void cris_cc_mask(DisasContext *dc, unsigned int mask)
651 uint32_t ovl;
653 if (!mask) {
654 dc->update_cc = 0;
655 return;
658 /* Check if we need to evaluate the condition codes due to
659 CC overlaying. */
660 ovl = (dc->cc_mask ^ mask) & ~mask;
661 if (ovl) {
662 /* TODO: optimize this case. It trigs all the time. */
663 cris_evaluate_flags (dc);
665 dc->cc_mask = mask;
666 dc->update_cc = 1;
669 static void cris_update_cc_op(DisasContext *dc, int op, int size)
671 dc->cc_op = op;
672 dc->cc_size = size;
673 dc->flags_uptodate = 0;
676 static inline void cris_update_cc_x(DisasContext *dc)
678 /* Save the x flag state at the time of the cc snapshot. */
679 if (dc->flagx_known) {
680 if (dc->cc_x_uptodate == (2 | dc->flags_x))
681 return;
682 tcg_gen_movi_tl(cc_x, dc->flags_x);
683 dc->cc_x_uptodate = 2 | dc->flags_x;
685 else {
686 tcg_gen_andi_tl(cc_x, cpu_PR[PR_CCS], X_FLAG);
687 dc->cc_x_uptodate = 1;
691 /* Update cc prior to executing ALU op. Needs source operands untouched. */
692 static void cris_pre_alu_update_cc(DisasContext *dc, int op,
693 TCGv dst, TCGv src, int size)
695 if (dc->update_cc) {
696 cris_update_cc_op(dc, op, size);
697 tcg_gen_mov_tl(cc_src, src);
699 if (op != CC_OP_MOVE
700 && op != CC_OP_AND
701 && op != CC_OP_OR
702 && op != CC_OP_XOR
703 && op != CC_OP_ASR
704 && op != CC_OP_LSR
705 && op != CC_OP_LSL)
706 tcg_gen_mov_tl(cc_dest, dst);
708 cris_update_cc_x(dc);
712 /* Update cc after executing ALU op. needs the result. */
713 static inline void cris_update_result(DisasContext *dc, TCGv res)
715 if (dc->update_cc)
716 tcg_gen_mov_tl(cc_result, res);
719 /* Returns one if the write back stage should execute. */
720 static void cris_alu_op_exec(DisasContext *dc, int op,
721 TCGv dst, TCGv a, TCGv b, int size)
723 /* Emit the ALU insns. */
724 switch (op)
726 case CC_OP_ADD:
727 tcg_gen_add_tl(dst, a, b);
728 /* Extended arithmetics. */
729 t_gen_addx_carry(dc, dst);
730 break;
731 case CC_OP_ADDC:
732 tcg_gen_add_tl(dst, a, b);
733 t_gen_add_flag(dst, 0); /* C_FLAG. */
734 break;
735 case CC_OP_MCP:
736 tcg_gen_add_tl(dst, a, b);
737 t_gen_add_flag(dst, 8); /* R_FLAG. */
738 break;
739 case CC_OP_SUB:
740 tcg_gen_sub_tl(dst, a, b);
741 /* Extended arithmetics. */
742 t_gen_subx_carry(dc, dst);
743 break;
744 case CC_OP_MOVE:
745 tcg_gen_mov_tl(dst, b);
746 break;
747 case CC_OP_OR:
748 tcg_gen_or_tl(dst, a, b);
749 break;
750 case CC_OP_AND:
751 tcg_gen_and_tl(dst, a, b);
752 break;
753 case CC_OP_XOR:
754 tcg_gen_xor_tl(dst, a, b);
755 break;
756 case CC_OP_LSL:
757 t_gen_lsl(dst, a, b);
758 break;
759 case CC_OP_LSR:
760 t_gen_lsr(dst, a, b);
761 break;
762 case CC_OP_ASR:
763 t_gen_asr(dst, a, b);
764 break;
765 case CC_OP_NEG:
766 tcg_gen_neg_tl(dst, b);
767 /* Extended arithmetics. */
768 t_gen_subx_carry(dc, dst);
769 break;
770 case CC_OP_LZ:
771 gen_helper_lz(dst, b);
772 break;
773 case CC_OP_MULS:
774 t_gen_muls(dst, cpu_PR[PR_MOF], a, b);
775 break;
776 case CC_OP_MULU:
777 t_gen_mulu(dst, cpu_PR[PR_MOF], a, b);
778 break;
779 case CC_OP_DSTEP:
780 t_gen_cris_dstep(dst, a, b);
781 break;
782 case CC_OP_BOUND:
784 int l1;
785 l1 = gen_new_label();
786 tcg_gen_mov_tl(dst, a);
787 tcg_gen_brcond_tl(TCG_COND_LEU, a, b, l1);
788 tcg_gen_mov_tl(dst, b);
789 gen_set_label(l1);
791 break;
792 case CC_OP_CMP:
793 tcg_gen_sub_tl(dst, a, b);
794 /* Extended arithmetics. */
795 t_gen_subx_carry(dc, dst);
796 break;
797 default:
798 fprintf (logfile, "illegal ALU op.\n");
799 BUG();
800 break;
803 if (size == 1)
804 tcg_gen_andi_tl(dst, dst, 0xff);
805 else if (size == 2)
806 tcg_gen_andi_tl(dst, dst, 0xffff);
809 static void cris_alu(DisasContext *dc, int op,
810 TCGv d, TCGv op_a, TCGv op_b, int size)
812 TCGv tmp;
813 int writeback;
815 writeback = 1;
817 if (op == CC_OP_CMP) {
818 tmp = tcg_temp_new();
819 writeback = 0;
820 } else if (size == 4) {
821 tmp = d;
822 writeback = 0;
823 } else
824 tmp = tcg_temp_new();
827 cris_pre_alu_update_cc(dc, op, op_a, op_b, size);
828 cris_alu_op_exec(dc, op, tmp, op_a, op_b, size);
829 cris_update_result(dc, tmp);
831 /* Writeback. */
832 if (writeback) {
833 if (size == 1)
834 tcg_gen_andi_tl(d, d, ~0xff);
835 else
836 tcg_gen_andi_tl(d, d, ~0xffff);
837 tcg_gen_or_tl(d, d, tmp);
839 if (!TCGV_EQUAL(tmp, d))
840 tcg_temp_free(tmp);
843 static int arith_cc(DisasContext *dc)
845 if (dc->update_cc) {
846 switch (dc->cc_op) {
847 case CC_OP_ADDC: return 1;
848 case CC_OP_ADD: return 1;
849 case CC_OP_SUB: return 1;
850 case CC_OP_DSTEP: return 1;
851 case CC_OP_LSL: return 1;
852 case CC_OP_LSR: return 1;
853 case CC_OP_ASR: return 1;
854 case CC_OP_CMP: return 1;
855 case CC_OP_NEG: return 1;
856 case CC_OP_OR: return 1;
857 case CC_OP_AND: return 1;
858 case CC_OP_XOR: return 1;
859 case CC_OP_MULU: return 1;
860 case CC_OP_MULS: return 1;
861 default:
862 return 0;
865 return 0;
868 static void gen_tst_cc (DisasContext *dc, TCGv cc, int cond)
870 int arith_opt, move_opt;
872 /* TODO: optimize more condition codes. */
875 * If the flags are live, we've gotta look into the bits of CCS.
876 * Otherwise, if we just did an arithmetic operation we try to
877 * evaluate the condition code faster.
879 * When this function is done, T0 should be non-zero if the condition
880 * code is true.
882 arith_opt = arith_cc(dc) && !dc->flags_uptodate;
883 move_opt = (dc->cc_op == CC_OP_MOVE);
884 switch (cond) {
885 case CC_EQ:
886 if (arith_opt || move_opt) {
887 /* If cc_result is zero, T0 should be
888 non-zero otherwise T0 should be zero. */
889 int l1;
890 l1 = gen_new_label();
891 tcg_gen_movi_tl(cc, 0);
892 tcg_gen_brcondi_tl(TCG_COND_NE, cc_result,
893 0, l1);
894 tcg_gen_movi_tl(cc, 1);
895 gen_set_label(l1);
897 else {
898 cris_evaluate_flags(dc);
899 tcg_gen_andi_tl(cc,
900 cpu_PR[PR_CCS], Z_FLAG);
902 break;
903 case CC_NE:
904 if (arith_opt || move_opt)
905 tcg_gen_mov_tl(cc, cc_result);
906 else {
907 cris_evaluate_flags(dc);
908 tcg_gen_xori_tl(cc, cpu_PR[PR_CCS],
909 Z_FLAG);
910 tcg_gen_andi_tl(cc, cc, Z_FLAG);
912 break;
913 case CC_CS:
914 cris_evaluate_flags(dc);
915 tcg_gen_andi_tl(cc, cpu_PR[PR_CCS], C_FLAG);
916 break;
917 case CC_CC:
918 cris_evaluate_flags(dc);
919 tcg_gen_xori_tl(cc, cpu_PR[PR_CCS], C_FLAG);
920 tcg_gen_andi_tl(cc, cc, C_FLAG);
921 break;
922 case CC_VS:
923 cris_evaluate_flags(dc);
924 tcg_gen_andi_tl(cc, cpu_PR[PR_CCS], V_FLAG);
925 break;
926 case CC_VC:
927 cris_evaluate_flags(dc);
928 tcg_gen_xori_tl(cc, cpu_PR[PR_CCS],
929 V_FLAG);
930 tcg_gen_andi_tl(cc, cc, V_FLAG);
931 break;
932 case CC_PL:
933 if (arith_opt || move_opt) {
934 int bits = 31;
936 if (dc->cc_size == 1)
937 bits = 7;
938 else if (dc->cc_size == 2)
939 bits = 15;
941 tcg_gen_shri_tl(cc, cc_result, bits);
942 tcg_gen_xori_tl(cc, cc, 1);
943 } else {
944 cris_evaluate_flags(dc);
945 tcg_gen_xori_tl(cc, cpu_PR[PR_CCS],
946 N_FLAG);
947 tcg_gen_andi_tl(cc, cc, N_FLAG);
949 break;
950 case CC_MI:
951 if (arith_opt || move_opt) {
952 int bits = 31;
954 if (dc->cc_size == 1)
955 bits = 7;
956 else if (dc->cc_size == 2)
957 bits = 15;
959 tcg_gen_shri_tl(cc, cc_result, 31);
961 else {
962 cris_evaluate_flags(dc);
963 tcg_gen_andi_tl(cc, cpu_PR[PR_CCS],
964 N_FLAG);
966 break;
967 case CC_LS:
968 cris_evaluate_flags(dc);
969 tcg_gen_andi_tl(cc, cpu_PR[PR_CCS],
970 C_FLAG | Z_FLAG);
971 break;
972 case CC_HI:
973 cris_evaluate_flags(dc);
975 TCGv tmp;
977 tmp = tcg_temp_new();
978 tcg_gen_xori_tl(tmp, cpu_PR[PR_CCS],
979 C_FLAG | Z_FLAG);
980 /* Overlay the C flag on top of the Z. */
981 tcg_gen_shli_tl(cc, tmp, 2);
982 tcg_gen_and_tl(cc, tmp, cc);
983 tcg_gen_andi_tl(cc, cc, Z_FLAG);
985 tcg_temp_free(tmp);
987 break;
988 case CC_GE:
989 cris_evaluate_flags(dc);
990 /* Overlay the V flag on top of the N. */
991 tcg_gen_shli_tl(cc, cpu_PR[PR_CCS], 2);
992 tcg_gen_xor_tl(cc,
993 cpu_PR[PR_CCS], cc);
994 tcg_gen_andi_tl(cc, cc, N_FLAG);
995 tcg_gen_xori_tl(cc, cc, N_FLAG);
996 break;
997 case CC_LT:
998 cris_evaluate_flags(dc);
999 /* Overlay the V flag on top of the N. */
1000 tcg_gen_shli_tl(cc, cpu_PR[PR_CCS], 2);
1001 tcg_gen_xor_tl(cc,
1002 cpu_PR[PR_CCS], cc);
1003 tcg_gen_andi_tl(cc, cc, N_FLAG);
1004 break;
1005 case CC_GT:
1006 cris_evaluate_flags(dc);
1008 TCGv n, z;
1010 n = tcg_temp_new();
1011 z = tcg_temp_new();
1013 /* To avoid a shift we overlay everything on
1014 the V flag. */
1015 tcg_gen_shri_tl(n, cpu_PR[PR_CCS], 2);
1016 tcg_gen_shri_tl(z, cpu_PR[PR_CCS], 1);
1017 /* invert Z. */
1018 tcg_gen_xori_tl(z, z, 2);
1020 tcg_gen_xor_tl(n, n, cpu_PR[PR_CCS]);
1021 tcg_gen_xori_tl(n, n, 2);
1022 tcg_gen_and_tl(cc, z, n);
1023 tcg_gen_andi_tl(cc, cc, 2);
1025 tcg_temp_free(n);
1026 tcg_temp_free(z);
1028 break;
1029 case CC_LE:
1030 cris_evaluate_flags(dc);
1032 TCGv n, z;
1034 n = tcg_temp_new();
1035 z = tcg_temp_new();
1037 /* To avoid a shift we overlay everything on
1038 the V flag. */
1039 tcg_gen_shri_tl(n, cpu_PR[PR_CCS], 2);
1040 tcg_gen_shri_tl(z, cpu_PR[PR_CCS], 1);
1042 tcg_gen_xor_tl(n, n, cpu_PR[PR_CCS]);
1043 tcg_gen_or_tl(cc, z, n);
1044 tcg_gen_andi_tl(cc, cc, 2);
1046 tcg_temp_free(n);
1047 tcg_temp_free(z);
1049 break;
1050 case CC_P:
1051 cris_evaluate_flags(dc);
1052 tcg_gen_andi_tl(cc, cpu_PR[PR_CCS], P_FLAG);
1053 break;
1054 case CC_A:
1055 tcg_gen_movi_tl(cc, 1);
1056 break;
1057 default:
1058 BUG();
1059 break;
1063 static void cris_store_direct_jmp(DisasContext *dc)
1065 /* Store the direct jmp state into the cpu-state. */
1066 if (dc->jmp == JMP_DIRECT) {
1067 tcg_gen_movi_tl(env_btarget, dc->jmp_pc);
1068 tcg_gen_movi_tl(env_btaken, 1);
1072 static void cris_prepare_cc_branch (DisasContext *dc,
1073 int offset, int cond)
1075 /* This helps us re-schedule the micro-code to insns in delay-slots
1076 before the actual jump. */
1077 dc->delayed_branch = 2;
1078 dc->jmp_pc = dc->pc + offset;
1080 if (cond != CC_A)
1082 dc->jmp = JMP_INDIRECT;
1083 gen_tst_cc (dc, env_btaken, cond);
1084 tcg_gen_movi_tl(env_btarget, dc->jmp_pc);
1085 } else {
1086 /* Allow chaining. */
1087 dc->jmp = JMP_DIRECT;
1092 /* jumps, when the dest is in a live reg for example. Direct should be set
1093 when the dest addr is constant to allow tb chaining. */
1094 static inline void cris_prepare_jmp (DisasContext *dc, unsigned int type)
1096 /* This helps us re-schedule the micro-code to insns in delay-slots
1097 before the actual jump. */
1098 dc->delayed_branch = 2;
1099 dc->jmp = type;
1100 if (type == JMP_INDIRECT)
1101 tcg_gen_movi_tl(env_btaken, 1);
1104 static void gen_load64(DisasContext *dc, TCGv_i64 dst, TCGv addr)
1106 int mem_index = cpu_mmu_index(dc->env);
1108 /* If we get a fault on a delayslot we must keep the jmp state in
1109 the cpu-state to be able to re-execute the jmp. */
1110 if (dc->delayed_branch == 1)
1111 cris_store_direct_jmp(dc);
1113 tcg_gen_qemu_ld64(dst, addr, mem_index);
1116 static void gen_load(DisasContext *dc, TCGv dst, TCGv addr,
1117 unsigned int size, int sign)
1119 int mem_index = cpu_mmu_index(dc->env);
1121 /* If we get a fault on a delayslot we must keep the jmp state in
1122 the cpu-state to be able to re-execute the jmp. */
1123 if (dc->delayed_branch == 1)
1124 cris_store_direct_jmp(dc);
1126 if (size == 1) {
1127 if (sign)
1128 tcg_gen_qemu_ld8s(dst, addr, mem_index);
1129 else
1130 tcg_gen_qemu_ld8u(dst, addr, mem_index);
1132 else if (size == 2) {
1133 if (sign)
1134 tcg_gen_qemu_ld16s(dst, addr, mem_index);
1135 else
1136 tcg_gen_qemu_ld16u(dst, addr, mem_index);
1138 else if (size == 4) {
1139 tcg_gen_qemu_ld32u(dst, addr, mem_index);
1141 else {
1142 abort();
1146 static void gen_store (DisasContext *dc, TCGv addr, TCGv val,
1147 unsigned int size)
1149 int mem_index = cpu_mmu_index(dc->env);
1151 /* If we get a fault on a delayslot we must keep the jmp state in
1152 the cpu-state to be able to re-execute the jmp. */
1153 if (dc->delayed_branch == 1)
1154 cris_store_direct_jmp(dc);
1157 /* Conditional writes. We only support the kind were X and P are known
1158 at translation time. */
1159 if (dc->flagx_known && dc->flags_x && (dc->tb_flags & P_FLAG)) {
1160 dc->postinc = 0;
1161 cris_evaluate_flags(dc);
1162 tcg_gen_ori_tl(cpu_PR[PR_CCS], cpu_PR[PR_CCS], C_FLAG);
1163 return;
1166 if (size == 1)
1167 tcg_gen_qemu_st8(val, addr, mem_index);
1168 else if (size == 2)
1169 tcg_gen_qemu_st16(val, addr, mem_index);
1170 else
1171 tcg_gen_qemu_st32(val, addr, mem_index);
1173 if (dc->flagx_known && dc->flags_x) {
1174 cris_evaluate_flags(dc);
1175 tcg_gen_andi_tl(cpu_PR[PR_CCS], cpu_PR[PR_CCS], ~C_FLAG);
1179 static inline void t_gen_sext(TCGv d, TCGv s, int size)
1181 if (size == 1)
1182 tcg_gen_ext8s_i32(d, s);
1183 else if (size == 2)
1184 tcg_gen_ext16s_i32(d, s);
1185 else if(!TCGV_EQUAL(d, s))
1186 tcg_gen_mov_tl(d, s);
1189 static inline void t_gen_zext(TCGv d, TCGv s, int size)
1191 if (size == 1)
1192 tcg_gen_ext8u_i32(d, s);
1193 else if (size == 2)
1194 tcg_gen_ext16u_i32(d, s);
1195 else if (!TCGV_EQUAL(d, s))
1196 tcg_gen_mov_tl(d, s);
1199 #if DISAS_CRIS
1200 static char memsize_char(int size)
1202 switch (size)
1204 case 1: return 'b'; break;
1205 case 2: return 'w'; break;
1206 case 4: return 'd'; break;
1207 default:
1208 return 'x';
1209 break;
1212 #endif
1214 static inline unsigned int memsize_z(DisasContext *dc)
1216 return dc->zsize + 1;
1219 static inline unsigned int memsize_zz(DisasContext *dc)
1221 switch (dc->zzsize)
1223 case 0: return 1;
1224 case 1: return 2;
1225 default:
1226 return 4;
1230 static inline void do_postinc (DisasContext *dc, int size)
1232 if (dc->postinc)
1233 tcg_gen_addi_tl(cpu_R[dc->op1], cpu_R[dc->op1], size);
1236 static inline void dec_prep_move_r(DisasContext *dc, int rs, int rd,
1237 int size, int s_ext, TCGv dst)
1239 if (s_ext)
1240 t_gen_sext(dst, cpu_R[rs], size);
1241 else
1242 t_gen_zext(dst, cpu_R[rs], size);
1245 /* Prepare T0 and T1 for a register alu operation.
1246 s_ext decides if the operand1 should be sign-extended or zero-extended when
1247 needed. */
1248 static void dec_prep_alu_r(DisasContext *dc, int rs, int rd,
1249 int size, int s_ext, TCGv dst, TCGv src)
1251 dec_prep_move_r(dc, rs, rd, size, s_ext, src);
1253 if (s_ext)
1254 t_gen_sext(dst, cpu_R[rd], size);
1255 else
1256 t_gen_zext(dst, cpu_R[rd], size);
1259 static int dec_prep_move_m(DisasContext *dc, int s_ext, int memsize,
1260 TCGv dst)
1262 unsigned int rs, rd;
1263 uint32_t imm;
1264 int is_imm;
1265 int insn_len = 2;
1267 rs = dc->op1;
1268 rd = dc->op2;
1269 is_imm = rs == 15 && dc->postinc;
1271 /* Load [$rs] onto T1. */
1272 if (is_imm) {
1273 insn_len = 2 + memsize;
1274 if (memsize == 1)
1275 insn_len++;
1277 if (memsize != 4) {
1278 if (s_ext) {
1279 if (memsize == 1)
1280 imm = ldsb_code(dc->pc + 2);
1281 else
1282 imm = ldsw_code(dc->pc + 2);
1283 } else {
1284 if (memsize == 1)
1285 imm = ldub_code(dc->pc + 2);
1286 else
1287 imm = lduw_code(dc->pc + 2);
1289 } else
1290 imm = ldl_code(dc->pc + 2);
1292 tcg_gen_movi_tl(dst, imm);
1293 dc->postinc = 0;
1294 } else {
1295 cris_flush_cc_state(dc);
1296 gen_load(dc, dst, cpu_R[rs], memsize, 0);
1297 if (s_ext)
1298 t_gen_sext(dst, dst, memsize);
1299 else
1300 t_gen_zext(dst, dst, memsize);
1302 return insn_len;
1305 /* Prepare T0 and T1 for a memory + alu operation.
1306 s_ext decides if the operand1 should be sign-extended or zero-extended when
1307 needed. */
1308 static int dec_prep_alu_m(DisasContext *dc, int s_ext, int memsize,
1309 TCGv dst, TCGv src)
1311 int insn_len;
1313 insn_len = dec_prep_move_m(dc, s_ext, memsize, src);
1314 tcg_gen_mov_tl(dst, cpu_R[dc->op2]);
1315 return insn_len;
1318 #if DISAS_CRIS
1319 static const char *cc_name(int cc)
1321 static const char *cc_names[16] = {
1322 "cc", "cs", "ne", "eq", "vc", "vs", "pl", "mi",
1323 "ls", "hi", "ge", "lt", "gt", "le", "a", "p"
1325 assert(cc < 16);
1326 return cc_names[cc];
1328 #endif
1330 /* Start of insn decoders. */
1332 static unsigned int dec_bccq(DisasContext *dc)
1334 int32_t offset;
1335 int sign;
1336 uint32_t cond = dc->op2;
1337 int tmp;
1339 offset = EXTRACT_FIELD (dc->ir, 1, 7);
1340 sign = EXTRACT_FIELD(dc->ir, 0, 0);
1342 offset *= 2;
1343 offset |= sign << 8;
1344 tmp = offset;
1345 offset = sign_extend(offset, 8);
1347 DIS(fprintf (logfile, "b%s %x\n", cc_name(cond), dc->pc + offset));
1349 /* op2 holds the condition-code. */
1350 cris_cc_mask(dc, 0);
1351 cris_prepare_cc_branch (dc, offset, cond);
1352 return 2;
1354 static unsigned int dec_addoq(DisasContext *dc)
1356 int32_t imm;
1358 dc->op1 = EXTRACT_FIELD(dc->ir, 0, 7);
1359 imm = sign_extend(dc->op1, 7);
1361 DIS(fprintf (logfile, "addoq %d, $r%u\n", imm, dc->op2));
1362 cris_cc_mask(dc, 0);
1363 /* Fetch register operand, */
1364 tcg_gen_addi_tl(cpu_R[R_ACR], cpu_R[dc->op2], imm);
1366 return 2;
1368 static unsigned int dec_addq(DisasContext *dc)
1370 DIS(fprintf (logfile, "addq %u, $r%u\n", dc->op1, dc->op2));
1372 dc->op1 = EXTRACT_FIELD(dc->ir, 0, 5);
1374 cris_cc_mask(dc, CC_MASK_NZVC);
1376 cris_alu(dc, CC_OP_ADD,
1377 cpu_R[dc->op2], cpu_R[dc->op2], tcg_const_tl(dc->op1), 4);
1378 return 2;
1380 static unsigned int dec_moveq(DisasContext *dc)
1382 uint32_t imm;
1384 dc->op1 = EXTRACT_FIELD(dc->ir, 0, 5);
1385 imm = sign_extend(dc->op1, 5);
1386 DIS(fprintf (logfile, "moveq %d, $r%u\n", imm, dc->op2));
1388 tcg_gen_mov_tl(cpu_R[dc->op2], tcg_const_tl(imm));
1389 return 2;
1391 static unsigned int dec_subq(DisasContext *dc)
1393 dc->op1 = EXTRACT_FIELD(dc->ir, 0, 5);
1395 DIS(fprintf (logfile, "subq %u, $r%u\n", dc->op1, dc->op2));
1397 cris_cc_mask(dc, CC_MASK_NZVC);
1398 cris_alu(dc, CC_OP_SUB,
1399 cpu_R[dc->op2], cpu_R[dc->op2], tcg_const_tl(dc->op1), 4);
1400 return 2;
1402 static unsigned int dec_cmpq(DisasContext *dc)
1404 uint32_t imm;
1405 dc->op1 = EXTRACT_FIELD(dc->ir, 0, 5);
1406 imm = sign_extend(dc->op1, 5);
1408 DIS(fprintf (logfile, "cmpq %d, $r%d\n", imm, dc->op2));
1409 cris_cc_mask(dc, CC_MASK_NZVC);
1411 cris_alu(dc, CC_OP_CMP,
1412 cpu_R[dc->op2], cpu_R[dc->op2], tcg_const_tl(imm), 4);
1413 return 2;
1415 static unsigned int dec_andq(DisasContext *dc)
1417 uint32_t imm;
1418 dc->op1 = EXTRACT_FIELD(dc->ir, 0, 5);
1419 imm = sign_extend(dc->op1, 5);
1421 DIS(fprintf (logfile, "andq %d, $r%d\n", imm, dc->op2));
1422 cris_cc_mask(dc, CC_MASK_NZ);
1424 cris_alu(dc, CC_OP_AND,
1425 cpu_R[dc->op2], cpu_R[dc->op2], tcg_const_tl(imm), 4);
1426 return 2;
1428 static unsigned int dec_orq(DisasContext *dc)
1430 uint32_t imm;
1431 dc->op1 = EXTRACT_FIELD(dc->ir, 0, 5);
1432 imm = sign_extend(dc->op1, 5);
1433 DIS(fprintf (logfile, "orq %d, $r%d\n", imm, dc->op2));
1434 cris_cc_mask(dc, CC_MASK_NZ);
1436 cris_alu(dc, CC_OP_OR,
1437 cpu_R[dc->op2], cpu_R[dc->op2], tcg_const_tl(imm), 4);
1438 return 2;
1440 static unsigned int dec_btstq(DisasContext *dc)
1442 dc->op1 = EXTRACT_FIELD(dc->ir, 0, 4);
1443 DIS(fprintf (logfile, "btstq %u, $r%d\n", dc->op1, dc->op2));
1445 cris_cc_mask(dc, CC_MASK_NZ);
1446 cris_evaluate_flags(dc);
1447 gen_helper_btst(cpu_PR[PR_CCS], cpu_R[dc->op2],
1448 tcg_const_tl(dc->op1), cpu_PR[PR_CCS]);
1449 cris_alu(dc, CC_OP_MOVE,
1450 cpu_R[dc->op2], cpu_R[dc->op2], cpu_R[dc->op2], 4);
1451 cris_update_cc_op(dc, CC_OP_FLAGS, 4);
1452 dc->flags_uptodate = 1;
1453 return 2;
1455 static unsigned int dec_asrq(DisasContext *dc)
1457 dc->op1 = EXTRACT_FIELD(dc->ir, 0, 4);
1458 DIS(fprintf (logfile, "asrq %u, $r%d\n", dc->op1, dc->op2));
1459 cris_cc_mask(dc, CC_MASK_NZ);
1461 tcg_gen_sari_tl(cpu_R[dc->op2], cpu_R[dc->op2], dc->op1);
1462 cris_alu(dc, CC_OP_MOVE,
1463 cpu_R[dc->op2],
1464 cpu_R[dc->op2], cpu_R[dc->op2], 4);
1465 return 2;
1467 static unsigned int dec_lslq(DisasContext *dc)
1469 dc->op1 = EXTRACT_FIELD(dc->ir, 0, 4);
1470 DIS(fprintf (logfile, "lslq %u, $r%d\n", dc->op1, dc->op2));
1472 cris_cc_mask(dc, CC_MASK_NZ);
1474 tcg_gen_shli_tl(cpu_R[dc->op2], cpu_R[dc->op2], dc->op1);
1476 cris_alu(dc, CC_OP_MOVE,
1477 cpu_R[dc->op2],
1478 cpu_R[dc->op2], cpu_R[dc->op2], 4);
1479 return 2;
1481 static unsigned int dec_lsrq(DisasContext *dc)
1483 dc->op1 = EXTRACT_FIELD(dc->ir, 0, 4);
1484 DIS(fprintf (logfile, "lsrq %u, $r%d\n", dc->op1, dc->op2));
1486 cris_cc_mask(dc, CC_MASK_NZ);
1488 tcg_gen_shri_tl(cpu_R[dc->op2], cpu_R[dc->op2], dc->op1);
1489 cris_alu(dc, CC_OP_MOVE,
1490 cpu_R[dc->op2],
1491 cpu_R[dc->op2], cpu_R[dc->op2], 4);
1492 return 2;
1495 static unsigned int dec_move_r(DisasContext *dc)
1497 int size = memsize_zz(dc);
1499 DIS(fprintf (logfile, "move.%c $r%u, $r%u\n",
1500 memsize_char(size), dc->op1, dc->op2));
1502 cris_cc_mask(dc, CC_MASK_NZ);
1503 if (size == 4) {
1504 dec_prep_move_r(dc, dc->op1, dc->op2, size, 0, cpu_R[dc->op2]);
1505 cris_cc_mask(dc, CC_MASK_NZ);
1506 cris_update_cc_op(dc, CC_OP_MOVE, 4);
1507 cris_update_cc_x(dc);
1508 cris_update_result(dc, cpu_R[dc->op2]);
1510 else {
1511 TCGv t0;
1513 t0 = tcg_temp_new();
1514 dec_prep_move_r(dc, dc->op1, dc->op2, size, 0, t0);
1515 cris_alu(dc, CC_OP_MOVE,
1516 cpu_R[dc->op2],
1517 cpu_R[dc->op2], t0, size);
1518 tcg_temp_free(t0);
1520 return 2;
1523 static unsigned int dec_scc_r(DisasContext *dc)
1525 int cond = dc->op2;
1527 DIS(fprintf (logfile, "s%s $r%u\n",
1528 cc_name(cond), dc->op1));
1530 if (cond != CC_A)
1532 int l1;
1534 gen_tst_cc (dc, cpu_R[dc->op1], cond);
1535 l1 = gen_new_label();
1536 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_R[dc->op1], 0, l1);
1537 tcg_gen_movi_tl(cpu_R[dc->op1], 1);
1538 gen_set_label(l1);
1540 else
1541 tcg_gen_movi_tl(cpu_R[dc->op1], 1);
1543 cris_cc_mask(dc, 0);
1544 return 2;
1547 static inline void cris_alu_alloc_temps(DisasContext *dc, int size, TCGv *t)
1549 if (size == 4) {
1550 t[0] = cpu_R[dc->op2];
1551 t[1] = cpu_R[dc->op1];
1552 } else {
1553 t[0] = tcg_temp_new();
1554 t[1] = tcg_temp_new();
1558 static inline void cris_alu_free_temps(DisasContext *dc, int size, TCGv *t)
1560 if (size != 4) {
1561 tcg_temp_free(t[0]);
1562 tcg_temp_free(t[1]);
1566 static unsigned int dec_and_r(DisasContext *dc)
1568 TCGv t[2];
1569 int size = memsize_zz(dc);
1571 DIS(fprintf (logfile, "and.%c $r%u, $r%u\n",
1572 memsize_char(size), dc->op1, dc->op2));
1574 cris_cc_mask(dc, CC_MASK_NZ);
1576 cris_alu_alloc_temps(dc, size, t);
1577 dec_prep_alu_r(dc, dc->op1, dc->op2, size, 0, t[0], t[1]);
1578 cris_alu(dc, CC_OP_AND, cpu_R[dc->op2], t[0], t[1], size);
1579 cris_alu_free_temps(dc, size, t);
1580 return 2;
1583 static unsigned int dec_lz_r(DisasContext *dc)
1585 TCGv t0;
1586 DIS(fprintf (logfile, "lz $r%u, $r%u\n",
1587 dc->op1, dc->op2));
1588 cris_cc_mask(dc, CC_MASK_NZ);
1589 t0 = tcg_temp_new();
1590 dec_prep_alu_r(dc, dc->op1, dc->op2, 4, 0, cpu_R[dc->op2], t0);
1591 cris_alu(dc, CC_OP_LZ, cpu_R[dc->op2], cpu_R[dc->op2], t0, 4);
1592 tcg_temp_free(t0);
1593 return 2;
1596 static unsigned int dec_lsl_r(DisasContext *dc)
1598 TCGv t[2];
1599 int size = memsize_zz(dc);
1601 DIS(fprintf (logfile, "lsl.%c $r%u, $r%u\n",
1602 memsize_char(size), dc->op1, dc->op2));
1604 cris_cc_mask(dc, CC_MASK_NZ);
1605 cris_alu_alloc_temps(dc, size, t);
1606 dec_prep_alu_r(dc, dc->op1, dc->op2, size, 0, t[0], t[1]);
1607 tcg_gen_andi_tl(t[1], t[1], 63);
1608 cris_alu(dc, CC_OP_LSL, cpu_R[dc->op2], t[0], t[1], size);
1609 cris_alu_alloc_temps(dc, size, t);
1610 return 2;
1613 static unsigned int dec_lsr_r(DisasContext *dc)
1615 TCGv t[2];
1616 int size = memsize_zz(dc);
1618 DIS(fprintf (logfile, "lsr.%c $r%u, $r%u\n",
1619 memsize_char(size), dc->op1, dc->op2));
1621 cris_cc_mask(dc, CC_MASK_NZ);
1622 cris_alu_alloc_temps(dc, size, t);
1623 dec_prep_alu_r(dc, dc->op1, dc->op2, size, 0, t[0], t[1]);
1624 tcg_gen_andi_tl(t[1], t[1], 63);
1625 cris_alu(dc, CC_OP_LSR, cpu_R[dc->op2], t[0], t[1], size);
1626 cris_alu_free_temps(dc, size, t);
1627 return 2;
1630 static unsigned int dec_asr_r(DisasContext *dc)
1632 TCGv t[2];
1633 int size = memsize_zz(dc);
1635 DIS(fprintf (logfile, "asr.%c $r%u, $r%u\n",
1636 memsize_char(size), dc->op1, dc->op2));
1638 cris_cc_mask(dc, CC_MASK_NZ);
1639 cris_alu_alloc_temps(dc, size, t);
1640 dec_prep_alu_r(dc, dc->op1, dc->op2, size, 1, t[0], t[1]);
1641 tcg_gen_andi_tl(t[1], t[1], 63);
1642 cris_alu(dc, CC_OP_ASR, cpu_R[dc->op2], t[0], t[1], size);
1643 cris_alu_free_temps(dc, size, t);
1644 return 2;
1647 static unsigned int dec_muls_r(DisasContext *dc)
1649 TCGv t[2];
1650 int size = memsize_zz(dc);
1652 DIS(fprintf (logfile, "muls.%c $r%u, $r%u\n",
1653 memsize_char(size), dc->op1, dc->op2));
1654 cris_cc_mask(dc, CC_MASK_NZV);
1655 cris_alu_alloc_temps(dc, size, t);
1656 dec_prep_alu_r(dc, dc->op1, dc->op2, size, 1, t[0], t[1]);
1658 cris_alu(dc, CC_OP_MULS, cpu_R[dc->op2], t[0], t[1], 4);
1659 cris_alu_free_temps(dc, size, t);
1660 return 2;
1663 static unsigned int dec_mulu_r(DisasContext *dc)
1665 TCGv t[2];
1666 int size = memsize_zz(dc);
1668 DIS(fprintf (logfile, "mulu.%c $r%u, $r%u\n",
1669 memsize_char(size), dc->op1, dc->op2));
1670 cris_cc_mask(dc, CC_MASK_NZV);
1671 cris_alu_alloc_temps(dc, size, t);
1672 dec_prep_alu_r(dc, dc->op1, dc->op2, size, 0, t[0], t[1]);
1674 cris_alu(dc, CC_OP_MULU, cpu_R[dc->op2], t[0], t[1], 4);
1675 cris_alu_alloc_temps(dc, size, t);
1676 return 2;
1680 static unsigned int dec_dstep_r(DisasContext *dc)
1682 DIS(fprintf (logfile, "dstep $r%u, $r%u\n", dc->op1, dc->op2));
1683 cris_cc_mask(dc, CC_MASK_NZ);
1684 cris_alu(dc, CC_OP_DSTEP,
1685 cpu_R[dc->op2], cpu_R[dc->op2], cpu_R[dc->op1], 4);
1686 return 2;
1689 static unsigned int dec_xor_r(DisasContext *dc)
1691 TCGv t[2];
1692 int size = memsize_zz(dc);
1693 DIS(fprintf (logfile, "xor.%c $r%u, $r%u\n",
1694 memsize_char(size), dc->op1, dc->op2));
1695 BUG_ON(size != 4); /* xor is dword. */
1696 cris_cc_mask(dc, CC_MASK_NZ);
1697 cris_alu_alloc_temps(dc, size, t);
1698 dec_prep_alu_r(dc, dc->op1, dc->op2, size, 0, t[0], t[1]);
1700 cris_alu(dc, CC_OP_XOR, cpu_R[dc->op2], t[0], t[1], 4);
1701 cris_alu_free_temps(dc, size, t);
1702 return 2;
1705 static unsigned int dec_bound_r(DisasContext *dc)
1707 TCGv l0;
1708 int size = memsize_zz(dc);
1709 DIS(fprintf (logfile, "bound.%c $r%u, $r%u\n",
1710 memsize_char(size), dc->op1, dc->op2));
1711 cris_cc_mask(dc, CC_MASK_NZ);
1712 l0 = tcg_temp_local_new();
1713 dec_prep_move_r(dc, dc->op1, dc->op2, size, 0, l0);
1714 cris_alu(dc, CC_OP_BOUND, cpu_R[dc->op2], cpu_R[dc->op2], l0, 4);
1715 tcg_temp_free(l0);
1716 return 2;
1719 static unsigned int dec_cmp_r(DisasContext *dc)
1721 TCGv t[2];
1722 int size = memsize_zz(dc);
1723 DIS(fprintf (logfile, "cmp.%c $r%u, $r%u\n",
1724 memsize_char(size), dc->op1, dc->op2));
1725 cris_cc_mask(dc, CC_MASK_NZVC);
1726 cris_alu_alloc_temps(dc, size, t);
1727 dec_prep_alu_r(dc, dc->op1, dc->op2, size, 0, t[0], t[1]);
1729 cris_alu(dc, CC_OP_CMP, cpu_R[dc->op2], t[0], t[1], size);
1730 cris_alu_free_temps(dc, size, t);
1731 return 2;
1734 static unsigned int dec_abs_r(DisasContext *dc)
1736 TCGv t0;
1738 DIS(fprintf (logfile, "abs $r%u, $r%u\n",
1739 dc->op1, dc->op2));
1740 cris_cc_mask(dc, CC_MASK_NZ);
1742 t0 = tcg_temp_new();
1743 tcg_gen_sari_tl(t0, cpu_R[dc->op1], 31);
1744 tcg_gen_xor_tl(cpu_R[dc->op2], cpu_R[dc->op1], t0);
1745 tcg_gen_sub_tl(cpu_R[dc->op2], cpu_R[dc->op2], t0);
1746 tcg_temp_free(t0);
1748 cris_alu(dc, CC_OP_MOVE,
1749 cpu_R[dc->op2], cpu_R[dc->op2], cpu_R[dc->op2], 4);
1750 return 2;
1753 static unsigned int dec_add_r(DisasContext *dc)
1755 TCGv t[2];
1756 int size = memsize_zz(dc);
1757 DIS(fprintf (logfile, "add.%c $r%u, $r%u\n",
1758 memsize_char(size), dc->op1, dc->op2));
1759 cris_cc_mask(dc, CC_MASK_NZVC);
1760 cris_alu_alloc_temps(dc, size, t);
1761 dec_prep_alu_r(dc, dc->op1, dc->op2, size, 0, t[0], t[1]);
1763 cris_alu(dc, CC_OP_ADD, cpu_R[dc->op2], t[0], t[1], size);
1764 cris_alu_free_temps(dc, size, t);
1765 return 2;
1768 static unsigned int dec_addc_r(DisasContext *dc)
1770 DIS(fprintf (logfile, "addc $r%u, $r%u\n",
1771 dc->op1, dc->op2));
1772 cris_evaluate_flags(dc);
1773 /* Set for this insn. */
1774 dc->flagx_known = 1;
1775 dc->flags_x = X_FLAG;
1777 cris_cc_mask(dc, CC_MASK_NZVC);
1778 cris_alu(dc, CC_OP_ADDC,
1779 cpu_R[dc->op2], cpu_R[dc->op2], cpu_R[dc->op1], 4);
1780 return 2;
1783 static unsigned int dec_mcp_r(DisasContext *dc)
1785 DIS(fprintf (logfile, "mcp $p%u, $r%u\n",
1786 dc->op2, dc->op1));
1787 cris_evaluate_flags(dc);
1788 cris_cc_mask(dc, CC_MASK_RNZV);
1789 cris_alu(dc, CC_OP_MCP,
1790 cpu_R[dc->op1], cpu_R[dc->op1], cpu_PR[dc->op2], 4);
1791 return 2;
1794 #if DISAS_CRIS
1795 static char * swapmode_name(int mode, char *modename) {
1796 int i = 0;
1797 if (mode & 8)
1798 modename[i++] = 'n';
1799 if (mode & 4)
1800 modename[i++] = 'w';
1801 if (mode & 2)
1802 modename[i++] = 'b';
1803 if (mode & 1)
1804 modename[i++] = 'r';
1805 modename[i++] = 0;
1806 return modename;
1808 #endif
1810 static unsigned int dec_swap_r(DisasContext *dc)
1812 TCGv t0;
1813 #if DISAS_CRIS
1814 char modename[4];
1815 #endif
1816 DIS(fprintf (logfile, "swap%s $r%u\n",
1817 swapmode_name(dc->op2, modename), dc->op1));
1819 cris_cc_mask(dc, CC_MASK_NZ);
1820 t0 = tcg_temp_new();
1821 t_gen_mov_TN_reg(t0, dc->op1);
1822 if (dc->op2 & 8)
1823 tcg_gen_not_tl(t0, t0);
1824 if (dc->op2 & 4)
1825 t_gen_swapw(t0, t0);
1826 if (dc->op2 & 2)
1827 t_gen_swapb(t0, t0);
1828 if (dc->op2 & 1)
1829 t_gen_swapr(t0, t0);
1830 cris_alu(dc, CC_OP_MOVE,
1831 cpu_R[dc->op1], cpu_R[dc->op1], t0, 4);
1832 tcg_temp_free(t0);
1833 return 2;
1836 static unsigned int dec_or_r(DisasContext *dc)
1838 TCGv t[2];
1839 int size = memsize_zz(dc);
1840 DIS(fprintf (logfile, "or.%c $r%u, $r%u\n",
1841 memsize_char(size), dc->op1, dc->op2));
1842 cris_cc_mask(dc, CC_MASK_NZ);
1843 cris_alu_alloc_temps(dc, size, t);
1844 dec_prep_alu_r(dc, dc->op1, dc->op2, size, 0, t[0], t[1]);
1845 cris_alu(dc, CC_OP_OR, cpu_R[dc->op2], t[0], t[1], size);
1846 cris_alu_free_temps(dc, size, t);
1847 return 2;
1850 static unsigned int dec_addi_r(DisasContext *dc)
1852 TCGv t0;
1853 DIS(fprintf (logfile, "addi.%c $r%u, $r%u\n",
1854 memsize_char(memsize_zz(dc)), dc->op2, dc->op1));
1855 cris_cc_mask(dc, 0);
1856 t0 = tcg_temp_new();
1857 tcg_gen_shl_tl(t0, cpu_R[dc->op2], tcg_const_tl(dc->zzsize));
1858 tcg_gen_add_tl(cpu_R[dc->op1], cpu_R[dc->op1], t0);
1859 tcg_temp_free(t0);
1860 return 2;
1863 static unsigned int dec_addi_acr(DisasContext *dc)
1865 TCGv t0;
1866 DIS(fprintf (logfile, "addi.%c $r%u, $r%u, $acr\n",
1867 memsize_char(memsize_zz(dc)), dc->op2, dc->op1));
1868 cris_cc_mask(dc, 0);
1869 t0 = tcg_temp_new();
1870 tcg_gen_shl_tl(t0, cpu_R[dc->op2], tcg_const_tl(dc->zzsize));
1871 tcg_gen_add_tl(cpu_R[R_ACR], cpu_R[dc->op1], t0);
1872 tcg_temp_free(t0);
1873 return 2;
1876 static unsigned int dec_neg_r(DisasContext *dc)
1878 TCGv t[2];
1879 int size = memsize_zz(dc);
1880 DIS(fprintf (logfile, "neg.%c $r%u, $r%u\n",
1881 memsize_char(size), dc->op1, dc->op2));
1882 cris_cc_mask(dc, CC_MASK_NZVC);
1883 cris_alu_alloc_temps(dc, size, t);
1884 dec_prep_alu_r(dc, dc->op1, dc->op2, size, 0, t[0], t[1]);
1886 cris_alu(dc, CC_OP_NEG, cpu_R[dc->op2], t[0], t[1], size);
1887 cris_alu_free_temps(dc, size, t);
1888 return 2;
1891 static unsigned int dec_btst_r(DisasContext *dc)
1893 DIS(fprintf (logfile, "btst $r%u, $r%u\n",
1894 dc->op1, dc->op2));
1895 cris_cc_mask(dc, CC_MASK_NZ);
1896 cris_evaluate_flags(dc);
1897 gen_helper_btst(cpu_PR[PR_CCS], cpu_R[dc->op2],
1898 cpu_R[dc->op1], cpu_PR[PR_CCS]);
1899 cris_alu(dc, CC_OP_MOVE, cpu_R[dc->op2],
1900 cpu_R[dc->op2], cpu_R[dc->op2], 4);
1901 cris_update_cc_op(dc, CC_OP_FLAGS, 4);
1902 dc->flags_uptodate = 1;
1903 return 2;
1906 static unsigned int dec_sub_r(DisasContext *dc)
1908 TCGv t[2];
1909 int size = memsize_zz(dc);
1910 DIS(fprintf (logfile, "sub.%c $r%u, $r%u\n",
1911 memsize_char(size), dc->op1, dc->op2));
1912 cris_cc_mask(dc, CC_MASK_NZVC);
1913 cris_alu_alloc_temps(dc, size, t);
1914 dec_prep_alu_r(dc, dc->op1, dc->op2, size, 0, t[0], t[1]);
1915 cris_alu(dc, CC_OP_SUB, cpu_R[dc->op2], t[0], t[1], size);
1916 cris_alu_free_temps(dc, size, t);
1917 return 2;
1920 /* Zero extension. From size to dword. */
1921 static unsigned int dec_movu_r(DisasContext *dc)
1923 TCGv t0;
1924 int size = memsize_z(dc);
1925 DIS(fprintf (logfile, "movu.%c $r%u, $r%u\n",
1926 memsize_char(size),
1927 dc->op1, dc->op2));
1929 cris_cc_mask(dc, CC_MASK_NZ);
1930 t0 = tcg_temp_new();
1931 dec_prep_move_r(dc, dc->op1, dc->op2, size, 0, t0);
1932 cris_alu(dc, CC_OP_MOVE, cpu_R[dc->op2], cpu_R[dc->op2], t0, 4);
1933 tcg_temp_free(t0);
1934 return 2;
1937 /* Sign extension. From size to dword. */
1938 static unsigned int dec_movs_r(DisasContext *dc)
1940 TCGv t0;
1941 int size = memsize_z(dc);
1942 DIS(fprintf (logfile, "movs.%c $r%u, $r%u\n",
1943 memsize_char(size),
1944 dc->op1, dc->op2));
1946 cris_cc_mask(dc, CC_MASK_NZ);
1947 t0 = tcg_temp_new();
1948 /* Size can only be qi or hi. */
1949 t_gen_sext(t0, cpu_R[dc->op1], size);
1950 cris_alu(dc, CC_OP_MOVE,
1951 cpu_R[dc->op2], cpu_R[dc->op1], t0, 4);
1952 tcg_temp_free(t0);
1953 return 2;
1956 /* zero extension. From size to dword. */
1957 static unsigned int dec_addu_r(DisasContext *dc)
1959 TCGv t0;
1960 int size = memsize_z(dc);
1961 DIS(fprintf (logfile, "addu.%c $r%u, $r%u\n",
1962 memsize_char(size),
1963 dc->op1, dc->op2));
1965 cris_cc_mask(dc, CC_MASK_NZVC);
1966 t0 = tcg_temp_new();
1967 /* Size can only be qi or hi. */
1968 t_gen_zext(t0, cpu_R[dc->op1], size);
1969 cris_alu(dc, CC_OP_ADD,
1970 cpu_R[dc->op2], cpu_R[dc->op2], t0, 4);
1971 tcg_temp_free(t0);
1972 return 2;
1975 /* Sign extension. From size to dword. */
1976 static unsigned int dec_adds_r(DisasContext *dc)
1978 TCGv t0;
1979 int size = memsize_z(dc);
1980 DIS(fprintf (logfile, "adds.%c $r%u, $r%u\n",
1981 memsize_char(size),
1982 dc->op1, dc->op2));
1984 cris_cc_mask(dc, CC_MASK_NZVC);
1985 t0 = tcg_temp_new();
1986 /* Size can only be qi or hi. */
1987 t_gen_sext(t0, cpu_R[dc->op1], size);
1988 cris_alu(dc, CC_OP_ADD,
1989 cpu_R[dc->op2], cpu_R[dc->op2], t0, 4);
1990 tcg_temp_free(t0);
1991 return 2;
1994 /* Zero extension. From size to dword. */
1995 static unsigned int dec_subu_r(DisasContext *dc)
1997 TCGv t0;
1998 int size = memsize_z(dc);
1999 DIS(fprintf (logfile, "subu.%c $r%u, $r%u\n",
2000 memsize_char(size),
2001 dc->op1, dc->op2));
2003 cris_cc_mask(dc, CC_MASK_NZVC);
2004 t0 = tcg_temp_new();
2005 /* Size can only be qi or hi. */
2006 t_gen_zext(t0, cpu_R[dc->op1], size);
2007 cris_alu(dc, CC_OP_SUB,
2008 cpu_R[dc->op2], cpu_R[dc->op2], t0, 4);
2009 tcg_temp_free(t0);
2010 return 2;
2013 /* Sign extension. From size to dword. */
2014 static unsigned int dec_subs_r(DisasContext *dc)
2016 TCGv t0;
2017 int size = memsize_z(dc);
2018 DIS(fprintf (logfile, "subs.%c $r%u, $r%u\n",
2019 memsize_char(size),
2020 dc->op1, dc->op2));
2022 cris_cc_mask(dc, CC_MASK_NZVC);
2023 t0 = tcg_temp_new();
2024 /* Size can only be qi or hi. */
2025 t_gen_sext(t0, cpu_R[dc->op1], size);
2026 cris_alu(dc, CC_OP_SUB,
2027 cpu_R[dc->op2], cpu_R[dc->op2], t0, 4);
2028 tcg_temp_free(t0);
2029 return 2;
2032 static unsigned int dec_setclrf(DisasContext *dc)
2034 uint32_t flags;
2035 int set = (~dc->opcode >> 2) & 1;
2038 flags = (EXTRACT_FIELD(dc->ir, 12, 15) << 4)
2039 | EXTRACT_FIELD(dc->ir, 0, 3);
2040 if (set && flags == 0) {
2041 DIS(fprintf (logfile, "nop\n"));
2042 return 2;
2043 } else if (!set && (flags & 0x20)) {
2044 DIS(fprintf (logfile, "di\n"));
2046 else {
2047 DIS(fprintf (logfile, "%sf %x\n",
2048 set ? "set" : "clr",
2049 flags));
2052 /* User space is not allowed to touch these. Silently ignore. */
2053 if (dc->tb_flags & U_FLAG) {
2054 flags &= ~(S_FLAG | I_FLAG | U_FLAG);
2057 if (flags & X_FLAG) {
2058 dc->flagx_known = 1;
2059 if (set)
2060 dc->flags_x = X_FLAG;
2061 else
2062 dc->flags_x = 0;
2065 /* Break the TB if the P flag changes. */
2066 if (flags & P_FLAG) {
2067 if ((set && !(dc->tb_flags & P_FLAG))
2068 || (!set && (dc->tb_flags & P_FLAG))) {
2069 tcg_gen_movi_tl(env_pc, dc->pc + 2);
2070 dc->is_jmp = DISAS_UPDATE;
2071 dc->cpustate_changed = 1;
2074 if (flags & S_FLAG) {
2075 dc->cpustate_changed = 1;
2079 /* Simply decode the flags. */
2080 cris_evaluate_flags (dc);
2081 cris_update_cc_op(dc, CC_OP_FLAGS, 4);
2082 cris_update_cc_x(dc);
2083 tcg_gen_movi_tl(cc_op, dc->cc_op);
2085 if (set) {
2086 if (!(dc->tb_flags & U_FLAG) && (flags & U_FLAG)) {
2087 /* Enter user mode. */
2088 t_gen_mov_env_TN(ksp, cpu_R[R_SP]);
2089 tcg_gen_mov_tl(cpu_R[R_SP], cpu_PR[PR_USP]);
2090 dc->cpustate_changed = 1;
2092 tcg_gen_ori_tl(cpu_PR[PR_CCS], cpu_PR[PR_CCS], flags);
2094 else
2095 tcg_gen_andi_tl(cpu_PR[PR_CCS], cpu_PR[PR_CCS], ~flags);
2097 dc->flags_uptodate = 1;
2098 dc->clear_x = 0;
2099 return 2;
2102 static unsigned int dec_move_rs(DisasContext *dc)
2104 DIS(fprintf (logfile, "move $r%u, $s%u\n", dc->op1, dc->op2));
2105 cris_cc_mask(dc, 0);
2106 gen_helper_movl_sreg_reg(tcg_const_tl(dc->op2), tcg_const_tl(dc->op1));
2107 return 2;
2109 static unsigned int dec_move_sr(DisasContext *dc)
2111 DIS(fprintf (logfile, "move $s%u, $r%u\n", dc->op2, dc->op1));
2112 cris_cc_mask(dc, 0);
2113 gen_helper_movl_reg_sreg(tcg_const_tl(dc->op1), tcg_const_tl(dc->op2));
2114 return 2;
2117 static unsigned int dec_move_rp(DisasContext *dc)
2119 TCGv t[2];
2120 DIS(fprintf (logfile, "move $r%u, $p%u\n", dc->op1, dc->op2));
2121 cris_cc_mask(dc, 0);
2123 t[0] = tcg_temp_new();
2124 if (dc->op2 == PR_CCS) {
2125 cris_evaluate_flags(dc);
2126 t_gen_mov_TN_reg(t[0], dc->op1);
2127 if (dc->tb_flags & U_FLAG) {
2128 t[1] = tcg_temp_new();
2129 /* User space is not allowed to touch all flags. */
2130 tcg_gen_andi_tl(t[0], t[0], 0x39f);
2131 tcg_gen_andi_tl(t[1], cpu_PR[PR_CCS], ~0x39f);
2132 tcg_gen_or_tl(t[0], t[1], t[0]);
2133 tcg_temp_free(t[1]);
2136 else
2137 t_gen_mov_TN_reg(t[0], dc->op1);
2139 t_gen_mov_preg_TN(dc, dc->op2, t[0]);
2140 if (dc->op2 == PR_CCS) {
2141 cris_update_cc_op(dc, CC_OP_FLAGS, 4);
2142 dc->flags_uptodate = 1;
2144 tcg_temp_free(t[0]);
2145 return 2;
2147 static unsigned int dec_move_pr(DisasContext *dc)
2149 TCGv t0;
2150 DIS(fprintf (logfile, "move $p%u, $r%u\n", dc->op1, dc->op2));
2151 cris_cc_mask(dc, 0);
2153 if (dc->op2 == PR_CCS)
2154 cris_evaluate_flags(dc);
2156 t0 = tcg_temp_new();
2157 t_gen_mov_TN_preg(t0, dc->op2);
2158 cris_alu(dc, CC_OP_MOVE,
2159 cpu_R[dc->op1], cpu_R[dc->op1], t0, preg_sizes[dc->op2]);
2160 tcg_temp_free(t0);
2161 return 2;
2164 static unsigned int dec_move_mr(DisasContext *dc)
2166 int memsize = memsize_zz(dc);
2167 int insn_len;
2168 DIS(fprintf (logfile, "move.%c [$r%u%s, $r%u\n",
2169 memsize_char(memsize),
2170 dc->op1, dc->postinc ? "+]" : "]",
2171 dc->op2));
2173 if (memsize == 4) {
2174 insn_len = dec_prep_move_m(dc, 0, 4, cpu_R[dc->op2]);
2175 cris_cc_mask(dc, CC_MASK_NZ);
2176 cris_update_cc_op(dc, CC_OP_MOVE, 4);
2177 cris_update_cc_x(dc);
2178 cris_update_result(dc, cpu_R[dc->op2]);
2180 else {
2181 TCGv t0;
2183 t0 = tcg_temp_new();
2184 insn_len = dec_prep_move_m(dc, 0, memsize, t0);
2185 cris_cc_mask(dc, CC_MASK_NZ);
2186 cris_alu(dc, CC_OP_MOVE,
2187 cpu_R[dc->op2], cpu_R[dc->op2], t0, memsize);
2188 tcg_temp_free(t0);
2190 do_postinc(dc, memsize);
2191 return insn_len;
2194 static inline void cris_alu_m_alloc_temps(TCGv *t)
2196 t[0] = tcg_temp_new();
2197 t[1] = tcg_temp_new();
2200 static inline void cris_alu_m_free_temps(TCGv *t)
2202 tcg_temp_free(t[0]);
2203 tcg_temp_free(t[1]);
2206 static unsigned int dec_movs_m(DisasContext *dc)
2208 TCGv t[2];
2209 int memsize = memsize_z(dc);
2210 int insn_len;
2211 DIS(fprintf (logfile, "movs.%c [$r%u%s, $r%u\n",
2212 memsize_char(memsize),
2213 dc->op1, dc->postinc ? "+]" : "]",
2214 dc->op2));
2216 cris_alu_m_alloc_temps(t);
2217 /* sign extend. */
2218 insn_len = dec_prep_alu_m(dc, 1, memsize, t[0], t[1]);
2219 cris_cc_mask(dc, CC_MASK_NZ);
2220 cris_alu(dc, CC_OP_MOVE,
2221 cpu_R[dc->op2], cpu_R[dc->op2], t[1], 4);
2222 do_postinc(dc, memsize);
2223 cris_alu_m_free_temps(t);
2224 return insn_len;
2227 static unsigned int dec_addu_m(DisasContext *dc)
2229 TCGv t[2];
2230 int memsize = memsize_z(dc);
2231 int insn_len;
2232 DIS(fprintf (logfile, "addu.%c [$r%u%s, $r%u\n",
2233 memsize_char(memsize),
2234 dc->op1, dc->postinc ? "+]" : "]",
2235 dc->op2));
2237 cris_alu_m_alloc_temps(t);
2238 /* sign extend. */
2239 insn_len = dec_prep_alu_m(dc, 0, memsize, t[0], t[1]);
2240 cris_cc_mask(dc, CC_MASK_NZVC);
2241 cris_alu(dc, CC_OP_ADD,
2242 cpu_R[dc->op2], cpu_R[dc->op2], t[1], 4);
2243 do_postinc(dc, memsize);
2244 cris_alu_m_free_temps(t);
2245 return insn_len;
2248 static unsigned int dec_adds_m(DisasContext *dc)
2250 TCGv t[2];
2251 int memsize = memsize_z(dc);
2252 int insn_len;
2253 DIS(fprintf (logfile, "adds.%c [$r%u%s, $r%u\n",
2254 memsize_char(memsize),
2255 dc->op1, dc->postinc ? "+]" : "]",
2256 dc->op2));
2258 cris_alu_m_alloc_temps(t);
2259 /* sign extend. */
2260 insn_len = dec_prep_alu_m(dc, 1, memsize, t[0], t[1]);
2261 cris_cc_mask(dc, CC_MASK_NZVC);
2262 cris_alu(dc, CC_OP_ADD, cpu_R[dc->op2], cpu_R[dc->op2], t[1], 4);
2263 do_postinc(dc, memsize);
2264 cris_alu_m_free_temps(t);
2265 return insn_len;
2268 static unsigned int dec_subu_m(DisasContext *dc)
2270 TCGv t[2];
2271 int memsize = memsize_z(dc);
2272 int insn_len;
2273 DIS(fprintf (logfile, "subu.%c [$r%u%s, $r%u\n",
2274 memsize_char(memsize),
2275 dc->op1, dc->postinc ? "+]" : "]",
2276 dc->op2));
2278 cris_alu_m_alloc_temps(t);
2279 /* sign extend. */
2280 insn_len = dec_prep_alu_m(dc, 0, memsize, t[0], t[1]);
2281 cris_cc_mask(dc, CC_MASK_NZVC);
2282 cris_alu(dc, CC_OP_SUB, cpu_R[dc->op2], cpu_R[dc->op2], t[1], 4);
2283 do_postinc(dc, memsize);
2284 cris_alu_m_free_temps(t);
2285 return insn_len;
2288 static unsigned int dec_subs_m(DisasContext *dc)
2290 TCGv t[2];
2291 int memsize = memsize_z(dc);
2292 int insn_len;
2293 DIS(fprintf (logfile, "subs.%c [$r%u%s, $r%u\n",
2294 memsize_char(memsize),
2295 dc->op1, dc->postinc ? "+]" : "]",
2296 dc->op2));
2298 cris_alu_m_alloc_temps(t);
2299 /* sign extend. */
2300 insn_len = dec_prep_alu_m(dc, 1, memsize, t[0], t[1]);
2301 cris_cc_mask(dc, CC_MASK_NZVC);
2302 cris_alu(dc, CC_OP_SUB, cpu_R[dc->op2], cpu_R[dc->op2], t[1], 4);
2303 do_postinc(dc, memsize);
2304 cris_alu_m_free_temps(t);
2305 return insn_len;
2308 static unsigned int dec_movu_m(DisasContext *dc)
2310 TCGv t[2];
2311 int memsize = memsize_z(dc);
2312 int insn_len;
2314 DIS(fprintf (logfile, "movu.%c [$r%u%s, $r%u\n",
2315 memsize_char(memsize),
2316 dc->op1, dc->postinc ? "+]" : "]",
2317 dc->op2));
2319 cris_alu_m_alloc_temps(t);
2320 insn_len = dec_prep_alu_m(dc, 0, memsize, t[0], t[1]);
2321 cris_cc_mask(dc, CC_MASK_NZ);
2322 cris_alu(dc, CC_OP_MOVE, cpu_R[dc->op2], cpu_R[dc->op2], t[1], 4);
2323 do_postinc(dc, memsize);
2324 cris_alu_m_free_temps(t);
2325 return insn_len;
2328 static unsigned int dec_cmpu_m(DisasContext *dc)
2330 TCGv t[2];
2331 int memsize = memsize_z(dc);
2332 int insn_len;
2333 DIS(fprintf (logfile, "cmpu.%c [$r%u%s, $r%u\n",
2334 memsize_char(memsize),
2335 dc->op1, dc->postinc ? "+]" : "]",
2336 dc->op2));
2338 cris_alu_m_alloc_temps(t);
2339 insn_len = dec_prep_alu_m(dc, 0, memsize, t[0], t[1]);
2340 cris_cc_mask(dc, CC_MASK_NZVC);
2341 cris_alu(dc, CC_OP_CMP, cpu_R[dc->op2], cpu_R[dc->op2], t[1], 4);
2342 do_postinc(dc, memsize);
2343 cris_alu_m_free_temps(t);
2344 return insn_len;
2347 static unsigned int dec_cmps_m(DisasContext *dc)
2349 TCGv t[2];
2350 int memsize = memsize_z(dc);
2351 int insn_len;
2352 DIS(fprintf (logfile, "cmps.%c [$r%u%s, $r%u\n",
2353 memsize_char(memsize),
2354 dc->op1, dc->postinc ? "+]" : "]",
2355 dc->op2));
2357 cris_alu_m_alloc_temps(t);
2358 insn_len = dec_prep_alu_m(dc, 1, memsize, t[0], t[1]);
2359 cris_cc_mask(dc, CC_MASK_NZVC);
2360 cris_alu(dc, CC_OP_CMP,
2361 cpu_R[dc->op2], cpu_R[dc->op2], t[1],
2362 memsize_zz(dc));
2363 do_postinc(dc, memsize);
2364 cris_alu_m_free_temps(t);
2365 return insn_len;
2368 static unsigned int dec_cmp_m(DisasContext *dc)
2370 TCGv t[2];
2371 int memsize = memsize_zz(dc);
2372 int insn_len;
2373 DIS(fprintf (logfile, "cmp.%c [$r%u%s, $r%u\n",
2374 memsize_char(memsize),
2375 dc->op1, dc->postinc ? "+]" : "]",
2376 dc->op2));
2378 cris_alu_m_alloc_temps(t);
2379 insn_len = dec_prep_alu_m(dc, 0, memsize, t[0], t[1]);
2380 cris_cc_mask(dc, CC_MASK_NZVC);
2381 cris_alu(dc, CC_OP_CMP,
2382 cpu_R[dc->op2], cpu_R[dc->op2], t[1],
2383 memsize_zz(dc));
2384 do_postinc(dc, memsize);
2385 cris_alu_m_free_temps(t);
2386 return insn_len;
2389 static unsigned int dec_test_m(DisasContext *dc)
2391 TCGv t[2];
2392 int memsize = memsize_zz(dc);
2393 int insn_len;
2394 DIS(fprintf (logfile, "test.%d [$r%u%s] op2=%x\n",
2395 memsize_char(memsize),
2396 dc->op1, dc->postinc ? "+]" : "]",
2397 dc->op2));
2399 cris_evaluate_flags(dc);
2401 cris_alu_m_alloc_temps(t);
2402 insn_len = dec_prep_alu_m(dc, 0, memsize, t[0], t[1]);
2403 cris_cc_mask(dc, CC_MASK_NZ);
2404 tcg_gen_andi_tl(cpu_PR[PR_CCS], cpu_PR[PR_CCS], ~3);
2406 cris_alu(dc, CC_OP_CMP,
2407 cpu_R[dc->op2], t[1], tcg_const_tl(0), memsize_zz(dc));
2408 do_postinc(dc, memsize);
2409 cris_alu_m_free_temps(t);
2410 return insn_len;
2413 static unsigned int dec_and_m(DisasContext *dc)
2415 TCGv t[2];
2416 int memsize = memsize_zz(dc);
2417 int insn_len;
2418 DIS(fprintf (logfile, "and.%d [$r%u%s, $r%u\n",
2419 memsize_char(memsize),
2420 dc->op1, dc->postinc ? "+]" : "]",
2421 dc->op2));
2423 cris_alu_m_alloc_temps(t);
2424 insn_len = dec_prep_alu_m(dc, 0, memsize, t[0], t[1]);
2425 cris_cc_mask(dc, CC_MASK_NZ);
2426 cris_alu(dc, CC_OP_AND, cpu_R[dc->op2], t[0], t[1], memsize_zz(dc));
2427 do_postinc(dc, memsize);
2428 cris_alu_m_free_temps(t);
2429 return insn_len;
2432 static unsigned int dec_add_m(DisasContext *dc)
2434 TCGv t[2];
2435 int memsize = memsize_zz(dc);
2436 int insn_len;
2437 DIS(fprintf (logfile, "add.%d [$r%u%s, $r%u\n",
2438 memsize_char(memsize),
2439 dc->op1, dc->postinc ? "+]" : "]",
2440 dc->op2));
2442 cris_alu_m_alloc_temps(t);
2443 insn_len = dec_prep_alu_m(dc, 0, memsize, t[0], t[1]);
2444 cris_cc_mask(dc, CC_MASK_NZVC);
2445 cris_alu(dc, CC_OP_ADD,
2446 cpu_R[dc->op2], t[0], t[1], memsize_zz(dc));
2447 do_postinc(dc, memsize);
2448 cris_alu_m_free_temps(t);
2449 return insn_len;
2452 static unsigned int dec_addo_m(DisasContext *dc)
2454 TCGv t[2];
2455 int memsize = memsize_zz(dc);
2456 int insn_len;
2457 DIS(fprintf (logfile, "add.%d [$r%u%s, $r%u\n",
2458 memsize_char(memsize),
2459 dc->op1, dc->postinc ? "+]" : "]",
2460 dc->op2));
2462 cris_alu_m_alloc_temps(t);
2463 insn_len = dec_prep_alu_m(dc, 1, memsize, t[0], t[1]);
2464 cris_cc_mask(dc, 0);
2465 cris_alu(dc, CC_OP_ADD, cpu_R[R_ACR], t[0], t[1], 4);
2466 do_postinc(dc, memsize);
2467 cris_alu_m_free_temps(t);
2468 return insn_len;
2471 static unsigned int dec_bound_m(DisasContext *dc)
2473 TCGv l[2];
2474 int memsize = memsize_zz(dc);
2475 int insn_len;
2476 DIS(fprintf (logfile, "bound.%d [$r%u%s, $r%u\n",
2477 memsize_char(memsize),
2478 dc->op1, dc->postinc ? "+]" : "]",
2479 dc->op2));
2481 l[0] = tcg_temp_local_new();
2482 l[1] = tcg_temp_local_new();
2483 insn_len = dec_prep_alu_m(dc, 0, memsize, l[0], l[1]);
2484 cris_cc_mask(dc, CC_MASK_NZ);
2485 cris_alu(dc, CC_OP_BOUND, cpu_R[dc->op2], l[0], l[1], 4);
2486 do_postinc(dc, memsize);
2487 tcg_temp_free(l[0]);
2488 tcg_temp_free(l[1]);
2489 return insn_len;
2492 static unsigned int dec_addc_mr(DisasContext *dc)
2494 TCGv t[2];
2495 int insn_len = 2;
2496 DIS(fprintf (logfile, "addc [$r%u%s, $r%u\n",
2497 dc->op1, dc->postinc ? "+]" : "]",
2498 dc->op2));
2500 cris_evaluate_flags(dc);
2502 /* Set for this insn. */
2503 dc->flagx_known = 1;
2504 dc->flags_x = X_FLAG;
2506 cris_alu_m_alloc_temps(t);
2507 insn_len = dec_prep_alu_m(dc, 0, 4, t[0], t[1]);
2508 cris_cc_mask(dc, CC_MASK_NZVC);
2509 cris_alu(dc, CC_OP_ADDC, cpu_R[dc->op2], t[0], t[1], 4);
2510 do_postinc(dc, 4);
2511 cris_alu_m_free_temps(t);
2512 return insn_len;
2515 static unsigned int dec_sub_m(DisasContext *dc)
2517 TCGv t[2];
2518 int memsize = memsize_zz(dc);
2519 int insn_len;
2520 DIS(fprintf (logfile, "sub.%c [$r%u%s, $r%u ir=%x zz=%x\n",
2521 memsize_char(memsize),
2522 dc->op1, dc->postinc ? "+]" : "]",
2523 dc->op2, dc->ir, dc->zzsize));
2525 cris_alu_m_alloc_temps(t);
2526 insn_len = dec_prep_alu_m(dc, 0, memsize, t[0], t[1]);
2527 cris_cc_mask(dc, CC_MASK_NZVC);
2528 cris_alu(dc, CC_OP_SUB, cpu_R[dc->op2], t[0], t[1], memsize);
2529 do_postinc(dc, memsize);
2530 cris_alu_m_free_temps(t);
2531 return insn_len;
2534 static unsigned int dec_or_m(DisasContext *dc)
2536 TCGv t[2];
2537 int memsize = memsize_zz(dc);
2538 int insn_len;
2539 DIS(fprintf (logfile, "or.%d [$r%u%s, $r%u pc=%x\n",
2540 memsize_char(memsize),
2541 dc->op1, dc->postinc ? "+]" : "]",
2542 dc->op2, dc->pc));
2544 cris_alu_m_alloc_temps(t);
2545 insn_len = dec_prep_alu_m(dc, 0, memsize, t[0], t[1]);
2546 cris_cc_mask(dc, CC_MASK_NZ);
2547 cris_alu(dc, CC_OP_OR,
2548 cpu_R[dc->op2], t[0], t[1], memsize_zz(dc));
2549 do_postinc(dc, memsize);
2550 cris_alu_m_free_temps(t);
2551 return insn_len;
2554 static unsigned int dec_move_mp(DisasContext *dc)
2556 TCGv t[2];
2557 int memsize = memsize_zz(dc);
2558 int insn_len = 2;
2560 DIS(fprintf (logfile, "move.%c [$r%u%s, $p%u\n",
2561 memsize_char(memsize),
2562 dc->op1,
2563 dc->postinc ? "+]" : "]",
2564 dc->op2));
2566 cris_alu_m_alloc_temps(t);
2567 insn_len = dec_prep_alu_m(dc, 0, memsize, t[0], t[1]);
2568 cris_cc_mask(dc, 0);
2569 if (dc->op2 == PR_CCS) {
2570 cris_evaluate_flags(dc);
2571 if (dc->tb_flags & U_FLAG) {
2572 /* User space is not allowed to touch all flags. */
2573 tcg_gen_andi_tl(t[1], t[1], 0x39f);
2574 tcg_gen_andi_tl(t[0], cpu_PR[PR_CCS], ~0x39f);
2575 tcg_gen_or_tl(t[1], t[0], t[1]);
2579 t_gen_mov_preg_TN(dc, dc->op2, t[1]);
2581 do_postinc(dc, memsize);
2582 cris_alu_m_free_temps(t);
2583 return insn_len;
2586 static unsigned int dec_move_pm(DisasContext *dc)
2588 TCGv t0;
2589 int memsize;
2591 memsize = preg_sizes[dc->op2];
2593 DIS(fprintf (logfile, "move.%c $p%u, [$r%u%s\n",
2594 memsize_char(memsize),
2595 dc->op2, dc->op1, dc->postinc ? "+]" : "]"));
2597 /* prepare store. Address in T0, value in T1. */
2598 if (dc->op2 == PR_CCS)
2599 cris_evaluate_flags(dc);
2600 t0 = tcg_temp_new();
2601 t_gen_mov_TN_preg(t0, dc->op2);
2602 cris_flush_cc_state(dc);
2603 gen_store(dc, cpu_R[dc->op1], t0, memsize);
2604 tcg_temp_free(t0);
2606 cris_cc_mask(dc, 0);
2607 if (dc->postinc)
2608 tcg_gen_addi_tl(cpu_R[dc->op1], cpu_R[dc->op1], memsize);
2609 return 2;
2612 static unsigned int dec_movem_mr(DisasContext *dc)
2614 TCGv_i64 tmp[16];
2615 TCGv tmp32;
2616 TCGv addr;
2617 int i;
2618 int nr = dc->op2 + 1;
2620 DIS(fprintf (logfile, "movem [$r%u%s, $r%u\n", dc->op1,
2621 dc->postinc ? "+]" : "]", dc->op2));
2623 addr = tcg_temp_new();
2624 /* There are probably better ways of doing this. */
2625 cris_flush_cc_state(dc);
2626 for (i = 0; i < (nr >> 1); i++) {
2627 tmp[i] = tcg_temp_new_i64();
2628 tcg_gen_addi_tl(addr, cpu_R[dc->op1], i * 8);
2629 gen_load64(dc, tmp[i], addr);
2631 if (nr & 1) {
2632 tmp32 = tcg_temp_new_i32();
2633 tcg_gen_addi_tl(addr, cpu_R[dc->op1], i * 8);
2634 gen_load(dc, tmp32, addr, 4, 0);
2636 tcg_temp_free(addr);
2638 for (i = 0; i < (nr >> 1); i++) {
2639 tcg_gen_trunc_i64_i32(cpu_R[i * 2], tmp[i]);
2640 tcg_gen_shri_i64(tmp[i], tmp[i], 32);
2641 tcg_gen_trunc_i64_i32(cpu_R[i * 2 + 1], tmp[i]);
2642 tcg_temp_free_i64(tmp[i]);
2644 if (nr & 1) {
2645 tcg_gen_mov_tl(cpu_R[dc->op2], tmp32);
2646 tcg_temp_free(tmp32);
2649 /* writeback the updated pointer value. */
2650 if (dc->postinc)
2651 tcg_gen_addi_tl(cpu_R[dc->op1], cpu_R[dc->op1], nr * 4);
2653 /* gen_load might want to evaluate the previous insns flags. */
2654 cris_cc_mask(dc, 0);
2655 return 2;
2658 static unsigned int dec_movem_rm(DisasContext *dc)
2660 TCGv tmp;
2661 TCGv addr;
2662 int i;
2664 DIS(fprintf (logfile, "movem $r%u, [$r%u%s\n", dc->op2, dc->op1,
2665 dc->postinc ? "+]" : "]"));
2667 cris_flush_cc_state(dc);
2669 tmp = tcg_temp_new();
2670 addr = tcg_temp_new();
2671 tcg_gen_movi_tl(tmp, 4);
2672 tcg_gen_mov_tl(addr, cpu_R[dc->op1]);
2673 for (i = 0; i <= dc->op2; i++) {
2674 /* Displace addr. */
2675 /* Perform the store. */
2676 gen_store(dc, addr, cpu_R[i], 4);
2677 tcg_gen_add_tl(addr, addr, tmp);
2679 if (dc->postinc)
2680 tcg_gen_mov_tl(cpu_R[dc->op1], addr);
2681 cris_cc_mask(dc, 0);
2682 tcg_temp_free(tmp);
2683 tcg_temp_free(addr);
2684 return 2;
2687 static unsigned int dec_move_rm(DisasContext *dc)
2689 int memsize;
2691 memsize = memsize_zz(dc);
2693 DIS(fprintf (logfile, "move.%d $r%u, [$r%u]\n",
2694 memsize, dc->op2, dc->op1));
2696 /* prepare store. */
2697 cris_flush_cc_state(dc);
2698 gen_store(dc, cpu_R[dc->op1], cpu_R[dc->op2], memsize);
2700 if (dc->postinc)
2701 tcg_gen_addi_tl(cpu_R[dc->op1], cpu_R[dc->op1], memsize);
2702 cris_cc_mask(dc, 0);
2703 return 2;
2706 static unsigned int dec_lapcq(DisasContext *dc)
2708 DIS(fprintf (logfile, "lapcq %x, $r%u\n",
2709 dc->pc + dc->op1*2, dc->op2));
2710 cris_cc_mask(dc, 0);
2711 tcg_gen_movi_tl(cpu_R[dc->op2], dc->pc + dc->op1 * 2);
2712 return 2;
2715 static unsigned int dec_lapc_im(DisasContext *dc)
2717 unsigned int rd;
2718 int32_t imm;
2719 int32_t pc;
2721 rd = dc->op2;
2723 cris_cc_mask(dc, 0);
2724 imm = ldl_code(dc->pc + 2);
2725 DIS(fprintf (logfile, "lapc 0x%x, $r%u\n", imm + dc->pc, dc->op2));
2727 pc = dc->pc;
2728 pc += imm;
2729 t_gen_mov_reg_TN(rd, tcg_const_tl(pc));
2730 return 6;
2733 /* Jump to special reg. */
2734 static unsigned int dec_jump_p(DisasContext *dc)
2736 DIS(fprintf (logfile, "jump $p%u\n", dc->op2));
2738 if (dc->op2 == PR_CCS)
2739 cris_evaluate_flags(dc);
2740 t_gen_mov_TN_preg(env_btarget, dc->op2);
2741 /* rete will often have low bit set to indicate delayslot. */
2742 tcg_gen_andi_tl(env_btarget, env_btarget, ~1);
2743 cris_cc_mask(dc, 0);
2744 cris_prepare_jmp(dc, JMP_INDIRECT);
2745 return 2;
2748 /* Jump and save. */
2749 static unsigned int dec_jas_r(DisasContext *dc)
2751 DIS(fprintf (logfile, "jas $r%u, $p%u\n", dc->op1, dc->op2));
2752 cris_cc_mask(dc, 0);
2753 /* Store the return address in Pd. */
2754 tcg_gen_mov_tl(env_btarget, cpu_R[dc->op1]);
2755 if (dc->op2 > 15)
2756 abort();
2757 t_gen_mov_preg_TN(dc, dc->op2, tcg_const_tl(dc->pc + 4));
2759 cris_prepare_jmp(dc, JMP_INDIRECT);
2760 return 2;
2763 static unsigned int dec_jas_im(DisasContext *dc)
2765 uint32_t imm;
2767 imm = ldl_code(dc->pc + 2);
2769 DIS(fprintf (logfile, "jas 0x%x\n", imm));
2770 cris_cc_mask(dc, 0);
2771 /* Store the return address in Pd. */
2772 t_gen_mov_preg_TN(dc, dc->op2, tcg_const_tl(dc->pc + 8));
2774 dc->jmp_pc = imm;
2775 cris_prepare_jmp(dc, JMP_DIRECT);
2776 return 6;
2779 static unsigned int dec_jasc_im(DisasContext *dc)
2781 uint32_t imm;
2783 imm = ldl_code(dc->pc + 2);
2785 DIS(fprintf (logfile, "jasc 0x%x\n", imm));
2786 cris_cc_mask(dc, 0);
2787 /* Store the return address in Pd. */
2788 t_gen_mov_preg_TN(dc, dc->op2, tcg_const_tl(dc->pc + 8 + 4));
2790 dc->jmp_pc = imm;
2791 cris_prepare_jmp(dc, JMP_DIRECT);
2792 return 6;
2795 static unsigned int dec_jasc_r(DisasContext *dc)
2797 DIS(fprintf (logfile, "jasc_r $r%u, $p%u\n", dc->op1, dc->op2));
2798 cris_cc_mask(dc, 0);
2799 /* Store the return address in Pd. */
2800 tcg_gen_mov_tl(env_btarget, cpu_R[dc->op1]);
2801 t_gen_mov_preg_TN(dc, dc->op2, tcg_const_tl(dc->pc + 4 + 4));
2802 cris_prepare_jmp(dc, JMP_INDIRECT);
2803 return 2;
2806 static unsigned int dec_bcc_im(DisasContext *dc)
2808 int32_t offset;
2809 uint32_t cond = dc->op2;
2811 offset = ldsw_code(dc->pc + 2);
2813 DIS(fprintf (logfile, "b%s %d pc=%x dst=%x\n",
2814 cc_name(cond), offset,
2815 dc->pc, dc->pc + offset));
2817 cris_cc_mask(dc, 0);
2818 /* op2 holds the condition-code. */
2819 cris_prepare_cc_branch (dc, offset, cond);
2820 return 4;
2823 static unsigned int dec_bas_im(DisasContext *dc)
2825 int32_t simm;
2828 simm = ldl_code(dc->pc + 2);
2830 DIS(fprintf (logfile, "bas 0x%x, $p%u\n", dc->pc + simm, dc->op2));
2831 cris_cc_mask(dc, 0);
2832 /* Store the return address in Pd. */
2833 t_gen_mov_preg_TN(dc, dc->op2, tcg_const_tl(dc->pc + 8));
2835 dc->jmp_pc = dc->pc + simm;
2836 cris_prepare_jmp(dc, JMP_DIRECT);
2837 return 6;
2840 static unsigned int dec_basc_im(DisasContext *dc)
2842 int32_t simm;
2843 simm = ldl_code(dc->pc + 2);
2845 DIS(fprintf (logfile, "basc 0x%x, $p%u\n", dc->pc + simm, dc->op2));
2846 cris_cc_mask(dc, 0);
2847 /* Store the return address in Pd. */
2848 t_gen_mov_preg_TN(dc, dc->op2, tcg_const_tl(dc->pc + 12));
2850 dc->jmp_pc = dc->pc + simm;
2851 cris_prepare_jmp(dc, JMP_DIRECT);
2852 return 6;
2855 static unsigned int dec_rfe_etc(DisasContext *dc)
2857 cris_cc_mask(dc, 0);
2859 if (dc->op2 == 15) {
2860 t_gen_mov_env_TN(halted, tcg_const_tl(1));
2861 tcg_gen_movi_tl(env_pc, dc->pc + 2);
2862 t_gen_raise_exception(EXCP_HLT);
2863 return 2;
2866 switch (dc->op2 & 7) {
2867 case 2:
2868 /* rfe. */
2869 DIS(fprintf(logfile, "rfe\n"));
2870 cris_evaluate_flags(dc);
2871 gen_helper_rfe();
2872 dc->is_jmp = DISAS_UPDATE;
2873 break;
2874 case 5:
2875 /* rfn. */
2876 DIS(fprintf(logfile, "rfn\n"));
2877 cris_evaluate_flags(dc);
2878 gen_helper_rfn();
2879 dc->is_jmp = DISAS_UPDATE;
2880 break;
2881 case 6:
2882 DIS(fprintf(logfile, "break %d\n", dc->op1));
2883 cris_evaluate_flags (dc);
2884 /* break. */
2885 tcg_gen_movi_tl(env_pc, dc->pc + 2);
2887 /* Breaks start at 16 in the exception vector. */
2888 t_gen_mov_env_TN(trap_vector,
2889 tcg_const_tl(dc->op1 + 16));
2890 t_gen_raise_exception(EXCP_BREAK);
2891 dc->is_jmp = DISAS_UPDATE;
2892 break;
2893 default:
2894 printf ("op2=%x\n", dc->op2);
2895 BUG();
2896 break;
2899 return 2;
2902 static unsigned int dec_ftag_fidx_d_m(DisasContext *dc)
2904 return 2;
2907 static unsigned int dec_ftag_fidx_i_m(DisasContext *dc)
2909 return 2;
2912 static unsigned int dec_null(DisasContext *dc)
2914 printf ("unknown insn pc=%x opc=%x op1=%x op2=%x\n",
2915 dc->pc, dc->opcode, dc->op1, dc->op2);
2916 fflush(NULL);
2917 BUG();
2918 return 2;
2921 static struct decoder_info {
2922 struct {
2923 uint32_t bits;
2924 uint32_t mask;
2926 unsigned int (*dec)(DisasContext *dc);
2927 } decinfo[] = {
2928 /* Order matters here. */
2929 {DEC_MOVEQ, dec_moveq},
2930 {DEC_BTSTQ, dec_btstq},
2931 {DEC_CMPQ, dec_cmpq},
2932 {DEC_ADDOQ, dec_addoq},
2933 {DEC_ADDQ, dec_addq},
2934 {DEC_SUBQ, dec_subq},
2935 {DEC_ANDQ, dec_andq},
2936 {DEC_ORQ, dec_orq},
2937 {DEC_ASRQ, dec_asrq},
2938 {DEC_LSLQ, dec_lslq},
2939 {DEC_LSRQ, dec_lsrq},
2940 {DEC_BCCQ, dec_bccq},
2942 {DEC_BCC_IM, dec_bcc_im},
2943 {DEC_JAS_IM, dec_jas_im},
2944 {DEC_JAS_R, dec_jas_r},
2945 {DEC_JASC_IM, dec_jasc_im},
2946 {DEC_JASC_R, dec_jasc_r},
2947 {DEC_BAS_IM, dec_bas_im},
2948 {DEC_BASC_IM, dec_basc_im},
2949 {DEC_JUMP_P, dec_jump_p},
2950 {DEC_LAPC_IM, dec_lapc_im},
2951 {DEC_LAPCQ, dec_lapcq},
2953 {DEC_RFE_ETC, dec_rfe_etc},
2954 {DEC_ADDC_MR, dec_addc_mr},
2956 {DEC_MOVE_MP, dec_move_mp},
2957 {DEC_MOVE_PM, dec_move_pm},
2958 {DEC_MOVEM_MR, dec_movem_mr},
2959 {DEC_MOVEM_RM, dec_movem_rm},
2960 {DEC_MOVE_PR, dec_move_pr},
2961 {DEC_SCC_R, dec_scc_r},
2962 {DEC_SETF, dec_setclrf},
2963 {DEC_CLEARF, dec_setclrf},
2965 {DEC_MOVE_SR, dec_move_sr},
2966 {DEC_MOVE_RP, dec_move_rp},
2967 {DEC_SWAP_R, dec_swap_r},
2968 {DEC_ABS_R, dec_abs_r},
2969 {DEC_LZ_R, dec_lz_r},
2970 {DEC_MOVE_RS, dec_move_rs},
2971 {DEC_BTST_R, dec_btst_r},
2972 {DEC_ADDC_R, dec_addc_r},
2974 {DEC_DSTEP_R, dec_dstep_r},
2975 {DEC_XOR_R, dec_xor_r},
2976 {DEC_MCP_R, dec_mcp_r},
2977 {DEC_CMP_R, dec_cmp_r},
2979 {DEC_ADDI_R, dec_addi_r},
2980 {DEC_ADDI_ACR, dec_addi_acr},
2982 {DEC_ADD_R, dec_add_r},
2983 {DEC_SUB_R, dec_sub_r},
2985 {DEC_ADDU_R, dec_addu_r},
2986 {DEC_ADDS_R, dec_adds_r},
2987 {DEC_SUBU_R, dec_subu_r},
2988 {DEC_SUBS_R, dec_subs_r},
2989 {DEC_LSL_R, dec_lsl_r},
2991 {DEC_AND_R, dec_and_r},
2992 {DEC_OR_R, dec_or_r},
2993 {DEC_BOUND_R, dec_bound_r},
2994 {DEC_ASR_R, dec_asr_r},
2995 {DEC_LSR_R, dec_lsr_r},
2997 {DEC_MOVU_R, dec_movu_r},
2998 {DEC_MOVS_R, dec_movs_r},
2999 {DEC_NEG_R, dec_neg_r},
3000 {DEC_MOVE_R, dec_move_r},
3002 {DEC_FTAG_FIDX_I_M, dec_ftag_fidx_i_m},
3003 {DEC_FTAG_FIDX_D_M, dec_ftag_fidx_d_m},
3005 {DEC_MULS_R, dec_muls_r},
3006 {DEC_MULU_R, dec_mulu_r},
3008 {DEC_ADDU_M, dec_addu_m},
3009 {DEC_ADDS_M, dec_adds_m},
3010 {DEC_SUBU_M, dec_subu_m},
3011 {DEC_SUBS_M, dec_subs_m},
3013 {DEC_CMPU_M, dec_cmpu_m},
3014 {DEC_CMPS_M, dec_cmps_m},
3015 {DEC_MOVU_M, dec_movu_m},
3016 {DEC_MOVS_M, dec_movs_m},
3018 {DEC_CMP_M, dec_cmp_m},
3019 {DEC_ADDO_M, dec_addo_m},
3020 {DEC_BOUND_M, dec_bound_m},
3021 {DEC_ADD_M, dec_add_m},
3022 {DEC_SUB_M, dec_sub_m},
3023 {DEC_AND_M, dec_and_m},
3024 {DEC_OR_M, dec_or_m},
3025 {DEC_MOVE_RM, dec_move_rm},
3026 {DEC_TEST_M, dec_test_m},
3027 {DEC_MOVE_MR, dec_move_mr},
3029 {{0, 0}, dec_null}
3032 static inline unsigned int
3033 cris_decoder(DisasContext *dc)
3035 unsigned int insn_len = 2;
3036 int i;
3038 if (unlikely(loglevel & CPU_LOG_TB_OP))
3039 tcg_gen_debug_insn_start(dc->pc);
3041 /* Load a halfword onto the instruction register. */
3042 dc->ir = lduw_code(dc->pc);
3044 /* Now decode it. */
3045 dc->opcode = EXTRACT_FIELD(dc->ir, 4, 11);
3046 dc->op1 = EXTRACT_FIELD(dc->ir, 0, 3);
3047 dc->op2 = EXTRACT_FIELD(dc->ir, 12, 15);
3048 dc->zsize = EXTRACT_FIELD(dc->ir, 4, 4);
3049 dc->zzsize = EXTRACT_FIELD(dc->ir, 4, 5);
3050 dc->postinc = EXTRACT_FIELD(dc->ir, 10, 10);
3052 /* Large switch for all insns. */
3053 for (i = 0; i < ARRAY_SIZE(decinfo); i++) {
3054 if ((dc->opcode & decinfo[i].mask) == decinfo[i].bits)
3056 insn_len = decinfo[i].dec(dc);
3057 break;
3061 #if !defined(CONFIG_USER_ONLY)
3062 /* Single-stepping ? */
3063 if (dc->tb_flags & S_FLAG) {
3064 int l1;
3066 l1 = gen_new_label();
3067 tcg_gen_brcondi_tl(TCG_COND_NE, cpu_PR[PR_SPC], dc->pc, l1);
3068 /* We treat SPC as a break with an odd trap vector. */
3069 cris_evaluate_flags (dc);
3070 t_gen_mov_env_TN(trap_vector, tcg_const_tl(3));
3071 tcg_gen_movi_tl(env_pc, dc->pc + insn_len);
3072 tcg_gen_movi_tl(cpu_PR[PR_SPC], dc->pc + insn_len);
3073 t_gen_raise_exception(EXCP_BREAK);
3074 gen_set_label(l1);
3076 #endif
3077 return insn_len;
3080 static void check_breakpoint(CPUState *env, DisasContext *dc)
3082 CPUBreakpoint *bp;
3084 if (unlikely(!TAILQ_EMPTY(&env->breakpoints))) {
3085 TAILQ_FOREACH(bp, &env->breakpoints, entry) {
3086 if (bp->pc == dc->pc) {
3087 cris_evaluate_flags (dc);
3088 tcg_gen_movi_tl(env_pc, dc->pc);
3089 t_gen_raise_exception(EXCP_DEBUG);
3090 dc->is_jmp = DISAS_UPDATE;
3098 * Delay slots on QEMU/CRIS.
3100 * If an exception hits on a delayslot, the core will let ERP (the Exception
3101 * Return Pointer) point to the branch (the previous) insn and set the lsb to
3102 * to give SW a hint that the exception actually hit on the dslot.
3104 * CRIS expects all PC addresses to be 16-bit aligned. The lsb is ignored by
3105 * the core and any jmp to an odd addresses will mask off that lsb. It is
3106 * simply there to let sw know there was an exception on a dslot.
3108 * When the software returns from an exception, the branch will re-execute.
3109 * On QEMU care needs to be taken when a branch+delayslot sequence is broken
3110 * and the branch and delayslot dont share pages.
3112 * The TB contaning the branch insn will set up env->btarget and evaluate
3113 * env->btaken. When the translation loop exits we will note that the branch
3114 * sequence is broken and let env->dslot be the size of the branch insn (those
3115 * vary in length).
3117 * The TB contaning the delayslot will have the PC of its real insn (i.e no lsb
3118 * set). It will also expect to have env->dslot setup with the size of the
3119 * delay slot so that env->pc - env->dslot point to the branch insn. This TB
3120 * will execute the dslot and take the branch, either to btarget or just one
3121 * insn ahead.
3123 * When exceptions occur, we check for env->dslot in do_interrupt to detect
3124 * broken branch sequences and setup $erp accordingly (i.e let it point to the
3125 * branch and set lsb). Then env->dslot gets cleared so that the exception
3126 * handler can enter. When returning from exceptions (jump $erp) the lsb gets
3127 * masked off and we will reexecute the branch insn.
3131 /* generate intermediate code for basic block 'tb'. */
3132 static void
3133 gen_intermediate_code_internal(CPUState *env, TranslationBlock *tb,
3134 int search_pc)
3136 uint16_t *gen_opc_end;
3137 uint32_t pc_start;
3138 unsigned int insn_len;
3139 int j, lj;
3140 struct DisasContext ctx;
3141 struct DisasContext *dc = &ctx;
3142 uint32_t next_page_start;
3143 target_ulong npc;
3144 int num_insns;
3145 int max_insns;
3147 if (!logfile)
3148 logfile = stderr;
3150 /* Odd PC indicates that branch is rexecuting due to exception in the
3151 * delayslot, like in real hw.
3153 pc_start = tb->pc & ~1;
3154 dc->env = env;
3155 dc->tb = tb;
3157 gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
3159 dc->is_jmp = DISAS_NEXT;
3160 dc->ppc = pc_start;
3161 dc->pc = pc_start;
3162 dc->singlestep_enabled = env->singlestep_enabled;
3163 dc->flags_uptodate = 1;
3164 dc->flagx_known = 1;
3165 dc->flags_x = tb->flags & X_FLAG;
3166 dc->cc_x_uptodate = 0;
3167 dc->cc_mask = 0;
3168 dc->update_cc = 0;
3170 cris_update_cc_op(dc, CC_OP_FLAGS, 4);
3171 dc->cc_size_uptodate = -1;
3173 /* Decode TB flags. */
3174 dc->tb_flags = tb->flags & (S_FLAG | P_FLAG | U_FLAG | X_FLAG);
3175 dc->delayed_branch = !!(tb->flags & 7);
3176 if (dc->delayed_branch)
3177 dc->jmp = JMP_INDIRECT;
3178 else
3179 dc->jmp = JMP_NOJMP;
3181 dc->cpustate_changed = 0;
3183 if (loglevel & CPU_LOG_TB_IN_ASM) {
3184 fprintf(logfile,
3185 "srch=%d pc=%x %x flg=%llx bt=%x ds=%u ccs=%x\n"
3186 "pid=%x usp=%x\n"
3187 "%x.%x.%x.%x\n"
3188 "%x.%x.%x.%x\n"
3189 "%x.%x.%x.%x\n"
3190 "%x.%x.%x.%x\n",
3191 search_pc, dc->pc, dc->ppc,
3192 (unsigned long long)tb->flags,
3193 env->btarget, (unsigned)tb->flags & 7,
3194 env->pregs[PR_CCS],
3195 env->pregs[PR_PID], env->pregs[PR_USP],
3196 env->regs[0], env->regs[1], env->regs[2], env->regs[3],
3197 env->regs[4], env->regs[5], env->regs[6], env->regs[7],
3198 env->regs[8], env->regs[9],
3199 env->regs[10], env->regs[11],
3200 env->regs[12], env->regs[13],
3201 env->regs[14], env->regs[15]);
3202 fprintf(logfile, "--------------\n");
3203 fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
3206 next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
3207 lj = -1;
3208 num_insns = 0;
3209 max_insns = tb->cflags & CF_COUNT_MASK;
3210 if (max_insns == 0)
3211 max_insns = CF_COUNT_MASK;
3213 gen_icount_start();
3216 check_breakpoint(env, dc);
3218 if (search_pc) {
3219 j = gen_opc_ptr - gen_opc_buf;
3220 if (lj < j) {
3221 lj++;
3222 while (lj < j)
3223 gen_opc_instr_start[lj++] = 0;
3225 if (dc->delayed_branch == 1)
3226 gen_opc_pc[lj] = dc->ppc | 1;
3227 else
3228 gen_opc_pc[lj] = dc->pc;
3229 gen_opc_instr_start[lj] = 1;
3230 gen_opc_icount[lj] = num_insns;
3233 /* Pretty disas. */
3234 DIS(fprintf(logfile, "%8.8x:\t", dc->pc));
3236 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
3237 gen_io_start();
3238 dc->clear_x = 1;
3240 insn_len = cris_decoder(dc);
3241 dc->ppc = dc->pc;
3242 dc->pc += insn_len;
3243 if (dc->clear_x)
3244 cris_clear_x_flag(dc);
3246 num_insns++;
3247 /* Check for delayed branches here. If we do it before
3248 actually generating any host code, the simulator will just
3249 loop doing nothing for on this program location. */
3250 if (dc->delayed_branch) {
3251 dc->delayed_branch--;
3252 if (dc->delayed_branch == 0)
3254 if (tb->flags & 7)
3255 t_gen_mov_env_TN(dslot,
3256 tcg_const_tl(0));
3257 if (dc->jmp == JMP_DIRECT) {
3258 dc->is_jmp = DISAS_NEXT;
3259 } else {
3260 t_gen_cc_jmp(env_btarget,
3261 tcg_const_tl(dc->pc));
3262 dc->is_jmp = DISAS_JUMP;
3264 break;
3268 /* If we are rexecuting a branch due to exceptions on
3269 delay slots dont break. */
3270 if (!(tb->pc & 1) && env->singlestep_enabled)
3271 break;
3272 } while (!dc->is_jmp && !dc->cpustate_changed
3273 && gen_opc_ptr < gen_opc_end
3274 && (dc->pc < next_page_start)
3275 && num_insns < max_insns);
3277 npc = dc->pc;
3278 if (dc->jmp == JMP_DIRECT && !dc->delayed_branch)
3279 npc = dc->jmp_pc;
3281 if (tb->cflags & CF_LAST_IO)
3282 gen_io_end();
3283 /* Force an update if the per-tb cpu state has changed. */
3284 if (dc->is_jmp == DISAS_NEXT
3285 && (dc->cpustate_changed || !dc->flagx_known
3286 || (dc->flags_x != (tb->flags & X_FLAG)))) {
3287 dc->is_jmp = DISAS_UPDATE;
3288 tcg_gen_movi_tl(env_pc, npc);
3290 /* Broken branch+delayslot sequence. */
3291 if (dc->delayed_branch == 1) {
3292 /* Set env->dslot to the size of the branch insn. */
3293 t_gen_mov_env_TN(dslot, tcg_const_tl(dc->pc - dc->ppc));
3294 cris_store_direct_jmp(dc);
3297 cris_evaluate_flags (dc);
3299 if (unlikely(env->singlestep_enabled)) {
3300 if (dc->is_jmp == DISAS_NEXT)
3301 tcg_gen_movi_tl(env_pc, npc);
3302 t_gen_raise_exception(EXCP_DEBUG);
3303 } else {
3304 switch(dc->is_jmp) {
3305 case DISAS_NEXT:
3306 gen_goto_tb(dc, 1, npc);
3307 break;
3308 default:
3309 case DISAS_JUMP:
3310 case DISAS_UPDATE:
3311 /* indicate that the hash table must be used
3312 to find the next TB */
3313 tcg_gen_exit_tb(0);
3314 break;
3315 case DISAS_SWI:
3316 case DISAS_TB_JUMP:
3317 /* nothing more to generate */
3318 break;
3321 gen_icount_end(tb, num_insns);
3322 *gen_opc_ptr = INDEX_op_end;
3323 if (search_pc) {
3324 j = gen_opc_ptr - gen_opc_buf;
3325 lj++;
3326 while (lj <= j)
3327 gen_opc_instr_start[lj++] = 0;
3328 } else {
3329 tb->size = dc->pc - pc_start;
3330 tb->icount = num_insns;
3333 #ifdef DEBUG_DISAS
3334 #if !DISAS_CRIS
3335 if (loglevel & CPU_LOG_TB_IN_ASM) {
3336 target_disas(logfile, pc_start, dc->pc - pc_start, 0);
3337 fprintf(logfile, "\nisize=%d osize=%zd\n",
3338 dc->pc - pc_start, gen_opc_ptr - gen_opc_buf);
3340 #endif
3341 #endif
3344 void gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
3346 gen_intermediate_code_internal(env, tb, 0);
3349 void gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
3351 gen_intermediate_code_internal(env, tb, 1);
3354 void cpu_dump_state (CPUState *env, FILE *f,
3355 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
3356 int flags)
3358 int i;
3359 uint32_t srs;
3361 if (!env || !f)
3362 return;
3364 cpu_fprintf(f, "PC=%x CCS=%x btaken=%d btarget=%x\n"
3365 "cc_op=%d cc_src=%d cc_dest=%d cc_result=%x cc_mask=%x\n",
3366 env->pc, env->pregs[PR_CCS], env->btaken, env->btarget,
3367 env->cc_op,
3368 env->cc_src, env->cc_dest, env->cc_result, env->cc_mask);
3371 for (i = 0; i < 16; i++) {
3372 cpu_fprintf(f, "r%2.2d=%8.8x ", i, env->regs[i]);
3373 if ((i + 1) % 4 == 0)
3374 cpu_fprintf(f, "\n");
3376 cpu_fprintf(f, "\nspecial regs:\n");
3377 for (i = 0; i < 16; i++) {
3378 cpu_fprintf(f, "p%2.2d=%8.8x ", i, env->pregs[i]);
3379 if ((i + 1) % 4 == 0)
3380 cpu_fprintf(f, "\n");
3382 srs = env->pregs[PR_SRS];
3383 cpu_fprintf(f, "\nsupport function regs bank %x:\n", srs);
3384 if (srs < 256) {
3385 for (i = 0; i < 16; i++) {
3386 cpu_fprintf(f, "s%2.2d=%8.8x ",
3387 i, env->sregs[srs][i]);
3388 if ((i + 1) % 4 == 0)
3389 cpu_fprintf(f, "\n");
3392 cpu_fprintf(f, "\n\n");
3396 CPUCRISState *cpu_cris_init (const char *cpu_model)
3398 CPUCRISState *env;
3399 static int tcg_initialized = 0;
3400 int i;
3402 env = qemu_mallocz(sizeof(CPUCRISState));
3403 if (!env)
3404 return NULL;
3406 cpu_exec_init(env);
3407 cpu_reset(env);
3409 if (tcg_initialized)
3410 return env;
3412 tcg_initialized = 1;
3414 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
3415 cc_x = tcg_global_mem_new(TCG_AREG0,
3416 offsetof(CPUState, cc_x), "cc_x");
3417 cc_src = tcg_global_mem_new(TCG_AREG0,
3418 offsetof(CPUState, cc_src), "cc_src");
3419 cc_dest = tcg_global_mem_new(TCG_AREG0,
3420 offsetof(CPUState, cc_dest),
3421 "cc_dest");
3422 cc_result = tcg_global_mem_new(TCG_AREG0,
3423 offsetof(CPUState, cc_result),
3424 "cc_result");
3425 cc_op = tcg_global_mem_new(TCG_AREG0,
3426 offsetof(CPUState, cc_op), "cc_op");
3427 cc_size = tcg_global_mem_new(TCG_AREG0,
3428 offsetof(CPUState, cc_size),
3429 "cc_size");
3430 cc_mask = tcg_global_mem_new(TCG_AREG0,
3431 offsetof(CPUState, cc_mask),
3432 "cc_mask");
3434 env_pc = tcg_global_mem_new(TCG_AREG0,
3435 offsetof(CPUState, pc),
3436 "pc");
3437 env_btarget = tcg_global_mem_new(TCG_AREG0,
3438 offsetof(CPUState, btarget),
3439 "btarget");
3440 env_btaken = tcg_global_mem_new(TCG_AREG0,
3441 offsetof(CPUState, btaken),
3442 "btaken");
3443 for (i = 0; i < 16; i++) {
3444 cpu_R[i] = tcg_global_mem_new(TCG_AREG0,
3445 offsetof(CPUState, regs[i]),
3446 regnames[i]);
3448 for (i = 0; i < 16; i++) {
3449 cpu_PR[i] = tcg_global_mem_new(TCG_AREG0,
3450 offsetof(CPUState, pregs[i]),
3451 pregnames[i]);
3454 #define GEN_HELPER 2
3455 #include "helper.h"
3457 return env;
3460 void cpu_reset (CPUCRISState *env)
3462 memset(env, 0, offsetof(CPUCRISState, breakpoints));
3463 tlb_flush(env, 1);
3465 env->pregs[PR_VR] = 32;
3466 #if defined(CONFIG_USER_ONLY)
3467 /* start in user mode with interrupts enabled. */
3468 env->pregs[PR_CCS] |= U_FLAG | I_FLAG;
3469 #else
3470 env->pregs[PR_CCS] = 0;
3471 #endif
3474 void gen_pc_load(CPUState *env, struct TranslationBlock *tb,
3475 unsigned long searched_pc, int pc_pos, void *puc)
3477 env->pc = gen_opc_pc[pc_pos];