1 /*
2  * Copyright (c) 1994, 2003, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package sun.tools.tree;
27 
28 import sun.tools.java.*;
29 import sun.tools.asm.Assembler;
30 import sun.tools.asm.Label;
31 import java.util.Hashtable;
32 
33 /**
34  * WARNING: The contents of this source file are not part of any
35  * supported API.  Code that depends on them does so at its own risk:
36  * they are subject to change or removal without notice.
37  */
38 public
39 class OrExpression extends BinaryLogicalExpression {
40     /**
41      * constructor
42      */
OrExpression(long where, Expression left, Expression right)43     public OrExpression(long where, Expression left, Expression right) {
44         super(OR, where, left, right);
45     }
46 
47     /*
48      * Check an "or" expression.
49      *
50      * cvars is modified so that
51      *    cvar.vsTrue indicates variables with a known value if
52      *        either the left and right hand side isn true
53      *    cvars.vsFalse indicates variables with a known value if
54      *        both the left or right hand side are false
55      */
checkCondition(Environment env, Context ctx, Vset vset, Hashtable<Object, Object> exp, ConditionVars cvars)56     public void checkCondition(Environment env, Context ctx, Vset vset,
57                                Hashtable<Object, Object> exp, ConditionVars cvars) {
58         // Find out when the left side is true/false
59         left.checkCondition(env, ctx, vset, exp, cvars);
60         left = convert(env, ctx, Type.tBoolean, left);
61         Vset vsTrue = cvars.vsTrue.copy();
62         Vset vsFalse = cvars.vsFalse.copy();
63 
64         // Only look at the right side if the left side is false
65         right.checkCondition(env, ctx, vsFalse, exp, cvars);
66         right = convert(env, ctx, Type.tBoolean, right);
67 
68         // cvars.vsFalse actually reports that both returned false
69         // cvars.vsTrue must be set back to either left side or the right
70         //     side returning false;
71         cvars.vsTrue = cvars.vsTrue.join(vsTrue);
72     }
73 
74     /**
75      * Evaluate
76      */
eval(boolean a, boolean b)77     Expression eval(boolean a, boolean b) {
78         return new BooleanExpression(where, a || b);
79     }
80 
81     /**
82      * Simplify
83      */
simplify()84     Expression simplify() {
85         if (right.equals(false)) {
86             return left;
87         }
88         if (left.equals(true)) {
89             return left;
90         }
91         if (left.equals(false)) {
92             return right;
93         }
94         if (right.equals(true)) {
95             // Preserve effects of left argument.
96             return new CommaExpression(where, left, right).simplify();
97         }
98         return this;
99     }
100 
101     /**
102      * Code
103      */
codeBranch(Environment env, Context ctx, Assembler asm, Label lbl, boolean whenTrue)104     void codeBranch(Environment env, Context ctx, Assembler asm, Label lbl, boolean whenTrue) {
105         if (whenTrue) {
106             left.codeBranch(env, ctx, asm, lbl, true);
107             right.codeBranch(env, ctx, asm, lbl, true);
108         } else {
109             Label lbl2 = new Label();
110             left.codeBranch(env, ctx, asm, lbl2, true);
111             right.codeBranch(env, ctx, asm, lbl, false);
112             asm.add(lbl2);
113         }
114     }
115 }
116