-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLocals.cs
94 lines (88 loc) · 4.08 KB
/
Locals.cs
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
using System.Reflection.Emit;
using JetBrains.Annotations;
namespace ILGeneratorExtensions
{
/// <summary>
/// Contains extension methods for the manipulation of local variables
/// </summary>
public static partial class Locals
{
/// <summary>
/// Pushes the value of the given local onto the evaluation stack
/// </summary>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
/// <param name="local">The local to get the value of</param>
[PublicAPI]
public static ILGenerator LoadLocal(this ILGenerator generator, LocalBuilder local)
{
switch (local.LocalIndex)
{
case 0:
return generator.FluentEmit(OpCodes.Ldloc_0);
case 1:
return generator.FluentEmit(OpCodes.Ldloc_1);
case 2:
return generator.FluentEmit(OpCodes.Ldloc_2);
default:
return local.LocalIndex <= 255
? generator.FluentEmit(OpCodes.Ldloc_S, local)
: generator.FluentEmit(OpCodes.Ldloc, local);
}
}
/// <summary>
/// Pushes the value of the given local onto the evaluation stack
/// </summary>
/// <param name="generator"></param>
/// <param name="localName">The name of the fluently-specified local</param>
public static ILGenerator LoadLocal(this ILGenerator generator, string localName)
=> generator.LoadLocal(generator.GetLocal(localName));
/// <summary>
/// Pushes the address of the given local onto the evaluation stack
/// </summary>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
/// <param name="local">The local to get the address of</param>
[PublicAPI]
public static ILGenerator LoadLocalAddress(this ILGenerator generator, LocalBuilder local)
{
return local.LocalIndex <= 255
? generator.FluentEmit(OpCodes.Ldloca_S, local)
: generator.FluentEmit(OpCodes.Ldloca, local);
}
/// <summary>
/// Pushes the address of the given local onto the evaluation stack
/// </summary>
/// <param name="generator"></param>
/// <param name="localName">The name of the fluently-specified local</param>
public static ILGenerator LoadLocalAddress(this ILGenerator generator, string localName)
=> generator.LoadLocalAddress(generator.GetLocal(localName));
/// <summary>
/// Pops a value from the evaluation stack and stores it in the given local
/// </summary>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
/// <param name="local">The local to store the evaluation stack value in</param>
[PublicAPI]
public static ILGenerator StoreInLocal(this ILGenerator generator, LocalBuilder local)
{
switch (local.LocalIndex)
{
case 0:
return generator.FluentEmit(OpCodes.Stloc_0);
case 1:
return generator.FluentEmit(OpCodes.Stloc_1);
case 2:
return generator.FluentEmit(OpCodes.Stloc_2);
default:
return (local.LocalIndex <= 255)
? generator.FluentEmit(OpCodes.Stloc_S, local)
: generator.FluentEmit(OpCodes.Stloc, local);
}
}
/// <summary>
/// Pops a value from the evaluation stack and stores it in the given local
/// </summary>
/// <param name="generator"></param>
/// <param name="localName">The name of the fluently-specified local</param>
public static ILGenerator StoreInLocal(this ILGenerator generator, string localName)
=> generator.StoreInLocal(generator.GetLocal(localName));
}
}