0% found this document useful (0 votes)
26 views

CD 5

Uploaded by

vijaykonada5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

CD 5

Uploaded by

vijaykonada5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Runtime Environment:

A translation needs to relate the static source text of a program to


the dynamic actions that must occur at runtime to implement the
program. The program consists of names for procedures,
identifiers, etc., that require mapping with the actual memory
location at runtime. 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.
Activation tree:
• The main aim of runtime systems is to map procedures to their
activations.
• Each execution of the procedure is called an activation .
• A procedure Definition associates an identifier with a statement or a
block of statements.
• The identifier to the procedure is called the procedure name
• An activation tree is used to determine the way the control flows
between procedures.
• During program Execution ,the control flow is sequential among
procedures.
• In a procedure ,the execution begins at the starting point of its body .
• Activation tree for the procedure call fib(4) as fib(4)

fib(3) fib(2)

fib(2) fib(1)
Storage organization: It deals with the division of memory into
different areas ,management of the activation records and layout of
the local data.
a.Sub division of run-time memory: The underlying hardware and
operating system provide a division of memory into the following
areas. Heap area
Stack area
Static area
Code area

Code area: This contains the generated code


Static area: This contains data whose absolute address can be determined
at compile time.In C the global and static variables are kept in this area.
Stack area: The local variables and parameters of all ‘C’ procedures would
reside in this area.
Heap area: This is data entered at run time.
b.Activation Records:
• An activation record is a conceptual aggregate of data which contains
all information required for a single activation of a procedure.
• Typically space must be provided for the following components in an
activation record as
Return value
Actual parameters
Control link(optional)
Access link(optional)
Saved machine status
Local data
temporaries

format of activation record


Storage Allocation: Different types of storage allocation schemes are
followed.They are 1.Static Allocation 2.Stack Allocation 3.Heap allocation
1.Static storage allocation
• In static allocation, names are bound to storage locations.
• If memory is created at compile time then the memory will be created in static
area and only once.
• Static allocation supports the dynamic data structure that means memory is
created only at compile time and deallocated after program completion.
• The drawback with static storage allocation is that the size and position of data
objects should be known at compile time.
• Another drawback is restriction of the recursion procedure.
2.Stack Storage Allocation
• In static storage allocation, storage is organized as a stack.
• An activation record is pushed into the stack when activation begins and it is
popped when the activation end.
• It works on the basis of last-in-first-out (LIFO) and this allocation supports the
recursion process.
3.Heap Storage Allocation
• Heap allocation is the most flexible allocation scheme.
• Allocation and deallocation of memory can be done at any time and
at any place depending upon the user's requirement.
• Heap allocation is used to allocate memory to the variables
dynamically and when the variables are no more used then claim it
back.
• Heap storage allocation supports the recursion process.
Example:
fact (int n)
{
if (n<=1)
return 1;
else
return (n * fact(n-1));
}
Recursion:
Recursion is the process of repeating items in a self-similar
way. In programming languages, if a program allows you to call a
function inside the same function, then it is called a recursive call of
the function.
void recursion()
{
recursion(); /* function calls itself */
}
int main()
{
recursion();
}
#include <stdio.h>
int factorial( int i)
{
if(i <= 1)
{
return 1;
}
return i * factorial(i - 1);
}
int main()
{
int i = 6;
printf("Factorial of %d is %d\n", i, factorial(i));
return 0;
}
Procedures call
• Procedure is an important and frequently used programming construct for a
compiler. It is used to generate good code for procedure calls and returns.
• Calling sequence:
• The translation for a call includes a sequence of actions taken on entry and exit
from each procedure.

Following actions take place in a calling sequence:

• When a procedure call occurs then space is allocated for activation record.
• Evaluate the argument of the called procedure.
• Establish the environment pointers to enable the called procedure to access data in
enclosing blocks.
• Save the state of the calling procedure so that it can resume execution after the call.
• Also save the return address. It is the address of the location to which the called
routine must transfer after it is finished.
• Finally generate a jump to the beginning of the code for the called procedure.
• Let us consider a grammar for a simple procedure call statement
• S → call id(Elist)
• Elist → Elist, E
• Elist → E

A suitable transition scheme for procedure call would be:

Production Rule Semantic Action


S → call id(Elist) for each item p on QUEUE do
GEN(param p)
GEN (call id.PLACE)
Elist → Elist, E append E.PLACE to the end of QUEUE
Elist → E initialize QUEUE to contain only
E.PLACE
Code generation:
• Code generation can be considered as the final phase of
compilation.
• The code generated by the compiler is an object code of some
lower-level programming language, for example, assembly
language.
• We have seen that the source code written in a higher-level
language is transformed into a lower-level language that results in a
lower-level object code, which should have the following minimum
properties:
It should carry the exact meaning of the source code.
It should be efficient interms of CPU usage and memory
management.
• We will now see how the intermediate code is transformed into
target object code
Ex: add r1,r2;
A code generator is expected to have an understanding of the target
machine’s runtime environment and its instruction set. The code
generator should take the following things into consideration to
generate the code:
• Target language : The code generator has to be aware of the nature of
the target language for which the code is to be transformed. That
language may facilitate some machine-specific instructions to help the
compiler generate the code in a more convenient way. Instructions like
this
1.Load –This operation loads the value of one location to other
location.Example: LD r1,X loads the value present in location X to
register r1 . or LD r1,r2;
2.Store-This operation stores the content of one register to some
location .Ex:store a,r2;
3.Computation –This operation perform various operations like
ADD,SUB,MUL,DIV etc.EX:ADD r1,r2;
4.Unconditional Jumps: This instruction is of type, BR L
here BR stands for Branch and L stands for Label.
• IR Type : Intermediate representation has various forms. It can be in
Abstract Syntax Tree (AST) structure, Polish Notation, or 3-address
code.
• Selection of instruction : The code generator takes Intermediate
Representation as input and converts (maps) it into target machine’s
instruction set. One representation can have many ways (instructions)
to convert it, so it becomes the responsibility of the code generator to
choose the appropriate instructions wisely.
• Register allocation : A program has a number of values to be
maintained during the execution. The target machine’s architecture
may not allow all of the values to be kept in the CPU memory or
registers. Code generator decides what values to keep in the registers.
Also, it decides the registers to be used to keep these values.
• Ordering of instructions : At last, the code generator decides the order
in which the instruction will be executed. It creates schedules for
instructions to execute them.

You might also like