12 2 Imperative Programming Languages
the input program is type-correct and that the type of each variable, each function
and each subexpression is available.
Program variables are allocated in the main memory S where their values are
stored in memorylocations. The generated code uses the addresses of these locations
to load the current values of the variables or to save new values (see Fig. 2.7).
z:
y:
x:
Fig. 2.7. The implementation of variables
Therefore, the code-functions require a function
ρ
which assigns to each variable
x its address in main memory. The function
ρ
is called address environment.As
we will see later, the address of a variable is in fact a relative address, that is, a
constant difference between two absolute addresses in S, namely the address of the
location of this variable and the initial address of a memory area, which we will later
call a frame or stack frame. Such a frame will contain space for the instances of all
variables, parameters, etc., of a function. For the moment, we may assume that
ρ
(x)
is the address of x relative to the beginning of S.
Inimperativelanguages,variables canbeusedintwoways.Considerforexample
the assignment x
← y + 1. For variable y, it is the valuethat is required to determine
the value of the expression y
+ 1. The value of variable x, on the other hand, is not
important. For variable x, the address of the memory location is required that holds
thevalueof x. The newlycomputedvalueneeds to be storedin this memorylocation.
We conclude that, when translating assignments, a variable identifier that occurs on
theleftsideoftheassignment has tobecompileddifferentlyfrom avariableidentifier
that occurs on the right side. From the variable identifier on the left, the address
of its memory location is required. This value is called left value (L-value) of the
identifier. From the variable identifier that occurs on the right, its value is required,
more precisely, the contents of the memory cell associated with the identifier. This
value is called the right value (R-value) of the identifier.Our code functions therefore
may be subscripted with L or R. The function code
L
generates code for computing
the L-value while the function code
R
generates code for computing the R-value.
The function code (without subscript) translates statements, statement sequences,
function definitions or whole programs. We may note already here that, while every
expression has an R-value, not every expression has an L-value. A simple example
is the expression y
+ 1. The value of this expression only temporarily occurs on top
of the stack and therefore may not be accessed via a fixed address.