-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
callingconvention.h
2281 lines (1948 loc) · 73.1 KB
/
callingconvention.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.
//
//
// Provides an abstraction over platform specific calling conventions (specifically, the calling convention
// utilized by the JIT on that platform). The caller enumerates each argument of a signature in turn, and is
// provided with information mapping that argument into registers and/or stack locations.
//
#ifndef __CALLING_CONVENTION_INCLUDED
#define __CALLING_CONVENTION_INCLUDED
BOOL IsRetBuffPassedAsFirstArg();
// Describes how a single argument is laid out in registers and/or stack locations when given as an input to a
// managed method as part of a larger signature.
//
// Locations are split into floating point registers, general registers and stack offsets. Registers are
// obviously architecture dependent but are represented as a zero-based index into the usual sequence in which
// such registers are allocated for input on the platform in question. For instance:
// X86: 0 == ecx, 1 == edx
// ARM: 0 == r0, 1 == r1, 2 == r2 etc.
//
// Stack locations are represented as offsets from the stack pointer (at the point of the call). The offset is
// given as an index of a pointer sized slot. Similarly the size of data on the stack is given in slot-sized
// units. For instance, given an index of 2 and a size of 3:
// X86: argument starts at [ESP + 8] and is 12 bytes long
// AMD64: argument starts at [RSP + 16] and is 24 bytes long
//
// The structure is flexible enough to describe an argument that is split over several (consecutive) registers
// and possibly on to the stack as well.
struct ArgLocDesc
{
int m_idxFloatReg; // First floating point register used (or -1)
int m_cFloatReg; // Count of floating point registers used (or 0)
int m_idxGenReg; // First general register used (or -1)
int m_cGenReg; // Count of general registers used (or 0)
int m_byteStackIndex; // Stack offset in bytes (or -1)
int m_byteStackSize; // Stack size in bytes
#if defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
FpStructInRegistersInfo m_structFields; // Struct field info when using floating-point register(s)
#endif
#if defined(UNIX_AMD64_ABI)
EEClass* m_eeClass; // For structs passed in register, it points to the EEClass of the struct
#endif // UNIX_AMD64_ABI
#ifdef FEATURE_HFA
static unsigned getHFAFieldSize(CorInfoHFAElemType hfaType)
{
switch (hfaType)
{
case CORINFO_HFA_ELEM_FLOAT: return 4;
case CORINFO_HFA_ELEM_DOUBLE: return 8;
case CORINFO_HFA_ELEM_VECTOR64: return 8;
case CORINFO_HFA_ELEM_VECTOR128: return 16;
default: _ASSERTE(!"Invalid HFA Type"); return 0;
}
}
#endif
#if defined(TARGET_ARM64)
unsigned m_hfaFieldSize; // Size of HFA field in bytes.
void setHFAFieldSize(CorInfoHFAElemType hfaType)
{
m_hfaFieldSize = getHFAFieldSize(hfaType);
}
#endif // defined(TARGET_ARM64)
#if defined(TARGET_ARM)
BOOL m_fRequires64BitAlignment; // True if the argument should always be aligned (in registers or on the stack
#endif
ArgLocDesc()
{
Init();
}
// Initialize to represent a non-placed argument (no register or stack slots referenced).
void Init()
{
m_idxFloatReg = -1;
m_cFloatReg = 0;
m_idxGenReg = -1;
m_cGenReg = 0;
m_byteStackIndex = -1;
m_byteStackSize = 0;
#if defined(TARGET_ARM)
m_fRequires64BitAlignment = FALSE;
#endif
#if defined(TARGET_ARM64)
m_hfaFieldSize = 0;
#endif // defined(TARGET_ARM64)
#if defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
m_structFields = {};
#endif
#if defined(UNIX_AMD64_ABI)
m_eeClass = NULL;
#endif
}
};
//
// TransitionBlock is layout of stack frame of method call, saved argument registers and saved callee saved registers. Even though not
// all fields are used all the time, we use uniform form for simplicity.
//
struct TransitionBlock
{
#if defined(TARGET_X86)
ArgumentRegisters m_argumentRegisters;
CalleeSavedRegisters m_calleeSavedRegisters;
TADDR m_ReturnAddress;
#elif defined(TARGET_AMD64)
#ifdef UNIX_AMD64_ABI
ArgumentRegisters m_argumentRegisters;
#endif
CalleeSavedRegisters m_calleeSavedRegisters;
TADDR m_ReturnAddress;
#elif defined(TARGET_ARM)
union {
CalleeSavedRegisters m_calleeSavedRegisters;
// alias saved link register as m_ReturnAddress
struct {
INT32 r4, r5, r6, r7, r8, r9, r10;
INT32 r11;
TADDR m_ReturnAddress;
};
};
ArgumentRegisters m_argumentRegisters;
#elif defined(TARGET_ARM64)
union {
CalleeSavedRegisters m_calleeSavedRegisters;
struct {
INT64 x29; // frame pointer
TADDR m_ReturnAddress;
INT64 x19, x20, x21, x22, x23, x24, x25, x26, x27, x28;
};
};
TADDR padding; // Keep size of TransitionBlock as multiple of 16-byte. Simplifies code in PROLOG_WITH_TRANSITION_BLOCK
INT64 m_x8RetBuffReg;
ArgumentRegisters m_argumentRegisters;
#elif defined(TARGET_LOONGARCH64)
union {
CalleeSavedRegisters m_calleeSavedRegisters;
struct {
INT64 fp; // frame pointer
TADDR m_ReturnAddress;
INT64 s0;
INT64 s1;
INT64 s2;
INT64 s3;
INT64 s4;
INT64 s5;
INT64 s6;
INT64 s7;
INT64 s8;
INT64 tp;
};
};
//TADDR padding; // Keep size of TransitionBlock as multiple of 16-byte. Simplifies code in PROLOG_WITH_TRANSITION_BLOCK
ArgumentRegisters m_argumentRegisters;
#elif defined(TARGET_RISCV64)
union {
CalleeSavedRegisters m_calleeSavedRegisters;
struct {
INT64 fp; // frame pointer
TADDR m_ReturnAddress;
INT64 s1;
INT64 s2;
INT64 s3;
INT64 s4;
INT64 s5;
INT64 s6;
INT64 s7;
INT64 s8;
INT64 s9;
INT64 s10;
INT64 s11;
INT64 tp;
INT64 gp;
};
};
TADDR padding; // Keep size of TransitionBlock as multiple of 16-byte. Simplifies code in PROLOG_WITH_TRANSITION_BLOCK
ArgumentRegisters m_argumentRegisters;
#else
PORTABILITY_ASSERT("TransitionBlock");
#endif
// The transition block should define everything pushed by callee. The code assumes in number of places that
// end of the transition block is caller's stack pointer.
static int GetOffsetOfReturnAddress()
{
LIMITED_METHOD_CONTRACT;
return offsetof(TransitionBlock, m_ReturnAddress);
}
#ifdef TARGET_ARM64
static int GetOffsetOfRetBuffArgReg()
{
LIMITED_METHOD_CONTRACT;
return offsetof(TransitionBlock, m_x8RetBuffReg);
}
static int GetOffsetOfFirstGCRefMapSlot()
{
return GetOffsetOfRetBuffArgReg();
}
#else
static int GetOffsetOfFirstGCRefMapSlot()
{
return GetOffsetOfArgumentRegisters();
}
#endif
static BYTE GetOffsetOfArgs()
{
LIMITED_METHOD_CONTRACT;
// Offset of the stack args (which are after the TransitionBlock)
return sizeof(TransitionBlock);
}
static int GetOffsetOfArgumentRegisters()
{
LIMITED_METHOD_CONTRACT;
int offs;
#if defined(TARGET_AMD64) && !defined(UNIX_AMD64_ABI)
offs = sizeof(TransitionBlock);
#else
offs = offsetof(TransitionBlock, m_argumentRegisters);
#endif
return offs;
}
static BOOL IsStackArgumentOffset(int offset)
{
LIMITED_METHOD_CONTRACT;
#if defined(UNIX_AMD64_ABI)
return offset >= (int)sizeof(TransitionBlock);
#else
int ofsArgRegs = GetOffsetOfArgumentRegisters();
return offset >= (int) (ofsArgRegs + ARGUMENTREGISTERS_SIZE);
#endif
}
static BOOL IsArgumentRegisterOffset(int offset)
{
LIMITED_METHOD_CONTRACT;
int ofsArgRegs = GetOffsetOfArgumentRegisters();
return offset >= ofsArgRegs && offset < (int) (ofsArgRegs + ARGUMENTREGISTERS_SIZE);
}
static UINT GetArgumentIndexFromOffset(int offset)
{
LIMITED_METHOD_CONTRACT;
#if defined(UNIX_AMD64_ABI)
_ASSERTE(offset != TransitionBlock::StructInRegsOffset);
#endif
offset -= GetOffsetOfArgumentRegisters();
_ASSERTE((offset % TARGET_POINTER_SIZE) == 0);
return offset / TARGET_POINTER_SIZE;
}
static UINT GetStackArgumentIndexFromOffset(int offset)
{
LIMITED_METHOD_CONTRACT;
return (offset - TransitionBlock::GetOffsetOfArgs()) / TARGET_POINTER_SIZE;
}
static UINT GetStackArgumentByteIndexFromOffset(int offset)
{
LIMITED_METHOD_CONTRACT;
return (offset - TransitionBlock::GetOffsetOfArgs());
}
#ifdef CALLDESCR_FPARGREGS
static BOOL IsFloatArgumentRegisterOffset(int offset)
{
LIMITED_METHOD_CONTRACT;
#if defined(UNIX_AMD64_ABI)
return (offset != TransitionBlock::StructInRegsOffset) && (offset < 0);
#else
return offset < 0;
#endif
}
// Check if an argument has floating point register, that means that it is
// either a floating point argument or a struct passed in registers that
// has a floating point member.
static BOOL HasFloatRegister(int offset, ArgLocDesc* argLocDescForStructInRegs)
{
LIMITED_METHOD_CONTRACT;
#if defined(UNIX_AMD64_ABI)
if (offset == TransitionBlock::StructInRegsOffset)
{
return argLocDescForStructInRegs->m_cFloatReg > 0;
}
#elif defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
if (argLocDescForStructInRegs != NULL)
{
return argLocDescForStructInRegs->m_cFloatReg > 0;
}
#endif
return offset < 0;
}
static int GetOffsetOfFloatArgumentRegisters()
{
LIMITED_METHOD_CONTRACT;
return -GetNegSpaceSize();
}
#endif // CALLDESCR_FPARGREGS
static int GetOffsetOfCalleeSavedRegisters()
{
LIMITED_METHOD_CONTRACT;
return offsetof(TransitionBlock, m_calleeSavedRegisters);
}
static int GetNegSpaceSize()
{
LIMITED_METHOD_CONTRACT;
int negSpaceSize = 0;
#ifdef CALLDESCR_FPARGREGS
negSpaceSize += sizeof(FloatArgumentRegisters);
#endif
#ifdef TARGET_ARM
negSpaceSize += TARGET_POINTER_SIZE; // padding to make FloatArgumentRegisters address 8-byte aligned
#endif
return negSpaceSize;
}
static const int InvalidOffset = -1;
#if defined(UNIX_AMD64_ABI)
// Special offset value to represent struct passed in registers. Such a struct can span both
// general purpose and floating point registers, so it can have two different offsets.
static const int StructInRegsOffset = -2;
#endif
};
//-----------------------------------------------------------------------
// ArgIterator is helper for dealing with calling conventions.
// It is tightly coupled with TransitionBlock. It uses offsets into
// TransitionBlock to represent argument locations for efficiency
// reasons. Alternatively, it can also return ArgLocDesc for less
// performance critical code.
//
// The ARGITERATOR_BASE argument of the template is provider of the parsed
// method signature. Typically, the arg iterator works on top of MetaSig.
// Reflection invoke uses alternative implementation to save signature parsing
// time because of it has the parsed signature available.
//-----------------------------------------------------------------------
template<class ARGITERATOR_BASE>
class ArgIteratorTemplate : public ARGITERATOR_BASE
{
public:
//------------------------------------------------------------
// Constructor
//------------------------------------------------------------
ArgIteratorTemplate()
{
WRAPPER_NO_CONTRACT;
m_dwFlags = 0;
#if defined(TARGET_RISCV64) || defined(TARGET_LOONGARCH64)
m_returnedFpFieldOffsets[0] = 0;
m_returnedFpFieldOffsets[1] = 0;
#endif
}
UINT SizeOfArgStack()
{
WRAPPER_NO_CONTRACT;
if (!(m_dwFlags & SIZE_OF_ARG_STACK_COMPUTED))
ForceSigWalk();
_ASSERTE((m_dwFlags & SIZE_OF_ARG_STACK_COMPUTED) != 0);
_ASSERTE((m_nSizeOfArgStack % TARGET_POINTER_SIZE) == 0);
return m_nSizeOfArgStack;
}
// For use with ArgIterator. This function computes the amount of additional
// memory required above the TransitionBlock. The parameter offsets
// returned by ArgIteratorTemplate::GetNextOffset are relative to a
// FramedMethodFrame, and may be in either of these regions.
UINT SizeOfFrameArgumentArray()
{
WRAPPER_NO_CONTRACT;
UINT size = SizeOfArgStack();
#if defined(TARGET_AMD64) && !defined(UNIX_AMD64_ABI)
// The argument registers are not included in the stack size on AMD64
size += ARGUMENTREGISTERS_SIZE;
#endif
_ASSERTE((size % TARGET_POINTER_SIZE) == 0);
return size;
}
//------------------------------------------------------------------------
#ifdef TARGET_X86
UINT CbStackPop()
{
WRAPPER_NO_CONTRACT;
if (this->IsVarArg())
return 0;
else
return SizeOfArgStack();
}
#endif
// Is there a hidden parameter for the return parameter?
//
BOOL HasRetBuffArg()
{
WRAPPER_NO_CONTRACT;
if (!(m_dwFlags & RETURN_FLAGS_COMPUTED))
ComputeReturnFlags();
return (m_dwFlags & RETURN_HAS_RET_BUFFER);
}
UINT GetFPReturnSize()
{
WRAPPER_NO_CONTRACT;
if (!(m_dwFlags & RETURN_FLAGS_COMPUTED))
ComputeReturnFlags();
return m_dwFlags >> RETURN_FP_SIZE_SHIFT;
}
#if defined(TARGET_RISCV64) || defined(TARGET_LOONGARCH64)
FpStructInRegistersInfo GetReturnFpStructInRegistersInfo()
{
WRAPPER_NO_CONTRACT;
if (!(m_dwFlags & RETURN_FLAGS_COMPUTED))
ComputeReturnFlags();
return {
FpStruct::Flags(m_dwFlags >> RETURN_FP_SIZE_SHIFT),
m_returnedFpFieldOffsets[0],
m_returnedFpFieldOffsets[1],
};
}
#endif // defined(TARGET_RISCV64) || defined(TARGET_LOONGARCH64)
#ifdef TARGET_X86
//=========================================================================
// Indicates whether an argument is to be put in a register using the
// default IL calling convention. This should be called on each parameter
// in the order it appears in the call signature. For a non-static method,
// this function should also be called once for the "this" argument, prior
// to calling it for the "real" arguments. Pass in a typ of ELEMENT_TYPE_CLASS.
//
// *pNumRegistersUsed: [in,out]: keeps track of the number of argument
// registers assigned previously. The caller should
// initialize this variable to 0 - then each call
// will update it.
//
// typ: the signature type
//=========================================================================
static BOOL IsArgumentInRegister(int * pNumRegistersUsed, CorElementType typ, TypeHandle hnd)
{
LIMITED_METHOD_CONTRACT;
if ( (*pNumRegistersUsed) < NUM_ARGUMENT_REGISTERS)
{
if (typ == ELEMENT_TYPE_VALUETYPE)
{
// The JIT enables passing trivial pointer sized structs in registers.
MethodTable* pMT = hnd.GetMethodTable();
while (typ == ELEMENT_TYPE_VALUETYPE &&
pMT->GetNumInstanceFields() == 1 && (!pMT->HasLayout() ||
pMT->GetNumInstanceFieldBytes() == 4
)) // Don't do the optimization if we're getting specified anything but the trivial layout.
{
FieldDesc * pFD = pMT->GetApproxFieldDescListRaw();
CorElementType type = pFD->GetFieldType();
bool exitLoop = false;
switch (type)
{
case ELEMENT_TYPE_VALUETYPE:
{
//@todo: Is it more apropos to call LookupApproxFieldTypeHandle() here?
TypeHandle fldHnd = pFD->GetApproxFieldTypeHandleThrowing();
CONSISTENCY_CHECK(!fldHnd.IsNull());
pMT = fldHnd.GetMethodTable();
FALLTHROUGH;
}
case ELEMENT_TYPE_PTR:
case ELEMENT_TYPE_I:
case ELEMENT_TYPE_U:
case ELEMENT_TYPE_I4:
case ELEMENT_TYPE_U4:
{
typ = type;
break;
}
default:
exitLoop = true;
break;
}
if (exitLoop)
{
break;
}
}
}
if (gElementTypeInfo[typ].m_enregister)
{
(*pNumRegistersUsed)++;
return(TRUE);
}
}
return(FALSE);
}
#endif // TARGET_X86
#if defined(ENREGISTERED_PARAMTYPE_MAXSIZE)
// Note that this overload does not handle varargs
static BOOL IsArgPassedByRef(TypeHandle th)
{
LIMITED_METHOD_CONTRACT;
_ASSERTE(!th.IsNull());
// This method only works for valuetypes. It includes true value types,
// primitives, enums and TypedReference.
_ASSERTE(th.IsValueType());
size_t size = th.GetSize();
#ifdef TARGET_AMD64
return IsArgPassedByRef(size);
#elif defined(TARGET_ARM64)
// Composites greater than 16 bytes are passed by reference
return ((size > ENREGISTERED_PARAMTYPE_MAXSIZE) && !th.IsHFA());
#elif defined(TARGET_LOONGARCH64)
// Composites greater than 16 bytes are passed by reference
return (size > ENREGISTERED_PARAMTYPE_MAXSIZE);
#elif defined(TARGET_RISCV64)
return (size > ENREGISTERED_PARAMTYPE_MAXSIZE);
#else
PORTABILITY_ASSERT("ArgIteratorTemplate::IsArgPassedByRef");
return FALSE;
#endif
}
#ifdef TARGET_AMD64
// This overload should only be used in AMD64-specific code only.
static BOOL IsArgPassedByRef(size_t size)
{
LIMITED_METHOD_CONTRACT;
#ifdef UNIX_AMD64_ABI
// No arguments are passed by reference on AMD64 on Unix
return FALSE;
#else
// If the size is bigger than ENREGISTERED_PARAM_TYPE_MAXSIZE, or if the size is NOT a power of 2, then
// the argument is passed by reference.
return (size > ENREGISTERED_PARAMTYPE_MAXSIZE) || ((size & (size-1)) != 0);
#endif
}
#endif // TARGET_AMD64
// This overload should be used for varargs only.
static BOOL IsVarArgPassedByRef(size_t size)
{
LIMITED_METHOD_CONTRACT;
#ifdef TARGET_AMD64
#ifdef UNIX_AMD64_ABI
PORTABILITY_ASSERT("ArgIteratorTemplate::IsVarArgPassedByRef");
return FALSE;
#else // UNIX_AMD64_ABI
return IsArgPassedByRef(size);
#endif // UNIX_AMD64_ABI
#else
return (size > ENREGISTERED_PARAMTYPE_MAXSIZE);
#endif
}
BOOL IsArgPassedByRef()
{
LIMITED_METHOD_CONTRACT;
#ifdef TARGET_AMD64
return IsArgPassedByRef(m_argSize);
#elif defined(TARGET_ARM64)
if (m_argType == ELEMENT_TYPE_VALUETYPE)
{
_ASSERTE(!m_argTypeHandle.IsNull());
return ((m_argSize > ENREGISTERED_PARAMTYPE_MAXSIZE) && (!m_argTypeHandle.IsHFA() || this->IsVarArg()));
}
return FALSE;
#elif defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
if (m_argType == ELEMENT_TYPE_VALUETYPE)
{
_ASSERTE(!m_argTypeHandle.IsNull());
return (m_argSize > ENREGISTERED_PARAMTYPE_MAXSIZE);
}
return FALSE;
#else
PORTABILITY_ASSERT("ArgIteratorTemplate::IsArgPassedByRef");
return FALSE;
#endif
}
#endif // ENREGISTERED_PARAMTYPE_MAXSIZE
//------------------------------------------------------------
// Return the offsets of the special arguments
//------------------------------------------------------------
static int GetThisOffset();
int GetRetBuffArgOffset();
int GetVASigCookieOffset();
int GetParamTypeArgOffset();
//------------------------------------------------------------
// Each time this is called, this returns a byte offset of the next
// argument from the TransitionBlock* pointer.
//
// Returns TransitionBlock::InvalidOffset once you've hit the end
// of the list.
//------------------------------------------------------------
int GetNextOffset();
CorElementType GetArgType(TypeHandle *pTypeHandle = NULL) const
{
LIMITED_METHOD_CONTRACT;
if (pTypeHandle != NULL)
{
*pTypeHandle = m_argTypeHandle;
}
return m_argType;
}
int GetArgSize() const
{
LIMITED_METHOD_CONTRACT;
return m_argSize;
}
void ForceSigWalk();
#ifndef TARGET_X86
// Accessors for built in argument descriptions of the special implicit parameters not mentioned directly
// in signatures (this pointer and the like). Whether or not these can be used successfully before all the
// explicit arguments have been scanned is platform dependent.
void GetThisLoc(ArgLocDesc * pLoc) { WRAPPER_NO_CONTRACT; GetSimpleLoc(GetThisOffset(), pLoc); }
void GetParamTypeLoc(ArgLocDesc * pLoc) { WRAPPER_NO_CONTRACT; GetSimpleLoc(GetParamTypeArgOffset(), pLoc); }
void GetVASigCookieLoc(ArgLocDesc * pLoc) { WRAPPER_NO_CONTRACT; GetSimpleLoc(GetVASigCookieOffset(), pLoc); }
#ifndef CALLDESCR_RETBUFFARGREG
void GetRetBuffArgLoc(ArgLocDesc * pLoc) { WRAPPER_NO_CONTRACT; GetSimpleLoc(GetRetBuffArgOffset(), pLoc); }
#endif
#endif // !TARGET_X86
ArgLocDesc* GetArgLocDescForStructInRegs()
{
#if defined(UNIX_AMD64_ABI) || defined (TARGET_ARM64) || defined(TARGET_LOONGARCH64) || defined (TARGET_RISCV64)
return m_hasArgLocDescForStructInRegs ? &m_argLocDescForStructInRegs : NULL;
#else
return NULL;
#endif
}
#ifdef TARGET_X86
// Get layout information for the argument that the ArgIterator is currently visiting.
void GetArgLoc(int argOffset, ArgLocDesc *pLoc)
{
LIMITED_METHOD_CONTRACT;
pLoc->Init();
if (!TransitionBlock::IsStackArgumentOffset(argOffset))
{
pLoc->m_idxGenReg = TransitionBlock::GetArgumentIndexFromOffset(argOffset);
_ASSERTE(GetArgSize() <= TARGET_POINTER_SIZE);
pLoc->m_cGenReg = 1;
}
else
{
pLoc->m_byteStackSize = GetArgSize();
pLoc->m_byteStackIndex = TransitionBlock::GetStackArgumentByteIndexFromOffset(argOffset);
}
}
#endif
#ifdef TARGET_ARM
// Get layout information for the argument that the ArgIterator is currently visiting.
void GetArgLoc(int argOffset, ArgLocDesc *pLoc)
{
LIMITED_METHOD_CONTRACT;
pLoc->Init();
pLoc->m_fRequires64BitAlignment = m_fRequires64BitAlignment;
const int byteArgSize = GetArgSize();
if (TransitionBlock::IsFloatArgumentRegisterOffset(argOffset))
{
const int floatRegOfsInBytes = argOffset - TransitionBlock::GetOffsetOfFloatArgumentRegisters();
_ASSERTE((floatRegOfsInBytes % FLOAT_REGISTER_SIZE) == 0);
pLoc->m_idxFloatReg = floatRegOfsInBytes / FLOAT_REGISTER_SIZE;
pLoc->m_cFloatReg = ALIGN_UP(byteArgSize, FLOAT_REGISTER_SIZE) / FLOAT_REGISTER_SIZE;
return;
}
if (!TransitionBlock::IsStackArgumentOffset(argOffset))
{
pLoc->m_idxGenReg = TransitionBlock::GetArgumentIndexFromOffset(argOffset);
if (byteArgSize <= (4 - pLoc->m_idxGenReg) * TARGET_POINTER_SIZE)
{
pLoc->m_cGenReg = ALIGN_UP(byteArgSize, TARGET_POINTER_SIZE) / TARGET_POINTER_SIZE;
}
else
{
pLoc->m_cGenReg = 4 - pLoc->m_idxGenReg;
pLoc->m_byteStackIndex = 0;
pLoc->m_byteStackSize = StackElemSize(byteArgSize) - pLoc->m_cGenReg * TARGET_POINTER_SIZE;
}
}
else
{
pLoc->m_byteStackIndex = TransitionBlock::GetStackArgumentByteIndexFromOffset(argOffset);
pLoc->m_byteStackSize = StackElemSize(byteArgSize);
}
}
#endif // TARGET_ARM
#ifdef TARGET_ARM64
// Get layout information for the argument that the ArgIterator is currently visiting.
void GetArgLoc(int argOffset, ArgLocDesc *pLoc)
{
LIMITED_METHOD_CONTRACT;
pLoc->Init();
if (TransitionBlock::IsFloatArgumentRegisterOffset(argOffset))
{
const int floatRegOfsInBytes = argOffset - TransitionBlock::GetOffsetOfFloatArgumentRegisters();
_ASSERTE((floatRegOfsInBytes % FLOAT_REGISTER_SIZE) == 0);
pLoc->m_idxFloatReg = floatRegOfsInBytes / FLOAT_REGISTER_SIZE;
if (!m_argTypeHandle.IsNull() && m_argTypeHandle.IsHFA())
{
CorInfoHFAElemType type = m_argTypeHandle.GetHFAType();
pLoc->setHFAFieldSize(type);
pLoc->m_cFloatReg = GetArgSize() / pLoc->m_hfaFieldSize;
}
else
{
pLoc->m_cFloatReg = 1;
}
return;
}
unsigned byteArgSize = GetArgSize();
// On ARM64 some composites are implicitly passed by reference.
if (IsArgPassedByRef())
{
byteArgSize = TARGET_POINTER_SIZE;
}
// Sanity check to make sure no caller is trying to get an ArgLocDesc that
// describes the return buffer reg field that's in the TransitionBlock.
_ASSERTE(argOffset != TransitionBlock::GetOffsetOfRetBuffArgReg());
if (!TransitionBlock::IsStackArgumentOffset(argOffset))
{
pLoc->m_idxGenReg = TransitionBlock::GetArgumentIndexFromOffset(argOffset);
pLoc->m_cGenReg = ALIGN_UP(byteArgSize, TARGET_POINTER_SIZE) / TARGET_POINTER_SIZE;;
}
else
{
pLoc->m_byteStackIndex = TransitionBlock::GetStackArgumentByteIndexFromOffset(argOffset);
const bool isValueType = (m_argType == ELEMENT_TYPE_VALUETYPE);
const bool isFloatHfa = (isValueType && !m_argTypeHandle.IsNull() && m_argTypeHandle.IsHFA());
if (isFloatHfa)
{
CorInfoHFAElemType type = m_argTypeHandle.GetHFAType();
pLoc->setHFAFieldSize(type);
}
pLoc->m_byteStackSize = StackElemSize(byteArgSize, isValueType, isFloatHfa);
}
}
#endif // TARGET_ARM64
#if defined(TARGET_AMD64)
// Get layout information for the argument that the ArgIterator is currently visiting.
void GetArgLoc(int argOffset, ArgLocDesc* pLoc)
{
LIMITED_METHOD_CONTRACT;
#if defined(UNIX_AMD64_ABI)
if (m_hasArgLocDescForStructInRegs)
{
*pLoc = m_argLocDescForStructInRegs;
return;
}
if (argOffset == TransitionBlock::StructInRegsOffset)
{
// We always already have argLocDesc for structs passed in registers, we
// compute it in the GetNextOffset for those since it is always needed.
_ASSERTE(false);
return;
}
#endif // UNIX_AMD64_ABI
pLoc->Init();
#if defined(UNIX_AMD64_ABI)
if (TransitionBlock::IsFloatArgumentRegisterOffset(argOffset))
{
const int floatRegOfsInBytes = argOffset - TransitionBlock::GetOffsetOfFloatArgumentRegisters();
_ASSERTE((floatRegOfsInBytes % FLOAT_REGISTER_SIZE) == 0);
pLoc->m_idxFloatReg = floatRegOfsInBytes / FLOAT_REGISTER_SIZE;
pLoc->m_cFloatReg = 1;
}
else
#endif // UNIX_AMD64_ABI
if (!TransitionBlock::IsStackArgumentOffset(argOffset))
{
#if !defined(UNIX_AMD64_ABI)
// On Windows x64, we re-use the location in the transition block for both the integer and floating point registers
if ((m_argType == ELEMENT_TYPE_R4) || (m_argType == ELEMENT_TYPE_R8))
{
pLoc->m_idxFloatReg = TransitionBlock::GetArgumentIndexFromOffset(argOffset);
pLoc->m_cFloatReg = 1;
}
else
#endif
{
pLoc->m_idxGenReg = TransitionBlock::GetArgumentIndexFromOffset(argOffset);
pLoc->m_cGenReg = 1;
}
}
else
{
pLoc->m_byteStackIndex = TransitionBlock::GetStackArgumentByteIndexFromOffset(argOffset);
int argSizeInBytes;
if (IsArgPassedByRef())
argSizeInBytes = TARGET_POINTER_SIZE;
else
argSizeInBytes = GetArgSize();
pLoc->m_byteStackSize = StackElemSize(argSizeInBytes);
}
}
#endif // TARGET_AMD64
#if defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
// Get layout information for the argument that the ArgIterator is currently visiting.
// TODO-RISCV64: support SIMD.
void GetArgLoc(int argOffset, ArgLocDesc *pLoc)
{
LIMITED_METHOD_CONTRACT;
pLoc->Init();
if (m_hasArgLocDescForStructInRegs)
{
*pLoc = m_argLocDescForStructInRegs;
return;
}
if (TransitionBlock::IsFloatArgumentRegisterOffset(argOffset))
{
// Dividing by 8 as size of each register in FloatArgumentRegisters is 8 bytes.
const int floatRegOfsInBytes = argOffset - TransitionBlock::GetOffsetOfFloatArgumentRegisters();
_ASSERTE((floatRegOfsInBytes % FLOAT_REGISTER_SIZE) == 0);
pLoc->m_idxFloatReg = floatRegOfsInBytes / FLOAT_REGISTER_SIZE;
pLoc->m_cFloatReg = 1;
return;
}
int cSlots = (GetArgSize() + 7)/ 8;
// Composites greater than 16bytes are passed by reference
if (IsArgPassedByRef())
{
cSlots = 1;
}
if (!TransitionBlock::IsStackArgumentOffset(argOffset))
{
// At least one used integer register passed.
pLoc->m_idxGenReg = TransitionBlock::GetArgumentIndexFromOffset(argOffset);
pLoc->m_cGenReg = cSlots;
}
else
{
pLoc->m_byteStackIndex = TransitionBlock::GetStackArgumentByteIndexFromOffset(argOffset);
pLoc->m_byteStackSize = cSlots << 3;
}
return;
}
#endif // TARGET_LOONGARCH64 || TARGET_RISCV64
protected:
DWORD m_dwFlags; // Cached flags
int m_nSizeOfArgStack; // Cached value of SizeOfArgStack
#if defined(TARGET_RISCV64) || defined(TARGET_LOONGARCH64)
// Cached offsets of struct fields returned according to hardware floating-point calling convention
// (FpStruct::Flags are packed in m_dwFlags)
unsigned m_returnedFpFieldOffsets[ENREGISTERED_RETURNTYPE_MAXSIZE / sizeof(ARG_SLOT)];
#endif
DWORD m_argNum;
// Cached information about last argument
CorElementType m_argType;
int m_argSize;
TypeHandle m_argTypeHandle;
#if (defined(TARGET_AMD64) && defined(UNIX_AMD64_ABI)) || defined(TARGET_ARM64) || defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
ArgLocDesc m_argLocDescForStructInRegs;
bool m_hasArgLocDescForStructInRegs;
#endif // (TARGET_AMD64 && UNIX_AMD64_ABI) || TARGET_ARM64 || TARGET_LOONGARCH64 || TARGET_RISCV64
int m_ofsStack; // Current position of the stack iterator, in bytes
#ifdef TARGET_X86
int m_numRegistersUsed;
#ifdef FEATURE_INTERPRETER
bool m_fUnmanagedCallConv;
#endif
#endif
#ifdef UNIX_AMD64_ABI
int m_idxGenReg; // Next general register to be assigned a value
int m_idxFPReg; // Next floating point register to be assigned a value
bool m_fArgInRegisters; // Indicates that the current argument is stored in registers
#endif
#ifdef TARGET_ARM
int m_idxGenReg; // Next general register to be assigned a value
WORD m_wFPRegs; // Bitmask of available floating point argument registers (s0-s15/d0-d7)
bool m_fRequires64BitAlignment; // Cached info about the current arg
#endif
#ifdef TARGET_ARM64
int m_idxGenReg; // Next general register to be assigned a value
int m_idxFPReg; // Next FP register to be assigned a value
#endif
#ifdef TARGET_LOONGARCH64
int m_idxGenReg; // Next general register to be assigned a value
int m_idxStack; // Next stack slot to be assigned a value
int m_idxFPReg; // Next FP register to be assigned a value
#endif
#ifdef TARGET_RISCV64
int m_idxGenReg; // Next general register to be assigned a value
int m_idxStack; // Next stack slot to be assigned a value
int m_idxFPReg; // Next FP register to be assigned a value
#endif
enum {
ITERATION_STARTED = 0x0001, // Started iterating over arguments
SIZE_OF_ARG_STACK_COMPUTED = 0x0002,
RETURN_FLAGS_COMPUTED = 0x0004,
RETURN_HAS_RET_BUFFER = 0x0008, // Cached value of HasRetBuffArg
#ifdef TARGET_X86
PARAM_TYPE_REGISTER_MASK = 0x0030,
PARAM_TYPE_REGISTER_STACK = 0x0010,
PARAM_TYPE_REGISTER_ECX = 0x0020,
PARAM_TYPE_REGISTER_EDX = 0x0030,
#endif
METHOD_INVOKE_NEEDS_ACTIVATION = 0x0040, // Flag used by ArgIteratorForMethodInvoke