forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emitloongarch64.cpp
5355 lines (4729 loc) · 167 KB
/
emitloongarch64.cpp
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX XX
XX emitloongarch64.cpp XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#if defined(TARGET_LOONGARCH64)
/*****************************************************************************/
/*****************************************************************************/
#include "instr.h"
#include "emit.h"
#include "codegen.h"
/*****************************************************************************/
const instruction emitJumpKindInstructions[] = {
INS_nop,
#define JMP_SMALL(en, rev, ins) INS_##ins,
#include "emitjmps.h"
};
const emitJumpKind emitReverseJumpKinds[] = {
EJ_NONE,
#define JMP_SMALL(en, rev, ins) EJ_##rev,
#include "emitjmps.h"
};
/*****************************************************************************
* Look up the instruction for a jump kind
*/
/*static*/ instruction emitter::emitJumpKindToIns(emitJumpKind jumpKind)
{
assert((unsigned)jumpKind < ArrLen(emitJumpKindInstructions));
return emitJumpKindInstructions[jumpKind];
}
/*****************************************************************************
* Look up the jump kind for an instruction. It better be a conditional
* branch instruction with a jump kind!
*/
/*static*/ emitJumpKind emitter::emitInsToJumpKind(instruction ins)
{
NYI_LOONGARCH64("emitInsToJumpKind-----unimplemented on LOONGARCH64 yet----");
return EJ_NONE;
}
/*****************************************************************************
* Reverse the conditional jump
*/
/*static*/ emitJumpKind emitter::emitReverseJumpKind(emitJumpKind jumpKind)
{
assert(jumpKind < EJ_COUNT);
return emitReverseJumpKinds[jumpKind];
}
/*****************************************************************************
*
* Return the allocated size (in bytes) of the given instruction descriptor.
*/
size_t emitter::emitSizeOfInsDsc(instrDesc* id) const
{
if (emitIsSmallInsDsc(id))
return SMALL_IDSC_SIZE;
insOpts insOp = id->idInsOpt();
switch (insOp)
{
case INS_OPTS_JIRL:
case INS_OPTS_J_cond:
case INS_OPTS_J:
return sizeof(instrDescJmp);
case INS_OPTS_C:
if (id->idIsLargeCall())
{
/* Must be a "fat" call descriptor */
return sizeof(instrDescCGCA);
}
else
{
assert(!id->idIsLargeDsp());
assert(!id->idIsLargeCns());
return sizeof(instrDesc);
}
case INS_OPTS_I:
case INS_OPTS_RC:
case INS_OPTS_RL:
case INS_OPTS_RELOC:
case INS_OPTS_NONE:
return sizeof(instrDesc);
default:
NO_WAY("unexpected instruction descriptor format");
break;
}
}
inline bool emitter::emitInsMayWriteToGCReg(instruction ins)
{
assert(ins != INS_invalid);
// NOTE: please reference the file "instrsloongarch64.h" for details !!!
return (INS_mov <= ins) && (ins <= INS_jirl) ? true : false;
}
bool emitter::emitInsWritesToLclVarStackLoc(instrDesc* id)
{
if (!id->idIsLclVar())
return false;
instruction ins = id->idIns();
// This list is related to the list of instructions used to store local vars in emitIns_S_R().
// We don't accept writing to float local vars.
switch (ins)
{
case INS_st_d:
case INS_st_w:
case INS_st_b:
case INS_st_h:
case INS_stptr_d:
case INS_stx_d:
case INS_stx_w:
case INS_stx_b:
case INS_stx_h:
return true;
default:
return false;
}
}
#define LD 1
#define ST 2
// clang-format off
/*static*/ const BYTE CodeGenInterface::instInfo[] =
{
#define INST(id, nm, info, e1, msk, fmt) info,
#include "instrs.h"
};
// clang-format on
//------------------------------------------------------------------------
// emitInsLoad: Returns true if the instruction is some kind of load instruction.
//
bool emitter::emitInsIsLoad(instruction ins)
{
// We have pseudo ins like lea which are not included in emitInsLdStTab.
if (ins < ArrLen(CodeGenInterface::instInfo))
return (CodeGenInterface::instInfo[ins] & LD) != 0;
else
return false;
}
//------------------------------------------------------------------------
// emitInsIsStore: Returns true if the instruction is some kind of store instruction.
//
bool emitter::emitInsIsStore(instruction ins)
{
// We have pseudo ins like lea which are not included in emitInsLdStTab.
if (ins < ArrLen(CodeGenInterface::instInfo))
return (CodeGenInterface::instInfo[ins] & ST) != 0;
else
return false;
}
//-------------------------------------------------------------------------
// emitInsIsLoadOrStore: Returns true if the instruction is some kind of load/store instruction.
//
bool emitter::emitInsIsLoadOrStore(instruction ins)
{
// We have pseudo ins like lea which are not included in emitInsLdStTab.
if (ins < ArrLen(CodeGenInterface::instInfo))
return (CodeGenInterface::instInfo[ins] & (LD | ST)) != 0;
else
return false;
}
#undef LD
#undef ST
/*****************************************************************************
*
* Returns the specific encoding of the given CPU instruction.
*/
inline emitter::code_t emitter::emitInsCode(instruction ins /*, insFormat fmt*/)
{
code_t code = BAD_CODE;
// clang-format off
const static code_t insCode[] =
{
#define INST(id, nm, info, e1, msk, fmt) e1,
#include "instrs.h"
};
// clang-format on
code = insCode[ins];
assert((code != BAD_CODE));
return code;
}
/****************************************************************************
*
* Add an instruction with no operands.
*/
void emitter::emitIns(instruction ins)
{
// instrDesc* id = emitNewInstrSmall(EA_8BYTE);
instrDesc* id = emitNewInstr(EA_8BYTE);
id->idIns(ins);
code_t code = emitInsCode(ins);
#if DEBUG
if (ins == INS_break)
{
code |= 0x5; // just for gdb catch.
}
#endif
id->idAddr()->iiaSetInstrEncode(code);
id->idCodeSize(4);
appendToCurIG(id);
}
/*****************************************************************************
* emitter::emitIns_S_R() and emitter::emitIns_R_S():
*
* Add an Load/Store instruction(s): base+offset and base-addr-computing if needed.
* For referencing a stack-based local variable and a register
*
* Special notes for LoongArch64:
* The parameter `offs` has special info.
*
* (1) The real value of `offs` is positive. `offs` = `offs`.
*
* (2) If the `offs` is negtive, `offs` = -(offs),
* the negtive `offs` is special for optimizing the large offset which >2047.
* when offs >2047 we can't encode one instruction to load/store the data,
* if there are several load/store at this case, you have to repeat the similar
* large offs with reduntant instructions and maybe eat up the `emitIGbuffSize`.
*
* Before optimizing the following instructions:
* lu12i.w x0, 0x0
* ori x0, x0, 0x9ac
* add.d x0, x0, fp
* fst.s fa0, x0, 0
*
* After optimized the instructions:
* For the offs within range [0,0x7ff], using one instruction:
* ori x0, x0, offs
* For the offs within range [0x1000,0xffffffff], using two instruction
* lu12i.w x0, offs-hi-20bits
* ori x0, x0, offs-low-12bits
*
* Then Store/Load the data:
* fstx.s fa0, x0, fp
*
* If storing/loading the second field of a struct,
* addi_d x0,x0,sizeof(type)
* fstx.s fa0, x0, fp
*
*/
void emitter::emitIns_S_R(instruction ins, emitAttr attr, regNumber reg1, int varx, int offs)
{
ssize_t imm;
emitAttr size = EA_SIZE(attr);
#ifdef DEBUG
switch (ins)
{
case INS_st_d:
case INS_stx_d:
case INS_st_w:
case INS_stx_w:
case INS_fst_s:
case INS_fst_d:
case INS_fstx_s:
case INS_fstx_d:
case INS_st_b:
case INS_st_h:
case INS_stx_b:
case INS_stx_h:
#ifdef FEATURE_SIMD
case INS_vst:
case INS_vstx:
case INS_xvst:
case INS_xvstx:
#endif
break;
default:
NYI("emitIns_S_R");
return;
} // end switch (ins)
#endif
/* Figure out the variable's frame position */
int base;
bool FPbased;
base = emitComp->lvaFrameAddress(varx, &FPbased);
imm = offs < 0 ? -offs - 8 : base + offs;
regNumber reg3 = FPbased ? REG_FPBASE : REG_SPBASE;
regNumber reg2 = offs < 0 ? REG_R21 : reg3;
offs = offs < 0 ? -offs - 8 : offs;
if ((-2048 <= imm) && (imm < 2048))
{
// regs[1] = reg2;
}
else
{
ssize_t imm3 = imm & 0x800;
ssize_t imm2 = imm + imm3;
assert(isValidSimm20(imm2 >> 12));
emitIns_R_I(INS_lu12i_w, EA_PTRSIZE, REG_RA, imm2 >> 12);
emitIns_R_R_R(INS_add_d, EA_PTRSIZE, REG_RA, REG_RA, reg2);
imm2 = imm2 & 0x7ff;
imm = imm3 ? imm2 - imm3 : imm2;
reg2 = REG_RA;
}
instrDesc* id = emitNewInstr(attr);
id->idReg1(reg1);
id->idReg2(reg2);
id->idIns(ins);
code_t code = emitInsCode(ins);
code |= (code_t)(reg1 & 0x1f);
code |= (code_t)reg2 << 5;
if ((ins == INS_stx_d) || (ins == INS_stx_w) || (ins == INS_stx_h) || (ins == INS_stx_b) ||
#ifdef FEATURE_SIMD
(ins == INS_vstx) || (ins == INS_xvstx) ||
#endif
(ins == INS_fstx_s) || (ins == INS_fstx_d))
{
code |= (code_t)reg3 << 10;
}
else
{
code |= (code_t)(imm & 0xfff) << 10;
}
id->idAddr()->iiaSetInstrEncode(code);
id->idAddr()->iiaLclVar.initLclVarAddr(varx, offs);
id->idSetIsLclVar();
id->idCodeSize(4);
appendToCurIG(id);
}
/*
* Special notes for `offs`, please see the comment for `emitter::emitIns_S_R`.
*/
void emitter::emitIns_R_S(instruction ins, emitAttr attr, regNumber reg1, int varx, int offs)
{
ssize_t imm;
emitAttr size = EA_SIZE(attr);
#ifdef DEBUG
switch (ins)
{
case INS_ld_b:
case INS_ld_bu:
case INS_ld_h:
case INS_ld_hu:
case INS_ld_w:
case INS_ld_wu:
case INS_fld_s:
case INS_ld_d:
case INS_fld_d:
#ifdef FEATURE_SIMD
case INS_vld:
case INS_xvld:
#endif
break;
case INS_lea:
assert(size == EA_8BYTE);
break;
default:
NYI("emitIns_R_S");
return;
} // end switch (ins)
#endif
/* Figure out the variable's frame position */
int base;
bool FPbased;
base = emitComp->lvaFrameAddress(varx, &FPbased);
imm = offs < 0 ? -offs - 8 : base + offs;
regNumber reg2 = FPbased ? REG_FPBASE : REG_SPBASE;
reg2 = offs < 0 ? REG_R21 : reg2;
offs = offs < 0 ? -offs - 8 : offs;
reg1 = (regNumber)((char)reg1 & 0x1f);
code_t code;
if ((-2048 <= imm) && (imm < 2048))
{
if (ins == INS_lea)
{
ins = INS_addi_d;
}
code = emitInsCode(ins);
code |= (code_t)(reg1 & 0x1f);
code |= (code_t)reg2 << 5;
code |= (imm & 0xfff) << 10;
}
else
{
if (ins == INS_lea)
{
assert(isValidSimm20(imm >> 12));
emitIns_R_I(INS_lu12i_w, EA_PTRSIZE, REG_RA, imm >> 12);
ssize_t imm2 = imm & 0xfff;
emitIns_R_R_I(INS_ori, EA_PTRSIZE, REG_RA, REG_RA, imm2);
ins = INS_add_d;
code = emitInsCode(ins);
code |= (code_t)reg1;
code |= (code_t)reg2 << 5;
code |= (code_t)REG_RA << 10;
}
else
{
ssize_t imm3 = imm & 0x800;
ssize_t imm2 = imm + imm3;
assert(isValidSimm20(imm2 >> 12));
emitIns_R_I(INS_lu12i_w, EA_PTRSIZE, REG_RA, imm2 >> 12);
emitIns_R_R_R(INS_add_d, EA_PTRSIZE, REG_RA, REG_RA, reg2);
imm2 = imm2 & 0x7ff;
imm3 = imm3 ? imm2 - imm3 : imm2;
code = emitInsCode(ins);
code |= (code_t)reg1;
code |= (code_t)REG_RA << 5;
code |= (code_t)(imm3 & 0xfff) << 10;
}
}
instrDesc* id = emitNewInstr(attr);
id->idReg1(reg1);
id->idIns(ins);
id->idAddr()->iiaSetInstrEncode(code);
id->idAddr()->iiaLclVar.initLclVarAddr(varx, offs);
id->idSetIsLclVar();
id->idCodeSize(4);
appendToCurIG(id);
}
/*****************************************************************************
*
* Add an instruction with a single immediate value.
*/
void emitter::emitIns_I(instruction ins, emitAttr attr, ssize_t imm)
{
code_t code = emitInsCode(ins);
switch (ins)
{
case INS_b:
case INS_bl:
assert(!(imm & 0x3));
code |= ((imm >> 18) & 0x3ff); // offs[25:16]
code |= ((imm >> 2) & 0xffff) << 10; // offs[15:0]
break;
case INS_dbar:
case INS_ibar:
assert((0 <= imm) && (imm <= 0x7fff));
code |= (imm & 0x7fff); // hint
break;
default:
unreached();
}
instrDesc* id = emitNewInstr(attr);
id->idIns(ins);
id->idAddr()->iiaSetInstrEncode(code);
id->idCodeSize(4);
appendToCurIG(id);
}
void emitter::emitIns_I_I(instruction ins, emitAttr attr, ssize_t cc, ssize_t offs)
{
#ifdef DEBUG
switch (ins)
{
case INS_bceqz:
case INS_bcnez:
break;
default:
unreached();
}
#endif
code_t code = emitInsCode(ins);
assert(!(offs & 0x3));
assert(!(cc >> 3));
code |= ((cc & 0x7) << 5); // cj
code |= ((offs >> 18) & 0x1f); // offs[20:16]
code |= ((offs >> 2) & 0xffff) << 10; // offs[15:0]
instrDesc* id = emitNewInstr(attr);
id->idIns(ins);
id->idAddr()->iiaSetInstrEncode(code);
id->idCodeSize(4);
appendToCurIG(id);
}
/*****************************************************************************
*
* Add an instruction referencing a register and a constant.
*/
void emitter::emitIns_R_I(instruction ins, emitAttr attr, regNumber reg, ssize_t imm, insOpts opt /* = INS_OPTS_NONE */)
{
code_t code = emitInsCode(ins);
switch (ins)
{
case INS_lu12i_w:
case INS_lu32i_d:
case INS_pcaddi:
case INS_pcalau12i:
case INS_pcaddu12i:
case INS_pcaddu18i:
assert(isGeneralRegister(reg));
assert((-524288 <= imm) && (imm < 524288));
code |= reg; // rd
code |= (imm & 0xfffff) << 5; // si20
break;
case INS_beqz:
case INS_bnez:
assert(isGeneralRegisterOrR0(reg));
assert(!(imm & 0x3));
assert((-1048576 <= (imm >> 2)) && ((imm >> 2) <= 1048575));
code |= ((imm >> 18) & 0x1f); // offs[20:16]
code |= reg << 5; // rj
code |= ((imm >> 2) & 0xffff) << 10; // offs[15:0]
break;
case INS_movfr2cf:
assert(isFloatReg(reg));
assert((0 <= imm) && (imm <= 7));
code |= (reg & 0x1f) << 5; // fj
code |= imm; // cc
break;
case INS_movcf2fr:
assert(isFloatReg(reg));
assert((0 <= imm) && (imm <= 7));
code |= (reg & 0x1f); // fd
code |= imm << 5; // cc
break;
case INS_movgr2cf:
assert(isGeneralRegister(reg));
assert((0 <= imm) && (imm <= 7));
code |= reg << 5; // rj
code |= imm; // cc
break;
case INS_movcf2gr:
assert(isGeneralRegister(reg));
assert((0 <= imm) && (imm <= 7));
code |= reg; // rd
code |= imm << 5; // cc
break;
#ifdef FEATURE_SIMD
case INS_vldi:
case INS_xvldi:
assert(isVectorRegister(reg));
assert((imm >> 13) == 0);
code |= (reg & 0x1f); // vd/xd
code |= imm << 5; // si13
break;
case INS_vseteqz_v:
case INS_vsetnez_v:
case INS_vsetanyeqz_b:
case INS_vsetanyeqz_h:
case INS_vsetanyeqz_w:
case INS_vsetanyeqz_d:
case INS_vsetallnez_b:
case INS_vsetallnez_h:
case INS_vsetallnez_w:
case INS_vsetallnez_d:
case INS_xvseteqz_v:
case INS_xvsetnez_v:
case INS_xvsetanyeqz_b:
case INS_xvsetanyeqz_h:
case INS_xvsetanyeqz_w:
case INS_xvsetanyeqz_d:
case INS_xvsetallnez_b:
case INS_xvsetallnez_h:
case INS_xvsetallnez_w:
case INS_xvsetallnez_d:
assert(isVectorRegister(reg));
assert((imm >> 3) == 0);
code |= imm; // cc
code |= (reg & 0x1f) << 5; // vj/xj
break;
#endif
default:
unreached();
break;
} // end switch (ins)
instrDesc* id = emitNewInstr(attr);
id->idIns(ins);
id->idReg1(reg);
id->idAddr()->iiaSetInstrEncode(code);
id->idCodeSize(4);
appendToCurIG(id);
}
//------------------------------------------------------------------------
// emitIns_Mov: Emits a move instruction
//
// Arguments:
// ins -- The instruction being emitted
// attr -- The emit attribute
// dstReg -- The destination register
// srcReg -- The source register
// canSkip -- true if the move can be elided when dstReg == srcReg, otherwise false
// insOpts -- The instruction options
//
void emitter::emitIns_Mov(
instruction ins, emitAttr attr, regNumber dstReg, regNumber srcReg, bool canSkip, insOpts opt /* = INS_OPTS_NONE */)
{
assert(IsMovInstruction(ins));
if (!canSkip || (dstReg != srcReg))
{
#ifdef FEATURE_SIMD
// This include dstReg is Float/SIMD type.
if (isVectorRegister(dstReg))
{
assert(size <= EA_32BYTE);
if (isVectorRegister(srcReg))
{
ins = size == EA_32BYTE ? INS_xvbsll_v : INS_vbsll_v;
emitIns_R_R_I(ins, size, dstReg, srcReg, 0);
}
else
{
assert((INS_vreplgr2vr_b <= ins && ins <= INS_vreplgr2vr_d) ||
(INS_xvreplgr2vr_b <= ins && ins <= INS_xvreplgr2vr_d) ||
(INS_movgr2fr_w <= ins && ins <= INS_movgr2fr_d));
emitIns_R_R(ins, attr, dstReg, srcReg);
}
}
else
#endif
{
if ((EA_4BYTE == attr) && (INS_mov == ins))
{
emitIns_R_R_I(INS_slli_w, attr, dstReg, srcReg, 0);
}
else
{
emitIns_R_R(ins, attr, dstReg, srcReg);
}
}
}
}
/*****************************************************************************
*
* Add an instruction referencing two registers
*/
void emitter::emitIns_R_R(
instruction ins, emitAttr attr, regNumber reg1, regNumber reg2, insOpts opt /* = INS_OPTS_NONE */)
{
code_t code = emitInsCode(ins);
if (INS_mov == ins)
{
assert(isGeneralRegisterOrR0(reg1));
assert(isGeneralRegisterOrR0(reg2));
code |= reg1; // rd
code |= reg2 << 5; // rj
}
else if ((INS_ext_w_b <= ins) && (ins <= INS_cpucfg))
{
#ifdef DEBUG
switch (ins)
{
case INS_ext_w_b:
case INS_ext_w_h:
case INS_clo_w:
case INS_clz_w:
case INS_cto_w:
case INS_ctz_w:
case INS_clo_d:
case INS_clz_d:
case INS_cto_d:
case INS_ctz_d:
case INS_revb_2h:
case INS_revb_4h:
case INS_revb_2w:
case INS_revb_d:
case INS_revh_2w:
case INS_revh_d:
case INS_bitrev_4b:
case INS_bitrev_8b:
case INS_bitrev_w:
case INS_bitrev_d:
case INS_rdtimel_w:
case INS_rdtimeh_w:
case INS_rdtime_d:
case INS_cpucfg:
break;
default:
NYI_LOONGARCH64("illegal ins within emitIns_R_R --1!");
}
#endif
assert(isGeneralRegisterOrR0(reg1));
assert(isGeneralRegisterOrR0(reg2));
code |= reg1; // rd
code |= reg2 << 5; // rj
}
// else if ((INS_asrtle_d == ins) || (INS_asrtgt_d == ins))
// {
// assert(isGeneralRegisterOrR0(reg1));
// assert(isGeneralRegisterOrR0(reg2));
// code |= reg1 << 5; // rj
// code |= reg2 << 10; // rk
// }
else if ((INS_fabs_s <= ins) && (ins <= INS_fmov_d))
{
#ifdef DEBUG
switch (ins)
{
case INS_fabs_s:
case INS_fabs_d:
case INS_fneg_s:
case INS_fneg_d:
case INS_fsqrt_s:
case INS_fsqrt_d:
case INS_frsqrt_s:
case INS_frsqrt_d:
case INS_frsqrte_s:
case INS_frsqrte_d:
case INS_frecip_s:
case INS_frecip_d:
case INS_frecipe_s:
case INS_frecipe_d:
case INS_flogb_s:
case INS_flogb_d:
case INS_fclass_s:
case INS_fclass_d:
case INS_fcvt_s_d:
case INS_fcvt_d_s:
case INS_ffint_s_w:
case INS_ffint_s_l:
case INS_ffint_d_w:
case INS_ffint_d_l:
case INS_ftint_w_s:
case INS_ftint_w_d:
case INS_ftint_l_s:
case INS_ftint_l_d:
case INS_ftintrm_w_s:
case INS_ftintrm_w_d:
case INS_ftintrm_l_s:
case INS_ftintrm_l_d:
case INS_ftintrp_w_s:
case INS_ftintrp_w_d:
case INS_ftintrp_l_s:
case INS_ftintrp_l_d:
case INS_ftintrz_w_s:
case INS_ftintrz_w_d:
case INS_ftintrz_l_s:
case INS_ftintrz_l_d:
case INS_ftintrne_w_s:
case INS_ftintrne_w_d:
case INS_ftintrne_l_s:
case INS_ftintrne_l_d:
case INS_frint_s:
case INS_frint_d:
case INS_fmov_s:
case INS_fmov_d:
break;
default:
NYI_LOONGARCH64("illegal ins within emitIns_R_R --2!");
}
#endif
assert(isFloatReg(reg1));
assert(isFloatReg(reg2));
code |= (reg1 & 0x1f); // fd
code |= (reg2 & 0x1f) << 5; // fj
}
else if ((INS_movgr2fr_w <= ins) && (ins <= INS_movgr2frh_w))
{
#ifdef DEBUG
switch (ins)
{
case INS_movgr2fr_w:
case INS_movgr2fr_d:
case INS_movgr2frh_w:
break;
default:
NYI_LOONGARCH64("illegal ins within emitIns_R_R --3!");
}
#endif
assert(isFloatReg(reg1));
assert(isGeneralRegisterOrR0(reg2));
code |= (reg1 & 0x1f); // fd
code |= reg2 << 5; // rj
}
else if ((INS_movfr2gr_s <= ins) && (ins <= INS_movfrh2gr_s))
{
#ifdef DEBUG
switch (ins)
{
case INS_movfr2gr_s:
case INS_movfr2gr_d:
case INS_movfrh2gr_s:
break;
default:
NYI_LOONGARCH64("illegal ins within emitIns_R_R --4!");
}
#endif
assert(isGeneralRegisterOrR0(reg1));
assert(isFloatReg(reg2));
code |= reg1; // rd
code |= (reg2 & 0x1f) << 5; // fj
}
else if ((INS_dneg == ins) || (INS_neg == ins))
{
assert(isGeneralRegisterOrR0(reg1));
assert(isGeneralRegisterOrR0(reg2));
// sub_d rd, zero, rk
// sub_w rd, zero, rk
code |= reg1; // rd
code |= reg2 << 10; // rk
}
else if (INS_not == ins)
{
assert(isGeneralRegisterOrR0(reg1));
assert(isGeneralRegisterOrR0(reg2));
// nor rd, rj, zero
code |= reg1; // rd
code |= reg2 << 5; // rj
}
#ifdef FEATURE_SIMD
else if (((INS_vreplgr2vr_b <= ins) && (ins <= INS_vreplgr2vr_d)) ||
((INS_xvreplgr2vr_b <= ins) && (ins <= INS_xvreplgr2vr_d)))
{
// [x]vreplgr2vr.{b/h/w/d} xd, rj
assert(isVectorRegister(reg1)); // vd(xd)
assert(isGeneralRegisterOrR0(reg2)); // rj
code |= (reg1 & 0x1f); // xd , the bit field in the instruction is between 0 and 31.
code |= reg2 << 5; // rj
}
else if (((INS_vclo_b <= ins) && (ins <= INS_vextl_qu_du)) || ((INS_xvclo_b <= ins) && (ins <= INS_xvextl_qu_du)))
{
assert(isVectorRegister(reg1));
assert(isVectorRegister(reg2));
code |= (reg1 & 0x1f); // vd(xd)
code |= (reg2 & 0x1f) << 5; // vj(xj)
}
#endif // FEATURE_SIMD
else
{
unreached();
}
instrDesc* id = emitNewInstr(attr);
id->idIns(ins);
id->idReg1(reg1);
id->idReg2(reg2);
id->idAddr()->iiaSetInstrEncode(code);
id->idCodeSize(4);
appendToCurIG(id);
}
/*****************************************************************************
*
* Add an instruction referencing two registers and a constant.
*/
void emitter::emitIns_R_R_I(
instruction ins, emitAttr attr, regNumber reg1, regNumber reg2, ssize_t imm, insOpts opt /* = INS_OPTS_NONE */)
{
code_t code = emitInsCode(ins);
if ((INS_slli_w <= ins) && (ins <= INS_rotri_w))
{
#ifdef DEBUG
switch (ins)
{
case INS_slli_w:
case INS_srli_w:
case INS_srai_w:
case INS_rotri_w:
break;
default:
NYI_LOONGARCH64("illegal ins within emitIns_R_R_I --1!");
}
#endif
assert(isGeneralRegister(reg1));
assert(isGeneralRegisterOrR0(reg2));
assert((0 <= imm) && (imm <= 0x1f));
code |= reg1; // rd
code |= reg2 << 5; // rj
code |= (imm & 0x1f) << 10; // ui5
}
else if ((INS_slli_d <= ins) && (ins <= INS_rotri_d))
{
#ifdef DEBUG
switch (ins)
{
case INS_slli_d:
case INS_srli_d:
case INS_srai_d:
case INS_rotri_d:
break;
default:
NYI_LOONGARCH64("illegal ins within emitIns_R_R_I --2!");
}
#endif
assert(isGeneralRegister(reg1));
assert(isGeneralRegisterOrR0(reg2));
assert((0 <= imm) && (imm <= 0x3f));
code |= reg1; // rd
code |= reg2 << 5; // rj
code |= (imm & 0x3f) << 10; // ui6