Allow modularising yul assembly
- Dominant language
- C++
- Stars
- 25.7k
- Forks
- 6.2k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 21
Description
## Abstract
Currently the scope of functions defined in the yul assembly is just that assembly block. This limits yul code reuse and we have to write solidity bindings to be able to move that logic in a separate file.
```solidity
function getter(address key1, address key2) internal view returns (uint256 result) {
bytes32 ptr = 0;
assembly {
ptr := hashTwo(ptr,key1)
function hashTwo(k, s) -> h {
mstore(0, k)
mstore(0x20, s)
h := keccak256(0, 0x40)
}
}
// some solidity logic and again assembly block that needs the hashTwo util
assembly {
ptr := hashTwo(ptr,key2)
function hashTwo(k, s) -> h {
mstore(0, k)
mstore(0x20, s)
h := keccak256(0, 0x40)
}
}
}
```
## Motivation
By allowing to the scope of yul functions to be shared across assembly blocks, it will give rise to code modularisation.
```solidity
// just a block that defines functions which can be used by
// other assembly blocks, this can also be in other file too
// and imported by other solidity files to get access to the
// `hashTwo` util as in this case.
assembly {
function hashTwo(k, s) -> h {
mstore(0, k)
mstore(0x20, s)
h := keccak256(0, 0x40)
}
}
function getter(address key1, address key2) internal view returns (uint256 result) {
bytes32 ptr = 0;
assembly {
ptr := hashTwo(ptr,key1)
}
// some solidity logic
assembly {
ptr := hashTwo(ptr,key2)
}
}
```
## Specification
Functions defined in the assembly block are also registered in the super scope.
Also allow defining an assembly block in a global, since it is currently only possible to define inside a solidity function. However, assembly block outside function should only be able to define yul functions.
## Backwards Compatibility
There should be no backward compatibility issue mostly.
But if same function name in scope is not allowed then it can break some existing code with error like "multiple yul functions in the scope found with name hashTwo".
However, if a locally defined yul function within the same assembly block is allowed to take precedence then this code can be allowed to compile but with code duplication.
Contributor guide
Research direction
No source files, tests, or compiler entry points are named in the issue. Start by locating the parser and semantic-scope handling for Yul assembly, then define and test global assembly blocks, cross-block function visibility, and name-precedence or duplicate-name behavior as described in the specification.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- solidity
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100