|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace ILCompiler.Reflection.ReadyToRun.LoongArch64 |
| 7 | +{ |
| 8 | + public class Epilog |
| 9 | + { |
| 10 | + public int Index { get; set; } |
| 11 | + |
| 12 | + public uint EpilogStartOffset { get; set; } |
| 13 | + public uint Res { get; set; } |
| 14 | + public uint Condition { get; set; } |
| 15 | + public uint EpilogStartIndex { get; set; } |
| 16 | + public uint EpilogStartOffsetFromMainFunctionBegin { get; set; } |
| 17 | + |
| 18 | + public Epilog() { } |
| 19 | + |
| 20 | + public Epilog(int index, int dw, uint startOffset) |
| 21 | + { |
| 22 | + Index = index; |
| 23 | + |
| 24 | + EpilogStartOffset = UnwindInfo.ExtractBits(dw, 0, 18); |
| 25 | + Res = UnwindInfo.ExtractBits(dw, 18, 4); |
| 26 | + Condition = UnwindInfo.ExtractBits(dw, 20, 4); |
| 27 | + EpilogStartIndex = UnwindInfo.ExtractBits(dw, 22, 10); |
| 28 | + |
| 29 | + // Note that epilogStartOffset for a funclet is the offset from the beginning |
| 30 | + // of the current funclet, not the offset from the beginning of the main function. |
| 31 | + // To help find it when looking through JitDump output, also show the offset from |
| 32 | + // the beginning of the main function. |
| 33 | + EpilogStartOffsetFromMainFunctionBegin = EpilogStartOffset * 4 + startOffset; |
| 34 | + } |
| 35 | + |
| 36 | + public override string ToString() |
| 37 | + { |
| 38 | + StringBuilder sb = new StringBuilder(); |
| 39 | + sb.AppendLine($" Epilog Start Offset: 0x{EpilogStartOffset:X5} Actual offset = 0x{EpilogStartOffset * 4:X5} Offset from main function begin = 0x{EpilogStartOffsetFromMainFunctionBegin:X6}"); |
| 40 | + sb.AppendLine($" Condition: {Condition} (0x{Condition:X})" + ((Condition == 0xE) ? " (always)" : "")); |
| 41 | + sb.Append($" Epilog Start Index: {EpilogStartIndex} (0x{EpilogStartIndex:X})"); |
| 42 | + return sb.ToString(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public class UnwindCode |
| 47 | + { |
| 48 | + public int Index { get; set; } |
| 49 | + |
| 50 | + public UnwindCode() { } |
| 51 | + |
| 52 | + public UnwindCode(int index) |
| 53 | + { |
| 54 | + Index = index; |
| 55 | + |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// based on <a href="https://github.com/dotnet/runtime/src/coreclr/jit/unwindloongarch64.cpp">src/jit/unwindloongarch64.cpp</a> DumpUnwindInfo |
| 61 | + /// </summary> |
| 62 | + public class UnwindInfo : BaseUnwindInfo |
| 63 | + { |
| 64 | + public uint CodeWords { get; set; } |
| 65 | + public uint EpilogCount { get; set; } |
| 66 | + public uint EBit { get; set; } |
| 67 | + public uint XBit { get; set; } |
| 68 | + public uint Vers { get; set; } |
| 69 | + public uint FunctionLength { get; set; } |
| 70 | + |
| 71 | + public uint ExtendedCodeWords { get; set; } |
| 72 | + public uint ExtendedEpilogCount { get; set; } |
| 73 | + |
| 74 | + public Epilog[] Epilogs { get; set; } |
| 75 | + |
| 76 | + public UnwindInfo() { } |
| 77 | + |
| 78 | + public UnwindInfo(byte[] image, int offset) |
| 79 | + { |
| 80 | + uint startOffset = (uint)offset; |
| 81 | + |
| 82 | + int dw = NativeReader.ReadInt32(image, ref offset); |
| 83 | + CodeWords = ExtractBits(dw, 27, 5); |
| 84 | + EpilogCount = ExtractBits(dw, 22, 5); |
| 85 | + EBit = ExtractBits(dw, 21, 1); |
| 86 | + XBit = ExtractBits(dw, 20, 1); |
| 87 | + Vers = ExtractBits(dw, 18, 2); |
| 88 | + FunctionLength = ExtractBits(dw, 0, 18) * 4; |
| 89 | + |
| 90 | + if (CodeWords == 0 && EpilogCount == 0) |
| 91 | + { |
| 92 | + // We have an extension word specifying a larger number of Code Words or Epilog Counts |
| 93 | + // than can be specified in the header word. |
| 94 | + dw = NativeReader.ReadInt32(image, ref offset); |
| 95 | + ExtendedCodeWords = ExtractBits(dw, 16, 8); |
| 96 | + ExtendedEpilogCount = ExtractBits(dw, 0, 16); |
| 97 | + } |
| 98 | + |
| 99 | + bool[] epilogStartAt = new bool[1024]; // One byte per possible epilog start index; initialized to false |
| 100 | + |
| 101 | + if (EBit == 0) |
| 102 | + { |
| 103 | + Epilogs = new Epilog[EpilogCount]; |
| 104 | + if (EpilogCount != 0) |
| 105 | + { |
| 106 | + for (int scope = 0; scope < EpilogCount; scope++) |
| 107 | + { |
| 108 | + dw = NativeReader.ReadInt32(image, ref offset); |
| 109 | + Epilogs[scope] = new Epilog(scope, dw, startOffset); |
| 110 | + epilogStartAt[Epilogs[scope].EpilogStartIndex] = true; // an epilog starts at this offset in the unwind codes |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + Epilogs = new Epilog[0]; |
| 117 | + epilogStartAt[EpilogCount] = true; // the one and only epilog starts its unwind codes at this offset |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + Size = offset - (int)startOffset + (int)CodeWords * 4; |
| 123 | + int alignmentPad = ((Size + sizeof(int) - 1) & ~(sizeof(int) - 1)) - Size; |
| 124 | + Size += (alignmentPad + sizeof(uint)); |
| 125 | + } |
| 126 | + |
| 127 | + public override string ToString() |
| 128 | + { |
| 129 | + StringBuilder sb = new StringBuilder(); |
| 130 | + sb.AppendLine($" CodeWords: {CodeWords}"); |
| 131 | + sb.AppendLine($" EpilogCount: {EpilogCount}"); |
| 132 | + sb.AppendLine($" EBit: {EBit}"); |
| 133 | + sb.AppendLine($" XBit: {XBit}"); |
| 134 | + sb.AppendLine($" Vers: {Vers}"); |
| 135 | + sb.AppendLine($" FunctionLength: {FunctionLength}"); |
| 136 | + if (CodeWords == 0 && EpilogCount == 0) |
| 137 | + { |
| 138 | + sb.AppendLine(" ---- Extension word ----"); |
| 139 | + sb.AppendLine($" Extended Code Words: {CodeWords}"); |
| 140 | + sb.AppendLine($" Extended Epilog Count: {EpilogCount}"); |
| 141 | + } |
| 142 | + if (EpilogCount == 0) |
| 143 | + { |
| 144 | + sb.AppendLine(" No epilogs"); |
| 145 | + } |
| 146 | + else |
| 147 | + { |
| 148 | + for (int i = 0; i < Epilogs.Length; i++) |
| 149 | + { |
| 150 | + sb.AppendLine(" -------------------------"); |
| 151 | + sb.AppendLine(Epilogs[i].ToString()); |
| 152 | + sb.AppendLine(" -------------------------"); |
| 153 | + } |
| 154 | + } |
| 155 | + return sb.ToString(); |
| 156 | + } |
| 157 | + |
| 158 | + internal static uint ExtractBits(int dw, int start, int length) |
| 159 | + { |
| 160 | + return (uint)((dw >> start) & ((1 << length) - 1)); |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments