3 * http://www.tensilica.com/products/literature-docs/documentation/xtensa-isa-databook.htm
5 * Copyright (c) 2011, Max Filippov, Open Source and Linux Lab.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of the Open Source and Linux Lab nor the
16 * names of its contributors may be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 typedef struct DisasContext {
45 const XtensaConfig *config;
55 int singlestep_enabled;
59 bool sar_m32_allocated;
62 uint32_t ccount_delta;
66 static TCGv_ptr cpu_env;
67 static TCGv_i32 cpu_pc;
68 static TCGv_i32 cpu_R[16];
69 static TCGv_i32 cpu_SR[256];
70 static TCGv_i32 cpu_UR[256];
72 #include "gen-icount.h"
74 static const char * const sregnames[256] = {
79 [LITBASE] = "LITBASE",
80 [SCOMPARE1] = "SCOMPARE1",
81 [WINDOW_BASE] = "WINDOW_BASE",
82 [WINDOW_START] = "WINDOW_START",
97 [EXCSAVE1] = "EXCSAVE1",
98 [EXCSAVE1 + 1] = "EXCSAVE2",
99 [EXCSAVE1 + 2] = "EXCSAVE3",
100 [EXCSAVE1 + 3] = "EXCSAVE4",
101 [EXCSAVE1 + 4] = "EXCSAVE5",
102 [EXCSAVE1 + 5] = "EXCSAVE6",
103 [EXCSAVE1 + 6] = "EXCSAVE7",
104 [CPENABLE] = "CPENABLE",
106 [INTCLEAR] = "INTCLEAR",
107 [INTENABLE] = "INTENABLE",
109 [VECBASE] = "VECBASE",
110 [EXCCAUSE] = "EXCCAUSE",
113 [EXCVADDR] = "EXCVADDR",
114 [CCOMPARE] = "CCOMPARE0",
115 [CCOMPARE + 1] = "CCOMPARE1",
116 [CCOMPARE + 2] = "CCOMPARE2",
119 static const char * const uregnames[256] = {
120 [THREADPTR] = "THREADPTR",
125 void xtensa_translate_init(void)
127 static const char * const regnames[] = {
128 "ar0", "ar1", "ar2", "ar3",
129 "ar4", "ar5", "ar6", "ar7",
130 "ar8", "ar9", "ar10", "ar11",
131 "ar12", "ar13", "ar14", "ar15",
135 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
136 cpu_pc = tcg_global_mem_new_i32(TCG_AREG0,
137 offsetof(CPUState, pc), "pc");
139 for (i = 0; i < 16; i++) {
140 cpu_R[i] = tcg_global_mem_new_i32(TCG_AREG0,
141 offsetof(CPUState, regs[i]),
145 for (i = 0; i < 256; ++i) {
147 cpu_SR[i] = tcg_global_mem_new_i32(TCG_AREG0,
148 offsetof(CPUState, sregs[i]),
153 for (i = 0; i < 256; ++i) {
155 cpu_UR[i] = tcg_global_mem_new_i32(TCG_AREG0,
156 offsetof(CPUState, uregs[i]),
164 static inline bool option_enabled(DisasContext *dc, int opt)
166 return xtensa_option_enabled(dc->config, opt);
169 static void init_litbase(DisasContext *dc)
171 if (dc->tb->flags & XTENSA_TBFLAG_LITBASE) {
172 dc->litbase = tcg_temp_local_new_i32();
173 tcg_gen_andi_i32(dc->litbase, cpu_SR[LITBASE], 0xfffff000);
177 static void reset_litbase(DisasContext *dc)
179 if (dc->tb->flags & XTENSA_TBFLAG_LITBASE) {
180 tcg_temp_free(dc->litbase);
184 static void init_sar_tracker(DisasContext *dc)
186 dc->sar_5bit = false;
187 dc->sar_m32_5bit = false;
188 dc->sar_m32_allocated = false;
191 static void reset_sar_tracker(DisasContext *dc)
193 if (dc->sar_m32_allocated) {
194 tcg_temp_free(dc->sar_m32);
198 static void gen_right_shift_sar(DisasContext *dc, TCGv_i32 sa)
200 tcg_gen_andi_i32(cpu_SR[SAR], sa, 0x1f);
201 if (dc->sar_m32_5bit) {
202 tcg_gen_discard_i32(dc->sar_m32);
205 dc->sar_m32_5bit = false;
208 static void gen_left_shift_sar(DisasContext *dc, TCGv_i32 sa)
210 TCGv_i32 tmp = tcg_const_i32(32);
211 if (!dc->sar_m32_allocated) {
212 dc->sar_m32 = tcg_temp_local_new_i32();
213 dc->sar_m32_allocated = true;
215 tcg_gen_andi_i32(dc->sar_m32, sa, 0x1f);
216 tcg_gen_sub_i32(cpu_SR[SAR], tmp, dc->sar_m32);
217 dc->sar_5bit = false;
218 dc->sar_m32_5bit = true;
222 static void gen_advance_ccount(DisasContext *dc)
224 if (dc->ccount_delta > 0) {
225 TCGv_i32 tmp = tcg_const_i32(dc->ccount_delta);
226 dc->ccount_delta = 0;
227 gen_helper_advance_ccount(tmp);
232 static void reset_used_window(DisasContext *dc)
237 static void gen_exception(DisasContext *dc, int excp)
239 TCGv_i32 tmp = tcg_const_i32(excp);
240 gen_advance_ccount(dc);
241 gen_helper_exception(tmp);
245 static void gen_exception_cause(DisasContext *dc, uint32_t cause)
247 TCGv_i32 tpc = tcg_const_i32(dc->pc);
248 TCGv_i32 tcause = tcg_const_i32(cause);
249 gen_advance_ccount(dc);
250 gen_helper_exception_cause(tpc, tcause);
252 tcg_temp_free(tcause);
255 static void gen_exception_cause_vaddr(DisasContext *dc, uint32_t cause,
258 TCGv_i32 tpc = tcg_const_i32(dc->pc);
259 TCGv_i32 tcause = tcg_const_i32(cause);
260 gen_advance_ccount(dc);
261 gen_helper_exception_cause_vaddr(tpc, tcause, vaddr);
263 tcg_temp_free(tcause);
266 static void gen_check_privilege(DisasContext *dc)
269 gen_exception_cause(dc, PRIVILEGED_CAUSE);
273 static void gen_jump_slot(DisasContext *dc, TCGv dest, int slot)
275 tcg_gen_mov_i32(cpu_pc, dest);
276 if (dc->singlestep_enabled) {
277 gen_exception(dc, EXCP_DEBUG);
279 gen_advance_ccount(dc);
281 tcg_gen_goto_tb(slot);
282 tcg_gen_exit_tb((tcg_target_long)dc->tb + slot);
287 dc->is_jmp = DISAS_UPDATE;
290 static void gen_jump(DisasContext *dc, TCGv dest)
292 gen_jump_slot(dc, dest, -1);
295 static void gen_jumpi(DisasContext *dc, uint32_t dest, int slot)
297 TCGv_i32 tmp = tcg_const_i32(dest);
298 if (((dc->pc ^ dest) & TARGET_PAGE_MASK) != 0) {
301 gen_jump_slot(dc, tmp, slot);
305 static void gen_callw_slot(DisasContext *dc, int callinc, TCGv_i32 dest,
308 TCGv_i32 tcallinc = tcg_const_i32(callinc);
310 tcg_gen_deposit_i32(cpu_SR[PS], cpu_SR[PS],
311 tcallinc, PS_CALLINC_SHIFT, PS_CALLINC_LEN);
312 tcg_temp_free(tcallinc);
313 tcg_gen_movi_i32(cpu_R[callinc << 2],
314 (callinc << 30) | (dc->next_pc & 0x3fffffff));
315 gen_jump_slot(dc, dest, slot);
318 static void gen_callw(DisasContext *dc, int callinc, TCGv_i32 dest)
320 gen_callw_slot(dc, callinc, dest, -1);
323 static void gen_callwi(DisasContext *dc, int callinc, uint32_t dest, int slot)
325 TCGv_i32 tmp = tcg_const_i32(dest);
326 if (((dc->pc ^ dest) & TARGET_PAGE_MASK) != 0) {
329 gen_callw_slot(dc, callinc, tmp, slot);
333 static bool gen_check_loop_end(DisasContext *dc, int slot)
335 if (option_enabled(dc, XTENSA_OPTION_LOOP) &&
336 !(dc->tb->flags & XTENSA_TBFLAG_EXCM) &&
337 dc->next_pc == dc->lend) {
338 int label = gen_new_label();
340 tcg_gen_brcondi_i32(TCG_COND_EQ, cpu_SR[LCOUNT], 0, label);
341 tcg_gen_subi_i32(cpu_SR[LCOUNT], cpu_SR[LCOUNT], 1);
342 gen_jumpi(dc, dc->lbeg, slot);
343 gen_set_label(label);
344 gen_jumpi(dc, dc->next_pc, -1);
350 static void gen_jumpi_check_loop_end(DisasContext *dc, int slot)
352 if (!gen_check_loop_end(dc, slot)) {
353 gen_jumpi(dc, dc->next_pc, slot);
357 static void gen_brcond(DisasContext *dc, TCGCond cond,
358 TCGv_i32 t0, TCGv_i32 t1, uint32_t offset)
360 int label = gen_new_label();
362 tcg_gen_brcond_i32(cond, t0, t1, label);
363 gen_jumpi_check_loop_end(dc, 0);
364 gen_set_label(label);
365 gen_jumpi(dc, dc->pc + offset, 1);
368 static void gen_brcondi(DisasContext *dc, TCGCond cond,
369 TCGv_i32 t0, uint32_t t1, uint32_t offset)
371 TCGv_i32 tmp = tcg_const_i32(t1);
372 gen_brcond(dc, cond, t0, tmp, offset);
376 static void gen_rsr_ccount(DisasContext *dc, TCGv_i32 d, uint32_t sr)
378 gen_advance_ccount(dc);
379 tcg_gen_mov_i32(d, cpu_SR[sr]);
382 static void gen_rsr(DisasContext *dc, TCGv_i32 d, uint32_t sr)
384 static void (* const rsr_handler[256])(DisasContext *dc,
385 TCGv_i32 d, uint32_t sr) = {
386 [CCOUNT] = gen_rsr_ccount,
390 if (rsr_handler[sr]) {
391 rsr_handler[sr](dc, d, sr);
393 tcg_gen_mov_i32(d, cpu_SR[sr]);
396 qemu_log("RSR %d not implemented, ", sr);
400 static void gen_wsr_lbeg(DisasContext *dc, uint32_t sr, TCGv_i32 s)
402 gen_helper_wsr_lbeg(s);
405 static void gen_wsr_lend(DisasContext *dc, uint32_t sr, TCGv_i32 s)
407 gen_helper_wsr_lend(s);
410 static void gen_wsr_sar(DisasContext *dc, uint32_t sr, TCGv_i32 s)
412 tcg_gen_andi_i32(cpu_SR[sr], s, 0x3f);
413 if (dc->sar_m32_5bit) {
414 tcg_gen_discard_i32(dc->sar_m32);
416 dc->sar_5bit = false;
417 dc->sar_m32_5bit = false;
420 static void gen_wsr_litbase(DisasContext *dc, uint32_t sr, TCGv_i32 s)
422 tcg_gen_andi_i32(cpu_SR[sr], s, 0xfffff001);
423 /* This can change tb->flags, so exit tb */
424 gen_jumpi_check_loop_end(dc, -1);
427 static void gen_wsr_windowbase(DisasContext *dc, uint32_t sr, TCGv_i32 v)
429 gen_helper_wsr_windowbase(v);
430 reset_used_window(dc);
433 static void gen_wsr_windowstart(DisasContext *dc, uint32_t sr, TCGv_i32 v)
435 tcg_gen_mov_i32(cpu_SR[sr], v);
436 reset_used_window(dc);
439 static void gen_wsr_intset(DisasContext *dc, uint32_t sr, TCGv_i32 v)
441 tcg_gen_andi_i32(cpu_SR[sr], v,
442 dc->config->inttype_mask[INTTYPE_SOFTWARE]);
443 gen_helper_check_interrupts(cpu_env);
444 gen_jumpi_check_loop_end(dc, 0);
447 static void gen_wsr_intclear(DisasContext *dc, uint32_t sr, TCGv_i32 v)
449 TCGv_i32 tmp = tcg_temp_new_i32();
451 tcg_gen_andi_i32(tmp, v,
452 dc->config->inttype_mask[INTTYPE_EDGE] |
453 dc->config->inttype_mask[INTTYPE_NMI] |
454 dc->config->inttype_mask[INTTYPE_SOFTWARE]);
455 tcg_gen_andc_i32(cpu_SR[INTSET], cpu_SR[INTSET], tmp);
457 gen_helper_check_interrupts(cpu_env);
460 static void gen_wsr_intenable(DisasContext *dc, uint32_t sr, TCGv_i32 v)
462 tcg_gen_mov_i32(cpu_SR[sr], v);
463 gen_helper_check_interrupts(cpu_env);
464 gen_jumpi_check_loop_end(dc, 0);
467 static void gen_wsr_ps(DisasContext *dc, uint32_t sr, TCGv_i32 v)
469 uint32_t mask = PS_WOE | PS_CALLINC | PS_OWB |
470 PS_UM | PS_EXCM | PS_INTLEVEL;
472 if (option_enabled(dc, XTENSA_OPTION_MMU)) {
475 tcg_gen_andi_i32(cpu_SR[sr], v, mask);
476 reset_used_window(dc);
477 gen_helper_check_interrupts(cpu_env);
478 /* This can change mmu index and tb->flags, so exit tb */
479 gen_jumpi_check_loop_end(dc, -1);
482 static void gen_wsr_prid(DisasContext *dc, uint32_t sr, TCGv_i32 v)
486 static void gen_wsr_ccompare(DisasContext *dc, uint32_t sr, TCGv_i32 v)
488 uint32_t id = sr - CCOMPARE;
489 if (id < dc->config->nccompare) {
490 uint32_t int_bit = 1 << dc->config->timerint[id];
491 gen_advance_ccount(dc);
492 tcg_gen_mov_i32(cpu_SR[sr], v);
493 tcg_gen_andi_i32(cpu_SR[INTSET], cpu_SR[INTSET], ~int_bit);
494 gen_helper_check_interrupts(cpu_env);
498 static void gen_wsr(DisasContext *dc, uint32_t sr, TCGv_i32 s)
500 static void (* const wsr_handler[256])(DisasContext *dc,
501 uint32_t sr, TCGv_i32 v) = {
502 [LBEG] = gen_wsr_lbeg,
503 [LEND] = gen_wsr_lend,
505 [LITBASE] = gen_wsr_litbase,
506 [WINDOW_BASE] = gen_wsr_windowbase,
507 [WINDOW_START] = gen_wsr_windowstart,
508 [INTSET] = gen_wsr_intset,
509 [INTCLEAR] = gen_wsr_intclear,
510 [INTENABLE] = gen_wsr_intenable,
512 [PRID] = gen_wsr_prid,
513 [CCOMPARE] = gen_wsr_ccompare,
514 [CCOMPARE + 1] = gen_wsr_ccompare,
515 [CCOMPARE + 2] = gen_wsr_ccompare,
519 if (wsr_handler[sr]) {
520 wsr_handler[sr](dc, sr, s);
522 tcg_gen_mov_i32(cpu_SR[sr], s);
525 qemu_log("WSR %d not implemented, ", sr);
529 static void gen_load_store_alignment(DisasContext *dc, int shift,
530 TCGv_i32 addr, bool no_hw_alignment)
532 if (!option_enabled(dc, XTENSA_OPTION_UNALIGNED_EXCEPTION)) {
533 tcg_gen_andi_i32(addr, addr, ~0 << shift);
534 } else if (option_enabled(dc, XTENSA_OPTION_HW_ALIGNMENT) &&
536 int label = gen_new_label();
537 TCGv_i32 tmp = tcg_temp_new_i32();
538 tcg_gen_andi_i32(tmp, addr, ~(~0 << shift));
539 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, label);
540 gen_exception_cause_vaddr(dc, LOAD_STORE_ALIGNMENT_CAUSE, addr);
541 gen_set_label(label);
546 static void gen_waiti(DisasContext *dc, uint32_t imm4)
548 TCGv_i32 pc = tcg_const_i32(dc->next_pc);
549 TCGv_i32 intlevel = tcg_const_i32(imm4);
550 gen_advance_ccount(dc);
551 gen_helper_waiti(pc, intlevel);
553 tcg_temp_free(intlevel);
556 static void gen_window_check1(DisasContext *dc, unsigned r1)
558 if (dc->tb->flags & XTENSA_TBFLAG_EXCM) {
561 if (option_enabled(dc, XTENSA_OPTION_WINDOWED_REGISTER) &&
562 r1 / 4 > dc->used_window) {
563 TCGv_i32 pc = tcg_const_i32(dc->pc);
564 TCGv_i32 w = tcg_const_i32(r1 / 4);
566 dc->used_window = r1 / 4;
567 gen_advance_ccount(dc);
568 gen_helper_window_check(pc, w);
575 static void gen_window_check2(DisasContext *dc, unsigned r1, unsigned r2)
577 gen_window_check1(dc, r1 > r2 ? r1 : r2);
580 static void gen_window_check3(DisasContext *dc, unsigned r1, unsigned r2,
583 gen_window_check2(dc, r1, r2 > r3 ? r2 : r3);
586 static void disas_xtensa_insn(DisasContext *dc)
588 #define HAS_OPTION(opt) do { \
589 if (!option_enabled(dc, opt)) { \
590 qemu_log("Option %d is not enabled %s:%d\n", \
591 (opt), __FILE__, __LINE__); \
592 goto invalid_opcode; \
596 #define TBD() qemu_log("TBD(pc = %08x): %s:%d\n", dc->pc, __FILE__, __LINE__)
597 #define RESERVED() do { \
598 qemu_log("RESERVED(pc = %08x, %02x%02x%02x): %s:%d\n", \
599 dc->pc, b0, b1, b2, __FILE__, __LINE__); \
600 goto invalid_opcode; \
604 #ifdef TARGET_WORDS_BIGENDIAN
605 #define OP0 (((b0) & 0xf0) >> 4)
606 #define OP1 (((b2) & 0xf0) >> 4)
607 #define OP2 ((b2) & 0xf)
608 #define RRR_R ((b1) & 0xf)
609 #define RRR_S (((b1) & 0xf0) >> 4)
610 #define RRR_T ((b0) & 0xf)
612 #define OP0 (((b0) & 0xf))
613 #define OP1 (((b2) & 0xf))
614 #define OP2 (((b2) & 0xf0) >> 4)
615 #define RRR_R (((b1) & 0xf0) >> 4)
616 #define RRR_S (((b1) & 0xf))
617 #define RRR_T (((b0) & 0xf0) >> 4)
627 #define RRI8_IMM8 (b2)
628 #define RRI8_IMM8_SE ((((b2) & 0x80) ? 0xffffff00 : 0) | RRI8_IMM8)
630 #ifdef TARGET_WORDS_BIGENDIAN
631 #define RI16_IMM16 (((b1) << 8) | (b2))
633 #define RI16_IMM16 (((b2) << 8) | (b1))
636 #ifdef TARGET_WORDS_BIGENDIAN
637 #define CALL_N (((b0) & 0xc) >> 2)
638 #define CALL_OFFSET ((((b0) & 0x3) << 16) | ((b1) << 8) | (b2))
640 #define CALL_N (((b0) & 0x30) >> 4)
641 #define CALL_OFFSET ((((b0) & 0xc0) >> 6) | ((b1) << 2) | ((b2) << 10))
643 #define CALL_OFFSET_SE \
644 (((CALL_OFFSET & 0x20000) ? 0xfffc0000 : 0) | CALL_OFFSET)
646 #define CALLX_N CALL_N
647 #ifdef TARGET_WORDS_BIGENDIAN
648 #define CALLX_M ((b0) & 0x3)
650 #define CALLX_M (((b0) & 0xc0) >> 6)
652 #define CALLX_S RRR_S
654 #define BRI12_M CALLX_M
655 #define BRI12_S RRR_S
656 #ifdef TARGET_WORDS_BIGENDIAN
657 #define BRI12_IMM12 ((((b1) & 0xf) << 8) | (b2))
659 #define BRI12_IMM12 ((((b1) & 0xf0) >> 4) | ((b2) << 4))
661 #define BRI12_IMM12_SE (((BRI12_IMM12 & 0x800) ? 0xfffff000 : 0) | BRI12_IMM12)
663 #define BRI8_M BRI12_M
664 #define BRI8_R RRI8_R
665 #define BRI8_S RRI8_S
666 #define BRI8_IMM8 RRI8_IMM8
667 #define BRI8_IMM8_SE RRI8_IMM8_SE
671 uint8_t b0 = ldub_code(dc->pc);
672 uint8_t b1 = ldub_code(dc->pc + 1);
673 uint8_t b2 = ldub_code(dc->pc + 2);
675 static const uint32_t B4CONST[] = {
676 0xffffffff, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 16, 32, 64, 128, 256
679 static const uint32_t B4CONSTU[] = {
680 32768, 65536, 2, 3, 4, 5, 6, 7, 8, 10, 12, 16, 32, 64, 128, 256
684 dc->next_pc = dc->pc + 2;
685 HAS_OPTION(XTENSA_OPTION_CODE_DENSITY);
687 dc->next_pc = dc->pc + 3;
696 if ((RRR_R & 0xc) == 0x8) {
697 HAS_OPTION(XTENSA_OPTION_BOOLEAN);
704 gen_exception_cause(dc, ILLEGAL_INSTRUCTION_CAUSE);
715 gen_window_check1(dc, CALLX_S);
716 gen_jump(dc, cpu_R[CALLX_S]);
720 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER);
722 TCGv_i32 tmp = tcg_const_i32(dc->pc);
723 gen_advance_ccount(dc);
724 gen_helper_retw(tmp, tmp);
737 gen_window_check2(dc, CALLX_S, CALLX_N << 2);
741 TCGv_i32 tmp = tcg_temp_new_i32();
742 tcg_gen_mov_i32(tmp, cpu_R[CALLX_S]);
743 tcg_gen_movi_i32(cpu_R[0], dc->next_pc);
752 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER);
754 TCGv_i32 tmp = tcg_temp_new_i32();
756 tcg_gen_mov_i32(tmp, cpu_R[CALLX_S]);
757 gen_callw(dc, CALLX_N, tmp);
767 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER);
768 gen_window_check2(dc, RRR_T, RRR_S);
770 TCGv_i32 pc = tcg_const_i32(dc->pc);
771 gen_advance_ccount(dc);
772 gen_helper_movsp(pc);
773 tcg_gen_mov_i32(cpu_R[RRR_T], cpu_R[RRR_S]);
793 HAS_OPTION(XTENSA_OPTION_EXCEPTION);
805 default: /*reserved*/
814 HAS_OPTION(XTENSA_OPTION_EXCEPTION);
817 gen_check_privilege(dc);
818 tcg_gen_andi_i32(cpu_SR[PS], cpu_SR[PS], ~PS_EXCM);
819 gen_helper_check_interrupts(cpu_env);
820 gen_jump(dc, cpu_SR[EPC1]);
828 gen_check_privilege(dc);
830 dc->config->ndepc ? DEPC : EPC1]);
835 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER);
836 gen_check_privilege(dc);
838 TCGv_i32 tmp = tcg_const_i32(1);
841 cpu_SR[PS], cpu_SR[PS], ~PS_EXCM);
842 tcg_gen_shl_i32(tmp, tmp, cpu_SR[WINDOW_BASE]);
845 tcg_gen_andc_i32(cpu_SR[WINDOW_START],
846 cpu_SR[WINDOW_START], tmp);
848 tcg_gen_or_i32(cpu_SR[WINDOW_START],
849 cpu_SR[WINDOW_START], tmp);
852 gen_helper_restore_owb();
853 gen_helper_check_interrupts(cpu_env);
854 gen_jump(dc, cpu_SR[EPC1]);
860 default: /*reserved*/
867 HAS_OPTION(XTENSA_OPTION_HIGH_PRIORITY_INTERRUPT);
868 if (RRR_S >= 2 && RRR_S <= dc->config->nlevel) {
869 gen_check_privilege(dc);
870 tcg_gen_mov_i32(cpu_SR[PS],
871 cpu_SR[EPS2 + RRR_S - 2]);
872 gen_helper_check_interrupts(cpu_env);
873 gen_jump(dc, cpu_SR[EPC1 + RRR_S - 1]);
875 qemu_log("RFI %d is illegal\n", RRR_S);
876 gen_exception_cause(dc, ILLEGAL_INSTRUCTION_CAUSE);
884 default: /*reserved*/
892 HAS_OPTION(XTENSA_OPTION_EXCEPTION);
897 HAS_OPTION(XTENSA_OPTION_EXCEPTION);
900 gen_exception_cause(dc, SYSCALL_CAUSE);
904 if (semihosting_enabled) {
905 gen_check_privilege(dc);
906 gen_helper_simcall(cpu_env);
908 qemu_log("SIMCALL but semihosting is disabled\n");
909 gen_exception_cause(dc, ILLEGAL_INSTRUCTION_CAUSE);
920 HAS_OPTION(XTENSA_OPTION_INTERRUPT);
921 gen_check_privilege(dc);
922 gen_window_check1(dc, RRR_T);
923 tcg_gen_mov_i32(cpu_R[RRR_T], cpu_SR[PS]);
924 tcg_gen_andi_i32(cpu_SR[PS], cpu_SR[PS], ~PS_INTLEVEL);
925 tcg_gen_ori_i32(cpu_SR[PS], cpu_SR[PS], RRR_S);
926 gen_helper_check_interrupts(cpu_env);
927 gen_jumpi_check_loop_end(dc, 0);
931 HAS_OPTION(XTENSA_OPTION_INTERRUPT);
932 gen_check_privilege(dc);
933 gen_waiti(dc, RRR_S);
937 HAS_OPTION(XTENSA_OPTION_BOOLEAN);
942 HAS_OPTION(XTENSA_OPTION_BOOLEAN);
947 HAS_OPTION(XTENSA_OPTION_BOOLEAN);
952 HAS_OPTION(XTENSA_OPTION_BOOLEAN);
956 default: /*reserved*/
964 gen_window_check3(dc, RRR_R, RRR_S, RRR_T);
965 tcg_gen_and_i32(cpu_R[RRR_R], cpu_R[RRR_S], cpu_R[RRR_T]);
969 gen_window_check3(dc, RRR_R, RRR_S, RRR_T);
970 tcg_gen_or_i32(cpu_R[RRR_R], cpu_R[RRR_S], cpu_R[RRR_T]);
974 gen_window_check3(dc, RRR_R, RRR_S, RRR_T);
975 tcg_gen_xor_i32(cpu_R[RRR_R], cpu_R[RRR_S], cpu_R[RRR_T]);
981 gen_window_check1(dc, RRR_S);
982 gen_right_shift_sar(dc, cpu_R[RRR_S]);
986 gen_window_check1(dc, RRR_S);
987 gen_left_shift_sar(dc, cpu_R[RRR_S]);
991 gen_window_check1(dc, RRR_S);
993 TCGv_i32 tmp = tcg_temp_new_i32();
994 tcg_gen_shli_i32(tmp, cpu_R[RRR_S], 3);
995 gen_right_shift_sar(dc, tmp);
1001 gen_window_check1(dc, RRR_S);
1003 TCGv_i32 tmp = tcg_temp_new_i32();
1004 tcg_gen_shli_i32(tmp, cpu_R[RRR_S], 3);
1005 gen_left_shift_sar(dc, tmp);
1012 TCGv_i32 tmp = tcg_const_i32(
1013 RRR_S | ((RRR_T & 1) << 4));
1014 gen_right_shift_sar(dc, tmp);
1028 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER);
1029 gen_check_privilege(dc);
1031 TCGv_i32 tmp = tcg_const_i32(
1032 RRR_T | ((RRR_T & 8) ? 0xfffffff0 : 0));
1033 gen_helper_rotw(tmp);
1035 reset_used_window(dc);
1040 HAS_OPTION(XTENSA_OPTION_MISC_OP);
1041 gen_window_check2(dc, RRR_S, RRR_T);
1042 gen_helper_nsa(cpu_R[RRR_T], cpu_R[RRR_S]);
1046 HAS_OPTION(XTENSA_OPTION_MISC_OP);
1047 gen_window_check2(dc, RRR_S, RRR_T);
1048 gen_helper_nsau(cpu_R[RRR_T], cpu_R[RRR_S]);
1051 default: /*reserved*/
1062 gen_window_check2(dc, RRR_R, RRR_T);
1065 tcg_gen_neg_i32(cpu_R[RRR_R], cpu_R[RRR_T]);
1070 int label = gen_new_label();
1071 tcg_gen_mov_i32(cpu_R[RRR_R], cpu_R[RRR_T]);
1072 tcg_gen_brcondi_i32(
1073 TCG_COND_GE, cpu_R[RRR_R], 0, label);
1074 tcg_gen_neg_i32(cpu_R[RRR_R], cpu_R[RRR_T]);
1075 gen_set_label(label);
1079 default: /*reserved*/
1085 case 7: /*reserved*/
1090 gen_window_check3(dc, RRR_R, RRR_S, RRR_T);
1091 tcg_gen_add_i32(cpu_R[RRR_R], cpu_R[RRR_S], cpu_R[RRR_T]);
1097 gen_window_check3(dc, RRR_R, RRR_S, RRR_T);
1099 TCGv_i32 tmp = tcg_temp_new_i32();
1100 tcg_gen_shli_i32(tmp, cpu_R[RRR_S], OP2 - 8);
1101 tcg_gen_add_i32(cpu_R[RRR_R], tmp, cpu_R[RRR_T]);
1107 gen_window_check3(dc, RRR_R, RRR_S, RRR_T);
1108 tcg_gen_sub_i32(cpu_R[RRR_R], cpu_R[RRR_S], cpu_R[RRR_T]);
1114 gen_window_check3(dc, RRR_R, RRR_S, RRR_T);
1116 TCGv_i32 tmp = tcg_temp_new_i32();
1117 tcg_gen_shli_i32(tmp, cpu_R[RRR_S], OP2 - 12);
1118 tcg_gen_sub_i32(cpu_R[RRR_R], tmp, cpu_R[RRR_T]);
1129 gen_window_check2(dc, RRR_R, RRR_S);
1130 tcg_gen_shli_i32(cpu_R[RRR_R], cpu_R[RRR_S],
1131 32 - (RRR_T | ((OP2 & 1) << 4)));
1136 gen_window_check2(dc, RRR_R, RRR_T);
1137 tcg_gen_sari_i32(cpu_R[RRR_R], cpu_R[RRR_T],
1138 RRR_S | ((OP2 & 1) << 4));
1142 gen_window_check2(dc, RRR_R, RRR_T);
1143 tcg_gen_shri_i32(cpu_R[RRR_R], cpu_R[RRR_T], RRR_S);
1148 TCGv_i32 tmp = tcg_temp_new_i32();
1150 gen_check_privilege(dc);
1152 gen_window_check1(dc, RRR_T);
1153 tcg_gen_mov_i32(tmp, cpu_R[RRR_T]);
1154 gen_rsr(dc, cpu_R[RRR_T], RSR_SR);
1155 gen_wsr(dc, RSR_SR, tmp);
1157 if (!sregnames[RSR_SR]) {
1164 * Note: 64 bit ops are used here solely because SAR values
1167 #define gen_shift_reg(cmd, reg) do { \
1168 TCGv_i64 tmp = tcg_temp_new_i64(); \
1169 tcg_gen_extu_i32_i64(tmp, reg); \
1170 tcg_gen_##cmd##_i64(v, v, tmp); \
1171 tcg_gen_trunc_i64_i32(cpu_R[RRR_R], v); \
1172 tcg_temp_free_i64(v); \
1173 tcg_temp_free_i64(tmp); \
1176 #define gen_shift(cmd) gen_shift_reg(cmd, cpu_SR[SAR])
1179 gen_window_check3(dc, RRR_R, RRR_S, RRR_T);
1181 TCGv_i64 v = tcg_temp_new_i64();
1182 tcg_gen_concat_i32_i64(v, cpu_R[RRR_T], cpu_R[RRR_S]);
1188 gen_window_check2(dc, RRR_R, RRR_T);
1190 tcg_gen_shr_i32(cpu_R[RRR_R], cpu_R[RRR_T], cpu_SR[SAR]);
1192 TCGv_i64 v = tcg_temp_new_i64();
1193 tcg_gen_extu_i32_i64(v, cpu_R[RRR_T]);
1199 gen_window_check2(dc, RRR_R, RRR_S);
1200 if (dc->sar_m32_5bit) {
1201 tcg_gen_shl_i32(cpu_R[RRR_R], cpu_R[RRR_S], dc->sar_m32);
1203 TCGv_i64 v = tcg_temp_new_i64();
1204 TCGv_i32 s = tcg_const_i32(32);
1205 tcg_gen_sub_i32(s, s, cpu_SR[SAR]);
1206 tcg_gen_andi_i32(s, s, 0x3f);
1207 tcg_gen_extu_i32_i64(v, cpu_R[RRR_S]);
1208 gen_shift_reg(shl, s);
1214 gen_window_check2(dc, RRR_R, RRR_T);
1216 tcg_gen_sar_i32(cpu_R[RRR_R], cpu_R[RRR_T], cpu_SR[SAR]);
1218 TCGv_i64 v = tcg_temp_new_i64();
1219 tcg_gen_ext_i32_i64(v, cpu_R[RRR_T]);
1224 #undef gen_shift_reg
1227 HAS_OPTION(XTENSA_OPTION_16_BIT_IMUL);
1228 gen_window_check3(dc, RRR_R, RRR_S, RRR_T);
1230 TCGv_i32 v1 = tcg_temp_new_i32();
1231 TCGv_i32 v2 = tcg_temp_new_i32();
1232 tcg_gen_ext16u_i32(v1, cpu_R[RRR_S]);
1233 tcg_gen_ext16u_i32(v2, cpu_R[RRR_T]);
1234 tcg_gen_mul_i32(cpu_R[RRR_R], v1, v2);
1241 HAS_OPTION(XTENSA_OPTION_16_BIT_IMUL);
1242 gen_window_check3(dc, RRR_R, RRR_S, RRR_T);
1244 TCGv_i32 v1 = tcg_temp_new_i32();
1245 TCGv_i32 v2 = tcg_temp_new_i32();
1246 tcg_gen_ext16s_i32(v1, cpu_R[RRR_S]);
1247 tcg_gen_ext16s_i32(v2, cpu_R[RRR_T]);
1248 tcg_gen_mul_i32(cpu_R[RRR_R], v1, v2);
1254 default: /*reserved*/
1261 gen_window_check3(dc, RRR_R, RRR_S, RRR_T);
1264 HAS_OPTION(XTENSA_OPTION_32_BIT_IDIV);
1265 int label = gen_new_label();
1266 tcg_gen_brcondi_i32(TCG_COND_NE, cpu_R[RRR_T], 0, label);
1267 gen_exception_cause(dc, INTEGER_DIVIDE_BY_ZERO_CAUSE);
1268 gen_set_label(label);
1273 HAS_OPTION(XTENSA_OPTION_32_BIT_IMUL);
1274 tcg_gen_mul_i32(cpu_R[RRR_R], cpu_R[RRR_S], cpu_R[RRR_T]);
1279 HAS_OPTION(XTENSA_OPTION_32_BIT_IMUL);
1281 TCGv_i64 r = tcg_temp_new_i64();
1282 TCGv_i64 s = tcg_temp_new_i64();
1283 TCGv_i64 t = tcg_temp_new_i64();
1286 tcg_gen_extu_i32_i64(s, cpu_R[RRR_S]);
1287 tcg_gen_extu_i32_i64(t, cpu_R[RRR_T]);
1289 tcg_gen_ext_i32_i64(s, cpu_R[RRR_S]);
1290 tcg_gen_ext_i32_i64(t, cpu_R[RRR_T]);
1292 tcg_gen_mul_i64(r, s, t);
1293 tcg_gen_shri_i64(r, r, 32);
1294 tcg_gen_trunc_i64_i32(cpu_R[RRR_R], r);
1296 tcg_temp_free_i64(r);
1297 tcg_temp_free_i64(s);
1298 tcg_temp_free_i64(t);
1303 tcg_gen_divu_i32(cpu_R[RRR_R], cpu_R[RRR_S], cpu_R[RRR_T]);
1309 int label1 = gen_new_label();
1310 int label2 = gen_new_label();
1312 tcg_gen_brcondi_i32(TCG_COND_NE, cpu_R[RRR_S], 0x80000000,
1314 tcg_gen_brcondi_i32(TCG_COND_NE, cpu_R[RRR_T], 0xffffffff,
1316 tcg_gen_movi_i32(cpu_R[RRR_R],
1317 OP2 == 13 ? 0x80000000 : 0);
1319 gen_set_label(label1);
1321 tcg_gen_div_i32(cpu_R[RRR_R],
1322 cpu_R[RRR_S], cpu_R[RRR_T]);
1324 tcg_gen_rem_i32(cpu_R[RRR_R],
1325 cpu_R[RRR_S], cpu_R[RRR_T]);
1327 gen_set_label(label2);
1332 tcg_gen_remu_i32(cpu_R[RRR_R], cpu_R[RRR_S], cpu_R[RRR_T]);
1335 default: /*reserved*/
1345 gen_check_privilege(dc);
1347 gen_window_check1(dc, RRR_T);
1348 gen_rsr(dc, cpu_R[RRR_T], RSR_SR);
1349 if (!sregnames[RSR_SR]) {
1356 gen_check_privilege(dc);
1358 gen_window_check1(dc, RRR_T);
1359 gen_wsr(dc, RSR_SR, cpu_R[RRR_T]);
1360 if (!sregnames[RSR_SR]) {
1366 HAS_OPTION(XTENSA_OPTION_MISC_OP);
1367 gen_window_check2(dc, RRR_R, RRR_S);
1369 int shift = 24 - RRR_T;
1372 tcg_gen_ext8s_i32(cpu_R[RRR_R], cpu_R[RRR_S]);
1373 } else if (shift == 16) {
1374 tcg_gen_ext16s_i32(cpu_R[RRR_R], cpu_R[RRR_S]);
1376 TCGv_i32 tmp = tcg_temp_new_i32();
1377 tcg_gen_shli_i32(tmp, cpu_R[RRR_S], shift);
1378 tcg_gen_sari_i32(cpu_R[RRR_R], tmp, shift);
1385 HAS_OPTION(XTENSA_OPTION_MISC_OP);
1386 gen_window_check2(dc, RRR_R, RRR_S);
1388 TCGv_i32 tmp1 = tcg_temp_new_i32();
1389 TCGv_i32 tmp2 = tcg_temp_new_i32();
1390 int label = gen_new_label();
1392 tcg_gen_sari_i32(tmp1, cpu_R[RRR_S], 24 - RRR_T);
1393 tcg_gen_xor_i32(tmp2, tmp1, cpu_R[RRR_S]);
1394 tcg_gen_andi_i32(tmp2, tmp2, 0xffffffff << (RRR_T + 7));
1395 tcg_gen_mov_i32(cpu_R[RRR_R], cpu_R[RRR_S]);
1396 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp2, 0, label);
1398 tcg_gen_sari_i32(tmp1, cpu_R[RRR_S], 31);
1399 tcg_gen_xori_i32(cpu_R[RRR_R], tmp1,
1400 0xffffffff >> (25 - RRR_T));
1402 gen_set_label(label);
1404 tcg_temp_free(tmp1);
1405 tcg_temp_free(tmp2);
1413 HAS_OPTION(XTENSA_OPTION_MISC_OP);
1414 gen_window_check3(dc, RRR_R, RRR_S, RRR_T);
1416 static const TCGCond cond[] = {
1422 int label = gen_new_label();
1424 if (RRR_R != RRR_T) {
1425 tcg_gen_mov_i32(cpu_R[RRR_R], cpu_R[RRR_S]);
1426 tcg_gen_brcond_i32(cond[OP2 - 4],
1427 cpu_R[RRR_S], cpu_R[RRR_T], label);
1428 tcg_gen_mov_i32(cpu_R[RRR_R], cpu_R[RRR_T]);
1430 tcg_gen_brcond_i32(cond[OP2 - 4],
1431 cpu_R[RRR_T], cpu_R[RRR_S], label);
1432 tcg_gen_mov_i32(cpu_R[RRR_R], cpu_R[RRR_S]);
1434 gen_set_label(label);
1442 gen_window_check3(dc, RRR_R, RRR_S, RRR_T);
1444 static const TCGCond cond[] = {
1450 int label = gen_new_label();
1451 tcg_gen_brcondi_i32(cond[OP2 - 8], cpu_R[RRR_T], 0, label);
1452 tcg_gen_mov_i32(cpu_R[RRR_R], cpu_R[RRR_S]);
1453 gen_set_label(label);
1458 HAS_OPTION(XTENSA_OPTION_BOOLEAN);
1463 HAS_OPTION(XTENSA_OPTION_BOOLEAN);
1468 gen_window_check1(dc, RRR_R);
1470 int st = (RRR_S << 4) + RRR_T;
1471 if (uregnames[st]) {
1472 tcg_gen_mov_i32(cpu_R[RRR_R], cpu_UR[st]);
1474 qemu_log("RUR %d not implemented, ", st);
1481 gen_window_check1(dc, RRR_T);
1483 if (uregnames[RSR_SR]) {
1484 tcg_gen_mov_i32(cpu_UR[RSR_SR], cpu_R[RRR_T]);
1486 qemu_log("WUR %d not implemented, ", RSR_SR);
1497 gen_window_check2(dc, RRR_R, RRR_T);
1499 int shiftimm = RRR_S | (OP1 << 4);
1500 int maskimm = (1 << (OP2 + 1)) - 1;
1502 TCGv_i32 tmp = tcg_temp_new_i32();
1503 tcg_gen_shri_i32(tmp, cpu_R[RRR_T], shiftimm);
1504 tcg_gen_andi_i32(cpu_R[RRR_R], tmp, maskimm);
1518 HAS_OPTION(XTENSA_OPTION_COPROCESSOR);
1523 gen_window_check2(dc, RRR_S, RRR_T);
1526 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER);
1527 gen_check_privilege(dc);
1529 TCGv_i32 addr = tcg_temp_new_i32();
1530 tcg_gen_addi_i32(addr, cpu_R[RRR_S],
1531 (0xffffffc0 | (RRR_R << 2)));
1532 tcg_gen_qemu_ld32u(cpu_R[RRR_T], addr, dc->ring);
1533 tcg_temp_free(addr);
1538 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER);
1539 gen_check_privilege(dc);
1541 TCGv_i32 addr = tcg_temp_new_i32();
1542 tcg_gen_addi_i32(addr, cpu_R[RRR_S],
1543 (0xffffffc0 | (RRR_R << 2)));
1544 tcg_gen_qemu_st32(cpu_R[RRR_T], addr, dc->ring);
1545 tcg_temp_free(addr);
1556 HAS_OPTION(XTENSA_OPTION_FP_COPROCESSOR);
1561 HAS_OPTION(XTENSA_OPTION_FP_COPROCESSOR);
1565 default: /*reserved*/
1572 gen_window_check1(dc, RRR_T);
1574 TCGv_i32 tmp = tcg_const_i32(
1575 ((dc->tb->flags & XTENSA_TBFLAG_LITBASE) ?
1576 0 : ((dc->pc + 3) & ~3)) +
1577 (0xfffc0000 | (RI16_IMM16 << 2)));
1579 if (dc->tb->flags & XTENSA_TBFLAG_LITBASE) {
1580 tcg_gen_add_i32(tmp, tmp, dc->litbase);
1582 tcg_gen_qemu_ld32u(cpu_R[RRR_T], tmp, dc->cring);
1588 #define gen_load_store(type, shift) do { \
1589 TCGv_i32 addr = tcg_temp_new_i32(); \
1590 gen_window_check2(dc, RRI8_S, RRI8_T); \
1591 tcg_gen_addi_i32(addr, cpu_R[RRI8_S], RRI8_IMM8 << shift); \
1593 gen_load_store_alignment(dc, shift, addr, false); \
1595 tcg_gen_qemu_##type(cpu_R[RRI8_T], addr, dc->cring); \
1596 tcg_temp_free(addr); \
1601 gen_load_store(ld8u, 0);
1605 gen_load_store(ld16u, 1);
1609 gen_load_store(ld32u, 2);
1613 gen_load_store(st8, 0);
1617 gen_load_store(st16, 1);
1621 gen_load_store(st32, 2);
1626 HAS_OPTION(XTENSA_OPTION_DCACHE);
1657 HAS_OPTION(XTENSA_OPTION_DCACHE_INDEX_LOCK);
1661 HAS_OPTION(XTENSA_OPTION_DCACHE_INDEX_LOCK);
1665 HAS_OPTION(XTENSA_OPTION_DCACHE_INDEX_LOCK);
1669 HAS_OPTION(XTENSA_OPTION_DCACHE);
1673 HAS_OPTION(XTENSA_OPTION_DCACHE);
1676 default: /*reserved*/
1684 HAS_OPTION(XTENSA_OPTION_ICACHE);
1690 HAS_OPTION(XTENSA_OPTION_ICACHE_INDEX_LOCK);
1694 HAS_OPTION(XTENSA_OPTION_ICACHE_INDEX_LOCK);
1698 HAS_OPTION(XTENSA_OPTION_ICACHE_INDEX_LOCK);
1701 default: /*reserved*/
1708 HAS_OPTION(XTENSA_OPTION_ICACHE);
1712 HAS_OPTION(XTENSA_OPTION_ICACHE);
1715 default: /*reserved*/
1722 gen_load_store(ld16s, 1);
1724 #undef gen_load_store
1727 gen_window_check1(dc, RRI8_T);
1728 tcg_gen_movi_i32(cpu_R[RRI8_T],
1729 RRI8_IMM8 | (RRI8_S << 8) |
1730 ((RRI8_S & 0x8) ? 0xfffff000 : 0));
1733 #define gen_load_store_no_hw_align(type) do { \
1734 TCGv_i32 addr = tcg_temp_local_new_i32(); \
1735 gen_window_check2(dc, RRI8_S, RRI8_T); \
1736 tcg_gen_addi_i32(addr, cpu_R[RRI8_S], RRI8_IMM8 << 2); \
1737 gen_load_store_alignment(dc, 2, addr, true); \
1738 tcg_gen_qemu_##type(cpu_R[RRI8_T], addr, dc->cring); \
1739 tcg_temp_free(addr); \
1743 HAS_OPTION(XTENSA_OPTION_MP_SYNCHRO);
1744 gen_load_store_no_hw_align(ld32u); /*TODO acquire?*/
1748 gen_window_check2(dc, RRI8_S, RRI8_T);
1749 tcg_gen_addi_i32(cpu_R[RRI8_T], cpu_R[RRI8_S], RRI8_IMM8_SE);
1753 gen_window_check2(dc, RRI8_S, RRI8_T);
1754 tcg_gen_addi_i32(cpu_R[RRI8_T], cpu_R[RRI8_S], RRI8_IMM8_SE << 8);
1757 case 14: /*S32C1Iy*/
1758 HAS_OPTION(XTENSA_OPTION_MP_SYNCHRO);
1759 gen_window_check2(dc, RRI8_S, RRI8_T);
1761 int label = gen_new_label();
1762 TCGv_i32 tmp = tcg_temp_local_new_i32();
1763 TCGv_i32 addr = tcg_temp_local_new_i32();
1765 tcg_gen_mov_i32(tmp, cpu_R[RRI8_T]);
1766 tcg_gen_addi_i32(addr, cpu_R[RRI8_S], RRI8_IMM8 << 2);
1767 gen_load_store_alignment(dc, 2, addr, true);
1768 tcg_gen_qemu_ld32u(cpu_R[RRI8_T], addr, dc->cring);
1769 tcg_gen_brcond_i32(TCG_COND_NE, cpu_R[RRI8_T],
1770 cpu_SR[SCOMPARE1], label);
1772 tcg_gen_qemu_st32(tmp, addr, dc->cring);
1774 gen_set_label(label);
1775 tcg_temp_free(addr);
1781 HAS_OPTION(XTENSA_OPTION_MP_SYNCHRO);
1782 gen_load_store_no_hw_align(st32); /*TODO release?*/
1784 #undef gen_load_store_no_hw_align
1786 default: /*reserved*/
1793 HAS_OPTION(XTENSA_OPTION_COPROCESSOR);
1798 HAS_OPTION(XTENSA_OPTION_MAC16);
1805 tcg_gen_movi_i32(cpu_R[0], dc->next_pc);
1806 gen_jumpi(dc, (dc->pc & ~3) + (CALL_OFFSET_SE << 2) + 4, 0);
1812 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER);
1813 gen_window_check1(dc, CALL_N << 2);
1814 gen_callwi(dc, CALL_N,
1815 (dc->pc & ~3) + (CALL_OFFSET_SE << 2) + 4, 0);
1823 gen_jumpi(dc, dc->pc + 4 + CALL_OFFSET_SE, 0);
1827 gen_window_check1(dc, BRI12_S);
1829 static const TCGCond cond[] = {
1830 TCG_COND_EQ, /*BEQZ*/
1831 TCG_COND_NE, /*BNEZ*/
1832 TCG_COND_LT, /*BLTZ*/
1833 TCG_COND_GE, /*BGEZ*/
1836 gen_brcondi(dc, cond[BRI12_M & 3], cpu_R[BRI12_S], 0,
1837 4 + BRI12_IMM12_SE);
1842 gen_window_check1(dc, BRI8_S);
1844 static const TCGCond cond[] = {
1845 TCG_COND_EQ, /*BEQI*/
1846 TCG_COND_NE, /*BNEI*/
1847 TCG_COND_LT, /*BLTI*/
1848 TCG_COND_GE, /*BGEI*/
1851 gen_brcondi(dc, cond[BRI8_M & 3],
1852 cpu_R[BRI8_S], B4CONST[BRI8_R], 4 + BRI8_IMM8_SE);
1859 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER);
1861 TCGv_i32 pc = tcg_const_i32(dc->pc);
1862 TCGv_i32 s = tcg_const_i32(BRI12_S);
1863 TCGv_i32 imm = tcg_const_i32(BRI12_IMM12);
1864 gen_advance_ccount(dc);
1865 gen_helper_entry(pc, s, imm);
1869 reset_used_window(dc);
1876 HAS_OPTION(XTENSA_OPTION_BOOLEAN);
1881 HAS_OPTION(XTENSA_OPTION_BOOLEAN);
1887 case 10: /*LOOPGTZ*/
1888 HAS_OPTION(XTENSA_OPTION_LOOP);
1889 gen_window_check1(dc, RRI8_S);
1891 uint32_t lend = dc->pc + RRI8_IMM8 + 4;
1892 TCGv_i32 tmp = tcg_const_i32(lend);
1894 tcg_gen_subi_i32(cpu_SR[LCOUNT], cpu_R[RRI8_S], 1);
1895 tcg_gen_movi_i32(cpu_SR[LBEG], dc->next_pc);
1896 gen_wsr_lend(dc, LEND, tmp);
1900 int label = gen_new_label();
1901 tcg_gen_brcondi_i32(
1902 BRI8_R == 9 ? TCG_COND_NE : TCG_COND_GT,
1903 cpu_R[RRI8_S], 0, label);
1904 gen_jumpi(dc, lend, 1);
1905 gen_set_label(label);
1908 gen_jumpi(dc, dc->next_pc, 0);
1912 default: /*reserved*/
1921 gen_window_check1(dc, BRI8_S);
1922 gen_brcondi(dc, BRI8_M == 2 ? TCG_COND_LTU : TCG_COND_GEU,
1923 cpu_R[BRI8_S], B4CONSTU[BRI8_R], 4 + BRI8_IMM8_SE);
1933 TCGCond eq_ne = (RRI8_R & 8) ? TCG_COND_NE : TCG_COND_EQ;
1935 switch (RRI8_R & 7) {
1936 case 0: /*BNONE*/ /*BANY*/
1937 gen_window_check2(dc, RRI8_S, RRI8_T);
1939 TCGv_i32 tmp = tcg_temp_new_i32();
1940 tcg_gen_and_i32(tmp, cpu_R[RRI8_S], cpu_R[RRI8_T]);
1941 gen_brcondi(dc, eq_ne, tmp, 0, 4 + RRI8_IMM8_SE);
1946 case 1: /*BEQ*/ /*BNE*/
1947 case 2: /*BLT*/ /*BGE*/
1948 case 3: /*BLTU*/ /*BGEU*/
1949 gen_window_check2(dc, RRI8_S, RRI8_T);
1951 static const TCGCond cond[] = {
1957 [11] = TCG_COND_GEU,
1959 gen_brcond(dc, cond[RRI8_R], cpu_R[RRI8_S], cpu_R[RRI8_T],
1964 case 4: /*BALL*/ /*BNALL*/
1965 gen_window_check2(dc, RRI8_S, RRI8_T);
1967 TCGv_i32 tmp = tcg_temp_new_i32();
1968 tcg_gen_and_i32(tmp, cpu_R[RRI8_S], cpu_R[RRI8_T]);
1969 gen_brcond(dc, eq_ne, tmp, cpu_R[RRI8_T],
1975 case 5: /*BBC*/ /*BBS*/
1976 gen_window_check2(dc, RRI8_S, RRI8_T);
1978 TCGv_i32 bit = tcg_const_i32(1);
1979 TCGv_i32 tmp = tcg_temp_new_i32();
1980 tcg_gen_andi_i32(tmp, cpu_R[RRI8_T], 0x1f);
1981 tcg_gen_shl_i32(bit, bit, tmp);
1982 tcg_gen_and_i32(tmp, cpu_R[RRI8_S], bit);
1983 gen_brcondi(dc, eq_ne, tmp, 0, 4 + RRI8_IMM8_SE);
1989 case 6: /*BBCI*/ /*BBSI*/
1991 gen_window_check1(dc, RRI8_S);
1993 TCGv_i32 tmp = tcg_temp_new_i32();
1994 tcg_gen_andi_i32(tmp, cpu_R[RRI8_S],
1995 1 << (((RRI8_R & 1) << 4) | RRI8_T));
1996 gen_brcondi(dc, eq_ne, tmp, 0, 4 + RRI8_IMM8_SE);
2005 #define gen_narrow_load_store(type) do { \
2006 TCGv_i32 addr = tcg_temp_new_i32(); \
2007 gen_window_check2(dc, RRRN_S, RRRN_T); \
2008 tcg_gen_addi_i32(addr, cpu_R[RRRN_S], RRRN_R << 2); \
2009 gen_load_store_alignment(dc, 2, addr, false); \
2010 tcg_gen_qemu_##type(cpu_R[RRRN_T], addr, dc->cring); \
2011 tcg_temp_free(addr); \
2015 gen_narrow_load_store(ld32u);
2019 gen_narrow_load_store(st32);
2021 #undef gen_narrow_load_store
2024 gen_window_check3(dc, RRRN_R, RRRN_S, RRRN_T);
2025 tcg_gen_add_i32(cpu_R[RRRN_R], cpu_R[RRRN_S], cpu_R[RRRN_T]);
2028 case 11: /*ADDI.Nn*/
2029 gen_window_check2(dc, RRRN_R, RRRN_S);
2030 tcg_gen_addi_i32(cpu_R[RRRN_R], cpu_R[RRRN_S], RRRN_T ? RRRN_T : -1);
2034 gen_window_check1(dc, RRRN_S);
2035 if (RRRN_T < 8) { /*MOVI.Nn*/
2036 tcg_gen_movi_i32(cpu_R[RRRN_S],
2037 RRRN_R | (RRRN_T << 4) |
2038 ((RRRN_T & 6) == 6 ? 0xffffff80 : 0));
2039 } else { /*BEQZ.Nn*/ /*BNEZ.Nn*/
2040 TCGCond eq_ne = (RRRN_T & 4) ? TCG_COND_NE : TCG_COND_EQ;
2042 gen_brcondi(dc, eq_ne, cpu_R[RRRN_S], 0,
2043 4 + (RRRN_R | ((RRRN_T & 3) << 4)));
2050 gen_window_check2(dc, RRRN_S, RRRN_T);
2051 tcg_gen_mov_i32(cpu_R[RRRN_T], cpu_R[RRRN_S]);
2057 gen_jump(dc, cpu_R[0]);
2061 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER);
2063 TCGv_i32 tmp = tcg_const_i32(dc->pc);
2064 gen_advance_ccount(dc);
2065 gen_helper_retw(tmp, tmp);
2071 case 2: /*BREAK.Nn*/
2079 gen_exception_cause(dc, ILLEGAL_INSTRUCTION_CAUSE);
2082 default: /*reserved*/
2088 default: /*reserved*/
2094 default: /*reserved*/
2099 gen_check_loop_end(dc, 0);
2100 dc->pc = dc->next_pc;
2105 qemu_log("INVALID(pc = %08x)\n", dc->pc);
2106 dc->pc = dc->next_pc;
2110 static void check_breakpoint(CPUState *env, DisasContext *dc)
2114 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
2115 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
2116 if (bp->pc == dc->pc) {
2117 tcg_gen_movi_i32(cpu_pc, dc->pc);
2118 gen_exception(dc, EXCP_DEBUG);
2119 dc->is_jmp = DISAS_UPDATE;
2125 static void gen_intermediate_code_internal(
2126 CPUState *env, TranslationBlock *tb, int search_pc)
2131 uint16_t *gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
2132 int max_insns = tb->cflags & CF_COUNT_MASK;
2133 uint32_t pc_start = tb->pc;
2134 uint32_t next_page_start =
2135 (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
2137 if (max_insns == 0) {
2138 max_insns = CF_COUNT_MASK;
2141 dc.config = env->config;
2142 dc.singlestep_enabled = env->singlestep_enabled;
2145 dc.ring = tb->flags & XTENSA_TBFLAG_RING_MASK;
2146 dc.cring = (tb->flags & XTENSA_TBFLAG_EXCM) ? 0 : dc.ring;
2147 dc.lbeg = env->sregs[LBEG];
2148 dc.lend = env->sregs[LEND];
2149 dc.is_jmp = DISAS_NEXT;
2150 dc.ccount_delta = 0;
2153 init_sar_tracker(&dc);
2154 reset_used_window(&dc);
2158 if (env->singlestep_enabled && env->exception_taken) {
2159 env->exception_taken = 0;
2160 tcg_gen_movi_i32(cpu_pc, dc.pc);
2161 gen_exception(&dc, EXCP_DEBUG);
2165 check_breakpoint(env, &dc);
2168 j = gen_opc_ptr - gen_opc_buf;
2172 gen_opc_instr_start[lj++] = 0;
2175 gen_opc_pc[lj] = dc.pc;
2176 gen_opc_instr_start[lj] = 1;
2177 gen_opc_icount[lj] = insn_count;
2180 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP))) {
2181 tcg_gen_debug_insn_start(dc.pc);
2186 if (insn_count + 1 == max_insns && (tb->cflags & CF_LAST_IO)) {
2190 disas_xtensa_insn(&dc);
2192 if (env->singlestep_enabled) {
2193 tcg_gen_movi_i32(cpu_pc, dc.pc);
2194 gen_exception(&dc, EXCP_DEBUG);
2197 } while (dc.is_jmp == DISAS_NEXT &&
2198 insn_count < max_insns &&
2199 dc.pc < next_page_start &&
2200 gen_opc_ptr < gen_opc_end);
2203 reset_sar_tracker(&dc);
2205 if (tb->cflags & CF_LAST_IO) {
2209 if (dc.is_jmp == DISAS_NEXT) {
2210 gen_jumpi(&dc, dc.pc, 0);
2212 gen_icount_end(tb, insn_count);
2213 *gen_opc_ptr = INDEX_op_end;
2216 tb->size = dc.pc - pc_start;
2217 tb->icount = insn_count;
2221 void gen_intermediate_code(CPUState *env, TranslationBlock *tb)
2223 gen_intermediate_code_internal(env, tb, 0);
2226 void gen_intermediate_code_pc(CPUState *env, TranslationBlock *tb)
2228 gen_intermediate_code_internal(env, tb, 1);
2231 void cpu_dump_state(CPUState *env, FILE *f, fprintf_function cpu_fprintf,
2236 cpu_fprintf(f, "PC=%08x\n\n", env->pc);
2238 for (i = j = 0; i < 256; ++i) {
2240 cpu_fprintf(f, "%s=%08x%c", sregnames[i], env->sregs[i],
2241 (j++ % 4) == 3 ? '\n' : ' ');
2245 cpu_fprintf(f, (j % 4) == 0 ? "\n" : "\n\n");
2247 for (i = j = 0; i < 256; ++i) {
2249 cpu_fprintf(f, "%s=%08x%c", uregnames[i], env->uregs[i],
2250 (j++ % 4) == 3 ? '\n' : ' ');
2254 cpu_fprintf(f, (j % 4) == 0 ? "\n" : "\n\n");
2256 for (i = 0; i < 16; ++i) {
2257 cpu_fprintf(f, "A%02d=%08x%c", i, env->regs[i],
2258 (i % 4) == 3 ? '\n' : ' ');
2261 cpu_fprintf(f, "\n");
2263 for (i = 0; i < env->config->nareg; ++i) {
2264 cpu_fprintf(f, "AR%02d=%08x%c", i, env->phys_regs[i],
2265 (i % 4) == 3 ? '\n' : ' ');
2269 void restore_state_to_opc(CPUState *env, TranslationBlock *tb, int pc_pos)
2271 env->pc = gen_opc_pc[pc_pos];