<p dir="ltr"><br>
On 2 Apr 2014 21:00, "Sarath Kodali" <<a href="mailto:sarath@dummy.com">sarath@dummy.com</a>> wrote:<br>
><br>
> On Wednesday, 2 April 2014 at 14:43:44 UTC, Iain Buclaw wrote:<br>
>>><br>
>>><br>
>>><br>
>>> Please do not get confused between operands evaluation order in an<br>
>>> expression and arguments passing order to a function. Those are two<br>
>>> different things. I was talking about both of them because both of them are<br>
>>> involved in the evaluation of a()[] = b()[] + c()[]. To a programmer this is<br>
>>> an expression that should follow expression evaluation rules. To a compiler<br>
>>> implementer, this is a builtin function call whose arguments should be<br>
>>> evaluated such that the expression evaluation rules are not broken.<br>
>>><br>
>><br>
>> Right.  But order of evaluation is Language-specific, order of pushing<br>
>> arguments is Target-specific.  Both are completely indifferent from<br>
>> each other, and this is what I think you are not understanding.<br>
>><br>
>><br>
> I started my career, 19 years back, as a C compiler developer. So I know what is evaluation order and argument passing order. And more importantly, the discussion is about the *evaluation order* of "a()[] = b()[] + c()[]" and not about what I understand or don't! So if you have any valid points that says why this expression should be evaluated in LTR order (i.e. first a then b and then c) let us discuss that.<br>

> You can write a small code that evaluates "a()[] = b()[] + c()[]" before and after the proposed modifications and check whether the evaluation order is same w.r.t dmd. DMD v2.64 evaluates first b, then c and then a. This behaviour conforms to the D spec.<br>

></p>
<p dir="ltr">Array ops follow a different behaviour to what is what normally expected.  In a() = b() + c(), the order is abc, not bca.</p>
<p dir="ltr">The fact that the current behaviour is written in the spec is not a good reason to keep it.<br></p>
<p dir="ltr">>>> If you read the last para in my first post, I was talking about argument<br>
>>> pushing order *not* evaluation order for function args. The function<br>
>>> argument passing order (called calling convention) is not defined by C spec,<br>
>>> but by C ABI spec of any architecture. In all the C calling conventions, the<br>
>>> first few arguments are passed in registers and the remaining on the stack.<br>
>>> On Linux+x86, all the arguments are passed on the stack. For C, the<br>
>>> arguments that are passed on the stack are in reverse order i.e RTL. Since<br>
>>> the proposal was to change the argument evaluation order for extern(C)<br>
>>> functions,<br>
>><br>
>><br>
>> And the pushing order is unaffected, so why bring it up in the first place?<br>
>><br>
><br>
> Let me take an example to explain what I'm trying to say.<br>
><br>
> extern (C) int foo(int a, int b);<br>
><br>
> void main(void)<br>
> {<br>
>     foo(a(), b());<br>
> }<br>
><br>
> With RTL function argument evaluation order and with push instructions, the above code gets compiled by dmd as (only relevant asm code shown) (on x86)<br>
><br>
> main:<br>
>    call b<br>
>    push %eax<br>
>    call a<br>
>    push %eax<br>
>    call foo<br>
><br>
> Now if the evaluation order of function args is changed to LTR, the new asm code would be<br>
><br>
> main:<br>
>     call a<br>
>     mov %eax, %esi<br>
>     call b<br>
>     push %eax<br>
>     push %esi<br>
>     call foo<br>
><br>
> Notice the additional mov instruction to save the return value of a() in a temporary.  This is the impact that I'm talking about. Now if dmd backend uses mov instructions to push args on to the stack instead of push, then there will not be a need for temporary. But the code size will increase as push is only 1 byte where as mov %eax offset(%esp) is 3 to 4 bytes long.<br>

><br>
> Asm code with LTR func args evaluation order for extern(C) foo with mov instrs<br>
> main:<br>
>      call a<br>
>      mov %eax, (%esp)<br>
>      call b<br>
>      mov %eax, 0x4(%esp)<br>
>      call foo<br>
><br>
> Notice that the args are still pushed in RTL order.<br>
></p>
<p dir="ltr">Your point?<br>
</p>