[cc65] Solution for Variable-argument-list Problem

Date view Thread view Subject view

From: Greg King (gngking_at_erols.com)
Date: 2000-11-29 23:32:12


Here is a way to fix cc65's variable-argument-list problem:

When cc65 compiles a "varargs" function, it knows about the named (fixxed)
parameters; it does not know about the unnamed (variable) parameters.  When
you use a named parameter inside that function, cc65 points to where it
thinks that argument sits on the data-stack.  That location is different
from the true location by the length of all of the variable arguments.  The
compiler needs to add that length to the "fixxed-offset," in order to
"touch" the proper argument.

The library's "enter" subroutine can compute that length.  It gets the size
of the entire argument-list from the caller.  Cc65 can give, to "enter,"
the size of the fixxed part of that list.  The subroutine can subtract one
size from the other, and put the difference on the stack.  After that, the
compiler can include code to add it to offsets whenever cc65 wants a named
argument.

Until the compiler is fixxed, you can use this "standard argument header"
file, to do the same thing.  Wrap my va_par() macro around the names of
fixxed parameters.

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

/* stdarg.h -- 2000-11-26 G.King
**
** Ullrich von Bassewitz, 31.05.1998
*/
#ifndef _STDARG_H
#define _STDARG_H

/* Declare these automatic local variables, in the function:
** va_list ap;
** unsigned char _va_;
*/

/* Standard C macroes */

typedef unsigned char* va_list;

#define va_start(ap, last) (ap) = (va_list)&(last) + \
   (_va_ = *((va_list)&(last)-1) - (unsigned char)__fixargs__)
#define va_arg(ap, type) ((type)*((ap) -= ((sizeof (type) + 1) & ~1)))
#define va_end(ap)

#define va_copy(dest, src) ((dest)=(src))

/* cc65 macroes */

#define va_par(name, type) (*(type*)((va_list)&(name) + _va_))

/* va_fix() makes smaller code than va_par(); but, it is less "portable,"
** can't work after va_arg() is used, and assumes that all arguments
** are two bytes long.
*/
#define va_fix(ap, num)  (*((ap)+(__fixargs__-(num)*2)))

#endif /* End of stdarg.h */



----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo_at_musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.


Date view Thread view Subject view

This archive was generated by hypermail 2.1.3 : 2001-12-14 22:05:37 CET