Mercurial > hg > truffle
annotate truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/PostOrderDeserializer.java @ 22157:dc83cc1f94f2
Using fully qualified imports
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Wed, 16 Sep 2015 11:33:22 +0200 |
parents | 4858c5e074e9 |
children | 0d4b0e4263ee |
rev | line source |
---|---|
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
1 /* |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
4 * |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
7 * published by the Free Software Foundation. Oracle designates this |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
8 * particular file as subject to the "Classpath" exception as provided |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
9 * by Oracle in the LICENSE file that accompanied this code. |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
10 * |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
15 * accompanied this code). |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
16 * |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
17 * You should have received a copy of the GNU General Public License version |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
20 * |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
22 * or visit www.oracle.com if you need additional information or have any |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
23 * questions. |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
24 */ |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
25 package com.oracle.truffle.api.nodes.serial; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
26 |
22157
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21985
diff
changeset
|
27 import com.oracle.truffle.api.nodes.Node; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21985
diff
changeset
|
28 import com.oracle.truffle.api.nodes.NodeClass; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21985
diff
changeset
|
29 import com.oracle.truffle.api.nodes.NodeFieldAccessor; |
20129
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
18904
diff
changeset
|
30 import com.oracle.truffle.api.nodes.NodeFieldAccessor.NodeFieldKind; |
22157
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21985
diff
changeset
|
31 import com.oracle.truffle.api.source.SourceSection; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21985
diff
changeset
|
32 import java.lang.reflect.Array; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21985
diff
changeset
|
33 import java.lang.reflect.Field; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21985
diff
changeset
|
34 import java.util.ArrayList; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21985
diff
changeset
|
35 import java.util.List; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21985
diff
changeset
|
36 import sun.misc.Unsafe; |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
37 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
38 /** |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
39 * Experimental API. May change without notice. |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
40 */ |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
41 public final class PostOrderDeserializer { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
42 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
43 private static final Unsafe unsafe = loadUnsafe(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
44 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
45 private final SerializerConstantPool cp; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
46 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
47 private final HierarchicalStack stack = new HierarchicalStack(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
48 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
49 /** |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
50 * Constructs a new serializer using a custom {@link SerializerConstantPool} implementation. For |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
51 * the {@link SerializerConstantPool} implementation at least the following methods must be |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
52 * implemented: |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
53 * <ul> |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
54 * <li>{@link SerializerConstantPool#getInt(int)}</li> |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
55 * <li>{@link SerializerConstantPool#getClass(int)}</li> |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
56 * </ul> |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
57 */ |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
58 public PostOrderDeserializer(SerializerConstantPool cp) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
59 this.cp = cp; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
60 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
61 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
62 /** |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
63 * Deserializes the byte stream and returns the deserialized Truffle AST node. |
16067
915ebb306fcc
Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
14991
diff
changeset
|
64 * |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
65 * @param bytes the trimmed byte array containing the serialized data |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
66 * @param expectedType the expected root node type. Throws an exception if the root node is not |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
67 * assignable from this type. |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
68 * @return the deserialized Truffle AST represented by the root Node. |
16067
915ebb306fcc
Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
14991
diff
changeset
|
69 * |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
70 * @throws UnsupportedConstantPoolTypeException thrown if a type is encountered that is not |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
71 * supported by the constant pool implementation. |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
72 */ |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
73 @SuppressWarnings("unchecked") |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
74 public <T extends Node> T deserialize(byte[] bytes, Class<T> expectedType) throws UnsupportedConstantPoolTypeException { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
75 VariableLengthIntBuffer buffer = new VariableLengthIntBuffer(bytes); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
76 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
77 while (buffer.hasRemaining()) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
78 int classCPI = buffer.get(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
79 if (classCPI == VariableLengthIntBuffer.NULL) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
80 pushNode(null); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
81 } else { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
82 Class<?> clazz = cp.getClass(classCPI); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
83 if (clazz.isArray()) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
84 int lengthCPI = buffer.get(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
85 if (lengthCPI == VariableLengthIntBuffer.NULL) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
86 pushArray(null); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
87 } else { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
88 pushArray((Node[]) Array.newInstance(clazz.getComponentType(), cp.getInt(lengthCPI))); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
89 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
90 } else { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
91 pushNode(invokeDeserialize(buffer, clazz.asSubclass(Node.class))); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
92 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
93 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
94 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
95 T returnNode = (T) popNode(null, expectedType); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
96 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
97 assert stack.dynamicStack.isEmpty(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
98 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
99 return returnNode; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
100 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
101 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
102 private void pushNode(Node node) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
103 stack.push(node); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
104 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
105 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
106 private void pushArray(Node[] array) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
107 stack.pushStack(array); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
108 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
109 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
110 private Node[] popArray(Node parent, Class<?> expectedType) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
111 Node[] array = (Node[]) stack.popStack(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
112 if (array != null) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
113 assertType(array, expectedType); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
114 for (int i = 0; i < array.length; i++) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
115 updateParent(parent, array[i]); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
116 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
117 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
118 return array; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
119 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
120 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
121 private Node popNode(Node parent, Class<?> expectedType) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
122 Object o = stack.pop(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
123 assertType(o, expectedType); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
124 updateParent(parent, (Node) o); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
125 return (Node) o; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
126 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
127 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
128 private static void assertType(Object o, Class<?> expectedType) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
129 if (o != null && !expectedType.isAssignableFrom(o.getClass())) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
130 throw new AssertionError("Expected element type '" + expectedType.getName() + "' but was '" + o.getClass().getName() + "'."); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
131 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
132 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
133 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
134 private Node invokeDeserialize(VariableLengthIntBuffer buffer, Class<? extends Node> nodeClass) throws UnsupportedConstantPoolTypeException { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
135 if (nodeClass == null) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
136 return null; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
137 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
138 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
139 Object object; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
140 try { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
141 object = unsafe.allocateInstance(nodeClass); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
142 } catch (InstantiationException e) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
143 throw new RuntimeException("Unable to allocate truffle node " + nodeClass, e); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
144 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
145 if (!(object instanceof Node)) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
146 throw new RuntimeException("Class is not a truffle node " + nodeClass); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
147 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
148 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
149 Node node = (Node) object; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
150 |
20129
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
18904
diff
changeset
|
151 NodeFieldAccessor[] nodeFields = NodeClass.get(nodeClass).getFields(); |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
152 deserializeChildrenFields(node, nodeFields); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
153 deserializeChildFields(node, nodeFields); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
154 deserializeDataFields(buffer, node, nodeFields); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
155 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
156 return node; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
157 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
158 |
20129
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
18904
diff
changeset
|
159 private void deserializeDataFields(VariableLengthIntBuffer buffer, Node nodeInstance, NodeFieldAccessor[] nodeFields) throws UnsupportedConstantPoolTypeException { |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
160 for (int i = 0; i < nodeFields.length; i++) { |
20129
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
18904
diff
changeset
|
161 NodeFieldAccessor field = nodeFields[i]; |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
162 if (field.getKind() == NodeFieldKind.DATA) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
163 Class<?> fieldClass = field.getType(); |
21985
4858c5e074e9
clean up NodeFieldAccessor (remove #getOffset())
Andreas Woess <andreas.woess@oracle.com>
parents:
21951
diff
changeset
|
164 long offset = getFieldOffset(field); |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
165 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
166 // source sections are not serialized |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
167 // TODO add support for source sections |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
168 if (field.getType().isAssignableFrom(SourceSection.class)) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
169 continue; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
170 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
171 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
172 int cpi = buffer.get(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
173 if (cpi == VariableLengthIntBuffer.NULL) { |
16542
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
174 deserializeDataFieldsLengthNull(nodeInstance, fieldClass, offset); |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
175 } else { |
16542
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
176 deserializeDataFieldsDefault(nodeInstance, fieldClass, offset, cpi); |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
177 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
178 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
179 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
180 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
181 |
16542
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
182 private void deserializeDataFieldsDefault(Node nodeInstance, Class<?> fieldClass, long offset, int cpi) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
183 if (fieldClass == int.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
184 unsafe.putInt(nodeInstance, offset, cp.getInt(cpi)); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
185 } else if (fieldClass == long.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
186 unsafe.putLong(nodeInstance, offset, cp.getLong(cpi)); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
187 } else if (fieldClass == float.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
188 unsafe.putFloat(nodeInstance, offset, cp.getFloat(cpi)); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
189 } else if (fieldClass == double.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
190 unsafe.putDouble(nodeInstance, offset, cp.getDouble(cpi)); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
191 } else if (fieldClass == byte.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
192 unsafe.putByte(nodeInstance, offset, (byte) cp.getInt(cpi)); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
193 } else if (fieldClass == short.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
194 unsafe.putShort(nodeInstance, offset, (short) cp.getInt(cpi)); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
195 } else if (fieldClass == char.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
196 unsafe.putChar(nodeInstance, offset, (char) cp.getInt(cpi)); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
197 } else if (fieldClass == boolean.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
198 unsafe.putBoolean(nodeInstance, offset, cp.getInt(cpi) == 1 ? true : false); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
199 } else if (fieldClass == Integer.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
200 unsafe.putObject(nodeInstance, offset, cp.getInt(cpi)); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
201 } else if (fieldClass == Long.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
202 unsafe.putObject(nodeInstance, offset, cp.getLong(cpi)); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
203 } else if (fieldClass == Float.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
204 unsafe.putObject(nodeInstance, offset, cp.getFloat(cpi)); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
205 } else if (fieldClass == Double.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
206 unsafe.putObject(nodeInstance, offset, cp.getDouble(cpi)); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
207 } else if (fieldClass == Byte.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
208 unsafe.putObject(nodeInstance, offset, (byte) cp.getInt(cpi)); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
209 } else if (fieldClass == Short.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
210 unsafe.putObject(nodeInstance, offset, (short) cp.getInt(cpi)); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
211 } else if (fieldClass == Character.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
212 unsafe.putObject(nodeInstance, offset, (char) cp.getInt(cpi)); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
213 } else if (fieldClass == Boolean.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
214 unsafe.putObject(nodeInstance, offset, cp.getInt(cpi) == 1 ? Boolean.TRUE : Boolean.FALSE); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
215 } else { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
216 unsafe.putObject(nodeInstance, offset, cp.getObject(fieldClass, cpi)); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
217 } |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
218 } |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
219 |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
220 private static void deserializeDataFieldsLengthNull(Node nodeInstance, Class<?> fieldClass, long offset) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
221 if (fieldClass == int.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
222 unsafe.putInt(nodeInstance, offset, 0); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
223 } else if (fieldClass == long.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
224 unsafe.putLong(nodeInstance, offset, 0L); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
225 } else if (fieldClass == float.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
226 unsafe.putFloat(nodeInstance, offset, 0.0F); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
227 } else if (fieldClass == double.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
228 unsafe.putDouble(nodeInstance, offset, 0.0D); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
229 } else if (fieldClass == byte.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
230 unsafe.putByte(nodeInstance, offset, (byte) 0); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
231 } else if (fieldClass == short.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
232 unsafe.putShort(nodeInstance, offset, (short) 0); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
233 } else if (fieldClass == char.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
234 unsafe.putChar(nodeInstance, offset, (char) 0); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
235 } else if (fieldClass == boolean.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
236 unsafe.putBoolean(nodeInstance, offset, false); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
237 } else { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
238 unsafe.putObject(nodeInstance, offset, null); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
239 } |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
240 } |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
241 |
20129
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
18904
diff
changeset
|
242 private void deserializeChildFields(Node parent, NodeFieldAccessor[] nodeFields) { |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
243 for (int i = nodeFields.length - 1; i >= 0; i--) { |
20129
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
18904
diff
changeset
|
244 NodeFieldAccessor field = nodeFields[i]; |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
245 if (field.getKind() == NodeFieldKind.CHILD) { |
21985
4858c5e074e9
clean up NodeFieldAccessor (remove #getOffset())
Andreas Woess <andreas.woess@oracle.com>
parents:
21951
diff
changeset
|
246 unsafe.putObject(parent, getFieldOffset(field), popNode(parent, field.getType())); |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
247 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
248 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
249 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
250 |
20129
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
18904
diff
changeset
|
251 private void deserializeChildrenFields(Node parent, NodeFieldAccessor[] nodeFields) { |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
252 for (int i = nodeFields.length - 1; i >= 0; i--) { |
20129
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
18904
diff
changeset
|
253 NodeFieldAccessor field = nodeFields[i]; |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
254 if (field.getKind() == NodeFieldKind.CHILDREN) { |
21985
4858c5e074e9
clean up NodeFieldAccessor (remove #getOffset())
Andreas Woess <andreas.woess@oracle.com>
parents:
21951
diff
changeset
|
255 unsafe.putObject(parent, getFieldOffset(field), popArray(parent, field.getType())); |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
256 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
257 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
258 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
259 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
260 private static Node updateParent(Node parent, Node child) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
261 if (child != null) { |
20130
8dc73c226c63
Truffle: cache NodeClass lookup in Node.
Christian Humer <christian.humer@gmail.com>
parents:
20129
diff
changeset
|
262 NodeClass nodeClass = NodeClass.get(child.getClass()); |
8dc73c226c63
Truffle: cache NodeClass lookup in Node.
Christian Humer <christian.humer@gmail.com>
parents:
20129
diff
changeset
|
263 nodeClass.getNodeClassField().putObject(child, nodeClass); |
8dc73c226c63
Truffle: cache NodeClass lookup in Node.
Christian Humer <christian.humer@gmail.com>
parents:
20129
diff
changeset
|
264 nodeClass.getParentField().putObject(child, parent); |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
265 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
266 return child; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
267 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
268 |
21985
4858c5e074e9
clean up NodeFieldAccessor (remove #getOffset())
Andreas Woess <andreas.woess@oracle.com>
parents:
21951
diff
changeset
|
269 static long getFieldOffset(NodeFieldAccessor field) { |
4858c5e074e9
clean up NodeFieldAccessor (remove #getOffset())
Andreas Woess <andreas.woess@oracle.com>
parents:
21951
diff
changeset
|
270 if (field instanceof NodeFieldAccessor.AbstractUnsafeNodeFieldAccessor) { |
4858c5e074e9
clean up NodeFieldAccessor (remove #getOffset())
Andreas Woess <andreas.woess@oracle.com>
parents:
21951
diff
changeset
|
271 return ((NodeFieldAccessor.AbstractUnsafeNodeFieldAccessor) field).getOffset(); |
4858c5e074e9
clean up NodeFieldAccessor (remove #getOffset())
Andreas Woess <andreas.woess@oracle.com>
parents:
21951
diff
changeset
|
272 } else { |
4858c5e074e9
clean up NodeFieldAccessor (remove #getOffset())
Andreas Woess <andreas.woess@oracle.com>
parents:
21951
diff
changeset
|
273 try { |
4858c5e074e9
clean up NodeFieldAccessor (remove #getOffset())
Andreas Woess <andreas.woess@oracle.com>
parents:
21951
diff
changeset
|
274 Field reflectionField = field.getDeclaringClass().getDeclaredField(field.getName()); |
4858c5e074e9
clean up NodeFieldAccessor (remove #getOffset())
Andreas Woess <andreas.woess@oracle.com>
parents:
21951
diff
changeset
|
275 return unsafe.objectFieldOffset(reflectionField); |
4858c5e074e9
clean up NodeFieldAccessor (remove #getOffset())
Andreas Woess <andreas.woess@oracle.com>
parents:
21951
diff
changeset
|
276 } catch (NoSuchFieldException | SecurityException e) { |
4858c5e074e9
clean up NodeFieldAccessor (remove #getOffset())
Andreas Woess <andreas.woess@oracle.com>
parents:
21951
diff
changeset
|
277 throw new RuntimeException(e); |
4858c5e074e9
clean up NodeFieldAccessor (remove #getOffset())
Andreas Woess <andreas.woess@oracle.com>
parents:
21951
diff
changeset
|
278 } |
4858c5e074e9
clean up NodeFieldAccessor (remove #getOffset())
Andreas Woess <andreas.woess@oracle.com>
parents:
21951
diff
changeset
|
279 } |
4858c5e074e9
clean up NodeFieldAccessor (remove #getOffset())
Andreas Woess <andreas.woess@oracle.com>
parents:
21951
diff
changeset
|
280 } |
4858c5e074e9
clean up NodeFieldAccessor (remove #getOffset())
Andreas Woess <andreas.woess@oracle.com>
parents:
21951
diff
changeset
|
281 |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
282 private static Unsafe loadUnsafe() { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
283 try { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
284 return Unsafe.getUnsafe(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
285 } catch (SecurityException e) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
286 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
287 try { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
288 Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe"); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
289 theUnsafeInstance.setAccessible(true); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
290 return (Unsafe) theUnsafeInstance.get(Unsafe.class); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
291 } catch (Exception e) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
292 throw new RuntimeException("exception while trying to get Unsafe.theUnsafe via reflection:", e); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
293 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
294 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
295 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
296 private static class HierarchicalStack { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
297 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
298 private static final Object NULL_STACK = new Object(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
299 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
300 private final List<Object> dynamicStack = new ArrayList<>(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
301 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
302 void pushStack(Object[] array) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
303 if (array == null) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
304 dynamicStack.add(NULL_STACK); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
305 } else { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
306 dynamicStack.add(new FixedSizeNodeStack(array)); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
307 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
308 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
309 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
310 private FixedSizeNodeStack getTosStack() { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
311 if (dynamicStack.isEmpty()) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
312 return null; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
313 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
314 Object peekTos = dynamicStack.get(dynamicStack.size() - 1); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
315 if (peekTos != null && peekTos.getClass() == FixedSizeNodeStack.class) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
316 return (FixedSizeNodeStack) peekTos; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
317 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
318 return null; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
319 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
320 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
321 Object[] popStack() { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
322 Object tos = dynamicStack.remove(dynamicStack.size() - 1); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
323 if (tos == NULL_STACK) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
324 return null; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
325 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
326 return ((FixedSizeNodeStack) tos).getArray(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
327 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
328 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
329 void push(Object o) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
330 FixedSizeNodeStack tosStack = getTosStack(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
331 if (tosStack != null && !tosStack.isFull()) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
332 tosStack.push(o); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
333 } else { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
334 dynamicStack.add(o); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
335 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
336 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
337 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
338 Object pop() { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
339 FixedSizeNodeStack tosStack = getTosStack(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
340 Object value; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
341 if (tosStack != null) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
342 assert !tosStack.isEmpty(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
343 value = tosStack.pop(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
344 } else { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
345 value = dynamicStack.remove(dynamicStack.size() - 1); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
346 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
347 assert value != NULL_STACK; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
348 return value; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
349 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
350 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
351 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
352 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
353 private static class FixedSizeNodeStack { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
354 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
355 private final Object[] array; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
356 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
357 private int tos; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
358 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
359 FixedSizeNodeStack(Object[] array) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
360 this.array = array; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
361 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
362 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
363 boolean isFull() { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
364 return tos == array.length; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
365 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
366 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
367 boolean isEmpty() { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
368 return tos == 0; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
369 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
370 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
371 private void push(Object node) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
372 if (tos >= array.length) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
373 throw new ArrayIndexOutOfBoundsException(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
374 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
375 unsafe.putObject(array, Unsafe.ARRAY_OBJECT_BASE_OFFSET + Unsafe.ARRAY_OBJECT_INDEX_SCALE * (long) (tos++), node); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
376 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
377 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
378 private Object pop() { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
379 if (tos <= 0) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
380 throw new ArrayIndexOutOfBoundsException(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
381 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
382 return unsafe.getObject(array, Unsafe.ARRAY_OBJECT_BASE_OFFSET + Unsafe.ARRAY_OBJECT_INDEX_SCALE * (long) (--tos)); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
383 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
384 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
385 private Object[] getArray() { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
386 return array; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
387 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
388 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
389 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
390 } |