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

Re: Bitfields accesses (causing problems to some ARM hardware)


On Thu, May 18, 2000 at 06:24:50PM -0700, Igor Shevlyakov wrote:
> > Use a union of the struct and an int for a temporary variable.
> > Declare the I/O port as volatile int.  To read the port, assign its value
> > to the int part of the union, and then look at the various fields in your
> > temporary variable.  Likewise, to write, assemble the value in your
> > temporary
> > variable, and then assign it as an integer to the port register.
> 
> It's easy but looks very heavy in code there are a lot of playing with those
> bitfields.  Actualy I was looking for way to change structure definition but
> leave all accesses as is.

That's all well and good, except that what you want isn't C,
and never has been. 

Further, I can't think of any sensible way do create such an
extension that would be generally useful.  For instance, how
might you tell the difference between bitfields in a 64-bit
word or a 32-bit word on a 64-bit machine talking to 32-bit
external hardware?  Yeah, maybe you could hack something that
would be functional, but it would certainly be less than obvious.

I suggest your customer use accessor macros:

typedef struct s
{
  volatile int f1:2,
      f2:6,
      f3:3,
      f4:5,
      f5:8,
      f6:7,
      f7:1;
} S;

#define GET_FIELD(TYPE, FIELD, PTR)		\
  ({ union { int i; TYPE s; } u_;		\
     u_.i = *(volatile int *)(PTR);		\
     u_.s.FIELD; })

#define SET_FIELD(TYPE, FIELD, PTR, VAL)	\
  do { union { int i; TYPE s; } u_;		\
     u_.i = *(volatile int *)(PTR);		\
     u_.s.FIELD = (VAL);			\
     *(volatile int *)(PTR) = u_.i;		\
  } while (0)

int f(S *p)
{
    SET_FIELD(S, f5, p, 0);
    SET_FIELD(S, f3, p, 0);
}


r~

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