Merge branch 'msbuilddll2'
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Dynamic / Interpreter / Instructions / AndInstruction.cs
1 // 
2 // AndbInstruction.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 : Instruction {
36         private static Instruction _Int16, _Int32, _Int64, _UInt16, _UInt32, _UInt64, _Boolean;
37
38         public override int ConsumedStack { get { return 2; } }
39         public override int ProducedStack { get { return 1; } }
40
41         private AndInstruction() {
42         }
43
44         internal sealed class AndInt32 : AndInstruction {
45             public override int Run(InterpretedFrame frame) {
46                 object l = frame.Data[frame.StackIndex - 2];
47                 object r = frame.Data[frame.StackIndex - 1];
48                 frame.Data[frame.StackIndex - 2] = ScriptingRuntimeHelpers.Int32ToObject((Int32)l & (Int32)r);
49                 frame.StackIndex--;
50                 return 1;
51             }
52         }
53
54         internal sealed class AndInt16 : AndInstruction {
55             public override int Run(InterpretedFrame frame) {
56                 object l = frame.Data[frame.StackIndex - 2];
57                 object r = frame.Data[frame.StackIndex - 1];
58                 frame.Data[frame.StackIndex - 2] = (Int16)((Int16)l & (Int16)r);
59                 frame.StackIndex--;
60                 return 1;
61             }
62         }
63
64         internal sealed class AndInt64 : AndInstruction {
65             public override int Run(InterpretedFrame frame) {
66                 object l = frame.Data[frame.StackIndex - 2];
67                 object r = frame.Data[frame.StackIndex - 1];
68                 frame.Data[frame.StackIndex - 2] = (Int64)((Int64)l & (Int64)r);
69                 frame.StackIndex--;
70                 return 1;
71             }
72         }
73
74         internal sealed class AndUInt16 : AndInstruction {
75             public override int Run(InterpretedFrame frame) {
76                 object l = frame.Data[frame.StackIndex - 2];
77                 object r = frame.Data[frame.StackIndex - 1];
78                 frame.Data[frame.StackIndex - 2] = (UInt16)((UInt16)l & (UInt16)r);
79                 frame.StackIndex--;
80                 return 1;
81             }
82         }
83
84         internal sealed class AndUInt32 : AndInstruction {
85             public override int Run(InterpretedFrame frame) {
86                 object l = frame.Data[frame.StackIndex - 2];
87                 object r = frame.Data[frame.StackIndex - 1];
88                 frame.Data[frame.StackIndex - 2] = (UInt32)((UInt32)l & (UInt32)r);
89                 frame.StackIndex--;
90                 return 1;
91             }
92         }
93
94         internal sealed class AndUInt64 : AndInstruction {
95             public override int Run(InterpretedFrame frame) {
96                 object l = frame.Data[frame.StackIndex - 2];
97                 object r = frame.Data[frame.StackIndex - 1];
98                 frame.Data[frame.StackIndex - 2] = (UInt64)((UInt64)l & (UInt64)r);
99                 frame.StackIndex--;
100                 return 1;
101             }
102         }
103
104         internal sealed class AndBoolean : AndInstruction {
105             public override int Run(InterpretedFrame frame) {
106                 object l = frame.Data[frame.StackIndex - 2];
107                 object r = frame.Data[frame.StackIndex - 1];
108                 frame.Data[frame.StackIndex - 2] = (Boolean)((Boolean)l & (Boolean)r);
109                 frame.StackIndex--;
110                 return 1;
111             }
112         }
113
114         public static Instruction Create(Type type) {
115             Debug.Assert(!type.IsEnum());
116             switch (type.GetTypeCode()) {
117                 case TypeCode.Int16: return _Int16 ?? (_Int16 = new AndInt16());
118                 case TypeCode.Int32: return _Int32 ?? (_Int32 = new AndInt32());
119                 case TypeCode.Int64: return _Int64 ?? (_Int64 = new AndInt64());
120                 case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new AndUInt16());
121                 case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new AndUInt32());
122                 case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new AndUInt64());
123                 case TypeCode.Boolean: return _Boolean ?? (_Boolean = new AndBoolean());
124
125                 default:
126                     throw Assert.Unreachable;
127             }
128         }
129
130         public override string ToString() {
131             return "And()";
132         }
133     }
134 }