Skip to content
This repository was archived by the owner on Nov 1, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ namespace ILCompiler.DependencyAnalysis
[Flags]
public enum FrameInfoFlags
{
Handler = 0x01,
Filter = 0x02,
Handler = 0x01,
Filter = 0x02,

HasEHInfo = 0x04,
ReversePInvoke = 0x08,
HasEHInfo = 0x04,
ReversePInvoke = 0x08,
HasAssociatedData = 0x10,
}

public struct FrameInfo
Expand Down Expand Up @@ -48,5 +49,7 @@ ObjectNode.ObjectData EHInfo
{
get;
}

ISymbolNode GetAssociatedDataNode(NodeFactory factory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.

using Internal.TypeSystem;

namespace ILCompiler.DependencyAnalysis
{
/// <summary>
/// A dependency analysis node that represents a special instantiating unboxing stub.
/// </summary>
public interface ISpecialUnboxThunkNode : IMethodNode
{
bool IsSpecialUnboxingThunk { get; }
ISymbolNode GetUnboxingThunkTarget(NodeFactory factory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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.

using System;
using System.Diagnostics;

using Internal.Text;
using Internal.TypeSystem;
using Internal.TypeSystem.Interop;

namespace ILCompiler.DependencyAnalysis
{
[Flags]
public enum AssociatedDataFlags : byte
{
None = 0,
HasUnboxingStubTarget = 1,
}

/// <summary>
/// This node contains any custom data that we'd like to associated with a method. The unwind info of the method
/// will have a reloc to this custom data if it exists. Not all methods need custom data to be emitted.
/// This custom data excludes gcinfo and ehinfo (they are written by ObjectWriter during obj file emission).
/// </summary>
public class MethodAssociatedDataNode : ObjectNode, ISymbolDefinitionNode
{
private IMethodNode _methodNode;

public MethodAssociatedDataNode(IMethodNode methodNode)
{
Debug.Assert(!methodNode.Method.IsAbstract);
Debug.Assert(methodNode.Method.GetCanonMethodTarget(CanonicalFormKind.Specific) == methodNode.Method);
_methodNode = methodNode;
}

protected override string GetName(NodeFactory factory) => this.GetMangledName(factory.NameMangler);

public override ObjectNodeSection Section => ObjectNodeSection.ReadOnlyDataSection;
public override bool StaticDependenciesAreComputed => true;
public int Offset => 0;
public override bool IsShareable => _methodNode.Method is InstantiatedMethod || EETypeNode.IsTypeNodeShareable(_methodNode.Method.OwningType);

public virtual void AppendMangledName(NameMangler nameMangler, Utf8StringBuilder sb)
{
sb.Append("_associatedData_").Append(nameMangler.GetMangledMethodName(_methodNode.Method));
}

public static bool MethodHasAssociatedData(NodeFactory factory, IMethodNode methodNode)
{
// Instantiating unboxing stubs. We need to store their non-unboxing target pointer (looked up by runtime)
ISpecialUnboxThunkNode unboxThunk = methodNode as ISpecialUnboxThunkNode;
if(unboxThunk != null && unboxThunk.IsSpecialUnboxingThunk)
return true;

return false;
}

public override ObjectData GetData(NodeFactory factory, bool relocsOnly)
{
Debug.Assert(MethodHasAssociatedData(factory, _methodNode));

ObjectDataBuilder objData = new ObjectDataBuilder(factory, relocsOnly);
objData.RequireInitialAlignment(1);
objData.AddSymbol(this);

AssociatedDataFlags flags = AssociatedDataFlags.None;

var flagsReservation = objData.ReserveByte();

ISpecialUnboxThunkNode unboxThunkNode = _methodNode as ISpecialUnboxThunkNode;
if (unboxThunkNode != null && unboxThunkNode.IsSpecialUnboxingThunk)
{
flags |= AssociatedDataFlags.HasUnboxingStubTarget;
objData.EmitPointerReloc(unboxThunkNode.GetUnboxingThunkTarget(factory));
}

objData.EmitByte(flagsReservation, (byte)flags);

return objData.ToObjectData();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace ILCompiler.DependencyAnalysis
{
public class MethodCodeNode : ObjectNode, IMethodBodyNode, INodeWithCodeInfo, INodeWithDebugInfo, IMethodCodeNode
public class MethodCodeNode : ObjectNode, IMethodBodyNode, INodeWithCodeInfo, INodeWithDebugInfo, IMethodCodeNode, ISpecialUnboxThunkNode
{
public static readonly ObjectNodeSection StartSection = new ObjectNodeSection(".managedcode$A", SectionType.Executable);
public static readonly ObjectNodeSection WindowsContentSection = new ObjectNodeSection(".managedcode$I", SectionType.Executable);
Expand Down Expand Up @@ -82,6 +82,12 @@ protected override DependencyList ComputeNonRelocationBasedDependencies(NodeFact
}
}

if (MethodAssociatedDataNode.MethodHasAssociatedData(factory, this))
{
dependencies = dependencies ?? new DependencyList();
dependencies.Add(new DependencyListEntry(factory.MethodAssociatedData(this), "Method associated data"));
}

CodeBasedDependencyAlgorithm.AddDependenciesDueToMethodCodePresence(ref dependencies, factory, _method);

return dependencies;
Expand All @@ -92,10 +98,28 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly)
return _methodCode;
}

public bool IsSpecialUnboxingThunk => ((CompilerTypeSystemContext)Method.Context).IsSpecialUnboxingThunk(_method);

public ISymbolNode GetUnboxingThunkTarget(NodeFactory factory)
{
Debug.Assert(IsSpecialUnboxingThunk);

MethodDesc nonUnboxingMethod = ((CompilerTypeSystemContext)Method.Context).GetTargetOfSpecialUnboxingThunk(_method);
return factory.MethodEntrypoint(nonUnboxingMethod, false);
}

public FrameInfo[] FrameInfos => _frameInfos;
public byte[] GCInfo => _gcInfo;
public ObjectData EHInfo => _ehInfo;

public ISymbolNode GetAssociatedDataNode(NodeFactory factory)
{
if (MethodAssociatedDataNode.MethodHasAssociatedData(factory, this))
return factory.MethodAssociatedData(this);

return null;
}

public void InitializeFrameInfos(FrameInfo[] frameInfos)
{
Debug.Assert(_frameInfos == null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ private void CreateNodeCaches()

_unboxingStubs = new NodeCache<MethodDesc, IMethodNode>(CreateUnboxingStubNode);

_methodAssociatedData = new NodeCache<IMethodNode, MethodAssociatedDataNode>(methodNode =>
{
return new MethodAssociatedDataNode(methodNode);
});

_fatFunctionPointers = new NodeCache<MethodKey, FatFunctionPointerNode>(method =>
{
return new FatFunctionPointerNode(method.Method, method.IsUnboxingStub);
Expand Down Expand Up @@ -707,6 +712,7 @@ public IMethodNode StringAllocator(MethodDesc stringConstructor)

private NodeCache<MethodDesc, IMethodNode> _methodEntrypoints;
private NodeCache<MethodDesc, IMethodNode> _unboxingStubs;
private NodeCache<IMethodNode, MethodAssociatedDataNode> _methodAssociatedData;

public IMethodNode MethodEntrypoint(MethodDesc method, bool unboxingStub = false)
{
Expand All @@ -718,6 +724,11 @@ public IMethodNode MethodEntrypoint(MethodDesc method, bool unboxingStub = false
return _methodEntrypoints.GetOrAdd(method);
}

public MethodAssociatedDataNode MethodAssociatedData(IMethodNode methodNode)
{
return _methodAssociatedData.GetOrAdd(methodNode);
}

private NodeCache<MethodKey, FatFunctionPointerNode> _fatFunctionPointers;

public IMethodNode FatFunctionPointer(MethodDesc method, bool isUnboxingStub = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ namespace ILCompiler.DependencyAnalysis
/// in the DependencyAnalysis infrastructure during compilation that is compiled
/// in the current compilation process
/// </summary>
public class NonExternMethodSymbolNode : ExternSymbolNode, IMethodBodyNodeWithFuncletSymbols
public class NonExternMethodSymbolNode : ExternSymbolNode, IMethodBodyNodeWithFuncletSymbols, ISpecialUnboxThunkNode
{
private MethodDesc _method;
private bool _isUnboxing;
private List<DependencyListEntry> _compilationDiscoveredDependencies;
ISymbolNode[] _funcletSymbols = Array.Empty<ISymbolNode>();
bool _dependenciesQueried;
Expand All @@ -29,6 +30,7 @@ public NonExternMethodSymbolNode(NodeFactory factory, MethodDesc method, bool is
: base(isUnboxing ? UnboxingStubNode.GetMangledName(factory.NameMangler, method) :
factory.NameMangler.GetMangledMethodName(method))
{
_isUnboxing = isUnboxing;
_method = method;
}

Expand All @@ -42,6 +44,26 @@ public MethodDesc Method
}
}

public bool IsSpecialUnboxingThunk
{
get
{
if (_isUnboxing)
{
if (!_method.HasInstantiation && _method.OwningType.IsValueType && !_method.Signature.IsStatic)
return _method.IsCanonicalMethod(CanonicalFormKind.Any);
}

return false;
}
}
public ISymbolNode GetUnboxingThunkTarget(NodeFactory factory)
{
Debug.Assert(IsSpecialUnboxingThunk);

return factory.MethodEntrypoint(_method.GetCanonMethodTarget(CanonicalFormKind.Specific), false);
}

public bool HasCompiledBody => _hasCompiledBody;
public void SetHasCompiledBody()
{
Expand Down Expand Up @@ -99,6 +121,12 @@ public override IEnumerable<DependencyListEntry> GetStaticDependencies(NodeFacto
dependencies.AddRange(_compilationDiscoveredDependencies);
}

if (MethodAssociatedDataNode.MethodHasAssociatedData(factory, this))
{
dependencies = dependencies ?? new DependencyList();
dependencies.Add(new DependencyListEntry(factory.MethodAssociatedData(this), "Method associated data"));
}

return dependencies;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,15 @@ public void PublishUnwindInfo(ObjectNode node)
FrameInfo[] frameInfos = nodeWithCodeInfo.FrameInfos;
if (frameInfos == null)
{
// Data should only be present if the method has unwind info
Debug.Assert(nodeWithCodeInfo.GetAssociatedDataNode(_nodeFactory) == null);

return;
}

byte[] gcInfo = nodeWithCodeInfo.GCInfo;
ObjectData ehInfo = nodeWithCodeInfo.EHInfo;
ISymbolNode associatedDataNode = nodeWithCodeInfo.GetAssociatedDataNode(_nodeFactory);

for (int i = 0; i < frameInfos.Length; i++)
{
Expand All @@ -458,15 +462,19 @@ public void PublishUnwindInfo(ObjectNode node)
EmitSymbolDef(blobSymbolName);

FrameInfoFlags flags = frameInfo.Flags;
if (ehInfo != null)
{
flags |= FrameInfoFlags.HasEHInfo;
}
flags |= ehInfo != null ? FrameInfoFlags.HasEHInfo : 0;
flags |= associatedDataNode != null ? FrameInfoFlags.HasAssociatedData : 0;

EmitBlob(blob);

EmitIntValue((byte)flags, 1);

if (associatedDataNode != null)
{
EmitSymbolRef(_sb.Clear().Append(associatedDataNode.GetMangledName(_nodeFactory.NameMangler)), RelocType.IMAGE_REL_BASED_ABSOLUTE);
associatedDataNode = null;
}

if (ehInfo != null)
{
EmitSymbolRef(_sb.Clear().Append(_nodeFactory.NameMangler.CompilationUnitPrefix).Append("_ehInfo").Append(_currentNodeZeroTerminatedName), RelocType.IMAGE_REL_BASED_ABSOLUTE);
Expand Down Expand Up @@ -512,11 +520,15 @@ public void BuildCFIMap(NodeFactory factory, ObjectNode node)
FrameInfo[] frameInfos = nodeWithCodeInfo.FrameInfos;
if (frameInfos == null)
{
// Data should only be present if the method has unwind info
Debug.Assert(nodeWithCodeInfo.GetAssociatedDataNode(_nodeFactory) == null);

return;
}

byte[] gcInfo = nodeWithCodeInfo.GCInfo;
ObjectData ehInfo = nodeWithCodeInfo.EHInfo;
ISymbolNode associatedDataNode = nodeWithCodeInfo.GetAssociatedDataNode(_nodeFactory);

for (int i = 0; i < frameInfos.Length; i++)
{
Expand All @@ -539,10 +551,9 @@ public void BuildCFIMap(NodeFactory factory, ObjectNode node)
EmitSymbolDef(blobSymbolName);

FrameInfoFlags flags = frameInfo.Flags;
if (ehInfo != null)
{
flags |= FrameInfoFlags.HasEHInfo;
}
flags |= ehInfo != null ? FrameInfoFlags.HasEHInfo : 0;
flags |= associatedDataNode != null ? FrameInfoFlags.HasAssociatedData : 0;

EmitIntValue((byte)flags, 1);

if (i != 0)
Expand All @@ -553,6 +564,14 @@ public void BuildCFIMap(NodeFactory factory, ObjectNode node)
EmitIntValue((ulong)(start - frameInfos[0].StartOffset), 4);
}

if (associatedDataNode != null)
{
_sb.Clear();
AppendExternCPrefix(_sb);
EmitSymbolRef(_sb.Append(associatedDataNode.GetMangledName(_nodeFactory.NameMangler)), RelocType.IMAGE_REL_BASED_RELPTR32);
associatedDataNode = null;
}

if (ehInfo != null)
{
EmitSymbolRef(_sb.Clear().Append("_ehInfo").Append(_currentNodeZeroTerminatedName), RelocType.IMAGE_REL_BASED_RELPTR32);
Expand Down
6 changes: 4 additions & 2 deletions src/ILCompiler.Compiler/src/ILCompiler.Compiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@
<Compile Include="Compiler\DependencyAnalysis\INodeWithRuntimeDeterminedDependencies.cs" />
<Compile Include="Compiler\DependencyAnalysis\ImportedEETypeSymbolNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\ImportedStaticsNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\ImportedThreadStaticsNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\NonExternMethodSymbolNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\ImportedThreadStaticsNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\NonExternMethodSymbolNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\PInvokeMethodFixupNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\PInvokeModuleFixupNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\ResourceDataNode.cs" />
Expand All @@ -208,9 +208,11 @@
<Compile Include="Compiler\DependencyAnalysis\EmbeddedPointerIndirectionNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\ExternMethodSymbolNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\IMethodNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\ISpecialUnboxThunkNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\InterfaceDispatchCellNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\MetadataNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\MethodCodeNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\MethodAssociatedDataNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\ArrayOfEmbeddedPointersNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\ObjectNodeSection.cs" />
<Compile Include="Compiler\DependencyAnalysis\ReadyToRunHeaderNode.cs" />
Expand Down
Loading