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
/
emit.h
2766 lines (2230 loc) · 85.1 KB
/
emit.h
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.
/*****************************************************************************/
#ifndef _EMIT_H_
#define _EMIT_H_
#include "instr.h"
#ifndef _GCINFO_H_
#include "gcinfo.h"
#endif
#include "jitgcinfo.h"
/*****************************************************************************/
#ifdef _MSC_VER
#pragma warning(disable : 4200) // allow arrays of 0 size inside structs
#endif
/*****************************************************************************/
#if 0
#define EMITVERBOSE 1
#else
#define EMITVERBOSE (emitComp->verbose)
#endif
#if 0
#define EMIT_GC_VERBOSE 0
#else
#define EMIT_GC_VERBOSE (emitComp->verbose)
#endif
#if 1
#define EMIT_INSTLIST_VERBOSE 0
#else
#define EMIT_INSTLIST_VERBOSE (emitComp->verbose)
#endif
/*****************************************************************************/
#ifdef DEBUG
#define DEBUG_EMIT 1
#else
#define DEBUG_EMIT 0
#endif
#if EMITTER_STATS
void emitterStats(FILE* fout);
void emitterStaticStats(FILE* fout); // Static stats about the emitter (data structure offsets, sizes, etc.)
#endif
void printRegMaskInt(regMaskTP mask);
/*****************************************************************************/
/* Forward declarations */
class emitLocation;
class emitter;
struct insGroup;
typedef void (*emitSplitCallbackType)(void* context, emitLocation* emitLoc);
/*****************************************************************************/
//-----------------------------------------------------------------------------
inline bool needsGC(GCtype gcType)
{
if (gcType == GCT_NONE)
{
return false;
}
else
{
assert(gcType == GCT_GCREF || gcType == GCT_BYREF);
return true;
}
}
//-----------------------------------------------------------------------------
#ifdef DEBUG
inline bool IsValidGCtype(GCtype gcType)
{
return (gcType == GCT_NONE || gcType == GCT_GCREF || gcType == GCT_BYREF);
}
// Get a string name to represent the GC type
inline const char* GCtypeStr(GCtype gcType)
{
switch (gcType)
{
case GCT_NONE:
return "npt";
case GCT_GCREF:
return "gcr";
case GCT_BYREF:
return "byr";
default:
assert(!"Invalid GCtype");
return "err";
}
}
#endif // DEBUG
/*****************************************************************************/
#if DEBUG_EMIT
#define INTERESTING_JUMP_NUM -1 // set to 0 to see all jump info
//#define INTERESTING_JUMP_NUM 0
#endif
/*****************************************************************************
*
* Represent an emitter location.
*/
class emitLocation
{
public:
emitLocation() : ig(nullptr), codePos(0)
{
}
emitLocation(insGroup* _ig) : ig(_ig), codePos(0)
{
}
emitLocation(void* emitCookie) : ig((insGroup*)emitCookie), codePos(0)
{
}
// A constructor for code that needs to call it explicitly.
void Init()
{
*this = emitLocation();
}
void CaptureLocation(emitter* emit);
bool IsCurrentLocation(emitter* emit) const;
// This function is highly suspect, since it presumes knowledge of the codePos "cookie",
// and doesn't look at the 'ig' pointer.
bool IsOffsetZero() const
{
return (codePos == 0);
}
UNATIVE_OFFSET CodeOffset(emitter* emit) const;
insGroup* GetIG() const
{
return ig;
}
int GetInsNum() const;
bool operator!=(const emitLocation& other) const
{
return (ig != other.ig) || (codePos != other.codePos);
}
bool operator==(const emitLocation& other) const
{
return !(*this != other);
}
bool Valid() const
{
// Things we could validate:
// 1. the instruction group pointer is non-nullptr.
// 2. 'ig' is a legal pointer to an instruction group.
// 3. 'codePos' is a legal offset into 'ig'.
// Currently, we just do #1.
// #2 and #3 should only be done in DEBUG, if they are implemented.
if (ig == nullptr)
{
return false;
}
return true;
}
UNATIVE_OFFSET GetFuncletPrologOffset(emitter* emit) const;
#ifdef DEBUG
void Print() const;
#endif // DEBUG
private:
insGroup* ig; // the instruction group
unsigned codePos; // the code position within the IG (see emitCurOffset())
};
enum class emitDataAlignment
{
None,
Preferred,
Required
};
/************************************************************************/
/* The following describes an instruction group */
/************************************************************************/
enum insGroupPlaceholderType : unsigned char
{
IGPT_PROLOG, // currently unused
IGPT_EPILOG,
#if defined(FEATURE_EH_FUNCLETS)
IGPT_FUNCLET_PROLOG,
IGPT_FUNCLET_EPILOG,
#endif // FEATURE_EH_FUNCLETS
};
#if defined(_MSC_VER) && defined(_TARGET_ARM_)
// ARM aligns structures that contain 64-bit ints or doubles on 64-bit boundaries. This causes unwanted
// padding to be added to the end, so sizeof() is unnecessarily big.
#pragma pack(push)
#pragma pack(4)
#endif // defined(_MSC_VER) && defined(_TARGET_ARM_)
struct insPlaceholderGroupData
{
insGroup* igPhNext;
BasicBlock* igPhBB;
VARSET_TP igPhInitGCrefVars;
regMaskTP igPhInitGCrefRegs;
regMaskTP igPhInitByrefRegs;
VARSET_TP igPhPrevGCrefVars;
regMaskTP igPhPrevGCrefRegs;
regMaskTP igPhPrevByrefRegs;
insGroupPlaceholderType igPhType;
}; // end of struct insPlaceholderGroupData
struct insGroup
{
insGroup* igNext;
#ifdef DEBUG
insGroup* igSelf; // for consistency checking
#endif
#if defined(DEBUG) || defined(LATE_DISASM)
BasicBlock::weight_t igWeight; // the block weight used for this insGroup
#endif
UNATIVE_OFFSET igNum; // for ordering (and display) purposes
UNATIVE_OFFSET igOffs; // offset of this group within method
unsigned int igFuncIdx; // Which function/funclet does this belong to? (Index into Compiler::compFuncInfos array.)
unsigned short igFlags; // see IGF_xxx below
unsigned short igSize; // # of bytes of code in this group
#define IGF_GC_VARS 0x0001 // new set of live GC ref variables
#define IGF_BYREF_REGS 0x0002 // new set of live by-ref registers
#if defined(FEATURE_EH_FUNCLETS) && defined(_TARGET_ARM_)
#define IGF_FINALLY_TARGET 0x0004 // this group is the start of a basic block that is returned to after a finally.
#endif // defined(FEATURE_EH_FUNCLETS) && defined(_TARGET_ARM_)
#define IGF_FUNCLET_PROLOG 0x0008 // this group belongs to a funclet prolog
#define IGF_FUNCLET_EPILOG 0x0010 // this group belongs to a funclet epilog.
#define IGF_EPILOG 0x0020 // this group belongs to a main function epilog
#define IGF_NOGCINTERRUPT 0x0040 // this IG is is a no-interrupt region (prolog, epilog, etc.)
#define IGF_UPD_ISZ 0x0080 // some instruction sizes updated
#define IGF_PLACEHOLDER 0x0100 // this is a placeholder group, to be filled in later
#define IGF_EXTEND 0x0200 // this block is conceptually an extension of the previous block
// and the emitter should continue to track GC info as if there was no new block.
// Mask of IGF_* flags that should be propagated to new blocks when they are created.
// This allows prologs and epilogs to be any number of IGs, but still be
// automatically marked properly.
#if defined(FEATURE_EH_FUNCLETS)
#ifdef DEBUG
#define IGF_PROPAGATE_MASK (IGF_EPILOG | IGF_FUNCLET_PROLOG | IGF_FUNCLET_EPILOG)
#else // DEBUG
#define IGF_PROPAGATE_MASK (IGF_EPILOG | IGF_FUNCLET_PROLOG)
#endif // DEBUG
#else // !FEATURE_EH_FUNCLETS
#define IGF_PROPAGATE_MASK (IGF_EPILOG)
#endif // !FEATURE_EH_FUNCLETS
// Try to do better packing based on how large regMaskSmall is (8, 16, or 64 bits).
CLANG_FORMAT_COMMENT_ANCHOR;
#if REGMASK_BITS <= 32
union {
BYTE* igData; // addr of instruction descriptors
insPlaceholderGroupData* igPhData; // when igFlags & IGF_PLACEHOLDER
};
#if EMIT_TRACK_STACK_DEPTH
unsigned igStkLvl; // stack level on entry
#endif
regMaskSmall igGCregs; // set of registers with live GC refs
unsigned char igInsCnt; // # of instructions in this group
#else // REGMASK_BITS
regMaskSmall igGCregs; // set of registers with live GC refs
union {
BYTE* igData; // addr of instruction descriptors
insPlaceholderGroupData* igPhData; // when igFlags & IGF_PLACEHOLDER
};
#if EMIT_TRACK_STACK_DEPTH
unsigned igStkLvl; // stack level on entry
#endif
unsigned char igInsCnt; // # of instructions in this group
#endif // REGMASK_BITS
VARSET_VALRET_TP igGCvars() const
{
assert(igFlags & IGF_GC_VARS);
BYTE* ptr = (BYTE*)igData;
ptr -= sizeof(VARSET_TP);
return *(VARSET_TP*)ptr;
}
unsigned igByrefRegs() const
{
assert(igFlags & IGF_BYREF_REGS);
BYTE* ptr = (BYTE*)igData;
if (igFlags & IGF_GC_VARS)
{
ptr -= sizeof(VARSET_TP);
}
ptr -= sizeof(unsigned);
return *(unsigned*)ptr;
}
}; // end of struct insGroup
// For AMD64 the maximum prolog/epilog size supported on the OS is 256 bytes
// Since it is incorrect for us to be jumping across funclet prolog/epilogs
// we will use the following estimate as the maximum placeholder size.
//
#define MAX_PLACEHOLDER_IG_SIZE 256
#if defined(_MSC_VER) && defined(_TARGET_ARM_)
#pragma pack(pop)
#endif // defined(_MSC_VER) && defined(_TARGET_ARM_)
/*****************************************************************************/
#define DEFINE_ID_OPS
#include "emitfmts.h"
#undef DEFINE_ID_OPS
enum LclVarAddrTag
{
LVA_STANDARD_ENCODING = 0,
LVA_LARGE_OFFSET = 1,
LVA_COMPILER_TEMP = 2,
LVA_LARGE_VARNUM = 3
};
struct emitLclVarAddr
{
// Constructor
void initLclVarAddr(int varNum, unsigned offset);
int lvaVarNum(); // Returns the variable to access. Note that it returns a negative number for compiler spill temps.
unsigned lvaOffset(); // returns the offset into the variable to access
// This struct should be 32 bits in size for the release build.
// We have this constraint because this type is used in a union
// with several other pointer sized types in the instrDesc struct.
//
protected:
unsigned _lvaVarNum : 15; // Usually the lvaVarNum
unsigned _lvaExtra : 15; // Usually the lvaOffset
unsigned _lvaTag : 2; // tag field to support larger varnums
};
enum idAddrUnionTag
{
iaut_ALIGNED_POINTER = 0x0,
iaut_DATA_OFFSET = 0x1,
iaut_INST_COUNT = 0x2,
iaut_UNUSED_TAG = 0x3,
iaut_MASK = 0x3,
iaut_SHIFT = 2
};
class emitter
{
friend class emitLocation;
friend class Compiler;
friend class CodeGen;
friend class CodeGenInterface;
public:
/*************************************************************************
*
* Define the public entry points.
*/
// Constructor.
emitter()
{
#ifdef DEBUG
// There seem to be some cases where this is used without being initialized via CodeGen::inst_set_SV_var().
emitVarRefOffs = 0;
#endif // DEBUG
#ifdef _TARGET_XARCH_
SetUseVEXEncoding(false);
#endif // _TARGET_XARCH_
emitDataSecCur = nullptr;
}
#include "emitpub.h"
protected:
/************************************************************************/
/* Miscellaneous stuff */
/************************************************************************/
Compiler* emitComp;
GCInfo* gcInfo;
CodeGen* codeGen;
typedef GCInfo::varPtrDsc varPtrDsc;
typedef GCInfo::regPtrDsc regPtrDsc;
typedef GCInfo::CallDsc callDsc;
void* emitGetMem(size_t sz);
enum opSize : unsigned
{
OPSZ1 = 0,
OPSZ2 = 1,
OPSZ4 = 2,
OPSZ8 = 3,
OPSZ16 = 4,
OPSZ32 = 5,
OPSZ_COUNT = 6,
#ifdef _TARGET_AMD64_
OPSZP = OPSZ8,
#else
OPSZP = OPSZ4,
#endif
};
#define OPSIZE_INVALID ((opSize)0xffff)
static const emitter::opSize emitSizeEncode[];
static const emitAttr emitSizeDecode[];
static emitter::opSize emitEncodeSize(emitAttr size);
static emitAttr emitDecodeSize(emitter::opSize ensz);
// Currently, we only allow one IG for the prolog
bool emitIGisInProlog(const insGroup* ig)
{
return ig == emitPrologIG;
}
bool emitIGisInEpilog(const insGroup* ig)
{
return (ig != nullptr) && ((ig->igFlags & IGF_EPILOG) != 0);
}
#if defined(FEATURE_EH_FUNCLETS)
bool emitIGisInFuncletProlog(const insGroup* ig)
{
return (ig != nullptr) && ((ig->igFlags & IGF_FUNCLET_PROLOG) != 0);
}
bool emitIGisInFuncletEpilog(const insGroup* ig)
{
return (ig != nullptr) && ((ig->igFlags & IGF_FUNCLET_EPILOG) != 0);
}
#endif // FEATURE_EH_FUNCLETS
// If "ig" corresponds to the start of a basic block that is the
// target of a funclet return, generate GC information for it's start
// address "cp", as if it were the return address of a call.
void emitGenGCInfoIfFuncletRetTarget(insGroup* ig, BYTE* cp);
void emitRecomputeIGoffsets();
/************************************************************************/
/* The following describes a single instruction */
/************************************************************************/
enum insFormat : unsigned
{
#define IF_DEF(en, op1, op2) IF_##en,
#include "emitfmts.h"
IF_COUNT
};
#define AM_DISP_BITS ((sizeof(unsigned) * 8) - 2 * (REGNUM_BITS + 1) - 2)
#define AM_DISP_BIG_VAL (-(1 << (AM_DISP_BITS - 1)))
#define AM_DISP_MIN (-((1 << (AM_DISP_BITS - 1)) - 1))
#define AM_DISP_MAX (+((1 << (AM_DISP_BITS - 1)) - 1))
struct emitAddrMode
{
regNumber amBaseReg : REGNUM_BITS + 1;
regNumber amIndxReg : REGNUM_BITS + 1;
emitter::opSize amScale : 2;
int amDisp : AM_DISP_BITS;
};
#ifdef DEBUG // This information is used in DEBUG builds to display the method name for call instructions
struct instrDesc;
struct instrDescDebugInfo
{
unsigned idNum;
size_t idSize; // size of the instruction descriptor
unsigned idVarRefOffs; // IL offset for LclVar reference
size_t idMemCookie; // for display of method name (also used by switch table)
bool idFinallyCall; // Branch instruction is a call to finally
bool idCatchRet; // Instruction is for a catch 'return'
CORINFO_SIG_INFO* idCallSig; // Used to report native call site signatures to the EE
};
#endif // DEBUG
#ifdef _TARGET_ARM_
unsigned insEncodeSetFlags(insFlags sf);
enum insSize : unsigned
{
ISZ_16BIT,
ISZ_32BIT,
ISZ_48BIT // pseudo-instruction for conditional branch with imm24 range,
// encoded as IT of condition followed by an unconditional branch
};
unsigned insEncodeShiftOpts(insOpts opt);
unsigned insEncodePUW_G0(insOpts opt, int imm);
unsigned insEncodePUW_H0(insOpts opt, int imm);
#endif // _TARGET_ARM_
struct instrDescCns;
struct instrDesc
{
private:
// The assembly instruction
#if defined(_TARGET_XARCH_)
static_assert_no_msg(INS_count <= 1024);
instruction _idIns : 10;
#elif defined(_TARGET_ARM64_)
static_assert_no_msg(INS_count <= 512);
instruction _idIns : 9;
#else // !(defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_))
static_assert_no_msg(INS_count <= 256);
instruction _idIns : 8;
#endif // !(defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_))
// The format for the instruction
#if defined(_TARGET_XARCH_)
static_assert_no_msg(IF_COUNT <= 128);
insFormat _idInsFmt : 7;
#else
static_assert_no_msg(IF_COUNT <= 256);
insFormat _idInsFmt : 8;
#endif
public:
instruction idIns() const
{
return _idIns;
}
void idIns(instruction ins)
{
assert((ins != INS_invalid) && (ins < INS_count));
_idIns = ins;
}
insFormat idInsFmt() const
{
return _idInsFmt;
}
void idInsFmt(insFormat insFmt)
{
#if defined(_TARGET_ARM64_)
noway_assert(insFmt != IF_NONE); // Only the x86 emitter uses IF_NONE, it is invalid for ARM64 (and ARM32)
#endif
assert(insFmt < IF_COUNT);
_idInsFmt = insFmt;
}
void idSetRelocFlags(emitAttr attr)
{
_idCnsReloc = (EA_IS_CNS_RELOC(attr) ? 1 : 0);
_idDspReloc = (EA_IS_DSP_RELOC(attr) ? 1 : 0);
}
////////////////////////////////////////////////////////////////////////
// Space taken up to here:
// x86: 17 bits
// amd64: 17 bits
// arm: 16 bits
// arm64: 17 bits
private:
#if defined(_TARGET_XARCH_)
unsigned _idCodeSize : 4; // size of instruction in bytes. Max size of an Intel instruction is 15 bytes.
opSize _idOpSize : 3; // operand size: 0=1 , 1=2 , 2=4 , 3=8, 4=16, 5=32
// At this point we have fully consumed first DWORD so that next field
// doesn't cross a byte boundary.
#elif defined(_TARGET_ARM64_)
// Moved the definition of '_idOpSize' later so that we don't cross a 32-bit boundary when laying out bitfields
#else // ARM
opSize _idOpSize : 2; // operand size: 0=1 , 1=2 , 2=4 , 3=8
#endif // ARM
// On Amd64, this is where the second DWORD begins
// On System V a call could return a struct in 2 registers. The instrDescCGCA struct below has member that
// stores the GC-ness of the second register.
// It is added to the instrDescCGCA and not here (the base struct) since it is not needed by all the
// instructions. This struct (instrDesc) is very carefully kept to be no more than 128 bytes. There is no more
// space to add members for keeping GC-ness of the second return registers. It will also bloat the base struct
// unnecessarily since the GC-ness of the second register is only needed for call instructions.
// The instrDescCGCA struct's member keeping the GC-ness of the first return register is _idcSecondRetRegGCType.
GCtype _idGCref : 2; // GCref operand? (value is a "GCtype")
// The idReg1 and idReg2 fields hold the first and second register
// operand(s), whenever these are present. Note that currently the
// size of these fields is 6 bits on all targets, and care needs to
// be taken to make sure all of these fields stay reasonably packed.
// Note that we use the _idReg1 and _idReg2 fields to hold
// the live gcrefReg mask for the call instructions on x86/x64
//
regNumber _idReg1 : REGNUM_BITS; // register num
regNumber _idReg2 : REGNUM_BITS;
////////////////////////////////////////////////////////////////////////
// Space taken up to here:
// x86: 38 bits
// amd64: 38 bits
// arm: 32 bits
// arm64: 31 bits
CLANG_FORMAT_COMMENT_ANCHOR;
unsigned _idSmallDsc : 1; // is this a "small" descriptor?
unsigned _idLargeCns : 1; // does a large constant follow?
unsigned _idLargeDsp : 1; // does a large displacement follow?
unsigned _idLargeCall : 1; // large call descriptor used
unsigned _idBound : 1; // jump target / frame offset bound
unsigned _idCallRegPtr : 1; // IL indirect calls: addr in reg
unsigned _idCallAddr : 1; // IL indirect calls: can make a direct call to iiaAddr
unsigned _idNoGC : 1; // Some helpers don't get recorded in GC tables
#ifdef _TARGET_ARM64_
opSize _idOpSize : 3; // operand size: 0=1 , 1=2 , 2=4 , 3=8, 4=16
insOpts _idInsOpt : 6; // options for instructions
unsigned _idLclVar : 1; // access a local on stack
#endif
#ifdef _TARGET_ARM_
insSize _idInsSize : 2; // size of instruction: 16, 32 or 48 bits
insFlags _idInsFlags : 1; // will this instruction set the flags
unsigned _idLclVar : 1; // access a local on stack
unsigned _idLclFPBase : 1; // access a local on stack - SP based offset
insOpts _idInsOpt : 3; // options for Load/Store instructions
// For arm we have used 16 bits
#define ID_EXTRA_BITFIELD_BITS (16)
#elif defined(_TARGET_ARM64_)
// For Arm64, we have used 17 bits from the second DWORD.
#define ID_EXTRA_BITFIELD_BITS (17)
#elif defined(_TARGET_XARCH_)
// For xarch, we have used 14 bits from the second DWORD.
#define ID_EXTRA_BITFIELD_BITS (14)
#else
#error Unsupported or unset target architecture
#endif
////////////////////////////////////////////////////////////////////////
// Space taken up to here:
// x86: 46 bits
// amd64: 46 bits
// arm: 48 bits
// arm64: 49 bits
unsigned _idCnsReloc : 1; // LargeCns is an RVA and needs reloc tag
unsigned _idDspReloc : 1; // LargeDsp is an RVA and needs reloc tag
#define ID_EXTRA_RELOC_BITS (2)
////////////////////////////////////////////////////////////////////////
// Space taken up to here:
// x86: 48 bits
// amd64: 48 bits
// arm: 50 bits
// arm64: 51 bits
CLANG_FORMAT_COMMENT_ANCHOR;
#define ID_EXTRA_BITS (ID_EXTRA_RELOC_BITS + ID_EXTRA_BITFIELD_BITS)
/* Use whatever bits are left over for small constants */
#define ID_BIT_SMALL_CNS (32 - ID_EXTRA_BITS)
#define ID_MIN_SMALL_CNS 0
#define ID_MAX_SMALL_CNS (int)((1 << ID_BIT_SMALL_CNS) - 1U)
////////////////////////////////////////////////////////////////////////
// Small constant size:
// x86: 16 bits
// amd64: 16 bits
// arm: 14 bits
// arm64: 13 bits
unsigned _idSmallCns : ID_BIT_SMALL_CNS;
////////////////////////////////////////////////////////////////////////
// Space taken up to here: 64 bits, all architectures, by design.
////////////////////////////////////////////////////////////////////////
CLANG_FORMAT_COMMENT_ANCHOR;
#ifdef DEBUG
instrDescDebugInfo* _idDebugOnlyInfo;
public:
instrDescDebugInfo* idDebugOnlyInfo() const
{
return _idDebugOnlyInfo;
}
void idDebugOnlyInfo(instrDescDebugInfo* info)
{
_idDebugOnlyInfo = info;
}
private:
#endif // DEBUG
CLANG_FORMAT_COMMENT_ANCHOR;
//
// This is the end of the 'small' instrDesc which is the same on all
// platforms (except 64-bit DEBUG which is a little bigger).
// Non-DEBUG sizes:
// x86/amd64/arm/arm64: 64 bits
// DEBUG sizes (includes one pointer):
// x86: 2 DWORDs, 96 bits
// amd64: 4 DWORDs, 128 bits
// arm: 3 DWORDs, 96 bits
// arm64: 4 DWORDs, 128 bits
// There should no padding or alignment issues on any platform or
// configuration (including DEBUG which has 1 extra pointer).
//
/*
If you add lots more fields that need to be cleared (such
as various flags), you might need to update the body of
emitter::emitAllocInstr() to clear them.
*/
#if DEBUG
#define SMALL_IDSC_DEBUG_EXTRA (sizeof(void*))
#else
#define SMALL_IDSC_DEBUG_EXTRA (0)
#endif
#define SMALL_IDSC_SIZE (8 + SMALL_IDSC_DEBUG_EXTRA)
void checkSizes();
union idAddrUnion {
// TODO-Cleanup: We should really add a DEBUG-only tag to this union so we can add asserts
// about reading what we think is here, to avoid unexpected corruption issues.
#ifndef _TARGET_ARM64_
emitLclVarAddr iiaLclVar;
#endif
BasicBlock* iiaBBlabel;
insGroup* iiaIGlabel;
BYTE* iiaAddr;
emitAddrMode iiaAddrMode;
CORINFO_FIELD_HANDLE iiaFieldHnd; // iiaFieldHandle is also used to encode
// an offset into the JIT data constant area
bool iiaIsJitDataOffset() const;
int iiaGetJitDataOffset() const;
#ifdef _TARGET_ARMARCH_
// iiaEncodedInstrCount and its accessor functions are used to specify an instruction
// count for jumps, instead of using a label and multiple blocks. This is used in the
// prolog as well as for IF_LARGEJMP pseudo-branch instructions.
int iiaEncodedInstrCount;
bool iiaHasInstrCount() const
{
return (iiaEncodedInstrCount & iaut_MASK) == iaut_INST_COUNT;
}
int iiaGetInstrCount() const
{
assert(iiaHasInstrCount());
return (iiaEncodedInstrCount >> iaut_SHIFT);
}
void iiaSetInstrCount(int count)
{
assert(abs(count) < 10);
iiaEncodedInstrCount = (count << iaut_SHIFT) | iaut_INST_COUNT;
}
struct
{
#ifdef _TARGET_ARM64_
// For 64-bit architecture this 32-bit structure can pack with these unsigned bit fields
emitLclVarAddr iiaLclVar;
unsigned _idReg3Scaled : 1; // Reg3 is scaled by idOpSize bits
GCtype _idGCref2 : 2;
#endif
regNumber _idReg3 : REGNUM_BITS;
regNumber _idReg4 : REGNUM_BITS;
};
#elif defined(_TARGET_XARCH_)
struct
{
regNumber _idReg3 : REGNUM_BITS;
regNumber _idReg4 : REGNUM_BITS;
};
#endif // defined(_TARGET_XARCH_)
} _idAddrUnion;
/* Trivial wrappers to return properly typed enums */
public:
bool idIsSmallDsc() const
{
return (_idSmallDsc != 0);
}
void idSetIsSmallDsc()
{
_idSmallDsc = 1;
}
#if defined(_TARGET_XARCH_)
unsigned idCodeSize() const
{
return _idCodeSize;
}
void idCodeSize(unsigned sz)
{
if (sz > 15)
{
// This is a temporary workaround for non-precise instr size
// estimator on XARCH. It often overestimates sizes and can
// return value more than 15 that doesn't fit in 4 bits _idCodeSize.
// If somehow we generate instruction that needs more than 15 bytes we
// will fail on another assert in emit.cpp: noway_assert(id->idCodeSize() >= csz).
// Issue https://github.com/dotnet/coreclr/issues/25050.
sz = 15;
}
assert(sz <= 15); // Intel decoder limit.
_idCodeSize = sz;
assert(sz == _idCodeSize);
}
#elif defined(_TARGET_ARM64_)
unsigned idCodeSize() const
{
int size = 4;
switch (idInsFmt())
{
case IF_LARGEADR:
// adrp + add
case IF_LARGEJMP:
// b<cond> + b<uncond>
size = 8;
break;
case IF_LARGELDC:
if (isVectorRegister(idReg1()))
{
// adrp + ldr + fmov
size = 12;
}
else
{
// adrp + ldr
size = 8;
}
break;
default:
break;
}
return size;
}
#elif defined(_TARGET_ARM_)
bool idInstrIsT1() const
{
return (_idInsSize == ISZ_16BIT);
}
unsigned idCodeSize() const
{
unsigned result = (_idInsSize == ISZ_16BIT) ? 2 : (_idInsSize == ISZ_32BIT) ? 4 : 6;
return result;
}
insSize idInsSize() const
{
return _idInsSize;
}
void idInsSize(insSize isz)
{
_idInsSize = isz;
assert(isz == _idInsSize);
}
insFlags idInsFlags() const
{
return _idInsFlags;
}
void idInsFlags(insFlags sf)
{
_idInsFlags = sf;
assert(sf == _idInsFlags);
}
#endif // _TARGET_ARM_
emitAttr idOpSize()
{
return emitDecodeSize(_idOpSize);
}
void idOpSize(emitAttr opsz)
{
_idOpSize = emitEncodeSize(opsz);
}
GCtype idGCref() const
{
return (GCtype)_idGCref;
}
void idGCref(GCtype gctype)
{
_idGCref = gctype;
}
regNumber idReg1() const
{
return _idReg1;
}
void idReg1(regNumber reg)
{
_idReg1 = reg;
assert(reg == _idReg1);
}
#ifdef _TARGET_ARM64_
GCtype idGCrefReg2() const
{
assert(!idIsSmallDsc());
return (GCtype)idAddr()->_idGCref2;
}
void idGCrefReg2(GCtype gctype)
{
assert(!idIsSmallDsc());
idAddr()->_idGCref2 = gctype;
}
#endif // _TARGET_ARM64_
regNumber idReg2() const
{
return _idReg2;
}
void idReg2(regNumber reg)
{
_idReg2 = reg;
assert(reg == _idReg2);
}
#if defined(_TARGET_XARCH_)