Add warning/error for unassigned variable access

This commit is contained in:
drmortalwombat 2022-10-16 13:33:20 +02:00
parent 8ee390a532
commit 4ff762b711
8 changed files with 409 additions and 458 deletions

View File

@ -29,7 +29,7 @@ Compiler::Compiler(void)
mByteCodeGenerator = new ByteCodeGenerator(mErrors, mLinker); mByteCodeGenerator = new ByteCodeGenerator(mErrors, mLinker);
mInterCodeGenerator = new InterCodeGenerator(mErrors, mLinker); mInterCodeGenerator = new InterCodeGenerator(mErrors, mLinker);
mNativeCodeGenerator = new NativeCodeGenerator(mErrors, mLinker, mCompilationUnits->mSectionCode); mNativeCodeGenerator = new NativeCodeGenerator(mErrors, mLinker, mCompilationUnits->mSectionCode);
mInterCodeModule = new InterCodeModule(mLinker); mInterCodeModule = new InterCodeModule(mErrors, mLinker);
mGlobalAnalyzer = new GlobalAnalyzer(mErrors, mLinker); mGlobalAnalyzer = new GlobalAnalyzer(mErrors, mLinker);
} }

View File

@ -24,6 +24,7 @@ enum ErrorID
EWARN_BOOL_SHORTCUT, EWARN_BOOL_SHORTCUT,
EWARN_OPTIMIZER_LOCKED, EWARN_OPTIMIZER_LOCKED,
EWARN_LOOP_UNROLL_IGNORED, EWARN_LOOP_UNROLL_IGNORED,
EWARN_USE_OF_UNINITIALIZED_VARIABLE,
EERR_GENERIC = 3000, EERR_GENERIC = 3000,
EERR_FILE_NOT_FOUND, EERR_FILE_NOT_FOUND,
@ -63,6 +64,8 @@ enum ErrorID
ERRR_INTERRUPT_TO_COMPLEX, ERRR_INTERRUPT_TO_COMPLEX,
ERRR_INVALID_STORAGE_TYPE, ERRR_INVALID_STORAGE_TYPE,
ERRR_SEMICOLON_EXPECTED, ERRR_SEMICOLON_EXPECTED,
ERRR_USE_OF_UNINITIALIZED_VARIABLE,
EERR_INVALID_PREPROCESSOR, EERR_INVALID_PREPROCESSOR,
}; };

View File

@ -1407,7 +1407,7 @@ bool InterInstruction::ReferencesTemp(int temp) const
InterInstruction* InterInstruction::Clone(void) const InterInstruction* InterInstruction::Clone(void) const
{ {
InterInstruction* ins = new InterInstruction(); InterInstruction* ins = new InterInstruction(mLocation, mCode);
*ins = *this; *ins = *this;
return ins; return ins;
} }
@ -2504,9 +2504,9 @@ bool InterOperand::IsEqual(const InterOperand& op) const
return true; return true;
} }
InterInstruction::InterInstruction(void) InterInstruction::InterInstruction(const Location& loc, InterCode code)
: mLocation(loc), mCode(code)
{ {
mCode = IC_NONE;
mOperator = IA_NONE; mOperator = IA_NONE;
mNumOperands = 3; mNumOperands = 3;
@ -2517,12 +2517,6 @@ InterInstruction::InterInstruction(void)
mSingleAssignment = false; mSingleAssignment = false;
} }
void InterInstruction::SetCode(const Location& loc, InterCode code)
{
this->mCode = code;
this->mLocation = loc;
}
static bool TypeInteger(InterType t) static bool TypeInteger(InterType t)
{ {
return t == IT_INT8 || t == IT_INT16 || t == IT_INT32 || t == IT_BOOL || t == IT_POINTER; return t == IT_INT8 || t == IT_INT16 || t == IT_INT32 || t == IT_BOOL || t == IT_POINTER;
@ -6445,8 +6439,10 @@ void InterCodeBasicBlock::BuildLocalTempSets(int num)
mEntryRequiredTemps = NumberSet(num); mEntryRequiredTemps = NumberSet(num);
mEntryProvidedTemps = NumberSet(num); mEntryProvidedTemps = NumberSet(num);
mEntryPotentialTemps = NumberSet(num);
mExitRequiredTemps = NumberSet(num); mExitRequiredTemps = NumberSet(num);
mExitProvidedTemps = NumberSet(num); mExitProvidedTemps = NumberSet(num);
mExitPotentialTemps = NumberSet(num);
for (i = 0; i < mInstructions.Size(); i++) for (i = 0; i < mInstructions.Size(); i++)
{ {
@ -6455,36 +6451,49 @@ void InterCodeBasicBlock::BuildLocalTempSets(int num)
mEntryRequiredTemps = mLocalRequiredTemps; mEntryRequiredTemps = mLocalRequiredTemps;
mExitProvidedTemps = mLocalProvidedTemps; mExitProvidedTemps = mLocalProvidedTemps;
mExitPotentialTemps = mLocalProvidedTemps;
if (mTrueJump) mTrueJump->BuildLocalTempSets(num); if (mTrueJump) mTrueJump->BuildLocalTempSets(num);
if (mFalseJump) mFalseJump->BuildLocalTempSets(num); if (mFalseJump) mFalseJump->BuildLocalTempSets(num);
} }
} }
void InterCodeBasicBlock::BuildGlobalProvidedTempSet(const NumberSet & fromProvidedTemps) void InterCodeBasicBlock::BuildGlobalProvidedTempSet(const NumberSet & fromProvidedTemps, const NumberSet& potentialProvidedTemps)
{ {
bool changed = false; bool changed = false;
if (!mVisited) if (!mVisited)
{ {
mEntryProvidedTemps = fromProvidedTemps; mEntryProvidedTemps = fromProvidedTemps;
mEntryPotentialTemps = potentialProvidedTemps;
changed = true; changed = true;
} }
else if (!(mEntryProvidedTemps <= fromProvidedTemps)) else
{
if (!(mEntryProvidedTemps <= fromProvidedTemps))
{ {
mEntryProvidedTemps &= fromProvidedTemps; mEntryProvidedTemps &= fromProvidedTemps;
changed = true; changed = true;
} }
if (!(potentialProvidedTemps <= mEntryPotentialTemps))
{
mEntryPotentialTemps |= potentialProvidedTemps;
changed = true;
}
}
if (changed) if (changed)
{ {
mExitProvidedTemps = mLocalProvidedTemps; mExitProvidedTemps = mLocalProvidedTemps;
mExitProvidedTemps |= mEntryProvidedTemps; mExitProvidedTemps |= mEntryProvidedTemps;
mExitPotentialTemps = mLocalProvidedTemps;
mExitPotentialTemps |= mEntryPotentialTemps;
mVisited = true; mVisited = true;
if (mTrueJump) mTrueJump->BuildGlobalProvidedTempSet(mExitProvidedTemps); if (mTrueJump) mTrueJump->BuildGlobalProvidedTempSet(mExitProvidedTemps, mExitPotentialTemps);
if (mFalseJump) mFalseJump->BuildGlobalProvidedTempSet(mExitProvidedTemps); if (mFalseJump) mFalseJump->BuildGlobalProvidedTempSet(mExitProvidedTemps, mExitPotentialTemps);
} }
} }
@ -7381,8 +7390,7 @@ bool InterCodeBasicBlock::SimplifyIntegerNumeric(const GrowingInstructionPtrArra
if (spareTemps + 2 >= ltvalue.Size()) if (spareTemps + 2 >= ltvalue.Size())
return true; return true;
InterInstruction* cins = new InterInstruction(); InterInstruction* cins = new InterInstruction(ins->mLocation, IC_CONVERSION_OPERATOR);
cins->mCode = IC_CONVERSION_OPERATOR;
cins->mOperator = IA_EXT8TO16U; cins->mOperator = IA_EXT8TO16U;
cins->mSrc[0] = ains->mSrc[1]; cins->mSrc[0] = ains->mSrc[1];
cins->mDst.mTemp = spareTemps++; cins->mDst.mTemp = spareTemps++;
@ -7391,8 +7399,7 @@ bool InterCodeBasicBlock::SimplifyIntegerNumeric(const GrowingInstructionPtrArra
cins->mDst.mRange.LimitMin(0); cins->mDst.mRange.LimitMin(0);
mInstructions.Insert(i, cins); mInstructions.Insert(i, cins);
InterInstruction* nins = new InterInstruction(); InterInstruction* nins = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR);
nins->mCode = IC_BINARY_OPERATOR;
nins->mOperator = IA_SHL; nins->mOperator = IA_SHL;
nins->mSrc[0] = ins->mSrc[0]; nins->mSrc[0] = ins->mSrc[0];
nins->mSrc[1] = cins->mDst; nins->mSrc[1] = cins->mDst;
@ -7562,8 +7569,7 @@ bool InterCodeBasicBlock::SimplifyIntegerNumeric(const GrowingInstructionPtrArra
if (spareTemps + 2 >= ltvalue.Size()) if (spareTemps + 2 >= ltvalue.Size())
return true; return true;
InterInstruction* nins = new InterInstruction(); InterInstruction* nins = new InterInstruction(ins->mLocation, IC_CONVERSION_OPERATOR);
nins->mCode = IC_CONVERSION_OPERATOR;
nins->mOperator = IA_EXT8TO16U; nins->mOperator = IA_EXT8TO16U;
nins->mSrc[0] = ains->mSrc[1]; nins->mSrc[0] = ains->mSrc[1];
nins->mDst.mTemp = spareTemps++; nins->mDst.mTemp = spareTemps++;
@ -7655,8 +7661,7 @@ bool InterCodeBasicBlock::SimplifyIntegerNumeric(const GrowingInstructionPtrArra
if (spareTemps + 2 >= ltvalue.Size()) if (spareTemps + 2 >= ltvalue.Size())
return true; return true;
InterInstruction* nins = new InterInstruction(); InterInstruction* nins = new InterInstruction(ins->mLocation, IC_LEA);
nins->mCode = IC_LEA;
nins->mSrc[0].Forward(pins->mSrc[0]); nins->mSrc[0].Forward(pins->mSrc[0]);
nins->mSrc[1].ForwardMem(pins->mSrc[1]); nins->mSrc[1].ForwardMem(pins->mSrc[1]);
nins->mSrc[1].mIntConst += ins->mSrc[1].mIntConst; nins->mSrc[1].mIntConst += ins->mSrc[1].mIntConst;
@ -7704,8 +7709,7 @@ bool InterCodeBasicBlock::SimplifyIntegerNumeric(const GrowingInstructionPtrArra
if (spareTemps + 2 >= ltvalue.Size()) if (spareTemps + 2 >= ltvalue.Size())
return true; return true;
InterInstruction* nins = new InterInstruction(); InterInstruction* nins = new InterInstruction(ins->mLocation, IC_LEA);
nins->mCode = IC_LEA;
nins->mSrc[0].Forward(pins->mSrc[0]); nins->mSrc[0].Forward(pins->mSrc[0]);
nins->mSrc[1].ForwardMem(pins->mSrc[1]); nins->mSrc[1].ForwardMem(pins->mSrc[1]);
nins->mSrc[1].mIntConst += ins->mSrc[0].mIntConst; nins->mSrc[1].mIntConst += ins->mSrc[0].mIntConst;
@ -7842,8 +7846,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra
InterInstruction* ai0 = ltvalue[mi0->mSrc[0].mTemp], * ai1 = ltvalue[mi0->mSrc[1].mTemp]; InterInstruction* ai0 = ltvalue[mi0->mSrc[0].mTemp], * ai1 = ltvalue[mi0->mSrc[1].mTemp];
if (ai0 && ai0->mCode == IC_CONSTANT) if (ai0 && ai0->mCode == IC_CONSTANT)
{ {
InterInstruction* nai = new InterInstruction(); InterInstruction* nai = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR);
nai->mCode = IC_BINARY_OPERATOR;
nai->mOperator = IA_MUL; nai->mOperator = IA_MUL;
nai->mSrc[0].mTemp = mi0->mSrc[1].mTemp; nai->mSrc[0].mTemp = mi0->mSrc[1].mTemp;
nai->mSrc[0].mType = IT_INT16; nai->mSrc[0].mType = IT_INT16;
@ -7855,8 +7858,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra
ltvalue[nai->mDst.mTemp] = nullptr; ltvalue[nai->mDst.mTemp] = nullptr;
InterInstruction* cai = new InterInstruction(); InterInstruction* cai = new InterInstruction(ins->mLocation, IC_CONSTANT);
cai->mCode = IC_CONSTANT;
cai->mDst.mTemp = spareTemps++; cai->mDst.mTemp = spareTemps++;
cai->mDst.mType = IT_INT16; cai->mDst.mType = IT_INT16;
cai->mConst.mIntConst = ai0->mConst.mIntConst * mi1->mConst.mIntConst; cai->mConst.mIntConst = ai0->mConst.mIntConst * mi1->mConst.mIntConst;
@ -7870,8 +7872,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra
} }
else if (ai1 && ai1->mCode == IC_CONSTANT) else if (ai1 && ai1->mCode == IC_CONSTANT)
{ {
InterInstruction* nai = new InterInstruction(); InterInstruction* nai = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR);
nai->mCode = IC_BINARY_OPERATOR;
nai->mOperator = IA_MUL; nai->mOperator = IA_MUL;
nai->mSrc[0].mTemp = mi0->mSrc[0].mTemp; nai->mSrc[0].mTemp = mi0->mSrc[0].mTemp;
nai->mSrc[0].mType = IT_INT16; nai->mSrc[0].mType = IT_INT16;
@ -7883,8 +7884,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra
ltvalue[nai->mDst.mTemp] = nullptr; ltvalue[nai->mDst.mTemp] = nullptr;
InterInstruction* cai = new InterInstruction(); InterInstruction* cai = new InterInstruction(ins->mLocation, IC_CONSTANT);
cai->mCode = IC_CONSTANT;
cai->mDst.mTemp = spareTemps++; cai->mDst.mTemp = spareTemps++;
cai->mDst.mType = IT_INT16; cai->mDst.mType = IT_INT16;
cai->mConst.mIntConst = ai1->mConst.mIntConst * mi1->mConst.mIntConst; cai->mConst.mIntConst = ai1->mConst.mIntConst * mi1->mConst.mIntConst;
@ -7902,8 +7902,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra
InterInstruction* ai0 = ltvalue[mi1->mSrc[0].mTemp], * ai1 = ltvalue[mi1->mSrc[1].mTemp]; InterInstruction* ai0 = ltvalue[mi1->mSrc[0].mTemp], * ai1 = ltvalue[mi1->mSrc[1].mTemp];
if (ai0 && ai0->mCode == IC_CONSTANT) if (ai0 && ai0->mCode == IC_CONSTANT)
{ {
InterInstruction* nai = new InterInstruction(); InterInstruction* nai = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR);
nai->mCode = IC_BINARY_OPERATOR;
nai->mOperator = IA_MUL; nai->mOperator = IA_MUL;
nai->mSrc[0].mTemp = mi1->mSrc[1].mTemp; nai->mSrc[0].mTemp = mi1->mSrc[1].mTemp;
nai->mSrc[0].mType = IT_INT16; nai->mSrc[0].mType = IT_INT16;
@ -7915,8 +7914,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra
ltvalue[nai->mDst.mTemp] = nullptr; ltvalue[nai->mDst.mTemp] = nullptr;
InterInstruction* cai = new InterInstruction(); InterInstruction* cai = new InterInstruction(ins->mLocation, IC_CONSTANT);
cai->mCode = IC_CONSTANT;
cai->mDst.mTemp = spareTemps++; cai->mDst.mTemp = spareTemps++;
cai->mDst.mType = IT_INT16; cai->mDst.mType = IT_INT16;
cai->mConst.mIntConst = ai0->mConst.mIntConst * mi0->mConst.mIntConst; cai->mConst.mIntConst = ai0->mConst.mIntConst * mi0->mConst.mIntConst;
@ -7930,8 +7928,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra
} }
else if (ai1 && ai1->mCode == IC_CONSTANT) else if (ai1 && ai1->mCode == IC_CONSTANT)
{ {
InterInstruction* nai = new InterInstruction(); InterInstruction* nai = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR);
nai->mCode = IC_BINARY_OPERATOR;
nai->mOperator = IA_MUL; nai->mOperator = IA_MUL;
nai->mSrc[0].mTemp = mi1->mSrc[0].mTemp; nai->mSrc[0].mTemp = mi1->mSrc[0].mTemp;
nai->mSrc[0].mType = IT_INT16; nai->mSrc[0].mType = IT_INT16;
@ -7943,8 +7940,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra
ltvalue[nai->mDst.mTemp] = nullptr; ltvalue[nai->mDst.mTemp] = nullptr;
InterInstruction* cai = new InterInstruction(); InterInstruction* cai = new InterInstruction(ins->mLocation, IC_CONSTANT);
cai->mCode = IC_CONSTANT;
cai->mDst.mTemp = spareTemps++; cai->mDst.mTemp = spareTemps++;
cai->mDst.mType = IT_INT16; cai->mDst.mType = IT_INT16;
cai->mConst.mIntConst = ai1->mConst.mIntConst * mi0->mConst.mIntConst; cai->mConst.mIntConst = ai1->mConst.mIntConst * mi0->mConst.mIntConst;
@ -7963,8 +7959,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra
InterInstruction* ai0 = ltvalue[mi1->mSrc[0].mTemp], * ai1 = ltvalue[mi1->mSrc[1].mTemp]; InterInstruction* ai0 = ltvalue[mi1->mSrc[0].mTemp], * ai1 = ltvalue[mi1->mSrc[1].mTemp];
if (ai0 && ai0->mCode == IC_CONSTANT) if (ai0 && ai0->mCode == IC_CONSTANT)
{ {
InterInstruction* nai = new InterInstruction(); InterInstruction* nai = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR);
nai->mCode = IC_BINARY_OPERATOR;
nai->mOperator = IA_MUL; nai->mOperator = IA_MUL;
nai->mSrc[0].mTemp = mi1->mSrc[1].mTemp; nai->mSrc[0].mTemp = mi1->mSrc[1].mTemp;
nai->mSrc[0].mType = IT_INT16; nai->mSrc[0].mType = IT_INT16;
@ -7976,8 +7971,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra
ltvalue[nai->mDst.mTemp] = nullptr; ltvalue[nai->mDst.mTemp] = nullptr;
InterInstruction* cai = new InterInstruction(); InterInstruction* cai = new InterInstruction(ins->mLocation, IC_CONSTANT);
cai->mCode = IC_CONSTANT;
cai->mDst.mTemp = spareTemps++; cai->mDst.mTemp = spareTemps++;
cai->mDst.mType = IT_INT16; cai->mDst.mType = IT_INT16;
cai->mConst.mIntConst = ai0->mConst.mIntConst * mi0->mConst.mIntConst; cai->mConst.mIntConst = ai0->mConst.mIntConst * mi0->mConst.mIntConst;
@ -8007,8 +8001,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra
{ {
if (ai0 && ai0->mCode == IC_CONSTANT) if (ai0 && ai0->mCode == IC_CONSTANT)
{ {
InterInstruction* cai = new InterInstruction(); InterInstruction* cai = new InterInstruction(ins->mLocation, IC_CONSTANT);
cai->mCode = IC_CONSTANT;
cai->mDst.mTemp = spareTemps++; cai->mDst.mTemp = spareTemps++;
cai->mDst.mType = IT_INT16; cai->mDst.mType = IT_INT16;
cai->mConst.mIntConst = ai0->mConst.mIntConst + mi1->mConst.mIntConst; cai->mConst.mIntConst = ai0->mConst.mIntConst + mi1->mConst.mIntConst;
@ -8021,8 +8014,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra
} }
else if (ai1 && ai1->mCode == IC_CONSTANT) else if (ai1 && ai1->mCode == IC_CONSTANT)
{ {
InterInstruction* cai = new InterInstruction(); InterInstruction* cai = new InterInstruction(ins->mLocation, IC_CONSTANT);
cai->mCode = IC_CONSTANT;
cai->mDst.mTemp = spareTemps++; cai->mDst.mTemp = spareTemps++;
cai->mDst.mType = IT_INT16; cai->mDst.mType = IT_INT16;
cai->mConst.mIntConst = ai1->mConst.mIntConst + mi1->mConst.mIntConst; cai->mConst.mIntConst = ai1->mConst.mIntConst + mi1->mConst.mIntConst;
@ -8042,8 +8034,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra
{ {
if (ai0 && ai0->mCode == IC_CONSTANT) if (ai0 && ai0->mCode == IC_CONSTANT)
{ {
InterInstruction* cai = new InterInstruction(); InterInstruction* cai = new InterInstruction(ins->mLocation, IC_CONSTANT);
cai->mCode = IC_CONSTANT;
cai->mDst.mTemp = spareTemps++; cai->mDst.mTemp = spareTemps++;
cai->mDst.mType = IT_INT16; cai->mDst.mType = IT_INT16;
cai->mConst.mIntConst = ai0->mConst.mIntConst + mi0->mConst.mIntConst; cai->mConst.mIntConst = ai0->mConst.mIntConst + mi0->mConst.mIntConst;
@ -8056,8 +8047,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra
} }
else if (ai1 && ai1->mCode == IC_CONSTANT) else if (ai1 && ai1->mCode == IC_CONSTANT)
{ {
InterInstruction* cai = new InterInstruction(); InterInstruction* cai = new InterInstruction(ins->mLocation, IC_CONSTANT);
cai->mCode = IC_CONSTANT;
cai->mDst.mTemp = spareTemps++; cai->mDst.mTemp = spareTemps++;
cai->mDst.mType = IT_INT16; cai->mDst.mType = IT_INT16;
cai->mConst.mIntConst = ai1->mConst.mIntConst + mi0->mConst.mIntConst; cai->mConst.mIntConst = ai1->mConst.mIntConst + mi0->mConst.mIntConst;
@ -8084,8 +8074,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra
InterInstruction* ai0 = ltvalue[li0->mSrc[0].mTemp], * ai1 = ltvalue[li0->mSrc[1].mTemp]; InterInstruction* ai0 = ltvalue[li0->mSrc[0].mTemp], * ai1 = ltvalue[li0->mSrc[1].mTemp];
if (ai0 && ai1 && ai0->mCode == IC_CONSTANT && ai0->mConst.mIntConst >= 0) if (ai0 && ai1 && ai0->mCode == IC_CONSTANT && ai0->mConst.mIntConst >= 0)
{ {
InterInstruction* nai = new InterInstruction(); InterInstruction* nai = new InterInstruction(ins->mLocation, IC_LEA);
nai->mCode = IC_LEA;
nai->mSrc[1].mMemory = IM_INDIRECT; nai->mSrc[1].mMemory = IM_INDIRECT;
nai->mSrc[0].mTemp = li0->mSrc[1].mTemp; nai->mSrc[0].mTemp = li0->mSrc[1].mTemp;
nai->mSrc[0].mType = IT_INT16; nai->mSrc[0].mType = IT_INT16;
@ -8109,8 +8098,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra
InterInstruction* ai0 = ltvalue[li1->mSrc[0].mTemp], * ai1 = ltvalue[li1->mSrc[1].mTemp]; InterInstruction* ai0 = ltvalue[li1->mSrc[0].mTemp], * ai1 = ltvalue[li1->mSrc[1].mTemp];
if (ai0 && ai1 && ai0->mCode == IC_CONSTANT && ai0->mConst.mIntConst >= 0) if (ai0 && ai1 && ai0->mCode == IC_CONSTANT && ai0->mConst.mIntConst >= 0)
{ {
InterInstruction* cai = new InterInstruction(); InterInstruction* cai = new InterInstruction(ins->mLocation, IC_CONSTANT);
cai->mCode = IC_CONSTANT;
cai->mDst.mTemp = spareTemps++; cai->mDst.mTemp = spareTemps++;
cai->mDst.mType = IT_INT16; cai->mDst.mType = IT_INT16;
cai->mConst.mIntConst = ai0->mConst.mIntConst + li0->mConst.mIntConst; cai->mConst.mIntConst = ai0->mConst.mIntConst + li0->mConst.mIntConst;
@ -8832,8 +8820,7 @@ bool InterCodeBasicBlock::MergeCommonPathInstructions(void)
{ {
if (fins->mDst.mTemp != tins->mDst.mTemp) if (fins->mDst.mTemp != tins->mDst.mTemp)
{ {
InterInstruction* nins = new InterInstruction(); InterInstruction* nins = new InterInstruction(tins->mLocation, IC_LOAD_TEMPORARY);
nins->mCode = IC_LOAD_TEMPORARY;
nins->mDst.mTemp = fins->mDst.mTemp; nins->mDst.mTemp = fins->mDst.mTemp;
nins->mDst.mType = fins->mDst.mType; nins->mDst.mType = fins->mDst.mType;
nins->mSrc[0].mTemp = tins->mDst.mTemp; nins->mSrc[0].mTemp = tins->mDst.mTemp;
@ -9105,8 +9092,7 @@ bool InterCodeBasicBlock::ForwardDiamondMovedTemp(void)
{ {
tblock->mInstructions[j]->mDst.mTemp = stemp; tblock->mInstructions[j]->mDst.mTemp = stemp;
InterInstruction* nins = new InterInstruction(); InterInstruction* nins = new InterInstruction(mins->mLocation, IC_LOAD_TEMPORARY);
nins->mCode = IC_LOAD_TEMPORARY;
nins->mDst.mTemp = ttemp; nins->mDst.mTemp = ttemp;
nins->mDst.mType = mins->mDst.mType; nins->mDst.mType = mins->mDst.mType;
nins->mSrc[0].mTemp = stemp; nins->mSrc[0].mTemp = stemp;
@ -9483,49 +9469,46 @@ void InterCodeBasicBlock::ExpandSelect(InterCodeProcedure* proc)
mInstructions.SetSize(i); mInstructions.SetSize(i);
InterInstruction* bins = new InterInstruction(); InterInstruction* bins = new InterInstruction(sins->mLocation, IC_BRANCH);
bins->mCode = IC_BRANCH;
bins->mSrc[0] = sins->mSrc[2]; bins->mSrc[0] = sins->mSrc[2];
mInstructions.Push(bins); mInstructions.Push(bins);
InterInstruction* tins = new InterInstruction(); InterInstruction* tins;
if (sins->mSrc[1].mTemp < 0) if (sins->mSrc[1].mTemp < 0)
{ {
tins->mCode = IC_CONSTANT; tins = new InterInstruction(sins->mLocation, IC_CONSTANT);
tins->mConst = sins->mSrc[1]; tins->mConst = sins->mSrc[1];
} }
else else
{ {
tins->mCode = IC_LOAD_TEMPORARY; tins = new InterInstruction(sins->mLocation, IC_LOAD_TEMPORARY);
tins->mSrc[0] = sins->mSrc[1]; tins->mSrc[0] = sins->mSrc[1];
} }
tins->mDst = sins->mDst; tins->mDst = sins->mDst;
InterInstruction* fins = new InterInstruction(); InterInstruction* fins;
if (sins->mSrc[0].mTemp < 0) if (sins->mSrc[0].mTemp < 0)
{ {
fins->mCode = IC_CONSTANT; fins = new InterInstruction(sins->mLocation, IC_CONSTANT);
fins->mConst = sins->mSrc[0]; fins->mConst = sins->mSrc[0];
} }
else else
{ {
fins->mCode = IC_LOAD_TEMPORARY; fins = new InterInstruction(sins->mLocation, IC_LOAD_TEMPORARY);
fins->mSrc[0] = sins->mSrc[0]; fins->mSrc[0] = sins->mSrc[0];
} }
fins->mDst = sins->mDst; fins->mDst = sins->mDst;
tblock->mInstructions.Push(tins); tblock->mInstructions.Push(tins);
InterInstruction* jins = new InterInstruction(); InterInstruction* jins = new InterInstruction(sins->mLocation, IC_JUMP);
jins->mCode = IC_JUMP;
tblock->mInstructions.Push(jins); tblock->mInstructions.Push(jins);
tblock->Close(eblock, nullptr); tblock->Close(eblock, nullptr);
fblock->mInstructions.Push(fins); fblock->mInstructions.Push(fins);
jins = new InterInstruction(); jins = new InterInstruction(sins->mLocation, IC_JUMP);
jins->mCode = IC_JUMP;
fblock->mInstructions.Push(jins); fblock->mInstructions.Push(jins);
fblock->Close(eblock, nullptr); fblock->Close(eblock, nullptr);
@ -9550,6 +9533,8 @@ void InterCodeBasicBlock::SplitBranches(InterCodeProcedure* proc)
if (mTrueJump && mFalseJump && (mInstructions.Size() > 2 || mInstructions.Size() == 2 && mInstructions[0]->mCode != IC_RELATIONAL_OPERATOR)) if (mTrueJump && mFalseJump && (mInstructions.Size() > 2 || mInstructions.Size() == 2 && mInstructions[0]->mCode != IC_RELATIONAL_OPERATOR))
{ {
InterCodeBasicBlock* block = new InterCodeBasicBlock(); InterCodeBasicBlock* block = new InterCodeBasicBlock();
InterInstruction* ins = mInstructions.Last();
proc->Append(block); proc->Append(block);
if (mInstructions[mInstructions.Size() - 2]->mCode == IC_RELATIONAL_OPERATOR) if (mInstructions[mInstructions.Size() - 2]->mCode == IC_RELATIONAL_OPERATOR)
{ {
@ -9562,8 +9547,7 @@ void InterCodeBasicBlock::SplitBranches(InterCodeProcedure* proc)
block->mInstructions.Push(mInstructions.Pop()); block->mInstructions.Push(mInstructions.Pop());
} }
InterInstruction* jins = new InterInstruction(); InterInstruction* jins = new InterInstruction(ins->mLocation, IC_JUMP);
jins->mCode = IC_JUMP;
mInstructions.Push(jins); mInstructions.Push(jins);
block->Close(mTrueJump, mFalseJump); block->Close(mTrueJump, mFalseJump);
mTrueJump = block; mTrueJump = block;
@ -9848,8 +9832,7 @@ void InterCodeBasicBlock::BuildLoopSuffix(InterCodeProcedure* proc)
{ {
InterCodeBasicBlock* suffix = new InterCodeBasicBlock(); InterCodeBasicBlock* suffix = new InterCodeBasicBlock();
proc->Append(suffix); proc->Append(suffix);
InterInstruction* jins = new InterInstruction(); InterInstruction* jins = new InterInstruction(mInstructions[0]->mLocation, IC_JUMP);
jins->mCode = IC_JUMP;
suffix->Append(jins); suffix->Append(jins);
suffix->Close(mFalseJump, nullptr); suffix->Close(mFalseJump, nullptr);
mFalseJump = suffix; mFalseJump = suffix;
@ -9862,8 +9845,7 @@ void InterCodeBasicBlock::BuildLoopSuffix(InterCodeProcedure* proc)
{ {
InterCodeBasicBlock* suffix = new InterCodeBasicBlock(); InterCodeBasicBlock* suffix = new InterCodeBasicBlock();
proc->Append(suffix); proc->Append(suffix);
InterInstruction* jins = new InterInstruction(); InterInstruction* jins = new InterInstruction(mInstructions[0]->mLocation, IC_JUMP);
jins->mCode = IC_JUMP;
suffix->Append(jins); suffix->Append(jins);
suffix->Close(mTrueJump, nullptr); suffix->Close(mTrueJump, nullptr);
mTrueJump = suffix; mTrueJump = suffix;
@ -9894,8 +9876,7 @@ InterCodeBasicBlock* InterCodeBasicBlock::BuildLoopPrefix(InterCodeProcedure* pr
{ {
mLoopPrefix = new InterCodeBasicBlock(); mLoopPrefix = new InterCodeBasicBlock();
proc->Append(mLoopPrefix); proc->Append(mLoopPrefix);
InterInstruction* jins = new InterInstruction(); InterInstruction* jins = new InterInstruction(mInstructions[0]->mLocation, IC_JUMP);
jins->mCode = IC_JUMP;
mLoopPrefix->Append(jins); mLoopPrefix->Append(jins);
mLoopPrefix->Close(this, nullptr); mLoopPrefix->Close(this, nullptr);
} }
@ -10314,8 +10295,7 @@ void InterCodeBasicBlock::SingleBlockLoopUnrolling(void)
mTrueJump = mFalseJump; mTrueJump = mFalseJump;
mFalseJump = nullptr; mFalseJump = nullptr;
InterInstruction* jins = new InterInstruction(); InterInstruction* jins = new InterInstruction(mInstructions[0]->mLocation, IC_JUMP);
jins->mCode = IC_JUMP;
mInstructions.Push(jins); mInstructions.Push(jins);
} }
} }
@ -10384,8 +10364,7 @@ bool InterCodeBasicBlock::SingleBlockLoopPointerSplit(int& spareTemps)
return true; return true;
InterInstruction* pins = tvalues[lins->mSrc[1].mTemp]; InterInstruction* pins = tvalues[lins->mSrc[1].mTemp];
InterInstruction* nins = new InterInstruction(); InterInstruction* nins = new InterInstruction(lins->mLocation, IC_LEA);
nins->mCode = IC_LEA;
nins->mSrc[1] = pins->mSrc[1]; nins->mSrc[1] = pins->mSrc[1];
nins->mSrc[0].mTemp = -1; nins->mSrc[0].mTemp = -1;
nins->mSrc[0].mType = IT_INT16; nins->mSrc[0].mType = IT_INT16;
@ -10487,9 +10466,8 @@ void InterCodeBasicBlock::SingleBlockLoopOptimisation(const NumberSet& aliasedPa
// Move load before loop // Move load before loop
mLoopPrefix->mInstructions.Insert(mLoopPrefix->mInstructions.Size() - 1, ins); mLoopPrefix->mInstructions.Insert(mLoopPrefix->mInstructions.Size() - 1, ins);
InterInstruction* nins = new InterInstruction(); InterInstruction* nins = new InterInstruction(sins->mLocation, IC_LOAD_TEMPORARY);
mInstructions[i] = nins; mInstructions[i] = nins;
nins->mCode = IC_LOAD_TEMPORARY;
nins->mDst.Forward(ins->mDst); nins->mDst.Forward(ins->mDst);
nins->mSrc[0].Forward(sins->mSrc[0]); nins->mSrc[0].Forward(sins->mSrc[0]);
ins->mDst.Forward(sins->mSrc[0]); ins->mDst.Forward(sins->mSrc[0]);
@ -10777,16 +10755,14 @@ void InterCodeBasicBlock::SingleBlockLoopOptimisation(const NumberSet& aliasedPa
indexStep[ins->mDst.mTemp] = ins->mSrc[1].mIntConst * indexStep[ins->mSrc[0].mTemp]; indexStep[ins->mDst.mTemp] = ins->mSrc[1].mIntConst * indexStep[ins->mSrc[0].mTemp];
indexBase[ins->mDst.mTemp] = ins->mSrc[1].mIntConst * indexBase[ins->mSrc[0].mTemp]; indexBase[ins->mDst.mTemp] = ins->mSrc[1].mIntConst * indexBase[ins->mSrc[0].mTemp];
InterInstruction* bins = new InterInstruction(); InterInstruction* bins = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR);
bins->mCode = IC_BINARY_OPERATOR;
bins->mOperator = IA_MUL; bins->mOperator = IA_MUL;
bins->mDst = ins->mDst; bins->mDst = ins->mDst;
bins->mSrc[0] = ins->mSrc[0]; bins->mSrc[0] = ins->mSrc[0];
bins->mSrc[1] = ins->mSrc[1]; bins->mSrc[1] = ins->mSrc[1];
mLoopPrefix->mInstructions.Insert(mLoopPrefix->mInstructions.Size() - 1, bins); mLoopPrefix->mInstructions.Insert(mLoopPrefix->mInstructions.Size() - 1, bins);
InterInstruction* ains = new InterInstruction(); InterInstruction* ains = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR);
ains->mCode = IC_BINARY_OPERATOR;
ains->mOperator = IA_ADD; ains->mOperator = IA_ADD;
ains->mDst = ins->mDst; ains->mDst = ins->mDst;
ains->mSrc[0] = ins->mDst; ains->mSrc[0] = ins->mDst;
@ -10804,16 +10780,14 @@ void InterCodeBasicBlock::SingleBlockLoopOptimisation(const NumberSet& aliasedPa
indexStep[ins->mDst.mTemp] = ins->mSrc[0].mIntConst * indexStep[ins->mSrc[1].mTemp]; indexStep[ins->mDst.mTemp] = ins->mSrc[0].mIntConst * indexStep[ins->mSrc[1].mTemp];
indexBase[ins->mDst.mTemp] = ins->mSrc[0].mIntConst * indexBase[ins->mSrc[1].mTemp]; indexBase[ins->mDst.mTemp] = ins->mSrc[0].mIntConst * indexBase[ins->mSrc[1].mTemp];
InterInstruction* bins = new InterInstruction(); InterInstruction* bins = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR);
bins->mCode = IC_BINARY_OPERATOR;
bins->mOperator = IA_MUL; bins->mOperator = IA_MUL;
bins->mDst = ins->mDst; bins->mDst = ins->mDst;
bins->mSrc[0] = ins->mSrc[0]; bins->mSrc[0] = ins->mSrc[0];
bins->mSrc[1] = ins->mSrc[1]; bins->mSrc[1] = ins->mSrc[1];
mLoopPrefix->mInstructions.Insert(mLoopPrefix->mInstructions.Size() - 1, bins); mLoopPrefix->mInstructions.Insert(mLoopPrefix->mInstructions.Size() - 1, bins);
InterInstruction* ains = new InterInstruction(); InterInstruction* ains = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR);
ains->mCode = IC_BINARY_OPERATOR;
ains->mOperator = IA_ADD; ains->mOperator = IA_ADD;
ains->mDst = ins->mDst; ains->mDst = ins->mDst;
ains->mSrc[1] = ins->mDst; ains->mSrc[1] = ins->mDst;
@ -10831,16 +10805,14 @@ void InterCodeBasicBlock::SingleBlockLoopOptimisation(const NumberSet& aliasedPa
indexStep[ins->mDst.mTemp] = indexStep[ins->mSrc[1].mTemp] << ins->mSrc[0].mIntConst; indexStep[ins->mDst.mTemp] = indexStep[ins->mSrc[1].mTemp] << ins->mSrc[0].mIntConst;
indexBase[ins->mDst.mTemp] = indexBase[ins->mSrc[1].mTemp] << ins->mSrc[0].mIntConst; indexBase[ins->mDst.mTemp] = indexBase[ins->mSrc[1].mTemp] << ins->mSrc[0].mIntConst;
InterInstruction* bins = new InterInstruction(); InterInstruction* bins = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR);
bins->mCode = IC_BINARY_OPERATOR;
bins->mOperator = IA_SHL; bins->mOperator = IA_SHL;
bins->mDst = ins->mDst; bins->mDst = ins->mDst;
bins->mSrc[0] = ins->mSrc[0]; bins->mSrc[0] = ins->mSrc[0];
bins->mSrc[1] = ins->mSrc[1]; bins->mSrc[1] = ins->mSrc[1];
mLoopPrefix->mInstructions.Insert(mLoopPrefix->mInstructions.Size() - 1, bins); mLoopPrefix->mInstructions.Insert(mLoopPrefix->mInstructions.Size() - 1, bins);
InterInstruction* ains = new InterInstruction(); InterInstruction* ains = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR);
ains->mCode = IC_BINARY_OPERATOR;
ains->mOperator = IA_ADD; ains->mOperator = IA_ADD;
ains->mDst = ins->mDst; ains->mDst = ins->mDst;
ains->mSrc[1] = ins->mDst; ains->mSrc[1] = ins->mDst;
@ -10860,8 +10832,7 @@ void InterCodeBasicBlock::SingleBlockLoopOptimisation(const NumberSet& aliasedPa
mLoopPrefix->mInstructions.Insert(mLoopPrefix->mInstructions.Size() - 1, ins); mLoopPrefix->mInstructions.Insert(mLoopPrefix->mInstructions.Size() - 1, ins);
InterInstruction* ains = new InterInstruction(); InterInstruction* ains = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR);
ains->mCode = IC_BINARY_OPERATOR;
ains->mOperator = IA_ADD; ains->mOperator = IA_ADD;
ains->mDst = ins->mDst; ains->mDst = ins->mDst;
ains->mSrc[0] = ins->mDst; ains->mSrc[0] = ins->mDst;
@ -10878,8 +10849,7 @@ void InterCodeBasicBlock::SingleBlockLoopOptimisation(const NumberSet& aliasedPa
mLoopPrefix->mInstructions.Insert(mLoopPrefix->mInstructions.Size() - 1, ins); mLoopPrefix->mInstructions.Insert(mLoopPrefix->mInstructions.Size() - 1, ins);
InterInstruction* ains = new InterInstruction(); InterInstruction* ains = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR);
ains->mCode = IC_BINARY_OPERATOR;
ains->mOperator = IA_ADD; ains->mOperator = IA_ADD;
ains->mDst = ins->mDst; ains->mDst = ins->mDst;
ains->mSrc[1] = ins->mDst; ains->mSrc[1] = ins->mDst;
@ -10917,8 +10887,7 @@ void InterCodeBasicBlock::SingleBlockLoopOptimisation(const NumberSet& aliasedPa
mLoopPrefix->mInstructions.Insert(mLoopPrefix->mInstructions.Size() - 1, ins); mLoopPrefix->mInstructions.Insert(mLoopPrefix->mInstructions.Size() - 1, ins);
InterInstruction* ains = new InterInstruction(); InterInstruction* ains = new InterInstruction(ins->mLocation, IC_LEA);
ains->mCode = IC_LEA;
ains->mDst = ins->mDst; ains->mDst = ins->mDst;
ains->mSrc[1] = ins->mDst; ains->mSrc[1] = ins->mDst;
ains->mSrc[1].mMemory = IM_INDIRECT; ains->mSrc[1].mMemory = IM_INDIRECT;
@ -10929,8 +10898,7 @@ void InterCodeBasicBlock::SingleBlockLoopOptimisation(const NumberSet& aliasedPa
if (tailBlock->mEntryRequiredTemps[ains->mDst.mTemp]) if (tailBlock->mEntryRequiredTemps[ains->mDst.mTemp])
{ {
InterInstruction* dins = new InterInstruction(); InterInstruction* dins = new InterInstruction(ins->mLocation, IC_LEA);
dins->mCode = IC_LEA;
dins->mDst = ins->mDst; dins->mDst = ins->mDst;
dins->mSrc[1] = ins->mDst; dins->mSrc[1] = ins->mDst;
dins->mSrc[1].mMemory = IM_INDIRECT; dins->mSrc[1].mMemory = IM_INDIRECT;
@ -10954,8 +10922,7 @@ void InterCodeBasicBlock::SingleBlockLoopOptimisation(const NumberSet& aliasedPa
mLoopPrefix->mInstructions.Insert(mLoopPrefix->mInstructions.Size() - 1, ins); mLoopPrefix->mInstructions.Insert(mLoopPrefix->mInstructions.Size() - 1, ins);
InterInstruction* ains = new InterInstruction(); InterInstruction* ains = new InterInstruction(ins->mLocation, ins->mCode);
ains->mCode = ins->mCode;
ains->mOperator = ins->mOperator; ains->mOperator = ins->mOperator;
ains->mSrc[0] = ins->mSrc[0]; ains->mSrc[0] = ins->mSrc[0];
ains->mDst = ins->mDst; ains->mDst = ins->mDst;
@ -12137,6 +12104,70 @@ void InterCodeBasicBlock::PeepholeOptimization(const GrowingVariableArray& stati
} }
} }
void InterCodeBasicBlock::WarnUsedUndefinedVariables(InterCodeProcedure* proc)
{
if (!mVisited)
{
mVisited = true;
NumberSet providedTemps(mEntryProvidedTemps), potentialTemps(mEntryPotentialTemps);
for (int i = 0; i < mInstructions.Size(); i++)
{
InterInstruction* ins = mInstructions[i];
for (int j = 0; j < ins->mNumOperands; j++)
{
if (ins->mSrc[j].mTemp >= 0 && !providedTemps[ins->mSrc[j].mTemp])
{
int t = ins->mSrc[j].mTemp;
int k = 0;
while (k < proc->mLocalVars.Size() && !(proc->mLocalVars[k] && proc->mLocalVars[k]->mTempIndex == t))
k++;
if (potentialTemps[t])
{
if (k < proc->mLocalVars.Size() && proc->mLocalVars[k]->mIdent)
proc->mModule->mErrors->Error(ins->mLocation, EWARN_USE_OF_UNINITIALIZED_VARIABLE, "Use of potentialy uninitialized variable", proc->mLocalVars[k]->mIdent);
else
proc->mModule->mErrors->Error(ins->mLocation, EWARN_USE_OF_UNINITIALIZED_VARIABLE, "Use of potentialy uninitialized expression");
}
else
{
if (k < proc->mLocalVars.Size() && proc->mLocalVars[k]->mIdent)
proc->mModule->mErrors->Error(ins->mLocation, ERRR_USE_OF_UNINITIALIZED_VARIABLE, "Use of uninitialized variable", proc->mLocalVars[k]->mIdent);
else
proc->mModule->mErrors->Error(ins->mLocation, ERRR_USE_OF_UNINITIALIZED_VARIABLE, "Use of uninitialized expression");
if (ins->mCode == IC_LOAD_TEMPORARY)
{
ins->mCode = IC_CONSTANT;
ins->mConst = ins->mSrc[j];
ins->mConst.mTemp = -1;
ins->mConst.mIntConst = 0;
ins->mConst.mLinkerObject = nullptr;
ins->mConst.mVarIndex = -1;
ins->mConst.mMemory = IM_ABSOLUTE;
}
else
{
ins->mSrc[j].mTemp = -1;
ins->mSrc[j].mIntConst = 0;
}
}
}
}
if (ins->mDst.mTemp >= 0)
providedTemps += ins->mDst.mTemp;
}
if (mTrueJump) mTrueJump->WarnUsedUndefinedVariables(proc);
if (mFalseJump) mFalseJump->WarnUsedUndefinedVariables(proc);
}
}
void InterCodeBasicBlock::CollectVariables(GrowingVariableArray& globalVars, GrowingVariableArray& localVars, GrowingVariableArray& paramVars, InterMemory paramMemory) void InterCodeBasicBlock::CollectVariables(GrowingVariableArray& globalVars, GrowingVariableArray& localVars, GrowingVariableArray& paramVars, InterMemory paramMemory)
{ {
@ -12553,7 +12584,7 @@ void InterCodeProcedure::BuildDataFlowSets(void)
// Build set of globaly provided temporaries // Build set of globaly provided temporaries
// //
ResetVisited(); ResetVisited();
mEntryBlock->BuildGlobalProvidedTempSet(NumberSet(numTemps)); mEntryBlock->BuildGlobalProvidedTempSet(NumberSet(numTemps), NumberSet(numTemps));
// //
// Build set of globaly required temporaries, might need // Build set of globaly required temporaries, might need
@ -12708,6 +12739,17 @@ void InterCodeProcedure::CheckUsedDefinedTemps(void)
#endif #endif
} }
void InterCodeProcedure::WarnUsedUndefinedVariables(void)
{
ResetEntryBlocks();
ResetVisited();
mEntryBlock->CollectEntryBlocks(nullptr);
ResetVisited();
mEntryBlock->WarnUsedUndefinedVariables(this);
}
void InterCodeProcedure::TempForwarding(void) void InterCodeProcedure::TempForwarding(void)
{ {
int numTemps = mTemporaries.Size(); int numTemps = mTemporaries.Size();
@ -12739,7 +12781,7 @@ void InterCodeProcedure::RemoveUnusedInstructions(void)
mEntryBlock->BuildLocalTempSets(numTemps); mEntryBlock->BuildLocalTempSets(numTemps);
ResetVisited(); ResetVisited();
mEntryBlock->BuildGlobalProvidedTempSet(NumberSet(numTemps)); mEntryBlock->BuildGlobalProvidedTempSet(NumberSet(numTemps), NumberSet(numTemps));
NumberSet totalRequired2(numTemps); NumberSet totalRequired2(numTemps);
@ -12884,8 +12926,9 @@ void InterCodeProcedure::PromoteSimpleLocalsToTemp(InterMemory paramMemory, int
if (!complexLocals[vi]) if (!complexLocals[vi])
{ {
mLocalVars[vi]->mTemp = true; mLocalVars[vi]->mTemp = true;
mLocalVars[vi]->mTempIndex = AddTemporary(localTypes[vi]);
ResetVisited(); ResetVisited();
mEntryBlock->SimpleLocalToTemp(vi, AddTemporary(localTypes[vi])); mEntryBlock->SimpleLocalToTemp(vi, mLocalVars[vi]->mTempIndex);
} }
} }
@ -12895,11 +12938,15 @@ void InterCodeProcedure::PromoteSimpleLocalsToTemp(InterMemory paramMemory, int
BuildDataFlowSets(); BuildDataFlowSets();
WarnUsedUndefinedVariables();
RenameTemporaries(); RenameTemporaries();
do { do {
BuildDataFlowSets(); BuildDataFlowSets();
WarnUsedUndefinedVariables();
TempForwarding(); TempForwarding();
} while (GlobalConstantPropagation()); } while (GlobalConstantPropagation());
@ -14009,7 +14056,7 @@ void InterCodeProcedure::MergeBasicBlocks(void)
mblocks[i]->mTrueJump = nblock; mblocks[i]->mTrueJump = nblock;
block->mNumEntries -= mblocks.Size(); block->mNumEntries -= mblocks.Size();
InterInstruction* jins = new InterInstruction(); InterInstruction* jins = new InterInstruction(mblocks[0]->mInstructions.Last()->mLocation, IC_JUMP);
jins->mCode = IC_JUMP; jins->mCode = IC_JUMP;
nblock->mInstructions.Push(jins); nblock->mInstructions.Push(jins);
nblock->Close(block, nullptr); nblock->Close(block, nullptr);
@ -14094,7 +14141,7 @@ void InterCodeProcedure::ReduceTemporaries(void)
mEntryBlock->BuildLocalTempSets(numTemps); mEntryBlock->BuildLocalTempSets(numTemps);
ResetVisited(); ResetVisited();
mEntryBlock->BuildGlobalProvidedTempSet(NumberSet(numTemps)); mEntryBlock->BuildGlobalProvidedTempSet(NumberSet(numTemps), NumberSet(numTemps));
NumberSet totalRequired2(numTemps); NumberSet totalRequired2(numTemps);
@ -14149,7 +14196,7 @@ void InterCodeProcedure::ReduceTemporaries(void)
mEntryBlock->BuildLocalTempSets(numRenamedTemps); mEntryBlock->BuildLocalTempSets(numRenamedTemps);
ResetVisited(); ResetVisited();
mEntryBlock->BuildGlobalProvidedTempSet(NumberSet(numRenamedTemps)); mEntryBlock->BuildGlobalProvidedTempSet(NumberSet(numRenamedTemps), NumberSet(numRenamedTemps));
NumberSet totalRequired3(numRenamedTemps); NumberSet totalRequired3(numRenamedTemps);
@ -14300,8 +14347,8 @@ void InterCodeProcedure::Disassemble(const char* name, bool dumpSets)
#endif #endif
} }
InterCodeModule::InterCodeModule(Linker * linker) InterCodeModule::InterCodeModule(Errors* errors, Linker * linker)
: mLinker(linker), mGlobalVars(nullptr), mProcedures(nullptr), mCompilerOptions(0) : mErrors(errors), mLinker(linker), mGlobalVars(nullptr), mProcedures(nullptr), mCompilerOptions(0)
{ {
} }

View File

@ -233,13 +233,13 @@ class InterVariable
public: public:
Location mLocation; Location mLocation;
bool mUsed, mAliased, mTemp; bool mUsed, mAliased, mTemp;
int mIndex, mSize, mOffset, mAddr; int mIndex, mSize, mOffset, mAddr, mTempIndex;
int mNumReferences; int mNumReferences;
const Ident * mIdent; const Ident * mIdent;
LinkerObject * mLinkerObject; LinkerObject * mLinkerObject;
InterVariable(void) InterVariable(void)
: mUsed(false), mAliased(false), mTemp(false), mIndex(-1), mSize(0), mOffset(0), mIdent(nullptr), mLinkerObject(nullptr) : mUsed(false), mAliased(false), mTemp(false), mIndex(-1), mSize(0), mOffset(0), mIdent(nullptr), mLinkerObject(nullptr), mTempIndex(-1)
{ {
} }
}; };
@ -274,17 +274,17 @@ public:
class InterInstruction class InterInstruction
{ {
public: public:
Location mLocation;
InterCode mCode; InterCode mCode;
InterOperand mSrc[8]; InterOperand mSrc[8];
InterOperand mDst; InterOperand mDst;
InterOperand mConst; InterOperand mConst;
InterOperator mOperator; InterOperator mOperator;
int mNumOperands; int mNumOperands;
Location mLocation;
bool mInUse, mInvariant, mVolatile, mExpensive, mSingleAssignment; bool mInUse, mInvariant, mVolatile, mExpensive, mSingleAssignment;
InterInstruction(void); InterInstruction(const Location& loc, InterCode code);
bool IsEqual(const InterInstruction* ins) const; bool IsEqual(const InterInstruction* ins) const;
bool IsEqualSource(const InterInstruction* ins) const; bool IsEqualSource(const InterInstruction* ins) const;
@ -294,8 +294,6 @@ public:
bool ReferencesTemp(int temp) const; bool ReferencesTemp(int temp) const;
bool UsesTemp(int temp) const; bool UsesTemp(int temp) const;
void SetCode(const Location & loc, InterCode code);
void CollectLocalAddressTemps(GrowingIntArray& localTable, GrowingIntArray& paramTable); void CollectLocalAddressTemps(GrowingIntArray& localTable, GrowingIntArray& paramTable);
void MarkAliasedLocalTemps(const GrowingIntArray& localTable, NumberSet& aliasedLocals, const GrowingIntArray& paramTable, NumberSet& aliasedParams); void MarkAliasedLocalTemps(const GrowingIntArray& localTable, NumberSet& aliasedLocals, const GrowingIntArray& paramTable, NumberSet& aliasedParams);
@ -341,8 +339,8 @@ public:
NumberSet mLocalUsedTemps, mLocalModifiedTemps; NumberSet mLocalUsedTemps, mLocalModifiedTemps;
NumberSet mLocalRequiredTemps, mLocalProvidedTemps; NumberSet mLocalRequiredTemps, mLocalProvidedTemps;
NumberSet mEntryRequiredTemps, mEntryProvidedTemps; NumberSet mEntryRequiredTemps, mEntryProvidedTemps, mEntryPotentialTemps;
NumberSet mExitRequiredTemps, mExitProvidedTemps; NumberSet mExitRequiredTemps, mExitProvidedTemps, mExitPotentialTemps;
NumberSet mEntryConstTemp, mExitConstTemp; NumberSet mEntryConstTemp, mExitConstTemp;
NumberSet mLocalRequiredVars, mLocalProvidedVars; NumberSet mLocalRequiredVars, mLocalProvidedVars;
@ -396,7 +394,7 @@ public:
bool PropagateVariableCopy(const GrowingInstructionPtrArray& ctemps, const GrowingVariableArray& staticVars); bool PropagateVariableCopy(const GrowingInstructionPtrArray& ctemps, const GrowingVariableArray& staticVars);
void BuildLocalTempSets(int num); void BuildLocalTempSets(int num);
void BuildGlobalProvidedTempSet(const NumberSet & fromProvidedTemps); void BuildGlobalProvidedTempSet(const NumberSet & fromProvidedTemps, const NumberSet& potentialProvidedTemps);
bool BuildGlobalRequiredTempSet(NumberSet& fromRequiredTemps); bool BuildGlobalRequiredTempSet(NumberSet& fromRequiredTemps);
bool RemoveUnusedResultInstructions(void); bool RemoveUnusedResultInstructions(void);
void BuildCallerSaveTempSet(NumberSet& callerSaveTemps); void BuildCallerSaveTempSet(NumberSet& callerSaveTemps);
@ -508,6 +506,8 @@ public:
void CollectStaticStack(LinkerObject * lobj, const GrowingVariableArray& localVars); void CollectStaticStack(LinkerObject * lobj, const GrowingVariableArray& localVars);
bool SameExitCode(const InterCodeBasicBlock* block) const; bool SameExitCode(const InterCodeBasicBlock* block) const;
void WarnUsedUndefinedVariables(InterCodeProcedure* proc);
}; };
class InterCodeModule; class InterCodeModule;
@ -590,6 +590,7 @@ protected:
void MergeBasicBlocks(void); void MergeBasicBlocks(void);
void CheckUsedDefinedTemps(void); void CheckUsedDefinedTemps(void);
void WarnUsedUndefinedVariables(void);
void PeepholeOptimization(void); void PeepholeOptimization(void);
@ -601,7 +602,7 @@ protected:
class InterCodeModule class InterCodeModule
{ {
public: public:
InterCodeModule(Linker * linker); InterCodeModule(Errors* errors, Linker * linker);
~InterCodeModule(void); ~InterCodeModule(void);
bool Disassemble(const char* name); bool Disassemble(const char* name);
@ -611,6 +612,7 @@ public:
GrowingVariableArray mGlobalVars; GrowingVariableArray mGlobalVars;
Linker * mLinker; Linker * mLinker;
Errors* mErrors;
uint64 mCompilerOptions; uint64 mCompilerOptions;

File diff suppressed because it is too large Load Diff

View File

@ -26,6 +26,7 @@ public:
InterCodeProcedure* TranslateProcedure(InterCodeModule* mod, Expression* exp, Declaration * dec); InterCodeProcedure* TranslateProcedure(InterCodeModule* mod, Expression* exp, Declaration * dec);
void TranslateAssembler(InterCodeModule* mod, Expression * exp, GrowingArray<Declaration *> * refvars); void TranslateAssembler(InterCodeModule* mod, Expression * exp, GrowingArray<Declaration *> * refvars);
void InitGlobalVariable(InterCodeModule* mod, Declaration* dec); void InitGlobalVariable(InterCodeModule* mod, Declaration* dec);
void InitLocalVariable(InterCodeProcedure* proc, Declaration* dec, int index);
protected: protected:
Errors* mErrors; Errors* mErrors;
@ -51,10 +52,10 @@ protected:
typedef GrowingArray<SwitchNode> SwitchNodeArray; typedef GrowingArray<SwitchNode> SwitchNodeArray;
void BuildSwitchTree(InterCodeProcedure* proc, InterCodeBasicBlock* block, ExValue v, const SwitchNodeArray& nodes, int left, int right, InterCodeBasicBlock* dblock); void BuildSwitchTree(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock* block, ExValue v, const SwitchNodeArray& nodes, int left, int right, InterCodeBasicBlock* dblock);
ExValue Dereference(InterCodeProcedure* proc, InterCodeBasicBlock*& block, ExValue v, int level = 0); ExValue Dereference(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, ExValue v, int level = 0);
ExValue CoerceType(InterCodeProcedure* proc, InterCodeBasicBlock*& block, ExValue v, Declaration * type); ExValue CoerceType(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, ExValue v, Declaration * type);
ExValue TranslateExpression(Declaration * procType, InterCodeProcedure * proc, InterCodeBasicBlock*& block, Expression* exp, InterCodeBasicBlock* breakBlock, InterCodeBasicBlock* continueBlock, InlineMapper * inlineMapper, ExValue * lrexp = nullptr); ExValue TranslateExpression(Declaration * procType, InterCodeProcedure * proc, InterCodeBasicBlock*& block, Expression* exp, InterCodeBasicBlock* breakBlock, InterCodeBasicBlock* continueBlock, InlineMapper * inlineMapper, ExValue * lrexp = nullptr);
void TranslateLogic(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock* block, InterCodeBasicBlock* tblock, InterCodeBasicBlock* fblock, Expression* exp, InlineMapper* inlineMapper); void TranslateLogic(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock* block, InterCodeBasicBlock* tblock, InterCodeBasicBlock* fblock, Expression* exp, InlineMapper* inlineMapper);

View File

@ -147,7 +147,7 @@ NumberSet& NumberSet::operator-=(const NumberSet& set)
return *this; return *this;
} }
bool NumberSet::operator<=(const NumberSet& set) bool NumberSet::operator<=(const NumberSet& set) const
{ {
int i; int i;

View File

@ -28,7 +28,7 @@ public:
NumberSet& operator|=(const NumberSet& set); NumberSet& operator|=(const NumberSet& set);
NumberSet& operator-=(const NumberSet& set); NumberSet& operator-=(const NumberSet& set);
bool operator<=(const NumberSet& set); bool operator<=(const NumberSet& set) const;
void OrNot(const NumberSet& set); void OrNot(const NumberSet& set);