Compiler Construction Lecture 05 A Simple Stack Machine. Lent Term, Lecturer: Timothy G. Griffin. Computer Laboratory University of Cambridge

Similar documents
CA Compiler Construction

Topic 7: Activation Records

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

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

Code Generation. Lecture 19

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

Compilers and computer architecture: A realistic compiler to MIPS

Compiler Construction Lent Term 2015 Lectures 10, 11 (of 16)

Code Generation. Lecture 12

Run-time Environment

6.001 Recitation 23: Register Machines and Stack Frames

G Programming Languages - Fall 2012

Scope, Functions, and Storage Management

Midterm II CS164, Spring 2006

Review of Activation Frames. FP of caller Y X Return value A B C

Compilation 2014 Activation Records (Part 1)

Course Administration

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

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

Typical Runtime Layout. Tiger Runtime Environments. Example: Nested Functions. Activation Trees. code. Memory Layout

The Activation Record (AR)

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

CS153: Compilers Lecture 8: Compiling Calls

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

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

Code Generation II. Code generation for OO languages. Object layout Dynamic dispatch. Parameter-passing mechanisms Allocating temporaries in the AR

What is SaM? SaM is a simple stack machine designed to introduce you to compilers in a few lectures SaM I: written by Dr. Keshav Pingali around 2000

Lecture08: Scope and Lexical Address

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

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

ECE232: Hardware Organization and Design

Compilation 2014 Activation Records

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

CSE Lecture In Class Example Handout

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

Procedures and Stacks

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

Operational Semantics. One-Slide Summary. Lecture Outline

Functions & First Class Function Values

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

We ve written these as a grammar, but the grammar also stands for an abstract syntax tree representation of the IR.

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

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

MIPS Procedure Calls. Lecture 6 CS301

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

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

CS415 Compilers. Procedure Abstractions. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Code Generation Super Lectures

Programming Languages

CS 6353 Compiler Construction Project Assignments

CS 314 Principles of Programming Languages. Lecture 13

Operational Semantics of Cool

Datatype declarations

CS64 Week 5 Lecture 1. Kyle Dewey

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types

CSE 341: Programming Languages

Functions in C. Memory Allocation in C. C to LC3 Code generation. Next.. Complete and submit C to LC3 code generation. How to handle function calls?

Announcements. EE108B Lecture MIPS Assembly Language III. MIPS Machine Instruction Review: Instruction Format Summary

Quiz for Chapter 2 Instructions: Language of the Computer3.10

CSE 341 Section 5. Winter 2018

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

MIPS Functions and Instruction Formats

CMa simple C Abstract Machine

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

LECTURE 16. Functional Programming

Virtual Machine. Part II: Program Control. Building a Modern Computer From First Principles.

CSC 2400: Computing Systems. X86 Assembly: Function Calls

CS 61A Higher Order Functions and Recursion Fall 2018 Discussion 2: June 26, Higher Order Functions. HOFs in Environment Diagrams

Exercise 1 ( = 18 points)

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Lexical Considerations

Functions in MIPS. Functions in MIPS 1

1 Lexical Considerations

Assembly Language: Function Calls

System Software Assignment 1 Runtime Support for Procedures

ECE260: Fundamentals of Computer Engineering

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

CS 316: Procedure Calls/Pipelining

Lecture 2: SML Basics

Forward recursion. CS 321 Programming Languages. Functions calls and the stack. Functions calls and the stack

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

! What do we care about? n Fast program execution. n Efficient memory usage. n Avoid memory fragmentation. n Maintain data locality

Storage in Programs. largest. address. address

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

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

ENEE 457: Computer Systems Security. Lecture 16 Buffer Overflow Attacks

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

Programming Languages and Compilers (CS 421)

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

Control Instructions

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

This section provides some reminders and some terminology with which you might not be familiar.

Functional Programming. Pure Functional Programming

Run-Time Environments

Principles of Programming Languages

Lexical Considerations

Run-time Environments - 2

User-defined Functions. Conditional Expressions in Scheme

Transcription:

Compiler Construction Lecture 05 A Simple Stack Machine Lent Term, 2015 Lecturer: Timothy G. Griffin Computer Laboratory University of Cambridge 1

Where are we going? When we derived the stack machine from the expression evaluator, we really knew where we were going --- to a simple stack machine with a simple compiler for reverse Polish notation. (Well, at least I knew that.) Let s pause to think about what the stack machine target of our Slang.1 derivation might look like. Today, we will consider only the simple case : simple functions with NO nesting.

Caller and Callee! fun f (x, y) = e1!!!! fun g(w, v) =! w + f(v, v)!! For this invocation of the function f, we say that g is the caller while f is the callee Recursive functions can play both roles at the same time

A word about dynamic binding --- IT IS A VERY BAD IDEA! let val x = 1! fun g(y) = x + y! fun h(x) = g(x) + 1! in! h(17)! end! With good old static binding we get 19. With insane dynamic binding we get 35. But might there be a place for dynamic binding? Is there dynamic binding of some kind behind the raise/handle exception mechanism?

Jargon Virtual Machine grows stack pointer shrinks Four ecial-purpose registers frame pointer fp Stack Code pointer Instructions Status register sr status 5

halt j : stop halt j : stop sr 0 sr 1 Status codes 0 = running 1 = stopped (program answer should be on top of stack) 2 = store index out of bounds 3 = call out of bounds 4 = stack overflow 5 = fp offset out of bounds 6 = return out of bounds 7 = arith error (div by 0) 6

Top-of-Stack arithmetic push value value stack value 2 value 1 arith op result = (value 1) op (value 2) result Op in { +, *, -, /, <, >, <=, >=, =, &&, } 7

Translation of expressions e1 op e2 code for e1 code for e2 arith op 3 * ((8 + 17) * (2-6)) 0 : push 3 1 : push 8 2 : push 17 3 : arith + 4 : push 2 5 : push 6 6 : arith - 7 : arith * 8 : arith * 8

goto, skip j : goto k goto k k :.. k :.. (set status to an error code if k is not in range ) skip j : skip j+1 :.. j : skip j+1 :.. 9

test j : test k test k j+1 : If VALUE is TRUE j+1 : k :.. k :.. If VALUE is FALSE VALUE stack stack 10

Conditionals, Loops If e then c1 else c2 while e { c } code for e m: code for e test k code for c1 goto m k: code for c2 test k code for c goto m k: skip m: skip 11

How do we organize the call stack? let rec fib m = if m = 0 then 1 else if m = 1 then 1 else fib(m - 1) + fib (m - 2) List.map fib [0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10];; = [1; 1; 2; 3; 5; 8; 13; 21; 34; 55; 89] fib(0) 1 fib(1) 1 1 1 2 fib(1) 1 fib(2) fib(2) fib(2) fib(2) fib(2) fib(2) 2 2 2 3 3 What information does the call stack contain? Does the answer depend on the language implemented? Yes! 12

First : Assume simple functions with NO nesting stack pointer Stack[] = next available slot at top of stack Callee stack frame (activation record) Stack[fp + 1] contains return address (RA) Stack[fp] contains the fp of the caller s frame Caller stack frame (activation record) RA FP Optional reserved ace Stack[fp + 2] to Stack[fp + k] (perhaps for values of local variables) fp frame pointer Stack[fp - 1] to Stack[fp - n] are arguments passed by caller 13

We can now design high level VSM commands j : call f f :.. Code call f fp j : call f f :.. Code j+1 This is a VM-level abstraction. At the level of the program implementing the VM call is implemented with many instructions. If we are targeting an OS/ISA pair (Unix/x86, ), then there are many more options as to who (caller or callee) does what when and where (registers or stack). This is captured in a Calling Convention. caller s frame fp 14

Return Code return n ra :.. Code return n ra :.. return value return n fp ra n args return value fp 1

Access to argument values arg j V fp ra fp ra fp - j V

Translation of (call-by-value) functions f(e_1,, e_n) fun f(x_1,, x_n) = e code for e_1 : code for e_n : call k This will leave the values of each arg on the stack, with the value of e_n at the top. Here k is the address for the start of the code for f. k : code for e return n k is a location (address) where code for function f starts. In code for e, access to variable x_i is translated to arg ((n i) + 1). 17

simple expressions e Code to leave the value of e on top of the stack constant j = (n i) + 1 where x is the i-th formal parameter (from left to right) c push c x arg j

What if we allow nested functions? fun g(x) = fun h(y) = e1 in e2 end g(17) an h stack frame from call to h in e2 : : : : : : g s stack frame 17 How will the code generated from e1 find the value of x? 19

Approach 1: Lambda Lifting fun g(x) = fun h(y) = e1 in e2 end g(17) fun h(y, x) = e1 fun g(x) = e3 g(17) Construct e3 from e2 by replacing each call h(e) with h(e, x) (+) Keeps our VM simple (+) Low variable access cost (-) can duplicate many arg values on the stack 20