Motherboard Forums


Reply
Thread Tools Display Modes

va_args (va_start, va_end, va_list ) souce?

 
 





















Not Really Me
Guest
Posts: n/a

 
      11-07-2009, 05:52 PM


I'm trying to make a simplified printf that does not rely on stdarg.h and
its accompanying baggage, so I need the source to the va_args functions.
Including stdarg.h tends to pull in all sorts of extraneous library
functions.

The source for libgcc was my first guess, but good gravy, it could take a
year to unravel that stuff.

Does anyone have a clean va_args source to share, i.e., va_start, va_end,
va_list in source that would be compiler independent?

Scott



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4581 (20091107) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com




 
Reply With Quote
 
Boudewijn Dijkstra
Guest
Posts: n/a

 
      11-07-2009, 05:57 PM
Op Sat, 07 Nov 2009 18:52:20 +0100 schreef Not Really Me
<>:
> I'm trying to make a simplified printf that does not rely on stdarg.h and
> its accompanying baggage, so I need the source to the va_args functions.


These are CPU dependent. Some arguments might be in registers, some might
be on the stack.



--
Gemaakt met Opera's revolutionaire e-mailprogramma:
http://www.opera.com/mail/
(remove the obvious prefix to reply by mail)
 
Reply With Quote
 
Jon Kirwan
Guest
Posts: n/a

 
      11-07-2009, 09:19 PM
On Sat, 07 Nov 2009 18:57:28 +0100, "Boudewijn Dijkstra"
<> wrote:

>Op Sat, 07 Nov 2009 18:52:20 +0100 schreef Not Really Me
><>:
>> I'm trying to make a simplified printf that does not rely on stdarg.h and
>> its accompanying baggage, so I need the source to the va_args functions.

>
>These are CPU dependent. Some arguments might be in registers, some might
>be on the stack.


Worse, this can also depend upon #pragma, extended keywords not
explicitly part of the c standard, and compile options, as well. For
example, parameters may be placed in an XRAM parameter stack on the
8051. Or passed in fixed static memory locations assigned and reused.
Also, parameters can be _spilled_ out of registers by the compiler if
their address is taken in the routine.

As you imply, the OP needs to disclose the compiler toolset and target
before getting closer to a meaningful answer.

Jon
 
Reply With Quote
 
Hans-Bernhard Bröker
Guest
Posts: n/a

 
      11-08-2009, 02:12 PM
Not Really Me wrote:
> I'm trying to make a simplified printf that does not rely on stdarg.h and
> its accompanying baggage, so I need the source to the va_args functions.


No can do. For starters, the va_args functionality consists of macros,
not functions. I.e. the bulk of it is _in_ <stdarg.h>. Trying to
implement a variadic function without it would be quite exactly like
trying to make omelettes without a pan.

> Including stdarg.h tends to pull in all sorts of extraneous library
> functions.


If so, that'll be because it has to.

Variable argument lists are pretty much by definition totally
system-specific. They depend on the compiler, the switches it was
invoked with, the operating system ABI, and all sorts of other things.
 
Reply With Quote
 
Not Really Me
Guest
Posts: n/a

 
      11-09-2009, 03:03 PM
Jon Kirwan wrote:
> On Sat, 07 Nov 2009 18:57:28 +0100, "Boudewijn Dijkstra"
> <> wrote:
>
>> Op Sat, 07 Nov 2009 18:52:20 +0100 schreef Not Really Me
>> <>:
>>> I'm trying to make a simplified printf that does not rely on
>>> stdarg.h and its accompanying baggage, so I need the source to the
>>> va_args functions.

>>
>> These are CPU dependent. Some arguments might be in registers, some
>> might be on the stack.

>
> Worse, this can also depend upon #pragma, extended keywords not
> explicitly part of the c standard, and compile options, as well. For
> example, parameters may be placed in an XRAM parameter stack on the
> 8051. Or passed in fixed static memory locations assigned and reused.
> Also, parameters can be _spilled_ out of registers by the compiler if
> their address is taken in the routine.
>
> As you imply, the OP needs to disclose the compiler toolset and target
> before getting closer to a meaningful answer.
>
> Jon



grumble, grumble. I suppose you are both right. It really does have to be
processor/compiler specific. Sometimes it is only clear when someone else
points it out.

Thanks,
Scott



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4588 (20091109) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com




 
Reply With Quote
 
Nobody
Guest
Posts: n/a

 
      11-09-2009, 10:34 PM
On Sat, 07 Nov 2009 10:52:20 -0700, Not Really Me wrote:

> I'm trying to make a simplified printf that does not rely on stdarg.h and
> its accompanying baggage, so I need the source to the va_args functions.


They're macros, not functions. The fact that the second argument to
va_arg() is a type should be a clue (you can't pass a type as a function
argument).

> Including stdarg.h tends to pull in all sorts of extraneous library
> functions.
>
> The source for libgcc was my first guess,


Nope; try the source for gcc itself.

gcc's stdarg.h (it's part of gcc, not libc) defines them as:

#define va_start(v,l) __builtin_va_start(v,l)
#define va_end(v) __builtin_va_end(v)
#define va_arg(v,l) __builtin_va_arg(v,l)

the __builtin_* "functions" are rather like what Lisp calls "special
forms": neither functions nor macros, but constructs which the compiler
recognises and handles specially.

If you want to write equivalents, you need to know the details of the
platform's calling convention, i.e. which arguments will be in which
registers, which will be on the stack (and where).

If you're lucky, the compiler will just push all of the unspecified
arguments onto the stack in right-to-left order, so va_arg() is just
essentially "(*((type *)(ap))++)"; most x86 platforms behave like this. If
you're not so lucky, you will have to enumerate all of the possible
combinations of argument types until the registers have been exhausted
(after which, the rest will be on the stack).

Also, some platforms use a different calling convention for variadic
functions (e.g. Windows uses "cdecl" rather than "stdcall"), in which case
the convention for variadic functions is often simpler (e.g. pushing all
unspecified arguments onto the stack rather than using registers).

 
Reply With Quote
 
John Devereux
Guest
Posts: n/a

 
      11-11-2009, 08:00 AM
(Chris Giese) writes:

> "Not Really Me" <> wrote:
>
>>I'm trying to make a simplified printf that does not rely on stdarg.h and
>>its accompanying baggage, so I need the source to the va_args functions.
>>Including stdarg.h tends to pull in all sorts of extraneous library
>>functions.

>
> http://my.execpc.com/~geezer/code/printf.c
>
> This code assumes arguments on the stack. There are STDARG.H macro
> definitions in there, but be sure to check the assumptions I made
> with those.


Hi Chris,

Hey, that's the code I used in a lot of my own projects (after I dumped
newlib for small systems).

Thanks for making it available!

--

John Devereux
 
Reply With Quote
 
Not Really Me
Guest
Posts: n/a

 
      11-11-2009, 02:42 PM
Chris Giese wrote:
> "Not Really Me" <> wrote:
>
>> I'm trying to make a simplified printf that does not rely on
>> stdarg.h and its accompanying baggage, so I need the source to the
>> va_args functions. Including stdarg.h tends to pull in all sorts of
>> extraneous library functions.

>
> http://my.execpc.com/~geezer/code/printf.c
>
> This code assumes arguments on the stack. There are STDARG.H macro
> definitions in there, but be sure to check the assumptions I made
> with those.


Thanks. It could be real useful in the future. My current project is using
Wind River Compiler for PPC and it uses registers for the first seven
arguments.

As others so helpfully pointed out, that is the reason the _va macros are
compiler specific.

Scott



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4595 (20091111) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com




 
Reply With Quote
 
vladitx
Guest
Posts: n/a

 
      11-11-2009, 11:28 PM
On Nov 11, 4:42*pm, "Not Really Me"
<sc...@validatedQWERTYsoftware.XYZZY.com> wrote:

> Thanks. *It could be real useful in the future. *My current project is using
> Wind River Compiler for PPC and it uses registers for the first seven
> arguments.
>
> As others so helpfully pointed out, that is the reason the _va macros are
> compiler specific.


Functions with variable number of arguments receive all their variable
arguments through stack. The <stdarg.h> macros dealing with accessing
the arguments on the stack are compiler (and possibly OS, because of
calling conventions) specific, but their purpose is simple enough -
walk through a stack frame.
 
Reply With Quote
 
steve_schefter@hotmail.com
Guest
Posts: n/a

 
      11-12-2009, 12:22 AM
On Nov 11, 6:28*pm, vladitx <vlad...@nucleusys.com> wrote:
> Functions with variable number of arguments receive all their variable
> arguments through stack.


That's compiler dependent and frequently not the case. Even on x86.

Steve
 
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT. The time now is 04:35 PM.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43