Skip to content

Instantly share code, notes, and snippets.

@hrkrshnn
hrkrshnn / stack.org
Last active July 6, 2021 10:03
Encoding stack too deep

Encoding stack too deep

{
    let a := calldataload(0) // x1
    let b := calldataload(2) // x2
    mstore(100, 200)  // x3
    mstore(a, b)  // x4
}  
"""
An approach for
https://github.com/ethereum/solidity/pull/11493/files#r651807831
"""
# stack is modelled by an array of characters
stackAfter = ['a', 'x', 'b', 'e', 'x', 'b', 'd', 'a', 'z', 'c']
# operation
# (a, b, c, d) -> (x, y, z)
@hrkrshnn
hrkrshnn / after-enabling-old-optimizer.md
Last active July 1, 2021 13:32
Gas comparision between yul codegen and old codegen (with optimizer enabled). Comparing various branches.

ethereum/solidity#10286

Click for a table of gas differences

reative = (ir_optimized - legacy_optimized) / legacy_optimized * 100

File name IR-optimized Legacy-Optimized relative (%)
abiEncoderV1/abi_decode_v2_storage.sol 193182 193405 -0.115302
abiEncoderV1/abi_encode_calldata_slice.sol 1034560 873928 18.3805
diff -u /tmp/oldyul.txt /tmp/newyul.txt
--- /tmp/oldyul.txt 2021-06-23 15:41:31.859708208 +0200
+++ /tmp/newyul.txt 2021-06-23 15:41:06.418758767 +0200
@@ -28,11 +28,11 @@
{
if callvalue() { revert(_1, _1) }
if slt(add(calldatasize(), not(3)), _1) { revert(_1, _1) }
- let expr_mpos := allocate_memory_1132()
+ let expr_mpos := allocate_memory_1140()
mstore(expr_mpos, 0x2a)
#include <iostream>
#include <vector>
#include <set>
/// `_sigma` denotes a vector of size say `n`.
/// Corresponds to a permutation of {0, 1, 2, ..., n - 1}
/// The value `_sigma[i]` means `i` is mapped to `_sigma[i]`.
///
/// A cycle is a set {a_1, a_2, ..., a_N} such that
/// a_1 -> a_2; a_2 -> a_3, ..., a_{N-1} -> a_N; a_N -> a_1
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 {
@hrkrshnn
hrkrshnn / event_extra_copy.yul
Last active April 8, 2021 09:06
extra memory copy example. Memory Store remover
{
let x := 64
// can be removed
mstore(x, 128)
if iszero(lt(calldatasize(), 4))
{
let _1 := 0
if eq(0x8868a931, shr(224, calldataload(_1)))
{
if callvalue() { revert(_1, _1) }
@hrkrshnn
hrkrshnn / gas_stats.py
Last active June 7, 2021 20:34
Needs parsec and tabulate. Inside `scripts`, run `python3 gas_stats.py`
"""A script to collect gas statistics and print it.
Dependencies: Parsec (https://pypi.org/project/parsec/) and Tabulate
https://pypi.org/project/tabulate/
pip install parsec tabulate
Run from root project dir.
python3 scripts/gas_stats.py
diff -u /tmp/old.txt /tmp/new.txt
--- /tmp/old.txt 2021-03-26 11:50:28.707345655 +0100
+++ /tmp/new.txt 2021-03-26 11:50:03.290450403 +0100
@@ -3,7 +3,7 @@
======= <stdin>:Test =======
EVM assembly:
- /* "<stdin>":0:210 contract Test {... */
+ /* "<stdin>":0:233 contract Test {... */
mstore(0x40, 0x80)
diff -u /tmp/jumpold.txt /tmp/jumpnew.txt
--- /tmp/jumpold.txt 2021-03-24 13:23:56.931683183 +0100
+++ /tmp/jumpnew.txt 2021-03-24 13:23:41.046763769 +0100
@@ -97,16 +97,18 @@
iszero
tag_11
jumpi
- /* "<stdin>":128:133 leave */
- jump(tag_10)
+ /* "<stdin>":119:144 { if x { leave } y := 8 } */