Skip to content

Commit 953bc9f

Browse files
authored
Merge pull request ethereum#12983 from ethereum/hint-via-ir
Recommend `via-ir` whenever a stack too deep error is encountered.
2 parents f617d27 + 5ae17c8 commit 953bc9f

11 files changed

+59
-20
lines changed

libevmasm/CommonSubexpressionEliminator.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <libsolutil/Keccak256.h>
2727
#include <libevmasm/CommonSubexpressionEliminator.h>
2828
#include <libevmasm/AssemblyItem.h>
29+
#include <libsolutil/StackTooDeepString.h>
2930

3031
#include <range/v3/view/reverse.hpp>
3132

@@ -163,7 +164,7 @@ AssemblyItems CSECodeGenerator::generateCode(
163164
// Invalid sequenced operation.
164165
// @todo quick fix for now. Proper fix needs to choose representative with higher
165166
// sequence number during dependency analysis.
166-
assertThrow(seqNr >= _initialSequenceNumber, StackTooDeepException, "");
167+
assertThrow(seqNr >= _initialSequenceNumber, StackTooDeepException, util::stackTooDeepString);
167168
sequencedExpressions.insert(make_pair(seqNr, id));
168169
}
169170

@@ -471,7 +472,7 @@ void CSECodeGenerator::appendDup(int _fromPosition, SourceLocation const& _locat
471472
{
472473
assertThrow(_fromPosition != c_invalidPosition, OptimizerException, "");
473474
int instructionNum = 1 + m_stackHeight - _fromPosition;
474-
assertThrow(instructionNum <= 16, StackTooDeepException, "Stack too deep, try removing local variables.");
475+
assertThrow(instructionNum <= 16, StackTooDeepException, util::stackTooDeepString);
475476
assertThrow(1 <= instructionNum, OptimizerException, "Invalid stack access.");
476477
appendItem(AssemblyItem(dupInstruction(static_cast<unsigned>(instructionNum)), _location));
477478
m_stack[m_stackHeight] = m_stack[_fromPosition];
@@ -484,7 +485,7 @@ void CSECodeGenerator::appendOrRemoveSwap(int _fromPosition, SourceLocation cons
484485
if (_fromPosition == m_stackHeight)
485486
return;
486487
int instructionNum = m_stackHeight - _fromPosition;
487-
assertThrow(instructionNum <= 16, StackTooDeepException, "Stack too deep, try removing local variables.");
488+
assertThrow(instructionNum <= 16, StackTooDeepException, util::stackTooDeepString);
488489
assertThrow(1 <= instructionNum, OptimizerException, "Invalid stack access.");
489490
appendItem(AssemblyItem(swapInstruction(static_cast<unsigned>(instructionNum)), _location));
490491

libsolidity/codegen/ArrayUtils.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include <libsolutil/FunctionSelector.h>
3333
#include <libsolutil/Whiskers.h>
34+
#include <libsolutil/StackTooDeepString.h>
3435

3536
#include <libevmasm/Instruction.h>
3637
#include <liblangutil/Exceptions.h>
@@ -242,7 +243,7 @@ void ArrayUtils::copyArrayToStorage(ArrayType const& _targetType, ArrayType cons
242243
assertThrow(
243244
2 + byteOffsetSize + sourceBaseType->sizeOnStack() <= 16,
244245
StackTooDeepError,
245-
"Stack too deep, try removing local variables."
246+
util::stackTooDeepString
246247
);
247248
// fetch target storage reference
248249
_context << dupInstruction(2 + byteOffsetSize + sourceBaseType->sizeOnStack());

libsolidity/codegen/CompilerContext.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
#include <libsolutil/Whiskers.h>
4545
#include <libsolutil/FunctionSelector.h>
46+
#include <libsolutil/StackTooDeepString.h>
4647

4748
#include <liblangutil/ErrorReporter.h>
4849
#include <liblangutil/Scanner.h>
@@ -422,7 +423,7 @@ void CompilerContext::appendInlineAssembly(
422423
BOOST_THROW_EXCEPTION(
423424
StackTooDeepError() <<
424425
errinfo_sourceLocation(nativeLocationOf(_identifier)) <<
425-
util::errinfo_comment("Stack too deep (" + to_string(stackDiff) + "), try removing local variables.")
426+
util::errinfo_comment(util::stackTooDeepString)
426427
);
427428
if (_context == yul::IdentifierContext::RValue)
428429
_assembly.appendInstruction(dupInstruction(static_cast<unsigned>(stackDiff)));

libsolidity/codegen/CompilerUtils.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <libsolutil/FunctionSelector.h>
3232
#include <libevmasm/Instruction.h>
3333
#include <libsolutil/Whiskers.h>
34+
#include <libsolutil/StackTooDeepString.h>
3435

3536
using namespace std;
3637
using namespace solidity;
@@ -476,7 +477,7 @@ void CompilerUtils::encodeToMemory(
476477
assertThrow(
477478
(argSize + dynPointers) < 16,
478479
StackTooDeepError,
479-
"Stack too deep, try using fewer variables."
480+
util::stackTooDeepString
480481
);
481482
}
482483
else
@@ -537,7 +538,7 @@ void CompilerUtils::encodeToMemory(
537538
assertThrow(
538539
(2 + dynPointers) <= 16,
539540
StackTooDeepError,
540-
"Stack too deep(" + to_string(2 + dynPointers) + "), try using fewer variables."
541+
util::stackTooDeepString
541542
);
542543
m_context << dupInstruction(2 + dynPointers) << Instruction::DUP2;
543544
m_context << Instruction::SUB;
@@ -1418,7 +1419,7 @@ void CompilerUtils::moveToStackVariable(VariableDeclaration const& _variable)
14181419
BOOST_THROW_EXCEPTION(
14191420
StackTooDeepError() <<
14201421
errinfo_sourceLocation(_variable.location()) <<
1421-
util::errinfo_comment("Stack too deep, try removing local variables.")
1422+
util::errinfo_comment(util::stackTooDeepString)
14221423
);
14231424
for (unsigned i = 0; i < size; ++i)
14241425
m_context << swapInstruction(stackPosition - size + 1) << Instruction::POP;
@@ -1429,7 +1430,7 @@ void CompilerUtils::copyToStackTop(unsigned _stackDepth, unsigned _itemSize)
14291430
assertThrow(
14301431
_stackDepth <= 16,
14311432
StackTooDeepError,
1432-
"Stack too deep, try removing local variables."
1433+
util::stackTooDeepString
14331434
);
14341435
for (unsigned i = 0; i < _itemSize; ++i)
14351436
m_context << dupInstruction(_stackDepth);
@@ -1455,7 +1456,7 @@ void CompilerUtils::rotateStackUp(unsigned _items)
14551456
assertThrow(
14561457
_items - 1 <= 16,
14571458
StackTooDeepError,
1458-
"Stack too deep, try removing local variables."
1459+
util::stackTooDeepString
14591460
);
14601461
for (unsigned i = 1; i < _items; ++i)
14611462
m_context << swapInstruction(_items - i);
@@ -1466,7 +1467,7 @@ void CompilerUtils::rotateStackDown(unsigned _items)
14661467
assertThrow(
14671468
_items - 1 <= 16,
14681469
StackTooDeepError,
1469-
"Stack too deep, try removing local variables."
1470+
util::stackTooDeepString
14701471
);
14711472
for (unsigned i = 1; i < _items; ++i)
14721473
m_context << swapInstruction(i);

libsolidity/codegen/ContractCompiler.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
#include <libsolutil/Whiskers.h>
5050
#include <libsolutil/FunctionSelector.h>
51+
#include <libsolutil/StackTooDeepString.h>
5152

5253
#include <range/v3/view/reverse.hpp>
5354

@@ -672,7 +673,7 @@ bool ContractCompiler::visit(FunctionDefinition const& _function)
672673
BOOST_THROW_EXCEPTION(
673674
StackTooDeepError() <<
674675
errinfo_sourceLocation(_function.location()) <<
675-
util::errinfo_comment("Stack too deep, try removing local variables.")
676+
util::errinfo_comment(util::stackTooDeepString)
676677
);
677678
while (!stackLayout.empty() && stackLayout.back() != static_cast<int>(stackLayout.size() - 1))
678679
if (stackLayout.back() < 0)
@@ -842,7 +843,7 @@ bool ContractCompiler::visit(InlineAssembly const& _inlineAssembly)
842843
BOOST_THROW_EXCEPTION(
843844
StackTooDeepError() <<
844845
errinfo_sourceLocation(_inlineAssembly.location()) <<
845-
util::errinfo_comment("Stack too deep, try removing local variables.")
846+
util::errinfo_comment(util::stackTooDeepString)
846847
);
847848
_assembly.appendInstruction(dupInstruction(stackDiff));
848849
}
@@ -916,7 +917,7 @@ bool ContractCompiler::visit(InlineAssembly const& _inlineAssembly)
916917
BOOST_THROW_EXCEPTION(
917918
StackTooDeepError() <<
918919
errinfo_sourceLocation(_inlineAssembly.location()) <<
919-
util::errinfo_comment("Stack too deep(" + to_string(stackDiff) + "), try removing local variables.")
920+
util::errinfo_comment(util::stackTooDeepString)
920921
);
921922
_assembly.appendInstruction(swapInstruction(stackDiff));
922923
_assembly.appendInstruction(Instruction::POP);

libsolidity/codegen/ExpressionCompiler.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <libsolutil/FunctionSelector.h>
3838
#include <libsolutil/Keccak256.h>
3939
#include <libsolutil/Whiskers.h>
40+
#include <libsolutil/StackTooDeepString.h>
4041

4142
#include <boost/algorithm/string/replace.hpp>
4243
#include <numeric>
@@ -268,7 +269,7 @@ void ExpressionCompiler::appendStateVariableAccessor(VariableDeclaration const&
268269
BOOST_THROW_EXCEPTION(
269270
StackTooDeepError() <<
270271
errinfo_sourceLocation(_varDecl.location()) <<
271-
util::errinfo_comment("Stack too deep.")
272+
util::errinfo_comment(util::stackTooDeepString)
272273
);
273274
m_context << dupInstruction(retSizeOnStack + 1);
274275
m_context.appendJump(evmasm::AssemblyItem::JumpType::OutOfFunction);
@@ -350,7 +351,7 @@ bool ExpressionCompiler::visit(Assignment const& _assignment)
350351
BOOST_THROW_EXCEPTION(
351352
StackTooDeepError() <<
352353
errinfo_sourceLocation(_assignment.location()) <<
353-
util::errinfo_comment("Stack too deep, try removing local variables.")
354+
util::errinfo_comment(util::stackTooDeepString)
354355
);
355356
// value [lvalue_ref] updated_value
356357
for (unsigned i = 0; i < itemSize; ++i)

libsolidity/codegen/LValue.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include <libsolidity/codegen/CompilerUtils.h>
2929
#include <libevmasm/Instruction.h>
3030

31+
#include <libsolutil/StackTooDeepString.h>
32+
3133
using namespace std;
3234
using namespace solidity;
3335
using namespace solidity::evmasm;
@@ -50,7 +52,7 @@ void StackVariable::retrieveValue(SourceLocation const& _location, bool) const
5052
BOOST_THROW_EXCEPTION(
5153
StackTooDeepError() <<
5254
errinfo_sourceLocation(_location) <<
53-
util::errinfo_comment("Stack too deep, try removing local variables.")
55+
util::errinfo_comment(util::stackTooDeepString)
5456
);
5557
solAssert(stackPos + 1 >= m_size, "Size and stack pos mismatch.");
5658
for (unsigned i = 0; i < m_size; ++i)
@@ -64,7 +66,7 @@ void StackVariable::storeValue(Type const&, SourceLocation const& _location, boo
6466
BOOST_THROW_EXCEPTION(
6567
StackTooDeepError() <<
6668
errinfo_sourceLocation(_location) <<
67-
util::errinfo_comment("Stack too deep, try removing local variables.")
69+
util::errinfo_comment(util::stackTooDeepString)
6870
);
6971
else if (stackDiff > 0)
7072
for (unsigned i = 0; i < m_size; ++i)

libsolutil/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ set(sources
2828
picosha2.h
2929
Result.h
3030
SetOnce.h
31+
StackTooDeepString.h
3132
StringUtils.cpp
3233
StringUtils.h
3334
SwarmHash.cpp

libsolutil/StackTooDeepString.h

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
This file is part of solidity.
3+
4+
solidity is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
solidity is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with solidity. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
// SPDX-License-Identifier: GPL-3.0
18+
#pragma once
19+
20+
#include <string>
21+
22+
namespace solidity::util
23+
{
24+
static std::string stackTooDeepString =
25+
"Stack too deep. "
26+
"Try compiling with `--via-ir` (cli) or the equivalent `viaIR: true` (standard JSON) "
27+
"while enabling the optimizer. Otherwise, try removing local variables.";
28+
}

libyul/backends/evm/AsmCodeGen.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <libyul/AST.h>
2727
#include <libyul/AsmAnalysisInfo.h>
2828

29+
#include <libsolutil/StackTooDeepString.h>
30+
2931
using namespace std;
3032
using namespace solidity;
3133
using namespace solidity::yul;
@@ -61,7 +63,7 @@ void CodeGenerator::assemble(
6163
assertThrow(
6264
false,
6365
langutil::StackTooDeepError,
64-
"Stack too deep when compiling inline assembly" +
66+
util::stackTooDeepString + " When compiling inline assembly" +
6567
(transform.stackErrors().front().comment() ? ": " + *transform.stackErrors().front().comment() : ".")
6668
);
6769
}

test/libsolidity/StandardCompiler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,7 @@ BOOST_AUTO_TEST_CASE(use_stack_optimization)
14281428
BOOST_REQUIRE(result["errors"].isArray());
14291429
BOOST_CHECK(result["errors"][0]["severity"] == "error");
14301430
BOOST_REQUIRE(result["errors"][0]["message"].isString());
1431-
BOOST_CHECK(result["errors"][0]["message"].asString().find("Stack too deep when compiling inline assembly") != std::string::npos);
1431+
BOOST_CHECK(result["errors"][0]["message"].asString().find("When compiling inline assembly") != std::string::npos);
14321432
BOOST_CHECK(result["errors"][0]["type"] == "CompilerError");
14331433
}
14341434

0 commit comments

Comments
 (0)