Re: Function Argument Evaluation

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

 



On Dec 6, 2006, at 10:21 AM, Young, Michael wrote:

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.

Are you mixing two concepts? The "order of evaluation" is not the same as the
calling convention.

For example, on AIX the calling convention is to have the first parameter in register 3, the second in register 4. But they can get evaluated in any order (and
they never really get pushed on the stack).

Perry Smith ( pedz@xxxxxxxxxxxxxxxx )
Ease Software, Inc. ( http://www.easesoftware.com )

Low cost SATA Disk Systems for IBMs p5, pSeries, and RS/6000 AIX systems



[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