jdk/src/share/classes/sun/tools/tree/TryStatement.java
author ntoda
Thu, 31 Jul 2014 17:01:24 -0700
changeset 25799 1afc4675dc75
parent 5506 202f599c92aa
permissions -rw-r--r--
8044867: Fix raw and unchecked lint warnings in sun.tools.* Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1994, 2003, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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 sun.tools.asm.TryData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.tools.asm.CatchData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * WARNING: The contents of this source file are not part of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * supported API.  Code that depends on them does so at its own risk:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * they are subject to change or removal without notice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class TryStatement extends Statement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    Statement body;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    Statement args[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    long arrayCloneWhere;       // private note posted from MethodExpression
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public TryStatement(long where, Statement body, Statement args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        super(TRY, where);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        this.body = body;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        this.args = args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Check statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 5506
diff changeset
    60
    Vset check(Environment env, Context ctx, Vset vset, Hashtable<Object, Object> exp) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        checkLabel(env, ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            vset = reach(env, vset);
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 5506
diff changeset
    64
            Hashtable<Object, Object> newexp = new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            CheckContext newctx =  new CheckContext(ctx, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            // Check 'try' block.  A variable is DA (DU) before the try
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            // block if it is DA (DU) before the try statement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            Vset vs = body.check(env, newctx, vset.copy(), newexp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            // A variable is DA before a catch block if it is DA before the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            // try statement.  A variable is DU before a catch block if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            // is DU after the try block and before any 'break', 'continue',
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            // 'throw', or 'return' contained therein. That is, the variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            // is DU upon entry to the try-statement and is not assigned to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            // anywhere within the try block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            Vset cvs = Vset.firstDAandSecondDU(vset, vs.copy().join(newctx.vsTryExit));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            for (int i = 0 ; i < args.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                // A variable is DA (DU) after a try statement if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                // it is DA (DU) after every catch block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                vs = vs.join(args[i].check(env, newctx, cvs.copy(), exp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            // Check that catch statements are actually reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            for (int i = 1 ; i < args.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                CatchStatement cs = (CatchStatement)args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                if (cs.field == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                Type type = cs.field.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                ClassDefinition def = env.getClassDefinition(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                for (int j = 0 ; j < i ; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    CatchStatement cs2 = (CatchStatement)args[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    if (cs2.field == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    Type t = cs2.field.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    ClassDeclaration c = env.getClassDeclaration(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    if (def.subClassOf(env, c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        env.error(args[i].where, "catch.not.reached");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            ClassDeclaration ignore1 = env.getClassDeclaration(idJavaLangError);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            ClassDeclaration ignore2 = env.getClassDeclaration(idJavaLangRuntimeException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            // Make sure the exception is actually throw in that part of the code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            for (int i = 0 ; i < args.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                CatchStatement cs = (CatchStatement)args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                if (cs.field == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                Type type = cs.field.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                if (!type.isType(TC_CLASS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    // CatchStatement.checkValue() will have already printed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    // an error message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                ClassDefinition def = env.getClassDefinition(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                // Anyone can throw these!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                if (def.subClassOf(env, ignore1) || def.superClassOf(env, ignore1) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    def.subClassOf(env, ignore2) || def.superClassOf(env, ignore2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                // Make sure the exception is actually throw in that part of the code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                boolean ok = false;
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 5506
diff changeset
   134
                for (Enumeration<?> e = newexp.keys() ; e.hasMoreElements() ; ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    ClassDeclaration c = (ClassDeclaration)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    if (def.superClassOf(env, c) || def.subClassOf(env, c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                if (!ok && arrayCloneWhere != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    && def.getName().toString().equals("java.lang.CloneNotSupportedException")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    env.error(arrayCloneWhere, "warn.array.clone.supported", def.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                if (!ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    env.error(cs.where, "catch.not.thrown", def.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            // Only carry over exceptions that are not caught
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 5506
diff changeset
   152
            for (Enumeration<?> e = newexp.keys() ; e.hasMoreElements() ; ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                ClassDeclaration c = (ClassDeclaration)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                ClassDefinition def = c.getClassDefinition(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                boolean add = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                for (int i = 0 ; i < args.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    CatchStatement cs = (CatchStatement)args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    if (cs.field == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    Type type = cs.field.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    if (type.isType(TC_ERROR))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    if (def.subClassOf(env, env.getClassDeclaration(type))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        add = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                if (add) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    exp.put(c, newexp.get(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            // A variable is DA (DU) after a try statement if it is DA (DU)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            // after the try block and after every catch block. These variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            // are represented by 'vs'.  If the try statement is labelled, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            // may also exit from it (including from within a catch block) via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            // a break statement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            // If there is a finally block, the Vset returned here is further
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            // adjusted. Note that this 'TryStatement' node will be a child of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            // a 'FinallyStatement' node in that case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            return ctx.removeAdditionalVars(vs.join(newctx.vsBreak));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } catch (ClassNotFound e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            env.error(where, "class.not.found", e.name, opNames[op]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            return vset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Inline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public Statement inline(Environment env, Context ctx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (body != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            body = body.inline(env, new Context(ctx, this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (body == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        for (int i = 0 ; i < args.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            if (args[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                args[i] = args[i].inline(env, new Context(ctx, this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return (args.length == 0) ? eliminate(env, body) : this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Create a copy of the statement for method inlining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public Statement copyInline(Context ctx, boolean valNeeded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        TryStatement s = (TryStatement)clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (body != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            s.body = body.copyInline(ctx, valNeeded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        s.args = new Statement[args.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        for (int i = 0 ; i < args.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            if (args[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                s.args[i] = args[i].copyInline(ctx, valNeeded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Compute cost of inlining this statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public int costInline(int thresh, Environment env, Context ctx){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        // Don't inline methods containing try statements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        // If the try statement is being inlined in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        // inline a method that returns a value which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        // a subexpression of an expression involving the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // operand stack, then the early operands may get lost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        // This shows up as a verifier error.  For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        // in the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        //    public static int test() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        //       try { return 2; } catch (Exception e)  { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        //    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        //    System.out.println(test());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        // an inlined call to test() might look like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        //     0 getstatic <Field java.io.PrintStream out>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        //     3 iconst_2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        //     4 goto 9
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        //     7 pop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        //     8 iconst_0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        //     9 invokevirtual <Method void println(int)>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        //    12 return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        //  Exception table:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        //     from   to  target type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        //       3     7     7   <Class java.lang.Exception>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        // This fails to verify because the operand stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        // for System.out gets axed at an exception, leading to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        // an inconsistent stack depth at pc=7.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        // Note that although all code must be able to be inlined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        // to implement initializers, this problem doesn't come up,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        // as try statements themselves can never be expressions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        // It suffices here to make sure they are never inlined as part
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        // of optimization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        return thresh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public void code(Environment env, Context ctx, Assembler asm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        CodeContext newctx = new CodeContext(ctx, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        TryData td = new TryData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        for (int i = 0 ; i < args.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            Type t = ((CatchStatement)args[i]).field.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            if (t.isType(TC_CLASS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                td.add(env.getClassDeclaration(t));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                td.add(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        asm.add(where, opc_try, td);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (body != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            body.code(env, newctx, asm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        asm.add(td.getEndLabel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        asm.add(where, opc_goto, newctx.breakLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        for (int i = 0 ; i < args.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            CatchData cd = td.getCatch(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            asm.add(cd.getLabel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            args[i].code(env, newctx, asm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            asm.add(where, opc_goto, newctx.breakLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        asm.add(newctx.breakLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Print
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public void print(PrintStream out, int indent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        super.print(out, indent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        out.print("try ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (body != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            body.print(out, indent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            out.print("<empty>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        for (int i = 0 ; i < args.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            out.print(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            args[i].print(out, indent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
}