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 AndInstruction : AritmeticInstruction {
36 private static Instruction _Int16, _Int32, _Int64, _UInt16, _UInt32, _UInt64, _Boolean;
37 private static Instruction _Int16Lifted, _Int32Lifted, _Int64Lifted, _UInt16Lifted, _UInt32Lifted, _UInt64Lifted, _BooleanLifted;
39 private AndInstruction() {
42 internal sealed class AndInt32 : AndInstruction {
43 protected override object Calculate (object l, object r)
45 return ScriptingRuntimeHelpers.Int32ToObject((Int32)l & (Int32)r);
49 internal sealed class AndInt16 : AndInstruction {
50 protected override object Calculate (object l, object r)
52 return (Int16)((Int16)l & (Int16)r);
56 internal sealed class AndInt64 : AndInstruction {
57 protected override object Calculate (object l, object r)
59 return (Int64)((Int64)l & (Int64)r);
63 internal sealed class AndUInt16 : AndInstruction {
64 protected override object Calculate (object l, object r)
66 return (UInt16)((UInt16)l & (UInt16)r);
70 internal sealed class AndUInt32 : AndInstruction {
71 protected override object Calculate (object l, object r)
73 return (UInt32)((UInt32)l & (UInt32)r);
77 internal sealed class AndUInt64 : AndInstruction {
78 protected override object Calculate (object l, object r)
80 return (UInt64)((UInt64)l & (UInt64)r);
84 internal sealed class AndBoolean : AndInstruction {
85 protected override object Calculate (object l, object r)
87 return (Boolean)((Boolean)l & (Boolean)r);
91 internal sealed class AndInt32Lifted : AndInstruction {
92 protected override object Calculate (object l, object r)
94 return (Int32?)((Int32?)l & (Int32?)r);
98 internal sealed class AndInt16Lifted : AndInstruction {
99 protected override object Calculate (object l, object r)
101 return (Int16?)((Int16?)l & (Int16?)r);
105 internal sealed class AndInt64Lifted : AndInstruction {
106 protected override object Calculate (object l, object r)
108 return (Int64?)((Int64?)l & (Int64?)r);
112 internal sealed class AndUInt16Lifted : AndInstruction {
113 protected override object Calculate (object l, object r)
115 return (UInt16?)((UInt16?)l & (UInt16?)r);
119 internal sealed class AndUInt32Lifted : AndInstruction {
120 protected override object Calculate (object l, object r)
122 return (UInt32?)((UInt32?)l & (UInt32?)r);
126 internal sealed class AndUInt64Lifted : AndInstruction {
127 protected override object Calculate (object l, object r)
129 return (UInt64?)((UInt64?)l & (UInt64?)r);
133 internal sealed class AndBooleanLifted : AndInstruction {
134 protected override object Calculate (object l, object r)
136 return ((Boolean?)l & (Boolean?)r);
140 public static Instruction Create(Type type) {
141 Debug.Assert(!type.IsEnum());
142 switch (type.GetTypeCode()) {
143 case TypeCode.Int16: return _Int16 ?? (_Int16 = new AndInt16());
144 case TypeCode.Int32: return _Int32 ?? (_Int32 = new AndInt32());
145 case TypeCode.Int64: return _Int64 ?? (_Int64 = new AndInt64());
146 case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new AndUInt16());
147 case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new AndUInt32());
148 case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new AndUInt64());
149 case TypeCode.Boolean: return _Boolean ?? (_Boolean = new AndBoolean());
152 throw Assert.Unreachable;
156 public static Instruction CreateLifted(Type type) {
157 Debug.Assert(!type.IsEnum());
158 switch (type.GetTypeCode()) {
159 case TypeCode.Int16: return _Int16Lifted ?? (_Int16Lifted = new AndInt16Lifted());
160 case TypeCode.Int32: return _Int32Lifted ?? (_Int32Lifted = new AndInt32Lifted());
161 case TypeCode.Int64: return _Int64Lifted ?? (_Int64Lifted = new AndInt64Lifted());
162 case TypeCode.UInt16: return _UInt16Lifted ?? (_UInt16Lifted = new AndUInt16Lifted());
163 case TypeCode.UInt32: return _UInt32Lifted ?? (_UInt32Lifted = new AndUInt32Lifted());
164 case TypeCode.UInt64: return _UInt64Lifted ?? (_UInt64Lifted = new AndUInt64Lifted());
165 case TypeCode.Boolean: return _BooleanLifted ?? (_BooleanLifted = new AndBooleanLifted());
168 throw Assert.Unreachable;
172 public override string ToString() {