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 DoStatement extends Statement {
040: Statement body;
041: Expression cond;
042:
043: /**
044: * Constructor
045: */
046: public DoStatement(long where, Statement body, Expression cond) {
047: super (DO, where);
048: this .body = body;
049: this .cond = cond;
050: }
051:
052: /**
053: * Check statement
054: */
055: Vset check(Environment env, Context ctx, Vset vset, Hashtable exp) {
056: checkLabel(env, ctx);
057: CheckContext newctx = new CheckContext(ctx, this );
058: // remember what was unassigned on entry
059: Vset vsEntry = vset.copy();
060: vset = body.check(env, newctx, reach(env, vset), exp);
061: vset = vset.join(newctx.vsContinue);
062: // get to the test either by falling through the body, or through
063: // a "continue" statement.
064: ConditionVars cvars = cond.checkCondition(env, newctx, vset,
065: exp);
066: cond = convert(env, newctx, Type.tBoolean, cond);
067: // make sure the back-branch fits the entry of the loop
068: ctx.checkBackBranch(env, this , vsEntry, cvars.vsTrue);
069: // exit the loop through the test returning false, or a "break"
070: vset = newctx.vsBreak.join(cvars.vsFalse);
071: return ctx.removeAdditionalVars(vset);
072: }
073:
074: /**
075: * Inline
076: */
077: public Statement inline(Environment env, Context ctx) {
078: ctx = new Context(ctx, this );
079: if (body != null) {
080: body = body.inline(env, ctx);
081: }
082: cond = cond.inlineValue(env, ctx);
083: return this ;
084: }
085:
086: /**
087: * Create a copy of the statement for method inlining
088: */
089: public Statement copyInline(Context ctx, boolean valNeeded) {
090: DoStatement s = (DoStatement) clone();
091: s.cond = cond.copyInline(ctx);
092: if (body != null) {
093: s.body = body.copyInline(ctx, valNeeded);
094: }
095: return s;
096: }
097:
098: /**
099: * The cost of inlining this statement
100: */
101: public int costInline(int thresh, Environment env, Context ctx) {
102: return 1
103: + cond.costInline(thresh, env, ctx)
104: + ((body != null) ? body.costInline(thresh, env, ctx)
105: : 0);
106: }
107:
108: /**
109: * Code
110: */
111: public void code(Environment env, Context ctx, Assembler asm) {
112: Label l1 = new Label();
113: asm.add(l1);
114:
115: CodeContext newctx = new CodeContext(ctx, this );
116:
117: if (body != null) {
118: body.code(env, newctx, asm);
119: }
120: asm.add(newctx.contLabel);
121: cond.codeBranch(env, newctx, asm, l1, true);
122: asm.add(newctx.breakLabel);
123: }
124:
125: /**
126: * Print
127: */
128: public void print(PrintStream out, int indent) {
129: super .print(out, indent);
130: out.print("do ");
131: body.print(out, indent);
132: out.print(" while ");
133: cond.print(out);
134: out.print(";");
135: }
136: }
|