Mercurial > hg > truffle
changeset 15295:21663230ba88
Move FieldIntrospection to graal.compiler.common.
author | Josef Eisl <josef.eisl@jku.at> |
---|---|
date | Tue, 22 Apr 2014 17:15:55 +0200 |
parents | 23711d4e6108 |
children | c98708ec9663 |
files | graal/com.oracle.graal.compiler.common/src/com/oracle/graal/compiler/common/FieldIntrospection.java graal/com.oracle.graal.graph/src/com/oracle/graal/graph/FieldIntrospection.java graal/com.oracle.graal.graph/src/com/oracle/graal/graph/NodeClass.java graal/com.oracle.graal.lir/src/com/oracle/graal/lir/CompositeValueClass.java graal/com.oracle.graal.lir/src/com/oracle/graal/lir/LIRInstructionClass.java graal/com.oracle.graal.lir/src/com/oracle/graal/lir/LIRIntrospection.java |
diffstat | 6 files changed, 181 insertions(+), 179 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.compiler.common/src/com/oracle/graal/compiler/common/FieldIntrospection.java Tue Apr 22 17:15:55 2014 +0200 @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.graal.compiler.common; + +import java.lang.reflect.*; +import java.util.*; +import java.util.concurrent.*; + +public abstract class FieldIntrospection extends UnsafeAccess { + + /** + * Interface used by {@link #rescanAllFieldOffsets(CalcOffset)} to determine the offset (in + * bytes) of a field. + */ + public interface CalcOffset { + + long getOffset(Field field); + } + + public static class DefaultCalcOffset implements CalcOffset { + + @Override + public long getOffset(Field field) { + return unsafe.objectFieldOffset(field); + } + } + + protected static final ConcurrentHashMap<Class<?>, FieldIntrospection> allClasses = new ConcurrentHashMap<>(); + + private final Class<?> clazz; + protected long[] dataOffsets; + protected Map<Long, String> fieldNames; + protected Map<Long, Class<?>> fieldTypes; + + public FieldIntrospection(Class<?> clazz) { + this.clazz = clazz; + } + + public Class<?> getClazz() { + return clazz; + } + + public static void rescanAllFieldOffsets(CalcOffset calc) { + for (FieldIntrospection nodeClass : allClasses.values()) { + nodeClass.rescanFieldOffsets(calc); + } + } + + protected abstract void rescanFieldOffsets(CalcOffset calc); + + public abstract static class BaseFieldScanner { + + private final CalcOffset calc; + + /** The offsets of fields that are not specially handled by subclasses. */ + public final ArrayList<Long> dataOffsets = new ArrayList<>(); + + public final Map<Long, String> fieldNames = new HashMap<>(); + public final Map<Long, Class<?>> fieldTypes = new HashMap<>(); + + protected BaseFieldScanner(CalcOffset calc) { + this.calc = calc; + } + + public void scan(Class<?> clazz) { + Class<?> currentClazz = clazz; + do { + for (Field field : currentClazz.getDeclaredFields()) { + if (Modifier.isStatic(field.getModifiers())) { + continue; + } + Class<?> type = field.getType(); + long offset = calc.getOffset(field); + + // scanField() may overwrite the name with a customized name. + fieldNames.put(offset, field.getName()); + fieldTypes.put(offset, type); + + scanField(field, type, offset); + } + currentClazz = currentClazz.getSuperclass(); + } while (currentClazz.getSuperclass() != Object.class); + } + + protected abstract void scanField(Field field, Class<?> type, long offset); + } + + protected static void copyInto(long[] dest, long[] src) { + assert dest.length == src.length; + for (int i = 0; i < dest.length; i++) { + dest[i] = src[i]; + } + } + + protected static <T> void copyInto(T[] dest, T[] src) { + assert dest.length == src.length; + for (int i = 0; i < dest.length; i++) { + dest[i] = src[i]; + } + } + + protected static <T> void copyInto(T[] dest, List<T> src) { + assert dest.length == src.size(); + for (int i = 0; i < dest.length; i++) { + dest[i] = src.get(i); + } + } + + protected static <T> T[] arrayUsingSortedOffsets(Map<Long, T> map, long[] sortedOffsets, T[] result) { + for (int i = 0; i < sortedOffsets.length; i++) { + result[i] = map.get(sortedOffsets[i]); + } + return result; + } + + protected static long[] sortedLongCopy(ArrayList<Long> list1) { + Collections.sort(list1); + long[] result = new long[list1.size()]; + for (int i = 0; i < list1.size(); i++) { + result[i] = list1.get(i); + } + return result; + } + + protected static long[] sortedLongCopy(ArrayList<Long> list1, ArrayList<Long> list2) { + Collections.sort(list1); + Collections.sort(list2); + long[] result = new long[list1.size() + list2.size()]; + for (int i = 0; i < list1.size(); i++) { + result[i] = list1.get(i); + } + for (int i = 0; i < list2.size(); i++) { + result[list1.size() + i] = list2.get(i); + } + return result; + } +}
--- a/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/FieldIntrospection.java Wed Apr 23 11:50:59 2014 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,155 +0,0 @@ -/* - * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.oracle.graal.graph; - -import java.lang.reflect.*; -import java.util.*; -import java.util.concurrent.*; - -import com.oracle.graal.compiler.common.*; - -public abstract class FieldIntrospection extends UnsafeAccess { - - /** - * Interface used by {@link #rescanAllFieldOffsets(CalcOffset)} to determine the offset (in - * bytes) of a field. - */ - public interface CalcOffset { - - long getOffset(Field field); - } - - public static class DefaultCalcOffset implements CalcOffset { - - @Override - public long getOffset(Field field) { - return unsafe.objectFieldOffset(field); - } - } - - protected static final ConcurrentHashMap<Class<?>, FieldIntrospection> allClasses = new ConcurrentHashMap<>(); - - protected final Class<?> clazz; - protected long[] dataOffsets; - protected Map<Long, String> fieldNames; - protected Map<Long, Class<?>> fieldTypes; - - public FieldIntrospection(Class<?> clazz) { - this.clazz = clazz; - } - - public static void rescanAllFieldOffsets(CalcOffset calc) { - for (FieldIntrospection nodeClass : allClasses.values()) { - nodeClass.rescanFieldOffsets(calc); - } - } - - protected abstract void rescanFieldOffsets(CalcOffset calc); - - public abstract static class BaseFieldScanner { - - private final CalcOffset calc; - - /** The offsets of fields that are not specially handled by subclasses. */ - public final ArrayList<Long> dataOffsets = new ArrayList<>(); - - public final Map<Long, String> fieldNames = new HashMap<>(); - public final Map<Long, Class<?>> fieldTypes = new HashMap<>(); - - protected BaseFieldScanner(CalcOffset calc) { - this.calc = calc; - } - - protected void scan(Class<?> clazz) { - Class<?> currentClazz = clazz; - do { - for (Field field : currentClazz.getDeclaredFields()) { - if (Modifier.isStatic(field.getModifiers())) { - continue; - } - Class<?> type = field.getType(); - long offset = calc.getOffset(field); - - // scanField() may overwrite the name with a customized name. - fieldNames.put(offset, field.getName()); - fieldTypes.put(offset, type); - - scanField(field, type, offset); - } - currentClazz = currentClazz.getSuperclass(); - } while (currentClazz.getSuperclass() != Object.class); - } - - protected abstract void scanField(Field field, Class<?> type, long offset); - } - - protected static void copyInto(long[] dest, long[] src) { - assert dest.length == src.length; - for (int i = 0; i < dest.length; i++) { - dest[i] = src[i]; - } - } - - protected static <T> void copyInto(T[] dest, T[] src) { - assert dest.length == src.length; - for (int i = 0; i < dest.length; i++) { - dest[i] = src[i]; - } - } - - protected static <T> void copyInto(T[] dest, List<T> src) { - assert dest.length == src.size(); - for (int i = 0; i < dest.length; i++) { - dest[i] = src.get(i); - } - } - - protected static <T> T[] arrayUsingSortedOffsets(Map<Long, T> map, long[] sortedOffsets, T[] result) { - for (int i = 0; i < sortedOffsets.length; i++) { - result[i] = map.get(sortedOffsets[i]); - } - return result; - } - - protected static long[] sortedLongCopy(ArrayList<Long> list1) { - Collections.sort(list1); - long[] result = new long[list1.size()]; - for (int i = 0; i < list1.size(); i++) { - result[i] = list1.get(i); - } - return result; - } - - protected static long[] sortedLongCopy(ArrayList<Long> list1, ArrayList<Long> list2) { - Collections.sort(list1); - Collections.sort(list2); - long[] result = new long[list1.size() + list2.size()]; - for (int i = 0; i < list1.size(); i++) { - result[i] = list1.get(i); - } - for (int i = 0; i < list2.size(); i++) { - result[list1.size() + i] = list2.get(i); - } - return result; - } -}
--- a/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/NodeClass.java Wed Apr 23 11:50:59 2014 +0200 +++ b/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/NodeClass.java Tue Apr 22 17:15:55 2014 +0200 @@ -179,10 +179,10 @@ this.iterableId = nextIterableId++; List<NodeClass> existingClasses = new LinkedList<>(); for (FieldIntrospection nodeClass : allClasses.values()) { - if (clazz.isAssignableFrom(nodeClass.clazz)) { + if (clazz.isAssignableFrom(nodeClass.getClazz())) { existingClasses.add((NodeClass) nodeClass); } - if (nodeClass.clazz.isAssignableFrom(clazz) && IterableNodeType.class.isAssignableFrom(nodeClass.clazz)) { + if (nodeClass.getClazz().isAssignableFrom(clazz) && IterableNodeType.class.isAssignableFrom(nodeClass.getClazz())) { NodeClass superNodeClass = (NodeClass) nodeClass; superNodeClass.iterableIds = Arrays.copyOf(superNodeClass.iterableIds, superNodeClass.iterableIds.length + 1); superNodeClass.iterableIds[superNodeClass.iterableIds.length - 1] = this.iterableId; @@ -207,7 +207,7 @@ @Override protected void rescanFieldOffsets(CalcOffset calc) { FieldScanner scanner = new FieldScanner(calc); - scanner.scan(clazz); + scanner.scan(getClazz()); assert directInputCount == scanner.inputOffsets.size(); copyInto(inputOffsets, sortedLongCopy(scanner.inputOffsets, scanner.inputListOffsets)); assert directSuccessorCount == scanner.successorOffsets.size(); @@ -324,7 +324,7 @@ @Override public String toString() { StringBuilder str = new StringBuilder(); - str.append("NodeClass ").append(clazz.getSimpleName()).append(" ["); + str.append("NodeClass ").append(getClazz().getSimpleName()).append(" ["); for (int i = 0; i < inputOffsets.length; i++) { str.append(i == 0 ? "" : ", ").append(inputOffsets[i]); } @@ -972,7 +972,7 @@ int index = startIndex; while (index < inputOffsets.length) { NodeList<Node> list = getNodeList(node, inputOffsets[index]); - assert list != null : clazz; + assert list != null : getClazz(); putNodeList(node, inputOffsets[index], updateInputListCopy(list, node, duplicationReplacement)); index++; } @@ -982,7 +982,7 @@ int index = startIndex; while (index < successorOffsets.length) { NodeList<Node> list = getNodeList(node, successorOffsets[index]); - assert list != null : clazz; + assert list != null : getClazz(); putNodeList(node, successorOffsets[index], updateSuccListCopy(list, node, duplicationReplacement)); index++; } @@ -1039,7 +1039,7 @@ } public NodeClassIterable getInputIterable(final Node node) { - assert clazz.isInstance(node); + assert getClazz().isInstance(node); return new NodeClassIterable() { @Override @@ -1059,7 +1059,7 @@ } public NodeClassIterable getSuccessorIterable(final Node node) { - assert clazz.isInstance(node); + assert getClazz().isInstance(node); return new NodeClassIterable() { @Override @@ -1092,7 +1092,7 @@ } while (index < inputOffsets.length) { NodeList<Node> list = getNodeList(node, inputOffsets[index]); - assert list != null : clazz; + assert list != null : getClazz(); if (list.replaceFirst(old, other)) { return true; } @@ -1115,7 +1115,7 @@ } while (index < successorOffsets.length) { NodeList<Node> list = getNodeList(node, successorOffsets[index]); - assert list != null : clazz + " " + successorOffsets[index] + " " + node; + assert list != null : getClazz() + " " + successorOffsets[index] + " " + node; if (list.replaceFirst(old, other)) { return true; } @@ -1172,7 +1172,7 @@ * @param newNode the node to which the inputs should be copied. */ public void copyInputs(Node node, Node newNode) { - assert node.getClass() == clazz && newNode.getClass() == clazz; + assert node.getClass() == getClazz() && newNode.getClass() == getClazz(); int index = 0; while (index < directInputCount) { @@ -1194,7 +1194,7 @@ * @param newNode the node to which the successors should be copied. */ public void copySuccessors(Node node, Node newNode) { - assert node.getClass() == clazz && newNode.getClass() == clazz; + assert node.getClass() == getClazz() && newNode.getClass() == getClazz(); int index = 0; while (index < directSuccessorCount) { @@ -1213,7 +1213,7 @@ } public boolean inputsEqual(Node node, Node other) { - assert node.getClass() == clazz && other.getClass() == clazz; + assert node.getClass() == getClazz() && other.getClass() == getClazz(); int index = 0; while (index < directInputCount) { if (getNode(other, inputOffsets[index]) != getNode(node, inputOffsets[index])) { @@ -1232,7 +1232,7 @@ } public boolean successorsEqual(Node node, Node other) { - assert node.getClass() == clazz && other.getClass() == clazz; + assert node.getClass() == getClazz() && other.getClass() == getClazz(); int index = 0; while (index < directSuccessorCount) { if (getNode(other, successorOffsets[index]) != getNode(node, successorOffsets[index])) { @@ -1251,7 +1251,7 @@ } public boolean inputContains(Node node, Node other) { - assert node.getClass() == clazz; + assert node.getClass() == getClazz(); int index = 0; while (index < directInputCount) { @@ -1271,7 +1271,7 @@ } public boolean successorContains(Node node, Node other) { - assert node.getClass() == clazz; + assert node.getClass() == getClazz(); int index = 0; while (index < directSuccessorCount) { @@ -1353,7 +1353,7 @@ } public Class<?> getJavaClass() { - return clazz; + return getClazz(); } /**
--- a/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/CompositeValueClass.java Wed Apr 23 11:50:59 2014 +0200 +++ b/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/CompositeValueClass.java Tue Apr 22 17:15:55 2014 +0200 @@ -86,7 +86,7 @@ @Override protected void rescanFieldOffsets(CalcOffset calc) { ValueFieldScanner scanner = new ValueFieldScanner(calc); - scanner.scan(clazz); + scanner.scan(getClazz()); OperandModeAnnotation mode = scanner.valueAnnotations.get(CompositeValue.Component.class); copyInto(componentOffsets, sortedLongCopy(mode.scalarOffsets, mode.arrayOffsets)); @@ -108,7 +108,7 @@ } @Override - protected void scan(Class<?> clazz) { + public void scan(Class<?> clazz) { super.scan(clazz); } @@ -127,7 +127,7 @@ @Override public String toString() { StringBuilder str = new StringBuilder(); - str.append(getClass().getSimpleName()).append(" ").append(clazz.getSimpleName()).append(" component["); + str.append(getClass().getSimpleName()).append(" ").append(getClazz().getSimpleName()).append(" component["); for (int i = 0; i < componentOffsets.length; i++) { str.append(i == 0 ? "" : ", ").append(componentOffsets[i]); }
--- a/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/LIRInstructionClass.java Wed Apr 23 11:50:59 2014 +0200 +++ b/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/LIRInstructionClass.java Tue Apr 22 17:15:55 2014 +0200 @@ -118,7 +118,7 @@ @Override protected void rescanFieldOffsets(CalcOffset calc) { InstructionFieldScanner scanner = new InstructionFieldScanner(calc); - scanner.scan(clazz); + scanner.scan(getClazz()); OperandModeAnnotation mode = scanner.valueAnnotations.get(LIRInstruction.Use.class); copyInto(useOffsets, sortedLongCopy(mode.scalarOffsets, mode.arrayOffsets)); @@ -177,7 +177,7 @@ } @Override - protected void scan(Class<?> clazz) { + public void scan(Class<?> clazz) { if (clazz.getAnnotation(Opcode.class) != null) { opcodeConstant = clazz.getAnnotation(Opcode.class).value(); } @@ -213,7 +213,7 @@ @Override public String toString() { StringBuilder str = new StringBuilder(); - str.append(getClass().getSimpleName()).append(" ").append(clazz.getSimpleName()).append(" use["); + str.append(getClass().getSimpleName()).append(" ").append(getClazz().getSimpleName()).append(" use["); for (int i = 0; i < useOffsets.length; i++) { str.append(i == 0 ? "" : ", ").append(useOffsets[i]); }
--- a/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/LIRIntrospection.java Wed Apr 23 11:50:59 2014 +0200 +++ b/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/LIRIntrospection.java Tue Apr 22 17:15:55 2014 +0200 @@ -31,7 +31,7 @@ import com.oracle.graal.api.code.*; import com.oracle.graal.api.meta.*; -import com.oracle.graal.graph.*; +import com.oracle.graal.compiler.common.*; import com.oracle.graal.lir.LIRInstruction.OperandFlag; import com.oracle.graal.lir.LIRInstruction.OperandMode; import com.oracle.graal.lir.LIRInstruction.ValueProcedure;