| File: Compiler\DependencyAnalysis\ReadyToRun\Import.cs | Web Access |
| Project: ILCompiler.ReadyToRun.csproj (ILCompiler.ReadyToRun) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; using System.Collections.Generic; using System.Diagnostics; using Internal.Text; using Internal.TypeSystem; using ILCompiler.DependencyAnalysisFramework; namespace ILCompiler.DependencyAnalysis.ReadyToRun { /// <summary> /// This class represents a single indirection cell in one of the import tables. /// </summary> public class Import : EmbeddedObjectNode, ISymbolDefinitionNode, ISortableSymbolNode, IObjectNodeWithAlignment { public ImportSectionNode Table { get; private set; } internal readonly SignatureEmbeddedPointerIndirectionNode ImportSignature; internal readonly MethodDesc CallingMethod; private readonly bool _sortLast; private readonly ImportSectionNode _originalTable; private bool _placementDeferred; public Signature Signature => ImportSignature.Target; public Import(ImportSectionNode tableNode, Signature importSignature, MethodDesc callingMethod = null, bool sortLast = false) { Table = tableNode; _originalTable = tableNode; CallingMethod = callingMethod; _sortLast = sortLast; ImportSignature = new SignatureEmbeddedPointerIndirectionNode(this, importSignature); } protected override void OnMarked(NodeFactory factory) { if (factory.DeferredImportPlacement is List<Import> imports) { _placementDeferred = true; imports.Add(this); return; } Table.AddImport(factory, this); } internal void CompleteDeferredPlacement(NodeFactory factory) { Debug.Assert(_placementDeferred); _placementDeferred = false; Table.AddImport(factory, this); } internal void PromoteToEager(ImportSectionNode table) { // Placement, including the signature pointer, must wait until promotion is decided. Debug.Assert(!Marked || _placementDeferred); Debug.Assert(table.IsEager && !table.EmitPrecode); Debug.Assert(Table.EntrySize == table.EntrySize); Table = table; } protected override string GetName(NodeFactory factory) { Utf8StringBuilder sb = new Utf8StringBuilder(); AppendMangledName(factory.NameMangler, sb); return sb.ToString(); } public override int ClassCode => 667823013; public virtual bool EmitPrecode => Table.EmitPrecode; // Import cells live in a pointer-aligned import section whose entries are 'EntrySize' bytes // each, so every cell is guaranteed to be aligned to 'EntrySize' (the pointer size). Keep // this in sync with the alignment applied by the containing ImportSectionNode. public int GetAlignment(NodeFactory factory) => Table.EntrySize; public override void EncodeData(ref ObjectDataBuilder dataBuilder, NodeFactory factory, bool relocsOnly) { // This needs to be an empty target pointer since it will be filled in with Module* // when loaded by CoreCLR dataBuilder.EmitZeroPointer(); } public virtual void AppendMangledName(NameMangler nameMangler, Utf8StringBuilder sb) { // Promotion changes placement, not identity; an eager cell may share this signature. sb.Append(_originalTable.Name); sb.Append("->"u8); ImportSignature.AppendMangledName(nameMangler, sb); } public override bool StaticDependenciesAreComputed => true; public override void AddStaticDependencies(DependencySink sink, NodeFactory factory) { sink.Add(ImportSignature, "Signature for ready-to-run fixup import"); } // Eager promotion mixes import subclasses and cells from different original sections. // Concrete subclasses need distinct class codes, and the original table must break ties // when promoted cells otherwise share their current section and signature. Signature // pointers compare through their imports, keeping separately sorted cell/signature arrays paired. public override int CompareToImpl(ISortableNode other, CompilerComparer comparer) { Import otherNode = (Import)other; int result = _sortLast.CompareTo(otherNode._sortLast); if (result != 0) return result; result = comparer.Compare(CallingMethod, otherNode.CallingMethod); if (result != 0) return result; result = comparer.Compare(ImportSignature.Target, otherNode.ImportSignature.Target); if (result != 0) return result; result = Table.CompareToImpl(otherNode.Table, comparer); if (result != 0) return result; return _originalTable.CompareToImpl(otherNode._originalTable, comparer); } public override bool RepresentsIndirectionCell => true; int ISymbolDefinitionNode.Offset => OffsetFromBeginningOfArray; int ISymbolNode.Offset => 0; } }