How does solc handle immutable variables?

<aside> đź’ˇ https://blog.soliditylang.org/2020/05/13/immutable-keyword/

</aside>

… during contract creation, a dedicated memory area is reserved for the values of immutables. The constructor code can then store the intended values for the immutables in that memory area. The compiler-generated part of the creation code that is executed after the constructor and returns the contract’s runtime code will read back the values of the immutables and insert them into all occurrences in the runtime bytecode.

immutable variables in solidity are stored in the contract’s runtime bytecode, however their values are not known at compile-time. To get around this, the solidity compiler instead adds placeholders that are replaced with PUSH32 <value> upon the immutables being defined in the contract’s constructor. The runtime bytecode is then altered within the constructor, at runtime to fill these placeholders. This is much different from the behavior of constant variables, which have known values at compile-time and can be directly inlined before deployment.

Implications of adding an immutable type to Huff

Adding Immutables to Huff would be another high-level feature added to the compiler, which is something we have been trying to avoid for the most part. Immutables are not entirely necessary, as the compiler already supports constants as well as it storing constructor arguments at the end of the runtime bytecode. However, there are some cases where they can be useful.

Pros/Cons

Arguments against adding immutable:

Arguments for adding immutable:

Proposed Solution

After looking at the pros/cons, it seems there might be a solution that lies in the middle. Instead of adding an immutable type, we could add a new flag to the CLI that allows setting new / overriding existing constant values for the current compilation. With this method, we can add support for the new CLI flag to foundry-huff, and developers could easily assign constant values from their deployment scripts.

This way: