ARM bare-metal programming in D (cont) - volatile

Iain Buclaw ibuclaw at ubuntu.com
Thu Oct 24 15:52:46 PDT 2013


On 24 October 2013 07:36, Iain Buclaw <ibuclaw at ubuntu.com> wrote:
> On 24 October 2013 06:37, Walter Bright <newshound2 at digitalmars.com> wrote:
>> On 10/23/2013 5:43 PM, Mike wrote:
>>>
>>> I'm interested in ARM bare-metal programming with D, and I'm trying to get
>>> my
>>> head wrapped around how to approach this.  I'm making progress, but I
>>> found
>>> something that was surprising to me: deprecation of the volatile keyword.
>>>
>>> In the bare-metal/hardware/driver world, this keyword is important to
>>> ensure the
>>> optimizer doesn't cache reads to memory-mapped IO, as some hardware
>>> peripheral
>>> may modify the value without involving the processor.
>>>
>>> I've read a few discussions on the D forums about the volatile keyword
>>> debate,
>>> but noone seemed to reconcile the need for volatile in memory-mapped IO.
>>> Was
>>> this an oversight?
>>>
>>> What's D's answer to this?  If one were to use D to read from
>>> memory-mapped IO,
>>> how would one ensure the compiler doesn't cache the value?
>>
>>
>> volatile was never a reliable method for dealing with memory mapped I/O.
>
> Are you talking dmd or in general (it's hard to tell).  In gdc,
> volatile is the same as in gcc/g++ in behaviour.  Although in one
> aspect, when the default storage model was switched to thread-local,
> that made volatile on it's own pointless.
>
> As a side note, 'shared' is considered a volatile type in gdc, which
> differs from the deprecated keyword which set volatile at a
> decl/expression level.  There is a difference in semantics, but it
> escapes this author at 6.30am in the morning.  :o)
>

To elaborate (now I am a little more awake :) - 'volatile' on the type
means that it's volatile-qualified.  volatile on the decl means it's
treated as volatile in the 'C' sense.

What's the difference?  Well for the backend a volatile type only
really has an effect on function returns  (eg: a function that returns
a shared int may not be subject for use in, say, tail-call
optimisations).  GDC propagates the volatile flag of the type to the
decl, so that there is effectively no difference between shared and
volatile, except in a semantic sense in the D frontend language
implementation.


Regards
-- 
Iain Buclaw

*(p < e ? p++ : p) = (c & 0x0f) + '0';


More information about the Digitalmars-d mailing list