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.

InstructionSelect.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package org.aspectj.apache.bcel.generic;
  2. /* ====================================================================
  3. * The Apache Software License, Version 1.1
  4. *
  5. * Copyright (c) 2001 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Apache" and "Apache Software Foundation" and
  28. * "Apache BCEL" must not be used to endorse or promote products
  29. * derived from this software without prior written permission. For
  30. * written permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * "Apache BCEL", nor may "Apache" appear in their name, without
  34. * prior written permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation. For more
  52. * information on the Apache Software Foundation, please see
  53. * <http://www.apache.org/>.
  54. */
  55. import java.io.DataOutputStream;
  56. import java.io.IOException;
  57. import org.aspectj.apache.bcel.util.ByteSequence;
  58. /**
  59. * Select - Abstract super class for LOOKUPSWITCH and TABLESWITCH instructions.
  60. *
  61. * @version $Id: InstructionSelect.java,v 1.4 2009/10/05 17:35:36 aclement Exp $
  62. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  63. * @see LOOKUPSWITCH
  64. * @see TABLESWITCH
  65. * @see InstructionList
  66. */
  67. public abstract class InstructionSelect extends InstructionBranch {
  68. protected int[] match; // matches, i.e., case 1: ...
  69. protected int[] indices; // target offsets
  70. protected InstructionHandle[] targets; // target objects in instruction list
  71. protected int fixedLength; // fixed length defined by subclasses
  72. protected int matchLength; // number of cases
  73. protected int padding = 0; // number of pad bytes for alignment
  74. protected short length;
  75. /**
  76. * (Match, target) pairs for switch. `Match' and `targets' must have the same length of course.
  77. *
  78. * @param match array of matching values
  79. * @param targets instruction targets
  80. * @param target default instruction target
  81. */
  82. InstructionSelect(short opcode, int[] match, InstructionHandle[] targets, InstructionHandle target) {
  83. super(opcode, target);
  84. this.targets = targets;
  85. for (InstructionHandle instructionHandle : targets) {
  86. notifyTarget(null, instructionHandle, this);
  87. }
  88. this.match = match;
  89. if ((matchLength = match.length) != targets.length) {
  90. throw new ClassGenException("Match and target array have not the same length");
  91. }
  92. indices = new int[matchLength];
  93. }
  94. protected int getTargetOffset(InstructionHandle target) {
  95. if (target == null) {
  96. throw new ClassGenException("Target of " + super.toString(true) + " is invalid null handle");
  97. }
  98. int t = target.getPosition();
  99. if (t < 0) {
  100. throw new ClassGenException("Invalid branch target position offset for " + super.toString(true) + ":" + t + ":"
  101. + target);
  102. }
  103. return t - positionOfThisInstruction;
  104. }
  105. /**
  106. * Since this is a variable length instruction, it may shift the following instructions which then need to update their
  107. * position.
  108. *
  109. * Called by InstructionList.setPositions when setting the position for every instruction. In the presence of variable length
  110. * instructions `setPositions' performs multiple passes over the instruction list to calculate the correct (byte) positions and
  111. * offsets by calling this function.
  112. *
  113. * @param offset additional offset caused by preceding (variable length) instructions
  114. * @param max_offset the maximum offset that may be caused by these instructions
  115. * @return additional offset caused by possible change of this instruction's length
  116. */
  117. protected int updatePosition(int offset, int max_offset) {
  118. positionOfThisInstruction += offset; // Additional offset caused by
  119. // preceding SWITCHs, GOTOs,
  120. // etc.
  121. short old_length = length;
  122. /*
  123. * Alignment on 4-byte-boundary, + 1, because of tag byte.
  124. */
  125. padding = (4 - (positionOfThisInstruction + 1) % 4) % 4;
  126. length = (short) (fixedLength + padding); // Update length
  127. return length - old_length;
  128. }
  129. /**
  130. * Dump instruction as byte code to stream out.
  131. *
  132. * @param out Output stream
  133. */
  134. public void dump(DataOutputStream out) throws IOException {
  135. out.writeByte(opcode);
  136. for (int i = 0; i < padding; i++) {
  137. out.writeByte(0);
  138. }
  139. targetIndex = getTargetOffset(); // Write default target offset
  140. out.writeInt(targetIndex);
  141. }
  142. public InstructionSelect(short opcode, ByteSequence bytes) throws IOException {
  143. super(opcode);
  144. padding = (4 - bytes.getIndex() % 4) % 4; // Compute number of pad bytes
  145. for (int i = 0; i < padding; i++) {
  146. bytes.readByte();
  147. }
  148. // Default branch target common for both cases (TABLESWITCH,
  149. // LOOKUPSWITCH)
  150. targetIndex = bytes.readInt();
  151. }
  152. /**
  153. * @return mnemonic for instruction
  154. */
  155. public String toString(boolean verbose) {
  156. StringBuilder buf = new StringBuilder(super.toString(verbose));
  157. if (verbose) {
  158. for (int i = 0; i < matchLength; i++) {
  159. String s = "null";
  160. if (targets[i] != null) {
  161. s = targets[i].getInstruction().toString();
  162. }
  163. buf.append("(" + match[i] + ", " + s + " = {" + indices[i] + "})");
  164. }
  165. } else {
  166. buf.append(" ...");
  167. }
  168. return buf.toString();
  169. }
  170. /**
  171. * Set branch target for `i'th case
  172. */
  173. public void setTarget(int i, InstructionHandle target) {
  174. notifyTarget(targets[i], target, this);
  175. targets[i] = target;
  176. }
  177. /**
  178. * @param old_ih old target
  179. * @param new_ih new target
  180. */
  181. public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
  182. boolean targeted = false;
  183. if (targetInstruction == old_ih) {
  184. targeted = true;
  185. setTarget(new_ih);
  186. }
  187. for (int i = 0; i < targets.length; i++) {
  188. if (targets[i] == old_ih) {
  189. targeted = true;
  190. setTarget(i, new_ih);
  191. }
  192. }
  193. if (!targeted) {
  194. throw new ClassGenException("Not targeting " + old_ih);
  195. }
  196. }
  197. /**
  198. * @return true, if ih is target of this instruction
  199. */
  200. public boolean containsTarget(InstructionHandle ih) {
  201. if (targetInstruction == ih) {
  202. return true;
  203. }
  204. for (InstructionHandle target : targets) {
  205. if (target == ih) {
  206. return true;
  207. }
  208. }
  209. return false;
  210. }
  211. /**
  212. * Inform targets that they're not targeted anymore.
  213. */
  214. void dispose() {
  215. super.dispose();
  216. for (InstructionHandle target : targets) {
  217. target.removeTargeter(this);
  218. }
  219. }
  220. /**
  221. * @return array of match indices
  222. */
  223. public int[] getMatchs() {
  224. return match;
  225. }
  226. /**
  227. * @return array of match target offsets
  228. */
  229. public int[] getIndices() {
  230. return indices;
  231. }
  232. public boolean equals(Object other) {
  233. return this == other;
  234. }
  235. public int hashCode() {
  236. return opcode * 37;
  237. }
  238. /**
  239. * @return array of match targets
  240. */
  241. public InstructionHandle[] getTargets() {
  242. return targets;
  243. }
  244. public int getLength() {
  245. return length;
  246. }
  247. }