INF 212 ANALYSIS OF PROG. LANGS PROCEDURES & FUNCTIONS. Instructors: Kaj Dreef Copyright Instructors.

Similar documents
CS 320: Concepts of Programming Languages

Scope, Functions, and Storage Management

22c:111 Programming Language Concepts. Fall Functions

Concepts Introduced in Chapter 7

Run-Time Data Structures

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

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

Run-time Environments

Run-time Environments

INF 212 ANALYSIS OF PROG. LANGS ELEMENTS OF IMPERATIVE PROGRAMMING STYLE. Instructors: Crista Lopes Copyright Instructors.

Programming Languages

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

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

CS558 Programming Languages

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

CS 230 Programming Languages

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

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

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

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

Compilation I. Hwansoo Han

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

The role of semantic analysis in a compiler

Subroutines and Functions

Subroutines. Subroutine. Subroutine design. Control abstraction. If a subroutine does not fit on the screen, it is too long

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

LECTURE 18. Control Flow

Chapter 5. Names, Bindings, and Scopes

Instruction Set Architectures (4)

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

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

CS 415 Midterm Exam Spring 2002

G Programming Languages - Fall 2012

Names, Bindings, Scopes

Names, Scopes, and Bindings II. Hwansoo Han

Concepts of Programming Languages

G Programming Languages - Fall 2012

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

Topic 7: Activation Records

Programming Languages, Summary CSC419; Odelia Schwartz

We can emit stack-machine-style code for expressions via recursion

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

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

Programmin Languages/Variables and Storage

Functional Programming. Big Picture. Design of Programming Languages

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

Principles of Programming Languages

CS 314 Principles of Programming Languages

22c:111 Programming Language Concepts. Fall Types I

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

Wednesday, October 15, 14. Functions

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

known as non-void functions/methods in C/C++/Java called from within an expression.

Chapter 9 Subprograms

TYPES, VALUES AND DECLARATIONS

Chapter 1. Preliminaries

CIT Week13 Lecture

Com S 541. Programming Languages I

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

G Programming Languages - Fall 2012

CS558 Programming Languages. Winter 2013 Lecture 4

Surprising behaviour of Fortran (90/95)

Programming Languages Third Edition. Chapter 7 Basic Semantics

Chapter 9 :: Subroutines and Control Abstraction

Type Checking Binary Operators

Programming Languages: Lecture 11

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Scope and Parameter Passing 1 / 19

Introduction to Fortran Programming. -Internal subprograms (1)-

Threads CS1372. Lecture 13. CS1372 Threads Fall / 10

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

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

Chapter 5 Names, Binding, Type Checking and Scopes

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

Organization of Programming Languages CS 3200/5200N. Lecture 09

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

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

Compiler Construction

Chapter 1. Preliminaries

Early computers (1940s) cost millions of dollars and were programmed in machine language. less error-prone method needed

Monday Feb 16, 5pm. solve these equalities. Midterm Thursday Feb 12. covers only ocaml part of the course, assignments 1 through 3.

LECTURE 14. Names, Scopes, and Bindings: Scopes

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

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

Review Functions Subroutines Flow Control Summary

Procedure Call Instructions

Compiler Construction

Python: Functions. Thomas Schwarz, SJ Marquette University

CSE 307: Principles of Programming Languages

Types II. Hwansoo Han

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Transcription:

INF 212 ANALYSIS OF PROG. LANGS PROCEDURES & FUNCTIONS Instructors: Kaj Dreef Copyright Instructors.

Subroutines aka Procedures Historically: blocks of instructions executed several times during program execution May have 0 or more input arguments May have 0 or more output arguments May perform IO, side effects Mid-50s

Functions Take 0 or more input arguments Return one value Used as expressions Additional constraint for pure functions:! No IO, no side effects

Procedures vs. Functions Distinction existed as early as 1958 (FORTRAN) subroutine square_cube(i,isquare,icube) integer, intent(in) :: i! input integer, intent(out) :: isquare,icube! output isquare = i**2 icube = i**3 end subroutine square_cube program xx implicit none integer :: i,isq,icub i = 4 call square_cube(i,isq,icub) print*,"i,i^2,i^3=",i,isq,icub end program xx

Procedures vs. Functions Distinction existed as early as 1958 (FORTRAN) function func(i) result(j) integer, intent(in) :: i! input integer :: j! output j = i**2 + i**3 end function func program xfunc implicit none integer :: i integer :: func i = 3 print*,"sum of the square and cube of",i," is",func(i) end program xfunc Additionally, Fortran has a pure keyword for pure functions

Procedures vs. Functions Distinction was lost at some point, mainstream PLs merged the two concepts into one! C/C++, Java, Python, Perl, PHP,... No distinction: Procedures can also return values! Lisp, ML, Haskell,... Only functions, but: Functions can be pure or impure

Pure Functional Programming Mathematical functions! No side effects! No IO (other than at the beginning and the end) High-order functions! Functions can take functions as arguments! Functions can return functions as values More on this later...

Function/procedure calls Implementation details

Simplified Machine Model!9 Registers Code Data Stack Program counter Environment pointer Heap

Function definition def: fact(n) = if n<=1 then 1 else n * fact(n-1)... call: fact(3)...?

Activation Records for Functions!11 Block of information ( frame ) associated with each function call, including:! Parameters! Local variables! Return address! Location to put return value when function exits! to the caller s activation record! Saved registers! Temporary variables and intermediate results! (not always) Access link to the function s static parent

Activation Record Layout!12 Return address Parameters Local variables Intermediate results Environment pointer Return address! Location of code to execute on function return ess! Address in activation record of calling block to receive returned value Parameters! Locations to contain data from calling block

Example!13 Return address Return result addr Parameters Local variables Intermediate results Environment pointer Function fact(n) = if n<=1 then 1 else n * fact(n-1)! Return result address: location to put fact(n) Parameter! Set to value of n by calling sequence Intermediate result! Locations to contain value of fact(n-1)

Typical x86 Activation Record!14 frame pointer stack pointer

Run-Time Stack!15 Activation records are kept on the stack! Each new call pushes an activation record! Each completing call pops the topmost one! Stack has all records of all active calls at any moment during execution (topmost record = most recent call) Example: fact(3)! Pushes one activation record on the stack, calls fact(2)! This call pushes another record, calls fact(1)! This call pushes another record, resulting in three activation records on the stack

Function Call!16 fact(k) n fact(n-1) Environment pointer fact(n) = if n<= 1 then 1 else n * fact(n-1) Return address omitted; would be a pointer into code segment k fact(3) fact(2) fact(1) n 3 fact(n-1) n 2 fact(n-1) n 1 fact(n-1)

Function Return!17 fact(3) High addresses fact(2) fact(1) Low addresses n 3 fact(n-1) n 2 fact(n-1) 1 n 1 fact(n-1) fact(3) n 3 fact(n-1) 2 fact(2) n 2 fact(n-1) 1 fact(n) = if n<=1 then 1 else n * fact(n-1)