2 // GreaterThanOrEqualInstruction.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 abstract class GreaterThanOrEqualInstruction : ComparisonInstruction {
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 private GreaterThanOrEqualInstruction() {
45 protected override object DoNullComparison (object l, object r)
47 return LiftedToNull ? (object) null : (object) false;
50 internal sealed class GreaterThanOrEqualSByte : GreaterThanOrEqualInstruction {
51 protected override object DoCalculate (object l, object r)
53 return (SByte)l >= (SByte)r;
57 internal sealed class GreaterThanOrEqualInt16 : GreaterThanOrEqualInstruction {
58 protected override object DoCalculate (object l, object r)
60 return (Int16)l >= (Int16)r;
64 internal sealed class GreaterThanOrEqualChar : GreaterThanOrEqualInstruction {
65 protected override object DoCalculate (object l, object r)
67 return (Char)l >= (Char)r;
71 internal sealed class GreaterThanOrEqualInt32 : GreaterThanOrEqualInstruction {
72 protected override object DoCalculate (object l, object r)
74 return (Int32)l >= (Int32)r;
78 internal sealed class GreaterThanOrEqualInt64 : GreaterThanOrEqualInstruction {
79 protected override object DoCalculate (object l, object r)
81 return (Int64)l >= (Int64)r;
85 internal sealed class GreaterThanOrEqualByte : GreaterThanOrEqualInstruction {
86 protected override object DoCalculate (object l, object r)
88 return (Byte)l >= (Byte)r;
92 internal sealed class GreaterThanOrEqualUInt16 : GreaterThanOrEqualInstruction {
93 protected override object DoCalculate (object l, object r)
95 return (UInt16)l >= (UInt16)r;
99 internal sealed class GreaterThanOrEqualUInt32 : GreaterThanOrEqualInstruction {
100 protected override object DoCalculate (object l, object r)
102 return (UInt32)l >= (UInt32)r;
106 internal sealed class GreaterThanOrEqualUInt64 : GreaterThanOrEqualInstruction {
107 protected override object DoCalculate (object l, object r)
109 return (UInt64)l >= (UInt64)r;
113 internal sealed class GreaterThanOrEqualSingle : GreaterThanOrEqualInstruction {
114 protected override object DoCalculate (object l, object r)
116 return (Single)l >= (Single)r;
120 internal sealed class GreaterThanOrEqualDouble : GreaterThanOrEqualInstruction {
121 protected override object DoCalculate (object l, object r)
123 return (Double)l >= (Double)r;
127 public static Instruction Create(Type type) {
128 Debug.Assert(!type.IsEnum());
129 switch (type.GetTypeCode()) {
130 case TypeCode.SByte: return _SByte ?? (_SByte = new GreaterThanOrEqualSByte());
131 case TypeCode.Byte: return _Byte ?? (_Byte = new GreaterThanOrEqualByte());
132 case TypeCode.Char: return _Char ?? (_Char = new GreaterThanOrEqualChar());
133 case TypeCode.Int16: return _Int16 ?? (_Int16 = new GreaterThanOrEqualInt16());
134 case TypeCode.Int32: return _Int32 ?? (_Int32 = new GreaterThanOrEqualInt32());
135 case TypeCode.Int64: return _Int64 ?? (_Int64 = new GreaterThanOrEqualInt64());
136 case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new GreaterThanOrEqualUInt16());
137 case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new GreaterThanOrEqualUInt32());
138 case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new GreaterThanOrEqualUInt64());
139 case TypeCode.Single: return _Single ?? (_Single = new GreaterThanOrEqualSingle());
140 case TypeCode.Double: return _Double ?? (_Double = new GreaterThanOrEqualDouble());
143 throw Assert.Unreachable;
147 public static Instruction CreateLifted(Type type) {
148 Debug.Assert(!type.IsEnum());
149 switch (type.GetTypeCode()) {
150 case TypeCode.SByte: return _SByteLifted ?? (_SByteLifted = new GreaterThanOrEqualSByte() { LiftedToNull = true });
151 case TypeCode.Byte: return _ByteLifted ?? (_ByteLifted = new GreaterThanOrEqualByte() { LiftedToNull = true });
152 case TypeCode.Char: return _CharLifted ?? (_CharLifted = new GreaterThanOrEqualChar() { LiftedToNull = true });
153 case TypeCode.Int16: return _Int16Lifted ?? (_Int16Lifted = new GreaterThanOrEqualInt16() { LiftedToNull = true });
154 case TypeCode.Int32: return _Int32Lifted ?? (_Int32Lifted = new GreaterThanOrEqualInt32() { LiftedToNull = true });
155 case TypeCode.Int64: return _Int64Lifted ?? (_Int64Lifted = new GreaterThanOrEqualInt64() { LiftedToNull = true });
156 case TypeCode.UInt16: return _UInt16Lifted ?? (_UInt16Lifted = new GreaterThanOrEqualUInt16() { LiftedToNull = true });
157 case TypeCode.UInt32: return _UInt32Lifted ?? (_UInt32Lifted = new GreaterThanOrEqualUInt32() { LiftedToNull = true });
158 case TypeCode.UInt64: return _UInt64Lifted ?? (_UInt64Lifted = new GreaterThanOrEqualUInt64() { LiftedToNull = true });
159 case TypeCode.Single: return _SingleLifted ?? (_SingleLifted = new GreaterThanOrEqualSingle() { LiftedToNull = true });
160 case TypeCode.Double: return _DoubleLifted ?? (_DoubleLifted = new GreaterThanOrEqualDouble() { LiftedToNull = true });
163 throw Assert.Unreachable;
167 public override string ToString() {
168 return "GreaterThanOrEqual()";