cs412 413
Download
Skip this Video
Download Presentation
CS412/413

Loading in 2 Seconds...

play fullscreen
1 / 26

CS412/413 - PowerPoint PPT Presentation


  • 118 Views
  • Uploaded on

CS412/413. Introduction to Compilers and Translators March 10, 1999 Lecture 17: Finishing basic code generation. Outline. Implementing function calls Implementing functions Optimizing away the frame pointer Dynamically-allocated structures: strings and arrays

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' CS412/413' - amos-good


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cs412 413

CS412/413

Introduction to

Compilers and Translators

March 10, 1999

Lecture 17: Finishing basic code generation

outline
Outline
  • Implementing function calls
  • Implementing functions
  • Optimizing away the frame pointer
  • Dynamically-allocated structures: strings and arrays
  • Register allocation the easy way

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

function calls
Function calls
  • How to generate code for function calls?
  • Two kinds of IR statements:

MOVE

EXP

CALL

dest

CALL

f

e1 … en

f

e1 … en

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

stack layout
Stack layout

old pc

old pc

old bp

old bp

bp

bp

current stack

frame

locals

current stack

frame

locals

sp

en

sp

:

:

e1

sp

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

two translations

CALL

f

e1 … en

Two translations

old pc

old fp

fp

sub sp, 4*n

mov [sp + 4], e1

...

mov [sp + 4*n], en

call f

mov dest, eax

add sp, 4*n

locals

en

sp

:

:

RISC

e1

non-RISC

sp

push en

push e1

call f

mov dest, eax

add sp, 4*n

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

how to generate code

CALL

f

e1 … en

How to generate code?

push en

push e1

call f

mov dest, eax

add sp, 4*n

?

  • Problem: doesn’t fit into tiling paradigm; unbounded # tiles required

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

tiling calls

MOVE

dest

CALL

f

e1 … en

Tiling calls
  • Break down into simpler IR constructs

MOVE

dest

PUSH

PUSH

CALL

...

en

e1

f

  • Use tiles for push, call instructions to generate code (push r/m32, push imm, call addr, call r/m32)

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

compiling function bodies
Compiling function bodies
  • Translation to IR:

T [ f(…) : T = E ] = MOVE(RV, T[E])

T [ f(…) = E] = EXP(T[E])

  • Try it out:

f(x: int, y:int) = x + y

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

abstract assembly for f

f(x: int, y:int) = x + y

Abstract assembly for f

mov t1, [fp + x]

mov t2, [fp + y]

mov t3, t1

add t3, t2

mov eax, t3

MOVE

RV

+

MEM

MEM

+

+

FP

x

FP

y

What’s missing here?

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

stack frame setup
Stack frame setup
  • Need code to set up stack frame on entry

push bp

mov bp,sp

sub sp, 4*l

bp

prev. bp

prev

locals

prev. stack

frame

prev. stack

frame

locals

en

en

:

:

:

:

mov sp, bp

pop bp

e1

e1

prev pc

prev pc

sp

prev bp

new bp

new

locals

new stack

frame

new sp

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

function code
Function code

f: push bp

mov bp,sp

sub sp, 4*l

mov t1, [fp + x]

mov t2, [fp + y]

mov t3, t1

add t3, t2

mov eax, t3

mov sp, bp

pop bp

ret

function prologue

function epilogue

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

compiling return
Compiling return
  • Iota return statement returns immediately from function. Translation:

T [ return E; ] = SEQ(MOVE(RV, T[E]), JUMP (epilogue))

  • Every function f has epilogue label

f:

prologue

body

f_epilogue:

epilogue

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

glories of cisc
Glories of CISC

f: push bp enter 4*l, 0

mov bp, sp

sub sp, 4*l

mov t1, [fp + x] ...

mov t2, [fp + y] ...

mov t3, t1 ...

add t3, t2 ...

mov eax, t3 …

mov sp, bp leave

pop bp

ret ret

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

optimizing away bp
Optimizing away bp
  • Idea: maintain constant offset k between frame pointer and stack pointer
  • Use RISC-style argument passing rather than pushing arguments on stack
  • All references to FP+n translated to operand sp + (n + k) instead of to bp + n
  • Advantage: get whole extra register to use when allocating registers (7!)

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

stack frame setup1
Stack frame setup

bp

prev. bp

sub sp, 4*l

prev

locals

prev. stack

frame

prev. stack

frame

locals

en

en

:

:

:

:

add sp, 4*l

e1

e1

prev pc

prev pc

sp

new

locals

new stack

frame (size 4l)

max arg

space

new sp

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

caveats
Caveats
  • Get even faster (and RISC-core) prologue and epilogue than with enter/leave but:
  • Must save bp register if we want to use it (like sp, callee-save)
  • Doesn’t work if stack frame is truly variable-sized : e.g., alloca() call in C allocates variable-sized array on the stack -- not a problem in Iota where arrays heap-allocated

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

dynamic structures
Dynamic structures
  • Modern programming languages allow dynamically allocated data structures: strings, arrays, objects

C:char *x = (char *)malloc(strlen(s) + 1);

C++:Foo *f = new Foo(…);

Java:Foo f = new Foo(…);

String s = s1 + s2;

Iota:x: array[int] = new int[5] = 0;

String s = s1 + s2;

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

program heap

stack

Program Heap
  • Program has 4 memory areas: code segment, stack segment, static data, heap
  • Two typical memory layouts (OS-dep.):

sp

heap

static data

heap

pc

code

static data

stack

sp

pc

code

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

object allocation

stack

Object allocation
  • Dynamic objects allocated in the heap
    • array creation, string concatenation
    • malloc(n) returns new chunk of n bytes, free(x) releases memory starting at x
  • Constants staticallyallocated in data segment
    • string constants
    • assembler supports datasegment declarations

sp

heap

static data

pc

code

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

iota dynamic structures
Iota dynamic structures

a: array[T]

a = new T[n] = E;

a.length

a

elements

of

a

T temp = E;

a = malloc(n * 4 + 4);

MEM(a) = n;

a = a + 4;

for (i = 0; i < n; i++) {

MEM(a + 4*i) = temp;

}

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

trivial register allocation
Trivial register allocation
  • Can convert abstract assembly to real assembly easily (but generate bad code)
  • Allocate every temporary to location in the current stack frame rather than to a register
  • Every temporary stored in different place -- no possibility of conflict
  • Three registers needed to shuttle data in and out of stack frame (max. # registers used by one instruction) : e.g, eax, ebx, ecx

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

rewriting abstract code
Rewriting abstract code
  • Given instruction, replace every temporary in instruction with one of three registers
  • Add mov instructions before instruction to load registers properly
  • Add mov instructions after instruction to put data back onto stack (if necessary)

push t1  mov eax, [fp - t1off]; push eax

mov [fp+4], t3  ?

add t1, [fp - 4]  ?

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

result
Result
  • Simple way to get working code
  • Code is longer than necessary, slower
  • Also can allocate temporaries to registers until registers run out (3 temporaries on Pentium, 20+ on MIPS, Alpha)
  • Code generation technique actually used by some compilers when all optimization turned off (-O0)
  • Will use for Programming Assignment 3

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

summary
Summary
  • Now: complete code generation technique
  • Use tiling to perform instruction selection
  • Function code generated by gluing prologue, epilogue onto body
  • Dynamic structure allocation handled by relying on heap allocation routines (malloc)
  • Allocate temporaries to stack locations to eliminate use of unbounded # of registers

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

where we are
Where we are

High-level source code

Assembly code

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

further topics
Further topics
  • Generating better code
    • register allocation
    • optimization (high- and low-level)
  • Supporting language features
    • objects, polymorphism, function values
    • advanced GC techniques
    • dynamic linking, loading, & PIC
    • dynamic types and reflection
  • Compiler-like programs
    • source-to-source translation
    • interpreters

CS 412/413 Introduction to Compilers and Translators -- Spring \'99 Andrew Myers

ad