Mercurial > hg > truffle
comparison graal/com.oracle.jvmci.hotspot/src/com/oracle/jvmci/hotspot/HotSpotCompiledCode.java @ 21551:5324104ac4f3
moved com.oracle.graal.hotspot.jvmci classes to com.oracle.jvmci.hotspot module (JBS:GRAAL-53)
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Tue, 26 May 2015 17:13:37 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
21550:f48a6cea31eb | 21551:5324104ac4f3 |
---|---|
1 /* | |
2 * Copyright (c) 2011, 2014, 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 com.oracle.jvmci.hotspot; | |
24 | |
25 import java.nio.*; | |
26 import java.util.*; | |
27 import java.util.stream.*; | |
28 import java.util.stream.Stream.Builder; | |
29 | |
30 import com.oracle.graal.api.code.*; | |
31 import com.oracle.graal.api.code.CompilationResult.CodeAnnotation; | |
32 import com.oracle.graal.api.code.CompilationResult.CodeComment; | |
33 import com.oracle.graal.api.code.CompilationResult.DataPatch; | |
34 import com.oracle.graal.api.code.CompilationResult.ExceptionHandler; | |
35 import com.oracle.graal.api.code.CompilationResult.Infopoint; | |
36 import com.oracle.graal.api.code.CompilationResult.JumpTable; | |
37 import com.oracle.graal.api.code.CompilationResult.Mark; | |
38 import com.oracle.graal.api.code.CompilationResult.Site; | |
39 | |
40 /** | |
41 * A {@link CompilationResult} with additional HotSpot-specific information required for installing | |
42 * the code in HotSpot's code cache. | |
43 */ | |
44 public abstract class HotSpotCompiledCode { | |
45 | |
46 public final CompilationResult comp; | |
47 | |
48 public final Site[] sites; | |
49 public final ExceptionHandler[] exceptionHandlers; | |
50 public final Comment[] comments; | |
51 | |
52 public final byte[] dataSection; | |
53 public final int dataSectionAlignment; | |
54 public final DataPatch[] dataSectionPatches; | |
55 | |
56 public static class Comment { | |
57 | |
58 public final String text; | |
59 public final int pcOffset; | |
60 | |
61 public Comment(int pcOffset, String text) { | |
62 this.text = text; | |
63 this.pcOffset = pcOffset; | |
64 } | |
65 } | |
66 | |
67 public HotSpotCompiledCode(CompilationResult compResult) { | |
68 this.comp = compResult; | |
69 sites = getSortedSites(compResult); | |
70 if (compResult.getExceptionHandlers().isEmpty()) { | |
71 exceptionHandlers = null; | |
72 } else { | |
73 exceptionHandlers = compResult.getExceptionHandlers().toArray(new ExceptionHandler[compResult.getExceptionHandlers().size()]); | |
74 } | |
75 List<CodeAnnotation> annotations = compResult.getAnnotations(); | |
76 comments = new Comment[annotations.size()]; | |
77 if (!annotations.isEmpty()) { | |
78 for (int i = 0; i < comments.length; i++) { | |
79 CodeAnnotation annotation = annotations.get(i); | |
80 String text; | |
81 if (annotation instanceof CodeComment) { | |
82 CodeComment codeComment = (CodeComment) annotation; | |
83 text = codeComment.value; | |
84 } else if (annotation instanceof JumpTable) { | |
85 JumpTable jumpTable = (JumpTable) annotation; | |
86 text = "JumpTable [" + jumpTable.low + " .. " + jumpTable.high + "]"; | |
87 } else { | |
88 text = annotation.toString(); | |
89 } | |
90 comments[i] = new Comment(annotation.position, text); | |
91 } | |
92 } | |
93 assert validateFrames(); | |
94 | |
95 DataSection data = compResult.getDataSection(); | |
96 data.finalizeLayout(); | |
97 dataSection = new byte[data.getSectionSize()]; | |
98 | |
99 ByteBuffer buffer = ByteBuffer.wrap(dataSection).order(ByteOrder.nativeOrder()); | |
100 Builder<DataPatch> patchBuilder = Stream.builder(); | |
101 data.buildDataSection(buffer, patchBuilder); | |
102 | |
103 dataSectionAlignment = data.getSectionAlignment(); | |
104 dataSectionPatches = patchBuilder.build().toArray(len -> new DataPatch[len]); | |
105 } | |
106 | |
107 /** | |
108 * Ensure that all the frames passed into HotSpot are properly formatted with an empty or | |
109 * illegal slot following double word slots. | |
110 */ | |
111 private boolean validateFrames() { | |
112 for (Site site : sites) { | |
113 if (site instanceof Infopoint) { | |
114 Infopoint info = (Infopoint) site; | |
115 if (info.debugInfo != null) { | |
116 BytecodeFrame frame = info.debugInfo.frame(); | |
117 assert frame == null || frame.validateFormat(false); | |
118 } | |
119 } | |
120 } | |
121 return true; | |
122 } | |
123 | |
124 static class SiteComparator implements Comparator<Site> { | |
125 | |
126 public int compare(Site s1, Site s2) { | |
127 if (s1.pcOffset == s2.pcOffset && (s1 instanceof Mark ^ s2 instanceof Mark)) { | |
128 return s1 instanceof Mark ? -1 : 1; | |
129 } | |
130 return s1.pcOffset - s2.pcOffset; | |
131 } | |
132 } | |
133 | |
134 private static Site[] getSortedSites(CompilationResult target) { | |
135 List<?>[] lists = new List<?>[]{target.getInfopoints(), target.getDataPatches(), target.getMarks()}; | |
136 int count = 0; | |
137 for (List<?> list : lists) { | |
138 count += list.size(); | |
139 } | |
140 Site[] result = new Site[count]; | |
141 int pos = 0; | |
142 for (List<?> list : lists) { | |
143 for (Object elem : list) { | |
144 result[pos++] = (Site) elem; | |
145 } | |
146 } | |
147 Arrays.sort(result, new SiteComparator()); | |
148 return result; | |
149 } | |
150 } |