runtime organization. 2 overview program organization memory pools –static –automatic –dynamic...

50
Runtime Organization

Upload: theodora-berenice-morrison

Post on 03-Jan-2016

230 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

Runtime Organization

Page 2: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

2

Overview• Program Organization

• Memory pools– Static

– Automatic

– Dynamic

• Activation Records

• Parameter Passing Modes

• Symbol Table

Page 3: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

3

Terminology• Executable

– A file containing machine code

– Examples• A bash script, a perl script, a ‘compiled’ java program,

a compiled C program,...

• Native executable– A file containing machine code that the CPU

understand without any intervening “layers” of abstractions

– Examples• A compiled C program, a Java program compiled

natively (with GCJ)

Page 4: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

4

Virtual Address Space• Traditional Organization

– Code Area at the bottom

– Static Data above• Constants

• Static strings

• Static variables

– Heap• Grows upward

– Stack• Grows downward

– Lot’s of free VM in between

0x0

0xffffffff

Page 5: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

5

Zooming In.• Close look on the code

area

Page 6: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

6

Execution Stack• A memory area at the top of the VM

– Grows downward

– Grows on demand (with OS collaboration)

• Purpose– Automatic storage for local variables

Page 7: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

7

Overview• Program Organization

• Memory pools– Static

– Automatic

– Dynamic

• Activation Records

• Parameter Passing Modes

• Symbol Table

Page 8: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

8

Memory Pools• Where does memory comes from ?

• Three pools– Static

– Automatic

– Dynamic

StaticStatic

AutomaticAutomatic

DynamicDynamic

Page 9: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

9

Static Pool• Content

– All the static “strings” that appear in the program

– All the static constants used in the program

– All the static variables declared in the program• static int

• static arrays

• static records

• static ....

• Allocation ?– Well... it is static, i.e.,

• All the sizes are determined at compile time.

• Cannot grow or shrink

StaticStatic

Page 10: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

10

Dynamic Pool• Content

– Anything allocated by the program at runtime

• Allocation– Depends on the language

• C malloc

• C++/Java/C# new

• ML/Lisp/Scheme implicit

• Deallocation– Depends on the language

• C free

• C++ delete

• Java/C#/ML/Lisp/Scheme Garbage collection

DynamicDynamic

Page 11: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

11

Automatic Pool• Content

– Local variables

– Actuals (arguments to methods/functions/procedures)

• Allocation– Automatic when calling a

method/function/procedure

• Deallocation– Automatic when returning from a

method/function/procedure

• Management policy– Stack-like

Page 12: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

12

Overview• Program Organization

• Memory pools– Static

– Automatic

– Dynamic

• Activation Records

• Parameter Passing Modes

Page 13: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

13

Activation Records• Also known as “Frames”

– A record pushed on the execution stack

Page 14: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

14

Creating the Frame• Three actors

– The caller

– The CPU

– The callee

int foo(int x,int y) { ...}

bar() { ... x = foo(3,y); ...}

int foo(int x,int y) { ...}

bar() { ... x = foo(3,y); ...}

Page 15: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

15

Creating the Frame• Three actors

– The caller

– The CPU

– The callee

int foo(int x,int y) { ...}

bar() { ... x = foo(3,y); ...}

int foo(int x,int y) { ...}

bar() { ... x = foo(3,y); ...}

Actual Function Call

Page 16: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

16

Creating the Frame• Three actors

– The caller

– The CPU

– The callee

int foo(int x,int y) { ...}

bar() { ... x = foo(3,y); ...}

int foo(int x,int y) { ...}

bar() { ... x = foo(3,y); ...}

Page 17: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

17

Closeup on management data

Page 18: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

18

Returning From a Call• Easy

– The RET instruction simply• Access MGMT Area from

FP

• Restores SP

• Restores FP

• Transfer control to return address

Page 19: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

19

Returning From a Call• Easy

– The RET instruction simply• Access MGMT Area from

FP

• Restores SP

• Restores FP

• Transfer control to return address

Page 20: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

20

Returning From a Call• Easy

– The RET instruction simply• Access MGMT Area from

FP

• Restores SP

• Restores FP

• Transfer control to return address

Page 21: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

21

Returning From a Call• Easy

– The RET instruction simply• Access MGMT Area from

FP

• Restores SP

• Restores FP

• Transfer control to return address

Page 22: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

22

Overview• Program Organization

• Memory pools– Static

– Automatic

– Dynamic

• Layout Activation Records

• Parameter Passing Modes

• Symbol Table

Page 23: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

23

Parameter Passing Modes• A Few Options

– By value

– By reference

– By value/result

– By name

Page 24: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

24

Call By Value• Simple strategy

– Push on the stack a copy of the argument

– Size depends on argument type

– Any write operation overwrites the copy

– Copy automatically discarded on exit

Page 25: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

25

Call By Reference• Simple strategy too

– Place the address of the actual on the stack

– A write operation simply follows the pointer

– By-reference is identical to By-address• Only difference is in the syntax.

• Advantages?

Page 26: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

26

Value vs. Reference• Pass by Value

– Called routine cannot modify the Actual Parameter

• Pass by Reference– Called routine can modify Actual Parameter

Page 27: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

27

Language Specific Variations• Pascal: Call by Value is the default, the

keyword VAR denotes Call by Reference

• Fortran: all parameters passed by Reference

• Smalltalk, Lisp: Actual Parameter is already a reference to the object

• C: always passed by Value

• Java:– Pass by value for primitive data type

– Pass by reference for object

Page 28: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

28

Call By Name• Slightly more involved

– The actual is not evaluated

– The program fragment that evaluates the actual is “wrapped up” so that it can be evaluated later on

– Each time the formal is used the actual gets evaluated

• It is a lot more like macro expansion!

• Advantage ?

Page 29: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

29

Call by name• Pretty much only in Algol

• Arguments are not evaluated until their actual use in the called program.

• Re-evaluates the actual parameter on every use– For actual parameters that are simple variables, it’s the

same as call by reference

– For actual parameters that are expressions, the expression is re-evaluated on each access

• No other language ever used call by name…

Page 30: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

30

Ada Parameter ModesThree parameter passing modes

• In – Passes information from the caller to the callee, can read

but not write

– Call by Value

• Out – Passes information from the callee to the caller, can write

but not read

– Call by Result (formal parameter is copied to actual parameter when subroutine exits)

• Inout – passes information both directions

– Call by Value/Result

Page 31: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

31

Class Practicea=2;

f(a,a)

Print a;

f(y,z){y=y+1;

z=z+1;}

Page 32: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

32

Class PracticeInt k=0,i=0,j=0;

procedure sub1(x: int; y: int; z: int); begin

k := 1; y := x; k := 5; z := x; end;

sub1(k+1, j, i);

Print(i,j,k);

Page 33: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

33

Class PracticeX:int;

X=2;

Foo(X);

Print x;

Procedure foo (y:int)Y=3;

Page 34: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

34

Scope Rules• Scope of a binding: a program region (textually) in

which binding is active.

• Scope: a program region of maximal size where no bindings change.

• Scoping can be:– Lexical or static (bindings known at compile time).

– Dynamic (bindings depend on flow of execution at run time).

Page 35: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

35

Static Scope• Current binding for name:

the one encountered most recently in top-to-bottom scan of the program text.

• Nested subroutines

Page 36: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

36

Nested Subroutines in Pascal

Visible:P2, P4 within P1P3 within P2F1 within P4

Which X:In F1?In P4?In P2?

Page 37: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

37

Static Chains

Nested calls:A, E, B, D, C

Static link:to the frame of the most recentinvocation of the lexically surroundingsubroutine

Page 38: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

38

Dynamic Scope• The current binding for a given name is the

one encountered most recently during execution, and not yet destroyed by returning from its scope.

Page 39: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

39

Dynamic scope example

What does the program print?

Under static scoping? 1 regardless of value read

Under dynamic scoping? 2: if value read is >0 1: if value read is <= 0

Dynamic scoping is usuallya bad idea

Page 40: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

40

Shallow & Deep bindingWhen subroutine is passed as a parameter, when is the

referencing environment bound?

• Shallow binding: when the function is called

• Deep binding: when the function is first passed as a parameter.

Important in both dynamic and static scoping.

Page 41: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

41

int max_score;

float scale_score (int raw_score) {

return (float) raw_score / (float) max_score;

}

float highest_score (int[] scores, function_ptr scaling_function) {

float max_score = 0;

foreach score in scores {

float percent = scaling_function (score);

if ( percent > max_score )

max_score = percent;

}

return max_score;

}

main() {

max_score = 50;

int[] scores = ...

print highest_score (scores, scale_score);

}

function is called

reference is created

Page 42: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

42

Example

threshold: integer

Most appropriate for:

older_than: deep binding (to get global threshold)

print_person: shallow binding (to get locally set line_length)

(dynamic scoping assumed)

Page 43: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

43

var x : integer; /* global variable */

procedure Update

x := x + 3;

procedure DoCall(P: procedure)

var x : integer;

x := 4;

P();

write_integer(x);

begin /* body of main program */

x := 0;

DoCall(Update);

end /* body of main program */

Page 44: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

44

Overview• Program Organization

• Memory pools– Static

– Automatic

– Dynamic

• Activation Records

• Parameter Passing Modes

• Symbol Table

Page 45: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

45

Symbol Table

• A “Dictionary” that maps names to info the compiler knows about that name.

• What names?– Variable and procedure names– Literal constants and strings

• What info?Textual nameData typeDeclaring procedureLexical level of declarationIf array, number and size of dimensionsIf procedure, number and type of parameters

Page 46: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

46

Sample program

Program gcd (input, output);

Var I,j: integer;

BeginRead(I,j);

While I <> j toIf I > j then I := I – j

Else j := j – I;

Writeln (i)

End.

Page 47: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

47

Page 48: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

48

Symbol Table Implementation

• Usually implemented as hash tables

• Return closest lexical declaration to handle nested lexical scoping

• How to handle multiple scopes?

Page 49: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

49

One option

• Use one symbol table per scope

• Chain tables to enclosing scope

• Insert names in tables for current scope

• Start name lookup in current table, checking enclosing scopes in order if needed

Page 50: Runtime Organization. 2 Overview Program Organization Memory pools –Static –Automatic –Dynamic Activation Records Parameter Passing Modes Symbol Table

50

LeBlanc-Cook symbol tables- Give each scope a number,

- All names in a hash table, keyed by name

- Also have a scope stack – to show current referencing environment.

- As analyzer looks at programs, it pushes and pops this stack as it enters and leaves scopes.

- To look up – scan down the appropriate hash chain, for each matching entry, scan down the scope stack to see if that is visible. We look no deeper than the top-most scope.