Short code to determine compiler's register use

Greg Onufer greg at uop.EDU
Thu Jul 13 11:03:17 AEST 1989


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!
We are interested in hearing about the results of this program when
run on different architectures of machines and when compiled by
different compilers, with and without optimization.  The GNU C
compiler should not be used, it will always return 19 (the max
number of registers the program was written to find...), optimization
on or off.

Please reply to:
Cheers!greg  (cheers!greg at Apple.COM, cheers!greg at lll-winken.llnl.gov)

-------------------------------------------------------------------
/*
 * Code to determine the number of registers a C compiler can use.
 * Compile WITHOUT optimization!!
 * G. Onufer / D. Christensen   5/12/89, early morning.
 */

#include <stdio.h>

main()
{
    {
        int count1; /* Base of stackframe */

        register x1, x2, x3, x4, x5, x6, x7, x8, x9, x10,
                 x11, x12, x13, x14, x15, x16, x17, x18, x19;
        {
            int count2;
            printf("Number of data registers = %d\n", 19 - abs(&count1 - &count2) + 1);
        }
    }
    {
        int count1;

        register *x1, *x2, *x3, *x4, *x5, *x6, *x7, *x8,
                 *x9, *x10, *x11, *x12, *x13, *x14, *x15;
        register *x16, *x17, *x18, *x19;
        {
            int count2; /* Base of new stackframe */
            printf("Number of address registers = %d\n", 19 - abs(&count1 - &count2) + 1);
        }
    }
}
======================================================================



More information about the Comp.lang.c mailing list