29.12.2014 Views

RealView Compilation Tools Compiler Reference Guide - ARM ...

RealView Compilation Tools Compiler Reference Guide - ARM ...

RealView Compilation Tools Compiler Reference Guide - ARM ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

C and C++ Implementation Details<br />

struct X<br />

{<br />

int x:10;<br />

int y:20;<br />

int z:5;<br />

};<br />

The compiler pads the remaining two bits for the first container and assigns a new<br />

integer container for z.<br />

Bitfield containers can overlap each other, for example:<br />

struct X<br />

{<br />

int x:10;<br />

char y:2;<br />

};<br />

The first declaration creates an integer container and allocates 10 bits to x. These 10 bits<br />

occupy the first byte and two bits of the second byte of the integer container. At the<br />

second declaration, the compiler checks for a container of type char. There is no suitable<br />

container, so the compiler allocates a new correctly aligned char container.<br />

Because the natural alignment of char is 1, the compiler searches for the first byte that<br />

contains a sufficient number of unallocated bits to completely contain the bitfield. In the<br />

example structure, the second byte of the int container has two bits allocated to x, and<br />

six bits unallocated. The compiler allocates a char container starting at the second byte<br />

of the previous int container, skips the first two bits that are allocated to x, and allocates<br />

two bits to y.<br />

If y is declared char y:8, the compiler pads the second byte and allocates a new char<br />

container to the third byte, because the bitfield cannot overflow its container. Figure 5-2<br />

shows the bitfield allocation for the following example structure:<br />

struct X<br />

{<br />

int x:10;<br />

char y:8;<br />

};<br />

Bit number<br />

31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0<br />

unallocated<br />

y<br />

padding<br />

x<br />

Figure 5-2 Bitfield allocation 1<br />

<strong>ARM</strong> DUI 0348A Copyright © 2007, 2010 <strong>ARM</strong> Limited. All rights reserved. 5-11<br />

Non-Confidential

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!