Member-only story

Counter.sol in Yul

Naz
10 min readMay 25, 2023

--

1/ What / Why

Yul is what Solidity compiler transforms the abstract syntax tree into after it parses your Solidity code. Yul is used for intermediate optimizations before generating the bytecode and then performing more optimizations on the bytecode. It is useful to look at Yul to get a better understanding of the code you write.

2/ Let’s walk through the generated Yul

For reference, this is our Counter.sol Solidity code. One public storage variable, one function that takes one 32 byte argument, and one function that takes no arguments and increments the storage variable.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

contract Counter {
uint256 public number;

function setNumber(uint256 newNumber) public {
number = newNumber;
}

function increment() public {
number++;
}
}

We shall use forge to inspect the yul representation. To do so, run

forge inspect Counter irOptimized

Note that there are a lot of interesting

--

--

Naz
Naz

Written by Naz

I crave knowledge. Mathematical knowledge and then computer science.

Responses (1)