This is the mail archive of the gcc-help@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]

struct, union and field alignment in gcc


Hi

I'm trying to figure out a way to pack structs, unions and bitfields
in gcc.  Here are some typedefs:

typedef struct {

unsigned int address : 26;

} FORK;

typedef union {
  FORK fork;
  MERGE merge;
  FEATURE feature;
  INTRA intra;
  TERMINAL term;
  INTER inter;
  TEST test;
} types_u;

typedef struct {
  types_u u;
  unsigned int type : 6;
} ins;

typedef union {
  unsigned long int bytes;
  ins ins;
} instruction;

All of the members in the union types_u define a total of 26 bits so
I want an instruction to always add up to 32 bits aligned like this:

[______][_..._]
6 bits  32 bits

when printing using %o in C (endianness?).  In other words I don't
want ins forced to a byte boundary.  Previously I was using these
typedefs:

typedef struct {

  unsigned int address : 26;
  unsigned int type : 6;

} FORK;

typedef union {
  FORK fork;
  MERGE merge;
  FEATURE feature;
  INTRA intra;
  TERMINAL term;
  INTER inter;
  TEST test;
  unsigned long int bytes;
} types_u;

However every member of types_u except bytes has a six bit type field
so there's a lot of redundancy in code that uses the latter typedefs.

I know I could do all this with masks and shifting, etc. but I have
some code I am trying to understand and then change which is set up
like this and I want to be able to make make minimal changes to the
typedefs when I do, i.e., the sizes, types and alignment will change
in the structs and unions but the member declarations won't.

Is there a way using compiler flag options or:

__attribute__ ((__packed__))

to get what I want?  BTW it would be nice if possible to be able to
reverse the members in FORK in the latter typedefs and in ins in the
former typedefs (endianness again?).

I apologise if I've missed something obvious in C or in gcc.  BTW I'm
running on an Intel x86 machine under F16.  The version information
is:

gcc (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2)

Thanks in advance,
Mike


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