[comp.sys.sun] C++/sparc : problem with <stdarg.h>

martou@fynu.ucl.ac.be (Jean-Luc Martou) (06/05/91)

Hello!

We have a big problem concerning the file <stdarg.h> with the AT&T C++ 2.1
translator we have ported on SPARC.  In fact, all the macro va_start,...
are wrong for the sparc.  Who has build a right version of this header
file?  

Please send me your answer on my electronic-mail : martou@fynu.ucl.ac.be,
and I shall summarize all the answers.

Thanks for your help.

Jean-Luc MARTOU
Computer Staff
Nuclear Physic Department
Universite Catholique de Louvain
B-1348 Louvain-La-Neuve
BELGIUM

E-MAIL : martou@fynu.ucl.ac.be

joost@cadlab.de (Michael Joosten) (06/13/91)

Well, there are two solutions:

1) Either you cope around the problem, as demonstrated in NIHCL (Class.c)
   by renaming the last parameter before the ellipsis to
   '__builtin_va_alist'

2) Or you do the Right Thing: Solve the problem. But this is
   machine-dependent (better: RISC-dependent) and you have to patch the
   cfront sources. I have done it one DECStation a year ago (cfront 1.2) and
   for a Pyramid and a SPARC last month for cfront 2.0 .

Short Description:

The MIPS and Sun-SPARC compiler recognize their 'magic word'
(__builtin_va_alist for SPARC, va_alist for MIPS) and emit code to copy
the input window/registers into contigous memory, i.e. the stack, so that
va_arg could extract whatever it wants.

(Sorry, no diffs, I don't have the originals handy...)

First what AT&T forgot: Set the right preprocessor flags in CC.  The
(undocumented, of course) variable 'Y' keeps the flags for the
preprocessor, while 'X' is responsible for the 'cc' flags.  One should do
this for other machines, too !

Here are my adds in bin/CC:

Y="-Dsun -Dsparc -Dunix -D__BUILTIN_VA_ARG_INCR" # forgotten by AT&T... for SUN4
   Don't forget to delete the statement 'Y=' in the block around line 65:

TMPDIR=${TMPDIR-"/usr/tmp"}
TEMPDIR=$TMPDIR/CC.$$
C= EE=0 FON= XON= O=
PON= R= SUF= X= Y=                 <------------ !!!
DASHR= PLUSI= STRIP=
Z= E=0 OF= P=

   Now the changes for stargs:

-------------------------stdarg.h----------------------------------------------
..........
.........

#ifndef va_start


#ifdef pyr
typedef int     va_buf[3];
typedef va_buf  *va_list;

extern "C" {
extern void     _vastart(va_list, char*);
extern char     *_vaarg(va_list, int);
}

#define         va_start(ap, parmN)     {\
        va_buf  _va;\
        _vastart(ap = (va_list)_va, (char *)&parmN + sizeof parmN)
#define         va_end(ap)      }
#define         va_arg(ap, mode)        *((mode *)_vaarg(ap, sizeof (mode)))
#else

typedef char *va_list;
#define va_end(ap)

#ifdef sparc
#define va_start(ap, parmN) \
	(ap = ((char *) &(parmN) + (((sizeof (parmN) + \
		   sizeof (int) - 1) / sizeof (int)) * sizeof (int))))

#ifdef __BUILTIN_VA_ARG_INCR
#    define va_arg(list,mode) ((mode*)__builtin_va_arg_incr((mode *)list))[0]
#else
#    define va_arg(list,mode) ((mode *)(list += sizeof(mode)))[-1]
#endif
extern "C" void *__builtin_va_arg_incr(...);
#else

#ifdef u370
#define va_start(ap, parmN) ap =\
	(char *) ((int)&parmN + 2*sizeof(parmN) - 1 & -sizeof(parmN))
#define va_arg(ap, mode) ((mode *)(ap = \
	(char *) ((int)ap + 2*sizeof(mode) - 1 & -sizeof(mode))))[-1]
#else

#ifdef hp9000s800
#define va_start(ap, parmN) (ap = (char *)&parmN + sizeof(parmN),(void)va_arg(ap,int))
#define va_arg(ap, mode) ((mode *)(ap = (char *)((int)(ap-sizeof(mode)) & ~(sizeof(mode)-1))))[0]
#else
#define va_start(ap, parmN) ap = (char *)( &parmN+1 )
#define va_arg(ap, mode) ((mode *)(ap += sizeof(mode)))[-1]
#endif

#endif
#endif
#endif

...............
...............

-------------------------------------------------------------------------------

And now a patch for print2.c:

In method fct::dcl_print(), add/replace the following at the end:

.........
.........
        else {
                // print argument names followed by argument type declarations:
                if (body && Cast==0) {
                        for (nn=at; nn;) {
                                nn->print();
                                if (nn=nn->n_list) puttok(CM); else break;
                        }
#ifdef pyr
                        if (nargs_known == ELLIPSIS) putstring(",__0va0,__0va1,_
_0va2,__0va3,__0va4,__0va5,__0va6,__0va7,__0va8,__0va9,__0va10,__0va11");
#endif
#ifdef mips
                        if (nargs_known == ELLIPSIS) {
                                putst(",va_alist) int va_alist;");
                        } else
#endif
#ifdef sparc
                        if (nargs_known == ELLIPSIS) {
                                putst(",__builtin_va_alist) int __builtin_va_ali
st;");
                        } else
#endif
                        putch(')');
                }
                else
                        putch(')');
        }
}

-------------------------------------------------------------------------------

Good Luck			Michael


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Michael Joosten   |       Tel.  : (+49) (+) 5251-284 120                 |
| CADLAB            |       Fax   : (+49) (+) 5251-284 140                 |
| Bahnhofstr. 32    |       E-Mail: joost@cadlab.uucp                      |
| D-4790 Paderborn  |        	    joost@cadlab.cadlab.de                 | 
| FRG               |               ...!uunet!unido!cadlab!joost           | 
|                   | Mass mail to: joost@pbinfo.uni-paderborn.de          |
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Michael Joosten   |       Tel.  : (+49) (+) 5251-284 120                 |
| CADLAB            |       Fax   : (+49) (+) 5251-284 140                 |
| Bahnhofstr. 32    |       E-Mail: joost@cadlab.uucp                      |
| D-4790 Paderborn  |        	    joost@cadlab.cadlab.de                 | 
| FRG               |               ...!uunet!unido!cadlab!joost           | 
|                   | Mass mail to: joost@pbinfo.uni-paderborn.de          |
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~