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>
  75  *            type of the Value used for the analysis.
  76  *
  77  * @author Eric Bruneton
  78  */
  79 public abstract class Interpreter<V extends Value> {
  80 
  81     protected final int api;
  82 
  83     protected Interpreter(final int api) {
  84         this.api = api;
  85     }
  86 
  87     /**
  88      * Creates a new value that represents the given type.
  89      *
  90      * Called for method parameters (including <code>this</code>), exception
  91      * handler variable and with <code>null</code> type for variables reserved
  92      * by long and double types.
  93      *
  94      * @param type
  95      *            a primitive or reference type, or <tt>null</tt> to represent
  96      *            an uninitialized value.
  97      * @return a value that represents the given type. The size of the returned
  98      *         value must be equal to the size of the given type.
  99      */
 100     public abstract V newValue(Type type);
 101 
 102     /**
 103      * Interprets a bytecode instruction without arguments. This method is
 104      * called for the following opcodes:
 105      *
 106      * ACONST_NULL, ICONST_M1, ICONST_0, ICONST_1, ICONST_2, ICONST_3, ICONST_4,
 107      * ICONST_5, LCONST_0, LCONST_1, FCONST_0, FCONST_1, FCONST_2, DCONST_0,
 108      * DCONST_1, BIPUSH, SIPUSH, LDC, JSR, GETSTATIC, NEW
 109      *
 110      * @param insn
 111      *            the bytecode instruction to be interpreted.
 112      * @return the result of the interpretation of the given instruction.
 113      * @throws AnalyzerException
 114      *             if an error occured during the interpretation.
 115      */
 116     public abstract V newOperation(AbstractInsnNode insn)
 117             throws AnalyzerException;
 118 
 119     /**
 120      * Interprets a bytecode instruction that moves a value on the stack or to
 121      * or from local variables. This method is called for the following opcodes:
 122      *
 123      * ILOAD, LLOAD, FLOAD, DLOAD, ALOAD, ISTORE, LSTORE, FSTORE, DSTORE,
 124      * ASTORE, DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2, SWAP
 125      *
 126      * @param insn
 127      *            the bytecode instruction to be interpreted.
 128      * @param value
 129      *            the value that must be moved by the instruction.
 130      * @return the result of the interpretation of the given instruction. The
 131      *         returned value must be <tt>equal</tt> to the given value.
 132      * @throws AnalyzerException
 133      *             if an error occured during the interpretation.
 134      */
 135     public abstract V copyOperation(AbstractInsnNode insn, V value)
 136             throws AnalyzerException;
 137 
 138     /**
 139      * Interprets a bytecode instruction with a single argument. This method is
 140      * called for the following opcodes:
 141      *
 142      * INEG, LNEG, FNEG, DNEG, IINC, I2L, I2F, I2D, L2I, L2F, L2D, F2I, F2L,
 143      * F2D, D2I, D2L, D2F, I2B, I2C, I2S, IFEQ, IFNE, IFLT, IFGE, IFGT, IFLE,
 144      * TABLESWITCH, LOOKUPSWITCH, IRETURN, LRETURN, FRETURN, DRETURN, ARETURN,
 145      * PUTSTATIC, GETFIELD, NEWARRAY, ANEWARRAY, ARRAYLENGTH, ATHROW, CHECKCAST,
 146      * INSTANCEOF, MONITORENTER, MONITOREXIT, IFNULL, IFNONNULL
 147      *
 148      * @param insn
 149      *            the bytecode instruction to be interpreted.
 150      * @param value
 151      *            the argument of the instruction to be interpreted.
 152      * @return the result of the interpretation of the given instruction.
 153      * @throws AnalyzerException
 154      *             if an error occured during the interpretation.
 155      */
 156     public abstract V unaryOperation(AbstractInsnNode insn, V value)
 157             throws AnalyzerException;
 158 
 159     /**
 160      * Interprets a bytecode instruction with two arguments. This method is
 161      * called for the following opcodes:
 162      *
 163      * IALOAD, LALOAD, FALOAD, DALOAD, AALOAD, BALOAD, CALOAD, SALOAD, IADD,
 164      * LADD, FADD, DADD, ISUB, LSUB, FSUB, DSUB, IMUL, LMUL, FMUL, DMUL, IDIV,
 165      * LDIV, FDIV, DDIV, IREM, LREM, FREM, DREM, ISHL, LSHL, ISHR, LSHR, IUSHR,
 166      * LUSHR, IAND, LAND, IOR, LOR, IXOR, LXOR, LCMP, FCMPL, FCMPG, DCMPL,
 167      * DCMPG, IF_ICMPEQ, IF_ICMPNE, IF_ICMPLT, IF_ICMPGE, IF_ICMPGT, IF_ICMPLE,
 168      * IF_ACMPEQ, IF_ACMPNE, PUTFIELD
 169      *
 170      * @param insn
 171      *            the bytecode instruction to be interpreted.
 172      * @param value1
 173      *            the first argument of the instruction to be interpreted.
 174      * @param value2
 175      *            the second argument of the instruction to be interpreted.
 176      * @return the result of the interpretation of the given instruction.
 177      * @throws AnalyzerException
 178      *             if an error occured during the interpretation.
 179      */
 180     public abstract V binaryOperation(AbstractInsnNode insn, V value1, V value2)
 181             throws AnalyzerException;
 182 
 183     /**
 184      * Interprets a bytecode instruction with three arguments. This method is
 185      * called for the following opcodes:
 186      *
 187      * IASTORE, LASTORE, FASTORE, DASTORE, AASTORE, BASTORE, CASTORE, SASTORE
 188      *
 189      * @param insn
 190      *            the bytecode instruction to be interpreted.
 191      * @param value1
 192      *            the first argument of the instruction to be interpreted.
 193      * @param value2
 194      *            the second argument of the instruction to be interpreted.
 195      * @param value3
 196      *            the third argument of the instruction to be interpreted.
 197      * @return the result of the interpretation of the given instruction.
 198      * @throws AnalyzerException
 199      *             if an error occured during the interpretation.
 200      */
 201     public abstract V ternaryOperation(AbstractInsnNode insn, V value1,
 202             V value2, V value3) throws AnalyzerException;
 203 
 204     /**
 205      * Interprets a bytecode instruction with a variable number of arguments.
 206      * This method is called for the following opcodes:
 207      *
 208      * INVOKEVIRTUAL, INVOKESPECIAL, INVOKESTATIC, INVOKEINTERFACE,
 209      * MULTIANEWARRAY and INVOKEDYNAMIC
 210      *
 211      * @param insn
 212      *            the bytecode instruction to be interpreted.
 213      * @param values
 214      *            the arguments of the instruction to be interpreted.
 215      * @return the result of the interpretation of the given instruction.
 216      * @throws AnalyzerException
 217      *             if an error occured during the interpretation.
 218      */
 219     public abstract V naryOperation(AbstractInsnNode insn,
 220             List<? extends V> values) throws AnalyzerException;
 221 
 222     /**
 223      * Interprets a bytecode return instruction. This method is called for the
 224      * following opcodes:
 225      *
 226      * IRETURN, LRETURN, FRETURN, DRETURN, ARETURN
 227      *
 228      * @param insn
 229      *            the bytecode instruction to be interpreted.
 230      * @param value
 231      *            the argument of the instruction to be interpreted.
 232      * @param expected
 233      *            the expected return type of the analyzed method.
 234      * @throws AnalyzerException
 235      *             if an error occured during the interpretation.
 236      */
 237     public abstract void returnOperation(AbstractInsnNode insn, V value,
 238             V expected) throws AnalyzerException;
 239 
 240     /**
 241      * Merges two values. The merge operation must return a value that
 242      * represents both values (for instance, if the two values are two types,
 243      * the merged value must be a common super type of the two types. If the two
 244      * values are integer intervals, the merged value must be an interval that
 245      * contains the previous ones. Likewise for other types of values).
 246      *
 247      * @param v
 248      *            a value.
 249      * @param w
 250      *            another value.
 251      * @return the merged value. If the merged value is equal to <tt>v</tt>,
 252      *         this method <i>must</i> return <tt>v</tt>.
 253      */
 254     public abstract V merge(V v, V w);
 255 }