forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheeinterface.cpp
618 lines (546 loc) · 19.1 KB
/
eeinterface.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX XX
XX EEInterface XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
//------------------------------------------------------------------------
// StringPrinter::Grow:
// Grow the internal buffer to a new specified size.
//
// Arguments:
// newSize - the new size.
//
void StringPrinter::Grow(size_t newSize)
{
assert(newSize > m_bufferMax);
char* newBuffer = m_alloc.allocate<char>(newSize);
memcpy(newBuffer, m_buffer, m_bufferIndex + 1); // copy null terminator too
m_buffer = newBuffer;
m_bufferMax = newSize;
}
//------------------------------------------------------------------------
// StringPrinter::Append:
// Append a substring to the internal buffer.
//
// Arguments:
// str - the substring to append
//
void StringPrinter::Append(const char* str)
{
size_t strLen = strlen(str);
size_t newIndex = m_bufferIndex + strLen;
if (newIndex >= m_bufferMax)
{
size_t newSize = m_bufferMax * 2;
while (newIndex >= newSize)
{
newSize *= 2;
}
Grow(newSize);
}
memcpy(&m_buffer[m_bufferIndex], str, strLen + 1);
m_bufferIndex += strLen;
}
//------------------------------------------------------------------------
// StringPrinter::Append:
// Append a single character to the internal buffer.
//
// Arguments:
// chr - the character
//
void StringPrinter::Append(char chr)
{
if (m_bufferIndex + 1 >= m_bufferMax)
{
Grow(m_bufferMax * 2);
}
m_buffer[m_bufferIndex] = chr;
m_buffer[m_bufferIndex + 1] = '\0';
m_bufferIndex++;
}
//------------------------------------------------------------------------
// eePrintJitType:
// Print a JIT type.
//
// Arguments:
// printer - the printer
// jitType - the JIT type
//
void Compiler::eePrintJitType(StringPrinter* printer, var_types jitType)
{
printer->Append(varTypeName(jitType));
}
//------------------------------------------------------------------------
// eeAppendPrint:
// Append the output of one of the JIT-EE 'print' functions to a StringPrinter.
//
// Arguments:
// printer - the printer
// print - A functor to print the string that follows the conventions of the JIT-EE print* functions.
//
template <typename TPrint>
void Compiler::eeAppendPrint(StringPrinter* printer, TPrint print)
{
size_t requiredBufferSize;
char buffer[256];
size_t printed = print(buffer, sizeof(buffer), &requiredBufferSize);
if (requiredBufferSize <= sizeof(buffer))
{
assert(printed == requiredBufferSize - 1);
printer->Append(buffer);
}
else
{
char* pBuffer = new (this, CMK_DebugOnly) char[requiredBufferSize];
printed = print(pBuffer, requiredBufferSize, nullptr);
assert(printed == requiredBufferSize - 1);
printer->Append(pBuffer);
}
}
//------------------------------------------------------------------------
// eePrintType:
// Print a type given by a class handle.
//
// Arguments:
// printer - the printer
// clsHnd - Handle for the class
// includeNamespace - Whether to print namespaces before type names
// includeInstantiation - Whether to print the instantiation of the class
//
void Compiler::eePrintType(StringPrinter* printer, CORINFO_CLASS_HANDLE clsHnd, bool includeInstantiation)
{
unsigned arrayRank = info.compCompHnd->getArrayRank(clsHnd);
if (arrayRank > 0)
{
CORINFO_CLASS_HANDLE childClsHnd;
CorInfoType childType = info.compCompHnd->getChildType(clsHnd, &childClsHnd);
if ((childType == CORINFO_TYPE_CLASS) || (childType == CORINFO_TYPE_VALUECLASS))
{
eePrintType(printer, childClsHnd, includeInstantiation);
}
else
{
eePrintJitType(printer, JitType2PreciseVarType(childType));
}
printer->Append('[');
for (unsigned i = 1; i < arrayRank; i++)
{
printer->Append(',');
}
printer->Append(']');
return;
}
eeAppendPrint(printer, [&](char* buffer, size_t bufferSize, size_t* requiredBufferSize) {
return info.compCompHnd->printClassName(clsHnd, buffer, bufferSize, requiredBufferSize);
});
if (!includeInstantiation)
{
return;
}
char pref = '[';
for (unsigned typeArgIndex = 0;; typeArgIndex++)
{
CORINFO_CLASS_HANDLE typeArg = info.compCompHnd->getTypeInstantiationArgument(clsHnd, typeArgIndex);
if (typeArg == NO_CLASS_HANDLE)
{
break;
}
printer->Append(pref);
pref = ',';
eePrintTypeOrJitAlias(printer, typeArg, true);
}
if (pref != '[')
{
printer->Append(']');
}
}
//------------------------------------------------------------------------
// eePrintTypeOrJitAlias:
// Print a type given by a class handle. If the type is a primitive type,
// prints its JIT alias.
//
// Arguments:
// printer - the printer
// clsHnd - Handle for the class
// includeInstantiation - Whether to print the instantiation of the class
//
void Compiler::eePrintTypeOrJitAlias(StringPrinter* printer, CORINFO_CLASS_HANDLE clsHnd, bool includeInstantiation)
{
CorInfoType typ = info.compCompHnd->asCorInfoType(clsHnd);
if ((typ == CORINFO_TYPE_CLASS) || (typ == CORINFO_TYPE_VALUECLASS))
{
eePrintType(printer, clsHnd, includeInstantiation);
}
else
{
eePrintJitType(printer, JitType2PreciseVarType(typ));
}
}
static const char* s_jitHelperNames[CORINFO_HELP_COUNT] = {
#define JITHELPER(code, pfnHelper, sig) #code,
#define DYNAMICJITHELPER(code, pfnHelper, sig) #code,
#include "jithelpers.h"
};
//------------------------------------------------------------------------
// eePrintMethod:
// Print a method given by a method handle, its owning class handle and its
// signature.
//
// Arguments:
// printer - the printer
// clsHnd - Handle for the owning class, or NO_CLASS_HANDLE to not print the class.
// sig - The signature of the method.
// includeClassInstantiation - Whether to print the class instantiation. Only valid when clsHnd is passed.
// includeMethodInstantiation - Whether to print the method instantiation. Requires the signature to be passed.
// includeSignature - Whether to print the signature.
// includeReturnType - Whether to include the return type at the end.
// includeThisSpecifier - Whether to include a specifier at the end for whether the method is an instance
// method.
//
void Compiler::eePrintMethod(StringPrinter* printer,
CORINFO_CLASS_HANDLE clsHnd,
CORINFO_METHOD_HANDLE methHnd,
CORINFO_SIG_INFO* sig,
bool includeClassInstantiation,
bool includeMethodInstantiation,
bool includeSignature,
bool includeReturnType,
bool includeThisSpecifier)
{
CorInfoHelpFunc helper = eeGetHelperNum(methHnd);
if (helper != CORINFO_HELP_UNDEF)
{
assert(helper < CORINFO_HELP_COUNT);
printer->Append(s_jitHelperNames[helper]);
return;
}
if (clsHnd != NO_CLASS_HANDLE)
{
eePrintType(printer, clsHnd, includeClassInstantiation);
printer->Append(':');
}
eeAppendPrint(printer, [&](char* buffer, size_t bufferSize, size_t* requiredBufferSize) {
return info.compCompHnd->printMethodName(methHnd, buffer, bufferSize, requiredBufferSize);
});
if (includeMethodInstantiation && (sig->sigInst.methInstCount > 0))
{
printer->Append('[');
for (unsigned i = 0; i < sig->sigInst.methInstCount; i++)
{
if (i > 0)
{
printer->Append(',');
}
eePrintTypeOrJitAlias(printer, sig->sigInst.methInst[i], true);
}
printer->Append(']');
}
if (includeSignature)
{
printer->Append('(');
CORINFO_ARG_LIST_HANDLE argLst = sig->args;
for (unsigned i = 0; i < sig->numArgs; i++)
{
if (i > 0)
printer->Append(',');
CORINFO_CLASS_HANDLE vcClsHnd;
var_types type = JitType2PreciseVarType(strip(info.compCompHnd->getArgType(sig, argLst, &vcClsHnd)));
switch (type)
{
case TYP_REF:
case TYP_STRUCT:
{
CORINFO_CLASS_HANDLE clsHnd = eeGetArgClass(sig, argLst);
// For some SIMD struct types we can get a nullptr back from eeGetArgClass on Linux/X64
if (clsHnd != NO_CLASS_HANDLE)
{
eePrintType(printer, clsHnd, true);
break;
}
}
FALLTHROUGH;
default:
eePrintJitType(printer, type);
break;
}
argLst = info.compCompHnd->getArgNext(argLst);
}
printer->Append(')');
if (includeReturnType)
{
var_types retType = JitType2PreciseVarType(sig->retType);
if (retType != TYP_VOID)
{
printer->Append(':');
switch (retType)
{
case TYP_REF:
case TYP_STRUCT:
{
CORINFO_CLASS_HANDLE clsHnd = sig->retTypeClass;
if (clsHnd != NO_CLASS_HANDLE)
{
eePrintType(printer, clsHnd, true);
break;
}
}
FALLTHROUGH;
default:
eePrintJitType(printer, retType);
break;
}
}
}
// Does it have a 'this' pointer? Don't count explicit this, which has
// the this pointer type as the first element of the arg type list
if (includeThisSpecifier && sig->hasThis() && !sig->hasExplicitThis())
{
printer->Append(":this");
}
}
}
//------------------------------------------------------------------------
// eePrintField:
// Print a field name to a StringPrinter.
//
// Arguments:
// printer - the printer
// fld - The field
// includeType - Whether to prefix the string by <class name>:
//
void Compiler::eePrintField(StringPrinter* printer, CORINFO_FIELD_HANDLE fld, bool includeType)
{
if (includeType)
{
CORINFO_CLASS_HANDLE cls = info.compCompHnd->getFieldClass(fld);
eePrintType(printer, cls, true);
printer->Append(':');
}
eeAppendPrint(printer, [&](char* buffer, size_t bufferSize, size_t* requiredBufferSize) {
return info.compCompHnd->printFieldName(fld, buffer, bufferSize, requiredBufferSize);
});
}
//------------------------------------------------------------------------
// eeGetMethodFullName:
// Get a string describing a method.
//
// Arguments:
// hnd - the method handle
// includeReturnType - Whether to include the return type in the string
// includeThisSpecifier - Whether to include a specifier for whether this is an instance method.
// buffer - Preexisting buffer to use (can be nullptr to allocate on heap).
// bufferSize - Size of preexisting buffer.
//
// Remarks:
// If the final string is larger than the preexisting buffer can contain then
// the string will be jit memory allocated.
//
// Returns:
// The string.
//
const char* Compiler::eeGetMethodFullName(
CORINFO_METHOD_HANDLE hnd, bool includeReturnType, bool includeThisSpecifier, char* buffer, size_t bufferSize)
{
CorInfoHelpFunc helper = eeGetHelperNum(hnd);
if (helper != CORINFO_HELP_UNDEF)
{
return s_jitHelperNames[helper];
}
StringPrinter p(getAllocator(CMK_DebugOnly), buffer, bufferSize);
CORINFO_CLASS_HANDLE clsHnd = NO_CLASS_HANDLE;
bool success = eeRunFunctorWithSPMIErrorTrap([&]() {
clsHnd = info.compCompHnd->getMethodClass(hnd);
CORINFO_SIG_INFO sig;
eeGetMethodSig(hnd, &sig);
eePrintMethod(&p, clsHnd, hnd, &sig,
/* includeClassInstantiation */ true,
/* includeMethodInstantiation */ true,
/* includeSignature */ true, includeReturnType, includeThisSpecifier);
});
if (success)
{
return p.GetBuffer();
}
// Try without signature
p.Truncate(0);
success = eeRunFunctorWithSPMIErrorTrap([&]() {
eePrintMethod(&p, clsHnd, hnd,
/* sig */ nullptr,
/* includeClassInstantiation */ false,
/* includeMethodInstantiation */ false,
/* includeSignature */ false,
/* includeReturnType */ false,
/* includeThisSpecifier */ false);
});
if (success)
{
return p.GetBuffer();
}
// Try with bare minimum
p.Truncate(0);
success = eeRunFunctorWithSPMIErrorTrap([&]() {
eePrintMethod(&p, nullptr, hnd,
/* sig */ nullptr,
/* includeClassInstantiation */ false,
/* includeMethodInstantiation */ false,
/* includeSignature */ false,
/* includeReturnType */ false,
/* includeThisSpecifier */ false);
});
if (success)
{
return p.GetBuffer();
}
p.Truncate(0);
p.Append("<unknown method>");
return p.GetBuffer();
}
//------------------------------------------------------------------------
// eeGetMethodName:
// Get the name of a method.
//
// Arguments:
// hnd - the method handle
// buffer - Preexisting buffer to use (can be nullptr to allocate on heap).
// bufferSize - Size of preexisting buffer.
//
// Remarks:
// See eeGetMethodFullName for documentation about the buffer.
//
// Returns:
// The string.
//
const char* Compiler::eeGetMethodName(CORINFO_METHOD_HANDLE methHnd, char* buffer, size_t bufferSize)
{
StringPrinter p(getAllocator(CMK_DebugOnly), buffer, bufferSize);
bool success = eeRunFunctorWithSPMIErrorTrap([&]() {
eePrintMethod(&p, NO_CLASS_HANDLE, methHnd,
/* sig */ nullptr,
/* includeClassInstantiation */ false,
/* includeMethodInstantiation */ false,
/* includeSignature */ false,
/* includeReturnType */ false,
/* includeThisSpecifier */ false);
});
if (!success)
{
p.Truncate(0);
p.Append("<unknown method>");
}
return p.GetBuffer();
}
//------------------------------------------------------------------------
// eeGetFieldName:
// Get a string describing a field.
//
// Arguments:
// fldHnd - the field handle
// includeType - Whether to prefix the string with <type name>:
// buffer - Preexisting buffer to use (can be nullptr to allocate on heap).
// bufferSize - Size of preexisting buffer.
//
// Remarks:
// See eeGetMethodFullName for documentation about the buffer.
//
// Returns:
// The string.
//
const char* Compiler::eeGetFieldName(CORINFO_FIELD_HANDLE fldHnd, bool includeType, char* buffer, size_t bufferSize)
{
StringPrinter p(getAllocator(CMK_DebugOnly), buffer, bufferSize);
bool success = eeRunFunctorWithSPMIErrorTrap([&]() { eePrintField(&p, fldHnd, includeType); });
if (success)
{
return p.GetBuffer();
}
p.Truncate(0);
if (includeType)
{
p.Append("<unknown class>:");
success = eeRunFunctorWithSPMIErrorTrap([&]() { eePrintField(&p, fldHnd, false); });
if (success)
{
return p.GetBuffer();
}
p.Truncate(0);
}
if (includeType)
{
p.Append("<unknown class>:");
}
p.Append("<unknown field>");
return p.GetBuffer();
}
//------------------------------------------------------------------------
// eeGetClassName:
// Get the name (including namespace and instantiation) of a type.
// If missing information (in SPMI), then return a placeholder string.
//
// Parameters:
// clsHnd - the handle of the class
// buffer - a buffer to use for scratch space, or null pointer to allocate a new string.
// bufferSize - the size of buffer. If the final class name is longer a new string will be allocated.
//
// Return value:
// The name string.
//
const char* Compiler::eeGetClassName(CORINFO_CLASS_HANDLE clsHnd, char* buffer, size_t bufferSize)
{
StringPrinter printer(getAllocator(CMK_DebugOnly), buffer, bufferSize);
if (!eeRunFunctorWithSPMIErrorTrap([&]() { eePrintType(&printer, clsHnd, true); }))
{
printer.Truncate(0);
printer.Append("<unknown class>");
}
return printer.GetBuffer();
}
//------------------------------------------------------------------------
// eeGetShortClassName: Returns class name with no instantiation.
//
// Arguments:
// clsHnd - the class handle to get the type name of
//
// Return value:
// String without instantiation.
//
const char* Compiler::eeGetShortClassName(CORINFO_CLASS_HANDLE clsHnd)
{
StringPrinter printer(getAllocator(CMK_DebugOnly));
if (!eeRunFunctorWithSPMIErrorTrap([&]() { eePrintType(&printer, clsHnd, false); }))
{
printer.Truncate(0);
printer.Append("<unknown class>");
}
return printer.GetBuffer();
}
void Compiler::eePrintObjectDescription(const char* prefix, CORINFO_OBJECT_HANDLE handle)
{
const size_t maxStrSize = 64;
char str[maxStrSize];
size_t actualLen = 0;
// Ignore potential SPMI failures
bool success = eeRunFunctorWithSPMIErrorTrap(
[&]() { actualLen = this->info.compCompHnd->printObjectDescription(handle, str, maxStrSize); });
if (!success)
{
return;
}
for (size_t i = 0; i < actualLen; i++)
{
// Replace \n and \r symbols with whitespaces
if (str[i] == '\n' || str[i] == '\r')
{
str[i] = ' ';
}
}
printf("%s '%s'\n", prefix, str);
}