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

Daniel Murphy yebblies at nospamgmail.com
Thu Oct 24 04:50:57 PDT 2013


"Mike" <none at none.com> wrote in message 
news:bifrvifzrhgocrejepvc at forum.dlang.org...
> 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?

There are a few options:

1. Use shared in place of volatile.  I'm not sure this actually works, but 
otherwise this is pretty good.

2. Use the deprecated volatile statement.  D got it right that volatile 
access is a property of the load/store and not the variable, but missed the 
point that it's a huge pain to have to remember volatile at use.  Could be 
made better with a wrapper.  I think this still works.

3. Use inline assembly.  This sucks.

4. Defeat the optimizer with inline assembly.

asm { nop; } // Haha, gotcha
*my_hardware_register = 999;
asm { nop; }

This might be harder with gdc/ldc than it is with dmd, but I'm pretty sure 
there's a way to trick it into thinking an asm block could clobber/read 
arbitrary memory.

5. Lobby for/implement some nice new volatile_read and volatile_write 
intrinsics.

Old discussion:
http://www.digitalmars.com/d/archives/digitalmars/D/volatile_variables_in_D...._51984.html 




More information about the Digitalmars-d mailing list