2019-03-05 16:20:28 +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-03-05 16:20:28 +00:00
|
|
|
/**
|
|
|
|
* Component that can generate various useful Yul functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libsolidity/codegen/YulUtilFunctions.h>
|
|
|
|
|
|
|
|
#include <libsolidity/codegen/MultiUseYulFunctionCollector.h>
|
|
|
|
#include <libsolidity/ast/AST.h>
|
|
|
|
#include <libsolidity/codegen/CompilerUtils.h>
|
2019-11-21 17:56:25 +00:00
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
2020-10-12 14:36:48 +00:00
|
|
|
#include <libsolutil/FunctionSelector.h>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/Whiskers.h>
|
|
|
|
#include <libsolutil/StringUtils.h>
|
2021-12-16 11:35:39 +00:00
|
|
|
#include <libsolidity/ast/TypeProvider.h>
|
2019-03-05 16:20:28 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::frontend;
|
2019-03-05 16:20:28 +00:00
|
|
|
|
2021-08-05 11:21:08 +00:00
|
|
|
string YulUtilFunctions::identityFunction()
|
|
|
|
{
|
|
|
|
string functionName = "identity";
|
|
|
|
return m_functionCollector.createFunction("identity", [&](vector<string>& _args, vector<string>& _rets) {
|
|
|
|
_args.push_back("value");
|
|
|
|
_rets.push_back("ret");
|
|
|
|
return "ret := value";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-05 16:20:28 +00:00
|
|
|
string YulUtilFunctions::combineExternalFunctionIdFunction()
|
|
|
|
{
|
|
|
|
string functionName = "combine_external_function_id";
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-03-05 16:20:28 +00:00
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(addr, selector) -> combined {
|
|
|
|
combined := <shl64>(or(<shl32>(addr), and(selector, 0xffffffff)))
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("shl32", shiftLeftFunction(32))
|
|
|
|
("shl64", shiftLeftFunction(64))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
string YulUtilFunctions::splitExternalFunctionIdFunction()
|
|
|
|
{
|
|
|
|
string functionName = "split_external_function_id";
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-03-05 16:20:28 +00:00
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(combined) -> addr, selector {
|
|
|
|
combined := <shr64>(combined)
|
|
|
|
selector := and(combined, 0xffffffff)
|
|
|
|
addr := <shr32>(combined)
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("shr32", shiftRightFunction(32))
|
|
|
|
("shr64", shiftRightFunction(64))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-27 16:46:36 +00:00
|
|
|
string YulUtilFunctions::copyToMemoryFunction(bool _fromCalldata, bool _cleanup)
|
2019-03-05 16:20:28 +00:00
|
|
|
{
|
2022-07-27 16:46:36 +00:00
|
|
|
string functionName =
|
|
|
|
"copy_"s +
|
|
|
|
(_fromCalldata ? "calldata"s : "memory"s) +
|
|
|
|
"_to_memory"s +
|
|
|
|
(_cleanup ? "_with_cleanup"s : ""s);
|
|
|
|
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-03-05 16:20:28 +00:00
|
|
|
if (_fromCalldata)
|
|
|
|
{
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(src, dst, length) {
|
|
|
|
calldatacopy(dst, src, length)
|
2022-07-27 16:46:36 +00:00
|
|
|
<?cleanup>mstore(add(dst, length), 0)</cleanup>
|
2019-03-05 16:20:28 +00:00
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2022-07-27 16:46:36 +00:00
|
|
|
("cleanup", _cleanup)
|
2019-03-05 16:20:28 +00:00
|
|
|
.render();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(src, dst, length) {
|
|
|
|
let i := 0
|
|
|
|
for { } lt(i, length) { i := add(i, 32) }
|
|
|
|
{
|
|
|
|
mstore(add(dst, i), mload(add(src, i)))
|
|
|
|
}
|
2022-07-27 16:46:36 +00:00
|
|
|
<?cleanup>mstore(add(dst, length), 0)</cleanup>
|
2019-03-05 16:20:28 +00:00
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2022-07-27 16:46:36 +00:00
|
|
|
("cleanup", _cleanup)
|
2019-03-05 16:20:28 +00:00
|
|
|
.render();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-22 14:48:31 +00:00
|
|
|
string YulUtilFunctions::copyLiteralToMemoryFunction(string const& _literal)
|
2020-12-11 15:33:10 +00:00
|
|
|
{
|
2020-12-22 14:48:31 +00:00
|
|
|
string functionName = "copy_literal_to_memory_" + util::toHex(util::keccak256(_literal).asBytes());
|
|
|
|
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>() -> memPtr {
|
|
|
|
memPtr := <arrayAllocationFunction>(<size>)
|
|
|
|
<storeLiteralInMem>(add(memPtr, 32))
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("arrayAllocationFunction", allocateMemoryArrayFunction(*TypeProvider::array(DataLocation::Memory, true)))
|
|
|
|
("size", to_string(_literal.size()))
|
|
|
|
("storeLiteralInMem", storeLiteralInMemoryFunction(_literal))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
string YulUtilFunctions::storeLiteralInMemoryFunction(string const& _literal)
|
|
|
|
{
|
|
|
|
string functionName = "store_literal_in_memory_" + util::toHex(util::keccak256(_literal).asBytes());
|
2020-12-11 15:33:10 +00:00
|
|
|
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
size_t words = (_literal.length() + 31) / 32;
|
|
|
|
vector<map<string, string>> wordParams(words);
|
|
|
|
for (size_t i = 0; i < words; ++i)
|
|
|
|
{
|
|
|
|
wordParams[i]["offset"] = to_string(i * 32);
|
|
|
|
wordParams[i]["wordValue"] = formatAsStringOrNumber(_literal.substr(32 * i, 32));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Whiskers(R"(
|
2020-12-22 14:48:31 +00:00
|
|
|
function <functionName>(memPtr) {
|
2020-12-11 15:33:10 +00:00
|
|
|
<#word>
|
2020-12-22 14:48:31 +00:00
|
|
|
mstore(add(memPtr, <offset>), <wordValue>)
|
2020-12-11 15:33:10 +00:00
|
|
|
</word>
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("word", wordParams)
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-05 06:57:19 +00:00
|
|
|
string YulUtilFunctions::copyLiteralToStorageFunction(string const& _literal)
|
|
|
|
{
|
|
|
|
string functionName = "copy_literal_to_storage_" + util::toHex(util::keccak256(_literal).asBytes());
|
|
|
|
|
|
|
|
return m_functionCollector.createFunction(functionName, [&](vector<string>& _args, vector<string>&) {
|
|
|
|
_args = {"slot"};
|
|
|
|
|
|
|
|
if (_literal.size() >= 32)
|
|
|
|
{
|
|
|
|
size_t words = (_literal.length() + 31) / 32;
|
|
|
|
vector<map<string, string>> wordParams(words);
|
|
|
|
for (size_t i = 0; i < words; ++i)
|
|
|
|
{
|
|
|
|
wordParams[i]["offset"] = to_string(i);
|
|
|
|
wordParams[i]["wordValue"] = formatAsStringOrNumber(_literal.substr(32 * i, 32));
|
|
|
|
}
|
|
|
|
return Whiskers(R"(
|
|
|
|
let oldLen := <byteArrayLength>(sload(slot))
|
|
|
|
<cleanUpArrayEnd>(slot, oldLen, <length>)
|
|
|
|
sstore(slot, <encodedLen>)
|
|
|
|
let dstPtr := <dataArea>(slot)
|
|
|
|
<#word>
|
|
|
|
sstore(add(dstPtr, <offset>), <wordValue>)
|
|
|
|
</word>
|
|
|
|
)")
|
|
|
|
("byteArrayLength", extractByteArrayLengthFunction())
|
|
|
|
("cleanUpArrayEnd", cleanUpDynamicByteArrayEndSlotsFunction(*TypeProvider::bytesStorage()))
|
|
|
|
("dataArea", arrayDataAreaFunction(*TypeProvider::bytesStorage()))
|
|
|
|
("word", wordParams)
|
|
|
|
("length", to_string(_literal.size()))
|
|
|
|
("encodedLen", to_string(2 * _literal.size() + 1))
|
|
|
|
.render();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return Whiskers(R"(
|
|
|
|
let oldLen := <byteArrayLength>(sload(slot))
|
|
|
|
<cleanUpArrayEnd>(slot, oldLen, <length>)
|
|
|
|
sstore(slot, add(<wordValue>, <encodedLen>))
|
|
|
|
)")
|
|
|
|
("byteArrayLength", extractByteArrayLengthFunction())
|
|
|
|
("cleanUpArrayEnd", cleanUpDynamicByteArrayEndSlotsFunction(*TypeProvider::bytesStorage()))
|
|
|
|
("wordValue", formatAsStringOrNumber(_literal))
|
|
|
|
("length", to_string(_literal.size()))
|
|
|
|
("encodedLen", to_string(2 * _literal.size()))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-30 17:08:02 +00:00
|
|
|
string YulUtilFunctions::requireOrAssertFunction(bool _assert, Type const* _messageType)
|
|
|
|
{
|
|
|
|
string functionName =
|
|
|
|
string(_assert ? "assert_helper" : "require_helper") +
|
|
|
|
(_messageType ? ("_" + _messageType->identifier()) : "");
|
|
|
|
|
|
|
|
solAssert(!_assert || !_messageType, "Asserts can't have messages!");
|
|
|
|
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-04-30 17:08:02 +00:00
|
|
|
if (!_messageType)
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(condition) {
|
2020-09-15 16:57:59 +00:00
|
|
|
if iszero(condition) { <error> }
|
2019-04-30 17:08:02 +00:00
|
|
|
}
|
|
|
|
)")
|
2020-10-12 14:01:45 +00:00
|
|
|
("error", _assert ? panicFunction(PanicCode::Assert) + "()" : "revert(0, 0)")
|
2019-04-30 17:08:02 +00:00
|
|
|
("functionName", functionName)
|
|
|
|
.render();
|
|
|
|
|
|
|
|
int const hashHeaderSize = 4;
|
2022-09-27 12:13:44 +00:00
|
|
|
u256 const errorHash = util::selectorFromSignatureU256("Error(string)");
|
2019-04-30 17:08:02 +00:00
|
|
|
|
2020-01-22 14:48:56 +00:00
|
|
|
string const encodeFunc = ABIFunctions(m_evmVersion, m_revertStrings, m_functionCollector)
|
2019-04-30 17:08:02 +00:00
|
|
|
.tupleEncoder(
|
|
|
|
{_messageType},
|
|
|
|
{TypeProvider::stringMemory()}
|
|
|
|
);
|
|
|
|
|
|
|
|
return Whiskers(R"(
|
2019-05-09 10:22:37 +00:00
|
|
|
function <functionName>(condition <messageVars>) {
|
2019-04-30 17:08:02 +00:00
|
|
|
if iszero(condition) {
|
2021-01-14 16:13:51 +00:00
|
|
|
let memPtr := <allocateUnbounded>()
|
|
|
|
mstore(memPtr, <errorHash>)
|
|
|
|
let end := <abiEncodeFunc>(add(memPtr, <hashHeaderSize>) <messageVars>)
|
|
|
|
revert(memPtr, sub(end, memPtr))
|
2019-04-30 17:08:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2021-01-14 16:13:51 +00:00
|
|
|
("allocateUnbounded", allocateUnboundedFunction())
|
2019-05-09 10:22:37 +00:00
|
|
|
("errorHash", formatNumber(errorHash))
|
2019-04-30 17:08:02 +00:00
|
|
|
("abiEncodeFunc", encodeFunc)
|
|
|
|
("hashHeaderSize", to_string(hashHeaderSize))
|
2019-05-09 10:22:37 +00:00
|
|
|
("messageVars",
|
|
|
|
(_messageType->sizeOnStack() > 0 ? ", " : "") +
|
|
|
|
suffixedVariableNameList("message_", 1, 1 + _messageType->sizeOnStack())
|
|
|
|
)
|
2019-04-30 17:08:02 +00:00
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-05 16:20:28 +00:00
|
|
|
string YulUtilFunctions::leftAlignFunction(Type const& _type)
|
|
|
|
{
|
|
|
|
string functionName = string("leftAlign_") + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-03-05 16:20:28 +00:00
|
|
|
Whiskers templ(R"(
|
|
|
|
function <functionName>(value) -> aligned {
|
|
|
|
<body>
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
templ("functionName", functionName);
|
|
|
|
switch (_type.category())
|
|
|
|
{
|
|
|
|
case Type::Category::Address:
|
|
|
|
templ("body", "aligned := " + leftAlignFunction(IntegerType(160)) + "(value)");
|
|
|
|
break;
|
|
|
|
case Type::Category::Integer:
|
|
|
|
{
|
|
|
|
IntegerType const& type = dynamic_cast<IntegerType const&>(_type);
|
|
|
|
if (type.numBits() == 256)
|
|
|
|
templ("body", "aligned := value");
|
|
|
|
else
|
|
|
|
templ("body", "aligned := " + shiftLeftFunction(256 - type.numBits()) + "(value)");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::Category::RationalNumber:
|
|
|
|
solAssert(false, "Left align requested for rational number.");
|
|
|
|
break;
|
|
|
|
case Type::Category::Bool:
|
|
|
|
templ("body", "aligned := " + leftAlignFunction(IntegerType(8)) + "(value)");
|
|
|
|
break;
|
|
|
|
case Type::Category::FixedPoint:
|
|
|
|
solUnimplemented("Fixed point types not implemented.");
|
|
|
|
break;
|
|
|
|
case Type::Category::Array:
|
|
|
|
case Type::Category::Struct:
|
|
|
|
solAssert(false, "Left align requested for non-value type.");
|
|
|
|
break;
|
|
|
|
case Type::Category::FixedBytes:
|
|
|
|
templ("body", "aligned := value");
|
|
|
|
break;
|
|
|
|
case Type::Category::Contract:
|
2019-04-17 11:40:50 +00:00
|
|
|
templ("body", "aligned := " + leftAlignFunction(*TypeProvider::address()) + "(value)");
|
2019-03-05 16:20:28 +00:00
|
|
|
break;
|
|
|
|
case Type::Category::Enum:
|
|
|
|
{
|
2020-11-10 13:26:29 +00:00
|
|
|
solAssert(dynamic_cast<EnumType const&>(_type).storageBytes() == 1, "");
|
|
|
|
templ("body", "aligned := " + leftAlignFunction(IntegerType(8)) + "(value)");
|
2019-03-05 16:20:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::Category::InaccessibleDynamic:
|
|
|
|
solAssert(false, "Left align requested for inaccessible dynamic type.");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
solAssert(false, "Left align of type " + _type.identifier() + " requested.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return templ.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
string YulUtilFunctions::shiftLeftFunction(size_t _numBits)
|
|
|
|
{
|
|
|
|
solAssert(_numBits < 256, "");
|
|
|
|
|
|
|
|
string functionName = "shift_left_" + to_string(_numBits);
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-06-06 15:59:30 +00:00
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(value) -> newValue {
|
|
|
|
newValue :=
|
|
|
|
<?hasShifts>
|
|
|
|
shl(<numBits>, value)
|
|
|
|
<!hasShifts>
|
|
|
|
mul(value, <multiplier>)
|
|
|
|
</hasShifts>
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("numBits", to_string(_numBits))
|
|
|
|
("hasShifts", m_evmVersion.hasBitwiseShifting())
|
|
|
|
("multiplier", toCompactHexWithPrefix(u256(1) << _numBits))
|
|
|
|
.render();
|
|
|
|
});
|
2019-03-05 16:20:28 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 13:26:40 +00:00
|
|
|
string YulUtilFunctions::shiftLeftFunctionDynamic()
|
2019-06-05 17:32:30 +00:00
|
|
|
{
|
2019-06-17 13:26:40 +00:00
|
|
|
string functionName = "shift_left_dynamic";
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-06-05 17:32:30 +00:00
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(bits, value) -> newValue {
|
|
|
|
newValue :=
|
|
|
|
<?hasShifts>
|
|
|
|
shl(bits, value)
|
|
|
|
<!hasShifts>
|
|
|
|
mul(value, exp(2, bits))
|
|
|
|
</hasShifts>
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("hasShifts", m_evmVersion.hasBitwiseShifting())
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-05 16:20:28 +00:00
|
|
|
string YulUtilFunctions::shiftRightFunction(size_t _numBits)
|
|
|
|
{
|
|
|
|
solAssert(_numBits < 256, "");
|
|
|
|
|
|
|
|
// Note that if this is extended with signed shifts,
|
|
|
|
// the opcodes SAR and SDIV behave differently with regards to rounding!
|
|
|
|
|
2019-06-06 15:59:30 +00:00
|
|
|
string functionName = "shift_right_" + to_string(_numBits) + "_unsigned";
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-05-07 13:12:21 +00:00
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(value) -> newValue {
|
|
|
|
newValue :=
|
|
|
|
<?hasShifts>
|
|
|
|
shr(<numBits>, value)
|
|
|
|
<!hasShifts>
|
|
|
|
div(value, <multiplier>)
|
|
|
|
</hasShifts>
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("hasShifts", m_evmVersion.hasBitwiseShifting())
|
|
|
|
("numBits", to_string(_numBits))
|
|
|
|
("multiplier", toCompactHexWithPrefix(u256(1) << _numBits))
|
|
|
|
.render();
|
|
|
|
});
|
2019-03-05 16:20:28 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 13:26:40 +00:00
|
|
|
string YulUtilFunctions::shiftRightFunctionDynamic()
|
2019-06-05 17:32:30 +00:00
|
|
|
{
|
2019-06-17 13:26:40 +00:00
|
|
|
string const functionName = "shift_right_unsigned_dynamic";
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-06-05 17:32:30 +00:00
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(bits, value) -> newValue {
|
|
|
|
newValue :=
|
|
|
|
<?hasShifts>
|
|
|
|
shr(bits, value)
|
|
|
|
<!hasShifts>
|
|
|
|
div(value, exp(2, bits))
|
|
|
|
</hasShifts>
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("hasShifts", m_evmVersion.hasBitwiseShifting())
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-20 21:16:42 +00:00
|
|
|
string YulUtilFunctions::shiftRightSignedFunctionDynamic()
|
|
|
|
{
|
|
|
|
string const functionName = "shift_right_signed_dynamic";
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(bits, value) -> result {
|
|
|
|
<?hasShifts>
|
|
|
|
result := sar(bits, value)
|
|
|
|
<!hasShifts>
|
|
|
|
let divisor := exp(2, bits)
|
|
|
|
let xor_mask := sub(0, slt(value, 0))
|
|
|
|
result := xor(div(xor(value, xor_mask), divisor), xor_mask)
|
|
|
|
// combined version of
|
|
|
|
// switch slt(value, 0)
|
|
|
|
// case 0 { result := div(value, divisor) }
|
|
|
|
// default { result := not(div(not(value), divisor)) }
|
|
|
|
</hasShifts>
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("hasShifts", m_evmVersion.hasBitwiseShifting())
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string YulUtilFunctions::typedShiftLeftFunction(Type const& _type, Type const& _amountType)
|
|
|
|
{
|
2021-02-08 12:13:19 +00:00
|
|
|
solUnimplementedAssert(_type.category() != Type::Category::FixedPoint, "Not yet implemented - FixedPointType.");
|
2020-04-20 21:16:42 +00:00
|
|
|
solAssert(_type.category() == Type::Category::FixedBytes || _type.category() == Type::Category::Integer, "");
|
|
|
|
solAssert(_amountType.category() == Type::Category::Integer, "");
|
2020-07-23 14:45:16 +00:00
|
|
|
solAssert(!dynamic_cast<IntegerType const&>(_amountType).isSigned(), "");
|
2020-04-20 21:16:42 +00:00
|
|
|
string const functionName = "shift_left_" + _type.identifier() + "_" + _amountType.identifier();
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(value, bits) -> result {
|
|
|
|
bits := <cleanAmount>(bits)
|
2021-08-04 11:48:39 +00:00
|
|
|
result := <cleanup>(<shift>(bits, <cleanup>(value)))
|
2020-04-20 21:16:42 +00:00
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("cleanAmount", cleanupFunction(_amountType))
|
|
|
|
("shift", shiftLeftFunctionDynamic())
|
|
|
|
("cleanup", cleanupFunction(_type))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
string YulUtilFunctions::typedShiftRightFunction(Type const& _type, Type const& _amountType)
|
|
|
|
{
|
2021-02-09 15:17:56 +00:00
|
|
|
solUnimplementedAssert(_type.category() != Type::Category::FixedPoint, "Not yet implemented - FixedPointType.");
|
2020-04-20 21:16:42 +00:00
|
|
|
solAssert(_type.category() == Type::Category::FixedBytes || _type.category() == Type::Category::Integer, "");
|
|
|
|
solAssert(_amountType.category() == Type::Category::Integer, "");
|
2020-07-23 14:45:16 +00:00
|
|
|
solAssert(!dynamic_cast<IntegerType const&>(_amountType).isSigned(), "");
|
2020-04-20 21:16:42 +00:00
|
|
|
IntegerType const* integerType = dynamic_cast<IntegerType const*>(&_type);
|
|
|
|
bool valueSigned = integerType && integerType->isSigned();
|
|
|
|
|
|
|
|
string const functionName = "shift_right_" + _type.identifier() + "_" + _amountType.identifier();
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(value, bits) -> result {
|
|
|
|
bits := <cleanAmount>(bits)
|
|
|
|
result := <cleanup>(<shift>(bits, <cleanup>(value)))
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("cleanAmount", cleanupFunction(_amountType))
|
|
|
|
("shift", valueSigned ? shiftRightSignedFunctionDynamic() : shiftRightFunctionDynamic())
|
|
|
|
("cleanup", cleanupFunction(_type))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-30 16:32:56 +00:00
|
|
|
string YulUtilFunctions::updateByteSliceFunction(size_t _numBytes, size_t _shiftBytes)
|
|
|
|
{
|
|
|
|
solAssert(_numBytes <= 32, "");
|
|
|
|
solAssert(_shiftBytes <= 32, "");
|
|
|
|
size_t numBits = _numBytes * 8;
|
|
|
|
size_t shiftBits = _shiftBytes * 8;
|
|
|
|
string functionName = "update_byte_slice_" + to_string(_numBytes) + "_shift_" + to_string(_shiftBytes);
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-04-30 16:32:56 +00:00
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(value, toInsert) -> result {
|
|
|
|
let mask := <mask>
|
|
|
|
toInsert := <shl>(toInsert)
|
|
|
|
value := and(value, not(mask))
|
|
|
|
result := or(value, and(toInsert, mask))
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("mask", formatNumber(((bigint(1) << numBits) - 1) << shiftBits))
|
|
|
|
("shl", shiftLeftFunction(shiftBits))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-17 13:26:40 +00:00
|
|
|
string YulUtilFunctions::updateByteSliceFunctionDynamic(size_t _numBytes)
|
2019-06-05 17:32:30 +00:00
|
|
|
{
|
|
|
|
solAssert(_numBytes <= 32, "");
|
|
|
|
size_t numBits = _numBytes * 8;
|
2019-06-17 13:26:40 +00:00
|
|
|
string functionName = "update_byte_slice_dynamic" + to_string(_numBytes);
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-06-05 17:32:30 +00:00
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(value, shiftBytes, toInsert) -> result {
|
|
|
|
let shiftBits := mul(shiftBytes, 8)
|
|
|
|
let mask := <shl>(shiftBits, <mask>)
|
|
|
|
toInsert := <shl>(shiftBits, toInsert)
|
|
|
|
value := and(value, not(mask))
|
|
|
|
result := or(value, and(toInsert, mask))
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("mask", formatNumber((bigint(1) << numBits) - 1))
|
2019-06-17 13:26:40 +00:00
|
|
|
("shl", shiftLeftFunctionDynamic())
|
2019-06-05 17:32:30 +00:00
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-20 15:29:11 +00:00
|
|
|
string YulUtilFunctions::maskBytesFunctionDynamic()
|
|
|
|
{
|
|
|
|
string functionName = "mask_bytes_dynamic";
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(data, bytes) -> result {
|
|
|
|
let mask := not(<shr>(mul(8, bytes), not(0)))
|
|
|
|
result := and(data, mask)
|
|
|
|
})")
|
|
|
|
("functionName", functionName)
|
|
|
|
("shr", shiftRightFunctionDynamic())
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-12 13:46:10 +00:00
|
|
|
string YulUtilFunctions::maskLowerOrderBytesFunction(size_t _bytes)
|
|
|
|
{
|
|
|
|
string functionName = "mask_lower_order_bytes_" + to_string(_bytes);
|
|
|
|
solAssert(_bytes <= 32, "");
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(data) -> result {
|
|
|
|
result := and(data, <mask>)
|
|
|
|
})")
|
|
|
|
("functionName", functionName)
|
|
|
|
("mask", formatNumber((~u256(0)) >> (256 - 8 * _bytes)))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
string YulUtilFunctions::maskLowerOrderBytesFunctionDynamic()
|
|
|
|
{
|
|
|
|
string functionName = "mask_lower_order_bytes_dynamic";
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(data, bytes) -> result {
|
|
|
|
let mask := not(<shl>(mul(8, bytes), not(0)))
|
|
|
|
result := and(data, mask)
|
|
|
|
})")
|
|
|
|
("functionName", functionName)
|
|
|
|
("shl", shiftLeftFunctionDynamic())
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-05 16:20:28 +00:00
|
|
|
string YulUtilFunctions::roundUpFunction()
|
|
|
|
{
|
|
|
|
string functionName = "round_up_to_mul_of_32";
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-03-05 16:20:28 +00:00
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(value) -> result {
|
|
|
|
result := and(add(value, 31), not(31))
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-05 08:12:12 +00:00
|
|
|
string YulUtilFunctions::divide32CeilFunction()
|
|
|
|
{
|
|
|
|
return m_functionCollector.createFunction(
|
|
|
|
"divide_by_32_ceil",
|
|
|
|
[&](vector<string>& _args, vector<string>& _ret) {
|
|
|
|
_args = {"value"};
|
|
|
|
_ret = {"result"};
|
|
|
|
return "result := div(add(value, 31), 32)";
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-12 09:24:19 +00:00
|
|
|
string YulUtilFunctions::overflowCheckedIntAddFunction(IntegerType const& _type)
|
2019-03-18 10:21:41 +00:00
|
|
|
{
|
2019-06-12 09:24:19 +00:00
|
|
|
string functionName = "checked_add_" + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-05-07 13:12:21 +00:00
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(x, y) -> sum {
|
2020-05-05 11:06:01 +00:00
|
|
|
x := <cleanupFunction>(x)
|
|
|
|
y := <cleanupFunction>(y)
|
2022-06-03 20:04:16 +00:00
|
|
|
sum := add(x, y)
|
2019-06-12 09:24:19 +00:00
|
|
|
<?signed>
|
2022-06-03 20:04:16 +00:00
|
|
|
<?256bit>
|
|
|
|
// overflow, if x >= 0 and sum < y
|
|
|
|
// underflow, if x < 0 and sum >= y
|
|
|
|
if or(
|
|
|
|
and(iszero(slt(x, 0)), slt(sum, y)),
|
|
|
|
and(slt(x, 0), iszero(slt(sum, y)))
|
|
|
|
) { <panic>() }
|
|
|
|
<!256bit>
|
|
|
|
if or(
|
|
|
|
sgt(sum, <maxValue>),
|
|
|
|
slt(sum, <minValue>)
|
|
|
|
) { <panic>() }
|
|
|
|
</256bit>
|
2019-06-12 09:24:19 +00:00
|
|
|
<!signed>
|
2022-06-03 20:04:16 +00:00
|
|
|
<?256bit>
|
|
|
|
if gt(x, sum) { <panic>() }
|
|
|
|
<!256bit>
|
|
|
|
if gt(sum, <maxValue>) { <panic>() }
|
|
|
|
</256bit>
|
2019-06-12 09:24:19 +00:00
|
|
|
</signed>
|
2019-05-07 13:12:21 +00:00
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2019-06-12 09:24:19 +00:00
|
|
|
("signed", _type.isSigned())
|
|
|
|
("maxValue", toCompactHexWithPrefix(u256(_type.maxValue())))
|
|
|
|
("minValue", toCompactHexWithPrefix(u256(_type.minValue())))
|
2020-05-05 11:06:01 +00:00
|
|
|
("cleanupFunction", cleanupFunction(_type))
|
2020-10-12 14:01:45 +00:00
|
|
|
("panic", panicFunction(PanicCode::UnderOverflow))
|
2022-06-03 20:04:16 +00:00
|
|
|
("256bit", _type.numBits() == 256)
|
2019-05-07 13:12:21 +00:00
|
|
|
.render();
|
2019-03-18 10:21:41 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-22 08:28:04 +00:00
|
|
|
string YulUtilFunctions::wrappingIntAddFunction(IntegerType const& _type)
|
|
|
|
{
|
|
|
|
string functionName = "wrapping_add_" + _type.identifier();
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(x, y) -> sum {
|
|
|
|
sum := <cleanupFunction>(add(x, y))
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("cleanupFunction", cleanupFunction(_type))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-12 12:06:13 +00:00
|
|
|
string YulUtilFunctions::overflowCheckedIntMulFunction(IntegerType const& _type)
|
2019-05-20 14:42:27 +00:00
|
|
|
{
|
2019-06-12 12:06:13 +00:00
|
|
|
string functionName = "checked_mul_" + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-05-20 14:42:27 +00:00
|
|
|
return
|
2019-06-12 12:06:13 +00:00
|
|
|
// Multiplication by zero could be treated separately and directly return zero.
|
2019-05-20 14:42:27 +00:00
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(x, y) -> product {
|
2020-05-05 11:06:01 +00:00
|
|
|
x := <cleanupFunction>(x)
|
|
|
|
y := <cleanupFunction>(y)
|
2022-06-20 22:19:20 +00:00
|
|
|
let product_raw := mul(x, y)
|
|
|
|
product := <cleanupFunction>(product_raw)
|
2019-06-12 12:06:13 +00:00
|
|
|
<?signed>
|
2022-06-20 22:19:20 +00:00
|
|
|
<?gt128bit>
|
|
|
|
<?256bit>
|
|
|
|
// special case
|
|
|
|
if and(slt(x, 0), eq(y, <minValue>)) { <panic>() }
|
|
|
|
</256bit>
|
|
|
|
// overflow, if x != 0 and y != product/x
|
|
|
|
if iszero(
|
|
|
|
or(
|
|
|
|
iszero(x),
|
|
|
|
eq(y, sdiv(product, x))
|
|
|
|
)
|
|
|
|
) { <panic>() }
|
|
|
|
<!gt128bit>
|
|
|
|
if iszero(eq(product, product_raw)) { <panic>() }
|
|
|
|
</gt128bit>
|
2019-06-12 12:06:13 +00:00
|
|
|
<!signed>
|
2022-06-20 22:19:20 +00:00
|
|
|
<?gt128bit>
|
|
|
|
// overflow, if x != 0 and y != product/x
|
|
|
|
if iszero(
|
|
|
|
or(
|
|
|
|
iszero(x),
|
|
|
|
eq(y, div(product, x))
|
|
|
|
)
|
|
|
|
) { <panic>() }
|
|
|
|
<!gt128bit>
|
|
|
|
if iszero(eq(product, product_raw)) { <panic>() }
|
|
|
|
</gt128bit>
|
2019-06-12 12:06:13 +00:00
|
|
|
</signed>
|
2019-05-20 14:42:27 +00:00
|
|
|
}
|
|
|
|
)")
|
2019-06-12 12:06:13 +00:00
|
|
|
("functionName", functionName)
|
|
|
|
("signed", _type.isSigned())
|
2020-05-05 11:06:01 +00:00
|
|
|
("cleanupFunction", cleanupFunction(_type))
|
2020-10-12 14:01:45 +00:00
|
|
|
("panic", panicFunction(PanicCode::UnderOverflow))
|
2022-06-20 22:19:20 +00:00
|
|
|
("minValue", toCompactHexWithPrefix(u256(_type.minValue())))
|
|
|
|
("256bit", _type.numBits() == 256)
|
|
|
|
("gt128bit", _type.numBits() > 128)
|
2019-06-12 12:06:13 +00:00
|
|
|
.render();
|
2019-05-20 14:42:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-22 08:28:04 +00:00
|
|
|
string YulUtilFunctions::wrappingIntMulFunction(IntegerType const& _type)
|
|
|
|
{
|
|
|
|
string functionName = "wrapping_mul_" + _type.identifier();
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(x, y) -> product {
|
|
|
|
product := <cleanupFunction>(mul(x, y))
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("cleanupFunction", cleanupFunction(_type))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-23 14:12:32 +00:00
|
|
|
string YulUtilFunctions::overflowCheckedIntDivFunction(IntegerType const& _type)
|
|
|
|
{
|
|
|
|
string functionName = "checked_div_" + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-05-23 14:12:32 +00:00
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(x, y) -> r {
|
2020-05-05 11:06:01 +00:00
|
|
|
x := <cleanupFunction>(x)
|
|
|
|
y := <cleanupFunction>(y)
|
2020-10-12 14:01:45 +00:00
|
|
|
if iszero(y) { <panicDivZero>() }
|
2019-05-23 14:12:32 +00:00
|
|
|
<?signed>
|
2019-06-12 11:19:38 +00:00
|
|
|
// overflow for minVal / -1
|
2019-05-23 14:12:32 +00:00
|
|
|
if and(
|
|
|
|
eq(x, <minVal>),
|
|
|
|
eq(y, sub(0, 1))
|
2020-10-12 14:01:45 +00:00
|
|
|
) { <panicOverflow>() }
|
2019-05-23 14:12:32 +00:00
|
|
|
</signed>
|
|
|
|
r := <?signed>s</signed>div(x, y)
|
|
|
|
}
|
|
|
|
)")
|
2019-06-12 11:19:38 +00:00
|
|
|
("functionName", functionName)
|
|
|
|
("signed", _type.isSigned())
|
|
|
|
("minVal", toCompactHexWithPrefix(u256(_type.minValue())))
|
2020-05-05 11:06:01 +00:00
|
|
|
("cleanupFunction", cleanupFunction(_type))
|
2020-10-12 14:01:45 +00:00
|
|
|
("panicDivZero", panicFunction(PanicCode::DivisionByZero))
|
|
|
|
("panicOverflow", panicFunction(PanicCode::UnderOverflow))
|
2019-06-12 11:19:38 +00:00
|
|
|
.render();
|
2019-05-23 14:12:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-22 08:28:04 +00:00
|
|
|
string YulUtilFunctions::wrappingIntDivFunction(IntegerType const& _type)
|
|
|
|
{
|
|
|
|
string functionName = "wrapping_div_" + _type.identifier();
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(x, y) -> r {
|
|
|
|
x := <cleanupFunction>(x)
|
|
|
|
y := <cleanupFunction>(y)
|
|
|
|
if iszero(y) { <error>() }
|
|
|
|
r := <?signed>s</signed>div(x, y)
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("cleanupFunction", cleanupFunction(_type))
|
|
|
|
("signed", _type.isSigned())
|
2020-10-12 14:01:45 +00:00
|
|
|
("error", panicFunction(PanicCode::DivisionByZero))
|
2020-07-22 08:28:04 +00:00
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
string YulUtilFunctions::intModFunction(IntegerType const& _type)
|
2019-06-12 14:27:26 +00:00
|
|
|
{
|
2020-07-22 08:28:04 +00:00
|
|
|
string functionName = "mod_" + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-06-12 14:27:26 +00:00
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(x, y) -> r {
|
2020-05-05 11:06:01 +00:00
|
|
|
x := <cleanupFunction>(x)
|
|
|
|
y := <cleanupFunction>(y)
|
2020-09-15 16:57:59 +00:00
|
|
|
if iszero(y) { <panic>() }
|
2019-06-12 14:27:26 +00:00
|
|
|
r := <?signed>s</signed>mod(x, y)
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("signed", _type.isSigned())
|
2020-05-05 11:06:01 +00:00
|
|
|
("cleanupFunction", cleanupFunction(_type))
|
2020-10-12 14:01:45 +00:00
|
|
|
("panic", panicFunction(PanicCode::DivisionByZero))
|
2019-06-12 14:27:26 +00:00
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-12 11:07:06 +00:00
|
|
|
string YulUtilFunctions::overflowCheckedIntSubFunction(IntegerType const& _type)
|
2019-05-08 19:25:47 +00:00
|
|
|
{
|
2019-06-12 11:07:06 +00:00
|
|
|
string functionName = "checked_sub_" + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&] {
|
2019-05-08 19:25:47 +00:00
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(x, y) -> diff {
|
2020-05-05 11:06:01 +00:00
|
|
|
x := <cleanupFunction>(x)
|
|
|
|
y := <cleanupFunction>(y)
|
2022-06-03 20:04:16 +00:00
|
|
|
diff := sub(x, y)
|
2019-06-12 11:07:06 +00:00
|
|
|
<?signed>
|
2022-06-03 20:04:16 +00:00
|
|
|
<?256bit>
|
|
|
|
// underflow, if y >= 0 and diff > x
|
|
|
|
// overflow, if y < 0 and diff < x
|
|
|
|
if or(
|
|
|
|
and(iszero(slt(y, 0)), sgt(diff, x)),
|
|
|
|
and(slt(y, 0), slt(diff, x))
|
|
|
|
) { <panic>() }
|
|
|
|
<!256bit>
|
|
|
|
if or(
|
|
|
|
slt(diff, <minValue>),
|
|
|
|
sgt(diff, <maxValue>)
|
|
|
|
) { <panic>() }
|
|
|
|
</256bit>
|
2019-06-12 11:07:06 +00:00
|
|
|
<!signed>
|
2022-06-03 20:04:16 +00:00
|
|
|
<?256bit>
|
|
|
|
if gt(diff, x) { <panic>() }
|
|
|
|
<!256bit>
|
|
|
|
if gt(diff, <maxValue>) { <panic>() }
|
|
|
|
</256bit>
|
2019-06-12 11:07:06 +00:00
|
|
|
</signed>
|
2019-05-08 19:25:47 +00:00
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2019-06-12 11:07:06 +00:00
|
|
|
("signed", _type.isSigned())
|
|
|
|
("maxValue", toCompactHexWithPrefix(u256(_type.maxValue())))
|
|
|
|
("minValue", toCompactHexWithPrefix(u256(_type.minValue())))
|
2020-05-05 11:06:01 +00:00
|
|
|
("cleanupFunction", cleanupFunction(_type))
|
2020-10-12 14:01:45 +00:00
|
|
|
("panic", panicFunction(PanicCode::UnderOverflow))
|
2022-06-03 20:04:16 +00:00
|
|
|
("256bit", _type.numBits() == 256)
|
2019-05-08 19:25:47 +00:00
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-22 08:28:04 +00:00
|
|
|
string YulUtilFunctions::wrappingIntSubFunction(IntegerType const& _type)
|
|
|
|
{
|
|
|
|
string functionName = "wrapping_sub_" + _type.identifier();
|
|
|
|
return m_functionCollector.createFunction(functionName, [&] {
|
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(x, y) -> diff {
|
|
|
|
diff := <cleanupFunction>(sub(x, y))
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("cleanupFunction", cleanupFunction(_type))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-22 14:52:28 +00:00
|
|
|
string YulUtilFunctions::overflowCheckedIntExpFunction(
|
|
|
|
IntegerType const& _type,
|
|
|
|
IntegerType const& _exponentType
|
|
|
|
)
|
|
|
|
{
|
|
|
|
solAssert(!_exponentType.isSigned(), "");
|
|
|
|
|
|
|
|
string functionName = "checked_exp_" + _type.identifier() + "_" + _exponentType.identifier();
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(base, exponent) -> power {
|
|
|
|
base := <baseCleanupFunction>(base)
|
|
|
|
exponent := <exponentCleanupFunction>(exponent)
|
|
|
|
<?signed>
|
|
|
|
power := <exp>(base, exponent, <minValue>, <maxValue>)
|
|
|
|
<!signed>
|
|
|
|
power := <exp>(base, exponent, <maxValue>)
|
|
|
|
</signed>
|
|
|
|
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("signed", _type.isSigned())
|
|
|
|
("exp", _type.isSigned() ? overflowCheckedSignedExpFunction() : overflowCheckedUnsignedExpFunction())
|
|
|
|
("maxValue", toCompactHexWithPrefix(_type.max()))
|
|
|
|
("minValue", toCompactHexWithPrefix(_type.min()))
|
|
|
|
("baseCleanupFunction", cleanupFunction(_type))
|
|
|
|
("exponentCleanupFunction", cleanupFunction(_exponentType))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-15 20:42:48 +00:00
|
|
|
string YulUtilFunctions::overflowCheckedIntLiteralExpFunction(
|
|
|
|
RationalNumberType const& _baseType,
|
|
|
|
IntegerType const& _exponentType,
|
|
|
|
IntegerType const& _commonType
|
|
|
|
)
|
|
|
|
{
|
|
|
|
solAssert(!_exponentType.isSigned(), "");
|
|
|
|
solAssert(_baseType.isNegative() == _commonType.isSigned(), "");
|
|
|
|
solAssert(_commonType.numBits() == 256, "");
|
|
|
|
|
|
|
|
string functionName = "checked_exp_" + _baseType.richIdentifier() + "_" + _exponentType.identifier();
|
|
|
|
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]()
|
|
|
|
{
|
|
|
|
// Converts a bigint number into u256 (negative numbers represented in two's complement form.)
|
|
|
|
// We assume that `_v` fits in 256 bits.
|
|
|
|
auto bigint2u = [&](bigint const& _v) -> u256
|
|
|
|
{
|
|
|
|
if (_v < 0)
|
|
|
|
return s2u(s256(_v));
|
|
|
|
return u256(_v);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Calculates the upperbound for exponentiation, that is, calculate `b`, such that
|
|
|
|
// _base**b <= _maxValue and _base**(b + 1) > _maxValue
|
|
|
|
auto findExponentUpperbound = [](bigint const _base, bigint const _maxValue) -> unsigned
|
|
|
|
{
|
|
|
|
// There is no overflow for these cases
|
|
|
|
if (_base == 0 || _base == -1 || _base == 1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
unsigned first = 0;
|
|
|
|
unsigned last = 255;
|
|
|
|
unsigned middle;
|
|
|
|
|
|
|
|
while (first < last)
|
|
|
|
{
|
|
|
|
middle = (first + last) / 2;
|
|
|
|
|
|
|
|
if (
|
|
|
|
// The condition on msb is a shortcut that avoids computing large powers in
|
|
|
|
// arbitrary precision.
|
|
|
|
boost::multiprecision::msb(_base) * middle <= boost::multiprecision::msb(_maxValue) &&
|
|
|
|
boost::multiprecision::pow(_base, middle) <= _maxValue
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (boost::multiprecision::pow(_base, middle + 1) > _maxValue)
|
|
|
|
return middle;
|
|
|
|
else
|
|
|
|
first = middle + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
last = middle;
|
|
|
|
}
|
|
|
|
|
|
|
|
return last;
|
|
|
|
};
|
|
|
|
|
|
|
|
bigint baseValue = _baseType.isNegative() ?
|
|
|
|
u2s(_baseType.literalValue(nullptr)) :
|
|
|
|
_baseType.literalValue(nullptr);
|
|
|
|
bool needsOverflowCheck = !((baseValue == 0) || (baseValue == -1) || (baseValue == 1));
|
|
|
|
unsigned exponentUpperbound;
|
|
|
|
|
|
|
|
if (_baseType.isNegative())
|
|
|
|
{
|
|
|
|
// Only checks for underflow. The only case where this can be a problem is when, for a
|
|
|
|
// negative base, say `b`, and an even exponent, say `e`, `b**e = 2**255` (which is an
|
|
|
|
// overflow.) But this never happens because, `255 = 3*5*17`, and therefore there is no even
|
|
|
|
// number `e` such that `b**e = 2**255`.
|
|
|
|
exponentUpperbound = findExponentUpperbound(abs(baseValue), abs(_commonType.minValue()));
|
|
|
|
|
|
|
|
bigint power = boost::multiprecision::pow(baseValue, exponentUpperbound);
|
|
|
|
bigint overflowedPower = boost::multiprecision::pow(baseValue, exponentUpperbound + 1);
|
|
|
|
|
|
|
|
if (needsOverflowCheck)
|
|
|
|
solAssert(
|
|
|
|
(power <= _commonType.maxValue()) && (power >= _commonType.minValue()) &&
|
|
|
|
!((overflowedPower <= _commonType.maxValue()) && (overflowedPower >= _commonType.minValue())),
|
|
|
|
"Incorrect exponent upper bound calculated."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
exponentUpperbound = findExponentUpperbound(baseValue, _commonType.maxValue());
|
|
|
|
|
|
|
|
if (needsOverflowCheck)
|
|
|
|
solAssert(
|
|
|
|
boost::multiprecision::pow(baseValue, exponentUpperbound) <= _commonType.maxValue() &&
|
|
|
|
boost::multiprecision::pow(baseValue, exponentUpperbound + 1) > _commonType.maxValue(),
|
|
|
|
"Incorrect exponent upper bound calculated."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(exponent) -> power {
|
|
|
|
exponent := <exponentCleanupFunction>(exponent)
|
|
|
|
<?needsOverflowCheck>
|
|
|
|
if gt(exponent, <exponentUpperbound>) { <panic>() }
|
|
|
|
</needsOverflowCheck>
|
|
|
|
power := exp(<base>, exponent)
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("exponentCleanupFunction", cleanupFunction(_exponentType))
|
|
|
|
("needsOverflowCheck", needsOverflowCheck)
|
|
|
|
("exponentUpperbound", to_string(exponentUpperbound))
|
2020-10-12 14:01:45 +00:00
|
|
|
("panic", panicFunction(PanicCode::UnderOverflow))
|
2020-09-15 20:42:48 +00:00
|
|
|
("base", bigint2u(baseValue).str())
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-22 14:52:28 +00:00
|
|
|
string YulUtilFunctions::overflowCheckedUnsignedExpFunction()
|
|
|
|
{
|
2020-09-15 12:25:07 +00:00
|
|
|
// Checks for the "small number specialization" below.
|
|
|
|
using namespace boost::multiprecision;
|
|
|
|
solAssert(pow(bigint(10), 77) < pow(bigint(2), 256), "");
|
|
|
|
solAssert(pow(bigint(11), 77) >= pow(bigint(2), 256), "");
|
|
|
|
solAssert(pow(bigint(10), 78) >= pow(bigint(2), 256), "");
|
|
|
|
|
|
|
|
solAssert(pow(bigint(306), 31) < pow(bigint(2), 256), "");
|
|
|
|
solAssert(pow(bigint(307), 31) >= pow(bigint(2), 256), "");
|
|
|
|
solAssert(pow(bigint(306), 32) >= pow(bigint(2), 256), "");
|
|
|
|
|
2020-07-22 14:52:28 +00:00
|
|
|
string functionName = "checked_exp_unsigned";
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(base, exponent, max) -> power {
|
|
|
|
// This function currently cannot be inlined because of the
|
|
|
|
// "leave" statements. We have to improve the optimizer.
|
|
|
|
|
|
|
|
// Note that 0**0 == 1
|
|
|
|
if iszero(exponent) { power := 1 leave }
|
|
|
|
if iszero(base) { power := 0 leave }
|
|
|
|
|
2020-09-15 12:25:07 +00:00
|
|
|
// Specializations for small bases
|
|
|
|
switch base
|
|
|
|
// 0 is handled above
|
|
|
|
case 1 { power := 1 leave }
|
|
|
|
case 2
|
|
|
|
{
|
2020-09-15 16:57:59 +00:00
|
|
|
if gt(exponent, 255) { <panic>() }
|
2020-09-15 12:25:07 +00:00
|
|
|
power := exp(2, exponent)
|
2020-09-15 16:57:59 +00:00
|
|
|
if gt(power, max) { <panic>() }
|
2020-09-15 12:25:07 +00:00
|
|
|
leave
|
|
|
|
}
|
|
|
|
if or(
|
|
|
|
and(lt(base, 11), lt(exponent, 78)),
|
|
|
|
and(lt(base, 307), lt(exponent, 32))
|
|
|
|
)
|
|
|
|
{
|
|
|
|
power := exp(base, exponent)
|
2020-09-15 16:57:59 +00:00
|
|
|
if gt(power, max) { <panic>() }
|
2020-09-15 12:25:07 +00:00
|
|
|
leave
|
|
|
|
}
|
|
|
|
|
2020-09-15 12:33:36 +00:00
|
|
|
power, base := <expLoop>(1, base, exponent, max)
|
2020-07-22 14:52:28 +00:00
|
|
|
|
2020-09-15 16:57:59 +00:00
|
|
|
if gt(power, div(max, base)) { <panic>() }
|
2020-07-22 14:52:28 +00:00
|
|
|
power := mul(power, base)
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2020-10-12 14:01:45 +00:00
|
|
|
("panic", panicFunction(PanicCode::UnderOverflow))
|
2020-09-15 12:33:36 +00:00
|
|
|
("expLoop", overflowCheckedExpLoopFunction())
|
2020-07-22 14:52:28 +00:00
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
string YulUtilFunctions::overflowCheckedSignedExpFunction()
|
|
|
|
{
|
|
|
|
string functionName = "checked_exp_signed";
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(base, exponent, min, max) -> power {
|
|
|
|
// Currently, `leave` avoids this function being inlined.
|
|
|
|
// We have to improve the optimizer.
|
|
|
|
|
|
|
|
// Note that 0**0 == 1
|
|
|
|
switch exponent
|
|
|
|
case 0 { power := 1 leave }
|
|
|
|
case 1 { power := base leave }
|
|
|
|
if iszero(base) { power := 0 leave }
|
|
|
|
|
|
|
|
power := 1
|
|
|
|
|
|
|
|
// We pull out the first iteration because it is the only one in which
|
|
|
|
// base can be negative.
|
|
|
|
// Exponent is at least 2 here.
|
|
|
|
|
|
|
|
// overflow check for base * base
|
|
|
|
switch sgt(base, 0)
|
2020-09-15 16:57:59 +00:00
|
|
|
case 1 { if gt(base, div(max, base)) { <panic>() } }
|
|
|
|
case 0 { if slt(base, sdiv(max, base)) { <panic>() } }
|
2020-07-22 14:52:28 +00:00
|
|
|
if and(exponent, 1)
|
|
|
|
{
|
|
|
|
power := base
|
|
|
|
}
|
|
|
|
base := mul(base, base)
|
|
|
|
exponent := <shr_1>(exponent)
|
|
|
|
|
|
|
|
// Below this point, base is always positive.
|
|
|
|
|
2020-09-15 12:33:36 +00:00
|
|
|
power, base := <expLoop>(power, base, exponent, max)
|
|
|
|
|
2020-09-15 16:57:59 +00:00
|
|
|
if and(sgt(power, 0), gt(power, div(max, base))) { <panic>() }
|
|
|
|
if and(slt(power, 0), slt(power, sdiv(min, base))) { <panic>() }
|
2020-09-15 12:33:36 +00:00
|
|
|
power := mul(power, base)
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2020-10-12 14:01:45 +00:00
|
|
|
("panic", panicFunction(PanicCode::UnderOverflow))
|
2020-09-15 12:33:36 +00:00
|
|
|
("expLoop", overflowCheckedExpLoopFunction())
|
|
|
|
("shr_1", shiftRightFunction(1))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
string YulUtilFunctions::overflowCheckedExpLoopFunction()
|
|
|
|
{
|
|
|
|
// We use this loop for both signed and unsigned exponentiation
|
|
|
|
// because we pull out the first iteration in the signed case which
|
|
|
|
// results in the base always being positive.
|
|
|
|
|
|
|
|
// This function does not include the final multiplication.
|
|
|
|
|
|
|
|
string functionName = "checked_exp_helper";
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(_power, _base, exponent, max) -> power, base {
|
|
|
|
power := _power
|
|
|
|
base := _base
|
2020-07-22 14:52:28 +00:00
|
|
|
for { } gt(exponent, 1) {}
|
|
|
|
{
|
|
|
|
// overflow check for base * base
|
2020-09-15 16:57:59 +00:00
|
|
|
if gt(base, div(max, base)) { <panic>() }
|
2020-07-22 14:52:28 +00:00
|
|
|
if and(exponent, 1)
|
|
|
|
{
|
|
|
|
// No checks for power := mul(power, base) needed, because the check
|
|
|
|
// for base * base above is sufficient, since:
|
|
|
|
// |power| <= base (proof by induction) and thus:
|
2020-09-15 12:33:36 +00:00
|
|
|
// |power * base| <= base * base <= max <= |min| (for signed)
|
|
|
|
// (this is equally true for signed and unsigned exp)
|
2020-07-22 14:52:28 +00:00
|
|
|
power := mul(power, base)
|
|
|
|
}
|
|
|
|
base := mul(base, base)
|
|
|
|
exponent := <shr_1>(exponent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2020-10-12 14:01:45 +00:00
|
|
|
("panic", panicFunction(PanicCode::UnderOverflow))
|
2020-07-22 14:52:28 +00:00
|
|
|
("shr_1", shiftRightFunction(1))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-22 08:28:04 +00:00
|
|
|
string YulUtilFunctions::wrappingIntExpFunction(
|
|
|
|
IntegerType const& _type,
|
|
|
|
IntegerType const& _exponentType
|
|
|
|
)
|
|
|
|
{
|
|
|
|
solAssert(!_exponentType.isSigned(), "");
|
|
|
|
|
|
|
|
string functionName = "wrapping_exp_" + _type.identifier() + "_" + _exponentType.identifier();
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return
|
|
|
|
Whiskers(R"(
|
|
|
|
function <functionName>(base, exponent) -> power {
|
|
|
|
base := <baseCleanupFunction>(base)
|
|
|
|
exponent := <exponentCleanupFunction>(exponent)
|
|
|
|
power := <baseCleanupFunction>(exp(base, exponent))
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("baseCleanupFunction", cleanupFunction(_type))
|
|
|
|
("exponentCleanupFunction", cleanupFunction(_exponentType))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-05 16:20:28 +00:00
|
|
|
string YulUtilFunctions::arrayLengthFunction(ArrayType const& _type)
|
|
|
|
{
|
|
|
|
string functionName = "array_length_" + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-03-05 16:20:28 +00:00
|
|
|
Whiskers w(R"(
|
2020-10-08 18:31:13 +00:00
|
|
|
function <functionName>(value<?dynamic><?calldata>, len</calldata></dynamic>) -> length {
|
2019-05-07 13:12:21 +00:00
|
|
|
<?dynamic>
|
|
|
|
<?memory>
|
|
|
|
length := mload(value)
|
|
|
|
</memory>
|
|
|
|
<?storage>
|
|
|
|
length := sload(value)
|
|
|
|
<?byteArray>
|
2020-05-05 17:53:17 +00:00
|
|
|
length := <extractByteArrayLength>(length)
|
2019-05-07 13:12:21 +00:00
|
|
|
</byteArray>
|
|
|
|
</storage>
|
2020-10-08 18:31:13 +00:00
|
|
|
<?calldata>
|
|
|
|
length := len
|
|
|
|
</calldata>
|
2019-05-07 13:12:21 +00:00
|
|
|
<!dynamic>
|
|
|
|
length := <length>
|
|
|
|
</dynamic>
|
2019-03-05 16:20:28 +00:00
|
|
|
}
|
|
|
|
)");
|
|
|
|
w("functionName", functionName);
|
2019-05-07 13:12:21 +00:00
|
|
|
w("dynamic", _type.isDynamicallySized());
|
2019-03-05 16:20:28 +00:00
|
|
|
if (!_type.isDynamicallySized())
|
2019-05-07 13:12:21 +00:00
|
|
|
w("length", toCompactHexWithPrefix(_type.length()));
|
|
|
|
w("memory", _type.location() == DataLocation::Memory);
|
|
|
|
w("storage", _type.location() == DataLocation::Storage);
|
2020-10-08 18:31:13 +00:00
|
|
|
w("calldata", _type.location() == DataLocation::CallData);
|
2020-05-05 17:53:17 +00:00
|
|
|
if (_type.location() == DataLocation::Storage)
|
|
|
|
{
|
2022-01-28 17:57:05 +00:00
|
|
|
w("byteArray", _type.isByteArrayOrString());
|
|
|
|
if (_type.isByteArrayOrString())
|
2020-05-05 17:53:17 +00:00
|
|
|
w("extractByteArrayLength", extractByteArrayLengthFunction());
|
|
|
|
}
|
2020-10-08 18:31:13 +00:00
|
|
|
|
2019-03-05 16:20:28 +00:00
|
|
|
return w.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-09 14:43:57 +00:00
|
|
|
string YulUtilFunctions::extractByteArrayLengthFunction()
|
|
|
|
{
|
|
|
|
string functionName = "extract_byte_array_length";
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
Whiskers w(R"(
|
|
|
|
function <functionName>(data) -> length {
|
|
|
|
length := div(data, 2)
|
|
|
|
let outOfPlaceEncoding := and(data, 1)
|
|
|
|
if iszero(outOfPlaceEncoding) {
|
|
|
|
length := and(length, 0x7f)
|
|
|
|
}
|
|
|
|
|
|
|
|
if eq(outOfPlaceEncoding, lt(length, 32)) {
|
|
|
|
<panic>()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
w("functionName", functionName);
|
|
|
|
w("panic", panicFunction(PanicCode::StorageEncodingError));
|
|
|
|
return w.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-26 16:15:41 +00:00
|
|
|
std::string YulUtilFunctions::resizeArrayFunction(ArrayType const& _type)
|
2019-06-06 12:07:40 +00:00
|
|
|
{
|
|
|
|
solAssert(_type.location() == DataLocation::Storage, "");
|
2021-09-23 15:18:13 +00:00
|
|
|
solUnimplementedAssert(_type.baseType()->storageBytes() <= 32);
|
2019-06-06 12:07:40 +00:00
|
|
|
|
2022-01-28 17:57:05 +00:00
|
|
|
if (_type.isByteArrayOrString())
|
2020-10-06 14:00:32 +00:00
|
|
|
return resizeDynamicByteArrayFunction(_type);
|
|
|
|
|
2019-06-06 12:07:40 +00:00
|
|
|
string functionName = "resize_array_" + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2020-12-21 12:17:49 +00:00
|
|
|
Whiskers templ(R"(
|
2019-06-06 12:07:40 +00:00
|
|
|
function <functionName>(array, newLen) {
|
|
|
|
if gt(newLen, <maxArrayLength>) {
|
2020-09-15 16:57:59 +00:00
|
|
|
<panic>()
|
2019-06-06 12:07:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let oldLen := <fetchLength>(array)
|
|
|
|
|
2020-11-26 16:15:41 +00:00
|
|
|
<?isDynamic>
|
|
|
|
// Store new length
|
|
|
|
sstore(array, newLen)
|
|
|
|
</isDynamic>
|
2019-06-06 12:07:40 +00:00
|
|
|
|
2020-12-21 12:17:49 +00:00
|
|
|
<?needsClearing>
|
2021-04-22 07:47:12 +00:00
|
|
|
<cleanUpArrayEnd>(array, oldLen, newLen)
|
2020-12-21 12:17:49 +00:00
|
|
|
</needsClearing>
|
|
|
|
})");
|
|
|
|
templ("functionName", functionName);
|
|
|
|
templ("maxArrayLength", (u256(1) << 64).str());
|
|
|
|
templ("panic", panicFunction(util::PanicCode::ResourceError));
|
|
|
|
templ("fetchLength", arrayLengthFunction(_type));
|
|
|
|
templ("isDynamic", _type.isDynamicallySized());
|
|
|
|
bool isMappingBase = _type.baseType()->category() == Type::Category::Mapping;
|
|
|
|
templ("needsClearing", !isMappingBase);
|
|
|
|
if (!isMappingBase)
|
2021-04-22 07:47:12 +00:00
|
|
|
templ("cleanUpArrayEnd", cleanUpStorageArrayEndFunction(_type));
|
2020-12-21 12:17:49 +00:00
|
|
|
return templ.render();
|
2019-06-06 12:07:40 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-22 07:47:12 +00:00
|
|
|
string YulUtilFunctions::cleanUpStorageArrayEndFunction(ArrayType const& _type)
|
|
|
|
{
|
|
|
|
solAssert(_type.location() == DataLocation::Storage, "");
|
|
|
|
solAssert(_type.baseType()->category() != Type::Category::Mapping, "");
|
2022-01-28 17:57:05 +00:00
|
|
|
solAssert(!_type.isByteArrayOrString(), "");
|
2021-09-23 15:18:13 +00:00
|
|
|
solUnimplementedAssert(_type.baseType()->storageBytes() <= 32);
|
2021-04-22 07:47:12 +00:00
|
|
|
|
|
|
|
string functionName = "cleanup_storage_array_end_" + _type.identifier();
|
|
|
|
return m_functionCollector.createFunction(functionName, [&](vector<string>& _args, vector<string>&) {
|
|
|
|
_args = {"array", "len", "startIndex"};
|
|
|
|
return Whiskers(R"(
|
|
|
|
if lt(startIndex, len) {
|
|
|
|
// Size was reduced, clear end of array
|
|
|
|
let oldSlotCount := <convertToSize>(len)
|
|
|
|
let newSlotCount := <convertToSize>(startIndex)
|
|
|
|
let arrayDataStart := <dataPosition>(array)
|
|
|
|
let deleteStart := add(arrayDataStart, newSlotCount)
|
|
|
|
let deleteEnd := add(arrayDataStart, oldSlotCount)
|
|
|
|
<?packed>
|
|
|
|
// if we are dealing with packed array and offset is greater than zero
|
|
|
|
// we have to partially clear last slot that is still used, so decreasing start by one
|
|
|
|
let offset := mul(mod(startIndex, <itemsPerSlot>), <storageBytes>)
|
|
|
|
if gt(offset, 0) { <partialClearStorageSlot>(sub(deleteStart, 1), offset) }
|
|
|
|
</packed>
|
|
|
|
<clearStorageRange>(deleteStart, deleteEnd)
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("convertToSize", arrayConvertLengthToSize(_type))
|
|
|
|
("dataPosition", arrayDataAreaFunction(_type))
|
|
|
|
("clearStorageRange", clearStorageRangeFunction(*_type.baseType()))
|
|
|
|
("packed", _type.baseType()->storageBytes() <= 16)
|
|
|
|
("itemsPerSlot", to_string(32 / _type.baseType()->storageBytes()))
|
|
|
|
("storageBytes", to_string(_type.baseType()->storageBytes()))
|
|
|
|
("partialClearStorageSlot", partialClearStorageSlotFunction())
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-06 14:00:32 +00:00
|
|
|
string YulUtilFunctions::resizeDynamicByteArrayFunction(ArrayType const& _type)
|
|
|
|
{
|
|
|
|
string functionName = "resize_array_" + _type.identifier();
|
2021-04-19 14:35:21 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&](vector<string>& _args, vector<string>&) {
|
|
|
|
_args = {"array", "newLen"};
|
2020-10-06 14:00:32 +00:00
|
|
|
return Whiskers(R"(
|
2021-04-19 14:35:21 +00:00
|
|
|
let data := sload(array)
|
|
|
|
let oldLen := <extractLength>(data)
|
2020-10-06 14:00:32 +00:00
|
|
|
|
2021-04-19 14:35:21 +00:00
|
|
|
if gt(newLen, oldLen) {
|
|
|
|
<increaseSize>(array, data, oldLen, newLen)
|
|
|
|
}
|
2020-10-06 14:00:32 +00:00
|
|
|
|
2021-04-19 14:35:21 +00:00
|
|
|
if lt(newLen, oldLen) {
|
|
|
|
<decreaseSize>(array, data, oldLen, newLen)
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("extractLength", extractByteArrayLengthFunction())
|
|
|
|
("decreaseSize", decreaseByteArraySizeFunction(_type))
|
|
|
|
("increaseSize", increaseByteArraySizeFunction(_type))
|
|
|
|
.render();
|
2020-10-06 14:00:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-22 07:47:12 +00:00
|
|
|
string YulUtilFunctions::cleanUpDynamicByteArrayEndSlotsFunction(ArrayType const& _type)
|
|
|
|
{
|
2022-01-28 17:57:05 +00:00
|
|
|
solAssert(_type.isByteArrayOrString(), "");
|
2021-04-22 07:47:12 +00:00
|
|
|
solAssert(_type.isDynamicallySized(), "");
|
|
|
|
|
|
|
|
string functionName = "clean_up_bytearray_end_slots_" + _type.identifier();
|
|
|
|
return m_functionCollector.createFunction(functionName, [&](vector<string>& _args, vector<string>&) {
|
|
|
|
_args = {"array", "len", "startIndex"};
|
|
|
|
return Whiskers(R"(
|
|
|
|
if gt(len, 31) {
|
|
|
|
let dataArea := <dataLocation>(array)
|
2021-05-05 08:12:12 +00:00
|
|
|
let deleteStart := add(dataArea, <div32Ceil>(startIndex))
|
2021-04-22 07:47:12 +00:00
|
|
|
// If we are clearing array to be short byte array, we want to clear only data starting from array data area.
|
|
|
|
if lt(startIndex, 32) { deleteStart := dataArea }
|
2021-05-05 08:12:12 +00:00
|
|
|
<clearStorageRange>(deleteStart, add(dataArea, <div32Ceil>(len)))
|
2021-04-22 07:47:12 +00:00
|
|
|
}
|
|
|
|
)")
|
|
|
|
("dataLocation", arrayDataAreaFunction(_type))
|
2021-05-05 08:12:12 +00:00
|
|
|
("div32Ceil", divide32CeilFunction())
|
2021-04-22 07:47:12 +00:00
|
|
|
("clearStorageRange", clearStorageRangeFunction(*_type.baseType()))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-06 14:00:32 +00:00
|
|
|
string YulUtilFunctions::decreaseByteArraySizeFunction(ArrayType const& _type)
|
|
|
|
{
|
|
|
|
string functionName = "byte_array_decrease_size_" + _type.identifier();
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(array, data, oldLen, newLen) {
|
|
|
|
switch lt(newLen, 32)
|
|
|
|
case 0 {
|
|
|
|
let arrayDataStart := <dataPosition>(array)
|
2021-05-05 08:12:12 +00:00
|
|
|
let deleteStart := add(arrayDataStart, <div32Ceil>(newLen))
|
2020-10-06 14:00:32 +00:00
|
|
|
|
|
|
|
// we have to partially clear last slot that is still used
|
|
|
|
let offset := and(newLen, 0x1f)
|
|
|
|
if offset { <partialClearStorageSlot>(sub(deleteStart, 1), offset) }
|
|
|
|
|
2021-05-05 08:12:12 +00:00
|
|
|
<clearStorageRange>(deleteStart, add(arrayDataStart, <div32Ceil>(oldLen)))
|
2020-10-06 14:00:32 +00:00
|
|
|
|
|
|
|
sstore(array, or(mul(2, newLen), 1))
|
|
|
|
}
|
|
|
|
default {
|
|
|
|
switch gt(oldLen, 31)
|
|
|
|
case 1 {
|
|
|
|
let arrayDataStart := <dataPosition>(array)
|
|
|
|
// clear whole old array, as we are transforming to short bytes array
|
2021-05-05 08:12:12 +00:00
|
|
|
<clearStorageRange>(add(arrayDataStart, 1), add(arrayDataStart, <div32Ceil>(oldLen)))
|
2020-10-06 14:00:32 +00:00
|
|
|
<transitLongToShort>(array, newLen)
|
|
|
|
}
|
|
|
|
default {
|
|
|
|
sstore(array, <encodeUsedSetLen>(data, newLen))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})")
|
|
|
|
("functionName", functionName)
|
|
|
|
("dataPosition", arrayDataAreaFunction(_type))
|
|
|
|
("partialClearStorageSlot", partialClearStorageSlotFunction())
|
|
|
|
("clearStorageRange", clearStorageRangeFunction(*_type.baseType()))
|
|
|
|
("transitLongToShort", byteArrayTransitLongToShortFunction(_type))
|
2021-05-05 08:12:12 +00:00
|
|
|
("div32Ceil", divide32CeilFunction())
|
2020-10-06 14:00:32 +00:00
|
|
|
("encodeUsedSetLen", shortByteArrayEncodeUsedAreaSetLengthFunction())
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
string YulUtilFunctions::increaseByteArraySizeFunction(ArrayType const& _type)
|
|
|
|
{
|
|
|
|
string functionName = "byte_array_increase_size_" + _type.identifier();
|
2021-04-19 14:35:21 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&](vector<string>& _args, vector<string>&) {
|
|
|
|
_args = {"array", "data", "oldLen", "newLen"};
|
2020-10-06 14:00:32 +00:00
|
|
|
return Whiskers(R"(
|
2021-04-19 14:35:21 +00:00
|
|
|
if gt(newLen, <maxArrayLength>) { <panic>() }
|
|
|
|
|
|
|
|
switch lt(oldLen, 32)
|
|
|
|
case 0 {
|
|
|
|
// in this case array stays unpacked, so we just set new length
|
|
|
|
sstore(array, add(mul(2, newLen), 1))
|
|
|
|
}
|
|
|
|
default {
|
|
|
|
switch lt(newLen, 32)
|
2020-10-06 14:00:32 +00:00
|
|
|
case 0 {
|
2021-04-19 14:35:21 +00:00
|
|
|
// we need to copy elements to data area as we changed array from packed to unpacked
|
|
|
|
data := and(not(0xff), data)
|
|
|
|
sstore(<dataPosition>(array), data)
|
2020-10-06 14:00:32 +00:00
|
|
|
sstore(array, add(mul(2, newLen), 1))
|
|
|
|
}
|
|
|
|
default {
|
2021-04-19 14:35:21 +00:00
|
|
|
// here array stays packed, we just need to increase length
|
|
|
|
sstore(array, <encodeUsedSetLen>(data, newLen))
|
2020-10-06 14:00:32 +00:00
|
|
|
}
|
2021-04-19 14:35:21 +00:00
|
|
|
}
|
|
|
|
)")
|
|
|
|
("panic", panicFunction(PanicCode::ResourceError))
|
|
|
|
("maxArrayLength", (u256(1) << 64).str())
|
|
|
|
("dataPosition", arrayDataAreaFunction(_type))
|
|
|
|
("encodeUsedSetLen", shortByteArrayEncodeUsedAreaSetLengthFunction())
|
|
|
|
.render();
|
2020-10-06 14:00:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
string YulUtilFunctions::byteArrayTransitLongToShortFunction(ArrayType const& _type)
|
|
|
|
{
|
|
|
|
string functionName = "transit_byte_array_long_to_short_" + _type.identifier();
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(array, len) {
|
|
|
|
// we need to copy elements from old array to new
|
|
|
|
// we want to copy only elements that are part of the array after resizing
|
|
|
|
let dataPos := <dataPosition>(array)
|
|
|
|
let data := <extractUsedApplyLen>(sload(dataPos), len)
|
|
|
|
sstore(array, data)
|
|
|
|
sstore(dataPos, 0)
|
|
|
|
})")
|
|
|
|
("functionName", functionName)
|
|
|
|
("dataPosition", arrayDataAreaFunction(_type))
|
|
|
|
("extractUsedApplyLen", shortByteArrayEncodeUsedAreaSetLengthFunction())
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
string YulUtilFunctions::shortByteArrayEncodeUsedAreaSetLengthFunction()
|
|
|
|
{
|
|
|
|
string functionName = "extract_used_part_and_set_length_of_short_byte_array";
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(data, len) -> used {
|
|
|
|
// we want to save only elements that are part of the array after resizing
|
|
|
|
// others should be set to zero
|
2020-10-20 15:29:11 +00:00
|
|
|
data := <maskBytes>(data, len)
|
|
|
|
used := or(data, mul(2, len))
|
2020-10-06 14:00:32 +00:00
|
|
|
})")
|
|
|
|
("functionName", functionName)
|
2020-10-20 15:29:11 +00:00
|
|
|
("maskBytes", maskBytesFunctionDynamic())
|
2020-10-06 14:00:32 +00:00
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-20 16:06:06 +00:00
|
|
|
string YulUtilFunctions::longByteArrayStorageIndexAccessNoCheckFunction()
|
|
|
|
{
|
|
|
|
return m_functionCollector.createFunction(
|
|
|
|
"long_byte_array_index_access_no_checks",
|
|
|
|
[&](vector<string>& _args, vector<string>& _returnParams) {
|
|
|
|
_args = {"array", "index"};
|
|
|
|
_returnParams = {"slot", "offset"};
|
|
|
|
return Whiskers(R"(
|
|
|
|
offset := sub(31, mod(index, 0x20))
|
|
|
|
let dataArea := <dataAreaFunc>(array)
|
|
|
|
slot := add(dataArea, div(index, 0x20))
|
|
|
|
)")
|
|
|
|
("dataAreaFunc", arrayDataAreaFunction(*TypeProvider::bytesStorage()))
|
|
|
|
.render();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-07 20:22:58 +00:00
|
|
|
string YulUtilFunctions::storageArrayPopFunction(ArrayType const& _type)
|
|
|
|
{
|
|
|
|
solAssert(_type.location() == DataLocation::Storage, "");
|
|
|
|
solAssert(_type.isDynamicallySized(), "");
|
|
|
|
solUnimplementedAssert(_type.baseType()->storageBytes() <= 32, "Base type is not yet implemented.");
|
2022-01-28 17:57:05 +00:00
|
|
|
if (_type.isByteArrayOrString())
|
2020-05-05 17:53:17 +00:00
|
|
|
return storageByteArrayPopFunction(_type);
|
2019-11-07 20:22:58 +00:00
|
|
|
|
|
|
|
string functionName = "array_pop_" + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-11-07 20:22:58 +00:00
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(array) {
|
|
|
|
let oldLen := <fetchLength>(array)
|
2020-09-15 16:57:59 +00:00
|
|
|
if iszero(oldLen) { <panic>() }
|
2019-11-07 20:22:58 +00:00
|
|
|
let newLen := sub(oldLen, 1)
|
|
|
|
let slot, offset := <indexAccess>(array, newLen)
|
2020-12-21 12:17:49 +00:00
|
|
|
<?+setToZero><setToZero>(slot, offset)</+setToZero>
|
2019-11-07 20:22:58 +00:00
|
|
|
sstore(array, newLen)
|
|
|
|
})")
|
|
|
|
("functionName", functionName)
|
2020-10-12 14:01:45 +00:00
|
|
|
("panic", panicFunction(PanicCode::EmptyArrayPop))
|
2019-11-07 20:22:58 +00:00
|
|
|
("fetchLength", arrayLengthFunction(_type))
|
|
|
|
("indexAccess", storageArrayIndexAccessFunction(_type))
|
2020-12-21 12:17:49 +00:00
|
|
|
(
|
|
|
|
"setToZero",
|
|
|
|
_type.baseType()->category() != Type::Category::Mapping ? storageSetToZeroFunction(*_type.baseType()) : ""
|
|
|
|
)
|
2019-11-07 20:22:58 +00:00
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-05 17:53:17 +00:00
|
|
|
string YulUtilFunctions::storageByteArrayPopFunction(ArrayType const& _type)
|
|
|
|
{
|
|
|
|
solAssert(_type.location() == DataLocation::Storage, "");
|
|
|
|
solAssert(_type.isDynamicallySized(), "");
|
2022-01-28 17:57:05 +00:00
|
|
|
solAssert(_type.isByteArrayOrString(), "");
|
2020-05-05 17:53:17 +00:00
|
|
|
|
|
|
|
string functionName = "byte_array_pop_" + _type.identifier();
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(array) {
|
|
|
|
let data := sload(array)
|
|
|
|
let oldLen := <extractByteArrayLength>(data)
|
2020-09-15 16:57:59 +00:00
|
|
|
if iszero(oldLen) { <panic>() }
|
2020-05-05 17:53:17 +00:00
|
|
|
|
2020-10-06 14:00:32 +00:00
|
|
|
switch oldLen
|
|
|
|
case 32 {
|
2020-05-05 17:53:17 +00:00
|
|
|
// Here we have a special case where array transitions to shorter than 32
|
|
|
|
// So we need to copy data
|
2020-10-06 14:00:32 +00:00
|
|
|
<transitLongToShort>(array, 31)
|
2020-05-05 17:53:17 +00:00
|
|
|
}
|
|
|
|
default {
|
|
|
|
let newLen := sub(oldLen, 1)
|
|
|
|
switch lt(oldLen, 32)
|
|
|
|
case 1 {
|
2020-10-06 14:00:32 +00:00
|
|
|
sstore(array, <encodeUsedSetLen>(data, newLen))
|
2020-05-05 17:53:17 +00:00
|
|
|
}
|
|
|
|
default {
|
2021-04-20 16:06:06 +00:00
|
|
|
let slot, offset := <indexAccessNoChecks>(array, newLen)
|
2020-05-05 17:53:17 +00:00
|
|
|
<setToZero>(slot, offset)
|
2020-10-06 14:00:32 +00:00
|
|
|
sstore(array, sub(data, 2))
|
2020-05-05 17:53:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})")
|
|
|
|
("functionName", functionName)
|
2020-10-12 14:01:45 +00:00
|
|
|
("panic", panicFunction(PanicCode::EmptyArrayPop))
|
2020-05-05 17:53:17 +00:00
|
|
|
("extractByteArrayLength", extractByteArrayLengthFunction())
|
2020-10-06 14:00:32 +00:00
|
|
|
("transitLongToShort", byteArrayTransitLongToShortFunction(_type))
|
|
|
|
("encodeUsedSetLen", shortByteArrayEncodeUsedAreaSetLengthFunction())
|
2021-04-20 16:06:06 +00:00
|
|
|
("indexAccessNoChecks", longByteArrayStorageIndexAccessNoCheckFunction())
|
2020-05-05 17:53:17 +00:00
|
|
|
("setToZero", storageSetToZeroFunction(*_type.baseType()))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-10 09:13:16 +00:00
|
|
|
string YulUtilFunctions::storageArrayPushFunction(ArrayType const& _type, Type const* _fromType)
|
2019-11-07 20:22:58 +00:00
|
|
|
{
|
|
|
|
solAssert(_type.location() == DataLocation::Storage, "");
|
|
|
|
solAssert(_type.isDynamicallySized(), "");
|
2020-12-10 09:13:16 +00:00
|
|
|
if (!_fromType)
|
|
|
|
_fromType = _type.baseType();
|
|
|
|
else if (_fromType->isValueType())
|
2021-09-23 15:18:13 +00:00
|
|
|
solUnimplementedAssert(*_fromType == *_type.baseType());
|
2019-11-07 20:22:58 +00:00
|
|
|
|
2020-12-10 09:13:16 +00:00
|
|
|
string functionName =
|
|
|
|
string{"array_push_from_"} +
|
|
|
|
_fromType->identifier() +
|
|
|
|
"_to_" +
|
|
|
|
_type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-11-07 20:22:58 +00:00
|
|
|
return Whiskers(R"(
|
2020-12-10 09:13:16 +00:00
|
|
|
function <functionName>(array <values>) {
|
2022-01-28 17:57:05 +00:00
|
|
|
<?isByteArrayOrString>
|
2020-05-05 17:53:17 +00:00
|
|
|
let data := sload(array)
|
|
|
|
let oldLen := <extractByteArrayLength>(data)
|
2020-09-15 16:57:59 +00:00
|
|
|
if iszero(lt(oldLen, <maxArrayLength>)) { <panic>() }
|
2020-05-05 17:53:17 +00:00
|
|
|
|
|
|
|
switch gt(oldLen, 31)
|
|
|
|
case 0 {
|
2020-12-10 09:13:16 +00:00
|
|
|
let value := byte(0 <values>)
|
2020-05-05 17:53:17 +00:00
|
|
|
switch oldLen
|
|
|
|
case 31 {
|
|
|
|
// Here we have special case when array switches from short array to long array
|
|
|
|
// We need to copy data
|
|
|
|
let dataArea := <dataAreaFunction>(array)
|
|
|
|
data := and(data, not(0xff))
|
|
|
|
sstore(dataArea, or(and(0xff, value), data))
|
|
|
|
// New length is 32, encoded as (32 * 2 + 1)
|
|
|
|
sstore(array, 65)
|
|
|
|
}
|
|
|
|
default {
|
|
|
|
data := add(data, 2)
|
|
|
|
let shiftBits := mul(8, sub(31, oldLen))
|
|
|
|
let valueShifted := <shl>(shiftBits, and(0xff, value))
|
|
|
|
let mask := <shl>(shiftBits, 0xff)
|
|
|
|
data := or(and(data, not(mask)), valueShifted)
|
|
|
|
sstore(array, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default {
|
|
|
|
sstore(array, add(data, 2))
|
|
|
|
let slot, offset := <indexAccess>(array, oldLen)
|
2020-12-10 09:13:16 +00:00
|
|
|
<storeValue>(slot, offset <values>)
|
2020-05-05 17:53:17 +00:00
|
|
|
}
|
2022-01-28 17:57:05 +00:00
|
|
|
<!isByteArrayOrString>
|
2020-05-05 17:53:17 +00:00
|
|
|
let oldLen := sload(array)
|
2020-09-15 16:57:59 +00:00
|
|
|
if iszero(lt(oldLen, <maxArrayLength>)) { <panic>() }
|
2020-05-05 17:53:17 +00:00
|
|
|
sstore(array, add(oldLen, 1))
|
|
|
|
let slot, offset := <indexAccess>(array, oldLen)
|
2020-12-10 09:13:16 +00:00
|
|
|
<storeValue>(slot, offset <values>)
|
2022-01-28 17:57:05 +00:00
|
|
|
</isByteArrayOrString>
|
2019-11-07 20:22:58 +00:00
|
|
|
})")
|
|
|
|
("functionName", functionName)
|
2020-12-10 09:13:16 +00:00
|
|
|
("values", _fromType->sizeOnStack() == 0 ? "" : ", " + suffixedVariableNameList("value", 0, _fromType->sizeOnStack()))
|
2020-10-12 14:01:45 +00:00
|
|
|
("panic", panicFunction(PanicCode::ResourceError))
|
2022-01-28 17:57:05 +00:00
|
|
|
("extractByteArrayLength", _type.isByteArrayOrString() ? extractByteArrayLengthFunction() : "")
|
2020-05-05 17:53:17 +00:00
|
|
|
("dataAreaFunction", arrayDataAreaFunction(_type))
|
2022-01-28 17:57:05 +00:00
|
|
|
("isByteArrayOrString", _type.isByteArrayOrString())
|
2019-11-07 20:22:58 +00:00
|
|
|
("indexAccess", storageArrayIndexAccessFunction(_type))
|
2020-12-10 09:13:16 +00:00
|
|
|
("storeValue", updateStorageValueFunction(*_fromType, *_type.baseType()))
|
2019-11-07 20:22:58 +00:00
|
|
|
("maxArrayLength", (u256(1) << 64).str())
|
2020-05-05 17:53:17 +00:00
|
|
|
("shl", shiftLeftFunctionDynamic())
|
2019-11-07 20:22:58 +00:00
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-28 17:05:11 +00:00
|
|
|
string YulUtilFunctions::storageArrayPushZeroFunction(ArrayType const& _type)
|
|
|
|
{
|
|
|
|
solAssert(_type.location() == DataLocation::Storage, "");
|
|
|
|
solAssert(_type.isDynamicallySized(), "");
|
|
|
|
solUnimplementedAssert(_type.baseType()->storageBytes() <= 32, "Base type is not yet implemented.");
|
|
|
|
|
|
|
|
string functionName = "array_push_zero_" + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-11-28 17:05:11 +00:00
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(array) -> slot, offset {
|
2020-12-09 18:29:11 +00:00
|
|
|
<?isBytes>
|
|
|
|
let data := sload(array)
|
|
|
|
let oldLen := <extractLength>(data)
|
|
|
|
<increaseBytesSize>(array, data, oldLen, add(oldLen, 1))
|
|
|
|
<!isBytes>
|
|
|
|
let oldLen := <fetchLength>(array)
|
|
|
|
if iszero(lt(oldLen, <maxArrayLength>)) { <panic>() }
|
|
|
|
sstore(array, add(oldLen, 1))
|
|
|
|
</isBytes>
|
2019-11-28 17:05:11 +00:00
|
|
|
slot, offset := <indexAccess>(array, oldLen)
|
|
|
|
})")
|
|
|
|
("functionName", functionName)
|
2022-01-28 17:57:05 +00:00
|
|
|
("isBytes", _type.isByteArrayOrString())
|
|
|
|
("increaseBytesSize", _type.isByteArrayOrString() ? increaseByteArraySizeFunction(_type) : "")
|
|
|
|
("extractLength", _type.isByteArrayOrString() ? extractByteArrayLengthFunction() : "")
|
2020-10-12 14:01:45 +00:00
|
|
|
("panic", panicFunction(PanicCode::ResourceError))
|
2019-11-28 17:05:11 +00:00
|
|
|
("fetchLength", arrayLengthFunction(_type))
|
|
|
|
("indexAccess", storageArrayIndexAccessFunction(_type))
|
|
|
|
("maxArrayLength", (u256(1) << 64).str())
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-18 13:10:23 +00:00
|
|
|
string YulUtilFunctions::partialClearStorageSlotFunction()
|
|
|
|
{
|
|
|
|
string functionName = "partial_clear_storage_slot";
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(slot, offset) {
|
|
|
|
let mask := <shr>(mul(8, sub(32, offset)), <ones>)
|
|
|
|
sstore(slot, and(mask, sload(slot)))
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("ones", formatNumber((bigint(1) << 256) - 1))
|
|
|
|
("shr", shiftRightFunctionDynamic())
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-06 12:07:40 +00:00
|
|
|
string YulUtilFunctions::clearStorageRangeFunction(Type const& _type)
|
|
|
|
{
|
2020-09-18 13:10:23 +00:00
|
|
|
if (_type.storageBytes() < 32)
|
|
|
|
solAssert(_type.isValueType(), "");
|
2019-06-06 12:07:40 +00:00
|
|
|
|
2020-09-18 13:10:23 +00:00
|
|
|
string functionName = "clear_storage_range_" + _type.identifier();
|
2019-06-06 12:07:40 +00:00
|
|
|
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-06-06 12:07:40 +00:00
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(start, end) {
|
2019-07-10 10:41:31 +00:00
|
|
|
for {} lt(start, end) { start := add(start, <increment>) }
|
2019-06-06 12:07:40 +00:00
|
|
|
{
|
2019-07-10 10:41:31 +00:00
|
|
|
<setToZero>(start, 0)
|
2019-06-06 12:07:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2020-09-18 13:10:23 +00:00
|
|
|
("setToZero", storageSetToZeroFunction(_type.storageBytes() < 32 ? *TypeProvider::uint256() : _type))
|
2019-07-10 10:41:31 +00:00
|
|
|
("increment", _type.storageSize().str())
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
string YulUtilFunctions::clearStorageArrayFunction(ArrayType const& _type)
|
|
|
|
{
|
|
|
|
solAssert(_type.location() == DataLocation::Storage, "");
|
|
|
|
|
|
|
|
if (_type.baseType()->storageBytes() < 32)
|
|
|
|
{
|
|
|
|
solAssert(_type.baseType()->isValueType(), "Invalid storage size for non-value type.");
|
|
|
|
solAssert(_type.baseType()->storageSize() <= 1, "Invalid storage size for type.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type.baseType()->isValueType())
|
|
|
|
solAssert(_type.baseType()->storageSize() <= 1, "Invalid size for value type.");
|
|
|
|
|
|
|
|
string functionName = "clear_storage_array_" + _type.identifier();
|
|
|
|
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-07-10 10:41:31 +00:00
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(slot) {
|
|
|
|
<?dynamic>
|
|
|
|
<resizeArray>(slot, 0)
|
|
|
|
<!dynamic>
|
2020-12-21 12:17:49 +00:00
|
|
|
<?+clearRange><clearRange>(slot, add(slot, <lenToSize>(<len>)))</+clearRange>
|
2019-07-10 10:41:31 +00:00
|
|
|
</dynamic>
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("dynamic", _type.isDynamicallySized())
|
2020-11-26 16:15:41 +00:00
|
|
|
("resizeArray", _type.isDynamicallySized() ? resizeArrayFunction(_type) : "")
|
2019-07-10 10:41:31 +00:00
|
|
|
(
|
|
|
|
"clearRange",
|
2020-12-21 12:17:49 +00:00
|
|
|
_type.baseType()->category() != Type::Category::Mapping ?
|
|
|
|
clearStorageRangeFunction((_type.baseType()->storageBytes() < 32) ? *TypeProvider::uint256() : *_type.baseType()) :
|
|
|
|
""
|
2019-07-10 10:41:31 +00:00
|
|
|
)
|
|
|
|
("lenToSize", arrayConvertLengthToSize(_type))
|
|
|
|
("len", _type.length().str())
|
2019-06-06 12:07:40 +00:00
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-18 10:37:38 +00:00
|
|
|
string YulUtilFunctions::clearStorageStructFunction(StructType const& _type)
|
|
|
|
{
|
|
|
|
solAssert(_type.location() == DataLocation::Storage, "");
|
|
|
|
|
|
|
|
string functionName = "clear_struct_storage_" + _type.identifier();
|
|
|
|
|
|
|
|
return m_functionCollector.createFunction(functionName, [&] {
|
|
|
|
MemberList::MemberMap structMembers = _type.nativeMembers(nullptr);
|
|
|
|
vector<map<string, string>> memberSetValues;
|
|
|
|
|
2020-10-07 17:23:08 +00:00
|
|
|
set<u256> slotsCleared;
|
2020-09-18 10:37:38 +00:00
|
|
|
for (auto const& member: structMembers)
|
2020-12-21 12:17:49 +00:00
|
|
|
{
|
|
|
|
if (member.type->category() == Type::Category::Mapping)
|
|
|
|
continue;
|
2020-10-07 17:23:08 +00:00
|
|
|
if (member.type->storageBytes() < 32)
|
|
|
|
{
|
|
|
|
auto const& slotDiff = _type.storageOffsetsOfMember(member.name).first;
|
|
|
|
if (!slotsCleared.count(slotDiff))
|
|
|
|
{
|
|
|
|
memberSetValues.emplace_back().emplace("clearMember", "sstore(add(slot, " + slotDiff.str() + "), 0)");
|
|
|
|
slotsCleared.emplace(slotDiff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto const& [memberSlotDiff, memberStorageOffset] = _type.storageOffsetsOfMember(member.name);
|
|
|
|
solAssert(memberStorageOffset == 0, "");
|
|
|
|
|
|
|
|
memberSetValues.emplace_back().emplace("clearMember", Whiskers(R"(
|
|
|
|
<setZero>(add(slot, <memberSlotDiff>), <memberStorageOffset>)
|
|
|
|
)")
|
|
|
|
("setZero", storageSetToZeroFunction(*member.type))
|
|
|
|
("memberSlotDiff", memberSlotDiff.str())
|
|
|
|
("memberStorageOffset", to_string(memberStorageOffset))
|
|
|
|
.render()
|
|
|
|
);
|
|
|
|
}
|
2020-12-21 12:17:49 +00:00
|
|
|
}
|
2020-09-18 10:37:38 +00:00
|
|
|
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(slot) {
|
|
|
|
<#member>
|
|
|
|
<clearMember>
|
|
|
|
</member>
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("member", memberSetValues)
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-20 13:49:54 +00:00
|
|
|
string YulUtilFunctions::copyArrayToStorageFunction(ArrayType const& _fromType, ArrayType const& _toType)
|
2020-10-08 18:31:13 +00:00
|
|
|
{
|
|
|
|
solAssert(
|
2023-05-09 17:33:02 +00:00
|
|
|
(*_fromType.copyForLocation(_toType.location(), _toType.isPointer())).equals(dynamic_cast<ReferenceType const&>(_toType)),
|
2020-10-08 18:31:13 +00:00
|
|
|
""
|
|
|
|
);
|
2020-11-26 16:15:41 +00:00
|
|
|
if (!_toType.isDynamicallySized())
|
|
|
|
solAssert(!_fromType.isDynamicallySized() && _fromType.length() <= _toType.length(), "");
|
|
|
|
|
2022-01-28 17:57:05 +00:00
|
|
|
if (_fromType.isByteArrayOrString())
|
2020-10-20 15:05:58 +00:00
|
|
|
return copyByteArrayToStorageFunction(_fromType, _toType);
|
2022-06-09 13:39:58 +00:00
|
|
|
if (_toType.baseType()->isValueType())
|
|
|
|
return copyValueArrayToStorageFunction(_fromType, _toType);
|
|
|
|
|
|
|
|
solAssert(_toType.storageStride() == 32);
|
|
|
|
solAssert(!_fromType.baseType()->isValueType());
|
2020-10-08 18:31:13 +00:00
|
|
|
|
|
|
|
string functionName = "copy_array_to_storage_from_" + _fromType.identifier() + "_to_" + _toType.identifier();
|
|
|
|
return m_functionCollector.createFunction(functionName, [&](){
|
|
|
|
Whiskers templ(R"(
|
|
|
|
function <functionName>(slot, value<?isFromDynamicCalldata>, len</isFromDynamicCalldata>) {
|
2020-11-12 13:46:10 +00:00
|
|
|
<?fromStorage> if eq(slot, value) { leave } </fromStorage>
|
2020-10-08 18:31:13 +00:00
|
|
|
let length := <arrayLength>(value<?isFromDynamicCalldata>, len</isFromDynamicCalldata>)
|
2020-11-26 16:15:41 +00:00
|
|
|
|
|
|
|
<resizeArray>(slot, length)
|
2020-10-08 18:31:13 +00:00
|
|
|
|
2020-11-12 13:46:10 +00:00
|
|
|
let srcPtr := <srcDataLocation>(value)
|
2020-10-08 18:31:13 +00:00
|
|
|
|
|
|
|
let elementSlot := <dstDataLocation>(slot)
|
|
|
|
|
|
|
|
for { let i := 0 } lt(i, length) {i := add(i, 1)} {
|
|
|
|
<?fromCalldata>
|
2022-06-15 10:13:38 +00:00
|
|
|
let <stackItems> :=
|
2020-10-08 18:31:13 +00:00
|
|
|
<?dynamicallyEncodedBase>
|
|
|
|
<accessCalldataTail>(value, srcPtr)
|
|
|
|
<!dynamicallyEncodedBase>
|
|
|
|
srcPtr
|
|
|
|
</dynamicallyEncodedBase>
|
|
|
|
</fromCalldata>
|
|
|
|
|
|
|
|
<?fromMemory>
|
2022-06-15 10:13:38 +00:00
|
|
|
let <stackItems> := <readFromMemoryOrCalldata>(srcPtr)
|
2020-10-08 18:31:13 +00:00
|
|
|
</fromMemory>
|
|
|
|
|
2020-11-12 13:46:10 +00:00
|
|
|
<?fromStorage>
|
2022-06-15 10:13:38 +00:00
|
|
|
let <stackItems> := srcPtr
|
2020-11-12 13:46:10 +00:00
|
|
|
</fromStorage>
|
|
|
|
|
2022-06-15 10:13:38 +00:00
|
|
|
<updateStorageValue>(elementSlot, <stackItems>)
|
2020-10-08 18:31:13 +00:00
|
|
|
|
2020-11-17 13:51:43 +00:00
|
|
|
srcPtr := add(srcPtr, <srcStride>)
|
2020-10-08 18:31:13 +00:00
|
|
|
|
2022-06-09 13:39:58 +00:00
|
|
|
elementSlot := add(elementSlot, <storageSize>)
|
2020-10-08 18:31:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)");
|
2020-11-12 13:46:10 +00:00
|
|
|
if (_fromType.dataStoredIn(DataLocation::Storage))
|
|
|
|
solAssert(!_fromType.isValueType(), "");
|
2020-10-08 18:31:13 +00:00
|
|
|
templ("functionName", functionName);
|
|
|
|
bool fromCalldata = _fromType.dataStoredIn(DataLocation::CallData);
|
|
|
|
templ("isFromDynamicCalldata", _fromType.isDynamicallySized() && fromCalldata);
|
2020-11-12 13:46:10 +00:00
|
|
|
templ("fromStorage", _fromType.dataStoredIn(DataLocation::Storage));
|
|
|
|
bool fromMemory = _fromType.dataStoredIn(DataLocation::Memory);
|
|
|
|
templ("fromMemory", fromMemory);
|
2020-10-08 18:31:13 +00:00
|
|
|
templ("fromCalldata", fromCalldata);
|
2020-11-12 13:46:10 +00:00
|
|
|
templ("srcDataLocation", arrayDataAreaFunction(_fromType));
|
2020-10-08 18:31:13 +00:00
|
|
|
if (fromCalldata)
|
|
|
|
{
|
|
|
|
templ("dynamicallyEncodedBase", _fromType.baseType()->isDynamicallyEncoded());
|
|
|
|
if (_fromType.baseType()->isDynamicallyEncoded())
|
|
|
|
templ("accessCalldataTail", accessCalldataTailFunction(*_fromType.baseType()));
|
|
|
|
}
|
2020-11-26 16:15:41 +00:00
|
|
|
templ("resizeArray", resizeArrayFunction(_toType));
|
2020-10-08 18:31:13 +00:00
|
|
|
templ("arrayLength",arrayLengthFunction(_fromType));
|
|
|
|
templ("dstDataLocation", arrayDataAreaFunction(_toType));
|
2020-11-12 13:46:10 +00:00
|
|
|
if (fromMemory || (fromCalldata && _fromType.baseType()->isValueType()))
|
2022-06-15 10:13:38 +00:00
|
|
|
templ("readFromMemoryOrCalldata", readFromMemoryOrCalldata(*_fromType.baseType(), fromCalldata));
|
|
|
|
templ("stackItems", suffixedVariableNameList(
|
|
|
|
"stackItem_",
|
2020-10-08 18:31:13 +00:00
|
|
|
0,
|
|
|
|
_fromType.baseType()->stackItems().size()
|
|
|
|
));
|
2022-06-09 13:39:58 +00:00
|
|
|
templ("updateStorageValue", updateStorageValueFunction(*_fromType.baseType(), *_toType.baseType(), 0));
|
2020-11-17 13:51:43 +00:00
|
|
|
templ("srcStride",
|
|
|
|
fromCalldata ?
|
|
|
|
to_string(_fromType.calldataStride()) :
|
|
|
|
fromMemory ?
|
|
|
|
to_string(_fromType.memoryStride()) :
|
|
|
|
formatNumber(_fromType.baseType()->storageSize())
|
|
|
|
);
|
2020-10-08 18:31:13 +00:00
|
|
|
templ("storageSize", _toType.baseType()->storageSize().str());
|
|
|
|
|
|
|
|
return templ.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-20 15:05:58 +00:00
|
|
|
|
|
|
|
string YulUtilFunctions::copyByteArrayToStorageFunction(ArrayType const& _fromType, ArrayType const& _toType)
|
|
|
|
{
|
|
|
|
solAssert(
|
2023-05-09 17:33:02 +00:00
|
|
|
(*_fromType.copyForLocation(_toType.location(), _toType.isPointer())).equals(dynamic_cast<ReferenceType const&>(_toType)),
|
2020-10-20 15:05:58 +00:00
|
|
|
""
|
|
|
|
);
|
2022-01-28 17:57:05 +00:00
|
|
|
solAssert(_fromType.isByteArrayOrString(), "");
|
|
|
|
solAssert(_toType.isByteArrayOrString(), "");
|
2020-10-20 15:05:58 +00:00
|
|
|
|
|
|
|
string functionName = "copy_byte_array_to_storage_from_" + _fromType.identifier() + "_to_" + _toType.identifier();
|
|
|
|
return m_functionCollector.createFunction(functionName, [&](){
|
|
|
|
Whiskers templ(R"(
|
|
|
|
function <functionName>(slot, src<?fromCalldata>, len</fromCalldata>) {
|
2020-11-12 10:54:37 +00:00
|
|
|
<?fromStorage> if eq(slot, src) { leave } </fromStorage>
|
|
|
|
|
2020-10-20 15:05:58 +00:00
|
|
|
let newLen := <arrayLength>(src<?fromCalldata>, len</fromCalldata>)
|
|
|
|
// Make sure array length is sane
|
|
|
|
if gt(newLen, 0xffffffffffffffff) { <panic>() }
|
|
|
|
|
|
|
|
let oldLen := <byteArrayLength>(sload(slot))
|
|
|
|
|
2021-04-22 07:47:12 +00:00
|
|
|
// potentially truncate data
|
|
|
|
<cleanUpEndArray>(slot, oldLen, newLen)
|
|
|
|
|
2020-12-10 17:16:45 +00:00
|
|
|
let srcOffset := 0
|
2020-10-20 15:05:58 +00:00
|
|
|
<?fromMemory>
|
2020-12-10 17:16:45 +00:00
|
|
|
srcOffset := 0x20
|
2020-10-20 15:05:58 +00:00
|
|
|
</fromMemory>
|
|
|
|
|
|
|
|
switch gt(newLen, 31)
|
|
|
|
case 1 {
|
|
|
|
let loopEnd := and(newLen, not(0x1f))
|
2020-12-10 17:16:45 +00:00
|
|
|
<?fromStorage> src := <srcDataLocation>(src) </fromStorage>
|
2021-04-22 07:47:12 +00:00
|
|
|
let dstPtr := <dstDataLocation>(slot)
|
2020-10-20 15:05:58 +00:00
|
|
|
let i := 0
|
2020-12-10 17:16:45 +00:00
|
|
|
for { } lt(i, loopEnd) { i := add(i, 0x20) } {
|
|
|
|
sstore(dstPtr, <read>(add(src, srcOffset)))
|
2020-10-20 15:05:58 +00:00
|
|
|
dstPtr := add(dstPtr, 1)
|
2020-12-10 17:16:45 +00:00
|
|
|
srcOffset := add(srcOffset, <srcIncrement>)
|
2020-10-20 15:05:58 +00:00
|
|
|
}
|
|
|
|
if lt(loopEnd, newLen) {
|
2020-12-10 17:16:45 +00:00
|
|
|
let lastValue := <read>(add(src, srcOffset))
|
2020-10-20 15:05:58 +00:00
|
|
|
sstore(dstPtr, <maskBytes>(lastValue, and(newLen, 0x1f)))
|
|
|
|
}
|
|
|
|
sstore(slot, add(mul(newLen, 2), 1))
|
|
|
|
}
|
|
|
|
default {
|
|
|
|
let value := 0
|
|
|
|
if newLen {
|
2020-12-10 17:16:45 +00:00
|
|
|
value := <read>(add(src, srcOffset))
|
2020-10-20 15:05:58 +00:00
|
|
|
}
|
|
|
|
sstore(slot, <byteArrayCombineShort>(value, newLen))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
templ("functionName", functionName);
|
2020-11-12 10:54:37 +00:00
|
|
|
bool fromStorage = _fromType.dataStoredIn(DataLocation::Storage);
|
|
|
|
templ("fromStorage", fromStorage);
|
2020-10-20 15:05:58 +00:00
|
|
|
bool fromCalldata = _fromType.dataStoredIn(DataLocation::CallData);
|
|
|
|
templ("fromMemory", _fromType.dataStoredIn(DataLocation::Memory));
|
|
|
|
templ("fromCalldata", fromCalldata);
|
|
|
|
templ("arrayLength", arrayLengthFunction(_fromType));
|
2020-11-03 12:59:25 +00:00
|
|
|
templ("panic", panicFunction(PanicCode::ResourceError));
|
2020-10-20 15:05:58 +00:00
|
|
|
templ("byteArrayLength", extractByteArrayLengthFunction());
|
|
|
|
templ("dstDataLocation", arrayDataAreaFunction(_toType));
|
2020-12-10 17:16:45 +00:00
|
|
|
if (fromStorage)
|
|
|
|
templ("srcDataLocation", arrayDataAreaFunction(_fromType));
|
2021-04-22 07:47:12 +00:00
|
|
|
templ("cleanUpEndArray", cleanUpDynamicByteArrayEndSlotsFunction(_toType));
|
2020-12-10 17:16:45 +00:00
|
|
|
templ("srcIncrement", to_string(fromStorage ? 1 : 0x20));
|
2020-11-12 10:54:37 +00:00
|
|
|
templ("read", fromStorage ? "sload" : fromCalldata ? "calldataload" : "mload");
|
2020-10-20 15:05:58 +00:00
|
|
|
templ("maskBytes", maskBytesFunctionDynamic());
|
|
|
|
templ("byteArrayCombineShort", shortByteArrayEncodeUsedAreaSetLengthFunction());
|
|
|
|
|
|
|
|
return templ.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-09 13:39:58 +00:00
|
|
|
string YulUtilFunctions::copyValueArrayToStorageFunction(ArrayType const& _fromType, ArrayType const& _toType)
|
2020-11-12 13:46:10 +00:00
|
|
|
{
|
2020-12-03 17:56:57 +00:00
|
|
|
solAssert(_fromType.baseType()->isValueType(), "");
|
|
|
|
solAssert(_toType.baseType()->isValueType(), "");
|
|
|
|
solAssert(_fromType.baseType()->isImplicitlyConvertibleTo(*_toType.baseType()), "");
|
|
|
|
|
2022-01-28 17:57:05 +00:00
|
|
|
solAssert(!_fromType.isByteArrayOrString(), "");
|
|
|
|
solAssert(!_toType.isByteArrayOrString(), "");
|
2020-11-12 13:46:10 +00:00
|
|
|
solAssert(_toType.dataStoredIn(DataLocation::Storage), "");
|
|
|
|
|
2020-12-03 17:56:57 +00:00
|
|
|
solAssert(_fromType.storageStride() <= _toType.storageStride(), "");
|
|
|
|
solAssert(_toType.storageStride() <= 32, "");
|
2020-11-26 16:15:41 +00:00
|
|
|
|
2020-11-12 13:46:10 +00:00
|
|
|
string functionName = "copy_array_to_storage_from_" + _fromType.identifier() + "_to_" + _toType.identifier();
|
|
|
|
return m_functionCollector.createFunction(functionName, [&](){
|
|
|
|
Whiskers templ(R"(
|
2022-06-09 13:39:58 +00:00
|
|
|
function <functionName>(dst, src<?isFromDynamicCalldata>, len</isFromDynamicCalldata>) {
|
|
|
|
<?isFromStorage>
|
2020-11-12 13:46:10 +00:00
|
|
|
if eq(dst, src) { leave }
|
2022-06-09 13:39:58 +00:00
|
|
|
</isFromStorage>
|
|
|
|
let length := <arrayLength>(src<?isFromDynamicCalldata>, len</isFromDynamicCalldata>)
|
2020-11-12 13:46:10 +00:00
|
|
|
// Make sure array length is sane
|
|
|
|
if gt(length, 0xffffffffffffffff) { <panic>() }
|
2020-11-26 16:15:41 +00:00
|
|
|
<resizeArray>(dst, length)
|
2020-11-12 13:46:10 +00:00
|
|
|
|
2022-06-09 13:39:58 +00:00
|
|
|
let srcPtr := <srcDataLocation>(src)
|
2020-12-03 17:56:57 +00:00
|
|
|
let dstSlot := <dstDataLocation>(dst)
|
2020-11-12 13:46:10 +00:00
|
|
|
|
|
|
|
let fullSlots := div(length, <itemsPerSlot>)
|
2020-12-03 17:56:57 +00:00
|
|
|
|
2022-06-09 13:39:58 +00:00
|
|
|
<?isFromStorage>
|
|
|
|
let srcSlotValue := sload(srcPtr)
|
2020-12-03 17:56:57 +00:00
|
|
|
let srcItemIndexInSlot := 0
|
2022-06-09 13:39:58 +00:00
|
|
|
</isFromStorage>
|
|
|
|
|
2020-12-03 17:56:57 +00:00
|
|
|
for { let i := 0 } lt(i, fullSlots) { i := add(i, 1) } {
|
|
|
|
let dstSlotValue := 0
|
2022-06-09 13:39:58 +00:00
|
|
|
<?sameTypeFromStorage>
|
2020-12-03 17:56:57 +00:00
|
|
|
dstSlotValue := <maskFull>(srcSlotValue)
|
2022-06-09 13:39:58 +00:00
|
|
|
<updateSrcPtr>
|
|
|
|
<!sameTypeFromStorage>
|
2020-12-03 17:56:57 +00:00
|
|
|
<?multipleItemsPerSlotDst>for { let j := 0 } lt(j, <itemsPerSlot>) { j := add(j, 1) } </multipleItemsPerSlotDst>
|
|
|
|
{
|
2022-06-09 13:39:58 +00:00
|
|
|
<?isFromStorage>
|
2022-06-15 10:13:38 +00:00
|
|
|
let <stackItems> := <convert>(
|
2020-12-03 17:56:57 +00:00
|
|
|
<extractFromSlot>(srcSlotValue, mul(<srcStride>, srcItemIndexInSlot))
|
|
|
|
)
|
2022-06-09 13:39:58 +00:00
|
|
|
<!isFromStorage>
|
2022-06-15 10:13:38 +00:00
|
|
|
let <stackItems> := <readFromMemoryOrCalldata>(srcPtr)
|
2022-06-09 13:39:58 +00:00
|
|
|
</isFromStorage>
|
2022-06-15 10:13:38 +00:00
|
|
|
let itemValue := <prepareStore>(<stackItems>)
|
2020-12-03 17:56:57 +00:00
|
|
|
dstSlotValue :=
|
|
|
|
<?multipleItemsPerSlotDst>
|
|
|
|
<updateByteSlice>(dstSlotValue, mul(<dstStride>, j), itemValue)
|
|
|
|
<!multipleItemsPerSlotDst>
|
|
|
|
itemValue
|
|
|
|
</multipleItemsPerSlotDst>
|
|
|
|
|
2022-06-09 13:39:58 +00:00
|
|
|
<updateSrcPtr>
|
2020-12-03 17:56:57 +00:00
|
|
|
}
|
2022-06-09 13:39:58 +00:00
|
|
|
</sameTypeFromStorage>
|
2020-12-03 17:56:57 +00:00
|
|
|
|
|
|
|
sstore(add(dstSlot, i), dstSlotValue)
|
2020-11-12 13:46:10 +00:00
|
|
|
}
|
2020-12-03 17:56:57 +00:00
|
|
|
|
|
|
|
<?multipleItemsPerSlotDst>
|
|
|
|
let spill := sub(length, mul(fullSlots, <itemsPerSlot>))
|
|
|
|
if gt(spill, 0) {
|
|
|
|
let dstSlotValue := 0
|
2022-06-09 13:39:58 +00:00
|
|
|
<?sameTypeFromStorage>
|
2020-12-03 17:56:57 +00:00
|
|
|
dstSlotValue := <maskBytes>(srcSlotValue, mul(spill, <srcStride>))
|
2022-06-09 13:39:58 +00:00
|
|
|
<updateSrcPtr>
|
|
|
|
<!sameTypeFromStorage>
|
2020-12-03 17:56:57 +00:00
|
|
|
for { let j := 0 } lt(j, spill) { j := add(j, 1) } {
|
2022-06-09 13:39:58 +00:00
|
|
|
<?isFromStorage>
|
2022-06-15 10:13:38 +00:00
|
|
|
let <stackItems> := <convert>(
|
2020-12-03 17:56:57 +00:00
|
|
|
<extractFromSlot>(srcSlotValue, mul(<srcStride>, srcItemIndexInSlot))
|
|
|
|
)
|
2022-06-09 13:39:58 +00:00
|
|
|
<!isFromStorage>
|
2022-06-15 10:13:38 +00:00
|
|
|
let <stackItems> := <readFromMemoryOrCalldata>(srcPtr)
|
2022-06-09 13:39:58 +00:00
|
|
|
</isFromStorage>
|
2022-06-15 10:13:38 +00:00
|
|
|
let itemValue := <prepareStore>(<stackItems>)
|
2020-12-03 17:56:57 +00:00
|
|
|
dstSlotValue := <updateByteSlice>(dstSlotValue, mul(<dstStride>, j), itemValue)
|
|
|
|
|
2022-06-09 13:39:58 +00:00
|
|
|
<updateSrcPtr>
|
2020-12-03 17:56:57 +00:00
|
|
|
}
|
2022-06-09 13:39:58 +00:00
|
|
|
</sameTypeFromStorage>
|
2020-12-03 17:56:57 +00:00
|
|
|
sstore(add(dstSlot, fullSlots), dstSlotValue)
|
|
|
|
}
|
|
|
|
</multipleItemsPerSlotDst>
|
2020-11-12 13:46:10 +00:00
|
|
|
}
|
|
|
|
)");
|
|
|
|
if (_fromType.dataStoredIn(DataLocation::Storage))
|
|
|
|
solAssert(!_fromType.isValueType(), "");
|
2022-06-09 13:39:58 +00:00
|
|
|
|
|
|
|
bool fromCalldata = _fromType.dataStoredIn(DataLocation::CallData);
|
|
|
|
bool fromStorage = _fromType.dataStoredIn(DataLocation::Storage);
|
2020-11-12 13:46:10 +00:00
|
|
|
templ("functionName", functionName);
|
2020-11-26 16:15:41 +00:00
|
|
|
templ("resizeArray", resizeArrayFunction(_toType));
|
2021-12-06 12:31:27 +00:00
|
|
|
templ("arrayLength", arrayLengthFunction(_fromType));
|
2020-11-17 17:59:15 +00:00
|
|
|
templ("panic", panicFunction(PanicCode::ResourceError));
|
2022-06-09 13:39:58 +00:00
|
|
|
templ("isFromDynamicCalldata", _fromType.isDynamicallySized() && fromCalldata);
|
|
|
|
templ("isFromStorage", fromStorage);
|
2022-06-15 10:13:38 +00:00
|
|
|
templ("readFromMemoryOrCalldata", readFromMemoryOrCalldata(*_fromType.baseType(), fromCalldata));
|
2020-11-12 13:46:10 +00:00
|
|
|
templ("srcDataLocation", arrayDataAreaFunction(_fromType));
|
|
|
|
templ("dstDataLocation", arrayDataAreaFunction(_toType));
|
2020-12-03 17:56:57 +00:00
|
|
|
templ("srcStride", to_string(_fromType.storageStride()));
|
2022-06-15 10:13:38 +00:00
|
|
|
templ("stackItems", suffixedVariableNameList(
|
|
|
|
"stackItem_",
|
2022-06-09 13:39:58 +00:00
|
|
|
0,
|
|
|
|
_fromType.baseType()->stackItems().size()
|
|
|
|
));
|
2020-11-12 13:46:10 +00:00
|
|
|
unsigned itemsPerSlot = 32 / _toType.storageStride();
|
|
|
|
templ("itemsPerSlot", to_string(itemsPerSlot));
|
2020-12-03 17:56:57 +00:00
|
|
|
templ("multipleItemsPerSlotDst", itemsPerSlot > 1);
|
2022-06-09 13:39:58 +00:00
|
|
|
bool sameTypeFromStorage = fromStorage && (*_fromType.baseType() == *_toType.baseType());
|
2021-12-06 12:31:27 +00:00
|
|
|
if (auto functionType = dynamic_cast<FunctionType const*>(_fromType.baseType()))
|
|
|
|
{
|
|
|
|
solAssert(functionType->equalExcludingStateMutability(
|
|
|
|
dynamic_cast<FunctionType const&>(*_toType.baseType())
|
|
|
|
));
|
2022-06-09 13:39:58 +00:00
|
|
|
sameTypeFromStorage = fromStorage;
|
2021-12-06 12:31:27 +00:00
|
|
|
}
|
2022-06-09 13:39:58 +00:00
|
|
|
templ("sameTypeFromStorage", sameTypeFromStorage);
|
|
|
|
if (sameTypeFromStorage)
|
2020-12-03 17:56:57 +00:00
|
|
|
{
|
|
|
|
templ("maskFull", maskLowerOrderBytesFunction(itemsPerSlot * _toType.storageStride()));
|
|
|
|
templ("maskBytes", maskLowerOrderBytesFunctionDynamic());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
templ("dstStride", to_string(_toType.storageStride()));
|
|
|
|
templ("extractFromSlot", extractFromStorageValueDynamic(*_fromType.baseType()));
|
|
|
|
templ("updateByteSlice", updateByteSliceFunctionDynamic(_toType.storageStride()));
|
|
|
|
templ("convert", conversionFunction(*_fromType.baseType(), *_toType.baseType()));
|
|
|
|
templ("prepareStore", prepareStoreFunction(*_toType.baseType()));
|
|
|
|
}
|
2022-06-09 13:39:58 +00:00
|
|
|
if (fromStorage)
|
|
|
|
templ("updateSrcPtr", Whiskers(R"(
|
|
|
|
<?srcReadMultiPerSlot>
|
|
|
|
srcItemIndexInSlot := add(srcItemIndexInSlot, 1)
|
|
|
|
if eq(srcItemIndexInSlot, <srcItemsPerSlot>) {
|
|
|
|
// here we are done with this slot, we need to read next one
|
|
|
|
srcPtr := add(srcPtr, 1)
|
|
|
|
srcSlotValue := sload(srcPtr)
|
|
|
|
srcItemIndexInSlot := 0
|
|
|
|
}
|
|
|
|
<!srcReadMultiPerSlot>
|
|
|
|
srcPtr := add(srcPtr, 1)
|
|
|
|
srcSlotValue := sload(srcPtr)
|
|
|
|
</srcReadMultiPerSlot>
|
|
|
|
)")
|
|
|
|
("srcReadMultiPerSlot", !sameTypeFromStorage && _fromType.storageStride() <= 16)
|
|
|
|
("srcItemsPerSlot", to_string(32 / _fromType.storageStride()))
|
|
|
|
.render()
|
|
|
|
);
|
|
|
|
else
|
|
|
|
templ("updateSrcPtr", Whiskers(R"(
|
|
|
|
srcPtr := add(srcPtr, <srcStride>)
|
|
|
|
)")
|
|
|
|
("srcStride", fromCalldata ? to_string(_fromType.calldataStride()) : to_string(_fromType.memoryStride()))
|
|
|
|
.render()
|
|
|
|
);
|
2020-11-12 13:46:10 +00:00
|
|
|
|
|
|
|
return templ.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-06 12:07:40 +00:00
|
|
|
string YulUtilFunctions::arrayConvertLengthToSize(ArrayType const& _type)
|
|
|
|
{
|
|
|
|
string functionName = "array_convert_length_to_size_" + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-06-06 12:07:40 +00:00
|
|
|
Type const& baseType = *_type.baseType();
|
|
|
|
|
|
|
|
switch (_type.location())
|
|
|
|
{
|
|
|
|
case DataLocation::Storage:
|
|
|
|
{
|
|
|
|
unsigned const baseStorageBytes = baseType.storageBytes();
|
|
|
|
solAssert(baseStorageBytes > 0, "");
|
|
|
|
solAssert(32 / baseStorageBytes > 0, "");
|
|
|
|
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(length) -> size {
|
|
|
|
size := length
|
|
|
|
<?multiSlot>
|
|
|
|
size := <mul>(<storageSize>, length)
|
|
|
|
<!multiSlot>
|
|
|
|
// Number of slots rounded up
|
|
|
|
size := div(add(length, sub(<itemsPerSlot>, 1)), <itemsPerSlot>)
|
|
|
|
</multiSlot>
|
|
|
|
})")
|
|
|
|
("functionName", functionName)
|
|
|
|
("multiSlot", baseType.storageSize() > 1)
|
|
|
|
("itemsPerSlot", to_string(32 / baseStorageBytes))
|
|
|
|
("storageSize", baseType.storageSize().str())
|
2019-06-12 12:06:13 +00:00
|
|
|
("mul", overflowCheckedIntMulFunction(*TypeProvider::uint256()))
|
2019-06-06 12:07:40 +00:00
|
|
|
.render();
|
|
|
|
}
|
|
|
|
case DataLocation::CallData: // fallthrough
|
|
|
|
case DataLocation::Memory:
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(length) -> size {
|
|
|
|
<?byteArray>
|
|
|
|
size := length
|
|
|
|
<!byteArray>
|
2019-07-09 14:10:38 +00:00
|
|
|
size := <mul>(length, <stride>)
|
2019-06-06 12:07:40 +00:00
|
|
|
</byteArray>
|
|
|
|
})")
|
|
|
|
("functionName", functionName)
|
2019-07-09 14:10:38 +00:00
|
|
|
("stride", to_string(_type.location() == DataLocation::Memory ? _type.memoryStride() : _type.calldataStride()))
|
2022-01-28 17:57:05 +00:00
|
|
|
("byteArray", _type.isByteArrayOrString())
|
2019-06-12 12:06:13 +00:00
|
|
|
("mul", overflowCheckedIntMulFunction(*TypeProvider::uint256()))
|
2019-06-06 12:07:40 +00:00
|
|
|
.render();
|
|
|
|
default:
|
|
|
|
solAssert(false, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
2020-03-02 20:42:46 +00:00
|
|
|
|
2019-03-05 16:20:28 +00:00
|
|
|
string YulUtilFunctions::arrayAllocationSizeFunction(ArrayType const& _type)
|
|
|
|
{
|
|
|
|
solAssert(_type.dataStoredIn(DataLocation::Memory), "");
|
|
|
|
string functionName = "array_allocation_size_" + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-03-05 16:20:28 +00:00
|
|
|
Whiskers w(R"(
|
|
|
|
function <functionName>(length) -> size {
|
|
|
|
// Make sure we can allocate memory without overflow
|
2020-09-15 16:57:59 +00:00
|
|
|
if gt(length, 0xffffffffffffffff) { <panic>() }
|
2019-05-07 13:12:21 +00:00
|
|
|
<?byteArray>
|
2021-01-14 18:50:02 +00:00
|
|
|
size := <roundUp>(length)
|
2019-05-07 13:12:21 +00:00
|
|
|
<!byteArray>
|
|
|
|
size := mul(length, 0x20)
|
|
|
|
</byteArray>
|
|
|
|
<?dynamic>
|
|
|
|
// add length slot
|
|
|
|
size := add(size, 0x20)
|
|
|
|
</dynamic>
|
2019-03-05 16:20:28 +00:00
|
|
|
}
|
|
|
|
)");
|
|
|
|
w("functionName", functionName);
|
2020-10-12 14:01:45 +00:00
|
|
|
w("panic", panicFunction(PanicCode::ResourceError));
|
2022-01-28 17:57:05 +00:00
|
|
|
w("byteArray", _type.isByteArrayOrString());
|
2021-01-14 18:50:02 +00:00
|
|
|
w("roundUp", roundUpFunction());
|
2019-05-07 13:12:21 +00:00
|
|
|
w("dynamic", _type.isDynamicallySized());
|
2019-03-05 16:20:28 +00:00
|
|
|
return w.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
string YulUtilFunctions::arrayDataAreaFunction(ArrayType const& _type)
|
|
|
|
{
|
|
|
|
string functionName = "array_dataslot_" + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-05-07 13:37:22 +00:00
|
|
|
// No special processing for calldata arrays, because they are stored as
|
|
|
|
// offset of the data area and length on the stack, so the offset already
|
|
|
|
// points to the data area.
|
|
|
|
// This might change, if calldata arrays are stored in a single
|
|
|
|
// stack slot at some point.
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(ptr) -> data {
|
|
|
|
data := ptr
|
|
|
|
<?dynamic>
|
|
|
|
<?memory>
|
|
|
|
data := add(ptr, 0x20)
|
|
|
|
</memory>
|
|
|
|
<?storage>
|
|
|
|
mstore(0, ptr)
|
|
|
|
data := keccak256(0, 0x20)
|
|
|
|
</storage>
|
|
|
|
</dynamic>
|
2019-03-05 16:20:28 +00:00
|
|
|
}
|
2019-05-07 13:37:22 +00:00
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("dynamic", _type.isDynamicallySized())
|
|
|
|
("memory", _type.location() == DataLocation::Memory)
|
|
|
|
("storage", _type.location() == DataLocation::Storage)
|
|
|
|
.render();
|
2019-03-05 16:20:28 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-13 15:22:24 +00:00
|
|
|
string YulUtilFunctions::storageArrayIndexAccessFunction(ArrayType const& _type)
|
|
|
|
{
|
|
|
|
string functionName = "storage_array_index_access_" + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-06-13 15:22:24 +00:00
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(array, index) -> slot, offset {
|
2020-05-05 17:53:17 +00:00
|
|
|
let arrayLength := <arrayLen>(array)
|
2020-09-15 16:57:59 +00:00
|
|
|
if iszero(lt(index, arrayLength)) { <panic>() }
|
2019-06-13 15:22:24 +00:00
|
|
|
|
|
|
|
<?multipleItemsPerSlot>
|
2020-05-05 17:53:17 +00:00
|
|
|
<?isBytesArray>
|
|
|
|
switch lt(arrayLength, 0x20)
|
|
|
|
case 0 {
|
2021-04-20 16:06:06 +00:00
|
|
|
slot, offset := <indexAccessNoChecks>(array, index)
|
2020-05-05 17:53:17 +00:00
|
|
|
}
|
|
|
|
default {
|
2021-04-20 16:06:06 +00:00
|
|
|
offset := sub(31, mod(index, 0x20))
|
2020-05-05 17:53:17 +00:00
|
|
|
slot := array
|
|
|
|
}
|
|
|
|
<!isBytesArray>
|
|
|
|
let dataArea := <dataAreaFunc>(array)
|
2020-09-08 13:37:41 +00:00
|
|
|
slot := add(dataArea, div(index, <itemsPerSlot>))
|
|
|
|
offset := mul(mod(index, <itemsPerSlot>), <storageBytes>)
|
2020-05-05 17:53:17 +00:00
|
|
|
</isBytesArray>
|
2019-06-13 15:22:24 +00:00
|
|
|
<!multipleItemsPerSlot>
|
2020-05-05 17:53:17 +00:00
|
|
|
let dataArea := <dataAreaFunc>(array)
|
|
|
|
slot := add(dataArea, mul(index, <storageSize>))
|
2019-06-13 15:22:24 +00:00
|
|
|
offset := 0
|
|
|
|
</multipleItemsPerSlot>
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2020-10-12 14:01:45 +00:00
|
|
|
("panic", panicFunction(PanicCode::ArrayOutOfBounds))
|
2019-06-13 15:22:24 +00:00
|
|
|
("arrayLen", arrayLengthFunction(_type))
|
|
|
|
("dataAreaFunc", arrayDataAreaFunction(_type))
|
2021-04-20 16:06:06 +00:00
|
|
|
("indexAccessNoChecks", longByteArrayStorageIndexAccessNoCheckFunction())
|
2019-06-13 15:22:24 +00:00
|
|
|
("multipleItemsPerSlot", _type.baseType()->storageBytes() <= 16)
|
2022-01-28 17:57:05 +00:00
|
|
|
("isBytesArray", _type.isByteArrayOrString())
|
2019-06-13 15:22:24 +00:00
|
|
|
("storageSize", _type.baseType()->storageSize().str())
|
2020-05-05 17:53:17 +00:00
|
|
|
("storageBytes", toString(_type.baseType()->storageBytes()))
|
2020-09-08 13:37:41 +00:00
|
|
|
("itemsPerSlot", to_string(32 / _type.baseType()->storageBytes()))
|
2019-06-13 15:22:24 +00:00
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-27 11:36:06 +00:00
|
|
|
string YulUtilFunctions::memoryArrayIndexAccessFunction(ArrayType const& _type)
|
|
|
|
{
|
|
|
|
string functionName = "memory_array_index_access_" + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-06-27 11:36:06 +00:00
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(baseRef, index) -> addr {
|
|
|
|
if iszero(lt(index, <arrayLen>(baseRef))) {
|
2020-09-15 16:57:59 +00:00
|
|
|
<panic>()
|
2019-06-27 11:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let offset := mul(index, <stride>)
|
|
|
|
<?dynamicallySized>
|
|
|
|
offset := add(offset, 32)
|
|
|
|
</dynamicallySized>
|
|
|
|
addr := add(baseRef, offset)
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2020-10-12 14:01:45 +00:00
|
|
|
("panic", panicFunction(PanicCode::ArrayOutOfBounds))
|
2019-06-27 11:36:06 +00:00
|
|
|
("arrayLen", arrayLengthFunction(_type))
|
|
|
|
("stride", to_string(_type.memoryStride()))
|
|
|
|
("dynamicallySized", _type.isDynamicallySized())
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-11 16:43:43 +00:00
|
|
|
string YulUtilFunctions::calldataArrayIndexAccessFunction(ArrayType const& _type)
|
|
|
|
{
|
|
|
|
solAssert(_type.dataStoredIn(DataLocation::CallData), "");
|
|
|
|
string functionName = "calldata_array_index_access_" + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2020-02-11 16:43:43 +00:00
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(base_ref<?dynamicallySized>, length</dynamicallySized>, index) -> addr<?dynamicallySizedBase>, len</dynamicallySizedBase> {
|
2020-09-15 16:57:59 +00:00
|
|
|
if iszero(lt(index, <?dynamicallySized>length<!dynamicallySized><arrayLen></dynamicallySized>)) { <panic>() }
|
2020-02-11 16:43:43 +00:00
|
|
|
addr := add(base_ref, mul(index, <stride>))
|
|
|
|
<?dynamicallyEncodedBase>
|
|
|
|
addr<?dynamicallySizedBase>, len</dynamicallySizedBase> := <accessCalldataTail>(base_ref, addr)
|
|
|
|
</dynamicallyEncodedBase>
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2020-10-12 14:01:45 +00:00
|
|
|
("panic", panicFunction(PanicCode::ArrayOutOfBounds))
|
2020-02-11 16:43:43 +00:00
|
|
|
("stride", to_string(_type.calldataStride()))
|
|
|
|
("dynamicallySized", _type.isDynamicallySized())
|
|
|
|
("dynamicallyEncodedBase", _type.baseType()->isDynamicallyEncoded())
|
|
|
|
("dynamicallySizedBase", _type.baseType()->isDynamicallySized())
|
|
|
|
("arrayLen", toCompactHexWithPrefix(_type.length()))
|
|
|
|
("accessCalldataTail", _type.baseType()->isDynamicallyEncoded() ? accessCalldataTailFunction(*_type.baseType()): "")
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-06 19:59:34 +00:00
|
|
|
string YulUtilFunctions::calldataArrayIndexRangeAccess(ArrayType const& _type)
|
|
|
|
{
|
|
|
|
solAssert(_type.dataStoredIn(DataLocation::CallData), "");
|
|
|
|
solAssert(_type.isDynamicallySized(), "");
|
|
|
|
string functionName = "calldata_array_index_range_access_" + _type.identifier();
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(offset, length, startIndex, endIndex) -> offsetOut, lengthOut {
|
2021-03-15 15:45:00 +00:00
|
|
|
if gt(startIndex, endIndex) { <revertSliceStartAfterEnd>() }
|
|
|
|
if gt(endIndex, length) { <revertSliceGreaterThanLength>() }
|
2020-03-06 19:59:34 +00:00
|
|
|
offsetOut := add(offset, mul(startIndex, <stride>))
|
|
|
|
lengthOut := sub(endIndex, startIndex)
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("stride", to_string(_type.calldataStride()))
|
2021-03-15 15:45:00 +00:00
|
|
|
("revertSliceStartAfterEnd", revertReasonIfDebugFunction("Slice starts after end"))
|
|
|
|
("revertSliceGreaterThanLength", revertReasonIfDebugFunction("Slice is greater than length"))
|
2020-03-06 19:59:34 +00:00
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-11 16:43:43 +00:00
|
|
|
string YulUtilFunctions::accessCalldataTailFunction(Type const& _type)
|
|
|
|
{
|
|
|
|
solAssert(_type.isDynamicallyEncoded(), "");
|
|
|
|
solAssert(_type.dataStoredIn(DataLocation::CallData), "");
|
|
|
|
string functionName = "access_calldata_tail_" + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2020-02-11 16:43:43 +00:00
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(base_ref, ptr_to_tail) -> addr<?dynamicallySized>, length</dynamicallySized> {
|
|
|
|
let rel_offset_of_tail := calldataload(ptr_to_tail)
|
2021-03-15 15:45:00 +00:00
|
|
|
if iszero(slt(rel_offset_of_tail, sub(sub(calldatasize(), base_ref), sub(<neededLength>, 1)))) { <invalidCalldataTailOffset>() }
|
2020-02-11 16:43:43 +00:00
|
|
|
addr := add(base_ref, rel_offset_of_tail)
|
|
|
|
<?dynamicallySized>
|
|
|
|
length := calldataload(addr)
|
2021-03-15 15:45:00 +00:00
|
|
|
if gt(length, 0xffffffffffffffff) { <invalidCalldataTailLength>() }
|
2020-02-11 16:43:43 +00:00
|
|
|
addr := add(addr, 32)
|
2021-03-15 15:45:00 +00:00
|
|
|
if sgt(addr, sub(calldatasize(), mul(length, <calldataStride>))) { <shortCalldataTail>() }
|
2020-02-11 16:43:43 +00:00
|
|
|
</dynamicallySized>
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("dynamicallySized", _type.isDynamicallySized())
|
|
|
|
("neededLength", toCompactHexWithPrefix(_type.calldataEncodedTailSize()))
|
|
|
|
("calldataStride", toCompactHexWithPrefix(_type.isDynamicallySized() ? dynamic_cast<ArrayType const&>(_type).calldataStride() : 0))
|
2021-03-15 15:45:00 +00:00
|
|
|
("invalidCalldataTailOffset", revertReasonIfDebugFunction("Invalid calldata tail offset"))
|
|
|
|
("invalidCalldataTailLength", revertReasonIfDebugFunction("Invalid calldata tail length"))
|
|
|
|
("shortCalldataTail", revertReasonIfDebugFunction("Calldata tail too short"))
|
2020-02-11 16:43:43 +00:00
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-05 16:20:28 +00:00
|
|
|
string YulUtilFunctions::nextArrayElementFunction(ArrayType const& _type)
|
|
|
|
{
|
2022-01-28 17:57:05 +00:00
|
|
|
solAssert(!_type.isByteArrayOrString(), "");
|
2019-03-15 17:26:17 +00:00
|
|
|
if (_type.dataStoredIn(DataLocation::Storage))
|
|
|
|
solAssert(_type.baseType()->storageBytes() > 16, "");
|
2019-03-05 16:20:28 +00:00
|
|
|
string functionName = "array_nextElement_" + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-05-07 13:37:22 +00:00
|
|
|
Whiskers templ(R"(
|
|
|
|
function <functionName>(ptr) -> next {
|
|
|
|
next := add(ptr, <advance>)
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
templ("functionName", functionName);
|
2019-06-19 02:46:05 +00:00
|
|
|
switch (_type.location())
|
|
|
|
{
|
|
|
|
case DataLocation::Memory:
|
2019-05-07 13:37:22 +00:00
|
|
|
templ("advance", "0x20");
|
2019-06-19 02:46:05 +00:00
|
|
|
break;
|
|
|
|
case DataLocation::Storage:
|
|
|
|
{
|
|
|
|
u256 size = _type.baseType()->storageSize();
|
|
|
|
solAssert(size >= 1, "");
|
|
|
|
templ("advance", toCompactHexWithPrefix(size));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DataLocation::CallData:
|
|
|
|
{
|
2019-07-09 14:10:38 +00:00
|
|
|
u256 size = _type.calldataStride();
|
2019-06-19 02:46:05 +00:00
|
|
|
solAssert(size >= 32 && size % 32 == 0, "");
|
|
|
|
templ("advance", toCompactHexWithPrefix(size));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-05-07 13:37:22 +00:00
|
|
|
return templ.render();
|
2019-03-05 16:20:28 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-01 17:42:42 +00:00
|
|
|
string YulUtilFunctions::copyArrayFromStorageToMemoryFunction(ArrayType const& _from, ArrayType const& _to)
|
|
|
|
{
|
|
|
|
solAssert(_from.dataStoredIn(DataLocation::Storage), "");
|
|
|
|
solAssert(_to.dataStoredIn(DataLocation::Memory), "");
|
|
|
|
solAssert(_from.isDynamicallySized() == _to.isDynamicallySized(), "");
|
|
|
|
if (!_from.isDynamicallySized())
|
|
|
|
solAssert(_from.length() == _to.length(), "");
|
|
|
|
|
|
|
|
string functionName = "copy_array_from_storage_to_memory_" + _from.identifier();
|
|
|
|
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
if (_from.baseType()->isValueType())
|
|
|
|
{
|
2021-09-28 11:33:16 +00:00
|
|
|
solAssert(*_from.baseType() == *_to.baseType(), "");
|
2020-10-01 17:42:42 +00:00
|
|
|
ABIFunctions abi(m_evmVersion, m_revertStrings, m_functionCollector);
|
|
|
|
return Whiskers(R"(
|
2021-01-14 16:13:51 +00:00
|
|
|
function <functionName>(slot) -> memPtr {
|
|
|
|
memPtr := <allocateUnbounded>()
|
|
|
|
let end := <encode>(slot, memPtr)
|
|
|
|
<finalizeAllocation>(memPtr, sub(end, memPtr))
|
2020-10-01 17:42:42 +00:00
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2021-01-14 16:13:51 +00:00
|
|
|
("allocateUnbounded", allocateUnboundedFunction())
|
2020-10-01 17:42:42 +00:00
|
|
|
(
|
|
|
|
"encode",
|
|
|
|
abi.abiEncodeAndReturnUpdatedPosFunction(_from, _to, ABIFunctions::EncodingOptions{})
|
|
|
|
)
|
2021-01-14 16:13:51 +00:00
|
|
|
("finalizeAllocation", finalizeAllocationFunction())
|
2020-10-01 17:42:42 +00:00
|
|
|
.render();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
solAssert(_to.memoryStride() == 32, "");
|
|
|
|
solAssert(_to.baseType()->dataStoredIn(DataLocation::Memory), "");
|
|
|
|
solAssert(_from.baseType()->dataStoredIn(DataLocation::Storage), "");
|
2022-01-28 17:57:05 +00:00
|
|
|
solAssert(!_from.isByteArrayOrString(), "");
|
2020-10-01 17:42:42 +00:00
|
|
|
solAssert(*_to.withLocation(DataLocation::Storage, _from.isPointer()) == _from, "");
|
|
|
|
return Whiskers(R"(
|
2021-01-14 16:13:51 +00:00
|
|
|
function <functionName>(slot) -> memPtr {
|
2020-10-01 17:42:42 +00:00
|
|
|
let length := <lengthFunction>(slot)
|
2021-01-14 16:13:51 +00:00
|
|
|
memPtr := <allocateArray>(length)
|
|
|
|
let mpos := memPtr
|
2020-10-01 17:42:42 +00:00
|
|
|
<?dynamic>mpos := add(mpos, 0x20)</dynamic>
|
|
|
|
let spos := <arrayDataArea>(slot)
|
|
|
|
for { let i := 0 } lt(i, length) { i := add(i, 1) } {
|
|
|
|
mstore(mpos, <convert>(spos))
|
|
|
|
mpos := add(mpos, 0x20)
|
|
|
|
spos := add(spos, <baseStorageSize>)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("lengthFunction", arrayLengthFunction(_from))
|
|
|
|
("allocateArray", allocateMemoryArrayFunction(_to))
|
|
|
|
("arrayDataArea", arrayDataAreaFunction(_from))
|
|
|
|
("dynamic", _to.isDynamicallySized())
|
|
|
|
("convert", conversionFunction(*_from.baseType(), *_to.baseType()))
|
|
|
|
("baseStorageSize", _from.baseType()->storageSize().str())
|
|
|
|
.render();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-04 13:56:03 +00:00
|
|
|
string YulUtilFunctions::bytesOrStringConcatFunction(
|
|
|
|
vector<Type const*> const& _argumentTypes,
|
|
|
|
FunctionType::Kind _functionTypeKind
|
|
|
|
)
|
2021-02-23 13:40:46 +00:00
|
|
|
{
|
2022-02-04 13:56:03 +00:00
|
|
|
solAssert(_functionTypeKind == FunctionType::Kind::BytesConcat || _functionTypeKind == FunctionType::Kind::StringConcat);
|
|
|
|
std::string functionName = (_functionTypeKind == FunctionType::Kind::StringConcat) ? "string_concat" : "bytes_concat";
|
2021-02-23 13:40:46 +00:00
|
|
|
size_t totalParams = 0;
|
|
|
|
vector<Type const*> targetTypes;
|
2022-02-04 13:56:03 +00:00
|
|
|
|
2021-02-23 13:40:46 +00:00
|
|
|
for (Type const* argumentType: _argumentTypes)
|
|
|
|
{
|
2022-02-04 13:56:03 +00:00
|
|
|
if (_functionTypeKind == FunctionType::Kind::StringConcat)
|
|
|
|
solAssert(argumentType->isImplicitlyConvertibleTo(*TypeProvider::stringMemory()));
|
|
|
|
else if (_functionTypeKind == FunctionType::Kind::BytesConcat)
|
|
|
|
solAssert(
|
|
|
|
argumentType->isImplicitlyConvertibleTo(*TypeProvider::bytesMemory()) ||
|
|
|
|
argumentType->isImplicitlyConvertibleTo(*TypeProvider::fixedBytes(32))
|
|
|
|
);
|
|
|
|
|
2021-02-23 13:40:46 +00:00
|
|
|
if (argumentType->category() == Type::Category::FixedBytes)
|
|
|
|
targetTypes.emplace_back(argumentType);
|
|
|
|
else if (
|
|
|
|
auto const* literalType = dynamic_cast<StringLiteralType const*>(argumentType);
|
2021-06-25 18:48:34 +00:00
|
|
|
literalType && !literalType->value().empty() && literalType->value().size() <= 32
|
2021-02-23 13:40:46 +00:00
|
|
|
)
|
|
|
|
targetTypes.emplace_back(TypeProvider::fixedBytes(static_cast<unsigned>(literalType->value().size())));
|
|
|
|
else
|
|
|
|
{
|
2022-02-04 13:56:03 +00:00
|
|
|
solAssert(!dynamic_cast<RationalNumberType const*>(argumentType));
|
|
|
|
targetTypes.emplace_back(
|
|
|
|
_functionTypeKind == FunctionType::Kind::StringConcat ?
|
|
|
|
TypeProvider::stringMemory() :
|
|
|
|
TypeProvider::bytesMemory()
|
|
|
|
);
|
2021-02-23 13:40:46 +00:00
|
|
|
}
|
|
|
|
totalParams += argumentType->sizeOnStack();
|
|
|
|
functionName += "_" + argumentType->identifier();
|
|
|
|
}
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
Whiskers templ(R"(
|
|
|
|
function <functionName>(<parameters>) -> outPtr {
|
|
|
|
outPtr := <allocateUnbounded>()
|
|
|
|
let dataStart := add(outPtr, 0x20)
|
|
|
|
let dataEnd := <encodePacked>(dataStart<?+parameters>, <parameters></+parameters>)
|
|
|
|
mstore(outPtr, sub(dataEnd, dataStart))
|
|
|
|
<finalizeAllocation>(outPtr, sub(dataEnd, outPtr))
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
templ("functionName", functionName);
|
|
|
|
templ("parameters", suffixedVariableNameList("param_", 0, totalParams));
|
|
|
|
templ("allocateUnbounded", allocateUnboundedFunction());
|
|
|
|
templ("finalizeAllocation", finalizeAllocationFunction());
|
|
|
|
templ(
|
|
|
|
"encodePacked",
|
|
|
|
ABIFunctions{m_evmVersion, m_revertStrings, m_functionCollector}.tupleEncoderPacked(
|
|
|
|
_argumentTypes,
|
|
|
|
targetTypes
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return templ.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-02 10:48:34 +00:00
|
|
|
string YulUtilFunctions::mappingIndexAccessFunction(MappingType const& _mappingType, Type const& _keyType)
|
|
|
|
{
|
|
|
|
string functionName = "mapping_index_access_" + _mappingType.identifier() + "_of_" + _keyType.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-05-02 10:48:34 +00:00
|
|
|
if (_mappingType.keyType()->isDynamicallySized())
|
|
|
|
return Whiskers(R"(
|
2020-05-06 23:55:30 +00:00
|
|
|
function <functionName>(slot <?+key>,</+key> <key>) -> dataSlot {
|
|
|
|
dataSlot := <hash>(<key> <?+key>,</+key> slot)
|
2019-05-02 10:48:34 +00:00
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2021-02-24 12:44:23 +00:00
|
|
|
("key", suffixedVariableNameList("key_", 0, _keyType.sizeOnStack()))
|
2019-05-02 10:48:34 +00:00
|
|
|
("hash", packedHashFunction(
|
|
|
|
{&_keyType, TypeProvider::uint256()},
|
|
|
|
{_mappingType.keyType(), TypeProvider::uint256()}
|
|
|
|
))
|
|
|
|
.render();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
solAssert(CompilerUtils::freeMemoryPointer >= 0x40, "");
|
|
|
|
solAssert(!_mappingType.keyType()->isDynamicallyEncoded(), "");
|
|
|
|
solAssert(_mappingType.keyType()->calldataEncodedSize(false) <= 0x20, "");
|
|
|
|
Whiskers templ(R"(
|
|
|
|
function <functionName>(slot <key>) -> dataSlot {
|
|
|
|
mstore(0, <convertedKey>)
|
|
|
|
mstore(0x20, slot)
|
|
|
|
dataSlot := keccak256(0, 0x40)
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
templ("functionName", functionName);
|
|
|
|
templ("key", _keyType.sizeOnStack() == 1 ? ", key" : "");
|
|
|
|
if (_keyType.sizeOnStack() == 0)
|
|
|
|
templ("convertedKey", conversionFunction(_keyType, *_mappingType.keyType()) + "()");
|
|
|
|
else
|
|
|
|
templ("convertedKey", conversionFunction(_keyType, *_mappingType.keyType()) + "(key)");
|
|
|
|
return templ.render();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-05-02 09:05:02 +00:00
|
|
|
|
|
|
|
string YulUtilFunctions::readFromStorage(Type const& _type, size_t _offset, bool _splitFunctionTypes)
|
|
|
|
{
|
2020-08-19 11:13:05 +00:00
|
|
|
if (_type.isValueType())
|
|
|
|
return readFromStorageValueType(_type, _offset, _splitFunctionTypes);
|
|
|
|
else
|
2020-07-30 09:33:34 +00:00
|
|
|
{
|
|
|
|
solAssert(_offset == 0, "");
|
2020-08-19 11:13:05 +00:00
|
|
|
return readFromStorageReferenceType(_type);
|
|
|
|
}
|
|
|
|
}
|
2020-07-30 09:33:34 +00:00
|
|
|
|
2020-08-19 11:13:05 +00:00
|
|
|
string YulUtilFunctions::readFromStorageDynamic(Type const& _type, bool _splitFunctionTypes)
|
|
|
|
{
|
2020-12-09 17:02:25 +00:00
|
|
|
if (_type.isValueType())
|
|
|
|
return readFromStorageValueType(_type, {}, _splitFunctionTypes);
|
|
|
|
string functionName =
|
|
|
|
"read_from_storage__dynamic_" +
|
|
|
|
string(_splitFunctionTypes ? "split_" : "") +
|
|
|
|
_type.identifier();
|
|
|
|
|
|
|
|
return m_functionCollector.createFunction(functionName, [&] {
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(slot, offset) -> value {
|
|
|
|
if gt(offset, 0) { <panic>() }
|
|
|
|
value := <readFromStorage>(slot)
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("panic", panicFunction(util::PanicCode::Generic))
|
|
|
|
("readFromStorage", readFromStorageReferenceType(_type))
|
|
|
|
.render();
|
|
|
|
});
|
2020-08-19 11:13:05 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 10:15:49 +00:00
|
|
|
string YulUtilFunctions::readFromStorageValueType(Type const& _type, optional<size_t> _offset, bool _splitFunctionTypes)
|
2020-08-19 11:13:05 +00:00
|
|
|
{
|
|
|
|
solAssert(_type.isValueType(), "");
|
|
|
|
|
|
|
|
string functionName =
|
|
|
|
"read_from_storage_" +
|
2020-11-03 10:15:49 +00:00
|
|
|
string(_splitFunctionTypes ? "split_" : "") + (
|
|
|
|
_offset.has_value() ?
|
|
|
|
"offset_" + to_string(*_offset) :
|
|
|
|
"dynamic"
|
|
|
|
) +
|
2020-08-19 11:13:05 +00:00
|
|
|
"_" +
|
|
|
|
_type.identifier();
|
2020-07-30 09:33:34 +00:00
|
|
|
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&] {
|
2020-11-03 10:15:49 +00:00
|
|
|
Whiskers templ(R"(
|
|
|
|
function <functionName>(slot<?dynamic>, offset</dynamic>) -> <?split>addr, selector<!split>value</split> {
|
|
|
|
<?split>let</split> value := <extract>(sload(slot)<?dynamic>, offset</dynamic>)
|
|
|
|
<?split>
|
|
|
|
addr, selector := <splitFunction>(value)
|
|
|
|
</split>
|
2019-05-02 09:05:02 +00:00
|
|
|
}
|
2020-11-03 10:15:49 +00:00
|
|
|
)");
|
|
|
|
templ("functionName", functionName);
|
|
|
|
templ("dynamic", !_offset.has_value());
|
|
|
|
if (_offset.has_value())
|
|
|
|
templ("extract", extractFromStorageValue(_type, *_offset));
|
|
|
|
else
|
|
|
|
templ("extract", extractFromStorageValueDynamic(_type));
|
|
|
|
auto const* funType = dynamic_cast<FunctionType const*>(&_type);
|
|
|
|
bool split = _splitFunctionTypes && funType && funType->kind() == FunctionType::Kind::External;
|
|
|
|
templ("split", split);
|
|
|
|
if (split)
|
|
|
|
templ("splitFunction", splitExternalFunctionIdFunction());
|
|
|
|
return templ.render();
|
2019-05-02 09:05:02 +00:00
|
|
|
});
|
|
|
|
}
|
2020-07-30 09:33:34 +00:00
|
|
|
|
2020-08-19 11:13:05 +00:00
|
|
|
string YulUtilFunctions::readFromStorageReferenceType(Type const& _type)
|
|
|
|
{
|
2020-12-09 17:02:25 +00:00
|
|
|
if (auto const* arrayType = dynamic_cast<ArrayType const*>(&_type))
|
|
|
|
{
|
|
|
|
solAssert(arrayType->dataStoredIn(DataLocation::Memory), "");
|
|
|
|
return copyArrayFromStorageToMemoryFunction(
|
|
|
|
dynamic_cast<ArrayType const&>(*arrayType->copyForLocation(DataLocation::Storage, false)),
|
|
|
|
*arrayType
|
|
|
|
);
|
|
|
|
}
|
|
|
|
solAssert(_type.category() == Type::Category::Struct, "");
|
2020-08-19 11:13:05 +00:00
|
|
|
|
|
|
|
string functionName = "read_from_storage_reference_type_" + _type.identifier();
|
|
|
|
|
|
|
|
auto const& structType = dynamic_cast<StructType const&>(_type);
|
|
|
|
solAssert(structType.location() == DataLocation::Memory, "");
|
|
|
|
MemberList::MemberMap structMembers = structType.nativeMembers(nullptr);
|
|
|
|
vector<map<string, string>> memberSetValues(structMembers.size());
|
|
|
|
for (size_t i = 0; i < structMembers.size(); ++i)
|
|
|
|
{
|
|
|
|
auto const& [memberSlotDiff, memberStorageOffset] = structType.storageOffsetsOfMember(structMembers[i].name);
|
2020-12-09 17:02:25 +00:00
|
|
|
solAssert(structMembers[i].type->isValueType() || memberStorageOffset == 0, "");
|
2020-08-19 11:13:05 +00:00
|
|
|
|
|
|
|
memberSetValues[i]["setMember"] = Whiskers(R"(
|
|
|
|
{
|
2020-12-09 17:02:25 +00:00
|
|
|
let <memberValues> := <readFromStorage>(add(slot, <memberSlotDiff>))
|
2020-08-19 11:13:05 +00:00
|
|
|
<writeToMemory>(add(value, <memberMemoryOffset>), <memberValues>)
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("memberValues", suffixedVariableNameList("memberValue_", 0, structMembers[i].type->stackItems().size()))
|
|
|
|
("memberMemoryOffset", structType.memoryOffsetOfMember(structMembers[i].name).str())
|
|
|
|
("memberSlotDiff", memberSlotDiff.str())
|
2020-12-09 17:02:25 +00:00
|
|
|
("readFromStorage", readFromStorage(*structMembers[i].type, memberStorageOffset, true))
|
2020-08-19 11:13:05 +00:00
|
|
|
("writeToMemory", writeToMemoryFunction(*structMembers[i].type))
|
|
|
|
.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_functionCollector.createFunction(functionName, [&] {
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(slot) -> value {
|
|
|
|
value := <allocStruct>()
|
|
|
|
<#member>
|
|
|
|
<setMember>
|
|
|
|
</member>
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("allocStruct", allocateMemoryStructFunction(structType))
|
|
|
|
("member", memberSetValues)
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
2019-06-05 17:32:30 +00:00
|
|
|
|
2019-06-27 11:36:06 +00:00
|
|
|
string YulUtilFunctions::readFromMemory(Type const& _type)
|
|
|
|
{
|
|
|
|
return readFromMemoryOrCalldata(_type, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
string YulUtilFunctions::readFromCalldata(Type const& _type)
|
|
|
|
{
|
|
|
|
return readFromMemoryOrCalldata(_type, true);
|
|
|
|
}
|
|
|
|
|
2020-07-27 08:04:19 +00:00
|
|
|
string YulUtilFunctions::updateStorageValueFunction(
|
2020-09-01 12:43:40 +00:00
|
|
|
Type const& _fromType,
|
2020-07-27 08:04:19 +00:00
|
|
|
Type const& _toType,
|
|
|
|
std::optional<unsigned> const& _offset
|
|
|
|
)
|
2019-06-05 17:32:30 +00:00
|
|
|
{
|
|
|
|
string const functionName =
|
|
|
|
"update_storage_value_" +
|
2019-10-28 10:39:30 +00:00
|
|
|
(_offset.has_value() ? ("offset_" + to_string(*_offset)) : "") +
|
2020-09-01 12:43:40 +00:00
|
|
|
_fromType.identifier() +
|
2020-07-27 08:04:19 +00:00
|
|
|
"_to_" +
|
|
|
|
_toType.identifier();
|
2019-06-05 17:32:30 +00:00
|
|
|
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&] {
|
2020-07-27 08:04:19 +00:00
|
|
|
if (_toType.isValueType())
|
2019-06-05 17:32:30 +00:00
|
|
|
{
|
2020-09-01 12:43:40 +00:00
|
|
|
solAssert(_fromType.isImplicitlyConvertibleTo(_toType), "");
|
2020-07-27 08:04:19 +00:00
|
|
|
solAssert(_toType.storageBytes() <= 32, "Invalid storage bytes size.");
|
|
|
|
solAssert(_toType.storageBytes() > 0, "Invalid storage bytes size.");
|
2019-06-05 17:32:30 +00:00
|
|
|
|
|
|
|
return Whiskers(R"(
|
2020-11-02 19:53:42 +00:00
|
|
|
function <functionName>(slot, <offset><fromValues>) {
|
|
|
|
let <toValues> := <convert>(<fromValues>)
|
|
|
|
sstore(slot, <update>(sload(slot), <offset><prepare>(<toValues>)))
|
2019-06-05 17:32:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("update",
|
2019-10-28 10:39:30 +00:00
|
|
|
_offset.has_value() ?
|
2020-07-27 08:04:19 +00:00
|
|
|
updateByteSliceFunction(_toType.storageBytes(), *_offset) :
|
|
|
|
updateByteSliceFunctionDynamic(_toType.storageBytes())
|
2019-06-05 17:32:30 +00:00
|
|
|
)
|
2019-10-28 10:39:30 +00:00
|
|
|
("offset", _offset.has_value() ? "" : "offset, ")
|
2020-11-02 19:53:42 +00:00
|
|
|
("convert", conversionFunction(_fromType, _toType))
|
|
|
|
("fromValues", suffixedVariableNameList("value_", 0, _fromType.sizeOnStack()))
|
|
|
|
("toValues", suffixedVariableNameList("convertedValue_", 0, _toType.sizeOnStack()))
|
2020-07-27 08:04:19 +00:00
|
|
|
("prepare", prepareStoreFunction(_toType))
|
2019-06-05 17:32:30 +00:00
|
|
|
.render();
|
|
|
|
}
|
2020-11-12 10:54:37 +00:00
|
|
|
|
|
|
|
auto const* toReferenceType = dynamic_cast<ReferenceType const*>(&_toType);
|
|
|
|
auto const* fromReferenceType = dynamic_cast<ReferenceType const*>(&_fromType);
|
2021-01-13 17:33:49 +00:00
|
|
|
solAssert(toReferenceType, "");
|
|
|
|
|
|
|
|
if (!fromReferenceType)
|
|
|
|
{
|
|
|
|
solAssert(_fromType.category() == Type::Category::StringLiteral, "");
|
|
|
|
solAssert(toReferenceType->category() == Type::Category::Array, "");
|
|
|
|
auto const& toArrayType = dynamic_cast<ArrayType const&>(*toReferenceType);
|
2022-01-28 17:57:05 +00:00
|
|
|
solAssert(toArrayType.isByteArrayOrString(), "");
|
2021-01-13 17:33:49 +00:00
|
|
|
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(slot<?dynamicOffset>, offset</dynamicOffset>) {
|
|
|
|
<?dynamicOffset>if offset { <panic>() }</dynamicOffset>
|
2021-05-05 06:57:19 +00:00
|
|
|
<copyToStorage>(slot)
|
2021-01-13 17:33:49 +00:00
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("dynamicOffset", !_offset.has_value())
|
|
|
|
("panic", panicFunction(PanicCode::Generic))
|
2021-05-05 06:57:19 +00:00
|
|
|
("copyToStorage", copyLiteralToStorageFunction(dynamic_cast<StringLiteralType const&>(_fromType).value()))
|
2021-01-13 17:33:49 +00:00
|
|
|
.render();
|
|
|
|
}
|
|
|
|
|
2023-05-09 17:33:02 +00:00
|
|
|
solAssert((*toReferenceType->copyForLocation(
|
2020-11-12 10:54:37 +00:00
|
|
|
fromReferenceType->location(),
|
|
|
|
fromReferenceType->isPointer()
|
2023-05-09 17:33:02 +00:00
|
|
|
).get()).equals(*fromReferenceType), "");
|
2020-11-12 10:54:37 +00:00
|
|
|
|
2021-05-05 06:57:19 +00:00
|
|
|
if (fromReferenceType->category() == Type::Category::ArraySlice)
|
|
|
|
solAssert(toReferenceType->category() == Type::Category::Array, "");
|
|
|
|
else
|
|
|
|
solAssert(toReferenceType->category() == fromReferenceType->category(), "");
|
2020-12-10 09:13:16 +00:00
|
|
|
solAssert(_offset.value_or(0) == 0, "");
|
2020-09-01 12:43:40 +00:00
|
|
|
|
2020-12-10 09:13:16 +00:00
|
|
|
Whiskers templ(R"(
|
|
|
|
function <functionName>(slot, <?dynamicOffset>offset, </dynamicOffset><value>) {
|
|
|
|
<?dynamicOffset>if offset { <panic>() }</dynamicOffset>
|
|
|
|
<copyToStorage>(slot, <value>)
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
templ("functionName", functionName);
|
|
|
|
templ("dynamicOffset", !_offset.has_value());
|
|
|
|
templ("panic", panicFunction(PanicCode::Generic));
|
|
|
|
templ("value", suffixedVariableNameList("value_", 0, _fromType.sizeOnStack()));
|
|
|
|
if (_fromType.category() == Type::Category::Array)
|
|
|
|
templ("copyToStorage", copyArrayToStorageFunction(
|
2020-11-12 10:54:37 +00:00
|
|
|
dynamic_cast<ArrayType const&>(_fromType),
|
|
|
|
dynamic_cast<ArrayType const&>(_toType)
|
|
|
|
));
|
2021-05-05 06:57:19 +00:00
|
|
|
else if (_fromType.category() == Type::Category::ArraySlice)
|
|
|
|
{
|
|
|
|
solAssert(
|
|
|
|
_fromType.dataStoredIn(DataLocation::CallData),
|
|
|
|
"Currently only calldata array slices are supported!"
|
|
|
|
);
|
|
|
|
templ("copyToStorage", copyArrayToStorageFunction(
|
|
|
|
dynamic_cast<ArraySliceType const&>(_fromType).arrayType(),
|
|
|
|
dynamic_cast<ArrayType const&>(_toType)
|
|
|
|
));
|
|
|
|
}
|
2019-06-05 17:32:30 +00:00
|
|
|
else
|
2020-12-10 09:13:16 +00:00
|
|
|
templ("copyToStorage", copyStructToStorageFunction(
|
|
|
|
dynamic_cast<StructType const&>(_fromType),
|
|
|
|
dynamic_cast<StructType const&>(_toType)
|
|
|
|
));
|
2020-09-01 12:43:40 +00:00
|
|
|
|
2020-12-10 09:13:16 +00:00
|
|
|
return templ.render();
|
2019-06-05 17:32:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-27 11:36:06 +00:00
|
|
|
string YulUtilFunctions::writeToMemoryFunction(Type const& _type)
|
|
|
|
{
|
2020-11-03 10:15:49 +00:00
|
|
|
string const functionName = "write_to_memory_" + _type.identifier();
|
2019-06-27 11:36:06 +00:00
|
|
|
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&] {
|
2019-06-27 11:36:06 +00:00
|
|
|
solAssert(!dynamic_cast<StringLiteralType const*>(&_type), "");
|
|
|
|
if (auto ref = dynamic_cast<ReferenceType const*>(&_type))
|
|
|
|
{
|
|
|
|
solAssert(
|
|
|
|
ref->location() == DataLocation::Memory,
|
|
|
|
"Can only update types with location memory."
|
|
|
|
);
|
|
|
|
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(memPtr, value) {
|
|
|
|
mstore(memPtr, value)
|
2020-03-02 20:42:46 +00:00
|
|
|
}
|
2019-06-27 11:36:06 +00:00
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
.render();
|
|
|
|
}
|
|
|
|
else if (
|
|
|
|
_type.category() == Type::Category::Function &&
|
|
|
|
dynamic_cast<FunctionType const&>(_type).kind() == FunctionType::Kind::External
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(memPtr, addr, selector) {
|
|
|
|
mstore(memPtr, <combine>(addr, selector))
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("combine", combineExternalFunctionIdFunction())
|
|
|
|
.render();
|
|
|
|
}
|
|
|
|
else if (_type.isValueType())
|
|
|
|
{
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(memPtr, value) {
|
|
|
|
mstore(memPtr, <cleanup>(value))
|
2020-03-02 20:42:46 +00:00
|
|
|
}
|
2019-06-27 11:36:06 +00:00
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("cleanup", cleanupFunction(_type))
|
|
|
|
.render();
|
|
|
|
}
|
|
|
|
else // Should never happen
|
|
|
|
{
|
|
|
|
solAssert(
|
|
|
|
false,
|
|
|
|
"Memory store of type " + _type.toString(true) + " not allowed."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-03 10:15:49 +00:00
|
|
|
string YulUtilFunctions::extractFromStorageValueDynamic(Type const& _type)
|
2019-06-05 17:32:30 +00:00
|
|
|
{
|
|
|
|
string functionName =
|
2019-06-17 13:26:40 +00:00
|
|
|
"extract_from_storage_value_dynamic" +
|
2019-06-05 17:32:30 +00:00
|
|
|
_type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&] {
|
2019-06-05 17:32:30 +00:00
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(slot_value, offset) -> value {
|
|
|
|
value := <cleanupStorage>(<shr>(mul(offset, 8), slot_value))
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2019-06-17 13:26:40 +00:00
|
|
|
("shr", shiftRightFunctionDynamic())
|
2020-11-03 10:15:49 +00:00
|
|
|
("cleanupStorage", cleanupFromStorageFunction(_type))
|
2019-06-05 17:32:30 +00:00
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-03 10:15:49 +00:00
|
|
|
string YulUtilFunctions::extractFromStorageValue(Type const& _type, size_t _offset)
|
2019-05-02 09:05:02 +00:00
|
|
|
{
|
2020-11-03 10:15:49 +00:00
|
|
|
string functionName = "extract_from_storage_value_offset_" + to_string(_offset) + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&] {
|
2019-05-02 09:05:02 +00:00
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(slot_value) -> value {
|
|
|
|
value := <cleanupStorage>(<shr>(slot_value))
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("shr", shiftRightFunction(_offset * 8))
|
2020-11-03 10:15:49 +00:00
|
|
|
("cleanupStorage", cleanupFromStorageFunction(_type))
|
2019-05-02 09:05:02 +00:00
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-03 10:15:49 +00:00
|
|
|
string YulUtilFunctions::cleanupFromStorageFunction(Type const& _type)
|
2019-05-02 09:05:02 +00:00
|
|
|
{
|
|
|
|
solAssert(_type.isValueType(), "");
|
|
|
|
|
2020-11-03 10:15:49 +00:00
|
|
|
string functionName = string("cleanup_from_storage_") + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&] {
|
2019-05-02 09:05:02 +00:00
|
|
|
Whiskers templ(R"(
|
|
|
|
function <functionName>(value) -> cleaned {
|
2019-05-07 13:44:21 +00:00
|
|
|
cleaned := <cleaned>
|
2019-05-02 09:05:02 +00:00
|
|
|
}
|
|
|
|
)");
|
|
|
|
templ("functionName", functionName);
|
|
|
|
|
2021-09-28 13:39:18 +00:00
|
|
|
Type const* encodingType = &_type;
|
|
|
|
if (_type.category() == Type::Category::UserDefinedValueType)
|
|
|
|
encodingType = _type.encodingType();
|
|
|
|
unsigned storageBytes = encodingType->storageBytes();
|
|
|
|
if (IntegerType const* intType = dynamic_cast<IntegerType const*>(encodingType))
|
|
|
|
if (intType->isSigned() && storageBytes != 32)
|
2019-05-02 09:05:02 +00:00
|
|
|
{
|
2019-05-07 13:44:21 +00:00
|
|
|
templ("cleaned", "signextend(" + to_string(storageBytes - 1) + ", value)");
|
2019-05-02 09:05:02 +00:00
|
|
|
return templ.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (storageBytes == 32)
|
2019-05-07 13:44:21 +00:00
|
|
|
templ("cleaned", "value");
|
2021-09-28 14:07:43 +00:00
|
|
|
else if (encodingType->leftAligned())
|
2019-05-07 13:44:21 +00:00
|
|
|
templ("cleaned", shiftLeftFunction(256 - 8 * storageBytes) + "(value)");
|
2019-05-02 09:05:02 +00:00
|
|
|
else
|
2019-05-07 13:44:21 +00:00
|
|
|
templ("cleaned", "and(value, " + toCompactHexWithPrefix((u256(1) << (8 * storageBytes)) - 1) + ")");
|
2019-05-02 09:05:02 +00:00
|
|
|
|
|
|
|
return templ.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-30 16:32:56 +00:00
|
|
|
string YulUtilFunctions::prepareStoreFunction(Type const& _type)
|
|
|
|
{
|
|
|
|
string functionName = "prepare_store_" + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2020-11-02 20:07:27 +00:00
|
|
|
solAssert(_type.isValueType(), "");
|
|
|
|
auto const* funType = dynamic_cast<FunctionType const*>(&_type);
|
|
|
|
if (funType && funType->kind() == FunctionType::Kind::External)
|
|
|
|
{
|
|
|
|
Whiskers templ(R"(
|
|
|
|
function <functionName>(addr, selector) -> ret {
|
|
|
|
ret := <prepareBytes>(<combine>(addr, selector))
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
templ("functionName", functionName);
|
|
|
|
templ("prepareBytes", prepareStoreFunction(*TypeProvider::fixedBytes(24)));
|
|
|
|
templ("combine", combineExternalFunctionIdFunction());
|
|
|
|
return templ.render();
|
|
|
|
}
|
2019-04-30 16:32:56 +00:00
|
|
|
else
|
2020-11-02 20:07:27 +00:00
|
|
|
{
|
|
|
|
solAssert(_type.sizeOnStack() == 1, "");
|
|
|
|
Whiskers templ(R"(
|
|
|
|
function <functionName>(value) -> ret {
|
|
|
|
ret := <actualPrepare>
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
templ("functionName", functionName);
|
2021-09-28 12:21:39 +00:00
|
|
|
if (_type.leftAligned())
|
2020-11-02 20:07:27 +00:00
|
|
|
templ("actualPrepare", shiftRightFunction(256 - 8 * _type.storageBytes()) + "(value)");
|
|
|
|
else
|
|
|
|
templ("actualPrepare", "value");
|
|
|
|
return templ.render();
|
|
|
|
}
|
2019-04-30 16:32:56 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-05 16:20:28 +00:00
|
|
|
string YulUtilFunctions::allocationFunction()
|
|
|
|
{
|
2021-01-14 16:13:51 +00:00
|
|
|
string functionName = "allocate_memory";
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-03-05 16:20:28 +00:00
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(size) -> memPtr {
|
2021-01-14 16:13:51 +00:00
|
|
|
memPtr := <allocateUnbounded>()
|
|
|
|
<finalizeAllocation>(memPtr, size)
|
2019-03-05 16:20:28 +00:00
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2021-01-14 16:13:51 +00:00
|
|
|
("allocateUnbounded", allocateUnboundedFunction())
|
|
|
|
("finalizeAllocation", finalizeAllocationFunction())
|
2019-03-05 16:20:28 +00:00
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:13:51 +00:00
|
|
|
string YulUtilFunctions::allocateUnboundedFunction()
|
2020-04-09 19:59:17 +00:00
|
|
|
{
|
2021-01-14 16:13:51 +00:00
|
|
|
string functionName = "allocate_unbounded";
|
2020-04-09 19:59:17 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>() -> memPtr {
|
|
|
|
memPtr := mload(<freeMemoryPointer>)
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("freeMemoryPointer", to_string(CompilerUtils::freeMemoryPointer))
|
|
|
|
("functionName", functionName)
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:13:51 +00:00
|
|
|
string YulUtilFunctions::finalizeAllocationFunction()
|
2020-04-09 19:59:17 +00:00
|
|
|
{
|
2021-01-14 16:13:51 +00:00
|
|
|
string functionName = "finalize_allocation";
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2020-04-09 19:59:17 +00:00
|
|
|
return Whiskers(R"(
|
2021-01-14 16:13:51 +00:00
|
|
|
function <functionName>(memPtr, size) {
|
|
|
|
let newFreePtr := add(memPtr, <roundUp>(size))
|
|
|
|
// protect against overflow
|
|
|
|
if or(gt(newFreePtr, 0xffffffffffffffff), lt(newFreePtr, memPtr)) { <panic>() }
|
|
|
|
mstore(<freeMemoryPointer>, newFreePtr)
|
2020-04-09 19:59:17 +00:00
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2021-01-14 16:13:51 +00:00
|
|
|
("freeMemoryPointer", to_string(CompilerUtils::freeMemoryPointer))
|
|
|
|
("roundUp", roundUpFunction())
|
|
|
|
("panic", panicFunction(PanicCode::ResourceError))
|
2020-04-09 19:59:17 +00:00
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:42:46 +00:00
|
|
|
string YulUtilFunctions::zeroMemoryArrayFunction(ArrayType const& _type)
|
2019-06-27 11:36:06 +00:00
|
|
|
{
|
2020-03-02 20:42:46 +00:00
|
|
|
if (_type.baseType()->hasSimpleZeroValueInMemory())
|
|
|
|
return zeroMemoryFunction(*_type.baseType());
|
|
|
|
return zeroComplexMemoryArrayFunction(_type);
|
|
|
|
}
|
2019-06-27 11:36:06 +00:00
|
|
|
|
2020-03-02 20:42:46 +00:00
|
|
|
string YulUtilFunctions::zeroMemoryFunction(Type const& _type)
|
|
|
|
{
|
|
|
|
solAssert(_type.hasSimpleZeroValueInMemory(), "");
|
|
|
|
|
|
|
|
string functionName = "zero_memory_chunk_" + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-06-27 11:36:06 +00:00
|
|
|
return Whiskers(R"(
|
2020-03-02 20:42:46 +00:00
|
|
|
function <functionName>(dataStart, dataSizeInBytes) {
|
|
|
|
calldatacopy(dataStart, calldatasize(), dataSizeInBytes)
|
2019-06-27 11:36:06 +00:00
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:42:46 +00:00
|
|
|
string YulUtilFunctions::zeroComplexMemoryArrayFunction(ArrayType const& _type)
|
|
|
|
{
|
|
|
|
solAssert(!_type.baseType()->hasSimpleZeroValueInMemory(), "");
|
|
|
|
|
|
|
|
string functionName = "zero_complex_memory_array_" + _type.identifier();
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
solAssert(_type.memoryStride() == 32, "");
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(dataStart, dataSizeInBytes) {
|
|
|
|
for {let i := 0} lt(i, dataSizeInBytes) { i := add(i, <stride>) } {
|
|
|
|
mstore(add(dataStart, i), <zeroValue>())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("stride", to_string(_type.memoryStride()))
|
|
|
|
("zeroValue", zeroValueFunction(*_type.baseType(), false))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-20 12:37:19 +00:00
|
|
|
string YulUtilFunctions::allocateMemoryArrayFunction(ArrayType const& _type)
|
2020-03-02 20:42:46 +00:00
|
|
|
{
|
2020-07-20 12:37:19 +00:00
|
|
|
string functionName = "allocate_memory_array_" + _type.identifier();
|
2020-03-02 20:42:46 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(length) -> memPtr {
|
|
|
|
let allocSize := <allocSize>(length)
|
|
|
|
memPtr := <alloc>(allocSize)
|
2020-07-20 12:37:19 +00:00
|
|
|
<?dynamic>
|
|
|
|
mstore(memPtr, length)
|
|
|
|
</dynamic>
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("alloc", allocationFunction())
|
|
|
|
("allocSize", arrayAllocationSizeFunction(_type))
|
|
|
|
("dynamic", _type.isDynamicallySized())
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
string YulUtilFunctions::allocateAndInitializeMemoryArrayFunction(ArrayType const& _type)
|
|
|
|
{
|
|
|
|
string functionName = "allocate_and_zero_memory_array_" + _type.identifier();
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(length) -> memPtr {
|
|
|
|
memPtr := <allocArray>(length)
|
2020-03-02 20:42:46 +00:00
|
|
|
let dataStart := memPtr
|
2020-07-20 12:37:19 +00:00
|
|
|
let dataSize := <allocSize>(length)
|
2020-03-02 20:42:46 +00:00
|
|
|
<?dynamic>
|
|
|
|
dataStart := add(dataStart, 32)
|
|
|
|
dataSize := sub(dataSize, 32)
|
|
|
|
</dynamic>
|
|
|
|
<zeroArrayFunction>(dataStart, dataSize)
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2020-07-20 12:37:19 +00:00
|
|
|
("allocArray", allocateMemoryArrayFunction(_type))
|
2020-03-02 20:42:46 +00:00
|
|
|
("allocSize", arrayAllocationSizeFunction(_type))
|
|
|
|
("zeroArrayFunction", zeroMemoryArrayFunction(_type))
|
|
|
|
("dynamic", _type.isDynamicallySized())
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-24 16:14:29 +00:00
|
|
|
string YulUtilFunctions::allocateMemoryStructFunction(StructType const& _type)
|
2020-03-02 20:42:46 +00:00
|
|
|
{
|
2020-06-24 16:14:29 +00:00
|
|
|
string functionName = "allocate_memory_struct_" + _type.identifier();
|
2020-03-02 20:42:46 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
Whiskers templ(R"(
|
|
|
|
function <functionName>() -> memPtr {
|
2020-06-11 10:10:33 +00:00
|
|
|
memPtr := <alloc>(<allocSize>)
|
2020-06-24 16:14:29 +00:00
|
|
|
}
|
|
|
|
)");
|
|
|
|
templ("functionName", functionName);
|
|
|
|
templ("alloc", allocationFunction());
|
|
|
|
templ("allocSize", _type.memoryDataSize().str());
|
|
|
|
|
|
|
|
return templ.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
string YulUtilFunctions::allocateAndInitializeMemoryStructFunction(StructType const& _type)
|
|
|
|
{
|
|
|
|
string functionName = "allocate_and_zero_memory_struct_" + _type.identifier();
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
Whiskers templ(R"(
|
|
|
|
function <functionName>() -> memPtr {
|
|
|
|
memPtr := <allocStruct>()
|
2020-03-02 20:42:46 +00:00
|
|
|
let offset := memPtr
|
|
|
|
<#member>
|
|
|
|
mstore(offset, <zeroValue>())
|
|
|
|
offset := add(offset, 32)
|
|
|
|
</member>
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
templ("functionName", functionName);
|
2020-06-24 16:14:29 +00:00
|
|
|
templ("allocStruct", allocateMemoryStructFunction(_type));
|
2020-03-02 20:42:46 +00:00
|
|
|
|
|
|
|
TypePointers const& members = _type.memoryMemberTypes();
|
|
|
|
|
|
|
|
vector<map<string, string>> memberParams(members.size());
|
|
|
|
for (size_t i = 0; i < members.size(); ++i)
|
|
|
|
{
|
|
|
|
solAssert(members[i]->memoryHeadSize() == 32, "");
|
2020-05-26 08:35:04 +00:00
|
|
|
memberParams[i]["zeroValue"] = zeroValueFunction(
|
|
|
|
*TypeProvider::withLocationIfReference(DataLocation::Memory, members[i]),
|
|
|
|
false
|
|
|
|
);
|
2020-03-02 20:42:46 +00:00
|
|
|
}
|
|
|
|
templ("member", memberParams);
|
|
|
|
return templ.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-09 13:30:54 +00:00
|
|
|
string YulUtilFunctions::conversionFunction(Type const& _from, Type const& _to)
|
|
|
|
{
|
2021-08-16 12:28:15 +00:00
|
|
|
if (_from.category() == Type::Category::UserDefinedValueType)
|
|
|
|
{
|
|
|
|
solAssert(_from == _to || _to == dynamic_cast<UserDefinedValueType const&>(_from).underlyingType(), "");
|
|
|
|
return conversionFunction(dynamic_cast<UserDefinedValueType const&>(_from).underlyingType(), _to);
|
|
|
|
}
|
|
|
|
if (_to.category() == Type::Category::UserDefinedValueType)
|
|
|
|
{
|
|
|
|
solAssert(_from == _to || _from.isImplicitlyConvertibleTo(dynamic_cast<UserDefinedValueType const&>(_to).underlyingType()), "");
|
|
|
|
return conversionFunction(_from, dynamic_cast<UserDefinedValueType const&>(_to).underlyingType());
|
|
|
|
}
|
2020-03-03 11:20:04 +00:00
|
|
|
if (_from.category() == Type::Category::Function)
|
|
|
|
{
|
|
|
|
solAssert(_to.category() == Type::Category::Function, "");
|
|
|
|
FunctionType const& fromType = dynamic_cast<FunctionType const&>(_from);
|
|
|
|
FunctionType const& targetType = dynamic_cast<FunctionType const&>(_to);
|
|
|
|
solAssert(
|
|
|
|
fromType.isImplicitlyConvertibleTo(targetType) &&
|
|
|
|
fromType.sizeOnStack() == targetType.sizeOnStack() &&
|
|
|
|
(fromType.kind() == FunctionType::Kind::Internal || fromType.kind() == FunctionType::Kind::External) &&
|
|
|
|
fromType.kind() == targetType.kind(),
|
|
|
|
"Invalid function type conversion requested."
|
|
|
|
);
|
|
|
|
string const functionName =
|
|
|
|
"convert_" +
|
|
|
|
_from.identifier() +
|
|
|
|
"_to_" +
|
|
|
|
_to.identifier();
|
2020-03-09 10:42:34 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2020-03-03 11:20:04 +00:00
|
|
|
return Whiskers(R"(
|
2020-11-02 12:05:07 +00:00
|
|
|
function <functionName>(<?external>addr, </external>functionId) -> <?external>outAddr, </external>outFunctionId {
|
|
|
|
<?external>outAddr := addr</external>
|
2020-03-03 11:20:04 +00:00
|
|
|
outFunctionId := functionId
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2020-11-02 12:05:07 +00:00
|
|
|
("external", fromType.kind() == FunctionType::Kind::External)
|
2020-03-03 11:20:04 +00:00
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
2020-11-06 11:23:31 +00:00
|
|
|
else if (_from.category() == Type::Category::ArraySlice)
|
2020-03-06 19:59:34 +00:00
|
|
|
{
|
2020-12-11 10:07:22 +00:00
|
|
|
auto const& fromType = dynamic_cast<ArraySliceType const&>(_from);
|
2021-03-04 09:09:37 +00:00
|
|
|
if (_to.category() == Type::Category::FixedBytes)
|
|
|
|
{
|
2022-02-02 10:44:24 +00:00
|
|
|
solAssert(fromType.arrayType().isByteArray(), "Array types other than bytes not convertible to bytesNN.");
|
2021-03-04 09:09:37 +00:00
|
|
|
return bytesToFixedBytesConversionFunction(fromType.arrayType(), dynamic_cast<FixedBytesType const &>(_to));
|
|
|
|
}
|
2022-01-20 14:03:37 +00:00
|
|
|
solAssert(_to.category() == Type::Category::Array);
|
2020-12-11 10:07:22 +00:00
|
|
|
auto const& targetType = dynamic_cast<ArrayType const&>(_to);
|
2020-03-06 19:59:34 +00:00
|
|
|
|
2022-01-20 13:53:18 +00:00
|
|
|
solAssert(
|
|
|
|
fromType.arrayType().isImplicitlyConvertibleTo(targetType) ||
|
2022-01-28 17:57:05 +00:00
|
|
|
(fromType.arrayType().isByteArrayOrString() && targetType.isByteArrayOrString())
|
2022-01-20 13:53:18 +00:00
|
|
|
);
|
2020-03-06 19:59:34 +00:00
|
|
|
solAssert(
|
2020-12-14 10:48:51 +00:00
|
|
|
fromType.arrayType().dataStoredIn(DataLocation::CallData) &&
|
|
|
|
fromType.arrayType().isDynamicallySized() &&
|
2022-01-20 14:03:37 +00:00
|
|
|
!fromType.arrayType().baseType()->isDynamicallyEncoded()
|
2020-03-06 19:59:34 +00:00
|
|
|
);
|
|
|
|
|
2020-12-11 10:07:22 +00:00
|
|
|
if (!targetType.dataStoredIn(DataLocation::CallData))
|
|
|
|
return arrayConversionFunction(fromType.arrayType(), targetType);
|
|
|
|
|
2020-03-06 19:59:34 +00:00
|
|
|
string const functionName =
|
|
|
|
"convert_" +
|
|
|
|
_from.identifier() +
|
|
|
|
"_to_" +
|
|
|
|
_to.identifier();
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(offset, length) -> outOffset, outLength {
|
|
|
|
outOffset := offset
|
|
|
|
outLength := length
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
2020-11-06 11:23:31 +00:00
|
|
|
else if (_from.category() == Type::Category::Array)
|
|
|
|
{
|
2021-03-04 09:09:37 +00:00
|
|
|
auto const& fromArrayType = dynamic_cast<ArrayType const&>(_from);
|
|
|
|
if (_to.category() == Type::Category::FixedBytes)
|
|
|
|
{
|
2022-02-02 10:44:24 +00:00
|
|
|
solAssert(fromArrayType.isByteArray(), "Array types other than bytes not convertible to bytesNN.");
|
2021-03-04 09:09:37 +00:00
|
|
|
return bytesToFixedBytesConversionFunction(fromArrayType, dynamic_cast<FixedBytesType const &>(_to));
|
|
|
|
}
|
2020-11-06 11:23:31 +00:00
|
|
|
solAssert(_to.category() == Type::Category::Array, "");
|
2021-03-04 09:09:37 +00:00
|
|
|
return arrayConversionFunction(fromArrayType, dynamic_cast<ArrayType const&>(_to));
|
2020-11-06 11:23:31 +00:00
|
|
|
}
|
2020-03-06 19:59:34 +00:00
|
|
|
|
2019-05-02 14:24:54 +00:00
|
|
|
if (_from.sizeOnStack() != 1 || _to.sizeOnStack() != 1)
|
|
|
|
return conversionFunctionSpecial(_from, _to);
|
|
|
|
|
2019-04-09 13:30:54 +00:00
|
|
|
string functionName =
|
|
|
|
"convert_" +
|
|
|
|
_from.identifier() +
|
|
|
|
"_to_" +
|
|
|
|
_to.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-04-09 13:30:54 +00:00
|
|
|
Whiskers templ(R"(
|
|
|
|
function <functionName>(value) -> converted {
|
|
|
|
<body>
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
templ("functionName", functionName);
|
|
|
|
string body;
|
|
|
|
auto toCategory = _to.category();
|
|
|
|
auto fromCategory = _from.category();
|
|
|
|
switch (fromCategory)
|
|
|
|
{
|
|
|
|
case Type::Category::Address:
|
2021-08-04 13:48:10 +00:00
|
|
|
case Type::Category::Contract:
|
2019-04-09 13:30:54 +00:00
|
|
|
body =
|
|
|
|
Whiskers("converted := <convert>(value)")
|
|
|
|
("convert", conversionFunction(IntegerType(160), _to))
|
|
|
|
.render();
|
|
|
|
break;
|
|
|
|
case Type::Category::Integer:
|
|
|
|
case Type::Category::RationalNumber:
|
|
|
|
{
|
2021-08-04 13:48:10 +00:00
|
|
|
solAssert(_from.mobileType(), "");
|
2019-04-09 13:30:54 +00:00
|
|
|
if (RationalNumberType const* rational = dynamic_cast<RationalNumberType const*>(&_from))
|
2021-08-04 13:48:10 +00:00
|
|
|
if (rational->isFractional())
|
|
|
|
solAssert(toCategory == Type::Category::FixedPoint, "");
|
|
|
|
|
2021-08-05 11:21:08 +00:00
|
|
|
if (toCategory == Type::Category::Address || toCategory == Type::Category::Contract)
|
2019-04-09 13:30:54 +00:00
|
|
|
body =
|
|
|
|
Whiskers("converted := <convert>(value)")
|
2021-08-04 13:48:10 +00:00
|
|
|
("convert", conversionFunction(_from, IntegerType(160)))
|
|
|
|
.render();
|
2021-08-05 11:21:08 +00:00
|
|
|
else
|
2019-04-09 13:30:54 +00:00
|
|
|
{
|
2021-08-05 11:21:08 +00:00
|
|
|
Whiskers bodyTemplate("converted := <cleanOutput>(<convert>(<cleanInput>(value)))");
|
|
|
|
bodyTemplate("cleanInput", cleanupFunction(_from));
|
|
|
|
bodyTemplate("cleanOutput", cleanupFunction(_to));
|
|
|
|
string convert;
|
|
|
|
|
2021-09-28 12:21:39 +00:00
|
|
|
solAssert(_to.category() != Type::Category::UserDefinedValueType, "");
|
2021-08-05 11:21:08 +00:00
|
|
|
if (auto const* toFixedBytes = dynamic_cast<FixedBytesType const*>(&_to))
|
|
|
|
convert = shiftLeftFunction(256 - toFixedBytes->numBytes() * 8);
|
|
|
|
else if (dynamic_cast<FixedPointType const*>(&_to))
|
2021-09-23 15:18:13 +00:00
|
|
|
solUnimplemented("");
|
2021-08-05 11:21:08 +00:00
|
|
|
else if (dynamic_cast<IntegerType const*>(&_to))
|
2019-04-09 13:30:54 +00:00
|
|
|
{
|
2021-09-23 15:18:13 +00:00
|
|
|
solUnimplementedAssert(fromCategory != Type::Category::FixedPoint);
|
2021-08-05 11:21:08 +00:00
|
|
|
convert = identityFunction();
|
2019-04-09 13:30:54 +00:00
|
|
|
}
|
2021-08-05 11:21:08 +00:00
|
|
|
else if (toCategory == Type::Category::Enum)
|
|
|
|
{
|
|
|
|
solAssert(fromCategory != Type::Category::FixedPoint, "");
|
|
|
|
convert = identityFunction();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
solAssert(false, "");
|
|
|
|
solAssert(!convert.empty(), "");
|
|
|
|
bodyTemplate("convert", convert);
|
|
|
|
body = bodyTemplate.render();
|
2019-04-09 13:30:54 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::Category::Bool:
|
|
|
|
{
|
|
|
|
solAssert(_from == _to, "Invalid conversion for bool.");
|
|
|
|
body =
|
|
|
|
Whiskers("converted := <clean>(value)")
|
|
|
|
("clean", cleanupFunction(_from))
|
|
|
|
.render();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::Category::FixedPoint:
|
|
|
|
solUnimplemented("Fixed point types not implemented.");
|
|
|
|
break;
|
|
|
|
case Type::Category::Struct:
|
2020-07-24 16:42:15 +00:00
|
|
|
{
|
|
|
|
solAssert(toCategory == Type::Category::Struct, "");
|
|
|
|
auto const& fromStructType = dynamic_cast<StructType const &>(_from);
|
|
|
|
auto const& toStructType = dynamic_cast<StructType const &>(_to);
|
|
|
|
solAssert(fromStructType.structDefinition() == toStructType.structDefinition(), "");
|
|
|
|
|
2020-10-06 12:08:00 +00:00
|
|
|
if (fromStructType.location() == toStructType.location() && toStructType.isPointer())
|
|
|
|
body = "converted := value";
|
2020-07-30 09:33:34 +00:00
|
|
|
else
|
|
|
|
{
|
2021-09-23 15:18:13 +00:00
|
|
|
solUnimplementedAssert(toStructType.location() == DataLocation::Memory);
|
|
|
|
solUnimplementedAssert(fromStructType.location() != DataLocation::Memory);
|
2020-07-30 09:33:34 +00:00
|
|
|
|
2020-10-06 12:08:00 +00:00
|
|
|
if (fromStructType.location() == DataLocation::CallData)
|
|
|
|
body = Whiskers(R"(
|
|
|
|
converted := <abiDecode>(value, calldatasize())
|
2020-11-23 14:17:11 +00:00
|
|
|
)")
|
|
|
|
(
|
|
|
|
"abiDecode",
|
|
|
|
ABIFunctions(m_evmVersion, m_revertStrings, m_functionCollector).abiDecodingFunctionStruct(
|
|
|
|
toStructType,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
).render();
|
2020-10-06 12:08:00 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
solAssert(fromStructType.location() == DataLocation::Storage, "");
|
|
|
|
|
|
|
|
body = Whiskers(R"(
|
|
|
|
converted := <readFromStorage>(value)
|
|
|
|
)")
|
|
|
|
("readFromStorage", readFromStorage(toStructType, 0, true))
|
|
|
|
.render();
|
|
|
|
}
|
2020-07-30 09:33:34 +00:00
|
|
|
}
|
2020-07-24 16:42:15 +00:00
|
|
|
|
2019-04-09 13:30:54 +00:00
|
|
|
break;
|
2020-07-24 16:42:15 +00:00
|
|
|
}
|
2019-04-09 13:30:54 +00:00
|
|
|
case Type::Category::FixedBytes:
|
|
|
|
{
|
|
|
|
FixedBytesType const& from = dynamic_cast<FixedBytesType const&>(_from);
|
|
|
|
if (toCategory == Type::Category::Integer)
|
|
|
|
body =
|
|
|
|
Whiskers("converted := <convert>(<shift>(value))")
|
|
|
|
("shift", shiftRightFunction(256 - from.numBytes() * 8))
|
|
|
|
("convert", conversionFunction(IntegerType(from.numBytes() * 8), _to))
|
|
|
|
.render();
|
|
|
|
else if (toCategory == Type::Category::Address)
|
|
|
|
body =
|
|
|
|
Whiskers("converted := <convert>(value)")
|
|
|
|
("convert", conversionFunction(_from, IntegerType(160)))
|
|
|
|
.render();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
solAssert(toCategory == Type::Category::FixedBytes, "Invalid type conversion requested.");
|
2022-01-25 15:01:01 +00:00
|
|
|
FixedBytesType const& to = dynamic_cast<FixedBytesType const&>(_to);
|
2019-04-09 13:30:54 +00:00
|
|
|
body =
|
|
|
|
Whiskers("converted := <clean>(value)")
|
2022-01-25 15:01:01 +00:00
|
|
|
("clean", cleanupFunction((to.numBytes() <= from.numBytes()) ? to : from))
|
2019-04-09 13:30:54 +00:00
|
|
|
.render();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::Category::Function:
|
|
|
|
{
|
|
|
|
solAssert(false, "Conversion should not be called for function types.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::Category::Enum:
|
|
|
|
{
|
|
|
|
solAssert(toCategory == Type::Category::Integer || _from == _to, "");
|
|
|
|
EnumType const& enumType = dynamic_cast<decltype(enumType)>(_from);
|
|
|
|
body =
|
|
|
|
Whiskers("converted := <clean>(value)")
|
|
|
|
("clean", cleanupFunction(enumType))
|
|
|
|
.render();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::Category::Tuple:
|
|
|
|
{
|
2021-09-23 15:18:13 +00:00
|
|
|
solUnimplemented("Tuple conversion not implemented.");
|
2019-04-09 13:30:54 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-10-27 16:14:58 +00:00
|
|
|
case Type::Category::TypeType:
|
|
|
|
{
|
|
|
|
TypeType const& typeType = dynamic_cast<decltype(typeType)>(_from);
|
|
|
|
if (
|
|
|
|
auto const* contractType = dynamic_cast<ContractType const*>(typeType.actualType());
|
|
|
|
contractType->contractDefinition().isLibrary() &&
|
|
|
|
_to == *TypeProvider::address()
|
|
|
|
)
|
|
|
|
body = "converted := value";
|
|
|
|
else
|
|
|
|
solAssert(false, "Invalid conversion from " + _from.canonicalName() + " to " + _to.canonicalName());
|
|
|
|
break;
|
|
|
|
}
|
2020-11-18 19:20:25 +00:00
|
|
|
case Type::Category::Mapping:
|
|
|
|
{
|
|
|
|
solAssert(_from == _to, "");
|
|
|
|
body = "converted := value";
|
|
|
|
break;
|
|
|
|
}
|
2019-04-09 13:30:54 +00:00
|
|
|
default:
|
2020-11-08 21:01:22 +00:00
|
|
|
solAssert(false, "Invalid conversion from " + _from.canonicalName() + " to " + _to.canonicalName());
|
2019-04-09 13:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
solAssert(!body.empty(), _from.canonicalName() + " to " + _to.canonicalName());
|
|
|
|
templ("body", body);
|
|
|
|
return templ.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-04 09:09:37 +00:00
|
|
|
string YulUtilFunctions::bytesToFixedBytesConversionFunction(ArrayType const& _from, FixedBytesType const& _to)
|
|
|
|
{
|
2022-02-02 10:44:24 +00:00
|
|
|
solAssert(_from.isByteArray(), "");
|
2021-03-04 09:09:37 +00:00
|
|
|
solAssert(_from.isDynamicallySized(), "");
|
|
|
|
string functionName = "convert_bytes_to_fixedbytes_from_" + _from.identifier() + "_to_" + _to.identifier();
|
|
|
|
return m_functionCollector.createFunction(functionName, [&](auto& _args, auto& _returnParams) {
|
|
|
|
_args = { "array" };
|
|
|
|
bool fromCalldata = _from.dataStoredIn(DataLocation::CallData);
|
|
|
|
if (fromCalldata)
|
|
|
|
_args.emplace_back("len");
|
|
|
|
_returnParams = {"value"};
|
|
|
|
Whiskers templ(R"(
|
|
|
|
let length := <arrayLen>(array<?fromCalldata>, len</fromCalldata>)
|
|
|
|
let dataArea := array
|
|
|
|
<?fromMemory>
|
|
|
|
dataArea := <dataArea>(array)
|
|
|
|
</fromMemory>
|
|
|
|
<?fromStorage>
|
|
|
|
if gt(length, 31) { dataArea := <dataArea>(array) }
|
|
|
|
</fromStorage>
|
|
|
|
|
|
|
|
<?fromCalldata>
|
|
|
|
value := <cleanup>(calldataload(dataArea))
|
|
|
|
<!fromCalldata>
|
|
|
|
value := <extractValue>(dataArea)
|
|
|
|
</fromCalldata>
|
|
|
|
|
|
|
|
if lt(length, <fixedBytesLen>) {
|
|
|
|
value := and(
|
|
|
|
value,
|
|
|
|
<shl>(
|
|
|
|
mul(8, sub(<fixedBytesLen>, length)),
|
|
|
|
<mask>
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
templ("fromCalldata", fromCalldata);
|
|
|
|
templ("arrayLen", arrayLengthFunction(_from));
|
|
|
|
templ("fixedBytesLen", to_string(_to.numBytes()));
|
|
|
|
templ("fromMemory", _from.dataStoredIn(DataLocation::Memory));
|
|
|
|
templ("fromStorage", _from.dataStoredIn(DataLocation::Storage));
|
|
|
|
templ("dataArea", arrayDataAreaFunction(_from));
|
|
|
|
if (fromCalldata)
|
|
|
|
templ("cleanup", cleanupFunction(_to));
|
|
|
|
else
|
|
|
|
templ(
|
|
|
|
"extractValue",
|
|
|
|
_from.dataStoredIn(DataLocation::Storage) ?
|
|
|
|
readFromStorage(_to, 32 - _to.numBytes(), false) :
|
|
|
|
readFromMemory(_to)
|
|
|
|
);
|
|
|
|
templ("shl", shiftLeftFunctionDynamic());
|
|
|
|
templ("mask", formatNumber(~((u256(1) << (256 - _to.numBytes() * 8)) - 1)));
|
|
|
|
return templ.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-21 16:58:08 +00:00
|
|
|
string YulUtilFunctions::copyStructToStorageFunction(StructType const& _from, StructType const& _to)
|
|
|
|
{
|
|
|
|
solAssert(_to.dataStoredIn(DataLocation::Storage), "");
|
|
|
|
solAssert(_from.structDefinition() == _to.structDefinition(), "");
|
|
|
|
|
|
|
|
string functionName =
|
|
|
|
"copy_struct_to_storage_from_" +
|
|
|
|
_from.identifier() +
|
|
|
|
"_to_" +
|
|
|
|
_to.identifier();
|
|
|
|
|
2021-03-07 20:08:20 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&](auto& _arguments, auto&) {
|
|
|
|
_arguments = {"slot", "value"};
|
2020-12-21 16:58:08 +00:00
|
|
|
Whiskers templ(R"(
|
2021-03-07 20:08:20 +00:00
|
|
|
<?fromStorage> if iszero(eq(slot, value)) { </fromStorage>
|
|
|
|
<#member>
|
|
|
|
{
|
|
|
|
<updateMemberCall>
|
2020-12-21 16:58:08 +00:00
|
|
|
}
|
2021-03-07 20:08:20 +00:00
|
|
|
</member>
|
|
|
|
<?fromStorage> } </fromStorage>
|
2020-12-21 16:58:08 +00:00
|
|
|
)");
|
|
|
|
templ("fromStorage", _from.dataStoredIn(DataLocation::Storage));
|
|
|
|
|
|
|
|
MemberList::MemberMap structMembers = _from.nativeMembers(nullptr);
|
|
|
|
MemberList::MemberMap toStructMembers = _to.nativeMembers(nullptr);
|
|
|
|
|
|
|
|
vector<map<string, string>> memberParams(structMembers.size());
|
|
|
|
for (size_t i = 0; i < structMembers.size(); ++i)
|
|
|
|
{
|
|
|
|
Type const& memberType = *structMembers[i].type;
|
|
|
|
solAssert(memberType.memoryHeadSize() == 32, "");
|
|
|
|
auto const&[slotDiff, offset] = _to.storageOffsetsOfMember(structMembers[i].name);
|
|
|
|
|
|
|
|
Whiskers t(R"(
|
|
|
|
let memberSlot := add(slot, <memberStorageSlotDiff>)
|
|
|
|
let memberSrcPtr := add(value, <memberOffset>)
|
|
|
|
|
|
|
|
<?fromCalldata>
|
|
|
|
let <memberValues> :=
|
|
|
|
<?dynamicallyEncodedMember>
|
|
|
|
<accessCalldataTail>(value, memberSrcPtr)
|
|
|
|
<!dynamicallyEncodedMember>
|
|
|
|
memberSrcPtr
|
|
|
|
</dynamicallyEncodedMember>
|
|
|
|
|
|
|
|
<?isValueType>
|
|
|
|
<memberValues> := <read>(<memberValues>)
|
|
|
|
</isValueType>
|
|
|
|
</fromCalldata>
|
|
|
|
|
|
|
|
<?fromMemory>
|
|
|
|
let <memberValues> := <read>(memberSrcPtr)
|
|
|
|
</fromMemory>
|
|
|
|
|
|
|
|
<?fromStorage>
|
|
|
|
let <memberValues> :=
|
|
|
|
<?isValueType>
|
|
|
|
<read>(memberSrcPtr)
|
|
|
|
<!isValueType>
|
|
|
|
memberSrcPtr
|
|
|
|
</isValueType>
|
|
|
|
</fromStorage>
|
|
|
|
|
|
|
|
<updateStorageValue>(memberSlot, <memberValues>)
|
|
|
|
)");
|
|
|
|
bool fromCalldata = _from.location() == DataLocation::CallData;
|
|
|
|
t("fromCalldata", fromCalldata);
|
|
|
|
bool fromMemory = _from.location() == DataLocation::Memory;
|
|
|
|
t("fromMemory", fromMemory);
|
|
|
|
bool fromStorage = _from.location() == DataLocation::Storage;
|
|
|
|
t("fromStorage", fromStorage);
|
|
|
|
t("isValueType", memberType.isValueType());
|
|
|
|
t("memberValues", suffixedVariableNameList("memberValue_", 0, memberType.stackItems().size()));
|
|
|
|
|
|
|
|
t("memberStorageSlotDiff", slotDiff.str());
|
|
|
|
if (fromCalldata)
|
|
|
|
{
|
|
|
|
t("memberOffset", to_string(_from.calldataOffsetOfMember(structMembers[i].name)));
|
|
|
|
t("dynamicallyEncodedMember", memberType.isDynamicallyEncoded());
|
|
|
|
if (memberType.isDynamicallyEncoded())
|
|
|
|
t("accessCalldataTail", accessCalldataTailFunction(memberType));
|
|
|
|
if (memberType.isValueType())
|
|
|
|
t("read", readFromCalldata(memberType));
|
|
|
|
}
|
|
|
|
else if (fromMemory)
|
|
|
|
{
|
|
|
|
t("memberOffset", _from.memoryOffsetOfMember(structMembers[i].name).str());
|
|
|
|
t("read", readFromMemory(memberType));
|
|
|
|
}
|
|
|
|
else if (fromStorage)
|
|
|
|
{
|
2021-03-07 20:08:20 +00:00
|
|
|
auto const& [srcSlotOffset, srcOffset] = _from.storageOffsetsOfMember(structMembers[i].name);
|
2020-12-21 16:58:08 +00:00
|
|
|
t("memberOffset", formatNumber(srcSlotOffset));
|
|
|
|
if (memberType.isValueType())
|
2022-01-18 14:21:48 +00:00
|
|
|
t("read", readFromStorageValueType(memberType, srcOffset, true));
|
2020-12-21 16:58:08 +00:00
|
|
|
else
|
|
|
|
solAssert(srcOffset == 0, "");
|
|
|
|
|
|
|
|
}
|
|
|
|
t("updateStorageValue", updateStorageValueFunction(
|
|
|
|
memberType,
|
|
|
|
*toStructMembers[i].type,
|
|
|
|
optional<unsigned>{offset}
|
|
|
|
));
|
|
|
|
memberParams[i]["updateMemberCall"] = t.render();
|
|
|
|
}
|
|
|
|
templ("member", memberParams);
|
|
|
|
|
|
|
|
return templ.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-06 11:23:31 +00:00
|
|
|
string YulUtilFunctions::arrayConversionFunction(ArrayType const& _from, ArrayType const& _to)
|
|
|
|
{
|
2021-02-03 15:56:33 +00:00
|
|
|
if (_to.dataStoredIn(DataLocation::CallData))
|
|
|
|
solAssert(
|
2022-01-28 17:57:05 +00:00
|
|
|
_from.dataStoredIn(DataLocation::CallData) && _from.isByteArrayOrString() && _to.isByteArrayOrString(),
|
2021-02-03 15:56:33 +00:00
|
|
|
""
|
|
|
|
);
|
2020-11-10 16:41:52 +00:00
|
|
|
|
2020-11-06 11:23:31 +00:00
|
|
|
// Other cases are done explicitly in LValue::storeValue, and only possible by assignment.
|
|
|
|
if (_to.location() == DataLocation::Storage)
|
|
|
|
solAssert(
|
2022-01-28 17:57:05 +00:00
|
|
|
(_to.isPointer() || (_from.isByteArrayOrString() && _to.isByteArrayOrString())) &&
|
2020-11-06 11:23:31 +00:00
|
|
|
_from.location() == DataLocation::Storage,
|
|
|
|
"Invalid conversion to storage type."
|
|
|
|
);
|
|
|
|
|
|
|
|
string functionName =
|
|
|
|
"convert_array_" +
|
|
|
|
_from.identifier() +
|
|
|
|
"_to_" +
|
|
|
|
_to.identifier();
|
|
|
|
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
Whiskers templ(R"(
|
2021-02-03 15:56:33 +00:00
|
|
|
function <functionName>(value<?fromCalldataDynamic>, length</fromCalldataDynamic>) -> converted <?toCalldataDynamic>, outLength</toCalldataDynamic> {
|
2020-11-06 11:23:31 +00:00
|
|
|
<body>
|
2021-02-03 15:56:33 +00:00
|
|
|
<?toCalldataDynamic>
|
|
|
|
outLength := <length>
|
|
|
|
</toCalldataDynamic>
|
2020-11-06 11:23:31 +00:00
|
|
|
}
|
|
|
|
)");
|
|
|
|
templ("functionName", functionName);
|
|
|
|
templ("fromCalldataDynamic", _from.dataStoredIn(DataLocation::CallData) && _from.isDynamicallySized());
|
2021-02-03 15:56:33 +00:00
|
|
|
templ("toCalldataDynamic", _to.dataStoredIn(DataLocation::CallData) && _to.isDynamicallySized());
|
|
|
|
templ("length", _from.isDynamicallySized() ? "length" : _from.length().str());
|
2020-11-06 11:23:31 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
_from == _to ||
|
|
|
|
(_from.dataStoredIn(DataLocation::Memory) && _to.dataStoredIn(DataLocation::Memory)) ||
|
2021-02-03 15:56:33 +00:00
|
|
|
(_from.dataStoredIn(DataLocation::CallData) && _to.dataStoredIn(DataLocation::CallData)) ||
|
2020-11-06 11:23:31 +00:00
|
|
|
_to.dataStoredIn(DataLocation::Storage)
|
|
|
|
)
|
|
|
|
templ("body", "converted := value");
|
|
|
|
else if (_to.dataStoredIn(DataLocation::Memory))
|
|
|
|
templ(
|
|
|
|
"body",
|
|
|
|
Whiskers(R"(
|
|
|
|
// Copy the array to a free position in memory
|
2020-11-10 16:41:52 +00:00
|
|
|
converted :=
|
2020-11-06 11:23:31 +00:00
|
|
|
<?fromStorage>
|
2020-11-10 16:41:52 +00:00
|
|
|
<arrayStorageToMem>(value)
|
2020-11-06 11:23:31 +00:00
|
|
|
</fromStorage>
|
|
|
|
<?fromCalldata>
|
2020-11-10 16:41:52 +00:00
|
|
|
<abiDecode>(value, <length>, calldatasize())
|
2020-11-06 11:23:31 +00:00
|
|
|
</fromCalldata>
|
|
|
|
)")
|
|
|
|
("fromStorage", _from.dataStoredIn(DataLocation::Storage))
|
|
|
|
("fromCalldata", _from.dataStoredIn(DataLocation::CallData))
|
2020-11-10 16:41:52 +00:00
|
|
|
("length", _from.isDynamicallySized() ? "length" : _from.length().str())
|
|
|
|
(
|
|
|
|
"abiDecode",
|
|
|
|
_from.dataStoredIn(DataLocation::CallData) ?
|
|
|
|
ABIFunctions(
|
|
|
|
m_evmVersion,
|
|
|
|
m_revertStrings,
|
|
|
|
m_functionCollector
|
|
|
|
).abiDecodingFunctionArrayAvailableLength(_to, false) :
|
|
|
|
""
|
|
|
|
)
|
|
|
|
(
|
|
|
|
"arrayStorageToMem",
|
|
|
|
_from.dataStoredIn(DataLocation::Storage) ? copyArrayFromStorageToMemoryFunction(_from, _to) : ""
|
|
|
|
)
|
2020-11-06 11:23:31 +00:00
|
|
|
.render()
|
|
|
|
);
|
|
|
|
else
|
|
|
|
solAssert(false, "");
|
|
|
|
|
|
|
|
return templ.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-09 13:30:54 +00:00
|
|
|
string YulUtilFunctions::cleanupFunction(Type const& _type)
|
|
|
|
{
|
2021-08-16 12:28:15 +00:00
|
|
|
if (auto userDefinedValueType = dynamic_cast<UserDefinedValueType const*>(&_type))
|
|
|
|
return cleanupFunction(userDefinedValueType->underlyingType());
|
|
|
|
|
2019-04-09 13:30:54 +00:00
|
|
|
string functionName = string("cleanup_") + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-04-09 13:30:54 +00:00
|
|
|
Whiskers templ(R"(
|
|
|
|
function <functionName>(value) -> cleaned {
|
|
|
|
<body>
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
templ("functionName", functionName);
|
|
|
|
switch (_type.category())
|
|
|
|
{
|
|
|
|
case Type::Category::Address:
|
|
|
|
templ("body", "cleaned := " + cleanupFunction(IntegerType(160)) + "(value)");
|
|
|
|
break;
|
|
|
|
case Type::Category::Integer:
|
|
|
|
{
|
|
|
|
IntegerType const& type = dynamic_cast<IntegerType const&>(_type);
|
|
|
|
if (type.numBits() == 256)
|
|
|
|
templ("body", "cleaned := value");
|
|
|
|
else if (type.isSigned())
|
|
|
|
templ("body", "cleaned := signextend(" + to_string(type.numBits() / 8 - 1) + ", value)");
|
|
|
|
else
|
|
|
|
templ("body", "cleaned := and(value, " + toCompactHexWithPrefix((u256(1) << type.numBits()) - 1) + ")");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::Category::RationalNumber:
|
|
|
|
templ("body", "cleaned := value");
|
|
|
|
break;
|
|
|
|
case Type::Category::Bool:
|
|
|
|
templ("body", "cleaned := iszero(iszero(value))");
|
|
|
|
break;
|
|
|
|
case Type::Category::FixedPoint:
|
|
|
|
solUnimplemented("Fixed point types not implemented.");
|
|
|
|
break;
|
|
|
|
case Type::Category::Function:
|
2020-05-05 11:07:10 +00:00
|
|
|
switch (dynamic_cast<FunctionType const&>(_type).kind())
|
|
|
|
{
|
|
|
|
case FunctionType::Kind::External:
|
|
|
|
templ("body", "cleaned := " + cleanupFunction(FixedBytesType(24)) + "(value)");
|
|
|
|
break;
|
|
|
|
case FunctionType::Kind::Internal:
|
|
|
|
templ("body", "cleaned := value");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
solAssert(false, "");
|
|
|
|
break;
|
|
|
|
}
|
2019-04-09 13:30:54 +00:00
|
|
|
break;
|
|
|
|
case Type::Category::Array:
|
|
|
|
case Type::Category::Struct:
|
|
|
|
case Type::Category::Mapping:
|
|
|
|
solAssert(_type.dataStoredIn(DataLocation::Storage), "Cleanup requested for non-storage reference type.");
|
|
|
|
templ("body", "cleaned := value");
|
|
|
|
break;
|
|
|
|
case Type::Category::FixedBytes:
|
|
|
|
{
|
|
|
|
FixedBytesType const& type = dynamic_cast<FixedBytesType const&>(_type);
|
|
|
|
if (type.numBytes() == 32)
|
|
|
|
templ("body", "cleaned := value");
|
|
|
|
else if (type.numBytes() == 0)
|
|
|
|
// This is disallowed in the type system.
|
|
|
|
solAssert(false, "");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t numBits = type.numBytes() * 8;
|
|
|
|
u256 mask = ((u256(1) << numBits) - 1) << (256 - numBits);
|
|
|
|
templ("body", "cleaned := and(value, " + toCompactHexWithPrefix(mask) + ")");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::Category::Contract:
|
|
|
|
{
|
|
|
|
AddressType addressType(dynamic_cast<ContractType const&>(_type).isPayable() ?
|
|
|
|
StateMutability::Payable :
|
|
|
|
StateMutability::NonPayable
|
|
|
|
);
|
|
|
|
templ("body", "cleaned := " + cleanupFunction(addressType) + "(value)");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::Category::Enum:
|
|
|
|
{
|
|
|
|
// Out of range enums cannot be truncated unambigiously and therefore it should be an error.
|
2020-10-08 17:43:16 +00:00
|
|
|
templ("body", "cleaned := value " + validatorFunction(_type, false) + "(value)");
|
2019-04-09 13:30:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::Category::InaccessibleDynamic:
|
|
|
|
templ("body", "cleaned := 0");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
solAssert(false, "Cleanup of type " + _type.identifier() + " requested.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return templ.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
string YulUtilFunctions::validatorFunction(Type const& _type, bool _revertOnFailure)
|
|
|
|
{
|
|
|
|
string functionName = string("validator_") + (_revertOnFailure ? "revert_" : "assert_") + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-04-09 13:30:54 +00:00
|
|
|
Whiskers templ(R"(
|
|
|
|
function <functionName>(value) {
|
|
|
|
if iszero(<condition>) { <failure> }
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
templ("functionName", functionName);
|
2020-10-12 14:01:45 +00:00
|
|
|
PanicCode panicCode = PanicCode::Generic;
|
2019-04-09 13:30:54 +00:00
|
|
|
|
|
|
|
switch (_type.category())
|
|
|
|
{
|
|
|
|
case Type::Category::Address:
|
|
|
|
case Type::Category::Integer:
|
|
|
|
case Type::Category::RationalNumber:
|
|
|
|
case Type::Category::Bool:
|
|
|
|
case Type::Category::FixedPoint:
|
|
|
|
case Type::Category::Function:
|
|
|
|
case Type::Category::Array:
|
|
|
|
case Type::Category::Struct:
|
|
|
|
case Type::Category::Mapping:
|
|
|
|
case Type::Category::FixedBytes:
|
|
|
|
case Type::Category::Contract:
|
2021-08-16 12:28:15 +00:00
|
|
|
case Type::Category::UserDefinedValueType:
|
2019-04-09 13:30:54 +00:00
|
|
|
{
|
|
|
|
templ("condition", "eq(value, " + cleanupFunction(_type) + "(value))");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::Category::Enum:
|
|
|
|
{
|
|
|
|
size_t members = dynamic_cast<EnumType const&>(_type).numberOfMembers();
|
|
|
|
solAssert(members > 0, "empty enum should have caused a parser error.");
|
2020-10-12 14:01:45 +00:00
|
|
|
panicCode = PanicCode::EnumConversionError;
|
2019-04-09 13:30:54 +00:00
|
|
|
templ("condition", "lt(value, " + to_string(members) + ")");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::Category::InaccessibleDynamic:
|
|
|
|
templ("condition", "1");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
solAssert(false, "Validation of type " + _type.identifier() + " requested.");
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:01:45 +00:00
|
|
|
if (_revertOnFailure)
|
|
|
|
templ("failure", "revert(0, 0)");
|
|
|
|
else
|
|
|
|
templ("failure", panicFunction(panicCode) + "()");
|
|
|
|
|
2019-04-09 13:30:54 +00:00
|
|
|
return templ.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-02 20:33:47 +00:00
|
|
|
string YulUtilFunctions::packedHashFunction(
|
|
|
|
vector<Type const*> const& _givenTypes,
|
|
|
|
vector<Type const*> const& _targetTypes
|
|
|
|
)
|
|
|
|
{
|
|
|
|
string functionName = string("packed_hashed_");
|
|
|
|
for (auto const& t: _givenTypes)
|
|
|
|
functionName += t->identifier() + "_";
|
|
|
|
functionName += "_to_";
|
|
|
|
for (auto const& t: _targetTypes)
|
|
|
|
functionName += t->identifier() + "_";
|
|
|
|
size_t sizeOnStack = 0;
|
|
|
|
for (Type const* t: _givenTypes)
|
|
|
|
sizeOnStack += t->sizeOnStack();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-05-02 20:33:47 +00:00
|
|
|
Whiskers templ(R"(
|
|
|
|
function <functionName>(<variables>) -> hash {
|
2021-01-14 16:13:51 +00:00
|
|
|
let pos := <allocateUnbounded>()
|
2019-05-02 20:33:47 +00:00
|
|
|
let end := <packedEncode>(pos <comma> <variables>)
|
|
|
|
hash := keccak256(pos, sub(end, pos))
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
templ("functionName", functionName);
|
|
|
|
templ("variables", suffixedVariableNameList("var_", 1, 1 + sizeOnStack));
|
|
|
|
templ("comma", sizeOnStack > 0 ? "," : "");
|
2021-01-14 16:13:51 +00:00
|
|
|
templ("allocateUnbounded", allocateUnboundedFunction());
|
2020-01-22 14:48:56 +00:00
|
|
|
templ("packedEncode", ABIFunctions(m_evmVersion, m_revertStrings, m_functionCollector).tupleEncoderPacked(_givenTypes, _targetTypes));
|
2019-05-02 20:33:47 +00:00
|
|
|
return templ.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-06 17:48:31 +00:00
|
|
|
string YulUtilFunctions::forwardingRevertFunction()
|
|
|
|
{
|
|
|
|
bool forward = m_evmVersion.supportsReturndata();
|
|
|
|
string functionName = "revert_forward_" + to_string(forward);
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-05-06 17:48:31 +00:00
|
|
|
if (forward)
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>() {
|
2021-03-15 16:33:21 +00:00
|
|
|
let pos := <allocateUnbounded>()
|
|
|
|
returndatacopy(pos, 0, returndatasize())
|
|
|
|
revert(pos, returndatasize())
|
2019-05-06 17:48:31 +00:00
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2021-03-15 16:33:21 +00:00
|
|
|
("allocateUnbounded", allocateUnboundedFunction())
|
2019-05-06 17:48:31 +00:00
|
|
|
.render();
|
|
|
|
else
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>() {
|
|
|
|
revert(0, 0)
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-13 13:26:55 +00:00
|
|
|
std::string YulUtilFunctions::decrementCheckedFunction(Type const& _type)
|
|
|
|
{
|
2020-12-11 23:54:14 +00:00
|
|
|
solAssert(_type.category() == Type::Category::Integer, "");
|
2019-05-13 13:26:55 +00:00
|
|
|
IntegerType const& type = dynamic_cast<IntegerType const&>(_type);
|
|
|
|
|
|
|
|
string const functionName = "decrement_" + _type.identifier();
|
|
|
|
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-05-13 13:26:55 +00:00
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(value) -> ret {
|
2020-05-05 11:06:01 +00:00
|
|
|
value := <cleanupFunction>(value)
|
2020-07-22 08:28:04 +00:00
|
|
|
if eq(value, <minval>) { <panic>() }
|
2019-05-13 13:26:55 +00:00
|
|
|
ret := sub(value, 1)
|
|
|
|
}
|
|
|
|
)")
|
2020-05-05 11:06:01 +00:00
|
|
|
("functionName", functionName)
|
2020-10-12 14:01:45 +00:00
|
|
|
("panic", panicFunction(PanicCode::UnderOverflow))
|
2020-07-22 08:28:04 +00:00
|
|
|
("minval", toCompactHexWithPrefix(type.min()))
|
2020-05-05 11:06:01 +00:00
|
|
|
("cleanupFunction", cleanupFunction(_type))
|
|
|
|
.render();
|
2019-05-13 13:26:55 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-22 08:28:04 +00:00
|
|
|
std::string YulUtilFunctions::decrementWrappingFunction(Type const& _type)
|
2019-05-13 13:26:55 +00:00
|
|
|
{
|
2020-12-11 23:54:14 +00:00
|
|
|
solAssert(_type.category() == Type::Category::Integer, "");
|
2019-05-13 13:26:55 +00:00
|
|
|
IntegerType const& type = dynamic_cast<IntegerType const&>(_type);
|
|
|
|
|
2020-07-22 08:28:04 +00:00
|
|
|
string const functionName = "decrement_wrapping_" + _type.identifier();
|
2019-05-13 13:26:55 +00:00
|
|
|
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2020-07-22 08:28:04 +00:00
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(value) -> ret {
|
|
|
|
ret := <cleanupFunction>(sub(value, 1))
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("cleanupFunction", cleanupFunction(type))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
2019-05-13 13:26:55 +00:00
|
|
|
|
2020-07-22 08:28:04 +00:00
|
|
|
std::string YulUtilFunctions::incrementCheckedFunction(Type const& _type)
|
|
|
|
{
|
2020-12-11 23:54:14 +00:00
|
|
|
solAssert(_type.category() == Type::Category::Integer, "");
|
2020-07-22 08:28:04 +00:00
|
|
|
IntegerType const& type = dynamic_cast<IntegerType const&>(_type);
|
|
|
|
|
|
|
|
string const functionName = "increment_" + _type.identifier();
|
2019-05-13 13:26:55 +00:00
|
|
|
|
2020-07-22 08:28:04 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-05-13 13:26:55 +00:00
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(value) -> ret {
|
2020-05-05 11:06:01 +00:00
|
|
|
value := <cleanupFunction>(value)
|
2020-07-22 08:28:04 +00:00
|
|
|
if eq(value, <maxval>) { <panic>() }
|
2019-05-13 13:26:55 +00:00
|
|
|
ret := add(value, 1)
|
|
|
|
}
|
|
|
|
)")
|
2020-05-05 11:06:01 +00:00
|
|
|
("functionName", functionName)
|
2020-07-22 08:28:04 +00:00
|
|
|
("maxval", toCompactHexWithPrefix(type.max()))
|
2020-10-12 14:01:45 +00:00
|
|
|
("panic", panicFunction(PanicCode::UnderOverflow))
|
2020-05-05 11:06:01 +00:00
|
|
|
("cleanupFunction", cleanupFunction(_type))
|
|
|
|
.render();
|
2019-05-13 13:26:55 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-22 08:28:04 +00:00
|
|
|
std::string YulUtilFunctions::incrementWrappingFunction(Type const& _type)
|
|
|
|
{
|
2020-12-11 23:54:14 +00:00
|
|
|
solAssert(_type.category() == Type::Category::Integer, "");
|
2020-07-22 08:28:04 +00:00
|
|
|
IntegerType const& type = dynamic_cast<IntegerType const&>(_type);
|
|
|
|
|
|
|
|
string const functionName = "increment_wrapping_" + _type.identifier();
|
|
|
|
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(value) -> ret {
|
|
|
|
ret := <cleanupFunction>(add(value, 1))
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("cleanupFunction", cleanupFunction(type))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-16 16:59:29 +00:00
|
|
|
string YulUtilFunctions::negateNumberCheckedFunction(Type const& _type)
|
|
|
|
{
|
2020-12-11 23:54:14 +00:00
|
|
|
solAssert(_type.category() == Type::Category::Integer, "");
|
2019-05-16 16:59:29 +00:00
|
|
|
IntegerType const& type = dynamic_cast<IntegerType const&>(_type);
|
|
|
|
solAssert(type.isSigned(), "Expected signed type!");
|
|
|
|
|
|
|
|
string const functionName = "negate_" + _type.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-05-16 16:59:29 +00:00
|
|
|
return Whiskers(R"(
|
2020-05-05 11:06:01 +00:00
|
|
|
function <functionName>(value) -> ret {
|
|
|
|
value := <cleanupFunction>(value)
|
2020-07-22 08:28:04 +00:00
|
|
|
if eq(value, <minval>) { <panic>() }
|
2020-05-05 11:06:01 +00:00
|
|
|
ret := sub(0, value)
|
2019-05-16 16:59:29 +00:00
|
|
|
}
|
|
|
|
)")
|
2020-05-05 11:06:01 +00:00
|
|
|
("functionName", functionName)
|
2020-07-22 08:28:04 +00:00
|
|
|
("minval", toCompactHexWithPrefix(type.min()))
|
2020-05-05 11:06:01 +00:00
|
|
|
("cleanupFunction", cleanupFunction(_type))
|
2020-10-12 14:01:45 +00:00
|
|
|
("panic", panicFunction(PanicCode::UnderOverflow))
|
2020-05-05 11:06:01 +00:00
|
|
|
.render();
|
|
|
|
});
|
2019-05-16 16:59:29 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 08:28:04 +00:00
|
|
|
string YulUtilFunctions::negateNumberWrappingFunction(Type const& _type)
|
|
|
|
{
|
2020-12-11 23:54:14 +00:00
|
|
|
solAssert(_type.category() == Type::Category::Integer, "");
|
2020-07-22 08:28:04 +00:00
|
|
|
IntegerType const& type = dynamic_cast<IntegerType const&>(_type);
|
|
|
|
solAssert(type.isSigned(), "Expected signed type!");
|
|
|
|
|
2021-07-07 18:20:06 +00:00
|
|
|
string const functionName = "negate_wrapping_" + _type.identifier();
|
2020-07-22 08:28:04 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(value) -> ret {
|
2021-01-13 14:34:48 +00:00
|
|
|
ret := <cleanupFunction>(sub(0, value))
|
2020-07-22 08:28:04 +00:00
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("cleanupFunction", cleanupFunction(type))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:42:46 +00:00
|
|
|
string YulUtilFunctions::zeroValueFunction(Type const& _type, bool _splitFunctionTypes)
|
2019-05-22 10:25:00 +00:00
|
|
|
{
|
2020-03-02 20:42:46 +00:00
|
|
|
solAssert(_type.category() != Type::Category::Mapping, "");
|
2019-05-22 10:25:00 +00:00
|
|
|
|
2020-03-02 20:42:46 +00:00
|
|
|
string const functionName = "zero_value_for_" + string(_splitFunctionTypes ? "split_" : "") + _type.identifier();
|
2019-05-22 10:25:00 +00:00
|
|
|
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2020-03-02 20:42:46 +00:00
|
|
|
FunctionType const* fType = dynamic_cast<FunctionType const*>(&_type);
|
|
|
|
if (fType && fType->kind() == FunctionType::Kind::External && _splitFunctionTypes)
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>() -> retAddress, retFunction {
|
|
|
|
retAddress := 0
|
|
|
|
retFunction := 0
|
|
|
|
}
|
|
|
|
)")
|
2019-05-22 10:25:00 +00:00
|
|
|
("functionName", functionName)
|
|
|
|
.render();
|
2020-03-02 20:42:46 +00:00
|
|
|
|
2020-05-25 17:30:17 +00:00
|
|
|
if (_type.dataStoredIn(DataLocation::CallData))
|
|
|
|
{
|
|
|
|
solAssert(
|
|
|
|
_type.category() == Type::Category::Struct ||
|
|
|
|
_type.category() == Type::Category::Array,
|
|
|
|
"");
|
|
|
|
Whiskers templ(R"(
|
|
|
|
function <functionName>() -> offset<?hasLength>, length</hasLength> {
|
|
|
|
offset := calldatasize()
|
|
|
|
<?hasLength> length := 0 </hasLength>
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
templ("functionName", functionName);
|
|
|
|
templ("hasLength",
|
|
|
|
_type.category() == Type::Category::Array &&
|
|
|
|
dynamic_cast<ArrayType const&>(_type).isDynamicallySized()
|
|
|
|
);
|
|
|
|
|
|
|
|
return templ.render();
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:42:46 +00:00
|
|
|
Whiskers templ(R"(
|
|
|
|
function <functionName>() -> ret {
|
|
|
|
ret := <zeroValue>
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
templ("functionName", functionName);
|
|
|
|
|
|
|
|
if (_type.isValueType())
|
|
|
|
{
|
|
|
|
solAssert((
|
|
|
|
_type.hasSimpleZeroValueInMemory() ||
|
|
|
|
(fType && (fType->kind() == FunctionType::Kind::Internal || fType->kind() == FunctionType::Kind::External))
|
|
|
|
), "");
|
|
|
|
templ("zeroValue", "0");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
solAssert(_type.dataStoredIn(DataLocation::Memory), "");
|
|
|
|
if (auto const* arrayType = dynamic_cast<ArrayType const*>(&_type))
|
|
|
|
{
|
|
|
|
if (_type.isDynamicallySized())
|
|
|
|
templ("zeroValue", to_string(CompilerUtils::zeroPointer));
|
|
|
|
else
|
|
|
|
templ("zeroValue", allocateAndInitializeMemoryArrayFunction(*arrayType) + "(" + to_string(unsigned(arrayType->length())) + ")");
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (auto const* structType = dynamic_cast<StructType const*>(&_type))
|
|
|
|
templ("zeroValue", allocateAndInitializeMemoryStructFunction(*structType) + "()");
|
|
|
|
else
|
2021-09-23 15:18:13 +00:00
|
|
|
solUnimplemented("");
|
2020-03-02 20:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return templ.render();
|
|
|
|
});
|
2019-05-22 10:25:00 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:41:31 +00:00
|
|
|
string YulUtilFunctions::storageSetToZeroFunction(Type const& _type)
|
|
|
|
{
|
|
|
|
string const functionName = "storage_set_to_zero_" + _type.identifier();
|
|
|
|
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2019-07-10 10:41:31 +00:00
|
|
|
if (_type.isValueType())
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(slot, offset) {
|
2021-02-24 10:48:55 +00:00
|
|
|
let <values> := <zeroValue>()
|
|
|
|
<store>(slot, offset, <values>)
|
2019-07-10 10:41:31 +00:00
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2020-09-01 12:43:40 +00:00
|
|
|
("store", updateStorageValueFunction(_type, _type))
|
2021-02-24 10:48:55 +00:00
|
|
|
("values", suffixedVariableNameList("zero_", 0, _type.sizeOnStack()))
|
2019-07-10 10:41:31 +00:00
|
|
|
("zeroValue", zeroValueFunction(_type))
|
|
|
|
.render();
|
|
|
|
else if (_type.category() == Type::Category::Array)
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(slot, offset) {
|
2020-10-06 12:08:00 +00:00
|
|
|
if iszero(eq(offset, 0)) { <panic>() }
|
2019-07-10 10:41:31 +00:00
|
|
|
<clearArray>(slot)
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("clearArray", clearStorageArrayFunction(dynamic_cast<ArrayType const&>(_type)))
|
2020-10-12 14:01:45 +00:00
|
|
|
("panic", panicFunction(PanicCode::Generic))
|
2019-07-10 10:41:31 +00:00
|
|
|
.render();
|
2020-09-18 10:37:38 +00:00
|
|
|
else if (_type.category() == Type::Category::Struct)
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(slot, offset) {
|
2020-10-06 12:08:00 +00:00
|
|
|
if iszero(eq(offset, 0)) { <panic>() }
|
2020-09-18 10:37:38 +00:00
|
|
|
<clearStruct>(slot)
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("clearStruct", clearStorageStructFunction(dynamic_cast<StructType const&>(_type)))
|
2020-10-12 14:01:45 +00:00
|
|
|
("panic", panicFunction(PanicCode::Generic))
|
2019-07-10 10:41:31 +00:00
|
|
|
.render();
|
|
|
|
else
|
|
|
|
solUnimplemented("setToZero for type " + _type.identifier() + " not yet implemented!");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-02 14:24:54 +00:00
|
|
|
string YulUtilFunctions::conversionFunctionSpecial(Type const& _from, Type const& _to)
|
|
|
|
{
|
|
|
|
string functionName =
|
|
|
|
"convert_" +
|
|
|
|
_from.identifier() +
|
|
|
|
"_to_" +
|
|
|
|
_to.identifier();
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2020-02-06 13:05:25 +00:00
|
|
|
if (
|
|
|
|
auto fromTuple = dynamic_cast<TupleType const*>(&_from), toTuple = dynamic_cast<TupleType const*>(&_to);
|
|
|
|
fromTuple && toTuple && fromTuple->components().size() == toTuple->components().size()
|
|
|
|
)
|
|
|
|
{
|
|
|
|
size_t sourceStackSize = 0;
|
|
|
|
size_t destStackSize = 0;
|
|
|
|
std::string conversions;
|
|
|
|
for (size_t i = 0; i < fromTuple->components().size(); ++i)
|
|
|
|
{
|
|
|
|
auto fromComponent = fromTuple->components()[i];
|
|
|
|
auto toComponent = toTuple->components()[i];
|
|
|
|
solAssert(fromComponent, "");
|
|
|
|
if (toComponent)
|
|
|
|
{
|
|
|
|
conversions +=
|
|
|
|
suffixedVariableNameList("converted", destStackSize, destStackSize + toComponent->sizeOnStack()) +
|
2020-04-15 07:41:36 +00:00
|
|
|
(toComponent->sizeOnStack() > 0 ? " := " : "") +
|
2020-02-06 13:05:25 +00:00
|
|
|
conversionFunction(*fromComponent, *toComponent) +
|
|
|
|
"(" +
|
|
|
|
suffixedVariableNameList("value", sourceStackSize, sourceStackSize + fromComponent->sizeOnStack()) +
|
|
|
|
")\n";
|
|
|
|
destStackSize += toComponent->sizeOnStack();
|
|
|
|
}
|
|
|
|
sourceStackSize += fromComponent->sizeOnStack();
|
|
|
|
}
|
|
|
|
return Whiskers(R"(
|
2020-04-15 07:41:36 +00:00
|
|
|
function <functionName>(<values>) <arrow> <converted> {
|
2020-02-06 13:05:25 +00:00
|
|
|
<conversions>
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("values", suffixedVariableNameList("value", 0, sourceStackSize))
|
2020-04-15 07:41:36 +00:00
|
|
|
("arrow", destStackSize > 0 ? "->" : "")
|
2020-02-06 13:05:25 +00:00
|
|
|
("converted", suffixedVariableNameList("converted", 0, destStackSize))
|
|
|
|
("conversions", conversions)
|
|
|
|
.render();
|
|
|
|
}
|
|
|
|
|
2019-05-02 14:24:54 +00:00
|
|
|
solUnimplementedAssert(
|
|
|
|
_from.category() == Type::Category::StringLiteral,
|
|
|
|
"Type conversion " + _from.toString() + " -> " + _to.toString() + " not yet implemented."
|
|
|
|
);
|
|
|
|
string const& data = dynamic_cast<StringLiteralType const&>(_from).value();
|
|
|
|
if (_to.category() == Type::Category::FixedBytes)
|
|
|
|
{
|
|
|
|
unsigned const numBytes = dynamic_cast<FixedBytesType const&>(_to).numBytes();
|
|
|
|
solAssert(data.size() <= 32, "");
|
|
|
|
Whiskers templ(R"(
|
|
|
|
function <functionName>() -> converted {
|
|
|
|
converted := <data>
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
templ("functionName", functionName);
|
|
|
|
templ("data", formatNumber(
|
|
|
|
h256::Arith(h256(data, h256::AlignLeft)) &
|
|
|
|
(~(u256(-1) >> (8 * numBytes)))
|
|
|
|
));
|
|
|
|
return templ.render();
|
|
|
|
}
|
|
|
|
else if (_to.category() == Type::Category::Array)
|
|
|
|
{
|
2022-01-28 17:57:05 +00:00
|
|
|
solAssert(dynamic_cast<ArrayType const&>(_to).isByteArrayOrString(), "");
|
2019-05-02 14:24:54 +00:00
|
|
|
Whiskers templ(R"(
|
|
|
|
function <functionName>() -> converted {
|
2020-12-22 14:48:31 +00:00
|
|
|
converted := <copyLiteralToMemory>()
|
2019-05-02 14:24:54 +00:00
|
|
|
}
|
|
|
|
)");
|
|
|
|
templ("functionName", functionName);
|
2020-12-22 14:48:31 +00:00
|
|
|
templ("copyLiteralToMemory", copyLiteralToMemoryFunction(data));
|
2019-05-02 14:24:54 +00:00
|
|
|
return templ.render();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
solAssert(
|
|
|
|
false,
|
|
|
|
"Invalid conversion from string literal to " + _to.toString() + " requested."
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2019-06-27 11:36:06 +00:00
|
|
|
|
|
|
|
string YulUtilFunctions::readFromMemoryOrCalldata(Type const& _type, bool _fromCalldata)
|
|
|
|
{
|
|
|
|
string functionName =
|
|
|
|
string("read_from_") +
|
|
|
|
(_fromCalldata ? "calldata" : "memory") +
|
|
|
|
_type.identifier();
|
|
|
|
|
|
|
|
// TODO use ABI functions for handling calldata
|
|
|
|
if (_fromCalldata)
|
|
|
|
solAssert(!_type.isDynamicallyEncoded(), "");
|
|
|
|
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&] {
|
2019-06-27 11:36:06 +00:00
|
|
|
if (auto refType = dynamic_cast<ReferenceType const*>(&_type))
|
|
|
|
{
|
|
|
|
solAssert(refType->sizeOnStack() == 1, "");
|
|
|
|
solAssert(!_fromCalldata, "");
|
|
|
|
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(memPtr) -> value {
|
|
|
|
value := mload(memPtr)
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
solAssert(_type.isValueType(), "");
|
2020-11-03 12:27:18 +00:00
|
|
|
Whiskers templ(R"(
|
|
|
|
function <functionName>(ptr) -> <returnVariables> {
|
2020-05-04 12:21:48 +00:00
|
|
|
<?fromCalldata>
|
2020-11-03 12:27:18 +00:00
|
|
|
let value := calldataload(ptr)
|
2020-02-11 16:43:43 +00:00
|
|
|
<validate>(value)
|
2020-05-04 12:21:48 +00:00
|
|
|
<!fromCalldata>
|
2020-11-03 12:27:18 +00:00
|
|
|
let value := <cleanup>(mload(ptr))
|
2020-05-04 12:21:48 +00:00
|
|
|
</fromCalldata>
|
2020-11-03 12:27:18 +00:00
|
|
|
|
|
|
|
<returnVariables> :=
|
|
|
|
<?externalFunction>
|
|
|
|
<splitFunction>(value)
|
|
|
|
<!externalFunction>
|
|
|
|
value
|
|
|
|
</externalFunction>
|
2019-06-27 11:36:06 +00:00
|
|
|
}
|
2020-11-03 12:27:18 +00:00
|
|
|
)");
|
|
|
|
templ("functionName", functionName);
|
|
|
|
templ("fromCalldata", _fromCalldata);
|
|
|
|
if (_fromCalldata)
|
|
|
|
templ("validate", validatorFunction(_type, true));
|
|
|
|
auto const* funType = dynamic_cast<FunctionType const*>(&_type);
|
|
|
|
if (funType && funType->kind() == FunctionType::Kind::External)
|
|
|
|
{
|
|
|
|
templ("externalFunction", true);
|
|
|
|
templ("splitFunction", splitExternalFunctionIdFunction());
|
|
|
|
templ("returnVariables", "addr, selector");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
templ("externalFunction", false);
|
|
|
|
templ("returnVariables", "returnValue");
|
|
|
|
}
|
|
|
|
|
2020-05-04 12:21:48 +00:00
|
|
|
// Byte array elements generally need cleanup.
|
|
|
|
// Other types are cleaned as well to account for dirty memory e.g. due to inline assembly.
|
2020-11-03 12:27:18 +00:00
|
|
|
templ("cleanup", cleanupFunction(_type));
|
|
|
|
return templ.render();
|
2019-06-27 11:36:06 +00:00
|
|
|
});
|
|
|
|
}
|
2020-01-22 14:48:56 +00:00
|
|
|
|
2021-03-15 15:45:00 +00:00
|
|
|
string YulUtilFunctions::revertReasonIfDebugFunction(string const& _message)
|
2020-01-22 14:48:56 +00:00
|
|
|
{
|
2021-03-15 15:45:00 +00:00
|
|
|
string functionName = "revert_error_" + util::toHex(util::keccak256(_message).asBytes());
|
|
|
|
return m_functionCollector.createFunction(functionName, [&](auto&, auto&) -> string {
|
|
|
|
return revertReasonIfDebugBody(m_revertStrings, allocateUnboundedFunction() + "()", _message);
|
|
|
|
});
|
2020-01-22 14:48:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 15:45:00 +00:00
|
|
|
string YulUtilFunctions::revertReasonIfDebugBody(
|
|
|
|
RevertStrings _revertStrings,
|
|
|
|
string const& _allocation,
|
|
|
|
string const& _message
|
|
|
|
)
|
2020-01-22 14:48:56 +00:00
|
|
|
{
|
2021-03-15 15:45:00 +00:00
|
|
|
if (_revertStrings < RevertStrings::Debug || _message.empty())
|
|
|
|
return "revert(0, 0)";
|
|
|
|
|
|
|
|
Whiskers templ(R"(
|
|
|
|
let start := <allocate>
|
|
|
|
let pos := start
|
|
|
|
mstore(pos, <sig>)
|
|
|
|
pos := add(pos, 4)
|
|
|
|
mstore(pos, 0x20)
|
|
|
|
pos := add(pos, 0x20)
|
|
|
|
mstore(pos, <length>)
|
|
|
|
pos := add(pos, 0x20)
|
|
|
|
<#word>
|
|
|
|
mstore(add(pos, <offset>), <wordValue>)
|
|
|
|
</word>
|
|
|
|
revert(start, <overallLength>)
|
|
|
|
)");
|
|
|
|
templ("allocate", _allocation);
|
2022-09-27 12:13:44 +00:00
|
|
|
templ("sig", util::selectorFromSignatureU256("Error(string)").str());
|
2021-03-15 15:45:00 +00:00
|
|
|
templ("length", to_string(_message.length()));
|
|
|
|
|
|
|
|
size_t words = (_message.length() + 31) / 32;
|
|
|
|
vector<map<string, string>> wordParams(words);
|
|
|
|
for (size_t i = 0; i < words; ++i)
|
|
|
|
{
|
|
|
|
wordParams[i]["offset"] = to_string(i * 32);
|
|
|
|
wordParams[i]["wordValue"] = formatAsStringOrNumber(_message.substr(32 * i, 32));
|
|
|
|
}
|
|
|
|
templ("word", wordParams);
|
|
|
|
templ("overallLength", to_string(4 + 0x20 + 0x20 + words * 32));
|
|
|
|
|
|
|
|
return templ.render();
|
2020-01-22 14:48:56 +00:00
|
|
|
}
|
2020-02-10 10:58:36 +00:00
|
|
|
|
2020-10-12 14:01:45 +00:00
|
|
|
string YulUtilFunctions::panicFunction(util::PanicCode _code)
|
2020-09-15 16:57:59 +00:00
|
|
|
{
|
2020-10-12 14:01:45 +00:00
|
|
|
string functionName = "panic_error_" + toCompactHexWithPrefix(uint64_t(_code));
|
2020-09-15 16:57:59 +00:00
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>() {
|
2020-10-12 14:01:45 +00:00
|
|
|
mstore(0, <selector>)
|
|
|
|
mstore(4, <code>)
|
|
|
|
revert(0, 0x24)
|
2020-09-15 16:57:59 +00:00
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2022-09-27 12:13:44 +00:00
|
|
|
("selector", util::selectorFromSignatureU256("Panic(uint256)").str())
|
2021-05-15 22:01:48 +00:00
|
|
|
("code", toCompactHexWithPrefix(static_cast<unsigned>(_code)))
|
2020-09-15 16:57:59 +00:00
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-20 13:30:46 +00:00
|
|
|
string YulUtilFunctions::returnDataSelectorFunction()
|
|
|
|
{
|
|
|
|
string const functionName = "return_data_selector";
|
|
|
|
solAssert(m_evmVersion.supportsReturndata(), "");
|
|
|
|
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return util::Whiskers(R"(
|
|
|
|
function <functionName>() -> sig {
|
|
|
|
if gt(returndatasize(), 3) {
|
|
|
|
returndatacopy(0, 0, 4)
|
|
|
|
sig := <shr224>(mload(0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("shr224", shiftRightFunction(224))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-10 10:58:36 +00:00
|
|
|
string YulUtilFunctions::tryDecodeErrorMessageFunction()
|
|
|
|
{
|
|
|
|
string const functionName = "try_decode_error_message";
|
2020-10-20 13:30:46 +00:00
|
|
|
solAssert(m_evmVersion.supportsReturndata(), "");
|
2020-02-10 10:58:36 +00:00
|
|
|
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return util::Whiskers(R"(
|
|
|
|
function <functionName>() -> ret {
|
|
|
|
if lt(returndatasize(), 0x44) { leave }
|
|
|
|
|
2021-01-14 16:13:51 +00:00
|
|
|
let data := <allocateUnbounded>()
|
2020-02-10 10:58:36 +00:00
|
|
|
returndatacopy(data, 4, sub(returndatasize(), 4))
|
|
|
|
|
|
|
|
let offset := mload(data)
|
|
|
|
if or(
|
|
|
|
gt(offset, 0xffffffffffffffff),
|
|
|
|
gt(add(offset, 0x24), returndatasize())
|
|
|
|
) {
|
|
|
|
leave
|
|
|
|
}
|
|
|
|
|
|
|
|
let msg := add(data, offset)
|
|
|
|
let length := mload(msg)
|
|
|
|
if gt(length, 0xffffffffffffffff) { leave }
|
|
|
|
|
|
|
|
let end := add(add(msg, 0x20), length)
|
2020-12-22 10:05:02 +00:00
|
|
|
if gt(end, add(data, sub(returndatasize(), 4))) { leave }
|
2020-02-10 10:58:36 +00:00
|
|
|
|
2021-01-14 16:13:51 +00:00
|
|
|
<finalizeAllocation>(data, add(offset, add(0x20, length)))
|
2020-02-10 10:58:36 +00:00
|
|
|
ret := msg
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
2021-01-14 16:13:51 +00:00
|
|
|
("allocateUnbounded", allocateUnboundedFunction())
|
|
|
|
("finalizeAllocation", finalizeAllocationFunction())
|
2020-02-10 10:58:36 +00:00
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-20 13:30:46 +00:00
|
|
|
string YulUtilFunctions::tryDecodePanicDataFunction()
|
|
|
|
{
|
|
|
|
string const functionName = "try_decode_panic_data";
|
|
|
|
solAssert(m_evmVersion.supportsReturndata(), "");
|
|
|
|
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return util::Whiskers(R"(
|
|
|
|
function <functionName>() -> success, data {
|
|
|
|
if gt(returndatasize(), 0x23) {
|
|
|
|
returndatacopy(0, 4, 0x20)
|
|
|
|
success := 1
|
|
|
|
data := mload(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-10 10:58:36 +00:00
|
|
|
string YulUtilFunctions::extractReturndataFunction()
|
|
|
|
{
|
|
|
|
string const functionName = "extract_returndata";
|
|
|
|
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return util::Whiskers(R"(
|
|
|
|
function <functionName>() -> data {
|
|
|
|
<?supportsReturndata>
|
|
|
|
switch returndatasize()
|
|
|
|
case 0 {
|
|
|
|
data := <emptyArray>()
|
|
|
|
}
|
|
|
|
default {
|
2021-01-14 18:39:02 +00:00
|
|
|
data := <allocateArray>(returndatasize())
|
2020-02-10 10:58:36 +00:00
|
|
|
returndatacopy(add(data, 0x20), 0, returndatasize())
|
|
|
|
}
|
|
|
|
<!supportsReturndata>
|
|
|
|
data := <emptyArray>()
|
|
|
|
</supportsReturndata>
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("supportsReturndata", m_evmVersion.supportsReturndata())
|
2021-01-14 18:39:02 +00:00
|
|
|
("allocateArray", allocateMemoryArrayFunction(*TypeProvider::bytesMemory()))
|
2020-02-10 10:58:36 +00:00
|
|
|
("emptyArray", zeroValueFunction(*TypeProvider::bytesMemory()))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-06 15:26:59 +00:00
|
|
|
string YulUtilFunctions::copyConstructorArgumentsToMemoryFunction(
|
|
|
|
ContractDefinition const& _contract,
|
|
|
|
string const& _creationObjectName
|
|
|
|
)
|
|
|
|
{
|
|
|
|
string functionName = "copy_arguments_for_constructor_" +
|
|
|
|
toString(_contract.constructor()->id()) +
|
|
|
|
"_object_" +
|
|
|
|
_contract.name() +
|
|
|
|
"_" +
|
|
|
|
toString(_contract.id());
|
|
|
|
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
2021-02-12 16:40:00 +00:00
|
|
|
string returnParams = suffixedVariableNameList("ret_param_",0, CompilerUtils::sizeOnStack(_contract.constructor()->parameters()));
|
2020-04-06 15:26:59 +00:00
|
|
|
ABIFunctions abiFunctions(m_evmVersion, m_revertStrings, m_functionCollector);
|
|
|
|
|
|
|
|
return util::Whiskers(R"(
|
|
|
|
function <functionName>() -> <retParams> {
|
|
|
|
let programSize := datasize("<object>")
|
|
|
|
let argSize := sub(codesize(), programSize)
|
|
|
|
|
|
|
|
let memoryDataOffset := <allocate>(argSize)
|
|
|
|
codecopy(memoryDataOffset, programSize, argSize)
|
|
|
|
|
|
|
|
<retParams> := <abiDecode>(memoryDataOffset, add(memoryDataOffset, argSize))
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("retParams", returnParams)
|
|
|
|
("object", _creationObjectName)
|
|
|
|
("allocate", allocationFunction())
|
|
|
|
("abiDecode", abiFunctions.tupleDecoder(FunctionType(*_contract.constructor()).parameterTypes(), true))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
2020-12-10 13:37:26 +00:00
|
|
|
|
|
|
|
string YulUtilFunctions::externalCodeFunction()
|
|
|
|
{
|
|
|
|
string functionName = "external_code_at";
|
|
|
|
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return util::Whiskers(R"(
|
|
|
|
function <functionName>(addr) -> mpos {
|
|
|
|
let length := extcodesize(addr)
|
|
|
|
mpos := <allocateArray>(length)
|
|
|
|
extcodecopy(addr, add(mpos, 0x20), 0, length)
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("allocateArray", allocateMemoryArrayFunction(*TypeProvider::bytesMemory()))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|
2021-12-16 11:35:39 +00:00
|
|
|
|
|
|
|
std::string YulUtilFunctions::externalFunctionPointersEqualFunction()
|
|
|
|
{
|
|
|
|
std::string const functionName = "externalFunctionPointersEqualFunction";
|
|
|
|
return m_functionCollector.createFunction(functionName, [&]() {
|
|
|
|
return util::Whiskers(R"(
|
|
|
|
function <functionName>(
|
|
|
|
leftAddress,
|
|
|
|
leftSelector,
|
|
|
|
rightAddress,
|
|
|
|
rightSelector
|
|
|
|
) -> result {
|
|
|
|
result := and(
|
|
|
|
eq(
|
|
|
|
<addressCleanUpFunction>(leftAddress), <addressCleanUpFunction>(rightAddress)
|
|
|
|
),
|
|
|
|
eq(
|
|
|
|
<selectorCleanUpFunction>(leftSelector), <selectorCleanUpFunction>(rightSelector)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("addressCleanUpFunction", cleanupFunction(*TypeProvider::address()))
|
|
|
|
("selectorCleanUpFunction", cleanupFunction(*TypeProvider::uint(32)))
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
}
|