lecture12

lecture12 - 1 Prof. Aiken CS 143 Lecture 12 1 Code...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Prof. Aiken CS 143 Lecture 12 1 Code Generation Lecture 12 Prof. Aiken CS 143 Lecture 12 2 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 for Objects Prof. Aiken CS 143 Lecture 12 3 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 stack machine instructions using MIPS instructions and registers Prof. Aiken CS 143 Lecture 12 4 Simulating a Stack Machine 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 The top of the stack is at address $sp + 4 Prof. Aiken CS 143 Lecture 12 5 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) Read the SPIM documentation for details Prof. Aiken CS 143 Lecture 12 6 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 2 Prof. Aiken CS 143 Lecture 12 7 MIPS Assembly. Example. The stack-machine code for 7 + 5 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 Prof. Aiken CS 143 Lecture 12 8 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 ) Prof. Aiken CS 143 Lecture 12 9 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) Prof. Aiken CS 143 Lecture 12 10 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 Prof. Aiken CS 143 Lecture 12 11 Code Generation for Constants...
View Full Document

This document was uploaded on 04/06/2012.

Page1 / 10

lecture12 - 1 Prof. Aiken CS 143 Lecture 12 1 Code...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online