ARM Technical Support Knowledge Articles

EXECUTION OF SPECIAL INSTRUCTIONS WITH OPTIMIZE 3

Applies to: RealView C Compiler

Answer


Information in this support solution applies to:


SYMPTOM

The execution of instructions seems to be out of sequence. This causes strange effects with special instructions such as WFI or WFE.

CAUSE

When using the highest Compiler optimization level 3, the compiler rearranges instructions. Therefore a special instructions such as WFI or WFE may be re-arranged.

EXAMPLE

The C code looks like:

SysCtrl |= 0x00000004;   /* set DEEPSLEEP bit */
__wfi();                 /* Request Wait For Interrupt */

The compiler generates with optimization level 3 the following code:

LDR      r0,[r1,#0x00]           ;Read
WFI                              ;WFI
ORR      r0,r0,#0x04             ;Modify
STR      r0,[r1,#0x00]           ;Write

But expected is this code:

LDR      r0,[r1,#0x00]           ;Read
ORR      r0,r0,#0x04             ;Modify
STR      r0,[r1,#0x00]           ;Write
WFI                              ;WFI

RESOLUTION

Insert the __force_stores() intrinsic function to ensure that write operations are completed before a special instruction is executed.

SysCtrl |= 0x00000004;   /* set DEEPSLEEP bit */
__force_stores();        /* ensure store operation is completed */
__wfi();                 /* Request Wait For Interrupt */

ATTACHED FILES

Request the files attached to this knowledgebase article.

Article last edited on: 2009-03-24 09:32:15

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential