1 /* 2 * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.java.util.jar.pack; 27 28 import java.util.Arrays; 29 import java.util.List; 30 31 /** 32 * Shared constants 33 * @author John Rose 34 */ 35 class Constants { 36 37 private Constants(){} 38 39 public static final int JAVA_MAGIC = 0xCAFEBABE; 40 41 /* 42 Java Class Version numbers history 43 1.0 to 1.3.X 45,3 44 1.4 to 1.4.X 46,0 45 1.5 to 1.5.X 49,0 46 1.6 to 1.6.X 50,0 47 1.7 to 1.7.X 51,0 48 1.8 to 1.8.X 52,0 49 1.9 to 1.9.X 53,0 50 1.10 to 1.10.X 54,0 51 1.11 to 1.11.X 55,0 52 1.12 to 1.12.X 56,0 53 1.13 to 1.13.X 57,0 54 1.14 to 1.14.X 58,0 55 1.15 to 1.15.X 59,0 56 */ 57 58 public static final Package.Version JAVA_MIN_CLASS_VERSION = 59 Package.Version.of(45, 03); 60 61 public static final Package.Version JAVA5_MAX_CLASS_VERSION = 62 Package.Version.of(49, 00); 63 64 public static final Package.Version JAVA6_MAX_CLASS_VERSION = 65 Package.Version.of(50, 00); 66 67 public static final Package.Version JAVA7_MAX_CLASS_VERSION = 68 Package.Version.of(51, 00); 69 70 public static final Package.Version JAVA8_MAX_CLASS_VERSION = 71 Package.Version.of(52, 00); 72 73 public static final Package.Version JAVA9_MAX_CLASS_VERSION = 74 Package.Version.of(53, 00); 75 76 public static final Package.Version JAVA10_MAX_CLASS_VERSION = 77 Package.Version.of(54, 00); 78 79 public static final Package.Version JAVA11_MAX_CLASS_VERSION = 80 Package.Version.of(55, 00); 81 82 public static final Package.Version JAVA12_MAX_CLASS_VERSION = 83 Package.Version.of(56, 00); 84 85 public static final Package.Version JAVA13_MAX_CLASS_VERSION = 86 Package.Version.of(57, 00); 87 88 public static final Package.Version JAVA14_MAX_CLASS_VERSION = 89 Package.Version.of(58, 00); 90 91 public static final int JAVA_PACKAGE_MAGIC = 0xCAFED00D; 92 93 public static final Package.Version JAVA5_PACKAGE_VERSION = 94 Package.Version.of(150, 7); 95 96 public static final Package.Version JAVA6_PACKAGE_VERSION = 97 Package.Version.of(160, 1); 98 99 public static final Package.Version JAVA7_PACKAGE_VERSION = 100 Package.Version.of(170, 1); 101 102 public static final Package.Version JAVA8_PACKAGE_VERSION = 103 Package.Version.of(171, 0); 104 105 // upper limit, should point to the latest class version 106 public static final Package.Version JAVA_MAX_CLASS_VERSION = 107 JAVA13_MAX_CLASS_VERSION; 108 109 // upper limit should point to the latest package version, for version info!. 110 public static final Package.Version MAX_PACKAGE_VERSION = 111 JAVA7_PACKAGE_VERSION; 112 113 public static final int CONSTANT_POOL_INDEX_LIMIT = 0x10000; 114 public static final int CONSTANT_POOL_NARROW_LIMIT = 0x00100; 115 116 public static final String JAVA_SIGNATURE_CHARS = "BSCIJFDZLV(["; 117 118 public static final byte CONSTANT_Utf8 = 1; 119 public static final byte CONSTANT_unused2 = 2; // unused, was Unicode 120 public static final byte CONSTANT_Integer = 3; 121 public static final byte CONSTANT_Float = 4; 122 public static final byte CONSTANT_Long = 5; 123 public static final byte CONSTANT_Double = 6; 124 public static final byte CONSTANT_Class = 7; 125 public static final byte CONSTANT_String = 8; 126 public static final byte CONSTANT_Fieldref = 9; 127 public static final byte CONSTANT_Methodref = 10; 128 public static final byte CONSTANT_InterfaceMethodref = 11; 129 public static final byte CONSTANT_NameandType = 12; 130 public static final byte CONSTANT_unused13 = 13; 131 public static final byte CONSTANT_unused14 = 14; 132 public static final byte CONSTANT_MethodHandle = 15; 133 public static final byte CONSTANT_MethodType = 16; 134 public static final byte CONSTANT_unused17 = 17; // unused 135 public static final byte CONSTANT_InvokeDynamic = 18; 136 137 // pseudo-constants: 138 public static final byte CONSTANT_None = 0; 139 public static final byte CONSTANT_Signature = CONSTANT_unused13; 140 public static final byte CONSTANT_BootstrapMethod = CONSTANT_unused17; // used only in InvokeDynamic constants 141 public static final byte CONSTANT_Limit = 19; 142 143 public static final byte CONSTANT_All = 50; // combined global map 144 public static final byte CONSTANT_LoadableValue = 51; // used for 'KL' and qldc operands 145 public static final byte CONSTANT_AnyMember = 52; // union of refs to field or (interface) method 146 public static final byte CONSTANT_FieldSpecific = 53; // used only for 'KQ' ConstantValue attrs 147 public static final byte CONSTANT_GroupFirst = CONSTANT_All; 148 public static final byte CONSTANT_GroupLimit = CONSTANT_FieldSpecific+1; 149 150 // CONSTANT_MethodHandle reference kinds 151 public static final byte REF_getField = 1; 152 public static final byte REF_getStatic = 2; 153 public static final byte REF_putField = 3; 154 public static final byte REF_putStatic = 4; 155 public static final byte REF_invokeVirtual = 5; 156 public static final byte REF_invokeStatic = 6; 157 public static final byte REF_invokeSpecial = 7; 158 public static final byte REF_newInvokeSpecial = 8; 159 public static final byte REF_invokeInterface = 9; 160 161 // pseudo-access bits 162 public static final int ACC_IC_LONG_FORM = (1<<16); //for ic_flags 163 164 // attribute "context types" 165 public static final int ATTR_CONTEXT_CLASS = 0; 166 public static final int ATTR_CONTEXT_FIELD = 1; 167 public static final int ATTR_CONTEXT_METHOD = 2; 168 public static final int ATTR_CONTEXT_CODE = 3; 169 public static final int ATTR_CONTEXT_LIMIT = 4; 170 public static final String[] ATTR_CONTEXT_NAME 171 = { "class", "field", "method", "code" }; 172 173 // predefined attr bits 174 public static final int 175 X_ATTR_OVERFLOW = 16, 176 CLASS_ATTR_SourceFile = 17, 177 METHOD_ATTR_Code = 17, 178 FIELD_ATTR_ConstantValue = 17, 179 CLASS_ATTR_EnclosingMethod = 18, 180 METHOD_ATTR_Exceptions = 18, 181 X_ATTR_Signature = 19, 182 X_ATTR_Deprecated = 20, 183 X_ATTR_RuntimeVisibleAnnotations = 21, 184 X_ATTR_RuntimeInvisibleAnnotations = 22, 185 METHOD_ATTR_RuntimeVisibleParameterAnnotations = 23, 186 CLASS_ATTR_InnerClasses = 23, 187 METHOD_ATTR_RuntimeInvisibleParameterAnnotations = 24, 188 CLASS_ATTR_ClassFile_version = 24, 189 METHOD_ATTR_AnnotationDefault = 25, 190 METHOD_ATTR_MethodParameters = 26, // JDK8 191 X_ATTR_RuntimeVisibleTypeAnnotations = 27, // JDK8 192 X_ATTR_RuntimeInvisibleTypeAnnotations = 28, // JDK8 193 CODE_ATTR_StackMapTable = 0, // new in Java 6 194 CODE_ATTR_LineNumberTable = 1, 195 CODE_ATTR_LocalVariableTable = 2, 196 CODE_ATTR_LocalVariableTypeTable = 3; 197 198 // File option bits, from LSB in ascending bit position. 199 public static final int FO_DEFLATE_HINT = 1<<0; 200 public static final int FO_IS_CLASS_STUB = 1<<1; 201 202 // Archive option bits, from LSB in ascending bit position: 203 public static final int AO_HAVE_SPECIAL_FORMATS = 1<<0; 204 public static final int AO_HAVE_CP_NUMBERS = 1<<1; 205 public static final int AO_HAVE_ALL_CODE_FLAGS = 1<<2; 206 public static final int AO_HAVE_CP_EXTRAS = 1<<3; 207 public static final int AO_HAVE_FILE_HEADERS = 1<<4; 208 public static final int AO_DEFLATE_HINT = 1<<5; 209 public static final int AO_HAVE_FILE_MODTIME = 1<<6; 210 public static final int AO_HAVE_FILE_OPTIONS = 1<<7; 211 public static final int AO_HAVE_FILE_SIZE_HI = 1<<8; 212 public static final int AO_HAVE_CLASS_FLAGS_HI = 1<<9; 213 public static final int AO_HAVE_FIELD_FLAGS_HI = 1<<10; 214 public static final int AO_HAVE_METHOD_FLAGS_HI = 1<<11; 215 public static final int AO_HAVE_CODE_FLAGS_HI = 1<<12; 216 public static final int AO_UNUSED_MBZ = (-1)<<13; // option bits reserved for future use 217 218 public static final int LG_AO_HAVE_XXX_FLAGS_HI = 9; 219 220 // visitRefs modes: 221 static final int VRM_CLASSIC = 0; 222 static final int VRM_PACKAGE = 1; 223 224 public static final int NO_MODTIME = 0; // null modtime value 225 226 // some comstantly empty containers 227 public static final int[] noInts = {}; 228 public static final byte[] noBytes = {}; 229 public static final Object[] noValues = {}; 230 public static final String[] noStrings = {}; 231 public static final List<Object> emptyList = Arrays.asList(noValues); 232 233 // meta-coding 234 public static final int 235 _meta_default = 0, 236 _meta_canon_min = 1, 237 _meta_canon_max = 115, 238 _meta_arb = 116, 239 _meta_run = 117, 240 _meta_pop = 141, 241 _meta_limit = 189; 242 243 // bytecodes 244 public static final int 245 _nop = 0, // 0x00 246 _aconst_null = 1, // 0x01 247 _iconst_m1 = 2, // 0x02 248 _iconst_0 = 3, // 0x03 249 _iconst_1 = 4, // 0x04 250 _iconst_2 = 5, // 0x05 251 _iconst_3 = 6, // 0x06 252 _iconst_4 = 7, // 0x07 253 _iconst_5 = 8, // 0x08 254 _lconst_0 = 9, // 0x09 255 _lconst_1 = 10, // 0x0a 256 _fconst_0 = 11, // 0x0b 257 _fconst_1 = 12, // 0x0c 258 _fconst_2 = 13, // 0x0d 259 _dconst_0 = 14, // 0x0e 260 _dconst_1 = 15, // 0x0f 261 _bipush = 16, // 0x10 262 _sipush = 17, // 0x11 263 _ldc = 18, // 0x12 264 _ldc_w = 19, // 0x13 265 _ldc2_w = 20, // 0x14 266 _iload = 21, // 0x15 267 _lload = 22, // 0x16 268 _fload = 23, // 0x17 269 _dload = 24, // 0x18 270 _aload = 25, // 0x19 271 _iload_0 = 26, // 0x1a 272 _iload_1 = 27, // 0x1b 273 _iload_2 = 28, // 0x1c 274 _iload_3 = 29, // 0x1d 275 _lload_0 = 30, // 0x1e 276 _lload_1 = 31, // 0x1f 277 _lload_2 = 32, // 0x20 278 _lload_3 = 33, // 0x21 279 _fload_0 = 34, // 0x22 280 _fload_1 = 35, // 0x23 281 _fload_2 = 36, // 0x24 282 _fload_3 = 37, // 0x25 283 _dload_0 = 38, // 0x26 284 _dload_1 = 39, // 0x27 285 _dload_2 = 40, // 0x28 286 _dload_3 = 41, // 0x29 287 _aload_0 = 42, // 0x2a 288 _aload_1 = 43, // 0x2b 289 _aload_2 = 44, // 0x2c 290 _aload_3 = 45, // 0x2d 291 _iaload = 46, // 0x2e 292 _laload = 47, // 0x2f 293 _faload = 48, // 0x30 294 _daload = 49, // 0x31 295 _aaload = 50, // 0x32 296 _baload = 51, // 0x33 297 _caload = 52, // 0x34 298 _saload = 53, // 0x35 299 _istore = 54, // 0x36 300 _lstore = 55, // 0x37 301 _fstore = 56, // 0x38 302 _dstore = 57, // 0x39 303 _astore = 58, // 0x3a 304 _istore_0 = 59, // 0x3b 305 _istore_1 = 60, // 0x3c 306 _istore_2 = 61, // 0x3d 307 _istore_3 = 62, // 0x3e 308 _lstore_0 = 63, // 0x3f 309 _lstore_1 = 64, // 0x40 310 _lstore_2 = 65, // 0x41 311 _lstore_3 = 66, // 0x42 312 _fstore_0 = 67, // 0x43 313 _fstore_1 = 68, // 0x44 314 _fstore_2 = 69, // 0x45 315 _fstore_3 = 70, // 0x46 316 _dstore_0 = 71, // 0x47 317 _dstore_1 = 72, // 0x48 318 _dstore_2 = 73, // 0x49 319 _dstore_3 = 74, // 0x4a 320 _astore_0 = 75, // 0x4b 321 _astore_1 = 76, // 0x4c 322 _astore_2 = 77, // 0x4d 323 _astore_3 = 78, // 0x4e 324 _iastore = 79, // 0x4f 325 _lastore = 80, // 0x50 326 _fastore = 81, // 0x51 327 _dastore = 82, // 0x52 328 _aastore = 83, // 0x53 329 _bastore = 84, // 0x54 330 _castore = 85, // 0x55 331 _sastore = 86, // 0x56 332 _pop = 87, // 0x57 333 _pop2 = 88, // 0x58 334 _dup = 89, // 0x59 335 _dup_x1 = 90, // 0x5a 336 _dup_x2 = 91, // 0x5b 337 _dup2 = 92, // 0x5c 338 _dup2_x1 = 93, // 0x5d 339 _dup2_x2 = 94, // 0x5e 340 _swap = 95, // 0x5f 341 _iadd = 96, // 0x60 342 _ladd = 97, // 0x61 343 _fadd = 98, // 0x62 344 _dadd = 99, // 0x63 345 _isub = 100, // 0x64 346 _lsub = 101, // 0x65 347 _fsub = 102, // 0x66 348 _dsub = 103, // 0x67 349 _imul = 104, // 0x68 350 _lmul = 105, // 0x69 351 _fmul = 106, // 0x6a 352 _dmul = 107, // 0x6b 353 _idiv = 108, // 0x6c 354 _ldiv = 109, // 0x6d 355 _fdiv = 110, // 0x6e 356 _ddiv = 111, // 0x6f 357 _irem = 112, // 0x70 358 _lrem = 113, // 0x71 359 _frem = 114, // 0x72 360 _drem = 115, // 0x73 361 _ineg = 116, // 0x74 362 _lneg = 117, // 0x75 363 _fneg = 118, // 0x76 364 _dneg = 119, // 0x77 365 _ishl = 120, // 0x78 366 _lshl = 121, // 0x79 367 _ishr = 122, // 0x7a 368 _lshr = 123, // 0x7b 369 _iushr = 124, // 0x7c 370 _lushr = 125, // 0x7d 371 _iand = 126, // 0x7e 372 _land = 127, // 0x7f 373 _ior = 128, // 0x80 374 _lor = 129, // 0x81 375 _ixor = 130, // 0x82 376 _lxor = 131, // 0x83 377 _iinc = 132, // 0x84 378 _i2l = 133, // 0x85 379 _i2f = 134, // 0x86 380 _i2d = 135, // 0x87 381 _l2i = 136, // 0x88 382 _l2f = 137, // 0x89 383 _l2d = 138, // 0x8a 384 _f2i = 139, // 0x8b 385 _f2l = 140, // 0x8c 386 _f2d = 141, // 0x8d 387 _d2i = 142, // 0x8e 388 _d2l = 143, // 0x8f 389 _d2f = 144, // 0x90 390 _i2b = 145, // 0x91 391 _i2c = 146, // 0x92 392 _i2s = 147, // 0x93 393 _lcmp = 148, // 0x94 394 _fcmpl = 149, // 0x95 395 _fcmpg = 150, // 0x96 396 _dcmpl = 151, // 0x97 397 _dcmpg = 152, // 0x98 398 _ifeq = 153, // 0x99 399 _ifne = 154, // 0x9a 400 _iflt = 155, // 0x9b 401 _ifge = 156, // 0x9c 402 _ifgt = 157, // 0x9d 403 _ifle = 158, // 0x9e 404 _if_icmpeq = 159, // 0x9f 405 _if_icmpne = 160, // 0xa0 406 _if_icmplt = 161, // 0xa1 407 _if_icmpge = 162, // 0xa2 408 _if_icmpgt = 163, // 0xa3 409 _if_icmple = 164, // 0xa4 410 _if_acmpeq = 165, // 0xa5 411 _if_acmpne = 166, // 0xa6 412 _goto = 167, // 0xa7 413 _jsr = 168, // 0xa8 414 _ret = 169, // 0xa9 415 _tableswitch = 170, // 0xaa 416 _lookupswitch = 171, // 0xab 417 _ireturn = 172, // 0xac 418 _lreturn = 173, // 0xad 419 _freturn = 174, // 0xae 420 _dreturn = 175, // 0xaf 421 _areturn = 176, // 0xb0 422 _return = 177, // 0xb1 423 _getstatic = 178, // 0xb2 424 _putstatic = 179, // 0xb3 425 _getfield = 180, // 0xb4 426 _putfield = 181, // 0xb5 427 _invokevirtual = 182, // 0xb6 428 _invokespecial = 183, // 0xb7 429 _invokestatic = 184, // 0xb8 430 _invokeinterface = 185, // 0xb9 431 _invokedynamic = 186, // 0xba 432 _new = 187, // 0xbb 433 _newarray = 188, // 0xbc 434 _anewarray = 189, // 0xbd 435 _arraylength = 190, // 0xbe 436 _athrow = 191, // 0xbf 437 _checkcast = 192, // 0xc0 438 _instanceof = 193, // 0xc1 439 _monitorenter = 194, // 0xc2 440 _monitorexit = 195, // 0xc3 441 _wide = 196, // 0xc4 442 _multianewarray = 197, // 0xc5 443 _ifnull = 198, // 0xc6 444 _ifnonnull = 199, // 0xc7 445 _goto_w = 200, // 0xc8 446 _jsr_w = 201, // 0xc9 447 _bytecode_limit = 202; // 0xca 448 449 // End marker, used to terminate bytecode sequences: 450 public static final int _end_marker = 255; 451 // Escapes: 452 public static final int _byte_escape = 254; 453 public static final int _ref_escape = 253; 454 455 // Self-relative pseudo-opcodes for better compression. 456 // A "linker op" is a bytecode which links to a class member. 457 // (But in what follows, "invokeinterface" ops are excluded.) 458 // 459 // A "self linker op" is a variant bytecode which works only 460 // with the current class or its super. Because the number of 461 // possible targets is small, it admits a more compact encoding. 462 // Self linker ops are allowed to absorb a previous "aload_0" op. 463 // There are (7 * 4) self linker ops (super or not, aload_0 or not). 464 // 465 // For simplicity, we define the full symmetric set of variants. 466 // However, some of them are relatively useless. 467 // Self linker ops are enabled by Pack.selfCallVariants (true). 468 public static final int _first_linker_op = _getstatic; 469 public static final int _last_linker_op = _invokestatic; 470 public static final int _num_linker_ops = (_last_linker_op - _first_linker_op) + 1; 471 public static final int _self_linker_op = _bytecode_limit; 472 public static final int _self_linker_aload_flag = 1*_num_linker_ops; 473 public static final int _self_linker_super_flag = 2*_num_linker_ops; 474 public static final int _self_linker_limit = _self_linker_op + 4*_num_linker_ops; 475 // An "invoke init" op is a variant of invokespecial which works 476 // only with the method name "<init>". There are variants which 477 // link to the current class, the super class, or the class of the 478 // immediately previous "newinstance" op. There are 3 of these ops. 479 // They all take method signature references as operands. 480 // Invoke init ops are enabled by Pack.initCallVariants (true). 481 public static final int _invokeinit_op = _self_linker_limit; 482 public static final int _invokeinit_self_option = 0; 483 public static final int _invokeinit_super_option = 1; 484 public static final int _invokeinit_new_option = 2; 485 public static final int _invokeinit_limit = _invokeinit_op+3; 486 487 public static final int _pseudo_instruction_limit = _invokeinit_limit; 488 // linker variant limit == 202+(7*4)+3 == 233 489 490 // Ldc variants support strongly typed references to constants. 491 // This lets us index constant pool entries completely according to tag, 492 // which is a great simplification. 493 // Ldc variants gain us only 0.007% improvement in compression ratio, 494 // but they simplify the file format greatly. 495 public static final int _xldc_op = _invokeinit_limit; 496 public static final int _sldc = _ldc; // previously named _aldc 497 public static final int _cldc = _xldc_op+0; 498 public static final int _ildc = _xldc_op+1; 499 public static final int _fldc = _xldc_op+2; 500 public static final int _sldc_w = _ldc_w; // previously named _aldc_w 501 public static final int _cldc_w = _xldc_op+3; 502 public static final int _ildc_w = _xldc_op+4; 503 public static final int _fldc_w = _xldc_op+5; 504 public static final int _lldc2_w = _ldc2_w; 505 public static final int _dldc2_w = _xldc_op+6; 506 // anything other than primitive, string, or class must be handled with qldc: 507 public static final int _qldc = _xldc_op+7; 508 public static final int _qldc_w = _xldc_op+8; 509 public static final int _xldc_limit = _xldc_op+9; 510 511 // handling of InterfaceMethodRef 512 public static final int _invoke_int_op = _xldc_limit; 513 public static final int _invokespecial_int = _invoke_int_op+0; 514 public static final int _invokestatic_int = _invoke_int_op+1; 515 public static final int _invoke_int_limit = _invoke_int_op+2; 516 }