comp.lang.ada
 help / color / mirror / Atom feed
* Problem with representation clause in gnat 4.3
@ 2008-10-03 22:08 Michael Bode
  2008-10-04  2:36 ` Randy Brukardt
  2008-10-04 10:10 ` Per Sandberg
  0 siblings, 2 replies; 5+ messages in thread
From: Michael Bode @ 2008-10-03 22:08 UTC (permalink / raw)


Hi,

I've a problem with a representation that I don't understand. And I'm
pretty sure older versions of gnat than 4.3 had a different behaviour.

This stuff compiles as I expect with gnat 4.3 (on Debian Lenny):

with Ada.Text_Io;

procedure Align is

   type Compound is record
      A : Boolean;
      B : Integer;
      C : Integer;
   end record;
   for Compound use record
      A at 0 range 0 .. 0;
      B at 2 range 0 .. 31;
      C at 6 range 0 .. 31;
   end record;

   type C_Array is array (Natural range <>) of Compound;
   for C_Array'Component_Size use 80;

   Size : Natural := Compound'Size;
begin
   Ada.Text_Io.Put_Line ("Compound has size" & Natural'Image(Size));
end Align;

~/myprogs/test $ gnatmake align
gcc-4.3 -c align.adb
gnatbind -x align.ali
gnatlink align.ali
~/myprogs/test $ ./align 
Compound has size 80
~/myprogs/test $ 

Now if I move component A of the record to the end as in
...
   type Compound is record
      B : Integer;
      C : Integer;
      A : Boolean;
   end record;
   for Compound use record
      B at 0 range 0 .. 31;
      C at 4 range 0 .. 31;
      A at 8 range 0 .. 0;  
   end record;
...

~/myprogs/test $ gnatmake align
gcc-4.3 -c align.adb
align.adb:16:09: component size for "C_Array" too small, minimum allowed is 96
gnatmake: "align.adb" compilation error
~/myprogs/test $ 

And if i remove the line 'for C_Array'Component_Size use 80;' so the
program compiles I get

~/myprogs/test $ gnatmake align
gcc-4.3 -c align.adb
gnatbind -x align.ali
gnatlink align.ali
~/myprogs/test $ ./align 
Compound has size 65

So we see Compound should definitivly fit into 80 bits.

Obviously gnat wants to align to DWORD boundaries in the 2nd case but
not in the 1st. Why and what can I do about it?



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem with representation clause in gnat 4.3
  2008-10-03 22:08 Problem with representation clause in gnat 4.3 Michael Bode
@ 2008-10-04  2:36 ` Randy Brukardt
  2008-10-04 12:36   ` Michael Bode
  2008-10-04 10:10 ` Per Sandberg
  1 sibling, 1 reply; 5+ messages in thread
From: Randy Brukardt @ 2008-10-04  2:36 UTC (permalink / raw)


"Michael Bode" <m.g.bode@web.de> wrote in message 
news:gc655v$702$1@online.de...
...
> Obviously gnat wants to align to DWORD boundaries in the 2nd case but
> not in the 1st. Why and what can I do about it?

I can't answer the why (you'd have to ask the GNAT implementers that), but 
the usual fix is to give the size of the record type:

    for Compound'Size use 80;

We did that for all of the interface record types in Claw, because otherwise 
compilers tended to come up with different answers (and that didn't work 
very well). For instance, Janus/Ada would have used Size = 72 for this 
record (which wouldn't matter in this case, but might in some 
circumstances).

                                   Randy.





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem with representation clause in gnat 4.3
  2008-10-03 22:08 Problem with representation clause in gnat 4.3 Michael Bode
  2008-10-04  2:36 ` Randy Brukardt
@ 2008-10-04 10:10 ` Per Sandberg
  2008-10-04 12:37   ` Michael Bode
  1 sibling, 1 reply; 5+ messages in thread
From: Per Sandberg @ 2008-10-04 10:10 UTC (permalink / raw)


Hi
Think this has to do with the implicit alignment of the record, in the 
first case the integer (integers has a default alignment of 4) 
components are aligned to word boundaries breaking their default 
alignment resulting in an alignment of 2 for the record as whole.
In the second case the integers are aligned to dword boundaries in the 
record resulting in a propagation of the default alignment of 4 for the 
integers the record.

So if you add an alignment clause "for Compound'Alignment use 2" the 
compiler will do what you want.

Haven't tested it since I don't run "Debian Lenny".

/Hope it helps
/Per

Michael Bode wrote:
> Hi,
> 
> I've a problem with a representation that I don't understand. And I'm
> pretty sure older versions of gnat than 4.3 had a different behaviour.
> 
> This stuff compiles as I expect with gnat 4.3 (on Debian Lenny):
> 
> with Ada.Text_Io;
> 
> procedure Align is
> 
>    type Compound is record
>       A : Boolean;
>       B : Integer;
>       C : Integer;
>    end record;
>    for Compound use record
>       A at 0 range 0 .. 0;
>       B at 2 range 0 .. 31;
>       C at 6 range 0 .. 31;
>    end record;
> 
>    type C_Array is array (Natural range <>) of Compound;
>    for C_Array'Component_Size use 80;
> 
>    Size : Natural := Compound'Size;
> begin
>    Ada.Text_Io.Put_Line ("Compound has size" & Natural'Image(Size));
> end Align;
> 
> ~/myprogs/test $ gnatmake align
> gcc-4.3 -c align.adb
> gnatbind -x align.ali
> gnatlink align.ali
> ~/myprogs/test $ ./align 
> Compound has size 80
> ~/myprogs/test $ 
> 
> Now if I move component A of the record to the end as in
> ....
>    type Compound is record
>       B : Integer;
>       C : Integer;
>       A : Boolean;
>    end record;
>    for Compound use record
>       B at 0 range 0 .. 31;
>       C at 4 range 0 .. 31;
>       A at 8 range 0 .. 0;  
>    end record;
> ....
> 
> ~/myprogs/test $ gnatmake align
> gcc-4.3 -c align.adb
> align.adb:16:09: component size for "C_Array" too small, minimum allowed is 96
> gnatmake: "align.adb" compilation error
> ~/myprogs/test $ 
> 
> And if i remove the line 'for C_Array'Component_Size use 80;' so the
> program compiles I get
> 
> ~/myprogs/test $ gnatmake align
> gcc-4.3 -c align.adb
> gnatbind -x align.ali
> gnatlink align.ali
> ~/myprogs/test $ ./align 
> Compound has size 65
> 
> So we see Compound should definitivly fit into 80 bits.
> 
> Obviously gnat wants to align to DWORD boundaries in the 2nd case but
> not in the 1st. Why and what can I do about it?



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem with representation clause in gnat 4.3
  2008-10-04  2:36 ` Randy Brukardt
@ 2008-10-04 12:36   ` Michael Bode
  0 siblings, 0 replies; 5+ messages in thread
From: Michael Bode @ 2008-10-04 12:36 UTC (permalink / raw)


"Randy Brukardt" <randy@rrsoftware.com> writes:

> I can't answer the why (you'd have to ask the GNAT implementers that), but 
> the usual fix is to give the size of the record type:
>
>     for Compound'Size use 80;

Thanks for answering, but it doesn't work with gnat. But the other tip
to use the alignment clause works.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem with representation clause in gnat 4.3
  2008-10-04 10:10 ` Per Sandberg
@ 2008-10-04 12:37   ` Michael Bode
  0 siblings, 0 replies; 5+ messages in thread
From: Michael Bode @ 2008-10-04 12:37 UTC (permalink / raw)


Per Sandberg <per.sandberg@bredband.net> writes:

> So if you add an alignment clause "for Compound'Alignment use 2" the
> compiler will do what you want.

Thanks, that works fine.



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2008-10-04 12:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-10-03 22:08 Problem with representation clause in gnat 4.3 Michael Bode
2008-10-04  2:36 ` Randy Brukardt
2008-10-04 12:36   ` Michael Bode
2008-10-04 10:10 ` Per Sandberg
2008-10-04 12:37   ` Michael Bode

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