forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemit.h
4307 lines (3615 loc) · 136 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.
/*****************************************************************************/
#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 TARGET_XARCH
#define EMIT_BACKWARDS_NAVIGATION 1 // If 1, enable backwards navigation code for MIR (insGroup/instrDesc).
#else
#define EMIT_BACKWARDS_NAVIGATION 0
#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(insGroup* _ig, unsigned _codePos)
{
SetLocation(_ig, _codePos);
}
emitLocation(emitter* emit)
{
CaptureLocation(emit);
}
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);
void SetLocation(insGroup* _ig, unsigned _codePos);
void SetLocation(emitLocation newLocation);
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;
int GetInsOffset() 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;
bool IsPreviousInsNum(emitter* emit) const;
#ifdef DEBUG
void Print(LONG compMethodID) const;
#endif // DEBUG
private:
insGroup* ig; // the instruction group
unsigned codePos; // the code position within the IG (see emitCurOffset())
};
/************************************************************************/
/* The following describes an instruction group */
/************************************************************************/
enum insGroupPlaceholderType : unsigned char
{
IGPT_PROLOG, // currently unused
IGPT_EPILOG,
IGPT_FUNCLET_PROLOG,
IGPT_FUNCLET_EPILOG,
};
#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;
#if EMIT_BACKWARDS_NAVIGATION
insGroup* igPrev;
#endif
#ifdef DEBUG
insGroup* igSelf; // for consistency checking
#endif
#if defined(DEBUG) || defined(LATE_DISASM)
weight_t igWeight; // the block weight used for this insGroup
double igPerfScore; // The PerfScore for this insGroup
#endif
#ifdef DEBUG
BasicBlock* lastGeneratedBlock; // The last block that generated code into this insGroup.
jitstd::list<BasicBlock*> igBlocks; // All the blocks that generated code into this insGroup.
size_t igDataSize; // size of instrDesc data pointed to by 'igData'
#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
#if FEATURE_LOOP_ALIGN
insGroup* igLoopBackEdge; // "last" back-edge that branches back to an aligned loop head.
#endif
#define IGF_GC_VARS 0x0001 // new set of live GC ref variables
#define IGF_BYREF_REGS 0x0002 // new set of live by-ref registers
#define IGF_FUNCLET_PROLOG 0x0004 // this group belongs to a funclet prolog
#define IGF_FUNCLET_EPILOG 0x0008 // this group belongs to a funclet epilog.
#define IGF_EPILOG 0x0010 // this group belongs to a main function epilog
#define IGF_NOGCINTERRUPT 0x0020 // this IG is in a no-interrupt region (prolog, epilog, etc.)
#define IGF_UPD_ISZ 0x0040 // some instruction sizes updated
#define IGF_PLACEHOLDER 0x0080 // this is a placeholder group, to be filled in later
#define IGF_EXTEND \
0x0100 // 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.
#define IGF_HAS_ALIGN \
0x0200 // this group contains an alignment instruction(s) at the end to align either the next
// IG, or, if this IG contains with an unconditional branch, some subsequent IG.
#define IGF_REMOVED_ALIGN \
0x0400 // IG was marked as having an alignment instruction(s), but was later unmarked
// without updating the IG's size/offsets.
#define IGF_HAS_REMOVABLE_JMP 0x0800 // this group ends with an unconditional jump which is a candidate for removal
#ifdef TARGET_ARM64
#define IGF_HAS_REMOVED_INSTR 0x1000 // this group has an instruction that was removed.
#endif
// 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.
#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
// Try to do better packing based on how large regMaskSmall is (8, 16, or 64 bits).
#if !(REGMASK_BITS <= 32)
regMaskSmall igGCregs; // set of registers with live GC refs
#endif // !(REGMASK_BITS <= 32)
union
{
BYTE* igData; // addr of instruction descriptors
insPlaceholderGroupData* igPhData; // when igFlags & IGF_PLACEHOLDER
};
#if EMIT_BACKWARDS_NAVIGATION
// Last instruction in group, if any (nullptr if none); used for backwards navigation.
// (Should be type emitter::instrDesc*).
void* igLastIns;
#endif // EMIT_BACKWARDS_NAVIGATION
#if EMIT_TRACK_STACK_DEPTH
unsigned igStkLvl; // stack level on entry
#endif // EMIT_TRACK_STACK_DEPTH
#if REGMASK_BITS <= 32
regMaskSmall igGCregs; // set of registers with live GC refs
#endif // REGMASK_BITS <= 32
unsigned char igInsCnt; // # of instructions in this group
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;
}
bool endsWithAlignInstr() const
{
return (igFlags & IGF_HAS_ALIGN) != 0;
}
// hadAlignInstr: Checks if this IG was ever marked as aligned and later
// decided to not align. Sometimes, a loop is marked as not
// needing alignment, but the igSize was not adjusted immediately.
// This method is used during loopSize calculation, where we adjust
// the loop size by removed alignment bytes.
bool hadAlignInstr() const
{
return (igFlags & IGF_REMOVED_ALIGN) != 0;
}
}; // 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() const; // Returns the variable to access. Note that it returns a negative number for compiler spill
// temps.
unsigned lvaOffset() const; // 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);
SetUseEvexEncoding(false);
#endif // TARGET_XARCH
emitDataSecCur = nullptr;
}
#include "emitpub.h"
protected:
/************************************************************************/
/* Miscellaneous stuff */
/************************************************************************/
Compiler* emitComp;
GCInfo* gcInfo;
CodeGen* codeGen;
size_t m_debugInfoSize;
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,
#if defined(TARGET_XARCH)
OPSZ32 = 5,
OPSZ64 = 6,
OPSZ_COUNT = 7,
#elif defined(TARGET_ARM64)
OPSZ_SCALABLE = 5,
OPSZ_COUNT = 6,
#else
OPSZ_COUNT = 5,
#endif
#ifdef TARGET_AMD64
OPSZP = OPSZ8,
#else
OPSZP = OPSZ4,
#endif
};
#define OPSIZE_INVALID ((opSize)0xffff)
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);
}
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);
}
void emitRecomputeIGoffsets();
void emitDispCommentForHandle(size_t handle, size_t cookie, GenTreeFlags flags);
/************************************************************************/
/* The following describes a single instruction */
/************************************************************************/
enum insFormat : unsigned
{
#define IF_DEF(en, op1, op2) IF_##en,
#include "emitfmts.h"
#if defined(TARGET_ARM64)
#define IF_DEF(en, op1, op2) IF_##en,
#include "emitfmtsarm64sve.h"
#endif
IF_COUNT
};
#ifdef TARGET_XARCH
#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;
};
#endif // TARGET_XARCH
struct instrDescDebugInfo
{
unsigned idNum;
size_t idSize; // size of the instruction descriptor
unsigned idVarRefOffs; // IL offset for LclVar reference
unsigned idVarRefOffs2; // IL offset for 2nd LclVar reference (in case this is a pair)
size_t idMemCookie; // compile time handle (check idFlags)
GenTreeFlags idFlags; // for determining type of handle in idMemCookie
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
};
#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;
#define MAX_ENCODED_SIZE 15
#elif defined(TARGET_ARM64)
#define INSTR_ENCODED_SIZE 4
static_assert_no_msg(INS_count <= 2048);
instruction _idIns : 11;
#elif defined(TARGET_LOONGARCH64)
// TODO-LoongArch64: not include SIMD-vector.
static_assert_no_msg(INS_count <= 512);
instruction _idIns : 9;
#else
static_assert_no_msg(INS_count <= 256);
instruction _idIns : 8;
#endif // !(defined(TARGET_XARCH) || defined(TARGET_ARM64) || defined(TARGET_LOONGARCH64))
// The format for the instruction
#if defined(TARGET_XARCH)
static_assert_no_msg(IF_COUNT <= 128);
insFormat _idInsFmt : 7;
#elif defined(TARGET_LOONGARCH64)
unsigned _idCodeSize : 5; // the instruction(s) size of this instrDesc described.
#elif defined(TARGET_RISCV64)
unsigned _idCodeSize : 6; // the instruction(s) size of this instrDesc described.
#elif defined(TARGET_ARM64)
static_assert_no_msg(IF_COUNT <= 1024);
insFormat _idInsFmt : 10;
#else
static_assert_no_msg(IF_COUNT <= 256);
insFormat _idInsFmt : 8;
#endif
public:
instrDesc() = delete; // Do not stack alloc this due to debug info that has to come before it.
instruction idIns() const
{
return _idIns;
}
void idIns(instruction ins)
{
assert((ins != INS_invalid) && (ins < INS_count));
_idIns = ins;
}
bool idInsIs(instruction ins) const
{
return idIns() == ins;
}
template <typename... T>
bool idInsIs(instruction ins, T... rest) const
{
return idInsIs(ins) || idInsIs(rest...);
}
#if defined(TARGET_LOONGARCH64)
insFormat idInsFmt() const
{ // not used for LOONGARCH64.
return (insFormat)0;
}
void idInsFmt(insFormat insFmt)
{
}
#elif defined(TARGET_RISCV64)
insFormat idInsFmt() const
{
NYI_RISCV64("idInsFmt-----unimplemented on RISCV64 yet----");
return (insFormat)0;
}
void idInsFmt(insFormat insFmt)
{
NYI_RISCV64("idInsFmt-----unimplemented on RISCV64 yet----");
}
#else
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;
}
#endif
////////////////////////////////////////////////////////////////////////
// Space taken up to here:
// x86: 17 bits
// amd64: 17 bits
// arm: 16 bits
// arm64: 21 bits
// loongarch64: 14 bits
// risc-v: 14 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)
opSize _idOpSize : 3; // operand size: 0=1 , 1=2 , 2=4 , 3=8, 4=16
insOpts _idInsOpt : 6; // options for instructions
#elif defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
/* _idOpSize defined below. */
#else
opSize _idOpSize : 2; // operand size: 0=1 , 1=2 , 2=4 , 3=8
#endif // TARGET_ARM64 || TARGET_LOONGARCH64 || TARGET_RISCV64
// 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: 46 bits
// loongarch64: 28 bits
// risc-v: 28 bits
unsigned _idSmallDsc : 1; // is this a "small" descriptor?
unsigned _idLargeCns : 1; // does a large constant follow? (or if large call descriptor used)
unsigned _idLargeDsp : 1; // does a large displacement follow?
unsigned _idCall : 1; // this is a call
// We have several pieces of information we need to encode but which are only applicable
// to a subset of instrDescs. To accommodate that, we define a several _idCustom# bitfields
// and then some defineds to make accessing them simpler
unsigned _idCustom1 : 1;
unsigned _idCustom2 : 1;
unsigned _idCustom3 : 1;
#define _idBound _idCustom1 /* jump target / frame offset bound */
#define _idTlsGD _idCustom2 /* Used to store information related to TLS GD access on linux */
#define _idNoGC _idCustom3 /* Some helpers don't get recorded in GC tables */
#define _idEvexAaaContext (_idCustom3 << 2) | (_idCustom2 << 1) | _idCustom1 /* bits used for the EVEX.aaa context */
#if !defined(TARGET_ARMARCH)
unsigned _idCustom4 : 1;
#define _idCallRegPtr _idCustom4 /* IL indirect calls : addr in reg */
#define _idEvexZContext _idCustom4 /* bits used for the EVEX.z context */
#endif // !TARGET_ARMARCH
#if defined(TARGET_XARCH)
// EVEX.b can indicate several context: embedded broadcast, embedded rounding.
// For normal and embedded broadcast intrinsics, EVEX.L'L has the same semantic, vector length.
// For embedded rounding, EVEX.L'L semantic changes to indicate the rounding mode.
// Multiple bits in _idEvexbContext are used to inform emitter to specially handle the EVEX.L'L bits.
unsigned _idEvexbContext : 2;
#endif // TARGET_XARCH
#ifdef TARGET_ARM64
unsigned _idLclVar : 1; // access a local on stack
unsigned _idLclVarPair : 1; // carries information for 2 GC lcl vars.
#endif
#ifdef TARGET_LOONGARCH64
// TODO-LoongArch64: maybe delete on future.
opSize _idOpSize : 3; // operand size: 0=1 , 1=2 , 2=4 , 3=8, 4=16
insOpts _idInsOpt : 6; // loongarch options for special: placeholders. e.g emitIns_R_C, also identifying the
// accessing a local on stack.
unsigned _idLclVar : 1; // access a local on stack.
#endif
#ifdef TARGET_RISCV64
// TODO-RISCV64: maybe delete on future
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
#endif
////////////////////////////////////////////////////////////////////////
// Space taken up to here:
// x86: 48 bits
// amd64: 48 bits
// arm: 48 bits
// arm64: 55 bits
// loongarch64: 46 bits
// risc-v: 46 bits
//
// How many bits have been used beyond the first 32?
// Define ID_EXTRA_BITFIELD_BITS to that number.
//
#if defined(TARGET_ARM)
#define ID_EXTRA_BITFIELD_BITS (16)
#elif defined(TARGET_ARM64)
#define ID_EXTRA_BITFIELD_BITS (23)
#elif defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
#define ID_EXTRA_BITFIELD_BITS (14)
#elif defined(TARGET_XARCH)
#define ID_EXTRA_BITFIELD_BITS (16)
#else
#error Unsupported or unset target architecture
#endif
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)
#if EMIT_BACKWARDS_NAVIGATION
// "Pointer" to previous instrDesc in this group. If zero, there is
// no previous instrDesc. If non-zero, then _idScaledPrevOffset * 4
// is the size in bytes of the previous instrDesc; subtract that from
// the current instrDesc* to reach the previous one.
// All instrDesc types are <= 56 bytes, but we also need m_debugInfoSize,
// which is pointer sized, so 5 bits are required on 64-bit and 4 bits
// on 32-bit.
#ifdef HOST_64BIT
unsigned _idScaledPrevOffset : 5;
#define ID_EXTRA_PREV_OFFSET_BITS (5)
#else
unsigned _idScaledPrevOffset : 4;
#define ID_EXTRA_PREV_OFFSET_BITS (4)
#endif
#else // !EMIT_BACKWARDS_NAVIGATION
#define ID_EXTRA_PREV_OFFSET_BITS (0)
#endif // !EMIT_BACKWARDS_NAVIGATION
////////////////////////////////////////////////////////////////////////
// Space taken up to here (with/without prev offset, assuming host==target):
// x86: 54/50 bits
// amd64: 55/50 bits
// arm: 54/50 bits
// arm64: 62/57 bits
// loongarch64: 53/48 bits
// risc-v: 53/48 bits
#define ID_EXTRA_BITS (ID_EXTRA_RELOC_BITS + ID_EXTRA_BITFIELD_BITS + ID_EXTRA_PREV_OFFSET_BITS)
/* Use whatever bits are left over for small constants */
#define ID_BIT_SMALL_CNS (32 - ID_EXTRA_BITS)
C_ASSERT(ID_BIT_SMALL_CNS > 0);
////////////////////////////////////////////////////////////////////////
// Small constant size (with/without prev offset, assuming host==target):
// x86: 10/14 bits
// amd64: 9/14 bits
// arm: 10/14 bits
// arm64: 2/7 bits
// loongarch64: 11/16 bits
// risc-v: 11/16 bits
#define ID_ADJ_SMALL_CNS (int)(1 << (ID_BIT_SMALL_CNS - 1))
#define ID_CNT_SMALL_CNS (int)(1 << ID_BIT_SMALL_CNS)
#define ID_MIN_SMALL_CNS (int)(0 - ID_ADJ_SMALL_CNS)
#define ID_MAX_SMALL_CNS (int)(ID_CNT_SMALL_CNS - ID_ADJ_SMALL_CNS - 1)
// We encounter many constants, but there is a disproportionate amount that are in the range [-1, +4]
// and otherwise powers of 2. We therefore allow the tracked range here to include negative values.
signed _idSmallCns : ID_BIT_SMALL_CNS;
////////////////////////////////////////////////////////////////////////
// Space taken up to here: 64 bits, all architectures, by design.
////////////////////////////////////////////////////////////////////////
//
// This is the end of the 'small' instrDesc which is the same on all platforms
//
// 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.
//
// SMALL_IDSC_SIZE is this size, in bytes.
//
#define SMALL_IDSC_SIZE 8
public:
instrDescDebugInfo* idDebugOnlyInfo() const
{
const char* addr = reinterpret_cast<const char*>(this);
return *reinterpret_cast<instrDescDebugInfo* const*>(addr - sizeof(instrDescDebugInfo*));
}
void idDebugOnlyInfo(instrDescDebugInfo* info)
{
char* addr = reinterpret_cast<char*>(this);
*reinterpret_cast<instrDescDebugInfo**>(addr - sizeof(instrDescDebugInfo*)) = info;
}
private:
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.
#if !defined(TARGET_ARM64) && !defined(TARGET_LOONGARCH64)
emitLclVarAddr iiaLclVar;
#endif
BasicBlock* iiaBBlabel;
insGroup* iiaIGlabel;
BYTE* iiaAddr;
#ifdef TARGET_XARCH
emitAddrMode iiaAddrMode;
#endif // TARGET_XARCH
CORINFO_FIELD_HANDLE iiaFieldHnd; // iiaFieldHandle is also used to encode
// an offset into the JIT data constant area
bool iiaIsJitDataOffset() const;
int iiaGetJitDataOffset() const;
// 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;
}
#ifdef TARGET_ARM
struct
{
regNumber _idReg3 : REGNUM_BITS;
regNumber _idReg4 : REGNUM_BITS;
};
#elif defined(TARGET_ARM64)
struct
{
// This 32-bit structure can pack with these unsigned bit fields
emitLclVarAddr iiaLclVar;
unsigned _idRegBit : 1; // Reg3 is scaled by idOpSize bits
GCtype _idGCref2 : 2;
regNumber _idReg3 : REGNUM_BITS;
regNumber _idReg4 : REGNUM_BITS;