2 // NegateInstruction.cs:
4 // Authors: Marek Safar (marek.safar@gmail.com)
6 // Copyright 2014 Xamarin Inc
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Diagnostics;
31 using Microsoft.Scripting.Runtime;
32 using Microsoft.Scripting.Utils;
34 namespace Microsoft.Scripting.Interpreter {
35 internal abstract class NegateInstruction : Instruction {
36 private static Instruction _Int16, _Int32, _Int64, _UInt16, _UInt32, _Single, _Double;
37 private static Instruction _Int16Lifted, _Int32Lifted, _Int64Lifted, _UInt16Lifted, _UInt32Lifted, _SingleLifted, _DoubleLifted;
39 public override int ConsumedStack { get { return 1; } }
40 public override int ProducedStack { get { return 1; } }
42 private NegateInstruction() {
45 internal sealed class NegateInt32 : NegateInstruction {
46 public override int Run(InterpretedFrame frame) {
47 var v = frame.Data[frame.StackIndex - 1];
48 frame.Data[frame.StackIndex - 1] = ScriptingRuntimeHelpers.Int32ToObject(unchecked(-(Int32)v));
53 internal sealed class NegateInt16 : NegateInstruction {
54 public override int Run(InterpretedFrame frame) {
55 var v = (Int16)frame.Data[frame.StackIndex - 1];
56 frame.Data[frame.StackIndex - 1] = (Int16)unchecked(-v);
61 internal sealed class NegateInt64 : NegateInstruction {
62 public override int Run(InterpretedFrame frame) {
63 var v = (Int64)frame.Data[frame.StackIndex - 1];
64 frame.Data[frame.StackIndex - 1] = (Int64)unchecked(-v);
69 internal sealed class NegateUInt16 : NegateInstruction {
70 public override int Run(InterpretedFrame frame) {
71 var v = (UInt16)frame.Data[frame.StackIndex - 1];
72 frame.Data[frame.StackIndex - 1] = (UInt16)unchecked(-v);
78 internal sealed class NegateUInt32 : NegateInstruction {
79 public override int Run(InterpretedFrame frame) {
80 var v = (UInt32)frame.Data[frame.StackIndex - 1];
81 frame.Data[frame.StackIndex - 1] = (UInt32)unchecked(-v);
87 internal sealed class NegateSingle : NegateInstruction {
88 public override int Run(InterpretedFrame frame) {
89 var v = (Single)frame.Data[frame.StackIndex - 1];
90 frame.Data[frame.StackIndex - 1] = (Single)unchecked(-v);
96 internal sealed class NegateDouble : NegateInstruction {
97 public override int Run(InterpretedFrame frame) {
98 var v = (Double)frame.Data[frame.StackIndex - 1];
99 frame.Data[frame.StackIndex - 1] = (Double)unchecked(-v);
105 internal sealed class NegateInt32Lifted : NegateInstruction {
106 public override int Run(InterpretedFrame frame) {
107 var v = (Int32?)frame.Data[frame.StackIndex - 1];
108 frame.Data[frame.StackIndex - 1] = (Int32?)(unchecked(-v));
113 internal sealed class NegateInt16Lifted : NegateInstruction {
114 public override int Run(InterpretedFrame frame) {
115 var v = (Int16?)frame.Data[frame.StackIndex - 1];
116 frame.Data[frame.StackIndex - 1] = (Int16?)unchecked(-v);
121 internal sealed class NegateInt64Lifted : NegateInstruction {
122 public override int Run(InterpretedFrame frame) {
123 var v = (Int64?)frame.Data[frame.StackIndex - 1];
124 frame.Data[frame.StackIndex - 1] = (Int64?)unchecked(-v);
129 internal sealed class NegateUInt16Lifted : NegateInstruction {
130 public override int Run(InterpretedFrame frame) {
131 var v = (UInt16?)frame.Data[frame.StackIndex - 1];
132 frame.Data[frame.StackIndex - 1] = (UInt16?)unchecked(-v);
138 internal sealed class NegateUInt32Lifted : NegateInstruction {
139 public override int Run(InterpretedFrame frame) {
140 var v = (UInt32?)frame.Data[frame.StackIndex - 1];
141 frame.Data[frame.StackIndex - 1] = (UInt32?)unchecked(-v);
147 internal sealed class NegateSingleLifted : NegateInstruction {
148 public override int Run(InterpretedFrame frame) {
149 var v = (Single?)frame.Data[frame.StackIndex - 1];
150 frame.Data[frame.StackIndex - 1] = (Single?)unchecked(-v);
156 internal sealed class NegateDoubleLifted : NegateInstruction {
157 public override int Run(InterpretedFrame frame) {
158 var v = (Double?)frame.Data[frame.StackIndex - 1];
159 frame.Data[frame.StackIndex - 1] = (Double?)unchecked(-v);
165 public static Instruction Create(Type type) {
166 Debug.Assert(!type.IsEnum());
167 switch (type.GetTypeCode()) {
168 case TypeCode.Int16: return _Int16 ?? (_Int16 = new NegateInt16());
169 case TypeCode.Int32: return _Int32 ?? (_Int32 = new NegateInt32());
170 case TypeCode.Int64: return _Int64 ?? (_Int64 = new NegateInt64());
171 case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new NegateUInt16());
172 case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new NegateUInt32());
173 case TypeCode.Single: return _Single ?? (_Single = new NegateSingle());
174 case TypeCode.Double: return _Double ?? (_Double = new NegateDouble());
177 throw Assert.Unreachable;
181 public static Instruction CreateLifted(Type type) {
182 Debug.Assert(!type.IsEnum());
183 switch (type.GetTypeCode()) {
184 case TypeCode.Int16: return _Int16Lifted ?? (_Int16Lifted = new NegateInt16Lifted());
185 case TypeCode.Int32: return _Int32Lifted ?? (_Int32Lifted = new NegateInt32Lifted());
186 case TypeCode.Int64: return _Int64Lifted ?? (_Int64Lifted = new NegateInt64Lifted());
187 case TypeCode.UInt16: return _UInt16Lifted ?? (_UInt16Lifted = new NegateUInt16Lifted());
188 case TypeCode.UInt32: return _UInt32Lifted ?? (_UInt32Lifted = new NegateUInt32Lifted());
189 case TypeCode.Single: return _SingleLifted ?? (_SingleLifted = new NegateSingleLifted());
190 case TypeCode.Double: return _DoubleLifted ?? (_DoubleLifted = new NegateDoubleLifted());
193 throw Assert.Unreachable;
197 public override string ToString() {
202 internal abstract class NegateOvfInstruction : Instruction {
203 private static Instruction _Int16, _Int32, _Int64, _UInt16, _UInt32, _Single, _Double;
204 private static Instruction _Int16Lifted, _Int32Lifted, _Int64Lifted, _UInt16Lifted, _UInt32Lifted, _SingleLifted, _DoubleLifted;
206 public override int ConsumedStack { get { return 1; } }
207 public override int ProducedStack { get { return 1; } }
209 private NegateOvfInstruction() {
212 internal sealed class NegateOvfInt32 : NegateOvfInstruction {
213 public override int Run(InterpretedFrame frame) {
214 var v = (Int32)frame.Data[frame.StackIndex - 1];
215 frame.Data[frame.StackIndex - 1] = ScriptingRuntimeHelpers.Int32ToObject(checked(-v));
220 internal sealed class NegateOvfInt16 : NegateOvfInstruction {
221 public override int Run(InterpretedFrame frame) {
222 var v = (Int16)frame.Data[frame.StackIndex - 1];
223 frame.Data[frame.StackIndex - 1] = checked((Int16)(-v));
228 internal sealed class NegateOvfInt64 : NegateOvfInstruction {
229 public override int Run(InterpretedFrame frame) {
230 var v = (Int64)frame.Data[frame.StackIndex - 1];
231 frame.Data[frame.StackIndex - 1] = checked((Int64)(-v));
236 internal sealed class NegateOvfUInt16 : NegateOvfInstruction {
237 public override int Run(InterpretedFrame frame) {
238 var v = (UInt16)frame.Data[frame.StackIndex - 1];
239 frame.Data[frame.StackIndex - 1] = checked((UInt16)(-v));
245 internal sealed class NegateOvfUInt32 : NegateOvfInstruction {
246 public override int Run(InterpretedFrame frame) {
247 var v = (UInt32)frame.Data[frame.StackIndex - 1];
248 frame.Data[frame.StackIndex - 1] = checked((UInt32)(-v));
254 internal sealed class NegateOvfSingle : NegateOvfInstruction {
255 public override int Run(InterpretedFrame frame) {
256 var v = (Single)frame.Data[frame.StackIndex - 1];
257 frame.Data[frame.StackIndex - 1] = (Single)checked(-v);
263 internal sealed class NegateOvfDouble : NegateOvfInstruction {
264 public override int Run(InterpretedFrame frame) {
265 var v = (Double)frame.Data[frame.StackIndex - 1];
266 frame.Data[frame.StackIndex - 1] = (Double)checked(-v);
272 internal sealed class NegateOvfInt32Lifted : NegateOvfInstruction {
273 public override int Run(InterpretedFrame frame) {
274 var v = (Int32?)frame.Data[frame.StackIndex - 1];
275 frame.Data[frame.StackIndex - 1] = (Int32?)(unchecked(-v));
280 internal sealed class NegateOvfInt16Lifted : NegateOvfInstruction {
281 public override int Run(InterpretedFrame frame) {
282 var v = (Int16?)frame.Data[frame.StackIndex - 1];
283 frame.Data[frame.StackIndex - 1] = (Int16?)unchecked(-v);
288 internal sealed class NegateOvfInt64Lifted : NegateOvfInstruction {
289 public override int Run(InterpretedFrame frame) {
290 var v = (Int64?)frame.Data[frame.StackIndex - 1];
291 frame.Data[frame.StackIndex - 1] = (Int64?)unchecked(-v);
296 internal sealed class NegateOvfUInt16Lifted : NegateOvfInstruction {
297 public override int Run(InterpretedFrame frame) {
298 var v = (UInt16?)frame.Data[frame.StackIndex - 1];
299 frame.Data[frame.StackIndex - 1] = (UInt16?)unchecked(-v);
305 internal sealed class NegateOvfUInt32Lifted : NegateOvfInstruction {
306 public override int Run(InterpretedFrame frame) {
307 var v = (UInt32?)frame.Data[frame.StackIndex - 1];
308 frame.Data[frame.StackIndex - 1] = (UInt32?)unchecked(-v);
314 internal sealed class NegateOvfSingleLifted : NegateOvfInstruction {
315 public override int Run(InterpretedFrame frame) {
316 var v = (Single?)frame.Data[frame.StackIndex - 1];
317 frame.Data[frame.StackIndex - 1] = (Single?)unchecked(-v);
323 internal sealed class NegateOvfDoubleLifted : NegateOvfInstruction {
324 public override int Run(InterpretedFrame frame) {
325 var v = (Double?)frame.Data[frame.StackIndex - 1];
326 frame.Data[frame.StackIndex - 1] = (Double?)unchecked(-v);
332 public static Instruction Create(Type type) {
333 Debug.Assert(!type.IsEnum());
334 switch (type.GetTypeCode()) {
335 case TypeCode.Int16: return _Int16 ?? (_Int16 = new NegateOvfInt16());
336 case TypeCode.Int32: return _Int32 ?? (_Int32 = new NegateOvfInt32());
337 case TypeCode.Int64: return _Int64 ?? (_Int64 = new NegateOvfInt64());
338 case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new NegateOvfUInt16());
339 case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new NegateOvfUInt32());
340 case TypeCode.Single: return _Single ?? (_Single = new NegateOvfSingle());
341 case TypeCode.Double: return _Double ?? (_Double = new NegateOvfDouble());
344 throw Assert.Unreachable;
348 public static Instruction CreateLifted(Type type) {
349 Debug.Assert(!type.IsEnum());
350 switch (type.GetTypeCode()) {
351 case TypeCode.Int16: return _Int16Lifted ?? (_Int16Lifted = new NegateOvfInt16Lifted());
352 case TypeCode.Int32: return _Int32Lifted ?? (_Int32Lifted = new NegateOvfInt32Lifted());
353 case TypeCode.Int64: return _Int64Lifted ?? (_Int64Lifted = new NegateOvfInt64Lifted());
354 case TypeCode.UInt16: return _UInt16Lifted ?? (_UInt16Lifted = new NegateOvfUInt16Lifted());
355 case TypeCode.UInt32: return _UInt32Lifted ?? (_UInt32Lifted = new NegateOvfUInt32Lifted());
356 case TypeCode.Single: return _SingleLifted ?? (_SingleLifted = new NegateOvfSingleLifted());
357 case TypeCode.Double: return _DoubleLifted ?? (_DoubleLifted = new NegateOvfDoubleLifted());
360 throw Assert.Unreachable;
364 public override string ToString() {
365 return "NegateOvf()";