Docsity

Code Generation - Compiler Construction - Lecture Slides, Slides of Compiler Construction

Partial preview of the text

Download Code Generation - Compiler Construction - Lecture Slides and more Slides Compiler Construction in PDF only on Docsity!

Code Generation (I)

Lecture Outline

  • Stack machines
  • The MIPS assembly language
  • A simple source language
  • Stack-machine implementation of the simple language

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

Stack Machine. Example

  • 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

stack …

5 7 …

push 5

12 …

…

push 7

7

add

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

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!

Stack Machine with Accumulator. Example

  • Compute 7 + 5 using an accumulator

…

acc

stack

5

7 …

acc  5

12

…

acc  acc + top_of_stack pop

…

7

acc  7 push acc

7

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

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 implement stack machine instructions using MIPS instructions and registers

Simulating a Stack Machine…

  • The accumulator is kept in MIPS register $a
  • 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

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

MIPS Assembly. Example.

  • The stack-machine code for 7 + 5 in MIPS:

acc  7 push acc

acc  5 acc  acc + top_of_stack

pop

li $a0 7 sw $a0 0($sp) addiu $sp $sp - li $a0 5 lw $t1 4($sp) add $a0 $a0 $t addiu $sp $sp 4

  • We now generalize this to a simple language…

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 ,…,En)

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)