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;
  60 
  61 import java.util.ArrayList;
  62 import java.util.Arrays;
  63 import java.util.List;
  64 import java.util.Map;
  65 
  66 import jdk.internal.org.objectweb.asm.MethodVisitor;
  67 import jdk.internal.org.objectweb.asm.Opcodes;
  68 
  69 /**
  70  * A node that represents a stack map frame. These nodes are pseudo instruction
  71  * nodes in order to be inserted in an instruction list. In fact these nodes
  72  * must(*) be inserted <i>just before</i> any instruction node <b>i</b> that
  73  * follows an unconditionnal branch instruction such as GOTO or THROW, that is
  74  * the target of a jump instruction, or that starts an exception handler block.
  75  * The stack map frame types must describe the values of the local variables and
  76  * of the operand stack elements <i>just before</i> <b>i</b> is executed. <br>
  77  * <br> (*) this is mandatory only for classes whose version is greater than or
  78  * equal to {@link Opcodes#V1_6 V1_6}.
  79  *
  80  * @author Eric Bruneton
  81  */
  82 public class FrameNode extends AbstractInsnNode {
  83 
  84     /**
  85      * The type of this frame. Must be {@link Opcodes#F_NEW} for expanded
  86      * frames, or {@link Opcodes#F_FULL}, {@link Opcodes#F_APPEND},
  87      * {@link Opcodes#F_CHOP}, {@link Opcodes#F_SAME} or
  88      * {@link Opcodes#F_APPEND}, {@link Opcodes#F_SAME1} for compressed frames.
  89      */
  90     public int type;
  91 
  92     /**
  93      * The types of the local variables of this stack map frame. Elements of
  94      * this list can be Integer, String or LabelNode objects (for primitive,
  95      * reference and uninitialized types respectively - see
  96      * {@link MethodVisitor}).
  97      */
  98     public List<Object> local;
  99 
 100     /**
 101      * The types of the operand stack elements of this stack map frame. Elements
 102      * of this list can be Integer, String or LabelNode objects (for primitive,
 103      * reference and uninitialized types respectively - see
 104      * {@link MethodVisitor}).
 105      */
 106     public List<Object> stack;
 107 
 108     private FrameNode() {
 109         super(-1);
 110     }
 111 
 112     /**
 113      * Constructs a new {@link FrameNode}.
 114      *
 115      * @param type the type of this frame. Must be {@link Opcodes#F_NEW} for
 116      *        expanded frames, or {@link Opcodes#F_FULL},
 117      *        {@link Opcodes#F_APPEND}, {@link Opcodes#F_CHOP},
 118      *        {@link Opcodes#F_SAME} or {@link Opcodes#F_APPEND},
 119      *        {@link Opcodes#F_SAME1} for compressed frames.
 120      * @param nLocal number of local variables of this stack map frame.
 121      * @param local the types of the local variables of this stack map frame.
 122      *        Elements of this list can be Integer, String or LabelNode objects
 123      *        (for primitive, reference and uninitialized types respectively -
 124      *        see {@link MethodVisitor}).
 125      * @param nStack number of operand stack elements of this stack map frame.
 126      * @param stack the types of the operand stack elements of this stack map
 127      *        frame. Elements of this list can be Integer, String or LabelNode
 128      *        objects (for primitive, reference and uninitialized types
 129      *        respectively - see {@link MethodVisitor}).
 130      */
 131     public FrameNode(
 132         final int type,
 133         final int nLocal,
 134         final Object[] local,
 135         final int nStack,
 136         final Object[] stack)
 137     {
 138         super(-1);
 139         this.type = type;
 140         switch (type) {
 141             case Opcodes.F_NEW:
 142             case Opcodes.F_FULL:
 143                 this.local = asList(nLocal, local);
 144                 this.stack = asList(nStack, stack);
 145                 break;
 146             case Opcodes.F_APPEND:
 147                 this.local = asList(nLocal, local);
 148                 break;
 149             case Opcodes.F_CHOP:
 150                 this.local = Arrays.asList(new Object[nLocal]);
 151                 break;
 152             case Opcodes.F_SAME:
 153                 break;
 154             case Opcodes.F_SAME1:
 155                 this.stack = asList(1, stack);
 156                 break;
 157         }
 158     }
 159 
 160     @Override
 161     public int getType() {
 162         return FRAME;
 163     }
 164 
 165     /**
 166      * Makes the given visitor visit this stack map frame.
 167      *
 168      * @param mv a method visitor.
 169      */
 170     @Override
 171     public void accept(final MethodVisitor mv) {
 172         switch (type) {
 173             case Opcodes.F_NEW:
 174             case Opcodes.F_FULL:
 175                 mv.visitFrame(type,
 176                         local.size(),
 177                         asArray(local),
 178                         stack.size(),
 179                         asArray(stack));
 180                 break;
 181             case Opcodes.F_APPEND:
 182                 mv.visitFrame(type, local.size(), asArray(local), 0, null);
 183                 break;
 184             case Opcodes.F_CHOP:
 185                 mv.visitFrame(type, local.size(), null, 0, null);
 186                 break;
 187             case Opcodes.F_SAME:
 188                 mv.visitFrame(type, 0, null, 0, null);
 189                 break;
 190             case Opcodes.F_SAME1:
 191                 mv.visitFrame(type, 0, null, 1, asArray(stack));
 192                 break;
 193         }
 194     }
 195 
 196     @Override
 197     public AbstractInsnNode clone(final Map<LabelNode, LabelNode> labels) {
 198         FrameNode clone = new FrameNode();
 199         clone.type = type;
 200         if (local != null) {
 201             clone.local = new ArrayList<Object>();
 202             for (int i = 0; i < local.size(); ++i) {
 203                 Object l = local.get(i);
 204                 if (l instanceof LabelNode) {
 205                     l = labels.get(l);
 206                 }
 207                 clone.local.add(l);
 208             }
 209         }
 210         if (stack != null) {
 211             clone.stack = new ArrayList<Object>();
 212             for (int i = 0; i < stack.size(); ++i) {
 213                 Object s = stack.get(i);
 214                 if (s instanceof LabelNode) {
 215                     s = labels.get(s);
 216                 }
 217                 clone.stack.add(s);
 218             }
 219         }
 220         return clone;
 221     }
 222 
 223     // ------------------------------------------------------------------------
 224 
 225     private static List<Object> asList(final int n, final Object[] o) {
 226         return Arrays.asList(o).subList(0, n);
 227     }
 228 
 229     private static Object[] asArray(final List<Object> l) {
 230         Object[] objs = new Object[l.size()];
 231         for (int i = 0; i < objs.length; ++i) {
 232             Object o = l.get(i);
 233             if (o instanceof LabelNode) {
 234                 o = ((LabelNode) o).getLabel();
 235             }
 236             objs[i] = o;
 237         }
 238         return objs;
 239     }
 240 }