/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache BCEL" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache BCEL", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
*/
/**
* This class is a container for a list of <a
* href="Instruction.html">Instruction</a> objects. Instructions can
* be appended, inserted, moved, deleted, etc.. Instructions are being
* wrapped into <a
* href="InstructionHandle.html">InstructionHandles</a> objects that
* (read only) access to the list structure, such that it can be traversed and
* manipulated in a controlled way.
*
* A list is finally dumped to a byte code array with <a
* href="#getByteCode()">getByteCode</a>.
*
* @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
* @see Instruction
* @see InstructionHandle
* @see BranchHandle
*/
/**
* Create (empty) instruction list.
*/
public InstructionList() {}
/**
* Create instruction list containing one instruction.
* @param i initial instruction
*/
append(i);
}
/**
* Create instruction list containing one instruction.
* @param i initial instruction
*/
append(i);
}
/**
* Initialize list with (nonnull) compound instruction. Consumes argument
* list, i.e., it becomes empty.
*
* @param c compound instruction (list)
*/
append(c.getInstructionList());
}
/**
* Test for empty list.
*/
/**
* Find the target instruction (handle) that corresponds to the given target
* position (byte code offset).
*
* @param ihs array of instruction handles, i.e. il.getInstructionHandles()
* @param pos array of positions corresponding to ihs, i.e. il.getInstructionPositions()
* @param count length of arrays
* @param target target position to search for
* @return target position's instruction handle if available
*/
int target) {
/* Do a binary search since the pos array is orderd.
*/
do {
int i = (l + r) / 2;
int j = pos[i];
if(j == target) // target found
return ihs[i];
else if(target < j) // else constrain search area
r = i - 1;
else // target > j
l = i + 1;
} while(l <= r);
return null;
}
/**
* Get instruction handle for instruction at byte code position pos.
* This only works properly, if the list is freshly initialized from a byte array or
* setPositions() has been called before this method.
*
* @param pos byte code position to search for
* @return target position's instruction handle if available
*/
}
/**
* Initialize instruction list from byte array.
*
* @param code byte array containing the instructions
*/
/* Pass 1: Create an object for each byte code and append them
* to the list.
*/
try {
// Remember byte offset and associate it with the instruction
/* Read one instruction from the byte stream, the byte position is set
* accordingly.
*/
if(i instanceof BranchInstruction) // Use proper append() method
else
count++;
}
/* Pass 2: Look for BranchInstruction and update their targets, i.e.,
* convert offsets to instruction handles.
*/
for(int i=0; i < count; i++) {
if(ihs[i] instanceof BranchHandle) {
* relative -> absolute. */
// Search for target position
// If it is a Select instruction, update all branch targets
int[] indices = s.getIndices();
}
}
}
}
}
/**
* Append another list after instruction (handle) ih contained in this list.
* Consumes argument list, i.e., it becomes empty.
*
* @param ih where to append the instruction list
* @param il Instruction list to append to this one
* @return instruction handle pointing to the <B>first</B> appended instruction
*/
throw new ClassGenException("Appending null InstructionList");
return ih;
else
return ret;
}
/**
* Append another list after instruction i contained in this list.
* Consumes argument list, i.e., it becomes empty.
*
* @param i where to append the instruction list
* @param il Instruction list to append to this one
* @return instruction handle pointing to the <B>first</B> appended instruction
*/
throw new ClassGenException("Instruction " + i +
" is not contained in this list.");
}
/**
* Append another list to this one.
* Consumes argument list, i.e., it becomes empty.
*
* @param il list to append to end of this list
* @return instruction handle of the <B>first</B> appended instruction
*/
throw new ClassGenException("Appending null InstructionList");
return null;
if(isEmpty()) {
return start;
} else
}
/**
* Append an instruction to the end of this list.
*
* @param ih instruction to append
*/
if(isEmpty()) {
}
else {
}
length++; // Update length
}
/**
* Append an instruction to the end of this list.
*
* @param i instruction to append
* @return instruction handle of the appended instruction
*/
return ih;
}
/**
* Append a branch instruction to the end of this list.
*
* @param i branch instruction to append
* @return branch instruction handle of the appended instruction
*/
return ih;
}
/**
* Append a single instruction j after another instruction i, which
* must be in this list of course!
*
* @param i Instruction in list
* @param j Instruction to append after i in list
* @return instruction handle of the first appended instruction
*/
return append(i, new InstructionList(j));
}
/**
* Append a compound instruction, after instruction i.
*
* @param i Instruction in list
* @param c The composite instruction (containing an InstructionList)
* @return instruction handle of the first appended instruction
*/
return append(i, c.getInstructionList());
}
/**
* Append a compound instruction.
*
* @param c The composite instruction (containing an InstructionList)
* @return instruction handle of the first appended instruction
*/
return append(c.getInstructionList());
}
/**
* Append a compound instruction.
*
* @param ih where to append the instruction list
* @param c The composite instruction (containing an InstructionList)
* @return instruction handle of the first appended instruction
*/
}
/**
* Append an instruction after instruction (handle) ih contained in this list.
*
* @param ih where to append the instruction list
* @param i Instruction to append
* @return instruction handle pointing to the <B>first</B> appended instruction
*/
}
/**
* Append an instruction after instruction (handle) ih contained in this list.
*
* @param ih where to append the instruction list
* @param i Instruction to append
* @return instruction handle pointing to the <B>first</B> appended instruction
*/
return bh;
}
/**
* Insert another list before Instruction handle ih contained in this list.
* Consumes argument list, i.e., it becomes empty.
*
* @param i where to append the instruction list
* @param il Instruction list to insert
* @return instruction handle of the first inserted instruction
*/
throw new ClassGenException("Inserting null InstructionList");
return ih;
else
return ret;
}
/**
* Insert another list.
*
* @param il list to insert before start of this list
* @return instruction handle of the first inserted instruction
*/
if(isEmpty()) {
return start;
}
else
}
/**
* Insert an instruction at start of this list.
*
* @param ih instruction to insert
*/
if(isEmpty()) {
} else {
}
length++;
}
/**
* Insert another list before Instruction i contained in this list.
* Consumes argument list, i.e., it becomes empty.
*
* @param i where to append the instruction list
* @param il Instruction list to insert
* @return instruction handle pointing to the first inserted instruction,
* i.e., il.getStart()
*/
throw new ClassGenException("Instruction " + i +
" is not contained in this list.");
}
/**
* Insert an instruction at start of this list.
*
* @param i instruction to insert
* @return instruction handle of the inserted instruction
*/
return ih;
}
/**
* Insert a branch instruction at start of this list.
*
* @param i branch instruction to insert
* @return branch instruction handle of the appended instruction
*/
return ih;
}
/**
* Insert a single instruction j before another instruction i, which
* must be in this list of course!
*
* @param i Instruction in list
* @param j Instruction to insert before i in list
* @return instruction handle of the first inserted instruction
*/
return insert(i, new InstructionList(j));
}
/**
* Insert a compound instruction before instruction i.
*
* @param i Instruction in list
* @param c The composite instruction (containing an InstructionList)
* @return instruction handle of the first inserted instruction
*/
return insert(i, c.getInstructionList());
}
/**
* Insert a compound instruction.
*
* @param c The composite instruction (containing an InstructionList)
* @return instruction handle of the first inserted instruction
*/
return insert(c.getInstructionList());
}
/**
* Insert an instruction before instruction (handle) ih contained in this list.
*
* @param ih where to insert to the instruction list
* @param i Instruction to insert
* @return instruction handle of the first inserted instruction
*/
}
/**
* Insert a compound instruction.
*
* @param ih where to insert the instruction list
* @param c The composite instruction (containing an InstructionList)
* @return instruction handle of the first inserted instruction
*/
}
/**
* Insert an instruction before instruction (handle) ih contained in this list.
*
* @param ih where to insert to the instruction list
* @param i Instruction to insert
* @return instruction handle of the first inserted instruction
*/
return bh;
}
/**
* Take all instructions (handles) from "start" to "end" and append them after the
* new location "target". Of course, "end" must be after "start" and target must
* not be located withing this range. If you want to move something to the start of
* the list use null as value for target.<br>
* Any instruction targeters pointing to handles within the block, keep their targets.
*
* @param start of moved block
* @param end of moved block
* @param target of moved block
*/
// Step 1: Check constraints
" contains target " + target);
" contains target " + target);
}
// Step 2: Temporarily remove the given instructions from the list
else // start == this.start!
else // end == this.end!
// Step 3: append after target
} else {
}
}
/**
* Move a single instruction (handle) to a new location.
*
* @param ih moved instruction
* @param target new location of moved instruction
*/
}
/**
* Remove from instruction `prev' to instruction `next' both contained
* in this list. Throws TargetLostException when one of the removed instruction handles
* is still being targeted.
*
* @param prev where to start deleting (predecessor, exclusive)
* @param next where to end deleting (successor, exclusive)
*/
throws TargetLostException
{
} else {
} else {
}
} else {
}
}
length--;
} else
}
if(!target_vec.isEmpty()) {
}
}
/**
* Remove instruction from this list. The corresponding Instruction
* handles must not be reused!
*
* @param ih instruction (handle) to remove
*/
}
/**
* Remove instruction from this list. The corresponding Instruction
* handles must not be reused!
*
* @param i instruction to remove
*/
throw new ClassGenException("Instruction " + i +
" is not contained in this list.");
}
/**
* Remove instructions from instruction `from' to instruction `to' contained
* in this list. The user must ensure that `from' is an instruction before
* `to', or risk havoc. The corresponding Instruction handles must not be reused!
*
* @param from where to start deleting (inclusive)
* @param to where to end deleting (inclusive)
*/
throws TargetLostException
{
}
/**
* Remove instructions from instruction `from' to instruction `to' contained
* in this list. The user must ensure that `from' is an instruction before
* `to', or risk havoc. The corresponding Instruction handles must not be reused!
*
* @param from where to start deleting (inclusive)
* @param to where to end deleting (inclusive)
*/
" is not contained in this list.");
" is not contained in this list.");
}
/**
* Search for given Instruction reference, start at beginning of list.
*
* @param i instruction to search for
* @return instruction found on success, null otherwise
*/
if(ih.instruction == i)
return ih;
return null;
}
/**
* Search for given Instruction reference, start at end of list
*
* @param i instruction to search for
* @return instruction found on success, null otherwise
*/
if(ih.instruction == i)
return ih;
return null;
}
if(i == null)
return false;
if(ih == i)
return true;
return false;
}
return findInstruction1(i) != null;
}
public void setPositions() {
setPositions(false);
}
/**
* Give all instructions their position number (offset in byte stream), i.e.,
* make the list ready to be dumped.
*
* @param check Perform sanity checks, e.g. if all targeted instructions really belong
* to this list
*/
/* Pass 0: Sanity checks
*/
if(check) {
if(i instanceof BranchInstruction) { // target instruction within list?
throw new ClassGenException("Branch target of " +
inst + " not in instruction list");
if(i instanceof Select) {
throw new ClassGenException("Branch target of " +
inst + " not in instruction list");
}
}
if(!(ih instanceof BranchHandle))
throw new ClassGenException("Branch instruction " +
inst + " not contained in BranchHandle.");
}
}
}
/* Pass 1: Set position numbers and sum up the maximum number of bytes an
* instruction may be shifted.
*/
/* Get an estimate about how many additional bytes may be added, because
* BranchInstructions may have variable length depending on the target
* offset (short vs. int) or alignment issues (TABLESWITCH and
* LOOKUPSWITCH).
*/
switch(i.getOpcode()) {
max_additional_bytes += 2;
break;
max_additional_bytes += 3;
break;
}
}
/* Pass 2: Expand the variable-length (Branch)Instructions depending on
* the target offset (short or int) and ensure that branch targets are
* within this list.
*/
/* Pass 3: Update position numbers (which may have changed due to the
* preceding expansions), like pass 1.
*/
}
}
/**
* When everything is finished, use this method to convert the instruction
* list into an array of bytes.
*
* @return the byte code ready to be dumped
*/
public byte[] getByteCode() {
// Update position indices of instructions
setPositions();
ByteArrayOutputStream b = new ByteArrayOutputStream();
try {
}
} catch(IOException e) {
return null;
}
return b.toByteArray();
}
/**
* @return an array of instructions without target information for branch instructions.
*/
try {
}
return result;
}
return toString(true);
}
/**
* @param verbose toggle output format
* @return String containing all instructions in this list.
*/
}
}
/**
* @return Enumeration that lists all instructions (handles)
*/
return new Iterator() {
InstructionHandle i = ih;
return i;
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
/**
* @return array containing all instructions (handles)
*/
for(int i=0; i < length; i++) {
}
return ihs;
}
/**
* Get positions (offsets) of all instructions in the list. This relies on that
* the list has been freshly created from an byte code array, or that setPositions()
* has been called. Otherwise this may be inaccurate.
*
* @return array containing all instruction's offset in byte code
*/
/**
* @return complete, i.e., deep copy of this list
*/
/* Pass 1: Make copies of all instructions, append them to the new list
* and associate old instruction references with the new ones, i.e.,
* a 1:1 mapping.
*/
if(c instanceof BranchInstruction)
else
}
/* Pass 2: Update branch targets.
*/
if(i instanceof BranchInstruction) {
// New target is in hash map
}
}
}
}
return il;
}
/** Replace all references to the old constant pool with references to the new
* constant pool
*/
if(i instanceof CPInstruction) {
}
}
}
private void clear() {
length = 0;
}
/**
* Delete contents of list. Provides besser memory utilization,
* because the system then may reuse the instruction handles. This
* method is typically called right after
* <href="MethodGen.html#getMethod()">MethodGen.getMethod()</a>.
*/
public void dispose() {
// Traverse in reverse order, because ih.next is overwritten
/* Causes BranchInstructions to release target and targeters, because it
* calls dispose() on the contained instruction.
*/
clear();
}
/**
* @return start of list
*/
/**
* @return end of list
*/
/**
* @return length of list (Number of instructions, not bytes)
*/
/**
* @return length of list (Number of instructions, not bytes)
*/
/**
* Redirect all references from old_target to new_target, i.e., update targets
* of branch instructions.
*
* @param old_target the old target instruction handle
* @param new_target the new target instruction handle
*/
if(i instanceof BranchInstruction) {
BranchInstruction b = (BranchInstruction)i;
if(target == old_target)
b.setTarget(new_target);
if(b instanceof Select) { // Either LOOKUPSWITCH or TABLESWITCH
if(targets[j] == old_target)
}
}
}
}
/**
* Redirect all references of local variables from old_target to new_target.
*
* @param lg array of local variables
* @param old_target the old target instruction handle
* @param new_target the new target instruction handle
* @see MethodGen
*/
if(start == old_target)
if(end == old_target)
}
}
/**
* Redirect all references of exception handlers from old_target to new_target.
*
* @param exceptions array of exception handlers
* @param old_target the old target instruction handle
* @param new_target the new target instruction handle
* @see MethodGen
*/
}
}
/** Add observer for this object.
*/
}
/** Remove observer for this object.
*/
}
/** Call notify() method on all observers. This method is not called
* automatically whenever the state has changed, but has to be
* called by the user after he has finished editing the object.
*/
public void update() {
}
}