Mercurial > hg > graal-jvmci-8
annotate graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/PostOrderSerializer.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 | 45fff0246a43 |
children |
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.nio.*; |
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:
16542
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:
12388
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 PostOrderSerializer { |
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 /** |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
46 * Constructs a new deserializer using a custom {@link SerializerConstantPool} implementation. |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
47 * For 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
|
48 * implemented: |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
49 * <ul> |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
50 * <li>{@link SerializerConstantPool#putInt(int)}</li> |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
51 * <li>{@link SerializerConstantPool#putClass(Class)}</li> |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
52 * </ul> |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
53 */ |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
54 public PostOrderSerializer(SerializerConstantPool cp) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
55 this.cp = cp; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
56 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
57 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
58 /** |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
59 * Serializes the node AST and returns the serialized data as byte array. |
16067
915ebb306fcc
Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
12388
diff
changeset
|
60 * |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
61 * @param node the root node that represents the Truffle AST that should be serialized. |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
62 * @return a trimmed byte array that contains the serialized data. |
16067
915ebb306fcc
Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
12388
diff
changeset
|
63 * |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
64 * @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
|
65 * supported by the constant pool implementation. |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
66 */ |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
67 public byte[] serialize(Node node) throws UnsupportedConstantPoolTypeException { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
68 VariableLengthIntBuffer buffer = new VariableLengthIntBuffer(ByteBuffer.allocate(512)); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
69 serialize(buffer, node); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
70 return buffer.getBytes(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
71 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
72 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
73 private void serialize(VariableLengthIntBuffer buffer, Node node) throws UnsupportedConstantPoolTypeException { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
74 if (node == null) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
75 buffer.put(VariableLengthIntBuffer.NULL); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
76 return; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
77 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
78 Class<? extends Node> nodeClass = node.getClass(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
79 |
20129
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
16542
diff
changeset
|
80 NodeFieldAccessor[] nodeFields = NodeClass.get(nodeClass).getFields(); |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
81 serializeChildFields(buffer, node, nodeFields); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
82 serializeChildrenFields(buffer, node, nodeFields); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
83 buffer.put(cp.putClass(node.getClass())); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
84 serializeDataFields(buffer, node, nodeFields); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
85 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
86 |
20129
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
16542
diff
changeset
|
87 private void serializeDataFields(VariableLengthIntBuffer buffer, Node node, NodeFieldAccessor[] nodeFields) throws UnsupportedConstantPoolTypeException { |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
88 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:
16542
diff
changeset
|
89 NodeFieldAccessor field = nodeFields[i]; |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
90 if (field.getKind() == NodeFieldKind.DATA) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
91 Class<?> fieldClass = field.getType(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
92 long offset = field.getOffset(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
93 int cpi; |
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 if (field.getType().isAssignableFrom(SourceSection.class)) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
96 continue; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
97 } |
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 if (fieldClass == int.class) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
100 cpi = cp.putInt(unsafe.getInt(node, offset)); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
101 } else if (fieldClass == long.class) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
102 cpi = cp.putLong(unsafe.getLong(node, offset)); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
103 } else if (fieldClass == float.class) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
104 cpi = cp.putFloat(unsafe.getFloat(node, offset)); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
105 } else if (fieldClass == double.class) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
106 cpi = cp.putDouble(unsafe.getDouble(node, offset)); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
107 } else if (fieldClass == byte.class) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
108 cpi = cp.putInt(unsafe.getByte(node, offset)); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
109 } else if (fieldClass == short.class) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
110 cpi = cp.putInt(unsafe.getShort(node, offset)); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
111 } else if (fieldClass == char.class) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
112 cpi = cp.putInt(unsafe.getChar(node, offset)); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
113 } else if (fieldClass == boolean.class) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
114 cpi = cp.putInt(unsafe.getBoolean(node, offset) ? 1 : 0); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
115 } else { |
16542
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
116 cpi = serializeDataFieldsObject(node, fieldClass, offset); |
12388
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 buffer.put(cpi); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
120 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
121 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
122 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
123 |
16542
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
124 private int serializeDataFieldsObject(Node node, Class<?> fieldClass, long offset) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
125 Object value = unsafe.getObject(node, offset); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
126 if (value == null) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
127 return VariableLengthIntBuffer.NULL; |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
128 } else if (fieldClass == Integer.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
129 return cp.putInt((Integer) value); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
130 } else if (fieldClass == Long.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
131 return cp.putLong((Long) value); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
132 } else if (fieldClass == Float.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
133 return cp.putFloat((Float) value); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
134 } else if (fieldClass == Double.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
135 return cp.putDouble((Double) value); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
136 } else if (fieldClass == Byte.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
137 return cp.putInt((Byte) value); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
138 } else if (fieldClass == Short.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
139 return cp.putInt((Short) value); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
140 } else if (fieldClass == Character.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
141 return cp.putInt((Character) value); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
142 } else if (fieldClass == Boolean.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
143 return cp.putInt((Boolean) value ? 1 : 0); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
144 } else { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
145 return cp.putObject(fieldClass, value); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
146 } |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
147 } |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
148 |
20129
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
16542
diff
changeset
|
149 private void serializeChildrenFields(VariableLengthIntBuffer buffer, Node nodeInstance, NodeFieldAccessor[] nodeFields) throws UnsupportedConstantPoolTypeException { |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
150 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:
16542
diff
changeset
|
151 NodeFieldAccessor field = nodeFields[i]; |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
152 if (field.getKind() == NodeFieldKind.CHILDREN) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
153 Object childArrayObject = unsafe.getObject(nodeInstance, field.getOffset()); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
154 if (childArrayObject != null && !(childArrayObject instanceof Node[])) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
155 throw new AssertionError("Node children must be instanceof Node[]"); |
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 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
158 buffer.put(cp.putClass(field.getType())); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
159 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
160 Node[] childArray = (Node[]) childArrayObject; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
161 if (childArray == null) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
162 buffer.put(VariableLengthIntBuffer.NULL); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
163 } else { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
164 buffer.put(cp.putInt(childArray.length)); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
165 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
166 for (int j = 0; j < childArray.length; j++) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
167 serialize(buffer, childArray[j]); |
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 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
171 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
172 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
173 |
20129
5b7db8941fd7
Truffle: make NodeClass and NodeField a top-level class.
Christian Humer <christian.humer@gmail.com>
parents:
16542
diff
changeset
|
174 private void serializeChildFields(VariableLengthIntBuffer buffer, Node nodeInstance, NodeFieldAccessor[] nodeFields) throws UnsupportedConstantPoolTypeException { |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
175 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:
16542
diff
changeset
|
176 NodeFieldAccessor field = nodeFields[i]; |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
177 if (field.getKind() == NodeFieldKind.CHILD) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
178 Object childObject = unsafe.getObject(nodeInstance, field.getOffset()); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
179 if (childObject != null && !(childObject instanceof Node)) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
180 throw new AssertionError("Node children must be instanceof Node"); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
181 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
182 serialize(buffer, (Node) childObject); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
183 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
184 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
185 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
186 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
187 private static Unsafe loadUnsafe() { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
188 try { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
189 return Unsafe.getUnsafe(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
190 } catch (SecurityException e) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
191 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
192 try { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
193 Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe"); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
194 theUnsafeInstance.setAccessible(true); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
195 return (Unsafe) theUnsafeInstance.get(Unsafe.class); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
196 } catch (Exception e) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
197 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
|
198 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
199 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
200 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
201 } |