Merge pull request #9760 from ethereum/sol2YulStorageArrayIndexAccessFix

[Sol->Yul] Fixing storage array index access
This commit is contained in:
chriseth 2020-09-08 16:33:54 +02:00 committed by GitHub
commit 202332405f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 3 deletions

View File

@ -1198,10 +1198,9 @@ string YulUtilFunctions::storageArrayIndexAccessFunction(ArrayType const& _type)
slot := array
}
<!isBytesArray>
let itemsPerSlot := div(0x20, <storageBytes>)
let dataArea := <dataAreaFunc>(array)
slot := add(dataArea, div(index, itemsPerSlot))
offset := mod(index, itemsPerSlot)
slot := add(dataArea, div(index, <itemsPerSlot>))
offset := mul(mod(index, <itemsPerSlot>), <storageBytes>)
</isBytesArray>
<!multipleItemsPerSlot>
let dataArea := <dataAreaFunc>(array)
@ -1217,6 +1216,7 @@ string YulUtilFunctions::storageArrayIndexAccessFunction(ArrayType const& _type)
("isBytesArray", _type.isByteArray())
("storageSize", _type.baseType()->storageSize().str())
("storageBytes", toString(_type.baseType()->storageBytes()))
("itemsPerSlot", to_string(32 / _type.baseType()->storageBytes()))
.render();
});
}

View File

@ -12,5 +12,7 @@ contract c {
}
}
// ====
// compileViaYul: also
// ----
// test() -> 1, 2, 3, 4

View File

@ -0,0 +1,16 @@
contract C {
uint8[33] a;
uint32[9] b;
uint120[3] c;
function f() public returns (uint8, uint32, uint120) {
a[32] = 1; a[31] = 2; a[30] = 3;
b[0] = 1; b[1] = 2; b[2] = 3;
c[2] = 3; c[1] = 1;
return (a[32], b[1], c[2]);
}
}
// ====
// compileViaYul: also
// ----
// f() -> 1, 2, 3