[comp.lang.c] Short code to determine compiler's register use

greg@uop.EDU (Greg Onufer) (07/13/89)

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@Apple.COM, cheers!greg@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);
        }
    }
}
======================================================================

desnoyer@apple.com (Peter Desnoyers) (07/14/89)

In article <396@uop.uop.EDU> greg@uop.EDU (Greg Onufer) writes:
> main(){{ /* block 1 */
>         int count1; /* Base of stackframe */
>         register x1, x2, ... {
>             int count2;
>         }}
>        { /* block 2 */
>         int count1;
>         register *x1, *x2, ... {
>             int count2; /* Base of new stackframe */
                                     ^^^^^^^^^^^^^^
> [...] (code has been brutalized to squeeze out lines)

Remember that the compiler does not have to create a new stack frame
when it encounters a block. It can just append the block variables
to the auto variables for the function and add initialization code
where needed. If it is smart it will know that auto variables in 
different blocks don't conflict and can use the same storage locations.

Try printing out the addresses of each instance of count1 and count2
on different compilers. That might give you some additional insight
into what is going on.

                                      Peter Desnoyers
                                      Apple ATG
                                      (408) 974-4469

john@frog.UUCP (John Woods) (07/18/89)

In article <396@uop.uop.EDU>, greg@uop.EDU (Greg Onufer) writes:
> Some students here had to determine the number of registers (data 
> and address, we use 680x0's) the C compiler uses.

The Greenhills C compiler returns 19 data and 19 address registers.  Tee hee.

> 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.

That should have been a red warning.

-- 
John Woods, Charles River Data Systems, Framingham MA, (508) 626-1101
...!decvax!frog!john, john@frog.UUCP, ...!mit-eddie!jfw, jfw@eddie.mit.edu
    People...How you gonna FIGURE 'em?
    Don't bother, S.L.--Just stand back and enjoy the EVOLUTIONARY PROCESS...

hascall@atanasoff.cs.iastate.edu (John Hascall) (07/22/89)

In <untold articles> <everyone@everywhere> "hordes o' people" have wrote:

    [.. various schemes (all flamed) to count used registers ..]

  Well, I think we've decided this is machine dependent, so I submit for your
kind flamage a VMS version (tested even, imagine that :-)

REG.C:
main()
{     
        register int        a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p;
        int                 fn(); 
        short               *regs;

        a = fn(); b = fn(); c = fn(); d = fn();
        e = fn(); f = fn(); g = fn(); h = fn();
        i = fn(); j = fn(); k = fn(); l = fn();
        m = fn(); n = fn(); o = fn(); p = fn();

        regs = (short*)main;

        printf("Register mask = %04.4x\n", *regs);

        fx(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p);
}
RDUMMY.C:
fn()
{return(1);}
fx(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p)
int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p;
{}
------------------------------------------------------

John