ffcall-vacall man page
vacall — C functions called with variable arguments
Synopsis
#include <vacall.h>
extern void* vacall_function;
void function (va_alistalist) { va_start_type(alist[, return_type]); arg = va_arg_type(alist[, arg_type]); va_return_type(alist[[, return_type], return_value]); }
vacall_function = &function;
val = ((return_type (*) ()) vacall) (arg1,arg2,...);
Description
This set of macros permit a C function function to be called with variable arguments and to return variable return values. This is much like the stdarg(3) facility, but also allows the return value to be specified and call the function stored in the variable vacall_function. If you want to make more than one use of vacall, use the trampoline_return_type must be the same.
The type in va_arg_type shall be one of int, uint, long, ulong, longlong, prescribed by the default K&R C expression promotions, they have to use int instead of char, schar, uchar, short, ushort and double instead of float.
The macros va_start_longlong(), va_start_ulonglong(), va_return_longlong(),.
Example
This example, a possible implementation of execl(3) on top of execv(2) using stdarg(3),
#include <stdarg.h> #define MAXARGS 100 /* execl is called by execl(file, arg1, arg2, ..., (char *)0); */ int execl (...) { va_list ap; char* file; char* args[MAXARGS]; int argno = 0; va_start (ap); file = va_arg(ap, char*); while ((args[argno] = va_arg(ap, char*)) != (char *)0) argno++; va_end (ap); return execv(file, args); }
looks like this using vacall(3):
#include <vacall.h> #define MAXARGS 100 /* execl is called by vacall(file, arg1, arg2, ..., (char *)0); */ void execl (va_alist ap) { char* file; char* args[MAXARGS]; int argno = 0; int retval; va_start_int (ap); file = va_arg_ptr(ap, char*); while ((args[argno] = va_arg_ptr(ap, char*)) != (char *)0) argno++; retval = execv(file, args); va_return_int (ap, retval); } vacall_function = &execl;
See Also
stdarg(3), trampoline(3), callback memory considers only the struct's size and alignment. This is inaccurate: for example, gcc on m68k-next returns struct { char a,b,c; } in registers and struct { char a[3]; } in memory, although both types have the same size and the same alignment.
The argument list can only be walked once.
The use of the global variable vacall_function is not reentrant. This is fixed in the callback(3) package.
Porting
Knowledge about argument passing conventions can be found in the gcc source, file gcc-2.6.3/config/cpu/cpu.h, section "Stack layout; function entry, exit and calling."
The implementation of varargs for gcc can be found in the gcc source, files gcc-2.6.3/ginclude/va*.h.
gcc's __builtin_saveregs() function is defined in the gcc source, file gcc-2.6.3/libgcc2.c.
Author
Bruno Haible <bruno@clisp.org>
Acknowledgements
Many ideas and a lot of code were cribbed from the gcc source.