Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Subroutines and Control Abstraction.

Similar presentations


Presentation on theme: "CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Subroutines and Control Abstraction."— Presentation transcript:

1 CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Subroutines and Control Abstraction

2 Subroutines Execute an operation on behalf of a calling program unit. Subroutines may be parameterized. – The parameters in the definition of the subroutine are known as formal parameters. – The parameters passed in the subroutine call are known as actual parameters or arguments. – At the time of the call, actual parameters are mapped to formal parameters. Functions are subroutines that return a value.

3 Subroutines Each subroutine requires a subroutine frame (a.k.a. activation record or stack frame) to keep track of – arguments and return values – local variables and temporaries – bookkeeping information When a subroutine returns, its frame is removed

4 Stack Frame Arguments are accessed at positive offsets from the fp. Local variables and temporaries are accessed at negative offsets from the fp. Arguments to be passed to called routines are assembled at the top of the frame, using positive offsets from the sp.

5 Stack Frame The stack pointer (sp) register holds the address of the last used location at the top of the stack. The frame pointer (fp) register holds an address within a frame. Setup foo(a, b) – Move sp to allocate a new stack frame. – Copy args a, b into frame. – Copy return address into frame. – Set fp to point to new frame. – Maintain static chain or display. – Move PC to procedure address.

6 Stack Layout Allocation strategies – Static Code Globals Own variables Explicit constants (including strings, sets, other aggregates) Small scalars may be stored in the instructions themselves

7 Stack Layout with Subroutine Nesting Given the calling sequence A, E, B, D, C, in that order, frames will be allocated on the stack as shown at right, with the indicated static and dynamic links.

8 Review of Stack Layout Allocation strategies – Stack parameters local variables temporaries bookkeeping information – Heap dynamic allocation

9 Review of Stack Layout Contents of a stack frame – bookkeeping return PC (dynamic link) saved registers line number saved display entries static link – arguments and returns – local variables – temporaries

10 Static Chain Maintenance In languages with nested subroutines, the maintenance of the static chain must be performed by the caller, rather than the callee. The callee is nested (directly) inside the caller. The caller, therefore, passes its own frame pointer as the callee’s static link. The callee is k scopes “outward”-closer to the outer level of lexical nesting, where k ≥ 0. The caller dereferences its own static link k times and passes the result as the callee’s static link.

11 Typical Calling Sequence Caller’s task before the call: – Saves any caller-saves registers – Computes the values of arguments and moves them into the stack or registers – Computes the static link (if needed) and passes it as an extra, hidden argument – Jumps to the subroutine, simultaneously passing the return address on the stack or in a register Callee’s task before the execution: – Allocates a frame by subtracting an appropriate constant from the sp – Saves the old frame pointer into the stack, and assigns it an appropriate new value – Saves any callee-saves registers that may be overwritten by the current routine

12 Typical Calling Sequence Callee’s task after the execution: – Moves the return value (if any) into a register or a reserved location in the stack. – Restores callee-saves registers if needed. – Restores the fp and sp registers. – Jumps back to the return address. Caller’s task after the call: – Moves the return value to wherever it is needed. – Restores caller-saves registers if needed.

13 Parameter Passing Parameter passing modes: – In – Out – In+out Important parameter passing mechanisms: – Call by value (in) – Call by reference (in+out) – Call by result (out) – Call by value/result (in+out) – Call by name (in+out) – Call by need (in+out)

14 Call by Value Representative language: C Actual parameters are evaluated and their values are assigned to the formal parameters. Actual parameters are not affected by changes to the formal parameters Data can only be modified by passing pointers to the data swap(int *a, int *b) { int t = *a; *a = *b; *b = t; }

15 Call by Reference Representative language: FORTRAN SUBROUTINE SWAP(A,B) INTEGER A, B, C C=A A=B B=C END If an r-value appears as an argument, a temporary variable is created to hold the value, and this variable is passed by reference. SWAP(A,2) is equivalent to A = 2

16 Call by Reference Disadvantage: If an error occurs, it is hard to trace values since some side-effected values are in the environment of the caller. What happens when someone uses an expression argument for a by-reference parameter? (2*x)??

17 Call by Value and Call by Reference Provide call-by-value as well as call-by-reference. Representative languages: Pascal, C++ Call by value is similar to as in C. Call by reference is indicated by additional qualifiers. In Pascal:In C++: procedure swap(var a:integer, void swap(int &a, int &b) var b:integer) var t;{ int t; begin t := a; t = a; a := b; a = b; b := t b = t end}

18 Call by Result Representative language: Ada Implements out-mode semantics. Each formal parameter is a new local variable that exists within the scope of the function. No value is transmitted from the caller to the callee. Just before control is transferred back to the caller, the value of the formal parameter copied into the corresponding actual parameter. Each actual parameter MUST be a variable, not an expression.

19 Call by Result Parameter collisions may occur! – write(p1,p1);.. If the two formal parameters in write had different names, which value should go into p1? – Order in which actual parameters are copied determines their value! Typically implemented by copy – Inefficiency in storage allocation – inefficiency in copying value – For objects and arrays, the copy semantics are costly! – The added problem is that the value of the actual parameter can’t be used as the initial value of the formal parameter.

20 Call by Result void plus(by-value int a, by-value int b, by-result int c) { c = a+b; } void f() { int x = 3; int y = 4; int z; plus(x, y, z); write z; } Output: 7

21 Call by Result m, n : integer; procedure r (k,j : integer) begin k := k+1; j := j+2; end r; … m := 5; n := 3; r(m,n); write m,n; Error in procedure r: can’t use parameters which are uninitialized!

22 Call by Value-result Implements in-out mode semantics. Combination of pass-by-value and pass-by-result. Formal parameter is a new local variable that exists within the scope of the function. Value of actual parameter is used to initialize the formal parameter. Just before control is transferred back to the caller, the value of the formal parameter is transmitted back to the actual parameter.

23 Call by Value-result { m,n : integer; procedure r (k,j : integer) begin k := k+1; j := j+2; end r; … m := 5; n := 3; r(m,n); write m,n ; } Output: 6 5

24 Call by Value-result { c: array [1..10] of integer; m, n : integer; procedure r (k, j : integer) begin k := k+1; j := j+2; end r ; /* set c[m] = m */ m := 2; r(m, c[m]); write c[1], c[2], …, c[10]; } What element of c has its value changed? c[2]? c[3]? 3 4

25 Call by Value-result x : integer; procedure foo(y : out integer) y := 3; print x;... x := 2; foo(x); print x; If y is passed by reference the program will print 3 twice. If y is passed by value/result, it will print 2 and then 3.

26 Call by Reference vs. Call by Value-result { y: integer; procedure p(x: integer) { x := x + 1; x := x + y; } … y := 2; p(y); write y; } Result (call-by-reference): 6 { y: integer; procedure p(x: integer) { x := x + 1; x := x + y; } … y := 2; p(y); write y; } Result (call-by-value-result): 5

27 Call by Reference vs. Call by Value-result Call by reference Advantage: Is more efficient than copying Disadvantages: – Leads to aliasing: when there are two or more different names for the same storage location – Side effects not visible from code itself Call by value-result Has all advantages and disadvantages of call-by-value and call-by-result together.

28 Call by Name Algol 60 device – Defer calculation of the argument until needed; like textual substitution with name clashes resolved – THUNK - evaluates argument in caller’s environment and returns address of location containing the result Characteristics – Inefficient – Same as pass by reference for scalars

29 Call by Name void f(by-name int a, by-name int b) { b=5; b=a; } int g() { int i = 3; f(i+1,i); write i; } Output: 6

30 Call by Name

31 { c: array [1..10] of integer; m, n : integer; procedure r (k, j : integer) begin k := k+1; j := j+2; end r; /* set c[n] to n */ m := 2; r(m, c[m]); write m, c[m]; } m:= m+1 c[m] := c[m] + 2 m c[ ]

32 Call by Need Each actual parameter is evaluated in the caller’s context, on the first use of the corresponding formal parameter. The value of the actual parameter is then cached, so that subsequent uses of the corresponding formal parameter do not cause re-evaluation.

33 Call by Need Representative language: Haskell A memoized version of call-by-name; if the function argument is evaluated, that value is stored for subsequent uses Call by name: 3 evaluations doub {x = x + x;} doub (doub 8) = doub 8 + doub 8 = = 32 Call by need: 2 evaluations doub {x = x + x;} doub (doub 8) = doub 8 + doub 8 = doub 8 = 16

34 Call by Name vs. Call by Need void f(by-name int a, by-name int b) { b = a; b = a; } int g() { int i = 3; f(i+1, i); write i; } Output: 5 void f(by-need int a, by-need int b) { b=a; b=a; } int g() { int i = 3; f(i+1,i); write i; } Output: 4

35 Closures as Parameters A closure (a reference to a subroutine, together with its referencing environment) can be passed as a parameter. In Standard Pascal: A : array [low..high : integer] of integer; procedure apply_to_A(function f(n : integer) : integer); var i : integer; begin for i := low to high do A[i] := f(A[i]); end; In C and C++: int A[50]; void apply_to_A(int (*f)(int)) { int i; for (i = 0; i < sizeof(A); i++) A[i] = f(A[i]);

36 Parameter Passing Parameter passing modes.


Download ppt "CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Subroutines and Control Abstraction."

Similar presentations


Ads by Google