1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /*
  26  * This file is available under and governed by the GNU General Public
  27  * License version 2 only, as published by the Free Software Foundation.
  28  * However, the following notice accompanied the original version of this
  29  * file:
  30  *
  31  * ASM: a very small and fast Java bytecode manipulation framework
  32  * Copyright (c) 2000-2011 INRIA, France Telecom
  33  * All rights reserved.
  34  *
  35  * Redistribution and use in source and binary forms, with or without
  36  * modification, are permitted provided that the following conditions
  37  * are met:
  38  * 1. Redistributions of source code must retain the above copyright
  39  *    notice, this list of conditions and the following disclaimer.
  40  * 2. Redistributions in binary form must reproduce the above copyright
  41  *    notice, this list of conditions and the following disclaimer in the
  42  *    documentation and/or other materials provided with the distribution.
  43  * 3. Neither the name of the copyright holders nor the names of its
  44  *    contributors may be used to endorse or promote products derived from
  45  *    this software without specific prior written permission.
  46  *
  47  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  48  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  51  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  52  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  53  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  54  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  55  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  56  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  57  * THE POSSIBILITY OF SUCH DAMAGE.
  58  */
  59 package jdk.internal.org.objectweb.asm.tree.analysis;
  60 
  61 import java.util.List;
  62 
  63 import jdk.internal.org.objectweb.asm.Type;
  64 import jdk.internal.org.objectweb.asm.tree.AbstractInsnNode;
  65 
  66 /**
  67  * A semantic bytecode interpreter. More precisely, this interpreter only
  68  * manages the computation of values from other values: it does not manage the
  69  * transfer of values to or from the stack, and to or from the local variables.
  70  * This separation allows a generic bytecode {@link Analyzer} to work with
  71  * various semantic interpreters, without needing to duplicate the code to
  72  * simulate the transfer of values.
  73  *
  74  * @param <V> type of the Value used for the analysis.
  75  *
  76  * @author Eric Bruneton
  77  */
  78 public abstract class Interpreter<V extends Value> {
  79 
  80     protected final int api;
  81 
  82     protected Interpreter(final int api) {
  83         this.api = api;
  84     }
  85 
  86     /**
  87      * Creates a new value that represents the given type.
  88      *
  89      * Called for method parameters (including <code>this</code>),
  90      * exception handler variable and with <code>null</code> type
  91      * for variables reserved by long and double types.
  92      *
  93      * @param type a primitive or reference type, or <tt>null</tt> to
  94      *        represent an uninitialized value.
  95      * @return a value that represents the given type. The size of the returned
  96      *         value must be equal to the size of the given type.
  97      */
  98     public abstract V newValue(Type type);
  99 
 100     /**
 101      * Interprets a bytecode instruction without arguments. This method is
 102      * called for the following opcodes:
 103      *
 104      * ACONST_NULL, ICONST_M1, ICONST_0, ICONST_1, ICONST_2, ICONST_3, ICONST_4,
 105      * ICONST_5, LCONST_0, LCONST_1, FCONST_0, FCONST_1, FCONST_2, DCONST_0,
 106      * DCONST_1, BIPUSH, SIPUSH, LDC, JSR, GETSTATIC, NEW
 107      *
 108      * @param insn the bytecode instruction to be interpreted.
 109      * @return the result of the interpretation of the given instruction.
 110      * @throws AnalyzerException if an error occured during the interpretation.
 111      */
 112     public abstract V newOperation(AbstractInsnNode insn)
 113             throws AnalyzerException;
 114 
 115     /**
 116      * Interprets a bytecode instruction that moves a value on the stack or to
 117      * or from local variables. This method is called for the following opcodes:
 118      *
 119      * ILOAD, LLOAD, FLOAD, DLOAD, ALOAD, ISTORE, LSTORE, FSTORE, DSTORE,
 120      * ASTORE, DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2, SWAP
 121      *
 122      * @param insn the bytecode instruction to be interpreted.
 123      * @param value the value that must be moved by the instruction.
 124      * @return the result of the interpretation of the given instruction. The
 125      *         returned value must be <tt>equal</tt> to the given value.
 126      * @throws AnalyzerException if an error occured during the interpretation.
 127      */
 128     public abstract V copyOperation(AbstractInsnNode insn, V value)
 129             throws AnalyzerException;
 130 
 131     /**
 132      * Interprets a bytecode instruction with a single argument. This method is
 133      * called for the following opcodes:
 134      *
 135      * INEG, LNEG, FNEG, DNEG, IINC, I2L, I2F, I2D, L2I, L2F, L2D, F2I, F2L,
 136      * F2D, D2I, D2L, D2F, I2B, I2C, I2S, IFEQ, IFNE, IFLT, IFGE, IFGT, IFLE,
 137      * TABLESWITCH, LOOKUPSWITCH, IRETURN, LRETURN, FRETURN, DRETURN, ARETURN,
 138      * PUTSTATIC, GETFIELD, NEWARRAY, ANEWARRAY, ARRAYLENGTH, ATHROW, CHECKCAST,
 139      * INSTANCEOF, MONITORENTER, MONITOREXIT, IFNULL, IFNONNULL
 140      *
 141      * @param insn the bytecode instruction to be interpreted.
 142      * @param value the argument of the instruction to be interpreted.
 143      * @return the result of the interpretation of the given instruction.
 144      * @throws AnalyzerException if an error occured during the interpretation.
 145      */
 146     public abstract V unaryOperation(AbstractInsnNode insn, V value)
 147             throws AnalyzerException;
 148 
 149     /**
 150      * Interprets a bytecode instruction with two arguments. This method is
 151      * called for the following opcodes:
 152      *
 153      * IALOAD, LALOAD, FALOAD, DALOAD, AALOAD, BALOAD, CALOAD, SALOAD, IADD,
 154      * LADD, FADD, DADD, ISUB, LSUB, FSUB, DSUB, IMUL, LMUL, FMUL, DMUL, IDIV,
 155      * LDIV, FDIV, DDIV, IREM, LREM, FREM, DREM, ISHL, LSHL, ISHR, LSHR, IUSHR,
 156      * LUSHR, IAND, LAND, IOR, LOR, IXOR, LXOR, LCMP, FCMPL, FCMPG, DCMPL,
 157      * DCMPG, IF_ICMPEQ, IF_ICMPNE, IF_ICMPLT, IF_ICMPGE, IF_ICMPGT, IF_ICMPLE,
 158      * IF_ACMPEQ, IF_ACMPNE, PUTFIELD
 159      *
 160      * @param insn the bytecode instruction to be interpreted.
 161      * @param value1 the first argument of the instruction to be interpreted.
 162      * @param value2 the second argument of the instruction to be interpreted.
 163      * @return the result of the interpretation of the given instruction.
 164      * @throws AnalyzerException if an error occured during the interpretation.
 165      */
 166     public abstract V binaryOperation(AbstractInsnNode insn, V value1, V value2)
 167             throws AnalyzerException;
 168 
 169     /**
 170      * Interprets a bytecode instruction with three arguments. This method is
 171      * called for the following opcodes:
 172      *
 173      * IASTORE, LASTORE, FASTORE, DASTORE, AASTORE, BASTORE, CASTORE, SASTORE
 174      *
 175      * @param insn the bytecode instruction to be interpreted.
 176      * @param value1 the first argument of the instruction to be interpreted.
 177      * @param value2 the second argument of the instruction to be interpreted.
 178      * @param value3 the third argument of the instruction to be interpreted.
 179      * @return the result of the interpretation of the given instruction.
 180      * @throws AnalyzerException if an error occured during the interpretation.
 181      */
 182     public abstract V ternaryOperation(
 183         AbstractInsnNode insn,
 184         V value1,
 185         V value2,
 186         V value3) throws AnalyzerException;
 187 
 188     /**
 189      * Interprets a bytecode instruction with a variable number of arguments.
 190      * This method is called for the following opcodes:
 191      *
 192      * INVOKEVIRTUAL, INVOKESPECIAL, INVOKESTATIC, INVOKEINTERFACE,
 193      * MULTIANEWARRAY and INVOKEDYNAMIC
 194      *
 195      * @param insn the bytecode instruction to be interpreted.
 196      * @param values the arguments of the instruction to be interpreted.
 197      * @return the result of the interpretation of the given instruction.
 198      * @throws AnalyzerException if an error occured during the interpretation.
 199      */
 200     public abstract V naryOperation(
 201         AbstractInsnNode insn,
 202         List< ? extends V> values) throws AnalyzerException;
 203 
 204     /**
 205      * Interprets a bytecode return instruction. This method is called for the
 206      * following opcodes:
 207      *
 208      * IRETURN, LRETURN, FRETURN, DRETURN, ARETURN
 209      *
 210      * @param insn the bytecode instruction to be interpreted.
 211      * @param value the argument of the instruction to be interpreted.
 212      * @param expected the expected return type of the analyzed method.
 213      * @throws AnalyzerException if an error occured during the interpretation.
 214      */
 215     public abstract void returnOperation(
 216         AbstractInsnNode insn,
 217         V value,
 218         V expected) throws AnalyzerException;
 219 
 220     /**
 221      * Merges two values. The merge operation must return a value that
 222      * represents both values (for instance, if the two values are two types,
 223      * the merged value must be a common super type of the two types. If the two
 224      * values are integer intervals, the merged value must be an interval that
 225      * contains the previous ones. Likewise for other types of values).
 226      *
 227      * @param v a value.
 228      * @param w another value.
 229      * @return the merged value. If the merged value is equal to <tt>v</tt>,
 230      *         this method <i>must</i> return <tt>v</tt>.
 231      */
 232     public abstract V merge(V v, V w);
 233 }