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.io.PrintStream;
032: import java.util.Hashtable;
033:
034: /**
035: * WARNING: The contents of this source file are not part of any
036: * supported API. Code that depends on them does so at its own risk:
037: * they are subject to change or removal without notice.
038: */
039: public class Statement extends Node {
040: public static final Vset DEAD_END = Vset.DEAD_END;
041: Identifier labels[] = null;
042:
043: /**
044: * Constructor
045: */
046: Statement(int op, long where) {
047: super (op, where);
048: }
049:
050: /**
051: * An empty statement. Its costInline is infinite.
052: */
053: public static final Statement empty = new Statement(STAT, 0);
054:
055: /**
056: * The largest possible interesting inline cost value.
057: */
058: public static final int MAXINLINECOST = Integer.getInteger(
059: "javac.maxinlinecost", 30).intValue();
060:
061: /**
062: * Insert a bit of code at the front of a statement.
063: * Side-effect s2, if it is a CompoundStatement.
064: */
065: public static Statement insertStatement(Statement s1, Statement s2) {
066: if (s2 == null) {
067: s2 = s1;
068: } else if (s2 instanceof CompoundStatement) {
069: // Do not add another level of block nesting.
070: ((CompoundStatement) s2).insertStatement(s1);
071: } else {
072: Statement body[] = { s1, s2 };
073: s2 = new CompoundStatement(s1.getWhere(), body);
074: }
075: return s2;
076: }
077:
078: /**
079: * Set the label of a statement
080: */
081: public void setLabel(Environment env, Expression e) {
082: if (e.op == IDENT) {
083: if (labels == null) {
084: labels = new Identifier[1];
085: } else {
086: // this should almost never happen. Multiple labels on
087: // the same statement. But handle it gracefully.
088: Identifier newLabels[] = new Identifier[labels.length + 1];
089: System
090: .arraycopy(labels, 0, newLabels, 1,
091: labels.length);
092: labels = newLabels;
093: }
094: labels[0] = ((IdentifierExpression) e).id;
095: } else {
096: env.error(e.where, "invalid.label");
097: }
098: }
099:
100: /**
101: * Check a statement
102: */
103: public Vset checkMethod(Environment env, Context ctx, Vset vset,
104: Hashtable exp) {
105: // Set up ctx.getReturnContext() for the sake of ReturnStatement.check().
106: CheckContext mctx = new CheckContext(ctx, new Statement(METHOD,
107: 0));
108: ctx = mctx;
109:
110: vset = check(env, ctx, vset, exp);
111:
112: // Check for return
113: if (!ctx.field.getType().getReturnType().isType(TC_VOID)) {
114: // In general, we suppress further error messages due to
115: // unreachable statements after reporting the first error
116: // along a flow path (using 'clearDeadEnd'). Here, we
117: // report an error anyway, because the end of the method
118: // should be unreachable despite the earlier error. The
119: // difference in treatment is due to the fact that, in this
120: // case, the error is reachability, not unreachability.
121: // NOTE: In addition to this subtle difference in the quality
122: // of the error diagnostics, this treatment is essential to
123: // preserve the correctness of using 'clearDeadEnd' to implement
124: // the special-case reachability rules for if-then and if-then-else.
125: if (!vset.isDeadEnd()) {
126: env.error(ctx.field.getWhere(),
127: "return.required.at.end", ctx.field);
128: }
129: }
130:
131: // Simulate a return at the end.
132: vset = vset.join(mctx.vsBreak);
133:
134: return vset;
135: }
136:
137: Vset checkDeclaration(Environment env, Context ctx, Vset vset,
138: int mod, Type t, Hashtable exp) {
139: throw new CompilerError("checkDeclaration");
140: }
141:
142: /**
143: * Make sure the labels on this statement do not duplicate the
144: * labels on any enclosing statement. Provided as a convenience
145: * for subclasses.
146: */
147: protected void checkLabel(Environment env, Context ctx) {
148: if (labels != null) {
149: loop: for (int i = 0; i < labels.length; i++) {
150: // Make sure there is not a double label on this statement.
151: for (int j = i + 1; j < labels.length; j++) {
152: if (labels[i] == labels[j]) {
153: env.error(where, "nested.duplicate.label",
154: labels[i]);
155: continue loop;
156: }
157: }
158:
159: // Make sure no enclosing statement has the same label.
160: CheckContext destCtx = (CheckContext) ctx
161: .getLabelContext(labels[i]);
162:
163: if (destCtx != null) {
164: // Check to make sure the label is in not uplevel.
165: if (destCtx.frameNumber == ctx.frameNumber) {
166: env.error(where, "nested.duplicate.label",
167: labels[i]);
168: }
169: }
170: } // end loop
171: }
172: }
173:
174: Vset check(Environment env, Context ctx, Vset vset, Hashtable exp) {
175: throw new CompilerError("check");
176: }
177:
178: /** This is called in contexts where declarations are valid. */
179: Vset checkBlockStatement(Environment env, Context ctx, Vset vset,
180: Hashtable exp) {
181: return check(env, ctx, vset, exp);
182: }
183:
184: Vset reach(Environment env, Vset vset) {
185: if (vset.isDeadEnd()) {
186: env.error(where, "stat.not.reached");
187: vset = vset.clearDeadEnd();
188: }
189: return vset;
190: }
191:
192: /**
193: * Inline
194: */
195: public Statement inline(Environment env, Context ctx) {
196: return this ;
197: }
198:
199: /**
200: * Eliminate this statement, which is only possible if it has no label.
201: */
202: public Statement eliminate(Environment env, Statement s) {
203: if ((s != null) && (labels != null)) {
204: Statement args[] = { s };
205: s = new CompoundStatement(where, args);
206: s.labels = labels;
207: }
208: return s;
209: }
210:
211: /**
212: * Code
213: */
214: public void code(Environment env, Context ctx, Assembler asm) {
215: throw new CompilerError("code");
216: }
217:
218: /**
219: * Generate the code to call all finally's for a break, continue, or
220: * return statement. We must call "jsr" on all the cleanup code between
221: * the current context "ctx", and the destination context "stopctx".
222: * If 'save' isn't null, there is also a value on the top of the stack
223: */
224: void codeFinally(Environment env, Context ctx, Assembler asm,
225: Context stopctx, Type save) {
226: Integer num = null;
227: boolean haveCleanup = false; // there is a finally or synchronize;
228: boolean haveNonLocalFinally = false; // some finally doesn't return;
229:
230: for (Context c = ctx; (c != null) && (c != stopctx); c = c.prev) {
231: if (c.node == null)
232: continue;
233: if (c.node.op == SYNCHRONIZED) {
234: haveCleanup = true;
235: } else if (c.node.op == FINALLY
236: && ((CodeContext) c).contLabel != null) {
237: // c.contLabel == null indicates we're in the "finally" part
238: haveCleanup = true;
239: FinallyStatement st = ((FinallyStatement) (c.node));
240: if (!st.finallyCanFinish) {
241: haveNonLocalFinally = true;
242: // after hitting a non-local finally, no need generating
243: // further code, because it won't get executed.
244: break;
245: }
246: }
247: }
248: if (!haveCleanup) {
249: // there is no cleanup that needs to be done. Just quit.
250: return;
251: }
252: if (save != null) {
253: // This statement has a return value on the stack.
254: ClassDefinition def = ctx.field.getClassDefinition();
255: if (!haveNonLocalFinally) {
256: // Save the return value in the register which should have
257: // been reserved.
258: LocalMember lf = ctx
259: .getLocalField(idFinallyReturnValue);
260: num = new Integer(lf.number);
261: asm.add(where, opc_istore + save.getTypeCodeOffset(),
262: num);
263: } else {
264: // Pop the return value.
265: switch (ctx.field.getType().getReturnType()
266: .getTypeCode()) {
267: case TC_VOID:
268: break;
269: case TC_DOUBLE:
270: case TC_LONG:
271: asm.add(where, opc_pop2);
272: break;
273: default:
274: asm.add(where, opc_pop);
275: break;
276: }
277: }
278: }
279: // Call each of the cleanup functions, as necessary.
280: for (Context c = ctx; (c != null) && (c != stopctx); c = c.prev) {
281: if (c.node == null)
282: continue;
283: if (c.node.op == SYNCHRONIZED) {
284: asm.add(where, opc_jsr, ((CodeContext) c).contLabel);
285: } else if (c.node.op == FINALLY
286: && ((CodeContext) c).contLabel != null) {
287: FinallyStatement st = ((FinallyStatement) (c.node));
288: Label label = ((CodeContext) c).contLabel;
289: if (st.finallyCanFinish) {
290: asm.add(where, opc_jsr, label);
291: } else {
292: // the code never returns, so we're done.
293: asm.add(where, opc_goto, label);
294: break;
295: }
296: }
297: }
298: // Move the return value from the register back to the stack.
299: if (num != null) {
300: asm.add(where, opc_iload + save.getTypeCodeOffset(), num);
301: }
302: }
303:
304: /*
305: * Return true if the statement has the given label
306: */
307: public boolean hasLabel(Identifier lbl) {
308: Identifier labels[] = this .labels;
309: if (labels != null) {
310: for (int i = labels.length; --i >= 0;) {
311: if (labels[i].equals(lbl)) {
312: return true;
313: }
314: }
315: }
316: return false;
317: }
318:
319: /**
320: * Check if the first thing is a constructor invocation
321: */
322: public Expression firstConstructor() {
323: return null;
324: }
325:
326: /**
327: * Create a copy of the statement for method inlining
328: */
329: public Statement copyInline(Context ctx, boolean valNeeded) {
330: return (Statement) clone();
331: }
332:
333: public int costInline(int thresh, Environment env, Context ctx) {
334: return thresh;
335: }
336:
337: /**
338: * Print
339: */
340: void printIndent(PrintStream out, int indent) {
341: for (int i = 0; i < indent; i++) {
342: out.print(" ");
343: }
344: }
345:
346: public void print(PrintStream out, int indent) {
347: if (labels != null) {
348: for (int i = labels.length; --i >= 0;)
349: out.print(labels[i] + ": ");
350: }
351: }
352:
353: public void print(PrintStream out) {
354: print(out, 0);
355: }
356: }
|