Merge pull request #10579 from ethereum/supportMetaTypeNameSol2Yul

[Sol->Yul] Implementing type name.
This commit is contained in:
Đorđe Mijović 2020-12-30 18:46:24 +01:00 committed by GitHub
commit 0e32fa8209
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 154 additions and 85 deletions

View File

@ -976,30 +976,20 @@ string ABIFunctions::abiEncodingFunctionStringLiteral(
Whiskers templ(R"(
function <functionName>(pos) -> end {
pos := <storeLength>(pos, <length>)
<#word>
mstore(add(pos, <offset>), <wordValue>)
</word>
<storeLiteralInMemory>(pos)
end := add(pos, <overallSize>)
}
)");
templ("functionName", functionName);
// TODO this can make use of CODECOPY for large strings once we have that in Yul
size_t words = (value.size() + 31) / 32;
templ("length", to_string(value.size()));
templ("storeLength", arrayStoreLengthForEncodingFunction(dynamic_cast<ArrayType const&>(_to), _options));
if (_options.padded)
templ("overallSize", to_string(words * 32));
templ("overallSize", to_string(((value.size() + 31) / 32) * 32));
else
templ("overallSize", to_string(value.size()));
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(value.substr(32 * i, 32));
}
templ("word", wordParams);
templ("storeLiteralInMemory", m_utils.storeLiteralInMemoryFunction(value));
return templ.render();
}
else

View File

@ -107,6 +107,51 @@ string YulUtilFunctions::copyToMemoryFunction(bool _fromCalldata)
});
}
string YulUtilFunctions::copyLiteralToMemoryFunction(string const& _literal)
{
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());
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"(
function <functionName>(memPtr) {
<#word>
mstore(add(memPtr, <offset>), <wordValue>)
</word>
}
)")
("functionName", functionName)
("word", wordParams)
.render();
});
}
string YulUtilFunctions::requireOrAssertFunction(bool _assert, Type const* _messageType)
{
string functionName =
@ -3901,31 +3946,14 @@ string YulUtilFunctions::conversionFunctionSpecial(Type const& _from, Type const
}
else if (_to.category() == Type::Category::Array)
{
auto const& arrayType = dynamic_cast<ArrayType const&>(_to);
solAssert(arrayType.isByteArray(), "");
size_t words = (data.size() + 31) / 32;
size_t storageSize = 32 + words * 32;
solAssert(dynamic_cast<ArrayType const&>(_to).isByteArray(), "");
Whiskers templ(R"(
function <functionName>() -> converted {
converted := <allocate>(<storageSize>)
mstore(converted, <size>)
<#word>
mstore(add(converted, <offset>), <wordValue>)
</word>
converted := <copyLiteralToMemory>()
}
)");
templ("functionName", functionName);
templ("allocate", allocationFunction());
templ("storageSize", to_string(storageSize));
templ("size", to_string(data.size()));
vector<map<string, string>> wordParams(words);
for (size_t i = 0; i < words; ++i)
{
wordParams[i]["offset"] = to_string(32 + i * 32);
wordParams[i]["wordValue"] = formatAsStringOrNumber(data.substr(32 * i, 32));
}
templ("word", wordParams);
templ("copyLiteralToMemory", copyLiteralToMemoryFunction(data));
return templ.render();
}
else

View File

@ -72,6 +72,15 @@ public:
/// Pads with zeros and might write more than exactly length.
std::string copyToMemoryFunction(bool _fromCalldata);
/// @returns the name of a function that copies a string literal to memory
/// and returns a pointer to the memory area containing the string literal.
/// signature: () -> memPtr
std::string copyLiteralToMemoryFunction(std::string const& _literal);
/// @returns the name of a function that stores a string literal at a specific location in memory
/// signature: (memPtr) ->
std::string storeLiteralInMemoryFunction(std::string const& _literal);
// @returns the name of a function that has the equivalent logic of an
// `assert` or `require` call.
std::string requireOrAssertFunction(bool _assert, Type const* _messageType = nullptr);

View File

@ -1737,7 +1737,9 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess)
}
else if (member == "name")
{
solUnimplementedAssert(false, "");
TypePointer arg = dynamic_cast<MagicType const&>(*_memberAccess.expression().annotation().type).typeArgument();
ContractDefinition const& contract = dynamic_cast<ContractType const&>(*arg).contractDefinition();
define(IRVariable(_memberAccess)) << m_utils.copyLiteralToMemoryFunction(contract.name()) << "()\n";
}
else if (member == "interfaceId")
{

View File

@ -188,14 +188,14 @@ def examine_id_coverage(top_dir, source_id_to_file_names, new_ids_only=False):
# Warning (1878): SPDX license identifier not provided in source file. ....
# Warning (3420): Source file does not specify required compiler version!
test_ids |= find_ids_in_cmdline_test_err(path.join(top_dir, "test", "cmdlineTests", "error_codes", "err"))
test_ids |= find_ids_in_cmdline_test_err(path.join(top_dir, "test", "cmdlineTests", "yul_unimplemented", "err"))
# white list of ids which are not covered by tests
white_ids = {
"3805", # "This is a pre-release compiler version, please do not use it in production."
# The warning may or may not exist in a compiler build.
"4591" # "There are more than 256 warnings. Ignoring the rest."
"4591", # "There are more than 256 warnings. Ignoring the rest."
# Due to 3805, the warning lists look different for different compiler builds.
"1834" # Unimplemented feature error, as we do not test it anymore via cmdLineTests
}
assert len(test_ids & white_ids) == 0, "The sets are not supposed to intersect"
test_ids |= white_ids

View File

@ -87,18 +87,18 @@ object "C_59" {
if or(gt(newFreePtr, 0xffffffffffffffff), lt(newFreePtr, memPtr)) { panic_error_0x41() }
mstore(64, newFreePtr)
}
function convert_t_stringliteral_6490_to_t_string() -> converted
function copy_literal_to_memory_64902fd228f7ef267f3b474dd6ef84bae434cf5546eee948e7ca26df3eda1927() -> memPtr
{
let memPtr := mload(64)
let newFreePtr := add(memPtr, 160)
if or(gt(newFreePtr, 0xffffffffffffffff), lt(newFreePtr, memPtr)) { panic_error_0x41() }
let memPtr_1 := mload(64)
let newFreePtr := add(memPtr_1, 160)
if or(gt(newFreePtr, 0xffffffffffffffff), lt(newFreePtr, memPtr_1)) { panic_error_0x41() }
mstore(64, newFreePtr)
converted := memPtr
mstore(memPtr, 100)
mstore(add(memPtr, 32), "longstringlongstringlongstringlo")
mstore(add(memPtr, 64), "ngstringlongstringlongstringlong")
mstore(add(memPtr, 96), "stringlongstringlongstringlongst")
mstore(add(memPtr, 128), "ring")
mstore(memPtr_1, 100)
memPtr := memPtr_1
mstore(add(memPtr_1, 0x20), "longstringlongstringlongstringlo")
mstore(add(memPtr_1, 64), "ngstringlongstringlongstringlong")
mstore(add(memPtr_1, 96), "stringlongstringlongstringlongst")
mstore(add(memPtr_1, 128), "ring")
}
function extract_from_storage_value_dynamict_uint256(slot_value, offset) -> value
{
@ -120,7 +120,7 @@ object "C_59" {
sstore(slot, or(and(_5, not(mask)), and(shl(shiftBits, _4), mask)))
let _6, _7 := storage_array_index_access$_t_struct$_S_storage(0x02, vloc)
vloc := extract_from_storage_value_dynamict_uint256(sload(_6), _7)
vloc__27_mpos := convert_t_stringliteral_6490_to_t_string()
vloc__27_mpos := copy_literal_to_memory_64902fd228f7ef267f3b474dd6ef84bae434cf5546eee948e7ca26df3eda1927()
}
function panic_error_0x32()
{

View File

@ -75,6 +75,26 @@ object \"C_11\" {
mstore(64, newFreePtr)
}
function allocate_memory_array_t_string_memory_ptr(length) -> memPtr {
let allocSize := array_allocation_size_t_string_memory_ptr(length)
memPtr := allocateMemory(allocSize)
mstore(memPtr, length)
}
function array_allocation_size_t_string_memory_ptr(length) -> size {
// Make sure we can allocate memory without overflow
if gt(length, 0xffffffffffffffff) { panic_error_0x41() }
// round up
size := and(add(length, 0x1f), not(0x1f))
// add length slot
size := add(size, 0x20)
}
function array_length_t_string_memory_ptr(value) -> length {
length := mload(value)
@ -87,11 +107,12 @@ object \"C_11\" {
}
function convert_t_stringliteral_9f0adad0a59b05d2e04a1373342b10b9eb16c57c164c8a3bfcbf46dccee39a21_to_t_string_memory_ptr() -> converted {
converted := allocateMemory(64)
mstore(converted, 6)
mstore(add(converted, 32), \"abcabc\")
converted := copy_literal_to_memory_9f0adad0a59b05d2e04a1373342b10b9eb16c57c164c8a3bfcbf46dccee39a21()
}
function copy_literal_to_memory_9f0adad0a59b05d2e04a1373342b10b9eb16c57c164c8a3bfcbf46dccee39a21() -> memPtr {
memPtr := allocate_memory_array_t_string_memory_ptr(6)
store_literal_in_memory_9f0adad0a59b05d2e04a1373342b10b9eb16c57c164c8a3bfcbf46dccee39a21(add(memPtr, 32))
}
function copy_memory_to_memory(src, dst, length) {
@ -133,6 +154,12 @@ object \"C_11\" {
}
function store_literal_in_memory_9f0adad0a59b05d2e04a1373342b10b9eb16c57c164c8a3bfcbf46dccee39a21(memPtr) {
mstore(add(memPtr, 0), \"abcabc\")
}
function zero_value_for_split_t_string_memory_ptr() -> ret {
ret := 96
}

View File

@ -75,6 +75,26 @@ object \"C_11\" {
mstore(64, newFreePtr)
}
function allocate_memory_array_t_string_memory_ptr(length) -> memPtr {
let allocSize := array_allocation_size_t_string_memory_ptr(length)
memPtr := allocateMemory(allocSize)
mstore(memPtr, length)
}
function array_allocation_size_t_string_memory_ptr(length) -> size {
// Make sure we can allocate memory without overflow
if gt(length, 0xffffffffffffffff) { panic_error_0x41() }
// round up
size := and(add(length, 0x1f), not(0x1f))
// add length slot
size := add(size, 0x20)
}
function array_length_t_string_memory_ptr(value) -> length {
length := mload(value)
@ -87,15 +107,12 @@ object \"C_11\" {
}
function convert_t_stringliteral_d6604f85ac07e2b33103a620b3d3d75b0473c7214912beded67b9b624d41c571_to_t_string_memory_ptr() -> converted {
converted := allocateMemory(128)
mstore(converted, 85)
mstore(add(converted, 32), \"abcdabcdcafecafeabcdabcdcafecafe\")
mstore(add(converted, 64), \"ffffzzzzoooo0123456789,.<,>.?:;'\")
mstore(add(converted, 96), \"[{]}|`~!@#$%^&*()-_=+\")
converted := copy_literal_to_memory_d6604f85ac07e2b33103a620b3d3d75b0473c7214912beded67b9b624d41c571()
}
function copy_literal_to_memory_d6604f85ac07e2b33103a620b3d3d75b0473c7214912beded67b9b624d41c571() -> memPtr {
memPtr := allocate_memory_array_t_string_memory_ptr(85)
store_literal_in_memory_d6604f85ac07e2b33103a620b3d3d75b0473c7214912beded67b9b624d41c571(add(memPtr, 32))
}
function copy_memory_to_memory(src, dst, length) {
@ -137,6 +154,16 @@ object \"C_11\" {
}
function store_literal_in_memory_d6604f85ac07e2b33103a620b3d3d75b0473c7214912beded67b9b624d41c571(memPtr) {
mstore(add(memPtr, 0), \"abcdabcdcafecafeabcdabcdcafecafe\")
mstore(add(memPtr, 32), \"ffffzzzzoooo0123456789,.<,>.?:;'\")
mstore(add(memPtr, 64), \"[{]}|`~!@#$%^&*()-_=+\")
}
function zero_value_for_split_t_string_memory_ptr() -> ret {
ret := 96
}

View File

@ -1 +0,0 @@
--ir --error-codes

View File

@ -1,5 +0,0 @@
Error (1834): Unimplemented feature error in <FILENAME REMOVED>
--> yul_unimplemented/input.sol:6:16:
|
6 | return type(test).name;
| ^^^^^^^^^^^^^^^

View File

@ -1 +0,0 @@
1

View File

@ -1,8 +0,0 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.0;
pragma abicoder v2;
contract test {
function f() public pure returns (string memory) {
return type(test).name;
}
}

View File

@ -101,7 +101,7 @@ BOOST_AUTO_TEST_CASE(string_storage)
if (CommonOptions::get().useABIEncoderV1)
CHECK_DEPLOY_GAS(133045, 129731, evmVersion);
else
CHECK_DEPLOY_GAS(152657, 135201, evmVersion);
CHECK_DEPLOY_GAS(155553, 135201, evmVersion);
}
// This is only correct on >=Constantinople.
else if (!CommonOptions::get().useABIEncoderV1)
@ -117,9 +117,9 @@ BOOST_AUTO_TEST_CASE(string_storage)
else
{
if (evmVersion < EVMVersion::istanbul())
CHECK_DEPLOY_GAS(146671, 123969, evmVersion);
CHECK_DEPLOY_GAS(149567, 123969, evmVersion);
else
CHECK_DEPLOY_GAS(131591, 110969, evmVersion);
CHECK_DEPLOY_GAS(134123, 110969, evmVersion);
}
}
else if (evmVersion < EVMVersion::istanbul())
@ -131,7 +131,7 @@ BOOST_AUTO_TEST_CASE(string_storage)
{
callContractFunction("f()");
if (evmVersion == EVMVersion::byzantium())
CHECK_GAS(21712, 21555, 20);
CHECK_GAS(21741, 21555, 20);
// This is only correct on >=Constantinople.
else if (!CommonOptions::get().useABIEncoderV1)
{
@ -145,9 +145,9 @@ BOOST_AUTO_TEST_CASE(string_storage)
else
{
if (evmVersion < EVMVersion::istanbul())
CHECK_GAS(21707, 21559, 20);
CHECK_GAS(21736, 21559, 20);
else
CHECK_GAS(21499, 21351, 20);
CHECK_GAS(21528, 21351, 20);
}
}
else if (evmVersion < EVMVersion::istanbul())

View File

@ -13,8 +13,8 @@ contract C {
}
// ----
// creation:
// codeDepositCost: 376800
// executionCost: 411
// totalCost: 377211
// codeDepositCost: 398400
// executionCost: 436
// totalCost: 398836
// external:
// f(): 399
// f(): 428

View File

@ -22,7 +22,8 @@ contract Test is C {
return type(I).name;
}
}
// ====
// compileViaYul: also
// ----
// c() -> 0x20, 1, "C"
// a() -> 0x20, 1, "A"