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 DoStatement extends Statement {
41    Statement body;
42    Expression cond;
43
44    /**
45     * Constructor
46     */
47    public DoStatement(long where, Statement body, Expression cond) {
48        super(DO, where);
49        this.body = body;
50        this.cond = cond;
51    }
52
53    /**
54     * Check 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        vset = body.check(env, newctx, reach(env, vset), exp);
62        vset = vset.join(newctx.vsContinue);
63        // get to the test either by falling through the body, or through
64        // a "continue" statement.
65        ConditionVars cvars =
66            cond.checkCondition(env, newctx, vset, exp);
67        cond = convert(env, newctx, Type.tBoolean, cond);
68        // make sure the back-branch fits the entry of the loop
69        ctx.checkBackBranch(env, this, vsEntry, cvars.vsTrue);
70        // exit the loop through the test returning false, or a "break"
71        vset = newctx.vsBreak.join(cvars.vsFalse);
72        return ctx.removeAdditionalVars(vset);
73    }
74
75    /**
76     * Inline
77     */
78    public Statement inline(Environment env, Context ctx) {
79        ctx = new Context(ctx, this);
80        if (body != null) {
81            body = body.inline(env, ctx);
82        }
83        cond = cond.inlineValue(env, ctx);
84        return this;
85    }
86
87    /**
88     * Create a copy of the statement for method inlining
89     */
90    public Statement copyInline(Context ctx, boolean valNeeded) {
91        DoStatement s = (DoStatement)clone();
92        s.cond = cond.copyInline(ctx);
93        if (body != null) {
94            s.body = body.copyInline(ctx, valNeeded);
95        }
96        return s;
97    }
98
99    /**
100     * The cost of inlining this statement
101     */
102    public int costInline(int thresh, Environment env, Context ctx) {
103        return 1 + cond.costInline(thresh, env, ctx)
104                + ((body != null) ? body.costInline(thresh, env, ctx) : 0);
105    }
106
107    /**
108     * Code
109     */
110    public void code(Environment env, Context ctx, Assembler asm) {
111        Label l1 = new Label();
112        asm.add(l1);
113
114        CodeContext newctx = new CodeContext(ctx, this);
115
116        if (body != null) {
117            body.code(env, newctx, asm);
118        }
119        asm.add(newctx.contLabel);
120        cond.codeBranch(env, newctx, asm, l1, true);
121        asm.add(newctx.breakLabel);
122    }
123
124    /**
125     * Print
126     */
127    public void print(PrintStream out, int indent) {
128        super.print(out, indent);
129        out.print("do ");
130        body.print(out, indent);
131        out.print(" while ");
132        cond.print(out);
133        out.print(";");
134    }
135}
136