Merge pull request #9447 from ethereum/inlineArrays

[Sol->Yul] Implement inline arrays.
This commit is contained in:
chriseth 2020-07-20 17:53:08 +02:00 committed by GitHub
commit 6a1b1283fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 68 additions and 7 deletions

View File

@ -1620,26 +1620,45 @@ string YulUtilFunctions::zeroComplexMemoryArrayFunction(ArrayType const& _type)
});
}
string YulUtilFunctions::allocateMemoryArrayFunction(ArrayType const& _type)
{
string functionName = "allocate_memory_array_" + _type.identifier();
return m_functionCollector.createFunction(functionName, [&]() {
return Whiskers(R"(
function <functionName>(length) -> memPtr {
let allocSize := <allocSize>(length)
memPtr := <alloc>(allocSize)
<?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 {
let allocSize := <allocSize>(length)
memPtr := <alloc>(allocSize)
memPtr := <allocArray>(length)
let dataStart := memPtr
let dataSize := allocSize
let dataSize := <allocSize>(length)
<?dynamic>
dataStart := add(dataStart, 32)
dataSize := sub(dataSize, 32)
mstore(memPtr, length)
</dynamic>
<zeroArrayFunction>(dataStart, dataSize)
}
)")
("functionName", functionName)
("alloc", allocationFunction())
("allocArray", allocateMemoryArrayFunction(_type))
("allocSize", arrayAllocationSizeFunction(_type))
("zeroArrayFunction", zeroMemoryArrayFunction(_type))
("dynamic", _type.isDynamicallySized())

View File

@ -282,6 +282,12 @@ public:
/// signature: (dataStart, dataSizeInBytes) ->
std::string zeroComplexMemoryArrayFunction(ArrayType const& _type);
/// @returns the name of a function that allocates a memory array.
/// For dynamic arrays it adds space for length and stores it.
/// The contents of the data area are unspecified.
/// signature: (length) -> memPtr
std::string allocateMemoryArrayFunction(ArrayType const& _type);
/// @returns the name of a function that allocates and zeroes a memory array.
/// For dynamic arrays it adds space for length and stores it.
/// signature: (length) -> memPtr

View File

@ -311,7 +311,31 @@ bool IRGeneratorForStatements::visit(Assignment const& _assignment)
bool IRGeneratorForStatements::visit(TupleExpression const& _tuple)
{
if (_tuple.isInlineArray())
solUnimplementedAssert(false, "");
{
auto const& arrayType = dynamic_cast<ArrayType const&>(*_tuple.annotation().type);
solAssert(!arrayType.isDynamicallySized(), "Cannot create dynamically sized inline array.");
define(_tuple) <<
m_utils.allocateMemoryArrayFunction(arrayType) <<
"(" <<
_tuple.components().size() <<
")\n";
string mpos = IRVariable(_tuple).part("mpos").name();
Type const& baseType = *arrayType.baseType();
for (size_t i = 0; i < _tuple.components().size(); i++)
{
Expression const& component = *_tuple.components()[i];
component.accept(*this);
IRVariable converted = convert(component, baseType);
m_code <<
m_utils.writeToMemoryFunction(baseType) <<
"(" <<
("add(" + mpos + ", " + to_string(i * arrayType.memoryStride()) + ")") <<
", " <<
converted.name() <<
")\n";
}
}
else
{
bool willBeWrittenTo = _tuple.annotation().willBeWrittenTo;

View File

@ -4,5 +4,7 @@ contract C {
}
}
// ====
// compileViaYul: also
// ----
// f() -> 3

View File

@ -4,6 +4,7 @@ contract C {
return [4][0];
}
}
// ====
// compileViaYul: also
// ----
// f() -> 4

View File

@ -7,5 +7,7 @@ contract C {
}
}
// ====
// compileViaYul: also
// ----
// f() -> 3, 6

View File

@ -66,6 +66,8 @@ contract C {
// found expectation comments:
// same offset for both arrays @ ABI_CHECK(
// ====
// compileViaYul: false
// ----
// f1(bytes[1]): 0x20, 0x20, 0x3, hex"0102030000000000000000000000000000000000000000000000000000000000" -> 0x3, 0x1, 0x2, 0x3
// f2(bytes[1],bytes[1]): 0x40, 0xa0, 0x20, 0x3, hex"0102030000000000000000000000000000000000000000000000000000000000", 0x20, 0x2, hex"0102000000000000000000000000000000000000000000000000000000000000" -> 0x3, 0x1, 0x2, 0x3, 0x2, 0x1, 0x2

View File

@ -8,6 +8,8 @@ contract C {
return a;
}
}
// ====
// compileViaYul: also
// ----
// f() -> 4, 11, 0x0111, 0x333333, 2222222222222222222
// g() -> 0x10, 0x0100, 0x0101, 0x333333, 2222222222222222222

View File

@ -288,6 +288,7 @@ contract Test {
/// testMul() -> true
//
// ====
// compileViaYul: also
// EVMVersion: >=constantinople
// ----
// library: Pairing

View File

@ -5,5 +5,7 @@ contract c {
}
}
// ====
// compileViaYul: also
// ----
// f() -> 1