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

ILStreamReader.cs « Compiler « src « ILCompiler.Compiler « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: badf0151aaad62cafe6d2d7b309b2060f149b0ce (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
// 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.Text;

using Internal.TypeSystem;
using Internal.IL;

using Debug = System.Diagnostics.Debug;

namespace Internal.Compiler
{
    /// <summary>
    /// IL Opcode reader in external reader style where the reading is done by trying to read
    /// various opcodes, and the reader can indicate success or failure of reading a particular opcode
    /// 
    /// Used by logic which is designed to encode information in il structure, but not used
    /// to support general compilation of IL.
    /// </summary>
    public struct ILStreamReader
    {
        private byte[] _ilBytes;
        private MethodIL _methodIL;
        private int _currentOffset;

        public ILStreamReader(MethodIL methodIL)
        {
            _methodIL = methodIL;
            _ilBytes = methodIL.GetILBytes();
            _currentOffset = 0;
        }

        //
        // IL stream reading
        //

        private byte ReadILByte()
        {
            return _ilBytes[_currentOffset++];
        }

        private ILOpcode ReadILOpcode()
        {
            return (ILOpcode)ReadILByte();
        }
        
        private byte PeekILByte()
        {
            return _ilBytes[_currentOffset];
        }

        private ILOpcode PeekILOpcode()
        {
            return (ILOpcode)PeekILByte();
        }

        private bool TryReadILByte(out byte ilbyte)
        {
            if (_currentOffset >= _ilBytes.Length)
                throw new BadImageFormatException();

            ilbyte = ReadILByte();
            return true;
        }

        private UInt16 ReadILUInt16()
        {
            UInt16 val = (UInt16)(_ilBytes[_currentOffset] + (_ilBytes[_currentOffset + 1] << 8));
            _currentOffset += 2;
            return val;
        }

        private bool TryReadILUInt16(out UInt16 ilUint16)
        {
            if (checked(_currentOffset + 1) >= _ilBytes.Length)
                throw new BadImageFormatException();

            ilUint16 = ReadILUInt16();
            return true;
        }

        private UInt32 ReadILUInt32()
        {
            UInt32 val = (UInt32)(_ilBytes[_currentOffset] + (_ilBytes[_currentOffset + 1] << 8) + (_ilBytes[_currentOffset + 2] << 16) + (_ilBytes[_currentOffset + 3] << 24));
            _currentOffset += 4;
            return val;
        }

        private bool TryReadILUInt32(out UInt32 ilUint32)
        {
            if (checked(_currentOffset + 3) >= _ilBytes.Length)
                throw new BadImageFormatException();

            ilUint32 = ReadILUInt32();
            return true;
        }

        private int ReadILToken()
        {
            return (int)ReadILUInt32();
        }

        private bool TryReadILToken(out int ilToken)
        {
            uint ilTokenUint;
            bool result = TryReadILUInt32(out ilTokenUint);
            ilToken = (int)ilTokenUint;
            return result;
        }

        private ulong ReadILUInt64()
        {
            ulong value = ReadILUInt32();
            value |= (((ulong)ReadILUInt32()) << 32);
            return value;
        }

        private unsafe float ReadILFloat()
        {
            uint value = ReadILUInt32();
            return *(float*)(&value);
        }

        private unsafe double ReadILDouble()
        {
            ulong value = ReadILUInt64();
            return *(double*)(&value);
        }

        private void SkipIL(int bytes)
        {
            _currentOffset += bytes;
        }

        public bool HasNextInstruction
        {
            get
            {
                return _currentOffset < _ilBytes.Length;
            }
        }

        public int CodeSize
        {
            get
            {
                return _ilBytes.Length;
            }
        }

        public bool TryReadLdtoken(out int token)
        {
            if (PeekILOpcode() != ILOpcode.ldtoken)
            {
                token = 0;
                return false;
            }

            ReadILOpcode();
            token = ReadILToken();
            return true;
        }

        public int ReadLdtoken()
        {
            int result;
            if (!TryReadLdtoken(out result))
                throw new BadImageFormatException();

            return result;
        }

        public bool TryReadLdtokenAsTypeSystemEntity(out TypeSystemEntity entity)
        {
            int token;
            bool tokenResolved;
            try
            {
                tokenResolved = TryReadLdtoken(out token);
                entity = tokenResolved ? (TypeSystemEntity)_methodIL.GetObject(token) : null;
            }
            catch (TypeSystemException)
            {
                tokenResolved = false;
                entity = null;
            }

            return tokenResolved;
        }

        public TypeSystemEntity ReadLdtokenAsTypeSystemEntity()
        {
            TypeSystemEntity result;
            if (!TryReadLdtokenAsTypeSystemEntity(out result))
                throw new BadImageFormatException();

            return result;
        }

        public bool TryReadLdcI4(out int value)
        {
            ILOpcode opcode = PeekILOpcode();

            if (opcode == ILOpcode.ldc_i4) // ldc.i4
            {
                ReadILOpcode();
                value = unchecked((int)ReadILUInt32());
                return true;
            }

            if ((opcode >= ILOpcode.ldc_i4_m1) && (opcode <= ILOpcode.ldc_i4_8)) // ldc.m1 to ldc.i4.8
            {
                ReadILOpcode();
                value = -1 + ((int)opcode) - 0x15;
                return true;
            }

            if (opcode == ILOpcode.ldc_i4_s) // ldc.i4.s
            {
                ReadILOpcode();

                value = (int)unchecked((sbyte)ReadILByte());
                return true;
            }
            value = 0;
            return false;
        }

        public int ReadLdcI4()
        {
            int result;
            if (!TryReadLdcI4(out result))
                throw new BadImageFormatException();

            return result;
        }

        public bool TryReadRet()
        {
            ILOpcode opcode = PeekILOpcode();
            if (opcode == ILOpcode.ret)
            {
                ReadILOpcode();
                return true;
            }
            return false;
        }

        public void ReadRet()
        {
            if (!TryReadRet())
                throw new BadImageFormatException();
        }

        public bool TryReadPop()
        {
            ILOpcode opcode = PeekILOpcode();
            if (opcode == ILOpcode.pop)
            {
                ReadILOpcode();
                return true;
            }
            return false;
        }

        public void ReadPop()
        {
            if (!TryReadPop())
                throw new BadImageFormatException();
        }

        public bool TryReadLdstr(out string ldstrString)
        {
            if (PeekILOpcode() != ILOpcode.ldstr)
            {
                ldstrString = null;
                return false;
            }

            ReadILOpcode();
            int token = ReadILToken();
            ldstrString = (string)_methodIL.GetObject(token);
            return true;
        }

        public string ReadLdstr()
        {
            string result;
            if (!TryReadLdstr(out result))
                throw new BadImageFormatException();

            return result;
        }
    }
}