1/*
2 * RISC-V translation routines for the RVXI Base Integer Instruction Set.
3 *
4 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
5 * Copyright (c) 2018 Peer Adelt, peer.adelt@hni.uni-paderborn.de
6 *                    Bastian Koppelmann, kbastian@mail.uni-paderborn.de
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2 or later, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21static bool trans_illegal(DisasContext *ctx, arg_empty *a)
22{
23    gen_exception_illegal(ctx);
24    return true;
25}
26
27static bool trans_c64_illegal(DisasContext *ctx, arg_empty *a)
28{
29    REQUIRE_64_OR_128BIT(ctx);
30    return trans_illegal(ctx, a);
31}
32
33static bool trans_lui(DisasContext *ctx, arg_lui *a)
34{
35    gen_set_gpri(ctx, a->rd, a->imm);
36    return true;
37}
38
39static bool trans_auipc(DisasContext *ctx, arg_auipc *a)
40{
41    TCGv target_pc = dest_gpr(ctx, a->rd);
42    gen_pc_plus_diff(target_pc, ctx, a->imm);
43    gen_set_gpr(ctx, a->rd, target_pc);
44    return true;
45}
46
47static bool trans_jal(DisasContext *ctx, arg_jal *a)
48{
49    gen_jal(ctx, a->rd, a->imm);
50    return true;
51}
52
53static bool trans_jalr(DisasContext *ctx, arg_jalr *a)
54{
55    TCGLabel *misaligned = NULL;
56    TCGv target_pc = tcg_temp_new();
57    TCGv succ_pc = dest_gpr(ctx, a->rd);
58
59    tcg_gen_addi_tl(target_pc, get_gpr(ctx, a->rs1, EXT_NONE), a->imm);
60    tcg_gen_andi_tl(target_pc, target_pc, (target_ulong)-2);
61
62    if (get_xl(ctx) == MXL_RV32) {
63        tcg_gen_ext32s_tl(target_pc, target_pc);
64    }
65
66    if (!has_ext(ctx, RVC) && !ctx->cfg_ptr->ext_zca) {
67        TCGv t0 = tcg_temp_new();
68
69        misaligned = gen_new_label();
70        tcg_gen_andi_tl(t0, target_pc, 0x2);
71        tcg_gen_brcondi_tl(TCG_COND_NE, t0, 0x0, misaligned);
72    }
73
74    gen_pc_plus_diff(succ_pc, ctx, ctx->cur_insn_len);
75    gen_set_gpr(ctx, a->rd, succ_pc);
76
77    tcg_gen_mov_tl(cpu_pc, target_pc);
78    lookup_and_goto_ptr(ctx);
79
80    if (misaligned) {
81        gen_set_label(misaligned);
82        gen_exception_inst_addr_mis(ctx, target_pc);
83    }
84    ctx->base.is_jmp = DISAS_NORETURN;
85
86    return true;
87}
88
89static TCGCond gen_compare_i128(bool bz, TCGv rl,
90                                TCGv al, TCGv ah, TCGv bl, TCGv bh,
91                                TCGCond cond)
92{
93    TCGv rh = tcg_temp_new();
94    bool invert = false;
95
96    switch (cond) {
97    case TCG_COND_EQ:
98    case TCG_COND_NE:
99        if (bz) {
100            tcg_gen_or_tl(rl, al, ah);
101        } else {
102            tcg_gen_xor_tl(rl, al, bl);
103            tcg_gen_xor_tl(rh, ah, bh);
104            tcg_gen_or_tl(rl, rl, rh);
105        }
106        break;
107
108    case TCG_COND_GE:
109    case TCG_COND_LT:
110        if (bz) {
111            tcg_gen_mov_tl(rl, ah);
112        } else {
113            TCGv tmp = tcg_temp_new();
114
115            tcg_gen_sub2_tl(rl, rh, al, ah, bl, bh);
116            tcg_gen_xor_tl(rl, rh, ah);
117            tcg_gen_xor_tl(tmp, ah, bh);
118            tcg_gen_and_tl(rl, rl, tmp);
119            tcg_gen_xor_tl(rl, rh, rl);
120        }
121        break;
122
123    case TCG_COND_LTU:
124        invert = true;
125        /* fallthrough */
126    case TCG_COND_GEU:
127        {
128            TCGv tmp = tcg_temp_new();
129            TCGv zero = tcg_constant_tl(0);
130            TCGv one = tcg_constant_tl(1);
131
132            cond = TCG_COND_NE;
133            /* borrow in to second word */
134            tcg_gen_setcond_tl(TCG_COND_LTU, tmp, al, bl);
135            /* seed third word with 1, which will be result */
136            tcg_gen_sub2_tl(tmp, rh, ah, one, tmp, zero);
137            tcg_gen_sub2_tl(tmp, rl, tmp, rh, bh, zero);
138        }
139        break;
140
141    default:
142        g_assert_not_reached();
143    }
144
145    if (invert) {
146        cond = tcg_invert_cond(cond);
147    }
148    return cond;
149}
150
151static void gen_setcond_i128(TCGv rl, TCGv rh,
152                             TCGv src1l, TCGv src1h,
153                             TCGv src2l, TCGv src2h,
154                             TCGCond cond)
155{
156    cond = gen_compare_i128(false, rl, src1l, src1h, src2l, src2h, cond);
157    tcg_gen_setcondi_tl(cond, rl, rl, 0);
158    tcg_gen_movi_tl(rh, 0);
159}
160
161static bool gen_branch(DisasContext *ctx, arg_b *a, TCGCond cond)
162{
163    TCGLabel *l = gen_new_label();
164    TCGv src1 = get_gpr(ctx, a->rs1, EXT_SIGN);
165    TCGv src2 = get_gpr(ctx, a->rs2, EXT_SIGN);
166    target_ulong orig_pc_save = ctx->pc_save;
167
168    if (get_xl(ctx) == MXL_RV128) {
169        TCGv src1h = get_gprh(ctx, a->rs1);
170        TCGv src2h = get_gprh(ctx, a->rs2);
171        TCGv tmp = tcg_temp_new();
172
173        cond = gen_compare_i128(a->rs2 == 0,
174                                tmp, src1, src1h, src2, src2h, cond);
175        tcg_gen_brcondi_tl(cond, tmp, 0, l);
176    } else {
177        tcg_gen_brcond_tl(cond, src1, src2, l);
178    }
179    gen_goto_tb(ctx, 1, ctx->cur_insn_len);
180    ctx->pc_save = orig_pc_save;
181
182    gen_set_label(l); /* branch taken */
183
184    if (!has_ext(ctx, RVC) && !ctx->cfg_ptr->ext_zca &&
185        (a->imm & 0x3)) {
186        /* misaligned */
187        TCGv target_pc = tcg_temp_new();
188        gen_pc_plus_diff(target_pc, ctx, a->imm);
189        gen_exception_inst_addr_mis(ctx, target_pc);
190    } else {
191        gen_goto_tb(ctx, 0, a->imm);
192    }
193    ctx->pc_save = -1;
194    ctx->base.is_jmp = DISAS_NORETURN;
195
196    return true;
197}
198
199static bool trans_beq(DisasContext *ctx, arg_beq *a)
200{
201    return gen_branch(ctx, a, TCG_COND_EQ);
202}
203
204static bool trans_bne(DisasContext *ctx, arg_bne *a)
205{
206    return gen_branch(ctx, a, TCG_COND_NE);
207}
208
209static bool trans_blt(DisasContext *ctx, arg_blt *a)
210{
211    return gen_branch(ctx, a, TCG_COND_LT);
212}
213
214static bool trans_bge(DisasContext *ctx, arg_bge *a)
215{
216    return gen_branch(ctx, a, TCG_COND_GE);
217}
218
219static bool trans_bltu(DisasContext *ctx, arg_bltu *a)
220{
221    return gen_branch(ctx, a, TCG_COND_LTU);
222}
223
224static bool trans_bgeu(DisasContext *ctx, arg_bgeu *a)
225{
226    return gen_branch(ctx, a, TCG_COND_GEU);
227}
228
229static bool gen_load_tl(DisasContext *ctx, arg_lb *a, MemOp memop)
230{
231    TCGv dest = dest_gpr(ctx, a->rd);
232    TCGv addr = get_address(ctx, a->rs1, a->imm);
233
234    tcg_gen_qemu_ld_tl(dest, addr, ctx->mem_idx, memop);
235    gen_set_gpr(ctx, a->rd, dest);
236    return true;
237}
238
239/* Compute only 64-bit addresses to use the address translation mechanism */
240static bool gen_load_i128(DisasContext *ctx, arg_lb *a, MemOp memop)
241{
242    TCGv src1l = get_gpr(ctx, a->rs1, EXT_NONE);
243    TCGv destl = dest_gpr(ctx, a->rd);
244    TCGv desth = dest_gprh(ctx, a->rd);
245    TCGv addrl = tcg_temp_new();
246
247    tcg_gen_addi_tl(addrl, src1l, a->imm);
248
249    if ((memop & MO_SIZE) <= MO_64) {
250        tcg_gen_qemu_ld_tl(destl, addrl, ctx->mem_idx, memop);
251        if (memop & MO_SIGN) {
252            tcg_gen_sari_tl(desth, destl, 63);
253        } else {
254            tcg_gen_movi_tl(desth, 0);
255        }
256    } else {
257        /* assume little-endian memory access for now */
258        tcg_gen_qemu_ld_tl(destl, addrl, ctx->mem_idx, MO_TEUQ);
259        tcg_gen_addi_tl(addrl, addrl, 8);
260        tcg_gen_qemu_ld_tl(desth, addrl, ctx->mem_idx, MO_TEUQ);
261    }
262
263    gen_set_gpr128(ctx, a->rd, destl, desth);
264    return true;
265}
266
267static bool gen_load(DisasContext *ctx, arg_lb *a, MemOp memop)
268{
269    bool out;
270
271    decode_save_opc(ctx);
272    if (get_xl(ctx) == MXL_RV128) {
273        out = gen_load_i128(ctx, a, memop);
274    } else {
275        out = gen_load_tl(ctx, a, memop);
276    }
277
278    if (ctx->ztso) {
279        tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
280    }
281
282    return out;
283}
284
285static bool trans_lb(DisasContext *ctx, arg_lb *a)
286{
287    return gen_load(ctx, a, MO_SB);
288}
289
290static bool trans_lh(DisasContext *ctx, arg_lh *a)
291{
292    return gen_load(ctx, a, MO_TESW);
293}
294
295static bool trans_lw(DisasContext *ctx, arg_lw *a)
296{
297    return gen_load(ctx, a, MO_TESL);
298}
299
300static bool trans_ld(DisasContext *ctx, arg_ld *a)
301{
302    REQUIRE_64_OR_128BIT(ctx);
303    return gen_load(ctx, a, MO_TESQ);
304}
305
306static bool trans_lq(DisasContext *ctx, arg_lq *a)
307{
308    REQUIRE_128BIT(ctx);
309    return gen_load(ctx, a, MO_TEUO);
310}
311
312static bool trans_lbu(DisasContext *ctx, arg_lbu *a)
313{
314    return gen_load(ctx, a, MO_UB);
315}
316
317static bool trans_lhu(DisasContext *ctx, arg_lhu *a)
318{
319    return gen_load(ctx, a, MO_TEUW);
320}
321
322static bool trans_lwu(DisasContext *ctx, arg_lwu *a)
323{
324    REQUIRE_64_OR_128BIT(ctx);
325    return gen_load(ctx, a, MO_TEUL);
326}
327
328static bool trans_ldu(DisasContext *ctx, arg_ldu *a)
329{
330    REQUIRE_128BIT(ctx);
331    return gen_load(ctx, a, MO_TEUQ);
332}
333
334static bool gen_store_tl(DisasContext *ctx, arg_sb *a, MemOp memop)
335{
336    TCGv addr = get_address(ctx, a->rs1, a->imm);
337    TCGv data = get_gpr(ctx, a->rs2, EXT_NONE);
338
339    if (ctx->ztso) {
340        tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
341    }
342
343    tcg_gen_qemu_st_tl(data, addr, ctx->mem_idx, memop);
344    return true;
345}
346
347static bool gen_store_i128(DisasContext *ctx, arg_sb *a, MemOp memop)
348{
349    TCGv src1l = get_gpr(ctx, a->rs1, EXT_NONE);
350    TCGv src2l = get_gpr(ctx, a->rs2, EXT_NONE);
351    TCGv src2h = get_gprh(ctx, a->rs2);
352    TCGv addrl = tcg_temp_new();
353
354    tcg_gen_addi_tl(addrl, src1l, a->imm);
355
356    if ((memop & MO_SIZE) <= MO_64) {
357        tcg_gen_qemu_st_tl(src2l, addrl, ctx->mem_idx, memop);
358    } else {
359        /* little-endian memory access assumed for now */
360        tcg_gen_qemu_st_tl(src2l, addrl, ctx->mem_idx, MO_TEUQ);
361        tcg_gen_addi_tl(addrl, addrl, 8);
362        tcg_gen_qemu_st_tl(src2h, addrl, ctx->mem_idx, MO_TEUQ);
363    }
364    return true;
365}
366
367static bool gen_store(DisasContext *ctx, arg_sb *a, MemOp memop)
368{
369    decode_save_opc(ctx);
370    if (get_xl(ctx) == MXL_RV128) {
371        return gen_store_i128(ctx, a, memop);
372    } else {
373        return gen_store_tl(ctx, a, memop);
374    }
375}
376
377static bool trans_sb(DisasContext *ctx, arg_sb *a)
378{
379    return gen_store(ctx, a, MO_SB);
380}
381
382static bool trans_sh(DisasContext *ctx, arg_sh *a)
383{
384    return gen_store(ctx, a, MO_TESW);
385}
386
387static bool trans_sw(DisasContext *ctx, arg_sw *a)
388{
389    return gen_store(ctx, a, MO_TESL);
390}
391
392static bool trans_sd(DisasContext *ctx, arg_sd *a)
393{
394    REQUIRE_64_OR_128BIT(ctx);
395    return gen_store(ctx, a, MO_TEUQ);
396}
397
398static bool trans_sq(DisasContext *ctx, arg_sq *a)
399{
400    REQUIRE_128BIT(ctx);
401    return gen_store(ctx, a, MO_TEUO);
402}
403
404static bool trans_addd(DisasContext *ctx, arg_addd *a)
405{
406    REQUIRE_128BIT(ctx);
407    ctx->ol = MXL_RV64;
408    return gen_arith(ctx, a, EXT_NONE, tcg_gen_add_tl, NULL);
409}
410
411static bool trans_addid(DisasContext *ctx, arg_addid *a)
412{
413    REQUIRE_128BIT(ctx);
414    ctx->ol = MXL_RV64;
415    return gen_arith_imm_fn(ctx, a, EXT_NONE, tcg_gen_addi_tl, NULL);
416}
417
418static bool trans_subd(DisasContext *ctx, arg_subd *a)
419{
420    REQUIRE_128BIT(ctx);
421    ctx->ol = MXL_RV64;
422    return gen_arith(ctx, a, EXT_NONE, tcg_gen_sub_tl, NULL);
423}
424
425static void gen_addi2_i128(TCGv retl, TCGv reth,
426                           TCGv srcl, TCGv srch, target_long imm)
427{
428    TCGv imml  = tcg_constant_tl(imm);
429    TCGv immh  = tcg_constant_tl(-(imm < 0));
430    tcg_gen_add2_tl(retl, reth, srcl, srch, imml, immh);
431}
432
433static bool trans_addi(DisasContext *ctx, arg_addi *a)
434{
435    return gen_arith_imm_fn(ctx, a, EXT_NONE, tcg_gen_addi_tl, gen_addi2_i128);
436}
437
438static void gen_slt(TCGv ret, TCGv s1, TCGv s2)
439{
440    tcg_gen_setcond_tl(TCG_COND_LT, ret, s1, s2);
441}
442
443static void gen_slt_i128(TCGv retl, TCGv reth,
444                         TCGv s1l, TCGv s1h, TCGv s2l, TCGv s2h)
445{
446    gen_setcond_i128(retl, reth, s1l, s1h, s2l, s2h, TCG_COND_LT);
447}
448
449static void gen_sltu(TCGv ret, TCGv s1, TCGv s2)
450{
451    tcg_gen_setcond_tl(TCG_COND_LTU, ret, s1, s2);
452}
453
454static void gen_sltu_i128(TCGv retl, TCGv reth,
455                          TCGv s1l, TCGv s1h, TCGv s2l, TCGv s2h)
456{
457    gen_setcond_i128(retl, reth, s1l, s1h, s2l, s2h, TCG_COND_LTU);
458}
459
460static bool trans_slti(DisasContext *ctx, arg_slti *a)
461{
462    return gen_arith_imm_tl(ctx, a, EXT_SIGN, gen_slt, gen_slt_i128);
463}
464
465static bool trans_sltiu(DisasContext *ctx, arg_sltiu *a)
466{
467    return gen_arith_imm_tl(ctx, a, EXT_SIGN, gen_sltu, gen_sltu_i128);
468}
469
470static bool trans_xori(DisasContext *ctx, arg_xori *a)
471{
472    return gen_logic_imm_fn(ctx, a, tcg_gen_xori_tl);
473}
474
475static bool trans_ori(DisasContext *ctx, arg_ori *a)
476{
477    return gen_logic_imm_fn(ctx, a, tcg_gen_ori_tl);
478}
479
480static bool trans_andi(DisasContext *ctx, arg_andi *a)
481{
482    return gen_logic_imm_fn(ctx, a, tcg_gen_andi_tl);
483}
484
485static void gen_slli_i128(TCGv retl, TCGv reth,
486                          TCGv src1l, TCGv src1h,
487                          target_long shamt)
488{
489    if (shamt >= 64) {
490        tcg_gen_shli_tl(reth, src1l, shamt - 64);
491        tcg_gen_movi_tl(retl, 0);
492    } else {
493        tcg_gen_extract2_tl(reth, src1l, src1h, 64 - shamt);
494        tcg_gen_shli_tl(retl, src1l, shamt);
495    }
496}
497
498static bool trans_slli(DisasContext *ctx, arg_slli *a)
499{
500    return gen_shift_imm_fn(ctx, a, EXT_NONE, tcg_gen_shli_tl, gen_slli_i128);
501}
502
503static void gen_srliw(TCGv dst, TCGv src, target_long shamt)
504{
505    tcg_gen_extract_tl(dst, src, shamt, 32 - shamt);
506}
507
508static void gen_srli_i128(TCGv retl, TCGv reth,
509                          TCGv src1l, TCGv src1h,
510                          target_long shamt)
511{
512    if (shamt >= 64) {
513        tcg_gen_shri_tl(retl, src1h, shamt - 64);
514        tcg_gen_movi_tl(reth, 0);
515    } else {
516        tcg_gen_extract2_tl(retl, src1l, src1h, shamt);
517        tcg_gen_shri_tl(reth, src1h, shamt);
518    }
519}
520
521static bool trans_srli(DisasContext *ctx, arg_srli *a)
522{
523    return gen_shift_imm_fn_per_ol(ctx, a, EXT_NONE,
524                                   tcg_gen_shri_tl, gen_srliw, gen_srli_i128);
525}
526
527static void gen_sraiw(TCGv dst, TCGv src, target_long shamt)
528{
529    tcg_gen_sextract_tl(dst, src, shamt, 32 - shamt);
530}
531
532static void gen_srai_i128(TCGv retl, TCGv reth,
533                          TCGv src1l, TCGv src1h,
534                          target_long shamt)
535{
536    if (shamt >= 64) {
537        tcg_gen_sari_tl(retl, src1h, shamt - 64);
538        tcg_gen_sari_tl(reth, src1h, 63);
539    } else {
540        tcg_gen_extract2_tl(retl, src1l, src1h, shamt);
541        tcg_gen_sari_tl(reth, src1h, shamt);
542    }
543}
544
545static bool trans_srai(DisasContext *ctx, arg_srai *a)
546{
547    return gen_shift_imm_fn_per_ol(ctx, a, EXT_NONE,
548                                   tcg_gen_sari_tl, gen_sraiw, gen_srai_i128);
549}
550
551static bool trans_add(DisasContext *ctx, arg_add *a)
552{
553    return gen_arith(ctx, a, EXT_NONE, tcg_gen_add_tl, tcg_gen_add2_tl);
554}
555
556static bool trans_sub(DisasContext *ctx, arg_sub *a)
557{
558    return gen_arith(ctx, a, EXT_NONE, tcg_gen_sub_tl, tcg_gen_sub2_tl);
559}
560
561static void gen_sll_i128(TCGv destl, TCGv desth,
562                         TCGv src1l, TCGv src1h, TCGv shamt)
563{
564    TCGv ls = tcg_temp_new();
565    TCGv rs = tcg_temp_new();
566    TCGv hs = tcg_temp_new();
567    TCGv ll = tcg_temp_new();
568    TCGv lr = tcg_temp_new();
569    TCGv h0 = tcg_temp_new();
570    TCGv h1 = tcg_temp_new();
571    TCGv zero = tcg_constant_tl(0);
572
573    tcg_gen_andi_tl(hs, shamt, 64);
574    tcg_gen_andi_tl(ls, shamt, 63);
575    tcg_gen_neg_tl(shamt, shamt);
576    tcg_gen_andi_tl(rs, shamt, 63);
577
578    tcg_gen_shl_tl(ll, src1l, ls);
579    tcg_gen_shl_tl(h0, src1h, ls);
580    tcg_gen_shr_tl(lr, src1l, rs);
581    tcg_gen_movcond_tl(TCG_COND_NE, lr, shamt, zero, lr, zero);
582    tcg_gen_or_tl(h1, h0, lr);
583
584    tcg_gen_movcond_tl(TCG_COND_NE, destl, hs, zero, zero, ll);
585    tcg_gen_movcond_tl(TCG_COND_NE, desth, hs, zero, ll, h1);
586}
587
588static bool trans_sll(DisasContext *ctx, arg_sll *a)
589{
590    return gen_shift(ctx, a, EXT_NONE, tcg_gen_shl_tl, gen_sll_i128);
591}
592
593static bool trans_slt(DisasContext *ctx, arg_slt *a)
594{
595    return gen_arith(ctx, a, EXT_SIGN, gen_slt, gen_slt_i128);
596}
597
598static bool trans_sltu(DisasContext *ctx, arg_sltu *a)
599{
600    return gen_arith(ctx, a, EXT_SIGN, gen_sltu, gen_sltu_i128);
601}
602
603static void gen_srl_i128(TCGv destl, TCGv desth,
604                         TCGv src1l, TCGv src1h, TCGv shamt)
605{
606    TCGv ls = tcg_temp_new();
607    TCGv rs = tcg_temp_new();
608    TCGv hs = tcg_temp_new();
609    TCGv ll = tcg_temp_new();
610    TCGv lr = tcg_temp_new();
611    TCGv h0 = tcg_temp_new();
612    TCGv h1 = tcg_temp_new();
613    TCGv zero = tcg_constant_tl(0);
614
615    tcg_gen_andi_tl(hs, shamt, 64);
616    tcg_gen_andi_tl(rs, shamt, 63);
617    tcg_gen_neg_tl(shamt, shamt);
618    tcg_gen_andi_tl(ls, shamt, 63);
619
620    tcg_gen_shr_tl(lr, src1l, rs);
621    tcg_gen_shr_tl(h1, src1h, rs);
622    tcg_gen_shl_tl(ll, src1h, ls);
623    tcg_gen_movcond_tl(TCG_COND_NE, ll, shamt, zero, ll, zero);
624    tcg_gen_or_tl(h0, ll, lr);
625
626    tcg_gen_movcond_tl(TCG_COND_NE, destl, hs, zero, h1, h0);
627    tcg_gen_movcond_tl(TCG_COND_NE, desth, hs, zero, zero, h1);
628}
629
630static bool trans_srl(DisasContext *ctx, arg_srl *a)
631{
632    return gen_shift(ctx, a, EXT_ZERO, tcg_gen_shr_tl, gen_srl_i128);
633}
634
635static void gen_sra_i128(TCGv destl, TCGv desth,
636                         TCGv src1l, TCGv src1h, TCGv shamt)
637{
638    TCGv ls = tcg_temp_new();
639    TCGv rs = tcg_temp_new();
640    TCGv hs = tcg_temp_new();
641    TCGv ll = tcg_temp_new();
642    TCGv lr = tcg_temp_new();
643    TCGv h0 = tcg_temp_new();
644    TCGv h1 = tcg_temp_new();
645    TCGv zero = tcg_constant_tl(0);
646
647    tcg_gen_andi_tl(hs, shamt, 64);
648    tcg_gen_andi_tl(rs, shamt, 63);
649    tcg_gen_neg_tl(shamt, shamt);
650    tcg_gen_andi_tl(ls, shamt, 63);
651
652    tcg_gen_shr_tl(lr, src1l, rs);
653    tcg_gen_sar_tl(h1, src1h, rs);
654    tcg_gen_shl_tl(ll, src1h, ls);
655    tcg_gen_movcond_tl(TCG_COND_NE, ll, shamt, zero, ll, zero);
656    tcg_gen_or_tl(h0, ll, lr);
657    tcg_gen_sari_tl(lr, src1h, 63);
658
659    tcg_gen_movcond_tl(TCG_COND_NE, destl, hs, zero, h1, h0);
660    tcg_gen_movcond_tl(TCG_COND_NE, desth, hs, zero, lr, h1);
661}
662
663static bool trans_sra(DisasContext *ctx, arg_sra *a)
664{
665    return gen_shift(ctx, a, EXT_SIGN, tcg_gen_sar_tl, gen_sra_i128);
666}
667
668static bool trans_xor(DisasContext *ctx, arg_xor *a)
669{
670    return gen_logic(ctx, a, tcg_gen_xor_tl);
671}
672
673static bool trans_or(DisasContext *ctx, arg_or *a)
674{
675    return gen_logic(ctx, a, tcg_gen_or_tl);
676}
677
678static bool trans_and(DisasContext *ctx, arg_and *a)
679{
680    return gen_logic(ctx, a, tcg_gen_and_tl);
681}
682
683static bool trans_addiw(DisasContext *ctx, arg_addiw *a)
684{
685    REQUIRE_64_OR_128BIT(ctx);
686    ctx->ol = MXL_RV32;
687    return gen_arith_imm_fn(ctx, a, EXT_NONE, tcg_gen_addi_tl, NULL);
688}
689
690static bool trans_slliw(DisasContext *ctx, arg_slliw *a)
691{
692    REQUIRE_64_OR_128BIT(ctx);
693    ctx->ol = MXL_RV32;
694    return gen_shift_imm_fn(ctx, a, EXT_NONE, tcg_gen_shli_tl, NULL);
695}
696
697static bool trans_srliw(DisasContext *ctx, arg_srliw *a)
698{
699    REQUIRE_64_OR_128BIT(ctx);
700    ctx->ol = MXL_RV32;
701    return gen_shift_imm_fn(ctx, a, EXT_NONE, gen_srliw, NULL);
702}
703
704static bool trans_sraiw(DisasContext *ctx, arg_sraiw *a)
705{
706    REQUIRE_64_OR_128BIT(ctx);
707    ctx->ol = MXL_RV32;
708    return gen_shift_imm_fn(ctx, a, EXT_NONE, gen_sraiw, NULL);
709}
710
711static bool trans_sllid(DisasContext *ctx, arg_sllid *a)
712{
713    REQUIRE_128BIT(ctx);
714    ctx->ol = MXL_RV64;
715    return gen_shift_imm_fn(ctx, a, EXT_NONE, tcg_gen_shli_tl, NULL);
716}
717
718static bool trans_srlid(DisasContext *ctx, arg_srlid *a)
719{
720    REQUIRE_128BIT(ctx);
721    ctx->ol = MXL_RV64;
722    return gen_shift_imm_fn(ctx, a, EXT_NONE, tcg_gen_shri_tl, NULL);
723}
724
725static bool trans_sraid(DisasContext *ctx, arg_sraid *a)
726{
727    REQUIRE_128BIT(ctx);
728    ctx->ol = MXL_RV64;
729    return gen_shift_imm_fn(ctx, a, EXT_NONE, tcg_gen_sari_tl,  NULL);
730}
731
732static bool trans_addw(DisasContext *ctx, arg_addw *a)
733{
734    REQUIRE_64_OR_128BIT(ctx);
735    ctx->ol = MXL_RV32;
736    return gen_arith(ctx, a, EXT_NONE, tcg_gen_add_tl, NULL);
737}
738
739static bool trans_subw(DisasContext *ctx, arg_subw *a)
740{
741    REQUIRE_64_OR_128BIT(ctx);
742    ctx->ol = MXL_RV32;
743    return gen_arith(ctx, a, EXT_NONE, tcg_gen_sub_tl, NULL);
744}
745
746static bool trans_sllw(DisasContext *ctx, arg_sllw *a)
747{
748    REQUIRE_64_OR_128BIT(ctx);
749    ctx->ol = MXL_RV32;
750    return gen_shift(ctx, a, EXT_NONE, tcg_gen_shl_tl, NULL);
751}
752
753static bool trans_srlw(DisasContext *ctx, arg_srlw *a)
754{
755    REQUIRE_64_OR_128BIT(ctx);
756    ctx->ol = MXL_RV32;
757    return gen_shift(ctx, a, EXT_ZERO, tcg_gen_shr_tl, NULL);
758}
759
760static bool trans_sraw(DisasContext *ctx, arg_sraw *a)
761{
762    REQUIRE_64_OR_128BIT(ctx);
763    ctx->ol = MXL_RV32;
764    return gen_shift(ctx, a, EXT_SIGN, tcg_gen_sar_tl, NULL);
765}
766
767static bool trans_slld(DisasContext *ctx, arg_slld *a)
768{
769    REQUIRE_128BIT(ctx);
770    ctx->ol = MXL_RV64;
771    return gen_shift(ctx, a, EXT_NONE, tcg_gen_shl_tl, NULL);
772}
773
774static bool trans_srld(DisasContext *ctx, arg_srld *a)
775{
776    REQUIRE_128BIT(ctx);
777    ctx->ol = MXL_RV64;
778    return gen_shift(ctx, a, EXT_ZERO, tcg_gen_shr_tl, NULL);
779}
780
781static bool trans_srad(DisasContext *ctx, arg_srad *a)
782{
783    REQUIRE_128BIT(ctx);
784    ctx->ol = MXL_RV64;
785    return gen_shift(ctx, a, EXT_SIGN, tcg_gen_sar_tl, NULL);
786}
787
788static bool trans_pause(DisasContext *ctx, arg_pause *a)
789{
790    if (!ctx->cfg_ptr->ext_zihintpause) {
791        return false;
792    }
793
794    /*
795     * PAUSE is a no-op in QEMU,
796     * end the TB and return to main loop
797     */
798    gen_update_pc(ctx, ctx->cur_insn_len);
799    exit_tb(ctx);
800    ctx->base.is_jmp = DISAS_NORETURN;
801
802    return true;
803}
804
805static bool trans_fence(DisasContext *ctx, arg_fence *a)
806{
807    /* FENCE is a full memory barrier. */
808    tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
809    return true;
810}
811
812static bool trans_fence_i(DisasContext *ctx, arg_fence_i *a)
813{
814    if (!ctx->cfg_ptr->ext_zifencei) {
815        return false;
816    }
817
818    /*
819     * FENCE_I is a no-op in QEMU,
820     * however we need to end the translation block
821     */
822    gen_update_pc(ctx, ctx->cur_insn_len);
823    exit_tb(ctx);
824    ctx->base.is_jmp = DISAS_NORETURN;
825    return true;
826}
827
828static bool do_csr_post(DisasContext *ctx)
829{
830    /* The helper may raise ILLEGAL_INSN -- record binv for unwind. */
831    decode_save_opc(ctx);
832    /* We may have changed important cpu state -- exit to main loop. */
833    gen_update_pc(ctx, ctx->cur_insn_len);
834    exit_tb(ctx);
835    ctx->base.is_jmp = DISAS_NORETURN;
836    return true;
837}
838
839static bool do_csrr(DisasContext *ctx, int rd, int rc)
840{
841    TCGv dest = dest_gpr(ctx, rd);
842    TCGv_i32 csr = tcg_constant_i32(rc);
843
844    translator_io_start(&ctx->base);
845    gen_helper_csrr(dest, tcg_env, csr);
846    gen_set_gpr(ctx, rd, dest);
847    return do_csr_post(ctx);
848}
849
850static bool do_csrw(DisasContext *ctx, int rc, TCGv src)
851{
852    TCGv_i32 csr = tcg_constant_i32(rc);
853
854    translator_io_start(&ctx->base);
855    gen_helper_csrw(tcg_env, csr, src);
856    return do_csr_post(ctx);
857}
858
859static bool do_csrrw(DisasContext *ctx, int rd, int rc, TCGv src, TCGv mask)
860{
861    TCGv dest = dest_gpr(ctx, rd);
862    TCGv_i32 csr = tcg_constant_i32(rc);
863
864    translator_io_start(&ctx->base);
865    gen_helper_csrrw(dest, tcg_env, csr, src, mask);
866    gen_set_gpr(ctx, rd, dest);
867    return do_csr_post(ctx);
868}
869
870static bool do_csrr_i128(DisasContext *ctx, int rd, int rc)
871{
872    TCGv destl = dest_gpr(ctx, rd);
873    TCGv desth = dest_gprh(ctx, rd);
874    TCGv_i32 csr = tcg_constant_i32(rc);
875
876    translator_io_start(&ctx->base);
877    gen_helper_csrr_i128(destl, tcg_env, csr);
878    tcg_gen_ld_tl(desth, tcg_env, offsetof(CPURISCVState, retxh));
879    gen_set_gpr128(ctx, rd, destl, desth);
880    return do_csr_post(ctx);
881}
882
883static bool do_csrw_i128(DisasContext *ctx, int rc, TCGv srcl, TCGv srch)
884{
885    TCGv_i32 csr = tcg_constant_i32(rc);
886
887    translator_io_start(&ctx->base);
888    gen_helper_csrw_i128(tcg_env, csr, srcl, srch);
889    return do_csr_post(ctx);
890}
891
892static bool do_csrrw_i128(DisasContext *ctx, int rd, int rc,
893                          TCGv srcl, TCGv srch, TCGv maskl, TCGv maskh)
894{
895    TCGv destl = dest_gpr(ctx, rd);
896    TCGv desth = dest_gprh(ctx, rd);
897    TCGv_i32 csr = tcg_constant_i32(rc);
898
899    translator_io_start(&ctx->base);
900    gen_helper_csrrw_i128(destl, tcg_env, csr, srcl, srch, maskl, maskh);
901    tcg_gen_ld_tl(desth, tcg_env, offsetof(CPURISCVState, retxh));
902    gen_set_gpr128(ctx, rd, destl, desth);
903    return do_csr_post(ctx);
904}
905
906static bool trans_csrrw(DisasContext *ctx, arg_csrrw *a)
907{
908    RISCVMXL xl = get_xl(ctx);
909    if (xl < MXL_RV128) {
910        TCGv src = get_gpr(ctx, a->rs1, EXT_NONE);
911
912        /*
913         * If rd == 0, the insn shall not read the csr, nor cause any of the
914         * side effects that might occur on a csr read.
915         */
916        if (a->rd == 0) {
917            return do_csrw(ctx, a->csr, src);
918        }
919
920        TCGv mask = tcg_constant_tl(xl == MXL_RV32 ? UINT32_MAX :
921                                                     (target_ulong)-1);
922        return do_csrrw(ctx, a->rd, a->csr, src, mask);
923    } else {
924        TCGv srcl = get_gpr(ctx, a->rs1, EXT_NONE);
925        TCGv srch = get_gprh(ctx, a->rs1);
926
927        /*
928         * If rd == 0, the insn shall not read the csr, nor cause any of the
929         * side effects that might occur on a csr read.
930         */
931        if (a->rd == 0) {
932            return do_csrw_i128(ctx, a->csr, srcl, srch);
933        }
934
935        TCGv mask = tcg_constant_tl(-1);
936        return do_csrrw_i128(ctx, a->rd, a->csr, srcl, srch, mask, mask);
937    }
938}
939
940static bool trans_csrrs(DisasContext *ctx, arg_csrrs *a)
941{
942    /*
943     * If rs1 == 0, the insn shall not write to the csr at all, nor
944     * cause any of the side effects that might occur on a csr write.
945     * Note that if rs1 specifies a register other than x0, holding
946     * a zero value, the instruction will still attempt to write the
947     * unmodified value back to the csr and will cause side effects.
948     */
949    if (get_xl(ctx) < MXL_RV128) {
950        if (a->rs1 == 0) {
951            return do_csrr(ctx, a->rd, a->csr);
952        }
953
954        TCGv ones = tcg_constant_tl(-1);
955        TCGv mask = get_gpr(ctx, a->rs1, EXT_ZERO);
956        return do_csrrw(ctx, a->rd, a->csr, ones, mask);
957    } else {
958        if (a->rs1 == 0) {
959            return do_csrr_i128(ctx, a->rd, a->csr);
960        }
961
962        TCGv ones = tcg_constant_tl(-1);
963        TCGv maskl = get_gpr(ctx, a->rs1, EXT_ZERO);
964        TCGv maskh = get_gprh(ctx, a->rs1);
965        return do_csrrw_i128(ctx, a->rd, a->csr, ones, ones, maskl, maskh);
966    }
967}
968
969static bool trans_csrrc(DisasContext *ctx, arg_csrrc *a)
970{
971    /*
972     * If rs1 == 0, the insn shall not write to the csr at all, nor
973     * cause any of the side effects that might occur on a csr write.
974     * Note that if rs1 specifies a register other than x0, holding
975     * a zero value, the instruction will still attempt to write the
976     * unmodified value back to the csr and will cause side effects.
977     */
978    if (get_xl(ctx) < MXL_RV128) {
979        if (a->rs1 == 0) {
980            return do_csrr(ctx, a->rd, a->csr);
981        }
982
983        TCGv mask = get_gpr(ctx, a->rs1, EXT_ZERO);
984        return do_csrrw(ctx, a->rd, a->csr, ctx->zero, mask);
985    } else {
986        if (a->rs1 == 0) {
987            return do_csrr_i128(ctx, a->rd, a->csr);
988        }
989
990        TCGv maskl = get_gpr(ctx, a->rs1, EXT_ZERO);
991        TCGv maskh = get_gprh(ctx, a->rs1);
992        return do_csrrw_i128(ctx, a->rd, a->csr,
993                             ctx->zero, ctx->zero, maskl, maskh);
994    }
995}
996
997static bool trans_csrrwi(DisasContext *ctx, arg_csrrwi *a)
998{
999    RISCVMXL xl = get_xl(ctx);
1000    if (xl < MXL_RV128) {
1001        TCGv src = tcg_constant_tl(a->rs1);
1002
1003        /*
1004         * If rd == 0, the insn shall not read the csr, nor cause any of the
1005         * side effects that might occur on a csr read.
1006         */
1007        if (a->rd == 0) {
1008            return do_csrw(ctx, a->csr, src);
1009        }
1010
1011        TCGv mask = tcg_constant_tl(xl == MXL_RV32 ? UINT32_MAX :
1012                                                     (target_ulong)-1);
1013        return do_csrrw(ctx, a->rd, a->csr, src, mask);
1014    } else {
1015        TCGv src = tcg_constant_tl(a->rs1);
1016
1017        /*
1018         * If rd == 0, the insn shall not read the csr, nor cause any of the
1019         * side effects that might occur on a csr read.
1020         */
1021        if (a->rd == 0) {
1022            return do_csrw_i128(ctx, a->csr, src, ctx->zero);
1023        }
1024
1025        TCGv mask = tcg_constant_tl(-1);
1026        return do_csrrw_i128(ctx, a->rd, a->csr, src, ctx->zero, mask, mask);
1027    }
1028}
1029
1030static bool trans_csrrsi(DisasContext *ctx, arg_csrrsi *a)
1031{
1032    /*
1033     * If rs1 == 0, the insn shall not write to the csr at all, nor
1034     * cause any of the side effects that might occur on a csr write.
1035     * Note that if rs1 specifies a register other than x0, holding
1036     * a zero value, the instruction will still attempt to write the
1037     * unmodified value back to the csr and will cause side effects.
1038     */
1039    if (get_xl(ctx) < MXL_RV128) {
1040        if (a->rs1 == 0) {
1041            return do_csrr(ctx, a->rd, a->csr);
1042        }
1043
1044        TCGv ones = tcg_constant_tl(-1);
1045        TCGv mask = tcg_constant_tl(a->rs1);
1046        return do_csrrw(ctx, a->rd, a->csr, ones, mask);
1047    } else {
1048        if (a->rs1 == 0) {
1049            return do_csrr_i128(ctx, a->rd, a->csr);
1050        }
1051
1052        TCGv ones = tcg_constant_tl(-1);
1053        TCGv mask = tcg_constant_tl(a->rs1);
1054        return do_csrrw_i128(ctx, a->rd, a->csr, ones, ones, mask, ctx->zero);
1055    }
1056}
1057
1058static bool trans_csrrci(DisasContext *ctx, arg_csrrci * a)
1059{
1060    /*
1061     * If rs1 == 0, the insn shall not write to the csr at all, nor
1062     * cause any of the side effects that might occur on a csr write.
1063     * Note that if rs1 specifies a register other than x0, holding
1064     * a zero value, the instruction will still attempt to write the
1065     * unmodified value back to the csr and will cause side effects.
1066     */
1067    if (get_xl(ctx) < MXL_RV128) {
1068        if (a->rs1 == 0) {
1069            return do_csrr(ctx, a->rd, a->csr);
1070        }
1071
1072        TCGv mask = tcg_constant_tl(a->rs1);
1073        return do_csrrw(ctx, a->rd, a->csr, ctx->zero, mask);
1074    } else {
1075        if (a->rs1 == 0) {
1076            return do_csrr_i128(ctx, a->rd, a->csr);
1077        }
1078
1079        TCGv mask = tcg_constant_tl(a->rs1);
1080        return do_csrrw_i128(ctx, a->rd, a->csr,
1081                             ctx->zero, ctx->zero, mask, ctx->zero);
1082    }
1083}
1084