This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathinstr.cpp
2373 lines (2090 loc) · 64.2 KB
/
instr.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.
// See the LICENSE file in the project root for more information.
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX XX
XX Instruction XX
XX XX
XX The interface to generate a machine-instruction. XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#include "codegen.h"
#include "instr.h"
#include "emit.h"
/*****************************************************************************/
#ifdef DEBUG
/*****************************************************************************
*
* Returns the string representation of the given CPU instruction.
*/
const char* CodeGen::genInsName(instruction ins)
{
// clang-format off
static
const char * const insNames[] =
{
#if defined(_TARGET_XARCH_)
#define INST0(id, nm, um, mr, flags) nm,
#define INST1(id, nm, um, mr, flags) nm,
#define INST2(id, nm, um, mr, mi, flags) nm,
#define INST3(id, nm, um, mr, mi, rm, flags) nm,
#define INST4(id, nm, um, mr, mi, rm, a4, flags) nm,
#define INST5(id, nm, um, mr, mi, rm, a4, rr, flags) nm,
#include "instrs.h"
#elif defined(_TARGET_ARM_)
#define INST1(id, nm, fp, ldst, fmt, e1 ) nm,
#define INST2(id, nm, fp, ldst, fmt, e1, e2 ) nm,
#define INST3(id, nm, fp, ldst, fmt, e1, e2, e3 ) nm,
#define INST4(id, nm, fp, ldst, fmt, e1, e2, e3, e4 ) nm,
#define INST5(id, nm, fp, ldst, fmt, e1, e2, e3, e4, e5 ) nm,
#define INST6(id, nm, fp, ldst, fmt, e1, e2, e3, e4, e5, e6 ) nm,
#define INST8(id, nm, fp, ldst, fmt, e1, e2, e3, e4, e5, e6, e7, e8 ) nm,
#define INST9(id, nm, fp, ldst, fmt, e1, e2, e3, e4, e5, e6, e7, e8, e9 ) nm,
#include "instrs.h"
#elif defined(_TARGET_ARM64_)
#define INST1(id, nm, fp, ldst, fmt, e1 ) nm,
#define INST2(id, nm, fp, ldst, fmt, e1, e2 ) nm,
#define INST3(id, nm, fp, ldst, fmt, e1, e2, e3 ) nm,
#define INST4(id, nm, fp, ldst, fmt, e1, e2, e3, e4 ) nm,
#define INST5(id, nm, fp, ldst, fmt, e1, e2, e3, e4, e5 ) nm,
#define INST6(id, nm, fp, ldst, fmt, e1, e2, e3, e4, e5, e6 ) nm,
#define INST9(id, nm, fp, ldst, fmt, e1, e2, e3, e4, e5, e6, e7, e8, e9 ) nm,
#include "instrs.h"
#else
#error "Unknown _TARGET_"
#endif
};
// clang-format on
assert((unsigned)ins < _countof(insNames));
assert(insNames[ins] != nullptr);
return insNames[ins];
}
void __cdecl CodeGen::instDisp(instruction ins, bool noNL, const char* fmt, ...)
{
if (compiler->opts.dspCode)
{
/* Display the instruction offset within the emit block */
// printf("[%08X:%04X]", GetEmitter().emitCodeCurBlock(), GetEmitter().emitCodeOffsInBlock());
/* Display the FP stack depth (before the instruction is executed) */
// printf("[FP=%02u] ", genGetFPstkLevel());
/* Display the instruction mnemonic */
printf(" ");
printf(" %-8s", genInsName(ins));
if (fmt)
{
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}
if (!noNL)
{
printf("\n");
}
}
}
/*****************************************************************************/
#endif // DEBUG
/*****************************************************************************/
void CodeGen::instInit()
{
}
/*****************************************************************************
*
* Return the size string (e.g. "word ptr") appropriate for the given size.
*/
#ifdef DEBUG
const char* CodeGen::genSizeStr(emitAttr attr)
{
// clang-format off
static
const char * const sizes[] =
{
"",
"byte ptr ",
"word ptr ",
nullptr,
"dword ptr ",
nullptr,
nullptr,
nullptr,
"qword ptr ",
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
"xmmword ptr ",
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
"ymmword ptr"
};
// clang-format on
unsigned size = EA_SIZE(attr);
assert(size == 0 || size == 1 || size == 2 || size == 4 || size == 8 || size == 16 || size == 32);
if (EA_ATTR(size) == attr)
{
return sizes[size];
}
else if (attr == EA_GCREF)
{
return "gword ptr ";
}
else if (attr == EA_BYREF)
{
return "bword ptr ";
}
else if (EA_IS_DSP_RELOC(attr))
{
return "rword ptr ";
}
else
{
assert(!"Unexpected");
return "unknw ptr ";
}
}
#endif
/*****************************************************************************
*
* Generate an instruction.
*/
void CodeGen::instGen(instruction ins)
{
GetEmitter()->emitIns(ins);
#ifdef _TARGET_XARCH_
// A workaround necessitated by limitations of emitter
// if we are scheduled to insert a nop here, we have to delay it
// hopefully we have not missed any other prefix instructions or places
// they could be inserted
if (ins == INS_lock && GetEmitter()->emitNextNop == 0)
{
GetEmitter()->emitNextNop = 1;
}
#endif
}
/*****************************************************************************
*
* Returns non-zero if the given CPU instruction is a floating-point ins.
*/
// static inline
bool CodeGenInterface::instIsFP(instruction ins)
{
assert((unsigned)ins < _countof(instInfo));
#ifdef _TARGET_XARCH_
return (instInfo[ins] & INS_FLAGS_x87Instr) != 0;
#else
return (instInfo[ins] & INST_FP) != 0;
#endif
}
#ifdef _TARGET_XARCH_
/*****************************************************************************
*
* Generate a multi-byte NOP instruction.
*/
void CodeGen::instNop(unsigned size)
{
assert(size <= 15);
GetEmitter()->emitIns_Nop(size);
}
#endif
/*****************************************************************************
*
* Generate a jump instruction.
*/
void CodeGen::inst_JMP(emitJumpKind jmp, BasicBlock* tgtBlock)
{
#if !FEATURE_FIXED_OUT_ARGS
// On the x86 we are pushing (and changing the stack level), but on x64 and other archs we have
// a fixed outgoing args area that we store into and we never change the stack level when calling methods.
//
// Thus only on x86 do we need to assert that the stack level at the target block matches the current stack level.
//
CLANG_FORMAT_COMMENT_ANCHOR;
#ifdef UNIX_X86_ABI
// bbTgtStkDepth is a (pure) argument count (stack alignment padding should be excluded).
assert((tgtBlock->bbTgtStkDepth * sizeof(int) == (genStackLevel - curNestedAlignment)) || isFramePointerUsed());
#else
assert((tgtBlock->bbTgtStkDepth * sizeof(int) == genStackLevel) || isFramePointerUsed());
#endif
#endif // !FEATURE_FIXED_OUT_ARGS
GetEmitter()->emitIns_J(emitter::emitJumpKindToIns(jmp), tgtBlock);
}
/*****************************************************************************
*
* Generate a set instruction.
*/
void CodeGen::inst_SET(emitJumpKind condition, regNumber reg)
{
#ifdef _TARGET_XARCH_
instruction ins;
/* Convert the condition to an instruction opcode */
switch (condition)
{
case EJ_js:
ins = INS_sets;
break;
case EJ_jns:
ins = INS_setns;
break;
case EJ_je:
ins = INS_sete;
break;
case EJ_jne:
ins = INS_setne;
break;
case EJ_jl:
ins = INS_setl;
break;
case EJ_jle:
ins = INS_setle;
break;
case EJ_jge:
ins = INS_setge;
break;
case EJ_jg:
ins = INS_setg;
break;
case EJ_jb:
ins = INS_setb;
break;
case EJ_jbe:
ins = INS_setbe;
break;
case EJ_jae:
ins = INS_setae;
break;
case EJ_ja:
ins = INS_seta;
break;
case EJ_jp:
ins = INS_setp;
break;
case EJ_jnp:
ins = INS_setnp;
break;
default:
NO_WAY("unexpected condition type");
return;
}
assert(genRegMask(reg) & RBM_BYTE_REGS);
// These instructions only write the low byte of 'reg'
GetEmitter()->emitIns_R(ins, EA_1BYTE, reg);
#elif defined(_TARGET_ARM64_)
insCond cond;
/* Convert the condition to an insCond value */
switch (condition)
{
case EJ_eq:
cond = INS_COND_EQ;
break;
case EJ_ne:
cond = INS_COND_NE;
break;
case EJ_hs:
cond = INS_COND_HS;
break;
case EJ_lo:
cond = INS_COND_LO;
break;
case EJ_mi:
cond = INS_COND_MI;
break;
case EJ_pl:
cond = INS_COND_PL;
break;
case EJ_vs:
cond = INS_COND_VS;
break;
case EJ_vc:
cond = INS_COND_VC;
break;
case EJ_hi:
cond = INS_COND_HI;
break;
case EJ_ls:
cond = INS_COND_LS;
break;
case EJ_ge:
cond = INS_COND_GE;
break;
case EJ_lt:
cond = INS_COND_LT;
break;
case EJ_gt:
cond = INS_COND_GT;
break;
case EJ_le:
cond = INS_COND_LE;
break;
default:
NO_WAY("unexpected condition type");
return;
}
GetEmitter()->emitIns_R_COND(INS_cset, EA_8BYTE, reg, cond);
#else
NYI("inst_SET");
#endif
}
/*****************************************************************************
*
* Generate a "op reg" instruction.
*/
void CodeGen::inst_RV(instruction ins, regNumber reg, var_types type, emitAttr size)
{
if (size == EA_UNKNOWN)
{
size = emitActualTypeSize(type);
}
GetEmitter()->emitIns_R(ins, size, reg);
}
/*****************************************************************************
*
* Generate a "op reg1, reg2" instruction.
*/
void CodeGen::inst_RV_RV(instruction ins,
regNumber reg1,
regNumber reg2,
var_types type,
emitAttr size,
insFlags flags /* = INS_FLAGS_DONT_CARE */)
{
if (size == EA_UNKNOWN)
{
size = emitActualTypeSize(type);
}
#ifdef _TARGET_ARM_
GetEmitter()->emitIns_R_R(ins, size, reg1, reg2, flags);
#else
GetEmitter()->emitIns_R_R(ins, size, reg1, reg2);
#endif
}
/*****************************************************************************
*
* Generate a "op reg1, reg2, reg3" instruction.
*/
void CodeGen::inst_RV_RV_RV(instruction ins,
regNumber reg1,
regNumber reg2,
regNumber reg3,
emitAttr size,
insFlags flags /* = INS_FLAGS_DONT_CARE */)
{
#ifdef _TARGET_ARM_
GetEmitter()->emitIns_R_R_R(ins, size, reg1, reg2, reg3, flags);
#elif defined(_TARGET_XARCH_)
GetEmitter()->emitIns_R_R_R(ins, size, reg1, reg2, reg3);
#else
NYI("inst_RV_RV_RV");
#endif
}
/*****************************************************************************
*
* Generate a "op icon" instruction.
*/
void CodeGen::inst_IV(instruction ins, int val)
{
GetEmitter()->emitIns_I(ins, EA_PTRSIZE, val);
}
/*****************************************************************************
*
* Generate a "op icon" instruction where icon is a handle of type specified
* by 'flags'
*/
void CodeGen::inst_IV_handle(instruction ins, int val)
{
GetEmitter()->emitIns_I(ins, EA_HANDLE_CNS_RELOC, val);
}
/*****************************************************************************
*
* Display a stack frame reference.
*/
void CodeGen::inst_set_SV_var(GenTree* tree)
{
#ifdef DEBUG
assert(tree && (tree->gtOper == GT_LCL_VAR || tree->gtOper == GT_LCL_VAR_ADDR || tree->gtOper == GT_STORE_LCL_VAR));
assert(tree->AsLclVarCommon()->GetLclNum() < compiler->lvaCount);
GetEmitter()->emitVarRefOffs = tree->AsLclVar()->gtLclILoffs;
#endif // DEBUG
}
/*****************************************************************************
*
* Generate a "op reg, icon" instruction.
*/
void CodeGen::inst_RV_IV(
instruction ins, regNumber reg, target_ssize_t val, emitAttr size, insFlags flags /* = INS_FLAGS_DONT_CARE */)
{
#if !defined(_TARGET_64BIT_)
assert(size != EA_8BYTE);
#endif
#ifdef _TARGET_ARM_
if (arm_Valid_Imm_For_Instr(ins, val, flags))
{
GetEmitter()->emitIns_R_I(ins, size, reg, val, flags);
}
else if (ins == INS_mov)
{
instGen_Set_Reg_To_Imm(size, reg, val);
}
else
{
// TODO-Cleanup: Add a comment about why this is unreached() for RyuJIT backend.
unreached();
}
#elif defined(_TARGET_ARM64_)
// TODO-Arm64-Bug: handle large constants!
// Probably need something like the ARM case above: if (arm_Valid_Imm_For_Instr(ins, val)) ...
assert(ins != INS_cmp);
assert(ins != INS_tst);
assert(ins != INS_mov);
GetEmitter()->emitIns_R_R_I(ins, size, reg, reg, val);
#else // !_TARGET_ARM_
#ifdef _TARGET_AMD64_
// Instead of an 8-byte immediate load, a 4-byte immediate will do fine
// as the high 4 bytes will be zero anyway.
if (size == EA_8BYTE && ins == INS_mov && ((val & 0xFFFFFFFF00000000LL) == 0))
{
size = EA_4BYTE;
GetEmitter()->emitIns_R_I(ins, size, reg, val);
}
else if (EA_SIZE(size) == EA_8BYTE && ins != INS_mov && (((int)val != val) || EA_IS_CNS_RELOC(size)))
{
assert(!"Invalid immediate for inst_RV_IV");
}
else
#endif // _TARGET_AMD64_
{
GetEmitter()->emitIns_R_I(ins, size, reg, val);
}
#endif // !_TARGET_ARM_
}
/*****************************************************************************
*
* Generate an instruction that has one operand given by a tree (which has
* been made addressable).
*/
void CodeGen::inst_TT(instruction ins, GenTree* tree, unsigned offs, int shfv, emitAttr size)
{
bool sizeInferred = false;
if (size == EA_UNKNOWN)
{
sizeInferred = true;
if (instIsFP(ins))
{
size = EA_ATTR(genTypeSize(tree->TypeGet()));
}
else
{
size = emitTypeSize(tree->TypeGet());
}
}
AGAIN:
/* Is this a spilled value? */
if (tree->gtFlags & GTF_SPILLED)
{
assert(!"ISSUE: If this can happen, we need to generate 'ins [ebp+spill]'");
}
switch (tree->gtOper)
{
unsigned varNum;
case GT_LCL_VAR:
inst_set_SV_var(tree);
goto LCL;
case GT_LCL_FLD:
offs += tree->AsLclFld()->gtLclOffs;
goto LCL;
LCL:
varNum = tree->AsLclVarCommon()->GetLclNum();
assert(varNum < compiler->lvaCount);
if (shfv)
{
GetEmitter()->emitIns_S_I(ins, size, varNum, offs, shfv);
}
else
{
GetEmitter()->emitIns_S(ins, size, varNum, offs);
}
return;
case GT_CLS_VAR:
// Make sure FP instruction size matches the operand size
// (We optimized constant doubles to floats when we can, just want to
// make sure that we don't mistakenly use 8 bytes when the
// constant.
assert(!isFloatRegType(tree->gtType) || genTypeSize(tree->gtType) == EA_SIZE_IN_BYTES(size));
if (shfv)
{
GetEmitter()->emitIns_C_I(ins, size, tree->AsClsVar()->gtClsVarHnd, offs, shfv);
}
else
{
GetEmitter()->emitIns_C(ins, size, tree->AsClsVar()->gtClsVarHnd, offs);
}
return;
case GT_IND:
case GT_NULLCHECK:
case GT_ARR_ELEM:
{
assert(!"inst_TT not supported for GT_IND, GT_NULLCHECK or GT_ARR_ELEM");
}
break;
#ifdef _TARGET_X86_
case GT_CNS_INT:
// We will get here for GT_MKREFANY from CodeGen::genPushArgList
assert(offs == 0);
assert(!shfv);
if (tree->IsIconHandle())
inst_IV_handle(ins, tree->AsIntCon()->gtIconVal);
else
inst_IV(ins, tree->AsIntCon()->gtIconVal);
break;
#endif
case GT_COMMA:
// tree->AsOp()->gtOp1 - already processed by genCreateAddrMode()
tree = tree->AsOp()->gtOp2;
goto AGAIN;
default:
assert(!"invalid address");
}
}
/*****************************************************************************
*
* Generate an instruction that has one operand given by a tree (which has
* been made addressable) and another that is a register.
*/
void CodeGen::inst_TT_RV(instruction ins, GenTree* tree, regNumber reg, unsigned offs, emitAttr size, insFlags flags)
{
assert(reg != REG_STK);
AGAIN:
/* Is this a spilled value? */
if (tree->gtFlags & GTF_SPILLED)
{
assert(!"ISSUE: If this can happen, we need to generate 'ins [ebp+spill]'");
}
if (size == EA_UNKNOWN)
{
if (instIsFP(ins))
{
size = EA_ATTR(genTypeSize(tree->TypeGet()));
}
else
{
size = emitTypeSize(tree->TypeGet());
}
}
switch (tree->gtOper)
{
unsigned varNum;
case GT_LCL_VAR:
inst_set_SV_var(tree);
goto LCL;
case GT_LCL_FLD:
case GT_STORE_LCL_FLD:
offs += tree->AsLclFld()->gtLclOffs;
goto LCL;
LCL:
varNum = tree->AsLclVarCommon()->GetLclNum();
assert(varNum < compiler->lvaCount);
#if CPU_LOAD_STORE_ARCH
if (!GetEmitter()->emitInsIsStore(ins))
{
// TODO-LdStArch-Bug: Should regTmp be a dst on the node or an internal reg?
// Either way, it is not currently being handled by Lowering.
regNumber regTmp = tree->GetRegNum();
assert(regTmp != REG_NA);
GetEmitter()->emitIns_R_S(ins_Load(tree->TypeGet()), size, regTmp, varNum, offs);
GetEmitter()->emitIns_R_R(ins, size, regTmp, reg, flags);
GetEmitter()->emitIns_S_R(ins_Store(tree->TypeGet()), size, regTmp, varNum, offs);
regSet.verifyRegUsed(regTmp);
}
else
#endif
{
// ins is a Store instruction
//
GetEmitter()->emitIns_S_R(ins, size, reg, varNum, offs);
#ifdef _TARGET_ARM_
// If we need to set the flags then add an extra movs reg,reg instruction
if (flags == INS_FLAGS_SET)
GetEmitter()->emitIns_R_R(INS_mov, size, reg, reg, INS_FLAGS_SET);
#endif
}
return;
case GT_CLS_VAR:
// Make sure FP instruction size matches the operand size
// (We optimized constant doubles to floats when we can, just want to
// make sure that we don't mistakenly use 8 bytes when the
// constant).
assert(!isFloatRegType(tree->gtType) || genTypeSize(tree->gtType) == EA_SIZE_IN_BYTES(size));
#if CPU_LOAD_STORE_ARCH
if (!GetEmitter()->emitInsIsStore(ins))
{
NYI("Store of GT_CLS_VAR not supported for ARM");
}
else
#endif // CPU_LOAD_STORE_ARCH
{
GetEmitter()->emitIns_C_R(ins, size, tree->AsClsVar()->gtClsVarHnd, reg, offs);
}
return;
case GT_IND:
case GT_NULLCHECK:
case GT_ARR_ELEM:
{
assert(!"inst_TT_RV not supported for GT_IND, GT_NULLCHECK or GT_ARR_ELEM");
}
break;
case GT_COMMA:
// tree->AsOp()->gtOp1 - already processed by genCreateAddrMode()
tree = tree->AsOp()->gtOp2;
goto AGAIN;
default:
assert(!"invalid address");
}
}
/*****************************************************************************
*
* Generate an instruction that has one operand given by a register and the
* other one by a tree (which has been made addressable).
*/
void CodeGen::inst_RV_TT(instruction ins,
regNumber reg,
GenTree* tree,
unsigned offs,
emitAttr size,
insFlags flags /* = INS_FLAGS_DONT_CARE */)
{
assert(reg != REG_STK);
if (size == EA_UNKNOWN)
{
if (!instIsFP(ins))
{
size = emitTypeSize(tree->TypeGet());
}
else
{
size = EA_ATTR(genTypeSize(tree->TypeGet()));
}
}
#ifdef _TARGET_XARCH_
#ifdef DEBUG
// If it is a GC type and the result is not, then either
// 1) it is an LEA
// 2) optOptimizeBools() optimized if (ref != 0 && ref != 0) to if (ref & ref)
// 3) optOptimizeBools() optimized if (ref == 0 || ref == 0) to if (ref | ref)
// 4) byref - byref = int
if (tree->gtType == TYP_REF && !EA_IS_GCREF(size))
{
assert((EA_IS_BYREF(size) && ins == INS_add) || (ins == INS_lea || ins == INS_and || ins == INS_or));
}
if (tree->gtType == TYP_BYREF && !EA_IS_BYREF(size))
{
assert(ins == INS_lea || ins == INS_and || ins == INS_or || ins == INS_sub);
}
#endif
#endif
#if CPU_LOAD_STORE_ARCH
if (ins == INS_mov)
{
#if defined(_TARGET_ARM64_) || defined(_TARGET_ARM64_)
ins = ins_Move_Extend(tree->TypeGet(), false);
#else
NYI("CodeGen::inst_RV_TT with INS_mov");
#endif
}
#endif // CPU_LOAD_STORE_ARCH
AGAIN:
/* Is this a spilled value? */
if (tree->gtFlags & GTF_SPILLED)
{
assert(!"ISSUE: If this can happen, we need to generate 'ins [ebp+spill]'");
}
switch (tree->gtOper)
{
unsigned varNum;
case GT_LCL_VAR:
case GT_LCL_VAR_ADDR:
inst_set_SV_var(tree);
goto LCL;
case GT_LCL_FLD_ADDR:
case GT_LCL_FLD:
offs += tree->AsLclFld()->gtLclOffs;
goto LCL;
LCL:
varNum = tree->AsLclVarCommon()->GetLclNum();
assert(varNum < compiler->lvaCount);
#ifdef _TARGET_ARM_
switch (ins)
{
case INS_mov:
ins = ins_Load(tree->TypeGet());
__fallthrough;
case INS_lea:
case INS_ldr:
case INS_ldrh:
case INS_ldrb:
case INS_ldrsh:
case INS_ldrsb:
case INS_vldr:
assert(flags != INS_FLAGS_SET);
GetEmitter()->emitIns_R_S(ins, size, reg, varNum, offs);
return;
default:
regNumber regTmp;
regTmp = tree->GetRegNum();
GetEmitter()->emitIns_R_S(ins_Load(tree->TypeGet()), size, regTmp, varNum, offs);
GetEmitter()->emitIns_R_R(ins, size, reg, regTmp, flags);
regSet.verifyRegUsed(regTmp);
return;
}
#else // !_TARGET_ARM_
GetEmitter()->emitIns_R_S(ins, size, reg, varNum, offs);
return;
#endif // !_TARGET_ARM_
case GT_CLS_VAR:
// Make sure FP instruction size matches the operand size
// (We optimized constant doubles to floats when we can, just want to
// make sure that we don't mistakenly use 8 bytes when the
// constant.
assert(!isFloatRegType(tree->gtType) || genTypeSize(tree->gtType) == EA_SIZE_IN_BYTES(size));
#if CPU_LOAD_STORE_ARCH
assert(!"GT_CLS_VAR not supported in ARM backend");
#else // CPU_LOAD_STORE_ARCH
GetEmitter()->emitIns_R_C(ins, size, reg, tree->AsClsVar()->gtClsVarHnd, offs);
#endif // CPU_LOAD_STORE_ARCH
return;
case GT_IND:
case GT_NULLCHECK:
case GT_ARR_ELEM:
case GT_LEA:
{
assert(!"inst_RV_TT not supported for GT_IND, GT_NULLCHECK, GT_ARR_ELEM or GT_LEA");
}
break;
case GT_CNS_INT:
assert(offs == 0);
// TODO-CrossBitness: we wouldn't need the cast below if GenTreeIntCon::gtIconVal had target_ssize_t type.
inst_RV_IV(ins, reg, (target_ssize_t)tree->AsIntCon()->gtIconVal, emitActualTypeSize(tree->TypeGet()),
flags);
break;
case GT_CNS_LNG:
assert(size == EA_4BYTE || size == EA_8BYTE);
#ifdef _TARGET_AMD64_
assert(offs == 0);
#endif // _TARGET_AMD64_
target_ssize_t constVal;
emitAttr size;
if (offs == 0)
{
constVal = (target_ssize_t)(tree->AsLngCon()->gtLconVal);
size = EA_PTRSIZE;
}
else
{
constVal = (target_ssize_t)(tree->AsLngCon()->gtLconVal >> 32);
size = EA_4BYTE;
}
inst_RV_IV(ins, reg, constVal, size, flags);
break;
case GT_COMMA:
tree = tree->AsOp()->gtOp2;
goto AGAIN;
default:
assert(!"invalid address");
}
}
/*****************************************************************************
*
* Generate a "shift reg, icon" instruction.
*/
void CodeGen::inst_RV_SH(
instruction ins, emitAttr size, regNumber reg, unsigned val, insFlags flags /* = INS_FLAGS_DONT_CARE */)
{
#if defined(_TARGET_ARM_)
if (val >= 32)
val &= 0x1f;
GetEmitter()->emitIns_R_I(ins, size, reg, val, flags);
#elif defined(_TARGET_XARCH_)
#ifdef _TARGET_AMD64_
// X64 JB BE insures only encodable values make it here.
// x86 can encode 8 bits, though it masks down to 5 or 6
// depending on 32-bit or 64-bit registers are used.
// Here we will allow anything that is encodable.
assert(val < 256);
#endif
ins = genMapShiftInsToShiftByConstantIns(ins, val);
if (val == 1)
{
GetEmitter()->emitIns_R(ins, size, reg);
}
else
{
GetEmitter()->emitIns_R_I(ins, size, reg, val);
}
#else
NYI("inst_RV_SH - unknown target");
#endif // _TARGET_*
}
/*****************************************************************************
*
* Generate a "shift [r/m], icon" instruction.
*/
void CodeGen::inst_TT_SH(instruction ins, GenTree* tree, unsigned val, unsigned offs)
{
#ifdef _TARGET_XARCH_
if (val == 0)
{
// Shift by 0 - why are you wasting our precious time????
return;