-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathemitriscv64.cpp
4772 lines (4198 loc) · 161 KB
/
emitriscv64.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 emitriscv64.cpp XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#if defined(TARGET_RISCV64)
/*****************************************************************************/
/*****************************************************************************/
#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];
}
/*****************************************************************************
* 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_JALR:
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;
}
}
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_sd:
case INS_sw:
case INS_sb:
case INS_sh:
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) info,
#include "instrs.h"
};
// clang-format on
inline bool emitter::emitInsMayWriteToGCReg(instruction ins)
{
assert(ins != INS_invalid);
return (ins <= INS_remuw) && (ins >= INS_mov) && !(ins >= INS_jal && ins <= INS_bgeu && ins != INS_jalr) &&
(CodeGenInterface::instInfo[ins] & ST) == 0
? true
: false;
}
//------------------------------------------------------------------------
// 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;
}
/*****************************************************************************
*
* 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) 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 = emitNewInstr(EA_8BYTE);
id->idIns(ins);
id->idAddr()->iiaSetInstrEncode(emitInsCode(ins));
id->idCodeSize(4);
appendToCurIG(id);
}
/*****************************************************************************
* emitter::emitIns_S_R(), emitter::emitIns_S_R_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
*
*/
void emitter::emitIns_S_R(instruction ins, emitAttr attr, regNumber reg1, int varx, int offs)
{
emitIns_S_R_R(ins, attr, reg1, REG_NA, varx, offs);
}
void emitter::emitIns_S_R_R(instruction ins, emitAttr attr, regNumber reg1, regNumber tmpReg, int varx, int offs)
{
ssize_t imm;
assert(tmpReg != codeGen->rsGetRsvdReg());
assert(reg1 != codeGen->rsGetRsvdReg());
emitAttr size = EA_SIZE(attr);
#ifdef DEBUG
switch (ins)
{
case INS_sd:
case INS_sw:
case INS_sh:
case INS_sb:
case INS_fsd:
case INS_fsw:
break;
default:
NYI_RISCV64("illegal ins within emitIns_S_R_R!");
return;
} // end switch (ins)
#endif
/* Figure out the variable's frame position */
int base;
bool FPbased;
base = emitComp->lvaFrameAddress(varx, &FPbased);
regNumber regBase = FPbased ? REG_FPBASE : REG_SPBASE;
regNumber reg2;
if (tmpReg == REG_NA)
{
reg2 = regBase;
imm = base + offs;
}
else
{
reg2 = tmpReg;
imm = offs;
}
assert(reg2 != REG_NA && reg2 != codeGen->rsGetRsvdReg());
if (!isValidSimm12(imm))
{
// If immediate does not fit to store immediate 12 bits, construct necessary value in rsRsvdReg()
// and keep tmpReg hint value unchanged.
assert(isValidSimm20((imm + 0x800) >> 12));
emitIns_R_I(INS_lui, EA_PTRSIZE, codeGen->rsGetRsvdReg(), (imm + 0x800) >> 12);
emitIns_R_R_R(INS_add, EA_PTRSIZE, codeGen->rsGetRsvdReg(), codeGen->rsGetRsvdReg(), reg2);
imm = imm & 0xfff;
reg2 = codeGen->rsGetRsvdReg();
}
instrDesc* id = emitNewInstr(attr);
id->idReg1(reg1);
id->idReg2(reg2);
id->idIns(ins);
assert(isGeneralRegister(reg2));
code_t code = emitInsCode(ins);
code |= (code_t)(reg1 & 0x1f) << 20;
code |= (code_t)reg2 << 15;
code |= (((imm >> 5) & 0x7f) << 25) | ((imm & 0x1f) << 7);
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_lb:
case INS_lbu:
case INS_lh:
case INS_lhu:
case INS_lw:
case INS_lwu:
case INS_flw:
case INS_ld:
case INS_fld:
break;
case INS_lea:
assert(size == EA_8BYTE);
break;
default:
NYI_RISCV64("illegal ins within 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;
assert(offs >= 0);
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;
}
code = emitInsCode(ins);
code |= (code_t)reg1 << 7;
code |= (code_t)reg2 << 15;
code |= (imm & 0xfff) << 20;
}
else
{
if (ins == INS_lea)
{
assert(isValidSimm20((imm + 0x800) >> 12));
emitIns_R_I(INS_lui, EA_PTRSIZE, codeGen->rsGetRsvdReg(), (imm + 0x800) >> 12);
ssize_t imm2 = imm & 0xfff;
emitIns_R_R_I(INS_addi, EA_PTRSIZE, codeGen->rsGetRsvdReg(), codeGen->rsGetRsvdReg(), imm2);
ins = INS_add;
code = emitInsCode(ins);
code |= (code_t)reg1 << 7;
code |= (code_t)reg2 << 15;
code |= (code_t)codeGen->rsGetRsvdReg() << 20;
}
else
{
assert(isValidSimm20((imm + 0x800) >> 12));
emitIns_R_I(INS_lui, EA_PTRSIZE, codeGen->rsGetRsvdReg(), (imm + 0x800) >> 12);
emitIns_R_R_R(INS_add, EA_PTRSIZE, codeGen->rsGetRsvdReg(), codeGen->rsGetRsvdReg(), reg2);
ssize_t imm2 = imm & 0xfff;
code = emitInsCode(ins);
code |= (code_t)reg1 << 7;
code |= (code_t)codeGen->rsGetRsvdReg() << 15;
code |= (code_t)(imm2 & 0xfff) << 20;
}
}
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_fence:
code |= ((imm & 0xff) << 20);
break;
case INS_j:
assert(imm >= -1048576 && imm < 1048576);
code |= ((imm >> 12) & 0xff) << 12;
code |= ((imm >> 11) & 0x1) << 20;
code |= ((imm >> 1) & 0x3ff) << 21;
code |= ((imm >> 20) & 0x1) << 31;
break;
default:
NO_WAY("illegal ins within emitIns_I!");
}
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)
{
NYI_RISCV64("emitIns_I_I-----unimplemented/unused on RISCV64 yet----");
}
/*****************************************************************************
*
* 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_lui:
case INS_auipc:
assert(reg != REG_R0);
assert(isGeneralRegister(reg));
assert((((size_t)imm) >> 20) == 0);
code |= reg << 7;
code |= (imm & 0xfffff) << 12;
break;
case INS_jal:
assert(isGeneralRegisterOrR0(reg));
assert(isValidSimm21(imm));
code |= reg << 7;
code |= ((imm >> 12) & 0xff) << 12;
code |= ((imm >> 11) & 0x1) << 20;
code |= ((imm >> 1) & 0x3ff) << 21;
code |= ((imm >> 20) & 0x1) << 31;
break;
default:
NO_WAY("illegal ins within emitIns_R_I!");
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 */)
{
if (!canSkip || (dstReg != srcReg))
{
if ((EA_4BYTE == attr) && (INS_mov == ins))
{
assert(isGeneralRegisterOrR0(srcReg));
assert(isGeneralRegisterOrR0(dstReg));
emitIns_R_R_I(INS_addiw, attr, dstReg, srcReg, 0);
}
else if (INS_fsgnj_s == ins || INS_fsgnj_d == ins)
{
assert(isFloatReg(srcReg));
assert(isFloatReg(dstReg));
emitIns_R_R_R(ins, attr, dstReg, srcReg, srcReg);
}
else if (genIsValidFloatReg(srcReg) || genIsValidFloatReg(dstReg))
{
emitIns_R_R(ins, attr, dstReg, srcReg);
}
else
{
assert(isGeneralRegisterOrR0(srcReg));
assert(isGeneralRegisterOrR0(dstReg));
emitIns_R_R_I(INS_addi, attr, dstReg, srcReg, 0);
}
}
}
void emitter::emitIns_Mov(emitAttr attr, regNumber dstReg, regNumber srcReg, bool canSkip)
{
if (!canSkip || dstReg != srcReg)
{
assert(attr == EA_4BYTE || attr == EA_PTRSIZE);
if (isGeneralRegisterOrR0(dstReg) && isGeneralRegisterOrR0(srcReg))
{
emitIns_R_R_I(attr == EA_4BYTE ? INS_addiw : INS_addi, attr, dstReg, srcReg, 0);
}
else if (isGeneralRegisterOrR0(dstReg) && genIsValidFloatReg(srcReg))
{
emitIns_R_R(attr == EA_4BYTE ? INS_fmv_x_w : INS_fmv_x_d, attr, dstReg, srcReg);
}
else if (genIsValidFloatReg(dstReg) && isGeneralRegisterOrR0(srcReg))
{
emitIns_R_R(attr == EA_4BYTE ? INS_fmv_w_x : INS_fmv_d_x, attr, dstReg, srcReg);
}
else if (genIsValidFloatReg(dstReg) && genIsValidFloatReg(srcReg))
{
emitIns_R_R_R(attr == EA_4BYTE ? INS_fsgnj_s : INS_fsgnj_d, attr, dstReg, srcReg, srcReg);
}
else
{
assert(!"Invalid registers in emitIns_Mov()\n");
}
}
}
/*****************************************************************************
*
* 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 << 7;
code |= reg2 << 15;
}
else if (INS_fmv_x_d == ins || INS_fmv_x_w == ins || INS_fclass_s == ins || INS_fclass_d == ins)
{
assert(isGeneralRegisterOrR0(reg1));
assert(isFloatReg(reg2));
code |= reg1 << 7;
code |= (reg2 & 0x1f) << 15;
}
else if (INS_fcvt_w_s == ins || INS_fcvt_wu_s == ins || INS_fcvt_w_d == ins || INS_fcvt_wu_d == ins ||
INS_fcvt_l_s == ins || INS_fcvt_lu_s == ins || INS_fcvt_l_d == ins || INS_fcvt_lu_d == ins)
{
assert(isGeneralRegisterOrR0(reg1));
assert(isFloatReg(reg2));
code |= reg1 << 7;
code |= (reg2 & 0x1f) << 15;
code |= 0x1 << 12;
}
else if (INS_fmv_w_x == ins || INS_fmv_d_x == ins)
{
assert(isFloatReg(reg1));
assert(isGeneralRegisterOrR0(reg2));
code |= (reg1 & 0x1f) << 7;
code |= reg2 << 15;
}
else if (INS_fcvt_s_w == ins || INS_fcvt_s_wu == ins || INS_fcvt_d_w == ins || INS_fcvt_d_wu == ins ||
INS_fcvt_s_l == ins || INS_fcvt_s_lu == ins || INS_fcvt_d_l == ins || INS_fcvt_d_lu == ins)
{
assert(isFloatReg(reg1));
assert(isGeneralRegisterOrR0(reg2));
code |= (reg1 & 0x1f) << 7;
code |= reg2 << 15;
code |= 0x7 << 12;
}
else if (INS_fcvt_s_d == ins || INS_fcvt_d_s == ins)
{
assert(isFloatReg(reg1));
assert(isFloatReg(reg2));
code |= (reg1 & 0x1f) << 7;
code |= (reg2 & 0x1f) << 15;
code |= 0x7 << 12;
}
else
{
NYI_RISCV64("illegal ins within emitIns_R_R!");
}
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_addi <= ins && INS_srai >= ins) || (INS_addiw <= ins && INS_sraiw >= ins) ||
(INS_lb <= ins && INS_lhu >= ins) || INS_ld == ins || INS_lw == ins || INS_jalr == ins || INS_fld == ins ||
INS_flw == ins)
{
assert(isGeneralRegister(reg2));
code |= (reg1 & 0x1f) << 7; // rd
code |= reg2 << 15; // rs1
code |= imm << 20; // imm
}
else if (INS_sd == ins || INS_sw == ins || INS_sh == ins || INS_sb == ins || INS_fsw == ins || INS_fsd == ins)
{
assert(isGeneralRegister(reg2));
code |= (reg1 & 0x1f) << 20; // rs2
code |= reg2 << 15; // rs1
code |= (((imm >> 5) & 0x7f) << 25) | ((imm & 0x1f) << 7); // imm
}
else if (INS_beq <= ins && INS_bgeu >= ins)
{
assert(isGeneralRegister(reg1));
assert(isGeneralRegister(reg2));
assert(isValidSimm13(imm));
assert(!(imm & 3));
code |= reg1 << 15;
code |= reg2 << 20;
code |= ((imm >> 11) & 0x1) << 7;
code |= ((imm >> 1) & 0xf) << 8;
code |= ((imm >> 5) & 0x3f) << 25;
code |= ((imm >> 12) & 0x1) << 31;
}
else if (ins == INS_csrrs || ins == INS_csrrw || ins == INS_csrrc)
{
assert(isGeneralRegisterOrR0(reg1));
assert(isGeneralRegisterOrR0(reg2));
assert(isValidUimm12(imm));
code |= reg1 << 7;
code |= reg2 << 15;
code |= imm << 20;
}
else
{
NYI_RISCV64("illegal ins within emitIns_R_R_I!");
}
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 register and two constants.
*/
void emitter::emitIns_R_I_I(
instruction ins, emitAttr attr, regNumber reg1, ssize_t imm1, ssize_t imm2, insOpts opt) /* = INS_OPTS_NONE */
{
code_t code = emitInsCode(ins);
if (INS_csrrwi <= ins && ins <= INS_csrrci)
{
assert(isGeneralRegisterOrR0(reg1));
assert(isValidUimm5(imm1));
assert(isValidUimm12(imm2));
code |= reg1 << 7;
code |= imm1 << 15;
code |= imm2 << 20;
}
else
{
NYI_RISCV64("illegal ins within emitIns_R_I_I!");
}
instrDesc* id = emitNewInstr(attr);
id->idIns(ins);
id->idReg1(reg1);
id->idAddr()->iiaSetInstrEncode(code);
id->idCodeSize(4);
appendToCurIG(id);
}
/*****************************************************************************
*
* Add an instruction referencing three registers.
*/
void emitter::emitIns_R_R_R(
instruction ins, emitAttr attr, regNumber reg1, regNumber reg2, regNumber reg3, insOpts opt) /* = INS_OPTS_NONE */
{
code_t code = emitInsCode(ins);
if ((INS_add <= ins && ins <= INS_and) || (INS_mul <= ins && ins <= INS_remuw) ||
(INS_addw <= ins && ins <= INS_sraw) || (INS_fadd_s <= ins && ins <= INS_fmax_s) ||
(INS_fadd_d <= ins && ins <= INS_fmax_d) || (INS_feq_s <= ins && ins <= INS_fle_s) ||
(INS_feq_d <= ins && ins <= INS_fle_d) || (INS_lr_w <= ins && ins <= INS_amomaxu_d))
{
#ifdef DEBUG
switch (ins)
{
case INS_add:
case INS_sub:
case INS_sll:
case INS_slt:
case INS_sltu:
case INS_xor:
case INS_srl:
case INS_sra:
case INS_or:
case INS_and:
case INS_addw:
case INS_subw:
case INS_sllw:
case INS_srlw:
case INS_sraw:
case INS_mul:
case INS_mulh:
case INS_mulhsu:
case INS_mulhu:
case INS_div:
case INS_divu:
case INS_rem:
case INS_remu:
case INS_mulw:
case INS_divw:
case INS_divuw:
case INS_remw:
case INS_remuw:
case INS_fadd_s:
case INS_fsub_s:
case INS_fmul_s:
case INS_fdiv_s:
case INS_fsqrt_s:
case INS_fsgnj_s:
case INS_fsgnjn_s:
case INS_fsgnjx_s:
case INS_fmin_s:
case INS_fmax_s:
case INS_feq_s:
case INS_flt_s:
case INS_fle_s:
case INS_fadd_d:
case INS_fsub_d:
case INS_fmul_d:
case INS_fdiv_d:
case INS_fsqrt_d:
case INS_fsgnj_d:
case INS_fsgnjn_d:
case INS_fsgnjx_d:
case INS_fmin_d:
case INS_fmax_d:
case INS_feq_d:
case INS_flt_d:
case INS_fle_d:
case INS_lr_w:
case INS_lr_d:
case INS_sc_w:
case INS_sc_d:
case INS_amoswap_w:
case INS_amoswap_d:
case INS_amoadd_w:
case INS_amoadd_d:
case INS_amoxor_w:
case INS_amoxor_d:
case INS_amoand_w:
case INS_amoand_d:
case INS_amoor_w:
case INS_amoor_d:
case INS_amomin_w:
case INS_amomin_d:
case INS_amomax_w:
case INS_amomax_d:
case INS_amominu_w:
case INS_amominu_d:
case INS_amomaxu_w:
case INS_amomaxu_d:
break;
default:
NYI_RISCV64("illegal ins within emitIns_R_R_R!");
}
#endif
// Src/data register for load reserved should be empty
assert((ins != INS_lr_w && ins != INS_lr_d) || reg3 == REG_R0);
code |= ((reg1 & 0x1f) << 7);
code |= ((reg2 & 0x1f) << 15);
code |= ((reg3 & 0x1f) << 20);
if ((INS_fadd_s <= ins && INS_fsqrt_s >= ins) || (INS_fadd_d <= ins && INS_fsqrt_d >= ins))
{
code |= 0x7 << 12;
}
else if (INS_lr_w <= ins && ins <= INS_amomaxu_d)
{
// For now all atomics are seq. consistent as Interlocked.* APIs don't expose acquire/release ordering
code |= 0b11 << 25;
}
}
else
{
NYI_RISCV64("illegal ins within emitIns_R_R_R!");
}
instrDesc* id = emitNewInstr(attr);
id->idIns(ins);
id->idReg1(reg1);
id->idReg2(reg2);
id->idReg3(reg3);
id->idAddr()->iiaSetInstrEncode(code);
id->idCodeSize(4);
appendToCurIG(id);
}
/*****************************************************************************
*
* Add an instruction referencing three registers and a constant.
*/
void emitter::emitIns_R_R_R_I(instruction ins,
emitAttr attr,
regNumber reg1,
regNumber reg2,
regNumber reg3,
ssize_t imm,
insOpts opt /* = INS_OPTS_NONE */,
emitAttr attrReg2 /* = EA_UNKNOWN */)
{
NYI_RISCV64("emitIns_R_R_R_I-----unimplemented/unused on RISCV64 yet----");
}
/*****************************************************************************
*
* Add an instruction referencing two registers and two constants.
*/
void emitter::emitIns_R_R_I_I(
instruction ins, emitAttr attr, regNumber reg1, regNumber reg2, int imm1, int imm2, insOpts opt)
{
NYI_RISCV64("emitIns_R_R_I_I-----unimplemented/unused on RISCV64 yet----");
}
/*****************************************************************************
*
* Add an instruction referencing four registers.
*/
void emitter::emitIns_R_R_R_R(
instruction ins, emitAttr attr, regNumber reg1, regNumber reg2, regNumber reg3, regNumber reg4)
{
NYI_RISCV64("emitIns_R_R_R_R-----unimplemented/unused on RISCV64 yet----");
}
/*****************************************************************************
*
* Add an instruction with a register + static member operands.
* Constant is stored into JIT data which is adjacent to code.
*
*/
void emitter::emitIns_R_C(
instruction ins, emitAttr attr, regNumber reg, regNumber addrReg, CORINFO_FIELD_HANDLE fldHnd, int offs)
{
assert(offs >= 0);
assert(instrDesc::fitsInSmallCns(offs)); // can optimize.
instrDesc* id = emitNewInstr(attr);
id->idIns(ins);
assert(reg != REG_R0); // for special. reg Must not be R0.
id->idReg1(reg); // destination register that will get the constant value.
id->idSmallCns(offs); // usually is 0.
id->idInsOpt(INS_OPTS_RC);
if (emitComp->opts.compReloc)
{
id->idSetIsDspReloc();
id->idCodeSize(8);
}
else
id->idCodeSize(16);
if (EA_IS_GCREF(attr))
{
/* A special value indicates a GCref pointer value */
id->idGCref(GCT_GCREF);
id->idOpSize(EA_PTRSIZE);
}
else if (EA_IS_BYREF(attr))
{
/* A special value indicates a Byref pointer value */
id->idGCref(GCT_BYREF);
id->idOpSize(EA_PTRSIZE);
}
// TODO-RISCV64: this maybe deleted.
id->idSetIsBound(); // We won't patch address since we will know the exact distance
// once JIT code and data are allocated together.
assert(addrReg == REG_NA); // NOTE: for RISV64, not support addrReg != REG_NA.
id->idAddr()->iiaFieldHnd = fldHnd;
appendToCurIG(id);
}
void emitter::emitIns_R_AR(instruction ins, emitAttr attr, regNumber ireg, regNumber reg, int offs)
{
NYI_RISCV64("emitIns_R_AR-----unimplemented/unused on RISCV64 yet----");
}
// This computes address from the immediate which is relocatable.
void emitter::emitIns_R_AI(instruction ins,
emitAttr attr,
regNumber reg,
ssize_t addr DEBUGARG(size_t targetHandle) DEBUGARG(GenTreeFlags gtFlags))
{
assert(EA_IS_RELOC(attr)); // EA_PTR_DSP_RELOC
assert(ins == INS_jal); // for special.
assert(isGeneralRegister(reg));
// INS_OPTS_RELOC: placeholders. 2-ins:
// case:EA_HANDLE_CNS_RELOC
// auipc reg, off-hi-20bits
// addi reg, reg, off-lo-12bits
// case:EA_PTR_DSP_RELOC
// auipc reg, off-hi-20bits