Maker Pro
Maker Pro

IAR AVR Compiler

N

Neil

I used IAR (v3.2 Evaluation)for first time and found that the assignment
operator was not "working" on local variables, particularly in main(). If I
moved the variable outside of main(), making them global, all worked ok??

heres an example...

unsigned char LegSensors = 0x00;
int main(void)
{
unsigned char var1;
DDRA = 0x00;
LegSensors = PINA;
var1 = var1 + 1;
return 0;
}

Now, this compiles fine, although if I had used var1++ the compiler warns
about var1 being set but never used. In fact, during debug, var1=var1+1 does
not even exist (nor would var1++).

This assignment operator issue also occurs when trying to assign the return
value from a function to a variable declared locally. ie myvar =
value_returning_funtion();

Is this an eval version thing, some config thing I am overlooking, or does
IAR do this all the time?

Thanks
Neil
 
R

Rich Webb

I used IAR (v3.2 Evaluation)for first time and found that the assignment
operator was not "working" on local variables, particularly in main(). If I
moved the variable outside of main(), making them global, all worked ok??

heres an example...

unsigned char LegSensors = 0x00;
int main(void)
{
unsigned char var1;
DDRA = 0x00;
LegSensors = PINA;
var1 = var1 + 1;
return 0;
}

Now, this compiles fine, although if I had used var1++ the compiler warns
about var1 being set but never used. In fact, during debug, var1=var1+1 does
not even exist (nor would var1++).

It should give you a warning about an uninitialized variable. You've
defined var1 as an auto but haven't given it an initial value. On the
other hand, when you define var1 outside of main() it is an external
variable and so is initialized to 0.

I can't check the behavior in detail, though, since I use Imagecraft's
AVR compiler...
 
N

Neil

Sorry to say, it made no difference. The code below compiled, but produced
only the one the warning:
"Warning[Pe550]: variable "LegSensors" was set but never used"
Running the debugger, the locals window has all three variables assigned to
registers(var2=R18, var1=R19, LegSensors=R20). However only LegSensors is in
the assembly code, there is no assembly working on R18 and R19.
I don't get it???
int main(void)
{
unsigned char LegSensors = 0x00;
unsigned char var1 = 0x00;
unsigned char var2 = 0x00;
DDRA = 0x00;
LegSensors = PINA;
var1 = var1 + 1;
var2++;
return 0;
}

Regards,
Neil.
 
R

Rich Webb

Sorry to say, it made no difference. The code below compiled, but produced
only the one the warning:
"Warning[Pe550]: variable "LegSensors" was set but never used"
Running the debugger, the locals window has all three variables assigned to
registers(var2=R18, var1=R19, LegSensors=R20). However only LegSensors is in
the assembly code, there is no assembly working on R18 and R19.
I don't get it???

Possibly the compiler is looking at the code and notices that "nothing
happens" to those variables and so they are optimized out.

LegSensors is retained, perhaps, because it "touches" the outside world
via the port read.

Try keeping what you have but add a volatile external (something that
the compiler shouldn't optimize out) and then write the apparently
non-functioning variables to it. The compiler shouldn't optimize them
away because it's not supposed to make any assumptions about volatiles.

/* add this */
unsigned char foo;
int main(void)
{
unsigned char LegSensors = 0x00;
unsigned char var1 = 0x00;
unsigned char var2 = 0x00;
DDRA = 0x00;
LegSensors = PINA;
var1 = var1 + 1;
var2++;

/* and these */
foo = var1;
foo = var2;
 
W

Wayne Farmer

Neil said:
I used IAR (v3.2 Evaluation)for first time and found that the assignment
operator was not "working" on local variables, particularly in main(). If I
moved the variable outside of main(), making them global, all worked ok??

heres an example...

unsigned char LegSensors = 0x00;
int main(void)
{
unsigned char var1;
DDRA = 0x00;
LegSensors = PINA;
var1 = var1 + 1;
return 0;
}

Now, this compiles fine, although if I had used var1++ the compiler warns
about var1 being set but never used. In fact, during debug, var1=var1+1 does
not even exist (nor would var1++).

This assignment operator issue also occurs when trying to assign the return
value from a function to a variable declared locally. ie myvar =
value_returning_funtion();

Is this an eval version thing, some config thing I am overlooking, or does
IAR do this all the time?

Thanks
Neil

The compiler is working fine. You told it to allocate space for var1 as a
local variable, increment that value by 1, and then discard the value. The
compiler optimized that to not increment the value by 1, because the result
was never used.

If you declare var1 to be "static", so that its value remains after the call
to main, then I think you'll see the "var1 = var1 + 1" code remain.
 
N

Neil

declaring variables as volatile solves the issue.
it should be noted that all optimisation settings are off so it is obviously
a default thing...I am happy to be corrected on that of course...

thanks all.


Rich Webb said:
Sorry to say, it made no difference. The code below compiled, but produced
only the one the warning:
"Warning[Pe550]: variable "LegSensors" was set but never used"
Running the debugger, the locals window has all three variables assigned to
registers(var2=R18, var1=R19, LegSensors=R20). However only LegSensors is in
the assembly code, there is no assembly working on R18 and R19.
I don't get it???

Possibly the compiler is looking at the code and notices that "nothing
happens" to those variables and so they are optimized out.

LegSensors is retained, perhaps, because it "touches" the outside world
via the port read.

Try keeping what you have but add a volatile external (something that
the compiler shouldn't optimize out) and then write the apparently
non-functioning variables to it. The compiler shouldn't optimize them
away because it's not supposed to make any assumptions about volatiles.

/* add this */
unsigned char foo;
int main(void)
{
unsigned char LegSensors = 0x00;
unsigned char var1 = 0x00;
unsigned char var2 = 0x00;
DDRA = 0x00;
LegSensors = PINA;
var1 = var1 + 1;
var2++;

/* and these */
foo = var1;
foo = var2;
return 0;
}
 
Top