001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020package org.apache.bcel.generic; 021 022import org.apache.bcel.Const; 023 024/** 025 * Contains shareable instruction objects. 026 * <p> 027 * Reusing predefined instruction objects can save time and memory, especially for frequently used instructions such as ICONST_0. 028 * These objects are shared and should not be modified. This approach adapts the Flyweight design pattern by using an array instead of a factory. 029 * </p> 030 * <p> 031 * The instruction objects can also be accessed directly by name, for example, {@code il.append(InstructionConst.ICONST_0)}. 032 * </p> 033 */ 034public final class InstructionConst { 035 036 /** 037 * Predefined instruction objects. NOTE these are not currently immutable, because Instruction has mutable protected fields opcode and length. 038 */ 039 040 /** NOP instruction. */ 041 public static final Instruction NOP = new NOP(); 042 043 /** ACONST_NULL instruction. */ 044 public static final Instruction ACONST_NULL = new ACONST_NULL(); 045 046 /** ICONST_M1 instruction. */ 047 public static final Instruction ICONST_M1 = new ICONST(-1); 048 049 /** ICONST_0 instruction. */ 050 public static final Instruction ICONST_0 = new ICONST(0); 051 052 /** ICONST_1 instruction. */ 053 public static final Instruction ICONST_1 = new ICONST(1); 054 055 /** ICONST_2 instruction. */ 056 public static final Instruction ICONST_2 = new ICONST(2); 057 058 /** ICONST_3 instruction. */ 059 public static final Instruction ICONST_3 = new ICONST(3); 060 061 /** ICONST_4 instruction. */ 062 public static final Instruction ICONST_4 = new ICONST(4); 063 064 /** ICONST_5 instruction. */ 065 public static final Instruction ICONST_5 = new ICONST(5); 066 067 /** LCONST_0 instruction. */ 068 public static final Instruction LCONST_0 = new LCONST(0); 069 070 /** LCONST_1 instruction. */ 071 public static final Instruction LCONST_1 = new LCONST(1); 072 073 /** FCONST_0 instruction. */ 074 public static final Instruction FCONST_0 = new FCONST(0); 075 076 /** FCONST_1 instruction. */ 077 public static final Instruction FCONST_1 = new FCONST(1); 078 079 /** FCONST_2 instruction. */ 080 public static final Instruction FCONST_2 = new FCONST(2); 081 082 /** DCONST_0 instruction. */ 083 public static final Instruction DCONST_0 = new DCONST(0); 084 085 /** DCONST_1 instruction. */ 086 public static final Instruction DCONST_1 = new DCONST(1); 087 088 /** IALOAD instruction. */ 089 public static final ArrayInstruction IALOAD = new IALOAD(); 090 091 /** LALOAD instruction. */ 092 public static final ArrayInstruction LALOAD = new LALOAD(); 093 094 /** FALOAD instruction. */ 095 public static final ArrayInstruction FALOAD = new FALOAD(); 096 097 /** DALOAD instruction. */ 098 public static final ArrayInstruction DALOAD = new DALOAD(); 099 100 /** AALOAD instruction. */ 101 public static final ArrayInstruction AALOAD = new AALOAD(); 102 103 /** BALOAD instruction. */ 104 public static final ArrayInstruction BALOAD = new BALOAD(); 105 106 /** CALOAD instruction. */ 107 public static final ArrayInstruction CALOAD = new CALOAD(); 108 109 /** SALOAD instruction. */ 110 public static final ArrayInstruction SALOAD = new SALOAD(); 111 112 /** IASTORE instruction. */ 113 public static final ArrayInstruction IASTORE = new IASTORE(); 114 115 /** LASTORE instruction. */ 116 public static final ArrayInstruction LASTORE = new LASTORE(); 117 118 /** FASTORE instruction. */ 119 public static final ArrayInstruction FASTORE = new FASTORE(); 120 121 /** DASTORE instruction. */ 122 public static final ArrayInstruction DASTORE = new DASTORE(); 123 124 /** AASTORE instruction. */ 125 public static final ArrayInstruction AASTORE = new AASTORE(); 126 127 /** BASTORE instruction. */ 128 public static final ArrayInstruction BASTORE = new BASTORE(); 129 130 /** CASTORE instruction. */ 131 public static final ArrayInstruction CASTORE = new CASTORE(); 132 133 /** SASTORE instruction. */ 134 public static final ArrayInstruction SASTORE = new SASTORE(); 135 136 /** POP instruction. */ 137 public static final StackInstruction POP = new POP(); 138 139 /** POP2 instruction. */ 140 public static final StackInstruction POP2 = new POP2(); 141 142 /** DUP instruction. */ 143 public static final StackInstruction DUP = new DUP(); 144 145 /** DUP_X1 instruction. */ 146 public static final StackInstruction DUP_X1 = new DUP_X1(); 147 148 /** DUP_X2 instruction. */ 149 public static final StackInstruction DUP_X2 = new DUP_X2(); 150 151 /** DUP2 instruction. */ 152 public static final StackInstruction DUP2 = new DUP2(); 153 154 /** DUP2_X1 instruction. */ 155 public static final StackInstruction DUP2_X1 = new DUP2_X1(); 156 157 /** DUP2_X2 instruction. */ 158 public static final StackInstruction DUP2_X2 = new DUP2_X2(); 159 160 /** SWAP instruction. */ 161 public static final StackInstruction SWAP = new SWAP(); 162 163 /** IADD instruction. */ 164 public static final ArithmeticInstruction IADD = new IADD(); 165 166 /** LADD instruction. */ 167 public static final ArithmeticInstruction LADD = new LADD(); 168 169 /** FADD instruction. */ 170 public static final ArithmeticInstruction FADD = new FADD(); 171 172 /** DADD instruction. */ 173 public static final ArithmeticInstruction DADD = new DADD(); 174 175 /** ISUB instruction. */ 176 public static final ArithmeticInstruction ISUB = new ISUB(); 177 178 /** LSUB instruction. */ 179 public static final ArithmeticInstruction LSUB = new LSUB(); 180 181 /** FSUB instruction. */ 182 public static final ArithmeticInstruction FSUB = new FSUB(); 183 184 /** DSUB instruction. */ 185 public static final ArithmeticInstruction DSUB = new DSUB(); 186 187 /** IMUL instruction. */ 188 public static final ArithmeticInstruction IMUL = new IMUL(); 189 190 /** LMUL instruction. */ 191 public static final ArithmeticInstruction LMUL = new LMUL(); 192 193 /** FMUL instruction. */ 194 public static final ArithmeticInstruction FMUL = new FMUL(); 195 196 /** DMUL instruction. */ 197 public static final ArithmeticInstruction DMUL = new DMUL(); 198 199 /** IDIV instruction. */ 200 public static final ArithmeticInstruction IDIV = new IDIV(); 201 202 /** LDIV instruction. */ 203 public static final ArithmeticInstruction LDIV = new LDIV(); 204 205 /** FDIV instruction. */ 206 public static final ArithmeticInstruction FDIV = new FDIV(); 207 208 /** DDIV instruction. */ 209 public static final ArithmeticInstruction DDIV = new DDIV(); 210 211 /** IREM instruction. */ 212 public static final ArithmeticInstruction IREM = new IREM(); 213 214 /** LREM instruction. */ 215 public static final ArithmeticInstruction LREM = new LREM(); 216 217 /** FREM instruction. */ 218 public static final ArithmeticInstruction FREM = new FREM(); 219 220 /** DREM instruction. */ 221 public static final ArithmeticInstruction DREM = new DREM(); 222 223 /** INEG instruction. */ 224 public static final ArithmeticInstruction INEG = new INEG(); 225 226 /** LNEG instruction. */ 227 public static final ArithmeticInstruction LNEG = new LNEG(); 228 229 /** FNEG instruction. */ 230 public static final ArithmeticInstruction FNEG = new FNEG(); 231 232 /** DNEG instruction. */ 233 public static final ArithmeticInstruction DNEG = new DNEG(); 234 235 /** ISHL instruction. */ 236 public static final ArithmeticInstruction ISHL = new ISHL(); 237 238 /** LSHL instruction. */ 239 public static final ArithmeticInstruction LSHL = new LSHL(); 240 241 /** ISHR instruction. */ 242 public static final ArithmeticInstruction ISHR = new ISHR(); 243 244 /** LSHR instruction. */ 245 public static final ArithmeticInstruction LSHR = new LSHR(); 246 247 /** IUSHR instruction. */ 248 public static final ArithmeticInstruction IUSHR = new IUSHR(); 249 250 /** LUSHR instruction. */ 251 public static final ArithmeticInstruction LUSHR = new LUSHR(); 252 253 /** IAND instruction. */ 254 public static final ArithmeticInstruction IAND = new IAND(); 255 256 /** LAND instruction. */ 257 public static final ArithmeticInstruction LAND = new LAND(); 258 259 /** IOR instruction. */ 260 public static final ArithmeticInstruction IOR = new IOR(); 261 262 /** LOR instruction. */ 263 public static final ArithmeticInstruction LOR = new LOR(); 264 265 /** IXOR instruction. */ 266 public static final ArithmeticInstruction IXOR = new IXOR(); 267 268 /** LXOR instruction. */ 269 public static final ArithmeticInstruction LXOR = new LXOR(); 270 271 /** I2L instruction. */ 272 public static final ConversionInstruction I2L = new I2L(); 273 274 /** I2F instruction. */ 275 public static final ConversionInstruction I2F = new I2F(); 276 277 /** I2D instruction. */ 278 public static final ConversionInstruction I2D = new I2D(); 279 280 /** L2I instruction. */ 281 public static final ConversionInstruction L2I = new L2I(); 282 283 /** L2F instruction. */ 284 public static final ConversionInstruction L2F = new L2F(); 285 286 /** L2D instruction. */ 287 public static final ConversionInstruction L2D = new L2D(); 288 289 /** F2I instruction. */ 290 public static final ConversionInstruction F2I = new F2I(); 291 292 /** F2L instruction. */ 293 public static final ConversionInstruction F2L = new F2L(); 294 295 /** F2D instruction. */ 296 public static final ConversionInstruction F2D = new F2D(); 297 298 /** D2I instruction. */ 299 public static final ConversionInstruction D2I = new D2I(); 300 301 /** D2L instruction. */ 302 public static final ConversionInstruction D2L = new D2L(); 303 304 /** D2F instruction. */ 305 public static final ConversionInstruction D2F = new D2F(); 306 307 /** I2B instruction. */ 308 public static final ConversionInstruction I2B = new I2B(); 309 310 /** I2C instruction. */ 311 public static final ConversionInstruction I2C = new I2C(); 312 313 /** I2S instruction. */ 314 public static final ConversionInstruction I2S = new I2S(); 315 316 /** LCMP instruction. */ 317 public static final Instruction LCMP = new LCMP(); 318 319 /** FCMPL instruction. */ 320 public static final Instruction FCMPL = new FCMPL(); 321 322 /** FCMPG instruction. */ 323 public static final Instruction FCMPG = new FCMPG(); 324 325 /** DCMPL instruction. */ 326 public static final Instruction DCMPL = new DCMPL(); 327 328 /** DCMPG instruction. */ 329 public static final Instruction DCMPG = new DCMPG(); 330 331 /** IRETURN instruction. */ 332 public static final ReturnInstruction IRETURN = new IRETURN(); 333 334 /** LRETURN instruction. */ 335 public static final ReturnInstruction LRETURN = new LRETURN(); 336 337 /** FRETURN instruction. */ 338 public static final ReturnInstruction FRETURN = new FRETURN(); 339 340 /** DRETURN instruction. */ 341 public static final ReturnInstruction DRETURN = new DRETURN(); 342 343 /** ARETURN instruction. */ 344 public static final ReturnInstruction ARETURN = new ARETURN(); 345 346 /** RETURN instruction. */ 347 public static final ReturnInstruction RETURN = new RETURN(); 348 349 /** ARRAYLENGTH instruction. */ 350 public static final Instruction ARRAYLENGTH = new ARRAYLENGTH(); 351 352 /** ATHROW instruction. */ 353 public static final Instruction ATHROW = new ATHROW(); 354 355 /** MONITORENTER instruction. */ 356 public static final Instruction MONITORENTER = new MONITORENTER(); 357 358 /** MONITOREXIT instruction. */ 359 public static final Instruction MONITOREXIT = new MONITOREXIT(); 360 361 /** 362 * You can use these constants in multiple places safely, if you can guarantee that you will never alter their internal values, for example call setIndex(). 363 * THIS instruction (ALOAD_0) 364 */ 365 public static final LocalVariableInstruction THIS = new ALOAD(0); 366 367 /** ALOAD_0 instruction (same as THIS). */ 368 public static final LocalVariableInstruction ALOAD_0 = THIS; 369 370 /** ALOAD_1 instruction. */ 371 public static final LocalVariableInstruction ALOAD_1 = new ALOAD(1); 372 373 /** ALOAD_2 instruction. */ 374 public static final LocalVariableInstruction ALOAD_2 = new ALOAD(2); 375 376 /** ILOAD_0 instruction. */ 377 public static final LocalVariableInstruction ILOAD_0 = new ILOAD(0); 378 379 /** ILOAD_1 instruction. */ 380 public static final LocalVariableInstruction ILOAD_1 = new ILOAD(1); 381 382 /** ILOAD_2 instruction. */ 383 public static final LocalVariableInstruction ILOAD_2 = new ILOAD(2); 384 385 /** ASTORE_0 instruction. */ 386 public static final LocalVariableInstruction ASTORE_0 = new ASTORE(0); 387 388 /** ASTORE_1 instruction. */ 389 public static final LocalVariableInstruction ASTORE_1 = new ASTORE(1); 390 391 /** ASTORE_2 instruction. */ 392 public static final LocalVariableInstruction ASTORE_2 = new ASTORE(2); 393 394 /** ISTORE_0 instruction. */ 395 public static final LocalVariableInstruction ISTORE_0 = new ISTORE(0); 396 397 /** ISTORE_1 instruction. */ 398 public static final LocalVariableInstruction ISTORE_1 = new ISTORE(1); 399 400 /** ISTORE_2 instruction. */ 401 public static final LocalVariableInstruction ISTORE_2 = new ISTORE(2); 402 403 /** 404 * Instruction objects indexed by opcode. Entries for instructions without a predefined instance, such as branch instructions, are null. 405 */ 406 static final Instruction[] INSTRUCTIONS = new Instruction[256]; 407 408 static { 409 INSTRUCTIONS[Const.NOP] = NOP; 410 INSTRUCTIONS[Const.ACONST_NULL] = ACONST_NULL; 411 INSTRUCTIONS[Const.ICONST_M1] = ICONST_M1; 412 INSTRUCTIONS[Const.ICONST_0] = ICONST_0; 413 INSTRUCTIONS[Const.ICONST_1] = ICONST_1; 414 INSTRUCTIONS[Const.ICONST_2] = ICONST_2; 415 INSTRUCTIONS[Const.ICONST_3] = ICONST_3; 416 INSTRUCTIONS[Const.ICONST_4] = ICONST_4; 417 INSTRUCTIONS[Const.ICONST_5] = ICONST_5; 418 INSTRUCTIONS[Const.LCONST_0] = LCONST_0; 419 INSTRUCTIONS[Const.LCONST_1] = LCONST_1; 420 INSTRUCTIONS[Const.FCONST_0] = FCONST_0; 421 INSTRUCTIONS[Const.FCONST_1] = FCONST_1; 422 INSTRUCTIONS[Const.FCONST_2] = FCONST_2; 423 INSTRUCTIONS[Const.DCONST_0] = DCONST_0; 424 INSTRUCTIONS[Const.DCONST_1] = DCONST_1; 425 INSTRUCTIONS[Const.IALOAD] = IALOAD; 426 INSTRUCTIONS[Const.LALOAD] = LALOAD; 427 INSTRUCTIONS[Const.FALOAD] = FALOAD; 428 INSTRUCTIONS[Const.DALOAD] = DALOAD; 429 INSTRUCTIONS[Const.AALOAD] = AALOAD; 430 INSTRUCTIONS[Const.BALOAD] = BALOAD; 431 INSTRUCTIONS[Const.CALOAD] = CALOAD; 432 INSTRUCTIONS[Const.SALOAD] = SALOAD; 433 INSTRUCTIONS[Const.IASTORE] = IASTORE; 434 INSTRUCTIONS[Const.LASTORE] = LASTORE; 435 INSTRUCTIONS[Const.FASTORE] = FASTORE; 436 INSTRUCTIONS[Const.DASTORE] = DASTORE; 437 INSTRUCTIONS[Const.AASTORE] = AASTORE; 438 INSTRUCTIONS[Const.BASTORE] = BASTORE; 439 INSTRUCTIONS[Const.CASTORE] = CASTORE; 440 INSTRUCTIONS[Const.SASTORE] = SASTORE; 441 INSTRUCTIONS[Const.POP] = POP; 442 INSTRUCTIONS[Const.POP2] = POP2; 443 INSTRUCTIONS[Const.DUP] = DUP; 444 INSTRUCTIONS[Const.DUP_X1] = DUP_X1; 445 INSTRUCTIONS[Const.DUP_X2] = DUP_X2; 446 INSTRUCTIONS[Const.DUP2] = DUP2; 447 INSTRUCTIONS[Const.DUP2_X1] = DUP2_X1; 448 INSTRUCTIONS[Const.DUP2_X2] = DUP2_X2; 449 INSTRUCTIONS[Const.SWAP] = SWAP; 450 INSTRUCTIONS[Const.IADD] = IADD; 451 INSTRUCTIONS[Const.LADD] = LADD; 452 INSTRUCTIONS[Const.FADD] = FADD; 453 INSTRUCTIONS[Const.DADD] = DADD; 454 INSTRUCTIONS[Const.ISUB] = ISUB; 455 INSTRUCTIONS[Const.LSUB] = LSUB; 456 INSTRUCTIONS[Const.FSUB] = FSUB; 457 INSTRUCTIONS[Const.DSUB] = DSUB; 458 INSTRUCTIONS[Const.IMUL] = IMUL; 459 INSTRUCTIONS[Const.LMUL] = LMUL; 460 INSTRUCTIONS[Const.FMUL] = FMUL; 461 INSTRUCTIONS[Const.DMUL] = DMUL; 462 INSTRUCTIONS[Const.IDIV] = IDIV; 463 INSTRUCTIONS[Const.LDIV] = LDIV; 464 INSTRUCTIONS[Const.FDIV] = FDIV; 465 INSTRUCTIONS[Const.DDIV] = DDIV; 466 INSTRUCTIONS[Const.IREM] = IREM; 467 INSTRUCTIONS[Const.LREM] = LREM; 468 INSTRUCTIONS[Const.FREM] = FREM; 469 INSTRUCTIONS[Const.DREM] = DREM; 470 INSTRUCTIONS[Const.INEG] = INEG; 471 INSTRUCTIONS[Const.LNEG] = LNEG; 472 INSTRUCTIONS[Const.FNEG] = FNEG; 473 INSTRUCTIONS[Const.DNEG] = DNEG; 474 INSTRUCTIONS[Const.ISHL] = ISHL; 475 INSTRUCTIONS[Const.LSHL] = LSHL; 476 INSTRUCTIONS[Const.ISHR] = ISHR; 477 INSTRUCTIONS[Const.LSHR] = LSHR; 478 INSTRUCTIONS[Const.IUSHR] = IUSHR; 479 INSTRUCTIONS[Const.LUSHR] = LUSHR; 480 INSTRUCTIONS[Const.IAND] = IAND; 481 INSTRUCTIONS[Const.LAND] = LAND; 482 INSTRUCTIONS[Const.IOR] = IOR; 483 INSTRUCTIONS[Const.LOR] = LOR; 484 INSTRUCTIONS[Const.IXOR] = IXOR; 485 INSTRUCTIONS[Const.LXOR] = LXOR; 486 INSTRUCTIONS[Const.I2L] = I2L; 487 INSTRUCTIONS[Const.I2F] = I2F; 488 INSTRUCTIONS[Const.I2D] = I2D; 489 INSTRUCTIONS[Const.L2I] = L2I; 490 INSTRUCTIONS[Const.L2F] = L2F; 491 INSTRUCTIONS[Const.L2D] = L2D; 492 INSTRUCTIONS[Const.F2I] = F2I; 493 INSTRUCTIONS[Const.F2L] = F2L; 494 INSTRUCTIONS[Const.F2D] = F2D; 495 INSTRUCTIONS[Const.D2I] = D2I; 496 INSTRUCTIONS[Const.D2L] = D2L; 497 INSTRUCTIONS[Const.D2F] = D2F; 498 INSTRUCTIONS[Const.I2B] = I2B; 499 INSTRUCTIONS[Const.I2C] = I2C; 500 INSTRUCTIONS[Const.I2S] = I2S; 501 INSTRUCTIONS[Const.LCMP] = LCMP; 502 INSTRUCTIONS[Const.FCMPL] = FCMPL; 503 INSTRUCTIONS[Const.FCMPG] = FCMPG; 504 INSTRUCTIONS[Const.DCMPL] = DCMPL; 505 INSTRUCTIONS[Const.DCMPG] = DCMPG; 506 INSTRUCTIONS[Const.IRETURN] = IRETURN; 507 INSTRUCTIONS[Const.LRETURN] = LRETURN; 508 INSTRUCTIONS[Const.FRETURN] = FRETURN; 509 INSTRUCTIONS[Const.DRETURN] = DRETURN; 510 INSTRUCTIONS[Const.ARETURN] = ARETURN; 511 INSTRUCTIONS[Const.RETURN] = RETURN; 512 INSTRUCTIONS[Const.ARRAYLENGTH] = ARRAYLENGTH; 513 INSTRUCTIONS[Const.ATHROW] = ATHROW; 514 INSTRUCTIONS[Const.MONITORENTER] = MONITORENTER; 515 INSTRUCTIONS[Const.MONITOREXIT] = MONITOREXIT; 516 } 517 518 /** 519 * Gets the Instruction. 520 * 521 * @param index The index, for example {@link Const#RETURN}. 522 * @return The entry from the private INSTRUCTIONS table. 523 */ 524 public static Instruction getInstruction(final int index) { 525 return INSTRUCTIONS[index]; 526 } 527 528 private InstructionConst() { 529 } // non-instantiable 530}