home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18257 < prev    next >
Encoding:
Text File  |  1992-12-12  |  3.4 KB  |  76 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: Calling convention: C vs PASCAL
  5. Message-ID: <1992Dec12.171137.17577@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <1992Dec11.225756.26780@eng.ufl.edu>
  8. Distribution: usa
  9. Date: Sat, 12 Dec 1992 17:11:37 GMT
  10. Lines: 64
  11.  
  12. zzang@stat.ufl.edu (zzang) writes:
  13.  
  14. >the C pushs the args from right to left onto the stack when calling a function.
  15. >the PASCAL pushs the args from left to right.
  16. >I was told that the C convention results in larger as well as slower program.
  17. >(due to some cleanup work of the stack after call??)
  18.  
  19. >can someone tell me why?
  20.  
  21. A partially correct answer was posted to this question.
  22.  
  23. Neither the C nor Pascal language specifications require the parameter
  24. ordering mentioned, but they are common implementation techniques.
  25.  
  26. C allows a function to be called with more parameters than were declared
  27. for it.  The called function can't in general know how many parameters
  28. were passed, or their sizes.  The simplest way around this is to
  29. pass parameters in reverse order.  The leftmost (first) parameter
  30. is always in a known location, and the remainder of the declared
  31. parameters can be reliably found by the called function.  (Other
  32. techniques are of course possible; this is a popular solution.)
  33.  
  34. Pascal requires that all function calls match the declaration, so the
  35. called function always knows the number and type of each parameter.
  36. It is easier for a compiler to process the actual arguments in the
  37. order they are encountered, so Pascal compilers typically pass
  38. parameters from left to right.  (To pass them in reverse order means
  39. the compiler has to save up all the parameter expressions first.
  40. Passing them in order means you don't have to save up the expressions.)
  41.  
  42. A Pascal compiler designed to be compatible with a C compiler, or on a
  43. system where calling sequences were standardized, would use the same
  44. parameter passing scheme as the C compiler.
  45.  
  46. When parameters are passed on the stack, the stack must be restored at
  47. some point (pop off the parameters).  If the called function does not
  48. know the number or size of passed parameters, it can't do the job.
  49. In that case, the caller must pop the stack.  If the caller restores
  50. the stack after each function call, you have code to pop the stack
  51. at each place a function is called.  If the called function restores
  52. the stack, you have such code once per function.  There is no difference
  53. (necessarily) in speed, but the code size is larger if the caller pops.
  54.  
  55. Some compilers generate faster code by not popping the stack after every
  56. function call, but only when absolutely necessary.  This reduces the
  57. discrepancy in code size between caller popping versus callee popping.
  58. Example:
  59.     foo(1,2,3);
  60.     bar(2,3,4)
  61.     baz(3,4,5);
  62.     return;
  63. It is not necessary to pop the stack after the calls to foo and bar
  64. unless you are worried about total stack size.  It is sufficient to
  65. pop the stack at the return, when the stack must look like it did
  66. when the function was called.
  67.  
  68. Finally, some machines have a pop-and-return instruction.  The callee
  69. can pop the stack simultaneously with the return, needing no extra
  70. instruction.  When the callee doesn't know how much to pop, it can't
  71. take advantage of this.  In this case, the usual C technique would
  72. result in larger and slower code than the usual Pascal technique.
  73. -- 
  74.  
  75. Steve Clamage, TauMetric Corp, steve@taumet.com
  76.