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.

Frame.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later,
  9. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist.bytecode.analysis;
  17. /**
  18. * Represents the stack frame and local variable table at a particular point in time.
  19. *
  20. * @author Jason T. Greene
  21. */
  22. public class Frame {
  23. private Type[] locals;
  24. private Type[] stack;
  25. private int top;
  26. private boolean jsrMerged;
  27. private boolean retMerged;
  28. /**
  29. * Create a new frame with the specified local variable table size, and max stack size
  30. *
  31. * @param locals the number of local variable table entries
  32. * @param stack the maximum stack size
  33. */
  34. public Frame(int locals, int stack) {
  35. this.locals = new Type[locals];
  36. this.stack = new Type[stack];
  37. }
  38. /**
  39. * Returns the local varaible table entry at index.
  40. *
  41. * @param index the position in the table
  42. * @return the type if one exists, or null if the position is empty
  43. */
  44. public Type getLocal(int index) {
  45. return locals[index];
  46. }
  47. /**
  48. * Sets the local variable table entry at index to a type.
  49. *
  50. * @param index the position in the table
  51. * @param type the type to set at the position
  52. */
  53. public void setLocal(int index, Type type) {
  54. locals[index] = type;
  55. }
  56. /**
  57. * Returns the type on the stack at the specified index.
  58. *
  59. * @param index the position on the stack
  60. * @return the type of the stack position
  61. */
  62. public Type getStack(int index) {
  63. return stack[index];
  64. }
  65. /**
  66. * Sets the type of the stack position
  67. *
  68. * @param index the position on the stack
  69. * @param type the type to set
  70. */
  71. public void setStack(int index, Type type) {
  72. stack[index] = type;
  73. }
  74. /**
  75. * Empties the stack
  76. */
  77. public void clearStack() {
  78. top = 0;
  79. }
  80. /**
  81. * Gets the index of the type sitting at the top of the stack.
  82. * This is not to be confused with a length operation which
  83. * would return the number of elements, not the position of
  84. * the last element.
  85. *
  86. * @return the position of the element at the top of the stack
  87. */
  88. public int getTopIndex() {
  89. return top - 1;
  90. }
  91. /**
  92. * Returns the number of local variable table entries, specified
  93. * at construction.
  94. *
  95. * @return the number of local variable table entries
  96. */
  97. public int localsLength() {
  98. return locals.length;
  99. }
  100. /**
  101. * Gets the top of the stack without altering it
  102. *
  103. * @return the top of the stack
  104. */
  105. public Type peek() {
  106. if (top < 1)
  107. throw new IndexOutOfBoundsException("Stack is empty");
  108. return stack[top - 1];
  109. }
  110. /**
  111. * Alters the stack to contain one less element and return it.
  112. *
  113. * @return the element popped from the stack
  114. */
  115. public Type pop() {
  116. if (top < 1)
  117. throw new IndexOutOfBoundsException("Stack is empty");
  118. return stack[--top];
  119. }
  120. /**
  121. * Alters the stack by placing the passed type on the top
  122. *
  123. * @param type the type to add to the top
  124. */
  125. public void push(Type type) {
  126. stack[top++] = type;
  127. }
  128. /**
  129. * Makes a shallow copy of this frame, i.e. the type instances will
  130. * remain the same.
  131. *
  132. * @return the shallow copy
  133. */
  134. public Frame copy() {
  135. Frame frame = new Frame(locals.length, stack.length);
  136. System.arraycopy(locals, 0, frame.locals, 0, locals.length);
  137. System.arraycopy(stack, 0, frame.stack, 0, stack.length);
  138. frame.top = top;
  139. return frame;
  140. }
  141. /**
  142. * Makes a shallow copy of the stack portion of this frame. The local
  143. * variable table size will be copied, but its contents will be empty.
  144. *
  145. * @return the shallow copy of the stack
  146. */
  147. public Frame copyStack() {
  148. Frame frame = new Frame(locals.length, stack.length);
  149. System.arraycopy(stack, 0, frame.stack, 0, stack.length);
  150. frame.top = top;
  151. return frame;
  152. }
  153. /**
  154. * Merges all types on the stack of this frame instance with that of the specified frame.
  155. * The local variable table is left untouched.
  156. *
  157. * @param frame the frame to merge the stack from
  158. * @return true if any changes where made
  159. */
  160. public boolean mergeStack(Frame frame) {
  161. boolean changed = false;
  162. if (top != frame.top)
  163. throw new RuntimeException("Operand stacks could not be merged, they are different sizes!");
  164. for (int i = 0; i < top; i++) {
  165. if (stack[i] != null) {
  166. Type prev = stack[i];
  167. Type merged = prev.merge(frame.stack[i]);
  168. if (merged == Type.BOGUS)
  169. throw new RuntimeException("Operand stacks could not be merged due to differing primitive types: pos = " + i);
  170. stack[i] = merged;
  171. // always replace the instance in case a multi-interface type changes to a normal Type
  172. if ((! merged.equals(prev)) || merged.popChanged()) {
  173. changed = true;
  174. }
  175. }
  176. }
  177. return changed;
  178. }
  179. /**
  180. * Merges all types on the stack and local variable table of this frame with that of the specified
  181. * type.
  182. *
  183. * @param frame the frame to merge with
  184. * @return true if any changes to this frame where made by this merge
  185. */
  186. public boolean merge(Frame frame) {
  187. boolean changed = false;
  188. // Local variable table
  189. for (int i = 0; i < locals.length; i++) {
  190. if (locals[i] != null) {
  191. Type prev = locals[i];
  192. Type merged = prev.merge(frame.locals[i]);
  193. // always replace the instance in case a multi-interface type changes to a normal Type
  194. locals[i] = merged;
  195. if (! merged.equals(prev) || merged.popChanged()) {
  196. changed = true;
  197. }
  198. } else if (frame.locals[i] != null) {
  199. locals[i] = frame.locals[i];
  200. changed = true;
  201. }
  202. }
  203. changed |= mergeStack(frame);
  204. return changed;
  205. }
  206. public String toString() {
  207. StringBuffer buffer = new StringBuffer();
  208. buffer.append("locals = [");
  209. for (int i = 0; i < locals.length; i++) {
  210. buffer.append(locals[i] == null ? "empty" : locals[i].toString());
  211. if (i < locals.length - 1)
  212. buffer.append(", ");
  213. }
  214. buffer.append("] stack = [");
  215. for (int i = 0; i < top; i++) {
  216. buffer.append(stack[i]);
  217. if (i < top - 1)
  218. buffer.append(", ");
  219. }
  220. buffer.append("]");
  221. return buffer.toString();
  222. }
  223. /**
  224. * Whether or not state from the source JSR instruction has been merged
  225. *
  226. * @return true if JSR state has been merged
  227. */
  228. boolean isJsrMerged() {
  229. return jsrMerged;
  230. }
  231. /**
  232. * Sets whether of not the state from the source JSR instruction has been merged
  233. *
  234. * @param jsrMerged true if merged, otherwise false
  235. */
  236. void setJsrMerged(boolean jsrMerged) {
  237. this.jsrMerged = jsrMerged;
  238. }
  239. /**
  240. * Whether or not state from the RET instruction, of the subroutine that was jumped
  241. * to has been merged.
  242. *
  243. * @return true if RET state has been merged
  244. */
  245. boolean isRetMerged() {
  246. return retMerged;
  247. }
  248. /**
  249. * Sets whether or not state from the RET instruction, of the subroutine that was jumped
  250. * to has been merged.
  251. *
  252. * @param retMerged true if RET state has been merged
  253. */
  254. void setRetMerged(boolean retMerged) {
  255. this.retMerged = retMerged;
  256. }
  257. }