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.

BasicBlock.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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.stackmap;
  17. import javassist.bytecode.*;
  18. import java.util.HashMap;
  19. import java.util.ArrayList;
  20. /**
  21. * A basic block is a sequence of bytecode that does not contain jump/branch
  22. * instructions except at the last bytecode.
  23. * Since Java7 or later does not allow JSR, this class throws an exception when
  24. * it finds JSR.
  25. */
  26. public class BasicBlock {
  27. static class JsrBytecode extends BadBytecode {
  28. JsrBytecode() { super("JSR"); }
  29. }
  30. protected int position, length;
  31. protected int incoming; // the number of incoming branches.
  32. protected BasicBlock[] exit; // null if the block is a leaf.
  33. protected boolean stop; // true if the block ends with an unconditional jump.
  34. protected Catch toCatch;
  35. protected BasicBlock(int pos) {
  36. position = pos;
  37. length = 0;
  38. incoming = 0;
  39. }
  40. public static BasicBlock find(BasicBlock[] blocks, int pos)
  41. throws BadBytecode
  42. {
  43. for (int i = 0; i < blocks.length; i++) {
  44. int iPos = blocks[i].position;
  45. if (iPos <= pos && pos < iPos + blocks[i].length)
  46. return blocks[i];
  47. }
  48. throw new BadBytecode("no basic block at " + pos);
  49. }
  50. public static class Catch {
  51. public Catch next;
  52. public BasicBlock body;
  53. public int typeIndex;
  54. Catch(BasicBlock b, int i, Catch c) {
  55. body = b;
  56. typeIndex = i;
  57. next = c;
  58. }
  59. }
  60. public String toString() {
  61. StringBuffer sbuf = new StringBuffer();
  62. String cname = this.getClass().getName();
  63. int i = cname.lastIndexOf('.');
  64. sbuf.append(i < 0 ? cname : cname.substring(i + 1));
  65. sbuf.append("[");
  66. toString2(sbuf);
  67. sbuf.append("]");
  68. return sbuf.toString();
  69. }
  70. protected void toString2(StringBuffer sbuf) {
  71. sbuf.append("pos=").append(position).append(", len=")
  72. .append(length).append(", in=").append(incoming)
  73. .append(", exit{");
  74. if (exit != null) {
  75. for (int i = 0; i < exit.length; i++)
  76. sbuf.append(exit[i].position).append(",");
  77. }
  78. sbuf.append("}, {");
  79. Catch th = toCatch;
  80. while (th != null) {
  81. sbuf.append("(").append(th.body.position).append(", ")
  82. .append(th.typeIndex).append("), ");
  83. th = th.next;
  84. }
  85. sbuf.append("}");
  86. }
  87. /**
  88. * A Mark indicates the position of a branch instruction
  89. * or a branch target.
  90. */
  91. static class Mark implements Comparable {
  92. int position;
  93. BasicBlock block;
  94. BasicBlock[] jump;
  95. boolean alwaysJmp; // true if an unconditional branch.
  96. int size; // 0 unless the mark indicates RETURN etc.
  97. Catch catcher;
  98. Mark(int p) {
  99. position = p;
  100. block = null;
  101. jump = null;
  102. alwaysJmp = false;
  103. size = 0;
  104. catcher = null;
  105. }
  106. public int compareTo(Object obj) {
  107. if (obj instanceof Mark) {
  108. int pos = ((Mark)obj).position;
  109. return position - pos;
  110. }
  111. return -1;
  112. }
  113. void setJump(BasicBlock[] bb, int s, boolean always) {
  114. jump = bb;
  115. size = s;
  116. alwaysJmp = always;
  117. }
  118. }
  119. public static class Maker {
  120. /* Override these two methods if a subclass of BasicBlock must be
  121. * instantiated.
  122. */
  123. protected BasicBlock makeBlock(int pos) {
  124. return new BasicBlock(pos);
  125. }
  126. protected BasicBlock[] makeArray(int size) {
  127. return new BasicBlock[size];
  128. }
  129. private BasicBlock[] makeArray(BasicBlock b) {
  130. BasicBlock[] array = makeArray(1);
  131. array[0] = b;
  132. return array;
  133. }
  134. private BasicBlock[] makeArray(BasicBlock b1, BasicBlock b2) {
  135. BasicBlock[] array = makeArray(2);
  136. array[0] = b1;
  137. array[1] = b2;
  138. return array;
  139. }
  140. public BasicBlock[] make(MethodInfo minfo) throws BadBytecode {
  141. CodeAttribute ca = minfo.getCodeAttribute();
  142. if (ca == null)
  143. return null;
  144. CodeIterator ci = ca.iterator();
  145. return make(ci, 0, ci.getCodeLength(), ca.getExceptionTable());
  146. }
  147. public BasicBlock[] make(CodeIterator ci, int begin, int end,
  148. ExceptionTable et)
  149. throws BadBytecode
  150. {
  151. HashMap marks = makeMarks(ci, begin, end, et);
  152. BasicBlock[] bb = makeBlocks(marks);
  153. addCatchers(bb, et);
  154. return bb;
  155. }
  156. /* Branch target
  157. */
  158. private Mark makeMark(HashMap table, int pos) {
  159. return makeMark0(table, pos, true, true);
  160. }
  161. /* Branch instruction.
  162. * size > 0
  163. */
  164. private Mark makeMark(HashMap table, int pos, BasicBlock[] jump,
  165. int size, boolean always) {
  166. Mark m = makeMark0(table, pos, false, false);
  167. m.setJump(jump, size, always);
  168. return m;
  169. }
  170. private Mark makeMark0(HashMap table, int pos,
  171. boolean isBlockBegin, boolean isTarget) {
  172. Integer p = new Integer(pos);
  173. Mark m = (Mark)table.get(p);
  174. if (m == null) {
  175. m = new Mark(pos);
  176. table.put(p, m);
  177. }
  178. if (isBlockBegin) {
  179. if (m.block == null)
  180. m.block = makeBlock(pos);
  181. if (isTarget)
  182. m.block.incoming++;
  183. }
  184. return m;
  185. }
  186. private HashMap makeMarks(CodeIterator ci, int begin, int end,
  187. ExceptionTable et)
  188. throws BadBytecode
  189. {
  190. ci.begin();
  191. ci.move(begin);
  192. HashMap marks = new HashMap();
  193. while (ci.hasNext()) {
  194. int index = ci.next();
  195. if (index >= end)
  196. break;
  197. int op = ci.byteAt(index);
  198. if ((Opcode.IFEQ <= op && op <= Opcode.IF_ACMPNE)
  199. || op == Opcode.IFNULL || op == Opcode.IFNONNULL) {
  200. Mark to = makeMark(marks, index + ci.s16bitAt(index + 1));
  201. Mark next = makeMark(marks, index + 3);
  202. makeMark(marks, index, makeArray(to.block, next.block), 3, false);
  203. }
  204. else if (Opcode.GOTO <= op && op <= Opcode.LOOKUPSWITCH)
  205. switch (op) {
  206. case Opcode.GOTO :
  207. makeGoto(marks, index, index + ci.s16bitAt(index + 1), 3);
  208. break;
  209. case Opcode.JSR :
  210. makeJsr(marks, index, index + ci.s16bitAt(index + 1), 3);
  211. break;
  212. case Opcode.RET :
  213. makeMark(marks, index, null, 2, true);
  214. break;
  215. case Opcode.TABLESWITCH : {
  216. int pos = (index & ~3) + 4;
  217. int low = ci.s32bitAt(pos + 4);
  218. int high = ci.s32bitAt(pos + 8);
  219. int ncases = high - low + 1;
  220. BasicBlock[] to = makeArray(ncases + 1);
  221. to[0] = makeMark(marks, index + ci.s32bitAt(pos)).block; // default branch target
  222. int p = pos + 12;
  223. int n = p + ncases * 4;
  224. int k = 1;
  225. while (p < n) {
  226. to[k++] = makeMark(marks, index + ci.s32bitAt(p)).block;
  227. p += 4;
  228. }
  229. makeMark(marks, index, to, n - index, true);
  230. break; }
  231. case Opcode.LOOKUPSWITCH : {
  232. int pos = (index & ~3) + 4;
  233. int ncases = ci.s32bitAt(pos + 4);
  234. BasicBlock[] to = makeArray(ncases + 1);
  235. to[0] = makeMark(marks, index + ci.s32bitAt(pos)).block; // default branch target
  236. int p = pos + 8 + 4;
  237. int n = p + ncases * 8 - 4;
  238. int k = 1;
  239. while (p < n) {
  240. to[k++] = makeMark(marks, index + ci.s32bitAt(p)).block;
  241. p += 8;
  242. }
  243. makeMark(marks, index, to, n - index, true);
  244. break; }
  245. }
  246. else if ((Opcode.IRETURN <= op && op <= Opcode.RETURN) || op == Opcode.ATHROW)
  247. makeMark(marks, index, null, 1, true);
  248. else if (op == Opcode.GOTO_W)
  249. makeGoto(marks, index, index + ci.s32bitAt(index + 1), 5);
  250. else if (op == Opcode.JSR_W)
  251. makeJsr(marks, index, index + ci.s32bitAt(index + 1), 5);
  252. else if (op == Opcode.WIDE && ci.byteAt(index + 1) == Opcode.RET)
  253. makeMark(marks, index, null, 4, true);
  254. }
  255. if (et != null) {
  256. int i = et.size();
  257. while (--i >= 0) {
  258. makeMark0(marks, et.startPc(i), true, false);
  259. makeMark(marks, et.handlerPc(i));
  260. }
  261. }
  262. return marks;
  263. }
  264. private void makeGoto(HashMap marks, int pos, int target, int size) {
  265. Mark to = makeMark(marks, target);
  266. BasicBlock[] jumps = makeArray(to.block);
  267. makeMark(marks, pos, jumps, size, true);
  268. }
  269. /*
  270. * We could ignore JSR since Java 7 or later does not allow it.
  271. * See The JVM Spec. Sec. 4.10.2.5.
  272. */
  273. protected void makeJsr(HashMap marks, int pos, int target, int size) throws BadBytecode {
  274. /*
  275. Mark to = makeMark(marks, target);
  276. Mark next = makeMark(marks, pos + size);
  277. BasicBlock[] jumps = makeArray(to.block, next.block);
  278. makeMark(marks, pos, jumps, size, false);
  279. */
  280. throw new JsrBytecode();
  281. }
  282. private BasicBlock[] makeBlocks(HashMap markTable) {
  283. Mark[] marks = (Mark[])markTable.values()
  284. .toArray(new Mark[markTable.size()]);
  285. java.util.Arrays.sort(marks);
  286. ArrayList blocks = new ArrayList();
  287. int i = 0;
  288. BasicBlock prev;
  289. if (marks.length > 0 && marks[0].position == 0 && marks[0].block != null)
  290. prev = getBBlock(marks[i++]);
  291. else
  292. prev = makeBlock(0);
  293. blocks.add(prev);
  294. while (i < marks.length) {
  295. Mark m = marks[i++];
  296. BasicBlock bb = getBBlock(m);
  297. if (bb == null) {
  298. // the mark indicates a branch instruction
  299. if (prev.length > 0) {
  300. // the previous mark already has exits.
  301. prev = makeBlock(prev.position + prev.length);
  302. blocks.add(prev);
  303. }
  304. prev.length = m.position + m.size - prev.position;
  305. prev.exit = m.jump;
  306. prev.stop = m.alwaysJmp;
  307. }
  308. else {
  309. // the mark indicates a branch target
  310. if (prev.length == 0) {
  311. prev.length = m.position - prev.position;
  312. bb.incoming++;
  313. prev.exit = makeArray(bb);
  314. }
  315. else {
  316. // the previous mark already has exits.
  317. if (prev.position + prev.length < m.position) {
  318. // dead code is found.
  319. prev = makeBlock(prev.position + prev.length);
  320. blocks.add(prev);
  321. prev.length = m.position - prev.position;
  322. // the incoming flow from dead code is not counted
  323. // bb.incoming++;
  324. prev.exit = makeArray(bb);
  325. }
  326. }
  327. blocks.add(bb);
  328. prev = bb;
  329. }
  330. }
  331. return (BasicBlock[])blocks.toArray(makeArray(blocks.size()));
  332. }
  333. private static BasicBlock getBBlock(Mark m) {
  334. BasicBlock b = m.block;
  335. if (b != null && m.size > 0) {
  336. b.exit = m.jump;
  337. b.length = m.size;
  338. b.stop = m.alwaysJmp;
  339. }
  340. return b;
  341. }
  342. private void addCatchers(BasicBlock[] blocks, ExceptionTable et)
  343. throws BadBytecode
  344. {
  345. if (et == null)
  346. return;
  347. int i = et.size();
  348. while (--i >= 0) {
  349. BasicBlock handler = find(blocks, et.handlerPc(i));
  350. int start = et.startPc(i);
  351. int end = et.endPc(i);
  352. int type = et.catchType(i);
  353. handler.incoming--;
  354. for (int k = 0; k < blocks.length; k++) {
  355. BasicBlock bb = blocks[k];
  356. int iPos = bb.position;
  357. if (start <= iPos && iPos < end) {
  358. bb.toCatch = new Catch(handler, type, bb.toCatch);
  359. handler.incoming++;
  360. }
  361. }
  362. }
  363. }
  364. }
  365. }