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 NotExpression extends UnaryExpression {
039: /**
040: * Constructor
041: */
042: public NotExpression(long where, Expression right) {
043: super (NOT, where, Type.tBoolean, right);
044: }
045:
046: /**
047: * Select the type of the expression
048: */
049: void selectType(Environment env, Context ctx, int tm) {
050: right = convert(env, ctx, Type.tBoolean, right);
051: }
052:
053: /*
054: * Check a "not" expression.
055: *
056: * cvars is modified so that
057: * cvar.vsTrue indicates variables with a known value if
058: * the expression is true.
059: * cvars.vsFalse indicates variables with a known value if
060: * the expression is false
061: *
062: * For "not" expressions, we look at the inside expression, and then
063: * swap true and false.
064: */
065:
066: public void checkCondition(Environment env, Context ctx, Vset vset,
067: Hashtable exp, ConditionVars cvars) {
068: right.checkCondition(env, ctx, vset, exp, cvars);
069: right = convert(env, ctx, Type.tBoolean, right);
070: // swap true and false
071: Vset temp = cvars.vsFalse;
072: cvars.vsFalse = cvars.vsTrue;
073: cvars.vsTrue = temp;
074: }
075:
076: /**
077: * Evaluate
078: */
079: Expression eval(boolean a) {
080: return new BooleanExpression(where, !a);
081: }
082:
083: /**
084: * Simplify
085: */
086: Expression simplify() {
087: // Check if the expression can be optimized
088: switch (right.op) {
089: case NOT:
090: return ((NotExpression) right).right;
091:
092: case EQ:
093: case NE:
094: case LT:
095: case LE:
096: case GT:
097: case GE:
098: break;
099:
100: default:
101: return this ;
102: }
103:
104: // Can't negate real comparisons
105: BinaryExpression bin = (BinaryExpression) right;
106: if (bin.left.type.inMask(TM_REAL)) {
107: return this ;
108: }
109:
110: // Negate comparison
111: switch (right.op) {
112: case EQ:
113: return new NotEqualExpression(where, bin.left, bin.right);
114: case NE:
115: return new EqualExpression(where, bin.left, bin.right);
116: case LT:
117: return new GreaterOrEqualExpression(where, bin.left,
118: bin.right);
119: case LE:
120: return new GreaterExpression(where, bin.left, bin.right);
121: case GT:
122: return new LessOrEqualExpression(where, bin.left, bin.right);
123: case GE:
124: return new LessExpression(where, bin.left, bin.right);
125: }
126: return this ;
127: }
128:
129: /**
130: * Code
131: */
132: void codeBranch(Environment env, Context ctx, Assembler asm,
133: Label lbl, boolean whenTrue) {
134: right.codeBranch(env, ctx, asm, lbl, !whenTrue);
135: }
136:
137: /**
138: * Instead of relying on the default code generation which uses
139: * conditional branching, generate a simpler stream using XOR.
140: */
141: public void codeValue(Environment env, Context ctx, Assembler asm) {
142: right.codeValue(env, ctx, asm);
143: asm.add(where, opc_ldc, new Integer(1));
144: asm.add(where, opc_ixor);
145: }
146:
147: }
|