diff --git a/oscar64/Compiler.cpp b/oscar64/Compiler.cpp index ffd2d70..21f38c1 100644 --- a/oscar64/Compiler.cpp +++ b/oscar64/Compiler.cpp @@ -192,7 +192,7 @@ bool Compiler::GenerateCode(void) { InterCodeProcedure* proc = mInterCodeModule->mProcedures[i]; - proc->ReduceTemporaries(); +// proc->ReduceTemporaries(); #if _DEBUG proc->Disassemble("final"); diff --git a/oscar64/InterCode.cpp b/oscar64/InterCode.cpp index f70a1ba..f4aec16 100644 --- a/oscar64/InterCode.cpp +++ b/oscar64/InterCode.cpp @@ -3577,11 +3577,12 @@ InterCodeProcedure::InterCodeProcedure(InterCodeModule * mod, const Location & l mRenameTable(-1), mRenameUnionTable(-1), mGlobalRenameTable(-1), mValueForwardingTable(nullptr), mLocalVars(nullptr), mModule(mod), mIdent(ident), mLinkerObject(linkerObject), - mNativeProcedure(false), mLeafProcedure(false) + mNativeProcedure(false), mLeafProcedure(false), mCallsFunctionPointer(false), mCalledFunctions(nullptr) { mID = mModule->mProcedures.Size(); mModule->mProcedures.Push(this); mLinkerObject->mProc = this; + mCallerSavedTemps = 16; } InterCodeProcedure::~InterCodeProcedure(void) @@ -4024,6 +4025,20 @@ void InterCodeProcedure::Close(void) MapVariables(); DisassembleDebug("mapped variabled"); + + ReduceTemporaries(); + + DisassembleDebug("Reduced Temporaries"); +} + +void InterCodeProcedure::AddCalledFunction(InterCodeProcedure* proc) +{ + mCalledFunctions.Push(proc); +} + +void InterCodeProcedure::CallsFunctionPointer(void) +{ + mCallsFunctionPointer = true; } void InterCodeProcedure::MapVariables(void) @@ -4145,7 +4160,21 @@ void InterCodeProcedure::ReduceTemporaries(void) ResetVisited(); mEntryBlock->BuildCallerSaveTempSet(callerSaved); - int callerSavedTemps = 0, calleeSavedTemps = 16; + int callerSavedTemps = 0, calleeSavedTemps = 16, freeCallerSavedTemps = 0, freeTemps = 0; + + if (mCallsFunctionPointer) + freeCallerSavedTemps = 16; + else + { + for (int i = 0; i < mCalledFunctions.Size(); i++) + { + InterCodeProcedure* proc = mCalledFunctions[i]; + if (proc->mCallerSavedTemps > freeCallerSavedTemps) + freeCallerSavedTemps = proc->mCallerSavedTemps; + } + } + + callerSavedTemps = freeCallerSavedTemps; mTempOffset.SetSize(0); @@ -4153,7 +4182,13 @@ void InterCodeProcedure::ReduceTemporaries(void) { int size = InterTypeSize[mTemporaries[i]]; - if (callerSavedTemps + size <= 16 && !callerSaved[i]) + if (freeTemps + size <= freeCallerSavedTemps && !callerSaved[i]) + { + mTempOffset.Push(freeTemps); + mTempSizes.Push(size); + freeTemps += size; + } + else if (callerSavedTemps + size <= 16) { mTempOffset.Push(callerSavedTemps); mTempSizes.Push(size); @@ -4167,6 +4202,7 @@ void InterCodeProcedure::ReduceTemporaries(void) } } mTempSize = calleeSavedTemps; + mCallerSavedTemps = callerSavedTemps; } void InterCodeProcedure::Disassemble(const char* name, bool dumpSets) diff --git a/oscar64/InterCode.h b/oscar64/InterCode.h index a542947..353e99f 100644 --- a/oscar64/InterCode.h +++ b/oscar64/InterCode.h @@ -486,8 +486,9 @@ public: GrowingInterCodeBasicBlockPtrArray mBlocks; GrowingTypeArray mTemporaries; GrowingIntArray mTempOffset, mTempSizes; - int mTempSize, mCommonFrameSize; - bool mLeafProcedure, mNativeProcedure; + int mTempSize, mCommonFrameSize, mCallerSavedTemps; + bool mLeafProcedure, mNativeProcedure, mCallsFunctionPointer; + GrowingInterCodeProcedurePtrArray mCalledFunctions; InterCodeModule * mModule; int mID; @@ -510,6 +511,9 @@ public: // void Set(InterCodeIDMapper* mapper, BitVector localStructure, Scanner scanner, bool debug); + void AddCalledFunction(InterCodeProcedure* proc); + void CallsFunctionPointer(void); + void MapVariables(void); void ReduceTemporaries(void); void Disassemble(const char* name, bool dumpSets = false); diff --git a/oscar64/InterCodeGenerator.cpp b/oscar64/InterCodeGenerator.cpp index ca8d460..4984490 100644 --- a/oscar64/InterCodeGenerator.cpp +++ b/oscar64/InterCodeGenerator.cpp @@ -296,7 +296,6 @@ void InterCodeGenerator::TranslateAssembler(InterCodeModule* mod, Expression * e if (!aexp->mBase->mLinkerObject) { InterCodeProcedure* cproc = this->TranslateProcedure(mod, aexp->mBase->mValue, aexp->mBase); - cproc->ReduceTemporaries(); } LinkerReference ref; @@ -432,7 +431,6 @@ void InterCodeGenerator::TranslateAssembler(InterCodeModule* mod, Expression * e if (!aexp->mLinkerObject) { InterCodeProcedure* cproc = this->TranslateProcedure(mod, aexp->mValue, aexp); - cproc->ReduceTemporaries(); } LinkerReference ref; @@ -450,7 +448,6 @@ void InterCodeGenerator::TranslateAssembler(InterCodeModule* mod, Expression * e if (!aexp->mBase->mLinkerObject) { InterCodeProcedure* cproc = this->TranslateProcedure(mod, aexp->mBase->mValue, aexp->mBase); - cproc->ReduceTemporaries(); } LinkerReference ref; @@ -571,7 +568,6 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (!dec->mLinkerObject) { InterCodeProcedure* cproc = this->TranslateProcedure(proc->mModule, dec->mValue, dec); - cproc->ReduceTemporaries(); } InterInstruction * ins = new InterInstruction(); @@ -1461,6 +1457,11 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (ftype->mType == DT_TYPE_POINTER) ftype = ftype->mBase; + if (exp->mLeft->mDecValue->mType == DT_CONST_FUNCTION) + proc->AddCalledFunction(proc->mModule->mProcedures[exp->mLeft->mDecValue->mVarIndex]); + else + proc->CallsFunctionPointer(); + Declaration* pdec = ftype->mParams; Expression* pex = exp->mRight; while (pex) @@ -2185,7 +2186,6 @@ void InterCodeGenerator::BuildInitializer(InterCodeModule * mod, uint8* dp, int if (!data->mLinkerObject) { InterCodeProcedure* cproc = this->TranslateProcedure(mod, data->mValue, data); - cproc->ReduceTemporaries(); } LinkerReference ref; diff --git a/oscar64/oscar64.cpp b/oscar64/oscar64.cpp index 018b32d..cc8d92d 100644 --- a/oscar64/oscar64.cpp +++ b/oscar64/oscar64.cpp @@ -4,6 +4,9 @@ #else #include #endif +#ifdef __APPLE__ +#include +#endif #include "Compiler.h" #ifdef _WIN32