CSE 131B – Compiler Construction II Discussion 6: Operations, Branches and Functions 2/21/2007.

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
Branches Two branch instructions:
The University of Adelaide, School of Computer Science
Chapter 8 ICS 412. Code Generation Final phase of a compiler construction. It generates executable code for a target machine. A compiler may instead generate.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
SPARC Architecture & Assembly Language
Fall EE 333 Lillevik 333f06-l4 University of Portland School of Engineering Computer Organization Lecture 4 Assembly language programming ALU and.
1 Compiler Construction Intermediate Code Generation.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
CSE 131B – Compiler Construction II Discussion 8: Complex Data Structures 3/7/2007 Reminder: No class Thursday – lab hours instead.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
CS 536 Spring Run-time organization Lecture 19.
CSE 131B – Compiler Construction II Discussion 5: SPARC Assembly (The Fun Stuff!) 2/14/2007 Happy Valentine’s Day!
CS 536 Spring Code generation I Lecture 20.
CSE 131B – Compiler Construction II Discussion 3: Project 1 – Constants, Type Aliases, Procedures 1/24/2007.
CSE 131B – Compiler Construction II Discussion 7: Short-Circuiting, Pointers/Records, and Arrays 2/28/2007.
Run time vs. Compile time
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Microprocessors Frame Pointers and the use of the –fomit-frame-pointer switch Feb 25th, 2002.
Run-time Environment and Program Organization
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Code Generation CS 480. Can be complex To do a good job of teaching about code generation I could easily spend ten weeks But, don’t have ten weeks, so.
Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
CSC 3210 Computer Organization and Programming Chapter 9 EXTERNAL DATA AND TEXT D.M. Rasanjalee Himali.
Introduction CS 104: Applied C++ What is Programming? For some given problem: __________ a solution for it -- identify, organize & store the problem's.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Programmer's view on Computer Architecture by Istvan Haller.
Compiler Construction
Languages and the Machine Chapter 5 CS221. Topics The Compilation Process The Assembly Process Linking and Loading Macros We will skip –Case Study: Extensions.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
CPS120: Introduction to Computer Science Decision Making in Programs.
CS 2130 Lecture 27 Functional Optimizations. Just the factorial... (define fact (lambda (n) (if (= 0 n) 1 (* n (fact (- n 1))) ) (define fact (lambda...))
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
Runtime Stack Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens MIPS Memory Organization In addition to memory for static.
Simple Code Generation CS153: Compilers. Code Generation Next PS: Map Fish code to MIPS code Issues: –eliminating compound expressions –eliminating variables.
Prof. Fateman CS 164 Lecture 281 Virtual Machine Structure Lecture 28.
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Csci 136 Computer Architecture II – Summary of MIPS ISA Xiuzhen Cheng
CSC 8505 Compiler Construction Runtime Environments.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Working with Loops, Conditional Statements, and Arrays.
Code Generation CPSC 388 Ellen Walker Hiram College.
ARM-7 Assembly: Example Programs 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
1 Assembly Language: Function Calls Jennifer Rexford.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Assembly language.
Instructions - Type and Format
Stack Frame Linkage.
MIPS Instructions.
Program and memory layout
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Some Assembly (Part 2) set.html.
Where is all the knowledge we lost with information? T. S. Eliot
Presentation transcript:

CSE 131B – Compiler Construction II Discussion 6: Operations, Branches and Functions 2/21/2007

Overview  Phase 1  Some Phase 2

Refresher  Remember to work in increments! BEGIN END..section“.text”.align4.globalmain main: set.SAVE.main, %g1 save%sp, %g1, %sp ret restore.SAVE.main = -(92 + 0) & -8

Refresher  Remember to work in increments! VAR x: INTEGER; BEGIN x := 99; END..section“.data”.align4.varX:.word0! Global label method.section“.text”.align4.globalmain main: set.SAVE.main, %g1 save%sp, %g1, %sp set99, %l0 st%l0, [%fp-4]! Temporary storage set.varX, %l0 ld[%fp-4], %l1 st%l1, [%l0] ret restore.SAVE.main = -(92 + 4) & -8! 4 bytes of temp storage

General Principles  Start off doing simple load-load-compute-store. Don’t worry about efficiency or speed, as you won’t get any points for those.  VarSTOs will always be somewhere in memory (offsets from %fp or %g6). If the VarSTO is a parameter, it is initially in the I-registers until you move it to it’s proper place (%fp+68, %fp+72, etc).  ExprSTOs will always be in some temporary memory location (offsets from %fp).  ConstSTOs shouldn’t be stored in the run-time memory. Just do a “set” instruction with the value when you need it.

Things to Know  We won't be passing more than 6 parameters to procedures (hence, allowing you to rely on the %o0-%o5/%i0-%i5 registers). For the purposes of procedures with receiver variables, we'll only pass up to 5 parameters (with the 6th parameter implicitly being "this").  We WILL test more than 4K local variables/temporary memory (as well as large arrays). Make sure your load/store instructions and save instructions can handle those cases where the constant is more than +/- 4K.  When you make labels for things, make sure you make them unique. Think of cases like a procedure in Oberon named "main" (this is legal). You should name- mangle procedure, variable, and loop labels with a "." in front of them, as Oberon won't allow names with a "." (and thus avoid colliding names).  Don't assume statements will be simple. We may having complex things like: Z := X + (Y + (Z + (W + (B + C)))) DIV (X + Y + A + B + C). Your compiler should easily scale if you properly do the load-load-compute-store scheme.

Phase 1 –Arithmetic Expressions  Given (assume x is in %fp-4): x := x + 7; ld[%fp-4], %l0 set7, %l1 add%l0, %l1, %l0 st%l0, [%fp-8]! Tmp ld[%fp-8], %l0 st%l0, [%fp-4]

Arithmetic Expressions  OK, that’s simple for a simple statement.  What if we had a statement like this: Z := ((A+B) + (C+D)) + ((E+F) + (X+Y))  Which registers do we use???

Method 1 – Simplicity  Doing load-load-compute-store will only require 3 registers at any given time.  Thus, even with that complicated example, you just load two operands, compute it, and store the result into some temporary memory location. Then, the registers are all free again for use.  You will need to store the temporary memory location you used in your ExprSTO so you can reference it later.

Method 2 – Register Allocation  Have some data structure that lets you know what registers are free and which are currently in use.  Every time you need a register, request one from the data structure, which will remove that register from the available list  When you are done with a register, let the data structure know it is available for use again.  This is a good method too!

Method 2 – Register Allocation  Make some class (RegClass) with: GetFreeReg() – returns an available register FreeReg(String r) – marks that “r” is available  You will need to store the allotted register in your ExprSTO so you can reference it later.

Method 3 – Clumsy/Bad!  We can take advantage of Java Strings and encapsulate chunks of assembly code within each ExprSTO.  This will require a fixed register approach – you will need to have registers that serve a specific purpose (ie, a specific register is the result of an operation, etc).  Very confusing to keep track of – avoid this unless you really feel it is necessary!

Method 3 – Clumsy/Bad!  Always put operands into %o0 and %o1 Useful for calling.mul and.div  Compute operation, and place result in %g1  Store the resulting assembly code string into the ExprSTO, without outputting to the file  When that ExprSTO is used in another Expression, dump the stored code and make some small register moves.

Method 3 – Example (a + b) + (x + y) lda, %o0ldx, %o0 ldb, %o1ldy, %o1 add%o0, %o1, %g1add%o0, %o1, %g1 {place code (a+b) here} mov%g1, %l0 {place code (x+y) here} mov%l0, %o0 mov%g1, %o1 add%o0, %o1, %g1

Methods are your Friend!  Consider adding methods to your STO’s that make generating assembly for certain cases easier: GetAddress() – returns base/offset (ie, %fp – 4) GetValue() – will combine GetAddress with an appropriate load instruction Etc.

Conditions – Branching  Given this: IF x THEN (* … *) END; ldx, %l0 cmp%l0, %g0! Compare with zero be.IfL1! Opposite logic nop (* … *).IfL1:

Branching – Where to?  You will need to generate labels for your branch statements. These labels must be unique  A simple solution would be to use some prefix string (i.e., “.IfL”), and append some counter at the end:.IfL1,.IfL2,.IfL3, …

Branching – Label Stack  Consider you had: IF X THEN IF Y THEN (*…*) END; END;  You will eventually need some sort of label stack to alleviate issues that arise from nested conditions.

Branching – Label Stack IF X THEN – load X, compare, branch to L1, push L1 onto stack IF Y THEN – load Y, compare, branch to L2, push L2 onto stack (*…*) END;- Pop L2 from stack and output label END;- Pop L1 from stack and output label

Procedures  How to call a procedure? Ex:call foo nop  How to return from a procedure? Ex:ret restore  How to return a value from a procedure? Ex:mov%l0, %i0 ret restore

Procedures – Example PROCEDURE foo () : INTEGER; VAR x: INTEGER; BEGIN x := 2; RETURN x; END foo;

Procedures – Example The following can be generated just by parsing “PROCEDURE foo”:.section“.text”.align4.global.foo.foo: set.foo.SIZE, %g1 save%sp, %g1, %sp

Procedures – Example Now, the body of the function: set2, %l0! Put “2” in a reg. st%l0, [%fp-8]! Store in tmp mem ld[%fp-8], %l0! Load from tmp st%l0, [%fp-4]! Store expr into “x” ld[%fp-4], %i0! Put “x” in return ret! Return statement restore

Procedures – Example Lastly, now that we got to “END foo;”:.foo.SIZE = -(92 + 8) & -8 ! Bytes of local and tmp vars  By leaving this to the end, you can also allocate extra stack space for intermediate expression storage if needed during the body of the function.

Large Example VAR y : INTEGER; PROCEDURE foo() : INTEGER; VAR x : INTEGER; BEGIN x := y;(* note how this uses a global that is set at runtime *) RETURN (x + 1); END foo; VAR z : BOOLEAN; BEGIN y := 4; IF y < foo() THEN WRITE “yes\n”; z := TRUE; END; WRITE z, NL; END.

Large Example.section“.rodata”.align4.I.s:.asciz“%d” ! General stuff we should always have handy.NL.s:.asciz“\n”! Good idea to output this when you open the file.T.s:.asciz“TRUE”.F.s:.asciz“FALSE”.section“.text”.align4.global.foo.foo: set.SAVE.foo, %g1 save%sp, %g1, %sp ! x := y; ld[%g6-4], %l0! Load global y st%l0, [%fp-8]! Store in temporary mem ld[%fp-8], %l0! Get expression to assign statement st%l0, [%fp-4]! Store result into local x ! RETURN (x+1); ld[%fp-4], %l0 set1, %l1! Use set for a const (ConstSTO or literal) add%l0, %l1, %l2! Do addition st%l2, [%fp-12]! Store in temporary mem ld[%fp-12], %l0! Get expression result from temp mem mov%l0, %i0! Put result in %i0, which is return value for procedure ret! Do return restore! Restore register window.SAVE.foo = -( ) & -8! We had 1 local var and 2 temporary locations  12 bytes

Large Example.section“.bss” ! Right before “main”, we go into BSS and set all the “globals”..align4.skip4! Global z  [%g6-8].skip4! Global y  [%g6-4].globals:! Note how they are in reverse order. The easiest way to do this is to have a vector of VarSTOs ! that you add to every time you do a global VarDecl. Then, walk through it backwards right before “main”.section“.text”.align4.globalmain main: set.SAVEmain, %g1 save%sp, %g1, %sp set.globals, %g6! One-time initialization of %g6 the be the base address of the globals ! y := 4; set4, %l0! Use set for integer literal st%l0, [%fp-4]! Store in temporary mem ld[%fp-4], %l0! Get expression to assign statement st%l0, [%g6-4]! Store result into global y ! IF y < foo() ld[%g6-4], %l0! Load global y call.foo nop mov%o0, %l1! Get result of foo into %l1 mov%g0, %l2! Initialize boolean result to FALSE cmp%l0, %l1! Compare the two bge.L1! Opposite logic! nop

Large Example set 1, %l2! Change boolean result to TRUE if branch failed.L1:! Branch will jump to here, skipping setting to TRUE st%l2, [%fp-8]! Store y < foo() boolean result into temporary mem ld[%fp-8], %l0! Get y < foo() result from temp mem cmp%l0, %g0! Compare with FALSE be.L2! Skip over code if FALSE nop ! WRITE “yes\n”;.section“.rodata”! Switch to read-only data segment (data segment also OK).L3:.asciz“yes\n”! No align necessary for.asciz (since byte-array, no alignment restrictions).section“.text”! Switch back to text segment.align4 set.L3, %o0! Used a unique global label to point to string literal callprintf nop ! z := TRUE; set1, %l0! Set TRUE constant literal into register st%l0, [%fp-12]! Store into temp mem ld[%fp-12], %l0! Get from result expression from temp mem st%l0, [%g6-8]! Store into global z.L2:! Skip-over label from IF stmt

Large Example ! WRITE z, NL; ld[%g6-8], %l0! Load global z st%l0, [%fp-16]! Store into temp mem ld[%fp-16], %l0! Load expression result from temp mem set.F.s, %o0! Default to print “FALSE” cmp%l0, %g0! Since z is boolean, determine if we print TRUE or FALSE be.L4! Skip over if FALSE nop set.T.s, %o0! Change to “TRUE” if z is TRUE.L4:! Branch over here callprintf! Will print TRUE or FALSE depending on what %o0 was nop set.NL.s, %o0! Set NL literal for output callprintf! Output newline nop ret! Implicit return at end of every procedure (including main) restore.SAVEmain = -( ) & -8! No local vars and 4 temp locations (16 bytes) When you run this, you should get: yes TRUE -bash-3.00$

What to do Next! 1.Continue planning out how you want to structure your project – good planning leads to an easier design in the long run. 2.Finish Phase 1. 3.Start of Phase 2. 4.Come to lab hours and ask questions.

Topics/Questions you may have  Anything else you would like me to go over now?  Anything in particular you would like to see next week?