From fe667863b2133ea3fd441c2f195382be09068293 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Thu, 15 May 2025 07:58:58 +0200 Subject: [PATCH] Constant folding of absolute pointer difference --- oscar64/Declaration.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/oscar64/Declaration.cpp b/oscar64/Declaration.cpp index 883c19d..8aafe25 100644 --- a/oscar64/Declaration.cpp +++ b/oscar64/Declaration.cpp @@ -1032,6 +1032,20 @@ Expression* Expression::ConstantFold(Errors * errors, LinkerSection * dataSectio ex->mDecType = dec->mBase; return ex; } + else if (mLeft->mDecValue->mType == DT_CONST_ADDRESS && mRight->mDecValue->mType == DT_CONST_ADDRESS && mToken == TK_SUB) + { + if (mLeft->mDecType->mType == DT_TYPE_POINTER && mRight->mDecType->mType == DT_TYPE_POINTER && + mLeft->mDecType->mBase->IsConstSame(mRight->mDecType->mBase)) + { + Expression* ex = new Expression(mLocation, EX_CONSTANT); + Declaration* dec = new Declaration(mLocation, DT_CONST_INTEGER); + dec->mBase = TheSignedIntTypeDeclaration; + dec->mInteger = (mLeft->mDecValue->mInteger - mRight->mDecValue->mInteger) / mLeft->mDecType->mBase->mSize; + ex->mDecValue = dec; + ex->mDecType = dec->mBase; + return ex; + } + } #if 0 else if (mLeft->mDecValue->mType == DT_CONST_POINTER && mRight->mDecValue->mType == DT_CONST_INTEGER && (mToken == TK_ADD || mToken == TK_SUB)) {