Remove copies from struct function return

This commit is contained in:
drmortalwombat 2021-10-16 18:17:54 +02:00
parent 19a54432f6
commit ed52725a01
4 changed files with 105 additions and 42 deletions

View File

@ -61,6 +61,30 @@ void ValueSet::FlushAll(void)
mNum = 0; mNum = 0;
} }
void ValueSet::FlushFrameAliases(void)
{
int i;
i = 0;
while (i < mNum)
{
if (mInstructions[i]->mCode == IC_CONSTANT && mInstructions[i]->mDst.mType == IT_POINTER && mInstructions[i]->mConst.mMemory == IM_FRAME)
{
//
// Address in frame space
//
mNum--;
if (i < mNum)
{
mInstructions[i] = mInstructions[mNum];
}
}
else
i++;
}
}
void ValueSet::FlushCallAliases(void) void ValueSet::FlushCallAliases(void)
{ {
int i; int i;
@ -1077,6 +1101,10 @@ void ValueSet::UpdateValue(InterInstruction * ins, const GrowingInstructionPtrAr
ins->mSrc[0].mTemp = -1; ins->mSrc[0].mTemp = -1;
} }
break; break;
case IC_PUSH_FRAME:
case IC_POP_FRAME:
FlushFrameAliases();
break;
case IC_CALL: case IC_CALL:
case IC_CALL_NATIVE: case IC_CALL_NATIVE:
FlushCallAliases(); FlushCallAliases();
@ -3484,6 +3512,11 @@ static bool CanBypass(const InterInstruction* lins, const InterInstruction* bins
if (bins->mDst.mTemp == lins->mSrc[i].mTemp) if (bins->mDst.mTemp == lins->mSrc[i].mTemp)
return false; return false;
} }
if (bins->mCode == IC_PUSH_FRAME || bins->mCode == IC_POP_FRAME)
{
if (lins->mCode == IC_CONSTANT && lins->mDst.mType == IT_POINTER && lins->mConst.mMemory == IM_FRAME)
return false;
}
return true; return true;
} }

View File

@ -141,6 +141,8 @@ public:
void FlushAll(void); void FlushAll(void);
void FlushCallAliases(void); void FlushCallAliases(void);
void FlushFrameAliases(void);
void RemoveValue(int index); void RemoveValue(int index);
void InsertValue(InterInstruction * ins); void InsertValue(InterInstruction * ins);

View File

@ -658,7 +658,7 @@ void InterCodeGenerator::BuildSwitchTree(InterCodeProcedure* proc, InterCodeBasi
} }
} }
InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock*& block, Expression* exp, InterCodeBasicBlock* breakBlock, InterCodeBasicBlock* continueBlock, InlineMapper* inlineMapper) InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock*& block, Expression* exp, InterCodeBasicBlock* breakBlock, InterCodeBasicBlock* continueBlock, InlineMapper* inlineMapper, ExValue* lrexp)
{ {
Declaration* dec; Declaration* dec;
ExValue vl, vr; ExValue vl, vr;
@ -928,8 +928,16 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration*
case EX_ASSIGNMENT: case EX_ASSIGNMENT:
{ {
vr = TranslateExpression(procType, proc, block, exp->mRight, breakBlock, continueBlock, inlineMapper); if (exp->mLeft->mDecType->mType == DT_TYPE_STRUCT)
vl = TranslateExpression(procType, proc, block, exp->mLeft, breakBlock, continueBlock, inlineMapper); {
vl = TranslateExpression(procType, proc, block, exp->mLeft, breakBlock, continueBlock, inlineMapper);
vr = TranslateExpression(procType, proc, block, exp->mRight, breakBlock, continueBlock, inlineMapper, &vl);
}
else
{
vr = TranslateExpression(procType, proc, block, exp->mRight, breakBlock, continueBlock, inlineMapper);
vl = TranslateExpression(procType, proc, block, exp->mLeft, breakBlock, continueBlock, inlineMapper);
}
if (exp->mToken == TK_ASSIGN || !(vl.mType->mType == DT_TYPE_POINTER && vr.mType->IsIntegerType() && (exp->mToken == TK_ASSIGN_ADD || exp->mToken == TK_ASSIGN_SUB))) if (exp->mToken == TK_ASSIGN || !(vl.mType->mType == DT_TYPE_POINTER && vr.mType->IsIntegerType() && (exp->mToken == TK_ASSIGN_ADD || exp->mToken == TK_ASSIGN_SUB)))
{ {
@ -950,16 +958,18 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration*
if (vr.mReference != 1) if (vr.mReference != 1)
mErrors->Error(exp->mLeft->mLocation, EERR_NOT_AN_LVALUE, "Not an adressable expression"); mErrors->Error(exp->mLeft->mLocation, EERR_NOT_AN_LVALUE, "Not an adressable expression");
if (vr.mTemp != vl.mTemp)
InterInstruction * ins = new InterInstruction(); {
ins->mCode = IC_COPY; InterInstruction* ins = new InterInstruction();
ins->mConst.mMemory = IM_INDIRECT; ins->mCode = IC_COPY;
ins->mSrc[0].mType = IT_POINTER; ins->mConst.mMemory = IM_INDIRECT;
ins->mSrc[0].mTemp = vr.mTemp; ins->mSrc[0].mType = IT_POINTER;
ins->mSrc[1].mType = IT_POINTER; ins->mSrc[0].mTemp = vr.mTemp;
ins->mSrc[1].mTemp = vl.mTemp; ins->mSrc[1].mType = IT_POINTER;
ins->mConst.mOperandSize = vl.mType->mSize; ins->mSrc[1].mTemp = vl.mTemp;
block->Append(ins); ins->mConst.mOperandSize = vl.mType->mSize;
block->Append(ins);
}
} }
else else
{ {
@ -1925,24 +1935,36 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration*
if (ftype->mBase->mType == DT_TYPE_STRUCT) if (ftype->mBase->mType == DT_TYPE_STRUCT)
{ {
int nindex = proc->mNumLocals++; int ttemp;
if (lrexp)
{
ttemp = lrexp->mTemp;
Declaration* vdec = new Declaration(exp->mLocation, DT_VARIABLE); decResult = lrexp->mType;
}
else
{
int nindex = proc->mNumLocals++;
vdec->mVarIndex = nindex; Declaration* vdec = new Declaration(exp->mLocation, DT_VARIABLE);
vdec->mBase = ftype->mBase;
vdec->mSize = ftype->mBase->mSize;
decResult = vdec; vdec->mVarIndex = nindex;
vdec->mBase = ftype->mBase;
vdec->mSize = ftype->mBase->mSize;
InterInstruction* vins = new InterInstruction(); decResult = vdec;
vins->mCode = IC_CONSTANT;
vins->mDst.mType = IT_POINTER; InterInstruction* vins = new InterInstruction();
vins->mDst.mTemp = proc->AddTemporary(IT_POINTER); vins->mCode = IC_CONSTANT;
vins->mConst.mMemory = IM_LOCAL; vins->mDst.mType = IT_POINTER;
vins->mConst.mVarIndex = nindex; vins->mDst.mTemp = proc->AddTemporary(IT_POINTER);
vins->mConst.mOperandSize = ftype->mBase->mSize; vins->mConst.mMemory = IM_LOCAL;
block->Append(vins); vins->mConst.mVarIndex = nindex;
vins->mConst.mOperandSize = ftype->mBase->mSize;
block->Append(vins);
ttemp = vins->mDst.mTemp;
}
InterInstruction* ains = new InterInstruction(); InterInstruction* ains = new InterInstruction();
ains->mCode = IC_CONSTANT; ains->mCode = IC_CONSTANT;
@ -1961,15 +1983,13 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration*
wins->mCode = IC_STORE; wins->mCode = IC_STORE;
wins->mSrc[1].mMemory = IM_INDIRECT; wins->mSrc[1].mMemory = IM_INDIRECT;
wins->mSrc[0].mType = IT_POINTER; wins->mSrc[0].mType = IT_POINTER;
wins->mSrc[0].mTemp = vins->mDst.mTemp; wins->mSrc[0].mTemp = ttemp;
wins->mSrc[1].mType = IT_POINTER; wins->mSrc[1].mType = IT_POINTER;
wins->mSrc[1].mTemp = ains->mDst.mTemp; wins->mSrc[1].mTemp = ains->mDst.mTemp;
wins->mSrc[1].mOperandSize = 2; wins->mSrc[1].mOperandSize = 2;
block->Append(wins); block->Append(wins);
atotal = 2; atotal = 2;
decResult = vdec;
} }
if (exp->mLeft->mDecValue->mType == DT_CONST_FUNCTION) if (exp->mLeft->mDecValue->mType == DT_CONST_FUNCTION)
@ -2028,7 +2048,9 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration*
pex = nullptr; 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 (vr.mType->mType == DT_TYPE_STRUCT || vr.mType->mType == DT_TYPE_UNION)
{ {
@ -2040,17 +2062,20 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration*
if (vr.mReference != 1) if (vr.mReference != 1)
mErrors->Error(exp->mLeft->mLocation, EERR_NOT_AN_LVALUE, "Not an adressable expression"); mErrors->Error(exp->mLeft->mLocation, EERR_NOT_AN_LVALUE, "Not an adressable expression");
InterInstruction* cins = new InterInstruction(); if (vp.mTemp != vr.mTemp)
cins->mCode = IC_COPY; {
cins->mConst.mMemory = IM_INDIRECT; InterInstruction* cins = new InterInstruction();
cins->mSrc[0].mType = IT_POINTER; cins->mCode = IC_COPY;
cins->mSrc[0].mTemp = vr.mTemp; cins->mConst.mMemory = IM_INDIRECT;
cins->mSrc[1].mType = IT_POINTER; cins->mSrc[0].mType = IT_POINTER;
cins->mSrc[1].mTemp = ains->mDst.mTemp; cins->mSrc[0].mTemp = vr.mTemp;
cins->mConst.mOperandSize = vr.mType->mSize; cins->mSrc[1].mType = IT_POINTER;
block->Append(cins); cins->mSrc[1].mTemp = ains->mDst.mTemp;
cins->mConst.mOperandSize = vr.mType->mSize;
block->Append(cins);
}
atotal += cins->mConst.mOperandSize; atotal += vr.mType->mSize;
} }
else else
{ {
@ -2134,6 +2159,9 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration*
if (decResult) if (decResult)
{ {
if (lrexp)
return *lrexp;
InterInstruction* vins = new InterInstruction(); InterInstruction* vins = new InterInstruction();
vins->mCode = IC_CONSTANT; vins->mCode = IC_CONSTANT;
vins->mDst.mType = IT_POINTER; vins->mDst.mType = IT_POINTER;

View File

@ -54,7 +54,7 @@ protected:
ExValue Dereference(InterCodeProcedure* proc, InterCodeBasicBlock*& block, ExValue v, int level = 0); ExValue Dereference(InterCodeProcedure* proc, InterCodeBasicBlock*& block, ExValue v, int level = 0);
ExValue CoerceType(InterCodeProcedure* proc, InterCodeBasicBlock*& block, ExValue v, Declaration * type); ExValue CoerceType(InterCodeProcedure* proc, InterCodeBasicBlock*& block, ExValue v, Declaration * type);
ExValue TranslateExpression(Declaration * procType, InterCodeProcedure * proc, InterCodeBasicBlock*& block, Expression* exp, InterCodeBasicBlock* breakBlock, InterCodeBasicBlock* continueBlock, InlineMapper * inlineMapper); ExValue TranslateExpression(Declaration * procType, InterCodeProcedure * proc, InterCodeBasicBlock*& block, Expression* exp, InterCodeBasicBlock* breakBlock, InterCodeBasicBlock* continueBlock, InlineMapper * inlineMapper, ExValue * lrexp = nullptr);
void TranslateLogic(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock* block, InterCodeBasicBlock* tblock, InterCodeBasicBlock* fblock, Expression* exp, InlineMapper* inlineMapper); void TranslateLogic(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock* block, InterCodeBasicBlock* tblock, InterCodeBasicBlock* fblock, Expression* exp, InlineMapper* inlineMapper);
void BuildInitializer(InterCodeModule* mod, uint8 * dp, int offset, Declaration* data, InterVariable * variable); void BuildInitializer(InterCodeModule* mod, uint8 * dp, int offset, Declaration* data, InterVariable * variable);