1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.bytecode;
  24 
  25 import jdk.vm.ci.meta.ConstantPool;
  26 import jdk.vm.ci.meta.ExceptionHandler;
  27 import jdk.vm.ci.meta.LineNumberTable;
  28 import jdk.vm.ci.meta.LocalVariableTable;
  29 import jdk.vm.ci.meta.ProfilingInfo;
  30 import jdk.vm.ci.meta.ResolvedJavaMethod;
  31 
  32 /**
  33  * An interface for accessing the bytecode properties of a {@link ResolvedJavaMethod} that allows
  34  * for different properties than those returned by {@link ResolvedJavaMethod}. Since the bytecode
  35  * accessed directly from {@link ResolvedJavaMethod} may have been subject to bytecode
  36  * instrumentation and VM rewriting, this indirection can be used to enable access to the original
  37  * bytecode of a method (i.e., as defined in a class file).
  38  */
  39 public interface Bytecode {
  40 
  41     /**
  42      * Gets the method this object supplies bytecode for.
  43      */
  44     ResolvedJavaMethod getMethod();
  45 
  46     byte[] getCode();
  47 
  48     int getCodeSize();
  49 
  50     int getMaxStackSize();
  51 
  52     int getMaxLocals();
  53 
  54     ConstantPool getConstantPool();
  55 
  56     LineNumberTable getLineNumberTable();
  57 
  58     LocalVariableTable getLocalVariableTable();
  59 
  60     StackTraceElement asStackTraceElement(int bci);
  61 
  62     ProfilingInfo getProfilingInfo();
  63 
  64     ExceptionHandler[] getExceptionHandlers();
  65 
  66     static String toLocation(Bytecode bytecode, int bci) {
  67         return appendLocation(new StringBuilder(), bytecode, bci).toString();
  68     }
  69 
  70     static StringBuilder appendLocation(StringBuilder sb, Bytecode bytecode, int bci) {
  71         if (bytecode != null) {
  72             StackTraceElement ste = bytecode.asStackTraceElement(bci);
  73             if (ste.getFileName() != null && ste.getLineNumber() > 0) {
  74                 sb.append(ste);
  75             } else {
  76                 sb.append(bytecode.getMethod().format("%H.%n(%p)"));
  77             }
  78         } else {
  79             sb.append("Null method");
  80         }
  81         return sb.append(" [bci: ").append(bci).append(']');
  82     }
  83 }