-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArrayManipulation.cs
More file actions
247 lines (232 loc) · 12.4 KB
/
ArrayManipulation.cs
File metadata and controls
247 lines (232 loc) · 12.4 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
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
using System;
using System.Reflection.Emit;
using JetBrains.Annotations;
namespace ILGeneratorExtensions
{
/// <summary>
/// Contains extension methods for manipulating an array and its elements
/// </summary>
[PublicAPI]
public static partial class ArrayManipulation
{
/// <summary>
/// Pops an array reference off the evaluation stack and pushes the length of the array
/// </summary>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
[PublicAPI]
public static ILGenerator ArrayLength(this ILGenerator generator) => generator.FluentEmit(OpCodes.Ldlen);
/// <summary>
/// Pops an array reference (containing elements of the given type) and an index off the evaluation stack and pushes the element at that array index
/// </summary>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
/// <param name="type">The type of elements in the array</param>
[PublicAPI]
public static ILGenerator LoadElement(this ILGenerator generator, Type type)
{
if (!type.IsValueType)
{
return generator.FluentEmit(OpCodes.Ldelem_Ref);
}
else if (type == typeof(sbyte) || type == typeof(bool))
{
return generator.FluentEmit(OpCodes.Ldelem_I1);
}
else if (type == typeof(byte))
{
return generator.FluentEmit(OpCodes.Ldelem_U1);
}
else if (type == typeof(short))
{
return generator.FluentEmit(OpCodes.Ldelem_I2);
}
else if (type == typeof(ushort))
{
return generator.FluentEmit(OpCodes.Ldelem_U2);
}
else if (type == typeof(int))
{
return generator.FluentEmit(OpCodes.Ldelem_I4);
}
else if (type == typeof(uint))
{
return generator.FluentEmit(OpCodes.Ldelem_U4);
}
else if (type == typeof(long))
{
return generator.FluentEmit(OpCodes.Ldelem_I8);
}
else if (type == typeof(ulong))
{
// Not a mistake! ldelem.U8 is an alias for ldelem.I8
return generator.FluentEmit(OpCodes.Ldelem_I8);
}
else if (type == typeof(float))
{
return generator.FluentEmit(OpCodes.Ldelem_R4);
}
else if (type == typeof(double))
{
return generator.FluentEmit(OpCodes.Ldelem_R8);
}
else
{
return generator.FluentEmit(OpCodes.Ldelem, type);
}
}
/// <summary>
/// Pops an array reference (containing elements of the given type) and an index off the evaluation stack and pushes the element at that array index
/// </summary>
/// <typeparam name="T">The type of elements in the array</typeparam>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
[PublicAPI]
public static ILGenerator LoadElement<T>(this ILGenerator generator) => generator.LoadElement(typeof(T));
/// <summary>
/// Pops an array reference (containing elements of the given type) off the evaluation stack and pushes the element at the given array index
/// </summary>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
/// <param name="type">The type of elements in the array</param>
/// <param name="index">The index of the element to load</param>
[PublicAPI]
public static ILGenerator LoadElementAtIndex(this ILGenerator generator, Type type, uint index)
{
return generator.LoadConstant(index)
.LoadElement(type);
}
/// <summary>
/// Pops an array reference (containing elements of the given type) off the evaluation stack and pushes the element at the given array index
/// </summary>
/// <typeparam name="T">The type of elements in the array</typeparam>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
/// <param name="index">The index of the element to load</param>
[PublicAPI]
public static ILGenerator LoadElementAtIndex<T>(this ILGenerator generator, uint index) => generator.LoadElementAtIndex(typeof(T), index);
/// <summary>
/// Pops an array reference (containing elements of the given type) and an index off the evaluation stack and pushes the address of the element
/// </summary>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
/// <param name="type">The type of elements in the array</param>
[PublicAPI]
public static ILGenerator LoadElementAddress(this ILGenerator generator, Type type) => generator.FluentEmit(OpCodes.Ldelema, type);
/// <summary>
/// Pops an array reference (containing elements of the given type) and pushes the address of the element at the given index
/// </summary>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
/// <param name="type">The type of elements in the array</param>
/// <param name="index">The index of the element to load the address of</param>
[PublicAPI]
public static ILGenerator LoadElementAddressAtIndex(this ILGenerator generator, Type type, uint index)
{
return generator.LoadConstant(index)
.LoadElementAddress(type);
}
/// <summary>
/// Pops an array reference (containing elements of the given type) and an index off the evaluation stack and pushes the address of the element at that array index
/// </summary>
/// <typeparam name="T">The type of elements in the array</typeparam>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
[PublicAPI]
public static ILGenerator LoadElementAddress<T>(this ILGenerator generator) => generator.LoadElementAddress(typeof(T));
/// <summary>
/// Pops an array reference (containing elements of the given type) off the evaluation stack and pushes the address of the element at the given index
/// </summary>
/// <typeparam name="T">The type of elements in the array</typeparam>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
/// <param name="index">The index of the element to load the address of</param>
[PublicAPI]
public static ILGenerator LoadElementAddressAtIndex<T>(this ILGenerator generator, uint index)
{
return generator.LoadConstant(index)
.LoadElementAddress(typeof(T));
}
/// <summary>
/// Pops an array reference (containing elements of the given type) and an index off the evaluation stack and pushes the address of the element at that array index, with restrictions on its use by other code
/// </summary>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
/// <param name="type">The type of elements in the array</param>
[PublicAPI]
public static ILGenerator LoadElementAddressReadonly(this ILGenerator generator, Type type)
{
return generator.FluentEmit(OpCodes.Readonly)
.FluentEmit(OpCodes.Ldelema, type);
}
/// <summary>
/// Pops an array reference (containing elements of the given type) and an index off the evaluation stack and pushes the address of the element at the given index, with restrictions on its use by other code
/// </summary>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
/// <param name="type">The type of elements in the array</param>
/// <param name="index">The index of the element to load the address of</param>
[PublicAPI]
public static ILGenerator LoadElementAddressAtIndexReadonly(this ILGenerator generator, Type type, uint index)
{
return generator.LoadConstant(index)
.LoadElementAddressReadonly(type);
}
/// <summary>
/// Pops an array reference (containing elements of the given type) and an index off the evaluation stack and pushes the address of the element at that array index, with restrictions on its use by other code
/// </summary>
/// <typeparam name="T">The type of elements in the array</typeparam>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
[PublicAPI]
public static ILGenerator LoadElementAddressReadonly<T>(this ILGenerator generator) => generator.LoadElementAddressReadonly(typeof(T));
/// <summary>
/// Pops an array reference (containing elements of the given type) and an index off the evaluation stack and pushes the address of the element at the given index, with restrictions on its use by other code
/// </summary>
/// <typeparam name="T">The type of elements in the array</typeparam>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
/// <param name="index">The index of the element to load the address of</param>
[PublicAPI]
public static ILGenerator LoadElementAddressAtIndexReadonly<T>(this ILGenerator generator, uint index)
{
return generator.LoadConstant(index)
.LoadElementAddressReadonly(typeof(T));
}
/// <summary>
/// Pops an array reference (containing elements of the given type), and index and a value of the given type, and stores the value in the array at that index
/// </summary>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
/// <param name="type">The type of elements in the array</param>
[PublicAPI]
public static ILGenerator StoreElement(this ILGenerator generator, Type type)
{
if (!type.IsValueType)
{
return generator.FluentEmit(OpCodes.Stelem_Ref);
}
else if (type == typeof(sbyte) || type == typeof(byte) || type == typeof(bool))
{
return generator.FluentEmit(OpCodes.Stelem_I1);
}
else if (type == typeof(short) || type == typeof(ushort))
{
return generator.FluentEmit(OpCodes.Stelem_I2);
}
else if (type == typeof(int) || type == typeof(uint))
{
return generator.FluentEmit(OpCodes.Stelem_I4);
}
else if (type == typeof(long) || type == typeof(ulong))
{
return generator.FluentEmit(OpCodes.Ldelem_I8);
}
else if (type == typeof(float))
{
return generator.FluentEmit(OpCodes.Stelem_R4);
}
else if (type == typeof(double))
{
return generator.FluentEmit(OpCodes.Stelem_R8);
}
else
{
return generator.FluentEmit(OpCodes.Stelem, type);
}
}
/// <summary>
/// Pops an array reference (containing elements of the given type), and index and a value of the given type, and stores the value in the array at that index
/// </summary>
/// <typeparam name="T">The type of elements in the array</typeparam>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
[PublicAPI]
public static ILGenerator StoreElement<T>(this ILGenerator generator) => generator.StoreElement(typeof(T));
}
}