CS 314 Principles of Programming Languages. Lecture 13

Similar documents
CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages. Lecture 11

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

CA Compiler Construction

G Programming Languages - Fall 2012

Concepts Introduced in Chapter 7

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

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

CS 314 Principles of Programming Languages. Lecture 9

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

CS 314 Principles of Programming Languages. Lecture 16

Chap. 8 :: Subroutines and Control Abstraction

Programming Languages

Chapter 8 :: Subroutines and Control Abstraction. Final Test. Final Test Review Tomorrow

CIT Week13 Lecture

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

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

System Software Assignment 1 Runtime Support for Procedures

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

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

Principles of Programming Languages

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

Scope, Functions, and Storage Management

Special Topics: Programming Languages

Chapter 9 :: Subroutines and Control Abstraction

Lecture08: Scope and Lexical Address

Runtime Support for Algol-Like Languages Comp 412

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

Programming Languages

CSE 307: Principles of Programming Languages

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

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

Procedure and Object- Oriented Abstraction

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

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

Optimizer. Defining and preserving the meaning of the program

Run-time Environments - 2

The Procedure Abstraction

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

Implementing Subprograms

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

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

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

Class Information ANNOUCEMENTS

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

Run Time Environments

The Procedure Abstraction Part I: Basics

LECTURE 14. Names, Scopes, and Bindings: Scopes

Memory Management and Run-Time Systems

Run-time Environment

Code Generation & Parameter Passing

Weeks 6&7: Procedures and Parameter Passing

CS558 Programming Languages

Intermediate Representations & Symbol Tables

Chapter 5 Names, Binding, Type Checking and Scopes

CS 345. Functions. Vitaly Shmatikov. slide 1

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

Run-Time Environments

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

Programming Languages: Lecture 12

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Chapter 10. Implementing Subprograms ISBN

CS 314 Principles of Programming Languages. Lecture 21

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Wednesday, October 15, 14. Functions

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

Names, Scopes, and Bindings II. Hwansoo Han

Acknowledgement. CS Compiler Design. Semantic Processing. Alternatives for semantic processing. Intro to Semantic Analysis. V.

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

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

5. Semantic Analysis!

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

UNIT V Sub u P b ro r g o r g a r m a s

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

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

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

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Control Abstraction. Hwansoo Han

Chapter 10 Implementing Subprograms

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

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

Special Topics: Programming Languages

Chapter 9 Subprograms

Compiler Design Spring 2017

Symbol Tables. ASU Textbook Chapter 7.6, 6.5 and 6.3. Tsan-sheng Hsu.

CS 406/534 Compiler Construction Intermediate Representation and Procedure Abstraction

CS 480 Fall Runtime Environments. Mike Lam, Professor. (a.k.a. procedure calls and heap management)

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

Run-time Environments

NOTE: Answer ANY FOUR of the following 6 sections:

Run-time Environments

Chapter 5. Names, Bindings, and Scopes

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

CSE 504: Compiler Design. Runtime Environments

Scope. Chapter Ten Modern Programming Languages 1

CS 415 Midterm Exam Spring 2002

Semantic Analysis. Compiler Architecture

Lexical Considerations

Transcription:

CS 314 Principles of Programming Languages Lecture 13 Zheng Zhang Department of Computer Science Rutgers University Wednesday 19 th October, 2016 Zheng Zhang 1 CS@Rutgers University

Class Information Reminder: HW5 posted, due this Saturday October 22, 11:55pm EDT. Reminder: Project 1 due in five days, October 23, 11:55pm EDT. Midterm: November 2, in class, closed-book, closed-notes. Zheng Zhang 2 CS@Rutgers University

Review: Lexical / Dynamic Scope lexical Non-local variables are associated with declarations at compile time Find the smallest block syntactically enclosing the reference and containing a declaration of the variable dynamic Non-local variables are associated with declarations at run time Find the most recent, currently active run-time stack frame containing a declaration of the variable Zheng Zhang 3 CS@Rutgers University

Review: Context of Procedures Two contexts: static placement in source code (same for each invocation) dynamic run-time stack context (different for each invocation) Scope Rules Each variable reference must be associated with a single declaration (ie, an offset within a stack frame). Two choices: 1. Use static and dynamic context: lexical scope 2. Use dynamic context: dynamic scope Easy for variables declared locally, and same for lexical and dynamic scoping Harder for variables not declared locally, and not same for lexical and dynamic scoping Zheng Zhang 4 CS@Rutgers University

Review: Lexical and Dynamic Scoping Example Scope of a declaration: Portion of program to which the declaration applies Program x, y: integer // declarations of x and y Procedure B // declaration of B y, z: real // declaration of y and z... y = x + z // occurrences of y, x, and z if (...) call B // occurrence of B Procedure C // declaration of C x: real // declaration of x... call B // occurrence of B... call C // occurrence of C call B // occurrence of B Zheng Zhang 5 CS@Rutgers University

Review: Lexical and Dynamic Scoping Example Calling chain: MAIN C B B access links main control links x y FP C x B y z B increasing memory addresses y z Zheng Zhang 6 CS@Rutgers University

Symbol Table Is a compile time data structure. Maps variable to their declarations. Stores attributes of variables needed, for instance, for type checking and code generation, e.g., (nesting-level, offset) pairs. There are different implementation choices for symbol tables. One uses a stack of local scopes (block structured symbol table). Zheng Zhang 7 CS@Rutgers University

Review: Lexical scoping (de Bruijn notation) Symbol table matches declarations and occurrences. Each variable name can be represented as a pair (nesting level, local index). Program (1,1), (1,2): integer // declarations of x and y Procedure B // declaration of B (2,1), (2,2): real // declaration of y and z... // occurrences of y, x, and z ( ) (2,1) = (1,1) + (2,2) if (...) call B // occurrence of B Procedure C // declaration of C (2,1): real // declaration of x... call B // occurrence of B... call C // occurrence of C call B // occurrence of B Zheng Zhang 8 CS@Rutgers University

Review: Access to non-local data How does the code find non-local data at run-time? Real globals visible everywhere translated into an address at compile time Lexical scoping view variables as (level,offset) pairs table) (compile-time symbol look-up of (level,offset) pair uses chains of access links (at run-time) Dynamic scoping variable names must be preserved look-up of variable name uses chains of control links (at run-time) Zheng Zhang 9 CS@Rutgers University

Review: Access to non-local data (lexical scoping) What code (ILOC) do we need to generate for statement ( )? (2,1) = (1,1) + (2,2) What do we know? 1. The nesting level of the statement is level 2. 2. Register r 0 contains the current FP (frame pointer). 3. (2,1) and (2,2) are local variables, so they are allocated in the activation record that current FP points to; (1,1) is a non-local variable. 4. Two new instructions: LOAD R x R y means R y MEM(R x ) STORE R x R y means MEM(R y ) R x Zheng Zhang 10 CS@Rutgers University

Access to non-local data (lexical scoping) What code do we need to generate for statement ( )? (2,1) = (1,1) + (2,2) (1,1) LOADI #4 => r1 // offset of local variable // in frame (bytes) LOADI #-4 => r2 // offset of access link // in frame (bytes) ADD r0 r2 => r3 // address of access link in frame LOAD r3 r4 // get access link; r4 now // contains one-level-up FP ADD r4 r1 => r5 // address of first local variable // in frame LOAD r5 => r6 // get content of variable (2,2) LOADI #8 => r7 // offset of local variable in // frame (bytes) ADD r0 r7 => r8 // address of second local variable // in current frame LOAD r8 => r9 // get content of variable + ADD r6 r9 => r10 // (1,1) + (2,2) (2,1) LOADI #4 => r11 // offset of local variable in frame (bytes) ADD r0 r11 => r12 // address of first local variable // in current frame = STORE r10 => r12 // (2,1) = (1,1) + (2,2) Zheng Zhang 11 CS@Rutgers University

Access to non-local data (lexical scoping) Given a (level,offset) pair, what s the address? Two classic approaches (run-time) access links displays (static links) Zheng Zhang 12 CS@Rutgers University

Access to non-local data (lexical scoping) To find the value specified by (l, o) need current procedure level, k if k = l, is a local value if k > l, must find l s activation record follow k l access links k < l cannot occur Zheng Zhang 13 CS@Rutgers University

Maintaining access links If procedure p is nested immediately within procedure q, the access link for p points to the activation record of the most recent activation of q. calling level k + 1 procedure 1. pass my FP as access link 2. my backward chain will work for lower levels calling procedure at level l k 1. find my link to level l 1 and pass it 2. its access link will work for lower levels Zheng Zhang 14 CS@Rutgers University

The display To improve run-time access costs, use a display. table of access links for lower levels lookup is index from known offset takes slight amount of time at call a single display or one per frame Zheng Zhang 15 CS@Rutgers University

Display management Single global display: on entry to a procedure at level l save the level l display value push FP into level l display slot on return restore the level l display value simple method Zheng Zhang 16 CS@Rutgers University

Procedures Modularize program structure Argument: information passed from caller to callee (actual parameter) Parameter: local variable whose value (usually) is received from caller (formal parameter) Procedure declaration procedure name, formal parameters, procedure body with local declarations and statement lists, optional result type example: void translate(point *p, int dx) Zheng Zhang 17 CS@Rutgers University

Parameters Scott: Chapter 9.3 Parameter Association Positional association: Arguments associated with formals one-by-one; example: C, Pascal, Scheme, Java. Keyword association: formal/actual pairs; mix of positional and keyword possible; example: Ada procedure plot(x, y: in real; z: in boolean)... plot (0.0, 0.0, z true)... plot (z true, x 0.0, y 0.0) Parameter Passing Modes pass-by-value: C, Pascal, Ada (in parameter), Scheme, Algol 68 pass-by-result: Ada (out parameter) pass-by-value-result: Ada (in out parameter) pass-by-reference: Fortran, Pascal (var parameter) pass-by-name (not really used any more): Algol60 Zheng Zhang 18 CS@Rutgers University

Review: Stack Frame Scott: Chap. 9.1-9.3; ALSU Chap. 7.1-7.3 Run-time stack contains frames for main program and each active procedure. Each stack frame includes: 1. Pointer to stack frame of caller (control link for stack maintainance and Direction of stack growth dynamic scoping) (lower addresses) 2. Return address (within calling procedure) fp 3. Mechanism to find non-local variables (access link for lexical scoping) 4. Storage for parameters, local variables, and final values sp Arguments to called routines Temporaries Local variables Saved regs., static link Saved fp Return address (Arguments from caller) Current frame Previous (calling) frame Zheng Zhang 19 CS@Rutgers University

Pass-by-value c: array[1..10] of integer; m, n: integer; procedure r(k, j: integer) k := k+1; j := j+2; r;... m := 5; n := 3 r(m,n); write m,n; Output: 5 3 Advantage: Argument protected from changes in callee Disadvantage: Copying of values takes execution time and space, especially for aggregate values (e.g.:arrays, structs). Zheng Zhang 20 CS@Rutgers University

Pass-by-reference c: array[1..10] of integer; m, n: integer; procedure r(k, j: integer) k := k+1; j := j+2; r;... m := 5; n := 3 r(m,n); write m,n; Output: 6 5 Advantage: more efficient than copying Disadvantage: leads to aliasing: there are two or more names for the same storage location; hard to track side effects Zheng Zhang 21 CS@Rutgers University

Pass-by-result c: array[1..10] of integer; m, n: integer; procedure r(k, j: integer) k := k+1; ==> ERROR: CANNOT USE PARAMETERS j := j+2; WHICH ARE UNINITIALIZED r;... m := 5; n := 3 r(m,n); write m,n; Output: program doesn t compile or has runtime error Zheng Zhang 22 CS@Rutgers University

Pass-by-result c: array[1..10] of integer; m, n: integer; procedure r(k, j: integer) k := 1; ==> HERE IS ANOTHER PROGRAM j := 2; THAT WORKS r;... m := 5; n := 3 r(m,m); ==> NOTE: CHANGED THE CALL write m,n; Output: 1 or 2? Problem: order of copy-back makes a difference; implementation depent. Zheng Zhang 23 CS@Rutgers University

Pass-by-value-result c: array[1..10] of integer; m, n: integer; procedure r(k, j: integer) k := k+1; j := j+2; r;... m := 5; n := 3 r(m,n); write m,n; Output: 6 5 Problem: order of copy-back can make a difference; implementation depent. Zheng Zhang 24 CS@Rutgers University

Pass-by-value-result c: array[1..10] of integer; m, n: integer; procedure r(k, j: integer) k := k+1; j := j+2; r;... /* set c[m] = m */ m := 2; r(m,c[m]); ==> WHAT ELEMENT OF c IS ASSIGNED TO? write c[1], c[2],... c[10]; Output: 1 4 3 4 5... 10 on entry 1 2 4 4 5... 10 on exit Problem: When is the address computed for the copy-back operation? At procedure call (procedure entry), just before procedure exit, somewhere inbetween? (Example: ADA on entry) Zheng Zhang 25 CS@Rutgers University

Aliasing Aliasing: More than two ways to name the same object within a scope Even without pointers, you can have aliasing through (global formal) and (formal formal) parameter passing. j, k, m: integer; procedure q(a,b: integer); b := 3; m := m*a;... q(m,k); ==> global/formal <m,a> ALIAS PAIR q(j,j); ==> formal/formal <a,b> ALIAS PAIR write y; Zheng Zhang 26 CS@Rutgers University

Comparison: by-value-result vs. by-reference Actual parameters need to evaluate to L-values (addresses). y: integer; procedure p(x: integer); x := x+1; ==> ref: x and y are ALIASED x := x+y; ==> val-res: x and y are NOT ALIASED... y := 2; p(y); write y; Output: - pass-by-reference: 6 - pass-by-value-result: 5 Note: by-value-result: Requires copying of parameter values (expensive for aggregate values); does not have aliasing, but copy-back order depence; Zheng Zhang 27 CS@Rutgers University

Next Lectures Roadmap Introduction to functional languages; read Scott Chapter 10 Lambda calculus Zheng Zhang 28 CS@Rutgers University