43972
|
1 |
/*
|
52910
|
2 |
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.lir;
|
|
26 |
|
|
27 |
import java.lang.reflect.Field;
|
|
28 |
import java.util.Arrays;
|
|
29 |
import java.util.EnumSet;
|
|
30 |
|
|
31 |
import org.graalvm.compiler.core.common.FieldIntrospection;
|
|
32 |
import org.graalvm.compiler.core.common.Fields;
|
|
33 |
import org.graalvm.compiler.core.common.FieldsScanner;
|
|
34 |
import org.graalvm.compiler.debug.GraalError;
|
|
35 |
import org.graalvm.compiler.lir.CompositeValue.Component;
|
|
36 |
import org.graalvm.compiler.lir.LIRInstruction.OperandFlag;
|
|
37 |
import org.graalvm.compiler.lir.LIRIntrospection.LIRFieldsScanner;
|
|
38 |
import org.graalvm.compiler.lir.LIRIntrospection.OperandModeAnnotation;
|
|
39 |
import org.graalvm.compiler.lir.LIRIntrospection.Values;
|
|
40 |
|
|
41 |
/**
|
|
42 |
* Lazily associated metadata for every {@link CompositeValue} type. The metadata includes:
|
|
43 |
* <ul>
|
|
44 |
* <li>The offsets of fields annotated with {@link Component} as well as methods for iterating over
|
|
45 |
* such fields.</li>
|
|
46 |
* </ul>
|
|
47 |
*/
|
|
48 |
public final class CompositeValueClass<T> extends FieldIntrospection<T> {
|
|
49 |
|
|
50 |
/**
|
|
51 |
* The CompositeValueClass is only used for formatting for the most part so cache it as a
|
|
52 |
* ClassValue.
|
|
53 |
*/
|
|
54 |
private static final ClassValue<CompositeValueClass<?>> compositeClass = new ClassValue<CompositeValueClass<?>>() {
|
|
55 |
|
|
56 |
@Override
|
|
57 |
protected CompositeValueClass<?> computeValue(Class<?> type) {
|
|
58 |
CompositeValueClass<?> compositeValueClass = new CompositeValueClass<>(type);
|
|
59 |
assert compositeValueClass.values.getDirectCount() == compositeValueClass.values.getCount() : "only direct fields are allowed in composites";
|
|
60 |
return compositeValueClass;
|
|
61 |
}
|
|
62 |
|
|
63 |
};
|
|
64 |
|
|
65 |
@SuppressWarnings("unchecked")
|
|
66 |
public static <T> CompositeValueClass<T> get(Class<T> type) {
|
|
67 |
return (CompositeValueClass<T>) compositeClass.get(type);
|
|
68 |
}
|
|
69 |
|
|
70 |
private final Values values;
|
|
71 |
|
|
72 |
private CompositeValueClass(Class<T> clazz) {
|
|
73 |
super(clazz);
|
|
74 |
|
|
75 |
CompositeValueFieldsScanner vfs = new CompositeValueFieldsScanner(new FieldsScanner.DefaultCalcOffset());
|
|
76 |
vfs.scan(clazz, CompositeValue.class, false);
|
|
77 |
|
|
78 |
values = new Values(vfs.valueAnnotations.get(CompositeValue.Component.class));
|
|
79 |
data = new Fields(vfs.data);
|
|
80 |
}
|
|
81 |
|
|
82 |
private static class CompositeValueFieldsScanner extends LIRFieldsScanner {
|
|
83 |
|
|
84 |
CompositeValueFieldsScanner(FieldsScanner.CalcOffset calc) {
|
|
85 |
super(calc);
|
|
86 |
valueAnnotations.put(CompositeValue.Component.class, new OperandModeAnnotation());
|
|
87 |
}
|
|
88 |
|
|
89 |
@Override
|
|
90 |
protected EnumSet<OperandFlag> getFlags(Field field) {
|
|
91 |
EnumSet<OperandFlag> result = EnumSet.noneOf(OperandFlag.class);
|
|
92 |
if (field.isAnnotationPresent(CompositeValue.Component.class)) {
|
|
93 |
result.addAll(Arrays.asList(field.getAnnotation(CompositeValue.Component.class).value()));
|
|
94 |
} else {
|
|
95 |
GraalError.shouldNotReachHere();
|
|
96 |
}
|
|
97 |
return result;
|
|
98 |
}
|
|
99 |
}
|
|
100 |
|
|
101 |
@Override
|
|
102 |
public Fields[] getAllFields() {
|
|
103 |
return new Fields[]{data, values};
|
|
104 |
}
|
|
105 |
|
|
106 |
@Override
|
|
107 |
public String toString() {
|
|
108 |
StringBuilder str = new StringBuilder();
|
|
109 |
str.append(getClass().getSimpleName()).append(" ").append(getClazz().getSimpleName()).append(" components[");
|
|
110 |
values.appendFields(str);
|
|
111 |
str.append("] data[");
|
|
112 |
data.appendFields(str);
|
|
113 |
str.append("]");
|
|
114 |
return str.toString();
|
|
115 |
}
|
|
116 |
|
|
117 |
public static String format(CompositeValue obj) {
|
|
118 |
CompositeValueClass<?> valueClass = compositeClass.get(obj.getClass());
|
|
119 |
StringBuilder result = new StringBuilder();
|
|
120 |
|
|
121 |
LIRIntrospection.appendValues(result, obj, "", "", "{", "}", new String[]{""}, valueClass.values);
|
|
122 |
|
|
123 |
for (int i = 0; i < valueClass.data.getCount(); i++) {
|
|
124 |
result.append(" ").append(valueClass.data.getName(i)).append(": ").append(LIRIntrospection.getFieldString(obj, i, valueClass.data));
|
|
125 |
}
|
|
126 |
|
|
127 |
return result.toString();
|
|
128 |
}
|
|
129 |
}
|