001: /***
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (c) 2000-2005 INRIA, France Telecom
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */package com.tc.asm.tree.analysis;
030:
031: import java.util.List;
032:
033: import com.tc.asm.Type;
034: import com.tc.asm.tree.AbstractInsnNode;
035:
036: /**
037: * A semantic bytecode interpreter. More precisely, this interpreter only
038: * manages the computation of values from other values: it does not manage the
039: * transfer of values to or from the stack, and to or from the local variables.
040: * This separation allows a generic bytecode {@link Analyzer} to work with
041: * various semantic interpreters, without needing to duplicate the code to
042: * simulate the transfer of values.
043: *
044: * @author Eric Bruneton
045: */
046: public interface Interpreter {
047:
048: /**
049: * Creates a new value that represents the given type.
050: *
051: * Called for method parameters (including <code>this</code>),
052: * exception handler variable and with <code>null</code> type
053: * for variables reserved by long and double types.
054: *
055: * @param type a primitive or reference type, or <tt>null</tt> to
056: * represent an uninitialized value.
057: * @return a value that represents the given type. The size of the returned
058: * value must be equal to the size of the given type.
059: */
060: Value newValue(Type type);
061:
062: /**
063: * Interprets a bytecode instruction without arguments. This method is
064: * called for the following opcodes:
065: *
066: * ACONST_NULL, ICONST_M1, ICONST_0, ICONST_1, ICONST_2, ICONST_3, ICONST_4,
067: * ICONST_5, LCONST_0, LCONST_1, FCONST_0, FCONST_1, FCONST_2, DCONST_0,
068: * DCONST_1, BIPUSH, SIPUSH, LDC, JSR, GETSTATIC, NEW
069: *
070: * @param insn the bytecode instruction to be interpreted.
071: * @return the result of the interpretation of the given instruction.
072: * @throws AnalyzerException if an error occured during the interpretation.
073: */
074: Value newOperation(AbstractInsnNode insn) throws AnalyzerException;
075:
076: /**
077: * Interprets a bytecode instruction that moves a value on the stack or to
078: * or from local variables. This method is called for the following opcodes:
079: *
080: * ILOAD, LLOAD, FLOAD, DLOAD, ALOAD, ISTORE, LSTORE, FSTORE, DSTORE,
081: * ASTORE, DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2, SWAP
082: *
083: * @param insn the bytecode instruction to be interpreted.
084: * @param value the value that must be moved by the instruction.
085: * @return the result of the interpretation of the given instruction. The
086: * returned value must be <tt>equal</tt> to the given value.
087: * @throws AnalyzerException if an error occured during the interpretation.
088: */
089: Value copyOperation(AbstractInsnNode insn, Value value)
090: throws AnalyzerException;
091:
092: /**
093: * Interprets a bytecode instruction with a single argument. This method is
094: * called for the following opcodes:
095: *
096: * INEG, LNEG, FNEG, DNEG, IINC, I2L, I2F, I2D, L2I, L2F, L2D, F2I, F2L,
097: * F2D, D2I, D2L, D2F, I2B, I2C, I2S, IFEQ, IFNE, IFLT, IFGE, IFGT, IFLE,
098: * TABLESWITCH, LOOKUPSWITCH, IRETURN, LRETURN, FRETURN, DRETURN, ARETURN,
099: * PUTSTATIC, GETFIELD, NEWARRAY, ANEWARRAY, ARRAYLENGTH, ATHROW, CHECKCAST,
100: * INSTANCEOF, MONITORENTER, MONITOREXIT, IFNULL, IFNONNULL
101: *
102: * @param insn the bytecode instruction to be interpreted.
103: * @param value the argument of the instruction to be interpreted.
104: * @return the result of the interpretation of the given instruction.
105: * @throws AnalyzerException if an error occured during the interpretation.
106: */
107: Value unaryOperation(AbstractInsnNode insn, Value value)
108: throws AnalyzerException;
109:
110: /**
111: * Interprets a bytecode instruction with two arguments. This method is
112: * called for the following opcodes:
113: *
114: * IALOAD, LALOAD, FALOAD, DALOAD, AALOAD, BALOAD, CALOAD, SALOAD, IADD,
115: * LADD, FADD, DADD, ISUB, LSUB, FSUB, DSUB, IMUL, LMUL, FMUL, DMUL, IDIV,
116: * LDIV, FDIV, DDIV, IREM, LREM, FREM, DREM, ISHL, LSHL, ISHR, LSHR, IUSHR,
117: * LUSHR, IAND, LAND, IOR, LOR, IXOR, LXOR, LCMP, FCMPL, FCMPG, DCMPL,
118: * DCMPG, IF_ICMPEQ, IF_ICMPNE, IF_ICMPLT, IF_ICMPGE, IF_ICMPGT, IF_ICMPLE,
119: * IF_ACMPEQ, IF_ACMPNE, PUTFIELD
120: *
121: * @param insn the bytecode instruction to be interpreted.
122: * @param value1 the first argument of the instruction to be interpreted.
123: * @param value2 the second argument of the instruction to be interpreted.
124: * @return the result of the interpretation of the given instruction.
125: * @throws AnalyzerException if an error occured during the interpretation.
126: */
127: Value binaryOperation(AbstractInsnNode insn, Value value1,
128: Value value2) throws AnalyzerException;
129:
130: /**
131: * Interprets a bytecode instruction with three arguments. This method is
132: * called for the following opcodes:
133: *
134: * IASTORE, LASTORE, FASTORE, DASTORE, AASTORE, BASTORE, CASTORE, SASTORE
135: *
136: * @param insn the bytecode instruction to be interpreted.
137: * @param value1 the first argument of the instruction to be interpreted.
138: * @param value2 the second argument of the instruction to be interpreted.
139: * @param value3 the third argument of the instruction to be interpreted.
140: * @return the result of the interpretation of the given instruction.
141: * @throws AnalyzerException if an error occured during the interpretation.
142: */
143: Value ternaryOperation(AbstractInsnNode insn, Value value1,
144: Value value2, Value value3) throws AnalyzerException;
145:
146: /**
147: * Interprets a bytecode instruction with a variable number of arguments.
148: * This method is called for the following opcodes:
149: *
150: * INVOKEVIRTUAL, INVOKESPECIAL, INVOKESTATIC, INVOKEINTERFACE,
151: * MULTIANEWARRAY
152: *
153: * @param insn the bytecode instruction to be interpreted.
154: * @param values the arguments of the instruction to be interpreted.
155: * @return the result of the interpretation of the given instruction.
156: * @throws AnalyzerException if an error occured during the interpretation.
157: */
158: Value naryOperation(AbstractInsnNode insn, List values)
159: throws AnalyzerException;
160:
161: /**
162: * Merges two values. The merge operation must return a value that
163: * represents both values (for instance, if the two values are two types,
164: * the merged value must be a common super type of the two types. If the two
165: * values are integer intervals, the merged value must be an interval that
166: * contains the previous ones. Likewise for other types of values).
167: *
168: * @param v a value.
169: * @param w another value.
170: * @return the merged value. If the merged value is equal to <tt>v</tt>,
171: * this method <i>must</i> return <tt>v</tt>.
172: */
173: Value merge(Value v, Value w);
174: }
|