From c674fc9a8b9d3386c87f602dc0cfb479b5476ff9 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Sun, 17 Sep 2023 11:39:18 +0200 Subject: [PATCH] Add variadic sizeof... --- oscar64/Declaration.cpp | 12 ++++-------- oscar64/InterCodeGenerator.cpp | 16 ++++++++++++---- oscar64/Linker.cpp | 32 ++++++++++++++++++++++++++++++++ oscar64/Linker.h | 3 +++ oscar64/Parser.cpp | 30 ++++++++++++++++++++++++++---- 5 files changed, 77 insertions(+), 16 deletions(-) diff --git a/oscar64/Declaration.cpp b/oscar64/Declaration.cpp index 8c3690b..8622ac8 100644 --- a/oscar64/Declaration.cpp +++ b/oscar64/Declaration.cpp @@ -1069,15 +1069,15 @@ const Ident* Declaration::MangleIdent(void) } else if (mType == DT_TYPE_REFERENCE) { - mMangleIdent = mBase->MangleIdent()->PreMangle("&"); + mMangleIdent = mBase->MangleIdent()->Mangle("&"); } else if (mType == DT_TYPE_RVALUEREF) { - mMangleIdent = mBase->MangleIdent()->PreMangle("&&"); + mMangleIdent = mBase->MangleIdent()->Mangle("&&"); } else if (mType == DT_TYPE_POINTER) { - mMangleIdent = mBase->MangleIdent()->PreMangle("*"); + mMangleIdent = mBase->MangleIdent()->Mangle("*"); } else if (mType == DT_TYPE_ARRAY) { @@ -1121,11 +1121,7 @@ const Ident* Declaration::MangleIdent(void) Declaration* dec = mBase; while (dec) { - const Ident* id; - if (dec->mBase) - id = dec->mBase->MangleIdent(); - else - id = dec->MangleIdent(); + const Ident* id = dec->MangleIdent(); if (id) { diff --git a/oscar64/InterCodeGenerator.cpp b/oscar64/InterCodeGenerator.cpp index 36efa32..4593fa3 100644 --- a/oscar64/InterCodeGenerator.cpp +++ b/oscar64/InterCodeGenerator.cpp @@ -1851,11 +1851,19 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* var->mIdent = dec->mQualIdent; var->mOffset = 0; var->mSize = dec->mSize; - if ((dec->mFlags & DTF_VAR_ALIASING) || dec->mBase->mType == DT_TYPE_ARRAY) - var->mAliased = true; var->mLinkerObject = mLinker->AddObject(dec->mLocation, dec->mQualIdent, dec->mSection, LOT_DATA, dec->mAlignment); - dec->mLinkerObject = var->mLinkerObject; + + if (dec->mBase->mFlags & DTF_CONST) + var->mLinkerObject->mFlags |= LOBJF_CONST; + else if ((dec->mFlags & DTF_VAR_ALIASING) || dec->mBase->mType == DT_TYPE_ARRAY) + var->mAliased = true; var->mLinkerObject->AddData(dec->mData, dec->mSize); + + LinkerObject* aobj = mLinker->FindSame(var->mLinkerObject); + if (aobj) + var->mLinkerObject = aobj; + + dec->mLinkerObject = var->mLinkerObject; proc->mModule->mGlobalVars.Push(var); } @@ -4989,7 +4997,7 @@ InterCodeProcedure* InterCodeGenerator::TranslateProcedure(InterCodeModule * mod { InterCodeProcedure* proc = new InterCodeProcedure(mod, dec->mLocation, dec->mQualIdent, mLinker->AddObject(dec->mLocation, dec->mQualIdent, dec->mSection, LOT_BYTE_CODE, dec->mAlignment)); -#if 0 +#if 1 if (proc->mIdent && !strcmp(proc->mIdent->mString, "main")) exp->Dump(0); #endif diff --git a/oscar64/Linker.cpp b/oscar64/Linker.cpp index 05d803a..8bc60d5 100644 --- a/oscar64/Linker.cpp +++ b/oscar64/Linker.cpp @@ -274,6 +274,38 @@ LinkerObject* Linker::FindObjectByAddr(int bank, int addr) return FindObjectByAddr(addr); } +LinkerObject* Linker::FindSame(LinkerObject* obj) +{ + for (int i = 0; i < mObjects.Size(); i++) + { + LinkerObject* lobj = mObjects[i]; + if (lobj != obj && obj->IsSameConst(lobj)) + return lobj; + } + + return nullptr; +} + +bool LinkerObject::IsSameConst(const LinkerObject* obj) const +{ + if ((mFlags & LOBJF_CONST) && mFlags == obj->mFlags && + mSection == obj->mSection && mSize == obj->mSize && mAlignment == obj->mAlignment && + mReferences.Size() == obj->mReferences.Size()) + { + for (int i = 0; i < mSize; i++) + if (mData[i] != obj->mData[i]) + return false; + + for (int i = 0; i < mReferences.Size(); i++) + if (mReferences[i] != obj->mReferences[i]) + return false; + + return true; + } + + return false; +} + LinkerObject * Linker::AddObject(const Location& location, const Ident* ident, LinkerSection * section, LinkerObjectType type, int alignment) { LinkerObject* obj = new LinkerObject; diff --git a/oscar64/Linker.h b/oscar64/Linker.h index 364c3c8..c87a689 100644 --- a/oscar64/Linker.h +++ b/oscar64/Linker.h @@ -213,6 +213,8 @@ public: void MoveToSection(LinkerSection* section); void MarkRelevant(void); + + bool IsSameConst(const LinkerObject* obj) const; }; class LinkerOverlay @@ -246,6 +248,7 @@ public: bool IsSectionPlaced(LinkerSection* section); LinkerObject * AddObject(const Location & location, const Ident* ident, LinkerSection * section, LinkerObjectType type, int alignment = 1); + LinkerObject* FindSame(LinkerObject* obj); LinkerOverlay* AddOverlay(const Location& location, const Ident* ident, int bank); diff --git a/oscar64/Parser.cpp b/oscar64/Parser.cpp index 30391e4..5089016 100644 --- a/oscar64/Parser.cpp +++ b/oscar64/Parser.cpp @@ -3415,7 +3415,7 @@ Expression* Parser::AddFunctionCallRefReturned(Expression* exp) else if ((pdec->mBase->mType == DT_TYPE_REFERENCE || pdec->mBase->mType == DT_TYPE_RVALUEREF) && pex->mType == EX_CONSTANT) { // A simple constant is passed by const ref - if (pex->mDecValue->mType == DT_CONST_INTEGER || pex->mDecValue->mType == DT_CONST_FLOAT || pex->mDecValue->mType == DT_CONST_POINTER) + if (pex->mDecValue->mType == DT_CONST_INTEGER || pex->mDecValue->mType == DT_CONST_FLOAT || pex->mDecValue->mType == DT_CONST_POINTER || pex->mDecValue->mType == DT_CONST_ADDRESS) { int nindex = mLocalIndex++; @@ -5076,7 +5076,7 @@ Expression* Parser::ParseSimpleExpression(bool lhs) dec->mBase = new Declaration(mScanner->mLocation, DT_TYPE_ARRAY); dec->mBase->mSize = dec->mSize; dec->mBase->mBase = TheConstCharTypeDeclaration; - dec->mBase->mFlags |= DTF_DEFINED; + dec->mBase->mFlags |= DTF_DEFINED | DTF_CONST; uint8* d = new uint8[size + 1]; dec->mData = d; @@ -5343,10 +5343,32 @@ Expression* Parser::ParseSimpleExpression(bool lhs) break; case TK_SIZEOF: mScanner->NextToken(); - rexp = ParseParenthesisExpression(); + dec = new Declaration(mScanner->mLocation, DT_CONST_INTEGER); dec->mBase = TheSignedIntTypeDeclaration; - dec->mInteger = rexp->mDecType->mSize; + + if (ConsumeTokenIf(TK_ELLIPSIS)) + { + rexp = ParseParenthesisExpression(); + if (rexp->mType == EX_PACK) + { + int n = 0; + Declaration* vdec = rexp->mDecValue->mParams; + while (vdec) + { + n++; + vdec = vdec->mNext; + } + dec->mInteger = n; + } + else + mErrors->Error(mScanner->mLocation, EERR_INVALID_PACK_USAGE, "variadic pack expected"); + } + else + { + rexp = ParseParenthesisExpression(); + dec->mInteger = rexp->mDecType->mSize; + } exp = new Expression(mScanner->mLocation, EX_CONSTANT); exp->mDecValue = dec; exp->mDecType = dec->mBase;