From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 12466 invoked by alias); 22 May 2009 06:56:28 -0000 Received: (qmail 12455 invoked by uid 22791); 22 May 2009 06:56:27 -0000 X-SWARE-Spam-Status: No, hits=-1.8 required=5.0 tests=AWL,BAYES_00,SARE_MSGID_LONG40,SPF_PASS X-Spam-Check-By: sourceware.org Received: from rv-out-0708.google.com (HELO rv-out-0708.google.com) (209.85.198.244) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 22 May 2009 06:56:22 +0000 Received: by rv-out-0708.google.com with SMTP id l33so500814rvb.56 for ; Thu, 21 May 2009 23:56:20 -0700 (PDT) MIME-Version: 1.0 Received: by 10.141.29.14 with SMTP id g14mr1660666rvj.150.1242975380082; Thu, 21 May 2009 23:56:20 -0700 (PDT) In-Reply-To: References: From: Mohamed Shafi Date: Fri, 22 May 2009 08:01:00 -0000 Message-ID: Subject: Re: structure packing To: Ian Lance Taylor Cc: gcc-help@gcc.gnu.org, Rohit Arul Raj Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2009-05/txt/msg00261.txt.bz2 2009/5/22 Ian Lance Taylor : > Mohamed Shafi writes: > >> The below code is compiled using a private port based on GCC 4.1.1. >> >> struct data { >> =A0=A0=A0=A0 int wdata; >> =A0=A0=A0=A0 int rdata; >> =A0};// __attribute__((__packed__)); >> >> typedef struct data data; >> #define data_p (*(volatile data *)(0)) >> >> int main(void) >> { >> =A0 data_p.wdata =3D data_p.rdata; >> } >> >> For the above source code without the 'packed' attribute the assembly >> code is generated with word-aligned access. But with packed attribute >> enabled the assembly code is generated with byte access. When i looked >> into x86 compiler i find that the assembly code generated is same >> irrespective of whether the attribute is provided or not. > > I don't see this. =A0When I compile the above test case (i.e., the struct > is not packed) on x86, I see word accesses. > Sorry for the confusion. What i meant was that irrespective of whether the structure is packed or not the code generated is for word access. It should be because the compiler is built with STRICT_ALIGNMENT =3D 0. But in my compiler it is byte access for packed structure and my compiler is built with STRICT_ALIGNMENT =3D 1. >> In case, >> 1) if the packing is enabled and all the data elements inside the >> struct are by default word-aligned (all the 32bit integers) is it >> possible to generate word-aligned assembly code? > > No. =A0The packed attribute tells the compiler that the struct may be > misaligned. I thought so. With STRICT_ALIGNMENT =3D 1 the compiler makes sure that all access are aligned. So irrespective of whether the structure is aligned or not to be safe it generates byte access. Am i right? If so in the given example the base address is 0 and the offset will be known to the compiler. If the compiler is aware of these two information can't it decide whether the structure is aligned or not? Shafi > >> 2) Packing is enabled for a structure with different data elements. >> >> i.e. >> struct data { >> =A0=A0=A0=A0 int wdata; >> =A0=A0=A0 char t; >> =A0=A0=A0=A0 int rdata; >> =A0} __attribute__((__packed__)); >> >> Is it possible to generate code like: >> wdata -> word aligned access. >> rdata -> byte aligned access. > > No. =A0You could try putting a packed struct inside the unpacked struct, > though; I don't know whether or not that would work. > > Ian >