Parameter Passing in Compiler Design
Parameter Passing
- The communication medium among procedures is known as parameter passing. The values of the variables from a calling procedure are transferred to the called procedure by some mechanism.
R- value
- The value of an expression is called its r-value. The value contained in a single variable also becomes an r-value if its appear on the right side of the assignment operator.
- R-value can always be assigned to some other variable.
L-value
- The location of the memory(address) where the expression is stored is known as the l-value of that expression.
- It always appears on the left side if the assignment operator.
Types of Parameter Passing
Different ways of passing the parameters to the procedure
- Call by Value
- Call by reference
- Copy restore
- Call by name
Call by Value
- In call by value the calling procedure pass the r-value of the actual parameters and the compiler puts that into called procedure’s activation record.
- Formal parameters hold the values passed by the calling procedure, thus any changes made in the formal parameters does not affect the actual parameters.
Call by Value
Call by Reference
- In call by reference the formal and actual parameters refers to same memory location.
- The l-value of actual parameters is copied to the activation record of the called function. Thus the called function has the address of the actual parameters.
- If the actual parameters does not have a l-value (eg- i+3) then it is evaluated in a new temporary location and the address of the location is passed.
- Any changes made in the formal parameter is reflected in the actual parameters (because changes are made at the address).
Call by Reference
Call by Copy Restore
- In call by copy restore compiler copies the value in formal parameters when the procedure is called and copy them back in actual parameters when control returns to the called function.
- The r-values are passed and on return r-value of formals are copied into l-value of actuals.
Call by Copy
Call by Name
- In call by name the actual parameters are substituted for formals in all the places formals occur in the procedure.
- It is also referred as lazy evaluation because evaluation is done on parameters only when needed.
Call by Name