jdk/src/share/classes/sun/tools/tree/Statement.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1994-2003 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.tree;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.tools.java.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.tools.asm.Assembler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.tools.asm.Label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * WARNING: The contents of this source file are not part of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * supported API.  Code that depends on them does so at its own risk:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * they are subject to change or removal without notice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
class Statement extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public static final Vset DEAD_END = Vset.DEAD_END;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    Identifier labels[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    Statement(int op, long where) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        super(op, where);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * An empty statement.  Its costInline is infinite.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public static final Statement empty = new Statement(STAT, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * The largest possible interesting inline cost value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static final int MAXINLINECOST =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                      Integer.getInteger("javac.maxinlinecost",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                         30).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Insert a bit of code at the front of a statement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Side-effect s2, if it is a CompoundStatement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static Statement insertStatement(Statement s1, Statement s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (s2 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            s2 = s1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        } else if (s2 instanceof CompoundStatement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            // Do not add another level of block nesting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            ((CompoundStatement)s2).insertStatement(s1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            Statement body[] = { s1, s2 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            s2 = new CompoundStatement(s1.getWhere(), body);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return s2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Set the label of a statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public void setLabel(Environment env, Expression e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (e.op == IDENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (labels == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                labels = new Identifier[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                // this should almost never happen.  Multiple labels on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                // the same statement.  But handle it gracefully.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                Identifier newLabels[] = new Identifier[labels.length + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                System.arraycopy(labels, 0, newLabels, 1, labels.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                labels = newLabels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            labels[0] = ((IdentifierExpression)e).id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            env.error(e.where, "invalid.label");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Check a statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public Vset checkMethod(Environment env, Context ctx, Vset vset, Hashtable exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // Set up ctx.getReturnContext() for the sake of ReturnStatement.check().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        CheckContext mctx = new CheckContext(ctx, new Statement(METHOD, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        ctx = mctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        vset = check(env, ctx, vset, exp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // Check for return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (!ctx.field.getType().getReturnType().isType(TC_VOID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            // In general, we suppress further error messages due to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            // unreachable statements after reporting the first error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            // along a flow path (using 'clearDeadEnd').   Here, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            // report an error anyway, because the end of the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            // should be unreachable despite the earlier error.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            // difference in treatment is due to the fact that, in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            // case, the error is reachability, not unreachability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            // NOTE: In addition to this subtle difference in the quality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            // of the error diagnostics, this treatment is essential to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            // preserve the correctness of using 'clearDeadEnd' to implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            // the special-case reachability rules for if-then and if-then-else.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (!vset.isDeadEnd()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                env.error(ctx.field.getWhere(), "return.required.at.end", ctx.field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // Simulate a return at the end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        vset = vset.join(mctx.vsBreak);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return vset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    Vset checkDeclaration(Environment env, Context ctx, Vset vset, int mod, Type t, Hashtable exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        throw new CompilerError("checkDeclaration");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Make sure the labels on this statement do not duplicate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * labels on any enclosing statement.  Provided as a convenience
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * for subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    protected void checkLabel(Environment env, Context ctx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (labels != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            loop: for (int i = 0; i < labels.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                // Make sure there is not a double label on this statement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                for (int j = i+1; j < labels.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    if (labels[i] == labels[j]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        env.error(where, "nested.duplicate.label", labels[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        continue loop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                // Make sure no enclosing statement has the same label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                CheckContext destCtx =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    (CheckContext) ctx.getLabelContext(labels[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                if (destCtx != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    // Check to make sure the label is in not uplevel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    if (destCtx.frameNumber == ctx.frameNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        env.error(where, "nested.duplicate.label", labels[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            } // end loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    Vset check(Environment env, Context ctx, Vset vset, Hashtable exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        throw new CompilerError("check");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /** This is called in contexts where declarations are valid. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    Vset checkBlockStatement(Environment env, Context ctx, Vset vset, Hashtable exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return check(env, ctx, vset, exp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    Vset reach(Environment env, Vset vset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (vset.isDeadEnd()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            env.error(where, "stat.not.reached");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            vset = vset.clearDeadEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return vset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Inline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public Statement inline(Environment env, Context ctx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Eliminate this statement, which is only possible if it has no label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public Statement eliminate(Environment env, Statement s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if ((s != null) && (labels != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            Statement args[] = {s};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            s = new CompoundStatement(where, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            s.labels = labels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public void code(Environment env, Context ctx, Assembler asm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        throw new CompilerError("code");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Generate the code to call all finally's for a break, continue, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * return statement.  We must call "jsr" on all the cleanup code between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * the current context "ctx", and the destination context "stopctx".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * If 'save' isn't null, there is also a value on the top of the stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    void codeFinally(Environment env, Context ctx, Assembler asm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                        Context stopctx, Type save) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        Integer num = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        boolean haveCleanup = false; // there is a finally or synchronize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        boolean haveNonLocalFinally = false; // some finally doesn't return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        for (Context c = ctx; (c != null) && (c != stopctx); c = c.prev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            if (c.node == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            if (c.node.op == SYNCHRONIZED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                haveCleanup = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            } else if (c.node.op == FINALLY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                          && ((CodeContext)c).contLabel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                // c.contLabel == null indicates we're in the "finally" part
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                haveCleanup = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                FinallyStatement st = ((FinallyStatement)(c.node));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                if (!st.finallyCanFinish) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    haveNonLocalFinally = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    // after hitting a non-local finally, no need generating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    // further code, because it won't get executed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (!haveCleanup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            // there is no cleanup that needs to be done.  Just quit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (save != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            // This statement has a return value on the stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            ClassDefinition def = ctx.field.getClassDefinition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if (!haveNonLocalFinally) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                // Save the return value in the register which should have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                // been reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                LocalMember lf = ctx.getLocalField(idFinallyReturnValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                num = new Integer(lf.number);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                asm.add(where, opc_istore + save.getTypeCodeOffset(), num);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                // Pop the return value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                switch(ctx.field.getType().getReturnType().getTypeCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    case TC_VOID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    case TC_DOUBLE: case TC_LONG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                        asm.add(where, opc_pop2); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        asm.add(where, opc_pop); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        // Call each of the cleanup functions, as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        for (Context c = ctx ; (c != null)  && (c != stopctx) ; c = c.prev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            if (c.node == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            if (c.node.op == SYNCHRONIZED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                asm.add(where, opc_jsr, ((CodeContext)c).contLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            } else if (c.node.op == FINALLY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                          && ((CodeContext)c).contLabel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                FinallyStatement st = ((FinallyStatement)(c.node));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                Label label = ((CodeContext)c).contLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                if (st.finallyCanFinish) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    asm.add(where, opc_jsr, label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    // the code never returns, so we're done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    asm.add(where, opc_goto, label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        // Move the return value from the register back to the stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (num != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            asm.add(where, opc_iload + save.getTypeCodeOffset(), num);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Return true if the statement has the given label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public boolean hasLabel (Identifier lbl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        Identifier labels[] = this.labels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (labels != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            for (int i = labels.length; --i >= 0; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                if (labels[i].equals(lbl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Check if the first thing is a constructor invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public Expression firstConstructor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Create a copy of the statement for method inlining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public Statement copyInline(Context ctx, boolean valNeeded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        return (Statement)clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public int costInline(int thresh, Environment env, Context ctx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        return thresh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Print
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    void printIndent(PrintStream out, int indent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        for (int i = 0 ; i < indent ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            out.print("    ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public void print(PrintStream out, int indent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if (labels != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            for (int i = labels.length; --i >= 0; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                out.print(labels[i] + ": ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public void print(PrintStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        print(out, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
}