Merge pull request #819 from brendanzagaeski/patch-1
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Dynamic / Interpreter / Instructions / ShrInstruction.cs
1 // 
2 // ShrInstruction.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 ShrInstruction : Instruction {
36         private static Instruction _Int16, _Int32, _Int64, _UInt16, _UInt32, _UInt64;
37         private static Instruction _Int16Lifted, _Int32Lifted, _Int64Lifted, _UInt16Lifted, _UInt32Lifted, _UInt64Lifted;
38
39         public override int ConsumedStack { get { return 2; } }
40         public override int ProducedStack { get { return 1; } }
41
42         private ShrInstruction() {
43         }
44
45         internal sealed class ShrInt32 : ShrInstruction {
46             public override int Run(InterpretedFrame frame) {
47                 object l = frame.Data[frame.StackIndex - 2];
48                 object 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 ShrInt16 : ShrInstruction {
56             public override int Run(InterpretedFrame frame) {
57                 object l = frame.Data[frame.StackIndex - 2];
58                 object r = frame.Data[frame.StackIndex - 1];
59                 frame.Data[frame.StackIndex - 2] = (Int32)((Int16)l >> (Int32)r);
60                 frame.StackIndex--;
61                 return 1;
62             }
63         }
64
65         internal sealed class ShrInt64 : ShrInstruction {
66             public override int Run(InterpretedFrame frame) {
67                 object l = frame.Data[frame.StackIndex - 2];
68                 object r = frame.Data[frame.StackIndex - 1];
69                 frame.Data[frame.StackIndex - 2] = (Int64)((Int64)l >> (Int32)r);
70                 frame.StackIndex--;
71                 return 1;
72             }
73         }
74
75         internal sealed class ShrUInt16 : ShrInstruction {
76             public override int Run(InterpretedFrame frame) {
77                 object l = frame.Data[frame.StackIndex - 2];
78                 object r = frame.Data[frame.StackIndex - 1];
79                 frame.Data[frame.StackIndex - 2] = (Int32)((UInt16)l >> (Int32)r);
80                 frame.StackIndex--;
81                 return 1;
82             }
83         }
84
85         internal sealed class ShrUInt32 : ShrInstruction {
86             public override int Run(InterpretedFrame frame) {
87                 object l = frame.Data[frame.StackIndex - 2];
88                 object r = frame.Data[frame.StackIndex - 1];
89                 frame.Data[frame.StackIndex - 2] = (UInt32)((UInt32)l >> (Int32)r);
90                 frame.StackIndex--;
91                 return 1;
92             }
93         }
94
95         internal sealed class ShrUInt64 : ShrInstruction {
96             public override int Run(InterpretedFrame frame) {
97                 object l = frame.Data[frame.StackIndex - 2];
98                 object r = frame.Data[frame.StackIndex - 1];
99                 frame.Data[frame.StackIndex - 2] = (UInt64)((UInt64)l >> (Int32)r);
100                 frame.StackIndex--;
101                 return 1;
102             }
103         }
104
105         internal sealed class ShrInt32Lifted : ShrInstruction {
106             public override int Run(InterpretedFrame frame) {
107                 var l = (Int32?)frame.Data[frame.StackIndex - 2];
108                 var r = (Int32?)frame.Data[frame.StackIndex - 1];
109                 frame.Data[frame.StackIndex - 2] = (Int32?)(l >> r);
110                 frame.StackIndex--;
111                 return 1;
112             }
113         }
114
115         internal sealed class ShrInt16Lifted : ShrInstruction {
116             public override int Run(InterpretedFrame frame) {
117                 var l = (Int16?)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 ShrInt64Lifted : ShrInstruction {
126             public override int Run(InterpretedFrame frame) {
127                 var l = (Int64?)frame.Data[frame.StackIndex - 2];
128                 var r = (Int32?)frame.Data[frame.StackIndex - 1];
129                 frame.Data[frame.StackIndex - 2] = (Int64?)(l >> r);
130                 frame.StackIndex--;
131                 return 1;
132             }
133         }
134
135         internal sealed class ShrUInt16Lifted : ShrInstruction {
136             public override int Run(InterpretedFrame frame) {
137                 var l = (UInt16?)frame.Data[frame.StackIndex - 2];
138                 var r = (Int32?)frame.Data[frame.StackIndex - 1];
139                 frame.Data[frame.StackIndex - 2] = (Int32?)(l >> r);
140                 frame.StackIndex--;
141                 return 1;
142             }
143         }
144
145         internal sealed class ShrUInt32Lifted : ShrInstruction {
146             public override int Run(InterpretedFrame frame) {
147                 var l = (UInt32?)frame.Data[frame.StackIndex - 2];
148                 var r = (Int32?)frame.Data[frame.StackIndex - 1];
149                 frame.Data[frame.StackIndex - 2] = (UInt32?)(l >> r);
150                 frame.StackIndex--;
151                 return 1;
152             }
153         }
154
155         internal sealed class ShrUInt64Lifted : ShrInstruction {
156             public override int Run(InterpretedFrame frame) {
157                 var l = (UInt64?)frame.Data[frame.StackIndex - 2];
158                 var r = (Int32?)frame.Data[frame.StackIndex - 1];
159                 frame.Data[frame.StackIndex - 2] = (UInt64?)(l >> r);
160                 frame.StackIndex--;
161                 return 1;
162             }
163         }
164
165         public static Instruction Create(Type type) {
166             Debug.Assert(!type.IsEnum());
167             switch (type.GetTypeCode()) {
168                 case TypeCode.Int16: return _Int16 ?? (_Int16 = new ShrInt16());
169                 case TypeCode.Int32: return _Int32 ?? (_Int32 = new ShrInt32());
170                 case TypeCode.Int64: return _Int64 ?? (_Int64 = new ShrInt64());
171                 case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new ShrUInt16());
172                 case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new ShrUInt32());
173                 case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new ShrUInt64());
174
175                 default:
176                     throw Assert.Unreachable;
177             }
178         }
179
180         public static Instruction CreateLifted(Type type) {
181             Debug.Assert(!type.IsEnum());
182             switch (type.GetTypeCode()) {
183                 case TypeCode.Int16: return _Int16Lifted ?? (_Int16Lifted = new ShrInt16Lifted());
184                 case TypeCode.Int32: return _Int32Lifted ?? (_Int32Lifted = new ShrInt32Lifted());
185                 case TypeCode.Int64: return _Int64Lifted ?? (_Int64Lifted = new ShrInt64Lifted());
186                 case TypeCode.UInt16: return _UInt16Lifted ?? (_UInt16Lifted = new ShrUInt16Lifted());
187                 case TypeCode.UInt32: return _UInt32Lifted ?? (_UInt32Lifted = new ShrUInt32Lifted());
188                 case TypeCode.UInt64: return _UInt64Lifted ?? (_UInt64Lifted = new ShrUInt64Lifted());
189
190                 default:
191                     throw Assert.Unreachable;
192             }
193         }
194
195         public override string ToString() {
196             return "Shr()";
197         }
198     }
199 }