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.Diagnostics;
31 using Microsoft.Scripting.Runtime;
32 using Microsoft.Scripting.Utils;
34 namespace Microsoft.Scripting.Interpreter {
35 internal abstract class ShrInstruction : ArithmeticInstruction {
36 private static Instruction _Int16, _Int32, _Int64, _UInt16, _UInt32, _UInt64;
37 private static Instruction _Int16Lifted, _Int32Lifted, _Int64Lifted, _UInt16Lifted, _UInt32Lifted, _UInt64Lifted;
39 private ShrInstruction() {
42 internal sealed class ShrInt32 : ShrInstruction {
43 protected override object Calculate (object l, object r)
45 return ScriptingRuntimeHelpers.Int32ToObject((Int32)l >> (Int32)r);
49 internal sealed class ShrInt16 : ShrInstruction {
50 protected override object Calculate (object l, object r)
52 return (Int32)((Int16)l >> (Int32)r);
56 internal sealed class ShrInt64 : ShrInstruction {
57 protected override object Calculate (object l, object r)
59 return (Int64)((Int64)l >> (Int32)r);
63 internal sealed class ShrUInt16 : ShrInstruction {
64 protected override object Calculate (object l, object r)
66 return (Int32)((UInt16)l >> (Int32)r);
70 internal sealed class ShrUInt32 : ShrInstruction {
71 protected override object Calculate (object l, object r)
73 return (UInt32)((UInt32)l >> (Int32)r);
77 internal sealed class ShrUInt64 : ShrInstruction {
78 protected override object Calculate (object l, object r)
80 return (UInt64)((UInt64)l >> (Int32)r);
84 internal sealed class ShrInt32Lifted : ShrInstruction {
85 protected override object Calculate (object l, object r)
87 return (Int32?)((Int32?)l >> (Int32?)r);
91 internal sealed class ShrInt16Lifted : ShrInstruction {
92 protected override object Calculate (object l, object r)
94 return (Int32)((Int16?)l >> (Int32?)r);
98 internal sealed class ShrInt64Lifted : ShrInstruction {
99 protected override object Calculate (object l, object r)
101 return (Int64?)((Int64?)l >> (Int32?)r);
105 internal sealed class ShrUInt16Lifted : ShrInstruction {
106 protected override object Calculate (object l, object r)
108 return (Int32?)((UInt16?)l >> (Int32?)r);
112 internal sealed class ShrUInt32Lifted : ShrInstruction {
113 protected override object Calculate (object l, object r)
115 return (UInt32?)((UInt32?)l >> (Int32?)r);
119 internal sealed class ShrUInt64Lifted : ShrInstruction {
120 protected override object Calculate (object l, object r)
122 return (UInt64?)((UInt64?)l >> (Int32?)r);
126 public static Instruction Create(Type type) {
127 Debug.Assert(!type.IsEnum());
128 switch (type.GetTypeCode()) {
129 case TypeCode.Int16: return _Int16 ?? (_Int16 = new ShrInt16());
130 case TypeCode.Int32: return _Int32 ?? (_Int32 = new ShrInt32());
131 case TypeCode.Int64: return _Int64 ?? (_Int64 = new ShrInt64());
132 case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new ShrUInt16());
133 case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new ShrUInt32());
134 case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new ShrUInt64());
137 throw Assert.Unreachable;
141 public static Instruction CreateLifted(Type type) {
142 Debug.Assert(!type.IsEnum());
143 switch (type.GetTypeCode()) {
144 case TypeCode.Int16: return _Int16Lifted ?? (_Int16Lifted = new ShrInt16Lifted());
145 case TypeCode.Int32: return _Int32Lifted ?? (_Int32Lifted = new ShrInt32Lifted());
146 case TypeCode.Int64: return _Int64Lifted ?? (_Int64Lifted = new ShrInt64Lifted());
147 case TypeCode.UInt16: return _UInt16Lifted ?? (_UInt16Lifted = new ShrUInt16Lifted());
148 case TypeCode.UInt32: return _UInt32Lifted ?? (_UInt32Lifted = new ShrUInt32Lifted());
149 case TypeCode.UInt64: return _UInt64Lifted ?? (_UInt64Lifted = new ShrUInt64Lifted());
152 throw Assert.Unreachable;
156 public override string ToString() {