Skip to content

Commit

Permalink
Using default evm version in StackLayoutGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
r0qs committed Dec 19, 2022
1 parent 6e15741 commit 34c0beb
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 23 deletions.
2 changes: 1 addition & 1 deletion libyul/backends/evm/OptimizedEVMCodeTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ vector<StackTooDeepError> OptimizedEVMCodeTransform::run(
)
{
std::unique_ptr<CFG> dfg = ControlFlowGraphBuilder::build(_analysisInfo, _dialect, _block);
StackLayout stackLayout = StackLayoutGenerator::run(_dialect.evmVersion(), *dfg);
StackLayout stackLayout = StackLayoutGenerator::run(*dfg);
OptimizedEVMCodeTransform optimizedCodeTransform(
_assembly,
_builtinContext,
Expand Down
26 changes: 13 additions & 13 deletions libyul/backends/evm/StackLayoutGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,28 @@ using namespace solidity;
using namespace solidity::yul;
using namespace std;

StackLayout StackLayoutGenerator::run(langutil::EVMVersion _evmVersion, CFG const& _cfg)
StackLayout StackLayoutGenerator::run(CFG const& _cfg)
{
StackLayout stackLayout;
StackLayoutGenerator{_evmVersion, stackLayout}.processEntryPoint(*_cfg.entry);
StackLayoutGenerator{stackLayout}.processEntryPoint(*_cfg.entry);

for (auto& functionInfo: _cfg.functionInfo | ranges::views::values)
StackLayoutGenerator{_evmVersion, stackLayout}.processEntryPoint(*functionInfo.entry, &functionInfo);
StackLayoutGenerator{stackLayout}.processEntryPoint(*functionInfo.entry, &functionInfo);

return stackLayout;
}

map<YulString, vector<StackLayoutGenerator::StackTooDeep>> StackLayoutGenerator::reportStackTooDeep(langutil::EVMVersion _evmVersion, CFG const& _cfg)
map<YulString, vector<StackLayoutGenerator::StackTooDeep>> StackLayoutGenerator::reportStackTooDeep(CFG const& _cfg)
{
map<YulString, vector<StackLayoutGenerator::StackTooDeep>> stackTooDeepErrors;
stackTooDeepErrors[YulString{}] = reportStackTooDeep(_evmVersion, _cfg, YulString{});
stackTooDeepErrors[YulString{}] = reportStackTooDeep(_cfg, YulString{});
for (auto const& function: _cfg.functions)
if (auto errors = reportStackTooDeep(_evmVersion, _cfg, function->name); !errors.empty())
if (auto errors = reportStackTooDeep(_cfg, function->name); !errors.empty())
stackTooDeepErrors[function->name] = std::move(errors);
return stackTooDeepErrors;
}

vector<StackLayoutGenerator::StackTooDeep> StackLayoutGenerator::reportStackTooDeep(langutil::EVMVersion _evmVersion, CFG const& _cfg, YulString _functionName)
vector<StackLayoutGenerator::StackTooDeep> StackLayoutGenerator::reportStackTooDeep(CFG const& _cfg, YulString _functionName)
{
StackLayout stackLayout;
CFG::FunctionInfo const* functionInfo = nullptr;
Expand All @@ -83,13 +83,13 @@ vector<StackLayoutGenerator::StackTooDeep> StackLayoutGenerator::reportStackTooD
yulAssert(functionInfo, "Function not found.");
}

StackLayoutGenerator generator{_evmVersion, stackLayout};
StackLayoutGenerator generator{stackLayout};
CFG::BasicBlock const* entry = functionInfo ? functionInfo->entry : _cfg.entry;
generator.processEntryPoint(*entry);
return generator.reportStackTooDeep(*entry);
}

StackLayoutGenerator::StackLayoutGenerator(langutil::EVMVersion _evmVersion, StackLayout& _layout): m_evmVersion(_evmVersion), m_layout(_layout)
StackLayoutGenerator::StackLayoutGenerator(StackLayout& _layout): m_layout(_layout)
{
}

Expand Down Expand Up @@ -747,23 +747,23 @@ void StackLayoutGenerator::fillInJunk(CFG::BasicBlock const& _block, CFG::Functi
if (_swapDepth > 16)
opGas += 1000;
else
opGas += evmasm::GasMeter::runGas(evmasm::swapInstruction(_swapDepth), m_evmVersion);
opGas += evmasm::GasMeter::runGas(evmasm::swapInstruction(_swapDepth), langutil::EVMVersion());
};
auto dupOrPush = [&](StackSlot const& _slot)
{
if (canBeFreelyGenerated(_slot))
opGas += evmasm::GasMeter::runGas(evmasm::pushInstruction(32), m_evmVersion);
opGas += evmasm::GasMeter::runGas(evmasm::pushInstruction(32), langutil::EVMVersion());
else
{
auto depth = util::findOffset(_source | ranges::views::reverse, _slot);
yulAssert(depth);
if (*depth < 16)
opGas += evmasm::GasMeter::runGas(evmasm::dupInstruction(static_cast<unsigned>(*depth + 1)), m_evmVersion);
opGas += evmasm::GasMeter::runGas(evmasm::dupInstruction(static_cast<unsigned>(*depth + 1)), langutil::EVMVersion());
else
opGas += 1000;
}
};
auto pop = [&]() { opGas += evmasm::GasMeter::runGas(evmasm::Instruction::POP,m_evmVersion); };
auto pop = [&]() { opGas += evmasm::GasMeter::runGas(evmasm::Instruction::POP,langutil::EVMVersion()); };
createStackLayout(_source, _target, swap, dupOrPush, pop);
return opGas;
};
Expand Down
10 changes: 4 additions & 6 deletions libyul/backends/evm/StackLayoutGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#pragma once

#include <libyul/backends/evm/ControlFlowGraph.h>
#include <liblangutil/EVMVersion.h>

#include <map>

Expand Down Expand Up @@ -56,18 +55,18 @@ class StackLayoutGenerator
std::vector<YulString> variableChoices;
};

static StackLayout run(langutil::EVMVersion _evmVersion, CFG const& _cfg);
static StackLayout run(CFG const& _cfg);
/// @returns a map from function names to the stack too deep errors occurring in that function.
/// Requires @a _cfg to be a control flow graph generated from disambiguated Yul.
/// The empty string is mapped to the stack too deep errors of the main entry point.
static std::map<YulString, std::vector<StackTooDeep>> reportStackTooDeep(langutil::EVMVersion _evmVersion, CFG const& _cfg);
static std::map<YulString, std::vector<StackTooDeep>> reportStackTooDeep(CFG const& _cfg);
/// @returns all stack too deep errors in the function named @a _functionName.
/// Requires @a _cfg to be a control flow graph generated from disambiguated Yul.
/// If @a _functionName is empty, the stack too deep errors of the main entry point are reported instead.
static std::vector<StackTooDeep> reportStackTooDeep(langutil::EVMVersion _evmVersion, CFG const& _cfg, YulString _functionName);
static std::vector<StackTooDeep> reportStackTooDeep(CFG const& _cfg, YulString _functionName);

private:
StackLayoutGenerator(langutil::EVMVersion _evmVersion, StackLayout& _context);
StackLayoutGenerator(StackLayout& _context);

/// @returns the optimal entry stack layout, s.t. @a _operation can be applied to it and
/// the result can be transformed to @a _exitStack with minimal stack shuffling.
Expand Down Expand Up @@ -115,7 +114,6 @@ class StackLayoutGenerator
//// Fills in junk when entering branches that do not need a clean stack in case the result is cheaper.
void fillInJunk(CFG::BasicBlock const& _block, CFG::FunctionInfo const* _functionInfo = nullptr);

langutil::EVMVersion m_evmVersion;
StackLayout& m_layout;
};

Expand Down
2 changes: 1 addition & 1 deletion libyul/optimiser/StackCompressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ bool StackCompressor::run(
eliminateVariablesOptimizedCodegen(
_dialect,
*_object.code,
StackLayoutGenerator::reportStackTooDeep(evmDialect->evmVersion(), *cfg),
StackLayoutGenerator::reportStackTooDeep(*cfg),
allowMSizeOptimzation
);
}
Expand Down
2 changes: 1 addition & 1 deletion libyul/optimiser/StackLimitEvader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void StackLimitEvader::run(
{
yul::AsmAnalysisInfo analysisInfo = yul::AsmAnalyzer::analyzeStrictAssertCorrect(*evmDialect, _object);
unique_ptr<CFG> cfg = ControlFlowGraphBuilder::build(analysisInfo, *evmDialect, *_object.code);
run(_context, _object, StackLayoutGenerator::reportStackTooDeep(evmDialect->evmVersion(), *cfg));
run(_context, _object, StackLayoutGenerator::reportStackTooDeep(*cfg));
}
else
run(_context, _object, CompilabilityChecker{
Expand Down
2 changes: 1 addition & 1 deletion test/libyul/StackLayoutGeneratorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ TestCase::TestResult StackLayoutGeneratorTest::run(ostream& _stream, string cons
std::ostringstream output;

std::unique_ptr<CFG> cfg = ControlFlowGraphBuilder::build(*analysisInfo, *m_dialect, *object->code);
StackLayout stackLayout = StackLayoutGenerator::run(solidity::test::CommonOptions::get().evmVersion(), *cfg);
StackLayout stackLayout = StackLayoutGenerator::run(*cfg);

output << "digraph CFG {\nnodesep=0.7;\nnode[shape=box];\n\n";
StackLayoutPrinter printer{output, stackLayout};
Expand Down

0 comments on commit 34c0beb

Please sign in to comment.