From 14f6af66afdd80835fcd1294ea38907b234e35d8 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Tue, 7 Sep 2021 12:44:45 +0200 Subject: [PATCH] Fixed array and structure initialisation of locals --- autotest/arrayinittest.c | 23 +++++++++++++++++++ autotest/autotest.bat | 3 +++ oscar64/Declaration.cpp | 2 +- oscar64/InterCodeGenerator.cpp | 40 ++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 autotest/arrayinittest.c diff --git a/autotest/arrayinittest.c b/autotest/arrayinittest.c new file mode 100644 index 0000000..2b1557a --- /dev/null +++ b/autotest/arrayinittest.c @@ -0,0 +1,23 @@ +#include +#include + +int fg[15] = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610}; + +int main(void) +{ + int fl[15] = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610}; + + int sg = 0, sl = 0; + + for(int i=0; i<15; i++) + { + sl += fl[i]; + sg += fg[i]; + } + + assert(sl == 1596); + assert(sg == 1596); + + printf("%d %d\n", sl, sg); + return 0; +} \ No newline at end of file diff --git a/autotest/autotest.bat b/autotest/autotest.bat index afc379c..602354c 100644 --- a/autotest/autotest.bat +++ b/autotest/autotest.bat @@ -30,6 +30,9 @@ if %errorlevel% neq 0 goto :error ..\release\oscar64 -i=../include -rt=../include/crt.c -e staticconsttest.c if %errorlevel% neq 0 goto :error +..\release\oscar64 -i=../include -rt=../include/crt.c -e arrayinittest.c +if %errorlevel% neq 0 goto :error + exit /b 0 :error echo Failed with error #%errorlevel%. diff --git a/oscar64/Declaration.cpp b/oscar64/Declaration.cpp index b53bd11..af6439e 100644 --- a/oscar64/Declaration.cpp +++ b/oscar64/Declaration.cpp @@ -331,7 +331,7 @@ Expression* Expression::ConstantFold(void) } Declaration::Declaration(const Location& loc, DecType type) - : mLocation(loc), mType(type), mScope(nullptr), mData(nullptr), mIdent(nullptr), mSize(0), mOffset(0), mFlags(0), mBase(nullptr), mParams(nullptr), mValue(nullptr), mNext(nullptr) + : mLocation(loc), mType(type), mScope(nullptr), mData(nullptr), mIdent(nullptr), mSize(0), mOffset(0), mFlags(0), mBase(nullptr), mParams(nullptr), mValue(nullptr), mNext(nullptr), mVarIndex(-1) {} Declaration::~Declaration(void) diff --git a/oscar64/InterCodeGenerator.cpp b/oscar64/InterCodeGenerator.cpp index b8cb34b..befedee 100644 --- a/oscar64/InterCodeGenerator.cpp +++ b/oscar64/InterCodeGenerator.cpp @@ -424,6 +424,46 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* return ExValue(dec->mBase, ins.mTTemp); } + case DT_CONST_STRUCT: + { + if (dec->mVarIndex < 0) + { + InterVariable var; + var.mOffset = 0; + var.mSize = dec->mSize; + var.mData = nullptr; + var.mIdent = dec->mIdent; + + uint8* d = new uint8[dec->mSize]; + memset(d, 0, dec->mSize); + var.mData = d; + + GrowingArray references({ 0 }); + BuildInitializer(proc->mModule, d, 0, dec, references); + var.mNumReferences = references.Size(); + if (var.mNumReferences) + { + var.mReferences = new InterVariable::Reference[var.mNumReferences]; + for (int i = 0; i < var.mNumReferences; i++) + var.mReferences[i] = references[i]; + } + + dec->mVarIndex = proc->mModule->mGlobalVars.Size(); + var.mIndex = dec->mVarIndex; + proc->mModule->mGlobalVars.Push(var); + } + + InterInstruction ins; + ins.mCode = IC_CONSTANT; + ins.mTType = IT_POINTER; + ins.mTTemp = proc->AddTemporary(IT_POINTER); + ins.mIntValue = 0; + ins.mVarIndex = dec->mVarIndex; + ins.mMemory = IM_GLOBAL; + block->Append(ins); + return ExValue(dec->mBase, ins.mTTemp, 1); + } + default: mErrors->Error(dec->mLocation, "Unimplemented constant type"); }