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

ILReader.cs « IL « TypeSystem « Common « tools « coreclr « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d0022a5bad78be4ee392a406ed5882e7458c851f (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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Internal.TypeSystem;

using Debug = System.Diagnostics.Debug;

namespace Internal.IL
{
    internal struct ILReader
    {
        private int _currentOffset;
        private readonly byte[] _ilBytes;

        public int Offset
        {
            get
            {
                return _currentOffset;
            }
        }

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

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

        public ILReader(byte[] ilBytes, int currentOffset = 0)
        {
            _ilBytes = ilBytes;
            _currentOffset = currentOffset;
        }

        //
        // IL stream reading
        //

        public byte ReadILByte()
        {
            if (_currentOffset + 1 > _ilBytes.Length)
                ThrowHelper.ThrowInvalidProgramException();

            return _ilBytes[_currentOffset++];
        }

        public ushort ReadILUInt16()
        {
            if (_currentOffset + 2 > _ilBytes.Length)
                ThrowHelper.ThrowInvalidProgramException();

            ushort val = (ushort)(_ilBytes[_currentOffset] + (_ilBytes[_currentOffset + 1] << 8));
            _currentOffset += 2;
            return val;
        }

        public uint ReadILUInt32()
        {
            if (_currentOffset + 4 > _ilBytes.Length)
                ThrowHelper.ThrowInvalidProgramException();

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

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

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

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

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

        public ILOpcode ReadILOpcode()
        {
            ILOpcode opcode = (ILOpcode)ReadILByte();
            if (opcode == ILOpcode.prefix1)
            {
                opcode = (ILOpcode)(0x100 + ReadILByte());
            }

            return opcode;
        }

        public ILOpcode PeekILOpcode()
        {
            ILOpcode opcode = (ILOpcode)_ilBytes[_currentOffset];
            if (opcode == ILOpcode.prefix1)
            {
                if (_currentOffset + 2 > _ilBytes.Length)
                    ThrowHelper.ThrowInvalidProgramException();
                opcode = (ILOpcode)(0x100 + _ilBytes[_currentOffset + 1]);
            }

            return opcode;
        }

        public void Skip(ILOpcode opcode)
        {
            if (!opcode.IsValid())
                ThrowHelper.ThrowInvalidProgramException();

            if (opcode != ILOpcode.switch_)
            {
                int opcodeSize = (byte)opcode != (int)opcode ? 2 : 1;
                _currentOffset += opcode.GetSize() - opcodeSize;
            }
            else
            {
                // "switch" opcode is special
                uint count = ReadILUInt32();
                _currentOffset += checked((int)(count * 4));
            }
        }

        public void Seek(int offset)
        {
            _currentOffset = offset;
        }

        public int ReadBranchDestination(ILOpcode currentOpcode)
        {
            if ((currentOpcode >= ILOpcode.br_s && currentOpcode <= ILOpcode.blt_un_s)
                || currentOpcode == ILOpcode.leave_s)
            {
                return (sbyte)ReadILByte() + Offset;
            }
            else
            {
                Debug.Assert((currentOpcode >= ILOpcode.br && currentOpcode <= ILOpcode.blt_un)
                    || currentOpcode == ILOpcode.leave);
                return (int)ReadILUInt32() + Offset;
            }
        }
    }
}