diff --git a/oscar64/Compiler.cpp b/oscar64/Compiler.cpp index 14eede2..d976804 100644 --- a/oscar64/Compiler.cpp +++ b/oscar64/Compiler.cpp @@ -919,13 +919,19 @@ bool Compiler::GenerateCode(void) if (!dec->mLinkerObject) mInterCodeGenerator->TranslateProcedure(mInterCodeModule, dec->mValue, dec); } - else + } + for (int i = 0; i < mCompilationUnits->mReferenced.Size(); i++) + { + Declaration* dec = mCompilationUnits->mReferenced[i]; + if (dec->mType != DT_CONST_FUNCTION) { if (!dec->mLinkerObject) mInterCodeGenerator->InitGlobalVariable(mInterCodeModule, dec); } } + mInterCodeGenerator->CompleteMainInit(); + if (mErrors->mErrorCount != 0) return false; diff --git a/oscar64/InterCodeGenerator.cpp b/oscar64/InterCodeGenerator.cpp index 9314c36..e47af06 100644 --- a/oscar64/InterCodeGenerator.cpp +++ b/oscar64/InterCodeGenerator.cpp @@ -5839,9 +5839,7 @@ InterCodeProcedure* InterCodeGenerator::TranslateProcedure(InterCodeModule * mod if (!strcmp(proc->mIdent->mString, "main")) { - InterInstruction* ins = new InterInstruction(proc->mLocation, IC_JUMP); - mMainInitBlock->Append(ins); - mMainInitBlock->Close(entryBlock, nullptr); + mMainStartupBlock = entryBlock; } } else @@ -5851,7 +5849,7 @@ InterCodeProcedure* InterCodeGenerator::TranslateProcedure(InterCodeModule * mod exitBlock->Append(ins); exitBlock->Close(nullptr, nullptr); - if (mErrors->mErrorCount == 0) + if (mErrors->mErrorCount == 0 && proc != mMainInitProc) { if (mCompilerOptions & COPT_VERBOSE2) printf("Optimize intermediate code <%s>\n", proc->mIdent->mString); @@ -5863,3 +5861,18 @@ InterCodeProcedure* InterCodeGenerator::TranslateProcedure(InterCodeModule * mod return proc; } + +void InterCodeGenerator::CompleteMainInit(void) +{ + if (mErrors->mErrorCount == 0) + { + InterInstruction* ins = new InterInstruction(mMainInitProc->mLocation, IC_JUMP); + mMainInitBlock->Append(ins); + mMainInitBlock->Close(mMainStartupBlock, nullptr); + + if (mCompilerOptions & COPT_VERBOSE2) + printf("Optimize intermediate code <%s>\n", mMainInitProc->mIdent->mString); + + mMainInitProc->Close(); + } +} diff --git a/oscar64/InterCodeGenerator.h b/oscar64/InterCodeGenerator.h index 9058a0e..4a203d4 100644 --- a/oscar64/InterCodeGenerator.h +++ b/oscar64/InterCodeGenerator.h @@ -49,6 +49,8 @@ public: void InitGlobalVariable(InterCodeModule* mod, Declaration* dec); void InitLocalVariable(InterCodeProcedure* proc, Declaration* dec, int index); void InitParameter(InterCodeProcedure* proc, Declaration* dec, int index); + + void CompleteMainInit(void); protected: Errors* mErrors; @@ -85,7 +87,7 @@ protected: }; InterCodeProcedure* mMainInitProc; - InterCodeBasicBlock* mMainInitBlock; + InterCodeBasicBlock* mMainInitBlock, *mMainStartupBlock; Location MapLocation(Expression * exp, InlineMapper* inlineMapper); diff --git a/oscar64/Parser.cpp b/oscar64/Parser.cpp index 07ba972..cd05e3e 100644 --- a/oscar64/Parser.cpp +++ b/oscar64/Parser.cpp @@ -1312,9 +1312,18 @@ Declaration* Parser::ParsePostfixDeclaration(void) ndec->mBase = dec; dec = ndec; } - else if (mScanner->mToken == TK_OPEN_PARENTHESIS) + else if (ConsumeTokenIf(TK_OPEN_PARENTHESIS)) { - dec = ParseFunctionDeclaration(dec); + if (mScanner->mToken == TK_CLOSE_PARENTHESIS || mScanner->mToken == TK_ELLIPSIS || IsTypeToken()) + { + mScanner->UngetToken(TK_OPEN_PARENTHESIS); + dec = ParseFunctionDeclaration(dec); + } + else + { + mScanner->UngetToken(TK_OPEN_PARENTHESIS); + return dec; + } } else return dec; @@ -11878,6 +11887,67 @@ bool Parser::ConsumeIdentIf(const char* ident) return false; } +bool Parser::IsTypeToken(void) +{ + switch (mScanner->mToken) + { + case TK_INT: + case TK_SHORT: + case TK_LONG: + case TK_FLOAT: + case TK_CHAR: + case TK_BOOL: + case TK_VOID: + case TK_UNSIGNED: + case TK_SIGNED: + case TK_CONST: + case TK_VOLATILE: + case TK_STRUCT: + case TK_CLASS: + case TK_UNION: + case TK_ENUM: + case TK_TYPEDEF: + case TK_STATIC: + case TK_AUTO: + case TK_STRIPED: + return true; + case TK_IDENT: + { + const Ident* pident = mScanner->mTokenIdent; + + if (mTemplateScope && mTemplateScope->Lookup(pident)) + return true; + + Declaration* dec = nullptr; + + if (mThisPointer && !mScope->Lookup(pident, SLEVEL_FUNCTION)) + { + int offset; + uint64 flags; + + if (mThisPointer->mType == DT_ARGUMENT) + dec = MemberLookup(mThisPointer->mBase->mBase, mScanner->mTokenIdent, offset, flags); + else if (mThisPointer->mType == DT_TYPE_POINTER) + dec = MemberLookup(mThisPointer->mBase, mScanner->mTokenIdent, offset, flags); + } + + if (!dec) + dec = mScope->Lookup(pident); + + if (!dec) + return true; + + if (dec->mType <= DT_TYPE_AUTO) + return true; + + + } break; + + } + + return false; +} + bool Parser::IsIntegerToken(void) { return (mScanner->mToken == TK_INTEGER || mScanner->mToken == TK_INTEGERL || mScanner->mToken == TK_INTEGERU || mScanner->mToken == TK_INTEGERUL); diff --git a/oscar64/Parser.h b/oscar64/Parser.h index a8b027f..627328a 100644 --- a/oscar64/Parser.h +++ b/oscar64/Parser.h @@ -31,6 +31,7 @@ protected: bool ConsumeTokenIf(Token token); bool ConsumeIdentIf(const char* ident); bool IsIntegerToken(void); + bool IsTypeToken(void); uint8 mCharMap[256]; int mUnrollLoop; diff --git a/oscar64/Scanner.cpp b/oscar64/Scanner.cpp index dab6b86..1cf1166 100644 --- a/oscar64/Scanner.cpp +++ b/oscar64/Scanner.cpp @@ -486,6 +486,13 @@ void Scanner::UngetToken(Token token) void Scanner::NextToken(void) { + if (mUngetToken) + { + mToken = mUngetToken; + mUngetToken = TK_NONE; + return; + } + if (mReplay) { mLocation = mReplay->mLocation;