FileDocCategorySizeDatePackage
Simulator.javaAPI DocAndroid 1.5 API25983Wed May 06 22:41:02 BST 2009com.android.dx.cf.code

Simulator

public class Simulator extends Object
Class which knows how to simulate the effects of executing bytecode.

Note: This class is not thread-safe. If multiple threads need to use a single instance, they must synchronize access explicitly between themselves.

Fields Summary
private static final String
LOCAL_MISMATCH_ERROR
non-null; canned error message for local variable table mismatches
private final Machine
machine
non-null; machine to use when simulating
private final BytecodeArray
code
non-null; array of bytecode
private final LocalVariableList
localVariables
non-null; local variable information
private final SimVisitor
visitor
non-null; visitor instance to use
Constructors Summary
public Simulator(Machine machine, ConcreteMethod method)
Constructs an instance.

param
machine non-null; machine to use when simulating
param
method non-null; method data to use


                            
         
        if (machine == null) {
            throw new NullPointerException("machine == null");
        }

        if (method == null) {
            throw new NullPointerException("method == null");
        }

        this.machine = machine;
        this.code = method.getCode();
        this.localVariables = method.getLocalVariables();
        this.visitor = new SimVisitor();
    
Methods Summary
private static SimExceptionillegalTos()
Constructs an "illegal top-of-stack" exception, for the stack manipulation opcodes.

        return new SimException("stack mismatch: illegal " +
                "top-of-stack for opcode");
    
public voidsimulate(ByteBlock bb, Frame frame)
Simulates the effect of executing the given basic block. This modifies the passed-in frame to represent the end result.

param
bb non-null; the basic block
param
frame non-null; frame to operate on

        int end = bb.getEnd();

        visitor.setFrame(frame);

        try {
            for (int off = bb.getStart(); off < end; /*off*/) {
                int length = code.parseInstruction(off, visitor);
                visitor.setPreviousOffset(off);
                off += length;
            }
        } catch (SimException ex) {
            frame.annotate(ex);
            throw ex;
        }
    
public intsimulate(int offset, Frame frame)
Simulates the effect of the instruction at the given offset, by making appropriate calls on the given frame.

param
offset >= 0; offset of the instruction to simulate
param
frame non-null; frame to operate on
return
the length of the instruction, in bytes

        visitor.setFrame(frame);
        return code.parseInstruction(offset, visitor);