PageRenderTime 18ms CodeModel.GetById 1ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/objectweb/asm/Label.java

#
Java | 279 lines | 103 code | 34 blank | 142 comment | 28 complexity | 9dbe18e31b379f4123a55ac4454cf23c MD5 | raw file
  1/***
  2 * ASM: a very small and fast Java bytecode manipulation framework
  3 * Copyright (C) 2000 INRIA, France Telecom
  4 * Copyright (C) 2002 France Telecom
  5 *
  6 * This library is free software; you can redistribute it and/or
  7 * modify it under the terms of the GNU Lesser General Public
  8 * License as published by the Free Software Foundation; either
  9 * version 2 of the License, or (at your option) any later version.
 10 *
 11 * This library is distributed in the hope that it will be useful,
 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14 * Lesser General Public License for more details.
 15 *
 16 * You should have received a copy of the GNU Lesser General Public
 17 * License along with this library; if not, write to the Free Software
 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 19 *
 20 * Contact: Eric.Bruneton@rd.francetelecom.com
 21 *
 22 * Author: Eric Bruneton
 23 */
 24
 25package org.objectweb.asm;
 26
 27/**
 28 * A label represents a position in the bytecode of a method. Labels are used
 29 * for jump, goto, and switch instructions, and for try catch blocks.
 30 */
 31
 32public class Label {
 33
 34  /**
 35   * The code writer to which this label belongs, or <tt>null</tt> if unknown.
 36   */
 37
 38  CodeWriter owner;
 39
 40  /**
 41   * Indicates if the position of this label is known.
 42   */
 43
 44  boolean resolved;
 45
 46  /**
 47   * The position of this label in the code, if known.
 48   */
 49
 50  int position;
 51
 52  /**
 53   * Number of forward references to this label, times two.
 54   */
 55
 56  private int referenceCount;
 57
 58  /**
 59   * Informations about forward references. Each forward reference is described
 60   * by two consecutive integers in this array: the first one is the position
 61   * of the first byte of the bytecode instruction that contains the forward
 62   * reference, while the second is the position of the first byte of the
 63   * forward reference itself. In fact the sign of the first integer indicates
 64   * if this reference uses 2 or 4 bytes, and its absolute value gives the
 65   * position of the bytecode instruction.
 66   */
 67
 68  private int[] srcAndRefPositions;
 69
 70  // --------------------------------------------------------------------------
 71  // Fields for the control flow graph analysis algorithm (used to compute the
 72  // maximum stack size). A control flow graph contains one node per "basic
 73  // block", and one edge per "jump" from one basic block to another. Each node
 74  // (i.e., each basic block) is represented by the Label object that
 75  // corresponds to the first instruction of this basic block. Each node also
 76  // stores the list of it successors in the graph, as a linked list of Edge
 77  // objects.
 78  // --------------------------------------------------------------------------
 79
 80  /**
 81   * The stack size at the beginning of this basic block.
 82   * This size is initially unknown. It is computed by the control flow
 83   * analysis algorithm (see {@link CodeWriter#visitMaxs visitMaxs}).
 84   */
 85
 86  int beginStackSize;
 87
 88  /**
 89   * The (relative) maximum stack size corresponding to this basic block. This
 90   * size is relative to the stack size at the beginning of the basic block,
 91   * i.e., the true maximum stack size is equal to {@link #beginStackSize
 92   * beginStackSize} + {@link #maxStackSize maxStackSize}.
 93   */
 94
 95  int maxStackSize;
 96
 97  /**
 98   * The successors of this node in the control flow graph. These successors
 99   * are stored in a linked list of {@link Edge Edge} objects, linked to each
100   * other by their {@link Edge#next} field.
101   */
102
103  Edge successors;
104
105  /**
106   * The next basic block in the basic block stack.
107   * See {@link CodeWriter#visitMaxs visitMaxs}.
108   */
109
110  Label next;
111
112  /**
113   * <tt>true</tt> if this basic block has been pushed in the basic block stack.
114   * See {@link CodeWriter#visitMaxs visitMaxs}.
115   */
116
117  boolean pushed;
118
119  // --------------------------------------------------------------------------
120  // Constructor
121  // --------------------------------------------------------------------------
122
123  /**
124   * Constructs a new label.
125   */
126
127  public Label () {
128  }
129
130  // --------------------------------------------------------------------------
131  // Methods to compute offsets and to manage forward references
132  // --------------------------------------------------------------------------
133
134  /**
135   * Puts a reference to this label in the bytecode of a method. If the position
136   * of the label is known, the offset is computed and written directly.
137   * Otherwise, a null offset is written and a new forward reference is declared
138   * for this label.
139   *
140   * @param owner the code writer that calls this method.
141   * @param out the bytecode of the method.
142   * @param source the position of first byte of the bytecode instruction that
143   *      contains this label.
144   * @param wideOffset <tt>true</tt> if the reference must be stored in 4 bytes,
145   *      or <tt>false</tt> if it must be stored with 2 bytes.
146   * @throws IllegalArgumentException if this label has not been created by the
147   *      given code writer.
148   */
149
150  void put (
151    final CodeWriter owner,
152    final ByteVector out,
153    final int source,
154    final boolean wideOffset)
155  {
156    if (CodeWriter.CHECK) {
157      if (this.owner == null) {
158        this.owner = owner;
159      } else if (this.owner != owner) {
160        throw new IllegalArgumentException();
161      }
162    }
163    if (resolved) {
164      if (wideOffset) {
165        out.put4(position - source);
166      } else {
167        out.put2(position - source);
168      }
169    } else {
170      if (wideOffset) {
171        addReference(-1 - source, out.length);
172        out.put4(-1);
173      } else {
174        addReference(source, out.length);
175        out.put2(-1);
176      }
177    }
178  }
179
180  /**
181   * Adds a forward reference to this label. This method must be called only for
182   * a true forward reference, i.e. only if this label is not resolved yet. For
183   * backward references, the offset of the reference can be, and must be,
184   * computed and stored directly.
185   *
186   * @param sourcePosition the position of the referencing instruction. This
187   *      position will be used to compute the offset of this forward reference.
188   * @param referencePosition the position where the offset for this forward
189   *      reference must be stored.
190   */
191
192  private void addReference (
193    final int sourcePosition,
194    final int referencePosition)
195  {
196    if (srcAndRefPositions == null) {
197      srcAndRefPositions = new int[6];
198    }
199    if (referenceCount >= srcAndRefPositions.length) {
200      int[] a = new int[srcAndRefPositions.length + 6];
201      System.arraycopy(srcAndRefPositions, 0, a, 0, srcAndRefPositions.length);
202      srcAndRefPositions = a;
203    }
204    srcAndRefPositions[referenceCount++] = sourcePosition;
205    srcAndRefPositions[referenceCount++] = referencePosition;
206  }
207
208  /**
209   * Resolves all forward references to this label. This method must be called
210   * when this label is added to the bytecode of the method, i.e. when its
211   * position becomes known. This method fills in the blanks that where left in
212   * the bytecode by each forward reference previously added to this label.
213   *
214   * @param owner the code writer that calls this method.
215   * @param position the position of this label in the bytecode.
216   * @param data the bytecode of the method.
217   * @return <tt>true</tt> if a blank that was left for this label was to small
218   *      to store the offset. In such a case the corresponding jump instruction
219   *      is replaced with a pseudo instruction (using unused opcodes) using an
220   *      unsigned two bytes offset. These pseudo instructions will need to be
221   *      replaced with true instructions with wider offsets (4 bytes instead of
222   *      2). This is done in {@link CodeWriter#resizeInstructions}.
223   * @throws IllegalArgumentException if this label has already been resolved,
224   *      or if it has not been created by the given code writer.
225   */
226
227  boolean resolve (
228    final CodeWriter owner,
229    final int position,
230    final byte[] data)
231  {
232    if (CodeWriter.CHECK) {
233      if (this.owner == null) {
234        this.owner = owner;
235      }
236      if (resolved || this.owner != owner) {
237        throw new IllegalArgumentException();
238      }
239    }
240    boolean needUpdate = false;
241    this.resolved = true;
242    this.position = position;
243    int i = 0;
244    while (i < referenceCount) {
245      int source = srcAndRefPositions[i++];
246      int reference = srcAndRefPositions[i++];
247      int offset;
248      if (source >= 0) {
249        offset = position - source;
250        if (offset < Short.MIN_VALUE || offset > Short.MAX_VALUE) {
251          // changes the opcode of the jump instruction, in order to be able to
252          // find it later (see resizeInstructions in CodeWriter). These
253          // temporary opcodes are similar to jump instruction opcodes, except
254          // that the 2 bytes offset is unsigned (and can therefore represent
255          // values from 0 to 65535, which is sufficient since the size of a
256          // method is limited to 65535 bytes).
257          int opcode = data[reference - 1] & 0xFF;
258          if (opcode <= Constants.JSR) {
259            // changes IFEQ ... JSR to opcodes 202 to 217 (inclusive)
260            data[reference - 1] = (byte)(opcode + 49);
261          } else {
262            // changes IFNULL and IFNONNULL to opcodes 218 and 219 (inclusive)
263            data[reference - 1] = (byte)(opcode + 20);
264          }
265          needUpdate = true;
266        }
267        data[reference++] = (byte)(offset >>> 8);
268        data[reference] = (byte)offset;
269      } else {
270        offset = position + source + 1;
271        data[reference++] = (byte)(offset >>> 24);
272        data[reference++] = (byte)(offset >>> 16);
273        data[reference++] = (byte)(offset >>> 8);
274        data[reference] = (byte)offset;
275      }
276    }
277    return needUpdate;
278  }
279}