Fixed array and structure initialisation of locals

This commit is contained in:
drmortalwombat 2021-09-07 12:44:45 +02:00
parent a839ccc960
commit 14f6af66af
4 changed files with 67 additions and 1 deletions

23
autotest/arrayinittest.c Normal file
View File

@ -0,0 +1,23 @@
#include <stdio.h>
#include <assert.h>
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;
}

View File

@ -30,6 +30,9 @@ if %errorlevel% neq 0 goto :error
..\release\oscar64 -i=../include -rt=../include/crt.c -e staticconsttest.c ..\release\oscar64 -i=../include -rt=../include/crt.c -e staticconsttest.c
if %errorlevel% neq 0 goto :error 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 exit /b 0
:error :error
echo Failed with error #%errorlevel%. echo Failed with error #%errorlevel%.

View File

@ -331,7 +331,7 @@ Expression* Expression::ConstantFold(void)
} }
Declaration::Declaration(const Location& loc, DecType type) 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) Declaration::~Declaration(void)

View File

@ -424,6 +424,46 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration*
return ExValue(dec->mBase, ins.mTTemp); 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<InterVariable::Reference> 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: default:
mErrors->Error(dec->mLocation, "Unimplemented constant type"); mErrors->Error(dec->mLocation, "Unimplemented constant type");
} }