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",
105 [INTCLEAR
] = "INTCLEAR",
106 [INTENABLE
] = "INTENABLE",
108 [EXCCAUSE
] = "EXCCAUSE",
110 [EXCVADDR
] = "EXCVADDR",
111 [CCOMPARE
] = "CCOMPARE0",
112 [CCOMPARE
+ 1] = "CCOMPARE1",
113 [CCOMPARE
+ 2] = "CCOMPARE2",
116 static const char * const uregnames
[256] = {
117 [THREADPTR
] = "THREADPTR",
122 void xtensa_translate_init(void)
124 static const char * const regnames
[] = {
125 "ar0", "ar1", "ar2", "ar3",
126 "ar4", "ar5", "ar6", "ar7",
127 "ar8", "ar9", "ar10", "ar11",
128 "ar12", "ar13", "ar14", "ar15",
132 cpu_env
= tcg_global_reg_new_ptr(TCG_AREG0
, "env");
133 cpu_pc
= tcg_global_mem_new_i32(TCG_AREG0
,
134 offsetof(CPUState
, pc
), "pc");
136 for (i
= 0; i
< 16; i
++) {
137 cpu_R
[i
] = tcg_global_mem_new_i32(TCG_AREG0
,
138 offsetof(CPUState
, regs
[i
]),
142 for (i
= 0; i
< 256; ++i
) {
144 cpu_SR
[i
] = tcg_global_mem_new_i32(TCG_AREG0
,
145 offsetof(CPUState
, sregs
[i
]),
150 for (i
= 0; i
< 256; ++i
) {
152 cpu_UR
[i
] = tcg_global_mem_new_i32(TCG_AREG0
,
153 offsetof(CPUState
, uregs
[i
]),
161 static inline bool option_enabled(DisasContext
*dc
, int opt
)
163 return xtensa_option_enabled(dc
->config
, opt
);
166 static void init_litbase(DisasContext
*dc
)
168 if (dc
->tb
->flags
& XTENSA_TBFLAG_LITBASE
) {
169 dc
->litbase
= tcg_temp_local_new_i32();
170 tcg_gen_andi_i32(dc
->litbase
, cpu_SR
[LITBASE
], 0xfffff000);
174 static void reset_litbase(DisasContext
*dc
)
176 if (dc
->tb
->flags
& XTENSA_TBFLAG_LITBASE
) {
177 tcg_temp_free(dc
->litbase
);
181 static void init_sar_tracker(DisasContext
*dc
)
183 dc
->sar_5bit
= false;
184 dc
->sar_m32_5bit
= false;
185 dc
->sar_m32_allocated
= false;
188 static void reset_sar_tracker(DisasContext
*dc
)
190 if (dc
->sar_m32_allocated
) {
191 tcg_temp_free(dc
->sar_m32
);
195 static void gen_right_shift_sar(DisasContext
*dc
, TCGv_i32 sa
)
197 tcg_gen_andi_i32(cpu_SR
[SAR
], sa
, 0x1f);
198 if (dc
->sar_m32_5bit
) {
199 tcg_gen_discard_i32(dc
->sar_m32
);
202 dc
->sar_m32_5bit
= false;
205 static void gen_left_shift_sar(DisasContext
*dc
, TCGv_i32 sa
)
207 TCGv_i32 tmp
= tcg_const_i32(32);
208 if (!dc
->sar_m32_allocated
) {
209 dc
->sar_m32
= tcg_temp_local_new_i32();
210 dc
->sar_m32_allocated
= true;
212 tcg_gen_andi_i32(dc
->sar_m32
, sa
, 0x1f);
213 tcg_gen_sub_i32(cpu_SR
[SAR
], tmp
, dc
->sar_m32
);
214 dc
->sar_5bit
= false;
215 dc
->sar_m32_5bit
= true;
219 static void gen_advance_ccount(DisasContext
*dc
)
221 if (dc
->ccount_delta
> 0) {
222 TCGv_i32 tmp
= tcg_const_i32(dc
->ccount_delta
);
223 dc
->ccount_delta
= 0;
224 gen_helper_advance_ccount(tmp
);
229 static void reset_used_window(DisasContext
*dc
)
234 static void gen_exception(DisasContext
*dc
, int excp
)
236 TCGv_i32 tmp
= tcg_const_i32(excp
);
237 gen_advance_ccount(dc
);
238 gen_helper_exception(tmp
);
242 static void gen_exception_cause(DisasContext
*dc
, uint32_t cause
)
244 TCGv_i32 tpc
= tcg_const_i32(dc
->pc
);
245 TCGv_i32 tcause
= tcg_const_i32(cause
);
246 gen_advance_ccount(dc
);
247 gen_helper_exception_cause(tpc
, tcause
);
249 tcg_temp_free(tcause
);
252 static void gen_exception_cause_vaddr(DisasContext
*dc
, uint32_t cause
,
255 TCGv_i32 tpc
= tcg_const_i32(dc
->pc
);
256 TCGv_i32 tcause
= tcg_const_i32(cause
);
257 gen_advance_ccount(dc
);
258 gen_helper_exception_cause_vaddr(tpc
, tcause
, vaddr
);
260 tcg_temp_free(tcause
);
263 static void gen_check_privilege(DisasContext
*dc
)
266 gen_exception_cause(dc
, PRIVILEGED_CAUSE
);
270 static void gen_jump_slot(DisasContext
*dc
, TCGv dest
, int slot
)
272 tcg_gen_mov_i32(cpu_pc
, dest
);
273 if (dc
->singlestep_enabled
) {
274 gen_exception(dc
, EXCP_DEBUG
);
276 gen_advance_ccount(dc
);
278 tcg_gen_goto_tb(slot
);
279 tcg_gen_exit_tb((tcg_target_long
)dc
->tb
+ slot
);
284 dc
->is_jmp
= DISAS_UPDATE
;
287 static void gen_jump(DisasContext
*dc
, TCGv dest
)
289 gen_jump_slot(dc
, dest
, -1);
292 static void gen_jumpi(DisasContext
*dc
, uint32_t dest
, int slot
)
294 TCGv_i32 tmp
= tcg_const_i32(dest
);
295 if (((dc
->pc
^ dest
) & TARGET_PAGE_MASK
) != 0) {
298 gen_jump_slot(dc
, tmp
, slot
);
302 static void gen_callw_slot(DisasContext
*dc
, int callinc
, TCGv_i32 dest
,
305 TCGv_i32 tcallinc
= tcg_const_i32(callinc
);
307 tcg_gen_deposit_i32(cpu_SR
[PS
], cpu_SR
[PS
],
308 tcallinc
, PS_CALLINC_SHIFT
, PS_CALLINC_LEN
);
309 tcg_temp_free(tcallinc
);
310 tcg_gen_movi_i32(cpu_R
[callinc
<< 2],
311 (callinc
<< 30) | (dc
->next_pc
& 0x3fffffff));
312 gen_jump_slot(dc
, dest
, slot
);
315 static void gen_callw(DisasContext
*dc
, int callinc
, TCGv_i32 dest
)
317 gen_callw_slot(dc
, callinc
, dest
, -1);
320 static void gen_callwi(DisasContext
*dc
, int callinc
, uint32_t dest
, int slot
)
322 TCGv_i32 tmp
= tcg_const_i32(dest
);
323 if (((dc
->pc
^ dest
) & TARGET_PAGE_MASK
) != 0) {
326 gen_callw_slot(dc
, callinc
, tmp
, slot
);
330 static bool gen_check_loop_end(DisasContext
*dc
, int slot
)
332 if (option_enabled(dc
, XTENSA_OPTION_LOOP
) &&
333 !(dc
->tb
->flags
& XTENSA_TBFLAG_EXCM
) &&
334 dc
->next_pc
== dc
->lend
) {
335 int label
= gen_new_label();
337 tcg_gen_brcondi_i32(TCG_COND_EQ
, cpu_SR
[LCOUNT
], 0, label
);
338 tcg_gen_subi_i32(cpu_SR
[LCOUNT
], cpu_SR
[LCOUNT
], 1);
339 gen_jumpi(dc
, dc
->lbeg
, slot
);
340 gen_set_label(label
);
341 gen_jumpi(dc
, dc
->next_pc
, -1);
347 static void gen_jumpi_check_loop_end(DisasContext
*dc
, int slot
)
349 if (!gen_check_loop_end(dc
, slot
)) {
350 gen_jumpi(dc
, dc
->next_pc
, slot
);
354 static void gen_brcond(DisasContext
*dc
, TCGCond cond
,
355 TCGv_i32 t0
, TCGv_i32 t1
, uint32_t offset
)
357 int label
= gen_new_label();
359 tcg_gen_brcond_i32(cond
, t0
, t1
, label
);
360 gen_jumpi_check_loop_end(dc
, 0);
361 gen_set_label(label
);
362 gen_jumpi(dc
, dc
->pc
+ offset
, 1);
365 static void gen_brcondi(DisasContext
*dc
, TCGCond cond
,
366 TCGv_i32 t0
, uint32_t t1
, uint32_t offset
)
368 TCGv_i32 tmp
= tcg_const_i32(t1
);
369 gen_brcond(dc
, cond
, t0
, tmp
, offset
);
373 static void gen_rsr_ccount(DisasContext
*dc
, TCGv_i32 d
, uint32_t sr
)
375 gen_advance_ccount(dc
);
376 tcg_gen_mov_i32(d
, cpu_SR
[sr
]);
379 static void gen_rsr(DisasContext
*dc
, TCGv_i32 d
, uint32_t sr
)
381 static void (* const rsr_handler
[256])(DisasContext
*dc
,
382 TCGv_i32 d
, uint32_t sr
) = {
383 [CCOUNT
] = gen_rsr_ccount
,
387 if (rsr_handler
[sr
]) {
388 rsr_handler
[sr
](dc
, d
, sr
);
390 tcg_gen_mov_i32(d
, cpu_SR
[sr
]);
393 qemu_log("RSR %d not implemented, ", sr
);
397 static void gen_wsr_lbeg(DisasContext
*dc
, uint32_t sr
, TCGv_i32 s
)
399 gen_helper_wsr_lbeg(s
);
402 static void gen_wsr_lend(DisasContext
*dc
, uint32_t sr
, TCGv_i32 s
)
404 gen_helper_wsr_lend(s
);
407 static void gen_wsr_sar(DisasContext
*dc
, uint32_t sr
, TCGv_i32 s
)
409 tcg_gen_andi_i32(cpu_SR
[sr
], s
, 0x3f);
410 if (dc
->sar_m32_5bit
) {
411 tcg_gen_discard_i32(dc
->sar_m32
);
413 dc
->sar_5bit
= false;
414 dc
->sar_m32_5bit
= false;
417 static void gen_wsr_litbase(DisasContext
*dc
, uint32_t sr
, TCGv_i32 s
)
419 tcg_gen_andi_i32(cpu_SR
[sr
], s
, 0xfffff001);
420 /* This can change tb->flags, so exit tb */
421 gen_jumpi_check_loop_end(dc
, -1);
424 static void gen_wsr_windowbase(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
426 gen_helper_wsr_windowbase(v
);
427 reset_used_window(dc
);
430 static void gen_wsr_windowstart(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
432 tcg_gen_mov_i32(cpu_SR
[sr
], v
);
433 reset_used_window(dc
);
436 static void gen_wsr_intset(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
438 tcg_gen_andi_i32(cpu_SR
[sr
], v
,
439 dc
->config
->inttype_mask
[INTTYPE_SOFTWARE
]);
440 gen_helper_check_interrupts(cpu_env
);
441 gen_jumpi_check_loop_end(dc
, 0);
444 static void gen_wsr_intclear(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
446 TCGv_i32 tmp
= tcg_temp_new_i32();
448 tcg_gen_andi_i32(tmp
, v
,
449 dc
->config
->inttype_mask
[INTTYPE_EDGE
] |
450 dc
->config
->inttype_mask
[INTTYPE_NMI
] |
451 dc
->config
->inttype_mask
[INTTYPE_SOFTWARE
]);
452 tcg_gen_andc_i32(cpu_SR
[INTSET
], cpu_SR
[INTSET
], tmp
);
454 gen_helper_check_interrupts(cpu_env
);
457 static void gen_wsr_intenable(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
459 tcg_gen_mov_i32(cpu_SR
[sr
], v
);
460 gen_helper_check_interrupts(cpu_env
);
461 gen_jumpi_check_loop_end(dc
, 0);
464 static void gen_wsr_ps(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
466 uint32_t mask
= PS_WOE
| PS_CALLINC
| PS_OWB
|
467 PS_UM
| PS_EXCM
| PS_INTLEVEL
;
469 if (option_enabled(dc
, XTENSA_OPTION_MMU
)) {
472 tcg_gen_andi_i32(cpu_SR
[sr
], v
, mask
);
473 reset_used_window(dc
);
474 gen_helper_check_interrupts(cpu_env
);
475 /* This can change mmu index and tb->flags, so exit tb */
476 gen_jumpi_check_loop_end(dc
, -1);
479 static void gen_wsr_ccompare(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
481 uint32_t id
= sr
- CCOMPARE
;
482 if (id
< dc
->config
->nccompare
) {
483 uint32_t int_bit
= 1 << dc
->config
->timerint
[id
];
484 gen_advance_ccount(dc
);
485 tcg_gen_mov_i32(cpu_SR
[sr
], v
);
486 tcg_gen_andi_i32(cpu_SR
[INTSET
], cpu_SR
[INTSET
], ~int_bit
);
487 gen_helper_check_interrupts(cpu_env
);
491 static void gen_wsr(DisasContext
*dc
, uint32_t sr
, TCGv_i32 s
)
493 static void (* const wsr_handler
[256])(DisasContext
*dc
,
494 uint32_t sr
, TCGv_i32 v
) = {
495 [LBEG
] = gen_wsr_lbeg
,
496 [LEND
] = gen_wsr_lend
,
498 [LITBASE
] = gen_wsr_litbase
,
499 [WINDOW_BASE
] = gen_wsr_windowbase
,
500 [WINDOW_START
] = gen_wsr_windowstart
,
501 [INTSET
] = gen_wsr_intset
,
502 [INTCLEAR
] = gen_wsr_intclear
,
503 [INTENABLE
] = gen_wsr_intenable
,
505 [CCOMPARE
] = gen_wsr_ccompare
,
506 [CCOMPARE
+ 1] = gen_wsr_ccompare
,
507 [CCOMPARE
+ 2] = gen_wsr_ccompare
,
511 if (wsr_handler
[sr
]) {
512 wsr_handler
[sr
](dc
, sr
, s
);
514 tcg_gen_mov_i32(cpu_SR
[sr
], s
);
517 qemu_log("WSR %d not implemented, ", sr
);
521 static void gen_load_store_alignment(DisasContext
*dc
, int shift
,
522 TCGv_i32 addr
, bool no_hw_alignment
)
524 if (!option_enabled(dc
, XTENSA_OPTION_UNALIGNED_EXCEPTION
)) {
525 tcg_gen_andi_i32(addr
, addr
, ~0 << shift
);
526 } else if (option_enabled(dc
, XTENSA_OPTION_HW_ALIGNMENT
) &&
528 int label
= gen_new_label();
529 TCGv_i32 tmp
= tcg_temp_new_i32();
530 tcg_gen_andi_i32(tmp
, addr
, ~(~0 << shift
));
531 tcg_gen_brcondi_i32(TCG_COND_EQ
, tmp
, 0, label
);
532 gen_exception_cause_vaddr(dc
, LOAD_STORE_ALIGNMENT_CAUSE
, addr
);
533 gen_set_label(label
);
538 static void gen_waiti(DisasContext
*dc
, uint32_t imm4
)
540 TCGv_i32 pc
= tcg_const_i32(dc
->next_pc
);
541 TCGv_i32 intlevel
= tcg_const_i32(imm4
);
542 gen_advance_ccount(dc
);
543 gen_helper_waiti(pc
, intlevel
);
545 tcg_temp_free(intlevel
);
548 static void gen_window_check1(DisasContext
*dc
, unsigned r1
)
550 if (dc
->tb
->flags
& XTENSA_TBFLAG_EXCM
) {
553 if (option_enabled(dc
, XTENSA_OPTION_WINDOWED_REGISTER
) &&
554 r1
/ 4 > dc
->used_window
) {
555 TCGv_i32 pc
= tcg_const_i32(dc
->pc
);
556 TCGv_i32 w
= tcg_const_i32(r1
/ 4);
558 dc
->used_window
= r1
/ 4;
559 gen_advance_ccount(dc
);
560 gen_helper_window_check(pc
, w
);
567 static void gen_window_check2(DisasContext
*dc
, unsigned r1
, unsigned r2
)
569 gen_window_check1(dc
, r1
> r2
? r1
: r2
);
572 static void gen_window_check3(DisasContext
*dc
, unsigned r1
, unsigned r2
,
575 gen_window_check2(dc
, r1
, r2
> r3
? r2
: r3
);
578 static void disas_xtensa_insn(DisasContext
*dc
)
580 #define HAS_OPTION(opt) do { \
581 if (!option_enabled(dc, opt)) { \
582 qemu_log("Option %d is not enabled %s:%d\n", \
583 (opt), __FILE__, __LINE__); \
584 goto invalid_opcode; \
588 #define TBD() qemu_log("TBD(pc = %08x): %s:%d\n", dc->pc, __FILE__, __LINE__)
589 #define RESERVED() do { \
590 qemu_log("RESERVED(pc = %08x, %02x%02x%02x): %s:%d\n", \
591 dc->pc, b0, b1, b2, __FILE__, __LINE__); \
592 goto invalid_opcode; \
596 #ifdef TARGET_WORDS_BIGENDIAN
597 #define OP0 (((b0) & 0xf0) >> 4)
598 #define OP1 (((b2) & 0xf0) >> 4)
599 #define OP2 ((b2) & 0xf)
600 #define RRR_R ((b1) & 0xf)
601 #define RRR_S (((b1) & 0xf0) >> 4)
602 #define RRR_T ((b0) & 0xf)
604 #define OP0 (((b0) & 0xf))
605 #define OP1 (((b2) & 0xf))
606 #define OP2 (((b2) & 0xf0) >> 4)
607 #define RRR_R (((b1) & 0xf0) >> 4)
608 #define RRR_S (((b1) & 0xf))
609 #define RRR_T (((b0) & 0xf0) >> 4)
619 #define RRI8_IMM8 (b2)
620 #define RRI8_IMM8_SE ((((b2) & 0x80) ? 0xffffff00 : 0) | RRI8_IMM8)
622 #ifdef TARGET_WORDS_BIGENDIAN
623 #define RI16_IMM16 (((b1) << 8) | (b2))
625 #define RI16_IMM16 (((b2) << 8) | (b1))
628 #ifdef TARGET_WORDS_BIGENDIAN
629 #define CALL_N (((b0) & 0xc) >> 2)
630 #define CALL_OFFSET ((((b0) & 0x3) << 16) | ((b1) << 8) | (b2))
632 #define CALL_N (((b0) & 0x30) >> 4)
633 #define CALL_OFFSET ((((b0) & 0xc0) >> 6) | ((b1) << 2) | ((b2) << 10))
635 #define CALL_OFFSET_SE \
636 (((CALL_OFFSET & 0x20000) ? 0xfffc0000 : 0) | CALL_OFFSET)
638 #define CALLX_N CALL_N
639 #ifdef TARGET_WORDS_BIGENDIAN
640 #define CALLX_M ((b0) & 0x3)
642 #define CALLX_M (((b0) & 0xc0) >> 6)
644 #define CALLX_S RRR_S
646 #define BRI12_M CALLX_M
647 #define BRI12_S RRR_S
648 #ifdef TARGET_WORDS_BIGENDIAN
649 #define BRI12_IMM12 ((((b1) & 0xf) << 8) | (b2))
651 #define BRI12_IMM12 ((((b1) & 0xf0) >> 4) | ((b2) << 4))
653 #define BRI12_IMM12_SE (((BRI12_IMM12 & 0x800) ? 0xfffff000 : 0) | BRI12_IMM12)
655 #define BRI8_M BRI12_M
656 #define BRI8_R RRI8_R
657 #define BRI8_S RRI8_S
658 #define BRI8_IMM8 RRI8_IMM8
659 #define BRI8_IMM8_SE RRI8_IMM8_SE
663 uint8_t b0
= ldub_code(dc
->pc
);
664 uint8_t b1
= ldub_code(dc
->pc
+ 1);
665 uint8_t b2
= ldub_code(dc
->pc
+ 2);
667 static const uint32_t B4CONST
[] = {
668 0xffffffff, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 16, 32, 64, 128, 256
671 static const uint32_t B4CONSTU
[] = {
672 32768, 65536, 2, 3, 4, 5, 6, 7, 8, 10, 12, 16, 32, 64, 128, 256
676 dc
->next_pc
= dc
->pc
+ 2;
677 HAS_OPTION(XTENSA_OPTION_CODE_DENSITY
);
679 dc
->next_pc
= dc
->pc
+ 3;
688 if ((RRR_R
& 0xc) == 0x8) {
689 HAS_OPTION(XTENSA_OPTION_BOOLEAN
);
696 gen_exception_cause(dc
, ILLEGAL_INSTRUCTION_CAUSE
);
707 gen_window_check1(dc
, CALLX_S
);
708 gen_jump(dc
, cpu_R
[CALLX_S
]);
712 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER
);
714 TCGv_i32 tmp
= tcg_const_i32(dc
->pc
);
715 gen_advance_ccount(dc
);
716 gen_helper_retw(tmp
, tmp
);
729 gen_window_check2(dc
, CALLX_S
, CALLX_N
<< 2);
733 TCGv_i32 tmp
= tcg_temp_new_i32();
734 tcg_gen_mov_i32(tmp
, cpu_R
[CALLX_S
]);
735 tcg_gen_movi_i32(cpu_R
[0], dc
->next_pc
);
744 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER
);
746 TCGv_i32 tmp
= tcg_temp_new_i32();
748 tcg_gen_mov_i32(tmp
, cpu_R
[CALLX_S
]);
749 gen_callw(dc
, CALLX_N
, tmp
);
759 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER
);
760 gen_window_check2(dc
, RRR_T
, RRR_S
);
762 TCGv_i32 pc
= tcg_const_i32(dc
->pc
);
763 gen_advance_ccount(dc
);
764 gen_helper_movsp(pc
);
765 tcg_gen_mov_i32(cpu_R
[RRR_T
], cpu_R
[RRR_S
]);
785 HAS_OPTION(XTENSA_OPTION_EXCEPTION
);
797 default: /*reserved*/
806 HAS_OPTION(XTENSA_OPTION_EXCEPTION
);
809 gen_check_privilege(dc
);
810 tcg_gen_andi_i32(cpu_SR
[PS
], cpu_SR
[PS
], ~PS_EXCM
);
811 gen_helper_check_interrupts(cpu_env
);
812 gen_jump(dc
, cpu_SR
[EPC1
]);
820 gen_check_privilege(dc
);
822 dc
->config
->ndepc
? DEPC
: EPC1
]);
827 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER
);
828 gen_check_privilege(dc
);
830 TCGv_i32 tmp
= tcg_const_i32(1);
833 cpu_SR
[PS
], cpu_SR
[PS
], ~PS_EXCM
);
834 tcg_gen_shl_i32(tmp
, tmp
, cpu_SR
[WINDOW_BASE
]);
837 tcg_gen_andc_i32(cpu_SR
[WINDOW_START
],
838 cpu_SR
[WINDOW_START
], tmp
);
840 tcg_gen_or_i32(cpu_SR
[WINDOW_START
],
841 cpu_SR
[WINDOW_START
], tmp
);
844 gen_helper_restore_owb();
845 gen_helper_check_interrupts(cpu_env
);
846 gen_jump(dc
, cpu_SR
[EPC1
]);
852 default: /*reserved*/
859 HAS_OPTION(XTENSA_OPTION_HIGH_PRIORITY_INTERRUPT
);
860 if (RRR_S
>= 2 && RRR_S
<= dc
->config
->nlevel
) {
861 gen_check_privilege(dc
);
862 tcg_gen_mov_i32(cpu_SR
[PS
],
863 cpu_SR
[EPS2
+ RRR_S
- 2]);
864 gen_helper_check_interrupts(cpu_env
);
865 gen_jump(dc
, cpu_SR
[EPC1
+ RRR_S
- 1]);
867 qemu_log("RFI %d is illegal\n", RRR_S
);
868 gen_exception_cause(dc
, ILLEGAL_INSTRUCTION_CAUSE
);
876 default: /*reserved*/
884 HAS_OPTION(XTENSA_OPTION_EXCEPTION
);
889 HAS_OPTION(XTENSA_OPTION_EXCEPTION
);
892 gen_exception_cause(dc
, SYSCALL_CAUSE
);
896 if (semihosting_enabled
) {
897 gen_check_privilege(dc
);
898 gen_helper_simcall(cpu_env
);
900 qemu_log("SIMCALL but semihosting is disabled\n");
901 gen_exception_cause(dc
, ILLEGAL_INSTRUCTION_CAUSE
);
912 HAS_OPTION(XTENSA_OPTION_INTERRUPT
);
913 gen_check_privilege(dc
);
914 gen_window_check1(dc
, RRR_T
);
915 tcg_gen_mov_i32(cpu_R
[RRR_T
], cpu_SR
[PS
]);
916 tcg_gen_andi_i32(cpu_SR
[PS
], cpu_SR
[PS
], ~PS_INTLEVEL
);
917 tcg_gen_ori_i32(cpu_SR
[PS
], cpu_SR
[PS
], RRR_S
);
918 gen_helper_check_interrupts(cpu_env
);
919 gen_jumpi_check_loop_end(dc
, 0);
923 HAS_OPTION(XTENSA_OPTION_INTERRUPT
);
924 gen_check_privilege(dc
);
925 gen_waiti(dc
, RRR_S
);
929 HAS_OPTION(XTENSA_OPTION_BOOLEAN
);
934 HAS_OPTION(XTENSA_OPTION_BOOLEAN
);
939 HAS_OPTION(XTENSA_OPTION_BOOLEAN
);
944 HAS_OPTION(XTENSA_OPTION_BOOLEAN
);
948 default: /*reserved*/
956 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
957 tcg_gen_and_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
], cpu_R
[RRR_T
]);
961 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
962 tcg_gen_or_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
], cpu_R
[RRR_T
]);
966 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
967 tcg_gen_xor_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
], cpu_R
[RRR_T
]);
973 gen_window_check1(dc
, RRR_S
);
974 gen_right_shift_sar(dc
, cpu_R
[RRR_S
]);
978 gen_window_check1(dc
, RRR_S
);
979 gen_left_shift_sar(dc
, cpu_R
[RRR_S
]);
983 gen_window_check1(dc
, RRR_S
);
985 TCGv_i32 tmp
= tcg_temp_new_i32();
986 tcg_gen_shli_i32(tmp
, cpu_R
[RRR_S
], 3);
987 gen_right_shift_sar(dc
, tmp
);
993 gen_window_check1(dc
, RRR_S
);
995 TCGv_i32 tmp
= tcg_temp_new_i32();
996 tcg_gen_shli_i32(tmp
, cpu_R
[RRR_S
], 3);
997 gen_left_shift_sar(dc
, tmp
);
1004 TCGv_i32 tmp
= tcg_const_i32(
1005 RRR_S
| ((RRR_T
& 1) << 4));
1006 gen_right_shift_sar(dc
, tmp
);
1020 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER
);
1021 gen_check_privilege(dc
);
1023 TCGv_i32 tmp
= tcg_const_i32(
1024 RRR_T
| ((RRR_T
& 8) ? 0xfffffff0 : 0));
1025 gen_helper_rotw(tmp
);
1027 reset_used_window(dc
);
1032 HAS_OPTION(XTENSA_OPTION_MISC_OP
);
1033 gen_window_check2(dc
, RRR_S
, RRR_T
);
1034 gen_helper_nsa(cpu_R
[RRR_T
], cpu_R
[RRR_S
]);
1038 HAS_OPTION(XTENSA_OPTION_MISC_OP
);
1039 gen_window_check2(dc
, RRR_S
, RRR_T
);
1040 gen_helper_nsau(cpu_R
[RRR_T
], cpu_R
[RRR_S
]);
1043 default: /*reserved*/
1054 gen_window_check2(dc
, RRR_R
, RRR_T
);
1057 tcg_gen_neg_i32(cpu_R
[RRR_R
], cpu_R
[RRR_T
]);
1062 int label
= gen_new_label();
1063 tcg_gen_mov_i32(cpu_R
[RRR_R
], cpu_R
[RRR_T
]);
1064 tcg_gen_brcondi_i32(
1065 TCG_COND_GE
, cpu_R
[RRR_R
], 0, label
);
1066 tcg_gen_neg_i32(cpu_R
[RRR_R
], cpu_R
[RRR_T
]);
1067 gen_set_label(label
);
1071 default: /*reserved*/
1077 case 7: /*reserved*/
1082 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1083 tcg_gen_add_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
], cpu_R
[RRR_T
]);
1089 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1091 TCGv_i32 tmp
= tcg_temp_new_i32();
1092 tcg_gen_shli_i32(tmp
, cpu_R
[RRR_S
], OP2
- 8);
1093 tcg_gen_add_i32(cpu_R
[RRR_R
], tmp
, cpu_R
[RRR_T
]);
1099 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1100 tcg_gen_sub_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
], cpu_R
[RRR_T
]);
1106 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1108 TCGv_i32 tmp
= tcg_temp_new_i32();
1109 tcg_gen_shli_i32(tmp
, cpu_R
[RRR_S
], OP2
- 12);
1110 tcg_gen_sub_i32(cpu_R
[RRR_R
], tmp
, cpu_R
[RRR_T
]);
1121 gen_window_check2(dc
, RRR_R
, RRR_S
);
1122 tcg_gen_shli_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
],
1123 32 - (RRR_T
| ((OP2
& 1) << 4)));
1128 gen_window_check2(dc
, RRR_R
, RRR_T
);
1129 tcg_gen_sari_i32(cpu_R
[RRR_R
], cpu_R
[RRR_T
],
1130 RRR_S
| ((OP2
& 1) << 4));
1134 gen_window_check2(dc
, RRR_R
, RRR_T
);
1135 tcg_gen_shri_i32(cpu_R
[RRR_R
], cpu_R
[RRR_T
], RRR_S
);
1140 TCGv_i32 tmp
= tcg_temp_new_i32();
1142 gen_check_privilege(dc
);
1144 gen_window_check1(dc
, RRR_T
);
1145 tcg_gen_mov_i32(tmp
, cpu_R
[RRR_T
]);
1146 gen_rsr(dc
, cpu_R
[RRR_T
], RSR_SR
);
1147 gen_wsr(dc
, RSR_SR
, tmp
);
1149 if (!sregnames
[RSR_SR
]) {
1156 * Note: 64 bit ops are used here solely because SAR values
1159 #define gen_shift_reg(cmd, reg) do { \
1160 TCGv_i64 tmp = tcg_temp_new_i64(); \
1161 tcg_gen_extu_i32_i64(tmp, reg); \
1162 tcg_gen_##cmd##_i64(v, v, tmp); \
1163 tcg_gen_trunc_i64_i32(cpu_R[RRR_R], v); \
1164 tcg_temp_free_i64(v); \
1165 tcg_temp_free_i64(tmp); \
1168 #define gen_shift(cmd) gen_shift_reg(cmd, cpu_SR[SAR])
1171 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1173 TCGv_i64 v
= tcg_temp_new_i64();
1174 tcg_gen_concat_i32_i64(v
, cpu_R
[RRR_T
], cpu_R
[RRR_S
]);
1180 gen_window_check2(dc
, RRR_R
, RRR_T
);
1182 tcg_gen_shr_i32(cpu_R
[RRR_R
], cpu_R
[RRR_T
], cpu_SR
[SAR
]);
1184 TCGv_i64 v
= tcg_temp_new_i64();
1185 tcg_gen_extu_i32_i64(v
, cpu_R
[RRR_T
]);
1191 gen_window_check2(dc
, RRR_R
, RRR_S
);
1192 if (dc
->sar_m32_5bit
) {
1193 tcg_gen_shl_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
], dc
->sar_m32
);
1195 TCGv_i64 v
= tcg_temp_new_i64();
1196 TCGv_i32 s
= tcg_const_i32(32);
1197 tcg_gen_sub_i32(s
, s
, cpu_SR
[SAR
]);
1198 tcg_gen_andi_i32(s
, s
, 0x3f);
1199 tcg_gen_extu_i32_i64(v
, cpu_R
[RRR_S
]);
1200 gen_shift_reg(shl
, s
);
1206 gen_window_check2(dc
, RRR_R
, RRR_T
);
1208 tcg_gen_sar_i32(cpu_R
[RRR_R
], cpu_R
[RRR_T
], cpu_SR
[SAR
]);
1210 TCGv_i64 v
= tcg_temp_new_i64();
1211 tcg_gen_ext_i32_i64(v
, cpu_R
[RRR_T
]);
1216 #undef gen_shift_reg
1219 HAS_OPTION(XTENSA_OPTION_16_BIT_IMUL
);
1220 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1222 TCGv_i32 v1
= tcg_temp_new_i32();
1223 TCGv_i32 v2
= tcg_temp_new_i32();
1224 tcg_gen_ext16u_i32(v1
, cpu_R
[RRR_S
]);
1225 tcg_gen_ext16u_i32(v2
, cpu_R
[RRR_T
]);
1226 tcg_gen_mul_i32(cpu_R
[RRR_R
], v1
, v2
);
1233 HAS_OPTION(XTENSA_OPTION_16_BIT_IMUL
);
1234 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1236 TCGv_i32 v1
= tcg_temp_new_i32();
1237 TCGv_i32 v2
= tcg_temp_new_i32();
1238 tcg_gen_ext16s_i32(v1
, cpu_R
[RRR_S
]);
1239 tcg_gen_ext16s_i32(v2
, cpu_R
[RRR_T
]);
1240 tcg_gen_mul_i32(cpu_R
[RRR_R
], v1
, v2
);
1246 default: /*reserved*/
1253 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1256 HAS_OPTION(XTENSA_OPTION_32_BIT_IDIV
);
1257 int label
= gen_new_label();
1258 tcg_gen_brcondi_i32(TCG_COND_NE
, cpu_R
[RRR_T
], 0, label
);
1259 gen_exception_cause(dc
, INTEGER_DIVIDE_BY_ZERO_CAUSE
);
1260 gen_set_label(label
);
1265 HAS_OPTION(XTENSA_OPTION_32_BIT_IMUL
);
1266 tcg_gen_mul_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
], cpu_R
[RRR_T
]);
1271 HAS_OPTION(XTENSA_OPTION_32_BIT_IMUL
);
1273 TCGv_i64 r
= tcg_temp_new_i64();
1274 TCGv_i64 s
= tcg_temp_new_i64();
1275 TCGv_i64 t
= tcg_temp_new_i64();
1278 tcg_gen_extu_i32_i64(s
, cpu_R
[RRR_S
]);
1279 tcg_gen_extu_i32_i64(t
, cpu_R
[RRR_T
]);
1281 tcg_gen_ext_i32_i64(s
, cpu_R
[RRR_S
]);
1282 tcg_gen_ext_i32_i64(t
, cpu_R
[RRR_T
]);
1284 tcg_gen_mul_i64(r
, s
, t
);
1285 tcg_gen_shri_i64(r
, r
, 32);
1286 tcg_gen_trunc_i64_i32(cpu_R
[RRR_R
], r
);
1288 tcg_temp_free_i64(r
);
1289 tcg_temp_free_i64(s
);
1290 tcg_temp_free_i64(t
);
1295 tcg_gen_divu_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
], cpu_R
[RRR_T
]);
1301 int label1
= gen_new_label();
1302 int label2
= gen_new_label();
1304 tcg_gen_brcondi_i32(TCG_COND_NE
, cpu_R
[RRR_S
], 0x80000000,
1306 tcg_gen_brcondi_i32(TCG_COND_NE
, cpu_R
[RRR_T
], 0xffffffff,
1308 tcg_gen_movi_i32(cpu_R
[RRR_R
],
1309 OP2
== 13 ? 0x80000000 : 0);
1311 gen_set_label(label1
);
1313 tcg_gen_div_i32(cpu_R
[RRR_R
],
1314 cpu_R
[RRR_S
], cpu_R
[RRR_T
]);
1316 tcg_gen_rem_i32(cpu_R
[RRR_R
],
1317 cpu_R
[RRR_S
], cpu_R
[RRR_T
]);
1319 gen_set_label(label2
);
1324 tcg_gen_remu_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
], cpu_R
[RRR_T
]);
1327 default: /*reserved*/
1337 gen_check_privilege(dc
);
1339 gen_window_check1(dc
, RRR_T
);
1340 gen_rsr(dc
, cpu_R
[RRR_T
], RSR_SR
);
1341 if (!sregnames
[RSR_SR
]) {
1348 gen_check_privilege(dc
);
1350 gen_window_check1(dc
, RRR_T
);
1351 gen_wsr(dc
, RSR_SR
, cpu_R
[RRR_T
]);
1352 if (!sregnames
[RSR_SR
]) {
1358 HAS_OPTION(XTENSA_OPTION_MISC_OP
);
1359 gen_window_check2(dc
, RRR_R
, RRR_S
);
1361 int shift
= 24 - RRR_T
;
1364 tcg_gen_ext8s_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
]);
1365 } else if (shift
== 16) {
1366 tcg_gen_ext16s_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
]);
1368 TCGv_i32 tmp
= tcg_temp_new_i32();
1369 tcg_gen_shli_i32(tmp
, cpu_R
[RRR_S
], shift
);
1370 tcg_gen_sari_i32(cpu_R
[RRR_R
], tmp
, shift
);
1377 HAS_OPTION(XTENSA_OPTION_MISC_OP
);
1378 gen_window_check2(dc
, RRR_R
, RRR_S
);
1380 TCGv_i32 tmp1
= tcg_temp_new_i32();
1381 TCGv_i32 tmp2
= tcg_temp_new_i32();
1382 int label
= gen_new_label();
1384 tcg_gen_sari_i32(tmp1
, cpu_R
[RRR_S
], 24 - RRR_T
);
1385 tcg_gen_xor_i32(tmp2
, tmp1
, cpu_R
[RRR_S
]);
1386 tcg_gen_andi_i32(tmp2
, tmp2
, 0xffffffff << (RRR_T
+ 7));
1387 tcg_gen_mov_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
]);
1388 tcg_gen_brcondi_i32(TCG_COND_EQ
, tmp2
, 0, label
);
1390 tcg_gen_sari_i32(tmp1
, cpu_R
[RRR_S
], 31);
1391 tcg_gen_xori_i32(cpu_R
[RRR_R
], tmp1
,
1392 0xffffffff >> (25 - RRR_T
));
1394 gen_set_label(label
);
1396 tcg_temp_free(tmp1
);
1397 tcg_temp_free(tmp2
);
1405 HAS_OPTION(XTENSA_OPTION_MISC_OP
);
1406 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1408 static const TCGCond cond
[] = {
1414 int label
= gen_new_label();
1416 if (RRR_R
!= RRR_T
) {
1417 tcg_gen_mov_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
]);
1418 tcg_gen_brcond_i32(cond
[OP2
- 4],
1419 cpu_R
[RRR_S
], cpu_R
[RRR_T
], label
);
1420 tcg_gen_mov_i32(cpu_R
[RRR_R
], cpu_R
[RRR_T
]);
1422 tcg_gen_brcond_i32(cond
[OP2
- 4],
1423 cpu_R
[RRR_T
], cpu_R
[RRR_S
], label
);
1424 tcg_gen_mov_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
]);
1426 gen_set_label(label
);
1434 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1436 static const TCGCond cond
[] = {
1442 int label
= gen_new_label();
1443 tcg_gen_brcondi_i32(cond
[OP2
- 8], cpu_R
[RRR_T
], 0, label
);
1444 tcg_gen_mov_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
]);
1445 gen_set_label(label
);
1450 HAS_OPTION(XTENSA_OPTION_BOOLEAN
);
1455 HAS_OPTION(XTENSA_OPTION_BOOLEAN
);
1460 gen_window_check1(dc
, RRR_R
);
1462 int st
= (RRR_S
<< 4) + RRR_T
;
1463 if (uregnames
[st
]) {
1464 tcg_gen_mov_i32(cpu_R
[RRR_R
], cpu_UR
[st
]);
1466 qemu_log("RUR %d not implemented, ", st
);
1473 gen_window_check1(dc
, RRR_T
);
1475 if (uregnames
[RSR_SR
]) {
1476 tcg_gen_mov_i32(cpu_UR
[RSR_SR
], cpu_R
[RRR_T
]);
1478 qemu_log("WUR %d not implemented, ", RSR_SR
);
1489 gen_window_check2(dc
, RRR_R
, RRR_T
);
1491 int shiftimm
= RRR_S
| (OP1
<< 4);
1492 int maskimm
= (1 << (OP2
+ 1)) - 1;
1494 TCGv_i32 tmp
= tcg_temp_new_i32();
1495 tcg_gen_shri_i32(tmp
, cpu_R
[RRR_T
], shiftimm
);
1496 tcg_gen_andi_i32(cpu_R
[RRR_R
], tmp
, maskimm
);
1510 HAS_OPTION(XTENSA_OPTION_COPROCESSOR
);
1515 gen_window_check2(dc
, RRR_S
, RRR_T
);
1518 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER
);
1519 gen_check_privilege(dc
);
1521 TCGv_i32 addr
= tcg_temp_new_i32();
1522 tcg_gen_addi_i32(addr
, cpu_R
[RRR_S
],
1523 (0xffffffc0 | (RRR_R
<< 2)));
1524 tcg_gen_qemu_ld32u(cpu_R
[RRR_T
], addr
, dc
->ring
);
1525 tcg_temp_free(addr
);
1530 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER
);
1531 gen_check_privilege(dc
);
1533 TCGv_i32 addr
= tcg_temp_new_i32();
1534 tcg_gen_addi_i32(addr
, cpu_R
[RRR_S
],
1535 (0xffffffc0 | (RRR_R
<< 2)));
1536 tcg_gen_qemu_st32(cpu_R
[RRR_T
], addr
, dc
->ring
);
1537 tcg_temp_free(addr
);
1548 HAS_OPTION(XTENSA_OPTION_FP_COPROCESSOR
);
1553 HAS_OPTION(XTENSA_OPTION_FP_COPROCESSOR
);
1557 default: /*reserved*/
1564 gen_window_check1(dc
, RRR_T
);
1566 TCGv_i32 tmp
= tcg_const_i32(
1567 ((dc
->tb
->flags
& XTENSA_TBFLAG_LITBASE
) ?
1568 0 : ((dc
->pc
+ 3) & ~3)) +
1569 (0xfffc0000 | (RI16_IMM16
<< 2)));
1571 if (dc
->tb
->flags
& XTENSA_TBFLAG_LITBASE
) {
1572 tcg_gen_add_i32(tmp
, tmp
, dc
->litbase
);
1574 tcg_gen_qemu_ld32u(cpu_R
[RRR_T
], tmp
, dc
->cring
);
1580 #define gen_load_store(type, shift) do { \
1581 TCGv_i32 addr = tcg_temp_new_i32(); \
1582 gen_window_check2(dc, RRI8_S, RRI8_T); \
1583 tcg_gen_addi_i32(addr, cpu_R[RRI8_S], RRI8_IMM8 << shift); \
1585 gen_load_store_alignment(dc, shift, addr, false); \
1587 tcg_gen_qemu_##type(cpu_R[RRI8_T], addr, dc->cring); \
1588 tcg_temp_free(addr); \
1593 gen_load_store(ld8u
, 0);
1597 gen_load_store(ld16u
, 1);
1601 gen_load_store(ld32u
, 2);
1605 gen_load_store(st8
, 0);
1609 gen_load_store(st16
, 1);
1613 gen_load_store(st32
, 2);
1618 HAS_OPTION(XTENSA_OPTION_DCACHE
);
1649 HAS_OPTION(XTENSA_OPTION_DCACHE_INDEX_LOCK
);
1653 HAS_OPTION(XTENSA_OPTION_DCACHE_INDEX_LOCK
);
1657 HAS_OPTION(XTENSA_OPTION_DCACHE_INDEX_LOCK
);
1661 HAS_OPTION(XTENSA_OPTION_DCACHE
);
1665 HAS_OPTION(XTENSA_OPTION_DCACHE
);
1668 default: /*reserved*/
1676 HAS_OPTION(XTENSA_OPTION_ICACHE
);
1682 HAS_OPTION(XTENSA_OPTION_ICACHE_INDEX_LOCK
);
1686 HAS_OPTION(XTENSA_OPTION_ICACHE_INDEX_LOCK
);
1690 HAS_OPTION(XTENSA_OPTION_ICACHE_INDEX_LOCK
);
1693 default: /*reserved*/
1700 HAS_OPTION(XTENSA_OPTION_ICACHE
);
1704 HAS_OPTION(XTENSA_OPTION_ICACHE
);
1707 default: /*reserved*/
1714 gen_load_store(ld16s
, 1);
1716 #undef gen_load_store
1719 gen_window_check1(dc
, RRI8_T
);
1720 tcg_gen_movi_i32(cpu_R
[RRI8_T
],
1721 RRI8_IMM8
| (RRI8_S
<< 8) |
1722 ((RRI8_S
& 0x8) ? 0xfffff000 : 0));
1725 #define gen_load_store_no_hw_align(type) do { \
1726 TCGv_i32 addr = tcg_temp_local_new_i32(); \
1727 gen_window_check2(dc, RRI8_S, RRI8_T); \
1728 tcg_gen_addi_i32(addr, cpu_R[RRI8_S], RRI8_IMM8 << 2); \
1729 gen_load_store_alignment(dc, 2, addr, true); \
1730 tcg_gen_qemu_##type(cpu_R[RRI8_T], addr, dc->cring); \
1731 tcg_temp_free(addr); \
1735 HAS_OPTION(XTENSA_OPTION_MP_SYNCHRO
);
1736 gen_load_store_no_hw_align(ld32u
); /*TODO acquire?*/
1740 gen_window_check2(dc
, RRI8_S
, RRI8_T
);
1741 tcg_gen_addi_i32(cpu_R
[RRI8_T
], cpu_R
[RRI8_S
], RRI8_IMM8_SE
);
1745 gen_window_check2(dc
, RRI8_S
, RRI8_T
);
1746 tcg_gen_addi_i32(cpu_R
[RRI8_T
], cpu_R
[RRI8_S
], RRI8_IMM8_SE
<< 8);
1749 case 14: /*S32C1Iy*/
1750 HAS_OPTION(XTENSA_OPTION_MP_SYNCHRO
);
1751 gen_window_check2(dc
, RRI8_S
, RRI8_T
);
1753 int label
= gen_new_label();
1754 TCGv_i32 tmp
= tcg_temp_local_new_i32();
1755 TCGv_i32 addr
= tcg_temp_local_new_i32();
1757 tcg_gen_mov_i32(tmp
, cpu_R
[RRI8_T
]);
1758 tcg_gen_addi_i32(addr
, cpu_R
[RRI8_S
], RRI8_IMM8
<< 2);
1759 gen_load_store_alignment(dc
, 2, addr
, true);
1760 tcg_gen_qemu_ld32u(cpu_R
[RRI8_T
], addr
, dc
->cring
);
1761 tcg_gen_brcond_i32(TCG_COND_NE
, cpu_R
[RRI8_T
],
1762 cpu_SR
[SCOMPARE1
], label
);
1764 tcg_gen_qemu_st32(tmp
, addr
, dc
->cring
);
1766 gen_set_label(label
);
1767 tcg_temp_free(addr
);
1773 HAS_OPTION(XTENSA_OPTION_MP_SYNCHRO
);
1774 gen_load_store_no_hw_align(st32
); /*TODO release?*/
1776 #undef gen_load_store_no_hw_align
1778 default: /*reserved*/
1785 HAS_OPTION(XTENSA_OPTION_COPROCESSOR
);
1790 HAS_OPTION(XTENSA_OPTION_MAC16
);
1797 tcg_gen_movi_i32(cpu_R
[0], dc
->next_pc
);
1798 gen_jumpi(dc
, (dc
->pc
& ~3) + (CALL_OFFSET_SE
<< 2) + 4, 0);
1804 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER
);
1805 gen_window_check1(dc
, CALL_N
<< 2);
1806 gen_callwi(dc
, CALL_N
,
1807 (dc
->pc
& ~3) + (CALL_OFFSET_SE
<< 2) + 4, 0);
1815 gen_jumpi(dc
, dc
->pc
+ 4 + CALL_OFFSET_SE
, 0);
1819 gen_window_check1(dc
, BRI12_S
);
1821 static const TCGCond cond
[] = {
1822 TCG_COND_EQ
, /*BEQZ*/
1823 TCG_COND_NE
, /*BNEZ*/
1824 TCG_COND_LT
, /*BLTZ*/
1825 TCG_COND_GE
, /*BGEZ*/
1828 gen_brcondi(dc
, cond
[BRI12_M
& 3], cpu_R
[BRI12_S
], 0,
1829 4 + BRI12_IMM12_SE
);
1834 gen_window_check1(dc
, BRI8_S
);
1836 static const TCGCond cond
[] = {
1837 TCG_COND_EQ
, /*BEQI*/
1838 TCG_COND_NE
, /*BNEI*/
1839 TCG_COND_LT
, /*BLTI*/
1840 TCG_COND_GE
, /*BGEI*/
1843 gen_brcondi(dc
, cond
[BRI8_M
& 3],
1844 cpu_R
[BRI8_S
], B4CONST
[BRI8_R
], 4 + BRI8_IMM8_SE
);
1851 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER
);
1853 TCGv_i32 pc
= tcg_const_i32(dc
->pc
);
1854 TCGv_i32 s
= tcg_const_i32(BRI12_S
);
1855 TCGv_i32 imm
= tcg_const_i32(BRI12_IMM12
);
1856 gen_advance_ccount(dc
);
1857 gen_helper_entry(pc
, s
, imm
);
1861 reset_used_window(dc
);
1868 HAS_OPTION(XTENSA_OPTION_BOOLEAN
);
1873 HAS_OPTION(XTENSA_OPTION_BOOLEAN
);
1879 case 10: /*LOOPGTZ*/
1880 HAS_OPTION(XTENSA_OPTION_LOOP
);
1881 gen_window_check1(dc
, RRI8_S
);
1883 uint32_t lend
= dc
->pc
+ RRI8_IMM8
+ 4;
1884 TCGv_i32 tmp
= tcg_const_i32(lend
);
1886 tcg_gen_subi_i32(cpu_SR
[LCOUNT
], cpu_R
[RRI8_S
], 1);
1887 tcg_gen_movi_i32(cpu_SR
[LBEG
], dc
->next_pc
);
1888 gen_wsr_lend(dc
, LEND
, tmp
);
1892 int label
= gen_new_label();
1893 tcg_gen_brcondi_i32(
1894 BRI8_R
== 9 ? TCG_COND_NE
: TCG_COND_GT
,
1895 cpu_R
[RRI8_S
], 0, label
);
1896 gen_jumpi(dc
, lend
, 1);
1897 gen_set_label(label
);
1900 gen_jumpi(dc
, dc
->next_pc
, 0);
1904 default: /*reserved*/
1913 gen_window_check1(dc
, BRI8_S
);
1914 gen_brcondi(dc
, BRI8_M
== 2 ? TCG_COND_LTU
: TCG_COND_GEU
,
1915 cpu_R
[BRI8_S
], B4CONSTU
[BRI8_R
], 4 + BRI8_IMM8_SE
);
1925 TCGCond eq_ne
= (RRI8_R
& 8) ? TCG_COND_NE
: TCG_COND_EQ
;
1927 switch (RRI8_R
& 7) {
1928 case 0: /*BNONE*/ /*BANY*/
1929 gen_window_check2(dc
, RRI8_S
, RRI8_T
);
1931 TCGv_i32 tmp
= tcg_temp_new_i32();
1932 tcg_gen_and_i32(tmp
, cpu_R
[RRI8_S
], cpu_R
[RRI8_T
]);
1933 gen_brcondi(dc
, eq_ne
, tmp
, 0, 4 + RRI8_IMM8_SE
);
1938 case 1: /*BEQ*/ /*BNE*/
1939 case 2: /*BLT*/ /*BGE*/
1940 case 3: /*BLTU*/ /*BGEU*/
1941 gen_window_check2(dc
, RRI8_S
, RRI8_T
);
1943 static const TCGCond cond
[] = {
1949 [11] = TCG_COND_GEU
,
1951 gen_brcond(dc
, cond
[RRI8_R
], cpu_R
[RRI8_S
], cpu_R
[RRI8_T
],
1956 case 4: /*BALL*/ /*BNALL*/
1957 gen_window_check2(dc
, RRI8_S
, RRI8_T
);
1959 TCGv_i32 tmp
= tcg_temp_new_i32();
1960 tcg_gen_and_i32(tmp
, cpu_R
[RRI8_S
], cpu_R
[RRI8_T
]);
1961 gen_brcond(dc
, eq_ne
, tmp
, cpu_R
[RRI8_T
],
1967 case 5: /*BBC*/ /*BBS*/
1968 gen_window_check2(dc
, RRI8_S
, RRI8_T
);
1970 TCGv_i32 bit
= tcg_const_i32(1);
1971 TCGv_i32 tmp
= tcg_temp_new_i32();
1972 tcg_gen_andi_i32(tmp
, cpu_R
[RRI8_T
], 0x1f);
1973 tcg_gen_shl_i32(bit
, bit
, tmp
);
1974 tcg_gen_and_i32(tmp
, cpu_R
[RRI8_S
], bit
);
1975 gen_brcondi(dc
, eq_ne
, tmp
, 0, 4 + RRI8_IMM8_SE
);
1981 case 6: /*BBCI*/ /*BBSI*/
1983 gen_window_check1(dc
, RRI8_S
);
1985 TCGv_i32 tmp
= tcg_temp_new_i32();
1986 tcg_gen_andi_i32(tmp
, cpu_R
[RRI8_S
],
1987 1 << (((RRI8_R
& 1) << 4) | RRI8_T
));
1988 gen_brcondi(dc
, eq_ne
, tmp
, 0, 4 + RRI8_IMM8_SE
);
1997 #define gen_narrow_load_store(type) do { \
1998 TCGv_i32 addr = tcg_temp_new_i32(); \
1999 gen_window_check2(dc, RRRN_S, RRRN_T); \
2000 tcg_gen_addi_i32(addr, cpu_R[RRRN_S], RRRN_R << 2); \
2001 gen_load_store_alignment(dc, 2, addr, false); \
2002 tcg_gen_qemu_##type(cpu_R[RRRN_T], addr, dc->cring); \
2003 tcg_temp_free(addr); \
2007 gen_narrow_load_store(ld32u
);
2011 gen_narrow_load_store(st32
);
2013 #undef gen_narrow_load_store
2016 gen_window_check3(dc
, RRRN_R
, RRRN_S
, RRRN_T
);
2017 tcg_gen_add_i32(cpu_R
[RRRN_R
], cpu_R
[RRRN_S
], cpu_R
[RRRN_T
]);
2020 case 11: /*ADDI.Nn*/
2021 gen_window_check2(dc
, RRRN_R
, RRRN_S
);
2022 tcg_gen_addi_i32(cpu_R
[RRRN_R
], cpu_R
[RRRN_S
], RRRN_T
? RRRN_T
: -1);
2026 gen_window_check1(dc
, RRRN_S
);
2027 if (RRRN_T
< 8) { /*MOVI.Nn*/
2028 tcg_gen_movi_i32(cpu_R
[RRRN_S
],
2029 RRRN_R
| (RRRN_T
<< 4) |
2030 ((RRRN_T
& 6) == 6 ? 0xffffff80 : 0));
2031 } else { /*BEQZ.Nn*/ /*BNEZ.Nn*/
2032 TCGCond eq_ne
= (RRRN_T
& 4) ? TCG_COND_NE
: TCG_COND_EQ
;
2034 gen_brcondi(dc
, eq_ne
, cpu_R
[RRRN_S
], 0,
2035 4 + (RRRN_R
| ((RRRN_T
& 3) << 4)));
2042 gen_window_check2(dc
, RRRN_S
, RRRN_T
);
2043 tcg_gen_mov_i32(cpu_R
[RRRN_T
], cpu_R
[RRRN_S
]);
2049 gen_jump(dc
, cpu_R
[0]);
2053 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER
);
2055 TCGv_i32 tmp
= tcg_const_i32(dc
->pc
);
2056 gen_advance_ccount(dc
);
2057 gen_helper_retw(tmp
, tmp
);
2063 case 2: /*BREAK.Nn*/
2071 gen_exception_cause(dc
, ILLEGAL_INSTRUCTION_CAUSE
);
2074 default: /*reserved*/
2080 default: /*reserved*/
2086 default: /*reserved*/
2091 gen_check_loop_end(dc
, 0);
2092 dc
->pc
= dc
->next_pc
;
2097 qemu_log("INVALID(pc = %08x)\n", dc
->pc
);
2098 dc
->pc
= dc
->next_pc
;
2102 static void check_breakpoint(CPUState
*env
, DisasContext
*dc
)
2106 if (unlikely(!QTAILQ_EMPTY(&env
->breakpoints
))) {
2107 QTAILQ_FOREACH(bp
, &env
->breakpoints
, entry
) {
2108 if (bp
->pc
== dc
->pc
) {
2109 tcg_gen_movi_i32(cpu_pc
, dc
->pc
);
2110 gen_exception(dc
, EXCP_DEBUG
);
2111 dc
->is_jmp
= DISAS_UPDATE
;
2117 static void gen_intermediate_code_internal(
2118 CPUState
*env
, TranslationBlock
*tb
, int search_pc
)
2123 uint16_t *gen_opc_end
= gen_opc_buf
+ OPC_MAX_SIZE
;
2124 int max_insns
= tb
->cflags
& CF_COUNT_MASK
;
2125 uint32_t pc_start
= tb
->pc
;
2126 uint32_t next_page_start
=
2127 (pc_start
& TARGET_PAGE_MASK
) + TARGET_PAGE_SIZE
;
2129 if (max_insns
== 0) {
2130 max_insns
= CF_COUNT_MASK
;
2133 dc
.config
= env
->config
;
2134 dc
.singlestep_enabled
= env
->singlestep_enabled
;
2137 dc
.ring
= tb
->flags
& XTENSA_TBFLAG_RING_MASK
;
2138 dc
.cring
= (tb
->flags
& XTENSA_TBFLAG_EXCM
) ? 0 : dc
.ring
;
2139 dc
.lbeg
= env
->sregs
[LBEG
];
2140 dc
.lend
= env
->sregs
[LEND
];
2141 dc
.is_jmp
= DISAS_NEXT
;
2142 dc
.ccount_delta
= 0;
2145 init_sar_tracker(&dc
);
2146 reset_used_window(&dc
);
2150 if (env
->singlestep_enabled
&& env
->exception_taken
) {
2151 env
->exception_taken
= 0;
2152 tcg_gen_movi_i32(cpu_pc
, dc
.pc
);
2153 gen_exception(&dc
, EXCP_DEBUG
);
2157 check_breakpoint(env
, &dc
);
2160 j
= gen_opc_ptr
- gen_opc_buf
;
2164 gen_opc_instr_start
[lj
++] = 0;
2167 gen_opc_pc
[lj
] = dc
.pc
;
2168 gen_opc_instr_start
[lj
] = 1;
2169 gen_opc_icount
[lj
] = insn_count
;
2172 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP
))) {
2173 tcg_gen_debug_insn_start(dc
.pc
);
2178 if (insn_count
+ 1 == max_insns
&& (tb
->cflags
& CF_LAST_IO
)) {
2182 disas_xtensa_insn(&dc
);
2184 if (env
->singlestep_enabled
) {
2185 tcg_gen_movi_i32(cpu_pc
, dc
.pc
);
2186 gen_exception(&dc
, EXCP_DEBUG
);
2189 } while (dc
.is_jmp
== DISAS_NEXT
&&
2190 insn_count
< max_insns
&&
2191 dc
.pc
< next_page_start
&&
2192 gen_opc_ptr
< gen_opc_end
);
2195 reset_sar_tracker(&dc
);
2197 if (tb
->cflags
& CF_LAST_IO
) {
2201 if (dc
.is_jmp
== DISAS_NEXT
) {
2202 gen_jumpi(&dc
, dc
.pc
, 0);
2204 gen_icount_end(tb
, insn_count
);
2205 *gen_opc_ptr
= INDEX_op_end
;
2208 tb
->size
= dc
.pc
- pc_start
;
2209 tb
->icount
= insn_count
;
2213 void gen_intermediate_code(CPUState
*env
, TranslationBlock
*tb
)
2215 gen_intermediate_code_internal(env
, tb
, 0);
2218 void gen_intermediate_code_pc(CPUState
*env
, TranslationBlock
*tb
)
2220 gen_intermediate_code_internal(env
, tb
, 1);
2223 void cpu_dump_state(CPUState
*env
, FILE *f
, fprintf_function cpu_fprintf
,
2228 cpu_fprintf(f
, "PC=%08x\n\n", env
->pc
);
2230 for (i
= j
= 0; i
< 256; ++i
) {
2232 cpu_fprintf(f
, "%s=%08x%c", sregnames
[i
], env
->sregs
[i
],
2233 (j
++ % 4) == 3 ? '\n' : ' ');
2237 cpu_fprintf(f
, (j
% 4) == 0 ? "\n" : "\n\n");
2239 for (i
= j
= 0; i
< 256; ++i
) {
2241 cpu_fprintf(f
, "%s=%08x%c", uregnames
[i
], env
->uregs
[i
],
2242 (j
++ % 4) == 3 ? '\n' : ' ');
2246 cpu_fprintf(f
, (j
% 4) == 0 ? "\n" : "\n\n");
2248 for (i
= 0; i
< 16; ++i
) {
2249 cpu_fprintf(f
, "A%02d=%08x%c", i
, env
->regs
[i
],
2250 (i
% 4) == 3 ? '\n' : ' ');
2253 cpu_fprintf(f
, "\n");
2255 for (i
= 0; i
< env
->config
->nareg
; ++i
) {
2256 cpu_fprintf(f
, "AR%02d=%08x%c", i
, env
->phys_regs
[i
],
2257 (i
% 4) == 3 ? '\n' : ' ');
2261 void restore_state_to_opc(CPUState
*env
, TranslationBlock
*tb
, int pc_pos
)
2263 env
->pc
= gen_opc_pc
[pc_pos
];