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 ForStatement extends Statement {
040: Statement init;
041: Expression cond;
042: Expression inc;
043: Statement body;
044:
045: /**
046: * Constructor
047: */
048: public ForStatement(long where, Statement init, Expression cond,
049: Expression inc, Statement body) {
050: super (FOR, where);
051: this .init = init;
052: this .cond = cond;
053: this .inc = inc;
054: this .body = body;
055: }
056:
057: /**
058: * Check statement
059: */
060: Vset check(Environment env, Context ctx, Vset vset, Hashtable exp) {
061: checkLabel(env, ctx);
062: vset = reach(env, vset);
063: Context initctx = new Context(ctx, this );
064: if (init != null) {
065: vset = init.checkBlockStatement(env, initctx, vset, exp);
066: }
067: CheckContext newctx = new CheckContext(initctx, this );
068: // remember what was unassigned on entry
069: Vset vsEntry = vset.copy();
070: ConditionVars cvars;
071: if (cond != null) {
072: cvars = cond.checkCondition(env, newctx, vset, exp);
073: cond = convert(env, newctx, Type.tBoolean, cond);
074: } else {
075: // a missing test is equivalent to "true"
076: cvars = new ConditionVars();
077: cvars.vsFalse = Vset.DEAD_END;
078: cvars.vsTrue = vset;
079: }
080: vset = body.check(env, newctx, cvars.vsTrue, exp);
081: vset = vset.join(newctx.vsContinue);
082: if (inc != null) {
083: vset = inc.check(env, newctx, vset, exp);
084: }
085: // Make sure the back-branch fits the entry of the loop.
086: // Must include variables declared in the for-init part in the
087: // set of variables visible upon loop entry that must be checked.
088: initctx.checkBackBranch(env, this , vsEntry, vset);
089: // exit by testing false or executing a break;
090: vset = newctx.vsBreak.join(cvars.vsFalse);
091: return ctx.removeAdditionalVars(vset);
092: }
093:
094: /**
095: * Inline
096: */
097: public Statement inline(Environment env, Context ctx) {
098: ctx = new Context(ctx, this );
099: if (init != null) {
100: Statement body[] = { init, this };
101: init = null;
102: return new CompoundStatement(where, body).inline(env, ctx);
103: }
104: if (cond != null) {
105: cond = cond.inlineValue(env, ctx);
106: }
107: if (body != null) {
108: body = body.inline(env, ctx);
109: }
110: if (inc != null) {
111: inc = inc.inline(env, ctx);
112: }
113: return this ;
114: }
115:
116: /**
117: * Create a copy of the statement for method inlining
118: */
119: public Statement copyInline(Context ctx, boolean valNeeded) {
120: ForStatement s = (ForStatement) clone();
121: if (init != null) {
122: s.init = init.copyInline(ctx, valNeeded);
123: }
124: if (cond != null) {
125: s.cond = cond.copyInline(ctx);
126: }
127: if (body != null) {
128: s.body = body.copyInline(ctx, valNeeded);
129: }
130: if (inc != null) {
131: s.inc = inc.copyInline(ctx);
132: }
133: return s;
134: }
135:
136: /**
137: * The cost of inlining this statement
138: */
139: public int costInline(int thresh, Environment env, Context ctx) {
140: int cost = 2;
141: if (init != null) {
142: cost += init.costInline(thresh, env, ctx);
143: }
144: if (cond != null) {
145: cost += cond.costInline(thresh, env, ctx);
146: }
147: if (body != null) {
148: cost += body.costInline(thresh, env, ctx);
149: }
150: if (inc != null) {
151: cost += inc.costInline(thresh, env, ctx);
152: }
153: return cost;
154: }
155:
156: /**
157: * Code
158: */
159: public void code(Environment env, Context ctx, Assembler asm) {
160: CodeContext newctx = new CodeContext(ctx, this );
161: if (init != null) {
162: init.code(env, newctx, asm);
163: }
164:
165: Label l1 = new Label();
166: Label l2 = new Label();
167:
168: asm.add(where, opc_goto, l2);
169:
170: asm.add(l1);
171: if (body != null) {
172: body.code(env, newctx, asm);
173: }
174:
175: asm.add(newctx.contLabel);
176: if (inc != null) {
177: inc.code(env, newctx, asm);
178: }
179:
180: asm.add(l2);
181: if (cond != null) {
182: cond.codeBranch(env, newctx, asm, l1, true);
183: } else {
184: asm.add(where, opc_goto, l1);
185: }
186: asm.add(newctx.breakLabel);
187: }
188:
189: /**
190: * Print
191: */
192: public void print(PrintStream out, int indent) {
193: super .print(out, indent);
194: out.print("for (");
195: if (init != null) {
196: init.print(out, indent);
197: out.print(" ");
198: } else {
199: out.print("; ");
200: }
201: if (cond != null) {
202: cond.print(out);
203: out.print(" ");
204: }
205: out.print("; ");
206: if (inc != null) {
207: inc.print(out);
208: }
209: out.print(") ");
210: if (body != null) {
211: body.print(out, indent);
212: } else {
213: out.print(";");
214: }
215: }
216: }
|