Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Dynamic / Interpreter / Instructions / OrInstruction.cs
1 // 
2 // OrInstruction.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 OrInstruction : Instruction {
36         private static Instruction _Int16, _Int32, _Int64, _UInt16, _UInt32, _UInt64, _Boolean;
37         private static Instruction _Int16Lifted, _Int32Lifted, _Int64Lifted, _UInt16Lifted, _UInt32Lifted, _UInt64Lifted, _BooleanLifted;
38
39         public override int ConsumedStack { get { return 2; } }
40         public override int ProducedStack { get { return 1; } }
41
42         private OrInstruction() {
43         }
44
45         internal sealed class OrInt32 : OrInstruction {
46             public override int Run(InterpretedFrame frame) {
47                 var l = frame.Data[frame.StackIndex - 2];
48                 var r = frame.Data[frame.StackIndex - 1];
49                 frame.Data[frame.StackIndex - 2] = ScriptingRuntimeHelpers.Int32ToObject((Int32)l | (Int32)r);
50                 frame.StackIndex--;
51                 return 1;
52             }
53         }
54
55         internal sealed class OrInt16 : OrInstruction {
56             public override int Run(InterpretedFrame frame) {
57                 var l = frame.Data[frame.StackIndex - 2];
58                 var r = frame.Data[frame.StackIndex - 1];
59                 frame.Data[frame.StackIndex - 2] = (Int16)((Int16)l | (Int16)r);
60                 frame.StackIndex--;
61                 return 1;
62             }
63         }
64
65         internal sealed class OrInt64 : OrInstruction {
66             public override int Run(InterpretedFrame frame) {
67                 var l = frame.Data[frame.StackIndex - 2];
68                 var r = frame.Data[frame.StackIndex - 1];
69                 frame.Data[frame.StackIndex - 2] = (Int64)((Int64)l | (Int64)r);
70                 frame.StackIndex--;
71                 return 1;
72             }
73         }
74
75         internal sealed class OrUInt16 : OrInstruction {
76             public override int Run(InterpretedFrame frame) {
77                 var l = frame.Data[frame.StackIndex - 2];
78                 var r = frame.Data[frame.StackIndex - 1];
79                 frame.Data[frame.StackIndex - 2] = (UInt16)((UInt16)l | (UInt16)r);
80                 frame.StackIndex--;
81                 return 1;
82             }
83         }
84
85         internal sealed class OrUInt32 : OrInstruction {
86             public override int Run(InterpretedFrame frame) {
87                 var l = frame.Data[frame.StackIndex - 2];
88                 var r = frame.Data[frame.StackIndex - 1];
89                 frame.Data[frame.StackIndex - 2] = (UInt32)((UInt32)l | (UInt32)r);
90                 frame.StackIndex--;
91                 return 1;
92             }
93         }
94
95         internal sealed class OrUInt64 : OrInstruction {
96             public override int Run(InterpretedFrame frame) {
97                 var l = frame.Data[frame.StackIndex - 2];
98                 var r = frame.Data[frame.StackIndex - 1];
99                 frame.Data[frame.StackIndex - 2] = (UInt64)((UInt64)l | (UInt64)r);
100                 frame.StackIndex--;
101                 return 1;
102             }
103         }
104
105         internal sealed class OrBoolean : OrInstruction {
106             public override int Run(InterpretedFrame frame) {
107                 var l = frame.Data[frame.StackIndex - 2];
108                 var r = frame.Data[frame.StackIndex - 1];
109                 frame.Data[frame.StackIndex - 2] = (Boolean)((Boolean)l | (Boolean)r);
110                 frame.StackIndex--;
111                 return 1;
112             }
113         }
114
115         internal sealed class OrInt32Lifted : OrInstruction {
116             public override int Run(InterpretedFrame frame) {
117                 var l = (Int32?)frame.Data[frame.StackIndex - 2];
118                 var r = (Int32?)frame.Data[frame.StackIndex - 1];
119                 frame.Data[frame.StackIndex - 2] = (Int32?)(l | r);
120                 frame.StackIndex--;
121                 return 1;
122             }
123         }
124
125         internal sealed class OrInt16Lifted : OrInstruction {
126             public override int Run(InterpretedFrame frame) {
127                 var l = (Int16?)frame.Data[frame.StackIndex - 2];
128                 var r = (Int16?)frame.Data[frame.StackIndex - 1];
129                 frame.Data[frame.StackIndex - 2] = (Int16?)(l | r);
130                 frame.StackIndex--;
131                 return 1;
132             }
133         }
134
135         internal sealed class OrInt64Lifted : OrInstruction {
136             public override int Run(InterpretedFrame frame) {
137                 var l = (Int64?)frame.Data[frame.StackIndex - 2];
138                 var r = (Int64?)frame.Data[frame.StackIndex - 1];
139                 frame.Data[frame.StackIndex - 2] = (Int64?)(l | r);
140                 frame.StackIndex--;
141                 return 1;
142             }
143         }
144
145         internal sealed class OrUInt16Lifted : OrInstruction {
146             public override int Run(InterpretedFrame frame) {
147                 var l = (UInt16?)frame.Data[frame.StackIndex - 2];
148                 var r = (UInt16?)frame.Data[frame.StackIndex - 1];
149                 frame.Data[frame.StackIndex - 2] = (UInt16?)(l | r);
150                 frame.StackIndex--;
151                 return 1;
152             }
153         }
154
155         internal sealed class OrUInt32Lifted : OrInstruction {
156             public override int Run(InterpretedFrame frame) {
157                 var l = (UInt32?)frame.Data[frame.StackIndex - 2];
158                 var r = (UInt32?)frame.Data[frame.StackIndex - 1];
159                 frame.Data[frame.StackIndex - 2] = (UInt32?)(l | r);
160                 frame.StackIndex--;
161                 return 1;
162             }
163         }
164
165         internal sealed class OrUInt64Lifted : OrInstruction {
166             public override int Run(InterpretedFrame frame) {
167                 var l = (UInt64?)frame.Data[frame.StackIndex - 2];
168                 var r = (UInt64?)frame.Data[frame.StackIndex - 1];
169                 frame.Data[frame.StackIndex - 2] = (UInt64?)(l | r);
170                 frame.StackIndex--;
171                 return 1;
172             }
173         }
174
175         internal sealed class OrBooleanLifted : OrInstruction {
176             public override int Run(InterpretedFrame frame) {
177                 var l = (Boolean?)frame.Data[frame.StackIndex - 2];
178                 var r = (Boolean?)frame.Data[frame.StackIndex - 1];
179                 frame.Data[frame.StackIndex - 2] = (Boolean?)(l | r);
180                 frame.StackIndex--;
181                 return 1;
182             }
183         }
184
185         public static Instruction Create(Type type) {
186             Debug.Assert(!type.IsEnum());
187             switch (type.GetTypeCode()) {
188                 case TypeCode.Int16: return _Int16 ?? (_Int16 = new OrInt16());
189                 case TypeCode.Int32: return _Int32 ?? (_Int32 = new OrInt32());
190                 case TypeCode.Int64: return _Int64 ?? (_Int64 = new OrInt64());
191                 case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new OrUInt16());
192                 case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new OrUInt32());
193                 case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new OrUInt64());
194                 case TypeCode.Boolean: return _Boolean ?? (_Boolean = new OrBoolean());
195
196                 default:
197                     throw Assert.Unreachable;
198             }
199         }
200
201         public static Instruction CreateLifted(Type type) {
202             Debug.Assert(!type.IsEnum());
203             switch (type.GetTypeCode()) {
204                 case TypeCode.Int16: return _Int16Lifted ?? (_Int16Lifted = new OrInt16Lifted());
205                 case TypeCode.Int32: return _Int32Lifted ?? (_Int32Lifted = new OrInt32Lifted());
206                 case TypeCode.Int64: return _Int64Lifted ?? (_Int64Lifted = new OrInt64Lifted());
207                 case TypeCode.UInt16: return _UInt16Lifted ?? (_UInt16Lifted = new OrUInt16Lifted());
208                 case TypeCode.UInt32: return _UInt32Lifted ?? (_UInt32Lifted = new OrUInt32Lifted());
209                 case TypeCode.UInt64: return _UInt64Lifted ?? (_UInt64Lifted = new OrUInt64Lifted());
210                 case TypeCode.Boolean: return _BooleanLifted ?? (_BooleanLifted = new OrBooleanLifted());
211
212                 default:
213                     throw Assert.Unreachable;
214             }
215         }
216
217         public override string ToString() {
218             return "Or()";
219         }
220     }
221 }