001: /*
002: * Copyright 1994-2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.tree;
027:
028: import sun.tools.java.*;
029: import sun.tools.asm.Assembler;
030: import sun.tools.asm.Label;
031: import java.util.Hashtable;
032:
033: /**
034: * WARNING: The contents of this source file are not part of any
035: * supported API. Code that depends on them does so at its own risk:
036: * they are subject to change or removal without notice.
037: */
038: public class AndExpression extends BinaryLogicalExpression {
039: /**
040: * constructor
041: */
042: public AndExpression(long where, Expression left, Expression right) {
043: super (AND, where, left, right);
044: }
045:
046: /*
047: * Check an "and" expression.
048: *
049: * cvars is modified so that
050: * cvar.vsTrue indicates variables with a known value if
051: * both the left and right hand side are true
052: * cvars.vsFalse indicates variables with a known value
053: * either the left or right hand side is false
054: */
055: public void checkCondition(Environment env, Context ctx, Vset vset,
056: Hashtable exp, ConditionVars cvars) {
057: // Find out when the left side is true/false
058: left.checkCondition(env, ctx, vset, exp, cvars);
059: left = convert(env, ctx, Type.tBoolean, left);
060: Vset vsTrue = cvars.vsTrue.copy();
061: Vset vsFalse = cvars.vsFalse.copy();
062:
063: // Only look at the right side if the left side is true
064: right.checkCondition(env, ctx, vsTrue, exp, cvars);
065: right = convert(env, ctx, Type.tBoolean, right);
066:
067: // cvars.vsTrue already reports when both returned true
068: // cvars.vsFalse must be set to either the left or right side
069: // returning false
070: cvars.vsFalse = cvars.vsFalse.join(vsFalse);
071: }
072:
073: /**
074: * Evaluate
075: */
076: Expression eval(boolean a, boolean b) {
077: return new BooleanExpression(where, a && b);
078: }
079:
080: /**
081: * Simplify
082: */
083: Expression simplify() {
084: if (left.equals(true)) {
085: return right;
086: }
087: if (right.equals(false)) {
088: // Preserve effects of left argument.
089: return new CommaExpression(where, left, right).simplify();
090: }
091: if (right.equals(true)) {
092: return left;
093: }
094: if (left.equals(false)) {
095: return left;
096: }
097: return this ;
098: }
099:
100: /**
101: * Code
102: */
103: void codeBranch(Environment env, Context ctx, Assembler asm,
104: 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: }
|