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/>.
32 #include "qemu-common.h"
34 #include "hw/lm32_pic.h"
41 # define LOG_DIS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
43 # define LOG_DIS(...) do { } while (0)
46 #define EXTRACT_FIELD(src, start, end) \
47 (((src) >> start) & ((1 << (end - start + 1)) - 1))
51 static TCGv_ptr cpu_env
;
52 static TCGv cpu_R
[32];
62 static TCGv cpu_bp
[4];
63 static TCGv cpu_wp
[4];
65 #include "gen-icount.h"
74 /* This is the state at translation time. */
75 typedef struct DisasContext
{
83 uint8_t r0
, r1
, r2
, csr
;
88 unsigned int delayed_branch
;
89 unsigned int tb_flags
, synced_flags
; /* tb dependent flags. */
93 struct TranslationBlock
*tb
;
94 int singlestep_enabled
;
97 static const char *regnames
[] = {
98 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
99 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
100 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
101 "r24", "r25", "r26/gp", "r27/fp", "r28/sp", "r29/ra",
102 "r30/ea", "r31/ba", "bp0", "bp1", "bp2", "bp3", "wp0",
106 static inline int zero_extend(unsigned int val
, int width
)
108 return val
& ((1 << width
) - 1);
111 static inline int sign_extend(unsigned int val
, int width
)
124 static inline void t_gen_raise_exception(DisasContext
*dc
, uint32_t index
)
126 TCGv_i32 tmp
= tcg_const_i32(index
);
128 gen_helper_raise_exception(tmp
);
129 tcg_temp_free_i32(tmp
);
132 static void gen_goto_tb(DisasContext
*dc
, int n
, target_ulong dest
)
134 TranslationBlock
*tb
;
137 if ((tb
->pc
& TARGET_PAGE_MASK
) == (dest
& TARGET_PAGE_MASK
) &&
138 likely(!dc
->singlestep_enabled
)) {
140 tcg_gen_movi_tl(cpu_pc
, dest
);
141 tcg_gen_exit_tb((tcg_target_long
)tb
+ n
);
143 tcg_gen_movi_tl(cpu_pc
, dest
);
144 if (dc
->singlestep_enabled
) {
145 t_gen_raise_exception(dc
, EXCP_DEBUG
);
151 static void dec_add(DisasContext
*dc
)
153 if (dc
->format
== OP_FMT_RI
) {
154 if (dc
->r0
== R_R0
) {
155 if (dc
->r1
== R_R0
&& dc
->imm16
== 0) {
158 LOG_DIS("mvi r%d, %d\n", dc
->r1
, sign_extend(dc
->imm16
, 16));
161 LOG_DIS("addi r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
162 sign_extend(dc
->imm16
, 16));
165 LOG_DIS("add r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
168 if (dc
->format
== OP_FMT_RI
) {
169 tcg_gen_addi_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
],
170 sign_extend(dc
->imm16
, 16));
172 tcg_gen_add_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
176 static void dec_and(DisasContext
*dc
)
178 if (dc
->format
== OP_FMT_RI
) {
179 LOG_DIS("andi r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
180 zero_extend(dc
->imm16
, 16));
182 LOG_DIS("and r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
185 if (dc
->format
== OP_FMT_RI
) {
186 tcg_gen_andi_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
],
187 zero_extend(dc
->imm16
, 16));
189 if (dc
->r0
== 0 && dc
->r1
== 0 && dc
->r2
== 0) {
190 tcg_gen_movi_tl(cpu_pc
, dc
->pc
+ 4);
193 tcg_gen_and_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
198 static void dec_andhi(DisasContext
*dc
)
200 LOG_DIS("andhi r%d, r%d, %d\n", dc
->r2
, dc
->r0
, dc
->imm16
);
202 tcg_gen_andi_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], (dc
->imm16
<< 16));
205 static void dec_b(DisasContext
*dc
)
207 if (dc
->r0
== R_RA
) {
209 } else if (dc
->r0
== R_EA
) {
211 } else if (dc
->r0
== R_BA
) {
214 LOG_DIS("b r%d\n", dc
->r0
);
217 /* restore IE.IE in case of an eret */
218 if (dc
->r0
== R_EA
) {
219 TCGv t0
= tcg_temp_new();
220 int l1
= gen_new_label();
221 tcg_gen_andi_tl(t0
, cpu_ie
, IE_EIE
);
222 tcg_gen_ori_tl(cpu_ie
, cpu_ie
, IE_IE
);
223 tcg_gen_brcondi_tl(TCG_COND_EQ
, t0
, IE_EIE
, l1
);
224 tcg_gen_andi_tl(cpu_ie
, cpu_ie
, ~IE_IE
);
227 } else if (dc
->r0
== R_BA
) {
228 TCGv t0
= tcg_temp_new();
229 int l1
= gen_new_label();
230 tcg_gen_andi_tl(t0
, cpu_ie
, IE_BIE
);
231 tcg_gen_ori_tl(cpu_ie
, cpu_ie
, IE_IE
);
232 tcg_gen_brcondi_tl(TCG_COND_EQ
, t0
, IE_BIE
, l1
);
233 tcg_gen_andi_tl(cpu_ie
, cpu_ie
, ~IE_IE
);
237 tcg_gen_mov_tl(cpu_pc
, cpu_R
[dc
->r0
]);
239 dc
->is_jmp
= DISAS_JUMP
;
242 static void dec_bi(DisasContext
*dc
)
244 LOG_DIS("bi %d\n", sign_extend(dc
->imm26
<< 2, 26));
246 gen_goto_tb(dc
, 0, dc
->pc
+ (sign_extend(dc
->imm26
<< 2, 26)));
248 dc
->is_jmp
= DISAS_TB_JUMP
;
251 static inline void gen_cond_branch(DisasContext
*dc
, int cond
)
255 l1
= gen_new_label();
256 tcg_gen_brcond_tl(cond
, cpu_R
[dc
->r0
], cpu_R
[dc
->r1
], l1
);
257 gen_goto_tb(dc
, 0, dc
->pc
+ 4);
259 gen_goto_tb(dc
, 1, dc
->pc
+ (sign_extend(dc
->imm16
<< 2, 16)));
260 dc
->is_jmp
= DISAS_TB_JUMP
;
263 static void dec_be(DisasContext
*dc
)
265 LOG_DIS("be r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
266 sign_extend(dc
->imm16
, 16) * 4);
268 gen_cond_branch(dc
, TCG_COND_EQ
);
271 static void dec_bg(DisasContext
*dc
)
273 LOG_DIS("bg r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
274 sign_extend(dc
->imm16
, 16 * 4));
276 gen_cond_branch(dc
, TCG_COND_GT
);
279 static void dec_bge(DisasContext
*dc
)
281 LOG_DIS("bge r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
282 sign_extend(dc
->imm16
, 16) * 4);
284 gen_cond_branch(dc
, TCG_COND_GE
);
287 static void dec_bgeu(DisasContext
*dc
)
289 LOG_DIS("bgeu r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
290 sign_extend(dc
->imm16
, 16) * 4);
292 gen_cond_branch(dc
, TCG_COND_GEU
);
295 static void dec_bgu(DisasContext
*dc
)
297 LOG_DIS("bgu r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
298 sign_extend(dc
->imm16
, 16) * 4);
300 gen_cond_branch(dc
, TCG_COND_GTU
);
303 static void dec_bne(DisasContext
*dc
)
305 LOG_DIS("bne r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
306 sign_extend(dc
->imm16
, 16) * 4);
308 gen_cond_branch(dc
, TCG_COND_NE
);
311 static void dec_call(DisasContext
*dc
)
313 LOG_DIS("call r%d\n", dc
->r0
);
315 tcg_gen_movi_tl(cpu_R
[R_RA
], dc
->pc
+ 4);
316 tcg_gen_mov_tl(cpu_pc
, cpu_R
[dc
->r0
]);
318 dc
->is_jmp
= DISAS_JUMP
;
321 static void dec_calli(DisasContext
*dc
)
323 LOG_DIS("calli %d\n", sign_extend(dc
->imm26
, 26) * 4);
325 tcg_gen_movi_tl(cpu_R
[R_RA
], dc
->pc
+ 4);
326 gen_goto_tb(dc
, 0, dc
->pc
+ (sign_extend(dc
->imm26
<< 2, 26)));
328 dc
->is_jmp
= DISAS_TB_JUMP
;
331 static inline void gen_compare(DisasContext
*dc
, int cond
)
333 int rX
= (dc
->format
== OP_FMT_RR
) ? dc
->r2
: dc
->r1
;
334 int rY
= (dc
->format
== OP_FMT_RR
) ? dc
->r0
: dc
->r0
;
335 int rZ
= (dc
->format
== OP_FMT_RR
) ? dc
->r1
: -1;
337 if (dc
->format
== OP_FMT_RI
) {
338 tcg_gen_setcondi_tl(cond
, cpu_R
[rX
], cpu_R
[rY
],
339 sign_extend(dc
->imm16
, 16));
341 tcg_gen_setcond_tl(cond
, cpu_R
[rX
], cpu_R
[rY
], cpu_R
[rZ
]);
345 static void dec_cmpe(DisasContext
*dc
)
347 if (dc
->format
== OP_FMT_RI
) {
348 LOG_DIS("cmpei r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
349 sign_extend(dc
->imm16
, 16));
351 LOG_DIS("cmpe r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
354 gen_compare(dc
, TCG_COND_EQ
);
357 static void dec_cmpg(DisasContext
*dc
)
359 if (dc
->format
== OP_FMT_RI
) {
360 LOG_DIS("cmpgi r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
361 sign_extend(dc
->imm16
, 16));
363 LOG_DIS("cmpg r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
366 gen_compare(dc
, TCG_COND_GT
);
369 static void dec_cmpge(DisasContext
*dc
)
371 if (dc
->format
== OP_FMT_RI
) {
372 LOG_DIS("cmpgei r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
373 sign_extend(dc
->imm16
, 16));
375 LOG_DIS("cmpge r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
378 gen_compare(dc
, TCG_COND_GE
);
381 static void dec_cmpgeu(DisasContext
*dc
)
383 if (dc
->format
== OP_FMT_RI
) {
384 LOG_DIS("cmpgeui r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
385 sign_extend(dc
->imm16
, 16));
387 LOG_DIS("cmpgeu r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
390 gen_compare(dc
, TCG_COND_GEU
);
393 static void dec_cmpgu(DisasContext
*dc
)
395 if (dc
->format
== OP_FMT_RI
) {
396 LOG_DIS("cmpgui r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
397 sign_extend(dc
->imm16
, 16));
399 LOG_DIS("cmpgu r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
402 gen_compare(dc
, TCG_COND_GTU
);
405 static void dec_cmpne(DisasContext
*dc
)
407 if (dc
->format
== OP_FMT_RI
) {
408 LOG_DIS("cmpnei r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
409 sign_extend(dc
->imm16
, 16));
411 LOG_DIS("cmpne r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
414 gen_compare(dc
, TCG_COND_NE
);
417 static void dec_divu(DisasContext
*dc
)
421 LOG_DIS("divu r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
423 if (!(dc
->env
->features
& LM32_FEATURE_DIVIDE
)) {
424 cpu_abort(dc
->env
, "hardware divider is not available\n");
427 l1
= gen_new_label();
428 tcg_gen_brcondi_tl(TCG_COND_NE
, cpu_R
[dc
->r1
], 0, l1
);
429 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
430 t_gen_raise_exception(dc
, EXCP_DIVIDE_BY_ZERO
);
432 tcg_gen_divu_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
435 static void dec_lb(DisasContext
*dc
)
439 LOG_DIS("lb r%d, (r%d+%d)\n", dc
->r1
, dc
->r0
, dc
->imm16
);
442 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
443 tcg_gen_qemu_ld8s(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
447 static void dec_lbu(DisasContext
*dc
)
451 LOG_DIS("lbu r%d, (r%d+%d)\n", dc
->r1
, dc
->r0
, dc
->imm16
);
454 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
455 tcg_gen_qemu_ld8u(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
459 static void dec_lh(DisasContext
*dc
)
463 LOG_DIS("lh r%d, (r%d+%d)\n", dc
->r1
, dc
->r0
, dc
->imm16
);
466 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
467 tcg_gen_qemu_ld16s(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
471 static void dec_lhu(DisasContext
*dc
)
475 LOG_DIS("lhu r%d, (r%d+%d)\n", dc
->r1
, dc
->r0
, dc
->imm16
);
478 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
479 tcg_gen_qemu_ld16u(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
483 static void dec_lw(DisasContext
*dc
)
487 LOG_DIS("lw r%d, (r%d+%d)\n", dc
->r1
, dc
->r0
, sign_extend(dc
->imm16
, 16));
490 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
491 tcg_gen_qemu_ld32s(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
495 static void dec_modu(DisasContext
*dc
)
499 LOG_DIS("modu r%d, r%d, %d\n", dc
->r2
, dc
->r0
, dc
->r1
);
501 if (!(dc
->env
->features
& LM32_FEATURE_DIVIDE
)) {
502 cpu_abort(dc
->env
, "hardware divider is not available\n");
505 l1
= gen_new_label();
506 tcg_gen_brcondi_tl(TCG_COND_NE
, cpu_R
[dc
->r1
], 0, l1
);
507 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
508 t_gen_raise_exception(dc
, EXCP_DIVIDE_BY_ZERO
);
510 tcg_gen_remu_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
513 static void dec_mul(DisasContext
*dc
)
515 if (dc
->format
== OP_FMT_RI
) {
516 LOG_DIS("muli r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
517 sign_extend(dc
->imm16
, 16));
519 LOG_DIS("mul r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
522 if (!(dc
->env
->features
& LM32_FEATURE_MULTIPLY
)) {
523 cpu_abort(dc
->env
, "hardware multiplier is not available\n");
526 if (dc
->format
== OP_FMT_RI
) {
527 tcg_gen_muli_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
],
528 sign_extend(dc
->imm16
, 16));
530 tcg_gen_mul_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
534 static void dec_nor(DisasContext
*dc
)
536 if (dc
->format
== OP_FMT_RI
) {
537 LOG_DIS("nori r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
538 zero_extend(dc
->imm16
, 16));
540 LOG_DIS("nor r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
543 if (dc
->format
== OP_FMT_RI
) {
544 TCGv t0
= tcg_temp_new();
545 tcg_gen_movi_tl(t0
, zero_extend(dc
->imm16
, 16));
546 tcg_gen_nor_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], t0
);
549 tcg_gen_nor_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
553 static void dec_or(DisasContext
*dc
)
555 if (dc
->format
== OP_FMT_RI
) {
556 LOG_DIS("ori r%d, r%d, %d\n", dc
->r1
, dc
->r0
,
557 zero_extend(dc
->imm16
, 16));
559 if (dc
->r1
== R_R0
) {
560 LOG_DIS("mv r%d, r%d\n", dc
->r2
, dc
->r0
);
562 LOG_DIS("or r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
566 if (dc
->format
== OP_FMT_RI
) {
567 tcg_gen_ori_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
],
568 zero_extend(dc
->imm16
, 16));
570 tcg_gen_or_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
574 static void dec_orhi(DisasContext
*dc
)
576 if (dc
->r0
== R_R0
) {
577 LOG_DIS("mvhi r%d, %d\n", dc
->r1
, dc
->imm16
);
579 LOG_DIS("orhi r%d, r%d, %d\n", dc
->r1
, dc
->r0
, dc
->imm16
);
582 tcg_gen_ori_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], (dc
->imm16
<< 16));
585 static void dec_scall(DisasContext
*dc
)
592 } else if (dc
->imm5
== 2) {
595 cpu_abort(dc
->env
, "invalid opcode\n");
599 l1
= gen_new_label();
602 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
603 t_gen_raise_exception(dc
, EXCP_SYSTEMCALL
);
605 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
606 t_gen_raise_exception(dc
, EXCP_BREAKPOINT
);
610 static void dec_rcsr(DisasContext
*dc
)
612 LOG_DIS("rcsr r%d, %d\n", dc
->r2
, dc
->csr
);
616 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_ie
);
619 gen_helper_rcsr_im(cpu_R
[dc
->r2
]);
622 gen_helper_rcsr_ip(cpu_R
[dc
->r2
]);
625 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_cc
);
628 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_cfg
);
631 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_eba
);
634 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_dc
);
637 tcg_gen_mov_tl(cpu_R
[dc
->r2
], cpu_deba
);
640 gen_helper_rcsr_jtx(cpu_R
[dc
->r2
]);
643 gen_helper_rcsr_jrx(cpu_R
[dc
->r2
]);
655 cpu_abort(dc
->env
, "invalid read access csr=%x\n", dc
->csr
);
658 cpu_abort(dc
->env
, "read_csr: unknown csr=%x\n", dc
->csr
);
663 static void dec_sb(DisasContext
*dc
)
667 LOG_DIS("sb (r%d+%d), r%d\n", dc
->r0
, dc
->imm16
, dc
->r1
);
670 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
671 tcg_gen_qemu_st8(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
675 static void dec_sextb(DisasContext
*dc
)
677 LOG_DIS("sextb r%d, r%d\n", dc
->r2
, dc
->r0
);
679 if (!(dc
->env
->features
& LM32_FEATURE_SIGN_EXTEND
)) {
680 cpu_abort(dc
->env
, "hardware sign extender is not available\n");
683 tcg_gen_ext8s_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
]);
686 static void dec_sexth(DisasContext
*dc
)
688 LOG_DIS("sexth r%d, r%d\n", dc
->r2
, dc
->r0
);
690 if (!(dc
->env
->features
& LM32_FEATURE_SIGN_EXTEND
)) {
691 cpu_abort(dc
->env
, "hardware sign extender is not available\n");
694 tcg_gen_ext16s_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
]);
697 static void dec_sh(DisasContext
*dc
)
701 LOG_DIS("sh (r%d+%d), r%d\n", dc
->r0
, dc
->imm16
, dc
->r1
);
704 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
705 tcg_gen_qemu_st16(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
709 static void dec_sl(DisasContext
*dc
)
711 if (dc
->format
== OP_FMT_RI
) {
712 LOG_DIS("sli r%d, r%d, %d\n", dc
->r1
, dc
->r0
, dc
->imm5
);
714 LOG_DIS("sl r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
717 if (!(dc
->env
->features
& LM32_FEATURE_SHIFT
)) {
718 cpu_abort(dc
->env
, "hardware shifter is not available\n");
721 if (dc
->format
== OP_FMT_RI
) {
722 tcg_gen_shli_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], dc
->imm5
);
724 TCGv t0
= tcg_temp_new();
725 tcg_gen_andi_tl(t0
, cpu_R
[dc
->r1
], 0x1f);
726 tcg_gen_shl_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], t0
);
731 static void dec_sr(DisasContext
*dc
)
733 if (dc
->format
== OP_FMT_RI
) {
734 LOG_DIS("sri r%d, r%d, %d\n", dc
->r1
, dc
->r0
, dc
->imm5
);
736 LOG_DIS("sr r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
739 if (!(dc
->env
->features
& LM32_FEATURE_SHIFT
)) {
740 if (dc
->format
== OP_FMT_RI
) {
741 /* TODO: check r1 == 1 during runtime */
744 cpu_abort(dc
->env
, "hardware shifter is not available\n");
749 if (dc
->format
== OP_FMT_RI
) {
750 tcg_gen_sari_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], dc
->imm5
);
752 TCGv t0
= tcg_temp_new();
753 tcg_gen_andi_tl(t0
, cpu_R
[dc
->r1
], 0x1f);
754 tcg_gen_sar_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], t0
);
759 static void dec_sru(DisasContext
*dc
)
761 if (dc
->format
== OP_FMT_RI
) {
762 LOG_DIS("srui r%d, r%d, %d\n", dc
->r1
, dc
->r0
, dc
->imm5
);
764 LOG_DIS("sru r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
767 if (!(dc
->env
->features
& LM32_FEATURE_SHIFT
)) {
768 if (dc
->format
== OP_FMT_RI
) {
769 /* TODO: check r1 == 1 during runtime */
772 cpu_abort(dc
->env
, "hardware shifter is not available\n");
777 if (dc
->format
== OP_FMT_RI
) {
778 tcg_gen_shri_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
], dc
->imm5
);
780 TCGv t0
= tcg_temp_new();
781 tcg_gen_andi_tl(t0
, cpu_R
[dc
->r1
], 0x1f);
782 tcg_gen_shr_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], t0
);
787 static void dec_sub(DisasContext
*dc
)
789 LOG_DIS("sub r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
791 tcg_gen_sub_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
794 static void dec_sw(DisasContext
*dc
)
798 LOG_DIS("sw (r%d+%d), r%d\n", dc
->r0
, sign_extend(dc
->imm16
, 16), dc
->r1
);
801 tcg_gen_addi_tl(t0
, cpu_R
[dc
->r0
], sign_extend(dc
->imm16
, 16));
802 tcg_gen_qemu_st32(cpu_R
[dc
->r1
], t0
, MEM_INDEX
);
806 static void dec_user(DisasContext
*dc
)
810 cpu_abort(dc
->env
, "user insn undefined\n");
813 static void dec_wcsr(DisasContext
*dc
)
817 LOG_DIS("wcsr r%d, %d\n", dc
->r1
, dc
->csr
);
821 tcg_gen_mov_tl(cpu_ie
, cpu_R
[dc
->r1
]);
822 tcg_gen_movi_tl(cpu_pc
, dc
->pc
+ 4);
823 dc
->is_jmp
= DISAS_UPDATE
;
826 /* mark as an io operation because it could cause an interrupt */
830 gen_helper_wcsr_im(cpu_R
[dc
->r1
]);
831 tcg_gen_movi_tl(cpu_pc
, dc
->pc
+ 4);
835 dc
->is_jmp
= DISAS_UPDATE
;
838 /* mark as an io operation because it could cause an interrupt */
842 gen_helper_wcsr_ip(cpu_R
[dc
->r1
]);
843 tcg_gen_movi_tl(cpu_pc
, dc
->pc
+ 4);
847 dc
->is_jmp
= DISAS_UPDATE
;
856 tcg_gen_mov_tl(cpu_eba
, cpu_R
[dc
->r1
]);
859 tcg_gen_mov_tl(cpu_deba
, cpu_R
[dc
->r1
]);
862 gen_helper_wcsr_jtx(cpu_R
[dc
->r1
]);
865 gen_helper_wcsr_jrx(cpu_R
[dc
->r1
]);
868 tcg_gen_mov_tl(cpu_dc
, cpu_R
[dc
->r1
]);
874 no
= dc
->csr
- CSR_BP0
;
875 if (dc
->env
->num_bps
<= no
) {
876 cpu_abort(dc
->env
, "breakpoint #%i is not available\n", no
);
878 tcg_gen_mov_tl(cpu_bp
[no
], cpu_R
[dc
->r1
]);
884 no
= dc
->csr
- CSR_WP0
;
885 if (dc
->env
->num_wps
<= no
) {
886 cpu_abort(dc
->env
, "watchpoint #%i is not available\n", no
);
888 tcg_gen_mov_tl(cpu_wp
[no
], cpu_R
[dc
->r1
]);
892 cpu_abort(dc
->env
, "invalid write access csr=%x\n", dc
->csr
);
895 cpu_abort(dc
->env
, "write_csr unknown csr=%x\n", dc
->csr
);
900 static void dec_xnor(DisasContext
*dc
)
902 if (dc
->format
== OP_FMT_RI
) {
903 LOG_DIS("xnori r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
904 zero_extend(dc
->imm16
, 16));
906 if (dc
->r1
== R_R0
) {
907 LOG_DIS("not r%d, r%d\n", dc
->r2
, dc
->r0
);
909 LOG_DIS("xnor r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
913 if (dc
->format
== OP_FMT_RI
) {
914 tcg_gen_xori_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
],
915 zero_extend(dc
->imm16
, 16));
916 tcg_gen_not_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r1
]);
918 tcg_gen_eqv_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
922 static void dec_xor(DisasContext
*dc
)
924 if (dc
->format
== OP_FMT_RI
) {
925 LOG_DIS("xori r%d, r%d, %d\n", dc
->r0
, dc
->r1
,
926 zero_extend(dc
->imm16
, 16));
928 LOG_DIS("xor r%d, r%d, r%d\n", dc
->r2
, dc
->r0
, dc
->r1
);
931 if (dc
->format
== OP_FMT_RI
) {
932 tcg_gen_xori_tl(cpu_R
[dc
->r1
], cpu_R
[dc
->r0
],
933 zero_extend(dc
->imm16
, 16));
935 tcg_gen_xor_tl(cpu_R
[dc
->r2
], cpu_R
[dc
->r0
], cpu_R
[dc
->r1
]);
939 static void dec_ill(DisasContext
*dc
)
941 cpu_abort(dc
->env
, "unknown opcode 0x%02x\n", dc
->opcode
);
944 typedef void (*DecoderInfo
)(DisasContext
*dc
);
945 static const DecoderInfo decinfo
[] = {
946 dec_sru
, dec_nor
, dec_mul
, dec_sh
, dec_lb
, dec_sr
, dec_xor
, dec_lh
,
947 dec_and
, dec_xnor
, dec_lw
, dec_lhu
, dec_sb
, dec_add
, dec_or
, dec_sl
,
948 dec_lbu
, dec_be
, dec_bg
, dec_bge
, dec_bgeu
, dec_bgu
, dec_sw
, dec_bne
,
949 dec_andhi
, dec_cmpe
, dec_cmpg
, dec_cmpge
, dec_cmpgeu
, dec_cmpgu
, dec_orhi
,
951 dec_sru
, dec_nor
, dec_mul
, dec_divu
, dec_rcsr
, dec_sr
, dec_xor
, dec_ill
,
952 dec_and
, dec_xnor
, dec_ill
, dec_scall
, dec_sextb
, dec_add
, dec_or
, dec_sl
,
953 dec_b
, dec_modu
, dec_sub
, dec_user
, dec_wcsr
, dec_ill
, dec_call
, dec_sexth
,
954 dec_bi
, dec_cmpe
, dec_cmpg
, dec_cmpge
, dec_cmpgeu
, dec_cmpgu
, dec_calli
,
958 static inline void decode(DisasContext
*dc
)
962 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP
))) {
963 tcg_gen_debug_insn_start(dc
->pc
);
966 dc
->ir
= ir
= ldl_code(dc
->pc
);
967 LOG_DIS("%8.8x\t", dc
->ir
);
969 /* try guessing 'empty' instruction memory, although it may be a valid
970 * instruction sequence (eg. srui r0, r0, 0) */
974 LOG_DIS("nr_nops=%d\t", dc
->nr_nops
);
976 if (dc
->nr_nops
> 4) {
977 cpu_abort(dc
->env
, "fetching nop sequence\n");
981 dc
->opcode
= EXTRACT_FIELD(ir
, 26, 31);
983 dc
->imm5
= EXTRACT_FIELD(ir
, 0, 4);
984 dc
->imm16
= EXTRACT_FIELD(ir
, 0, 15);
985 dc
->imm26
= EXTRACT_FIELD(ir
, 0, 25);
987 dc
->csr
= EXTRACT_FIELD(ir
, 21, 25);
988 dc
->r0
= EXTRACT_FIELD(ir
, 21, 25);
989 dc
->r1
= EXTRACT_FIELD(ir
, 16, 20);
990 dc
->r2
= EXTRACT_FIELD(ir
, 11, 15);
992 /* bit 31 seems to indicate insn type. */
993 if (ir
& (1 << 31)) {
994 dc
->format
= OP_FMT_RR
;
996 dc
->format
= OP_FMT_RI
;
999 assert(ARRAY_SIZE(decinfo
) == 64);
1000 assert(dc
->opcode
< 64);
1002 decinfo
[dc
->opcode
](dc
);
1005 static void check_breakpoint(CPUState
*env
, DisasContext
*dc
)
1009 if (unlikely(!QTAILQ_EMPTY(&env
->breakpoints
))) {
1010 QTAILQ_FOREACH(bp
, &env
->breakpoints
, entry
) {
1011 if (bp
->pc
== dc
->pc
) {
1012 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
1013 t_gen_raise_exception(dc
, EXCP_DEBUG
);
1014 dc
->is_jmp
= DISAS_UPDATE
;
1020 /* generate intermediate code for basic block 'tb'. */
1021 static void gen_intermediate_code_internal(CPUState
*env
,
1022 TranslationBlock
*tb
, int search_pc
)
1024 struct DisasContext ctx
, *dc
= &ctx
;
1025 uint16_t *gen_opc_end
;
1028 uint32_t next_page_start
;
1032 qemu_log_try_set_file(stderr
);
1038 gen_opc_end
= gen_opc_buf
+ OPC_MAX_SIZE
;
1040 dc
->is_jmp
= DISAS_NEXT
;
1042 dc
->singlestep_enabled
= env
->singlestep_enabled
;
1046 cpu_abort(env
, "LM32: unaligned PC=%x\n", pc_start
);
1049 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)) {
1050 qemu_log("-----------------------------------------\n");
1051 log_cpu_state(env
, 0);
1054 next_page_start
= (pc_start
& TARGET_PAGE_MASK
) + TARGET_PAGE_SIZE
;
1057 max_insns
= tb
->cflags
& CF_COUNT_MASK
;
1058 if (max_insns
== 0) {
1059 max_insns
= CF_COUNT_MASK
;
1064 check_breakpoint(env
, dc
);
1067 j
= gen_opc_ptr
- gen_opc_buf
;
1071 gen_opc_instr_start
[lj
++] = 0;
1074 gen_opc_pc
[lj
] = dc
->pc
;
1075 gen_opc_instr_start
[lj
] = 1;
1076 gen_opc_icount
[lj
] = num_insns
;
1080 LOG_DIS("%8.8x:\t", dc
->pc
);
1082 if (num_insns
+ 1 == max_insns
&& (tb
->cflags
& CF_LAST_IO
)) {
1090 } while (!dc
->is_jmp
1091 && gen_opc_ptr
< gen_opc_end
1092 && !env
->singlestep_enabled
1094 && (dc
->pc
< next_page_start
)
1095 && num_insns
< max_insns
);
1097 if (tb
->cflags
& CF_LAST_IO
) {
1101 if (unlikely(env
->singlestep_enabled
)) {
1102 if (dc
->is_jmp
== DISAS_NEXT
) {
1103 tcg_gen_movi_tl(cpu_pc
, dc
->pc
);
1105 t_gen_raise_exception(dc
, EXCP_DEBUG
);
1107 switch (dc
->is_jmp
) {
1109 gen_goto_tb(dc
, 1, dc
->pc
);
1114 /* indicate that the hash table must be used
1115 to find the next TB */
1119 /* nothing more to generate */
1124 gen_icount_end(tb
, num_insns
);
1125 *gen_opc_ptr
= INDEX_op_end
;
1127 j
= gen_opc_ptr
- gen_opc_buf
;
1130 gen_opc_instr_start
[lj
++] = 0;
1133 tb
->size
= dc
->pc
- pc_start
;
1134 tb
->icount
= num_insns
;
1138 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)) {
1140 log_target_disas(pc_start
, dc
->pc
- pc_start
, 0);
1141 qemu_log("\nisize=%d osize=%zd\n",
1142 dc
->pc
- pc_start
, gen_opc_ptr
- gen_opc_buf
);
1147 void gen_intermediate_code(CPUState
*env
, struct TranslationBlock
*tb
)
1149 gen_intermediate_code_internal(env
, tb
, 0);
1152 void gen_intermediate_code_pc(CPUState
*env
, struct TranslationBlock
*tb
)
1154 gen_intermediate_code_internal(env
, tb
, 1);
1157 void cpu_dump_state(CPUState
*env
, FILE *f
, fprintf_function cpu_fprintf
,
1166 cpu_fprintf(f
, "IN: PC=%x %s\n",
1167 env
->pc
, lookup_symbol(env
->pc
));
1169 cpu_fprintf(f
, "ie=%8.8x (IE=%x EIE=%x BIE=%x) im=%8.8x ip=%8.8x\n",
1171 (env
->ie
& IE_IE
) ? 1 : 0,
1172 (env
->ie
& IE_EIE
) ? 1 : 0,
1173 (env
->ie
& IE_BIE
) ? 1 : 0,
1174 lm32_pic_get_im(env
->pic_state
),
1175 lm32_pic_get_ip(env
->pic_state
));
1176 cpu_fprintf(f
, "eba=%8.8x deba=%8.8x\n",
1180 for (i
= 0; i
< 32; i
++) {
1181 cpu_fprintf(f
, "r%2.2d=%8.8x ", i
, env
->regs
[i
]);
1182 if ((i
+ 1) % 4 == 0) {
1183 cpu_fprintf(f
, "\n");
1186 cpu_fprintf(f
, "\n\n");
1189 void restore_state_to_opc(CPUState
*env
, TranslationBlock
*tb
, int pc_pos
)
1191 env
->pc
= gen_opc_pc
[pc_pos
];
1194 void lm32_translate_init(void)
1198 cpu_env
= tcg_global_reg_new_ptr(TCG_AREG0
, "env");
1200 for (i
= 0; i
< ARRAY_SIZE(cpu_R
); i
++) {
1201 cpu_R
[i
] = tcg_global_mem_new(TCG_AREG0
,
1202 offsetof(CPUState
, regs
[i
]),
1206 for (i
= 0; i
< ARRAY_SIZE(cpu_bp
); i
++) {
1207 cpu_bp
[i
] = tcg_global_mem_new(TCG_AREG0
,
1208 offsetof(CPUState
, bp
[i
]),
1212 for (i
= 0; i
< ARRAY_SIZE(cpu_wp
); i
++) {
1213 cpu_wp
[i
] = tcg_global_mem_new(TCG_AREG0
,
1214 offsetof(CPUState
, wp
[i
]),
1218 cpu_pc
= tcg_global_mem_new(TCG_AREG0
,
1219 offsetof(CPUState
, pc
),
1221 cpu_ie
= tcg_global_mem_new(TCG_AREG0
,
1222 offsetof(CPUState
, ie
),
1224 cpu_icc
= tcg_global_mem_new(TCG_AREG0
,
1225 offsetof(CPUState
, icc
),
1227 cpu_dcc
= tcg_global_mem_new(TCG_AREG0
,
1228 offsetof(CPUState
, dcc
),
1230 cpu_cc
= tcg_global_mem_new(TCG_AREG0
,
1231 offsetof(CPUState
, cc
),
1233 cpu_cfg
= tcg_global_mem_new(TCG_AREG0
,
1234 offsetof(CPUState
, cfg
),
1236 cpu_eba
= tcg_global_mem_new(TCG_AREG0
,
1237 offsetof(CPUState
, eba
),
1239 cpu_dc
= tcg_global_mem_new(TCG_AREG0
,
1240 offsetof(CPUState
, dc
),
1242 cpu_deba
= tcg_global_mem_new(TCG_AREG0
,
1243 offsetof(CPUState
, deba
),