001: /***
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (c) 2000-2005 INRIA, France Telecom
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */package org.objectweb.asm.jip;
030:
031: /**
032: * A label represents a position in the bytecode of a method. Labels are used
033: * for jump, goto, and switch instructions, and for try catch blocks.
034: *
035: * @author Eric Bruneton
036: */
037: public class Label {
038:
039: // AW: added to support unwinding the call stack when an exception is caught
040: public Object info;
041:
042: /**
043: * The line number corresponding to this label, if known.
044: */
045: int line;
046:
047: /**
048: * Indicates if the position of this label is known.
049: */
050: boolean resolved;
051:
052: /**
053: * The position of this label in the code, if known.
054: */
055: int position;
056:
057: /**
058: * If the label position has been updated, after instruction resizing.
059: */
060: boolean resized;
061:
062: /**
063: * Number of forward references to this label, times two.
064: */
065: private int referenceCount;
066:
067: /**
068: * Informations about forward references. Each forward reference is
069: * described by two consecutive integers in this array: the first one is the
070: * position of the first byte of the bytecode instruction that contains the
071: * forward reference, while the second is the position of the first byte of
072: * the forward reference itself. In fact the sign of the first integer
073: * indicates if this reference uses 2 or 4 bytes, and its absolute value
074: * gives the position of the bytecode instruction.
075: */
076: private int[] srcAndRefPositions;
077:
078: /*
079: * Fields for the control flow graph analysis algorithm (used to compute the
080: * maximum stack size). A control flow graph contains one node per "basic
081: * block", and one edge per "jump" from one basic block to another. Each
082: * node (i.e., each basic block) is represented by the Label object that
083: * corresponds to the first instruction of this basic block. Each node also
084: * stores the list of it successors in the graph, as a linked list of Edge
085: * objects.
086: */
087:
088: /**
089: * The stack size at the beginning of this basic block. This size is
090: * initially unknown. It is computed by the control flow analysis algorithm
091: * (see {@link MethodWriter#visitMaxs visitMaxs}).
092: */
093: int beginStackSize;
094:
095: /**
096: * The (relative) maximum stack size corresponding to this basic block. This
097: * size is relative to the stack size at the beginning of the basic block,
098: * i.e., the true maximum stack size is equal to {@link #beginStackSize
099: * beginStackSize} + {@link #maxStackSize maxStackSize}.
100: */
101: int maxStackSize;
102:
103: /**
104: * The successors of this node in the control flow graph. These successors
105: * are stored in a linked list of {@link Edge Edge} objects, linked to each
106: * other by their {@link Edge#next} field.
107: */
108: Edge successors;
109:
110: /**
111: * The next basic block in the basic block stack. See
112: * {@link MethodWriter#visitMaxs visitMaxs}.
113: */
114: Label next;
115:
116: /**
117: * <tt>true</tt> if this basic block has been pushed in the basic block
118: * stack. See {@link MethodWriter#visitMaxs visitMaxs}.
119: */
120: boolean pushed;
121:
122: // ------------------------------------------------------------------------
123: // Constructor
124: // ------------------------------------------------------------------------
125:
126: /**
127: * Constructs a new label.
128: */
129: public Label() {
130: }
131:
132: // ------------------------------------------------------------------------
133: // Methods to compute offsets and to manage forward references
134: // ------------------------------------------------------------------------
135:
136: /**
137: * Returns the offset corresponding to this label. This offset is computed
138: * from the start of the method's bytecode. <i>This method is intended for
139: * {@link Attribute} sub classes, and is normally not needed by class
140: * generators or adapters.</i>
141: *
142: * @return the offset corresponding to this label.
143: * @throws IllegalStateException if this label is not resolved yet.
144: */
145: public int getOffset() {
146: if (!resolved) {
147: throw new IllegalStateException(
148: "Label offset position has not been resolved yet");
149: }
150: return position;
151: }
152:
153: /**
154: * Puts a reference to this label in the bytecode of a method. If the
155: * position of the label is known, the offset is computed and written
156: * directly. Otherwise, a null offset is written and a new forward reference
157: * is declared for this label.
158: *
159: * @param owner the code writer that calls this method.
160: * @param out the bytecode of the method.
161: * @param source the position of first byte of the bytecode instruction that
162: * contains this label.
163: * @param wideOffset <tt>true</tt> if the reference must be stored in 4
164: * bytes, or <tt>false</tt> if it must be stored with 2 bytes.
165: * @throws IllegalArgumentException if this label has not been created by
166: * the given code writer.
167: */
168: void put(final MethodWriter owner, final ByteVector out,
169: final int source, final boolean wideOffset) {
170: if (resolved) {
171: if (wideOffset) {
172: out.putInt(position - source);
173: } else {
174: out.putShort(position - source);
175: }
176: } else {
177: if (wideOffset) {
178: addReference(-1 - source, out.length);
179: out.putInt(-1);
180: } else {
181: addReference(source, out.length);
182: out.putShort(-1);
183: }
184: }
185: }
186:
187: /**
188: * Adds a forward reference to this label. This method must be called only
189: * for a true forward reference, i.e. only if this label is not resolved
190: * yet. For backward references, the offset of the reference can be, and
191: * must be, computed and stored directly.
192: *
193: * @param sourcePosition the position of the referencing instruction. This
194: * position will be used to compute the offset of this forward
195: * reference.
196: * @param referencePosition the position where the offset for this forward
197: * reference must be stored.
198: */
199: private void addReference(final int sourcePosition,
200: final int referencePosition) {
201: if (srcAndRefPositions == null) {
202: srcAndRefPositions = new int[6];
203: }
204: if (referenceCount >= srcAndRefPositions.length) {
205: int[] a = new int[srcAndRefPositions.length + 6];
206: System.arraycopy(srcAndRefPositions, 0, a, 0,
207: srcAndRefPositions.length);
208: srcAndRefPositions = a;
209: }
210: srcAndRefPositions[referenceCount++] = sourcePosition;
211: srcAndRefPositions[referenceCount++] = referencePosition;
212: }
213:
214: /**
215: * Resolves all forward references to this label. This method must be called
216: * when this label is added to the bytecode of the method, i.e. when its
217: * position becomes known. This method fills in the blanks that where left
218: * in the bytecode by each forward reference previously added to this label.
219: *
220: * @param owner the code writer that calls this method.
221: * @param position the position of this label in the bytecode.
222: * @param data the bytecode of the method.
223: * @return <tt>true</tt> if a blank that was left for this label was to
224: * small to store the offset. In such a case the corresponding jump
225: * instruction is replaced with a pseudo instruction (using unused
226: * opcodes) using an unsigned two bytes offset. These pseudo
227: * instructions will need to be replaced with true instructions with
228: * wider offsets (4 bytes instead of 2). This is done in
229: * {@link MethodWriter#resizeInstructions}.
230: * @throws IllegalArgumentException if this label has already been resolved,
231: * or if it has not been created by the given code writer.
232: */
233: boolean resolve(final MethodWriter owner, final int position,
234: final byte[] data) {
235: boolean needUpdate = false;
236: this .resolved = true;
237: this .position = position;
238: int i = 0;
239: while (i < referenceCount) {
240: int source = srcAndRefPositions[i++];
241: int reference = srcAndRefPositions[i++];
242: int offset;
243: if (source >= 0) {
244: offset = position - source;
245: if (offset < Short.MIN_VALUE
246: || offset > Short.MAX_VALUE) {
247: /*
248: * changes the opcode of the jump instruction, in order to
249: * be able to find it later (see resizeInstructions in
250: * MethodWriter). These temporary opcodes are similar to
251: * jump instruction opcodes, except that the 2 bytes offset
252: * is unsigned (and can therefore represent values from 0 to
253: * 65535, which is sufficient since the size of a method is
254: * limited to 65535 bytes).
255: */
256: int opcode = data[reference - 1] & 0xFF;
257: if (opcode <= Opcodes.JSR) {
258: // changes IFEQ ... JSR to opcodes 202 to 217
259: data[reference - 1] = (byte) (opcode + 49);
260: } else {
261: // changes IFNULL and IFNONNULL to opcodes 218 and 219
262: data[reference - 1] = (byte) (opcode + 20);
263: }
264: needUpdate = true;
265: }
266: data[reference++] = (byte) (offset >>> 8);
267: data[reference] = (byte) offset;
268: } else {
269: offset = position + source + 1;
270: data[reference++] = (byte) (offset >>> 24);
271: data[reference++] = (byte) (offset >>> 16);
272: data[reference++] = (byte) (offset >>> 8);
273: data[reference] = (byte) offset;
274: }
275: }
276: return needUpdate;
277: }
278:
279: // ------------------------------------------------------------------------
280: // Overriden Object methods
281: // ------------------------------------------------------------------------
282:
283: /**
284: * Returns a string representation of this label.
285: *
286: * @return a string representation of this label.
287: */
288: public String toString() {
289: return "L" + System.identityHashCode(this);
290: }
291: }
|