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"
25 #include "hw/lm32/lm32_pic.h"
32 # define LOG_DIS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
34 # define LOG_DIS(...) do { } while (0)
37 #define EXTRACT_FIELD(src, start, end) \
38 (((src) >> start) & ((1 << (end - start + 1)) - 1))
42 static TCGv_ptr cpu_env
;
43 static TCGv cpu_R
[32];
53 static TCGv cpu_bp
[4];
54 static TCGv cpu_wp
[4];
56 #include "exec/gen-icount.h"
65 /* This is the state at translation time. */
66 typedef struct DisasContext
{
73 uint8_t r0
, r1
, r2
, csr
;
78 unsigned int delayed_branch
;
79 unsigned int tb_flags
, synced_flags
; /* tb dependent flags. */
82 struct TranslationBlock
*tb
;
83 int singlestep_enabled
;
86 uint8_t num_breakpoints
;
87 uint8_t num_watchpoints
;
90 static const char *regnames
[] = {
91 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
92 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
93 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
94 "r24", "r25", "r26/gp", "r27/fp", "r28/sp", "r29/ra",
95 "r30/ea", "r31/ba", "bp0", "bp1", "bp2", "bp3", "wp0",
99 static inline int zero_extend(unsigned int val
, int width
)
101 return val
& ((1 << width
) - 1);
104 static inline int sign_extend(unsigned int val
, int width
)
117 static inline void t_gen_raise_exception(DisasContext
*dc
, uint32_t index
)
119 TCGv_i32 tmp
= tcg_const_i32(index
);
121 gen_helper_raise_exception(cpu_env
, tmp
);
122 tcg_temp_free_i32(tmp
);
125 static inline void t_gen_illegal_insn(DisasContext
*dc
)
127 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
128 gen_helper_ill(cpu_env
);
131 static void gen_goto_tb(DisasContext
*dc
, int n
, target_ulong dest
)
133 TranslationBlock
*tb
;
136 if ((tb
->pc
& TARGET_PAGE_MASK
) == (dest
& TARGET_PAGE_MASK
) &&
137 likely(!dc
->singlestep_enabled
)) {
139 tcg_gen_movi_tl(cpu_pc
, dest
);
140 tcg_gen_exit_tb((uintptr_t)tb
+ n
);
142 tcg_gen_movi_tl(cpu_pc
, dest
);
143 if (dc
->singlestep_enabled
) {
144 t_gen_raise_exception(dc
, EXCP_DEBUG
);
150 static void dec_add(DisasContext
*dc
)
152 if (dc
->format
== OP_FMT_RI
) {
153 if (dc
->r0
== R_R0
) {
154 if (dc
->r1
== R_R0
&& dc
->imm16
== 0) {
157 LOG_DIS("mvi r%d, %d\n", dc
->r1
, sign_extend(dc
->imm16
, 16));
160 LOG_DIS("addi r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
161 sign_extend(dc
->imm16
, 16));
164 LOG_DIS("add r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
167 if (dc
->format
== OP_FMT_RI
) {
168 tcg_gen_addi_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
],
169 sign_extend(dc
->imm16
, 16));
171 tcg_gen_add_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
175 static void dec_and(DisasContext
*dc
)
177 if (dc
->format
== OP_FMT_RI
) {
178 LOG_DIS("andi r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
179 zero_extend(dc
->imm16
, 16));
181 LOG_DIS("and r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
184 if (dc
->format
== OP_FMT_RI
) {
185 tcg_gen_andi_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
],
186 zero_extend(dc
->imm16
, 16));
188 if (dc
->r0
== 0 && dc
->r1
== 0 && dc
->r2
== 0) {
189 tcg_gen_movi_tl(cpu_pc
, dc
->pc
+ 4);
190 gen_helper_hlt(cpu_env
);
192 tcg_gen_and_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
197 static void dec_andhi(DisasContext
*dc
)
199 LOG_DIS("andhi r%d, r%d, %d\n", dc
->r2
, dc
->r0
, dc
->imm16
);
201 tcg_gen_andi_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], (dc
->imm16
<< 16));
204 static void dec_b(DisasContext
*dc
)
206 if (dc
->r0
== R_RA
) {
208 } else if (dc
->r0
== R_EA
) {
210 } else if (dc
->r0
== R_BA
) {
213 LOG_DIS("b r%d\n", dc
->r0
);
216 /* restore IE.IE in case of an eret */
217 if (dc
->r0
== R_EA
) {
218 TCGv t0
= tcg_temp_new();
219 int l1
= gen_new_label();
220 tcg_gen_andi_tl(t0
, cpu_ie
, IE_EIE
);
221 tcg_gen_ori_tl(cpu_ie
, cpu_ie
, IE_IE
);
222 tcg_gen_brcondi_tl(TCG_COND_EQ
, t0
, IE_EIE
, l1
);
223 tcg_gen_andi_tl(cpu_ie
, cpu_ie
, ~IE_IE
);
226 } else if (dc
->r0
== R_BA
) {
227 TCGv t0
= tcg_temp_new();
228 int l1
= gen_new_label();
229 tcg_gen_andi_tl(t0
, cpu_ie
, IE_BIE
);
230 tcg_gen_ori_tl(cpu_ie
, cpu_ie
, IE_IE
);
231 tcg_gen_brcondi_tl(TCG_COND_EQ
, t0
, IE_BIE
, l1
);
232 tcg_gen_andi_tl(cpu_ie
, cpu_ie
, ~IE_IE
);
236 tcg_gen_mov_tl(cpu_pc
, cpu_R
[dc
->r0
]);
238 dc
->is_jmp
= DISAS_JUMP
;
241 static void dec_bi(DisasContext
*dc
)
243 LOG_DIS("bi %d\n", sign_extend(dc
->imm26
<< 2, 26));
245 gen_goto_tb(dc
, 0, dc
->pc
+ (sign_extend(dc
->imm26
<< 2, 26)));
247 dc
->is_jmp
= DISAS_TB_JUMP
;
250 static inline void gen_cond_branch(DisasContext
*dc
, int cond
)
254 l1
= gen_new_label();
255 tcg_gen_brcond_tl(cond
, cpu_R
[dc
->r0
], cpu_R
[dc
->r1
], l1
);
256 gen_goto_tb(dc
, 0, dc
->pc
+ 4);
258 gen_goto_tb(dc
, 1, dc
->pc
+ (sign_extend(dc
->imm16
<< 2, 16)));
259 dc
->is_jmp
= DISAS_TB_JUMP
;
262 static void dec_be(DisasContext
*dc
)
264 LOG_DIS("be r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
265 sign_extend(dc
->imm16
, 16) * 4);
267 gen_cond_branch(dc
, TCG_COND_EQ
);
270 static void dec_bg(DisasContext
*dc
)
272 LOG_DIS("bg r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
273 sign_extend(dc
->imm16
, 16 * 4));
275 gen_cond_branch(dc
, TCG_COND_GT
);
278 static void dec_bge(DisasContext
*dc
)
280 LOG_DIS("bge r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
281 sign_extend(dc
->imm16
, 16) * 4);
283 gen_cond_branch(dc
, TCG_COND_GE
);
286 static void dec_bgeu(DisasContext
*dc
)
288 LOG_DIS("bgeu r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
289 sign_extend(dc
->imm16
, 16) * 4);
291 gen_cond_branch(dc
, TCG_COND_GEU
);
294 static void dec_bgu(DisasContext
*dc
)
296 LOG_DIS("bgu r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
297 sign_extend(dc
->imm16
, 16) * 4);
299 gen_cond_branch(dc
, TCG_COND_GTU
);
302 static void dec_bne(DisasContext
*dc
)
304 LOG_DIS("bne r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
305 sign_extend(dc
->imm16
, 16) * 4);
307 gen_cond_branch(dc
, TCG_COND_NE
);
310 static void dec_call(DisasContext
*dc
)
312 LOG_DIS("call r%d\n", dc
->r0
);
314 tcg_gen_movi_tl(cpu_R
[R_RA
], dc
->pc
+ 4);
315 tcg_gen_mov_tl(cpu_pc
, cpu_R
[dc
->r0
]);
317 dc
->is_jmp
= DISAS_JUMP
;
320 static void dec_calli(DisasContext
*dc
)
322 LOG_DIS("calli %d\n", sign_extend(dc
->imm26
, 26) * 4);
324 tcg_gen_movi_tl(cpu_R
[R_RA
], dc
->pc
+ 4);
325 gen_goto_tb(dc
, 0, dc
->pc
+ (sign_extend(dc
->imm26
<< 2, 26)));
327 dc
->is_jmp
= DISAS_TB_JUMP
;
330 static inline void gen_compare(DisasContext
*dc
, int cond
)
332 int rX
= (dc
->format
== OP_FMT_RR
) ? dc
->r2
: dc
->r1
;
333 int rY
= (dc
->format
== OP_FMT_RR
) ? dc
->r0
: dc
->r0
;
334 int rZ
= (dc
->format
== OP_FMT_RR
) ? dc
->r1
: -1;
337 if (dc
->format
== OP_FMT_RI
) {
341 i
= zero_extend(dc
->imm16
, 16);
344 i
= sign_extend(dc
->imm16
, 16);
348 tcg_gen_setcondi_tl(cond
, cpu_R
[rX
], cpu_R
[rY
], i
);
350 tcg_gen_setcond_tl(cond
, cpu_R
[rX
], cpu_R
[rY
], cpu_R
[rZ
]);
354 static void dec_cmpe(DisasContext
*dc
)
356 if (dc
->format
== OP_FMT_RI
) {
357 LOG_DIS("cmpei r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
358 sign_extend(dc
->imm16
, 16));
360 LOG_DIS("cmpe r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
363 gen_compare(dc
, TCG_COND_EQ
);
366 static void dec_cmpg(DisasContext
*dc
)
368 if (dc
->format
== OP_FMT_RI
) {
369 LOG_DIS("cmpgi r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
370 sign_extend(dc
->imm16
, 16));
372 LOG_DIS("cmpg r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
375 gen_compare(dc
, TCG_COND_GT
);
378 static void dec_cmpge(DisasContext
*dc
)
380 if (dc
->format
== OP_FMT_RI
) {
381 LOG_DIS("cmpgei r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
382 sign_extend(dc
->imm16
, 16));
384 LOG_DIS("cmpge r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
387 gen_compare(dc
, TCG_COND_GE
);
390 static void dec_cmpgeu(DisasContext
*dc
)
392 if (dc
->format
== OP_FMT_RI
) {
393 LOG_DIS("cmpgeui r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
394 zero_extend(dc
->imm16
, 16));
396 LOG_DIS("cmpgeu r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
399 gen_compare(dc
, TCG_COND_GEU
);
402 static void dec_cmpgu(DisasContext
*dc
)
404 if (dc
->format
== OP_FMT_RI
) {
405 LOG_DIS("cmpgui r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
406 zero_extend(dc
->imm16
, 16));
408 LOG_DIS("cmpgu r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
411 gen_compare(dc
, TCG_COND_GTU
);
414 static void dec_cmpne(DisasContext
*dc
)
416 if (dc
->format
== OP_FMT_RI
) {
417 LOG_DIS("cmpnei r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
418 sign_extend(dc
->imm16
, 16));
420 LOG_DIS("cmpne r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
423 gen_compare(dc
, TCG_COND_NE
);
426 static void dec_divu(DisasContext
*dc
)
430 LOG_DIS("divu r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
432 if (!(dc
->features
& LM32_FEATURE_DIVIDE
)) {
433 qemu_log_mask(LOG_GUEST_ERROR
, "hardware divider is not available\n");
434 t_gen_illegal_insn(dc
);
438 l1
= gen_new_label();
439 tcg_gen_brcondi_tl(TCG_COND_NE
, cpu_R
[dc
->r1
], 0, l1
);
440 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
441 t_gen_raise_exception(dc
, EXCP_DIVIDE_BY_ZERO
);
443 tcg_gen_divu_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
446 static void dec_lb(DisasContext
*dc
)
450 LOG_DIS("lb r%d, (r%d+%d)\n", dc
->r1
, dc
->r0
, dc
->imm16
);
453 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
454 tcg_gen_qemu_ld8s(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
458 static void dec_lbu(DisasContext
*dc
)
462 LOG_DIS("lbu r%d, (r%d+%d)\n", dc
->r1
, dc
->r0
, dc
->imm16
);
465 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
466 tcg_gen_qemu_ld8u(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
470 static void dec_lh(DisasContext
*dc
)
474 LOG_DIS("lh r%d, (r%d+%d)\n", dc
->r1
, dc
->r0
, dc
->imm16
);
477 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
478 tcg_gen_qemu_ld16s(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
482 static void dec_lhu(DisasContext
*dc
)
486 LOG_DIS("lhu r%d, (r%d+%d)\n", dc
->r1
, dc
->r0
, dc
->imm16
);
489 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
490 tcg_gen_qemu_ld16u(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
494 static void dec_lw(DisasContext
*dc
)
498 LOG_DIS("lw r%d, (r%d+%d)\n", dc
->r1
, dc
->r0
, sign_extend(dc
->imm16
, 16));
501 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
502 tcg_gen_qemu_ld32s(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
506 static void dec_modu(DisasContext
*dc
)
510 LOG_DIS("modu r%d, r%d, %d\n", dc
->r2
, dc
->r0
, dc
->r1
);
512 if (!(dc
->features
& LM32_FEATURE_DIVIDE
)) {
513 qemu_log_mask(LOG_GUEST_ERROR
, "hardware divider is not available\n");
514 t_gen_illegal_insn(dc
);
518 l1
= gen_new_label();
519 tcg_gen_brcondi_tl(TCG_COND_NE
, cpu_R
[dc
->r1
], 0, l1
);
520 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
521 t_gen_raise_exception(dc
, EXCP_DIVIDE_BY_ZERO
);
523 tcg_gen_remu_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
526 static void dec_mul(DisasContext
*dc
)
528 if (dc
->format
== OP_FMT_RI
) {
529 LOG_DIS("muli r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
530 sign_extend(dc
->imm16
, 16));
532 LOG_DIS("mul r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
535 if (!(dc
->features
& LM32_FEATURE_MULTIPLY
)) {
536 qemu_log_mask(LOG_GUEST_ERROR
,
537 "hardware multiplier is not available\n");
538 t_gen_illegal_insn(dc
);
542 if (dc
->format
== OP_FMT_RI
) {
543 tcg_gen_muli_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
],
544 sign_extend(dc
->imm16
, 16));
546 tcg_gen_mul_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
550 static void dec_nor(DisasContext
*dc
)
552 if (dc
->format
== OP_FMT_RI
) {
553 LOG_DIS("nori r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
554 zero_extend(dc
->imm16
, 16));
556 LOG_DIS("nor r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
559 if (dc
->format
== OP_FMT_RI
) {
560 TCGv t0
= tcg_temp_new();
561 tcg_gen_movi_tl(t0
, zero_extend(dc
->imm16
, 16));
562 tcg_gen_nor_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], t0
);
565 tcg_gen_nor_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
569 static void dec_or(DisasContext
*dc
)
571 if (dc
->format
== OP_FMT_RI
) {
572 LOG_DIS("ori r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
573 zero_extend(dc
->imm16
, 16));
575 if (dc
->r1
== R_R0
) {
576 LOG_DIS("mv r%d, r%d\n", dc
->r2
, dc
->r0
);
578 LOG_DIS("or r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
582 if (dc
->format
== OP_FMT_RI
) {
583 tcg_gen_ori_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
],
584 zero_extend(dc
->imm16
, 16));
586 tcg_gen_or_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
590 static void dec_orhi(DisasContext
*dc
)
592 if (dc
->r0
== R_R0
) {
593 LOG_DIS("mvhi r%d, %d\n", dc
->r1
, dc
->imm16
);
595 LOG_DIS("orhi r%d, r%d, %d\n", dc
->r1
, dc
->r0
, dc
->imm16
);
598 tcg_gen_ori_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], (dc
->imm16
<< 16));
601 static void dec_scall(DisasContext
*dc
)
606 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
607 t_gen_raise_exception(dc
, EXCP_BREAKPOINT
);
611 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
612 t_gen_raise_exception(dc
, EXCP_SYSTEMCALL
);
615 qemu_log_mask(LOG_GUEST_ERROR
, "invalid opcode @0x%x", dc
->pc
);
616 t_gen_illegal_insn(dc
);
621 static void dec_rcsr(DisasContext
*dc
)
623 LOG_DIS("rcsr r%d, %d\n", dc
->r2
, dc
->csr
);
627 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_ie
);
630 gen_helper_rcsr_im(cpu_R
[dc
->r2
], cpu_env
);
633 gen_helper_rcsr_ip(cpu_R
[dc
->r2
], cpu_env
);
636 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_cc
);
639 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_cfg
);
642 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_eba
);
645 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_dc
);
648 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_deba
);
651 gen_helper_rcsr_jtx(cpu_R
[dc
->r2
], cpu_env
);
654 gen_helper_rcsr_jrx(cpu_R
[dc
->r2
], cpu_env
);
666 qemu_log_mask(LOG_GUEST_ERROR
, "invalid read access csr=%x\n", dc
->csr
);
669 qemu_log_mask(LOG_GUEST_ERROR
, "read_csr: unknown csr=%x\n", dc
->csr
);
674 static void dec_sb(DisasContext
*dc
)
678 LOG_DIS("sb (r%d+%d), r%d\n", dc
->r0
, dc
->imm16
, dc
->r1
);
681 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
682 tcg_gen_qemu_st8(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
686 static void dec_sextb(DisasContext
*dc
)
688 LOG_DIS("sextb r%d, r%d\n", dc
->r2
, dc
->r0
);
690 if (!(dc
->features
& LM32_FEATURE_SIGN_EXTEND
)) {
691 qemu_log_mask(LOG_GUEST_ERROR
,
692 "hardware sign extender is not available\n");
693 t_gen_illegal_insn(dc
);
697 tcg_gen_ext8s_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
]);
700 static void dec_sexth(DisasContext
*dc
)
702 LOG_DIS("sexth r%d, r%d\n", dc
->r2
, dc
->r0
);
704 if (!(dc
->features
& LM32_FEATURE_SIGN_EXTEND
)) {
705 qemu_log_mask(LOG_GUEST_ERROR
,
706 "hardware sign extender is not available\n");
707 t_gen_illegal_insn(dc
);
711 tcg_gen_ext16s_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
]);
714 static void dec_sh(DisasContext
*dc
)
718 LOG_DIS("sh (r%d+%d), r%d\n", dc
->r0
, dc
->imm16
, dc
->r1
);
721 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
722 tcg_gen_qemu_st16(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
726 static void dec_sl(DisasContext
*dc
)
728 if (dc
->format
== OP_FMT_RI
) {
729 LOG_DIS("sli r%d, r%d, %d\n", dc
->r1
, dc
->r0
, dc
->imm5
);
731 LOG_DIS("sl r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
734 if (!(dc
->features
& LM32_FEATURE_SHIFT
)) {
735 qemu_log_mask(LOG_GUEST_ERROR
, "hardware shifter is not available\n");
736 t_gen_illegal_insn(dc
);
740 if (dc
->format
== OP_FMT_RI
) {
741 tcg_gen_shli_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], dc
->imm5
);
743 TCGv t0
= tcg_temp_new();
744 tcg_gen_andi_tl(t0
, cpu_R
[dc
->r1
], 0x1f);
745 tcg_gen_shl_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], t0
);
750 static void dec_sr(DisasContext
*dc
)
752 if (dc
->format
== OP_FMT_RI
) {
753 LOG_DIS("sri r%d, r%d, %d\n", dc
->r1
, dc
->r0
, dc
->imm5
);
755 LOG_DIS("sr r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
758 /* The real CPU (w/o hardware shifter) only supports right shift by exactly
760 if (dc
->format
== OP_FMT_RI
) {
761 if (!(dc
->features
& LM32_FEATURE_SHIFT
) && (dc
->imm5
!= 1)) {
762 qemu_log_mask(LOG_GUEST_ERROR
,
763 "hardware shifter is not available\n");
764 t_gen_illegal_insn(dc
);
767 tcg_gen_sari_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], dc
->imm5
);
769 int l1
= gen_new_label();
770 int l2
= gen_new_label();
771 TCGv t0
= tcg_temp_local_new();
772 tcg_gen_andi_tl(t0
, cpu_R
[dc
->r1
], 0x1f);
774 if (!(dc
->features
& LM32_FEATURE_SHIFT
)) {
775 tcg_gen_brcondi_tl(TCG_COND_EQ
, t0
, 1, l1
);
776 t_gen_illegal_insn(dc
);
781 tcg_gen_sar_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], t0
);
788 static void dec_sru(DisasContext
*dc
)
790 if (dc
->format
== OP_FMT_RI
) {
791 LOG_DIS("srui r%d, r%d, %d\n", dc
->r1
, dc
->r0
, dc
->imm5
);
793 LOG_DIS("sru r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
796 if (dc
->format
== OP_FMT_RI
) {
797 if (!(dc
->features
& LM32_FEATURE_SHIFT
) && (dc
->imm5
!= 1)) {
798 qemu_log_mask(LOG_GUEST_ERROR
,
799 "hardware shifter is not available\n");
800 t_gen_illegal_insn(dc
);
803 tcg_gen_shri_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], dc
->imm5
);
805 int l1
= gen_new_label();
806 int l2
= gen_new_label();
807 TCGv t0
= tcg_temp_local_new();
808 tcg_gen_andi_tl(t0
, cpu_R
[dc
->r1
], 0x1f);
810 if (!(dc
->features
& LM32_FEATURE_SHIFT
)) {
811 tcg_gen_brcondi_tl(TCG_COND_EQ
, t0
, 1, l1
);
812 t_gen_illegal_insn(dc
);
817 tcg_gen_shr_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], t0
);
824 static void dec_sub(DisasContext
*dc
)
826 LOG_DIS("sub r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
828 tcg_gen_sub_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
831 static void dec_sw(DisasContext
*dc
)
835 LOG_DIS("sw (r%d+%d), r%d\n", dc
->r0
, sign_extend(dc
->imm16
, 16), dc
->r1
);
838 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
839 tcg_gen_qemu_st32(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
843 static void dec_user(DisasContext
*dc
)
847 qemu_log_mask(LOG_GUEST_ERROR
, "user instruction undefined\n");
848 t_gen_illegal_insn(dc
);
851 static void dec_wcsr(DisasContext
*dc
)
855 LOG_DIS("wcsr r%d, %d\n", dc
->r1
, dc
->csr
);
859 tcg_gen_mov_tl(cpu_ie
, cpu_R
[dc
->r1
]);
860 tcg_gen_movi_tl(cpu_pc
, dc
->pc
+ 4);
861 dc
->is_jmp
= DISAS_UPDATE
;
864 /* mark as an io operation because it could cause an interrupt */
868 gen_helper_wcsr_im(cpu_env
, cpu_R
[dc
->r1
]);
869 tcg_gen_movi_tl(cpu_pc
, dc
->pc
+ 4);
873 dc
->is_jmp
= DISAS_UPDATE
;
876 /* mark as an io operation because it could cause an interrupt */
880 gen_helper_wcsr_ip(cpu_env
, cpu_R
[dc
->r1
]);
881 tcg_gen_movi_tl(cpu_pc
, dc
->pc
+ 4);
885 dc
->is_jmp
= DISAS_UPDATE
;
894 tcg_gen_mov_tl(cpu_eba
, cpu_R
[dc
->r1
]);
897 tcg_gen_mov_tl(cpu_deba
, cpu_R
[dc
->r1
]);
900 gen_helper_wcsr_jtx(cpu_env
, cpu_R
[dc
->r1
]);
903 gen_helper_wcsr_jrx(cpu_env
, cpu_R
[dc
->r1
]);
906 gen_helper_wcsr_dc(cpu_env
, cpu_R
[dc
->r1
]);
912 no
= dc
->csr
- CSR_BP0
;
913 if (dc
->num_breakpoints
<= no
) {
914 qemu_log_mask(LOG_GUEST_ERROR
,
915 "breakpoint #%i is not available\n", no
);
916 t_gen_illegal_insn(dc
);
919 gen_helper_wcsr_bp(cpu_env
, cpu_R
[dc
->r1
], tcg_const_i32(no
));
925 no
= dc
->csr
- CSR_WP0
;
926 if (dc
->num_watchpoints
<= no
) {
927 qemu_log_mask(LOG_GUEST_ERROR
,
928 "watchpoint #%i is not available\n", no
);
929 t_gen_illegal_insn(dc
);
932 gen_helper_wcsr_wp(cpu_env
, cpu_R
[dc
->r1
], tcg_const_i32(no
));
936 qemu_log_mask(LOG_GUEST_ERROR
, "invalid write access csr=%x\n",
940 qemu_log_mask(LOG_GUEST_ERROR
, "write_csr: unknown csr=%x\n",
946 static void dec_xnor(DisasContext
*dc
)
948 if (dc
->format
== OP_FMT_RI
) {
949 LOG_DIS("xnori r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
950 zero_extend(dc
->imm16
, 16));
952 if (dc
->r1
== R_R0
) {
953 LOG_DIS("not r%d, r%d\n", dc
->r2
, dc
->r0
);
955 LOG_DIS("xnor r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
959 if (dc
->format
== OP_FMT_RI
) {
960 tcg_gen_xori_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
],
961 zero_extend(dc
->imm16
, 16));
962 tcg_gen_not_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r1
]);
964 tcg_gen_eqv_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
968 static void dec_xor(DisasContext
*dc
)
970 if (dc
->format
== OP_FMT_RI
) {
971 LOG_DIS("xori r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
972 zero_extend(dc
->imm16
, 16));
974 LOG_DIS("xor r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
977 if (dc
->format
== OP_FMT_RI
) {
978 tcg_gen_xori_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
],
979 zero_extend(dc
->imm16
, 16));
981 tcg_gen_xor_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
985 static void dec_ill(DisasContext
*dc
)
987 qemu_log_mask(LOG_GUEST_ERROR
, "invalid opcode 0x%02x\n", dc
->opcode
);
988 t_gen_illegal_insn(dc
);
991 typedef void (*DecoderInfo
)(DisasContext
*dc
);
992 static const DecoderInfo decinfo
[] = {
993 dec_sru
, dec_nor
, dec_mul
, dec_sh
, dec_lb
, dec_sr
, dec_xor
, dec_lh
,
994 dec_and
, dec_xnor
, dec_lw
, dec_lhu
, dec_sb
, dec_add
, dec_or
, dec_sl
,
995 dec_lbu
, dec_be
, dec_bg
, dec_bge
, dec_bgeu
, dec_bgu
, dec_sw
, dec_bne
,
996 dec_andhi
, dec_cmpe
, dec_cmpg
, dec_cmpge
, dec_cmpgeu
, dec_cmpgu
, dec_orhi
,
998 dec_sru
, dec_nor
, dec_mul
, dec_divu
, dec_rcsr
, dec_sr
, dec_xor
, dec_ill
,
999 dec_and
, dec_xnor
, dec_ill
, dec_scall
, dec_sextb
, dec_add
, dec_or
, dec_sl
,
1000 dec_b
, dec_modu
, dec_sub
, dec_user
, dec_wcsr
, dec_ill
, dec_call
, dec_sexth
,
1001 dec_bi
, dec_cmpe
, dec_cmpg
, dec_cmpge
, dec_cmpgeu
, dec_cmpgu
, dec_calli
,
1005 static inline void decode(DisasContext
*dc
, uint32_t ir
)
1007 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP
| CPU_LOG_TB_OP_OPT
))) {
1008 tcg_gen_debug_insn_start(dc
->pc
);
1012 LOG_DIS("%8.8x\t", dc
->ir
);
1014 dc
->opcode
= EXTRACT_FIELD(ir
, 26, 31);
1016 dc
->imm5
= EXTRACT_FIELD(ir
, 0, 4);
1017 dc
->imm16
= EXTRACT_FIELD(ir
, 0, 15);
1018 dc
->imm26
= EXTRACT_FIELD(ir
, 0, 25);
1020 dc
->csr
= EXTRACT_FIELD(ir
, 21, 25);
1021 dc
->r0
= EXTRACT_FIELD(ir
, 21, 25);
1022 dc
->r1
= EXTRACT_FIELD(ir
, 16, 20);
1023 dc
->r2
= EXTRACT_FIELD(ir
, 11, 15);
1025 /* bit 31 seems to indicate insn type. */
1026 if (ir
& (1 << 31)) {
1027 dc
->format
= OP_FMT_RR
;
1029 dc
->format
= OP_FMT_RI
;
1032 assert(ARRAY_SIZE(decinfo
) == 64);
1033 assert(dc
->opcode
< 64);
1035 decinfo
[dc
->opcode
](dc
);
1038 static void check_breakpoint(CPULM32State
*env
, DisasContext
*dc
)
1042 if (unlikely(!QTAILQ_EMPTY(&env
->breakpoints
))) {
1043 QTAILQ_FOREACH(bp
, &env
->breakpoints
, entry
) {
1044 if (bp
->pc
== dc
->pc
) {
1045 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
1046 t_gen_raise_exception(dc
, EXCP_DEBUG
);
1047 dc
->is_jmp
= DISAS_UPDATE
;
1053 /* generate intermediate code for basic block 'tb'. */
1055 void gen_intermediate_code_internal(LM32CPU
*cpu
,
1056 TranslationBlock
*tb
, bool search_pc
)
1058 CPUState
*cs
= CPU(cpu
);
1059 CPULM32State
*env
= &cpu
->env
;
1060 struct DisasContext ctx
, *dc
= &ctx
;
1061 uint16_t *gen_opc_end
;
1064 uint32_t next_page_start
;
1069 dc
->features
= cpu
->features
;
1070 dc
->num_breakpoints
= cpu
->num_breakpoints
;
1071 dc
->num_watchpoints
= cpu
->num_watchpoints
;
1074 gen_opc_end
= tcg_ctx
.gen_opc_buf
+ OPC_MAX_SIZE
;
1076 dc
->is_jmp
= DISAS_NEXT
;
1078 dc
->singlestep_enabled
= cs
->singlestep_enabled
;
1081 qemu_log_mask(LOG_GUEST_ERROR
,
1082 "unaligned PC=%x. Ignoring lowest bits.\n", pc_start
);
1086 next_page_start
= (pc_start
& TARGET_PAGE_MASK
) + TARGET_PAGE_SIZE
;
1089 max_insns
= tb
->cflags
& CF_COUNT_MASK
;
1090 if (max_insns
== 0) {
1091 max_insns
= CF_COUNT_MASK
;
1096 check_breakpoint(env
, dc
);
1099 j
= tcg_ctx
.gen_opc_ptr
- tcg_ctx
.gen_opc_buf
;
1103 tcg_ctx
.gen_opc_instr_start
[lj
++] = 0;
1106 tcg_ctx
.gen_opc_pc
[lj
] = dc
->pc
;
1107 tcg_ctx
.gen_opc_instr_start
[lj
] = 1;
1108 tcg_ctx
.gen_opc_icount
[lj
] = num_insns
;
1112 LOG_DIS("%8.8x:\t", dc
->pc
);
1114 if (num_insns
+ 1 == max_insns
&& (tb
->cflags
& CF_LAST_IO
)) {
1118 decode(dc
, cpu_ldl_code(env
, dc
->pc
));
1122 } while (!dc
->is_jmp
1123 && tcg_ctx
.gen_opc_ptr
< gen_opc_end
1124 && !cs
->singlestep_enabled
1126 && (dc
->pc
< next_page_start
)
1127 && num_insns
< max_insns
);
1129 if (tb
->cflags
& CF_LAST_IO
) {
1133 if (unlikely(cs
->singlestep_enabled
)) {
1134 if (dc
->is_jmp
== DISAS_NEXT
) {
1135 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
1137 t_gen_raise_exception(dc
, EXCP_DEBUG
);
1139 switch (dc
->is_jmp
) {
1141 gen_goto_tb(dc
, 1, dc
->pc
);
1146 /* indicate that the hash table must be used
1147 to find the next TB */
1151 /* nothing more to generate */
1156 gen_tb_end(tb
, num_insns
);
1157 *tcg_ctx
.gen_opc_ptr
= INDEX_op_end
;
1159 j
= tcg_ctx
.gen_opc_ptr
- tcg_ctx
.gen_opc_buf
;
1162 tcg_ctx
.gen_opc_instr_start
[lj
++] = 0;
1165 tb
->size
= dc
->pc
- pc_start
;
1166 tb
->icount
= num_insns
;
1170 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)) {
1172 log_target_disas(env
, pc_start
, dc
->pc
- pc_start
, 0);
1173 qemu_log("\nisize=%d osize=%td\n",
1174 dc
->pc
- pc_start
, tcg_ctx
.gen_opc_ptr
-
1175 tcg_ctx
.gen_opc_buf
);
1180 void gen_intermediate_code(CPULM32State
*env
, struct TranslationBlock
*tb
)
1182 gen_intermediate_code_internal(lm32_env_get_cpu(env
), tb
, false);
1185 void gen_intermediate_code_pc(CPULM32State
*env
, struct TranslationBlock
*tb
)
1187 gen_intermediate_code_internal(lm32_env_get_cpu(env
), tb
, true);
1190 void lm32_cpu_dump_state(CPUState
*cs
, FILE *f
, fprintf_function cpu_fprintf
,
1193 LM32CPU
*cpu
= LM32_CPU(cs
);
1194 CPULM32State
*env
= &cpu
->env
;
1201 cpu_fprintf(f
, "IN: PC=%x %s\n",
1202 env
->pc
, lookup_symbol(env
->pc
));
1204 cpu_fprintf(f
, "ie=%8.8x (IE=%x EIE=%x BIE=%x) im=%8.8x ip=%8.8x\n",
1206 (env
->ie
& IE_IE
) ? 1 : 0,
1207 (env
->ie
& IE_EIE
) ? 1 : 0,
1208 (env
->ie
& IE_BIE
) ? 1 : 0,
1209 lm32_pic_get_im(env
->pic_state
),
1210 lm32_pic_get_ip(env
->pic_state
));
1211 cpu_fprintf(f
, "eba=%8.8x deba=%8.8x\n",
1215 for (i
= 0; i
< 32; i
++) {
1216 cpu_fprintf(f
, "r%2.2d=%8.8x ", i
, env
->regs
[i
]);
1217 if ((i
+ 1) % 4 == 0) {
1218 cpu_fprintf(f
, "\n");
1221 cpu_fprintf(f
, "\n\n");
1224 void restore_state_to_opc(CPULM32State
*env
, TranslationBlock
*tb
, int pc_pos
)
1226 env
->pc
= tcg_ctx
.gen_opc_pc
[pc_pos
];
1229 void lm32_translate_init(void)
1233 cpu_env
= tcg_global_reg_new_ptr(TCG_AREG0
, "env");
1235 for (i
= 0; i
< ARRAY_SIZE(cpu_R
); i
++) {
1236 cpu_R
[i
] = tcg_global_mem_new(TCG_AREG0
,
1237 offsetof(CPULM32State
, regs
[i
]),
1241 for (i
= 0; i
< ARRAY_SIZE(cpu_bp
); i
++) {
1242 cpu_bp
[i
] = tcg_global_mem_new(TCG_AREG0
,
1243 offsetof(CPULM32State
, bp
[i
]),
1247 for (i
= 0; i
< ARRAY_SIZE(cpu_wp
); i
++) {
1248 cpu_wp
[i
] = tcg_global_mem_new(TCG_AREG0
,
1249 offsetof(CPULM32State
, wp
[i
]),
1253 cpu_pc
= tcg_global_mem_new(TCG_AREG0
,
1254 offsetof(CPULM32State
, pc
),
1256 cpu_ie
= tcg_global_mem_new(TCG_AREG0
,
1257 offsetof(CPULM32State
, ie
),
1259 cpu_icc
= tcg_global_mem_new(TCG_AREG0
,
1260 offsetof(CPULM32State
, icc
),
1262 cpu_dcc
= tcg_global_mem_new(TCG_AREG0
,
1263 offsetof(CPULM32State
, dcc
),
1265 cpu_cc
= tcg_global_mem_new(TCG_AREG0
,
1266 offsetof(CPULM32State
, cc
),
1268 cpu_cfg
= tcg_global_mem_new(TCG_AREG0
,
1269 offsetof(CPULM32State
, cfg
),
1271 cpu_eba
= tcg_global_mem_new(TCG_AREG0
,
1272 offsetof(CPULM32State
, eba
),
1274 cpu_dc
= tcg_global_mem_new(TCG_AREG0
,
1275 offsetof(CPULM32State
, dc
),
1277 cpu_deba
= tcg_global_mem_new(TCG_AREG0
,
1278 offsetof(CPULM32State
, deba
),