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
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Internal.TypeSystem;
using Internal.TypeSystem.Ecma;
namespace ILCompiler
{
public static class MethodExtensions
{
public static string GetRuntimeImportName(this EcmaMethod This)
{
var decoded = This.GetDecodedCustomAttribute("System.Runtime", "RuntimeImportAttribute");
if (decoded == null)
return null;
var decodedValue = decoded.Value;
if (decodedValue.FixedArguments.Length != 0)
return (string)decodedValue.FixedArguments[decodedValue.FixedArguments.Length - 1].Value;
return null;
}
public static string GetRuntimeImportDllName(this EcmaMethod This)
{
var decoded = This.GetDecodedCustomAttribute("System.Runtime", "RuntimeImportAttribute");
if (decoded == null)
return null;
var decodedValue = decoded.Value;
if (decodedValue.FixedArguments.Length == 2)
return (string)decodedValue.FixedArguments[0].Value;
return null;
}
public static string GetRuntimeExportName(this EcmaMethod This)
{
var decoded = This.GetDecodedCustomAttribute("System.Runtime", "RuntimeExportAttribute");
if (decoded == null)
return null;
var decodedValue = decoded.Value;
if (decodedValue.FixedArguments.Length != 0)
return (string)decodedValue.FixedArguments[0].Value;
foreach (var argument in decodedValue.NamedArguments)
{
if (argument.Name == "EntryPoint")
return (string)argument.Value;
}
return null;
}
public static string GetUnmanagedCallersOnlyExportName(this EcmaMethod This)
{
var decoded = This.GetDecodedCustomAttribute("System.Runtime.InteropServices", "UnmanagedCallersOnlyAttribute");
if (decoded == null)
return null;
var decodedValue = decoded.Value;
foreach (var argument in decodedValue.NamedArguments)
{
if (argument.Name == "EntryPoint")
return (string)argument.Value;
}
return null;
}
/// <summary>
/// Determine whether a method can go into the sealed vtable of a type. Such method must be a sealed virtual
/// method that is not overriding any method on a base type.
/// Given that such methods can never be overridden in any derived type, we can
/// save space in the vtable of a type, and all of its derived types by not emitting these methods in their vtables,
/// and storing them in a separate table on the side. This is especially beneficial for all array types,
/// since all of their collection interface methods are sealed and implemented on the System.Array and
/// System.Array<T> base types, and therefore we can minimize the vtable sizes of all derived array types.
/// </summary>
public static bool CanMethodBeInSealedVTable(this MethodDesc method)
{
bool isInterfaceMethod = method.OwningType.IsInterface;
// Methods on interfaces never go into sealed vtable
// We would hit this code path for default implementations of interface methods (they are newslot+final).
// Interface types don't get physical slots, but they have logical slot numbers and that logic shouldn't
// attempt to place final+newslot methods differently.
if (method.IsFinal && method.IsNewSlot && !isInterfaceMethod)
return true;
// Implementations of static virtual method also go into the sealed vtable.
// Again, we don't let that happen for interface methods because the slot numbers are only logical,
// not physical.
if (method.Signature.IsStatic && !isInterfaceMethod)
return true;
return false;
}
public static bool NotCallableWithoutOwningEEType(this MethodDesc method)
{
TypeDesc owningType = method.OwningType;
return !method.Signature.IsStatic && /* Static methods don't have this */
!owningType.IsValueType && /* Value type instance methods take a ref to data */
!owningType.IsArrayTypeWithoutGenericInterfaces() && /* Type loader can make these at runtime */
(owningType is not MetadataType mdType || !mdType.IsModuleType) && /* Compiler parks some instance methods on the <Module> type */
!method.IsSharedByGenericInstantiations; /* Current impl limitation; can be lifted */
}
}
}
|