comparison jvmci/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotCompiledCode.java @ 22672:1bbd4a7c274b

Rename jdk.internal.jvmci to jdk.vm.ci
author Tom Rodriguez <tom.rodriguez@oracle.com>
date Thu, 08 Oct 2015 17:28:41 -0700
parents jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotCompiledCode.java@545590b1ab83
children eb6d572dfa61
comparison
equal deleted inserted replaced
22671:97f30e4d0e95 22672:1bbd4a7c274b
1 /*
2 * Copyright (c) 2011, 2015, 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 jdk.vm.ci.hotspot;
24
25 import java.nio.ByteBuffer;
26 import java.nio.ByteOrder;
27 import java.util.Arrays;
28 import java.util.Comparator;
29 import java.util.List;
30 import java.util.stream.Stream;
31 import java.util.stream.Stream.Builder;
32
33 import jdk.vm.ci.code.BytecodeFrame;
34 import jdk.vm.ci.code.CompilationResult;
35 import jdk.vm.ci.code.CompilationResult.CodeAnnotation;
36 import jdk.vm.ci.code.CompilationResult.CodeComment;
37 import jdk.vm.ci.code.CompilationResult.DataPatch;
38 import jdk.vm.ci.code.CompilationResult.ExceptionHandler;
39 import jdk.vm.ci.code.CompilationResult.Infopoint;
40 import jdk.vm.ci.code.CompilationResult.JumpTable;
41 import jdk.vm.ci.code.CompilationResult.Mark;
42 import jdk.vm.ci.code.CompilationResult.Site;
43 import jdk.vm.ci.code.DataSection;
44 import jdk.vm.ci.meta.Assumptions.Assumption;
45 import jdk.vm.ci.meta.ResolvedJavaMethod;
46
47 /**
48 * A {@link CompilationResult} with additional HotSpot-specific information required for installing
49 * the code in HotSpot's code cache.
50 */
51 public class HotSpotCompiledCode {
52
53 public final String name;
54 public final Site[] sites;
55 public final ExceptionHandler[] exceptionHandlers;
56 public final Comment[] comments;
57 public final Assumption[] assumptions;
58
59 public final byte[] targetCode;
60 public final int targetCodeSize;
61
62 public final byte[] dataSection;
63 public final int dataSectionAlignment;
64 public final DataPatch[] dataSectionPatches;
65
66 public final int totalFrameSize;
67 public final int customStackAreaOffset;
68
69 /**
70 * The list of the methods whose bytecodes were used as input to the compilation. If
71 * {@code null}, then the compilation did not record method dependencies. Otherwise, the first
72 * element of this array is the root method of the compilation.
73 */
74 public final ResolvedJavaMethod[] methods;
75
76 public static class Comment {
77
78 public final String text;
79 public final int pcOffset;
80
81 public Comment(int pcOffset, String text) {
82 this.text = text;
83 this.pcOffset = pcOffset;
84 }
85 }
86
87 public HotSpotCompiledCode(CompilationResult compResult) {
88 name = compResult.getName();
89 sites = getSortedSites(compResult);
90 if (compResult.getExceptionHandlers().isEmpty()) {
91 exceptionHandlers = null;
92 } else {
93 exceptionHandlers = compResult.getExceptionHandlers().toArray(new ExceptionHandler[compResult.getExceptionHandlers().size()]);
94 }
95 List<CodeAnnotation> annotations = compResult.getAnnotations();
96 comments = new Comment[annotations.size()];
97 if (!annotations.isEmpty()) {
98 for (int i = 0; i < comments.length; i++) {
99 CodeAnnotation annotation = annotations.get(i);
100 String text;
101 if (annotation instanceof CodeComment) {
102 CodeComment codeComment = (CodeComment) annotation;
103 text = codeComment.value;
104 } else if (annotation instanceof JumpTable) {
105 JumpTable jumpTable = (JumpTable) annotation;
106 text = "JumpTable [" + jumpTable.low + " .. " + jumpTable.high + "]";
107 } else {
108 text = annotation.toString();
109 }
110 comments[i] = new Comment(annotation.position, text);
111 }
112 }
113 assumptions = compResult.getAssumptions();
114 assert validateFrames();
115
116 targetCode = compResult.getTargetCode();
117 targetCodeSize = compResult.getTargetCodeSize();
118
119 DataSection data = compResult.getDataSection();
120 data.finalizeLayout();
121 dataSection = new byte[data.getSectionSize()];
122
123 ByteBuffer buffer = ByteBuffer.wrap(dataSection).order(ByteOrder.nativeOrder());
124 Builder<DataPatch> patchBuilder = Stream.builder();
125 data.buildDataSection(buffer, patchBuilder);
126
127 dataSectionAlignment = data.getSectionAlignment();
128 dataSectionPatches = patchBuilder.build().toArray(len -> new DataPatch[len]);
129
130 totalFrameSize = compResult.getTotalFrameSize();
131 customStackAreaOffset = compResult.getCustomStackAreaOffset();
132
133 methods = compResult.getMethods();
134 }
135
136 /**
137 * Ensure that all the frames passed into HotSpot are properly formatted with an empty or
138 * illegal slot following double word slots.
139 */
140 private boolean validateFrames() {
141 for (Site site : sites) {
142 if (site instanceof Infopoint) {
143 Infopoint info = (Infopoint) site;
144 if (info.debugInfo != null) {
145 BytecodeFrame frame = info.debugInfo.frame();
146 assert frame == null || frame.validateFormat();
147 }
148 }
149 }
150 return true;
151 }
152
153 static class SiteComparator implements Comparator<Site> {
154
155 public int compare(Site s1, Site s2) {
156 if (s1.pcOffset == s2.pcOffset && (s1 instanceof Mark ^ s2 instanceof Mark)) {
157 return s1 instanceof Mark ? -1 : 1;
158 }
159 return s1.pcOffset - s2.pcOffset;
160 }
161 }
162
163 private static Site[] getSortedSites(CompilationResult target) {
164 List<?>[] lists = new List<?>[]{target.getInfopoints(), target.getDataPatches(), target.getMarks()};
165 int count = 0;
166 for (List<?> list : lists) {
167 count += list.size();
168 }
169 Site[] result = new Site[count];
170 int pos = 0;
171 for (List<?> list : lists) {
172 for (Object elem : list) {
173 result[pos++] = (Site) elem;
174 }
175 }
176 Arrays.sort(result, new SiteComparator());
177 return result;
178 }
179
180 @Override
181 public String toString() {
182 return name;
183 }
184 }