annotate graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/PostOrderDeserializer.java @ 12388:96c1d057a5ed

Truffle: Added experimental serialization API.
author Christian Humer <christian.humer@gmail.com>
date Wed, 02 Oct 2013 15:33:08 +0200
parents
children 64dcb92ee75a
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.*;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
33 import com.oracle.truffle.api.nodes.*;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
34 import com.oracle.truffle.api.nodes.NodeUtil.*;
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.
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 * @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.
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
67 *
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
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
149 NodeField[] nodeFields = NodeClass.get(nodeClass).getFields();
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
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
157 private void deserializeDataFields(VariableLengthIntBuffer buffer, Node nodeInstance, NodeField[] nodeFields) throws UnsupportedConstantPoolTypeException {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
158 for (int i = 0; i < nodeFields.length; i++) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
159 NodeField field = nodeFields[i];
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) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
172 if (fieldClass == int.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
173 unsafe.putInt(nodeInstance, offset, 0);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
174 } else if (fieldClass == long.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
175 unsafe.putLong(nodeInstance, offset, 0L);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
176 } else if (fieldClass == float.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
177 unsafe.putFloat(nodeInstance, offset, 0.0F);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
178 } else if (fieldClass == double.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
179 unsafe.putDouble(nodeInstance, offset, 0.0D);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
180 } else if (fieldClass == byte.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
181 unsafe.putByte(nodeInstance, offset, (byte) 0);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
182 } else if (fieldClass == short.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
183 unsafe.putShort(nodeInstance, offset, (short) 0);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
184 } else if (fieldClass == char.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
185 unsafe.putChar(nodeInstance, offset, (char) 0);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
186 } else if (fieldClass == boolean.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
187 unsafe.putBoolean(nodeInstance, offset, false);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
188 } else {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
189 unsafe.putObject(nodeInstance, offset, null);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
190 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
191 } else {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
192 if (fieldClass == int.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
193 unsafe.putInt(nodeInstance, offset, cp.getInt(cpi));
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
194 } else if (fieldClass == long.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
195 unsafe.putLong(nodeInstance, offset, cp.getLong(cpi));
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
196 } else if (fieldClass == float.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
197 unsafe.putFloat(nodeInstance, offset, cp.getFloat(cpi));
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
198 } else if (fieldClass == double.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
199 unsafe.putDouble(nodeInstance, offset, cp.getDouble(cpi));
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
200 } else if (fieldClass == byte.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
201 unsafe.putByte(nodeInstance, offset, (byte) cp.getInt(cpi));
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
202 } else if (fieldClass == short.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
203 unsafe.putShort(nodeInstance, offset, (short) cp.getInt(cpi));
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
204 } else if (fieldClass == char.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
205 unsafe.putChar(nodeInstance, offset, (char) cp.getInt(cpi));
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
206 } else if (fieldClass == boolean.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
207 unsafe.putBoolean(nodeInstance, offset, cp.getInt(cpi) == 1 ? true : false);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
208 } else if (fieldClass == Integer.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
209 unsafe.putObject(nodeInstance, offset, cp.getInt(cpi));
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
210 } else if (fieldClass == Long.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
211 unsafe.putObject(nodeInstance, offset, cp.getLong(cpi));
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
212 } else if (fieldClass == Float.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
213 unsafe.putObject(nodeInstance, offset, cp.getFloat(cpi));
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
214 } else if (fieldClass == Double.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
215 unsafe.putObject(nodeInstance, offset, cp.getDouble(cpi));
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
216 } else if (fieldClass == Byte.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
217 unsafe.putObject(nodeInstance, offset, (byte) cp.getInt(cpi));
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
218 } else if (fieldClass == Short.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
219 unsafe.putObject(nodeInstance, offset, (short) cp.getInt(cpi));
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
220 } else if (fieldClass == Character.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
221 unsafe.putObject(nodeInstance, offset, (char) cp.getInt(cpi));
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
222 } else if (fieldClass == Boolean.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
223 unsafe.putObject(nodeInstance, offset, cp.getInt(cpi) == 1 ? Boolean.TRUE : Boolean.FALSE);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
224 } else {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
225 unsafe.putObject(nodeInstance, offset, cp.getObject(fieldClass, cpi));
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
226 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
227 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
228 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
229 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
230 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
231
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
232 private void deserializeChildFields(Node parent, NodeField[] nodeFields) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
233 for (int i = nodeFields.length - 1; i >= 0; i--) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
234 NodeField field = nodeFields[i];
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
235 if (field.getKind() == NodeFieldKind.CHILD) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
236 unsafe.putObject(parent, field.getOffset(), popNode(parent, field.getType()));
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
237 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
238 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
239 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
240
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
241 private void deserializeChildrenFields(Node parent, NodeField[] nodeFields) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
242 for (int i = nodeFields.length - 1; i >= 0; i--) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
243 NodeField field = nodeFields[i];
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
244 if (field.getKind() == NodeFieldKind.CHILDREN) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
245 unsafe.putObject(parent, field.getOffset(), popArray(parent, field.getType()));
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 }
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 private static Node updateParent(Node parent, Node child) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
251 if (child != null) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
252 long parentOffset = NodeClass.get(child.getClass()).getParentOffset();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
253 unsafe.putObject(child, parentOffset, parent);
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 return child;
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 Unsafe loadUnsafe() {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
259 try {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
260 return Unsafe.getUnsafe();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
261 } catch (SecurityException e) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
262 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
263 try {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
264 Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe");
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
265 theUnsafeInstance.setAccessible(true);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
266 return (Unsafe) theUnsafeInstance.get(Unsafe.class);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
267 } catch (Exception e) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
268 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
269 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
270 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
271
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
272 private static class HierarchicalStack {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
273
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
274 private static final Object NULL_STACK = new Object();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
275
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
276 private final List<Object> dynamicStack = new ArrayList<>();
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 void pushStack(Object[] array) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
279 if (array == null) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
280 dynamicStack.add(NULL_STACK);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
281 } else {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
282 dynamicStack.add(new FixedSizeNodeStack(array));
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
283 }
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
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
286 private FixedSizeNodeStack getTosStack() {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
287 if (dynamicStack.isEmpty()) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
288 return null;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
289 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
290 Object peekTos = dynamicStack.get(dynamicStack.size() - 1);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
291 if (peekTos != null && peekTos.getClass() == FixedSizeNodeStack.class) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
292 return (FixedSizeNodeStack) peekTos;
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 return null;
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
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
297 Object[] popStack() {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
298 Object tos = dynamicStack.remove(dynamicStack.size() - 1);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
299 if (tos == NULL_STACK) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
300 return null;
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 return ((FixedSizeNodeStack) tos).getArray();
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
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
305 void push(Object o) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
306 FixedSizeNodeStack tosStack = getTosStack();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
307 if (tosStack != null && !tosStack.isFull()) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
308 tosStack.push(o);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
309 } else {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
310 dynamicStack.add(o);
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 }
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 pop() {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
315 FixedSizeNodeStack tosStack = getTosStack();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
316 Object value;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
317 if (tosStack != null) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
318 assert !tosStack.isEmpty();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
319 value = tosStack.pop();
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
320 } else {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
321 value = dynamicStack.remove(dynamicStack.size() - 1);
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
322 }
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
323 assert value != NULL_STACK;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
324 return value;
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
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 private static class FixedSizeNodeStack {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
330
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
331 private final Object[] array;
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 private int tos;
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 FixedSizeNodeStack(Object[] array) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
336 this.array = array;
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
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
339 boolean isFull() {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
340 return tos == array.length;
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
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
343 boolean isEmpty() {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
344 return tos == 0;
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
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
347 private void push(Object node) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
348 if (tos >= array.length) {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
349 throw new ArrayIndexOutOfBoundsException();
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 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
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 Object pop() {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
355 if (tos <= 0) {
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 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
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[] getArray() {
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
362 return array;
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
363 }
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
96c1d057a5ed Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
366 }