lecture12 - CS 143 Lecture 12 1 Code Gener ation Lecture 12...

Info iconThis preview shows pages 1–12. 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

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight 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: CS 143 Lecture 12 1 Code Gener ation Lecture 12 CS 143 Lecture 12 2 Lectur e Outline Topic 1: Basic Code Generation The MI PS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation for Objects CS 143 Lecture 12 3 Fr om Stack Machines to MI PS The compiler generates code for a stack machine with accumulator We want to run the resulting code on the MI PS processor (or simulator) We simulate stack machine instructions using MI PS instructions and registers CS 143 Lecture 12 4 Simulating a Stack Machine The accumulator is kept in MI PS register $a0 The stack is kept in memory The stack grows towards lower addresses Standard convention on the MI PS architecture The address of the next location on the stack is kept in MI PS register $sp The top of the stack is at address $sp + 4 CS 143 Lecture 12 5 MI PS Assembly MI PS architecture Prototypical Reduced I nstruction Set Computer (RI SC) 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 SPI M documentation for details CS 143 Lecture 12 6 A Sample of MI PS I nstr uctions 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 CS 143 Lecture 12 7 MI PS Assembly. Example. The stack-machine code for 7 + 5 in MI PS: 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 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 ) 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) CS 143 Lecture 12 10 Code Gener ation Str ategy For each expression e we generate MI PS 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 CS 143 Lecture 12 11 Code Gener ation for Constants The code to evaluate a constant simply copies it into the accumulator: cgen(i) = li $a0 i This preserves the stack, as required Color key: RED : compile time BLUE: run time CS 143...
View Full Document

This note was uploaded on 05/05/2010 for the course COMPILER AC 1 taught by Professor Sergio during the Spring '10 term at Institute of Management Technology.

Page1 / 57

lecture12 - CS 143 Lecture 12 1 Code Gener ation Lecture 12...

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

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