-
Notifications
You must be signed in to change notification settings - Fork 71
Update alignment to handle terminator alignment #1601
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -134,11 +134,14 @@ trait AlignedMixin extends GrammarMixin { self: Term => | |
| LengthExact(trailingSkipInBits) | ||
| } | ||
|
|
||
| // FIXME: DAFFODIL-2295 | ||
| // Does not take into account that in a sequence, what may be prior may be a separator. | ||
| // The separator is text in some encoding, might not be the same as this term's encoding, and | ||
| // the alignment will be left where that text leaves it. | ||
| // | ||
| /** | ||
| * The priorAlignmentApprox doesn't need to take into account the separator | ||
| * alignment/length as that comes after the priorAlignmentApprox, but before | ||
| * the leadingSkip. | ||
| * | ||
| * TODO: DAFFODIL-3056 We need to consider the initiator MTA/length, | ||
| * as well as the prefix length | ||
| */ | ||
| private lazy val priorAlignmentApprox: AlignmentMultipleOf = | ||
| LV(Symbol("priorAlignmentApprox")) { | ||
| if (this.isInstanceOf[Root] || this.isInstanceOf[QuasiElementDeclBase]) { | ||
|
|
@@ -232,6 +235,31 @@ trait AlignedMixin extends GrammarMixin { self: Term => | |
| priorAlignmentApprox + leadingSkipApprox | ||
| } | ||
|
|
||
| private lazy val terminatorMTAApprox = | ||
| if (this.hasTerminator) { | ||
| AlignmentMultipleOf(this.knownEncodingAlignmentInBits) | ||
| } else { | ||
| AlignmentMultipleOf(0) | ||
| } | ||
|
|
||
| private lazy val terminatorLengthApprox = if (this.hasTerminator) { | ||
| getEncodingLengthApprox(this) | ||
| } else { | ||
| LengthMultipleOf(0) | ||
| } | ||
|
|
||
| private def getEncodingLengthApprox(t: Term) = { | ||
| if (t.isKnownEncoding) { | ||
| val dfdlCharset = t.charsetEv.optConstant.get | ||
| val fixedWidth = | ||
| if (dfdlCharset.maybeFixedWidth.isDefined) dfdlCharset.maybeFixedWidth.get else 8 | ||
| LengthMultipleOf(fixedWidth) | ||
| } else { | ||
| // runtime encoding, which must be a byte-length encoding | ||
| LengthMultipleOf(8) | ||
| } | ||
| } | ||
|
|
||
| protected lazy val contentStartAlignment: AlignmentMultipleOf = { | ||
| if (priorAlignmentWithLeadingSkipApprox % alignmentApprox == 0) { | ||
| // alignment won't be needed, continue using prior alignment as start alignment | ||
|
|
@@ -242,15 +270,22 @@ trait AlignedMixin extends GrammarMixin { self: Term => | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Accounts for the terminator MTA/Length and trailing skip | ||
| */ | ||
| protected lazy val endingAlignmentApprox: AlignmentMultipleOf = { | ||
| this match { | ||
| case eb: ElementBase => { | ||
| if (eb.isComplexType && eb.lengthKind == LengthKind.Implicit) { | ||
| eb.complexType.group.endingAlignmentApprox + trailingSkipApprox | ||
| val res = if (eb.isComplexType && eb.lengthKind == LengthKind.Implicit) { | ||
| eb.complexType.group.endingAlignmentApprox | ||
| } else { | ||
| // simple type or complex type with specified length | ||
| contentStartAlignment + (elementSpecifiedLengthApprox + trailingSkipApprox) | ||
| contentStartAlignment + elementSpecifiedLengthApprox | ||
| } | ||
| val cea = res * terminatorMTAApprox | ||
| + terminatorLengthApprox | ||
| + trailingSkipApprox | ||
| cea | ||
| } | ||
| case mg: ModelGroup => { | ||
| // | ||
|
|
@@ -261,31 +296,36 @@ trait AlignedMixin extends GrammarMixin { self: Term => | |
| val (lastChildren, couldBeLast) = mg.potentialLastChildren | ||
| val lastApproxesConsideringChildren: Seq[AlignmentMultipleOf] = lastChildren.map { lc => | ||
| // | ||
| // for each possible last child, add its ending alignment | ||
| // to our trailing skip to get where it would leave off were | ||
| // it the actual last child. | ||
| // | ||
| // for each possible last child, gather its endingAlignmentApprox | ||
| val lceaa = lc.endingAlignmentApprox | ||
| val res = lceaa + trailingSkipApprox | ||
| res | ||
| lceaa | ||
| } | ||
| val optApproxIfNoChildren = | ||
| // | ||
| // gather possibilities for this item itself | ||
| // | ||
| if (couldBeLast) | ||
| if (couldBeLast) { | ||
| // | ||
| // if this model group could be last, then consider | ||
| // if none of its content was present. | ||
| // We'd just have the contentStart, nothing, and the trailing Skip. | ||
| // We'd just have the contentStart | ||
| // | ||
| Some(contentStartAlignment + trailingSkipApprox) | ||
| else | ||
| val res = Some(contentStartAlignment) | ||
| res | ||
| } else | ||
|
Member
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. Instead of adding the same three fields to a bunch of places in the various conditions can we do something like val contentEndAlignment = ... //existing logic
val res =
contentEndAlignment
+ terminatorMTAAPprox
+ terminatorLengthApprox
+ trailingSkipApproxI'm not sure if contentEndAlignment is the right name, but something along those lines? |
||
| // can't be last, no possibilities to gather. | ||
| None | ||
| val lastApproxes = lastApproxesConsideringChildren ++ optApproxIfNoChildren | ||
| Assert.invariant(lastApproxes.nonEmpty) | ||
| val res = lastApproxes.reduce { _ * _ } | ||
| // take all the gathered possibilities and add the ending alignment | ||
| // to terminator MTA/length and our trailing skip to get where it would leave off were | ||
| // each the actual last child. | ||
| val lastApproxesFinal = lastApproxes.map { | ||
| _ * terminatorMTAApprox | ||
| + terminatorLengthApprox | ||
| + trailingSkipApprox | ||
| } | ||
| Assert.invariant(lastApproxesFinal.nonEmpty) | ||
| val res = lastApproxesFinal.reduce { _ * _ } | ||
| res | ||
| } | ||
| } | ||
|
|
@@ -329,15 +369,7 @@ trait AlignedMixin extends GrammarMixin { self: Term => | |
| } | ||
|
|
||
| private lazy val encodingLengthApprox: LengthApprox = { | ||
| if (isKnownEncoding) { | ||
| val dfdlCharset = charsetEv.optConstant.get | ||
| val fixedWidth = | ||
| if (dfdlCharset.maybeFixedWidth.isDefined) dfdlCharset.maybeFixedWidth.get else 8 | ||
| LengthMultipleOf(fixedWidth) | ||
| } else { | ||
| // runtime encoding, which must be a byte-length encoding | ||
| LengthMultipleOf(8) | ||
| } | ||
| getEncodingLengthApprox(this) | ||
| } | ||
|
|
||
| protected lazy val isKnownToBeByteAlignedAndByteLength: Boolean = { | ||
|
|
||
This file contains hidden or 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 hidden or 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.
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.
Same here, can this be simplified to an if-statement?