PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/alibaba/fastjson/asm/Label.java

https://bitbucket.org/xiejuntao/xdesktop
Java | 248 lines | 68 code | 21 blank | 159 comment | 13 complexity | c19b6bfa19a480be57d122db7ffe5e02 MD5 | raw file
  1. /***
  2. * ASM: a very small and fast Java bytecode manipulation framework
  3. * Copyright (c) 2000-2007 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 com.alibaba.fastjson.asm;
  31. /**
  32. * A label represents a position in the bytecode of a method. Labels are used for jump, goto, and switch instructions,
  33. * and for try catch blocks. A label designates the <i>instruction</i> that is just after. Note however that there can
  34. * be other elements between a label and the instruction it designates (such as other labels, stack map frames, line
  35. * numbers, etc.).
  36. *
  37. * @author Eric Bruneton
  38. */
  39. public class Label {
  40. /**
  41. * Indicates if the position of this label is known.
  42. */
  43. static final int RESOLVED = 2;
  44. /**
  45. * Field used to associate user information to a label. Warning: this field is used by the ASM tree package. In
  46. * order to use it with the ASM tree package you must override the
  47. * {@link com.alibaba.fastjson.asm.tree.MethodNode#getLabelNode} method.
  48. */
  49. public Object info;
  50. int status;
  51. /**
  52. * The line number corresponding to this label, if known.
  53. */
  54. int line;
  55. /**
  56. * The position of this label in the code, if known.
  57. */
  58. int position;
  59. /**
  60. * Number of forward references to this label, times two.
  61. */
  62. private int referenceCount;
  63. /**
  64. * Informations about forward references. Each forward reference is described by two consecutive integers in this
  65. * array: the first one is the position of the first byte of the bytecode instruction that contains the forward
  66. * reference, while the second is the position of the first byte of the forward reference itself. In fact the sign
  67. * of the first integer indicates if this reference uses 2 or 4 bytes, and its absolute value gives the position of
  68. * the bytecode instruction. This array is also used as a bitset to store the subroutines to which a basic block
  69. * belongs. This information is needed in {@linked MethodWriter#visitMaxs}, after all forward references have been
  70. * resolved. Hence the same array can be used for both purposes without problems.
  71. */
  72. private int[] srcAndRefPositions;
  73. // ------------------------------------------------------------------------
  74. /*
  75. * Fields for the control flow and data flow graph analysis algorithms (used to compute the maximum stack size or
  76. * the stack map frames). A control flow graph contains one node per "basic block", and one edge per "jump" from one
  77. * basic block to another. Each node (i.e., each basic block) is represented by the Label object that corresponds to
  78. * the first instruction of this basic block. Each node also stores the list of its successors in the graph, as a
  79. * linked list of Edge objects. The control flow analysis algorithms used to compute the maximum stack size or the
  80. * stack map frames are similar and use two steps. The first step, during the visit of each instruction, builds
  81. * information about the state of the local variables and the operand stack at the end of each basic block, called
  82. * the "output frame", <i>relatively</i> to the frame state at the beginning of the basic block, which is called the
  83. * "input frame", and which is <i>unknown</i> during this step. The second step, in {@link MethodWriter#visitMaxs},
  84. * is a fix point algorithm that computes information about the input frame of each basic block, from the input
  85. * state of the first basic block (known from the method signature), and by the using the previously computed
  86. * relative output frames. The algorithm used to compute the maximum stack size only computes the relative output
  87. * and absolute input stack heights, while the algorithm used to compute stack map frames computes relative output
  88. * frames and absolute input frames.
  89. */
  90. /**
  91. * Start of the output stack relatively to the input stack. The exact semantics of this field depends on the
  92. * algorithm that is used. When only the maximum stack size is computed, this field is the number of elements in the
  93. * input stack. When the stack map frames are completely computed, this field is the offset of the first output
  94. * stack element relatively to the top of the input stack. This offset is always negative or null. A null offset
  95. * means that the output stack must be appended to the input stack. A -n offset means that the first n output stack
  96. * elements must replace the top n input stack elements, and that the other elements must be appended to the input
  97. * stack.
  98. */
  99. int inputStackTop;
  100. /**
  101. * Maximum height reached by the output stack, relatively to the top of the input stack. This maximum is always
  102. * positive or null.
  103. */
  104. int outputStackMax;
  105. /**
  106. * The successor of this label, in the order they are visited. This linked list does not include labels used for
  107. * debug info only. If {@link ClassWriter#COMPUTE_FRAMES} option is used then, in addition, it does not contain
  108. * successive labels that denote the same bytecode position (in this case only the first label appears in this
  109. * list).
  110. */
  111. Label successor;
  112. /**
  113. * The next basic block in the basic block stack. This stack is used in the main loop of the fix point algorithm
  114. * used in the second step of the control flow analysis algorithms. It is also used in {@link #visitSubroutine} to
  115. * avoid using a recursive method.
  116. *
  117. * @see MethodWriter#visitMaxs
  118. */
  119. Label next;
  120. // ------------------------------------------------------------------------
  121. // Constructor
  122. // ------------------------------------------------------------------------
  123. /**
  124. * Constructs a new label.
  125. */
  126. public Label(){
  127. }
  128. // ------------------------------------------------------------------------
  129. // Methods to compute offsets and to manage forward references
  130. // ------------------------------------------------------------------------
  131. /**
  132. * Puts a reference to this label in the bytecode of a method. If the position of the label is known, the offset is
  133. * computed and written directly. Otherwise, a null offset is written and a new forward reference is declared for
  134. * this label.
  135. *
  136. * @param owner the code writer that calls this method.
  137. * @param out the bytecode of the method.
  138. * @param source the position of first byte of the bytecode instruction that contains this label.
  139. * @param wideOffset <tt>true</tt> if the reference must be stored in 4 bytes, or <tt>false</tt> if it must be
  140. * stored with 2 bytes.
  141. * @throws IllegalArgumentException if this label has not been created by the given code writer.
  142. */
  143. void put(final MethodWriter owner, final ByteVector out, final int source) {
  144. if ((status & RESOLVED) == 0) {
  145. addReference(source, out.length);
  146. out.putShort(-1);
  147. } else {
  148. out.putShort(position - source);
  149. }
  150. }
  151. /**
  152. * Adds a forward reference to this label. This method must be called only for a true forward reference, i.e. only
  153. * if this label is not resolved yet. For backward references, the offset of the reference can be, and must be,
  154. * computed and stored directly.
  155. *
  156. * @param sourcePosition the position of the referencing instruction. This position will be used to compute the
  157. * offset of this forward reference.
  158. * @param referencePosition the position where the offset for this forward reference must be stored.
  159. */
  160. private void addReference(final int sourcePosition, final int referencePosition) {
  161. if (srcAndRefPositions == null) {
  162. srcAndRefPositions = new int[6];
  163. }
  164. if (referenceCount >= srcAndRefPositions.length) {
  165. int[] a = new int[srcAndRefPositions.length + 6];
  166. System.arraycopy(srcAndRefPositions, 0, a, 0, srcAndRefPositions.length);
  167. srcAndRefPositions = a;
  168. }
  169. srcAndRefPositions[referenceCount++] = sourcePosition;
  170. srcAndRefPositions[referenceCount++] = referencePosition;
  171. }
  172. /**
  173. * Resolves all forward references to this label. This method must be called when this label is added to the
  174. * bytecode of the method, i.e. when its position becomes known. This method fills in the blanks that where left in
  175. * the bytecode by each forward reference previously added to this label.
  176. *
  177. * @param owner the code writer that calls this method.
  178. * @param position the position of this label in the bytecode.
  179. * @param data the bytecode of the method.
  180. * @return <tt>true</tt> if a blank that was left for this label was to small to store the offset. In such a case
  181. * the corresponding jump instruction is replaced with a pseudo instruction (using unused opcodes) using an unsigned
  182. * two bytes offset. These pseudo instructions will need to be replaced with true instructions with wider offsets (4
  183. * bytes instead of 2). This is done in {@link MethodWriter#resizeInstructions}.
  184. * @throws IllegalArgumentException if this label has already been resolved, or if it has not been created by the
  185. * given code writer.
  186. */
  187. boolean resolve(final MethodWriter owner, final int position, final byte[] data) {
  188. boolean needUpdate = false;
  189. this.status |= RESOLVED;
  190. this.position = position;
  191. int i = 0;
  192. while (i < referenceCount) {
  193. int source = srcAndRefPositions[i++];
  194. int reference = srcAndRefPositions[i++];
  195. int offset;
  196. if (source >= 0) {
  197. offset = position - source;
  198. if (offset < Short.MIN_VALUE || offset > Short.MAX_VALUE) {
  199. /*
  200. * changes the opcode of the jump instruction, in order to be able to find it later (see
  201. * resizeInstructions in MethodWriter). These temporary opcodes are similar to jump instruction
  202. * opcodes, except that the 2 bytes offset is unsigned (and can therefore represent values from 0 to
  203. * 65535, which is sufficient since the size of a method is limited to 65535 bytes).
  204. */
  205. int opcode = data[reference - 1] & 0xFF;
  206. if (opcode <= Opcodes.JSR) {
  207. // changes IFEQ ... JSR to opcodes 202 to 217
  208. data[reference - 1] = (byte) (opcode + 49);
  209. } else {
  210. // changes IFNULL and IFNONNULL to opcodes 218 and 219
  211. data[reference - 1] = (byte) (opcode + 20);
  212. }
  213. needUpdate = true;
  214. }
  215. data[reference++] = (byte) (offset >>> 8);
  216. data[reference] = (byte) offset;
  217. } else {
  218. offset = position + source + 1;
  219. data[reference++] = (byte) (offset >>> 24);
  220. data[reference++] = (byte) (offset >>> 16);
  221. data[reference++] = (byte) (offset >>> 8);
  222. data[reference] = (byte) offset;
  223. }
  224. }
  225. return needUpdate;
  226. }
  227. }