From: toddb@nvr.com (Todd Brunhoff)
Subject: passing arguments in registers
Reply-To: toddb@nvr.com
Organization: GNUs Not Usenet
Distribution: gnu
Date: Fri, 6 Nov 1992 11:12:02 GMT

[There are 3 problems here, all related.  I'd be glad to split them up if
 that's more appropriate...]

Gcc version: 2.2.2
Host machine: Sun SPARCstation 2
Host OS: SunOS 4.1.2 (aka Solaris 1.0.1)
Environment: Cshell.

Problem 1:

I am using the asm() constructs to pass more than six arguments in registers
to a function.  Most of the time I can move values into place in %g1, %g2,
etc. and then force the compiler to leave them there with a local block
surrounding the call to the function, like this (normally hidden in a
macro):

        {
                register long reg7 asm("%g2");
                register long reg8 asm("%g3");
                register long reg9 asm("%g1");
                reg7 = (long) C67;
                reg8 = (long) C77;
                reg9 = (long) 6;
                asm volatile ("! (%0) reg7 is live" : : "r" ((long)reg7));
                asm volatile ("! (%0) reg8 is live" : : "r" ((long)reg8));
                asm volatile ("! (%0) reg9 is live" : : "r" ((long)reg9));
                function( c70,  c71,  c72,  c73,  c74,  c75);
        }

This produces code like below, which looks right.

        mov %l2,%g2
        mov %g5,%g3
        mov 6,%g1
        ! (%g2) reg7 is live
        ! (%g3) reg8 is live
        ! (%g1) reg9 is live
        mov %i2,%o0
        mov %i4,%o1
        mov %i1,%o2
        mov %l6,%o3
        mov %l3,%o4
        call _function,0
        mov %l2,%o5

This doesn't seem to be sufficient, although I don't have evidence for
this.  I think I am just plain lucky that the compiler didn't decide that
the lifetimes for the register variables were up just before the
function call, and clobber them.  Is there a way to assure myself
that the registers stay put?

(related) Problem 2:

The called function needs to recognize the presence of the arguments
in registers, of course.  I can't just say they are there, because
the compiler either thinks they are dead or that they are generated
in the routine, and hence uses the argument registers freely for
other purposes.  I seem to be able to get around this with another
local block in which I overwrite the declared arguments with the
values in the registers.  Like this
        {
            register long reg7 asm("%g2"), reg8 asm("%g3");    
            asm volatile ("! reg7,8 are %0 %1" : "=r" (reg7), "=r" (reg8));    
            asm volatile ("! bypass c6@%0 & c7@%1" : : "o" (c6), "o" ( c7));    
            c6 = reg7;   
            c7 = reg8;  
            asm volatile ("! now: c6 in %0, c7 in %1" : : "r" (c6), "r" ( c7));
        }

Sometimes this produces code like:
        !#PROLOGUE# 0
        save %sp,-112,%sp
        !#PROLOGUE# 1
        ! reg7,8 are %g2 %g3
        ! bypass c6@[%fp+92] & c7@[%fp+96]
    *   mov %g2,%o7
    *   mov %g3,%o0
        ! now: c6 in %o7, c7 in %o0

Note that it did an explicit move (marked with *).  Looking at the code that
follows, there doesn't seem to be a reason for this... it could have just as
easily left them in %g2 and %g3.  Other times, the compiler leaves them
in place.  What I really want to do is say, "here is argument 7".  I don't
I want to explicitly declare them as %g2 and %g3, for two reasons:
  - I like to be able to lint the code, because it helps me find where
    I might have misplaced a variable.  This whole mechanism is done in
    macros such that there is a machine-dependent version and a machine-
    independent version, depending on how it is compiled.  Makes debugging
    more reasonable.
  - I want the compiler to reallocate and use the registers as it sees fit.
    All I want to do is tell it where to start.

Problem 3:

What program do I use to peruse the gcc.info-* files.  Besides vi :-)

Can you help me?  Thanks either way.

---------------
internet: toddb@nvr.com                                             c--Q Q
US:       Todd Brunhoff; North Valley Research;                         `
          15262 NW Greenbriar Pkwy; Beaverton, OR  97006                -
Phone:    (503) 531-5707
Fax:      (503) 690-2320

Newsgroups: gnu.gcc.bug