From c3c0583f198d4dcccd2c60367b1c5ade492ad46f Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Fri, 12 Apr 2024 16:51:26 +0200 Subject: [PATCH] Fix const array dereference --- oscar64/Declaration.cpp | 57 +++++++++++++++++++++----------- oscar64/Declaration.h | 2 +- oscar64/Parser.cpp | 2 +- oscar64setup/oscar64setup.vdproj | 6 ++-- 4 files changed, 42 insertions(+), 25 deletions(-) diff --git a/oscar64/Declaration.cpp b/oscar64/Declaration.cpp index e873432..73c13de 100644 --- a/oscar64/Declaration.cpp +++ b/oscar64/Declaration.cpp @@ -452,40 +452,57 @@ Expression* Expression::LogicInvertExpression(void) } } -Expression* Expression::ConstantDereference(void) +Expression* Expression::ConstantDereference(Errors* errors, LinkerSection* dataSection) { if (mType == EX_VARIABLE) { - if (mDecValue->mType == DT_VARIABLE && (mDecValue->mFlags & DTF_CONST) && mDecValue->mValue) - return mDecValue->mValue; - else if (mDecValue->mType == DT_VARIABLE_REF && (mDecValue->mFlags & DTF_CONST) && mDecValue->mBase->mValue && mDecValue->mBase->mValue->mType == EX_CONSTANT && mDecValue->mBase->mValue->mDecValue->mType == DT_CONST_STRUCT) + if (mDecType->IsSimpleType()) { - Declaration* mdec = mDecValue->mBase->mValue->mDecValue->mParams; - int coffset = mDecValue->mOffset; - while (mdec) + if (mDecValue->mType == DT_VARIABLE && (mDecValue->mFlags & DTF_CONST) && mDecValue->mValue) + return mDecValue->mValue; + else if (mDecValue->mType == DT_VARIABLE_REF && (mDecValue->mFlags & DTF_CONST) && mDecValue->mBase->mValue && mDecValue->mBase->mValue->mType == EX_CONSTANT && mDecValue->mBase->mValue->mDecValue->mType == DT_CONST_STRUCT) { - if (mdec->mType == DT_CONST_STRUCT) + Declaration* mdec = mDecValue->mBase->mValue->mDecValue->mParams; + int coffset = mDecValue->mOffset; + while (mdec) { - if (mdec->mOffset > coffset || mdec->mOffset + mdec->mSize <= coffset) + if (mdec->mType == DT_CONST_STRUCT) + { + if (mdec->mOffset > coffset || mdec->mOffset + mdec->mSize <= coffset) + mdec = mdec->mNext; + else + { + coffset -= mdec->mOffset; + mdec = mdec->mParams; + } + } + else if (mdec->mOffset != coffset) mdec = mdec->mNext; else { - coffset -= mdec->mOffset; - mdec = mdec->mParams; + Expression* ex = new Expression(mLocation, EX_CONSTANT); + ex->mDecValue = mdec; + ex->mDecType = mDecType; + return ex; } } - else if (mdec->mOffset != coffset) - mdec = mdec->mNext; - else - { - Expression* ex = new Expression(mLocation, EX_CONSTANT); - ex->mDecValue = mdec; - ex->mDecType = mDecType; - return ex; - } } } } + else if (mType == EX_BINARY) + { + Expression* lexp = mLeft->ConstantDereference(errors, dataSection); + Expression* rexp = mRight->ConstantDereference(errors, dataSection); + if (lexp != mLeft || rexp != mRight) + { + Expression* nexp = new Expression(mLocation, EX_BINARY); + nexp->mToken = mToken; + nexp->mDecType = mDecType; + nexp->mLeft = lexp; + nexp->mRight = rexp; + return nexp->ConstantFold(errors, dataSection)->ConstantDereference(errors, dataSection); + } + } return this; } diff --git a/oscar64/Declaration.h b/oscar64/Declaration.h index a28071b..a3243d3 100644 --- a/oscar64/Declaration.h +++ b/oscar64/Declaration.h @@ -248,7 +248,7 @@ public: Expression* LogicInvertExpression(void); Expression* ConstantFold(Errors * errors, LinkerSection* dataSection, Linker * linker = nullptr); - Expression* ConstantDereference(void); + Expression* ConstantDereference(Errors* errors, LinkerSection* dataSection); bool HasSideEffects(void) const; bool IsSame(const Expression* exp) const; diff --git a/oscar64/Parser.cpp b/oscar64/Parser.cpp index 6d00d42..3c50649 100644 --- a/oscar64/Parser.cpp +++ b/oscar64/Parser.cpp @@ -1405,7 +1405,7 @@ Declaration* Parser::ReverseDeclaration(Declaration* odec, Declaration* bdec) Declaration * Parser::CopyConstantInitializer(int offset, Declaration* dtype, Expression* exp) { - exp = exp->ConstantDereference(); + exp = exp->ConstantDereference(mErrors, mDataSection); Declaration* dec = exp->mDecValue; diff --git a/oscar64setup/oscar64setup.vdproj b/oscar64setup/oscar64setup.vdproj index 7e81bcd..fd4cb13 100644 --- a/oscar64setup/oscar64setup.vdproj +++ b/oscar64setup/oscar64setup.vdproj @@ -6154,15 +6154,15 @@ { "Name" = "8:Microsoft Visual Studio" "ProductName" = "8:oscar64" - "ProductCode" = "8:{D12BF264-6CF6-4631-A5F7-D73B85702BEA}" - "PackageCode" = "8:{1A998B16-D7AB-43C3-8240-BA87403D6169}" + "ProductCode" = "8:{7E6F8BA9-C000-403E-A935-50863009AD03}" + "PackageCode" = "8:{EAE2B6EE-7460-4439-9EA5-71D17A7290BA}" "UpgradeCode" = "8:{9AB61EFF-ACAC-4079-9950-8D96615CD4EF}" "AspNetVersion" = "8:2.0.50727.0" "RestartWWWService" = "11:FALSE" "RemovePreviousVersions" = "11:TRUE" "DetectNewerInstalledVersion" = "11:TRUE" "InstallAllUsers" = "11:FALSE" - "ProductVersion" = "8:1.27.241" + "ProductVersion" = "8:1.27.242" "Manufacturer" = "8:oscar64" "ARPHELPTELEPHONE" = "8:" "ARPHELPLINK" = "8:"