[Info-vax] Volatile, was: Re: yet another sys$qiow question

Simon Clubley clubley at remove_me.eisner.decus.org-Earth.UFP
Wed Aug 19 09:31:14 EDT 2015


On 2015-08-19, Johnny Billquist <bqt at softjar.se> wrote:
> On 2015-08-19 14:36, Jan-Erik Soderholm wrote:
>
>> Now in the embedded world we might have:
>>
>> loop [some condition]
>>    VarB = PORTA
>> endloop
>>
>> where PORTA is a hardware port (I/O pins) on the processor.
>>
>> If PORTA is not volatile, the compiler can do a single read
>> of PORTA and then use a value saved in a processor register.
>> With volatile, the hardware I/O port will be read each time.
>>
>> The "value" of PORTA can of course change at any time depending
>> on what happens in the hardware around the processor.
>
> Right. Except that it would also move the assignment out of the loop if 
> it wasn't volatile, as there is no point in repeating a constant assignment.
>

Do you mean if VarB wasn't volatile and if [some condition] didn't
reference a volatile variable ?

Don't forget that reads from a device can have side effects as well
so the above loop might be required as a loop.

I'm not sure if gcc would ever move the assignment out of the loop
because of this as I thought the rule was that the compiler must
generate code to do a volatile read or write exactly the same number
of times as it is indicated within the source code.

Simon.

-- 
Simon Clubley, clubley at remove_me.eisner.decus.org-Earth.UFP
Microsoft: Bringing you 1980s technology to a 21st century world



More information about the Info-vax mailing list