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.

Fundamentals.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. package org.aspectj.apache.bcel.classfile.tests;
  2. import org.aspectj.apache.bcel.Constants;
  3. import org.aspectj.apache.bcel.generic.FieldInstruction;
  4. import org.aspectj.apache.bcel.generic.IINC;
  5. import org.aspectj.apache.bcel.generic.INVOKEINTERFACE;
  6. import org.aspectj.apache.bcel.generic.Instruction;
  7. import org.aspectj.apache.bcel.generic.InstructionBranch;
  8. import org.aspectj.apache.bcel.generic.InstructionByte;
  9. import org.aspectj.apache.bcel.generic.InstructionCP;
  10. import org.aspectj.apache.bcel.generic.InstructionConstants;
  11. import org.aspectj.apache.bcel.generic.InstructionHandle;
  12. import org.aspectj.apache.bcel.generic.InstructionLV;
  13. import org.aspectj.apache.bcel.generic.InstructionShort;
  14. import org.aspectj.apache.bcel.generic.InvokeInstruction;
  15. import org.aspectj.apache.bcel.generic.LOOKUPSWITCH;
  16. import org.aspectj.apache.bcel.generic.MULTIANEWARRAY;
  17. import org.aspectj.apache.bcel.generic.RET;
  18. import org.aspectj.apache.bcel.generic.TABLESWITCH;
  19. import junit.framework.TestCase;
  20. // Check things that have to be true based on the specification
  21. public class Fundamentals extends TestCase {
  22. // Checking: opcode, length, consumed stack entries, produced stack entries
  23. public void testInstructions() {
  24. // Instructions 000-009
  25. checkInstruction(InstructionConstants.NOP,0,1,0,0);
  26. checkInstruction(InstructionConstants.ACONST_NULL,1,1,0,1);
  27. checkInstruction(InstructionConstants.ICONST_M1,2,1,0,1);
  28. checkInstruction(InstructionConstants.ICONST_0,3,1,0,1);
  29. checkInstruction(InstructionConstants.ICONST_1,4,1,0,1);
  30. checkInstruction(InstructionConstants.ICONST_2,5,1,0,1);
  31. checkInstruction(InstructionConstants.ICONST_3,6,1,0,1);
  32. checkInstruction(InstructionConstants.ICONST_4,7,1,0,1);
  33. checkInstruction(InstructionConstants.ICONST_5,8,1,0,1);
  34. checkInstruction(InstructionConstants.LCONST_0,9,1,0,2);
  35. // Instructions 010-019
  36. checkInstruction(InstructionConstants.LCONST_1,10,1,0,2);
  37. checkInstruction(InstructionConstants.FCONST_0,11,1,0,1);
  38. checkInstruction(InstructionConstants.FCONST_1,12,1,0,1);
  39. checkInstruction(InstructionConstants.FCONST_2,13,1,0,1);
  40. checkInstruction(InstructionConstants.DCONST_0,14,1,0,2);
  41. checkInstruction(InstructionConstants.DCONST_1,15,1,0,2);
  42. checkInstruction(new InstructionByte(Constants.BIPUSH,b0),16,2,0,1);
  43. checkInstruction(new InstructionShort(Constants.SIPUSH,s0),17,3,0,1);
  44. checkInstruction(new InstructionCP(Constants.LDC,b0),18,2,0,1);
  45. checkInstruction(new InstructionCP(Constants.LDC_W,s0),19,2,0,1);
  46. // Instructions 020-029
  47. checkInstruction(new InstructionCP(Constants.LDC2_W,s0),20,3,0,2);
  48. checkInstruction(new InstructionLV(Constants.ILOAD,s20),21,2,0,1);
  49. checkInstruction(new InstructionLV(Constants.LLOAD,s20),22,2,0,2);
  50. checkInstruction(new InstructionLV(Constants.FLOAD,s20),23,2,0,1);
  51. checkInstruction(new InstructionLV(Constants.DLOAD,s20),24,2,0,2);
  52. checkInstruction(new InstructionLV(Constants.ALOAD,s20),25,2,0,1);
  53. checkInstruction(InstructionConstants.ILOAD_0,26,1,0,1);
  54. checkInstruction(InstructionConstants.ILOAD_1,27,1,0,1);
  55. checkInstruction(InstructionConstants.ILOAD_2,28,1,0,1);
  56. checkInstruction(InstructionConstants.ILOAD_3,29,1,0,1);
  57. // Instructions 030-039
  58. checkInstruction(InstructionConstants.LLOAD_0,30,1,0,2);
  59. checkInstruction(InstructionConstants.LLOAD_1,31,1,0,2);
  60. checkInstruction(InstructionConstants.LLOAD_2,32,1,0,2);
  61. checkInstruction(InstructionConstants.LLOAD_3,33,1,0,2);
  62. checkInstruction(InstructionConstants.FLOAD_0,34,1,0,1);
  63. checkInstruction(InstructionConstants.FLOAD_1,35,1,0,1);
  64. checkInstruction(InstructionConstants.FLOAD_2,36,1,0,1);
  65. checkInstruction(InstructionConstants.FLOAD_3,37,1,0,1);
  66. checkInstruction(InstructionConstants.DLOAD_0,38,1,0,2);
  67. checkInstruction(InstructionConstants.DLOAD_1,39,1,0,2);
  68. // Instructions 040-049
  69. checkInstruction(InstructionConstants.DLOAD_2,40,1,0,2);
  70. checkInstruction(InstructionConstants.DLOAD_3,41,1,0,2);
  71. checkInstruction(InstructionConstants.ALOAD_0,42,1,0,1);
  72. checkInstruction(InstructionConstants.ALOAD_1,43,1,0,1);
  73. checkInstruction(InstructionConstants.ALOAD_2,44,1,0,1);
  74. checkInstruction(InstructionConstants.ALOAD_3,45,1,0,1);
  75. checkInstruction(InstructionConstants.IALOAD,46,1,2,1);
  76. checkInstruction(InstructionConstants.LALOAD,47,1,2,2);
  77. checkInstruction(InstructionConstants.FALOAD,48,1,2,1);
  78. checkInstruction(InstructionConstants.DALOAD,49,1,2,2);
  79. // Instructions 050-059
  80. checkInstruction(InstructionConstants.AALOAD,50,1,2,1);
  81. checkInstruction(InstructionConstants.BALOAD,51,1,2,1);
  82. checkInstruction(InstructionConstants.CALOAD,52,1,2,1);
  83. checkInstruction(InstructionConstants.SALOAD,53,1,2,1);
  84. checkInstruction(new InstructionLV(Constants.ISTORE,s20),54,2,1,0);
  85. checkInstruction(new InstructionLV(Constants.LSTORE,s20),55,2,2,0);
  86. checkInstruction(new InstructionLV(Constants.FSTORE,s20),56,2,1,0);
  87. checkInstruction(new InstructionLV(Constants.DSTORE,s20),57,2,2,0);
  88. checkInstruction(new InstructionLV(Constants.ASTORE,s20),58,2,1,0);
  89. checkInstruction(InstructionConstants.ISTORE_0,59,1,1,0);
  90. // Instructions 060-069
  91. checkInstruction(InstructionConstants.ISTORE_1,60,1,1,0);
  92. checkInstruction(InstructionConstants.ISTORE_2,61,1,1,0);
  93. checkInstruction(InstructionConstants.ISTORE_3,62,1,1,0);
  94. checkInstruction(InstructionConstants.LSTORE_0,63,1,2,0);
  95. checkInstruction(InstructionConstants.LSTORE_1,64,1,2,0);
  96. checkInstruction(InstructionConstants.LSTORE_2,65,1,2,0);
  97. checkInstruction(InstructionConstants.LSTORE_3,66,1,2,0);
  98. checkInstruction(InstructionConstants.FSTORE_0,67,1,1,0);
  99. checkInstruction(InstructionConstants.FSTORE_1,68,1,1,0);
  100. checkInstruction(InstructionConstants.FSTORE_2,69,1,1,0);
  101. // Instructions 070-079
  102. checkInstruction(InstructionConstants.FSTORE_3,70,1,1,0);
  103. checkInstruction(InstructionConstants.DSTORE_0,71,1,2,0);
  104. checkInstruction(InstructionConstants.DSTORE_1,72,1,2,0);
  105. checkInstruction(InstructionConstants.DSTORE_2,73,1,2,0);
  106. checkInstruction(InstructionConstants.DSTORE_3,74,1,2,0);
  107. checkInstruction(InstructionConstants.ASTORE_0,75,1,1,0);
  108. checkInstruction(InstructionConstants.ASTORE_1,76,1,1,0);
  109. checkInstruction(InstructionConstants.ASTORE_2,77,1,1,0);
  110. checkInstruction(InstructionConstants.ASTORE_3,78,1,1,0);
  111. checkInstruction(InstructionConstants.IASTORE,79,1,3,0);
  112. // Instructions 080-089
  113. checkInstruction(InstructionConstants.LASTORE,80,1,4,0);
  114. checkInstruction(InstructionConstants.FASTORE,81,1,3,0);
  115. checkInstruction(InstructionConstants.DASTORE,82,1,4,0);
  116. checkInstruction(InstructionConstants.AASTORE,83,1,3,0);
  117. checkInstruction(InstructionConstants.BASTORE,84,1,3,0);
  118. checkInstruction(InstructionConstants.CASTORE,85,1,3,0);
  119. checkInstruction(InstructionConstants.SASTORE,86,1,3,0);
  120. checkInstruction(InstructionConstants.POP,87,1,1,0);
  121. checkInstruction(InstructionConstants.POP2,88,1,2,0);
  122. checkInstruction(InstructionConstants.DUP,89,1,1,2);
  123. // Instructions 090-099
  124. checkInstruction(InstructionConstants.DUP_X1,90,1,2,3);
  125. checkInstruction(InstructionConstants.DUP_X2,91,1,3,4);
  126. checkInstruction(InstructionConstants.DUP2,92,1,2,4);
  127. checkInstruction(InstructionConstants.DUP2_X1,93,1,3,5);
  128. checkInstruction(InstructionConstants.DUP2_X2,94,1,4,6);
  129. checkInstruction(InstructionConstants.SWAP,95,1,2,2);
  130. checkInstruction(InstructionConstants.IADD,96,1,2,1);
  131. checkInstruction(InstructionConstants.LADD,97,1,4,2);
  132. checkInstruction(InstructionConstants.FADD,98,1,2,1);
  133. checkInstruction(InstructionConstants.DADD,99,1,4,2);
  134. // Instructions 100-109
  135. checkInstruction(InstructionConstants.ISUB,100,1,2,1);
  136. checkInstruction(InstructionConstants.LSUB,101,1,4,2);
  137. checkInstruction(InstructionConstants.FSUB,102,1,2,1);
  138. checkInstruction(InstructionConstants.DSUB,103,1,4,2);
  139. checkInstruction(InstructionConstants.IMUL,104,1,2,1);
  140. checkInstruction(InstructionConstants.LMUL,105,1,4,2);
  141. checkInstruction(InstructionConstants.FMUL,106,1,2,1);
  142. checkInstruction(InstructionConstants.DMUL,107,1,4,2);
  143. checkInstruction(InstructionConstants.IDIV,108,1,2,1);
  144. checkInstruction(InstructionConstants.LDIV,109,1,4,2);
  145. // Instructions 110-119
  146. checkInstruction(InstructionConstants.FDIV,110,1,2,1);
  147. checkInstruction(InstructionConstants.DDIV,111,1,4,2);
  148. checkInstruction(InstructionConstants.IREM,112,1,2,1);
  149. checkInstruction(InstructionConstants.LREM,113,1,4,2);
  150. checkInstruction(InstructionConstants.FREM,114,1,2,1);
  151. checkInstruction(InstructionConstants.DREM,115,1,4,2);
  152. checkInstruction(InstructionConstants.INEG,116,1,1,1);
  153. checkInstruction(InstructionConstants.LNEG,117,1,2,2);
  154. checkInstruction(InstructionConstants.FNEG,118,1,1,1);
  155. checkInstruction(InstructionConstants.DNEG,119,1,2,2);
  156. // Instructions 120-129
  157. checkInstruction(InstructionConstants.ISHL,120,1,2,1);
  158. checkInstruction(InstructionConstants.LSHL,121,1,3,2);
  159. checkInstruction(InstructionConstants.ISHR,122,1,2,1);
  160. checkInstruction(InstructionConstants.LSHR,123,1,3,2);
  161. checkInstruction(InstructionConstants.IUSHR,124,1,2,1);
  162. checkInstruction(InstructionConstants.LUSHR,125,1,3,2);
  163. checkInstruction(InstructionConstants.IAND,126,1,2,1);
  164. checkInstruction(InstructionConstants.LAND,127,1,4,2);
  165. checkInstruction(InstructionConstants.IOR,128,1,2,1);
  166. checkInstruction(InstructionConstants.LOR,129,1,4,2);
  167. // Instructions 130-139
  168. checkInstruction(InstructionConstants.IXOR,130,1,2,1);
  169. checkInstruction(InstructionConstants.LXOR,131,1,4,2);
  170. checkInstruction(new IINC(0,0,false),132,3,0,0);
  171. checkInstruction(InstructionConstants.I2L,133,1,1,2);
  172. checkInstruction(InstructionConstants.I2F,134,1,1,1);
  173. checkInstruction(InstructionConstants.I2D,135,1,1,2);
  174. checkInstruction(InstructionConstants.L2I,136,1,2,1);
  175. checkInstruction(InstructionConstants.L2F,137,1,2,1);
  176. checkInstruction(InstructionConstants.L2D,138,1,2,2);
  177. checkInstruction(InstructionConstants.F2I,139,1,1,1);
  178. // Instructions 140-149
  179. checkInstruction(InstructionConstants.F2L,140,1,1,2);
  180. checkInstruction(InstructionConstants.F2D,141,1,1,2);
  181. checkInstruction(InstructionConstants.D2I,142,1,2,1);
  182. checkInstruction(InstructionConstants.D2L,143,1,2,2);
  183. checkInstruction(InstructionConstants.D2F,144,1,2,1);
  184. checkInstruction(InstructionConstants.I2B,145,1,1,1);
  185. checkInstruction(InstructionConstants.I2C,146,1,1,1);
  186. checkInstruction(InstructionConstants.I2S,147,1,1,1);
  187. checkInstruction(InstructionConstants.LCMP,148,1,4,1);
  188. checkInstruction(InstructionConstants.FCMPL,149,1,2,1);
  189. // Instructions 150-159
  190. checkInstruction(InstructionConstants.FCMPG,150,1,2,1);
  191. checkInstruction(InstructionConstants.DCMPL,151,1,4,1);
  192. checkInstruction(InstructionConstants.DCMPG,152,1,4,1);
  193. checkInstruction(new InstructionBranch(Constants.IFEQ,s0),153,3,1,0);
  194. checkInstruction(new InstructionBranch(Constants.IFNE,s0),154,3,1,0);
  195. checkInstruction(new InstructionBranch(Constants.IFLT,s0),155,3,1,0);
  196. checkInstruction(new InstructionBranch(Constants.IFGE,s0),156,3,1,0);
  197. checkInstruction(new InstructionBranch(Constants.IFGT,s0),157,3,1,0);
  198. checkInstruction(new InstructionBranch(Constants.IFLE,s0),158,3,1,0);
  199. checkInstruction(new InstructionBranch(Constants.IF_ICMPEQ,s0),159,3,2,0);
  200. // Instructions 160-169
  201. checkInstruction(new InstructionBranch(Constants.IF_ICMPNE,s0),160,3,2,0);
  202. checkInstruction(new InstructionBranch(Constants.IF_ICMPLT,s0),161,3,2,0);
  203. checkInstruction(new InstructionBranch(Constants.IF_ICMPGE,s0),162,3,2,0);
  204. checkInstruction(new InstructionBranch(Constants.IF_ICMPGT,s0),163,3,2,0);
  205. checkInstruction(new InstructionBranch(Constants.IF_ICMPLE,s0),164,3,2,0);
  206. checkInstruction(new InstructionBranch(Constants.IF_ACMPEQ,s0),165,3,2,0);
  207. checkInstruction(new InstructionBranch(Constants.IF_ACMPNE,s0),166,3,2,0);
  208. checkInstruction(new InstructionBranch(Constants.GOTO,s0),167,3,0,0);
  209. checkInstruction(new InstructionBranch(Constants.JSR,s0),168,3,0,1);
  210. checkInstruction(new RET(0,false),169,2,0,0);
  211. // Instructions 170-179
  212. checkInstruction(new TABLESWITCH(new int[]{},new InstructionHandle[]{},null),170,VARIES,1,0);
  213. checkInstruction(new LOOKUPSWITCH(new int[]{},new InstructionHandle[]{},null),171,VARIES,1,0);
  214. checkInstruction(InstructionConstants.IRETURN,172,1,1,0);
  215. checkInstruction(InstructionConstants.LRETURN,173,1,2,0);
  216. checkInstruction(InstructionConstants.FRETURN,174,1,1,0);
  217. checkInstruction(InstructionConstants.DRETURN,175,1,2,0);
  218. checkInstruction(InstructionConstants.ARETURN,176,1,1,0);
  219. checkInstruction(InstructionConstants.RETURN,177,1,0,0);
  220. checkInstruction(new FieldInstruction(Constants.GETSTATIC,0),178,3,0,VARIES);
  221. checkInstruction(new FieldInstruction(Constants.PUTSTATIC,0),179,3,VARIES,0);
  222. // Instructions 180-189
  223. checkInstruction(new FieldInstruction(Constants.GETFIELD,0),180,3,1,VARIES);
  224. checkInstruction(new FieldInstruction(Constants.PUTFIELD,0),181,3,VARIES,0);
  225. checkInstruction(new InvokeInstruction(Constants.INVOKEVIRTUAL,0),182,3,VARIES,VARIES); // PRODUCE STACK VARIES OK HERE? (AND NEXT COUPLE)
  226. checkInstruction(new InvokeInstruction(Constants.INVOKESPECIAL,0),183,3,VARIES,VARIES);
  227. checkInstruction(new InvokeInstruction(Constants.INVOKESTATIC,0),184,3,VARIES,VARIES);
  228. checkInstruction(new INVOKEINTERFACE(0,1,0),185,5,VARIES,VARIES);
  229. // 186 does not exist
  230. checkInstruction(new InstructionCP(Constants.NEW,b0),187,3,0,1);
  231. checkInstruction(new InstructionByte(Constants.NEWARRAY,b0),188,2,1,1);
  232. checkInstruction(new InstructionCP(Constants.ANEWARRAY,0),189,3,1,1);
  233. // Instructions 190-199
  234. checkInstruction(InstructionConstants.ARRAYLENGTH,190,1,1,1);
  235. checkInstruction(InstructionConstants.ATHROW,191,1,1,1);
  236. checkInstruction(new InstructionCP(Constants.CHECKCAST,s0),192,3,1,1);
  237. checkInstruction(new InstructionCP(Constants.INSTANCEOF,s0),193,3,1,1);
  238. checkInstruction(InstructionConstants.MONITORENTER,194,1,1,0);
  239. checkInstruction(InstructionConstants.MONITOREXIT,195,1,1,0);
  240. // 196 is 'wide' tag
  241. checkInstruction(new MULTIANEWARRAY(s0,b0),197,4,VARIES,1);
  242. checkInstruction(new InstructionBranch(Constants.IFNULL,s0),198,3,1,0);
  243. checkInstruction(new InstructionBranch(Constants.IFNONNULL,s0),199,3,1,0);
  244. // Instructions 200-209
  245. checkInstruction(new InstructionBranch(Constants.GOTO_W,0),200,5,0,0);
  246. checkInstruction(new InstructionBranch(Constants.JSR_W,0),201,5,0,1);
  247. // Internally used instructions skipped
  248. }
  249. public void checkInstruction(Instruction i,int opcode, int length, int stackConsumed, int stackProduced) {
  250. String header = new String("Checking instruction '"+i+"' ");
  251. if (i.opcode!=opcode)
  252. fail(header+" expected opcode "+opcode+" but it is "+i.opcode);
  253. if (length!=VARIES && i.getLength()!=length)
  254. fail(header+" expected length "+length+" but it is "+i.getLength());
  255. // if (stackConsumed>0) {
  256. // if ((Constants.instFlags[opcode]&Constants.STACK_CONSUMER)==0)
  257. // fail(header+" expected it to be a STACK_CONSUMER but it is not");
  258. // } else {
  259. // if ((Constants.instFlags[opcode]&Constants.STACK_CONSUMER)!=0)
  260. // fail(header+" expected it not to be a STACK_CONSUMER but it is");
  261. // }
  262. if (stackConsumed==VARIES) {
  263. if (Constants.CONSUME_STACK[opcode]!=Constants.UNPREDICTABLE)
  264. fail("Instruction '"+i+"' should be consuming some unpredictable number of stack entries but it says it will consume "+Constants.CONSUME_STACK[opcode]);
  265. } else {
  266. if (Constants.CONSUME_STACK[opcode]!=stackConsumed)
  267. fail("Instruction '"+i+"' should be consuming "+stackConsumed+" stack entries but it says it will consume "+Constants.CONSUME_STACK[opcode]);
  268. }
  269. // if (stackProduced>0) {
  270. // if ((Constants.instFlags[opcode]&Constants.STACK_PRODUCER)==0)
  271. // fail(header+" expected it to be a STACK_PRODUCER but it is not");
  272. // } else {
  273. // if ((Constants.instFlags[opcode]&Constants.STACK_PRODUCER)!=0)
  274. // fail(header+" expected it not to be a STACK_PRODUCER but it is");
  275. // }
  276. if (stackProduced==VARIES) {
  277. if (Constants.stackEntriesProduced[opcode]!=Constants.UNPREDICTABLE)
  278. fail(header+" should be producing some unpredictable number of stack entries but it says it will produce "+Constants.stackEntriesProduced[opcode]);
  279. } else {
  280. if (Constants.stackEntriesProduced[opcode]!=stackProduced)
  281. fail(header+" should be producing "+stackProduced+" stack entries but it says it will produce "+Constants.stackEntriesProduced[opcode]);
  282. }
  283. }
  284. private final static byte b0 = 0;
  285. private final static short s0 = 0;
  286. private final static short s20 = 20;
  287. private final static int VARIES = -1;
  288. }