[net.unix-wizards] Various C replies

KENDALL@Harv-10@sri-unix (09/01/82)

Date: 26 Aug 1982 (Thursday) 2158-EST
Re: pcc bug.

mit-vax!mp's (Mark Plotnick's?) letter in UW 3 Aug 82 reported a pcc
crash on the rather odd program:

	int a();
	int (*buf)() = (int (*)()) ((int)a | 0x80000000);
	main() {}
	a() {}

As he says, the pcc does generate almost 2000 move instructions before
realizing it is in a loop.  This is strange behavior, but the pcc has a
partial excuse:  the Ritchie compilers won't compile this program
either.  It's really not a legal program.

The problem is that the expression on line 2 asks the compiler to use
the address of `a' as an integer constant even though the compiler does
not (at this point in the program text) know the address of `a'.  Here
is a simpler example:

	extern int ei;
	int i1 = (int) &ei + 1;		/* works */
	int i2 = (int) &ei | 1;		/* doesn't work */

The linker will handle addition of a signed constant to an address, but
no other operation on addresses.  The initialization value of a static
location must be a compile-time (which includes link-time) constant;
thus other operations on unknown addresses will not compile.

-------------

Re: Article about C
Short review of article in December Sigplan Notices:  Patrick Fitzhorn
and Gerold Johnson, "C:  Toward a Concise Syntactic Description".

The authors have attempted to improve the grammar from the C Reference
Manual, seemingly without much understanding of the issues involved.
Here are a few of the mistakes that pervade their paper:
(1) "main" is incorrectly a reserved word in their grammar.
(2) They have attempted to check for illegal declarations (illegal
combination of storage class and type, for example) in their grammar,
not realizing that typedef'd type names make this check impossible to
include in the grammar.  (Their treatment of typedef makes it clear that
they understand neither its syntax nor its semantics.)
(3) They incorrectly fault the Reference Manual syntax for the switch
statement, because they fail to understand that case is simply a
special form of label.  In their grammar, the statement attached to a
switch statement must be compound; a case label must be the first thing
in that compound statement; the default label, if given, must follow
all other case labels.  These three restrictions, and many others in
their grammar, are wrong.

Fitzhorn and Johnson seem to have a naive faith in the power of
context-free grammars to solve language specification problems,
problems which C certainly has; but CFGs alone cannot solve such
problems even when well-handled.  (I recommend that the authors
investigate the two-level grammar specification of Algol 68 if they are
interested in powerful grammars.)

------------------

Re: "Horrid 'float' code in 4.1BSD C compiler."

I believe the problem is that the pcc, on which that compiler is based,
has trouble dealing with three-operand instructions.  Thus it does not
generate the obvious best one for many situations.  I have heard that
the peephole optimizer fixes up the code for integers; I guess it
doesn't for floats.  C compilers have always been very weak on code
generation and register assignment for floats.

------------------

Re: "VAX System III PCC Question" and "C sizeof" (sizeof structs on the VAX)

As far as I know, the VAX C compilers are written under the assumption
that you want the speed that comes with aligning objects of various
sizes, rather than the storage savings that come with not aligning.
Thus with

	struct { char x1; int x2; };

three padding bytes are left after the char to properly align the int,
and the sizeof is 8.  But

	sizeof (struct { int i; char c; })

is also 8, even though the padding is after the useful data.  This is
because sizeof returns the number of bytes that an object must occupy
in an array in order to keep all the elements aligned.

					Sam Kendall
					kendall @ Harv-10