Fix compiler crash when using function result in if condition

This commit is contained in:
drmortalwombat 2023-10-24 14:29:46 +02:00
parent 0a3ad006b7
commit 982fe17aed
11 changed files with 378 additions and 311 deletions

View File

@ -73,6 +73,15 @@ void vic_waitFrame(void)
; ;
} }
void vic_waitFrames(char n)
{
while (n > 0)
{
vic_waitFrame();
n--;
}
}
void vic_waitLine(int line) void vic_waitLine(int line)
{ {
char upper = (char)(line >> 1) & VIC_CTRL1_RST8; char upper = (char)(line >> 1) & VIC_CTRL1_RST8;

View File

@ -109,6 +109,9 @@ inline void vic_waitTop(void);
// wait for the top of the frame and then for the bottom of the visual area // wait for the top of the frame and then for the bottom of the visual area
inline void vic_waitFrame(void); inline void vic_waitFrame(void);
// wait for n frames
void vic_waitFrames(char n);
// wait for a specific raster line // wait for a specific raster line
void vic_waitLine(int line); void vic_waitLine(int line);

View File

@ -516,7 +516,7 @@ const char * string::cend(void) const
string string::substr(char pos, char len) const string string::substr(char pos, char len) const
{ {
if (!cstr || len == 0 || pos >= cstr[0]) if (!cstr || len == 0 || pos >= cstr[0])
return string; return string();
else else
{ {
char l = cstr[0]; char l = cstr[0];

View File

@ -40,6 +40,10 @@ void Errors::Error(const Location& loc, ErrorID eid, const char* msg, const char
else else
fprintf(stderr, "%s(%d, %d) : %s %d: %s '%s' != '%s'\n", loc.mFileName, loc.mLine, loc.mColumn, level, eid, msg, info1, info2); 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");
if (mErrorCount > 10 || eid >= EFATAL_GENERIC) if (mErrorCount > 10 || eid >= EFATAL_GENERIC)
exit(20); exit(20);
} }

View File

@ -3,10 +3,14 @@
class Location class Location
{ {
public: public:
const char* mFileName; const char * mFileName;
int mLine, mColumn; int mLine, mColumn;
const Location * mFrom;
Location() : mFileName(nullptr), mLine(0), mColumn(0) {} Location() : mFileName(nullptr), mLine(0), mColumn(0), mFrom(nullptr) {}
Location(const Location& loc, const Location* from)
: mFileName(loc.mFileName), mLine(loc.mLine), mColumn(loc.mColumn), mFrom(from)
{}
}; };
class Ident; class Ident;
@ -14,6 +18,7 @@ class Ident;
enum ErrorID enum ErrorID
{ {
EINFO_GENERIC = 1000, EINFO_GENERIC = 1000,
EINFO_EXPANDED = 1001,
EWARN_GENERIC = 2000, EWARN_GENERIC = 2000,
EWARN_CONSTANT_TRUNCATED, EWARN_CONSTANT_TRUNCATED,

View File

@ -16826,6 +16826,26 @@ void InterCodeBasicBlock::PeepholeOptimization(const GrowingVariableArray& stati
} }
} }
void InterCodeBasicBlock::CheckNullptrDereference(void)
{
if (!mVisited)
{
mVisited = true;
for (int i = 0; i < mInstructions.Size(); i++)
{
InterInstruction* ins = mInstructions[i];
if (ins->mCode == IC_LOAD && ins->mSrc[0].mTemp < 0 && ins->mSrc[0].mMemory == IM_ABSOLUTE && ins->mSrc[0].mIntConst == 0 && !ins->mVolatile)
mProc->mModule->mErrors->Error(ins->mLocation, EWARN_NULL_POINTER_DEREFERENCED, "nullptr dereferenced");
else if (ins->mCode == IC_STORE && ins->mSrc[1].mTemp < 0 && ins->mSrc[1].mMemory == IM_ABSOLUTE && ins->mSrc[1].mIntConst == 0 && !ins->mVolatile)
mProc->mModule->mErrors->Error(ins->mLocation, EWARN_NULL_POINTER_DEREFERENCED, "nullptr dereferenced");
}
if (mTrueJump) mTrueJump->CheckNullptrDereference();
if (mFalseJump) mFalseJump->CheckNullptrDereference();
}
}
void InterCodeBasicBlock::CheckValueReturn(void) void InterCodeBasicBlock::CheckValueReturn(void)
{ {
if (!mVisited) if (!mVisited)
@ -19088,6 +19108,9 @@ void InterCodeProcedure::Close(void)
mEntryBlock->CheckValueReturn(); mEntryBlock->CheckValueReturn();
} }
ResetVisited();
mEntryBlock->CheckNullptrDereference();
if (mSaveTempsLinkerObject && mTempSize > BC_REG_TMP_SAVED - BC_REG_TMP) if (mSaveTempsLinkerObject && mTempSize > BC_REG_TMP_SAVED - BC_REG_TMP)
mSaveTempsLinkerObject->AddSpace(mTempSize - (BC_REG_TMP_SAVED - BC_REG_TMP)); mSaveTempsLinkerObject->AddSpace(mTempSize - (BC_REG_TMP_SAVED - BC_REG_TMP));

View File

@ -605,6 +605,7 @@ public:
void WarnUsedUndefinedVariables(void); void WarnUsedUndefinedVariables(void);
void CheckValueReturn(void); void CheckValueReturn(void);
void CheckNullptrDereference(void);
void CollectGlobalReferences(NumberSet& referencedGlobals, NumberSet& modifiedGlobals, bool & storesIndirect, bool & loadsIndirect, bool & globalsChecked); void CollectGlobalReferences(NumberSet& referencedGlobals, NumberSet& modifiedGlobals, bool & storesIndirect, bool & loadsIndirect, bool & globalsChecked);

File diff suppressed because it is too large Load Diff

View File

@ -61,6 +61,7 @@ protected:
int mResult, mDepth, mVarIndex; int mResult, mDepth, mVarIndex;
bool mConstExpr; bool mConstExpr;
ExValue * mResultExp; ExValue * mResultExp;
Location * mLocation;
InlineMapper(void) InlineMapper(void)
: mParams(-1), mResult(-1), mDepth(0), mResultExp(nullptr) : mParams(-1), mResult(-1), mDepth(0), mResultExp(nullptr)
@ -78,17 +79,19 @@ protected:
InterCodeProcedure* mMainInitProc; InterCodeProcedure* mMainInitProc;
InterCodeBasicBlock* mMainInitBlock; InterCodeBasicBlock* mMainInitBlock;
void BuildSwitchTree(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock* block, ExValue v, const SwitchNodeArray& nodes, int left, int right, int vleft, int vright, InterCodeBasicBlock* dblock); Location MapLocation(Expression * exp, InlineMapper* inlineMapper);
ExValue ToValue(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, ExValue v); void BuildSwitchTree(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock* block, InlineMapper * inlineMapper, ExValue v, const SwitchNodeArray& nodes, int left, int right, int vleft, int vright, InterCodeBasicBlock* dblock);
ExValue Dereference(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, ExValue v, int level = 0);
ExValue CoerceType(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, ExValue v, Declaration * type, bool checkTrunc = true); ExValue ToValue(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, InlineMapper* inlineMapper, ExValue v);
ExValue Dereference(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, InlineMapper* inlineMapper, ExValue v, int level = 0);
ExValue CoerceType(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, InlineMapper* inlineMapper, ExValue v, Declaration * type, bool checkTrunc = true);
ExValue TranslateExpression(Declaration * procType, InterCodeProcedure * proc, InterCodeBasicBlock*& block, Expression* exp, DestructStack*& destack, const BranchTarget & breakBlock, const BranchTarget& continueBlock, InlineMapper * inlineMapper, ExValue * lrexp = nullptr); ExValue TranslateExpression(Declaration * procType, InterCodeProcedure * proc, InterCodeBasicBlock*& block, Expression* exp, DestructStack*& destack, const BranchTarget & breakBlock, const BranchTarget& continueBlock, InlineMapper * inlineMapper, ExValue * lrexp = nullptr);
void TranslateLogic(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock* block, InterCodeBasicBlock* tblock, InterCodeBasicBlock* fblock, Expression* exp, DestructStack*& destack, InlineMapper* inlineMapper); void TranslateLogic(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock* block, InterCodeBasicBlock* tblock, InterCodeBasicBlock* fblock, Expression* exp, DestructStack*& destack, InlineMapper* inlineMapper);
ExValue TranslateInline(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock*& block, Expression* exp, const BranchTarget& breakBlock, const BranchTarget& continueBlock, InlineMapper* inlineMapper, bool inlineConstexpr, ExValue* lrexp); ExValue TranslateInline(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock*& block, Expression* exp, const BranchTarget& breakBlock, const BranchTarget& continueBlock, InlineMapper* inlineMapper, bool inlineConstexpr, ExValue* lrexp);
void CopyStruct(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, ExValue vl, ExValue vr, InlineMapper* inlineMapper, bool moving); void CopyStruct(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, ExValue vl, ExValue vr, InlineMapper* inlineMapper, bool moving);
void CopyStructSimple(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock * block, ExValue vl, ExValue vr); void CopyStructSimple(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock * block, InlineMapper* inlineMapper, ExValue vl, ExValue vr);
void StoreValue(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, ExValue vl, ExValue vr); void StoreValue(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, InlineMapper* inlineMapper, ExValue vl, ExValue vr);
void UnwindDestructStack(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock*& block, DestructStack* stack, DestructStack * bottom, InlineMapper* inlineMapper); void UnwindDestructStack(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock*& block, DestructStack* stack, DestructStack * bottom, InlineMapper* inlineMapper);
void BuildInitializer(InterCodeModule* mod, uint8 * dp, int offset, Declaration* data, InterVariable * variable); void BuildInitializer(InterCodeModule* mod, uint8 * dp, int offset, Declaration* data, InterVariable * variable);

View File

@ -14159,6 +14159,15 @@ bool NativeCodeBasicBlock::ForwardZpYIndex(bool full)
yoffset = 0; yoffset = 0;
ypred = i; ypred = i;
} }
else if (mIns[i].mType == ASMIT_INC && mIns[i].mMode == ASMIM_ZERO_PAGE && mIns[i].mAddress == yreg && yoffset == 1 && !(mIns[i].mLive & LIVE_CPU_REG_Z))
{
mIns[i].mType = ASMIT_STY;
for (int j = ypred; j < i; j++)
mIns[j].mLive |= LIVE_CPU_REG_Y;
yoffset = 0;
ypred = i;
changed = true;
}
#if 1 #if 1
else if (mIns[i].mType == ASMIT_INC && mIns[i].mMode == ASMIM_ZERO_PAGE && mIns[i].mAddress == yreg && yoffset == 0 && mIns[ypred].mType == ASMIT_STY && !(mIns[i].mLive & LIVE_CPU_REG_Y)) else if (mIns[i].mType == ASMIT_INC && mIns[i].mMode == ASMIM_ZERO_PAGE && mIns[i].mAddress == yreg && yoffset == 0 && mIns[ypred].mType == ASMIT_STY && !(mIns[i].mLive & LIVE_CPU_REG_Y))
{ {

View File

@ -3279,6 +3279,7 @@ Expression* Parser::CleanupExpression(Expression* exp)
Expression* cexp = new Expression(exp->mLocation, EX_CLEANUP); Expression* cexp = new Expression(exp->mLocation, EX_CLEANUP);
cexp->mLeft = exp; cexp->mLeft = exp;
cexp->mRight = xexp; cexp->mRight = xexp;
cexp->mDecType = exp->mDecType;
FreeTempVarExp(xexp); FreeTempVarExp(xexp);
return cexp; return cexp;