Lectures 07 to 08

Lectures 07 to 08 - Subprograms Are Implemented Using...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Subprograms Are Implemented Using Activation Records (AR): When the main/subprogram S calls subroutine/function F , the execution control will be changing from “ caller ” S to the “ callee ” F (upon encountering a subroutine CALL” statement). Since we need to resume the execution of S after returning from F , we need to save the state of the caller S in some data structure (e.g., record), that is called “ A ctivation R ecord ” ( AR ). Such saved state will be used to restore the activation of the caller upon the return from the callee subroutine, i.e., after executing the “ RETURN” statement at the callee . 1- All of its local&formal parameters (if any), and any temp values in registers (TEMP). The state of a computation (program in execution) consists of: 2- The I nstruction P ointer ( IP ) that points to the next statement to be executed. At the subroutine invocation (i.e., call time), the IP will hold the resume (return) address in the caller code. 3- Pointer to the caller’s AR called “ D ynamic L ink ” ( DL ). 1- Allocate a new AR for the callee . Steps taken upon subroutine invocation (call), via compiler generated code to be executed at run-time ( overhead! ): 2- Save the “ state ” of the caller in its AR . 3- Compute the actual parameters and transfer them to their corresponding formals , in the callee AR . 4- Place a pointer to the caller AR into the callee AR (the DL ). 5- Transfer the execution to the beginning of the callee code.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Implementation of Non-recursive Call, in FORTRAN: Code in the caller S : Call F(P 1 , P 2 , P 3 , …, P n ) Save Temp values in M[AR( S )].TEMP; M[AR( S )].IP := resume address (IP); M[AR( F )].PARM[1] := compute P 1 (address in case of by-reference); M[AR( F )].PARM[2] := compute P 2 (address in case of by-reference); *** M[AR( F )].PARM[n] := compute P n (address in case of by-reference); M[AR( F )].DL := Address of AR( S ); Go to entry ( F ) resume: Restore registers from M[AR( S )].TEMP; If F is a function then get its returned value. ----------------------------------- Code in the callee F entry ( F ): …… the machine code of F …. : ………. RETURN If F is a function, place the returned value where accessible to the caller; Go to ( M[M[AR( F )].DL].IP);
Background image of page 2
Data Structures: Operators are overloaded : it is when an operator is used with integer, double/float, and complex operands, e.g., the “+” in FORTRAN, i.e., the “+” carries more than one meaning (problem?!? If so, how does the compiler solve it?) . “ Operators are overloaded ” is an ad-hoc polymorphism . Early FORTRAN did not allow
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

Lectures 07 to 08 - Subprograms Are Implemented Using...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online