huff-language / huff-rs

A low-level assembly language for the Ethereum Virtual Machine built in blazing-fast pure rust.
https://huff.sh
Apache License 2.0
590 stars 79 forks source link

Builtin for runtime code / creation code #278

Open obatirou opened 1 year ago

obatirou commented 1 year ago

A builtin to get the runtime code could make sense, especially with contracts having jumptable / immutable variables. Creation code could also have use cases.

cc @Philogy First suggested by @Maddiaa0

Philogy commented 1 year ago

Isn't this already possible with __codesize(MAIN) and __codesize(CONSTRUCTOR)? Would be cool to have a built-in for the sum of the two tho i.e. deploy code, which would be very useful for determining the position of constructor arguments.

Also, a builtin to get the code offset for certain constants/placeholders would help immensely, have had to write a script to find that manually in METH: https://github.com/Philogy/meth-weth/blob/main/script/build-helpers/immutable-offsets.py

obatirou commented 1 year ago

Isn't this already possible with __codesize(MAIN) and __codesize(CONSTRUCTOR)? Would be cool to have a built-in for the sum of the two tho i.e. deploy code, which would be very useful for determining the position of constructor arguments.

Also, a builtin to get the code offset for certain constants/placeholders would help immensely, have had to write a script to find that manually in METH: https://github.com/Philogy/meth-weth/blob/main/script/build-helpers/immutable-offsets.py

__codesize(MAIN) + __codesize(CONSTRUCTOR) will work for contracts that do not have a jumptable: they are appended at end of the MAIN bytecode but are not considered part of the MAIN macro leading to differences between __codesize(MAIN) and deploy code.
I agree with you, would be nice to have a built-in for deploy code size.

Interesting idea for the code offset of placeholders !

Philogy commented 1 year ago

Oh I see, yeah that'd be helpful. Not to mention __codesize(MAIN) __codesize(CONSTRUCTOR) add costs 9 gas and is 7-bytes vs if it was a single push instruction (3-bytes and 3 gas).