Chapter 9 Subprograms • Specification: name, signature, actions • Signature: number and types of input arguments, number and types of output results –Book calls it protocol Subprograms • Actions: direct function relating input values to output values; side effects on global state and subprogram internal state • May depend on implicit arguments in form of non-local variables Subprogram As Abstraction • Subprograms encapsulate local variables, specifics of algorithm applied –Once compiled, programmer cannot access these details in other programs Subprogram As Abstraction • Application of subprogram does not require user to know details of input data layout (just its type) –Form of information hiding Subprogram Parameters • Formal parameters: names (and types) of arguments to the subprogram used in defining the subprogram body • Are types checked? • Is number of arguments fixed? • Actual parameters: arguments supplied for formal parameters when subprogram is called Establishing the correspondence • positional - good for small lists • Keyword parameters Example: (Ada) SUB(Y=>ACT_Y, MAX=>100) Disadvantage is that the caller must know names is callee • May be default values (Ada, C++) if no parameter is supplied Subprogram Parameters • Parameters may be used to: – Deliver a value to subprogram – in mode – Return a result from subprogram – out mode – Both – in out mode • Most languages use only in mode • Ada uses all Parameter Passing • Aliases may be created • a formal parameter is a nonlocal variable • the same data object passed for two parameters CALL S(X,X) • With aliasing, interesting problems in optimizations occur. Parameter Passing (In Mode) • Pass-by-value: Subprogram makes local copy of value (r-value) given to input parameter • Assignments to parameter not visible outside program • Most common in current popular languages • If pass array by value, entire array does not get copied C is not consistent here • transmission by constant value - as in C++ (make it by reference for speed, but don’t allow changes) • no assignment to param is allowed OR is allowed, but only changes local copy • For protection, then the formal parameter may not be transmitted to another subprogram except as a constant value parameter • May be implemented as transmission by value or reference • costly to pass by value if parameter is large - storage and time Parameter Passing (In Mode) • Pass-by-name: text for argument is passed to subprogram and expanded in each place parameter is used – Roughly same as using macros – Note, you can’t evaluate late without having “code” to execute – You also need to know a context for evaluating non-local variables • Achieves late binding Pass-by-name Example integer INDEX= 1; integer array ARRAY[1:2] procedure UPDATE (PARAM); integer PARAM begin PARAM := 3; INDEX := INDEX + 1; PARAM := 5; end UPDATE(ARRAY[INDEX]); Pass-by-name Example • Previous code puts 3 in ARRAY[1] and 5 in ARRAY[2] • How is this accomplished if the compiled code must work for ANY argument that is passed? • PARAM can be x, x+y, 2*t[6*x]+7 • How can you generate code for UPDATE when you don’t know what is passed? • New interest in functional languages means more interest in delayed evaluation. • Very flexible, but inefficient. Difficult to implement. Confusing to read/write. • Some simple operations are not possible with pass by name. • Lazy evaluation is another form of late binding. Only evaluate when it becomes necessary. • Substitute name or expression (in calling environment) for formal parameter • The name location binding is delayed until (and established fresh each time) the formal parameter is encountered. • Implemented by passing parameter-less subprograms (thunks) rather than variable name. An expression needs to be evaluated IN the proper environment. Don't have mechanism to do that other than thru procedure call. • Whenever formal parameter is referenced, a call is made to thunk, which evaluates the parameter in the proper (caller) environment and returns proper resulting value (or location) • Example: procedure R(var i,j: integer); begin var m:boolean; m := true; i := i + 1; j := j + 1; write(i,j); end; m := 2; for(i=0;i<10;i++) c[i]=10*i; R(m,c[m]); • pass by reference: adds 1 to m and c[2] Pass by name: adds 1 to m and c[3] Example for Call by Name b1: begin real x,y; y := 0.0; procedure G(t): name t; begin integer w; integer x; w := 10; y := 20; x := 50 print t x:= 0; print t end G; b2: begin real y; y := 0.5; x := 1.0; call G(y-x) end end thunk() return(y-x) end; • If name parameter is on left hand side, thunk would have to return the address of the element (rather than the value). • Not a problem for in–only parameters IN OUT parameters • transmission by reference formal parameter is local object of type pointer If expression is passed as an in/out parameter: a temporary location may be passed (and then the copy is changed, not the original) • Disadvantages: – access slower as is indirect (always follow a pointer to access), but passing is fast (only copy a pointer, not a whole structure) – may make inadvertent changes to parameters (if out only was desired) Parameter Passing Out Mode) (In • Same effect can be had in pass-byvalue by passing a pointer IN OUT parameters – Value-restore. Copy value in on call Copy changed value back on return. – Used to save cost of indirect access. – aliases may be problematic - especially likely if pass same parameter twice. Then if arguments are changed, original values may be changed differently depending on order of change (if value-restore) Value-restore • value copied at time of call and copied back at time of return has the same as call by reference if – the subprogram terminates normally, and – the called subprogram cannot also access the actual parameter through an alias – parameters are names for same argument doit(X,X). • Need to know order arguments are copied back. • Need to know whether address is computed again before copying back. XX(i,a[i]) OUT only parameters • formal parameter is a local variable with no initial value • copied back at termination of subprogram Pass by result • Explicit function Values: may be considered an extra OUT parameter • return(expr) • value to be returned by assignment to function name • if return is an address (e.g., list[index]), when is it evaluated? time of call? time of return? Subprogram Implementation • Subprogram definition gives template for its execution –May be executed many times with different arguments Subprogram Implementation • Template holds code to create storage for program data, code to execute program statements, code to delete program storage when through, and storage for program constants • Layout of all but code for program statements called activation record Actions to pass parameters: • Point of call: parameters evaluated and list of parms is set up • Perform type checking and promotion (if needed) • point of entry: prologue of called routine completes association by copying pointers into locations or copying entire contents • point of exit: epilogue of called routine must copy result values into actual parameters transmitted by result or value-result Subprogram Implementation • Code segment Activation Record Code to create Return point activation record inst Static link Program code Dynamic link Code to delete Result data activation record inst Input parameters const 1 Local parameters const n Activation Records • Code segment generated once at compile time • Activation record instances created at run time • Fresh activation record instance created for each call of subprogram – Usually put in a stack (runtime stack) – “return point” inserted first Activation Records • Static link – pointer to bottom of activation record instance of static parent – Used to find non-local variables • Dynamic link – pointer to end of activation record instance of the caller – Used to delete subprogram activation record instance from runtime stack at completion (by resetting stack pointer) Subprograms as parameters: • • • • Corresponding formal parameter is of type subprogram name Problems static type checking: cannot determine if number of arguments is correct Needs not just name, but full procedure specification: type returned, number order and type of args. nonlocal references (free variables) – variables without bindings assume same nonlocal environment as if inline expansion were used usually not what was intended – nonlocal reference means the same thing during execution of the subprogram passed as a parameter as it would if the subprogram were invoked at the point where it appears as an actual parameter in the parameter list • • Need to create the correct nonlocal environment fairly straightforward with static chain method determine correct static chain pointer for the subprogram parameter and pass that along as part of the information transmitted with a subprogram parameter As in passing labels as parameters, need an (ip, ep) (instruction pointer, environment pointer) pair Are three choices of environment for evaluating unbound variables of function passed as parameter 1. the environment of the subprogram to which it is passed (shallow binding) Not appropriate for block structures languages because of static binding of variables 2. the environment of the subprogram which is passed (deep binding) 3. the environment of the subprogram which passes the subprogram (ad hoc) not used