CSE 131B – Compiler Construction II Discussion 5: SPARC Assembly (The Fun Stuff!) 2/14/2007 Happy Valentine’s Day!

Slides:



Advertisements
Similar presentations
Cosc 2150: Computer Organization
Advertisements

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.
Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
The University of Adelaide, School of Computer Science
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
10/6: Lecture Topics Procedure call Calling conventions The stack
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
SPARC Architecture & Assembly Language
I/O: SPARC Assembly Department of Computer Science Georgia State University Georgia State University Updated Spring 2014.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Lecture 8: MIPS Instruction Set
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.
CSE 131B – Compiler Construction II Discussion 8: Complex Data Structures 3/7/2007 Reminder: No class Thursday – lab hours instead.
CS 536 Spring Run-time organization Lecture 19.
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.
CSE 131B – Compiler Construction II Discussion 6: Operations, Branches and Functions 2/21/2007.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Microprocessors Frame Pointers and the use of the –fomit-frame-pointer switch Feb 25th, 2002.
CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.
Run-time Environment and Program Organization
Sparc Architecture Overview
CSC 3210 Computer Organization and Programming Chapter 9 EXTERNAL DATA AND TEXT D.M. Rasanjalee Himali.
Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs.
Fall 2008CS 334: Computer SecuritySlide #1 Smashing The Stack A detailed look at buffer overflows as described in Smashing the Stack for Fun and Profit.
CSC 3210 Computer Organization and Programming Chapter 2 SPARC Architecture Dr. Anu Bourgeois 1.
ITEC 352 Lecture 12 ISA(3). Review Buses Memory ALU Registers Process of compiling.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Languages and the Machine Chapter 5 CS221. Topics The Compilation Process The Assembly Process Linking and Loading Macros We will skip –Case Study: Extensions.
Natawut NupairojAssembly Language1 Memory and Stack.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Topic 2d High-Level languages and Systems Software
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
Lecture 4: MIPS Instruction Set
V 1.01 Arrays and Pointers in C A pointer variable is a variable that contains the address of another variable. An array is a collection of like elements,
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
CNIT 127: Exploit Development Ch 1: Before you begin.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
ARM-7 Assembly: Example Programs 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Intel Xscale® Assembly Language and C. The Intel Xscale® Programmer’s Model (1) (We will not be using the Thumb instruction set.) Memory Formats –We will.
Lecture 5: Procedure Calls
Lecture 6: Assembly Programs
MIPS Instruction Set Advantages
Lecture 4: MIPS Instruction Set
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Introduction to Compilers Tim Teitelbaum
Chapter 7 Subroutines Dr. A.P. Preethy
Assembly Programming using MIPS R3000 CPU
ARM Assembly Programming
Instructions - Type and Format
Ken D. Nguyen Department of Computer Science Georgia State University
Lecture 4: MIPS Instruction Set
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
CSC 3210 Computer Organization and Programming
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Lecture 6: Assembly Programs
Assembly Programming using MIPS R3000 CPU
Computer Architecture and System Programming Laboratory
Computer Architecture
Generalities for Assembly Language
Ken D. Nguyen Department of Computer Science Georgia State University
Presentation transcript:

CSE 131B – Compiler Construction II Discussion 5: SPARC Assembly (The Fun Stuff!) 2/14/2007 Happy Valentine’s Day!

Overview  Project II Overview  SPARC Architecture and Assembly  An Example of Code Generation

Project II Overview  For Project II, there will be no type checking Only syntactically and semantically correct code will be given to your compiler. You will most likely still need the structures you created in Project I, but need to make sure your Project I code isn’t printing error messages when there are none (false positives).

Project II Overview  So, what is Project II supposed to do? You will be generating SPARC assembly code for a given Oberon program (creating oberon.s) Once your compiler outputs an assembly program, it will be fed into a C compiler (“cc”) to create the resulting executable (a.out)

SPARC Architecture  SPARC is a RISC (Reduced Instruction Set Computer) Architecture This means there are a small number of simple instructions (as opposed to CISC). Load/Store Architecture (load-load-compute-store) Branching on Condition Codes Z (zero), N (negative), C (carry), V (overflow) bit integer registers (global, local, input, output) bit floating-point registers (we won’t be using these) Sliding Register Window

SPARC Memory Text Data BSS Heap Stack Low High  Text – Instructions  Data – Initialized global & static variables  BSS – Uninitialized global & static variables  Heap – Dynamically allocated memory  Stack – Stack Frames (local variables and procedure parameters) %fp (Frame Pointer) Local Vars Parameters High Low  Local variables at negative offset  Parameters at positive offset Stack Frame:

SPARC Integer Registers  Global (%g1 - %g7) Consistent throughout function calls %g0 is always 0 %g1 - %g4 are volatile (temp use only)  Local (%l0 - %l7) Values local to each procedure (disappear after procedure returns)  Input (%i0 - %i5) Input parameters to a procedure (this is why there is a 6 parameter limit issue) %i0 is where to store return value before returning %i6 and %i7 are reserved – don’t mess with them  Output (%o0 - %o5) Output arguments to a procedure (do right before “call”) Once procedure returns, return result is in %o0 %o6 and %o7 are reserved – don’t mess with them

Sliding Register Window  When main calls foo, main’s output registers will become foo’s input registers.  Also, as you can see, foo’s frame pointer is just main’s stack pointer. main foo

Common SPARC Instructions Set (no 4K restriction) – cannot be in delay slot!: set 12345, %l0! %l0 = Move (constants between +/- 4K OK): mov %l0, %o0! %o0 = %l0 Simple Arithmetic (add, sub): add %o0, %o1, %o2! %o2 = %o0 + %o1 Increment/Decrement (inc, dec): inc %l0! %l0 = %l0 + 1 Shifting (sll, srl, sra): sll %o1, 5, %o0! %o0 = %o1 << 5

Common SPARC Instructions  For *, DIV, and MOD, you should use the built-in “leaf” functions in SPARC (there is no actual instruction for these): “.mul”, “.div”, “.rem” Put the two operands in %o0 and %o1, call the function, then the result will be in %o0. mov 5, %o0 mov 7, %o1 call.rem nop ! Result is in %o0

The Save Instruction set -(92 + ?) & -8, %g1! ? = # of bytes of local variables save %sp, %g1, %sp  The reason we have the “set” instruction is to avoid the 4K pitfall of just placing the number in the “save” instruction.  The 92 comes from: 64 bytes for in/local registers 4 bytes for returning by value 24 bytes for first 6 parameters (%i0 - %i5)  What if you had a value parameter to foo that needed to be sent as a VAR parameter to baz? st%i0, [%fp+68]! Put value of parameter in memory parameter location add %fp, 68, %o0! Put address in output argument to baz.

Common SPARC Instructions Load: ld [%fp – 4], %i4! %i4 = *(%fp – 4) Store: st %i3, [%fp – 8]! *(%fp – 8) = %i3 Compare: cmp %o0, %o1! Sets condition codes based on %o0 - %o1 Branch (bg, bge, bl, ble, ba, etc)**: ble loop2! Go to label “loop2” IF prior cmp was <= Call**: call foo! Go to label “foo” ** Remember to have a “nop” after a branch or call statement!!!

Assembly Sections .text – Instructions .data – Data (Initialized global/static vars) .rodata – Read-only Data .bss – BSS (will be automatically set to 0)  You can switch between the different sections throughout the code (just don’t forget to align!)

Global Variables – Method 1  Oberon code: VAR x, y : INTEGER;  Assembly code:.section“.data”.align4 x:.word0! 0 to initialize to zero y:.word0! 0 to initialize to zero

Global Variables – Method 2  Oberon code: VAR x, y : INTEGER;  Assembly code:.section“.bss”! Will auto initialize to zero.align4 x:.space4! Same as.skip y:.skip4! Same as.space

Global Variables – Method 3  Assembly code:.section“.bss”.align4.skip4! y is %g7 - 8.skip4! x is %g7 - 4 globals: (Later somewhere in main) set globals, %g7 Now all global variables are at a negative offset of %g7. Useful, since it mimics local variables with regards to having negative offsets from some base pointer. Note, variables are listed in reverse order for this method.

Variables  One important thing we will need to know about variables will be their base and offset in memory.  For example, if X was at %fp – 8, you would want to store the following in X’s VarSTO: X.base = “%fp” X.offset = “-8”  Now, when you see X used in the code, you know to load from base+offset (i.e., [%fp-8])

Procedure Calls  In Oberon: foo(5, 9);  In Assembly: set 5, %o0; set 9, %o1; call foo nop

Useful Constants  Consider always defining the following useful constants:.section “.rodata” NL:.asciz “\n” intFmt:.asciz “%d” boolT:.asciz “TRUE” boolF:.asciz “FALSE” Note: Always use.asciz instead of.ascii, since the former will automatically null-terminate your ASCII string, which is necessary for printf.

Outputting Stuff  In Oberon: WRITE (5);  In Assembly: set intFmt, %o0! Assuming you have defined this set 5, %o1 call printf nop

Outputting Stuff  In Oberon: WRITE “hello world”, NL;  In Assembly:.section“.data”.align4 tmp1:.asciz“hello world” NL:.asciz“\n”.section“.text”.align4 settmp1, %o0 callprintf nop setNL, %o0 callprintf nop

A Large Example  Given this Oberon Code, what is the assembly? VAR X : INTEGER; VAR Y : INTEGER; VAR Z : INTEGER; CONST C = 5; BEGIN X := 4; Y := 11; Z := C - Y; Z := Z + X; WRITE Z, NL; END.

A Large Example !----Global Variables----.section “.bss”.align 4.skip 4 ! Z at %g7-12.skip 4 ! Y at %g7-8.skip 4 ! X at %g7-4 globals: !----Default String Formatters----.section “.data” intFmt:.asciz “%d” NL:.asciz“\n” !----Main----.section “.text”.align 4.global main

A Large Example main: set SAVE.main, %g1 save %sp, %g1, %sp set globals, %g7 ! X := 4 set 4, %l0 st %l0, [%g7-4] ! Y := 11 set 11, %l0 st %l0, [%g7-8] ! Z := C - Y (where C = 5) set 5, %l0 ld [%g7-8], %l1 sub %l0, %l1, %l0 st %l0, [%g7-12] ! Z := Z + X ld [%g7-12], %l0 ld [%g7-4], %l1 add %l0, %l1, %l0 st %l0, [%g7-12] ! WRITE set intFmt, %o0 ld [%g7-12], %o1 call printf nop set NL, %o0 call printf nop ret restore SAVE.main = -(92 + 0) & -8

Where do you do this?  There are many ways to output your assembly code. Try to be as organized as possible – don’t just throw some println statements all over your CUP and OParser files. Consider making a separate class that just deals with outputting code Evan (a previous tutor) provides an excellent idea for a possible class. See the website for the link under Project 2. Make sure to use ample formatting (tabs, blank lines, comments), as this will help you greatly with debugging.

What to do Next! 1.Fix Project I – ensure there are no errors generated. 2.Familiarize yourself with SPARC assembly. 3.Plan out how you want to structure your project – good planning leads to an easier design in the long run. 4.Start on Phase 1. 5.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?