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