comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/PostOrderSerializer.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 915ebb306fcc
comparison
equal deleted inserted replaced
12387:aff825fef0fd 12388:96c1d057a5ed
1 /*
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package com.oracle.truffle.api.nodes.serial;
26
27 import java.lang.reflect.*;
28 import java.nio.*;
29
30 import sun.misc.*;
31
32 import com.oracle.truffle.api.*;
33 import com.oracle.truffle.api.nodes.*;
34 import com.oracle.truffle.api.nodes.NodeUtil.NodeClass;
35 import com.oracle.truffle.api.nodes.NodeUtil.NodeField;
36 import com.oracle.truffle.api.nodes.NodeUtil.NodeFieldKind;
37
38 /**
39 * Experimental API. May change without notice.
40 */
41 public final class PostOrderSerializer {
42
43 private static final Unsafe unsafe = loadUnsafe();
44
45 private final SerializerConstantPool cp;
46
47 /**
48 * Constructs a new deserializer using a custom {@link SerializerConstantPool} implementation.
49 * For the {@link SerializerConstantPool} implementation at least the following methods must be
50 * implemented:
51 * <ul>
52 * <li>{@link SerializerConstantPool#putInt(int)}</li>
53 * <li>{@link SerializerConstantPool#putClass(Class)}</li>
54 * </ul>
55 */
56 public PostOrderSerializer(SerializerConstantPool cp) {
57 this.cp = cp;
58 }
59
60 /**
61 * Serializes the node AST and returns the serialized data as byte array.
62 *
63 * @param node the root node that represents the Truffle AST that should be serialized.
64 * @return a trimmed byte array that contains the serialized data.
65 *
66 * @throws UnsupportedConstantPoolTypeException thrown if a type is encountered that is not
67 * supported by the constant pool implementation.
68 */
69 public byte[] serialize(Node node) throws UnsupportedConstantPoolTypeException {
70 VariableLengthIntBuffer buffer = new VariableLengthIntBuffer(ByteBuffer.allocate(512));
71 serialize(buffer, node);
72 return buffer.getBytes();
73 }
74
75 private void serialize(VariableLengthIntBuffer buffer, Node node) throws UnsupportedConstantPoolTypeException {
76 if (node == null) {
77 buffer.put(VariableLengthIntBuffer.NULL);
78 return;
79 }
80 Class<? extends Node> nodeClass = node.getClass();
81
82 NodeField[] nodeFields = NodeClass.get(nodeClass).getFields();
83 serializeChildFields(buffer, node, nodeFields);
84 serializeChildrenFields(buffer, node, nodeFields);
85 buffer.put(cp.putClass(node.getClass()));
86 serializeDataFields(buffer, node, nodeFields);
87 }
88
89 private void serializeDataFields(VariableLengthIntBuffer buffer, Node node, NodeField[] nodeFields) throws UnsupportedConstantPoolTypeException {
90 for (int i = 0; i < nodeFields.length; i++) {
91 NodeField field = nodeFields[i];
92 if (field.getKind() == NodeFieldKind.DATA) {
93 Class<?> fieldClass = field.getType();
94 long offset = field.getOffset();
95 int cpi;
96
97 if (field.getType().isAssignableFrom(SourceSection.class)) {
98 continue;
99 }
100
101 if (fieldClass == int.class) {
102 cpi = cp.putInt(unsafe.getInt(node, offset));
103 } else if (fieldClass == long.class) {
104 cpi = cp.putLong(unsafe.getLong(node, offset));
105 } else if (fieldClass == float.class) {
106 cpi = cp.putFloat(unsafe.getFloat(node, offset));
107 } else if (fieldClass == double.class) {
108 cpi = cp.putDouble(unsafe.getDouble(node, offset));
109 } else if (fieldClass == byte.class) {
110 cpi = cp.putInt(unsafe.getByte(node, offset));
111 } else if (fieldClass == short.class) {
112 cpi = cp.putInt(unsafe.getShort(node, offset));
113 } else if (fieldClass == char.class) {
114 cpi = cp.putInt(unsafe.getChar(node, offset));
115 } else if (fieldClass == boolean.class) {
116 cpi = cp.putInt(unsafe.getBoolean(node, offset) ? 1 : 0);
117 } else {
118 Object value = unsafe.getObject(node, offset);
119 if (value == null) {
120 cpi = VariableLengthIntBuffer.NULL;
121 } else if (fieldClass == Integer.class) {
122 cpi = cp.putInt((Integer) value);
123 } else if (fieldClass == Long.class) {
124 cpi = cp.putLong((Long) value);
125 } else if (fieldClass == Float.class) {
126 cpi = cp.putFloat((Float) value);
127 } else if (fieldClass == Double.class) {
128 cpi = cp.putDouble((Double) value);
129 } else if (fieldClass == Byte.class) {
130 cpi = cp.putInt((Byte) value);
131 } else if (fieldClass == Short.class) {
132 cpi = cp.putInt((Short) value);
133 } else if (fieldClass == Character.class) {
134 cpi = cp.putInt((Character) value);
135 } else if (fieldClass == Boolean.class) {
136 cpi = cp.putInt((Boolean) value ? 1 : 0);
137 } else {
138 cpi = cp.putObject(fieldClass, value);
139 }
140 }
141
142 buffer.put(cpi);
143 }
144 }
145 }
146
147 private void serializeChildrenFields(VariableLengthIntBuffer buffer, Node nodeInstance, NodeField[] nodeFields) throws UnsupportedConstantPoolTypeException {
148 for (int i = 0; i < nodeFields.length; i++) {
149 NodeField field = nodeFields[i];
150 if (field.getKind() == NodeFieldKind.CHILDREN) {
151 Object childArrayObject = unsafe.getObject(nodeInstance, field.getOffset());
152 if (childArrayObject != null && !(childArrayObject instanceof Node[])) {
153 throw new AssertionError("Node children must be instanceof Node[]");
154 }
155
156 buffer.put(cp.putClass(field.getType()));
157
158 Node[] childArray = (Node[]) childArrayObject;
159 if (childArray == null) {
160 buffer.put(VariableLengthIntBuffer.NULL);
161 } else {
162 buffer.put(cp.putInt(childArray.length));
163
164 for (int j = 0; j < childArray.length; j++) {
165 serialize(buffer, childArray[j]);
166 }
167 }
168 }
169 }
170 }
171
172 private void serializeChildFields(VariableLengthIntBuffer buffer, Node nodeInstance, NodeField[] nodeFields) throws UnsupportedConstantPoolTypeException {
173 for (int i = 0; i < nodeFields.length; i++) {
174 NodeField field = nodeFields[i];
175 if (field.getKind() == NodeFieldKind.CHILD) {
176 Object childObject = unsafe.getObject(nodeInstance, field.getOffset());
177 if (childObject != null && !(childObject instanceof Node)) {
178 throw new AssertionError("Node children must be instanceof Node");
179 }
180 serialize(buffer, (Node) childObject);
181 }
182 }
183 }
184
185 private static Unsafe loadUnsafe() {
186 try {
187 return Unsafe.getUnsafe();
188 } catch (SecurityException e) {
189 }
190 try {
191 Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe");
192 theUnsafeInstance.setAccessible(true);
193 return (Unsafe) theUnsafeInstance.get(Unsafe.class);
194 } catch (Exception e) {
195 throw new RuntimeException("exception while trying to get Unsafe.theUnsafe via reflection:", e);
196 }
197 }
198
199 }