diff options
author | chriseth <chris@ethereum.org> | 2018-12-06 23:47:18 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-06 23:47:18 +0800 |
commit | e1e578d71e22577dc7313214143b81aa24f5ebab (patch) | |
tree | b008fb966c8371a47d6610aa4c40ac8afe87ff19 /libyul | |
parent | 0e8841005ca7bf93545a1bc76bd8fa0ed67cb32d (diff) | |
parent | b7cfa499b0bd674b59284821e33349726cbc4299 (diff) | |
download | dexon-solidity-e1e578d71e22577dc7313214143b81aa24f5ebab.tar.gz dexon-solidity-e1e578d71e22577dc7313214143b81aa24f5ebab.tar.zst dexon-solidity-e1e578d71e22577dc7313214143b81aa24f5ebab.zip |
Merge pull request #5589 from ethereum/yulObjectCodegen
Yul object codegen
Diffstat (limited to 'libyul')
-rw-r--r-- | libyul/CMakeLists.txt | 1 | ||||
-rw-r--r-- | libyul/backends/evm/AbstractAssembly.h | 10 | ||||
-rw-r--r-- | libyul/backends/evm/EVMAssembly.cpp | 21 | ||||
-rw-r--r-- | libyul/backends/evm/EVMAssembly.h | 4 | ||||
-rw-r--r-- | libyul/backends/evm/EVMObjectCompiler.cpp | 55 | ||||
-rw-r--r-- | libyul/backends/evm/EVMObjectCompiler.h | 43 |
6 files changed, 134 insertions, 0 deletions
diff --git a/libyul/CMakeLists.txt b/libyul/CMakeLists.txt index 2dec0a44..4be8155f 100644 --- a/libyul/CMakeLists.txt +++ b/libyul/CMakeLists.txt @@ -9,6 +9,7 @@ add_library(yul ObjectParser.cpp backends/evm/EVMAssembly.cpp backends/evm/EVMCodeTransform.cpp + backends/evm/EVMObjectCompiler.cpp optimiser/ASTCopier.cpp optimiser/ASTWalker.cpp optimiser/BlockFlattener.cpp diff --git a/libyul/backends/evm/AbstractAssembly.h b/libyul/backends/evm/AbstractAssembly.h index 97b1d305..1f224ded 100644 --- a/libyul/backends/evm/AbstractAssembly.h +++ b/libyul/backends/evm/AbstractAssembly.h @@ -26,6 +26,7 @@ #include <libdevcore/CommonData.h> #include <functional> +#include <memory> namespace langutil { @@ -52,6 +53,7 @@ class AbstractAssembly { public: using LabelID = size_t; + using SubID = size_t; virtual ~AbstractAssembly() {} @@ -98,6 +100,14 @@ public: /// Append the assembled size as a constant. virtual void appendAssemblySize() = 0; + /// Creates a new sub-assembly, which can be referenced using dataSize and dataOffset. + virtual std::pair<std::shared_ptr<AbstractAssembly>, SubID> createSubAssembly() = 0; + /// Appends the offset of the given sub-assembly or data. + virtual void appendDataOffset(SubID _sub) = 0; + /// Appends the size of the given sub-assembly or data. + virtual void appendDataSize(SubID _sub) = 0; + /// Appends the given data to the assembly and returns its ID. + virtual SubID appendData(dev::bytes const& _data) = 0; }; enum class IdentifierContext { LValue, RValue }; diff --git a/libyul/backends/evm/EVMAssembly.cpp b/libyul/backends/evm/EVMAssembly.cpp index 99506317..2cf9f001 100644 --- a/libyul/backends/evm/EVMAssembly.cpp +++ b/libyul/backends/evm/EVMAssembly.cpp @@ -194,6 +194,27 @@ void EVMAssembly::appendAssemblySize() m_bytecode += bytes(assemblySizeReferenceSize); } +pair<shared_ptr<AbstractAssembly>, AbstractAssembly::SubID> EVMAssembly::createSubAssembly() +{ + solAssert(false, "Sub assemblies not implemented."); + return {}; +} + +void EVMAssembly::appendDataOffset(AbstractAssembly::SubID) +{ + solAssert(false, "Data not implemented."); +} + +void EVMAssembly::appendDataSize(AbstractAssembly::SubID) +{ + solAssert(false, "Data not implemented."); +} + +AbstractAssembly::SubID EVMAssembly::appendData(bytes const&) +{ + solAssert(false, "Data not implemented."); +} + void EVMAssembly::updateReference(size_t pos, size_t size, u256 value) { solAssert(m_bytecode.size() >= size && pos <= m_bytecode.size() - size, ""); diff --git a/libyul/backends/evm/EVMAssembly.h b/libyul/backends/evm/EVMAssembly.h index d0a437cc..cef9c19a 100644 --- a/libyul/backends/evm/EVMAssembly.h +++ b/libyul/backends/evm/EVMAssembly.h @@ -77,6 +77,10 @@ public: /// Append the assembled size as a constant. void appendAssemblySize() override; + std::pair<std::shared_ptr<AbstractAssembly>, SubID> createSubAssembly() override; + void appendDataOffset(SubID _sub) override; + void appendDataSize(SubID _sub) override; + SubID appendData(dev::bytes const& _data) override; /// Resolves references inside the bytecode and returns the linker object. dev::eth::LinkerObject finalize(); diff --git a/libyul/backends/evm/EVMObjectCompiler.cpp b/libyul/backends/evm/EVMObjectCompiler.cpp new file mode 100644 index 00000000..e7e8ad99 --- /dev/null +++ b/libyul/backends/evm/EVMObjectCompiler.cpp @@ -0,0 +1,55 @@ +/* + 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/>. +*/ +/** + * Compiler that transforms Yul Objects to EVM bytecode objects. + */ + +#include <libyul/backends/evm/EVMObjectCompiler.h> + +#include <libyul/backends/evm/EVMCodeTransform.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) +{ + EVMObjectCompiler compiler(_assembly, _yul, _evm15); + compiler.run(_object); +} + +void EVMObjectCompiler::run(Object& _object) +{ + map<YulString, AbstractAssembly::SubID> subIDs; + + for (auto& subNode: _object.subObjects) + if (Object* subObject = dynamic_cast<Object*>(subNode.get())) + { + auto subAssemblyAndID = m_assembly.createSubAssembly(); + subIDs[subObject->name] = subAssemblyAndID.second; + compile(*subObject, *subAssemblyAndID.first, m_yul, m_evm15); + } + else + { + Data const& data = dynamic_cast<Data const&>(*subNode); + subIDs[data.name] = m_assembly.appendData(data.data); + } + + yulAssert(_object.analysisInfo, "No analysis info."); + CodeTransform{m_assembly, *_object.analysisInfo, m_yul, m_evm15}(*_object.code); +} diff --git a/libyul/backends/evm/EVMObjectCompiler.h b/libyul/backends/evm/EVMObjectCompiler.h new file mode 100644 index 00000000..c7172e47 --- /dev/null +++ b/libyul/backends/evm/EVMObjectCompiler.h @@ -0,0 +1,43 @@ +/* + 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/>. +*/ +/** + * Compiler that transforms Yul Objects to EVM bytecode objects. + */ + + +namespace yul +{ +struct Object; +class AbstractAssembly; + +class EVMObjectCompiler +{ +public: + static void compile(Object& _object, AbstractAssembly& _assembly, bool _yul, bool _evm15); +private: + EVMObjectCompiler(AbstractAssembly& _assembly, bool _yul, bool _evm15): + m_assembly(_assembly), m_yul(_yul), m_evm15(_evm15) + {} + + void run(Object& _object); + + AbstractAssembly& m_assembly; + bool m_yul = false; + bool m_evm15 = false; +}; + +} |