annotate graal/com.oracle.graal.lir/src/com/oracle/graal/lir/CompositeValueClass.java @ 8165:a85ef330ffe7

Composite value.
author Roland Schatz <roland.schatz@oracle.com>
date Thu, 07 Mar 2013 15:16:19 +0100
parents
children aaf95ab77bf5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8165
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
1 /*
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
4 *
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
8 *
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
13 * accompanied this code).
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
14 *
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
18 *
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
21 * questions.
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
22 */
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
23 package com.oracle.graal.lir;
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
24
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
25 import java.lang.reflect.*;
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
26 import java.util.*;
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
27
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
28 import com.oracle.graal.graph.*;
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
29 import com.oracle.graal.lir.LIRInstruction.OperandFlag;
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
30 import com.oracle.graal.lir.LIRInstruction.OperandMode;
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
31 import com.oracle.graal.lir.LIRInstruction.ValueProcedure;
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
32
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
33 public class CompositeValueClass extends LIRIntrospection {
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
34
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
35 public static final CompositeValueClass get(Class<? extends CompositeValue> c) {
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
36 CompositeValueClass clazz = (CompositeValueClass) allClasses.get(c);
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
37 if (clazz != null) {
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
38 return clazz;
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
39 }
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
40
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
41 // We can have a race of multiple threads creating the LIRInstructionClass at the same time.
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
42 // However, only one will be put into the map, and this is the one returned by all threads.
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
43 clazz = new CompositeValueClass(c);
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
44 CompositeValueClass oldClazz = (CompositeValueClass) allClasses.putIfAbsent(c, clazz);
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
45 if (oldClazz != null) {
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
46 return oldClazz;
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
47 } else {
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
48 return clazz;
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
49 }
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
50 }
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
51
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
52 private final int directComponentCount;
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
53 private final long[] componentOffsets;
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
54 private final EnumSet<OperandFlag>[] componentFlags;
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
55
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
56 @SuppressWarnings("unchecked")
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
57 public CompositeValueClass(Class<? extends CompositeValue> clazz) {
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
58 super(clazz);
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
59
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
60 ValueFieldScanner scanner = new ValueFieldScanner(new DefaultCalcOffset());
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
61 scanner.scan(clazz);
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
62
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
63 OperandModeAnnotation mode = scanner.valueAnnotations.get(CompositeValue.Component.class);
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
64 directComponentCount = mode.scalarOffsets.size();
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
65 componentOffsets = sortedLongCopy(mode.scalarOffsets, mode.arrayOffsets);
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
66 componentFlags = arrayUsingSortedOffsets(mode.flags, componentOffsets, new EnumSet[componentOffsets.length]);
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
67
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
68 dataOffsets = sortedLongCopy(scanner.dataOffsets);
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
69
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
70 fieldNames = scanner.fieldNames;
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
71 fieldTypes = scanner.fieldTypes;
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
72 }
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
73
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
74 @Override
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
75 protected void rescanFieldOffsets(CalcOffset calc) {
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
76 ValueFieldScanner scanner = new ValueFieldScanner(calc);
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
77 scanner.scan(clazz);
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
78
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
79 OperandModeAnnotation mode = scanner.valueAnnotations.get(CompositeValue.Component.class);
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
80 copyInto(componentOffsets, sortedLongCopy(mode.scalarOffsets, mode.arrayOffsets));
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
81
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
82 copyInto(dataOffsets, sortedLongCopy(scanner.dataOffsets));
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
83
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
84 fieldNames.clear();
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
85 fieldNames.putAll(scanner.fieldNames);
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
86 fieldTypes.clear();
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
87 fieldTypes.putAll(scanner.fieldTypes);
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
88 }
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
89
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
90 private static class ValueFieldScanner extends FieldScanner {
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
91
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
92 public ValueFieldScanner(CalcOffset calc) {
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
93 super(calc);
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
94
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
95 valueAnnotations.put(CompositeValue.Component.class, new OperandModeAnnotation());
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
96 }
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
97
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
98 @Override
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
99 protected void scan(Class<?> clazz) {
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
100 super.scan(clazz);
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
101 }
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
102
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
103 @Override
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
104 protected EnumSet<OperandFlag> getFlags(Field field) {
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
105 EnumSet<OperandFlag> result = EnumSet.noneOf(OperandFlag.class);
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
106 if (field.isAnnotationPresent(CompositeValue.Component.class)) {
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
107 result.addAll(Arrays.asList(field.getAnnotation(CompositeValue.Component.class).value()));
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
108 } else {
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
109 GraalInternalError.shouldNotReachHere();
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
110 }
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
111 return result;
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
112 }
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
113 }
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
114
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
115 @Override
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
116 public String toString() {
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
117 StringBuilder str = new StringBuilder();
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
118 str.append(getClass().getSimpleName()).append(" ").append(clazz.getSimpleName()).append(" component[");
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
119 for (int i = 0; i < componentOffsets.length; i++) {
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
120 str.append(i == 0 ? "" : ", ").append(componentOffsets[i]);
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
121 }
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
122 str.append("] data[");
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
123 for (int i = 0; i < dataOffsets.length; i++) {
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
124 str.append(i == 0 ? "" : ", ").append(dataOffsets[i]);
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
125 }
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
126 str.append("]");
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
127 return str.toString();
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
128 }
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
129
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
130 public final void forEachComponent(CompositeValue obj, OperandMode mode, ValueProcedure proc) {
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
131 forEach(obj, directComponentCount, componentOffsets, mode, componentFlags, proc);
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
132 }
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
133
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
134 public String toString(CompositeValue obj) {
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
135 StringBuilder result = new StringBuilder();
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
136
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
137 appendValues(result, obj, "", "", "{", "}", new String[]{""}, componentOffsets);
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
138
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
139 for (int i = 0; i < dataOffsets.length; i++) {
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
140 result.append(" ").append(fieldNames.get(dataOffsets[i])).append(": ").append(getFieldString(obj, dataOffsets[i]));
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
141 }
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
142
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
143 return result.toString();
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
144 }
a85ef330ffe7 Composite value.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff changeset
145 }