forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGcSlotTable.cs
More file actions
206 lines (176 loc) · 7.98 KB
/
Copy pathGcSlotTable.cs
File metadata and controls
206 lines (176 loc) · 7.98 KB
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
// 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 System.Reflection.PortableExecutable;
using System.Text;
namespace ILCompiler.Reflection.ReadyToRun.Amd64
{
public class GcSlotTable
{
public class GcSlot : BaseGcSlot
{
public int Index { get; set; }
public int RegisterNumber { get; set; }
public GcStackSlot StackSlot { get; set; }
public GcSlotFlags Flags { get; set; }
public GcSlot() { }
public GcSlot(int index, int registerNumber, GcStackSlot stack, GcSlotFlags flags, bool isUntracked = false)
{
Index = index;
RegisterNumber = registerNumber;
StackSlot = stack;
Flags = flags;
if (isUntracked)
{
Flags |= GcSlotFlags.GC_SLOT_UNTRACKED;
}
}
public override GcSlotFlags WriteTo(StringBuilder sb, Machine machine, GcSlotFlags prevFlags)
{
if (prevFlags != Flags)
{
sb.Append(Flags.ToString());
sb.Append(' ');
}
if (StackSlot != null)
{
sb.Append(StackSlot.ToString());
}
else
{
sb.Append(GetRegisterName(RegisterNumber, machine));
}
return Flags;
}
private static string GetRegisterName(int registerNumber, Machine machine)
{
switch (machine)
{
case Machine.I386:
return ((x86.Registers)registerNumber).ToString();
case Machine.Amd64:
return ((Amd64.Registers)registerNumber).ToString();
case Machine.ArmThumb2:
return ((Arm.Registers)registerNumber).ToString();
case Machine.Arm64:
return ((Arm64.Registers)registerNumber).ToString();
case Machine.LoongArch64:
return ((LoongArch64.Registers)registerNumber).ToString();
case Machine.RiscV64:
return ((RiscV64.Registers)registerNumber).ToString();
case WasmMachine.Wasm32:
throw new NotImplementedException("No implementation for machine type Wasm32.");
default:
throw new NotImplementedException(machine.ToString());
}
}
}
public uint NumRegisters { get; set; }
public uint NumStackSlots { get; set; }
public uint NumUntracked { get; set; }
public uint NumSlots { get; set; }
private Machine _machine;
public uint NumTracked
{
get
{
Debug.Assert(NumSlots == GcSlots.Count);
return NumSlots - NumUntracked;
}
}
public List<GcSlot> GcSlots { get; set; }
public GcSlotTable() { }
/// <summary>
/// based on <a href="https://github.com/dotnet/runtime/blob/main/src/coreclr/vm/gcinfodecoder.cpp">GcSlotDecoder::DecodeSlotTable</a>
/// </summary>
public GcSlotTable(NativeReader imageReader, Machine machine, GcInfoTypes gcInfoTypes, ref int bitOffset)
{
_machine = machine;
if (imageReader.ReadBits(1, ref bitOffset) != 0)
{
NumRegisters = imageReader.DecodeVarLengthUnsigned(gcInfoTypes.NUM_REGISTERS_ENCBASE, ref bitOffset);
}
if (imageReader.ReadBits(1, ref bitOffset) != 0)
{
NumStackSlots = imageReader.DecodeVarLengthUnsigned(gcInfoTypes.NUM_STACK_SLOTS_ENCBASE, ref bitOffset);
NumUntracked = imageReader.DecodeVarLengthUnsigned(gcInfoTypes.NUM_UNTRACKED_SLOTS_ENCBASE, ref bitOffset);
}
NumSlots = NumRegisters + NumStackSlots + NumUntracked;
GcSlots = new List<GcSlot>();
if (NumRegisters > 0)
{
DecodeRegisters(imageReader, gcInfoTypes, ref bitOffset);
}
if (NumStackSlots > 0)
{
DecodeStackSlots(imageReader, machine, gcInfoTypes, NumStackSlots, false, ref bitOffset);
}
if (NumUntracked > 0)
{
DecodeStackSlots(imageReader, machine, gcInfoTypes, NumUntracked, true, ref bitOffset);
}
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine($" NumSlots({NumSlots}) = NumRegisters({NumRegisters}) + NumStackSlots({NumStackSlots}) + NumUntracked({NumUntracked})");
sb.AppendLine($" GcSlots:");
foreach (GcSlot slot in GcSlots)
{
sb.Append(' ', 12);
slot.WriteTo(sb, _machine, GcSlotFlags.GC_SLOT_INVALID);
sb.AppendLine();
}
return sb.ToString();
}
private void DecodeRegisters(NativeReader imageReader, GcInfoTypes gcInfoTypes, ref int bitOffset)
{
// We certainly predecode the first register
uint regNum = imageReader.DecodeVarLengthUnsigned(gcInfoTypes.REGISTER_ENCBASE, ref bitOffset);
GcSlotFlags flags = (GcSlotFlags)imageReader.ReadBits(2, ref bitOffset);
GcSlots.Add(new GcSlot(GcSlots.Count, (int)regNum, null, flags));
for (int i = 1; i < NumRegisters; i++)
{
if ((uint)flags != 0)
{
regNum = imageReader.DecodeVarLengthUnsigned(gcInfoTypes.REGISTER_ENCBASE, ref bitOffset);
flags = (GcSlotFlags)imageReader.ReadBits(2, ref bitOffset);
}
else
{
uint regDelta = imageReader.DecodeVarLengthUnsigned(gcInfoTypes.REGISTER_DELTA_ENCBASE, ref bitOffset) + 1;
regNum += regDelta;
}
GcSlots.Add(new GcSlot(GcSlots.Count, (int)regNum, null, flags));
}
}
private void DecodeStackSlots(NativeReader imageReader, Machine machine, GcInfoTypes gcInfoTypes, uint nSlots, bool isUntracked, ref int bitOffset)
{
// We have stack slots left and more room to predecode
GcStackSlotBase spBase = (GcStackSlotBase)imageReader.ReadBits(2, ref bitOffset);
int normSpOffset = imageReader.DecodeVarLengthSigned(gcInfoTypes.STACK_SLOT_ENCBASE, ref bitOffset);
int spOffset = gcInfoTypes.DenormalizeStackSlot(normSpOffset);
GcSlotFlags flags = (GcSlotFlags)imageReader.ReadBits(2, ref bitOffset);
GcSlots.Add(new GcSlot(GcSlots.Count, -1, new GcStackSlot(spOffset, spBase), flags, isUntracked));
for (int i = 1; i < nSlots; i++)
{
spBase = (GcStackSlotBase)imageReader.ReadBits(2, ref bitOffset);
if ((uint)flags != 0)
{
normSpOffset = imageReader.DecodeVarLengthSigned(gcInfoTypes.STACK_SLOT_ENCBASE, ref bitOffset);
spOffset = gcInfoTypes.DenormalizeStackSlot(normSpOffset);
flags = (GcSlotFlags)imageReader.ReadBits(2, ref bitOffset);
}
else
{
int normSpOffsetDelta = imageReader.DecodeVarLengthSigned(gcInfoTypes.STACK_SLOT_DELTA_ENCBASE, ref bitOffset);
normSpOffset += normSpOffsetDelta;
spOffset = gcInfoTypes.DenormalizeStackSlot(normSpOffset);
}
GcSlots.Add(new GcSlot(GcSlots.Count, -1, new GcStackSlot(spOffset, spBase), flags, isUntracked));
}
}
}
}