2 // LessThanOrEqualInstruction.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 LessThanOrEqualInstruction : 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 LessThanOrEqualInstruction() {
45 protected override object DoNullComparison (object l, object r)
47 return LiftedToNull ? (object) null : (object) false;
50 internal sealed class LessThanOrEqualSByte : LessThanOrEqualInstruction {
51 protected override object DoCalculate (object l, object r)
53 return (SByte)l <= (SByte)r;
57 internal sealed class LessThanOrEqualInt16 : LessThanOrEqualInstruction {
58 protected override object DoCalculate (object l, object r)
60 return (Int16)l <= (Int16)r;
64 internal sealed class LessThanOrEqualChar : LessThanOrEqualInstruction {
65 protected override object DoCalculate (object l, object r)
67 return (Char)l <= (Char)r;
71 internal sealed class LessThanOrEqualInt32 : LessThanOrEqualInstruction {
72 protected override object DoCalculate (object l, object r)
74 return (Int32)l <= (Int32)r;
78 internal sealed class LessThanOrEqualInt64 : LessThanOrEqualInstruction {
79 protected override object DoCalculate (object l, object r)
81 return (Int64)l <= (Int64)r;
85 internal sealed class LessThanOrEqualByte : LessThanOrEqualInstruction {
86 protected override object DoCalculate (object l, object r)
88 return (Byte)l <= (Byte)r;
92 internal sealed class LessThanOrEqualUInt16 : LessThanOrEqualInstruction {
93 protected override object DoCalculate (object l, object r)
95 return (UInt16)l <= (UInt16)r;
99 internal sealed class LessThanOrEqualUInt32 : LessThanOrEqualInstruction {
100 protected override object DoCalculate (object l, object r)
102 return (UInt32)l <= (UInt32)r;
106 internal sealed class LessThanOrEqualUInt64 : LessThanOrEqualInstruction {
107 protected override object DoCalculate (object l, object r)
109 return (UInt64)l <= (UInt64)r;
113 internal sealed class LessThanOrEqualSingle : LessThanOrEqualInstruction {
114 protected override object DoCalculate (object l, object r)
116 return (Single)l <= (Single)r;
120 internal sealed class LessThanOrEqualDouble : LessThanOrEqualInstruction {
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 LessThanOrEqualSByte());
131 case TypeCode.Byte: return _Byte ?? (_Byte = new LessThanOrEqualByte());
132 case TypeCode.Char: return _Char ?? (_Char = new LessThanOrEqualChar());
133 case TypeCode.Int16: return _Int16 ?? (_Int16 = new LessThanOrEqualInt16());
134 case TypeCode.Int32: return _Int32 ?? (_Int32 = new LessThanOrEqualInt32());
135 case TypeCode.Int64: return _Int64 ?? (_Int64 = new LessThanOrEqualInt64());
136 case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new LessThanOrEqualUInt16());
137 case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new LessThanOrEqualUInt32());
138 case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new LessThanOrEqualUInt64());
139 case TypeCode.Single: return _Single ?? (_Single = new LessThanOrEqualSingle());
140 case TypeCode.Double: return _Double ?? (_Double = new LessThanOrEqualDouble());
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 LessThanOrEqualSByte() { LiftedToNull = true });
151 case TypeCode.Byte: return _ByteLifted ?? (_ByteLifted = new LessThanOrEqualByte() { LiftedToNull = true });
152 case TypeCode.Char: return _CharLifted ?? (_CharLifted = new LessThanOrEqualChar() { LiftedToNull = true });
153 case TypeCode.Int16: return _Int16Lifted ?? (_Int16Lifted = new LessThanOrEqualInt16() { LiftedToNull = true });
154 case TypeCode.Int32: return _Int32Lifted ?? (_Int32Lifted = new LessThanOrEqualInt32() { LiftedToNull = true });
155 case TypeCode.Int64: return _Int64Lifted ?? (_Int64Lifted = new LessThanOrEqualInt64() { LiftedToNull = true });
156 case TypeCode.UInt16: return _UInt16Lifted ?? (_UInt16Lifted = new LessThanOrEqualUInt16() { LiftedToNull = true });
157 case TypeCode.UInt32: return _UInt32Lifted ?? (_UInt32Lifted = new LessThanOrEqualUInt32() { LiftedToNull = true });
158 case TypeCode.UInt64: return _UInt64Lifted ?? (_UInt64Lifted = new LessThanOrEqualUInt64() { LiftedToNull = true });
159 case TypeCode.Single: return _SingleLifted ?? (_SingleLifted = new LessThanOrEqualSingle() { LiftedToNull = true });
160 case TypeCode.Double: return _DoubleLifted ?? (_DoubleLifted = new LessThanOrEqualDouble() { LiftedToNull = true });
163 throw Assert.Unreachable;
167 public override string ToString() {
168 return "LessThanOrEqual()";