001: /*
002: * Copyright 1994-2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.tree;
027:
028: import sun.tools.java.*;
029: import sun.tools.asm.Assembler;
030: import sun.tools.asm.Label;
031: import sun.tools.asm.TryData;
032: import sun.tools.asm.CatchData;
033: import java.io.PrintStream;
034: import java.util.Hashtable;
035:
036: /**
037: * WARNING: The contents of this source file are not part of any
038: * supported API. Code that depends on them does so at its own risk:
039: * they are subject to change or removal without notice.
040: */
041: public class SynchronizedStatement extends Statement {
042: Expression expr;
043: Statement body;
044: boolean needReturnSlot; // set by inner return statement
045:
046: /**
047: * Constructor
048: */
049: public SynchronizedStatement(long where, Expression expr,
050: Statement body) {
051: super (SYNCHRONIZED, where);
052: this .expr = expr;
053: this .body = body;
054: }
055:
056: /**
057: * Check statement
058: */
059: Vset check(Environment env, Context ctx, Vset vset, Hashtable exp) {
060: checkLabel(env, ctx);
061: CheckContext newctx = new CheckContext(ctx, this );
062: vset = reach(env, vset);
063: vset = expr.checkValue(env, newctx, vset, exp);
064: if (expr.type.equals(Type.tNull)) {
065: env.error(expr.where, "synchronized.null");
066: }
067: expr = convert(env, newctx, Type.tClass(idJavaLangObject), expr);
068: vset = body.check(env, newctx, vset, exp);
069: return ctx.removeAdditionalVars(vset.join(newctx.vsBreak));
070: }
071:
072: /**
073: * Inline
074: */
075: public Statement inline(Environment env, Context ctx) {
076: if (body != null) {
077: body = body.inline(env, ctx);
078: }
079: expr = expr.inlineValue(env, ctx);
080: return this ;
081: }
082:
083: /**
084: * Create a copy of the statement for method inlining
085: */
086: public Statement copyInline(Context ctx, boolean valNeeded) {
087: SynchronizedStatement s = (SynchronizedStatement) clone();
088: s.expr = expr.copyInline(ctx);
089: if (body != null) {
090: s.body = body.copyInline(ctx, valNeeded);
091: }
092: return s;
093: }
094:
095: /**
096: * Compute cost of inlining this statement
097: */
098: public int costInline(int thresh, Environment env, Context ctx) {
099: int cost = 1;
100: if (expr != null) {
101: cost += expr.costInline(thresh, env, ctx);
102: if (cost >= thresh)
103: return cost;
104: }
105: if (body != null) {
106: cost += body.costInline(thresh, env, ctx);
107: }
108: return cost;
109: }
110:
111: /**
112: * Code
113: */
114: public void code(Environment env, Context ctx, Assembler asm) {
115: ClassDefinition clazz = ctx.field.getClassDefinition();
116: expr.codeValue(env, ctx, asm);
117: ctx = new Context(ctx);
118:
119: if (needReturnSlot) {
120: Type returnType = ctx.field.getType().getReturnType();
121: LocalMember localfield = new LocalMember(0, clazz, 0,
122: returnType, idFinallyReturnValue);
123: ctx.declare(env, localfield);
124: env.debugOutput("Assigning return slot to "
125: + localfield.number);
126: }
127:
128: LocalMember f1 = new LocalMember(where, clazz, 0, Type.tObject,
129: null);
130: LocalMember f2 = new LocalMember(where, clazz, 0, Type.tInt,
131: null);
132: Integer num1 = new Integer(ctx.declare(env, f1));
133: Integer num2 = new Integer(ctx.declare(env, f2));
134:
135: Label endLabel = new Label();
136:
137: TryData td = new TryData();
138: td.add(null);
139:
140: // lock the object
141: asm.add(where, opc_astore, num1);
142: asm.add(where, opc_aload, num1);
143: asm.add(where, opc_monitorenter);
144:
145: // Main body
146: CodeContext bodyctx = new CodeContext(ctx, this );
147: asm.add(where, opc_try, td);
148: if (body != null) {
149: body.code(env, bodyctx, asm);
150: } else {
151: asm.add(where, opc_nop);
152: }
153: asm.add(bodyctx.breakLabel);
154: asm.add(td.getEndLabel());
155:
156: // Cleanup afer body
157: asm.add(where, opc_aload, num1);
158: asm.add(where, opc_monitorexit);
159: asm.add(where, opc_goto, endLabel);
160:
161: // Catch code
162: CatchData cd = td.getCatch(0);
163: asm.add(cd.getLabel());
164: asm.add(where, opc_aload, num1);
165: asm.add(where, opc_monitorexit);
166: asm.add(where, opc_athrow);
167:
168: // Final body
169: asm.add(bodyctx.contLabel);
170: asm.add(where, opc_astore, num2);
171: asm.add(where, opc_aload, num1);
172: asm.add(where, opc_monitorexit);
173: asm.add(where, opc_ret, num2);
174:
175: asm.add(endLabel);
176: }
177:
178: /**
179: * Print
180: */
181: public void print(PrintStream out, int indent) {
182: super .print(out, indent);
183: out.print("synchronized ");
184: expr.print(out);
185: out.print(" ");
186: if (body != null) {
187: body.print(out, indent);
188: } else {
189: out.print("{}");
190: }
191: }
192: }
|