Lecture#22.ppt

download Lecture#22.ppt

of 10

Transcript of Lecture#22.ppt

  • 8/19/2019 Lecture#22.ppt

    1/24

    Lecture # 22

    Run-Time Environments

    Chapter 7

    1

  • 8/19/2019 Lecture#22.ppt

    2/24

    Runtime Environments

    • Before code generation static source text of a

    program needs to be related to the actions that

    must occur at runtime to implement the program.• As execution proceeds same name in the source

    text can denote different data objects in the target

    machine.

    • The allocation and de allocation of data objects is

    managed by the runtime support package.

    2

  • 8/19/2019 Lecture#22.ppt

    3/24

    7.1 Source Language Issues

    • Each execution of a function is referred to

    as an activation of the procedure/function

    • If the function is recursive several of its

    activations may be alive at the same time

    3

  • 8/19/2019 Lecture#22.ppt

    4/24

    4

    Procedure Activation and

    Lifetime• A procedure is activated  when called

    • The lifetime of an activation of a procedure

    is the sequence of steps between the firstand last steps in the execution of theprocedure body

    • A procedure is recursive if a new activationcan begin before an earlier activation of thesame procedure has ended

  • 8/19/2019 Lecture#22.ppt

    5/24

    Activation Tree• We use an activation tree to depict the way control

    enters and leaves activations. In an activation tree:

    • Each node represents activation of a procedure

    • The root represents activation of main program

    • The node for a is the parent of the node for b if

    the control flows from activation a to b

    • The node for a is to the left of node for b if thelifetime of a occurs before the lifetime of b

    5

  • 8/19/2019 Lecture#22.ppt

    6/24

    6

    Procedure Activations: Example program sort(input, output)  var a : array [0..10] of integer;

      procedure readarray;

      var i : integer;

      begin

      for i := 1 to 9 do read(a[i])

      end;

      function partition(y, : integer) : integer

      var i, !, ", v : integer;

      begin #

      end 

      procedure $uic%sort(m, n : integer);

      var i : integer;

      begin

      if (n & m) t'en begin

      i := partition(m, n);  $uic%sort(m, i 1);

      $uic%sort(i 1, n)

      end 

      end;

      begin

      a[0] := 9999; a[10] := 9999;

      readarray;

      $uic%sort(1, 9)

      end.

    Activations: begin sort

     enter readarray

     *eave readarray enter $uic%sort(1,9)

      enter partition(1,9)

      *eave partition(1,9)

      enter $uic%sort(1,+)

      #

      *eave $uic%sort(1,+)

      enter $uic%sort(,9)

      #

      *eave $uic%sort(,9)

     *eave $uic%sort(1,9)

    end sort.

  • 8/19/2019 Lecture#22.ppt

    7/24

    7

    Activation Trees: Example

    s

    $(1,9)

    $(1,+)

     p(1,+) $(1,0) $(-,+)

    $(-,1) p(-,+) $(+,+)

     p(1,9)

    r

    $(,9)

     p(,9) $(,) $(,9)

    $(,) p(,9) $(9,9)

    Activation tree for the sort program

    Note: also referred to as the dynamic call graph

  • 8/19/2019 Lecture#22.ppt

    8/24

    Control Stack

    • The flow of control of a program

    corresponds to depth first traversal of the

    activation tree

    • Control stack is used to keep track of the

    live procedure activations.

    • The idea is to push the node when

    activation begins and pop when it ends

    8

  • 8/19/2019 Lecture#22.ppt

    9/24

    Factorial Example discussed

    public static int Factorial(int num)

      {

      if (num == 1)

      {

      fact=1;

      }

      else

      {

      fact=fact+num*Factorial(--num);

      }  return fact;

      }

    9

  • 8/19/2019 Lecture#22.ppt

    10/24

    Addition of Elements of Array

    recursively• public static int Add(int index)

    •   {

    •   if (index == 0)

    •   {•   sum = arr[index];

    •   }

    •   else

    •   {

    •   sum = sum + Add(arr[index]);•   }

    •   return sum;

    •   }

    10

  • 8/19/2019 Lecture#22.ppt

    11/24

    11

    Control Stack 

    s

    $(1,9)

    $(1,+)

     p(1,+) $(1,0) $(-,+)

     p(1,9)

    r

    Activations: begin sort

     enter readarray

     *eave readarray enter $uic%sort(1,9)

      enter partition(1,9)

      *eave partition(1,9)

      enter $uic%sort(1,+)

      enter partition(1,+)

      *eave partition(1,+)

      enter $uic%sort(1,0)

      *eave $uic%sort(1,0)

      enter $uic%sort(-,+)

      #

    Control

    stack:

    Activation tree:

    s

    $(1,9)

    $(1,+)

    $(-,+)

  • 8/19/2019 Lecture#22.ppt

    12/24

    12

    Scope Rules

    •   Environment  determines name-to-object

    bindings: which objects are in scope? program prg;  var y : rea*;

    function "(a : rea*) : rea*;

      begin # end;

     procedure p;

      var " : integer;

      begin  " := 1;

      #

      end;

     begin

      y := "(0.0);

      #end.

    Variable " locally declared in p

    A function "

  • 8/19/2019 Lecture#22.ppt

    13/24

    Binding of Names

    • If a name is declared once in a program it

    can hold different values at runtime

    • The term environment refers to a function

    that maps name to a storage location

    • The term state is referred to a function that

    maps a storage location to the value held

    there

    13

  • 8/19/2019 Lecture#22.ppt

    14/24

    14

    Mapping Names to Values

    name storage value

    environment state

    var i;#

    i := 0;

    #

    i := i 1;

  • 8/19/2019 Lecture#22.ppt

    15/24

    15

    Mapping Names to Values

    name storage value

    environment state

    var i; i := 0; i := i 1;

    /nvironment and states are different

     n assignment c'anges t'e state but not t'e

    environment

    At compile time At run time

  • 8/19/2019 Lecture#22.ppt

    16/24

    16

    Static and Dynamic Notions of

    Bindings

    Static Notion Dynamic Notion

    Definition of a procedureActivations of the

    procedure

    Declaration of a name Bindings of the name

    Scope of a declaration Lifetime of a binding

  • 8/19/2019 Lecture#22.ppt

    17/24

    Storage Organization

    • Runtime storage may be subdivided as:

    • The generated target code

    • Data objects

    • Control stack to keep track of procedure

    activations

    • The size of target code is fixed at compile time

    • Similarly size of data objects may be known

    17

  • 8/19/2019 Lecture#22.ppt

    18/24

    Typical subdivision of runtime

    memory• A separate area of runtime memory called

    Heap holds all other runtime information

    18

    Code

    Static

    DataStack 

    Heap

  • 8/19/2019 Lecture#22.ppt

    19/24

    19

    Stack Allocation

    •  Activation records (subroutine frames) on the run-time stack hold the state of a subroutine

    •  Calling sequences are code statements to createactivations records on the stack and enter data inthem– Caller’s calling sequence enters actual arguments,

    control link, access link, and saved machine state

    – Callee’s calling sequence initializes local data

    – Callee’s return sequence enters return value

    – Caller’s return sequence removes activation record

  • 8/19/2019 Lecture#22.ppt

    20/24

    20

    Activation Records

    (Subroutine Frames)

    Returned value

    Actual parameters

    Optional control link 

    Optional access link 

    Save machine status

    Local data

    Temporaries

    Caller’s

    responsibility

    to initialize

    Callee’s

    responsibility

    to initialize

    fp

    (frame pointer)

  • 8/19/2019 Lecture#22.ppt

    21/24

    21

    Control Links

    fp

    spControl link 

    Stack 

    growth

    Callee’s activation record

    Caller’s activation record

    The control link is the old

    value of the fp

  • 8/19/2019 Lecture#22.ppt

    22/24

    22

    Access Links (Static Links)s

    a "

    $(1,9) 

    access % v

    s

    a "

    $(1,9) 

    access % v

    $(1,+) 

    access

     % v

    s

    a "

    $(1,9) 

    access % v

    $(1,+) 

    access

     % v

     p(1,+) access

     i !

    s

    a "

    $(1,9) 

    access % v

    $(1,+) 

    access

     % v

     p(1,+) access

     i !

    e(1,+) 

    access

     

    The access link points to the

    activation record of the static

    parent procedure:

    s is parent of r, e, and $ 

    $  is parent of p

  • 8/19/2019 Lecture#22.ppt

    23/24

    23

    Accessing Nonlocal Data

    • To implement access to nonlocal data a in

    procedure p, the compiler generates code to

    traverse n p - na access links to reach theactivation record where a resides

    – n p is the nesting depth of procedure p

    – na is the nesting depth of the procedurecontaining a

  • 8/19/2019 Lecture#22.ppt

    24/24

    24

    Parameter Passing Modes

    •   Call-by-value: evaluate actual parameters andenter r-values in activation record

    •   Call-by-reference: enter pointer to the storage ofthe actual parameter