jaxp/src/com/sun/org/apache/bcel/internal/generic/InstructionList.java
author dfuchs
Fri, 17 May 2013 10:40:21 +0200
changeset 17538 d8d911c4e5d4
parent 12457 c348e06f0e82
permissions -rw-r--r--
8013900: More warnings compiling jaxp. Summary: Some internal implementation classes in Jaxp were redefining equals() without redefining hashCode(). This patch adds hashCode() methods that are consistent with equals(). Reviewed-by: chegar, joehw
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
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
package com.sun.org.apache.bcel.internal.generic;
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
/* ====================================================================
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * The Apache Software License, Version 1.1
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * Copyright (c) 2001 The Apache Software Foundation.  All rights
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 * reserved.
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 * Redistribution and use in source and binary forms, with or without
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * modification, are permitted provided that the following conditions
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * are met:
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * 1. Redistributions of source code must retain the above copyright
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 *    notice, this list of conditions and the following disclaimer.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
 * 2. Redistributions in binary form must reproduce the above copyright
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 *    notice, this list of conditions and the following disclaimer in
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 *    the documentation and/or other materials provided with the
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
 *    distribution.
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
 * 3. The end-user documentation included with the redistribution,
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
 *    if any, must include the following acknowledgment:
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
 *       "This product includes software developed by the
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
 *        Apache Software Foundation (http://www.apache.org/)."
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
 *    Alternately, this acknowledgment may appear in the software itself,
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
 *    if and wherever such third-party acknowledgments normally appear.
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
 * 4. The names "Apache" and "Apache Software Foundation" and
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 *    "Apache BCEL" must not be used to endorse or promote products
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 *    derived from this software without prior written permission. For
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 *    written permission, please contact apache@apache.org.
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 * 5. Products derived from this software may not be called "Apache",
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 *    "Apache BCEL", nor may "Apache" appear in their name, without
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 *    prior written permission of the Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
 * SUCH DAMAGE.
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
 * ====================================================================
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
 * This software consists of voluntary contributions made by many
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
 * individuals on behalf of the Apache Software Foundation.  For more
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
 * information on the Apache Software Foundation, please see
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
 * <http://www.apache.org/>.
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
import com.sun.org.apache.bcel.internal.Constants;
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
import com.sun.org.apache.bcel.internal.classfile.Constant;
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
import com.sun.org.apache.bcel.internal.util.ByteSequence;
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
import java.io.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
import java.util.Iterator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
import java.util.HashMap;
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
import java.util.ArrayList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
 * This class is a container for a list of <a
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
 * href="Instruction.html">Instruction</a> objects. Instructions can
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
 * be appended, inserted, moved, deleted, etc.. Instructions are being
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
 * wrapped into <a
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
 * href="InstructionHandle.html">InstructionHandles</a> objects that
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
 * are returned upon append/insert operations. They give the user
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
 * (read only) access to the list structure, such that it can be traversed and
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
 * manipulated in a controlled way.
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
 * A list is finally dumped to a byte code array with <a
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
 * href="#getByteCode()">getByteCode</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
 * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
 * @see     Instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
 * @see     InstructionHandle
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
 * @see BranchHandle
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
public class InstructionList implements Serializable {
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
  private InstructionHandle start  = null, end = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
  private int               length = 0; // number of elements in list
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
  private int[]             byte_positions; // byte code offsets corresponding to instructions
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
   * Create (empty) instruction list.
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
  public InstructionList() {}
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
   * Create instruction list containing one instruction.
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
   * @param i initial instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
  public InstructionList(Instruction i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
    append(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
   * Create instruction list containing one instruction.
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
   * @param i initial instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
  public InstructionList(BranchInstruction i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
    append(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
   * Initialize list with (nonnull) compound instruction. Consumes argument
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
   * list, i.e., it becomes empty.
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
   * @param c compound instruction (list)
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
  public InstructionList(CompoundInstruction c) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
    append(c.getInstructionList());
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
   * Test for empty list.
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
  public boolean isEmpty() { return start == null; } // && end == null
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
   * Find the target instruction (handle) that corresponds to the given target
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
   * position (byte code offset).
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
   * @param ihs array of instruction handles, i.e. il.getInstructionHandles()
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
   * @param pos array of positions corresponding to ihs, i.e. il.getInstructionPositions()
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
   * @param count length of arrays
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
   * @param target target position to search for
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
   * @return target position's instruction handle if available
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
  public static InstructionHandle findHandle(InstructionHandle[] ihs,
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
                                             int[] pos, int count,
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
                                             int target) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
    int l=0, r = count - 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
    /* Do a binary search since the pos array is orderd.
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
    do {
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
      int i = (l + r) / 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
      int j = pos[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
      if(j == target) // target found
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
        return ihs[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
      else if(target < j) // else constrain search area
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
        r = i - 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
      else // target > j
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
        l = i + 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
    } while(l <= r);
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
    return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
   * Get instruction handle for instruction at byte code position pos.
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
   * This only works properly, if the list is freshly initialized from a byte array or
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
   * setPositions() has been called before this method.
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
   * @param pos byte code position to search for
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
   * @return target position's instruction handle if available
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
  public InstructionHandle findHandle(int pos) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
    InstructionHandle[] ihs = getInstructionHandles();
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
    return findHandle(ihs, byte_positions, length, pos);
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
   * Initialize instruction list from byte array.
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
   * @param code byte array containing the instructions
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
  public InstructionList(byte[] code) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
    ByteSequence        bytes = new ByteSequence(code);
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
    InstructionHandle[] ihs   = new InstructionHandle[code.length];
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
    int[]               pos   = new int[code.length]; // Can't be more than that
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
    int                 count = 0; // Contains actual length
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
    /* Pass 1: Create an object for each byte code and append them
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
     * to the list.
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
    try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
      while(bytes.available() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
        // Remember byte offset and associate it with the instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
        int off =  bytes.getIndex();
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
        pos[count] = off;
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
        /* Read one instruction from the byte stream, the byte position is set
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
         * accordingly.
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
         */
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
        Instruction       i = Instruction.readInstruction(bytes);
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
        InstructionHandle ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
        if(i instanceof BranchInstruction) // Use proper append() method
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
          ih = append((BranchInstruction)i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
          ih = append(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
        ih.setPosition(off);
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
        ihs[count] = ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
        count++;
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
    } catch(IOException e) { throw new ClassGenException(e.toString()); }
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
    byte_positions = new int[count]; // Trim to proper size
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
    System.arraycopy(pos, 0, byte_positions, 0, count);
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
    /* Pass 2: Look for BranchInstruction and update their targets, i.e.,
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
     * convert offsets to instruction handles.
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
    for(int i=0; i < count; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
      if(ihs[i] instanceof BranchHandle) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
        BranchInstruction bi = (BranchInstruction)ihs[i].instruction;
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
        int target = bi.position + bi.getIndex(); /* Byte code position:
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
                                                   * relative -> absolute. */
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
        // Search for target position
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
        InstructionHandle ih = findHandle(ihs, pos, count, target);
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
        if(ih == null) // Search failed
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
          throw new ClassGenException("Couldn't find target for branch: " + bi);
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
        bi.setTarget(ih); // Update target
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
        // If it is a Select instruction, update all branch targets
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
        if(bi instanceof Select) { // Either LOOKUPSWITCH or TABLESWITCH
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
          Select s       = (Select)bi;
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
          int[]  indices = s.getIndices();
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
          for(int j=0; j < indices.length; j++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
            target = bi.position + indices[j];
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
            ih     = findHandle(ihs, pos, count, target);
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
            if(ih == null) // Search failed
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
              throw new ClassGenException("Couldn't find target for switch: " + bi);
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
            s.setTarget(j, ih); // Update target
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
   * Append another list after instruction (handle) ih contained in this list.
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
   * Consumes argument list, i.e., it becomes empty.
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
   * @param ih where to append the instruction list
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
   * @param il Instruction list to append to this one
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
   * @return instruction handle pointing to the <B>first</B> appended instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
  public InstructionHandle append(InstructionHandle ih, InstructionList il) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
    if(il == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
      throw new ClassGenException("Appending null InstructionList");
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
    if(il.isEmpty()) // Nothing to do
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
      return ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
    InstructionHandle next = ih.next, ret = il.start;
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
    ih.next = il.start;
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
    il.start.prev = ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
    il.end.next = next;
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
    if(next != null) // i == end ?
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
      next.prev = il.end;
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
    else
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
      end = il.end; // Update end ...
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
    length += il.length; // Update length
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
    il.clear();
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
    return ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
   * Append another list after instruction i contained in this list.
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
   * Consumes argument list, i.e., it becomes empty.
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
   * @param i  where to append the instruction list
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
   * @param il Instruction list to append to this one
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
   * @return instruction handle pointing to the <B>first</B> appended instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
  public InstructionHandle append(Instruction i, InstructionList il) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
    InstructionHandle ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
    if((ih = findInstruction2(i)) == null) // Also applies for empty list
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
      throw new ClassGenException("Instruction " + i +
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
                                  " is not contained in this list.");
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
    return append(ih, il);
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
   * Append another list to this one.
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
   * Consumes argument list, i.e., it becomes empty.
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
   * @param il list to append to end of this list
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
   * @return instruction handle of the <B>first</B> appended instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
  public InstructionHandle append(InstructionList il) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
    if(il == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
      throw new ClassGenException("Appending null InstructionList");
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
    if(il.isEmpty()) // Nothing to do
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
      return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
    if(isEmpty()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
      start  = il.start;
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
      end    = il.end;
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
      length = il.length;
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
      il.clear();
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
      return start;
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
    } else
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
      return append(end, il);  // was end.instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
   * Append an instruction to the end of this list.
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
   * @param ih instruction to append
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
  private void append(InstructionHandle ih) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
    if(isEmpty()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
      start = end = ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
      ih.next = ih.prev = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
    else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
      end.next = ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
      ih.prev  = end;
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
      ih.next  = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
      end      = ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
    length++; // Update length
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
   * Append an instruction to the end of this list.
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
   * @param i instruction to append
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
   * @return instruction handle of the appended instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
  public InstructionHandle append(Instruction i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
    InstructionHandle ih = InstructionHandle.getInstructionHandle(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
    append(ih);
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
    return ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
   * Append a branch instruction to the end of this list.
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
   * @param i branch instruction to append
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
   * @return branch instruction handle of the appended instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
  public BranchHandle append(BranchInstruction i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
    BranchHandle ih = BranchHandle.getBranchHandle(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
    append(ih);
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
    return ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
   * Append a single instruction j after another instruction i, which
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
   * must be in this list of course!
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
   * @param i Instruction in list
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
   * @param j Instruction to append after i in list
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
   * @return instruction handle of the first appended instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
  public InstructionHandle append(Instruction i, Instruction j) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
    return append(i, new InstructionList(j));
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
   * Append a compound instruction, after instruction i.
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
   * @param i Instruction in list
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
   * @param c The composite instruction (containing an InstructionList)
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
   * @return instruction handle of the first appended instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
  public InstructionHandle append(Instruction i, CompoundInstruction c) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
    return append(i, c.getInstructionList());
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
   * Append a compound instruction.
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
   * @param c The composite instruction (containing an InstructionList)
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
   * @return instruction handle of the first appended instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
  public InstructionHandle append(CompoundInstruction c) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
    return append(c.getInstructionList());
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
   * Append a compound instruction.
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
   * @param ih where to append the instruction list
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
   * @param c The composite instruction (containing an InstructionList)
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
   * @return instruction handle of the first appended instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
  public InstructionHandle append(InstructionHandle ih, CompoundInstruction c) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
    return append(ih, c.getInstructionList());
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
   * Append an instruction after instruction (handle) ih contained in this list.
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
   * @param ih where to append the instruction list
7f561c08de6b Initial load
duke
parents:
diff changeset
   420
   * @param i Instruction to append
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
   * @return instruction handle pointing to the <B>first</B> appended instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   422
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
  public InstructionHandle append(InstructionHandle ih, Instruction i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   424
    return append(ih, new InstructionList(i));
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
   * Append an instruction after instruction (handle) ih contained in this list.
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
   * @param ih where to append the instruction list
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
   * @param i Instruction to append
7f561c08de6b Initial load
duke
parents:
diff changeset
   432
   * @return instruction handle pointing to the <B>first</B> appended instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   434
  public BranchHandle append(InstructionHandle ih, BranchInstruction i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   435
    BranchHandle    bh = BranchHandle.getBranchHandle(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   436
    InstructionList il = new InstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   437
    il.append(bh);
7f561c08de6b Initial load
duke
parents:
diff changeset
   438
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
    append(ih, il);
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
7f561c08de6b Initial load
duke
parents:
diff changeset
   441
    return bh;
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
7f561c08de6b Initial load
duke
parents:
diff changeset
   444
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   445
   * Insert another list before Instruction handle ih contained in this list.
7f561c08de6b Initial load
duke
parents:
diff changeset
   446
   * Consumes argument list, i.e., it becomes empty.
7f561c08de6b Initial load
duke
parents:
diff changeset
   447
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   448
   * @param i  where to append the instruction list
7f561c08de6b Initial load
duke
parents:
diff changeset
   449
   * @param il Instruction list to insert
7f561c08de6b Initial load
duke
parents:
diff changeset
   450
   * @return instruction handle of the first inserted instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   451
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   452
  public InstructionHandle insert(InstructionHandle ih, InstructionList il) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   453
    if(il == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   454
      throw new ClassGenException("Inserting null InstructionList");
7f561c08de6b Initial load
duke
parents:
diff changeset
   455
7f561c08de6b Initial load
duke
parents:
diff changeset
   456
    if(il.isEmpty()) // Nothing to do
7f561c08de6b Initial load
duke
parents:
diff changeset
   457
      return ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   458
7f561c08de6b Initial load
duke
parents:
diff changeset
   459
    InstructionHandle prev = ih.prev, ret = il.start;
7f561c08de6b Initial load
duke
parents:
diff changeset
   460
7f561c08de6b Initial load
duke
parents:
diff changeset
   461
    ih.prev = il.end;
7f561c08de6b Initial load
duke
parents:
diff changeset
   462
    il.end.next = ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   463
7f561c08de6b Initial load
duke
parents:
diff changeset
   464
    il.start.prev = prev;
7f561c08de6b Initial load
duke
parents:
diff changeset
   465
7f561c08de6b Initial load
duke
parents:
diff changeset
   466
    if(prev != null) // ih == start ?
7f561c08de6b Initial load
duke
parents:
diff changeset
   467
      prev.next = il.start;
7f561c08de6b Initial load
duke
parents:
diff changeset
   468
    else
7f561c08de6b Initial load
duke
parents:
diff changeset
   469
      start = il.start; // Update start ...
7f561c08de6b Initial load
duke
parents:
diff changeset
   470
7f561c08de6b Initial load
duke
parents:
diff changeset
   471
    length += il.length; // Update length
7f561c08de6b Initial load
duke
parents:
diff changeset
   472
7f561c08de6b Initial load
duke
parents:
diff changeset
   473
    il.clear();
7f561c08de6b Initial load
duke
parents:
diff changeset
   474
7f561c08de6b Initial load
duke
parents:
diff changeset
   475
    return ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   476
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   477
7f561c08de6b Initial load
duke
parents:
diff changeset
   478
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   479
   * Insert another list.
7f561c08de6b Initial load
duke
parents:
diff changeset
   480
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   481
   * @param il list to insert before start of this list
7f561c08de6b Initial load
duke
parents:
diff changeset
   482
   * @return instruction handle of the first inserted instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   483
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   484
  public InstructionHandle insert(InstructionList il) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   485
    if(isEmpty()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   486
      append(il); // Code is identical for this case
7f561c08de6b Initial load
duke
parents:
diff changeset
   487
      return start;
7f561c08de6b Initial load
duke
parents:
diff changeset
   488
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   489
    else
7f561c08de6b Initial load
duke
parents:
diff changeset
   490
      return insert(start, il);
7f561c08de6b Initial load
duke
parents:
diff changeset
   491
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   492
7f561c08de6b Initial load
duke
parents:
diff changeset
   493
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   494
   * Insert an instruction at start of this list.
7f561c08de6b Initial load
duke
parents:
diff changeset
   495
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   496
   * @param ih instruction to insert
7f561c08de6b Initial load
duke
parents:
diff changeset
   497
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   498
  private void insert(InstructionHandle ih) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   499
    if(isEmpty()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   500
      start = end = ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   501
      ih.next = ih.prev = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   502
    } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   503
      start.prev = ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   504
      ih.next    = start;
7f561c08de6b Initial load
duke
parents:
diff changeset
   505
      ih.prev    = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   506
      start      = ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   507
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   508
7f561c08de6b Initial load
duke
parents:
diff changeset
   509
    length++;
7f561c08de6b Initial load
duke
parents:
diff changeset
   510
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   511
7f561c08de6b Initial load
duke
parents:
diff changeset
   512
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   513
   * Insert another list before Instruction i contained in this list.
7f561c08de6b Initial load
duke
parents:
diff changeset
   514
   * Consumes argument list, i.e., it becomes empty.
7f561c08de6b Initial load
duke
parents:
diff changeset
   515
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   516
   * @param i  where to append the instruction list
7f561c08de6b Initial load
duke
parents:
diff changeset
   517
   * @param il Instruction list to insert
7f561c08de6b Initial load
duke
parents:
diff changeset
   518
   * @return instruction handle pointing to the first inserted instruction,
7f561c08de6b Initial load
duke
parents:
diff changeset
   519
   * i.e., il.getStart()
7f561c08de6b Initial load
duke
parents:
diff changeset
   520
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   521
  public InstructionHandle insert(Instruction i, InstructionList il) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   522
    InstructionHandle ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   523
7f561c08de6b Initial load
duke
parents:
diff changeset
   524
    if((ih = findInstruction1(i)) == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   525
      throw new ClassGenException("Instruction " + i +
7f561c08de6b Initial load
duke
parents:
diff changeset
   526
                                  " is not contained in this list.");
7f561c08de6b Initial load
duke
parents:
diff changeset
   527
7f561c08de6b Initial load
duke
parents:
diff changeset
   528
    return insert(ih, il);
7f561c08de6b Initial load
duke
parents:
diff changeset
   529
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   530
7f561c08de6b Initial load
duke
parents:
diff changeset
   531
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   532
   * Insert an instruction at start of this list.
7f561c08de6b Initial load
duke
parents:
diff changeset
   533
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   534
   * @param i instruction to insert
7f561c08de6b Initial load
duke
parents:
diff changeset
   535
   * @return instruction handle of the inserted instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   536
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   537
  public InstructionHandle insert(Instruction i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   538
    InstructionHandle ih = InstructionHandle.getInstructionHandle(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   539
    insert(ih);
7f561c08de6b Initial load
duke
parents:
diff changeset
   540
7f561c08de6b Initial load
duke
parents:
diff changeset
   541
    return ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   542
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   543
7f561c08de6b Initial load
duke
parents:
diff changeset
   544
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   545
   * Insert a branch instruction at start of this list.
7f561c08de6b Initial load
duke
parents:
diff changeset
   546
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   547
   * @param i branch instruction to insert
7f561c08de6b Initial load
duke
parents:
diff changeset
   548
   * @return branch instruction handle of the appended instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   549
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   550
  public BranchHandle insert(BranchInstruction i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   551
    BranchHandle ih = BranchHandle.getBranchHandle(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   552
    insert(ih);
7f561c08de6b Initial load
duke
parents:
diff changeset
   553
    return ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   554
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   555
7f561c08de6b Initial load
duke
parents:
diff changeset
   556
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   557
   * Insert a single instruction j before another instruction i, which
7f561c08de6b Initial load
duke
parents:
diff changeset
   558
   * must be in this list of course!
7f561c08de6b Initial load
duke
parents:
diff changeset
   559
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   560
   * @param i Instruction in list
7f561c08de6b Initial load
duke
parents:
diff changeset
   561
   * @param j Instruction to insert before i in list
7f561c08de6b Initial load
duke
parents:
diff changeset
   562
   * @return instruction handle of the first inserted instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   563
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   564
  public InstructionHandle insert(Instruction i, Instruction j) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   565
    return insert(i, new InstructionList(j));
7f561c08de6b Initial load
duke
parents:
diff changeset
   566
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   567
7f561c08de6b Initial load
duke
parents:
diff changeset
   568
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   569
   * Insert a compound instruction before instruction i.
7f561c08de6b Initial load
duke
parents:
diff changeset
   570
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   571
   * @param i Instruction in list
7f561c08de6b Initial load
duke
parents:
diff changeset
   572
   * @param c The composite instruction (containing an InstructionList)
7f561c08de6b Initial load
duke
parents:
diff changeset
   573
   * @return instruction handle of the first inserted instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   574
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   575
  public InstructionHandle insert(Instruction i, CompoundInstruction c) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   576
    return insert(i, c.getInstructionList());
7f561c08de6b Initial load
duke
parents:
diff changeset
   577
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   578
7f561c08de6b Initial load
duke
parents:
diff changeset
   579
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   580
   * Insert a compound instruction.
7f561c08de6b Initial load
duke
parents:
diff changeset
   581
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   582
   * @param c The composite instruction (containing an InstructionList)
7f561c08de6b Initial load
duke
parents:
diff changeset
   583
   * @return instruction handle of the first inserted instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   584
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   585
  public InstructionHandle insert(CompoundInstruction c) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   586
    return insert(c.getInstructionList());
7f561c08de6b Initial load
duke
parents:
diff changeset
   587
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   588
7f561c08de6b Initial load
duke
parents:
diff changeset
   589
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   590
   * Insert an instruction before instruction (handle) ih contained in this list.
7f561c08de6b Initial load
duke
parents:
diff changeset
   591
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   592
   * @param ih where to insert to the instruction list
7f561c08de6b Initial load
duke
parents:
diff changeset
   593
   * @param i Instruction to insert
7f561c08de6b Initial load
duke
parents:
diff changeset
   594
   * @return instruction handle of the first inserted instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   595
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   596
  public InstructionHandle insert(InstructionHandle ih, Instruction i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   597
    return insert(ih, new InstructionList(i));
7f561c08de6b Initial load
duke
parents:
diff changeset
   598
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   599
7f561c08de6b Initial load
duke
parents:
diff changeset
   600
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   601
   * Insert a compound instruction.
7f561c08de6b Initial load
duke
parents:
diff changeset
   602
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   603
   * @param ih where to insert the instruction list
7f561c08de6b Initial load
duke
parents:
diff changeset
   604
   * @param c The composite instruction (containing an InstructionList)
7f561c08de6b Initial load
duke
parents:
diff changeset
   605
   * @return instruction handle of the first inserted instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   606
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   607
  public InstructionHandle insert(InstructionHandle ih, CompoundInstruction c) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   608
    return insert(ih, c.getInstructionList());
7f561c08de6b Initial load
duke
parents:
diff changeset
   609
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   610
7f561c08de6b Initial load
duke
parents:
diff changeset
   611
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   612
   * Insert an instruction before instruction (handle) ih contained in this list.
7f561c08de6b Initial load
duke
parents:
diff changeset
   613
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   614
   * @param ih where to insert to the instruction list
7f561c08de6b Initial load
duke
parents:
diff changeset
   615
   * @param i Instruction to insert
7f561c08de6b Initial load
duke
parents:
diff changeset
   616
   * @return instruction handle of the first inserted instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   617
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   618
  public BranchHandle insert(InstructionHandle ih, BranchInstruction i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   619
    BranchHandle    bh = BranchHandle.getBranchHandle(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   620
    InstructionList il = new InstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   621
    il.append(bh);
7f561c08de6b Initial load
duke
parents:
diff changeset
   622
7f561c08de6b Initial load
duke
parents:
diff changeset
   623
    insert(ih, il);
7f561c08de6b Initial load
duke
parents:
diff changeset
   624
7f561c08de6b Initial load
duke
parents:
diff changeset
   625
    return bh;
7f561c08de6b Initial load
duke
parents:
diff changeset
   626
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   627
7f561c08de6b Initial load
duke
parents:
diff changeset
   628
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   629
   * Take all instructions (handles) from "start" to "end" and append them after the
7f561c08de6b Initial load
duke
parents:
diff changeset
   630
   * new location "target". Of course, "end" must be after "start" and target must
7f561c08de6b Initial load
duke
parents:
diff changeset
   631
   * not be located withing this range. If you want to move something to the start of
7f561c08de6b Initial load
duke
parents:
diff changeset
   632
   * the list use null as value for target.<br>
7f561c08de6b Initial load
duke
parents:
diff changeset
   633
   * Any instruction targeters pointing to handles within the block, keep their targets.
7f561c08de6b Initial load
duke
parents:
diff changeset
   634
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   635
   * @param start  of moved block
7f561c08de6b Initial load
duke
parents:
diff changeset
   636
   * @param end    of moved block
7f561c08de6b Initial load
duke
parents:
diff changeset
   637
   * @param target of moved block
7f561c08de6b Initial load
duke
parents:
diff changeset
   638
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   639
  public void move(InstructionHandle start, InstructionHandle end, InstructionHandle target) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   640
    // Step 1: Check constraints
7f561c08de6b Initial load
duke
parents:
diff changeset
   641
7f561c08de6b Initial load
duke
parents:
diff changeset
   642
    if((start == null) || (end == null))
7f561c08de6b Initial load
duke
parents:
diff changeset
   643
      throw new ClassGenException("Invalid null handle: From " + start + " to " + end);
7f561c08de6b Initial load
duke
parents:
diff changeset
   644
7f561c08de6b Initial load
duke
parents:
diff changeset
   645
    if((target == start) || (target == end))
7f561c08de6b Initial load
duke
parents:
diff changeset
   646
      throw new ClassGenException("Invalid range: From " + start + " to " + end +
7f561c08de6b Initial load
duke
parents:
diff changeset
   647
                                  " contains target " + target);
7f561c08de6b Initial load
duke
parents:
diff changeset
   648
7f561c08de6b Initial load
duke
parents:
diff changeset
   649
    for(InstructionHandle ih = start; ih != end.next; ih = ih.next) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   650
      if(ih == null) // At end of list, end not found yet
7f561c08de6b Initial load
duke
parents:
diff changeset
   651
        throw new ClassGenException("Invalid range: From " + start + " to " + end);
7f561c08de6b Initial load
duke
parents:
diff changeset
   652
      else if(ih == target) // target may be null
7f561c08de6b Initial load
duke
parents:
diff changeset
   653
        throw new ClassGenException("Invalid range: From " + start + " to " + end +
7f561c08de6b Initial load
duke
parents:
diff changeset
   654
                                    " contains target " + target);
7f561c08de6b Initial load
duke
parents:
diff changeset
   655
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   656
7f561c08de6b Initial load
duke
parents:
diff changeset
   657
    // Step 2: Temporarily remove the given instructions from the list
7f561c08de6b Initial load
duke
parents:
diff changeset
   658
7f561c08de6b Initial load
duke
parents:
diff changeset
   659
    InstructionHandle prev = start.prev, next = end.next;
7f561c08de6b Initial load
duke
parents:
diff changeset
   660
7f561c08de6b Initial load
duke
parents:
diff changeset
   661
    if(prev != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   662
      prev.next = next;
7f561c08de6b Initial load
duke
parents:
diff changeset
   663
    else // start == this.start!
7f561c08de6b Initial load
duke
parents:
diff changeset
   664
      this.start = next;
7f561c08de6b Initial load
duke
parents:
diff changeset
   665
7f561c08de6b Initial load
duke
parents:
diff changeset
   666
    if(next != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   667
      next.prev = prev;
7f561c08de6b Initial load
duke
parents:
diff changeset
   668
    else // end == this.end!
7f561c08de6b Initial load
duke
parents:
diff changeset
   669
      this.end = prev;
7f561c08de6b Initial load
duke
parents:
diff changeset
   670
7f561c08de6b Initial load
duke
parents:
diff changeset
   671
    start.prev = end.next = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   672
7f561c08de6b Initial load
duke
parents:
diff changeset
   673
    // Step 3: append after target
7f561c08de6b Initial load
duke
parents:
diff changeset
   674
7f561c08de6b Initial load
duke
parents:
diff changeset
   675
    if(target == null) { // append to start of list
7f561c08de6b Initial load
duke
parents:
diff changeset
   676
      end.next = this.start;
7f561c08de6b Initial load
duke
parents:
diff changeset
   677
      this.start = start;
7f561c08de6b Initial load
duke
parents:
diff changeset
   678
    } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   679
      next = target.next;
7f561c08de6b Initial load
duke
parents:
diff changeset
   680
7f561c08de6b Initial load
duke
parents:
diff changeset
   681
      target.next = start;
7f561c08de6b Initial load
duke
parents:
diff changeset
   682
      start.prev  = target;
7f561c08de6b Initial load
duke
parents:
diff changeset
   683
      end.next    = next;
7f561c08de6b Initial load
duke
parents:
diff changeset
   684
7f561c08de6b Initial load
duke
parents:
diff changeset
   685
      if(next != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   686
        next.prev = end;
7f561c08de6b Initial load
duke
parents:
diff changeset
   687
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   688
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   689
7f561c08de6b Initial load
duke
parents:
diff changeset
   690
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   691
   * Move a single instruction (handle) to a new location.
7f561c08de6b Initial load
duke
parents:
diff changeset
   692
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   693
   * @param ih     moved instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   694
   * @param target new location of moved instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   695
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   696
  public void move(InstructionHandle ih, InstructionHandle target) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   697
    move(ih, ih, target);
7f561c08de6b Initial load
duke
parents:
diff changeset
   698
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   699
7f561c08de6b Initial load
duke
parents:
diff changeset
   700
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   701
   * Remove from instruction `prev' to instruction `next' both contained
7f561c08de6b Initial load
duke
parents:
diff changeset
   702
   * in this list. Throws TargetLostException when one of the removed instruction handles
7f561c08de6b Initial load
duke
parents:
diff changeset
   703
   * is still being targeted.
7f561c08de6b Initial load
duke
parents:
diff changeset
   704
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   705
   * @param prev where to start deleting (predecessor, exclusive)
7f561c08de6b Initial load
duke
parents:
diff changeset
   706
   * @param next where to end deleting (successor, exclusive)
7f561c08de6b Initial load
duke
parents:
diff changeset
   707
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   708
  private void remove(InstructionHandle prev, InstructionHandle next)
7f561c08de6b Initial load
duke
parents:
diff changeset
   709
    throws TargetLostException
7f561c08de6b Initial load
duke
parents:
diff changeset
   710
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   711
    InstructionHandle first, last; // First and last deleted instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   712
7f561c08de6b Initial load
duke
parents:
diff changeset
   713
    if((prev == null) && (next == null)) { // singleton list
7f561c08de6b Initial load
duke
parents:
diff changeset
   714
      first = last = start;
7f561c08de6b Initial load
duke
parents:
diff changeset
   715
      start = end = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   716
    } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   717
      if(prev == null) { // At start of list
7f561c08de6b Initial load
duke
parents:
diff changeset
   718
        first = start;
7f561c08de6b Initial load
duke
parents:
diff changeset
   719
        start = next;
7f561c08de6b Initial load
duke
parents:
diff changeset
   720
      } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   721
        first     = prev.next;
7f561c08de6b Initial load
duke
parents:
diff changeset
   722
        prev.next = next;
7f561c08de6b Initial load
duke
parents:
diff changeset
   723
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   724
7f561c08de6b Initial load
duke
parents:
diff changeset
   725
      if(next == null) { // At end of list
7f561c08de6b Initial load
duke
parents:
diff changeset
   726
        last = end;
7f561c08de6b Initial load
duke
parents:
diff changeset
   727
        end  = prev;
7f561c08de6b Initial load
duke
parents:
diff changeset
   728
      } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   729
        last      = next.prev;
7f561c08de6b Initial load
duke
parents:
diff changeset
   730
        next.prev = prev;
7f561c08de6b Initial load
duke
parents:
diff changeset
   731
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   732
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   733
7f561c08de6b Initial load
duke
parents:
diff changeset
   734
    first.prev = null; // Completely separated from rest of list
7f561c08de6b Initial load
duke
parents:
diff changeset
   735
    last.next  = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   736
7f561c08de6b Initial load
duke
parents:
diff changeset
   737
    ArrayList target_vec = new ArrayList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   738
7f561c08de6b Initial load
duke
parents:
diff changeset
   739
    for(InstructionHandle ih=first; ih != null; ih = ih.next)
7f561c08de6b Initial load
duke
parents:
diff changeset
   740
      ih.getInstruction().dispose(); // e.g. BranchInstructions release their targets
7f561c08de6b Initial load
duke
parents:
diff changeset
   741
7f561c08de6b Initial load
duke
parents:
diff changeset
   742
    StringBuffer buf = new StringBuffer("{ ");
7f561c08de6b Initial load
duke
parents:
diff changeset
   743
    for(InstructionHandle ih=first; ih != null; ih = next) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   744
      next = ih.next;
7f561c08de6b Initial load
duke
parents:
diff changeset
   745
      length--;
7f561c08de6b Initial load
duke
parents:
diff changeset
   746
7f561c08de6b Initial load
duke
parents:
diff changeset
   747
      if(ih.hasTargeters()) { // Still got targeters?
7f561c08de6b Initial load
duke
parents:
diff changeset
   748
        target_vec.add(ih);
7f561c08de6b Initial load
duke
parents:
diff changeset
   749
        buf.append(ih.toString(true) + " ");
7f561c08de6b Initial load
duke
parents:
diff changeset
   750
        ih.next = ih.prev = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   751
      } else
7f561c08de6b Initial load
duke
parents:
diff changeset
   752
        ih.dispose();
7f561c08de6b Initial load
duke
parents:
diff changeset
   753
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   754
7f561c08de6b Initial load
duke
parents:
diff changeset
   755
    buf.append("}");
7f561c08de6b Initial load
duke
parents:
diff changeset
   756
7f561c08de6b Initial load
duke
parents:
diff changeset
   757
    if(!target_vec.isEmpty()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   758
      InstructionHandle[] targeted = new InstructionHandle[target_vec.size()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   759
      target_vec.toArray(targeted);
7f561c08de6b Initial load
duke
parents:
diff changeset
   760
      throw new TargetLostException(targeted, buf.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   761
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   762
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   763
7f561c08de6b Initial load
duke
parents:
diff changeset
   764
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   765
   * Remove instruction from this list. The corresponding Instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   766
   * handles must not be reused!
7f561c08de6b Initial load
duke
parents:
diff changeset
   767
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   768
   * @param ih instruction (handle) to remove
7f561c08de6b Initial load
duke
parents:
diff changeset
   769
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   770
  public void delete(InstructionHandle ih) throws TargetLostException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   771
    remove(ih.prev, ih.next);
7f561c08de6b Initial load
duke
parents:
diff changeset
   772
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   773
7f561c08de6b Initial load
duke
parents:
diff changeset
   774
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   775
   * Remove instruction from this list. The corresponding Instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   776
   * handles must not be reused!
7f561c08de6b Initial load
duke
parents:
diff changeset
   777
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   778
   * @param i instruction to remove
7f561c08de6b Initial load
duke
parents:
diff changeset
   779
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   780
  public void delete(Instruction i) throws TargetLostException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   781
    InstructionHandle ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   782
7f561c08de6b Initial load
duke
parents:
diff changeset
   783
    if((ih = findInstruction1(i)) == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   784
      throw new ClassGenException("Instruction " + i +
7f561c08de6b Initial load
duke
parents:
diff changeset
   785
                                  " is not contained in this list.");
7f561c08de6b Initial load
duke
parents:
diff changeset
   786
    delete(ih);
7f561c08de6b Initial load
duke
parents:
diff changeset
   787
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   788
7f561c08de6b Initial load
duke
parents:
diff changeset
   789
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   790
   * Remove instructions from instruction `from' to instruction `to' contained
7f561c08de6b Initial load
duke
parents:
diff changeset
   791
   * in this list. The user must ensure that `from' is an instruction before
7f561c08de6b Initial load
duke
parents:
diff changeset
   792
   * `to', or risk havoc. The corresponding Instruction handles must not be reused!
7f561c08de6b Initial load
duke
parents:
diff changeset
   793
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   794
   * @param from where to start deleting (inclusive)
7f561c08de6b Initial load
duke
parents:
diff changeset
   795
   * @param to   where to end deleting (inclusive)
7f561c08de6b Initial load
duke
parents:
diff changeset
   796
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   797
  public void delete(InstructionHandle from, InstructionHandle to)
7f561c08de6b Initial load
duke
parents:
diff changeset
   798
    throws TargetLostException
7f561c08de6b Initial load
duke
parents:
diff changeset
   799
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   800
    remove(from.prev, to.next);
7f561c08de6b Initial load
duke
parents:
diff changeset
   801
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   802
7f561c08de6b Initial load
duke
parents:
diff changeset
   803
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   804
   * Remove instructions from instruction `from' to instruction `to' contained
7f561c08de6b Initial load
duke
parents:
diff changeset
   805
   * in this list. The user must ensure that `from' is an instruction before
7f561c08de6b Initial load
duke
parents:
diff changeset
   806
   * `to', or risk havoc. The corresponding Instruction handles must not be reused!
7f561c08de6b Initial load
duke
parents:
diff changeset
   807
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   808
   * @param from where to start deleting (inclusive)
7f561c08de6b Initial load
duke
parents:
diff changeset
   809
   * @param to   where to end deleting (inclusive)
7f561c08de6b Initial load
duke
parents:
diff changeset
   810
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   811
  public void delete(Instruction from, Instruction to) throws TargetLostException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   812
    InstructionHandle from_ih, to_ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   813
7f561c08de6b Initial load
duke
parents:
diff changeset
   814
    if((from_ih = findInstruction1(from)) == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   815
      throw new ClassGenException("Instruction " + from +
7f561c08de6b Initial load
duke
parents:
diff changeset
   816
                                  " is not contained in this list.");
7f561c08de6b Initial load
duke
parents:
diff changeset
   817
7f561c08de6b Initial load
duke
parents:
diff changeset
   818
    if((to_ih = findInstruction2(to)) == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   819
      throw new ClassGenException("Instruction " + to +
7f561c08de6b Initial load
duke
parents:
diff changeset
   820
                                  " is not contained in this list.");
7f561c08de6b Initial load
duke
parents:
diff changeset
   821
    delete(from_ih, to_ih);
7f561c08de6b Initial load
duke
parents:
diff changeset
   822
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   823
7f561c08de6b Initial load
duke
parents:
diff changeset
   824
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   825
   * Search for given Instruction reference, start at beginning of list.
7f561c08de6b Initial load
duke
parents:
diff changeset
   826
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   827
   * @param i instruction to search for
7f561c08de6b Initial load
duke
parents:
diff changeset
   828
   * @return instruction found on success, null otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
   829
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   830
  private InstructionHandle findInstruction1(Instruction i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   831
    for(InstructionHandle ih=start; ih != null; ih = ih.next)
7f561c08de6b Initial load
duke
parents:
diff changeset
   832
      if(ih.instruction == i)
7f561c08de6b Initial load
duke
parents:
diff changeset
   833
        return ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   834
7f561c08de6b Initial load
duke
parents:
diff changeset
   835
    return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   836
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   837
7f561c08de6b Initial load
duke
parents:
diff changeset
   838
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   839
   * Search for given Instruction reference, start at end of list
7f561c08de6b Initial load
duke
parents:
diff changeset
   840
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   841
   * @param i instruction to search for
7f561c08de6b Initial load
duke
parents:
diff changeset
   842
   * @return instruction found on success, null otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
   843
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   844
  private InstructionHandle findInstruction2(Instruction i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   845
    for(InstructionHandle ih=end; ih != null; ih = ih.prev)
7f561c08de6b Initial load
duke
parents:
diff changeset
   846
      if(ih.instruction == i)
7f561c08de6b Initial load
duke
parents:
diff changeset
   847
        return ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   848
7f561c08de6b Initial load
duke
parents:
diff changeset
   849
    return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   850
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   851
7f561c08de6b Initial load
duke
parents:
diff changeset
   852
  public boolean contains(InstructionHandle i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   853
    if(i == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   854
      return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   855
7f561c08de6b Initial load
duke
parents:
diff changeset
   856
    for(InstructionHandle ih=start; ih != null; ih = ih.next)
7f561c08de6b Initial load
duke
parents:
diff changeset
   857
      if(ih == i)
7f561c08de6b Initial load
duke
parents:
diff changeset
   858
        return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   859
7f561c08de6b Initial load
duke
parents:
diff changeset
   860
    return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   861
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   862
7f561c08de6b Initial load
duke
parents:
diff changeset
   863
  public boolean contains(Instruction i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   864
    return findInstruction1(i) != null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   865
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   866
7f561c08de6b Initial load
duke
parents:
diff changeset
   867
  public void setPositions() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   868
    setPositions(false);
7f561c08de6b Initial load
duke
parents:
diff changeset
   869
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   870
7f561c08de6b Initial load
duke
parents:
diff changeset
   871
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   872
   * Give all instructions their position number (offset in byte stream), i.e.,
7f561c08de6b Initial load
duke
parents:
diff changeset
   873
   * make the list ready to be dumped.
7f561c08de6b Initial load
duke
parents:
diff changeset
   874
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   875
   * @param check Perform sanity checks, e.g. if all targeted instructions really belong
7f561c08de6b Initial load
duke
parents:
diff changeset
   876
   * to this list
7f561c08de6b Initial load
duke
parents:
diff changeset
   877
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   878
  public void setPositions(boolean check) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   879
    int max_additional_bytes = 0, additional_bytes = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   880
    int index = 0, count = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   881
    int[] pos = new int[length];
7f561c08de6b Initial load
duke
parents:
diff changeset
   882
7f561c08de6b Initial load
duke
parents:
diff changeset
   883
    /* Pass 0: Sanity checks
7f561c08de6b Initial load
duke
parents:
diff changeset
   884
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   885
    if(check) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   886
      for(InstructionHandle ih=start; ih != null; ih = ih.next) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   887
        Instruction i = ih.instruction;
7f561c08de6b Initial load
duke
parents:
diff changeset
   888
7f561c08de6b Initial load
duke
parents:
diff changeset
   889
        if(i instanceof BranchInstruction) { // target instruction within list?
7f561c08de6b Initial load
duke
parents:
diff changeset
   890
          Instruction inst = ((BranchInstruction)i).getTarget().instruction;
7f561c08de6b Initial load
duke
parents:
diff changeset
   891
          if(!contains(inst))
7f561c08de6b Initial load
duke
parents:
diff changeset
   892
            throw new ClassGenException("Branch target of " +
7f561c08de6b Initial load
duke
parents:
diff changeset
   893
                                        Constants.OPCODE_NAMES[i.opcode] + ":" +
7f561c08de6b Initial load
duke
parents:
diff changeset
   894
                                        inst + " not in instruction list");
7f561c08de6b Initial load
duke
parents:
diff changeset
   895
7f561c08de6b Initial load
duke
parents:
diff changeset
   896
          if(i instanceof Select) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   897
            InstructionHandle[] targets = ((Select)i).getTargets();
7f561c08de6b Initial load
duke
parents:
diff changeset
   898
7f561c08de6b Initial load
duke
parents:
diff changeset
   899
            for(int j=0; j < targets.length; j++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   900
              inst = targets[j].instruction;
7f561c08de6b Initial load
duke
parents:
diff changeset
   901
              if(!contains(inst))
7f561c08de6b Initial load
duke
parents:
diff changeset
   902
                throw new ClassGenException("Branch target of " +
7f561c08de6b Initial load
duke
parents:
diff changeset
   903
                                            Constants.OPCODE_NAMES[i.opcode] + ":" +
7f561c08de6b Initial load
duke
parents:
diff changeset
   904
                                            inst + " not in instruction list");
7f561c08de6b Initial load
duke
parents:
diff changeset
   905
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   906
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   907
7f561c08de6b Initial load
duke
parents:
diff changeset
   908
          if(!(ih instanceof BranchHandle))
7f561c08de6b Initial load
duke
parents:
diff changeset
   909
            throw new ClassGenException("Branch instruction " +
7f561c08de6b Initial load
duke
parents:
diff changeset
   910
                                        Constants.OPCODE_NAMES[i.opcode] + ":" +
7f561c08de6b Initial load
duke
parents:
diff changeset
   911
                                        inst + " not contained in BranchHandle.");
7f561c08de6b Initial load
duke
parents:
diff changeset
   912
7f561c08de6b Initial load
duke
parents:
diff changeset
   913
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   914
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   915
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   916
7f561c08de6b Initial load
duke
parents:
diff changeset
   917
    /* Pass 1: Set position numbers and sum up the maximum number of bytes an
7f561c08de6b Initial load
duke
parents:
diff changeset
   918
     * instruction may be shifted.
7f561c08de6b Initial load
duke
parents:
diff changeset
   919
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   920
    for(InstructionHandle ih=start; ih != null; ih = ih.next) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   921
      Instruction i = ih.instruction;
7f561c08de6b Initial load
duke
parents:
diff changeset
   922
7f561c08de6b Initial load
duke
parents:
diff changeset
   923
      ih.setPosition(index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   924
      pos[count++] = index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   925
7f561c08de6b Initial load
duke
parents:
diff changeset
   926
      /* Get an estimate about how many additional bytes may be added, because
7f561c08de6b Initial load
duke
parents:
diff changeset
   927
       * BranchInstructions may have variable length depending on the target
7f561c08de6b Initial load
duke
parents:
diff changeset
   928
       * offset (short vs. int) or alignment issues (TABLESWITCH and
7f561c08de6b Initial load
duke
parents:
diff changeset
   929
       * LOOKUPSWITCH).
7f561c08de6b Initial load
duke
parents:
diff changeset
   930
       */
7f561c08de6b Initial load
duke
parents:
diff changeset
   931
      switch(i.getOpcode()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   932
      case Constants.JSR: case Constants.GOTO:
7f561c08de6b Initial load
duke
parents:
diff changeset
   933
        max_additional_bytes += 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
   934
        break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   935
7f561c08de6b Initial load
duke
parents:
diff changeset
   936
      case Constants.TABLESWITCH: case Constants.LOOKUPSWITCH:
7f561c08de6b Initial load
duke
parents:
diff changeset
   937
        max_additional_bytes += 3;
7f561c08de6b Initial load
duke
parents:
diff changeset
   938
        break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   939
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   940
7f561c08de6b Initial load
duke
parents:
diff changeset
   941
      index += i.getLength();
7f561c08de6b Initial load
duke
parents:
diff changeset
   942
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   943
7f561c08de6b Initial load
duke
parents:
diff changeset
   944
    /* Pass 2: Expand the variable-length (Branch)Instructions depending on
7f561c08de6b Initial load
duke
parents:
diff changeset
   945
     * the target offset (short or int) and ensure that branch targets are
7f561c08de6b Initial load
duke
parents:
diff changeset
   946
     * within this list.
7f561c08de6b Initial load
duke
parents:
diff changeset
   947
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   948
    for(InstructionHandle ih=start; ih != null; ih = ih.next)
7f561c08de6b Initial load
duke
parents:
diff changeset
   949
      additional_bytes += ih.updatePosition(additional_bytes, max_additional_bytes);
7f561c08de6b Initial load
duke
parents:
diff changeset
   950
7f561c08de6b Initial load
duke
parents:
diff changeset
   951
    /* Pass 3: Update position numbers (which may have changed due to the
7f561c08de6b Initial load
duke
parents:
diff changeset
   952
     * preceding expansions), like pass 1.
7f561c08de6b Initial load
duke
parents:
diff changeset
   953
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   954
    index=count=0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   955
    for(InstructionHandle ih=start; ih != null; ih = ih.next) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   956
      Instruction i = ih.instruction;
7f561c08de6b Initial load
duke
parents:
diff changeset
   957
7f561c08de6b Initial load
duke
parents:
diff changeset
   958
      ih.setPosition(index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   959
      pos[count++] = index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   960
      index += i.getLength();
7f561c08de6b Initial load
duke
parents:
diff changeset
   961
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   962
7f561c08de6b Initial load
duke
parents:
diff changeset
   963
    byte_positions = new int[count]; // Trim to proper size
7f561c08de6b Initial load
duke
parents:
diff changeset
   964
    System.arraycopy(pos, 0, byte_positions, 0, count);
7f561c08de6b Initial load
duke
parents:
diff changeset
   965
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   966
7f561c08de6b Initial load
duke
parents:
diff changeset
   967
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   968
   * When everything is finished, use this method to convert the instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   969
   * list into an array of bytes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   970
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   971
   * @return the byte code ready to be dumped
7f561c08de6b Initial load
duke
parents:
diff changeset
   972
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   973
  public byte[] getByteCode() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   974
    // Update position indices of instructions
7f561c08de6b Initial load
duke
parents:
diff changeset
   975
    setPositions();
7f561c08de6b Initial load
duke
parents:
diff changeset
   976
7f561c08de6b Initial load
duke
parents:
diff changeset
   977
    ByteArrayOutputStream b   = new ByteArrayOutputStream();
7f561c08de6b Initial load
duke
parents:
diff changeset
   978
    DataOutputStream      out = new DataOutputStream(b);
7f561c08de6b Initial load
duke
parents:
diff changeset
   979
7f561c08de6b Initial load
duke
parents:
diff changeset
   980
    try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   981
      for(InstructionHandle ih=start; ih != null; ih = ih.next) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   982
        Instruction i = ih.instruction;
7f561c08de6b Initial load
duke
parents:
diff changeset
   983
        i.dump(out); // Traverse list
7f561c08de6b Initial load
duke
parents:
diff changeset
   984
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   985
    } catch(IOException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   986
      System.err.println(e);
7f561c08de6b Initial load
duke
parents:
diff changeset
   987
      return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   988
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   989
7f561c08de6b Initial load
duke
parents:
diff changeset
   990
    return b.toByteArray();
7f561c08de6b Initial load
duke
parents:
diff changeset
   991
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   992
7f561c08de6b Initial load
duke
parents:
diff changeset
   993
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   994
   * @return an array of instructions without target information for branch instructions.
7f561c08de6b Initial load
duke
parents:
diff changeset
   995
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   996
  public Instruction[] getInstructions() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   997
    ByteSequence  bytes        = new ByteSequence(getByteCode());
7f561c08de6b Initial load
duke
parents:
diff changeset
   998
    ArrayList     instructions = new ArrayList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   999
7f561c08de6b Initial load
duke
parents:
diff changeset
  1000
    try {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1001
      while(bytes.available() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1002
        instructions.add(Instruction.readInstruction(bytes));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1003
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1004
    } catch(IOException e) { throw new ClassGenException(e.toString()); }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1005
7f561c08de6b Initial load
duke
parents:
diff changeset
  1006
    Instruction[] result = new Instruction[instructions.size()];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1007
    instructions.toArray(result);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1008
    return result;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1009
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1010
7f561c08de6b Initial load
duke
parents:
diff changeset
  1011
  public String toString() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1012
    return toString(true);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1013
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1014
7f561c08de6b Initial load
duke
parents:
diff changeset
  1015
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1016
   * @param verbose toggle output format
7f561c08de6b Initial load
duke
parents:
diff changeset
  1017
   * @return String containing all instructions in this list.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1018
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1019
  public String toString(boolean verbose) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1020
    StringBuffer buf = new StringBuffer();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1021
7f561c08de6b Initial load
duke
parents:
diff changeset
  1022
    for(InstructionHandle ih=start; ih != null; ih = ih.next) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1023
      buf.append(ih.toString(verbose) + "\n");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1024
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1025
7f561c08de6b Initial load
duke
parents:
diff changeset
  1026
    return buf.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1027
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1028
7f561c08de6b Initial load
duke
parents:
diff changeset
  1029
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1030
   * @return Enumeration that lists all instructions (handles)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1031
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1032
  public Iterator iterator() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1033
    return new Iterator() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1034
      private InstructionHandle ih = start;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1035
7f561c08de6b Initial load
duke
parents:
diff changeset
  1036
      public Object next() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1037
        InstructionHandle i = ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1038
        ih = ih.next;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1039
        return i;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1040
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1041
7f561c08de6b Initial load
duke
parents:
diff changeset
  1042
      public void remove() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1043
        throw new UnsupportedOperationException();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1044
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1045
7f561c08de6b Initial load
duke
parents:
diff changeset
  1046
      public boolean hasNext() { return ih != null; }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1047
    };
7f561c08de6b Initial load
duke
parents:
diff changeset
  1048
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1049
7f561c08de6b Initial load
duke
parents:
diff changeset
  1050
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1051
   * @return array containing all instructions (handles)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1052
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1053
  public InstructionHandle[] getInstructionHandles() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1054
    InstructionHandle[] ihs = new InstructionHandle[length];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1055
    InstructionHandle   ih  = start;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1056
7f561c08de6b Initial load
duke
parents:
diff changeset
  1057
    for(int i=0; i < length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1058
      ihs[i] = ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1059
      ih = ih.next;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1060
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1061
7f561c08de6b Initial load
duke
parents:
diff changeset
  1062
    return ihs;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1063
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1064
7f561c08de6b Initial load
duke
parents:
diff changeset
  1065
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1066
   * Get positions (offsets) of all instructions in the list. This relies on that
7f561c08de6b Initial load
duke
parents:
diff changeset
  1067
   * the list has been freshly created from an byte code array, or that setPositions()
7f561c08de6b Initial load
duke
parents:
diff changeset
  1068
   * has been called. Otherwise this may be inaccurate.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1069
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1070
   * @return array containing all instruction's offset in byte code
7f561c08de6b Initial load
duke
parents:
diff changeset
  1071
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1072
  public int[] getInstructionPositions() { return byte_positions; }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1073
7f561c08de6b Initial load
duke
parents:
diff changeset
  1074
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1075
   * @return complete, i.e., deep copy of this list
7f561c08de6b Initial load
duke
parents:
diff changeset
  1076
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1077
  public InstructionList copy() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1078
    HashMap         map = new HashMap();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1079
    InstructionList il  = new InstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1080
7f561c08de6b Initial load
duke
parents:
diff changeset
  1081
    /* Pass 1: Make copies of all instructions, append them to the new list
7f561c08de6b Initial load
duke
parents:
diff changeset
  1082
     * and associate old instruction references with the new ones, i.e.,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1083
     * a 1:1 mapping.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1084
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1085
    for(InstructionHandle ih=start; ih != null; ih = ih.next) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1086
      Instruction i = ih.instruction;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1087
      Instruction c = i.copy(); // Use clone for shallow copy
7f561c08de6b Initial load
duke
parents:
diff changeset
  1088
7f561c08de6b Initial load
duke
parents:
diff changeset
  1089
      if(c instanceof BranchInstruction)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1090
        map.put(ih, il.append((BranchInstruction)c));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1091
      else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1092
        map.put(ih, il.append(c));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1093
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1094
7f561c08de6b Initial load
duke
parents:
diff changeset
  1095
    /* Pass 2: Update branch targets.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1096
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1097
    InstructionHandle ih=start;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1098
    InstructionHandle ch=il.start;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1099
7f561c08de6b Initial load
duke
parents:
diff changeset
  1100
    while(ih != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1101
      Instruction i = ih.instruction;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1102
      Instruction c = ch.instruction;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1103
7f561c08de6b Initial load
duke
parents:
diff changeset
  1104
      if(i instanceof BranchInstruction) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1105
        BranchInstruction bi      = (BranchInstruction)i;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1106
        BranchInstruction bc      = (BranchInstruction)c;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1107
        InstructionHandle itarget = bi.getTarget(); // old target
7f561c08de6b Initial load
duke
parents:
diff changeset
  1108
7f561c08de6b Initial load
duke
parents:
diff changeset
  1109
        // New target is in hash map
7f561c08de6b Initial load
duke
parents:
diff changeset
  1110
        bc.setTarget((InstructionHandle)map.get(itarget));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1111
7f561c08de6b Initial load
duke
parents:
diff changeset
  1112
        if(bi instanceof Select) { // Either LOOKUPSWITCH or TABLESWITCH
7f561c08de6b Initial load
duke
parents:
diff changeset
  1113
          InstructionHandle[] itargets = ((Select)bi).getTargets();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1114
          InstructionHandle[] ctargets = ((Select)bc).getTargets();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1115
7f561c08de6b Initial load
duke
parents:
diff changeset
  1116
          for(int j=0; j < itargets.length; j++) { // Update all targets
7f561c08de6b Initial load
duke
parents:
diff changeset
  1117
            ctargets[j] = (InstructionHandle)map.get(itargets[j]);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1118
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1119
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1120
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1121
7f561c08de6b Initial load
duke
parents:
diff changeset
  1122
      ih = ih.next;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1123
      ch = ch.next;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1124
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1125
7f561c08de6b Initial load
duke
parents:
diff changeset
  1126
    return il;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1127
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1128
7f561c08de6b Initial load
duke
parents:
diff changeset
  1129
  /** Replace all references to the old constant pool with references to the new
7f561c08de6b Initial load
duke
parents:
diff changeset
  1130
   *  constant pool
7f561c08de6b Initial load
duke
parents:
diff changeset
  1131
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1132
  public void replaceConstantPool(ConstantPoolGen old_cp, ConstantPoolGen new_cp) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1133
    for(InstructionHandle ih=start; ih != null; ih = ih.next) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1134
      Instruction i = ih.instruction;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1135
7f561c08de6b Initial load
duke
parents:
diff changeset
  1136
      if(i instanceof CPInstruction) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1137
        CPInstruction ci = (CPInstruction)i;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1138
        Constant      c  = old_cp.getConstant(ci.getIndex());
7f561c08de6b Initial load
duke
parents:
diff changeset
  1139
        ci.setIndex(new_cp.addConstant(c, old_cp));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1140
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1141
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1142
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1143
7f561c08de6b Initial load
duke
parents:
diff changeset
  1144
  private void clear() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1145
    start = end = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1146
    length = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1147
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1148
7f561c08de6b Initial load
duke
parents:
diff changeset
  1149
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1150
   * Delete contents of list. Provides besser memory utilization,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1151
   * because the system then may reuse the instruction handles. This
7f561c08de6b Initial load
duke
parents:
diff changeset
  1152
   * method is typically called right after
7f561c08de6b Initial load
duke
parents:
diff changeset
  1153
   * <href="MethodGen.html#getMethod()">MethodGen.getMethod()</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1154
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1155
  public void dispose() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1156
    // Traverse in reverse order, because ih.next is overwritten
7f561c08de6b Initial load
duke
parents:
diff changeset
  1157
    for(InstructionHandle ih=end; ih != null; ih = ih.prev)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1158
      /* Causes BranchInstructions to release target and targeters, because it
7f561c08de6b Initial load
duke
parents:
diff changeset
  1159
       * calls dispose() on the contained instruction.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1160
       */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1161
      ih.dispose();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1162
7f561c08de6b Initial load
duke
parents:
diff changeset
  1163
    clear();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1164
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1165
7f561c08de6b Initial load
duke
parents:
diff changeset
  1166
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1167
   * @return start of list
7f561c08de6b Initial load
duke
parents:
diff changeset
  1168
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1169
  public InstructionHandle getStart() { return start; }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1170
7f561c08de6b Initial load
duke
parents:
diff changeset
  1171
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1172
   * @return end of list
7f561c08de6b Initial load
duke
parents:
diff changeset
  1173
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1174
  public InstructionHandle getEnd()   { return end; }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1175
7f561c08de6b Initial load
duke
parents:
diff changeset
  1176
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1177
   * @return length of list (Number of instructions, not bytes)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1178
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1179
  public int getLength() { return length; }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1180
7f561c08de6b Initial load
duke
parents:
diff changeset
  1181
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1182
   * @return length of list (Number of instructions, not bytes)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1183
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1184
  public int size() { return length; }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1185
7f561c08de6b Initial load
duke
parents:
diff changeset
  1186
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1187
   * Redirect all references from old_target to new_target, i.e., update targets
7f561c08de6b Initial load
duke
parents:
diff changeset
  1188
   * of branch instructions.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1189
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1190
   * @param old_target the old target instruction handle
7f561c08de6b Initial load
duke
parents:
diff changeset
  1191
   * @param new_target the new target instruction handle
7f561c08de6b Initial load
duke
parents:
diff changeset
  1192
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1193
  public void redirectBranches(InstructionHandle old_target,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1194
                               InstructionHandle new_target) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1195
    for(InstructionHandle ih = start; ih != null; ih = ih.next) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1196
      Instruction i  = ih.getInstruction();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1197
7f561c08de6b Initial load
duke
parents:
diff changeset
  1198
      if(i instanceof BranchInstruction) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1199
        BranchInstruction b      = (BranchInstruction)i;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1200
        InstructionHandle target = b.getTarget();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1201
7f561c08de6b Initial load
duke
parents:
diff changeset
  1202
        if(target == old_target)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1203
          b.setTarget(new_target);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1204
7f561c08de6b Initial load
duke
parents:
diff changeset
  1205
        if(b instanceof Select) { // Either LOOKUPSWITCH or TABLESWITCH
7f561c08de6b Initial load
duke
parents:
diff changeset
  1206
          InstructionHandle[] targets = ((Select)b).getTargets();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1207
7f561c08de6b Initial load
duke
parents:
diff changeset
  1208
          for(int j=0; j < targets.length; j++) // Update targets
7f561c08de6b Initial load
duke
parents:
diff changeset
  1209
            if(targets[j] == old_target)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1210
              ((Select)b).setTarget(j, new_target);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1211
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1212
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1213
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1214
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1215
7f561c08de6b Initial load
duke
parents:
diff changeset
  1216
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1217
   * Redirect all references of local variables from old_target to new_target.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1218
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1219
   * @param lg array of local variables
7f561c08de6b Initial load
duke
parents:
diff changeset
  1220
   * @param old_target the old target instruction handle
7f561c08de6b Initial load
duke
parents:
diff changeset
  1221
   * @param new_target the new target instruction handle
7f561c08de6b Initial load
duke
parents:
diff changeset
  1222
   * @see MethodGen
7f561c08de6b Initial load
duke
parents:
diff changeset
  1223
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1224
  public void redirectLocalVariables(LocalVariableGen[] lg,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1225
                                     InstructionHandle old_target,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1226
                                     InstructionHandle new_target) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1227
    for(int i=0; i < lg.length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1228
      InstructionHandle start = lg[i].getStart();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1229
      InstructionHandle end   = lg[i].getEnd();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1230
7f561c08de6b Initial load
duke
parents:
diff changeset
  1231
      if(start == old_target)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1232
        lg[i].setStart(new_target);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1233
7f561c08de6b Initial load
duke
parents:
diff changeset
  1234
      if(end == old_target)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1235
        lg[i].setEnd(new_target);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1236
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1237
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1238
7f561c08de6b Initial load
duke
parents:
diff changeset
  1239
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1240
   * Redirect all references of exception handlers from old_target to new_target.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1241
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1242
   * @param exceptions array of exception handlers
7f561c08de6b Initial load
duke
parents:
diff changeset
  1243
   * @param old_target the old target instruction handle
7f561c08de6b Initial load
duke
parents:
diff changeset
  1244
   * @param new_target the new target instruction handle
7f561c08de6b Initial load
duke
parents:
diff changeset
  1245
   * @see MethodGen
7f561c08de6b Initial load
duke
parents:
diff changeset
  1246
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1247
  public void redirectExceptionHandlers(CodeExceptionGen[] exceptions,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1248
                                        InstructionHandle old_target,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1249
                                        InstructionHandle new_target) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1250
    for(int i=0; i < exceptions.length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1251
      if(exceptions[i].getStartPC() == old_target)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1252
        exceptions[i].setStartPC(new_target);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1253
7f561c08de6b Initial load
duke
parents:
diff changeset
  1254
      if(exceptions[i].getEndPC() == old_target)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1255
        exceptions[i].setEndPC(new_target);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1256
7f561c08de6b Initial load
duke
parents:
diff changeset
  1257
      if(exceptions[i].getHandlerPC() == old_target)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1258
        exceptions[i].setHandlerPC(new_target);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1259
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1260
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1261
7f561c08de6b Initial load
duke
parents:
diff changeset
  1262
  private ArrayList observers;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1263
7f561c08de6b Initial load
duke
parents:
diff changeset
  1264
  /** Add observer for this object.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1265
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1266
  public void addObserver(InstructionListObserver o) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1267
    if(observers == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1268
      observers = new ArrayList();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1269
7f561c08de6b Initial load
duke
parents:
diff changeset
  1270
    observers.add(o);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1271
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1272
7f561c08de6b Initial load
duke
parents:
diff changeset
  1273
  /** Remove observer for this object.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1274
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1275
  public void removeObserver(InstructionListObserver o) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1276
    if(observers != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1277
      observers.remove(o);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1278
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1279
7f561c08de6b Initial load
duke
parents:
diff changeset
  1280
  /** Call notify() method on all observers. This method is not called
7f561c08de6b Initial load
duke
parents:
diff changeset
  1281
   * automatically whenever the state has changed, but has to be
7f561c08de6b Initial load
duke
parents:
diff changeset
  1282
   * called by the user after he has finished editing the object.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1283
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1284
  public void update() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1285
    if(observers != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1286
      for(Iterator e = observers.iterator(); e.hasNext(); )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1287
        ((InstructionListObserver)e.next()).notify(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1288
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1289
}