Skip to content

Commit

Permalink
Fix const array dereference
Browse files Browse the repository at this point in the history
  • Loading branch information
drmortalwombat committed Apr 12, 2024
1 parent 55ddce2 commit c3c0583
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
57 changes: 37 additions & 20 deletions oscar64/Declaration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion oscar64/Declaration.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class Expression

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;
Expand Down
2 changes: 1 addition & 1 deletion oscar64/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions oscar64setup/oscar64setup.vdproj
Original file line number Diff line number Diff line change
Expand Up @@ -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:"
Expand Down

0 comments on commit c3c0583

Please sign in to comment.