Global object initialization

This commit is contained in:
drmortalwombat 2023-08-14 18:09:35 +02:00
parent 80426d974c
commit ae2fbb6256
4 changed files with 26 additions and 0 deletions

View File

@ -973,3 +973,5 @@ void cistream::refill(void)
}
}
cistream cin;
costream cout;

View File

@ -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");

View File

@ -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);

View File

@ -74,6 +74,9 @@ protected:
typedef GrowingArray<SwitchNode> 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);