0N/A/*
2362N/A * Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.tools.tree;
0N/A
0N/Aimport sun.tools.java.*;
0N/Aimport sun.tools.asm.Assembler;
0N/Aimport java.io.PrintStream;
0N/A
0N/A/**
0N/A * This class encapsulates the information required to generate an update to a private
0N/A * field referenced from another class, e.g., an inner class. An expression denoting a
0N/A * reference to the object to which the field belongs is associated with getter and
0N/A * setter methods.
0N/A * <p>
0N/A * We use this class only for assignment, increment, and decrement operators, in which
0N/A * the old value is first retrieved and then a new value is computed and stored.
0N/A * Simple assignment expressions in which a value is copied without modification are
0N/A * handled by another mechanism.
0N/A *
0N/A * WARNING: The contents of this source file are not part of any
0N/A * supported API. Code that depends on them does so at its own risk:
0N/A * they are subject to change or removal without notice.
0N/A */
0N/A
0N/Aclass FieldUpdater implements Constants {
0N/A
0N/A // Location for reporting errors.
0N/A // Errors will always indicate compiler failure, but these will be easier to diagnose
0N/A // if the bogus error is localized to the offending assignment.
0N/A private long where;
0N/A
0N/A // The field to which this updater applies.
0N/A // It would be easy to eliminate the need to store the field here, but we retain it for
0N/A // diagnostic purposes.
0N/A private MemberDefinition field;
0N/A
0N/A // Expression denoting the object to which the getter and setter are applied.
0N/A // If the field is static, 'base' may be null, but need not be, as a static field
0N/A // may be selected from an object reference. Even though the value of the object
0N/A // reference will be ignored, it may have side-effects.
0N/A private Expression base;
0N/A
0N/A // The getter and setter methods, generated by 'getAccessMember' and 'getUpdateMember'.
0N/A private MemberDefinition getter;
0N/A private MemberDefinition setter;
0N/A
0N/A // The number of words occupied on the stack by the object reference.
0N/A // For static fields, this is zero.
0N/A private int depth;
0N/A
0N/A /**
0N/A * Constructor.
0N/A */
0N/A
0N/A public FieldUpdater(long where, MemberDefinition field,
0N/A Expression base, MemberDefinition getter, MemberDefinition setter) {
0N/A this.where = where;
0N/A this.field = field;
0N/A this.base = base;
0N/A this.getter = getter;
0N/A this.setter = setter;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Since the object reference expression may be captured before it has been inlined,
0N/A * we must inline it later. A <code>FieldUpdater</code> is inlined essentially as if
0N/A * it were a child of the assignment node to which it belongs.
0N/A */
0N/A
0N/A public FieldUpdater inline(Environment env, Context ctx) {
0N/A if (base != null) {
0N/A if (field.isStatic()) {
0N/A base = base.inline(env, ctx);
0N/A } else {
0N/A base = base.inlineValue(env, ctx);
0N/A }
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A public FieldUpdater copyInline(Context ctx) {
0N/A return new FieldUpdater(where, field, base.copyInline(ctx), getter, setter);
0N/A }
0N/A
0N/A public int costInline(int thresh, Environment env, Context ctx, boolean needGet) {
0N/A // Size of 'invokestatic' call for access method is 3 bytes.
0N/A int cost = needGet ? 7 : 3; // getter needs extra invokestatic + dup
0N/A // Size of expression to compute 'this' arg if needed.
0N/A if (!field.isStatic() && base != null) {
0N/A cost += base.costInline(thresh, env, ctx);
0N/A }
0N/A // We ignore the cost of duplicating value in value-needed context.
0N/A return cost;
0N/A }
0N/A
0N/A /**
0N/A * Duplicate <code>items</code> words from the top of the stack, locating them
0N/A * below the topmost <code>depth</code> words on the stack.
0N/A */
0N/A
0N/A // This code was cribbed from 'Expression.java'. We cannot reuse that code here,
0N/A // because we do not inherit from class 'Expression'.
0N/A
0N/A private void codeDup(Assembler asm, int items, int depth) {
0N/A switch (items) {
0N/A case 0:
0N/A return;
0N/A case 1:
0N/A switch (depth) {
0N/A case 0:
0N/A asm.add(where, opc_dup);
0N/A return;
0N/A case 1:
0N/A asm.add(where, opc_dup_x1);
0N/A return;
0N/A case 2:
0N/A asm.add(where, opc_dup_x2);
0N/A return;
0N/A
0N/A }
0N/A break;
0N/A case 2:
0N/A switch (depth) {
0N/A case 0:
0N/A asm.add(where, opc_dup2);
0N/A return;
0N/A case 1:
0N/A asm.add(where, opc_dup2_x1);
0N/A return;
0N/A case 2:
0N/A asm.add(where, opc_dup2_x2);
0N/A return;
0N/A
0N/A }
0N/A break;
0N/A }
0N/A throw new CompilerError("can't dup: " + items + ", " + depth);
0N/A }
0N/A
0N/A /**
0N/A * Begin a field update by an assignment, increment, or decrement operator.
0N/A * The current value of the field is left at the top of the stack.
0N/A * If <code>valNeeded</code> is true, we arrange for the initial value to remain
0N/A * on the stack after the update.
0N/A */
0N/A
0N/A public void startUpdate(Environment env, Context ctx, Assembler asm, boolean valNeeded) {
0N/A if (!(getter.isStatic() && setter.isStatic())) {
0N/A throw new CompilerError("startUpdate isStatic");
0N/A }
0N/A if (!field.isStatic()) {
0N/A // Provide explicit 'this' argument.
0N/A base.codeValue(env, ctx, asm);
0N/A depth = 1;
0N/A } else {
0N/A // May need to evaluate 'base' for effect.
0N/A // If 'base' was a type expression, it should have previously been inlined away.
0N/A if (base != null) {
0N/A base.code(env, ctx, asm);
0N/A }
0N/A depth = 0;
0N/A }
0N/A codeDup(asm, depth, 0);
0N/A asm.add(where, opc_invokestatic, getter);
0N/A if (valNeeded) {
0N/A codeDup(asm, field.getType().stackSize(), depth);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Complete a field update by an assignment, increment, or decrement operator.
0N/A * The original value of the field left on the stack by <code>startUpdate</code>
0N/A * must have been replaced with the updated value, with no other stack alterations.
0N/A * If <code>valNeeded</code> is true, we arrange for the updated value to remain
0N/A * on the stack after the update. The <code>valNeeded</code> argument must not be
0N/A * true in both <code>startUpdate</code> and <code>finishUpdate</code>.
0N/A */
0N/A
0N/A public void finishUpdate(Environment env, Context ctx, Assembler asm, boolean valNeeded) {
0N/A if (valNeeded) {
0N/A codeDup(asm, field.getType().stackSize(), depth);
0N/A }
0N/A asm.add(where, opc_invokestatic, setter);
0N/A }
0N/A
0N/A /**
0N/A * Like above, but used when assigning a new value independent of the
0N/A * old, as in a simple assignment expression. After 'startAssign',
0N/A * code must be emitted to leave one additional value on the stack without
0N/A * altering any others, followed by 'finishAssign'.
0N/A */
0N/A
0N/A public void startAssign(Environment env, Context ctx, Assembler asm) {
0N/A if (!setter.isStatic()) {
0N/A throw new CompilerError("startAssign isStatic");
0N/A }
0N/A if (!field.isStatic()) {
0N/A // Provide explicit 'this' argument.
0N/A base.codeValue(env, ctx, asm);
0N/A depth = 1;
0N/A } else {
0N/A // May need to evaluate 'base' for effect.
0N/A // If 'base' was a type expression, it should have previously been inlined away.
0N/A if (base != null) {
0N/A base.code(env, ctx, asm);
0N/A }
0N/A depth = 0;
0N/A }
0N/A }
0N/A
0N/A public void finishAssign(Environment env, Context ctx, Assembler asm, boolean valNeeded) {
0N/A if (valNeeded) {
0N/A codeDup(asm, field.getType().stackSize(), depth);
0N/A }
0N/A asm.add(where, opc_invokestatic, setter);
0N/A }
0N/A
0N/A}