KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > Label


1 /***
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000-2005 INRIA, France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package org.objectweb.asm;
31
32 /**
33  * A label represents a position in the bytecode of a method. Labels are used
34  * for jump, goto, and switch instructions, and for try catch blocks.
35  *
36  * @author Eric Bruneton
37  */

38 public class Label {
39
40     /**
41      * The line number corresponding to this label, if known.
42      */

43     int line;
44
45     /**
46      * Indicates if the position of this label is known.
47      */

48     boolean resolved;
49
50     /**
51      * The position of this label in the code, if known.
52      */

53     int position;
54
55     /**
56      * If the label position has been updated, after instruction resizing.
57      */

58     boolean resized;
59
60     /**
61      * Number of forward references to this label, times two.
62      */

63     private int referenceCount;
64
65     /**
66      * Informations about forward references. Each forward reference is
67      * described by two consecutive integers in this array: the first one is the
68      * position of the first byte of the bytecode instruction that contains the
69      * forward reference, while the second is the position of the first byte of
70      * the forward reference itself. In fact the sign of the first integer
71      * indicates if this reference uses 2 or 4 bytes, and its absolute value
72      * gives the position of the bytecode instruction.
73      */

74     private int[] srcAndRefPositions;
75
76     /*
77      * Fields for the control flow graph analysis algorithm (used to compute the
78      * maximum stack size). A control flow graph contains one node per "basic
79      * block", and one edge per "jump" from one basic block to another. Each
80      * node (i.e., each basic block) is represented by the Label object that
81      * corresponds to the first instruction of this basic block. Each node also
82      * stores the list of it successors in the graph, as a linked list of Edge
83      * objects.
84      */
/**
85          * The stack size at the beginning of this basic block. This size is
86          * initially unknown. It is computed by the control flow analysis
87          * algorithm (see {@link MethodWriter#visitMaxs visitMaxs}).
88          */

89     int beginStackSize;
90
91     /**
92      * The (relative) maximum stack size corresponding to this basic block. This
93      * size is relative to the stack size at the beginning of the basic block,
94      * i.e., the true maximum stack size is equal to {@link #beginStackSize
95      * beginStackSize} + {@link #maxStackSize maxStackSize}.
96      */

97     int maxStackSize;
98
99     /**
100      * The successors of this node in the control flow graph. These successors
101      * are stored in a linked list of {@link Edge Edge} objects, linked to each
102      * other by their {@link Edge#next} field.
103      */

104     Edge successors;
105
106     /**
107      * The next basic block in the basic block stack. See
108      * {@link MethodWriter#visitMaxs visitMaxs}.
109      */

110     Label next;
111
112     /**
113      * <tt>true</tt> if this basic block has been pushed in the basic block
114      * stack. See {@link MethodWriter#visitMaxs visitMaxs}.
115      */

116     boolean pushed;
117
118     // ------------------------------------------------------------------------
119
// Constructor
120
// ------------------------------------------------------------------------
121

122     /**
123      * Constructs a new label.
124      */

125     public Label() {
126     }
127
128     // ------------------------------------------------------------------------
129
// Methods to compute offsets and to manage forward references
130
// ------------------------------------------------------------------------
131

132     /**
133      * Returns the offset corresponding to this label. This offset is computed
134      * from the start of the method's bytecode. <i>This method is intended for
135      * {@link Attribute} sub classes, and is normally not needed by class
136      * generators or adapters.</i>
137      *
138      * @return the offset corresponding to this label.
139      * @throws IllegalStateException if this label is not resolved yet.
140      */

141     public int getOffset() {
142         if (!resolved) {
143             throw new IllegalStateException JavaDoc("Label offset position has not been resolved yet");
144         }
145         return position;
146     }
147
148     /**
149      * Puts a reference to this label in the bytecode of a method. If the
150      * position of the label is known, the offset is computed and written
151      * directly. Otherwise, a null offset is written and a new forward reference
152      * is declared for this label.
153      *
154      * @param owner the code writer that calls this method.
155      * @param out the bytecode of the method.
156      * @param source the position of first byte of the bytecode instruction that
157      * contains this label.
158      * @param wideOffset <tt>true</tt> if the reference must be stored in 4
159      * bytes, or <tt>false</tt> if it must be stored with 2 bytes.
160      * @throws IllegalArgumentException if this label has not been created by
161      * the given code writer.
162      */

163     void put(
164         final MethodWriter owner,
165         final ByteVector out,
166         final int source,
167         final boolean wideOffset)
168     {
169         if (resolved) {
170             if (wideOffset) {
171                 out.putInt(position - source);
172             } else {
173                 out.putShort(position - source);
174             }
175         } else {
176             if (wideOffset) {
177                 addReference(-1 - source, out.length);
178                 out.putInt(-1);
179             } else {
180                 addReference(source, out.length);
181                 out.putShort(-1);
182             }
183         }
184     }
185
186     /**
187      * Adds a forward reference to this label. This method must be called only
188      * for a true forward reference, i.e. only if this label is not resolved
189      * yet. For backward references, the offset of the reference can be, and
190      * must be, computed and stored directly.
191      *
192      * @param sourcePosition the position of the referencing instruction. This
193      * position will be used to compute the offset of this forward
194      * reference.
195      * @param referencePosition the position where the offset for this forward
196      * reference must be stored.
197      */

198     private void addReference(
199         final int sourcePosition,
200         final int referencePosition)
201     {
202         if (srcAndRefPositions == null) {
203             srcAndRefPositions = new int[6];
204         }
205         if (referenceCount >= srcAndRefPositions.length) {
206             int[] a = new int[srcAndRefPositions.length + 6];
207             System.arraycopy(srcAndRefPositions,
208                     0,
209                     a,
210                     0,
211                     srcAndRefPositions.length);
212             srcAndRefPositions = a;
213         }
214         srcAndRefPositions[referenceCount++] = sourcePosition;
215         srcAndRefPositions[referenceCount++] = referencePosition;
216     }
217
218     /**
219      * Resolves all forward references to this label. This method must be called
220      * when this label is added to the bytecode of the method, i.e. when its
221      * position becomes known. This method fills in the blanks that where left
222      * in the bytecode by each forward reference previously added to this label.
223      *
224      * @param owner the code writer that calls this method.
225      * @param position the position of this label in the bytecode.
226      * @param data the bytecode of the method.
227      * @return <tt>true</tt> if a blank that was left for this label was to
228      * small to store the offset. In such a case the corresponding jump
229      * instruction is replaced with a pseudo instruction (using unused
230      * opcodes) using an unsigned two bytes offset. These pseudo
231      * instructions will need to be replaced with true instructions with
232      * wider offsets (4 bytes instead of 2). This is done in
233      * {@link MethodWriter#resizeInstructions}.
234      * @throws IllegalArgumentException if this label has already been resolved,
235      * or if it has not been created by the given code writer.
236      */

237     boolean resolve(
238         final MethodWriter owner,
239         final int position,
240         final byte[] data)
241     {
242         boolean needUpdate = false;
243         this.resolved = true;
244         this.position = position;
245         int i = 0;
246         while (i < referenceCount) {
247             int source = srcAndRefPositions[i++];
248             int reference = srcAndRefPositions[i++];
249             int offset;
250             if (source >= 0) {
251                 offset = position - source;
252                 if (offset < Short.MIN_VALUE || offset > Short.MAX_VALUE) {
253                     /*
254                      * changes the opcode of the jump instruction, in order to
255                      * be able to find it later (see resizeInstructions in
256                      * MethodWriter). These temporary opcodes are similar to
257                      * jump instruction opcodes, except that the 2 bytes offset
258                      * is unsigned (and can therefore represent values from 0 to
259                      * 65535, which is sufficient since the size of a method is
260                      * limited to 65535 bytes).
261                      */

262                     int opcode = data[reference - 1] & 0xFF;
263                     if (opcode <= Opcodes.JSR) {
264                         // changes IFEQ ... JSR to opcodes 202 to 217
265
data[reference - 1] = (byte) (opcode + 49);
266                     } else {
267                         // changes IFNULL and IFNONNULL to opcodes 218 and 219
268
data[reference - 1] = (byte) (opcode + 20);
269                     }
270                     needUpdate = true;
271                 }
272                 data[reference++] = (byte) (offset >>> 8);
273                 data[reference] = (byte) offset;
274             } else {
275                 offset = position + source + 1;
276                 data[reference++] = (byte) (offset >>> 24);
277                 data[reference++] = (byte) (offset >>> 16);
278                 data[reference++] = (byte) (offset >>> 8);
279                 data[reference] = (byte) offset;
280             }
281         }
282         return needUpdate;
283     }
284
285     // ------------------------------------------------------------------------
286
// Overriden Object methods
287
// ------------------------------------------------------------------------
288

289     /**
290      * Returns a string representation of this label.
291      *
292      * @return a string representation of this label.
293      */

294     public String JavaDoc toString() {
295         return "L" + System.identityHashCode(this);
296     }
297 }
298
Popular Tags