Mercurial > hg > truffle
annotate graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/PostOrderSerializer.java @ 16542:45fff0246a43
extract methods in (de)serializer
author | Christian Wirth <christian.wirth@oracle.com> |
---|---|
date | Thu, 17 Jul 2014 11:13:31 +0200 |
parents | 915ebb306fcc |
children | 5b7db8941fd7 |
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.*; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
33 import com.oracle.truffle.api.nodes.NodeUtil.NodeClass; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
34 import com.oracle.truffle.api.nodes.NodeUtil.NodeField; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
35 import com.oracle.truffle.api.nodes.NodeUtil.NodeFieldKind; |
16067
915ebb306fcc
Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
12388
diff
changeset
|
36 import com.oracle.truffle.api.source.*; |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
37 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
38 /** |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
39 * Experimental API. May change without notice. |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
40 */ |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
41 public final class PostOrderSerializer { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
42 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
43 private static final Unsafe unsafe = loadUnsafe(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
44 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
45 private final SerializerConstantPool cp; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
46 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
47 /** |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
48 * 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
|
49 * 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
|
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#putInt(int)}</li> |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
53 * <li>{@link SerializerConstantPool#putClass(Class)}</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 PostOrderSerializer(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 * 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
|
62 * |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
63 * @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
|
64 * @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
|
65 * |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
66 * @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
|
67 * supported by the constant pool implementation. |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
68 */ |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
69 public byte[] serialize(Node node) throws UnsupportedConstantPoolTypeException { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
70 VariableLengthIntBuffer buffer = new VariableLengthIntBuffer(ByteBuffer.allocate(512)); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
71 serialize(buffer, node); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
72 return buffer.getBytes(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
73 } |
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 private void serialize(VariableLengthIntBuffer buffer, Node node) throws UnsupportedConstantPoolTypeException { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
76 if (node == null) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
77 buffer.put(VariableLengthIntBuffer.NULL); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
78 return; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
79 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
80 Class<? extends Node> nodeClass = node.getClass(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
81 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
82 NodeField[] nodeFields = NodeClass.get(nodeClass).getFields(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
83 serializeChildFields(buffer, node, nodeFields); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
84 serializeChildrenFields(buffer, node, nodeFields); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
85 buffer.put(cp.putClass(node.getClass())); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
86 serializeDataFields(buffer, node, nodeFields); |
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 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
89 private void serializeDataFields(VariableLengthIntBuffer buffer, Node node, NodeField[] nodeFields) throws UnsupportedConstantPoolTypeException { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
90 for (int i = 0; i < nodeFields.length; i++) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
91 NodeField field = nodeFields[i]; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
92 if (field.getKind() == NodeFieldKind.DATA) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
93 Class<?> fieldClass = field.getType(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
94 long offset = field.getOffset(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
95 int cpi; |
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 if (field.getType().isAssignableFrom(SourceSection.class)) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
98 continue; |
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 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
101 if (fieldClass == int.class) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
102 cpi = cp.putInt(unsafe.getInt(node, offset)); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
103 } else if (fieldClass == long.class) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
104 cpi = cp.putLong(unsafe.getLong(node, offset)); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
105 } else if (fieldClass == float.class) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
106 cpi = cp.putFloat(unsafe.getFloat(node, offset)); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
107 } else if (fieldClass == double.class) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
108 cpi = cp.putDouble(unsafe.getDouble(node, offset)); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
109 } else if (fieldClass == byte.class) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
110 cpi = cp.putInt(unsafe.getByte(node, offset)); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
111 } else if (fieldClass == short.class) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
112 cpi = cp.putInt(unsafe.getShort(node, offset)); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
113 } else if (fieldClass == char.class) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
114 cpi = cp.putInt(unsafe.getChar(node, offset)); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
115 } else if (fieldClass == boolean.class) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
116 cpi = cp.putInt(unsafe.getBoolean(node, offset) ? 1 : 0); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
117 } else { |
16542
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
118 cpi = serializeDataFieldsObject(node, fieldClass, offset); |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
119 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
120 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
121 buffer.put(cpi); |
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 } |
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 |
16542
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
126 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
|
127 Object value = unsafe.getObject(node, offset); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
128 if (value == null) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
129 return VariableLengthIntBuffer.NULL; |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
130 } else if (fieldClass == Integer.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
131 return cp.putInt((Integer) value); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
132 } else if (fieldClass == Long.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
133 return cp.putLong((Long) value); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
134 } else if (fieldClass == Float.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
135 return cp.putFloat((Float) value); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
136 } else if (fieldClass == Double.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
137 return cp.putDouble((Double) value); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
138 } else if (fieldClass == Byte.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
139 return cp.putInt((Byte) value); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
140 } else if (fieldClass == Short.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
141 return cp.putInt((Short) value); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
142 } else if (fieldClass == Character.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
143 return cp.putInt((Character) value); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
144 } else if (fieldClass == Boolean.class) { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
145 return cp.putInt((Boolean) value ? 1 : 0); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
146 } else { |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
147 return cp.putObject(fieldClass, value); |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
148 } |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
149 } |
45fff0246a43
extract methods in (de)serializer
Christian Wirth <christian.wirth@oracle.com>
parents:
16067
diff
changeset
|
150 |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
151 private void serializeChildrenFields(VariableLengthIntBuffer buffer, Node nodeInstance, NodeField[] nodeFields) throws UnsupportedConstantPoolTypeException { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
152 for (int i = 0; i < nodeFields.length; i++) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
153 NodeField field = nodeFields[i]; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
154 if (field.getKind() == NodeFieldKind.CHILDREN) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
155 Object childArrayObject = unsafe.getObject(nodeInstance, field.getOffset()); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
156 if (childArrayObject != null && !(childArrayObject instanceof Node[])) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
157 throw new AssertionError("Node children must be instanceof Node[]"); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
158 } |
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 buffer.put(cp.putClass(field.getType())); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
161 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
162 Node[] childArray = (Node[]) childArrayObject; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
163 if (childArray == null) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
164 buffer.put(VariableLengthIntBuffer.NULL); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
165 } else { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
166 buffer.put(cp.putInt(childArray.length)); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
167 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
168 for (int j = 0; j < childArray.length; j++) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
169 serialize(buffer, childArray[j]); |
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 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
174 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
175 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
176 private void serializeChildFields(VariableLengthIntBuffer buffer, Node nodeInstance, NodeField[] nodeFields) throws UnsupportedConstantPoolTypeException { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
177 for (int i = 0; i < nodeFields.length; i++) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
178 NodeField field = nodeFields[i]; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
179 if (field.getKind() == NodeFieldKind.CHILD) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
180 Object childObject = unsafe.getObject(nodeInstance, field.getOffset()); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
181 if (childObject != null && !(childObject instanceof Node)) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
182 throw new AssertionError("Node children must be instanceof Node"); |
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 serialize(buffer, (Node) childObject); |
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 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
188 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
189 private static Unsafe loadUnsafe() { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
190 try { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
191 return Unsafe.getUnsafe(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
192 } catch (SecurityException e) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
193 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
194 try { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
195 Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe"); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
196 theUnsafeInstance.setAccessible(true); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
197 return (Unsafe) theUnsafeInstance.get(Unsafe.class); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
198 } catch (Exception e) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
199 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
|
200 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
201 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
202 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
203 } |