register unions

Mike McNelly mike at hpfclq.HP.COM
Thu Feb 25 03:00:42 AEST 1988


Register unions are indeed useful and implementable.  I implemented them
in a pcc based compiler a couple of years ago primarily for our internal
use.  Many programs show negligible speed improvement but some,
particularly those using yacc output gained about 10% speed and space
improvement.  As a consequence we use register unions to generate our
compilers and our assembler, which is also yacc based.

There are difficulties that I never bothered to resolve in our
implementation.  The most obvious one involves multiple assignment.

	e.g.
		register union foo
			{
			int i;
			float f;
			int *ip;
			} a,b,c;

		a.i = b.f = (int)c.ip = ...;

After a series of multiple assignments the internal representation
within pcc becomes very messy.  Since the implementation was for an
internal tool only it was easier to avoid the problem than add lots of
code to fix it. The situation is very rare and not worth slowing the 
compiler down to handle an occasional occurrence.

You may also have problems deciding which register class in which to put
the union.  For the above example, it may be wiser to locate the union
in a data register (MC68000 family) unless the pointer member is to be
accessed frequently in which case an address register may be more
appropriate.  If you guess wrong the resulting code can actually be
somewhat slower than if the union is in memory.  We chose to use the
first union member type to be a clue.


Mike McNelly	hplabs!hpfcla!mike
Hewlett Packard Company



More information about the Comp.lang.c mailing list