Complete function struct return

This commit is contained in:
drmortalwombat 2021-10-16 19:46:04 +02:00
parent ed52725a01
commit 001e50ae08
5 changed files with 138 additions and 57 deletions

View File

@ -27,7 +27,6 @@ There are still several open areas, but most targets have been reached. The cur
### Language
* No struct function return
* Missing const checks for structs and enums
* Missing warnings for all kind of abuses

View File

@ -93,6 +93,9 @@ if %errorlevel% neq 0 goto :error
call :test funcvartest.c
if %errorlevel% neq 0 goto :error
call :test structassigntest.c
if %errorlevel% neq 0 goto :error
exit /b 0
:error

View File

@ -0,0 +1,31 @@
#include <stdio.h>
struct Vec3
{
float x, y, z;
};
Vec3 v;
Vec3 vadd(Vec3 s1, Vec3 s2)
{
Vec3 r;
r.x = s1.x + s2.x;
r.y = s1.y + s2.y;
r.z = s1.z + s2.z;
return r;
}
int main(void)
{
Vec3 m = {1, 2, -3}, u = {4, 5, -9}, t = {7, -2, -5};
v.x = 99;
v.y = 100;
v.z = 101;
v = vadd(m, vadd(u, t));
return v.x + v.y + v.z;
}

View File

@ -72,7 +72,7 @@ void GlobalAnalyzer::AutoInline(void)
int cost = (f->mComplexity - 20 * nparams);
bool doinline = false;
if ((mCompilerOptions & COPT_OPTIMIZE_INLINE) && (f->mFlags & DTF_INLINE))
if ((mCompilerOptions & COPT_OPTIMIZE_INLINE) && (f->mFlags & DTF_REQUEST_INLINE))
doinline = true;
if ((mCompilerOptions & COPT_OPTIMIZE_AUTO_INLINE) && (cost * (f->mCallers.Size() - 1) <= 0))
doinline = true;

View File

@ -962,11 +962,12 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration*
{
InterInstruction* ins = new InterInstruction();
ins->mCode = IC_COPY;
ins->mConst.mMemory = IM_INDIRECT;
ins->mSrc[0].mType = IT_POINTER;
ins->mSrc[0].mTemp = vr.mTemp;
ins->mSrc[0].mMemory = IM_INDIRECT;
ins->mSrc[1].mType = IT_POINTER;
ins->mSrc[1].mTemp = vl.mTemp;
ins->mSrc[1].mMemory = IM_INDIRECT;
ins->mConst.mOperandSize = vl.mType->mSize;
block->Append(ins);
}
@ -1819,8 +1820,36 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration*
pex = nullptr;
}
vr = TranslateExpression(procType, proc, block, texp, breakBlock, continueBlock, inlineMapper);
ExValue vp(pdec ? pdec->mBase : TheSignedIntTypeDeclaration, ains->mDst.mTemp, 1);
vr = TranslateExpression(procType, proc, block, texp, breakBlock, continueBlock, inlineMapper, &vp);
if (vr.mType->mType == DT_TYPE_STRUCT || vr.mType->mType == DT_TYPE_UNION)
{
if (pdec && !pdec->mBase->CanAssign(vr.mType))
mErrors->Error(texp->mLocation, EERR_INCOMPATIBLE_TYPES, "Cannot assign incompatible types");
vr = Dereference(proc, block, vr, 1);
if (vr.mReference != 1)
mErrors->Error(exp->mLeft->mLocation, EERR_NOT_AN_LVALUE, "Not an adressable expression");
if (vp.mTemp != vr.mTemp)
{
InterInstruction* cins = new InterInstruction();
cins->mCode = IC_COPY;
cins->mSrc[0].mType = IT_POINTER;
cins->mSrc[0].mTemp = vr.mTemp;
cins->mSrc[0].mMemory = IM_INDIRECT;
cins->mSrc[1].mType = IT_POINTER;
cins->mSrc[1].mTemp = ains->mDst.mTemp;
cins->mSrc[1].mMemory = IM_INDIRECT;
cins->mConst.mOperandSize = vr.mType->mSize;
block->Append(cins);
}
}
else
{
if (vr.mType->mType == DT_TYPE_ARRAY || vr.mType->mType == DT_TYPE_FUNCTION)
vr = Dereference(proc, block, vr, 1);
else
@ -1837,7 +1866,6 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration*
vr = CoerceType(proc, block, vr, TheSignedIntTypeDeclaration);
}
InterInstruction* wins = new InterInstruction();
wins->mCode = IC_STORE;
wins->mSrc[1].mMemory = IM_INDIRECT;
@ -1856,8 +1884,9 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration*
wins->mSrc[1].mOperandSize = vr.mType->mSize;
else
wins->mSrc[1].mOperandSize = 2;
block->Append(wins);
}
if (pdec)
pdec = pdec->mNext;
}
@ -2066,11 +2095,12 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration*
{
InterInstruction* cins = new InterInstruction();
cins->mCode = IC_COPY;
cins->mConst.mMemory = IM_INDIRECT;
cins->mSrc[0].mType = IT_POINTER;
cins->mSrc[0].mTemp = vr.mTemp;
cins->mSrc[0].mMemory = IM_INDIRECT;
cins->mSrc[1].mType = IT_POINTER;
cins->mSrc[1].mTemp = ains->mDst.mTemp;
cins->mSrc[1].mMemory = IM_INDIRECT;
cins->mConst.mOperandSize = vr.mType->mSize;
block->Append(cins);
}
@ -2273,38 +2303,56 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration*
mErrors->Error(exp->mLocation, EERR_INVALID_RETURN, "Non addressable object");
InterInstruction* ains = new InterInstruction();
if (inlineMapper)
{
ains->mCode = IC_CONSTANT;
ains->mDst.mType = IT_POINTER;
ains->mDst.mTemp = proc->AddTemporary(IT_POINTER);
ains->mConst.mVarIndex = 0;
ains->mConst.mOperandSize = procType->mBase->mSize;
ains->mConst.mIntConst = 0;
ains->mConst.mOperandSize = 2;
if (procType->mFlags & DTF_FASTCALL)
ains->mConst.mMemory = IM_FPARAM;
else
ains->mConst.mMemory = IM_PARAM;
ains->mConst.mVarIndex = inlineMapper->mResult;
ains->mConst.mMemory = IM_LOCAL;
block->Append(ains);
InterInstruction* rins = new InterInstruction();
rins->mCode = IC_LOAD;
rins->mSrc[0].mMemory = IM_INDIRECT;
rins->mSrc[0].mType = IT_POINTER;
rins->mSrc[0].mTemp = ains->mDst.mTemp;
rins->mDst.mType = IT_POINTER;
rins->mDst.mTemp = proc->AddTemporary(IT_POINTER);
block->Append(rins);
ins->mCode = IC_NONE;
}
else
{
InterInstruction* pins = new InterInstruction();
pins->mCode = IC_CONSTANT;
pins->mDst.mType = IT_POINTER;
pins->mDst.mTemp = proc->AddTemporary(IT_POINTER);
pins->mConst.mVarIndex = 0;
pins->mConst.mIntConst = 0;
pins->mConst.mOperandSize = 2;
if (procType->mFlags & DTF_FASTCALL)
pins->mConst.mMemory = IM_FPARAM;
else
pins->mConst.mMemory = IM_PARAM;
block->Append(pins);
ains->mCode = IC_LOAD;
ains->mSrc[0].mMemory = IM_INDIRECT;
ains->mSrc[0].mType = IT_POINTER;
ains->mSrc[0].mTemp = pins->mDst.mTemp;
ains->mDst.mType = IT_POINTER;
ains->mDst.mTemp = proc->AddTemporary(IT_POINTER);
block->Append(ains);
ins->mCode = IC_RETURN;
}
InterInstruction* cins = new InterInstruction();
cins->mCode = IC_COPY;
cins->mConst.mMemory = IM_INDIRECT;
cins->mSrc[0].mType = IT_POINTER;
cins->mSrc[0].mTemp = vr.mTemp;
cins->mSrc[0].mMemory = IM_INDIRECT;
cins->mSrc[1].mType = IT_POINTER;
cins->mSrc[1].mTemp = rins->mDst.mTemp;
cins->mSrc[1].mTemp = ains->mDst.mTemp;
cins->mSrc[1].mMemory = IM_INDIRECT;
cins->mConst.mOperandSize = vr.mType->mSize;
block->Append(cins);
ins->mCode = IC_RETURN;
}
else
{