aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/apache/bcel/generic/Instruction.java
blob: a94896886ed95840a4912c74bfe4a58ab5d4708c (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*
 * 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 java.io.DataOutputStream;
import java.io.IOException;

import org.apache.bcel.Const;
import org.apache.bcel.classfile.ConstantPool;
import org.apache.bcel.util.ByteSequence;

/**
 * Abstract super class for all Java byte codes.
 *
 * @version $Id$
 */
public abstract class Instruction implements Cloneable {

    /**
     * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
     */
    @Deprecated
    protected short length = 1; // Length of instruction in bytes

    /**
     * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
     */
    @Deprecated
    protected short opcode = -1; // Opcode number

    private static InstructionComparator cmp = InstructionComparator.DEFAULT;


    /**
     * Empty constructor needed for Instruction.readInstruction.
     * Not to be used otherwise.
     */
    Instruction() {
    }


    public Instruction(final short opcode, final short length) {
        this.length = length;
        this.opcode = opcode;
    }


    /**
     * Dump instruction as byte code to stream out.
     * @param out Output stream
     */
    public void dump( final DataOutputStream out ) throws IOException {
        out.writeByte(opcode); // Common for all instructions
    }


    /** @return name of instruction, i.e., opcode name
     */
    public String getName() {
        return Const.getOpcodeName(opcode);
    }


    /**
     * Long output format:
     *
     * <name of opcode> "["<opcode number>"]"
     * "("<length of instruction>")"
     *
     * @param verbose long/short format switch
     * @return mnemonic for instruction
     */
    public String toString( final boolean verbose ) {
        if (verbose) {
            return getName() + "[" + opcode + "](" + length + ")";
        }
        return getName();
    }


    /**
     * @return mnemonic for instruction in verbose format
     */
    @Override
    public String toString() {
        return toString(true);
    }


    /**
     * @return mnemonic for instruction with sumbolic references resolved
     */
    public String toString( final ConstantPool cp ) {
        return toString(false);
    }


    /**
     * Use with caution, since `BranchInstruction's have a `target' reference which
     * is not copied correctly (only basic types are). This also applies for
     * `Select' instructions with their multiple branch targets.
     *
     * @see BranchInstruction
     * @return (shallow) copy of an instruction
     */
    public Instruction copy() {
        Instruction i = null;
        // "Constant" instruction, no need to duplicate
        if (InstructionConst.getInstruction(this.getOpcode()) != null) {
            i = this;
        } else {
            try {
                i = (Instruction) clone();
            } catch (final CloneNotSupportedException e) {
                System.err.println(e);
            }
        }
        return i;
    }


    /**
     * Read needed data (e.g. index) from file.
     *
     * @param bytes byte sequence to read from
     * @param wide "wide" instruction flag
     * @throws IOException may be thrown if the implementation needs to read data from the file
     */
    protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException {
    }


    /**
     * Read an instruction from (byte code) input stream and return the
     * appropiate object.
     * <p>
     * If the Instruction is defined in {@link InstructionConst}, then the
     * singleton instance is returned.
     * @param bytes input stream bytes
     * @return instruction object being read
     * @see InstructionConst#getInstruction(int)
     */
    // @since 6.0 no longer final
    public static Instruction readInstruction( final ByteSequence bytes ) throws IOException {
        boolean wide = false;
        short opcode = (short) bytes.readUnsignedByte();
        Instruction obj = null;
        if (opcode == Const.WIDE) { // Read next opcode after wide byte
            wide = true;
            opcode = (short) bytes.readUnsignedByte();
        }
        final Instruction instruction = InstructionConst.getInstruction(opcode);
        if (instruction != null) {
            return instruction; // Used predefined immutable object, if available
        }

        switch (opcode) {
            case Const.BIPUSH:
                obj = new BIPUSH();
                break;
            case Const.SIPUSH:
                obj = new SIPUSH();
                break;
            case Const.LDC:
                obj = new LDC();
                break;
            case Const.LDC_W:
                obj = new LDC_W();
                break;
            case Const.LDC2_W:
                obj = new LDC2_W();
                break;
            case Const.ILOAD:
                obj = new ILOAD();
                break;
            case Const.LLOAD:
                obj = new LLOAD();
                break;
            case Const.FLOAD:
                obj = new FLOAD();
                break;
            case Const.DLOAD:
                obj = new DLOAD();
                break;
            case Const.ALOAD:
                obj = new ALOAD();
                break;
            case Const.ILOAD_0:
                obj = new ILOAD(0);
                break;
            case Const.ILOAD_1:
                obj = new ILOAD(1);
                break;
            case Const.ILOAD_2:
                obj = new ILOAD(2);
                break;
            case Const.ILOAD_3:
                obj = new ILOAD(3);
                break;
            case Const.LLOAD_0:
                obj = new LLOAD(0);
                break;
            case Const.LLOAD_1:
                obj = new LLOAD(1);
                break;
            case Const.LLOAD_2:
                obj = new LLOAD(2);
                break;
            case Const.LLOAD_3:
                obj = new LLOAD(3);
                break;
            case Const.FLOAD_0:
                obj = new FLOAD(0);
                break;
            case Const.FLOAD_1:
                obj = new FLOAD(1);
                break;
            case Const.FLOAD_2:
                obj = new FLOAD(2);
                break;
            case Const.FLOAD_3:
                obj = new FLOAD(3);
                break;
            case Const.DLOAD_0:
                obj = new DLOAD(0);
                break;
            case Const.DLOAD_1:
                obj = new DLOAD(1);
                break;
            case Const.DLOAD_2:
                obj = new DLOAD(2);
                break;
            case Const.DLOAD_3:
                obj = new DLOAD(3);
                break;
            case Const.ALOAD_0:
                obj = new ALOAD(0);
                break;
            case Const.ALOAD_1:
                obj = new ALOAD(1);
                break;
            case Const.ALOAD_2:
                obj = new ALOAD(2);
                break;
            case Const.ALOAD_3:
                obj = new ALOAD(3);
                break;
            case Const.ISTORE:
                obj = new ISTORE();
                break;
            case Const.LSTORE:
                obj = new LSTORE();
                break;
            case Const.FSTORE:
                obj = new FSTORE();
                break;
            case Const.DSTORE:
                obj = new DSTORE();
                break;
            case Const.ASTORE:
                obj = new ASTORE();
                break;
            case Const.ISTORE_0:
                obj = new ISTORE(0);
                break;
            case Const.ISTORE_1:
                obj = new ISTORE(1);
                break;
            case Const.ISTORE_2:
                obj = new ISTORE(2);
                break;
            case Const.ISTORE_3:
                obj = new ISTORE(3);
                break;
            case Const.LSTORE_0:
                obj = new LSTORE(0);
                break;
            case Const.LSTORE_1:
                obj = new LSTORE(1);
                break;
            case Const.LSTORE_2:
                obj = new LSTORE(2);
                break;
            case Const.LSTORE_3:
                obj = new LSTORE(3);
                break;
            case Const.FSTORE_0:
                obj = new FSTORE(0);
                break;
            case Const.FSTORE_1:
                obj = new FSTORE(1);
                break;
            case Const.FSTORE_2:
                obj = new FSTORE(2);
                break;
            case Const.FSTORE_3:
                obj = new FSTORE(3);
                break;
            case Const.DSTORE_0:
                obj = new DSTORE(0);
                break;
            case Const.DSTORE_1:
                obj = new DSTORE(1);
                break;
            case Const.DSTORE_2:
                obj = new DSTORE(2);
                break;
            case Const.DSTORE_3:
                obj = new DSTORE(3);
                break;
            case Const.ASTORE_0:
                obj = new ASTORE(0);
                break;
            case Const.ASTORE_1:
                obj = new ASTORE(1);
                break;
            case Const.ASTORE_2:
                obj = new ASTORE(2);
                break;
            case Const.ASTORE_3:
                obj = new ASTORE(3);
                break;
            case Const.IINC:
                obj = new IINC();
                break;
            case Const.IFEQ:
                obj = new IFEQ();
                break;
            case Const.IFNE:
                obj = new IFNE();
                break;
            case Const.IFLT:
                obj = new IFLT();
                break;
            case Const.IFGE:
                obj = new IFGE();
                break;
            case Const.IFGT:
                obj = new IFGT();
                break;
            case Const.IFLE:
                obj = new IFLE();
                break;
            case Const.IF_ICMPEQ:
                obj = new IF_ICMPEQ();
                break;
            case Const.IF_ICMPNE:
                obj = new IF_ICMPNE();
                break;
            case Const.IF_ICMPLT:
                obj = new IF_ICMPLT();
                break;
            case Const.IF_ICMPGE:
                obj = new IF_ICMPGE();
                break;
            case Const.IF_ICMPGT:
                obj = new IF_ICMPGT();
                break;
            case Const.IF_ICMPLE:
                obj = new IF_ICMPLE();
                break;
            case Const.IF_ACMPEQ:
                obj = new IF_ACMPEQ();
                break;
            case Const.IF_ACMPNE:
                obj = new IF_ACMPNE();
                break;
            case Const.GOTO:
                obj = new GOTO();
                break;
            case Const.JSR:
                obj = new JSR();
                break;
            case Const.RET:
                obj = new RET();
                break;
            case Const.TABLESWITCH:
                obj = new TABLESWITCH();
                break;
            case Const.LOOKUPSWITCH:
                obj = new LOOKUPSWITCH();
                break;
            case Const.GETSTATIC:
                obj = new GETSTATIC();
                break;
            case Const.PUTSTATIC:
                obj = new PUTSTATIC();
                break;
            case Const.GETFIELD:
                obj = new GETFIELD();
                break;
            case Const.PUTFIELD:
                obj = new PUTFIELD();
                break;
            case Const.INVOKEVIRTUAL:
                obj = new INVOKEVIRTUAL();
                break;
            case Const.INVOKESPECIAL:
                obj = new INVOKESPECIAL();
                break;
            case Const.INVOKESTATIC:
                obj = new INVOKESTATIC();
                break;
            case Const.INVOKEINTERFACE:
                obj = new INVOKEINTERFACE();
                break;
            case Const.INVOKEDYNAMIC:
                obj = new INVOKEDYNAMIC();
                break;
            case Const.NEW:
                obj = new NEW();
                break;
            case Const.NEWARRAY:
                obj = new NEWARRAY();
                break;
            case Const.ANEWARRAY:
                obj = new ANEWARRAY();
                break;
            case Const.CHECKCAST:
                obj = new CHECKCAST();
                break;
            case Const.INSTANCEOF:
                obj = new INSTANCEOF();
                break;
            case Const.MULTIANEWARRAY:
                obj = new MULTIANEWARRAY();
                break;
            case Const.IFNULL:
                obj = new IFNULL();
                break;
            case Const.IFNONNULL:
                obj = new IFNONNULL();
                break;
            case Const.GOTO_W:
                obj = new GOTO_W();
                break;
            case Const.JSR_W:
                obj = new JSR_W();
                break;
            case Const.BREAKPOINT:
                obj = new BREAKPOINT();
                break;
            case Const.IMPDEP1:
                obj = new IMPDEP1();
                break;
            case Const.IMPDEP2:
                obj = new IMPDEP2();
                break;
            default:
                throw new ClassGenException("Illegal opcode detected: " + opcode);

        }

        if (wide
                && !((obj instanceof LocalVariableInstruction) || (obj instanceof IINC) || (obj instanceof RET))) {
            throw new ClassGenException("Illegal opcode after wide: " + opcode);
        }
        obj.setOpcode(opcode);
        obj.initFromFile(bytes, wide); // Do further initializations, if any
        return obj;
    }

    /**
     * This method also gives right results for instructions whose
     * effect on the stack depends on the constant pool entry they
     * reference.
     *  @return Number of words consumed from stack by this instruction,
     * or Constants.UNPREDICTABLE, if this can not be computed statically
     */
    public int consumeStack( final ConstantPoolGen cpg ) {
        return Const.getConsumeStack(opcode);
    }


    /**
     * This method also gives right results for instructions whose
     * effect on the stack depends on the constant pool entry they
     * reference.
     * @return Number of words produced onto stack by this instruction,
     * or Constants.UNPREDICTABLE, if this can not be computed statically
     */
    public int produceStack( final ConstantPoolGen cpg ) {
        return Const.getProduceStack(opcode);
    }


    /**
     * @return this instructions opcode
     */
    public short getOpcode() {
        return opcode;
    }


    /**
     * @return length (in bytes) of instruction
     */
    public int getLength() {
        return length;
    }


    /**
     * Needed in readInstruction and subclasses in this package
     */
    final void setOpcode( final short opcode ) {
        this.opcode = opcode;
    }


    /**
     * Needed in readInstruction and subclasses in this package
     * @since 6.0
     */
    final void setLength( final int length ) {
        this.length = (short) length; // TODO check range?
    }


    /** Some instructions may be reused, so don't do anything by default.
     */
    void dispose() {
    }


    /**
     * Call corresponding visitor method(s). The order is:
     * Call visitor methods of implemented interfaces first, then
     * call methods according to the class hierarchy in descending order,
     * i.e., the most specific visitXXX() call comes last.
     *
     * @param v Visitor object
     */
    public abstract void accept( Visitor v );


    /** Get Comparator object used in the equals() method to determine
     * equality of instructions.
     *
     * @return currently used comparator for equals()
     * @deprecated (6.0) use the built in comparator, or wrap this class in another object that implements these methods
     */
    @Deprecated
    public static InstructionComparator getComparator() {
        return cmp;
    }


    /** Set comparator to be used for equals().
      * @deprecated (6.0) use the built in comparator, or wrap this class in another object that implements these methods
     */
    @Deprecated
    public static void setComparator( final InstructionComparator c ) {
        cmp = c;
    }


    /** Check for equality, delegated to comparator
     * @return true if that is an Instruction and has the same opcode
     */
    @Override
    public boolean equals( final Object that ) {
        return (that instanceof Instruction) ? cmp.equals(this, (Instruction) that) : false;
    }

    /** calculate the hashCode of this object
     * @return the hashCode
     * @since 6.0
     */
    @Override
    public int hashCode() {
        return opcode;
    }

    /**
     * Check if the value can fit in a byte (signed)
     * @param value the value to check
     * @return true if the value is in range
     * @since 6.0
     */
    public static boolean isValidByte(final int value) {
        return value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE;
    }

    /**
     * Check if the value can fit in a short (signed)
     * @param value the value to check
     * @return true if the value is in range
     * @since 6.0
     */
    public static boolean isValidShort(final int value) {
        return value >= Short.MIN_VALUE && value <= Short.MAX_VALUE;
    }
}