Skip to content

Commit 44c0697

Browse files
committed
Test cases for SWAPN, DUPN and related stack shuffling.
1 parent bff7605 commit 44c0697

File tree

9 files changed

+5712
-8
lines changed

9 files changed

+5712
-8
lines changed

test/libyul/StackLayoutGeneratorTest.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,10 @@ TestCase::TestResult StackLayoutGeneratorTest::run(std::ostream& _stream, std::s
232232
yulStack.parserResult()->code()->root()
233233
);
234234

235-
bool simulateFunctionsWithJumps = true;
236-
size_t reachableStackDepth = 16;
237-
if (auto const* evmDialect = dynamic_cast<EVMDialect const*>(&yulStack.dialect()))
238-
{
239-
simulateFunctionsWithJumps = !evmDialect->eofVersion().has_value();
240-
reachableStackDepth = evmDialect->reachableStackDepth();
241-
}
235+
auto const* evmDialect = dynamic_cast<EVMDialect const*>(&yulStack.dialect());
236+
solAssert(evmDialect, "StackLayoutGenerator can only be run on EVM dialects.");
237+
bool simulateFunctionsWithJumps = !evmDialect->eofVersion().has_value();
238+
size_t reachableStackDepth = evmDialect->reachableStackDepth();
242239

243240
StackLayout stackLayout = StackLayoutGenerator::run(*cfg, simulateFunctionsWithJumps, reachableStackDepth);
244241

test/libyul/StackShufflingTest.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include <liblangutil/Scanner.h>
2626
#include <libsolutil/AnsiColorized.h>
27+
#include <libsolutil/StringUtils.h>
2728

2829
using namespace solidity::test;
2930
using namespace solidity::util;
@@ -136,10 +137,20 @@ bool StackShufflingTest::parse(std::string const& _source)
136137
StackShufflingTest::StackShufflingTest(std::string const& _filename):
137138
TestCase(_filename)
138139
{
140+
processSettings();
139141
m_source = m_reader.source();
140142
m_expectation = m_reader.simpleExpectations();
141143
}
142144

145+
void StackShufflingTest::processSettings()
146+
{
147+
std::string depthString = m_reader.stringSetting("maximumStackDepth", "16");
148+
std::optional<unsigned> depth = toUnsignedInt(depthString);
149+
if (!depth.has_value())
150+
BOOST_THROW_EXCEPTION(std::runtime_error{"Invalid maximum stack depth: \"" + depthString + "\""});
151+
m_maximumStackDepth = *depth;
152+
}
153+
143154
TestCase::TestResult StackShufflingTest::run(std::ostream& _stream, std::string const& _linePrefix, bool _formatted)
144155
{
145156
auto const& dialect = CommonOptions::get().evmDialect();
@@ -150,16 +161,19 @@ TestCase::TestResult StackShufflingTest::run(std::ostream& _stream, std::string
150161
}
151162

152163
std::ostringstream output;
164+
size_t operations = 0;
153165
createStackLayout(
154166
m_sourceStack,
155167
m_targetStack,
156168
[&](unsigned _swapDepth) // swap
157169
{
170+
++operations;
158171
output << stackToString(m_sourceStack, dialect) << std::endl;
159172
output << "SWAP" << _swapDepth << std::endl;
160173
},
161174
[&](StackSlot const& _slot) // dupOrPush
162175
{
176+
++operations;
163177
output << stackToString(m_sourceStack, dialect) << std::endl;
164178
if (canBeFreelyGenerated(_slot))
165179
output << "PUSH " << stackSlotToString(_slot, dialect) << std::endl;
@@ -172,13 +186,15 @@ TestCase::TestResult StackShufflingTest::run(std::ostream& _stream, std::string
172186
}
173187
},
174188
[&](){ // pop
189+
++operations;
175190
output << stackToString(m_sourceStack, dialect) << std::endl;
176191
output << "POP" << std::endl;
177192
},
178-
16u // TODO: make it a test setting
193+
m_maximumStackDepth
179194
);
180195

181196
output << stackToString(m_sourceStack, dialect) << std::endl;
197+
output << operations << " operations" << std::endl;
182198
m_obtainedResult = output.str();
183199

184200
return checkResult(_stream, _linePrefix, _formatted);

test/libyul/StackShufflingTest.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ class StackShufflingTest: public TestCase
3737
explicit StackShufflingTest(std::string const& _filename);
3838
TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override;
3939
private:
40+
void processSettings();
4041
bool parse(std::string const& _source);
4142

43+
size_t m_maximumStackDepth{};
4244
Stack m_sourceStack;
4345
Stack m_targetStack;
4446
std::map<YulName, yul::FunctionCall> m_functions;

0 commit comments

Comments
 (0)