/
Emit.Arithmetic.cs
210 lines (179 loc) · 8.05 KB
/
Emit.Arithmetic.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
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
using Sigil.Impl;
using System.Collections.Generic;
using System.Reflection.Emit;
namespace Sigil
{
public partial class Emit<DelegateType>
{
private void VerifyAndDoArithmetic(string name, OpCode addOp, bool allowReference = false)
{
// See: http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.add.aspx
// For legal arguments table
IEnumerable<StackTransition> transitions;
if (allowReference)
{
// TODO: figure out how to represent pointer/reference addition rules
transitions =
new[]
{
new StackTransition(new [] { typeof(int), typeof(int) }, new [] { typeof(int) }),
new StackTransition(new [] { typeof(int), typeof(NativeIntType) }, new [] { typeof(NativeIntType) }),
new StackTransition(new [] { typeof(long), typeof(long) }, new [] { typeof(long) }),
new StackTransition(new [] { typeof(NativeIntType), typeof(int) }, new [] { typeof(NativeIntType) }),
new StackTransition(new [] { typeof(NativeIntType), typeof(NativeIntType) }, new [] { typeof(NativeIntType) }),
new StackTransition(new [] { typeof(float), typeof(float) }, new [] { typeof(float) }),
new StackTransition(new [] { typeof(double), typeof(double) }, new [] { typeof(double) })
};
}
else
{
transitions =
new[]
{
new StackTransition(new [] { typeof(int), typeof(int) }, new [] { typeof(int) }),
new StackTransition(new [] { typeof(int), typeof(NativeIntType) }, new [] { typeof(NativeIntType) }),
new StackTransition(new [] { typeof(long), typeof(long) }, new [] { typeof(long) }),
new StackTransition(new [] { typeof(NativeIntType), typeof(int) }, new [] { typeof(NativeIntType) }),
new StackTransition(new [] { typeof(NativeIntType), typeof(NativeIntType) }, new [] { typeof(NativeIntType) }),
new StackTransition(new [] { typeof(float), typeof(float) }, new [] { typeof(float) }),
new StackTransition(new [] { typeof(double), typeof(double) }, new [] { typeof(double) })
};
}
UpdateState(addOp, Wrap(transitions, name));
}
/// <summary>
/// Pops two arguments off the stack, adds them, and pushes the result.
/// </summary>
public Emit<DelegateType> Add()
{
VerifyAndDoArithmetic("Add", OpCodes.Add, allowReference: true);
return this;
}
/// <summary>
/// Pops two arguments off the stack, adds them, and pushes the result.
///
/// Throws an OverflowException if the result overflows the destination type.
/// </summary>
public Emit<DelegateType> AddOverflow()
{
VerifyAndDoArithmetic("AddOverflow", OpCodes.Add_Ovf, allowReference: true);
return this;
}
/// <summary>
/// Pops two arguments off the stack, adds them as if they were unsigned, and pushes the result.
///
/// Throws an OverflowException if the result overflows the destination type.
/// </summary>
public Emit<DelegateType> UnsignedAddOverflow()
{
VerifyAndDoArithmetic("UnsignedAddOverflow", OpCodes.Add_Ovf_Un, allowReference: true);
return this;
}
/// <summary>
/// Pops two arguments off the stack, divides the second by the first, and pushes the result.
/// </summary>
public Emit<DelegateType> Divide()
{
VerifyAndDoArithmetic("Divide", OpCodes.Div);
return this;
}
/// <summary>
/// Pops two arguments off the stack, divides the second by the first as if they were unsigned, and pushes the result.
/// </summary>
public Emit<DelegateType> UnsignedDivide()
{
VerifyAndDoArithmetic("UnsignedDivide", OpCodes.Div_Un);
return this;
}
/// <summary>
/// Pops two arguments off the stack, multiplies them, and pushes the result.
/// </summary>
public Emit<DelegateType> Multiply()
{
VerifyAndDoArithmetic("Multiply", OpCodes.Mul);
return this;
}
/// <summary>
/// Pops two arguments off the stack, multiplies them, and pushes the result.
///
/// Throws an OverflowException if the result overflows the destination type.
/// </summary>
public Emit<DelegateType> MultiplyOverflow()
{
VerifyAndDoArithmetic("MultiplyOverflow", OpCodes.Mul_Ovf);
return this;
}
/// <summary>
/// Pops two arguments off the stack, multiplies them as if they were unsigned, and pushes the result.
///
/// Throws an OverflowException if the result overflows the destination type.
/// </summary>
public Emit<DelegateType> UnsignedMultiplyOverflow()
{
VerifyAndDoArithmetic("UnsignedMultiplyOverflow", OpCodes.Mul_Ovf_Un);
return this;
}
/// <summary>
/// Pops two arguments off the stack, calculates the remainder of the second divided by the first, and pushes the result.
/// </summary>
public Emit<DelegateType> Remainder()
{
VerifyAndDoArithmetic("Remainder", OpCodes.Rem);
return this;
}
/// <summary>
/// Pops two arguments off the stack, calculates the remainder of the second divided by the first as if both were unsigned, and pushes the result.
/// </summary>
public Emit<DelegateType> UnsignedRemainder()
{
VerifyAndDoArithmetic("UnsignedRemainder", OpCodes.Rem_Un);
return this;
}
/// <summary>
/// Pops two arguments off the stack, subtracts the first from the second, and pushes the result.
/// </summary>
public Emit<DelegateType> Subtract()
{
VerifyAndDoArithmetic("Subtract", OpCodes.Sub);
return this;
}
/// <summary>
/// Pops two arguments off the stack, subtracts the first from the second, and pushes the result.
///
/// Throws an OverflowException if the result overflows the destination type.
/// </summary>
public Emit<DelegateType> SubtractOverflow()
{
VerifyAndDoArithmetic("SubtractOverflow", OpCodes.Sub_Ovf);
return this;
}
/// <summary>
/// Pops two arguments off the stack, subtracts the first from the second as if they were unsigned, and pushes the result.
///
/// Throws an OverflowException if the result overflows the destination type.
/// </summary>
public Emit<DelegateType> UnsignedSubtractOverflow()
{
VerifyAndDoArithmetic("UnsignedSubtractOverflow", OpCodes.Sub_Ovf_Un);
return this;
}
/// <summary>
/// Pops an argument off the stack, negates it, and pushes the result.
/// </summary>
public Emit<DelegateType> Negate()
{
var transitions =
new[]
{
new StackTransition(new [] { typeof(int) }, new [] { typeof(int) }),
new StackTransition(new [] { typeof(NativeIntType) }, new [] { typeof(NativeIntType) }),
new StackTransition(new [] { typeof(long) }, new [] { typeof(long) }),
new StackTransition(new [] { typeof(float) }, new [] { typeof(float) }),
new StackTransition(new [] { typeof(double) }, new [] { typeof(double) })
};
UpdateState(OpCodes.Neg, Wrap(transitions, "Negate"));
return this;
}
}
}