Fix const array dereference

This commit is contained in:
drmortalwombat 2024-04-12 16:51:26 +02:00
parent 55ddce2211
commit c3c0583f19
4 changed files with 42 additions and 25 deletions

View File

@ -452,40 +452,57 @@ Expression* Expression::LogicInvertExpression(void)
} }
} }
Expression* Expression::ConstantDereference(void) Expression* Expression::ConstantDereference(Errors* errors, LinkerSection* dataSection)
{ {
if (mType == EX_VARIABLE) if (mType == EX_VARIABLE)
{ {
if (mDecValue->mType == DT_VARIABLE && (mDecValue->mFlags & DTF_CONST) && mDecValue->mValue) if (mDecType->IsSimpleType())
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)
{ {
Declaration* mdec = mDecValue->mBase->mValue->mDecValue->mParams; if (mDecValue->mType == DT_VARIABLE && (mDecValue->mFlags & DTF_CONST) && mDecValue->mValue)
int coffset = mDecValue->mOffset; return mDecValue->mValue;
while (mdec) 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; mdec = mdec->mNext;
else else
{ {
coffset -= mdec->mOffset; Expression* ex = new Expression(mLocation, EX_CONSTANT);
mdec = mdec->mParams; 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; return this;
} }

View File

@ -248,7 +248,7 @@ public:
Expression* LogicInvertExpression(void); Expression* LogicInvertExpression(void);
Expression* ConstantFold(Errors * errors, LinkerSection* dataSection, Linker * linker = nullptr); Expression* ConstantFold(Errors * errors, LinkerSection* dataSection, Linker * linker = nullptr);
Expression* ConstantDereference(void); Expression* ConstantDereference(Errors* errors, LinkerSection* dataSection);
bool HasSideEffects(void) const; bool HasSideEffects(void) const;
bool IsSame(const Expression* exp) const; bool IsSame(const Expression* exp) const;

View File

@ -1405,7 +1405,7 @@ Declaration* Parser::ReverseDeclaration(Declaration* odec, Declaration* bdec)
Declaration * Parser::CopyConstantInitializer(int offset, Declaration* dtype, Expression* exp) Declaration * Parser::CopyConstantInitializer(int offset, Declaration* dtype, Expression* exp)
{ {
exp = exp->ConstantDereference(); exp = exp->ConstantDereference(mErrors, mDataSection);
Declaration* dec = exp->mDecValue; Declaration* dec = exp->mDecValue;

View File

@ -6154,15 +6154,15 @@
{ {
"Name" = "8:Microsoft Visual Studio" "Name" = "8:Microsoft Visual Studio"
"ProductName" = "8:oscar64" "ProductName" = "8:oscar64"
"ProductCode" = "8:{D12BF264-6CF6-4631-A5F7-D73B85702BEA}" "ProductCode" = "8:{7E6F8BA9-C000-403E-A935-50863009AD03}"
"PackageCode" = "8:{1A998B16-D7AB-43C3-8240-BA87403D6169}" "PackageCode" = "8:{EAE2B6EE-7460-4439-9EA5-71D17A7290BA}"
"UpgradeCode" = "8:{9AB61EFF-ACAC-4079-9950-8D96615CD4EF}" "UpgradeCode" = "8:{9AB61EFF-ACAC-4079-9950-8D96615CD4EF}"
"AspNetVersion" = "8:2.0.50727.0" "AspNetVersion" = "8:2.0.50727.0"
"RestartWWWService" = "11:FALSE" "RestartWWWService" = "11:FALSE"
"RemovePreviousVersions" = "11:TRUE" "RemovePreviousVersions" = "11:TRUE"
"DetectNewerInstalledVersion" = "11:TRUE" "DetectNewerInstalledVersion" = "11:TRUE"
"InstallAllUsers" = "11:FALSE" "InstallAllUsers" = "11:FALSE"
"ProductVersion" = "8:1.27.241" "ProductVersion" = "8:1.27.242"
"Manufacturer" = "8:oscar64" "Manufacturer" = "8:oscar64"
"ARPHELPTELEPHONE" = "8:" "ARPHELPTELEPHONE" = "8:"
"ARPHELPLINK" = "8:" "ARPHELPLINK" = "8:"