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/>.
31 #include "qemu-common.h"
33 #include "hw/lm32_pic.h"
40 # define LOG_DIS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
42 # define LOG_DIS(...) do { } while (0)
45 #define EXTRACT_FIELD(src, start, end) \
46 (((src) >> start) & ((1 << (end - start + 1)) - 1))
50 static TCGv_ptr cpu_env
;
51 static TCGv cpu_R
[32];
61 static TCGv cpu_bp
[4];
62 static TCGv cpu_wp
[4];
64 #include "gen-icount.h"
73 /* This is the state at translation time. */
74 typedef struct DisasContext
{
82 uint8_t r0
, r1
, r2
, csr
;
87 unsigned int delayed_branch
;
88 unsigned int tb_flags
, synced_flags
; /* tb dependent flags. */
92 struct TranslationBlock
*tb
;
93 int singlestep_enabled
;
96 static const char *regnames
[] = {
97 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
98 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
99 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
100 "r24", "r25", "r26/gp", "r27/fp", "r28/sp", "r29/ra",
101 "r30/ea", "r31/ba", "bp0", "bp1", "bp2", "bp3", "wp0",
105 static inline int zero_extend(unsigned int val
, int width
)
107 return val
& ((1 << width
) - 1);
110 static inline int sign_extend(unsigned int val
, int width
)
123 static inline void t_gen_raise_exception(DisasContext
*dc
, uint32_t index
)
125 TCGv_i32 tmp
= tcg_const_i32(index
);
127 gen_helper_raise_exception(tmp
);
128 tcg_temp_free_i32(tmp
);
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((tcg_target_long
)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);
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;
336 if (dc
->format
== OP_FMT_RI
) {
337 tcg_gen_setcondi_tl(cond
, cpu_R
[rX
], cpu_R
[rY
],
338 sign_extend(dc
->imm16
, 16));
340 tcg_gen_setcond_tl(cond
, cpu_R
[rX
], cpu_R
[rY
], cpu_R
[rZ
]);
344 static void dec_cmpe(DisasContext
*dc
)
346 if (dc
->format
== OP_FMT_RI
) {
347 LOG_DIS("cmpei r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
348 sign_extend(dc
->imm16
, 16));
350 LOG_DIS("cmpe r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
353 gen_compare(dc
, TCG_COND_EQ
);
356 static void dec_cmpg(DisasContext
*dc
)
358 if (dc
->format
== OP_FMT_RI
) {
359 LOG_DIS("cmpgi r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
360 sign_extend(dc
->imm16
, 16));
362 LOG_DIS("cmpg r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
365 gen_compare(dc
, TCG_COND_GT
);
368 static void dec_cmpge(DisasContext
*dc
)
370 if (dc
->format
== OP_FMT_RI
) {
371 LOG_DIS("cmpgei r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
372 sign_extend(dc
->imm16
, 16));
374 LOG_DIS("cmpge r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
377 gen_compare(dc
, TCG_COND_GE
);
380 static void dec_cmpgeu(DisasContext
*dc
)
382 if (dc
->format
== OP_FMT_RI
) {
383 LOG_DIS("cmpgeui r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
384 sign_extend(dc
->imm16
, 16));
386 LOG_DIS("cmpgeu r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
389 gen_compare(dc
, TCG_COND_GEU
);
392 static void dec_cmpgu(DisasContext
*dc
)
394 if (dc
->format
== OP_FMT_RI
) {
395 LOG_DIS("cmpgui r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
396 sign_extend(dc
->imm16
, 16));
398 LOG_DIS("cmpgu r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
401 gen_compare(dc
, TCG_COND_GTU
);
404 static void dec_cmpne(DisasContext
*dc
)
406 if (dc
->format
== OP_FMT_RI
) {
407 LOG_DIS("cmpnei r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
408 sign_extend(dc
->imm16
, 16));
410 LOG_DIS("cmpne r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
413 gen_compare(dc
, TCG_COND_NE
);
416 static void dec_divu(DisasContext
*dc
)
420 LOG_DIS("divu r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
422 if (!(dc
->env
->features
& LM32_FEATURE_DIVIDE
)) {
423 cpu_abort(dc
->env
, "hardware divider is not available\n");
426 l1
= gen_new_label();
427 tcg_gen_brcondi_tl(TCG_COND_NE
, cpu_R
[dc
->r1
], 0, l1
);
428 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
429 t_gen_raise_exception(dc
, EXCP_DIVIDE_BY_ZERO
);
431 tcg_gen_divu_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
434 static void dec_lb(DisasContext
*dc
)
438 LOG_DIS("lb r%d, (r%d+%d)\n", dc
->r1
, dc
->r0
, dc
->imm16
);
441 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
442 tcg_gen_qemu_ld8s(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
446 static void dec_lbu(DisasContext
*dc
)
450 LOG_DIS("lbu 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_ld8u(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
458 static void dec_lh(DisasContext
*dc
)
462 LOG_DIS("lh 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_ld16s(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
470 static void dec_lhu(DisasContext
*dc
)
474 LOG_DIS("lhu 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_ld16u(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
482 static void dec_lw(DisasContext
*dc
)
486 LOG_DIS("lw r%d, (r%d+%d)\n", dc
->r1
, dc
->r0
, sign_extend(dc
->imm16
, 16));
489 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
490 tcg_gen_qemu_ld32s(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
494 static void dec_modu(DisasContext
*dc
)
498 LOG_DIS("modu r%d, r%d, %d\n", dc
->r2
, dc
->r0
, dc
->r1
);
500 if (!(dc
->env
->features
& LM32_FEATURE_DIVIDE
)) {
501 cpu_abort(dc
->env
, "hardware divider is not available\n");
504 l1
= gen_new_label();
505 tcg_gen_brcondi_tl(TCG_COND_NE
, cpu_R
[dc
->r1
], 0, l1
);
506 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
507 t_gen_raise_exception(dc
, EXCP_DIVIDE_BY_ZERO
);
509 tcg_gen_remu_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
512 static void dec_mul(DisasContext
*dc
)
514 if (dc
->format
== OP_FMT_RI
) {
515 LOG_DIS("muli r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
516 sign_extend(dc
->imm16
, 16));
518 LOG_DIS("mul r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
521 if (!(dc
->env
->features
& LM32_FEATURE_MULTIPLY
)) {
522 cpu_abort(dc
->env
, "hardware multiplier is not available\n");
525 if (dc
->format
== OP_FMT_RI
) {
526 tcg_gen_muli_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
],
527 sign_extend(dc
->imm16
, 16));
529 tcg_gen_mul_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
533 static void dec_nor(DisasContext
*dc
)
535 if (dc
->format
== OP_FMT_RI
) {
536 LOG_DIS("nori r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
537 zero_extend(dc
->imm16
, 16));
539 LOG_DIS("nor r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
542 if (dc
->format
== OP_FMT_RI
) {
543 TCGv t0
= tcg_temp_new();
544 tcg_gen_movi_tl(t0
, zero_extend(dc
->imm16
, 16));
545 tcg_gen_nor_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], t0
);
548 tcg_gen_nor_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
552 static void dec_or(DisasContext
*dc
)
554 if (dc
->format
== OP_FMT_RI
) {
555 LOG_DIS("ori r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
556 zero_extend(dc
->imm16
, 16));
558 if (dc
->r1
== R_R0
) {
559 LOG_DIS("mv r%d, r%d\n", dc
->r2
, dc
->r0
);
561 LOG_DIS("or r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
565 if (dc
->format
== OP_FMT_RI
) {
566 tcg_gen_ori_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
],
567 zero_extend(dc
->imm16
, 16));
569 tcg_gen_or_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
573 static void dec_orhi(DisasContext
*dc
)
575 if (dc
->r0
== R_R0
) {
576 LOG_DIS("mvhi r%d, %d\n", dc
->r1
, dc
->imm16
);
578 LOG_DIS("orhi r%d, r%d, %d\n", dc
->r1
, dc
->r0
, dc
->imm16
);
581 tcg_gen_ori_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], (dc
->imm16
<< 16));
584 static void dec_scall(DisasContext
*dc
)
588 } else if (dc
->imm5
== 2) {
591 cpu_abort(dc
->env
, "invalid opcode\n");
595 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
596 t_gen_raise_exception(dc
, EXCP_SYSTEMCALL
);
598 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
599 t_gen_raise_exception(dc
, EXCP_BREAKPOINT
);
603 static void dec_rcsr(DisasContext
*dc
)
605 LOG_DIS("rcsr r%d, %d\n", dc
->r2
, dc
->csr
);
609 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_ie
);
612 gen_helper_rcsr_im(cpu_R
[dc
->r2
]);
615 gen_helper_rcsr_ip(cpu_R
[dc
->r2
]);
618 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_cc
);
621 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_cfg
);
624 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_eba
);
627 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_dc
);
630 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_deba
);
633 gen_helper_rcsr_jtx(cpu_R
[dc
->r2
]);
636 gen_helper_rcsr_jrx(cpu_R
[dc
->r2
]);
648 cpu_abort(dc
->env
, "invalid read access csr=%x\n", dc
->csr
);
651 cpu_abort(dc
->env
, "read_csr: unknown csr=%x\n", dc
->csr
);
656 static void dec_sb(DisasContext
*dc
)
660 LOG_DIS("sb (r%d+%d), r%d\n", dc
->r0
, dc
->imm16
, dc
->r1
);
663 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
664 tcg_gen_qemu_st8(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
668 static void dec_sextb(DisasContext
*dc
)
670 LOG_DIS("sextb r%d, r%d\n", dc
->r2
, dc
->r0
);
672 if (!(dc
->env
->features
& LM32_FEATURE_SIGN_EXTEND
)) {
673 cpu_abort(dc
->env
, "hardware sign extender is not available\n");
676 tcg_gen_ext8s_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
]);
679 static void dec_sexth(DisasContext
*dc
)
681 LOG_DIS("sexth r%d, r%d\n", dc
->r2
, dc
->r0
);
683 if (!(dc
->env
->features
& LM32_FEATURE_SIGN_EXTEND
)) {
684 cpu_abort(dc
->env
, "hardware sign extender is not available\n");
687 tcg_gen_ext16s_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
]);
690 static void dec_sh(DisasContext
*dc
)
694 LOG_DIS("sh (r%d+%d), r%d\n", dc
->r0
, dc
->imm16
, dc
->r1
);
697 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
698 tcg_gen_qemu_st16(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
702 static void dec_sl(DisasContext
*dc
)
704 if (dc
->format
== OP_FMT_RI
) {
705 LOG_DIS("sli r%d, r%d, %d\n", dc
->r1
, dc
->r0
, dc
->imm5
);
707 LOG_DIS("sl r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
710 if (!(dc
->env
->features
& LM32_FEATURE_SHIFT
)) {
711 cpu_abort(dc
->env
, "hardware shifter is not available\n");
714 if (dc
->format
== OP_FMT_RI
) {
715 tcg_gen_shli_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], dc
->imm5
);
717 TCGv t0
= tcg_temp_new();
718 tcg_gen_andi_tl(t0
, cpu_R
[dc
->r1
], 0x1f);
719 tcg_gen_shl_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], t0
);
724 static void dec_sr(DisasContext
*dc
)
726 if (dc
->format
== OP_FMT_RI
) {
727 LOG_DIS("sri r%d, r%d, %d\n", dc
->r1
, dc
->r0
, dc
->imm5
);
729 LOG_DIS("sr r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
732 if (!(dc
->env
->features
& LM32_FEATURE_SHIFT
)) {
733 if (dc
->format
== OP_FMT_RI
) {
734 /* TODO: check r1 == 1 during runtime */
737 cpu_abort(dc
->env
, "hardware shifter is not available\n");
742 if (dc
->format
== OP_FMT_RI
) {
743 tcg_gen_sari_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], dc
->imm5
);
745 TCGv t0
= tcg_temp_new();
746 tcg_gen_andi_tl(t0
, cpu_R
[dc
->r1
], 0x1f);
747 tcg_gen_sar_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], t0
);
752 static void dec_sru(DisasContext
*dc
)
754 if (dc
->format
== OP_FMT_RI
) {
755 LOG_DIS("srui r%d, r%d, %d\n", dc
->r1
, dc
->r0
, dc
->imm5
);
757 LOG_DIS("sru r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
760 if (!(dc
->env
->features
& LM32_FEATURE_SHIFT
)) {
761 if (dc
->format
== OP_FMT_RI
) {
762 /* TODO: check r1 == 1 during runtime */
765 cpu_abort(dc
->env
, "hardware shifter is not available\n");
770 if (dc
->format
== OP_FMT_RI
) {
771 tcg_gen_shri_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], dc
->imm5
);
773 TCGv t0
= tcg_temp_new();
774 tcg_gen_andi_tl(t0
, cpu_R
[dc
->r1
], 0x1f);
775 tcg_gen_shr_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], t0
);
780 static void dec_sub(DisasContext
*dc
)
782 LOG_DIS("sub r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
784 tcg_gen_sub_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
787 static void dec_sw(DisasContext
*dc
)
791 LOG_DIS("sw (r%d+%d), r%d\n", dc
->r0
, sign_extend(dc
->imm16
, 16), dc
->r1
);
794 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
795 tcg_gen_qemu_st32(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
799 static void dec_user(DisasContext
*dc
)
803 cpu_abort(dc
->env
, "user insn undefined\n");
806 static void dec_wcsr(DisasContext
*dc
)
810 LOG_DIS("wcsr r%d, %d\n", dc
->r1
, dc
->csr
);
814 tcg_gen_mov_tl(cpu_ie
, cpu_R
[dc
->r1
]);
815 tcg_gen_movi_tl(cpu_pc
, dc
->pc
+ 4);
816 dc
->is_jmp
= DISAS_UPDATE
;
819 /* mark as an io operation because it could cause an interrupt */
823 gen_helper_wcsr_im(cpu_R
[dc
->r1
]);
824 tcg_gen_movi_tl(cpu_pc
, dc
->pc
+ 4);
828 dc
->is_jmp
= DISAS_UPDATE
;
831 /* mark as an io operation because it could cause an interrupt */
835 gen_helper_wcsr_ip(cpu_R
[dc
->r1
]);
836 tcg_gen_movi_tl(cpu_pc
, dc
->pc
+ 4);
840 dc
->is_jmp
= DISAS_UPDATE
;
849 tcg_gen_mov_tl(cpu_eba
, cpu_R
[dc
->r1
]);
852 tcg_gen_mov_tl(cpu_deba
, cpu_R
[dc
->r1
]);
855 gen_helper_wcsr_jtx(cpu_R
[dc
->r1
]);
858 gen_helper_wcsr_jrx(cpu_R
[dc
->r1
]);
861 tcg_gen_mov_tl(cpu_dc
, cpu_R
[dc
->r1
]);
867 no
= dc
->csr
- CSR_BP0
;
868 if (dc
->env
->num_bps
<= no
) {
869 cpu_abort(dc
->env
, "breakpoint #%i is not available\n", no
);
871 tcg_gen_mov_tl(cpu_bp
[no
], cpu_R
[dc
->r1
]);
877 no
= dc
->csr
- CSR_WP0
;
878 if (dc
->env
->num_wps
<= no
) {
879 cpu_abort(dc
->env
, "watchpoint #%i is not available\n", no
);
881 tcg_gen_mov_tl(cpu_wp
[no
], cpu_R
[dc
->r1
]);
885 cpu_abort(dc
->env
, "invalid write access csr=%x\n", dc
->csr
);
888 cpu_abort(dc
->env
, "write_csr unknown csr=%x\n", dc
->csr
);
893 static void dec_xnor(DisasContext
*dc
)
895 if (dc
->format
== OP_FMT_RI
) {
896 LOG_DIS("xnori r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
897 zero_extend(dc
->imm16
, 16));
899 if (dc
->r1
== R_R0
) {
900 LOG_DIS("not r%d, r%d\n", dc
->r2
, dc
->r0
);
902 LOG_DIS("xnor r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
906 if (dc
->format
== OP_FMT_RI
) {
907 tcg_gen_xori_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
],
908 zero_extend(dc
->imm16
, 16));
909 tcg_gen_not_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r1
]);
911 tcg_gen_eqv_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
915 static void dec_xor(DisasContext
*dc
)
917 if (dc
->format
== OP_FMT_RI
) {
918 LOG_DIS("xori r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
919 zero_extend(dc
->imm16
, 16));
921 LOG_DIS("xor r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
924 if (dc
->format
== OP_FMT_RI
) {
925 tcg_gen_xori_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
],
926 zero_extend(dc
->imm16
, 16));
928 tcg_gen_xor_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
932 static void dec_ill(DisasContext
*dc
)
934 cpu_abort(dc
->env
, "unknown opcode 0x%02x\n", dc
->opcode
);
937 typedef void (*DecoderInfo
)(DisasContext
*dc
);
938 static const DecoderInfo decinfo
[] = {
939 dec_sru
, dec_nor
, dec_mul
, dec_sh
, dec_lb
, dec_sr
, dec_xor
, dec_lh
,
940 dec_and
, dec_xnor
, dec_lw
, dec_lhu
, dec_sb
, dec_add
, dec_or
, dec_sl
,
941 dec_lbu
, dec_be
, dec_bg
, dec_bge
, dec_bgeu
, dec_bgu
, dec_sw
, dec_bne
,
942 dec_andhi
, dec_cmpe
, dec_cmpg
, dec_cmpge
, dec_cmpgeu
, dec_cmpgu
, dec_orhi
,
944 dec_sru
, dec_nor
, dec_mul
, dec_divu
, dec_rcsr
, dec_sr
, dec_xor
, dec_ill
,
945 dec_and
, dec_xnor
, dec_ill
, dec_scall
, dec_sextb
, dec_add
, dec_or
, dec_sl
,
946 dec_b
, dec_modu
, dec_sub
, dec_user
, dec_wcsr
, dec_ill
, dec_call
, dec_sexth
,
947 dec_bi
, dec_cmpe
, dec_cmpg
, dec_cmpge
, dec_cmpgeu
, dec_cmpgu
, dec_calli
,
951 static inline void decode(DisasContext
*dc
)
955 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP
))) {
956 tcg_gen_debug_insn_start(dc
->pc
);
959 dc
->ir
= ir
= ldl_code(dc
->pc
);
960 LOG_DIS("%8.8x\t", dc
->ir
);
962 /* try guessing 'empty' instruction memory, although it may be a valid
963 * instruction sequence (eg. srui r0, r0, 0) */
967 LOG_DIS("nr_nops=%d\t", dc
->nr_nops
);
969 if (dc
->nr_nops
> 4) {
970 cpu_abort(dc
->env
, "fetching nop sequence\n");
974 dc
->opcode
= EXTRACT_FIELD(ir
, 26, 31);
976 dc
->imm5
= EXTRACT_FIELD(ir
, 0, 4);
977 dc
->imm16
= EXTRACT_FIELD(ir
, 0, 15);
978 dc
->imm26
= EXTRACT_FIELD(ir
, 0, 25);
980 dc
->csr
= EXTRACT_FIELD(ir
, 21, 25);
981 dc
->r0
= EXTRACT_FIELD(ir
, 21, 25);
982 dc
->r1
= EXTRACT_FIELD(ir
, 16, 20);
983 dc
->r2
= EXTRACT_FIELD(ir
, 11, 15);
985 /* bit 31 seems to indicate insn type. */
986 if (ir
& (1 << 31)) {
987 dc
->format
= OP_FMT_RR
;
989 dc
->format
= OP_FMT_RI
;
992 assert(ARRAY_SIZE(decinfo
) == 64);
993 assert(dc
->opcode
< 64);
995 decinfo
[dc
->opcode
](dc
);
998 static void check_breakpoint(CPUState
*env
, DisasContext
*dc
)
1002 if (unlikely(!QTAILQ_EMPTY(&env
->breakpoints
))) {
1003 QTAILQ_FOREACH(bp
, &env
->breakpoints
, entry
) {
1004 if (bp
->pc
== dc
->pc
) {
1005 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
1006 t_gen_raise_exception(dc
, EXCP_DEBUG
);
1007 dc
->is_jmp
= DISAS_UPDATE
;
1013 /* generate intermediate code for basic block 'tb'. */
1014 static void gen_intermediate_code_internal(CPUState
*env
,
1015 TranslationBlock
*tb
, int search_pc
)
1017 struct DisasContext ctx
, *dc
= &ctx
;
1018 uint16_t *gen_opc_end
;
1021 uint32_t next_page_start
;
1025 qemu_log_try_set_file(stderr
);
1031 gen_opc_end
= gen_opc_buf
+ OPC_MAX_SIZE
;
1033 dc
->is_jmp
= DISAS_NEXT
;
1035 dc
->singlestep_enabled
= env
->singlestep_enabled
;
1039 cpu_abort(env
, "LM32: unaligned PC=%x\n", pc_start
);
1042 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)) {
1043 qemu_log("-----------------------------------------\n");
1044 log_cpu_state(env
, 0);
1047 next_page_start
= (pc_start
& TARGET_PAGE_MASK
) + TARGET_PAGE_SIZE
;
1050 max_insns
= tb
->cflags
& CF_COUNT_MASK
;
1051 if (max_insns
== 0) {
1052 max_insns
= CF_COUNT_MASK
;
1057 check_breakpoint(env
, dc
);
1060 j
= gen_opc_ptr
- gen_opc_buf
;
1064 gen_opc_instr_start
[lj
++] = 0;
1067 gen_opc_pc
[lj
] = dc
->pc
;
1068 gen_opc_instr_start
[lj
] = 1;
1069 gen_opc_icount
[lj
] = num_insns
;
1073 LOG_DIS("%8.8x:\t", dc
->pc
);
1075 if (num_insns
+ 1 == max_insns
&& (tb
->cflags
& CF_LAST_IO
)) {
1083 } while (!dc
->is_jmp
1084 && gen_opc_ptr
< gen_opc_end
1085 && !env
->singlestep_enabled
1087 && (dc
->pc
< next_page_start
)
1088 && num_insns
< max_insns
);
1090 if (tb
->cflags
& CF_LAST_IO
) {
1094 if (unlikely(env
->singlestep_enabled
)) {
1095 if (dc
->is_jmp
== DISAS_NEXT
) {
1096 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
1098 t_gen_raise_exception(dc
, EXCP_DEBUG
);
1100 switch (dc
->is_jmp
) {
1102 gen_goto_tb(dc
, 1, dc
->pc
);
1107 /* indicate that the hash table must be used
1108 to find the next TB */
1112 /* nothing more to generate */
1117 gen_icount_end(tb
, num_insns
);
1118 *gen_opc_ptr
= INDEX_op_end
;
1120 j
= gen_opc_ptr
- gen_opc_buf
;
1123 gen_opc_instr_start
[lj
++] = 0;
1126 tb
->size
= dc
->pc
- pc_start
;
1127 tb
->icount
= num_insns
;
1131 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)) {
1133 log_target_disas(pc_start
, dc
->pc
- pc_start
, 0);
1134 qemu_log("\nisize=%d osize=%td\n",
1135 dc
->pc
- pc_start
, gen_opc_ptr
- gen_opc_buf
);
1140 void gen_intermediate_code(CPUState
*env
, struct TranslationBlock
*tb
)
1142 gen_intermediate_code_internal(env
, tb
, 0);
1145 void gen_intermediate_code_pc(CPUState
*env
, struct TranslationBlock
*tb
)
1147 gen_intermediate_code_internal(env
, tb
, 1);
1150 void cpu_dump_state(CPUState
*env
, FILE *f
, fprintf_function cpu_fprintf
,
1159 cpu_fprintf(f
, "IN: PC=%x %s\n",
1160 env
->pc
, lookup_symbol(env
->pc
));
1162 cpu_fprintf(f
, "ie=%8.8x (IE=%x EIE=%x BIE=%x) im=%8.8x ip=%8.8x\n",
1164 (env
->ie
& IE_IE
) ? 1 : 0,
1165 (env
->ie
& IE_EIE
) ? 1 : 0,
1166 (env
->ie
& IE_BIE
) ? 1 : 0,
1167 lm32_pic_get_im(env
->pic_state
),
1168 lm32_pic_get_ip(env
->pic_state
));
1169 cpu_fprintf(f
, "eba=%8.8x deba=%8.8x\n",
1173 for (i
= 0; i
< 32; i
++) {
1174 cpu_fprintf(f
, "r%2.2d=%8.8x ", i
, env
->regs
[i
]);
1175 if ((i
+ 1) % 4 == 0) {
1176 cpu_fprintf(f
, "\n");
1179 cpu_fprintf(f
, "\n\n");
1182 void restore_state_to_opc(CPUState
*env
, TranslationBlock
*tb
, int pc_pos
)
1184 env
->pc
= gen_opc_pc
[pc_pos
];
1187 void lm32_translate_init(void)
1191 cpu_env
= tcg_global_reg_new_ptr(TCG_AREG0
, "env");
1193 for (i
= 0; i
< ARRAY_SIZE(cpu_R
); i
++) {
1194 cpu_R
[i
] = tcg_global_mem_new(TCG_AREG0
,
1195 offsetof(CPUState
, regs
[i
]),
1199 for (i
= 0; i
< ARRAY_SIZE(cpu_bp
); i
++) {
1200 cpu_bp
[i
] = tcg_global_mem_new(TCG_AREG0
,
1201 offsetof(CPUState
, bp
[i
]),
1205 for (i
= 0; i
< ARRAY_SIZE(cpu_wp
); i
++) {
1206 cpu_wp
[i
] = tcg_global_mem_new(TCG_AREG0
,
1207 offsetof(CPUState
, wp
[i
]),
1211 cpu_pc
= tcg_global_mem_new(TCG_AREG0
,
1212 offsetof(CPUState
, pc
),
1214 cpu_ie
= tcg_global_mem_new(TCG_AREG0
,
1215 offsetof(CPUState
, ie
),
1217 cpu_icc
= tcg_global_mem_new(TCG_AREG0
,
1218 offsetof(CPUState
, icc
),
1220 cpu_dcc
= tcg_global_mem_new(TCG_AREG0
,
1221 offsetof(CPUState
, dcc
),
1223 cpu_cc
= tcg_global_mem_new(TCG_AREG0
,
1224 offsetof(CPUState
, cc
),
1226 cpu_cfg
= tcg_global_mem_new(TCG_AREG0
,
1227 offsetof(CPUState
, cfg
),
1229 cpu_eba
= tcg_global_mem_new(TCG_AREG0
,
1230 offsetof(CPUState
, eba
),
1232 cpu_dc
= tcg_global_mem_new(TCG_AREG0
,
1233 offsetof(CPUState
, dc
),
1235 cpu_deba
= tcg_global_mem_new(TCG_AREG0
,
1236 offsetof(CPUState
, deba
),