Merge branch 'BigIntegerParse'
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Dynamic / Interpreter / Instructions / MulInstruction.cs
1 // 
2 // MulInstruction.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 MulInstruction : Instruction {
36         private static Instruction _Int16, _Int32, _Int64, _UInt16, _UInt32, _UInt64, _Single, _Double;
37
38         public override int ConsumedStack { get { return 2; } }
39         public override int ProducedStack { get { return 1; } }
40
41         private MulInstruction() {
42         }
43
44         internal sealed class MulInt32 : MulInstruction {
45             public override int Run(InterpretedFrame frame) {
46                 object l = frame.Data[frame.StackIndex - 2];
47                 object r = frame.Data[frame.StackIndex - 1];
48                 frame.Data[frame.StackIndex - 2] = ScriptingRuntimeHelpers.Int32ToObject(unchecked((Int32)l * (Int32)r));
49                 frame.StackIndex--;
50                 return +1;
51             }
52         }
53
54         internal sealed class MulInt16 : MulInstruction {
55             public override int Run(InterpretedFrame frame) {
56                 object l = frame.Data[frame.StackIndex - 2];
57                 object r = frame.Data[frame.StackIndex - 1];
58                 frame.Data[frame.StackIndex - 2] = (Int16)unchecked((Int16)l * (Int16)r);
59                 frame.StackIndex--;
60                 return +1;
61             }
62         }
63
64         internal sealed class MulInt64 : MulInstruction {
65             public override int Run(InterpretedFrame frame) {
66                 object l = frame.Data[frame.StackIndex - 2];
67                 object r = frame.Data[frame.StackIndex - 1];
68                 frame.Data[frame.StackIndex - 2] = (Int64)unchecked((Int64)l * (Int64)r);
69                 frame.StackIndex--;
70                 return +1;
71             }
72         }
73
74         internal sealed class MulUInt16 : MulInstruction {
75             public override int Run(InterpretedFrame frame) {
76                 object l = frame.Data[frame.StackIndex - 2];
77                 object r = frame.Data[frame.StackIndex - 1];
78                 frame.Data[frame.StackIndex - 2] = (UInt16)unchecked((UInt16)l * (UInt16)r);
79                 frame.StackIndex--;
80                 return +1;
81             }
82         }
83
84         internal sealed class MulUInt32 : MulInstruction {
85             public override int Run(InterpretedFrame frame) {
86                 object l = frame.Data[frame.StackIndex - 2];
87                 object r = frame.Data[frame.StackIndex - 1];
88                 frame.Data[frame.StackIndex - 2] = (UInt32)unchecked((UInt32)l * (UInt32)r);
89                 frame.StackIndex--;
90                 return +1;
91             }
92         }
93
94         internal sealed class MulUInt64 : MulInstruction {
95             public override int Run(InterpretedFrame frame) {
96                 object l = frame.Data[frame.StackIndex - 2];
97                 object r = frame.Data[frame.StackIndex - 1];
98                 frame.Data[frame.StackIndex - 2] = (UInt64)unchecked((UInt64)l * (UInt64)r);
99                 frame.StackIndex--;
100                 return +1;
101             }
102         }
103
104         internal sealed class MulSingle : MulInstruction {
105             public override int Run(InterpretedFrame frame) {
106                 object l = frame.Data[frame.StackIndex - 2];
107                 object r = frame.Data[frame.StackIndex - 1];
108                 frame.Data[frame.StackIndex - 2] = (Single)((Single)l * (Single)r);
109                 frame.StackIndex--;
110                 return +1;
111             }
112         }
113
114         internal sealed class MulDouble : MulInstruction {
115             public override int Run(InterpretedFrame frame) {
116                 object l = frame.Data[frame.StackIndex - 2];
117                 object r = frame.Data[frame.StackIndex - 1];
118                 frame.Data[frame.StackIndex - 2] = (Double)l * (Double)r;
119                 frame.StackIndex--;
120                 return +1;
121             }
122         }
123
124         public static Instruction Create(Type type) {
125             Debug.Assert(!type.IsEnum());
126             switch (type.GetTypeCode()) {
127                 case TypeCode.Int16: return _Int16 ?? (_Int16 = new MulInt16());
128                 case TypeCode.Int32: return _Int32 ?? (_Int32 = new MulInt32());
129                 case TypeCode.Int64: return _Int64 ?? (_Int64 = new MulInt64());
130                 case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new MulUInt16());
131                 case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new MulUInt32());
132                 case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new MulUInt64());
133                 case TypeCode.Single: return _Single ?? (_Single = new MulSingle());
134                 case TypeCode.Double: return _Double ?? (_Double = new MulDouble());
135
136                 default:
137                     throw Assert.Unreachable;
138             }
139         }
140
141         public override string ToString() {
142             return "Mul()";
143         }
144     }
145
146     internal abstract class MulOvfInstruction : Instruction {
147         private static Instruction _Int16, _Int32, _Int64, _UInt16, _UInt32, _UInt64, _Single, _Double;
148
149         public override int ConsumedStack { get { return 2; } }
150         public override int ProducedStack { get { return 1; } }
151
152         private MulOvfInstruction() {
153         }
154
155         internal sealed class MulOvfInt32 : MulOvfInstruction {
156             public override int Run(InterpretedFrame frame) {
157                 object l = frame.Data[frame.StackIndex - 2];
158                 object r = frame.Data[frame.StackIndex - 1];
159                 frame.Data[frame.StackIndex - 2] = ScriptingRuntimeHelpers.Int32ToObject(checked((Int32)l * (Int32)r));
160                 frame.StackIndex--;
161                 return +1;
162             }
163         }
164
165         internal sealed class MulOvfInt16 : MulOvfInstruction {
166             public override int Run(InterpretedFrame frame) {
167                 object l = frame.Data[frame.StackIndex - 2];
168                 object r = frame.Data[frame.StackIndex - 1];
169                 frame.Data[frame.StackIndex - 2] = checked((Int16)((Int16)l * (Int16)r));
170                 frame.StackIndex--;
171                 return +1;
172             }
173         }
174
175         internal sealed class MulOvfInt64 : MulOvfInstruction {
176             public override int Run(InterpretedFrame frame) {
177                 object l = frame.Data[frame.StackIndex - 2];
178                 object r = frame.Data[frame.StackIndex - 1];
179                 frame.Data[frame.StackIndex - 2] = checked((Int64)((Int64)l * (Int64)r));
180                 frame.StackIndex--;
181                 return +1;
182             }
183         }
184
185         internal sealed class MulOvfUInt16 : MulOvfInstruction {
186             public override int Run(InterpretedFrame frame) {
187                 object l = frame.Data[frame.StackIndex - 2];
188                 object r = frame.Data[frame.StackIndex - 1];
189                 frame.Data[frame.StackIndex - 2] = checked((UInt16)((UInt16)l * (UInt16)r));
190                 frame.StackIndex--;
191                 return +1;
192             }
193         }
194
195         internal sealed class MulOvfUInt32 : MulOvfInstruction {
196             public override int Run(InterpretedFrame frame) {
197                 object l = frame.Data[frame.StackIndex - 2];
198                 object r = frame.Data[frame.StackIndex - 1];
199                 frame.Data[frame.StackIndex - 2] = checked((UInt32)((UInt32)l * (UInt32)r));
200                 frame.StackIndex--;
201                 return +1;
202             }
203         }
204
205         internal sealed class MulOvfUInt64 : MulOvfInstruction {
206             public override int Run(InterpretedFrame frame) {
207                 object l = frame.Data[frame.StackIndex - 2];
208                 object r = frame.Data[frame.StackIndex - 1];
209                 frame.Data[frame.StackIndex - 2] = checked((UInt64)((UInt64)l * (UInt64)r));
210                 frame.StackIndex--;
211                 return +1;
212             }
213         }
214
215         internal sealed class MulOvfSingle : MulOvfInstruction {
216             public override int Run(InterpretedFrame frame) {
217                 object l = frame.Data[frame.StackIndex - 2];
218                 object r = frame.Data[frame.StackIndex - 1];
219                 frame.Data[frame.StackIndex - 2] = (Single)((Single)l * (Single)r);
220                 frame.StackIndex--;
221                 return +1;
222             }
223         }
224
225         internal sealed class MulOvfDouble : MulOvfInstruction {
226             public override int Run(InterpretedFrame frame) {
227                 object l = frame.Data[frame.StackIndex - 2];
228                 object r = frame.Data[frame.StackIndex - 1];
229                 frame.Data[frame.StackIndex - 2] = (Double)l * (Double)r;
230                 frame.StackIndex--;
231                 return +1;
232             }
233         }
234
235         public static Instruction Create(Type type) {
236             Debug.Assert(!type.IsEnum());
237             switch (type.GetTypeCode()) {
238                 case TypeCode.Int16: return _Int16 ?? (_Int16 = new MulOvfInt16());
239                 case TypeCode.Int32: return _Int32 ?? (_Int32 = new MulOvfInt32());
240                 case TypeCode.Int64: return _Int64 ?? (_Int64 = new MulOvfInt64());
241                 case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new MulOvfUInt16());
242                 case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new MulOvfUInt32());
243                 case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new MulOvfUInt64());
244                 case TypeCode.Single: return _Single ?? (_Single = new MulOvfSingle());
245                 case TypeCode.Double: return _Double ?? (_Double = new MulOvfDouble());
246
247                 default:
248                     throw Assert.Unreachable;
249             }
250         }
251
252         public override string ToString() {
253             return "MulOvf()";
254         }
255     }
256 }