public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* va_arglist
@ 2008-12-02 12:48 Austin, Alex
  2008-12-02 13:30 ` va_arglist Harvey Chapman
  2008-12-02 15:37 ` va_arglist Ian Lance Taylor
  0 siblings, 2 replies; 7+ messages in thread
From: Austin, Alex @ 2008-12-02 12:48 UTC (permalink / raw)
  To: gcc-help

Is there any way to write code to generate a variable argument list and call a variable function?

For example:

int callPrintf(char *format, void *args[])
{
	void **arg;
	for(arg = args; *arg != NULL; arg++)
		PushAnArgument(*arg);
	return printf(format);
}

I am trying to wrap a poorly-designed API in a Python extension module. There is no way to separate out the variable argument call to multiple calls.

Is this possible?
- Alex

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: va_arglist
  2008-12-02 12:48 va_arglist Austin, Alex
@ 2008-12-02 13:30 ` Harvey Chapman
  2008-12-02 15:39   ` va_arglist Austin, Alex
  2008-12-02 15:37 ` va_arglist Ian Lance Taylor
  1 sibling, 1 reply; 7+ messages in thread
From: Harvey Chapman @ 2008-12-02 13:30 UTC (permalink / raw)
  To: Austin, Alex; +Cc: gcc-help

Maybe this will help?

http://www.cl.cam.ac.uk/cgi-bin/manpage?3+stdarg

Although, I tried nesting some printf-like calls and had no success. 
Perhaps there's a trick to it?

H.


#include <stdio.h>
#include <stdarg.h>

int testB(char *fmt, ...)
{
  va_list ap;

  va_start(ap, fmt);
  printf("%p  ", va_arg(ap, char *));
  printf("%c\n", va_arg(ap, int));
  va_end(ap);

  va_start(ap, fmt);
  vprintf(fmt, ap);
  va_end(ap);

  return(0);
}

int testA(char *fmt, ...)
{
  int rc = 0;
  va_list ap;

  va_start(ap, fmt);
  printf("%p  ", va_arg(ap, char *));
  printf("%c\n", va_arg(ap, int));
  va_end(ap);

  va_start(ap, fmt);
  rc = testB(fmt, ap);
  va_end(ap);

  return(rc);
}

int main(int argc, char *argv[])
{
  testA("Hello, %s%c\n", "World", '!');

  return(0);
}

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: va_arglist
  2008-12-02 12:48 va_arglist Austin, Alex
  2008-12-02 13:30 ` va_arglist Harvey Chapman
@ 2008-12-02 15:37 ` Ian Lance Taylor
  1 sibling, 0 replies; 7+ messages in thread
From: Ian Lance Taylor @ 2008-12-02 15:37 UTC (permalink / raw)
  To: Austin, Alex; +Cc: gcc-help

"Austin, Alex" <Alex.Austin@spectrumdsi.com> writes:

> Is there any way to write code to generate a variable argument list
> and call a variable function?

There is no portable way to do this.  You may be able to use the FFI
library.  It's distributed with gcc, in the libffi directory.

Ian

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: va_arglist
  2008-12-02 13:30 ` va_arglist Harvey Chapman
@ 2008-12-02 15:39   ` Austin, Alex
  2008-12-02 18:24     ` va_arglist David Daney
  0 siblings, 1 reply; 7+ messages in thread
From: Austin, Alex @ 2008-12-02 15:39 UTC (permalink / raw)
  To: Harvey Chapman; +Cc: gcc-help

That's all about unpacking an arglist. I need to pack an arglist. Also, the function in question isn't actually printf, but a custom API, and it doesn't have a vprintf equivalent.

-----Original Message-----
From: Harvey Chapman [mailto:hchapman-gcc-help@3gfp.com] 
Sent: Tuesday, December 02, 2008 7:29 AM
To: Austin, Alex
Cc: gcc-help@gcc.gnu.org
Subject: Re: va_arglist

Maybe this will help?

http://www.cl.cam.ac.uk/cgi-bin/manpage?3+stdarg

Although, I tried nesting some printf-like calls and had no success. 
Perhaps there's a trick to it?

H.


#include <stdio.h>
#include <stdarg.h>

int testB(char *fmt, ...)
{
  va_list ap;

  va_start(ap, fmt);
  printf("%p  ", va_arg(ap, char *));
  printf("%c\n", va_arg(ap, int));
  va_end(ap);

  va_start(ap, fmt);
  vprintf(fmt, ap);
  va_end(ap);

  return(0);
}

int testA(char *fmt, ...)
{
  int rc = 0;
  va_list ap;

  va_start(ap, fmt);
  printf("%p  ", va_arg(ap, char *));
  printf("%c\n", va_arg(ap, int));
  va_end(ap);

  va_start(ap, fmt);
  rc = testB(fmt, ap);
  va_end(ap);

  return(rc);
}

int main(int argc, char *argv[])
{
  testA("Hello, %s%c\n", "World", '!');

  return(0);
}

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: va_arglist
  2008-12-02 15:39   ` va_arglist Austin, Alex
@ 2008-12-02 18:24     ` David Daney
  2008-12-02 23:27       ` va_arglist Robert William Fuller
  0 siblings, 1 reply; 7+ messages in thread
From: David Daney @ 2008-12-02 18:24 UTC (permalink / raw)
  To: Austin, Alex; +Cc: Harvey Chapman, gcc-help

Austin, Alex wrote:
> That's all about unpacking an arglist.

No, it does both 'packing' and 'unpacking'.

> I need to pack an arglist. Also, the function in question isn't
> actually printf, but a custom API, and it doesn't have a vprintf
> equivalent.

If your architecture is supported by libffi, and the vargs ABI is the 
same as the ABI for fixed args, libffi should do exactly what you need.

David Daney

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: va_arglist
  2008-12-02 18:24     ` va_arglist David Daney
@ 2008-12-02 23:27       ` Robert William Fuller
  2008-12-04  1:37         ` va_arglist Austin, Alex
  0 siblings, 1 reply; 7+ messages in thread
From: Robert William Fuller @ 2008-12-02 23:27 UTC (permalink / raw)
  To: David Daney; +Cc: Austin, Alex, Harvey Chapman, gcc-help

David Daney wrote:
> Austin, Alex wrote:
>> That's all about unpacking an arglist.
> 
> No, it does both 'packing' and 'unpacking'.
> 
>> I need to pack an arglist. Also, the function in question isn't
>> actually printf, but a custom API, and it doesn't have a vprintf
>> equivalent.
> 
> If your architecture is supported by libffi, and the vargs ABI is the 
> same as the ABI for fixed args, libffi should do exactly what you need.
> 
> David Daney

Whatever you do, don't try to use __builtin_apply.  It's busted on 
x86-64.  This is why the Objective-C runtime is broken out of the box on 
x86-64.  The only reason Objective-C works for the GnuStep project is 
they are using hooks in the runtime to replace calls to __builtin_apply 
with calls to libffi in their runtime.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: va_arglist
  2008-12-02 23:27       ` va_arglist Robert William Fuller
@ 2008-12-04  1:37         ` Austin, Alex
  0 siblings, 0 replies; 7+ messages in thread
From: Austin, Alex @ 2008-12-04  1:37 UTC (permalink / raw)
  To: Robert William Fuller, David Daney; +Cc: Harvey Chapman, gcc-help

I'm not aware of __builtin_apply, but the target arch is arm. Not sure whether it's eabi or oabi. Whatever the default of gcc-4.0.1 is.

-----Original Message-----
From: Robert William Fuller [mailto:hydrologiccycle@gmail.com] 
Sent: Tuesday, December 02, 2008 5:27 PM
To: David Daney
Cc: Austin, Alex; Harvey Chapman; gcc-help@gcc.gnu.org
Subject: Re: va_arglist

David Daney wrote:
> Austin, Alex wrote:
>> That's all about unpacking an arglist.
> 
> No, it does both 'packing' and 'unpacking'.
> 
>> I need to pack an arglist. Also, the function in question isn't
>> actually printf, but a custom API, and it doesn't have a vprintf
>> equivalent.
> 
> If your architecture is supported by libffi, and the vargs ABI is the 
> same as the ABI for fixed args, libffi should do exactly what you need.
> 
> David Daney

Whatever you do, don't try to use __builtin_apply.  It's busted on 
x86-64.  This is why the Objective-C runtime is broken out of the box on 
x86-64.  The only reason Objective-C works for the GnuStep project is 
they are using hooks in the runtime to replace calls to __builtin_apply 
with calls to libffi in their runtime.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2008-12-04  1:37 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-12-02 12:48 va_arglist Austin, Alex
2008-12-02 13:30 ` va_arglist Harvey Chapman
2008-12-02 15:39   ` va_arglist Austin, Alex
2008-12-02 18:24     ` va_arglist David Daney
2008-12-02 23:27       ` va_arglist Robert William Fuller
2008-12-04  1:37         ` va_arglist Austin, Alex
2008-12-02 15:37 ` va_arglist Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).