aboutsummaryrefslogtreecommitdiffstats
path: root/libyul/backends
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2018-12-07 07:56:16 +0800
committerchriseth <chris@ethereum.org>2018-12-12 02:24:44 +0800
commitfb3a0ac1c7d2c4624df6ae62d290a2de7768d036 (patch)
tree72a8593bf822032ecf50dbc8e1c78e899a5befd4 /libyul/backends
parent8d49e539951791a25a63f470e8a9935679c7404f (diff)
downloaddexon-solidity-fb3a0ac1c7d2c4624df6ae62d290a2de7768d036.tar.gz
dexon-solidity-fb3a0ac1c7d2c4624df6ae62d290a2de7768d036.tar.zst
dexon-solidity-fb3a0ac1c7d2c4624df6ae62d290a2de7768d036.zip
Codegen for object access.
Diffstat (limited to 'libyul/backends')
-rw-r--r--libyul/backends/evm/EVMCodeTransform.cpp63
-rw-r--r--libyul/backends/evm/EVMCodeTransform.h12
-rw-r--r--libyul/backends/evm/EVMDialect.cpp141
-rw-r--r--libyul/backends/evm/EVMDialect.h85
-rw-r--r--libyul/backends/evm/EVMObjectCompiler.cpp12
-rw-r--r--libyul/backends/evm/EVMObjectCompiler.h8
6 files changed, 283 insertions, 38 deletions
diff --git a/libyul/backends/evm/EVMCodeTransform.cpp b/libyul/backends/evm/EVMCodeTransform.cpp
index 025f937f..bd18985c 100644
--- a/libyul/backends/evm/EVMCodeTransform.cpp
+++ b/libyul/backends/evm/EVMCodeTransform.cpp
@@ -97,7 +97,7 @@ CodeTransform::CodeTransform(
AsmAnalysisInfo& _analysisInfo,
Block const& _block,
bool _allowStackOpt,
- bool _yul,
+ EVMDialect const& _dialect,
bool _evm15,
ExternalIdentifierAccess const& _identifierAccess,
bool _useNamedLabelsForFunctions,
@@ -106,8 +106,8 @@ CodeTransform::CodeTransform(
):
m_assembly(_assembly),
m_info(_analysisInfo),
+ m_dialect(_dialect),
m_allowStackOpt(_allowStackOpt),
- m_yul(_yul),
m_evm15(_evm15),
m_useNamedLabelsForFunctions(_useNamedLabelsForFunctions),
m_identifierAccess(_identifierAccess),
@@ -267,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)
diff --git a/libyul/backends/evm/EVMCodeTransform.h b/libyul/backends/evm/EVMCodeTransform.h
index e305a68c..28ef4e45 100644
--- a/libyul/backends/evm/EVMCodeTransform.h
+++ b/libyul/backends/evm/EVMCodeTransform.h
@@ -20,10 +20,10 @@
#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 <libyul/Dialect.h>
#include <boost/variant.hpp>
#include <boost/optional.hpp>
@@ -87,8 +87,8 @@ public:
AbstractAssembly& _assembly,
AsmAnalysisInfo& _analysisInfo,
Block const& _block,
+ EVMDialect const& _dialect,
bool _allowStackOpt = false,
- Dialect const& _dialect,
bool _evm15 = false,
ExternalIdentifierAccess const& _identifierAccess = ExternalIdentifierAccess(),
bool _useNamedLabelsForFunctions = false
@@ -115,7 +115,7 @@ protected:
AsmAnalysisInfo& _analysisInfo,
Block const& _block,
bool _allowStackOpt,
- Dialect const& _dialect,
+ EVMDialect const& _dialect,
bool _evm15,
ExternalIdentifierAccess const& _identifierAccess,
bool _useNamedLabelsForFunctions,
@@ -179,10 +179,10 @@ private:
AbstractAssembly& m_assembly;
AsmAnalysisInfo& m_info;
Scope* m_scope = nullptr;
+ EVMDialect const& m_dialect;
bool const m_allowStackOpt = true;
- Dialect const& m_dialect;
- bool m_evm15 = false;
- bool m_useNamedLabelsForFunctions = false;
+ 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
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 ec849faa..3f7634b2 100644
--- a/libyul/backends/evm/EVMObjectCompiler.cpp
+++ b/libyul/backends/evm/EVMObjectCompiler.cpp
@@ -21,13 +21,15 @@
#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, Dialect const& _dialect, bool _evm15, bool _optimize)
+void EVMObjectCompiler::compile(Object& _object, AbstractAssembly& _assembly, EVMDialect& _dialect, bool _evm15, bool _optimize)
{
EVMObjectCompiler compiler(_assembly, _dialect, _evm15);
compiler.run(_object, _optimize);
@@ -50,7 +52,13 @@ void EVMObjectCompiler::run(Object& _object, bool _optimize)
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.");
yulAssert(_object.code, "No code.");
- CodeTransform{m_assembly, *_object.analysisInfo, *_object.code, _optimize, m_yul, m_evm15, _optimize}(*_object.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 bb265ff6..057521a8 100644
--- a/libyul/backends/evm/EVMObjectCompiler.h
+++ b/libyul/backends/evm/EVMObjectCompiler.h
@@ -23,21 +23,21 @@ namespace yul
{
struct Object;
class AbstractAssembly;
-struct Dialect;
+struct EVMDialect;
class EVMObjectCompiler
{
public:
- static void compile(Object& _object, AbstractAssembly& _assembly, Dialect const& _dialect, bool _evm15, bool _optimize);
+ static void compile(Object& _object, AbstractAssembly& _assembly, EVMDialect& _dialect, bool _evm15, bool _optimize);
private:
- EVMObjectCompiler(AbstractAssembly& _assembly, Dialect const& _dialect, bool _evm15):
+ EVMObjectCompiler(AbstractAssembly& _assembly, EVMDialect& _dialect, bool _evm15):
m_assembly(_assembly), m_dialect(_dialect), m_evm15(_evm15)
{}
void run(Object& _object, bool _optimize);
AbstractAssembly& m_assembly;
- Dialect const& m_dialect;
+ EVMDialect& m_dialect;
bool m_evm15 = false;
};