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.

InstructionWalker.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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, i list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, i 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. * "i product includes software developed by the
  22. * Apache Software Foundation (https://www.apache.org/)."
  23. * Alternately, i 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 i software without prior written permission. For
  29. * written permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from i 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. * i 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 i SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * i 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.verifier;
  55. import org.aspectj.apache.bcel.Constants;
  56. import org.aspectj.apache.bcel.generic.FieldInstruction;
  57. import org.aspectj.apache.bcel.generic.InstVisitor;
  58. import org.aspectj.apache.bcel.generic.Instruction;
  59. import org.aspectj.apache.bcel.generic.InstructionBranch;
  60. import org.aspectj.apache.bcel.generic.InstructionLV;
  61. import org.aspectj.apache.bcel.generic.InvokeInstruction;
  62. /**
  63. * Traverse an instruction
  64. *
  65. * @author Andy Clement
  66. */
  67. public class InstructionWalker implements Constants {
  68. /**
  69. * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call methods
  70. * according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
  71. *
  72. * @param i the instruction to visit
  73. * @param v Visitor object
  74. */
  75. public static void accept(Instruction i, InstVisitor v) {
  76. switch (i.opcode) {
  77. case IMPDEP1:
  78. v.visitIMPDEP1(i);
  79. break;
  80. case IMPDEP2:
  81. v.visitIMPDEP2(i);
  82. break;
  83. case MONITORENTER:
  84. v.visitExceptionThrower(i);
  85. v.visitStackConsumer(i);
  86. v.visitMONITORENTER(i);
  87. break;
  88. case MONITOREXIT:
  89. v.visitExceptionThrower(i);
  90. v.visitStackConsumer(i);
  91. v.visitMONITOREXIT(i);
  92. break;
  93. case LCMP:
  94. v.visitTypedInstruction(i);
  95. v.visitStackProducer(i);
  96. v.visitStackConsumer(i);
  97. v.visitLCMP(i);
  98. break;
  99. case FCMPL:
  100. v.visitTypedInstruction(i);
  101. v.visitStackProducer(i);
  102. v.visitStackConsumer(i);
  103. v.visitFCMPL(i);
  104. break;
  105. case FCMPG:
  106. v.visitTypedInstruction(i);
  107. v.visitStackProducer(i);
  108. v.visitStackConsumer(i);
  109. v.visitFCMPG(i);
  110. break;
  111. case DCMPL:
  112. v.visitTypedInstruction(i);
  113. v.visitStackProducer(i);
  114. v.visitStackConsumer(i);
  115. v.visitDCMPL(i);
  116. break;
  117. case DCMPG:
  118. v.visitTypedInstruction(i);
  119. v.visitStackProducer(i);
  120. v.visitStackConsumer(i);
  121. v.visitDCMPG(i);
  122. break;
  123. case NOP:
  124. v.visitNOP(i);
  125. break;
  126. case BREAKPOINT:
  127. v.visitBREAKPOINT(i);
  128. break;
  129. case SWAP:
  130. v.visitStackConsumer(i);
  131. v.visitStackProducer(i);
  132. v.visitStackInstruction(i);
  133. v.visitSWAP(i);
  134. break;
  135. case POP:
  136. v.visitStackConsumer(i);
  137. v.visitPopInstruction(i);
  138. v.visitStackInstruction(i);
  139. v.visitPOP(i);
  140. break;
  141. case POP2:
  142. v.visitStackConsumer(i);
  143. v.visitPopInstruction(i);
  144. v.visitStackInstruction(i);
  145. v.visitPOP2(i);
  146. break;
  147. case DUP2_X1:
  148. v.visitStackInstruction(i);
  149. v.visitDUP2_X1(i);
  150. break;
  151. case DUP2_X2:
  152. v.visitStackInstruction(i);
  153. v.visitDUP2_X2(i);
  154. break;
  155. case DUP2:
  156. v.visitStackProducer(i);
  157. v.visitPushInstruction(i);
  158. v.visitStackInstruction(i);
  159. v.visitDUP2(i);
  160. break;
  161. case DUP_X1:
  162. v.visitStackInstruction(i);
  163. v.visitDUP_X1(i);
  164. break;
  165. case DUP_X2:
  166. v.visitStackInstruction(i);
  167. v.visitDUP_X2(i);
  168. break;
  169. case DUP:
  170. v.visitStackProducer(i);
  171. v.visitPushInstruction(i);
  172. v.visitStackInstruction(i);
  173. v.visitDUP(i);
  174. break;
  175. case BASTORE:
  176. v.visitStackConsumer(i);
  177. v.visitExceptionThrower(i);
  178. v.visitTypedInstruction(i);
  179. v.visitArrayInstruction(i);
  180. v.visitBASTORE(i);
  181. break;
  182. case CASTORE:
  183. v.visitStackConsumer(i);
  184. v.visitExceptionThrower(i);
  185. v.visitTypedInstruction(i);
  186. v.visitArrayInstruction(i);
  187. v.visitCASTORE(i);
  188. break;
  189. case SASTORE:
  190. v.visitStackConsumer(i);
  191. v.visitExceptionThrower(i);
  192. v.visitTypedInstruction(i);
  193. v.visitArrayInstruction(i);
  194. v.visitSASTORE(i);
  195. break;
  196. case DASTORE:
  197. v.visitStackConsumer(i);
  198. v.visitExceptionThrower(i);
  199. v.visitTypedInstruction(i);
  200. v.visitArrayInstruction(i);
  201. v.visitDASTORE(i);
  202. break;
  203. case FASTORE:
  204. v.visitStackConsumer(i);
  205. v.visitExceptionThrower(i);
  206. v.visitTypedInstruction(i);
  207. v.visitArrayInstruction(i);
  208. v.visitFASTORE(i);
  209. break;
  210. case LASTORE:
  211. v.visitStackConsumer(i);
  212. v.visitExceptionThrower(i);
  213. v.visitTypedInstruction(i);
  214. v.visitArrayInstruction(i);
  215. v.visitLASTORE(i);
  216. break;
  217. case IASTORE:
  218. v.visitStackConsumer(i);
  219. v.visitExceptionThrower(i);
  220. v.visitTypedInstruction(i);
  221. v.visitArrayInstruction(i);
  222. v.visitIASTORE(i);
  223. break;
  224. case AASTORE:
  225. v.visitStackConsumer(i);
  226. v.visitExceptionThrower(i);
  227. v.visitTypedInstruction(i);
  228. v.visitArrayInstruction(i);
  229. v.visitAASTORE(i);
  230. break;
  231. case SALOAD:
  232. v.visitStackProducer(i);
  233. v.visitExceptionThrower(i);
  234. v.visitTypedInstruction(i);
  235. v.visitArrayInstruction(i);
  236. v.visitSALOAD(i);
  237. break;
  238. case CALOAD:
  239. v.visitStackProducer(i);
  240. v.visitExceptionThrower(i);
  241. v.visitTypedInstruction(i);
  242. v.visitArrayInstruction(i);
  243. v.visitCALOAD(i);
  244. break;
  245. case DALOAD:
  246. v.visitStackProducer(i);
  247. v.visitExceptionThrower(i);
  248. v.visitTypedInstruction(i);
  249. v.visitArrayInstruction(i);
  250. v.visitDALOAD(i);
  251. break;
  252. case FALOAD:
  253. v.visitStackProducer(i);
  254. v.visitExceptionThrower(i);
  255. v.visitTypedInstruction(i);
  256. v.visitArrayInstruction(i);
  257. v.visitFALOAD(i);
  258. break;
  259. case LALOAD:
  260. v.visitStackProducer(i);
  261. v.visitExceptionThrower(i);
  262. v.visitTypedInstruction(i);
  263. v.visitArrayInstruction(i);
  264. v.visitLALOAD(i);
  265. break;
  266. case AALOAD:
  267. v.visitStackProducer(i);
  268. v.visitExceptionThrower(i);
  269. v.visitTypedInstruction(i);
  270. v.visitArrayInstruction(i);
  271. v.visitAALOAD(i);
  272. break;
  273. case ATHROW:
  274. v.visitUnconditionalBranch(i);
  275. v.visitExceptionThrower(i);
  276. v.visitATHROW(i);
  277. break;
  278. case ACONST_NULL:
  279. v.visitStackProducer(i);
  280. v.visitPushInstruction(i);
  281. v.visitTypedInstruction(i);
  282. v.visitACONST_NULL(i);
  283. break;
  284. case ICONST_M1:
  285. case ICONST_0:
  286. case ICONST_1:
  287. case ICONST_2:
  288. case ICONST_3:
  289. case ICONST_4:
  290. case ICONST_5:
  291. v.visitPushInstruction(i);
  292. v.visitStackProducer(i);
  293. v.visitTypedInstruction(i);
  294. v.visitConstantPushInstruction(i);
  295. v.visitICONST(i);
  296. break;
  297. case LCONST_0:
  298. case LCONST_1:
  299. v.visitPushInstruction(i);
  300. v.visitStackProducer(i);
  301. v.visitTypedInstruction(i);
  302. v.visitConstantPushInstruction(i);
  303. v.visitLCONST(i);
  304. break;
  305. case FCONST_0:
  306. case FCONST_1:
  307. case FCONST_2:
  308. v.visitPushInstruction(i);
  309. v.visitStackProducer(i);
  310. v.visitTypedInstruction(i);
  311. v.visitConstantPushInstruction(i);
  312. v.visitFCONST(i);
  313. break;
  314. case DCONST_0:
  315. case DCONST_1:
  316. v.visitPushInstruction(i);
  317. v.visitStackProducer(i);
  318. v.visitTypedInstruction(i);
  319. v.visitConstantPushInstruction(i);
  320. v.visitDCONST(i);
  321. break;
  322. case BALOAD:
  323. v.visitStackProducer(i);
  324. v.visitExceptionThrower(i);
  325. v.visitTypedInstruction(i);
  326. v.visitArrayInstruction(i);
  327. v.visitBALOAD(i);
  328. break;
  329. case IALOAD:
  330. v.visitStackProducer(i);
  331. v.visitExceptionThrower(i);
  332. v.visitTypedInstruction(i);
  333. v.visitArrayInstruction(i);
  334. v.visitIALOAD(i);
  335. break;
  336. case BIPUSH:
  337. v.visitPushInstruction(i);
  338. v.visitStackProducer(i);
  339. v.visitTypedInstruction(i);
  340. v.visitConstantPushInstruction(i);
  341. v.visitBIPUSH(i);
  342. break;
  343. case SIPUSH:
  344. v.visitPushInstruction(i);
  345. v.visitStackProducer(i);
  346. v.visitTypedInstruction(i);
  347. v.visitConstantPushInstruction(i);
  348. v.visitSIPUSH(i);
  349. break;
  350. case LDC:
  351. case LDC_W:
  352. v.visitStackProducer(i);
  353. v.visitPushInstruction(i);
  354. v.visitExceptionThrower(i);
  355. v.visitTypedInstruction(i);
  356. v.visitCPInstruction(i);
  357. v.visitLDC(i);
  358. break;
  359. case LDC2_W:
  360. v.visitStackProducer(i);
  361. v.visitPushInstruction(i);
  362. v.visitTypedInstruction(i);
  363. v.visitCPInstruction(i);
  364. v.visitLDC2_W(i);
  365. break;
  366. case ARRAYLENGTH:
  367. v.visitExceptionThrower(i);
  368. v.visitStackProducer(i);
  369. v.visitARRAYLENGTH(i);
  370. break;
  371. case ASTORE_0:
  372. v.visitStackConsumer(i);
  373. v.visitPopInstruction(i);
  374. v.visitStoreInstruction(i);
  375. v.visitTypedInstruction(i);
  376. v.visitLocalVariableInstruction((InstructionLV) i);
  377. v.visitStoreInstruction(i);
  378. v.visitASTORE(i);
  379. break;
  380. case ALOAD_0:
  381. v.visitStackConsumer(i);
  382. v.visitPopInstruction(i);
  383. v.visitStoreInstruction(i);
  384. v.visitTypedInstruction(i);
  385. v.visitLocalVariableInstruction((InstructionLV) i);
  386. v.visitStoreInstruction(i);
  387. v.visitALOAD(i);
  388. break;
  389. // for store instructions: ISTORE > ASTORE_3 - needs to visit the instruction too
  390. // v.visitStackConsumer(i);
  391. // v.visitPopInstruction(i);
  392. // v.visitStoreInstruction(i);
  393. // v.visitTypedInstruction(i);
  394. // v.visitLocalVariableInstruction(i);
  395. // v.visitStoreInstruction(i);
  396. // for load instructions: ILOAD > ALOAD_3 - needs to visit the instruction too
  397. // v.visitStackProducer(i);
  398. // v.visitPushInstruction(i);
  399. // v.visitTypedInstruction(i);
  400. // v.visitLocalVariableInstruction(i);
  401. // v.visitLoadInstruction(i);
  402. // for conversion instructions: (all 15 of them) - needs to visit conversion instruction too
  403. // v.visitTypedInstruction(i);
  404. // v.visitStackProducer(i);
  405. // v.visitStackConsumer(i);
  406. // v.visitConversionInstruction(i);
  407. // arithmetic instructions - need to visit the instructions too (iadd etc)
  408. // v.visitTypedInstruction(i);
  409. // v.visitStackProducer(i);
  410. // v.visitStackConsumer(i);
  411. // v.visitArithmeticInstruction(i);
  412. case INVOKESTATIC:
  413. v.visitExceptionThrower(i);
  414. v.visitTypedInstruction(i);
  415. v.visitStackConsumer(i);
  416. v.visitStackProducer(i);
  417. v.visitLoadClass(i);
  418. v.visitCPInstruction(i);
  419. v.visitFieldOrMethod(i);
  420. v.visitInvokeInstruction((InvokeInstruction) i);
  421. v.visitINVOKESTATIC((InvokeInstruction) i);
  422. break;
  423. case GOTO:
  424. v.visitVariableLengthInstruction(i);
  425. v.visitUnconditionalBranch(i);
  426. v.visitBranchInstruction((InstructionBranch) i);
  427. v.visitGotoInstruction(i);
  428. v.visitGOTO(i);
  429. break;
  430. case PUTSTATIC:
  431. v.visitExceptionThrower(i);
  432. v.visitStackConsumer(i);
  433. v.visitPopInstruction(i);
  434. v.visitTypedInstruction(i);
  435. v.visitLoadClass(i);
  436. v.visitCPInstruction(i);
  437. v.visitFieldOrMethod(i);
  438. v.visitFieldInstruction(i);
  439. v.visitPUTSTATIC((FieldInstruction) i);
  440. break;
  441. case RETURN:
  442. v.visitExceptionThrower(i);
  443. v.visitTypedInstruction(i);
  444. v.visitStackConsumer(i);
  445. v.visitReturnInstruction(i);
  446. v.visitRETURN(i);
  447. break;
  448. default:
  449. throw new IllegalStateException("visiting not yet implemented for " + i.getName().toUpperCase());
  450. }
  451. }
  452. }