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 OrInstruction
: ArithmeticInstruction
{
36 private static Instruction _Int16
, _Int32
, _Int64
, _UInt16
, _UInt32
, _UInt64
, _Boolean
;
37 private static Instruction _Int16Lifted
, _Int32Lifted
, _Int64Lifted
, _UInt16Lifted
, _UInt32Lifted
, _UInt64Lifted
, _BooleanLifted
;
39 private OrInstruction() {
42 internal sealed class OrInt32
: OrInstruction
{
43 protected override object Calculate (object l
, object r
)
45 return ScriptingRuntimeHelpers
.Int32ToObject((Int32
)l
| (Int32
)r
);
49 internal sealed class OrInt16
: OrInstruction
{
50 protected override object Calculate (object l
, object r
)
52 return (Int16
)((Int16
)l
| (Int16
)r
);
56 internal sealed class OrInt64
: OrInstruction
{
57 protected override object Calculate (object l
, object r
)
59 return (Int64
)((Int64
)l
| (Int64
)r
);
63 internal sealed class OrUInt16
: OrInstruction
{
64 protected override object Calculate (object l
, object r
)
66 return (UInt16
)((UInt16
)l
| (UInt16
)r
);
70 internal sealed class OrUInt32
: OrInstruction
{
71 protected override object Calculate (object l
, object r
)
73 return (UInt32
)((UInt32
)l
| (UInt32
)r
);
77 internal sealed class OrUInt64
: OrInstruction
{
78 protected override object Calculate (object l
, object r
)
80 return (UInt64
)((UInt64
)l
| (UInt64
)r
);
84 internal sealed class OrBoolean
: OrInstruction
{
85 protected override object Calculate (object l
, object r
)
87 return (Boolean
)((Boolean
)l
| (Boolean
)r
);
91 internal sealed class OrInt32Lifted
: OrInstruction
{
92 protected override object Calculate (object l
, object r
)
94 return (Int32
?)((Int32
?)l
| (Int32
?)r
);
98 internal sealed class OrInt16Lifted
: OrInstruction
{
99 protected override object Calculate (object l
, object r
)
101 return (Int16
?)((Int16
?)l
| (Int16
?)r
);
105 internal sealed class OrInt64Lifted
: OrInstruction
{
106 protected override object Calculate (object l
, object r
)
108 return (Int64
?)((Int64
?)l
| (Int64
?)r
);
112 internal sealed class OrUInt16Lifted
: OrInstruction
{
113 protected override object Calculate (object l
, object r
)
115 return (UInt16
?)((Int16
?)l
| (Int16
?)r
);
119 internal sealed class OrUInt32Lifted
: OrInstruction
{
120 protected override object Calculate (object l
, object r
)
122 return (UInt32
?)((UInt32
?)l
| (UInt32
?)r
);
126 internal sealed class OrUInt64Lifted
: OrInstruction
{
127 protected override object Calculate (object l
, object r
)
129 return (UInt64
?)((UInt64
?)l
| (UInt64
?)r
);
133 internal sealed class OrBooleanLifted
: OrInstruction
{
134 protected override object Calculate (object l
, object r
)
136 return (Boolean
?)((Boolean
?)l
| (Boolean
?)r
);
140 public static Instruction
Create(Type type
) {
141 Debug
.Assert(!type
.IsEnum());
142 switch (type
.GetTypeCode()) {
143 case TypeCode
.Int16
: return _Int16
?? (_Int16
= new OrInt16());
144 case TypeCode
.Int32
: return _Int32
?? (_Int32
= new OrInt32());
145 case TypeCode
.Int64
: return _Int64
?? (_Int64
= new OrInt64());
146 case TypeCode
.UInt16
: return _UInt16
?? (_UInt16
= new OrUInt16());
147 case TypeCode
.UInt32
: return _UInt32
?? (_UInt32
= new OrUInt32());
148 case TypeCode
.UInt64
: return _UInt64
?? (_UInt64
= new OrUInt64());
149 case TypeCode
.Boolean
: return _Boolean
?? (_Boolean
= new OrBoolean());
152 throw Assert
.Unreachable
;
156 public static Instruction
CreateLifted(Type type
) {
157 Debug
.Assert(!type
.IsEnum());
158 switch (type
.GetTypeCode()) {
159 case TypeCode
.Int16
: return _Int16Lifted
?? (_Int16Lifted
= new OrInt16Lifted());
160 case TypeCode
.Int32
: return _Int32Lifted
?? (_Int32Lifted
= new OrInt32Lifted());
161 case TypeCode
.Int64
: return _Int64Lifted
?? (_Int64Lifted
= new OrInt64Lifted());
162 case TypeCode
.UInt16
: return _UInt16Lifted
?? (_UInt16Lifted
= new OrUInt16Lifted());
163 case TypeCode
.UInt32
: return _UInt32Lifted
?? (_UInt32Lifted
= new OrUInt32Lifted());
164 case TypeCode
.UInt64
: return _UInt64Lifted
?? (_UInt64Lifted
= new OrUInt64Lifted());
165 case TypeCode
.Boolean
: return _BooleanLifted
?? (_BooleanLifted
= new OrBooleanLifted());
168 throw Assert
.Unreachable
;
172 public override string ToString() {