SPRU514T – Optimizing C Compiler reference sheet says the following:
7.1.7 Field/Structure Alignment
When the compiler determines the layout of a structure, it provides as many words as are needed to hold
all of the members, while complying with each member's alignment constraint. This means that padding
bytes may be placed between members and at the end of the structure. Each member is aligned as its
type requires.
Types with a size of 16 bits are aligned on 16-bit boundaries. Types with a size of 32 bits or larger are
aligned on 32-bit (2 word) boundaries. For details on EABI field alignment, refer to the C28x Embedded
Application Binary Interface (EABI) Reference Guide (SPRAC71).
However, bit-fields may not be aligned as strictly as the declared type of the bit-field would be if it were not
a bit-field. Bit-fields are packed in the order seen in the source code. The least significant bits of the
structure word are filled first. Bit-fields are allocated only as many bits as requested. Bit-fields are packed
into adjacent bits of a word.
For COFF, bit-fields do not overlap word boundaries. If a bit-field would overlap into the next word, the
entire bit-field is placed into the next word.
Can you please clarify explicitly and exactly under what conditions the C2000 pads struct members/bitfields (COFF)? This is very important for our CAN messaging architecture which relies heavily on bytes being ordered/packed correctly. From what I can gather from the field/structure alignment section there are 3 conditions that must be followed.
- 16-bit native data types (uint16_t, int16_t, etc.) must be 16-bit aligned.
- Does this mean that if I have a 1-bit field followed by a 16-bit field, 15 bits of padding will be added between the 1-bit field and the 16-bit field?
- Similarly, if I have an 8-bit field followed by a 16-bit field, 8 bits of padding will be added between the 8-bit field and the 16-bit field?
- 32-bit+ native data types (uint32_t, uint64_t, etc.) must be 32-bit aligned (2-words).
- Does this mean that if I have a 1-bit field followed by a 32-bit field, 31 bits of padding will be added between the 1-bit field and the 32-bit field?
- Does this mean if I have a 16-bit field followed by a 32-bit field, 16 bits of padding will be added between the 16-bit field and the 32-bit field?
- For COFF, bit fields don't overlap word (16-bit?) boundaries. If overlapping, entire bit-field is placed into next word.
- How would this work for bit fields that are greater than 16-bits in size? Those technically have to overlap word boundaries, no? For example, an 8-bit field followed by a 24-bit field (which is one case I've actually tested and it fits into 32 bits in memory...seemingly violating this rule)?
- What would happen if there was a 9-bit field followed by 24-bit field? Would there be 7 bits of padding (16-bit/word aligned) or 23 bits of padding (32-bit aligned)?
These are just a couple example questions, but overall, the datasheet doesn't seem to match what is being observed (or at the very least, I am misunderstanding what it's saying). I need clarification on exactly how bit/byte alignment works in the C2000 for structs/bitfields (COFF) and most importantly what constraints we need to follow so that intermediary bit/byte padding does not happen since our CAN architecture requires strict byte ordering.
Thanks!