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 ShlInstruction : Instruction {
36 private static Instruction _Int16, _Int32, _Int64, _UInt16, _UInt32, _UInt64;
37 private static Instruction _Int16Lifted, _Int32Lifted, _Int64Lifted, _UInt16Lifted, _UInt32Lifted, _UInt64Lifted;
39 public override int ConsumedStack { get { return 2; } }
40 public override int ProducedStack { get { return 1; } }
42 private ShlInstruction() {
45 internal sealed class ShlInt32 : ShlInstruction {
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);
55 internal sealed class ShlInt16 : ShlInstruction {
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);
65 internal sealed class ShlInt64 : ShlInstruction {
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);
75 internal sealed class ShlUInt16 : ShlInstruction {
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);
85 internal sealed class ShlUInt32 : ShlInstruction {
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);
95 internal sealed class ShlUInt64 : ShlInstruction {
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);
105 internal sealed class ShlInt32Lifted : ShlInstruction {
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);
115 internal sealed class ShlInt16Lifted : ShlInstruction {
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);
125 internal sealed class ShlInt64Lifted : ShlInstruction {
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);
135 internal sealed class ShlUInt16Lifted : ShlInstruction {
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);
145 internal sealed class ShlUInt32Lifted : ShlInstruction {
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);
155 internal sealed class ShlUInt64Lifted : ShlInstruction {
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);
165 public static Instruction Create(Type type) {
166 Debug.Assert(!type.IsEnum());
167 switch (type.GetTypeCode()) {
168 case TypeCode.Int16: return _Int16 ?? (_Int16 = new ShlInt16());
169 case TypeCode.Int32: return _Int32 ?? (_Int32 = new ShlInt32());
170 case TypeCode.Int64: return _Int64 ?? (_Int64 = new ShlInt64());
171 case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new ShlUInt16());
172 case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new ShlUInt32());
173 case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new ShlUInt64());
176 throw Assert.Unreachable;
180 public static Instruction CreateLifted(Type type) {
181 Debug.Assert(!type.IsEnum());
182 switch (type.GetTypeCode()) {
183 case TypeCode.Int16: return _Int16Lifted ?? (_Int16Lifted = new ShlInt16Lifted());
184 case TypeCode.Int32: return _Int32Lifted ?? (_Int32Lifted = new ShlInt32Lifted());
185 case TypeCode.Int64: return _Int64Lifted ?? (_Int64Lifted = new ShlInt64Lifted());
186 case TypeCode.UInt16: return _UInt16Lifted ?? (_UInt16Lifted = new ShlUInt16Lifted());
187 case TypeCode.UInt32: return _UInt32Lifted ?? (_UInt32Lifted = new ShlUInt32Lifted());
188 case TypeCode.UInt64: return _UInt64Lifted ?? (_UInt64Lifted = new ShlUInt64Lifted());
191 throw Assert.Unreachable;
195 public override string ToString() {