Principles of Programming Languages
|
|
- Dwayne Gallagher
- 5 years ago
- Views:
Transcription
1 Ting Zhang Iowa State University Computer Science Department Lecture Note 16 October 26, 2010 Control Abstraction: Subroutines 1 / 26
2 Outline 1 Subroutines 2 Parameter Passing 3 Generic Subroutines 2 / 26
3 Stack Layout Typical stack layout with subroutine nesting: Given the calling sequence A, E, B, D, C, in that order, frames will be allocated on the stack as shown at right, with the indicated static and dynamic links. 3 / 26
4 Stack Frame A typical stack frame: Arguments are accessed at positive offsets from the fp. Local variables and temporaries are accessed at negative offsets from the fp. Arguments to be passed to called routines are assembled at the top of the frame, using positive offsets from the sp. 4 / 26
5 Static Chain Maintainence In languages with nested subroutines, the maintenance of the static chain must be performed by the caller, rather than the callee The callee is nested (directly) inside the caller. The caller therefore passes its own frame pointer as the callee s static link. 5 / 26
6 Static Chain Maintainence In languages with nested subroutines, the maintenance of the static chain must be performed by the caller, rather than the callee The callee is nested (directly) inside the caller. The caller therefore passes its own frame pointer as the callee s static link. The callee is k 0 scopes outward -closer to the outer level of lexical nesting. The caller dereferences its own static link k times and passes the result as the callee s static link. 5 / 26
7 Typical Calling Sequence Caller s task before the call: saves any caller-saves registers 6 / 26
8 Caller s task before the call: Typical Calling Sequence saves any caller-saves registers computes the values of arguments and moves them into the stack or registers 6 / 26
9 Caller s task before the call: Typical Calling Sequence saves any caller-saves registers computes the values of arguments and moves them into the stack or registers computes the static link (if needed) and passes it as an extra, hidden argument 6 / 26
10 Caller s task before the call: Typical Calling Sequence saves any caller-saves registers computes the values of arguments and moves them into the stack or registers computes the static link (if needed) and passes it as an extra, hidden argument jump to the subroutine, simultaneously passing the return address on the stack or in a register 6 / 26
11 Caller s task before the call: Typical Calling Sequence saves any caller-saves registers computes the values of arguments and moves them into the stack or registers computes the static link (if needed) and passes it as an extra, hidden argument jump to the subroutine, simultaneously passing the return address on the stack or in a register 6 / 26
12 Caller s task before the call: Typical Calling Sequence saves any caller-saves registers computes the values of arguments and moves them into the stack or registers computes the static link (if needed) and passes it as an extra, hidden argument jump to the subroutine, simultaneously passing the return address on the stack or in a register Callee s task before the execution: allocates a frame by subtracting an appropriate constant from the sp 6 / 26
13 Caller s task before the call: Typical Calling Sequence saves any caller-saves registers computes the values of arguments and moves them into the stack or registers computes the static link (if needed) and passes it as an extra, hidden argument jump to the subroutine, simultaneously passing the return address on the stack or in a register Callee s task before the execution: allocates a frame by subtracting an appropriate constant from the sp saves the old frame pointer into the stack, and assigns it an appropriate new value 6 / 26
14 Caller s task before the call: Typical Calling Sequence saves any caller-saves registers computes the values of arguments and moves them into the stack or registers computes the static link (if needed) and passes it as an extra, hidden argument jump to the subroutine, simultaneously passing the return address on the stack or in a register Callee s task before the execution: allocates a frame by subtracting an appropriate constant from the sp saves the old frame pointer into the stack, and assigns it an appropriate new value saves any callee-saves registers that may be overwritten by the current routine 6 / 26
15 Typical Calling Sequence Callee s after the execution: moves the return value (if any) into a register or a reserved location in the stack 7 / 26
16 Typical Calling Sequence Callee s after the execution: moves the return value (if any) into a register or a reserved location in the stack restores callee-saves registers if needed 7 / 26
17 Typical Calling Sequence Callee s after the execution: moves the return value (if any) into a register or a reserved location in the stack restores callee-saves registers if needed restores the fp and the sp 7 / 26
18 Typical Calling Sequence Callee s after the execution: moves the return value (if any) into a register or a reserved location in the stack restores callee-saves registers if needed restores the fp and the sp jumps back to the return address 7 / 26
19 Typical Calling Sequence Callee s after the execution: moves the return value (if any) into a register or a reserved location in the stack restores callee-saves registers if needed restores the fp and the sp jumps back to the return address 7 / 26
20 Typical Calling Sequence Callee s after the execution: moves the return value (if any) into a register or a reserved location in the stack restores callee-saves registers if needed restores the fp and the sp jumps back to the return address Caller s task after the call: moves the return value to wherever it is needed 7 / 26
21 Typical Calling Sequence Callee s after the execution: moves the return value (if any) into a register or a reserved location in the stack restores callee-saves registers if needed restores the fp and the sp jumps back to the return address Caller s task after the call: moves the return value to wherever it is needed restores caller-saves registers if needed 7 / 26
22 Frame Allocation A stack-frame is created merely by moving the stack pointer up by d locations at the start of a subroutine 8 / 26
23 Frame Growth 9 / 26
24 Outline 1 Subroutines 2 Parameter Passing 3 Generic Subroutines 10 / 26
25 Parameter Passing Parameter passing modes: 11 / 26
26 Parameter Passing Parameter passing modes: In 11 / 26
27 Parameter Passing Parameter passing modes: In Out 11 / 26
28 Parameter Passing Parameter passing modes: In Out In/Out 11 / 26
29 Parameter Passing Parameter passing modes: In Out In/Out Parameter passing mechanisms: 11 / 26
30 Parameter Passing Parameter passing modes: In Out In/Out Parameter passing mechanisms: Call by value (in) 11 / 26
31 Parameter Passing Parameter passing modes: In Out In/Out Parameter passing mechanisms: Call by value (in) Call by reference (in+out) 11 / 26
32 Parameter Passing Parameter passing modes: In Out In/Out Parameter passing mechanisms: Call by value (in) Call by reference (in+out) Call by result (out) 11 / 26
33 Parameter Passing Parameter passing modes: In Out In/Out Parameter passing mechanisms: Call by value (in) Call by reference (in+out) Call by result (out) Call by value/result (in+out) 11 / 26
34 Parameter Passing Parameter passing modes: In Out In/Out Parameter passing mechanisms: Call by value (in) Call by reference (in+out) Call by result (out) Call by value/result (in+out) Call by name (in+out) 11 / 26
35 Parameter Passing Parameter passing modes: In Out In/Out Parameter passing mechanisms: Call by value (in) Call by reference (in+out) Call by result (out) Call by value/result (in+out) Call by name (in+out) Call by need (in+out) 11 / 26
36 Call by Value Representative language: C 12 / 26
37 Call by Value Representative language: C Actual parameters are evaluated and their values are assigned to the formal parameters 12 / 26
38 Call by Value Representative language: C Actual parameters are evaluated and their values are assigned to the formal parameters Actual parameters are not affected by the change to the formal parameters 12 / 26
39 Call by Value Representative language: C Actual parameters are evaluated and their values are assigned to the formal parameters Actual parameters are not affected by the change to the formal parameters Data can only be modified by passing pointers to the data swap(int *a, int *b) { int t = *a; *a = *b; *b = t; } 12 / 26
40 Call by Reference Representative language: Fortran SUBROUTINE SWAP(A,B) INTEGER A, B, C C=A A=B B=C END 13 / 26
41 Call by Reference Representative language: Fortran SUBROUTINE SWAP(A,B) INTEGER A, B, C C=A A=B B=C END If an R-value appears as an argument, a temporary variable is created to hold the value, and this variable is passed by reference SWAP(A,2) is equivalent to A = 2 13 / 26
42 Call by Value/Reference Provides call-by-value as well as call-by-reference Representative language: Pascal, C++ 14 / 26
43 Call by Value/Reference Provides call-by-value as well as call-by-reference Representative language: Pascal, C++ Call by value is similar to C 14 / 26
44 Call by Value/Reference Provides call-by-value as well as call-by-reference Representative language: Pascal, C++ Call by value is similar to C Call by reference is indicated by additional qualifiers In Pascal: procedure swap(var a:integer, var b:integer) var t; begin t := a; a := b; b := t end In C++: void swap(int& a, int& b) int t; begin t := a; a := b; b := t end 14 / 26
45 Call by Sharing Acts as call-by-value for the immutable objects and call-by-reference for mutable objects. Also called call-by-object Representative language: Python, Java, C# 15 / 26
46 Call by Sharing Acts as call-by-value for the immutable objects and call-by-reference for mutable objects. Also called call-by-object Representative language: Python, Java, C# Behaves like call-by-value for immutable objects in assignments 15 / 26
47 Call by Sharing Acts as call-by-value for the immutable objects and call-by-reference for mutable objects. Also called call-by-object Representative language: Python, Java, C# Behaves like call-by-value for immutable objects in assignments Behaves like call-by-reference for mutable objects Immutable objects in Python: Mutable objects in Python: str = "cannot be changed" lst = ["can", "be", "changed"] def change(par): par = "change" change(str) def change(par): par.append("sure") change(lst) 15 / 26
48 Representative language: Ada Call by Result 16 / 26
49 Representative language: Ada Call by Result The value of the formal parameter is copied into the actual parameter when the subroutine returns x : integer procedure foo(y : out integer) y := 3 print x... x := 2 foo(x) print x If y is passed by reference the program will print 3 twice. If y is passed by value/result, it will print 2 and then 3 16 / 26
50 Call by Name Representative language: Algol / 26
51 Call by Name Representative language: Algol 60 The actual arguments are not evaluated at all; they are substituted into the function body directly integer i; real procedure sum (i, lo, hi, term); value lo, hi; integer i, lo, hi; real term; begin real temp; temp := 0; for i := lo step 1 until hi do temp := temp + term; sum := temp end; print (sum (i, 1, 100, 1/i)) It computes the 100th harmonic number by the formula H 100 = i i=1 17 / 26
52 Call by Need Representative language: Haskell 18 / 26
53 Call by Need Representative language: Haskell A memorized version of call-by-name; if the function argument is evaluated, that value is stored for subsequent uses Call by name: 3 evaluations doub x = x + x Call by need: 2 evaluations doub x = x + x doub (doub 8) = doub 8 + doub 8 = = 32 doub (doub 8) = doub 8 + doub 8 = doub 8 = = / 26
54 Closures as Parameters A closure (a reference to a subroutine, together with its referencing environment) can be passed as a parameter In Standard Pascal: A : array [low..high : integer] of integer; procedure apply_to_a(function f(n : integer) : integer); var i : integer; begin for i := low to high do A[i] := f(a[i]); end; 19 / 26
55 Closures as Parameters A closure (a reference to a subroutine, together with its referencing environment) can be passed as a parameter In Standard Pascal: A : array [low..high : integer] of integer; procedure apply_to_a(function f(n : integer) : integer); var i : integer; begin for i := low to high do A[i] := f(a[i]); end; In C and C++: int A[50]; void apply_to_a(int (*f)(int)) { int i; for (i = 0; i < sizeof(a); i++) A[i] = f(a[i]); } 19 / 26
56 Conformant Arrays A formal array parameter whose shape is finalized at run time Languages that support conformant arrays include : Ada, Pascal 20 / 26
57 Conformant Arrays A formal array parameter whose shape is finalized at run time Languages that support conformant arrays include : Ada, Pascal In Standard Pascal: procedure apply_to_a(function f(n : integer) : integer; A : array [low..high : integer] of integer); var i : integer; begin for i := low to high do A[i] := f(a[i]); end; 20 / 26
58 Conformant Arrays A formal array parameter whose shape is finalized at run time Languages that support conformant arrays include : Ada, Pascal In Standard Pascal: procedure apply_to_a(function f(n : integer) : integer; A : array [low..high : integer] of integer); var i : integer; begin for i := low to high do A[i] := f(a[i]); end; C passes only pointers to arrays to functions and array size has to be determined using some other means (e.g. as another parameter) void apply_to_a(int (*f)(int), int A[], int size_of_a) { int i; for (i = 0; i < size_of_a; i++) A[i] = f(a[i]); } 20 / 26
59 Conformant Arrays A formal array parameter whose shape is finalized at run time Languages that support conformant arrays include : Ada, Pascal In Standard Pascal: procedure apply_to_a(function f(n : integer) : integer; A : array [low..high : integer] of integer); var i : integer; begin for i := low to high do A[i] := f(a[i]); end; C passes only pointers to arrays to functions and array size has to be determined using some other means (e.g. as another parameter) void apply_to_a(int (*f)(int), int A[], int size_of_a) { int i; for (i = 0; i < size_of_a; i++) A[i] = f(a[i]); } What is made harder in a language with non-conformant arrays? 20 / 26
60 Default Parameters A default parameter is one that need not necessarily be provided by the caller Languages that support default parameters include : Ada, C++, Common Lisp, Python 21 / 26
61 Default Parameters A default parameter is one that need not necessarily be provided by the caller Languages that support default parameters include : Ada, C++, Common Lisp, Python When an actual parameter is missing, a preestablished default value will be used instead 21 / 26
62 Default Parameters A default parameter is one that need not necessarily be provided by the caller Languages that support default parameters include : Ada, C++, Common Lisp, Python When an actual parameter is missing, a preestablished default value will be used instead Example in C++: void print_num(int n, int base = 10) / 26
63 Default Parameters A default parameter is one that need not necessarily be provided by the caller Languages that support default parameters include : Ada, C++, Common Lisp, Python When an actual parameter is missing, a preestablished default value will be used instead Example in C++: void print_num(int n, int base = 10)... Example in Python: def function(data=[]): data.append(1) return data 21 / 26
64 Named Parameters Named parameter (aka keyword parameter) explicitly binds the actual parameter to the formal parameter Languages that support named parameters include : Ada, Modula-3, Common Lisp, and Python 22 / 26
65 Named Parameters Named parameter (aka keyword parameter) explicitly binds the actual parameter to the formal parameter Languages that support named parameters include : Ada, Modula-3, Common Lisp, and Python Good for documentation of the purpose of parameters format_page(columns => 2,window_height => 400, window_width => 200, header_font => Helvetica,..., background_color => white); 22 / 26
66 Named Parameters Named parameter (aka keyword parameter) explicitly binds the actual parameter to the formal parameter Languages that support named parameters include : Ada, Modula-3, Common Lisp, and Python Good for documentation of the purpose of parameters format_page(columns => 2,window_height => 400, window_width => 200, header_font => Helvetica,..., background_color => white); Allows default parameters anywhere in formal parameter list format_page(window_width => 200); 22 / 26
67 Variable-length Arguments Languages that support variable-length arguments include : C,C++,Java,C#, Python 23 / 26
68 Variable-length Arguments Languages that support variable-length arguments include : C,C++,Java,C#, Python Variable number of arguments in C and C++ is not type safe int printf(char *format,...) 23 / 26
69 Variable-length Arguments Languages that support variable-length arguments include : C,C++,Java,C#, Python Variable number of arguments in C and C++ is not type safe int printf(char *format,...) Java supports typed variable argument lists via the <type-name>... syntax static void print_lines(string foo, String... lines) 23 / 26
70 Variable-length Arguments Languages that support variable-length arguments include : C,C++,Java,C#, Python Variable number of arguments in C and C++ is not type safe int printf(char *format,...) Java supports typed variable argument lists via the <type-name>... syntax static void print_lines(string foo, String... lines) C# allows typed variable argument lists via the params keyword static void print_lines(string foo, params String[] lines) 23 / 26
71 Variable-length Arguments Languages that support variable-length arguments include : C,C++,Java,C#, Python Variable number of arguments in C and C++ is not type safe int printf(char *format,...) Java supports typed variable argument lists via the <type-name>... syntax static void print_lines(string foo, String... lines) C# allows typed variable argument lists via the params keyword static void print_lines(string foo, params String[] lines) Python takes variable argument lists via the asterisk operator (*) def foo(*args): print("number of arguments:", len(args)) print("arguments are: ", args) 23 / 26
72 Outline 1 Subroutines 2 Parameter Passing 3 Generic Subroutines 24 / 26
73 Generic Subroutines and Modules Generic Parameters Options: 25 / 26
74 Generic Subroutines and Modules Generic Parameters Options: Java and C# pass only types 25 / 26
75 Generic Subroutines and Modules Generic Parameters Options: Java and C# pass only types Ada and C++ allow values of ordinary (nongeneric) types, including subroutines and classes 25 / 26
76 Generic Subroutines and Modules Generic Parameters Options: Java and C# pass only types Ada and C++ allow values of ordinary (nongeneric) types, including subroutines and classes Implementation Options: 25 / 26
77 Generic Subroutines and Modules Generic Parameters Options: Java and C# pass only types Ada and C++ allow values of ordinary (nongeneric) types, including subroutines and classes Implementation Options: Ada and C++ use a purely static mechanism: the compiler creates a separate copy of the code for every instance 25 / 26
78 Generic Subroutines and Modules Generic Parameters Options: Java and C# pass only types Ada and C++ allow values of ordinary (nongeneric) types, including subroutines and classes Implementation Options: Ada and C++ use a purely static mechanism: the compiler creates a separate copy of the code for every instance Java guarantees that all instances of a given generic will share the same code at run time 25 / 26
79 Generic Subroutines and Modules Generic Parameters Options: Java and C# pass only types Ada and C++ allow values of ordinary (nongeneric) types, including subroutines and classes Implementation Options: Ada and C++ use a purely static mechanism: the compiler creates a separate copy of the code for every instance Java guarantees that all instances of a given generic will share the same code at run time C# uses different implementations for different built-in or value types while maintaining type safety 25 / 26
80 Generic Parameter Constraints In Java one requires that a generic parameter support a particular set of methods public static <T extends Comparable<T>> void sort(t A[]) {... if (A[i].compareTo(A[j]) >= 0) } 26 / 26
81 Generic Parameter Constraints In Java one requires that a generic parameter support a particular set of methods public static <T extends Comparable<T>> void sort(t A[]) {... if (A[i].compareTo(A[j]) >= 0) } Same in C# in a different syntax static void sort<t>(t[] A) where T : IComparable {... if (A[i].CompareTo(A[j]) >= 0) } 26 / 26
82 Generic Parameter Constraints In Java one requires that a generic parameter support a particular set of methods public static <T extends Comparable<T>> void sort(t A[]) {... if (A[i].compareTo(A[j]) >= 0) } Same in C# in a different syntax static void sort<t>(t[] A) where T : IComparable {... if (A[i].CompareTo(A[j]) >= 0) } C++ does not impose explicit constraints 26 / 26
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
More informationProgramming 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
More informationControl 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,
More informationChap. 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
More informationG 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
More informationChapter 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
More informationChapter 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
More informationG 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
More informationChapter 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
More informationSystem 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
More informationCS 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
More informationCS 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
More informationCS 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:
More informationLecture 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,
More informationProgramming 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
More informationDesign 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
More informationChapter 9 Subprograms
Chapter 9 Subprograms We now explore the design of subprograms, including parameter-passing methods, local referencing environment, overloaded subprograms, generic subprograms, and the aliasing and problematic
More informationConcepts 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
More informationCSE 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
More informationLECTURE 14. Names, Scopes, and Bindings: Scopes
LECTURE 14 Names, Scopes, and Bindings: Scopes SCOPE The scope of a binding is the textual region of a program in which a name-to-object binding is active. Nonspecifically, scope is a program region of
More informationWeeks 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
More informationLECTURE 19. Subroutines and Parameter Passing
LECTURE 19 Subroutines and Parameter Passing ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments behind a simple name. Data abstraction: hide data
More informationCA Compiler Construction
CA4003 - Compiler Construction David Sinclair When procedure A calls procedure B, we name procedure A the caller and procedure B the callee. A Runtime Environment, also called an Activation Record, is
More informationImplementing 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
More informationChapter 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
More informationProgramming 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
More informationRun-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
More informationCSc 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
More informationCSc 520 Principles of Programming Languages
CSc 520 Principles of Programming Languages 28 : Control Structures Parameters Christian Collberg Department of Computer Science University of Arizona collberg+520@gmail.com Copyright c 2008 Christian
More informationCSc 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;
More informationCMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction
9. 9.1. Introduction Two fundamental abstraction facilities Process abstraction Emphasized from early days Data abstraction Emphasized in the1980s 9.2. 9.2.1. General Subprogram Characteristics Each subprogram
More informationCIT 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
More informationUNIT 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
More informationMemory 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,
More informationAnnouncements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.
IR Generation Announcements My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM. This is a hard deadline and no late submissions will be
More informationCode Generation & Parameter Passing
Code Generation & Parameter Passing Lecture Outline 1. Allocating temporaries in the activation record Let s optimize our code generator a bit 2. A deeper look into calling sequences Caller/Callee responsibilities
More informationG Programming Languages - Fall 2012
G22.2110-003 Programming Languages - Fall 2012 Lecture 2 Thomas Wies New York University Review Last week Programming Languages Overview Syntax and Semantics Grammars and Regular Expressions High-level
More informationRun 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
More informationInformal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility
Informal Semantics of Data semantic specification names (identifiers) attributes binding declarations scope rules visibility 1 Ways to Specify Semantics Standards Documents (Language Definition) Language
More informationPrinciples of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004
Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004 CS 314, LS,BR,LTM: Scope and Memory 1 Review Functions as first-class objects What can you do with an integer?
More informationChapter 10. Implementing Subprograms
Chapter 10 Implementing Subprograms Chapter 10 Topics The General Semantics of Calls and Returns Implementing Simple Subprograms Implementing Subprograms with Stack-Dynamic Local Variables Nested Subprograms
More informationCS 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
More informationCSE Lecture In Class Example Handout
CSE 30321 Lecture 07-09 In Class Example Handout Part A: A Simple, MIPS-based Procedure: Swap Procedure Example: Let s write the MIPS code for the following statement (and function call): if (A[i] > A
More informationCS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline
CS 0 Lecture 8 Chapter 5 Louden Outline The symbol table Static scoping vs dynamic scoping Symbol table Dictionary associates names to attributes In general: hash tables, tree and lists (assignment ) can
More informationProgramming Languages
Programming Languages Tevfik Koşar Lecture - VIII February 9 th, 2006 1 Roadmap Allocation techniques Static Allocation Stack-based Allocation Heap-based Allocation Scope Rules Static Scopes Dynamic Scopes
More informationCS558 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.
More informationReferences 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,
More informationRun-time Environments - 2
Run-time Environments - 2 Y.N. Srikant Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler Design Outline of the Lecture n What is run-time
More informationCS558 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.
More informationRun 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
More informationSubprograms. Copyright 2015 Pearson. All rights reserved. 1-1
Subprograms Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing Methods Parameters That Are Subprograms Calling Subprograms Indirectly
More informationChapter 9. Def: The subprogram call and return operations of a language are together called its subprogram linkage
Def: The subprogram call and return operations of a language are together called its subprogram linkage Implementing FORTRAN 77 Subprograms Call Semantics: 1. Save the execution status of the caller 2.
More information6. Names, Scopes, and Bindings
Copyright (C) R.A. van Engelen, FSU Department of Computer Science, 2000-2004 6. Names, Scopes, and Bindings Overview Names Binding time Object lifetime Object storage management Static allocation Stack
More informationNames, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University
Names, Scopes, and Bindings CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Names, Scopes, and Bindings Names are identifiers (mnemonic character
More informationSubroutines and Control Abstraction. CSE 307 Principles of Programming Languages Stony Brook University
Subroutines and Control Abstraction CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Subroutines Why use subroutines? Give a name to a task. We
More informationNames, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University
Names, Scopes, and Bindings CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Names, Scopes, and Bindings Names are identifiers (mnemonic character
More informationNames and Abstractions: What s in a Name?
Copyright R.A. van Engelen, FSU Department of Computer Science, 2000 Names, Scopes, and Bindings Binding time Object lifetime Object storage management Static allocation Stack allocation Heap allocation
More informationExample. 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
More informationRun-time Environment
Run-time Environment Prof. James L. Frankel Harvard University Version of 3:08 PM 20-Apr-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Storage Organization Automatic objects are
More informationCOP4020 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;
More informationSubroutines. Subroutine. Subroutine design. Control abstraction. If a subroutine does not fit on the screen, it is too long
Subroutines Subroutine = procedure (statement) - no return value - side effects function (expression) - return value - (no side effects in some languages) Subroutine Control abstraction Subroutine design
More informationWednesday, 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:
More informationCOMP 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
More informationSubprograms. 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
More informationNames, Scopes, and Bindings II. Hwansoo Han
Names, Scopes, and Bindings II Hwansoo Han Scope Rules A scope is textual region where bindings are active A program section of maximal size Bindings become active at the entry No bindings change in the
More information11/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
More informationLecture 7: Binding Time and Storage
Lecture 7: Binding Time and Storage COMP 524 Programming Language Concepts Stephen Olivier February 5, 2009 Based on notes by A. Block, N. Fisher, F. Hernandez-Campos, and D. Stotts Goal of Lecture The
More informationRun Time Environments
Run Time Environments ALSU Textbook Chapter 7.1 7.3 Tsan-sheng Hsu tshsu@iis.sinica.edu.tw http://www.iis.sinica.edu.tw/~tshsu 1 Preliminaries During the execution of a program, the same name in the source
More information12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return
Outline Implementing Subprograms In Text: Chapter 10 General semantics of calls and returns Implementing simple subroutines Call Stack Implementing subroutines with stackdynamic local variables Nested
More informationCSc 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
More informationSpecial 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
More informationCSc 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
More informationCOP4020 Programming Languages. Names, Scopes, and Bindings Prof. Robert van Engelen
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Robert van Engelen Overview Abstractions and names Binding time Object lifetime Object storage management Static allocation Stack allocation
More informationCSC 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
More informationModule 27 Switch-case statements and Run-time storage management
Module 27 Switch-case statements and Run-time storage management In this module we will discuss the pending constructs in generating three-address code namely switch-case statements. We will also discuss
More informationPROCEDURES, 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
More informationMIPS Functions and the Runtime Stack
MIPS Functions and the Runtime Stack COE 301 Computer Organization Prof. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals Presentation Outline
More informationAssembly Programming (III) Lecturer: Sri Parameswaran Notes by: Annie Guo Dr. Hui Wu
Assembly Programming (III) Lecturer: Sri Parameswaran Notes by: Annie Guo Dr. Hui Wu 1 Lecture overview Stack and stack operations Functions and function calls Calling conventions 2 Stack What is stack?
More informationOrganization of Programming Languages CS 3200/5200N. Lecture 09
Organization of Programming Languages CS 3200/5200N Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.edu Control Flow Control flow = the flow of control, or execution
More informationEvery language has its own scoping rules. For example, what is the scope of variable j in this Java program?
Lexical Binding There are two ways a variable can be used in a program: As a declaration As a "reference" or use of the variable Scheme has two kinds of variable "declarations" -- the bindings of a let-expression
More informationCompiler 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
More informationImplementing Subprograms
Implementing Subprograms 1 Topics The General Semantics of Calls and Returns Implementing Simple Subprograms Implementing Subprograms with Stack-Dynamic Local Variables Nested Subprograms Blocks Implementing
More informationG Programming Languages - Fall 2012
G22.2110-003 Programming Languages - Fall 2012 Lecture 3 Thomas Wies New York University Review Last week Names and Bindings Lifetimes and Allocation Garbage Collection Scope Outline Control Flow Sequencing
More informationImplementing Subroutines. Outline [1]
Implementing Subroutines In Text: Chapter 9 Outline [1] General semantics of calls and returns Implementing simple subroutines Call Stack Implementing subroutines with stackdynamic local variables Nested
More informationType Bindings. Static Type Binding
Type Bindings Two key issues in binding (or associating) a type to an identifier: How is type binding specified? When does the type binding take place? N. Meng, S. Arthur 1 Static Type Binding An explicit
More informationChapter 5. Names, Bindings, and Scopes
Chapter 5 Names, Bindings, and Scopes Chapter 5 Topics Introduction Names Variables The Concept of Binding Scope Scope and Lifetime Referencing Environments Named Constants 1-2 Introduction Imperative
More informationWhere We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.
Where We Are Source Code Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Machine Code Where We Are Source Code Lexical Analysis Syntax Analysis
More informationECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware
Supporting Procedures in Computer Hardware James Moscola Dept. of Engineering & Computer Science York College of Pennsylvania Based on Computer Organization and Design, 5th Edition by Patterson & Hennessy
More informationChapter 5 Names, Binding, Type Checking and Scopes
Chapter 5 Names, Binding, Type Checking and Scopes Names - We discuss all user-defined names here - Design issues for names: -Maximum length? - Are connector characters allowed? - Are names case sensitive?
More informationScope. Chapter Ten Modern Programming Languages 1
Scope Chapter Ten Modern Programming Languages 1 Reusing Names Scope is trivial if you have a unique name for everything: fun square a = a * a; fun double b = b + b; But in modern languages, we often use
More informationCompilation /15a Lecture 7. Activation Records Noam Rinetzky
Compilation 0368-3133 2014/15a Lecture 7 Activation Records Noam Rinetzky 1 Code generation for procedure calls (+ a few words on the runtime system) 2 Code generation for procedure calls Compile time
More informationLectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS
Lectures 5 Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS 1 OOPS - What does this C code do? int foo(char *s) { int L = 0; while (*s++) { ++L; } return L; } 2
More informationLECTURE 18. Control Flow
LECTURE 18 Control Flow CONTROL FLOW Sequencing: the execution of statements and evaluation of expressions is usually in the order in which they appear in a program text. Selection (or alternation): a
More informationCS 415 Midterm Exam Spring 2002
CS 415 Midterm Exam Spring 2002 Name KEY Email Address Student ID # Pledge: This exam is closed note, closed book. Good Luck! Score Fortran Algol 60 Compilation Names, Bindings, Scope Functional Programming
More informationCS 4100 Block Structured Languages. Fixed vs Variable. Activation Record. Activation Records. State of an Activation
Chapter 6: Implementation of Block-Structure CS 4100 Block Structured Languages From Principles of Programming Languages: Design, Evaluation, and Implementation (Third Edition), by Bruce J. MacLennan,
More information22c: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
More informationTopic 3-a. Calling Convention 2/29/2008 1
Topic 3-a Calling Convention 2/29/2008 1 Calling Convention Calling convention is a standardized method for a program to pass parameters to a procedure and receive result values back from it. 2/29/2008
More informationInformatica 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
More informationChapter 10 Implementing Subprograms
Chapter 10 Implementing Subprograms The General Semantics of Calls and Returns - Definition: The subprogram call and return operations of a language are together called its subprogram linkage Implementing
More informationCompilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1
Compilers 8. Run-time Support Laszlo Böszörmenyi Compilers Run-time - 1 Run-Time Environment A compiler needs an abstract model of the runtime environment of the compiled code It must generate code for
More information