annotate graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/PostOrderDeserializer.java @ 20129:5b7db8941fd7

Truffle: make NodeClass and NodeField a top-level class.
author Christian Humer <christian.humer@gmail.com>
date Thu, 02 Apr 2015 01:22:41 +0200
parents 6ba170cb6f53
children 8dc73c226c63
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
27 import java.lang.reflect.*;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
28 import java.util.*;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
29
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
30 import sun.misc.*;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
31
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
32 import com.oracle.truffle.api.nodes.*;
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 18904
diff changeset
33 import com.oracle.truffle.api.nodes.NodeFieldAccessor.NodeFieldKind;
16067
915ebb306fcc Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 14991
diff changeset
34 import com.oracle.truffle.api.source.*;
12388
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
35
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
36 /**
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
37 * Experimental API. May change without notice.
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 public final class PostOrderDeserializer {
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 private static final Unsafe unsafe = loadUnsafe();
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 final SerializerConstantPool cp;
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 HierarchicalStack stack = new HierarchicalStack();
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 /**
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
48 * 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
49 * 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
50 * implemented:
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
51 * <ul>
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
52 * <li>{@link SerializerConstantPool#getInt(int)}</li>
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
53 * <li>{@link SerializerConstantPool#getClass(int)}</li>
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
54 * </ul>
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
55 */
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
56 public PostOrderDeserializer(SerializerConstantPool cp) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
57 this.cp = cp;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
58 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
59
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 * 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
62 *
12388
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
63 * @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
64 * @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
65 * assignable from this type.
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
66 * @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
67 *
12388
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
68 * @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
69 * supported by the constant pool implementation.
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
70 */
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
71 @SuppressWarnings("unchecked")
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
72 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
73 VariableLengthIntBuffer buffer = new VariableLengthIntBuffer(bytes);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
74
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
75 while (buffer.hasRemaining()) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
76 int classCPI = buffer.get();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
77 if (classCPI == VariableLengthIntBuffer.NULL) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
78 pushNode(null);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
79 } else {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
80 Class<?> clazz = cp.getClass(classCPI);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
81 if (clazz.isArray()) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
82 int lengthCPI = buffer.get();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
83 if (lengthCPI == VariableLengthIntBuffer.NULL) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
84 pushArray(null);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
85 } else {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
86 pushArray((Node[]) Array.newInstance(clazz.getComponentType(), cp.getInt(lengthCPI)));
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
87 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
88 } else {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
89 pushNode(invokeDeserialize(buffer, clazz.asSubclass(Node.class)));
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
90 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
91 }
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 T returnNode = (T) popNode(null, expectedType);
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 assert stack.dynamicStack.isEmpty();
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 return returnNode;
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
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
100 private void pushNode(Node node) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
101 stack.push(node);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
102 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
103
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
104 private void pushArray(Node[] array) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
105 stack.pushStack(array);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
106 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
107
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
108 private Node[] popArray(Node parent, Class<?> expectedType) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
109 Node[] array = (Node[]) stack.popStack();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
110 if (array != null) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
111 assertType(array, expectedType);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
112 for (int i = 0; i < array.length; i++) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
113 updateParent(parent, array[i]);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
114 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
115 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
116 return array;
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
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
119 private Node popNode(Node parent, Class<?> expectedType) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
120 Object o = stack.pop();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
121 assertType(o, expectedType);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
122 updateParent(parent, (Node) o);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
123 return (Node) o;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
124 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
125
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
126 private static void assertType(Object o, Class<?> expectedType) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
127 if (o != null && !expectedType.isAssignableFrom(o.getClass())) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
128 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
129 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
130 }
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 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
133 if (nodeClass == null) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
134 return null;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
135 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
136
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
137 Object object;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
138 try {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
139 object = unsafe.allocateInstance(nodeClass);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
140 } catch (InstantiationException e) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
141 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
142 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
143 if (!(object instanceof Node)) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
144 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
145 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
146
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
147 Node node = (Node) object;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
148
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 18904
diff changeset
149 NodeFieldAccessor[] nodeFields = NodeClass.get(nodeClass).getFields();
12388
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
150 deserializeChildrenFields(node, nodeFields);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
151 deserializeChildFields(node, nodeFields);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
152 deserializeDataFields(buffer, node, nodeFields);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
153
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
154 return node;
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
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 18904
diff changeset
157 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
158 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
159 NodeFieldAccessor field = nodeFields[i];
12388
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
160 if (field.getKind() == NodeFieldKind.DATA) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
161 Class<?> fieldClass = field.getType();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
162 long offset = field.getOffset();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
163
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
164 // source sections are not serialized
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
165 // TODO add support for source sections
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
166 if (field.getType().isAssignableFrom(SourceSection.class)) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
167 continue;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
168 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
169
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
170 int cpi = buffer.get();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
171 if (cpi == VariableLengthIntBuffer.NULL) {
16542
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
172 deserializeDataFieldsLengthNull(nodeInstance, fieldClass, offset);
12388
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
173 } else {
16542
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
174 deserializeDataFieldsDefault(nodeInstance, fieldClass, offset, cpi);
12388
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
175 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
176 }
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
16542
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
180 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
181 if (fieldClass == int.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
182 unsafe.putInt(nodeInstance, offset, cp.getInt(cpi));
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
183 } else if (fieldClass == long.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
184 unsafe.putLong(nodeInstance, offset, cp.getLong(cpi));
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
185 } else if (fieldClass == float.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
186 unsafe.putFloat(nodeInstance, offset, cp.getFloat(cpi));
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
187 } else if (fieldClass == double.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
188 unsafe.putDouble(nodeInstance, offset, cp.getDouble(cpi));
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
189 } else if (fieldClass == byte.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
190 unsafe.putByte(nodeInstance, offset, (byte) cp.getInt(cpi));
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
191 } else if (fieldClass == short.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
192 unsafe.putShort(nodeInstance, offset, (short) cp.getInt(cpi));
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
193 } else if (fieldClass == char.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
194 unsafe.putChar(nodeInstance, offset, (char) cp.getInt(cpi));
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
195 } else if (fieldClass == boolean.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
196 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
197 } else if (fieldClass == Integer.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
198 unsafe.putObject(nodeInstance, offset, cp.getInt(cpi));
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
199 } else if (fieldClass == Long.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
200 unsafe.putObject(nodeInstance, offset, cp.getLong(cpi));
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
201 } else if (fieldClass == Float.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
202 unsafe.putObject(nodeInstance, offset, cp.getFloat(cpi));
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
203 } else if (fieldClass == Double.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
204 unsafe.putObject(nodeInstance, offset, cp.getDouble(cpi));
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
205 } else if (fieldClass == Byte.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
206 unsafe.putObject(nodeInstance, offset, (byte) cp.getInt(cpi));
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
207 } else if (fieldClass == Short.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
208 unsafe.putObject(nodeInstance, offset, (short) cp.getInt(cpi));
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
209 } else if (fieldClass == Character.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
210 unsafe.putObject(nodeInstance, offset, (char) cp.getInt(cpi));
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
211 } else if (fieldClass == Boolean.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
212 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
213 } else {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
214 unsafe.putObject(nodeInstance, offset, cp.getObject(fieldClass, cpi));
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
215 }
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
216 }
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 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
219 if (fieldClass == int.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
220 unsafe.putInt(nodeInstance, offset, 0);
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
221 } else if (fieldClass == long.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
222 unsafe.putLong(nodeInstance, offset, 0L);
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
223 } else if (fieldClass == float.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
224 unsafe.putFloat(nodeInstance, offset, 0.0F);
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
225 } else if (fieldClass == double.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
226 unsafe.putDouble(nodeInstance, offset, 0.0D);
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
227 } else if (fieldClass == byte.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
228 unsafe.putByte(nodeInstance, offset, (byte) 0);
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
229 } else if (fieldClass == short.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
230 unsafe.putShort(nodeInstance, offset, (short) 0);
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
231 } else if (fieldClass == char.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
232 unsafe.putChar(nodeInstance, offset, (char) 0);
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
233 } else if (fieldClass == boolean.class) {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
234 unsafe.putBoolean(nodeInstance, offset, false);
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
235 } else {
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
236 unsafe.putObject(nodeInstance, offset, null);
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
237 }
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
238 }
45fff0246a43 extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents: 16067
diff changeset
239
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 18904
diff changeset
240 private void deserializeChildFields(Node parent, NodeFieldAccessor[] nodeFields) {
12388
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
241 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
242 NodeFieldAccessor field = nodeFields[i];
12388
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
243 if (field.getKind() == NodeFieldKind.CHILD) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
244 unsafe.putObject(parent, field.getOffset(), popNode(parent, field.getType()));
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
245 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
246 }
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
20129
5b7db8941fd7 Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents: 18904
diff changeset
249 private void deserializeChildrenFields(Node parent, NodeFieldAccessor[] nodeFields) {
12388
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
250 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
251 NodeFieldAccessor field = nodeFields[i];
12388
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
252 if (field.getKind() == NodeFieldKind.CHILDREN) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
253 unsafe.putObject(parent, field.getOffset(), popArray(parent, field.getType()));
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
254 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
255 }
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 private static Node updateParent(Node parent, Node child) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
259 if (child != null) {
18904
6ba170cb6f53 Truffle: channel all NodeUtil field accesses through NodeField class
Andreas Woess <andreas.woess@jku.at>
parents: 16542
diff changeset
260 NodeClass.get(child.getClass()).getParentField().putObject(child, parent);
12388
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
261 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
262 return child;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
263 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
264
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
265 private static Unsafe loadUnsafe() {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
266 try {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
267 return Unsafe.getUnsafe();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
268 } catch (SecurityException e) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
269 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
270 try {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
271 Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe");
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
272 theUnsafeInstance.setAccessible(true);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
273 return (Unsafe) theUnsafeInstance.get(Unsafe.class);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
274 } catch (Exception e) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
275 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
276 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
277 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
278
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
279 private static class HierarchicalStack {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
280
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
281 private static final Object NULL_STACK = new Object();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
282
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
283 private final List<Object> dynamicStack = new ArrayList<>();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
284
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
285 void pushStack(Object[] array) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
286 if (array == null) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
287 dynamicStack.add(NULL_STACK);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
288 } else {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
289 dynamicStack.add(new FixedSizeNodeStack(array));
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
290 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
291 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
292
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
293 private FixedSizeNodeStack getTosStack() {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
294 if (dynamicStack.isEmpty()) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
295 return null;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
296 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
297 Object peekTos = dynamicStack.get(dynamicStack.size() - 1);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
298 if (peekTos != null && peekTos.getClass() == FixedSizeNodeStack.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
299 return (FixedSizeNodeStack) peekTos;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
300 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
301 return null;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
302 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
303
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
304 Object[] popStack() {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
305 Object tos = dynamicStack.remove(dynamicStack.size() - 1);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
306 if (tos == NULL_STACK) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
307 return null;
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 return ((FixedSizeNodeStack) tos).getArray();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
310 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
311
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
312 void push(Object o) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
313 FixedSizeNodeStack tosStack = getTosStack();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
314 if (tosStack != null && !tosStack.isFull()) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
315 tosStack.push(o);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
316 } else {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
317 dynamicStack.add(o);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
318 }
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 pop() {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
322 FixedSizeNodeStack tosStack = getTosStack();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
323 Object value;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
324 if (tosStack != null) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
325 assert !tosStack.isEmpty();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
326 value = tosStack.pop();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
327 } else {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
328 value = dynamicStack.remove(dynamicStack.size() - 1);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
329 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
330 assert value != NULL_STACK;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
331 return value;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
332 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
333
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
334 }
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 private static class FixedSizeNodeStack {
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 private final Object[] array;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
339
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
340 private int tos;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
341
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
342 FixedSizeNodeStack(Object[] array) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
343 this.array = array;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
344 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
345
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
346 boolean isFull() {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
347 return tos == array.length;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
348 }
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 boolean isEmpty() {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
351 return tos == 0;
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
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
354 private void push(Object node) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
355 if (tos >= array.length) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
356 throw new ArrayIndexOutOfBoundsException();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
357 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
358 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
359 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
360
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
361 private Object pop() {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
362 if (tos <= 0) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
363 throw new ArrayIndexOutOfBoundsException();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
364 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
365 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
366 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
367
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
368 private Object[] getArray() {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
369 return array;
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 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
372
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
373 }