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
number of arguments.Why can't compiler generate more information for such cases.Please
explain the root problem in this convention.
Regards,
Chai-yo
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
>> 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?
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?
mebbe u can take a look at this if interested
http://msdn.microsoft.com/library/techart/msdn_dllart.htm
http://www.cs.utexas.edu/users/dianelaw/cs310/templates/stack.htm
bookmak this site
http://www.cs.utexas.edu/users/dianelaw/cs310/templates/templats.htm
http://msdn.microsoft.com/library/techart/msdn_dllart.htm
http://www.cs.utexas.edu/users/dianelaw/cs310/templates/stack.htm
bookmak this site
http://www.cs.utexas.edu/users/dianelaw/cs310/templates/templats.htm
I seemed to recall that the calling convention is not stack based only but can b register based so see
http://msdn.microsoft.com/library/devprods/vs6/visualc/pgalpha/procedure_calling_convention_on_alpha.htm
and
http://www.cs.utexas.edu/users/dianelaw/cs310/templates/regs.htm
lastly
http://msdn.microsoft.com/library/devprods/vs6/visualc/vccore/_core_.2f.gd.2c_2f.gr.2c_2f.gz.htm
http://msdn.microsoft.com/library/devprods/vs6/visualc/pgalpha/procedure_calling_convention_on_alpha.htm
and
http://www.cs.utexas.edu/users/dianelaw/cs310/templates/regs.htm
lastly
http://msdn.microsoft.com/library/devprods/vs6/visualc/vccore/_core_.2f.gd.2c_2f.gr.2c_2f.gz.htm
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?