Variables
Definition
A variable is an abstraction of a storage location that can hold a value
Variable Instances
Example
How many variables are in this program?
def fact(x: Int): Int = {
if (x < 2) {
1
} else {
x * fact(x - 1)
}
}
x
is a variable, but at runtime, each recursion creates an instance of x
, and each of these instances needs a different storage location.
Extent
Definition
The extent of a variable instance is the time interval when the instance can be accessed
Type of Variable | Extent | Implementation |
---|---|---|
Global | Entire execution | Constant address (label) |
Procedure/local | Execution of the procedure (LIFO) | Stack |
Field of object | From creation time of object to last use | Heap |
Local Variables
Info
The compiler keeps a symbol table that maps each variable to its offset from the frame pointer. The symbol table exists at compile time only.