comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Forcing GNAT to use 32-bit load/store instructions on ARM?
Date: Wed, 02 Jul 2014 00:01:35 +0300
Date: 2014-07-02T00:01:35+03:00	[thread overview]
Message-ID: <c1glt9F8dvoU1@mid.individual.net> (raw)
In-Reply-To: <lov6bu$j0s$1@dont-email.me>

On 14-07-01 23:40 , Simon Clubley wrote:
> On 2014-07-01, daniel.dmk@googlemail.com <daniel.dmk@googlemail.com> wrote:
>> Thank you everyone for your suggestions and comments. 
>>
>> On Tuesday, 1 July 2014 13:03:30 UTC+1, Simon Clubley  wrote:
>>>
>>> Pragma Atomic _should_ have given you the guarantee you needed.

Unfortunately not, see below.

>>> Are you double checked how it's being used in the code ?
>>
>> So I've set up a project to demonstrate only this problem. I've
>> created a package called "Test" which contains the structure
>> definition, and the definition of the register itself at the
>> fixed address:
>>
>>    with System;
>>
>>    package Test is
>>       type Bits_1 is mod 2**1 with Size => 1;
>>       type Bits_2 is mod 2**2 with Size => 2;
>>       type Bits_28 is mod 2**28 with Size => 28;
>>       
>>       type CR_Register is
>>          record
>>             Reserved_1 : Bits_2;
>>             RNGEN      : Bits_1;
>>             IE         : Bits_1;
>>             Reserved_2 : Bits_28;
>>          end record;
>>       for CR_Register use
>>          record
>>             Reserved_1 at 0 range 0 .. 1;
>>             RNGEN      at 0 range 2 .. 2;
>>             IE         at 0 range 3 .. 3;
>>             Reserved_2 at 0 range 4 .. 31;
>>          end record;
>>       for CR_Register'Size use 32;
>>
>>       CR : CR_Register with
>>         Volatile,
>>           Atomic,
>>           Address => System'To_Address(16#5006_0800#);
>>    end Test;
>>
>> So I've now defined the reserved bits in the register, and I have
>> added "Atomic" to the CR register.
>>
>> In my main procedure I have the following:
>>
>>    with System;
>>    with Test;
>>
>>    procedure Main
>>      with SPARK_Mode => On
>>    is
>>       pragma Priority(System.Priority'First);
>>       
>>    begin
>>       
>>       Test.CR.RNGEN := 1;
>>       
>>       loop
>>          null;
>>       end loop;
>>
>>    end Main;
>>
>> The line where I assign the RNGEN bit produces the following
>> assembly code (using no optimization: -O0):
>>
>>    mov.w   r3, #2048       ; 0x800
>>    movt    r3, #20486      ; 0x5006
>>    ldrb    r2, [r3, #0]
>>    orr.w   r2, r2, #4
>>    strb    r2, [r3, #0]
>>
> 
> This is the exact same issue I encountered when I tried to use bitfields
> in C instead of bitmasks. The difference here is that Ada's Atomic pragma
> is supposed to stop this type of code from being generated.

Actually not... as I also forgot... because the Ada rule is RM C.6(15):

"For an atomic object (including an atomic component) all reads and
updates of the object as a whole are indivisible."

Note the part "as a whole". If you access a component of an atomic
record, that is not accessing the record "as a whole".

>> For reference, here's the code when I use a temporary copy of the register:
>>
>>    declare
>>       Temp : Test.CR_Register;
>>    begin
>>       Temp := Test.CR;
>>       Temp.RNGEN := 1;
>>       Test.CR := Temp;
>>    end;

This code accesses the record as a whole, and therefore...

> This is exactly the kind of ARM code I would expect to see generated.
> 
>> In this case, it is always using the word load/store instructions
>> (ldr and str).

>> So could there be a problem with GNAT's Atomic on ARM?
>>
> 
> Yes, big time.

No. The Atomic guarantee only applies to read/write of the whole record.
My earlier post in this thread was in error.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
      .      @       .


  parent reply	other threads:[~2014-07-01 21:01 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-30 22:11 Forcing GNAT to use 32-bit load/store instructions on ARM? daniel.dmk
2014-06-30 23:41 ` Jeffrey Carter
2014-07-01 12:06   ` Simon Clubley
2014-07-01 15:44     ` Niklas Holsti
2014-07-01 17:26       ` Simon Clubley
2014-07-01 17:18     ` Simon Wright
2014-07-01 19:43       ` Simon Wright
2014-07-01 17:28     ` Jeffrey Carter
2014-07-01  0:55 ` anon
2014-07-01  4:30 ` Niklas Holsti
2014-07-01  8:11 ` Dmitry A. Kazakov
2014-07-01 12:09   ` Simon Clubley
2014-07-01 12:20     ` Dmitry A. Kazakov
2014-07-01 17:00       ` Simon Clubley
2014-07-01 19:36         ` Dmitry A. Kazakov
2014-07-01 20:08           ` Simon Clubley
2014-07-02 22:24             ` Randy Brukardt
2014-07-06 20:40               ` MatthiasR
2014-07-07  0:25                 ` Simon Clubley
2014-07-07 22:38                 ` Randy Brukardt
2014-07-08  6:51                   ` Simon Wright
2014-07-10 11:47                     ` Simon Wright
2014-07-10 13:06                       ` Simon Clubley
2014-07-11 18:05                         ` Simon Wright
2014-07-11 20:22                           ` Simon Clubley
2014-07-08  8:50                   ` Brian Drummond
2014-07-08 12:12                   ` Simon Clubley
2014-07-08 13:26                     ` G.B.
2014-07-08 17:13                       ` Simon Clubley
2014-07-08 15:36                     ` Adam Beneschan
2014-07-08 15:40                       ` Adam Beneschan
2014-07-08 20:34                     ` Randy Brukardt
2014-07-09  7:31                       ` Dmitry A. Kazakov
2014-07-10  0:11                         ` Simon Clubley
2014-07-20 11:35                   ` MatthiasR
2014-07-20 15:49                     ` Simon Clubley
2014-07-26 11:05                       ` MatthiasR
2014-08-10 11:20                         ` MatthiasR
2014-07-01 12:03 ` Simon Clubley
2014-07-01 19:52   ` daniel.dmk
2014-07-01 20:40     ` Simon Clubley
2014-07-01 20:55       ` Simon Clubley
2014-07-01 21:01       ` Niklas Holsti [this message]
2014-07-01 21:20         ` Simon Clubley
2014-07-01 22:38           ` Niklas Holsti
2014-07-02 16:49             ` Simon Clubley
2014-07-01 21:55         ` daniel.dmk
2014-07-02  7:30     ` Simon Wright
2014-07-02 18:52       ` daniel.dmk
2014-07-04 23:51       ` Niklas Holsti
2014-07-05  0:18         ` Niklas Holsti
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox