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

Similar documents
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

Code Generation. Lecture 30

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

Code Generation. Lecture 12

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

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

Code Generation Super Lectures

Compilers and computer architecture: A realistic compiler to MIPS

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

The Activation Record (AR)

Course Administration

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

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

Code Generation Super Lectures

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

CA Compiler Construction

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

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

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

CSE Lecture In Class Example Handout

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

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

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

Functions in MIPS. Functions in MIPS 1

Chapter 2A Instructions: Language of the Computer

CS 316: Procedure Calls/Pipelining

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

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

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

Control Instructions

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

Lecture 5: Procedure Calls

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

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

CSE Lecture In Class Example Handout

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer

MIPS Functions and Instruction Formats

ECE232: Hardware Organization and Design

ECE260: Fundamentals of Computer Engineering

Instruction Set Architectures (4)

CS 61c: Great Ideas in Computer Architecture

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

Implementing Procedure Calls

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

Computer Architecture. The Language of the Machine

Computer Architecture

Compiling Code, Procedures and Stacks

MIPS%Assembly% E155%

Code Generation & Parameter Passing

Chapter 2: Instructions:

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

Today. Putting it all together

MIPS Functions and the Runtime Stack

Procedures and Stacks

Lecture #31: Code Generation

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

SPIM Procedure Calls

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

ECE 331 Hardware Organization and Design. Professor Jay Taneja UMass ECE - Discussion 3 2/8/2018

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

CS153: Compilers Lecture 8: Compiling Calls

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

EE 361 University of Hawaii Fall

ECE331: Hardware Organization and Design

Instruction Set Architecture

CS64 Week 5 Lecture 1. Kyle Dewey

MIPS Procedure Calls. Lecture 6 CS301

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions

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

CS3350B Computer Architecture MIPS Introduction

Lecture 5: Procedure Calls

CS61C : Machine Structures

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

Storage in Programs. largest. address. address

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

Lecture 7: Procedures

Instructions: Assembly Language

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

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

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

CSCE 5610: Computer Architecture

Anne Bracy CS 3410 Computer Science Cornell University

MIPS Assembly (Functions)

Function Calls. 1 Administrivia. Tom Kelliher, CS 240. Feb. 13, Announcements. Collect homework. Assignment. Read

ECE 30 Introduction to Computer Engineering

CENG3420 Lecture 03 Review

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

Lecture 4: MIPS Instruction Set

Lectures 3-4: MIPS instructions

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure

Lecture #19: Code Generation

Intermediate Languages and Machine Languages. Lecture #31: Code Generation. Stack Machine with Accumulator. Stack Machines as Virtual Machines

CS61C : Machine Structures

Functions and Procedures

MIPS Procedure Calls - Review

Transcription:

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? The MIPS assembly language A simple source language ( Mini Bar ) A stack machine implementation of the simple language Stack Machines A simple evaluation model No variables or registers A stack of values for intermediate results Each instruction: Takes its operands from the top of the stack Removes those operands from the stack Computes the required operation on them Pushes the result onto the stack 3 4

Example of Stack Machine Operation The addition operation on a stack machine 5 5 9 9 pop add push 12 9 Example of a Stack Machine Program Consider two instructions push i - place the integer i on top of the stack add - pop topmost two elements, add them and put the result back onto the stack A program to compute + 5: push push 5 add 5 6 Why Use a Stack Machine? Each operation takes operands from the same place and puts results in the same place This means a uniform compilation scheme And therefore a simpler compiler Why Use a Stack Machine? Location of the operands is implicit Always on the top of the stack No need to specify operands explicitly No need to specify the location of the result Instruction is add as opposed to add r 1, r 2 (or add r d r i1 r i2 ) Smaller encoding of instructions More compact programs This is one of the reasons why Java Bytecode uses a stack evaluation model 8

Optimizing the Stack Machine The add instruction does 3 memory operations Two reads and one write to the stack The top of the stack is frequently accessed Idea: keep the top of the stack in a dedicated register (called the accumulator ) Register accesses are faster (why?) The add instruction is now acc acc + top_of_stack Only one memory operation! Stack Machine with Accumulator Invariants The result of computing an expression is always placed in the accumulator For an operation op(e 1,,e n ) compute each e i and then push the accumulator (= the result of evaluating e i ) onto the stack After the operation pop n-1 values After computing an expression the stack is as before 9 10 Stack Machine with Accumulator: Example Compute + 5 using an accumulator A Bigger Example: 3 + ( + 5) Code Acc Stack acc stack acc push acc acc 5 5 12 acc acc + top_of_stack pop acc 3 3 <init> push acc 3 3, <init> acc 3, <init> push acc, 3, <init> acc 5 5, 3, <init> acc acc + top_of_stack 12, 3, <init> pop 12 3, <init> acc acc + top_of_stack 15 3, <init> pop 15 <init> 11 12

Notes It is very important that the stack is preserved across the evaluation of a subexpression Stack before the evaluation of + 5 is 3, <init> Stack after the evaluation of + 5 is 3, <init> The first operand is on top of the stack From Stack Machines to MIPS The compiler generates code for a stack machine with accumulator We want to run the resulting code on the MIPS processor (or simulator) We simulate the stack machine instructions using MIPS instructions and registers 13 14 Simulating a Stack Machine on the MIPS The accumulator is kept in MIPS register $a0 The stack is kept in memory The stack grows towards lower addresses Standard convention on the MIPS architecture The address of the next location on the stack is kept in MIPS register $sp Guess: what does sp stand for? The top of the stack is at address $sp + 4 MIPS Assembly MIPS architecture Prototypical Reduced Instruction Set Computer (RISC) architecture Arithmetic operations use registers for operands and results Must use load and store instructions to use operands and store results in memory 32 general purpose registers (32 bits each) We will use $sp, $a0 and $t1 (a temporary register) Read the IM documentation for more details 15 16

A Sample of MIPS Instructions lw reg 1 offset(reg 2 ) load word Load 32-bit word from address reg 2 + offset into reg 1 add reg 1 reg 2 reg 3 reg 1 reg 2 + reg 3 sw reg 1 offset(reg 2 ) store word Store 32-bit word in reg 1 at address reg 2 + offset addiu reg 1 reg 2 imm add immediate reg 1 reg 2 + imm u means overflow is not checked li reg imm load immediate reg imm MIPS Assembly: Example The stack-machine code for + 5 in MIPS: acc push acc acc 5 acc acc + top_of_stack pop li $a0 sw $a0 0($sp) li $a0 5 lw $t1 4($sp) add $a0 $a0 $t1 addiu $sp $sp 4 We now generalize this to a simple language 1 18 A Small Language A language with only integers and integer operations ( Mini Bar ) P F P F F id(args) begin E end ARGS id, ARGS id E int id if E 1 = E 2 then E 3 else E 4 E 1 + E 2 E 1 E 2 id(e 1,,E n ) A Small Language (Cont.) The first function definition f is the main routine Running the program on input i means computing f(i) Program for computing the Fibonacci numbers: fib(x) begin if x = 1 then 0 else if x = 2 then 1 else fib(x - 1) + fib(x 2) end 19 20

Code Generation Strategy For each expression e we generate MIPS code that: Computes the value of e in $a0 Preserves $sp and the contents of the stack We define a code generation function cgen(e) whose result is the code generated for e cgen(e) will be recursive Code Generation for Constants The code to evaluate an integer constant simply copies it into the accumulator: cgen(int) = li $a0 int Note that this also preserves the stack, as required 21 22 Code Generation for Addition cgen(e 1 + e 2 ) = cgen(e 1 ) ; $a0 value of e 1 sw $a0 0($sp) ; push that value addiu $sp $sp 4 ; onto the stack cgen(e 2 ) ; $a0 value of e 2 lw $t1 4($sp) ; grab value of e 1 add $a0 $t1 $a0 ; do the addition addiu $sp $sp 4 ; pop the stack Possible optimization: Put the result of e 1 directly in register $t1? Code Generation for Addition: Wrong Attempt! Optimization: Put the result of e 1 directly in $t1? cgen(e 1 + e 2 ) = cgen(e 1 ) ; $a0 value of e 1 move $t1 $a0 ; save that value in $t1 cgen(e 2 ) ; $a0 value of e 2 ; may clobber $t1 add $a0 $t1 $a0 ; perform the addition Try to generate code for : 3 + ( + 5) 23 24

Code Generation Notes The code for e 1 + e 2 is a template with holes for code for evaluating e 1 and e 2 Stack machine code generation is recursive Code for e 1 + e 2 consists of code for e 1 and e 2 glued together Code generation can be written as a recursivedescent of the AST At least for (arithmetic) expressions Code Generation for Subtraction and Constants New instruction: sub reg 1 reg 2 reg 3 Implements reg 1 reg 2 -reg 3 cgen(e 1 -e 2 ) = cgen(e 1 ) ; $a0 value of e 1 sw $a0 0($sp) ; push that value addiu $sp $sp 4 ; onto the stack cgen(e 2 ) ; $a0 value of e 2 lw $t1 4($sp) ; grab value of e 1 sub $a0 $t1 $a0 ; do the subtraction addiu $sp $sp 4 ; pop the stack 25 26 Code Generation for Conditional Code Generation for If (Cont.) We need flow control instructions New MIPS instruction: beq reg 1 reg 2 label Branch to label if reg 1 = reg 2 New MIPS instruction: j label Unconditional jump to label cgen(if e 1 = e 2 then e 3 else e 4 ) = cgen(e 1 ) sw $a0 0($sp) cgen(e 2 ) lw $t1 4($sp) addiu $sp $sp 4 beq $a0 $t1 true_branch false_branch: cgen(e 4 ) j end_if true_branch: cgen(e 3 ) end_if: 2 28

Meet The Activation Record Code for function calls and function definitions depends on the layout of the activation record (or AR ) A very simple AR suffices for this language: The result is always in the accumulator No need to store the result in the AR The activation record holds actual parameters For f(x 1,,x n ) push the arguments x n,,x 1 onto the stack These are the only variables in this language Meet The Activation Record (Cont.) The stack discipline guarantees that on function exit, $sp is the same as it was before the args got pushed (i.e., before function call) We need the return address It s also handy to have a pointer to the current activation This pointer lives in register $fp (frame pointer) Reason for frame pointer will be clear shortly (at least I hope!) 29 30 Layout of the Activation Record Summary: For this language, an AR with the caller s frame pointer, the actual parameters, and the return address suffices Picture: Consider a call to f(x,y), the AR will be: FP old fp y x AR of f Code Generation for Function Call The calling sequence is the instructions (of both caller and callee) to set up a function invocation New instruction: jal label Jump to label, save address of next instruction in special register $ra On other architectures the return address is stored on the stack by the call instruction 31 32

Code Generation for Function Call (Cont.) Code Generation for Function Definition cgen(f(e 1,,e n )) = sw $fp 0($sp) cgen(e n ) sw $a0 0($sp) cgen(e 1 ) sw $a0 0($sp) jal f_entry The caller saves the value of the frame pointer Then it pushes the actual parameters in reverse order The caller s jal puts the return address in register $ra The AR so far is 4*n+4 bytes long New MIPS instruction: jr reg Jump to address in register reg cgen(f(x 1,,x n ) begin e end) = f_entry: move $fp $sp sw $ra 0($sp) cgen(e) lw $ra 4($sp) addiu $sp $sp frame_size lw $fp 0($sp) jr $ra Note: The frame pointer points to the top, not bottom of the frame Callee saves old return addr, evaluates its body, pops the return addr, pops the args, and then restores $fp frame_size = 4*n + 8 33 34 Calling Sequence: Example for f(x,y) Before call On entry After body After call FP 1 FP 1 FP 1 FP 1 FP 1 y x y x FP 2 return Code Generation for Variables/Parameters Variable references are the last construct The variables of a function are just its parameters They are all in the AR Pushed by the caller Problem: Because the stack grows when intermediate results are saved, the variables are not at a fixed offset from $sp 35 36

Code Generation for Variables/Parameters Solution: use the frame pointer Always points to the return address on the stack Since it does not move, it can be used to find the variables Let x i be the i th (i = 1,,n) formal parameter of the function for which code is being generated cgen(x i ) = lw $a0 offset($fp) ( offset = 4*i ) Code Generation for Variables/Parameters Example: For a function f(x,y) begin e end the activation and frame pointer are set up as follows (when evaluating e): FP old fp y x return x is at fp + 4 y is at fp + 8 3 38 Activation Record & Code Generation Summary The activation record must be designed together with the code generator Code generation can be done by recursive traversal of the AST Discussion Production compilers do different things Emphasis is on keeping values (esp. current stack frame) in registers Intermediate results are laid out in the AR, not pushed and popped from the stack As a result, code generation is often performed in synergy with register allocation Next time: code generation for temporaries and a deeper look into parameter passing mechanisms 39 40