package sun.tools.tree;
import sun.tools.java.*;
import sun.tools.asm.Assembler;
import sun.tools.asm.Label;
import sun.tools.asm.TryData;
import sun.tools.asm.CatchData;
import java.io.PrintStream;
import java.util.Hashtable;
public
class SynchronizedStatement extends Statement {
Expression expr;
Statement body;
boolean needReturnSlot;
public SynchronizedStatement(long where, Expression expr, Statement body) {
super(SYNCHRONIZED, where);
this.expr = expr;
this.body = body;
}
Vset check(Environment env, Context ctx, Vset vset, Hashtable exp) {
checkLabel(env, ctx);
CheckContext newctx = new CheckContext(ctx, this);
vset = reach(env, vset);
vset = expr.checkValue(env, newctx, vset, exp);
if (expr.type.equals(Type.tNull)) {
env.error(expr.where, "synchronized.null");
}
expr = convert(env, newctx, Type.tClass(idJavaLangObject), expr);
vset = body.check(env, newctx, vset, exp);
return ctx.removeAdditionalVars(vset.join(newctx.vsBreak));
}
public Statement inline(Environment env, Context ctx) {
if (body != null) {
body = body.inline(env, ctx);
}
expr = expr.inlineValue(env, ctx);
return this;
}
public Statement copyInline(Context ctx, boolean valNeeded) {
SynchronizedStatement s = (SynchronizedStatement)clone();
s.expr = expr.copyInline(ctx);
if (body != null) {
s.body = body.copyInline(ctx, valNeeded);
}
return s;
}
public int costInline(int thresh, Environment env, Context ctx){
int cost = 1;
if (expr != null) {
cost += expr.costInline(thresh, env,ctx);
if (cost >= thresh) return cost;
}
if (body != null) {
cost += body.costInline(thresh, env,ctx);
}
return cost;
}
public void code(Environment env, Context ctx, Assembler asm) {
ClassDefinition clazz = ctx.field.getClassDefinition();
expr.codeValue(env, ctx, asm);
ctx = new Context(ctx);
if (needReturnSlot) {
Type returnType = ctx.field.getType().getReturnType();
LocalMember localfield = new LocalMember(0, clazz, 0, returnType,
idFinallyReturnValue);
ctx.declare(env, localfield);
env.debugOutput("Assigning return slot to " + localfield.number);
}
LocalMember f1 = new LocalMember(where, clazz, 0, Type.tObject, null);
LocalMember f2 = new LocalMember(where, clazz, 0, Type.tInt, null);
Integer num1 = new Integer(ctx.declare(env, f1));
Integer num2 = new Integer(ctx.declare(env, f2));
Label endLabel = new Label();
TryData td = new TryData();
td.add(null);
asm.add(where, opc_astore, num1);
asm.add(where, opc_aload, num1);
asm.add(where, opc_monitorenter);
CodeContext bodyctx = new CodeContext(ctx, this);
asm.add(where, opc_try, td);
if (body != null) {
body.code(env, bodyctx, asm);
} else {
asm.add(where, opc_nop);
}
asm.add(bodyctx.breakLabel);
asm.add(td.getEndLabel());
asm.add(where, opc_aload, num1);
asm.add(where, opc_monitorexit);
asm.add(where, opc_goto, endLabel);
CatchData cd = td.getCatch(0);
asm.add(cd.getLabel());
asm.add(where, opc_aload, num1);
asm.add(where, opc_monitorexit);
asm.add(where, opc_athrow);
asm.add(bodyctx.contLabel);
asm.add(where, opc_astore, num2);
asm.add(where, opc_aload, num1);
asm.add(where, opc_monitorexit);
asm.add(where, opc_ret, num2);
asm.add(endLabel);
}
public void print(PrintStream out, int indent) {
super.print(out, indent);
out.print("synchronized ");
expr.print(out);
out.print(" ");
if (body != null) {
body.print(out, indent);
} else {
out.print("{}");
}
}
}