Question: How to fix structs on broken compiler (Byron A Jeff)
Thu, 29 Oct 1992 12:19:26 GMT

          From comp.compilers

Related articles
Question: How to fix structs on broken compiler (1992-10-29)
| List of all articles for this month |

Newsgroups: comp.lang.c,comp.sys.m68k,comp.sys.amiga.programmer,comp.compilers
From: (Byron A Jeff)
Organization: Georgia Institute of Technology
Date: Thu, 29 Oct 1992 12:19:26 GMT
Followup-To: comp.lang.c,comp.compilers
Keywords: C, question, comment

I'm using the public domain amiga C compiler HCC to do software
development for the 68000 family single board computers I build. I've
found a bug in it and I'd like to get some opinions on the best way to fix
it. The problem is alignment of fields in a structure.

For your reference this compiler produces 4 byte longs and pointers, 2
byte ints, and of course 1 byte chars.

Suppose we have a structure definition like this. It is from an assembler
I'm porting.

struct mne{ /* a mnemonic */
char *mne_name;
char ntmpl; /* number of templates to test */
struct tmpl *ptmpl; /* pointer to templates to test */

Note that the char (1 byte) is stuck right inbetween 2 four byte pointers.
The assembly code produced for one of these initialized structs defined
something like this:

align 2
dc.l #_name
dc.b 3
dc.l #_template+20

The problem is that some 68000 family CPUs require that word and longword
accesses be word aligned. The compiler word aligns the structure but does
not word align the 3rd field.

The immediate solution (which I took) is to make the 2nd field an integer
which keeps everything word aligned. I did that and it worked.

But the problem runs a bit deeper. While the initializer routine did not
align the 3rd field, the code that accesses the field assumed that field
was in fact aligned. Code to this effect:

x.ptmpl = template+1;


movea #x,A0
move.l #template+20,6(A0)

which given the first initialization is plain wrong.

So my problem is how to fix it. I know that some code assumes that structs
are packed. An example is a structure definition for a device where a
pointer to a structure is assigned the address of the device and the
device is accesed through the pointer. I do it all the time. It requires
that structures be packed.

On the other hand at the expense of some space a properly aligned
structure can give you a performance improvement. I'm working on a 68040
board with a full 32 bit data bus. If long words are long word aligned
then I can access their contents in memory with a single access.

The good thing is that I have the source code for the compiler and can
change it.

So my questions:

1. Should I properly align all struct fields? Or should I just align enough
      so that the machine doesn't crash.

2. If I do properly align everything should I warn the user?

3. Is there any defined way of saying that a structure should be aligned or

4. How would you fix this bug?

I think that comp.lang.c is the proper place for followups. I crossposted
the initial message because I think these are the folks who would be
interested in the issue.

Thanks a bunch.

Another random extraction from the mental bit stream of...
Byron A. Jeff - PhD student operating in parallel!
Georgia Tech, Atlanta GA 30332 Internet:
[The standard specifically avoids making any promises about field
alignment in structures. An approach used on some PC C compilers is to
align fields by default, but to use a ``#pragma pack'' to tell the
compiler not to insert any padding. -John]

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.