Merge branch 'BigIntegerParse'
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Dynamic / Interpreter / Instructions / NegateInstruction.cs
1 // 
2 // NegateInstruction.cs:
3 //
4 // Authors: Marek Safar (marek.safar@gmail.com)
5 //     
6 // Copyright 2014 Xamarin Inc
7 //
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:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 // 
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.
26 //
27 //
28
29 using System;
30 using System.Diagnostics;
31 using Microsoft.Scripting.Runtime;
32 using Microsoft.Scripting.Utils;
33
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;
38
39         public override int ConsumedStack { get { return 1; } }
40         public override int ProducedStack { get { return 1; } }
41
42         private NegateInstruction() {
43         }
44
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));
49                 return 1;
50             }
51         }
52
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);
57                 return 1;
58             }
59         }
60
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);
65                 return 1;
66             }
67         }
68
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);
73                 return 1;
74
75             }
76         }
77
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);
82                 return 1;
83
84             }
85         }
86
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);
91                 return 1;
92
93             }
94         }
95
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);
100                 return 1;
101
102             }
103         }
104
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));
109                 return 1;
110             }
111         }
112
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);
117                 return 1;
118             }
119         }
120
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);
125                 return 1;
126             }
127         }
128
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);
133                 return 1;
134
135             }
136         }
137
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);
142                 return 1;
143
144             }
145         }
146
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);
151                 return 1;
152
153             }
154         }
155
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);
160                 return 1;
161
162             }
163         }
164
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());
175
176                 default:
177                     throw Assert.Unreachable;
178             }
179         }
180
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());
191
192                 default:
193                     throw Assert.Unreachable;
194             }
195         }
196
197         public override string ToString() {
198             return "Negate()";
199         }
200     }
201
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;
205
206         public override int ConsumedStack { get { return 1; } }
207         public override int ProducedStack { get { return 1; } }
208
209         private NegateOvfInstruction() {
210         }
211
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));
216                 return 1;
217             }
218         }
219
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));
224                 return 1;
225             }
226         }
227
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));
232                 return 1;
233             }
234         }
235
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));
240                 return 1;
241
242             }
243         }
244
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));
249                 return 1;
250
251             }
252         }
253
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);
258                 return 1;
259
260             }
261         }
262
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);
267                 return 1;
268
269             }
270         }
271
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));
276                 return 1;
277             }
278         }
279
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);
284                 return 1;
285             }
286         }
287
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);
292                 return 1;
293             }
294         }
295
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);
300                 return 1;
301
302             }
303         }
304
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);
309                 return 1;
310
311             }
312         }
313
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);
318                 return 1;
319
320             }
321         }
322
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);
327                 return 1;
328
329             }
330         }
331
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());
342
343                 default:
344                     throw Assert.Unreachable;
345             }
346         }
347
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());
358
359                 default:
360                     throw Assert.Unreachable;
361             }
362         }
363
364         public override string ToString() {
365             return "NegateOvf()";
366         }
367     }
368 }