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/>.
20 #include "qemu/osdep.h"
22 #include "disas/disas.h"
23 #include "exec/helper-proto.h"
24 #include "exec/exec-all.h"
27 #include "exec/cpu_ldst.h"
28 #include "hw/lm32/lm32_pic.h"
30 #include "exec/helper-gen.h"
32 #include "trace-tcg.h"
38 #define LOG_DIS(...) \
41 qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__); \
45 #define EXTRACT_FIELD(src, start, end) \
46 (((src) >> start) & ((1 << (end - start + 1)) - 1))
50 static TCGv_env cpu_env
;
51 static TCGv cpu_R
[32];
61 static TCGv cpu_bp
[4];
62 static TCGv cpu_wp
[4];
64 #include "exec/gen-icount.h"
73 /* This is the state at translation time. */
74 typedef struct DisasContext
{
81 uint8_t r0
, r1
, r2
, csr
;
86 unsigned int delayed_branch
;
87 unsigned int tb_flags
, synced_flags
; /* tb dependent flags. */
90 struct TranslationBlock
*tb
;
91 int singlestep_enabled
;
94 uint8_t num_breakpoints
;
95 uint8_t num_watchpoints
;
98 static const char *regnames
[] = {
99 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
100 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
101 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
102 "r24", "r25", "r26/gp", "r27/fp", "r28/sp", "r29/ra",
103 "r30/ea", "r31/ba", "bp0", "bp1", "bp2", "bp3", "wp0",
107 static inline int zero_extend(unsigned int val
, int width
)
109 return val
& ((1 << width
) - 1);
112 static inline int sign_extend(unsigned int val
, int width
)
125 static inline void t_gen_raise_exception(DisasContext
*dc
, uint32_t index
)
127 TCGv_i32 tmp
= tcg_const_i32(index
);
129 gen_helper_raise_exception(cpu_env
, tmp
);
130 tcg_temp_free_i32(tmp
);
133 static inline void t_gen_illegal_insn(DisasContext
*dc
)
135 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
136 gen_helper_ill(cpu_env
);
139 static inline bool use_goto_tb(DisasContext
*dc
, target_ulong dest
)
141 if (unlikely(dc
->singlestep_enabled
)) {
145 #ifndef CONFIG_USER_ONLY
146 return (dc
->tb
->pc
& TARGET_PAGE_MASK
) == (dest
& TARGET_PAGE_MASK
);
152 static void gen_goto_tb(DisasContext
*dc
, int n
, target_ulong dest
)
154 if (use_goto_tb(dc
, dest
)) {
156 tcg_gen_movi_tl(cpu_pc
, dest
);
157 tcg_gen_exit_tb((uintptr_t)dc
->tb
+ n
);
159 tcg_gen_movi_tl(cpu_pc
, dest
);
160 if (dc
->singlestep_enabled
) {
161 t_gen_raise_exception(dc
, EXCP_DEBUG
);
167 static void dec_add(DisasContext
*dc
)
169 if (dc
->format
== OP_FMT_RI
) {
170 if (dc
->r0
== R_R0
) {
171 if (dc
->r1
== R_R0
&& dc
->imm16
== 0) {
174 LOG_DIS("mvi r%d, %d\n", dc
->r1
, sign_extend(dc
->imm16
, 16));
177 LOG_DIS("addi r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
178 sign_extend(dc
->imm16
, 16));
181 LOG_DIS("add r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
184 if (dc
->format
== OP_FMT_RI
) {
185 tcg_gen_addi_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
],
186 sign_extend(dc
->imm16
, 16));
188 tcg_gen_add_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
192 static void dec_and(DisasContext
*dc
)
194 if (dc
->format
== OP_FMT_RI
) {
195 LOG_DIS("andi r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
196 zero_extend(dc
->imm16
, 16));
198 LOG_DIS("and r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
201 if (dc
->format
== OP_FMT_RI
) {
202 tcg_gen_andi_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
],
203 zero_extend(dc
->imm16
, 16));
205 if (dc
->r0
== 0 && dc
->r1
== 0 && dc
->r2
== 0) {
206 tcg_gen_movi_tl(cpu_pc
, dc
->pc
+ 4);
207 gen_helper_hlt(cpu_env
);
209 tcg_gen_and_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
214 static void dec_andhi(DisasContext
*dc
)
216 LOG_DIS("andhi r%d, r%d, %d\n", dc
->r1
, dc
->r0
, dc
->imm16
);
218 tcg_gen_andi_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], (dc
->imm16
<< 16));
221 static void dec_b(DisasContext
*dc
)
223 if (dc
->r0
== R_RA
) {
225 } else if (dc
->r0
== R_EA
) {
227 } else if (dc
->r0
== R_BA
) {
230 LOG_DIS("b r%d\n", dc
->r0
);
233 /* restore IE.IE in case of an eret */
234 if (dc
->r0
== R_EA
) {
235 TCGv t0
= tcg_temp_new();
236 TCGLabel
*l1
= gen_new_label();
237 tcg_gen_andi_tl(t0
, cpu_ie
, IE_EIE
);
238 tcg_gen_ori_tl(cpu_ie
, cpu_ie
, IE_IE
);
239 tcg_gen_brcondi_tl(TCG_COND_EQ
, t0
, IE_EIE
, l1
);
240 tcg_gen_andi_tl(cpu_ie
, cpu_ie
, ~IE_IE
);
243 } else if (dc
->r0
== R_BA
) {
244 TCGv t0
= tcg_temp_new();
245 TCGLabel
*l1
= gen_new_label();
246 tcg_gen_andi_tl(t0
, cpu_ie
, IE_BIE
);
247 tcg_gen_ori_tl(cpu_ie
, cpu_ie
, IE_IE
);
248 tcg_gen_brcondi_tl(TCG_COND_EQ
, t0
, IE_BIE
, l1
);
249 tcg_gen_andi_tl(cpu_ie
, cpu_ie
, ~IE_IE
);
253 tcg_gen_mov_tl(cpu_pc
, cpu_R
[dc
->r0
]);
255 dc
->is_jmp
= DISAS_JUMP
;
258 static void dec_bi(DisasContext
*dc
)
260 LOG_DIS("bi %d\n", sign_extend(dc
->imm26
<< 2, 26));
262 gen_goto_tb(dc
, 0, dc
->pc
+ (sign_extend(dc
->imm26
<< 2, 26)));
264 dc
->is_jmp
= DISAS_TB_JUMP
;
267 static inline void gen_cond_branch(DisasContext
*dc
, int cond
)
269 TCGLabel
*l1
= gen_new_label();
270 tcg_gen_brcond_tl(cond
, cpu_R
[dc
->r0
], cpu_R
[dc
->r1
], l1
);
271 gen_goto_tb(dc
, 0, dc
->pc
+ 4);
273 gen_goto_tb(dc
, 1, dc
->pc
+ (sign_extend(dc
->imm16
<< 2, 16)));
274 dc
->is_jmp
= DISAS_TB_JUMP
;
277 static void dec_be(DisasContext
*dc
)
279 LOG_DIS("be r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
280 sign_extend(dc
->imm16
, 16) * 4);
282 gen_cond_branch(dc
, TCG_COND_EQ
);
285 static void dec_bg(DisasContext
*dc
)
287 LOG_DIS("bg r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
288 sign_extend(dc
->imm16
, 16 * 4));
290 gen_cond_branch(dc
, TCG_COND_GT
);
293 static void dec_bge(DisasContext
*dc
)
295 LOG_DIS("bge r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
296 sign_extend(dc
->imm16
, 16) * 4);
298 gen_cond_branch(dc
, TCG_COND_GE
);
301 static void dec_bgeu(DisasContext
*dc
)
303 LOG_DIS("bgeu r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
304 sign_extend(dc
->imm16
, 16) * 4);
306 gen_cond_branch(dc
, TCG_COND_GEU
);
309 static void dec_bgu(DisasContext
*dc
)
311 LOG_DIS("bgu r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
312 sign_extend(dc
->imm16
, 16) * 4);
314 gen_cond_branch(dc
, TCG_COND_GTU
);
317 static void dec_bne(DisasContext
*dc
)
319 LOG_DIS("bne r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
320 sign_extend(dc
->imm16
, 16) * 4);
322 gen_cond_branch(dc
, TCG_COND_NE
);
325 static void dec_call(DisasContext
*dc
)
327 LOG_DIS("call r%d\n", dc
->r0
);
329 tcg_gen_movi_tl(cpu_R
[R_RA
], dc
->pc
+ 4);
330 tcg_gen_mov_tl(cpu_pc
, cpu_R
[dc
->r0
]);
332 dc
->is_jmp
= DISAS_JUMP
;
335 static void dec_calli(DisasContext
*dc
)
337 LOG_DIS("calli %d\n", sign_extend(dc
->imm26
, 26) * 4);
339 tcg_gen_movi_tl(cpu_R
[R_RA
], dc
->pc
+ 4);
340 gen_goto_tb(dc
, 0, dc
->pc
+ (sign_extend(dc
->imm26
<< 2, 26)));
342 dc
->is_jmp
= DISAS_TB_JUMP
;
345 static inline void gen_compare(DisasContext
*dc
, int cond
)
349 if (dc
->format
== OP_FMT_RI
) {
353 i
= zero_extend(dc
->imm16
, 16);
356 i
= sign_extend(dc
->imm16
, 16);
360 tcg_gen_setcondi_tl(cond
, cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], i
);
362 tcg_gen_setcond_tl(cond
, cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
366 static void dec_cmpe(DisasContext
*dc
)
368 if (dc
->format
== OP_FMT_RI
) {
369 LOG_DIS("cmpei r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
370 sign_extend(dc
->imm16
, 16));
372 LOG_DIS("cmpe r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
375 gen_compare(dc
, TCG_COND_EQ
);
378 static void dec_cmpg(DisasContext
*dc
)
380 if (dc
->format
== OP_FMT_RI
) {
381 LOG_DIS("cmpgi r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
382 sign_extend(dc
->imm16
, 16));
384 LOG_DIS("cmpg r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
387 gen_compare(dc
, TCG_COND_GT
);
390 static void dec_cmpge(DisasContext
*dc
)
392 if (dc
->format
== OP_FMT_RI
) {
393 LOG_DIS("cmpgei r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
394 sign_extend(dc
->imm16
, 16));
396 LOG_DIS("cmpge r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
399 gen_compare(dc
, TCG_COND_GE
);
402 static void dec_cmpgeu(DisasContext
*dc
)
404 if (dc
->format
== OP_FMT_RI
) {
405 LOG_DIS("cmpgeui r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
406 zero_extend(dc
->imm16
, 16));
408 LOG_DIS("cmpgeu r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
411 gen_compare(dc
, TCG_COND_GEU
);
414 static void dec_cmpgu(DisasContext
*dc
)
416 if (dc
->format
== OP_FMT_RI
) {
417 LOG_DIS("cmpgui r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
418 zero_extend(dc
->imm16
, 16));
420 LOG_DIS("cmpgu r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
423 gen_compare(dc
, TCG_COND_GTU
);
426 static void dec_cmpne(DisasContext
*dc
)
428 if (dc
->format
== OP_FMT_RI
) {
429 LOG_DIS("cmpnei r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
430 sign_extend(dc
->imm16
, 16));
432 LOG_DIS("cmpne r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
435 gen_compare(dc
, TCG_COND_NE
);
438 static void dec_divu(DisasContext
*dc
)
442 LOG_DIS("divu r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
444 if (!(dc
->features
& LM32_FEATURE_DIVIDE
)) {
445 qemu_log_mask(LOG_GUEST_ERROR
, "hardware divider is not available\n");
446 t_gen_illegal_insn(dc
);
450 l1
= gen_new_label();
451 tcg_gen_brcondi_tl(TCG_COND_NE
, cpu_R
[dc
->r1
], 0, l1
);
452 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
453 t_gen_raise_exception(dc
, EXCP_DIVIDE_BY_ZERO
);
455 tcg_gen_divu_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
458 static void dec_lb(DisasContext
*dc
)
462 LOG_DIS("lb 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_ld8s(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
470 static void dec_lbu(DisasContext
*dc
)
474 LOG_DIS("lbu 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_ld8u(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
482 static void dec_lh(DisasContext
*dc
)
486 LOG_DIS("lh 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_ld16s(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
494 static void dec_lhu(DisasContext
*dc
)
498 LOG_DIS("lhu r%d, (r%d+%d)\n", dc
->r1
, dc
->r0
, dc
->imm16
);
501 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
502 tcg_gen_qemu_ld16u(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
506 static void dec_lw(DisasContext
*dc
)
510 LOG_DIS("lw r%d, (r%d+%d)\n", dc
->r1
, dc
->r0
, sign_extend(dc
->imm16
, 16));
513 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
514 tcg_gen_qemu_ld32s(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
518 static void dec_modu(DisasContext
*dc
)
522 LOG_DIS("modu r%d, r%d, %d\n", dc
->r2
, dc
->r0
, dc
->r1
);
524 if (!(dc
->features
& LM32_FEATURE_DIVIDE
)) {
525 qemu_log_mask(LOG_GUEST_ERROR
, "hardware divider is not available\n");
526 t_gen_illegal_insn(dc
);
530 l1
= gen_new_label();
531 tcg_gen_brcondi_tl(TCG_COND_NE
, cpu_R
[dc
->r1
], 0, l1
);
532 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
533 t_gen_raise_exception(dc
, EXCP_DIVIDE_BY_ZERO
);
535 tcg_gen_remu_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
538 static void dec_mul(DisasContext
*dc
)
540 if (dc
->format
== OP_FMT_RI
) {
541 LOG_DIS("muli r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
542 sign_extend(dc
->imm16
, 16));
544 LOG_DIS("mul r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
547 if (!(dc
->features
& LM32_FEATURE_MULTIPLY
)) {
548 qemu_log_mask(LOG_GUEST_ERROR
,
549 "hardware multiplier is not available\n");
550 t_gen_illegal_insn(dc
);
554 if (dc
->format
== OP_FMT_RI
) {
555 tcg_gen_muli_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
],
556 sign_extend(dc
->imm16
, 16));
558 tcg_gen_mul_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
562 static void dec_nor(DisasContext
*dc
)
564 if (dc
->format
== OP_FMT_RI
) {
565 LOG_DIS("nori r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
566 zero_extend(dc
->imm16
, 16));
568 LOG_DIS("nor r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
571 if (dc
->format
== OP_FMT_RI
) {
572 TCGv t0
= tcg_temp_new();
573 tcg_gen_movi_tl(t0
, zero_extend(dc
->imm16
, 16));
574 tcg_gen_nor_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], t0
);
577 tcg_gen_nor_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
581 static void dec_or(DisasContext
*dc
)
583 if (dc
->format
== OP_FMT_RI
) {
584 LOG_DIS("ori r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
585 zero_extend(dc
->imm16
, 16));
587 if (dc
->r1
== R_R0
) {
588 LOG_DIS("mv r%d, r%d\n", dc
->r2
, dc
->r0
);
590 LOG_DIS("or r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
594 if (dc
->format
== OP_FMT_RI
) {
595 tcg_gen_ori_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
],
596 zero_extend(dc
->imm16
, 16));
598 tcg_gen_or_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
602 static void dec_orhi(DisasContext
*dc
)
604 if (dc
->r0
== R_R0
) {
605 LOG_DIS("mvhi r%d, %d\n", dc
->r1
, dc
->imm16
);
607 LOG_DIS("orhi r%d, r%d, %d\n", dc
->r1
, dc
->r0
, dc
->imm16
);
610 tcg_gen_ori_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], (dc
->imm16
<< 16));
613 static void dec_scall(DisasContext
*dc
)
618 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
619 t_gen_raise_exception(dc
, EXCP_BREAKPOINT
);
623 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
624 t_gen_raise_exception(dc
, EXCP_SYSTEMCALL
);
627 qemu_log_mask(LOG_GUEST_ERROR
, "invalid opcode @0x%x", dc
->pc
);
628 t_gen_illegal_insn(dc
);
633 static void dec_rcsr(DisasContext
*dc
)
635 LOG_DIS("rcsr r%d, %d\n", dc
->r2
, dc
->csr
);
639 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_ie
);
642 gen_helper_rcsr_im(cpu_R
[dc
->r2
], cpu_env
);
645 gen_helper_rcsr_ip(cpu_R
[dc
->r2
], cpu_env
);
648 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_cc
);
651 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_cfg
);
654 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_eba
);
657 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_dc
);
660 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_deba
);
663 gen_helper_rcsr_jtx(cpu_R
[dc
->r2
], cpu_env
);
666 gen_helper_rcsr_jrx(cpu_R
[dc
->r2
], cpu_env
);
678 qemu_log_mask(LOG_GUEST_ERROR
, "invalid read access csr=%x\n", dc
->csr
);
681 qemu_log_mask(LOG_GUEST_ERROR
, "read_csr: unknown csr=%x\n", dc
->csr
);
686 static void dec_sb(DisasContext
*dc
)
690 LOG_DIS("sb (r%d+%d), r%d\n", dc
->r0
, dc
->imm16
, dc
->r1
);
693 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
694 tcg_gen_qemu_st8(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
698 static void dec_sextb(DisasContext
*dc
)
700 LOG_DIS("sextb r%d, r%d\n", dc
->r2
, dc
->r0
);
702 if (!(dc
->features
& LM32_FEATURE_SIGN_EXTEND
)) {
703 qemu_log_mask(LOG_GUEST_ERROR
,
704 "hardware sign extender is not available\n");
705 t_gen_illegal_insn(dc
);
709 tcg_gen_ext8s_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
]);
712 static void dec_sexth(DisasContext
*dc
)
714 LOG_DIS("sexth r%d, r%d\n", dc
->r2
, dc
->r0
);
716 if (!(dc
->features
& LM32_FEATURE_SIGN_EXTEND
)) {
717 qemu_log_mask(LOG_GUEST_ERROR
,
718 "hardware sign extender is not available\n");
719 t_gen_illegal_insn(dc
);
723 tcg_gen_ext16s_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
]);
726 static void dec_sh(DisasContext
*dc
)
730 LOG_DIS("sh (r%d+%d), r%d\n", dc
->r0
, dc
->imm16
, dc
->r1
);
733 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
734 tcg_gen_qemu_st16(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
738 static void dec_sl(DisasContext
*dc
)
740 if (dc
->format
== OP_FMT_RI
) {
741 LOG_DIS("sli r%d, r%d, %d\n", dc
->r1
, dc
->r0
, dc
->imm5
);
743 LOG_DIS("sl r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
746 if (!(dc
->features
& LM32_FEATURE_SHIFT
)) {
747 qemu_log_mask(LOG_GUEST_ERROR
, "hardware shifter is not available\n");
748 t_gen_illegal_insn(dc
);
752 if (dc
->format
== OP_FMT_RI
) {
753 tcg_gen_shli_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], dc
->imm5
);
755 TCGv t0
= tcg_temp_new();
756 tcg_gen_andi_tl(t0
, cpu_R
[dc
->r1
], 0x1f);
757 tcg_gen_shl_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], t0
);
762 static void dec_sr(DisasContext
*dc
)
764 if (dc
->format
== OP_FMT_RI
) {
765 LOG_DIS("sri r%d, r%d, %d\n", dc
->r1
, dc
->r0
, dc
->imm5
);
767 LOG_DIS("sr r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
770 /* The real CPU (w/o hardware shifter) only supports right shift by exactly
772 if (dc
->format
== OP_FMT_RI
) {
773 if (!(dc
->features
& LM32_FEATURE_SHIFT
) && (dc
->imm5
!= 1)) {
774 qemu_log_mask(LOG_GUEST_ERROR
,
775 "hardware shifter is not available\n");
776 t_gen_illegal_insn(dc
);
779 tcg_gen_sari_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], dc
->imm5
);
781 TCGLabel
*l1
= gen_new_label();
782 TCGLabel
*l2
= gen_new_label();
783 TCGv t0
= tcg_temp_local_new();
784 tcg_gen_andi_tl(t0
, cpu_R
[dc
->r1
], 0x1f);
786 if (!(dc
->features
& LM32_FEATURE_SHIFT
)) {
787 tcg_gen_brcondi_tl(TCG_COND_EQ
, t0
, 1, l1
);
788 t_gen_illegal_insn(dc
);
793 tcg_gen_sar_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], t0
);
800 static void dec_sru(DisasContext
*dc
)
802 if (dc
->format
== OP_FMT_RI
) {
803 LOG_DIS("srui r%d, r%d, %d\n", dc
->r1
, dc
->r0
, dc
->imm5
);
805 LOG_DIS("sru r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
808 if (dc
->format
== OP_FMT_RI
) {
809 if (!(dc
->features
& LM32_FEATURE_SHIFT
) && (dc
->imm5
!= 1)) {
810 qemu_log_mask(LOG_GUEST_ERROR
,
811 "hardware shifter is not available\n");
812 t_gen_illegal_insn(dc
);
815 tcg_gen_shri_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], dc
->imm5
);
817 TCGLabel
*l1
= gen_new_label();
818 TCGLabel
*l2
= gen_new_label();
819 TCGv t0
= tcg_temp_local_new();
820 tcg_gen_andi_tl(t0
, cpu_R
[dc
->r1
], 0x1f);
822 if (!(dc
->features
& LM32_FEATURE_SHIFT
)) {
823 tcg_gen_brcondi_tl(TCG_COND_EQ
, t0
, 1, l1
);
824 t_gen_illegal_insn(dc
);
829 tcg_gen_shr_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], t0
);
836 static void dec_sub(DisasContext
*dc
)
838 LOG_DIS("sub r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
840 tcg_gen_sub_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
843 static void dec_sw(DisasContext
*dc
)
847 LOG_DIS("sw (r%d+%d), r%d\n", dc
->r0
, sign_extend(dc
->imm16
, 16), dc
->r1
);
850 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
851 tcg_gen_qemu_st32(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
855 static void dec_user(DisasContext
*dc
)
859 qemu_log_mask(LOG_GUEST_ERROR
, "user instruction undefined\n");
860 t_gen_illegal_insn(dc
);
863 static void dec_wcsr(DisasContext
*dc
)
867 LOG_DIS("wcsr %d, r%d\n", dc
->csr
, dc
->r1
);
871 tcg_gen_mov_tl(cpu_ie
, cpu_R
[dc
->r1
]);
872 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 */
877 if (dc
->tb
->cflags
& CF_USE_ICOUNT
) {
880 gen_helper_wcsr_im(cpu_env
, cpu_R
[dc
->r1
]);
881 tcg_gen_movi_tl(cpu_pc
, dc
->pc
+ 4);
882 if (dc
->tb
->cflags
& CF_USE_ICOUNT
) {
885 dc
->is_jmp
= DISAS_UPDATE
;
888 /* mark as an io operation because it could cause an interrupt */
889 if (dc
->tb
->cflags
& CF_USE_ICOUNT
) {
892 gen_helper_wcsr_ip(cpu_env
, cpu_R
[dc
->r1
]);
893 tcg_gen_movi_tl(cpu_pc
, dc
->pc
+ 4);
894 if (dc
->tb
->cflags
& CF_USE_ICOUNT
) {
897 dc
->is_jmp
= DISAS_UPDATE
;
906 tcg_gen_mov_tl(cpu_eba
, cpu_R
[dc
->r1
]);
909 tcg_gen_mov_tl(cpu_deba
, cpu_R
[dc
->r1
]);
912 gen_helper_wcsr_jtx(cpu_env
, cpu_R
[dc
->r1
]);
915 gen_helper_wcsr_jrx(cpu_env
, cpu_R
[dc
->r1
]);
918 gen_helper_wcsr_dc(cpu_env
, cpu_R
[dc
->r1
]);
924 no
= dc
->csr
- CSR_BP0
;
925 if (dc
->num_breakpoints
<= no
) {
926 qemu_log_mask(LOG_GUEST_ERROR
,
927 "breakpoint #%i is not available\n", no
);
928 t_gen_illegal_insn(dc
);
931 gen_helper_wcsr_bp(cpu_env
, cpu_R
[dc
->r1
], tcg_const_i32(no
));
937 no
= dc
->csr
- CSR_WP0
;
938 if (dc
->num_watchpoints
<= no
) {
939 qemu_log_mask(LOG_GUEST_ERROR
,
940 "watchpoint #%i is not available\n", no
);
941 t_gen_illegal_insn(dc
);
944 gen_helper_wcsr_wp(cpu_env
, cpu_R
[dc
->r1
], tcg_const_i32(no
));
948 qemu_log_mask(LOG_GUEST_ERROR
, "invalid write access csr=%x\n",
952 qemu_log_mask(LOG_GUEST_ERROR
, "write_csr: unknown csr=%x\n",
958 static void dec_xnor(DisasContext
*dc
)
960 if (dc
->format
== OP_FMT_RI
) {
961 LOG_DIS("xnori r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
962 zero_extend(dc
->imm16
, 16));
964 if (dc
->r1
== R_R0
) {
965 LOG_DIS("not r%d, r%d\n", dc
->r2
, dc
->r0
);
967 LOG_DIS("xnor r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
971 if (dc
->format
== OP_FMT_RI
) {
972 tcg_gen_xori_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
],
973 zero_extend(dc
->imm16
, 16));
974 tcg_gen_not_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r1
]);
976 tcg_gen_eqv_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
980 static void dec_xor(DisasContext
*dc
)
982 if (dc
->format
== OP_FMT_RI
) {
983 LOG_DIS("xori r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
984 zero_extend(dc
->imm16
, 16));
986 LOG_DIS("xor r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
989 if (dc
->format
== OP_FMT_RI
) {
990 tcg_gen_xori_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
],
991 zero_extend(dc
->imm16
, 16));
993 tcg_gen_xor_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
997 static void dec_ill(DisasContext
*dc
)
999 qemu_log_mask(LOG_GUEST_ERROR
, "invalid opcode 0x%02x\n", dc
->opcode
);
1000 t_gen_illegal_insn(dc
);
1003 typedef void (*DecoderInfo
)(DisasContext
*dc
);
1004 static const DecoderInfo decinfo
[] = {
1005 dec_sru
, dec_nor
, dec_mul
, dec_sh
, dec_lb
, dec_sr
, dec_xor
, dec_lh
,
1006 dec_and
, dec_xnor
, dec_lw
, dec_lhu
, dec_sb
, dec_add
, dec_or
, dec_sl
,
1007 dec_lbu
, dec_be
, dec_bg
, dec_bge
, dec_bgeu
, dec_bgu
, dec_sw
, dec_bne
,
1008 dec_andhi
, dec_cmpe
, dec_cmpg
, dec_cmpge
, dec_cmpgeu
, dec_cmpgu
, dec_orhi
,
1010 dec_sru
, dec_nor
, dec_mul
, dec_divu
, dec_rcsr
, dec_sr
, dec_xor
, dec_ill
,
1011 dec_and
, dec_xnor
, dec_ill
, dec_scall
, dec_sextb
, dec_add
, dec_or
, dec_sl
,
1012 dec_b
, dec_modu
, dec_sub
, dec_user
, dec_wcsr
, dec_ill
, dec_call
, dec_sexth
,
1013 dec_bi
, dec_cmpe
, dec_cmpg
, dec_cmpge
, dec_cmpgeu
, dec_cmpgu
, dec_calli
,
1017 static inline void decode(DisasContext
*dc
, uint32_t ir
)
1020 LOG_DIS("%8.8x\t", dc
->ir
);
1022 dc
->opcode
= EXTRACT_FIELD(ir
, 26, 31);
1024 dc
->imm5
= EXTRACT_FIELD(ir
, 0, 4);
1025 dc
->imm16
= EXTRACT_FIELD(ir
, 0, 15);
1026 dc
->imm26
= EXTRACT_FIELD(ir
, 0, 25);
1028 dc
->csr
= EXTRACT_FIELD(ir
, 21, 25);
1029 dc
->r0
= EXTRACT_FIELD(ir
, 21, 25);
1030 dc
->r1
= EXTRACT_FIELD(ir
, 16, 20);
1031 dc
->r2
= EXTRACT_FIELD(ir
, 11, 15);
1033 /* bit 31 seems to indicate insn type. */
1034 if (ir
& (1 << 31)) {
1035 dc
->format
= OP_FMT_RR
;
1037 dc
->format
= OP_FMT_RI
;
1040 assert(ARRAY_SIZE(decinfo
) == 64);
1041 assert(dc
->opcode
< 64);
1043 decinfo
[dc
->opcode
](dc
);
1046 /* generate intermediate code for basic block 'tb'. */
1047 void gen_intermediate_code(CPULM32State
*env
, struct TranslationBlock
*tb
)
1049 LM32CPU
*cpu
= lm32_env_get_cpu(env
);
1050 CPUState
*cs
= CPU(cpu
);
1051 struct DisasContext ctx
, *dc
= &ctx
;
1053 uint32_t next_page_start
;
1058 dc
->features
= cpu
->features
;
1059 dc
->num_breakpoints
= cpu
->num_breakpoints
;
1060 dc
->num_watchpoints
= cpu
->num_watchpoints
;
1063 dc
->is_jmp
= DISAS_NEXT
;
1065 dc
->singlestep_enabled
= cs
->singlestep_enabled
;
1068 qemu_log_mask(LOG_GUEST_ERROR
,
1069 "unaligned PC=%x. Ignoring lowest bits.\n", pc_start
);
1073 next_page_start
= (pc_start
& TARGET_PAGE_MASK
) + TARGET_PAGE_SIZE
;
1075 max_insns
= tb
->cflags
& CF_COUNT_MASK
;
1076 if (max_insns
== 0) {
1077 max_insns
= CF_COUNT_MASK
;
1079 if (max_insns
> TCG_MAX_INSNS
) {
1080 max_insns
= TCG_MAX_INSNS
;
1085 tcg_gen_insn_start(dc
->pc
);
1088 if (unlikely(cpu_breakpoint_test(cs
, dc
->pc
, BP_ANY
))) {
1089 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
1090 t_gen_raise_exception(dc
, EXCP_DEBUG
);
1091 dc
->is_jmp
= DISAS_UPDATE
;
1092 /* The address covered by the breakpoint must be included in
1093 [tb->pc, tb->pc + tb->size) in order to for it to be
1094 properly cleared -- thus we increment the PC here so that
1095 the logic setting tb->size below does the right thing. */
1101 LOG_DIS("%8.8x:\t", dc
->pc
);
1103 if (num_insns
== max_insns
&& (tb
->cflags
& CF_LAST_IO
)) {
1107 decode(dc
, cpu_ldl_code(env
, dc
->pc
));
1109 } while (!dc
->is_jmp
1110 && !tcg_op_buf_full()
1111 && !cs
->singlestep_enabled
1113 && (dc
->pc
< next_page_start
)
1114 && num_insns
< max_insns
);
1116 if (tb
->cflags
& CF_LAST_IO
) {
1120 if (unlikely(cs
->singlestep_enabled
)) {
1121 if (dc
->is_jmp
== DISAS_NEXT
) {
1122 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
1124 t_gen_raise_exception(dc
, EXCP_DEBUG
);
1126 switch (dc
->is_jmp
) {
1128 gen_goto_tb(dc
, 1, dc
->pc
);
1133 /* indicate that the hash table must be used
1134 to find the next TB */
1138 /* nothing more to generate */
1143 gen_tb_end(tb
, num_insns
);
1145 tb
->size
= dc
->pc
- pc_start
;
1146 tb
->icount
= num_insns
;
1149 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)
1150 && qemu_log_in_addr_range(pc_start
)) {
1152 log_target_disas(cs
, pc_start
, dc
->pc
- pc_start
, 0);
1153 qemu_log("\nisize=%d osize=%d\n",
1154 dc
->pc
- pc_start
, tcg_op_buf_count());
1159 void lm32_cpu_dump_state(CPUState
*cs
, FILE *f
, fprintf_function cpu_fprintf
,
1162 LM32CPU
*cpu
= LM32_CPU(cs
);
1163 CPULM32State
*env
= &cpu
->env
;
1170 cpu_fprintf(f
, "IN: PC=%x %s\n",
1171 env
->pc
, lookup_symbol(env
->pc
));
1173 cpu_fprintf(f
, "ie=%8.8x (IE=%x EIE=%x BIE=%x) im=%8.8x ip=%8.8x\n",
1175 (env
->ie
& IE_IE
) ? 1 : 0,
1176 (env
->ie
& IE_EIE
) ? 1 : 0,
1177 (env
->ie
& IE_BIE
) ? 1 : 0,
1178 lm32_pic_get_im(env
->pic_state
),
1179 lm32_pic_get_ip(env
->pic_state
));
1180 cpu_fprintf(f
, "eba=%8.8x deba=%8.8x\n",
1184 for (i
= 0; i
< 32; i
++) {
1185 cpu_fprintf(f
, "r%2.2d=%8.8x ", i
, env
->regs
[i
]);
1186 if ((i
+ 1) % 4 == 0) {
1187 cpu_fprintf(f
, "\n");
1190 cpu_fprintf(f
, "\n\n");
1193 void restore_state_to_opc(CPULM32State
*env
, TranslationBlock
*tb
,
1199 void lm32_translate_init(void)
1203 cpu_env
= tcg_global_reg_new_ptr(TCG_AREG0
, "env");
1204 tcg_ctx
.tcg_env
= cpu_env
;
1206 for (i
= 0; i
< ARRAY_SIZE(cpu_R
); i
++) {
1207 cpu_R
[i
] = tcg_global_mem_new(cpu_env
,
1208 offsetof(CPULM32State
, regs
[i
]),
1212 for (i
= 0; i
< ARRAY_SIZE(cpu_bp
); i
++) {
1213 cpu_bp
[i
] = tcg_global_mem_new(cpu_env
,
1214 offsetof(CPULM32State
, bp
[i
]),
1218 for (i
= 0; i
< ARRAY_SIZE(cpu_wp
); i
++) {
1219 cpu_wp
[i
] = tcg_global_mem_new(cpu_env
,
1220 offsetof(CPULM32State
, wp
[i
]),
1224 cpu_pc
= tcg_global_mem_new(cpu_env
,
1225 offsetof(CPULM32State
, pc
),
1227 cpu_ie
= tcg_global_mem_new(cpu_env
,
1228 offsetof(CPULM32State
, ie
),
1230 cpu_icc
= tcg_global_mem_new(cpu_env
,
1231 offsetof(CPULM32State
, icc
),
1233 cpu_dcc
= tcg_global_mem_new(cpu_env
,
1234 offsetof(CPULM32State
, dcc
),
1236 cpu_cc
= tcg_global_mem_new(cpu_env
,
1237 offsetof(CPULM32State
, cc
),
1239 cpu_cfg
= tcg_global_mem_new(cpu_env
,
1240 offsetof(CPULM32State
, cfg
),
1242 cpu_eba
= tcg_global_mem_new(cpu_env
,
1243 offsetof(CPULM32State
, eba
),
1245 cpu_dc
= tcg_global_mem_new(cpu_env
,
1246 offsetof(CPULM32State
, dc
),
1248 cpu_deba
= tcg_global_mem_new(cpu_env
,
1249 offsetof(CPULM32State
, deba
),