[dlr] Handle more expressions in interpreter
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Dynamic / Interpreter / Instructions / GreaterThanInstruction.cs
index a35f16b0e349ad004c96d3bb4b0e5e84a1838c83..8849613fd51f1fbd9efc07e0b7799a418ebf4e98 100644 (file)
@@ -1,17 +1,30 @@
-/* ****************************************************************************
- *
- * Copyright (c) Microsoft Corporation. 
- *
- * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
- * copy of the license can be found in the License.html file at the root of this distribution. If 
- * you cannot locate the  Apache License, Version 2.0, please send an email to 
- * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
- * by the terms of the Apache License, Version 2.0.
- *
- * You must not remove this notice, or any other, from this software.
- *
- *
- * ***************************************************************************/
+// 
+// GreaterThanInstruction.cs:
+//
+// Authors: Marek Safar (marek.safar@gmail.com)
+//     
+// Copyright 2014 Xamarin Inc
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+//
 
 using System;
 using System.Collections.Generic;
@@ -22,8 +35,9 @@ using Microsoft.Scripting.Runtime;
 using Microsoft.Scripting.Utils;
 
 namespace Microsoft.Scripting.Interpreter {
-    internal abstract class GreaterThanInstruction : Instruction {
+    public abstract class GreaterThanInstruction : Instruction {
         private static Instruction _SByte, _Int16, _Char, _Int32, _Int64, _Byte, _UInt16, _UInt32, _UInt64, _Single, _Double;
+        private static Instruction _SByteLifted, _Int16Lifted, _CharLifted, _Int32Lifted, _Int64Lifted, _ByteLifted, _UInt16Lifted, _UInt32Lifted, _UInt64Lifted, _SingleLifted, _DoubleLifted;
 
         public override int ConsumedStack { get { return 2; } }
         public override int ProducedStack { get { return 1; } }
@@ -31,90 +45,158 @@ namespace Microsoft.Scripting.Interpreter {
         private GreaterThanInstruction() {
         }
 
+        public bool LiftedToNull { get; set; }
+
         internal sealed class GreaterThanSByte : GreaterThanInstruction {
             public override int Run(InterpretedFrame frame) {
-                SByte right = (SByte)frame.Pop();
-                frame.Push(((SByte)frame.Pop()) > right);
+                object l = frame.Data[frame.StackIndex - 2];
+                object r = frame.Data[frame.StackIndex - 1];
+                if (l == null || r == null)
+                    frame.Data[frame.StackIndex - 2] = LiftedToNull ? (object) null : (object) false;
+                else
+                    frame.Data[frame.StackIndex - 2] = (SByte)l > (SByte)r;
+
+                frame.StackIndex--;
                 return +1;
             }
         }
 
         internal sealed class GreaterThanInt16 : GreaterThanInstruction {
             public override int Run(InterpretedFrame frame) {
-                Int16 right = (Int16)frame.Pop();
-                frame.Push(((Int16)frame.Pop()) > right);
+                object l = frame.Data[frame.StackIndex - 2];
+                object r = frame.Data[frame.StackIndex - 1];
+                if (l == null || r == null)
+                    frame.Data[frame.StackIndex - 2] = LiftedToNull ? (object) null : (object) false;
+                else
+                    frame.Data[frame.StackIndex - 2] = (Int16)l > (Int16)r;
+
+                frame.StackIndex--;
                 return +1;
             }
         }
 
         internal sealed class GreaterThanChar : GreaterThanInstruction {
             public override int Run(InterpretedFrame frame) {
-                Char right = (Char)frame.Pop();
-                frame.Push(((Char)frame.Pop()) > right);
+                object l = frame.Data[frame.StackIndex - 2];
+                object r = frame.Data[frame.StackIndex - 1];
+                if (l == null || r == null)
+                    frame.Data[frame.StackIndex - 2] = LiftedToNull ? (object) null : (object) false;
+                else
+                    frame.Data[frame.StackIndex - 2] = (Char)l > (Char)r;
+
+                frame.StackIndex--;
                 return +1;
             }
         }
 
         internal sealed class GreaterThanInt32 : GreaterThanInstruction {
             public override int Run(InterpretedFrame frame) {
-                Int32 right = (Int32)frame.Pop();
-                frame.Push(((Int32)frame.Pop()) > right);
+                object l = frame.Data[frame.StackIndex - 2];
+                object r = frame.Data[frame.StackIndex - 1];
+                if (l == null || r == null)
+                    frame.Data[frame.StackIndex - 2] = LiftedToNull ? (object) null : (object) false;
+                else
+                    frame.Data[frame.StackIndex - 2] = (Int32)l > (Int32)r;
+
+                frame.StackIndex--;
                 return +1;
             }
         }
 
         internal sealed class GreaterThanInt64 : GreaterThanInstruction {
             public override int Run(InterpretedFrame frame) {
-                Int64 right = (Int64)frame.Pop();
-                frame.Push(((Int64)frame.Pop()) > right);
+                object l = frame.Data[frame.StackIndex - 2];
+                object r = frame.Data[frame.StackIndex - 1];
+                if (l == null || r == null)
+                    frame.Data[frame.StackIndex - 2] = LiftedToNull ? (object) null : (object) false;
+                else
+                    frame.Data[frame.StackIndex - 2] = (Int64)l > (Int64)r;
+
+                frame.StackIndex--;
                 return +1;
             }
         }
 
         internal sealed class GreaterThanByte : GreaterThanInstruction {
             public override int Run(InterpretedFrame frame) {
-                Byte right = (Byte)frame.Pop();
-                frame.Push(((Byte)frame.Pop()) > right);
+                object l = frame.Data[frame.StackIndex - 2];
+                object r = frame.Data[frame.StackIndex - 1];
+                if (l == null || r == null)
+                    frame.Data[frame.StackIndex - 2] = LiftedToNull ? (object) null : (object) false;
+                else
+                    frame.Data[frame.StackIndex - 2] = (Byte)l > (Byte)r;
+
+                frame.StackIndex--;
                 return +1;
             }
         }
 
         internal sealed class GreaterThanUInt16 : GreaterThanInstruction {
             public override int Run(InterpretedFrame frame) {
-                UInt16 right = (UInt16)frame.Pop();
-                frame.Push(((UInt16)frame.Pop()) > right);
+                object l = frame.Data[frame.StackIndex - 2];
+                object r = frame.Data[frame.StackIndex - 1];
+                if (l == null || r == null)
+                    frame.Data[frame.StackIndex - 2] = LiftedToNull ? (object) null : (object) false;
+                else
+                    frame.Data[frame.StackIndex - 2] = (UInt16)l > (UInt16)r;
+
+                frame.StackIndex--;
                 return +1;
             }
         }
 
         internal sealed class GreaterThanUInt32 : GreaterThanInstruction {
             public override int Run(InterpretedFrame frame) {
-                UInt32 right = (UInt32)frame.Pop();
-                frame.Push(((UInt32)frame.Pop()) > right);
+                object l = frame.Data[frame.StackIndex - 2];
+                object r = frame.Data[frame.StackIndex - 1];
+                if (l == null || r == null)
+                    frame.Data[frame.StackIndex - 2] = LiftedToNull ? (object) null : (object) false;
+                else
+                    frame.Data[frame.StackIndex - 2] = (UInt32)l > (UInt32)r;
+
+                frame.StackIndex--;
                 return +1;
             }
         }
 
         internal sealed class GreaterThanUInt64 : GreaterThanInstruction {
             public override int Run(InterpretedFrame frame) {
-                UInt64 right = (UInt64)frame.Pop();
-                frame.Push(((UInt64)frame.Pop()) > right);
+                object l = frame.Data[frame.StackIndex - 2];
+                object r = frame.Data[frame.StackIndex - 1];
+                if (l == null || r == null)
+                    frame.Data[frame.StackIndex - 2] = LiftedToNull ? (object) null : (object) false;
+                else
+                    frame.Data[frame.StackIndex - 2] = (UInt64)l > (UInt64)r;
+
+                frame.StackIndex--;
                 return +1;
             }
         }
 
         internal sealed class GreaterThanSingle : GreaterThanInstruction {
             public override int Run(InterpretedFrame frame) {
-                Single right = (Single)frame.Pop();
-                frame.Push(((Single)frame.Pop()) > right);
+                object l = frame.Data[frame.StackIndex - 2];
+                object r = frame.Data[frame.StackIndex - 1];
+                if (l == null || r == null)
+                    frame.Data[frame.StackIndex - 2] = LiftedToNull ? (object) null : (object) false;
+                else
+                    frame.Data[frame.StackIndex - 2] = (Single)l > (Single)r;
+
+                frame.StackIndex--;
                 return +1;
             }
         }
 
         internal sealed class GreaterThanDouble : GreaterThanInstruction {
             public override int Run(InterpretedFrame frame) {
-                Double right = (Double)frame.Pop();
-                frame.Push(((Double)frame.Pop()) > right);
+                object l = frame.Data[frame.StackIndex - 2];
+                object r = frame.Data[frame.StackIndex - 1];
+                if (l == null || r == null)
+                    frame.Data[frame.StackIndex - 2] = LiftedToNull ? (object) null : (object) false;
+                else
+                    frame.Data[frame.StackIndex - 2] = (Double)l > (Double)r;
+
+                frame.StackIndex--;
                 return +1;
             }
         }
@@ -139,6 +221,26 @@ namespace Microsoft.Scripting.Interpreter {
             }
         }
 
+        public static Instruction CreateLifted(Type type) {
+            Debug.Assert(!type.IsEnum());
+            switch (type.GetTypeCode()) {
+                case TypeCode.SByte: return _SByteLifted ?? (_SByteLifted = new GreaterThanSByte() { LiftedToNull = true });
+                case TypeCode.Byte: return _ByteLifted ?? (_ByteLifted = new GreaterThanByte() { LiftedToNull = true });
+                case TypeCode.Char: return _CharLifted ?? (_CharLifted = new GreaterThanChar() { LiftedToNull = true });
+                case TypeCode.Int16: return _Int16Lifted ?? (_Int16Lifted = new GreaterThanInt16() { LiftedToNull = true });
+                case TypeCode.Int32: return _Int32Lifted ?? (_Int32Lifted = new GreaterThanInt32() { LiftedToNull = true });
+                case TypeCode.Int64: return _Int64Lifted ?? (_Int64Lifted = new GreaterThanInt64() { LiftedToNull = true });
+                case TypeCode.UInt16: return _UInt16Lifted ?? (_UInt16Lifted = new GreaterThanUInt16() { LiftedToNull = true });
+                case TypeCode.UInt32: return _UInt32Lifted ?? (_UInt32Lifted = new GreaterThanUInt32() { LiftedToNull = true });
+                case TypeCode.UInt64: return _UInt64Lifted ?? (_UInt64Lifted = new GreaterThanUInt64() { LiftedToNull = true });
+                case TypeCode.Single: return _SingleLifted ?? (_SingleLifted = new GreaterThanSingle() { LiftedToNull = true });
+                case TypeCode.Double: return _DoubleLifted ?? (_DoubleLifted = new GreaterThanDouble() { LiftedToNull = true });
+
+                default:
+                    throw Assert.Unreachable;
+            }
+        }
+
         public override string ToString() {
             return "GreaterThan()";
         }