1/*
2 * Copyright (c) 1994, 2003, Oracle and/or its affiliates. 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.tools.tree;
27
28import sun.tools.java.*;
29import sun.tools.asm.Assembler;
30import sun.tools.asm.Label;
31import java.io.PrintStream;
32import 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 */
39public
40class WhileStatement extends Statement {
41    Expression cond;
42    Statement body;
43
44    /**
45     * Constructor
46     */
47    public WhileStatement(long where, Expression cond, Statement body) {
48        super(WHILE, where);
49        this.cond = cond;
50        this.body = body;
51    }
52
53    /**
54     * Check a while statement
55     */
56    Vset check(Environment env, Context ctx, Vset vset, Hashtable<Object, Object> exp) {
57        checkLabel(env, ctx);
58        CheckContext newctx = new CheckContext(ctx, this);
59        // remember what was unassigned on entry
60        Vset vsEntry = vset.copy();
61        // check the condition.  Determine which variables have values if
62        // it returns true or false.
63        ConditionVars cvars =
64              cond.checkCondition(env, newctx, reach(env, vset), exp);
65        cond = convert(env, newctx, Type.tBoolean, cond);
66        // check the body, given that the condition returned true.
67        vset = body.check(env, newctx, cvars.vsTrue, exp);
68        vset = vset.join(newctx.vsContinue);
69        // make sure the back-branch fits the entry of the loop
70        ctx.checkBackBranch(env, this, vsEntry, vset);
71        // Exit the while loop by testing false or getting a break statement
72        vset = newctx.vsBreak.join(cvars.vsFalse);
73        return ctx.removeAdditionalVars(vset);
74    }
75
76    /**
77     * Inline
78     */
79    public Statement inline(Environment env, Context ctx) {
80        ctx = new Context(ctx, this);
81        cond = cond.inlineValue(env, ctx);
82        if (body != null) {
83            body = body.inline(env, ctx);
84        }
85        return this;
86    }
87
88    /**
89     * The cost of inlining this statement
90     */
91    public int costInline(int thresh, Environment env, Context ctx) {
92        return 1 + cond.costInline(thresh, env, ctx)
93                 + ((body != null) ? body.costInline(thresh, env, ctx) : 0);
94    }
95
96    /**
97     * Create a copy of the statement for method inlining
98     */
99    public Statement copyInline(Context ctx, boolean valNeeded) {
100        WhileStatement s = (WhileStatement)clone();
101        s.cond = cond.copyInline(ctx);
102        if (body != null) {
103            s.body = body.copyInline(ctx, valNeeded);
104        }
105        return s;
106    }
107
108    /**
109     * Code
110     */
111    public void code(Environment env, Context ctx, Assembler asm) {
112        CodeContext newctx = new CodeContext(ctx, this);
113
114        asm.add(where, opc_goto, newctx.contLabel);
115
116        Label l1 = new Label();
117        asm.add(l1);
118
119        if (body != null) {
120            body.code(env, newctx, asm);
121        }
122
123        asm.add(newctx.contLabel);
124        cond.codeBranch(env, newctx, asm, l1, true);
125        asm.add(newctx.breakLabel);
126    }
127
128    /**
129     * Print
130     */
131    public void print(PrintStream out, int indent) {
132        super.print(out, indent);
133        out.print("while ");
134        cond.print(out);
135        if (body != null) {
136            out.print(" ");
137            body.print(out, indent);
138        } else {
139            out.print(";");
140        }
141    }
142}
143