-
-
Notifications
You must be signed in to change notification settings - Fork 613
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
flag non-portable bitfields #20840
Open
WalterBright
wants to merge
1
commit into
dlang:master
Choose a base branch
from
WalterBright:portableBitfields
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
flag non-portable bitfields #20840
Changes from all commits
Commits
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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -7026,12 +7026,14 @@ | |||||
AggregateDeclaration ad; | ||||||
FieldState* fieldState; | ||||||
bool isunion; | ||||||
LINK linkage; | ||||||
|
||||||
this(AggregateDeclaration ad, FieldState* fieldState, bool isunion) @safe | ||||||
{ | ||||||
this.ad = ad; | ||||||
this.fieldState = fieldState; | ||||||
this.isunion = isunion; | ||||||
this.linkage = LINK.d; | ||||||
} | ||||||
|
||||||
override void visit(Dsymbol d) {} | ||||||
|
@@ -7114,8 +7116,8 @@ | |||||
|
||||||
const sz = t.size(vd.loc); | ||||||
assert(sz != SIZE_INVALID && sz < uint.max); | ||||||
uint memsize = cast(uint)sz; // size of member | ||||||
uint memalignsize = target.fieldalign(t); // size of member for alignment purposes | ||||||
const memsize = cast(uint)sz; // size of member | ||||||
const memalignsize = target.fieldalign(t); // size of member for alignment purposes | ||||||
vd.offset = placeField(vd.loc, | ||||||
fieldState.offset, | ||||||
memsize, memalignsize, vd.alignment, | ||||||
|
@@ -7153,8 +7155,8 @@ | |||||
|
||||||
const sz = t.size(bfd.loc); | ||||||
assert(sz != SIZE_INVALID && sz < uint.max); | ||||||
uint memsize = cast(uint)sz; // size of member | ||||||
uint memalignsize = target.fieldalign(t); // size of member for alignment purposes | ||||||
const uint memsize = cast(uint)sz; // size of member | ||||||
const uint memalignsize = target.fieldalign(t); // size of member for alignment purposes | ||||||
if (log) printf(" memsize: %u memalignsize: %u\n", memsize, memalignsize); | ||||||
|
||||||
if (bfd.fieldWidth == 0 && !anon) | ||||||
|
@@ -7167,7 +7169,20 @@ | |||||
assert(0, "unsupported bit-field style"); | ||||||
|
||||||
const isMicrosoftStyle = style == TargetC.BitFieldStyle.MS; | ||||||
const contributesToAggregateAlignment = target.c.contributesToAggregateAlignment(bfd); | ||||||
|
||||||
/** | ||||||
* Indicates whether the specified bit-field contributes to the alignment | ||||||
* of the containing aggregate. | ||||||
* E.g., (not all) ARM ABIs do NOT ignore anonymous (incl. 0-length) | ||||||
* bit-fields. | ||||||
*/ | ||||||
const bool contributesToAggregateAlignment = isMicrosoftStyle || !bfd.isAnonymous; // sufficient for supported architectures | ||||||
|
||||||
/* Flag bitfield non-portable layouts that differ between MicrosoftStyle and non-MicrosoftStyle | ||||||
* unless it's C semantics. | ||||||
*/ | ||||||
const checkPortable = !ad.isCsymbol() && linkage == LINK.d; | ||||||
bool isPortable = true; // start out assuming portable | ||||||
|
||||||
void startNewField() | ||||||
{ | ||||||
|
@@ -7208,19 +7223,16 @@ | |||||
|
||||||
if (bfd.fieldWidth == 0) | ||||||
{ | ||||||
if (!isMicrosoftStyle && !isunion) | ||||||
if (!isunion) | ||||||
{ | ||||||
// Use type of zero width field to align to next field | ||||||
fieldState.offset = (fieldState.offset + memalignsize - 1) & ~(memalignsize - 1); | ||||||
ad.structsize = fieldState.offset; | ||||||
} | ||||||
else if (isMicrosoftStyle && fieldState.inFlight && !isunion) | ||||||
{ | ||||||
// documentation says align to next int | ||||||
//const alsz = cast(uint)Type.tint32.size(); | ||||||
const alsz = memsize; // but it really does this | ||||||
fieldState.offset = (fieldState.offset + alsz - 1) & ~(alsz - 1); | ||||||
ad.structsize = fieldState.offset; | ||||||
if (!isMicrosoftStyle || fieldState.inFlight) | ||||||
{ | ||||||
const alsz = isMicrosoftStyle | ||||||
? memsize // documentation says align to next int but memsize is actually used | ||||||
: memalignsize; // use type of zero width field to align to next field | ||||||
fieldState.offset = (fieldState.offset + alsz - 1) & ~(alsz - 1); | ||||||
ad.structsize = fieldState.offset; | ||||||
} | ||||||
} | ||||||
|
||||||
fieldState.inFlight = false; | ||||||
|
@@ -7229,7 +7241,7 @@ | |||||
|
||||||
if (!fieldState.inFlight) | ||||||
{ | ||||||
//printf("not in flight\n"); | ||||||
if (log) printf("not in flight\n"); | ||||||
startNewField(); | ||||||
} | ||||||
else if (!isMicrosoftStyle) | ||||||
|
@@ -7296,6 +7308,11 @@ | |||||
fieldState.bitOffset = pastField; | ||||||
} | ||||||
|
||||||
if (checkPortable && !isPortable) | ||||||
{ | ||||||
error(bfd.loc, "bitfield layout not portable among supported C compilers, wrap with `extern (C)` or `extern (C++)`"); | ||||||
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. This should probably mention other ABI's like Windows i.e.
Suggested change
|
||||||
} | ||||||
|
||||||
//printf("\t%s: offset = %d bitOffset = %d fieldWidth = %d memsize = %d\n", toChars(), offset, bitOffset, fieldWidth, memsize); | ||||||
//printf("\t%s: memalignsize = %d\n", toChars(), memalignsize); | ||||||
//printf(" addField '%s' to '%s' at offset %d, size = %d\n", toChars(), ad.toChars(), offset, memsize); | ||||||
|
@@ -7315,6 +7332,14 @@ | |||||
atd.include(null).foreachDsymbol( s => s.setFieldOffset(ad, fieldState, isunion) ); | ||||||
} | ||||||
|
||||||
override void visit(LinkDeclaration atd) | ||||||
{ | ||||||
LINK save = linkage; | ||||||
linkage = atd.linkage; | ||||||
atd.include(null).foreachDsymbol( s => s.setFieldOffset(ad, fieldState, isunion) ); | ||||||
linkage = save; | ||||||
} | ||||||
|
||||||
override void visit(AnonDeclaration anond) | ||||||
{ | ||||||
//printf("\tAnonDeclaration::setFieldOffset %s %p\n", isunion ? "union" : "struct", anond); | ||||||
|
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.
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.
This is target specific though. Each compiler back-end makes its own decision about this.
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.
For example, in GCC, back-ends set a
PCC_BITFIELD_TYPE_MATTERS
macro to notify us that type affects alignment. ThenTARGET_ALIGN_ANON_BITFIELD
if anonymous bitfields affect alignment as well.Taking this away would likely regress data layouts on those back-ends.
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.
What matters is which ones are actually different.