CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.

Slides:



Advertisements
Similar presentations
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Advertisements

Ch. 8 Functions.
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
Chapter 9 Subprograms Specification: name, signature, actions Signature: number and types of input arguments, number and types of output results –Book.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Implementing Subprograms
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison- Wesley. All rights reserved. 1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
Run-Time Storage Organization
Run time vs. Compile time
Chapter 9: Subprogram Control
ISBN Chapter 10 Implementing Subprograms –Semantics of Calls and Returns –Implementing “Simple” Subprograms –Implementing Subprograms with.
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
Compiler Construction
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 9 Def: The subprogram call and return operations of a language are together called its subprogram.
1 Run-Time Environments. 2 Procedure Activation and Lifetime A procedure is activated when called The lifetime of an activation of a procedure is the.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
CSC 8505 Compiler Construction Runtime Environments.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
1 Run-Time Environments Chapter 7 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
Chapter Ten: Implementing Subprograms Lesson 10. Implementing?  Previous lesson: parameter passing  In, out, inout  By value  By reference  Passing.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
ISBN Chapter 10 Implementing Subprograms.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Implementing Subprograms
Implementing Subprograms Chapter 10
Implementing Subprograms
Implementing Subprograms
Run-Time Storage Organization
Run-Time Storage Organization
Introduction to Compilers Tim Teitelbaum
Run-Time Environments
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Run-Time Environments
Implementing Subprograms
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
UNIT V Run Time Environments.
Languages and Compilers (SProg og Oversættere)
Runtime Environments What is in the memory?.
Implementing Subprograms
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Implementing Subprograms
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack Static chain Functions and the stack

CSI 3120, Implementing subprograms, page 2 The environment in block-structured languages Subprogram calls are strictly nested: the caller waits until the callee has terminated. call B A call C B call D CD

CSI 3120, Implementing subprograms, page 3 Every subprogram activation is represented as an activation record on the activation stack. The activation record of the callee is placed at the top of stack, directly above the activation record of the caller. Activation records vary in size. Sizes are usually determined at compile time, unless semidynamic arrays are supported, but every activation record contains the same kind of information about the caller and the callee. The environment in block-structured languages (2)

CSI 3120, Implementing subprograms, page 4 Information about the caller A pointer to the caller’s activation record (the next record on the stack, but this pointer is necessary to handle varying record sizes); this gives us access to the chain of previous callers up to the main program. The return address (what the caller will do after this subprogram call has terminated). If this is a function, the address where the function value should be stored.

CSI 3120, Implementing subprograms, page 5 Information about the callee Scoping information — a pointer to the activation record of the enclosing block (this need not be the same unit as the caller). Local variables and constants. Formal parameters (copies or only pointers). Temporary storage (to evaluate expressions).

CSI 3120, Implementing subprograms, page 6 Memory is allocated to procedure calls in segments of stack storage, as much as is necessary to represent the callee’s block. Activation records free bottom top Information about the callee (2) The compiler generates a prologue, next the translation of the subprogram body and finally an epilogue.

CSI 3120, Implementing subprograms, page 7 Prologue: entering a subprogram Get a segment of free stack storage — a stack frame — and move the top-of-the-stack pointer up. Put in the new stack frame all data about the caller and the callee.

CSI 3120, Implementing subprograms, page 8 Epilogue: exiting a subprogram Return a value (if the subprogram is a function). Remove the segment from the stack, move the top-of-the-stack pointer down. Jump to the return address (continue the statements in the caller's body).

CSI 3120, Implementing subprograms, page 9 Run-time memory Run-time memory is divided into three parts. 1.Code area: main program, subprograms. 2.Data area: the run-time stack (all variables are represented—global variables are local in the activation record of the main program).

CSI 3120, Implementing subprograms, page 10 Run-time memory (2) Run-time memory -- continued. 3.Control information: Current instruction pointer IP indicates the instruction about to be executed. Current environment pointer EP shows the activation record of the current block, giving access to local and non-local data.

CSI 3120, Implementing subprograms, page 11 In the following examples, we will assume a simple model: the pointer to the caller’s activation record, the pointer to the enclosing block, the return address, local data (if any), actual parameters (if any). Return addresses will be symbolic—see the boxes on the next page. Run-time memory (2)

CSI 3120, Implementing subprograms, page 12 The structure of the activation stack program M( input,output); var A, B: integer; procedure P( C: integer; var D: integer); begin {P} {P1} C := C + 2; {P2} D := D + 3; {P3} write('P:',A,B,C, D); {P4} end; procedure Q( var C:integer); var B: integer; procedure R( C: integer); begin {R} {R1} C := 29; {R2} P(B, C); {R3} write('R:',A, B, C); {R4} end; {continued} begin {Q} {Q1} B := 23; {Q2} R(A); {Q3} P(B, C); {Q4} write('Q:', A, B, C); {Q5} end; begin {M} {M1} A := 6; {M2} B := 17; {M3} P(B, A); {M4} write('M:', A, B); {M5} Q(A); {M6} write('M:', A, B); {M7} end.

CSI 3120, Implementing subprograms, page 13 The structure of the activation stack (2) (dynamic link) (static link) (return address) A’ 9 B’ 17 F1 M6 C’’  A’ B’’ 23 F2 Q3 C’’’ 29 F3 F1 R3 D’’’’  C’’’ C’’’’ 23 situation after P in R in Q in M called: IP = P1, EP = F4 F1 F2 F3 F4 M Q R P stack frame program unit dynamic link static link

CSI 3120, Implementing subprograms, page 14 Another example program Main; var A, B: integer; procedure P; begin {P} {L1P} A := A + 1; {L2P} B := B + 1; {L3P} end; procedure Q; var B: integer; procedure R; var A: integer; begin {R} {L1R} A := 16; {L2R} P; {L3R} write(A, B); {L4R} end; {continued} begin {Q} {L1Q} B := 11; {L2Q} R; {L3Q} P; {L4Q} write(A, B); {L5Q} end; begin {Main} {L1m} A := 1; {L2m} B := 6; {L3m} P; {L4m} write(A, B); {L5m} Q; {L6m} write(A, B); {L7m} end.

CSI 3120, Implementing subprograms, page 15 Another example (2) (dynamic link) (static link) (return address) A 1 B 6 situation in Main just before call to P: IP = L3m, EP = F1 F1 Main

CSI 3120, Implementing subprograms, page 16 Another example (3) (dynamic link) (static link) (return address) A 1 B 6 F1 L4m F1 F2 Main P situation after P in Main called: IP = L1P, EP = F2

CSI 3120, Implementing subprograms, page 17 Another example (4) (dynamic link) (static link) (return address) A 2 B 7 situation after P in Main terminated: IP = L4m, EP = F1 F1 Main

CSI 3120, Implementing subprograms, page 18 Another example (5) (dynamic link) (static link) (return address) A 2 B 7 F1 L6m B F1 F2 Main Q situation after Q in Main called: IP = L1Q, EP = F2

CSI 3120, Implementing subprograms, page 19 Another example (6) (dynamic link) (static link) (return address) A 2 B 7 F1 L6m B 11 F2 L3Q A F1 F2 F3 Main Q R situation after R in Q in Main called: IP = L1R, EP = F3

CSI 3120, Implementing subprograms, page 20 Another example (7) (dynamic link) (static link) (return address) A 2 B 7 F1 L6m B 11 F2 L3Q A 16 F3 F1 L3R F1 F2 F3 F4 Main Q R P situation after P in R in Q in Main called: IP = L1P, EP = F4

CSI 3120, Implementing subprograms, page 21 Another example (8) (dynamic link) (static link) (return address) A 3 B 8 F1 L6m B 11 F2 L3Q A 16 F1 F2 F3 Main Q R situation after P in R in Q in Main terminated: IP = L4R, EP = F3

CSI 3120, Implementing subprograms, page 22 Another example (9) (dynamic link) (static link) (return address) A 3 B 8 F1 L6m B 11 F1 F2 Main Q situation after P in Q in Main terminated: IP = L3Q, EP = F2

CSI 3120, Implementing subprograms, page 23 Another example (10) (dynamic link) (static link) (return address) A 3 B 8 F1 L6m B 11 F2 F1 L4Q F1 F2 F3 Main Q P situation after P in Q in Main called: IP = L1P, EP = F3

CSI 3120, Implementing subprograms, page 24 Another example (11) (dynamic link) (static link) (return address) A 4 B 9 F1 L6m B 11 F1 F2 Main Q situation after P in Q in Main terminated: IP = L4Q, EP = F2

CSI 3120, Implementing subprograms, page 25 Another example (12) (dynamic link) (static link) (return address) A 4 B 9 situation after Q in Main terminated: IP = L6m, EP = F1 F1 Main

CSI 3120, Implementing subprograms, page 26 Static chains Variables represented on the stack are not accessed by name. In a language with static scoping, a variable must, however, be located by moving up the chain of static nesting. An address of variable V on the stack is composed of two numbers that tell us: how many activation records up the chain is the record R that contains V, how far V is from the beginning of R.

CSI 3120, Implementing subprograms, page 27 In the situation when Q in Main has been called: Main.Q.B(0, 3) Main.A(1, 3) Main.Bunavailable (dynamic link) (static link) (return address) A 2 B 7 F1 L6m B F1 F2 Main Q Static chains (2)

CSI 3120, Implementing subprograms, page 28 In the situation when P in R in Q in Main has been called: Main.Q.R.Aunavailable Main.Q.Bunavailable Main.A(1, 3) Main.B(1, 4) (dynamic link) (static link) (return address) A 2 B 7 F1 L6m B 11 F2 L3Q A 16 F3 F1 L3R F1 F2 F3 F4 Main Q R P Static chains (3)

CSI 3120, Implementing subprograms, page 29 Functions and the stack program Main; var A: integer; function G(N: integer): integer; begin if N <= 1 then G := 1 else G := G(N-1) * N end; begin A := G(3); write(A) end. Assigning locations to program fragments must be a little more elaborate… L1Gif N <= 1 then L2Gvalue := 1 L3Ggoto L7G L4GG(N-1) L5G  temp L6Gvalue := temp * N L7G L1MG(3) L2M  temp L3MA := temp L4Mwrite(A) L5M

CSI 3120, Implementing subprograms, page 30 Functions and the stack (2) (dynamic link) (static link) (return address) A ? F1F1 F1F1 L2M N 3 value ? F2F2 F1F1 L5G value ? F3F3 F1F1 L5G N 1 value 1 G in G in G in Main returns with 1: IP = L3G, EP = F 4 F1F1 F2 F3 F4 Main G G G N 2