Mercurial > hg > truffle
annotate graal/com.oracle.graal.lir/src/com/oracle/graal/lir/CompositeValueClass.java @ 15637:40f13c935d8b
mx: fix constructor call
author | Bernhard Urban <bernhard.urban@jku.at> |
---|---|
date | Wed, 14 May 2014 11:08:05 +0200 |
parents | 21663230ba88 |
children | f315b1c0a590 |
rev | line source |
---|---|
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 | |
15193
96bb07a5d667
Spit up and move GraalInternalError.
Josef Eisl <josef.eisl@jku.at>
parents:
13127
diff
changeset
|
28 import com.oracle.graal.compiler.common.*; |
11404
aaf95ab77bf5
macro node for CompositeValue.get()
Doug Simon <doug.simon@oracle.com>
parents:
8165
diff
changeset
|
29 import com.oracle.graal.lir.CompositeValue.Component; |
8165 | 30 import com.oracle.graal.lir.LIRInstruction.OperandFlag; |
31 import com.oracle.graal.lir.LIRInstruction.OperandMode; | |
32 import com.oracle.graal.lir.LIRInstruction.ValueProcedure; | |
33 | |
11404
aaf95ab77bf5
macro node for CompositeValue.get()
Doug Simon <doug.simon@oracle.com>
parents:
8165
diff
changeset
|
34 /** |
aaf95ab77bf5
macro node for CompositeValue.get()
Doug Simon <doug.simon@oracle.com>
parents:
8165
diff
changeset
|
35 * Lazily associated metadata for every {@link CompositeValue} type. The metadata includes: |
aaf95ab77bf5
macro node for CompositeValue.get()
Doug Simon <doug.simon@oracle.com>
parents:
8165
diff
changeset
|
36 * <ul> |
aaf95ab77bf5
macro node for CompositeValue.get()
Doug Simon <doug.simon@oracle.com>
parents:
8165
diff
changeset
|
37 * <li>The offsets of fields annotated with {@link Component} as well as methods for iterating over |
aaf95ab77bf5
macro node for CompositeValue.get()
Doug Simon <doug.simon@oracle.com>
parents:
8165
diff
changeset
|
38 * such fields.</li> |
aaf95ab77bf5
macro node for CompositeValue.get()
Doug Simon <doug.simon@oracle.com>
parents:
8165
diff
changeset
|
39 * </ul> |
aaf95ab77bf5
macro node for CompositeValue.get()
Doug Simon <doug.simon@oracle.com>
parents:
8165
diff
changeset
|
40 */ |
8165 | 41 public class CompositeValueClass extends LIRIntrospection { |
42 | |
43 public static final CompositeValueClass get(Class<? extends CompositeValue> c) { | |
44 CompositeValueClass clazz = (CompositeValueClass) allClasses.get(c); | |
45 if (clazz != null) { | |
46 return clazz; | |
47 } | |
48 | |
49 // We can have a race of multiple threads creating the LIRInstructionClass at the same time. | |
50 // However, only one will be put into the map, and this is the one returned by all threads. | |
51 clazz = new CompositeValueClass(c); | |
52 CompositeValueClass oldClazz = (CompositeValueClass) allClasses.putIfAbsent(c, clazz); | |
53 if (oldClazz != null) { | |
54 return oldClazz; | |
55 } else { | |
56 return clazz; | |
57 } | |
58 } | |
59 | |
60 private final int directComponentCount; | |
61 private final long[] componentOffsets; | |
62 private final EnumSet<OperandFlag>[] componentFlags; | |
63 | |
13127
f42f1f5d5ce0
Allow custom field offset providers
Christian Wimmer <christian.wimmer@oracle.com>
parents:
11404
diff
changeset
|
64 public CompositeValueClass(Class<? extends CompositeValue> clazz) { |
f42f1f5d5ce0
Allow custom field offset providers
Christian Wimmer <christian.wimmer@oracle.com>
parents:
11404
diff
changeset
|
65 this(clazz, new DefaultCalcOffset()); |
f42f1f5d5ce0
Allow custom field offset providers
Christian Wimmer <christian.wimmer@oracle.com>
parents:
11404
diff
changeset
|
66 } |
f42f1f5d5ce0
Allow custom field offset providers
Christian Wimmer <christian.wimmer@oracle.com>
parents:
11404
diff
changeset
|
67 |
8165 | 68 @SuppressWarnings("unchecked") |
13127
f42f1f5d5ce0
Allow custom field offset providers
Christian Wimmer <christian.wimmer@oracle.com>
parents:
11404
diff
changeset
|
69 public CompositeValueClass(Class<? extends CompositeValue> clazz, CalcOffset calcOffset) { |
8165 | 70 super(clazz); |
71 | |
13127
f42f1f5d5ce0
Allow custom field offset providers
Christian Wimmer <christian.wimmer@oracle.com>
parents:
11404
diff
changeset
|
72 ValueFieldScanner scanner = new ValueFieldScanner(calcOffset); |
8165 | 73 scanner.scan(clazz); |
74 | |
75 OperandModeAnnotation mode = scanner.valueAnnotations.get(CompositeValue.Component.class); | |
76 directComponentCount = mode.scalarOffsets.size(); | |
77 componentOffsets = sortedLongCopy(mode.scalarOffsets, mode.arrayOffsets); | |
78 componentFlags = arrayUsingSortedOffsets(mode.flags, componentOffsets, new EnumSet[componentOffsets.length]); | |
79 | |
80 dataOffsets = sortedLongCopy(scanner.dataOffsets); | |
81 | |
82 fieldNames = scanner.fieldNames; | |
83 fieldTypes = scanner.fieldTypes; | |
84 } | |
85 | |
86 @Override | |
87 protected void rescanFieldOffsets(CalcOffset calc) { | |
88 ValueFieldScanner scanner = new ValueFieldScanner(calc); | |
15295
21663230ba88
Move FieldIntrospection to graal.compiler.common.
Josef Eisl <josef.eisl@jku.at>
parents:
15193
diff
changeset
|
89 scanner.scan(getClazz()); |
8165 | 90 |
91 OperandModeAnnotation mode = scanner.valueAnnotations.get(CompositeValue.Component.class); | |
92 copyInto(componentOffsets, sortedLongCopy(mode.scalarOffsets, mode.arrayOffsets)); | |
93 | |
94 copyInto(dataOffsets, sortedLongCopy(scanner.dataOffsets)); | |
95 | |
96 fieldNames.clear(); | |
97 fieldNames.putAll(scanner.fieldNames); | |
98 fieldTypes.clear(); | |
99 fieldTypes.putAll(scanner.fieldTypes); | |
100 } | |
101 | |
102 private static class ValueFieldScanner extends FieldScanner { | |
103 | |
104 public ValueFieldScanner(CalcOffset calc) { | |
105 super(calc); | |
106 | |
107 valueAnnotations.put(CompositeValue.Component.class, new OperandModeAnnotation()); | |
108 } | |
109 | |
110 @Override | |
15295
21663230ba88
Move FieldIntrospection to graal.compiler.common.
Josef Eisl <josef.eisl@jku.at>
parents:
15193
diff
changeset
|
111 public void scan(Class<?> clazz) { |
8165 | 112 super.scan(clazz); |
113 } | |
114 | |
115 @Override | |
116 protected EnumSet<OperandFlag> getFlags(Field field) { | |
117 EnumSet<OperandFlag> result = EnumSet.noneOf(OperandFlag.class); | |
118 if (field.isAnnotationPresent(CompositeValue.Component.class)) { | |
119 result.addAll(Arrays.asList(field.getAnnotation(CompositeValue.Component.class).value())); | |
120 } else { | |
121 GraalInternalError.shouldNotReachHere(); | |
122 } | |
123 return result; | |
124 } | |
125 } | |
126 | |
127 @Override | |
128 public String toString() { | |
129 StringBuilder str = new StringBuilder(); | |
15295
21663230ba88
Move FieldIntrospection to graal.compiler.common.
Josef Eisl <josef.eisl@jku.at>
parents:
15193
diff
changeset
|
130 str.append(getClass().getSimpleName()).append(" ").append(getClazz().getSimpleName()).append(" component["); |
8165 | 131 for (int i = 0; i < componentOffsets.length; i++) { |
132 str.append(i == 0 ? "" : ", ").append(componentOffsets[i]); | |
133 } | |
134 str.append("] data["); | |
135 for (int i = 0; i < dataOffsets.length; i++) { | |
136 str.append(i == 0 ? "" : ", ").append(dataOffsets[i]); | |
137 } | |
138 str.append("]"); | |
139 return str.toString(); | |
140 } | |
141 | |
142 public final void forEachComponent(CompositeValue obj, OperandMode mode, ValueProcedure proc) { | |
143 forEach(obj, directComponentCount, componentOffsets, mode, componentFlags, proc); | |
144 } | |
145 | |
146 public String toString(CompositeValue obj) { | |
147 StringBuilder result = new StringBuilder(); | |
148 | |
149 appendValues(result, obj, "", "", "{", "}", new String[]{""}, componentOffsets); | |
150 | |
151 for (int i = 0; i < dataOffsets.length; i++) { | |
152 result.append(" ").append(fieldNames.get(dataOffsets[i])).append(": ").append(getFieldString(obj, dataOffsets[i])); | |
153 } | |
154 | |
155 return result.toString(); | |
156 } | |
157 } |