Hi Stacey,
  ----- Original Message ----- 
  From: Stacey Verner 
  To: NZ Borland Developers Group - Delphi List 
  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.

Always this order? Or does it vary sometimes if there are a different number of 
parameters? or if the mix of parameters varies, ie between static values, 
in-line expressions, function invocations?


  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. 

It may be that the two registers it uses for parameters 1 and 2 are also used 
in calculating parameter addresses in any call; or used in evaluating 
expressions in parameters; if so, parameters 1 and 2 can't be set into the 
registers until all the rest of the parameters have been evaluated. Also, 
registers containing parameters of the main function can't be set before 
calling any functions in the parameters, if they will be used for parameters of 
those functions.

  The documentation also says that is supposed to pass the from left to right, 
but it obviously doesn't.

Do you mean the order in which parameter expressions are evaluated? Yes, if 
that's defined, the compiler should respect it. Or the documentation should be 
changed to included a caveat about the possible results of the compiler's 
optimisation.



You'd be safest to call your functions and save their results in temporary 
variables which you then pass to the main function. That way you can control 
the order of the side effects yourself and it'll work whatever the compiler 
does.





Brian



   

  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: [EMAIL PROTECTED]

  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/



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


  _______________________________________________
  Delphi mailing list
  [email protected]
  http://ns3.123.co.nz/mailman/listinfo/delphi
_______________________________________________
Delphi mailing list
[email protected]
http://ns3.123.co.nz/mailman/listinfo/delphi

Reply via email to