xref: /openbmc/qemu/target/riscv/translate.c (revision 05b80ed0)
1 /*
2  * RISC-V emulation for qemu: main translation routines.
3  *
4  * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2 or later, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "qemu/osdep.h"
20 #include "qemu/log.h"
21 #include "cpu.h"
22 #include "tcg/tcg-op.h"
23 #include "disas/disas.h"
24 #include "exec/cpu_ldst.h"
25 #include "exec/exec-all.h"
26 #include "exec/helper-proto.h"
27 #include "exec/helper-gen.h"
28 
29 #include "exec/translator.h"
30 #include "exec/log.h"
31 
32 #include "instmap.h"
33 
34 /* global register indices */
35 static TCGv cpu_gpr[32], cpu_pc, cpu_vl;
36 static TCGv_i64 cpu_fpr[32]; /* assume F and D extensions */
37 static TCGv load_res;
38 static TCGv load_val;
39 
40 #include "exec/gen-icount.h"
41 
42 typedef struct DisasContext {
43     DisasContextBase base;
44     /* pc_succ_insn points to the instruction following base.pc_next */
45     target_ulong pc_succ_insn;
46     target_ulong priv_ver;
47     bool virt_enabled;
48     uint32_t opcode;
49     uint32_t mstatus_fs;
50     target_ulong misa;
51     uint32_t mem_idx;
52     /* Remember the rounding mode encoded in the previous fp instruction,
53        which we have already installed into env->fp_status.  Or -1 for
54        no previous fp instruction.  Note that we exit the TB when writing
55        to any system register, which includes CSR_FRM, so we do not have
56        to reset this known value.  */
57     int frm;
58     bool ext_ifencei;
59     bool hlsx;
60     /* vector extension */
61     bool vill;
62     uint8_t lmul;
63     uint8_t sew;
64     uint16_t vlen;
65     uint16_t mlen;
66     bool vl_eq_vlmax;
67     CPUState *cs;
68 } DisasContext;
69 
70 static inline bool has_ext(DisasContext *ctx, uint32_t ext)
71 {
72     return ctx->misa & ext;
73 }
74 
75 #ifdef TARGET_RISCV32
76 # define is_32bit(ctx)  true
77 #elif defined(CONFIG_USER_ONLY)
78 # define is_32bit(ctx)  false
79 #else
80 static inline bool is_32bit(DisasContext *ctx)
81 {
82     return (ctx->misa & RV32) == RV32;
83 }
84 #endif
85 
86 /*
87  * RISC-V requires NaN-boxing of narrower width floating point values.
88  * This applies when a 32-bit value is assigned to a 64-bit FP register.
89  * For consistency and simplicity, we nanbox results even when the RVD
90  * extension is not present.
91  */
92 static void gen_nanbox_s(TCGv_i64 out, TCGv_i64 in)
93 {
94     tcg_gen_ori_i64(out, in, MAKE_64BIT_MASK(32, 32));
95 }
96 
97 /*
98  * A narrow n-bit operation, where n < FLEN, checks that input operands
99  * are correctly Nan-boxed, i.e., all upper FLEN - n bits are 1.
100  * If so, the least-significant bits of the input are used, otherwise the
101  * input value is treated as an n-bit canonical NaN (v2.2 section 9.2).
102  *
103  * Here, the result is always nan-boxed, even the canonical nan.
104  */
105 static void gen_check_nanbox_s(TCGv_i64 out, TCGv_i64 in)
106 {
107     TCGv_i64 t_max = tcg_constant_i64(0xffffffff00000000ull);
108     TCGv_i64 t_nan = tcg_constant_i64(0xffffffff7fc00000ull);
109 
110     tcg_gen_movcond_i64(TCG_COND_GEU, out, in, t_max, in, t_nan);
111 }
112 
113 static void generate_exception(DisasContext *ctx, int excp)
114 {
115     tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next);
116     gen_helper_raise_exception(cpu_env, tcg_constant_i32(excp));
117     ctx->base.is_jmp = DISAS_NORETURN;
118 }
119 
120 static void generate_exception_mtval(DisasContext *ctx, int excp)
121 {
122     tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next);
123     tcg_gen_st_tl(cpu_pc, cpu_env, offsetof(CPURISCVState, badaddr));
124     gen_helper_raise_exception(cpu_env, tcg_constant_i32(excp));
125     ctx->base.is_jmp = DISAS_NORETURN;
126 }
127 
128 static void gen_exception_debug(void)
129 {
130     gen_helper_raise_exception(cpu_env, tcg_constant_i32(EXCP_DEBUG));
131 }
132 
133 /* Wrapper around tcg_gen_exit_tb that handles single stepping */
134 static void exit_tb(DisasContext *ctx)
135 {
136     if (ctx->base.singlestep_enabled) {
137         gen_exception_debug();
138     } else {
139         tcg_gen_exit_tb(NULL, 0);
140     }
141 }
142 
143 /* Wrapper around tcg_gen_lookup_and_goto_ptr that handles single stepping */
144 static void lookup_and_goto_ptr(DisasContext *ctx)
145 {
146     if (ctx->base.singlestep_enabled) {
147         gen_exception_debug();
148     } else {
149         tcg_gen_lookup_and_goto_ptr();
150     }
151 }
152 
153 static void gen_exception_illegal(DisasContext *ctx)
154 {
155     generate_exception(ctx, RISCV_EXCP_ILLEGAL_INST);
156 }
157 
158 static void gen_exception_inst_addr_mis(DisasContext *ctx)
159 {
160     generate_exception_mtval(ctx, RISCV_EXCP_INST_ADDR_MIS);
161 }
162 
163 static void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
164 {
165     if (translator_use_goto_tb(&ctx->base, dest)) {
166         tcg_gen_goto_tb(n);
167         tcg_gen_movi_tl(cpu_pc, dest);
168         tcg_gen_exit_tb(ctx->base.tb, n);
169     } else {
170         tcg_gen_movi_tl(cpu_pc, dest);
171         lookup_and_goto_ptr(ctx);
172     }
173 }
174 
175 /* Wrapper for getting reg values - need to check of reg is zero since
176  * cpu_gpr[0] is not actually allocated
177  */
178 static inline void gen_get_gpr(TCGv t, int reg_num)
179 {
180     if (reg_num == 0) {
181         tcg_gen_movi_tl(t, 0);
182     } else {
183         tcg_gen_mov_tl(t, cpu_gpr[reg_num]);
184     }
185 }
186 
187 /* Wrapper for setting reg values - need to check of reg is zero since
188  * cpu_gpr[0] is not actually allocated. this is more for safety purposes,
189  * since we usually avoid calling the OP_TYPE_gen function if we see a write to
190  * $zero
191  */
192 static inline void gen_set_gpr(int reg_num_dst, TCGv t)
193 {
194     if (reg_num_dst != 0) {
195         tcg_gen_mov_tl(cpu_gpr[reg_num_dst], t);
196     }
197 }
198 
199 static void gen_mulhsu(TCGv ret, TCGv arg1, TCGv arg2)
200 {
201     TCGv rl = tcg_temp_new();
202     TCGv rh = tcg_temp_new();
203 
204     tcg_gen_mulu2_tl(rl, rh, arg1, arg2);
205     /* fix up for one negative */
206     tcg_gen_sari_tl(rl, arg1, TARGET_LONG_BITS - 1);
207     tcg_gen_and_tl(rl, rl, arg2);
208     tcg_gen_sub_tl(ret, rh, rl);
209 
210     tcg_temp_free(rl);
211     tcg_temp_free(rh);
212 }
213 
214 static void gen_div(TCGv ret, TCGv source1, TCGv source2)
215 {
216     TCGv cond1, cond2, zeroreg, resultopt1;
217     /*
218      * Handle by altering args to tcg_gen_div to produce req'd results:
219      * For overflow: want source1 in source1 and 1 in source2
220      * For div by zero: want -1 in source1 and 1 in source2 -> -1 result
221      */
222     cond1 = tcg_temp_new();
223     cond2 = tcg_temp_new();
224     zeroreg = tcg_constant_tl(0);
225     resultopt1 = tcg_temp_new();
226 
227     tcg_gen_movi_tl(resultopt1, (target_ulong)-1);
228     tcg_gen_setcondi_tl(TCG_COND_EQ, cond2, source2, (target_ulong)(~0L));
229     tcg_gen_setcondi_tl(TCG_COND_EQ, cond1, source1,
230                         ((target_ulong)1) << (TARGET_LONG_BITS - 1));
231     tcg_gen_and_tl(cond1, cond1, cond2); /* cond1 = overflow */
232     tcg_gen_setcondi_tl(TCG_COND_EQ, cond2, source2, 0); /* cond2 = div 0 */
233     /* if div by zero, set source1 to -1, otherwise don't change */
234     tcg_gen_movcond_tl(TCG_COND_EQ, source1, cond2, zeroreg, source1,
235             resultopt1);
236     /* if overflow or div by zero, set source2 to 1, else don't change */
237     tcg_gen_or_tl(cond1, cond1, cond2);
238     tcg_gen_movi_tl(resultopt1, (target_ulong)1);
239     tcg_gen_movcond_tl(TCG_COND_EQ, source2, cond1, zeroreg, source2,
240             resultopt1);
241     tcg_gen_div_tl(ret, source1, source2);
242 
243     tcg_temp_free(cond1);
244     tcg_temp_free(cond2);
245     tcg_temp_free(resultopt1);
246 }
247 
248 static void gen_divu(TCGv ret, TCGv source1, TCGv source2)
249 {
250     TCGv cond1, zeroreg, resultopt1;
251     cond1 = tcg_temp_new();
252 
253     zeroreg = tcg_constant_tl(0);
254     resultopt1 = tcg_temp_new();
255 
256     tcg_gen_setcondi_tl(TCG_COND_EQ, cond1, source2, 0);
257     tcg_gen_movi_tl(resultopt1, (target_ulong)-1);
258     tcg_gen_movcond_tl(TCG_COND_EQ, source1, cond1, zeroreg, source1,
259             resultopt1);
260     tcg_gen_movi_tl(resultopt1, (target_ulong)1);
261     tcg_gen_movcond_tl(TCG_COND_EQ, source2, cond1, zeroreg, source2,
262             resultopt1);
263     tcg_gen_divu_tl(ret, source1, source2);
264 
265     tcg_temp_free(cond1);
266     tcg_temp_free(resultopt1);
267 }
268 
269 static void gen_rem(TCGv ret, TCGv source1, TCGv source2)
270 {
271     TCGv cond1, cond2, zeroreg, resultopt1;
272 
273     cond1 = tcg_temp_new();
274     cond2 = tcg_temp_new();
275     zeroreg = tcg_constant_tl(0);
276     resultopt1 = tcg_temp_new();
277 
278     tcg_gen_movi_tl(resultopt1, 1L);
279     tcg_gen_setcondi_tl(TCG_COND_EQ, cond2, source2, (target_ulong)-1);
280     tcg_gen_setcondi_tl(TCG_COND_EQ, cond1, source1,
281                         (target_ulong)1 << (TARGET_LONG_BITS - 1));
282     tcg_gen_and_tl(cond2, cond1, cond2); /* cond1 = overflow */
283     tcg_gen_setcondi_tl(TCG_COND_EQ, cond1, source2, 0); /* cond2 = div 0 */
284     /* if overflow or div by zero, set source2 to 1, else don't change */
285     tcg_gen_or_tl(cond2, cond1, cond2);
286     tcg_gen_movcond_tl(TCG_COND_EQ, source2, cond2, zeroreg, source2,
287             resultopt1);
288     tcg_gen_rem_tl(resultopt1, source1, source2);
289     /* if div by zero, just return the original dividend */
290     tcg_gen_movcond_tl(TCG_COND_EQ, ret, cond1, zeroreg, resultopt1,
291             source1);
292 
293     tcg_temp_free(cond1);
294     tcg_temp_free(cond2);
295     tcg_temp_free(resultopt1);
296 }
297 
298 static void gen_remu(TCGv ret, TCGv source1, TCGv source2)
299 {
300     TCGv cond1, zeroreg, resultopt1;
301     cond1 = tcg_temp_new();
302     zeroreg = tcg_constant_tl(0);
303     resultopt1 = tcg_temp_new();
304 
305     tcg_gen_movi_tl(resultopt1, (target_ulong)1);
306     tcg_gen_setcondi_tl(TCG_COND_EQ, cond1, source2, 0);
307     tcg_gen_movcond_tl(TCG_COND_EQ, source2, cond1, zeroreg, source2,
308             resultopt1);
309     tcg_gen_remu_tl(resultopt1, source1, source2);
310     /* if div by zero, just return the original dividend */
311     tcg_gen_movcond_tl(TCG_COND_EQ, ret, cond1, zeroreg, resultopt1,
312             source1);
313 
314     tcg_temp_free(cond1);
315     tcg_temp_free(resultopt1);
316 }
317 
318 static void gen_jal(DisasContext *ctx, int rd, target_ulong imm)
319 {
320     target_ulong next_pc;
321 
322     /* check misaligned: */
323     next_pc = ctx->base.pc_next + imm;
324     if (!has_ext(ctx, RVC)) {
325         if ((next_pc & 0x3) != 0) {
326             gen_exception_inst_addr_mis(ctx);
327             return;
328         }
329     }
330     if (rd != 0) {
331         tcg_gen_movi_tl(cpu_gpr[rd], ctx->pc_succ_insn);
332     }
333 
334     gen_goto_tb(ctx, 0, ctx->base.pc_next + imm); /* must use this for safety */
335     ctx->base.is_jmp = DISAS_NORETURN;
336 }
337 
338 #ifndef CONFIG_USER_ONLY
339 /* The states of mstatus_fs are:
340  * 0 = disabled, 1 = initial, 2 = clean, 3 = dirty
341  * We will have already diagnosed disabled state,
342  * and need to turn initial/clean into dirty.
343  */
344 static void mark_fs_dirty(DisasContext *ctx)
345 {
346     TCGv tmp;
347     target_ulong sd;
348 
349     if (ctx->mstatus_fs == MSTATUS_FS) {
350         return;
351     }
352     /* Remember the state change for the rest of the TB.  */
353     ctx->mstatus_fs = MSTATUS_FS;
354 
355     tmp = tcg_temp_new();
356     sd = is_32bit(ctx) ? MSTATUS32_SD : MSTATUS64_SD;
357 
358     tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
359     tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS | sd);
360     tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
361 
362     if (ctx->virt_enabled) {
363         tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus_hs));
364         tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS | sd);
365         tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus_hs));
366     }
367     tcg_temp_free(tmp);
368 }
369 #else
370 static inline void mark_fs_dirty(DisasContext *ctx) { }
371 #endif
372 
373 static void gen_set_rm(DisasContext *ctx, int rm)
374 {
375     if (ctx->frm == rm) {
376         return;
377     }
378     ctx->frm = rm;
379     gen_helper_set_rounding_mode(cpu_env, tcg_constant_i32(rm));
380 }
381 
382 static int ex_plus_1(DisasContext *ctx, int nf)
383 {
384     return nf + 1;
385 }
386 
387 #define EX_SH(amount) \
388     static int ex_shift_##amount(DisasContext *ctx, int imm) \
389     {                                         \
390         return imm << amount;                 \
391     }
392 EX_SH(1)
393 EX_SH(2)
394 EX_SH(3)
395 EX_SH(4)
396 EX_SH(12)
397 
398 #define REQUIRE_EXT(ctx, ext) do { \
399     if (!has_ext(ctx, ext)) {      \
400         return false;              \
401     }                              \
402 } while (0)
403 
404 #define REQUIRE_64BIT(ctx) do { \
405     if (is_32bit(ctx)) {        \
406         return false;           \
407     }                           \
408 } while (0)
409 
410 static int ex_rvc_register(DisasContext *ctx, int reg)
411 {
412     return 8 + reg;
413 }
414 
415 static int ex_rvc_shifti(DisasContext *ctx, int imm)
416 {
417     /* For RV128 a shamt of 0 means a shift by 64. */
418     return imm ? imm : 64;
419 }
420 
421 /* Include the auto-generated decoder for 32 bit insn */
422 #include "decode-insn32.c.inc"
423 
424 static bool gen_arith_imm_fn(DisasContext *ctx, arg_i *a,
425                              void (*func)(TCGv, TCGv, target_long))
426 {
427     TCGv source1;
428     source1 = tcg_temp_new();
429 
430     gen_get_gpr(source1, a->rs1);
431 
432     (*func)(source1, source1, a->imm);
433 
434     gen_set_gpr(a->rd, source1);
435     tcg_temp_free(source1);
436     return true;
437 }
438 
439 static bool gen_arith_imm_tl(DisasContext *ctx, arg_i *a,
440                              void (*func)(TCGv, TCGv, TCGv))
441 {
442     TCGv source1, source2;
443     source1 = tcg_temp_new();
444     source2 = tcg_temp_new();
445 
446     gen_get_gpr(source1, a->rs1);
447     tcg_gen_movi_tl(source2, a->imm);
448 
449     (*func)(source1, source1, source2);
450 
451     gen_set_gpr(a->rd, source1);
452     tcg_temp_free(source1);
453     tcg_temp_free(source2);
454     return true;
455 }
456 
457 static void gen_addw(TCGv ret, TCGv arg1, TCGv arg2)
458 {
459     tcg_gen_add_tl(ret, arg1, arg2);
460     tcg_gen_ext32s_tl(ret, ret);
461 }
462 
463 static void gen_subw(TCGv ret, TCGv arg1, TCGv arg2)
464 {
465     tcg_gen_sub_tl(ret, arg1, arg2);
466     tcg_gen_ext32s_tl(ret, ret);
467 }
468 
469 static void gen_mulw(TCGv ret, TCGv arg1, TCGv arg2)
470 {
471     tcg_gen_mul_tl(ret, arg1, arg2);
472     tcg_gen_ext32s_tl(ret, ret);
473 }
474 
475 static bool gen_arith_div_w(DisasContext *ctx, arg_r *a,
476                             void(*func)(TCGv, TCGv, TCGv))
477 {
478     TCGv source1, source2;
479     source1 = tcg_temp_new();
480     source2 = tcg_temp_new();
481 
482     gen_get_gpr(source1, a->rs1);
483     gen_get_gpr(source2, a->rs2);
484     tcg_gen_ext32s_tl(source1, source1);
485     tcg_gen_ext32s_tl(source2, source2);
486 
487     (*func)(source1, source1, source2);
488 
489     tcg_gen_ext32s_tl(source1, source1);
490     gen_set_gpr(a->rd, source1);
491     tcg_temp_free(source1);
492     tcg_temp_free(source2);
493     return true;
494 }
495 
496 static bool gen_arith_div_uw(DisasContext *ctx, arg_r *a,
497                             void(*func)(TCGv, TCGv, TCGv))
498 {
499     TCGv source1, source2;
500     source1 = tcg_temp_new();
501     source2 = tcg_temp_new();
502 
503     gen_get_gpr(source1, a->rs1);
504     gen_get_gpr(source2, a->rs2);
505     tcg_gen_ext32u_tl(source1, source1);
506     tcg_gen_ext32u_tl(source2, source2);
507 
508     (*func)(source1, source1, source2);
509 
510     tcg_gen_ext32s_tl(source1, source1);
511     gen_set_gpr(a->rd, source1);
512     tcg_temp_free(source1);
513     tcg_temp_free(source2);
514     return true;
515 }
516 
517 static void gen_pack(TCGv ret, TCGv arg1, TCGv arg2)
518 {
519     tcg_gen_deposit_tl(ret, arg1, arg2,
520                        TARGET_LONG_BITS / 2,
521                        TARGET_LONG_BITS / 2);
522 }
523 
524 static void gen_packu(TCGv ret, TCGv arg1, TCGv arg2)
525 {
526     TCGv t = tcg_temp_new();
527     tcg_gen_shri_tl(t, arg1, TARGET_LONG_BITS / 2);
528     tcg_gen_deposit_tl(ret, arg2, t, 0, TARGET_LONG_BITS / 2);
529     tcg_temp_free(t);
530 }
531 
532 static void gen_packh(TCGv ret, TCGv arg1, TCGv arg2)
533 {
534     TCGv t = tcg_temp_new();
535     tcg_gen_ext8u_tl(t, arg2);
536     tcg_gen_deposit_tl(ret, arg1, t, 8, TARGET_LONG_BITS - 8);
537     tcg_temp_free(t);
538 }
539 
540 static void gen_sbop_mask(TCGv ret, TCGv shamt)
541 {
542     tcg_gen_movi_tl(ret, 1);
543     tcg_gen_shl_tl(ret, ret, shamt);
544 }
545 
546 static void gen_bset(TCGv ret, TCGv arg1, TCGv shamt)
547 {
548     TCGv t = tcg_temp_new();
549 
550     gen_sbop_mask(t, shamt);
551     tcg_gen_or_tl(ret, arg1, t);
552 
553     tcg_temp_free(t);
554 }
555 
556 static void gen_bclr(TCGv ret, TCGv arg1, TCGv shamt)
557 {
558     TCGv t = tcg_temp_new();
559 
560     gen_sbop_mask(t, shamt);
561     tcg_gen_andc_tl(ret, arg1, t);
562 
563     tcg_temp_free(t);
564 }
565 
566 static void gen_binv(TCGv ret, TCGv arg1, TCGv shamt)
567 {
568     TCGv t = tcg_temp_new();
569 
570     gen_sbop_mask(t, shamt);
571     tcg_gen_xor_tl(ret, arg1, t);
572 
573     tcg_temp_free(t);
574 }
575 
576 static void gen_bext(TCGv ret, TCGv arg1, TCGv shamt)
577 {
578     tcg_gen_shr_tl(ret, arg1, shamt);
579     tcg_gen_andi_tl(ret, ret, 1);
580 }
581 
582 static void gen_slo(TCGv ret, TCGv arg1, TCGv arg2)
583 {
584     tcg_gen_not_tl(ret, arg1);
585     tcg_gen_shl_tl(ret, ret, arg2);
586     tcg_gen_not_tl(ret, ret);
587 }
588 
589 static void gen_sro(TCGv ret, TCGv arg1, TCGv arg2)
590 {
591     tcg_gen_not_tl(ret, arg1);
592     tcg_gen_shr_tl(ret, ret, arg2);
593     tcg_gen_not_tl(ret, ret);
594 }
595 
596 static bool gen_grevi(DisasContext *ctx, arg_grevi *a)
597 {
598     TCGv source1 = tcg_temp_new();
599     TCGv source2;
600 
601     gen_get_gpr(source1, a->rs1);
602 
603     if (a->shamt == (TARGET_LONG_BITS - 8)) {
604         /* rev8, byte swaps */
605         tcg_gen_bswap_tl(source1, source1);
606     } else {
607         source2 = tcg_temp_new();
608         tcg_gen_movi_tl(source2, a->shamt);
609         gen_helper_grev(source1, source1, source2);
610         tcg_temp_free(source2);
611     }
612 
613     gen_set_gpr(a->rd, source1);
614     tcg_temp_free(source1);
615     return true;
616 }
617 
618 #define GEN_SHADD(SHAMT)                                       \
619 static void gen_sh##SHAMT##add(TCGv ret, TCGv arg1, TCGv arg2) \
620 {                                                              \
621     TCGv t = tcg_temp_new();                                   \
622                                                                \
623     tcg_gen_shli_tl(t, arg1, SHAMT);                           \
624     tcg_gen_add_tl(ret, t, arg2);                              \
625                                                                \
626     tcg_temp_free(t);                                          \
627 }
628 
629 GEN_SHADD(1)
630 GEN_SHADD(2)
631 GEN_SHADD(3)
632 
633 static void gen_ctzw(TCGv ret, TCGv arg1)
634 {
635     tcg_gen_ori_tl(ret, arg1, (target_ulong)MAKE_64BIT_MASK(32, 32));
636     tcg_gen_ctzi_tl(ret, ret, 64);
637 }
638 
639 static void gen_clzw(TCGv ret, TCGv arg1)
640 {
641     tcg_gen_ext32u_tl(ret, arg1);
642     tcg_gen_clzi_tl(ret, ret, 64);
643     tcg_gen_subi_tl(ret, ret, 32);
644 }
645 
646 static void gen_cpopw(TCGv ret, TCGv arg1)
647 {
648     tcg_gen_ext32u_tl(arg1, arg1);
649     tcg_gen_ctpop_tl(ret, arg1);
650 }
651 
652 static void gen_packw(TCGv ret, TCGv arg1, TCGv arg2)
653 {
654     TCGv t = tcg_temp_new();
655     tcg_gen_ext16s_tl(t, arg2);
656     tcg_gen_deposit_tl(ret, arg1, t, 16, 48);
657     tcg_temp_free(t);
658 }
659 
660 static void gen_packuw(TCGv ret, TCGv arg1, TCGv arg2)
661 {
662     TCGv t = tcg_temp_new();
663     tcg_gen_shri_tl(t, arg1, 16);
664     tcg_gen_deposit_tl(ret, arg2, t, 0, 16);
665     tcg_gen_ext32s_tl(ret, ret);
666     tcg_temp_free(t);
667 }
668 
669 static void gen_rorw(TCGv ret, TCGv arg1, TCGv arg2)
670 {
671     TCGv_i32 t1 = tcg_temp_new_i32();
672     TCGv_i32 t2 = tcg_temp_new_i32();
673 
674     /* truncate to 32-bits */
675     tcg_gen_trunc_tl_i32(t1, arg1);
676     tcg_gen_trunc_tl_i32(t2, arg2);
677 
678     tcg_gen_rotr_i32(t1, t1, t2);
679 
680     /* sign-extend 64-bits */
681     tcg_gen_ext_i32_tl(ret, t1);
682 
683     tcg_temp_free_i32(t1);
684     tcg_temp_free_i32(t2);
685 }
686 
687 static void gen_rolw(TCGv ret, TCGv arg1, TCGv arg2)
688 {
689     TCGv_i32 t1 = tcg_temp_new_i32();
690     TCGv_i32 t2 = tcg_temp_new_i32();
691 
692     /* truncate to 32-bits */
693     tcg_gen_trunc_tl_i32(t1, arg1);
694     tcg_gen_trunc_tl_i32(t2, arg2);
695 
696     tcg_gen_rotl_i32(t1, t1, t2);
697 
698     /* sign-extend 64-bits */
699     tcg_gen_ext_i32_tl(ret, t1);
700 
701     tcg_temp_free_i32(t1);
702     tcg_temp_free_i32(t2);
703 }
704 
705 static void gen_grevw(TCGv ret, TCGv arg1, TCGv arg2)
706 {
707     tcg_gen_ext32u_tl(arg1, arg1);
708     gen_helper_grev(ret, arg1, arg2);
709 }
710 
711 static void gen_gorcw(TCGv ret, TCGv arg1, TCGv arg2)
712 {
713     tcg_gen_ext32u_tl(arg1, arg1);
714     gen_helper_gorcw(ret, arg1, arg2);
715 }
716 
717 #define GEN_SHADD_UW(SHAMT)                                       \
718 static void gen_sh##SHAMT##add_uw(TCGv ret, TCGv arg1, TCGv arg2) \
719 {                                                                 \
720     TCGv t = tcg_temp_new();                                      \
721                                                                   \
722     tcg_gen_ext32u_tl(t, arg1);                                   \
723                                                                   \
724     tcg_gen_shli_tl(t, t, SHAMT);                                 \
725     tcg_gen_add_tl(ret, t, arg2);                                 \
726                                                                   \
727     tcg_temp_free(t);                                             \
728 }
729 
730 GEN_SHADD_UW(1)
731 GEN_SHADD_UW(2)
732 GEN_SHADD_UW(3)
733 
734 static void gen_add_uw(TCGv ret, TCGv arg1, TCGv arg2)
735 {
736     tcg_gen_ext32u_tl(arg1, arg1);
737     tcg_gen_add_tl(ret, arg1, arg2);
738 }
739 
740 static bool gen_arith(DisasContext *ctx, arg_r *a,
741                       void(*func)(TCGv, TCGv, TCGv))
742 {
743     TCGv source1, source2;
744     source1 = tcg_temp_new();
745     source2 = tcg_temp_new();
746 
747     gen_get_gpr(source1, a->rs1);
748     gen_get_gpr(source2, a->rs2);
749 
750     (*func)(source1, source1, source2);
751 
752     gen_set_gpr(a->rd, source1);
753     tcg_temp_free(source1);
754     tcg_temp_free(source2);
755     return true;
756 }
757 
758 static bool gen_shift(DisasContext *ctx, arg_r *a,
759                         void(*func)(TCGv, TCGv, TCGv))
760 {
761     TCGv source1 = tcg_temp_new();
762     TCGv source2 = tcg_temp_new();
763 
764     gen_get_gpr(source1, a->rs1);
765     gen_get_gpr(source2, a->rs2);
766 
767     tcg_gen_andi_tl(source2, source2, TARGET_LONG_BITS - 1);
768     (*func)(source1, source1, source2);
769 
770     gen_set_gpr(a->rd, source1);
771     tcg_temp_free(source1);
772     tcg_temp_free(source2);
773     return true;
774 }
775 
776 static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc)
777 {
778     DisasContext *ctx = container_of(dcbase, DisasContext, base);
779     CPUState *cpu = ctx->cs;
780     CPURISCVState *env = cpu->env_ptr;
781 
782     return cpu_ldl_code(env, pc);
783 }
784 
785 static bool gen_shifti(DisasContext *ctx, arg_shift *a,
786                        void(*func)(TCGv, TCGv, TCGv))
787 {
788     if (a->shamt >= TARGET_LONG_BITS) {
789         return false;
790     }
791 
792     TCGv source1 = tcg_temp_new();
793     TCGv source2 = tcg_temp_new();
794 
795     gen_get_gpr(source1, a->rs1);
796 
797     tcg_gen_movi_tl(source2, a->shamt);
798     (*func)(source1, source1, source2);
799 
800     gen_set_gpr(a->rd, source1);
801     tcg_temp_free(source1);
802     tcg_temp_free(source2);
803     return true;
804 }
805 
806 static bool gen_shiftw(DisasContext *ctx, arg_r *a,
807                        void(*func)(TCGv, TCGv, TCGv))
808 {
809     TCGv source1 = tcg_temp_new();
810     TCGv source2 = tcg_temp_new();
811 
812     gen_get_gpr(source1, a->rs1);
813     gen_get_gpr(source2, a->rs2);
814 
815     tcg_gen_andi_tl(source2, source2, 31);
816     (*func)(source1, source1, source2);
817     tcg_gen_ext32s_tl(source1, source1);
818 
819     gen_set_gpr(a->rd, source1);
820     tcg_temp_free(source1);
821     tcg_temp_free(source2);
822     return true;
823 }
824 
825 static bool gen_shiftiw(DisasContext *ctx, arg_shift *a,
826                         void(*func)(TCGv, TCGv, TCGv))
827 {
828     TCGv source1 = tcg_temp_new();
829     TCGv source2 = tcg_temp_new();
830 
831     gen_get_gpr(source1, a->rs1);
832     tcg_gen_movi_tl(source2, a->shamt);
833 
834     (*func)(source1, source1, source2);
835     tcg_gen_ext32s_tl(source1, source1);
836 
837     gen_set_gpr(a->rd, source1);
838     tcg_temp_free(source1);
839     tcg_temp_free(source2);
840     return true;
841 }
842 
843 static void gen_ctz(TCGv ret, TCGv arg1)
844 {
845     tcg_gen_ctzi_tl(ret, arg1, TARGET_LONG_BITS);
846 }
847 
848 static void gen_clz(TCGv ret, TCGv arg1)
849 {
850     tcg_gen_clzi_tl(ret, arg1, TARGET_LONG_BITS);
851 }
852 
853 static bool gen_unary(DisasContext *ctx, arg_r2 *a,
854                       void(*func)(TCGv, TCGv))
855 {
856     TCGv source = tcg_temp_new();
857 
858     gen_get_gpr(source, a->rs1);
859 
860     (*func)(source, source);
861 
862     gen_set_gpr(a->rd, source);
863     tcg_temp_free(source);
864     return true;
865 }
866 
867 /* Include insn module translation function */
868 #include "insn_trans/trans_rvi.c.inc"
869 #include "insn_trans/trans_rvm.c.inc"
870 #include "insn_trans/trans_rva.c.inc"
871 #include "insn_trans/trans_rvf.c.inc"
872 #include "insn_trans/trans_rvd.c.inc"
873 #include "insn_trans/trans_rvh.c.inc"
874 #include "insn_trans/trans_rvv.c.inc"
875 #include "insn_trans/trans_rvb.c.inc"
876 #include "insn_trans/trans_privileged.c.inc"
877 
878 /* Include the auto-generated decoder for 16 bit insn */
879 #include "decode-insn16.c.inc"
880 
881 static void decode_opc(CPURISCVState *env, DisasContext *ctx, uint16_t opcode)
882 {
883     /* check for compressed insn */
884     if (extract16(opcode, 0, 2) != 3) {
885         if (!has_ext(ctx, RVC)) {
886             gen_exception_illegal(ctx);
887         } else {
888             ctx->pc_succ_insn = ctx->base.pc_next + 2;
889             if (!decode_insn16(ctx, opcode)) {
890                 gen_exception_illegal(ctx);
891             }
892         }
893     } else {
894         uint32_t opcode32 = opcode;
895         opcode32 = deposit32(opcode32, 16, 16,
896                              translator_lduw(env, ctx->base.pc_next + 2));
897         ctx->pc_succ_insn = ctx->base.pc_next + 4;
898         if (!decode_insn32(ctx, opcode32)) {
899             gen_exception_illegal(ctx);
900         }
901     }
902 }
903 
904 static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
905 {
906     DisasContext *ctx = container_of(dcbase, DisasContext, base);
907     CPURISCVState *env = cs->env_ptr;
908     RISCVCPU *cpu = RISCV_CPU(cs);
909     uint32_t tb_flags = ctx->base.tb->flags;
910 
911     ctx->pc_succ_insn = ctx->base.pc_first;
912     ctx->mem_idx = tb_flags & TB_FLAGS_MMU_MASK;
913     ctx->mstatus_fs = tb_flags & TB_FLAGS_MSTATUS_FS;
914     ctx->priv_ver = env->priv_ver;
915 #if !defined(CONFIG_USER_ONLY)
916     if (riscv_has_ext(env, RVH)) {
917         ctx->virt_enabled = riscv_cpu_virt_enabled(env);
918     } else {
919         ctx->virt_enabled = false;
920     }
921 #else
922     ctx->virt_enabled = false;
923 #endif
924     ctx->misa = env->misa;
925     ctx->frm = -1;  /* unknown rounding mode */
926     ctx->ext_ifencei = cpu->cfg.ext_ifencei;
927     ctx->vlen = cpu->cfg.vlen;
928     ctx->hlsx = FIELD_EX32(tb_flags, TB_FLAGS, HLSX);
929     ctx->vill = FIELD_EX32(tb_flags, TB_FLAGS, VILL);
930     ctx->sew = FIELD_EX32(tb_flags, TB_FLAGS, SEW);
931     ctx->lmul = FIELD_EX32(tb_flags, TB_FLAGS, LMUL);
932     ctx->mlen = 1 << (ctx->sew  + 3 - ctx->lmul);
933     ctx->vl_eq_vlmax = FIELD_EX32(tb_flags, TB_FLAGS, VL_EQ_VLMAX);
934     ctx->cs = cs;
935 }
936 
937 static void riscv_tr_tb_start(DisasContextBase *db, CPUState *cpu)
938 {
939 }
940 
941 static void riscv_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
942 {
943     DisasContext *ctx = container_of(dcbase, DisasContext, base);
944 
945     tcg_gen_insn_start(ctx->base.pc_next);
946 }
947 
948 static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
949 {
950     DisasContext *ctx = container_of(dcbase, DisasContext, base);
951     CPURISCVState *env = cpu->env_ptr;
952     uint16_t opcode16 = translator_lduw(env, ctx->base.pc_next);
953 
954     decode_opc(env, ctx, opcode16);
955     ctx->base.pc_next = ctx->pc_succ_insn;
956 
957     if (ctx->base.is_jmp == DISAS_NEXT) {
958         target_ulong page_start;
959 
960         page_start = ctx->base.pc_first & TARGET_PAGE_MASK;
961         if (ctx->base.pc_next - page_start >= TARGET_PAGE_SIZE) {
962             ctx->base.is_jmp = DISAS_TOO_MANY;
963         }
964     }
965 }
966 
967 static void riscv_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
968 {
969     DisasContext *ctx = container_of(dcbase, DisasContext, base);
970 
971     switch (ctx->base.is_jmp) {
972     case DISAS_TOO_MANY:
973         gen_goto_tb(ctx, 0, ctx->base.pc_next);
974         break;
975     case DISAS_NORETURN:
976         break;
977     default:
978         g_assert_not_reached();
979     }
980 }
981 
982 static void riscv_tr_disas_log(const DisasContextBase *dcbase, CPUState *cpu)
983 {
984 #ifndef CONFIG_USER_ONLY
985     RISCVCPU *rvcpu = RISCV_CPU(cpu);
986     CPURISCVState *env = &rvcpu->env;
987 #endif
988 
989     qemu_log("IN: %s\n", lookup_symbol(dcbase->pc_first));
990 #ifndef CONFIG_USER_ONLY
991     qemu_log("Priv: "TARGET_FMT_ld"; Virt: "TARGET_FMT_ld"\n", env->priv, env->virt);
992 #endif
993     log_target_disas(cpu, dcbase->pc_first, dcbase->tb->size);
994 }
995 
996 static const TranslatorOps riscv_tr_ops = {
997     .init_disas_context = riscv_tr_init_disas_context,
998     .tb_start           = riscv_tr_tb_start,
999     .insn_start         = riscv_tr_insn_start,
1000     .translate_insn     = riscv_tr_translate_insn,
1001     .tb_stop            = riscv_tr_tb_stop,
1002     .disas_log          = riscv_tr_disas_log,
1003 };
1004 
1005 void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
1006 {
1007     DisasContext ctx;
1008 
1009     translator_loop(&riscv_tr_ops, &ctx.base, cs, tb, max_insns);
1010 }
1011 
1012 void riscv_translate_init(void)
1013 {
1014     int i;
1015 
1016     /* cpu_gpr[0] is a placeholder for the zero register. Do not use it. */
1017     /* Use the gen_set_gpr and gen_get_gpr helper functions when accessing */
1018     /* registers, unless you specifically block reads/writes to reg 0 */
1019     cpu_gpr[0] = NULL;
1020 
1021     for (i = 1; i < 32; i++) {
1022         cpu_gpr[i] = tcg_global_mem_new(cpu_env,
1023             offsetof(CPURISCVState, gpr[i]), riscv_int_regnames[i]);
1024     }
1025 
1026     for (i = 0; i < 32; i++) {
1027         cpu_fpr[i] = tcg_global_mem_new_i64(cpu_env,
1028             offsetof(CPURISCVState, fpr[i]), riscv_fpr_regnames[i]);
1029     }
1030 
1031     cpu_pc = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, pc), "pc");
1032     cpu_vl = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, vl), "vl");
1033     load_res = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, load_res),
1034                              "load_res");
1035     load_val = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, load_val),
1036                              "load_val");
1037 }
1038