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

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

Runtime Environment. Part II May 8th Wednesday, May 8, 13

Three-Address Code IR

IR Generation. May 13, Monday, May 13, 13

Run-time Environments

Run-time Environments

G Programming Languages - Fall 2012

Procedure and Object- Oriented Abstraction

Short Notes of CS201

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

CS201 - Introduction to Programming Glossary By

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

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

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

Run-time Environments - 2

Topic 7: Activation Records

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

Lexical Considerations

System Software Assignment 1 Runtime Support for Procedures

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

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

Concepts Introduced in Chapter 7

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

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

CS 432 Fall Mike Lam, Professor. Code Generation

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

Principles of Programming Languages

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

Assignment 11: functions, calling conventions, and the stack

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

CS 415 Midterm Exam Spring 2002

Implementing Subroutines. Outline [1]

Chapter 9 :: Subroutines and Control Abstraction

CS 2210 Programming Project (Part IV)

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

Lexical Considerations

2. Reachability in garbage collection is just an approximation of garbage.

1 Lexical Considerations

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

Code Generation. Lecture 12

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

Intermediate Code Generation

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

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

Introduction to Programming Using Java (98-388)

Intermediate Representations

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

Functions CHAPTER 5. FIGURE 1. Concrete syntax for the P 2 subset of Python. (In addition to that of P 1.)

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

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

Project 3 Due October 21, 2015, 11:59:59pm

Compilers and computer architecture: A realistic compiler to MIPS

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

Scope, Functions, and Storage Management

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

CS 314 Principles of Programming Languages

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

Compiling Techniques

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

Run-time Environment

UNIT 3

Intermediate Code Generation

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

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

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

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

INTERMEDIATE REPRESENTATIONS RTL EXAMPLE

The role of semantic analysis in a compiler

Intermediate Representations & Symbol Tables

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

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

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

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

CS 230 Programming Languages

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

CS 314 Principles of Programming Languages. Lecture 11

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

Accelerating Ruby with LLVM

Control Abstraction. Hwansoo Han

TACi: Three-Address Code Interpreter (version 1.0)

Topic 3-a. Calling Convention 2/29/2008 1

Lecture 3: C Programm

THEORY OF COMPILATION

Computer Components. Software{ User Programs. Operating System. Hardware

CA Compiler Construction

Informatica 3 Syntax and Semantics

Procedures and Stacks

G Programming Languages - Fall 2012

CS558 Programming Languages

Subroutines and Control Abstraction. CSE 307 Principles of Programming Languages Stony Brook University

Parsing Scheme (+ (* 2 3) 1) * 1

Code Generation. Dragon: Ch (Just part of it) Holub: Ch 6.

Run Time Environments

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

Implementing Subprograms

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

Transcription:

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 accepted, even with late days. Remainder of the project due Wednesday at 11:59PM. Midterms graded; we'll hand them back at the end of class.

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 Semantic Analysis IR Generation IR Optimization Code Generation Optimization Machine Code Arche-Type

Where We Are Source Code Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Machine Code

What is IR Generation? Intermediate Representation Generation. The final phase of the compiler front-end. Goal: Translate the program into the format expected by the compiler back-end. Generated code need not be optimized; that's handled by later passes. Generated code need not be in assembly; that can also be handled by later passes.

Why do IR Generation? Simplify certain optimizations. Machine code has many constraints that inhibit optimization. (Such as?) Working with an intermediate language makes optimizations easier and clearer. Have many front-ends into a single back-end. gcc can handle C, C++, Java, Fortran, Ada, and many other languages. Each front-end translates source to the GENERIC language. Have many back-ends from a single front-end. Do most optimization on intermediate representation before emitting code targeted at a single machine.

Designing a Good IR IRs are like type systems they're extremely hard to get right. Need to balance needs of high-level source language and low-level target language. Too high level: can't optimize certain implementation details. Too low level: can't use high-level knowledge to perform aggressive optimizations. Often have multiple IRs in a single compiler.

Architecture of gcc

Source Code Architecture of gcc

Architecture of gcc Source Code AST

Architecture of gcc Source Code AST GENERIC

Architecture of gcc Source Code AST GENERIC High GIMPLE

Architecture of gcc Source Code AST GENERIC High GIMPLE SSA

Architecture of gcc Source Code AST GENERIC High GIMPLE SSA Low GIMPLE

Architecture of gcc Source Code AST GENERIC High GIMPLE SSA Low GIMPLE RTL

Architecture of gcc Source Code AST GENERIC High GIMPLE SSA Low GIMPLE RTL Machine Code

Another Approach: High-Level IR Examples: Java bytecode CPython bytecode LLVM IR Microsoft CIL. Retains high-level program structure. Try playing around with javap vs. a disassembler. Allows for compilation on target machines. Allows for JIT compilation or interpretation.

Outline Runtime Environments (Today/Monday) How do we implement language features in machine code? What data structures do we need? Three-Address Code (Monday/Wednesday) What IR are we using in this course? What features does it have? TAC generation (Monday/Wednesday) How do we translate Decaf into TAC?

Runtime Environments

An Important Duality Programming languages contain high-level structures: Functions Objects Exceptions Dynamic typing Lazy evaluation (etc.) The physical computer only operates in terms of several primitive operations: Arithmetic Data movement Control jumps

Runtime Environments We need to come up with a representation of these highlevel structures using the low-level structures of the machine. A runtime environment is a set of data structures maintained at runtime to implement these high-level structures. e.g. the stack, the heap, static area, virtual function tables, etc. Strongly depends on the features of both the source and target language. (e.g compiler vs. cross-compiler) Our IR design will depend on how we set up our runtime environment.

The Decaf Runtime Environment Need to consider What do objects look like in memory? What do functions look like in memory? Where in memory should they be placed? How are function calls implemented? How is inheritance implemented? There are no right answers to this question. Many different options and tradeoffs. We will see several approaches.

Data Representations What do different types look like in memory? Machine typically supports only limited types: Fixed-width integers: 8-bit, 16-bit- 32-bit, signed, unsigned, etc. Floating point values: 32-bit, 64-bit, 80-bit IEEE 754. How do we encode our object types using these types?

Encoding Primitive Types Primitive integral types (byte, char, short, int, long, unsigned, uint16_t, etc.) typically map directly to the underlying machine type. Primitive real-valued types (float, double, long double) typically map directly to underlying machine type. Pointers typically implemented as integers holding memory addresses. Size of integer depends on machine architecture; hence 32-bit compatibility mode on 64-bit machines.

Encoding Arrays C-style arrays: Elements laid out consecutively in memory. Arr[0] Arr[1] Arr[2]... Arr[n-1] Java-style arrays: Elements laid out consecutively in memory with size information prepended. n Arr[0] Arr[1] Arr[2]... Arr[n-1] D-style arrays: Elements laid out consecutively in memory; array variables store pointers to first and past-the-end elements. Arr[0] Arr[1] Arr[2]... Arr[n-1] First Past-End (Which of these works well for Decaf?)

Encoding Arrays C-style arrays: Elements laid out consecutively in memory. Arr[0] Arr[1] Arr[2]... Arr[n-1] Java-style arrays: Elements laid out consecutively in memory with size information prepended. n Arr[0] Arr[1] Arr[2]... Arr[n-1] D-style arrays: Elements laid out consecutively in memory; array variables store pointers to first and past-the-end elements. Arr[0] Arr[1] Arr[2]... Arr[n-1] First Past-End (Which of these works well for Decaf?)

Encoding Arrays C-style arrays: Elements laid out consecutively in memory. Arr[0] Arr[1] Arr[2]... Arr[n-1] Java-style arrays: Elements laid out consecutively in memory with size information prepended. n Arr[0] Arr[1] Arr[2]... Arr[n-1] D-style arrays: Elements laid out consecutively in memory; array variables store pointers to first and past-the-end elements. Arr[0] Arr[1] Arr[2]... Arr[n-1] First Past-End (Which of these works well for Decaf?)

Encoding Arrays C-style arrays: Elements laid out consecutively in memory. Arr[0] Arr[1] Arr[2]... Arr[n-1] Java-style arrays: Elements laid out consecutively in memory with size information prepended. n Arr[0] Arr[1] Arr[2]... Arr[n-1] D-style arrays: Elements laid out consecutively in memory; array variables store pointers to first and past-the-end elements. Arr[0] Arr[1] Arr[2]... Arr[n-1] First Past-End (Which of these works well for Decaf?)

Encoding Arrays C-style arrays: Elements laid out consecutively in memory. Arr[0] Arr[1] Arr[2]... Arr[n-1] Java-style arrays: Elements laid out consecutively in memory with size information prepended. n Arr[0] Arr[1] Arr[2]... Arr[n-1] D-style arrays: Elements laid out consecutively in memory; array variables store pointers to first and past-the-end elements. Arr[0] Arr[1] Arr[2]... Arr[n-1] First Past-End (Which of these works well for Decaf?)

Encoding Multidimensional Arrays Often represented as an array of arrays. Shape depends on the array type used. C-style arrays: int a[3][2];

Encoding Multidimensional Arrays Often represented as an array of arrays. Shape depends on the array type used. C-style arrays: int a[3][2]; a[0][0] a[0][1] a[1][0] a[1][1] a[2][0] a[2][1]

Encoding Multidimensional Arrays Often represented as an array of arrays. Shape depends on the array type used. C-style arrays: int a[3][2]; a[0][0] a[0][1] a[1][0] a[1][1] a[2][0] a[2][1] Array of size 2 Array of size 2 Array of size 2

Encoding Multidimensional Arrays Often represented as an array of arrays. Shape depends on the array type used. C-style arrays: int a[3][2]; How do you know where to look for an element in an array like this? a[0][0] a[0][1] a[1][0] a[1][1] a[2][0] a[2][1] Array of size 2 Array of size 2 Array of size 2

Encoding Multidimensional Arrays Often represented as an array of arrays. Shape depends on the array type used. Java-style arrays: int[][] a = new int [3][2];

Encoding Multidimensional Arrays Often represented as an array of arrays. Shape depends on the array type used. Java-style arrays: int[][] a = new int [3][2]; 3 a[0] a[1] a[2]

Encoding Multidimensional Arrays Often represented as an array of arrays. Shape depends on the array type used. Java-style arrays: int[][] a = new int [3][2]; 3 2 a[0][0] a[0][1] a[0] a[1] 2 a[1][0] a[1][1] a[2] 2 a[2][0] a[2][1]

Encoding Functions Many questions to answer: What does the dynamic execution of functions look like? Where is the executable code for functions located? How are parameters passed in and out of functions? Where are local variables stored? The answers strongly depend on what the language supports.

Review: The Stack Function calls are often implemented using a stack of activation records (or stack frames). Calling a function pushes a new activation record onto the stack. Returning from a function pops the current activation record from the stack. Questions: Why does this work? Does this always work?

Activation Trees An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of a program; can't always be determined at compile-time. (The static equivalent is the call graph). Each node in the tree is an activation record. Each activation record stores a control link to the activation record of the function that invoked it.

Activation Trees

Activation Trees int main() { Fib(3); } int Fib(int n) { if (n <= 1) return n; return Fib(n 1) + Fib(n 2); }

Activation Trees int main() { Fib(3); } main int Fib(int n) { if (n <= 1) return n; return Fib(n 1) + Fib(n 2); }

Activation Trees int main() { Fib(3); } int Fib(int n) { if (n <= 1) return n; return Fib(n 1) + Fib(n 2); } main Fib n = 3

Activation Trees int main() { Fib(3); } int Fib(int n) { if (n <= 1) return n; return Fib(n 1) + Fib(n 2); } main Fib n = 3 Fib n = 2

Activation Trees int main() { Fib(3); } int Fib(int n) { if (n <= 1) return n; return Fib(n 1) + Fib(n 2); } main Fib n = 3 Fib n = 2 Fib n = 1

Activation Trees int main() { Fib(3); } int Fib(int n) { if (n <= 1) return n; return Fib(n 1) + Fib(n 2); } main Fib n = 3 Fib n = 2 Fib n = 1 Fib n = 0

Activation Trees int main() { Fib(3); } int Fib(int n) { if (n <= 1) return n; return Fib(n 1) + Fib(n 2); } main Fib n = 3 Fib n = 2 Fib n = 1 Fib n = 1 Fib n = 0

An activation tree is a spaghetti stack.

The runtime stack is an optimization of this spaghetti stack.

Why can we optimize the stack? Two main reasons: Once a function returns, its activation record cannot be referenced again. We don't need to store old nodes in the activation tree. Every activation record has either finished executing or is an ancestor of the current activation record. We don't need to keep multiple branches alive at any one time. These are not true in general.

Breaking Assumption 1 Once a function returns, its activation record cannot be referenced again. Any ideas on how to break this? One option: Local functions function CreateCounter() { var counter = 0; return function() { counter ++; return counter; } }

Breaking Assumption 1 Once a function returns, its activation record cannot be referenced again. Any ideas on how to break this? One option: Closures function CreateCounter() { var counter = 0; return function() { counter ++; return counter; } }

Closures

Closures function CreateCounter() { var counter = 0; return function() { counter ++; return counter; } } function MyFunction() { f = CreateCounter(); print(f()); print(f()); }

Closures function CreateCounter() { var counter = 0; return function() { counter ++; return counter; } } function MyFunction() { f = CreateCounter(); print(f()); print(f()); } >

Closures function CreateCounter() { var counter = 0; return function() { counter ++; return counter; } } MyFunction function MyFunction() { f = CreateCounter(); print(f()); print(f()); } >

Closures function CreateCounter() { var counter = 0; return function() { counter ++; return counter; } } CreateCounter counter = 0 MyFunction function MyFunction() { f = CreateCounter(); print(f()); print(f()); } >

Closures function CreateCounter() { var counter = 0; return function() { counter ++; return counter; } } CreateCounter counter = 0 MyFunction f = <fn> function MyFunction() { f = CreateCounter(); print(f()); print(f()); } >

Closures function CreateCounter() { var counter = 0; return function() { counter ++; return counter; } } CreateCounter counter = 0 MyFunction f = <fn> function MyFunction() { f = CreateCounter(); print(f()); print(f()); } <fn> >

Closures function CreateCounter() { var counter = 0; return function() { counter ++; return counter; } } CreateCounter counter = 1 MyFunction f = <fn> function MyFunction() { f = CreateCounter(); print(f()); print(f()); } <fn> >

Closures function CreateCounter() { var counter = 0; return function() { counter ++; return counter; } } CreateCounter counter = 1 MyFunction f = <fn> function MyFunction() { f = CreateCounter(); print(f()); print(f()); } <fn> > 1

Closures function CreateCounter() { var counter = 0; return function() { counter ++; return counter; } } CreateCounter counter = 1 MyFunction f = <fn> function MyFunction() { f = CreateCounter(); print(f()); print(f()); } > 1 <fn> <fn>

Closures function CreateCounter() { var counter = 0; return function() { counter ++; return counter; } } CreateCounter counter = 2 MyFunction f = <fn> function MyFunction() { f = CreateCounter(); print(f()); print(f()); } > 1 <fn> <fn>

Closures function CreateCounter() { var counter = 0; return function() { counter ++; return counter; } } CreateCounter counter = 2 MyFunction f = <fn> function MyFunction() { f = CreateCounter(); print(f()); print(f()); } > 1 2 <fn> <fn>

Control and Activation Links The control link of a function is a pointer to the function that called it. Used to determine where to resume execution after the function returns. The access link of a function is a pointer to the activation record in which the function was created. Used by nested functions to determine the location of variables from the outer scope.

Closures and the Runtime Stack Languages supporting closures do not typically have a runtime stack. Activation records typically dynamically allocated and garbage collected. Interesting exception: gcc C allows for nested functions, but uses a runtime stack. Behavior is undefined if nested function accesses data from its enclosing function once that function returns. (Why?)

Breaking Assumption 2 Every activation record has either finished executing or is an ancestor of the current activation record. Any ideas on how to break this?

Breaking Assumption 2 Every activation record has either finished executing or is an ancestor of the current activation record. Any ideas on how to break this? One idea: Coroutines def downfrom(n): while n > 0: yield n n = n - 1

Breaking Assumption 2 Every activation record has either finished executing or is an ancestor of the current activation record. Any ideas on how to break this? One idea: Coroutines def downfrom(n): while n > 0: yield n n = n - 1

Coroutines

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i >

Coroutines def downfrom(n): while n > 0: yield n n = n 1 myfunc def myfunc(): for i in downfrom(3): print i >

Coroutines def downfrom(n): while n > 0: yield n n = n 1 myfunc def myfunc(): for i in downfrom(3): print i >

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc downfrom n = 3 >

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc downfrom n = 3 >

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc downfrom n = 3 >

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 3 downfrom n = 3 >

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 3 downfrom n = 3 >

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 3 downfrom n = 3 > 3

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 3 downfrom n = 3 > 3

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 3 downfrom n = 3 > 3

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 3 downfrom n = 2 > 3

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 3 downfrom n = 2 > 3

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 3 downfrom n = 2 > 3

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 2 downfrom n = 2 > 3

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 2 downfrom n = 2 > 3

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 2 downfrom n = 2 > 3 2

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 2 downfrom n = 2 > 3 2

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 2 downfrom n = 2 > 3 2

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 2 downfrom n = 1 > 3 2

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 2 downfrom n = 1 > 3 2

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 2 downfrom n = 1 > 3 2

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 1 downfrom n = 1 > 3 2

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 1 downfrom n = 1 > 3 2

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 1 downfrom n = 1 > 3 2 1

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 1 downfrom n = 1 > 3 2 1

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 1 downfrom n = 1 > 3 2 1

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 1 downfrom n = 0 > 3 2 1

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 1 downfrom n = 0 > 3 2 1

Coroutines def downfrom(n): while n > 0: yield n n = n 1 def myfunc(): for i in downfrom(3): print i myfunc i = 1 downfrom n = 0 > 3 2 1

Coroutines A subroutine is a function that, when invoked, runs to completion and returns control to the calling function. Master/slave relationship between caller/callee. A coroutine is a function that, when invoked, does some amount of work, then returns control to the calling function. It can then be resumed later. Peer/peer relationship between caller/callee. Subroutines are a special case of coroutines.

Coroutines and the Runtime Stack Coroutines often cannot be implemented with purely a runtime stack. What if a function has multiple coroutines running alongside it? Few languages support coroutines, though some do (Python, for example).

So What? Even a concept as fundamental as the stack is actually quite complex. When designing a compiler or programming language, you must keep in mind how your language features influence the runtime environment. Always be critical of the languages you use!

Functions in Decaf We use an explicit runtime stack. Each activation record needs to hold All of its parameters. All of its local variables. All temporary variables introduced by the IR generator (more on that later). Where do these variables go? Who allocates space for them?

Decaf Stack Frames The logical layout of a Decaf stack frame is created by the IR generator. Ignores details about machine-specific calling conventions. We'll discuss today. The physical layout of a Decaf stack frame is created by the code generator. Based on the logical layout set up by the IR generator. Includes frame pointers, caller-saved registers, and other fun details like this. We'll discuss when talking about code generation.

A Logical Decaf Stack Frame Stack frame for function f(a,, n) Param N Param N 1... Param 1 Storage for Locals and Temporaries

A Logical Decaf Stack Frame Stack frame for function f(a,, n) Param N Param N 1... Param 1 Storage for Locals and Temporaries Param M

A Logical Decaf Stack Frame Stack frame for function f(a,, n) Param N Param N 1... Param 1 Storage for Locals and Temporaries Param M

A Logical Decaf Stack Frame Stack frame for function f(a,, n) Param N Param N 1... Param 1 Storage for Locals and Temporaries Param M Param 1

A Logical Decaf Stack Frame Stack frame for function f(a,, n) Param N Param N 1... Param 1 Storage for Locals and Temporaries Param M Param 1 Storage for Locals and Temporaries

A Logical Decaf Stack Frame Stack frame for function f(a,, n) Stack frame for function g(a,, m) Param N Param N 1... Param 1 Storage for Locals and Temporaries Param M Param 1 Storage for Locals and Temporaries

Decaf IR Calling Convention Caller responsible for pushing and popping space for callee's arguments. (Why?) Callee responsible for pushing and popping space for its own temporaries. (Why?)

Parameter Passing Approaches Two common approaches. Call-by-value Parameters are copies of the values specified as arguments. Call-by-reference: Parameters are pointers to values specified as parameters.

Other Parameter Passing Ideas JavaScript: Functions can be called with any number of arguments. Parameters are initialized to the corresponding argument, or undefined if not enough arguments were provided. The entire parameters array can be retrieved through the arguments array. How might this be implemented?

Other Parameter Passing Ideas Python: Keyword Arguments Functions can be written to accept any number of key/value pairs as arguments. Values stored in a special argument (traditionally named kwargs) kwargs can be manipulated (more or less) as a standard variable. How might this be implemented?

Summary of Function Calls The runtime stack is an optimization of the activation tree spaghetti stack. Most languages use a runtime stack, though certain language features prohibit this optimization. Activation records logically store a control link to the calling function and an access link to the function in which it was created. Decaf has the caller manage space for parameters and the callee manage space for its locals and temporaries. Call-by-value and call-by-name can be implemented using copying and pointers. More advanced parameter passing schemes exist!

Midterm Scores 12 10 Frequency 8 6 4 2 0 0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 Mean: 38 Median: 40 Stdev: 15.3

Next Time Object layouts The limits of standard vtables Inline caching Prototypical inheritance (ITA) Three-address code IR. Generating TAC.