Fix warnings in unreachable code

This commit is contained in:
drmortalwombat 2025-03-16 15:53:27 +01:00
parent 10c9b735e9
commit 32f0b8c6f3
5 changed files with 99 additions and 62 deletions

View File

@ -898,11 +898,19 @@ Expression* Expression::ConstantFold(Errors * errors, LinkerSection * dataSectio
{ {
int64 ival = 0, ileft = mLeft->mDecValue->mInteger, iright = mRight->mDecValue->mInteger; int64 ival = 0, ileft = mLeft->mDecValue->mInteger, iright = mRight->mDecValue->mInteger;
bool signop = Declaration* dtype = TheSignedIntTypeDeclaration;
(mLeft->mDecValue->mBase->mSize < 2 || (mLeft->mDecValue->mBase->mFlags & DTF_SIGNED)) && if (mLeft->mDecValue->mBase->mSize > mRight->mDecValue->mBase->mSize)
(mRight->mDecValue->mBase->mSize < 2 || (mRight->mDecValue->mBase->mFlags & DTF_SIGNED)); 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) switch (mToken)
{ {
case TK_ADD: case TK_ADD:
@ -916,27 +924,25 @@ Expression* Expression::ConstantFold(Errors * errors, LinkerSection * dataSectio
break; break;
case TK_DIV: case TK_DIV:
if (iright == 0) if (iright == 0)
errors->Error(mLocation, EERR_INVALID_VALUE, "Constant division by zero"); return this;
else if (signop) else if (dtype->mFlags & DTF_SIGNED)
ival = ileft / iright; ival = ileft / iright;
else else
ival = (uint64)ileft / (uint64)iright; ival = (uint64)ileft / (uint64)iright;
break; break;
case TK_MOD: case TK_MOD:
if (iright == 0) if (iright == 0)
errors->Error(mLocation, EERR_INVALID_VALUE, "Constant division by zero"); return this;
else if (signop) else if (dtype->mFlags & DTF_SIGNED)
ival = ileft % iright; ival = ileft % iright;
else else
ival = (uint64)ileft % (uint64)iright; ival = (uint64)ileft % (uint64)iright;
break; break;
case TK_LEFT_SHIFT: case TK_LEFT_SHIFT:
ival = ileft << iright; ival = ileft << iright;
promote = false;
break; break;
case TK_RIGHT_SHIFT: case TK_RIGHT_SHIFT:
ival = ileft >> iright; ival = ileft >> iright;
promote = false;
break; break;
case TK_BINARY_AND: case TK_BINARY_AND:
ival = ileft & iright; ival = ileft & iright;
@ -953,29 +959,14 @@ Expression* Expression::ConstantFold(Errors * errors, LinkerSection * dataSectio
Expression* ex = new Expression(mLocation, EX_CONSTANT); Expression* ex = new Expression(mLocation, EX_CONSTANT);
Declaration* dec = new Declaration(mLocation, DT_CONST_INTEGER); Declaration* dec = new Declaration(mLocation, DT_CONST_INTEGER);
if (promote) dec->mBase = dtype;
{ dec->mInteger = signextend(ival, dec->mBase);
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;
ex->mDecValue = dec; ex->mDecValue = dec;
ex->mDecType = dec->mBase; ex->mDecType = dec->mBase;
return ex; 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)) 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 dval;
double dleft = mLeft->mDecValue->mType == DT_CONST_INTEGER ? mLeft->mDecValue->mInteger : mLeft->mDecValue->mNumber; 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; double dright = mRight->mDecValue->mType == DT_CONST_INTEGER ? mRight->mDecValue->mInteger : mRight->mDecValue->mNumber;

View File

@ -4,11 +4,18 @@
#include <stdlib.h> #include <stdlib.h>
Errors::Errors(void) 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) void Errors::Error(const Location& loc, ErrorID eid, const char* msg, const Ident* info1, const Ident* info2)
{ {
if (!info1) if (!info1)
@ -21,6 +28,8 @@ 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) void Errors::Error(const Location& loc, ErrorID eid, const char* msg, const char* info1, const char * info2)
{
if (eid >= mMinLevel)
{ {
const char* level = "info"; const char* level = "info";
if (eid >= EERR_GENERIC) if (eid >= EERR_GENERIC)
@ -58,5 +67,6 @@ void Errors::Error(const Location& loc, ErrorID eid, const char* msg, const char
if (mErrorCount > 10 || eid >= EFATAL_GENERIC) if (mErrorCount > 10 || eid >= EFATAL_GENERIC)
exit(20); exit(20);
} }
}

View File

@ -45,6 +45,7 @@ enum ErrorID
EWARN_INSUFFICIENT_MEMORY, EWARN_INSUFFICIENT_MEMORY,
EWARN_FUNCTION_NOT_INLINED, EWARN_FUNCTION_NOT_INLINED,
EWARN_INVALID_VOID_POINTER_ARITHMETIC, EWARN_INVALID_VOID_POINTER_ARITHMETIC,
EWARN_DIVISION_BY_ZERO,
EERR_GENERIC = 3000, EERR_GENERIC = 3000,
EERR_FILE_NOT_FOUND, EERR_FILE_NOT_FOUND,
@ -129,7 +130,11 @@ class Errors
public: public:
Errors(void); Errors(void);
ErrorID SetMinLevel(ErrorID id);
int mErrorCount; 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 Ident* info1, const Ident* info2 = nullptr);
void Error(const Location& loc, ErrorID eid, const char* msg, const char* info1 = nullptr, const char* info2 = nullptr); void Error(const Location& loc, ErrorID eid, const char* msg, const char* info1 = nullptr, const char* info2 = nullptr);

View File

@ -243,6 +243,19 @@ bool GlobalOptimizer::ReplaceParamConst(Expression* exp, Declaration* param)
bool changed = false; bool changed = false;
if (exp) 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) if (exp->mType == EX_VARIABLE && exp->mDecValue == param)
{ {
exp->mType = EX_CONSTANT; exp->mType = EX_CONSTANT;
@ -251,10 +264,6 @@ bool GlobalOptimizer::ReplaceParamConst(Expression* exp, Declaration* param)
changed = true; changed = true;
} }
if (ReplaceParamConst(exp->mLeft, param))
changed = true;
if (ReplaceParamConst(exp->mRight, param))
changed = true;
} }
return changed; return changed;
} }
@ -382,6 +391,7 @@ bool GlobalOptimizer::Optimize(void)
{ {
if (ReplaceParamConst(func->mValue, pdec)) if (ReplaceParamConst(func->mValue, pdec))
{ {
func->mValue = func->mValue->ConstantFold(mErrors, nullptr);
#if DUMP_OPTS #if DUMP_OPTS
printf("Const parameter %s\n", pdec->mIdent ? pdec->mIdent->mString : "_"); printf("Const parameter %s\n", pdec->mIdent ? pdec->mIdent->mString : "_");
#endif #endif

View File

@ -3181,9 +3181,13 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration*
break; break;
case TK_DIV: case TK_DIV:
ins->mOperator = signedOP ? IA_DIVS : IA_DIVU; 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; break;
case TK_MOD: case TK_MOD:
ins->mOperator = signedOP ? IA_MODS : IA_MODU; 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; break;
case TK_LEFT_SHIFT: case TK_LEFT_SHIFT:
ins->mOperator = IA_SHL; ins->mOperator = IA_SHL;
@ -5349,19 +5353,36 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration*
TranslateLogic(procType, proc, block, tblock, fblock, exp->mLeft, destack, gotos, inlineMapper); TranslateLogic(procType, proc, block, tblock, fblock, exp->mLeft, destack, gotos, inlineMapper);
DestructStack* itdestack = destack; 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); vr = TranslateExpression(procType, proc, tblock, exp->mRight->mLeft, destack, gotos, breakBlock, continueBlock, inlineMapper);
UnwindDestructStack(procType, proc, tblock, destack, itdestack, inlineMapper); UnwindDestructStack(procType, proc, tblock, destack, itdestack, inlineMapper);
destack = itdestack; destack = itdestack;
if (alwaysFalse)
mErrors->SetMinLevel(eid);
tblock->Append(jins0); tblock->Append(jins0);
tblock->Close(eblock, nullptr); tblock->Close(eblock, nullptr);
if (exp->mRight->mRight) if (exp->mRight->mRight)
{ {
if (alwaysTrue)
eid = mErrors->SetMinLevel(EERR_GENERIC);
DestructStack* ifdestack = destack; DestructStack* ifdestack = destack;
vr = TranslateExpression(procType, proc, fblock, exp->mRight->mRight, destack, gotos, breakBlock, continueBlock, inlineMapper); vr = TranslateExpression(procType, proc, fblock, exp->mRight->mRight, destack, gotos, breakBlock, continueBlock, inlineMapper);
UnwindDestructStack(procType, proc, fblock, destack, ifdestack, inlineMapper); UnwindDestructStack(procType, proc, fblock, destack, ifdestack, inlineMapper);
destack = ifdestack; destack = ifdestack;
if (alwaysTrue)
mErrors->SetMinLevel(eid);
} }
fblock->Append(jins1); fblock->Append(jins1);
fblock->Close(eblock, nullptr); fblock->Close(eblock, nullptr);