diff --git a/oscar64/Array.h b/oscar64/Array.h index 84b3c5d..6615ed4 100644 --- a/oscar64/Array.h +++ b/oscar64/Array.h @@ -1,6 +1,7 @@ #pragma once #include +#include "MachineTypes.h" template class DynamicArray @@ -147,6 +148,7 @@ public: } }; + template class GrowingArray { @@ -231,7 +233,7 @@ public: return array[n]; } - T operator[](int n) const + const T & operator[](int n) const { assert(n >= 0); if (n >= size) return empty; @@ -361,3 +363,207 @@ public: return false; } }; + +template +class ExpandingArray +{ +public: +protected: + int size, range; + T * array; + + void Grow(int to, bool clear) + { + T* a2; + int i; + + if (clear) size = 0; + + if (to > range) + { + if (to > range * 2) + range = to; + else + range = range * 2; + + a2 = new T[range]; + if (to > size) + for (i = 0; i < size; i++) a2[i] = array[i]; + else + for (i = 0; i < to; i++) a2[i] = array[i]; + + delete[] array; + array = a2; + } + + size = to; + } + +public: + ExpandingArray(void) + { + size = 0; + range = 4; + array = new T[range]; + } + + ExpandingArray(const ExpandingArray& a) + { + size = a.size; + range = a.range; + array = new T[range]; + + int lsize = size; + const T* sap = a.array; + T* dap = array; + for (int i = 0; i < lsize; i++) dap[i] = sap[i]; + } + + ExpandingArray& operator=(const ExpandingArray& a) + { + if (a.size != size) + Grow(a.size, true); + + int lsize = size; + const T* sap = a.array; + T* dap = array; + for (int i = 0; i < lsize; i++) dap[i] = sap[i]; + + return *this; + } + + ~ExpandingArray(void) + { + delete[] array; + } + + void Push(const T & t) + { + int s = size; + Grow(size + 1, false); + array[s] = t; + } + + T Pop(void) + { + assert(size > 0); + return array[--size]; + } + + void Insert(int at, T t) + { + Grow(size + 1, false); + int j = size - 1; + while (j > at) + { + array[j] = array[j - 1]; + j--; + } + array[at] = t; + } + + void Remove(int at) + { + while (at + 1 < size) + { + array[at] = array[at + 1]; + at++; + } + Grow(at, false); + } + + void Remove(int at, int n) + { + while (at + n < size) + { + array[at] = array[at + n]; + at++; + } + Grow(at, false); + } + + int RemoveAll(const T& t) + { + int j = 0, i = 0; + while (i < size) + { + if (array[i] != t) + { + if (i != j) + array[j] = array[i]; + j++; + } + i++; + } + + Grow(j, false); + + return i - j; + } + + + T Top(void) const + { + return array[size - 1]; + } + + bool IsEmpty(void) const { return size == 0; } + + int Size(void) const { return size; } + + T Last() const + { + assert(size > 0); + return array[size - 1]; + } + + void SetSize(int size, bool clear = false) + { + Grow(size, clear); + } + + void Reserve(int to) + { + if (to > range) + { + range = to; + + T* a2 = new T[range]; + if (to > size) + for (int i = 0; i < size; i++) a2[i] = array[i]; + else + for (int i = 0; i < to; i++) a2[i] = array[i]; + + delete[] array; + array = a2; + } + } + + int IndexOf(const T& t) const + { + for (int i = 0; i < size; i++) + if (array[i] == t) + return i; + return -1; + } + + bool Contains(const T& t) const + { + for (int i = 0; i < size; i++) + if (array[i] == t) + return true; + return false; + } + + __forceinline T& operator[](int n) + { + assert(n >= 0 && n < size); + return array[n]; + } + + __forceinline const T& operator[](int n) const + { + assert(n >= 0 && n < size); + return array[n]; + } +}; diff --git a/oscar64/InterCode.cpp b/oscar64/InterCode.cpp index 4444095..6a5ab98 100644 --- a/oscar64/InterCode.cpp +++ b/oscar64/InterCode.cpp @@ -1001,6 +1001,7 @@ static void LoadConstantFold(InterInstruction* ins, InterInstruction* ains, cons ins->mCode = IC_CONSTANT; ins->mConst.mType = ins->mDst.mType; ins->mSrc[0].mTemp = -1; + ins->mNumOperands = 0; } void ValueSet::InsertValue(InterInstruction * ins) @@ -1728,6 +1729,7 @@ void ValueSet::UpdateValue(InterInstruction * ins, const GrowingInstructionPtrAr ins->mSrc[0].mTemp = -1; ins->mSrc[0].mType = mInstructions[i]->mSrc[0].mType; ins->mConst.mIntConst = mInstructions[i]->mSrc[0].mIntConst; + ins->mNumOperands = 0; } else { @@ -1874,6 +1876,7 @@ void ValueSet::UpdateValue(InterInstruction * ins, const GrowingInstructionPtrAr ins->mConst.mFloatConst = ConstantFolding(ins->mOperator, tvalue[ins->mSrc[1].mTemp]->mConst.mFloatConst, tvalue[ins->mSrc[0].mTemp]->mConst.mFloatConst); ins->mSrc[0].mTemp = -1; ins->mSrc[1].mTemp = -1; + ins->mNumOperands = 0; i = 0; while (i < mNum && @@ -1936,6 +1939,7 @@ void ValueSet::UpdateValue(InterInstruction * ins, const GrowingInstructionPtrAr ins->mConst.mIntConst = ConstantFolding(ins->mOperator, ins->mDst.mType, tvalue[ins->mSrc[1].mTemp]->mConst.mIntConst, tvalue[ins->mSrc[0].mTemp]->mConst.mIntConst); ins->mSrc[0].mTemp = -1; ins->mSrc[1].mTemp = -1; + ins->mNumOperands = 0; UpdateValue(ins, tvalue, aliasedLocals, aliasedParams, staticVars); @@ -1967,6 +1971,7 @@ void ValueSet::UpdateValue(InterInstruction * ins, const GrowingInstructionPtrAr ins->mConst.mIntConst = 0; ins->mSrc[0].mTemp = -1; ins->mSrc[1].mTemp = -1; + ins->mNumOperands = 0; UpdateValue(ins, tvalue, aliasedLocals, aliasedParams, staticVars); @@ -2007,6 +2012,7 @@ void ValueSet::UpdateValue(InterInstruction * ins, const GrowingInstructionPtrAr ins->mConst.mIntConst = 0; ins->mSrc[0].mTemp = -1; ins->mSrc[1].mTemp = -1; + ins->mNumOperands = 0; UpdateValue(ins, tvalue, aliasedLocals, aliasedParams, staticVars); @@ -2042,6 +2048,7 @@ void ValueSet::UpdateValue(InterInstruction * ins, const GrowingInstructionPtrAr ins->mConst.mIntConst = 0; ins->mSrc[0].mTemp = -1; ins->mSrc[1].mTemp = -1; + ins->mNumOperands = 0; UpdateValue(ins, tvalue, aliasedLocals, aliasedParams, staticVars); @@ -2156,6 +2163,7 @@ void ValueSet::UpdateValue(InterInstruction * ins, const GrowingInstructionPtrAr ins->mCode = IC_CONSTANT; ins->mConst.mFloatConst = ConstantFolding(ins->mOperator, tvalue[ins->mSrc[0].mTemp]->mConst.mFloatConst); ins->mSrc[0].mTemp = -1; + ins->mNumOperands = 0; i = 0; while (i < mNum && @@ -2213,6 +2221,7 @@ void ValueSet::UpdateValue(InterInstruction * ins, const GrowingInstructionPtrAr ins->mCode = IC_CONSTANT; ins->mConst.mIntConst = ConstantFolding(ins->mOperator, ins->mDst.mType, tvalue[ins->mSrc[0].mTemp]->mConst.mIntConst); ins->mSrc[0].mTemp = -1; + ins->mNumOperands = 0; i = 0; while (i < mNum && @@ -2275,6 +2284,7 @@ void ValueSet::UpdateValue(InterInstruction * ins, const GrowingInstructionPtrAr ins->mConst.mIntConst = ConstantRelationalFolding(ins->mOperator, tvalue[ins->mSrc[1].mTemp]->mConst.mFloatConst, tvalue[ins->mSrc[0].mTemp]->mConst.mFloatConst); ins->mSrc[0].mTemp = -1; ins->mSrc[1].mTemp = -1; + ins->mNumOperands = 0; UpdateValue(ins, tvalue, aliasedLocals, aliasedParams, staticVars); } @@ -2289,6 +2299,7 @@ void ValueSet::UpdateValue(InterInstruction * ins, const GrowingInstructionPtrAr ins->mConst.mIntConst = ConstantFolding(ins->mOperator, ins->mDst.mType, tvalue[ins->mSrc[1].mTemp]->mConst.mIntConst, tvalue[ins->mSrc[0].mTemp]->mConst.mIntConst); ins->mSrc[0].mTemp = -1; ins->mSrc[1].mTemp = -1; + ins->mNumOperands = 0; UpdateValue(ins, tvalue, aliasedLocals, aliasedParams, staticVars); } @@ -2430,6 +2441,7 @@ void ValueSet::UpdateValue(InterInstruction * ins, const GrowingInstructionPtrAr ins->mConst.mIntConst = cvalue; ins->mSrc[0].mTemp = -1; ins->mSrc[1].mTemp = -1; + ins->mNumOperands = 0; } else { @@ -2565,6 +2577,7 @@ void ValueSet::UpdateValue(InterInstruction * ins, const GrowingInstructionPtrAr ins->mConst.mIntConst = cvalue; ins->mSrc[0].mTemp = -1; ins->mSrc[1].mTemp = -1; + ins->mNumOperands = 0; } else { @@ -2598,6 +2611,7 @@ void ValueSet::UpdateValue(InterInstruction * ins, const GrowingInstructionPtrAr } ins->mSrc[0].mTemp = -1; ins->mSrc[1].mTemp = -1; + ins->mNumOperands = 0; UpdateValue(ins, tvalue, aliasedLocals, aliasedParams, staticVars); } @@ -2612,6 +2626,7 @@ void ValueSet::UpdateValue(InterInstruction * ins, const GrowingInstructionPtrAr else ins->mCode = IC_JUMPF; ins->mSrc[0].mTemp = -1; + ins->mNumOperands = 0; } break; case IC_PUSH_FRAME: @@ -2724,7 +2739,37 @@ InterInstruction::InterInstruction(const Location& loc, InterCode code) { mOperator = IA_NONE; - mNumOperands = 3; + switch (code) + { + case IC_LOAD_TEMPORARY: + case IC_LOAD: + case IC_UNARY_OPERATOR: + case IC_BRANCH: + case IC_TYPECAST: + case IC_RETURN_VALUE: + case IC_RETURN_STRUCT: + case IC_CONVERSION_OPERATOR: + mNumOperands = 1; + break; + + case IC_BINARY_OPERATOR: + case IC_RELATIONAL_OPERATOR: + case IC_STORE: + case IC_LEA: + mNumOperands = 2; + break; + + case IC_CONSTANT: + case IC_JUMP: + case IC_JUMPF: + case IC_RETURN: + mNumOperands = 0; + break; + + default: + mNumOperands = 3; + break; + } mInUse = false; mVolatile = false; @@ -3021,6 +3066,7 @@ bool InterInstruction::PropagateConstTemps(const GrowingInstructionPtrArray& cte mConst.mVarIndex = ains->mConst.mVarIndex; mConst.mMemory = ains->mConst.mMemory; mSrc[0].mTemp = -1; + mNumOperands = 0; return true; } break; @@ -3644,6 +3690,7 @@ void InterInstruction::SimpleLocalToTemp(int vindex, int temp) mCode = IC_CONSTANT; mConst.mIntConst = mSrc[0].mIntConst; mConst.mFloatConst = mSrc[0].mFloatConst; + mNumOperands = 0; } else { @@ -3877,28 +3924,34 @@ void InterInstruction::Disassemble(FILE* file) switch (this->mCode) { case IC_LOAD_TEMPORARY: - case IC_STORE_TEMPORARY: + assert(mNumOperands == 1); fprintf(file, "MOVE"); break; case IC_BINARY_OPERATOR: + assert(mNumOperands == 2); fprintf(file, "BINOP%d", mOperator); break; case IC_UNARY_OPERATOR: + assert(mNumOperands == 1); fprintf(file, "UNOP%d", mOperator); break; case IC_RELATIONAL_OPERATOR: + assert(mNumOperands == 2); fprintf(file, "RELOP%d", mOperator); break; case IC_CONVERSION_OPERATOR: + assert(mNumOperands == 1); fprintf(file, "CONV%d", mOperator); break; case IC_STORE: + assert(mNumOperands == 2); if (mSrc[1].mStride != 1) fprintf(file, "STORE%c%d:%d", memchars[mSrc[1].mMemory], mSrc[1].mOperandSize, mSrc[1].mStride); else fprintf(file, "STORE%c%d", memchars[mSrc[1].mMemory], mSrc[1].mOperandSize); break; case IC_LOAD: + assert(mNumOperands == 1); if (mSrc[0].mStride != 1) fprintf(file, "LOAD%c%d:%d", memchars[mSrc[0].mMemory], mSrc[0].mOperandSize, mSrc[0].mStride); else @@ -3921,24 +3974,31 @@ void InterInstruction::Disassemble(FILE* file) fprintf(file, "STRCPY%c%c", memchars[mSrc[0].mMemory], memchars[mSrc[1].mMemory]); break; case IC_LEA: + assert(mNumOperands == 2); fprintf(file, "LEA%c", memchars[mSrc[1].mMemory]); break; case IC_TYPECAST: + assert(mNumOperands == 1); fprintf(file, "CAST"); break; case IC_SELECT: + assert(mNumOperands == 3); fprintf(file, "SELECT"); break; case IC_CONSTANT: + assert(mNumOperands == 0); fprintf(file, "CONST"); break; case IC_BRANCH: + assert(mNumOperands == 1); fprintf(file, "BRANCH"); break; case IC_JUMP: + assert(mNumOperands == 0); fprintf(file, "JUMP"); break; case IC_JUMPF: + assert(mNumOperands == 0); fprintf(file, "JUMPF"); break; case IC_PUSH_FRAME: @@ -3957,9 +4017,11 @@ void InterInstruction::Disassemble(FILE* file) fprintf(file, "JSR"); break; case IC_RETURN_VALUE: + assert(mNumOperands == 1); fprintf(file, "RETV"); break; case IC_RETURN_STRUCT: + assert(mNumOperands == 1); fprintf(file, "RETS"); break; case IC_RETURN: @@ -4449,6 +4511,7 @@ void InterCodeBasicBlock::CheckValueUsage(InterInstruction * ins, const GrowingI ins->mCode = IC_CONSTANT; ins->mConst.mFloatConst = tvalue[ins->mSrc[0].mTemp]->mConst.mFloatConst; ins->mSrc[0].mTemp = -1; + ins->mNumOperands = 0; break; case IT_POINTER: ins->mCode = IC_CONSTANT; @@ -4458,11 +4521,13 @@ void InterCodeBasicBlock::CheckValueUsage(InterInstruction * ins, const GrowingI ins->mConst.mIntConst = tvalue[ins->mSrc[0].mTemp]->mConst.mIntConst; ins->mConst.mOperandSize = tvalue[ins->mSrc[0].mTemp]->mConst.mOperandSize; ins->mSrc[0].mTemp = -1; + ins->mNumOperands = 0; break; default: ins->mCode = IC_CONSTANT; ins->mConst.mIntConst = tvalue[ins->mSrc[0].mTemp]->mConst.mIntConst; ins->mSrc[0].mTemp = -1; + ins->mNumOperands = 0; break; } } @@ -4512,6 +4577,7 @@ void InterCodeBasicBlock::CheckValueUsage(InterInstruction * ins, const GrowingI ins->mConst.mOperandSize = tvalue[ins->mSrc[1].mTemp]->mConst.mOperandSize; ins->mSrc[0].mTemp = -1; ins->mSrc[1].mTemp = -1; + ins->mNumOperands = 0; } else if (tvalue[ins->mSrc[0].mTemp]->mConst.mIntConst == 0) { @@ -4609,6 +4675,7 @@ void InterCodeBasicBlock::CheckValueUsage(InterInstruction * ins, const GrowingI ins->mConst.mVarIndex = 0; ins->mConst.mIntConst = tvalue[ins->mSrc[0].mTemp]->mConst.mIntConst; ins->mSrc[0].mTemp = -1; + ins->mNumOperands = 0; } } else if (ins->mDst.mType == IT_INT16 && ins->mSrc[0].mType == IT_POINTER) @@ -4622,6 +4689,7 @@ void InterCodeBasicBlock::CheckValueUsage(InterInstruction * ins, const GrowingI ins->mDst.mType = IT_INT16; ins->mConst.mIntConst = cins->mConst.mIntConst; ins->mSrc[0].mTemp = -1; + ins->mNumOperands = 0; } } } @@ -4673,6 +4741,7 @@ void InterCodeBasicBlock::CheckValueUsage(InterInstruction * ins, const GrowingI ins->mConst.mFloatConst = ConstantFolding(ins->mOperator, tvalue[ins->mSrc[1].mTemp]->mConst.mFloatConst, tvalue[ins->mSrc[0].mTemp]->mConst.mFloatConst); ins->mSrc[0].mTemp = -1; ins->mSrc[1].mTemp = -1; + ins->mNumOperands = 0; } else { @@ -4699,6 +4768,7 @@ void InterCodeBasicBlock::CheckValueUsage(InterInstruction * ins, const GrowingI ins->mConst.mFloatConst = 0.0; ins->mSrc[0].mTemp = -1; ins->mSrc[1].mTemp = -1; + ins->mNumOperands = 0; } else if (ins->mSrc[1].mFloatConst == 2.0) { @@ -4738,6 +4808,7 @@ void InterCodeBasicBlock::CheckValueUsage(InterInstruction * ins, const GrowingI ins->mConst.mFloatConst = 0.0; ins->mSrc[0].mTemp = -1; ins->mSrc[1].mTemp = -1; + ins->mNumOperands = 0; } else if (ins->mSrc[0].mFloatConst == 2.0) { @@ -4759,6 +4830,7 @@ void InterCodeBasicBlock::CheckValueUsage(InterInstruction * ins, const GrowingI ins->mConst.mIntConst = ConstantFolding(ins->mOperator, ins->mDst.mType, tvalue[ins->mSrc[1].mTemp]->mConst.mIntConst, tvalue[ins->mSrc[0].mTemp]->mConst.mIntConst); ins->mSrc[0].mTemp = -1; ins->mSrc[1].mTemp = -1; + ins->mNumOperands = 0; } else { @@ -4805,7 +4877,7 @@ void InterCodeBasicBlock::CheckValueUsage(InterInstruction * ins, const GrowingI } else if (ins->mSrc[0].mType == IT_INT32 && ispow2(ins->mSrc[1].mIntConst)) { - __int64 s = ins->mSrc[1].mIntConst; + int64 s = ins->mSrc[1].mIntConst; ins->mOperator = IA_SHL; ins->mSrc[1].mTemp = ins->mSrc[0].mTemp; ins->mSrc[1].mType = ins->mSrc[0].mType; @@ -4862,7 +4934,7 @@ void InterCodeBasicBlock::CheckValueUsage(InterInstruction * ins, const GrowingI } else if (ins->mSrc[1].mType == IT_INT32 && ispow2(ins->mSrc[0].mIntConst)) { - __int64 s = ins->mSrc[0].mIntConst; + int64 s = ins->mSrc[0].mIntConst; ins->mOperator = IA_SHL; ins->mSrc[0].mIntConst = 0; while (s > 1) @@ -4892,6 +4964,7 @@ void InterCodeBasicBlock::CheckValueUsage(InterInstruction * ins, const GrowingI ins->mCode = IC_CONSTANT; ins->mConst.mType = ins->mDst.mType; ins->mConst.mIntConst = 0; + ins->mNumOperands = 0; } } @@ -4977,6 +5050,7 @@ void InterCodeBasicBlock::CheckValueUsage(InterInstruction * ins, const GrowingI ins->mCode = IC_CONSTANT; ins->mConst.mFloatConst = ConstantFolding(ins->mOperator, tvalue[ins->mSrc[0].mTemp]->mConst.mFloatConst); ins->mSrc[0].mTemp = -1; + ins->mNumOperands = 0; } break; case IT_POINTER: @@ -4997,6 +5071,7 @@ void InterCodeBasicBlock::CheckValueUsage(InterInstruction * ins, const GrowingI ins->mDst.mType = IT_BOOL; ins->mSrc[0].mTemp = -1; ins->mSrc[1].mTemp = -1; + ins->mNumOperands = 0; } else { @@ -5037,6 +5112,7 @@ void InterCodeBasicBlock::CheckValueUsage(InterInstruction * ins, const GrowingI ins->mConst.mIntConst = ConstantFolding(ins->mOperator, ins->mDst.mType, tvalue[ins->mSrc[1].mTemp]->mConst.mIntConst, tvalue[ins->mSrc[0].mTemp]->mConst.mIntConst); ins->mSrc[0].mTemp = -1; ins->mSrc[1].mTemp = -1; + ins->mNumOperands = 0; } else { @@ -5764,10 +5840,20 @@ void InterCodeBasicBlock::UpdateLocalIntegerRangeSets(const GrowingVariableArray { ins->mSrc[i].mRange = mLocalValueRange[ins->mSrc[i].mTemp]; #if 1 - if (ins->mCode != IC_ASSEMBLER && ins->mSrc[i].mRange.mMinState == IntegerValueRange::S_BOUND && ins->mSrc[i].mRange.mMaxState == IntegerValueRange::S_BOUND && ins->mSrc[i].mRange.mMinValue == ins->mSrc[i].mRange.mMaxValue) + if (ins->mCode != IC_ASSEMBLER&& ins->mSrc[i].mRange.mMinState == IntegerValueRange::S_BOUND && ins->mSrc[i].mRange.mMaxState == IntegerValueRange::S_BOUND && ins->mSrc[i].mRange.mMinValue == ins->mSrc[i].mRange.mMaxValue) { - ins->mSrc[i].mTemp = -1; - ins->mSrc[i].mIntConst = ins->mSrc[i].mRange.mMinValue; + if (ins->mCode == IC_LOAD_TEMPORARY) + { + ins->mCode = IC_CONSTANT; + ins->mConst.mType = ins->mSrc[0].mType; + ins->mConst.mIntConst = ins->mSrc[0].mRange.mMinValue; + ins->mNumOperands = 0; + } + else + { + ins->mSrc[i].mTemp = -1; + ins->mSrc[i].mIntConst = ins->mSrc[i].mRange.mMinValue; + } } #endif } @@ -6971,15 +7057,15 @@ void InterCodeBasicBlock::BuildLocalTempSets(int num) { mVisited = true; - mLocalRequiredTemps = NumberSet(num); - mLocalProvidedTemps = NumberSet(num); + mLocalRequiredTemps.Reset(num); + mLocalProvidedTemps.Reset(num); - mEntryRequiredTemps = NumberSet(num); - mEntryProvidedTemps = NumberSet(num); - mEntryPotentialTemps = NumberSet(num); - mExitRequiredTemps = NumberSet(num); - mExitProvidedTemps = NumberSet(num); - mExitPotentialTemps = NumberSet(num); + mEntryRequiredTemps.Reset(num); + mEntryProvidedTemps.Reset(num); + mEntryPotentialTemps.Reset(num); + mExitRequiredTemps.Reset(num); + mExitProvidedTemps.Reset(num); + mExitPotentialTemps.Reset(num); for (i = 0; i < mInstructions.Size(); i++) { @@ -8943,6 +9029,7 @@ bool InterCodeBasicBlock::LoadStoreForwarding(const GrowingInstructionPtrArray& { ins->mCode = IC_CONSTANT; ins->mConst = lins->mSrc[0]; + ins->mNumOperands = 0; changed = true; } else @@ -13615,6 +13702,7 @@ void InterCodeBasicBlock::WarnUsedUndefinedVariables(InterCodeProcedure* proc) ins->mConst.mLinkerObject = nullptr; ins->mConst.mVarIndex = -1; ins->mConst.mMemory = IM_ABSOLUTE; + ins->mNumOperands = 0; } else { @@ -15678,7 +15766,6 @@ void InterCodeProcedure::MergeBasicBlocks(void) block->mNumEntries -= mblocks.Size(); InterInstruction* jins = new InterInstruction(mblocks[0]->mInstructions.Last()->mLocation, IC_JUMP); - jins->mCode = IC_JUMP; nblock->mInstructions.Push(jins); nblock->Close(block, nullptr); diff --git a/oscar64/InterCode.h b/oscar64/InterCode.h index aa3c6a4..b67710d 100644 --- a/oscar64/InterCode.h +++ b/oscar64/InterCode.h @@ -13,7 +13,6 @@ enum InterCode { IC_NONE, IC_LOAD_TEMPORARY, - IC_STORE_TEMPORARY, IC_BINARY_OPERATOR, IC_UNARY_OPERATOR, IC_RELATIONAL_OPERATOR, diff --git a/oscar64/InterCodeGenerator.cpp b/oscar64/InterCodeGenerator.cpp index 4105ae9..89ce463 100644 --- a/oscar64/InterCodeGenerator.cpp +++ b/oscar64/InterCodeGenerator.cpp @@ -1377,6 +1377,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* ins->mSrc[1].mOperandSize = vl.mType->mSize; ins->mSrc[1].mStride = vl.mType->mStripe; ins->mVolatile = vl.mType->mFlags & DTF_VOLATILE; + ins->mNumOperands = 2; block->Append(ins); } } @@ -2668,6 +2669,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* ains->mSrc[0].mTemp = pins->mDst.mTemp; ains->mDst.mType = IT_POINTER; ains->mDst.mTemp = proc->AddTemporary(IT_POINTER); + ains->mNumOperands = 1; block->Append(ains); ins->mCode = IC_RETURN; @@ -2718,9 +2720,14 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* ins->mSrc[1].mMemory = IM_INDIRECT; ins->mCode = IC_STORE; ins->mSrc[1].mOperandSize = ains->mConst.mOperandSize; + ins->mNumOperands = 2; } else + { ins->mCode = IC_RETURN_VALUE; + ins->mNumOperands = 1; + } + } } else diff --git a/oscar64/MachineTypes.h b/oscar64/MachineTypes.h index 891f66f..7badfad 100644 --- a/oscar64/MachineTypes.h +++ b/oscar64/MachineTypes.h @@ -79,6 +79,8 @@ inline int sprintf_s(char* buffer, int size, const char* format, ...) return n; } +#define __forceinline inline + #endif extern uint8 BC_REG_WORK; diff --git a/oscar64/NativeCodeGenerator.cpp b/oscar64/NativeCodeGenerator.cpp index 1f2dec4..839f8f3 100644 --- a/oscar64/NativeCodeGenerator.cpp +++ b/oscar64/NativeCodeGenerator.cpp @@ -1109,7 +1109,7 @@ bool NativeCodeInstruction::ReferencesAccu(void) const return ChangesAccu() || RequiresAccu(); } -bool NativeCodeInstruction::ReferencesYReg(void) const +bool NativeCodeInstruction::ReferencesYReg(void) const { return ChangesYReg() || RequiresYReg(); } @@ -11641,7 +11641,7 @@ void NativeCodeBasicBlock::CallAssembler(InterCodeProcedure* proc, NativeCodePro if (ins->mCode == IC_ASSEMBLER && (proc->mModule->mCompilerOptions & COPT_OPTIMIZE_ASSEMBLER)) { - GrowingArray tains(NativeCodeInstruction(ASMIT_INV, ASMIM_IMPLIED)); + ExpandingArray tains; uint32 uflags = 0; bool simple = true; @@ -11952,7 +11952,7 @@ void NativeCodeBasicBlock::BuildDominatorTree(NativeCodeBasicBlock* from) return; else { - GrowingArray< NativeCodeBasicBlock * > d1(nullptr), d2(nullptr); + ExpandingArray< NativeCodeBasicBlock * > d1, d2; NativeCodeBasicBlock* b = mDominator; while (b) @@ -17584,6 +17584,9 @@ bool NativeCodeBasicBlock::JoinTailCodeSequences(NativeCodeProcedure* proc, bool } #endif + + CheckLive(); + if (mTrueJump && mFalseJump) { int addr, index, taddr; @@ -17628,6 +17631,8 @@ bool NativeCodeBasicBlock::JoinTailCodeSequences(NativeCodeProcedure* proc, bool } } + CheckLive(); + if (mTrueJump && mFalseJump) { if (mTrueJump->mIns.Size() > 0 && mFalseJump->mIns.Size() > 0 && !mExitRequiredRegs[CPU_REG_Z] && (mBranch == ASMIT_BCC || mBranch == ASMIT_BCS) && @@ -17636,6 +17641,7 @@ bool NativeCodeBasicBlock::JoinTailCodeSequences(NativeCodeProcedure* proc, bool if (!mTrueJump->mIns[0].ChangesCarry() && mTrueJump->mIns[0].IsSame(mFalseJump->mIns[0])) { int live = mTrueJump->mIns[0].mLive; + mTrueJump->mIns[0].mLive |= LIVE_CPU_REG_C; mIns.Push(mTrueJump->mIns[0]); mTrueJump->mIns.Remove(0); mFalseJump->mIns.Remove(0); @@ -17665,6 +17671,8 @@ bool NativeCodeBasicBlock::JoinTailCodeSequences(NativeCodeProcedure* proc, bool } changed = true; + + CheckLive(); } } } @@ -17895,6 +17903,7 @@ bool NativeCodeBasicBlock::JoinTailCodeSequences(NativeCodeProcedure* proc, bool } } #endif + CheckLive(); #if 1 if (mIns.Size() >= 1 && mIns[0].mType == ASMIT_TAX && !(mIns[0].mLive & (LIVE_CPU_REG_A | LIVE_CPU_REG_Z)) && !mEntryRegA) @@ -18141,6 +18150,7 @@ bool NativeCodeBasicBlock::JoinTailCodeSequences(NativeCodeProcedure* proc, bool } } } + CheckLive(); } #endif @@ -18240,6 +18250,7 @@ bool NativeCodeBasicBlock::JoinTailCodeSequences(NativeCodeProcedure* proc, bool } } } + CheckLive(); } #endif @@ -23518,7 +23529,7 @@ bool NativeCodeBasicBlock::BackwardReplaceZeroPage(int at, int from, int to, boo } -bool NativeCodeBasicBlock::Propagate16BitSum(const GrowingArray & cinfo) +bool NativeCodeBasicBlock::Propagate16BitSum(const ExpandingArray & cinfo) { bool changed = false; @@ -23526,7 +23537,7 @@ bool NativeCodeBasicBlock::Propagate16BitSum(const GrowingArray infos(NativeRegisterSum16Info{}); + ExpandingArray infos; if (mNumEntries == 1) infos = cinfo; @@ -27543,7 +27554,7 @@ bool NativeCodeBasicBlock::OptimizeSimpleLoop(NativeCodeProcedure * proc, bool f return false; } -bool NativeCodeBasicBlock::OptimizeInnerLoop(NativeCodeProcedure* proc, NativeCodeBasicBlock* head, NativeCodeBasicBlock* tail, GrowingArray& lblocks) +bool NativeCodeBasicBlock::OptimizeInnerLoop(NativeCodeProcedure* proc, NativeCodeBasicBlock* head, NativeCodeBasicBlock* tail, ExpandingArray& lblocks) { bool simple = true; @@ -27781,7 +27792,7 @@ bool NativeCodeBasicBlock::OptimizeInnerLoop(NativeCodeProcedure* proc, NativeCo return false; } -NativeCodeBasicBlock* NativeCodeBasicBlock::CollectInnerLoop(NativeCodeBasicBlock* head, GrowingArray& lblocks) +NativeCodeBasicBlock* NativeCodeBasicBlock::CollectInnerLoop(NativeCodeBasicBlock* head, ExpandingArray& lblocks) { if (mLoopHeadBlock != head) { @@ -27817,7 +27828,7 @@ bool NativeCodeBasicBlock::OptimizeInnerLoops(NativeCodeProcedure* proc) if (mLoopHead) { - GrowingArray lblocks(nullptr); + ExpandingArray lblocks; NativeCodeBasicBlock* tail = CollectInnerLoop(this, lblocks); @@ -27889,7 +27900,7 @@ bool NativeCodeBasicBlock::OptimizeGenericLoop(NativeCodeProcedure* proc) { if (mLoopHead) { - GrowingArray lblocks(nullptr); + ExpandingArray lblocks; proc->ResetPatched(); if (CollectGenericLoop(proc, lblocks)) @@ -28153,8 +28164,8 @@ bool NativeCodeBasicBlock::OptimizeGenericLoop(NativeCodeProcedure* proc) if (yreg >= 0 || xreg >= 0 || areg >= 0) { - GrowingArray entries(nullptr); - GrowingArray exits(nullptr); + ExpandingArray entries; + ExpandingArray exits; for (int i = 0; i < lblocks.Size(); i++) { @@ -28420,7 +28431,7 @@ bool NativeCodeBasicBlock::OptimizeGenericLoop(NativeCodeProcedure* proc) return changed; } -void NativeCodeBasicBlock::CollectReachable(GrowingArray& lblock) +void NativeCodeBasicBlock::CollectReachable(ExpandingArray& lblock) { if (!mVisited && !mPatched) { @@ -28432,9 +28443,9 @@ void NativeCodeBasicBlock::CollectReachable(GrowingArray& } } -bool NativeCodeBasicBlock::CollectGenericLoop(NativeCodeProcedure* proc, GrowingArray& lblocks) +bool NativeCodeBasicBlock::CollectGenericLoop(NativeCodeProcedure* proc, ExpandingArray& lblocks) { - GrowingArray rblocks(nullptr); + ExpandingArray rblocks; proc->ResetPatched(); CollectReachable(rblocks); @@ -28587,7 +28598,7 @@ bool NativeCodeBasicBlock::OptimizeSelect(NativeCodeProcedure* proc) return changed; } -static bool CheckBlockCopySequence(const GrowingArray& ins, int si, int n) +static bool CheckBlockCopySequence(const ExpandingArray& ins, int si, int n) { if (si + 2 * n <= ins.Size() && ins[si + 0].mType == ASMIT_LDA && (ins[si + 0].mMode == ASMIM_ZERO_PAGE || ins[si + 0].mMode == ASMIM_ABSOLUTE) && @@ -35897,7 +35908,7 @@ int NativeCodeBasicBlock::LeadsInto(NativeCodeBasicBlock* block, int dist) return 6; } -void NativeCodeBasicBlock::BuildPlacement(GrowingArray& placement) +void NativeCodeBasicBlock::BuildPlacement(ExpandingArray& placement) { if (!mPlaced) { @@ -36153,7 +36164,6 @@ void NativeCodeBasicBlock::CopyCode(NativeCodeProcedure * proc, uint8* target) } NativeCodeBasicBlock::NativeCodeBasicBlock(void) - : mIns(NativeCodeInstruction(ASMIT_INV, ASMIM_IMPLIED)), mRelocations({ 0 }), mEntryBlocks(nullptr), mCode(0) { mBranch = ASMIT_RTS; mTrueJump = mFalseJump = NULL; @@ -36181,7 +36191,7 @@ NativeCodeBasicBlock::~NativeCodeBasicBlock(void) } NativeCodeProcedure::NativeCodeProcedure(NativeCodeGenerator* generator) - : mGenerator(generator), mRelocations({ 0 }), mBlocks(nullptr) + : mGenerator(generator) { mTempBlocks = 1000; } @@ -36806,7 +36816,7 @@ void NativeCodeProcedure::Compile(InterCodeProcedure* proc) proc->mLinkerObject->mType = LOT_NATIVE_CODE; - GrowingArray placement(nullptr); + ExpandingArray placement; int total; total = 0; @@ -36997,7 +37007,7 @@ void NativeCodeProcedure::Optimize(void) if (step == 0) { ResetVisited(); - GrowingArray cinfo({ 0 }); + ExpandingArray cinfo; if (mEntryBlock->Propagate16BitSum(cinfo)) changed = true; @@ -38129,7 +38139,7 @@ void NativeCodeProcedure::CompileInterBlock(InterCodeProcedure* iproc, InterCode NativeCodeGenerator::NativeCodeGenerator(Errors* errors, Linker* linker, LinkerSection* runtimeSection) - : mErrors(errors), mLinker(linker), mRuntimeSection(runtimeSection), mCompilerOptions(COPT_DEFAULT), mRuntime({ 0 }), mMulTables({nullptr}) + : mErrors(errors), mLinker(linker), mRuntimeSection(runtimeSection), mCompilerOptions(COPT_DEFAULT) { } diff --git a/oscar64/NativeCodeGenerator.h b/oscar64/NativeCodeGenerator.h index b7cd76b..03e0106 100644 --- a/oscar64/NativeCodeGenerator.h +++ b/oscar64/NativeCodeGenerator.h @@ -167,16 +167,16 @@ public: NativeCodeBasicBlock(void); ~NativeCodeBasicBlock(void); - GrowingArray mCode; + ExpandingArray mCode; int mIndex; NativeCodeBasicBlock* mTrueJump, * mFalseJump, * mFromJump; AsmInsType mBranch; - GrowingArray mIns; - GrowingArray mRelocations; + ExpandingArray mIns; + ExpandingArray mRelocations; - GrowingArray mEntryBlocks; + ExpandingArray mEntryBlocks; int mOffset, mSize, mPlace, mNumEntries, mNumEntered, mFrameOffset, mTemp; bool mPlaced, mCopied, mKnownShortBranch, mBypassed, mAssembled, mNoFrame, mVisited, mLoopHead, mVisiting, mLocked, mPatched, mPatchFail, mPatchChecked, mPatchStart, mPatchLoop, mPatchLoopChanged, mPatchExit; @@ -199,7 +199,7 @@ public: void RemoveEntryBlock(NativeCodeBasicBlock* block); int LeadsInto(NativeCodeBasicBlock* block, int dist); - void BuildPlacement(GrowingArray& placement); + void BuildPlacement(ExpandingArray& placement); void InitialOffset(int& total); bool CalculateOffset(int& total); @@ -236,17 +236,17 @@ public: bool OptimizeSimpleLoop(NativeCodeProcedure* proc, bool full); bool SimpleLoopReversal(NativeCodeProcedure* proc); - bool OptimizeInnerLoop(NativeCodeProcedure* proc, NativeCodeBasicBlock* head, NativeCodeBasicBlock* tail, GrowingArray& blocks); + bool OptimizeInnerLoop(NativeCodeProcedure* proc, NativeCodeBasicBlock* head, NativeCodeBasicBlock* tail, ExpandingArray& blocks); bool OptimizeXYSimpleLoop(void); bool OptimizeSelect(NativeCodeProcedure* proc); bool OptimizeInnerLoops(NativeCodeProcedure* proc); - NativeCodeBasicBlock* CollectInnerLoop(NativeCodeBasicBlock* head, GrowingArray& lblocks); + NativeCodeBasicBlock* CollectInnerLoop(NativeCodeBasicBlock* head, ExpandingArray& lblocks); bool OptimizeGenericLoop(NativeCodeProcedure* proc); - bool CollectGenericLoop(NativeCodeProcedure* proc, GrowingArray& lblocks); - void CollectReachable(GrowingArray& lblock); + bool CollectGenericLoop(NativeCodeProcedure* proc, ExpandingArray& lblocks); + void CollectReachable(ExpandingArray& lblock); bool OptimizeFindLoop(NativeCodeProcedure* proc); @@ -435,7 +435,7 @@ public: bool BypassRegisterConditionBlock(void); bool Check16BitSum(int at, NativeRegisterSum16Info& info); - bool Propagate16BitSum(const GrowingArray& cinfo); + bool Propagate16BitSum(const ExpandingArray& cinfo); bool IsFinalZeroPageUse(const NativeCodeBasicBlock* block, int at, int from, int to, bool pair); bool ReplaceFinalZeroPageUse(NativeCodeProcedure* nproc); @@ -569,8 +569,8 @@ class NativeCodeProcedure bool mNoFrame; int mTempBlocks; - GrowingArray mRelocations; - GrowingArray < NativeCodeBasicBlock*> mBlocks; + ExpandingArray mRelocations; + ExpandingArray< NativeCodeBasicBlock*> mBlocks; void Compile(InterCodeProcedure* proc); void Optimize(void); @@ -626,6 +626,6 @@ public: Linker* mLinker; LinkerSection* mRuntimeSection; - GrowingArray mRuntime; - GrowingArray mMulTables; + ExpandingArray mRuntime; + ExpandingArray mMulTables; }; diff --git a/oscar64/NumberSet.cpp b/oscar64/NumberSet.cpp index 7833fad..e58a96e 100644 --- a/oscar64/NumberSet.cpp +++ b/oscar64/NumberSet.cpp @@ -49,12 +49,15 @@ void NumberSet::Reset(int size, bool set) { int i; - delete[] bits; + int ndwsize = (size + 31) >> 5; + if (this->dwsize != ndwsize) + { + delete[] bits; + dwsize = ndwsize; + bits = new uint32[dwsize]; + } this->size = size; - dwsize = (size + 31) >> 5; - - bits = new uint32[dwsize]; if (set) {