From c926456560b851325195cadaf20cc04bb2ee57db Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Mon, 11 Sep 2023 17:49:04 +0200 Subject: [PATCH] Add simple return type deduction --- oscar64/BitVector.h | 4 ++-- oscar64/Compiler.cpp | 10 ++++++++-- oscar64/Declaration.cpp | 2 +- oscar64/DiskImage.cpp | 5 ++++- oscar64/InterCode.h | 2 +- oscar64/NativeCodeGenerator.cpp | 4 ++-- oscar64/NumberSet.cpp | 8 ++++++++ oscar64/Parser.cpp | 15 +++++++++++---- oscar64/Parser.h | 2 +- oscar64/Scanner.cpp | 3 +++ 10 files changed, 41 insertions(+), 14 deletions(-) diff --git a/oscar64/BitVector.h b/oscar64/BitVector.h index ebe5300..e3c1fcf 100644 --- a/oscar64/BitVector.h +++ b/oscar64/BitVector.h @@ -71,7 +71,7 @@ inline BitVector::BitVector(int size, unsigned char * data) else bits = nullptr; - if (size) + if (size > 0) { memcpy(bits, data, (size + 7) / 8); } @@ -93,7 +93,7 @@ inline BitVector::BitVector(int size, bool set) else bits = NULL; - if (size) + if (size > 0) { if (set) { diff --git a/oscar64/Compiler.cpp b/oscar64/Compiler.cpp index 518da0d..a0d9cb7 100644 --- a/oscar64/Compiler.cpp +++ b/oscar64/Compiler.cpp @@ -1067,7 +1067,7 @@ bool Compiler::BuildLZO(const char* targetPath) CompilationUnit* cunit; - char data[65536]; + char * data = new char[65536]; int n = 0; while (mErrors->mErrorCount == 0 && (cunit = mCompilationUnits->PendingUnit())) @@ -1110,14 +1110,20 @@ bool Compiler::BuildLZO(const char* targetPath) { int done = fwrite(data, 1, n, file); fclose(file); + delete[] data; return done == n; } else + { + delete[] data; return false; + } } else + { + delete[] data; return false; - + } } bool Compiler::WriteOutputFile(const char* targetPath, DiskImage * d64) diff --git a/oscar64/Declaration.cpp b/oscar64/Declaration.cpp index fa28538..9e111b1 100644 --- a/oscar64/Declaration.cpp +++ b/oscar64/Declaration.cpp @@ -882,7 +882,7 @@ Expression* Expression::ConstantFold(Errors * errors, LinkerSection * dataSectio } Declaration::Declaration(const Location& loc, DecType type) - : mLocation(loc), mEndLocation(loc), mType(type), mScope(nullptr), mData(nullptr), mIdent(nullptr), mQualIdent(nullptr), + : mLocation(loc), mEndLocation(loc), mType(type), mScope(nullptr), mData(nullptr), mIdent(nullptr), mQualIdent(nullptr), mMangleIdent(nullptr), mSize(0), mOffset(0), mFlags(0), mComplexity(0), mLocalSize(0), mBase(nullptr), mParams(nullptr), mValue(nullptr), mNext(nullptr), mPrev(nullptr), mConst(nullptr), mMutable(nullptr), diff --git a/oscar64/DiskImage.cpp b/oscar64/DiskImage.cpp index 13b1020..5b5be37 100644 --- a/oscar64/DiskImage.cpp +++ b/oscar64/DiskImage.cpp @@ -250,7 +250,7 @@ bool DiskImage::WriteFile(const char* fname, bool compressed) if (OpenFile(dname)) { - uint8 buffer[65536], cbuffer[65536]; + uint8 * buffer = new uint8[65536], * cbuffer = new uint8[65536]; int size = fread(buffer, 1, 65536, file); int csize = 0; @@ -311,6 +311,9 @@ bool DiskImage::WriteFile(const char* fname, bool compressed) else WriteBytes(buffer, size); CloseFile(); + + delete[] buffer; + delete[] cbuffer; } fclose(file); diff --git a/oscar64/InterCode.h b/oscar64/InterCode.h index c8435f1..1c8e973 100644 --- a/oscar64/InterCode.h +++ b/oscar64/InterCode.h @@ -245,7 +245,7 @@ class InterVariable { public: bool mUsed, mAliased, mTemp; - int mIndex, mSize, mOffset, mAddr, mTempIndex; + int mIndex, mSize, mOffset, mTempIndex; int mNumReferences; const Ident * mIdent; LinkerObject * mLinkerObject; diff --git a/oscar64/NativeCodeGenerator.cpp b/oscar64/NativeCodeGenerator.cpp index 4b11ac5..44470aa 100644 --- a/oscar64/NativeCodeGenerator.cpp +++ b/oscar64/NativeCodeGenerator.cpp @@ -22386,8 +22386,8 @@ bool NativeCodeBasicBlock::PatchCrossBlock16BitFlood(const NativeCodeBasicBlock* mPatchExit = true; - mExitRequiredRegs |= sreg; - mExitRequiredRegs |= sreg + 1; + mExitRequiredRegs += sreg; + mExitRequiredRegs += sreg + 1; if (mTrueJump && mTrueJump->PatchCrossBlock16BitFlood(block, sreg, dreg, 0)) changed = true; diff --git a/oscar64/NumberSet.cpp b/oscar64/NumberSet.cpp index 5fa614b..c3f857b 100644 --- a/oscar64/NumberSet.cpp +++ b/oscar64/NumberSet.cpp @@ -120,6 +120,8 @@ NumberSet& NumberSet::operator=(const NumberSet& set) NumberSet& NumberSet::operator&=(const NumberSet& set) { + assert(dwsize == set.dwsize); + int size = dwsize; const uint32* sbits = set.bits; uint32* dbits = bits; @@ -132,6 +134,8 @@ NumberSet& NumberSet::operator&=(const NumberSet& set) NumberSet& NumberSet::operator|=(const NumberSet& set) { + assert(dwsize == set.dwsize); + int size = dwsize; const uint32* sbits = set.bits; uint32* dbits = bits; @@ -144,6 +148,8 @@ NumberSet& NumberSet::operator|=(const NumberSet& set) NumberSet& NumberSet::operator-=(const NumberSet& set) { + assert(dwsize == set.dwsize); + int i; for (i = 0; i < dwsize; i++) @@ -154,6 +160,8 @@ NumberSet& NumberSet::operator-=(const NumberSet& set) bool NumberSet::operator<=(const NumberSet& set) const { + assert(dwsize == set.dwsize); + int i; for (i = 0; i < dwsize; i++) diff --git a/oscar64/Parser.cpp b/oscar64/Parser.cpp index bede094..d5f5601 100644 --- a/oscar64/Parser.cpp +++ b/oscar64/Parser.cpp @@ -20,6 +20,7 @@ Parser::Parser(Errors* errors, Scanner* scanner, CompilationUnits* compilationUn mCompilerOptionSP = 0; mThisPointer = nullptr; mFunction = nullptr; + mFunctionType = nullptr; for (int i = 0; i < 256; i++) mCharMap[i] = i; @@ -7110,7 +7111,7 @@ Expression* Parser::ParseFunction(Declaration * dec) if (dec->mFlags & DTF_FUNC_THIS) mThisPointer = dec->mParams; - mReturnType = dec->mBase; + mFunctionType = dec; DeclarationScope* oscope = mScope; while (mScope->mLevel == SLEVEL_CLASS) @@ -7508,10 +7509,16 @@ Expression* Parser::ParseStatement(void) if (mScanner->mToken != TK_SEMICOLON) { exp->mLeft = ParseRExpression(); - if (mReturnType) - exp->mLeft = CoerceExpression(exp->mLeft, mReturnType); + if (mFunctionType && mFunctionType->mBase) + { + if (mFunctionType->mBase->mType == DT_TYPE_AUTO) + { + mFunctionType->mBase = exp->mLeft->mDecType; + } + exp->mLeft = CoerceExpression(exp->mLeft, mFunctionType->mBase); + } exp->mLeft = CleanupExpression(exp->mLeft); - if (exp->mLeft->mType == EX_CONSTRUCT && mReturnType && mReturnType->mType == DT_TYPE_STRUCT) + if (exp->mLeft->mType == EX_CONSTRUCT && mFunctionType && mFunctionType->mBase && mFunctionType->mBase->mType == DT_TYPE_STRUCT) { Expression* cexp = exp->mLeft->mLeft->mLeft; diff --git a/oscar64/Parser.h b/oscar64/Parser.h index 064be45..150113a 100644 --- a/oscar64/Parser.h +++ b/oscar64/Parser.h @@ -15,7 +15,7 @@ public: DeclarationScope * mGlobals, * mScope, * mTemplateScope; int mLocalIndex; CompilationUnits * mCompilationUnits; - Declaration * mThisPointer, * mReturnType, * mFunction; + Declaration * mThisPointer, * mFunctionType, * mFunction; LinkerSection * mCodeSection, * mDataSection, * mBSSection; diff --git a/oscar64/Scanner.cpp b/oscar64/Scanner.cpp index 516228c..5b23920 100644 --- a/oscar64/Scanner.cpp +++ b/oscar64/Scanner.cpp @@ -1277,6 +1277,7 @@ void Scanner::NextRawToken(void) { int n = 0; char tkprep[128]; + tkprep[0] = 0; while (NextChar() && IsAlpha(mTokenChar)) { @@ -1371,6 +1372,8 @@ void Scanner::NextRawToken(void) { int n = 0; char tkident[256]; + tkident[0] = 0; + for (;;) { if (IsIdentChar(mTokenChar))