[Sol->Yul] Implement index access for storage arrays

This commit is contained in:
Mathias Baumann
2019-06-20 16:14:51 +02:00
parent 346c512cd7
commit 1dd63f416e
7 changed files with 175 additions and 2 deletions
+30
View File
@@ -690,6 +690,36 @@ string YulUtilFunctions::arrayDataAreaFunction(ArrayType const& _type)
});
}
string YulUtilFunctions::storageArrayIndexAccessFunction(ArrayType const& _type)
{
solUnimplementedAssert(_type.baseType()->storageBytes() > 16, "");
string functionName = "storage_array_index_access_" + _type.identifier();
return m_functionCollector->createFunction(functionName, [&]() {
return Whiskers(R"(
function <functionName>(array, index) -> slot, offset {
if iszero(lt(index, <arrayLen>(array))) {
invalid()
}
let data := <dataAreaFunc>(array)
<?multipleItemsPerSlot>
<!multipleItemsPerSlot>
slot := add(data, mul(index, <storageSize>))
offset := 0
</multipleItemsPerSlot>
}
)")
("functionName", functionName)
("arrayLen", arrayLengthFunction(_type))
("dataAreaFunc", arrayDataAreaFunction(_type))
("multipleItemsPerSlot", _type.baseType()->storageBytes() <= 16)
("storageSize", _type.baseType()->storageSize().str())
.render();
});
}
string YulUtilFunctions::nextArrayElementFunction(ArrayType const& _type)
{
solAssert(!_type.isByteArray(), "");
+6
View File
@@ -136,6 +136,12 @@ public:
/// a memory pointer or a calldata pointer to the slot number / memory pointer / calldata pointer
/// for the data position of an array which is stored in that slot / memory area / calldata area.
std::string arrayDataAreaFunction(ArrayType const& _type);
/// @returns the name of a function that returns the slot and offset for the
/// given array and index
/// signature: (array, index) -> slot, offset
std::string storageArrayIndexAccessFunction(ArrayType const& _type);
/// @returns the name of a function that advances an array data pointer to the next element.
/// Only works for memory arrays, calldata arrays and storage arrays that store one item per slot.
std::string nextArrayElementFunction(ArrayType const& _type);
@@ -820,7 +820,45 @@ void IRGeneratorForStatements::endVisit(IndexAccess const& _indexAccess)
));
}
else if (baseType.category() == Type::Category::Array)
solUnimplementedAssert(false, "");
{
ArrayType const& arrayType = dynamic_cast<ArrayType const&>(baseType);
solAssert(_indexAccess.indexExpression(), "Index expression expected.");
switch (arrayType.location())
{
case DataLocation::Storage:
{
string slot = m_context.newYulVariable();
string offset = m_context.newYulVariable();
m_code << Whiskers(R"(
let <slot>, <offset> := <indexFunc>(<array>, <index>)
)")
("slot", slot)
("offset", offset)
("indexFunc", m_utils.storageArrayIndexAccessFunction(arrayType))
("array", m_context.variable(_indexAccess.baseExpression()))
("index", m_context.variable(*_indexAccess.indexExpression()))
.render();
setLValue(_indexAccess, make_unique<IRStorageItem>(
m_context,
slot,
offset,
*_indexAccess.annotation().type
));
break;
}
case DataLocation::Memory:
solUnimplementedAssert(false, "");
break;
case DataLocation::CallData:
solUnimplementedAssert(false, "");
break;
}
}
else if (baseType.category() == Type::Category::FixedBytes)
solUnimplementedAssert(false, "");
else if (baseType.category() == Type::Category::TypeType)
+2 -1
View File
@@ -135,7 +135,8 @@ string IRStorageItem::storeValue(string const& _value, Type const& _sourceType)
string IRStorageItem::setToZero() const
{
solUnimplemented("Delete for storage location not yet implemented");
solUnimplementedAssert(m_type->isValueType(), "");
return storeValue(m_context.utils().zeroValueFunction(*m_type) + "()", *m_type);
}
IRStorageArrayLength::IRStorageArrayLength(IRGenerationContext& _context, string _slot, Type const& _type, ArrayType const& _arrayType):