Lecture Outline. Stack machines The MIPS assembly language. Code Generation (I)



Similar documents
Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

Computer Systems Architecture

More MIPS: Recursion. Computer Science 104 Lecture 9

Instruction Set Architecture

Winter 2002 MID-SESSION TEST Friday, March 1 6:30 to 8:00pm

Code Generation & Parameter Passing

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

Instruction Set Architecture. or How to talk to computers if you aren t in Star Trek

Typy danych. Data types: Literals:

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

X86-64 Architecture Guide

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Instruction Set Design

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

Reduced Instruction Set Computer (RISC)

Assembly Language: Function Calls" Jennifer Rexford!

MIPS Assembly Code Layout

Review: MIPS Addressing Modes/Instruction Formats

Compilers I - Chapter 4: Generating Better Code

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

Assembly Language Programming

Instruction Set Architecture (ISA)

Intel 8086 architecture

Chapter 7D The Java Virtual Machine

CS2210: Compiler Construction. Runtime Environment

Translating C code to MIPS

Instruction Set Architecture (ISA) Design. Classification Categories

M A S S A C H U S E T T S I N S T I T U T E O F T E C H N O L O G Y DEPARTMENT OF ELECTRICAL ENGINEERING AND COMPUTER SCIENCE

MIPS Assembler and Simulator

EC 362 Problem Set #2

Machine-Code Generation for Functions

Introduction to MIPS Assembly Programming

Computer Architecture Lecture 2: Instruction Set Principles (Appendix A) Chih Wei Liu 劉 志 尉 National Chiao Tung University

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

Where we are CS 4120 Introduction to Compilers Abstract Assembly Instruction selection mov e1 , e2 jmp e cmp e1 , e2 [jne je jgt ] l push e1 call e

Computer Architecture Lecture 3: ISA Tradeoffs. Prof. Onur Mutlu Carnegie Mellon University Spring 2013, 1/18/2013

Chapter 2 Topics. 2.1 Classification of Computers & Instructions 2.2 Classes of Instruction Sets 2.3 Informal Description of Simple RISC Computer, SRC

LSN 2 Computer Processors

İSTANBUL AYDIN UNIVERSITY

Stack Allocation. Run-Time Data Structures. Static Structures

Computer Organization and Architecture

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

PROBLEMS (Cap. 4 - Istruzioni macchina)

Central Processing Unit (CPU)

COMP 303 MIPS Processor Design Project 4: MIPS Processor Due Date: 11 December :59

CSE 141 Introduction to Computer Architecture Summer Session I, Lecture 1 Introduction. Pramod V. Argade June 27, 2005

1 Classical Universal Computer 3

2) Write in detail the issues in the design of code generator.

An Overview of Stack Architecture and the PSC 1000 Microprocessor

5 MIPS Assembly Language

Chapter 5 Instructor's Manual

OAMulator. Online One Address Machine emulator and OAMPL compiler.

1 The Java Virtual Machine

VLIW Processors. VLIW Processors

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Introduction to MIPS Programming with Mars

picojava TM : A Hardware Implementation of the Java Virtual Machine

CS:APP Chapter 4 Computer Architecture Instruction Set Architecture. CS:APP2e

The Little Man Computer

CPU Organization and Assembly Language

Q. Consider a dynamic instruction execution (an execution trace, in other words) that consists of repeats of code in this pattern:

Habanero Extreme Scale Software Research Project

Hacking Techniques & Intrusion Detection. Ali Al-Shemery arabnix [at] gmail

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

l C-Programming l A real computer language l Data Representation l Everything goes down to bits and bytes l Machine representation Language

Instruction Set Architecture

Functional programming languages

Overview. CISC Developments. RISC Designs. CISC Designs. VAX: Addressing Modes. Digital VAX

Cloud Computing. Up until now

a storage location directly on the CPU, used for temporary storage of small amounts of data during processing.

THUMB Instruction Set

Chapter 4 Lecture 5 The Microarchitecture Level Integer JAVA Virtual Machine

What to do when I have a load/store instruction?

Computer organization

EE282 Computer Architecture and Organization Midterm Exam February 13, (Total Time = 120 minutes, Total Points = 100)

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

IA-64 Application Developer s Architecture Guide

Computer Organization and Components

umps software development

Recursive Fibonacci and the Stack Frame. the Fibonacci function. The Fibonacci function defines the Fibonacci sequence, using a recursive definition :

The previous chapter provided a definition of the semantics of a programming

The Java Virtual Machine and Mobile Devices. John Buford, Ph.D. Oct 2003 Presented to Gordon College CS 311

Solution: start more than one instruction in the same clock cycle CPI < 1 (or IPC > 1, Instructions per Cycle) Two approaches:

Lecture 3 Addressing Modes, Instruction Samples, Machine Code, Instruction Execution Cycle

A Tiny Guide to Programming in 32-bit x86 Assembly Language

The Operating System and the Kernel

Addressing The problem. When & Where do we encounter Data? The concept of addressing data' in computations. The implications for our machine design(s)

University of Twente. A simulation of the Java Virtual Machine using graph grammars

CHAPTER 7: The CPU and Memory

Module 2 Stacks and Queues: Abstract Data Types

Data Structure with C

A3 Computer Architecture

10CS35: Data Structures Using C

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

Z80 Instruction Set. Z80 Assembly Language

Administration. Instruction scheduling. Modern processors. Examples. Simplified architecture model. CS 412 Introduction to Compilers

Good FORTRAN Programs

We will use the accumulator machine architecture to demonstrate pass1 and pass2.

Transcription:

Lecture Outline Code Generation (I) Stack machines The MIPS assembl language Adapted from Lectures b Profs. Ale Aiken and George Necula (UCB) A simple source language Stack- machine implementation of the simple language CS81(Prasad) L23CG 1 CS81(Prasad) L23CG 2 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 CS81(Prasad) L23CG 3 Eample of Stack Machine Operation The addition operation on a stack machine 5 5 9 9 pop add 12 9 push CS81(Prasad) L23CG 4 Eample 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 + 5: push push 5 add Wh use a Stack Machine? Each operation takes operands from the same place and puts results in the same place Location of the operands and result implicit Alwas on the top of the stack This means a uniform compilation scheme and therefore a simpler compiler Eample: Instruction add as opposed to add r 1, r 2 Smaller encoding of instructions More compact programs This is one reason wh Java Btecodes use a stack evaluation model CS81(Prasad) L23CG 5 CS81(Prasad) L23CG 6

Optimizing the Stack Machine The add instruction does 3 memor operations Two reads and one write to the stack The top of the stack is frequentl 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 Onl one memor operation! Stack Machine with Accumulator Invariants The result of computing an epression is alwas 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 epression the stack is as before CS81(Prasad) L23CG CS81(Prasad) L23CG 8 Stack Machine with Accumulator. Eample Compute + 5 using an accumulator acc stack acc push acc acc 5 CS81(Prasad) L23CG 9 5 12 acc acc + top_of_stack pop A Bigger Eample: 3 + ( + 5) Code Acc Stack acc 3 3 <init> push acc 3 3, <init> acc 3, <init> push acc, 3, <init> acc 5 5, 3, <init> acc acc + top_of_stack 12, 3, <init> pop 12 3, <init> acc acc + top_of_stack 15 3, <init> pop 15 <init> CS81(Prasad) L23CG 10 Notes It is ver important that the stack is preserved across the evaluation of a subepression Stack before the evaluation of + 5 is 3, <init> Stack after the evaluation of + 5 is 3, <init> The first operand is on top of the stack 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 CS81(Prasad) L23CG 11 CS81(Prasad) L23CG 12

Simulating a Stack Machine The accumulator is kept in MIPS register $a0 The stack is kept in memor The stack grows towards lower addresses Standard convention on the MIPS architecture The address of the net location on the stack is kept in MIPS register $sp The top of the stack is at address $sp + 4 MIPS Assembl MIPS architecture Prototpical 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 memor 32 general purpose registers (32 bits each) We will use $sp, $a0 and $t1 (a temporar register) Read the IM handout for more details CS81(Prasad) L23CG 13 CS81(Prasad) L23CG 14 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 CS81(Prasad) L23CG 15 MIPS Assembl. Eample. The stack- acc push acc acc 5 acc acc + top_of_stack pop machine code for + 5 in MIPS: li $a0 li $a0 5 add $a0 $a0 $t1 We now generalize this to a simple language CS81(Prasad) L23CG 16 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 ) 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() = if = 1 then 0 else if = 2 then 1 else fib( - 1) + fib( 2) CS81(Prasad) L23CG 1 CS81(Prasad) L23CG 18

Code Generation Strateg (Invariant) For each epression 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 Code Generation for Constants The code to evaluate a constant simpl copies it into the accumulator: cgen(i) = li $a0 i Note that this also preserves the stack, as required CS81(Prasad) L23CG 19 CS81(Prasad) L23CG 20 Code Generation for Add cgen(e 1 + e 2 ) = add $a0 $t1 $a0 Possible optimization: Put the result of e 1 directl in register $t1? CS81(Prasad) L23CG 21 Code Generation for Add. Wrong! Optimization: Put the result of e 1 directl in $t1? cgen(e 1 + e 2 ) = move $t1 $a0 add $a0 $t1 $a0 Tr to generate code for : 3 + ( + 5) CS81(Prasad) L23CG 22 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 recursivedescent of the AST At least for epressions CS81(Prasad) L23CG 23 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 ) = sub $a0 $t1 $a0 CS81(Prasad) L23CG 24

Code Generation for Conditional Code Generation for If (Cont.) 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 cgen(if e 1 = e 2 then e 3 else e 4 ) = beq $a0 $t1 true_branch false_branch: cgen(e 4 ) b end_if true_branch: cgen(e 3 ) end_if: CS81(Prasad) L23CG 25 CS81(Prasad) L23CG 26 The Activation Record Code for function calls and function definitions depends on the laout of the activation record A ver simple AR suffices for this language: The result is alwas in the accumulator No need to store the result in the AR The activation record holds actual parameters For f( 1,, n ), push n,, 1 on the stack These are the onl variables in this language The Activation Record (Cont.) The stack discipline guarantees that on function eit $sp is the same as it was on function entr No need for a control link We need the return address It s hand to have a pointer to the current activation This pointer lives in register $fp (frame pointer) Reason for frame pointer will be clear shortl CS81(Prasad) L23CG 2 CS81(Prasad) L23CG 28 The Activation Record Code Generation for Function Call Summar: 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(,), The AR will be: old fp AR of f CS81(Prasad) L23CG 29 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 net instruction in $ra On other architectures, the return address is stored on the stack b the call instruction CS81(Prasad) L23CG 30

Code Generation for Function Call (Cont.) Code Generation for Function Definition cgen(f(e 1,,e n )) = sw $fp 0($sp) cgen(e n ) jal f_entr 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 btes long CS81(Prasad) L23CG 31 New instruction: jr reg Jump to address in register reg cgen(def f( 1,, n ) = e) = move $fp $sp sw $ra 0($sp) 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 saves the return address, to enable later calls. The callee finall restores the return address, pops the actual arguments, and restores the saved value of the frame pointer. z = 4*n + 8 CS81(Prasad) L23CG 32 Calling Sequence. Eample for f(,). Code Generation for Variables Before call On entr Before eit After call In caller old B caller old return CS81(Prasad) L23CG 33 B callee Popped b callee Variable references are the last construct The variables of a function are just its parameters The are all in the AR Pushed b the caller (and later popped b the callee) Problem: Because the stack grows when intermediate results are saved, the variables are not at a fied offset from $sp CS81(Prasad) L23CG 34 Code Generation for Variables (Cont.) Solution: use a frame pointer Alwas points to the return address on the stack Since it does not move, it can be used to find the variables Let i be the i th (i = 1,,n) formal parameter of the function for which code is being generated Code Generation for Variables (Cont.) Eample: For a function def f(,) = e the activation and frame pointer are set up as follows: old fp return X is at fp + 4 Y is at fp + 8 cgen( i ) = lw $a0 z($fp) ( z = 4*i ) CS81(Prasad) L23CG 35 CS81(Prasad) L23CG 36

Summar The activation record must be designed together with the code generator Code generation can be done b recursive traversal of the AST Use of a stack machine recommended for Cool compiler (it s simple) 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 CS81(Prasad) L23CG 3