Link to home
Start Free TrialLog in
Avatar of chai_yo123
chai_yo123

asked on

Parameter Passing Order

Can anybody explain why pascal order of pushing parameters is not suitable for variable
number of arguments.Why can't compiler generate more information for such cases.Please
explain the root problem in this convention.

Regards,
Chai-yo
Avatar of _lychee_
_lychee_

in pascal arguments pushed from left to right, so the rightmost argument is the shallowest element on the stack...

y this is bad for variable arguments: the info on no. of args is normally the 1st argument (y not the last? i guess that makes type checking much more difficult?)... so when the routine is entered, the code can't determine how long the variable list is....

i guess it's possible for the compiler to push an extra word of information on how many params were passed, but y use that when u can just use the C convention?
Another problem with the PASCAL calling convention is that the called function is responsible for cleaning up the stack. Whereas in the C calling convention the caller cleans up the stack after the call. The problem is that the called function does not know how many parameters are on the stack (it must be known during compile-time, but it can vary...), so it cannot clean up the stack.

>> I guess it's possible for the compiler to push an extra word
>> of information on how many params were passed
I don't thinks so... and not only the number is neccesairy but also the size, otherwise the called function still can't clean up the stack. This would also mean a special calling convention for var-args...

>> the info on no. of args is normally the 1st argument (y not
>> the last? i guess that makes type checking much more
>> difficult?)...
Even impossible, for the adress of that 1st argument can not be retrieved without the information in that same first argument...

Luc
actually the called function can (with quite a lot of hassle) clean up the stack....

it pops the cs/ip, pops everything else, pushes back the cs/ip and then returns... that's on the intel archi at least...

but again, y go thru the hassle when u have the (wonderful?) c calling convention?
LucHoltkamp is correct with the reason why PASCAL calling conventions cannot take variable parameters - the number of arguments to remove from the stack would be unknown in that you would only know the minimum number of arguments and that the number of arguments could be different on a per caller basis.

The language Pascal does runtime parameter checking so it requires that all of the parameters' types are known when it generates code.  With variable argument C functions - it is not possible for the compiler to know the true type of any one variable argument. It would be up to the called function to have a way of discerning the type of argument - printf uses the format masks to coerce values to match and assumes the type of the corresponding argument but you could just as easily pass garbage to printf and it wouldn't know until it blew up.


ASKER CERTIFIED SOLUTION
Avatar of isasori
isasori

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial