mirror of
https://github.com/beefytech/Beef.git
synced 2025-07-04 23:36:00 +02:00
Improved spaceship- const evaluation, subtraction optimization
This commit is contained in:
parent
292e0992a4
commit
2bcfdcc06c
2 changed files with 68 additions and 21 deletions
|
@ -18514,7 +18514,32 @@ void BfExprEvaluator::PerformBinaryOperation(BfType* resultType, BfIRValue convL
|
|||
case BfBinaryOp_Compare:
|
||||
{
|
||||
auto intType = mModule->GetPrimitiveType(BfTypeCode_IntPtr);
|
||||
|
||||
if ((convLeftValue.IsConst()) && (convRightValue.IsConst()))
|
||||
{
|
||||
auto cmpLtVal = mModule->mBfIRBuilder->CreateCmpLT(convLeftValue, convRightValue, resultType->IsSignedInt());
|
||||
auto ltConstant = mModule->mBfIRBuilder->GetConstant(cmpLtVal);
|
||||
if (ltConstant->mBool)
|
||||
{
|
||||
mResult = BfTypedValue(mModule->GetConstValue(-1, mModule->GetPrimitiveType(BfTypeCode_IntPtr)), intType);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto cmpGtVal = mModule->mBfIRBuilder->CreateCmpGT(convLeftValue, convRightValue, resultType->IsSignedInt());
|
||||
auto rtConstant = mModule->mBfIRBuilder->GetConstant(cmpGtVal);
|
||||
if (rtConstant->mBool)
|
||||
mResult = BfTypedValue(mModule->GetConstValue(1, mModule->GetPrimitiveType(BfTypeCode_IntPtr)), intType);
|
||||
else
|
||||
mResult = BfTypedValue(mModule->GetConstValue(0, mModule->GetPrimitiveType(BfTypeCode_IntPtr)), intType);
|
||||
}
|
||||
}
|
||||
else if ((resultType->IsIntegral()) && (resultType->mSize < intType->mSize))
|
||||
{
|
||||
auto leftIntValue = mModule->mBfIRBuilder->CreateNumericCast(convLeftValue, resultType->IsSignedInt(), BfTypeCode_IntPtr);
|
||||
auto rightIntValue = mModule->mBfIRBuilder->CreateNumericCast(convRightValue, resultType->IsSignedInt(), BfTypeCode_IntPtr);
|
||||
mResult = BfTypedValue(mModule->mBfIRBuilder->CreateSub(leftIntValue, rightIntValue), intType);
|
||||
}
|
||||
else
|
||||
{
|
||||
BfIRBlock checkGtBlock = mModule->mBfIRBuilder->CreateBlock("cmpCheckGt");
|
||||
BfIRBlock eqBlock = mModule->mBfIRBuilder->CreateBlock("cmpEq");
|
||||
BfIRBlock endBlock = mModule->mBfIRBuilder->CreateBlock("cmpEnd");
|
||||
|
@ -18542,6 +18567,7 @@ void BfExprEvaluator::PerformBinaryOperation(BfType* resultType, BfIRValue convL
|
|||
|
||||
mResult = BfTypedValue(phiVal, intType);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
mModule->Fail("Invalid operation", opToken);
|
||||
|
|
21
IDEHelper/Tests/src/Spaceship.bf
Normal file
21
IDEHelper/Tests/src/Spaceship.bf
Normal file
|
@ -0,0 +1,21 @@
|
|||
using System;
|
||||
|
||||
namespace Tests
|
||||
{
|
||||
class Spaceship
|
||||
{
|
||||
const bool sLess0 = 1.0 <=> 2.0 < 0;
|
||||
|
||||
[Test]
|
||||
public static void TestBasics()
|
||||
{
|
||||
Test.Assert(1 <=> 2 < 0);
|
||||
Test.Assert(1.0 <=> 2.0 < 0);
|
||||
|
||||
int8 i8a = -120;
|
||||
int8 i8b = 100;
|
||||
|
||||
Test.Assert(i8a <=> i8b < 0);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue