-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackVariableInstruction.cs
More file actions
170 lines (154 loc) · 7.29 KB
/
Copy pathStackVariableInstruction.cs
File metadata and controls
170 lines (154 loc) · 7.29 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
using System;
using System.Reflection.Emit;
using HarmonyLib;
namespace TranspileUtilities
{
/// <summary>
/// Handles creating the complementary load or store operand for a code instruction.
/// </summary>
public class StackVariableInstruction
{
/// <summary>
/// The load version of the source instruction
/// </summary>
public CodeInstruction Load { get; private set; }
/// <summary>
/// The store version of the source instruction
/// </summary>
public CodeInstruction Store { get; private set; }
/// <summary>
/// Creates the Load and Store code complementary instructions based on the
/// instruction provided.
/// </summary>
/// <param name="instruction">The load or store construction to build the Load and Store properties</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public StackVariableInstruction(CodeInstruction instruction)
{
if(instruction == null) throw new ArgumentNullException(nameof(instruction));
switch (instruction)
{
case CodeInstruction x when x.IsStloc():
Load = GetLoadLocal(instruction);
Store = instruction;
break;
case CodeInstruction x when x.IsLdloc():
Load = instruction;
Store = GetStoreLocal(instruction);
break;
default:
throw new ArgumentException($"Instruction OpCode is not a local load or store type.", nameof(instruction));
};
}
/// <summary>
/// Creates a StackVariableInstruction based on the code instruction.
/// Used to create a CodeMatch predicate. Creates a new StackVariableInstruction if the match succeeds.
/// </summary>
/// <param name="expectStore">If true, will expect the codeInstruction to be a store opcode. Otherwise, a load opcode</param>
/// <param name="codeInstruction">The instruction source</param>
/// <param name="stackVariable">The newly created StackVariableInstruction instance. Will be null if no match.</param>
/// <returns>True if the opcode matched the isStore expectations</returns>
/// <example>
/// StackVariableInstruction cardListVariable = null;
/// ...
/// new CodeMatch(instruction => StackVariableInstruction.Create(expectStore: true, instruction, out cardListVariable))
/// ...
/// </example>
public static bool Create(bool expectStore, CodeInstruction codeInstruction, out StackVariableInstruction stackVariable)
{
stackVariable = null;
//Excludes ldloca. They are included in the IsLdloc() test, but do not have a store complement and therefore unsupported.
if(codeInstruction.opcode == OpCodes.Ldloca || codeInstruction.opcode == OpCodes.Ldloca_S)
{
return false;
}
else if((expectStore && codeInstruction.IsStloc()) || (expectStore == false && codeInstruction.IsLdloc()))
{
stackVariable = new StackVariableInstruction(codeInstruction);
return true;
}
else
{
return false;
}
}
/// <summary>
/// Gets the load version of the instruction
/// </summary>
/// <param name="storeInstruction"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ApplicationException"></exception>
private CodeInstruction GetLoadLocal(CodeInstruction storeInstruction)
{
if (storeInstruction?.IsStloc() == false)
{
throw new ArgumentException("Must be a store instruction", nameof(storeInstruction));
}
switch (storeInstruction.opcode)
{
case OpCode x when x == OpCodes.Stloc:
VerifyLocalBuilder(storeInstruction.operand);
return new CodeInstruction(OpCodes.Ldloc, storeInstruction.operand);
case OpCode x when x == OpCodes.Stloc_S:
VerifyLocalBuilder(storeInstruction.operand);
return new CodeInstruction(OpCodes.Ldloc_S, storeInstruction.operand);
case OpCode x when x == OpCodes.Stloc_0:
return new CodeInstruction(OpCodes.Ldloc_0);
case OpCode x when x == OpCodes.Stloc_1:
return new CodeInstruction(OpCodes.Ldloc_1);
case OpCode x when x == OpCodes.Stloc_2:
return new CodeInstruction(OpCodes.Ldloc_2);
case OpCode x when x == OpCodes.Stloc_3:
return new CodeInstruction(OpCodes.Ldloc_3);
default:
throw new ApplicationException($"Unexpected opcode: {storeInstruction.opcode}");
}
}
/// <summary>
/// Gets the store version of the instruction
/// </summary>
/// <param name="loadInstruction"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ApplicationException"></exception>
private CodeInstruction GetStoreLocal( CodeInstruction loadInstruction)
{
if (loadInstruction?.IsLdloc() == false)
{
throw new ArgumentException("Must be a load instruction", nameof(loadInstruction));
}
switch (loadInstruction.opcode)
{
case OpCode x when x == OpCodes.Ldloc:
VerifyLocalBuilder(loadInstruction.operand);
return new CodeInstruction(OpCodes.Stloc, loadInstruction.operand);
case OpCode x when x == OpCodes.Ldloc_S:
VerifyLocalBuilder(loadInstruction.operand);
return new CodeInstruction(OpCodes.Stloc_S, loadInstruction.operand);
case OpCode x when x == OpCodes.Ldloc_0:
return new CodeInstruction(OpCodes.Stloc_0);
case OpCode x when x == OpCodes.Ldloc_1:
return new CodeInstruction(OpCodes.Stloc_1);
case OpCode x when x == OpCodes.Ldloc_2:
return new CodeInstruction(OpCodes.Stloc_2);
case OpCode x when x == OpCodes.Ldloc_3:
return new CodeInstruction(OpCodes.Stloc_3);
default:
throw new ApplicationException($"Unexpected opcode: {loadInstruction.opcode}");
}
}
/// <summary>
/// throws an exception if the operand is not a LocalBuilder or is null.
/// </summary>
/// <param name="operand">The operand of the CodeInstruction.</param>
/// <exception cref="ApplicationException"></exception>
private void VerifyLocalBuilder(object operand)
{
if ((operand is LocalBuilder localBuilder) == false || localBuilder == null)
{
throw new ApplicationException("*loc/*loc_s operand is not a local builder or is null");
}
}
}
}