From ae2fbb62565a88d3cb0cf8c2dbb032634cfa3781 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Mon, 14 Aug 2023 18:09:35 +0200 Subject: [PATCH] Global object initialization --- include/opp/iostream.cpp | 2 ++ include/opp/iostream.h | 2 ++ oscar64/InterCodeGenerator.cpp | 19 +++++++++++++++++++ oscar64/InterCodeGenerator.h | 3 +++ 4 files changed, 26 insertions(+) diff --git a/include/opp/iostream.cpp b/include/opp/iostream.cpp index f069548..7df8992 100644 --- a/include/opp/iostream.cpp +++ b/include/opp/iostream.cpp @@ -973,3 +973,5 @@ void cistream::refill(void) } } +cistream cin; +costream cout; diff --git a/include/opp/iostream.h b/include/opp/iostream.h index 00577d8..bca344f 100644 --- a/include/opp/iostream.h +++ b/include/opp/iostream.h @@ -202,6 +202,8 @@ iosetfill setfill(char fill); ostream & operator<<(ostream & os, const iosetfill & s); +extern cistream cin; +extern costream cout; #pragma compile("iostream.cpp"); diff --git a/oscar64/InterCodeGenerator.cpp b/oscar64/InterCodeGenerator.cpp index c887e95..1af651e 100644 --- a/oscar64/InterCodeGenerator.cpp +++ b/oscar64/InterCodeGenerator.cpp @@ -467,6 +467,11 @@ void InterCodeGenerator::InitGlobalVariable(InterCodeModule * mod, Declaration* else mErrors->Error(dec->mLocation, EERR_INCOMPATIBLE_TYPES, "Incompatible constant initializer"); } + else if (dec->mValue->mType == EX_CONSTRUCT) + { + DestructStack* destack = nullptr; + TranslateExpression(nullptr, mMainInitProc, mMainInitBlock, dec->mValue, destack, BranchTarget(), BranchTarget(), nullptr); + } else mErrors->Error(dec->mLocation, EERR_CONSTANT_INITIALIZER, "Non constant initializer"); } @@ -4742,6 +4747,13 @@ InterCodeProcedure* InterCodeGenerator::TranslateProcedure(InterCodeModule * mod InterCodeBasicBlock* entryBlock = new InterCodeBasicBlock(proc); + if (!strcmp(proc->mIdent->mString, "main")) + { + mMainInitBlock = entryBlock; + mMainInitProc = proc; + entryBlock = new InterCodeBasicBlock(proc); + } + InterCodeBasicBlock* exitBlock = entryBlock; if (dec->mFlags & DTF_DEFINED) @@ -4781,6 +4793,13 @@ InterCodeProcedure* InterCodeGenerator::TranslateProcedure(InterCodeModule * mod TranslateExpression(dec->mBase, proc, exitBlock, exp, destack, BranchTarget(), BranchTarget(), nullptr); UnwindDestructStack(dec->mBase, proc, exitBlock, destack, nullptr, nullptr); + + if (!strcmp(proc->mIdent->mString, "main")) + { + InterInstruction* ins = new InterInstruction(proc->mLocation, IC_JUMP); + mMainInitBlock->Append(ins); + mMainInitBlock->Close(entryBlock, nullptr); + } } else mErrors->Error(dec->mLocation, EERR_UNDEFINED_OBJECT, "Calling undefined function", dec->mQualIdent->mString); diff --git a/oscar64/InterCodeGenerator.h b/oscar64/InterCodeGenerator.h index cb4f5e2..9c48782 100644 --- a/oscar64/InterCodeGenerator.h +++ b/oscar64/InterCodeGenerator.h @@ -74,6 +74,9 @@ protected: typedef GrowingArray SwitchNodeArray; + InterCodeProcedure* mMainInitProc; + InterCodeBasicBlock* mMainInitBlock; + void BuildSwitchTree(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock* block, ExValue v, const SwitchNodeArray& nodes, int left, int right, InterCodeBasicBlock* dblock); ExValue ToValue(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, ExValue v);