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 net.sf.retrotranslator.runtime.asm;
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: /**
040: * The line number corresponding to this label, if known.
041: */
042: int line;
043:
044: /**
045: * Indicates if the position of this label is known.
046: */
047: boolean resolved;
048:
049: /**
050: * The position of this label in the code, if known.
051: */
052: int position;
053:
054: /**
055: * If the label position has been updated, after instruction resizing.
056: */
057: boolean resized;
058:
059: /**
060: * Number of forward references to this label, times two.
061: */
062: private int referenceCount;
063:
064: /**
065: * Informations about forward references. Each forward reference is
066: * described by two consecutive integers in this array: the first one is the
067: * position of the first byte of the bytecode instruction that contains the
068: * forward reference, while the second is the position of the first byte of
069: * the forward reference itself. In fact the sign of the first integer
070: * indicates if this reference uses 2 or 4 bytes, and its absolute value
071: * gives the position of the bytecode instruction.
072: */
073: private int[] srcAndRefPositions;
074:
075: /*
076: * Fields for the control flow graph analysis algorithm (used to compute the
077: * maximum stack size). A control flow graph contains one node per "basic
078: * block", and one edge per "jump" from one basic block to another. Each
079: * node (i.e., each basic block) is represented by the Label object that
080: * corresponds to the first instruction of this basic block. Each node also
081: * stores the list of it successors in the graph, as a linked list of Edge
082: * objects.
083: */
084:
085: /**
086: * The stack size at the beginning of this basic block. This size is
087: * initially unknown. It is computed by the control flow analysis algorithm
088: * (see {@link MethodWriter#visitMaxs visitMaxs}).
089: */
090: int beginStackSize;
091:
092: /**
093: * The (relative) maximum stack size corresponding to this basic block. This
094: * size is relative to the stack size at the beginning of the basic block,
095: * i.e., the true maximum stack size is equal to {@link #beginStackSize
096: * beginStackSize} + {@link #maxStackSize maxStackSize}.
097: */
098: int maxStackSize;
099:
100: /**
101: * The successors of this node in the control flow graph. These successors
102: * are stored in a linked list of {@link Edge Edge} objects, linked to each
103: * other by their {@link Edge#next} field.
104: */
105: Edge successors;
106:
107: /**
108: * The next basic block in the basic block stack. See
109: * {@link MethodWriter#visitMaxs visitMaxs}.
110: */
111: Label next;
112:
113: /**
114: * <tt>true</tt> if this basic block has been pushed in the basic block
115: * stack. See {@link MethodWriter#visitMaxs visitMaxs}.
116: */
117: boolean pushed;
118:
119: // ------------------------------------------------------------------------
120: // Constructor
121: // ------------------------------------------------------------------------
122:
123: /**
124: * Constructs a new label.
125: */
126: public Label() {
127: }
128:
129: // ------------------------------------------------------------------------
130: // Methods to compute offsets and to manage forward references
131: // ------------------------------------------------------------------------
132:
133: /**
134: * Returns the offset corresponding to this label. This offset is computed
135: * from the start of the method's bytecode. <i>This method is intended for
136: * {@link Attribute} sub classes, and is normally not needed by class
137: * generators or adapters.</i>
138: *
139: * @return the offset corresponding to this label.
140: * @throws IllegalStateException if this label is not resolved yet.
141: */
142: public int getOffset() {
143: if (!resolved) {
144: throw new IllegalStateException(
145: "Label offset position has not been resolved yet");
146: }
147: return position;
148: }
149:
150: /**
151: * Puts a reference to this label in the bytecode of a method. If the
152: * position of the label is known, the offset is computed and written
153: * directly. Otherwise, a null offset is written and a new forward reference
154: * is declared for this label.
155: *
156: * @param owner the code writer that calls this method.
157: * @param out the bytecode of the method.
158: * @param source the position of first byte of the bytecode instruction that
159: * contains this label.
160: * @param wideOffset <tt>true</tt> if the reference must be stored in 4
161: * bytes, or <tt>false</tt> if it must be stored with 2 bytes.
162: * @throws IllegalArgumentException if this label has not been created by
163: * the given code writer.
164: */
165: void put(final MethodWriter owner, final ByteVector out,
166: final int source, final boolean wideOffset) {
167: if (resolved) {
168: if (wideOffset) {
169: out.putInt(position - source);
170: } else {
171: out.putShort(position - source);
172: }
173: } else {
174: if (wideOffset) {
175: addReference(-1 - source, out.length);
176: out.putInt(-1);
177: } else {
178: addReference(source, out.length);
179: out.putShort(-1);
180: }
181: }
182: }
183:
184: /**
185: * Adds a forward reference to this label. This method must be called only
186: * for a true forward reference, i.e. only if this label is not resolved
187: * yet. For backward references, the offset of the reference can be, and
188: * must be, computed and stored directly.
189: *
190: * @param sourcePosition the position of the referencing instruction. This
191: * position will be used to compute the offset of this forward
192: * reference.
193: * @param referencePosition the position where the offset for this forward
194: * reference must be stored.
195: */
196: private void addReference(final int sourcePosition,
197: final int referencePosition) {
198: if (srcAndRefPositions == null) {
199: srcAndRefPositions = new int[6];
200: }
201: if (referenceCount >= srcAndRefPositions.length) {
202: int[] a = new int[srcAndRefPositions.length + 6];
203: System.arraycopy(srcAndRefPositions, 0, a, 0,
204: srcAndRefPositions.length);
205: srcAndRefPositions = a;
206: }
207: srcAndRefPositions[referenceCount++] = sourcePosition;
208: srcAndRefPositions[referenceCount++] = referencePosition;
209: }
210:
211: /**
212: * Resolves all forward references to this label. This method must be called
213: * when this label is added to the bytecode of the method, i.e. when its
214: * position becomes known. This method fills in the blanks that where left
215: * in the bytecode by each forward reference previously added to this label.
216: *
217: * @param owner the code writer that calls this method.
218: * @param position the position of this label in the bytecode.
219: * @param data the bytecode of the method.
220: * @return <tt>true</tt> if a blank that was left for this label was to
221: * small to store the offset. In such a case the corresponding jump
222: * instruction is replaced with a pseudo instruction (using unused
223: * opcodes) using an unsigned two bytes offset. These pseudo
224: * instructions will need to be replaced with true instructions with
225: * wider offsets (4 bytes instead of 2). This is done in
226: * {@link MethodWriter#resizeInstructions}.
227: * @throws IllegalArgumentException if this label has already been resolved,
228: * or if it has not been created by the given code writer.
229: */
230: boolean resolve(final MethodWriter owner, final int position,
231: final byte[] data) {
232: boolean needUpdate = false;
233: this .resolved = true;
234: this .position = position;
235: int i = 0;
236: while (i < referenceCount) {
237: int source = srcAndRefPositions[i++];
238: int reference = srcAndRefPositions[i++];
239: int offset;
240: if (source >= 0) {
241: offset = position - source;
242: if (offset < Short.MIN_VALUE
243: || offset > Short.MAX_VALUE) {
244: /*
245: * changes the opcode of the jump instruction, in order to
246: * be able to find it later (see resizeInstructions in
247: * MethodWriter). These temporary opcodes are similar to
248: * jump instruction opcodes, except that the 2 bytes offset
249: * is unsigned (and can therefore represent values from 0 to
250: * 65535, which is sufficient since the size of a method is
251: * limited to 65535 bytes).
252: */
253: int opcode = data[reference - 1] & 0xFF;
254: if (opcode <= Opcodes.JSR) {
255: // changes IFEQ ... JSR to opcodes 202 to 217
256: data[reference - 1] = (byte) (opcode + 49);
257: } else {
258: // changes IFNULL and IFNONNULL to opcodes 218 and 219
259: data[reference - 1] = (byte) (opcode + 20);
260: }
261: needUpdate = true;
262: }
263: data[reference++] = (byte) (offset >>> 8);
264: data[reference] = (byte) offset;
265: } else {
266: offset = position + source + 1;
267: data[reference++] = (byte) (offset >>> 24);
268: data[reference++] = (byte) (offset >>> 16);
269: data[reference++] = (byte) (offset >>> 8);
270: data[reference] = (byte) offset;
271: }
272: }
273: return needUpdate;
274: }
275:
276: // ------------------------------------------------------------------------
277: // Overriden Object methods
278: // ------------------------------------------------------------------------
279:
280: /**
281: * Returns a string representation of this label.
282: *
283: * @return a string representation of this label.
284: */
285: public String toString() {
286: return "L" + System.identityHashCode(this);
287: }
288: }
|