runtime organization. 2 overview program organization memory pools –static –automatic –dynamic...
TRANSCRIPT
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)
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
5
Zooming In.• Close look on the code
area
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
7
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
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
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
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
12
Overview• Program Organization
• Memory pools– Static
– Automatic
– Dynamic
• Activation Records
• Parameter Passing Modes
13
Activation Records• Also known as “Frames”
– A record pushed on the execution stack
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); ...}
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
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); ...}
17
Closeup on management data
18
Returning From a Call• Easy
– The RET instruction simply• Access MGMT Area from
FP
• Restores SP
• Restores FP
• Transfer control to return address
19
Returning From a Call• Easy
– The RET instruction simply• Access MGMT Area from
FP
• Restores SP
• Restores FP
• Transfer control to return address
20
Returning From a Call• Easy
– The RET instruction simply• Access MGMT Area from
FP
• Restores SP
• Restores FP
• Transfer control to return address
21
Returning From a Call• Easy
– The RET instruction simply• Access MGMT Area from
FP
• Restores SP
• Restores FP
• Transfer control to return address
22
Overview• Program Organization
• Memory pools– Static
– Automatic
– Dynamic
• Layout Activation Records
• Parameter Passing Modes
• Symbol Table
23
Parameter Passing Modes• A Few Options
– By value
– By reference
– By value/result
– By name
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
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?
26
Value vs. Reference• Pass by Value
– Called routine cannot modify the Actual Parameter
• Pass by Reference– Called routine can modify Actual Parameter
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
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 ?
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…
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
31
Class Practicea=2;
f(a,a)
Print a;
f(y,z){y=y+1;
z=z+1;}
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);
33
Class PracticeX:int;
X=2;
Foo(X);
Print x;
Procedure foo (y:int)Y=3;
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).
35
Static Scope• Current binding for name:
the one encountered most recently in top-to-bottom scan of the program text.
• Nested subroutines
36
Nested Subroutines in Pascal
Visible:P2, P4 within P1P3 within P2F1 within P4
Which X:In F1?In P4?In P2?
37
Static Chains
Nested calls:A, E, B, D, C
Static link:to the frame of the most recentinvocation of the lexically surroundingsubroutine
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.
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
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.
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
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)
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 */
44
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
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.
47
48
Symbol Table Implementation
• Usually implemented as hash tables
• Return closest lexical declaration to handle nested lexical scoping
• How to handle multiple scopes?
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
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.