Skip to content
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

Refactor Type::Aggregate #12

Merged
merged 4 commits into from
Jun 19, 2022
Merged

Conversation

YerinAlexey
Copy link
Contributor

@YerinAlexey YerinAlexey commented Jun 17, 2022

Description

Aggregate types weren't implemented quite right and caused various problems like Type.size() being broken. Now Type::Aggregate takes a reference to a TypeDef object, which it can use to calculate the size and assert correctness of the generated code because it would be a violation of Rust's rules to have an aggregate without a corresponding definition.

ToDo

  • Proposed feature/fix is sufficiently tested
  • Proposed feature/fix is sufficiently documented
  • The "Unreleased" section in the changelog has been updated, if applicable

@YerinAlexey YerinAlexey mentioned this pull request Jun 17, 2022
3 tasks
@garritfra
Copy link
Owner

Works like a charm! While trying this out I also wrote one more test, just to get a hang of it:

#[test]
fn type_size_nested_aggregate() {
    let inner = TypeDef {
        name: "dog".into(),
        align: None,
        items: vec![(Type::Long, 2)],
    };
    let inner_aggregate = Type::Aggregate(&inner);

    assert!(inner_aggregate.size() == 16);

    let typedef = TypeDef {
        name: "person".into(),
        align: None,
        items: vec![(Type::Long, 1), (Type::Word, 2), (Type::Byte, 1), (Type::Aggregate(&inner), 1)],
    };
    let aggregate = Type::Aggregate(&typedef);

    assert!(aggregate.size() == 33);
}

I don't think I can push to this branch, as it lives in your fork. Do you mind adding this, alongside the Changelog entry?

@garritfra
Copy link
Owner

Nevermind, I think I am able to push to your fork. I'll finish this up. Thanks!

@garritfra garritfra force-pushed the aggregate-refactor branch from b074996 to 91ac0bc Compare June 19, 2022 14:28
@garritfra garritfra merged commit f765067 into garritfra:main Jun 19, 2022
@YerinAlexey
Copy link
Contributor Author

we forgot about alignment

@garritfra
Copy link
Owner

How would that affect the size? Probably just has to be added to the sum of the size, right?

@YerinAlexey
Copy link
Contributor Author

How would that affect the size? Probably just has to be added to the sum of the size, right?

Yes, every item in a typedef has to be extended so that it's dividable by the alignment (or by default, non-aligned size of largest item).
And this obviously follows a change to Antimony to emit padding in structs and take alignment into account when doing field access.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants