From ec0b78595d6c8fc08f65947f1b082cbe4504f1e9 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 17 Apr 2019 19:14:27 +0200 Subject: [PATCH] [Yul] Wasm dialect. --- libyul/CMakeLists.txt | 2 + libyul/backends/wasm/WasmDialect.cpp | 75 ++++++++++++++++++++++++++++ libyul/backends/wasm/WasmDialect.h | 55 ++++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 libyul/backends/wasm/WasmDialect.cpp create mode 100644 libyul/backends/wasm/WasmDialect.h diff --git a/libyul/CMakeLists.txt b/libyul/CMakeLists.txt index ab0f53c97..b70d813b4 100644 --- a/libyul/CMakeLists.txt +++ b/libyul/CMakeLists.txt @@ -38,6 +38,8 @@ add_library(yul backends/evm/EVMObjectCompiler.h backends/evm/NoOutputAssembly.h backends/evm/NoOutputAssembly.cpp + backends/wasm/WasmDialect.cpp + backends/wasm/WasmDialect.h optimiser/ASTCopier.cpp optimiser/ASTCopier.h optimiser/ASTWalker.cpp diff --git a/libyul/backends/wasm/WasmDialect.cpp b/libyul/backends/wasm/WasmDialect.cpp new file mode 100644 index 000000000..5e03b037f --- /dev/null +++ b/libyul/backends/wasm/WasmDialect.cpp @@ -0,0 +1,75 @@ +/* + 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 . +*/ +/** + * Dialects for Wasm. + */ + +#include + +using namespace std; +using namespace yul; + +WasmDialect::WasmDialect(AsmFlavour _flavour): + Dialect{_flavour} +{ + for (auto const& name: { + "i64.add", + "i64.sub", + "i64.mul", + "i64.div_u", + "i64.rem_u", + "i64.and", + "i64.or", + "i64.xor", + "i64.shl", + "i64.shr_u", + "i64.eq", + "i64.ne", + "i64.lt_u", + "i64.gt_u", + "i64.le_u", + "i64.ge_u" + }) + addFunction(name, 2, 1); + + addFunction("i64.eqz", 1, 1); + addFunction("i64.store", 2, 0); + addFunction("i64.load", 1, 1); + + addFunction("drop", 1, 0); + addFunction("unreachable", 0, 0); +} + +BuiltinFunction const* WasmDialect::builtin(YulString _name) const +{ + auto it = m_functions.find(_name); + if (it != m_functions.end()) + return &it->second; + else + return nullptr; +} + +void WasmDialect::addFunction(string _name, size_t _params, size_t _returns) +{ + YulString name{std::move(_name)}; + BuiltinFunction& f = m_functions[name]; + f.name = name; + f.parameters.resize(_params); + f.returns.resize(_returns); + f.movable = false; + f.literalArguments = false; +} diff --git a/libyul/backends/wasm/WasmDialect.h b/libyul/backends/wasm/WasmDialect.h new file mode 100644 index 000000000..71509ca08 --- /dev/null +++ b/libyul/backends/wasm/WasmDialect.h @@ -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 . +*/ +/** + * Dialects for Wasm. + */ + +#pragma once + +#include + +#include + +namespace yul +{ + +class YulString; +using Type = YulString; +struct FunctionCall; +struct Object; + +/** + * Yul dialect for Wasm as a backend. + * + * Builtin functions are a subset of the wasm instructions, always implicitly assuming + * unsigned 64 bit types. + * + * !This is subject to changes! + */ +struct WasmDialect: public Dialect +{ + WasmDialect(AsmFlavour _flavour); + + BuiltinFunction const* builtin(YulString _name) const override; + +protected: + void addFunction(std::string _name, size_t _params, size_t _returns); + + std::map m_functions; +}; + +}