jaxp/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/generic/Select.java
author joehw
Mon, 17 Apr 2017 16:24:10 -0700
changeset 44797 8b3b3b911b8a
parent 25868 686eef1e7a79
child 46174 5611d2529b49
permissions -rw-r--r--
8162572: Update License Header for all JAXP sources Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     5
/*
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one or more
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     7
 * contributor license agreements.  See the NOTICE file distributed with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     8
 * this work for additional information regarding copyright ownership.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     9
 * The ASF licenses this file to You under the Apache License, Version 2.0
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    10
 * (the "License"); you may not use this file except in compliance with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    11
 * the License.  You may obtain a copy of the License at
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    12
 *
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    13
 *      http://www.apache.org/licenses/LICENSE-2.0
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    14
 *
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    15
 * Unless required by applicable law or agreed to in writing, software
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    16
 * distributed under the License is distributed on an "AS IS" BASIS,
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    18
 * See the License for the specific language governing permissions and
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    19
 * limitations under the License.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    20
 */
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    21
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
package com.sun.org.apache.bcel.internal.generic;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
import java.io.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import com.sun.org.apache.bcel.internal.util.ByteSequence;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
 * Select - Abstract super class for LOOKUPSWITCH and TABLESWITCH instructions.
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
 * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
 * @see LOOKUPSWITCH
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
 * @see TABLESWITCH
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 * @see InstructionList
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
public abstract class Select extends BranchInstruction
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
  implements VariableLengthInstruction, StackProducer
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
  protected int[]               match;        // matches, i.e., case 1: ...
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
  protected int[]               indices;      // target offsets
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
  protected InstructionHandle[] targets;      // target objects in instruction list
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
  protected int                 fixed_length; // fixed length defined by subclasses
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
  protected int                 match_length; // number of cases
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
  protected int                 padding = 0;  // number of pad bytes for alignment
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
   * Empty constructor needed for the Class.newInstance() statement in
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
   * Instruction.readInstruction(). Not to be used otherwise.
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
  Select() {}
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
   * (Match, target) pairs for switch.
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
   * `Match' and `targets' must have the same length of course.
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
   * @param match array of matching values
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
   * @param targets instruction targets
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
   * @param target default instruction target
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
  Select(short opcode, int[] match, InstructionHandle[] targets,
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
         InstructionHandle target) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
    super(opcode, target);
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
    this.targets = targets;
17538
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
    64
    for(int i=0; i < targets.length; i++) {
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
    65
      BranchInstruction.notifyTargetChanged(targets[i], this);
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
    66
    }
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
    this.match = match;
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
    if((match_length = match.length) != targets.length)
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
      throw new ClassGenException("Match and target array have not the same length");
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
    indices = new int[match_length];
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
   * Since this is a variable length instruction, it may shift the following
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
   * instructions which then need to update their position.
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
   * Called by InstructionList.setPositions when setting the position for every
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
   * instruction. In the presence of variable length instructions `setPositions'
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
   * performs multiple passes over the instruction list to calculate the
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
   * correct (byte) positions and offsets by calling this function.
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
   * @param offset additional offset caused by preceding (variable length) instructions
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
   * @param max_offset the maximum offset that may be caused by these instructions
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
   * @return additional offset caused by possible change of this instruction's length
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
   */
17538
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
    89
  @Override
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
  protected int updatePosition(int offset, int max_offset) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
    position += offset; // Additional offset caused by preceding SWITCHs, GOTOs, etc.
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
    short old_length = length;
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
    /* Alignment on 4-byte-boundary, + 1, because of tag byte.
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
    padding = (4 - ((position + 1) % 4)) % 4;
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
    length  = (short)(fixed_length + padding); // Update length
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
    return length - old_length;
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
   * Dump instruction as byte code to stream out.
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
   * @param out Output stream
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
   */
17538
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
   107
  @Override
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
  public void dump(DataOutputStream out) throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
    out.writeByte(opcode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
    for(int i=0; i < padding; i++) // Padding bytes
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
      out.writeByte(0);
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
    index = getTargetOffset();     // Write default target offset
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
    out.writeInt(index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
   * Read needed data (e.g. index) from file.
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
   */
17538
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
   121
  @Override
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
  protected void initFromFile(ByteSequence bytes, boolean wide) throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
    padding = (4 - (bytes.getIndex() % 4)) % 4; // Compute number of pad bytes
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
    for(int i=0; i < padding; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
      bytes.readByte();
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
    // Default branch target common for both cases (TABLESWITCH, LOOKUPSWITCH)
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
    index = bytes.readInt();
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
   * @return mnemonic for instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
   */
17538
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
   137
  @Override
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
  public String toString(boolean verbose) {
17538
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
   139
    final StringBuilder buf = new StringBuilder(super.toString(verbose));
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
    if(verbose) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
      for(int i=0; i < match_length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
        String s = "null";
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
        if(targets[i] != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
          s = targets[i].getInstruction().toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
17538
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
   148
          buf.append("(").append(match[i]).append(", ")
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
   149
             .append(s).append(" = {").append(indices[i]).append("})");
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
    else
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
      buf.append(" ...");
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
    return buf.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
   * Set branch target for `i'th case
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
   */
17538
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
   161
  public final void setTarget(int i, InstructionHandle target) {
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
   162
    notifyTargetChanging(targets[i], this);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
    targets[i] = target;
17538
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
   164
    notifyTargetChanged(targets[i], this);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
   * @param old_ih old target
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
   * @param new_ih new target
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
   */
17538
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
   171
  @Override
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
  public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
    boolean targeted = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
    if(target == old_ih) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
      targeted = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
      setTarget(new_ih);
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
    for(int i=0; i < targets.length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
      if(targets[i] == old_ih) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
        targeted = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
        setTarget(i, new_ih);
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
    if(!targeted)
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
      throw new ClassGenException("Not targeting " + old_ih);
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
   * @return true, if ih is target of this instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
   */
17538
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
   194
  @Override
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
  public boolean containsTarget(InstructionHandle ih) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
    if(target == ih)
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
      return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
    for(int i=0; i < targets.length; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
      if(targets[i] == ih)
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
        return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
    return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
   * Inform targets that they're not targeted anymore.
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
   */
17538
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
   209
  @Override
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
  void dispose() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
    super.dispose();
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
    for(int i=0; i < targets.length; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
      targets[i].removeTargeter(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
   * @return array of match indices
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
  public int[] getMatchs() { return match; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
   * @return array of match target offsets
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
  public int[] getIndices() { return indices; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
   * @return array of match targets
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
  public InstructionHandle[] getTargets() { return targets; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
}