Skip to content

Commit 0977921

Browse files
committed
Avoid declare variable when stack size is zero
1 parent a9a049e commit 0977921

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

Diff for: libsolidity/experimental/codegen/IRGeneratorForStatements.cpp

+11-5
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,10 @@ void IRGeneratorForStatements::endVisit(BinaryOperation const& _binaryOperation)
205205
// TODO: get around resolveRecursive by passing the environment further down?
206206
functionType = m_context.env->resolveRecursive(functionType);
207207
m_context.enqueueFunctionDefinition(&functionDefinition, functionType);
208-
m_code << "let " << var(_binaryOperation).commaSeparatedList() <<
209-
" := " << IRNames::function(*m_context.env, functionDefinition, functionType) << "(" <<
208+
std::string functionDeclaration = var(_binaryOperation).commaSeparatedList();
209+
if (!functionDeclaration.empty())
210+
m_code << "let " << functionDeclaration << " := ";
211+
m_code << IRNames::function(*m_context.env, functionDefinition, functionType) << "(" <<
210212
var(_binaryOperation.leftExpression()).commaSeparatedList() <<
211213
var(_binaryOperation.rightExpression()).commaSeparatedListPrefixed() << ")\n";
212214
}
@@ -344,15 +346,19 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
344346
m_context.enqueueFunctionDefinition(functionDefinition, functionType);
345347
// TODO: account for return stack size
346348
solAssert(!functionDefinition->returnParameterList());
347-
if (functionDefinition->experimentalReturnExpression())
349+
std::string functionDeclaration = var(_functionCall).commaSeparatedList();
350+
if (!functionDeclaration.empty())
348351
m_code << "let " << var(_functionCall).commaSeparatedList() << " := ";
349352
m_code << IRNames::function(*m_context.env, *functionDefinition, functionType) << "(";
350353
auto const& arguments = _functionCall.arguments();
351-
if (arguments.size() > 1)
354+
if (arguments.size() == 1)
355+
m_code << var(*arguments.back()).commaSeparatedList();
356+
else if (arguments.size() > 1)
357+
{
352358
for (auto arg: arguments | ranges::views::drop_last(1))
353359
m_code << var(*arg).commaSeparatedList();
354-
if (!arguments.empty())
355360
m_code << var(*arguments.back()).commaSeparatedListPrefixed();
361+
}
356362
m_code << ")\n";
357363
}
358364

Diff for: libsolidity/experimental/codegen/IRVariable.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,12 @@ IRVariable::IRVariable(Expression const& _expression, Type _type, size_t _stackS
4646
std::vector<std::string> IRVariable::stackSlots() const
4747
{
4848
std::vector<std::string> result;
49-
if (m_stackSize > 1)
50-
{
49+
result.reserve(m_stackSize);
50+
if (m_stackSize == 1)
51+
result.emplace_back(m_baseName);
52+
else
5153
for (size_t i = 0; i < m_stackSize; ++i)
5254
result.emplace_back(suffixedName(std::to_string(i)));
53-
return result;
54-
}
55-
result.emplace_back(m_baseName);
5655
return result;
5756
}
5857

0 commit comments

Comments
 (0)