run me environments
play

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


  1. Run-­‑%me ¡Environments ¡ Rajesh ¡Kr. ¡Thakur ¡ Dept. ¡of ¡Computer ¡Science ¡and ¡Automa%on, ¡ Indian ¡Ins%tute ¡of ¡Science, ¡Bangalore-­‑560012 ¡

  2. Outline ¡ • Why ¡Run%me ¡Support. ¡ • Parameter ¡Passing. ¡ • Storage ¡Organiza%on. ¡ • Ac%va%on ¡Trees ¡ ¡ • Procedure ¡Ac%va%on ¡Records. ¡ • Scope. ¡

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

  4. Parameter ¡passing ¡ 1.Parameter-­‑passing ¡methods ¡ – Call-­‑by-­‑value ¡ – Call-­‑by-­‑reference ¡ – Call-­‑by-­‑Value-­‑Result ¡

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

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

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

  8. Parameter ¡Passing ¡(Example) ¡ Call ¡by ¡Value ¡ int ¡n; ¡ void ¡p(int ¡k) ¡ print(n) ¡ 1 ¡ { ¡ print(n) ¡ 1 ¡ ¡ ¡ ¡n ¡:= ¡n+1; ¡ ¡ ¡ ¡k ¡:= ¡k+4; ¡ ¡ ¡ ¡print(n); ¡ ¡} ¡ main: ¡ int ¡main() ¡ n=0 ¡ { ¡ p(0); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡prints ¡n=1 ¡ ¡ ¡ ¡ ¡n ¡= ¡0; ¡ ¡ ¡ ¡ ¡ ¡p(n); ¡ p() ¡ ¡ ¡ ¡ ¡print(n); ¡ n=1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡prints ¡n=1 ¡ } ¡ k=4 ¡ returns ¡to ¡main ¡

  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0+4 ¡ returns ¡to ¡main ¡

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

  12. Memory ¡Organiza%on ¡(cont.) ¡ Code ¡ ¡ Global ¡/ ¡Sta%c ¡data ¡ Stack ¡ Heap ¡

  13. Code ¡Memory ¡ Entry ¡point ¡for ¡Procedure ¡1 ¡ Code ¡for ¡Procedure ¡1 ¡ Entry ¡point ¡for ¡Procedure ¡2 ¡ Code ¡for ¡Procedure ¡2 ¡ . ¡ . ¡ . ¡ . ¡ Entry ¡point ¡for ¡Procedure ¡n ¡ Code ¡for ¡Procedure ¡n ¡ Entry ¡point ¡of ¡each ¡procedure ¡and ¡func%on ¡is ¡known ¡at ¡compile ¡%me. ¡ ¡

  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 ¡ Dynamic ¡Memory ¡ 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 ¡

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

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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend