CPSC 388 – Compiler Design and Construction Code Generation.

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

Intermediate Code Generation
The University of Adelaide, School of Computer Science
Deeper Assembly: Addressing, Conditions, Branching, and Loops
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
SPIM and MIPS programming
1 Compiler Construction Intermediate Code Generation.
Computer Architecture CSCE 350
MIPS Function Continued
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
MIPS Assembly Language Programming
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
1 Computer Architecture MIPS Simulator and Assembly language.
The University of Adelaide, School of Computer Science
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
Procedure call frame: Hold values passed to a procedure as arguments
1 COMS 361 Computer Organization Title: Instructions Date: 9/28/2004 Lecture Number: 10.
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
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)
1 Handling nested procedures Method 1 : static (access) links –Reference to the frame of the lexically enclosing procedure –Static chains of such links.
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
CS 536 Spring Code generation I Lecture 20.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
20/06/2015CSE1303 Part B lecture notes 1 Functions, part 2 Lecture B15 Lecture notes, section B15.
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Compiler Construction
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Higher Level languages have adopted a standard Referred to as C-style calling Uses the stack.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
MIPS Calling Convention. Procedure Calls Procedure must work the same from any call Procedure uses regs that main was using We need a convention to –pass.
1 Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Lecture 4: MIPS Instruction Set
Subroutines, parameters and the stack Bryan Duggan.
MicroComputer Engineering IntroLab1 page 1 Introduction Lab1  A crash course in assembler programming  Learn how a processor works!  Decode a coded.
More on MIPS programs n SPIM does not support everything supported by a general MIPS assembler. For example, –.end doesn’t work Use j $ra –.macro doesn’t.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
Computer Architecture & Operations I
Computer Architecture & Operations I
MIPS Assembly Language Programming
Lecture 4: MIPS Instruction Set
Procedures (Functions)
Instructions - Type and Format
Chapter 6 Intermediate-Code Generation
MIPS Instructions.
The University of Adelaide, School of Computer Science
10/4: Lecture Topics Overflow and underflow Logical operations
MIPS function continued
UNIT V Run Time Environments.
COMS 361 Computer Organization
Program and memory layout
Lecture 6: Assembly Programs
Systems Architecture I
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
Presentation transcript:

CPSC 388 – Compiler Design and Construction Code Generation

 Global Variables  Functions (entry and exit)  Statements  Expressions  Assume only scalar variables, no arrays (you figure out arrays)  Generate MIPS assembly code for Spim 

Spim Interpreter spim –file  is the name file containing MIPS assembly code  Program will run, giving output and errors to screen  Also has graphical interface

Some Spim Registers RegisterPurpose $spStack pointer $fpFrame pointer $raReturn address $v0, $a0Used for output and return value $t0-$t7temporaries

Helps for Generating Assembly Code  Constants Used SP, FP, RA, V0, A0, T0, T1, TRUE, FALSE  Methods Used generate(opcode, arg1, arg2, arg3) generateIndexed(opcode,R1,R2,offset) genPush(R1) genPop(R1) String nextLabel() genLabel(label)

Global Variables  For each global variable, v:.data.align 2 # align on word boundary _v:.space N N is the size of the variable in bytes  int: 4 bytes  arrays: 4*(size of array)

Global Variable Example  Give source code int x; int y[10];  Generate code.data.align 2 _x:.space 4.data.align 2 _y:.space 40

Code Generation for Functions  For each Function Function preamble Function entry (setup AR) Function body (function’s statements) Function exit (restore stack, return to caller)

Function Preamble  For the main function generate:.text.globl main main:  All other functions:.text _ : Where is the function name

Function Entry Caller’s AR parameters <- FP <- SP Caller’s AR parameters <- FP <- SP return add Control link Space for Local vars New AR

Function Entry Steps  Push RA sw$ra, 0($sp) subu$sp, $sp, 4  Push CL sw$fp, 0($sp) subu$sp, $sp, 4  Set FP addu$fp, $sp,  Push space for local vars subu$sp, $sp,

Function Body  No need for code from DeclListNode  Call codeGen() for statement nodes in StmtListNode FnBodyNode DeclListNodeStmtListNode

Function Exit  Pop AR  Jump to RA field lw$ra, - ($fp) move$t0, $fp lw$fp, - ($fp) move$sp, $t0 jr$ra Caller’s AR parameters <- FP <- SP return add Control link Space for Local vars New AR

Function Returns  Return statement  End of function  Two ways to handle this Generate return code once and have return statements jump to this code (op code is ‘b’ for branch) Generate return code for each return statement and end of function

Return Statement’s value  Return statements can return a a value from an ExpNode  ExpNodes will push values onto the stack  Return statement should pop the top of stack and place return value in register V0 before the rest of return code

Statements  Write a different codeGen() method for each kind of statement in AST  Hard to debug assembly code  Alternate method: Write codeGen() for the WriteIntStmtNode and WriteStrStmtNode classes first (maybe one method) Test codeGen() for other kinds of statements and expressions by writing a c- program that computes and prints a value.

Write Statement  Call codeGen for expression being printed Leaves value on top of stack (if int) Leaves address on top of stack (if String)  Pop top of stack into A0 (register used for output)  Set register V0 to  1 of int  4 if String  Generate: syscall

Write Statement Example myExp.codeGen(); genPop(A0); if ( type is int) generate(“li”,V0,1); else if (type is String) generate(“li”,V0,4); generate(“syscall”);

If Statement Two Methods for Generating code Numeric Method Control-Flow Method IfStmtNode DeclListNodeStmtListNodeExpNode

Numeric Method for If Statements  Evaluate the condition, leave value on stack  Pop top of stack into register T0  Jump to FalseLabel if T0==FALSE  Code for statement list  FalseLabel: Note: Every Label in assembly code must be unique! I’m Using FalseLabel but the actual label is generated using genLabel()

You Try It  Write the actual code needed for IfStmtNode  What is the form for IfElseStmtNode?  What is the form for WhileStmtNode?

Return Stmt  Call codeGen() for expNode child (leaves result value on stack)  Pop value off stack into V0  Generate code for actual return Pop AR Jump to address in RA ReturnStmtNode ExpNode ReturnStmtNode

Read Statement  Code: li$v0, 5 syscall  Loads special value 5 into register V0, then does syscall.  5 tells syscall to read in an integer and store it back in V0  Need to write code to copy value from V0 back into address represented by ExpNode ReadStmtNode ExpNode

ReadStmtNode Examples int x; int *p; int **q; *q=p=&x; scanf(“%d”,&x); scanf(“%d”,p); scanf(“%d”,*q);  All three calls to scanf read in a value into variable x. The value of the expression is the address of x.  To store value into address do: Generate code to compute value of expression (value is pushed onto stack) Pop the value into T0 Store from V0 to address in T0

ReadStmtNode Example generate(“li”,V0,5); generate(“syscall”); myExp.codeGen(); genPop(T0); generateIndexed(“sw”,V0,T0,0);

Identifiers in Code Generation  Function call (id is name of function)  Expressions (can be just a name (id) or an id can be one of the operands)  Assignment statements (id of left- hand side) Need to jump-and-link to instruction using the name of function Generate code to fetch current value and push onto stack Generate code to fetch the address of variable and push address onto stack

IdNode  Needs several methods genJumpAndLink() generate jump and link code for given IdNode codeGen() pushes value of IdNode expression onto stack genAddr() pushes address of IdNode onto stack

genJumpAndLink() for IdNode  simply generate a jump-and-link instruction (with opcode jal ) using label as target of the jump.  If the called function is "main", the label is just "main". For all other functions, the label is of the form: _

codeGen() for IdNode  copy the value of the global / local variable into a register (e.g., T0), then push the value onto the stack  Different for local or global variables Examples: lw $t0 _g // load global g into T0 lw $t0 -4($fp) // load local into T0  How do you tell if variable is local or global? – Using Symbol Table

genAddr() for IdNode  load the address of the identifier into a register then push onto the stack  Uses opcode for loading address la rather than loading values lw  Different for locals or globals Examples: la $t0, _g // global la $t0, -8($fp) // local

AssignStmtNode  Push the address of the left-hand-side expression onto the stack.  Evaluate the right-hand-side expression, leaving the value on the stack.  Store the top-of-stack value into the second-from-the top address. AssignStmtNode ExpNode

Expression Node codeGen  Always generate code to leave value of expression on top of stack  Literals IntLitNode, StrLitNode  Function Call  Non short-circuited operators  Short-circuited operators

IntLitNode  generate code to push the literal value onto the stack  Generated code should look like: li $t0, # load value into T0 sw $t0, ($sp) # push onto stack subu $sp, $sp, 4

StrLitNode  Store string literal in data area  Push address of string onto stack  Two string lits should be equal if they contain the same characters  This means store only a single instance of a string literal no matter how often it appears in user code

Storing String Literals  Code to store a string literal in data area.data :.asciiz  needs to be a new label; e.g., returned by a call to nextLabel.  needs to be a string in quotes. You should be storing string literals that way, so just write out the value of the string literal, quotes and all.

Storing Strings Once  To avoid storing the same string literal value more than once, keep a hashtable in which the keys are the string literals, and the associated information is the static- data-area label.  When you process a string literal, look it up in the hashtable: if it is there, use its associated label; otherwise, generate code to store it in the static data area, and add it to the hashtable.

Pushing StrLitNodes onto stack  Generated Code:.text la $t0, #load addr into $t0 sw $t0, ($sp) #push onto stack subu $sp, $sp, 4

CallExpNode  Code Should: Evaluate each actual parameter, pushing the values onto the stack; Jump and link (jump to the called function, leaving the return address in the RA register). Push the returned value (which will be in register V0) onto the stack. CallExpNode ExpListNodeIdNode  Since the codeGen method for an expression generates code to evaluate the expression, leaving the value on the stack, all we need to do for step 1 is call the codeGen method of the ExpListNode (which will in turn call the codeGen methods of each ExpNode in the list). For step 2, we just call the genJumpAndLink method of the IdNode. For step 3, we just call genPush(V0).

Also CallStmtNode  CallExpNode pushes value onto stack (may be void, i.e. garbage from V0)  CallStmtNode MUST pop value off stack CallExpNode ExpListNodeIdNode CallStmtNode

Non-Short Circuited ExpNodes  Plus, Minus, …, Not, Less, Equals,…  All do Same basic sequence of tasks Call each child's codeGen method to generate code that will evaluate the operand(s), leaving the value(s) on the stack. Generate code to pop the operand value(s) off the stack into register(s) (e.g., T0 and T1). Remember that if there are two operands, the right one will be on the top of the stack. Generate code to perform the operation (see Spim documentation for a list of opcodes). Generate code to push the result onto the stack.

Note on SPIM op-codes  The NOT opcode is a bit-wise note (flips bits), this won’t work for the Not boolean operations  Suggest using seq opcode SeqRdest, Rsrc1, Src2

Example AddExpNode public void codeGen() { // step 1: evaluate both operands myExp1.codeGen(); myExp2.codeGen(); // step 2: pop values in T0 and T1 genPop(T1); genPop(T0); // step 3: do the addition (T0 = T0 + T1) generate("add", T0, T0, T1); // step 4: push result genPush(T0) } AddExpNode ExpNode

Short-Circuited Operators  AndNode and OrNode  Short-Circuit means the right operand is evaluated ONLY if it is needed to be evaluated  Example: (J != 0) && (I/J > Epsilon)

AndNode Procedure Evaluate left operand If left operand is true then Evaluate right operand Expression value is value of right operand Else Expression value is false

OrNode Procedure Evaluate left operand If left operand is false evaluate right operand expression is value of right operand Else expression is true

Short-Circuit Nodes  Need to do jump depending on values of sub-expressions  Look at if-node code for example of this

You Try It  Write code for AndExpNode

If Statement Two Methods for Generating code Numeric Method  Evaluate condition, pop off stack, jump on particular value Control-Flow Method  Evaluate condition and jump to TrueLabel on true or FalseLabel on false (i.e. ALWAYS do a jump)  Requires a new method for Expression Nodes (i.e. don’t put value on the stack, instead do jump)  Call New method genJumpCode(LabelTrue,LabelFalse) IfStmtNode DeclListNodeStmtListNodeExpNode

codeGen for IfStmtNode (control- flow method) public void codeGen() { String trueLab = nextLabel(); String doneLab = nextLab(); myExp.genJumpCode(trueLab, doneLab); genLabel(trueLab); myStmtList.codeGen(); genLabel(doneLab); }

genJumpCode() for IdNode  Old way lw$t0, push$t0  New way lw$t0, beq$t0, FALSE, falseLab btrueLab

genJumpCode() for LessNode  Old Way -- code to eval operands -- pop values into T1, T0 slt$t2, $t0, $t1 push$t2  New Way -- code to eval operands -- pop values into T1, T0 blt $t0, $t1, trueLab B falseLab

genJumpCode() for Short-Circuited Operators (AndExpNode)  Call genJumpCode() of left child. If child is false then jump to false label If child is true jump to right child  Generate label for right child  Call genJumpCode() of right child. If child is false jump to false label If child is true jump to true label AndExpNode ExpNode

genJumpCode() for AndExpNode Public void genJumpCode(String trueLab, String falseLab) { String newLab=nextLabel(); myExp1.genJumpCode(newLab,falseLab); genLabel(newLab); myExp2.genJumpCode(trueLab,falseLab); }

Example with genJumpCode If (a && b>0) { … IfStmtNode DeclListNode AndExpNode StmtListNode IdNodeLessExpNode … …

genJumpCode() Example  IfStmtNode creates two labels, trueLab, doneLab calls AndNode’s genJumpCode(trueLabel,doneLabel) Generate trueLab --code for StmtListNode Generate doneLabel  AndNode creates a label newLabel, calls IdNode’s genJumpCode(newLabel,doneLabel) Generate newLabel Call LessNode’s genJumpCode(trueLab,doneLab)

You Try It  What is the form of the code for genJumpCode() For an OrNode For a NotNode

Comparing Numeric and Control- Flow methods  Numeric Method -- code to evaluate left operand, leaving value on stack Pop into T0 Goto trueLab if t0==FALSE Push FALSE Goto doneLab trueLab: -- code to evalute right operand, leaving value on stack doneLab:  Control-Flow Method --code to evaluate left operand, including jumps to newLab and falseLab newLab: --code to evaluate right operand, including jumps to trueLab and falseLab

You Try It  Compare two approaches for OrNode and NotNode