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 XorInstruction : Instruction {
36 private static Instruction _Int16, _Int32, _Int64, _UInt16, _UInt32, _UInt64, _Boolean;
37 private static Instruction _Int16Lifted, _Int32Lifted, _Int64Lifted, _UInt16Lifted, _UInt32Lifted, _UInt64Lifted, _BooleanLifted;
39 public override int ConsumedStack { get { return 2; } }
40 public override int ProducedStack { get { return 1; } }
42 private XorInstruction() {
45 internal sealed class XorInt32 : XorInstruction {
46 public override int Run(InterpretedFrame frame) {
47 var l = (Int32)frame.Data[frame.StackIndex - 2];
48 var r = (Int32)frame.Data[frame.StackIndex - 1];
49 frame.Data[frame.StackIndex - 2] = ScriptingRuntimeHelpers.Int32ToObject(l ^ r);
55 internal sealed class XorInt16 : XorInstruction {
56 public override int Run(InterpretedFrame frame) {
57 var l = (Int16)frame.Data[frame.StackIndex - 2];
58 var r = (Int16)frame.Data[frame.StackIndex - 1];
59 frame.Data[frame.StackIndex - 2] = (Int16)(l ^ r);
65 internal sealed class XorInt64 : XorInstruction {
66 public override int Run(InterpretedFrame frame) {
67 var l = (Int64)frame.Data[frame.StackIndex - 2];
68 var r = (Int64)frame.Data[frame.StackIndex - 1];
69 frame.Data[frame.StackIndex - 2] = (Int64)(l ^ r);
75 internal sealed class XorUInt16 : XorInstruction {
76 public override int Run(InterpretedFrame frame) {
77 var l = (UInt16)frame.Data[frame.StackIndex - 2];
78 var r = (UInt16)frame.Data[frame.StackIndex - 1];
79 frame.Data[frame.StackIndex - 2] = (UInt16)(l ^ r);
85 internal sealed class XorUInt32 : XorInstruction {
86 public override int Run(InterpretedFrame frame) {
87 var l = (UInt32)frame.Data[frame.StackIndex - 2];
88 var r = (UInt32)frame.Data[frame.StackIndex - 1];
89 frame.Data[frame.StackIndex - 2] = (UInt32)(l ^ r);
95 internal sealed class XorUInt64 : XorInstruction {
96 public override int Run(InterpretedFrame frame) {
97 var l = (UInt64)frame.Data[frame.StackIndex - 2];
98 var r = (UInt64)frame.Data[frame.StackIndex - 1];
99 frame.Data[frame.StackIndex - 2] = (UInt64)(l ^ r);
105 internal sealed class XorBoolean : XorInstruction {
106 public override int Run(InterpretedFrame frame) {
107 var l = (Boolean)frame.Data[frame.StackIndex - 2];
108 var r = (Boolean)frame.Data[frame.StackIndex - 1];
109 frame.Data[frame.StackIndex - 2] = (Boolean)(l ^ r);
115 internal sealed class XorInt32Lifted : XorInstruction {
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);
125 internal sealed class XorInt16Lifted : XorInstruction {
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);
135 internal sealed class XorInt64Lifted : XorInstruction {
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);
145 internal sealed class XorUInt16Lifted : XorInstruction {
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);
155 internal sealed class XorUInt32Lifted : XorInstruction {
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);
165 internal sealed class XorUInt64Lifted : XorInstruction {
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);
175 internal sealed class XorBooleanLifted : XorInstruction {
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);
185 public static Instruction Create(Type type) {
186 Debug.Assert(!type.IsEnum());
187 switch (type.GetTypeCode()) {
188 case TypeCode.Int16: return _Int16 ?? (_Int16 = new XorInt16());
189 case TypeCode.Int32: return _Int32 ?? (_Int32 = new XorInt32());
190 case TypeCode.Int64: return _Int64 ?? (_Int64 = new XorInt64());
191 case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new XorUInt16());
192 case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new XorUInt32());
193 case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new XorUInt64());
194 case TypeCode.Boolean: return _Boolean ?? (_Boolean = new XorBoolean());
197 throw Assert.Unreachable;
201 public static Instruction CreateLifted(Type type) {
202 Debug.Assert(!type.IsEnum());
203 switch (type.GetTypeCode()) {
204 case TypeCode.Int16: return _Int16Lifted ?? (_Int16Lifted = new XorInt16Lifted());
205 case TypeCode.Int32: return _Int32Lifted ?? (_Int32Lifted = new XorInt32Lifted());
206 case TypeCode.Int64: return _Int64Lifted ?? (_Int64Lifted = new XorInt64Lifted());
207 case TypeCode.UInt16: return _UInt16Lifted ?? (_UInt16Lifted = new XorUInt16Lifted());
208 case TypeCode.UInt32: return _UInt32Lifted ?? (_UInt32Lifted = new XorUInt32Lifted());
209 case TypeCode.UInt64: return _UInt64Lifted ?? (_UInt64Lifted = new XorUInt64Lifted());
210 case TypeCode.Boolean: return _BooleanLifted ?? (_BooleanLifted = new XorBooleanLifted());
213 throw Assert.Unreachable;
217 public override string ToString() {