Skip to content

Rewrite fuel_asm::impl_instructions! to a proc macro #804

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

Closed
wants to merge 16 commits into from

Conversation

Dentosal
Copy link
Member

@Dentosal Dentosal commented Aug 21, 2024

What it says on the tin. No exteranal functional changes, just refactoring.

Crate-internal test_construct function has been removed, as the associated test didn't seem to catch any errors. It has been rewritten to use a fuzzing-style property-based testing.

The purpose of this PR is to allow more complex instruction definitions in the future. This should allow moving some validity checks to instruction parsing level, and to improve type safety.

Checklist

  • Breaking changes are clearly marked as such in the PR description and changelog (None!!)
  • New behavior is reflected in tests (No new behavior!)

Before requesting review

  • I have reviewed the code myself
  • I have created follow-up issues caused by this PR and linked them here

@Dentosal Dentosal added the fuel-asm Related to the `fuel-asm` crate. label Aug 21, 2024
@Dentosal Dentosal self-assigned this Aug 21, 2024
@Dentosal Dentosal marked this pull request as ready for review August 22, 2024 07:41
@Dentosal Dentosal requested a review from a team August 22, 2024 07:41
let name = &arg.name;
if arg.is_imm() {
quote! {
packed_integer |= (#name.to_smallest_int() as u32);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Places like this would be better with a single-use helper function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved most of these to smaller functions.

@Dentosal Dentosal requested review from MitchTurner and a team August 22, 2024 23:31
netrome
netrome previously approved these changes Aug 26, 2024
Copy link
Contributor

@netrome netrome left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice stuff! While I still find macros hard to read in general, the code after the refactor is much easier to follow than the previous implementation of this macro. Thanks for making this improvement!

.0
.iter()
.map(
|Instruction {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice for each closure that we use inside of the map to have a named function like make_constructor_for_opcode. Name is up to you=)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've simplified the code quite a bit and each map has only a small block of code inside. Let me know if you still feel we need to have the closures as separate functions as well.

@Dentosal Dentosal requested a review from a team August 27, 2024 01:52
Copy link
Contributor

@netrome netrome left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@AurelienFT
Copy link
Contributor

@xgreenx Given the fact that we are now open to big refactor like this one : #904. Can we consider to merge this PR ?

@xgreenx
Copy link
Collaborator

xgreenx commented Feb 12, 2025

Given the fact that we are now open to big refactor like this one : #904. Can we consider to merge this PR ?

The PR that you linked is pure refactoring, where we moved one part of the code to another part of the code. While in this PR, we re-implement the macro rule via the procedure macro. It is newly written logic.

We need to carefully review the implementation and compare the old expanded code with the new one to be sure that everything is correct.

Also, maybe we want to have an audit of this re-writing.

I still need to think about the main benefits of using procedural macro vs macro rule. I like the macro rule more because you can debug in your IDE there, but you can't do the same for the generated code from the procedure macro. But at the same time, the macro rule requires a lot of computation, and sometimes, my IDE freezes evaluating our macro rule for opcodes.

@AurelienFT
Copy link
Contributor

Okey, fair.

@Dentosal
Copy link
Member Author

I still need to think about the main benefits of using procedural macro vs macro rule. I like the macro rule more because you can debug in your IDE there, but you can't do the same for the generated code from the procedure macro. But at the same time, the macro rule requires a lot of computation, and sometimes, my IDE freezes evaluating our macro rule for opcodes.

We could instead publish the code produced by expanding the macro, and not ship a proc macro at all. The produced code is reasonably small and shouldn't have any ot these issues.

I find the declarative macro to be unreadable and error-prone. As long as we don't change anything it's fine though.

@Dentosal
Copy link
Member Author

That all said, I'm closing this and #805 for now. We can reopen if we need to modify the macros in the future.

@Dentosal Dentosal closed this Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fuel-asm Related to the `fuel-asm` crate.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants