-
Notifications
You must be signed in to change notification settings - Fork 6.1k
[Sol->Yul] Implement uint256[] memory arrays #7015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -754,6 +754,36 @@ string YulUtilFunctions::storageArrayIndexAccessFunction(ArrayType const& _type) | |
}); | ||
} | ||
|
||
string YulUtilFunctions::memoryArrayIndexAccessFunction(ArrayType const& _type) | ||
{ | ||
string functionName = "memory_array_index_access_" + _type.identifier(); | ||
return m_functionCollector->createFunction(functionName, [&]() { | ||
return Whiskers(R"( | ||
function <functionName>(baseRef, index) -> addr { | ||
if iszero(lt(index, <arrayLen>(baseRef))) { | ||
invalid() | ||
} | ||
|
||
let offset := mul(index, <stride>) | ||
<?dynamicallySized> | ||
offset := add(offset, 32) | ||
</dynamicallySized> | ||
addr := add(baseRef, offset) | ||
} | ||
)") | ||
("functionName", functionName) | ||
("arrayLen", arrayLengthFunction(_type)) | ||
("stride", to_string(_type.memoryStride())) | ||
("dynamicallySized", _type.isDynamicallySized()) | ||
.render(); | ||
}); | ||
} | ||
|
||
string YulUtilFunctions::calldataArrayIndexAccessFunction(ArrayType const& /*_type*/) | ||
{ | ||
solUnimplemented("Calldata arrays not yet implemented!"); | ||
} | ||
|
||
string YulUtilFunctions::nextArrayElementFunction(ArrayType const& _type) | ||
{ | ||
solAssert(!_type.isByteArray(), ""); | ||
|
@@ -881,6 +911,16 @@ string YulUtilFunctions::readFromStorageDynamic(Type const& _type, bool _splitFu | |
}); | ||
} | ||
|
||
string YulUtilFunctions::readFromMemory(Type const& _type) | ||
{ | ||
return readFromMemoryOrCalldata(_type, false); | ||
} | ||
|
||
string YulUtilFunctions::readFromCalldata(Type const& _type) | ||
{ | ||
return readFromMemoryOrCalldata(_type, true); | ||
} | ||
|
||
string YulUtilFunctions::updateStorageValueFunction(Type const& _type, boost::optional<unsigned> const _offset) | ||
{ | ||
string const functionName = | ||
|
@@ -922,6 +962,64 @@ string YulUtilFunctions::updateStorageValueFunction(Type const& _type, boost::op | |
}); | ||
} | ||
|
||
string YulUtilFunctions::writeToMemoryFunction(Type const& _type) | ||
{ | ||
string const functionName = | ||
string("write_to_memory_") + | ||
_type.identifier(); | ||
|
||
return m_functionCollector->createFunction(functionName, [&] { | ||
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) | ||
} | ||
)") | ||
("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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this perform cleanup? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, but I think we said we leave that for now as there was no function for cleaning up addr/selector. The evm code was only cleaning up addr I think and it used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, the function actually already performs cleanup. |
||
.render(); | ||
} | ||
else if (_type.isValueType()) | ||
{ | ||
return Whiskers(R"( | ||
function <functionName>(memPtr, value) { | ||
mstore(memPtr, <cleanup>(value)) | ||
} | ||
)") | ||
("functionName", functionName) | ||
("cleanup", cleanupFunction(_type)) | ||
.render(); | ||
} | ||
else // Should never happen | ||
{ | ||
solAssert( | ||
false, | ||
"Memory store of type " + _type.toString(true) + " not allowed." | ||
); | ||
} | ||
}); | ||
} | ||
|
||
string YulUtilFunctions::extractFromStorageValueDynamic(Type const& _type, bool _splitFunctionTypes) | ||
{ | ||
solUnimplementedAssert(!_splitFunctionTypes, ""); | ||
|
@@ -1038,6 +1136,28 @@ string YulUtilFunctions::allocationFunction() | |
}); | ||
} | ||
|
||
string YulUtilFunctions::allocateMemoryArrayFunction(ArrayType const& _type) | ||
{ | ||
solUnimplementedAssert(!_type.isByteArray(), ""); | ||
|
||
string functionName = "allocate_memory_array_" + _type.identifier(); | ||
return m_functionCollector->createFunction(functionName, [&]() { | ||
return Whiskers(R"( | ||
function <functionName>(length) -> memPtr { | ||
memPtr := <alloc>(<allocSize>(length)) | ||
<?dynamic> | ||
mstore(memPtr, length) | ||
</dynamic> | ||
} | ||
)") | ||
("functionName", functionName) | ||
("alloc", allocationFunction()) | ||
("allocSize", arrayAllocationSizeFunction(_type)) | ||
("dynamic", _type.isDynamicallySized()) | ||
.render(); | ||
}); | ||
} | ||
|
||
string YulUtilFunctions::conversionFunction(Type const& _from, Type const& _to) | ||
{ | ||
if (_from.sizeOnStack() != 1 || _to.sizeOnStack() != 1) | ||
|
@@ -1146,8 +1266,25 @@ string YulUtilFunctions::conversionFunction(Type const& _from, Type const& _to) | |
solUnimplemented("Fixed point types not implemented."); | ||
break; | ||
case Type::Category::Array: | ||
solUnimplementedAssert(false, "Array conversion not implemented."); | ||
{ | ||
bool equal = _from == _to; | ||
|
||
if (!equal) | ||
{ | ||
ArrayType const& from = dynamic_cast<decltype(from)>(_from); | ||
ArrayType const& to = dynamic_cast<decltype(to)>(_to); | ||
|
||
if (*from.mobileType() == *to.mobileType()) | ||
Marenz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
equal = true; | ||
} | ||
|
||
if (equal) | ||
body = "converted := value"; | ||
else | ||
solUnimplementedAssert(false, "Array conversion not implemented."); | ||
|
||
break; | ||
} | ||
case Type::Category::Struct: | ||
solUnimplementedAssert(false, "Struct conversion not implemented."); | ||
break; | ||
|
@@ -1579,3 +1716,60 @@ string YulUtilFunctions::conversionFunctionSpecial(Type const& _from, Type const | |
); | ||
}); | ||
} | ||
|
||
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(), ""); | ||
|
||
return m_functionCollector->createFunction(functionName, [&] { | ||
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(), ""); | ||
|
||
if (auto const* funType = dynamic_cast<FunctionType const*>(&_type)) | ||
if (funType->kind() == FunctionType::Kind::External) | ||
return Whiskers(R"( | ||
function <functionName>(memPtr) -> addr, selector { | ||
let combined := <load>(memPtr) | ||
addr, selector := <splitFunction>(combined) | ||
} | ||
)") | ||
("functionName", functionName) | ||
("load", _fromCalldata ? "calldataload" : "mload") | ||
("splitFunction", splitExternalFunctionIdFunction()) | ||
.render(); | ||
|
||
return Whiskers(R"( | ||
function <functionName>(memPtr) -> value { | ||
value := <load>(memPtr) | ||
<?needsValidation> | ||
value := <validate>(value) | ||
</needsValidation> | ||
} | ||
)") | ||
("functionName", functionName) | ||
("load", _fromCalldata ? "calldataload" : "mload") | ||
("needsValidation", _fromCalldata) | ||
("validate", _fromCalldata ? validatorFunction(_type) : "") | ||
.render(); | ||
}); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.