Short code to determine compiler's

Scott Horne Horne-Scott at cs.yale.edu
Wed Jul 19 00:15:40 AEST 1989


In article <579 at targon.UUCP>, andre at targon (andre) writes:
> 
> Yes, I think you can write a program that checks the nr of registers,
> but you will get the answer at compile time, not run time :-)
> 
> the program looks like this:
> 
> /* test register usage of compiler */
> 
> main()
> {
> 	register n1, n2, n3, n4, n5, n6, n7, n8; /* etc. */
> 	int *a;
> 
> 	a = &n8;
> 	a = &n7;
> 
> 	/* repeat n6 - n2 */
> 	a = &n1;
> }
> 
> /* end */
> 
> The compiler will assing n1 to n{x} to the registers it has available and
> the rest will be normal variables. You can take the address of a variable
> but not of a register, so the compiler will start to complain at the first
> line that tries to take the address of a register. That's why the a = &n{x};
> lines must count backwards.

But C doesn't guarantee the order of allocation of registers.  How do you know,
for example, that n8, n7, ..., n{x} won't be put into registers and n{x-1}..n1
made automatic?  Your program depends on the order of allocation and therefore
is not reliable.

Besides, the rule that the address of a register variable cannot be taken is
new in K&R 2; some old compilers might return an ``address''.

Nice try, though....  :-)

					--Scott

Scott Horne                              Hacker-in-Chief, Yale CS Dept Facility
horne at cs.Yale.edu                         ...!{harvard,cmcl2,decvax}!yale!horne
Home: 203 789-0877     SnailMail:  Box 7196 Yale Station, New Haven, CT   06520
Work: 203 432-1260              Summer residence:  175 Dwight St, New Haven, CT
Dare I speak for the amorphous gallimaufry of intellectual thought called Yale?



More information about the Comp.lang.c mailing list