Java Reference
In-Depth Information
a[3]
o.f
C.sf
x
have both l-values and r-values. That is, any of these may appear on the left-hand side or on
the right-hand side of an assignment operator. On the other hand, while all of the following
have r-values, none has an l-value:
5
x+5
Factorial.factorial(5)
None of these may meaningfully appear on the left-hand side of an assignment statement.
In compiling an assignment, compiling the right-hand side expression to produce code
for computing its r-value and leaving it on the stack is straightforward. But what code must
be generated for the left-hand side? In some cases, no code need be generated; for example,
assuming x and y are local integer variables, then compiling
x=y;
produces
iloady'
istorex'
where x' and y' are the stack offsets for x and y , respectively. On the other hand, compiling
a[x]=y;
produces
aloada'
iloadx'
iloady'
iastore
This code loads (a reference to) the array a and an index x onto the stack. It then loads the
r-value for y onto the stack. The iastore (integer array store) instruction pops the value
for y , the index x , and the array a from the stack, and it stores y in the element indexed
by x in array a .
Assignment Expressions versus Assignment Statements
Another issue is that an assignment may act as an expression producing a result (on the
stack) or as a statement (syntactically, as a statement expression). That is, we can have
x=y;
or
z=x=y;
In the first case, the assignment x=y; is a statement; no value is left on the stack. But
in the second case, the x=y must assign the value of y to x but also leave a value (the
r-value for y ) on the stack so that it may be popped off and assigned to z . That is, the code
might look something like
iloady'
dup
istorex'
istorez'
 
Search WWH ::




Custom Search