jdk/src/share/classes/sun/tools/tree/SynchronizedStatement.java
author prr
Tue, 15 Jul 2014 11:22:14 -0700
changeset 25522 10d789df41bb
parent 22567 5816a47fa4dd
child 25799 1afc4675dc75
permissions -rw-r--r--
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes Reviewed-by: psandoz, prr Contributed-by: otaviopolianasantana@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 1994, 2014, 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.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * WARNING: The contents of this source file are not part of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * supported API.  Code that depends on them does so at its own risk:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * they are subject to change or removal without notice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
class SynchronizedStatement extends Statement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    Expression expr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    Statement body;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    boolean needReturnSlot;   // set by inner return statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public SynchronizedStatement(long where, Expression expr, Statement body) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        super(SYNCHRONIZED, where);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        this.expr = expr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        this.body = body;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Check statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    Vset check(Environment env, Context ctx, Vset vset, Hashtable exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        checkLabel(env, ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        CheckContext newctx = new CheckContext(ctx, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        vset = reach(env, vset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        vset = expr.checkValue(env, newctx, vset, exp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (expr.type.equals(Type.tNull)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            env.error(expr.where, "synchronized.null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        expr = convert(env, newctx, Type.tClass(idJavaLangObject), expr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        vset = body.check(env, newctx, vset, exp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return ctx.removeAdditionalVars(vset.join(newctx.vsBreak));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Inline
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public Statement inline(Environment env, Context ctx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (body != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            body = body.inline(env, ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        expr = expr.inlineValue(env, ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Create a copy of the statement for method inlining
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public Statement copyInline(Context ctx, boolean valNeeded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        SynchronizedStatement s = (SynchronizedStatement)clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        s.expr = expr.copyInline(ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (body != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            s.body = body.copyInline(ctx, valNeeded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Compute cost of inlining this statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public int costInline(int thresh, Environment env, Context ctx){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        int cost = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (expr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            cost += expr.costInline(thresh, env,ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (cost >= thresh) return cost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (body != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            cost += body.costInline(thresh, env,ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return cost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public void code(Environment env, Context ctx, Assembler asm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        ClassDefinition clazz = ctx.field.getClassDefinition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        expr.codeValue(env, ctx, asm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        ctx = new Context(ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (needReturnSlot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            Type returnType = ctx.field.getType().getReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            LocalMember localfield = new LocalMember(0, clazz, 0, returnType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                                   idFinallyReturnValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            ctx.declare(env, localfield);
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 5506
diff changeset
   123
            Environment.debugOutput("Assigning return slot to " + localfield.number);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        LocalMember f1 = new LocalMember(where, clazz, 0, Type.tObject, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        LocalMember f2 = new LocalMember(where, clazz, 0, Type.tInt, null);
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 22567
diff changeset
   128
        Integer num1 = ctx.declare(env, f1);
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 22567
diff changeset
   129
        Integer num2 = ctx.declare(env, f2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        Label endLabel = new Label();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        TryData td = new TryData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        td.add(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        // lock the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        asm.add(where, opc_astore, num1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        asm.add(where, opc_aload, num1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        asm.add(where, opc_monitorenter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // Main body
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        CodeContext bodyctx = new CodeContext(ctx, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        asm.add(where, opc_try, td);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (body != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            body.code(env, bodyctx, asm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            asm.add(where, opc_nop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        asm.add(bodyctx.breakLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        asm.add(td.getEndLabel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // Cleanup afer body
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        asm.add(where, opc_aload, num1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        asm.add(where, opc_monitorexit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        asm.add(where, opc_goto, endLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // Catch code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        CatchData cd = td.getCatch(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        asm.add(cd.getLabel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        asm.add(where, opc_aload, num1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        asm.add(where, opc_monitorexit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        asm.add(where, opc_athrow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        // Final body
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        asm.add(bodyctx.contLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        asm.add(where, opc_astore, num2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        asm.add(where, opc_aload, num1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        asm.add(where, opc_monitorexit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        asm.add(where, opc_ret, num2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        asm.add(endLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Print
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public void print(PrintStream out, int indent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        super.print(out, indent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        out.print("synchronized ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        expr.print(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        out.print(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (body != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            body.print(out, indent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            out.print("{}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
}