-
Notifications
You must be signed in to change notification settings - Fork 144
feat(vm): Memory variable #1609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@winsvega I thought of this when reading the code to Let me know what you think. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest I read plain evm opcodes much better. The fact that I don't know what's going on in this optimisations makes me go back and forth in defenitions.
Let me read it again tomorrow
+ Op.JUMPDEST | ||
+ Op.CALLDATACOPY(63, Op.MLOAD(0), 1) | ||
+ Op.JUMPDEST | ||
code=initialize_code |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer not to subfunc the code for readability
+ Op.CALLDATACOPY(63, Op.MLOAD(0), 1) | ||
+ Op.JUMPDEST | ||
code=initialize_code | ||
+ calldata_copy # offset_calldata_copy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes it less readable
I was thinking the other night. maybe it will be even better for readability if Op will support memory variables?
|
# Code for memory initialization | ||
initialize_code = variable_byte_offset.store(0) | ||
calldata_copy = Op.JUMPDEST + Op.CALLDATACOPY( | ||
dest_offset=variable_current_byte.offset + 32 - 1, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
32, -1 magic numbers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was 63 before, we were putting it in the last byte of the variable.
So the offset, plus its size (32), minus the one byte.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it can be made more readable.
maybe Op.Mstore(var, value) can be supported?
but the most important. is to have some unit test for this logic.
to check that before and after refactoring it perfroms the same result.
and if you find it usefull it can be moved to a global namespace so we can reuse it in other tests.
🗒️ Description
Introduces the
MemoryVariable
abstraction to make contracts more readable when there's heavy use of the EVM memory as variable storage location.To use, simply declare a variable with an unique offset that is not used by any other variable, and then the variable can be used in-place to read the value from memory:
The previous example is equivalent to:
The variable also contains methods to generate bytecode to modify the variable in memory.
The previous example is equivalent to:
🔗 Related Issues
✅ Checklist
mkdocs serve
locally and verified the auto-generated docs for new tests in the Test Case Reference are correctly formatted.