Fix pass struct rvalue return to rvalue parameter conflicting zero page registers

This commit is contained in:
drmortalwombat 2025-05-22 15:28:39 +02:00
parent c6b794db3a
commit 27d3666285
5 changed files with 51 additions and 17 deletions

View File

@ -57,7 +57,7 @@ rem @echo off
@call :testh constructortest.cpp @call :testh constructortest.cpp
@if %errorlevel% neq 0 goto :error @if %errorlevel% neq 0 goto :error
@call :testh copyconstructor.cpp @call :testn copyconstructor.cpp
@if %errorlevel% neq 0 goto :error @if %errorlevel% neq 0 goto :error
@call :testh copyassign.cpp @call :testh copyassign.cpp

View File

@ -1,4 +1,5 @@
#include <assert.h> #include <assert.h>
#include <stdio.h>
int t, n; int t, n;
@ -161,7 +162,7 @@ void test_return_value(void)
C2 c(test_ret_v()); C2 c(test_ret_v());
} }
assert(n == 4 && t == 0); assert(n == 6 && t == 0);
} }
void test_return_reference(void) void test_return_reference(void)
@ -184,7 +185,7 @@ void test_retparam_value(void)
test_param_fv(test_ret_v()); test_param_fv(test_ret_v());
} }
assert(n == 4 && t == 0); assert(n == 6 && t == 0);
} }
void test_retparam_reference(void) void test_retparam_reference(void)
@ -200,7 +201,6 @@ void test_retparam_reference(void)
int main(void) int main(void)
{ {
#if 0
test_dcopy_init(); test_dcopy_init();
test_copy_init(); test_copy_init();
test_minit(); test_minit();
@ -208,9 +208,8 @@ int main(void)
test_param_value(); test_param_value();
test_param_ref(); test_param_ref();
test_return_value(); test_return_value();
#endif
test_retparam_value(); test_retparam_value();
// test_retparam_reference(); test_retparam_reference();
return 0; return 0;
} }

View File

@ -1659,7 +1659,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateInline(Declaration* pro
mErrors->Error(exp->mLeft->mLocation, EERR_NOT_AN_LVALUE, "Not an addressable expression"); mErrors->Error(exp->mLeft->mLocation, EERR_NOT_AN_LVALUE, "Not an addressable expression");
if (vp.mTemp != vr.mTemp) if (vp.mTemp != vr.mTemp)
CopyStructSimple(proc, exp, block, inlineMapper, vp, vr); CopyStruct(proc, exp, block, vp, vr, inlineMapper, false);
} }
else else
{ {

View File

@ -99,7 +99,7 @@ protected:
ExValue TranslateExpression(Declaration * procType, InterCodeProcedure * proc, InterCodeBasicBlock*& block, Expression* exp, DestructStack*& destack, GotoNode*& gotos, const BranchTarget & breakBlock, const BranchTarget& continueBlock, InlineMapper * inlineMapper, ExValue * lrexp = nullptr); ExValue TranslateExpression(Declaration * procType, InterCodeProcedure * proc, InterCodeBasicBlock*& block, Expression* exp, DestructStack*& destack, GotoNode*& gotos, const BranchTarget & breakBlock, const BranchTarget& continueBlock, InlineMapper * inlineMapper, ExValue * lrexp = nullptr);
void TranslateLogic(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock* block, InterCodeBasicBlock* tblock, InterCodeBasicBlock* fblock, Expression* exp, DestructStack*& destack, GotoNode*& gotos, InlineMapper* inlineMapper); void TranslateLogic(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock* block, InterCodeBasicBlock* tblock, InterCodeBasicBlock* fblock, Expression* exp, DestructStack*& destack, GotoNode*& gotos, InlineMapper* inlineMapper);
ExValue TranslateInline(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock*& block, Expression* exp, const BranchTarget& breakBlock, const BranchTarget& continueBlock, InlineMapper* inlineMapper, bool inlineConstexpr, ExValue* lrexp); ExValue TranslateInline(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock*& block, Expression* exp, const BranchTarget& breakBlock, const BranchTarget& continueBlock, InlineMapper* inlineMapper, bool inlineConstexpr, ExValue* lrexp);
void CopyStruct(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, ExValue vl, ExValue vr, InlineMapper* inlineMapper, bool moving); void CopyStruct (InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, ExValue vl, ExValue vr, InlineMapper* inlineMapper, bool moving);
void CopyStructSimple(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock * block, InlineMapper* inlineMapper, ExValue vl, ExValue vr); void CopyStructSimple(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock * block, InlineMapper* inlineMapper, ExValue vl, ExValue vr);
void StoreValue(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, InlineMapper* inlineMapper, ExValue vl, ExValue vr); void StoreValue(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, InlineMapper* inlineMapper, ExValue vl, ExValue vr);

View File

@ -4371,22 +4371,57 @@ Expression* Parser::AddFunctionCallRefReturned(Expression* exp)
rexp = ConcatExpression(rexp, AddFunctionCallRefReturned(pex)); rexp = ConcatExpression(rexp, AddFunctionCallRefReturned(pex));
if ((pdec->mBase->mType == DT_TYPE_REFERENCE || pdec->mBase->mType == DT_TYPE_RVALUEREF) && if (pdec->mBase->mType == DT_TYPE_STRUCT && pex->mType == EX_CALL && pex->mDecType->mType == DT_TYPE_STRUCT)
{
Declaration* vdec = AllocTempVar(pex->mDecType);
Expression* vexp = new Expression(pex->mLocation, EX_VARIABLE);
vexp->mDecType = pex->mDecType;
vexp->mDecValue = vdec;
Expression* cexp = new Expression(pex->mLocation, pex->mType);
cexp->mDecType = pex->mDecType;
cexp->mDecValue = pex->mDecValue;
cexp->mLeft = pex->mLeft;
cexp->mRight = pex->mRight;
cexp->mToken = pex->mToken;
pex->mType = EX_INITIALIZATION;
pex->mToken = TK_ASSIGN;
pex->mLeft = vexp;
pex->mRight = cexp;
pex->mDecValue = nullptr;
pex->mDecType = vdec->mBase;
if (vdec->mBase->mDestructor)
{
Expression* texp = new Expression(mScanner->mLocation, EX_PREFIX);
texp->mToken = TK_BINARY_AND;
texp->mLeft = vexp;
texp->mDecType = new Declaration(mScanner->mLocation, DT_TYPE_POINTER);
texp->mDecType->mFlags |= DTF_CONST | DTF_DEFINED;
texp->mDecType->mBase = vdec->mBase;
texp->mDecType->mSize = 2;
Expression* cexp = new Expression(mScanner->mLocation, EX_CONSTANT);
cexp->mDecValue = vdec->mBase->mDestructor;
cexp->mDecType = cexp->mDecValue->mBase;
Expression* dexp = new Expression(mScanner->mLocation, EX_CALL);
dexp->mLeft = cexp;
dexp->mRight = texp;
rexp = ConcatExpression(rexp, dexp);
}
}
else if ((pdec->mBase->mType == DT_TYPE_REFERENCE || pdec->mBase->mType == DT_TYPE_RVALUEREF) &&
(pex->mDecType->mType != DT_TYPE_REFERENCE && pex->mDecType->mType != DT_TYPE_RVALUEREF) && pex->mType == EX_CALL) (pex->mDecType->mType != DT_TYPE_REFERENCE && pex->mDecType->mType != DT_TYPE_RVALUEREF) && pex->mType == EX_CALL)
{ {
// Returning a value object for pass as reference // Returning a value object for pass as reference
// add a temporary variable // add a temporary variable
Declaration* vdec = AllocTempVar(pex->mDecType); Declaration* vdec = AllocTempVar(pex->mDecType);
#if 0
int nindex = mLocalIndex++;
Declaration* vdec = new Declaration(exp->mLocation, DT_VARIABLE);
vdec->mVarIndex = nindex;
vdec->mBase = pex->mDecType;
vdec->mSize = pex->mDecType->mSize;
#endif
Expression* vexp = new Expression(pex->mLocation, EX_VARIABLE); Expression* vexp = new Expression(pex->mLocation, EX_VARIABLE);
vexp->mDecType = pex->mDecType; vexp->mDecType = pex->mDecType;
vexp->mDecValue = vdec; vexp->mDecValue = vdec;