Label.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Copyright (c) 1997, 2004, 2008, 2010 Per M.A. Bothner.
  2. // This is free software; for terms and warranty disclaimer see ./COPYING.
  3. package gnu.bytecode;
  4. import java.util.ArrayList;
  5. /**
  6. * A Label represents a location in a Code attribute.
  7. */
  8. public class Label {
  9. /** Offset of definition in the fixup_offsets and fixup_labels arrays.
  10. * The offset corresponds to the fixup itself. */
  11. int first_fixup;
  12. /** The PC of where the label is, or -1 if not yet defined.
  13. * The value -2 means don't generate a StackMapTable entry.
  14. * This PC may be tentative if we later run processFixups.
  15. * The offset in the code array is cattr.fixupOffset(first_fixup). */
  16. int position;
  17. boolean needsStackMapEntry;
  18. // FIXME Probably more efficient to have a single array:
  19. // local-types followed by stack-types. We'd need an extra short field.
  20. Type[] stackTypes;
  21. Type[] localTypes;
  22. public final boolean defined () { return position >= 0; }
  23. public Label ()
  24. {
  25. this(-1);
  26. }
  27. public Label (CodeAttr code)
  28. {
  29. this(-1);
  30. }
  31. public Label (int position)
  32. {
  33. this.position = position;
  34. }
  35. public boolean isUsed() { return stackTypes != null; }
  36. Type mergeTypes (Type t1, Type t2)
  37. {
  38. if ((t1 instanceof PrimType) != (t2 instanceof PrimType))
  39. return null;
  40. return Type.lowestCommonSuperType(t1, t2);
  41. }
  42. void setTypes (Type[] locals, int usedLocals,
  43. Type[] stack, int usedStack)
  44. {
  45. for (; usedLocals > 0; usedLocals--)
  46. {
  47. Type last = locals[usedLocals-1];
  48. if (last != null)
  49. break;
  50. }
  51. if (stackTypes == null)
  52. {
  53. if (usedStack == 0)
  54. stackTypes = Type.typeArray0;
  55. else
  56. {
  57. stackTypes = new Type[usedStack];
  58. System.arraycopy(stack, 0, stackTypes, 0, usedStack);
  59. }
  60. if (usedLocals == 0)
  61. localTypes = Type.typeArray0;
  62. else
  63. {
  64. localTypes = new Type[usedLocals];
  65. System.arraycopy(locals, 0, localTypes, 0, usedLocals);
  66. }
  67. }
  68. else
  69. {
  70. int SP = usedStack;
  71. int slen = stackTypes.length;
  72. if (SP != slen)
  73. throw new InternalError("inconsistent stack length");
  74. for (int i = 0; i < SP; i++)
  75. {
  76. Type t = mergeTypes(stackTypes[i], stack[i]);
  77. if (t == null)
  78. throw new InternalError("inconsistent stackType");
  79. stackTypes[i] = t;
  80. }
  81. for (int i = 0; i < localTypes.length; i++)
  82. {
  83. mergeLocalType(i, i < usedLocals ? locals[i] : null);
  84. }
  85. }
  86. }
  87. public void setTypes (CodeAttr code)
  88. {
  89. addTypeChangeListeners(code);
  90. if (stackTypes != null && code.SP != stackTypes.length)
  91. throw new InternalError("bad type SP:"+code.SP+" st.l:"+stackTypes.length+" code:"+code.getMethod());
  92. setTypes(code.local_types,
  93. code.local_types == null ? 0 : code.local_types.length,
  94. code.stack_types,
  95. code.SP);
  96. }
  97. public void setTypes (Label other)
  98. {
  99. setTypes(other.localTypes, other.localTypes.length,
  100. other.stackTypes, other.stackTypes.length);
  101. }
  102. private void mergeLocalType (int varnum, Type newType)
  103. {
  104. if (varnum < localTypes.length)
  105. {
  106. Type oldLocal = localTypes[varnum];
  107. Type newLocal = mergeTypes(oldLocal, newType);
  108. if (newLocal != oldLocal)
  109. {
  110. localTypes[varnum] = newLocal;
  111. notifyTypeChangeListeners(varnum, newLocal);
  112. }
  113. }
  114. }
  115. private void notifyTypeChangeListeners (int varnum, Type newType)
  116. {
  117. Object[] arr = typeChangeListeners;
  118. if (arr == null || arr.length <= varnum)
  119. return;
  120. Object listeners = arr[varnum];
  121. if (listeners == null)
  122. return;
  123. if (listeners instanceof Label)
  124. ((Label) listeners).mergeLocalType(varnum, newType);
  125. else
  126. {
  127. for (Label listener : (ArrayList<Label>) listeners)
  128. listener.mergeLocalType(varnum, newType);
  129. }
  130. if (newType == null)
  131. arr[varnum] = null;
  132. }
  133. /** Map from Variable number to set of listeners.
  134. * When {@code this.localTypes[varnum]} is invalidated, then we also
  135. * need to invalidate that variable in all the listeners.
  136. * The type is actually a {@code Union<Label,ArrayList<Label>>[]}.
  137. */
  138. private Object[] typeChangeListeners;
  139. void addTypeChangeListener (int varnum, Label listener)
  140. {
  141. Object[] arr = typeChangeListeners;
  142. if (arr == null)
  143. typeChangeListeners = arr = new Object[varnum + 10];
  144. else if (arr.length <= varnum)
  145. {
  146. arr = new Object[varnum + 10];
  147. System.arraycopy(typeChangeListeners, 0, arr, 0, typeChangeListeners.length);
  148. typeChangeListeners = arr;
  149. }
  150. Object set = arr[varnum];
  151. if (set == null)
  152. arr[varnum] = listener;
  153. else
  154. {
  155. ArrayList<Label> list;
  156. if (set instanceof Label)
  157. {
  158. list = new ArrayList<Label>();
  159. list.add((Label) set);
  160. arr[varnum] = list;
  161. }
  162. else
  163. list = (ArrayList<Label>) set;
  164. list.add(listener);
  165. }
  166. }
  167. void addTypeChangeListeners (CodeAttr code)
  168. {
  169. if (code.local_types != null && code.previousLabel != null)
  170. {
  171. int len = code.local_types.length;
  172. for (int varnum = 0; varnum < len; varnum++)
  173. {
  174. if (code.local_types[varnum] != null
  175. && (code.varsSetInCurrentBlock == null
  176. || code.varsSetInCurrentBlock.length <= varnum
  177. || ! code.varsSetInCurrentBlock[varnum]))
  178. code.previousLabel.addTypeChangeListener(varnum, this);
  179. }
  180. }
  181. }
  182. /**
  183. * Define the value of a label as having the current location.
  184. * @param code the "Code" attribute of the current method
  185. */
  186. public void defineRaw (CodeAttr code) {
  187. defineRaw(code, CodeAttr.FIXUP_DEFINE);
  188. }
  189. void defineRaw (CodeAttr code, int fixupKind)
  190. {
  191. if (position >= 0)
  192. throw new Error ("label definition more than once");
  193. position = code.PC;
  194. first_fixup = code.fixup_count;
  195. if (first_fixup >= 0)
  196. code.fixupAdd(fixupKind, this);
  197. }
  198. /**
  199. * Define the value of a label as having the current location.
  200. * @param code the "Code" attribute of the current method
  201. */
  202. public void define (CodeAttr code)
  203. {
  204. boolean wasReachable=code.reachableHere();
  205. if (wasReachable)
  206. {
  207. setTypes(code);
  208. }
  209. else if (localTypes != null)
  210. {
  211. for (int i = localTypes.length; --i >= 0; )
  212. {
  213. if (localTypes[i] != null
  214. && (code.locals.used == null || code.locals.used[i] == null))
  215. {
  216. localTypes[i] = null;
  217. }
  218. }
  219. }
  220. code.setPreviousLabelHere(this);
  221. defineRaw(code, (wasReachable ? CodeAttr.FIXUP_DEFINE
  222. : CodeAttr.FIXUP_DEFINE_UNREACHABLE));
  223. if (localTypes != null) {
  224. // Copy merged type back to current state.
  225. code.setTypes(this);
  226. }
  227. code.setReachable(true);
  228. }
  229. /* DEBUG
  230. int id = ++counter;
  231. static int counter;
  232. public String toString() { return "Label#"+id+"-pos:"+position; }
  233. */
  234. }