Java Reference
In-Depth Information
To translate an array reference used as a left-hand side, we first visit the array
name, Marker 62 .
In Java this will push a reference to an array object.
In
languages like C and C
an array address will be loaded into a register.
Next, at Marker 63 , the array index is computed. The JVM will push its
value onto the stack; other architectures will load or compute the index into a
register.
Finally, at Marker 64 , we prepare a store instruction that will be issued
once the value to be stored is determined. On the JVM this is simple — special
array store instructions use the array reference, index value and right-hand
side value found at the TOS to store the right-hand side value into the proper
array element. Array bounds checking is included too.
For other architectures, several instructions may need to be generated to
compute the address of the selected array element. Again, we use the formulas
detailed in Section 12.3.1 on page 460. The selected element address is placed
in a register. If array bounds checking is activated, the index value is also
compared againt the array's lower and upper bounds. Then a suitable store
instruction is saved, and later generated (when the value to be stored into the
array element is known).
++
AST
JVM Instructions
;
AST and code for the
AssignIsh
;
assignment:
=
;
;
ar[i] = 250
;
;
Code generated for the array, ar
aload 3
ConstantIsh
ArrayReferencing
250
;
Code generated for the index, i
aload 4
;
Code generated for 250
sipush 250
LocalReferencing
i
LocalReferencing
;
Code to store into the array element
iastore
ar
In the above example, assume array ar is local variable 3 and that index i is
local 4. Since this is an assignment, code generastion begins with the Assignish
visitor defined at Marker 31 .TheLHSVistorfor ArrayReferencingis activated.
It pushes first a reference to the array ar and then the value of the index, i.It
then stores away, for later generation, an array store instruction, iastore.
Control returns to the Assignish visitor, which visits the assignment's
right-hand side. This pushes the constant 250 onto the stack. At this point, a
rererence to ar,thevalueofi and 250 are on the stack. Now the array store
instruction we earlier saved is generated (Marker 34 ). The top three stack
values are popped, and the selected array element is updated.
 
Search WWH ::




Custom Search