001// ASM: a very small and fast Java bytecode manipulation framework 002// Copyright (c) 2000-2011 INRIA, France Telecom 003// All rights reserved. 004// 005// Redistribution and use in source and binary forms, with or without 006// modification, are permitted provided that the following conditions 007// are met: 008// 1. Redistributions of source code must retain the above copyright 009// notice, this list of conditions and the following disclaimer. 010// 2. Redistributions in binary form must reproduce the above copyright 011// notice, this list of conditions and the following disclaimer in the 012// documentation and/or other materials provided with the distribution. 013// 3. Neither the name of the copyright holders nor the names of its 014// contributors may be used to endorse or promote products derived from 015// this software without specific prior written permission. 016// 017// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 018// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 019// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 020// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 021// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 022// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 023// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 024// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 025// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 026// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 027// THE POSSIBILITY OF SUCH DAMAGE. 028package io.ebean.enhance.asm; 029 030/** 031 * The JVM opcodes, access flags and array type codes. This interface does not define all the JVM 032 * opcodes because some opcodes are automatically handled. For example, the xLOAD and xSTORE opcodes 033 * are automatically replaced by xLOAD_n and xSTORE_n opcodes when possible. The xLOAD_n and 034 * xSTORE_n opcodes are therefore not defined in this interface. Likewise for LDC, automatically 035 * replaced by LDC_W or LDC2_W when necessary, WIDE, GOTO_W and JSR_W. 036 * 037 * @see <a href="https://docs.oracle.com/javase/specs/jvms/se11/html/jvms-6.html">JVMS 6</a> 038 * @author Eric Bruneton 039 * @author Eugene Kuleshov 040 */ 041// DontCheck(InterfaceIsType): can't be fixed (for backward binary compatibility). 042public interface Opcodes { 043 044 // ASM API versions. 045 046 int ASM4 = 4 << 16 | 0 << 8; 047 int ASM5 = 5 << 16 | 0 << 8; 048 int ASM6 = 6 << 16 | 0 << 8; 049 int ASM7 = 7 << 16 | 0 << 8; 050 051 /* 052 * Internal flags used to redirect calls to deprecated methods. For instance, if a visitOldStuff 053 * method in API_OLD is deprecated and replaced with visitNewStuff in API_NEW, then the 054 * redirection should be done as follows: 055 * 056 * <pre> 057 * public class StuffVisitor { 058 * ... 059 * 060 * @Deprecated public void visitOldStuff(int arg, ...) { 061 * // SOURCE_DEPRECATED means "a call from a deprecated method using the old 'api' value". 062 * visitNewStuf(arg | (api < API_NEW ? SOURCE_DEPRECATED : 0), ...); 063 * } 064 * 065 * public void visitNewStuff(int argAndSource, ...) { 066 * if (api < API_NEW && (argAndSource & SOURCE_DEPRECATED) == 0) { 067 * visitOldStuff(argAndSource, ...); 068 * } else { 069 * int arg = argAndSource & ~SOURCE_MASK; 070 * [ do stuff ] 071 * } 072 * } 073 * } 074 * </pre> 075 * 076 * <p>If 'api' is equal to API_NEW, there are two cases: 077 * 078 * <ul> 079 * <li>call visitNewStuff: the redirection test is skipped and 'do stuff' is executed directly. 080 * <li>call visitOldSuff: the source is not set to SOURCE_DEPRECATED before calling 081 * visitNewStuff, but the redirection test is skipped anyway in visitNewStuff, which 082 * directly executes 'do stuff'. 083 * </ul> 084 * 085 * <p>If 'api' is equal to API_OLD, there are two cases: 086 * 087 * <ul> 088 * <li>call visitOldSuff: the source is set to SOURCE_DEPRECATED before calling visitNewStuff. 089 * Because of this visitNewStuff does not redirect back to visitOldStuff, and instead 090 * executes 'do stuff'. 091 * <li>call visitNewStuff: the call is redirected to visitOldStuff because the source is 0. 092 * visitOldStuff now sets the source to SOURCE_DEPRECATED and calls visitNewStuff back. This 093 * time visitNewStuff does not redirect the call, and instead executes 'do stuff'. 094 * </ul> 095 * 096 * <h1>User subclasses</h1> 097 * 098 * <p>If a user subclass overrides one of these methods, there are only two cases: either 'api' is 099 * API_OLD and visitOldStuff is overridden (and visitNewStuff is not), or 'api' is API_NEW or 100 * more, and visitNewStuff is overridden (and visitOldStuff is not). Any other case is a user 101 * programming error. 102 * 103 * <p>If 'api' is equal to API_NEW, the class hierarchy is equivalent to 104 * 105 * <pre> 106 * public class StuffVisitor { 107 * @Deprecated public void visitOldStuff(int arg, ...) { visitNewStuf(arg, ...); } 108 * public void visitNewStuff(int arg, ...) { [ do stuff ] } 109 * } 110 * class UserStuffVisitor extends StuffVisitor { 111 * @Override public void visitNewStuff(int arg, ...) { 112 * super.visitNewStuff(int arg, ...); // optional 113 * [ do user stuff ] 114 * } 115 * } 116 * </pre> 117 * 118 * <p>It is then obvious that whether visitNewStuff or visitOldStuff is called, 'do stuff' and 'do 119 * user stuff' will be executed, in this order. 120 * 121 * <p>If 'api' is equal to API_OLD, the class hierarchy is equivalent to 122 * 123 * <pre> 124 * public class StuffVisitor { 125 * @Deprecated public void visitOldStuff(int arg, ...) { 126 * visitNewStuf(arg | SOURCE_DEPRECATED, ...); 127 * } 128 * public void visitNewStuff(int argAndSource...) { 129 * if ((argAndSource & SOURCE_DEPRECATED) == 0) { 130 * visitOldStuff(argAndSource, ...); 131 * } else { 132 * int arg = argAndSource & ~SOURCE_MASK; 133 * [ do stuff ] 134 * } 135 * } 136 * } 137 * class UserStuffVisitor extends StuffVisitor { 138 * @Override public void visitOldStuff(int arg, ...) { 139 * super.visitOldStuff(int arg, ...); // optional 140 * [ do user stuff ] 141 * } 142 * } 143 * </pre> 144 * 145 * <p>and there are two cases: 146 * 147 * <ul> 148 * <li>call visitOldSuff: in the call to super.visitOldStuff, the source is set to 149 * SOURCE_DEPRECATED and visitNewStuff is called. Here 'do stuff' is run because the source 150 * was previously set to SOURCE_DEPRECATED, and execution eventually returns to 151 * UserStuffVisitor.visitOldStuff, where 'do user stuff' is run. 152 * <li>call visitNewStuff: the call is redirected to UserStuffVisitor.visitOldStuff because the 153 * source is 0. Execution continues as in the previous case, resulting in 'do stuff' and 'do 154 * user stuff' being executed, in this order. 155 * </ul> 156 * 157 * <h1>ASM subclasses</h1> 158 * 159 * <p>In ASM packages, subclasses of StuffVisitor can typically be sub classed again by the user, 160 * and can be used with API_OLD or API_NEW. Because of this, if such a subclass must override 161 * visitNewStuff, it must do so in the following way (and must not override visitOldStuff): 162 * 163 * <pre> 164 * public class AsmStuffVisitor extends StuffVisitor { 165 * @Override public void visitNewStuff(int argAndSource, ...) { 166 * if (api < API_NEW && (argAndSource & SOURCE_DEPRECATED) == 0) { 167 * super.visitNewStuff(argAndSource, ...); 168 * return; 169 * } 170 * super.visitNewStuff(argAndSource, ...); // optional 171 * int arg = argAndSource & ~SOURCE_MASK; 172 * [ do other stuff ] 173 * } 174 * } 175 * </pre> 176 * 177 * <p>If a user class extends this with 'api' equal to API_NEW, the class hierarchy is equivalent 178 * to 179 * 180 * <pre> 181 * public class StuffVisitor { 182 * @Deprecated public void visitOldStuff(int arg, ...) { visitNewStuf(arg, ...); } 183 * public void visitNewStuff(int arg, ...) { [ do stuff ] } 184 * } 185 * public class AsmStuffVisitor extends StuffVisitor { 186 * @Override public void visitNewStuff(int arg, ...) { 187 * super.visitNewStuff(arg, ...); 188 * [ do other stuff ] 189 * } 190 * } 191 * class UserStuffVisitor extends StuffVisitor { 192 * @Override public void visitNewStuff(int arg, ...) { 193 * super.visitNewStuff(int arg, ...); 194 * [ do user stuff ] 195 * } 196 * } 197 * </pre> 198 * 199 * <p>It is then obvious that whether visitNewStuff or visitOldStuff is called, 'do stuff', 'do 200 * other stuff' and 'do user stuff' will be executed, in this order. If, on the other hand, a user 201 * class extends AsmStuffVisitor with 'api' equal to API_OLD, the class hierarchy is equivalent to 202 * 203 * <pre> 204 * public class StuffVisitor { 205 * @Deprecated public void visitOldStuff(int arg, ...) { 206 * visitNewStuf(arg | SOURCE_DEPRECATED, ...); 207 * } 208 * public void visitNewStuff(int argAndSource, ...) { 209 * if ((argAndSource & SOURCE_DEPRECATED) == 0) { 210 * visitOldStuff(argAndSource, ...); 211 * } else { 212 * int arg = argAndSource & ~SOURCE_MASK; 213 * [ do stuff ] 214 * } 215 * } 216 * } 217 * public class AsmStuffVisitor extends StuffVisitor { 218 * @Override public void visitNewStuff(int argAndSource, ...) { 219 * if ((argAndSource & SOURCE_DEPRECATED) == 0) { 220 * super.visitNewStuff(argAndSource, ...); 221 * return; 222 * } 223 * super.visitNewStuff(argAndSource, ...); // optional 224 * int arg = argAndSource & ~SOURCE_MASK; 225 * [ do other stuff ] 226 * } 227 * } 228 * class UserStuffVisitor extends StuffVisitor { 229 * @Override public void visitOldStuff(int arg, ...) { 230 * super.visitOldStuff(arg, ...); 231 * [ do user stuff ] 232 * } 233 * } 234 * </pre> 235 * 236 * <p>and, here again, whether visitNewStuff or visitOldStuff is called, 'do stuff', 'do other 237 * stuff' and 'do user stuff' will be executed, in this order (exercise left to the reader). 238 * 239 * <h1>Notes</h1> 240 * 241 * <ul> 242 * <li>the SOURCE_DEPRECATED flag is set only if 'api' is API_OLD, just before calling 243 * visitNewStuff. By hypothesis, this method is not overridden by the user. Therefore, user 244 * classes can never see this flag. Only ASM subclasses must take care of extracting the 245 * actual argument value by clearing the source flags. 246 * <li>because the SOURCE_DEPRECATED flag is immediately cleared in the caller, the caller can 247 * call visitOldStuff or visitNewStuff (in 'do stuff' and 'do user stuff') on a delegate 248 * visitor without any risks (breaking the redirection logic, "leaking" the flag, etc). 249 * <li>all the scenarios discussed above are unit tested in MethodVisitorTest. 250 * </ul> 251 */ 252 253 int SOURCE_DEPRECATED = 0x100; 254 int SOURCE_MASK = SOURCE_DEPRECATED; 255 256 // Java ClassFile versions (the minor version is stored in the 16 most significant bits, and the 257 // major version in the 16 least significant bits). 258 259 int V1_1 = 3 << 16 | 45; 260 int V1_2 = 0 << 16 | 46; 261 int V1_3 = 0 << 16 | 47; 262 int V1_4 = 0 << 16 | 48; 263 int V1_5 = 0 << 16 | 49; 264 int V1_6 = 0 << 16 | 50; 265 int V1_7 = 0 << 16 | 51; 266 int V1_8 = 0 << 16 | 52; 267 int V9 = 0 << 16 | 53; 268 int V10 = 0 << 16 | 54; 269 int V11 = 0 << 16 | 55; 270 int V12 = 0 << 16 | 56; 271 int V13 = 0 << 16 | 57; 272 273 /** 274 * Version flag indicating that the class is using 'preview' features. 275 * 276 * <p>{@code version & V_PREVIEW == V_PREVIEW} tests if a version is flagged with {@code 277 * V_PREVIEW}. 278 */ 279 int V_PREVIEW = 0xFFFF0000; 280 281 // Access flags values, defined in 282 // - https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html#jvms-4.1-200-E.1 283 // - https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html#jvms-4.5-200-A.1 284 // - https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html#jvms-4.6-200-A.1 285 // - https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html#jvms-4.7.25 286 287 int ACC_PUBLIC = 0x0001; // class, field, method 288 int ACC_PRIVATE = 0x0002; // class, field, method 289 int ACC_PROTECTED = 0x0004; // class, field, method 290 int ACC_STATIC = 0x0008; // field, method 291 int ACC_FINAL = 0x0010; // class, field, method, parameter 292 int ACC_SUPER = 0x0020; // class 293 int ACC_SYNCHRONIZED = 0x0020; // method 294 int ACC_OPEN = 0x0020; // module 295 int ACC_TRANSITIVE = 0x0020; // module requires 296 int ACC_VOLATILE = 0x0040; // field 297 int ACC_BRIDGE = 0x0040; // method 298 int ACC_STATIC_PHASE = 0x0040; // module requires 299 int ACC_VARARGS = 0x0080; // method 300 int ACC_TRANSIENT = 0x0080; // field 301 int ACC_NATIVE = 0x0100; // method 302 int ACC_INTERFACE = 0x0200; // class 303 int ACC_ABSTRACT = 0x0400; // class, method 304 int ACC_STRICT = 0x0800; // method 305 int ACC_SYNTHETIC = 0x1000; // class, field, method, parameter, module * 306 int ACC_ANNOTATION = 0x2000; // class 307 int ACC_ENUM = 0x4000; // class(?) field inner 308 int ACC_MANDATED = 0x8000; // parameter, module, module * 309 int ACC_MODULE = 0x8000; // class 310 311 // ASM specific access flags. 312 // WARNING: the 16 least significant bits must NOT be used, to avoid conflicts with standard 313 // access flags, and also to make sure that these flags are automatically filtered out when 314 // written in class files (because access flags are stored using 16 bits only). 315 316 int ACC_DEPRECATED = 0x20000; // class, field, method 317 318 // Possible values for the type operand of the NEWARRAY instruction. 319 // See https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-6.html#jvms-6.5.newarray. 320 321 int T_BOOLEAN = 4; 322 int T_CHAR = 5; 323 int T_FLOAT = 6; 324 int T_DOUBLE = 7; 325 int T_BYTE = 8; 326 int T_SHORT = 9; 327 int T_INT = 10; 328 int T_LONG = 11; 329 330 // Possible values for the reference_kind field of CONSTANT_MethodHandle_info structures. 331 // See https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html#jvms-4.4.8. 332 333 int H_GETFIELD = 1; 334 int H_GETSTATIC = 2; 335 int H_PUTFIELD = 3; 336 int H_PUTSTATIC = 4; 337 int H_INVOKEVIRTUAL = 5; 338 int H_INVOKESTATIC = 6; 339 int H_INVOKESPECIAL = 7; 340 int H_NEWINVOKESPECIAL = 8; 341 int H_INVOKEINTERFACE = 9; 342 343 // ASM specific stack map frame types, used in {@link ClassVisitor#visitFrame}. 344 345 /** An expanded frame. See {@link ClassReader#EXPAND_FRAMES}. */ 346 int F_NEW = -1; 347 348 /** A compressed frame with complete frame data. */ 349 int F_FULL = 0; 350 351 /** 352 * A compressed frame where locals are the same as the locals in the previous frame, except that 353 * additional 1-3 locals are defined, and with an empty stack. 354 */ 355 int F_APPEND = 1; 356 357 /** 358 * A compressed frame where locals are the same as the locals in the previous frame, except that 359 * the last 1-3 locals are absent and with an empty stack. 360 */ 361 int F_CHOP = 2; 362 363 /** 364 * A compressed frame with exactly the same locals as the previous frame and with an empty stack. 365 */ 366 int F_SAME = 3; 367 368 /** 369 * A compressed frame with exactly the same locals as the previous frame and with a single value 370 * on the stack. 371 */ 372 int F_SAME1 = 4; 373 374 // Standard stack map frame element types, used in {@link ClassVisitor#visitFrame}. 375 376 Integer TOP = Frame.ITEM_TOP; 377 Integer INTEGER = Frame.ITEM_INTEGER; 378 Integer FLOAT = Frame.ITEM_FLOAT; 379 Integer DOUBLE = Frame.ITEM_DOUBLE; 380 Integer LONG = Frame.ITEM_LONG; 381 Integer NULL = Frame.ITEM_NULL; 382 Integer UNINITIALIZED_THIS = Frame.ITEM_UNINITIALIZED_THIS; 383 384 // The JVM opcode values (with the MethodVisitor method name used to visit them in comment, and 385 // where '-' means 'same method name as on the previous line'). 386 // See https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-6.html. 387 388 int NOP = 0; // visitInsn 389 int ACONST_NULL = 1; // - 390 int ICONST_M1 = 2; // - 391 int ICONST_0 = 3; // - 392 int ICONST_1 = 4; // - 393 int ICONST_2 = 5; // - 394 int ICONST_3 = 6; // - 395 int ICONST_4 = 7; // - 396 int ICONST_5 = 8; // - 397 int LCONST_0 = 9; // - 398 int LCONST_1 = 10; // - 399 int FCONST_0 = 11; // - 400 int FCONST_1 = 12; // - 401 int FCONST_2 = 13; // - 402 int DCONST_0 = 14; // - 403 int DCONST_1 = 15; // - 404 int BIPUSH = 16; // visitIntInsn 405 int SIPUSH = 17; // - 406 int LDC = 18; // visitLdcInsn 407 int ILOAD = 21; // visitVarInsn 408 int LLOAD = 22; // - 409 int FLOAD = 23; // - 410 int DLOAD = 24; // - 411 int ALOAD = 25; // - 412 int IALOAD = 46; // visitInsn 413 int LALOAD = 47; // - 414 int FALOAD = 48; // - 415 int DALOAD = 49; // - 416 int AALOAD = 50; // - 417 int BALOAD = 51; // - 418 int CALOAD = 52; // - 419 int SALOAD = 53; // - 420 int ISTORE = 54; // visitVarInsn 421 int LSTORE = 55; // - 422 int FSTORE = 56; // - 423 int DSTORE = 57; // - 424 int ASTORE = 58; // - 425 int IASTORE = 79; // visitInsn 426 int LASTORE = 80; // - 427 int FASTORE = 81; // - 428 int DASTORE = 82; // - 429 int AASTORE = 83; // - 430 int BASTORE = 84; // - 431 int CASTORE = 85; // - 432 int SASTORE = 86; // - 433 int POP = 87; // - 434 int POP2 = 88; // - 435 int DUP = 89; // - 436 int DUP_X1 = 90; // - 437 int DUP_X2 = 91; // - 438 int DUP2 = 92; // - 439 int DUP2_X1 = 93; // - 440 int DUP2_X2 = 94; // - 441 int SWAP = 95; // - 442 int IADD = 96; // - 443 int LADD = 97; // - 444 int FADD = 98; // - 445 int DADD = 99; // - 446 int ISUB = 100; // - 447 int LSUB = 101; // - 448 int FSUB = 102; // - 449 int DSUB = 103; // - 450 int IMUL = 104; // - 451 int LMUL = 105; // - 452 int FMUL = 106; // - 453 int DMUL = 107; // - 454 int IDIV = 108; // - 455 int LDIV = 109; // - 456 int FDIV = 110; // - 457 int DDIV = 111; // - 458 int IREM = 112; // - 459 int LREM = 113; // - 460 int FREM = 114; // - 461 int DREM = 115; // - 462 int INEG = 116; // - 463 int LNEG = 117; // - 464 int FNEG = 118; // - 465 int DNEG = 119; // - 466 int ISHL = 120; // - 467 int LSHL = 121; // - 468 int ISHR = 122; // - 469 int LSHR = 123; // - 470 int IUSHR = 124; // - 471 int LUSHR = 125; // - 472 int IAND = 126; // - 473 int LAND = 127; // - 474 int IOR = 128; // - 475 int LOR = 129; // - 476 int IXOR = 130; // - 477 int LXOR = 131; // - 478 int IINC = 132; // visitIincInsn 479 int I2L = 133; // visitInsn 480 int I2F = 134; // - 481 int I2D = 135; // - 482 int L2I = 136; // - 483 int L2F = 137; // - 484 int L2D = 138; // - 485 int F2I = 139; // - 486 int F2L = 140; // - 487 int F2D = 141; // - 488 int D2I = 142; // - 489 int D2L = 143; // - 490 int D2F = 144; // - 491 int I2B = 145; // - 492 int I2C = 146; // - 493 int I2S = 147; // - 494 int LCMP = 148; // - 495 int FCMPL = 149; // - 496 int FCMPG = 150; // - 497 int DCMPL = 151; // - 498 int DCMPG = 152; // - 499 int IFEQ = 153; // visitJumpInsn 500 int IFNE = 154; // - 501 int IFLT = 155; // - 502 int IFGE = 156; // - 503 int IFGT = 157; // - 504 int IFLE = 158; // - 505 int IF_ICMPEQ = 159; // - 506 int IF_ICMPNE = 160; // - 507 int IF_ICMPLT = 161; // - 508 int IF_ICMPGE = 162; // - 509 int IF_ICMPGT = 163; // - 510 int IF_ICMPLE = 164; // - 511 int IF_ACMPEQ = 165; // - 512 int IF_ACMPNE = 166; // - 513 int GOTO = 167; // - 514 int JSR = 168; // - 515 int RET = 169; // visitVarInsn 516 int TABLESWITCH = 170; // visiTableSwitchInsn 517 int LOOKUPSWITCH = 171; // visitLookupSwitch 518 int IRETURN = 172; // visitInsn 519 int LRETURN = 173; // - 520 int FRETURN = 174; // - 521 int DRETURN = 175; // - 522 int ARETURN = 176; // - 523 int RETURN = 177; // - 524 int GETSTATIC = 178; // visitFieldInsn 525 int PUTSTATIC = 179; // - 526 int GETFIELD = 180; // - 527 int PUTFIELD = 181; // - 528 int INVOKEVIRTUAL = 182; // visitMethodInsn 529 int INVOKESPECIAL = 183; // - 530 int INVOKESTATIC = 184; // - 531 int INVOKEINTERFACE = 185; // - 532 int INVOKEDYNAMIC = 186; // visitInvokeDynamicInsn 533 int NEW = 187; // visitTypeInsn 534 int NEWARRAY = 188; // visitIntInsn 535 int ANEWARRAY = 189; // visitTypeInsn 536 int ARRAYLENGTH = 190; // visitInsn 537 int ATHROW = 191; // - 538 int CHECKCAST = 192; // visitTypeInsn 539 int INSTANCEOF = 193; // - 540 int MONITORENTER = 194; // visitInsn 541 int MONITOREXIT = 195; // - 542 int MULTIANEWARRAY = 197; // visitMultiANewArrayInsn 543 int IFNULL = 198; // visitJumpInsn 544 int IFNONNULL = 199; // - 545}