Short code to determine compiler's

Paul Hudson paul at moncam.co.uk
Wed Jul 19 20:31:45 AEST 1989


In article <579 at targon.UUCP> andre at targon.UUCP (andre) writes:
   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 hope this doesn't work either. I would expect the compiler to
complain if I took the address of a variable I had declared register,
regardless of whether it was in a register or not. At the time the
compiler emits such errors it may not even know whether the variable
will get a register or not.


--
Paul Hudson	 MAIL: Monotype ADG, Science Park, Cambridge, CB4 4FQ, UK.
		PHONE: +44 (223) 420018	  EMAIL: paul at moncam.co.uk,
	;"	  FAX: +44 (223) 420911		 ...!ukc!acorn!moncam!paul
 `"";";"        These opinions void where prohibited by law.



More information about the Comp.lang.c mailing list