Showing the changes to java/pkg/sun/tools/tree/FieldUpdater.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,242 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved. | |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| 4 | + * | |
| 5 | + * This code is free software; you can redistribute it and/or modify it | |
| 6 | + * under the terms of the GNU General Public License version 2 only, as | |
| 7 | + * published by the Free Software Foundation. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. | |
| 10 | + * | |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT | |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that | |
| 15 | + * accompanied this code). | |
| 16 | + * | |
| 17 | + * You should have received a copy of the GNU General Public License version | |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, | |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 20 | + * | |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
| 22 | + * or visit www.oracle.com if you need additional information or have any | |
| 23 | + * questions. | |
| 24 | + */ | |
| 25 | + | |
| 26 | +package sun.tools.tree; | |
| 27 | + | |
| 28 | +import sun.tools.java.*; | |
| 29 | +import sun.tools.asm.Assembler; | |
| 30 | +import java.io.PrintStream; | |
| 31 | + | |
| 32 | +/** | |
| 33 | + * This class encapsulates the information required to generate an update to a private | |
| 34 | + * field referenced from another class, e.g., an inner class. An expression denoting a | |
| 35 | + * reference to the object to which the field belongs is associated with getter and | |
| 36 | + * setter methods. | |
| 37 | + * <p> | |
| 38 | + * We use this class only for assignment, increment, and decrement operators, in which | |
| 39 | + * the old value is first retrieved and then a new value is computed and stored. | |
| 40 | + * Simple assignment expressions in which a value is copied without modification are | |
| 41 | + * handled by another mechanism. | |
| 42 | + * | |
| 43 | + * WARNING: The contents of this source file are not part of any | |
| 44 | + * supported API. Code that depends on them does so at its own risk: | |
| 45 | + * they are subject to change or removal without notice. | |
| 46 | + */ | |
| 47 | + | |
| 48 | +class FieldUpdater implements Constants { | |
| 49 | + | |
| 50 | + // Location for reporting errors. | |
| 51 | + // Errors will always indicate compiler failure, but these will be easier to diagnose | |
| 52 | + // if the bogus error is localized to the offending assignment. | |
| 53 | + private long where; | |
| 54 | + | |
| 55 | + // The field to which this updater applies. | |
| 56 | + // It would be easy to eliminate the need to store the field here, but we retain it for | |
| 57 | + // diagnostic purposes. | |
| 58 | + private MemberDefinition field; | |
| 59 | + | |
| 60 | + // Expression denoting the object to which the getter and setter are applied. | |
| 61 | + // If the field is static, 'base' may be null, but need not be, as a static field | |
| 62 | + // may be selected from an object reference. Even though the value of the object | |
| 63 | + // reference will be ignored, it may have side-effects. | |
| 64 | + private Expression base; | |
| 65 | + | |
| 66 | + // The getter and setter methods, generated by 'getAccessMember' and 'getUpdateMember'. | |
| 67 | + private MemberDefinition getter; | |
| 68 | + private MemberDefinition setter; | |
| 69 | + | |
| 70 | + // The number of words occupied on the stack by the object reference. | |
| 71 | + // For static fields, this is zero. | |
| 72 | + private int depth; | |
| 73 | + | |
| 74 | + /** | |
| 75 | + * Constructor. | |
| 76 | + */ | |
| 77 | + | |
| 78 | + public FieldUpdater(long where, MemberDefinition field, | |
| 79 | + Expression base, MemberDefinition getter, MemberDefinition setter) { | |
| 80 | + this.where = where; | |
| 81 | + this.field = field; | |
| 82 | + this.base = base; | |
| 83 | + this.getter = getter; | |
| 84 | + this.setter = setter; | |
| 85 | + } | |
| 86 | + | |
| 87 | + | |
| 88 | + /** | |
| 89 | + * Since the object reference expression may be captured before it has been inlined, | |
| 90 | + * we must inline it later. A <code>FieldUpdater</code> is inlined essentially as if | |
| 91 | + * it were a child of the assignment node to which it belongs. | |
| 92 | + */ | |
| 93 | + | |
| 94 | + public FieldUpdater inline(Environment env, Context ctx) { | |
| 95 | + if (base != null) { | |
| 96 | + if (field.isStatic()) { | |
| 97 | + base = base.inline(env, ctx); | |
| 98 | + } else { | |
| 99 | + base = base.inlineValue(env, ctx); | |
| 100 | + } | |
| 101 | + } | |
| 102 | + return this; | |
| 103 | + } | |
| 104 | + | |
| 105 | + public FieldUpdater copyInline(Context ctx) { | |
| 106 | + return new FieldUpdater(where, field, base.copyInline(ctx), getter, setter); | |
| 107 | + } | |
| 108 | + | |
| 109 | + public int costInline(int thresh, Environment env, Context ctx, boolean needGet) { | |
| 110 | + // Size of 'invokestatic' call for access method is 3 bytes. | |
| 111 | + int cost = needGet ? 7 : 3; // getter needs extra invokestatic + dup | |
| 112 | + // Size of expression to compute 'this' arg if needed. | |
| 113 | + if (!field.isStatic() && base != null) { | |
| 114 | + cost += base.costInline(thresh, env, ctx); | |
| 115 | + } | |
| 116 | + // We ignore the cost of duplicating value in value-needed context. | |
| 117 | + return cost; | |
| 118 | + } | |
| 119 | + | |
| 120 | + /** | |
| 121 | + * Duplicate <code>items</code> words from the top of the stack, locating them | |
| 122 | + * below the topmost <code>depth</code> words on the stack. | |
| 123 | + */ | |
| 124 | + | |
| 125 | + // This code was cribbed from 'Expression.java'. We cannot reuse that code here, | |
| 126 | + // because we do not inherit from class 'Expression'. | |
| 127 | + | |
| 128 | + private void codeDup(Assembler asm, int items, int depth) { | |
| 129 | + switch (items) { | |
| 130 | + case 0: | |
| 131 | + return; | |
| 132 | + case 1: | |
| 133 | + switch (depth) { | |
| 134 | + case 0: | |
| 135 | + asm.add(where, opc_dup); | |
| 136 | + return; | |
| 137 | + case 1: | |
| 138 | + asm.add(where, opc_dup_x1); | |
| 139 | + return; | |
| 140 | + case 2: | |
| 141 | + asm.add(where, opc_dup_x2); | |
| 142 | + return; | |
| 143 | + | |
| 144 | + } | |
| 145 | + break; | |
| 146 | + case 2: | |
| 147 | + switch (depth) { | |
| 148 | + case 0: | |
| 149 | + asm.add(where, opc_dup2); | |
| 150 | + return; | |
| 151 | + case 1: | |
| 152 | + asm.add(where, opc_dup2_x1); | |
| 153 | + return; | |
| 154 | + case 2: | |
| 155 | + asm.add(where, opc_dup2_x2); | |
| 156 | + return; | |
| 157 | + | |
| 158 | + } | |
| 159 | + break; | |
| 160 | + } | |
| 161 | + throw new CompilerError("can't dup: " + items + ", " + depth); | |
| 162 | + } | |
| 163 | + | |
| 164 | + /** | |
| 165 | + * Begin a field update by an assignment, increment, or decrement operator. | |
| 166 | + * The current value of the field is left at the top of the stack. | |
| 167 | + * If <code>valNeeded</code> is true, we arrange for the initial value to remain | |
| 168 | + * on the stack after the update. | |
| 169 | + */ | |
| 170 | + | |
| 171 | + public void startUpdate(Environment env, Context ctx, Assembler asm, boolean valNeeded) { | |
| 172 | + if (!(getter.isStatic() && setter.isStatic())) { | |
| 173 | + throw new CompilerError("startUpdate isStatic"); | |
| 174 | + } | |
| 175 | + if (!field.isStatic()) { | |
| 176 | + // Provide explicit 'this' argument. | |
| 177 | + base.codeValue(env, ctx, asm); | |
| 178 | + depth = 1; | |
| 179 | + } else { | |
| 180 | + // May need to evaluate 'base' for effect. | |
| 181 | + // If 'base' was a type expression, it should have previously been inlined away. | |
| 182 | + if (base != null) { | |
| 183 | + base.code(env, ctx, asm); | |
| 184 | + } | |
| 185 | + depth = 0; | |
| 186 | + } | |
| 187 | + codeDup(asm, depth, 0); | |
| 188 | + asm.add(where, opc_invokestatic, getter); | |
| 189 | + if (valNeeded) { | |
| 190 | + codeDup(asm, field.getType().stackSize(), depth); | |
| 191 | + } | |
| 192 | + } | |
| 193 | + | |
| 194 | + /** | |
| 195 | + * Complete a field update by an assignment, increment, or decrement operator. | |
| 196 | + * The original value of the field left on the stack by <code>startUpdate</code> | |
| 197 | + * must have been replaced with the updated value, with no other stack alterations. | |
| 198 | + * If <code>valNeeded</code> is true, we arrange for the updated value to remain | |
| 199 | + * on the stack after the update. The <code>valNeeded</code> argument must not be | |
| 200 | + * true in both <code>startUpdate</code> and <code>finishUpdate</code>. | |
| 201 | + */ | |
| 202 | + | |
| 203 | + public void finishUpdate(Environment env, Context ctx, Assembler asm, boolean valNeeded) { | |
| 204 | + if (valNeeded) { | |
| 205 | + codeDup(asm, field.getType().stackSize(), depth); | |
| 206 | + } | |
| 207 | + asm.add(where, opc_invokestatic, setter); | |
| 208 | + } | |
| 209 | + | |
| 210 | + /** | |
| 211 | + * Like above, but used when assigning a new value independent of the | |
| 212 | + * old, as in a simple assignment expression. After 'startAssign', | |
| 213 | + * code must be emitted to leave one additional value on the stack without | |
| 214 | + * altering any others, followed by 'finishAssign'. | |
| 215 | + */ | |
| 216 | + | |
| 217 | + public void startAssign(Environment env, Context ctx, Assembler asm) { | |
| 218 | + if (!setter.isStatic()) { | |
| 219 | + throw new CompilerError("startAssign isStatic"); | |
| 220 | + } | |
| 221 | + if (!field.isStatic()) { | |
| 222 | + // Provide explicit 'this' argument. | |
| 223 | + base.codeValue(env, ctx, asm); | |
| 224 | + depth = 1; | |
| 225 | + } else { | |
| 226 | + // May need to evaluate 'base' for effect. | |
| 227 | + // If 'base' was a type expression, it should have previously been inlined away. | |
| 228 | + if (base != null) { | |
| 229 | + base.code(env, ctx, asm); | |
| 230 | + } | |
| 231 | + depth = 0; | |
| 232 | + } | |
| 233 | + } | |
| 234 | + | |
| 235 | + public void finishAssign(Environment env, Context ctx, Assembler asm, boolean valNeeded) { | |
| 236 | + if (valNeeded) { | |
| 237 | + codeDup(asm, field.getType().stackSize(), depth); | |
| 238 | + } | |
| 239 | + asm.add(where, opc_invokestatic, setter); | |
| 240 | + } | |
| 241 | + | |
| 242 | +} | |