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

Similar documents
Code Generation. Lecture 30

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

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

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

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 12

Code Generation Super Lectures

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

Lecture #31: Code Generation

Lecture #19: Code Generation

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

Code Generation Super Lectures

Systems Architecture I

Intermediate Code & Local Optimizations

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

The Activation Record (AR)

EC-801 Advanced Computer Architecture

CS222: Dr. A. Sahu. Indian Institute of Technology Guwahati

x86 assembly CS449 Fall 2017

Lecture 4: MIPS Instruction Set

Second Part of the Course

CS429: Computer Organization and Architecture

Review Questions. 1 The DRAM problem [5 points] Suggest a solution. 2 Big versus Little Endian Addressing [5 points]

Compilers and computer architecture: A realistic compiler to MIPS

Course Administration

Chapter 2A Instructions: Language of the Computer

Lecture 3: Instruction Set Architecture

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.

Communicating with People (2.8)

Lecture 4: Instruction Set Architecture

Chapter 2. lw $s1,100($s2) $s1 = Memory[$s2+100] sw $s1,100($s2) Memory[$s2+100] = $s1

Instruction-set Design Issues: what is the ML instruction format(s) ML instruction Opcode Dest. Operand Source Operand 1...

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

Chapter 2. Instructions:

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

CS153: Compilers Lecture 2: Assembly

Computer Architecture

Computer Architecture. The Language of the Machine

W4118: PC Hardware and x86. Junfeng Yang

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Compiler Construction D7011E

Instructions: MIPS ISA. Chapter 2 Instructions: Language of the Computer 1

Instruction-set Design Issues: what is the ML instruction format(s) ML instruction Opcode Dest. Operand Source Operand 1...

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

Process Layout and Function Calls

Chapter 2: Instructions:

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

CENG3420 Lecture 03 Review

MIPS Assembly Programming

Instruction Set Architecture

Assembly Language: Overview!

CS61C : Machine Structures

CS61 Section Solutions 3

CS241 Computer Organization Spring 2015 IA

Instruction Set Architecture

ECE232: Hardware Organization and Design. Computer Organization - Previously covered

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

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

1 5. Addressing Modes COMP2611 Fall 2015 Instruction: Language of the Computer

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

Control Instructions

ECE 486/586. Computer Architecture. Lecture # 7

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

Course Administration

Chapter 3. Instructions:

Chapter 2. Instruction Set Architecture (ISA)

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

CISC 360 Instruction Set Architecture

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

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

Instruction Set Architecture

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

2.7 Supporting Procedures in hardware. Why procedures or functions? Procedure calls

CMSC Computer Architecture Lecture 2: ISA. Prof. Yanjing Li Department of Computer Science University of Chicago

Instructions: Language of the Computer

Instruction Set Principles. (Appendix B)

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

CSE Lecture In Class Example Handout

CS 316: Procedure Calls/Pipelining

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

Instructions: Language of the Computer

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

EITF20: Computer Architecture Part2.1.1: Instruction Set Architecture

Assignment 11: functions, calling conventions, and the stack

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

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

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

cmovxx ra, rb 2 fn ra rb irmovl V, rb rb V rmmovl ra, D(rB) 4 0 ra rb D mrmovl D(rB), ra 5 0 ra rb D OPl ra, rb 6 fn ra rb jxx Dest 7 fn Dest

55:132/22C:160, HPCA Spring 2011

Where we are. Instruction selection. Abstract Assembly. CS 4120 Introduction to Compilers

Computer Organization MIPS ISA

MIPS ISA. 1. Data and Address Size 8-, 16-, 32-, 64-bit 2. Which instructions does the processor support

CS3350B Computer Architecture

Mark Redekopp, All rights reserved. EE 357 Unit 11 MIPS ISA

Transcription:

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 of the simple language CS164 Lecture14 Fall2004 2

Stack Machines A simple evaluation model No variables or registers A stack of values for intermediate results CS164 Lecture14 Fall2004 3

Example of a Stack Machine Program Consider two instructions Ð push i - place the integer i on top of the stack Ð add - pop two elements, add them and put the result back on the stack A program to compute 7 + 5: push 7 push 5 add CS164 Lecture14 Fall2004 4

Stack Machine. Example 7 5 7 12 stack É É É É Each instruction: push 7 push 5 Ð Takes its operands from the top of the stack Ð Removes those operands from the stack Ð Computes the required operation on them Ð Pushes the result on the stack add CS164 Lecture14 Fall2004 5

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 CS164 Lecture14 Fall2004 6

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 ÒaddÓ as opposed to Òadd r 1, r 2 Ó " Smaller encoding of instructions " More compact programs This is one reason why Java Bytecodes use a stack evaluation model CS164 Lecture14 Fall2004 7

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 register (called accumulator) Ð Register accesses are faster The ÒaddÓ instruction is now acc acc + top_of_stack Ð Only one memory operation! CS164 Lecture14 Fall2004 8

Stack Machine with Accumulator Invariants The result of computing an expression is always in the accumulator For an operation op(e 1,É,e n ) push the accumulator on the stack after computing each of e 1,É,e n-1 Ð The result of e n is in the accumulator before op Ð After the operation pop n-1 values After computing an expression the stack is as before CS164 Lecture14 Fall2004 9

Stack Machine with Accumulator. Example Compute 7 + 5 using an accumulator acc 7 5 12 7 7 stack É É É É acc 7 push acc acc 5 acc acc + top_of_stack pop CS164 Lecture14 Fall2004 10

A Bigger Example: 3 + (7 + 5) Code Acc Stack acc 3 3 <init> push acc 3 3, <init> acc 7 7 3, <init> push acc 7 7, 3, <init> acc 5 5 7, 3, <init> acc acc + top_of_stack 12 7, 3, <init> pop 12 3, <init> acc acc + top_of_stack 15 3, <init> pop 15 <init> CS164 Lecture14 Fall2004 11

Notes It is very important that the stack is preserved across the evaluation of a subexpression Ð Stack before the evaluation of 7 + 5 is 3, <init> Ð Stack after the evaluation of 7 + 5 is 3, <init> Ð The first operand is on top of the stack CS164 Lecture14 Fall2004 12

From Stack Machines to MIPS The compiler generates code for a stack machine with accumulator We want to run the resulting code on an x86 or MIPS processor (or simulator) We implement stack machine instructions using MIPS instructions and registers CS164 Lecture14 Fall2004 13

MIPS assembly vs. x86 assembly In PA4 and PA5, you will generate x86 code Ð because we have no MIPS machines around Ð and using a MIPS simulator is less exciting In this lecture, we will use MIPS assembly Ð itõs somewhat more readable than x86 assembly Ð e.g. in x86, both store and load are called movl translation from MIPS to x86 trivial Ð see the translation table in a few slides CS164 Lecture14 Fall2004 14

Simulating a Stack MachineÉ The accumulator is kept in MIPS register $a0 Ð in x86, itõs in %eax The stack is kept in memory The stack grows towards lower addresses Ð standard convention on both MIPS and x86 The address of the next location on the stack is kept in MIPS register $sp Ð The top of the stack is at address $sp + 4 Ð in x86, itsõ %esp CS164 Lecture14 Fall2004 15

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 results in memory Ð 32 general purpose registers (32 bits each) We will use $sp, $a0 and $t1 (a temporary register) CS164 Lecture14 Fall2004 16

A Sample of MIPS Instructions Ð lw reg 1 offset(reg 2 ) 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 32-bit word in reg 1 at address reg 2 + offset Ð addiu reg 1, reg 2, imm reg 1 reg 2 + imm ÒuÓ means overflow is not checked Ð li reg, imm reg imm CS164 Lecture14 Fall2004 17

x86 Assembly x86 architecture Ð Complex Instruction Set Computer (CISC) architecture Ð Arithmetic operations can use both registers and memory for operands and results Ð So, you donõt have to use separate load and store instructions to operate on values in memory Ð CISC gives us more freedom in selecting instructions (hence, more powerful optimizations) Ð but weõll use a simple RISC subset of x86 so translation from MIPS to x86 will be easy CS164 Lecture14 Fall2004 18

x86 assembly x86 has two-operand instructions: Ð ex.: ADD dest, src dest := dest + src Ð in MIPS: dest := src1 + src2 An annoying fact to remember! Ð different x86 assembly versions exists Ð one important difference: order of operands Ð the manuals assume ADD dest, src Ð the gcc assembler weõll use uses opposite order ADD src, dest CS164 Lecture14 Fall2004 19

Sample x86 instructions (gcc order of operands) Ð movl offset(reg 2 ), reg 1 Load 32-bit word from address reg 2 + offset into reg 1 Ð add reg 2, reg 1 reg 1 reg 1 + reg 2 Ð movl reg 1 offset(reg 2 ) Store 32-bit word in reg 1 at address reg 2 + offset Ð add imm, reg 1 reg 1 reg 1 + imm use this for MIPSÕ addiu Ð movl imm, reg reg imm CS164 Lecture14 Fall2004 20

MIPS to x86 translation MIPS lw reg 1, offset(reg 2 ) x86 movl offset(reg 2 ), reg 1 add reg 1, reg 1, reg 2 sw reg 1, offset(reg 2 ) addiu reg 1, reg 1, imm li reg, imm add reg 2, reg 1 movl reg 1, offset(reg 2 ) add imm, reg 1 movl imm, reg CS164 Lecture14 Fall2004 21

x86 vs. MIPS registers MIPS $a0 $sp $fp $t x86 %eax %esp %ebp %ebx CS164 Lecture14 Fall2004 22

MIPS Assembly. Example. The stack-machine code for 7 + 5 in MIPS: acc 7 li $a0, 7 push acc sw $a0, 0($sp) addiu $sp, $sp, -4 acc 5 li $a0, 5 acc acc + top_of_stack lw $t1, 4($sp) add $a0, $a0, $t1 pop addiu $sp, $sp, 4 We now generalize this to a simple languageé CS164 Lecture14 Fall2004 23

Some Useful Macros We define the following abbreviation push $t addiu $sp, $sp, -4 sw $a0, 0($sp) pop addiu $sp, $sp, 4 $t top lw $t, 0($sp) CS164 Lecture14 Fall2004 24

A Small Language A language with integers and integer operations P$ D; P D D$def id(args) = E; 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 ) CS164 Lecture14 Fall2004 25

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: def fib(x) = if x = 1 then 0 else if x = 2 then 1 else fib(x - 1) + fib(x Ð 2) CS164 Lecture14 Fall2004 26

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 CS164 Lecture14 Fall2004 27

Code Generation for Constants The code to evaluate a constant simply copies it into the accumulator: cgen(i) = li $a0, i Note that this also preserves the stack, as required CS164 Lecture14 Fall2004 28

Code Generation for Add cgen(e 1 + e 2 ) = cgen(e 1 ) push $a0 cgen(e 2 ) $t1 top add $a0, $t1, $a0 pop Possible optimization: Put the result of e 1 directly in register $t1? CS164 Lecture14 Fall2004 29

Code Generation for Add. Wrong! Optimization: Put the result of e 1 directly in $t1? cgen(e 1 + e 2 ) = cgen(e 1 ) move $t1, $a0 cgen(e 2 ) add $a0, $t1, $a0 Try to generate code for : 3 + (7 + 5) CS164 Lecture14 Fall2004 30

Code Generation Notes The code for + 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 recursive-descent of the AST Ð At least for expressions CS164 Lecture14 Fall2004 31

Code Generation for Sub 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 ) push $a0 cgen(e 2 ) $t1 top sub $a0, $t1, $a0 pop CS164 Lecture14 Fall2004 32

Code Generation for Conditional We need flow control instructions New instruction: beq reg 1, reg 2, label Ð Branch to label if reg 1 = reg 2 Ð x86: cmpl reg 1, reg 2 je label New instruction: b label Ð Unconditional jump to label Ð x86: jmp label CS164 Lecture14 Fall2004 33

Code Generation for If (Cont.) cgen(if e 1 = e 2 then e 3 else e 4 ) = cgen(e 1 ) push $a0 cgen(e 2 ) $t1 top pop beq $a0, $t1, true_branch false_branch: cgen(e 4 ) b end_if true_branch: cgen(e 3 ) end_if: CS164 Lecture14 Fall2004 34