-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinterpreter.c
5309 lines (5189 loc) · 325 KB
/
interpreter.c
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
#include "qemu/osdep.h"
#include "cpu.h"
#include "internals.h"
#include "tcg/tcg-gvec-desc.h"
#include "fpu/softfloat.h"
#if defined(CONFIG_USER_ONLY)
#include "user.h"
#endif
#include "util.h"
#include <stdalign.h>
#ifndef CONFIG_DIFF
static inline long long la_get_tval(CPULoongArchState *env){
if (determined) {
return current_env->icount / TIME_SCALE + current_env->CSR_CNTC;
} else {
return nano_second() / TIMER_PERIOD + current_env->CSR_CNTC;
}
}
#endif
#ifndef CONFIG_USER_ONLY
#define CHECK_FPE(bytes) \
do { \
if (bytes == 8) { \
if (!FIELD_EX32(env->cpucfg[2], CPUCFG2, FP)) {return false;}; \
if (!FIELD_EX64(env->CSR_EUEN, CSR_EUEN, FPE)) {do_raise_exception(env, EXCCODE_FPD, 0); return true;} \
PERF_INC(COUNTER_INST_FP); \
} else if (bytes == 16) { \
if (!FIELD_EX32(env->cpucfg[2], CPUCFG2, LSX)) {return false;}; \
if (!FIELD_EX64(env->CSR_EUEN, CSR_EUEN, SXE)) {do_raise_exception(env, EXCCODE_SXD, 0); return true;} \
PERF_INC(COUNTER_INST_LSX); \
} else if (bytes == 32) { \
if (!FIELD_EX32(env->cpucfg[2], CPUCFG2, LASX)) {return false;}; \
if (!FIELD_EX64(env->CSR_EUEN, CSR_EUEN, ASXE)) {do_raise_exception(env, EXCCODE_ASXD, 0); return true;} \
PERF_INC(COUNTER_INST_LASX); \
} else {lsassert(0);}; \
} while (0)
#define CHECK_PLV(plv) \
do { \
if (FIELD_EX64(env->CSR_CRMD, CSR_CRMD, PLV) != plv) {do_raise_exception(env, EXCCODE_IPE, 0); return true;} \
} while (0)
#else
#define CHECK_FPE(bytes) \
do { \
if (bytes == 8) { \
PERF_INC(COUNTER_INST_FP); \
} else if (bytes == 16) { \
PERF_INC(COUNTER_INST_LSX); \
} else if (bytes == 32) { \
PERF_INC(COUNTER_INST_LASX); \
} else {lsassert(0);}; \
} while (0)
#endif
#define CHECK_LBT_X86 do { if (!FIELD_EX32(env->cpucfg[2], CPUCFG2, LBT_X86)) {return false;};} while (0)
#define CHECK_LBT_ARM do { if (!FIELD_EX32(env->cpucfg[2], CPUCFG2, LBT_ARM)) {return false;};} while (0)
#define CHECK_LBT_MIPS do { if (!FIELD_EX32(env->cpucfg[2], CPUCFG2, LBT_MIPS)) {return false;};} while (0)
#define CHECK_HPTW do { if (!FIELD_EX32(env->cpucfg[2], CPUCFG2, HPTW)) {return false;};} while (0)
#define CHECK_FRECIPE do { if (!FIELD_EX32(env->cpucfg[2], CPUCFG2, FRECIPE)) {return false;};} while (0)
#define CHECK_DIV32 do { if (!FIELD_EX32(env->cpucfg[2], CPUCFG2, DIV32)) {return false;};} while (0)
#define CHECK_LAM_BH do { if (!FIELD_EX32(env->cpucfg[2], CPUCFG2, LAM_BH)) {return false;};} while (0)
#define CHECK_LAMCAS do { if (!FIELD_EX32(env->cpucfg[2], CPUCFG2, LAMCAS)) {return false;};} while (0)
#define CHECK_LLACQ_SCREL do { if (!FIELD_EX32(env->cpucfg[2], CPUCFG2, LLACQ_SCREL)) {return false;};} while (0)
#define CHECK_SCQ do { if (!FIELD_EX32(env->cpucfg[2], CPUCFG2, SCQ)) {return false;};} while (0)
#ifdef CONFIG_DIFF
#define __NOT_IMPLEMENTED__ __NOT_IMPLEMENTED_EXIT__
#else
#define __NOT_IMPLEMENTED__ do {fprintf(stderr, "LA_EMU NOT IMPLEMENTED %s, pc:%lx\n", __func__, env->pc); env->pc += 4; return false;} while(0);
#endif
#define __NOT_CORRECTED_IMPLEMENTED__ do {fprintf(stderr, "LA_EMU NOT CORRECTED IMPLEMENTED %s, pc:%lx\n", __func__, env->pc);} while(0);
#define __NOT_IMPLEMENTED_EXIT__ do {fprintf(stderr, "LA_EMU NOT IMPLEMENTED %s, pc:%lx\n", __func__, env->pc); laemu_exit(1); return false;} while(0);
#define DisasContext CPULoongArchState
#define ctx env
#define TCGv int64_t
static inline int plus_1(DisasContext *ctx, int x)
{
return x + 1;
}
static inline int shl_1(DisasContext *ctx, int x)
{
return x << 1;
}
static inline int shl_2(DisasContext *ctx, int x)
{
return x << 2;
}
static inline int shl_3(DisasContext *ctx, int x)
{
return x << 3;
}
#include "trans_la.c.inc"
/*
* If an operation is being performed on less than TARGET_LONG_BITS,
* it may require the inputs to be sign- or zero-extended; which will
* depend on the exact operation being performed.
*/
typedef enum {
EXT_NONE,
EXT_SIGN,
EXT_ZERO,
} DisasExtend;
__attribute__((unused)) static inline int64_t gpr_src(CPULoongArchState *env, int reg_num, DisasExtend src_ext)
{
if (reg_num == 0) {
return 0;
}
switch (src_ext) {
case EXT_NONE:
return env->gpr[reg_num];
case EXT_SIGN:
return (int64_t)(int32_t)env->gpr[reg_num];
case EXT_ZERO:
return (uint64_t)(uint32_t)env->gpr[reg_num];
}
g_assert_not_reached();
}
static inline void gen_set_gpr(CPULoongArchState *env, int reg_num, int64_t t, DisasExtend dst_ext)
{
if (reg_num != 0) {
switch (dst_ext) {
case EXT_NONE:
env->gpr[reg_num] = t;
break;
case EXT_SIGN:
env->gpr[reg_num] = (int64_t)(int32_t)t;
break;
case EXT_ZERO:
env->gpr[reg_num] = (uint64_t)(uint32_t)t;
break;
default:
g_assert_not_reached();
}
}
}
static inline int64_t get_fpr(CPULoongArchState *env, int reg_num) {
return env->fpr[reg_num].vreg.D[0];
}
static inline void set_fpr(CPULoongArchState *env, int reg_num, int64_t val) {
env->fpr[reg_num].vreg.D[0] = val;
}
/* bit0(signaling/quiet) bit1(lt) bit2(eq) bit3(un) bit4(neq) */
static uint32_t get_fcmp_flags(int cond)
{
uint32_t flags = 0;
if (cond & 0x1) {
flags |= FCMP_LT;
}
if (cond & 0x2) {
flags |= FCMP_EQ;
}
if (cond & 0x4) {
flags |= FCMP_UN;
}
if (cond & 0x8) {
flags |= FCMP_GT | FCMP_LT;
}
return flags;
}
static bool trans_add_w(CPULoongArchState *env, arg_add_w *restrict a) {
env->gpr[a->rd] = (int64_t)(int32_t)(env->gpr[a->rj] + env->gpr[a->rk]);
env->pc += 4;
return true;
}
static bool trans_add_d(CPULoongArchState *env, arg_add_d *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] + env->gpr[a->rk];
env->pc += 4;
return true;
}
static bool trans_sub_w(CPULoongArchState *env, arg_sub_w *restrict a) {
env->gpr[a->rd] = (int64_t)(int32_t)(env->gpr[a->rj] - env->gpr[a->rk]);
env->pc += 4;
return true;
}
static bool trans_sub_d(CPULoongArchState *env, arg_sub_d *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] - env->gpr[a->rk];
env->pc += 4;
return true;
}
static bool trans_slt(CPULoongArchState *env, arg_slt *restrict a) {
env->gpr[a->rd] = (int64_t)env->gpr[a->rj] < (int64_t)env->gpr[a->rk];
env->pc += 4;
return true;
}
static bool trans_sltu(CPULoongArchState *env, arg_sltu *restrict a) {
env->gpr[a->rd] = (uint64_t)env->gpr[a->rj] < (uint64_t)env->gpr[a->rk];
env->pc += 4;
return true;
}
static bool trans_slti(CPULoongArchState *env, arg_slti *restrict a) {
env->gpr[a->rd] = (int64_t)env->gpr[a->rj] < (int64_t)a->imm;
env->pc += 4;
return true;
}
static bool trans_sltui(CPULoongArchState *env, arg_sltui *restrict a) {
env->gpr[a->rd] = (uint64_t)env->gpr[a->rj] < (uint64_t)(int64_t)a->imm;
env->pc += 4;
return true;
}
static bool trans_nor(CPULoongArchState *env, arg_nor *restrict a) {
env->gpr[a->rd] = ~(env->gpr[a->rj] | env->gpr[a->rk]);
env->pc += 4;
return true;
}
static bool trans_and(CPULoongArchState *env, arg_and *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] & env->gpr[a->rk];
env->pc += 4;
return true;
}
static bool trans_or(CPULoongArchState *env, arg_or *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] | env->gpr[a->rk];
env->pc += 4;
return true;
}
static bool trans_xor(CPULoongArchState *env, arg_xor *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] ^ env->gpr[a->rk];
env->pc += 4;
return true;
}
static bool trans_orn(CPULoongArchState *env, arg_orn *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] | (~ env->gpr[a->rk]);
env->pc += 4;
return true;
}
static bool trans_andn(CPULoongArchState *env, arg_andn *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] & (~ env->gpr[a->rk]);
env->pc += 4;
return true;
}
static bool trans_mul_w(CPULoongArchState *env, arg_mul_w *restrict a) {
env->gpr[a->rd] = (int64_t)((int32_t)env->gpr[a->rj] * (int32_t)env->gpr[a->rk]);
env->pc += 4;
return true;
}
static bool trans_mulh_w(CPULoongArchState *env, arg_mulh_w *restrict a) {
int64_t data = ((int64_t)(int32_t)env->gpr[a->rj] * (int64_t)(int32_t)env->gpr[a->rk]) >> 32;
env->gpr[a->rd] = data;
env->pc += 4;
return true;
}
static bool trans_mulh_wu(CPULoongArchState *env, arg_mulh_wu *restrict a) {
int64_t data = ((int64_t)((uint64_t)(uint32_t)env->gpr[a->rj] * (uint64_t)(uint32_t)env->gpr[a->rk])) >> 32;
env->gpr[a->rd] = data;
env->pc += 4;
return true;
}
static bool trans_mul_d(CPULoongArchState *env, arg_mul_d *restrict a) {
env->gpr[a->rd] = (int64_t)env->gpr[a->rj] * (int64_t)env->gpr[a->rk];
env->pc += 4;
return true;
}
static bool trans_mulh_d(CPULoongArchState *env, arg_mulh_d *restrict a) {
uint64_t high,low;
muls64(&low, &high, env->gpr[a->rj], env->gpr[a->rk]);
env->gpr[a->rd] = high;
env->pc += 4;
return true;
}
static bool trans_mulh_du(CPULoongArchState *env, arg_mulh_du *restrict a) {
uint64_t high,low;
mulu64(&low, &high, env->gpr[a->rj], env->gpr[a->rk]);
env->gpr[a->rd] = high;
env->pc += 4;
return true;
}
static bool trans_mulw_d_w(CPULoongArchState *env, arg_mulw_d_w *restrict a) {
env->gpr[a->rd] = (int64_t)(int32_t)env->gpr[a->rj] * (int64_t)(int32_t)env->gpr[a->rk];
env->pc += 4;
return true;
}
static bool trans_mulw_d_wu(CPULoongArchState *env, arg_mulw_d_wu *restrict a) {
env->gpr[a->rd] = (uint64_t)(uint32_t)env->gpr[a->rj] * (uint64_t)(uint32_t)env->gpr[a->rk];
env->pc += 4;
return true;
}
static bool trans_div_w(CPULoongArchState *env, arg_div_w *restrict a) {
env->gpr[a->rd] = (int32_t)env->gpr[a->rj] / (int32_t)env->gpr[a->rk];
env->pc += 4;
return true;
}
static bool trans_mod_w(CPULoongArchState *env, arg_mod_w *restrict a) {
env->gpr[a->rd] = (int32_t)env->gpr[a->rj] % (int32_t)env->gpr[a->rk];
env->pc += 4;
return true;
}
static bool trans_div_wu(CPULoongArchState *env, arg_div_wu *restrict a) {
env->gpr[a->rd] = (int32_t)((uint32_t)env->gpr[a->rj] / (uint32_t)env->gpr[a->rk]);
env->pc += 4;
return true;
}
static bool trans_mod_wu(CPULoongArchState *env, arg_mod_wu *restrict a) {
env->gpr[a->rd] = (uint32_t)env->gpr[a->rj] % (uint32_t)env->gpr[a->rk];
env->pc += 4;
return true;
}
static bool trans_div_d(CPULoongArchState *env, arg_div_d *restrict a) {
env->gpr[a->rd] = (int64_t)env->gpr[a->rj] / (int64_t)env->gpr[a->rk];
env->pc += 4;
return true;
}
static bool trans_mod_d(CPULoongArchState *env, arg_mod_d *restrict a) {
env->gpr[a->rd] = (int64_t)env->gpr[a->rj] % (int64_t)env->gpr[a->rk];
env->pc += 4;
return true;
}
static bool trans_div_du(CPULoongArchState *env, arg_div_du *restrict a) {
env->gpr[a->rd] = (uint64_t)env->gpr[a->rj] / (uint64_t)env->gpr[a->rk];
env->pc += 4;
return true;
}
static bool trans_mod_du(CPULoongArchState *env, arg_mod_du *restrict a) {
env->gpr[a->rd] = (uint64_t)env->gpr[a->rj] % (uint64_t)env->gpr[a->rk];
env->pc += 4;
return true;
}
static bool trans_alsl_w(CPULoongArchState *env, arg_alsl_w *restrict a) {
env->gpr[a->rd] = (int64_t)(int32_t)((env->gpr[a->rj] << a->sa) + env->gpr[a->rk]);
env->pc += 4;
return true;
}
static bool trans_alsl_wu(CPULoongArchState *env, arg_alsl_wu *restrict a) {
env->gpr[a->rd] = (uint32_t)((env->gpr[a->rj] << a->sa) + env->gpr[a->rk]);
env->pc += 4;
return true;
}
static bool trans_alsl_d(CPULoongArchState *env, arg_alsl_d *restrict a) {
env->gpr[a->rd] = (env->gpr[a->rj] << a->sa) + env->gpr[a->rk];
env->pc += 4;
return true;
}
static bool trans_lu12i_w(CPULoongArchState *env, arg_lu12i_w *restrict a) {
env->gpr[a->rd] = (int64_t)(a->imm << 12);
env->pc += 4;
return true;
}
static bool trans_lu32i_d(CPULoongArchState *env, arg_lu32i_d *restrict a) {
env->gpr[a->rd] = (uint64_t)(uint32_t)env->gpr[a->rd] | ((int64_t)a->imm << 32);
env->pc += 4;
return true;
}
static bool trans_lu52i_d(CPULoongArchState *env, arg_lu52i_d *restrict a) {
env->gpr[a->rd] = deposit64(env->gpr[a->rj], 52, 12, a->imm);
env->pc += 4;
return true;
}
static bool trans_pcaddi(CPULoongArchState *env, arg_pcaddi *restrict a) {
env->gpr[a->rd] = env->pc + (a->imm << 2);
env->pc += 4;
return true;
}
static bool trans_pcalau12i(CPULoongArchState *env, arg_pcalau12i *restrict a) {
env->gpr[a->rd] = env->pc + (a->imm << 12);
env->gpr[a->rd] &= ~0xfffull;
env->pc += 4;
return true;
}
static bool trans_pcaddu12i(CPULoongArchState *env, arg_pcaddu12i *restrict a) {
env->gpr[a->rd] = env->pc + (a->imm << 12);
env->pc += 4;
return true;
}
static bool trans_pcaddu18i(CPULoongArchState *env, arg_pcaddu18i *restrict a) {
env->gpr[a->rd] = env->pc + (a->imm << 18);
env->pc += 4;
return true;
}
static bool trans_addi_w(CPULoongArchState *env, arg_addi_w *restrict a) {
env->gpr[a->rd] = (int64_t)(int32_t)(env->gpr[a->rj] + a->imm);
env->pc += 4;
return true;
}
static bool trans_addi_d(CPULoongArchState *env, arg_addi_d *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] + a->imm;
env->pc += 4;
return true;
}
static bool trans_addu16i_d(CPULoongArchState *env, arg_addu16i_d *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] + (a->imm << 16);
env->pc += 4;
return true;
}
static bool trans_andi(CPULoongArchState *env, arg_andi *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] & a->imm;
env->pc += 4;
return true;
}
static bool trans_ori(CPULoongArchState *env, arg_ori *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] | a->imm;
env->pc += 4;
return true;
}
static bool trans_xori(CPULoongArchState *env, arg_xori *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] ^ a->imm;
env->pc += 4;
return true;
}
static bool trans_sll_w(CPULoongArchState *env, arg_sll_w *restrict a) {
env->gpr[a->rd] = (int64_t)((int32_t)env->gpr[a->rj] << (env->gpr[a->rk] & 0x1f));
env->pc += 4;
return true;
}
static bool trans_srl_w(CPULoongArchState *env, arg_srl_w *restrict a) {
env->gpr[a->rd] = (int64_t)(int32_t)((uint32_t)env->gpr[a->rj] >> (env->gpr[a->rk] & 0x1f));
env->pc += 4;
return true;
}
static bool trans_sra_w(CPULoongArchState *env, arg_sra_w *restrict a) {
env->gpr[a->rd] = (int64_t)((int32_t)env->gpr[a->rj] >> (env->gpr[a->rk] & 0x1f));
env->pc += 4;
return true;
}
static bool trans_sll_d(CPULoongArchState *env, arg_sll_d *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] << (env->gpr[a->rk] & 0x3f);
env->pc += 4;
return true;
}
static bool trans_srl_d(CPULoongArchState *env, arg_srl_d *restrict a) {
env->gpr[a->rd] = (uint64_t)env->gpr[a->rj] >> (env->gpr[a->rk] & 0x3f);
env->pc += 4;
return true;
}
static bool trans_sra_d(CPULoongArchState *env, arg_sra_d *restrict a) {
env->gpr[a->rd] = (int64_t)env->gpr[a->rj] >> (env->gpr[a->rk] & 0x3f);
env->pc += 4;
return true;
}
static bool trans_rotr_w(CPULoongArchState *env, arg_rotr_w *restrict a) {
uint32_t rj = env->gpr[a->rj];
int imm = env->gpr[a->rk] & 0x1f;
env->gpr[a->rd] = (int64_t)(int32_t)((rj >> imm) | (rj << (32 - imm)));
env->pc += 4;
return true;
}
static bool trans_rotr_d(CPULoongArchState *env, arg_rotr_d *restrict a) {
uint64_t rj = env->gpr[a->rj];
int imm = env->gpr[a->rk] & 0x3f;
env->gpr[a->rd] = (rj >> imm) | (rj << (64 - imm));
env->pc += 4;
return true;
}
static bool trans_slli_w(CPULoongArchState *env, arg_slli_w *restrict a) {
env->gpr[a->rd] = (int64_t)((int32_t)env->gpr[a->rj] << a->imm);
env->pc += 4;
return true;
}
static bool trans_slli_d(CPULoongArchState *env, arg_slli_d *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] << a->imm;
env->pc += 4;
return true;
}
static bool trans_srli_w(CPULoongArchState *env, arg_srli_w *restrict a) {
env->gpr[a->rd] = (int64_t)((uint32_t)env->gpr[a->rj] >> a->imm);
env->pc += 4;
return true;
}
static bool trans_srli_d(CPULoongArchState *env, arg_srli_d *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] >> a->imm;
env->pc += 4;
return true;
}
static bool trans_srai_w(CPULoongArchState *env, arg_srai_w *restrict a) {
env->gpr[a->rd] = (int64_t)((int32_t)env->gpr[a->rj] >> a->imm);
env->pc += 4;
return true;
}
static bool trans_srai_d(CPULoongArchState *env, arg_srai_d *restrict a) {
env->gpr[a->rd] = (int64_t)env->gpr[a->rj] >> a->imm;
env->pc += 4;
return true;
}
static bool trans_rotri_w(CPULoongArchState *env, arg_rotri_w *restrict a) {
uint32_t rj = env->gpr[a->rj];
int imm = a->imm & 0x1f;
env->gpr[a->rd] = (int64_t)(int32_t)((rj >> imm) | (rj << (32 - imm)));
env->pc += 4;
return true;
}
static bool trans_rotri_d(CPULoongArchState *env, arg_rotri_d *restrict a) {
uint64_t rj = env->gpr[a->rj];
int imm = a->imm & 0x3f;
env->gpr[a->rd] = (rj >> imm) | (rj << (64 - imm));
env->pc += 4;
return true;
}
static bool trans_ext_w_h(CPULoongArchState *env, arg_ext_w_h *restrict a) {
env->gpr[a->rd] = (int64_t)(int16_t)env->gpr[a->rj];
env->pc += 4;
return true;
}
static bool trans_ext_w_b(CPULoongArchState *env, arg_ext_w_b *restrict a) {
env->gpr[a->rd] = (int64_t)(int8_t)env->gpr[a->rj];
env->pc += 4;
return true;
}
static bool trans_clo_w(CPULoongArchState *env, arg_clo_w *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] ? clo32(env->gpr[a->rj]) : 0;
env->pc += 4;
return true;
}
static bool trans_clz_w(CPULoongArchState *env, arg_clz_w *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] ? clz32(env->gpr[a->rj]) : 32;
env->pc += 4;
return true;
}
static bool trans_cto_w(CPULoongArchState *env, arg_cto_w *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] ? cto32(env->gpr[a->rj]) : 0;
env->pc += 4;
return true;
}
static bool trans_ctz_w(CPULoongArchState *env, arg_ctz_w *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] ? ctz32(env->gpr[a->rj]) : 32;
env->pc += 4;
return true;
}
static bool trans_clo_d(CPULoongArchState *env, arg_clo_d *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] ? clo64(env->gpr[a->rj]) : 0;
env->pc += 4;
return true;
}
static bool trans_clz_d(CPULoongArchState *env, arg_clz_d *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] ? clz64(env->gpr[a->rj]) : 64;
env->pc += 4;
return true;
}
static bool trans_cto_d(CPULoongArchState *env, arg_cto_d *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] ? cto64(env->gpr[a->rj]) : 0;
env->pc += 4;
return true;
}
static bool trans_ctz_d(CPULoongArchState *env, arg_ctz_d *restrict a) {
env->gpr[a->rd] = env->gpr[a->rj] ? ctz64(env->gpr[a->rj]) : 64;
env->pc += 4;
return true;
}
static bool trans_revb_2h(CPULoongArchState *env, arg_revb_2h *restrict a) {
uint32_t mask = 0x00FF00FF;
uint32_t rj = env->gpr[a->rj];
env->gpr[a->rd] = (int64_t)(int32_t)(((rj >> 8) & mask) | ((rj & mask ) << 8));
env->pc += 4;
return true;
}
static bool trans_revb_4h(CPULoongArchState *env, arg_revb_4h *restrict a) {
uint64_t mask = 0x00FF00FF00FF00FFULL;
uint64_t rj = env->gpr[a->rj];
env->gpr[a->rd] = ((rj >> 8) & mask) | ((rj & mask ) << 8);
env->pc += 4;
return true;
}
static bool trans_revb_2w(CPULoongArchState *env, arg_revb_2w *restrict a) {
uint64_t rj = env->gpr[a->rj];
env->gpr[a->rd] =
((rj & 0xFF00000000000000u) >> 24u) |
((rj & 0x00FF000000000000u) >> 8u) |
((rj & 0x0000FF0000000000u) << 8u) |
((rj & 0x000000FF00000000u) << 24u) |
((rj & 0x00000000FF000000u) >> 24u) |
((rj & 0x0000000000FF0000u) >> 8u) |
((rj & 0x000000000000FF00u) << 8u) |
((rj & 0x00000000000000FFu) << 24u);
env->pc += 4;
return true;
}
static bool trans_revb_d(CPULoongArchState *env, arg_revb_d *restrict a) {
uint64_t rj = env->gpr[a->rj];
env->gpr[a->rd] =
((rj & 0xFF00000000000000u) >> 56u) |
((rj & 0x00FF000000000000u) >> 40u) |
((rj & 0x0000FF0000000000u) >> 24u) |
((rj & 0x000000FF00000000u) >> 8u) |
((rj & 0x00000000FF000000u) << 8u) |
((rj & 0x0000000000FF0000u) << 24u) |
((rj & 0x000000000000FF00u) << 40u) |
((rj & 0x00000000000000FFu) << 56u);
env->pc += 4;
return true;
}
static bool trans_revh_2w(CPULoongArchState *env, arg_revh_2w *restrict a) {
uint64_t rj = env->gpr[a->rj];
env->gpr[a->rd] =
((rj & 0xFFFF000000000000u) >> 16u) |
((rj & 0x0000FFFF00000000u) << 16u) |
((rj & 0x00000000FFFF0000u) >> 16u) |
((rj & 0x000000000000FFFFu) << 16u);
env->pc += 4;
return true;
}
static bool trans_revh_d(CPULoongArchState *env, arg_revh_d *restrict a) {
uint64_t mask = 0x0000FFFF0000FFFFULL;
uint64_t rj = env->gpr[a->rj];
uint64_t t = ((rj >> 16) & mask) | ((rj & mask ) << 16);
env->gpr[a->rd] = (t >> 32) | (t << 32);
env->pc += 4;
return true;
}
target_ulong helper_bitswap(target_ulong v)
{
v = ((v >> 1) & (target_ulong)0x5555555555555555ULL) |
((v & (target_ulong)0x5555555555555555ULL) << 1);
v = ((v >> 2) & (target_ulong)0x3333333333333333ULL) |
((v & (target_ulong)0x3333333333333333ULL) << 2);
v = ((v >> 4) & (target_ulong)0x0F0F0F0F0F0F0F0FULL) |
((v & (target_ulong)0x0F0F0F0F0F0F0F0FULL) << 4);
return v;
}
static bool trans_bitrev_4b(CPULoongArchState *env, arg_bitrev_4b *restrict a) {
gen_set_gpr(env, a->rd, helper_bitswap(env->gpr[a->rj]), EXT_SIGN);
env->pc += 4;
return true;
}
static bool trans_bitrev_8b(CPULoongArchState *env, arg_bitrev_8b *restrict a) {
gen_set_gpr(env, a->rd, helper_bitswap(env->gpr[a->rj]), EXT_NONE);
env->pc += 4;
return true;
}
static bool trans_bitrev_w(CPULoongArchState *env, arg_bitrev_w *restrict a) {
gen_set_gpr(env, a->rd, revbit32(env->gpr[a->rj]), EXT_SIGN);
env->pc += 4;
return true;
}
static bool trans_bitrev_d(CPULoongArchState *env, arg_bitrev_d *restrict a) {
gen_set_gpr(env, a->rd, revbit64(env->gpr[a->rj]), EXT_NONE);
env->pc += 4;
return true;
}
static bool trans_bytepick_w(CPULoongArchState *env, arg_bytepick_w *restrict a) {
uint64_t t = (env->gpr[a->rk] << 32) | (uint32_t)env->gpr[a->rj];
env->gpr[a->rd] = (int64_t)(int32_t)(t >> (32 - a->sa * 8));
env->pc += 4;
return true;
}
static bool trans_bytepick_d(CPULoongArchState *env, arg_bytepick_d *restrict a) {
uint64_t high = env->gpr[a->rk] << (a->sa * 8);
uint64_t low = env->gpr[a->rj] >> (64 - a->sa * 8);
env->gpr[a->rd] = high | low;
env->pc += 4;
return true;
}
static bool trans_maskeqz(CPULoongArchState *env, arg_maskeqz *restrict a) {
env->gpr[a->rd] = env->gpr[a->rk] == 0 ? 0 : env->gpr[a->rj];
env->pc += 4;
return true;
}
static bool trans_masknez(CPULoongArchState *env, arg_masknez *restrict a) {
env->gpr[a->rd] = env->gpr[a->rk] != 0 ? 0 : env->gpr[a->rj];
env->pc += 4;
return true;
}
static bool trans_bstrins_w(CPULoongArchState *env, arg_bstrins_w *restrict a) {
env->gpr[a->rd] = (int64_t)(int32_t)deposit32(env->gpr[a->rd], a->ls, a->ms - a->ls + 1, env->gpr[a->rj]);
env->pc += 4;
return true;
}
static bool trans_bstrpick_w(CPULoongArchState *env, arg_bstrpick_w *restrict a) {
if (a->ls > a->ms) {
return false;
}
env->gpr[a->rd] = (int64_t)extract32(env->gpr[a->rj], a->ls, a->ms - a->ls + 1);
env->pc += 4;
return true;
}
static bool trans_bstrins_d(CPULoongArchState *env, arg_bstrins_d *restrict a) {
env->gpr[a->rd] = deposit64(env->gpr[a->rd], a->ls, a->ms - a->ls + 1, env->gpr[a->rj]);
env->pc += 4;
return true;
}
static bool trans_bstrpick_d(CPULoongArchState *env, arg_bstrpick_d *restrict a) {
if (a->ls > a->ms) {
return false;
}
env->gpr[a->rd] = extract64(env->gpr[a->rj], a->ls, a->ms - a->ls + 1);
env->pc += 4;
return true;
}
bool is_one_page(uint64_t addr, int bytes) {
target_ulong pgsz = 0x4000;
target_ulong pgmsk = pgsz - 1;
return (addr & ~pgmsk) == ((addr + bytes - 1) & ~pgmsk);
}
bool is_two_page(uint64_t addr, int bytes) {
return !is_one_page(addr, bytes);
}
bool is_aligned(uint64_t addr, int bytes) {
#ifdef CONFIG_USER_ONLY
return true;
#endif
return is_one_page(addr, bytes);
// return !(addr & (bytes - 1));
}
bool is_unaligned(uint64_t addr, int bytes) {
return !is_aligned(addr, bytes);
}
static hwaddr load_pa(CPULoongArchState *env, uint64_t addr) {
PERF_INC(COUNTER_INST_LOAD);
#ifdef CONFIG_USER_ONLY
return addr;
#endif
hwaddr ha;
int prot;
int tc_index = TC_INDEX(addr);
TLBCache* tc = env->tc_load + tc_index;
uint64_t page_addr = addr & TARGET_PAGE_MASK;
if (likely(page_addr == tc->va)) {
uint64_t ha = (addr & (TARGET_PAGE_SIZE - 1)) | tc->pa;
// fprintf(stderr, "%lx %lx\n", addr, ha);
return ha;
}
int mmu_idx = FIELD_EX64(env->CSR_CRMD, CSR_CRMD, PLV) == 0 ? MMU_KERNEL_IDX : MMU_USER_IDX;
check_get_physical_address(env, &ha, &prot, addr, MMU_DATA_LOAD, mmu_idx);
tc->va = page_addr;
tc->pa = ha & TARGET_PAGE_MASK;
return ha;
}
static hwaddr store_pa(CPULoongArchState *env, uint64_t addr) {
PERF_INC(COUNTER_INST_STORE);
#ifdef CONFIG_USER_ONLY
return addr;
#endif
hwaddr ha;
int prot;
int tc_index = TC_INDEX(addr);
TLBCache* tc = env->tc_store + tc_index;
uint64_t page_addr = addr & TARGET_PAGE_MASK;
if (likely(page_addr == tc->va)) {
ha = (addr & (TARGET_PAGE_SIZE - 1)) | tc->pa;
// fprintf(stderr, "%lx %lx\n", addr, ha);
} else {
int mmu_idx = FIELD_EX64(env->CSR_CRMD, CSR_CRMD, PLV) == 0 ? MMU_KERNEL_IDX : MMU_USER_IDX;
check_get_physical_address(env, &ha, &prot, addr, MMU_DATA_STORE, mmu_idx);
tc->va = page_addr;
tc->pa = ha & TARGET_PAGE_MASK;
}
return ha;
}
#if defined(CONFIG_USER_ONLY) || defined(CONFIG_DIFF)
#define is_io(...) false
#else
// exclude 32MB bios
static bool is_io(hwaddr ha) {
return (ha >= 0x10000000 && ha < 0x1c000000)
|| (ha > 0x1e000000 && ha < 0x90000000);
}
#endif
static uint64_t add_addr(int64_t base, int64_t disp) {
return (uint64_t)(base + disp);
}
static int8_t ld_b(CPULoongArchState *env, uint64_t va) {
hwaddr ha = load_pa(env, va);
#if defined(CONFIG_USER_ONLY)
return ram_ldb(ha);
#else
return is_io(ha) ? do_io_ld(ha, 1) : ram_ldb(ha);
#endif
}
static int16_t ld_h(CPULoongArchState *env, uint64_t va) {
uint64_t data;
const int data_size = 2;
hwaddr ha = load_pa(env, va);
if (is_io(ha)) {
#if !defined(CONFIG_USER_ONLY)
data = do_io_ld(ha, data_size);
#endif
} else {
if (is_aligned(va, data_size)) {
data = ram_ldh(ha);
} else {
PERF_INC(COUNTER_INST_CROSS_PAGE_LOAD);
data = 0;
for (int i = (data_size - 1); i >= 0; i--){
data |= (((uint16_t)ld_b(env, va + i) & 0xff) << (i * 8)) ;
}
}
}
return data;
}
static int32_t ld_w(CPULoongArchState *env, uint64_t va) {
uint64_t data;
const int data_size = 4;
hwaddr ha = load_pa(env, va);
if (is_io(ha)) {
#if !defined(CONFIG_USER_ONLY)
data = do_io_ld(ha, data_size);
#endif
} else {
if (is_aligned(va, data_size)) {
data = ram_ldw(ha);
} else {
PERF_INC(COUNTER_INST_CROSS_PAGE_LOAD);
data = 0;
for (int i = (data_size - 1); i >= 0; i--){
data |= (((uint32_t)ld_b(env, va + i) & 0xff) << (i * 8)) ;
}
}
}
return data;
}
static int64_t ld_d(CPULoongArchState *env, uint64_t va) {
uint64_t data;
const int data_size = 8;
hwaddr ha = load_pa(env, va);
if (is_io(ha)) {
#if !defined(CONFIG_USER_ONLY)
data = do_io_ld(ha, data_size);
#endif
} else {
if (is_aligned(va, data_size)) {
data = ram_ldd(ha);
} else {
PERF_INC(COUNTER_INST_CROSS_PAGE_LOAD);
data = 0;
for (int i = (data_size - 1); i >= 0; i--){
data |= (((uint64_t)ld_b(env, va + i) & 0xff) << (i * 8)) ;
}
}
}
return data;
}
// static Int128 ld_128(CPULoongArchState *env, uint64_t va) {
// Int128 data;
// const int data_size = 16;
// hwaddr ha = load_pa(env, va);
// if (is_io(ha)) {
// lsassert(0);
// } else {
// if (is_aligned(va, data_size)) {
// data = ram_ld128(ha);
// } else {
// data = 0;
// for (int i = (data_size - 1); i >= 0; i--){
// data |= (((Int128)ld_b(env, va + i) & 0xff) << (i * 8));
// }
// }
// }
// return data;
// }
// static VReg ld_256(CPULoongArchState *env, uint64_t va) {
// VReg data;
// const int data_size = 32;
// hwaddr ha = load_pa(env, va);
// if (is_io(ha)) {
// lsassert(0);
// } else {
// if (is_aligned(va, data_size)) {
// data = ram_ld256(ha);
// } else {
// for (int i = (data_size - 1); i >= 0; i--){
// data.B[i] = (ld_b(env, va + i) & 0xff) << (i * 8);
// }
// }
// }
// return data;
// }
static void st_b(CPULoongArchState *env, uint64_t va, uint8_t data) {
hwaddr ha = store_pa(env, va);
#if defined(CONFIG_USER_ONLY)
ram_stb(ha, data);
#else
is_io(ha) ? do_io_st(ha, data, 1) : ram_stb(ha, data);
#endif
}
static void st_h(CPULoongArchState *env, uint64_t va, uint16_t data) {
const int data_size = 2;
hwaddr ha = store_pa(env, va);
if (is_io(ha)) {
#if !defined(CONFIG_USER_ONLY)
do_io_st(ha, data, data_size);
#endif
} else {
if (is_aligned(va, data_size)) {
ram_sth(ha, data);
} else {
PERF_INC(COUNTER_INST_CROSS_PAGE_LOAD);
for (int i = (data_size - 1); i >= 0; i--){
st_b(env, va + i, (data >> (i * 8)) & 0xff);
}
}
}
}
static void st_w(CPULoongArchState *env, uint64_t va, uint32_t data) {
const int data_size = 4;
hwaddr ha = store_pa(env, va);
if (is_io(ha)) {
#if !defined(CONFIG_USER_ONLY)
do_io_st(ha, data, data_size);
#endif
} else {
if (is_aligned(va, data_size)) {
ram_stw(ha, data);
} else {
PERF_INC(COUNTER_INST_CROSS_PAGE_LOAD);
for (int i = (data_size - 1); i >= 0; i--){
st_b(env, va + i, (data >> (i * 8)) & 0xff);
}
}
}
}
static void st_d(CPULoongArchState *env, uint64_t va, uint64_t data) {
const int data_size = 8;
hwaddr ha = store_pa(env, va);
if (is_io(ha)) {
#if !defined(CONFIG_USER_ONLY)
do_io_st(ha, data, data_size);
#endif
} else {
if (is_aligned(va, data_size)) {
ram_std(ha, data);
} else {
PERF_INC(COUNTER_INST_CROSS_PAGE_LOAD);
for (int i = (data_size - 1); i >= 0; i--){
st_b(env, va + i, (data >> (i * 8)) & 0xff);
}
}
}
}
// static void st_128(CPULoongArchState *env, uint64_t va, Int128 data) {
// const int data_size = 16;
// hwaddr ha = store_pa(env, va);
// if (is_io(ha)) {
// lsassert(0);
// } else {
// if (is_aligned(va, data_size)) {
// ram_st128(ha, data);
// } else {
// for (int i = (data_size - 1); i >= 0; i--){
// st_b(env, va + i, (data >> (i * 8)) & 0xff);
// }
// }
// }
// }
// static void st_256(CPULoongArchState *env, uint64_t va, VReg data) {
// const int data_size = 32;
// hwaddr ha = store_pa(env, va);
// if (is_io(ha)) {
// lsassert(0);
// } else {
// if (is_aligned(va, data_size)) {
// ram_st256(ha, data);
// } else {
// for (int i = (data_size - 1); i >= 0; i--){
// st_b(env, va + i, data.B[i]);
// }
// }
// }
// }
static bool trans_ld_b(CPULoongArchState *env, arg_ld_b *restrict a) {
env->gpr[a->rd] = (int64_t)ld_b(env, add_addr(env->gpr[a->rj], a->imm));
env->pc += 4;
return true;
}
static bool trans_ld_h(CPULoongArchState *env, arg_ld_h *restrict a) {
env->gpr[a->rd] = (int64_t)ld_h(env, add_addr(env->gpr[a->rj], a->imm));
env->pc += 4;
return true;
}
static bool trans_ld_w(CPULoongArchState *env, arg_ld_w *restrict a) {
env->gpr[a->rd] = (int64_t)ld_w(env, add_addr(env->gpr[a->rj], a->imm));
env->pc += 4;
return true;
}
static bool trans_ld_d(CPULoongArchState *env, arg_ld_d *restrict a) {
env->gpr[a->rd] = (int64_t)ld_d(env, add_addr(env->gpr[a->rj], a->imm));
env->pc += 4;
return true;