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