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
;
70 static TCGv_ptr cpu_env
;
71 static TCGv_i32 cpu_pc
;
72 static TCGv_i32 cpu_R
[16];
73 static TCGv_i32 cpu_SR
[256];
74 static TCGv_i32 cpu_UR
[256];
76 #include "gen-icount.h"
78 static const char * const sregnames
[256] = {
84 [LITBASE
] = "LITBASE",
85 [SCOMPARE1
] = "SCOMPARE1",
92 [WINDOW_BASE
] = "WINDOW_BASE",
93 [WINDOW_START
] = "WINDOW_START",
94 [PTEVADDR
] = "PTEVADDR",
96 [ITLBCFG
] = "ITLBCFG",
97 [DTLBCFG
] = "DTLBCFG",
98 [IBREAKENABLE
] = "IBREAKENABLE",
99 [IBREAKA
] = "IBREAKA0",
100 [IBREAKA
+ 1] = "IBREAKA1",
101 [DBREAKA
] = "DBREAKA0",
102 [DBREAKA
+ 1] = "DBREAKA1",
103 [DBREAKC
] = "DBREAKC0",
104 [DBREAKC
+ 1] = "DBREAKC1",
119 [EXCSAVE1
] = "EXCSAVE1",
120 [EXCSAVE1
+ 1] = "EXCSAVE2",
121 [EXCSAVE1
+ 2] = "EXCSAVE3",
122 [EXCSAVE1
+ 3] = "EXCSAVE4",
123 [EXCSAVE1
+ 4] = "EXCSAVE5",
124 [EXCSAVE1
+ 5] = "EXCSAVE6",
125 [EXCSAVE1
+ 6] = "EXCSAVE7",
126 [CPENABLE
] = "CPENABLE",
128 [INTCLEAR
] = "INTCLEAR",
129 [INTENABLE
] = "INTENABLE",
131 [VECBASE
] = "VECBASE",
132 [EXCCAUSE
] = "EXCCAUSE",
133 [DEBUGCAUSE
] = "DEBUGCAUSE",
137 [ICOUNTLEVEL
] = "ICOUNTLEVEL",
138 [EXCVADDR
] = "EXCVADDR",
139 [CCOMPARE
] = "CCOMPARE0",
140 [CCOMPARE
+ 1] = "CCOMPARE1",
141 [CCOMPARE
+ 2] = "CCOMPARE2",
144 static const char * const uregnames
[256] = {
145 [THREADPTR
] = "THREADPTR",
150 void xtensa_translate_init(void)
152 static const char * const regnames
[] = {
153 "ar0", "ar1", "ar2", "ar3",
154 "ar4", "ar5", "ar6", "ar7",
155 "ar8", "ar9", "ar10", "ar11",
156 "ar12", "ar13", "ar14", "ar15",
160 cpu_env
= tcg_global_reg_new_ptr(TCG_AREG0
, "env");
161 cpu_pc
= tcg_global_mem_new_i32(TCG_AREG0
,
162 offsetof(CPUXtensaState
, pc
), "pc");
164 for (i
= 0; i
< 16; i
++) {
165 cpu_R
[i
] = tcg_global_mem_new_i32(TCG_AREG0
,
166 offsetof(CPUXtensaState
, regs
[i
]),
170 for (i
= 0; i
< 256; ++i
) {
172 cpu_SR
[i
] = tcg_global_mem_new_i32(TCG_AREG0
,
173 offsetof(CPUXtensaState
, sregs
[i
]),
178 for (i
= 0; i
< 256; ++i
) {
180 cpu_UR
[i
] = tcg_global_mem_new_i32(TCG_AREG0
,
181 offsetof(CPUXtensaState
, uregs
[i
]),
189 static inline bool option_bits_enabled(DisasContext
*dc
, uint64_t opt
)
191 return xtensa_option_bits_enabled(dc
->config
, opt
);
194 static inline bool option_enabled(DisasContext
*dc
, int opt
)
196 return xtensa_option_enabled(dc
->config
, opt
);
199 static void init_litbase(DisasContext
*dc
)
201 if (dc
->tb
->flags
& XTENSA_TBFLAG_LITBASE
) {
202 dc
->litbase
= tcg_temp_local_new_i32();
203 tcg_gen_andi_i32(dc
->litbase
, cpu_SR
[LITBASE
], 0xfffff000);
207 static void reset_litbase(DisasContext
*dc
)
209 if (dc
->tb
->flags
& XTENSA_TBFLAG_LITBASE
) {
210 tcg_temp_free(dc
->litbase
);
214 static void init_sar_tracker(DisasContext
*dc
)
216 dc
->sar_5bit
= false;
217 dc
->sar_m32_5bit
= false;
218 dc
->sar_m32_allocated
= false;
221 static void reset_sar_tracker(DisasContext
*dc
)
223 if (dc
->sar_m32_allocated
) {
224 tcg_temp_free(dc
->sar_m32
);
228 static void gen_right_shift_sar(DisasContext
*dc
, TCGv_i32 sa
)
230 tcg_gen_andi_i32(cpu_SR
[SAR
], sa
, 0x1f);
231 if (dc
->sar_m32_5bit
) {
232 tcg_gen_discard_i32(dc
->sar_m32
);
235 dc
->sar_m32_5bit
= false;
238 static void gen_left_shift_sar(DisasContext
*dc
, TCGv_i32 sa
)
240 TCGv_i32 tmp
= tcg_const_i32(32);
241 if (!dc
->sar_m32_allocated
) {
242 dc
->sar_m32
= tcg_temp_local_new_i32();
243 dc
->sar_m32_allocated
= true;
245 tcg_gen_andi_i32(dc
->sar_m32
, sa
, 0x1f);
246 tcg_gen_sub_i32(cpu_SR
[SAR
], tmp
, dc
->sar_m32
);
247 dc
->sar_5bit
= false;
248 dc
->sar_m32_5bit
= true;
252 static void gen_advance_ccount(DisasContext
*dc
)
254 if (dc
->ccount_delta
> 0) {
255 TCGv_i32 tmp
= tcg_const_i32(dc
->ccount_delta
);
256 dc
->ccount_delta
= 0;
257 gen_helper_advance_ccount(cpu_env
, tmp
);
262 static void reset_used_window(DisasContext
*dc
)
267 static void gen_exception(DisasContext
*dc
, int excp
)
269 TCGv_i32 tmp
= tcg_const_i32(excp
);
270 gen_advance_ccount(dc
);
271 gen_helper_exception(cpu_env
, tmp
);
275 static void gen_exception_cause(DisasContext
*dc
, uint32_t cause
)
277 TCGv_i32 tpc
= tcg_const_i32(dc
->pc
);
278 TCGv_i32 tcause
= tcg_const_i32(cause
);
279 gen_advance_ccount(dc
);
280 gen_helper_exception_cause(cpu_env
, tpc
, tcause
);
282 tcg_temp_free(tcause
);
283 if (cause
== ILLEGAL_INSTRUCTION_CAUSE
||
284 cause
== SYSCALL_CAUSE
) {
285 dc
->is_jmp
= DISAS_UPDATE
;
289 static void gen_exception_cause_vaddr(DisasContext
*dc
, uint32_t cause
,
292 TCGv_i32 tpc
= tcg_const_i32(dc
->pc
);
293 TCGv_i32 tcause
= tcg_const_i32(cause
);
294 gen_advance_ccount(dc
);
295 gen_helper_exception_cause_vaddr(cpu_env
, tpc
, tcause
, vaddr
);
297 tcg_temp_free(tcause
);
300 static void gen_debug_exception(DisasContext
*dc
, uint32_t cause
)
302 TCGv_i32 tpc
= tcg_const_i32(dc
->pc
);
303 TCGv_i32 tcause
= tcg_const_i32(cause
);
304 gen_advance_ccount(dc
);
305 gen_helper_debug_exception(cpu_env
, tpc
, tcause
);
307 tcg_temp_free(tcause
);
308 if (cause
& (DEBUGCAUSE_IB
| DEBUGCAUSE_BI
| DEBUGCAUSE_BN
)) {
309 dc
->is_jmp
= DISAS_UPDATE
;
313 static void gen_check_privilege(DisasContext
*dc
)
316 gen_exception_cause(dc
, PRIVILEGED_CAUSE
);
317 dc
->is_jmp
= DISAS_UPDATE
;
321 static void gen_jump_slot(DisasContext
*dc
, TCGv dest
, int slot
)
323 tcg_gen_mov_i32(cpu_pc
, dest
);
324 gen_advance_ccount(dc
);
326 tcg_gen_mov_i32(cpu_SR
[ICOUNT
], dc
->next_icount
);
328 if (dc
->singlestep_enabled
) {
329 gen_exception(dc
, EXCP_DEBUG
);
332 tcg_gen_goto_tb(slot
);
333 tcg_gen_exit_tb((tcg_target_long
)dc
->tb
+ slot
);
338 dc
->is_jmp
= DISAS_UPDATE
;
341 static void gen_jump(DisasContext
*dc
, TCGv dest
)
343 gen_jump_slot(dc
, dest
, -1);
346 static void gen_jumpi(DisasContext
*dc
, uint32_t dest
, int slot
)
348 TCGv_i32 tmp
= tcg_const_i32(dest
);
349 if (((dc
->pc
^ dest
) & TARGET_PAGE_MASK
) != 0) {
352 gen_jump_slot(dc
, tmp
, slot
);
356 static void gen_callw_slot(DisasContext
*dc
, int callinc
, TCGv_i32 dest
,
359 TCGv_i32 tcallinc
= tcg_const_i32(callinc
);
361 tcg_gen_deposit_i32(cpu_SR
[PS
], cpu_SR
[PS
],
362 tcallinc
, PS_CALLINC_SHIFT
, PS_CALLINC_LEN
);
363 tcg_temp_free(tcallinc
);
364 tcg_gen_movi_i32(cpu_R
[callinc
<< 2],
365 (callinc
<< 30) | (dc
->next_pc
& 0x3fffffff));
366 gen_jump_slot(dc
, dest
, slot
);
369 static void gen_callw(DisasContext
*dc
, int callinc
, TCGv_i32 dest
)
371 gen_callw_slot(dc
, callinc
, dest
, -1);
374 static void gen_callwi(DisasContext
*dc
, int callinc
, uint32_t dest
, int slot
)
376 TCGv_i32 tmp
= tcg_const_i32(dest
);
377 if (((dc
->pc
^ dest
) & TARGET_PAGE_MASK
) != 0) {
380 gen_callw_slot(dc
, callinc
, tmp
, slot
);
384 static bool gen_check_loop_end(DisasContext
*dc
, int slot
)
386 if (option_enabled(dc
, XTENSA_OPTION_LOOP
) &&
387 !(dc
->tb
->flags
& XTENSA_TBFLAG_EXCM
) &&
388 dc
->next_pc
== dc
->lend
) {
389 int label
= gen_new_label();
391 gen_advance_ccount(dc
);
392 tcg_gen_brcondi_i32(TCG_COND_EQ
, cpu_SR
[LCOUNT
], 0, label
);
393 tcg_gen_subi_i32(cpu_SR
[LCOUNT
], cpu_SR
[LCOUNT
], 1);
394 gen_jumpi(dc
, dc
->lbeg
, slot
);
395 gen_set_label(label
);
396 gen_jumpi(dc
, dc
->next_pc
, -1);
402 static void gen_jumpi_check_loop_end(DisasContext
*dc
, int slot
)
404 if (!gen_check_loop_end(dc
, slot
)) {
405 gen_jumpi(dc
, dc
->next_pc
, slot
);
409 static void gen_brcond(DisasContext
*dc
, TCGCond cond
,
410 TCGv_i32 t0
, TCGv_i32 t1
, uint32_t offset
)
412 int label
= gen_new_label();
414 gen_advance_ccount(dc
);
415 tcg_gen_brcond_i32(cond
, t0
, t1
, label
);
416 gen_jumpi_check_loop_end(dc
, 0);
417 gen_set_label(label
);
418 gen_jumpi(dc
, dc
->pc
+ offset
, 1);
421 static void gen_brcondi(DisasContext
*dc
, TCGCond cond
,
422 TCGv_i32 t0
, uint32_t t1
, uint32_t offset
)
424 TCGv_i32 tmp
= tcg_const_i32(t1
);
425 gen_brcond(dc
, cond
, t0
, tmp
, offset
);
429 static void gen_rsr_ccount(DisasContext
*dc
, TCGv_i32 d
, uint32_t sr
)
431 gen_advance_ccount(dc
);
432 tcg_gen_mov_i32(d
, cpu_SR
[sr
]);
435 static void gen_rsr_ptevaddr(DisasContext
*dc
, TCGv_i32 d
, uint32_t sr
)
437 tcg_gen_shri_i32(d
, cpu_SR
[EXCVADDR
], 10);
438 tcg_gen_or_i32(d
, d
, cpu_SR
[sr
]);
439 tcg_gen_andi_i32(d
, d
, 0xfffffffc);
442 static void gen_rsr(DisasContext
*dc
, TCGv_i32 d
, uint32_t sr
)
444 static void (* const rsr_handler
[256])(DisasContext
*dc
,
445 TCGv_i32 d
, uint32_t sr
) = {
446 [CCOUNT
] = gen_rsr_ccount
,
447 [PTEVADDR
] = gen_rsr_ptevaddr
,
451 if (rsr_handler
[sr
]) {
452 rsr_handler
[sr
](dc
, d
, sr
);
454 tcg_gen_mov_i32(d
, cpu_SR
[sr
]);
457 qemu_log("RSR %d not implemented, ", sr
);
461 static void gen_wsr_lbeg(DisasContext
*dc
, uint32_t sr
, TCGv_i32 s
)
463 gen_helper_wsr_lbeg(cpu_env
, s
);
464 gen_jumpi_check_loop_end(dc
, 0);
467 static void gen_wsr_lend(DisasContext
*dc
, uint32_t sr
, TCGv_i32 s
)
469 gen_helper_wsr_lend(cpu_env
, s
);
470 gen_jumpi_check_loop_end(dc
, 0);
473 static void gen_wsr_sar(DisasContext
*dc
, uint32_t sr
, TCGv_i32 s
)
475 tcg_gen_andi_i32(cpu_SR
[sr
], s
, 0x3f);
476 if (dc
->sar_m32_5bit
) {
477 tcg_gen_discard_i32(dc
->sar_m32
);
479 dc
->sar_5bit
= false;
480 dc
->sar_m32_5bit
= false;
483 static void gen_wsr_br(DisasContext
*dc
, uint32_t sr
, TCGv_i32 s
)
485 tcg_gen_andi_i32(cpu_SR
[sr
], s
, 0xffff);
488 static void gen_wsr_litbase(DisasContext
*dc
, uint32_t sr
, TCGv_i32 s
)
490 tcg_gen_andi_i32(cpu_SR
[sr
], s
, 0xfffff001);
491 /* This can change tb->flags, so exit tb */
492 gen_jumpi_check_loop_end(dc
, -1);
495 static void gen_wsr_acchi(DisasContext
*dc
, uint32_t sr
, TCGv_i32 s
)
497 tcg_gen_ext8s_i32(cpu_SR
[sr
], s
);
500 static void gen_wsr_windowbase(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
502 gen_helper_wsr_windowbase(cpu_env
, v
);
503 reset_used_window(dc
);
506 static void gen_wsr_windowstart(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
508 tcg_gen_andi_i32(cpu_SR
[sr
], v
, (1 << dc
->config
->nareg
/ 4) - 1);
509 reset_used_window(dc
);
512 static void gen_wsr_ptevaddr(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
514 tcg_gen_andi_i32(cpu_SR
[sr
], v
, 0xffc00000);
517 static void gen_wsr_rasid(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
519 gen_helper_wsr_rasid(cpu_env
, v
);
520 /* This can change tb->flags, so exit tb */
521 gen_jumpi_check_loop_end(dc
, -1);
524 static void gen_wsr_tlbcfg(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
526 tcg_gen_andi_i32(cpu_SR
[sr
], v
, 0x01130000);
529 static void gen_wsr_ibreakenable(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
531 gen_helper_wsr_ibreakenable(cpu_env
, v
);
532 gen_jumpi_check_loop_end(dc
, 0);
535 static void gen_wsr_ibreaka(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
537 unsigned id
= sr
- IBREAKA
;
539 if (id
< dc
->config
->nibreak
) {
540 TCGv_i32 tmp
= tcg_const_i32(id
);
541 gen_helper_wsr_ibreaka(cpu_env
, tmp
, v
);
543 gen_jumpi_check_loop_end(dc
, 0);
547 static void gen_wsr_dbreaka(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
549 unsigned id
= sr
- DBREAKA
;
551 if (id
< dc
->config
->ndbreak
) {
552 TCGv_i32 tmp
= tcg_const_i32(id
);
553 gen_helper_wsr_dbreaka(cpu_env
, tmp
, v
);
558 static void gen_wsr_dbreakc(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
560 unsigned id
= sr
- DBREAKC
;
562 if (id
< dc
->config
->ndbreak
) {
563 TCGv_i32 tmp
= tcg_const_i32(id
);
564 gen_helper_wsr_dbreakc(cpu_env
, tmp
, v
);
569 static void gen_wsr_intset(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
571 tcg_gen_andi_i32(cpu_SR
[sr
], v
,
572 dc
->config
->inttype_mask
[INTTYPE_SOFTWARE
]);
573 gen_helper_check_interrupts(cpu_env
);
574 gen_jumpi_check_loop_end(dc
, 0);
577 static void gen_wsr_intclear(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
579 TCGv_i32 tmp
= tcg_temp_new_i32();
581 tcg_gen_andi_i32(tmp
, v
,
582 dc
->config
->inttype_mask
[INTTYPE_EDGE
] |
583 dc
->config
->inttype_mask
[INTTYPE_NMI
] |
584 dc
->config
->inttype_mask
[INTTYPE_SOFTWARE
]);
585 tcg_gen_andc_i32(cpu_SR
[INTSET
], cpu_SR
[INTSET
], tmp
);
587 gen_helper_check_interrupts(cpu_env
);
590 static void gen_wsr_intenable(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
592 tcg_gen_mov_i32(cpu_SR
[sr
], v
);
593 gen_helper_check_interrupts(cpu_env
);
594 gen_jumpi_check_loop_end(dc
, 0);
597 static void gen_wsr_ps(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
599 uint32_t mask
= PS_WOE
| PS_CALLINC
| PS_OWB
|
600 PS_UM
| PS_EXCM
| PS_INTLEVEL
;
602 if (option_enabled(dc
, XTENSA_OPTION_MMU
)) {
605 tcg_gen_andi_i32(cpu_SR
[sr
], v
, mask
);
606 reset_used_window(dc
);
607 gen_helper_check_interrupts(cpu_env
);
608 /* This can change mmu index and tb->flags, so exit tb */
609 gen_jumpi_check_loop_end(dc
, -1);
612 static void gen_wsr_debugcause(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
616 static void gen_wsr_prid(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
620 static void gen_wsr_icount(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
623 tcg_gen_mov_i32(dc
->next_icount
, v
);
625 tcg_gen_mov_i32(cpu_SR
[sr
], v
);
629 static void gen_wsr_icountlevel(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
631 tcg_gen_andi_i32(cpu_SR
[sr
], v
, 0xf);
632 /* This can change tb->flags, so exit tb */
633 gen_jumpi_check_loop_end(dc
, -1);
636 static void gen_wsr_ccompare(DisasContext
*dc
, uint32_t sr
, TCGv_i32 v
)
638 uint32_t id
= sr
- CCOMPARE
;
639 if (id
< dc
->config
->nccompare
) {
640 uint32_t int_bit
= 1 << dc
->config
->timerint
[id
];
641 gen_advance_ccount(dc
);
642 tcg_gen_mov_i32(cpu_SR
[sr
], v
);
643 tcg_gen_andi_i32(cpu_SR
[INTSET
], cpu_SR
[INTSET
], ~int_bit
);
644 gen_helper_check_interrupts(cpu_env
);
648 static void gen_wsr(DisasContext
*dc
, uint32_t sr
, TCGv_i32 s
)
650 static void (* const wsr_handler
[256])(DisasContext
*dc
,
651 uint32_t sr
, TCGv_i32 v
) = {
652 [LBEG
] = gen_wsr_lbeg
,
653 [LEND
] = gen_wsr_lend
,
656 [LITBASE
] = gen_wsr_litbase
,
657 [ACCHI
] = gen_wsr_acchi
,
658 [WINDOW_BASE
] = gen_wsr_windowbase
,
659 [WINDOW_START
] = gen_wsr_windowstart
,
660 [PTEVADDR
] = gen_wsr_ptevaddr
,
661 [RASID
] = gen_wsr_rasid
,
662 [ITLBCFG
] = gen_wsr_tlbcfg
,
663 [DTLBCFG
] = gen_wsr_tlbcfg
,
664 [IBREAKENABLE
] = gen_wsr_ibreakenable
,
665 [IBREAKA
] = gen_wsr_ibreaka
,
666 [IBREAKA
+ 1] = gen_wsr_ibreaka
,
667 [DBREAKA
] = gen_wsr_dbreaka
,
668 [DBREAKA
+ 1] = gen_wsr_dbreaka
,
669 [DBREAKC
] = gen_wsr_dbreakc
,
670 [DBREAKC
+ 1] = gen_wsr_dbreakc
,
671 [INTSET
] = gen_wsr_intset
,
672 [INTCLEAR
] = gen_wsr_intclear
,
673 [INTENABLE
] = gen_wsr_intenable
,
675 [DEBUGCAUSE
] = gen_wsr_debugcause
,
676 [PRID
] = gen_wsr_prid
,
677 [ICOUNT
] = gen_wsr_icount
,
678 [ICOUNTLEVEL
] = gen_wsr_icountlevel
,
679 [CCOMPARE
] = gen_wsr_ccompare
,
680 [CCOMPARE
+ 1] = gen_wsr_ccompare
,
681 [CCOMPARE
+ 2] = gen_wsr_ccompare
,
685 if (wsr_handler
[sr
]) {
686 wsr_handler
[sr
](dc
, sr
, s
);
688 tcg_gen_mov_i32(cpu_SR
[sr
], s
);
691 qemu_log("WSR %d not implemented, ", sr
);
695 static void gen_load_store_alignment(DisasContext
*dc
, int shift
,
696 TCGv_i32 addr
, bool no_hw_alignment
)
698 if (!option_enabled(dc
, XTENSA_OPTION_UNALIGNED_EXCEPTION
)) {
699 tcg_gen_andi_i32(addr
, addr
, ~0 << shift
);
700 } else if (option_enabled(dc
, XTENSA_OPTION_HW_ALIGNMENT
) &&
702 int label
= gen_new_label();
703 TCGv_i32 tmp
= tcg_temp_new_i32();
704 tcg_gen_andi_i32(tmp
, addr
, ~(~0 << shift
));
705 tcg_gen_brcondi_i32(TCG_COND_EQ
, tmp
, 0, label
);
706 gen_exception_cause_vaddr(dc
, LOAD_STORE_ALIGNMENT_CAUSE
, addr
);
707 gen_set_label(label
);
712 static void gen_waiti(DisasContext
*dc
, uint32_t imm4
)
714 TCGv_i32 pc
= tcg_const_i32(dc
->next_pc
);
715 TCGv_i32 intlevel
= tcg_const_i32(imm4
);
716 gen_advance_ccount(dc
);
717 gen_helper_waiti(cpu_env
, pc
, intlevel
);
719 tcg_temp_free(intlevel
);
722 static void gen_window_check1(DisasContext
*dc
, unsigned r1
)
724 if (dc
->tb
->flags
& XTENSA_TBFLAG_EXCM
) {
727 if (option_enabled(dc
, XTENSA_OPTION_WINDOWED_REGISTER
) &&
728 r1
/ 4 > dc
->used_window
) {
729 TCGv_i32 pc
= tcg_const_i32(dc
->pc
);
730 TCGv_i32 w
= tcg_const_i32(r1
/ 4);
732 dc
->used_window
= r1
/ 4;
733 gen_advance_ccount(dc
);
734 gen_helper_window_check(cpu_env
, pc
, w
);
741 static void gen_window_check2(DisasContext
*dc
, unsigned r1
, unsigned r2
)
743 gen_window_check1(dc
, r1
> r2
? r1
: r2
);
746 static void gen_window_check3(DisasContext
*dc
, unsigned r1
, unsigned r2
,
749 gen_window_check2(dc
, r1
, r2
> r3
? r2
: r3
);
752 static TCGv_i32
gen_mac16_m(TCGv_i32 v
, bool hi
, bool is_unsigned
)
754 TCGv_i32 m
= tcg_temp_new_i32();
757 (is_unsigned
? tcg_gen_shri_i32
: tcg_gen_sari_i32
)(m
, v
, 16);
759 (is_unsigned
? tcg_gen_ext16u_i32
: tcg_gen_ext16s_i32
)(m
, v
);
764 static void disas_xtensa_insn(DisasContext
*dc
)
766 #define HAS_OPTION_BITS(opt) do { \
767 if (!option_bits_enabled(dc, opt)) { \
768 qemu_log("Option is not enabled %s:%d\n", \
769 __FILE__, __LINE__); \
770 goto invalid_opcode; \
774 #define HAS_OPTION(opt) HAS_OPTION_BITS(XTENSA_OPTION_BIT(opt))
776 #define TBD() qemu_log("TBD(pc = %08x): %s:%d\n", dc->pc, __FILE__, __LINE__)
777 #define RESERVED() do { \
778 qemu_log("RESERVED(pc = %08x, %02x%02x%02x): %s:%d\n", \
779 dc->pc, b0, b1, b2, __FILE__, __LINE__); \
780 goto invalid_opcode; \
784 #ifdef TARGET_WORDS_BIGENDIAN
785 #define OP0 (((b0) & 0xf0) >> 4)
786 #define OP1 (((b2) & 0xf0) >> 4)
787 #define OP2 ((b2) & 0xf)
788 #define RRR_R ((b1) & 0xf)
789 #define RRR_S (((b1) & 0xf0) >> 4)
790 #define RRR_T ((b0) & 0xf)
792 #define OP0 (((b0) & 0xf))
793 #define OP1 (((b2) & 0xf))
794 #define OP2 (((b2) & 0xf0) >> 4)
795 #define RRR_R (((b1) & 0xf0) >> 4)
796 #define RRR_S (((b1) & 0xf))
797 #define RRR_T (((b0) & 0xf0) >> 4)
799 #define RRR_X ((RRR_R & 0x4) >> 2)
800 #define RRR_Y ((RRR_T & 0x4) >> 2)
801 #define RRR_W (RRR_R & 0x3)
810 #define RRI8_IMM8 (b2)
811 #define RRI8_IMM8_SE ((((b2) & 0x80) ? 0xffffff00 : 0) | RRI8_IMM8)
813 #ifdef TARGET_WORDS_BIGENDIAN
814 #define RI16_IMM16 (((b1) << 8) | (b2))
816 #define RI16_IMM16 (((b2) << 8) | (b1))
819 #ifdef TARGET_WORDS_BIGENDIAN
820 #define CALL_N (((b0) & 0xc) >> 2)
821 #define CALL_OFFSET ((((b0) & 0x3) << 16) | ((b1) << 8) | (b2))
823 #define CALL_N (((b0) & 0x30) >> 4)
824 #define CALL_OFFSET ((((b0) & 0xc0) >> 6) | ((b1) << 2) | ((b2) << 10))
826 #define CALL_OFFSET_SE \
827 (((CALL_OFFSET & 0x20000) ? 0xfffc0000 : 0) | CALL_OFFSET)
829 #define CALLX_N CALL_N
830 #ifdef TARGET_WORDS_BIGENDIAN
831 #define CALLX_M ((b0) & 0x3)
833 #define CALLX_M (((b0) & 0xc0) >> 6)
835 #define CALLX_S RRR_S
837 #define BRI12_M CALLX_M
838 #define BRI12_S RRR_S
839 #ifdef TARGET_WORDS_BIGENDIAN
840 #define BRI12_IMM12 ((((b1) & 0xf) << 8) | (b2))
842 #define BRI12_IMM12 ((((b1) & 0xf0) >> 4) | ((b2) << 4))
844 #define BRI12_IMM12_SE (((BRI12_IMM12 & 0x800) ? 0xfffff000 : 0) | BRI12_IMM12)
846 #define BRI8_M BRI12_M
847 #define BRI8_R RRI8_R
848 #define BRI8_S RRI8_S
849 #define BRI8_IMM8 RRI8_IMM8
850 #define BRI8_IMM8_SE RRI8_IMM8_SE
854 uint8_t b0
= cpu_ldub_code(cpu_single_env
, dc
->pc
);
855 uint8_t b1
= cpu_ldub_code(cpu_single_env
, dc
->pc
+ 1);
858 static const uint32_t B4CONST
[] = {
859 0xffffffff, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 16, 32, 64, 128, 256
862 static const uint32_t B4CONSTU
[] = {
863 32768, 65536, 2, 3, 4, 5, 6, 7, 8, 10, 12, 16, 32, 64, 128, 256
867 dc
->next_pc
= dc
->pc
+ 2;
868 HAS_OPTION(XTENSA_OPTION_CODE_DENSITY
);
870 dc
->next_pc
= dc
->pc
+ 3;
871 b2
= cpu_ldub_code(cpu_single_env
, dc
->pc
+ 2);
880 if ((RRR_R
& 0xc) == 0x8) {
881 HAS_OPTION(XTENSA_OPTION_BOOLEAN
);
888 gen_exception_cause(dc
, ILLEGAL_INSTRUCTION_CAUSE
);
899 gen_window_check1(dc
, CALLX_S
);
900 gen_jump(dc
, cpu_R
[CALLX_S
]);
904 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER
);
906 TCGv_i32 tmp
= tcg_const_i32(dc
->pc
);
907 gen_advance_ccount(dc
);
908 gen_helper_retw(tmp
, cpu_env
, tmp
);
921 gen_window_check2(dc
, CALLX_S
, CALLX_N
<< 2);
925 TCGv_i32 tmp
= tcg_temp_new_i32();
926 tcg_gen_mov_i32(tmp
, cpu_R
[CALLX_S
]);
927 tcg_gen_movi_i32(cpu_R
[0], dc
->next_pc
);
936 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER
);
938 TCGv_i32 tmp
= tcg_temp_new_i32();
940 tcg_gen_mov_i32(tmp
, cpu_R
[CALLX_S
]);
941 gen_callw(dc
, CALLX_N
, tmp
);
951 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER
);
952 gen_window_check2(dc
, RRR_T
, RRR_S
);
954 TCGv_i32 pc
= tcg_const_i32(dc
->pc
);
955 gen_advance_ccount(dc
);
956 gen_helper_movsp(cpu_env
, pc
);
957 tcg_gen_mov_i32(cpu_R
[RRR_T
], cpu_R
[RRR_S
]);
977 HAS_OPTION(XTENSA_OPTION_EXCEPTION
);
989 default: /*reserved*/
998 HAS_OPTION(XTENSA_OPTION_EXCEPTION
);
1001 gen_check_privilege(dc
);
1002 tcg_gen_andi_i32(cpu_SR
[PS
], cpu_SR
[PS
], ~PS_EXCM
);
1003 gen_helper_check_interrupts(cpu_env
);
1004 gen_jump(dc
, cpu_SR
[EPC1
]);
1012 gen_check_privilege(dc
);
1013 gen_jump(dc
, cpu_SR
[
1014 dc
->config
->ndepc
? DEPC
: EPC1
]);
1019 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER
);
1020 gen_check_privilege(dc
);
1022 TCGv_i32 tmp
= tcg_const_i32(1);
1025 cpu_SR
[PS
], cpu_SR
[PS
], ~PS_EXCM
);
1026 tcg_gen_shl_i32(tmp
, tmp
, cpu_SR
[WINDOW_BASE
]);
1029 tcg_gen_andc_i32(cpu_SR
[WINDOW_START
],
1030 cpu_SR
[WINDOW_START
], tmp
);
1032 tcg_gen_or_i32(cpu_SR
[WINDOW_START
],
1033 cpu_SR
[WINDOW_START
], tmp
);
1036 gen_helper_restore_owb(cpu_env
);
1037 gen_helper_check_interrupts(cpu_env
);
1038 gen_jump(dc
, cpu_SR
[EPC1
]);
1044 default: /*reserved*/
1051 HAS_OPTION(XTENSA_OPTION_HIGH_PRIORITY_INTERRUPT
);
1052 if (RRR_S
>= 2 && RRR_S
<= dc
->config
->nlevel
) {
1053 gen_check_privilege(dc
);
1054 tcg_gen_mov_i32(cpu_SR
[PS
],
1055 cpu_SR
[EPS2
+ RRR_S
- 2]);
1056 gen_helper_check_interrupts(cpu_env
);
1057 gen_jump(dc
, cpu_SR
[EPC1
+ RRR_S
- 1]);
1059 qemu_log("RFI %d is illegal\n", RRR_S
);
1060 gen_exception_cause(dc
, ILLEGAL_INSTRUCTION_CAUSE
);
1068 default: /*reserved*/
1076 HAS_OPTION(XTENSA_OPTION_DEBUG
);
1078 gen_debug_exception(dc
, DEBUGCAUSE_BI
);
1082 case 5: /*SYSCALLx*/
1083 HAS_OPTION(XTENSA_OPTION_EXCEPTION
);
1085 case 0: /*SYSCALLx*/
1086 gen_exception_cause(dc
, SYSCALL_CAUSE
);
1090 if (semihosting_enabled
) {
1091 gen_check_privilege(dc
);
1092 gen_helper_simcall(cpu_env
);
1094 qemu_log("SIMCALL but semihosting is disabled\n");
1095 gen_exception_cause(dc
, ILLEGAL_INSTRUCTION_CAUSE
);
1106 HAS_OPTION(XTENSA_OPTION_INTERRUPT
);
1107 gen_check_privilege(dc
);
1108 gen_window_check1(dc
, RRR_T
);
1109 tcg_gen_mov_i32(cpu_R
[RRR_T
], cpu_SR
[PS
]);
1110 tcg_gen_andi_i32(cpu_SR
[PS
], cpu_SR
[PS
], ~PS_INTLEVEL
);
1111 tcg_gen_ori_i32(cpu_SR
[PS
], cpu_SR
[PS
], RRR_S
);
1112 gen_helper_check_interrupts(cpu_env
);
1113 gen_jumpi_check_loop_end(dc
, 0);
1117 HAS_OPTION(XTENSA_OPTION_INTERRUPT
);
1118 gen_check_privilege(dc
);
1119 gen_waiti(dc
, RRR_S
);
1126 HAS_OPTION(XTENSA_OPTION_BOOLEAN
);
1128 const unsigned shift
= (RRR_R
& 2) ? 8 : 4;
1129 TCGv_i32 mask
= tcg_const_i32(
1130 ((1 << shift
) - 1) << RRR_S
);
1131 TCGv_i32 tmp
= tcg_temp_new_i32();
1133 tcg_gen_and_i32(tmp
, cpu_SR
[BR
], mask
);
1134 if (RRR_R
& 1) { /*ALL*/
1135 tcg_gen_addi_i32(tmp
, tmp
, 1 << RRR_S
);
1137 tcg_gen_add_i32(tmp
, tmp
, mask
);
1139 tcg_gen_shri_i32(tmp
, tmp
, RRR_S
+ shift
);
1140 tcg_gen_deposit_i32(cpu_SR
[BR
], cpu_SR
[BR
],
1142 tcg_temp_free(mask
);
1147 default: /*reserved*/
1155 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1156 tcg_gen_and_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
], cpu_R
[RRR_T
]);
1160 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1161 tcg_gen_or_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
], cpu_R
[RRR_T
]);
1165 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1166 tcg_gen_xor_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
], cpu_R
[RRR_T
]);
1172 gen_window_check1(dc
, RRR_S
);
1173 gen_right_shift_sar(dc
, cpu_R
[RRR_S
]);
1177 gen_window_check1(dc
, RRR_S
);
1178 gen_left_shift_sar(dc
, cpu_R
[RRR_S
]);
1182 gen_window_check1(dc
, RRR_S
);
1184 TCGv_i32 tmp
= tcg_temp_new_i32();
1185 tcg_gen_shli_i32(tmp
, cpu_R
[RRR_S
], 3);
1186 gen_right_shift_sar(dc
, tmp
);
1192 gen_window_check1(dc
, RRR_S
);
1194 TCGv_i32 tmp
= tcg_temp_new_i32();
1195 tcg_gen_shli_i32(tmp
, cpu_R
[RRR_S
], 3);
1196 gen_left_shift_sar(dc
, tmp
);
1203 TCGv_i32 tmp
= tcg_const_i32(
1204 RRR_S
| ((RRR_T
& 1) << 4));
1205 gen_right_shift_sar(dc
, tmp
);
1219 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER
);
1220 gen_check_privilege(dc
);
1222 TCGv_i32 tmp
= tcg_const_i32(
1223 RRR_T
| ((RRR_T
& 8) ? 0xfffffff0 : 0));
1224 gen_helper_rotw(cpu_env
, tmp
);
1226 reset_used_window(dc
);
1231 HAS_OPTION(XTENSA_OPTION_MISC_OP_NSA
);
1232 gen_window_check2(dc
, RRR_S
, RRR_T
);
1233 gen_helper_nsa(cpu_R
[RRR_T
], cpu_R
[RRR_S
]);
1237 HAS_OPTION(XTENSA_OPTION_MISC_OP_NSA
);
1238 gen_window_check2(dc
, RRR_S
, RRR_T
);
1239 gen_helper_nsau(cpu_R
[RRR_T
], cpu_R
[RRR_S
]);
1242 default: /*reserved*/
1250 XTENSA_OPTION_BIT(XTENSA_OPTION_MMU
) |
1251 XTENSA_OPTION_BIT(XTENSA_OPTION_REGION_PROTECTION
) |
1252 XTENSA_OPTION_BIT(XTENSA_OPTION_REGION_TRANSLATION
));
1253 gen_check_privilege(dc
);
1254 gen_window_check2(dc
, RRR_S
, RRR_T
);
1256 TCGv_i32 dtlb
= tcg_const_i32((RRR_R
& 8) != 0);
1258 switch (RRR_R
& 7) {
1259 case 3: /*RITLB0*/ /*RDTLB0*/
1260 gen_helper_rtlb0(cpu_R
[RRR_T
],
1261 cpu_env
, cpu_R
[RRR_S
], dtlb
);
1264 case 4: /*IITLB*/ /*IDTLB*/
1265 gen_helper_itlb(cpu_env
, cpu_R
[RRR_S
], dtlb
);
1266 /* This could change memory mapping, so exit tb */
1267 gen_jumpi_check_loop_end(dc
, -1);
1270 case 5: /*PITLB*/ /*PDTLB*/
1271 tcg_gen_movi_i32(cpu_pc
, dc
->pc
);
1272 gen_helper_ptlb(cpu_R
[RRR_T
],
1273 cpu_env
, cpu_R
[RRR_S
], dtlb
);
1276 case 6: /*WITLB*/ /*WDTLB*/
1278 cpu_env
, cpu_R
[RRR_T
], cpu_R
[RRR_S
], dtlb
);
1279 /* This could change memory mapping, so exit tb */
1280 gen_jumpi_check_loop_end(dc
, -1);
1283 case 7: /*RITLB1*/ /*RDTLB1*/
1284 gen_helper_rtlb1(cpu_R
[RRR_T
],
1285 cpu_env
, cpu_R
[RRR_S
], dtlb
);
1289 tcg_temp_free(dtlb
);
1293 tcg_temp_free(dtlb
);
1298 gen_window_check2(dc
, RRR_R
, RRR_T
);
1301 tcg_gen_neg_i32(cpu_R
[RRR_R
], cpu_R
[RRR_T
]);
1306 int label
= gen_new_label();
1307 tcg_gen_mov_i32(cpu_R
[RRR_R
], cpu_R
[RRR_T
]);
1308 tcg_gen_brcondi_i32(
1309 TCG_COND_GE
, cpu_R
[RRR_R
], 0, label
);
1310 tcg_gen_neg_i32(cpu_R
[RRR_R
], cpu_R
[RRR_T
]);
1311 gen_set_label(label
);
1315 default: /*reserved*/
1321 case 7: /*reserved*/
1326 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1327 tcg_gen_add_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
], cpu_R
[RRR_T
]);
1333 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1335 TCGv_i32 tmp
= tcg_temp_new_i32();
1336 tcg_gen_shli_i32(tmp
, cpu_R
[RRR_S
], OP2
- 8);
1337 tcg_gen_add_i32(cpu_R
[RRR_R
], tmp
, cpu_R
[RRR_T
]);
1343 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1344 tcg_gen_sub_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
], cpu_R
[RRR_T
]);
1350 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1352 TCGv_i32 tmp
= tcg_temp_new_i32();
1353 tcg_gen_shli_i32(tmp
, cpu_R
[RRR_S
], OP2
- 12);
1354 tcg_gen_sub_i32(cpu_R
[RRR_R
], tmp
, cpu_R
[RRR_T
]);
1365 gen_window_check2(dc
, RRR_R
, RRR_S
);
1366 tcg_gen_shli_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
],
1367 32 - (RRR_T
| ((OP2
& 1) << 4)));
1372 gen_window_check2(dc
, RRR_R
, RRR_T
);
1373 tcg_gen_sari_i32(cpu_R
[RRR_R
], cpu_R
[RRR_T
],
1374 RRR_S
| ((OP2
& 1) << 4));
1378 gen_window_check2(dc
, RRR_R
, RRR_T
);
1379 tcg_gen_shri_i32(cpu_R
[RRR_R
], cpu_R
[RRR_T
], RRR_S
);
1384 TCGv_i32 tmp
= tcg_temp_new_i32();
1386 gen_check_privilege(dc
);
1388 gen_window_check1(dc
, RRR_T
);
1389 tcg_gen_mov_i32(tmp
, cpu_R
[RRR_T
]);
1390 gen_rsr(dc
, cpu_R
[RRR_T
], RSR_SR
);
1391 gen_wsr(dc
, RSR_SR
, tmp
);
1393 if (!sregnames
[RSR_SR
]) {
1400 * Note: 64 bit ops are used here solely because SAR values
1403 #define gen_shift_reg(cmd, reg) do { \
1404 TCGv_i64 tmp = tcg_temp_new_i64(); \
1405 tcg_gen_extu_i32_i64(tmp, reg); \
1406 tcg_gen_##cmd##_i64(v, v, tmp); \
1407 tcg_gen_trunc_i64_i32(cpu_R[RRR_R], v); \
1408 tcg_temp_free_i64(v); \
1409 tcg_temp_free_i64(tmp); \
1412 #define gen_shift(cmd) gen_shift_reg(cmd, cpu_SR[SAR])
1415 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1417 TCGv_i64 v
= tcg_temp_new_i64();
1418 tcg_gen_concat_i32_i64(v
, cpu_R
[RRR_T
], cpu_R
[RRR_S
]);
1424 gen_window_check2(dc
, RRR_R
, RRR_T
);
1426 tcg_gen_shr_i32(cpu_R
[RRR_R
], cpu_R
[RRR_T
], cpu_SR
[SAR
]);
1428 TCGv_i64 v
= tcg_temp_new_i64();
1429 tcg_gen_extu_i32_i64(v
, cpu_R
[RRR_T
]);
1435 gen_window_check2(dc
, RRR_R
, RRR_S
);
1436 if (dc
->sar_m32_5bit
) {
1437 tcg_gen_shl_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
], dc
->sar_m32
);
1439 TCGv_i64 v
= tcg_temp_new_i64();
1440 TCGv_i32 s
= tcg_const_i32(32);
1441 tcg_gen_sub_i32(s
, s
, cpu_SR
[SAR
]);
1442 tcg_gen_andi_i32(s
, s
, 0x3f);
1443 tcg_gen_extu_i32_i64(v
, cpu_R
[RRR_S
]);
1444 gen_shift_reg(shl
, s
);
1450 gen_window_check2(dc
, RRR_R
, RRR_T
);
1452 tcg_gen_sar_i32(cpu_R
[RRR_R
], cpu_R
[RRR_T
], cpu_SR
[SAR
]);
1454 TCGv_i64 v
= tcg_temp_new_i64();
1455 tcg_gen_ext_i32_i64(v
, cpu_R
[RRR_T
]);
1460 #undef gen_shift_reg
1463 HAS_OPTION(XTENSA_OPTION_16_BIT_IMUL
);
1464 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1466 TCGv_i32 v1
= tcg_temp_new_i32();
1467 TCGv_i32 v2
= tcg_temp_new_i32();
1468 tcg_gen_ext16u_i32(v1
, cpu_R
[RRR_S
]);
1469 tcg_gen_ext16u_i32(v2
, cpu_R
[RRR_T
]);
1470 tcg_gen_mul_i32(cpu_R
[RRR_R
], v1
, v2
);
1477 HAS_OPTION(XTENSA_OPTION_16_BIT_IMUL
);
1478 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1480 TCGv_i32 v1
= tcg_temp_new_i32();
1481 TCGv_i32 v2
= tcg_temp_new_i32();
1482 tcg_gen_ext16s_i32(v1
, cpu_R
[RRR_S
]);
1483 tcg_gen_ext16s_i32(v2
, cpu_R
[RRR_T
]);
1484 tcg_gen_mul_i32(cpu_R
[RRR_R
], v1
, v2
);
1490 default: /*reserved*/
1498 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1502 HAS_OPTION(XTENSA_OPTION_32_BIT_IDIV
);
1503 int label
= gen_new_label();
1504 tcg_gen_brcondi_i32(TCG_COND_NE
, cpu_R
[RRR_T
], 0, label
);
1505 gen_exception_cause(dc
, INTEGER_DIVIDE_BY_ZERO_CAUSE
);
1506 gen_set_label(label
);
1510 #define BOOLEAN_LOGIC(fn, r, s, t) \
1512 HAS_OPTION(XTENSA_OPTION_BOOLEAN); \
1513 TCGv_i32 tmp1 = tcg_temp_new_i32(); \
1514 TCGv_i32 tmp2 = tcg_temp_new_i32(); \
1516 tcg_gen_shri_i32(tmp1, cpu_SR[BR], s); \
1517 tcg_gen_shri_i32(tmp2, cpu_SR[BR], t); \
1518 tcg_gen_##fn##_i32(tmp1, tmp1, tmp2); \
1519 tcg_gen_deposit_i32(cpu_SR[BR], cpu_SR[BR], tmp1, r, 1); \
1520 tcg_temp_free(tmp1); \
1521 tcg_temp_free(tmp2); \
1525 BOOLEAN_LOGIC(and, RRR_R
, RRR_S
, RRR_T
);
1529 BOOLEAN_LOGIC(andc
, RRR_R
, RRR_S
, RRR_T
);
1533 BOOLEAN_LOGIC(or, RRR_R
, RRR_S
, RRR_T
);
1537 BOOLEAN_LOGIC(orc
, RRR_R
, RRR_S
, RRR_T
);
1541 BOOLEAN_LOGIC(xor, RRR_R
, RRR_S
, RRR_T
);
1544 #undef BOOLEAN_LOGIC
1547 HAS_OPTION(XTENSA_OPTION_32_BIT_IMUL
);
1548 tcg_gen_mul_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
], cpu_R
[RRR_T
]);
1553 HAS_OPTION(XTENSA_OPTION_32_BIT_IMUL_HIGH
);
1555 TCGv_i64 r
= tcg_temp_new_i64();
1556 TCGv_i64 s
= tcg_temp_new_i64();
1557 TCGv_i64 t
= tcg_temp_new_i64();
1560 tcg_gen_extu_i32_i64(s
, cpu_R
[RRR_S
]);
1561 tcg_gen_extu_i32_i64(t
, cpu_R
[RRR_T
]);
1563 tcg_gen_ext_i32_i64(s
, cpu_R
[RRR_S
]);
1564 tcg_gen_ext_i32_i64(t
, cpu_R
[RRR_T
]);
1566 tcg_gen_mul_i64(r
, s
, t
);
1567 tcg_gen_shri_i64(r
, r
, 32);
1568 tcg_gen_trunc_i64_i32(cpu_R
[RRR_R
], r
);
1570 tcg_temp_free_i64(r
);
1571 tcg_temp_free_i64(s
);
1572 tcg_temp_free_i64(t
);
1577 tcg_gen_divu_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
], cpu_R
[RRR_T
]);
1583 int label1
= gen_new_label();
1584 int label2
= gen_new_label();
1586 tcg_gen_brcondi_i32(TCG_COND_NE
, cpu_R
[RRR_S
], 0x80000000,
1588 tcg_gen_brcondi_i32(TCG_COND_NE
, cpu_R
[RRR_T
], 0xffffffff,
1590 tcg_gen_movi_i32(cpu_R
[RRR_R
],
1591 OP2
== 13 ? 0x80000000 : 0);
1593 gen_set_label(label1
);
1595 tcg_gen_div_i32(cpu_R
[RRR_R
],
1596 cpu_R
[RRR_S
], cpu_R
[RRR_T
]);
1598 tcg_gen_rem_i32(cpu_R
[RRR_R
],
1599 cpu_R
[RRR_S
], cpu_R
[RRR_T
]);
1601 gen_set_label(label2
);
1606 tcg_gen_remu_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
], cpu_R
[RRR_T
]);
1609 default: /*reserved*/
1619 gen_check_privilege(dc
);
1621 gen_window_check1(dc
, RRR_T
);
1622 gen_rsr(dc
, cpu_R
[RRR_T
], RSR_SR
);
1623 if (!sregnames
[RSR_SR
]) {
1630 gen_check_privilege(dc
);
1632 gen_window_check1(dc
, RRR_T
);
1633 gen_wsr(dc
, RSR_SR
, cpu_R
[RRR_T
]);
1634 if (!sregnames
[RSR_SR
]) {
1640 HAS_OPTION(XTENSA_OPTION_MISC_OP_SEXT
);
1641 gen_window_check2(dc
, RRR_R
, RRR_S
);
1643 int shift
= 24 - RRR_T
;
1646 tcg_gen_ext8s_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
]);
1647 } else if (shift
== 16) {
1648 tcg_gen_ext16s_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
]);
1650 TCGv_i32 tmp
= tcg_temp_new_i32();
1651 tcg_gen_shli_i32(tmp
, cpu_R
[RRR_S
], shift
);
1652 tcg_gen_sari_i32(cpu_R
[RRR_R
], tmp
, shift
);
1659 HAS_OPTION(XTENSA_OPTION_MISC_OP_CLAMPS
);
1660 gen_window_check2(dc
, RRR_R
, RRR_S
);
1662 TCGv_i32 tmp1
= tcg_temp_new_i32();
1663 TCGv_i32 tmp2
= tcg_temp_new_i32();
1664 int label
= gen_new_label();
1666 tcg_gen_sari_i32(tmp1
, cpu_R
[RRR_S
], 24 - RRR_T
);
1667 tcg_gen_xor_i32(tmp2
, tmp1
, cpu_R
[RRR_S
]);
1668 tcg_gen_andi_i32(tmp2
, tmp2
, 0xffffffff << (RRR_T
+ 7));
1669 tcg_gen_mov_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
]);
1670 tcg_gen_brcondi_i32(TCG_COND_EQ
, tmp2
, 0, label
);
1672 tcg_gen_sari_i32(tmp1
, cpu_R
[RRR_S
], 31);
1673 tcg_gen_xori_i32(cpu_R
[RRR_R
], tmp1
,
1674 0xffffffff >> (25 - RRR_T
));
1676 gen_set_label(label
);
1678 tcg_temp_free(tmp1
);
1679 tcg_temp_free(tmp2
);
1687 HAS_OPTION(XTENSA_OPTION_MISC_OP_MINMAX
);
1688 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1690 static const TCGCond cond
[] = {
1696 int label
= gen_new_label();
1698 if (RRR_R
!= RRR_T
) {
1699 tcg_gen_mov_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
]);
1700 tcg_gen_brcond_i32(cond
[OP2
- 4],
1701 cpu_R
[RRR_S
], cpu_R
[RRR_T
], label
);
1702 tcg_gen_mov_i32(cpu_R
[RRR_R
], cpu_R
[RRR_T
]);
1704 tcg_gen_brcond_i32(cond
[OP2
- 4],
1705 cpu_R
[RRR_T
], cpu_R
[RRR_S
], label
);
1706 tcg_gen_mov_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
]);
1708 gen_set_label(label
);
1716 gen_window_check3(dc
, RRR_R
, RRR_S
, RRR_T
);
1718 static const TCGCond cond
[] = {
1724 int label
= gen_new_label();
1725 tcg_gen_brcondi_i32(cond
[OP2
- 8], cpu_R
[RRR_T
], 0, label
);
1726 tcg_gen_mov_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
]);
1727 gen_set_label(label
);
1733 HAS_OPTION(XTENSA_OPTION_BOOLEAN
);
1734 gen_window_check2(dc
, RRR_R
, RRR_S
);
1736 int label
= gen_new_label();
1737 TCGv_i32 tmp
= tcg_temp_new_i32();
1739 tcg_gen_andi_i32(tmp
, cpu_SR
[BR
], 1 << RRR_T
);
1740 tcg_gen_brcondi_i32(
1741 OP2
& 1 ? TCG_COND_EQ
: TCG_COND_NE
,
1743 tcg_gen_mov_i32(cpu_R
[RRR_R
], cpu_R
[RRR_S
]);
1744 gen_set_label(label
);
1750 gen_window_check1(dc
, RRR_R
);
1752 int st
= (RRR_S
<< 4) + RRR_T
;
1753 if (uregnames
[st
]) {
1754 tcg_gen_mov_i32(cpu_R
[RRR_R
], cpu_UR
[st
]);
1756 qemu_log("RUR %d not implemented, ", st
);
1763 gen_window_check1(dc
, RRR_T
);
1765 if (uregnames
[RSR_SR
]) {
1766 tcg_gen_mov_i32(cpu_UR
[RSR_SR
], cpu_R
[RRR_T
]);
1768 qemu_log("WUR %d not implemented, ", RSR_SR
);
1779 gen_window_check2(dc
, RRR_R
, RRR_T
);
1781 int shiftimm
= RRR_S
| (OP1
<< 4);
1782 int maskimm
= (1 << (OP2
+ 1)) - 1;
1784 TCGv_i32 tmp
= tcg_temp_new_i32();
1785 tcg_gen_shri_i32(tmp
, cpu_R
[RRR_T
], shiftimm
);
1786 tcg_gen_andi_i32(cpu_R
[RRR_R
], tmp
, maskimm
);
1800 HAS_OPTION(XTENSA_OPTION_COPROCESSOR
);
1805 gen_window_check2(dc
, RRR_S
, RRR_T
);
1808 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER
);
1809 gen_check_privilege(dc
);
1811 TCGv_i32 addr
= tcg_temp_new_i32();
1812 tcg_gen_addi_i32(addr
, cpu_R
[RRR_S
],
1813 (0xffffffc0 | (RRR_R
<< 2)));
1814 tcg_gen_qemu_ld32u(cpu_R
[RRR_T
], addr
, dc
->ring
);
1815 tcg_temp_free(addr
);
1820 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER
);
1821 gen_check_privilege(dc
);
1823 TCGv_i32 addr
= tcg_temp_new_i32();
1824 tcg_gen_addi_i32(addr
, cpu_R
[RRR_S
],
1825 (0xffffffc0 | (RRR_R
<< 2)));
1826 tcg_gen_qemu_st32(cpu_R
[RRR_T
], addr
, dc
->ring
);
1827 tcg_temp_free(addr
);
1838 HAS_OPTION(XTENSA_OPTION_FP_COPROCESSOR
);
1843 HAS_OPTION(XTENSA_OPTION_FP_COPROCESSOR
);
1847 default: /*reserved*/
1854 gen_window_check1(dc
, RRR_T
);
1856 TCGv_i32 tmp
= tcg_const_i32(
1857 ((dc
->tb
->flags
& XTENSA_TBFLAG_LITBASE
) ?
1858 0 : ((dc
->pc
+ 3) & ~3)) +
1859 (0xfffc0000 | (RI16_IMM16
<< 2)));
1861 if (dc
->tb
->flags
& XTENSA_TBFLAG_LITBASE
) {
1862 tcg_gen_add_i32(tmp
, tmp
, dc
->litbase
);
1864 tcg_gen_qemu_ld32u(cpu_R
[RRR_T
], tmp
, dc
->cring
);
1870 #define gen_load_store(type, shift) do { \
1871 TCGv_i32 addr = tcg_temp_new_i32(); \
1872 gen_window_check2(dc, RRI8_S, RRI8_T); \
1873 tcg_gen_addi_i32(addr, cpu_R[RRI8_S], RRI8_IMM8 << shift); \
1875 gen_load_store_alignment(dc, shift, addr, false); \
1877 tcg_gen_qemu_##type(cpu_R[RRI8_T], addr, dc->cring); \
1878 tcg_temp_free(addr); \
1883 gen_load_store(ld8u
, 0);
1887 gen_load_store(ld16u
, 1);
1891 gen_load_store(ld32u
, 2);
1895 gen_load_store(st8
, 0);
1899 gen_load_store(st16
, 1);
1903 gen_load_store(st32
, 2);
1908 HAS_OPTION(XTENSA_OPTION_DCACHE
);
1939 HAS_OPTION(XTENSA_OPTION_DCACHE_INDEX_LOCK
);
1943 HAS_OPTION(XTENSA_OPTION_DCACHE_INDEX_LOCK
);
1947 HAS_OPTION(XTENSA_OPTION_DCACHE_INDEX_LOCK
);
1951 HAS_OPTION(XTENSA_OPTION_DCACHE
);
1955 HAS_OPTION(XTENSA_OPTION_DCACHE
);
1958 default: /*reserved*/
1966 HAS_OPTION(XTENSA_OPTION_ICACHE
);
1972 HAS_OPTION(XTENSA_OPTION_ICACHE_INDEX_LOCK
);
1976 HAS_OPTION(XTENSA_OPTION_ICACHE_INDEX_LOCK
);
1980 HAS_OPTION(XTENSA_OPTION_ICACHE_INDEX_LOCK
);
1983 default: /*reserved*/
1990 HAS_OPTION(XTENSA_OPTION_ICACHE
);
1994 HAS_OPTION(XTENSA_OPTION_ICACHE
);
1997 default: /*reserved*/
2004 gen_load_store(ld16s
, 1);
2006 #undef gen_load_store
2009 gen_window_check1(dc
, RRI8_T
);
2010 tcg_gen_movi_i32(cpu_R
[RRI8_T
],
2011 RRI8_IMM8
| (RRI8_S
<< 8) |
2012 ((RRI8_S
& 0x8) ? 0xfffff000 : 0));
2015 #define gen_load_store_no_hw_align(type) do { \
2016 TCGv_i32 addr = tcg_temp_local_new_i32(); \
2017 gen_window_check2(dc, RRI8_S, RRI8_T); \
2018 tcg_gen_addi_i32(addr, cpu_R[RRI8_S], RRI8_IMM8 << 2); \
2019 gen_load_store_alignment(dc, 2, addr, true); \
2020 tcg_gen_qemu_##type(cpu_R[RRI8_T], addr, dc->cring); \
2021 tcg_temp_free(addr); \
2025 HAS_OPTION(XTENSA_OPTION_MP_SYNCHRO
);
2026 gen_load_store_no_hw_align(ld32u
); /*TODO acquire?*/
2030 gen_window_check2(dc
, RRI8_S
, RRI8_T
);
2031 tcg_gen_addi_i32(cpu_R
[RRI8_T
], cpu_R
[RRI8_S
], RRI8_IMM8_SE
);
2035 gen_window_check2(dc
, RRI8_S
, RRI8_T
);
2036 tcg_gen_addi_i32(cpu_R
[RRI8_T
], cpu_R
[RRI8_S
], RRI8_IMM8_SE
<< 8);
2039 case 14: /*S32C1Iy*/
2040 HAS_OPTION(XTENSA_OPTION_CONDITIONAL_STORE
);
2041 gen_window_check2(dc
, RRI8_S
, RRI8_T
);
2043 int label
= gen_new_label();
2044 TCGv_i32 tmp
= tcg_temp_local_new_i32();
2045 TCGv_i32 addr
= tcg_temp_local_new_i32();
2047 tcg_gen_mov_i32(tmp
, cpu_R
[RRI8_T
]);
2048 tcg_gen_addi_i32(addr
, cpu_R
[RRI8_S
], RRI8_IMM8
<< 2);
2049 gen_load_store_alignment(dc
, 2, addr
, true);
2050 tcg_gen_qemu_ld32u(cpu_R
[RRI8_T
], addr
, dc
->cring
);
2051 tcg_gen_brcond_i32(TCG_COND_NE
, cpu_R
[RRI8_T
],
2052 cpu_SR
[SCOMPARE1
], label
);
2054 tcg_gen_qemu_st32(tmp
, addr
, dc
->cring
);
2056 gen_set_label(label
);
2057 tcg_temp_free(addr
);
2063 HAS_OPTION(XTENSA_OPTION_MP_SYNCHRO
);
2064 gen_load_store_no_hw_align(st32
); /*TODO release?*/
2066 #undef gen_load_store_no_hw_align
2068 default: /*reserved*/
2075 HAS_OPTION(XTENSA_OPTION_COPROCESSOR
);
2080 HAS_OPTION(XTENSA_OPTION_MAC16
);
2089 bool is_m1_sr
= (OP2
& 0x3) == 2;
2090 bool is_m2_sr
= (OP2
& 0xc) == 0;
2091 uint32_t ld_offset
= 0;
2098 case 0: /*MACI?/MACC?*/
2100 ld_offset
= (OP2
& 1) ? -4 : 4;
2102 if (OP2
>= 8) { /*MACI/MACC*/
2103 if (OP1
== 0) { /*LDINC/LDDEC*/
2108 } else if (op
!= MAC16_MULA
) { /*MULA.*.*.LDINC/LDDEC*/
2113 case 2: /*MACD?/MACA?*/
2114 if (op
== MAC16_UMUL
&& OP2
!= 7) { /*UMUL only in MACAA*/
2120 if (op
!= MAC16_NONE
) {
2122 gen_window_check1(dc
, RRR_S
);
2125 gen_window_check1(dc
, RRR_T
);
2130 TCGv_i32 vaddr
= tcg_temp_new_i32();
2131 TCGv_i32 mem32
= tcg_temp_new_i32();
2134 gen_window_check1(dc
, RRR_S
);
2135 tcg_gen_addi_i32(vaddr
, cpu_R
[RRR_S
], ld_offset
);
2136 gen_load_store_alignment(dc
, 2, vaddr
, false);
2137 tcg_gen_qemu_ld32u(mem32
, vaddr
, dc
->cring
);
2139 if (op
!= MAC16_NONE
) {
2140 TCGv_i32 m1
= gen_mac16_m(
2141 is_m1_sr
? cpu_SR
[MR
+ RRR_X
] : cpu_R
[RRR_S
],
2142 OP1
& 1, op
== MAC16_UMUL
);
2143 TCGv_i32 m2
= gen_mac16_m(
2144 is_m2_sr
? cpu_SR
[MR
+ 2 + RRR_Y
] : cpu_R
[RRR_T
],
2145 OP1
& 2, op
== MAC16_UMUL
);
2147 if (op
== MAC16_MUL
|| op
== MAC16_UMUL
) {
2148 tcg_gen_mul_i32(cpu_SR
[ACCLO
], m1
, m2
);
2149 if (op
== MAC16_UMUL
) {
2150 tcg_gen_movi_i32(cpu_SR
[ACCHI
], 0);
2152 tcg_gen_sari_i32(cpu_SR
[ACCHI
], cpu_SR
[ACCLO
], 31);
2155 TCGv_i32 res
= tcg_temp_new_i32();
2156 TCGv_i64 res64
= tcg_temp_new_i64();
2157 TCGv_i64 tmp
= tcg_temp_new_i64();
2159 tcg_gen_mul_i32(res
, m1
, m2
);
2160 tcg_gen_ext_i32_i64(res64
, res
);
2161 tcg_gen_concat_i32_i64(tmp
,
2162 cpu_SR
[ACCLO
], cpu_SR
[ACCHI
]);
2163 if (op
== MAC16_MULA
) {
2164 tcg_gen_add_i64(tmp
, tmp
, res64
);
2166 tcg_gen_sub_i64(tmp
, tmp
, res64
);
2168 tcg_gen_trunc_i64_i32(cpu_SR
[ACCLO
], tmp
);
2169 tcg_gen_shri_i64(tmp
, tmp
, 32);
2170 tcg_gen_trunc_i64_i32(cpu_SR
[ACCHI
], tmp
);
2171 tcg_gen_ext8s_i32(cpu_SR
[ACCHI
], cpu_SR
[ACCHI
]);
2174 tcg_temp_free_i64(res64
);
2175 tcg_temp_free_i64(tmp
);
2181 tcg_gen_mov_i32(cpu_R
[RRR_S
], vaddr
);
2182 tcg_gen_mov_i32(cpu_SR
[MR
+ RRR_W
], mem32
);
2184 tcg_temp_free(vaddr
);
2185 tcg_temp_free(mem32
);
2193 tcg_gen_movi_i32(cpu_R
[0], dc
->next_pc
);
2194 gen_jumpi(dc
, (dc
->pc
& ~3) + (CALL_OFFSET_SE
<< 2) + 4, 0);
2200 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER
);
2201 gen_window_check1(dc
, CALL_N
<< 2);
2202 gen_callwi(dc
, CALL_N
,
2203 (dc
->pc
& ~3) + (CALL_OFFSET_SE
<< 2) + 4, 0);
2211 gen_jumpi(dc
, dc
->pc
+ 4 + CALL_OFFSET_SE
, 0);
2215 gen_window_check1(dc
, BRI12_S
);
2217 static const TCGCond cond
[] = {
2218 TCG_COND_EQ
, /*BEQZ*/
2219 TCG_COND_NE
, /*BNEZ*/
2220 TCG_COND_LT
, /*BLTZ*/
2221 TCG_COND_GE
, /*BGEZ*/
2224 gen_brcondi(dc
, cond
[BRI12_M
& 3], cpu_R
[BRI12_S
], 0,
2225 4 + BRI12_IMM12_SE
);
2230 gen_window_check1(dc
, BRI8_S
);
2232 static const TCGCond cond
[] = {
2233 TCG_COND_EQ
, /*BEQI*/
2234 TCG_COND_NE
, /*BNEI*/
2235 TCG_COND_LT
, /*BLTI*/
2236 TCG_COND_GE
, /*BGEI*/
2239 gen_brcondi(dc
, cond
[BRI8_M
& 3],
2240 cpu_R
[BRI8_S
], B4CONST
[BRI8_R
], 4 + BRI8_IMM8_SE
);
2247 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER
);
2249 TCGv_i32 pc
= tcg_const_i32(dc
->pc
);
2250 TCGv_i32 s
= tcg_const_i32(BRI12_S
);
2251 TCGv_i32 imm
= tcg_const_i32(BRI12_IMM12
);
2252 gen_advance_ccount(dc
);
2253 gen_helper_entry(cpu_env
, pc
, s
, imm
);
2257 reset_used_window(dc
);
2265 HAS_OPTION(XTENSA_OPTION_BOOLEAN
);
2267 TCGv_i32 tmp
= tcg_temp_new_i32();
2268 tcg_gen_andi_i32(tmp
, cpu_SR
[BR
], 1 << RRI8_S
);
2270 BRI8_R
== 1 ? TCG_COND_NE
: TCG_COND_EQ
,
2271 tmp
, 0, 4 + RRI8_IMM8_SE
);
2278 case 10: /*LOOPGTZ*/
2279 HAS_OPTION(XTENSA_OPTION_LOOP
);
2280 gen_window_check1(dc
, RRI8_S
);
2282 uint32_t lend
= dc
->pc
+ RRI8_IMM8
+ 4;
2283 TCGv_i32 tmp
= tcg_const_i32(lend
);
2285 tcg_gen_subi_i32(cpu_SR
[LCOUNT
], cpu_R
[RRI8_S
], 1);
2286 tcg_gen_movi_i32(cpu_SR
[LBEG
], dc
->next_pc
);
2287 gen_helper_wsr_lend(cpu_env
, tmp
);
2291 int label
= gen_new_label();
2292 tcg_gen_brcondi_i32(
2293 BRI8_R
== 9 ? TCG_COND_NE
: TCG_COND_GT
,
2294 cpu_R
[RRI8_S
], 0, label
);
2295 gen_jumpi(dc
, lend
, 1);
2296 gen_set_label(label
);
2299 gen_jumpi(dc
, dc
->next_pc
, 0);
2303 default: /*reserved*/
2312 gen_window_check1(dc
, BRI8_S
);
2313 gen_brcondi(dc
, BRI8_M
== 2 ? TCG_COND_LTU
: TCG_COND_GEU
,
2314 cpu_R
[BRI8_S
], B4CONSTU
[BRI8_R
], 4 + BRI8_IMM8_SE
);
2324 TCGCond eq_ne
= (RRI8_R
& 8) ? TCG_COND_NE
: TCG_COND_EQ
;
2326 switch (RRI8_R
& 7) {
2327 case 0: /*BNONE*/ /*BANY*/
2328 gen_window_check2(dc
, RRI8_S
, RRI8_T
);
2330 TCGv_i32 tmp
= tcg_temp_new_i32();
2331 tcg_gen_and_i32(tmp
, cpu_R
[RRI8_S
], cpu_R
[RRI8_T
]);
2332 gen_brcondi(dc
, eq_ne
, tmp
, 0, 4 + RRI8_IMM8_SE
);
2337 case 1: /*BEQ*/ /*BNE*/
2338 case 2: /*BLT*/ /*BGE*/
2339 case 3: /*BLTU*/ /*BGEU*/
2340 gen_window_check2(dc
, RRI8_S
, RRI8_T
);
2342 static const TCGCond cond
[] = {
2348 [11] = TCG_COND_GEU
,
2350 gen_brcond(dc
, cond
[RRI8_R
], cpu_R
[RRI8_S
], cpu_R
[RRI8_T
],
2355 case 4: /*BALL*/ /*BNALL*/
2356 gen_window_check2(dc
, RRI8_S
, RRI8_T
);
2358 TCGv_i32 tmp
= tcg_temp_new_i32();
2359 tcg_gen_and_i32(tmp
, cpu_R
[RRI8_S
], cpu_R
[RRI8_T
]);
2360 gen_brcond(dc
, eq_ne
, tmp
, cpu_R
[RRI8_T
],
2366 case 5: /*BBC*/ /*BBS*/
2367 gen_window_check2(dc
, RRI8_S
, RRI8_T
);
2369 TCGv_i32 bit
= tcg_const_i32(1);
2370 TCGv_i32 tmp
= tcg_temp_new_i32();
2371 tcg_gen_andi_i32(tmp
, cpu_R
[RRI8_T
], 0x1f);
2372 tcg_gen_shl_i32(bit
, bit
, tmp
);
2373 tcg_gen_and_i32(tmp
, cpu_R
[RRI8_S
], bit
);
2374 gen_brcondi(dc
, eq_ne
, tmp
, 0, 4 + RRI8_IMM8_SE
);
2380 case 6: /*BBCI*/ /*BBSI*/
2382 gen_window_check1(dc
, RRI8_S
);
2384 TCGv_i32 tmp
= tcg_temp_new_i32();
2385 tcg_gen_andi_i32(tmp
, cpu_R
[RRI8_S
],
2386 1 << (((RRI8_R
& 1) << 4) | RRI8_T
));
2387 gen_brcondi(dc
, eq_ne
, tmp
, 0, 4 + RRI8_IMM8_SE
);
2396 #define gen_narrow_load_store(type) do { \
2397 TCGv_i32 addr = tcg_temp_new_i32(); \
2398 gen_window_check2(dc, RRRN_S, RRRN_T); \
2399 tcg_gen_addi_i32(addr, cpu_R[RRRN_S], RRRN_R << 2); \
2400 gen_load_store_alignment(dc, 2, addr, false); \
2401 tcg_gen_qemu_##type(cpu_R[RRRN_T], addr, dc->cring); \
2402 tcg_temp_free(addr); \
2406 gen_narrow_load_store(ld32u
);
2410 gen_narrow_load_store(st32
);
2412 #undef gen_narrow_load_store
2415 gen_window_check3(dc
, RRRN_R
, RRRN_S
, RRRN_T
);
2416 tcg_gen_add_i32(cpu_R
[RRRN_R
], cpu_R
[RRRN_S
], cpu_R
[RRRN_T
]);
2419 case 11: /*ADDI.Nn*/
2420 gen_window_check2(dc
, RRRN_R
, RRRN_S
);
2421 tcg_gen_addi_i32(cpu_R
[RRRN_R
], cpu_R
[RRRN_S
], RRRN_T
? RRRN_T
: -1);
2425 gen_window_check1(dc
, RRRN_S
);
2426 if (RRRN_T
< 8) { /*MOVI.Nn*/
2427 tcg_gen_movi_i32(cpu_R
[RRRN_S
],
2428 RRRN_R
| (RRRN_T
<< 4) |
2429 ((RRRN_T
& 6) == 6 ? 0xffffff80 : 0));
2430 } else { /*BEQZ.Nn*/ /*BNEZ.Nn*/
2431 TCGCond eq_ne
= (RRRN_T
& 4) ? TCG_COND_NE
: TCG_COND_EQ
;
2433 gen_brcondi(dc
, eq_ne
, cpu_R
[RRRN_S
], 0,
2434 4 + (RRRN_R
| ((RRRN_T
& 3) << 4)));
2441 gen_window_check2(dc
, RRRN_S
, RRRN_T
);
2442 tcg_gen_mov_i32(cpu_R
[RRRN_T
], cpu_R
[RRRN_S
]);
2448 gen_jump(dc
, cpu_R
[0]);
2452 HAS_OPTION(XTENSA_OPTION_WINDOWED_REGISTER
);
2454 TCGv_i32 tmp
= tcg_const_i32(dc
->pc
);
2455 gen_advance_ccount(dc
);
2456 gen_helper_retw(tmp
, cpu_env
, tmp
);
2462 case 2: /*BREAK.Nn*/
2463 HAS_OPTION(XTENSA_OPTION_DEBUG
);
2465 gen_debug_exception(dc
, DEBUGCAUSE_BN
);
2473 gen_exception_cause(dc
, ILLEGAL_INSTRUCTION_CAUSE
);
2476 default: /*reserved*/
2482 default: /*reserved*/
2488 default: /*reserved*/
2493 gen_check_loop_end(dc
, 0);
2494 dc
->pc
= dc
->next_pc
;
2499 qemu_log("INVALID(pc = %08x)\n", dc
->pc
);
2500 gen_exception_cause(dc
, ILLEGAL_INSTRUCTION_CAUSE
);
2504 static void check_breakpoint(CPUXtensaState
*env
, DisasContext
*dc
)
2508 if (unlikely(!QTAILQ_EMPTY(&env
->breakpoints
))) {
2509 QTAILQ_FOREACH(bp
, &env
->breakpoints
, entry
) {
2510 if (bp
->pc
== dc
->pc
) {
2511 tcg_gen_movi_i32(cpu_pc
, dc
->pc
);
2512 gen_exception(dc
, EXCP_DEBUG
);
2513 dc
->is_jmp
= DISAS_UPDATE
;
2519 static void gen_ibreak_check(CPUXtensaState
*env
, DisasContext
*dc
)
2523 for (i
= 0; i
< dc
->config
->nibreak
; ++i
) {
2524 if ((env
->sregs
[IBREAKENABLE
] & (1 << i
)) &&
2525 env
->sregs
[IBREAKA
+ i
] == dc
->pc
) {
2526 gen_debug_exception(dc
, DEBUGCAUSE_IB
);
2532 static void gen_intermediate_code_internal(
2533 CPUXtensaState
*env
, TranslationBlock
*tb
, int search_pc
)
2538 uint16_t *gen_opc_end
= gen_opc_buf
+ OPC_MAX_SIZE
;
2539 int max_insns
= tb
->cflags
& CF_COUNT_MASK
;
2540 uint32_t pc_start
= tb
->pc
;
2541 uint32_t next_page_start
=
2542 (pc_start
& TARGET_PAGE_MASK
) + TARGET_PAGE_SIZE
;
2544 if (max_insns
== 0) {
2545 max_insns
= CF_COUNT_MASK
;
2548 dc
.config
= env
->config
;
2549 dc
.singlestep_enabled
= env
->singlestep_enabled
;
2552 dc
.ring
= tb
->flags
& XTENSA_TBFLAG_RING_MASK
;
2553 dc
.cring
= (tb
->flags
& XTENSA_TBFLAG_EXCM
) ? 0 : dc
.ring
;
2554 dc
.lbeg
= env
->sregs
[LBEG
];
2555 dc
.lend
= env
->sregs
[LEND
];
2556 dc
.is_jmp
= DISAS_NEXT
;
2557 dc
.ccount_delta
= 0;
2558 dc
.debug
= tb
->flags
& XTENSA_TBFLAG_DEBUG
;
2559 dc
.icount
= tb
->flags
& XTENSA_TBFLAG_ICOUNT
;
2562 init_sar_tracker(&dc
);
2563 reset_used_window(&dc
);
2565 dc
.next_icount
= tcg_temp_local_new_i32();
2570 if (env
->singlestep_enabled
&& env
->exception_taken
) {
2571 env
->exception_taken
= 0;
2572 tcg_gen_movi_i32(cpu_pc
, dc
.pc
);
2573 gen_exception(&dc
, EXCP_DEBUG
);
2577 check_breakpoint(env
, &dc
);
2580 j
= gen_opc_ptr
- gen_opc_buf
;
2584 gen_opc_instr_start
[lj
++] = 0;
2587 gen_opc_pc
[lj
] = dc
.pc
;
2588 gen_opc_instr_start
[lj
] = 1;
2589 gen_opc_icount
[lj
] = insn_count
;
2592 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP
))) {
2593 tcg_gen_debug_insn_start(dc
.pc
);
2598 if (insn_count
+ 1 == max_insns
&& (tb
->cflags
& CF_LAST_IO
)) {
2603 int label
= gen_new_label();
2605 tcg_gen_addi_i32(dc
.next_icount
, cpu_SR
[ICOUNT
], 1);
2606 tcg_gen_brcondi_i32(TCG_COND_NE
, dc
.next_icount
, 0, label
);
2607 tcg_gen_mov_i32(dc
.next_icount
, cpu_SR
[ICOUNT
]);
2609 gen_debug_exception(&dc
, DEBUGCAUSE_IC
);
2611 gen_set_label(label
);
2615 gen_ibreak_check(env
, &dc
);
2618 disas_xtensa_insn(&dc
);
2621 tcg_gen_mov_i32(cpu_SR
[ICOUNT
], dc
.next_icount
);
2623 if (env
->singlestep_enabled
) {
2624 tcg_gen_movi_i32(cpu_pc
, dc
.pc
);
2625 gen_exception(&dc
, EXCP_DEBUG
);
2628 } while (dc
.is_jmp
== DISAS_NEXT
&&
2629 insn_count
< max_insns
&&
2630 dc
.pc
< next_page_start
&&
2631 gen_opc_ptr
< gen_opc_end
);
2634 reset_sar_tracker(&dc
);
2636 tcg_temp_free(dc
.next_icount
);
2639 if (tb
->cflags
& CF_LAST_IO
) {
2643 if (dc
.is_jmp
== DISAS_NEXT
) {
2644 gen_jumpi(&dc
, dc
.pc
, 0);
2646 gen_icount_end(tb
, insn_count
);
2647 *gen_opc_ptr
= INDEX_op_end
;
2650 tb
->size
= dc
.pc
- pc_start
;
2651 tb
->icount
= insn_count
;
2655 void gen_intermediate_code(CPUXtensaState
*env
, TranslationBlock
*tb
)
2657 gen_intermediate_code_internal(env
, tb
, 0);
2660 void gen_intermediate_code_pc(CPUXtensaState
*env
, TranslationBlock
*tb
)
2662 gen_intermediate_code_internal(env
, tb
, 1);
2665 void cpu_dump_state(CPUXtensaState
*env
, FILE *f
, fprintf_function cpu_fprintf
,
2670 cpu_fprintf(f
, "PC=%08x\n\n", env
->pc
);
2672 for (i
= j
= 0; i
< 256; ++i
) {
2674 cpu_fprintf(f
, "%s=%08x%c", sregnames
[i
], env
->sregs
[i
],
2675 (j
++ % 4) == 3 ? '\n' : ' ');
2679 cpu_fprintf(f
, (j
% 4) == 0 ? "\n" : "\n\n");
2681 for (i
= j
= 0; i
< 256; ++i
) {
2683 cpu_fprintf(f
, "%s=%08x%c", uregnames
[i
], env
->uregs
[i
],
2684 (j
++ % 4) == 3 ? '\n' : ' ');
2688 cpu_fprintf(f
, (j
% 4) == 0 ? "\n" : "\n\n");
2690 for (i
= 0; i
< 16; ++i
) {
2691 cpu_fprintf(f
, "A%02d=%08x%c", i
, env
->regs
[i
],
2692 (i
% 4) == 3 ? '\n' : ' ');
2695 cpu_fprintf(f
, "\n");
2697 for (i
= 0; i
< env
->config
->nareg
; ++i
) {
2698 cpu_fprintf(f
, "AR%02d=%08x%c", i
, env
->phys_regs
[i
],
2699 (i
% 4) == 3 ? '\n' : ' ');
2703 void restore_state_to_opc(CPUXtensaState
*env
, TranslationBlock
*tb
, int pc_pos
)
2705 env
->pc
= gen_opc_pc
[pc_pos
];