Java Reference
In-Depth Information
Notice that our method prevents unnecessary branches to branches. For example, consider
the slightly more complicated condition in
if(a>b&&b>c&&c>5){
c=a;
}
else{
c=b;
}
The JVM code produced for this targets the same exit on false , for each of the && opera-
tions:
0: iload_1
1: iload_2
2: if_icmple 18
5: iload_2
6: iload_3
7: if_icmple 18
10: iload_3
11: iconst_5
12: if_icmple 18
15: iinc 1,-1
18: ...
Branches to branches are avoided even when generating code for a deeply nested and com-
plex Boolean expression because we pass the target through nested recursive calls to this
special version of codegen() .
The implementation of a || operator for a short-circuited logical or operation (not yet
in the j-- language) is left as an exercise.
5.3.3 Logical Not !
The code generation for the JLogicalNot operator ! is trivial: a branch on true becomes
a branch on false , and a branch on false becomes a branch on true :
publicvoidcodegen(CLEmitteroutput,StringtargetLabel,
booleanonTrue){
arg.codegen(output,targetLabel,!onTrue);
}
5.4 Generating Code for Message Expressions, Field Selection, and
Array Access Expressions
5.4.1 Message Expressions
Message expressions are what distinguish object-oriented programming languages from the
more traditional programming languages such as C. j--, like Java, is object-oriented.
Much of the work in decoding a message expression is done in analysis. By the time
the compiler has entered the code generation phase, any ambiguous targets have been
reclassified, instance messages have been distinguished from class (static) messages, implicit
targets have been made explicit, and the message's signature has been determined.
In fact, by examining the target's type (and if necessary, its sub-types) analysis can
 
Search WWH ::




Custom Search