Short code to determine compiler's

andre andre at targon.UUCP
Tue Jul 18 02:13:43 AEST 1989


In article <225800197 at uxe.cso.uiuc.edu> mcdonald at uxe.cso.uiuc.edu writes:
>
>>Some students here had to determine the number of registers (data 
>>and address, we use 680x0's) the C compiler uses.  A friend and
>>I wrote the following code to show to some students having trouble.
>>It is very short and simple, but it seems to work.  The only logical
>>next step is to post it to comp.lang.c and have it torn apart!

[ text explaining why this doesn't always work ]

>In fact, the really interesting question is, in legal C, is it
>even POSSIBLE to write a program to see how many registers are used?

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.

	I would like to know if this is also possible at run-time,
		Andre

-- 
    \---|    AAA         DDDD  It's not the kill, but the thrill of the chase.
     \  |   AA AAvv   vvDD  DD        Ketchup is a vegetable.
  /\  \ |  AAAAAAAvv vvDD  DD                    {nixbur|nixtor}!adalen.via
_/__\__\| AAA   AAAvvvDDDDDD    Andre van Dalen, uunet!hp4nl!targon!andre



More information about the Comp.lang.c mailing list