-
Notifications
You must be signed in to change notification settings - Fork 53
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
Encode complex types in section mangling #1199
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
823dc54
section_mangler: Encode structs and enums
CohenArthur 28b6b41
codegen: Mangle struct and enum types properly
CohenArthur 06ece68
section-mangler: Add base for handling arrays
CohenArthur 2e628ea
codegen: Handle more complex types in mangle_type
CohenArthur 8c8f973
codegen: Mangle type aliases properly
CohenArthur 54e08eb
rusty: Accept snapshots with new section names
CohenArthur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ use plc_diagnostics::diagnostics::Diagnostic; | |
use section_mangler::{StringEncoding as SectionStringEncoding, Type}; | ||
|
||
pub fn mangle_type(index: &Index, ty: &typesystem::DataType) -> Result<section_mangler::Type, Diagnostic> { | ||
let access_inner = |ty_name| mangle_type(index, index.get_effective_type_by_name(ty_name)?); | ||
|
||
// TODO: This is a bit ugly because we keep dereferencing references to Copy types like | ||
// bool, u32, etc, because `DataTypeInformation::Pointer` keeps a `String` which is not | ||
// Copy. the alternative is for section_mangle::Type to keep references everywhere, and | ||
|
@@ -23,10 +25,36 @@ pub fn mangle_type(index: &Index, ty: &typesystem::DataType) -> Result<section_m | |
|
||
Type::String { size: *size as usize, encoding } | ||
} | ||
DataTypeInformation::Pointer { inner_type_name, .. } => Type::Pointer { | ||
inner: Box::new(mangle_type(index, index.get_effective_type_by_name(inner_type_name)?)?), | ||
DataTypeInformation::Pointer { inner_type_name, .. } => { | ||
Type::Pointer { inner: Box::new(access_inner(inner_type_name)?) } | ||
} | ||
DataTypeInformation::Enum { referenced_type, variants, .. } => { | ||
Type::Enum { referenced_type: Box::new(access_inner(referenced_type)?), elements: variants.len() } | ||
} | ||
DataTypeInformation::Struct { members, .. } => Type::Struct { | ||
members: members.iter().try_fold(Vec::new(), |mut acc, m| -> Result<Vec<Type>, Diagnostic> { | ||
let inner = access_inner(m.get_type_name())?; | ||
|
||
acc.push(inner); | ||
|
||
Ok(acc) | ||
})?, | ||
}, | ||
// FIXME: For now, encode all unknown types as "void" since this is not required for | ||
DataTypeInformation::Array { inner_type_name, .. } => { | ||
Type::Array { inner: Box::new(access_inner(inner_type_name)?) } | ||
} | ||
// FIXME: Is that correct? | ||
// For code generation, the actual range does not matter - it is not a breaking change | ||
// if a variable's range changes, at least not for codegen, since the underlying type will stay | ||
// the same. Therefore, only encode it as its underlying type. | ||
DataTypeInformation::SubRange { referenced_type, .. } | ||
// Similarly, we do not care about the alias - only the type which is being codegen'd | ||
| DataTypeInformation::Alias { referenced_type, .. } => access_inner(referenced_type)?, | ||
DataTypeInformation::Generic { .. } => { | ||
// FIXME: Is that correct? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, generics and aliases will be resolved to the correct types during the resolve phase. |
||
unreachable!("generic types should not exist at codegen") | ||
} | ||
// FIXME: For now, encode all unknown types as "void" since this is not required for | ||
// execution. Not doing so (and doing an `unreachable!()` for example) obviously causes | ||
// failures, because complex types are already implemented in the compiler. | ||
_ => Type::Void, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Yes, the range is only relevant at compile time AFAIK.
Technically the norm allows for different runtime behaviour if a value is out of range, but this would still not affect the generated type, only some functions that the user needs to have.
The subrange has an implicit method that gives the upper and lower bounds but this is resolved on compile time as well since the range is known.