Mercurial > hg > graal-jvmci-8
annotate jvmci/jdk.internal.jvmci.code/src/jdk/internal/jvmci/code/DataSection.java @ 22565:a7c7901367ed
Merge
author | Christian Wimmer <christian.wimmer@oracle.com> |
---|---|
date | Tue, 15 Sep 2015 18:13:11 -0700 |
parents | 0915f5bfdfaa 118f9560e0ea |
children | ec96f33a101d |
rev | line source |
---|---|
22054 | 1 /* |
2 * Copyright (c) 2014, 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 jdk.internal.jvmci.code; | |
24 | |
25 import static jdk.internal.jvmci.meta.MetaUtil.*; | |
26 | |
27 import java.nio.*; | |
28 import java.util.*; | |
29 import java.util.function.*; | |
30 | |
31 import jdk.internal.jvmci.code.CompilationResult.*; | |
32 import jdk.internal.jvmci.code.DataSection.*; | |
33 import jdk.internal.jvmci.meta.*; | |
34 | |
35 public final class DataSection implements Iterable<Data> { | |
36 | |
37 @FunctionalInterface | |
38 public interface DataBuilder { | |
39 | |
40 void emit(ByteBuffer buffer, Consumer<DataPatch> patch); | |
41 | |
42 static DataBuilder raw(byte[] data) { | |
43 return (buffer, patch) -> buffer.put(data); | |
44 } | |
45 | |
46 static DataBuilder serializable(SerializableConstant c) { | |
47 return (buffer, patch) -> c.serialize(buffer); | |
48 } | |
49 | |
50 static DataBuilder zero(int size) { | |
51 switch (size) { | |
52 case 1: | |
53 return (buffer, patch) -> buffer.put((byte) 0); | |
54 case 2: | |
55 return (buffer, patch) -> buffer.putShort((short) 0); | |
56 case 4: | |
57 return (buffer, patch) -> buffer.putInt(0); | |
58 case 8: | |
59 return (buffer, patch) -> buffer.putLong(0L); | |
60 default: | |
61 return (buffer, patch) -> { | |
62 int rest = size; | |
63 while (rest > 8) { | |
64 buffer.putLong(0L); | |
65 rest -= 8; | |
66 } | |
67 while (rest > 0) { | |
68 buffer.put((byte) 0); | |
69 rest--; | |
70 } | |
71 }; | |
72 } | |
73 } | |
74 } | |
75 | |
76 public static final class Data { | |
77 | |
78 private int alignment; | |
79 private final int size; | |
80 private final DataBuilder builder; | |
81 | |
82 private DataSectionReference ref; | |
83 | |
84 public Data(int alignment, int size, DataBuilder builder) { | |
85 this.alignment = alignment; | |
86 this.size = size; | |
87 this.builder = builder; | |
88 | |
89 // initialized in DataSection.insertData(Data) | |
90 ref = null; | |
91 } | |
92 | |
93 public void updateAlignment(int newAlignment) { | |
94 if (newAlignment == alignment) { | |
95 return; | |
96 } | |
97 alignment = lcm(alignment, newAlignment); | |
98 } | |
99 | |
100 public int getAlignment() { | |
101 return alignment; | |
102 } | |
103 | |
104 public int getSize() { | |
105 return size; | |
106 } | |
107 | |
108 public DataBuilder getBuilder() { | |
109 return builder; | |
110 } | |
111 | |
112 @Override | |
113 public int hashCode() { | |
114 // Data instances should not be used as hash map keys | |
115 throw new UnsupportedOperationException("hashCode"); | |
116 } | |
117 | |
118 @Override | |
119 public String toString() { | |
120 return identityHashCodeString(this); | |
121 } | |
122 | |
123 @Override | |
124 public boolean equals(Object obj) { | |
125 assert ref != null; | |
126 if (obj == this) { | |
127 return true; | |
128 } | |
129 if (obj instanceof Data) { | |
130 Data that = (Data) obj; | |
131 if (this.alignment == that.alignment && this.size == that.size && this.ref.equals(that.ref)) { | |
132 return true; | |
133 } | |
134 } | |
135 return false; | |
136 } | |
137 } | |
138 | |
139 private final ArrayList<Data> dataItems = new ArrayList<>(); | |
140 | |
141 private boolean finalLayout; | |
142 private int sectionAlignment; | |
143 private int sectionSize; | |
144 | |
145 @Override | |
146 public int hashCode() { | |
147 // DataSection instances should not be used as hash map keys | |
148 throw new UnsupportedOperationException("hashCode"); | |
149 } | |
150 | |
151 @Override | |
152 public String toString() { | |
153 return identityHashCodeString(this); | |
154 } | |
155 | |
156 @Override | |
157 public boolean equals(Object obj) { | |
158 if (this == obj) { | |
159 return true; | |
160 } | |
161 if (obj instanceof DataSection) { | |
162 DataSection that = (DataSection) obj; | |
163 if (this.finalLayout == that.finalLayout && this.sectionAlignment == that.sectionAlignment && this.sectionSize == that.sectionSize && Objects.equals(this.dataItems, that.dataItems)) { | |
164 return true; | |
165 } | |
166 } | |
167 return false; | |
168 } | |
169 | |
170 /** | |
171 * Insert a {@link Data} item into the data section. If the item is already in the data section, | |
172 * the same {@link DataSectionReference} is returned. | |
173 * | |
174 * @param data the {@link Data} item to be inserted | |
175 * @return a unique {@link DataSectionReference} identifying the {@link Data} item | |
176 */ | |
177 public DataSectionReference insertData(Data data) { | |
178 assert !finalLayout; | |
22564
118f9560e0ea
Allow merging of DataSection, provide basic thread safety for adding Data to DataSection
Christian Wimmer <christian.wimmer@oracle.com>
parents:
22411
diff
changeset
|
179 synchronized (data) { |
118f9560e0ea
Allow merging of DataSection, provide basic thread safety for adding Data to DataSection
Christian Wimmer <christian.wimmer@oracle.com>
parents:
22411
diff
changeset
|
180 if (data.ref == null) { |
118f9560e0ea
Allow merging of DataSection, provide basic thread safety for adding Data to DataSection
Christian Wimmer <christian.wimmer@oracle.com>
parents:
22411
diff
changeset
|
181 data.ref = new DataSectionReference(); |
118f9560e0ea
Allow merging of DataSection, provide basic thread safety for adding Data to DataSection
Christian Wimmer <christian.wimmer@oracle.com>
parents:
22411
diff
changeset
|
182 dataItems.add(data); |
118f9560e0ea
Allow merging of DataSection, provide basic thread safety for adding Data to DataSection
Christian Wimmer <christian.wimmer@oracle.com>
parents:
22411
diff
changeset
|
183 } |
118f9560e0ea
Allow merging of DataSection, provide basic thread safety for adding Data to DataSection
Christian Wimmer <christian.wimmer@oracle.com>
parents:
22411
diff
changeset
|
184 return data.ref; |
118f9560e0ea
Allow merging of DataSection, provide basic thread safety for adding Data to DataSection
Christian Wimmer <christian.wimmer@oracle.com>
parents:
22411
diff
changeset
|
185 } |
118f9560e0ea
Allow merging of DataSection, provide basic thread safety for adding Data to DataSection
Christian Wimmer <christian.wimmer@oracle.com>
parents:
22411
diff
changeset
|
186 } |
118f9560e0ea
Allow merging of DataSection, provide basic thread safety for adding Data to DataSection
Christian Wimmer <christian.wimmer@oracle.com>
parents:
22411
diff
changeset
|
187 |
118f9560e0ea
Allow merging of DataSection, provide basic thread safety for adding Data to DataSection
Christian Wimmer <christian.wimmer@oracle.com>
parents:
22411
diff
changeset
|
188 /** |
118f9560e0ea
Allow merging of DataSection, provide basic thread safety for adding Data to DataSection
Christian Wimmer <christian.wimmer@oracle.com>
parents:
22411
diff
changeset
|
189 * Transfers all {@link Data} from the provided other {@link DataSection} to this |
118f9560e0ea
Allow merging of DataSection, provide basic thread safety for adding Data to DataSection
Christian Wimmer <christian.wimmer@oracle.com>
parents:
22411
diff
changeset
|
190 * {@link DataSection}, and empties the other section. |
118f9560e0ea
Allow merging of DataSection, provide basic thread safety for adding Data to DataSection
Christian Wimmer <christian.wimmer@oracle.com>
parents:
22411
diff
changeset
|
191 */ |
118f9560e0ea
Allow merging of DataSection, provide basic thread safety for adding Data to DataSection
Christian Wimmer <christian.wimmer@oracle.com>
parents:
22411
diff
changeset
|
192 public void addAll(DataSection other) { |
118f9560e0ea
Allow merging of DataSection, provide basic thread safety for adding Data to DataSection
Christian Wimmer <christian.wimmer@oracle.com>
parents:
22411
diff
changeset
|
193 assert !finalLayout && !other.finalLayout; |
118f9560e0ea
Allow merging of DataSection, provide basic thread safety for adding Data to DataSection
Christian Wimmer <christian.wimmer@oracle.com>
parents:
22411
diff
changeset
|
194 |
118f9560e0ea
Allow merging of DataSection, provide basic thread safety for adding Data to DataSection
Christian Wimmer <christian.wimmer@oracle.com>
parents:
22411
diff
changeset
|
195 for (Data data : other.dataItems) { |
118f9560e0ea
Allow merging of DataSection, provide basic thread safety for adding Data to DataSection
Christian Wimmer <christian.wimmer@oracle.com>
parents:
22411
diff
changeset
|
196 assert data.ref != null; |
22054 | 197 dataItems.add(data); |
198 } | |
22564
118f9560e0ea
Allow merging of DataSection, provide basic thread safety for adding Data to DataSection
Christian Wimmer <christian.wimmer@oracle.com>
parents:
22411
diff
changeset
|
199 other.dataItems.clear(); |
22054 | 200 } |
201 | |
202 /** | |
203 * Compute the layout of the data section. This can be called only once, and after it has been | |
204 * called, the data section can no longer be modified. | |
205 */ | |
206 public void finalizeLayout() { | |
207 assert !finalLayout; | |
208 finalLayout = true; | |
209 | |
210 // simple heuristic: put items with larger alignment requirement first | |
211 dataItems.sort((a, b) -> a.alignment - b.alignment); | |
212 | |
213 int position = 0; | |
22564
118f9560e0ea
Allow merging of DataSection, provide basic thread safety for adding Data to DataSection
Christian Wimmer <christian.wimmer@oracle.com>
parents:
22411
diff
changeset
|
214 int alignment = 1; |
22054 | 215 for (Data d : dataItems) { |
22564
118f9560e0ea
Allow merging of DataSection, provide basic thread safety for adding Data to DataSection
Christian Wimmer <christian.wimmer@oracle.com>
parents:
22411
diff
changeset
|
216 alignment = lcm(alignment, d.alignment); |
22054 | 217 position = align(position, d.alignment); |
218 | |
219 d.ref.setOffset(position); | |
220 position += d.size; | |
221 } | |
222 | |
22564
118f9560e0ea
Allow merging of DataSection, provide basic thread safety for adding Data to DataSection
Christian Wimmer <christian.wimmer@oracle.com>
parents:
22411
diff
changeset
|
223 sectionAlignment = alignment; |
22054 | 224 sectionSize = position; |
225 } | |
226 | |
22517
0915f5bfdfaa
Adopt some jvmci-9 source changes
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
22411
diff
changeset
|
227 public boolean isFinalized() { |
0915f5bfdfaa
Adopt some jvmci-9 source changes
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
22411
diff
changeset
|
228 return finalLayout; |
0915f5bfdfaa
Adopt some jvmci-9 source changes
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
22411
diff
changeset
|
229 } |
0915f5bfdfaa
Adopt some jvmci-9 source changes
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
22411
diff
changeset
|
230 |
22054 | 231 /** |
232 * Get the size of the data section. Can only be called after {@link #finalizeLayout}. | |
233 */ | |
234 public int getSectionSize() { | |
235 assert finalLayout; | |
236 return sectionSize; | |
237 } | |
238 | |
239 /** | |
240 * Get the minimum alignment requirement of the data section. Can only be called after | |
241 * {@link #finalizeLayout}. | |
242 */ | |
243 public int getSectionAlignment() { | |
244 assert finalLayout; | |
245 return sectionAlignment; | |
246 } | |
247 | |
248 /** | |
249 * Build the data section. Can only be called after {@link #finalizeLayout}. | |
250 * | |
251 * @param buffer The {@link ByteBuffer} where the data section should be built. The buffer must | |
252 * hold at least {@link #getSectionSize()} bytes. | |
253 * @param patch A {@link Consumer} to receive {@link DataPatch data patches} for relocations in | |
254 * the data section. | |
255 */ | |
256 public void buildDataSection(ByteBuffer buffer, Consumer<DataPatch> patch) { | |
257 assert finalLayout; | |
258 for (Data d : dataItems) { | |
259 buffer.position(d.ref.getOffset()); | |
260 d.builder.emit(buffer, patch); | |
261 } | |
262 } | |
263 | |
264 public Data findData(DataSectionReference ref) { | |
265 for (Data d : dataItems) { | |
266 if (d.ref == ref) { | |
267 return d; | |
268 } | |
269 } | |
270 return null; | |
271 } | |
272 | |
273 public Iterator<Data> iterator() { | |
274 return dataItems.iterator(); | |
275 } | |
276 | |
22411
78c691145967
Support putting multiple constants next to each other in DataSection.
Roland Schatz <roland.schatz@oracle.com>
parents:
22113
diff
changeset
|
277 public static int lcm(int x, int y) { |
22054 | 278 if (x == 0) { |
279 return y; | |
280 } else if (y == 0) { | |
281 return x; | |
282 } | |
283 | |
284 int a = Math.max(x, y); | |
285 int b = Math.min(x, y); | |
286 while (b > 0) { | |
287 int tmp = a % b; | |
288 a = b; | |
289 b = tmp; | |
290 } | |
291 | |
292 int gcd = a; | |
293 return x * y / gcd; | |
294 } | |
295 | |
296 private static int align(int position, int alignment) { | |
297 return ((position + alignment - 1) / alignment) * alignment; | |
298 } | |
22113
598c75a3d1d1
Clear DataSection too, when CompilationResult is cleared
Stefan Anzinger <stefan.anzinger@oracle.com>
parents:
22054
diff
changeset
|
299 |
598c75a3d1d1
Clear DataSection too, when CompilationResult is cleared
Stefan Anzinger <stefan.anzinger@oracle.com>
parents:
22054
diff
changeset
|
300 public void clear() { |
598c75a3d1d1
Clear DataSection too, when CompilationResult is cleared
Stefan Anzinger <stefan.anzinger@oracle.com>
parents:
22054
diff
changeset
|
301 assert !finalLayout; |
598c75a3d1d1
Clear DataSection too, when CompilationResult is cleared
Stefan Anzinger <stefan.anzinger@oracle.com>
parents:
22054
diff
changeset
|
302 this.dataItems.clear(); |
598c75a3d1d1
Clear DataSection too, when CompilationResult is cleared
Stefan Anzinger <stefan.anzinger@oracle.com>
parents:
22054
diff
changeset
|
303 this.sectionAlignment = 0; |
598c75a3d1d1
Clear DataSection too, when CompilationResult is cleared
Stefan Anzinger <stefan.anzinger@oracle.com>
parents:
22054
diff
changeset
|
304 this.sectionSize = 0; |
598c75a3d1d1
Clear DataSection too, when CompilationResult is cleared
Stefan Anzinger <stefan.anzinger@oracle.com>
parents:
22054
diff
changeset
|
305 } |
22054 | 306 } |