650 likes | 685 Views
PCD course. UNIT-4 Run-Time Storage Management and symbol table management. Run time environment. In previous units what we studied Phases of Compiler Now we turn our attention to the task of how a compiler generates executable code.
E N D
PCD course UNIT-4 Run-Time Storage Management and symbol table management
Run time environment • In previous units what we studied Phases of Compiler • Now we turn our attention to the task of how a compiler generates executable code
During execution of source program some data objects like variables are very important factors for code generation. • These variables are referred by their addresses. • Addresses to these data objects are dependent on
The organization of memory. • But the organization of memory is driven by source language features. • In this unit we will study organization of memory and allocation strategies
Some issues about source language & need of Run time storage • Does it support recursion ? when a procedure is recursively called then memory allocation is needed to store each instance with its copy of local variables and parameters passed to that recursive procedure . But the no. of active instances is decided by run time.
How the parameters are passed to the procedure. Two methods : call by value and call by references .the allocation strategies for each of these methods are different
Does language support memory allocation and deallocation. Dynamically. • Dynamic allocation and deallocation of memory brings the effective utilization of memory. • Three is great effect of these source language issues on run time environment. • Hence runtime storage management is very important.
Questions asked • Explain term runtime support and storage organization. 2007,08,10 10 marks • Compare static ,stack and heap allocation 8 marks • What are the allocation strategies explain anyone in detail 8 marks
Run time storage • Compiler must do the storage allocation and provide access to variables and data so it demands for the block of memory to operating system. • This block of memory is called as runtime storage.
Runtime environment • And runtime environment is nothing but the program during the execution and consists of source code of program, static and global variables , local variables, arguments passed to function or procedure etc
This runtime storage is divided to hold code and data such as 1. the generated target code 2. data objects 3. information which keeps track of procedure activation. • Memory layout of an executable program or storage Organization or subdivision of runtime memory
Code area :The size of generated code is fixed . Hence the target code occupies the statically determined area of memory. Compiler places the target code at the lower end of memory. • Data area : it consists of different types of variables i.e global, local, static constants and dynamic The size of space required for all these variables is known at compile time and then they are converted to target code.
But the space required for dynamic variables is known at execution time so it is unknown at compile time .so dynamic allocation can be done in heap • Control stack is used to keep the track of procedure activation.(how stack is used during procedure call ?) • The size of stack and heap is not fixed it may grow or shrink interchangeably during the program execution . Ex.pascal and C needs runtime stack.
Storage allocation strategies • Based on the subdivision of runtime storage following different storage allocation strategies are used • Static allocation : it is for all the data objects at compile time. • Stack allocation : here stack is used to manage the runtime storage • Heap allocation : in heap allocation heap is used to manage the dynamic memory allocation.
Static allocation • Size of the data objects is known at compile time. • The names of these objects are bound to storage at compile time only • The binding of names with the amount of storage allocated do not change at run time. Hence the name of this allocation is static allocation.
In static allocation the compiler can determine the amount of storage required by each data object. • And therefore it becomes easy for a compiler to find the address of these data in the activation record. • At compile time compiler can fill the addresses at which the target code can find the data it operates on
Limitations • The static allocation is done only if the size of the data object is known at compile time. • Data structures can be created dynamically • Recursive procedures are not supported by this type of allocation
Stack allocation • Stack allocation is strategy in which the storage is organized as stack. Called as control stack. • As activation begins the activation records are pushed onto the stack and on completion of this activation the corresponding activation records can be popped. • The data structures can be created dynamically for stack allocation
Limitations of stack • The memory addressing can be done using pointers and index registers. Hence this type of allocation is slower than static allocation
Heap allocation • Heap is used to manage dynamic memory allocation • Data structures and data objects can be created dynamically • If we want to retain a non local variable value even if the activation record is deactivated. Heap allocation is used to retain such non local variables. • For retaining non local variables stack is not used because it is LIFO nature.
The heap allocation allocates the continuous block of memory when required for activation records or other data objects. • Memory is deallocated after activation record ends. This free space can be further reused by heap manager.
The efficient heap management can be done by • 1. creating a linked list for the free blocks and when any memory is appended in the linked list. • 2. it will use best fit technique for allocation of block.
Activation record • It is contiguous block of storage to manage the information needed by a single execution of a procedure. • It is data about an execution of a procedure • Another name is stack frame • Pascal and c used uses stack area to store activation record. While FORTRAN uses static area to store the activation record. • It consists of collection of fields as shown
Activation Record • Temporary values: Temporary variables are needed during the evaluation of expression. Such variables are stored in the temporary field. • Local data variables : local data is the data that is local to the execution of procedure is stored in this field. • machine status : contents of m/c registers are stored in this field. When a procedure is called and before a control goes to a procedure code. The status of the current program is stored onto stack. It includes m/c registers and program counters. • An “access link” :it is used to refer to a nonlocal data in other activation record . It is static link field.
A control link : it is dynamic link . It points to the activation record of calling procedure. If A calls B then link is from B to A • Space for the return value of the called function : it stores result of function call. • The actual parameters used by the calling procedure : it holds actual parameters. These parameters are passed to the called procedure. • Ex. Show snapshots of activation records for Factorial program
Designing procedure Calling Sequences :Procedure call and return sequence • Two entities are involved Caller and Callee procedure • There will be two activation records one for caller and another for callee • Some tasks should be done by caller and some should be done by callee • Division of tasks between caller and callee • Calling sequence • Return sequence
Designing Calling Sequences :Procedure call and return sequence • 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 carefully: a common approach is to have it point to the end of fixed length fields.
Procedure call and return sequences • Procedure calls are implemented by generating calling sequences in target code • When there is a call for procedure then • Activation record for that procedure is allocated statically at compile time. or • Dynamically all information get inserted into fields. • Return sequence restores the state of m/c so the calling procedure can continue execution. • It also destroy the activation record.
The calling sequence is often divided between the caller (calling proc)and callee (called proc). • Each call has its own actual parameters • The caller usually evaluates actual parameters and communicates them to activation record of callee.
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.
Procedure Parameters We know two types of parameters • Actual parameters • formal parameters Based on these parameters there are various parameter passing methods • Call by value • Call by reference • Copy restore or copy in copy out or values result • Call by name
Call by value • Simplest method • Actual parameters are evaluated and their r – values are passed to callee. And placed in the AR of callee a = 10 l-value r-value • Operations are done on formal parameters. They do not changes the values of actual parameters. • parameterpassing\Argument passing .docx
Swap(in x, int y) { int temp; temp=x ; x=y ; y=temp; } Main() {int a=1,b=2; Swap(a,b); Print(“a=%d , b= %d\n” a,b); }
Call by reference or call by address • The L –value that is the address of actual parameter is passed to the called routines activation record : &y
Copy restore • It is a hybrid between call by value and call by reference also known as copy-in copy out or value result. • It is implemented as • 1. (a) before control flows to the called proc the actual parameters are evaluated. (b) the r –values of the actuals are passed to the called procedure as in call by value. (c ) in addition the r –values of those actual parameters having l - values are determined before call. 2. When control returns , the current r-values of the formal parameters are copied back into the l –values of the actuals
Symbol table • Explain the significance and design of symbol table in the context of compiler.
Role of Symbol Table • Very Essential data structure for compiler • Used for storing information about identifiers appearing in a source program • Lexical Analyzer and Parser fill up symbol table • Code generator and optimizer make use of symbol table • Entities stored in a symbol table • Variables, procedures, functions, defined constants, labels, structures, file identifications, compiler generated temporaries
Information in symbol table • Name: May be stored directly in the table or the entry may point to another character string, possibly in an associated string table • Type: type of identifier • Whether variable / function / procedure name • For variables, identify whether integer / real / array … • Location: offset in the program where the identifier is defined • Scope: identifies the region of the program in which the current symbol definition is valid • Other attributes: Array limits, record fields / parameters / return values of functions
Usage of symbol table information • Semantic Analysis: check correct semantic usage of language constructs • May need checking types of identifiers • Code generation: All program variables and temporaries need to be allocated some memory locations • Symbol table provides information regarding memory size required for identifiers by their types • Error Detection: Leave variables undefined • Optimization: To reduce the total number of variables used in a program we need to reuse the temporaries generated by the compiler
Features of symbol tables • Insert • Delete • Lookup • Modify
Simple symbol table: Operations • Enter a new symbol into the table • Lookup for a symbol • Modify information about a symbol stored earlier
Simple Symbol Table Formats • linear List : simplest but poor performance • Search Tree : AVL • Hash table