From 4ff762b711dd78d1b25f29ceb91d009b2a8ddf4a Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Sun, 16 Oct 2022 13:33:20 +0200 Subject: [PATCH] Add warning/error for unassigned variable access --- oscar64/Compiler.cpp | 2 +- oscar64/Errors.h | 3 + oscar64/InterCode.cpp | 281 ++++++++++------- oscar64/InterCode.h | 22 +- oscar64/InterCodeGenerator.cpp | 548 ++++++++++++++------------------- oscar64/InterCodeGenerator.h | 7 +- oscar64/NumberSet.cpp | 2 +- oscar64/NumberSet.h | 2 +- 8 files changed, 409 insertions(+), 458 deletions(-) diff --git a/oscar64/Compiler.cpp b/oscar64/Compiler.cpp index 4640789..a630f44 100644 --- a/oscar64/Compiler.cpp +++ b/oscar64/Compiler.cpp @@ -29,7 +29,7 @@ Compiler::Compiler(void) mByteCodeGenerator = new ByteCodeGenerator(mErrors, mLinker); mInterCodeGenerator = new InterCodeGenerator(mErrors, mLinker); mNativeCodeGenerator = new NativeCodeGenerator(mErrors, mLinker, mCompilationUnits->mSectionCode); - mInterCodeModule = new InterCodeModule(mLinker); + mInterCodeModule = new InterCodeModule(mErrors, mLinker); mGlobalAnalyzer = new GlobalAnalyzer(mErrors, mLinker); } diff --git a/oscar64/Errors.h b/oscar64/Errors.h index 0037c70..91211e8 100644 --- a/oscar64/Errors.h +++ b/oscar64/Errors.h @@ -24,6 +24,7 @@ enum ErrorID EWARN_BOOL_SHORTCUT, EWARN_OPTIMIZER_LOCKED, EWARN_LOOP_UNROLL_IGNORED, + EWARN_USE_OF_UNINITIALIZED_VARIABLE, EERR_GENERIC = 3000, EERR_FILE_NOT_FOUND, @@ -63,6 +64,8 @@ enum ErrorID ERRR_INTERRUPT_TO_COMPLEX, ERRR_INVALID_STORAGE_TYPE, ERRR_SEMICOLON_EXPECTED, + ERRR_USE_OF_UNINITIALIZED_VARIABLE, + EERR_INVALID_PREPROCESSOR, }; diff --git a/oscar64/InterCode.cpp b/oscar64/InterCode.cpp index 9483f88..9ab2b26 100644 --- a/oscar64/InterCode.cpp +++ b/oscar64/InterCode.cpp @@ -1407,7 +1407,7 @@ bool InterInstruction::ReferencesTemp(int temp) const InterInstruction* InterInstruction::Clone(void) const { - InterInstruction* ins = new InterInstruction(); + InterInstruction* ins = new InterInstruction(mLocation, mCode); *ins = *this; return ins; } @@ -2504,9 +2504,9 @@ bool InterOperand::IsEqual(const InterOperand& op) const return true; } -InterInstruction::InterInstruction(void) +InterInstruction::InterInstruction(const Location& loc, InterCode code) + : mLocation(loc), mCode(code) { - mCode = IC_NONE; mOperator = IA_NONE; mNumOperands = 3; @@ -2517,12 +2517,6 @@ InterInstruction::InterInstruction(void) mSingleAssignment = false; } -void InterInstruction::SetCode(const Location& loc, InterCode code) -{ - this->mCode = code; - this->mLocation = loc; -} - static bool TypeInteger(InterType t) { 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); mEntryProvidedTemps = NumberSet(num); + mEntryPotentialTemps = NumberSet(num); mExitRequiredTemps = NumberSet(num); mExitProvidedTemps = NumberSet(num); + mExitPotentialTemps = NumberSet(num); for (i = 0; i < mInstructions.Size(); i++) { @@ -6455,25 +6451,35 @@ void InterCodeBasicBlock::BuildLocalTempSets(int num) mEntryRequiredTemps = mLocalRequiredTemps; mExitProvidedTemps = mLocalProvidedTemps; + mExitPotentialTemps = mLocalProvidedTemps; if (mTrueJump) mTrueJump->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; if (!mVisited) { mEntryProvidedTemps = fromProvidedTemps; + mEntryPotentialTemps = potentialProvidedTemps; changed = true; } - else if (!(mEntryProvidedTemps <= fromProvidedTemps)) + else { - mEntryProvidedTemps &= fromProvidedTemps; - changed = true; + if (!(mEntryProvidedTemps <= fromProvidedTemps)) + { + mEntryProvidedTemps &= fromProvidedTemps; + changed = true; + } + if (!(potentialProvidedTemps <= mEntryPotentialTemps)) + { + mEntryPotentialTemps |= potentialProvidedTemps; + changed = true; + } } if (changed) @@ -6481,10 +6487,13 @@ void InterCodeBasicBlock::BuildGlobalProvidedTempSet(const NumberSet & fromProvi mExitProvidedTemps = mLocalProvidedTemps; mExitProvidedTemps |= mEntryProvidedTemps; + mExitPotentialTemps = mLocalProvidedTemps; + mExitPotentialTemps |= mEntryPotentialTemps; + mVisited = true; - if (mTrueJump) mTrueJump->BuildGlobalProvidedTempSet(mExitProvidedTemps); - if (mFalseJump) mFalseJump->BuildGlobalProvidedTempSet(mExitProvidedTemps); + if (mTrueJump) mTrueJump->BuildGlobalProvidedTempSet(mExitProvidedTemps, mExitPotentialTemps); + if (mFalseJump) mFalseJump->BuildGlobalProvidedTempSet(mExitProvidedTemps, mExitPotentialTemps); } } @@ -7381,8 +7390,7 @@ bool InterCodeBasicBlock::SimplifyIntegerNumeric(const GrowingInstructionPtrArra if (spareTemps + 2 >= ltvalue.Size()) return true; - InterInstruction* cins = new InterInstruction(); - cins->mCode = IC_CONVERSION_OPERATOR; + InterInstruction* cins = new InterInstruction(ins->mLocation, IC_CONVERSION_OPERATOR); cins->mOperator = IA_EXT8TO16U; cins->mSrc[0] = ains->mSrc[1]; cins->mDst.mTemp = spareTemps++; @@ -7391,8 +7399,7 @@ bool InterCodeBasicBlock::SimplifyIntegerNumeric(const GrowingInstructionPtrArra cins->mDst.mRange.LimitMin(0); mInstructions.Insert(i, cins); - InterInstruction* nins = new InterInstruction(); - nins->mCode = IC_BINARY_OPERATOR; + InterInstruction* nins = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR); nins->mOperator = IA_SHL; nins->mSrc[0] = ins->mSrc[0]; nins->mSrc[1] = cins->mDst; @@ -7562,8 +7569,7 @@ bool InterCodeBasicBlock::SimplifyIntegerNumeric(const GrowingInstructionPtrArra if (spareTemps + 2 >= ltvalue.Size()) return true; - InterInstruction* nins = new InterInstruction(); - nins->mCode = IC_CONVERSION_OPERATOR; + InterInstruction* nins = new InterInstruction(ins->mLocation, IC_CONVERSION_OPERATOR); nins->mOperator = IA_EXT8TO16U; nins->mSrc[0] = ains->mSrc[1]; nins->mDst.mTemp = spareTemps++; @@ -7655,8 +7661,7 @@ bool InterCodeBasicBlock::SimplifyIntegerNumeric(const GrowingInstructionPtrArra if (spareTemps + 2 >= ltvalue.Size()) return true; - InterInstruction* nins = new InterInstruction(); - nins->mCode = IC_LEA; + InterInstruction* nins = new InterInstruction(ins->mLocation, IC_LEA); nins->mSrc[0].Forward(pins->mSrc[0]); nins->mSrc[1].ForwardMem(pins->mSrc[1]); nins->mSrc[1].mIntConst += ins->mSrc[1].mIntConst; @@ -7704,8 +7709,7 @@ bool InterCodeBasicBlock::SimplifyIntegerNumeric(const GrowingInstructionPtrArra if (spareTemps + 2 >= ltvalue.Size()) return true; - InterInstruction* nins = new InterInstruction(); - nins->mCode = IC_LEA; + InterInstruction* nins = new InterInstruction(ins->mLocation, IC_LEA); nins->mSrc[0].Forward(pins->mSrc[0]); nins->mSrc[1].ForwardMem(pins->mSrc[1]); 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]; if (ai0 && ai0->mCode == IC_CONSTANT) { - InterInstruction* nai = new InterInstruction(); - nai->mCode = IC_BINARY_OPERATOR; + InterInstruction* nai = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR); nai->mOperator = IA_MUL; nai->mSrc[0].mTemp = mi0->mSrc[1].mTemp; nai->mSrc[0].mType = IT_INT16; @@ -7855,8 +7858,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra ltvalue[nai->mDst.mTemp] = nullptr; - InterInstruction* cai = new InterInstruction(); - cai->mCode = IC_CONSTANT; + InterInstruction* cai = new InterInstruction(ins->mLocation, IC_CONSTANT); cai->mDst.mTemp = spareTemps++; cai->mDst.mType = IT_INT16; 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) { - InterInstruction* nai = new InterInstruction(); - nai->mCode = IC_BINARY_OPERATOR; + InterInstruction* nai = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR); nai->mOperator = IA_MUL; nai->mSrc[0].mTemp = mi0->mSrc[0].mTemp; nai->mSrc[0].mType = IT_INT16; @@ -7883,8 +7884,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra ltvalue[nai->mDst.mTemp] = nullptr; - InterInstruction* cai = new InterInstruction(); - cai->mCode = IC_CONSTANT; + InterInstruction* cai = new InterInstruction(ins->mLocation, IC_CONSTANT); cai->mDst.mTemp = spareTemps++; cai->mDst.mType = IT_INT16; 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]; if (ai0 && ai0->mCode == IC_CONSTANT) { - InterInstruction* nai = new InterInstruction(); - nai->mCode = IC_BINARY_OPERATOR; + InterInstruction* nai = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR); nai->mOperator = IA_MUL; nai->mSrc[0].mTemp = mi1->mSrc[1].mTemp; nai->mSrc[0].mType = IT_INT16; @@ -7915,8 +7914,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra ltvalue[nai->mDst.mTemp] = nullptr; - InterInstruction* cai = new InterInstruction(); - cai->mCode = IC_CONSTANT; + InterInstruction* cai = new InterInstruction(ins->mLocation, IC_CONSTANT); cai->mDst.mTemp = spareTemps++; cai->mDst.mType = IT_INT16; 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) { - InterInstruction* nai = new InterInstruction(); - nai->mCode = IC_BINARY_OPERATOR; + InterInstruction* nai = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR); nai->mOperator = IA_MUL; nai->mSrc[0].mTemp = mi1->mSrc[0].mTemp; nai->mSrc[0].mType = IT_INT16; @@ -7943,8 +7940,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra ltvalue[nai->mDst.mTemp] = nullptr; - InterInstruction* cai = new InterInstruction(); - cai->mCode = IC_CONSTANT; + InterInstruction* cai = new InterInstruction(ins->mLocation, IC_CONSTANT); cai->mDst.mTemp = spareTemps++; cai->mDst.mType = IT_INT16; 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]; if (ai0 && ai0->mCode == IC_CONSTANT) { - InterInstruction* nai = new InterInstruction(); - nai->mCode = IC_BINARY_OPERATOR; + InterInstruction* nai = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR); nai->mOperator = IA_MUL; nai->mSrc[0].mTemp = mi1->mSrc[1].mTemp; nai->mSrc[0].mType = IT_INT16; @@ -7976,8 +7971,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra ltvalue[nai->mDst.mTemp] = nullptr; - InterInstruction* cai = new InterInstruction(); - cai->mCode = IC_CONSTANT; + InterInstruction* cai = new InterInstruction(ins->mLocation, IC_CONSTANT); cai->mDst.mTemp = spareTemps++; cai->mDst.mType = IT_INT16; cai->mConst.mIntConst = ai0->mConst.mIntConst * mi0->mConst.mIntConst; @@ -8007,8 +8001,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra { if (ai0 && ai0->mCode == IC_CONSTANT) { - InterInstruction* cai = new InterInstruction(); - cai->mCode = IC_CONSTANT; + InterInstruction* cai = new InterInstruction(ins->mLocation, IC_CONSTANT); cai->mDst.mTemp = spareTemps++; cai->mDst.mType = IT_INT16; 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) { - InterInstruction* cai = new InterInstruction(); - cai->mCode = IC_CONSTANT; + InterInstruction* cai = new InterInstruction(ins->mLocation, IC_CONSTANT); cai->mDst.mTemp = spareTemps++; cai->mDst.mType = IT_INT16; cai->mConst.mIntConst = ai1->mConst.mIntConst + mi1->mConst.mIntConst; @@ -8042,8 +8034,7 @@ void InterCodeBasicBlock::PerformValueForwarding(const GrowingInstructionPtrArra { if (ai0 && ai0->mCode == IC_CONSTANT) { - InterInstruction* cai = new InterInstruction(); - cai->mCode = IC_CONSTANT; + InterInstruction* cai = new InterInstruction(ins->mLocation, IC_CONSTANT); cai->mDst.mTemp = spareTemps++; cai->mDst.mType = IT_INT16; 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) { - InterInstruction* cai = new InterInstruction(); - cai->mCode = IC_CONSTANT; + InterInstruction* cai = new InterInstruction(ins->mLocation, IC_CONSTANT); cai->mDst.mTemp = spareTemps++; cai->mDst.mType = IT_INT16; 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]; if (ai0 && ai1 && ai0->mCode == IC_CONSTANT && ai0->mConst.mIntConst >= 0) { - InterInstruction* nai = new InterInstruction(); - nai->mCode = IC_LEA; + InterInstruction* nai = new InterInstruction(ins->mLocation, IC_LEA); nai->mSrc[1].mMemory = IM_INDIRECT; nai->mSrc[0].mTemp = li0->mSrc[1].mTemp; 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]; if (ai0 && ai1 && ai0->mCode == IC_CONSTANT && ai0->mConst.mIntConst >= 0) { - InterInstruction* cai = new InterInstruction(); - cai->mCode = IC_CONSTANT; + InterInstruction* cai = new InterInstruction(ins->mLocation, IC_CONSTANT); cai->mDst.mTemp = spareTemps++; cai->mDst.mType = IT_INT16; cai->mConst.mIntConst = ai0->mConst.mIntConst + li0->mConst.mIntConst; @@ -8832,8 +8820,7 @@ bool InterCodeBasicBlock::MergeCommonPathInstructions(void) { if (fins->mDst.mTemp != tins->mDst.mTemp) { - InterInstruction* nins = new InterInstruction(); - nins->mCode = IC_LOAD_TEMPORARY; + InterInstruction* nins = new InterInstruction(tins->mLocation, IC_LOAD_TEMPORARY); nins->mDst.mTemp = fins->mDst.mTemp; nins->mDst.mType = fins->mDst.mType; nins->mSrc[0].mTemp = tins->mDst.mTemp; @@ -9105,8 +9092,7 @@ bool InterCodeBasicBlock::ForwardDiamondMovedTemp(void) { tblock->mInstructions[j]->mDst.mTemp = stemp; - InterInstruction* nins = new InterInstruction(); - nins->mCode = IC_LOAD_TEMPORARY; + InterInstruction* nins = new InterInstruction(mins->mLocation, IC_LOAD_TEMPORARY); nins->mDst.mTemp = ttemp; nins->mDst.mType = mins->mDst.mType; nins->mSrc[0].mTemp = stemp; @@ -9483,49 +9469,46 @@ void InterCodeBasicBlock::ExpandSelect(InterCodeProcedure* proc) mInstructions.SetSize(i); - InterInstruction* bins = new InterInstruction(); - bins->mCode = IC_BRANCH; + InterInstruction* bins = new InterInstruction(sins->mLocation, IC_BRANCH); bins->mSrc[0] = sins->mSrc[2]; mInstructions.Push(bins); - InterInstruction* tins = new InterInstruction(); + InterInstruction* tins; if (sins->mSrc[1].mTemp < 0) { - tins->mCode = IC_CONSTANT; + tins = new InterInstruction(sins->mLocation, IC_CONSTANT); tins->mConst = sins->mSrc[1]; } else { - tins->mCode = IC_LOAD_TEMPORARY; + tins = new InterInstruction(sins->mLocation, IC_LOAD_TEMPORARY); tins->mSrc[0] = sins->mSrc[1]; } tins->mDst = sins->mDst; - InterInstruction* fins = new InterInstruction(); + InterInstruction* fins; if (sins->mSrc[0].mTemp < 0) { - fins->mCode = IC_CONSTANT; + fins = new InterInstruction(sins->mLocation, IC_CONSTANT); fins->mConst = sins->mSrc[0]; } else { - fins->mCode = IC_LOAD_TEMPORARY; + fins = new InterInstruction(sins->mLocation, IC_LOAD_TEMPORARY); fins->mSrc[0] = sins->mSrc[0]; } fins->mDst = sins->mDst; tblock->mInstructions.Push(tins); - InterInstruction* jins = new InterInstruction(); - jins->mCode = IC_JUMP; + InterInstruction* jins = new InterInstruction(sins->mLocation, IC_JUMP); tblock->mInstructions.Push(jins); tblock->Close(eblock, nullptr); fblock->mInstructions.Push(fins); - jins = new InterInstruction(); - jins->mCode = IC_JUMP; + jins = new InterInstruction(sins->mLocation, IC_JUMP); fblock->mInstructions.Push(jins); 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)) { InterCodeBasicBlock* block = new InterCodeBasicBlock(); + InterInstruction* ins = mInstructions.Last(); + proc->Append(block); if (mInstructions[mInstructions.Size() - 2]->mCode == IC_RELATIONAL_OPERATOR) { @@ -9562,8 +9547,7 @@ void InterCodeBasicBlock::SplitBranches(InterCodeProcedure* proc) block->mInstructions.Push(mInstructions.Pop()); } - InterInstruction* jins = new InterInstruction(); - jins->mCode = IC_JUMP; + InterInstruction* jins = new InterInstruction(ins->mLocation, IC_JUMP); mInstructions.Push(jins); block->Close(mTrueJump, mFalseJump); mTrueJump = block; @@ -9848,8 +9832,7 @@ void InterCodeBasicBlock::BuildLoopSuffix(InterCodeProcedure* proc) { InterCodeBasicBlock* suffix = new InterCodeBasicBlock(); proc->Append(suffix); - InterInstruction* jins = new InterInstruction(); - jins->mCode = IC_JUMP; + InterInstruction* jins = new InterInstruction(mInstructions[0]->mLocation, IC_JUMP); suffix->Append(jins); suffix->Close(mFalseJump, nullptr); mFalseJump = suffix; @@ -9862,8 +9845,7 @@ void InterCodeBasicBlock::BuildLoopSuffix(InterCodeProcedure* proc) { InterCodeBasicBlock* suffix = new InterCodeBasicBlock(); proc->Append(suffix); - InterInstruction* jins = new InterInstruction(); - jins->mCode = IC_JUMP; + InterInstruction* jins = new InterInstruction(mInstructions[0]->mLocation, IC_JUMP); suffix->Append(jins); suffix->Close(mTrueJump, nullptr); mTrueJump = suffix; @@ -9894,8 +9876,7 @@ InterCodeBasicBlock* InterCodeBasicBlock::BuildLoopPrefix(InterCodeProcedure* pr { mLoopPrefix = new InterCodeBasicBlock(); proc->Append(mLoopPrefix); - InterInstruction* jins = new InterInstruction(); - jins->mCode = IC_JUMP; + InterInstruction* jins = new InterInstruction(mInstructions[0]->mLocation, IC_JUMP); mLoopPrefix->Append(jins); mLoopPrefix->Close(this, nullptr); } @@ -10314,8 +10295,7 @@ void InterCodeBasicBlock::SingleBlockLoopUnrolling(void) mTrueJump = mFalseJump; mFalseJump = nullptr; - InterInstruction* jins = new InterInstruction(); - jins->mCode = IC_JUMP; + InterInstruction* jins = new InterInstruction(mInstructions[0]->mLocation, IC_JUMP); mInstructions.Push(jins); } } @@ -10384,8 +10364,7 @@ bool InterCodeBasicBlock::SingleBlockLoopPointerSplit(int& spareTemps) return true; InterInstruction* pins = tvalues[lins->mSrc[1].mTemp]; - InterInstruction* nins = new InterInstruction(); - nins->mCode = IC_LEA; + InterInstruction* nins = new InterInstruction(lins->mLocation, IC_LEA); nins->mSrc[1] = pins->mSrc[1]; nins->mSrc[0].mTemp = -1; nins->mSrc[0].mType = IT_INT16; @@ -10487,9 +10466,8 @@ void InterCodeBasicBlock::SingleBlockLoopOptimisation(const NumberSet& aliasedPa // Move load before loop mLoopPrefix->mInstructions.Insert(mLoopPrefix->mInstructions.Size() - 1, ins); - InterInstruction* nins = new InterInstruction(); + InterInstruction* nins = new InterInstruction(sins->mLocation, IC_LOAD_TEMPORARY); mInstructions[i] = nins; - nins->mCode = IC_LOAD_TEMPORARY; nins->mDst.Forward(ins->mDst); nins->mSrc[0].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]; indexBase[ins->mDst.mTemp] = ins->mSrc[1].mIntConst * indexBase[ins->mSrc[0].mTemp]; - InterInstruction* bins = new InterInstruction(); - bins->mCode = IC_BINARY_OPERATOR; + InterInstruction* bins = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR); bins->mOperator = IA_MUL; bins->mDst = ins->mDst; bins->mSrc[0] = ins->mSrc[0]; bins->mSrc[1] = ins->mSrc[1]; mLoopPrefix->mInstructions.Insert(mLoopPrefix->mInstructions.Size() - 1, bins); - InterInstruction* ains = new InterInstruction(); - ains->mCode = IC_BINARY_OPERATOR; + InterInstruction* ains = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR); ains->mOperator = IA_ADD; ains->mDst = 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]; indexBase[ins->mDst.mTemp] = ins->mSrc[0].mIntConst * indexBase[ins->mSrc[1].mTemp]; - InterInstruction* bins = new InterInstruction(); - bins->mCode = IC_BINARY_OPERATOR; + InterInstruction* bins = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR); bins->mOperator = IA_MUL; bins->mDst = ins->mDst; bins->mSrc[0] = ins->mSrc[0]; bins->mSrc[1] = ins->mSrc[1]; mLoopPrefix->mInstructions.Insert(mLoopPrefix->mInstructions.Size() - 1, bins); - InterInstruction* ains = new InterInstruction(); - ains->mCode = IC_BINARY_OPERATOR; + InterInstruction* ains = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR); ains->mOperator = IA_ADD; ains->mDst = 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; indexBase[ins->mDst.mTemp] = indexBase[ins->mSrc[1].mTemp] << ins->mSrc[0].mIntConst; - InterInstruction* bins = new InterInstruction(); - bins->mCode = IC_BINARY_OPERATOR; + InterInstruction* bins = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR); bins->mOperator = IA_SHL; bins->mDst = ins->mDst; bins->mSrc[0] = ins->mSrc[0]; bins->mSrc[1] = ins->mSrc[1]; mLoopPrefix->mInstructions.Insert(mLoopPrefix->mInstructions.Size() - 1, bins); - InterInstruction* ains = new InterInstruction(); - ains->mCode = IC_BINARY_OPERATOR; + InterInstruction* ains = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR); ains->mOperator = IA_ADD; ains->mDst = 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); - InterInstruction* ains = new InterInstruction(); - ains->mCode = IC_BINARY_OPERATOR; + InterInstruction* ains = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR); ains->mOperator = IA_ADD; ains->mDst = 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); - InterInstruction* ains = new InterInstruction(); - ains->mCode = IC_BINARY_OPERATOR; + InterInstruction* ains = new InterInstruction(ins->mLocation, IC_BINARY_OPERATOR); ains->mOperator = IA_ADD; ains->mDst = 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); - InterInstruction* ains = new InterInstruction(); - ains->mCode = IC_LEA; + InterInstruction* ains = new InterInstruction(ins->mLocation, IC_LEA); ains->mDst = ins->mDst; ains->mSrc[1] = ins->mDst; ains->mSrc[1].mMemory = IM_INDIRECT; @@ -10929,8 +10898,7 @@ void InterCodeBasicBlock::SingleBlockLoopOptimisation(const NumberSet& aliasedPa if (tailBlock->mEntryRequiredTemps[ains->mDst.mTemp]) { - InterInstruction* dins = new InterInstruction(); - dins->mCode = IC_LEA; + InterInstruction* dins = new InterInstruction(ins->mLocation, IC_LEA); dins->mDst = ins->mDst; dins->mSrc[1] = ins->mDst; dins->mSrc[1].mMemory = IM_INDIRECT; @@ -10954,8 +10922,7 @@ void InterCodeBasicBlock::SingleBlockLoopOptimisation(const NumberSet& aliasedPa mLoopPrefix->mInstructions.Insert(mLoopPrefix->mInstructions.Size() - 1, ins); - InterInstruction* ains = new InterInstruction(); - ains->mCode = ins->mCode; + InterInstruction* ains = new InterInstruction(ins->mLocation, ins->mCode); ains->mOperator = ins->mOperator; ains->mSrc[0] = ins->mSrc[0]; 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) { @@ -12553,7 +12584,7 @@ void InterCodeProcedure::BuildDataFlowSets(void) // Build set of globaly provided temporaries // ResetVisited(); - mEntryBlock->BuildGlobalProvidedTempSet(NumberSet(numTemps)); + mEntryBlock->BuildGlobalProvidedTempSet(NumberSet(numTemps), NumberSet(numTemps)); // // Build set of globaly required temporaries, might need @@ -12708,6 +12739,17 @@ void InterCodeProcedure::CheckUsedDefinedTemps(void) #endif } +void InterCodeProcedure::WarnUsedUndefinedVariables(void) +{ + ResetEntryBlocks(); + ResetVisited(); + mEntryBlock->CollectEntryBlocks(nullptr); + + ResetVisited(); + mEntryBlock->WarnUsedUndefinedVariables(this); +} + + void InterCodeProcedure::TempForwarding(void) { int numTemps = mTemporaries.Size(); @@ -12739,7 +12781,7 @@ void InterCodeProcedure::RemoveUnusedInstructions(void) mEntryBlock->BuildLocalTempSets(numTemps); ResetVisited(); - mEntryBlock->BuildGlobalProvidedTempSet(NumberSet(numTemps)); + mEntryBlock->BuildGlobalProvidedTempSet(NumberSet(numTemps), NumberSet(numTemps)); NumberSet totalRequired2(numTemps); @@ -12884,8 +12926,9 @@ void InterCodeProcedure::PromoteSimpleLocalsToTemp(InterMemory paramMemory, int if (!complexLocals[vi]) { mLocalVars[vi]->mTemp = true; + mLocalVars[vi]->mTempIndex = AddTemporary(localTypes[vi]); ResetVisited(); - mEntryBlock->SimpleLocalToTemp(vi, AddTemporary(localTypes[vi])); + mEntryBlock->SimpleLocalToTemp(vi, mLocalVars[vi]->mTempIndex); } } @@ -12895,11 +12938,15 @@ void InterCodeProcedure::PromoteSimpleLocalsToTemp(InterMemory paramMemory, int BuildDataFlowSets(); + WarnUsedUndefinedVariables(); + RenameTemporaries(); do { BuildDataFlowSets(); + WarnUsedUndefinedVariables(); + TempForwarding(); } while (GlobalConstantPropagation()); @@ -14009,7 +14056,7 @@ void InterCodeProcedure::MergeBasicBlocks(void) mblocks[i]->mTrueJump = nblock; block->mNumEntries -= mblocks.Size(); - InterInstruction* jins = new InterInstruction(); + InterInstruction* jins = new InterInstruction(mblocks[0]->mInstructions.Last()->mLocation, IC_JUMP); jins->mCode = IC_JUMP; nblock->mInstructions.Push(jins); nblock->Close(block, nullptr); @@ -14094,7 +14141,7 @@ void InterCodeProcedure::ReduceTemporaries(void) mEntryBlock->BuildLocalTempSets(numTemps); ResetVisited(); - mEntryBlock->BuildGlobalProvidedTempSet(NumberSet(numTemps)); + mEntryBlock->BuildGlobalProvidedTempSet(NumberSet(numTemps), NumberSet(numTemps)); NumberSet totalRequired2(numTemps); @@ -14149,7 +14196,7 @@ void InterCodeProcedure::ReduceTemporaries(void) mEntryBlock->BuildLocalTempSets(numRenamedTemps); ResetVisited(); - mEntryBlock->BuildGlobalProvidedTempSet(NumberSet(numRenamedTemps)); + mEntryBlock->BuildGlobalProvidedTempSet(NumberSet(numRenamedTemps), NumberSet(numRenamedTemps)); NumberSet totalRequired3(numRenamedTemps); @@ -14300,8 +14347,8 @@ void InterCodeProcedure::Disassemble(const char* name, bool dumpSets) #endif } -InterCodeModule::InterCodeModule(Linker * linker) - : mLinker(linker), mGlobalVars(nullptr), mProcedures(nullptr), mCompilerOptions(0) +InterCodeModule::InterCodeModule(Errors* errors, Linker * linker) + : mErrors(errors), mLinker(linker), mGlobalVars(nullptr), mProcedures(nullptr), mCompilerOptions(0) { } diff --git a/oscar64/InterCode.h b/oscar64/InterCode.h index dc40d63..5ee8b36 100644 --- a/oscar64/InterCode.h +++ b/oscar64/InterCode.h @@ -233,13 +233,13 @@ class InterVariable public: Location mLocation; bool mUsed, mAliased, mTemp; - int mIndex, mSize, mOffset, mAddr; + int mIndex, mSize, mOffset, mAddr, mTempIndex; int mNumReferences; const Ident * mIdent; LinkerObject * mLinkerObject; 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 { public: + Location mLocation; InterCode mCode; InterOperand mSrc[8]; InterOperand mDst; InterOperand mConst; InterOperator mOperator; int mNumOperands; - Location mLocation; bool mInUse, mInvariant, mVolatile, mExpensive, mSingleAssignment; - InterInstruction(void); + InterInstruction(const Location& loc, InterCode code); bool IsEqual(const InterInstruction* ins) const; bool IsEqualSource(const InterInstruction* ins) const; @@ -294,8 +294,6 @@ public: bool ReferencesTemp(int temp) const; bool UsesTemp(int temp) const; - void SetCode(const Location & loc, InterCode code); - void CollectLocalAddressTemps(GrowingIntArray& localTable, GrowingIntArray& paramTable); void MarkAliasedLocalTemps(const GrowingIntArray& localTable, NumberSet& aliasedLocals, const GrowingIntArray& paramTable, NumberSet& aliasedParams); @@ -341,8 +339,8 @@ public: NumberSet mLocalUsedTemps, mLocalModifiedTemps; NumberSet mLocalRequiredTemps, mLocalProvidedTemps; - NumberSet mEntryRequiredTemps, mEntryProvidedTemps; - NumberSet mExitRequiredTemps, mExitProvidedTemps; + NumberSet mEntryRequiredTemps, mEntryProvidedTemps, mEntryPotentialTemps; + NumberSet mExitRequiredTemps, mExitProvidedTemps, mExitPotentialTemps; NumberSet mEntryConstTemp, mExitConstTemp; NumberSet mLocalRequiredVars, mLocalProvidedVars; @@ -396,7 +394,7 @@ public: bool PropagateVariableCopy(const GrowingInstructionPtrArray& ctemps, const GrowingVariableArray& staticVars); void BuildLocalTempSets(int num); - void BuildGlobalProvidedTempSet(const NumberSet & fromProvidedTemps); + void BuildGlobalProvidedTempSet(const NumberSet & fromProvidedTemps, const NumberSet& potentialProvidedTemps); bool BuildGlobalRequiredTempSet(NumberSet& fromRequiredTemps); bool RemoveUnusedResultInstructions(void); void BuildCallerSaveTempSet(NumberSet& callerSaveTemps); @@ -508,6 +506,8 @@ public: void CollectStaticStack(LinkerObject * lobj, const GrowingVariableArray& localVars); bool SameExitCode(const InterCodeBasicBlock* block) const; + + void WarnUsedUndefinedVariables(InterCodeProcedure* proc); }; class InterCodeModule; @@ -590,6 +590,7 @@ protected: void MergeBasicBlocks(void); void CheckUsedDefinedTemps(void); + void WarnUsedUndefinedVariables(void); void PeepholeOptimization(void); @@ -601,7 +602,7 @@ protected: class InterCodeModule { public: - InterCodeModule(Linker * linker); + InterCodeModule(Errors* errors, Linker * linker); ~InterCodeModule(void); bool Disassemble(const char* name); @@ -611,6 +612,7 @@ public: GrowingVariableArray mGlobalVars; Linker * mLinker; + Errors* mErrors; uint64 mCompilerOptions; diff --git a/oscar64/InterCodeGenerator.cpp b/oscar64/InterCodeGenerator.cpp index c9caa48..7e06af1 100644 --- a/oscar64/InterCodeGenerator.cpp +++ b/oscar64/InterCodeGenerator.cpp @@ -37,12 +37,11 @@ static inline InterType InterTypeOf(const Declaration* dec) } } -InterCodeGenerator::ExValue InterCodeGenerator::Dereference(InterCodeProcedure* proc, InterCodeBasicBlock*& block, ExValue v, int level) +InterCodeGenerator::ExValue InterCodeGenerator::Dereference(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, ExValue v, int level) { while (v.mReference > level) { - InterInstruction * ins = new InterInstruction(); - ins->mCode = IC_LOAD; + InterInstruction * ins = new InterInstruction(exp->mLocation, IC_LOAD); ins->mSrc[0].mMemory = IM_INDIRECT; ins->mSrc[0].mType = IT_POINTER; ins->mSrc[0].mTemp = v.mTemp; @@ -64,7 +63,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::Dereference(InterCodeProcedure* return v; } -InterCodeGenerator::ExValue InterCodeGenerator::CoerceType(InterCodeProcedure* proc, InterCodeBasicBlock*& block, ExValue v, Declaration* type) +InterCodeGenerator::ExValue InterCodeGenerator::CoerceType(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, ExValue v, Declaration* type) { int stemp = v.mTemp; @@ -74,8 +73,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::CoerceType(InterCodeProcedure* p { if (v.mType->mFlags & DTF_SIGNED) { - InterInstruction * xins = new InterInstruction(); - xins->mCode = IC_CONVERSION_OPERATOR; + InterInstruction * xins = new InterInstruction(exp->mLocation, IC_CONVERSION_OPERATOR); xins->mOperator = IA_EXT8TO16S; xins->mSrc[0].mType = IT_INT8; xins->mSrc[0].mTemp = stemp; @@ -86,8 +84,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::CoerceType(InterCodeProcedure* p } else { - InterInstruction * xins = new InterInstruction(); - xins->mCode = IC_CONVERSION_OPERATOR; + InterInstruction * xins = new InterInstruction(exp->mLocation, IC_CONVERSION_OPERATOR); xins->mOperator = IA_EXT8TO16U; xins->mSrc[0].mType = IT_INT8; xins->mSrc[0].mTemp = stemp; @@ -98,8 +95,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::CoerceType(InterCodeProcedure* p } } - InterInstruction * cins = new InterInstruction(); - cins->mCode = IC_CONVERSION_OPERATOR; + InterInstruction * cins = new InterInstruction(exp->mLocation, IC_CONVERSION_OPERATOR); cins->mOperator = IA_INT2FLOAT; cins->mSrc[0].mType = IT_INT16; cins->mSrc[0].mTemp = stemp; @@ -112,8 +108,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::CoerceType(InterCodeProcedure* p } else if (v.mType->mType == DT_TYPE_FLOAT && type->IsIntegerType()) { - InterInstruction * cins = new InterInstruction(); - cins->mCode = IC_CONVERSION_OPERATOR; + InterInstruction * cins = new InterInstruction(exp->mLocation, IC_CONVERSION_OPERATOR); cins->mOperator = IA_FLOAT2INT; cins->mSrc[0].mType = IT_FLOAT; cins->mSrc[0].mTemp = v.mTemp; @@ -129,8 +124,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::CoerceType(InterCodeProcedure* p { if (v.mType->mFlags & DTF_SIGNED) { - InterInstruction * xins = new InterInstruction(); - xins->mCode = IC_CONVERSION_OPERATOR; + InterInstruction * xins = new InterInstruction(exp->mLocation, IC_CONVERSION_OPERATOR); xins->mOperator = IA_EXT8TO16S; xins->mSrc[0].mType = IT_INT8; xins->mSrc[0].mTemp = stemp; @@ -141,8 +135,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::CoerceType(InterCodeProcedure* p } else { - InterInstruction * xins = new InterInstruction(); - xins->mCode = IC_CONVERSION_OPERATOR; + InterInstruction * xins = new InterInstruction(exp->mLocation, IC_CONVERSION_OPERATOR); xins->mOperator = IA_EXT8TO16U; xins->mSrc[0].mType = IT_INT8; xins->mSrc[0].mTemp = stemp; @@ -156,8 +149,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::CoerceType(InterCodeProcedure* p { if (v.mType->mFlags & DTF_SIGNED) { - InterInstruction* xins = new InterInstruction(); - xins->mCode = IC_CONVERSION_OPERATOR; + InterInstruction* xins = new InterInstruction(exp->mLocation, IC_CONVERSION_OPERATOR); xins->mOperator = IA_EXT16TO32S; xins->mSrc[0].mType = IT_INT16; xins->mSrc[0].mTemp = stemp; @@ -168,8 +160,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::CoerceType(InterCodeProcedure* p } else { - InterInstruction* xins = new InterInstruction(); - xins->mCode = IC_CONVERSION_OPERATOR; + InterInstruction* xins = new InterInstruction(exp->mLocation, IC_CONVERSION_OPERATOR); xins->mOperator = IA_EXT16TO32U; xins->mSrc[0].mType = IT_INT16; xins->mSrc[0].mTemp = stemp; @@ -183,8 +174,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::CoerceType(InterCodeProcedure* p { if (v.mType->mFlags & DTF_SIGNED) { - InterInstruction* xins = new InterInstruction(); - xins->mCode = IC_CONVERSION_OPERATOR; + InterInstruction* xins = new InterInstruction(exp->mLocation, IC_CONVERSION_OPERATOR); xins->mOperator = IA_EXT8TO32S; xins->mSrc[0].mType = IT_INT8; xins->mSrc[0].mTemp = stemp; @@ -195,8 +185,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::CoerceType(InterCodeProcedure* p } else { - InterInstruction* xins = new InterInstruction(); - xins->mCode = IC_CONVERSION_OPERATOR; + InterInstruction* xins = new InterInstruction(exp->mLocation, IC_CONVERSION_OPERATOR); xins->mOperator = IA_EXT8TO32U; xins->mSrc[0].mType = IT_INT8; xins->mSrc[0].mTemp = stemp; @@ -239,6 +228,15 @@ static inline InterType InterTypeOfArithmetic(InterType t1, InterType t2) return IT_INT16; } +void InterCodeGenerator::InitLocalVariable(InterCodeProcedure* proc, Declaration* dec, int index) +{ + if (!proc->mLocalVars[index]) + { + proc->mLocalVars[index] = new InterVariable(); + proc->mLocalVars[index]->mIdent = dec->mIdent; + } +} + void InterCodeGenerator::InitGlobalVariable(InterCodeModule * mod, Declaration* dec) { if (!dec->mLinkerObject) @@ -601,7 +599,7 @@ void InterCodeGenerator::TranslateAssembler(InterCodeModule* mod, Expression* ex assert(offset == osize); } -void InterCodeGenerator::BuildSwitchTree(InterCodeProcedure* proc, InterCodeBasicBlock* block, ExValue v, const SwitchNodeArray& nodes, int left, int right, InterCodeBasicBlock* dblock) +void InterCodeGenerator::BuildSwitchTree(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock* block, ExValue v, const SwitchNodeArray& nodes, int left, int right, InterCodeBasicBlock* dblock) { if (right - left < 5) { @@ -610,16 +608,14 @@ void InterCodeGenerator::BuildSwitchTree(InterCodeProcedure* proc, InterCodeBasi InterCodeBasicBlock* cblock = new InterCodeBasicBlock(); proc->Append(cblock); - InterInstruction* vins = new InterInstruction(); - vins->mCode = IC_CONSTANT; + InterInstruction* vins = new InterInstruction(exp->mLocation, IC_CONSTANT); vins->mConst.mType = IT_INT16; vins->mConst.mIntConst = nodes[i].mValue; vins->mDst.mType = IT_INT16; vins->mDst.mTemp = proc->AddTemporary(vins->mDst.mType); block->Append(vins); - InterInstruction* cins = new InterInstruction(); - cins->mCode = IC_RELATIONAL_OPERATOR; + InterInstruction* cins = new InterInstruction(exp->mLocation, IC_RELATIONAL_OPERATOR); cins->mOperator = IA_CMPEQ; cins->mSrc[0].mType = vins->mDst.mType; cins->mSrc[0].mTemp = vins->mDst.mTemp; @@ -630,8 +626,7 @@ void InterCodeGenerator::BuildSwitchTree(InterCodeProcedure* proc, InterCodeBasi block->Append(cins); - InterInstruction* bins = new InterInstruction(); - bins->mCode = IC_BRANCH; + InterInstruction* bins = new InterInstruction(exp->mLocation, IC_BRANCH); bins->mSrc[0].mType = IT_BOOL; bins->mSrc[0].mTemp = cins->mDst.mTemp; block->Append(bins); @@ -641,8 +636,7 @@ void InterCodeGenerator::BuildSwitchTree(InterCodeProcedure* proc, InterCodeBasi block = cblock; } - InterInstruction* jins = new InterInstruction(); - jins->mCode = IC_JUMP; + InterInstruction* jins = new InterInstruction(exp->mLocation, IC_JUMP); block->Append(jins); block->Close(dblock, nullptr); @@ -660,16 +654,14 @@ void InterCodeGenerator::BuildSwitchTree(InterCodeProcedure* proc, InterCodeBasi InterCodeBasicBlock* lblock = new InterCodeBasicBlock(); proc->Append(lblock); - InterInstruction* vins = new InterInstruction(); - vins->mCode = IC_CONSTANT; + InterInstruction* vins = new InterInstruction(exp->mLocation, IC_CONSTANT); vins->mConst.mType = IT_INT16; vins->mConst.mIntConst = nodes[center].mValue; vins->mDst.mType = IT_INT16; vins->mDst.mTemp = proc->AddTemporary(vins->mDst.mType); block->Append(vins); - InterInstruction* cins = new InterInstruction(); - cins->mCode = IC_RELATIONAL_OPERATOR; + InterInstruction* cins = new InterInstruction(exp->mLocation, IC_RELATIONAL_OPERATOR); cins->mOperator = IA_CMPEQ; cins->mSrc[0].mType = vins->mDst.mType; cins->mSrc[0].mTemp = vins->mDst.mTemp; @@ -680,16 +672,14 @@ void InterCodeGenerator::BuildSwitchTree(InterCodeProcedure* proc, InterCodeBasi block->Append(cins); - InterInstruction* bins = new InterInstruction(); - bins->mCode = IC_BRANCH; + InterInstruction* bins = new InterInstruction(exp->mLocation, IC_BRANCH); bins->mSrc[0].mType = IT_BOOL; bins->mSrc[0].mTemp = cins->mDst.mTemp; block->Append(bins); block->Close(nodes[center].mBlock, cblock); - InterInstruction* rins = new InterInstruction(); - rins->mCode = IC_RELATIONAL_OPERATOR; + InterInstruction* rins = new InterInstruction(exp->mLocation, IC_RELATIONAL_OPERATOR); rins->mOperator = IA_CMPLS; rins->mSrc[0].mType = vins->mDst.mType; rins->mSrc[0].mTemp = vins->mDst.mTemp; @@ -700,16 +690,15 @@ void InterCodeGenerator::BuildSwitchTree(InterCodeProcedure* proc, InterCodeBasi cblock->Append(rins); - InterInstruction* rbins = new InterInstruction(); - rbins->mCode = IC_BRANCH; + InterInstruction* rbins = new InterInstruction(exp->mLocation, IC_BRANCH); rbins->mSrc[0].mType = IT_BOOL; rbins->mSrc[0].mTemp = rins->mDst.mTemp; cblock->Append(rbins); cblock->Close(lblock, rblock); - BuildSwitchTree(proc, lblock, v, nodes, left, center, dblock); - BuildSwitchTree(proc, rblock, v, nodes, center + 1, right, dblock); + BuildSwitchTree(proc, exp, lblock, v, nodes, left, center, dblock); + BuildSwitchTree(proc, exp, rblock, v, nodes, center + 1, right, dblock); } } @@ -737,8 +726,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* { case DT_CONST_INTEGER: { - InterInstruction * ins = new InterInstruction(); - ins->mCode = IC_CONSTANT; + InterInstruction * ins = new InterInstruction(exp->mLocation, IC_CONSTANT); ins->mDst.mType = InterTypeOf(dec->mBase); ins->mDst.mTemp = proc->AddTemporary(ins->mDst.mType); if (ins->mDst.mType == IT_INT8) @@ -797,8 +785,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* case DT_CONST_FLOAT: { - InterInstruction * ins = new InterInstruction(); - ins->mCode = IC_CONSTANT; + InterInstruction * ins = new InterInstruction(exp->mLocation, IC_CONSTANT); ins->mDst.mType = InterTypeOf(dec->mBase); ins->mDst.mTemp = proc->AddTemporary(ins->mDst.mType); ins->mConst.mFloatConst = dec->mNumber; @@ -809,8 +796,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* case DT_CONST_ADDRESS: { - InterInstruction * ins = new InterInstruction(); - ins->mCode = IC_CONSTANT; + InterInstruction * ins = new InterInstruction(exp->mLocation, IC_CONSTANT); ins->mDst.mType = IT_POINTER; ins->mDst.mTemp = proc->AddTemporary(IT_POINTER); ins->mConst.mIntConst = dec->mInteger; @@ -827,8 +813,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* InterCodeProcedure* cproc = this->TranslateProcedure(proc->mModule, dec->mValue, dec); } - InterInstruction * ins = new InterInstruction(); - ins->mCode = IC_CONSTANT; + InterInstruction * ins = new InterInstruction(exp->mLocation, IC_CONSTANT); ins->mDst.mType = InterTypeOf(dec->mBase); ins->mDst.mTemp = proc->AddTemporary(ins->mDst.mType); ins->mConst.mVarIndex = dec->mVarIndex; @@ -845,8 +830,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (!dec->mLinkerObject) TranslateAssembler(proc->mModule, dec->mValue, nullptr); - InterInstruction* ins = new InterInstruction(); - ins->mCode = IC_CONSTANT; + InterInstruction* ins = new InterInstruction(exp->mLocation, IC_CONSTANT); ins->mDst.mType = IT_POINTER; ins->mDst.mTemp = proc->AddTemporary(ins->mDst.mType); ins->mConst.mVarIndex = dec->mVarIndex; @@ -882,8 +866,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* proc->mModule->mGlobalVars.Push(var); } - InterInstruction * ins = new InterInstruction(); - ins->mCode = IC_CONSTANT; + InterInstruction * ins = new InterInstruction(exp->mLocation, IC_CONSTANT); ins->mDst.mType = IT_POINTER; ins->mDst.mTemp = proc->AddTemporary(IT_POINTER); ins->mConst.mIntConst = 0; @@ -914,8 +897,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* BuildInitializer(proc->mModule, d, 0, dec, var); } - InterInstruction * ins = new InterInstruction(); - ins->mCode = IC_CONSTANT; + InterInstruction * ins = new InterInstruction(exp->mLocation, IC_CONSTANT); ins->mDst.mType = IT_POINTER; ins->mDst.mTemp = proc->AddTemporary(IT_POINTER); ins->mConst.mIntConst = 0; @@ -937,8 +919,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* { dec = exp->mDecValue; - InterInstruction * ins = new InterInstruction(); - ins->mCode = IC_CONSTANT; + InterInstruction * ins = new InterInstruction(exp->mLocation, IC_CONSTANT); ins->mDst.mType = IT_POINTER; ins->mDst.mTemp = proc->AddTemporary(ins->mDst.mType); ins->mConst.mOperandSize = dec->mSize; @@ -983,6 +964,8 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (inlineMapper) ins->mConst.mVarIndex += inlineMapper->mVarIndex; + InitLocalVariable(proc, dec, ins->mConst.mVarIndex); + ins->mConst.mMemory = IM_LOCAL; } @@ -1020,8 +1003,8 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (vl.mType->mType == DT_TYPE_STRUCT || vl.mType->mType == DT_TYPE_ARRAY || vl.mType->mType == DT_TYPE_UNION) { - vr = Dereference(proc, block, vr, 1); - vl = Dereference(proc, block, vl, 1); + vr = Dereference(proc, exp, block, vr, 1); + vl = Dereference(proc, exp, block, vl, 1); if (vl.mReference != 1) mErrors->Error(exp->mLeft->mLocation, EERR_NOT_AN_LVALUE, "Not a left hand expression"); @@ -1030,8 +1013,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (vr.mTemp != vl.mTemp) { - InterInstruction* ins = new InterInstruction(); - ins->mCode = IC_COPY; + InterInstruction* ins = new InterInstruction(exp->mLocation, IC_COPY); ins->mSrc[0].mType = IT_POINTER; ins->mSrc[0].mTemp = vr.mTemp; ins->mSrc[0].mMemory = IM_INDIRECT; @@ -1048,28 +1030,27 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* { if (vl.mType->mType == DT_TYPE_POINTER && (vr.mType->mType == DT_TYPE_ARRAY || vr.mType->mType == DT_TYPE_FUNCTION)) { - vr = Dereference(proc, block, vr, 1); + vr = Dereference(proc, exp, block, vr, 1); vr.mReference = 0; vr.mType = vl.mType; } else - vr = Dereference(proc, block, vr); + vr = Dereference(proc, exp, block, vr); - vl = Dereference(proc, block, vl, 1); + vl = Dereference(proc, exp, block, vl, 1); if (vl.mReference != 1) mErrors->Error(exp->mLeft->mLocation, EERR_NOT_AN_LVALUE, "Not a left hand expression"); - InterInstruction * ins = new InterInstruction(); + InterInstruction * ins = new InterInstruction(exp->mLocation, IC_STORE); if (exp->mToken != TK_ASSIGN) { - ExValue vll = Dereference(proc, block, vl); + ExValue vll = Dereference(proc, exp, block, vl); if (vl.mType->mType == DT_TYPE_POINTER) { - InterInstruction * cins = new InterInstruction(); - cins->mCode = IC_CONSTANT; + InterInstruction * cins = new InterInstruction(exp->mLocation, IC_CONSTANT); if (exp->mToken == TK_ASSIGN_ADD) { @@ -1089,10 +1070,9 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* cins->mDst.mTemp = proc->AddTemporary(cins->mDst.mType); block->Append(cins); - vr = CoerceType(proc, block, vr, TheSignedIntTypeDeclaration); + vr = CoerceType(proc, exp, block, vr, TheSignedIntTypeDeclaration); - InterInstruction * mins = new InterInstruction(); - mins->mCode = IC_BINARY_OPERATOR; + InterInstruction * mins = new InterInstruction(exp->mLocation, IC_BINARY_OPERATOR); mins->mOperator = IA_MUL; mins->mSrc[0].mType = IT_INT16; mins->mSrc[0].mTemp = vr.mTemp; @@ -1102,8 +1082,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* mins->mDst.mTemp = proc->AddTemporary(mins->mDst.mType); block->Append(mins); - InterInstruction * ains = new InterInstruction(); - ains->mCode = IC_LEA; + InterInstruction * ains = new InterInstruction(exp->mLocation, IC_LEA); ains->mSrc[1].mMemory = IM_INDIRECT; ains->mSrc[0].mType = IT_INT16; ains->mSrc[0].mTemp = mins->mDst.mTemp; @@ -1129,13 +1108,12 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* otype = TheSignedIntTypeDeclaration; else otype = TheUnsignedIntTypeDeclaration; - vll = CoerceType(proc, block, vll, otype); + vll = CoerceType(proc, exp, block, vll, otype); } - vr = CoerceType(proc, block, vr, otype); + vr = CoerceType(proc, exp, block, vr, otype); - InterInstruction * oins = new InterInstruction(); - oins->mCode = IC_BINARY_OPERATOR; + InterInstruction * oins = new InterInstruction(exp->mLocation, IC_BINARY_OPERATOR); oins->mSrc[0].mType = InterTypeOf(otype); oins->mSrc[0].mTemp = vr.mTemp; oins->mSrc[1].mType = InterTypeOf(otype); @@ -1196,12 +1174,12 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* block->Append(oins); - vr = CoerceType(proc, block, vr, vl.mType); + vr = CoerceType(proc, exp, block, vr, vl.mType); } } else { - vr = CoerceType(proc, block, vr, vl.mType); + vr = CoerceType(proc, exp, block, vr, vl.mType); } ins->mCode = IC_STORE; @@ -1233,8 +1211,8 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* } } - vl = Dereference(proc, block, vl, vl.mType->mType == DT_TYPE_POINTER ? 0 : 1); - vr = Dereference(proc, block, vr); + vl = Dereference(proc, exp, block, vl, vl.mType->mType == DT_TYPE_POINTER ? 0 : 1); + vr = Dereference(proc, exp, block, vr); if (vl.mType->mType != DT_TYPE_ARRAY && vl.mType->mType != DT_TYPE_POINTER) mErrors->Error(exp->mLocation, EERR_INVALID_INDEX, "Invalid type for indexing"); @@ -1242,17 +1220,15 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (!vr.mType->IsIntegerType()) mErrors->Error(exp->mLocation, EERR_INVALID_INDEX, "Index operand is not integral number"); - vr = CoerceType(proc, block, vr, TheSignedIntTypeDeclaration); + vr = CoerceType(proc, exp, block, vr, TheSignedIntTypeDeclaration); - InterInstruction * cins = new InterInstruction(); - cins->mCode = IC_CONSTANT; + InterInstruction * cins = new InterInstruction(exp->mLocation, IC_CONSTANT); cins->mConst.mIntConst = vl.mType->mBase->mSize; cins->mDst.mType = IT_INT16; cins->mDst.mTemp = proc->AddTemporary(cins->mDst.mType); block->Append(cins); - InterInstruction * mins = new InterInstruction(); - mins->mCode = IC_BINARY_OPERATOR; + InterInstruction * mins = new InterInstruction(exp->mLocation, IC_BINARY_OPERATOR); mins->mOperator = IA_MUL; mins->mSrc[0].mType = IT_INT16; mins->mSrc[0].mTemp = vr.mTemp; @@ -1262,8 +1238,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* mins->mDst.mTemp = proc->AddTemporary(mins->mDst.mType); block->Append(mins); - InterInstruction * ains = new InterInstruction(); - ains->mCode = IC_LEA; + InterInstruction * ains = new InterInstruction(exp->mLocation, IC_LEA); ains->mSrc[1].mMemory = IM_INDIRECT; ains->mSrc[0].mType = IT_INT16; ains->mSrc[0].mTemp = mins->mDst.mTemp; @@ -1279,20 +1254,18 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* case EX_QUALIFY: { vl = TranslateExpression(procType, proc, block, exp->mLeft, breakBlock, continueBlock, inlineMapper); - vl = Dereference(proc, block, vl, 1); + vl = Dereference(proc, exp, block, vl, 1); if (vl.mReference != 1) mErrors->Error(exp->mLocation, EERR_NOT_AN_LVALUE, "Not an addressable expression"); - InterInstruction * cins = new InterInstruction(); - cins->mCode = IC_CONSTANT; + InterInstruction * cins = new InterInstruction(exp->mLocation, IC_CONSTANT); cins->mConst.mIntConst = exp->mDecValue->mOffset; cins->mDst.mType = IT_INT16; cins->mDst.mTemp = proc->AddTemporary(cins->mDst.mType); block->Append(cins); - InterInstruction * ains = new InterInstruction(); - ains->mCode = IC_LEA; + InterInstruction * ains = new InterInstruction(exp->mLocation, IC_LEA); ains->mSrc[1].mMemory = IM_INDIRECT; ains->mSrc[0].mType = IT_INT16; ains->mSrc[0].mTemp = cins->mDst.mTemp; @@ -1309,17 +1282,17 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* { vl = TranslateExpression(procType, proc, block, exp->mLeft, breakBlock, continueBlock, inlineMapper); vr = TranslateExpression(procType, proc, block, exp->mRight, breakBlock, continueBlock, inlineMapper); - vr = Dereference(proc, block, vr); + vr = Dereference(proc, exp, block, vr); - InterInstruction * ins = new InterInstruction(); + InterInstruction * ins = new InterInstruction(exp->mLocation, IC_BINARY_OPERATOR); if (vl.mType->mType == DT_TYPE_POINTER || vl.mType->mType == DT_TYPE_ARRAY) { if (vl.mType->mType == DT_TYPE_POINTER) - vl = Dereference(proc, block, vl); + vl = Dereference(proc, exp, block, vl); else { - vl = Dereference(proc, block, vl, 1); + vl = Dereference(proc, exp, block, vl, 1); Declaration* ptype = new Declaration(exp->mLocation, DT_TYPE_POINTER); ptype->mBase = vl.mType->mBase; @@ -1329,8 +1302,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (vr.mType->IsIntegerType()) { - InterInstruction * cins = new InterInstruction(); - cins->mCode = IC_CONSTANT; + InterInstruction * cins = new InterInstruction(exp->mLocation, IC_CONSTANT); if (exp->mToken == TK_ADD) { @@ -1347,9 +1319,9 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* cins->mDst.mTemp = proc->AddTemporary(cins->mDst.mType); block->Append(cins); - vr = CoerceType(proc, block, vr, TheSignedIntTypeDeclaration); + vr = CoerceType(proc, exp, block, vr, TheSignedIntTypeDeclaration); - InterInstruction * mins = new InterInstruction(); + InterInstruction * mins = new InterInstruction(exp->mLocation, IC_BINARY_OPERATOR); mins->mCode = IC_BINARY_OPERATOR; mins->mOperator = IA_MUL; mins->mSrc[0].mType = IT_INT16; @@ -1374,30 +1346,26 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* { if (exp->mToken == TK_SUB) { - InterInstruction * clins = new InterInstruction(), * crins = new InterInstruction(); - clins->mCode = IC_TYPECAST; + InterInstruction * clins = new InterInstruction(exp->mLocation, IC_TYPECAST), * crins = new InterInstruction(exp->mLocation, IC_TYPECAST); clins->mSrc[0].mTemp = vl.mTemp; clins->mSrc[0].mType = IT_POINTER; clins->mDst.mType = IT_INT16; clins->mDst.mTemp = proc->AddTemporary(clins->mDst.mType); block->Append(clins); - crins->mCode = IC_TYPECAST; crins->mSrc[0].mTemp = vr.mTemp; crins->mSrc[0].mType = IT_POINTER; crins->mDst.mType = IT_INT16; crins->mDst.mTemp = proc->AddTemporary(crins->mDst.mType); block->Append(crins); - InterInstruction * cins = new InterInstruction(); - cins->mCode = IC_CONSTANT; + InterInstruction * cins = new InterInstruction(exp->mLocation, IC_CONSTANT); cins->mConst.mIntConst = vl.mType->mBase->mSize; cins->mDst.mType = IT_INT16; cins->mDst.mTemp = proc->AddTemporary(cins->mDst.mType); block->Append(cins); - InterInstruction * sins = new InterInstruction(), * dins = new InterInstruction(); - sins->mCode = IC_BINARY_OPERATOR; + InterInstruction * sins = new InterInstruction(exp->mLocation, IC_BINARY_OPERATOR), * dins = new InterInstruction(exp->mLocation, IC_BINARY_OPERATOR); sins->mOperator = IA_SUB; sins->mSrc[0].mType = IT_INT16; sins->mSrc[0].mTemp = crins->mDst.mTemp; @@ -1407,7 +1375,6 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* sins->mDst.mTemp = proc->AddTemporary(sins->mDst.mType); block->Append(sins); - dins->mCode = IC_BINARY_OPERATOR; dins->mOperator = IA_DIVS; dins->mSrc[0].mType = IT_INT16; dins->mSrc[0].mTemp = cins->mDst.mTemp; @@ -1428,10 +1395,10 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* else if (vr.mType->mType == DT_TYPE_POINTER || vr.mType->mType == DT_TYPE_ARRAY) { if (vr.mType->mType == DT_TYPE_POINTER) - vr = Dereference(proc, block, vr); + vr = Dereference(proc, exp, block, vr); else { - vr = Dereference(proc, block, vr, 1); + vr = Dereference(proc, exp, block, vr, 1); Declaration* ptype = new Declaration(exp->mLocation, DT_TYPE_POINTER); ptype->mBase = vr.mType->mBase; @@ -1441,8 +1408,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (vl.mType->IsIntegerType()) { - InterInstruction* cins = new InterInstruction(); - cins->mCode = IC_CONSTANT; + InterInstruction* cins = new InterInstruction(exp->mLocation, IC_CONSTANT); if (exp->mToken == TK_ADD) { @@ -1455,10 +1421,9 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* cins->mDst.mTemp = proc->AddTemporary(cins->mDst.mType); block->Append(cins); - vl = CoerceType(proc, block, vl, TheSignedIntTypeDeclaration); + vl = CoerceType(proc, exp, block, vl, TheSignedIntTypeDeclaration); - InterInstruction* mins = new InterInstruction(); - mins->mCode = IC_BINARY_OPERATOR; + InterInstruction* mins = new InterInstruction(exp->mLocation, IC_BINARY_OPERATOR); mins->mOperator = IA_MUL; mins->mSrc[0].mType = IT_INT16; mins->mSrc[0].mTemp = vl.mTemp; @@ -1485,7 +1450,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* } else { - vl = Dereference(proc, block, vl); + vl = Dereference(proc, exp, block, vl); if (!vl.mType->IsNumericType()) mErrors->Error(exp->mLocation, EERR_INCOMPATIBLE_OPERATOR, "Left hand operand type is not numeric"); @@ -1556,8 +1521,8 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* dtype = TheUnsignedCharTypeDeclaration; } - vl = CoerceType(proc, block, vl, dtype); - vr = CoerceType(proc, block, vr, dtype); + vl = CoerceType(proc, exp, block, vl, dtype); + vr = CoerceType(proc, exp, block, vr, dtype); bool signedOP = dtype->mFlags & DTF_SIGNED; @@ -1612,7 +1577,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* case EX_PREINCDEC: { vl = TranslateExpression(procType, proc, block, exp->mLeft, breakBlock, continueBlock, inlineMapper); - vl = Dereference(proc, block, vl, 1); + vl = Dereference(proc, exp, block, vl, 1); if (vl.mReference != 1) mErrors->Error(exp->mLeft->mLocation, EERR_NOT_AN_LVALUE, "Not a left hand expression"); @@ -1620,13 +1585,12 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (vl.mType->mFlags & DTF_CONST) mErrors->Error(exp->mLocation, EERR_CONST_ASSIGN, "Cannot change const value"); - InterInstruction * cins = new InterInstruction(), * ains = new InterInstruction(), * sins = new InterInstruction(); + InterInstruction * cins = new InterInstruction(exp->mLocation, IC_CONSTANT), * ains = new InterInstruction(exp->mLocation, IC_BINARY_OPERATOR), * sins = new InterInstruction(exp->mLocation, IC_STORE); - ExValue vdl = Dereference(proc, block, vl); + ExValue vdl = Dereference(proc, exp, block, vl); bool ftype = vl.mType->mType == DT_TYPE_FLOAT; - cins->mCode = IC_CONSTANT; cins->mDst.mType = ftype ? IT_FLOAT : IT_INT16; cins->mDst.mTemp = proc->AddTemporary(cins->mDst.mType); if (vdl.mType->mType == DT_TYPE_POINTER) @@ -1657,7 +1621,6 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* ains->mDst.mTemp = proc->AddTemporary(ains->mDst.mType); block->Append(ains); - sins->mCode = IC_STORE; sins->mSrc[1].mMemory = IM_INDIRECT; sins->mSrc[0].mType = ains->mDst.mType; sins->mSrc[0].mTemp = ains->mDst.mTemp; @@ -1674,7 +1637,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* case EX_POSTINCDEC: { vl = TranslateExpression(procType, proc, block, exp->mLeft, breakBlock, continueBlock, inlineMapper); - vl = Dereference(proc, block, vl, 1); + vl = Dereference(proc, exp, block, vl, 1); if (vl.mReference != 1) mErrors->Error(exp->mLeft->mLocation, EERR_NOT_AN_LVALUE, "Not a left hand expression"); @@ -1682,13 +1645,12 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (vl.mType->mFlags & DTF_CONST) mErrors->Error(exp->mLocation, EERR_CONST_ASSIGN, "Cannot change const value"); - InterInstruction * cins = new InterInstruction(), * ains = new InterInstruction(), * sins = new InterInstruction(); + InterInstruction * cins = new InterInstruction(exp->mLocation, IC_CONSTANT), * ains = new InterInstruction(exp->mLocation, IC_BINARY_OPERATOR), * sins = new InterInstruction(exp->mLocation, IC_STORE); - ExValue vdl = Dereference(proc, block, vl); + ExValue vdl = Dereference(proc, exp, block, vl); bool ftype = vl.mType->mType == DT_TYPE_FLOAT; - cins->mCode = IC_CONSTANT; cins->mDst.mType = ftype ? IT_FLOAT : IT_INT16; cins->mDst.mTemp = proc->AddTemporary(cins->mDst.mType); if (vdl.mType->mType == DT_TYPE_POINTER) @@ -1718,7 +1680,6 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* ains->mDst.mTemp = proc->AddTemporary(ttype); block->Append(ains); - sins->mCode = IC_STORE; sins->mSrc[1].mMemory = IM_INDIRECT; sins->mSrc[0].mType = ains->mDst.mType; sins->mSrc[0].mTemp = ains->mDst.mTemp; @@ -1736,30 +1697,28 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* { vl = TranslateExpression(procType, proc, block, exp->mLeft, breakBlock, continueBlock, inlineMapper); - InterInstruction * ins = new InterInstruction(); - - ins->mCode = IC_UNARY_OPERATOR; + InterInstruction * ins = new InterInstruction(exp->mLocation, IC_UNARY_OPERATOR); switch (exp->mToken) { case TK_ADD: - vl = Dereference(proc, block, vl); + vl = Dereference(proc, exp, block, vl); ins->mOperator = IA_NONE; break; case TK_SUB: - vl = Dereference(proc, block, vl); + vl = Dereference(proc, exp, block, vl); if (!vl.mType->IsNumericType()) mErrors->Error(exp->mLocation, EERR_INCOMPATIBLE_OPERATOR, "Not a numeric type"); else if (vl.mType->mType == DT_TYPE_INTEGER && vl.mType->mSize < 2) - vl = CoerceType(proc, block, vl, TheSignedIntTypeDeclaration); + vl = CoerceType(proc, exp, block, vl, TheSignedIntTypeDeclaration); ins->mOperator = IA_NEG; break; case TK_BINARY_NOT: - vl = Dereference(proc, block, vl); + vl = Dereference(proc, exp, block, vl); if (!(vl.mType->mType == DT_TYPE_POINTER || vl.mType->IsNumericType())) mErrors->Error(exp->mLocation, EERR_INCOMPATIBLE_OPERATOR, "Not a numeric or pointer type"); else if (vl.mType->mType == DT_TYPE_INTEGER && vl.mType->mSize < 2) - vl = CoerceType(proc, block, vl, TheUnsignedIntTypeDeclaration); + vl = CoerceType(proc, exp, block, vl, TheUnsignedIntTypeDeclaration); ins->mOperator = IA_NOT; break; case TK_MUL: @@ -1792,11 +1751,11 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* case EX_RELATIONAL: { vl = TranslateExpression(procType, proc, block, exp->mLeft, breakBlock, continueBlock, inlineMapper); - vl = Dereference(proc, block, vl, vl.mType->mType == DT_TYPE_ARRAY ? 1 : 0); + vl = Dereference(proc, exp, block, vl, vl.mType->mType == DT_TYPE_ARRAY ? 1 : 0); vr = TranslateExpression(procType, proc, block, exp->mRight, breakBlock, continueBlock, inlineMapper); - vr = Dereference(proc, block, vr, vr.mType->mType == DT_TYPE_ARRAY ? 1 : 0); + vr = Dereference(proc, exp, block, vr, vr.mType->mType == DT_TYPE_ARRAY ? 1 : 0); - InterInstruction * ins = new InterInstruction(); + InterInstruction * ins = new InterInstruction(exp->mLocation, IC_RELATIONAL_OPERATOR); Declaration* dtype = TheSignedIntTypeDeclaration; @@ -1848,12 +1807,11 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* dtype = TheUnsignedIntTypeDeclaration; } - vl = CoerceType(proc, block, vl, dtype); - vr = CoerceType(proc, block, vr, dtype); + vl = CoerceType(proc, exp, block, vl, dtype); + vr = CoerceType(proc, exp, block, vr, dtype); bool signedCompare = dtype->mFlags & DTF_SIGNED; - ins->mCode = IC_RELATIONAL_OPERATOR; switch (exp->mToken) { case TK_EQUAL: @@ -1897,14 +1855,13 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (!strcmp(iname->mString, "fabs")) { vr = TranslateExpression(procType, proc, block, exp->mRight, breakBlock, continueBlock, inlineMapper); - vr = Dereference(proc, block, vr); + vr = Dereference(proc, exp, block, vr); if (decf->mBase->mParams->CanAssign(vr.mType)) mErrors->Error(exp->mLocation, EERR_INCOMPATIBLE_TYPES, "Cannot assign incompatible types"); - vr = CoerceType(proc, block, vr, decf->mBase->mParams); + vr = CoerceType(proc, exp, block, vr, decf->mBase->mParams); - InterInstruction * ins = new InterInstruction(); - ins->mCode = IC_UNARY_OPERATOR; + InterInstruction * ins = new InterInstruction(exp->mLocation, IC_UNARY_OPERATOR); ins->mOperator = IA_ABS; ins->mSrc[0].mType = IT_FLOAT; ins->mSrc[0].mTemp = vr.mTemp; @@ -1917,14 +1874,13 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* else if (!strcmp(iname->mString, "floor")) { vr = TranslateExpression(procType, proc, block, exp->mRight, breakBlock, continueBlock, inlineMapper); - vr = Dereference(proc, block, vr); + vr = Dereference(proc, exp, block, vr); if (decf->mBase->mParams->CanAssign(vr.mType)) mErrors->Error(exp->mLocation, EERR_INCOMPATIBLE_TYPES, "Cannot assign incompatible types"); - vr = CoerceType(proc, block, vr, decf->mBase->mParams); + vr = CoerceType(proc, exp, block, vr, decf->mBase->mParams); - InterInstruction * ins = new InterInstruction(); - ins->mCode = IC_UNARY_OPERATOR; + InterInstruction * ins = new InterInstruction(exp->mLocation, IC_UNARY_OPERATOR); ins->mOperator = IA_FLOOR; ins->mSrc[0].mType = IT_FLOAT; ins->mSrc[0].mTemp = vr.mTemp; @@ -1937,14 +1893,13 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* else if (!strcmp(iname->mString, "ceil")) { vr = TranslateExpression(procType, proc, block, exp->mRight, breakBlock, continueBlock, inlineMapper); - vr = Dereference(proc, block, vr); + vr = Dereference(proc, exp, block, vr); if (decf->mBase->mParams->CanAssign(vr.mType)) mErrors->Error(exp->mLocation, EERR_INCOMPATIBLE_TYPES, "Cannot assign incompatible types"); - vr = CoerceType(proc, block, vr, decf->mBase->mParams); + vr = CoerceType(proc, exp, block, vr, decf->mBase->mParams); - InterInstruction * ins = new InterInstruction(); - ins->mCode = IC_UNARY_OPERATOR; + InterInstruction * ins = new InterInstruction(exp->mLocation, IC_UNARY_OPERATOR); ins->mOperator = IA_CEIL; ins->mSrc[0].mType = IT_FLOAT; ins->mSrc[0].mTemp = vr.mTemp; @@ -1964,23 +1919,22 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* { vl = TranslateExpression(procType, proc, block, tex, breakBlock, continueBlock, inlineMapper); if (vl.mType->mType == DT_TYPE_ARRAY) - vl = Dereference(proc, block, vl, 1); + vl = Dereference(proc, exp, block, vl, 1); else - vl = Dereference(proc, block, vl); + vl = Dereference(proc, exp, block, vl); vr = TranslateExpression(procType, proc, block, sex, breakBlock, continueBlock, inlineMapper); if (vr.mType->mType == DT_TYPE_ARRAY) - vr = Dereference(proc, block, vr, 1); + vr = Dereference(proc, exp, block, vr, 1); else - vr = Dereference(proc, block, vr); + vr = Dereference(proc, exp, block, vr); if (!TheCharPointerTypeDeclaration->CanAssign(vl.mType)) mErrors->Error(tex->mLocation, EERR_INCOMPATIBLE_TYPES, "Cannot assign incompatible types"); if (!TheConstCharPointerTypeDeclaration->CanAssign(vr.mType)) mErrors->Error(sex->mLocation, EERR_INCOMPATIBLE_TYPES, "Cannot assign incompatible types"); - InterInstruction* ins = new InterInstruction(); - ins->mCode = IC_STRCPY; + InterInstruction* ins = new InterInstruction(exp->mLocation, IC_STRCPY); ins->mSrc[0].mType = IT_POINTER; ins->mSrc[0].mTemp = vr.mTemp; ins->mSrc[1].mType = IT_POINTER; @@ -2000,23 +1954,22 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* { vl = TranslateExpression(procType, proc, block, tex, breakBlock, continueBlock, inlineMapper); if (vl.mType->mType == DT_TYPE_ARRAY) - vl = Dereference(proc, block, vl, 1); + vl = Dereference(proc, exp, block, vl, 1); else - vl = Dereference(proc, block, vl); + vl = Dereference(proc, exp, block, vl); vr = TranslateExpression(procType, proc, block, sex, breakBlock, continueBlock, inlineMapper); if (vr.mType->mType == DT_TYPE_ARRAY) - vr = Dereference(proc, block, vr, 1); + vr = Dereference(proc, exp, block, vr, 1); else - vr = Dereference(proc, block, vr); + vr = Dereference(proc, exp, block, vr); if (!TheCharPointerTypeDeclaration->CanAssign(vl.mType)) mErrors->Error(tex->mLocation, EERR_INCOMPATIBLE_TYPES, "Cannot assign incompatible types"); if (!TheConstCharPointerTypeDeclaration->CanAssign(vr.mType)) mErrors->Error(sex->mLocation, EERR_INCOMPATIBLE_TYPES, "Cannot assign incompatible types"); - InterInstruction* ins = new InterInstruction(); - ins->mCode = IC_COPY; + InterInstruction* ins = new InterInstruction(exp->mLocation, IC_COPY); ins->mSrc[0].mType = IT_POINTER; ins->mSrc[0].mTemp = vr.mTemp; ins->mSrc[1].mType = IT_POINTER; @@ -2096,8 +2049,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* int nindex = proc->mNumLocals++; Declaration* vdec = new Declaration(pex->mLocation, DT_VARIABLE); - InterInstruction* ains = new InterInstruction(); - ains->mCode = IC_CONSTANT; + InterInstruction* ains = new InterInstruction(pex->mLocation, IC_CONSTANT); ains->mDst.mType = IT_POINTER; ains->mDst.mTemp = proc->AddTemporary(ains->mDst.mType); ains->mConst.mMemory = IM_LOCAL; @@ -2145,15 +2097,14 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (pdec && !pdec->mBase->CanAssign(vr.mType)) mErrors->Error(texp->mLocation, EERR_INCOMPATIBLE_TYPES, "Cannot assign incompatible types"); - vr = Dereference(proc, block, vr, 1); + vr = Dereference(proc, texp, block, vr, 1); if (vr.mReference != 1) mErrors->Error(exp->mLeft->mLocation, EERR_NOT_AN_LVALUE, "Not an adressable expression"); if (vp.mTemp != vr.mTemp) { - InterInstruction* cins = new InterInstruction(); - cins->mCode = IC_COPY; + InterInstruction* cins = new InterInstruction(exp->mLocation, IC_COPY); cins->mSrc[0].mType = IT_POINTER; cins->mSrc[0].mTemp = vr.mTemp; cins->mSrc[0].mMemory = IM_INDIRECT; @@ -2169,23 +2120,22 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* else { if (vr.mType->mType == DT_TYPE_ARRAY || vr.mType->mType == DT_TYPE_FUNCTION) - vr = Dereference(proc, block, vr, 1); + vr = Dereference(proc, texp, block, vr, 1); else - vr = Dereference(proc, block, vr); + vr = Dereference(proc, texp, block, vr); if (pdec) { if (!pdec->mBase->CanAssign(vr.mType)) mErrors->Error(texp->mLocation, EERR_INCOMPATIBLE_TYPES, "Cannot assign incompatible types"); - vr = CoerceType(proc, block, vr, pdec->mBase); + vr = CoerceType(proc, texp, block, vr, pdec->mBase); } else if (vr.mType->IsIntegerType() && vr.mType->mSize < 2) { - vr = CoerceType(proc, block, vr, TheSignedIntTypeDeclaration); + vr = CoerceType(proc, texp, block, vr, TheSignedIntTypeDeclaration); } - InterInstruction* wins = new InterInstruction(); - wins->mCode = IC_STORE; + InterInstruction* wins = new InterInstruction(texp->mLocation, IC_STORE); wins->mSrc[1].mMemory = IM_INDIRECT; wins->mSrc[0].mType = InterTypeOf(vr.mType);; wins->mSrc[0].mTemp = vr.mTemp; @@ -2226,8 +2176,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* vl = TranslateExpression(ftype, proc, block, fexp, nullptr, nullptr, &nmapper); - InterInstruction* jins = new InterInstruction(); - jins->mCode = IC_JUMP; + InterInstruction* jins = new InterInstruction(exp->mLocation, IC_JUMP); block->Append(jins); block->Close(nmapper.mReturn, nullptr); @@ -2235,8 +2184,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (rdec) { - InterInstruction* ins = new InterInstruction(); - ins->mCode = IC_CONSTANT; + InterInstruction* ins = new InterInstruction(exp->mLocation, IC_CONSTANT); ins->mDst.mType = IT_POINTER; ins->mDst.mTemp = proc->AddTemporary(ins->mDst.mType); ins->mConst.mOperandSize = rdec->mSize; @@ -2254,7 +2202,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* else { vl = TranslateExpression(procType, proc, block, exp->mLeft, breakBlock, continueBlock, inlineMapper); - vl = Dereference(proc, block, vl); + vl = Dereference(proc, exp, block, vl); int atotal = 0; @@ -2272,8 +2220,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* } else { - fins = new InterInstruction(); - fins->mCode = IC_PUSH_FRAME; + fins = new InterInstruction(exp->mLocation, IC_PUSH_FRAME); fins->mConst.mIntConst = atotal; block->Append(fins); } @@ -2301,8 +2248,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* decResult = vdec; - InterInstruction* vins = new InterInstruction(); - vins->mCode = IC_CONSTANT; + InterInstruction* vins = new InterInstruction(exp->mLocation, IC_CONSTANT); vins->mDst.mType = IT_POINTER; vins->mDst.mTemp = proc->AddTemporary(IT_POINTER); vins->mConst.mMemory = IM_LOCAL; @@ -2313,8 +2259,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* ttemp = vins->mDst.mTemp; } - InterInstruction* ains = new InterInstruction(); - ains->mCode = IC_CONSTANT; + InterInstruction* ains = new InterInstruction(exp->mLocation, IC_CONSTANT); ains->mDst.mType = IT_POINTER; ains->mDst.mTemp = proc->AddTemporary(IT_POINTER); ains->mConst.mVarIndex = 0; @@ -2329,8 +2274,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* ains->mConst.mMemory = IM_FRAME; block->Append(ains); - InterInstruction* wins = new InterInstruction(); - wins->mCode = IC_STORE; + InterInstruction* wins = new InterInstruction(exp->mLocation, IC_STORE); wins->mSrc[1].mMemory = IM_INDIRECT; wins->mSrc[0].mType = IT_POINTER; wins->mSrc[0].mTemp = ttemp; @@ -2353,8 +2297,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* Expression* pex = exp->mRight; while (pex) { - InterInstruction * ains = new InterInstruction(); - ains->mCode = IC_CONSTANT; + InterInstruction * ains = new InterInstruction(exp->mLocation, IC_CONSTANT); ains->mDst.mType = IT_POINTER; ains->mDst.mTemp = proc->AddTemporary(ains->mDst.mType); if (pdec) @@ -2410,15 +2353,14 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (pdec && !pdec->mBase->CanAssign(vr.mType)) mErrors->Error(texp->mLocation, EERR_INCOMPATIBLE_TYPES, "Cannot assign incompatible types"); - vr = Dereference(proc, block, vr, 1); + vr = Dereference(proc, texp, block, vr, 1); if (vr.mReference != 1) mErrors->Error(exp->mLeft->mLocation, EERR_NOT_AN_LVALUE, "Not an adressable expression"); if (vp.mTemp != vr.mTemp) { - InterInstruction* cins = new InterInstruction(); - cins->mCode = IC_COPY; + InterInstruction* cins = new InterInstruction(texp->mLocation, IC_COPY); cins->mSrc[0].mType = IT_POINTER; cins->mSrc[0].mTemp = vr.mTemp; cins->mSrc[0].mMemory = IM_INDIRECT; @@ -2436,24 +2378,23 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* else { if (vr.mType->mType == DT_TYPE_ARRAY || vr.mType->mType == DT_TYPE_FUNCTION) - vr = Dereference(proc, block, vr, 1); + vr = Dereference(proc, texp, block, vr, 1); else - vr = Dereference(proc, block, vr); + vr = Dereference(proc, texp, block, vr); if (pdec) { if (!pdec->mBase->CanAssign(vr.mType)) mErrors->Error(texp->mLocation, EERR_INCOMPATIBLE_TYPES, "Cannot assign incompatible types"); - vr = CoerceType(proc, block, vr, pdec->mBase); + vr = CoerceType(proc, texp, block, vr, pdec->mBase); } else if (vr.mType->IsIntegerType() && vr.mType->mSize < 2) { - vr = CoerceType(proc, block, vr, TheSignedIntTypeDeclaration); + vr = CoerceType(proc, texp, block, vr, TheSignedIntTypeDeclaration); } - InterInstruction* wins = new InterInstruction(); - wins->mCode = IC_STORE; + InterInstruction* wins = new InterInstruction(texp->mLocation, IC_STORE); wins->mSrc[1].mMemory = IM_INDIRECT; wins->mSrc[0].mType = InterTypeOf(vr.mType);; wins->mSrc[0].mTemp = vr.mTemp; @@ -2489,7 +2430,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* for (int i = 0; i < defins.Size(); i++) block->Append(defins[i]); - InterInstruction * cins = new InterInstruction(); + InterInstruction * cins = new InterInstruction(exp->mLocation, IC_CALL); if (exp->mLeft->mDecValue && exp->mLeft->mDecValue->mFlags & DTF_NATIVE) cins->mCode = IC_CALL_NATIVE; else @@ -2513,8 +2454,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* { fins->mConst.mIntConst = atotal; - InterInstruction* xins = new InterInstruction(); - xins->mCode = IC_POP_FRAME; + InterInstruction* xins = new InterInstruction(exp->mLocation, IC_POP_FRAME); xins->mConst.mIntConst = atotal; block->Append(xins); } @@ -2524,8 +2464,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (lrexp) return *lrexp; - InterInstruction* vins = new InterInstruction(); - vins->mCode = IC_CONSTANT; + InterInstruction* vins = new InterInstruction(exp->mLocation, IC_CONSTANT); vins->mDst.mType = IT_POINTER; vins->mDst.mTemp = proc->AddTemporary(IT_POINTER); vins->mConst.mMemory = IM_LOCAL; @@ -2552,8 +2491,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* { dec->mLinkerObject->mFlags |= LOBJF_INLINE; - InterInstruction* ins = new InterInstruction(); - ins->mCode = IC_CONSTANT; + InterInstruction* ins = new InterInstruction(exp->mLocation, IC_CONSTANT); ins->mDst.mType = IT_POINTER; ins->mDst.mTemp = proc->AddTemporary(ins->mDst.mType); ins->mConst.mOperandSize = dec->mSize; @@ -2563,16 +2501,14 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* ins->mConst.mVarIndex = dec->mVarIndex; block->Append(ins); - InterInstruction * jins = new InterInstruction(); - jins->mCode = IC_ASSEMBLER; + InterInstruction * jins = new InterInstruction(exp->mLocation, IC_ASSEMBLER); jins->mSrc[0].mType = IT_POINTER; jins->mSrc[0].mTemp = ins->mDst.mTemp; jins->mNumOperands = 1; for (int i = 0; i < refvars.Size(); i++) { - InterInstruction* vins = new InterInstruction(); - vins->mCode = IC_CONSTANT; + InterInstruction* vins = new InterInstruction(exp->mLocation, IC_CONSTANT); vins->mDst.mType = IT_POINTER; vins->mDst.mTemp = proc->AddTemporary(ins->mDst.mType); @@ -2605,8 +2541,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* block->Append(vins); - InterInstruction* lins = new InterInstruction(); - lins->mCode = IC_LOAD; + InterInstruction* lins = new InterInstruction(exp->mLocation, IC_LOAD); lins->mSrc[0].mMemory = IM_INDIRECT; lins->mSrc[0].mType = IT_POINTER; lins->mSrc[0].mTemp = vins->mDst.mTemp; @@ -2628,14 +2563,14 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* case EX_RETURN: { - InterInstruction * ins = new InterInstruction(); + InterInstruction * ins = new InterInstruction(exp->mLocation, IC_RETURN); if (exp->mLeft) { vr = TranslateExpression(procType, proc, block, exp->mLeft, breakBlock, continueBlock, inlineMapper); if (procType->mBase->mType == DT_TYPE_STRUCT) { - vr = Dereference(proc, block, vr, 1); + vr = Dereference(proc, exp, block, vr, 1); if (!procType->mBase || procType->mBase->mType == DT_TYPE_VOID) mErrors->Error(exp->mLocation, EERR_INVALID_RETURN, "Function has void return type"); @@ -2644,7 +2579,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* else if (vr.mReference != 1) mErrors->Error(exp->mLocation, EERR_INVALID_RETURN, "Non addressable object"); - InterInstruction* ains = new InterInstruction(); + InterInstruction* ains = new InterInstruction(exp->mLocation, IC_CONSTANT); if (inlineMapper) { @@ -2661,8 +2596,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* } else { - InterInstruction* pins = new InterInstruction(); - pins->mCode = IC_CONSTANT; + InterInstruction* pins = new InterInstruction(exp->mLocation, IC_CONSTANT); pins->mDst.mType = IT_POINTER; pins->mDst.mTemp = proc->AddTemporary(IT_POINTER); pins->mConst.mVarIndex = 0; @@ -2688,8 +2622,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* ins->mCode = IC_RETURN; } - InterInstruction* cins = new InterInstruction(); - cins->mCode = IC_COPY; + InterInstruction* cins = new InterInstruction(exp->mLocation, IC_COPY); cins->mSrc[0].mType = IT_POINTER; cins->mSrc[0].mTemp = vr.mTemp; cins->mSrc[0].mMemory = IM_INDIRECT; @@ -2703,22 +2636,21 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* } else { - vr = Dereference(proc, block, vr); + vr = Dereference(proc, exp, block, vr); if (!procType->mBase || procType->mBase->mType == DT_TYPE_VOID) mErrors->Error(exp->mLocation, EERR_INVALID_RETURN, "Function has void return type"); else if (!procType->mBase->CanAssign(vr.mType)) mErrors->Error(exp->mLocation, EERR_INVALID_RETURN, "Cannot return incompatible type"); - vr = CoerceType(proc, block, vr, procType->mBase); + vr = CoerceType(proc, exp, block, vr, procType->mBase); ins->mSrc[0].mType = InterTypeOf(vr.mType); ins->mSrc[0].mTemp = vr.mTemp; if (inlineMapper) { - InterInstruction* ains = new InterInstruction(); - ains->mCode = IC_CONSTANT; + InterInstruction* ains = new InterInstruction(exp->mLocation, IC_CONSTANT); ains->mDst.mType = IT_POINTER; ains->mDst.mTemp = proc->AddTemporary(ins->mDst.mType); ains->mConst.mOperandSize = procType->mBase->mSize; @@ -2753,8 +2685,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (inlineMapper) { - InterInstruction* jins = new InterInstruction(); - jins->mCode = IC_JUMP; + InterInstruction* jins = new InterInstruction(exp->mLocation, IC_JUMP); block->Append(jins); block->Close(inlineMapper->mReturn, nullptr); } @@ -2770,8 +2701,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* { if (breakBlock) { - InterInstruction * jins = new InterInstruction(); - jins->mCode = IC_JUMP; + InterInstruction * jins = new InterInstruction(exp->mLocation, IC_JUMP); block->Append(jins); block->Close(breakBlock, nullptr); @@ -2788,8 +2718,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* { if (continueBlock) { - InterInstruction * jins = new InterInstruction(); - jins->mCode = IC_JUMP; + InterInstruction * jins = new InterInstruction(exp->mLocation, IC_JUMP); block->Append(jins); block->Close(continueBlock, nullptr); @@ -2812,8 +2741,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* TranslateLogic(procType, proc, block, tblock, fblock, exp->mLeft, inlineMapper); - InterInstruction* ins = new InterInstruction(); - ins->mCode = IC_UNREACHABLE; + InterInstruction* ins = new InterInstruction(exp->mLocation, IC_UNREACHABLE); fblock->Append(ins); fblock->Close(nullptr, nullptr); @@ -2824,17 +2752,15 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* case EX_LOGICAL_NOT: { vl = TranslateExpression(procType, proc, block, exp->mLeft, breakBlock, continueBlock, inlineMapper); - vl = Dereference(proc, block, vl); + vl = Dereference(proc, exp, block, vl); - InterInstruction * zins = new InterInstruction(); - zins->mCode = IC_CONSTANT; + InterInstruction * zins = new InterInstruction(exp->mLocation, IC_CONSTANT); zins->mDst.mType = InterTypeOf(vl.mType); zins->mDst.mTemp = proc->AddTemporary(zins->mDst.mType); zins->mConst.mIntConst = 0; block->Append(zins); - InterInstruction * ins = new InterInstruction(); - ins->mCode = IC_RELATIONAL_OPERATOR; + InterInstruction * ins = new InterInstruction(exp->mLocation, IC_RELATIONAL_OPERATOR); ins->mOperator = IA_CMPEQ; ins->mSrc[0].mType = InterTypeOf(vl.mType); ins->mSrc[0].mTemp = zins->mDst.mTemp; @@ -2857,9 +2783,9 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* vl = TranslateExpression(procType, proc, block, exp->mRight->mLeft, breakBlock, continueBlock, inlineMapper); vr = TranslateExpression(procType, proc, block, exp->mRight->mRight, breakBlock, continueBlock, inlineMapper); - vc = Dereference(proc, block, vc); - vl = Dereference(proc, block, vl); - vr = Dereference(proc, block, vr); + vc = Dereference(proc, exp, block, vc); + vl = Dereference(proc, exp, block, vl); + vr = Dereference(proc, exp, block, vr); int ttemp; InterType ttype, stypel, styper; @@ -2871,14 +2797,14 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (stypel == IT_POINTER || styper == IT_POINTER) { if (vl.mType->mType == DT_TYPE_ARRAY) - vl = Dereference(proc, block, vl, 1); + vl = Dereference(proc, exp, block, vl, 1); else - vl = Dereference(proc, block, vl); + vl = Dereference(proc, exp, block, vl); if (vr.mType->mType == DT_TYPE_ARRAY) - vr = Dereference(proc, block, vr, 1); + vr = Dereference(proc, exp, block, vr, 1); else - vr = Dereference(proc, block, vr); + vr = Dereference(proc, exp, block, vr); if (vl.mType->mBase->IsSubType(vr.mType->mBase)) dtype = vr.mType; @@ -2909,23 +2835,22 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* ttype = stypel; dtype = vl.mType; - vr = CoerceType(proc, block, vr, dtype); + vr = CoerceType(proc, exp, block, vr, dtype); } else { ttype = styper; dtype = vr.mType; - vl = CoerceType(proc, block, vl, dtype); + vl = CoerceType(proc, exp, block, vl, dtype); } } - vc = CoerceType(proc, block, vc, TheBoolTypeDeclaration); + vc = CoerceType(proc, exp, block, vc, TheBoolTypeDeclaration); ttemp = proc->AddTemporary(ttype); - InterInstruction* sins = new InterInstruction(); - sins->mCode = IC_SELECT; + InterInstruction* sins = new InterInstruction(exp->mLocation, IC_SELECT); sins->mSrc[2].mType = InterTypeOf(vc.mType); sins->mSrc[2].mTemp = vc.mTemp; sins->mSrc[1].mType = ttype; @@ -2941,10 +2866,8 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* else #endif { - InterInstruction* jins0 = new InterInstruction(); - jins0->mCode = IC_JUMP; - InterInstruction* jins1 = new InterInstruction(); - jins1->mCode = IC_JUMP; + InterInstruction* jins0 = new InterInstruction(exp->mLocation, IC_JUMP); + InterInstruction* jins1 = new InterInstruction(exp->mLocation, IC_JUMP); InterCodeBasicBlock* tblock = new InterCodeBasicBlock(); proc->Append(tblock); @@ -2968,14 +2891,14 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (stypel == IT_POINTER || styper == IT_POINTER) { if (vl.mType->mType == DT_TYPE_ARRAY) - vl = Dereference(proc, tblock, vl, 1); + vl = Dereference(proc, exp, tblock, vl, 1); else - vl = Dereference(proc, tblock, vl); + vl = Dereference(proc, exp, tblock, vl); if (vr.mType->mType == DT_TYPE_ARRAY) - vr = Dereference(proc, fblock, vr, 1); + vr = Dereference(proc, exp, fblock, vr, 1); else - vr = Dereference(proc, fblock, vr); + vr = Dereference(proc, exp, fblock, vr); if (vl.mType->mBase->IsSubType(vr.mType->mBase)) dtype = vr.mType; @@ -2995,8 +2918,8 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* } else { - vl = Dereference(proc, tblock, vl); - vr = Dereference(proc, fblock, vr); + vl = Dereference(proc, exp, tblock, vl); + vr = Dereference(proc, exp, fblock, vr); if (stypel == styper) { @@ -3008,29 +2931,27 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* ttype = stypel; dtype = vl.mType; - vr = CoerceType(proc, fblock, vr, dtype); + vr = CoerceType(proc, exp, fblock, vr, dtype); } else { ttype = styper; dtype = vr.mType; - vl = CoerceType(proc, tblock, vl, dtype); + vl = CoerceType(proc, exp, tblock, vl, dtype); } } ttemp = proc->AddTemporary(ttype); - InterInstruction* rins = new InterInstruction(); - rins->mCode = IC_LOAD_TEMPORARY; + InterInstruction* rins = new InterInstruction(exp->mLocation, IC_LOAD_TEMPORARY); rins->mSrc[0].mType = ttype; rins->mSrc[0].mTemp = vr.mTemp; rins->mDst.mType = ttype; rins->mDst.mTemp = ttemp; fblock->Append(rins); - InterInstruction* lins = new InterInstruction(); - lins->mCode = IC_LOAD_TEMPORARY; + InterInstruction* lins = new InterInstruction(exp->mLocation, IC_LOAD_TEMPORARY); lins->mSrc[0].mType = ttype; lins->mSrc[0].mTemp = vl.mTemp; lins->mDst.mType = ttype; @@ -3055,11 +2976,11 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* { vr = TranslateExpression(procType, proc, block, exp->mRight, breakBlock, continueBlock, inlineMapper); - InterInstruction * ins = new InterInstruction(); + InterInstruction * ins = new InterInstruction(exp->mLocation, IC_CONVERSION_OPERATOR); if (exp->mLeft->mDecType->mType == DT_TYPE_FLOAT && vr.mType->IsIntegerType()) { - vr = Dereference(proc, block, vr); + vr = Dereference(proc, exp, block, vr); int stemp = vr.mTemp; @@ -3067,8 +2988,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* { if (vr.mType->mFlags & DTF_SIGNED) { - InterInstruction* xins = new InterInstruction(); - xins->mCode = IC_CONVERSION_OPERATOR; + InterInstruction* xins = new InterInstruction(exp->mLocation, IC_CONVERSION_OPERATOR); xins->mOperator = IA_EXT8TO16S; xins->mSrc[0].mType = IT_INT8; xins->mSrc[0].mTemp = stemp; @@ -3079,8 +2999,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* } else { - InterInstruction* xins = new InterInstruction(); - xins->mCode = IC_CONVERSION_OPERATOR; + InterInstruction* xins = new InterInstruction(exp->mLocation, IC_CONVERSION_OPERATOR); xins->mOperator = IA_EXT8TO16U; xins->mSrc[0].mType = IT_INT8; xins->mSrc[0].mTemp = stemp; @@ -3091,7 +3010,6 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* } } - ins->mCode = IC_CONVERSION_OPERATOR; ins->mOperator = IA_INT2FLOAT; ins->mSrc[0].mType = IT_INT16; ins->mSrc[0].mTemp = stemp; @@ -3101,8 +3019,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* } else if (exp->mLeft->mDecType->IsIntegerType() && vr.mType->mType == DT_TYPE_FLOAT) { - vr = Dereference(proc, block, vr); - ins->mCode = IC_CONVERSION_OPERATOR; + vr = Dereference(proc, exp, block, vr); ins->mOperator = IA_FLOAT2INT; ins->mSrc[0].mType = InterTypeOf(vr.mType); ins->mSrc[0].mTemp = vr.mTemp; @@ -3112,8 +3029,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (exp->mLeft->mDecType->mSize == 1) { - InterInstruction* xins = new InterInstruction(); - xins->mCode = IC_TYPECAST; + InterInstruction* xins = new InterInstruction(exp->mLocation, IC_TYPECAST); xins->mSrc[0].mType = IT_INT16; xins->mSrc[0].mTemp = ins->mDst.mTemp; xins->mDst.mType = InterTypeOf(exp->mLeft->mDecType); @@ -3139,12 +3055,12 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* } else if (exp->mLeft->mDecType->IsIntegerType() && vr.mType->IsIntegerType()) { - vr = Dereference(proc, block, vr); - return CoerceType(proc, block, vr, exp->mLeft->mDecType); + vr = Dereference(proc, exp, block, vr); + return CoerceType(proc, exp, block, vr, exp->mLeft->mDecType); } else { - vr = Dereference(proc, block, vr); + vr = Dereference(proc, exp, block, vr); ins->mCode = IC_TYPECAST; ins->mSrc[0].mType = InterTypeOf(vr.mType); ins->mSrc[0].mTemp = vr.mTemp; @@ -3160,10 +3076,8 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* case EX_LOGICAL_AND: case EX_LOGICAL_OR: { - InterInstruction* jins0 = new InterInstruction(); - jins0->mCode = IC_JUMP; - InterInstruction* jins1 = new InterInstruction(); - jins1->mCode = IC_JUMP; + InterInstruction* jins0 = new InterInstruction(exp->mLocation, IC_JUMP); + InterInstruction* jins1 = new InterInstruction(exp->mLocation, IC_JUMP); InterCodeBasicBlock* tblock = new InterCodeBasicBlock(); proc->Append(tblock); @@ -3176,15 +3090,13 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* int ttemp = proc->AddTemporary(IT_BOOL); - InterInstruction* tins = new InterInstruction(); - tins->mCode = IC_CONSTANT; + InterInstruction* tins = new InterInstruction(exp->mLocation, IC_CONSTANT); tins->mConst.mIntConst = 1; tins->mDst.mType = IT_BOOL; tins->mDst.mTemp = ttemp; tblock->Append(tins); - InterInstruction* fins = new InterInstruction(); - fins->mCode = IC_CONSTANT; + InterInstruction* fins = new InterInstruction(exp->mLocation, IC_CONSTANT); fins->mConst.mIntConst = 0; fins->mDst.mType = IT_BOOL; fins->mDst.mTemp = ttemp; @@ -3204,10 +3116,8 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* case EX_WHILE: { - InterInstruction * jins0 = new InterInstruction(); - jins0->mCode = IC_JUMP; - InterInstruction* jins1 = new InterInstruction(); - jins1->mCode = IC_JUMP; + InterInstruction * jins0 = new InterInstruction(exp->mLocation, IC_JUMP); + InterInstruction* jins1 = new InterInstruction(exp->mLocation, IC_JUMP); InterCodeBasicBlock* cblock = new InterCodeBasicBlock(); InterCodeBasicBlock* lblock = cblock; @@ -3233,10 +3143,8 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* case EX_IF: { - InterInstruction * jins0 = new InterInstruction(); - jins0->mCode = IC_JUMP; - InterInstruction* jins1 = new InterInstruction(); - jins1->mCode = IC_JUMP; + InterInstruction * jins0 = new InterInstruction(exp->mLocation, IC_JUMP); + InterInstruction* jins1 = new InterInstruction(exp->mLocation, IC_JUMP); InterCodeBasicBlock* tblock = new InterCodeBasicBlock(); proc->Append(tblock); @@ -3267,14 +3175,10 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (exp->mLeft->mRight) TranslateExpression(procType, proc, block, exp->mLeft->mRight, breakBlock, continueBlock, inlineMapper); - InterInstruction * jins0 = new InterInstruction(); - jins0->mCode = IC_JUMP; - InterInstruction* jins1 = new InterInstruction(); - jins1->mCode = IC_JUMP; - InterInstruction* jins2 = new InterInstruction(); - jins2->mCode = IC_JUMP; - InterInstruction* jins3 = new InterInstruction(); - jins3->mCode = IC_JUMP; + InterInstruction* jins0 = new InterInstruction(exp->mLocation, IC_JUMP); + InterInstruction* jins1 = new InterInstruction(exp->mLocation, IC_JUMP); + InterInstruction* jins2 = new InterInstruction(exp->mLocation, IC_JUMP); + InterInstruction* jins3 = new InterInstruction(exp->mLocation, IC_JUMP); InterCodeBasicBlock* cblock = new InterCodeBasicBlock(); InterCodeBasicBlock* lblock = cblock; @@ -3317,8 +3221,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* case EX_DO: { - InterInstruction * jins = new InterInstruction(); - jins->mCode = IC_JUMP; + InterInstruction * jins = new InterInstruction(exp->mLocation, IC_JUMP); InterCodeBasicBlock* cblock = new InterCodeBasicBlock(); InterCodeBasicBlock* lblock = cblock; @@ -3341,8 +3244,8 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* case EX_SWITCH: { vl = TranslateExpression(procType, proc, block, exp->mLeft, breakBlock, continueBlock, inlineMapper); - vl = Dereference(proc, block, vl); - vl = CoerceType(proc, block, vl, TheSignedIntTypeDeclaration); + vl = Dereference(proc, exp, block, vl); + vl = CoerceType(proc, exp, block, vl, TheSignedIntTypeDeclaration); InterCodeBasicBlock * dblock = nullptr; InterCodeBasicBlock* sblock = block; @@ -3382,8 +3285,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (block) { - InterInstruction* jins = new InterInstruction(); - jins->mCode = IC_JUMP; + InterInstruction* jins = new InterInstruction(exp->mLocation, IC_JUMP); block->Append(jins); block->Close(nblock, nullptr); @@ -3400,8 +3302,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (block) { - InterInstruction* jins = new InterInstruction(); - jins->mCode = IC_JUMP; + InterInstruction* jins = new InterInstruction(exp->mLocation, IC_JUMP); block->Append(jins); block->Close(dblock, nullptr); @@ -3419,12 +3320,11 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* sexp = sexp->mRight; } - BuildSwitchTree(proc, sblock, vl, switchNodes, 0, switchNodes.Size(), dblock ? dblock : eblock); + BuildSwitchTree(proc, exp, sblock, vl, switchNodes, 0, switchNodes.Size(), dblock ? dblock : eblock); if (block) { - InterInstruction* jins = new InterInstruction(); - jins->mCode = IC_JUMP; + InterInstruction* jins = new InterInstruction(exp->mLocation, IC_JUMP); block->Append(jins); block->Close(eblock, nullptr); @@ -3608,10 +3508,9 @@ void InterCodeGenerator::TranslateLogic(Declaration* procType, InterCodeProcedur default: { ExValue vr = TranslateExpression(procType, proc, block, exp, nullptr, nullptr, inlineMapper); - vr = Dereference(proc, block, vr); + vr = Dereference(proc, exp, block, vr); - InterInstruction * ins = new InterInstruction(); - ins->mCode = IC_BRANCH; + InterInstruction * ins = new InterInstruction(exp->mLocation, IC_BRANCH); ins->mSrc[0].mType = InterTypeOf(vr.mType); ins->mSrc[0].mTemp = vr.mTemp; block->Append(ins); @@ -3667,8 +3566,7 @@ InterCodeProcedure* InterCodeGenerator::TranslateProcedure(InterCodeModule * mod else mErrors->Error(dec->mLocation, EERR_UNDEFINED_OBJECT, "Calling undefined function", dec->mIdent->mString); - InterInstruction * ins = new InterInstruction(); - ins->mCode = IC_RETURN; + InterInstruction * ins = new InterInstruction(exp->mLocation, IC_RETURN); exitBlock->Append(ins); exitBlock->Close(nullptr, nullptr); diff --git a/oscar64/InterCodeGenerator.h b/oscar64/InterCodeGenerator.h index c8df044..29ed04d 100644 --- a/oscar64/InterCodeGenerator.h +++ b/oscar64/InterCodeGenerator.h @@ -26,6 +26,7 @@ public: InterCodeProcedure* TranslateProcedure(InterCodeModule* mod, Expression* exp, Declaration * dec); void TranslateAssembler(InterCodeModule* mod, Expression * exp, GrowingArray * refvars); void InitGlobalVariable(InterCodeModule* mod, Declaration* dec); + void InitLocalVariable(InterCodeProcedure* proc, Declaration* dec, int index); protected: Errors* mErrors; @@ -51,10 +52,10 @@ protected: typedef GrowingArray 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 CoerceType(InterCodeProcedure* proc, InterCodeBasicBlock*& block, ExValue v, Declaration * type); + 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); 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); diff --git a/oscar64/NumberSet.cpp b/oscar64/NumberSet.cpp index 9cf3792..7833fad 100644 --- a/oscar64/NumberSet.cpp +++ b/oscar64/NumberSet.cpp @@ -147,7 +147,7 @@ NumberSet& NumberSet::operator-=(const NumberSet& set) return *this; } -bool NumberSet::operator<=(const NumberSet& set) +bool NumberSet::operator<=(const NumberSet& set) const { int i; diff --git a/oscar64/NumberSet.h b/oscar64/NumberSet.h index e060b34..624e398 100644 --- a/oscar64/NumberSet.h +++ b/oscar64/NumberSet.h @@ -28,7 +28,7 @@ public: 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);