Object file questions

Artur Skawina via D.gnu d.gnu at puremagic.com
Sat Aug 16 13:00:50 PDT 2014


On 08/16/14 20:40, Artur Skawina wrote:
>> How can I use this with struct members ?
> 
> One possibility would be to declare all members as `Volatile!...`, or

I did not like that required dereference in the previous version,
and tried a different approach:

   struct Timer
   {
       Volatile!uint control;
       Volatile!uint data;
   }

   enum timerA = cast(Timer*)0xDEADBEAF;

   int main() {
      timerA.control |= 0b1;
      timerA.control += 1;
      timerA.control = 42;
      int a = timerA.data - timerA.data;
      int b = timerA.control;
      return timerA.control;
   }

   version (GNU) {
   static import gcc.attribute;
   enum inline = gcc.attribute.attribute("forceinline");
   }
   
   extern int volatile_dummy;
   
   @inline T volatile_load(T)(ref T v) nothrow {
      asm { "" : "+m" v, "+m" volatile_dummy; }
      T res = v;
      asm { "" : "+g" res, "+m" v, "+m" volatile_dummy; }
      return res;
   }

   @inline void volatile_store(T, A)(ref T v, A a) nothrow {
      asm { "" : "+m" volatile_dummy : "m" v; }
      v = a;
      asm { "" : "+m" v, "+m" volatile_dummy; }
   }
   
   struct Volatile(T) {
      T raw;
      nothrow: @inline:
      @disable this(this);
      void opAssign(A)(A a) { volatile_store(raw, a); }
      T load() @property { return volatile_load(raw); }
      alias load this;
      void opOpAssign(string OP)(const T rhs) {
           auto v = volatile_load(raw);
           mixin("v " ~ OP ~ "= rhs;");
           volatile_store(raw, v);
      }
   }


artur


More information about the D.gnu mailing list