2019-04-17 17:14:27 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2019-04-17 17:14:27 +00:00
|
|
|
/**
|
|
|
|
* Dialects for Wasm.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libyul/backends/wasm/WasmDialect.h>
|
|
|
|
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2020-01-30 18:40:06 +00:00
|
|
|
#include <libyul/Exceptions.h>
|
|
|
|
|
2019-04-17 17:14:27 +00:00
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity::yul;
|
2019-04-17 17:14:27 +00:00
|
|
|
|
2019-12-19 16:58:20 +00:00
|
|
|
WasmDialect::WasmDialect()
|
2019-04-17 17:14:27 +00:00
|
|
|
{
|
2020-01-30 18:40:06 +00:00
|
|
|
YulString i64 = "i64"_yulstring;
|
|
|
|
YulString i32 = "i32"_yulstring;
|
|
|
|
defaultType = i64;
|
|
|
|
boolType = i32;
|
|
|
|
types = {i64, i32};
|
|
|
|
|
|
|
|
for (auto t: types)
|
|
|
|
for (auto const& name: {
|
|
|
|
"add",
|
|
|
|
"sub",
|
|
|
|
"mul",
|
2019-12-11 13:23:43 +00:00
|
|
|
// TODO: div_s
|
2020-01-30 18:40:06 +00:00
|
|
|
"div_u",
|
2019-12-11 13:23:43 +00:00
|
|
|
// TODO: rem_s
|
2020-01-30 18:40:06 +00:00
|
|
|
"rem_u",
|
|
|
|
"and",
|
|
|
|
"or",
|
|
|
|
"xor",
|
|
|
|
"shl",
|
2019-12-11 13:23:43 +00:00
|
|
|
// TODO: shr_s
|
2020-01-30 18:40:06 +00:00
|
|
|
"shr_u",
|
2019-12-11 13:23:43 +00:00
|
|
|
// TODO: rotl
|
|
|
|
// TODO: rotr
|
2020-01-30 18:40:06 +00:00
|
|
|
})
|
|
|
|
addFunction(t.str() + "." + name, {t, t}, {t});
|
|
|
|
|
|
|
|
for (auto t: types)
|
|
|
|
for (auto const& name: {
|
|
|
|
"eq",
|
|
|
|
"ne",
|
2019-12-11 13:23:43 +00:00
|
|
|
// TODO: lt_s
|
2020-01-30 18:40:06 +00:00
|
|
|
"lt_u",
|
2019-12-11 13:23:43 +00:00
|
|
|
// TODO: gt_s
|
2020-01-30 18:40:06 +00:00
|
|
|
"gt_u",
|
2019-12-11 13:23:43 +00:00
|
|
|
// TODO: le_s
|
2020-01-30 18:40:06 +00:00
|
|
|
"le_u",
|
2019-12-11 13:23:43 +00:00
|
|
|
// TODO: ge_s
|
2020-01-30 18:40:06 +00:00
|
|
|
"ge_u"
|
|
|
|
})
|
|
|
|
addFunction(t.str() + "." + name, {t, t}, {i32});
|
|
|
|
|
|
|
|
addFunction("i32.eqz", {i32}, {i32});
|
|
|
|
addFunction("i64.eqz", {i64}, {i32});
|
|
|
|
|
2019-12-11 13:23:43 +00:00
|
|
|
for (auto t: types)
|
|
|
|
for (auto const& name: {
|
|
|
|
"clz",
|
|
|
|
"ctz",
|
|
|
|
"popcnt",
|
|
|
|
})
|
|
|
|
addFunction(t.str() + "." + name, {t}, {t});
|
2020-01-30 18:40:06 +00:00
|
|
|
|
|
|
|
addFunction("i32.wrap_i64", {i64}, {i32});
|
|
|
|
|
|
|
|
addFunction("i64.extend_i32_u", {i32}, {i64});
|
|
|
|
|
|
|
|
addFunction("i32.store", {i32, i32}, {}, false);
|
2020-07-01 10:07:00 +00:00
|
|
|
m_functions["i32.store"_yulstring].sideEffects.storage = SideEffects::None;
|
|
|
|
m_functions["i32.store"_yulstring].sideEffects.otherState = SideEffects::None;
|
2020-01-30 18:40:06 +00:00
|
|
|
addFunction("i64.store", {i32, i64}, {}, false);
|
2019-12-11 13:23:43 +00:00
|
|
|
// TODO: add i32.store16, i64.store8, i64.store16, i64.store32
|
2020-07-01 10:07:00 +00:00
|
|
|
m_functions["i64.store"_yulstring].sideEffects.storage = SideEffects::None;
|
|
|
|
m_functions["i64.store"_yulstring].sideEffects.otherState = SideEffects::None;
|
2019-06-27 11:25:40 +00:00
|
|
|
|
2020-01-30 18:40:06 +00:00
|
|
|
addFunction("i32.store8", {i32, i32}, {}, false);
|
2020-07-01 10:07:00 +00:00
|
|
|
m_functions["i32.store8"_yulstring].sideEffects.storage = SideEffects::None;
|
|
|
|
m_functions["i32.store8"_yulstring].sideEffects.otherState = SideEffects::None;
|
|
|
|
|
2020-01-30 18:40:06 +00:00
|
|
|
addFunction("i64.store8", {i32, i64}, {}, false);
|
2020-07-01 10:07:00 +00:00
|
|
|
m_functions["i64.store8"_yulstring].sideEffects.storage = SideEffects::None;
|
|
|
|
m_functions["i64.store8"_yulstring].sideEffects.otherState = SideEffects::None;
|
2019-12-18 18:48:29 +00:00
|
|
|
|
2020-01-30 18:40:06 +00:00
|
|
|
addFunction("i32.load", {i32}, {i32}, false);
|
2020-07-01 10:07:00 +00:00
|
|
|
m_functions["i32.load"_yulstring].sideEffects.canBeRemoved = true;
|
|
|
|
m_functions["i32.load"_yulstring].sideEffects.canBeRemovedIfNoMSize = true;
|
|
|
|
m_functions["i32.load"_yulstring].sideEffects.storage = SideEffects::None;
|
|
|
|
m_functions["i32.load"_yulstring].sideEffects.memory = SideEffects::Read;
|
|
|
|
m_functions["i32.load"_yulstring].sideEffects.otherState = SideEffects::None;
|
2020-01-30 18:40:06 +00:00
|
|
|
addFunction("i64.load", {i32}, {i64}, false);
|
2019-12-11 13:23:43 +00:00
|
|
|
// TODO: add i32.load8, i32.load16, i64.load8, i64.load16, i64.load32
|
2020-07-01 10:07:00 +00:00
|
|
|
m_functions["i64.load"_yulstring].sideEffects.canBeRemoved = true;
|
|
|
|
m_functions["i64.load"_yulstring].sideEffects.canBeRemovedIfNoMSize = true;
|
|
|
|
m_functions["i64.load"_yulstring].sideEffects.storage = SideEffects::None;
|
|
|
|
m_functions["i64.load"_yulstring].sideEffects.memory = SideEffects::Read;
|
|
|
|
m_functions["i64.load"_yulstring].sideEffects.otherState = SideEffects::None;
|
2019-04-17 17:14:27 +00:00
|
|
|
|
2020-01-30 18:40:06 +00:00
|
|
|
// Drop is actually overloaded for all types, but Yul does not support that.
|
2020-07-06 14:31:25 +00:00
|
|
|
// Because of that, we introduce "i32.drop" and "i64.drop".
|
2020-02-18 23:18:13 +00:00
|
|
|
addFunction("i32.drop", {i32}, {});
|
2020-07-06 14:31:25 +00:00
|
|
|
addFunction("i64.drop", {i64}, {});
|
2019-05-09 15:56:25 +00:00
|
|
|
|
2020-11-27 22:26:06 +00:00
|
|
|
// Select is also overloaded.
|
|
|
|
addFunction("i32.select", {i32, i32, i32}, {i32});
|
|
|
|
addFunction("i64.select", {i64, i64, i32}, {i64});
|
|
|
|
|
2020-02-04 17:54:35 +00:00
|
|
|
addFunction("nop", {}, {});
|
2020-01-30 18:40:06 +00:00
|
|
|
addFunction("unreachable", {}, {}, false);
|
2020-07-01 10:07:00 +00:00
|
|
|
m_functions["unreachable"_yulstring].sideEffects.storage = SideEffects::None;
|
|
|
|
m_functions["unreachable"_yulstring].sideEffects.memory = SideEffects::None;
|
|
|
|
m_functions["unreachable"_yulstring].sideEffects.otherState = SideEffects::None;
|
2020-03-05 09:47:01 +00:00
|
|
|
m_functions["unreachable"_yulstring].controlFlowSideEffects.terminates = true;
|
|
|
|
m_functions["unreachable"_yulstring].controlFlowSideEffects.reverts = true;
|
2019-06-27 11:25:40 +00:00
|
|
|
|
2020-08-04 15:30:16 +00:00
|
|
|
addFunction("datasize", {i64}, {i64}, true, {LiteralKind::String});
|
|
|
|
addFunction("dataoffset", {i64}, {i64}, true, {LiteralKind::String});
|
2019-07-09 13:19:50 +00:00
|
|
|
|
|
|
|
addEthereumExternals();
|
2019-04-17 17:14:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BuiltinFunction const* WasmDialect::builtin(YulString _name) const
|
|
|
|
{
|
|
|
|
auto it = m_functions.find(_name);
|
|
|
|
if (it != m_functions.end())
|
|
|
|
return &it->second;
|
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-18 23:18:13 +00:00
|
|
|
BuiltinFunction const* WasmDialect::discardFunction(YulString _type) const
|
|
|
|
{
|
|
|
|
if (_type == "i32"_yulstring)
|
|
|
|
return builtin("i32.drop"_yulstring);
|
|
|
|
yulAssert(_type == "i64"_yulstring, "");
|
2020-07-06 14:31:25 +00:00
|
|
|
return builtin("i64.drop"_yulstring);
|
2020-02-18 23:18:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BuiltinFunction const* WasmDialect::equalityFunction(YulString _type) const
|
|
|
|
{
|
|
|
|
if (_type == "i32"_yulstring)
|
|
|
|
return builtin("i32.eq"_yulstring);
|
|
|
|
yulAssert(_type == "i64"_yulstring, "");
|
|
|
|
return builtin("i64.eq"_yulstring);
|
|
|
|
}
|
|
|
|
|
2019-04-24 12:03:09 +00:00
|
|
|
WasmDialect const& WasmDialect::instance()
|
|
|
|
{
|
|
|
|
static std::unique_ptr<WasmDialect> dialect;
|
|
|
|
static YulStringRepository::ResetCallback callback{[&] { dialect.reset(); }};
|
|
|
|
if (!dialect)
|
|
|
|
dialect = make_unique<WasmDialect>();
|
|
|
|
return *dialect;
|
|
|
|
}
|
|
|
|
|
2019-07-09 13:19:50 +00:00
|
|
|
void WasmDialect::addEthereumExternals()
|
|
|
|
{
|
|
|
|
// These are not YulStrings because that would be too complicated with regards
|
|
|
|
// to the YulStringRepository reset.
|
|
|
|
static string const i64{"i64"};
|
|
|
|
static string const i32{"i32"};
|
|
|
|
static string const i32ptr{"i32"}; // Uses "i32" on purpose.
|
2020-03-10 11:55:23 +00:00
|
|
|
struct External
|
|
|
|
{
|
2020-03-05 09:47:01 +00:00
|
|
|
string name;
|
|
|
|
vector<string> parameters;
|
|
|
|
vector<string> returns;
|
2020-03-10 11:55:23 +00:00
|
|
|
ControlFlowSideEffects controlFlowSideEffects = ControlFlowSideEffects{};
|
2020-03-05 09:47:01 +00:00
|
|
|
};
|
2019-07-09 13:19:50 +00:00
|
|
|
static vector<External> externals{
|
|
|
|
{"getAddress", {i32ptr}, {}},
|
|
|
|
{"getExternalBalance", {i32ptr, i32ptr}, {}},
|
|
|
|
{"getBlockHash", {i64, i32ptr}, {i32}},
|
|
|
|
{"call", {i64, i32ptr, i32ptr, i32ptr, i32}, {i32}},
|
|
|
|
{"callDataCopy", {i32ptr, i32, i32}, {}},
|
|
|
|
{"getCallDataSize", {}, {i32}},
|
|
|
|
{"callCode", {i64, i32ptr, i32ptr, i32ptr, i32}, {i32}},
|
|
|
|
{"callDelegate", {i64, i32ptr, i32ptr, i32}, {i32}},
|
|
|
|
{"callStatic", {i64, i32ptr, i32ptr, i32}, {i32}},
|
|
|
|
{"storageStore", {i32ptr, i32ptr}, {}},
|
|
|
|
{"storageLoad", {i32ptr, i32ptr}, {}},
|
|
|
|
{"getCaller", {i32ptr}, {}},
|
|
|
|
{"getCallValue", {i32ptr}, {}},
|
|
|
|
{"codeCopy", {i32ptr, i32, i32}, {}},
|
2019-12-18 14:56:29 +00:00
|
|
|
{"getCodeSize", {}, {i32}},
|
2019-07-09 13:19:50 +00:00
|
|
|
{"getBlockCoinbase", {i32ptr}, {}},
|
|
|
|
{"create", {i32ptr, i32ptr, i32, i32ptr}, {i32}},
|
|
|
|
{"getBlockDifficulty", {i32ptr}, {}},
|
|
|
|
{"externalCodeCopy", {i32ptr, i32ptr, i32, i32}, {}},
|
|
|
|
{"getExternalCodeSize", {i32ptr}, {i32}},
|
|
|
|
{"getGasLeft", {}, {i64}},
|
|
|
|
{"getBlockGasLimit", {}, {i64}},
|
|
|
|
{"getTxGasPrice", {i32ptr}, {}},
|
|
|
|
{"log", {i32ptr, i32, i32, i32ptr, i32ptr, i32ptr, i32ptr}, {}},
|
|
|
|
{"getBlockNumber", {}, {i64}},
|
|
|
|
{"getTxOrigin", {i32ptr}, {}},
|
2020-03-10 11:55:23 +00:00
|
|
|
{"finish", {i32ptr, i32}, {}, ControlFlowSideEffects{true, false}},
|
|
|
|
{"revert", {i32ptr, i32}, {}, ControlFlowSideEffects{true, true}},
|
2019-07-09 13:19:50 +00:00
|
|
|
{"getReturnDataSize", {}, {i32}},
|
|
|
|
{"returnDataCopy", {i32ptr, i32, i32}, {}},
|
2020-03-10 11:55:23 +00:00
|
|
|
{"selfDestruct", {i32ptr}, {}, ControlFlowSideEffects{true, false}},
|
2019-07-09 13:19:50 +00:00
|
|
|
{"getBlockTimestamp", {}, {i64}}
|
|
|
|
};
|
|
|
|
for (External const& ext: externals)
|
|
|
|
{
|
|
|
|
YulString name{"eth." + ext.name};
|
|
|
|
BuiltinFunction& f = m_functions[name];
|
|
|
|
f.name = name;
|
|
|
|
for (string const& p: ext.parameters)
|
|
|
|
f.parameters.emplace_back(YulString(p));
|
|
|
|
for (string const& p: ext.returns)
|
|
|
|
f.returns.emplace_back(YulString(p));
|
|
|
|
// TODO some of them are side effect free.
|
2019-08-13 12:40:26 +00:00
|
|
|
f.sideEffects = SideEffects::worst();
|
2020-07-01 10:07:00 +00:00
|
|
|
f.sideEffects.cannotLoop = true;
|
|
|
|
f.sideEffects.movableApartFromEffects = !ext.controlFlowSideEffects.terminates;
|
2020-03-05 09:47:01 +00:00
|
|
|
f.controlFlowSideEffects = ext.controlFlowSideEffects;
|
2019-07-09 13:19:50 +00:00
|
|
|
f.isMSize = false;
|
2020-08-04 15:30:16 +00:00
|
|
|
f.literalArguments.clear();
|
2020-07-01 10:07:00 +00:00
|
|
|
|
|
|
|
static set<string> const writesToStorage{
|
|
|
|
"storageStore",
|
|
|
|
"call",
|
|
|
|
"callcode",
|
|
|
|
"callDelegate",
|
|
|
|
"create"
|
|
|
|
};
|
|
|
|
static set<string> const readsStorage{"storageLoad", "callStatic"};
|
|
|
|
if (readsStorage.count(ext.name))
|
|
|
|
f.sideEffects.storage = SideEffects::Read;
|
|
|
|
else if (!writesToStorage.count(ext.name))
|
|
|
|
f.sideEffects.storage = SideEffects::None;
|
2019-07-09 13:19:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 11:25:40 +00:00
|
|
|
void WasmDialect::addFunction(
|
|
|
|
string _name,
|
2020-01-30 18:40:06 +00:00
|
|
|
vector<YulString> _params,
|
|
|
|
vector<YulString> _returns,
|
2019-06-27 11:25:40 +00:00
|
|
|
bool _movable,
|
2020-08-04 15:30:16 +00:00
|
|
|
vector<optional<LiteralKind>> _literalArguments
|
2019-06-27 11:25:40 +00:00
|
|
|
)
|
2019-04-17 17:14:27 +00:00
|
|
|
{
|
2019-04-18 12:39:48 +00:00
|
|
|
YulString name{move(_name)};
|
2019-04-17 17:14:27 +00:00
|
|
|
BuiltinFunction& f = m_functions[name];
|
|
|
|
f.name = name;
|
2020-01-30 18:40:06 +00:00
|
|
|
f.parameters = std::move(_params);
|
|
|
|
yulAssert(_returns.size() <= 1, "The Wasm 1.0 specification only allows up to 1 return value.");
|
|
|
|
f.returns = std::move(_returns);
|
2019-08-13 12:40:26 +00:00
|
|
|
f.sideEffects = _movable ? SideEffects{} : SideEffects::worst();
|
2020-07-01 10:07:00 +00:00
|
|
|
f.sideEffects.cannotLoop = true;
|
|
|
|
// TODO This should be improved when LoopInvariantCodeMotion gets specialized for WASM
|
|
|
|
f.sideEffects.movableApartFromEffects = _movable;
|
2019-05-27 11:11:00 +00:00
|
|
|
f.isMSize = false;
|
2020-08-04 15:30:16 +00:00
|
|
|
f.literalArguments = std::move(_literalArguments);
|
2019-04-17 17:14:27 +00:00
|
|
|
}
|