2 * RISC-V translation routines for the RVXI Base Integer Instruction Set.
4 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
5 * Copyright (c) 2018 Peer Adelt, peer.adelt@hni.uni-paderborn.de
6 * Bastian Koppelmann, kbastian@mail.uni-paderborn.de
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2 or later, as published by the Free Software Foundation.
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
21 static bool trans_illegal(DisasContext *ctx, arg_empty *a)
23 gen_exception_illegal(ctx);
27 static bool trans_c64_illegal(DisasContext *ctx, arg_empty *a)
30 return trans_illegal(ctx, a);
33 static bool trans_lui(DisasContext *ctx, arg_lui *a)
36 tcg_gen_movi_tl(cpu_gpr[a->rd], a->imm);
41 static bool trans_auipc(DisasContext *ctx, arg_auipc *a)
44 tcg_gen_movi_tl(cpu_gpr[a->rd], a->imm + ctx->base.pc_next);
49 static bool trans_jal(DisasContext *ctx, arg_jal *a)
51 gen_jal(ctx, a->rd, a->imm);
55 static bool trans_jalr(DisasContext *ctx, arg_jalr *a)
57 TCGLabel *misaligned = NULL;
59 tcg_gen_addi_tl(cpu_pc, get_gpr(ctx, a->rs1, EXT_NONE), a->imm);
60 tcg_gen_andi_tl(cpu_pc, cpu_pc, (target_ulong)-2);
62 if (!has_ext(ctx, RVC)) {
63 TCGv t0 = tcg_temp_new();
65 misaligned = gen_new_label();
66 tcg_gen_andi_tl(t0, cpu_pc, 0x2);
67 tcg_gen_brcondi_tl(TCG_COND_NE, t0, 0x0, misaligned);
72 tcg_gen_movi_tl(cpu_gpr[a->rd], ctx->pc_succ_insn);
75 /* No chaining with JALR. */
76 lookup_and_goto_ptr(ctx);
79 gen_set_label(misaligned);
80 gen_exception_inst_addr_mis(ctx);
82 ctx->base.is_jmp = DISAS_NORETURN;
87 static bool gen_branch(DisasContext *ctx, arg_b *a, TCGCond cond)
89 TCGLabel *l = gen_new_label();
90 TCGv src1 = get_gpr(ctx, a->rs1, EXT_SIGN);
91 TCGv src2 = get_gpr(ctx, a->rs2, EXT_SIGN);
93 tcg_gen_brcond_tl(cond, src1, src2, l);
94 gen_goto_tb(ctx, 1, ctx->pc_succ_insn);
96 gen_set_label(l); /* branch taken */
98 if (!has_ext(ctx, RVC) && ((ctx->base.pc_next + a->imm) & 0x3)) {
100 gen_exception_inst_addr_mis(ctx);
102 gen_goto_tb(ctx, 0, ctx->base.pc_next + a->imm);
104 ctx->base.is_jmp = DISAS_NORETURN;
109 static bool trans_beq(DisasContext *ctx, arg_beq *a)
111 return gen_branch(ctx, a, TCG_COND_EQ);
114 static bool trans_bne(DisasContext *ctx, arg_bne *a)
116 return gen_branch(ctx, a, TCG_COND_NE);
119 static bool trans_blt(DisasContext *ctx, arg_blt *a)
121 return gen_branch(ctx, a, TCG_COND_LT);
124 static bool trans_bge(DisasContext *ctx, arg_bge *a)
126 return gen_branch(ctx, a, TCG_COND_GE);
129 static bool trans_bltu(DisasContext *ctx, arg_bltu *a)
131 return gen_branch(ctx, a, TCG_COND_LTU);
134 static bool trans_bgeu(DisasContext *ctx, arg_bgeu *a)
136 return gen_branch(ctx, a, TCG_COND_GEU);
139 static bool gen_load(DisasContext *ctx, arg_lb *a, MemOp memop)
141 TCGv dest = dest_gpr(ctx, a->rd);
142 TCGv addr = get_gpr(ctx, a->rs1, EXT_NONE);
145 TCGv temp = temp_new(ctx);
146 tcg_gen_addi_tl(temp, addr, a->imm);
150 tcg_gen_qemu_ld_tl(dest, addr, ctx->mem_idx, memop);
151 gen_set_gpr(ctx, a->rd, dest);
155 static bool trans_lb(DisasContext *ctx, arg_lb *a)
157 return gen_load(ctx, a, MO_SB);
160 static bool trans_lh(DisasContext *ctx, arg_lh *a)
162 return gen_load(ctx, a, MO_TESW);
165 static bool trans_lw(DisasContext *ctx, arg_lw *a)
167 return gen_load(ctx, a, MO_TESL);
170 static bool trans_lbu(DisasContext *ctx, arg_lbu *a)
172 return gen_load(ctx, a, MO_UB);
175 static bool trans_lhu(DisasContext *ctx, arg_lhu *a)
177 return gen_load(ctx, a, MO_TEUW);
180 static bool gen_store(DisasContext *ctx, arg_sb *a, MemOp memop)
182 TCGv addr = get_gpr(ctx, a->rs1, EXT_NONE);
183 TCGv data = get_gpr(ctx, a->rs2, EXT_NONE);
186 TCGv temp = temp_new(ctx);
187 tcg_gen_addi_tl(temp, addr, a->imm);
191 tcg_gen_qemu_st_tl(data, addr, ctx->mem_idx, memop);
195 static bool trans_sb(DisasContext *ctx, arg_sb *a)
197 return gen_store(ctx, a, MO_SB);
200 static bool trans_sh(DisasContext *ctx, arg_sh *a)
202 return gen_store(ctx, a, MO_TESW);
205 static bool trans_sw(DisasContext *ctx, arg_sw *a)
207 return gen_store(ctx, a, MO_TESL);
210 static bool trans_lwu(DisasContext *ctx, arg_lwu *a)
213 return gen_load(ctx, a, MO_TEUL);
216 static bool trans_ld(DisasContext *ctx, arg_ld *a)
219 return gen_load(ctx, a, MO_TEQ);
222 static bool trans_sd(DisasContext *ctx, arg_sd *a)
225 return gen_store(ctx, a, MO_TEQ);
228 static bool trans_addi(DisasContext *ctx, arg_addi *a)
230 return gen_arith_imm_fn(ctx, a, EXT_NONE, tcg_gen_addi_tl);
233 static void gen_slt(TCGv ret, TCGv s1, TCGv s2)
235 tcg_gen_setcond_tl(TCG_COND_LT, ret, s1, s2);
238 static void gen_sltu(TCGv ret, TCGv s1, TCGv s2)
240 tcg_gen_setcond_tl(TCG_COND_LTU, ret, s1, s2);
243 static bool trans_slti(DisasContext *ctx, arg_slti *a)
245 return gen_arith_imm_tl(ctx, a, EXT_SIGN, gen_slt);
248 static bool trans_sltiu(DisasContext *ctx, arg_sltiu *a)
250 return gen_arith_imm_tl(ctx, a, EXT_SIGN, gen_sltu);
253 static bool trans_xori(DisasContext *ctx, arg_xori *a)
255 return gen_arith_imm_fn(ctx, a, EXT_NONE, tcg_gen_xori_tl);
258 static bool trans_ori(DisasContext *ctx, arg_ori *a)
260 return gen_arith_imm_fn(ctx, a, EXT_NONE, tcg_gen_ori_tl);
263 static bool trans_andi(DisasContext *ctx, arg_andi *a)
265 return gen_arith_imm_fn(ctx, a, EXT_NONE, tcg_gen_andi_tl);
268 static bool trans_slli(DisasContext *ctx, arg_slli *a)
270 return gen_shift_imm_fn(ctx, a, EXT_NONE, tcg_gen_shli_tl);
273 static bool trans_srli(DisasContext *ctx, arg_srli *a)
275 return gen_shift_imm_fn(ctx, a, EXT_ZERO, tcg_gen_shri_tl);
278 static bool trans_srai(DisasContext *ctx, arg_srai *a)
280 return gen_shift_imm_fn(ctx, a, EXT_SIGN, tcg_gen_sari_tl);
283 static bool trans_add(DisasContext *ctx, arg_add *a)
285 return gen_arith(ctx, a, EXT_NONE, tcg_gen_add_tl);
288 static bool trans_sub(DisasContext *ctx, arg_sub *a)
290 return gen_arith(ctx, a, EXT_NONE, tcg_gen_sub_tl);
293 static bool trans_sll(DisasContext *ctx, arg_sll *a)
295 return gen_shift(ctx, a, EXT_NONE, tcg_gen_shl_tl);
298 static bool trans_slt(DisasContext *ctx, arg_slt *a)
300 return gen_arith(ctx, a, EXT_SIGN, gen_slt);
303 static bool trans_sltu(DisasContext *ctx, arg_sltu *a)
305 return gen_arith(ctx, a, EXT_SIGN, gen_sltu);
308 static bool trans_xor(DisasContext *ctx, arg_xor *a)
310 return gen_arith(ctx, a, EXT_NONE, tcg_gen_xor_tl);
313 static bool trans_srl(DisasContext *ctx, arg_srl *a)
315 return gen_shift(ctx, a, EXT_ZERO, tcg_gen_shr_tl);
318 static bool trans_sra(DisasContext *ctx, arg_sra *a)
320 return gen_shift(ctx, a, EXT_SIGN, tcg_gen_sar_tl);
323 static bool trans_or(DisasContext *ctx, arg_or *a)
325 return gen_arith(ctx, a, EXT_NONE, tcg_gen_or_tl);
328 static bool trans_and(DisasContext *ctx, arg_and *a)
330 return gen_arith(ctx, a, EXT_NONE, tcg_gen_and_tl);
333 static bool trans_addiw(DisasContext *ctx, arg_addiw *a)
337 return gen_arith_imm_fn(ctx, a, EXT_NONE, tcg_gen_addi_tl);
340 static bool trans_slliw(DisasContext *ctx, arg_slliw *a)
344 return gen_shift_imm_fn(ctx, a, EXT_NONE, tcg_gen_shli_tl);
347 static void gen_srliw(TCGv dst, TCGv src, target_long shamt)
349 tcg_gen_extract_tl(dst, src, shamt, 32 - shamt);
352 static bool trans_srliw(DisasContext *ctx, arg_srliw *a)
356 return gen_shift_imm_fn(ctx, a, EXT_NONE, gen_srliw);
359 static void gen_sraiw(TCGv dst, TCGv src, target_long shamt)
361 tcg_gen_sextract_tl(dst, src, shamt, 32 - shamt);
364 static bool trans_sraiw(DisasContext *ctx, arg_sraiw *a)
368 return gen_shift_imm_fn(ctx, a, EXT_NONE, gen_sraiw);
371 static bool trans_addw(DisasContext *ctx, arg_addw *a)
375 return gen_arith(ctx, a, EXT_NONE, tcg_gen_add_tl);
378 static bool trans_subw(DisasContext *ctx, arg_subw *a)
382 return gen_arith(ctx, a, EXT_NONE, tcg_gen_sub_tl);
385 static bool trans_sllw(DisasContext *ctx, arg_sllw *a)
389 return gen_shift(ctx, a, EXT_NONE, tcg_gen_shl_tl);
392 static bool trans_srlw(DisasContext *ctx, arg_srlw *a)
396 return gen_shift(ctx, a, EXT_ZERO, tcg_gen_shr_tl);
399 static bool trans_sraw(DisasContext *ctx, arg_sraw *a)
403 return gen_shift(ctx, a, EXT_SIGN, tcg_gen_sar_tl);
406 static bool trans_fence(DisasContext *ctx, arg_fence *a)
408 /* FENCE is a full memory barrier. */
409 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
413 static bool trans_fence_i(DisasContext *ctx, arg_fence_i *a)
415 if (!ctx->ext_ifencei) {
420 * FENCE_I is a no-op in QEMU,
421 * however we need to end the translation block
423 tcg_gen_movi_tl(cpu_pc, ctx->pc_succ_insn);
425 ctx->base.is_jmp = DISAS_NORETURN;
429 static bool do_csr_post(DisasContext *ctx)
431 /* We may have changed important cpu state -- exit to main loop. */
432 tcg_gen_movi_tl(cpu_pc, ctx->pc_succ_insn);
434 ctx->base.is_jmp = DISAS_NORETURN;
438 static bool do_csrr(DisasContext *ctx, int rd, int rc)
440 TCGv dest = dest_gpr(ctx, rd);
441 TCGv_i32 csr = tcg_constant_i32(rc);
443 if (tb_cflags(ctx->base.tb) & CF_USE_ICOUNT) {
446 gen_helper_csrr(dest, cpu_env, csr);
447 gen_set_gpr(ctx, rd, dest);
448 return do_csr_post(ctx);
451 static bool do_csrw(DisasContext *ctx, int rc, TCGv src)
453 TCGv_i32 csr = tcg_constant_i32(rc);
455 if (tb_cflags(ctx->base.tb) & CF_USE_ICOUNT) {
458 gen_helper_csrw(cpu_env, csr, src);
459 return do_csr_post(ctx);
462 static bool do_csrrw(DisasContext *ctx, int rd, int rc, TCGv src, TCGv mask)
464 TCGv dest = dest_gpr(ctx, rd);
465 TCGv_i32 csr = tcg_constant_i32(rc);
467 if (tb_cflags(ctx->base.tb) & CF_USE_ICOUNT) {
470 gen_helper_csrrw(dest, cpu_env, csr, src, mask);
471 gen_set_gpr(ctx, rd, dest);
472 return do_csr_post(ctx);
475 static bool trans_csrrw(DisasContext *ctx, arg_csrrw *a)
477 TCGv src = get_gpr(ctx, a->rs1, EXT_NONE);
480 * If rd == 0, the insn shall not read the csr, nor cause any of the
481 * side effects that might occur on a csr read.
484 return do_csrw(ctx, a->csr, src);
487 TCGv mask = tcg_constant_tl(-1);
488 return do_csrrw(ctx, a->rd, a->csr, src, mask);
491 static bool trans_csrrs(DisasContext *ctx, arg_csrrs *a)
494 * If rs1 == 0, the insn shall not write to the csr at all, nor
495 * cause any of the side effects that might occur on a csr write.
496 * Note that if rs1 specifies a register other than x0, holding
497 * a zero value, the instruction will still attempt to write the
498 * unmodified value back to the csr and will cause side effects.
501 return do_csrr(ctx, a->rd, a->csr);
504 TCGv ones = tcg_constant_tl(-1);
505 TCGv mask = get_gpr(ctx, a->rs1, EXT_ZERO);
506 return do_csrrw(ctx, a->rd, a->csr, ones, mask);
509 static bool trans_csrrc(DisasContext *ctx, arg_csrrc *a)
512 * If rs1 == 0, the insn shall not write to the csr at all, nor
513 * cause any of the side effects that might occur on a csr write.
514 * Note that if rs1 specifies a register other than x0, holding
515 * a zero value, the instruction will still attempt to write the
516 * unmodified value back to the csr and will cause side effects.
519 return do_csrr(ctx, a->rd, a->csr);
522 TCGv mask = get_gpr(ctx, a->rs1, EXT_ZERO);
523 return do_csrrw(ctx, a->rd, a->csr, ctx->zero, mask);
526 static bool trans_csrrwi(DisasContext *ctx, arg_csrrwi *a)
528 TCGv src = tcg_constant_tl(a->rs1);
531 * If rd == 0, the insn shall not read the csr, nor cause any of the
532 * side effects that might occur on a csr read.
535 return do_csrw(ctx, a->csr, src);
538 TCGv mask = tcg_constant_tl(-1);
539 return do_csrrw(ctx, a->rd, a->csr, src, mask);
542 static bool trans_csrrsi(DisasContext *ctx, arg_csrrsi *a)
545 * If rs1 == 0, the insn shall not write to the csr at all, nor
546 * cause any of the side effects that might occur on a csr write.
547 * Note that if rs1 specifies a register other than x0, holding
548 * a zero value, the instruction will still attempt to write the
549 * unmodified value back to the csr and will cause side effects.
552 return do_csrr(ctx, a->rd, a->csr);
555 TCGv ones = tcg_constant_tl(-1);
556 TCGv mask = tcg_constant_tl(a->rs1);
557 return do_csrrw(ctx, a->rd, a->csr, ones, mask);
560 static bool trans_csrrci(DisasContext *ctx, arg_csrrci *a)
563 * If rs1 == 0, the insn shall not write to the csr at all, nor
564 * cause any of the side effects that might occur on a csr write.
565 * Note that if rs1 specifies a register other than x0, holding
566 * a zero value, the instruction will still attempt to write the
567 * unmodified value back to the csr and will cause side effects.
570 return do_csrr(ctx, a->rd, a->csr);
573 TCGv mask = tcg_constant_tl(a->rs1);
574 return do_csrrw(ctx, a->rd, a->csr, ctx->zero, mask);