CS 4100 Block Structured Languages. Fixed vs Variable. Activation Record. Activation Records. State of an Activation

Similar documents
Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Implementing Subprograms

Special Topics: Programming Languages

CS 314 Principles of Programming Languages. Lecture 13

CS415 Compilers. Procedure Abstractions. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Run Time Environments

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

G Programming Languages - Fall 2012

The Procedure Abstraction

Principles of Programming Languages

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Chapter 10. Implementing Subprograms ISBN

Chapter 9. Def: The subprogram call and return operations of a language are together called its subprogram linkage

Functions and Procedures

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Compiler Construction

CS 314 Principles of Programming Languages

Compiler Construction

Programming Languages: Lecture 12

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

Attributes of Variable. Lecture 13: Run-Time Storage Management. Lifetime. Value & Location

Run-time Environments - 2

LECTURE 14. Names, Scopes, and Bindings: Scopes

CIT Week13 Lecture

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

Procedure and Function Calls, Part II. Comp 412 COMP 412 FALL Chapter 6 in EaC2e. target code. source code Front End Optimizer Back End

Concepts Introduced in Chapter 7

Run-Time Environments

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Informatica 3 Marcello Restelli

System Software Assignment 1 Runtime Support for Procedures

CA Compiler Construction

Principles of Programming Languages

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Special Topics: Programming Languages

Procedure and Object- Oriented Abstraction

CSE 504: Compiler Design. Runtime Environments

CSE 307: Principles of Programming Languages

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

The Activation Record (AR)

Optimizer. Defining and preserving the meaning of the program

Runtime Support for Algol-Like Languages Comp 412

Lecture 9: Procedures & Functions. CS 540 George Mason University

Memory Management and Run-Time Systems

Scope, Functions, and Storage Management

Overview of Course. Names, References, Pointers Expressions, Control Flow Types Blocks, Procedures Exceptions Modularity Object-Oriented Programming

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Today's Topics. Last Time Modelling Scopes and Visibility The Run Stack, the Dynamic Pointer Stack, the Display (LL,ON) addressing

Syntax Errors; Static Semantics

Chapter 10. Implementing Subprograms

Object Oriented Paradigm Languages

Today's Topics. CISC 458 Winter J.R. Cordy

Chapter 10 Implementing Subprograms

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

CS 314 Principles of Programming Languages

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

CSc 520 Principles of Programming Languages

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Intermediate Representations & Symbol Tables

Programming Languages Third Edition. Chapter 7 Basic Semantics

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Module 27 Switch-case statements and Run-time storage management

The Procedure Abstraction Part I: Basics

Compiler Construction

Compiler Construction

Implementing Subprograms

CSC 2400: Computer Systems. Using the Stack for Function Calls

Procedures: Advanced Topics Chapter 12

Runtime Environments I. Basilio B. Fraguela

Scope. Chapter Ten Modern Programming Languages 1

CSc 520 Principles of Programming Languages. Questions. rocedures as Control Abstractions... 30: Procedures Introduction

Implementing Subroutines. Outline [1]

CS 345. Functions. Vitaly Shmatikov. slide 1

MIDTERM EXAM (Solutions)

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

Compilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1

Chapter 8 ( ) Control Abstraction. Subprograms Issues related to subprograms How is control transferred to & from the subprogram?

CSE 307: Principles of Programming Languages

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

6. Names, Scopes, and Bindings

COP4020 Programming Languages. Names, Scopes, and Bindings Prof. Robert van Engelen

Chapter 3:: Names, Scopes, and Bindings (cont.)

CSc 520 final exam Wednesday 13 December 2000 TIME = 2 hours

Chapter 3:: Names, Scopes, and Bindings (cont.)

Run-time Environments

Run-time Environments

Control Abstraction. Hwansoo Han

Chapter 5 Names, Binding, Type Checking and Scopes

Weeks 6&7: Procedures and Parameter Passing

What about Object-Oriented Languages?

Typical Runtime Layout. Tiger Runtime Environments. Example: Nested Functions. Activation Trees. code. Memory Layout

Compiler Construction

CS213. Machine-Level Programming III: Procedures

Run-Time Environments

Digital Forensics Lecture 3 - Reverse Engineering

Code Generation. Lecture 12

Transcription:

Chapter 6: Implementation of Block-Structure CS 4100 Block Structured Languages From Principles of Programming Languages: Design, Evaluation, and Implementation (Third Edition), by Bruce J. MacLennan, Chapter 6, and based on slides by Istvan Jonyer Addressing implementation aspects of block-structured languages (Pascal and Algol) Fortran (and pseudocode) not block structured We ll focus on Pascal, since most languages these days are Pascal-like Algol is block structured 1 2 Activation Record Represents the state of a procedure Fixed vs Variable Program has two major components Fixed part Code (the program itself) Does not change during runtime Variable part Activation record Dynamically created and deleted at runtime We ll focus on this part 3 4 State of an Activation Point of execution (instruction pointer) Stored in IP of activation record (and IP register of processor) Usually points to next instruction Context of execution (scope/environment) Environment pointer (EP) Local context Local activation record Non-local context Non-local activation record 5 Activation Records Local variables and formal parameters are contained in the activation record Create and delete correspond to entry and exit Context of a statement Names declared in current procedure + Names declared in surrounding procedures For multiple bindings, innermost declaration is used (if name not found in current activation record, look to outer A/Rs successively) 6 1

Static Link How to keep track of outer scopes? (p214) Static link points to outer activation record Each context (A/R) has static link to outer scope Static links form a chain all the way to top level (global scope, and beyond to OS) Static chain reflects the static structure of the program The way procedures are nested Ends at global scope program a(); var N: integer; procedure b(sum: real); var i: integer; avg: real; Data: array[1..10] of real; procedure c(val: real); writeln (Data[i]); ; // c // b... ; // b // a... ; // a 7 8 Contour Diagram of Static Structure of Previous Program (a) N (b) sum i avg Data (c) val EP Pointers EP points to active local context SP points to register of active context IP points to next instruction outer activation record Environment of declaration Keeps track of outer scopes DL (coming soon) points to callers A/R Talked about this in Fortran 9 10 SP EP Activation Record for Procedures val Data avg i sum N Activation record of (c) Activation record of (b) Activation record of (a) 11 Variable Addressing Name lookup is done at compile time Names are not actually looked up at runtime Names are bound to addresses in activation record We need two addresses for accessing a variable How far we have to follow the static link Where the variable is defined Offset of variable in activation record 12 2

Terminology Static nesting level How deep the scope is where variable is defined (from global scope) Number of contour lines surrounding declaration or use Static distance Distance between the variable s declaration and use Offset Variables position inside activation record Fetching a Variable Notation M[i]: memory at address i EP: environment pointer (how to get to A/R) offset(v): relative offset of variable v in activation record (how to find in A/R) reg.x: processor register (EP,IP,SP) General case (v is local) fetch M[reg.EP + offset(v)] 13 14 Examples Activation Record for Procedures Get variable sum (with offset 1) at static distance of 1 ARP: activation record pointer ARP := M[reg.EP]; fetch M[ARP + 1]; Get variable N (with offset 1) at static distance of 2 ARP := M[reg.EP]; ARP := M[ARP]; fetch M[ARP + 1]; 15 SP EP val Data avg i sum N Activation record of (c) Activation record of (b) Activation record of (a) 16 Dynamic Link Dynamic vs Static Link How is dynamic link different from static link? Can we do with just one? Both are needed for static scoping Dynamic link is enough for dynamic scoping Static link Points to environment of declaration Dynamic link Points to caller Can restore caller s state on exit 17 Static chain Q P Q P Q P B Dynamic chain B() 18 3

Procedure Activation Three steps Save state of caller In local activation record Create activation record of callee Transmit parameters to callee Establish dynamic link from caller Enter callee At its first instruction Saving the Caller s State Saving address where caller must resume after returning from call Saving locals and non-locals No action is required Locals are already stored in AR Access to non-locals is already established () Saving processor registers Registers must be saved in AR Platform-specific (not discussed) Not visible to programmer 19 20 Creating Callee s AR Callee s AR has following components PAR: parameters Parameters are placed here by caller M[callee s AR].PAR[1] := evaluation of parameter 1; IP: resumption address Not used until making procedure call : static link Set to environment of definition Computed from static nesting levels of procedures M[callee s AR]. := reg.ep (if defined in current scope) DL: dynamic link Set to caller s AR (EP register) M[callee s AR].DL := reg.ep 21 Final Steps Install callee s AR as current activation record reg.ep := callee s AR; Include callee s AR in stack officially reg.sp := reg.sp + size(callee s AR); goto entry(callee); Both entry point and AR size are known at compile time Goto = reg.ip := entry(callee) 22 Procedure Exit We have to effectively reverse the entry procedure Delete callee s activation record Subtract size of AR from stack reg.sp := reg.sp size(callee s AR) Restore the state of the caller Reinstalling the caller s context reg.ep := M[reg.EP].DL; Resume execution of caller reg.ip := M[reg.EP].IP (goto M[reg.EP].IP) 23 Non-Local GOTOs Local GOTO Simple machine jump to address Non-local GOTO Requires restoration of environment Must manipulate runtime stack Analogous to returning from a procedure call 24 4

B() goto 1; 1: Example 25 Implementation How do we find the scope for the label? Static nesting level is kept in symbol table at compile time Static difference sd can be computed and found runtime Steps involved: Scan down static chain sd times sd times: reg.ep := M[reg.EP]. Remove ARs from top of stack reg.sp := reg.sp + size(ar of label) Transfer execution to point of label (constant) goto address(label) 26 Displays Traversing static chains is proportional to length of chain Would be nice if it was constant Solution Store the address of activation record for each environment (not procedure call!) in array This array is called the display D Accessing static nesting levels is easy D[snl] Accessing variables is now only two steps, always! fetch M[D[snl] + offset(variable)] 27 Static Chains vs. Displays Operation Static Chain Display Local variable 1 2 Non-local variable sd+1 2 Procedure call sd+3 6 Procedure return 2 5 SC values are estimates Displays o Better for variables o Worse for procedure calls 28 Blocks Pascal does not have blocks But Algol, C, Ada and many others do Blocks require activation records Thus, entering and exiting a block is analogous with calling and returning from a procedure Can they be implemented in the same way? Yes! 29 Block vs. Procedure Some efficiency hacks are possible with blocks Blocks are always called from the same place! and returns to the same place! No need to save IP (resume address) of caller No need to save processor registers Environment is always the same Environment of definition = Surrounding block Static and dynamic links are the same No parameters No need to evaluate and copy parameters 30 5

Improvements Simplified structure LV: local variables Block can have local variables (vs. compound statements) IP: resumption address Block may call procedure : static link Remove dynamic link, since they are the same Entry-Exit Entry: M[reg.SP]. := reg.ep; reg.ep := reg.sp; reg.sp := reg.sp + size(block AR) Exit reg.sp := reg.sp - size(block AR) reg.ep := M[reg.EP].; 31 32 6