aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/apache/bcel/generic/InstructionConst.java
blob: 3450c9317e5bd80ea26728f5a008ad92b4f74f5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.bcel.generic;

import org.apache.bcel.Const;

/** 
 * This interface contains shareable instruction objects.
 *
 * In order to save memory you can use some instructions multiply,
 * since they have an immutable state and are directly derived from
 * Instruction.  I.e. they have no instance fields that could be
 * changed. Since some of these instructions like ICONST_0 occur
 * very frequently this can save a lot of time and space. This
 * feature is an adaptation of the FlyWeight design pattern, we
 * just use an array instead of a factory.
 *
 * The Instructions can also accessed directly under their names, so
 * it's possible to write il.append(Instruction.ICONST_0);
 *
 * @version $Id: InstructionConstants.java 1695415 2015-08-12 01:02:39Z chas $
 */
public final class InstructionConst {

    /** 
     * Predefined instruction objects
     */
    /*
     * NOTE these are not currently immutable, because Instruction
     * has mutable protected fields opcode and length.
     */
    public static final Instruction NOP = new NOP();
    public static final Instruction ACONST_NULL = new ACONST_NULL();
    public static final Instruction ICONST_M1 = new ICONST(-1);
    public static final Instruction ICONST_0 = new ICONST(0);
    public static final Instruction ICONST_1 = new ICONST(1);
    public static final Instruction ICONST_2 = new ICONST(2);
    public static final Instruction ICONST_3 = new ICONST(3);
    public static final Instruction ICONST_4 = new ICONST(4);
    public static final Instruction ICONST_5 = new ICONST(5);
    public static final Instruction LCONST_0 = new LCONST(0);
    public static final Instruction LCONST_1 = new LCONST(1);
    public static final Instruction FCONST_0 = new FCONST(0);
    public static final Instruction FCONST_1 = new FCONST(1);
    public static final Instruction FCONST_2 = new FCONST(2);
    public static final Instruction DCONST_0 = new DCONST(0);
    public static final Instruction DCONST_1 = new DCONST(1);
    public static final ArrayInstruction IALOAD = new IALOAD();
    public static final ArrayInstruction LALOAD = new LALOAD();
    public static final ArrayInstruction FALOAD = new FALOAD();
    public static final ArrayInstruction DALOAD = new DALOAD();
    public static final ArrayInstruction AALOAD = new AALOAD();
    public static final ArrayInstruction BALOAD = new BALOAD();
    public static final ArrayInstruction CALOAD = new CALOAD();
    public static final ArrayInstruction SALOAD = new SALOAD();
    public static final ArrayInstruction IASTORE = new IASTORE();
    public static final ArrayInstruction LASTORE = new LASTORE();
    public static final ArrayInstruction FASTORE = new FASTORE();
    public static final ArrayInstruction DASTORE = new DASTORE();
    public static final ArrayInstruction AASTORE = new AASTORE();
    public static final ArrayInstruction BASTORE = new BASTORE();
    public static final ArrayInstruction CASTORE = new CASTORE();
    public static final ArrayInstruction SASTORE = new SASTORE();
    public static final StackInstruction POP = new POP();
    public static final StackInstruction POP2 = new POP2();
    public static final StackInstruction DUP = new DUP();
    public static final StackInstruction DUP_X1 = new DUP_X1();
    public static final StackInstruction DUP_X2 = new DUP_X2();
    public static final StackInstruction DUP2 = new DUP2();
    public static final StackInstruction DUP2_X1 = new DUP2_X1();
    public static final StackInstruction DUP2_X2 = new DUP2_X2();
    public static final StackInstruction SWAP = new SWAP();
    public static final ArithmeticInstruction IADD = new IADD();
    public static final ArithmeticInstruction LADD = new LADD();
    public static final ArithmeticInstruction FADD = new FADD();
    public static final ArithmeticInstruction DADD = new DADD();
    public static final ArithmeticInstruction ISUB = new ISUB();
    public static final ArithmeticInstruction LSUB = new LSUB();
    public static final ArithmeticInstruction FSUB = new FSUB();
    public static final ArithmeticInstruction DSUB = new DSUB();
    public static final ArithmeticInstruction IMUL = new IMUL();
    public static final ArithmeticInstruction LMUL = new LMUL();
    public static final ArithmeticInstruction FMUL = new FMUL();
    public static final ArithmeticInstruction DMUL = new DMUL();
    public static final ArithmeticInstruction IDIV = new IDIV();
    public static final ArithmeticInstruction LDIV = new LDIV();
    public static final ArithmeticInstruction FDIV = new FDIV();
    public static final ArithmeticInstruction DDIV = new DDIV();
    public static final ArithmeticInstruction IREM = new IREM();
    public static final ArithmeticInstruction LREM = new LREM();
    public static final ArithmeticInstruction FREM = new FREM();
    public static final ArithmeticInstruction DREM = new DREM();
    public static final ArithmeticInstruction INEG = new INEG();
    public static final ArithmeticInstruction LNEG = new LNEG();
    public static final ArithmeticInstruction FNEG = new FNEG();
    public static final ArithmeticInstruction DNEG = new DNEG();
    public static final ArithmeticInstruction ISHL = new ISHL();
    public static final ArithmeticInstruction LSHL = new LSHL();
    public static final ArithmeticInstruction ISHR = new ISHR();
    public static final ArithmeticInstruction LSHR = new LSHR();
    public static final ArithmeticInstruction IUSHR = new IUSHR();
    public static final ArithmeticInstruction LUSHR = new LUSHR();
    public static final ArithmeticInstruction IAND = new IAND();
    public static final ArithmeticInstruction LAND = new LAND();
    public static final ArithmeticInstruction IOR = new IOR();
    public static final ArithmeticInstruction LOR = new LOR();
    public static final ArithmeticInstruction IXOR = new IXOR();
    public static final ArithmeticInstruction LXOR = new LXOR();
    public static final ConversionInstruction I2L = new I2L();
    public static final ConversionInstruction I2F = new I2F();
    public static final ConversionInstruction I2D = new I2D();
    public static final ConversionInstruction L2I = new L2I();
    public static final ConversionInstruction L2F = new L2F();
    public static final ConversionInstruction L2D = new L2D();
    public static final ConversionInstruction F2I = new F2I();
    public static final ConversionInstruction F2L = new F2L();
    public static final ConversionInstruction F2D = new F2D();
    public static final ConversionInstruction D2I = new D2I();
    public static final ConversionInstruction D2L = new D2L();
    public static final ConversionInstruction D2F = new D2F();
    public static final ConversionInstruction I2B = new I2B();
    public static final ConversionInstruction I2C = new I2C();
    public static final ConversionInstruction I2S = new I2S();
    public static final Instruction LCMP = new LCMP();
    public static final Instruction FCMPL = new FCMPL();
    public static final Instruction FCMPG = new FCMPG();
    public static final Instruction DCMPL = new DCMPL();
    public static final Instruction DCMPG = new DCMPG();
    public static final ReturnInstruction IRETURN = new IRETURN();
    public static final ReturnInstruction LRETURN = new LRETURN();
    public static final ReturnInstruction FRETURN = new FRETURN();
    public static final ReturnInstruction DRETURN = new DRETURN();
    public static final ReturnInstruction ARETURN = new ARETURN();
    public static final ReturnInstruction RETURN = new RETURN();
    public static final Instruction ARRAYLENGTH = new ARRAYLENGTH();
    public static final Instruction ATHROW = new ATHROW();
    public static final Instruction MONITORENTER = new MONITORENTER();
    public static final Instruction MONITOREXIT = new MONITOREXIT();

    /** You can use these constants in multiple places safely, if you can guarantee
     * that you will never alter their internal values, e.g. call setIndex().
     */
    public static final LocalVariableInstruction THIS = new ALOAD(0);
    public static final LocalVariableInstruction ALOAD_0 = THIS;
    public static final LocalVariableInstruction ALOAD_1 = new ALOAD(1);
    public static final LocalVariableInstruction ALOAD_2 = new ALOAD(2);
    public static final LocalVariableInstruction ILOAD_0 = new ILOAD(0);
    public static final LocalVariableInstruction ILOAD_1 = new ILOAD(1);
    public static final LocalVariableInstruction ILOAD_2 = new ILOAD(2);
    public static final LocalVariableInstruction ASTORE_0 = new ASTORE(0);
    public static final LocalVariableInstruction ASTORE_1 = new ASTORE(1);
    public static final LocalVariableInstruction ASTORE_2 = new ASTORE(2);
    public static final LocalVariableInstruction ISTORE_0 = new ISTORE(0);
    public static final LocalVariableInstruction ISTORE_1 = new ISTORE(1);
    public static final LocalVariableInstruction ISTORE_2 = new ISTORE(2);

    /** Get object via its opcode, for immutable instructions like
     * branch instructions entries are set to null.
     */
    private static final Instruction[] INSTRUCTIONS = new Instruction[256];

    static {
        INSTRUCTIONS[Const.NOP] = NOP;
        INSTRUCTIONS[Const.ACONST_NULL] = ACONST_NULL;
        INSTRUCTIONS[Const.ICONST_M1] = ICONST_M1;
        INSTRUCTIONS[Const.ICONST_0] = ICONST_0;
        INSTRUCTIONS[Const.ICONST_1] = ICONST_1;
        INSTRUCTIONS[Const.ICONST_2] = ICONST_2;
        INSTRUCTIONS[Const.ICONST_3] = ICONST_3;
        INSTRUCTIONS[Const.ICONST_4] = ICONST_4;
        INSTRUCTIONS[Const.ICONST_5] = ICONST_5;
        INSTRUCTIONS[Const.LCONST_0] = LCONST_0;
        INSTRUCTIONS[Const.LCONST_1] = LCONST_1;
        INSTRUCTIONS[Const.FCONST_0] = FCONST_0;
        INSTRUCTIONS[Const.FCONST_1] = FCONST_1;
        INSTRUCTIONS[Const.FCONST_2] = FCONST_2;
        INSTRUCTIONS[Const.DCONST_0] = DCONST_0;
        INSTRUCTIONS[Const.DCONST_1] = DCONST_1;
        INSTRUCTIONS[Const.IALOAD] = IALOAD;
        INSTRUCTIONS[Const.LALOAD] = LALOAD;
        INSTRUCTIONS[Const.FALOAD] = FALOAD;
        INSTRUCTIONS[Const.DALOAD] = DALOAD;
        INSTRUCTIONS[Const.AALOAD] = AALOAD;
        INSTRUCTIONS[Const.BALOAD] = BALOAD;
        INSTRUCTIONS[Const.CALOAD] = CALOAD;
        INSTRUCTIONS[Const.SALOAD] = SALOAD;
        INSTRUCTIONS[Const.IASTORE] = IASTORE;
        INSTRUCTIONS[Const.LASTORE] = LASTORE;
        INSTRUCTIONS[Const.FASTORE] = FASTORE;
        INSTRUCTIONS[Const.DASTORE] = DASTORE;
        INSTRUCTIONS[Const.AASTORE] = AASTORE;
        INSTRUCTIONS[Const.BASTORE] = BASTORE;
        INSTRUCTIONS[Const.CASTORE] = CASTORE;
        INSTRUCTIONS[Const.SASTORE] = SASTORE;
        INSTRUCTIONS[Const.POP] = POP;
        INSTRUCTIONS[Const.POP2] = POP2;
        INSTRUCTIONS[Const.DUP] = DUP;
        INSTRUCTIONS[Const.DUP_X1] = DUP_X1;
        INSTRUCTIONS[Const.DUP_X2] = DUP_X2;
        INSTRUCTIONS[Const.DUP2] = DUP2;
        INSTRUCTIONS[Const.DUP2_X1] = DUP2_X1;
        INSTRUCTIONS[Const.DUP2_X2] = DUP2_X2;
        INSTRUCTIONS[Const.SWAP] = SWAP;
        INSTRUCTIONS[Const.IADD] = IADD;
        INSTRUCTIONS[Const.LADD] = LADD;
        INSTRUCTIONS[Const.FADD] = FADD;
        INSTRUCTIONS[Const.DADD] = DADD;
        INSTRUCTIONS[Const.ISUB] = ISUB;
        INSTRUCTIONS[Const.LSUB] = LSUB;
        INSTRUCTIONS[Const.FSUB] = FSUB;
        INSTRUCTIONS[Const.DSUB] = DSUB;
        INSTRUCTIONS[Const.IMUL] = IMUL;
        INSTRUCTIONS[Const.LMUL] = LMUL;
        INSTRUCTIONS[Const.FMUL] = FMUL;
        INSTRUCTIONS[Const.DMUL] = DMUL;
        INSTRUCTIONS[Const.IDIV] = IDIV;
        INSTRUCTIONS[Const.LDIV] = LDIV;
        INSTRUCTIONS[Const.FDIV] = FDIV;
        INSTRUCTIONS[Const.DDIV] = DDIV;
        INSTRUCTIONS[Const.IREM] = IREM;
        INSTRUCTIONS[Const.LREM] = LREM;
        INSTRUCTIONS[Const.FREM] = FREM;
        INSTRUCTIONS[Const.DREM] = DREM;
        INSTRUCTIONS[Const.INEG] = INEG;
        INSTRUCTIONS[Const.LNEG] = LNEG;
        INSTRUCTIONS[Const.FNEG] = FNEG;
        INSTRUCTIONS[Const.DNEG] = DNEG;
        INSTRUCTIONS[Const.ISHL] = ISHL;
        INSTRUCTIONS[Const.LSHL] = LSHL;
        INSTRUCTIONS[Const.ISHR] = ISHR;
        INSTRUCTIONS[Const.LSHR] = LSHR;
        INSTRUCTIONS[Const.IUSHR] = IUSHR;
        INSTRUCTIONS[Const.LUSHR] = LUSHR;
        INSTRUCTIONS[Const.IAND] = IAND;
        INSTRUCTIONS[Const.LAND] = LAND;
        INSTRUCTIONS[Const.IOR] = IOR;
        INSTRUCTIONS[Const.LOR] = LOR;
        INSTRUCTIONS[Const.IXOR] = IXOR;
        INSTRUCTIONS[Const.LXOR] = LXOR;
        INSTRUCTIONS[Const.I2L] = I2L;
        INSTRUCTIONS[Const.I2F] = I2F;
        INSTRUCTIONS[Const.I2D] = I2D;
        INSTRUCTIONS[Const.L2I] = L2I;
        INSTRUCTIONS[Const.L2F] = L2F;
        INSTRUCTIONS[Const.L2D] = L2D;
        INSTRUCTIONS[Const.F2I] = F2I;
        INSTRUCTIONS[Const.F2L] = F2L;
        INSTRUCTIONS[Const.F2D] = F2D;
        INSTRUCTIONS[Const.D2I] = D2I;
        INSTRUCTIONS[Const.D2L] = D2L;
        INSTRUCTIONS[Const.D2F] = D2F;
        INSTRUCTIONS[Const.I2B] = I2B;
        INSTRUCTIONS[Const.I2C] = I2C;
        INSTRUCTIONS[Const.I2S] = I2S;
        INSTRUCTIONS[Const.LCMP] = LCMP;
        INSTRUCTIONS[Const.FCMPL] = FCMPL;
        INSTRUCTIONS[Const.FCMPG] = FCMPG;
        INSTRUCTIONS[Const.DCMPL] = DCMPL;
        INSTRUCTIONS[Const.DCMPG] = DCMPG;
        INSTRUCTIONS[Const.IRETURN] = IRETURN;
        INSTRUCTIONS[Const.LRETURN] = LRETURN;
        INSTRUCTIONS[Const.FRETURN] = FRETURN;
        INSTRUCTIONS[Const.DRETURN] = DRETURN;
        INSTRUCTIONS[Const.ARETURN] = ARETURN;
        INSTRUCTIONS[Const.RETURN] = RETURN;
        INSTRUCTIONS[Const.ARRAYLENGTH] = ARRAYLENGTH;
        INSTRUCTIONS[Const.ATHROW] = ATHROW;
        INSTRUCTIONS[Const.MONITORENTER] = MONITORENTER;
        INSTRUCTIONS[Const.MONITOREXIT] = MONITOREXIT;
    }

    private InstructionConst() { } // non-instantiable 

    /**
     * Gets the Instruction.
     * @param index the index, e.g. {@link Const#RETURN}
     * @return the entry from the private INSTRUCTIONS table
     */
    public static Instruction getInstruction(final int index) {
        return INSTRUCTIONS[index];
    }
}