Learning Center
Plans & pricing Sign in
Sign Out

Runtime Environments Runtime Environments What is in


What is in the memory?
    Memory organization during program
    Static runtime environments
    Stack-based runtime environments
         Without local procedure
         With local procedure
    Parameter passing

2301373                   Runtime Environment   2
Memory organization during program
                                                Main memory
                    Code area                       Store large amount of
                 Global/static area                 Slower access

     Data area         stack                    Registers
                                                    Very small amount of
                    Free space
                                                    Faster access


                  Main memory
2301373                               Runtime Environment                    3
Code Area
    Addresses in code
     area are static (i.e. no
     change during
     execution) for most                       entry point

     programming                               entry point   proc 1

     language.                                               proc 2

    Addresses are known                       entry point
     at compile time.                                        proc n

2301373                  Runtime Environment                          4
Data Area

    Addresses in data area are static for some
     data and dynamic for others.
         Static data are located in static area.
         Dynamic data are located in stack or heap.
             Stack (LIFO allocation) for procedure activation
              record, etc.
             Heap for user allocated memory, etc.

2301373                        Runtime Environment               5
    General-purpose registers
         Used for calculation
    Special purpose registers
         Program counter (pc)
         Stack pointer (sp)
         Frame pointer (fp)
         Argument pointer (ap)

2301373                    Runtime Environment   6
Calling Sequence
    Sequence of operations that must be done for
     procedure calls
         Call sequence
           Sequence of operations performed during procedure
               Find the arguments and pass them to the callee.
               Save the caller environment, i.e. local variables in activation
                records, return address.
               Create the callee environment, i.e. local variables in activation
                records, callee’s entry point.
         Return sequence
           Sequence of operations performed when return from
            procedure calls
               Find the arguments and pass them back to the caller.
               Free the callee environment.
               Restore the caller environment, including PC.

2301373                             Runtime Environment                             7
Issues in Call Sequence
    Which part of the call sequence is
     included in the caller code? Which is in the
     callee code?
         Save space in the code segment if the call
          sequence is included in the callee code.
         Normally, the caller finds the arguments and
          provides them to the callee.
    Which operation is supported in
         The more operations supported in hardware,
          the lower cost (i.e. execution time and space
          for code) is.

2301373                    Runtime Environment            8
Static runtime environments
    Static data
         Both local and global variables are allocated
          once at the beginning and deallocated at
          program termination
         Fixed address
    No dynamic allocation
    No recursive call
         Procedure calls are allowed, but no recursion.
         One activation record for each procedure,
          allocated statically
    Example:FORTRAN 77
2301373                    Runtime Environment             9
Memory Organization for
Static Runtime Environment
INTEGER MAX                                                  MAX
                                               Global area
…                                                            TAB(1)
                                        Activation record
QMEAN(TAB, 3, TEMP)                     for main             TAB(2)
…                                                            …
END                                                          TAB(10)
SUBROUTINE QMEAN(A, SIZE, MEAN)                              3
INTEGER MAX, SIZE            Activation record
                             for QMEAN                       SIZE
REAL A(SIZE), MEAN, TEMP                                     MEAN
INTEGER K                                                    Return addr
…                                                            TEMP
END                                                          K

2301373                  Runtime Environment                               10
Stack-based runtime environments

    Handle recursive calls
    Activation records are allocated in stack,
     called rumtime stack or call stack
    One procedure can have more than one
     activation records in the stack at one time
    Call sequence is more complex than the
     sequence in static environment

2301373               Runtime Environment      11
Stack-based Environments
Without Local Procedures
    Maintain pointer to the current
     activation record
         Store frame pointer in an fp register                    SP
                                                    local var.s
    Record the link from an activation             return addr
     record to the previous record                  control link   FP
         In each activation record, a link from    parameters
          an activation record to the activation
          record of the caller, called a dynamic
          link or control link is stored.
    Sometimes, the area for
     parameters and local variables
     need to be identified.
    A stack pointer is maintained in
     an sp register.
2301373                       Runtime Environment                   12
Call Sequence in Stack-based Environments
Without Local Procedures
                                                                                 Global area
    Calling sequence                                                             activation

                                                     Direction of stack growth
         Push arguments                                                          record of       fp
         Push fp as control link                                                               sp
         Copy sp to fp                                                          arguments
                                                                                 control link
         Store return address                                                                  sp fp
                                                                                 return addr
         Jump to callee                                                                        sp
                                                                                 local var.s
         Reserve space for local                                                               sp
          variables                                                              arguments
    Return sequence                                                             control link
                                                                                                sp fp
                                                                                 return addr
         Copy fp to sp                                                                         sp
                                                                                 local var.s
         Load control link into fp                                                             sp
         Jump to return address         Return
                                         Calling sequence
         Change sp to pop               Storearguments local variables and
                                         Computearea for into and push into stack
                                         Reserveintocontrol pop
                                         Jump fp return (to link
                                          Push to arguments
                                          Pop return link
                                         Load fp as address
                                         Move controlsp addr fplocal var.s
          arguments                      return addr)

2301373                        Runtime Environment                                                      13
Activation Tree
{ …;
  g(x);                                        Global area
  return(0); }                                 activation
                             main              record for
void g(int m)                                  main
{ …                           g(2)             activation
   f(y);      …                                record for
   g(y);                                       g(2)
   …              f(1)                  g(1)   activation
}                                              record for
void f(int n)                                  activation
{ g(n);                                        record for

2301373           Runtime Environment                       14
Calculating Address in Stack-
Based Environments
    Address of local variables
     and parameters are                           parameters            a
     calculated dynamically
     during execution time                        control link

    Offset of variables and                                            fp

     parameters from fp is:                       return address
         static                                                        x
                                                  local variables
         known during compile time                                     sp
         Stored in symbol table
                                             Address of x =fp+xOffset

                                             Address of a =fp+aOffset

2301373                     Runtime Environment                                15
Considerations in
Address Calculation
    Sizes of variables depend on data types
    Addressing elements in array
         Number of dimensions
         Size of array
         Ordering in storage
             Row-major order
             Column-major order

2301373                      Runtime Environment   16
Local Temporaries
          X = (a+b)/((c-d)*f(i))

    Values of (a+b) and (c-d) must be stored
     before f can be called.
    Thus, area for temporary variables must
     be reserved in the stack.
    Usually, temporaries are pushed into
     stacks after local variables.

2301373              Runtime Environment       17
Nested Declarations
    Local variables of blocks can be defined.
                                                     rest of stack
     void f
     { int        x;    int    i;                   control link of f
        …                                                               activation
        {         char x;      int         j;       return addr of f     record
                  …                                                        of f
           }                                               i
           …                                               x            local var.
     }                                                     j             of block

    A block could be treated as a procedure, but it is
    Another simpler method is to:
         push local variables in stack when the block is entered
         pop the local variables when the block is exited
2301373                       Runtime Environment                              18
Local Procedures
program     main;
var a[10]:int;
  proc      cal(a[10]:int);
  var       ans:real;
      func sum(e[10]:int);           ans=ave(a,10);
      var t: int;                    return;
      begin                      end
            return(t);       begin
      end                        input(a);
      func ave(b[10],n:int);     cal(a);
      var     ans:int;           return;
      begin                  end
2301373             Runtime Environment          19
Stack-based Environments with
Local Procedures
    Access link/static link
     must be included in each
     activation records                             parameters
         Represents the defining                   access link    activation record
                                                    control link
    Access link and control                        return addr
     link need not be the same                       local var.
    Control link
         represents the calling

2301373                       Runtime Environment                                20
Call Sequence in Stack-based Environments
With Local Procedures           global area
program        main;                              record for
var     a[10],n :int;                               main
   proc        cal(a[10]:int);                      a[10]
   var ans:real;
        func sum(e[10]:int);                     access link
        var    t: int;                                          activation
        begin                                    control link
               … ans                             return addr      for cal
        end                                          ans
        func ave(b[10]);                            b[10]
        var    ans:int;
        begin                                    access link
               ans=sum(b);                                      activation
                                                 control link
               return(ans/n);                                    record
        end                                      return addr     for ave
        ans=ave(a,10));                              ans
        return;                                     e[10]
   end                                           access link
begin                                                           activation
   n=10;                                         control link
   input(a);                                                     record
   cal(a);                                       return addr     for sum
   return;                                            t

2301373                    Runtime Environment                               21
Call Sequence
    Calling sequence                                  Return
         Push arguments                                sequence
         Push access link
         Push fp as control link                          Copy fp to sp
         Copy sp to fp                                    Load control link
         Store return address                              into fp
         Jump to callee                                   Pop access link
         Reserve space for local variables                Jump to return
                                                           Change sp to
                                                            pop arguments

    How to find access link

2301373                       Runtime Environment                               22
Accessing Variables in
Local Procedures
    Access Chaining
         Follow access links from one activation record
          until the required variable is found
         Inefficient
    Display
         Access links are stored in an array called

2301373                    Runtime Environment             23
Accessing Chaining global area
                                                              record for
    Nesting level                                              main
         Indicates the depth of the                            a[10]
          procedure in program definition                    access link
             Level 0: outermost                             control link      record
         Need to be stored in the symbol                    return addr        for cal
          table                                                  ans        nesting level: 1
    To find the activation record for                          b[10]
     the nth nesting level from the                          access link
     activation record in the ith                            control link      record
     nesting level                                           return addr       for ave
         Follow the access link (n-i) times                     ans        nesting level: 2
    Examples:                                               access link
         To access ans in cal                               control link
             Follow links 2-1 times                         return addr       for sum
         To access n in main                                     t         nesting level:2
             Follow links 2-0 times

2301373                                Runtime Environment                                     24
Display                                       global area
                                               record for
    Access links are                           main
     stored in the array                         a[10]
                                              access link
     display.                                 control link
    The activation record                    return addr
                                                  ans        cal
     of a procedure in the                       b[10]
     ith nesting level is                     access link
     stoted in display[i].                    control link
                                              return addr
                                                  ans      ave
                                              access link
                                              control link
                                              return addr
                                                   t         sum

2301373                 Runtime Environment                                     25
Parameter Passing
    Pass   by   value
    Pass   by   reference
    Pass   by   value-result
    Pass   by   name

2301373                  Runtime Environment   26
Pass by Value
    Value parameters are          void change(int x)
     not changed during            { x++;
     the execution
    Only the value is sent
     into the procedure,           void main()
     and are used locally          { int y=0;
    When the control is             change(y);
     returned from the               printf(“%d\n”,y);
     callee, the value of            return;
     the parameter is not
     passed back to the            Output:
     caller.                         0
2301373                 Runtime Environment              27
Pass by Reference
    The reference to a variable is                   void change (int
     passed to the callee.                              &x)
    The callee used the reference                    { x++;
     (address) to refer to the                          return;
     variable.                                        }
         Indirect addressing is needed to
          refer to parameters
                                                      void main()
    The variable in the caller and
     the referenced memory in the                     { int y=0;
     callee share the same memory                       change(y);
     location.                                          printf(“%d\n”,y);
    The value of the variable in the                   return;
     caller is also changed when the                  }
     referenced in the callee is
     changed.                                         Output:

2301373                         Runtime Environment                         28
Pass by Value-Result
    The value of the                             void change(int x)
     parameter is copied into                     { x++;
     the callee when the callee
     is entered.
    New memory location is             void main()
     provided for the                   { int y=0;
     parameter in the callee’s            change(y);
     activation record.                   printf(“%d\n”,y);
         No indirect address is needed   return;
    When the control is
     returned to the caller, the                  Output:
     value is copied back.                          1
2301373                     Runtime Environment                        29
Pass by Name
    The argument is replaced           void change(int x)
     by the textual                     { i=4;
     representation of the                x++;
     parameter passed from                return;
     the caller.                        }
    The evaluation of the
                                        void main()
     actual parameters must             { int i=0;
     be delayed until the                 int a[5]={0,0,0,0,0};
     execution.                           change(a[i]);
    A procedure (thunk) is               return;
     called to find the value of        }
     the parameter in the
     callee.                            After the call:

2301373                      Runtime Environment              30

To top