Activation Records (Introduction)

45
Activation Records (Introduction) Mooly Sagiv html://www.math.tau.ac.il/~msagiv/ courses/wcc03.html Chapter 6.3

description

Activation Records (Introduction). Mooly Sagiv html://www.math.tau.ac.il/~msagiv/courses/wcc03.html Chapter 6.3. Outline. What is the problem? A possible structure of the activation records A simple stack machine Example compilation. The problem. - PowerPoint PPT Presentation

Transcript of Activation Records (Introduction)

Page 1: Activation Records (Introduction)

Activation Records(Introduction)

Mooly Sagiv

html://www.math.tau.ac.il/~msagiv/courses/wcc03.html

Chapter 6.3

Page 2: Activation Records (Introduction)

Outline

• What is the problem?

• A possible structure of the activation records

• A simple stack machine

• Example compilation

Page 3: Activation Records (Introduction)

The problem

• The compiler needs to allocate memory for variables

• Consistent with program semantics– Scope– Duration– Recursion

• Efficient (moderate runtime cost)• Solution?

Page 4: Activation Records (Introduction)

Typical Virtual Memory Content

Lower addresses

static area

Stack

area

Heap

area

Page 5: Activation Records (Introduction)

A Typical Stack Frame

Page 6: Activation Records (Introduction)

Basic Compiler Phases

Page 7: Activation Records (Introduction)

void main() {printf(“%d\n”, fact(3));}int fact(int n){ if (n==0) return 1; else return n * fact(n-1) ;}

Example Program

Page 8: Activation Records (Introduction)

void main() {printf(“%d\n”, fact(3));}

Activation Record for main(before fact)

Administrative part

3SP

FP

Page 9: Activation Records (Introduction)

void main() {printf(“%d\n”, fact(3));}

Activation Record for main(after fact)

Administrative part

3

6 SP

FP

Page 10: Activation Records (Introduction)

void main() {printf(“%d\n”, fact(3));}

Activation Record for main(before printf)

Administrative part

6

x87 SP

FP

x87 “%d\n”

Page 11: Activation Records (Introduction)

Stack Instructions

Instruction Actions

Push_Const c stack[--SP]=c

Push_Local i stack[--SP]=stack[FP+i]

Store_Local i stack[FP+i]=stack[SP++]

Add_Top2 stack[SP+1]+=stack[SP++]

Sub_Top2 stack[SP+1]-=stack[SP++]

Mul_Top2 stack[SP+1]*=stack[SP++]

Branch L PC = L;

Branch_Eq L T1= stack[SP++]; T2 = stack[SP++];

if (T1==T2) PC =L

JSR L stack[--SP] = FP; stack[--SP] = PC; FP=SP-1;PC =L

RTS T= stack[SP++]; PC=stack[SP++]; FP=stack[SP++]; stack[--SP]=T

WPop Stack[SP+1] = Stack[SP]

Page 12: Activation Records (Introduction)

void main() {printf(“%d\n”, fact(3));}

.global _main

L1: Push_Const 3

Push_Const 0

JSR _fact

WPop

Push_Const L2

JSR _printf

.data

L2 : “%d\n”

.end

Code Generated for main

Page 13: Activation Records (Introduction)

Activation Record for fact

Administrative part

SP

FP

int fact(int n){ if (n==0) return 1; else return n * fact(n-1) ;}

n 3ret

FP+5

0

Page 14: Activation Records (Introduction)

Code generated for fact

int fact(int n){ if (n==0) return 1; else return n * fact(n-1) ;}

L4: Push_Constant 1

RTS

.global _fact

L3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L5: Push_Local 5

Push_Local 5

Push_Const 1

Subtr_Top 2

Push_Const 0

JSR _fact

WPop

Mult_Top2

RTS

.end

Page 15: Activation Records (Introduction)

Execution of fact

Administrative part FP

n 3ret

FP+5

L3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L4: Push_Constant 1

RTS L5: Push_Local 5 Push_Local 5 Push_Const 1 Subtr_Top 2 Push_Const 0 JSR _fact WPop Mult_Top2 RTS

main

SP

0

Page 16: Activation Records (Introduction)

Execution of fact

Administrative part FP

n 3ret

FP+5

L3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L4: Push_Constant 1

RTS L5: Push_Local 5 Push_Local 5 Push_Const 1 Subtr_Top 2 Push_Const 0 JSR _fact WPop Mult_Top2 RTS

main

SP3

0

Page 17: Activation Records (Introduction)

Execution of fact

Administrative part FP

n 3ret

FP+5

L3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L4: Push_Constant 1

RTS L5: Push_Local 5 Push_Local 5 Push_Const 1 Subtr_Top 2 Push_Const 0 JSR _fact WPop Mult_Top2 RTS

main

SP

3

0

0

Page 18: Activation Records (Introduction)

Execution of fact

Administrative part FP

n 3ret

FP+5

L3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L4: Push_Constant 1

RTS L5: Push_Local 5 Push_Local 5 Push_Const 1 Subtr_Top 2 Push_Const 0 JSR _fact WPop Mult_Top2 RTS

main

SP

0

Page 19: Activation Records (Introduction)

Execution of fact

Administrative part FP

n 3ret

FP+5

L3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L4: Push_Constant 1

RTS L5: Push_Local 5 Push_Local 5 Push_Const 1 Subtr_Top 2 Push_Const 0 JSR _fact WPop Mult_Top2 RTS

main

SP

0

Page 20: Activation Records (Introduction)

Execution of fact

Administrative part FP

n 3ret

FP+5

L3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L4: Push_Constant 1

RTS L5: Push_Local 5 Push_Local 5 Push_Const 1 Subtr_Top 2 Push_Const 0 JSR _fact WPop Mult_Top2 RTS

main

SP3

0

Page 21: Activation Records (Introduction)

Execution of fact

Administrative part FP

n 3ret

FP+5

L3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L4: Push_Constant 1

RTS L5: Push_Local 5 Push_Local 5 Push_Const 1 Subtr_Top 2 Push_Const 0 JSR _fact WPop Mult_Top2 RTS

main

3

SP3

0

Page 22: Activation Records (Introduction)

Execution of fact

Administrative part FP

n 3ret

FP+5

L3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L4: Push_Constant 1

RTS L5: Push_Local 5 Push_Local 5 Push_Const 1 Subtr_Top 2 Push_Const 0 JSR _fact WPop Mult_Top2 RTS

main

3

3

SP1

0

Page 23: Activation Records (Introduction)

Execution of fact

Administrative part FP

n 3ret

FP+5

L3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L4: Push_Constant 1

RTS L5: Push_Local 5 Push_Local 5 Push_Const 1 Subtr_Top 2 Push_Const 0 JSR _fact WPop Mult_Top2 RTS

main

3

2 SP

0

Page 24: Activation Records (Introduction)

Execution of fact

Administrative part FP

n 3ret

FP+5

L3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L4: Push_Constant 1

RTS L5: Push_Local 5 Push_Local 5 Push_Const 1 Subtr_Top 2 Push_Const 0 JSR _fact WPop Mult_Top2 RTS

main

3

2

SP

0

0

Page 25: Activation Records (Introduction)

FP

L3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L4: Push_Constant 1

RTS L5: Push_Local 5 Push_Local 5 Push_Const 1 Subtr_Top 2 Push_Const 0 JSR _fact WPop Mult_Top2 RTS

main

SP

Administrative part

n

ret

3

3

Administrative part

n

ret

2

2

n

ret

1

0

0

0

Page 26: Activation Records (Introduction)

FP

L3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L4: Push_Constant 1

RTS L5: Push_Local 5 Push_Local 5 Push_Const 1 Subtr_Top 2 Push_Const 0 JSR _fact WPop Mult_Top2 RTS

main

SP

Administrative part

n

ret

3

3

Administrative part

n

ret

2

2

Administrative part

0

n

ret

1

1

0

0

0

Page 27: Activation Records (Introduction)

FP

L3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L4: Push_Constant 1

RTS L5: Push_Local 5 Push_Local 5 Push_Const 1 Subtr_Top 2 Push_Const 0 JSR _fact WPop Mult_Top2 RTS

main

SP

Administrative part

n

ret

3

3

Administrative part

n

ret

2

2

Administrative part

n

ret

1

1

Administrative part

n

ret

0

1

0

0

0

Page 28: Activation Records (Introduction)

FP

L3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L4: Push_Constant 1

RTS L5: Push_Local 5 Push_Local 5 Push_Const 1 Subtr_Top 2 Push_Const 0 JSR _fact WPop Mult_Top2 RTS

main

SP

Administrative part

n

ret

3

3

Administrative part

n

ret

2

2

Administrative part

0

1

n

ret

1

1

0

0

0

Page 29: Activation Records (Introduction)

FP

L3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L4: Push_Constant 1

RTS L5: Push_Local 5 Push_Local 5 Push_Const 1 Subtr_Top 2 JSR _fact Push_Const 0 WPop Mult_Top2 RTS

main

Administrative part

n

ret

3

3

Administrative part

n

ret

2

2

Administrative part

1

n

ret

1

1

SP

0

0

Page 30: Activation Records (Introduction)

FP

L3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L4: Push_Constant 1

RTS L5: Push_Local 5 Push_Local 5 Push_Const 1 Subtr_Top 2 Push_Const 0 JSR _fact WPop Mult_Top2 RTS

main

Administrative part

n

ret

3

3

Administrative part

n

ret

2

2

Administrative part

n

ret

1

1 SP

0

0

Page 31: Activation Records (Introduction)

FP

L3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L4: Push_Constant 1

RTS L5: Push_Local 5 Push_Local 5 Push_Const 1 Subtr_Top 2 Push_Const 0 JSR _fact WPop Mult_Top2 RTS

main

Administrative part

n

ret

3

3

Administrative part

n

ret

2

2

n

ret

1

1 SP

0

0

Page 32: Activation Records (Introduction)

FP

L3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L4: Push_Constant 1

RTS L5: Push_Local 5 Push_Local 5 Push_Const 1 Subtr_Top 2 Push_Const 0 JSR _fact WPop Mult_Top2 RTS

main

Administrative part

n

ret

3

3

Administrative part

n

ret

2

2

n 1 SP

0

0

Page 33: Activation Records (Introduction)

FP

L3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L4: Push_Constant 1

RTS L5: Push_Local 5 Push_Local 5 Push_Const 1 Subtr_Top 2 Push_Const 0 JSR _fact WPop Mult_Top2 RTS

main

Administrative part

n

ret

3

3

Administrative part

n

ret

2

2 SP

0

0

Page 34: Activation Records (Introduction)

FP

L3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L4: Push_Constant 1

RTS L5: Push_Local 5 Push_Local 5 Push_Const 1 Subtr_Top 2 Push_Const 0 JSR _fact WPop Mult_Top2 RTS

main

Administrative part

n

ret

3

3

n

ret

2

2 SP

0

Page 35: Activation Records (Introduction)

FPL3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L4: Push_Constant 1

RTS L5: Push_Local 5 Push_Local 5 Push_Const 1 Subtr_Top 2 Push_Const 0 JSR _fact WPop Mult_Top2 RTS

main

Administrative part

n

ret

3

3

2 SP

0

Page 36: Activation Records (Introduction)

FPL3: Push_Local 5

Push_Const 0

Branch_Eq L4

Branch L5

L4: Push_Constant 1

RTS L5: Push_Local 5 Push_Local 5 Push_Const 1 Subtr_Top 2 Push_Const 0 JSR _fact WPop Mult_Top2 RTS

main

Administrative part

n

ret

3

6 SP

0

Page 37: Activation Records (Introduction)

FPmain

n

ret

3

6 SP

L1: Push_Const 3

Push_Const 0

JSR _fact

WPop

Push_Const L2

JSR _printf

Page 38: Activation Records (Introduction)

FPmain

6 SPL1: Push_Const 3

Push_Const 0

JSR _fact

WPop

Push_Const L2

JSR _printf

Page 39: Activation Records (Introduction)

FPmain

6

SP

L1: Push_Const 3

Push_Const 0

JSR _fact

WPop

Push_Const L2

JSR _printf

x87

Page 40: Activation Records (Introduction)

main

6_printf

x87

FPAdministrative part

SP

x87

Page 41: Activation Records (Introduction)

Code For Register Machine

Page 42: Activation Records (Introduction)

Register Instructions

Instruction Actions

Load_Const c, Ri Ri=c

Load_Local c(Ri), Rj Rj=*(Ri+c)

Store_Local Ri, c(Rj) *(Rj+c)=Ri

Add_Constant c, Ri Ri = Ri+c

Add_Reg Ri, Rj Rj = Rj+Ri

Sub_Constant Ri, c Ri = Ri - c

Sub_Reg Ri, Rj Rj =Rj-Ri

CMP Ri, Rj CC = Ri -Rj

CMP_Const C, Ri CC = Ri - C

Branch L PC = L;

Branch_EQ L if (CC==EQ) PC =L

JSR L *(--SP) = FP; *(--SP) = PC; FP=SP-1;PC =L

RTS PC=*(SP++); FP=*(SP++)

Page 43: Activation Records (Introduction)

void main() {printf(“%d\n”, fact(3));}

.global _main

Add_Constant -K1, SP

L1: Load_Const 3, R0

JSR _fact

Load_Reg R0, R1

Load_Const L2, R0

JSR _printf

Add_Constant K1, SP RTS

.data

L2 : “%d\n”

.end

Register Code Generated for main

Page 44: Activation Records (Introduction)

Register Code generated for fact

int fact(int n){ if (n==0) return 1; else return n * fact(n-1) ;}

L4: Load_Constant 1, R0 Goto L6

.global _fact

Add _Constant -K2, SP L3: Cmp_Constant R0, 0

Branch_Eq L4

Branch L5

L5: Store_Local R0, 5(FP)

Sub_Constant 1, R0

JSR _fact Load_Local 5(FP), R1

Mult_Reg R1, R0

Goto L6

L6: Add_Constant K2, SP

RTS

.end

Page 45: Activation Records (Introduction)

Summary

• Activation records is a runtime data structure

• Updated by code generated by the compiler

• Support from machine instruction