The Procedure Abstraction Part I: Basics

Similar documents
The Procedure Abstraction Part I Basics

Optimizer. Defining and preserving the meaning of the program

The Procedure Abstraction

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

CS 406/534 Compiler Construction Intermediate Representation and Procedure Abstraction

Intermediate Representations & Symbol Tables

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

Naming in OOLs and Storage Layout Comp 412

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

CSE 504: Compiler Design. Runtime Environments

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

What about Object-Oriented Languages?

CSE 504: Compiler Design. Intermediate Representations Symbol Table

Runtime Support for Algol-Like Languages Comp 412

Concepts Introduced in Chapter 7

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

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

CA Compiler Construction

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

CS 314 Principles of Programming Languages. Lecture 13

Symbol Tables. For compile-time efficiency, compilers often use a symbol table: associates lexical names (symbols) with their attributes

Procedure and Object- Oriented Abstraction

Chapter 9 Subroutines and Control Abstraction. June 22, 2016

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

Symbol Table Information. Symbol Tables. Symbol table organization. Hash Tables. What kind of information might the compiler need?

Runtime Environments I. Basilio B. Fraguela

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

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

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

Implementing Subroutines. Outline [1]

Implementing Subprograms

Procedure Calls Main Procedure. MIPS Calling Convention. MIPS-specific info. Procedure Calls. MIPS-specific info who cares? Chapter 2.7 Appendix A.

The Software Stack: From Assembly Language to Machine Code

CS415 Compilers Context-Sensitive Analysis Type checking Symbol tables

Arrays and Functions

Functions in MIPS. Functions in MIPS 1

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

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

CS 314 Principles of Programming Languages

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

Implementing Procedure Calls

Compiler construction

Compilers and computer architecture: A realistic compiler to MIPS

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

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

The compilation process is driven by the syntactic structure of the program as discovered by the parser

System Software Assignment 1 Runtime Support for Procedures

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

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

1 Lexical Considerations

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

Lecture 5. Announcements: Today: Finish up functions in MIPS

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

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

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

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

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

Absolute C++ Walter Savitch

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

Scope, Functions, and Storage Management

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

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

Semantic Analysis and Type Checking

Control Abstraction. Hwansoo Han

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

Scope. Chapter Ten Modern Programming Languages 1

Run-time Environment

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

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

CSE 307: Principles of Programming Languages

Playing with bird guts. Jonathan Worthington YAPC::EU::2007

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

Function Call Stack and Activation Records

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Lexical Considerations

G Programming Languages - Fall 2012

Chapter 10 Implementing Subprograms

Chapter 6 Introduction to Defining Classes

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

Storage in Programs. largest. address. address

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

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

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

3. Process Management in xv6

Today. Putting it all together

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

Announcements. Scope, Function Calls and Storage Management. Block Structured Languages. Topics. Examples. Simplified Machine Model.

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

Compiler Construction

Programming Languages: Lecture 12

CS 4201 Compilers 2014/2015 Handout: Lab 1

Code Generation. Lecture 12

Run-time Environments

Run-time Environments

CSE 230 Intermediate Programming in C and C++ Functions

CS 415 Midterm Exam Spring SOLUTION

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

Names, Scopes, and Bindings II. Hwansoo Han

Transcription:

The Procedure Abstraction Part I: Basics

Procedure Abstraction Begins Chapter 6 in EAC The compiler must deal with interface between compile time and run time Most of the tricky issues arise in implementing procedures Issues Compile-time versus run-time behavior Finding storage for EVERYTHING and mapping names to addresses Generating code to compute addresses Interfaces with other programs, other languages, and the OS Efficiency of implementation

Where are we? Well understood Engineering Source Code Front End IR Middle End IR Back End Machine code Errors Contains more open problems and more challenges 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

The Procedure & Its Three Abstractions

The Procedure as a Name Space There is a strict constraints that each procedure must adhere to!

The Procedure & Its Three Abstractions

The Procedure & Its Three Abstractions

The Procedure & Its Three Abstractions

The Procedure: Three Abstractions Control Abstraction Well defined entries & exits Mechanism to return control to caller 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 Invoked procedure can ignore calling context Procedures permit a critical separation of concerns

The Procedure (Realist s View) Procedures are the key to building large systems Requires system-wide contract Conventions on memory layout, protection, resource allocation calling sequences, & error handling Must involve architecture (ISA), OS, & compiler Provides shared access to system-wide facilities Storage management, flow of control, interrupts Interface to input/output devices, protection facilities, timers, synchronization flags, counters, Establishes a private context Create private storage for each procedure invocation Encapsulate information about control flow & data abstractions

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

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, linker and loader, and OS

Run Time versus Compile Time These concepts are often confusing to the newcomer Linkages execute at run time Code for the linkage is emitted at compile time The linkage is designed long before either of these Compile time versus run time can be confusing to CISC672 students. We will emphasize the distinction between them.

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

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);...

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;

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;

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;

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

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 (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; Compiler emits code that causes all this to happen at run time

The Procedure as a Control Abstraction Implementing procedures with this behavior Must preserve p s state while q executes 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

The Procedure as a Name Space Why introduce lexical scoping? Provides a compile-time mechanism for binding variables Simplifies rules for naming & resolves conflicts Lets the programmer introduce local names 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 The compiler must model the name space Lexically scoped symbol tables (see 5.7.3)

Do People Use This Stuff? C macro from the MSCP compiler #define fix_inequality(oper, new_opcode) \ if (value0 < value1) \ { \ Unsigned_Int temp = value0; \ value0 = value1; \ value1 = temp; \ opcode_name = new_opcode; \ temp = oper->arguments[0]; \ oper->arguments[0] = oper->arguments[1]; \ oper->arguments[1] = temp; \ oper->opcode = new_opcode; \ Declares a new name

Lexically-scoped Symbol Tables 5.7 in EaC The problem The compiler needs a distinct record for each declaration Nested lexical scopes admit duplicate declarations The interface insert(name, level ) creates record for name at level lookup(name, level ) returns pointer or index delete(level ) removes all names declared at level Many implementation schemes have been proposed We ll stay at the conceptual level Hash table implementation is tricky and detailed (see B.4) Symbol tables are compile-time structures the compiler use to resolve references to names. We ll see the corresponding run-time structures that are used to establish addressability later.

Example procedure p { int a, b, c procedure q { int v, b, x, w procedure r { int x, y, z. procedure s { int x, a, v r s q B0: { int a, b, c B1: { int v, b, x, w B2: { int x, y, z. B3: { int x, a, v

Lexically-scoped Symbol Tables High-level idea Create a new table for each scope Chain them together for lookup Sheaf of tables implementation insert() may need to create table it always inserts at current level lookup() walks chain of tables & returns first occurrence of name delete() throws away table for level p, if it is top table in the chain If the compiler must preserve the table (for, say, the debugger ), this idea is actually practical. Individual tables can be hash tables.