aboutsummaryrefslogtreecommitdiffstats
path: root/libyul/backends/evm
diff options
context:
space:
mode:
Diffstat (limited to 'libyul/backends/evm')
-rw-r--r--libyul/backends/evm/EVMCodeTransform.cpp272
-rw-r--r--libyul/backends/evm/EVMCodeTransform.h97
-rw-r--r--libyul/backends/evm/EVMDialect.cpp141
-rw-r--r--libyul/backends/evm/EVMDialect.h85
-rw-r--r--libyul/backends/evm/EVMObjectCompiler.cpp21
-rw-r--r--libyul/backends/evm/EVMObjectCompiler.h11
6 files changed, 559 insertions, 68 deletions
diff --git a/libyul/backends/evm/EVMCodeTransform.cpp b/libyul/backends/evm/EVMCodeTransform.cpp
index 12abd754..bd18985c 100644
--- a/libyul/backends/evm/EVMCodeTransform.cpp
+++ b/libyul/backends/evm/EVMCodeTransform.cpp
@@ -20,6 +20,7 @@
#include <libyul/backends/evm/EVMCodeTransform.h>
+#include <libyul/optimiser/NameCollector.h>
#include <libyul/AsmAnalysisInfo.h>
#include <libyul/AsmData.h>
@@ -32,6 +33,144 @@ using namespace dev;
using namespace yul;
using namespace dev::solidity;
+void VariableReferenceCounter::operator()(Identifier const& _identifier)
+{
+ increaseRefIfFound(_identifier.name);
+}
+
+void VariableReferenceCounter::operator()(FunctionDefinition const& _function)
+{
+ Scope* originalScope = m_scope;
+
+ solAssert(m_info.virtualBlocks.at(&_function), "");
+ m_scope = m_info.scopes.at(m_info.virtualBlocks.at(&_function).get()).get();
+ solAssert(m_scope, "Variable scope does not exist.");
+
+ for (auto const& v: _function.returnVariables)
+ increaseRefIfFound(v.name);
+
+ VariableReferenceCounter{m_context, m_info}(_function.body);
+
+ m_scope = originalScope;
+}
+
+void VariableReferenceCounter::operator()(ForLoop const& _forLoop)
+{
+ Scope* originalScope = m_scope;
+ // Special scoping rules.
+ m_scope = m_info.scopes.at(&_forLoop.pre).get();
+
+ walkVector(_forLoop.pre.statements);
+ visit(*_forLoop.condition);
+ (*this)(_forLoop.body);
+ (*this)(_forLoop.post);
+
+ m_scope = originalScope;
+}
+
+
+void VariableReferenceCounter::operator()(Block const& _block)
+{
+ Scope* originalScope = m_scope;
+ m_scope = m_info.scopes.at(&_block).get();
+
+ ASTWalker::operator()(_block);
+
+ m_scope = originalScope;
+}
+
+void VariableReferenceCounter::increaseRefIfFound(YulString _variableName)
+{
+ m_scope->lookup(_variableName, Scope::Visitor(
+ [=](Scope::Variable const& _var)
+ {
+ ++m_context.variableReferences[&_var];
+ },
+ [=](Scope::Label const&) { },
+ [=](Scope::Function const&) { }
+ ));
+}
+
+
+CodeTransform::CodeTransform(
+ AbstractAssembly& _assembly,
+ AsmAnalysisInfo& _analysisInfo,
+ Block const& _block,
+ bool _allowStackOpt,
+ EVMDialect const& _dialect,
+ bool _evm15,
+ ExternalIdentifierAccess const& _identifierAccess,
+ bool _useNamedLabelsForFunctions,
+ int _stackAdjustment,
+ shared_ptr<Context> _context
+):
+ m_assembly(_assembly),
+ m_info(_analysisInfo),
+ m_dialect(_dialect),
+ m_allowStackOpt(_allowStackOpt),
+ m_evm15(_evm15),
+ m_useNamedLabelsForFunctions(_useNamedLabelsForFunctions),
+ m_identifierAccess(_identifierAccess),
+ m_stackAdjustment(_stackAdjustment),
+ m_context(_context)
+{
+ if (!m_context)
+ {
+ // initialize
+ m_context = make_shared<Context>();
+ if (m_allowStackOpt)
+ VariableReferenceCounter{*m_context, m_info}(_block);
+ }
+}
+
+void CodeTransform::decreaseReference(YulString, Scope::Variable const& _var)
+{
+ if (!m_allowStackOpt)
+ return;
+
+ unsigned& ref = m_context->variableReferences.at(&_var);
+ solAssert(ref >= 1, "");
+ --ref;
+ if (ref == 0)
+ m_variablesScheduledForDeletion.insert(&_var);
+}
+
+bool CodeTransform::unreferenced(Scope::Variable const& _var) const
+{
+ return !m_context->variableReferences.count(&_var) || m_context->variableReferences[&_var] == 0;
+}
+
+void CodeTransform::freeUnusedVariables()
+{
+ if (!m_allowStackOpt)
+ return;
+
+ for (auto const& identifier: m_scope->identifiers)
+ if (identifier.second.type() == typeid(Scope::Variable))
+ {
+ Scope::Variable const& var = boost::get<Scope::Variable>(identifier.second);
+ if (m_variablesScheduledForDeletion.count(&var))
+ deleteVariable(var);
+ }
+
+ while (m_unusedStackSlots.count(m_assembly.stackHeight() - 1))
+ {
+ solAssert(m_unusedStackSlots.erase(m_assembly.stackHeight() - 1), "");
+ m_assembly.appendInstruction(solidity::Instruction::POP);
+ --m_stackAdjustment;
+ }
+}
+
+void CodeTransform::deleteVariable(Scope::Variable const& _var)
+{
+ solAssert(m_allowStackOpt, "");
+ solAssert(m_context->variableStackHeights.count(&_var) > 0, "");
+ m_unusedStackSlots.insert(m_context->variableStackHeights[&_var]);
+ m_context->variableStackHeights.erase(&_var);
+ m_context->variableReferences.erase(&_var);
+ m_variablesScheduledForDeletion.erase(&_var);
+}
+
void CodeTransform::operator()(VariableDeclaration const& _varDecl)
{
solAssert(m_scope, "");
@@ -49,10 +188,40 @@ void CodeTransform::operator()(VariableDeclaration const& _varDecl)
while (variablesLeft--)
m_assembly.appendConstant(u256(0));
}
- for (auto const& variable: _varDecl.variables)
+
+ bool atTopOfStack = true;
+ for (int varIndex = numVariables - 1; varIndex >= 0; --varIndex)
{
- auto& var = boost::get<Scope::Variable>(m_scope->identifiers.at(variable.name));
- m_context->variableStackHeights[&var] = height++;
+ auto& var = boost::get<Scope::Variable>(m_scope->identifiers.at(_varDecl.variables[varIndex].name));
+ m_context->variableStackHeights[&var] = height + varIndex;
+ if (!m_allowStackOpt)
+ continue;
+
+ if (unreferenced(var))
+ {
+ if (atTopOfStack)
+ {
+ m_context->variableStackHeights.erase(&var);
+ m_assembly.setSourceLocation(_varDecl.location);
+ m_assembly.appendInstruction(solidity::Instruction::POP);
+ --m_stackAdjustment;
+ }
+ else
+ m_variablesScheduledForDeletion.insert(&var);
+ }
+ else if (m_unusedStackSlots.empty())
+ atTopOfStack = false;
+ else
+ {
+ int slot = *m_unusedStackSlots.begin();
+ m_unusedStackSlots.erase(m_unusedStackSlots.begin());
+ m_context->variableStackHeights[&var] = slot;
+ m_assembly.setSourceLocation(_varDecl.location);
+ if (int heightDiff = variableHeightDiff(var, true))
+ m_assembly.appendInstruction(solidity::swapInstruction(heightDiff - 1));
+ m_assembly.appendInstruction(solidity::Instruction::POP);
+ --m_stackAdjustment;
+ }
}
checkStackHeight(&_varDecl);
}
@@ -70,6 +239,7 @@ void CodeTransform::operator()(Assignment const& _assignment)
void CodeTransform::operator()(StackAssignment const& _assignment)
{
+ solAssert(!m_allowStackOpt, "");
m_assembly.setSourceLocation(_assignment.location);
generateAssignment(_assignment.variableName);
checkStackHeight(&_assignment);
@@ -84,6 +254,7 @@ void CodeTransform::operator()(ExpressionStatement const& _statement)
void CodeTransform::operator()(Label const& _label)
{
+ solAssert(!m_allowStackOpt, "");
m_assembly.setSourceLocation(_label.location);
solAssert(m_scope, "");
solAssert(m_scope->identifiers.count(_label.name), "");
@@ -96,35 +267,46 @@ void CodeTransform::operator()(FunctionCall const& _call)
{
solAssert(m_scope, "");
- m_assembly.setSourceLocation(_call.location);
- EVMAssembly::LabelID returnLabel(-1); // only used for evm 1.0
- if (!m_evm15)
+ if (BuiltinFunctionForEVM const* builtin = m_dialect.builtin(_call.functionName.name))
{
- returnLabel = m_assembly.newLabelId();
- m_assembly.appendLabelReference(returnLabel);
- m_stackAdjustment++;
+ builtin->generateCode(_call, m_assembly, [&]() {
+ for (auto const& arg: _call.arguments | boost::adaptors::reversed)
+ visitExpression(arg);
+ m_assembly.setSourceLocation(_call.location);
+ });
}
-
- Scope::Function* function = nullptr;
- solAssert(m_scope->lookup(_call.functionName.name, Scope::NonconstVisitor(
- [=](Scope::Variable&) { solAssert(false, "Expected function name."); },
- [=](Scope::Label&) { solAssert(false, "Expected function name."); },
- [&](Scope::Function& _function) { function = &_function; }
- )), "Function name not found.");
- solAssert(function, "");
- solAssert(function->arguments.size() == _call.arguments.size(), "");
- for (auto const& arg: _call.arguments | boost::adaptors::reversed)
- visitExpression(arg);
- m_assembly.setSourceLocation(_call.location);
- if (m_evm15)
- m_assembly.appendJumpsub(functionEntryID(_call.functionName.name, *function), function->arguments.size(), function->returns.size());
else
{
- m_assembly.appendJumpTo(functionEntryID(_call.functionName.name, *function), function->returns.size() - function->arguments.size() - 1);
- m_assembly.appendLabel(returnLabel);
- m_stackAdjustment--;
+ m_assembly.setSourceLocation(_call.location);
+ EVMAssembly::LabelID returnLabel(-1); // only used for evm 1.0
+ if (!m_evm15)
+ {
+ returnLabel = m_assembly.newLabelId();
+ m_assembly.appendLabelReference(returnLabel);
+ m_stackAdjustment++;
+ }
+
+ Scope::Function* function = nullptr;
+ solAssert(m_scope->lookup(_call.functionName.name, Scope::NonconstVisitor(
+ [=](Scope::Variable&) { solAssert(false, "Expected function name."); },
+ [=](Scope::Label&) { solAssert(false, "Expected function name."); },
+ [&](Scope::Function& _function) { function = &_function; }
+ )), "Function name not found.");
+ solAssert(function, "");
+ solAssert(function->arguments.size() == _call.arguments.size(), "");
+ for (auto const& arg: _call.arguments | boost::adaptors::reversed)
+ visitExpression(arg);
+ m_assembly.setSourceLocation(_call.location);
+ if (m_evm15)
+ m_assembly.appendJumpsub(functionEntryID(_call.functionName.name, *function), function->arguments.size(), function->returns.size());
+ else
+ {
+ m_assembly.appendJumpTo(functionEntryID(_call.functionName.name, *function), function->returns.size() - function->arguments.size() - 1);
+ m_assembly.appendLabel(returnLabel);
+ m_stackAdjustment--;
+ }
+ checkStackHeight(&_call);
}
- checkStackHeight(&_call);
}
void CodeTransform::operator()(FunctionalInstruction const& _instruction)
@@ -169,11 +351,14 @@ void CodeTransform::operator()(Identifier const& _identifier)
if (m_scope->lookup(_identifier.name, Scope::NonconstVisitor(
[=](Scope::Variable& _var)
{
+ // TODO: opportunity for optimization: Do not DUP if this is the last reference
+ // to the top most element of the stack
if (int heightDiff = variableHeightDiff(_var, false))
m_assembly.appendInstruction(solidity::dupInstruction(heightDiff));
else
// Store something to balance the stack
m_assembly.appendConstant(u256(0));
+ decreaseReference(_identifier.name, _var);
},
[=](Scope::Label& _label)
{
@@ -217,6 +402,7 @@ void CodeTransform::operator()(Literal const& _literal)
void CodeTransform::operator()(yul::Instruction const& _instruction)
{
+ solAssert(!m_allowStackOpt, "");
solAssert(!m_evm15 || _instruction.instruction != solidity::Instruction::JUMP, "Bare JUMP instruction used for EVM1.5");
solAssert(!m_evm15 || _instruction.instruction != solidity::Instruction::JUMPI, "Bare JUMPI instruction used for EVM1.5");
m_assembly.setSourceLocation(_instruction.location);
@@ -329,7 +515,9 @@ void CodeTransform::operator()(FunctionDefinition const& _function)
CodeTransform(
m_assembly,
m_info,
- m_yul,
+ _function.body,
+ m_allowStackOpt,
+ m_dialect,
m_evm15,
m_identifierAccess,
m_useNamedLabelsForFunctions,
@@ -350,6 +538,7 @@ void CodeTransform::operator()(FunctionDefinition const& _function)
if (!m_evm15)
stackLayout.push_back(_function.returnVariables.size()); // Move return label to the top
stackLayout += vector<int>(_function.parameters.size(), -1); // discard all arguments
+
for (size_t i = 0; i < _function.returnVariables.size(); ++i)
stackLayout.push_back(i); // Move return values down, but keep order.
@@ -475,20 +664,37 @@ void CodeTransform::visitExpression(Expression const& _expression)
void CodeTransform::visitStatements(vector<Statement> const& _statements)
{
for (auto const& statement: _statements)
+ {
+ freeUnusedVariables();
boost::apply_visitor(*this, statement);
+ }
+ freeUnusedVariables();
}
void CodeTransform::finalizeBlock(Block const& _block, int blockStartStackHeight)
{
m_assembly.setSourceLocation(_block.location);
+ freeUnusedVariables();
+
// pop variables
solAssert(m_info.scopes.at(&_block).get() == m_scope, "");
- for (size_t i = 0; i < m_scope->numberOfVariables(); ++i)
- m_assembly.appendInstruction(solidity::Instruction::POP);
+ for (auto const& id: m_scope->identifiers)
+ if (id.second.type() == typeid(Scope::Variable))
+ {
+ Scope::Variable const& var = boost::get<Scope::Variable>(id.second);
+ if (m_allowStackOpt)
+ {
+ solAssert(!m_context->variableStackHeights.count(&var), "");
+ solAssert(!m_context->variableReferences.count(&var), "");
+ m_stackAdjustment++;
+ }
+ else
+ m_assembly.appendInstruction(solidity::Instruction::POP);
+ }
int deposit = m_assembly.stackHeight() - blockStartStackHeight;
- solAssert(deposit == 0, "Invalid stack height at end of block.");
+ solAssert(deposit == 0, "Invalid stack height at end of block: " + to_string(deposit));
checkStackHeight(&_block);
}
@@ -502,13 +708,13 @@ void CodeTransform::generateMultiAssignment(vector<Identifier> const& _variableN
void CodeTransform::generateAssignment(Identifier const& _variableName)
{
solAssert(m_scope, "");
- auto var = m_scope->lookup(_variableName.name);
- if (var)
+ if (auto var = m_scope->lookup(_variableName.name))
{
Scope::Variable const& _var = boost::get<Scope::Variable>(*var);
if (int heightDiff = variableHeightDiff(_var, true))
m_assembly.appendInstruction(solidity::swapInstruction(heightDiff - 1));
m_assembly.appendInstruction(solidity::Instruction::POP);
+ decreaseReference(_variableName.name, _var);
}
else
{
diff --git a/libyul/backends/evm/EVMCodeTransform.h b/libyul/backends/evm/EVMCodeTransform.h
index d559f85a..28ef4e45 100644
--- a/libyul/backends/evm/EVMCodeTransform.h
+++ b/libyul/backends/evm/EVMCodeTransform.h
@@ -20,8 +20,9 @@
#include <libyul/backends/evm/EVMAssembly.h>
+#include <libyul/backends/evm/EVMDialect.h>
+#include <libyul/optimiser/ASTWalker.h>
#include <libyul/AsmDataForward.h>
-
#include <libyul/AsmScope.h>
#include <boost/variant.hpp>
@@ -37,6 +38,46 @@ namespace yul
struct AsmAnalysisInfo;
class EVMAssembly;
+struct CodeTransformContext
+{
+ std::map<Scope::Label const*, AbstractAssembly::LabelID> labelIDs;
+ std::map<Scope::Function const*, AbstractAssembly::LabelID> functionEntryIDs;
+ std::map<Scope::Variable const*, int> variableStackHeights;
+ std::map<Scope::Variable const*, unsigned> variableReferences;
+};
+
+/**
+ * Counts the number of references to a variable. This includes actual (read) references
+ * but also assignments to the variable. It does not include the declaration itself or
+ * function parameters, but it does include function return parameters.
+ *
+ * This component can handle multiple variables of the same name.
+ *
+ * Can only be applied to strict assembly.
+ */
+class VariableReferenceCounter: public yul::ASTWalker
+{
+public:
+ explicit VariableReferenceCounter(
+ CodeTransformContext& _context,
+ AsmAnalysisInfo const& _assemblyInfo
+ ): m_context(_context), m_info(_assemblyInfo)
+ {}
+
+public:
+ void operator()(Identifier const& _identifier);
+ void operator()(FunctionDefinition const&);
+ void operator()(ForLoop const&);
+ void operator()(Block const& _block);
+
+private:
+ void increaseRefIfFound(YulString _variableName);
+
+ CodeTransformContext& m_context;
+ AsmAnalysisInfo const& m_info;
+ Scope* m_scope = nullptr;
+};
+
class CodeTransform: public boost::static_visitor<>
{
public:
@@ -45,50 +86,51 @@ public:
CodeTransform(
AbstractAssembly& _assembly,
AsmAnalysisInfo& _analysisInfo,
- bool _yul = false,
+ Block const& _block,
+ EVMDialect const& _dialect,
+ bool _allowStackOpt = false,
bool _evm15 = false,
ExternalIdentifierAccess const& _identifierAccess = ExternalIdentifierAccess(),
bool _useNamedLabelsForFunctions = false
): CodeTransform(
_assembly,
_analysisInfo,
- _yul,
+ _block,
+ _allowStackOpt,
+ _dialect,
_evm15,
_identifierAccess,
_useNamedLabelsForFunctions,
_assembly.stackHeight(),
- std::make_shared<Context>()
+ nullptr
)
{
}
protected:
- struct Context
- {
- std::map<Scope::Label const*, AbstractAssembly::LabelID> labelIDs;
- std::map<Scope::Function const*, AbstractAssembly::LabelID> functionEntryIDs;
- std::map<Scope::Variable const*, int> variableStackHeights;
- };
+ using Context = CodeTransformContext;
CodeTransform(
AbstractAssembly& _assembly,
AsmAnalysisInfo& _analysisInfo,
- bool _yul,
+ Block const& _block,
+ bool _allowStackOpt,
+ EVMDialect const& _dialect,
bool _evm15,
ExternalIdentifierAccess const& _identifierAccess,
bool _useNamedLabelsForFunctions,
int _stackAdjustment,
std::shared_ptr<Context> _context
- ):
- m_assembly(_assembly),
- m_info(_analysisInfo),
- m_yul(_yul),
- m_evm15(_evm15),
- m_useNamedLabelsForFunctions(_useNamedLabelsForFunctions),
- m_identifierAccess(_identifierAccess),
- m_stackAdjustment(_stackAdjustment),
- m_context(_context)
- {}
+ );
+
+ void decreaseReference(YulString _name, Scope::Variable const& _var);
+ bool unreferenced(Scope::Variable const& _var) const;
+ /// Marks slots of variables that are not used anymore
+ /// and were defined in the current scope for reuse.
+ /// Also POPs unused topmost stack slots.
+ void freeUnusedVariables();
+ /// Marks the stack slot of @a _var to be reused.
+ void deleteVariable(Scope::Variable const& _var);
public:
void operator()(Instruction const& _instruction);
@@ -137,9 +179,10 @@ private:
AbstractAssembly& m_assembly;
AsmAnalysisInfo& m_info;
Scope* m_scope = nullptr;
- bool m_yul = false;
- bool m_evm15 = false;
- bool m_useNamedLabelsForFunctions = false;
+ EVMDialect const& m_dialect;
+ bool const m_allowStackOpt = true;
+ bool const m_evm15 = false;
+ bool const m_useNamedLabelsForFunctions = false;
ExternalIdentifierAccess m_identifierAccess;
/// Adjustment between the stack height as determined during the analysis phase
/// and the stack height in the assembly. This is caused by an initial stack being present
@@ -147,6 +190,12 @@ private:
/// (EVM 1.0 or 1.5).
int m_stackAdjustment = 0;
std::shared_ptr<Context> m_context;
+
+ /// Set of variables whose reference counter has reached zero,
+ /// and whose stack slot will be marked as unused once we reach
+ /// statement level in the scope where the variable was defined.
+ std::set<Scope::Variable const*> m_variablesScheduledForDeletion;
+ std::set<int> m_unusedStackSlots;
};
}
diff --git a/libyul/backends/evm/EVMDialect.cpp b/libyul/backends/evm/EVMDialect.cpp
new file mode 100644
index 00000000..33ee19d4
--- /dev/null
+++ b/libyul/backends/evm/EVMDialect.cpp
@@ -0,0 +1,141 @@
+/*
+ This file is part of solidity.
+
+ solidity is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ solidity is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with solidity. If not, see <http://www.gnu.org/licenses/>.
+*/
+/**
+ * Yul dialects for EVM.
+ */
+
+#include <libyul/backends/evm/EVMDialect.h>
+
+#include <libyul/AsmAnalysisInfo.h>
+#include <libyul/AsmData.h>
+#include <libyul/Object.h>
+#include <libyul/backends/evm/AbstractAssembly.h>
+
+#include <liblangutil/Exceptions.h>
+
+#include <libyul/Exceptions.h>
+
+#include <boost/range/adaptor/reversed.hpp>
+
+using namespace std;
+using namespace dev;
+using namespace yul;
+using namespace dev::solidity;
+
+
+EVMDialect::EVMDialect(AsmFlavour _flavour, bool _objectAccess):
+ Dialect(_flavour), m_objectAccess(_objectAccess)
+{
+ // The EVM instructions will be moved to builtins at some point.
+ if (!m_objectAccess)
+ return;
+
+ addFunction("datasize", 1, 1, true, [this](
+ FunctionCall const& _call,
+ AbstractAssembly& _assembly,
+ std::function<void()>
+ ) {
+ yulAssert(m_currentObject, "No object available.");
+ yulAssert(_call.arguments.size() == 1, "");
+ Expression const& arg = _call.arguments.front();
+ YulString dataName = boost::get<Literal>(arg).value;
+ if (m_currentObject->name == dataName)
+ _assembly.appendAssemblySize();
+ else
+ _assembly.appendDataSize(m_subIDs.at(dataName));
+ });
+ addFunction("dataoffset", 1, 1, true, [this](
+ FunctionCall const& _call,
+ AbstractAssembly& _assembly,
+ std::function<void()>
+ ) {
+ yulAssert(m_currentObject, "No object available.");
+ yulAssert(_call.arguments.size() == 1, "");
+ Expression const& arg = _call.arguments.front();
+ YulString dataName = boost::get<Literal>(arg).value;
+ if (m_currentObject->name == dataName)
+ _assembly.appendConstant(0);
+ else
+ _assembly.appendDataOffset(m_subIDs.at(dataName));
+ });
+ addFunction("datacopy", 3, 0, false, [](
+ FunctionCall const&,
+ AbstractAssembly& _assembly,
+ std::function<void()> _visitArguments
+ ) {
+ _visitArguments();
+ _assembly.appendInstruction(solidity::Instruction::CODECOPY);
+ });
+}
+
+BuiltinFunctionForEVM const* EVMDialect::builtin(YulString _name) const
+{
+ auto it = m_functions.find(_name);
+ if (it != m_functions.end())
+ return &it->second;
+ else
+ return nullptr;
+}
+
+shared_ptr<EVMDialect> EVMDialect::looseAssemblyForEVM()
+{
+ return make_shared<EVMDialect>(AsmFlavour::Loose, false);
+}
+
+shared_ptr<EVMDialect> EVMDialect::strictAssemblyForEVM()
+{
+ return make_shared<EVMDialect>(AsmFlavour::Strict, false);
+}
+
+shared_ptr<EVMDialect> EVMDialect::strictAssemblyForEVMObjects()
+{
+ return make_shared<EVMDialect>(AsmFlavour::Strict, true);
+}
+
+shared_ptr<yul::EVMDialect> EVMDialect::yulForEVM()
+{
+ return make_shared<EVMDialect>(AsmFlavour::Yul, false);
+}
+
+void EVMDialect::setSubIDs(map<YulString, AbstractAssembly::SubID> _subIDs)
+{
+ yulAssert(m_objectAccess, "Sub IDs set with dialect that does not support object access.");
+ m_subIDs = std::move(_subIDs);
+}
+
+void EVMDialect::setCurrentObject(Object const* _object)
+{
+ yulAssert(m_objectAccess, "Current object set with dialect that does not support object access.");
+ m_currentObject = _object;
+}
+
+void EVMDialect::addFunction(
+ string _name,
+ size_t _params,
+ size_t _returns,
+ bool _movable,
+ std::function<void(FunctionCall const&, AbstractAssembly&, std::function<void()>)> _generateCode
+)
+{
+ YulString name{std::move(_name)};
+ BuiltinFunctionForEVM& f = m_functions[name];
+ f.name = name;
+ f.parameters.resize(_params);
+ f.returns.resize(_returns);
+ f.movable = _movable;
+ f.generateCode = std::move(_generateCode);
+}
diff --git a/libyul/backends/evm/EVMDialect.h b/libyul/backends/evm/EVMDialect.h
new file mode 100644
index 00000000..feb00b03
--- /dev/null
+++ b/libyul/backends/evm/EVMDialect.h
@@ -0,0 +1,85 @@
+/*
+ This file is part of solidity.
+
+ solidity is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ solidity is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with solidity. If not, see <http://www.gnu.org/licenses/>.
+*/
+/**
+ * Yul dialects for EVM.
+ */
+
+#pragma once
+
+#include <libyul/Dialect.h>
+
+#include <libyul/backends/evm/AbstractAssembly.h>
+
+#include <map>
+
+namespace yul
+{
+
+class YulString;
+using Type = YulString;
+struct FunctionCall;
+struct Object;
+
+struct BuiltinFunctionForEVM: BuiltinFunction
+{
+ /// Function to generate code for the given function call and append it to the abstract
+ /// assembly. The third parameter is called to visit (and generate code for) the arguments
+ /// from right to left.
+ std::function<void(FunctionCall const&, AbstractAssembly&, std::function<void()>)> generateCode;
+};
+
+/**
+ * Yul dialect for EVM as a backend.
+ * The main difference is that the builtin functions take an AbstractAssembly for the
+ * code generation.
+ */
+struct EVMDialect: public Dialect
+{
+ EVMDialect(AsmFlavour _flavour, bool _objectAccess);
+
+ /// @returns the builtin function of the given name or a nullptr if it is not a builtin function.
+ BuiltinFunctionForEVM const* builtin(YulString _name) const override;
+
+ static std::shared_ptr<EVMDialect> looseAssemblyForEVM();
+ static std::shared_ptr<EVMDialect> strictAssemblyForEVM();
+ static std::shared_ptr<EVMDialect> strictAssemblyForEVMObjects();
+ static std::shared_ptr<EVMDialect> yulForEVM();
+
+ bool providesObjectAccess() const { return m_objectAccess; }
+
+ /// Sets the mapping of current sub assembly IDs. Used during code generation.
+ void setSubIDs(std::map<YulString, AbstractAssembly::SubID> _subIDs);
+ /// Sets the current object. Used during code generation.
+ void setCurrentObject(Object const* _object);
+
+private:
+ void addFunction(
+ std::string _name,
+ size_t _params,
+ size_t _returns,
+ bool _movable,
+ std::function<void(FunctionCall const&, AbstractAssembly&, std::function<void()>)> _generateCode
+ );
+
+ bool m_objectAccess;
+ Object const* m_currentObject = nullptr;
+ /// Mapping from named objects to abstract assembly sub IDs.
+ std::map<YulString, AbstractAssembly::SubID> m_subIDs;
+ std::map<YulString, BuiltinFunctionForEVM> m_functions;
+};
+
+}
diff --git a/libyul/backends/evm/EVMObjectCompiler.cpp b/libyul/backends/evm/EVMObjectCompiler.cpp
index e7e8ad99..3f7634b2 100644
--- a/libyul/backends/evm/EVMObjectCompiler.cpp
+++ b/libyul/backends/evm/EVMObjectCompiler.cpp
@@ -21,19 +21,21 @@
#include <libyul/backends/evm/EVMObjectCompiler.h>
#include <libyul/backends/evm/EVMCodeTransform.h>
+#include <libyul/backends/evm/EVMDialect.h>
+
#include <libyul/Object.h>
#include <libyul/Exceptions.h>
using namespace yul;
using namespace std;
-void EVMObjectCompiler::compile(Object& _object, AbstractAssembly& _assembly, bool _yul, bool _evm15)
+void EVMObjectCompiler::compile(Object& _object, AbstractAssembly& _assembly, EVMDialect& _dialect, bool _evm15, bool _optimize)
{
- EVMObjectCompiler compiler(_assembly, _yul, _evm15);
- compiler.run(_object);
+ EVMObjectCompiler compiler(_assembly, _dialect, _evm15);
+ compiler.run(_object, _optimize);
}
-void EVMObjectCompiler::run(Object& _object)
+void EVMObjectCompiler::run(Object& _object, bool _optimize)
{
map<YulString, AbstractAssembly::SubID> subIDs;
@@ -42,7 +44,7 @@ void EVMObjectCompiler::run(Object& _object)
{
auto subAssemblyAndID = m_assembly.createSubAssembly();
subIDs[subObject->name] = subAssemblyAndID.second;
- compile(*subObject, *subAssemblyAndID.first, m_yul, m_evm15);
+ compile(*subObject, *subAssemblyAndID.first, m_dialect, m_evm15, _optimize);
}
else
{
@@ -50,6 +52,13 @@ void EVMObjectCompiler::run(Object& _object)
subIDs[data.name] = m_assembly.appendData(data.data);
}
+ if (m_dialect.providesObjectAccess())
+ {
+ m_dialect.setSubIDs(std::move(subIDs));
+ m_dialect.setCurrentObject(&_object);
+ }
+
yulAssert(_object.analysisInfo, "No analysis info.");
- CodeTransform{m_assembly, *_object.analysisInfo, m_yul, m_evm15}(*_object.code);
+ yulAssert(_object.code, "No code.");
+ CodeTransform{m_assembly, *_object.analysisInfo, *_object.code, m_dialect, _optimize, m_evm15}(*_object.code);
}
diff --git a/libyul/backends/evm/EVMObjectCompiler.h b/libyul/backends/evm/EVMObjectCompiler.h
index c7172e47..057521a8 100644
--- a/libyul/backends/evm/EVMObjectCompiler.h
+++ b/libyul/backends/evm/EVMObjectCompiler.h
@@ -23,20 +23,21 @@ namespace yul
{
struct Object;
class AbstractAssembly;
+struct EVMDialect;
class EVMObjectCompiler
{
public:
- static void compile(Object& _object, AbstractAssembly& _assembly, bool _yul, bool _evm15);
+ static void compile(Object& _object, AbstractAssembly& _assembly, EVMDialect& _dialect, bool _evm15, bool _optimize);
private:
- EVMObjectCompiler(AbstractAssembly& _assembly, bool _yul, bool _evm15):
- m_assembly(_assembly), m_yul(_yul), m_evm15(_evm15)
+ EVMObjectCompiler(AbstractAssembly& _assembly, EVMDialect& _dialect, bool _evm15):
+ m_assembly(_assembly), m_dialect(_dialect), m_evm15(_evm15)
{}
- void run(Object& _object);
+ void run(Object& _object, bool _optimize);
AbstractAssembly& m_assembly;
- bool m_yul = false;
+ EVMDialect& m_dialect;
bool m_evm15 = false;
};