1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
|
/*
* RISC-V translation routines for the T-Head vendor extensions (xthead*).
*
* Copyright (c) 2022 VRULL GmbH.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2 or later, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define REQUIRE_XTHEADBA(ctx) do { \
if (!ctx->cfg_ptr->ext_xtheadba) { \
return false; \
} \
} while (0)
#define REQUIRE_XTHEADBB(ctx) do { \
if (!ctx->cfg_ptr->ext_xtheadbb) { \
return false; \
} \
} while (0)
#define REQUIRE_XTHEADBS(ctx) do { \
if (!ctx->cfg_ptr->ext_xtheadbs) { \
return false; \
} \
} while (0)
#define REQUIRE_XTHEADCMO(ctx) do { \
if (!ctx->cfg_ptr->ext_xtheadcmo) { \
return false; \
} \
} while (0)
#define REQUIRE_XTHEADCONDMOV(ctx) do { \
if (!ctx->cfg_ptr->ext_xtheadcondmov) { \
return false; \
} \
} while (0)
#define REQUIRE_XTHEADFMEMIDX(ctx) do { \
if (!ctx->cfg_ptr->ext_xtheadfmemidx) { \
return false; \
} \
} while (0)
#define REQUIRE_XTHEADFMV(ctx) do { \
if (!ctx->cfg_ptr->ext_xtheadfmv) { \
return false; \
} \
} while (0)
#define REQUIRE_XTHEADMAC(ctx) do { \
if (!ctx->cfg_ptr->ext_xtheadmac) { \
return false; \
} \
} while (0)
#define REQUIRE_XTHEADMEMIDX(ctx) do { \
if (!ctx->cfg_ptr->ext_xtheadmemidx) { \
return false; \
} \
} while (0)
#define REQUIRE_XTHEADMEMPAIR(ctx) do { \
if (!ctx->cfg_ptr->ext_xtheadmempair) { \
return false; \
} \
} while (0)
#define REQUIRE_XTHEADSYNC(ctx) do { \
if (!ctx->cfg_ptr->ext_xtheadsync) { \
return false; \
} \
} while (0)
/*
* Calculate and return the address for indexed mem operations:
* If !zext_offs, then the address is rs1 + (rs2 << imm2).
* If zext_offs, then the address is rs1 + (zext(rs2[31:0]) << imm2).
*/
static TCGv get_th_address_indexed(DisasContext *ctx, int rs1, int rs2,
int imm2, bool zext_offs)
{
TCGv src2 = get_gpr(ctx, rs2, EXT_NONE);
TCGv offs = tcg_temp_new();
if (zext_offs) {
tcg_gen_extract_tl(offs, src2, 0, 32);
tcg_gen_shli_tl(offs, offs, imm2);
} else {
tcg_gen_shli_tl(offs, src2, imm2);
}
return get_address_indexed(ctx, rs1, offs);
}
/* XTheadBa */
/*
* th.addsl is similar to sh[123]add (from Zba), but not an
* alternative encoding: while sh[123] applies the shift to rs1,
* th.addsl shifts rs2.
*/
#define GEN_TH_ADDSL(SHAMT) \
static void gen_th_addsl##SHAMT(TCGv ret, TCGv arg1, TCGv arg2) \
{ \
TCGv t = tcg_temp_new(); \
tcg_gen_shli_tl(t, arg2, SHAMT); \
tcg_gen_add_tl(ret, t, arg1); \
}
GEN_TH_ADDSL(1)
GEN_TH_ADDSL(2)
GEN_TH_ADDSL(3)
#define GEN_TRANS_TH_ADDSL(SHAMT) \
static bool trans_th_addsl##SHAMT(DisasContext *ctx, \
arg_th_addsl##SHAMT * a) \
{ \
REQUIRE_XTHEADBA(ctx); \
return gen_arith(ctx, a, EXT_NONE, gen_th_addsl##SHAMT, NULL); \
}
GEN_TRANS_TH_ADDSL(1)
GEN_TRANS_TH_ADDSL(2)
GEN_TRANS_TH_ADDSL(3)
/* XTheadBb */
/* th.srri is an alternate encoding for rori (from Zbb) */
static bool trans_th_srri(DisasContext *ctx, arg_th_srri * a)
{
REQUIRE_XTHEADBB(ctx);
return gen_shift_imm_fn_per_ol(ctx, a, EXT_NONE,
tcg_gen_rotri_tl, gen_roriw, NULL);
}
/* th.srriw is an alternate encoding for roriw (from Zbb) */
static bool trans_th_srriw(DisasContext *ctx, arg_th_srriw *a)
{
REQUIRE_XTHEADBB(ctx);
REQUIRE_64BIT(ctx);
ctx->ol = MXL_RV32;
return gen_shift_imm_fn(ctx, a, EXT_NONE, gen_roriw, NULL);
}
/* th.ext and th.extu perform signed/unsigned bitfield extraction */
static bool gen_th_bfextract(DisasContext *ctx, arg_th_bfext *a,
void (*f)(TCGv, TCGv, unsigned int, unsigned int))
{
TCGv dest = dest_gpr(ctx, a->rd);
TCGv source = get_gpr(ctx, a->rs1, EXT_ZERO);
if (a->lsb <= a->msb) {
f(dest, source, a->lsb, a->msb - a->lsb + 1);
gen_set_gpr(ctx, a->rd, dest);
}
return true;
}
static bool trans_th_ext(DisasContext *ctx, arg_th_ext *a)
{
REQUIRE_XTHEADBB(ctx);
return gen_th_bfextract(ctx, a, tcg_gen_sextract_tl);
}
static bool trans_th_extu(DisasContext *ctx, arg_th_extu *a)
{
REQUIRE_XTHEADBB(ctx);
return gen_th_bfextract(ctx, a, tcg_gen_extract_tl);
}
/* th.ff0: find first zero (clz on an inverted input) */
static bool gen_th_ff0(DisasContext *ctx, arg_th_ff0 *a, DisasExtend ext)
{
TCGv dest = dest_gpr(ctx, a->rd);
TCGv src1 = get_gpr(ctx, a->rs1, ext);
int olen = get_olen(ctx);
TCGv t = tcg_temp_new();
tcg_gen_not_tl(t, src1);
if (olen != TARGET_LONG_BITS) {
if (olen == 32) {
gen_clzw(dest, t);
} else {
g_assert_not_reached();
}
} else {
gen_clz(dest, t);
}
gen_set_gpr(ctx, a->rd, dest);
return true;
}
static bool trans_th_ff0(DisasContext *ctx, arg_th_ff0 *a)
{
REQUIRE_XTHEADBB(ctx);
return gen_th_ff0(ctx, a, EXT_NONE);
}
/* th.ff1 is an alternate encoding for clz (from Zbb) */
static bool trans_th_ff1(DisasContext *ctx, arg_th_ff1 *a)
{
REQUIRE_XTHEADBB(ctx);
return gen_unary_per_ol(ctx, a, EXT_NONE, gen_clz, gen_clzw);
}
static void gen_th_revw(TCGv ret, TCGv arg1)
{
tcg_gen_bswap32_tl(ret, arg1, TCG_BSWAP_OS);
}
/* th.rev is an alternate encoding for the RV64 rev8 (from Zbb) */
static bool trans_th_rev(DisasContext *ctx, arg_th_rev *a)
{
REQUIRE_XTHEADBB(ctx);
return gen_unary_per_ol(ctx, a, EXT_NONE, tcg_gen_bswap_tl, gen_th_revw);
}
/* th.revw is a sign-extended byte-swap of the lower word */
static bool trans_th_revw(DisasContext *ctx, arg_th_revw *a)
{
REQUIRE_XTHEADBB(ctx);
REQUIRE_64BIT(ctx);
return gen_unary(ctx, a, EXT_NONE, gen_th_revw);
}
/* th.tstnbz is equivalent to an orc.b (from Zbb) with inverted result */
static void gen_th_tstnbz(TCGv ret, TCGv source1)
{
gen_orc_b(ret, source1);
tcg_gen_not_tl(ret, ret);
}
static bool trans_th_tstnbz(DisasContext *ctx, arg_th_tstnbz *a)
{
REQUIRE_XTHEADBB(ctx);
return gen_unary(ctx, a, EXT_ZERO, gen_th_tstnbz);
}
/* XTheadBs */
/* th.tst is an alternate encoding for bexti (from Zbs) */
static bool trans_th_tst(DisasContext *ctx, arg_th_tst *a)
{
REQUIRE_XTHEADBS(ctx);
return gen_shift_imm_tl(ctx, a, EXT_NONE, gen_bext);
}
/* XTheadCmo */
/* Test if priv level is M, S, or U (cannot fail). */
#define REQUIRE_PRIV_MSU(ctx)
/* Test if priv level is M or S. */
#define REQUIRE_PRIV_MS(ctx) \
do { \
if (ctx->priv == PRV_U) { \
return false; \
} \
} while (0)
#define NOP_PRIVCHECK(insn, extcheck, privcheck) \
static bool trans_ ## insn(DisasContext *ctx, arg_ ## insn * a) \
{ \
(void) a; \
extcheck(ctx); \
privcheck(ctx); \
return true; \
}
NOP_PRIVCHECK(th_dcache_call, REQUIRE_XTHEADCMO, REQUIRE_PRIV_MS)
NOP_PRIVCHECK(th_dcache_ciall, REQUIRE_XTHEADCMO, REQUIRE_PRIV_MS)
NOP_PRIVCHECK(th_dcache_iall, REQUIRE_XTHEADCMO, REQUIRE_PRIV_MS)
NOP_PRIVCHECK(th_dcache_cpa, REQUIRE_XTHEADCMO, REQUIRE_PRIV_MS)
NOP_PRIVCHECK(th_dcache_cipa, REQUIRE_XTHEADCMO, REQUIRE_PRIV_MS)
NOP_PRIVCHECK(th_dcache_ipa, REQUIRE_XTHEADCMO, REQUIRE_PRIV_MS)
NOP_PRIVCHECK(th_dcache_cva, REQUIRE_XTHEADCMO, REQUIRE_PRIV_MSU)
NOP_PRIVCHECK(th_dcache_civa, REQUIRE_XTHEADCMO, REQUIRE_PRIV_MSU)
NOP_PRIVCHECK(th_dcache_iva, REQUIRE_XTHEADCMO, REQUIRE_PRIV_MSU)
NOP_PRIVCHECK(th_dcache_csw, REQUIRE_XTHEADCMO, REQUIRE_PRIV_MS)
NOP_PRIVCHECK(th_dcache_cisw, REQUIRE_XTHEADCMO, REQUIRE_PRIV_MS)
NOP_PRIVCHECK(th_dcache_isw, REQUIRE_XTHEADCMO, REQUIRE_PRIV_MS)
NOP_PRIVCHECK(th_dcache_cpal1, REQUIRE_XTHEADCMO, REQUIRE_PRIV_MS)
NOP_PRIVCHECK(th_dcache_cval1, REQUIRE_XTHEADCMO, REQUIRE_PRIV_MSU)
NOP_PRIVCHECK(th_icache_iall, REQUIRE_XTHEADCMO, REQUIRE_PRIV_MS)
NOP_PRIVCHECK(th_icache_ialls, REQUIRE_XTHEADCMO, REQUIRE_PRIV_MS)
NOP_PRIVCHECK(th_icache_ipa, REQUIRE_XTHEADCMO, REQUIRE_PRIV_MS)
NOP_PRIVCHECK(th_icache_iva, REQUIRE_XTHEADCMO, REQUIRE_PRIV_MSU)
NOP_PRIVCHECK(th_l2cache_call, REQUIRE_XTHEADCMO, REQUIRE_PRIV_MS)
NOP_PRIVCHECK(th_l2cache_ciall, REQUIRE_XTHEADCMO, REQUIRE_PRIV_MS)
NOP_PRIVCHECK(th_l2cache_iall, REQUIRE_XTHEADCMO, REQUIRE_PRIV_MS)
/* XTheadCondMov */
static bool gen_th_condmove(DisasContext *ctx, arg_r *a, TCGCond cond)
{
TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE);
TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE);
TCGv old = get_gpr(ctx, a->rd, EXT_NONE);
TCGv dest = dest_gpr(ctx, a->rd);
tcg_gen_movcond_tl(cond, dest, src2, ctx->zero, src1, old);
gen_set_gpr(ctx, a->rd, dest);
return true;
}
/* th.mveqz: "if (rs2 == 0) rd = rs1;" */
static bool trans_th_mveqz(DisasContext *ctx, arg_th_mveqz *a)
{
REQUIRE_XTHEADCONDMOV(ctx);
return gen_th_condmove(ctx, a, TCG_COND_EQ);
}
/* th.mvnez: "if (rs2 != 0) rd = rs1;" */
static bool trans_th_mvnez(DisasContext *ctx, arg_th_mveqz *a)
{
REQUIRE_XTHEADCONDMOV(ctx);
return gen_th_condmove(ctx, a, TCG_COND_NE);
}
/* XTheadFMem */
/*
* Load 64-bit float from indexed address.
* If !zext_offs, then address is rs1 + (rs2 << imm2).
* If zext_offs, then address is rs1 + (zext(rs2[31:0]) << imm2).
*/
static bool gen_fload_idx(DisasContext *ctx, arg_th_memidx *a, MemOp memop,
bool zext_offs)
{
TCGv_i64 rd = cpu_fpr[a->rd];
TCGv addr = get_th_address_indexed(ctx, a->rs1, a->rs2, a->imm2, zext_offs);
tcg_gen_qemu_ld_i64(rd, addr, ctx->mem_idx, memop);
if ((memop & MO_SIZE) == MO_32) {
gen_nanbox_s(rd, rd);
}
mark_fs_dirty(ctx);
return true;
}
/*
* Store 64-bit float to indexed address.
* If !zext_offs, then address is rs1 + (rs2 << imm2).
* If zext_offs, then address is rs1 + (zext(rs2[31:0]) << imm2).
*/
static bool gen_fstore_idx(DisasContext *ctx, arg_th_memidx *a, MemOp memop,
bool zext_offs)
{
TCGv_i64 rd = cpu_fpr[a->rd];
TCGv addr = get_th_address_indexed(ctx, a->rs1, a->rs2, a->imm2, zext_offs);
tcg_gen_qemu_st_i64(rd, addr, ctx->mem_idx, memop);
return true;
}
static bool trans_th_flrd(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADFMEMIDX(ctx);
REQUIRE_FPU;
REQUIRE_EXT(ctx, RVD);
return gen_fload_idx(ctx, a, MO_TEUQ, false);
}
static bool trans_th_flrw(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADFMEMIDX(ctx);
REQUIRE_FPU;
REQUIRE_EXT(ctx, RVF);
return gen_fload_idx(ctx, a, MO_TEUL, false);
}
static bool trans_th_flurd(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADFMEMIDX(ctx);
REQUIRE_FPU;
REQUIRE_EXT(ctx, RVD);
return gen_fload_idx(ctx, a, MO_TEUQ, true);
}
static bool trans_th_flurw(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADFMEMIDX(ctx);
REQUIRE_FPU;
REQUIRE_EXT(ctx, RVF);
return gen_fload_idx(ctx, a, MO_TEUL, true);
}
static bool trans_th_fsrd(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADFMEMIDX(ctx);
REQUIRE_FPU;
REQUIRE_EXT(ctx, RVD);
return gen_fstore_idx(ctx, a, MO_TEUQ, false);
}
static bool trans_th_fsrw(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADFMEMIDX(ctx);
REQUIRE_FPU;
REQUIRE_EXT(ctx, RVF);
return gen_fstore_idx(ctx, a, MO_TEUL, false);
}
static bool trans_th_fsurd(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADFMEMIDX(ctx);
REQUIRE_FPU;
REQUIRE_EXT(ctx, RVD);
return gen_fstore_idx(ctx, a, MO_TEUQ, true);
}
static bool trans_th_fsurw(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADFMEMIDX(ctx);
REQUIRE_FPU;
REQUIRE_EXT(ctx, RVF);
return gen_fstore_idx(ctx, a, MO_TEUL, true);
}
/* XTheadFmv */
static bool trans_th_fmv_hw_x(DisasContext *ctx, arg_th_fmv_hw_x *a)
{
REQUIRE_XTHEADFMV(ctx);
REQUIRE_32BIT(ctx);
REQUIRE_FPU;
REQUIRE_EXT(ctx, RVD);
TCGv src1 = get_gpr(ctx, a->rs1, EXT_ZERO);
TCGv_i64 t1 = tcg_temp_new_i64();
tcg_gen_extu_tl_i64(t1, src1);
tcg_gen_deposit_i64(cpu_fpr[a->rd], cpu_fpr[a->rd], t1, 32, 32);
mark_fs_dirty(ctx);
return true;
}
static bool trans_th_fmv_x_hw(DisasContext *ctx, arg_th_fmv_x_hw *a)
{
REQUIRE_XTHEADFMV(ctx);
REQUIRE_32BIT(ctx);
REQUIRE_FPU;
REQUIRE_EXT(ctx, RVD);
TCGv dst;
TCGv_i64 t1;
dst = dest_gpr(ctx, a->rd);
t1 = tcg_temp_new_i64();
tcg_gen_extract_i64(t1, cpu_fpr[a->rs1], 32, 32);
tcg_gen_trunc_i64_tl(dst, t1);
gen_set_gpr(ctx, a->rd, dst);
mark_fs_dirty(ctx);
return true;
}
/* XTheadMac */
static bool gen_th_mac(DisasContext *ctx, arg_r *a,
void (*accumulate_func)(TCGv, TCGv, TCGv),
void (*extend_operand_func)(TCGv, TCGv))
{
TCGv dest = dest_gpr(ctx, a->rd);
TCGv src0 = get_gpr(ctx, a->rd, EXT_NONE);
TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE);
TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE);
TCGv tmp = tcg_temp_new();
if (extend_operand_func) {
TCGv tmp2 = tcg_temp_new();
extend_operand_func(tmp, src1);
extend_operand_func(tmp2, src2);
tcg_gen_mul_tl(tmp, tmp, tmp2);
} else {
tcg_gen_mul_tl(tmp, src1, src2);
}
accumulate_func(dest, src0, tmp);
gen_set_gpr(ctx, a->rd, dest);
return true;
}
/* th.mula: "rd = rd + rs1 * rs2" */
static bool trans_th_mula(DisasContext *ctx, arg_th_mula *a)
{
REQUIRE_XTHEADMAC(ctx);
return gen_th_mac(ctx, a, tcg_gen_add_tl, NULL);
}
/* th.mulah: "rd = sext.w(rd + sext.w(rs1[15:0]) * sext.w(rs2[15:0]))" */
static bool trans_th_mulah(DisasContext *ctx, arg_th_mulah *a)
{
REQUIRE_XTHEADMAC(ctx);
ctx->ol = MXL_RV32;
return gen_th_mac(ctx, a, tcg_gen_add_tl, tcg_gen_ext16s_tl);
}
/* th.mulaw: "rd = sext.w(rd + rs1 * rs2)" */
static bool trans_th_mulaw(DisasContext *ctx, arg_th_mulaw *a)
{
REQUIRE_XTHEADMAC(ctx);
REQUIRE_64BIT(ctx);
ctx->ol = MXL_RV32;
return gen_th_mac(ctx, a, tcg_gen_add_tl, NULL);
}
/* th.muls: "rd = rd - rs1 * rs2" */
static bool trans_th_muls(DisasContext *ctx, arg_th_muls *a)
{
REQUIRE_XTHEADMAC(ctx);
return gen_th_mac(ctx, a, tcg_gen_sub_tl, NULL);
}
/* th.mulsh: "rd = sext.w(rd - sext.w(rs1[15:0]) * sext.w(rs2[15:0]))" */
static bool trans_th_mulsh(DisasContext *ctx, arg_th_mulsh *a)
{
REQUIRE_XTHEADMAC(ctx);
ctx->ol = MXL_RV32;
return gen_th_mac(ctx, a, tcg_gen_sub_tl, tcg_gen_ext16s_tl);
}
/* th.mulsw: "rd = sext.w(rd - rs1 * rs2)" */
static bool trans_th_mulsw(DisasContext *ctx, arg_th_mulsw *a)
{
REQUIRE_XTHEADMAC(ctx);
REQUIRE_64BIT(ctx);
ctx->ol = MXL_RV32;
return gen_th_mac(ctx, a, tcg_gen_sub_tl, NULL);
}
/* XTheadMemIdx */
/*
* Load with memop from indexed address and add (imm5 << imm2) to rs1.
* If !preinc, then the load address is rs1.
* If preinc, then the load address is rs1 + (imm5) << imm2).
*/
static bool gen_load_inc(DisasContext *ctx, arg_th_meminc *a, MemOp memop,
bool preinc)
{
if (a->rs1 == a->rd) {
return false;
}
int imm = a->imm5 << a->imm2;
TCGv addr = get_address(ctx, a->rs1, preinc ? imm : 0);
TCGv rd = dest_gpr(ctx, a->rd);
TCGv rs1 = get_gpr(ctx, a->rs1, EXT_NONE);
tcg_gen_qemu_ld_tl(rd, addr, ctx->mem_idx, memop);
tcg_gen_addi_tl(rs1, rs1, imm);
gen_set_gpr(ctx, a->rd, rd);
gen_set_gpr(ctx, a->rs1, rs1);
return true;
}
/*
* Store with memop to indexed address and add (imm5 << imm2) to rs1.
* If !preinc, then the store address is rs1.
* If preinc, then the store address is rs1 + (imm5) << imm2).
*/
static bool gen_store_inc(DisasContext *ctx, arg_th_meminc *a, MemOp memop,
bool preinc)
{
int imm = a->imm5 << a->imm2;
TCGv addr = get_address(ctx, a->rs1, preinc ? imm : 0);
TCGv data = get_gpr(ctx, a->rd, EXT_NONE);
TCGv rs1 = get_gpr(ctx, a->rs1, EXT_NONE);
tcg_gen_qemu_st_tl(data, addr, ctx->mem_idx, memop);
tcg_gen_addi_tl(rs1, rs1, imm);
gen_set_gpr(ctx, a->rs1, rs1);
return true;
}
static bool trans_th_ldia(DisasContext *ctx, arg_th_meminc *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
REQUIRE_64BIT(ctx);
return gen_load_inc(ctx, a, MO_TESQ, false);
}
static bool trans_th_ldib(DisasContext *ctx, arg_th_meminc *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
REQUIRE_64BIT(ctx);
return gen_load_inc(ctx, a, MO_TESQ, true);
}
static bool trans_th_lwia(DisasContext *ctx, arg_th_meminc *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_load_inc(ctx, a, MO_TESL, false);
}
static bool trans_th_lwib(DisasContext *ctx, arg_th_meminc *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_load_inc(ctx, a, MO_TESL, true);
}
static bool trans_th_lwuia(DisasContext *ctx, arg_th_meminc *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
REQUIRE_64BIT(ctx);
return gen_load_inc(ctx, a, MO_TEUL, false);
}
static bool trans_th_lwuib(DisasContext *ctx, arg_th_meminc *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
REQUIRE_64BIT(ctx);
return gen_load_inc(ctx, a, MO_TEUL, true);
}
static bool trans_th_lhia(DisasContext *ctx, arg_th_meminc *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_load_inc(ctx, a, MO_TESW, false);
}
static bool trans_th_lhib(DisasContext *ctx, arg_th_meminc *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_load_inc(ctx, a, MO_TESW, true);
}
static bool trans_th_lhuia(DisasContext *ctx, arg_th_meminc *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_load_inc(ctx, a, MO_TEUW, false);
}
static bool trans_th_lhuib(DisasContext *ctx, arg_th_meminc *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_load_inc(ctx, a, MO_TEUW, true);
}
static bool trans_th_lbia(DisasContext *ctx, arg_th_meminc *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_load_inc(ctx, a, MO_SB, false);
}
static bool trans_th_lbib(DisasContext *ctx, arg_th_meminc *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_load_inc(ctx, a, MO_SB, true);
}
static bool trans_th_lbuia(DisasContext *ctx, arg_th_meminc *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_load_inc(ctx, a, MO_UB, false);
}
static bool trans_th_lbuib(DisasContext *ctx, arg_th_meminc *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_load_inc(ctx, a, MO_UB, true);
}
static bool trans_th_sdia(DisasContext *ctx, arg_th_meminc *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
REQUIRE_64BIT(ctx);
return gen_store_inc(ctx, a, MO_TESQ, false);
}
static bool trans_th_sdib(DisasContext *ctx, arg_th_meminc *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
REQUIRE_64BIT(ctx);
return gen_store_inc(ctx, a, MO_TESQ, true);
}
static bool trans_th_swia(DisasContext *ctx, arg_th_meminc *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_store_inc(ctx, a, MO_TESL, false);
}
static bool trans_th_swib(DisasContext *ctx, arg_th_meminc *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_store_inc(ctx, a, MO_TESL, true);
}
static bool trans_th_shia(DisasContext *ctx, arg_th_meminc *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_store_inc(ctx, a, MO_TESW, false);
}
static bool trans_th_shib(DisasContext *ctx, arg_th_meminc *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_store_inc(ctx, a, MO_TESW, true);
}
static bool trans_th_sbia(DisasContext *ctx, arg_th_meminc *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_store_inc(ctx, a, MO_SB, false);
}
static bool trans_th_sbib(DisasContext *ctx, arg_th_meminc *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_store_inc(ctx, a, MO_SB, true);
}
/*
* Load with memop from indexed address.
* If !zext_offs, then address is rs1 + (rs2 << imm2).
* If zext_offs, then address is rs1 + (zext(rs2[31:0]) << imm2).
*/
static bool gen_load_idx(DisasContext *ctx, arg_th_memidx *a, MemOp memop,
bool zext_offs)
{
TCGv rd = dest_gpr(ctx, a->rd);
TCGv addr = get_th_address_indexed(ctx, a->rs1, a->rs2, a->imm2, zext_offs);
tcg_gen_qemu_ld_tl(rd, addr, ctx->mem_idx, memop);
gen_set_gpr(ctx, a->rd, rd);
return true;
}
/*
* Store with memop to indexed address.
* If !zext_offs, then address is rs1 + (rs2 << imm2).
* If zext_offs, then address is rs1 + (zext(rs2[31:0]) << imm2).
*/
static bool gen_store_idx(DisasContext *ctx, arg_th_memidx *a, MemOp memop,
bool zext_offs)
{
TCGv data = get_gpr(ctx, a->rd, EXT_NONE);
TCGv addr = get_th_address_indexed(ctx, a->rs1, a->rs2, a->imm2, zext_offs);
tcg_gen_qemu_st_tl(data, addr, ctx->mem_idx, memop);
return true;
}
static bool trans_th_lrd(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
REQUIRE_64BIT(ctx);
return gen_load_idx(ctx, a, MO_TESQ, false);
}
static bool trans_th_lrw(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_load_idx(ctx, a, MO_TESL, false);
}
static bool trans_th_lrwu(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
REQUIRE_64BIT(ctx);
return gen_load_idx(ctx, a, MO_TEUL, false);
}
static bool trans_th_lrh(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_load_idx(ctx, a, MO_TESW, false);
}
static bool trans_th_lrhu(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_load_idx(ctx, a, MO_TEUW, false);
}
static bool trans_th_lrb(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_load_idx(ctx, a, MO_SB, false);
}
static bool trans_th_lrbu(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_load_idx(ctx, a, MO_UB, false);
}
static bool trans_th_srd(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
REQUIRE_64BIT(ctx);
return gen_store_idx(ctx, a, MO_TESQ, false);
}
static bool trans_th_srw(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_store_idx(ctx, a, MO_TESL, false);
}
static bool trans_th_srh(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_store_idx(ctx, a, MO_TESW, false);
}
static bool trans_th_srb(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_store_idx(ctx, a, MO_SB, false);
}
static bool trans_th_lurd(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
REQUIRE_64BIT(ctx);
return gen_load_idx(ctx, a, MO_TESQ, true);
}
static bool trans_th_lurw(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_load_idx(ctx, a, MO_TESL, true);
}
static bool trans_th_lurwu(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
REQUIRE_64BIT(ctx);
return gen_load_idx(ctx, a, MO_TEUL, true);
}
static bool trans_th_lurh(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_load_idx(ctx, a, MO_TESW, true);
}
static bool trans_th_lurhu(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_load_idx(ctx, a, MO_TEUW, true);
}
static bool trans_th_lurb(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_load_idx(ctx, a, MO_SB, true);
}
static bool trans_th_lurbu(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_load_idx(ctx, a, MO_UB, true);
}
static bool trans_th_surd(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
REQUIRE_64BIT(ctx);
return gen_store_idx(ctx, a, MO_TESQ, true);
}
static bool trans_th_surw(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_store_idx(ctx, a, MO_TESL, true);
}
static bool trans_th_surh(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_store_idx(ctx, a, MO_TESW, true);
}
static bool trans_th_surb(DisasContext *ctx, arg_th_memidx *a)
{
REQUIRE_XTHEADMEMIDX(ctx);
return gen_store_idx(ctx, a, MO_SB, true);
}
/* XTheadMemPair */
static bool gen_loadpair_tl(DisasContext *ctx, arg_th_pair *a, MemOp memop,
int shamt)
{
if (a->rs == a->rd1 || a->rs == a->rd2 || a->rd1 == a->rd2) {
return false;
}
TCGv t1 = tcg_temp_new();
TCGv t2 = tcg_temp_new();
TCGv addr1 = tcg_temp_new();
TCGv addr2 = tcg_temp_new();
int imm = a->sh2 << shamt;
addr1 = get_address(ctx, a->rs, imm);
addr2 = get_address(ctx, a->rs, memop_size(memop) + imm);
tcg_gen_qemu_ld_tl(t1, addr1, ctx->mem_idx, memop);
tcg_gen_qemu_ld_tl(t2, addr2, ctx->mem_idx, memop);
gen_set_gpr(ctx, a->rd1, t1);
gen_set_gpr(ctx, a->rd2, t2);
return true;
}
static bool trans_th_ldd(DisasContext *ctx, arg_th_pair *a)
{
REQUIRE_XTHEADMEMPAIR(ctx);
REQUIRE_64BIT(ctx);
return gen_loadpair_tl(ctx, a, MO_TESQ, 4);
}
static bool trans_th_lwd(DisasContext *ctx, arg_th_pair *a)
{
REQUIRE_XTHEADMEMPAIR(ctx);
return gen_loadpair_tl(ctx, a, MO_TESL, 3);
}
static bool trans_th_lwud(DisasContext *ctx, arg_th_pair *a)
{
REQUIRE_XTHEADMEMPAIR(ctx);
return gen_loadpair_tl(ctx, a, MO_TEUL, 3);
}
static bool gen_storepair_tl(DisasContext *ctx, arg_th_pair *a, MemOp memop,
int shamt)
{
TCGv data1 = get_gpr(ctx, a->rd1, EXT_NONE);
TCGv data2 = get_gpr(ctx, a->rd2, EXT_NONE);
TCGv addr1 = tcg_temp_new();
TCGv addr2 = tcg_temp_new();
int imm = a->sh2 << shamt;
addr1 = get_address(ctx, a->rs, imm);
addr2 = get_address(ctx, a->rs, memop_size(memop) + imm);
tcg_gen_qemu_st_tl(data1, addr1, ctx->mem_idx, memop);
tcg_gen_qemu_st_tl(data2, addr2, ctx->mem_idx, memop);
return true;
}
static bool trans_th_sdd(DisasContext *ctx, arg_th_pair *a)
{
REQUIRE_XTHEADMEMPAIR(ctx);
REQUIRE_64BIT(ctx);
return gen_storepair_tl(ctx, a, MO_TESQ, 4);
}
static bool trans_th_swd(DisasContext *ctx, arg_th_pair *a)
{
REQUIRE_XTHEADMEMPAIR(ctx);
return gen_storepair_tl(ctx, a, MO_TESL, 3);
}
/* XTheadSync */
static bool trans_th_sfence_vmas(DisasContext *ctx, arg_th_sfence_vmas *a)
{
(void) a;
REQUIRE_XTHEADSYNC(ctx);
#ifndef CONFIG_USER_ONLY
REQUIRE_PRIV_MS(ctx);
gen_helper_tlb_flush_all(tcg_env);
return true;
#else
return false;
#endif
}
#ifndef CONFIG_USER_ONLY
static void gen_th_sync_local(DisasContext *ctx)
{
/*
* Emulate out-of-order barriers with pipeline flush
* by exiting the translation block.
*/
gen_update_pc(ctx, ctx->cur_insn_len);
tcg_gen_exit_tb(NULL, 0);
ctx->base.is_jmp = DISAS_NORETURN;
}
#endif
static bool trans_th_sync(DisasContext *ctx, arg_th_sync *a)
{
(void) a;
REQUIRE_XTHEADSYNC(ctx);
#ifndef CONFIG_USER_ONLY
REQUIRE_PRIV_MSU(ctx);
/*
* th.sync is an out-of-order barrier.
*/
gen_th_sync_local(ctx);
return true;
#else
return false;
#endif
}
static bool trans_th_sync_i(DisasContext *ctx, arg_th_sync_i *a)
{
(void) a;
REQUIRE_XTHEADSYNC(ctx);
#ifndef CONFIG_USER_ONLY
REQUIRE_PRIV_MSU(ctx);
/*
* th.sync.i is th.sync plus pipeline flush.
*/
gen_th_sync_local(ctx);
return true;
#else
return false;
#endif
}
static bool trans_th_sync_is(DisasContext *ctx, arg_th_sync_is *a)
{
/* This instruction has the same behaviour like th.sync.i. */
return trans_th_sync_i(ctx, a);
}
static bool trans_th_sync_s(DisasContext *ctx, arg_th_sync_s *a)
{
/* This instruction has the same behaviour like th.sync. */
return trans_th_sync(ctx, a);
}
|