Short code to determine number of registers

Eric Giguere giguere at aries5.uucp
Tue Jul 18 15:04:59 AEST 1989


I for one am quite surprised that the various tests for register allocation 
posted over the last few days can be compiled by any compiler.  In the case
of ANSI compilers the following section applies:

    "... storage-class specifier register suggests that access to the
     object be as fast as possible.  The extent to which such suggestions
     are effective is implementation-defined." (3.5.1)

to which the following (important!) footnote is added:

    "The implementation may treat any register declaration simply as an
     auto declaration.  However, whether or not addressable storage is
     actually used, the address of any part of an object declared with
     storage-class specifier register may not be computed, either explicitly
     (by use of the unary & operator as discussed in 3.3.3.2) or implicitly
     (by converting an array name to a pointer as discussed in 3.2.2.1).
     Thus the only operator that can be applied to an array declared with
     storage-class specifier register is sizeof."

So if I write

    main()
      {
        register int a, b, c, d, e, /* etc. */ x, y, z;
        int *ptr;

        ptr = &z;
      }

the compiler should flag this as an error, whether or not the underlying
architecture has 1, 2, 7, 15 or 26 registers available to be allocated.

Probably the only way to determine the number of registers a compiler
allocates is by disassembling the final object code.  This method is
quite portable :-)

Eric Giguere                                  268 Phillip St #CL-46
For the curious: it's French ("jee-gair")     Waterloo, Ontario  N2L 6G9
Bitnet  : GIGUERE at WATCSG                   (519) 746-6565
Internet: giguere at aries5.UWaterloo.ca         "Nothing but urges from HELL!!"



More information about the Comp.lang.c mailing list