Skip to content

Instantly share code, notes, and snippets.

@hrkrshnn
Created April 8, 2021 09:54
Show Gist options
  • Save hrkrshnn/5398c224974ce8116ce82aa641fae2e8 to your computer and use it in GitHub Desktop.
Save hrkrshnn/5398c224974ce8116ce82aa641fae2e8 to your computer and use it in GitHub Desktop.
Optimized IR:
/*******************************************************
,* WARNING *
,* Solidity to Yul compilation is still EXPERIMENTAL *
,* It can result in LOSS OF FUNDS or worse *
,* !USE AT YOUR OWN RISK! *
,*******************************************************/
object "C_26" {
code {
{
mstore(64, 128)
if callvalue() { revert(0, 0) }
let _1 := datasize("C_26_deployed")
codecopy(0, dataoffset("C_26_deployed"), _1)
return(0, _1)
}
}
object "C_26_deployed" {
code {
{
mstore(64, 128)
if iszero(lt(calldatasize(), 4))
{
let _1 := 0
if eq(0x26121ff0, shr(224, calldataload(_1)))
{
if callvalue() { revert(_1, _1) }
if slt(add(calldatasize(), not(3)), _1) { revert(_1, _1) }
let oldLen := sload(_1)
let _2 := 18446744073709551616
if iszero(lt(oldLen, _2)) { panic_error_0x41() }
sstore(_1, add(oldLen, 1))
let slot, offset := storage_array_index_access_bytes__dyn_ptr(oldLen)
if offset { panic_error_0x00() }
resize_array_bytes_storage_ptr(slot)
sstore(slot, add("abc", 6))
let oldLen_1 := sload(_1)
if iszero(lt(oldLen_1, _2)) { panic_error_0x41() }
sstore(_1, add(oldLen_1, 1))
let slot_1, offset_1 := storage_array_index_access_bytes__dyn_ptr(oldLen_1)
if offset_1 { panic_error_0x00() }
resize_array_bytes_storage_ptr_805(slot_1)
mstore(_1, slot_1)
sstore(keccak256(_1, 32), "abcdefghabcdefghabcdefghabcdefgh")
array_push_from_stringliteral_7eaa_to_array_bytes_storage_dyn_ptr()
return(allocate_memory(), _1)
}
}
revert(0, 0)
}
function allocate_memory() -> memPtr
{
memPtr := mload(64)
if gt(memPtr, 0xffffffffffffffff) { panic_error_0x41() }
mstore(64, memPtr)
}
function array_push_from_stringliteral_7eaa_to_array_bytes_storage_dyn_ptr()
{
let _1 := 0
let oldLen := sload(_1)
if iszero(lt(oldLen, 18446744073709551616)) { panic_error_0x41() }
let _2 := add(oldLen, 1)
sstore(_1, _2)
if iszero(lt(oldLen, _2))
{
mstore(_1, shl(224, 0x4e487b71))
mstore(4, 0x32)
revert(_1, 0x24)
}
mstore(_1, _1)
let slot := add(keccak256(_1, 0x20), oldLen)
let data := sload(slot)
let length := _1
length := shr(1, data)
let outOfPlaceEncoding := and(data, 1)
if iszero(outOfPlaceEncoding) { length := and(length, 0x7f) }
let _3 := lt(length, 0x20)
if eq(outOfPlaceEncoding, _3)
{
mstore(_1, shl(224, 0x4e487b71))
mstore(4, 0x22)
revert(_1, 0x24)
}
if gt(64, length)
{
switch _3
case 0 { sstore(slot, 129) }
default {
mstore(_1, slot)
sstore(keccak256(_1, 0x20), and(not(255), data))
sstore(slot, 129)
}
}
if lt(64, length)
{
byte_array_decrease_size_bytes_storage_ptr(slot, length)
}
mstore(_1, slot)
let data_1 := keccak256(_1, 0x20)
let _4 := "abcdefghabcdefghabcdefghabcdefgh"
sstore(data_1, _4)
sstore(add(data_1, 1), _4)
}
function byte_array_decrease_size_bytes_storage_ptr(array, oldLen)
{
mstore(0, array)
let data := keccak256(0, 32)
clear_storage_range_bytes1(add(data, 2), add(data, shr(5, add(oldLen, 31))))
sstore(array, 129)
}
function clear_storage_range_bytes1(start, end)
{
for { } lt(start, end) { start := add(start, 1) }
{ sstore(start, 0) }
}
function extract_used_part_and_set_length_of_short_byte_array(data) -> used
{
used := or(and(data, shl(232, 16777215)), 6)
}
function panic_error_0x00()
{
mstore(0, shl(224, 0x4e487b71))
mstore(4, 0)
revert(0, 0x24)
}
function panic_error_0x41()
{
mstore(0, shl(224, 0x4e487b71))
mstore(4, 0x41)
revert(0, 0x24)
}
function resize_array_bytes_storage_ptr(array)
{
let data := sload(array)
let length := 0
length := shr(1, data)
let outOfPlaceEncoding := and(data, 1)
if iszero(outOfPlaceEncoding) { length := and(length, 0x7f) }
let _1 := lt(length, 32)
if eq(outOfPlaceEncoding, _1)
{
mstore(0, shl(224, 0x4e487b71))
mstore(4, 0x22)
revert(0, 0x24)
}
if gt(3, length)
{
switch _1
case 0 { sstore(array, 7) }
default {
sstore(array, or(and(data, shl(232, 16777215)), 6))
}
}
if lt(3, length)
{
switch gt(length, 31)
case 1 {
mstore(0, array)
let data_1 := keccak256(0, 32)
clear_storage_range_bytes1(add(data_1, 1), add(data_1, shr(5, add(length, 31))))
mstore(0, array)
let data_2 := keccak256(0, 32)
sstore(array, extract_used_part_and_set_length_of_short_byte_array(sload(data_2)))
sstore(data_2, 0)
}
default {
sstore(array, or(and(data, shl(232, 16777215)), 6))
}
}
}
function resize_array_bytes_storage_ptr_805(array)
{
let data := sload(array)
let length := 0
length := shr(1, data)
let outOfPlaceEncoding := and(data, 1)
if iszero(outOfPlaceEncoding) { length := and(length, 0x7f) }
let _1 := lt(length, 32)
if eq(outOfPlaceEncoding, _1)
{
mstore(0, shl(224, 0x4e487b71))
mstore(4, 0x22)
revert(0, 0x24)
}
if gt(32, length)
{
switch _1
case 0 { sstore(array, 65) }
default {
mstore(0, array)
sstore(keccak256(0, 32), and(not(255), data))
sstore(array, 65)
}
}
if lt(32, length)
{
mstore(0, array)
let data_1 := keccak256(0, 32)
clear_storage_range_bytes1(add(data_1, 1), add(data_1, shr(5, add(length, 31))))
sstore(array, 65)
}
}
function storage_array_index_access_bytes__dyn_ptr(index) -> slot, offset
{
if iszero(lt(index, sload(slot)))
{
mstore(slot, shl(224, 0x4e487b71))
mstore(4, 0x32)
revert(slot, 0x24)
}
mstore(slot, slot)
slot := add(keccak256(slot, 0x20), index)
offset := offset
}
}
}
}
#+end_example
@hrkrshnn
Copy link
Author

hrkrshnn commented Apr 8, 2021

Contract

contract C {
    bytes[] a;

    function f() public {
        a.push("abc");
        a.push("abcdefghabcdefghabcdefghabcdefgh");
        a.push("abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh");
        assert(a[0][0] == "a");
        assert(a[1][31] == "h");
        assert(a[2][32] == "a");
    }
}
// ====
// compileViaYul: also
// ----
// f() ->
// gas irOptimized: 181480
// gas legacy: 180320
// gas legacyOptimized: 180103

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment