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