cs3012: formal languages and compilers the runtime environment after the analysis phases are...
TRANSCRIPT
![Page 1: CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The](https://reader035.vdocuments.us/reader035/viewer/2022071716/56649e555503460f94b4c75d/html5/thumbnails/1.jpg)
CS3012: Formal Languages and Compilers
The Runtime Environment
After the analysis phases are complete, thecompiler must generate executable code.
The generated code must include routines formanaging the changing data as the programexecutes.
Depending on the language, the runtimeenvironment may be:
fully static
stack-based
dynamic
The type of environment determines whether weneed to use a stack, a heap, or both.
![Page 2: CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The](https://reader035.vdocuments.us/reader035/viewer/2022071716/56649e555503460f94b4c75d/html5/thumbnails/2.jpg)
CS3012: Formal Languages and Compilers
Runtime Storage Structure
code for function 1
code for function 2
code for function n
. . .
global / static area
stack
heap
free space
entry address
entry address
entry address
![Page 3: CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The](https://reader035.vdocuments.us/reader035/viewer/2022071716/56649e555503460f94b4c75d/html5/thumbnails/3.jpg)
CS3012: Formal Languages and Compilers
Procedure Activation Record
activation record : functionX par1 (type) : <value>...parN (type): <value>start code ptr: <ptr>current ptr: <ptr>return address: <ptr>var1 (type) : <value>...varN (type): <value>
Each time the flow of control enters a functionor procedure, we update its procedure activationrecord.
This maintains the values of the function argumentsand all local variables defined inside the function,and pointers to the start of the code segment, thecurrent location in the code segment, and thesegment of code to which we return on exit.
![Page 4: CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The](https://reader035.vdocuments.us/reader035/viewer/2022071716/56649e555503460f94b4c75d/html5/thumbnails/4.jpg)
CS3012: Formal Languages and Compilers
Fully Static Environments
A fully static environment (FORTRAN77) has
no recursively-called procedures
no pointers
no dynamic memory allocation
This means that only one copy of each procedureactivation record can be active at any one time.
Therefore, we store only one copy, and we canallocate storage at compile time.
Each time we enter a procedure, we simply update the fields of its activation record.
Fully static environments do not need a stack.
![Page 5: CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The](https://reader035.vdocuments.us/reader035/viewer/2022071716/56649e555503460f94b4c75d/html5/thumbnails/5.jpg)
CS3012: Formal Languages and Compilers
Static example
activation record : f1j (int) :start code ptr:current ptr:return address:k (int) :
activation record : main start code ptr:current ptr:return address:k (int) :
global areai (int) :
1 int i = 10;
2 int f1(int j) {3 int k;4 k = 3 * j;5 if (k<i) print(i);6 else print(j);7 }
8 main() {9 int k = 1;10 while (k<5) {11 f1(k);12 k = k+1;13 }14 }
![Page 6: CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The](https://reader035.vdocuments.us/reader035/viewer/2022071716/56649e555503460f94b4c75d/html5/thumbnails/6.jpg)
CS3012: Formal Languages and Compilers
Stack-based Environment
Stack-based languages do allow procedures tobe called recursively. Typically, they also allowdynamic memory allocation and pointers todata locations.
Since procedures can be called recursively, wemay need to hold a number of different recordsfor the same function, representing eachinvocation.
Activation records will be created as requiredand placed on the stack. Each record willmaintain a pointer to the record that activated it,and on completion, the current record will bedeleted from the stack, and control passed backto the calling record.
Most high-level imperative languages arestack-based - e.g. C, Pascal.
![Page 7: CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The](https://reader035.vdocuments.us/reader035/viewer/2022071716/56649e555503460f94b4c75d/html5/thumbnails/7.jpg)
CS3012: Formal Languages and Compilers
Stack-based Example
1 int x, y;
2 int gcd(int u, int v) {3 if (v == 0) return u;4 else return gcd(v, u % v);5 }
6 main() {7 scanf("%d %d", &x, &y);8 printf("%d\n", gcd(x, y));9 }
global area x (int): 15y (int): 10
activation record: main start ptr: 6current ptr: 8
activation record: gcdu (int): 15v (int): 10start ptr: 2current ptr: 2return ptr:return address: 8
on 1st entry to gcd
fp
sp
global area x (int):y (int):
activation record: main start ptr: 6current ptr:
initial environment
fp
sp
![Page 8: CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The](https://reader035.vdocuments.us/reader035/viewer/2022071716/56649e555503460f94b4c75d/html5/thumbnails/8.jpg)
CS3012: Formal Languages and Compilers
global area x (int): 15y (int): 10
activation record: main start ptr: 6current ptr: 8
activation record: gcdu (int): 15v (int): 10start ptr: 2current ptr: 4return ptr:return address: 8
on 3rd entry to gcd
fp
sp
activation record: gcdu (int): 10v (int): 5start ptr: 2current ptr: 4return ptr:return address: 4
activation record: gcdu (int): 5v (int): 0start ptr: 2current ptr: 2return ptr:return address: 4
global area x (int): 15y (int): 10
activation record: main start ptr: 6current ptr: 9
about to exit main
fpsp
![Page 9: CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The](https://reader035.vdocuments.us/reader035/viewer/2022071716/56649e555503460f94b4c75d/html5/thumbnails/9.jpg)
CS3012: Formal Languages and Compilers
Dangling References
int *dangle() { int x = 3; return &x;}
int inc(int y) { int z; z = y + 1; return z;}
main() { int *w, t;
w = dangle(); printf("w = %d\n", *w); t = inc(10); printf("w = %d\n", *w);}
% a.outw = 3w = 11%
![Page 10: CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The](https://reader035.vdocuments.us/reader035/viewer/2022071716/56649e555503460f94b4c75d/html5/thumbnails/10.jpg)
CS3012: Formal Languages and Compilers
Dynamic Environments
Returning the address of a local variable isdefined to be a logical error in languages like C.
In a dynamic environment there is no suchrestriction - all variables and activation recordsmust be maintained for as long as there arereferences to them. It is also possible to return pointers to local functions.
Dynamic environments must deallocate spacewhen procedures and variables can no longerbe reached - garbage collection .
It is normal to use a heap to maintain suchrecords. Two functions are required:
allocate - returns the address of a memory block of the appropriate size
free - marks a block as being free for re-use
![Page 11: CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The](https://reader035.vdocuments.us/reader035/viewer/2022071716/56649e555503460f94b4c75d/html5/thumbnails/11.jpg)
CS3012: Formal Languages and Compilers
Heap Management
Stack-based languages like C also require a heapto maintain pointer allocation and dynamically allocated memory.
The two main challenges in heap management are:
stopping the heap becoming fragmented, bycombining successive freed blocks into one
ensuring that free is only ever applied to thestart of a block of the required size
allocate
free
malloc
free
new
delete
new
•
C C++ Java
![Page 12: CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The](https://reader035.vdocuments.us/reader035/viewer/2022071716/56649e555503460f94b4c75d/html5/thumbnails/12.jpg)
CS3012: Formal Languages and Compilers
a simple heap management scheme
circular linked list of allocated memory blocks
each block records its used size, the size of the freespace after it, and points to the next block
the block at the top of the heap points to a blockwith some free space after it
to allocate a block
to free a block
find a block with enough free space after it
jump to end of that block's used space
insert the new block into the heap
compute the reduced free space
set the previous block's free space to 0
update the list
move to top of the heap
step through list of blocks until required address
add the block's free size and used size to the free size of its previous block
![Page 13: CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The](https://reader035.vdocuments.us/reader035/viewer/2022071716/56649e555503460f94b4c75d/html5/thumbnails/13.jpg)
CS3012: Formal Languages and Compilers
Example allocation sequence
nextus:15 fs:0
headerlast next
nextus:10 fs:0
nextus:10 fs:20
used
used
used
free
5
20
30
40
60
0
nextus:15 fs:0
headerlast next
nextus:10 fs:0
nextus:10 fs:0
used
used
used
free
5
20
30
40
60
0
allocate a block ofsize 15
nextus:15 fs:5
used
55
![Page 14: CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The](https://reader035.vdocuments.us/reader035/viewer/2022071716/56649e555503460f94b4c75d/html5/thumbnails/14.jpg)
CS3012: Formal Languages and Compilers
Exampe sequence (cont)
nextus:15 fs:10
headerlast next
nextus:10 fs:0
used
used
free
5
20
30
40
60
0
nextus:15 fs:5
used
55
free block (size 10)at 20
free
nextus:15 fs:20
headerlast next
used
free
5
20
30
40
60
0
nextus:15 fs:5
used
55
free
free block (size 10)at 35 - fails
free block (size 10)at 30