blob: 9095a527be1840e4f67a70923c6c3ac85f80e8fe [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1994-2003 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.tools.tree;
27
28import sun.tools.java.*;
29import sun.tools.asm.Assembler;
30import sun.tools.asm.Label;
31import 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 */
38public
39class AndExpression extends BinaryLogicalExpression {
40 /**
41 * constructor
42 */
43 public AndExpression(long where, Expression left, Expression right) {
44 super(AND, where, left, right);
45 }
46
47 /*
48 * Check an "and" expression.
49 *
50 * cvars is modified so that
51 * cvar.vsTrue indicates variables with a known value if
52 * both the left and right hand side are true
53 * cvars.vsFalse indicates variables with a known value
54 * either the left or right hand side is false
55 */
56 public void checkCondition(Environment env, Context ctx, Vset vset,
57 Hashtable 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 true
65 right.checkCondition(env, ctx, vsTrue, exp, cvars);
66 right = convert(env, ctx, Type.tBoolean, right);
67
68 // cvars.vsTrue already reports when both returned true
69 // cvars.vsFalse must be set to either the left or right side
70 // returning false
71 cvars.vsFalse = cvars.vsFalse.join(vsFalse);
72 }
73
74 /**
75 * Evaluate
76 */
77 Expression eval(boolean a, boolean b) {
78 return new BooleanExpression(where, a && b);
79 }
80
81 /**
82 * Simplify
83 */
84 Expression simplify() {
85 if (left.equals(true)) {
86 return right;
87 }
88 if (right.equals(false)) {
89 // Preserve effects of left argument.
90 return new CommaExpression(where, left, right).simplify();
91 }
92 if (right.equals(true)) {
93 return left;
94 }
95 if (left.equals(false)) {
96 return left;
97 }
98 return this;
99 }
100
101 /**
102 * Code
103 */
104 void codeBranch(Environment env, Context ctx, Assembler asm, Label lbl, boolean whenTrue) {
105 if (whenTrue) {
106 Label lbl2 = new Label();
107 left.codeBranch(env, ctx, asm, lbl2, false);
108 right.codeBranch(env, ctx, asm, lbl, true);
109 asm.add(lbl2);
110 } else {
111 left.codeBranch(env, ctx, asm, lbl, false);
112 right.codeBranch(env, ctx, asm, lbl, false);
113 }
114 }
115}