8165
|
1 /*
|
|
2 * Copyright (c) 2013, 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.graal.lir;
|
|
24
|
|
25 import java.lang.reflect.*;
|
|
26 import java.util.*;
|
|
27
|
|
28 import com.oracle.graal.graph.*;
|
|
29 import com.oracle.graal.lir.LIRInstruction.OperandFlag;
|
|
30 import com.oracle.graal.lir.LIRInstruction.OperandMode;
|
|
31 import com.oracle.graal.lir.LIRInstruction.ValueProcedure;
|
|
32
|
|
33 public class CompositeValueClass extends LIRIntrospection {
|
|
34
|
|
35 public static final CompositeValueClass get(Class<? extends CompositeValue> c) {
|
|
36 CompositeValueClass clazz = (CompositeValueClass) allClasses.get(c);
|
|
37 if (clazz != null) {
|
|
38 return clazz;
|
|
39 }
|
|
40
|
|
41 // We can have a race of multiple threads creating the LIRInstructionClass at the same time.
|
|
42 // However, only one will be put into the map, and this is the one returned by all threads.
|
|
43 clazz = new CompositeValueClass(c);
|
|
44 CompositeValueClass oldClazz = (CompositeValueClass) allClasses.putIfAbsent(c, clazz);
|
|
45 if (oldClazz != null) {
|
|
46 return oldClazz;
|
|
47 } else {
|
|
48 return clazz;
|
|
49 }
|
|
50 }
|
|
51
|
|
52 private final int directComponentCount;
|
|
53 private final long[] componentOffsets;
|
|
54 private final EnumSet<OperandFlag>[] componentFlags;
|
|
55
|
|
56 @SuppressWarnings("unchecked")
|
|
57 public CompositeValueClass(Class<? extends CompositeValue> clazz) {
|
|
58 super(clazz);
|
|
59
|
|
60 ValueFieldScanner scanner = new ValueFieldScanner(new DefaultCalcOffset());
|
|
61 scanner.scan(clazz);
|
|
62
|
|
63 OperandModeAnnotation mode = scanner.valueAnnotations.get(CompositeValue.Component.class);
|
|
64 directComponentCount = mode.scalarOffsets.size();
|
|
65 componentOffsets = sortedLongCopy(mode.scalarOffsets, mode.arrayOffsets);
|
|
66 componentFlags = arrayUsingSortedOffsets(mode.flags, componentOffsets, new EnumSet[componentOffsets.length]);
|
|
67
|
|
68 dataOffsets = sortedLongCopy(scanner.dataOffsets);
|
|
69
|
|
70 fieldNames = scanner.fieldNames;
|
|
71 fieldTypes = scanner.fieldTypes;
|
|
72 }
|
|
73
|
|
74 @Override
|
|
75 protected void rescanFieldOffsets(CalcOffset calc) {
|
|
76 ValueFieldScanner scanner = new ValueFieldScanner(calc);
|
|
77 scanner.scan(clazz);
|
|
78
|
|
79 OperandModeAnnotation mode = scanner.valueAnnotations.get(CompositeValue.Component.class);
|
|
80 copyInto(componentOffsets, sortedLongCopy(mode.scalarOffsets, mode.arrayOffsets));
|
|
81
|
|
82 copyInto(dataOffsets, sortedLongCopy(scanner.dataOffsets));
|
|
83
|
|
84 fieldNames.clear();
|
|
85 fieldNames.putAll(scanner.fieldNames);
|
|
86 fieldTypes.clear();
|
|
87 fieldTypes.putAll(scanner.fieldTypes);
|
|
88 }
|
|
89
|
|
90 private static class ValueFieldScanner extends FieldScanner {
|
|
91
|
|
92 public ValueFieldScanner(CalcOffset calc) {
|
|
93 super(calc);
|
|
94
|
|
95 valueAnnotations.put(CompositeValue.Component.class, new OperandModeAnnotation());
|
|
96 }
|
|
97
|
|
98 @Override
|
|
99 protected void scan(Class<?> clazz) {
|
|
100 super.scan(clazz);
|
|
101 }
|
|
102
|
|
103 @Override
|
|
104 protected EnumSet<OperandFlag> getFlags(Field field) {
|
|
105 EnumSet<OperandFlag> result = EnumSet.noneOf(OperandFlag.class);
|
|
106 if (field.isAnnotationPresent(CompositeValue.Component.class)) {
|
|
107 result.addAll(Arrays.asList(field.getAnnotation(CompositeValue.Component.class).value()));
|
|
108 } else {
|
|
109 GraalInternalError.shouldNotReachHere();
|
|
110 }
|
|
111 return result;
|
|
112 }
|
|
113 }
|
|
114
|
|
115 @Override
|
|
116 public String toString() {
|
|
117 StringBuilder str = new StringBuilder();
|
|
118 str.append(getClass().getSimpleName()).append(" ").append(clazz.getSimpleName()).append(" component[");
|
|
119 for (int i = 0; i < componentOffsets.length; i++) {
|
|
120 str.append(i == 0 ? "" : ", ").append(componentOffsets[i]);
|
|
121 }
|
|
122 str.append("] data[");
|
|
123 for (int i = 0; i < dataOffsets.length; i++) {
|
|
124 str.append(i == 0 ? "" : ", ").append(dataOffsets[i]);
|
|
125 }
|
|
126 str.append("]");
|
|
127 return str.toString();
|
|
128 }
|
|
129
|
|
130 public final void forEachComponent(CompositeValue obj, OperandMode mode, ValueProcedure proc) {
|
|
131 forEach(obj, directComponentCount, componentOffsets, mode, componentFlags, proc);
|
|
132 }
|
|
133
|
|
134 public String toString(CompositeValue obj) {
|
|
135 StringBuilder result = new StringBuilder();
|
|
136
|
|
137 appendValues(result, obj, "", "", "{", "}", new String[]{""}, componentOffsets);
|
|
138
|
|
139 for (int i = 0; i < dataOffsets.length; i++) {
|
|
140 result.append(" ").append(fieldNames.get(dataOffsets[i])).append(": ").append(getFieldString(obj, dataOffsets[i]));
|
|
141 }
|
|
142
|
|
143 return result.toString();
|
|
144 }
|
|
145 }
|