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 */
23package org.graalvm.compiler.bytecode;
24
25import jdk.vm.ci.meta.ConstantPool;
26import jdk.vm.ci.meta.ExceptionHandler;
27import jdk.vm.ci.meta.LineNumberTable;
28import jdk.vm.ci.meta.LocalVariableTable;
29import jdk.vm.ci.meta.ProfilingInfo;
30import 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 */
39public 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    /**
67     * Gets the {@link BytecodeProvider} from which this object was acquired.
68     */
69    BytecodeProvider getOrigin();
70
71    static String toLocation(Bytecode bytecode, int bci) {
72        return appendLocation(new StringBuilder(), bytecode, bci).toString();
73    }
74
75    static StringBuilder appendLocation(StringBuilder sb, Bytecode bytecode, int bci) {
76        if (bytecode != null) {
77            StackTraceElement ste = bytecode.asStackTraceElement(bci);
78            if (ste.getFileName() != null && ste.getLineNumber() > 0) {
79                sb.append(ste);
80            } else {
81                sb.append(bytecode.getMethod().format("%H.%n(%p)"));
82            }
83        } else {
84            sb.append("Null method");
85        }
86        return sb.append(" [bci: ").append(bci).append(']');
87    }
88}
89