jdk/src/java.base/share/classes/com/sun/java/util/jar/pack/Instruction.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 25859 3317bb8137f4
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
15526
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12544
diff changeset
     2
 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.java.util.jar.pack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
    28
import java.io.IOException;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 6900
diff changeset
    29
import java.util.Arrays;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 6900
diff changeset
    30
import static com.sun.java.util.jar.pack.Constants.*;
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
    31
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A parsed bytecode instruction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Provides accessors to various relevant bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author John Rose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 6900
diff changeset
    37
class Instruction  {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    protected byte[] bytes;  // bytecodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    protected int pc;        // location of this instruction
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    protected int bc;        // opcode of this instruction
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    protected int w;         // 0 if normal, 1 if a _wide prefix at pc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    protected int length;    // bytes in this instruction
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    protected boolean special;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    protected Instruction(byte[] bytes, int pc, int bc, int w, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        reset(bytes, pc, bc, w, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private void reset(byte[] bytes, int pc, int bc, int w, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        this.bytes = bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        this.pc = pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        this.bc = bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        this.w = w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        this.length = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public int getBC() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public boolean isWide() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return w != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public byte[] getBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public int getPC() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        return pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public int getLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public int getNextPC() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return pc + length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public Instruction next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        int npc = pc + length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (npc == bytes.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            return Instruction.at(bytes, npc, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public boolean isNonstandard() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return isNonstandard(bc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public void setNonstandardLength(int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        assert(isNonstandard());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        this.length = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /** A fake instruction at this pc whose next() will be at nextpc. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public Instruction forceNextPC(int nextpc) {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 6900
diff changeset
    96
        int llength = nextpc - pc;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 6900
diff changeset
    97
        return new Instruction(bytes, pc, -1, -1, llength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static Instruction at(byte[] bytes, int pc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return Instruction.at(bytes, pc, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public static Instruction at(byte[] bytes, int pc, Instruction reuse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        int bc = getByte(bytes, pc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        int prefix = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        int w = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        int length = BC_LENGTH[w][bc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            // Hard cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            switch (bc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            case _wide:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                bc = getByte(bytes, pc+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                w = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                length = BC_LENGTH[w][bc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                if (length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    // unknown instruction; treat as one byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    length = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            case _tableswitch:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                return new TableSwitch(bytes, pc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            case _lookupswitch:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                return new LookupSwitch(bytes, pc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                // unknown instruction; treat as one byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                length = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        assert(length > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        assert(pc+length <= bytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // Speed hack:  Instruction.next reuses self if possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (reuse != null && !reuse.special) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            reuse.reset(bytes, pc, bc, w, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            return reuse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return new Instruction(bytes, pc, bc, w, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    // Return the constant pool reference type, or 0 if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public byte getCPTag() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return BC_TAG[w][bc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    // Return the constant pool index, or -1 if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public int getCPIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        int indexLoc = BC_INDEX[w][bc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (indexLoc == 0)  return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        assert(w == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (length == 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            return getByte(bytes, pc+indexLoc);  // _ldc opcode only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return getShort(bytes, pc+indexLoc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public void setCPIndex(int cpi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        int indexLoc = BC_INDEX[w][bc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        assert(indexLoc != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (length == 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            setByte(bytes, pc+indexLoc, cpi);  // _ldc opcode only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            setShort(bytes, pc+indexLoc, cpi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        assert(getCPIndex() == cpi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public ConstantPool.Entry getCPRef(ConstantPool.Entry[] cpMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        int index = getCPIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return (index < 0) ? null : cpMap[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    // Return the slot of the affected local, or -1 if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public int getLocalSlot() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        int slotLoc = BC_SLOT[w][bc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (slotLoc == 0)  return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (w == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return getByte(bytes, pc+slotLoc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            return getShort(bytes, pc+slotLoc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    // Return the target of the branch, or -1 if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public int getBranchLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        int branchLoc = BC_BRANCH[w][bc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (branchLoc == 0)  return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        assert(w == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        assert(length == 3 || length == 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (length == 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            offset = (short)getShort(bytes, pc+branchLoc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            offset = getInt(bytes, pc+branchLoc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        assert(offset+pc >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        assert(offset+pc <= bytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return offset+pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public void setBranchLabel(int targetPC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        int branchLoc = BC_BRANCH[w][bc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        assert(branchLoc != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (length == 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            setShort(bytes, pc+branchLoc, targetPC-pc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            setInt(bytes, pc+branchLoc, targetPC-pc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        assert(targetPC == getBranchLabel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    // Return the trailing constant in the instruction (as a signed value).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    // Return 0 if there is none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public int getConstant() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        int conLoc = BC_CON[w][bc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (conLoc == 0)  return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        switch (length - conLoc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        case 1: return (byte) getByte(bytes, pc+conLoc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        case 2: return (short) getShort(bytes, pc+conLoc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        assert(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public void setConstant(int con) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        int conLoc = BC_CON[w][bc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        assert(conLoc != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        switch (length - conLoc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        case 1: setByte(bytes, pc+conLoc, con); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        case 2: setShort(bytes, pc+conLoc, con); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        assert(con == getConstant());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public abstract static class Switch extends Instruction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // Each case is a (value, label) pair, indexed 0 <= n < caseCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        public abstract int  getCaseCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        public abstract int  getCaseValue(int n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        public abstract int  getCaseLabel(int n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        public abstract void setCaseCount(int caseCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        public abstract void setCaseValue(int n, int value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        public abstract void setCaseLabel(int n, int targetPC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        protected abstract int getLength(int caseCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        public int  getDefaultLabel()             { return intAt(0)+pc; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        public void setDefaultLabel(int targetPC) { setIntAt(0, targetPC-pc); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        protected int apc;        // aligned pc (table base)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        protected int intAt(int n) { return getInt(bytes, apc + n*4); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        protected void setIntAt(int n, int x) { setInt(bytes, apc + n*4, x); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        protected Switch(byte[] bytes, int pc, int bc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            super(bytes, pc, bc, /*w*/0, /*length*/0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            this.apc = alignPC(pc+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            this.special = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            length = getLength(getCaseCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        public int getAlignedPC() { return apc; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            String s = super.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            s += " Default:"+labstr(getDefaultLabel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            int caseCount = getCaseCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            for (int i = 0; i < caseCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                s += "\n\tCase "+getCaseValue(i)+":"+labstr(getCaseLabel(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        public static int alignPC(int apc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            while (apc % 4 != 0)  ++apc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            return apc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public static class TableSwitch extends Switch {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        // apc:  (df, lo, hi, (hi-lo+1)*(label))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        public int getLowCase()        { return intAt(1); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        public int getHighCase()       { return intAt(2); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        public int getCaseCount()      { return intAt(2)-intAt(1)+1; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        public int getCaseValue(int n) { return getLowCase()+n; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        public int getCaseLabel(int n) { return intAt(3+n)+pc; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        public void setLowCase(int val)  { setIntAt(1, val); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        public void setHighCase(int val) { setIntAt(2, val); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        public void setCaseLabel(int n, int tpc) { setIntAt(3+n, tpc-pc); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        public void setCaseCount(int caseCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            setHighCase(getLowCase() + caseCount - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            length = getLength(caseCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        public void setCaseValue(int n, int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (n != 0)  throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            int caseCount = getCaseCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            setLowCase(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            setCaseCount(caseCount);  // keep invariant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        TableSwitch(byte[] bytes, int pc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            super(bytes, pc, _tableswitch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        protected int getLength(int caseCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            return (apc-pc) + (3 + caseCount) * 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public static class LookupSwitch extends Switch {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        // apc:  (df, nc, nc*(case, label))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        public int getCaseCount()      { return intAt(1); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        public int getCaseValue(int n) { return intAt(2+n*2+0); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        public int getCaseLabel(int n) { return intAt(2+n*2+1)+pc; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        public void setCaseCount(int caseCount)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            setIntAt(1, caseCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            length = getLength(caseCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        public void setCaseValue(int n, int val) { setIntAt(2+n*2+0, val); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        public void setCaseLabel(int n, int tpc) { setIntAt(2+n*2+1, tpc-pc); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        LookupSwitch(byte[] bytes, int pc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            super(bytes, pc, _lookupswitch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        protected int getLength(int caseCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            return (apc-pc) + (2 + caseCount*2) * 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 6900
diff changeset
   320
    /** Two instructions are equal if they have the same bytes. */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public boolean equals(Object o) {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 6900
diff changeset
   322
        return (o != null) && (o.getClass() == Instruction.class)
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 6900
diff changeset
   323
                && equals((Instruction) o);
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 6900
diff changeset
   324
    }
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 6900
diff changeset
   325
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 6900
diff changeset
   326
    public int hashCode() {
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 6900
diff changeset
   327
        int hash = 3;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 6900
diff changeset
   328
        hash = 11 * hash + Arrays.hashCode(this.bytes);
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 6900
diff changeset
   329
        hash = 11 * hash + this.pc;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 6900
diff changeset
   330
        hash = 11 * hash + this.bc;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 6900
diff changeset
   331
        hash = 11 * hash + this.w;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 6900
diff changeset
   332
        hash = 11 * hash + this.length;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 6900
diff changeset
   333
        return hash;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public boolean equals(Instruction that) {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 6900
diff changeset
   337
        if (this.pc != that.pc)            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (this.bc != that.bc)            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (this.w  != that.w)             return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        if (this.length  != that.length)   return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        for (int i = 1; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            if (this.bytes[this.pc+i] != that.bytes[that.pc+i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    static String labstr(int pc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        if (pc >= 0 && pc < 100000)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            return ((100000+pc)+"").substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return pc+"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return toString(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    public String toString(ConstantPool.Entry[] cpMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        String s = labstr(pc) + ": ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (bc >= _bytecode_limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            s += Integer.toHexString(bc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if (w == 1)  s += "wide ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        String bcname = (bc < BC_NAME.length)? BC_NAME[bc]: null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (bcname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            return s+"opcode#"+bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        s += bcname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        int tag = getCPTag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (tag != 0)  s += " "+ConstantPool.tagName(tag)+":";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        int idx = getCPIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (idx >= 0)  s += (cpMap == null) ? ""+idx : "="+cpMap[idx].stringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        int slt = getLocalSlot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (slt >= 0)  s += " Local:"+slt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        int lab = getBranchLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (lab >= 0)  s += " To:"+labstr(lab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        int con = getConstant();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (con != 0)  s += " Con:"+con;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    //public static byte constantPoolTagFor(int bc) { return BC_TAG[0][bc]; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    /// Fetching values from byte arrays:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    public int getIntAt(int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        return getInt(bytes, pc+off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    public int getShortAt(int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        return getShort(bytes, pc+off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    public int getByteAt(int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return getByte(bytes, pc+off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    public static int getInt(byte[] bytes, int pc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return (getShort(bytes, pc+0) << 16) + (getShort(bytes, pc+2) << 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public static int getShort(byte[] bytes, int pc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        return (getByte(bytes, pc+0) << 8) + (getByte(bytes, pc+1) << 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    public static int getByte(byte[] bytes, int pc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return bytes[pc] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    public static void setInt(byte[] bytes, int pc, int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        setShort(bytes, pc+0, x >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        setShort(bytes, pc+2, x >> 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public static void setShort(byte[] bytes, int pc, int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        setByte(bytes, pc+0, x >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        setByte(bytes, pc+1, x >> 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    public static void setByte(byte[] bytes, int pc, int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        bytes[pc] = (byte)x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    // some bytecode classifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    public static boolean isNonstandard(int bc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return BC_LENGTH[0][bc] < 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    public static int opLength(int bc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        int l = BC_LENGTH[0][bc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        assert(l > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        return l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    public static int opWideLength(int bc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        int l = BC_LENGTH[1][bc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        assert(l > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        return l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    public static boolean isLocalSlotOp(int bc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        return (bc < BC_SLOT[0].length && BC_SLOT[0][bc] > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    public static boolean isBranchOp(int bc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        return (bc < BC_BRANCH[0].length && BC_BRANCH[0][bc] > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    public static boolean isCPRefOp(int bc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        if (bc < BC_INDEX[0].length && BC_INDEX[0][bc] > 0)  return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        if (bc >= _xldc_op && bc < _xldc_limit)  return true;
16050
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 15526
diff changeset
   449
        if (bc == _invokespecial_int || bc == _invokestatic_int) return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    public static byte getCPRefOpTag(int bc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        if (bc < BC_INDEX[0].length && BC_INDEX[0][bc] > 0)  return BC_TAG[0][bc];
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   455
        if (bc >= _xldc_op && bc < _xldc_limit)  return CONSTANT_LoadableValue;
16050
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 15526
diff changeset
   456
        if (bc == _invokestatic_int || bc == _invokespecial_int) return CONSTANT_InterfaceMethodref;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        return CONSTANT_None;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public static boolean isFieldOp(int bc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        return (bc >= _getstatic && bc <= _putfield);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    public static boolean isInvokeInitOp(int bc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        return (bc >= _invokeinit_op && bc < _invokeinit_limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    public static boolean isSelfLinkerOp(int bc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        return (bc >= _self_linker_op && bc < _self_linker_limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /// Format definitions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   474
    private static final byte[][] BC_LENGTH  = new byte[2][0x100];
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   475
    private static final byte[][] BC_INDEX   = new byte[2][0x100];
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   476
    private static final byte[][] BC_TAG     = new byte[2][0x100];
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   477
    private static final byte[][] BC_BRANCH  = new byte[2][0x100];
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   478
    private static final byte[][] BC_SLOT    = new byte[2][0x100];
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   479
    private static final byte[][] BC_CON     = new byte[2][0x100];
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   480
    private static final String[] BC_NAME    = new String[0x100]; // debug only
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   481
    private static final String[][] BC_FORMAT  = new String[2][_bytecode_limit]; // debug only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        for (int i = 0; i < _bytecode_limit; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            BC_LENGTH[0][i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            BC_LENGTH[1][i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        def("b", _nop, _dconst_1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        def("bx", _bipush);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        def("bxx", _sipush);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        def("bk", _ldc);                                // do not pack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        def("bkk", _ldc_w, _ldc2_w);            // do not pack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        def("blwbll", _iload, _aload);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        def("b", _iload_0, _saload);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        def("blwbll", _istore, _astore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        def("b", _istore_0, _lxor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        def("blxwbllxx", _iinc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        def("b", _i2l, _dcmpg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        def("boo", _ifeq, _jsr);                        // pack oo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        def("blwbll", _ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        def("", _tableswitch, _lookupswitch);   // pack all ints, omit padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        def("b", _ireturn, _return);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        def("bkf", _getstatic, _putfield);              // pack kf (base=Field)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        def("bkm", _invokevirtual, _invokestatic);      // pack kn (base=Method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        def("bkixx", _invokeinterface);         // pack ki (base=IMethod), omit xx
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   505
        def("bkyxx", _invokedynamic);           // pack ky (base=Any), omit xx
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        def("bkc", _new);                               // pack kc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        def("bx", _newarray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        def("bkc", _anewarray);                 // pack kc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        def("b", _arraylength, _athrow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        def("bkc", _checkcast, _instanceof);    // pack kc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        def("b", _monitorenter, _monitorexit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        def("", _wide);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        def("bkcx", _multianewarray);           // pack kc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        def("boo", _ifnull, _ifnonnull);                // pack oo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        def("boooo", _goto_w, _jsr_w);          // pack oooo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        for (int i = 0; i < _bytecode_limit; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            //System.out.println(i+": l="+BC_LENGTH[0][i]+" i="+BC_INDEX[0][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            //assert(BC_LENGTH[0][i] != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            if (BC_LENGTH[0][i] == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                continue;  // unknown opcode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            // Have a complete mapping, to support spurious _wide prefixes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            if (BC_LENGTH[1][i] == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                BC_LENGTH[1][i] = (byte)(1+BC_LENGTH[0][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        String names =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
  "nop aconst_null iconst_m1 iconst_0 iconst_1 iconst_2 iconst_3 iconst_4 "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
  "iconst_5 lconst_0 lconst_1 fconst_0 fconst_1 fconst_2 dconst_0 dconst_1 "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
  "bipush sipush ldc ldc_w ldc2_w iload lload fload dload aload iload_0 "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
  "iload_1 iload_2 iload_3 lload_0 lload_1 lload_2 lload_3 fload_0 fload_1 "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
  "fload_2 fload_3 dload_0 dload_1 dload_2 dload_3 aload_0 aload_1 aload_2 "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
  "aload_3 iaload laload faload daload aaload baload caload saload istore "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
  "lstore fstore dstore astore istore_0 istore_1 istore_2 istore_3 lstore_0 "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
  "lstore_1 lstore_2 lstore_3 fstore_0 fstore_1 fstore_2 fstore_3 dstore_0 "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
  "dstore_1 dstore_2 dstore_3 astore_0 astore_1 astore_2 astore_3 iastore "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
  "lastore fastore dastore aastore bastore castore sastore pop pop2 dup "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
  "dup_x1 dup_x2 dup2 dup2_x1 dup2_x2 swap iadd ladd fadd dadd isub lsub "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
  "fsub dsub imul lmul fmul dmul idiv ldiv fdiv ddiv irem lrem frem drem "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
  "ineg lneg fneg dneg ishl lshl ishr lshr iushr lushr iand land ior lor "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
  "ixor lxor iinc i2l i2f i2d l2i l2f l2d f2i f2l f2d d2i d2l d2f i2b i2c "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
  "i2s lcmp fcmpl fcmpg dcmpl dcmpg ifeq ifne iflt ifge ifgt ifle if_icmpeq "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
  "if_icmpne if_icmplt if_icmpge if_icmpgt if_icmple if_acmpeq if_acmpne "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
  "goto jsr ret tableswitch lookupswitch ireturn lreturn freturn dreturn "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
  "areturn return getstatic putstatic getfield putfield invokevirtual "+
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   547
  "invokespecial invokestatic invokeinterface invokedynamic new newarray "+
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
  "anewarray arraylength athrow checkcast instanceof monitorenter "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
  "monitorexit wide multianewarray ifnull ifnonnull goto_w jsr_w ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        for (int bc = 0; names.length() > 0; bc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            int sp = names.indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            BC_NAME[bc] = names.substring(0, sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            names = names.substring(sp+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    public static String byteName(int bc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        String iname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        if (bc < BC_NAME.length && BC_NAME[bc] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            iname = BC_NAME[bc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        } else if (isSelfLinkerOp(bc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            int idx = (bc - _self_linker_op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            boolean isSuper = (idx >= _self_linker_super_flag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            if (isSuper)  idx -= _self_linker_super_flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            boolean isAload = (idx >= _self_linker_aload_flag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            if (isAload)  idx -= _self_linker_aload_flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            int origBC = _first_linker_op + idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            assert(origBC >= _first_linker_op && origBC <= _last_linker_op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            iname = BC_NAME[origBC];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            iname += (isSuper ? "_super" : "_this");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            if (isAload)  iname = "aload_0&" + iname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            iname = "*"+iname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        } else if (isInvokeInitOp(bc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            int idx = (bc - _invokeinit_op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            switch (idx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            case _invokeinit_self_option:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                iname = "*invokespecial_init_this"; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            case _invokeinit_super_option:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                iname = "*invokespecial_init_super"; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                assert(idx == _invokeinit_new_option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                iname = "*invokespecial_init_new"; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            switch (bc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            case _ildc:  iname = "*ildc"; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            case _fldc:  iname = "*fldc"; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            case _ildc_w:  iname = "*ildc_w"; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            case _fldc_w:  iname = "*fldc_w"; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            case _dldc2_w:  iname = "*dldc2_w"; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            case _cldc:  iname = "*cldc"; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            case _cldc_w:  iname = "*cldc_w"; break;
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   592
            case _qldc:  iname = "*qldc"; break;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   593
            case _qldc_w:  iname = "*qldc_w"; break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            case _byte_escape:  iname = "*byte_escape"; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            case _ref_escape:  iname = "*ref_escape"; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            case _end_marker:  iname = "*end"; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            default:  iname = "*bc#"+bc; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        return iname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    private static int BW = 4;  // width of classification field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    private static void def(String fmt, int bc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        def(fmt, bc, bc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    private static void def(String fmt, int from_bc, int to_bc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        String[] fmts = { fmt, null };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        if (fmt.indexOf('w') > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            fmts[1] = fmt.substring(fmt.indexOf('w'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            fmts[0] = fmt.substring(0, fmt.indexOf('w'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        for (int w = 0; w <= 1; w++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            fmt = fmts[w];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            if (fmt == null)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            int length = fmt.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            int index  = Math.max(0, fmt.indexOf('k'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            int tag    = CONSTANT_None;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            int branch = Math.max(0, fmt.indexOf('o'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            int slot   = Math.max(0, fmt.indexOf('l'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            int con    = Math.max(0, fmt.indexOf('x'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            if (index > 0 && index+1 < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                switch (fmt.charAt(index+1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    case 'c': tag = CONSTANT_Class; break;
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   624
                    case 'k': tag = CONSTANT_LoadableValue; break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                    case 'f': tag = CONSTANT_Fieldref; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                    case 'm': tag = CONSTANT_Methodref; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                    case 'i': tag = CONSTANT_InterfaceMethodref; break;
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   628
                    case 'y': tag = CONSTANT_InvokeDynamic; break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                assert(tag != CONSTANT_None);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            } else if (index > 0 && length == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                assert(from_bc == _ldc);
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   633
                tag = CONSTANT_LoadableValue;  // _ldc opcode only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            for (int bc = from_bc; bc <= to_bc; bc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                BC_FORMAT[w][bc] = fmt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                assert(BC_LENGTH[w][bc] == -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                BC_LENGTH[w][bc] = (byte) length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                BC_INDEX[w][bc]  = (byte) index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                BC_TAG[w][bc]    = (byte) tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                assert(!(index == 0 && tag != CONSTANT_None));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                BC_BRANCH[w][bc] = (byte) branch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                BC_SLOT[w][bc]   = (byte) slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                assert(branch == 0 || slot == 0);   // not both branch & local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                assert(branch == 0 || index == 0);  // not both branch & cp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                assert(slot == 0   || index == 0);  // not both local & cp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                BC_CON[w][bc]    = (byte) con;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   651
16050
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 15526
diff changeset
   652
    public static void opcodeChecker(byte[] code, ConstantPool.Entry[] cpMap,
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 15526
diff changeset
   653
            Package.Version clsVersion) throws FormatException {
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   654
        Instruction i = at(code, 0);
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   655
        while (i != null) {
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   656
            int opcode = i.getBC();
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   657
            if (opcode < _nop || opcode > _jsr_w) {
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   658
                String message = "illegal opcode: " + opcode + " " + i;
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   659
                throw new FormatException(message);
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   660
            }
15526
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12544
diff changeset
   661
            ConstantPool.Entry e = i.getCPRef(cpMap);
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12544
diff changeset
   662
            if (e != null) {
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12544
diff changeset
   663
                byte tag = i.getCPTag();
16050
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 15526
diff changeset
   664
                boolean match = e.tagMatches(tag);
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 15526
diff changeset
   665
                if (!match &&
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 15526
diff changeset
   666
                        (i.bc == _invokespecial || i.bc == _invokestatic) &&
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 15526
diff changeset
   667
                        e.tagMatches(CONSTANT_InterfaceMethodref) &&
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 15526
diff changeset
   668
                        clsVersion.greaterThan(Constants.JAVA7_MAX_CLASS_VERSION)) {
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 15526
diff changeset
   669
                    match = true;
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 15526
diff changeset
   670
                }
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 15526
diff changeset
   671
                if (!match) {
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 15526
diff changeset
   672
                    String message = "illegal reference, expected type="
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 15526
diff changeset
   673
                            + ConstantPool.tagName(tag) + ": "
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 15526
diff changeset
   674
                            + i.toString(cpMap);
15526
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12544
diff changeset
   675
                    throw new FormatException(message);
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12544
diff changeset
   676
                }
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12544
diff changeset
   677
            }
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   678
            i = i.next();
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   679
        }
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   680
    }
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   681
    static class FormatException extends IOException {
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
   682
        private static final long serialVersionUID = 3175572275651367015L;
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
   683
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   684
        FormatException(String message) {
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   685
            super(message);
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   686
        }
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   687
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
}