Fix const array access folding

This commit is contained in:
drmortalwombat 2024-04-12 16:11:47 +02:00
parent 5971f9a80f
commit 55ddce2211
5 changed files with 46 additions and 49 deletions

View File

@ -452,6 +452,44 @@ Expression* Expression::LogicInvertExpression(void)
} }
} }
Expression* Expression::ConstantDereference(void)
{
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)
{
Declaration* mdec = mDecValue->mBase->mValue->mDecValue->mParams;
int coffset = mDecValue->mOffset;
while (mdec)
{
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
{
Expression* ex = new Expression(mLocation, EX_CONSTANT);
ex->mDecValue = mdec;
ex->mDecType = mDecType;
return ex;
}
}
}
}
return this;
}
Expression* Expression::ConstantFold(Errors * errors, LinkerSection * dataSection, Linker* linker) Expression* Expression::ConstantFold(Errors * errors, LinkerSection * dataSection, Linker* linker)
{ {
if (mType == EX_PREFIX && mToken == TK_BANKOF && linker) if (mType == EX_PREFIX && mToken == TK_BANKOF && linker)
@ -925,21 +963,6 @@ Expression* Expression::ConstantFold(Errors * errors, LinkerSection * dataSectio
{ {
int offset = int(mDecType->mSize * mRight->mDecValue->mInteger); int offset = int(mDecType->mSize * mRight->mDecValue->mInteger);
if (mDecType->IsSimpleType() && (mLeft->mDecValue->mFlags & DTF_CONST) && mLeft->mDecValue->mValue && mLeft->mDecValue->mValue->mType == EX_CONSTANT && mLeft->mDecValue->mValue->mDecValue->mType == DT_CONST_STRUCT)
{
Declaration* mdec = mLeft->mDecValue->mValue->mDecValue->mParams;
while (mdec && mdec->mOffset != offset)
mdec = mdec->mNext;
if (mdec)
{
Expression* ex = new Expression(mLocation, EX_CONSTANT);
ex->mDecValue = mdec;
ex->mDecType = mDecType;
return ex;
}
}
Expression* ex = new Expression(mLocation, EX_VARIABLE); Expression* ex = new Expression(mLocation, EX_VARIABLE);
Declaration* dec = new Declaration(mLocation, DT_VARIABLE_REF); Declaration* dec = new Declaration(mLocation, DT_VARIABLE_REF);
dec->mFlags = mLeft->mDecValue->mFlags; dec->mFlags = mLeft->mDecValue->mFlags;
@ -956,35 +979,6 @@ Expression* Expression::ConstantFold(Errors * errors, LinkerSection * dataSectio
{ {
int offset = mLeft->mDecValue->mOffset + int(mDecType->mSize * mRight->mDecValue->mInteger); int offset = mLeft->mDecValue->mOffset + int(mDecType->mSize * mRight->mDecValue->mInteger);
if (mDecType->IsSimpleType() && (mLeft->mDecValue->mFlags & DTF_CONST) && mLeft->mDecValue->mBase->mValue && mLeft->mDecValue->mBase->mValue->mType == EX_CONSTANT && mLeft->mDecValue->mBase->mValue->mDecValue->mType == DT_CONST_STRUCT)
{
Declaration* mdec = mLeft->mDecValue->mBase->mValue->mDecValue->mParams;
int coffset = offset;
while (mdec)
{
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
{
Expression* ex = new Expression(mLocation, EX_CONSTANT);
ex->mDecValue = mdec;
ex->mDecType = mDecType;
return ex;
}
}
}
Expression* ex = new Expression(mLocation, EX_VARIABLE); Expression* ex = new Expression(mLocation, EX_VARIABLE);
Declaration* dec = new Declaration(mLocation, DT_VARIABLE_REF); Declaration* dec = new Declaration(mLocation, DT_VARIABLE_REF);
dec->mFlags = mLeft->mDecValue->mFlags; dec->mFlags = mLeft->mDecValue->mFlags;

View File

@ -248,6 +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);
bool HasSideEffects(void) const; bool HasSideEffects(void) const;
bool IsSame(const Expression* exp) const; bool IsSame(const Expression* exp) const;

View File

@ -1405,6 +1405,8 @@ 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();
Declaration* dec = exp->mDecValue; Declaration* dec = exp->mDecValue;
if (exp->mType == EX_CONSTANT) if (exp->mType == EX_CONSTANT)

View File

@ -75,7 +75,7 @@ int main2(int argc, const char** argv)
#else #else
strcpy(strProductName, "oscar64"); strcpy(strProductName, "oscar64");
strcpy(strProductVersion, "1.27.241"); strcpy(strProductVersion, "1.27.242");
#ifdef __APPLE__ #ifdef __APPLE__
uint32_t length = sizeof(basePath); uint32_t length = sizeof(basePath);

View File

@ -25,8 +25,8 @@ LANGUAGE LANG_ENGLISH, SUBLANG_NEUTRAL
// //
VS_VERSION_INFO VERSIONINFO VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,27,241,0 FILEVERSION 1,27,242,0
PRODUCTVERSION 1,27,241,0 PRODUCTVERSION 1,27,242,0
FILEFLAGSMASK 0x3fL FILEFLAGSMASK 0x3fL
#ifdef _DEBUG #ifdef _DEBUG
FILEFLAGS 0x1L FILEFLAGS 0x1L
@ -43,12 +43,12 @@ BEGIN
BEGIN BEGIN
VALUE "CompanyName", "oscar64" VALUE "CompanyName", "oscar64"
VALUE "FileDescription", "oscar64 compiler" VALUE "FileDescription", "oscar64 compiler"
VALUE "FileVersion", "1.27.241.0" VALUE "FileVersion", "1.27.242.0"
VALUE "InternalName", "oscar64.exe" VALUE "InternalName", "oscar64.exe"
VALUE "LegalCopyright", "Copyright (C) 2021" VALUE "LegalCopyright", "Copyright (C) 2021"
VALUE "OriginalFilename", "oscar64.exe" VALUE "OriginalFilename", "oscar64.exe"
VALUE "ProductName", "oscar64" VALUE "ProductName", "oscar64"
VALUE "ProductVersion", "1.27.241.0" VALUE "ProductVersion", "1.27.242.0"
END END
END END
BLOCK "VarFileInfo" BLOCK "VarFileInfo"