001: /***
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (C) 2000 INRIA, France Telecom
004: * Copyright (C) 2002 France Telecom
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * Contact: Eric.Bruneton@rd.francetelecom.com
021: *
022: * Author: Eric Bruneton
023: */package bsh.org.objectweb.asm;
024:
025: /**
026: * A label represents a position in the bytecode of a method. Labels are used
027: * for jump, goto, and switch instructions, and for try catch blocks.
028: */
029:
030: public class Label {
031:
032: /**
033: * The code writer to which this label belongs, or <tt>null</tt> if unknown.
034: */
035:
036: CodeWriter owner;
037:
038: /**
039: * Indicates if the position of this label is known.
040: */
041:
042: boolean resolved;
043:
044: /**
045: * The position of this label in the code, if known.
046: */
047:
048: int position;
049:
050: /**
051: * Number of forward references to this label, times two.
052: */
053:
054: private int referenceCount;
055:
056: /**
057: * Informations about forward references. Each forward reference is described
058: * by two consecutive integers in this array: the first one is the position
059: * of the first byte of the bytecode instruction that contains the forward
060: * reference, while the second is the position of the first byte of the
061: * forward reference itself. In fact the sign of the first integer indicates
062: * if this reference uses 2 or 4 bytes, and its absolute value gives the
063: * position of the bytecode instruction.
064: */
065:
066: private int[] srcAndRefPositions;
067:
068: // --------------------------------------------------------------------------
069: // Fields for the control flow graph analysis algorithm (used to compute the
070: // maximum stack size). A control flow graph contains one node per "basic
071: // block", and one edge per "jump" from one basic block to another. Each node
072: // (i.e., each basic block) is represented by the Label object that
073: // corresponds to the first instruction of this basic block. Each node also
074: // stores the list of it successors in the graph, as a linked list of Edge
075: // objects.
076: // --------------------------------------------------------------------------
077:
078: /**
079: * The stack size at the beginning of this basic block.
080: * This size is initially unknown. It is computed by the control flow
081: * analysis algorithm (see {@link CodeWriter#visitMaxs visitMaxs}).
082: */
083:
084: int beginStackSize;
085:
086: /**
087: * The (relative) maximum stack size corresponding to this basic block. This
088: * size is relative to the stack size at the beginning of the basic block,
089: * i.e., the true maximum stack size is equal to {@link #beginStackSize
090: * beginStackSize} + {@link #maxStackSize maxStackSize}.
091: */
092:
093: int maxStackSize;
094:
095: /**
096: * The successors of this node in the control flow graph. These successors
097: * are stored in a linked list of {@link Edge Edge} objects, linked to each
098: * other by their {@link Edge#next} field.
099: */
100:
101: Edge successors;
102:
103: /**
104: * The next basic block in the basic block stack.
105: * See {@link CodeWriter#visitMaxs visitMaxs}.
106: */
107:
108: Label next;
109:
110: /**
111: * <tt>true</tt> if this basic block has been pushed in the basic block stack.
112: * See {@link CodeWriter#visitMaxs visitMaxs}.
113: */
114:
115: boolean pushed;
116:
117: // --------------------------------------------------------------------------
118: // Constructor
119: // --------------------------------------------------------------------------
120:
121: /**
122: * Constructs a new label.
123: */
124:
125: public Label() {
126: }
127:
128: // --------------------------------------------------------------------------
129: // Methods to compute offsets and to manage forward references
130: // --------------------------------------------------------------------------
131:
132: /**
133: * Puts a reference to this label in the bytecode of a method. If the position
134: * of the label is known, the offset is computed and written directly.
135: * Otherwise, a null offset is written and a new forward reference is declared
136: * for this label.
137: *
138: * @param owner the code writer that calls this method.
139: * @param out the bytecode of the method.
140: * @param source the position of first byte of the bytecode instruction that
141: * contains this label.
142: * @param wideOffset <tt>true</tt> if the reference must be stored in 4 bytes,
143: * or <tt>false</tt> if it must be stored with 2 bytes.
144: * @throws IllegalArgumentException if this label has not been created by the
145: * given code writer.
146: */
147:
148: void put(final CodeWriter owner, final ByteVector out,
149: final int source, final boolean wideOffset) {
150: if (CodeWriter.CHECK) {
151: if (this .owner == null) {
152: this .owner = owner;
153: } else if (this .owner != owner) {
154: throw new IllegalArgumentException();
155: }
156: }
157: if (resolved) {
158: if (wideOffset) {
159: out.put4(position - source);
160: } else {
161: out.put2(position - source);
162: }
163: } else {
164: if (wideOffset) {
165: addReference(-1 - source, out.length);
166: out.put4(-1);
167: } else {
168: addReference(source, out.length);
169: out.put2(-1);
170: }
171: }
172: }
173:
174: /**
175: * Adds a forward reference to this label. This method must be called only for
176: * a true forward reference, i.e. only if this label is not resolved yet. For
177: * backward references, the offset of the reference can be, and must be,
178: * computed and stored directly.
179: *
180: * @param sourcePosition the position of the referencing instruction. This
181: * position will be used to compute the offset of this forward reference.
182: * @param referencePosition the position where the offset for this forward
183: * reference must be stored.
184: */
185:
186: private void addReference(final int sourcePosition,
187: final int referencePosition) {
188: if (srcAndRefPositions == null) {
189: srcAndRefPositions = new int[6];
190: }
191: if (referenceCount >= srcAndRefPositions.length) {
192: int[] a = new int[srcAndRefPositions.length + 6];
193: System.arraycopy(srcAndRefPositions, 0, a, 0,
194: srcAndRefPositions.length);
195: srcAndRefPositions = a;
196: }
197: srcAndRefPositions[referenceCount++] = sourcePosition;
198: srcAndRefPositions[referenceCount++] = referencePosition;
199: }
200:
201: /**
202: * Resolves all forward references to this label. This method must be called
203: * when this label is added to the bytecode of the method, i.e. when its
204: * position becomes known. This method fills in the blanks that where left in
205: * the bytecode by each forward reference previously added to this label.
206: *
207: * @param owner the code writer that calls this method.
208: * @param position the position of this label in the bytecode.
209: * @param data the bytecode of the method.
210: * @return <tt>true</tt> if a blank that was left for this label was to small
211: * to store the offset. In such a case the corresponding jump instruction
212: * is replaced with a pseudo instruction (using unused opcodes) using an
213: * unsigned two bytes offset. These pseudo instructions will need to be
214: * replaced with true instructions with wider offsets (4 bytes instead of
215: * 2). This is done in {@link CodeWriter#resizeInstructions}.
216: * @throws IllegalArgumentException if this label has already been resolved,
217: * or if it has not been created by the given code writer.
218: */
219:
220: boolean resolve(final CodeWriter owner, final int position,
221: final byte[] data) {
222: if (CodeWriter.CHECK) {
223: if (this .owner == null) {
224: this .owner = owner;
225: }
226: if (resolved || this .owner != owner) {
227: throw new IllegalArgumentException();
228: }
229: }
230: boolean needUpdate = false;
231: this .resolved = true;
232: this .position = position;
233: int i = 0;
234: while (i < referenceCount) {
235: int source = srcAndRefPositions[i++];
236: int reference = srcAndRefPositions[i++];
237: int offset;
238: if (source >= 0) {
239: offset = position - source;
240: if (offset < Short.MIN_VALUE
241: || offset > Short.MAX_VALUE) {
242: // changes the opcode of the jump instruction, in order to be able to
243: // find it later (see resizeInstructions in CodeWriter). These
244: // temporary opcodes are similar to jump instruction opcodes, except
245: // that the 2 bytes offset is unsigned (and can therefore represent
246: // values from 0 to 65535, which is sufficient since the size of a
247: // method is limited to 65535 bytes).
248: int opcode = data[reference - 1] & 0xFF;
249: if (opcode <= Constants.JSR) {
250: // changes IFEQ ... JSR to opcodes 202 to 217 (inclusive)
251: data[reference - 1] = (byte) (opcode + 49);
252: } else {
253: // changes IFNULL and IFNONNULL to opcodes 218 and 219 (inclusive)
254: data[reference - 1] = (byte) (opcode + 20);
255: }
256: needUpdate = true;
257: }
258: data[reference++] = (byte) (offset >>> 8);
259: data[reference] = (byte) offset;
260: } else {
261: offset = position + source + 1;
262: data[reference++] = (byte) (offset >>> 24);
263: data[reference++] = (byte) (offset >>> 16);
264: data[reference++] = (byte) (offset >>> 8);
265: data[reference] = (byte) offset;
266: }
267: }
268: return needUpdate;
269: }
270: }
|