2 * LatticeMico32 main translation routines.
4 * Copyright (c) 2010 Michael Walle <michael@walle.cc>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "disas/disas.h"
22 #include "exec/helper-proto.h"
25 #include "exec/cpu_ldst.h"
26 #include "hw/lm32/lm32_pic.h"
28 #include "exec/helper-gen.h"
30 #include "trace-tcg.h"
35 # define LOG_DIS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
37 # define LOG_DIS(...) do { } while (0)
40 #define EXTRACT_FIELD(src, start, end) \
41 (((src) >> start) & ((1 << (end - start + 1)) - 1))
45 static TCGv_ptr cpu_env;
46 static TCGv cpu_R[32];
56 static TCGv cpu_bp[4];
57 static TCGv cpu_wp[4];
59 #include "exec/gen-icount.h"
68 /* This is the state at translation time. */
69 typedef struct DisasContext {
76 uint8_t r0, r1, r2, csr;
81 unsigned int delayed_branch;
82 unsigned int tb_flags, synced_flags; /* tb dependent flags. */
85 struct TranslationBlock *tb;
86 int singlestep_enabled;
89 uint8_t num_breakpoints;
90 uint8_t num_watchpoints;
93 static const char *regnames[] = {
94 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
95 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
96 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
97 "r24", "r25", "r26/gp", "r27/fp", "r28/sp", "r29/ra",
98 "r30/ea", "r31/ba", "bp0", "bp1", "bp2", "bp3", "wp0",
102 static inline int zero_extend(unsigned int val, int width)
104 return val & ((1 << width) - 1);
107 static inline int sign_extend(unsigned int val, int width)
120 static inline void t_gen_raise_exception(DisasContext *dc, uint32_t index)
122 TCGv_i32 tmp = tcg_const_i32(index);
124 gen_helper_raise_exception(cpu_env, tmp);
125 tcg_temp_free_i32(tmp);
128 static inline void t_gen_illegal_insn(DisasContext *dc)
130 tcg_gen_movi_tl(cpu_pc, dc->pc);
131 gen_helper_ill(cpu_env);
134 static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
136 TranslationBlock *tb;
139 if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
140 likely(!dc->singlestep_enabled)) {
142 tcg_gen_movi_tl(cpu_pc, dest);
143 tcg_gen_exit_tb((uintptr_t)tb + n);
145 tcg_gen_movi_tl(cpu_pc, dest);
146 if (dc->singlestep_enabled) {
147 t_gen_raise_exception(dc, EXCP_DEBUG);
153 static void dec_add(DisasContext *dc)
155 if (dc->format == OP_FMT_RI) {
156 if (dc->r0 == R_R0) {
157 if (dc->r1 == R_R0 && dc->imm16 == 0) {
160 LOG_DIS("mvi r%d, %d\n", dc->r1, sign_extend(dc->imm16, 16));
163 LOG_DIS("addi r%d, r%d, %d\n", dc->r1, dc->r0,
164 sign_extend(dc->imm16, 16));
167 LOG_DIS("add r%d, r%d, r%d\n", dc->r2, dc->r0, dc->r1);
170 if (dc->format == OP_FMT_RI) {
171 tcg_gen_addi_tl(cpu_R[dc->r1], cpu_R[dc->r0],
172 sign_extend(dc->imm16, 16));
174 tcg_gen_add_tl(cpu_R[dc->r2], cpu_R[dc->r0], cpu_R[dc->r1]);
178 static void dec_and(DisasContext *dc)
180 if (dc->format == OP_FMT_RI) {
181 LOG_DIS("andi r%d, r%d, %d\n", dc->r1, dc->r0,
182 zero_extend(dc->imm16, 16));
184 LOG_DIS("and r%d, r%d, r%d\n", dc->r2, dc->r0, dc->r1);
187 if (dc->format == OP_FMT_RI) {
188 tcg_gen_andi_tl(cpu_R[dc->r1], cpu_R[dc->r0],
189 zero_extend(dc->imm16, 16));
191 if (dc->r0 == 0 && dc->r1 == 0 && dc->r2 == 0) {
192 tcg_gen_movi_tl(cpu_pc, dc->pc + 4);
193 gen_helper_hlt(cpu_env);
195 tcg_gen_and_tl(cpu_R[dc->r2], cpu_R[dc->r0], cpu_R[dc->r1]);
200 static void dec_andhi(DisasContext *dc)
202 LOG_DIS("andhi r%d, r%d, %d\n", dc->r2, dc->r0, dc->imm16);
204 tcg_gen_andi_tl(cpu_R[dc->r1], cpu_R[dc->r0], (dc->imm16 << 16));
207 static void dec_b(DisasContext *dc)
209 if (dc->r0 == R_RA) {
211 } else if (dc->r0 == R_EA) {
213 } else if (dc->r0 == R_BA) {
216 LOG_DIS("b r%d\n", dc->r0);
219 /* restore IE.IE in case of an eret */
220 if (dc->r0 == R_EA) {
221 TCGv t0 = tcg_temp_new();
222 TCGLabel *l1 = gen_new_label();
223 tcg_gen_andi_tl(t0, cpu_ie, IE_EIE);
224 tcg_gen_ori_tl(cpu_ie, cpu_ie, IE_IE);
225 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, IE_EIE, l1);
226 tcg_gen_andi_tl(cpu_ie, cpu_ie, ~IE_IE);
229 } else if (dc->r0 == R_BA) {
230 TCGv t0 = tcg_temp_new();
231 TCGLabel *l1 = gen_new_label();
232 tcg_gen_andi_tl(t0, cpu_ie, IE_BIE);
233 tcg_gen_ori_tl(cpu_ie, cpu_ie, IE_IE);
234 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, IE_BIE, l1);
235 tcg_gen_andi_tl(cpu_ie, cpu_ie, ~IE_IE);
239 tcg_gen_mov_tl(cpu_pc, cpu_R[dc->r0]);
241 dc->is_jmp = DISAS_JUMP;
244 static void dec_bi(DisasContext *dc)
246 LOG_DIS("bi %d\n", sign_extend(dc->imm26 << 2, 26));
248 gen_goto_tb(dc, 0, dc->pc + (sign_extend(dc->imm26 << 2, 26)));
250 dc->is_jmp = DISAS_TB_JUMP;
253 static inline void gen_cond_branch(DisasContext *dc, int cond)
255 TCGLabel *l1 = gen_new_label();
256 tcg_gen_brcond_tl(cond, cpu_R[dc->r0], cpu_R[dc->r1], l1);
257 gen_goto_tb(dc, 0, dc->pc + 4);
259 gen_goto_tb(dc, 1, dc->pc + (sign_extend(dc->imm16 << 2, 16)));
260 dc->is_jmp = DISAS_TB_JUMP;
263 static void dec_be(DisasContext *dc)
265 LOG_DIS("be r%d, r%d, %d\n", dc->r0, dc->r1,
266 sign_extend(dc->imm16, 16) * 4);
268 gen_cond_branch(dc, TCG_COND_EQ);
271 static void dec_bg(DisasContext *dc)
273 LOG_DIS("bg r%d, r%d, %d\n", dc->r0, dc->r1,
274 sign_extend(dc->imm16, 16 * 4));
276 gen_cond_branch(dc, TCG_COND_GT);
279 static void dec_bge(DisasContext *dc)
281 LOG_DIS("bge r%d, r%d, %d\n", dc->r0, dc->r1,
282 sign_extend(dc->imm16, 16) * 4);
284 gen_cond_branch(dc, TCG_COND_GE);
287 static void dec_bgeu(DisasContext *dc)
289 LOG_DIS("bgeu r%d, r%d, %d\n", dc->r0, dc->r1,
290 sign_extend(dc->imm16, 16) * 4);
292 gen_cond_branch(dc, TCG_COND_GEU);
295 static void dec_bgu(DisasContext *dc)
297 LOG_DIS("bgu r%d, r%d, %d\n", dc->r0, dc->r1,
298 sign_extend(dc->imm16, 16) * 4);
300 gen_cond_branch(dc, TCG_COND_GTU);
303 static void dec_bne(DisasContext *dc)
305 LOG_DIS("bne r%d, r%d, %d\n", dc->r0, dc->r1,
306 sign_extend(dc->imm16, 16) * 4);
308 gen_cond_branch(dc, TCG_COND_NE);
311 static void dec_call(DisasContext *dc)
313 LOG_DIS("call r%d\n", dc->r0);
315 tcg_gen_movi_tl(cpu_R[R_RA], dc->pc + 4);
316 tcg_gen_mov_tl(cpu_pc, cpu_R[dc->r0]);
318 dc->is_jmp = DISAS_JUMP;
321 static void dec_calli(DisasContext *dc)
323 LOG_DIS("calli %d\n", sign_extend(dc->imm26, 26) * 4);
325 tcg_gen_movi_tl(cpu_R[R_RA], dc->pc + 4);
326 gen_goto_tb(dc, 0, dc->pc + (sign_extend(dc->imm26 << 2, 26)));
328 dc->is_jmp = DISAS_TB_JUMP;
331 static inline void gen_compare(DisasContext *dc, int cond)
333 int rX = (dc->format == OP_FMT_RR) ? dc->r2 : dc->r1;
334 int rY = (dc->format == OP_FMT_RR) ? dc->r0 : dc->r0;
335 int rZ = (dc->format == OP_FMT_RR) ? dc->r1 : -1;
338 if (dc->format == OP_FMT_RI) {
342 i = zero_extend(dc->imm16, 16);
345 i = sign_extend(dc->imm16, 16);
349 tcg_gen_setcondi_tl(cond, cpu_R[rX], cpu_R[rY], i);
351 tcg_gen_setcond_tl(cond, cpu_R[rX], cpu_R[rY], cpu_R[rZ]);
355 static void dec_cmpe(DisasContext *dc)
357 if (dc->format == OP_FMT_RI) {
358 LOG_DIS("cmpei r%d, r%d, %d\n", dc->r0, dc->r1,
359 sign_extend(dc->imm16, 16));
361 LOG_DIS("cmpe r%d, r%d, r%d\n", dc->r2, dc->r0, dc->r1);
364 gen_compare(dc, TCG_COND_EQ);
367 static void dec_cmpg(DisasContext *dc)
369 if (dc->format == OP_FMT_RI) {
370 LOG_DIS("cmpgi r%d, r%d, %d\n", dc->r0, dc->r1,
371 sign_extend(dc->imm16, 16));
373 LOG_DIS("cmpg r%d, r%d, r%d\n", dc->r2, dc->r0, dc->r1);
376 gen_compare(dc, TCG_COND_GT);
379 static void dec_cmpge(DisasContext *dc)
381 if (dc->format == OP_FMT_RI) {
382 LOG_DIS("cmpgei r%d, r%d, %d\n", dc->r0, dc->r1,
383 sign_extend(dc->imm16, 16));
385 LOG_DIS("cmpge r%d, r%d, r%d\n", dc->r2, dc->r0, dc->r1);
388 gen_compare(dc, TCG_COND_GE);
391 static void dec_cmpgeu(DisasContext *dc)
393 if (dc->format == OP_FMT_RI) {
394 LOG_DIS("cmpgeui r%d, r%d, %d\n", dc->r0, dc->r1,
395 zero_extend(dc->imm16, 16));
397 LOG_DIS("cmpgeu r%d, r%d, r%d\n", dc->r2, dc->r0, dc->r1);
400 gen_compare(dc, TCG_COND_GEU);
403 static void dec_cmpgu(DisasContext *dc)
405 if (dc->format == OP_FMT_RI) {
406 LOG_DIS("cmpgui r%d, r%d, %d\n", dc->r0, dc->r1,
407 zero_extend(dc->imm16, 16));
409 LOG_DIS("cmpgu r%d, r%d, r%d\n", dc->r2, dc->r0, dc->r1);
412 gen_compare(dc, TCG_COND_GTU);
415 static void dec_cmpne(DisasContext *dc)
417 if (dc->format == OP_FMT_RI) {
418 LOG_DIS("cmpnei r%d, r%d, %d\n", dc->r0, dc->r1,
419 sign_extend(dc->imm16, 16));
421 LOG_DIS("cmpne r%d, r%d, r%d\n", dc->r2, dc->r0, dc->r1);
424 gen_compare(dc, TCG_COND_NE);
427 static void dec_divu(DisasContext *dc)
431 LOG_DIS("divu r%d, r%d, r%d\n", dc->r2, dc->r0, dc->r1);
433 if (!(dc->features & LM32_FEATURE_DIVIDE)) {
434 qemu_log_mask(LOG_GUEST_ERROR, "hardware divider is not available\n");
435 t_gen_illegal_insn(dc);
439 l1 = gen_new_label();
440 tcg_gen_brcondi_tl(TCG_COND_NE, cpu_R[dc->r1], 0, l1);
441 tcg_gen_movi_tl(cpu_pc, dc->pc);
442 t_gen_raise_exception(dc, EXCP_DIVIDE_BY_ZERO);
444 tcg_gen_divu_tl(cpu_R[dc->r2], cpu_R[dc->r0], cpu_R[dc->r1]);
447 static void dec_lb(DisasContext *dc)
451 LOG_DIS("lb r%d, (r%d+%d)\n", dc->r1, dc->r0, dc->imm16);
454 tcg_gen_addi_tl(t0, cpu_R[dc->r0], sign_extend(dc->imm16, 16));
455 tcg_gen_qemu_ld8s(cpu_R[dc->r1], t0, MEM_INDEX);
459 static void dec_lbu(DisasContext *dc)
463 LOG_DIS("lbu r%d, (r%d+%d)\n", dc->r1, dc->r0, dc->imm16);
466 tcg_gen_addi_tl(t0, cpu_R[dc->r0], sign_extend(dc->imm16, 16));
467 tcg_gen_qemu_ld8u(cpu_R[dc->r1], t0, MEM_INDEX);
471 static void dec_lh(DisasContext *dc)
475 LOG_DIS("lh r%d, (r%d+%d)\n", dc->r1, dc->r0, dc->imm16);
478 tcg_gen_addi_tl(t0, cpu_R[dc->r0], sign_extend(dc->imm16, 16));
479 tcg_gen_qemu_ld16s(cpu_R[dc->r1], t0, MEM_INDEX);
483 static void dec_lhu(DisasContext *dc)
487 LOG_DIS("lhu r%d, (r%d+%d)\n", dc->r1, dc->r0, dc->imm16);
490 tcg_gen_addi_tl(t0, cpu_R[dc->r0], sign_extend(dc->imm16, 16));
491 tcg_gen_qemu_ld16u(cpu_R[dc->r1], t0, MEM_INDEX);
495 static void dec_lw(DisasContext *dc)
499 LOG_DIS("lw r%d, (r%d+%d)\n", dc->r1, dc->r0, sign_extend(dc->imm16, 16));
502 tcg_gen_addi_tl(t0, cpu_R[dc->r0], sign_extend(dc->imm16, 16));
503 tcg_gen_qemu_ld32s(cpu_R[dc->r1], t0, MEM_INDEX);
507 static void dec_modu(DisasContext *dc)
511 LOG_DIS("modu r%d, r%d, %d\n", dc->r2, dc->r0, dc->r1);
513 if (!(dc->features & LM32_FEATURE_DIVIDE)) {
514 qemu_log_mask(LOG_GUEST_ERROR, "hardware divider is not available\n");
515 t_gen_illegal_insn(dc);
519 l1 = gen_new_label();
520 tcg_gen_brcondi_tl(TCG_COND_NE, cpu_R[dc->r1], 0, l1);
521 tcg_gen_movi_tl(cpu_pc, dc->pc);
522 t_gen_raise_exception(dc, EXCP_DIVIDE_BY_ZERO);
524 tcg_gen_remu_tl(cpu_R[dc->r2], cpu_R[dc->r0], cpu_R[dc->r1]);
527 static void dec_mul(DisasContext *dc)
529 if (dc->format == OP_FMT_RI) {
530 LOG_DIS("muli r%d, r%d, %d\n", dc->r0, dc->r1,
531 sign_extend(dc->imm16, 16));
533 LOG_DIS("mul r%d, r%d, r%d\n", dc->r2, dc->r0, dc->r1);
536 if (!(dc->features & LM32_FEATURE_MULTIPLY)) {
537 qemu_log_mask(LOG_GUEST_ERROR,
538 "hardware multiplier is not available\n");
539 t_gen_illegal_insn(dc);
543 if (dc->format == OP_FMT_RI) {
544 tcg_gen_muli_tl(cpu_R[dc->r1], cpu_R[dc->r0],
545 sign_extend(dc->imm16, 16));
547 tcg_gen_mul_tl(cpu_R[dc->r2], cpu_R[dc->r0], cpu_R[dc->r1]);
551 static void dec_nor(DisasContext *dc)
553 if (dc->format == OP_FMT_RI) {
554 LOG_DIS("nori r%d, r%d, %d\n", dc->r0, dc->r1,
555 zero_extend(dc->imm16, 16));
557 LOG_DIS("nor r%d, r%d, r%d\n", dc->r2, dc->r0, dc->r1);
560 if (dc->format == OP_FMT_RI) {
561 TCGv t0 = tcg_temp_new();
562 tcg_gen_movi_tl(t0, zero_extend(dc->imm16, 16));
563 tcg_gen_nor_tl(cpu_R[dc->r1], cpu_R[dc->r0], t0);
566 tcg_gen_nor_tl(cpu_R[dc->r2], cpu_R[dc->r0], cpu_R[dc->r1]);
570 static void dec_or(DisasContext *dc)
572 if (dc->format == OP_FMT_RI) {
573 LOG_DIS("ori r%d, r%d, %d\n", dc->r1, dc->r0,
574 zero_extend(dc->imm16, 16));
576 if (dc->r1 == R_R0) {
577 LOG_DIS("mv r%d, r%d\n", dc->r2, dc->r0);
579 LOG_DIS("or r%d, r%d, r%d\n", dc->r2, dc->r0, dc->r1);
583 if (dc->format == OP_FMT_RI) {
584 tcg_gen_ori_tl(cpu_R[dc->r1], cpu_R[dc->r0],
585 zero_extend(dc->imm16, 16));
587 tcg_gen_or_tl(cpu_R[dc->r2], cpu_R[dc->r0], cpu_R[dc->r1]);
591 static void dec_orhi(DisasContext *dc)
593 if (dc->r0 == R_R0) {
594 LOG_DIS("mvhi r%d, %d\n", dc->r1, dc->imm16);
596 LOG_DIS("orhi r%d, r%d, %d\n", dc->r1, dc->r0, dc->imm16);
599 tcg_gen_ori_tl(cpu_R[dc->r1], cpu_R[dc->r0], (dc->imm16 << 16));
602 static void dec_scall(DisasContext *dc)
607 tcg_gen_movi_tl(cpu_pc, dc->pc);
608 t_gen_raise_exception(dc, EXCP_BREAKPOINT);
612 tcg_gen_movi_tl(cpu_pc, dc->pc);
613 t_gen_raise_exception(dc, EXCP_SYSTEMCALL);
616 qemu_log_mask(LOG_GUEST_ERROR, "invalid opcode @0x%x", dc->pc);
617 t_gen_illegal_insn(dc);
622 static void dec_rcsr(DisasContext *dc)
624 LOG_DIS("rcsr r%d, %d\n", dc->r2, dc->csr);
628 tcg_gen_mov_tl(cpu_R[dc->r2], cpu_ie);
631 gen_helper_rcsr_im(cpu_R[dc->r2], cpu_env);
634 gen_helper_rcsr_ip(cpu_R[dc->r2], cpu_env);
637 tcg_gen_mov_tl(cpu_R[dc->r2], cpu_cc);
640 tcg_gen_mov_tl(cpu_R[dc->r2], cpu_cfg);
643 tcg_gen_mov_tl(cpu_R[dc->r2], cpu_eba);
646 tcg_gen_mov_tl(cpu_R[dc->r2], cpu_dc);
649 tcg_gen_mov_tl(cpu_R[dc->r2], cpu_deba);
652 gen_helper_rcsr_jtx(cpu_R[dc->r2], cpu_env);
655 gen_helper_rcsr_jrx(cpu_R[dc->r2], cpu_env);
667 qemu_log_mask(LOG_GUEST_ERROR, "invalid read access csr=%x\n", dc->csr);
670 qemu_log_mask(LOG_GUEST_ERROR, "read_csr: unknown csr=%x\n", dc->csr);
675 static void dec_sb(DisasContext *dc)
679 LOG_DIS("sb (r%d+%d), r%d\n", dc->r0, dc->imm16, dc->r1);
682 tcg_gen_addi_tl(t0, cpu_R[dc->r0], sign_extend(dc->imm16, 16));
683 tcg_gen_qemu_st8(cpu_R[dc->r1], t0, MEM_INDEX);
687 static void dec_sextb(DisasContext *dc)
689 LOG_DIS("sextb r%d, r%d\n", dc->r2, dc->r0);
691 if (!(dc->features & LM32_FEATURE_SIGN_EXTEND)) {
692 qemu_log_mask(LOG_GUEST_ERROR,
693 "hardware sign extender is not available\n");
694 t_gen_illegal_insn(dc);
698 tcg_gen_ext8s_tl(cpu_R[dc->r2], cpu_R[dc->r0]);
701 static void dec_sexth(DisasContext *dc)
703 LOG_DIS("sexth r%d, r%d\n", dc->r2, dc->r0);
705 if (!(dc->features & LM32_FEATURE_SIGN_EXTEND)) {
706 qemu_log_mask(LOG_GUEST_ERROR,
707 "hardware sign extender is not available\n");
708 t_gen_illegal_insn(dc);
712 tcg_gen_ext16s_tl(cpu_R[dc->r2], cpu_R[dc->r0]);
715 static void dec_sh(DisasContext *dc)
719 LOG_DIS("sh (r%d+%d), r%d\n", dc->r0, dc->imm16, dc->r1);
722 tcg_gen_addi_tl(t0, cpu_R[dc->r0], sign_extend(dc->imm16, 16));
723 tcg_gen_qemu_st16(cpu_R[dc->r1], t0, MEM_INDEX);
727 static void dec_sl(DisasContext *dc)
729 if (dc->format == OP_FMT_RI) {
730 LOG_DIS("sli r%d, r%d, %d\n", dc->r1, dc->r0, dc->imm5);
732 LOG_DIS("sl r%d, r%d, r%d\n", dc->r2, dc->r0, dc->r1);
735 if (!(dc->features & LM32_FEATURE_SHIFT)) {
736 qemu_log_mask(LOG_GUEST_ERROR, "hardware shifter is not available\n");
737 t_gen_illegal_insn(dc);
741 if (dc->format == OP_FMT_RI) {
742 tcg_gen_shli_tl(cpu_R[dc->r1], cpu_R[dc->r0], dc->imm5);
744 TCGv t0 = tcg_temp_new();
745 tcg_gen_andi_tl(t0, cpu_R[dc->r1], 0x1f);
746 tcg_gen_shl_tl(cpu_R[dc->r2], cpu_R[dc->r0], t0);
751 static void dec_sr(DisasContext *dc)
753 if (dc->format == OP_FMT_RI) {
754 LOG_DIS("sri r%d, r%d, %d\n", dc->r1, dc->r0, dc->imm5);
756 LOG_DIS("sr r%d, r%d, r%d\n", dc->r2, dc->r0, dc->r1);
759 /* The real CPU (w/o hardware shifter) only supports right shift by exactly
761 if (dc->format == OP_FMT_RI) {
762 if (!(dc->features & LM32_FEATURE_SHIFT) && (dc->imm5 != 1)) {
763 qemu_log_mask(LOG_GUEST_ERROR,
764 "hardware shifter is not available\n");
765 t_gen_illegal_insn(dc);
768 tcg_gen_sari_tl(cpu_R[dc->r1], cpu_R[dc->r0], dc->imm5);
770 TCGLabel *l1 = gen_new_label();
771 TCGLabel *l2 = gen_new_label();
772 TCGv t0 = tcg_temp_local_new();
773 tcg_gen_andi_tl(t0, cpu_R[dc->r1], 0x1f);
775 if (!(dc->features & LM32_FEATURE_SHIFT)) {
776 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 1, l1);
777 t_gen_illegal_insn(dc);
782 tcg_gen_sar_tl(cpu_R[dc->r2], cpu_R[dc->r0], t0);
789 static void dec_sru(DisasContext *dc)
791 if (dc->format == OP_FMT_RI) {
792 LOG_DIS("srui r%d, r%d, %d\n", dc->r1, dc->r0, dc->imm5);
794 LOG_DIS("sru r%d, r%d, r%d\n", dc->r2, dc->r0, dc->r1);
797 if (dc->format == OP_FMT_RI) {
798 if (!(dc->features & LM32_FEATURE_SHIFT) && (dc->imm5 != 1)) {
799 qemu_log_mask(LOG_GUEST_ERROR,
800 "hardware shifter is not available\n");
801 t_gen_illegal_insn(dc);
804 tcg_gen_shri_tl(cpu_R[dc->r1], cpu_R[dc->r0], dc->imm5);
806 TCGLabel *l1 = gen_new_label();
807 TCGLabel *l2 = gen_new_label();
808 TCGv t0 = tcg_temp_local_new();
809 tcg_gen_andi_tl(t0, cpu_R[dc->r1], 0x1f);
811 if (!(dc->features & LM32_FEATURE_SHIFT)) {
812 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 1, l1);
813 t_gen_illegal_insn(dc);
818 tcg_gen_shr_tl(cpu_R[dc->r2], cpu_R[dc->r0], t0);
825 static void dec_sub(DisasContext *dc)
827 LOG_DIS("sub r%d, r%d, r%d\n", dc->r2, dc->r0, dc->r1);
829 tcg_gen_sub_tl(cpu_R[dc->r2], cpu_R[dc->r0], cpu_R[dc->r1]);
832 static void dec_sw(DisasContext *dc)
836 LOG_DIS("sw (r%d+%d), r%d\n", dc->r0, sign_extend(dc->imm16, 16), dc->r1);
839 tcg_gen_addi_tl(t0, cpu_R[dc->r0], sign_extend(dc->imm16, 16));
840 tcg_gen_qemu_st32(cpu_R[dc->r1], t0, MEM_INDEX);
844 static void dec_user(DisasContext *dc)
848 qemu_log_mask(LOG_GUEST_ERROR, "user instruction undefined\n");
849 t_gen_illegal_insn(dc);
852 static void dec_wcsr(DisasContext *dc)
856 LOG_DIS("wcsr r%d, %d\n", dc->r1, dc->csr);
860 tcg_gen_mov_tl(cpu_ie, cpu_R[dc->r1]);
861 tcg_gen_movi_tl(cpu_pc, dc->pc + 4);
862 dc->is_jmp = DISAS_UPDATE;
865 /* mark as an io operation because it could cause an interrupt */
866 if (dc->tb->cflags & CF_USE_ICOUNT) {
869 gen_helper_wcsr_im(cpu_env, cpu_R[dc->r1]);
870 tcg_gen_movi_tl(cpu_pc, dc->pc + 4);
871 if (dc->tb->cflags & CF_USE_ICOUNT) {
874 dc->is_jmp = DISAS_UPDATE;
877 /* mark as an io operation because it could cause an interrupt */
878 if (dc->tb->cflags & CF_USE_ICOUNT) {
881 gen_helper_wcsr_ip(cpu_env, cpu_R[dc->r1]);
882 tcg_gen_movi_tl(cpu_pc, dc->pc + 4);
883 if (dc->tb->cflags & CF_USE_ICOUNT) {
886 dc->is_jmp = DISAS_UPDATE;
895 tcg_gen_mov_tl(cpu_eba, cpu_R[dc->r1]);
898 tcg_gen_mov_tl(cpu_deba, cpu_R[dc->r1]);
901 gen_helper_wcsr_jtx(cpu_env, cpu_R[dc->r1]);
904 gen_helper_wcsr_jrx(cpu_env, cpu_R[dc->r1]);
907 gen_helper_wcsr_dc(cpu_env, cpu_R[dc->r1]);
913 no = dc->csr - CSR_BP0;
914 if (dc->num_breakpoints <= no) {
915 qemu_log_mask(LOG_GUEST_ERROR,
916 "breakpoint #%i is not available\n", no);
917 t_gen_illegal_insn(dc);
920 gen_helper_wcsr_bp(cpu_env, cpu_R[dc->r1], tcg_const_i32(no));
926 no = dc->csr - CSR_WP0;
927 if (dc->num_watchpoints <= no) {
928 qemu_log_mask(LOG_GUEST_ERROR,
929 "watchpoint #%i is not available\n", no);
930 t_gen_illegal_insn(dc);
933 gen_helper_wcsr_wp(cpu_env, cpu_R[dc->r1], tcg_const_i32(no));
937 qemu_log_mask(LOG_GUEST_ERROR, "invalid write access csr=%x\n",
941 qemu_log_mask(LOG_GUEST_ERROR, "write_csr: unknown csr=%x\n",
947 static void dec_xnor(DisasContext *dc)
949 if (dc->format == OP_FMT_RI) {
950 LOG_DIS("xnori r%d, r%d, %d\n", dc->r0, dc->r1,
951 zero_extend(dc->imm16, 16));
953 if (dc->r1 == R_R0) {
954 LOG_DIS("not r%d, r%d\n", dc->r2, dc->r0);
956 LOG_DIS("xnor r%d, r%d, r%d\n", dc->r2, dc->r0, dc->r1);
960 if (dc->format == OP_FMT_RI) {
961 tcg_gen_xori_tl(cpu_R[dc->r1], cpu_R[dc->r0],
962 zero_extend(dc->imm16, 16));
963 tcg_gen_not_tl(cpu_R[dc->r1], cpu_R[dc->r1]);
965 tcg_gen_eqv_tl(cpu_R[dc->r2], cpu_R[dc->r0], cpu_R[dc->r1]);
969 static void dec_xor(DisasContext *dc)
971 if (dc->format == OP_FMT_RI) {
972 LOG_DIS("xori r%d, r%d, %d\n", dc->r0, dc->r1,
973 zero_extend(dc->imm16, 16));
975 LOG_DIS("xor r%d, r%d, r%d\n", dc->r2, dc->r0, dc->r1);
978 if (dc->format == OP_FMT_RI) {
979 tcg_gen_xori_tl(cpu_R[dc->r1], cpu_R[dc->r0],
980 zero_extend(dc->imm16, 16));
982 tcg_gen_xor_tl(cpu_R[dc->r2], cpu_R[dc->r0], cpu_R[dc->r1]);
986 static void dec_ill(DisasContext *dc)
988 qemu_log_mask(LOG_GUEST_ERROR, "invalid opcode 0x%02x\n", dc->opcode);
989 t_gen_illegal_insn(dc);
992 typedef void (*DecoderInfo)(DisasContext *dc);
993 static const DecoderInfo decinfo[] = {
994 dec_sru, dec_nor, dec_mul, dec_sh, dec_lb, dec_sr, dec_xor, dec_lh,
995 dec_and, dec_xnor, dec_lw, dec_lhu, dec_sb, dec_add, dec_or, dec_sl,
996 dec_lbu, dec_be, dec_bg, dec_bge, dec_bgeu, dec_bgu, dec_sw, dec_bne,
997 dec_andhi, dec_cmpe, dec_cmpg, dec_cmpge, dec_cmpgeu, dec_cmpgu, dec_orhi,
999 dec_sru, dec_nor, dec_mul, dec_divu, dec_rcsr, dec_sr, dec_xor, dec_ill,
1000 dec_and, dec_xnor, dec_ill, dec_scall, dec_sextb, dec_add, dec_or, dec_sl,
1001 dec_b, dec_modu, dec_sub, dec_user, dec_wcsr, dec_ill, dec_call, dec_sexth,
1002 dec_bi, dec_cmpe, dec_cmpg, dec_cmpge, dec_cmpgeu, dec_cmpgu, dec_calli,
1006 static inline void decode(DisasContext *dc, uint32_t ir)
1008 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
1009 tcg_gen_debug_insn_start(dc->pc);
1013 LOG_DIS("%8.8x\t", dc->ir);
1015 dc->opcode = EXTRACT_FIELD(ir, 26, 31);
1017 dc->imm5 = EXTRACT_FIELD(ir, 0, 4);
1018 dc->imm16 = EXTRACT_FIELD(ir, 0, 15);
1019 dc->imm26 = EXTRACT_FIELD(ir, 0, 25);
1021 dc->csr = EXTRACT_FIELD(ir, 21, 25);
1022 dc->r0 = EXTRACT_FIELD(ir, 21, 25);
1023 dc->r1 = EXTRACT_FIELD(ir, 16, 20);
1024 dc->r2 = EXTRACT_FIELD(ir, 11, 15);
1026 /* bit 31 seems to indicate insn type. */
1027 if (ir & (1 << 31)) {
1028 dc->format = OP_FMT_RR;
1030 dc->format = OP_FMT_RI;
1033 assert(ARRAY_SIZE(decinfo) == 64);
1034 assert(dc->opcode < 64);
1036 decinfo[dc->opcode](dc);
1039 static void check_breakpoint(CPULM32State *env, DisasContext *dc)
1041 CPUState *cs = CPU(lm32_env_get_cpu(env));
1044 if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) {
1045 QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
1046 if (bp->pc == dc->pc) {
1047 tcg_gen_movi_tl(cpu_pc, dc->pc);
1048 t_gen_raise_exception(dc, EXCP_DEBUG);
1049 dc->is_jmp = DISAS_UPDATE;
1055 /* generate intermediate code for basic block 'tb'. */
1057 void gen_intermediate_code_internal(LM32CPU *cpu,
1058 TranslationBlock *tb, bool search_pc)
1060 CPUState *cs = CPU(cpu);
1061 CPULM32State *env = &cpu->env;
1062 struct DisasContext ctx, *dc = &ctx;
1065 uint32_t next_page_start;
1070 dc->features = cpu->features;
1071 dc->num_breakpoints = cpu->num_breakpoints;
1072 dc->num_watchpoints = cpu->num_watchpoints;
1075 dc->is_jmp = DISAS_NEXT;
1077 dc->singlestep_enabled = cs->singlestep_enabled;
1080 qemu_log_mask(LOG_GUEST_ERROR,
1081 "unaligned PC=%x. Ignoring lowest bits.\n", pc_start);
1085 next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
1088 max_insns = tb->cflags & CF_COUNT_MASK;
1089 if (max_insns == 0) {
1090 max_insns = CF_COUNT_MASK;
1095 check_breakpoint(env, dc);
1098 j = tcg_op_buf_count();
1102 tcg_ctx.gen_opc_instr_start[lj++] = 0;
1105 tcg_ctx.gen_opc_pc[lj] = dc->pc;
1106 tcg_ctx.gen_opc_instr_start[lj] = 1;
1107 tcg_ctx.gen_opc_icount[lj] = num_insns;
1111 LOG_DIS("%8.8x:\t", dc->pc);
1113 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO)) {
1117 decode(dc, cpu_ldl_code(env, dc->pc));
1121 } while (!dc->is_jmp
1122 && !tcg_op_buf_full()
1123 && !cs->singlestep_enabled
1125 && (dc->pc < next_page_start)
1126 && num_insns < max_insns);
1128 if (tb->cflags & CF_LAST_IO) {
1132 if (unlikely(cs->singlestep_enabled)) {
1133 if (dc->is_jmp == DISAS_NEXT) {
1134 tcg_gen_movi_tl(cpu_pc, dc->pc);
1136 t_gen_raise_exception(dc, EXCP_DEBUG);
1138 switch (dc->is_jmp) {
1140 gen_goto_tb(dc, 1, dc->pc);
1145 /* indicate that the hash table must be used
1146 to find the next TB */
1150 /* nothing more to generate */
1155 gen_tb_end(tb, num_insns);
1158 j = tcg_op_buf_count();
1161 tcg_ctx.gen_opc_instr_start[lj++] = 0;
1164 tb->size = dc->pc - pc_start;
1165 tb->icount = num_insns;
1169 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
1171 log_target_disas(cs, pc_start, dc->pc - pc_start, 0);
1172 qemu_log("\nisize=%d osize=%d\n",
1173 dc->pc - pc_start, tcg_op_buf_count());
1178 void gen_intermediate_code(CPULM32State *env, struct TranslationBlock *tb)
1180 gen_intermediate_code_internal(lm32_env_get_cpu(env), tb, false);
1183 void gen_intermediate_code_pc(CPULM32State *env, struct TranslationBlock *tb)
1185 gen_intermediate_code_internal(lm32_env_get_cpu(env), tb, true);
1188 void lm32_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
1191 LM32CPU *cpu = LM32_CPU(cs);
1192 CPULM32State *env = &cpu->env;
1199 cpu_fprintf(f, "IN: PC=%x %s\n",
1200 env->pc, lookup_symbol(env->pc));
1202 cpu_fprintf(f, "ie=%8.8x (IE=%x EIE=%x BIE=%x) im=%8.8x ip=%8.8x\n",
1204 (env->ie & IE_IE) ? 1 : 0,
1205 (env->ie & IE_EIE) ? 1 : 0,
1206 (env->ie & IE_BIE) ? 1 : 0,
1207 lm32_pic_get_im(env->pic_state),
1208 lm32_pic_get_ip(env->pic_state));
1209 cpu_fprintf(f, "eba=%8.8x deba=%8.8x\n",
1213 for (i = 0; i < 32; i++) {
1214 cpu_fprintf(f, "r%2.2d=%8.8x ", i, env->regs[i]);
1215 if ((i + 1) % 4 == 0) {
1216 cpu_fprintf(f, "\n");
1219 cpu_fprintf(f, "\n\n");
1222 void restore_state_to_opc(CPULM32State *env, TranslationBlock *tb, int pc_pos)
1224 env->pc = tcg_ctx.gen_opc_pc[pc_pos];
1227 void lm32_translate_init(void)
1231 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
1233 for (i = 0; i < ARRAY_SIZE(cpu_R); i++) {
1234 cpu_R[i] = tcg_global_mem_new(TCG_AREG0,
1235 offsetof(CPULM32State, regs[i]),
1239 for (i = 0; i < ARRAY_SIZE(cpu_bp); i++) {
1240 cpu_bp[i] = tcg_global_mem_new(TCG_AREG0,
1241 offsetof(CPULM32State, bp[i]),
1245 for (i = 0; i < ARRAY_SIZE(cpu_wp); i++) {
1246 cpu_wp[i] = tcg_global_mem_new(TCG_AREG0,
1247 offsetof(CPULM32State, wp[i]),
1251 cpu_pc = tcg_global_mem_new(TCG_AREG0,
1252 offsetof(CPULM32State, pc),
1254 cpu_ie = tcg_global_mem_new(TCG_AREG0,
1255 offsetof(CPULM32State, ie),
1257 cpu_icc = tcg_global_mem_new(TCG_AREG0,
1258 offsetof(CPULM32State, icc),
1260 cpu_dcc = tcg_global_mem_new(TCG_AREG0,
1261 offsetof(CPULM32State, dcc),
1263 cpu_cc = tcg_global_mem_new(TCG_AREG0,
1264 offsetof(CPULM32State, cc),
1266 cpu_cfg = tcg_global_mem_new(TCG_AREG0,
1267 offsetof(CPULM32State, cfg),
1269 cpu_eba = tcg_global_mem_new(TCG_AREG0,
1270 offsetof(CPULM32State, eba),
1272 cpu_dc = tcg_global_mem_new(TCG_AREG0,
1273 offsetof(CPULM32State, dc),
1275 cpu_deba = tcg_global_mem_new(TCG_AREG0,
1276 offsetof(CPULM32State, deba),