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

Similar documents
The Procedure Abstraction Part I Basics

Optimizer. Defining and preserving the meaning of the program

The Procedure Abstraction Part I: Basics

The Procedure Abstraction

CS 406/534 Compiler Construction Intermediate Representation and Procedure Abstraction

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

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

Naming in OOLs and Storage Layout Comp 412

CSE 504: Compiler Design. Runtime Environments

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

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

Runtime Support for Algol-Like Languages Comp 412

Intermediate Representations & Symbol Tables

CS 314 Principles of Programming Languages. Lecture 13

The Software Stack: From Assembly Language to Machine Code

Runtime Support for OOLs Object Records, Code Vectors, Inheritance Comp 412

Run-time Environments

Run-time Environments

Arrays and Functions

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

Procedure and Object- Oriented Abstraction

Special Topics: Programming Languages

Scope, Functions, and Storage Management

Concepts Introduced in Chapter 7

Run-time Environments - 2

CS415 Compilers. Intermediate Represeation & Code Generation

Compiler Theory. (Semantic Analysis and Run-Time Environments)

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

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

CS 406/534 Compiler Construction Putting It All Together

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

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

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

Compilers and computer architecture: A realistic compiler to MIPS

Implementing Subprograms

Register Allocation. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

CA Compiler Construction

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

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

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Programming Languages: Lecture 12

CS 314 Principles of Programming Languages

CS 415 Midterm Exam Spring SOLUTION

Parsing II Top-down parsing. Comp 412

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation

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

CS415 Compilers. Instruction Scheduling and Lexical Analysis

Storage in Programs. largest. address. address

Implementing Subroutines. Outline [1]

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

Short Notes of CS201

Code Shape II Expressions & Assignment

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

CS201 - Introduction to Programming Glossary By

CSE 307: Principles of Programming Languages

Implementing Subprograms

G Programming Languages - Fall 2012

CS /534 Compiler Construction University of Massachusetts Lowell

Chapter 10. Implementing Subprograms ISBN

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

EECS 6083 Intro to Parsing Context Free Grammars

CS415 Compilers. Lexical Analysis

Compilation 2014 Activation Records (Part 1)

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

Generating Code for Assignment Statements back to work. Comp 412 COMP 412 FALL Chapters 4, 6 & 7 in EaC2e. source code. IR IR target.

Programming Languages

Code Shape Comp 412 COMP 412 FALL Chapters 4, 5, 6 & 7 in EaC2e. source code. IR IR target. code. Front End Optimizer Back End

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Compiler Design. Code Shaping. Hwansoo Han

Run-Time Data Structures

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

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

CPS311 Lecture: Procedures Last revised 9/9/13. Objectives:

Weeks 6&7: Procedures and Parameter Passing

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

Run-Time Environments

CS 406/534 Compiler Construction Parsing Part I

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

Runtime Environments I. Basilio B. Fraguela

Special Topics: Programming Languages

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

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Lexical Analysis - An Introduction. Lecture 4 Spring 2005 Department of Computer Science University of Alabama Joel Jones

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

What about Object-Oriented Languages?

1 Lexical Considerations

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

The Processor Memory Hierarchy

This section provides some reminders and some terminology with which you might not be familiar.

Chapter 10 Implementing Subprograms

Computing Inside The Parser Syntax-Directed Translation. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Run Time Environments

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Ch. 11: References & the Copy-Constructor. - continued -

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Run-Time Environments

Operational Semantics of Cool

Intermediate Representations

Transcription:

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

Where are we? Well understood Engineering Source Code Front End IR Middle End IR Back End Machine code Errors The latter half of a compiler contains more open problems, more challenges, and more gray areas than the front half This is compilation, as opposed to parsing or translation Implementing promised behavior What defines the meaning of the program Managing target machine resources Registers, memory, issue slots, locality, power, These issues determine the quality of the compiler 2

The Procedure: Three Abstractions Control Abstraction Well defined entries & exits Mechanism to return control to caller Some notion of parameterization (usually) Clean Name Space Clean slate for writing locally visible names Local names may obscure identical, non-local names Local names cannot be seen outside External Interface Access is by procedure name & parameters Clear protection for both caller & callee Procedures permit a critical separation of concerns 3

The Procedure (Realist s View) Procedures allow us to use separate compilation Separate compilation allows us to build non-trivial programs Keeps compile times reasonable Lets multiple programmers collaborate Requires independent procedures Without separate compilation, we would not build large systems The procedure linkage convention Ensures that each procedure inherits a valid run-time environment and that the callers environment is restored on return The compiler must generate code to ensure this happens according to conventions established by the system 4

The Procedure (More Abstract View) A procedure is an abstract structure constructed via software Underlying hardware directly supports little of the abstraction it understands bits, bytes, integers, reals, and addresses, but not: Entries and exits Interfaces Call and return mechanisms may be a special instruction to save context at point of call Name space Nested scopes All these are established by a carefully-crafted system of mechanisms provided by compiler, run-time system, linkage editor and loader, and OS 5

Run Time versus Compile Time These concepts are often confusing to the newcomer Procedure linkages execute at run time Code for the procedure linkage is emitted at compile time The procedure linkage is designed long before either of these This issue (compile time versus run time) confuses students more than any other issue Keith Cooper (Rice University) 6

The Procedure as a Control Abstraction Procedures have well-defined control-flow The Algol-60 procedure call Invoked at a call site, with some set of actual parameters Control returns to call site, immediately after invocation 7

The Procedure as a Control Abstraction Procedures have well-defined control-flow The Algol-60 procedure call Invoked at a call site, with some set of actual parameters Control returns to call site, immediately after invocation s = p(10,t,u); int p(a,b,c) int a, b, c; int d; d = q(c,b);... 8

The Procedure as a Control Abstraction Procedures have well-defined control-flow The Algol-60 procedure call Invoked at a call site, with some set of actual parameters Control returns to call site, immediately after invocation s = p(10,t,u); int p(a,b,c) int a, b, c; int d; d = q(c,b);... int q(x,y) int x,y; return x + y; 9

The Procedure as a Control Abstraction Procedures have well-defined control-flow The Algol-60 procedure call Invoked at a call site, with some set of actual parameters Control returns to call site, immediately after invocation s = p(10,t,u); int p(a,b,c) int a, b, c; int d; d = q(c,b);... int q(x,y) int x,y; return x + y; 10

The Procedure as a Control Abstraction Procedures have well-defined control-flow The Algol-60 procedure call Invoked at a call site, with some set of actual parameters Control returns to call site, immediately after invocation s = p(10,t,u); int p(a,b,c) int a, b, c; int d; d = q(c,b);... int q(x,y) int x,y; return x + y; 11

The Procedure as a Control Abstraction Procedures have well-defined control-flow The Algol-60 procedure call Invoked at a call site, with some set of actual parameters Control returns to call site, immediately after invocation s = p(10,t,u); int p(a,b,c) int a, b, c; int d; d = q(c,b);... int q(x,y) int x,y; return x + y; Most languages allow recursion 12

The Procedure as a Control Abstraction Implementing procedures with this behavior Requires code to save and restore a return address Must map actual parameters to formal parameters q:(c x, b y) Must create storage for local variables (&, maybe, parameters) p needs space for d (&, maybe, a, b, & c) where does this space go in recursive invocations? s = p(10,t,u); int p(a,b,c) int a, b, c; int d; d = q(c,b);... int q(x,y) int x,y; return x + y; 13

The Procedure as a Control Abstraction Implementing procedures with this behavior Must preserve p s state while q executes Think about recursion Strategy: Create unique location for each procedure activation Can use a stack of memory blocks to hold local storage and return addresses s = p(10,t,u); int p(a,b,c) int a, b, c; int d; d = q(c,b);... int q(x,y) int x,y; return x + y; Compiler emits code that causes all this to happen at run time 14

Example: Dynamic vs. Static Views 0 int r () // declaration int d, s; 1 int q (x,y) // declaration int x,y; return x + y + d ; int p (a,b,c) // declaration 1 int a, b, c; int d; if () d = q (c,b); // call else d = p (a, d, c); // call s = p(10, d, s); // call s = p(11, s, d); // call 15

Example: Dynamic vs. Static Views 0 int r () // declaration int d, s; 1 int q (x,y) // declaration int x,y; return x + y + d; int p (a,b,c) // declaration 1 int a, b, c; int d; if () d = q (c,b); // call else d = p (a, d, c); // call s = p(10, d, s); // call s = p(11, s, d); // call activation tree p p q (1) dynamic (3) static r (2) dynamic activation records in runtime stack p q symbol table d:0.0 s:0.1 x:1.0 y:1.1 a:1.0 b:1.1 c:1.2 d:1.3 nesting level offset lexical scoping 16

The Procedure as a Name Space Each procedure creates its own name space Any name (almost) can be declared locally Local names obscure identical non-local names Local names cannot be seen outside the procedure Nested procedures are inside by definition We call this set of rules & conventions lexical scoping Examples C has global, static, local, and block scopes (Fortran-like) Blocks can be nested, procedures cannot Scheme has global, procedure-wide, and nested scopes (let) Procedure scope (typically) contains formal parameters 17

The Procedure as a Name Space Why introduce lexical scoping? Provides a compile-time mechanism for binding free variables Simplifies rules for naming & resolves conflicts How can the compiler keep track of all those names? The Problem At point p, which declaration of x is current? At run-time, where is x found? As parser goes in & out of scopes, how does it delete x? The Answer Lexically scoped symbol tables (see 5.7.3) 18

The Procedure as an External Interface OS needs a way to start the program s execution Programmer needs a way to indicate where it begins The main procedure in most languages When user invokes grep at a command line OS finds the executable OS creates a process and arranges for it to run grep grep is code from the compiler, linked with run-time system Starts the run-time environment & calls main After main, it shuts down run-time environment & returns When grep needs system services It makes a system call, such as fopen() UNIX/Linux specific discussion 19

Where Do All These Variables Go? Automatic & Local Keep them in the procedure activation record or in a register Automatic lifetime matches procedure s lifetime Static Procedure scope storage area affixed with procedure name File scope storage area affixed with file name Lifetime is entire execution Global One or more named global data areas One per program, Lifetime is entire execution 20

Placing Run-time Data Structures Classic Organization C o d e S G t l a & o t b i a c l H e a p 0 high Single Logical Address Space S t a c k Better utilization if stack & heap grow toward each other Code & data separate or interleaved Code, static, & global data have known size Ø Use symbolic labels in the code Heap & stack both grow & shrink over time This is a virtual address space 21

How Does This Really Work? The Big Picture Compiler s view virtual address spaces C o d e S G t l a & o t b i a c l H e a p S t a c k C o d e S G t l a & o t b i a c l H e a p S t a c k C o d e S G t l a & o t b i a c l H e a p S t a c k... C o d e S G t l a & o t b i a c l H e a p S t a c k OS s view... 0 high Hardware s view Physical address space_ 22

Lexical Scoping: Translating Local Names How does the compiler represent a specific instance of x? Name is translated into a static coordinate < level,offset > pair level is lexical nesting level of the procedure offset is unique within that scope Subsequent code will use the static coordinate to generate addresses and references level is a function of the table in which x is found Stored in the entry for each x offset must be assigned and stored in the symbol table Assigned at compile time Known at compile time Used to generate code that executes at run-time 23

Activation Record Basics ARP parameters register save area return value return address addressability caller s ARP local variables Space for parameters to the current routine Saved register contents If function, space for return value Address to resume caller Help with non-local access To restore caller s AR on a return (control link) Space for local values & variables (including spills) One AR for each invocation of a procedure 24

Next class Procedure abstraction Read EaC: Chapter 6.1 6.5 25