unit-v syllabus introduction · 2019-03-22 · explicit allocation of fixed size block contd…...

11
Blog: anilkumarprathipati.wordpress.com 1 UNIT-V SYLLABUS Storage Organization: Storage language Issues, Storage Allocation, Storage Allocation Strategies, Scope, Access to Nonlocal Names, Parameter Passing, Dynamics Storage Allocation Techniques. 1 Introduction What is Runtime Environment? It is the environment created and managed by compiler in which the compiler assumes that its target programs are being executed. Runtime environment requirement? To successfully implement the various issues of the source languages, like names, scope, bindings, data types, procedures, parameters, etc.. And also the compiler must be cooperate with OS. The compiler must also work together with the other system software to support the issues on the target machine. 2 What are the issues dealt by Runtime Environment? Issues handled by runtime environments are: Layout and allocation of storage locations for objects named in the source program. Mechanisms used by target program to access variables. Linkage between procedures. Mechanisms for passing parameters. Interface to the operating system, I/O devices and other programs. 3 Storage Language Issues Compiler must do the storage allocation and provide access to variables and data Memory management Code Static Stack allocation Heap management Storage Organization

Upload: others

Post on 03-May-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Blog: anilkumarprathipati.wordpress.com 1

UNIT-V SYLLABUS

Storage Organization: Storage language Issues, Storage Allocation, Storage Allocation Strategies, Scope, Access to Nonlocal Names, Parameter Passing, Dynamics Storage Allocation Techniques.

1

Introduction What is Runtime Environment? It is the environment created and managed by compiler in which the compiler assumes that its target programs are being executed. Runtime environment requirement? To successfully implement the various issues of the source languages, like names, scope, bindings, data types, procedures, parameters, etc.. And also the compiler must be cooperate with OS. The compiler must also work together with the other system software to support the issues on the target machine.

2

What are the issues dealt by Runtime Environment? Issues handled by runtime environments are: • Layout and allocation of storage locations for

objects named in the source program. • Mechanisms used by target program to access

variables. • Linkage between procedures. • Mechanisms for passing parameters. • Interface to the operating system, I/O devices and

other programs.

3

Storage Language Issues

• Compiler must do the storage allocation and provide access to variables and data

• Memory management

– Code

– Static

– Stack allocation

– Heap management

Storage Organization

Blog: anilkumarprathipati.wordpress.com 2

Static vs. Dynamic Allocation

• Static: Compile time, Dynamic: Runtime allocation

• Many compilers use some combination of following – Stack storage: for local variables, parameters and so

on

– Heap storage: Data that may outlive the call to the procedure that created it

• Stack allocation is a valid allocation for procedures since procedure calls are nested

• Static allocation lays out storage for all data objects at compile time.

• Stack allocation manages the runtime storage as a stack.

• Heap allocation allocates and deallocates storage as needed at runtime from a data area called heap.

6

Storage Allocations

Stack Allocation: Sketch of a quicksort program Activation for Quicksort

Blog: anilkumarprathipati.wordpress.com 3

Activation tree representing calls during an execution of quicksort

Activation records

• Procedure calls and returns are usually managed by a run-time stack called the control stack.

• Each live activation has an activation record (sometimes called a frame).

• The root of activation tree is at the bottom of the stack.

• The current execution path specifies the content of the stack with the last activation has record in the top of the stack.

Activation Record • Temporary values - that arise in the evaluation of an

expression

• Local data - that is local to the execution of the procedure.

• A saved machine status- status of machine just before the function call.

• Access link - non-local data held in other activation records.

• Control link - activation record of caller.

• Return value - used by the called procedure to return a value to calling procedure.

• Actual parameters - used by the calling procedure

Downward-growing stack of activation records

Blog: anilkumarprathipati.wordpress.com 4

Designing Calling Sequences

• Values communicated between caller and callee are generally placed at the beginning of callee’s activation record

• Fixed-length items: are generally placed at the middle

• Items whose size may not be known early enough: are placed at the end of activation record

• We must locate the top-of-stack pointer judiciously: a common approach is to have it point to the end of fixed length fields.

Access to dynamically allocated arrays

Calling Sequence

• The caller evaluates the actual parameters

• The caller stores a return address and the old value of top-sp into the callee's activation record.

• The callee saves the register values and other status information.

• The callee initializes its local data and begins execution.

corresponding return sequence

• The callee places the return value next to the parameters

• Using information in the machine-status field, the callee restores top-sp and other registers, and then branches to the return address that the caller placed in the status field.

• Although top-sp has been decremented, the caller knows where the return value is, relative to the current value of top-sp; the caller therefore may use that value.

Blog: anilkumarprathipati.wordpress.com 5

Division of tasks between caller and callee Scope

• The scope of a variable x is the region of the program in which uses of x refers to its declaration. One of the basic reasons of scoping is to keep variables in different parts of program distinct from one another.

• Scoping is generally divided into two classes:

1.Static Scoping

2.Dynamic Scoping

18

Static scoping

• Static scoping is also called lexical scoping. In this scoping a variable always refers to its top level environment. This is a property of the program text and unrelated to the run time call stack.

• Variables are always statically (or lexically) scoped. Binding of a variable can be determined by program text and is independent of the run-time function call stack.

• Example: C, C++ and Java.

19

Example #include<stdio.h>

int x = 10;

int f()

{

return x;

}

int g()

{

int x = 20;

return f();

}

20

int main() { printf("%d", g()); printf("\n"); return 0; } OUTPUT: 10

Blog: anilkumarprathipati.wordpress.com 6

Dynamic scoping

• With dynamic scope, a global identifier refers to the identifier associated with the most recent environment, and is uncommon in modern languages.

• Example: Perl.

21

Example $x = 10;

sub f

{

return $x;

}

sub g

{

local $x = 20;

return f();

}

print g()."\n";

22

OUTPUT: 20

Nonlocal Names Accessing A version of quicksort, in ML style Access links for finding nonlocal data

Blog: anilkumarprathipati.wordpress.com 7

Parameter Passing

25

• r-value: The value of an expression is called its r-value. It appears on the right-hand side of the assignment operator. r-values can always be assigned to some other variable.

• l-value: The location of memory (address) where an expression is stored is known as the l-value of that expression. It always appears at the left hand side of an assignment operator.

Ex 1: day = 1; week = day * 7; month = 1; year = month * 12;

Ex 2: 7 = x + y;

26

Parameter Passing Contd...

• The communication medium among procedures is known as parameter passing.

Parameters are two types:

i. Formal Parameter: Variables that take the information passed by the caller procedure are called formal parameters. These variables are declared in the definition of the called function.

ii. Actual Parameter: Variables whose values and functions are passed to the called function are called actual parameters. These variables are specified in the function call as arguments.

Parameter Passing Methods

• Call by values

• Call by Reference

• Call by copy restore

• Call by name

27

Parameter Passing Methods

• Call by copy restore - compiler copies the value in formal parameters when the procedure is called and copy them back in actual parameters when control returns to the called function.

28

void f(int x) { x= 2; //a is still 1 } //function ends so the value of x is now stored in a -> value of a is now 2

int main() { int a= 1; f(a); //when this ends the value of a will be 2 printf("%d\n", a); //prints 2 }

Blog: anilkumarprathipati.wordpress.com 8

Dynamic Storage Allocation

• The techniques needed to implement dynamic storage allocation techniques depends on how the space is de-allocated. i.e. implicitly or explicitly.

• Explicit allocation of Fixed size block allocation.

• Explicit allocation of Variable size block allocation.

• Implicit de-allocation.

29

Explicit allocation of Fixed size block

• The simplest form of dynamic storage mechanism.

• The blocks linked together in a list and the allocation and de-allocation done in quickly with less or no storage overhead.

• A pointer available points to the first block of the list of available blocks.

30

31

Explicit allocation of Fixed size block Contd… Explicit allocation of Variable size block

• When blocks are allocated & deallocated storage can become fragmented i.e., heap may consist alternate blocks that are free & in use.

• In variable size allocation it will be a problem because we could not allocate a block larger than any free blocks, even though the space is available.

• The first fit method can be used to allocate variable sized block

32

Blog: anilkumarprathipati.wordpress.com 9

Explicit allocation of Variable size block

• When a block of size is allocated it search for the first free block size f>=s. This block is then subdivided in to a used block of size ‘s’ & a free block of size ‘f-s’. Its time consuming;

• When a block is deallocated, it check to see if it is next to a free block. If possible, the deallocated is combined with a free block next to it to create larger free block. It helps to avoid fragmentation.

33 34

Explicit allocation of Variable size block Contd…

Implicit de-allocation

• Implicit de-allocation requires the cooperation between user program & runtime packages. this is implemented by fixing the format of storage blocks.

35

• The first problem is to recognize the block boundaries, for fixed size it is easy.

• In variable size block the size of block is kept in a inaccessible storage attached to the block.

• The second problem is of recognizing the if a block is in use. Used block can be referred by the user program using pointers. The pointers are kept in a fixed position in the block for the easiness of checking the reference .

36

Implicit de-allocation Contd..

Blog: anilkumarprathipati.wordpress.com 10

• Two approaches can be used for implicit de-allocation.

– Reference counts

– Marking techniques

37

Reference Counting

• Basic idea is give each chunk a special field that is the number of references to chunk. Whenever, new reference is made, field increment by 1. Whenever a reference is removed, decrement field by 1. when reference count goes to zero, collect chunks. It requires compiler support.

38

• Maintaining reference counts can be costly in time(the pointer assignment p:=q leads to changes in the reference counts of the blocks pointed by both p & q).

• Reference counts are best if there is no cyclical reference occurs.

39

Reference Counting Contd... Marking Technique

• The basic idea is - go through all memory and mark every chunk that is referenced and make a second pass through memory and remove all chunks not marked.

• Here the user program suspend temporarily & use the frozen pointers to determine the used blocks. This approach requires all the pointers to the heap to be known.(conceptually, it’s like pouring paint to the heap through the pointers).

40

Blog: anilkumarprathipati.wordpress.com 11

41

Marking Technique Contd...