29.10.2014 Views

ARM Compiler toolchain v4.1 for µVision Using the Compiler

ARM Compiler toolchain v4.1 for µVision Using the Compiler

ARM Compiler toolchain v4.1 for µVision Using the Compiler

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Compiler</strong> Coding Practices<br />

5.39 Detailed comparison of an unpacked struct, a __packed struct, and a struct<br />

with individually __packed fields<br />

The differences between not packing a struct, packing an entire struct, and packing individual<br />

fields of a struct are illustrated by <strong>the</strong> three implementations of a struct shown in Table 5-10.<br />

In <strong>the</strong> first implementation, <strong>the</strong> struct is not packed. In <strong>the</strong> second implementation, <strong>the</strong> entire<br />

structure is qualified as __packed. In <strong>the</strong> third implementation, <strong>the</strong> __packed attribute is removed<br />

from <strong>the</strong> structure and <strong>the</strong> individual field that is not naturally aligned is declared as __packed.<br />

Table 5-10 C code <strong>for</strong> an unpacked struct, a packed struct, and a struct with individually<br />

packed fields<br />

Unpacked struct __packed struct __packed fields<br />

struct foo<br />

{<br />

char one;<br />

short two;<br />

char three;<br />

int four;<br />

} c;<br />

__packed struct foo<br />

{<br />

char one;<br />

short two;<br />

char three;<br />

int four;<br />

} c;<br />

struct foo<br />

{<br />

char one;<br />

__packed short two;<br />

char three;<br />

int four;<br />

} c;<br />

Table 5-11 shows <strong>the</strong> corresponding disassembly of <strong>the</strong> machine code produced by <strong>the</strong> compiler<br />

<strong>for</strong> each of <strong>the</strong> sample implementations of Table 5-10, where <strong>the</strong> C code <strong>for</strong> each<br />

implementation has been compiled using <strong>the</strong> option -O2.<br />

Note<br />

The -Ospace and -Otime compiler options control whe<strong>the</strong>r accesses to unaligned elements are<br />

made inline or through a function call. <strong>Using</strong> -Otime results in inline unaligned accesses. <strong>Using</strong><br />

-Ospace results in unaligned accesses made through function calls.<br />

Table 5-11 Disassembly <strong>for</strong> an unpacked struct, a packed struct, and a struct with<br />

individually packed fields<br />

Unpacked struct __packed struct __packed fields<br />

; r0 contains address of c<br />

; char one<br />

LDRB r1, [r0, #0]<br />

; short two<br />

LDRSH r2, [r0, #2]<br />

; char three<br />

LDRB r3, [r0, #4]<br />

; int four<br />

LDR r12, [r0, #8]<br />

; r0 contains address of c<br />

; char one<br />

LDRB r1, [r0, #0]<br />

; short two<br />

LDRB r2, [r0, #1]<br />

LDRSB r12, [r0, #2]<br />

ORR r2, r12, r2, LSL #8<br />

; char three<br />

LDRB r3, [r0, #3]<br />

; int four<br />

ADD r0, r0, #4<br />

BL __aeabi_uread4<br />

; r0 contains address of c<br />

; char one<br />

LDRB r1, [r0, #0]<br />

; short two<br />

LDRB r2, [r0, #1]<br />

LDRSB r12, [r0, #2]<br />

ORR r2, r12, r2, LSL #8<br />

; char three<br />

LDRB r3, [r0, #3]<br />

; int four<br />

LDR r12, [r0, #4]<br />

In <strong>the</strong> disassembly of <strong>the</strong> unpacked struct in Table 5-11, <strong>the</strong> compiler always accesses data on<br />

aligned word or halfword addresses. The compiler is able to do this because <strong>the</strong> struct is padded<br />

so that every member of <strong>the</strong> struct lies on its natural size boundary.<br />

In <strong>the</strong> disassembly of <strong>the</strong> __packed struct in Table 5-11, fields one and three are aligned on <strong>the</strong>ir<br />

natural size boundaries by default, so <strong>the</strong> compiler makes aligned accesses. The compiler<br />

always carries out aligned word or halfword accesses <strong>for</strong> fields it can identify as being aligned.<br />

For <strong>the</strong> unaligned field two, <strong>the</strong> compiler uses multiple aligned memory accesses<br />

<strong>ARM</strong> DUI 0375C Copyright © 2007-2008, 2011 <strong>ARM</strong>. All rights reserved. 5-51<br />

ID061811<br />

Non-Confidential

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

Saved successfully!

Ooh no, something went wrong!