RE: Function Argument Evaluation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Young, Michael writes:
 > >> Good morning,
 > >>
 > >> Historically it's always been that arguments passed into functions get
 > >> evaluated from left to right
 > >
 > >Not true.
 > >
 > >> Ie. 'int foo(int a, char *b, float c) { }
 > >>
 > >> But I'm noticing that with gcc-4.1.1 (on Fedora Core 5), that does not
 > >> seem to be the case.  We have some code, granted it may be fragile or
 > >> broken code, that relies on the arguments being evaluated from left to
 > >> right and seems like that's not the case.  In my code the arguments  
 > >> seem
 > >> to be being evaluated right to left.
 > >
 > >I /think/ that with -O0 the arguments are evaluated left to right.  
 > >However, neither the standards nor gcc make any guarantee about  
 > >argument evaluation order. Not now and not historically either. Any  
 > >code that relies on a particular order of evaluation is broken.
 > >
 > >You've just been lucky (or not, depending on the point of view) up to  
 > >now. It is typical that such bugs are exposed when the optimization  
 > >level is changed, which is a good reason to test regularly with both - 
 > >O0 and -O2.
 > 
 > True - the standard doesn't say anything about the order in which parameter
 > values are passed/pushed on the stack.  (BTW - even ABIs aren't "standards", 
 > but rather "conventions".)  But it is absolutely necessary to know (and be 
 > able to explicitly define/control) this order if one is doing inter-language
 > or cross-tool development; without knowing the stack frame, how can an
 > assembler routine call a routine developed in 'C' or vice versa?  Most other
 > compilers I'm familiar with allow a declspec or define their call linkage
 > (standard, pascal, cdecl, etc.) to specify a particular order at compile-time.
 > (Obviously, functions defined as such are not candidates for the optimizations
 > to which you alluded.  But correctness trumps speed - getting invalid behavior
 > or bad results faster get me nowhere.)  Unfortunately, I looked through the gcc
 > documentation for something similar about a month ago, and the only thing I 
 > could find was an option that controls how stack frames are built - but this
 > was an option used when compiling gcc/g++ itself.
 > 
 > If anyone knows of switches that control the argument order in g++, please let
 > me know - I'll find the details in the docs (if it's there), but I didn't see 
 > anything when I looked.

What do you mean by "argument order" ?  The order of evaluation isn't
specified by anything.  The order in which arguments are pushed onto
the stack depends on the operating system, not on gcc's designe.  We
do whatever the OS needs.

Andrew.


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux