DOC PREVIEW
Berkeley COMPSCI 164 - Code Generation

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

14/6/08 Prof. Hilfinger CS164 Lecture 29 1Code GenerationLecture 29(based on slides by R. Bodik)4/6/08 Prof. Hilfinger CS164 Lecture 29 2Lecture Outline• Stack machines• The MIPS assembly language• The x86 assembly language• A simple source language• Stack-machine implementation of the simplelanguage4/6/08 Prof. Hilfinger CS164 Lecture 29 3Stack Machines• A simple evaluation model• No variables or registers• A stack of values for intermediate results4/6/08 Prof. Hilfinger CS164 Lecture 29 4Example 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 add4/6/08 Prof. Hilfinger CS164 Lecture 29 5Stack 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…stack57… push 512…⊕…push 77 add4/6/08 Prof. Hilfinger CS164 Lecture 29 6Why Use a Stack Machine ?• Each operation takes operands from the sameplace and puts results in the same place• This means a uniform compilation scheme• And therefore a simpler compiler24/6/08 Prof. Hilfinger CS164 Lecture 29 7Why 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 r1, r2” ⇒ Smaller encoding of instructions ⇒ More compact programs• This is one reason why the Java Virtual Machine usesa stack evaluation model4/6/08 Prof. Hilfinger CS164 Lecture 29 8Optimizing 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 (at least) 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!4/6/08 Prof. Hilfinger CS164 Lecture 29 9Stack Machine with AccumulatorInvariants• The result of computing an expression is always in theaccumulator• For an operation op(e1,…,en) push the accumulator onthe stack after computing each of e1,…,en-1– The result of en is in the accumulator before op– After the operation pop n-1 values• After computing an expression the stack is as before4/6/08 Prof. Hilfinger CS164 Lecture 29 10Stack Machine with Accumulator. Example• Compute 7 + 5 using an accumulator…accstack57…acc ← 512…⊕acc ← acc +top_of_stackpop…7acc ← 7push acc74/6/08 Prof. Hilfinger CS164 Lecture 29 11A Bigger Example: 3 + (7 + 5) Code Acc Stackacc ← 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>4/6/08 Prof. Hilfinger CS164 Lecture 29 12Notes• It is very important that the stack ispreserved across the evaluation of asubexpression– 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 stack34/6/08 Prof. Hilfinger CS164 Lecture 29 13From Stack Machines to MIPS• The compiler generates code for a stackmachine with accumulator• We want to run the resulting code on an x86or MIPS processor (or simulator)• We implement stack machine instructionsusing MIPS instructions and registers4/6/08 Prof. Hilfinger CS164 Lecture 29 14MIPS 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 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 for therestricted subset we’ll need– see the translation table in a few slides4/6/08 Prof. Hilfinger CS164 Lecture 29 15Simulating 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 keptin MIPS register $sp– The top of the stack is at address $sp + 4– in x86, it’s %esp4/6/08 Prof. Hilfinger CS164 Lecture 29 16MIPS AssemblyMIPS architecture– Prototypical Reduced Instruction Set Computer(RISC) architecture– Arithmetic operations use registers for operandsand results– Must use load and store instructions to useoperands and results in memory– 32 general purpose registers (32 bits each)• We will use $sp, $a0 and $t1 (a temporary register)4/6/08 Prof. Hilfinger CS164 Lecture 29 17A Sample of MIPS Instructions– lw reg1 offset(reg2)• Load 32-bit word from address reg2 + offset into reg1– add reg1, reg2, reg3• reg1 ← reg2 + reg3– sw reg1, offset(reg2)• Store 32-bit word in reg1 at address reg2 + offset– addiu reg1, reg2, imm• reg1 ← reg2 + imm• “u” means overflow is not checked– li reg, imm• reg ← imm4/6/08 Prof. Hilfinger CS164 Lecture 29 18x86 Assemblyx86 architecture– Complex Instruction Set Computer (CISC) architecture– Arithmetic operations can use both registers and memory foroperands and results– So, you don’t have to use separate load and store instructionsto 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 easy44/6/08 Prof. Hilfinger CS164 Lecture 29 19x86 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 exist– one important difference: order of operands– the manuals assume• ADD dest, src– the gcc assembler we’ll use uses opposite order• ADD src, dest4/6/08 Prof. Hilfinger CS164 Lecture 29 20Sample x86 instructions (gcc order of operands)– movl offset(reg2),


View Full Document

Berkeley COMPSCI 164 - Code Generation

Documents in this Course
Lecture 8

Lecture 8

40 pages

Load more
Download Code Generation
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Code Generation and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Code Generation 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?