This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: RFC: PATCH to layout_decl for middle-end/27945


> On investigation, it seems that we already optimize loads from a packed
> field that gets the appropriate alignment from its position in the struct.

That's true for direct loads, i.e. for a packed field that happens to be 
sufficiently aligned in a object.  But the testcase (attached) is about 
indirect loads, where we only have type alignment information.

Compile it for SPARC/Solaris with -gnatR1 and you get:

for Time_T'Size use 32;
for Time_T'Alignment use 4;
for Time_T use record
   Hour at 0 range  0 .. 31;
end record;

for Date_And_Time_T'Size use 64;
for Date_And_Time_T'Alignment use 1;
for Date_And_Time_T use record
   Date at 0 range  0 .. 31;
   Time at 4 range  0 .. 31;
end record;

So, given that the parameters are passed by reference

   procedure
     Assign_Hour_Of (T : out Time_T)
   is
   begin
      T.Hour := 44;
   end;

   procedure
     Clobber_Hour_Of (DT: out Date_And_Time_T)
   is
   begin
      Assign_Hour_Of (Dt.Time);
   end;

&Dt.Time is not aligned enough as a pointer to Time_T so, on a strict 
alignment platform, you get a temporary:

P.Clobber_Hour_Of (dt)
{
  struct p__time_t D.243;
  struct p__time_t D.244;

  D.244 = dt->time;
  D.243 = D.244;
  p__assign_hour_of (&D.243);
  dt->time = D.243;
  return;
}


Without the pragma Pack on the record:

for Time_T'Size use 32;
for Time_T'Alignment use 4;
for Time_T use record
   Hour at 0 range  0 .. 31;
end record;

for Date_And_Time_T'Size use 64;
for Date_And_Time_T'Alignment use 4;
for Date_And_Time_T use record
   Date at 0 range  0 .. 31;
   Time at 4 range  0 .. 31;
end record;

P.Clobber_Hour_Of (dt)
{
  struct p__time_t * D.243;

  D.243 = &dt->time;
  p__assign_hour_of (D.243);
  return;
}

> It seems that the functionality you want makes the packed directive on
> Date_And_Time_T do nothing.

In this case, yes, because the record is effectively packed by default.

> Is it the case that for Ada you only want the packed directive to affect the
> fields, not the record as a whole? 

I don't think so.  Here's the relevant except of the Ada RM (13.2(6)):

"If a type is packed, then the implementation should try to minimize storage 
allocated to objects of the type, possibly at the expense of speed of 
accessing components, subject to reasonable complexity in addressing 
calculations."

-- 
Eric Botcazou
with Ada.Text_IO; use Ada.Text_IO;

procedure P is

   type Time_T is record
      Hour : Integer;
   end record;

   type Date_And_Time_T is record
      Date : Integer;
      Time : Time_T;
   end record;
   pragma Pack(Date_And_Time_T);

   procedure
     Assign_Hour_Of (T : out Time_T)
   is
   begin
      T.Hour := 44;
   end;

   procedure
     Clobber_Hour_Of (DT: out Date_And_Time_T)
   is
   begin
      Assign_Hour_Of (Dt.Time);
   end;

   DT : Date_And_Time_T;

begin
   DT.Time.Hour := 22;
   Clobber_Hour_Of (DT);
   Put_Line (Integer'Image(DT.Time.Hour));
end;

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]