KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > expr > ConditionalTarget


1 package gnu.expr;
2
3 import gnu.bytecode.*;
4 /** This is the Target of a boolean expression, in a conditional context.
5   * If the expression evaluates to, transfer to the ifTrue label;
6   * if false, tranfer to the ifFalse label. */

7
8 public class ConditionalTarget extends Target
9 {
10   public Label ifTrue, ifFalse;
11   Language language;
12
13   /**
14     * @param ifTrue label to jump to if this evaluates to true
15     * @param ifFalse label to jump to if true
16     * @param language specifies what values are true
17     */

18
19   public ConditionalTarget (Label ifTrue, Label ifFalse,
20                 Language language)
21   {
22     this.ifTrue = ifTrue;
23     this.ifFalse = ifFalse;
24     this.language = language;
25   }
26
27   /** True if the ifTrue label comes before the ifFalse label.
28     * This is used in the hope we can optimize away a branch followed by
29     * its target. */

30   public boolean trueBranchComesFirst = true;
31
32   public Type getType() { return Type.boolean_type; }
33
34   public void compileFromStack(Compilation comp, Type stackType)
35   {
36     CodeAttr code = comp.getCode();
37     char sig = stackType.getSignature().charAt(0);
38     switch (sig)
39       {
40       case 'J':
41     code.emitPushLong(0);
42     break;
43       case 'D':
44     code.emitPushDouble(0.0);
45     break;
46       case 'F':
47     code.emitPushFloat(0.0f);
48     break;
49       default:
50     if (trueBranchComesFirst)
51       {
52         code.emitGotoIfIntEqZero(ifFalse);
53         code.emitGoto(ifTrue);
54       }
55     else
56       {
57         code.emitGotoIfIntNeZero(ifTrue);
58         code.emitGoto(ifFalse);
59       }
60     return;
61       case 'L': case '[':
62     comp.compileConstant(language == null ? Boolean.FALSE
63                  : language.booleanObject(false));
64     break;
65       }
66     if (trueBranchComesFirst)
67       code.emitGotoIfEq(ifFalse);
68     else
69       code.emitGotoIfNE(ifTrue);
70     emitGotoFirstBranch(code); // Usually a no-op.
71
}
72
73   /** Goto whichever of IfTrue or ifFalse is specified by trueBranchComesFirst.
74    * Normally, the goto should get optimized away as a no-op. */

75   public final void emitGotoFirstBranch(CodeAttr code)
76   {
77     code.emitGoto(trueBranchComesFirst ? ifTrue : ifFalse);
78   }
79 }
80
Popular Tags