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 java.io.PrintStream;
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 AssignExpression extends BinaryAssignExpression {
039:
040: private FieldUpdater updater = null;
041:
042: /**
043: * Constructor
044: */
045: public AssignExpression(long where, Expression left,
046: Expression right) {
047: super (ASSIGN, where, left, right);
048: }
049:
050: /**
051: * Check an assignment expression
052: */
053: public Vset checkValue(Environment env, Context ctx, Vset vset,
054: Hashtable exp) {
055: if (left instanceof IdentifierExpression) {
056: // we don't want to mark an identifier as having a value
057: // until having evaluated the right-hand side
058: vset = right.checkValue(env, ctx, vset, exp);
059: vset = left.checkLHS(env, ctx, vset, exp);
060: } else {
061: // normally left to right evaluation.
062: vset = left.checkLHS(env, ctx, vset, exp);
063: vset = right.checkValue(env, ctx, vset, exp);
064: }
065: type = left.type;
066: right = convert(env, ctx, type, right);
067:
068: // Get field updater (access method) if needed, else null.
069: updater = left.getAssigner(env, ctx);
070:
071: return vset;
072: }
073:
074: /**
075: * Inline
076: */
077: public Expression inlineValue(Environment env, Context ctx) {
078: if (implementation != null)
079: return implementation.inlineValue(env, ctx);
080: // Must be 'inlineLHS' here. But compare with similar case in
081: // 'AssignOpExpression' and 'IncDecExpression', which needs 'inlineValue'.
082: left = left.inlineLHS(env, ctx);
083: right = right.inlineValue(env, ctx);
084: if (updater != null) {
085: updater = updater.inline(env, ctx);
086: }
087: return this ;
088: }
089:
090: /**
091: * Create a copy of the expression for method inlining
092: */
093: public Expression copyInline(Context ctx) {
094: if (implementation != null)
095: return implementation.copyInline(ctx);
096: AssignExpression e = (AssignExpression) clone();
097: e.left = left.copyInline(ctx);
098: e.right = right.copyInline(ctx);
099: if (updater != null) {
100: e.updater = updater.copyInline(ctx);
101: }
102: return e;
103: }
104:
105: /**
106: * The cost of inlining this expression
107: */
108: public int costInline(int thresh, Environment env, Context ctx) {
109: /*----------*
110: return 2 + super.costInline(thresh, env, ctx);
111: *----------*/
112: return (updater != null)
113: // Cost of rhs expression + cost of access method call.
114: // Access method call cost includes lhs cost.
115: ? right.costInline(thresh, env, ctx)
116: + updater.costInline(thresh, env, ctx, false)
117: // Cost of rhs expression + cost of lhs expression +
118: // cost of store instruction.
119: : right.costInline(thresh, env, ctx)
120: + left.costInline(thresh, env, ctx) + 2;
121: }
122:
123: /**
124: * Code
125: */
126: public void codeValue(Environment env, Context ctx, Assembler asm) {
127: if (updater == null) {
128: // Field is directly accessible.
129: int depth = left.codeLValue(env, ctx, asm);
130: right.codeValue(env, ctx, asm);
131: codeDup(env, ctx, asm, right.type.stackSize(), depth);
132: left.codeStore(env, ctx, asm);
133: } else {
134: // Must use access method.
135: // Left operand is always a 'FieldExpression', or
136: // is rewritten as one via 'implementation'.
137: updater.startAssign(env, ctx, asm);
138: right.codeValue(env, ctx, asm);
139: updater.finishAssign(env, ctx, asm, true);
140: }
141: }
142:
143: public void code(Environment env, Context ctx, Assembler asm) {
144: if (updater == null) {
145: // Field is directly accessible.
146: left.codeLValue(env, ctx, asm);
147: right.codeValue(env, ctx, asm);
148: left.codeStore(env, ctx, asm);
149: } else {
150: // Must use access method.
151: // Left operand is always a 'FieldExpression', or
152: // is rewritten as one via 'implementation'.
153: updater.startAssign(env, ctx, asm);
154: right.codeValue(env, ctx, asm);
155: updater.finishAssign(env, ctx, asm, false);
156: }
157: }
158: }
|