Java Reference
In-Depth Information
5.2.4 Field Declarations
Because the analysis phase has moved initializations, codegen() for JFieldDeclaration
need only generate code for the field declaration itself:
publicvoidcodegen(CLEmitteroutput){
for(JVariableDeclaratordecl:decls){
//Addfieldtoclass
output.addField(mods,decl.name(),decl.type()
.toDescriptor(),false);
}
}
5.3 Generating Code for Control and Logical Expressions
5.3.1 Branching on Condition
Almost all control statements in j-- are controlled by some Boolean expression. Indeed,
control and Boolean expressions are intertwined in j--. For example, consider the if-then-
else statement below:
if(a>b){
c=a;
}else{
c=b;
}
The code produced for this is as follows:
0:iload_1
1:iload_2
2:if_icmple10
5:iload_1
6:istore_3
7:goto 12
10:iload_2
11:istore_3
12:...
Notice a couple of things about this code.
1. Rather than compute a Boolean value ( true or false ) onto the stack depending on
the condition and then branching on the value of that, the code branches directly on
the condition itself. This is faster, makes use of the underlying JVM instruction set,
and makes for more compact code.
2. A branch is made over the code for the then-part to the else-part if the condition's
complement is true , that is, if the condition is false ; in our example, an if_icmple
instruction is used.
branchtoelseLabelif<condition>isfalse
<codeforthenPart>
branchtoendLabel
elseLabel:
<codeforelsePart>
endLabel:
 
Search WWH ::




Custom Search