Presentation is loading. Please wait.

Presentation is loading. Please wait.

Run-Time Environments Presented By: Seema Gupta 09MCA102.

Similar presentations


Presentation on theme: "Run-Time Environments Presented By: Seema Gupta 09MCA102."— Presentation transcript:

1 Run-Time Environments Presented By: Seema Gupta 09MCA102

2 Run-Time Environments How do we allocate the space for the generated target code and the data object of our source programs? The places of the data objects that can be determined at compile time will be allocated statically. But the places of the some of data objects that can be determined at run-time will be allocated dynamically. The allocation and de-allocation of the data objects is managed by the run-time support package. – run-time support package is loaded together with the generate target code. – the structure of the run-time support package depends on the semantics of the programming language (especially the semantics of procedures in that language).

3 SOURCE LANGUAGE ISSUES Procedures Activation Trees Control Stacks The Scope of Declaration Binding of Names

4 Procedure A procedure definition is a form of declaration that associates an identifier(procedure name) with a statement(procedure Body). When a procedure name appears within an executable statement, it is said to be called at that point. Parameters passed to a function or procedure are two types: - Formal parameter :- identifiers that are appearing in a procedure definition. - Actual parameter :- identifiers that passed to a called procedure.

5 Procedure Activations Each execution of a procedure is called as an activation of that procedure. An execution of a procedure starts at the beginning of the procedure body; When the procedure is completed, it returns the control to the point immediately after the place where that procedure is called. Lifetime of an activation of a procedure is the sequence of the steps between the first and the last steps in the execution of that procedure (including the other procedures called by that procedure). If a and b are procedure activations, then their lifetimes are either non-overlapping or are nested. If a procedure is recursive, a new activation can begin before an earlier activation of the same procedure has ended.

6 A quicksort program

7 Activation Tree The nested property of activation lifetimes can be illustrated by inserting two print statements in each procedure, one before the first statement of the procedure body and other after the last. The first statement prints “enter” followed by the name of procedure and values of actual parameters and last statement prints “leave” followed by same information. We can use a tree (called activation tree) to show the way control enters and leaves activations. In an activation tree: – Each node represents an activation of a procedure. – The root represents the activation of the main program. – The node a is a parent of the node b iff the control flows from a to b. – The node a is left toS the node b iff the lifetime of a occurs before the lifetime of b.

8 Output suggesting the activations of procedure

9 Activation Tree (cont.)

10 Control Stack The flow of the control in a program corresponds to a depth-first traversal of the activation tree that: – starts at the root, – visits a node before its children, and – recursively visits children at each node an a left-to-right order. A stack (called control stack) can be used to keep track of live procedure activations. – An activation record is pushed onto the control stack as the activation starts. – That activation record is popped when that activation ends. When node n is at the top of the control stack, the stack contains the nodes along the path from n to the root.

11 Activations with labels r, p(1,9), p(1,3) and q(1,0) have executed to completion, so the figure contains dashed lines to their nodes.

12 The Scope of a declaration The same variable name can be used in the different parts of the program. The scope rules of the language determine which declaration of a name applies when the name appears in the program. An occurrence of a variable (a name) is: – local: If that occurrence is in the same procedure in which that name is declared. – non-local: Otherwise (ie. it is declared outside of that procedure) procedure p; var b:real; procedure p; var a: integer; a is local begin a := 1; b := 2; end; b is non-local begin... end;

13 Binding of names In the programming language semantics, the term environment refers to a function that maps a name to a storage location and the term state refers to a function that maps a storage location to the value held there. When an environment associated storage location s with a name x, we say that x is bound to s. A binding is the dynamic counterpart of a declaration. as we have seen, more than one activation of a recursive procedure can be alive at the same time.

14 Storage organization Presented By: Neha Jain 09MCA026

15 Run-Time Storage Organization CS416 Compiler Design15 Code Static Data Stack Heap Memory locations for code are determined at compile time. Locations of static data can also be determined at compile time. Data objects allocated at run-time. (Activation Records) Other dynamically allocated data objects at run-time. (For example, malloc area in C).

16 Activation Records Information needed by a single execution of a procedure is managed using a contiguous block of storage called activation record. An activation record is allocated when a procedure is entered, and it is de-allocated when that procedure exited. Size of each field can be determined at compile time (Although actual location of the activation record is determined at run-time). – Except that if the procedure has a local variable and its size depends on a parameter, its size is determined at the run time. CS416 Compiler Design16

17 Activation Records (cont.) CS416 Compiler Design17 return value actual parameters optional control link optional access link saved machine status local data temporaries The returned value of the called procedure is returned in this field to the calling procedure. In practice, we may use a machine register for the return value. The field for actual parameters is used by the calling procedure to supply parameters to the called procedure. The optional control link points to the activation record of the caller. The optional access link is used to refer to nonlocal data held in other activation records. The field for saved machine status holds information about the state of the machine before the procedure is called. The field of local data holds data that local to an execution of a procedure.. Temporay variables is stored in the field of temporaries.

18 Parameter passing Actual Parameters – Identifiers that are in procedure call statement Formal Parameters – Identifiers that are in procedure defination CS416 Compiler Design18

19 Parameter Passing Techniques Call-by-value Call-by-reference Call-by-name (used by Algol) Copy-restore CS416 Compiler Design19

20 Each variable is associated with two values: l-value – Refers to the storage represented by an expression (if value appears in left side of the expression) r-value – Refers to the value contained in the storage CS416 Compiler Design20

21 Call-by-value Actual value parameters are evaluated and their r- values are passed to the called procedure Used in C and PASCAL In this type of implementation – Formal parameters are treated just like local variables and hence the storage of the formals is in the activation of the called procedure – The caller evaluates the actual parameters and places their r-values in the storage of formals Formals do not affect the values in the activation record of the caller CS416 Compiler Design21

22 Call-by-reference The address or the reference of the variables will be passed as parameters to the called procedure. The caller passes a pointer to the address of each variable to the called procedure In this type of implementation – If the actual parameter is a name or a expression having an l-value, then l-value itself is passed – If the expression has no l-value,then the address of the location itself is passed Formals do affect the values in the activation record of the caller CS416 Compiler Design22

23 Copy Restore Its hybrid between call-by-value and call-by-reference It is also called as copy-in-copy-out or value-result technique In this type of implementation – Once the procedure is called, the actual parameters are evaluated and the r-value of the parameters are passed to the called procedure. This resembles the call-by-value technique. – When the called procedure ends and subsequently the control returns, the current r-values of the formal parameters are copied back into l-values of the actuals. This resembles to copy-by-reference technique. The first step is called copy-in and second step is called copy- out Some implementations of FORTRAN use this technique CS416 Compiler Design23

24 Call-by-name Also called as copy rule or macro-expansion type parameter passing In this type of implementation – The procedures are treated like a macro. Whenever there is a procedure call statement, the procedure body is substituted in the place of call. – By means of this the actual parameters will be literally substituted for the formals and hence the name macro- expansion or in-line expansion – The variable used in called procedure need to be distinct from the names of the calling procedure – Usually, the actual parameters are surrounded by the parentheses to preserve integrity Algol uses this type of parameter passing CS416 Compiler Design24


Download ppt "Run-Time Environments Presented By: Seema Gupta 09MCA102."

Similar presentations


Ads by Google