public static Instruction Create(Type type) { // Boxed enums can be unboxed as their underlying types: switch ((type.IsEnum() ? Enum.GetUnderlyingType(type) : type).GetTypeCode()) { case TypeCode.Boolean: return _Boolean ?? (_Boolean = new EqualBoolean()); case TypeCode.SByte: return _SByte ?? (_SByte = new EqualSByte()); case TypeCode.Byte: return _Byte ?? (_Byte = new EqualByte()); case TypeCode.Char: return _Char ?? (_Char = new EqualChar()); case TypeCode.Int16: return _Int16 ?? (_Int16 = new EqualInt16()); case TypeCode.Int32: return _Int32 ?? (_Int32 = new EqualInt32()); case TypeCode.Int64: return _Int64 ?? (_Int64 = new EqualInt64()); case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new EqualInt16()); case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new EqualInt32()); case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new EqualInt64()); case TypeCode.Single: return _Single ?? (_Single = new EqualSingle()); case TypeCode.Double: return _Double ?? (_Double = new EqualDouble()); case TypeCode.Object: if (!type.IsValueType()) { return _Reference ?? (_Reference = new EqualReference()); } // TODO: Nullable<T> throw new NotImplementedException(); default: throw new NotImplementedException(); } }

public static Instruction CreateLifted(Type type) { // Boxed enums can be unboxed as their underlying types: switch ((type.IsEnum() ? Enum.GetUnderlyingType(type) : type).GetTypeCode()) { case TypeCode.Boolean: return _BooleanLifted ?? (_BooleanLifted = new EqualBoolean() { LiftedToNull = true }); case TypeCode.SByte: return _SByteLifted ?? (_SByteLifted = new EqualSByte() { LiftedToNull = true }); case TypeCode.Byte: return _ByteLifted ?? (_ByteLifted = new EqualByte() { LiftedToNull = true }); case TypeCode.Char: return _CharLifted ?? (_CharLifted = new EqualChar() { LiftedToNull = true }); case TypeCode.Int16: return _Int16Lifted ?? (_Int16Lifted = new EqualInt16() { LiftedToNull = true }); case TypeCode.Int32: return _Int32Lifted ?? (_Int32Lifted = new EqualInt32() { LiftedToNull = true }); case TypeCode.Int64: return _Int64Lifted ?? (_Int64Lifted = new EqualInt64() { LiftedToNull = true }); case TypeCode.UInt16: return _UInt16Lifted ?? (_UInt16Lifted = new EqualInt16() { LiftedToNull = true }); case TypeCode.UInt32: return _UInt32Lifted ?? (_UInt32Lifted = new EqualInt32() { LiftedToNull = true }); case TypeCode.UInt64: return _UInt64Lifted ?? (_UInt64Lifted = new EqualInt64() { LiftedToNull = true }); case TypeCode.Single: return _SingleLifted ?? (_SingleLifted = new EqualSingle() { LiftedToNull = true }); case TypeCode.Double: return _DoubleLifted ?? (_DoubleLifted = new EqualDouble() { LiftedToNull = true }); default: throw Assert.Unreachable; } }

public static Instruction Create(Type type, bool liftedToNull) { // Boxed enums can be unboxed as their underlying types: if (liftedToNull) { switch (TypeHelper.GetTypeCode(type.IsEnum ? Enum.GetUnderlyingType(type) : TypeHelper.GetNonNullableType(type))) { case TypeCode.Boolean: return s_booleanLiftedToNull ?? (s_booleanLiftedToNull = new EqualBooleanLiftedToNull()); case TypeCode.SByte: return s_SByteLiftedToNull ?? (s_SByteLiftedToNull = new EqualSByteLiftedToNull()); case TypeCode.Byte: return s_byteLiftedToNull ?? (s_byteLiftedToNull = new EqualByteLiftedToNull()); case TypeCode.Char: return s_charLiftedToNull ?? (s_charLiftedToNull = new EqualCharLiftedToNull()); case TypeCode.Int16: return s_int16LiftedToNull ?? (s_int16LiftedToNull = new EqualInt16LiftedToNull()); case TypeCode.Int32: return s_int32LiftedToNull ?? (s_int32LiftedToNull = new EqualInt32LiftedToNull()); case TypeCode.Int64: return s_int64LiftedToNull ?? (s_int64LiftedToNull = new EqualInt64LiftedToNull()); case TypeCode.UInt16: return s_UInt16LiftedToNull ?? (s_UInt16LiftedToNull = new EqualUInt16LiftedToNull()); case TypeCode.UInt32: return s_UInt32LiftedToNull ?? (s_UInt32LiftedToNull = new EqualUInt32LiftedToNull()); case TypeCode.UInt64: return s_UInt64LiftedToNull ?? (s_UInt64LiftedToNull = new EqualUInt64LiftedToNull()); case TypeCode.Single: return s_singleLiftedToNull ?? (s_singleLiftedToNull = new EqualSingleLiftedToNull()); case TypeCode.Double: return s_doubleLiftedToNull ?? (s_doubleLiftedToNull = new EqualDoubleLiftedToNull()); case TypeCode.String: case TypeCode.Object: if (!type.IsValueType) { return s_referenceLiftedToNull ?? (s_referenceLiftedToNull = new EqualReferenceLiftedToNull()); } // TODO: Nullable<T> throw Error.ExpressionNotSupportedForNullableType("Equal", type); default: throw Error.ExpressionNotSupportedForType("Equal", type); } } else { switch (TypeHelper.GetTypeCode(type.IsEnum ? Enum.GetUnderlyingType(type) : TypeHelper.GetNonNullableType(type))) { case TypeCode.Boolean: return s_boolean ?? (s_boolean = new EqualBoolean()); case TypeCode.SByte: return s_SByte ?? (s_SByte = new EqualSByte()); case TypeCode.Byte: return s_byte ?? (s_byte = new EqualByte()); case TypeCode.Char: return s_char ?? (s_char = new EqualChar()); case TypeCode.Int16: return s_int16 ?? (s_int16 = new EqualInt16()); case TypeCode.Int32: return s_int32 ?? (s_int32 = new EqualInt32()); case TypeCode.Int64: return s_int64 ?? (s_int64 = new EqualInt64()); case TypeCode.UInt16: return s_UInt16 ?? (s_UInt16 = new EqualUInt16()); case TypeCode.UInt32: return s_UInt32 ?? (s_UInt32 = new EqualUInt32()); case TypeCode.UInt64: return s_UInt64 ?? (s_UInt64 = new EqualUInt64()); case TypeCode.Single: return s_single ?? (s_single = new EqualSingle()); case TypeCode.Double: return s_double ?? (s_double = new EqualDouble()); case TypeCode.String: case TypeCode.Object: if (!type.IsValueType) { return s_reference ?? (s_reference = new EqualReference()); } // TODO: Nullable<T> throw Error.ExpressionNotSupportedForNullableType("Equal", type); default: throw Error.ExpressionNotSupportedForType("Equal", type); } } }

public static Instruction Create(Type type) { var typeInfo = type.GetTypeInfo(); // Boxed enums can be unboxed as their underlying types: var typeToUse = typeInfo.IsEnum ? Enum.GetUnderlyingType(type) : type; switch (typeToUse.GetTypeCode()) { case TypeCode.Boolean: return s_boolean ?? (s_boolean = new EqualBoolean()); case TypeCode.SByte: return s_SByte ?? (s_SByte = new EqualSByte()); case TypeCode.Byte: return s_byte ?? (s_byte = new EqualByte()); case TypeCode.Char: return s_char ?? (s_char = new EqualChar()); case TypeCode.Int16: return s_int16 ?? (s_int16 = new EqualInt16()); case TypeCode.Int32: return s_int32 ?? (s_int32 = new EqualInt32()); case TypeCode.Int64: return s_int64 ?? (s_int64 = new EqualInt64()); case TypeCode.UInt16: return s_UInt16 ?? (s_UInt16 = new EqualInt16()); case TypeCode.UInt32: return s_UInt32 ?? (s_UInt32 = new EqualInt32()); case TypeCode.UInt64: return s_UInt64 ?? (s_UInt64 = new EqualInt64()); case TypeCode.Single: return s_single ?? (s_single = new EqualSingle()); case TypeCode.Double: return s_double ?? (s_double = new EqualDouble()); case TypeCode.Object: if (!typeInfo.IsValueType) { return s_reference ?? (s_reference = new EqualReference()); } // TODO: Nullable<T> throw new NotImplementedException(); default: throw new NotImplementedException(); } }

public static Instruction Create(Type type, bool liftedToNull) { // Boxed enums can be unboxed as their underlying types: Type underlyingType = type.GetTypeInfo().IsEnum ? Enum.GetUnderlyingType(type) : type.GetNonNullableType(); if (liftedToNull) { switch (underlyingType.GetTypeCode()) { case TypeCode.Boolean: return s_booleanLiftedToNull ?? (s_booleanLiftedToNull = new EqualBooleanLiftedToNull()); case TypeCode.SByte: return s_SByteLiftedToNull ?? (s_SByteLiftedToNull = new EqualSByteLiftedToNull()); case TypeCode.Byte: return s_byteLiftedToNull ?? (s_byteLiftedToNull = new EqualByteLiftedToNull()); case TypeCode.Char: return s_charLiftedToNull ?? (s_charLiftedToNull = new EqualCharLiftedToNull()); case TypeCode.Int16: return s_int16LiftedToNull ?? (s_int16LiftedToNull = new EqualInt16LiftedToNull()); case TypeCode.Int32: return s_int32LiftedToNull ?? (s_int32LiftedToNull = new EqualInt32LiftedToNull()); case TypeCode.Int64: return s_int64LiftedToNull ?? (s_int64LiftedToNull = new EqualInt64LiftedToNull()); case TypeCode.UInt16: return s_UInt16LiftedToNull ?? (s_UInt16LiftedToNull = new EqualUInt16LiftedToNull()); case TypeCode.UInt32: return s_UInt32LiftedToNull ?? (s_UInt32LiftedToNull = new EqualUInt32LiftedToNull()); case TypeCode.UInt64: return s_UInt64LiftedToNull ?? (s_UInt64LiftedToNull = new EqualUInt64LiftedToNull()); case TypeCode.Single: return s_singleLiftedToNull ?? (s_singleLiftedToNull = new EqualSingleLiftedToNull()); default: Debug.Assert(underlyingType.GetTypeCode() == TypeCode.Double); return s_doubleLiftedToNull ?? (s_doubleLiftedToNull = new EqualDoubleLiftedToNull()); } } else { switch (underlyingType.GetTypeCode()) { case TypeCode.Boolean: return s_boolean ?? (s_boolean = new EqualBoolean()); case TypeCode.SByte: return s_SByte ?? (s_SByte = new EqualSByte()); case TypeCode.Byte: return s_byte ?? (s_byte = new EqualByte()); case TypeCode.Char: return s_char ?? (s_char = new EqualChar()); case TypeCode.Int16: return s_int16 ?? (s_int16 = new EqualInt16()); case TypeCode.Int32: return s_int32 ?? (s_int32 = new EqualInt32()); case TypeCode.Int64: return s_int64 ?? (s_int64 = new EqualInt64()); case TypeCode.UInt16: return s_UInt16 ?? (s_UInt16 = new EqualUInt16()); case TypeCode.UInt32: return s_UInt32 ?? (s_UInt32 = new EqualUInt32()); case TypeCode.UInt64: return s_UInt64 ?? (s_UInt64 = new EqualUInt64()); case TypeCode.Single: return s_single ?? (s_single = new EqualSingle()); case TypeCode.Double: return s_double ?? (s_double = new EqualDouble()); default: // Nullable only valid if one operand is constant null, so this assert is slightly too broad. Debug.Assert(type.IsNullableOrReferenceType()); return s_reference ?? (s_reference = new EqualReference()); } } }