CSc 520 Principles of Programming Languages

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

520 Principles of Programming Languages. Arithmetic. Variable Declarations. 19: Pascal

Special Topics: Programming Languages

System Software Assignment 1 Runtime Support for Procedures

G Programming Languages - Fall 2012

CSc 520 Principles of Programming Languages

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

Implementing Subprograms

CSc 520 Principles of Programming Languages. ameter Passing Reference Parameter. Call-by-Value Parameters. 28 : Control Structures Parameters

Chapter 10. Implementing Subprograms ISBN

CSc 520 Principles of Programming Languages

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

CA Compiler Construction

Programming Languages: Lecture 12

CS 314 Principles of Programming Languages. Lecture 13

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

520 Principles of Programming Languages. Types. Types. 21: Ada. Modula-2 90% of the power of Ada at 10% of the cost.

Semantic Analysis and Type Checking

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

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

CSc 520 Principles of Programming Languages

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

CS558 Programming Languages

Memory Management and Run-Time Systems

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

Lexical Considerations

Run-Time Environments

Implementing Subprograms

Runtime Environments I. Basilio B. Fraguela

CS 314 Principles of Programming Languages

Concepts Introduced in Chapter 7

Lexical Considerations

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

Run-time Environments - 2

1 Lexical Considerations

Informatica 3 Marcello Restelli

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

Chapter 10 Implementing Subprograms

The PCAT Programming Language Reference Manual

Run-time Environment

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

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

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

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

Topic 7: Activation Records

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

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

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

CSE 307: Principles of Programming Languages

CIT Week13 Lecture

Programming Languages Third Edition. Chapter 7 Basic Semantics

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

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

Procedure and Object- Oriented Abstraction

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Run-Time Data Structures

Compiler Construction

Introduction. Inline Expansion. CSc 553. Principles of Compilation. 29 : Optimization IV. Department of Computer Science University of Arizona

Lecture08: Scope and Lexical Address

CSc 520 Principles of Programming Languages

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

CS 314 Principles of Programming Languages

Scope, Functions, and Storage Management

Compiler Construction

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

CSc 372. Comparative Programming Languages. 15 : Haskell List Comprehension. Department of Computer Science University of Arizona

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

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

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

Compilers and Code Optimization EDOARDO FUSELLA

CSCI Compiler Design

Chapter 10. Implementing Subprograms

Statement Basics. Assignment Statements

Functions and Procedures

COP4020 Programming Assignment 1 - Spring 2011

UNIT 3

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

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

Short Notes of CS201

CS536 Spring 2011 FINAL ID: Page 2 of 11

CS201 - Introduction to Programming Glossary By

Wednesday, October 15, 14. Functions

Run-time Environments

COMP520 - GoLite Type Checking Specification

Run Time Environments

Run-time Environments

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

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

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

Code Generation. Lecture 19

CSc 372. Comparative Programming Languages. 36 : Scheme Conditional Expressions. Department of Computer Science University of Arizona

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

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

SWIFT - CLOSURES. Global Functions Nested Functions Closure Expressions. Have a name. Capture values from enclosing function

CSc 520. Iterators. CLU-style Iterators... CLU-style Iterators. Principles of Programming Languages. Christian Collberg April 16, 2003

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

Compiler Design. Spring Run-Time Environments. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Transcription:

CSc 520 Principles of Programming Languages 27 : Control Structures Procedures Christian Collberg Department of Computer Science University of Arizona collberg+520@gmail.com Copyright c 2008 Christian Collberg March 31, 2008 1 Procedures as Control Abstractions A procedure is a collection of computation (expressions, statements, etc).that we can give a name. A call-site is a location where a caller invokes a procedure, the callee. The caller waits for the callee to finish executing, at which time controls to the point after the call-site. Most procedures are parameterized. The values passed to the procedure are called actual parameters. The actual parameters are mapped to formal parameters, which hold the actual values within the procedure. 2 Procedures as Control Abstractions... Some procedures (called functions) return a value. In some languages, a function can return multiple values. Most languages use a call-stack on which actual parameters and local variables are stored. Different languages have different rules as to how parameters should be passed to a procedure. 3 Questions How do we deal with recursion? Every new recursive call should get its own set of local variables. How do we pass parameters to a procedure? Call-by-Value or Call-by-Reference? In registers or on the stack? How do we allocate/access local and global variables? How do we access non-local variables? (A variable is non-local in a procedure P if it is declared in procedure that statically encloses P.) 1

How do we pass large structured parameters (arrays and records)? Case Study Pascal 4 Pascal Procedures PROCEDURE Name (list of formals); CONST (* Constant declarations *) TYPE (* Type declarations *) VAR (* Variable declarations *) (* Procedure and function definitions *) BEGIN (* procedure body *) END; Note the similarity with the program structure. Note that procedures can be nested. Note the semicolon after the end. 5 Pascal Procedures... Formal parameters look like this: procedure name (formal1:type1; formal2:type2;...); or like this procedure name (formal1,formal2...:type1;...); By default, arguments are passed by value. var indicates that they are passed by reference: procedure name (var formal1:type1;...); 6 Pascal Procedures... Functions are similar to procedures but return values: To return a value assign it to the function name. function func1 (formals); func1 := 99; 2

7 Pascal Procedures... Procedures can be nested: procedure A (); procedure B();...... Names declared in an outer procedure are visible to nested procedures unless the name is redeclared. 8 Pascal Procedures... Procedures can be recursive. The forward declaration is used to handle mutually recursive procedures: procedure foo (); forward; procedure bar (); foo(); procedure foo(); bar(); 3

Case Study Ada 9 Ada Subprogram Declarations procedure Traverse_Tree; procedure Increment(X : in out Integer); procedure Right_Indent(Margin : out Line_Size); procedure Switch(From, To : in out Link); function Random return Probability; function Min_Cell(X : Link) return Cell; function Next_Frame(K : Positive) return Frame; function Dot_Product(Left, Right : Vector) return Real; 10 Ada Subprogram Declarations function "*"(Left, Right : Matrix) return Matrix; Examples of in parameters with default expressions: procedure Print_Header(Pages : in Natural; Header : in Line := (1.. Line Last => ); Center : in Boolean := True); 11 Ada Subprogram Bodies -- Example of procedure body: procedure Push(E : in Element_Type; S : in out Stack) is if S.Index = S.Size then raise Stack_Overflow; else S.Index := S.Index + 1; S.Space(S.Index) := E; end if; end Push; 12 Ada Procedure Call Traverse_Tree; Print_Header(128, Title, True); 4

Switch(From => X, To => Next); Print_Header(128, Header => Title, Center => True); Print_Header(Header=>Title, Center=>True, Pages=>128); --Examples of function calls: Dot_Product(U, V) Clock 13 Ada Procedure Call -- Procedures with default expressions: procedure Activate( Process : in Process_Name; After : in Process_Name:=No_Process; Wait : in Duration := 0.0; Prior : in Boolean := False); procedure Pair(Left, Right : in Person_Name:=new Person); 14 Ada Procedure Call... -- Examples of their calls: Activate(X); Activate(X, After => Y); Activate(X, Wait => 60.0, Prior => True); Activate(X, Y, 10.0, False); Pair; Pair(Left => new Person, Right => new Person); 15 Ada Overloaded Calls procedure Put(X : in Integer); procedure Put(X : in String); procedure Set(Tint : in Color); procedure Set(Signal : in Light); -- Examples of their calls: Put(28); Put("no possible ambiguity here"); Set(Tint=>Red); -- Set(Red) is ambiguous. Set(Signal=>Red); -- Red can denote either Set(Color (Red)); -- a Color or a Light 5

16 Ada Userdefined Operators function "+" (Left,Right:Matrix) return Matrix; function "+" (Left,Right:Vector) return Vector; -- assuming that A, B, and C are of -- the type Vector the following two -- statements are equivalent: A := B + C; A := "+"(B, C); 17 Readings and References Read Scott, pp. 117 123, 428 433 18 Summary Each procedure call pushes a new activation record on the run-time stack. The AR contains local variables, actual parameters, a static (access) link, a dynamic (control) link, the return address, saved registers, etc. The frame pointer (FP) (which is usually kept in a register) points to a fixed place in the topmost activation record. Each local variable and actual parameter is at a fixed offset from FP. 19 Summary... The dynamic link is used to restore the FP when a procedure call returns. The static link is used to access non-local variables, i.e. local variables which are declared within a procedure which statically encloses the current one. 6