2 // GreaterThanInstruction.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.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;
37 namespace Microsoft.Scripting.Interpreter {
38 public abstract class GreaterThanInstruction : 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;
42 public override int ConsumedStack { get { return 2; } }
43 public override int ProducedStack { get { return 1; } }
45 private GreaterThanInstruction() {
48 public bool LiftedToNull { get; set; }
50 internal sealed class GreaterThanSByte : GreaterThanInstruction {
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;
57 frame.Data[frame.StackIndex - 2] = (SByte)l > (SByte)r;
64 internal sealed class GreaterThanInt16 : GreaterThanInstruction {
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;
71 frame.Data[frame.StackIndex - 2] = (Int16)l > (Int16)r;
78 internal sealed class GreaterThanChar : GreaterThanInstruction {
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;
85 frame.Data[frame.StackIndex - 2] = (Char)l > (Char)r;
92 internal sealed class GreaterThanInt32 : GreaterThanInstruction {
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;
99 frame.Data[frame.StackIndex - 2] = (Int32)l > (Int32)r;
106 internal sealed class GreaterThanInt64 : GreaterThanInstruction {
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;
113 frame.Data[frame.StackIndex - 2] = (Int64)l > (Int64)r;
120 internal sealed class GreaterThanByte : GreaterThanInstruction {
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;
127 frame.Data[frame.StackIndex - 2] = (Byte)l > (Byte)r;
134 internal sealed class GreaterThanUInt16 : GreaterThanInstruction {
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;
141 frame.Data[frame.StackIndex - 2] = (UInt16)l > (UInt16)r;
148 internal sealed class GreaterThanUInt32 : GreaterThanInstruction {
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;
155 frame.Data[frame.StackIndex - 2] = (UInt32)l > (UInt32)r;
162 internal sealed class GreaterThanUInt64 : GreaterThanInstruction {
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;
169 frame.Data[frame.StackIndex - 2] = (UInt64)l > (UInt64)r;
176 internal sealed class GreaterThanSingle : GreaterThanInstruction {
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;
183 frame.Data[frame.StackIndex - 2] = (Single)l > (Single)r;
190 internal sealed class GreaterThanDouble : GreaterThanInstruction {
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;
197 frame.Data[frame.StackIndex - 2] = (Double)l > (Double)r;
204 public static Instruction Create(Type type) {
205 Debug.Assert(!type.IsEnum());
206 switch (type.GetTypeCode()) {
207 case TypeCode.SByte: return _SByte ?? (_SByte = new GreaterThanSByte());
208 case TypeCode.Byte: return _Byte ?? (_Byte = new GreaterThanByte());
209 case TypeCode.Char: return _Char ?? (_Char = new GreaterThanChar());
210 case TypeCode.Int16: return _Int16 ?? (_Int16 = new GreaterThanInt16());
211 case TypeCode.Int32: return _Int32 ?? (_Int32 = new GreaterThanInt32());
212 case TypeCode.Int64: return _Int64 ?? (_Int64 = new GreaterThanInt64());
213 case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new GreaterThanUInt16());
214 case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new GreaterThanUInt32());
215 case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new GreaterThanUInt64());
216 case TypeCode.Single: return _Single ?? (_Single = new GreaterThanSingle());
217 case TypeCode.Double: return _Double ?? (_Double = new GreaterThanDouble());
220 throw Assert.Unreachable;
224 public static Instruction CreateLifted(Type type) {
225 Debug.Assert(!type.IsEnum());
226 switch (type.GetTypeCode()) {
227 case TypeCode.SByte: return _SByteLifted ?? (_SByteLifted = new GreaterThanSByte() { LiftedToNull = true });
228 case TypeCode.Byte: return _ByteLifted ?? (_ByteLifted = new GreaterThanByte() { LiftedToNull = true });
229 case TypeCode.Char: return _CharLifted ?? (_CharLifted = new GreaterThanChar() { LiftedToNull = true });
230 case TypeCode.Int16: return _Int16Lifted ?? (_Int16Lifted = new GreaterThanInt16() { LiftedToNull = true });
231 case TypeCode.Int32: return _Int32Lifted ?? (_Int32Lifted = new GreaterThanInt32() { LiftedToNull = true });
232 case TypeCode.Int64: return _Int64Lifted ?? (_Int64Lifted = new GreaterThanInt64() { LiftedToNull = true });
233 case TypeCode.UInt16: return _UInt16Lifted ?? (_UInt16Lifted = new GreaterThanUInt16() { LiftedToNull = true });
234 case TypeCode.UInt32: return _UInt32Lifted ?? (_UInt32Lifted = new GreaterThanUInt32() { LiftedToNull = true });
235 case TypeCode.UInt64: return _UInt64Lifted ?? (_UInt64Lifted = new GreaterThanUInt64() { LiftedToNull = true });
236 case TypeCode.Single: return _SingleLifted ?? (_SingleLifted = new GreaterThanSingle() { LiftedToNull = true });
237 case TypeCode.Double: return _DoubleLifted ?? (_DoubleLifted = new GreaterThanDouble() { LiftedToNull = true });
240 throw Assert.Unreachable;
244 public override string ToString() {
245 return "GreaterThan()";