From 32f0b8c6f3a7e89df70d4c353cab45207340b0fc Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Sun, 16 Mar 2025 15:53:27 +0100 Subject: [PATCH] Fix warnings in unreachable code --- oscar64/Declaration.cpp | 45 +++++++++------------ oscar64/Errors.cpp | 72 +++++++++++++++++++--------------- oscar64/Errors.h | 5 +++ oscar64/GlobalOptimizer.cpp | 18 +++++++-- oscar64/InterCodeGenerator.cpp | 21 ++++++++++ 5 files changed, 99 insertions(+), 62 deletions(-) diff --git a/oscar64/Declaration.cpp b/oscar64/Declaration.cpp index dc19398..ad0b8af 100644 --- a/oscar64/Declaration.cpp +++ b/oscar64/Declaration.cpp @@ -898,11 +898,19 @@ Expression* Expression::ConstantFold(Errors * errors, LinkerSection * dataSectio { int64 ival = 0, ileft = mLeft->mDecValue->mInteger, iright = mRight->mDecValue->mInteger; - bool signop = - (mLeft->mDecValue->mBase->mSize < 2 || (mLeft->mDecValue->mBase->mFlags & DTF_SIGNED)) && - (mRight->mDecValue->mBase->mSize < 2 || (mRight->mDecValue->mBase->mFlags & DTF_SIGNED)); + Declaration* dtype = TheSignedIntTypeDeclaration; + if (mLeft->mDecValue->mBase->mSize > mRight->mDecValue->mBase->mSize) + dtype = mLeft->mDecValue->mBase; + else if (mLeft->mDecValue->mBase->mSize < mRight->mDecValue->mBase->mSize) + dtype = mRight->mDecValue->mBase; + else if (mLeft->mDecValue->mBase->mSize > 1) + { + if ((mLeft->mDecValue->mBase->mFlags & DTF_SIGNED) && !(mRight->mDecValue->mBase->mFlags & DTF_SIGNED)) + dtype = mRight->mDecValue->mBase; + else + dtype = mLeft->mDecValue->mBase; + } - bool promote = true; switch (mToken) { case TK_ADD: @@ -916,27 +924,25 @@ Expression* Expression::ConstantFold(Errors * errors, LinkerSection * dataSectio break; case TK_DIV: if (iright == 0) - errors->Error(mLocation, EERR_INVALID_VALUE, "Constant division by zero"); - else if (signop) + return this; + else if (dtype->mFlags & DTF_SIGNED) ival = ileft / iright; else ival = (uint64)ileft / (uint64)iright; break; case TK_MOD: if (iright == 0) - errors->Error(mLocation, EERR_INVALID_VALUE, "Constant division by zero"); - else if (signop) + return this; + else if (dtype->mFlags & DTF_SIGNED) ival = ileft % iright; else ival = (uint64)ileft % (uint64)iright; break; case TK_LEFT_SHIFT: ival = ileft << iright; - promote = false; break; case TK_RIGHT_SHIFT: ival = ileft >> iright; - promote = false; break; case TK_BINARY_AND: ival = ileft & iright; @@ -953,29 +959,14 @@ Expression* Expression::ConstantFold(Errors * errors, LinkerSection * dataSectio Expression* ex = new Expression(mLocation, EX_CONSTANT); Declaration* dec = new Declaration(mLocation, DT_CONST_INTEGER); - if (promote) - { - if (mLeft->mDecValue->mBase->mSize <= 2 && mRight->mDecValue->mBase->mSize <= 2) - dec->mBase = ival < 32768 ? TheSignedIntTypeDeclaration : TheUnsignedIntTypeDeclaration; - else - dec->mBase = ival < 2147483648 ? TheSignedLongTypeDeclaration : TheUnsignedLongTypeDeclaration; - } - else - { - if (mLeft->mDecValue->mBase->mSize < 2) - dec->mBase = TheSignedIntTypeDeclaration; - else - dec->mBase = mLeft->mDecValue->mBase; - } - - dec->mInteger = ival; + dec->mBase = dtype; + dec->mInteger = signextend(ival, dec->mBase); ex->mDecValue = dec; ex->mDecType = dec->mBase; return ex; } else if ((mLeft->mDecValue->mType == DT_CONST_INTEGER || mLeft->mDecValue->mType == DT_CONST_FLOAT) && (mRight->mDecValue->mType == DT_CONST_INTEGER || mRight->mDecValue->mType == DT_CONST_FLOAT)) { - double dval; double dleft = mLeft->mDecValue->mType == DT_CONST_INTEGER ? mLeft->mDecValue->mInteger : mLeft->mDecValue->mNumber; double dright = mRight->mDecValue->mType == DT_CONST_INTEGER ? mRight->mDecValue->mInteger : mRight->mDecValue->mNumber; diff --git a/oscar64/Errors.cpp b/oscar64/Errors.cpp index 25b6052..fcfd0b7 100644 --- a/oscar64/Errors.cpp +++ b/oscar64/Errors.cpp @@ -4,11 +4,18 @@ #include Errors::Errors(void) - : mErrorCount(0) + : mErrorCount(0), mMinLevel(EINFO_GENERIC) { } +ErrorID Errors::SetMinLevel(ErrorID id) +{ + ErrorID pid = mMinLevel; + mMinLevel = id; + return pid; +} + void Errors::Error(const Location& loc, ErrorID eid, const char* msg, const Ident* info1, const Ident* info2) { if (!info1) @@ -22,41 +29,44 @@ void Errors::Error(const Location& loc, ErrorID eid, const char* msg, const Iden void Errors::Error(const Location& loc, ErrorID eid, const char* msg, const char* info1, const char * info2) { - const char* level = "info"; - if (eid >= EERR_GENERIC) + if (eid >= mMinLevel) { - level = "error"; - mErrorCount++; - } - else if (eid >= EWARN_GENERIC) - { - level = "warning"; - } + const char* level = "info"; + if (eid >= EERR_GENERIC) + { + level = "error"; + mErrorCount++; + } + else if (eid >= EWARN_GENERIC) + { + level = "warning"; + } - if (loc.mFileName) - { - if (!info1) - fprintf(stderr, "%s(%d, %d) : %s %d: %s\n", loc.mFileName, loc.mLine, loc.mColumn, level, eid, msg); - else if (!info2) - fprintf(stderr, "%s(%d, %d) : %s %d: %s '%s'\n", loc.mFileName, loc.mLine, loc.mColumn, level, eid, msg, info1); - else - fprintf(stderr, "%s(%d, %d) : %s %d: %s '%s' != '%s'\n", loc.mFileName, loc.mLine, loc.mColumn, level, eid, msg, info1, info2); + if (loc.mFileName) + { + if (!info1) + fprintf(stderr, "%s(%d, %d) : %s %d: %s\n", loc.mFileName, loc.mLine, loc.mColumn, level, eid, msg); + else if (!info2) + fprintf(stderr, "%s(%d, %d) : %s %d: %s '%s'\n", loc.mFileName, loc.mLine, loc.mColumn, level, eid, msg, info1); + else + fprintf(stderr, "%s(%d, %d) : %s %d: %s '%s' != '%s'\n", loc.mFileName, loc.mLine, loc.mColumn, level, eid, msg, info1, info2); - if (loc.mFrom) - Error(*(loc.mFrom), EINFO_EXPANDED, "While expanding here"); - } - else - { - if (!info1) - fprintf(stderr, "oscar64: %s %d: %s\n", level, eid, msg); - else if (!info2) - fprintf(stderr, "oscar64: %s %d: %s '%s'\n", level, eid, msg, info1); + if (loc.mFrom) + Error(*(loc.mFrom), EINFO_EXPANDED, "While expanding here"); + } else - fprintf(stderr, "oscar64: %s %d: %s '%s' != '%s'\n", level, eid, msg, info1, info2); + { + if (!info1) + fprintf(stderr, "oscar64: %s %d: %s\n", level, eid, msg); + else if (!info2) + fprintf(stderr, "oscar64: %s %d: %s '%s'\n", level, eid, msg, info1); + else + fprintf(stderr, "oscar64: %s %d: %s '%s' != '%s'\n", level, eid, msg, info1, info2); + } + + if (mErrorCount > 10 || eid >= EFATAL_GENERIC) + exit(20); } - - if (mErrorCount > 10 || eid >= EFATAL_GENERIC) - exit(20); } diff --git a/oscar64/Errors.h b/oscar64/Errors.h index 2899936..4707f87 100644 --- a/oscar64/Errors.h +++ b/oscar64/Errors.h @@ -45,6 +45,7 @@ enum ErrorID EWARN_INSUFFICIENT_MEMORY, EWARN_FUNCTION_NOT_INLINED, EWARN_INVALID_VOID_POINTER_ARITHMETIC, + EWARN_DIVISION_BY_ZERO, EERR_GENERIC = 3000, EERR_FILE_NOT_FOUND, @@ -129,7 +130,11 @@ class Errors public: Errors(void); + ErrorID SetMinLevel(ErrorID id); + int mErrorCount; + ErrorID mMinLevel; + void Error(const Location& loc, ErrorID eid, const char* msg, const Ident* info1, const Ident* info2 = nullptr); void Error(const Location& loc, ErrorID eid, const char* msg, const char* info1 = nullptr, const char* info2 = nullptr); diff --git a/oscar64/GlobalOptimizer.cpp b/oscar64/GlobalOptimizer.cpp index 3d57a42..360aca4 100644 --- a/oscar64/GlobalOptimizer.cpp +++ b/oscar64/GlobalOptimizer.cpp @@ -243,6 +243,19 @@ bool GlobalOptimizer::ReplaceParamConst(Expression* exp, Declaration* param) bool changed = false; if (exp) { + if (ReplaceParamConst(exp->mLeft, param)) + changed = true; + if (ReplaceParamConst(exp->mRight, param)) + changed = true; + + if (changed) + { + if (exp->mLeft) + exp->mLeft = exp->mLeft->ConstantFold(mErrors, nullptr); + if (exp->mRight) + exp->mRight = exp->mRight->ConstantFold(mErrors, nullptr); + } + if (exp->mType == EX_VARIABLE && exp->mDecValue == param) { exp->mType = EX_CONSTANT; @@ -251,10 +264,6 @@ bool GlobalOptimizer::ReplaceParamConst(Expression* exp, Declaration* param) changed = true; } - if (ReplaceParamConst(exp->mLeft, param)) - changed = true; - if (ReplaceParamConst(exp->mRight, param)) - changed = true; } return changed; } @@ -382,6 +391,7 @@ bool GlobalOptimizer::Optimize(void) { if (ReplaceParamConst(func->mValue, pdec)) { + func->mValue = func->mValue->ConstantFold(mErrors, nullptr); #if DUMP_OPTS printf("Const parameter %s\n", pdec->mIdent ? pdec->mIdent->mString : "_"); #endif diff --git a/oscar64/InterCodeGenerator.cpp b/oscar64/InterCodeGenerator.cpp index 1eb7cd4..13b96f7 100644 --- a/oscar64/InterCodeGenerator.cpp +++ b/oscar64/InterCodeGenerator.cpp @@ -3181,9 +3181,13 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* break; case TK_DIV: ins->mOperator = signedOP ? IA_DIVS : IA_DIVU; + if (exp->mRight->mType == EX_CONSTANT && exp->mRight->mDecValue->mType == DT_CONST_INTEGER && exp->mRight->mDecValue->mInteger == 0) + mErrors->Error(exp->mLocation, EWARN_DIVISION_BY_ZERO, "Constant division by zero"); break; case TK_MOD: ins->mOperator = signedOP ? IA_MODS : IA_MODU; + if (exp->mRight->mType == EX_CONSTANT && exp->mRight->mDecValue->mType == DT_CONST_INTEGER && exp->mRight->mDecValue->mInteger == 0) + mErrors->Error(exp->mLocation, EWARN_DIVISION_BY_ZERO, "Constant division by zero"); break; case TK_LEFT_SHIFT: ins->mOperator = IA_SHL; @@ -5349,19 +5353,36 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* TranslateLogic(procType, proc, block, tblock, fblock, exp->mLeft, destack, gotos, inlineMapper); DestructStack* itdestack = destack; + ErrorID eid; + + bool alwaysTrue = false, alwaysFalse = false; + if (exp->mLeft->mType == EX_CONSTANT && exp->mLeft->mDecValue->mType == DT_CONST_INTEGER) + { + alwaysTrue = exp->mLeft->mDecValue->mInteger != 0; + alwaysFalse = exp->mLeft->mDecValue->mInteger == 0; + } + + if (alwaysFalse) + eid = mErrors->SetMinLevel(EERR_GENERIC); vr = TranslateExpression(procType, proc, tblock, exp->mRight->mLeft, destack, gotos, breakBlock, continueBlock, inlineMapper); UnwindDestructStack(procType, proc, tblock, destack, itdestack, inlineMapper); destack = itdestack; + if (alwaysFalse) + mErrors->SetMinLevel(eid); tblock->Append(jins0); tblock->Close(eblock, nullptr); if (exp->mRight->mRight) { + if (alwaysTrue) + eid = mErrors->SetMinLevel(EERR_GENERIC); DestructStack* ifdestack = destack; vr = TranslateExpression(procType, proc, fblock, exp->mRight->mRight, destack, gotos, breakBlock, continueBlock, inlineMapper); UnwindDestructStack(procType, proc, fblock, destack, ifdestack, inlineMapper); destack = ifdestack; + if (alwaysTrue) + mErrors->SetMinLevel(eid); } fblock->Append(jins1); fblock->Close(eblock, nullptr);