Java Reference
In-Depth Information
determine whether or not a viable message matching the signature of the message exists. Of
course, for instance messages, the actual message that is invoked cannot be determined until
run-time. That depends on the actual run-time object that is the target of the expression
and so is determined by the JVM. But the code generated for a message expression does
state everything that the compiler can determine.
JMessageExpression 's codegen() proceeds as follows:
1. If the message expression involves an instance message, codegen() generates code for
the target.
2. The message invocation instruction is determined: invokevirtual for instance mes-
sages and invokestatic for static messages.
3. The addMemberAccessInstruction( ) method is invoked to generate the message in-
vocation instruction; this method takes the following arguments:
(a) The instruction ( invokevirtual or invokestatic ).
(b) The JVM name for the target's type.
(c) The message name.
(d) The descriptor of the invoked method, which was determined in analysis.
4. If the message expression is being used as a statement expression and the return type
of the method referred to in (d), and so the type of the message expression itself
is non-void, then method addNoArgInstruction() is invoked for generating a pop
instruction. This is necessary because executing the message expression will produce
a result on top of the stack, and this result is to be thrown away.
For example, the code generated for the message expression
...=s.square(6);
where square is an instance method, would be
aloads'
bipush 6
invokevirtual #6;//Methodsquare:(I)I
Notice this leaves an integer result 36 on the stack. But if the message expression were used
as a statement, as in
s.square(6);
our compiler generates a pop instruction to dispose of the result 6 :
aloads'
bipush 6
invokevirtual #6;//Methodsquare:(I)I
pop
We invoke static methods using invokestatic instructions. For example, say we wanted to
invoke the static method csquare ; the message expression
..=Square1.csquare(5);
would be translated as
6 s' indicates the stack frame offset for the local variable s .
 
Search WWH ::




Custom Search