Merge branch 'BigIntegerParse'
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Dynamic / Interpreter / Instructions / LessThanInstruction.cs
1 // 
2 // LessThanInstruction.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.Collections.Generic;
31 using System.Diagnostics;
32 using System.Reflection;
33 using System.Runtime.CompilerServices;
34 using Microsoft.Scripting.Runtime;
35 using Microsoft.Scripting.Utils;
36
37 namespace Microsoft.Scripting.Interpreter {
38     public abstract class LessThanInstruction : Instruction {
39         private static Instruction _SByte, _Int16, _Char, _Int32, _Int64, _Byte, _UInt16, _UInt32, _UInt64, _Single, _Double;
40         private static Instruction _SByteLifted, _Int16Lifted, _CharLifted, _Int32Lifted, _Int64Lifted, _ByteLifted, _UInt16Lifted, _UInt32Lifted, _UInt64Lifted, _SingleLifted, _DoubleLifted;
41
42         public override int ConsumedStack { get { return 2; } }
43         public override int ProducedStack { get { return 1; } }
44
45         private LessThanInstruction() {
46         }
47
48         public bool LiftedToNull { get; set; }
49
50         internal sealed class LessThanSByte : LessThanInstruction {
51             public override int Run(InterpretedFrame frame) {
52                 object l = frame.Data[frame.StackIndex - 2];
53                 object r = frame.Data[frame.StackIndex - 1];
54                 if (l == null || r == null)
55                     frame.Data[frame.StackIndex - 2] = LiftedToNull ? (object) null : (object) false;
56                 else
57                     frame.Data[frame.StackIndex - 2] = (SByte)l < (SByte)r;
58
59                 frame.StackIndex--;
60                 return +1;
61             }
62         }
63
64         internal sealed class LessThanInt16 : LessThanInstruction {
65             public override int Run(InterpretedFrame frame) {
66                 object l = frame.Data[frame.StackIndex - 2];
67                 object r = frame.Data[frame.StackIndex - 1];
68                 if (l == null || r == null)
69                     frame.Data[frame.StackIndex - 2] = LiftedToNull ? (object) null : (object) false;
70                 else
71                     frame.Data[frame.StackIndex - 2] = (Int16)l < (Int16)r;
72
73                 frame.StackIndex--;
74                 return +1;
75             }
76         }
77
78         internal sealed class LessThanChar : LessThanInstruction {
79             public override int Run(InterpretedFrame frame) {
80                 object l = frame.Data[frame.StackIndex - 2];
81                 object r = frame.Data[frame.StackIndex - 1];
82                 if (l == null || r == null)
83                     frame.Data[frame.StackIndex - 2] = LiftedToNull ? (object) null : (object) false;
84                 else
85                     frame.Data[frame.StackIndex - 2] = (Char)l < (Char)r;
86
87                 frame.StackIndex--;
88                 return +1;
89             }
90         }
91
92         internal sealed class LessThanInt32 : LessThanInstruction {
93             public override int Run(InterpretedFrame frame) {
94                 object l = frame.Data[frame.StackIndex - 2];
95                 object r = frame.Data[frame.StackIndex - 1];
96                 if (l == null || r == null)
97                     frame.Data[frame.StackIndex - 2] = LiftedToNull ? (object) null : (object) false;
98                 else
99                     frame.Data[frame.StackIndex - 2] = (Int32)l < (Int32)r;
100
101                 frame.StackIndex--;
102                 return +1;
103             }
104         }
105
106         internal sealed class LessThanInt64 : LessThanInstruction {
107             public override int Run(InterpretedFrame frame) {
108                 object l = frame.Data[frame.StackIndex - 2];
109                 object r = frame.Data[frame.StackIndex - 1];
110                 if (l == null || r == null)
111                     frame.Data[frame.StackIndex - 2] = LiftedToNull ? (object) null : (object) false;
112                 else
113                     frame.Data[frame.StackIndex - 2] = (Int64)l < (Int64)r;
114
115                 frame.StackIndex--;
116                 return +1;
117             }
118         }
119
120         internal sealed class LessThanByte : LessThanInstruction {
121             public override int Run(InterpretedFrame frame) {
122                 object l = frame.Data[frame.StackIndex - 2];
123                 object r = frame.Data[frame.StackIndex - 1];
124                 if (l == null || r == null)
125                     frame.Data[frame.StackIndex - 2] = LiftedToNull ? (object) null : (object) false;
126                 else
127                     frame.Data[frame.StackIndex - 2] = (Byte)l < (Byte)r;
128
129                 frame.StackIndex--;
130                 return +1;
131             }
132         }
133
134         internal sealed class LessThanUInt16 : LessThanInstruction {
135             public override int Run(InterpretedFrame frame) {
136                 object l = frame.Data[frame.StackIndex - 2];
137                 object r = frame.Data[frame.StackIndex - 1];
138                 if (l == null || r == null)
139                     frame.Data[frame.StackIndex - 2] = LiftedToNull ? (object) null : (object) false;
140                 else
141                     frame.Data[frame.StackIndex - 2] = (UInt16)l < (UInt16)r;
142
143                 frame.StackIndex--;
144                 return +1;
145             }
146         }
147
148         internal sealed class LessThanUInt32 : LessThanInstruction {
149             public override int Run(InterpretedFrame frame) {
150                 object l = frame.Data[frame.StackIndex - 2];
151                 object r = frame.Data[frame.StackIndex - 1];
152                 if (l == null || r == null)
153                     frame.Data[frame.StackIndex - 2] = LiftedToNull ? (object) null : (object) false;
154                 else
155                     frame.Data[frame.StackIndex - 2] = (UInt32)l < (UInt32)r;
156
157                 frame.StackIndex--;
158                 return +1;
159             }
160         }
161
162         internal sealed class LessThanUInt64 : LessThanInstruction {
163             public override int Run(InterpretedFrame frame) {
164                 object l = frame.Data[frame.StackIndex - 2];
165                 object r = frame.Data[frame.StackIndex - 1];
166                 if (l == null || r == null)
167                     frame.Data[frame.StackIndex - 2] = LiftedToNull ? (object) null : (object) false;
168                 else
169                     frame.Data[frame.StackIndex - 2] = (UInt64)l < (UInt64)r;
170
171                 frame.StackIndex--;
172                 return +1;
173             }
174         }
175
176         internal sealed class LessThanSingle : LessThanInstruction {
177             public override int Run(InterpretedFrame frame) {
178                 object l = frame.Data[frame.StackIndex - 2];
179                 object r = frame.Data[frame.StackIndex - 1];
180                 if (l == null || r == null)
181                     frame.Data[frame.StackIndex - 2] = LiftedToNull ? (object) null : (object) false;
182                 else
183                     frame.Data[frame.StackIndex - 2] = (Single)l < (Single)r;
184
185                 frame.StackIndex--;
186                 return +1;
187             }
188         }
189
190         internal sealed class LessThanDouble : LessThanInstruction {
191             public override int Run(InterpretedFrame frame) {
192                 object l = frame.Data[frame.StackIndex - 2];
193                 object r = frame.Data[frame.StackIndex - 1];
194                 if (l == null || r == null)
195                     frame.Data[frame.StackIndex - 2] = LiftedToNull ? (object) null : (object) false;
196                 else
197                     frame.Data[frame.StackIndex - 2] = (Double)l < (Double)r;
198
199                 frame.StackIndex--;
200                 return +1;
201             }
202         }
203
204         public static Instruction Create(Type type) {
205             Debug.Assert(!type.IsEnum());
206             switch (type.GetTypeCode()) {
207                 case TypeCode.SByte: return _SByte ?? (_SByte = new LessThanSByte());
208                 case TypeCode.Byte: return _Byte ?? (_Byte = new LessThanByte());
209                 case TypeCode.Char: return _Char ?? (_Char = new LessThanChar());
210                 case TypeCode.Int16: return _Int16 ?? (_Int16 = new LessThanInt16());
211                 case TypeCode.Int32: return _Int32 ?? (_Int32 = new LessThanInt32());
212                 case TypeCode.Int64: return _Int64 ?? (_Int64 = new LessThanInt64());
213                 case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new LessThanUInt16());
214                 case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new LessThanUInt32());
215                 case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new LessThanUInt64());
216                 case TypeCode.Single: return _Single ?? (_Single = new LessThanSingle());
217                 case TypeCode.Double: return _Double ?? (_Double = new LessThanDouble());
218
219                 default:
220                     throw Assert.Unreachable;
221             }
222         }
223
224         public static Instruction CreateLifted(Type type) {
225             Debug.Assert(!type.IsEnum());
226             switch (type.GetTypeCode()) {
227                 case TypeCode.SByte: return _SByteLifted ?? (_SByteLifted = new LessThanSByte() { LiftedToNull = true });
228                 case TypeCode.Byte: return _ByteLifted ?? (_ByteLifted = new LessThanByte() { LiftedToNull = true });
229                 case TypeCode.Char: return _CharLifted ?? (_CharLifted = new LessThanChar() { LiftedToNull = true });
230                 case TypeCode.Int16: return _Int16Lifted ?? (_Int16Lifted = new LessThanInt16() { LiftedToNull = true });
231                 case TypeCode.Int32: return _Int32Lifted ?? (_Int32Lifted = new LessThanInt32() { LiftedToNull = true });
232                 case TypeCode.Int64: return _Int64Lifted ?? (_Int64Lifted = new LessThanInt64() { LiftedToNull = true });
233                 case TypeCode.UInt16: return _UInt16Lifted ?? (_UInt16Lifted = new LessThanUInt16() { LiftedToNull = true });
234                 case TypeCode.UInt32: return _UInt32Lifted ?? (_UInt32Lifted = new LessThanUInt32() { LiftedToNull = true });
235                 case TypeCode.UInt64: return _UInt64Lifted ?? (_UInt64Lifted = new LessThanUInt64() { LiftedToNull = true });
236                 case TypeCode.Single: return _SingleLifted ?? (_SingleLifted = new LessThanSingle() { LiftedToNull = true });
237                 case TypeCode.Double: return _DoubleLifted ?? (_DoubleLifted = new LessThanDouble() { LiftedToNull = true });
238
239                 default:
240                     throw Assert.Unreachable;
241             }
242         }
243
244         public override string ToString() {
245             return "LessThan()";
246         }
247     }
248 }