Run-%me Environments Rajesh Kr. Thakur Dept. of Computer - - PowerPoint PPT Presentation

run me environments
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Run-­‑%me ¡Environments ¡

Rajesh ¡Kr. ¡Thakur ¡

  • Dept. ¡of ¡Computer ¡Science ¡and ¡Automa%on, ¡

Indian ¡Ins%tute ¡of ¡Science, ¡Bangalore-­‑560012 ¡

slide-2
SLIDE 2

Outline ¡

  • Why ¡Run%me ¡Support. ¡
  • Parameter ¡Passing. ¡
  • Storage ¡Organiza%on. ¡
  • Ac%va%on ¡Trees ¡ ¡
  • Procedure ¡Ac%va%on ¡Records. ¡
  • Scope. ¡
slide-3
SLIDE 3

Run%me ¡Support ¡

  • Genera%ng ¡machine ¡code ¡from ¡intermediate ¡

is ¡not ¡enough. ¡

  • Interfaces ¡between ¡the ¡program ¡and ¡

computer ¡resources ¡are ¡needed. ¡

– Memory ¡Management ¡ – Other ¡resources ¡like ¡file ¡systems, ¡printers, ¡also ¡ need ¡to ¡be ¡accessed. ¡

slide-4
SLIDE 4

Parameter ¡passing ¡

1.Parameter-­‑passing ¡methods ¡

– Call-­‑by-­‑value ¡ – Call-­‑by-­‑reference ¡ – Call-­‑by-­‑Value-­‑Result ¡

slide-5
SLIDE 5

Parameter ¡passing ¡

2.Call-­‑by-­‑value ¡

1) ¡In ¡C ¡and ¡Pascal ¡ 2) ¡Implementa%on ¡

  • A ¡formal ¡parameter ¡is ¡treated ¡just ¡like ¡a ¡local ¡name, ¡so ¡

the ¡storage ¡for ¡the ¡formals ¡is ¡in ¡the ¡ac%va%on ¡record ¡of ¡ the ¡called ¡procedure. ¡

  • The ¡caller ¡evaluates ¡the ¡actual ¡parameters ¡and ¡places ¡

their ¡r-­‑values ¡in ¡the ¡storage ¡for ¡the ¡formals. ¡

slide-6
SLIDE 6

Parameter ¡passing ¡

3.Call-­‑by-­‑Reference ¡

1) ¡call-­‑by-­‑address ¡or ¡call-­‑by-­‑loca%on. ¡ 2) ¡Implementa%on ¡

  • If ¡an ¡actual ¡parameter ¡is ¡a ¡name ¡or ¡an ¡expression ¡

having ¡an ¡l-­‑value, ¡then ¡that ¡l-­‑value ¡itself ¡is ¡passed. ¡

  • If ¡the ¡actual ¡parameter ¡is ¡an ¡expression ¡that ¡has ¡no ¡l-­‑

value, ¡then ¡the ¡expression ¡is ¡evaluated ¡in ¡a ¡new ¡ loca%on, ¡and ¡the ¡address ¡of ¡that ¡loca%on ¡is ¡passed. ¡

slide-7
SLIDE 7

Parameter ¡passing ¡

4.Copy-­‑by-­‑Value-­‑Result ¡

1) ¡Hybrid ¡between ¡call-­‑by-­‑value ¡and ¡call-­‑by-­‑reference. ¡ 2) ¡Implementa%on ¡

  • Before ¡control ¡flows ¡to ¡the ¡called ¡procedure, ¡the ¡actual ¡

parameters ¡are ¡evaluated.(copy-­‑in) ¡

  • When ¡control ¡returns, ¡the ¡current ¡r-­‑value ¡of ¡the ¡formal ¡

parameters ¡are ¡copied ¡back ¡into ¡the ¡l-­‑values ¡of ¡the ¡ actuals, ¡using ¡the ¡l-­‑values ¡computed ¡before ¡the ¡ call(copy-­‑out) ¡ Note: ¡Only ¡actuals ¡having ¡l-­‑values ¡are ¡copied. ¡

slide-8
SLIDE 8

Parameter ¡Passing ¡(Example) ¡

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 ¡

slide-9
SLIDE 9

Parameter ¡Passing ¡(Example) ¡

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 ¡

slide-10
SLIDE 10

Parameter ¡Passing ¡(Example) ¡

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 ¡

slide-11
SLIDE 11

Storage ¡Organiza%on ¡

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: ¡

  • Generated ¡target ¡code ¡
  • Data ¡objects, ¡and ¡
  • Control ¡stack ¡to ¡keep ¡track ¡of ¡procedure ¡ac%va%ons. ¡

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. ¡

slide-12
SLIDE 12

Memory ¡Organiza%on ¡(cont.) ¡

Code ¡ ¡ Global ¡/ ¡Sta%c ¡data ¡ Stack ¡ Heap ¡

slide-13
SLIDE 13

Code ¡Memory ¡

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 ¡

slide-14
SLIDE 14

Data ¡Area ¡

  • Only ¡a ¡small ¡part ¡of ¡data ¡can ¡be ¡assigned ¡fixed ¡loca%ons ¡before ¡execu%on ¡begins ¡ ¡

– Global ¡and/or ¡sta%c ¡data ¡ – Compile-­‑%me ¡constants ¡ ¡

  • Large ¡integer ¡values ¡
  • Floa%ng-­‑point ¡values ¡

– 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 ¡

Dynamic ¡Memory ¡

slide-15
SLIDE 15

Why ¡Stack ¡and ¡Heap? ¡

  • C/Pascal ¡uses ¡extensions ¡of ¡the ¡control ¡stack ¡to ¡manage ¡ac%va%ons ¡of ¡
  • procedures. ¡

¡

  • When ¡a ¡call ¡occurs, ¡execu%on ¡of ¡an ¡ac%va%on ¡is ¡interrupted ¡and ¡informa%on ¡

about ¡the ¡status ¡of ¡the ¡machine, ¡such ¡as ¡the ¡value ¡of ¡the ¡program ¡counter ¡and ¡ machine ¡registers, ¡is ¡saved ¡on ¡the ¡stack. ¡

  • When ¡control ¡returns ¡from ¡the ¡call, ¡this ¡ac%va%on ¡can ¡be ¡restarted ¡aier ¡

restoring ¡the ¡values ¡of ¡relevant ¡registers ¡and ¡sejng ¡the ¡program ¡counter ¡to ¡the ¡ point ¡immediately ¡a>er ¡the ¡call. ¡

  • Heap ¡holds ¡all ¡other ¡informa%on ¡like ¡the ¡data ¡under ¡program ¡control ¡i.e. ¡dynamic ¡

memory ¡alloca%on. ¡ ¡

  • By ¡conven%on, ¡stacks ¡grow ¡down. ¡Heaps ¡grow ¡up. ¡ ¡
slide-16
SLIDE 16

Ac%va%on ¡Trees ¡

  • Consider ¡the ¡following ¡program: ¡

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; ¡ } ¡

slide-17
SLIDE 17

Ac%va%on ¡Trees ¡

  • Consider ¡the ¡following ¡program: ¡

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 ¡

slide-18
SLIDE 18

Procedure ¡Ac%va%on ¡Record ¡

  • An ¡important ¡unit ¡of ¡memory ¡
  • Procedure ¡acDvaDon ¡record ¡contains ¡memory ¡allocated ¡for ¡the ¡local ¡data ¡of ¡

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 ¡

slide-19
SLIDE 19

Procedure ¡Ac%va%on ¡Record ¡(cont.) ¡

Returned ¡value Actual ¡parameter Op%onal ¡control ¡link Op%onal ¡access ¡link Saved ¡machine ¡status Local ¡data temporaries

Return ¡value ¡to ¡the ¡calling ¡procedure

slide-20
SLIDE 20

Procedure ¡Ac%va%on ¡Record ¡(cont.) ¡

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

slide-21
SLIDE 21

Procedure ¡Ac%va%on ¡Record ¡(cont.) ¡

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

slide-22
SLIDE 22

Procedure ¡Ac%va%on ¡Record ¡(cont.) ¡

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

slide-23
SLIDE 23

Procedure ¡Ac%va%on ¡Record ¡(cont.) ¡

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

slide-24
SLIDE 24

Procedure ¡Ac%va%on ¡Record ¡(cont.) ¡

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

slide-25
SLIDE 25

Procedure ¡Ac%va%on ¡Record ¡(cont.) ¡

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

slide-26
SLIDE 26

Example ¡1: ¡GCD ¡

slide-27
SLIDE 27

Ac%va%on ¡Records ¡

x: ¡15 ¡ y: ¡10 ¡ global ¡sta%c ¡area ¡

slide-28
SLIDE 28

Ac%va%on ¡Records ¡

x: ¡15 ¡ y: ¡10 ¡ global ¡sta%c ¡area ¡ ac%va%on ¡record ¡of ¡main ¡

slide-29
SLIDE 29

Ac%va%on ¡Records ¡

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 ¡

slide-30
SLIDE 30

Ac%va%on ¡Records ¡

x: ¡15 ¡ y: ¡10 ¡ global ¡sta%c ¡area ¡ ac%va%on ¡record ¡of ¡main ¡

u: ¡10 ¡ v: ¡ ¡ ¡5 ¡

  • ld ¡bp ¡

return ¡value ¡ return ¡address ¡

first ¡call ¡to ¡gcd ¡ second ¡call ¡to ¡gcd ¡

u: ¡15 ¡ v: ¡10 ¡ main ¡bp ¡ return ¡value ¡ return ¡address ¡

slide-31
SLIDE 31

Ac%va%on ¡Records ¡

x: ¡15 ¡ y: ¡10 ¡ global ¡sta%c ¡area ¡ ac%va%on ¡record ¡of ¡main ¡

u: ¡10 ¡ v: ¡ ¡ ¡5 ¡

  • ld ¡bp ¡

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 ¡

  • ld ¡bp ¡

return ¡address ¡

third ¡call ¡to ¡gcd ¡

slide-32
SLIDE 32

Ac%va%on ¡Records ¡

x: ¡15 ¡ y: ¡10 ¡ global ¡sta%c ¡area ¡ ac%va%on ¡record ¡of ¡main ¡

u: ¡10 ¡ v: ¡ ¡ ¡5 ¡

  • ld ¡bp ¡

rv: ¡5 ¡ return ¡address ¡

first ¡call ¡to ¡gcd ¡ second ¡call ¡to ¡gcd ¡

u: ¡15 ¡ v: ¡10 ¡ main ¡bp ¡ return ¡value ¡ return ¡address ¡

slide-33
SLIDE 33

Ac%va%on ¡Records ¡

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 ¡

slide-34
SLIDE 34

Ac%va%on ¡Records ¡

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 ¡

slide-35
SLIDE 35

Sample ¡code ¡

slide-36
SLIDE 36

Ac%va%on ¡Records ¡

x: ¡ ¡ ¡2 ¡ f::x ¡ ¡1 ¡ ¡ global ¡sta%c ¡area ¡ return ¡value ¡ ac%va%on ¡record ¡of ¡main ¡ sp ¡ bp ¡

slide-37
SLIDE 37

Ac%va%on ¡Records ¡

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 ¡

slide-38
SLIDE 38

Ac%va%on ¡Records ¡

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 ¡

slide-39
SLIDE 39

Ac%va%on ¡Records ¡

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 ¡

slide-40
SLIDE 40

Arrays ¡on ¡the ¡stack ¡

void ¡f ¡( ¡int ¡x, ¡char ¡c) ¡ { ¡ ¡int ¡a[10]; ¡ ¡double ¡y; ¡ ¡… ¡ } ¡ Consider ¡func%on ¡f: ¡

slide-41
SLIDE 41

Ac%va%on ¡Record ¡

x: ¡ ¡ ¡2 ¡ c: ¡ ¡ ¡1 ¡

  • ld ¡bp: ¡

return ¡address ¡ ¡ ¡a[9] ¡ ¡ … ¡ a[2] ¡ a[1] ¡ a[0] ¡ y ¡

sp ¡ bp ¡ low ¡memory ¡ high ¡memory ¡

  • ffset ¡of ¡a ¡= ¡-­‑13*2 ¡= ¡-­‑26 ¡

a[i] ¡= ¡bp ¡+ ¡a ¡+ ¡2*i ¡ calcula%on ¡of ¡a[i] ¡

slide-42
SLIDE 42

Scope

  • The ¡range ¡of ¡statements ¡where ¡a ¡variable ¡is ¡visible, ¡ ¡

i.e. ¡can ¡be ¡accessed ¡

  • Local ¡variables ¡are ¡visible ¡in ¡the ¡program ¡unit ¡where ¡they ¡are ¡

declared ¡

  • Non-­‑local ¡variables ¡are ¡visible ¡in ¡a ¡program ¡unit ¡but ¡not ¡declared ¡

there ¡

  • Scope ¡rules ¡determine ¡how ¡names ¡are ¡associated ¡with ¡variables ¡
slide-43
SLIDE 43

Static Scope

  • Also ¡called ¡Lexical ¡Scope ¡
  • Based ¡on ¡structure ¡of ¡program ¡text ¡
  • To ¡connect ¡a ¡name ¡to ¡a ¡variable, ¡one ¡(you ¡or ¡the ¡compiler) ¡must ¡

find ¡the ¡declara%on ¡

  • Search ¡process: ¡ ¡

– Search ¡declara%ons, ¡first ¡locally, ¡ ¡ – Then ¡in ¡increasingly ¡larger ¡outer ¡enclosing ¡scopes ¡ – Un%l ¡declara%on ¡for ¡the ¡name ¡is ¡found ¡ ¡

  • Enclosing ¡sta%c ¡scopes ¡(to ¡a ¡specific ¡scope) ¡are ¡called ¡its ¡staDc ¡

ancestors; ¡

  • The ¡nearest ¡sta%c ¡ancestor ¡is ¡called ¡a ¡staDc ¡parent ¡
slide-44
SLIDE 44

Shadowed Variables

  • Variables are shadowed (hidden) in part of the code where there is a

more immediate ancestor (closer in scope) with the same name

  • C++ and Ada allow access to hidden variables using longer names

– In Ada: <unit> .<name> – In C++: <class_name> ::<name>

  • Common Lisp

– Packages variables accessible via longer name or package imported

  • Java

– this.<name> – <class_name>.this.<name> – super.<name> // shadowed field in superclass

slide-45
SLIDE 45

Scope Blocks

  • Static scopes can be created inside program units
  • Started with ALGOL 60!

C and C++: for (int index; …) { int x; … }

  • Scope block

– statements in between {…}, and – condition expression in preceding or following (…)

slide-46
SLIDE 46

Static Scoping Diagram

Example:

  • MAIN calls A and B
  • A calls C and D
  • B calls A and E
slide-47
SLIDE 47

Static Scope Problems

  • Suppose the specs are

changed so that D must access data in B

  • Solutions:

– Put D in B

  • but then C can 't call D

and D can't access A's variables – Move the data from B that D needs to MAIN

  • but then all procedures

can access them

  • Same problem for

procedure access

  • Static scoping may

encourages many global variables

slide-48
SLIDE 48

Dynamic Scope

  • Uses calling sequences of program units, not

their textual layout

– I.e. temporal versus spatial

  • References are connected to declarations by

searching back through the chain of subprogram calls in execution

– I.e. search through the dynamic stack for the most recent variable with the name

slide-49
SLIDE 49

Scope Example

MAIN

  • declaration of x

SUB1

  • declaration of x -

... call SUB2 ... SUB2 ...

  • reference to x -

... ... call SUB1 ...

MAIN ¡calls ¡Sub1 ¡ Sub1 ¡calls ¡Sub2 ¡ Sub2 ¡uses ¡x ¡ ¡

StaDc ¡scoping ¡

Reference ¡to ¡x ¡is ¡to ¡MAIN's ¡x ¡ ¡

Dynamic ¡scoping ¡

Reference ¡to ¡x ¡is ¡to ¡SUB1's ¡x ¡

slide-50
SLIDE 50

Scope Evaluation

¡

  • Advantage ¡of ¡dynamic ¡scoping ¡

– convenience ¡ – flexibility ¡

  • Disadvantage ¡

– poor ¡readability ¡ – reliability ¡

slide-51
SLIDE 51

Java ¡Environment/ ¡Life ¡Cycle ¡of ¡ Java ¡Code ¡ ¡

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

slide-52
SLIDE 52

Topics ¡not ¡covered ¡

  • Passing ¡func%ons ¡as ¡parameters ¡
  • Heap ¡Memory ¡Management ¡
  • Garbage ¡Collec%on ¡
slide-53
SLIDE 53

Thank ¡You ¡