[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-chat] Missed Optimisation ?

From: Michael Hennebry
Subject: Re: [avr-chat] Missed Optimisation ?
Date: Tue, 1 Mar 2011 12:59:41 -0600 (CST)
User-agent: Alpine 1.00 (DEB 882 2007-12-20)

On Tue, 1 Mar 2011, Graham Davies wrote:

Michael Hennebry wrote:

We haven't been told how or if "result" is defined.

Yes, we have.

On further examination, I did find a "volatile uint32_t result;".
In context, I would guess that it was a complete
statement in the same file as the ISR.
Note the absence of attributes.
How could result not be in internal SRAM?

ISRs can be called like ordinary functions.

Only if you're nuts, but it's irrelevant, anyway.

The compiler doesn't know whether the programmer is nuts.

The compiler could know [whether Bob is right or wrong to
declare 'result' volatile].  If the definition is in the same file
as the ISR, the compiler could know that "result" need not
be volatile, though not in this case.

This is completely wrong-headed.  The whole point of volatile is to tell the
compiler something about a variable that it cannot see in the source code of
the translation unit.

That is not the same as blinding it to
things it can see in the source code.

In this case, if the definition is in the same file as the ISR,
the compiler would know that "result" is in internal SRAM.

No, it wouldn't.  The C language supports only a single address space and
assumes that all variables that are not declared or are implicitly automatic
are in "memory".  We have to jump through hoops to make the C language work
with the Harvard architecture of many microcontrollers.  This does not
further the discussion.

From this, the compiler could correctly conclude that
neither fetches nor stores have side-effects.

No, it can't.  Or, it isn't useful to conclude that there are not side
effects because there are other reasons to use the storage qualifier
volatile that require the same behavior from the compiler.

Can you give an example that cannot be
eliminated by looking at the source code?

PORTB |= _BV(3);
is usually compiled as an SBI instruction.
Following the volatile rules blindly
would require at least two accesses.

Michael   address@hidden
"Pessimist: The glass is half empty.
Optimist:   The glass is half full.
Engineer:   The glass is twice as big as it needs to be."

reply via email to

[Prev in Thread] Current Thread [Next in Thread]