Computer Science and Engineering
Anubhav Patrick
Assistant Professor
Fifth
Compiler Design
KCS502
Runtime Administration
A quick tutorial on Lex 1
Runtime Environment
l Run time memory organization.
char abc[1000];
char *fun1() { char buf[50], *c; buf[0] = ‘\0’; c=malloc(50);
return( c );}
main() {char *c; c = fun1();}
} We need to use memory to store:
– Code
– Static data (global variables)
– Dynamic data objects
• Data that are used when executing a certain procedure.
• Dynamically allocated objects (malloc, free).
2
Typical organization of run-time memory
Low Address
Code
Static Data (global variables)
Stack (memory for procedures)
Heap (memory for dynamically allocated data)
High Address
3
Activation Records
l Also called frames
l Information (memory) needed by a single execution of a procedure
l A general activation record:
return value
actual parameters
optional control link
optional access link
machine status
local variables
temporaries
4
Types of Allocation
l Storage Allocation Strategies
} Static Allocation lays out storage for all data objects at compile time.
– Restrictions:
• Size of object must be known and alignment requirements must be known at compile time.
• No recursion.
• No dynamic data structure
} Stack allocation manages the run time storage as a stack
– The activation record is pushed on a stack as a function is entered.
– The activation record is popped off a stack as a function exits.
– Restrictions:
• Values of locals cannot be retained when an activation ends.
• A called activation cannot outlive a caller.
5
Types of Allocation
} Heap allocation allocates and deallocates storage as needed at runtime
from a data area known as heap.
– Advantage:
• Most flexible: no longer requires the activation of procedures to be LIFO.
– Disadvantage:
• Most inefficient: need true dynamic memory management.
l Note: static allocation too restricted, heap allocation too inefficient. Most
current compiler/language/processor uses the stack allocation scheme.
6
Example of stack allocation (downward
growing):
7
Access to Non-Local Variables
l Access to nonlocal variables.
} e.g., Nonlocal variables in PASCAL (with nested procedures):
} The scheme for C will break (static for all non-locals).
} Solution -> Access links
– If p is nested immediately within q in the source text, then the access link in an
activation record for p points to the access link in the record for the most recent
activation of q.
– If a procedure p at nesting depth np accesses a nonlocal variable x which is an
element defined within some procedure q that surrounds p and has nesting depth nq:
we follow access links np – nq times
8
Access to Non-Local Variables
9
Access Links
10
Thank You
11