Run-‑%me ¡Environments ¡
Rajesh ¡Kr. ¡Thakur ¡
- Dept. ¡of ¡Computer ¡Science ¡and ¡Automa%on, ¡
Run-%me Environments Rajesh Kr. Thakur Dept. of Computer - - PowerPoint PPT Presentation
Run-%me Environments Rajesh Kr. Thakur Dept. of Computer Science and Automa%on, Indian Ins%tute of Science, Bangalore-560012 Outline Why Run%me
Call ¡by ¡Value ¡ print(n) ¡ 1 ¡ print(n) ¡ 1 ¡ int ¡n; ¡ void ¡p(int ¡k) ¡ { ¡ ¡ ¡ ¡n ¡:= ¡n+1; ¡ ¡ ¡ ¡k ¡:= ¡k+4; ¡ ¡ ¡ ¡print(n); ¡ ¡} ¡ int ¡main() ¡ { ¡ ¡ ¡ ¡ ¡n ¡= ¡0; ¡ ¡ ¡ ¡ ¡p(n); ¡ ¡ ¡ ¡ ¡print(n); ¡ } ¡ main: ¡ n=0 ¡ p(0); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡prints ¡n=1 ¡ ¡ p() ¡ n=1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡prints ¡n=1 ¡ k=4 ¡ returns ¡to ¡main ¡
Call ¡by ¡Value ¡ Call ¡by ¡ Reference ¡ print(n) ¡ 1 ¡ 5 ¡ print(n) ¡ 1 ¡ 5 ¡ int ¡n; ¡ void ¡p(int ¡k) ¡ { ¡ ¡ ¡ ¡n ¡:= ¡n+1; ¡ ¡ ¡ ¡k ¡:= ¡k+4; ¡ ¡ ¡ ¡print(n); ¡ ¡} ¡ int ¡main() ¡ { ¡ ¡ ¡ ¡ ¡n ¡= ¡0; ¡ ¡ ¡ ¡ ¡p(n); ¡ ¡ ¡ ¡ ¡print(n); ¡ } ¡ main: ¡ n=0 ¡ p(0); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡prints ¡n=5 ¡ ¡ p() ¡ ¡here ¡k=n ¡ n=1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡prints ¡n=5 ¡ k=4+1 ¡ returns ¡to ¡main ¡
Call ¡by ¡Value ¡ Call ¡by ¡ Reference ¡ Call ¡by ¡Value-‑ Result ¡ print(n) ¡ 1 ¡ 5 ¡ 1 ¡ print(n) ¡ 1 ¡ 5 ¡ 4 ¡ int ¡n; ¡ void ¡p(int ¡k) ¡ { ¡ ¡ ¡ ¡n ¡:= ¡n+1; ¡ ¡ ¡ ¡k ¡:= ¡k+4; ¡ ¡ ¡ ¡print(n); ¡ ¡} ¡ int ¡main() ¡ { ¡ ¡ ¡ ¡ ¡n ¡= ¡0; ¡ ¡ ¡ ¡ ¡p(n); ¡ ¡ ¡ ¡ ¡print(n); ¡ } ¡ main: ¡ n=0 ¡ p(0); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡prints ¡n=4 ¡ ¡ p: ¡ p() ¡ ¡here ¡k=n ¡(but ¡only ¡value ¡copied ¡and ¡later ¡result ¡put ¡back ¡to ¡n) ¡ n=1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡prints ¡n=1 ¡ K=0+4 ¡ returns ¡to ¡main ¡
1. The ¡compiler ¡obtains ¡a ¡block ¡of ¡storage ¡from ¡the ¡opera%ng ¡system ¡for ¡the ¡ compiled ¡program ¡to ¡run ¡in. ¡ 2. Run ¡%me ¡storage ¡might ¡be ¡subdivided ¡to ¡hold: ¡
3. In ¡most ¡compiled ¡languages, ¡it ¡is ¡not ¡possible ¡to ¡make ¡changes ¡to ¡the ¡code ¡ area ¡during ¡execu%on. ¡ 4. The ¡code ¡area ¡is ¡fixed ¡prior ¡to ¡the ¡execu%on, ¡and ¡all ¡code ¡addresses ¡are ¡ computable ¡at ¡compile ¡%me. ¡ 5. The ¡size ¡of ¡the ¡some ¡data ¡objects ¡may ¡also ¡be ¡known ¡at ¡compile ¡%me, ¡and ¡ these ¡too ¡can ¡be ¡placed ¡in ¡a ¡sta%cally ¡determined ¡area. ¡
Code ¡ ¡ Global ¡/ ¡Sta%c ¡data ¡ Stack ¡ Heap ¡
Entry ¡point ¡of ¡each ¡procedure ¡and ¡func%on ¡is ¡known ¡at ¡compile ¡%me. ¡ ¡ Code ¡for ¡Procedure ¡1 ¡ Code ¡for ¡Procedure ¡2 ¡ . ¡ . ¡ Code ¡for ¡Procedure ¡n ¡ Entry ¡point ¡for ¡Procedure ¡1 ¡ Entry ¡point ¡for ¡Procedure ¡2 ¡ . ¡ . ¡ Entry ¡point ¡for ¡Procedure ¡n ¡
– Global ¡and/or ¡sta%c ¡data ¡ – Compile-‑%me ¡constants ¡ ¡
– Literal ¡strings ¡ 1. The ¡memory ¡area ¡for ¡the ¡alloca%on ¡of ¡dynamic ¡data ¡can ¡be ¡organized ¡in ¡many ¡ different ¡ways. ¡ 2. A ¡typical ¡organiza%on ¡divides ¡the ¡dynamic ¡memory ¡into ¡ ¡ – ¡stack ¡area ¡(LIFO ¡protocol) ¡ – ¡heap ¡area ¡
¡
about ¡the ¡status ¡of ¡the ¡machine, ¡such ¡as ¡the ¡value ¡of ¡the ¡program ¡counter ¡and ¡ machine ¡registers, ¡is ¡saved ¡on ¡the ¡stack. ¡
restoring ¡the ¡values ¡of ¡relevant ¡registers ¡and ¡sejng ¡the ¡program ¡counter ¡to ¡the ¡ point ¡immediately ¡a>er ¡the ¡call. ¡
memory ¡alloca%on. ¡ ¡
int ¡x ¡= ¡2; ¡ ¡void ¡f(int ¡n) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡staDc ¡int ¡x ¡= ¡1; ¡ ¡ ¡ ¡ ¡ ¡ ¡g(n); ¡ ¡x-‑-‑; ¡ ¡} ¡ void ¡g(int ¡m) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡y ¡= ¡m-‑1; ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(y ¡> ¡0) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f(y); ¡ ¡x-‑-‑; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡g(y); ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ } ¡ main() ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡g(x); ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡0; ¡ } ¡
int ¡x ¡= ¡2; ¡ ¡void ¡f(int ¡n) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡staDc ¡int ¡x ¡= ¡1; ¡ ¡ ¡ ¡ ¡ ¡ ¡g(n); ¡ ¡x-‑-‑; ¡ ¡} ¡ void ¡g(int ¡m) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡y ¡= ¡m-‑1; ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(y ¡> ¡0) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f(y); ¡ ¡x-‑-‑; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡g(y); ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ } ¡ main() ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡g(x); ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡0; ¡ } ¡
¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡main() ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡| ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡g(2) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡\ ¡ ¡ ¡ ¡ ¡ ¡f(1) ¡g(1) ¡ ¡ ¡ ¡ ¡ ¡ ¡| ¡ ¡ ¡ ¡ ¡ ¡g(1) ¡ ¡ ¡ Ac%va%on ¡Tree ¡
a ¡procedure ¡or ¡func%on ¡when ¡it ¡is ¡called, ¡or ¡ac%vated. ¡ 1. Details depend on the architecture of target machine and properties of the language. 2. When activation records are kept on stack, they are called stack frames. Arguments ¡ Book-‑Keeping ¡informa%on ¡ (return ¡address) ¡ Local ¡Data ¡ Local ¡Temporaries ¡
Returned ¡value Actual ¡parameter Op%onal ¡control ¡link Op%onal ¡access ¡link Saved ¡machine ¡status Local ¡data temporaries
Return ¡value ¡to ¡the ¡calling ¡procedure
Returned ¡value Actual ¡parameter Op%onal ¡control ¡link Op%onal ¡access ¡link Saved ¡machine ¡status Local ¡data temporaries
Return ¡value ¡to ¡the ¡calling ¡procedure Parameters ¡to ¡the ¡called ¡procedure
Returned ¡value Actual ¡parameter Op%onal ¡control ¡link Op%onal ¡access ¡link Saved ¡machine ¡status Local ¡data temporaries
Return ¡value ¡to ¡the ¡calling ¡procedure Parameters ¡to ¡the ¡called ¡procedure Points ¡to ¡the ¡ac%va%on ¡record ¡of ¡the ¡caller
Returned ¡value Actual ¡parameter Op%onal ¡control ¡link Op%onal ¡access ¡link Saved ¡machine ¡status Local ¡data temporaries
Return ¡value ¡to ¡the ¡calling ¡procedure Parameters ¡to ¡the ¡called ¡procedure Points ¡to ¡the ¡ac%va%on ¡record ¡of ¡the ¡caller Refers ¡to ¡nonlocal ¡data ¡held ¡in ¡the ¡other ¡ ac%va%on ¡records
Returned ¡value Actual ¡parameter Op%onal ¡control ¡link Op%onal ¡access ¡link Saved ¡machine ¡status Local ¡data temporaries
Return ¡value ¡to ¡the ¡calling ¡procedure Parameters ¡to ¡the ¡called ¡procedure Points ¡to ¡the ¡ac%va%on ¡record ¡of ¡the ¡caller Refers ¡to ¡nonlocal ¡data ¡held ¡in ¡the ¡other ¡ ac%va%on ¡records Info ¡about ¡the ¡state ¡of ¡the ¡machine ¡just ¡before ¡ the ¡procedure ¡is ¡called
Returned ¡value Actual ¡parameter Op%onal ¡control ¡link Op%onal ¡access ¡link Saved ¡machine ¡status Local ¡data temporaries
Return ¡value ¡to ¡the ¡calling ¡procedure Parameters ¡to ¡the ¡called ¡procedure Points ¡to ¡the ¡ac%va%on ¡record ¡of ¡the ¡caller Refers ¡to ¡nonlocal ¡data ¡held ¡in ¡the ¡other ¡ ac%va%on ¡records Info ¡about ¡the ¡state ¡of ¡the ¡machine ¡just ¡before ¡ the ¡procedure ¡is ¡called Local ¡data
Returned ¡value Actual ¡parameter Op%onal ¡control ¡link Op%onal ¡access ¡link Saved ¡machine ¡status Local ¡data temporaries
Return ¡value ¡to ¡the ¡calling ¡procedure Parameters ¡to ¡the ¡called ¡procedure Points ¡to ¡the ¡ac%va%on ¡record ¡of ¡the ¡caller Refers ¡to ¡nonlocal ¡data ¡held ¡in ¡the ¡other ¡ ac%va%on ¡records Info ¡about ¡the ¡state ¡of ¡the ¡machine ¡just ¡before ¡ the ¡procedure ¡is ¡called Local ¡data Temporaries
x: ¡15 ¡ y: ¡10 ¡ global ¡sta%c ¡area ¡
x: ¡15 ¡ y: ¡10 ¡ global ¡sta%c ¡area ¡ ac%va%on ¡record ¡of ¡main ¡
x: ¡15 ¡ y: ¡10 ¡ global ¡sta%c ¡area ¡ ac%va%on ¡record ¡of ¡main ¡
u: ¡15 ¡ v: ¡10 ¡ main ¡bp ¡ return ¡value ¡ return ¡address ¡
first ¡call ¡to ¡gcd ¡
x: ¡15 ¡ y: ¡10 ¡ global ¡sta%c ¡area ¡ ac%va%on ¡record ¡of ¡main ¡
u: ¡10 ¡ v: ¡ ¡ ¡5 ¡
return ¡value ¡ return ¡address ¡
first ¡call ¡to ¡gcd ¡ second ¡call ¡to ¡gcd ¡
u: ¡15 ¡ v: ¡10 ¡ main ¡bp ¡ return ¡value ¡ return ¡address ¡
x: ¡15 ¡ y: ¡10 ¡ global ¡sta%c ¡area ¡ ac%va%on ¡record ¡of ¡main ¡
u: ¡10 ¡ v: ¡ ¡ ¡5 ¡
return ¡value ¡ return ¡address ¡
first ¡call ¡to ¡gcd ¡ second ¡call ¡to ¡gcd ¡
u: ¡15 ¡ v: ¡10 ¡ main ¡bp ¡ return ¡value ¡ return ¡address ¡
u: ¡ ¡ ¡5 ¡ v: ¡ ¡ ¡0 ¡
return ¡address ¡
third ¡call ¡to ¡gcd ¡
x: ¡15 ¡ y: ¡10 ¡ global ¡sta%c ¡area ¡ ac%va%on ¡record ¡of ¡main ¡
u: ¡10 ¡ v: ¡ ¡ ¡5 ¡
rv: ¡5 ¡ return ¡address ¡
first ¡call ¡to ¡gcd ¡ second ¡call ¡to ¡gcd ¡
u: ¡15 ¡ v: ¡10 ¡ main ¡bp ¡ return ¡value ¡ return ¡address ¡
x: ¡15 ¡ y: ¡10 ¡ global ¡sta%c ¡area ¡ ac%va%on ¡record ¡of ¡main ¡
u: ¡15 ¡ v: ¡10 ¡ main ¡bp ¡ rv ¡: ¡5 ¡ return ¡address ¡
first ¡call ¡to ¡gcd ¡
x: ¡15 ¡ y: ¡10 ¡ global ¡sta%c ¡area ¡ ac%va%on ¡record ¡of ¡main ¡
u: ¡15 ¡ v: ¡10 ¡ main ¡bp ¡ rv ¡: ¡5 ¡ return ¡address ¡
first ¡call ¡to ¡gcd ¡ this ¡value ¡sent ¡to ¡prinm ¡
x: ¡ ¡ ¡2 ¡ f::x ¡ ¡1 ¡ ¡ global ¡sta%c ¡area ¡ return ¡value ¡ ac%va%on ¡record ¡of ¡main ¡ sp ¡ bp ¡
x: ¡ ¡ ¡2 ¡ f::x ¡ ¡1 ¡ ¡ global ¡sta%c ¡area ¡ return ¡value ¡ ac%va%on ¡record ¡of ¡main ¡
m: ¡2 ¡ main ¡bp ¡ return ¡address ¡ y: ¡1 ¡
call ¡to ¡g( ¡) ¡ sp ¡ bp ¡
x: ¡ ¡ ¡2 ¡ f::x ¡ ¡1 ¡ ¡ global ¡sta%c ¡area ¡ return ¡value ¡ ac%va%on ¡record ¡of ¡main ¡
m: ¡2 ¡ main ¡bp ¡ return ¡address ¡ y: ¡1 ¡
call ¡to ¡g( ¡) ¡
n: ¡10 ¡ g: ¡bp ¡ return ¡address ¡
call ¡to ¡f( ¡) ¡ sp ¡ bp ¡
x: ¡ ¡ ¡2 ¡ f::x ¡ ¡1 ¡ ¡ global ¡sta%c ¡area ¡ return ¡value ¡ ac%va%on ¡record ¡of ¡main ¡
m: ¡2 ¡ main ¡bp ¡ return ¡address ¡ y: ¡1 ¡
call ¡to ¡g( ¡) ¡ n: ¡10 ¡ g: ¡bp ¡ return ¡address ¡ call ¡to ¡f( ¡) ¡
m: ¡1 ¡ main ¡bp ¡ return ¡address ¡ y: ¡0 ¡
call ¡to ¡g( ¡) ¡ sp ¡ bp ¡
void ¡f ¡( ¡int ¡x, ¡char ¡c) ¡ { ¡ ¡int ¡a[10]; ¡ ¡double ¡y; ¡ ¡… ¡ } ¡ Consider ¡func%on ¡f: ¡
x: ¡ ¡ ¡2 ¡ c: ¡ ¡ ¡1 ¡
return ¡address ¡ ¡ ¡a[9] ¡ ¡ … ¡ a[2] ¡ a[1] ¡ a[0] ¡ y ¡
sp ¡ bp ¡ low ¡memory ¡ high ¡memory ¡
a[i] ¡= ¡bp ¡+ ¡a ¡+ ¡2*i ¡ calcula%on ¡of ¡a[i] ¡
– Search ¡declara%ons, ¡first ¡locally, ¡ ¡ – Then ¡in ¡increasingly ¡larger ¡outer ¡enclosing ¡scopes ¡ – Un%l ¡declara%on ¡for ¡the ¡name ¡is ¡found ¡ ¡
more immediate ancestor (closer in scope) with the same name
– In Ada: <unit> .<name> – In C++: <class_name> ::<name>
– Packages variables accessible via longer name or package imported
– this.<name> – <class_name>.this.<name> – super.<name> // shadowed field in superclass
C and C++: for (int index; …) { int x; … }
– statements in between {…}, and – condition expression in preceding or following (…)
changed so that D must access data in B
– Put D in B
and D can't access A's variables – Move the data from B that D needs to MAIN
can access them
procedure access
encourages many global variables
MAIN
SUB1
... call SUB2 ... SUB2 ...
... ... call SUB1 ...
MAIN ¡calls ¡Sub1 ¡ Sub1 ¡calls ¡Sub2 ¡ Sub2 ¡uses ¡x ¡ ¡
Reference ¡to ¡x ¡is ¡to ¡MAIN's ¡x ¡ ¡
Reference ¡to ¡x ¡is ¡to ¡SUB1's ¡x ¡
¡
Java Bytecodes Java Source (.java) Java Compiler Java Bytecode (.class )
Java Interpreter
Just in Time Compiler Runtime System Class Loader Bytecode Verifier Java Class Libraries Operating System Hardware Java Virtual machine Runtime Environment Compile-time Environment