CS 4110 Programming Languages & Logics

Size: px
Start display at page:

Download "CS 4110 Programming Languages & Logics"

Transcription

1 CS 4110 Programming Languages & Logics Lecture 38 Typed Assembly Language 30 November 2012

2 Schedule 2 Monday Typed Assembly Language Wednesday Polymorphism Stack Types Today Compilation Course Review

3 Certi ed Compilation 3 An automatic way to generate certi able low-level code Type safety implies memory, security properties

4 Certi ed Compilation 3 An automatic way to generate certi able low-level code Type safety implies memory, security properties Tiny Source language

5 Certi ed Compilation 3 An automatic way to generate certi able low-level code Type safety implies memory, security properties Tiny Source language Typed Assembly Language Compiler target

6 Certi ed Compilation 3 An automatic way to generate certi able low-level code Type safety implies memory, security properties Tiny Source language Typed Assembly Language Compiler target Optimized Typed Assembly Language Optimizer target

7 The Tiny Language 4 Features Integer expressions Conditionals Recursive functions Function pointers (but no closures) A strong, static type system

8 The Tiny Language 4 Features Integer expressions Conditionals Recursive functions Function pointers (but no closures) A strong, static type system Example program letrec fun fact (n:int) : int = if n = 0 then 1 else n * fact (n 1) in fact (42)

9 Tiny Type System 5 Expressions Φ x : Φ(x)

10 Tiny Type System 5 Expressions Φ x : Φ(x) Φ f : Φ(f)

11 Tiny Type System 5 Expressions Φ x : Φ(x) Φ f : Φ(f) Φ n : int

12 Tiny Type System 5 Expressions Φ x : Φ(x) Φ f : Φ(f) Φ n : int Φ e 1 : int Φ e 2 : int Φ e 1 + e 2 : int

13 Tiny Type System 5 Expressions Φ x : Φ(x) Φ f : Φ(f) Φ n : int Φ e 1 : int Φ e 1 + e 2 : int Φ e 2 : int Φ e 1 : τ 1 τ 2 Φ e 2 : τ 1 Φ e 1 e 2 : τ 2

14 Tiny Type System 5 Expressions Φ x : Φ(x) Φ f : Φ(f) Φ n : int Φ e 1 : int Φ e 1 + e 2 : int Φ e 2 : int Φ e 1 : τ 1 τ 2 Φ e 2 : τ 1 Φ e 1 e 2 : τ 2 Φ e 1 : int Φ e 2 : τ Φ e 3 : τ Φ if e 1 = 0 then e 2 else e 3 : τ

15 Tiny Type System 5 Expressions Φ x : Φ(x) Φ f : Φ(f) Φ n : int Φ e 1 : int Φ e 1 + e 2 : int Φ e 2 : int Φ e 1 : τ 1 τ 2 Φ e 2 : τ 1 Φ e 1 e 2 : τ 2 Φ e 1 : int Φ e 2 : τ Φ e 3 : τ Φ if e 1 = 0 then e 2 else e 3 : τ Φ e 1 : τ 1 Φ, x : τ 1 e 2 : τ 2 Φ let x = e 1 in e 2 : τ 1

16 Tiny Type System 6 Declarations Φ, x : τ 1 e : τ 2 Φ fun f(x : τ 1 ) : τ 2 = e : (f : τ 1 τ 2 )

17 Tiny Type System 6 Declarations Φ, x : τ 1 e : τ 2 Φ fun f(x : τ 1 ) : τ 2 = e : (f : τ 1 τ 2 ) Programs Φ = f 1 : τ 11 τ 12,, f k : τ k1 τ k2 Φ d i : (f i : τ i1 τ i2 ) Φ e : int letrec d 1 d k in e

18 Tiny Type System 6 Declarations Φ, x : τ 1 e : τ 2 Φ fun f(x : τ 1 ) : τ 2 = e : (f : τ 1 τ 2 ) Programs Φ = f 1 : τ 11 τ 12,, f k : τ k1 τ k2 Φ d i : (f i : τ i1 τ i2 ) Φ e : int letrec d 1 d k in e Exercise: Chat that fact is well typed

19 Type-preserving Compilation 7 Most compilers consist of a series of transformations In our compiler, each step will propagate types A transformation consists of two sub-translations: From source types to target types From source terms to target terms After each step, can typecheck the intermediate output code to detect errors in the compiler Key correctness property will be that transformations preserve types (as well as semantics)

20 Type Translation 8 T [[ ]] maps Tiny types to TAL Types

21 Type Translation 8 T [[ ]] maps Tiny types to TAL Types Integers: T [[int]] = int

22 Type Translation 8 T [[ ]] maps Tiny types to TAL Types Integers: T [[int]] = int Functions: Caller pushes argument and return address on stack Callee pops the return address and argument and places result in ra T [[τ 1 τ 2 ]] = ρ{sp : K[[τ 2, ρ]] :: T [[τ 1 ]] :: ρ} {} where K[[τ, σ]] = {sp : σ, r a : T [[τ]]} {} K[[τ]] = {r a : T [[τ]]} {}

23 Expression Translation 9 E[[ ]] maps Tiny expressions (really typing derivations) to labeled code blocks

24 Expression Translation 9 E[[ ]] maps Tiny expressions (really typing derivations) to labeled code blocks We use the stack extensively: To evaluate an expression, evaluate subexpressions then push them onto the stack Return nal value in ra M maps expression variables to stack offsets I(M) increments the offset associated with each variable in M Overall, uses just two registers, r a and r t, and the stack Shape of the translation is E[[e]] M,σ = J, where J is a sequence of typed, labeled blocks Write L f for the TAL label of function f

25 Expression Translation 10 Expression variables E[[x]] M,σ = sld r a, M(x)

26 Expression Translation 10 Expression variables E[[x]] M,σ = sld r a, M(x) Function variables E[[f]] M,σ = mov r a, L f

27 Expression Translation 10 Expression variables E[[x]] M,σ = sld r a, M(x) Function variables E[[f]] M,σ = mov r a, L f Integers E[[n]] M,σ = mov r a, n

28 Expression Translation 10 Expression variables E[[x]] M,σ = sld r a, M(x) Function variables E[[f]] M,σ = mov r a, L f Addition E[[e 1 + e 2 ]] M,σ = E[[e 1 ]] M,σ push r a E[[e 2 ]] I(M),int :: σ pop r t add r a, r t, r a Integers E[[n]] M,σ = mov r a, n

29 Expression Translation 11 Application E[[e 1 e 2 ]] M,σ = E[[e 1 ]] M,σ push r a E[[e 2 ]] I(M),T [[τ1 τ 2 ]] :: σ pop r t push r a push L r [ρ] jmp r t [σ] L r : ρ K[[τ 2, σ]] where Φ e 1 : τ 1 τ 2 and L r fresh

30 Expression Translation 12 Conditional E[[if e 1 = 0 then e 2 else e 3 ]] M,σ = E[[e 1 ]] M,σ bneq r a, L else [ρ] E[[e 2 ]] M,σ jmp L end [ρ] L else : ρ{sp : σ} {} E[[e 3 ]] M,σ jmp L end [ρ] L end : ρk[[τ, σ]] where Φ e 2 : τ and Φ e 2 : τ and L else and L end fresh

31 Declaration Translation 13 Function F[[fun f (x : τ 1 ) : τ 2 = e]] = L f : T [[τ 1 τ 2 ]] E[[e 2 ]] x := 2,K[[τ2,ρ]] :: T [[τ 1 ]] :: ρ pop r t sfree 1 jmp r t

32 Program Translation 14 Program P[[letrec d 1 d k in e]] = F[[d 1 ]] F[[d k ]] L main : ρ{sp : K[[int, ρ]] :: ρ} {} E[[e]],K[[int,ρ]] :: ρ pop r t jmp r t To run the program, push return address on stack and jump to L main Result is returned in r a

33 Factorial L fact : ρ {sp : K[[int]] :: int :: ρ} sld r a,2 bneq r a,l else [ρ] mov r a,1 jmp L end L else : ρ {sp : K[[int]] :: int :: ρ} sld r a,2 push r a mov r a,l fact push r a sld r a,4 push r a mov r a,1 pop r t sub r a,r t,r a pop r t push L r [ρ] jmp r t [int :: K[[int, ρ]] :: int :: ρ] 15

34 Factorial L fact : ρ {sp : K[[int]] :: int :: ρ} sld r a,2 bneq r a,l else [ρ] mov r a,1 jmp L end L else : ρ {sp : K[[int]] :: int :: ρ} sld r a,2 push r a mov r a,l fact push r a sld r a,4 push r a mov r a,1 pop r t sub r a,r t,r a pop r t push L r [ρ] jmp r t [int :: K[[int, ρ]] :: int :: ρ] L r : ρ {sp : K[[int]] :: int :: ρ, r a : int} pop r t mul r a,r t,r t jmp L end [ρ] L end : ρ {sp : K[[int]] :: int :: ρ, r a : int} pop r t sfree 1 jmp r t 15

35 Compiler properties 16 Theorem If P then there exists Ψ such that P[[P]] : Ψ Proof by induction on P Main idea: We don t have to trust the compiler because we can typecheck the assembly code emitted as output!

36 Optimizations Almost any compiler will produce better code than ours! (But how many compilers t on four slides?) The type system makes it possible to implement many standard optimizations to generate better code: Instruction selection and scheduling Register allocation Common subexpression elimination Redundant load/store elimination Strength reduction Loop-invariant removal Tail-calls and others Types do not interfere with the most common optimizations 17

37 Tail-call Optimization 18 A crucial optimization for functional languages Applies when the nal operation in a function f is a call to another function g Instead of having f push the return address onto the stack and engage in the normal calling sequence, f pops all of its temporary values, jumps directly to g and never returns!

38 Tail-call Optimization 19 Unoptimized code L f : ρ {sp : K[[τ, ρ]] :: τ f :: ρ, r a : τ g } {} salloc 2 % setup stack frame sst L r % push return address sst r a,2 % push argument jmp L g [τ :: τ f :: ρ] L r : ρ {sp : τ :: τ f :: ρ, r a : τ} {} pop r t % pop return address sfree 1 % discard f s argument jmp r t % return Optimized code L f : ρ {sp : K[[τ, ρ]] :: τ f :: ρ, r a : τ g } {} sst r a,2 jmp L g [ρ] % g returns to f s caller

39 Course Review

40

41 Mathematical Preliminaries & Operational Semantics

42 Mathematical Preliminaries & Operational Semantics Denotational & Axiomatic Semantics

43 Mathematical Preliminaries & Operational Semantics Denotational & Axiomatic Semantics λ-calculus

44 Mathematical Preliminaries & Operational Semantics Denotational & Axiomatic Semantics λ-calculus Preliminary Exam I

45 Mathematical Preliminaries & Operational Semantics Denotational & Axiomatic Semantics λ-calculus Preliminary Exam I Fall Break

46 Mathematical Preliminaries & Operational Semantics Denotational & Axiomatic Semantics Type Systems & Static Analysis λ-calculus Preliminary Exam I Fall Break

47 Mathematical Preliminaries & Operational Semantics Denotational & Axiomatic Semantics Type Systems & Static Analysis λ-calculus Preliminary Exam I Preliminary Exam II Fall Break

48 Mathematical Preliminaries & Operational Semantics Denotational & Axiomatic Semantics Type Systems & Static Analysis λ-calculus Preliminary Exam I Preliminary Exam II Advanced Topics Fall Break

49 21 Mathematical Preliminaries & Operational Semantics Denotational & Axiomatic Semantics Type Systems & Static Analysis λ-calculus Preliminary Exam I Preliminary Exam II Advanced Topics Fall Break Final Exam

50 Final Topics 22 Mathematical Preliminaries (inductive de nitions)

51 Final Topics 22 Mathematical Preliminaries (inductive de nitions) Semantics (operational, axiomatic, denotational)

52 Final Topics 22 Mathematical Preliminaries (inductive de nitions) Semantics (operational, axiomatic, denotational) λ-calculus (basics, encodings, extensions)

53 Final Topics 22 Mathematical Preliminaries (inductive de nitions) Semantics (operational, axiomatic, denotational) λ-calculus (basics, encodings, extensions) Type systems (simple, extensions, properties)

54 Final Topics 22 Mathematical Preliminaries (inductive de nitions) Semantics (operational, axiomatic, denotational) λ-calculus (basics, encodings, extensions) Type systems (simple, extensions, properties) Advanced topics (logic, security, concurrency, DSLs, TAL)

55 Final Topics 22 Mathematical Preliminaries (inductive de nitions) Semantics (operational, axiomatic, denotational) λ-calculus (basics, encodings, extensions) Type systems (simple, extensions, properties) Advanced topics (logic, security, concurrency, DSLs, TAL) Expect to solve probems just like the ones we ve seen throughout the course

56 Final Topics 22 Mathematical Preliminaries (inductive de nitions) Semantics (operational, axiomatic, denotational) λ-calculus (basics, encodings, extensions) Type systems (simple, extensions, properties) Advanced topics (logic, security, concurrency, DSLs, TAL) Expect to solve probems just like the ones we ve seen throughout the course and to apply the skills you ve acquired to new problems too!

57 Final Logistics 23 Date: Wednesday, December 12th Time: 7-9:30pm Where: Hollister 110 Practice: Available from Upson 4146 starting Monday Review: Sunday, December 9th?

58 Going further 24 Apply to attend ACM SIGPLAN PLMW

59 Going further 24 Apply to attend ACM SIGPLAN PLMW CS 6110 Advanced Programming Languages

60 Going further 24 Apply to attend ACM SIGPLAN PLMW CS 6110 Advanced Programming Languages CS 5114/6114 Network Programming Languages

61 Going further 24 Apply to attend ACM SIGPLAN PLMW CS 6110 Advanced Programming Languages CS 5114/6114 Network Programming Languages CS 7190 Seminar in Programming Languages

62 Going further 24 Apply to attend ACM SIGPLAN PLMW CS 6110 Advanced Programming Languages CS 5114/6114 Network Programming Languages CS 7190 Seminar in Programming Languages CS 4999 Independent Research

63 Going further 24 Apply to attend ACM SIGPLAN PLMW CS 6110 Advanced Programming Languages CS 5114/6114 Network Programming Languages CS 7190 Seminar in Programming Languages CS 4999 Independent Research Thank you, and stay in touch!

CS 4110 Programming Languages & Logics. Lecture 35 Typed Assembly Language

CS 4110 Programming Languages & Logics. Lecture 35 Typed Assembly Language CS 4110 Programming Languages & Logics Lecture 35 Typed Assembly Language 1 December 2014 Announcements 2 Foster Office Hours today 4-5pm Optional Homework 10 out Final practice problems out this week

More information

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

More information

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

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline. The Main Idea of Today s Lecture Code Generation We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

More information

Scope, Functions, and Storage Management

Scope, Functions, and Storage Management Scope, Functions, and Storage Management Implementing Functions and Blocks cs3723 1 Simplified Machine Model (Compare To List Abstract Machine) Registers Code Data Program Counter (current instruction)

More information

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

We can emit stack-machine-style code for expressions via recursion Code Generation The Main Idea of Today s Lecture We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

More information

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;} Subroutines Also called procedures or functions Example C code: int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;} // subroutine converts Celsius to kelvin int celtokel(int i) { return (i

More information

Compilers and computer architecture: A realistic compiler to MIPS

Compilers and computer architecture: A realistic compiler to MIPS 1 / 1 Compilers and computer architecture: A realistic compiler to MIPS Martin Berger November 2017 Recall the function of compilers 2 / 1 3 / 1 Recall the structure of compilers Source program Lexical

More information

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

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13 Run-time Environments Lecture 13 by Prof. Vijay Ganesh) Lecture 13 1 What have we covered so far? We have covered the front-end phases Lexical analysis (Lexer, regular expressions,...) Parsing (CFG, Top-down,

More information

G Programming Languages - Fall 2012

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

More information

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9 Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9 Ziad Matni Dept. of Computer Science, UCSB Lecture Outline More on MIPS Calling Convention Functions calling functions

More information

COMP2611: Computer Organization MIPS function and recursion

COMP2611: Computer Organization MIPS function and recursion COMP2611 Fall2015 COMP2611: Computer Organization MIPS function and recursion Overview 2 You will learn the following in this lab: how to use MIPS functions in a program; the concept of recursion; how

More information

We defined congruence rules that determine the order of evaluation, using the following evaluation

We defined congruence rules that determine the order of evaluation, using the following evaluation CS 4110 Programming Languages and Logics Lectures #21: Advanced Types 1 Overview In this lecture we will extend the simply-typed λ-calculus with several features we saw earlier in the course, including

More information

CSE Lecture In Class Example Handout

CSE 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 information

CS143 - Written Assignment 4 Reference Solutions

CS143 - Written Assignment 4 Reference Solutions CS143 - Written Assignment 4 Reference Solutions 1. Consider the following program in Cool, representing a slightly over-engineered implementation which calculates the factorial of 3 using an operator

More information

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary Control Instructions Computer Organization Architectures for Embedded Computing Thursday, 26 September 2013 Many slides adapted from: Computer Organization and Design, Patterson & Hennessy 4th Edition,

More information

Control Instructions

Control Instructions Control Instructions Tuesday 22 September 15 Many slides adapted from: and Design, Patterson & Hennessy 5th Edition, 2014, MK and from Prof. Mary Jane Irwin, PSU Summary Previous Class Instruction Set

More information

Course Administration

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

More information

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI. CSCI 402: Computer Architectures Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI Recall Big endian, little endian Memory alignment Unsigned

More information

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

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

More information

Lecture 5. Announcements: Today: Finish up functions in MIPS

Lecture 5. Announcements: Today: Finish up functions in MIPS Lecture 5 Announcements: Today: Finish up functions in MIPS 1 Control flow in C Invoking a function changes the control flow of a program twice. 1. Calling the function 2. Returning from the function In

More information

Code Generation. Lecture 12

Code Generation. Lecture 12 Code Generation Lecture 12 1 Lecture Outline Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation

More information

Lectures 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 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 information

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

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15 Stack Frames Geoffrey Brown Bryce Himebaugh Indiana University September 2, 2016 Geoffrey Brown, Bryce Himebaugh 2015 September 2, 2016 1 / 15 Outline Preserving Registers Saving and Restoring Registers

More information

Functions in MIPS. Functions in MIPS 1

Functions in MIPS. Functions in MIPS 1 Functions in MIPS We ll talk about the 3 steps in handling function calls: 1. The program s flow of control must be changed. 2. Arguments and return values are passed back and forth. 3. Local variables

More information

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

Topic 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 information

Mutable References. Chapter 1

Mutable References. Chapter 1 Chapter 1 Mutable References In the (typed or untyped) λ-calculus, or in pure functional languages, a variable is immutable in that once bound to a value as the result of a substitution, its contents never

More information

Operational Semantics of Cool

Operational Semantics of Cool Operational Semantics of Cool Lecture 23 Dr. Sean Peisert ECS 142 Spring 2009 1 Status Project 3 due on Friday Project 4 assigned on Friday. Due June 5, 11:55pm Office Hours this week Fri at 11am No Class

More information

CS153: Compilers Lecture 15: Local Optimization

CS153: Compilers Lecture 15: Local Optimization CS153: Compilers Lecture 15: Local Optimization Stephen Chong https://www.seas.harvard.edu/courses/cs153 Announcements Project 4 out Due Thursday Oct 25 (2 days) Project 5 out Due Tuesday Nov 13 (21 days)

More information

SPIM Procedure Calls

SPIM Procedure Calls SPIM Procedure Calls 22C:60 Jonathan Hall March 29, 2008 1 Motivation We would like to create procedures that are easy to use and easy to read. To this end we will discuss standard conventions as it relates

More information

COMP 181. Prelude. Intermediate representations. Today. High-level IR. Types of IRs. Intermediate representations and code generation

COMP 181. Prelude. Intermediate representations. Today. High-level IR. Types of IRs. Intermediate representations and code generation Prelude COMP 181 Lecture 14 Intermediate representations and code generation October 19, 2006 Who is Seth Lloyd? Professor of mechanical engineering at MIT, pioneer in quantum computing Article in Nature:

More information

Type checking. Jianguo Lu. November 27, slides adapted from Sean Treichler and Alex Aiken s. Jianguo Lu November 27, / 39

Type checking. Jianguo Lu. November 27, slides adapted from Sean Treichler and Alex Aiken s. Jianguo Lu November 27, / 39 Type checking Jianguo Lu November 27, 2014 slides adapted from Sean Treichler and Alex Aiken s Jianguo Lu November 27, 2014 1 / 39 Outline 1 Language translation 2 Type checking 3 optimization Jianguo

More information

CS64 Week 5 Lecture 1. Kyle Dewey

CS64 Week 5 Lecture 1. Kyle Dewey CS64 Week 5 Lecture 1 Kyle Dewey Overview More branches in MIPS Memory in MIPS MIPS Calling Convention More Branches in MIPS else_if.asm nested_if.asm nested_else_if.asm Memory in MIPS Accessing Memory

More information

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine Lecture Outline Code Generation Lecture 12 Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation

More information

CS 316: Procedure Calls/Pipelining

CS 316: Procedure Calls/Pipelining CS 316: Procedure Calls/Pipelining Kavita Bala Fall 2007 Computer Science Cornell University Announcements PA 3 IS out today Lectures on it this Fri and next Tue/Thu Due on the Friday after Fall break

More information

CS 701. Class Meets. Instructor. Teaching Assistant. Key Dates. Charles N. Fischer. Fall Tuesdays & Thursdays, 11:00 12: Engineering Hall

CS 701. Class Meets. Instructor. Teaching Assistant. Key Dates. Charles N. Fischer. Fall Tuesdays & Thursdays, 11:00 12: Engineering Hall CS 701 Charles N. Fischer Class Meets Tuesdays & Thursdays, 11:00 12:15 2321 Engineering Hall Fall 2003 Instructor http://www.cs.wisc.edu/~fischer/cs703.html Charles N. Fischer 5397 Computer Sciences Telephone:

More information

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons CS 61C: Great Ideas in Computer Architecture Strings and Func.ons Instructor: Krste Asanovic, Randy H. Katz hdp://inst.eecs.berkeley.edu/~cs61c/sp12 Fall 2012 - - Lecture #7 1 New- School Machine Structures

More information

Final Exam Review Questions

Final Exam Review Questions EECS 665 Final Exam Review Questions 1. Give the three-address code (like the quadruples in the csem assignment) that could be emitted to translate the following assignment statement. However, you may

More information

IR Lowering. Notation. Lowering Methodology. Nested Expressions. Nested Statements CS412/CS413. Introduction to Compilers Tim Teitelbaum

IR Lowering. Notation. Lowering Methodology. Nested Expressions. Nested Statements CS412/CS413. Introduction to Compilers Tim Teitelbaum IR Lowering CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 19: Efficient IL Lowering 7 March 07 Use temporary variables for the translation Temporary variables in the Low IR store intermediate

More information

The remote testing experiment. It works! Code Generation. Lecture 12. Remote testing. From the cs164 newsgroup

The remote testing experiment. It works! Code Generation. Lecture 12. Remote testing. From the cs164 newsgroup The remote testing experiment. It works! Coverage - Score plot 0.9 Code Generation 0.7 0.6 Lecture 12 Coverage 0.5 0.4 0.3 Series1 Poly. (Series1) 0.2 0.1 0 0 10 20 30 40 50 Score CS 164 Lecture 14 Fall

More information

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions. CS 6110 S18 Lecture 18 Denotational Semantics 1 What is Denotational Semantics? So far we have looked at operational semantics involving rules for state transitions, definitional semantics involving translations

More information

Code Generation. Lecture 19

Code Generation. Lecture 19 Code Generation Lecture 19 Lecture Outline Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation

More information

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4 EN164: Design of Computing Systems Lecture 11: Processor / ISA 4 Professor Sherief Reda http://scale.engin.brown.edu Electrical Sciences and Computer Engineering School of Engineering Brown University

More information

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 19: Efficient IL Lowering 5 March 08

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 19: Efficient IL Lowering 5 March 08 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 19: Efficient IL Lowering 5 March 08 CS 412/413 Spring 2008 Introduction to Compilers 1 IR Lowering Use temporary variables for the translation

More information

This Lecture. G53CMP: Lecture 14 Run-Time Organisation I. Example: Lifetime (1) Storage Areas

This Lecture. G53CMP: Lecture 14 Run-Time Organisation I. Example: Lifetime (1) Storage Areas This Lecture G53CMP: Lecture 14 Run-Time Organisation I Henrik Nilsson University of Nottingham, UK One aspect of run-time organisation: stack-based storage allocation Lifetime and storage Basic stack

More information

G53CMP: Lecture 14. Run-Time Organisation I. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 14 p.1/37

G53CMP: Lecture 14. Run-Time Organisation I. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 14 p.1/37 G53CMP: Lecture 14 Run-Time Organisation I Henrik Nilsson University of Nottingham, UK G53CMP: Lecture 14 p.1/37 This Lecture One aspect of run-time organisation: stack-based storage allocation Lifetime

More information

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m. CS 6110 S18 Lecture 8 Structural Operational Semantics and IMP Today we introduce a very simple imperative language, IMP, along with two systems of rules for evaluation called small-step and big-step semantics.

More information

Code Generation. Lecture 30

Code Generation. Lecture 30 Code Generation Lecture 30 (based on slides by R. Bodik) 11/14/06 Prof. Hilfinger CS164 Lecture 30 1 Lecture Outline Stack machines The MIPS assembly language The x86 assembly language A simple source

More information

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number The plot thickens Some MIPS instructions you can write cannot be translated to a 32-bit number some reasons why 1) constants are too big 2) relative addresses are too big 3) absolute addresses are outside

More information

Code Generation. Lecture 31 (courtesy R. Bodik) CS164 Lecture14 Fall2004 1

Code Generation. Lecture 31 (courtesy R. Bodik) CS164 Lecture14 Fall2004 1 Code Generation Lecture 31 (courtesy R. Bodik) CS164 Lecture14 Fall2004 1 Lecture Outline Stack machines The MIPS assembly language The x86 assembly language A simple source language Stack-machine implementation

More information

CS 61C: Great Ideas in Computer Architecture (Machine Structures) More MIPS Machine Language

CS 61C: Great Ideas in Computer Architecture (Machine Structures) More MIPS Machine Language CS 61C: Great Ideas in Computer Architecture (Machine Structures) More MIPS Machine Language Instructors: Randy H. Katz David A. PaGerson hgp://inst.eecs.berkeley.edu/~cs61c/sp11 1 2 Machine Interpreta4on

More information

Today. Putting it all together

Today. Putting it all together Today! One complete example To put together the snippets of assembly code we have seen! Functions in MIPS Slides adapted from Josep Torrellas, Craig Zilles, and Howard Huang Putting it all together! Count

More information

Programming Studio #9 ECE 190

Programming Studio #9 ECE 190 Programming Studio #9 ECE 190 Programming Studio #9 Concepts: Functions review 2D Arrays GDB Announcements EXAM 3 CONFLICT REQUESTS, ON COMPASS, DUE THIS MONDAY 5PM. NO EXTENSIONS, NO EXCEPTIONS. Functions

More information

Optimization Prof. James L. Frankel Harvard University

Optimization Prof. James L. Frankel Harvard University Optimization Prof. James L. Frankel Harvard University Version of 4:24 PM 1-May-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Reasons to Optimize Reduce execution time Reduce memory

More information

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

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015 Branch Addressing Branch instructions specify Opcode, two registers, target address Most branch targets are near branch Forward or backward op rs rt constant or address 6 bits 5 bits 5 bits 16 bits PC-relative

More information

EE 361 University of Hawaii Fall

EE 361 University of Hawaii Fall C functions Road Map Computation flow Implementation using MIPS instructions Useful new instructions Addressing modes Stack data structure 1 EE 361 University of Hawaii Implementation of C functions and

More information

Implementing Subprograms

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

More information

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

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

More information

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

CSC 2400: Computing Systems. X86 Assembly: Function Calls CSC 24: Computing Systems X86 Assembly: Function Calls" 1 Lecture Goals! Challenges of supporting functions" Providing information for the called function" Function arguments and local variables" Allowing

More information

ECE232: Hardware Organization and Design

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

More information

The Activation Record (AR)

The Activation Record (AR) The Activation Record (AR) [Notes adapted from R. Bodik] Code for function calls and function definitions depends on the layout of the activation record Very simple AR suffices for this language: The result

More information

Sémantique des Langages de Programmation (SemLP) DM : Region Types

Sémantique des Langages de Programmation (SemLP) DM : Region Types Sémantique des Langages de Programmation (SemLP) DM : Region Types I) Submission Submission Date : 21/05/2017 Submission Format : Submit a virtual machine (.ova) 1 with 1. an executable of the interpreter,

More information

COL728 Minor2 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

COL728 Minor2 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20 COL728 Minor2 Exam Compiler Design Sem II, 2017-18 Answer all 5 questions Max. Marks: 20 1. Short questions a. Give an example of a program that is not a legal program if we assume static scoping, but

More information

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls Princeton University Computer Science 217: Introduction to Programming Systems Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems x86-64 solutions Pertinent

More information

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

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

More information

Operational Semantics. One-Slide Summary. Lecture Outline

Operational Semantics. One-Slide Summary. Lecture Outline Operational Semantics #1 One-Slide Summary Operational semantics are a precise way of specifying how to evaluate a program. A formal semantics tells you what each expression means. Meaning depends on context:

More information

MIPS Functions and Instruction Formats

MIPS Functions and Instruction Formats MIPS Functions and Instruction Formats 1 The Contract: The MIPS Calling Convention You write functions, your compiler writes functions, other compilers write functions And all your functions call other

More information

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam Compilers Intermediate representations and code generation Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Today Intermediate representations and code generation Scanner Parser Semantic

More information

Lecture 5: Procedure Calls

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

More information

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus CS 4110 Programming Languages & Logics Lecture 17 Programming in the λ-calculus 10 October 2014 Announcements 2 Foster Office Hours 11-12 Enjoy fall break! Review: Church Booleans 3 We can encode TRUE,

More information

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number The plot thickens Some MIPS instructions you can write cannot be translated to a 32-bit number some reasons why 1) constants are too big 2) relative addresses are too big 3) absolute addresses are outside

More information

MIPS Procedure Calls. Lecture 6 CS301

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

More information

MIPS Assembly (FuncDons)

MIPS Assembly (FuncDons) ECPE 170 Jeff Shafer University of the Pacific MIPS Assembly (FuncDons) 2 Lab Schedule AcDviDes Monday Discuss: MIPS FuncDons Lab 11 Assignments Due Sunday Apr 14 th Lab 11 due by 11:59pm Wednesday Discuss:

More information

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention Subroutines Why we use subroutines more modular program (small routines, outside data passed in) more readable, easier to debug code reuse i.e. smaller code space Memory Usage A software convention stack

More information

1 Introduction. 3 Syntax

1 Introduction. 3 Syntax CS 6110 S18 Lecture 19 Typed λ-calculus 1 Introduction Type checking is a lightweight technique for proving simple properties of programs. Unlike theorem-proving techniques based on axiomatic semantics,

More information

CS4215 Programming Language Implementation

CS4215 Programming Language Implementation CS4215 Programming Language Implementation You have 45 minutes to complete the exam. Use a B2 pencil to fill up the provided MCQ form. Leave Section A blank. Fill up Sections B and C. After finishing,

More information

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

IR Generation. May 13, Monday, May 13, 13 IR Generation May 13, 2013 Monday, May 13, 13 Monday, May 13, 13 Monday, May 13, 13 Midterm Results Grades already in Repeal policy Talk to TA by the end of the Tuesday reserve the right to regrade the

More information

Instruction Set Architecture

Instruction Set Architecture Computer Architecture Instruction Set Architecture Lynn Choi Korea University Machine Language Programming language High-level programming languages Procedural languages: C, PASCAL, FORTRAN Object-oriented

More information

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018 Using the MIPS Calling Convention Recursive Functions in Assembly CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018 Ziad Matni, Ph.D. Dept. of Computer Science, UCSB Administrative Lab

More information

CS153: Compilers Lecture 8: Compiling Calls

CS153: Compilers Lecture 8: Compiling Calls CS153: Compilers Lecture 8: Compiling Calls Stephen Chong https://www.seas.harvard.edu/courses/cs153 Announcements Project 2 out Due Thu Oct 4 (7 days) Project 3 out Due Tuesday Oct 9 (12 days) Reminder:

More information

Lecture Outline. Code Generation. Lecture 30. Example of a Stack Machine Program. Stack Machines

Lecture Outline. Code Generation. Lecture 30. Example of a Stack Machine Program. Stack Machines Lecture Outline Code Generation Lecture 30 (based on slides by R. Bodik) Stack machines The MIPS assembly language The x86 assembly language A simple source language Stack-machine implementation of the

More information

Run-time Environment

Run-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 information

CSE505, Fall 2012, Midterm Examination October 30, 2012

CSE505, Fall 2012, Midterm Examination October 30, 2012 CSE505, Fall 2012, Midterm Examination October 30, 2012 Rules: The exam is closed-book, closed-notes, except for one side of one 8.5x11in piece of paper. Please stop promptly at Noon. You can rip apart

More information

CS 406/534 Compiler Construction Putting It All Together

CS 406/534 Compiler Construction Putting It All Together CS 406/534 Compiler Construction Putting It All Together Prof. Li Xu Dept. of Computer Science UMass Lowell Fall 2004 Part of the course lecture notes are based on Prof. Keith Cooper, Prof. Ken Kennedy

More information

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions Instructor: Sören Schwertfeger http://shtech.org/courses/ca/ School of Information Science and Technology SIST ShanghaiTech University

More information

Language Techniques for Provably Safe Mobile Code

Language Techniques for Provably Safe Mobile Code Language Techniques for Provably Safe Mobile Code Frank Pfenning Carnegie Mellon University Distinguished Lecture Series Computing and Information Sciences Kansas State University October 27, 2000 Acknowledgments:

More information

CSCI 334: Principles of Programming Languages. Computer Architecture (a really really fast introduction) Lecture 11: Control Structures II

CSCI 334: Principles of Programming Languages. Computer Architecture (a really really fast introduction) Lecture 11: Control Structures II 1 byte{ 1 byte{ CSCI 334: Principles of Programming Languages Lecture 11: Control Structures II Computer Architecture (a really really fast introduction) Instructor: Dan Barowy Memory Instructions main

More information

Midterm II CS164, Spring 2006

Midterm II CS164, Spring 2006 Midterm II CS164, Spring 2006 April 11, 2006 Please read all instructions (including these) carefully. Write your name, login, SID, and circle the section time. There are 10 pages in this exam and 4 questions,

More information

Assembly Language: Function Calls

Assembly Language: Function Calls Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems: Calling and returning Passing parameters Storing local variables Handling registers without interference

More information

CODE GENERATION Monday, May 31, 2010

CODE GENERATION Monday, May 31, 2010 CODE GENERATION memory management returned value actual parameters commonly placed in registers (when possible) optional control link optional access link saved machine status local data temporaries A.R.

More information

Advanced C Programming

Advanced C Programming Advanced C Programming Compilers Sebastian Hack hack@cs.uni-sb.de Christoph Weidenbach weidenbach@mpi-inf.mpg.de 20.01.2009 saarland university computer science 1 Contents Overview Optimizations Program

More information

Operational Semantics of Cool

Operational Semantics of Cool Operational Semantics of Cool Lecture 22 Dr. Sean Peisert ECS 142 Spring 2009 1 Status Project 3 due on Friday Project 4 assigned on Friday. Due June 5, 11:55pm Office Hours this week are Wed at 4pm and

More information

Systems I. Machine-Level Programming V: Procedures

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

More information

Function Calls. Tom Kelliher, CS 220. Oct. 24, SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how.

Function Calls. Tom Kelliher, CS 220. Oct. 24, SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how. Function Calls Tom Kelliher, CS 220 Oct. 24, 2011 1 Administrivia Announcements Assignment SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how. From Last Time Outline 1.

More information

Reminder of the last lecture. Aliasing Issues: Call by reference, Pointer programs. Introducing Aliasing Issues. Home Work from previous lecture

Reminder of the last lecture. Aliasing Issues: Call by reference, Pointer programs. Introducing Aliasing Issues. Home Work from previous lecture Reminder of the last lecture Aliasing Issues: Call by reference, Pointer programs Claude Marché Cours MPRI 2-36-1 Preuve de Programme 18 janvier 2017 Additional features of the specification language Abstract

More information

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

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

More information

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls Goals of this Lecture Assembly Language: Function Calls" 1 Goals of this Lecture" Help you learn:" Function call problems:" Calling and returning" Passing parameters" Storing local variables" Handling registers without interference"

More information

Summary: Direct Code Generation

Summary: Direct Code Generation Summary: Direct Code Generation 1 Direct Code Generation Code generation involves the generation of the target representation (object code) from the annotated parse tree (or Abstract Syntactic Tree, AST)

More information

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

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

More information

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far Lecture Outline Operational Semantics of Cool Lecture 13 COOL operational semantics Motivation Notation The rules Prof. Aiken CS 143 Lecture 13 1 Prof. Aiken CS 143 Lecture 13 2 Motivation We must specify

More information

Introduction to Code Optimization. Lecture 36: Local Optimization. Basic Blocks. Basic-Block Example

Introduction to Code Optimization. Lecture 36: Local Optimization. Basic Blocks. Basic-Block Example Lecture 36: Local Optimization [Adapted from notes by R. Bodik and G. Necula] Introduction to Code Optimization Code optimization is the usual term, but is grossly misnamed, since code produced by optimizers

More information