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
/
ee_il_dll.cpp
1442 lines (1213 loc) · 42.5 KB
/
ee_il_dll.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX XX
XX ee_jit.cpp XX
XX XX
XX The functionality needed for the JIT DLL. Includes the DLL entry point XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#include "emit.h"
#include "corexcep.h"
#if !defined(_HOST_UNIX_)
#include <io.h> // For _dup, _setmode
#include <fcntl.h> // For _O_TEXT
#include <errno.h> // For EINVAL
#endif
#ifndef DLLEXPORT
#define DLLEXPORT
#endif // !DLLEXPORT
/*****************************************************************************/
FILE* jitstdout = nullptr;
ICorJitHost* g_jitHost = nullptr;
static CILJit* ILJitter = nullptr; // The one and only JITTER I return
bool g_jitInitialized = false;
#ifndef FEATURE_MERGE_JIT_AND_ENGINE
HINSTANCE g_hInst = nullptr;
#endif // FEATURE_MERGE_JIT_AND_ENGINE
/*****************************************************************************/
extern "C" DLLEXPORT void __stdcall jitStartup(ICorJitHost* jitHost)
{
if (g_jitInitialized)
{
if (jitHost != g_jitHost)
{
// We normally don't expect jitStartup() to be invoked more than once.
// (We check whether it has been called once due to an abundance of caution.)
// However, during SuperPMI playback of MCH file, we need to JIT many different methods.
// Each one carries its own environment configuration state.
// So, we need the JIT to reload the JitConfig state for each change in the environment state of the
// replayed compilations.
// We do this by calling jitStartup with a different ICorJitHost,
// and have the JIT re-initialize its JitConfig state when this happens.
JitConfig.destroy(g_jitHost);
JitConfig.initialize(jitHost);
g_jitHost = jitHost;
}
return;
}
g_jitHost = jitHost;
assert(!JitConfig.isInitialized());
JitConfig.initialize(jitHost);
#ifdef DEBUG
const WCHAR* jitStdOutFile = JitConfig.JitStdOutFile();
if (jitStdOutFile != nullptr)
{
jitstdout = _wfopen(jitStdOutFile, W("a"));
assert(jitstdout != nullptr);
}
#endif // DEBUG
#if !defined(_HOST_UNIX_)
if (jitstdout == nullptr)
{
int stdoutFd = _fileno(procstdout());
// Check fileno error output(s) -1 may overlap with errno result
// but is included for completness.
// We want to detect the case where the initial handle is null
// or bogus and avoid making further calls.
if ((stdoutFd != -1) && (stdoutFd != -2) && (errno != EINVAL))
{
int jitstdoutFd = _dup(_fileno(procstdout()));
// Check the error status returned by dup.
if (jitstdoutFd != -1)
{
_setmode(jitstdoutFd, _O_TEXT);
jitstdout = _fdopen(jitstdoutFd, "w");
assert(jitstdout != nullptr);
// Prevent the FILE* from buffering its output in order to avoid calls to
// `fflush()` throughout the code.
setvbuf(jitstdout, nullptr, _IONBF, 0);
}
}
}
#endif // !_HOST_UNIX_
// If jitstdout is still null, fallback to whatever procstdout() was
// initially set to.
if (jitstdout == nullptr)
{
jitstdout = procstdout();
}
#ifdef FEATURE_TRACELOGGING
JitTelemetry::NotifyDllProcessAttach();
#endif
Compiler::compStartup();
g_jitInitialized = true;
}
void jitShutdown(bool processIsTerminating)
{
if (!g_jitInitialized)
{
return;
}
Compiler::compShutdown();
if (jitstdout != procstdout())
{
// When the process is terminating, the fclose call is unnecessary and is also prone to
// crashing since the UCRT itself often frees the backing memory earlier on in the
// termination sequence.
if (!processIsTerminating)
{
fclose(jitstdout);
}
}
#ifdef FEATURE_TRACELOGGING
JitTelemetry::NotifyDllProcessDetach();
#endif
g_jitInitialized = false;
}
#ifndef FEATURE_MERGE_JIT_AND_ENGINE
extern "C"
#ifdef FEATURE_PAL
DLLEXPORT // For Win32 PAL LoadLibrary emulation
#endif
BOOL WINAPI
DllMain(HANDLE hInstance, DWORD dwReason, LPVOID pvReserved)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
g_hInst = (HINSTANCE)hInstance;
DisableThreadLibraryCalls((HINSTANCE)hInstance);
}
else if (dwReason == DLL_PROCESS_DETACH)
{
// From MSDN: If fdwReason is DLL_PROCESS_DETACH, lpvReserved is NULL if FreeLibrary has
// been called or the DLL load failed and non-NULL if the process is terminating.
bool processIsTerminating = (pvReserved != nullptr);
jitShutdown(processIsTerminating);
}
return TRUE;
}
HINSTANCE GetModuleInst()
{
return (g_hInst);
}
#ifndef FEATURE_CORECLR
extern "C" DLLEXPORT void __stdcall sxsJitStartup(CoreClrCallbacks const& cccallbacks)
{
#ifndef SELF_NO_HOST
InitUtilcode(cccallbacks);
#endif
}
#endif // FEATURE_CORECLR
#endif // !FEATURE_MERGE_JIT_AND_ENGINE
/*****************************************************************************/
struct CILJitSingletonAllocator
{
int x;
};
const CILJitSingletonAllocator CILJitSingleton = {0};
void* __cdecl operator new(size_t, const CILJitSingletonAllocator&)
{
static char CILJitBuff[sizeof(CILJit)];
return CILJitBuff;
}
ICorJitCompiler* g_realJitCompiler = nullptr;
DLLEXPORT ICorJitCompiler* __stdcall getJit()
{
if (ILJitter == nullptr)
{
ILJitter = new (CILJitSingleton) CILJit();
}
return (ILJitter);
}
/*****************************************************************************/
// Information kept in thread-local storage. This is used in the noway_assert exceptional path.
// If you are using it more broadly in retail code, you would need to understand the
// performance implications of accessing TLS.
#ifndef __GNUC__
__declspec(thread) void* gJitTls = nullptr;
#else // !__GNUC__
thread_local void* gJitTls = nullptr;
#endif // !__GNUC__
static void* GetJitTls()
{
return gJitTls;
}
void SetJitTls(void* value)
{
gJitTls = value;
}
#if defined(DEBUG)
JitTls::JitTls(ICorJitInfo* jitInfo) : m_compiler(nullptr), m_logEnv(jitInfo)
{
m_next = reinterpret_cast<JitTls*>(GetJitTls());
SetJitTls(this);
}
JitTls::~JitTls()
{
SetJitTls(m_next);
}
LogEnv* JitTls::GetLogEnv()
{
return &reinterpret_cast<JitTls*>(GetJitTls())->m_logEnv;
}
Compiler* JitTls::GetCompiler()
{
return reinterpret_cast<JitTls*>(GetJitTls())->m_compiler;
}
void JitTls::SetCompiler(Compiler* compiler)
{
reinterpret_cast<JitTls*>(GetJitTls())->m_compiler = compiler;
}
#else // !defined(DEBUG)
JitTls::JitTls(ICorJitInfo* jitInfo)
{
}
JitTls::~JitTls()
{
}
Compiler* JitTls::GetCompiler()
{
return reinterpret_cast<Compiler*>(GetJitTls());
}
void JitTls::SetCompiler(Compiler* compiler)
{
SetJitTls(compiler);
}
#endif // !defined(DEBUG)
//****************************************************************************
// The main JIT function for the 32 bit JIT. See code:ICorJitCompiler#EEToJitInterface for more on the EE-JIT
// interface. Things really don't get going inside the JIT until the code:Compiler::compCompile#Phases
// method. Usually that is where you want to go.
CorJitResult CILJit::compileMethod(
ICorJitInfo* compHnd, CORINFO_METHOD_INFO* methodInfo, unsigned flags, BYTE** entryAddress, ULONG* nativeSizeOfCode)
{
if (g_realJitCompiler != nullptr)
{
return g_realJitCompiler->compileMethod(compHnd, methodInfo, flags, entryAddress, nativeSizeOfCode);
}
JitFlags jitFlags;
assert(flags == CORJIT_FLAGS::CORJIT_FLAG_CALL_GETJITFLAGS);
CORJIT_FLAGS corJitFlags;
DWORD jitFlagsSize = compHnd->getJitFlags(&corJitFlags, sizeof(corJitFlags));
assert(jitFlagsSize == sizeof(corJitFlags));
jitFlags.SetFromFlags(corJitFlags);
int result;
void* methodCodePtr = nullptr;
CORINFO_METHOD_HANDLE methodHandle = methodInfo->ftn;
JitTls jitTls(compHnd); // Initialize any necessary thread-local state
assert(methodInfo->ILCode);
result = jitNativeCode(methodHandle, methodInfo->scope, compHnd, methodInfo, &methodCodePtr, nativeSizeOfCode,
&jitFlags, nullptr);
if (result == CORJIT_OK)
{
*entryAddress = (BYTE*)methodCodePtr;
}
return CorJitResult(result);
}
/*****************************************************************************
* Notification from VM to clear any caches
*/
void CILJit::clearCache(void)
{
if (g_realJitCompiler != nullptr)
{
g_realJitCompiler->clearCache();
// Continue...
}
return;
}
/*****************************************************************************
* Notify vm that we have something to clean up
*/
BOOL CILJit::isCacheCleanupRequired(void)
{
if (g_realJitCompiler != nullptr)
{
if (g_realJitCompiler->isCacheCleanupRequired())
{
return TRUE;
}
// Continue...
}
return FALSE;
}
void CILJit::ProcessShutdownWork(ICorStaticInfo* statInfo)
{
if (g_realJitCompiler != nullptr)
{
g_realJitCompiler->ProcessShutdownWork(statInfo);
// Continue, by shutting down this JIT as well.
}
jitShutdown(false);
Compiler::ProcessShutdownWork(statInfo);
}
/*****************************************************************************
* Verify the JIT/EE interface identifier.
*/
void CILJit::getVersionIdentifier(GUID* versionIdentifier)
{
if (g_realJitCompiler != nullptr)
{
g_realJitCompiler->getVersionIdentifier(versionIdentifier);
return;
}
assert(versionIdentifier != nullptr);
memcpy(versionIdentifier, &JITEEVersionIdentifier, sizeof(GUID));
}
/*****************************************************************************
* Determine the maximum length of SIMD vector supported by this JIT.
*/
unsigned CILJit::getMaxIntrinsicSIMDVectorLength(CORJIT_FLAGS cpuCompileFlags)
{
if (g_realJitCompiler != nullptr)
{
return g_realJitCompiler->getMaxIntrinsicSIMDVectorLength(cpuCompileFlags);
}
JitFlags jitFlags;
jitFlags.SetFromFlags(cpuCompileFlags);
#ifdef FEATURE_SIMD
#if defined(_TARGET_XARCH_)
if (!jitFlags.IsSet(JitFlags::JIT_FLAG_PREJIT) && jitFlags.IsSet(JitFlags::JIT_FLAG_FEATURE_SIMD) &&
jitFlags.IsSet(JitFlags::JIT_FLAG_USE_AVX2))
{
// Since the ISAs can be disabled individually and since they are hierarchical in nature (that is
// disabling SSE also disables SSE2 through AVX2), we need to check each ISA in the hierarchy to
// ensure that AVX2 is actually supported. Otherwise, we will end up getting asserts downstream.
if ((JitConfig.EnableAVX2() != 0) && (JitConfig.EnableAVX() != 0) && (JitConfig.EnableSSE42() != 0) &&
(JitConfig.EnableSSE41() != 0) && (JitConfig.EnableSSSE3() != 0) && (JitConfig.EnableSSE3_4() != 0) &&
(JitConfig.EnableSSE3() != 0) && (JitConfig.EnableSSE2() != 0) && (JitConfig.EnableSSE() != 0))
{
if (GetJitTls() != nullptr && JitTls::GetCompiler() != nullptr)
{
JITDUMP("getMaxIntrinsicSIMDVectorLength: returning 32\n");
}
return 32;
}
}
#endif // defined(_TARGET_XARCH_)
if (GetJitTls() != nullptr && JitTls::GetCompiler() != nullptr)
{
JITDUMP("getMaxIntrinsicSIMDVectorLength: returning 16\n");
}
return 16;
#else // !FEATURE_SIMD
if (GetJitTls() != nullptr && JitTls::GetCompiler() != nullptr)
{
JITDUMP("getMaxIntrinsicSIMDVectorLength: returning 0\n");
}
return 0;
#endif // !FEATURE_SIMD
}
void CILJit::setRealJit(ICorJitCompiler* realJitCompiler)
{
g_realJitCompiler = realJitCompiler;
}
/*****************************************************************************
* Returns the number of bytes required for the given type argument
*/
unsigned Compiler::eeGetArgSize(CORINFO_ARG_LIST_HANDLE list, CORINFO_SIG_INFO* sig)
{
#if defined(_TARGET_AMD64_)
// Everything fits into a single 'slot' size
// to accommodate irregular sized structs, they are passed byref
CLANG_FORMAT_COMMENT_ANCHOR;
#ifdef UNIX_AMD64_ABI
CORINFO_CLASS_HANDLE argClass;
CorInfoType argTypeJit = strip(info.compCompHnd->getArgType(sig, list, &argClass));
var_types argType = JITtype2varType(argTypeJit);
if (varTypeIsStruct(argType))
{
unsigned structSize = info.compCompHnd->getClassSize(argClass);
return structSize; // TODO: roundUp() needed here?
}
#endif // UNIX_AMD64_ABI
return TARGET_POINTER_SIZE;
#else // !_TARGET_AMD64_
CORINFO_CLASS_HANDLE argClass;
CorInfoType argTypeJit = strip(info.compCompHnd->getArgType(sig, list, &argClass));
var_types argType = JITtype2varType(argTypeJit);
if (varTypeIsStruct(argType))
{
unsigned structSize = info.compCompHnd->getClassSize(argClass);
// make certain the EE passes us back the right thing for refanys
assert(argTypeJit != CORINFO_TYPE_REFANY || structSize == 2 * TARGET_POINTER_SIZE);
// For each target that supports passing struct args in multiple registers
// apply the target specific rules for them here:
CLANG_FORMAT_COMMENT_ANCHOR;
#if FEATURE_MULTIREG_ARGS
#if defined(_TARGET_ARM64_)
// Any structs that are larger than MAX_PASS_MULTIREG_BYTES are always passed by reference
if (structSize > MAX_PASS_MULTIREG_BYTES)
{
// This struct is passed by reference using a single 'slot'
return TARGET_POINTER_SIZE;
}
else
{
// Is the struct larger than 16 bytes
if (structSize > (2 * TARGET_POINTER_SIZE))
{
var_types hfaType = GetHfaType(argClass); // set to float or double if it is an HFA, otherwise TYP_UNDEF
bool isHfa = (hfaType != TYP_UNDEF);
#ifndef _TARGET_UNIX_
if (info.compIsVarArgs)
{
// Arm64 Varargs ABI requires passing in general purpose
// registers. Force the decision of whether this is an HFA
// to false to correctly pass as if it was not an HFA.
isHfa = false;
}
#endif // _TARGET_UNIX_
if (!isHfa)
{
// This struct is passed by reference using a single 'slot'
return TARGET_POINTER_SIZE;
}
}
// otherwise will we pass this struct by value in multiple registers
}
#elif defined(_TARGET_ARM_)
// otherwise will we pass this struct by value in multiple registers
#else
NYI("unknown target");
#endif // defined(_TARGET_XXX_)
#endif // FEATURE_MULTIREG_ARGS
// we pass this struct by value in multiple registers
return roundUp(structSize, TARGET_POINTER_SIZE);
}
else
{
unsigned argSize = sizeof(int) * genTypeStSz(argType);
assert(0 < argSize && argSize <= sizeof(__int64));
return roundUp(argSize, TARGET_POINTER_SIZE);
}
#endif
}
/*****************************************************************************/
GenTree* Compiler::eeGetPInvokeCookie(CORINFO_SIG_INFO* szMetaSig)
{
void *cookie, *pCookie;
cookie = info.compCompHnd->GetCookieForPInvokeCalliSig(szMetaSig, &pCookie);
assert((cookie == nullptr) != (pCookie == nullptr));
return gtNewIconEmbHndNode(cookie, pCookie, GTF_ICON_PINVKI_HDL, szMetaSig);
}
//------------------------------------------------------------------------
// eeGetArrayDataOffset: Gets the offset of a SDArray's first element
//
// Arguments:
// type - The array element type
//
// Return Value:
// The offset to the first array element.
unsigned Compiler::eeGetArrayDataOffset(var_types type)
{
return varTypeIsGC(type) ? eeGetEEInfo()->offsetOfObjArrayData : OFFSETOF__CORINFO_Array__data;
}
//------------------------------------------------------------------------
// eeGetMDArrayDataOffset: Gets the offset of a MDArray's first element
//
// Arguments:
// type - The array element type
// rank - The array rank
//
// Return Value:
// The offset to the first array element.
//
// Assumptions:
// The rank should be greater than 0.
unsigned Compiler::eeGetMDArrayDataOffset(var_types type, unsigned rank)
{
assert(rank > 0);
// Note that below we're specifically using genTypeSize(TYP_INT) because array
// indices are not native int.
return eeGetArrayDataOffset(type) + 2 * genTypeSize(TYP_INT) * rank;
}
/*****************************************************************************/
void Compiler::eeGetStmtOffsets()
{
ULONG32 offsetsCount;
DWORD* offsets;
ICorDebugInfo::BoundaryTypes offsetsImplicit;
info.compCompHnd->getBoundaries(info.compMethodHnd, &offsetsCount, &offsets, &offsetsImplicit);
/* Set the implicit boundaries */
info.compStmtOffsetsImplicit = (ICorDebugInfo::BoundaryTypes)offsetsImplicit;
/* Process the explicit boundaries */
info.compStmtOffsetsCount = 0;
if (offsetsCount == 0)
{
return;
}
info.compStmtOffsets = new (this, CMK_DebugInfo) IL_OFFSET[offsetsCount];
for (unsigned i = 0; i < offsetsCount; i++)
{
if (offsets[i] > info.compILCodeSize)
{
continue;
}
info.compStmtOffsets[info.compStmtOffsetsCount] = offsets[i];
info.compStmtOffsetsCount++;
}
info.compCompHnd->freeArray(offsets);
}
/*****************************************************************************
*
* Debugging support - Local var info
*/
void Compiler::eeSetLVcount(unsigned count)
{
assert(opts.compScopeInfo);
JITDUMP("VarLocInfo count is %d\n", count);
eeVarsCount = count;
if (eeVarsCount)
{
eeVars = (VarResultInfo*)info.compCompHnd->allocateArray(eeVarsCount * sizeof(eeVars[0]));
}
else
{
eeVars = nullptr;
}
}
void Compiler::eeSetLVinfo(unsigned which,
UNATIVE_OFFSET startOffs,
UNATIVE_OFFSET length,
unsigned varNum,
const CodeGenInterface::siVarLoc& varLoc)
{
// ICorDebugInfo::VarLoc and CodeGenInterface::siVarLoc have to overlap
// This is checked in siInit()
assert(opts.compScopeInfo);
assert(eeVarsCount > 0);
assert(which < eeVarsCount);
if (eeVars != nullptr)
{
eeVars[which].startOffset = startOffs;
eeVars[which].endOffset = startOffs + length;
eeVars[which].varNumber = varNum;
eeVars[which].loc = varLoc;
}
}
void Compiler::eeSetLVdone()
{
// necessary but not sufficient condition that the 2 struct definitions overlap
assert(sizeof(eeVars[0]) == sizeof(ICorDebugInfo::NativeVarInfo));
assert(opts.compScopeInfo);
#ifdef DEBUG
if (verbose || opts.dspDebugInfo)
{
eeDispVars(info.compMethodHnd, eeVarsCount, (ICorDebugInfo::NativeVarInfo*)eeVars);
}
#endif // DEBUG
info.compCompHnd->setVars(info.compMethodHnd, eeVarsCount, (ICorDebugInfo::NativeVarInfo*)eeVars);
eeVars = nullptr; // We give up ownership after setVars()
}
void Compiler::eeGetVars()
{
ICorDebugInfo::ILVarInfo* varInfoTable;
ULONG32 varInfoCount;
bool extendOthers;
info.compCompHnd->getVars(info.compMethodHnd, &varInfoCount, &varInfoTable, &extendOthers);
#ifdef DEBUG
if (verbose)
{
printf("getVars() returned cVars = %d, extendOthers = %s\n", varInfoCount, extendOthers ? "true" : "false");
}
#endif
// Over allocate in case extendOthers is set.
SIZE_T varInfoCountExtra = varInfoCount;
if (extendOthers)
{
varInfoCountExtra += info.compLocalsCount;
}
if (varInfoCountExtra == 0)
{
return;
}
info.compVarScopes = new (this, CMK_DebugInfo) VarScopeDsc[varInfoCountExtra];
VarScopeDsc* localVarPtr = info.compVarScopes;
ICorDebugInfo::ILVarInfo* v = varInfoTable;
for (unsigned i = 0; i < varInfoCount; i++, v++)
{
#ifdef DEBUG
if (verbose)
{
printf("var:%d start:%d end:%d\n", v->varNumber, v->startOffset, v->endOffset);
}
#endif
if (v->startOffset >= v->endOffset)
{
continue;
}
assert(v->startOffset <= info.compILCodeSize);
assert(v->endOffset <= info.compILCodeSize);
localVarPtr->vsdLifeBeg = v->startOffset;
localVarPtr->vsdLifeEnd = v->endOffset;
localVarPtr->vsdLVnum = i;
localVarPtr->vsdVarNum = compMapILvarNum(v->varNumber);
#ifdef DEBUG
localVarPtr->vsdName = gtGetLclVarName(localVarPtr->vsdVarNum);
#endif
localVarPtr++;
info.compVarScopesCount++;
}
/* If extendOthers is set, then assume the scope of unreported vars
is the entire method. Note that this will cause fgExtendDbgLifetimes()
to zero-initalize all of them. This will be expensive if it's used
for too many variables.
*/
if (extendOthers)
{
// Allocate a bit-array for all the variables and initialize to false
bool* varInfoProvided = getAllocator(CMK_Unknown).allocate<bool>(info.compLocalsCount);
unsigned i;
for (i = 0; i < info.compLocalsCount; i++)
{
varInfoProvided[i] = false;
}
// Find which vars have absolutely no varInfo provided
for (i = 0; i < info.compVarScopesCount; i++)
{
varInfoProvided[info.compVarScopes[i].vsdVarNum] = true;
}
// Create entries for the variables with no varInfo
for (unsigned varNum = 0; varNum < info.compLocalsCount; varNum++)
{
if (varInfoProvided[varNum])
{
continue;
}
// Create a varInfo with scope over the entire method
localVarPtr->vsdLifeBeg = 0;
localVarPtr->vsdLifeEnd = info.compILCodeSize;
localVarPtr->vsdVarNum = varNum;
localVarPtr->vsdLVnum = info.compVarScopesCount;
#ifdef DEBUG
localVarPtr->vsdName = gtGetLclVarName(localVarPtr->vsdVarNum);
#endif
localVarPtr++;
info.compVarScopesCount++;
}
}
assert(localVarPtr <= info.compVarScopes + varInfoCountExtra);
if (varInfoCount != 0)
{
info.compCompHnd->freeArray(varInfoTable);
}
#ifdef DEBUG
if (verbose)
{
compDispLocalVars();
}
#endif // DEBUG
}
#ifdef DEBUG
void Compiler::eeDispVar(ICorDebugInfo::NativeVarInfo* var)
{
const char* name = nullptr;
if (var->varNumber == (DWORD)ICorDebugInfo::VARARGS_HND_ILNUM)
{
name = "varargsHandle";
}
else if (var->varNumber == (DWORD)ICorDebugInfo::RETBUF_ILNUM)
{
name = "retBuff";
}
else if (var->varNumber == (DWORD)ICorDebugInfo::TYPECTXT_ILNUM)
{
name = "typeCtx";
}
printf("%3d(%10s) : From %08Xh to %08Xh, in ", var->varNumber,
(VarNameToStr(name) == nullptr) ? "UNKNOWN" : VarNameToStr(name), var->startOffset, var->endOffset);
switch ((CodeGenInterface::siVarLocType)var->loc.vlType)
{
case CodeGenInterface::VLT_REG:
case CodeGenInterface::VLT_REG_BYREF:
case CodeGenInterface::VLT_REG_FP:
printf("%s", getRegName(var->loc.vlReg.vlrReg));
if (var->loc.vlType == (ICorDebugInfo::VarLocType)CodeGenInterface::VLT_REG_BYREF)
{
printf(" byref");
}
break;
case CodeGenInterface::VLT_STK:
case CodeGenInterface::VLT_STK_BYREF:
if ((int)var->loc.vlStk.vlsBaseReg != (int)ICorDebugInfo::REGNUM_AMBIENT_SP)
{
printf("%s[%d] (1 slot)", getRegName(var->loc.vlStk.vlsBaseReg), var->loc.vlStk.vlsOffset);
}
else
{
printf(STR_SPBASE "'[%d] (1 slot)", var->loc.vlStk.vlsOffset);
}
if (var->loc.vlType == (ICorDebugInfo::VarLocType)CodeGenInterface::VLT_REG_BYREF)
{
printf(" byref");
}
break;
#ifndef _TARGET_AMD64_
case CodeGenInterface::VLT_REG_REG:
printf("%s-%s", getRegName(var->loc.vlRegReg.vlrrReg1), getRegName(var->loc.vlRegReg.vlrrReg2));
break;
case CodeGenInterface::VLT_REG_STK:
if ((int)var->loc.vlRegStk.vlrsStk.vlrssBaseReg != (int)ICorDebugInfo::REGNUM_AMBIENT_SP)
{
printf("%s-%s[%d]", getRegName(var->loc.vlRegStk.vlrsReg),
getRegName(var->loc.vlRegStk.vlrsStk.vlrssBaseReg), var->loc.vlRegStk.vlrsStk.vlrssOffset);
}
else
{
printf("%s-" STR_SPBASE "'[%d]", getRegName(var->loc.vlRegStk.vlrsReg),
var->loc.vlRegStk.vlrsStk.vlrssOffset);
}
break;
case CodeGenInterface::VLT_STK_REG:
unreached(); // unexpected
case CodeGenInterface::VLT_STK2:
if ((int)var->loc.vlStk2.vls2BaseReg != (int)ICorDebugInfo::REGNUM_AMBIENT_SP)
{
printf("%s[%d] (2 slots)", getRegName(var->loc.vlStk2.vls2BaseReg), var->loc.vlStk2.vls2Offset);
}
else
{
printf(STR_SPBASE "'[%d] (2 slots)", var->loc.vlStk2.vls2Offset);
}
break;
case CodeGenInterface::VLT_FPSTK:
printf("ST(L-%d)", var->loc.vlFPstk.vlfReg);
break;
case CodeGenInterface::VLT_FIXED_VA:
printf("fxd_va[%d]", var->loc.vlFixedVarArg.vlfvOffset);
break;
#endif // !_TARGET_AMD64_
default:
unreached(); // unexpected
}
printf("\n");
}
// Same parameters as ICorStaticInfo::setVars().
void Compiler::eeDispVars(CORINFO_METHOD_HANDLE ftn, ULONG32 cVars, ICorDebugInfo::NativeVarInfo* vars)
{
printf("*************** Variable debug info\n");
printf("%d live ranges\n", cVars);
for (unsigned i = 0; i < cVars; i++)
{
eeDispVar(&vars[i]);
}
}
#endif // DEBUG
/*****************************************************************************
*
* Debugging support - Line number info
*/
void Compiler::eeSetLIcount(unsigned count)
{
assert(opts.compDbgInfo);
eeBoundariesCount = count;
if (eeBoundariesCount)
{
eeBoundaries = (boundariesDsc*)info.compCompHnd->allocateArray(eeBoundariesCount * sizeof(eeBoundaries[0]));
}
else
{
eeBoundaries = nullptr;
}
}
void Compiler::eeSetLIinfo(
unsigned which, UNATIVE_OFFSET nativeOffset, IL_OFFSET ilOffset, bool stkEmpty, bool callInstruction)
{
assert(opts.compDbgInfo);
assert(eeBoundariesCount > 0);
assert(which < eeBoundariesCount);
if (eeBoundaries != nullptr)
{
eeBoundaries[which].nativeIP = nativeOffset;
eeBoundaries[which].ilOffset = ilOffset;
eeBoundaries[which].sourceReason = stkEmpty ? ICorDebugInfo::STACK_EMPTY : 0;
eeBoundaries[which].sourceReason |= callInstruction ? ICorDebugInfo::CALL_INSTRUCTION : 0;
}
}
void Compiler::eeSetLIdone()
{
assert(opts.compDbgInfo);
#if defined(DEBUG)
if (verbose || opts.dspDebugInfo)
{
eeDispLineInfos();
}
#endif // DEBUG
// necessary but not sufficient condition that the 2 struct definitions overlap
assert(sizeof(eeBoundaries[0]) == sizeof(ICorDebugInfo::OffsetMapping));
info.compCompHnd->setBoundaries(info.compMethodHnd, eeBoundariesCount, (ICorDebugInfo::OffsetMapping*)eeBoundaries);
eeBoundaries = nullptr; // we give up ownership after setBoundaries();
}
#if defined(DEBUG)
/* static */
void Compiler::eeDispILOffs(IL_OFFSET offs)
{
const char* specialOffs[] = {"EPILOG", "PROLOG", "NO_MAP"};
switch ((int)offs) // Need the cast since offs is unsigned and the case statements are comparing to signed.
{
case ICorDebugInfo::EPILOG:
case ICorDebugInfo::PROLOG:
case ICorDebugInfo::NO_MAPPING:
assert(DWORD(ICorDebugInfo::EPILOG) + 1 == (unsigned)ICorDebugInfo::PROLOG);
assert(DWORD(ICorDebugInfo::EPILOG) + 2 == (unsigned)ICorDebugInfo::NO_MAPPING);
int specialOffsNum;
specialOffsNum = offs - DWORD(ICorDebugInfo::EPILOG);
printf("%s", specialOffs[specialOffsNum]);
break;
default:
printf("0x%04X", offs);
}
}
/* static */
void Compiler::eeDispLineInfo(const boundariesDsc* line)
{
printf("IL offs ");
eeDispILOffs(line->ilOffset);
printf(" : 0x%08X", line->nativeIP);
if (line->sourceReason != 0)
{
// It seems like it should probably never be zero since ICorDebugInfo::SOURCE_TYPE_INVALID is zero.