J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1994-2003 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.tools.tree; |
| 27 | |
| 28 | import sun.tools.java.*; |
| 29 | import sun.tools.asm.Assembler; |
| 30 | import sun.tools.asm.Label; |
| 31 | import java.io.PrintStream; |
| 32 | import java.util.Hashtable; |
| 33 | |
| 34 | /** |
| 35 | * WARNING: The contents of this source file are not part of any |
| 36 | * supported API. Code that depends on them does so at its own risk: |
| 37 | * they are subject to change or removal without notice. |
| 38 | */ |
| 39 | public |
| 40 | class ForStatement extends Statement { |
| 41 | Statement init; |
| 42 | Expression cond; |
| 43 | Expression inc; |
| 44 | Statement body; |
| 45 | |
| 46 | /** |
| 47 | * Constructor |
| 48 | */ |
| 49 | public ForStatement(long where, Statement init, Expression cond, Expression inc, Statement body) { |
| 50 | super(FOR, where); |
| 51 | this.init = init; |
| 52 | this.cond = cond; |
| 53 | this.inc = inc; |
| 54 | this.body = body; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Check statement |
| 59 | */ |
| 60 | Vset check(Environment env, Context ctx, Vset vset, Hashtable exp) { |
| 61 | checkLabel(env, ctx); |
| 62 | vset = reach(env, vset); |
| 63 | Context initctx = new Context(ctx, this); |
| 64 | if (init != null) { |
| 65 | vset = init.checkBlockStatement(env, initctx, vset, exp); |
| 66 | } |
| 67 | CheckContext newctx = new CheckContext(initctx, this); |
| 68 | // remember what was unassigned on entry |
| 69 | Vset vsEntry = vset.copy(); |
| 70 | ConditionVars cvars; |
| 71 | if (cond != null) { |
| 72 | cvars = cond.checkCondition(env, newctx, vset, exp); |
| 73 | cond = convert(env, newctx, Type.tBoolean, cond); |
| 74 | } else { |
| 75 | // a missing test is equivalent to "true" |
| 76 | cvars = new ConditionVars(); |
| 77 | cvars.vsFalse = Vset.DEAD_END; |
| 78 | cvars.vsTrue = vset; |
| 79 | } |
| 80 | vset = body.check(env, newctx, cvars.vsTrue, exp); |
| 81 | vset = vset.join(newctx.vsContinue); |
| 82 | if (inc != null) { |
| 83 | vset = inc.check(env, newctx, vset, exp); |
| 84 | } |
| 85 | // Make sure the back-branch fits the entry of the loop. |
| 86 | // Must include variables declared in the for-init part in the |
| 87 | // set of variables visible upon loop entry that must be checked. |
| 88 | initctx.checkBackBranch(env, this, vsEntry, vset); |
| 89 | // exit by testing false or executing a break; |
| 90 | vset = newctx.vsBreak.join(cvars.vsFalse); |
| 91 | return ctx.removeAdditionalVars(vset); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Inline |
| 96 | */ |
| 97 | public Statement inline(Environment env, Context ctx) { |
| 98 | ctx = new Context(ctx, this); |
| 99 | 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 | } |