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