# CSc 520 Principles of Programming Languages

Size: px
Start display at page:

Transcription

1 CSc 520 Principles of Programming Languages 28 : Control Structures Parameters Christian Collberg Department of Computer Science University of Arizona Copyright c 2008 Christian Collberg March 31, Parameter Passing Value Parameters 1mm PROG M; PROC P(X:INT); X:=5 VAR S:INT; S:=6; P(S);. X=5 S=6 } } P(6) M Value parameters are (usually) copied by the caller into the callee s activation record. Changes to a formal won t affect the actual. 2 Parameter Passing Reference Parameters 1mm PROG M; PROC P(VAR X:INT); X:=5 VAR S:INT; S:=6; P(S);. X=5 S=5 } } P(6) M Reference parameters are passed by passing the address (location, l-value) fo the parameter. Changes to a formal affects the actual also. 1

2 3 Call-by-Value Parameters 1. The caller computes the arguments r-value. 2. The caller places the r-values in the callee s activation record. The caller s actuals are never affected by the call. Copies may have to be made of large structures. TYPE T = ARRAY OF CHAR; PROC P (a:integer; b:t); a:=10; b[5]:="4" VAR r : INTEGER; X : T; P(r, X) 4 Call-by-Reference Parameters 1. The caller computes the arguments l-value. 2. Expression actuals (like a + b) are stored in a new location. 3. The caller places the l-values in the callee s activation record. The caller s actuals may be affected by the call. TYPE T = ARRAY OF CHAR; PROC P (VAR a:int; VAR b:t); a:=10; b[5]:="4" VAR r : INTEGER; X : T; P(5 + r, X) 5 Call-by-Name Parameters (Un-)popularized by Algol 60. A name parameter is (re-)evaluated every time it is referenced, in the callers environment. Algorithm: 1. The caller passes a thunk, a function which computes the argument s l-value and/or r-value, to the callee. 2. The caller also passes a static link to its environment. 3. Every time the callee references the name parameter, the thunk is called to evaluate it. The static link is passed to the thunk. 2

3 6 Call-by-Name Parameters... Algorithm: 4. If the parameter is used as an l-value, the thunk should return an l-value, otherwise an r-value. 5. If the parameter is used as an l-value, but the actual parameter has no l-value (it s a constant), the thunk should produce an error. Consequences: Every time a callee references a name parameter, it may produce a different result. 7 Call-by-Name Parameters... VAR i : INTEGER; VAR a : ARRAY 2 OF INTEGER; PROCEDURE P (NAME x:integer); i := i + 1; x := x + 1; ; i := 1; a[1] := 1; a[2] := 2; P(a[i]); WRITE a[1], a[2]; x := x + 1 becomes a[i] := a[i] + 1. Since i is incremented before x, we get a[2] := a[2] + 1. Print 1,3. 8 Call-by-Name Implementation PROCEDURE P (thunk : PROC()); i := i + 1; thunk() := thunk() + 1; ; PROCEDURE thunk1 () : ADDRESS; RETURN ADDR(a[i]) ; i := 1; a[1] := 1; a[2] := 2; P(thunk1); WRITE a[1], a[2]; 9 Call-by-Name Jensen s Device PROC Sum (NAME Expr:REAL; NAME Idx:INTEGER; Max:INTEGER):INTEGER; 3

4 VAR Result : REAL := 0; FOR k := 1 TO Max DO; Idx := k; Result := Result + Expr; FOR; RETURN Result; ; VAR i : INTEGER; WRITE Sum(i, i, 5); (* 5 i=1 i *) WRITE Sum(i*i, i, 10); (* 10 i=1 i2 *) 10 Large Value Parameters Large value parameters have to be treated specially, so that a change to the formal won t affect the actual. Example: TYPE T = ARRAY [ ] OF CHAR; PROCEDURE P (x : T); x[5] := "f"; VAR L : T; P(L);. 11 Large Value Parameters... Algorithm 1: Callee Copy PROCEDURE P (VAR x : T); VAR xt : T; copy(xt,x,1000); xt[5]:="f"; VAR L : T; P(L); Algorithm 2: Caller Copy PROCEDURE P (VAR x : T); x[5] := "f"; VAR L : T; VAR LT : T; copy(lt, L, 1000); P(LT); 12 Parameter Passing In Pascal, parameters are passed either by value or by reference (if the formal is preceeded by the keyword var). In C, all parameters are passed by value. Pass by reference can be simulated by explicitly passing the address of a variable: swap(&x,&y). 4

5 In FORTRAN, all parameters are passed by reference. A programmer can simulate pass-by-value by explicitly making a local copy of an argument. Unlike most languages, FORTRAN allows r-values to be passed by reference: swap(3+4,7*x). The compiler creates a temporary variable to hold the value. 13 Parameter Passing... In Java, object references are transfered using pass-by-sharing. This means that the actual and formal will refer to the same object. The compiler simply passes the address of the object. In Java, primitive types are passed by value. 14 Parameter Passing... In Pascal and Modula-2 a programmer would use call-by-value to ensure that the callee cannot modify the actual argument. In Pascal and Modula-2 a programmer would use call-by-reference to ensure that the callee can modify the actual argument, or to make sure that a large parameter (which semantically should be passed by value) is not copied. (This is done for efficiency reasons). 15 Parameter Passing... Modula-3 provides a READONLY parameter mode. A READONLY formal parameter cannot be changed by the callee. The formal 1. cannot be on the left-hand-side of an assignment statement, and 2. cannot be passed by reference to another routine. Small READONLY parameters are passed by value. Large READONLY parameters are passed by reference. 16 Parameter Passing in Ada Ada has three modes: 1. in-parameters pass information from the caller to the callee. The callee cannot write to them. 2. out-parameters pass information to the callee from the caller. The callee can read and write them. They start out being uninitialized. 3. in out-parameters pass information from the caller to the callee and back. 5

6 17 Parameter Passing in Ada... For scalars and pointers, all modes should be implemented by copying values. Thus 1. in-parameters are passed-by-value. 2. out-parameters are passed-by-result (the formal is copied into the actual when the procedure returns). 3. in out-parameters are passed-by-value/result (On entry, the actual is copied into the formal. On return, the formal is copied back into the actual). 18 Parameter Passing in Ada... For constructed types (records, arrays) an implementation is allowed to pass either values or addresses. If an in out parameter is passed by address an assignment to the formal changes the actual immediately. If an in out parameter is passed by value an assignment to the formal will not affect the actual until the procedure returns (and the formal is copied back into the actual). Ada disallows programs that can tell which implementation a compiler uses. 19 Parameter Passing in Ada... type t is record a, b : integer; end record; r : t; procedure foo (s : in out t) is begin r.a := r.a + 1; s.a := s.a + 1; end foo; r.a := 3; foo(r); if r.a = 4 then put("implementation uses pass-by-value") else put("implementation uses pass-by-address") 20 Exam Problem /96 (A) Show the status of the run-time stack when execution has reached point for the second time in the program on the next slide. Fill in the name of each procedure invocation in the correct activation record. Also fill in the values of local variables and actual parameters, and show where the static links and control links are pointing. Assume that all actual parameters are passed on the stack rather than in registers. 6

7 21 Exam Problem /96 (B) PROGRAM M; PROC P ( X:INT); VAR W:INT; PROC Q ( VAR Z:INT); VAR N:INT; PROC R ( V:INT); VAR L:INT; L := W; P(23); R; N := W; Z := Z+1; R(Z); Q; W := X + 1; Q(W); P(15); M. Activ ation Record Stack grows up! This is the activation Name: M record for the main program. 22 Homework Draw the stack when control reaches point for the third time. Include all actual parameters, local variables, return addresses, and static and dynamic links. PROGRAM M; PROCEDURE P(X:INTEGER); VAR A : INTEGER; PROCEDURE Q(Y : INTEGER); VAR B : INTEGER; B := Y + 1; A := B + 2; P(B); Q; A:= X + 1; Q(A); P(0); M. 23 Readings and References Read Scott, pp Summary A parameter is often passed by the caller copying it (or its address, in case of VAR parameters) into the callees activation record. On the MIPS, the caller has an area in its own activation record in which it puts actual parameters before it jumps to the callee. For each procedure P the compiler figures out 7

8 the maximum number of arguments P passes to any procedure it calls. The corresponding amount of memory has to be allocated in P s activation record. 8

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

Parameter Passing Value Parameters CSc 520 Principles of Programming Languages 28 : Control Structures Parameters Christian Collberg collberg+520@gmail.com PROG M; PROC P(X:INT); X:=5 VAR S:INT; S:=6;

### CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages 31: Procedures Parameters Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg

### Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions COMP 524 Programming Language Concepts Stephen Olivier February 12, 2008 Based on notes by A. Block, N. Fisher, F. Hernandez-Campos,

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

UNIT V SubPrograms Outline Subprograms Parameter Passing Parameter correspondence Main Issues when designing subroutine in programming languages Parameter passing techniques Characteristics of Subprogram

### Memory Management and Run-Time Systems

TDDD55 Compilers and Interpreters TDDB44 Compiler Construction Memory Management and Run-Time Systems Part of the Attribute Grammar Material Presented at the Beginning of this Lecture Peter Fritzson IDA,

### G Programming Languages - Fall 2012

G22.2110-003 Programming Languages - Fall 2012 Lecture 4 Thomas Wies New York University Review Last week Control Structures Selection Loops Adding Invariants Outline Subprograms Calling Sequences Parameter

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

Procedures as Control Abstractions CSc 520 Principles of Programming Languages 30: Procedures Introduction Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona

### COP4020 Programming Languages. Subroutines and Parameter Passing Prof. Robert van Engelen

COP4020 Programming Languages Subroutines and Parameter Passing Prof. Robert van Engelen Overview Parameter passing modes Subroutine closures as parameters Special-purpose parameters Function returns COP4020

### Programming Languages

Programming Languages Tevfik Koşar Lecture - XX April 4 th, 2006 1 Roadmap Subroutines Allocation Strategies Calling Sequences Parameter Passing Generic Subroutines Exception Handling Co-routines 2 1 Review

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

G22.2110-001 Programming Languages Spring 2010 Lecture 4 Robert Grimm, New York University 1 Review Last week Control Structures Selection Loops 2 Outline Subprograms Calling Sequences Parameter Passing

### Principles of Programming Languages

Ting Zhang Iowa State University Computer Science Department Lecture Note 16 October 26, 2010 Control Abstraction: Subroutines 1 / 26 Outline 1 Subroutines 2 Parameter Passing 3 Generic Subroutines 2 /

### Chap. 8 :: Subroutines and Control Abstraction

Chap. 8 :: Subroutines and Control Abstraction Michael L. Scott Programming Language Theory 2015, kkman@sangji.ac.kr 1 Review Of Stack Layout Allocation strategies Static Code Globals Own variables Explicit

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

NAME s GRADE Prob 1 2 3 4 5 I II III Σ Max 12 12 12 12 12 26 26 26 100(+... ) Score CSc 520 exam Wednesday 13 December 2000 TIME = 2 hours Write all answers ON THIS EXAMINATION, and submit it IN THE ENVELOPE

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

Control Abstraction Chapter 8 (81 84) Control Abstraction: Subroutines and parameters Programmer defined control structures Subprograms Procedures Functions Coroutines Exception handlers Processes Subprograms

### Chapter 9 :: Subroutines and Control Abstraction

Chapter 9 :: Subroutines and Control Abstraction Programming Language Pragmatics, Fourth Edition Michael L. Scott Copyright 2016 Elsevier 1 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November

### CS 345. Functions. Vitaly Shmatikov. slide 1

CS 345 Functions Vitaly Shmatikov slide 1 Reading Assignment Mitchell, Chapter 7 C Reference Manual, Chapters 4 and 9 slide 2 Procedural Abstraction Can be overloaded (e.g., binary +) Procedure is a named

### CSc 520 Principles of Programming Languages

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

### Implementing Subprograms

1 Implementing Subprograms CS 315 Programming Languages Pinar Duygulu Bilkent University CS315 Programming Languages Pinar Duygulu The General Semantics of Calls and Returns 2 The subprogram call and return

### Programming Languages: Lecture 12

1 Programming Languages: Lecture 12 Chapter 10: Implementing Subprograms Jinwoo Kim jwkim@jjay.cuny.edu Chapter 10 Topics 2 The General Semantics of Calls and Returns Implementing Simple Subprograms Implementing

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

CSc 520 Principles of Programming Languages 26 : Control Structures Introduction Christian Collberg Department of Computer Science University of Arizona collberg+520@gmail.com Copyright c 2008 Christian

### Object-Oriented Languages. CSc 453. Compilers and Systems Software. 23 : OO Languages. Department of Computer Science University of Arizona

Object-Oriented Languages CSc 453 Compilers and Systems Software 23 : OO Languages Department of Computer Science University of Arizona Object-oriented languages extend imperative languages with: 1 A classification

### Chapter 10. Implementing Subprograms ISBN

Chapter 10 Implementing Subprograms ISBN 0-321-33025-0 Chapter 10 Topics The General Semantics of Calls and Returns Implementing Simple Subprograms Implementing Subprograms with Stack-Dynamic Local Variables

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

Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott Administrative Notes Final Test Thursday, August 3 2006 at 11:30am No lecture before or after the mid-term

### CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages 32: Procedures Inlining Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg [1]

### CSc 453. Semantic Analysis III. Basic Types... Basic Types. Structured Types: Arrays. Compilers and Systems Software. Christian Collberg

Slide 11 2 Basic s... Enumeration types TYPE E1 = (white,blue,yellow,green,red); TYPE E2 = (apple=4,pear=9,kumquat=99); Pascal, Ada, Modula-2, C have some variant of enumeration types. Subrange types TYPE

### Control Abstraction. Hwansoo Han

Control Abstraction Hwansoo Han Review of Static Allocation Static allocation strategies Code Global variables Own variables (live within an encapsulation - static in C) Explicit constants (including strings,

### Run-time Environments -Part 1

Run-time Environments -Part 1 Y.N. Srikant Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Compiler Design Outline of the Lecture Part 1 What is run-time support?

### CSc 453. OO Languages. Object-Oriented Languages... Object-Oriented Languages. Compiling OO Languages. Compilers and Systems Software

Slide 22 2 Object-Oriented Languages... 3. Polymorphism, which is the ability of a variable to store values of different types. OO languages support a special kind of polymorphism, called inclusion polymorphism,

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

1/26 CSc 372 Comparative Programming Languages 36 : Scheme Conditional Expressions Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg 2/26 Comparison

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

CSC 24: Computer Systems Using the Stack for Function Calls Lecture Goals Challenges of supporting functions! Providing information for the called function Function arguments and local variables! Allowing

### CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages 36 : Scheme Conditional Expressions Christian Collberg Department of Computer Science University of Arizona collberg+520@gmail.com Copyright c 2008 Christian

### 1 Lexical Considerations

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

### System Software Assignment 1 Runtime Support for Procedures

System Software Assignment 1 Runtime Support for Procedures Exercise 1: Nested procedures Some programming languages like Oberon and Pascal support nested procedures. 1. Find a run-time structure for such

### CS 314 Principles of Programming Languages. Lecture 13

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:

### CSc 520. Principles of Programming Languages 45: OO Languages Introduction

CSc 520 Principles of Programming Languages 45: OO Languages Introduction Christian Collberg Department of Computer Science University of Arizona collberg@cs.arizona.edu Copyright c 2005 Christian Collberg

### Lexical Considerations

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2010 Handout Decaf Language Tuesday, Feb 2 The project for the course is to write a compiler

### CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages 32 : Control Structures Coroutines Christian Collberg Department of Computer Science University of Arizona collberg+520@gmail.com Copyright c 2008 Christian

### Parameter passing. Parameter: A variable in a procedure that represents some other data from the procedure that invoked the given procedure.

Parameter passing Parameter: A variable in a procedure that represents some other data from the procedure that invoked the given procedure. Parameter transmission: How that information is passed to the

### Wednesday, October 15, 14. Functions

Functions Terms void foo() { int a, b;... bar(a, b); void bar(int x, int y) {... foo is the caller bar is the callee a, b are the actual parameters to bar x, y are the formal parameters of bar Shorthand:

### Memory Management. Memory Management... Memory Management... Interface to Dynamic allocation

CSc 453 Compilers and Systems Software 24 : Garbage Collection Introduction Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2009 Christian Collberg Dynamic Memory Management

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

Expression evaluation CSE 504 Order of evaluation For the abstract syntax tree + + 5 Expression Evaluation, Runtime Environments + + x 3 2 4 the equivalent expression is (x + 3) + (2 + 4) + 5 1 2 (. Contd

### Concepts Introduced in Chapter 7

Concepts Introduced in Chapter 7 Storage Allocation Strategies Static Stack Heap Activation Records Access to Nonlocal Names Access links followed by Fig. 7.1 EECS 665 Compiler Construction 1 Activation

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

Programming Languages Third Edition Chapter 10 Control II Procedures and Environments Objectives Understand the nature of procedure definition and activation Understand procedure semantics Learn parameter-passing

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

CSc 553 Principles of Compilation 29 : Optimization IV Introduction Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2011 Christian Collberg Inline Expansion I Inline

### CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 13: Names, Scopes and Bindings Zheng (Eddy) Zhang Rutgers University February 28, 2018 Review: Names, Scopes and Binding What s in a name? Each name means

### procedure definition (or declaration - some make a distinction, we generally won t), e.g., void foo(int x) {... }

Parameters Terminology: procedure invocation, e.g., (in different language notations) foo(a*8) x:=goo(19,a) call hoo() actual parameters, e.g., a*8 is an actual parameter of the above inv ocation of foo

### Parameter Binding. Value: The formal parameter represents a local variable initialized to the value of the corresponding actual parameter.

Parameter Binding Almost all programming languages have some notion of binding an actual parameter (provided at the point of call) to a formal parameter (used in the body of a subprogram). There are many

### CSC 8400: Computer Systems. Using the Stack for Function Calls

CSC 84: Computer Systems Using the Stack for Function Calls Lecture Goals Challenges of supporting functions! Providing information for the called function Function arguments and local variables! Allowing

### Informatica 3 Syntax and Semantics

Informatica 3 Syntax and Semantics Marcello Restelli 9/15/07 Laurea in Ingegneria Informatica Politecnico di Milano Introduction Introduction to the concepts of syntax and semantics Binding Variables Routines

### CS558 Programming Languages

CS558 Programming Languages Fall 2016 Lecture 4a Andrew Tolmach Portland State University 1994-2016 Pragmatics of Large Values Real machines are very efficient at handling word-size chunks of data (e.g.

### Compiler Construction

Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-17/cc/ Generation of Intermediate Code Conceptual Structure of

### References and pointers

References and pointers Pointers are variables whose value is a reference, i.e. an address of a store location. Early languages (Fortran, COBOL, Algol 60) had no pointers; added to Fortran 90. In Pascal,

### CSc 453. Compilers and Systems Software. 13 : Intermediate Code I. Department of Computer Science University of Arizona

CSc 453 Compilers and Systems Software 13 : Intermediate Code I Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2009 Christian Collberg Introduction Compiler Phases

### Lexical Considerations

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a

### Compiler Construction

Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-17/cc/ Generation of Intermediate Code Outline of Lecture 15 Generation

### Weeks 6&7: Procedures and Parameter Passing

CS320 Principles of Programming Languages Weeks 6&7: Procedures and Parameter Passing Jingke Li Portland State University Fall 2017 PSU CS320 Fall 17 Weeks 6&7: Procedures and Parameter Passing 1 / 45

### MIPS Procedure Calls. Lecture 6 CS301

MIPS Procedure Calls Lecture 6 CS301 Function Call Steps Place parameters in accessible location Transfer control to function Acquire storage for procedure variables Perform calculations in function Place

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

CS558 Programming Languages Winter 2018 Lecture 4a Andrew Tolmach Portland State University 1994-2018 Pragmatics of Large Values Real machines are very efficient at handling word-size chunks of data (e.g.

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

Procedure Abstraction Run Time Environment Records Procedure Linkage Name Translation and Variable Access Copyright 2010, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at

### Control Structures. Boolean Expressions. CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV

CSc 453 Compilers and Systems Software 16 : Intermediate Code IV Control Structures Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2009 Christian Collberg Control Structures

### Topic IV. Block-structured procedural languages Algol and Pascal. References:

References: Topic IV Block-structured procedural languages Algol and Pascal Chapters 5 and 7, of Concepts in programming languages by J. C. Mitchell. CUP, 2003. Chapters 10( 2) and 11( 1) of Programming

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

Subroutines and Control Abstraction Textbook, Chapter 8 1 Subroutines and Control Abstraction Mechanisms for process abstraction Single entry (except FORTRAN, PL/I) Caller is suspended Control returns

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

CS356: Discussion #6 Assembly Procedures and Arrays Marco Paolieri (paolieri@usc.edu) Procedures Functions are a key abstraction in software They break down a problem into subproblems. Reusable functionality:

### Chapter 8. Fundamental Characteristics of Subprograms. 1. A subprogram has a single entry point

Fundamental Characteristics of Subprograms 1. A subprogram has a single entry point 2. The caller is suspended during execution of the called subprogram 3. Control always returns to the caller when the

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

Run Time Environment Activation Records Procedure Linkage Name Translation and Variable Access Copyright 2015, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University

Fall 2018 EE 3613: Computer Organization Chapter 2: Instruction Set Architecture Introduction 4/4 Avinash Karanth Department of Electrical Engineering & Computer Science Ohio University, Athens, Ohio 45701

### CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 15: Review and Functional Programming Zheng (Eddy) Zhang Rutgers University March 19, 2018 Class Information Midterm exam forum open in Sakai. HW4 and

### COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

COMP 303 Computer Architecture Lecture 3 Comp 303 Computer Architecture 1 Supporting procedures in computer hardware The execution of a procedure Place parameters in a place where the procedure can access

### Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

References: Topic IV Block-structured procedural languages Algol and Pascal Chapters 5 and 7, of Concepts in programming languages by J. C. Mitchell. CUP, 2003. Chapter 5 of Programming languages: Concepts

### CSc 453 Compilers and Systems Software

CSc 453 Compilers and Systems Software 3 : Lexical Analysis I Christian Collberg Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2009 Christian Collberg August 23, 2009

### Systems I. Machine-Level Programming V: Procedures

Systems I Machine-Level Programming V: Procedures Topics abstraction and implementation IA32 stack discipline Procedural Memory Usage void swap(int *xp, int *yp) int t0 = *xp; int t1 = *yp; *xp = t1; *yp

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

Runtime Environment Relationship between names and data objects (of target machine) Allocation & de-allocation is managed by run time support package Each execution of a procedure is an activation of the

### Run-Time Environments

1 Run-Time Environments Chapter 7 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2011 2 Procedure Activation and Lifetime A procedure is activated when called

### 9. Subprograms. 9.2 Fundamentals of Subprograms

9. Subprograms 9.2 Fundamentals of Subprograms General characteristics of subprograms A subprogram has a single entry point The caller is suspended during execution of the called subprogram Control always

### CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV. Department of Computer Science University of Arizona

CSc 453 Compilers and Systems Software 16 : Intermediate Code IV Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2009 Christian Collberg Control Structures Control Structures

### Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University See P&H 2.8 and 2.12 Goals for Today Calling Convention for Procedure Calls Enable code to be reused by allowing

### PROCEDURES, METHODS AND FUNCTIONS

Cosc 2P90 PROCEDURES, METHODS AND FUNCTIONS (c) S. Thompson, M. Winters 1 Procedures, Functions & Methods The are varied and complex rules for declaring, defining and calling procedures, methods and functions

### ECE232: Hardware Organization and Design

ECE232: Hardware Organization and Design Lecture 6: Procedures Adapted from Computer Organization and Design, Patterson & Hennessy, UCB Overview Procedures have different names in different languages Java:

### Run-time Environments

Run-time Environments Status We have so far covered the front-end phases Lexical analysis Parsing Semantic analysis Next come the back-end phases Code generation Optimization Register allocation Instruction

### CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages 16 : Types Polymorphism Christian Collberg collberg+520@gmail.com Department of Computer Science University of Arizona Copyright c 2008 Christian Collberg [1]

### Run-time Environments

Run-time Environments Status We have so far covered the front-end phases Lexical analysis Parsing Semantic analysis Next come the back-end phases Code generation Optimization Register allocation Instruction

### 11/29/17. Outline. Subprograms. Subroutine. Subroutine. Parameters. Characteristics of Subroutines/ Subprograms

Outline Subprograms In Text: Chapter 9 Definitions Design issues for subroutines Parameter passing modes and mechanisms Advanced subroutine issues N. Meng, S. Arthur 2 Subroutine A sequence of program

### COP4020 Fall 2006 Final Exam

COP4020 Fall 2006 Final Exam Name: (Please print) Put the answers on these sheets. You can collect 100 points in total for this exam. 1. Consider the following Ada program fragment: search: loop i := i+1;

### Goals of this Lecture

C Pointers Goals of this Lecture Help you learn about: Pointers and application Pointer variables Operators & relation to arrays 2 Pointer Variables The first step in understanding pointers is visualizing

### Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

1 Subprograms CS 315 Programming Languages Pinar Duygulu Bilkent University Introduction 2 Two fundamental abstraction facilities Process abstraction Emphasized from early days Data abstraction Emphasized

### Lecture08: Scope and Lexical Address

Lecture08: Scope and Lexical Address Free and Bound Variables (EOPL 1.3.1) Given an expression E, does a particular variable reference x appear free or bound in that expression? Definition: A variable

### Special Topics: Programming Languages

Lecture #17 0 V22.0490.001 Special Topics: Programming Languages B. Mishra New York University. Lecture # 17 Lecture #17 1 Slide 1 Runtime Representations Variable Names Environment L-values Scope, Extent

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

Attributes of Variable Lecture 13 Run-Time Storage Management CSC 131 Kim Bruce Scope Lifetime Location Value Done! Lifetime Value & Location FORTRAN - all allocated statically - Stack-based (C/C++/Java/Pascal/...)

### CIT Week13 Lecture

CIT 3136 - Week13 Lecture Runtime Environments During execution, allocation must be maintained by the generated code that is compatible with the scope and lifetime rules of the language. Typically there

### CS 415 Midterm Exam Spring SOLUTION

CS 415 Midterm Exam Spring 2005 - SOLUTION Name Email Address Student ID # Pledge: This exam is closed note, closed book. Questions will be graded on quality of answer. Please supply the best answer you

### Programming Languages: Lecture 11

1 Programming Languages: Lecture 11 Chapter 9: Subprograms Jinwoo Kim jwkim@jjay.cuny.edu Chapter 9 Topics 2 Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments

### 22c:111 Programming Language Concepts. Fall Functions

22c:111 Programming Language Concepts Fall 2008 Functions Copyright 2007-08, The McGraw-Hill Company and Cesare Tinelli. These notes were originally developed by Allen Tucker, Robert Noonan and modified

### Functions and Procedures

Functions and Procedures Function or Procedure A separate piece of code Possibly separately compiled Located at some address in the memory used for code, away from main and other functions (main is itself

### NOTE: Answer ANY FOUR of the following 6 sections:

SE352b Software Engineering Design Tools W3: Programming Paradigms Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa SE352b: Roadmap CASE Tools: Introduction System Programming Tools Programming Paradigms

### Lab 4 Prelab: MIPS Function Calls

Lab 4 Prelab: MIPS Function Calls Name: Sign the following statement: On my honor, as an Aggie, I have neither given nor received unauthorized aid on this academic work 1 Objective The main objective of

### Informatica 3 Marcello Restelli

Informatica 3 Marcello Restelli 9/15/07 Laurea in Ingegneria Informatica Politecnico di Milano Operational Semantics We describe the semantics of a programming language by means of an abstract semantic

### Lecture 5: Procedure Calls

Lecture 5: Procedure Calls Today s topics: Procedure calls and register saving conventions 1 Example Convert to assembly: while (save[i] == k) i += 1; i and k are in \$s3 and \$s5 and base of array save[]