tests: send error_report to test log
[qemu/ar7.git] / target-lm32 / translate.c
blob842af63a989b61d88195c8e5b062f178dd83548c
1 /*
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"
21 #include "cpu.h"
22 #include "disas/disas.h"
23 #include "exec/helper-proto.h"
24 #include "exec/exec-all.h"
25 #include "tcg-op.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"
33 #include "exec/log.h"
36 #define DISAS_LM32 0
38 #define LOG_DIS(...) \
39 do { \
40 if (DISAS_LM32) { \
41 qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__); \
42 } \
43 } while (0)
45 #define EXTRACT_FIELD(src, start, end) \
46 (((src) >> start) & ((1 << (end - start + 1)) - 1))
48 #define MEM_INDEX 0
50 static TCGv_env cpu_env;
51 static TCGv cpu_R[32];
52 static TCGv cpu_pc;
53 static TCGv cpu_ie;
54 static TCGv cpu_icc;
55 static TCGv cpu_dcc;
56 static TCGv cpu_cc;
57 static TCGv cpu_cfg;
58 static TCGv cpu_eba;
59 static TCGv cpu_dc;
60 static TCGv cpu_deba;
61 static TCGv cpu_bp[4];
62 static TCGv cpu_wp[4];
64 #include "exec/gen-icount.h"
66 enum {
67 OP_FMT_RI,
68 OP_FMT_RR,
69 OP_FMT_CR,
70 OP_FMT_I
73 /* This is the state at translation time. */
74 typedef struct DisasContext {
75 target_ulong pc;
77 /* Decoder. */
78 int format;
79 uint32_t ir;
80 uint8_t opcode;
81 uint8_t r0, r1, r2, csr;
82 uint16_t imm5;
83 uint16_t imm16;
84 uint32_t imm26;
86 unsigned int delayed_branch;
87 unsigned int tb_flags, synced_flags; /* tb dependent flags. */
88 int is_jmp;
90 struct TranslationBlock *tb;
91 int singlestep_enabled;
93 uint32_t features;
94 uint8_t num_breakpoints;
95 uint8_t num_watchpoints;
96 } DisasContext;
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",
104 "wp1", "wp2", "wp3"
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)
114 int sval;
116 /* LSL. */
117 val <<= 32 - width;
118 sval = val;
119 /* ASR. */
120 sval >>= 32 - width;
122 return sval;
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)) {
142 return false;
145 #ifndef CONFIG_USER_ONLY
146 return (dc->tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
147 #else
148 return true;
149 #endif
152 static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
154 if (use_goto_tb(dc, dest)) {
155 tcg_gen_goto_tb(n);
156 tcg_gen_movi_tl(cpu_pc, dest);
157 tcg_gen_exit_tb((uintptr_t)dc->tb + n);
158 } else {
159 tcg_gen_movi_tl(cpu_pc, dest);
160 if (dc->singlestep_enabled) {
161 t_gen_raise_exception(dc, EXCP_DEBUG);
163 tcg_gen_exit_tb(0);
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) {
172 LOG_DIS("nop\n");
173 } else {
174 LOG_DIS("mvi r%d, %d\n", dc->r1, sign_extend(dc->imm16, 16));
176 } else {
177 LOG_DIS("addi r%d, r%d, %d\n", dc->r1, dc->r0,
178 sign_extend(dc->imm16, 16));
180 } else {
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));
187 } else {
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));
197 } else {
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));
204 } else {
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);
208 } else {
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) {
224 LOG_DIS("ret\n");
225 } else if (dc->r0 == R_EA) {
226 LOG_DIS("eret\n");
227 } else if (dc->r0 == R_BA) {
228 LOG_DIS("bret\n");
229 } else {
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);
241 gen_set_label(l1);
242 tcg_temp_free(t0);
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);
250 gen_set_label(l1);
251 tcg_temp_free(t0);
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);
272 gen_set_label(l1);
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)
347 int i;
349 if (dc->format == OP_FMT_RI) {
350 switch (cond) {
351 case TCG_COND_GEU:
352 case TCG_COND_GTU:
353 i = zero_extend(dc->imm16, 16);
354 break;
355 default:
356 i = sign_extend(dc->imm16, 16);
357 break;
360 tcg_gen_setcondi_tl(cond, cpu_R[dc->r1], cpu_R[dc->r0], i);
361 } else {
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));
371 } else {
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));
383 } else {
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));
395 } else {
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));
407 } else {
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));
419 } else {
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));
431 } else {
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)
440 TCGLabel *l1;
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);
447 return;
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);
454 gen_set_label(l1);
455 tcg_gen_divu_tl(cpu_R[dc->r2], cpu_R[dc->r0], cpu_R[dc->r1]);
458 static void dec_lb(DisasContext *dc)
460 TCGv t0;
462 LOG_DIS("lb r%d, (r%d+%d)\n", dc->r1, dc->r0, dc->imm16);
464 t0 = tcg_temp_new();
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);
467 tcg_temp_free(t0);
470 static void dec_lbu(DisasContext *dc)
472 TCGv t0;
474 LOG_DIS("lbu r%d, (r%d+%d)\n", dc->r1, dc->r0, dc->imm16);
476 t0 = tcg_temp_new();
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);
479 tcg_temp_free(t0);
482 static void dec_lh(DisasContext *dc)
484 TCGv t0;
486 LOG_DIS("lh r%d, (r%d+%d)\n", dc->r1, dc->r0, dc->imm16);
488 t0 = tcg_temp_new();
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);
491 tcg_temp_free(t0);
494 static void dec_lhu(DisasContext *dc)
496 TCGv t0;
498 LOG_DIS("lhu r%d, (r%d+%d)\n", dc->r1, dc->r0, dc->imm16);
500 t0 = tcg_temp_new();
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);
503 tcg_temp_free(t0);
506 static void dec_lw(DisasContext *dc)
508 TCGv t0;
510 LOG_DIS("lw r%d, (r%d+%d)\n", dc->r1, dc->r0, sign_extend(dc->imm16, 16));
512 t0 = tcg_temp_new();
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);
515 tcg_temp_free(t0);
518 static void dec_modu(DisasContext *dc)
520 TCGLabel *l1;
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);
527 return;
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);
534 gen_set_label(l1);
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));
543 } else {
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);
551 return;
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));
557 } else {
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));
567 } else {
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);
575 tcg_temp_free(t0);
576 } else {
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));
586 } else {
587 if (dc->r1 == R_R0) {
588 LOG_DIS("mv r%d, r%d\n", dc->r2, dc->r0);
589 } else {
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));
597 } else {
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);
606 } else {
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)
615 switch (dc->imm5) {
616 case 2:
617 LOG_DIS("break\n");
618 tcg_gen_movi_tl(cpu_pc, dc->pc);
619 t_gen_raise_exception(dc, EXCP_BREAKPOINT);
620 break;
621 case 7:
622 LOG_DIS("scall\n");
623 tcg_gen_movi_tl(cpu_pc, dc->pc);
624 t_gen_raise_exception(dc, EXCP_SYSTEMCALL);
625 break;
626 default:
627 qemu_log_mask(LOG_GUEST_ERROR, "invalid opcode @0x%x", dc->pc);
628 t_gen_illegal_insn(dc);
629 break;
633 static void dec_rcsr(DisasContext *dc)
635 LOG_DIS("rcsr r%d, %d\n", dc->r2, dc->csr);
637 switch (dc->csr) {
638 case CSR_IE:
639 tcg_gen_mov_tl(cpu_R[dc->r2], cpu_ie);
640 break;
641 case CSR_IM:
642 gen_helper_rcsr_im(cpu_R[dc->r2], cpu_env);
643 break;
644 case CSR_IP:
645 gen_helper_rcsr_ip(cpu_R[dc->r2], cpu_env);
646 break;
647 case CSR_CC:
648 tcg_gen_mov_tl(cpu_R[dc->r2], cpu_cc);
649 break;
650 case CSR_CFG:
651 tcg_gen_mov_tl(cpu_R[dc->r2], cpu_cfg);
652 break;
653 case CSR_EBA:
654 tcg_gen_mov_tl(cpu_R[dc->r2], cpu_eba);
655 break;
656 case CSR_DC:
657 tcg_gen_mov_tl(cpu_R[dc->r2], cpu_dc);
658 break;
659 case CSR_DEBA:
660 tcg_gen_mov_tl(cpu_R[dc->r2], cpu_deba);
661 break;
662 case CSR_JTX:
663 gen_helper_rcsr_jtx(cpu_R[dc->r2], cpu_env);
664 break;
665 case CSR_JRX:
666 gen_helper_rcsr_jrx(cpu_R[dc->r2], cpu_env);
667 break;
668 case CSR_ICC:
669 case CSR_DCC:
670 case CSR_BP0:
671 case CSR_BP1:
672 case CSR_BP2:
673 case CSR_BP3:
674 case CSR_WP0:
675 case CSR_WP1:
676 case CSR_WP2:
677 case CSR_WP3:
678 qemu_log_mask(LOG_GUEST_ERROR, "invalid read access csr=%x\n", dc->csr);
679 break;
680 default:
681 qemu_log_mask(LOG_GUEST_ERROR, "read_csr: unknown csr=%x\n", dc->csr);
682 break;
686 static void dec_sb(DisasContext *dc)
688 TCGv t0;
690 LOG_DIS("sb (r%d+%d), r%d\n", dc->r0, dc->imm16, dc->r1);
692 t0 = tcg_temp_new();
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);
695 tcg_temp_free(t0);
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);
706 return;
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);
720 return;
723 tcg_gen_ext16s_tl(cpu_R[dc->r2], cpu_R[dc->r0]);
726 static void dec_sh(DisasContext *dc)
728 TCGv t0;
730 LOG_DIS("sh (r%d+%d), r%d\n", dc->r0, dc->imm16, dc->r1);
732 t0 = tcg_temp_new();
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);
735 tcg_temp_free(t0);
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);
742 } else {
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);
749 return;
752 if (dc->format == OP_FMT_RI) {
753 tcg_gen_shli_tl(cpu_R[dc->r1], cpu_R[dc->r0], dc->imm5);
754 } else {
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);
758 tcg_temp_free(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);
766 } else {
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
771 * one bit */
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);
777 return;
779 tcg_gen_sari_tl(cpu_R[dc->r1], cpu_R[dc->r0], dc->imm5);
780 } else {
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);
789 tcg_gen_br(l2);
792 gen_set_label(l1);
793 tcg_gen_sar_tl(cpu_R[dc->r2], cpu_R[dc->r0], t0);
794 gen_set_label(l2);
796 tcg_temp_free(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);
804 } else {
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);
813 return;
815 tcg_gen_shri_tl(cpu_R[dc->r1], cpu_R[dc->r0], dc->imm5);
816 } else {
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);
825 tcg_gen_br(l2);
828 gen_set_label(l1);
829 tcg_gen_shr_tl(cpu_R[dc->r2], cpu_R[dc->r0], t0);
830 gen_set_label(l2);
832 tcg_temp_free(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)
845 TCGv t0;
847 LOG_DIS("sw (r%d+%d), r%d\n", dc->r0, sign_extend(dc->imm16, 16), dc->r1);
849 t0 = tcg_temp_new();
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);
852 tcg_temp_free(t0);
855 static void dec_user(DisasContext *dc)
857 LOG_DIS("user");
859 qemu_log_mask(LOG_GUEST_ERROR, "user instruction undefined\n");
860 t_gen_illegal_insn(dc);
863 static void dec_wcsr(DisasContext *dc)
865 int no;
867 LOG_DIS("wcsr %d, r%d\n", dc->csr, dc->r1);
869 switch (dc->csr) {
870 case CSR_IE:
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;
874 break;
875 case CSR_IM:
876 /* mark as an io operation because it could cause an interrupt */
877 if (dc->tb->cflags & CF_USE_ICOUNT) {
878 gen_io_start();
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) {
883 gen_io_end();
885 dc->is_jmp = DISAS_UPDATE;
886 break;
887 case CSR_IP:
888 /* mark as an io operation because it could cause an interrupt */
889 if (dc->tb->cflags & CF_USE_ICOUNT) {
890 gen_io_start();
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) {
895 gen_io_end();
897 dc->is_jmp = DISAS_UPDATE;
898 break;
899 case CSR_ICC:
900 /* TODO */
901 break;
902 case CSR_DCC:
903 /* TODO */
904 break;
905 case CSR_EBA:
906 tcg_gen_mov_tl(cpu_eba, cpu_R[dc->r1]);
907 break;
908 case CSR_DEBA:
909 tcg_gen_mov_tl(cpu_deba, cpu_R[dc->r1]);
910 break;
911 case CSR_JTX:
912 gen_helper_wcsr_jtx(cpu_env, cpu_R[dc->r1]);
913 break;
914 case CSR_JRX:
915 gen_helper_wcsr_jrx(cpu_env, cpu_R[dc->r1]);
916 break;
917 case CSR_DC:
918 gen_helper_wcsr_dc(cpu_env, cpu_R[dc->r1]);
919 break;
920 case CSR_BP0:
921 case CSR_BP1:
922 case CSR_BP2:
923 case CSR_BP3:
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);
929 break;
931 gen_helper_wcsr_bp(cpu_env, cpu_R[dc->r1], tcg_const_i32(no));
932 break;
933 case CSR_WP0:
934 case CSR_WP1:
935 case CSR_WP2:
936 case CSR_WP3:
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);
942 break;
944 gen_helper_wcsr_wp(cpu_env, cpu_R[dc->r1], tcg_const_i32(no));
945 break;
946 case CSR_CC:
947 case CSR_CFG:
948 qemu_log_mask(LOG_GUEST_ERROR, "invalid write access csr=%x\n",
949 dc->csr);
950 break;
951 default:
952 qemu_log_mask(LOG_GUEST_ERROR, "write_csr: unknown csr=%x\n",
953 dc->csr);
954 break;
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));
963 } else {
964 if (dc->r1 == R_R0) {
965 LOG_DIS("not r%d, r%d\n", dc->r2, dc->r0);
966 } else {
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]);
975 } else {
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));
985 } else {
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));
992 } else {
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,
1009 dec_cmpne,
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,
1014 dec_cmpne
1017 static inline void decode(DisasContext *dc, uint32_t ir)
1019 dc->ir = 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;
1036 } else {
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;
1052 uint32_t pc_start;
1053 uint32_t next_page_start;
1054 int num_insns;
1055 int max_insns;
1057 pc_start = tb->pc;
1058 dc->features = cpu->features;
1059 dc->num_breakpoints = cpu->num_breakpoints;
1060 dc->num_watchpoints = cpu->num_watchpoints;
1061 dc->tb = tb;
1063 dc->is_jmp = DISAS_NEXT;
1064 dc->pc = pc_start;
1065 dc->singlestep_enabled = cs->singlestep_enabled;
1067 if (pc_start & 3) {
1068 qemu_log_mask(LOG_GUEST_ERROR,
1069 "unaligned PC=%x. Ignoring lowest bits.\n", pc_start);
1070 pc_start &= ~3;
1073 next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
1074 num_insns = 0;
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;
1083 gen_tb_start(tb);
1084 do {
1085 tcg_gen_insn_start(dc->pc);
1086 num_insns++;
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. */
1096 dc->pc += 4;
1097 break;
1100 /* Pretty disas. */
1101 LOG_DIS("%8.8x:\t", dc->pc);
1103 if (num_insns == max_insns && (tb->cflags & CF_LAST_IO)) {
1104 gen_io_start();
1107 decode(dc, cpu_ldl_code(env, dc->pc));
1108 dc->pc += 4;
1109 } while (!dc->is_jmp
1110 && !tcg_op_buf_full()
1111 && !cs->singlestep_enabled
1112 && !singlestep
1113 && (dc->pc < next_page_start)
1114 && num_insns < max_insns);
1116 if (tb->cflags & CF_LAST_IO) {
1117 gen_io_end();
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);
1125 } else {
1126 switch (dc->is_jmp) {
1127 case DISAS_NEXT:
1128 gen_goto_tb(dc, 1, dc->pc);
1129 break;
1130 default:
1131 case DISAS_JUMP:
1132 case DISAS_UPDATE:
1133 /* indicate that the hash table must be used
1134 to find the next TB */
1135 tcg_gen_exit_tb(0);
1136 break;
1137 case DISAS_TB_JUMP:
1138 /* nothing more to generate */
1139 break;
1143 gen_tb_end(tb, num_insns);
1145 tb->size = dc->pc - pc_start;
1146 tb->icount = num_insns;
1148 #ifdef DEBUG_DISAS
1149 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
1150 && qemu_log_in_addr_range(pc_start)) {
1151 qemu_log("\n");
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());
1156 #endif
1159 void lm32_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
1160 int flags)
1162 LM32CPU *cpu = LM32_CPU(cs);
1163 CPULM32State *env = &cpu->env;
1164 int i;
1166 if (!env || !f) {
1167 return;
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",
1174 env->ie,
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",
1181 env->eba,
1182 env->deba);
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,
1194 target_ulong *data)
1196 env->pc = data[0];
1199 void lm32_translate_init(void)
1201 int i;
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]),
1209 regnames[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]),
1215 regnames[32+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]),
1221 regnames[36+i]);
1224 cpu_pc = tcg_global_mem_new(cpu_env,
1225 offsetof(CPULM32State, pc),
1226 "pc");
1227 cpu_ie = tcg_global_mem_new(cpu_env,
1228 offsetof(CPULM32State, ie),
1229 "ie");
1230 cpu_icc = tcg_global_mem_new(cpu_env,
1231 offsetof(CPULM32State, icc),
1232 "icc");
1233 cpu_dcc = tcg_global_mem_new(cpu_env,
1234 offsetof(CPULM32State, dcc),
1235 "dcc");
1236 cpu_cc = tcg_global_mem_new(cpu_env,
1237 offsetof(CPULM32State, cc),
1238 "cc");
1239 cpu_cfg = tcg_global_mem_new(cpu_env,
1240 offsetof(CPULM32State, cfg),
1241 "cfg");
1242 cpu_eba = tcg_global_mem_new(cpu_env,
1243 offsetof(CPULM32State, eba),
1244 "eba");
1245 cpu_dc = tcg_global_mem_new(cpu_env,
1246 offsetof(CPULM32State, dc),
1247 "dc");
1248 cpu_deba = tcg_global_mem_new(cpu_env,
1249 offsetof(CPULM32State, deba),
1250 "deba");