You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InstructionBranch.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* ====================================================================
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2001 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution,
  20. * if any, must include the following acknowledgment:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (https://www.apache.org/)."
  23. * Alternately, this acknowledgment may appear in the software itself,
  24. * if and wherever such third-party acknowledgments normally appear.
  25. *
  26. * 4. The names "Apache" and "Apache Software Foundation" and
  27. * "Apache BCEL" must not be used to endorse or promote products
  28. * derived from this software without prior written permission. For
  29. * written permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache",
  32. * "Apache BCEL", nor may "Apache" appear in their name, without
  33. * prior written permission of the Apache Software Foundation.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <https://www.apache.org/>.
  53. */
  54. package org.aspectj.apache.bcel.generic;
  55. import java.io.DataOutputStream;
  56. import java.io.IOException;
  57. import org.aspectj.apache.bcel.Constants;
  58. import org.aspectj.apache.bcel.classfile.ConstantPool;
  59. /**
  60. * Abstract super class for branching instructions like GOTO, IFEQ, etc.. Branch instructions may have a variable length, namely
  61. * GOTO, JSR, LOOKUPSWITCH and TABLESWITCH. A branch instruction may be talking in terms of absolute destination (targetIndex) or
  62. * about an instruction it doesnt yet know the position if (targetInstruction). targetInstruction (if set) overrides targetIndex
  63. *
  64. * @see InstructionList
  65. * @version $Id: InstructionBranch.java,v 1.6 2009/10/05 17:35:36 aclement Exp $
  66. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  67. */
  68. public class InstructionBranch extends Instruction implements InstructionTargeter {
  69. private static final int UNSET = -1;
  70. protected int targetIndex = UNSET; // Branch target relative to this
  71. // instruction
  72. protected InstructionHandle targetInstruction; // Target object in
  73. // instruction list
  74. protected int positionOfThisInstruction; // for calculating relative branch
  75. // destinations!
  76. public InstructionBranch(short opcode, InstructionHandle target) {
  77. super(opcode);
  78. setTarget(target);
  79. }
  80. public InstructionBranch(short opcode, int index) {
  81. super(opcode);
  82. this.targetIndex = index;
  83. }
  84. public InstructionBranch(short opcode) {
  85. super(opcode);
  86. }
  87. public void dump(DataOutputStream out) throws IOException {
  88. int target = getTargetOffset();
  89. if (Math.abs(target) >= 32767 && opcode != GOTO_W && opcode != JSR_W) {
  90. throw new ClassGenException("Branch target offset too large for short. Instruction: " + getName().toUpperCase() + "("
  91. + opcode + ")");
  92. }
  93. out.writeByte(opcode);
  94. switch (opcode) {
  95. case GOTO_W:
  96. case JSR_W:
  97. out.writeInt(target);
  98. break;
  99. case IF_ACMPEQ:
  100. case IF_ACMPNE:
  101. case IF_ICMPEQ:
  102. case IF_ICMPGE:
  103. case IF_ICMPGT:
  104. case IF_ICMPLE:
  105. case IF_ICMPLT:
  106. case IF_ICMPNE:
  107. case IFEQ:
  108. case IFLE:
  109. case IFLT:
  110. case IFGT:
  111. case IFNE:
  112. case IFGE:
  113. case IFNULL:
  114. case IFNONNULL:
  115. case GOTO:
  116. case JSR:
  117. out.writeShort(target);
  118. break;
  119. default:
  120. throw new IllegalStateException("Don't know how to write out " + getName().toUpperCase());
  121. }
  122. }
  123. protected int getTargetOffset() {
  124. if (targetInstruction == null && targetIndex == UNSET) {
  125. throw new ClassGenException("Target of " + super.toString(true) + " is unknown");
  126. }
  127. if (targetInstruction == null) {
  128. return targetIndex;
  129. } else {
  130. return targetInstruction.getPosition() - positionOfThisInstruction;
  131. }
  132. }
  133. /**
  134. * Called by InstructionList.setPositions when setting the position for every instruction. In the presence of variable length
  135. * instructions `setPositions' performs multiple passes over the instruction list to calculate the correct (byte) positions and
  136. * offsets by calling this function.
  137. *
  138. * @param offset additional offset caused by preceding (variable length) instructions
  139. * @param max_offset the maximum offset that may be caused by these instructions
  140. * @return additional offset caused by possible change of this instruction's length
  141. */
  142. protected int updatePosition(int offset, int max_offset) {
  143. int i = getTargetOffset();
  144. positionOfThisInstruction += offset;
  145. if (Math.abs(i) >= 32767 - max_offset && opcode != JSR_W && opcode != GOTO_W) {
  146. // Try and promote it to wide if we can
  147. if (opcode == JSR || opcode == GOTO) {
  148. if (opcode == JSR) {
  149. opcode = JSR_W;
  150. } else {
  151. opcode = GOTO_W;
  152. }
  153. return 2; // instruction jump destination grows from a short to a long
  154. } else {
  155. throw new IllegalStateException("Unable to pack method, jump (with opcode=" + opcode + ") is too far: "
  156. + Math.abs(i));
  157. }
  158. }
  159. return 0;
  160. }
  161. /**
  162. * Long output format:
  163. *
  164. * @param verbose long/short format switch
  165. * @return mnemonic for instruction
  166. */
  167. public String toString(boolean verbose) {
  168. String s = super.toString(verbose);
  169. String t = "null";
  170. if (verbose) {
  171. if (targetInstruction != null) {
  172. if (targetInstruction.getInstruction() == this) {
  173. t = "<points to itself>";
  174. } else if (targetInstruction.getInstruction() == null) {
  175. t = "<null destination>";
  176. } else {
  177. t = targetInstruction.getInstruction().toString(false);
  178. }
  179. }
  180. } else {
  181. if (targetInstruction != null) {
  182. targetIndex = getTargetOffset();
  183. t = "" + (targetIndex + positionOfThisInstruction);
  184. }
  185. }
  186. return s + " -> " + t;
  187. }
  188. /**
  189. * @return target offset in byte code
  190. */
  191. public final int getIndex() {
  192. return targetIndex;
  193. }
  194. /**
  195. * @return target of branch instruction
  196. */
  197. public InstructionHandle getTarget() {
  198. return targetInstruction;
  199. }
  200. /**
  201. * Set branch target
  202. *
  203. * @param target branch target
  204. */
  205. public void setTarget(InstructionHandle target) {
  206. notifyTarget(this.targetInstruction, target, this);
  207. this.targetInstruction = target;
  208. }
  209. /**
  210. * Used by BranchInstruction, LocalVariableGen, CodeExceptionGen
  211. */
  212. static final void notifyTarget(InstructionHandle oldHandle, InstructionHandle newHandle, InstructionTargeter t) {
  213. if (oldHandle != null) {
  214. oldHandle.removeTargeter(t);
  215. }
  216. if (newHandle != null) {
  217. newHandle.addTargeter(t);
  218. }
  219. }
  220. /**
  221. * Update the target destination for this instruction. If an oldHandle is provided it is checked to verify that is where the
  222. * target currently points to before changing it.
  223. *
  224. * @param oldHandle old target
  225. * @param newHandle new target
  226. */
  227. public void updateTarget(InstructionHandle oldHandle, InstructionHandle newHandle) {
  228. if (targetInstruction == oldHandle) {
  229. setTarget(newHandle);
  230. } else {
  231. throw new ClassGenException("Not targeting " + oldHandle + ", but " + targetInstruction);
  232. }
  233. }
  234. /**
  235. * @return true, if ih is target of this instruction
  236. */
  237. public boolean containsTarget(InstructionHandle ih) {
  238. return targetInstruction == ih;
  239. }
  240. /**
  241. * Inform target that it's not targeted anymore.
  242. */
  243. void dispose() {
  244. setTarget(null);
  245. targetIndex = -1;
  246. positionOfThisInstruction = -1;
  247. }
  248. public Type getType(ConstantPool cp) {
  249. if ((Constants.instFlags[opcode] & Constants.JSR_INSTRUCTION) != 0) {
  250. return new ReturnaddressType(physicalSuccessor());
  251. }
  252. return super.getType(cp);
  253. }
  254. /**
  255. * Returns an InstructionHandle to the physical successor of this JsrInstruction. <B>For this method to work, this
  256. * JsrInstruction object must not be shared between multiple InstructionHandle objects!</B> Formally, there must not be
  257. * InstructionHandle objects i, j where i != j and i.getInstruction() == this == j.getInstruction().
  258. *
  259. * @return an InstructionHandle to the "next" instruction that will be executed when RETurned from a subroutine.
  260. */
  261. public InstructionHandle physicalSuccessor() {
  262. InstructionHandle ih = this.targetInstruction;
  263. // Rewind!
  264. while (ih.getPrev() != null) {
  265. ih = ih.getPrev();
  266. }
  267. // Find the handle for "this" JsrInstruction object.
  268. while (ih.getInstruction() != this) {
  269. ih = ih.getNext();
  270. }
  271. InstructionHandle toThis = ih;
  272. while (ih != null) {
  273. ih = ih.getNext();
  274. if (ih != null && ih.getInstruction() == this) {
  275. throw new RuntimeException("physicalSuccessor() called on a shared JsrInstruction.");
  276. }
  277. }
  278. // Return the physical successor
  279. return toThis.getNext();
  280. }
  281. public boolean isIfInstruction() {
  282. return (Constants.instFlags[opcode] & Constants.IF_INST) != 0;
  283. }
  284. /**
  285. * Only equal if they are the same branch instruction - otherwise too risky as the targets may only temporarily be pointing at
  286. * the same destination.
  287. */
  288. public boolean equals(Object other) {
  289. return this == other;
  290. }
  291. public int hashCode() {
  292. int result = 17;
  293. result = opcode * 37 + result;
  294. return result;
  295. }
  296. }