edu.arizona.cs.mbel.instructions
Class InstructionList

java.lang.Object
  extended byedu.arizona.cs.mbel.instructions.InstructionList

public class InstructionList
extends java.lang.Object

Represents a contiguous list of instructions, as in a method. The underlying structure is a linked list of InstructionHandle objects. There is also a factory method to parse an entire instruction list, using the Instruction.parseInstruction method. There are also methods to assign byte offsets to the InstructionHandles once user manipulation is complete.


Constructor Summary
InstructionList()
          Creates an InstructionList with no instructions in it.
InstructionList(ClassParser parse, long CodeSize)
          Parses an InstructionList from a file
 
Method Summary
 InstructionHandle append(Instruction i)
          Adds an instruction to the end of the list
 InstructionHandle append(InstructionHandle handle)
          Adds an InstructionHandle to the end of the list
 void clear()
          Deletes all the instructions in the list
 boolean contains(Instruction i)
          Returns true iff this InstructionList contains the given instruction
 boolean contains(InstructionHandle ih)
          Returns true iff this InstrucitonList contains the given handle
 void delete(Instruction i)
          Deletes the first occurrence of the given instruction from the list
 void delete(InstructionHandle ih)
          Deletes the given handle from the list.
 void delete(InstructionHandle from, InstructionHandle to)
          Searches for 'from' and 'to'.
 void delete(Instruction from, Instruction to)
          Searches for 'from' and 'to'.
 void emit(ByteBuffer buffer, ClassEmitter emitter)
          Write this InstructionList out to a buffer
 InstructionHandle getEnd()
          Returns the handle of the last instruction in this list
 InstructionHandle getHandleAt(int position)
          Returns the InstructionHandle whose byte offset corresponds to position (not to be used until after a call to setPositions)
 InstructionHandle getHandleEndingAt(int position)
          Returns the InstrucitonHandle that would end at byte offset 'position' (i.e. the next byte after this instruction is 'position')
 InstructionHandle[] getInstructionHandles()
          Returns an array of the handles in this list, in order.
 int[] getInstructionPositions()
          Returns the list of byte offset for each instruction (must be used after setPositions)
 Instruction[] getInstructions()
          Returns the instructions in this list, in order
 int getLength()
          Returns the number of instructions in this list
 int getSizeInBytes()
          Returns the total size in bytes of all the instructions in this list put together.
 InstructionHandle getStart()
          Returns the handle of the first instruction in this list
 boolean isEmpty()
          Returns true iff the list has no instructions in it
 java.util.Iterator iterator()
           
 InstructionHandle prepend(Instruction i)
          Adds this instruction as the first instruction in the list
 InstructionHandle prepend(InstructionHandle handle)
          Adds this InstructionHandle as the first handle in the list
 void setPositions()
          Sets the byte offset in the InstructionHandles of this list.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

InstructionList

public InstructionList()
Creates an InstructionList with no instructions in it.


InstructionList

public InstructionList(ClassParser parse,
                       long CodeSize)
                throws java.io.IOException,
                       MSILParseException
Parses an InstructionList from a file

Parameters:
parse - the ClassParser for this module
CodeSize - the size in bytes of the instrucitons on disk; used as a stopping point
Method Detail

iterator

public java.util.Iterator iterator()

getHandleAt

public InstructionHandle getHandleAt(int position)
Returns the InstructionHandle whose byte offset corresponds to position (not to be used until after a call to setPositions)

Parameters:
position - the byte offset to search for (starts before any prefixes on the instruction)
Returns:
the InstructionHandle starting at the given byte offset, or null if there is none

getHandleEndingAt

public InstructionHandle getHandleEndingAt(int position)
Returns the InstrucitonHandle that would end at byte offset 'position' (i.e. the next byte after this instruction is 'position')

Parameters:
position - the byte offset right after the target instruction
Returns:
the InstructionHandle of the desired instruction, or null if none

append

public InstructionHandle append(Instruction i)
Adds an instruction to the end of the list

Returns:
the new handle of the added instruction

append

public InstructionHandle append(InstructionHandle handle)
Adds an InstructionHandle to the end of the list

Returns:
handle

prepend

public InstructionHandle prepend(Instruction i)
Adds this instruction as the first instruction in the list

Returns:
the new handle of the added instruction

prepend

public InstructionHandle prepend(InstructionHandle handle)
Adds this InstructionHandle as the first handle in the list

Returns:
handle

contains

public boolean contains(InstructionHandle ih)
Returns true iff this InstrucitonList contains the given handle


contains

public boolean contains(Instruction i)
Returns true iff this InstructionList contains the given instruction


clear

public void clear()
Deletes all the instructions in the list


delete

public void delete(InstructionHandle ih)
            throws TargetLostException
Deletes the given handle from the list.

Throws:
TargetLostException

delete

public void delete(Instruction i)
            throws TargetLostException
Deletes the first occurrence of the given instruction from the list

Throws:
TargetLostException

delete

public void delete(InstructionHandle from,
                   InstructionHandle to)
            throws TargetLostException
Searches for 'from' and 'to'. If both are found and 'to' is later in the list than 'from', all instructions between 'from' and 'to' (inclusive) are removed from the list. The handle comparison is done by reference.

Throws:
TargetLostException

delete

public void delete(Instruction from,
                   Instruction to)
            throws TargetLostException
Searches for 'from' and 'to'. If both are found and 'to' is later in the list than 'from', all instructions between 'from' and 'to' (inclusive) are removed from the list.

Throws:
TargetLostException

getLength

public int getLength()
Returns the number of instructions in this list


getEnd

public InstructionHandle getEnd()
Returns the handle of the last instruction in this list


getStart

public InstructionHandle getStart()
Returns the handle of the first instruction in this list


getInstructionHandles

public InstructionHandle[] getInstructionHandles()
Returns an array of the handles in this list, in order.


getInstructionPositions

public int[] getInstructionPositions()
Returns the list of byte offset for each instruction (must be used after setPositions)


getInstructions

public Instruction[] getInstructions()
Returns the instructions in this list, in order


isEmpty

public boolean isEmpty()
Returns true iff the list has no instructions in it


setPositions

public void setPositions()
Sets the byte offset in the InstructionHandles of this list. These are calculated starting from 0 and using Instruction.getLength()


emit

public void emit(ByteBuffer buffer,
                 ClassEmitter emitter)
Write this InstructionList out to a buffer


getSizeInBytes

public int getSizeInBytes()
Returns the total size in bytes of all the instructions in this list put together. Must be called after setPositions.