cs412/413 introduction to compilers and translators spring ’99 lecture 11: functions and stack...

25
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

Upload: marsha-reeves

Post on 19-Jan-2016

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

CS412/413

Introduction to

Compilers and Translators

Spring ’99

Lecture 11: Functions and stack frames

Page 2: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

2

Administration

• Homework 2 due now

Page 3: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

3

Handling Recursion• Java, Iota: all global identifiers visible

through a module

• Need to create environment (symbol table) containing all of them for checking each function definition

• Global identifiers bound to their types

x: int {x : int}

• Functions bound to function types

gcd(x: int, y:int): int {gcd: int intint}

Page 4: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

4

Auxiliary environment info• Entries representing functions are not

normal environment entries

{gcd: int intint}

• int intint not a type in the type system : functions not first-class values in Iota

• Can’t use gcd as a variable name

• In general all kinds of information can be stuck into symbol table

Page 5: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

5

Handling Recursion• Need environment containing at least

{f: int int, g: int int}

when checking both f and g

• Two-pass approach:– Scan top level of AST picking up all function

signatures and creating an environment binding all global identifiers

– Type-check each function individually using this global environment

Page 6: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

6

Non-local control flow• How to check non-local control transfers?

– return, throw, break, continue

• Fits into existing framework for type checking (mostly)

• Question: what is the type of a return statement?

A |– return E : ?

Page 7: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

7

How to check return?A |– E : T

A |– return E : void

• A return statement has no value, so its type is void

• But… how to make sure the return type of the current function is T ?

Page 8: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

8

More auxiliary info• Answer: put it in the symbol table

• Add entry {return : int } when we start checking the function, look up this entry when we hit a return statement.

A’ = A { ..., ai : Ti, ... } {return : T} A’ |– E : T

A |– [ id ( ..., ai : Ti, ... ) : T = E ]

A |– E : T{return : T} A

A |– return E : void

Page 9: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

9

Summary• Static semantics provides a way to describe

semantic analysis concisely• If properly designed, leads to natural

recursive implementation

Page 10: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

10

Where we areSource code(character stream)

Lexical analysis

Syntactic Analysis

Token stream

Semantic Analysis

Intermediate CodeGeneration

Abstract syntax tree+ types

Abstract syntax tree

Intermediate Code

regular expressions

grammars

static semantics

translation functions

Page 11: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

11

Intermediate Code• Code for an abstract processor

• Processor-specific details avoided (e.g. # of registers)

• Generality enables optimization

• Tree representation conversion

if

==

int b int 0

=

int a int b

boolean int;

CJUMP ==

MEM

fp 8

+

CONST MOVE

0 MEM MEM

fp 4 fp 8

NOP

+ +

if (b==0) a = b;

Page 12: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

12

Variables in IR

if

==

int b int 0

=

int a int b

boolean int;

CJUMP ==

MEM

fp 8

-

CONST MOVE

0 MEM MEM

fp 4 fp 8

NOP

- -

• Variables mapped to memory locationsb Mem[fp - 8]

• How do we map them?

Page 13: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

13

IR Architecture• Infinite no. of general purpose registers• Stack pointer register (sp)• Frame pointer register (fp)• Program counter register (pc)• Versus Pentium:

– Very finite number of registers (EAX–EDX, ESI, EDI)– None really “general purpose”– Stack pointer (ESP), frame pointer (EBP), instruction

pointer (EIP)

• Versus MIPS, Alpha:– 32 general purpose registers (r0–r31) + PC

Page 14: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

14

Representing variables• Global variables: mapped to particular

locations in memory • Local variables, arguments: can’t map to fixed

locations because of recursion, threads

fact(x: int): int = {

if (x==0) 1; else x*fact(x-1);

}

where to store x?

Page 15: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

15

Stack• Local storage allocated on stack

– area of memory for storage specific to function invocations

– each function invocation: a new stack frame– same variable in different invocations stored in

different stack frames: no conflict

Page 16: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

16

Stack Frames• Program stack pointed to by

two registers– sp: stack pointer– fp: frame pointer

• New stack allocation at sp• Stack contents accessed relative

to fp• Positive offsets: function

arguments, static link to previous function

• Negative offsets: local storage, e.g. b fp - 8

spunused

fp

arguments

local varstemporaries

static link

increasingmemoryaddress

Page 17: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

17

Arguments (std. Pentium)gcd(x:int, y:int): int = { … }• Arguments part of calling stack frame• Pushed onto stack before return address

(positive offset from fp)• End of frame: static link

y

xret pc

call fpfp

+4

+8

+12

+16

callingframe

currentframe

Page 18: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

18

Local variablesgcd(x:int, y:int): int = {

if (x == 0) y; else {

if (x < y) { t:int = x; x = y; y = t; } gcd(x%y,y); }}

y

xret pcold fp

fp

+4+8

+12+16

ttemp1

temp1

+0-4

sp

callingframe

currentframe

t always at [fp+0]

Page 19: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

19

Making a callgcd(temp1,y)

y

xret pcold fp

fp

+4+8

+12+16

ttemp1

+0-4

sp

callingframe

currentframe

• Caller: push y push temp1 call function

push ret addr pc := gcd

On return:sp := sp + 8

ytemp1

ret addr

sp

Page 20: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

20

Entering a function• Callee: need to

establish new frame

• Push fp from calling frame

• Move sp to fp

• Adjust sp to make room for local variables

• On return:– move fp to sp

– pop fp

– return

y

xret pcold fp

fp+4+8

+12+16

ttemp1

+0-4

sp

callingframe

ytemp1

ret addrfp

fp

+8+12+16

tsp

currentframe

Page 21: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

21

Modern architectures• Pentium calling conventions (for C): lots of

memory traffic

• Modern processors: use of memory is much slower than register accesses

• Pentium has impoverished register set (4 somewhat general purpose registers)

• More registers better calling conventions?

Page 22: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

22

MIPS, Alpha calling conventions

• 32 registers!

• Up to 4 arguments (6 on Alpha) passed in registers

• Return address placed in register (r31)

• No frame pointer unless needed

• Local variables, temporary values placed in registers

Page 23: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

23

MIPS stack frame

sp

Caller:Use jal gcd, r31

leaf procedure:Return addr in r31, sp = r30K = max size of locals, tempsOn entry: sp := sp - KOn exit: sp := sp + K; ret r31fp = sp + K

non-leaf procedure:Put return addr on stackSave temporary registers on stack when making callOn entry: sp := sp - K;

[sp + K - 4] := r31On exit: r31 := [sp + K - 4];

sp += K; ret r31;

localsK

locals

ra

K

sp

Page 24: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

24

Mapping variables• Variables, temporaries assigned to locations

during intermediate code generation (& optimization)

– assigned to one of infinite no. registers initially– eventually allocated locations relative to fp

• Unoptimized code:– all locations are on stack– arguments pushed onto stack

Page 25: CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

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

25

Compiling Functions• Abstract machine for intermediate code

• Stack frames store state for functions

• Calling conventions– x86/Pentium: everything on stack– MIPS, Alpha: everything in registers

• Code transformations for intermediate code generation