ARM Technical Support Knowledge Articles

OPTIMIZER LEVELS AND VOLATILE VARIABLES

Applies to: C51 C Compiler

Answer

QUESTION

Help. We are porting code from an ancient C compiler to C51 Version 5.1 and we have a lot of memory-mapped devices. We access them using variables that are located at the physical address of the device. However, when the code was originally ported, the volatile keyword was left out. We are having problems because of code fragments like the following:

ADC_REG = 0x80;
ADC_REG = 0x01;
ADC_REG = 0x0A;

In these types of situations, the compiler optimizes out the first two assignments (which are critical). We are working on getting these variables declared with volatile, but, is there a different optimizer level we can use in the meantime to avoid this problem?

ANSWER

Yes. To figure out the optimizer level to use, we wrote the following "program":

void main (void)
{
volatile int i;
int j;

i = 1;
i = 2;
i = 3;

j = 1;
j = 2;
j = 3;
}

After compiling with all optimizer levels, optimizer level 3 is the setting that preserves all assignments. This is the optimizer level to use.

NOTE: This optimizer level may include new or improved optimization techniques. It is best to run this simple example to see what optimizer level will work for you.

Article last edited on: 2004-05-20 20:29:45

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