Professional Documents
Culture Documents
Compiler Design L1 PDF
Compiler Design L1 PDF
Lecture-1
Run-Time Environments
• The abstractions embodied in the source language definition are -
names, scopes, bindings, data types, operators, procedures,
parameters, and flow-of-control constructs.
2
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
3
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
Storage Organization
4
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
Storage Organization
• From the perspective of the compiler
writer, the executing target program runs
in its own logical address space in which
each program value has a location.
5
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
Storage Organization
We assume-
The run-time storage comes in blocks of contiguous bytes, where a
byte is the smallest unit of addressable memory.
6
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
Storage Organization
Code area
-Target code is fixed at compile time
To maximize the utilization of space at run time, the other two areas,
Stack and Heap, are at the opposite ends of the remainder of the
address space.
7
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
Storage Organization
Stack
-The stack is used to store data structures
called activation records that get generated
during procedure calls.
Heap
- The heap grows towards higher.
(We shall assume that the stack grows towards higher addresses so that
we can use positive offsets for notational convenience in all our
examples.)
8
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
Code Area
Stack Area
Heap Area
9
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
1. Static Allocation
The size of data object is known at compile time. The names of these
objects are bound to storage at compile time only.
Compiler can fill the addresses at which the target code can find the
data it operates on.
10
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
1. Static Allocation
Can be done if the size of the data object known at compile time.
11
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
2. Stack Allocation
On activation the activation records are pushed into the stack and on
completion of activation the corresponding record can be popped.
The locals are stored in the each activation record. Hence locals are
bound to corresponding activation record.
12
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
3. Heap Allocation
If the values of non local variables must be retained even after the
activation record then such a retaining is not possible by stack
allocation. This limitation of stack allocation is because of its LIFO
nature. For retaining of such local variables heap allocation strategy is
used.
•Allocate the most suitable block of memory from the linked list i.e.
Use best fit technique for allocation of bock.
13
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
15
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
2. Heap storage. Data that may outlive the call to the procedure that
created it is usually allocated on a "heap" of reusable storage. The heap
is an area of virtual memory that allows objects or other data elements
to obtain storage when they are created and to return that storage
when they are invalidated.
16
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
17
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
Activation Trees
Stack allocation would not be feasible if procedure calls, or activations
of procedures, did not nest in time.
If an activation of procedure p calls procedure q, then that activation of
q must end before the activation of p can end. There are three common
cases:
Example: Consider the program that reads nine integers into an array a
and sorts them using the recursive quicksort algorithm.
int a[11];
void readArrayO { /* Reads 9 integers into o[l], ...,o[9]. */
int i;
}
int partition(int m, int n) {
/* Picks a separator value v, and partitions a[m ..n] so that a[m ..p — 1] are less than v, a\p] = v,
and a[p + 1.. n] are equal to or greater than v. Returns p. */
}
void quicksort(int m, int n) {
int i;
if(n > m) {
i = partition( m , n );
quicksort(m, i - 1 );
quicksort( i + 1 , n );
}
}
mainQ {
readArrayO ;
a[0] = -9999;
a [10] = 9999;
quicksort( 1 , 9 ) ;
}
19
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
enter main()
enter readArray()
leave readArray()
enter quicksort( 1 , 9)
enter partition( 1 , 9)
leave partition( 1 , 9)
enter quicksort( 1 , 3)
leave quicksort( 1 , 3)
enter quicksort( 5 , 9)
leave quicksort( 5 , 9)
leave quicksort( 1 , 9)
leave main()
20
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
21
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
22
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
Activation Records
23
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
Activation Records
24
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
Activation Records
25
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
Example: By taking example of factorial program explain how activation record will
look like for every recursive call in case of factirial (3).
Solution:
main(){
int f;
f = factorial(3);
}
26
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
Example: By taking example of factorial program explain how activation record will
look like for every recursive call in case of factorial (3).
Step 1:
27
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
Example: By taking example of factorial program explain how activation record will
look like for every recursive call in case of factorial (3).
Step 2:
Act. Record for
main()
28
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
Example: By taking example of factorial program explain how activation record will
look like for every recursive call in case of factorial (3).
Step 3:
Act. Record for
main ()
29
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
Parameter Passing
There are two types of parameters-
i) Formal Parameter
ii) Actual Parameter
30
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
1. Call by value:
• The actual parameters are evaluated and their r-value are passed to
called procedure.
31
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
32
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
• If the actual parameter has L-value then at return the value of formal
parameter is copied to actual parameter.
33
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
4. Call by name:
34
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
Symbol Tables
35
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
Symbol Tables
Symbol-Table Entries
1. Fixed-length name
• A fixed space for each name is allocated in symbol table.
• If name is too small then there is wastage of space.
37
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
2. Variable-length name
• Rather than allocating in each symbol-table entry the maximum
possible amount of space to hold a lexeme, we can utilize space
more efficiently if there is only one pointer space in a symbol-table
entry.
• In the record for name, we place a pointer to separate array of
characters (the string table) giving position of the first charecter of
the lexeme.
38
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
39
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
41
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
3. Hash tables
Hashing is an important technique used to search records of symbol
table. This method is superior to list organization.
A hash table consisting of a fixed array of m pointers to table
entries.
Table entries organized into m separate linked lists, called buckets.
Each record in symbol table appears on exactly one of these lists.
The dynamic storage allocation facilities of the implementation
language can be used to obtain space for the records, often at some
loss of efficiency.
A hash table
of size 211
42
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata
Compiler Design
43
Arup Kr. Chattopadhyay, Department of IT, IEM, Kolkata