CS 536 Spring 20011 Code generation I Lecture 20.

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.
The University of Adelaide, School of Computer Science
Lecture 9: MIPS Instruction Set
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
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.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Procedure Calls Prof. Sirer CS 316 Cornell University.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
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.
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
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
Intro to Computer Architecture
CS 536 Spring Code Generation II Lecture 21.
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
4/6/08Prof. Hilfinger CS164 Lecture 291 Code Generation Lecture 29 (based on slides by R. Bodik)
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.
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
Linked Lists in MIPS Let’s see how singly linked lists are implemented in MIPS on MP2, we have a special type of doubly linked list Each node consists.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Compiler Run-time Organization Lecture 7. 2 What we have covered so far… We have covered the front-end phases –Lexical analysis –Parsing –Semantic analysis.
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.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Lecture 4: MIPS Instruction Set
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
CS 164 Lecture 151 Code Generation (I) ICOM4029 Lecture 9.
Function Calls in Assembly MIPS R3000 Language (extensive use of stack) Updated 7/11/2013.
Computer Architecture & Operations I
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
© Craig Zilles (adapted from slides by Howard Huang)
CSCI206 - Computer Organization & Programming
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Introduction to Compilers Tim Teitelbaum
Assembly Programming using MIPS R3000 CPU
Instructions - Type and Format
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012
Code Generation Lecture 12 CS 164 Lecture 14 Fall 2004.
Lecture 30 (based on slides by R. Bodik)
MIPS Instructions.
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Topic 3-a Calling Convention 1/10/2019.
10/4: Lecture Topics Overflow and underflow Logical operations
COMS 361 Computer Organization
Program and memory layout
Operating Systems Run-time Organization
Procedures and Calling Conventions
COMS 361 Computer Organization
Assembly Programming using MIPS R3000 CPU
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
© Craig Zilles (adapted from slides by Howard Huang)
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

CS 536 Spring Code generation I Lecture 20

CS 536 Spring Lecture Outline Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language

CS 536 Spring Stack Machines A simple evaluation model No variables or registers A stack of values for intermediate results 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

CS 536 Spring Example of Stack Machine Operation The addition operation on a stack machine … … pop  add 12 9 … push

CS 536 Spring Example 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 add

CS 536 Spring Why Use a Stack Machine ? Each operation takes operands from the same place and puts results in the same place This means a uniform compilation scheme And therefore a simpler compiler

CS 536 Spring Why 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 r 1, r 2 ”  Smaller encoding of instructions  More compact programs This is one reason why Java Bytecodes use a stack evaluation model

CS 536 Spring Optimizing 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 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!

CS 536 Spring Stack Machine with Accumulator Invariants The result of computing an expression is always in the accumulator For an operation op(e 1,…,e n ) push the accumulator on the stack after computing each of e 1,…,e n-1 –After the operation pop n-1 values After computing an expression the stack is as before

CS 536 Spring Stack Machine with Accumulator. Example Compute using an accumulator … acc stack 5 7 … acc  5 12 …  acc  acc + top_of_stack pop … 7 acc  7 push acc 7

CS 536 Spring A Bigger Example: 3 + (7 + 5) Code Acc Stack acc  3 3 push acc 3 3, acc  7 7 3, push acc 7 7, 3, acc  5 5 7, 3, acc  acc + top_of_stack 12 7, 3, pop 12 3, acc  acc + top_of_stack 15 3, pop 15

CS 536 Spring Notes It is very important that the stack is preserved across the evaluation of a subexpression –Stack before the evaluation of is 3, –Stack after the evaluation of is 3, –The first operand is on top of the stack

CS 536 Spring From Stack Machines to MIPS The compiler generates code for a stack machine with accumulator We want to run the resulting code on the MIPS processor (or simulator) We simulate stack machine instructions using MIPS instructions and registers

CS 536 Spring Simulating a Stack Machine… The accumulator is kept in MIPS register $a0 The stack is kept in memory The stack grows towards lower addresses –Standard convention on the MIPS architecture The address of the next location on the stack is kept in MIPS register $sp –The top of the stack is at address $sp + 4

CS 536 Spring MIPS Assembly MIPS architecture –Prototypical Reduced Instruction Set Computer (RISC) 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 SPIM tutorial for more details

CS 536 Spring A Sample of MIPS Instructions –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 536 Spring MIPS Assembly. Example. The stack-machine code for in MIPS: 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 536 Spring 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 536 Spring 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 536 Spring Code Generation Strategy For each expression e we generate MIPS 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 536 Spring Code Generation for Constants The code to evaluate a constant simply copies it into the accumulator: cgen(i) = li $a0 i Note that this also preserves the stack, as required

CS 536 Spring Code Generation for Add cgen(e 1 + e 2 ) = cgen(e 1 ) sw $a0 0($sp) addiu $sp $sp -4 cgen(e 2 ) lw $t1 4($sp) add $a0 $t1 $a0 addiu $sp $sp 4 Possible optimization: Put the result of e 1 directly in register $t1 ?

CS 536 Spring Code Generation for Add. Wrong! Optimization: Put the result of e 1 directly in $t1? cgen(e 1 + e 2 ) = cgen(e 1 ) move $t1 $a0 cgen(e 2 ) add $a0 $t1 $a0 Try to generate code for : 3 + (7 + 5)

CS 536 Spring Code Generation Notes The code for + is a template with “holes” for code for evaluating e 1 and e 2 Stack machine code generation is recursive Code for e 1 + e 2 consists of code for e 1 and e 2 glued together Code generation can be written as a recursive- descent of the AST –At least for expressions

CS 536 Spring Code Generation for Sub and Constants New instruction: sub reg 1 reg 2 reg 3 –Implements reg 1  reg 2 - reg 3 cgen(e 1 - e 2 ) = cgen(e 1 ) sw $a0 0($sp) addiu $sp $sp -4 cgen(e 2 ) lw $t1 4($sp) sub $a0 $t1 $a0 addiu $sp $sp 4

CS 536 Spring Code Generation for Conditional We need flow control instructions New instruction: beq reg 1 reg 2 label –Branch to label if reg 1 = reg 2 New instruction: b label –Unconditional jump to label

CS 536 Spring Code Generation for If (Cont.) cgen(if e 1 = e 2 then e 3 else e 4 ) = cgen(e 1 ) sw $a0 0($sp) addiu $sp $sp -4 cgen(e 2 ) lw $t1 4($sp) addiu $sp $sp 4 beq $a0 $t1 true_branch false_branch: cgen(e 4 ) b end_if true_branch: cgen(e 3 ) end_if:

CS 536 Spring The Activation Record Code for function calls and function definitions depends on the layout of the activation record A very simple AR suffices for this language: –The result is always in the accumulator No need to store the result in the AR –The activation record holds actual parameters For f(x 1,…,x n ) push x n,…,x 1 on the stack These are the only variables in this language

CS 536 Spring The Activation Record (Cont.) The stack discipline guarantees that on function exit $sp is the same as it was on function entry –No need for a control link We need the return address It’s handy to have a pointer to the current activation –This pointer lives in register $fp (frame pointer) –Reason for frame pointer will be clear shortly

CS 536 Spring The Activation Record Summary: For this language, an AR with the caller’s frame pointer, the actual parameters, and the return address suffices Picture: Consider a call to f(x,y), The AR will be: y x old fp SP FP AR of f

CS 536 Spring Code Generation for Function Call The calling sequence is the instructions (of both caller and callee) to set up a function invocation New instruction: jal label –Jump to label, save address of next instruction in $ra –On other architectures the return address is stored on the stack by the “call” instruction

CS 536 Spring Code Generation for Function Call (Cont.) cgen(f(e 1,…,e n )) = sw $fp 0($sp) addiu $sp $sp -4 cgen(e n ) sw $a0 0($sp) addiu $sp $sp -4 … cgen(e 1 ) sw $a0 0($sp) addiu $sp $sp -4 jal f_entry The caller saves its value of the frame pointer Then it saves the actual parameters in reverse order The caller saves the return address in register $ra The AR so far is 4*n+4 bytes long

CS 536 Spring Code Generation for Function Definition New instruction: jr reg –Jump to address in register reg cgen(def f(x 1,…,x n ) = e) = move $fp $sp sw $ra 0($sp) addiu $sp $sp -4 cgen(e) lw $ra 4($sp) addiu $sp $sp z lw $fp 0($sp) jr $ra Note: The frame pointer points to the top, not bottom of the frame The callee pops the return address, the actual arguments and the saved value of the frame pointer z = 4*n + 8

CS 536 Spring Calling Sequence. Example for f(x,y). Before call On entry Before exit After call SP FP y x old fp SP FP SP FP SP return y x old fp FP

CS 536 Spring Code Generation for Variables Variable references are the last construct The “variables” of a function are just its parameters –They are all in the AR –Pushed by the caller Problem: Because the stack grows when intermediate results are saved, the variables are not at a fixed offset from $sp

CS 536 Spring Code Generation for Variables (Cont.) Solution: use a frame pointer –Always points to the return address on the stack –Since it does not move it can be used to find the variables Let x i be the i th (i = 1,…,n) formal parameter of the function for which code is being generated cgen(x i ) = lw $a0 z($fp) ( z = 4*i )

CS 536 Spring Code Generation for Variables (Cont.) Example: For a function def f(x,y) = e the activation and frame pointer are set up as follows: y x return old fp X is at fp + 4 Y is at fp + 8 FP SP

CS 536 Spring Summary The activation record must be designed together with the code generator Code generation can be done by recursive traversal of the AST Production compilers do different things –Emphasis is on keeping values (esp. current stack frame) in registers –Intermediate results are laid out in the AR, not pushed and popped from the stack Next time: code generation for objects