1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-15 06:44:10 +02:00

Fixed const eval for typeof comparisons

This commit is contained in:
Brian Fiete 2021-01-20 07:25:08 -08:00
parent e35318c688
commit 7e307b2f0d

View file

@ -432,6 +432,9 @@ int BfIRConstHolder::CheckConstEquality(BfIRValue lhs, BfIRValue rhs)
if (constRHS == NULL) if (constRHS == NULL)
return -1; return -1;
if (constLHS == constRHS)
return 1;
if (constLHS->mConstType == BfConstType_BitCast) if (constLHS->mConstType == BfConstType_BitCast)
return CheckConstEquality(BfIRValue(BfIRValueFlags_Const, ((BfConstantBitCast*)constLHS)->mTarget), rhs); return CheckConstEquality(BfIRValue(BfIRValueFlags_Const, ((BfConstantBitCast*)constLHS)->mTarget), rhs);
if (constRHS->mConstType == BfConstType_BitCast) if (constRHS->mConstType == BfConstType_BitCast)
@ -448,6 +451,14 @@ int BfIRConstHolder::CheckConstEquality(BfIRValue lhs, BfIRValue rhs)
} }
} }
if (((constLHS->mConstType == BfConstType_TypeOf) || (constLHS->mConstType == BfConstType_TypeOf_WithData)) &&
((constRHS->mConstType == BfConstType_TypeOf) || (constRHS->mConstType == BfConstType_TypeOf_WithData)))
{
auto typeOfLHS = (BfTypeOf_Const*)constLHS;
auto typeOfRHS = (BfTypeOf_Const*)constRHS;
return (typeOfLHS->mType == typeOfRHS->mType) ? 1 : 0;
}
if (constLHS->mTypeCode != constRHS->mTypeCode) if (constLHS->mTypeCode != constRHS->mTypeCode)
return -1; return -1;
@ -473,6 +484,12 @@ int BfIRConstHolder::CheckConstEquality(BfIRValue lhs, BfIRValue rhs)
return 1; return 1;
} }
if (constLHS->mConstType == BfConstType_GlobalVar)
{
// We would have already caught the (constLHS == constRHS) case further up
return 0;
}
return -1; return -1;
} }
@ -3865,12 +3882,9 @@ BfIRValue BfIRBuilder::CreateCmpEQ(BfIRValue lhs, BfIRValue rhs)
if ((lhs.IsConst()) && (rhs.IsConst())) if ((lhs.IsConst()) && (rhs.IsConst()))
{ {
CMP_APPLY(lhs, rhs, ==); CMP_APPLY(lhs, rhs, ==);
int eqVal = CheckConstEquality(lhs, rhs);
if ((constLHS->mConstType == BfConstType_GlobalVar) || if (eqVal != -1)
(constRHS->mConstType == BfConstType_GlobalVar)) return CreateConst(BfTypeCode_Boolean, (eqVal == 1) ? (uint64)1 : (uint64)0);
{
return CreateConst(BfTypeCode_Boolean, (constLHS == constRHS) ? (uint64)1 : (uint64)0);
}
} }
auto retVal = WriteCmd(BfIRCmd_CmpEQ, lhs, rhs); auto retVal = WriteCmd(BfIRCmd_CmpEQ, lhs, rhs);
@ -3883,12 +3897,9 @@ BfIRValue BfIRBuilder::CreateCmpNE(BfIRValue lhs, BfIRValue rhs)
if ((lhs.IsConst()) && (rhs.IsConst())) if ((lhs.IsConst()) && (rhs.IsConst()))
{ {
CMP_APPLY(lhs, rhs, !=); CMP_APPLY(lhs, rhs, !=);
int eqVal = CheckConstEquality(lhs, rhs);
if ((constLHS->mConstType == BfConstType_GlobalVar) || if (eqVal != -1)
(constRHS->mConstType == BfConstType_GlobalVar)) return CreateConst(BfTypeCode_Boolean, (eqVal == 0) ? (uint64)1 : (uint64)0);
{
return CreateConst(BfTypeCode_Boolean, (constLHS != constRHS) ? (uint64)1 : (uint64)0);
}
} }
auto retVal = WriteCmd(BfIRCmd_CmpNE, lhs, rhs); auto retVal = WriteCmd(BfIRCmd_CmpNE, lhs, rhs);