Unit 5
Unit 5
Environment Environment
Stack
Heap
Fig 6.2 Typical subdivision of run time memory into code and data areas
Stack is used to manage the active procedure. Managing of active procedures means when a
call occurs then execution of activation is interrupted and information about status of the
stack is saved on the stack. When the control returns from the call this suspended activation
resumed after storing the values of relevant registers.
Heap area is the area of run time
time storage in which the other information is stored. For
example memory for some data items is allocated under the program control. Memory
required for these data items is obtained from this heap area. Memory for some activation is
also allocated from heap area.
2. Activation Records (Most IMP)
Various field of activation record are as follows:
1. Temporary values: The temporary variables are needed during the evaluation of
expressions. Such variables are stored
stored in the temporary field of activation record.
2. Local variables: The local data is a data that is local to the execution procedure is stored in
this field of activation record.
Dixita Kagathara, CE Department | 170701 – Compiler Design 66
Unit 6 – Run Time Memory Management
Return value
Actual parameter
Control link
Access link
Local variables
Temporaries
execution.
3 Variables remain permanently Allocated only when program unit is
allocated. active.
4 Implemented using stacks and heaps. Implemented using data segments.
5 Pointer is needed to accessing No need of dynamically
ynamically allocated
variables. pointers.
6 Faster execution than dynamic.
d Slower execution than static.
7 More memory space
pace required. Less memory
emory space required.
Table 6.1 Difference between Static and Dynamic memory allocation
Pointer to A
Pointer to B
Pointer to C
Array A
Array B
Array C
Control ink
q(1,9)
Control link
Shallow access
Display
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
C a I c u I a t e $ s u m $ a $ b $
Fig. 6.9 Variable length name
Name n Info n
Fig. 6.10 List data structure
To retrieve the information about some name we start from beginning of array and go on
searching up to available pointer. If we reach at pointer available without finding a name
we get an error "use of undeclared name".
While inserting a new name we should ensure that it should not be already there. If it is
there another error occurs i.e. "Multiple
" defined Name".
The advantage of list organization is that it takes minimum amount of space
space.
2. Self organizing list
To retrieve the information about some name we start from beginning of array and go on
searching up to available pointer. If we reach at pointer available without finding a name
we get an error "use of undeclared name".
This symbol table implementation is using linked list. A link field is added to each record.
We search the records in the order pointed by the link of link field.
Name 1 Info 1
Name 2 Info 2
First Name 3 Info 3
Name 4 Info 4
10 20 30 40 50 60
Available
10 20 30 40 50 60
The explicit allocation consists of taking a block off the list and de-allocation
allocation consist of
putting the block back on the list.
The advantage of this technique is that there is no space overhead.
Explicit Allocation of Variable Sized Blocks
Due to frequent memory allocation and de-allocation
de allocation the heap memory becomes
fragmented. That means heap may consist of some blocks that are free and some that
are allocated.
Free Free Free
Allocated Memory
Fig. 6.13 Heap Memory
In Fig. a fragmented heap memory is shown. Suppose a list of 7 blocks gets allocated and
second, fourth and sixth block is de-allocated
de then fragmentation occurs.
Thus we get variable sized blocks that are available free. For allocating variable sized
blocks some strategies such as first fit, worst fit and best fit are used.
Sometimes all thee free blocks are collected together to form a large free block. This
ultimately avoids the problem of fragmentation.
2. Implicit Allocation
The implicit allocation is performed using user program and runtime packages.
The run time package is required to know when the storage block is not in useuse.
The format of storage block is as shown in Fig 6.14.
Block size
Reference count
Mark
Pointers to block
User Data
Reference count:
Reference count is a special counter used during implicit memory allocation. If any block is
Dixita Kagathara, CE Department | 170701 – Compiler Design 77
Unit 6 – Run Time Memory Management
referred by some another block then its reference count incremented by one. That also
means if the reference count of particular block drops down to 0 then, that means that
block is not referenced one and hence it can be de-allocated.
allocated. Reference counts are best
used when pointers between blocks never appear in cycle.
Marking techniques:
This is an alternative approach to determine whether the block is in use or not. In this
method, the user program is suspended temporarily and frozen pointers are used to mark
the blocks that are in use. Sometime bitmaps are used. These pointers are then placed in
the heap memory. Again we go through hear memory and mark mark those blocks which are
unused.
There is one more technique called compaction in which all the used blocks are moved at
the one end of heap memory, so that all the free blocks are available in one large free
block.
Total cost=6
4. Basic Blocks.
A basic block is a sequence of consecutive statements in which flow of control enters at
the beginning and leaves at the end without halt or possibility of branching except at the
end.
The following sequence of three-address
three address statements forms a basic block:
t1 := a*a
t2 := a*b
t3 := 2*t2
t4 := t1+t3
t5 := b*b
6. Next-Use
Use information.
The next-use
use information is a collection of all the names that are useful for next
subsequent statement in a block. The use of a name is defined as follows,
Consider a statement,
x := i
j := x op y
That means the statement j uses value of x.
The next-use
use information can be collected by making the backward scan of the
programming code in that specific block.
Dixita Kagathara, CE Department | 170701 – Compiler Design 93
Unit 8 – Code Generation
Storage for Temporary Names
For the distinct names each time a temporary is needed. And each time a space gets
allocated for each temporary. To have optimization in the process of code generation
we pack two temporaries into the same location if they are not live simultaneously
simultaneously.
Consider three address code as,
t1 := a * a t1 := a * a
t2 := a * b t2 := a * b
t3 := 4 * t2 t2 := 4 * t2
t4 := t1+t3 t1 := t1+t2
t5 := b * b t2 := b * b
:
t6 = t4+t5 t1 := t1+t2
Loop L2 L1-L2
+
<=
[]
sum
* + 10
a
4 i 1
Fig 8.4 DAG for block B2
Algorithm for Construction of DAG
We assume the three address statement could of following types,
types
Case (i) x:=y op z
Case (ii)x:=op y
Case (iii) x:=y
Fig. 8.5.
8. Methods to generate code from DAG
1. Rearranging Order
The order of three address code affects the cost of the object code being generated. In
the senses that by changing the order in which computations are done we can obtain
the object code with minimum cost.
Consider the following code,
t1:=a+b
t2:=c+d
t3:=e-t2
t4:=t1-t3
The code can be generated by translating the three address code line by line.
MOV a, R0
ADD b, R0
MOV c, R1
ADD d, R1
MOV R0, t1
MOV e, R0
Dixita Kagathara, CE Department | 170701 – Compiler Design 97
Unit 8 – Code Generation
SUB R1, R0
MOV t1, R1
SUB R0, R1
MOV R1, t4
Now if we change the sequence of the above three address code.
t2:=c+d
t3:=e-t2
t1:=a+b
t4:=t1-t3
Then we can get improved code as
MOV c, R0
ADD d, R0
MOV e, R1
SUB R0, R1
MOV a, R0
ADD b, R0
SUB R1, R0
MOV R0, t4
2. Heuristic ordering
The heuristic ordering algorithm is as follows:
follows
1. Obtain all the interior nodes. Consider these interior nodes as unlisted nodes
nodes.
2. while( unlisted interior nodes remain)
3. {
4. pick up an unlisted node n, whose parents have been listed
5. list n;
6. while(the leftmost child m of n has no unlisted parent AND is not leaf
7. { 1
8. List m; *
2
9. n=m; 3
+
10. } 4
-
11. } *
5
8
+
/
6 7 11 12
- c d e
9 10
a
b
Fig 8.6 A DAG
1 2
t1 t3
a b e t2 1
1 0 1
c d
1 0
Fig 8.6 Labeled tree