![Page 1: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/1.jpg)
IR Generation
![Page 2: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/2.jpg)
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.
![Page 3: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/3.jpg)
Where We Are
Lexical Analysis
Semantic Analysis
Syntax Analysis
IR Generation
IR Optimization
Code Generation
Optimization
SourceCode
MachineCode
![Page 4: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/4.jpg)
Where We Are
IR Generation
IR Optimization
Code Generation
Optimization
SourceCode
MachineCode
Arche-Type
Lexical Analysis
Semantic Analysis
Syntax Analysis
Arche-Type
![Page 5: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/5.jpg)
Where We Are
Lexical Analysis
Semantic Analysis
Syntax Analysis
IR Generation
IR Optimization
Code Generation
Optimization
SourceCode
MachineCode
![Page 6: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/6.jpg)
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.
![Page 7: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/7.jpg)
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.
![Page 8: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/8.jpg)
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.
![Page 9: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/9.jpg)
Architecture of gcc
![Page 10: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/10.jpg)
Architecture of gcc
SourceCode
![Page 11: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/11.jpg)
Architecture of gcc
SourceCode
AST
![Page 12: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/12.jpg)
Architecture of gcc
SourceCode
AST
GENERIC
![Page 13: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/13.jpg)
Architecture of gcc
SourceCode
AST
GENERIC
HighGIMPLE
![Page 14: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/14.jpg)
Architecture of gcc
SourceCode
AST
GENERIC
HighGIMPLE
SSA
![Page 15: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/15.jpg)
Architecture of gcc
SourceCode
AST
GENERIC
HighGIMPLE
SSA
LowGIMPLE
![Page 16: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/16.jpg)
Architecture of gcc
SourceCode
AST
GENERIC
HighGIMPLE
SSA
LowGIMPLE
RTL
![Page 17: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/17.jpg)
Architecture of gcc
SourceCode
AST
GENERIC
HighGIMPLE
SSA
LowGIMPLE
RTL
MachineCode
![Page 18: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/18.jpg)
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.
![Page 19: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/19.jpg)
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?
![Page 20: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/20.jpg)
Runtime Environments
![Page 21: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/21.jpg)
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
![Page 22: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/22.jpg)
Runtime Environments
● We need to come up with a representation of these high-level 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.
![Page 23: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/23.jpg)
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.
![Page 24: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/24.jpg)
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?
![Page 25: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/25.jpg)
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.
![Page 26: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/26.jpg)
Encoding Arrays● C-style arrays: Elements laid out consecutively in memory.
● Java-style arrays: Elements laid out consecutively in memory with size information prepended.
● D-style arrays: Elements laid out consecutively in memory; array variables store pointers to first and past-the-end elements.
● (Which of these works well for Decaf?)
Arr[0] Arr[1] Arr[2] ... Arr[n-1]
Arr[0] Arr[1] Arr[2] ... Arr[n-1]n
Arr[0] Arr[1] Arr[2] ... Arr[n-1]
First Past-End
![Page 27: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/27.jpg)
Encoding Arrays● C-style arrays: Elements laid out consecutively in memory.
● Java-style arrays: Elements laid out consecutively in memory with size information prepended.
● D-style arrays: Elements laid out consecutively in memory; array variables store pointers to first and past-the-end elements.
● (Which of these works well for Decaf?)
Arr[0] Arr[1] Arr[2] ... Arr[n-1]
Arr[0] Arr[1] Arr[2] ... Arr[n-1]n
Arr[0] Arr[1] Arr[2] ... Arr[n-1]
First Past-End
![Page 28: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/28.jpg)
Encoding Arrays● C-style arrays: Elements laid out consecutively in memory.
● Java-style arrays: Elements laid out consecutively in memory with size information prepended.
● D-style arrays: Elements laid out consecutively in memory; array variables store pointers to first and past-the-end elements.
● (Which of these works well for Decaf?)
Arr[0] Arr[1] Arr[2] ... Arr[n-1]
Arr[0] Arr[1] Arr[2] ... Arr[n-1]n
Arr[0] Arr[1] Arr[2] ... Arr[n-1]
First Past-End
![Page 29: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/29.jpg)
Encoding Arrays● C-style arrays: Elements laid out consecutively in memory.
● Java-style arrays: Elements laid out consecutively in memory with size information prepended.
● D-style arrays: Elements laid out consecutively in memory; array variables store pointers to first and past-the-end elements.
● (Which of these works well for Decaf?)
Arr[0] Arr[1] Arr[2] ... Arr[n-1]
Arr[0] Arr[1] Arr[2] ... Arr[n-1]n
Arr[0] Arr[1] Arr[2] ... Arr[n-1]
First Past-End
![Page 30: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/30.jpg)
Encoding Arrays● C-style arrays: Elements laid out consecutively in memory.
● Java-style arrays: Elements laid out consecutively in memory with size information prepended.
● D-style arrays: Elements laid out consecutively in memory; array variables store pointers to first and past-the-end elements.
● (Which of these works well for Decaf?)
Arr[0] Arr[1] Arr[2] ... Arr[n-1]
Arr[0] Arr[1] Arr[2] ... Arr[n-1]n
Arr[0] Arr[1] Arr[2] ... Arr[n-1]
First Past-End
![Page 31: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/31.jpg)
Encoding Multidimensional Arrays
● Often represented as an array of arrays.● Shape depends on the array type used.● C-style arrays:
int a[3][2];
![Page 32: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/32.jpg)
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]
![Page 33: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/33.jpg)
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
![Page 34: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/34.jpg)
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
How do you know where to look for an element in an array
like this?
![Page 35: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/35.jpg)
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];
![Page 36: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/36.jpg)
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];
a[0]
a[1]
a[2]
3
![Page 37: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/37.jpg)
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];
a[0]
a[1]
a[2]
3 a[0][0] a[0][1]
a[1][0] a[1][1]
a[2][0] a[2][1]
2
2
2
![Page 38: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/38.jpg)
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.
![Page 39: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/39.jpg)
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?
![Page 40: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/40.jpg)
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.
![Page 41: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/41.jpg)
Activation Trees
![Page 42: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/42.jpg)
Activation Treesint main() { Fib(3);}
int Fib(int n) { if (n <= 1) return n; return Fib(n – 1) + Fib(n – 2);}
![Page 43: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/43.jpg)
Activation Treesint main() { Fib(3);}
int Fib(int n) { if (n <= 1) return n; return Fib(n – 1) + Fib(n – 2);}
main
![Page 44: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/44.jpg)
Activation Treesint main() { Fib(3);}
int Fib(int n) { if (n <= 1) return n; return Fib(n – 1) + Fib(n – 2);}
main
Fib
n = 3
![Page 45: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/45.jpg)
Activation Treesint 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
![Page 46: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/46.jpg)
Activation Treesint 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
![Page 47: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/47.jpg)
Activation Treesint 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 = 0
Fib
n = 1
![Page 48: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/48.jpg)
Activation Treesint 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 = 1
Fib
n = 2
Fib
n = 0
Fib
n = 1
![Page 49: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/49.jpg)
An activation tree is a spaghetti stack.
![Page 50: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/50.jpg)
The runtime stack is an optimization of this spaghetti stack.
![Page 51: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/51.jpg)
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.
![Page 52: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/52.jpg)
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; }}
![Page 53: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/53.jpg)
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; }}
![Page 54: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/54.jpg)
Closures
![Page 55: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/55.jpg)
Closuresfunction CreateCounter() { var counter = 0; return function() { counter ++; return counter; }}
function MyFunction() { f = CreateCounter(); print(f()); print(f());}
![Page 56: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/56.jpg)
Closuresfunction CreateCounter() { var counter = 0; return function() { counter ++; return counter; }}
function MyFunction() { f = CreateCounter(); print(f()); print(f());}
>
![Page 57: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/57.jpg)
Closuresfunction CreateCounter() { var counter = 0; return function() { counter ++; return counter; }}
function MyFunction() { f = CreateCounter(); print(f()); print(f());}
>
MyFunction
![Page 58: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/58.jpg)
Closuresfunction CreateCounter() { var counter = 0; return function() { counter ++; return counter; }}
function MyFunction() { f = CreateCounter(); print(f()); print(f());}
>
CreateCounter
counter = 0
MyFunction
![Page 59: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/59.jpg)
Closuresfunction CreateCounter() { var counter = 0; return function() { counter ++; return counter; }}
function MyFunction() { f = CreateCounter(); print(f()); print(f());}
>
CreateCounter
counter = 0
MyFunction
f = <fn>
![Page 60: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/60.jpg)
Closuresfunction CreateCounter() { var counter = 0; return function() { counter ++; return counter; }}
function MyFunction() { f = CreateCounter(); print(f()); print(f());}
>
CreateCounter
<fn>
counter = 0
MyFunction
f = <fn>
![Page 61: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/61.jpg)
Closuresfunction CreateCounter() { var counter = 0; return function() { counter ++; return counter; }}
function MyFunction() { f = CreateCounter(); print(f()); print(f());}
>
CreateCounter
<fn>
counter = 1
MyFunction
f = <fn>
![Page 62: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/62.jpg)
Closuresfunction CreateCounter() { var counter = 0; return function() { counter ++; return counter; }}
function MyFunction() { f = CreateCounter(); print(f()); print(f());}
> 1
CreateCounter
<fn>
counter = 1
MyFunction
f = <fn>
![Page 63: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/63.jpg)
Closuresfunction CreateCounter() { var counter = 0; return function() { counter ++; return counter; }}
function MyFunction() { f = CreateCounter(); print(f()); print(f());}
> 1
CreateCounter
<fn>
counter = 1
<fn>
MyFunction
f = <fn>
![Page 64: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/64.jpg)
Closuresfunction CreateCounter() { var counter = 0; return function() { counter ++; return counter; }}
function MyFunction() { f = CreateCounter(); print(f()); print(f());}
> 1
CreateCounter
<fn>
counter = 2
<fn>
MyFunction
f = <fn>
![Page 65: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/65.jpg)
Closuresfunction CreateCounter() { var counter = 0; return function() { counter ++; return counter; }}
function MyFunction() { f = CreateCounter(); print(f()); print(f());}
> 1 2
CreateCounter
<fn>
counter = 2
<fn>
MyFunction
f = <fn>
![Page 66: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/66.jpg)
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.
![Page 67: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/67.jpg)
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?)
![Page 68: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/68.jpg)
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?
![Page 69: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/69.jpg)
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
![Page 70: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/70.jpg)
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
![Page 71: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/71.jpg)
Coroutines
![Page 72: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/72.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
![Page 73: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/73.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
>
![Page 74: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/74.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
>
![Page 75: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/75.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
>
![Page 76: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/76.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
>
downFrom
n = 3
![Page 77: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/77.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
>
downFrom
n = 3
![Page 78: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/78.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
>
downFrom
n = 3
![Page 79: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/79.jpg)
Coroutinesdef 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
![Page 80: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/80.jpg)
Coroutinesdef 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
![Page 81: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/81.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
i = 3
> 3
downFrom
n = 3
![Page 82: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/82.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
i = 3
> 3
downFrom
n = 3
![Page 83: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/83.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
i = 3
> 3
downFrom
n = 3
![Page 84: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/84.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
i = 3
> 3
downFrom
n = 2
![Page 85: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/85.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
i = 3
> 3
downFrom
n = 2
![Page 86: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/86.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
i = 3
> 3
downFrom
n = 2
![Page 87: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/87.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
i = 2
> 3
downFrom
n = 2
![Page 88: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/88.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
i = 2
> 3
downFrom
n = 2
![Page 89: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/89.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
i = 2
> 3 2
downFrom
n = 2
![Page 90: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/90.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
i = 2
> 3 2
downFrom
n = 2
![Page 91: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/91.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
i = 2
> 3 2
downFrom
n = 2
![Page 92: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/92.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
i = 2
> 3 2
downFrom
n = 1
![Page 93: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/93.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
i = 2
> 3 2
downFrom
n = 1
![Page 94: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/94.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
i = 2
> 3 2
downFrom
n = 1
![Page 95: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/95.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
i = 1
> 3 2
downFrom
n = 1
![Page 96: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/96.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
i = 1
> 3 2
downFrom
n = 1
![Page 97: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/97.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
i = 1
> 3 2 1
downFrom
n = 1
![Page 98: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/98.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
i = 1
> 3 2 1
downFrom
n = 1
![Page 99: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/99.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
i = 1
> 3 2 1
downFrom
n = 1
![Page 100: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/100.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
i = 1
> 3 2 1
downFrom
n = 0
![Page 101: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/101.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
i = 1
> 3 2 1
downFrom
n = 0
![Page 102: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/102.jpg)
Coroutinesdef downFrom(n): while n > 0: yield n n = n – 1
def myFunc(): for i in downFrom(3): print i
myFunc
i = 1
> 3 2 1
downFrom
n = 0
![Page 103: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/103.jpg)
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.
![Page 104: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/104.jpg)
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).
![Page 105: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/105.jpg)
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!
![Page 106: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/106.jpg)
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?
![Page 107: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/107.jpg)
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.
![Page 108: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/108.jpg)
A Logical Decaf Stack Frame
Param N
Param N – 1
...
Param 1
Storage for Locals and Temporaries
Stack frame for function f(a, …, n)
![Page 109: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/109.jpg)
A Logical Decaf Stack Frame
Param N
Param N – 1
...
Param 1
Storage for Locals and Temporaries
Stack frame for function f(a, …, n)
Param M
![Page 110: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/110.jpg)
A Logical Decaf Stack Frame
Param N
Param N – 1
...
Param 1
Storage for Locals and Temporaries
Stack frame for function f(a, …, n)
Param M
…
![Page 111: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/111.jpg)
A Logical Decaf Stack Frame
Param N
Param N – 1
...
Param 1
Storage for Locals and Temporaries
Stack frame for function f(a, …, n)
Param M
…
Param 1
![Page 112: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/112.jpg)
A Logical Decaf Stack Frame
Param N
Param N – 1
...
Param 1
Storage for Locals and Temporaries
Stack frame for function f(a, …, n)
Param M
…
Param 1
Storage for Locals and Temporaries
![Page 113: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/113.jpg)
A Logical Decaf Stack Frame
Param N
Param N – 1
...
Param 1
Storage for Locals and Temporaries
Stack frame for function f(a, …, n)
Param M
…
Param 1
Storage for Locals and Temporaries
Stack frame for function g(a, …, m)
![Page 114: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/114.jpg)
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?)
![Page 115: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/115.jpg)
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.
![Page 116: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/116.jpg)
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?
![Page 117: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/117.jpg)
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?
![Page 118: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/118.jpg)
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!
![Page 119: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/119.jpg)
0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 600
2
4
6
8
10
12
Midterm ScoresF
req
ue
ncy
Mean: 38Median: 40Stdev: 15.3
![Page 120: 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](https://reader035.vdocument.in/reader035/viewer/2022071217/6049519d9d64e8570b418bf5/html5/thumbnails/120.jpg)
Next Time
● Object layouts● The limits of standard vtables● Inline caching● Prototypical inheritance (ITA)
● Three-address code IR.● Generating TAC.