[DUG] Delphi Calling Convetion is Weird!!

Dennis Chuah dennis_cs_chuah at hotmail.com
Fri Feb 16 14:02:46 NZDT 2007


Its not weird ... that is just how the optimiser works.  If you write code 
that depends on the order expression are evaluated in a function call, you 
will always be at the mercy of the optimiser.  Turn the optimiser off and 
your code behaves differently.  It is better to assign the expressions to 
temporary variables and use the variables in the function call.  That way, 
not only is your code easier to read, you are explicitly telling the 
compiler the order in which you want to evaluate those expressions.

Just as a aside, the C calling convention always passes from right to left, 
and the caller cleans up the stack.  Pascal calling convention is from left 
to right and the function cleans up the stack.

----- Original Message ----- 
From: "Stacey Verner" <stacey at cjntech.co.nz>
To: "NZ Borland Developers Group - Delphi List" <delphi at ns3.123.co.nz>
Sent: Friday, February 16, 2007 11:34 AM
Subject: [DUG] Delphi Calling Convetion is Weird!!


After an odd bug and a bit of testing I have found that the parameters
in Delphi (tested 6 and 2006) are evaluated very strangely.



The 3rd parameter is evaluated first followed by 4, 5, 6 etc and then
the 2nd and then the 1st.



This appears to be because the default calling convention is register
which (from the documentation) puts up to 3 parameters in registers and
the rest on the stack. It seems to process the stack ones first from
parameters 3 onwards (Why 3? Shouldn't it be 4 if we are putting 3 into
registers. Must be only putting 2 in the registers), and then the first
2 in reverse order into the registers. The documentation also says that
is supposed to pass the from left to right, but it obviously doesn't.



You can download an example to see for yourself!



http://www.verner.co.nz/download/FunctionCall.zip



If you are interested how we found this, we have a simple function that
strips the first delimited item from a string returning the item and
removing it from the input string. You can have a string something like:



LString := 'Stacey,G,Verner';



and call:



LFirstName := Extract(LString, ',');

LInitial := Extract(LString, ',');

LLastName := Extract(LString, ',');



In this case if we had a function like:



function FullName(PFirstName, PInitial, PLastName: String): String;



and you call it with:



LName := FullName(Extract(LString, ','), Extract(LString, ','),
Extract(LString, ','));



then we'll get the wrong result.



Stacey



Stacey Verner             Ph:   +64-9-4154790
Software Developer        Fax:  +64-9-4154791
                          DDI:  +64-9-4154797
                          Email: stacey at cjntech.co.nz
<BLOCKED::mailto:stacey at cjntech.co.nz>

CJN Technologies Ltd.

PO Box 302-278, North Harbour, Auckland 1330, New Zealand
12 Piermark Drive, North Harbour, Auckland, New Zealand
Visit our website at http://www.cjntech.co.nz/
<BLOCKED::http://www.cjntech.co.nz/>




--------------------------------------------------------------------------------


> _______________________________________________
> Delphi mailing list
> Delphi at ns3.123.co.nz
> http://ns3.123.co.nz/mailman/listinfo/delphi
> 



More information about the Delphi mailing list