|
| 1 | +/* |
| 2 | + * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product |
| 5 | + * that is described in this document. In particular, and without limitation, these intellectual property |
| 6 | + * rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or |
| 7 | + * more additional patents or pending patent applications in the U.S. and in other countries. |
| 8 | + * |
| 9 | + * U.S. Government Rights - Commercial software. Government users are subject to the Sun |
| 10 | + * Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its |
| 11 | + * supplements. |
| 12 | + * |
| 13 | + * Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or |
| 14 | + * registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks |
| 15 | + * are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the |
| 16 | + * U.S. and other countries. |
| 17 | + * |
| 18 | + * UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open |
| 19 | + * Company, Ltd. |
| 20 | + */ |
| 21 | +package com.sun.max.tele.interpreter; |
| 22 | + |
| 23 | +import java.util.*; |
| 24 | + |
| 25 | +import com.sun.max.vm.actor.holder.*; |
| 26 | +import com.sun.max.vm.actor.member.*; |
| 27 | +import com.sun.max.vm.classfile.*; |
| 28 | +import com.sun.max.vm.classfile.constant.*; |
| 29 | +import com.sun.max.vm.value.*; |
| 30 | + |
| 31 | +/** |
| 32 | + * Instances of this class represent individual execution frame entries on a given ExecutionThread's execution stack. |
| 33 | + * |
| 34 | + * @author Athul Acharya |
| 35 | + */ |
| 36 | +class ExecutionFrame { |
| 37 | + |
| 38 | + private final ClassMethodActor method; |
| 39 | + private int currentOpcodePosition; |
| 40 | + private int currentBytePosition; |
| 41 | + private final Value[] locals; |
| 42 | + private final Stack<Value> operands; |
| 43 | + private final ExecutionFrame callersFrame; |
| 44 | + private final byte[] code; |
| 45 | + private final int depth; |
| 46 | + |
| 47 | + public ExecutionFrame(ExecutionFrame callersFrame, ClassMethodActor method) { |
| 48 | + this.method = method; |
| 49 | + this.locals = new Value[method.codeAttribute().maxLocals]; |
| 50 | + this.operands = new Stack<Value>(); |
| 51 | + this.callersFrame = callersFrame; |
| 52 | + this.code = method.codeAttribute().code(); |
| 53 | + this.depth = callersFrame == null ? 1 : callersFrame.depth + 1; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Computes the number of frames on the call stack up to and including this frame. |
| 58 | + */ |
| 59 | + public int depth() { |
| 60 | + return depth; |
| 61 | + } |
| 62 | + |
| 63 | + public ExecutionFrame callersFrame() { |
| 64 | + return callersFrame; |
| 65 | + } |
| 66 | + |
| 67 | + public void setLocal(int index, Value value) { |
| 68 | + locals[index] = value; |
| 69 | + } |
| 70 | + |
| 71 | + public int readOpcode() { |
| 72 | + currentOpcodePosition = currentBytePosition; |
| 73 | + return readByte() & 0xff; |
| 74 | + } |
| 75 | + |
| 76 | + public byte readByte() { |
| 77 | + try { |
| 78 | + return code[currentBytePosition++]; |
| 79 | + } catch (ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException) { |
| 80 | + throw new VerifyError("Ran off end of code"); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public short readShort() { |
| 85 | + final int high = readByte(); |
| 86 | + final int low = readByte() & 0xff; |
| 87 | + return (short) ((high << 8) | low); |
| 88 | + } |
| 89 | + |
| 90 | + public int readInt() { |
| 91 | + final int b3 = readByte() << 24; |
| 92 | + final int b2 = (readByte() & 0xff) << 16; |
| 93 | + final int b1 = (readByte() & 0xff) << 8; |
| 94 | + final int b0 = readByte() & 0xff; |
| 95 | + return b3 | b2 | b1 | b0; |
| 96 | + } |
| 97 | + |
| 98 | + public void skipBytes(int n) { |
| 99 | + currentBytePosition += n; |
| 100 | + } |
| 101 | + |
| 102 | + public void alignInstructionPosition() { |
| 103 | + final int remainder = currentBytePosition % 4; |
| 104 | + if (remainder != 0) { |
| 105 | + currentBytePosition += 4 - remainder; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public void jump(int offset) { |
| 110 | + currentBytePosition = currentOpcodePosition + offset; |
| 111 | + } |
| 112 | + |
| 113 | + public int currentOpcodePosition() { |
| 114 | + return currentOpcodePosition; |
| 115 | + } |
| 116 | + |
| 117 | + public int currentBytePosition() { |
| 118 | + return currentBytePosition; |
| 119 | + } |
| 120 | + |
| 121 | + public void setBytecodePosition(int bcp) { |
| 122 | + currentBytePosition = bcp; |
| 123 | + } |
| 124 | + |
| 125 | + public byte[] code() { |
| 126 | + return code; |
| 127 | + } |
| 128 | + |
| 129 | + public Value getLocal(int index) { |
| 130 | + return locals[index]; |
| 131 | + } |
| 132 | + |
| 133 | + public Stack<Value> stack() { |
| 134 | + return operands; |
| 135 | + } |
| 136 | + |
| 137 | + public ConstantPool constantPool() { |
| 138 | + return method.codeAttribute().constantPool; |
| 139 | + } |
| 140 | + |
| 141 | + public ClassMethodActor method() { |
| 142 | + return method; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public String toString() { |
| 147 | + return method.format("%H.%n(%p) @ " + currentBytePosition); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Handles an exception at the current execution point in this frame by updating the {@linkplain #setBytecodePosition(int) |
| 152 | + * instruction pointer} to the matching exception handler in this frame. If no matching exception handler is found |
| 153 | + * for the current execution point and the given exception type, then the instruction pointer in this frame is left |
| 154 | + * unmodified. |
| 155 | + * <p> |
| 156 | + * The current execution point is derived from the value of {@link ExecutionFrame#currentBytePosition()} which is now at the first |
| 157 | + * byte passed the instruction currently being executed. That is, an instruction is completely decoded before any |
| 158 | + * exceptions are thrown while executing it. |
| 159 | + * |
| 160 | + * @param throwableClassActor the type of the exception being thrown |
| 161 | + * @return {@code true} if an exception handler was found, {@code false} otherwise |
| 162 | + */ |
| 163 | + public boolean handleException(ClassActor throwableClassActor) { |
| 164 | + final int bcp = currentOpcodePosition; |
| 165 | + final ExceptionHandlerEntry[] handlers = method().codeAttribute().exceptionHandlerTable(); |
| 166 | + for (ExceptionHandlerEntry handler : handlers) { |
| 167 | + if (bcp >= handler.startPosition() && bcp < handler.endPosition()) { |
| 168 | + if (handler.catchTypeIndex() == 0) { |
| 169 | + currentBytePosition = handler.handlerPosition(); |
| 170 | + return true; |
| 171 | + } |
| 172 | + final ClassActor catchType = constantPool().classAt(handler.catchTypeIndex()).resolve(constantPool(), handler.catchTypeIndex()); |
| 173 | + if (catchType.isAssignableFrom(throwableClassActor)) { |
| 174 | + currentBytePosition = handler.handlerPosition(); |
| 175 | + return true; |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + return false; |
| 180 | + } |
| 181 | +} |
0 commit comments