4/6/08Prof. Hilfinger CS164 Lecture 291 Code Generation Lecture 29 (based on slides by R. Bodik)

Slides:



Advertisements
Similar presentations
Lecture 13: 10/8/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Advertisements

Goal: Write Programs in Assembly
Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
1 Lecture 3: Instruction Set Architecture ISA types, register usage, memory addressing, endian and alignment, quantitative evaluation.
1 ECE462/562 ISA and Datapath Review Ali Akoglu. 2 Instruction Set Architecture A very important abstraction –interface between hardware and low-level.
Machine Instructions Operations 1 ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-1.ppt Modification date: March 18, 2015.
Deeper Assembly: Addressing, Conditions, Branching, and Loops
Chapter 2 — Instructions: Language of the Computer — 1 Branching Far Away If branch target is too far to encode with 16-bit offset, assembler rewrites.
Chapter 2 Instructions: Language of the Computer
Chapter 2.
Instruction Set Architecture Classification According to the type of internal storage in a processor the basic types are Stack Accumulator General Purpose.
Computer Architecture CSCE 350
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 6 – Introduction to MIPS Data Transfer & Decisions I Pieter Abbeel’s recent.
CS61C L09 Introduction to MIPS: Data Transfer & Decisions I (1) Garcia © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS 61C L09 Introduction to MIPS: Data Transfer & Decisions I (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS 536 Spring Code generation I Lecture 20.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
Intermediate Code. Local Optimizations
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
CS61C L09 Introduction to MIPS : Data Transfer and Decisions (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
Linked Lists in MIPS Let’s see how singly linked lists are implemented in MIPS on MP2, we have a special type of doubly linked list Each node consists.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
ITEC 352 Lecture 20 JVM Intro. Functions + Assembly Review Questions? Project due today Activation record –How is it used?
Compiler Run-time Organization Lecture 7. 2 What we have covered so far… We have covered the front-end phases –Lexical analysis –Parsing –Semantic analysis.
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Higher Level languages have adopted a standard Referred to as C-style calling Uses the stack.
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
Oct. 25, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Alternative Instruction Sets * Jeremy R. Johnson Wed. Oct. 25, 2000.
Operand Addressing And Instruction Representation Cs355-Chapter 6.
MS108 Computer System I Lecture 3 ISA Prof. Xiaoyao Liang 2015/3/13 1.
Functions/Methods in Assembly
Compiler Construction Code Generation Activation Records
Computer Organization Instructions Language of The Computer (MIPS) 2.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
Computer Organization 1
Lecture 16: 10/29/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
F453 Module 8: Low Level Languages 8.1: Use of Computer Architecture.
CS 164 Lecture 151 Code Generation (I) ICOM4029 Lecture 9.
Computer Architecture Instruction Set Architecture
MIPS Instruction Set Advantages
ISA's, Compilers, and Assembly
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Prof. Sirer CS 316 Cornell University
Assembly Programming using MIPS R3000 CPU
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
Code Generation Lecture 12 CS 164 Lecture 14 Fall 2004.
Systems Architecture I (CS ) Lecture 5: MIPS Instruction Set*
CSCI206 - Computer Organization & Programming
Lecture 30 (based on slides by R. Bodik)
The University of Adelaide, School of Computer Science
ECE232: Hardware Organization and Design
Machine-Level Programming 2 Control Flow
MIPS Procedure Calls CSE 378 – Section 3.
The University of Adelaide, School of Computer Science
Instruction encoding The ISA defines Format = Encoding
Computer Instructions
3.
Prof. Sirer CS 316 Cornell University
COMS 361 Computer Organization
Operating Systems Run-time Organization
COMS 361 Computer Organization
Assembly Programming using MIPS R3000 CPU
MIPS Assembly.
CPU Structure CPU must:
Systems Architecture I (CS ) Lecture 5: MIPS Instruction Set*
9/27: Lecture Topics Memory Data transfer instructions
Presentation transcript:

4/6/08Prof. Hilfinger CS164 Lecture 291 Code Generation Lecture 29 (based on slides by R. Bodik)

4/6/08Prof. Hilfinger CS164 Lecture 292 Lecture Outline Stack machines The MIPS assembly language The x86 assembly language A simple source language Stack-machine implementation of the simple language

4/6/08Prof. Hilfinger CS164 Lecture 293 Stack Machines A simple evaluation model No variables or registers A stack of values for intermediate results

4/6/08Prof. Hilfinger CS164 Lecture 294 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

4/6/08Prof. Hilfinger CS164 Lecture 295 Stack Machine. Example 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 on the stack … stack 5 7 … push 5 12 …  … push 7 7 add

4/6/08Prof. Hilfinger CS164 Lecture 296 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

4/6/08Prof. Hilfinger CS164 Lecture 297 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 the Java Virtual Machine uses a stack evaluation model

4/6/08Prof. Hilfinger CS164 Lecture 298 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 most recently computed value in a register (called accumulator) since register accesses are faster. The “add” instruction is now acc  acc + top_of_stack –Only one memory operation!

4/6/08Prof. Hilfinger CS164 Lecture 299 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

4/6/08Prof. Hilfinger CS164 Lecture 2910 Stack Machine with Accumulator. Example Compute using an accumulator … acc stack 5 7 … acc  5 12 …  acc  acc + top_of_stack pop … 7 acc  7 push acc 7

4/6/08Prof. Hilfinger CS164 Lecture 2911 A Bigger Example: 3 + (7 + 5) Code Acc Stack acc  3 3 push acc 3 3, acc  7 7 3, push acc 7 7, 3, acc  5 5 7, 3, acc  acc + top_of_stack 12 7, 3, pop 12 3, acc  acc + top_of_stack 15 3, pop 15

4/6/08Prof. Hilfinger CS164 Lecture 2912 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

4/6/08Prof. Hilfinger CS164 Lecture 2913 MIPS assembly vs. x86 assembly In Project 3, you will generate x86 code –because we have no MIPS machines around –and using a MIPS simulator is less exciting In this lecture, we’ll start with 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 for the restricted subset we’ll need –see the translation table in a few slides

4/6/08Prof. Hilfinger CS164 Lecture 2914 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, it’s %esp

4/6/08Prof. Hilfinger CS164 Lecture 2915 MIPS Assembly MIPS architecture –Typical 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)

4/6/08Prof. Hilfinger CS164 Lecture 2916 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

4/6/08Prof. Hilfinger CS164 Lecture 2917 MIPS Assembly. Example. The stack-machine code for in MIPS: acc  7 push acc acc  5 acc  acc + top_of_stack pop li $a0, 7 sw $a0, 0($sp) addiu $sp, $sp, -4 li $a0, 5 lw $t1, 4($sp) add $a0, $a0, $t1 addiu $sp, $sp, 4 We now generalize this to a simple language…

4/6/08Prof. Hilfinger CS164 Lecture 2918 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 )

4/6/08Prof. Hilfinger CS164 Lecture 2919 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)

4/6/08Prof. Hilfinger CS164 Lecture 2920 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

4/6/08Prof. Hilfinger CS164 Lecture 2921 Some Useful Macros We define the following abbreviation push $t sw $t, 0($sp) addiu $sp, $sp, -4 pop addiu $sp, $sp, 4 $t  top lw $t, 4($sp)

4/6/08Prof. Hilfinger CS164 Lecture 2922 Code Generation for Constants The code to evaluate a constant simply copies it into the accumulator: cgen(i) = li $a0, i This also preserves the stack, as required

4/6/08Prof. Hilfinger CS164 Lecture 2923 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 ?

4/6/08Prof. Hilfinger CS164 Lecture 2924 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)

4/6/08Prof. Hilfinger CS164 Lecture 2925 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 (modified) post-order traversal of the AST, at least for expressions

4/6/08Prof. Hilfinger CS164 Lecture 2926 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

4/6/08Prof. Hilfinger CS164 Lecture 2927 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

4/6/08Prof. Hilfinger CS164 Lecture 2928 Code Generation for If (Cont.) cgen(if e 1 = e 2 then e 3 else e 4 ) = false_branch = new_label () true_branch = new_label () end_if = new_label () 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:

4/6/08Prof. Hilfinger CS164 Lecture 2929 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

4/6/08Prof. Hilfinger CS164 Lecture 2930 x86 assembly x86 has two-operand instructions: –ex.: ADD dest, srcdest := dest + src –in MIPS: dest := src1 + src2 An annoying fact to remember  –different x86 assembly versions exist –one important difference: order of operands –the manuals assume ADD dest, src –the gcc assembler we’ll use uses opposite order ADD src, dest

4/6/08Prof. Hilfinger CS164 Lecture 2931 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

4/6/08Prof. Hilfinger CS164 Lecture 2932 MIPS to x86 translation MIPSx86 lw reg 1, offset(reg 2 )movl offset(reg 2 ), reg 1 add reg 1, reg 1, reg 2 add reg 2, reg 1 sw reg 1, offset(reg 2 )movl reg 1, offset(reg 2 ) addiu reg 1, reg 1, immadd imm, reg 1 li reg, immmovl imm, reg

4/6/08Prof. Hilfinger CS164 Lecture 2933 x86 vs. MIPS registers MIPSx86 $a0%eax $sp%esp $fp%ebp $t%ebx

4/6/08Prof. Hilfinger CS164 Lecture 2934 Useful Macros, IA32 version (GNU syntax) push %t pushl %t (t a general register) pop addl $4, %esp or popl %t (also moves top to %t) %t  top movl (%esp), %t