xref: /openbmc/qemu/target/riscv/translate.c (revision 3ceeb19a)
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 #include "semihosting/semihost.h"
32 
33 #include "instmap.h"
34 #include "internals.h"
35 
36 /* global register indices */
37 static TCGv cpu_gpr[32], cpu_gprh[32], cpu_pc, cpu_vl, cpu_vstart;
38 static TCGv_i64 cpu_fpr[32]; /* assume F and D extensions */
39 static TCGv load_res;
40 static TCGv load_val;
41 /* globals for PM CSRs */
42 static TCGv pm_mask;
43 static TCGv pm_base;
44 
45 #include "exec/gen-icount.h"
46 
47 /*
48  * If an operation is being performed on less than TARGET_LONG_BITS,
49  * it may require the inputs to be sign- or zero-extended; which will
50  * depend on the exact operation being performed.
51  */
52 typedef enum {
53     EXT_NONE,
54     EXT_SIGN,
55     EXT_ZERO,
56 } DisasExtend;
57 
58 typedef struct DisasContext {
59     DisasContextBase base;
60     /* pc_succ_insn points to the instruction following base.pc_next */
61     target_ulong pc_succ_insn;
62     target_ulong priv_ver;
63     RISCVMXL misa_mxl_max;
64     RISCVMXL xl;
65     uint32_t misa_ext;
66     uint32_t opcode;
67     uint32_t mstatus_fs;
68     uint32_t mstatus_vs;
69     uint32_t mstatus_hs_fs;
70     uint32_t mstatus_hs_vs;
71     uint32_t mem_idx;
72     /* Remember the rounding mode encoded in the previous fp instruction,
73        which we have already installed into env->fp_status.  Or -1 for
74        no previous fp instruction.  Note that we exit the TB when writing
75        to any system register, which includes CSR_FRM, so we do not have
76        to reset this known value.  */
77     int frm;
78     RISCVMXL ol;
79     bool virt_inst_excp;
80     bool virt_enabled;
81     const RISCVCPUConfig *cfg_ptr;
82     bool hlsx;
83     /* vector extension */
84     bool vill;
85     /*
86      * Encode LMUL to lmul as follows:
87      *     LMUL    vlmul    lmul
88      *      1       000       0
89      *      2       001       1
90      *      4       010       2
91      *      8       011       3
92      *      -       100       -
93      *     1/8      101      -3
94      *     1/4      110      -2
95      *     1/2      111      -1
96      */
97     int8_t lmul;
98     uint8_t sew;
99     uint8_t vta;
100     uint8_t vma;
101     bool cfg_vta_all_1s;
102     target_ulong vstart;
103     bool vl_eq_vlmax;
104     uint8_t ntemp;
105     CPUState *cs;
106     TCGv zero;
107     /* Space for 3 operands plus 1 extra for address computation. */
108     TCGv temp[4];
109     /* Space for 4 operands(1 dest and <=3 src) for float point computation */
110     TCGv_i64 ftemp[4];
111     uint8_t nftemp;
112     /* PointerMasking extension */
113     bool pm_mask_enabled;
114     bool pm_base_enabled;
115     /* Use icount trigger for native debug */
116     bool itrigger;
117     /* FRM is known to contain a valid value. */
118     bool frm_valid;
119     /* TCG of the current insn_start */
120     TCGOp *insn_start;
121 } DisasContext;
122 
123 static inline bool has_ext(DisasContext *ctx, uint32_t ext)
124 {
125     return ctx->misa_ext & ext;
126 }
127 
128 static bool always_true_p(DisasContext *ctx  __attribute__((__unused__)))
129 {
130     return true;
131 }
132 
133 #define MATERIALISE_EXT_PREDICATE(ext)  \
134     static bool has_ ## ext ## _p(DisasContext *ctx)    \
135     { \
136         return ctx->cfg_ptr->ext_ ## ext ; \
137     }
138 
139 MATERIALISE_EXT_PREDICATE(XVentanaCondOps);
140 
141 #ifdef TARGET_RISCV32
142 #define get_xl(ctx)    MXL_RV32
143 #elif defined(CONFIG_USER_ONLY)
144 #define get_xl(ctx)    MXL_RV64
145 #else
146 #define get_xl(ctx)    ((ctx)->xl)
147 #endif
148 
149 /* The word size for this machine mode. */
150 static inline int __attribute__((unused)) get_xlen(DisasContext *ctx)
151 {
152     return 16 << get_xl(ctx);
153 }
154 
155 /* The operation length, as opposed to the xlen. */
156 #ifdef TARGET_RISCV32
157 #define get_ol(ctx)    MXL_RV32
158 #else
159 #define get_ol(ctx)    ((ctx)->ol)
160 #endif
161 
162 static inline int get_olen(DisasContext *ctx)
163 {
164     return 16 << get_ol(ctx);
165 }
166 
167 /* The maximum register length */
168 #ifdef TARGET_RISCV32
169 #define get_xl_max(ctx)    MXL_RV32
170 #else
171 #define get_xl_max(ctx)    ((ctx)->misa_mxl_max)
172 #endif
173 
174 /*
175  * RISC-V requires NaN-boxing of narrower width floating point values.
176  * This applies when a 32-bit value is assigned to a 64-bit FP register.
177  * For consistency and simplicity, we nanbox results even when the RVD
178  * extension is not present.
179  */
180 static void gen_nanbox_s(TCGv_i64 out, TCGv_i64 in)
181 {
182     tcg_gen_ori_i64(out, in, MAKE_64BIT_MASK(32, 32));
183 }
184 
185 static void gen_nanbox_h(TCGv_i64 out, TCGv_i64 in)
186 {
187     tcg_gen_ori_i64(out, in, MAKE_64BIT_MASK(16, 48));
188 }
189 
190 /*
191  * A narrow n-bit operation, where n < FLEN, checks that input operands
192  * are correctly Nan-boxed, i.e., all upper FLEN - n bits are 1.
193  * If so, the least-significant bits of the input are used, otherwise the
194  * input value is treated as an n-bit canonical NaN (v2.2 section 9.2).
195  *
196  * Here, the result is always nan-boxed, even the canonical nan.
197  */
198 static void gen_check_nanbox_h(TCGv_i64 out, TCGv_i64 in)
199 {
200     TCGv_i64 t_max = tcg_const_i64(0xffffffffffff0000ull);
201     TCGv_i64 t_nan = tcg_const_i64(0xffffffffffff7e00ull);
202 
203     tcg_gen_movcond_i64(TCG_COND_GEU, out, in, t_max, in, t_nan);
204     tcg_temp_free_i64(t_max);
205     tcg_temp_free_i64(t_nan);
206 }
207 
208 static void gen_check_nanbox_s(TCGv_i64 out, TCGv_i64 in)
209 {
210     TCGv_i64 t_max = tcg_constant_i64(0xffffffff00000000ull);
211     TCGv_i64 t_nan = tcg_constant_i64(0xffffffff7fc00000ull);
212 
213     tcg_gen_movcond_i64(TCG_COND_GEU, out, in, t_max, in, t_nan);
214 }
215 
216 static void decode_save_opc(DisasContext *ctx)
217 {
218     assert(ctx->insn_start != NULL);
219     tcg_set_insn_start_param(ctx->insn_start, 1, ctx->opcode);
220     ctx->insn_start = NULL;
221 }
222 
223 static void gen_set_pc_imm(DisasContext *ctx, target_ulong dest)
224 {
225     if (get_xl(ctx) == MXL_RV32) {
226         dest = (int32_t)dest;
227     }
228     tcg_gen_movi_tl(cpu_pc, dest);
229 }
230 
231 static void gen_set_pc(DisasContext *ctx, TCGv dest)
232 {
233     if (get_xl(ctx) == MXL_RV32) {
234         tcg_gen_ext32s_tl(cpu_pc, dest);
235     } else {
236         tcg_gen_mov_tl(cpu_pc, dest);
237     }
238 }
239 
240 static void generate_exception(DisasContext *ctx, int excp)
241 {
242     gen_set_pc_imm(ctx, ctx->base.pc_next);
243     gen_helper_raise_exception(cpu_env, tcg_constant_i32(excp));
244     ctx->base.is_jmp = DISAS_NORETURN;
245 }
246 
247 static void gen_exception_illegal(DisasContext *ctx)
248 {
249     tcg_gen_st_i32(tcg_constant_i32(ctx->opcode), cpu_env,
250                    offsetof(CPURISCVState, bins));
251     if (ctx->virt_inst_excp) {
252         generate_exception(ctx, RISCV_EXCP_VIRT_INSTRUCTION_FAULT);
253     } else {
254         generate_exception(ctx, RISCV_EXCP_ILLEGAL_INST);
255     }
256 }
257 
258 static void gen_exception_inst_addr_mis(DisasContext *ctx)
259 {
260     tcg_gen_st_tl(cpu_pc, cpu_env, offsetof(CPURISCVState, badaddr));
261     generate_exception(ctx, RISCV_EXCP_INST_ADDR_MIS);
262 }
263 
264 static void lookup_and_goto_ptr(DisasContext *ctx)
265 {
266 #ifndef CONFIG_USER_ONLY
267     if (ctx->itrigger) {
268         gen_helper_itrigger_match(cpu_env);
269     }
270 #endif
271     tcg_gen_lookup_and_goto_ptr();
272 }
273 
274 static void exit_tb(DisasContext *ctx)
275 {
276 #ifndef CONFIG_USER_ONLY
277     if (ctx->itrigger) {
278         gen_helper_itrigger_match(cpu_env);
279     }
280 #endif
281     tcg_gen_exit_tb(NULL, 0);
282 }
283 
284 static void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
285 {
286      /*
287       * Under itrigger, instruction executes one by one like singlestep,
288       * direct block chain benefits will be small.
289       */
290     if (translator_use_goto_tb(&ctx->base, dest) && !ctx->itrigger) {
291         tcg_gen_goto_tb(n);
292         gen_set_pc_imm(ctx, dest);
293         tcg_gen_exit_tb(ctx->base.tb, n);
294     } else {
295         gen_set_pc_imm(ctx, dest);
296         lookup_and_goto_ptr(ctx);
297     }
298 }
299 
300 /*
301  * Wrappers for getting reg values.
302  *
303  * The $zero register does not have cpu_gpr[0] allocated -- we supply the
304  * constant zero as a source, and an uninitialized sink as destination.
305  *
306  * Further, we may provide an extension for word operations.
307  */
308 static TCGv temp_new(DisasContext *ctx)
309 {
310     assert(ctx->ntemp < ARRAY_SIZE(ctx->temp));
311     return ctx->temp[ctx->ntemp++] = tcg_temp_new();
312 }
313 
314 static TCGv get_gpr(DisasContext *ctx, int reg_num, DisasExtend ext)
315 {
316     TCGv t;
317 
318     if (reg_num == 0) {
319         return ctx->zero;
320     }
321 
322     switch (get_ol(ctx)) {
323     case MXL_RV32:
324         switch (ext) {
325         case EXT_NONE:
326             break;
327         case EXT_SIGN:
328             t = temp_new(ctx);
329             tcg_gen_ext32s_tl(t, cpu_gpr[reg_num]);
330             return t;
331         case EXT_ZERO:
332             t = temp_new(ctx);
333             tcg_gen_ext32u_tl(t, cpu_gpr[reg_num]);
334             return t;
335         default:
336             g_assert_not_reached();
337         }
338         break;
339     case MXL_RV64:
340     case MXL_RV128:
341         break;
342     default:
343         g_assert_not_reached();
344     }
345     return cpu_gpr[reg_num];
346 }
347 
348 static TCGv get_gprh(DisasContext *ctx, int reg_num)
349 {
350     assert(get_xl(ctx) == MXL_RV128);
351     if (reg_num == 0) {
352         return ctx->zero;
353     }
354     return cpu_gprh[reg_num];
355 }
356 
357 static TCGv dest_gpr(DisasContext *ctx, int reg_num)
358 {
359     if (reg_num == 0 || get_olen(ctx) < TARGET_LONG_BITS) {
360         return temp_new(ctx);
361     }
362     return cpu_gpr[reg_num];
363 }
364 
365 static TCGv dest_gprh(DisasContext *ctx, int reg_num)
366 {
367     if (reg_num == 0) {
368         return temp_new(ctx);
369     }
370     return cpu_gprh[reg_num];
371 }
372 
373 static void gen_set_gpr(DisasContext *ctx, int reg_num, TCGv t)
374 {
375     if (reg_num != 0) {
376         switch (get_ol(ctx)) {
377         case MXL_RV32:
378             tcg_gen_ext32s_tl(cpu_gpr[reg_num], t);
379             break;
380         case MXL_RV64:
381         case MXL_RV128:
382             tcg_gen_mov_tl(cpu_gpr[reg_num], t);
383             break;
384         default:
385             g_assert_not_reached();
386         }
387 
388         if (get_xl_max(ctx) == MXL_RV128) {
389             tcg_gen_sari_tl(cpu_gprh[reg_num], cpu_gpr[reg_num], 63);
390         }
391     }
392 }
393 
394 static void gen_set_gpri(DisasContext *ctx, int reg_num, target_long imm)
395 {
396     if (reg_num != 0) {
397         switch (get_ol(ctx)) {
398         case MXL_RV32:
399             tcg_gen_movi_tl(cpu_gpr[reg_num], (int32_t)imm);
400             break;
401         case MXL_RV64:
402         case MXL_RV128:
403             tcg_gen_movi_tl(cpu_gpr[reg_num], imm);
404             break;
405         default:
406             g_assert_not_reached();
407         }
408 
409         if (get_xl_max(ctx) == MXL_RV128) {
410             tcg_gen_movi_tl(cpu_gprh[reg_num], -(imm < 0));
411         }
412     }
413 }
414 
415 static void gen_set_gpr128(DisasContext *ctx, int reg_num, TCGv rl, TCGv rh)
416 {
417     assert(get_ol(ctx) == MXL_RV128);
418     if (reg_num != 0) {
419         tcg_gen_mov_tl(cpu_gpr[reg_num], rl);
420         tcg_gen_mov_tl(cpu_gprh[reg_num], rh);
421     }
422 }
423 
424 static TCGv_i64 ftemp_new(DisasContext *ctx)
425 {
426     assert(ctx->nftemp < ARRAY_SIZE(ctx->ftemp));
427     return ctx->ftemp[ctx->nftemp++] = tcg_temp_new_i64();
428 }
429 
430 static TCGv_i64 get_fpr_hs(DisasContext *ctx, int reg_num)
431 {
432     if (!ctx->cfg_ptr->ext_zfinx) {
433         return cpu_fpr[reg_num];
434     }
435 
436     if (reg_num == 0) {
437         return tcg_constant_i64(0);
438     }
439     switch (get_xl(ctx)) {
440     case MXL_RV32:
441 #ifdef TARGET_RISCV32
442     {
443         TCGv_i64 t = ftemp_new(ctx);
444         tcg_gen_ext_i32_i64(t, cpu_gpr[reg_num]);
445         return t;
446     }
447 #else
448     /* fall through */
449     case MXL_RV64:
450         return cpu_gpr[reg_num];
451 #endif
452     default:
453         g_assert_not_reached();
454     }
455 }
456 
457 static TCGv_i64 get_fpr_d(DisasContext *ctx, int reg_num)
458 {
459     if (!ctx->cfg_ptr->ext_zfinx) {
460         return cpu_fpr[reg_num];
461     }
462 
463     if (reg_num == 0) {
464         return tcg_constant_i64(0);
465     }
466     switch (get_xl(ctx)) {
467     case MXL_RV32:
468     {
469         TCGv_i64 t = ftemp_new(ctx);
470         tcg_gen_concat_tl_i64(t, cpu_gpr[reg_num], cpu_gpr[reg_num + 1]);
471         return t;
472     }
473 #ifdef TARGET_RISCV64
474     case MXL_RV64:
475         return cpu_gpr[reg_num];
476 #endif
477     default:
478         g_assert_not_reached();
479     }
480 }
481 
482 static TCGv_i64 dest_fpr(DisasContext *ctx, int reg_num)
483 {
484     if (!ctx->cfg_ptr->ext_zfinx) {
485         return cpu_fpr[reg_num];
486     }
487 
488     if (reg_num == 0) {
489         return ftemp_new(ctx);
490     }
491 
492     switch (get_xl(ctx)) {
493     case MXL_RV32:
494         return ftemp_new(ctx);
495 #ifdef TARGET_RISCV64
496     case MXL_RV64:
497         return cpu_gpr[reg_num];
498 #endif
499     default:
500         g_assert_not_reached();
501     }
502 }
503 
504 /* assume t is nanboxing (for normal) or sign-extended (for zfinx) */
505 static void gen_set_fpr_hs(DisasContext *ctx, int reg_num, TCGv_i64 t)
506 {
507     if (!ctx->cfg_ptr->ext_zfinx) {
508         tcg_gen_mov_i64(cpu_fpr[reg_num], t);
509         return;
510     }
511     if (reg_num != 0) {
512         switch (get_xl(ctx)) {
513         case MXL_RV32:
514 #ifdef TARGET_RISCV32
515             tcg_gen_extrl_i64_i32(cpu_gpr[reg_num], t);
516             break;
517 #else
518         /* fall through */
519         case MXL_RV64:
520             tcg_gen_mov_i64(cpu_gpr[reg_num], t);
521             break;
522 #endif
523         default:
524             g_assert_not_reached();
525         }
526     }
527 }
528 
529 static void gen_set_fpr_d(DisasContext *ctx, int reg_num, TCGv_i64 t)
530 {
531     if (!ctx->cfg_ptr->ext_zfinx) {
532         tcg_gen_mov_i64(cpu_fpr[reg_num], t);
533         return;
534     }
535 
536     if (reg_num != 0) {
537         switch (get_xl(ctx)) {
538         case MXL_RV32:
539 #ifdef TARGET_RISCV32
540             tcg_gen_extr_i64_i32(cpu_gpr[reg_num], cpu_gpr[reg_num + 1], t);
541             break;
542 #else
543             tcg_gen_ext32s_i64(cpu_gpr[reg_num], t);
544             tcg_gen_sari_i64(cpu_gpr[reg_num + 1], t, 32);
545             break;
546         case MXL_RV64:
547             tcg_gen_mov_i64(cpu_gpr[reg_num], t);
548             break;
549 #endif
550         default:
551             g_assert_not_reached();
552         }
553     }
554 }
555 
556 static void gen_jal(DisasContext *ctx, int rd, target_ulong imm)
557 {
558     target_ulong next_pc;
559 
560     /* check misaligned: */
561     next_pc = ctx->base.pc_next + imm;
562     if (!has_ext(ctx, RVC)) {
563         if ((next_pc & 0x3) != 0) {
564             gen_exception_inst_addr_mis(ctx);
565             return;
566         }
567     }
568 
569     gen_set_gpri(ctx, rd, ctx->pc_succ_insn);
570     gen_goto_tb(ctx, 0, ctx->base.pc_next + imm); /* must use this for safety */
571     ctx->base.is_jmp = DISAS_NORETURN;
572 }
573 
574 /* Compute a canonical address from a register plus offset. */
575 static TCGv get_address(DisasContext *ctx, int rs1, int imm)
576 {
577     TCGv addr = temp_new(ctx);
578     TCGv src1 = get_gpr(ctx, rs1, EXT_NONE);
579 
580     tcg_gen_addi_tl(addr, src1, imm);
581     if (ctx->pm_mask_enabled) {
582         tcg_gen_andc_tl(addr, addr, pm_mask);
583     } else if (get_xl(ctx) == MXL_RV32) {
584         tcg_gen_ext32u_tl(addr, addr);
585     }
586     if (ctx->pm_base_enabled) {
587         tcg_gen_or_tl(addr, addr, pm_base);
588     }
589     return addr;
590 }
591 
592 #ifndef CONFIG_USER_ONLY
593 /* The states of mstatus_fs are:
594  * 0 = disabled, 1 = initial, 2 = clean, 3 = dirty
595  * We will have already diagnosed disabled state,
596  * and need to turn initial/clean into dirty.
597  */
598 static void mark_fs_dirty(DisasContext *ctx)
599 {
600     TCGv tmp;
601 
602     if (!has_ext(ctx, RVF)) {
603         return;
604     }
605 
606     if (ctx->mstatus_fs != MSTATUS_FS) {
607         /* Remember the state change for the rest of the TB. */
608         ctx->mstatus_fs = MSTATUS_FS;
609 
610         tmp = tcg_temp_new();
611         tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
612         tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS);
613         tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
614         tcg_temp_free(tmp);
615     }
616 
617     if (ctx->virt_enabled && ctx->mstatus_hs_fs != MSTATUS_FS) {
618         /* Remember the stage change for the rest of the TB. */
619         ctx->mstatus_hs_fs = MSTATUS_FS;
620 
621         tmp = tcg_temp_new();
622         tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus_hs));
623         tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS);
624         tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus_hs));
625         tcg_temp_free(tmp);
626     }
627 }
628 #else
629 static inline void mark_fs_dirty(DisasContext *ctx) { }
630 #endif
631 
632 #ifndef CONFIG_USER_ONLY
633 /* The states of mstatus_vs are:
634  * 0 = disabled, 1 = initial, 2 = clean, 3 = dirty
635  * We will have already diagnosed disabled state,
636  * and need to turn initial/clean into dirty.
637  */
638 static void mark_vs_dirty(DisasContext *ctx)
639 {
640     TCGv tmp;
641 
642     if (ctx->mstatus_vs != MSTATUS_VS) {
643         /* Remember the state change for the rest of the TB.  */
644         ctx->mstatus_vs = MSTATUS_VS;
645 
646         tmp = tcg_temp_new();
647         tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
648         tcg_gen_ori_tl(tmp, tmp, MSTATUS_VS);
649         tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus));
650         tcg_temp_free(tmp);
651     }
652 
653     if (ctx->virt_enabled && ctx->mstatus_hs_vs != MSTATUS_VS) {
654         /* Remember the stage change for the rest of the TB. */
655         ctx->mstatus_hs_vs = MSTATUS_VS;
656 
657         tmp = tcg_temp_new();
658         tcg_gen_ld_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus_hs));
659         tcg_gen_ori_tl(tmp, tmp, MSTATUS_VS);
660         tcg_gen_st_tl(tmp, cpu_env, offsetof(CPURISCVState, mstatus_hs));
661         tcg_temp_free(tmp);
662     }
663 }
664 #else
665 static inline void mark_vs_dirty(DisasContext *ctx) { }
666 #endif
667 
668 static void gen_set_rm(DisasContext *ctx, int rm)
669 {
670     if (ctx->frm == rm) {
671         return;
672     }
673     ctx->frm = rm;
674 
675     if (rm == RISCV_FRM_ROD) {
676         gen_helper_set_rod_rounding_mode(cpu_env);
677         return;
678     }
679     if (rm == RISCV_FRM_DYN) {
680         /* The helper will return only if frm valid. */
681         ctx->frm_valid = true;
682     }
683 
684     /* The helper may raise ILLEGAL_INSN -- record binv for unwind. */
685     decode_save_opc(ctx);
686     gen_helper_set_rounding_mode(cpu_env, tcg_constant_i32(rm));
687 }
688 
689 static void gen_set_rm_chkfrm(DisasContext *ctx, int rm)
690 {
691     if (ctx->frm == rm && ctx->frm_valid) {
692         return;
693     }
694     ctx->frm = rm;
695     ctx->frm_valid = true;
696 
697     /* The helper may raise ILLEGAL_INSN -- record binv for unwind. */
698     decode_save_opc(ctx);
699     gen_helper_set_rounding_mode_chkfrm(cpu_env, tcg_constant_i32(rm));
700 }
701 
702 static int ex_plus_1(DisasContext *ctx, int nf)
703 {
704     return nf + 1;
705 }
706 
707 #define EX_SH(amount) \
708     static int ex_shift_##amount(DisasContext *ctx, int imm) \
709     {                                         \
710         return imm << amount;                 \
711     }
712 EX_SH(1)
713 EX_SH(2)
714 EX_SH(3)
715 EX_SH(4)
716 EX_SH(12)
717 
718 #define REQUIRE_EXT(ctx, ext) do { \
719     if (!has_ext(ctx, ext)) {      \
720         return false;              \
721     }                              \
722 } while (0)
723 
724 #define REQUIRE_32BIT(ctx) do {    \
725     if (get_xl(ctx) != MXL_RV32) { \
726         return false;              \
727     }                              \
728 } while (0)
729 
730 #define REQUIRE_64BIT(ctx) do {     \
731     if (get_xl(ctx) != MXL_RV64) {  \
732         return false;               \
733     }                               \
734 } while (0)
735 
736 #define REQUIRE_128BIT(ctx) do {    \
737     if (get_xl(ctx) != MXL_RV128) { \
738         return false;               \
739     }                               \
740 } while (0)
741 
742 #define REQUIRE_64_OR_128BIT(ctx) do { \
743     if (get_xl(ctx) == MXL_RV32) {     \
744         return false;                  \
745     }                                  \
746 } while (0)
747 
748 #define REQUIRE_EITHER_EXT(ctx, A, B) do {       \
749     if (!ctx->cfg_ptr->ext_##A &&      \
750         !ctx->cfg_ptr->ext_##B) {      \
751         return false;                            \
752     }                                            \
753 } while (0)
754 
755 static int ex_rvc_register(DisasContext *ctx, int reg)
756 {
757     return 8 + reg;
758 }
759 
760 static int ex_rvc_shiftli(DisasContext *ctx, int imm)
761 {
762     /* For RV128 a shamt of 0 means a shift by 64. */
763     if (get_ol(ctx) == MXL_RV128) {
764         imm = imm ? imm : 64;
765     }
766     return imm;
767 }
768 
769 static int ex_rvc_shiftri(DisasContext *ctx, int imm)
770 {
771     /*
772      * For RV128 a shamt of 0 means a shift by 64, furthermore, for right
773      * shifts, the shamt is sign-extended.
774      */
775     if (get_ol(ctx) == MXL_RV128) {
776         imm = imm | (imm & 32) << 1;
777         imm = imm ? imm : 64;
778     }
779     return imm;
780 }
781 
782 /* Include the auto-generated decoder for 32 bit insn */
783 #include "decode-insn32.c.inc"
784 
785 static bool gen_logic_imm_fn(DisasContext *ctx, arg_i *a,
786                              void (*func)(TCGv, TCGv, target_long))
787 {
788     TCGv dest = dest_gpr(ctx, a->rd);
789     TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE);
790 
791     func(dest, src1, a->imm);
792 
793     if (get_xl(ctx) == MXL_RV128) {
794         TCGv src1h = get_gprh(ctx, a->rs1);
795         TCGv desth = dest_gprh(ctx, a->rd);
796 
797         func(desth, src1h, -(a->imm < 0));
798         gen_set_gpr128(ctx, a->rd, dest, desth);
799     } else {
800         gen_set_gpr(ctx, a->rd, dest);
801     }
802 
803     return true;
804 }
805 
806 static bool gen_logic(DisasContext *ctx, arg_r *a,
807                       void (*func)(TCGv, TCGv, TCGv))
808 {
809     TCGv dest = dest_gpr(ctx, a->rd);
810     TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE);
811     TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE);
812 
813     func(dest, src1, src2);
814 
815     if (get_xl(ctx) == MXL_RV128) {
816         TCGv src1h = get_gprh(ctx, a->rs1);
817         TCGv src2h = get_gprh(ctx, a->rs2);
818         TCGv desth = dest_gprh(ctx, a->rd);
819 
820         func(desth, src1h, src2h);
821         gen_set_gpr128(ctx, a->rd, dest, desth);
822     } else {
823         gen_set_gpr(ctx, a->rd, dest);
824     }
825 
826     return true;
827 }
828 
829 static bool gen_arith_imm_fn(DisasContext *ctx, arg_i *a, DisasExtend ext,
830                              void (*func)(TCGv, TCGv, target_long),
831                              void (*f128)(TCGv, TCGv, TCGv, TCGv, target_long))
832 {
833     TCGv dest = dest_gpr(ctx, a->rd);
834     TCGv src1 = get_gpr(ctx, a->rs1, ext);
835 
836     if (get_ol(ctx) < MXL_RV128) {
837         func(dest, src1, a->imm);
838         gen_set_gpr(ctx, a->rd, dest);
839     } else {
840         if (f128 == NULL) {
841             return false;
842         }
843 
844         TCGv src1h = get_gprh(ctx, a->rs1);
845         TCGv desth = dest_gprh(ctx, a->rd);
846 
847         f128(dest, desth, src1, src1h, a->imm);
848         gen_set_gpr128(ctx, a->rd, dest, desth);
849     }
850     return true;
851 }
852 
853 static bool gen_arith_imm_tl(DisasContext *ctx, arg_i *a, DisasExtend ext,
854                              void (*func)(TCGv, TCGv, TCGv),
855                              void (*f128)(TCGv, TCGv, TCGv, TCGv, TCGv, TCGv))
856 {
857     TCGv dest = dest_gpr(ctx, a->rd);
858     TCGv src1 = get_gpr(ctx, a->rs1, ext);
859     TCGv src2 = tcg_constant_tl(a->imm);
860 
861     if (get_ol(ctx) < MXL_RV128) {
862         func(dest, src1, src2);
863         gen_set_gpr(ctx, a->rd, dest);
864     } else {
865         if (f128 == NULL) {
866             return false;
867         }
868 
869         TCGv src1h = get_gprh(ctx, a->rs1);
870         TCGv src2h = tcg_constant_tl(-(a->imm < 0));
871         TCGv desth = dest_gprh(ctx, a->rd);
872 
873         f128(dest, desth, src1, src1h, src2, src2h);
874         gen_set_gpr128(ctx, a->rd, dest, desth);
875     }
876     return true;
877 }
878 
879 static bool gen_arith(DisasContext *ctx, arg_r *a, DisasExtend ext,
880                       void (*func)(TCGv, TCGv, TCGv),
881                       void (*f128)(TCGv, TCGv, TCGv, TCGv, TCGv, TCGv))
882 {
883     TCGv dest = dest_gpr(ctx, a->rd);
884     TCGv src1 = get_gpr(ctx, a->rs1, ext);
885     TCGv src2 = get_gpr(ctx, a->rs2, ext);
886 
887     if (get_ol(ctx) < MXL_RV128) {
888         func(dest, src1, src2);
889         gen_set_gpr(ctx, a->rd, dest);
890     } else {
891         if (f128 == NULL) {
892             return false;
893         }
894 
895         TCGv src1h = get_gprh(ctx, a->rs1);
896         TCGv src2h = get_gprh(ctx, a->rs2);
897         TCGv desth = dest_gprh(ctx, a->rd);
898 
899         f128(dest, desth, src1, src1h, src2, src2h);
900         gen_set_gpr128(ctx, a->rd, dest, desth);
901     }
902     return true;
903 }
904 
905 static bool gen_arith_per_ol(DisasContext *ctx, arg_r *a, DisasExtend ext,
906                              void (*f_tl)(TCGv, TCGv, TCGv),
907                              void (*f_32)(TCGv, TCGv, TCGv),
908                              void (*f_128)(TCGv, TCGv, TCGv, TCGv, TCGv, TCGv))
909 {
910     int olen = get_olen(ctx);
911 
912     if (olen != TARGET_LONG_BITS) {
913         if (olen == 32) {
914             f_tl = f_32;
915         } else if (olen != 128) {
916             g_assert_not_reached();
917         }
918     }
919     return gen_arith(ctx, a, ext, f_tl, f_128);
920 }
921 
922 static bool gen_shift_imm_fn(DisasContext *ctx, arg_shift *a, DisasExtend ext,
923                              void (*func)(TCGv, TCGv, target_long),
924                              void (*f128)(TCGv, TCGv, TCGv, TCGv, target_long))
925 {
926     TCGv dest, src1;
927     int max_len = get_olen(ctx);
928 
929     if (a->shamt >= max_len) {
930         return false;
931     }
932 
933     dest = dest_gpr(ctx, a->rd);
934     src1 = get_gpr(ctx, a->rs1, ext);
935 
936     if (max_len < 128) {
937         func(dest, src1, a->shamt);
938         gen_set_gpr(ctx, a->rd, dest);
939     } else {
940         TCGv src1h = get_gprh(ctx, a->rs1);
941         TCGv desth = dest_gprh(ctx, a->rd);
942 
943         if (f128 == NULL) {
944             return false;
945         }
946         f128(dest, desth, src1, src1h, a->shamt);
947         gen_set_gpr128(ctx, a->rd, dest, desth);
948     }
949     return true;
950 }
951 
952 static bool gen_shift_imm_fn_per_ol(DisasContext *ctx, arg_shift *a,
953                                     DisasExtend ext,
954                                     void (*f_tl)(TCGv, TCGv, target_long),
955                                     void (*f_32)(TCGv, TCGv, target_long),
956                                     void (*f_128)(TCGv, TCGv, TCGv, TCGv,
957                                                   target_long))
958 {
959     int olen = get_olen(ctx);
960     if (olen != TARGET_LONG_BITS) {
961         if (olen == 32) {
962             f_tl = f_32;
963         } else if (olen != 128) {
964             g_assert_not_reached();
965         }
966     }
967     return gen_shift_imm_fn(ctx, a, ext, f_tl, f_128);
968 }
969 
970 static bool gen_shift_imm_tl(DisasContext *ctx, arg_shift *a, DisasExtend ext,
971                              void (*func)(TCGv, TCGv, TCGv))
972 {
973     TCGv dest, src1, src2;
974     int max_len = get_olen(ctx);
975 
976     if (a->shamt >= max_len) {
977         return false;
978     }
979 
980     dest = dest_gpr(ctx, a->rd);
981     src1 = get_gpr(ctx, a->rs1, ext);
982     src2 = tcg_constant_tl(a->shamt);
983 
984     func(dest, src1, src2);
985 
986     gen_set_gpr(ctx, a->rd, dest);
987     return true;
988 }
989 
990 static bool gen_shift(DisasContext *ctx, arg_r *a, DisasExtend ext,
991                       void (*func)(TCGv, TCGv, TCGv),
992                       void (*f128)(TCGv, TCGv, TCGv, TCGv, TCGv))
993 {
994     TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE);
995     TCGv ext2 = tcg_temp_new();
996     int max_len = get_olen(ctx);
997 
998     tcg_gen_andi_tl(ext2, src2, max_len - 1);
999 
1000     TCGv dest = dest_gpr(ctx, a->rd);
1001     TCGv src1 = get_gpr(ctx, a->rs1, ext);
1002 
1003     if (max_len < 128) {
1004         func(dest, src1, ext2);
1005         gen_set_gpr(ctx, a->rd, dest);
1006     } else {
1007         TCGv src1h = get_gprh(ctx, a->rs1);
1008         TCGv desth = dest_gprh(ctx, a->rd);
1009 
1010         if (f128 == NULL) {
1011             return false;
1012         }
1013         f128(dest, desth, src1, src1h, ext2);
1014         gen_set_gpr128(ctx, a->rd, dest, desth);
1015     }
1016     tcg_temp_free(ext2);
1017     return true;
1018 }
1019 
1020 static bool gen_shift_per_ol(DisasContext *ctx, arg_r *a, DisasExtend ext,
1021                              void (*f_tl)(TCGv, TCGv, TCGv),
1022                              void (*f_32)(TCGv, TCGv, TCGv),
1023                              void (*f_128)(TCGv, TCGv, TCGv, TCGv, TCGv))
1024 {
1025     int olen = get_olen(ctx);
1026     if (olen != TARGET_LONG_BITS) {
1027         if (olen == 32) {
1028             f_tl = f_32;
1029         } else if (olen != 128) {
1030             g_assert_not_reached();
1031         }
1032     }
1033     return gen_shift(ctx, a, ext, f_tl, f_128);
1034 }
1035 
1036 static bool gen_unary(DisasContext *ctx, arg_r2 *a, DisasExtend ext,
1037                       void (*func)(TCGv, TCGv))
1038 {
1039     TCGv dest = dest_gpr(ctx, a->rd);
1040     TCGv src1 = get_gpr(ctx, a->rs1, ext);
1041 
1042     func(dest, src1);
1043 
1044     gen_set_gpr(ctx, a->rd, dest);
1045     return true;
1046 }
1047 
1048 static bool gen_unary_per_ol(DisasContext *ctx, arg_r2 *a, DisasExtend ext,
1049                              void (*f_tl)(TCGv, TCGv),
1050                              void (*f_32)(TCGv, TCGv))
1051 {
1052     int olen = get_olen(ctx);
1053 
1054     if (olen != TARGET_LONG_BITS) {
1055         if (olen == 32) {
1056             f_tl = f_32;
1057         } else {
1058             g_assert_not_reached();
1059         }
1060     }
1061     return gen_unary(ctx, a, ext, f_tl);
1062 }
1063 
1064 static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc)
1065 {
1066     DisasContext *ctx = container_of(dcbase, DisasContext, base);
1067     CPUState *cpu = ctx->cs;
1068     CPURISCVState *env = cpu->env_ptr;
1069 
1070     return cpu_ldl_code(env, pc);
1071 }
1072 
1073 /* Include insn module translation function */
1074 #include "insn_trans/trans_rvi.c.inc"
1075 #include "insn_trans/trans_rvm.c.inc"
1076 #include "insn_trans/trans_rva.c.inc"
1077 #include "insn_trans/trans_rvf.c.inc"
1078 #include "insn_trans/trans_rvd.c.inc"
1079 #include "insn_trans/trans_rvh.c.inc"
1080 #include "insn_trans/trans_rvv.c.inc"
1081 #include "insn_trans/trans_rvb.c.inc"
1082 #include "insn_trans/trans_rvzawrs.c.inc"
1083 #include "insn_trans/trans_rvzfh.c.inc"
1084 #include "insn_trans/trans_rvk.c.inc"
1085 #include "insn_trans/trans_privileged.c.inc"
1086 #include "insn_trans/trans_svinval.c.inc"
1087 #include "insn_trans/trans_xventanacondops.c.inc"
1088 
1089 /* Include the auto-generated decoder for 16 bit insn */
1090 #include "decode-insn16.c.inc"
1091 /* Include decoders for factored-out extensions */
1092 #include "decode-XVentanaCondOps.c.inc"
1093 
1094 /* The specification allows for longer insns, but not supported by qemu. */
1095 #define MAX_INSN_LEN  4
1096 
1097 static inline int insn_len(uint16_t first_word)
1098 {
1099     return (first_word & 3) == 3 ? 4 : 2;
1100 }
1101 
1102 static void decode_opc(CPURISCVState *env, DisasContext *ctx, uint16_t opcode)
1103 {
1104     /*
1105      * A table with predicate (i.e., guard) functions and decoder functions
1106      * that are tested in-order until a decoder matches onto the opcode.
1107      */
1108     static const struct {
1109         bool (*guard_func)(DisasContext *);
1110         bool (*decode_func)(DisasContext *, uint32_t);
1111     } decoders[] = {
1112         { always_true_p,  decode_insn32 },
1113         { has_XVentanaCondOps_p,  decode_XVentanaCodeOps },
1114     };
1115 
1116     ctx->virt_inst_excp = false;
1117     /* Check for compressed insn */
1118     if (insn_len(opcode) == 2) {
1119         ctx->opcode = opcode;
1120         ctx->pc_succ_insn = ctx->base.pc_next + 2;
1121         if (has_ext(ctx, RVC) && decode_insn16(ctx, opcode)) {
1122             return;
1123         }
1124     } else {
1125         uint32_t opcode32 = opcode;
1126         opcode32 = deposit32(opcode32, 16, 16,
1127                              translator_lduw(env, &ctx->base,
1128                                              ctx->base.pc_next + 2));
1129         ctx->opcode = opcode32;
1130         ctx->pc_succ_insn = ctx->base.pc_next + 4;
1131 
1132         for (size_t i = 0; i < ARRAY_SIZE(decoders); ++i) {
1133             if (decoders[i].guard_func(ctx) &&
1134                 decoders[i].decode_func(ctx, opcode32)) {
1135                 return;
1136             }
1137         }
1138     }
1139 
1140     gen_exception_illegal(ctx);
1141 }
1142 
1143 static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
1144 {
1145     DisasContext *ctx = container_of(dcbase, DisasContext, base);
1146     CPURISCVState *env = cs->env_ptr;
1147     RISCVCPU *cpu = RISCV_CPU(cs);
1148     uint32_t tb_flags = ctx->base.tb->flags;
1149 
1150     ctx->pc_succ_insn = ctx->base.pc_first;
1151     ctx->mem_idx = FIELD_EX32(tb_flags, TB_FLAGS, MEM_IDX);
1152     ctx->mstatus_fs = tb_flags & TB_FLAGS_MSTATUS_FS;
1153     ctx->mstatus_vs = tb_flags & TB_FLAGS_MSTATUS_VS;
1154     ctx->priv_ver = env->priv_ver;
1155 #if !defined(CONFIG_USER_ONLY)
1156     if (riscv_has_ext(env, RVH)) {
1157         ctx->virt_enabled = riscv_cpu_virt_enabled(env);
1158     } else {
1159         ctx->virt_enabled = false;
1160     }
1161 #else
1162     ctx->virt_enabled = false;
1163 #endif
1164     ctx->misa_ext = env->misa_ext;
1165     ctx->frm = -1;  /* unknown rounding mode */
1166     ctx->cfg_ptr = &(cpu->cfg);
1167     ctx->mstatus_hs_fs = FIELD_EX32(tb_flags, TB_FLAGS, MSTATUS_HS_FS);
1168     ctx->mstatus_hs_vs = FIELD_EX32(tb_flags, TB_FLAGS, MSTATUS_HS_VS);
1169     ctx->hlsx = FIELD_EX32(tb_flags, TB_FLAGS, HLSX);
1170     ctx->vill = FIELD_EX32(tb_flags, TB_FLAGS, VILL);
1171     ctx->sew = FIELD_EX32(tb_flags, TB_FLAGS, SEW);
1172     ctx->lmul = sextract32(FIELD_EX32(tb_flags, TB_FLAGS, LMUL), 0, 3);
1173     ctx->vta = FIELD_EX32(tb_flags, TB_FLAGS, VTA) && cpu->cfg.rvv_ta_all_1s;
1174     ctx->vma = FIELD_EX32(tb_flags, TB_FLAGS, VMA) && cpu->cfg.rvv_ma_all_1s;
1175     ctx->cfg_vta_all_1s = cpu->cfg.rvv_ta_all_1s;
1176     ctx->vstart = env->vstart;
1177     ctx->vl_eq_vlmax = FIELD_EX32(tb_flags, TB_FLAGS, VL_EQ_VLMAX);
1178     ctx->misa_mxl_max = env->misa_mxl_max;
1179     ctx->xl = FIELD_EX32(tb_flags, TB_FLAGS, XL);
1180     ctx->cs = cs;
1181     ctx->ntemp = 0;
1182     memset(ctx->temp, 0, sizeof(ctx->temp));
1183     ctx->nftemp = 0;
1184     memset(ctx->ftemp, 0, sizeof(ctx->ftemp));
1185     ctx->pm_mask_enabled = FIELD_EX32(tb_flags, TB_FLAGS, PM_MASK_ENABLED);
1186     ctx->pm_base_enabled = FIELD_EX32(tb_flags, TB_FLAGS, PM_BASE_ENABLED);
1187     ctx->itrigger = FIELD_EX32(tb_flags, TB_FLAGS, ITRIGGER);
1188     ctx->zero = tcg_constant_tl(0);
1189 }
1190 
1191 static void riscv_tr_tb_start(DisasContextBase *db, CPUState *cpu)
1192 {
1193 }
1194 
1195 static void riscv_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
1196 {
1197     DisasContext *ctx = container_of(dcbase, DisasContext, base);
1198 
1199     tcg_gen_insn_start(ctx->base.pc_next, 0);
1200     ctx->insn_start = tcg_last_op();
1201 }
1202 
1203 static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
1204 {
1205     DisasContext *ctx = container_of(dcbase, DisasContext, base);
1206     CPURISCVState *env = cpu->env_ptr;
1207     uint16_t opcode16 = translator_lduw(env, &ctx->base, ctx->base.pc_next);
1208     int i;
1209 
1210     ctx->ol = ctx->xl;
1211     decode_opc(env, ctx, opcode16);
1212     ctx->base.pc_next = ctx->pc_succ_insn;
1213 
1214     for (i = ctx->ntemp - 1; i >= 0; --i) {
1215         tcg_temp_free(ctx->temp[i]);
1216         ctx->temp[i] = NULL;
1217     }
1218     ctx->ntemp = 0;
1219     for (i = ctx->nftemp - 1; i >= 0; --i) {
1220         tcg_temp_free_i64(ctx->ftemp[i]);
1221         ctx->ftemp[i] = NULL;
1222     }
1223     ctx->nftemp = 0;
1224 
1225     /* Only the first insn within a TB is allowed to cross a page boundary. */
1226     if (ctx->base.is_jmp == DISAS_NEXT) {
1227         if (ctx->itrigger || !is_same_page(&ctx->base, ctx->base.pc_next)) {
1228             ctx->base.is_jmp = DISAS_TOO_MANY;
1229         } else {
1230             unsigned page_ofs = ctx->base.pc_next & ~TARGET_PAGE_MASK;
1231 
1232             if (page_ofs > TARGET_PAGE_SIZE - MAX_INSN_LEN) {
1233                 uint16_t next_insn = cpu_lduw_code(env, ctx->base.pc_next);
1234                 int len = insn_len(next_insn);
1235 
1236                 if (!is_same_page(&ctx->base, ctx->base.pc_next + len)) {
1237                     ctx->base.is_jmp = DISAS_TOO_MANY;
1238                 }
1239             }
1240         }
1241     }
1242 }
1243 
1244 static void riscv_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
1245 {
1246     DisasContext *ctx = container_of(dcbase, DisasContext, base);
1247 
1248     switch (ctx->base.is_jmp) {
1249     case DISAS_TOO_MANY:
1250         gen_goto_tb(ctx, 0, ctx->base.pc_next);
1251         break;
1252     case DISAS_NORETURN:
1253         break;
1254     default:
1255         g_assert_not_reached();
1256     }
1257 }
1258 
1259 static void riscv_tr_disas_log(const DisasContextBase *dcbase,
1260                                CPUState *cpu, FILE *logfile)
1261 {
1262 #ifndef CONFIG_USER_ONLY
1263     RISCVCPU *rvcpu = RISCV_CPU(cpu);
1264     CPURISCVState *env = &rvcpu->env;
1265 #endif
1266 
1267     fprintf(logfile, "IN: %s\n", lookup_symbol(dcbase->pc_first));
1268 #ifndef CONFIG_USER_ONLY
1269     fprintf(logfile, "Priv: "TARGET_FMT_ld"; Virt: "TARGET_FMT_ld"\n",
1270             env->priv, env->virt);
1271 #endif
1272     target_disas(logfile, cpu, dcbase->pc_first, dcbase->tb->size);
1273 }
1274 
1275 static const TranslatorOps riscv_tr_ops = {
1276     .init_disas_context = riscv_tr_init_disas_context,
1277     .tb_start           = riscv_tr_tb_start,
1278     .insn_start         = riscv_tr_insn_start,
1279     .translate_insn     = riscv_tr_translate_insn,
1280     .tb_stop            = riscv_tr_tb_stop,
1281     .disas_log          = riscv_tr_disas_log,
1282 };
1283 
1284 void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns,
1285                            target_ulong pc, void *host_pc)
1286 {
1287     DisasContext ctx;
1288 
1289     translator_loop(cs, tb, max_insns, pc, host_pc, &riscv_tr_ops, &ctx.base);
1290 }
1291 
1292 void riscv_translate_init(void)
1293 {
1294     int i;
1295 
1296     /*
1297      * cpu_gpr[0] is a placeholder for the zero register. Do not use it.
1298      * Use the gen_set_gpr and get_gpr helper functions when accessing regs,
1299      * unless you specifically block reads/writes to reg 0.
1300      */
1301     cpu_gpr[0] = NULL;
1302     cpu_gprh[0] = NULL;
1303 
1304     for (i = 1; i < 32; i++) {
1305         cpu_gpr[i] = tcg_global_mem_new(cpu_env,
1306             offsetof(CPURISCVState, gpr[i]), riscv_int_regnames[i]);
1307         cpu_gprh[i] = tcg_global_mem_new(cpu_env,
1308             offsetof(CPURISCVState, gprh[i]), riscv_int_regnamesh[i]);
1309     }
1310 
1311     for (i = 0; i < 32; i++) {
1312         cpu_fpr[i] = tcg_global_mem_new_i64(cpu_env,
1313             offsetof(CPURISCVState, fpr[i]), riscv_fpr_regnames[i]);
1314     }
1315 
1316     cpu_pc = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, pc), "pc");
1317     cpu_vl = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, vl), "vl");
1318     cpu_vstart = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, vstart),
1319                             "vstart");
1320     load_res = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, load_res),
1321                              "load_res");
1322     load_val = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, load_val),
1323                              "load_val");
1324     /* Assign PM CSRs to tcg globals */
1325     pm_mask = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, cur_pmmask),
1326                                  "pmmask");
1327     pm_base = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, cur_pmbase),
1328                                  "pmbase");
1329 }
1330