[interpreter] Size reduction
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Dynamic / Interpreter / Instructions / AndInstruction.cs
1 // 
2 // AndInstruction.cs:
3 //
4 // Authors: Marek Safar (marek.safar@gmail.com)
5 //     
6 // Copyright 2014 Xamarin Inc
7 //
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:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 // 
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.
26 //
27 //
28
29 using System;
30 using System.Diagnostics;
31 using Microsoft.Scripting.Runtime;
32 using Microsoft.Scripting.Utils;
33
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;
38
39         private AndInstruction() {
40         }
41
42         internal sealed class AndInt32 : AndInstruction {
43             protected override object Calculate (object l, object r)
44             {
45                 return ScriptingRuntimeHelpers.Int32ToObject((Int32)l & (Int32)r);
46             }
47         }
48
49         internal sealed class AndInt16 : AndInstruction {
50             protected override object Calculate (object l, object r)
51             {
52                 return (Int16)((Int16)l & (Int16)r);
53             }
54         }
55
56         internal sealed class AndInt64 : AndInstruction {
57             protected override object Calculate (object l, object r)
58             {
59                 return (Int64)((Int64)l & (Int64)r);
60             }
61         }
62
63         internal sealed class AndUInt16 : AndInstruction {
64             protected override object Calculate (object l, object r)
65             {
66                 return (UInt16)((UInt16)l & (UInt16)r);
67             }
68         }
69
70         internal sealed class AndUInt32 : AndInstruction {
71             protected override object Calculate (object l, object r)
72             {
73                 return (UInt32)((UInt32)l & (UInt32)r);
74             }
75         }
76
77         internal sealed class AndUInt64 : AndInstruction {
78             protected override object Calculate (object l, object r)
79             {
80                 return (UInt64)((UInt64)l & (UInt64)r);
81             }
82         }
83
84         internal sealed class AndBoolean : AndInstruction {
85             protected override object Calculate (object l, object r)
86             {
87                 return (Boolean)((Boolean)l & (Boolean)r);
88             }
89         }
90
91         internal sealed class AndInt32Lifted : AndInstruction {
92             protected override object Calculate (object l, object r)
93             {
94                 return (Int32?)((Int32?)l & (Int32?)r);
95             }
96         }
97
98         internal sealed class AndInt16Lifted : AndInstruction {
99             protected override object Calculate (object l, object r)
100             {
101                 return (Int16?)((Int16?)l & (Int16?)r);
102             }
103         }
104
105         internal sealed class AndInt64Lifted : AndInstruction {
106             protected override object Calculate (object l, object r)
107             {
108                 return (Int64?)((Int64?)l & (Int64?)r);
109             }
110         }
111
112         internal sealed class AndUInt16Lifted : AndInstruction {
113             protected override object Calculate (object l, object r)
114             {
115                 return (UInt16?)((UInt16?)l & (UInt16?)r);
116             }
117         }
118
119         internal sealed class AndUInt32Lifted : AndInstruction {
120             protected override object Calculate (object l, object r)
121             {
122                 return (UInt32?)((UInt32?)l & (UInt32?)r);
123             }
124         }
125
126         internal sealed class AndUInt64Lifted : AndInstruction {
127             protected override object Calculate (object l, object r)
128             {
129                 return (UInt64?)((UInt64?)l & (UInt64?)r);
130             }
131         }
132
133         internal sealed class AndBooleanLifted : AndInstruction {
134             protected override object Calculate (object l, object r)
135             {
136                 return ((Boolean?)l & (Boolean?)r);
137             }
138         }
139
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());
150
151                 default:
152                     throw Assert.Unreachable;
153             }
154         }
155
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());
166
167                 default:
168                     throw Assert.Unreachable;
169             }
170         }
171
172         public override string ToString() {
173             return "And()";
174         }
175     }
176 }