jdk/src/share/classes/com/sun/java/util/jar/pack/Code.java
author ksrini
Sat, 27 Nov 2010 07:46:05 -0800
changeset 7299 0b68a82ebb55
parent 5506 202f599c92aa
child 7668 d4a77089c587
child 7795 98021fc612af
permissions -rw-r--r--
7002986: (pack200) intermittent failures compiling pack200 Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2001, 2003, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.java.util.jar.pack.Package.Class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.Modifier;
7299
0b68a82ebb55 7002986: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    30
import java.util.Arrays;
0b68a82ebb55 7002986: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    31
import java.util.Collection;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Represents a chunk of bytecodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author John Rose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
class Code extends Attribute.Holder implements Constants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    Class.Method m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public Code(Class.Method m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        this.m = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public Class.Method getMethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public Class thisClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        return m.thisClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public Package getPackage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        return m.thisClass().getPackage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public ConstantPool.Entry[] getCPMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        return m.getCPMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static private final ConstantPool.Entry[] noRefs = ConstantPool.noRefs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // The following fields are used directly by the ClassReader, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    int max_stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    int max_locals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    ConstantPool.Entry handler_class[] = noRefs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    int handler_start[] = noInts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    int handler_end[] = noInts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    int handler_catch[] = noInts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    byte[] bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    Fixups fixups;  // reference relocations, if any are required
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    Object insnMap; // array of instruction boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    int getLength() { return bytes.length; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    int getMaxStack() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        return max_stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    void setMaxStack(int ms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        max_stack = ms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    int getMaxNALocals() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        int argsize = m.getArgumentSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return max_locals - argsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    void setMaxNALocals(int ml) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        int argsize = m.getArgumentSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        max_locals = argsize + ml;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    int getHandlerCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        assert(handler_class.length == handler_start.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        assert(handler_class.length == handler_end.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        assert(handler_class.length == handler_catch.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return handler_class.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    void setHandlerCount(int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (h > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            handler_class = new ConstantPool.Entry[h];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            handler_start = new int[h];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            handler_end   = new int[h];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            handler_catch = new int[h];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            // caller must fill these in ASAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    void setBytes(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        this.bytes = bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (fixups != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            fixups.setBytes(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    void setInstructionMap(int[] insnMap, int mapLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        //int[] oldMap = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        //assert((oldMap = getInstructionMap()) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this.insnMap = allocateInstructionMap(insnMap, mapLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        //assert(Arrays.equals(oldMap, getInstructionMap()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    void setInstructionMap(int[] insnMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        setInstructionMap(insnMap, insnMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    int[] getInstructionMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return expandInstructionMap(getInsnMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    void addFixups(Collection moreFixups) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (fixups == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            fixups = new Fixups(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        assert(fixups.getBytes() == bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        fixups.addAll(moreFixups);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public void trimToSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (fixups != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            fixups.trimToSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (fixups.size() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                fixups = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        super.trimToSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    protected void visitRefs(int mode, Collection refs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        int verbose = getPackage().verbose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (verbose > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            System.out.println("Reference scan "+this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        Class cls = thisClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        Package pkg = cls.getPackage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        for (int i = 0; i < handler_class.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            refs.add(handler_class[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (fixups != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            fixups.visitRefs(refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            // References (to a local cpMap) are embedded in the bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            ConstantPool.Entry[] cpMap = getCPMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            for (Instruction i = instructionAt(0); i != null; i = i.next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                if (verbose > 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    System.out.println(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                int cpref = i.getCPIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                if (cpref >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    refs.add(cpMap[cpref]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // Handle attribute list:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        super.visitRefs(mode, refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    // Since bytecodes are the single largest contributor to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    // package size, it's worth a little bit of trouble
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    // to reduce the per-bytecode memory footprint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    // In the current scheme, half of the bulk of these arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    // due to bytes, and half to shorts.  (Ints are insignificant.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    // Given an average of 1.8 bytes per instruction, this means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    // instruction boundary arrays are about a 75% overhead--tolerable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    // (By using bytes, we get 33% savings over just shorts and ints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    // Using both bytes and shorts gives 66% savings over just ints.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    static final boolean shrinkMaps = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private Object allocateInstructionMap(int[] insnMap, int mapLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        int PClimit = getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (shrinkMaps && PClimit <= Byte.MAX_VALUE - Byte.MIN_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            byte[] map = new byte[mapLen+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            for (int i = 0; i < mapLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                map[i] = (byte)(insnMap[i] + Byte.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            map[mapLen] = (byte)(PClimit + Byte.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        } else if (shrinkMaps && PClimit < Short.MAX_VALUE - Short.MIN_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            short[] map = new short[mapLen+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            for (int i = 0; i < mapLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                map[i] = (short)(insnMap[i] + Short.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            map[mapLen] = (short)(PClimit + Short.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            int[] map = new int[mapLen+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            for (int i = 0; i < mapLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                map[i] = (int) insnMap[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            map[mapLen] = (int) PClimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    private int[] expandInstructionMap(Object map0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        int[] imap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (map0 instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            byte[] map = (byte[]) map0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            imap = new int[map.length-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            for (int i = 0; i < imap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                imap[i] = map[i] - Byte.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        } else if (map0 instanceof short[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            short[] map = (short[]) map0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            imap = new int[map.length-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            for (int i = 0; i < imap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                imap[i] = map[i] - Byte.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            int[] map = (int[]) map0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            imap = new int[map.length-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            for (int i = 0; i < imap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                imap[i] = map[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return imap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    Object getInsnMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // Build a map of instruction boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (insnMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            return insnMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        int[] map = new int[getLength()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        int fillp = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        for (Instruction i = instructionAt(0); i != null; i = i.next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            map[fillp++] = i.getPC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // Make it byte[], short[], or int[] according to the max BCI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        insnMap = allocateInstructionMap(map, fillp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        //assert(assertBCICodingsOK());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        return insnMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /** Encode the given BCI as an instruction boundary number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *  For completeness, irregular (non-boundary) BCIs are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *  encoded compactly immediately after the boundary numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *  This encoding is the identity mapping outside 0..length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *  and it is 1-1 everywhere.  All by itself this technique
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *  improved zipped rt.jar compression by 2.6%.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public int encodeBCI(int bci) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (bci <= 0 || bci > getLength())  return bci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        Object map0 = getInsnMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        int i, len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (shrinkMaps && map0 instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            byte[] map = (byte[]) map0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            len = map.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            i = Arrays.binarySearch(map, (byte)(bci + Byte.MIN_VALUE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        } else if (shrinkMaps && map0 instanceof short[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            short[] map = (short[]) map0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            len = map.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            i = Arrays.binarySearch(map, (short)(bci + Short.MIN_VALUE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            int[] map = (int[]) map0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            len = map.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            i = Arrays.binarySearch(map, (int)bci);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        assert(i != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        assert(i != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        assert(i != len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        assert(i != -len-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return (i >= 0) ? i : len + bci - (-i-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public int decodeBCI(int bciCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (bciCode <= 0 || bciCode > getLength())  return bciCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        Object map0 = getInsnMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        int i, len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        // len == map.length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // If bciCode < len, result is map[bciCode], the common and fast case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // Otherwise, let map[i] be the smallest map[*] larger than bci.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        // Then, required by the return statement of encodeBCI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        //   bciCode == len + bci - i
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        // Thus:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        //   bci-i == bciCode-len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        //   map[i]-adj-i == bciCode-len ; adj in (0..map[i]-map[i-1])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        // We can solve this by searching for adjacent entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        // map[i-1], map[i] such that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        //   map[i-1]-(i-1) <= bciCode-len < map[i]-i
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        // This can be approximated by searching map[i] for bciCode and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        // linear searching backward.  Given the right i, we then have:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        //   bci == bciCode-len + i
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        // This linear search is at its worst case for indexes in the beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        // of a large method, but it's not clear that this is a problem in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        // practice, since BCIs are usually on instruction boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (shrinkMaps && map0 instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            byte[] map = (byte[]) map0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            len = map.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            if (bciCode < len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                return map[bciCode] - Byte.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            i = Arrays.binarySearch(map, (byte)(bciCode + Byte.MIN_VALUE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            if (i < 0)  i = -i-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            int key = bciCode-len + Byte.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            for (;; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                if (map[i-1]-(i-1) <= key)  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        } else if (shrinkMaps && map0 instanceof short[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            short[] map = (short[]) map0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            len = map.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            if (bciCode < len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                return map[bciCode] - Short.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            i = Arrays.binarySearch(map, (short)(bciCode + Short.MIN_VALUE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (i < 0)  i = -i-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            int key = bciCode-len + Short.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            for (;; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                if (map[i-1]-(i-1) <= key)  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            int[] map = (int[]) map0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            len = map.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            if (bciCode < len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                return map[bciCode];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            i = Arrays.binarySearch(map, (int)bciCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            if (i < 0)  i = -i-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            int key = bciCode-len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            for (;; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                if (map[i-1]-(i-1) <= key)  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return bciCode-len + i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public void finishRefs(ConstantPool.Index ix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if (fixups != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            fixups.finishRefs(ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            fixups = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        // Code attributes are finished in ClassWriter.writeAttributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    Instruction instructionAt(int pc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return Instruction.at(bytes, pc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    static boolean flagsRequireCode(int flags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        // A method's flags force it to have a Code attribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        // if the flags are neither native nor abstract.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        return (flags & (Modifier.NATIVE | Modifier.ABSTRACT)) == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
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 m+".Code";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /// Fetching values from my own array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    public int getInt(int pc)    { return Instruction.getInt(bytes, pc); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public int getShort(int pc)  { return Instruction.getShort(bytes, pc); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public int getByte(int pc)   { return Instruction.getByte(bytes, pc); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    void setInt(int pc, int x)   { Instruction.setInt(bytes, pc, x); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    void setShort(int pc, int x) { Instruction.setShort(bytes, pc, x); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    void setByte(int pc, int x)  { Instruction.setByte(bytes, pc, x); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
/* TEST CODE ONLY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    private boolean assertBCICodingsOK() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        boolean ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        int len = java.lang.reflect.Array.getLength(insnMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        int base = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (insnMap.getClass().getComponentType() == Byte.TYPE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            base = Byte.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (insnMap.getClass().getComponentType() == Short.TYPE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            base = Short.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        for (int i = -1, imax = getLength()+1; i <= imax; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            int bci = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            int enc = Math.min(-999, bci-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            int dec = enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                enc = encodeBCI(bci);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                dec = decodeBCI(enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            } catch (RuntimeException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                ee.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            if (dec == bci) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                //System.out.println("BCI="+bci+(enc<len?"":"   ")+" enc="+enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            if (ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                for (int q = 0; q <= 1; q++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    sb.append("bci "+(q==0?"map":"del")+"["+len+"] = {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    for (int j = 0; j < len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                        int mapi = ((Number)java.lang.reflect.Array.get(insnMap, j)).intValue() - base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                        mapi -= j*q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                        sb.append(" "+mapi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    sb.append(" }");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    System.out.println("*** "+sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            System.out.println("*** BCI="+bci+" enc="+enc+" dec="+dec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
//*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
}