Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ObjectDataBuilder.cs « DependencyAnalysis « Compiler « Common « tools « coreclr « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f20020ad71051a178e939169fb692b226488e21b (plain)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// 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 Internal.TypeSystem;

using Debug = System.Diagnostics.Debug;

namespace ILCompiler.DependencyAnalysis
{
    public struct ObjectDataBuilder
#if !READYTORUN
        : Internal.Runtime.ITargetBinaryWriter
#endif
    {
        public ObjectDataBuilder(NodeFactory factory, bool relocsOnly) : this(factory.Target, relocsOnly)
        {
        }

        public ObjectDataBuilder(TargetDetails target, bool relocsOnly)
        {
            _target = target;
            _data = new ArrayBuilder<byte>();
            _relocs = new ArrayBuilder<Relocation>();
            Alignment = 1;
            _definedSymbols = new ArrayBuilder<ISymbolDefinitionNode>();
#if DEBUG
            _numReservations = 0;
            _checkAllSymbolDependenciesMustBeMarked = !relocsOnly;
#endif
        }

        private TargetDetails _target;
        private ArrayBuilder<Relocation> _relocs;
        private ArrayBuilder<byte> _data;
        public int Alignment { get; private set; }
        private ArrayBuilder<ISymbolDefinitionNode> _definedSymbols;

#if DEBUG
        private int _numReservations;
        private bool _checkAllSymbolDependenciesMustBeMarked;
#endif

        public int CountBytes
        {
            get
            {
                return _data.Count;
            }
        }

        public int TargetPointerSize
        {
            get
            {
                return _target.PointerSize;
            }
        }

        /// <summary>
        /// Raise the alignment requirement of this object to <paramref name="align"/>. This has no effect
        /// if the alignment requirement is already larger than <paramref name="align"/>.
        /// </summary>
        public void RequireInitialAlignment(int align)
        {
            Alignment = Math.Max(align, Alignment);
        }

        /// <summary>
        /// Raise the alignment requirement of this object to the target pointer size. This has no effect
        /// if the alignment requirement is already larger than a pointer size.
        /// </summary>
        public void RequireInitialPointerAlignment()
        {
            RequireInitialAlignment(_target.PointerSize);
        }

        public void EmitByte(byte emit)
        {
            _data.Add(emit);
        }

        public void EmitShort(short emit)
        {
            EmitByte((byte)(emit & 0xFF));
            EmitByte((byte)((emit >> 8) & 0xFF));
        }

        public void EmitUShort(ushort emit)
        {
            EmitByte((byte)(emit & 0xFF));
            EmitByte((byte)((emit >> 8) & 0xFF));
        }

        public void EmitInt(int emit)
        {
            EmitByte((byte)(emit & 0xFF));
            EmitByte((byte)((emit >> 8) & 0xFF));
            EmitByte((byte)((emit >> 16) & 0xFF));
            EmitByte((byte)((emit >> 24) & 0xFF));
        }

        public void EmitUInt(uint emit)
        {
            EmitByte((byte)(emit & 0xFF));
            EmitByte((byte)((emit >> 8) & 0xFF));
            EmitByte((byte)((emit >> 16) & 0xFF));
            EmitByte((byte)((emit >> 24) & 0xFF));
        }

        public void EmitLong(long emit)
        {
            EmitByte((byte)(emit & 0xFF));
            EmitByte((byte)((emit >> 8) & 0xFF));
            EmitByte((byte)((emit >> 16) & 0xFF));
            EmitByte((byte)((emit >> 24) & 0xFF));
            EmitByte((byte)((emit >> 32) & 0xFF));
            EmitByte((byte)((emit >> 40) & 0xFF));
            EmitByte((byte)((emit >> 48) & 0xFF));
            EmitByte((byte)((emit >> 56) & 0xFF));
        }

        public void EmitNaturalInt(int emit)
        {
            if (_target.PointerSize == 8)
            {
                EmitLong(emit);
            }
            else
            {
                Debug.Assert(_target.PointerSize == 4);
                EmitInt(emit);
            }
        }

        public void EmitHalfNaturalInt(short emit)
        {
            if (_target.PointerSize == 8)
            {
                EmitInt(emit);
            }
            else
            {
                Debug.Assert(_target.PointerSize == 4);
                EmitShort(emit);
            }
        }

        public void EmitCompressedUInt(uint emit)
        {
            if (emit < 128)
            {
                EmitByte((byte)(emit * 2 + 0));
            }
            else if (emit < 128 * 128)
            {
                EmitByte((byte)(emit * 4 + 1));
                EmitByte((byte)(emit >> 6));
            }
            else if (emit < 128 * 128 * 128)
            {
                EmitByte((byte)(emit * 8 + 3));
                EmitByte((byte)(emit >> 5));
                EmitByte((byte)(emit >> 13));
            }
            else if (emit < 128 * 128 * 128 * 128)
            {
                EmitByte((byte)(emit * 16 + 7));
                EmitByte((byte)(emit >> 4));
                EmitByte((byte)(emit >> 12));
                EmitByte((byte)(emit >> 20));
            }
            else
            {
                EmitByte((byte)15);
                EmitInt((int)emit);
            }
        }

        public void EmitBytes(byte[] bytes)
        {
            _data.Append(bytes);
        }

        public void EmitBytes(byte[] bytes, int offset, int length)
        {
            _data.Append(bytes, offset, length);
        }

        internal void EmitBytes(ArrayBuilder<byte> bytes)
        {
            _data.Append(bytes);
        }

        public void EmitZeroPointer()
        {
            _data.ZeroExtend(_target.PointerSize);
        }

        public void EmitZeros(int numBytes)
        {
            _data.ZeroExtend(numBytes);
        }

        private Reservation GetReservationTicket(int size)
        {
#if DEBUG
            _numReservations++;
#endif
            Reservation ticket = (Reservation)_data.Count;
            _data.ZeroExtend(size);
            return ticket;
        }

        private int ReturnReservationTicket(Reservation reservation)
        {
#if DEBUG
            Debug.Assert(_numReservations > 0);
            _numReservations--;
#endif
            return (int)reservation;
        }

        public Reservation ReserveByte()
        {
            return GetReservationTicket(1);
        }

        public void EmitByte(Reservation reservation, byte emit)
        {
            int offset = ReturnReservationTicket(reservation);
            _data[offset] = emit;
        }

        public Reservation ReserveShort()
        {
            return GetReservationTicket(2);
        }

        public void EmitShort(Reservation reservation, short emit)
        {
            int offset = ReturnReservationTicket(reservation);
            _data[offset] = (byte)(emit & 0xFF);
            _data[offset + 1] = (byte)((emit >> 8) & 0xFF);
        }

        public Reservation ReserveInt()
        {
            return GetReservationTicket(4);
        }

        public void EmitInt(Reservation reservation, int emit)
        {
            int offset = ReturnReservationTicket(reservation);
            _data[offset] = (byte)(emit & 0xFF);
            _data[offset + 1] = (byte)((emit >> 8) & 0xFF);
            _data[offset + 2] = (byte)((emit >> 16) & 0xFF);
            _data[offset + 3] = (byte)((emit >> 24) & 0xFF);
        }

        public void EmitUInt(Reservation reservation, uint emit)
        {
            int offset = ReturnReservationTicket(reservation);
            _data[offset] = (byte)(emit & 0xFF);
            _data[offset + 1] = (byte)((emit >> 8) & 0xFF);
            _data[offset + 2] = (byte)((emit >> 16) & 0xFF);
            _data[offset + 3] = (byte)((emit >> 24) & 0xFF);
        }

        public void EmitReloc(ISymbolNode symbol, RelocType relocType, int delta = 0)
        {
#if DEBUG
            if (_checkAllSymbolDependenciesMustBeMarked)
            {
                var node = symbol as ILCompiler.DependencyAnalysisFramework.DependencyNodeCore<NodeFactory>;
                if (node != null)
                    Debug.Assert(node.Marked);
            }
#endif

            _relocs.Add(new Relocation(relocType, _data.Count, symbol));

            // And add space for the reloc
            switch (relocType)
            {
                case RelocType.IMAGE_REL_BASED_REL32:
                case RelocType.IMAGE_REL_BASED_RELPTR32:
                case RelocType.IMAGE_REL_BASED_ABSOLUTE:
                case RelocType.IMAGE_REL_BASED_HIGHLOW:
                case RelocType.IMAGE_REL_SECREL:
                case RelocType.IMAGE_REL_FILE_ABSOLUTE:
                case RelocType.IMAGE_REL_BASED_ADDR32NB:
                case RelocType.IMAGE_REL_SYMBOL_SIZE:
                    EmitInt(delta);
                    break;
                case RelocType.IMAGE_REL_BASED_DIR64:
                    EmitLong(delta);
                    break;
                case RelocType.IMAGE_REL_BASED_THUMB_BRANCH24:
                case RelocType.IMAGE_REL_BASED_ARM64_BRANCH26:
                case RelocType.IMAGE_REL_BASED_THUMB_MOV32:
                case RelocType.IMAGE_REL_BASED_ARM64_PAGEBASE_REL21:
                case RelocType.IMAGE_REL_BASED_ARM64_PAGEOFFSET_12L:
                case RelocType.IMAGE_REL_BASED_ARM64_PAGEOFFSET_12A:
                case RelocType.IMAGE_REL_BASED_LOONGARCH64_PC:
                case RelocType.IMAGE_REL_BASED_LOONGARCH64_JIR:
                    Debug.Assert(delta == 0);
                    // Do not vacate space for this kind of relocation, because
                    // the space is embedded in the instruction.
                    break;                    
                default:
                    throw new NotImplementedException();
            }
        }

        public void EmitPointerReloc(ISymbolNode symbol, int delta = 0)
        {
            EmitReloc(symbol, (_target.PointerSize == 8) ? RelocType.IMAGE_REL_BASED_DIR64 : RelocType.IMAGE_REL_BASED_HIGHLOW, delta);
        }

        public ObjectNode.ObjectData ToObjectData()
        {
#if DEBUG
            Debug.Assert(_numReservations == 0);
#endif

            ObjectNode.ObjectData returnData = new ObjectNode.ObjectData(_data.ToArray(),
                                                                         _relocs.ToArray(),
                                                                         Alignment,
                                                                         _definedSymbols.ToArray());

            return returnData;
        }

        public enum Reservation { }

        public void AddSymbol(ISymbolDefinitionNode node)
        {
            _definedSymbols.Add(node);
        }

        public void PadAlignment(int align)
        {
            Debug.Assert((align == 2) || (align == 4) || (align == 8) || (align == 16));
            int misalignment = _data.Count & (align - 1);
            if (misalignment != 0)
            {
                EmitZeros(align - misalignment);
            }
        }
    }
}