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 
  24 
  25 package org.graalvm.compiler.bytecode;
  26 
  27 import jdk.vm.ci.meta.ConstantPool;
  28 import jdk.vm.ci.meta.ExceptionHandler;
  29 import jdk.vm.ci.meta.LineNumberTable;
  30 import jdk.vm.ci.meta.LocalVariableTable;
  31 import jdk.vm.ci.meta.ProfilingInfo;
  32 import jdk.vm.ci.meta.ResolvedJavaMethod;
  33 
  34 /**
  35  * An interface for accessing the bytecode properties of a {@link ResolvedJavaMethod} that allows
  36  * for different properties than those returned by {@link ResolvedJavaMethod}. Since the bytecode
  37  * accessed directly from {@link ResolvedJavaMethod} may have been subject to bytecode
  38  * instrumentation and VM rewriting, this indirection can be used to enable access to the original
  39  * bytecode of a method (i.e., as defined in a class file).
  40  */
  41 public interface Bytecode {
  42 
  43     /**
  44      * Gets the method this object supplies bytecode for.
  45      */
  46     ResolvedJavaMethod getMethod();
  47 
  48     byte[] getCode();
  49 
  50     int getCodeSize();
  51 
  52     int getMaxStackSize();
  53 
  54     int getMaxLocals();
  55 
  56     ConstantPool getConstantPool();
  57 
  58     LineNumberTable getLineNumberTable();
  59 
  60     LocalVariableTable getLocalVariableTable();
  61 
  62     StackTraceElement asStackTraceElement(int bci);
  63 
  64     ProfilingInfo getProfilingInfo();
  65 
  66     ExceptionHandler[] getExceptionHandlers();
  67 
  68     /**
  69      * Gets the {@link BytecodeProvider} from which this object was acquired.
  70      */
  71     BytecodeProvider getOrigin();
  72 
  73     static String toLocation(Bytecode bytecode, int bci) {
  74         return appendLocation(new StringBuilder(), bytecode, bci).toString();
  75     }
  76 
  77     static StringBuilder appendLocation(StringBuilder sb, Bytecode bytecode, int bci) {
  78         if (bytecode != null) {
  79             StackTraceElement ste = bytecode.asStackTraceElement(bci);
  80             if (ste.getFileName() != null && ste.getLineNumber() > 0) {
  81                 sb.append(ste);
  82             } else {
  83                 sb.append(bytecode.getMethod().format("%H.%n(%p)"));
  84             }
  85         } else {
  86             sb.append("Null method");
  87         }
  88         return sb.append(" [bci: ").append(bci).append(']');
  89     }
  90 }