1/*
2 * Copyright (c) 2011, 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 jdk.vm.ci.hotspot;
24
25import jdk.vm.ci.code.BytecodeFrame;
26import jdk.vm.ci.code.CompiledCode;
27import jdk.vm.ci.code.StackSlot;
28import jdk.vm.ci.code.site.DataPatch;
29import jdk.vm.ci.code.site.Infopoint;
30import jdk.vm.ci.code.site.Site;
31import jdk.vm.ci.meta.Assumptions.Assumption;
32import jdk.vm.ci.meta.ResolvedJavaMethod;
33
34/**
35 * A {@link CompiledCode} with additional HotSpot-specific information required for installing the
36 * code in HotSpot's code cache.
37 */
38public class HotSpotCompiledCode implements CompiledCode {
39
40    /**
41     * The name of this compilation unit.
42     */
43    protected final String name;
44
45    /**
46     * The buffer containing the emitted machine code.
47     */
48    protected final byte[] targetCode;
49
50    /**
51     * The leading number of bytes in {@link #targetCode} containing the emitted machine code.
52     */
53    protected final int targetCodeSize;
54
55    /**
56     * A list of code annotations describing special sites in {@link #targetCode}.
57     */
58    protected final Site[] sites;
59
60    /**
61     * A list of {@link Assumption} this code relies on.
62     */
63    protected final Assumption[] assumptions;
64
65    /**
66     * The list of the methods whose bytecodes were used as input to the compilation. If
67     * {@code null}, then the compilation did not record method dependencies. Otherwise, the first
68     * element of this array is the root method of the compilation.
69     */
70    protected final ResolvedJavaMethod[] methods;
71
72    /**
73     * A list of comments that will be included in code dumps.
74     */
75    protected final Comment[] comments;
76
77    /**
78     * The data section containing serialized constants for the emitted machine code.
79     */
80    protected final byte[] dataSection;
81
82    /**
83     * The minimum alignment of the data section.
84     */
85    protected final int dataSectionAlignment;
86
87    /**
88     * A list of relocations in the {@link #dataSection}.
89     */
90    protected final DataPatch[] dataSectionPatches;
91
92    /**
93     * A flag determining whether this code is immutable and position independent.
94     */
95    protected final boolean isImmutablePIC;
96
97    /**
98     * The total size of the stack frame of this compiled method.
99     */
100    protected final int totalFrameSize;
101
102    /**
103     * The deopt rescue slot. Must be non-null if there is a safepoint in the method.
104     */
105    protected final StackSlot deoptRescueSlot;
106
107    public static class Comment {
108
109        public final String text;
110        public final int pcOffset;
111
112        public Comment(int pcOffset, String text) {
113            this.text = text;
114            this.pcOffset = pcOffset;
115        }
116    }
117
118    @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "caller transfers ownership of `sites`, `targetCode`, `comments`, `methods`, `dataSection`, `dataSectionPatches` and `assumptions`")
119    public HotSpotCompiledCode(String name, byte[] targetCode, int targetCodeSize, Site[] sites, Assumption[] assumptions, ResolvedJavaMethod[] methods, Comment[] comments, byte[] dataSection,
120                    int dataSectionAlignment, DataPatch[] dataSectionPatches, boolean isImmutablePIC, int totalFrameSize, StackSlot deoptRescueSlot) {
121        this.name = name;
122        this.targetCode = targetCode;
123        this.targetCodeSize = targetCodeSize;
124        this.sites = sites;
125        this.assumptions = assumptions;
126        this.methods = methods;
127
128        this.comments = comments;
129        this.dataSection = dataSection;
130        this.dataSectionAlignment = dataSectionAlignment;
131        this.dataSectionPatches = dataSectionPatches;
132        this.isImmutablePIC = isImmutablePIC;
133        this.totalFrameSize = totalFrameSize;
134        this.deoptRescueSlot = deoptRescueSlot;
135
136        assert validateFrames();
137    }
138
139    public String getName() {
140        return name;
141    }
142
143    @Override
144    public String toString() {
145        return name;
146    }
147
148    /**
149     * Ensure that all the frames passed into the VM are properly formatted with an empty or illegal
150     * slot following double word slots.
151     */
152    private boolean validateFrames() {
153        for (Site site : sites) {
154            if (site instanceof Infopoint) {
155                Infopoint info = (Infopoint) site;
156                if (info.debugInfo != null) {
157                    BytecodeFrame frame = info.debugInfo.frame();
158                    assert frame == null || frame.validateFormat();
159                }
160            }
161        }
162        return true;
163    }
164}
165