Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #105855
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: va_start without the first argument? |
| Date | 2017-03-12 13:52 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <oa3g4n$egd$1@dont-email.me> (permalink) |
| References | (2 earlier) <6efb4ec5-8a50-4ecd-95ac-3328682a7a03@googlegroups.com> <1c6010ab-c0e6-468b-8b9d-94cbe2aa4ac8@googlegroups.com> <r5c6cch9bq72g8tk64sl9di7lp67ucm66h@4ax.com> <3fb4148c-8b5b-468c-8399-e5a5fc8575ee@googlegroups.com> <oa1gud$79a$1@dont-email.me> |
On 11/03/17 19:53, jacobnavia wrote: > Le 11/03/2017 à 00:37, Thiago Adams a écrit : >> On Friday, March 10, 2017 at 8:13:50 PM UTC-3, robert...@yahoo.com wrote: >>> On Fri, 10 Mar 2017 14:53:02 -0800 (PST), Thiago Adams >>> wrote: >>> >>>> On Friday, March 10, 2017 at 7:36:37 PM UTC-3, Thiago Adams wrote: >>>>> On Friday, March 10, 2017 at 5:32:05 PM UTC-3, Keith Thompson wrote: >>>>>> Thiago Adams writes: >>>>>>> Is it possible to create a var args function without the >>>>>>> first argument? (firstArgument in va_start) >>>>>>> >>>>>>> void F(...) >>>>>> No. The va_start macro requires the name of the last non-variadic >>>>>> parameter, and syntax of a parameter-type-list requires at least one >>>>>> parameter-declaration (N1570 6.7.6). >>>>>> >>>>>> [...] >>>>> >>>>> How it works? >>>>> How does it knows the alignment/padding between the arguments? >>>>> >>>> I opened the definition of va_list etc from Visual C++ 2017 includes: >>>> >>>> typedef char* va_list; >>>> >>>> >>>> #define _ADDRESSOF(v) (&(v)) >>>> >>>> #define _INTSIZEOF(n) ((sizeof(n) + sizeof(int) - 1) & >>>> ~(sizeof(int) - 1)) >>>> >>>> #define __crt_va_start_a(ap, v) ((void)(ap = (va_list)_ADDRESSOF(v) >>>> + _INTSIZEOF(v))) >>>> >>>> #define __crt_va_arg(ap, t) (*(t*)((ap += _INTSIZEOF(t)) - >>>> _INTSIZEOF(t))) >>>> >>>> >>>> #define __crt_va_end(ap) ((void)(ap = (va_list)0)) >>>> >>>> The macro _INTSIZEOF has some tricks I don't understand. >>> > > Hi Thiago. > > Look, conceptually this is very simple. You have: > > 1) A heap of arguments passed in the stack or in registers. 32 bit > software passes everything in memory, i.e. in the stack. The x86 had too > few registers in that time to have a meaningful register passing > convention. <https://en.wikipedia.org/wiki/X86_calling_conventions> The most common calling convention in 32-bit x86 is to pass arguments on the stack - but there are a dozen different variations in use, some of which use registers for a few of the arguments. Most importantly, the calling convention used by the Win32 API is different. > > That meant that everything was just a linear array of bytes with a > cursor pointing to the position AFTER the va_start. You read one > argument, then increment the pointer by sizeof argument and go on like > that. > > That was easy and you have the definitions above in that MSVC file. > Yes - for arguments passed on the stack, va_args is easy. > Everything changed with 64 bits and Intel coming back to it senses and > putting more registers in the machine. > > GREAT! > > After all those years intel put a bit more power in that machine. You mean AMD? Intel had refused for years to consider a 64-bit x86 cpu, until AMD made the design and Intel had to copy it. > > So microsoft deviced a simple thing with 4 registers that are passed in > the integer regs. At most 4 args can be passed in total, if I remember > correctly. MS's x86 calling convention puts the first 4 integer or pointer arguments into registers RCX, RDX, R8 and R9, and the first 4 floating point arguments into XMM0..XMM3. Additional arguments go on the stack. Their calling convention requires space to be left on the stack as though the first four arguments were placed there rather than in registers. This makes va_arg functions easy - the function can first copy RCX..R9 to this stack space, and then the arguments are all linear just like on the most common x86 convention. It has the side-effect of making every non-vaargs function call noticeably slower on 64-bit Windows, and pointlessly wastes precious cache space. But who cares about other functions, as long as printf and friends are a few nano seconds faster and the compiler support for va args is easier to write? > > Gcc followed the much more complicated but more efficient route of > making 3 different heaps of arguments: Do you mean /AMD/ picked a different approach when /they/ designed an efficient calling convention for the ISA that /they/ were designing? AMD discussed calling conventions at length, with their cpu design team, OS developers (including Linux, BSD, Solaris, Apple, and MS) and compiler developers (including gcc, clang, Sun, Apple, and MS). They came up with a solution aimed at being as efficient as possible for most code, at the cost of being more complicated for va args functions which are rarely used except for printf and friends. The System V AMD64 ABI is followed by every x86-64 OS except Windows. As far as I know, x86-64 compilers all use the ABI for the target OS, rather than the idiotic chaos of the x86-32 Windows world with every compiler having a different selection of calling conventions and different pragmas, keywords and other extensions to choose between them. So gcc on Windows uses the Windows calling conventions for 64-bit x86. > > 1) Arguments passed in integer registers. > 2) Arguments passed in floating point registers. > 3) Arguments passed in RAM (stack). Windows does that too - but the System V AMD64 ABI makes better use of the registers and does not waste extra stack space. > > Pointers and integers up to register size go into (1). All floating > point goes into heap (2) (the xmm registers) and in (3) we find all > arguments that come after any of the first two is exhausted. > When you say "heap", you really mean "stack". > Or such... The details are clearly specified, at least for the System V AMD64 ABI - just download the ABI document when you want to see it. I assume the Windows version is equally easy to obtain. > > as another guy told us several times. :-) > > Getting the next argument depends on the state of the cursor (which > argument are you reading) and in the type of the arguments being passed > (its size). > > This is called the ABI (application binary interface) of the os you are > running. > > The compiler that compiles the OS establishes the ABI. Linux has an ABI > written by gcc (that compiles linux) and windows has another ABI written > by the guys of MSVC that compiles windows. > The ABI is usually an agreement between OS designers and compiler designers, sometimes with input from the chip designers (especially for new cpu designs). It is rarely determined by the compiler alone, except in cases of a new OS using an existing compiler.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
va_start without the first argument? Thiago Adams <thiago.adams@gmail.com> - 2017-03-10 11:57 -0800
Re: va_start without the first argument? Thiago Adams <thiago.adams@gmail.com> - 2017-03-10 12:04 -0800
Re: va_start without the first argument? Keith Thompson <kst-u@mib.org> - 2017-03-10 12:32 -0800
Re: va_start without the first argument? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-03-10 15:51 -0500
Re: va_start without the first argument? Thiago Adams <thiago.adams@gmail.com> - 2017-03-10 14:36 -0800
Re: va_start without the first argument? Thiago Adams <thiago.adams@gmail.com> - 2017-03-10 14:53 -0800
Re: va_start without the first argument? Robert Wessel <robertwessel2@yahoo.com> - 2017-03-10 17:14 -0600
Re: va_start without the first argument? Thiago Adams <thiago.adams@gmail.com> - 2017-03-10 15:37 -0800
Re: va_start without the first argument? Robert Wessel <robertwessel2@yahoo.com> - 2017-03-10 23:12 -0600
Re: va_start without the first argument? Thiago Adams <thiago.adams@gmail.com> - 2017-03-13 10:29 -0700
Re: va_start without the first argument? Thiago Adams <thiago.adams@gmail.com> - 2017-03-13 11:31 -0700
Re: va_start without the first argument? scott@slp53.sl.home (Scott Lurndal) - 2017-03-13 18:40 +0000
Re: va_start without the first argument? Thiago Adams <thiago.adams@gmail.com> - 2017-03-13 12:31 -0700
Re: va_start without the first argument? jacobnavia <jacob@jacob.remcomp.fr> - 2017-03-11 19:53 +0100
Re: va_start without the first argument? BartC <bc@freeuk.com> - 2017-03-11 19:05 +0000
Re: va_start without the first argument? Keith Thompson <kst-u@mib.org> - 2017-03-11 11:56 -0800
Re: va_start without the first argument? antispam@math.uni.wroc.pl - 2017-03-12 05:41 +0000
Re: va_start without the first argument? David Brown <david.brown@hesbynett.no> - 2017-03-12 13:52 +0100
Re: va_start without the first argument? BartC <bc@freeuk.com> - 2017-03-12 14:40 +0000
Re: va_start without the first argument? David Brown <david.brown@hesbynett.no> - 2017-03-12 18:13 +0100
Re: va_start without the first argument? BartC <bc@freeuk.com> - 2017-03-12 18:04 +0000
Re: va_start without the first argument? David Brown <david.brown@hesbynett.no> - 2017-03-12 21:04 +0100
Re: va_start without the first argument? BartC <bc@freeuk.com> - 2017-03-12 21:02 +0000
Re: va_start without the first argument? David Brown <david.brown@hesbynett.no> - 2017-03-12 22:40 +0100
Re: va_start without the first argument? supercat@casperkitty.com - 2017-03-12 15:51 -0700
Re: va_start without the first argument? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-03-12 16:52 -0700
Re: va_start without the first argument? David Brown <david.brown@hesbynett.no> - 2017-03-13 00:56 +0100
Re: va_start without the first argument? supercat@casperkitty.com - 2017-03-13 09:17 -0700
Re: va_start without the first argument? BartC <bc@freeuk.com> - 2017-03-13 17:54 +0000
Re: va_start without the first argument? David Brown <david.brown@hesbynett.no> - 2017-03-13 22:12 +0100
Re: va_start without the first argument? BartC <bc@freeuk.com> - 2017-03-13 22:48 +0000
Re: va_start without the first argument? David Brown <david.brown@hesbynett.no> - 2017-03-14 13:46 +0100
Re: va_start without the first argument? BartC <bc@freeuk.com> - 2017-03-14 13:37 +0000
Re: va_start without the first argument? David Brown <david.brown@hesbynett.no> - 2017-03-14 15:24 +0100
Re: va_start without the first argument? supercat@casperkitty.com - 2017-03-14 08:25 -0700
Re: va_start without the first argument? David Brown <david.brown@hesbynett.no> - 2017-03-14 17:02 +0100
Re: va_start without the first argument? supercat@casperkitty.com - 2017-03-14 09:45 -0700
Re: va_start without the first argument? Keith Thompson <kst-u@mib.org> - 2017-03-14 09:15 -0700
Re: va_start without the first argument? supercat@casperkitty.com - 2017-03-14 10:02 -0700
Re: va_start without the first argument? jt@toerring.de (Jens Thoms Toerring) - 2017-03-14 20:32 +0000
Re: va_start without the first argument? raltbos@xs4all.nl (Richard Bos) - 2017-03-14 07:44 +0000
Re: va_start without the first argument? David Brown <david.brown@hesbynett.no> - 2017-03-14 13:38 +0100
Re: va_start without the first argument? Keith Thompson <kst-u@mib.org> - 2017-03-14 08:57 -0700
Re: va_start without the first argument? Keith Thompson <kst-u@mib.org> - 2017-03-13 11:21 -0700
Re: va_start without the first argument? scott@slp53.sl.home (Scott Lurndal) - 2017-03-13 18:33 +0000
Re: va_start without the first argument? David Brown <david.brown@hesbynett.no> - 2017-03-13 22:08 +0100
Re: va_start without the first argument? supercat@casperkitty.com - 2017-03-13 15:23 -0700
Re: va_start without the first argument? David Brown <david.brown@hesbynett.no> - 2017-03-14 13:58 +0100
Re: va_start without the first argument? Keith Thompson <kst-u@mib.org> - 2017-03-14 09:18 -0700
Re: va_start without the first argument? scott@slp53.sl.home (Scott Lurndal) - 2017-03-14 16:52 +0000
Re: va_start without the first argument? Keith Thompson <kst-u@mib.org> - 2017-03-14 11:34 -0700
Re: va_start without the first argument? scott@slp53.sl.home (Scott Lurndal) - 2017-03-14 18:54 +0000
Re: va_start without the first argument? supercat@casperkitty.com - 2017-03-14 13:30 -0700
Re: va_start without the first argument? Ike Naar <ike@iceland.freeshell.org> - 2017-03-15 21:55 +0000
Re: va_start without the first argument? supercat@casperkitty.com - 2017-03-15 15:16 -0700
Re: va_start without the first argument? Philip Lantz <prl@canterey.us> - 2017-03-23 21:04 -0700
Re: va_start without the first argument? David Brown <david.brown@hesbynett.no> - 2017-03-14 21:48 +0100
Re: va_start without the first argument? scott@slp53.sl.home (Scott Lurndal) - 2017-03-14 14:01 +0000
Re: va_start without the first argument? supercat@casperkitty.com - 2017-03-14 07:42 -0700
Re: va_start without the first argument? scott@slp53.sl.home (Scott Lurndal) - 2017-03-14 12:38 +0000
Re: va_start without the first argument? jacobnavia <jacob@jacob.remcomp.fr> - 2017-03-13 01:11 +0100
Re: va_start without the first argument? Ian Collins <ian-news@hotmail.com> - 2017-03-13 15:43 +1300
Re: va_start without the first argument? BartC <bc@freeuk.com> - 2017-03-13 10:36 +0000
Re: va_start without the first argument? Ian Collins <ian-news@hotmail.com> - 2017-03-14 07:33 +1300
Re: va_start without the first argument? BartC <bc@freeuk.com> - 2017-03-13 19:00 +0000
Re: va_start without the first argument? Ian Collins <ian-news@hotmail.com> - 2017-03-14 08:05 +1300
Re: va_start without the first argument? BartC <bc@freeuk.com> - 2017-03-13 23:10 +0000
Re: va_start without the first argument? Ian Collins <ian-news@hotmail.com> - 2017-03-14 17:38 +1300
Re: va_start without the first argument? Thiago Adams <thiago.adams@gmail.com> - 2017-03-14 05:48 -0700
Re: va_start without the first argument? Thiago Adams <thiago.adams@gmail.com> - 2017-03-13 12:12 -0700
Re: va_start without the first argument? jacobnavia <jacob@jacob.remcomp.fr> - 2017-03-12 19:39 +0100
Re: va_start without the first argument? jt@toerring.de (Jens Thoms Toerring) - 2017-03-10 20:49 +0000
Re: va_start without the first argument? Thiago Adams <thiago.adams@gmail.com> - 2017-03-10 14:41 -0800
Re: va_start without the first argument? John Bode <jfbode1029@gmail.com> - 2017-03-10 16:05 -0800
Re: va_start without the first argument? jacob navia <jacob@jacob.remcomp.fr> - 2017-03-11 21:47 +0100
Re: va_start without the first argument? supercat@casperkitty.com - 2017-03-11 13:07 -0800
Re: va_start without the first argument? jacobnavia <jacob@jacob.remcomp.fr> - 2017-03-11 22:24 +0100
Re: va_start without the first argument? Thiago Adams <thiago.adams@gmail.com> - 2017-03-13 05:44 -0700
Re: va_start without the first argument? Jerry Stuckle <jstucklex@attglobal.net> - 2017-03-13 08:59 -0400
Re: va_start without the first argument? Thiago Adams <thiago.adams@gmail.com> - 2017-03-13 06:17 -0700
Re: va_start without the first argument? Jerry Stuckle <jstucklex@attglobal.net> - 2017-03-13 13:27 -0400
Re: va_start without the first argument? Jerry Stuckle <jstucklex@attglobal.net> - 2017-03-13 13:30 -0400
Re: va_start without the first argument? Thiago Adams <thiago.adams@gmail.com> - 2017-03-13 11:00 -0700
Re: va_start without the first argument? Thiago Adams <thiago.adams@gmail.com> - 2017-03-13 10:34 -0700
Re: va_start without the first argument? antispam@math.uni.wroc.pl - 2017-03-12 06:00 +0000
Re: va_start without the first argument? gordonb.gas5o@burditt.org (Gordon Burditt) - 2017-03-17 09:46 -0500
Re: va_start without the first argument? supercat@casperkitty.com - 2017-03-17 09:07 -0700
Re: va_start without the first argument? Thiago Adams <thiago.adams@gmail.com> - 2017-03-13 05:39 -0700
Re: va_start without the first argument? John Bode <jfbode1029@gmail.com> - 2017-03-13 11:40 -0700
Re: va_start without the first argument? Thiago Adams <thiago.adams@gmail.com> - 2017-03-13 12:01 -0700
Re: va_start without the first argument? jacobnavia <jacob@jacob.remcomp.fr> - 2017-03-13 23:17 +0100
Re: va_start without the first argument? Thiago Adams <thiago.adams@gmail.com> - 2017-03-13 16:15 -0700
Re: va_start without the first argument? jacobnavia <jacob@jacob.remcomp.fr> - 2017-03-14 00:26 +0100
Re: va_start without the first argument? kikiins@gmail.com - 2017-03-13 16:51 -0700
Re: va_start without the first argument? jacobnavia <jacob@jacob.remcomp.fr> - 2017-03-14 01:12 +0100
Re: va_start without the first argument? Thiago Adams <thiago.adams@gmail.com> - 2017-03-13 18:24 -0700
Re: va_start without the first argument? Thiago Adams <thiago.adams@gmail.com> - 2017-03-13 18:57 -0700
Re: va_start without the first argument? raltbos@xs4all.nl (Richard Bos) - 2017-03-14 07:59 +0000
Re: va_start without the first argument? Thiago Adams <thiago.adams@gmail.com> - 2017-03-14 06:45 -0700
Re: va_start without the first argument? Robert Wessel <robertwessel2@yahoo.com> - 2017-03-13 18:20 -0500
Re: va_start without the first argument? jacobnavia <jacob@jacob.remcomp.fr> - 2017-03-14 00:31 +0100
Re: va_start without the first argument? raltbos@xs4all.nl (Richard Bos) - 2017-03-13 16:51 +0000
Re: va_start without the first argument? jacobnavia <jacob@jacob.remcomp.fr> - 2017-03-11 19:29 +0100
Re: va_start without the first argument? Siri Cruise <chine.bleu@yahoo.com> - 2017-03-11 12:12 -0800
Re: va_start without the first argument? luser droog <luser.droog@gmail.com> - 2017-03-13 05:37 -0700
Re: va_start without the first argument? luser droog <luser.droog@gmail.com> - 2017-03-15 11:45 -0700
Re: va_start without the first argument? Thiago Adams <thiago.adams@gmail.com> - 2017-03-15 13:39 -0700
csiph-web