-
Notifications
You must be signed in to change notification settings - Fork 91
/
il_rewriter_wrapper.cpp
344 lines (299 loc) · 9.14 KB
/
il_rewriter_wrapper.cpp
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#include "il_rewriter_wrapper.h"
ILRewriter* ILRewriterWrapper::GetILRewriter() const
{
return m_ILRewriter;
}
ILInstr* ILRewriterWrapper::GetCurrentILInstr() const
{
return m_ILInstr;
}
void ILRewriterWrapper::SetILPosition(ILInstr* pILInstr)
{
m_ILInstr = pILInstr;
}
void ILRewriterWrapper::Pop() const
{
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
pNewInstr->m_opcode = CEE_POP;
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
}
ILInstr* ILRewriterWrapper::LoadNull() const
{
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
pNewInstr->m_opcode = CEE_LDNULL;
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
return pNewInstr;
}
void ILRewriterWrapper::LoadInt64(const INT64 value) const
{
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
pNewInstr->m_opcode = CEE_LDC_I8;
pNewInstr->m_Arg64 = value;
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
}
void ILRewriterWrapper::LoadInt32(const INT32 value) const
{
static const std::vector<OPCODE> opcodes = {
CEE_LDC_I4_0, CEE_LDC_I4_1, CEE_LDC_I4_2, CEE_LDC_I4_3, CEE_LDC_I4_4,
CEE_LDC_I4_5, CEE_LDC_I4_6, CEE_LDC_I4_7, CEE_LDC_I4_8,
};
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
if (value >= 0 && value <= 8)
{
pNewInstr->m_opcode = opcodes[value];
}
else if (-128 <= value && value <= 127)
{
pNewInstr->m_opcode = CEE_LDC_I4_S;
pNewInstr->m_Arg8 = static_cast<INT8>(value);
}
else
{
pNewInstr->m_opcode = CEE_LDC_I4;
pNewInstr->m_Arg32 = value;
}
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
}
ILInstr* ILRewriterWrapper::LoadArgument(const UINT16 index) const
{
static const std::vector<OPCODE> opcodes = {
CEE_LDARG_0, CEE_LDARG_1, CEE_LDARG_2, CEE_LDARG_3,
};
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
if (index >= 0 && index <= 3)
{
pNewInstr->m_opcode = opcodes[index];
}
else if (index <= 255)
{
pNewInstr->m_opcode = CEE_LDARG_S;
pNewInstr->m_Arg8 = static_cast<UINT8>(index);
}
else
{
pNewInstr->m_opcode = CEE_LDARG;
pNewInstr->m_Arg16 = index;
}
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
return pNewInstr;
}
ILInstr* ILRewriterWrapper::LoadArgumentRef(const UINT16 index) const
{
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
if (index <= 255)
{
pNewInstr->m_opcode = CEE_LDARGA_S;
pNewInstr->m_Arg8 = static_cast<UINT8>(index);
}
else
{
pNewInstr->m_opcode = CEE_LDARGA;
pNewInstr->m_Arg16 = index;
}
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
return pNewInstr;
}
void ILRewriterWrapper::Cast(const mdTypeRef type_ref) const
{
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
pNewInstr->m_opcode = CEE_CASTCLASS;
pNewInstr->m_Arg32 = type_ref;
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
}
void ILRewriterWrapper::Box(const mdTypeRef type_ref) const
{
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
pNewInstr->m_opcode = CEE_BOX;
pNewInstr->m_Arg32 = type_ref;
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
}
void ILRewriterWrapper::UnboxAny(const mdTypeRef type_ref) const
{
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
pNewInstr->m_opcode = CEE_UNBOX_ANY;
pNewInstr->m_Arg32 = type_ref;
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
}
void ILRewriterWrapper::UnboxAnyAfter(const mdTypeRef type_ref) const
{
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
pNewInstr->m_opcode = CEE_UNBOX_ANY;
pNewInstr->m_Arg32 = type_ref;
m_ILRewriter->InsertAfter(m_ILInstr, pNewInstr);
}
void ILRewriterWrapper::CreateArray(const mdTypeRef type_ref, const INT32 size) const
{
mdTypeRef typeRef = mdTypeRefNil;
LoadInt32(size);
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
pNewInstr->m_opcode = CEE_NEWARR;
pNewInstr->m_Arg32 = type_ref;
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
}
ILInstr* ILRewriterWrapper::CallMember(const mdMemberRef& member_ref, const bool is_virtual) const
{
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
pNewInstr->m_opcode = is_virtual ? CEE_CALLVIRT : CEE_CALL;
pNewInstr->m_Arg32 = member_ref;
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
return pNewInstr;
}
void ILRewriterWrapper::Duplicate() const
{
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
pNewInstr->m_opcode = CEE_DUP;
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
}
void ILRewriterWrapper::BeginLoadValueIntoArray(const INT32 arrayIndex) const
{
// duplicate the array reference
Duplicate();
// load the specified array index
LoadInt32(arrayIndex);
}
void ILRewriterWrapper::EndLoadValueIntoArray() const
{
// stelem.ref (store value into array at the specified index)
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
pNewInstr->m_opcode = CEE_STELEM_REF;
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
}
bool ILRewriterWrapper::ReplaceMethodCalls(const mdMemberRef old_method_ref, const mdMemberRef new_method_ref) const
{
bool modified = false;
for (ILInstr* pInstr = m_ILRewriter->GetILList()->m_pNext; pInstr != m_ILRewriter->GetILList();
pInstr = pInstr->m_pNext)
{
if ((pInstr->m_opcode == CEE_CALL || pInstr->m_opcode == CEE_CALLVIRT) &&
pInstr->m_Arg32 == static_cast<INT32>(old_method_ref))
{
pInstr->m_opcode = CEE_CALL;
pInstr->m_Arg32 = new_method_ref;
modified = true;
}
}
return modified;
}
ILInstr* ILRewriterWrapper::LoadToken(mdToken token) const
{
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
pNewInstr->m_opcode = CEE_LDTOKEN;
pNewInstr->m_Arg32 = token;
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
return pNewInstr;
}
ILInstr* ILRewriterWrapper::LoadObj(mdToken token) const
{
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
pNewInstr->m_opcode = CEE_LDOBJ;
pNewInstr->m_Arg32 = token;
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
return pNewInstr;
}
ILInstr* ILRewriterWrapper::StLocal(unsigned index) const
{
static const std::vector<OPCODE> opcodes = {
CEE_STLOC_0, CEE_STLOC_1, CEE_STLOC_2, CEE_STLOC_3,
};
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
if (index <= 3)
{
pNewInstr->m_opcode = opcodes[index];
}
else if (index <= 255)
{
pNewInstr->m_opcode = CEE_STLOC_S;
pNewInstr->m_Arg8 = static_cast<UINT8>(index);
}
else
{
pNewInstr->m_opcode = CEE_STLOC;
pNewInstr->m_Arg16 = index;
}
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
return pNewInstr;
}
ILInstr* ILRewriterWrapper::LoadLocal(unsigned index) const
{
static const std::vector<OPCODE> opcodes = {
CEE_LDLOC_0, CEE_LDLOC_1, CEE_LDLOC_2, CEE_LDLOC_3,
};
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
if (index <= 3)
{
pNewInstr->m_opcode = opcodes[index];
}
else if (index <= 255)
{
pNewInstr->m_opcode = CEE_LDLOC_S;
pNewInstr->m_Arg8 = static_cast<UINT8>(index);
}
else
{
pNewInstr->m_opcode = CEE_LDLOC;
pNewInstr->m_Arg16 = index;
}
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
return pNewInstr;
}
ILInstr* ILRewriterWrapper::LoadLocalAddress(unsigned index) const
{
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
if (index <= 255)
{
pNewInstr->m_opcode = CEE_LDLOCA_S;
pNewInstr->m_Arg8 = static_cast<UINT8>(index);
}
else
{
pNewInstr->m_opcode = CEE_LDLOCA;
pNewInstr->m_Arg16 = index;
}
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
return pNewInstr;
}
ILInstr* ILRewriterWrapper::Return() const
{
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
pNewInstr->m_opcode = CEE_RET;
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
return pNewInstr;
}
ILInstr* ILRewriterWrapper::Rethrow() const
{
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
pNewInstr->m_opcode = CEE_RETHROW;
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
return pNewInstr;
}
ILInstr* ILRewriterWrapper::EndFinally() const
{
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
pNewInstr->m_opcode = CEE_ENDFINALLY;
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
return pNewInstr;
}
ILInstr* ILRewriterWrapper::NOP() const
{
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
pNewInstr->m_opcode = CEE_NOP;
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
return pNewInstr;
}
ILInstr* ILRewriterWrapper::CreateInstr(unsigned opCode) const
{
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
pNewInstr->m_opcode = opCode;
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
return pNewInstr;
}
ILInstr* ILRewriterWrapper::InitObj(mdTypeRef type_ref) const
{
ILInstr* pNewInstr = m_ILRewriter->NewILInstr();
pNewInstr->m_opcode = CEE_INITOBJ;
pNewInstr->m_Arg32 = type_ref;
m_ILRewriter->InsertBefore(m_ILInstr, pNewInstr);
return pNewInstr;
}