if you want to remove an article from website contact us from top.

    run time storage allocation in compiler design

    Mohammed

    Guys, does anyone know the answer?

    get run time storage allocation in compiler design from screen.

    Compiler Design

    Compiler Design - Run-Time Environment, A program as a source code is merely a collection of text (code, statements etc.) and to make it alive, it requires actions to be performed on the target machin

    Compiler Design - Run-Time Environment

    Advertisements Previous Page Next Page

    Complete Python Prime Pack for 2023

    9 Courses       2 eBooks Lifetime Access 30-Days Money Back Guarantee

    Buy Now

    Artificial Intelligence & Machine Learning Prime Pack

    6 Courses       1 eBooks Lifetime Access 30-Days Money Back Guarantee

    Buy Now

    Java Prime Pack 2023

    8 Courses       2 eBooks Lifetime Access 30-Days Money Back Guarantee

    Buy Now

    A program as a source code is merely a collection of text (code, statements etc.) and to make it alive, it requires actions to be performed on the target machine. A program needs memory resources to execute instructions. A program contains names for procedures, identifiers etc., that require mapping with the actual memory location at runtime.

    By runtime, we mean a program in execution. Runtime environment is a state of the target machine, which may include software libraries, environment variables, etc., to provide services to the processes running in the system.

    Runtime support system is a package, mostly generated with the executable program itself and facilitates the process communication between the process and the runtime environment. It takes care of memory allocation and de-allocation while the program is being executed.

    Activation Trees

    A program is a sequence of instructions combined into a number of procedures. Instructions in a procedure are executed sequentially. A procedure has a start and an end delimiter and everything inside it is called the body of the procedure. The procedure identifier and the sequence of finite instructions inside it make up the body of the procedure.

    The execution of a procedure is called its activation. An activation record contains all the necessary information required to call a procedure. An activation record may contain the following units (depending upon the source language used).

    Temporaries Stores temporary and intermediate values of an expression.

    Local Data Stores local data of the called procedure.

    Machine Status Stores machine status such as Registers, Program Counter etc., before the procedure is called.

    Control Link Stores the address of activation record of the caller procedure.

    Access Link Stores the information of data which is outside the local scope.

    Actual Parameters Stores actual parameters, i.e., parameters which are used to send input to the called procedure.

    Return Value Stores return values.

    Whenever a procedure is executed, its activation record is stored on the stack, also known as control stack. When a procedure calls another procedure, the execution of the caller is suspended until the called procedure finishes execution. At this time, the activation record of the called procedure is stored on the stack.

    We assume that the program control flows in a sequential manner and when a procedure is called, its control is transferred to the called procedure. When a called procedure is executed, it returns the control back to the caller. This type of control flow makes it easier to represent a series of activations in the form of a tree, known as the activation tree.

    To understand this concept, we take a piece of code as an example:

    . . .

    printf(“Enter Your Name: “);

    scanf(“%s”, username);

    show_data(username);

    printf(“Press any key to continue…”);

    . . .

    int show_data(char *user)

    {

    printf(“Your name is %s”, username);

    return 0; } . . .

    Below is the activation tree of the code given.

    Now we understand that procedures are executed in depth-first manner, thus stack allocation is the best suitable form of storage for procedure activations.

    Storage Allocation

    Runtime environment manages runtime memory requirements for the following entities:

    Code : It is known as the text part of a program that does not change at runtime. Its memory requirements are known at the compile time.Procedures : Their text part is static but they are called in a random manner. That is why, stack storage is used to manage procedure calls and activations.Variables : Variables are known at the runtime only, unless they are global or constant. Heap memory allocation scheme is used for managing allocation and de-allocation of memory for variables in runtime.

    Static Allocation

    In this allocation scheme, the compilation data is bound to a fixed location in the memory and it does not change when the program executes. As the memory requirement and storage locations are known in advance, runtime support package for memory allocation and de-allocation is not required.

    Stack Allocation

    Procedure calls and their activations are managed by means of stack memory allocation. It works in last-in-first-out (LIFO) method and this allocation strategy is very useful for recursive procedure calls.

    Heap Allocation

    Variables local to a procedure are allocated and de-allocated only at runtime. Heap allocation is used to dynamically allocate memory to the variables and claim it back when the variables are no more required.

    Except statically allocated memory area, both stack and heap memory can grow and shrink dynamically and unexpectedly. Therefore, they cannot be provided with a fixed amount of memory in the system.

    स्रोत : www.tutorialspoint.com

    Explain run time storage organization in details.

    0 15k views

    Explain run time storage organization in details.

    written 6.4 years ago by

    rajyadav.engg • 380

    modified 2.8 years ago by

    prashantsaini • 0 compiler design ADD COMMENT EDIT

    1 Answer

    3 162 views

    written 6.4 years ago by

    rajyadav.engg • 380

    modified 6.4 years ago by

    ramnath • 100

    An executable program generated by a compiler will have the following organization in memory on a typical architecture (such as on MIPS):

    This is the layout in memory of an executable program.

    Note that in a virtual memory architecture (which is the case for any modern operating system), some parts of the memory layout may in fact be located on disk blocks and they are retrieved in memory by demand (lazily).

    The machine code of the program is typically located at the lowest part of the layout.

    Then, after the code, there is a section to keep all the fixed size static data in the program.

    The dynamically allocated data (ie. the data created using malloc in C) as well as the static data without a fixed size (such as arrays of variable size) are created and kept in the heap. The heap grows from low to high addresses.

    When you call malloc in C to create a dynamically allocated structure, the program tries to find an empty place in the heap with sufficient space to insert the new data; if it can't do that, it puts the data at the end of the heap and increases the heap size.

    The focus of this section is the stack in the memory layout. It is called the run-time stack.

    The stack, in contrast to the heap, grows in the opposite direction (upside-down): from high to low addresses, which is a bit counterintuitive. The stack is not only used to push the return address when a function is called, but it is also used for allocating some of the local variables of a function during the function call, as well as for some bookkeeping.

    Activate Record

    It is used to store the current record and the record is been stored in the stack.

    It contains return value .After the execution the value is been return.

    It can be called as return value.

    Parameter

    It specifies the number of parameters used in functions.

    Local Data

    The data that is been used inside the function is called as local address

    Temporary Data

    It is used to store the data in temporary variables.

    Links

    It specifies the additional links that are required by the program.

    Status

    It specifies the status of program that is the flag used.

    Return value Parameters Local Data Temporary Data Links(optional) Status

    Storage Allocation

    Runtime environment manages runtime memory requirements for the following entities:

    Code: It is known as the text part of a program that does not change at runtime. Its memory requirements are known at the compile time.Procedures: Their text part is static but they are called in a random manner. That is why, stack storage is used to manage procedure calls and activations.Variables: Variables are known at the runtime only, unless they are global or constant. Heap memory allocation scheme is used for managing allocation and de-allocation of memory for variables in runtime.Static Allocation

    In this allocation scheme, the compilation data is bound to a fixed location in the memory and it does not change when the program executes.

    As the memory requirement and storage locations are known in advance, runtime support package for memory allocation and de-allocation is not required.

    In a static storage-allocation strategy, it is necessary to be able to decide at compile time exactly where each data object will reside at run time. In order to make such a decision, at least two criteria must be met:

    The size of each object must be known at compile time.

    Only one occurrence of each object is allowable at a given moment during program execution.

    Because of the first criterion, variable-length strings are disallowed, since their length cannot be established at compile time. Similarly dynamic arrays are disallowed, since their bounds are not known at compile time and hence the size of the data object is unknown.

    Because of the second criterion, nested procedures are not possible in a static storage-allocation scheme. This is the case because it is not known at compile time which or how many nested procedures, and hence their local variables, will be active at execution time.

    Dynamic Allocation

    The allocation can be varied during the execution

    It makes the use of recursive function.

    In a dynamic storage-allocation strategy, the data area requirements for a program are not known entirely at compilation time.

    In particular, the two criteria that were given in the previous section as necessary for static storage allocation do not apply for a dynamic storage-allocation scheme.

    The size and number of each object need not be known at compile time; however, they must be known at run time when a block is entered.

    स्रोत : www.ques10.com

    7.1 Run

    7.1 Run-Time Storage Organization

    Next: 7.2 Case Study: Activation Up: 7 Activation Records Previous: 7 Activation Records   Contents

    7.1 Run-Time Storage Organization

    An executable program generated by a compiler will have the following organization in memory on a typical architecture (such as on MIPS):

    This is the layout in memory of an executable program. Note that in a virtual memory architecture (which is the case for any modern operating system), some parts of the memory layout may in fact be located on disk blocks and they are retrieved in memory by demand (lazily).

    The machine code of the program is typically located at the lowest part of the layout. Then, after the code, there is a section to keep all the fixed size static data in the program. The dynamically allocated data (ie. the data created using malloc in C) as well as the static data without a fixed size (such as arrays of variable size) are created and kept in the heap. The heap grows from low to high addresses. When you call malloc in C to create a dynamically allocated structure, the program tries to find an empty place in the heap with sufficient space to insert the new data; if it can't do that, it puts the data at the end of the heap and increases the heap size.

    The focus of this section is the stack in the memory layout. It is called the run-time stack. The stack, in contrast to the heap, grows in the opposite direction (upside-down): from high to low addresses, which is a bit counterintuitive. The stack is not only used to push the return address when a function is called, but it is also used for allocating some of the local variables of a function during the function call, as well as for some bookkeeping.

    Lets consider the lifetime of a function call. When you call a function you not only want to access its parameters, but you may also want to access the variables local to the function. Worse, in a nested scoped system where nested function definitions are allowed, you may want to access the local variables of an enclosing function. In addition, when a function calls another function, we must forget about the variables of the caller function and work with the variables of the callee function and when we return from the callee, we want to switch back to the caller variables. That is, function calls behave in a stack-like manner. Consider for example the following program:

    procedure P ( c: integer )

    x: integer;

    procedure Q ( a, b: integer )

    i, j: integer; begin x := x+a+j; end; begin Q(x,c); end;

    At run-time, P will execute the statement x := x+a+j in Q. The variable a in this statement comes as a parameter to Q, while j is a local variable in Q and x is a local variable to P. How do we organize the runtime layout so that we will be able to access all these variables at run-time? The answer is to use the run-time stack.

    When we call a function f, we push a new activation record (also called a frame) on the run-time stack, which is particular to the function f. Each frame can occupy many consecutive bytes in the stack and may not be of a fixed size. When the callee function returns to the caller, the activation record of the callee is popped out. For example, if the main program calls function P, P calls E, and E calls P, then at the time of the second call to P, there will be 4 frames in the stack in the following order: main, P, E, P

    Note that a callee should not make any assumptions about who is the caller because there may be many different functions that call the same function. The frame layout in the stack should reflect this. When we execute a function, its frame is located on top of the stack. The function does not have any knowledge about what the previous frames contain. There two things that we need to do though when executing a function: the first is that we should be able to pop-out the current frame of the callee and restore the caller frame. This can be done with the help of a pointer in the current frame, called the dynamic link, that points to the previous frame (the caller's frame). Thus all frames are linked together in the stack using dynamic links. This is called the dynamic chain. When we pop the callee from the stack, we simply set the stack pointer to be the value of the dynamic link of the current frame. The second thing that we need to do, is if we allow nested functions, we need to be able to access the variables stored in previous activation records in the stack. This is done with the help of the static link. Frames linked together with static links form a static chain. The static link of a function f points to the latest frame in the stack of the function that statically contains f. If f is not lexically contained in any other function, its static link is null. For example, in the previous program, if P called Q then the static link of Q will point to the latest frame of P in the stack (which happened to be the previous frame in our example). Note that we may have multiple frames of P in the stack; Q will point to the latest. Also notice that there is no way to call Q if there is no P frame in the stack, since Q is hidden outside P in the program.

    A typical organization of a frame is the following:

    स्रोत : lambda.uta.edu

    Do you want to see answer or more ?
    Mohammed 1 month ago
    4

    Guys, does anyone know the answer?

    Click For Answer