changeset 12388:96c1d057a5ed

Truffle: Added experimental serialization API.
author Christian Humer <christian.humer@gmail.com>
date Wed, 02 Oct 2013 15:33:08 +0200
parents aff825fef0fd
children 9d1a5d61cc11
files graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/PostOrderDeserializerTest.java graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/PostOrderSerializerTest.java graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/TestNodes.java graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/TestSerializerConstantPool.java graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/VariableLengthIntBufferTest.java graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/PostOrderDeserializer.java graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/PostOrderSerializer.java graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/SerializerConstantPool.java graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/UnsupportedConstantPoolTypeException.java graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/VariableLengthIntBuffer.java
diffstat 10 files changed, 1648 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/PostOrderDeserializerTest.java	Wed Oct 02 15:33:08 2013 +0200
@@ -0,0 +1,184 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.test.nodes.serial;
+
+import java.nio.*;
+import java.util.*;
+
+import org.junit.*;
+
+import com.oracle.truffle.api.nodes.*;
+import com.oracle.truffle.api.nodes.serial.*;
+import com.oracle.truffle.api.test.nodes.serial.TestNodes.EmptyNode;
+import com.oracle.truffle.api.test.nodes.serial.TestNodes.NodeWithArray;
+import com.oracle.truffle.api.test.nodes.serial.TestNodes.NodeWithFields;
+import com.oracle.truffle.api.test.nodes.serial.TestNodes.NodeWithOneChild;
+import com.oracle.truffle.api.test.nodes.serial.TestNodes.NodeWithThreeChilds;
+import com.oracle.truffle.api.test.nodes.serial.TestNodes.NodeWithTwoArray;
+import com.oracle.truffle.api.test.nodes.serial.TestNodes.NodeWithTwoChilds;
+import com.oracle.truffle.api.test.nodes.serial.TestNodes.StringNode;
+
+public class PostOrderDeserializerTest {
+
+    private PostOrderDeserializer d;
+    private TestSerializerConstantPool cp;
+
+    @Before
+    public void setUp() {
+        cp = new TestSerializerConstantPool();
+        d = new PostOrderDeserializer(cp);
+    }
+
+    @After
+    public void tearDown() {
+        d = null;
+        cp = null;
+    }
+
+    private Node deserialize(byte[] bytes) {
+        return d.deserialize(bytes, Node.class);
+    }
+
+    @Test
+    public void testNull() {
+        createCP();
+        Node ast = deserialize(createBytes(VariableLengthIntBuffer.NULL));
+        Assert.assertNull(ast);
+    }
+
+    @Test
+    public void testSingleNode() {
+        createCP(EmptyNode.class);
+        Node expectedAst = new EmptyNode();
+        Node ast = deserialize(createBytes(0));
+        assertAST(expectedAst, ast);
+    }
+
+    @Test
+    public void testThreeChilds() {
+        createCP(EmptyNode.class, NodeWithThreeChilds.class);
+        Node expectedAst = new NodeWithThreeChilds(new EmptyNode(), null, new EmptyNode());
+        Node ast = deserialize(createBytes(0, VariableLengthIntBuffer.NULL, 0, 1));
+        assertAST(expectedAst, ast);
+    }
+
+    @Test
+    public void testFields() {
+        createCP(NodeWithFields.class, "test", Integer.MIN_VALUE, Integer.MAX_VALUE, Long.MIN_VALUE, Long.MAX_VALUE, Float.MIN_VALUE, Float.MAX_VALUE, Double.MIN_VALUE, Double.MAX_VALUE,
+                        (int) Character.MIN_VALUE, (int) Character.MAX_VALUE, (int) Short.MIN_VALUE, (int) Short.MAX_VALUE, (int) Byte.MIN_VALUE, (int) Byte.MAX_VALUE, 1);
+        NodeWithFields expectedAst = new NodeWithFields("test", Integer.MIN_VALUE, Integer.MAX_VALUE, Long.MIN_VALUE, Long.MAX_VALUE, Float.MIN_VALUE, Float.MAX_VALUE, Double.MIN_VALUE,
+                        Double.MAX_VALUE, Character.MIN_VALUE, Character.MAX_VALUE, Short.MIN_VALUE, Short.MAX_VALUE, Byte.MIN_VALUE, Byte.MAX_VALUE, Boolean.TRUE, Boolean.FALSE);
+        Node ast = deserialize(createBytes(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 10));
+        assertAST(expectedAst, ast);
+    }
+
+    @Test
+    public void testFieldsNull() {
+        createCP(NodeWithFields.class, "test", 0, 0L, 0.0F, 0.0D);
+        NodeWithFields expectedAst = new NodeWithFields("test", 0, null, 0L, null, 0f, null, 0d, null, (char) 0, null, (short) 0, null, (byte) 0, null, false, null);
+        int nil = VariableLengthIntBuffer.NULL;
+        Node ast = deserialize(createBytes(0, 1, 2, nil, 3, nil, 4, nil, 5, nil, 2, nil, 2, nil, 2, nil, 2, nil));
+        assertAST(expectedAst, ast);
+    }
+
+    @Test
+    public void testNullChilds() {
+        createCP(Node[].class, NodeWithArray.class);
+        Node expectedAst = new NodeWithArray(null);
+        Node ast = deserialize(createBytes(0, VariableLengthIntBuffer.NULL, 1));
+        assertAST(expectedAst, ast);
+    }
+
+    @Test
+    public void testNChilds() {
+        Node expectedAst = new NodeWithArray(new Node[]{new EmptyNode(), new NodeWithArray(new Node[]{new EmptyNode(), new EmptyNode(), new EmptyNode()}), new EmptyNode(), new EmptyNode()});
+        createCP(Node[].class, 4, EmptyNode.class, 3, NodeWithArray.class);
+        Node ast = deserialize(createBytes(0, 1, 2, 0, 3, 2, 2, 2, 4, 2, 2, 4));
+
+        assertAST(expectedAst, ast);
+    }
+
+    @Test
+    public void test2xNChilds() {
+        Node expectedAst = new NodeWithTwoArray(new Node[]{new StringNode("a0"), new StringNode("a1")}, new Node[]{new StringNode("b0"), new StringNode("b1"), new StringNode("b2")});
+        createCP(Node[].class, 2, StringNode.class, "a0", "a1", 3, "b0", "b1", "b2", NodeWithTwoArray.class);
+        Node ast = deserialize(createBytes(0, 1, 2, 3, 2, 4, 0, 5, 2, 6, 2, 7, 2, 8, 9));
+
+        assertAST(expectedAst, ast);
+    }
+
+    @Test
+    public void testBug0() {
+        Node expectedAst = new NodeWithArray(new Node[]{new NodeWithOneChild(new EmptyNode())});
+
+        createCP(Node[].class, 1, EmptyNode.class, NodeWithOneChild.class, NodeWithArray.class);
+        Node ast = deserialize(createBytes(0, 1, 2, 3, 4));
+        assertAST(expectedAst, ast);
+    }
+
+    @Test
+    public void testBug1() {
+        Node expectedAst = new NodeWithArray(new Node[]{new NodeWithTwoChilds(new EmptyNode(), new EmptyNode())});
+
+        createCP(Node[].class, 1, EmptyNode.class, NodeWithTwoChilds.class, NodeWithArray.class);
+        Node ast = deserialize(createBytes(0, 1, 2, 2, 3, 4));
+        assertAST(expectedAst, ast);
+    }
+
+    private static void assertAST(Node expectedAst, Node actualAst) {
+        if (expectedAst == null) {
+            Assert.assertNull(actualAst);
+            return;
+        }
+
+        Assert.assertNotNull(actualAst);
+        // fields are asserted using the corresponding equals implementation
+        Assert.assertEquals(expectedAst, actualAst);
+
+        Iterable<Node> expectedChildIterator = expectedAst.getChildren();
+        Iterator<Node> actualChildIterator = actualAst.getChildren().iterator();
+        for (Node node : expectedChildIterator) {
+            Assert.assertTrue(actualChildIterator.hasNext());
+            assertAST(node, actualChildIterator.next());
+        }
+        Assert.assertFalse(actualChildIterator.hasNext());
+    }
+
+    private static byte[] createBytes(int... refs) {
+        VariableLengthIntBuffer buf = new VariableLengthIntBuffer(ByteBuffer.allocate(512));
+        for (int i = 0; i < refs.length; i++) {
+            buf.put(refs[i]);
+        }
+        return buf.getBytes();
+    }
+
+    private void createCP(Object... cpData) {
+        for (int i = 0; i < cpData.length; i++) {
+            Object object = cpData[i];
+
+            cp.putObject(object.getClass(), object);
+
+        }
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/PostOrderSerializerTest.java	Wed Oct 02 15:33:08 2013 +0200
@@ -0,0 +1,130 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.test.nodes.serial;
+
+import org.junit.*;
+
+import com.oracle.truffle.api.nodes.*;
+import com.oracle.truffle.api.nodes.serial.*;
+import com.oracle.truffle.api.test.nodes.serial.TestNodes.EmptyNode;
+import com.oracle.truffle.api.test.nodes.serial.TestNodes.NodeWithArray;
+import com.oracle.truffle.api.test.nodes.serial.TestNodes.NodeWithFields;
+import com.oracle.truffle.api.test.nodes.serial.TestNodes.NodeWithThreeChilds;
+import com.oracle.truffle.api.test.nodes.serial.TestNodes.NodeWithTwoArray;
+import com.oracle.truffle.api.test.nodes.serial.TestNodes.StringNode;
+
+public class PostOrderSerializerTest {
+
+    private PostOrderSerializer s;
+    private TestSerializerConstantPool cp;
+
+    @Before
+    public void setUp() {
+        cp = new TestSerializerConstantPool();
+        s = new PostOrderSerializer(cp);
+    }
+
+    @After
+    public void tearDown() {
+        cp = null;
+        s = null;
+    }
+
+    @Test
+    public void testNull() {
+        Node ast = null;
+        assertBytes(s.serialize(ast), VariableLengthIntBuffer.NULL);
+        assertCP();
+    }
+
+    @Test
+    public void testSingleEmptyNode() {
+        Node ast = new EmptyNode();
+        assertBytes(s.serialize(ast), 0);
+        assertCP(EmptyNode.class);
+    }
+
+    @Test
+    public void testThreeChilds() {
+        Node ast = new NodeWithThreeChilds(new EmptyNode(), null, new EmptyNode());
+        assertBytes(s.serialize(ast), 0, VariableLengthIntBuffer.NULL, 0, 1);
+        assertCP(EmptyNode.class, NodeWithThreeChilds.class);
+    }
+
+    @Test
+    public void testFields() {
+        NodeWithFields ast = new NodeWithFields("test", Integer.MIN_VALUE, Integer.MAX_VALUE, Long.MIN_VALUE, Long.MAX_VALUE, Float.MIN_VALUE, Float.MAX_VALUE, Double.MIN_VALUE, Double.MAX_VALUE,
+                        Character.MIN_VALUE, Character.MAX_VALUE, Short.MIN_VALUE, Short.MAX_VALUE, Byte.MIN_VALUE, Byte.MAX_VALUE, Boolean.TRUE, Boolean.FALSE);
+        assertBytes(s.serialize(ast), 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 10);
+        assertCP(NodeWithFields.class, "test", Integer.MIN_VALUE, Integer.MAX_VALUE, Long.MIN_VALUE, Long.MAX_VALUE, Float.MIN_VALUE, Float.MAX_VALUE, Double.MIN_VALUE, Double.MAX_VALUE,
+                        (int) Character.MIN_VALUE, (int) Character.MAX_VALUE, (int) Short.MIN_VALUE, (int) Short.MAX_VALUE, (int) Byte.MIN_VALUE, (int) Byte.MAX_VALUE, 1);
+    }
+
+    @Test
+    public void testFieldsNull() {
+        NodeWithFields ast = new NodeWithFields("test", 0, null, 0L, null, 0f, null, 0d, null, (char) 0, null, (short) 0, null, (byte) 0, null, false, null);
+        int nil = VariableLengthIntBuffer.NULL;
+        assertBytes(s.serialize(ast), 0, 1, 2, nil, 3, nil, 4, nil, 5, nil, 2, nil, 2, nil, 2, nil, 2, nil);
+        assertCP(NodeWithFields.class, "test", 0, 0L, 0.0F, 0.0D);
+    }
+
+    @Test
+    public void testNChilds() {
+        Node ast = new NodeWithArray(new Node[]{new EmptyNode(), new NodeWithArray(new Node[]{new EmptyNode(), new EmptyNode(), new EmptyNode()}), new EmptyNode(), new EmptyNode()});
+        assertBytes(s.serialize(ast), 0, 1, 2, 0, 3, 2, 2, 2, 4, 2, 2, 4);
+        assertCP(Node[].class, 4, EmptyNode.class, 3, NodeWithArray.class);
+    }
+
+    @Test
+    public void testNullChilds() {
+        Node ast = new NodeWithArray(null);
+        assertBytes(s.serialize(ast), 0, VariableLengthIntBuffer.NULL, 1);
+        assertCP(Node[].class, NodeWithArray.class);
+    }
+
+    @Test
+    public void test2xNChilds() {
+        Node ast = new NodeWithTwoArray(new Node[]{new StringNode("a0"), new StringNode("a1")}, new Node[]{new StringNode("b0"), new StringNode("b1"), new StringNode("b2")});
+        assertBytes(s.serialize(ast), 0, 1, 2, 3, 2, 4, 0, 5, 2, 6, 2, 7, 2, 8, 9);
+        assertCP(Node[].class, 2, StringNode.class, "a0", "a1", 3, "b0", "b1", "b2", NodeWithTwoArray.class);
+    }
+
+    private static void assertBytes(byte[] actualBytes, int... expectedIndexes) {
+        VariableLengthIntBuffer buf = new VariableLengthIntBuffer(actualBytes);
+        for (int i = 0; i < expectedIndexes.length; i++) {
+            Assert.assertTrue("Unexpected EOF " + i, buf.hasRemaining());
+            Assert.assertEquals("Index at pos " + i + ".", expectedIndexes[i], buf.get());
+        }
+        Assert.assertFalse(buf.hasRemaining());
+    }
+
+    private void assertCP(Object... object) {
+        for (int i = 0; i < object.length; i++) {
+            Object cpvalue = object[i];
+            Assert.assertNotNull("CP at index " + i, cpvalue);
+            Assert.assertEquals("CP at index " + i, cpvalue, cp.getObject(cpvalue.getClass(), i));
+        }
+        Assert.assertEquals(object.length, cp.getIndex());
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/TestNodes.java	Wed Oct 02 15:33:08 2013 +0200
@@ -0,0 +1,221 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.test.nodes.serial;
+
+import java.util.*;
+
+import com.oracle.truffle.api.nodes.*;
+
+final class TestNodes {
+
+    private TestNodes() {
+    }
+
+    static class StringNode extends Node {
+
+        private final String name;
+
+        public StringNode(String name) {
+            this.name = name;
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hash(name);
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (obj == null) {
+                return false;
+            } else if (obj.getClass() != getClass()) {
+                return false;
+            } else if ((((Node) obj).getParent() != null) && !((Node) obj).getParent().equals(getParent())) {
+                return false;
+            } else if (!Objects.equals(name, ((StringNode) obj).name)) {
+                return false;
+            }
+            return true;
+        }
+    }
+
+    static class EmptyNode extends Node {
+
+        @Override
+        public int hashCode() {
+            return super.hashCode();
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (obj == null) {
+                return false;
+            } else if (obj.getClass() != getClass()) {
+                return false;
+            } else if ((((Node) obj).getParent() != null) && !((Node) obj).getParent().equals(getParent())) {
+                return false;
+            }
+            return true;
+        }
+    }
+
+    static class NodeWithOneChild extends EmptyNode {
+
+        @Child Node child;
+
+        public NodeWithOneChild(Node child) {
+            this.child = adoptChild(child);
+        }
+
+    }
+
+    static class NodeWithTwoChilds extends EmptyNode {
+
+        @Child Node child1;
+        @Child Node child2;
+
+        public NodeWithTwoChilds(Node child1, Node child2) {
+            this.child1 = adoptChild(child1);
+            this.child2 = adoptChild(child2);
+        }
+
+    }
+
+    static class NodeWithThreeChilds extends EmptyNode {
+
+        @Child Node child1;
+        @Child Node child2;
+        @Child Node child3;
+
+        public NodeWithThreeChilds(Node child1, Node child2, Node child3) {
+            this.child1 = adoptChild(child1);
+            this.child2 = adoptChild(child2);
+            this.child3 = adoptChild(child3);
+        }
+
+    }
+
+    static class NodeWithArray extends EmptyNode {
+
+        @Children private final Node[] childNodes;
+
+        NodeWithArray(Node[] children) {
+            this.childNodes = adoptChildren(children);
+        }
+
+        Node[] getChildNodes() {
+            return childNodes;
+        }
+    }
+
+    static class NodeWithTwoArray extends EmptyNode {
+
+        @Children private final Node[] childNodes1;
+        @Children private final Node[] childNodes2;
+
+        NodeWithTwoArray(Node[] childs1, Node[] childs2) {
+            this.childNodes1 = adoptChildren(childs1);
+            this.childNodes2 = adoptChildren(childs2);
+        }
+
+        Node[] getChildNodes1() {
+            return childNodes1;
+        }
+
+        Node[] getChildNodes2() {
+            return childNodes2;
+        }
+    }
+
+    static class NodeWithFields extends EmptyNode {
+
+        String stringField;
+        int integerField;
+        Integer integerObjectField;
+        long longField;
+        Long longObjectField;
+        float floatField;
+        Float floatObjectField;
+        double doubleField;
+        Double doubleObjectField;
+        char charField;
+        Character charObjectField;
+        short shortField;
+        Short shortObjecField;
+        byte byteField;
+        Byte byteObjectField;
+        boolean booleanField;
+        Boolean booleanObjectfield;
+
+        public NodeWithFields(String stringField, int integerField, Integer integerObjectField, long longField, Long longObjectField, float floatField, Float floatObjectField, double doubleField,
+                        Double doubleObjectField, char charField, Character charObjectField, short shortField, Short shortObjecField, byte byteField, Byte byteObjectField, boolean booleanField,
+                        Boolean booleanObjectfield) {
+            this.stringField = stringField;
+            this.integerField = integerField;
+            this.integerObjectField = integerObjectField;
+            this.longField = longField;
+            this.longObjectField = longObjectField;
+            this.floatField = floatField;
+            this.floatObjectField = floatObjectField;
+            this.doubleField = doubleField;
+            this.doubleObjectField = doubleObjectField;
+            this.charField = charField;
+            this.charObjectField = charObjectField;
+            this.shortField = shortField;
+            this.shortObjecField = shortObjecField;
+            this.byteField = byteField;
+            this.byteObjectField = byteObjectField;
+            this.booleanField = booleanField;
+            this.booleanObjectfield = booleanObjectfield;
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hash(stringField, integerField, integerObjectField, longField, longObjectField, floatField, floatObjectField, doubleField, doubleObjectField, charField, charObjectField,
+                            shortField, shortObjecField, byteField, byteObjectField, booleanField, booleanObjectfield);
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj) {
+                return true;
+            }
+            if (obj.getClass() != getClass()) {
+                return false;
+            }
+            NodeWithFields o = (NodeWithFields) obj;
+            return Objects.deepEquals(fieldArray(), o.fieldArray());
+        }
+
+        private Object[] fieldArray() {
+            return array(stringField, integerField, integerObjectField, longField, longObjectField, floatField, floatObjectField, doubleField, doubleObjectField, charField, charObjectField,
+                            shortField, shortObjecField, byteField, byteObjectField, booleanField, booleanObjectfield);
+        }
+
+        private static Object[] array(Object... values) {
+            return values;
+        }
+
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/TestSerializerConstantPool.java	Wed Oct 02 15:33:08 2013 +0200
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.test.nodes.serial;
+
+import java.util.*;
+
+import com.oracle.truffle.api.nodes.serial.*;
+
+class TestSerializerConstantPool implements SerializerConstantPool {
+
+    private final Map<Integer, Object> int2object = new HashMap<>();
+    private final Map<Object, Integer> object2int = new HashMap<>();
+
+    private int index;
+
+    public void setIndex(int index) {
+        this.index = index;
+    }
+
+    public int getIndex() {
+        return index;
+    }
+
+    @Override
+    public double getDouble(int cpi) {
+        return (Double) int2object.get(cpi);
+    }
+
+    @Override
+    public float getFloat(int cpi) {
+        return (Float) int2object.get(cpi);
+    }
+
+    @Override
+    public Object getObject(Class<?> clazz, int cpi) throws UnsupportedConstantPoolTypeException {
+        return int2object.get(cpi);
+    }
+
+    @Override
+    public int putDouble(double value) {
+        return put(value);
+    }
+
+    public int putFloat(float value) {
+        return put(value);
+    }
+
+    public int putObject(java.lang.Class<?> clazz, Object value) throws UnsupportedConstantPoolTypeException {
+        return put(value);
+    }
+
+    @Override
+    public int putClass(Class<?> clazz) {
+        return put(clazz);
+    }
+
+    private int put(Object o) {
+        Integer currentIndex = object2int.get(o);
+        if (currentIndex == null) {
+            int2object.put(index, o);
+            object2int.put(o, index);
+            return index++;
+        } else {
+            return currentIndex;
+        }
+    }
+
+    @Override
+    public Class<?> getClass(int idx) {
+        return (Class<?>) int2object.get(idx);
+    }
+
+    @Override
+    public int putInt(int constant) {
+        return put(constant);
+    }
+
+    @Override
+    public int getInt(int idx) {
+        return (Integer) int2object.get(idx);
+    }
+
+    @Override
+    public long getLong(int idx) {
+        return (Long) int2object.get(idx);
+    }
+
+    @Override
+    public int putLong(long value) {
+        return put(value);
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/VariableLengthIntBufferTest.java	Wed Oct 02 15:33:08 2013 +0200
@@ -0,0 +1,124 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.test.nodes.serial;
+
+import java.nio.*;
+
+import org.junit.*;
+
+import com.oracle.truffle.api.nodes.serial.*;
+
+public class VariableLengthIntBufferTest {
+
+    private VariableLengthIntBuffer buf;
+
+    @Before
+    public void setUp() {
+        buf = new VariableLengthIntBuffer(ByteBuffer.allocate(512));
+    }
+
+    @After
+    public void tearDown() {
+        buf = null;
+    }
+
+    @Test
+    public void testPutNull() {
+        buf.put(VariableLengthIntBuffer.NULL);
+        assertBytes(0xFF);
+    }
+
+    @Test
+    public void testPutByteCornerCase0() {
+        buf.put(0x00); // 0
+        assertBytes(0x00);
+    }
+
+    @Test
+    public void testPutByteCornerCase1() {
+        buf.put(0x7F); // 127
+        assertBytes(0x7F);
+    }
+
+    @Test
+    public void testPutByteCornerCase2() {
+        buf.put(0x3FFF_FFFF);
+        assertBytes(0xBF, 0xFF, 0xFF, 0xFF);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testPutByteCornerCase3() {
+        buf.put(0x4000_0000); // out of encodeable
+    }
+
+    @Test
+    public void testGetNull() {
+        create(0xFF);
+        assertGet(VariableLengthIntBuffer.NULL);
+    }
+
+    @Test
+    public void testGetCornerCase0() {
+        create(0x00);
+        assertGet(0x00);
+    }
+
+    @Test
+    public void testGetCornerCase1() {
+        create(0x7F);
+        assertGet(0x7F);
+    }
+
+    @Test
+    public void testGetCornerCase2() {
+        create(0xBF, 0xFF, 0xFF, 0xFF);
+        assertGet(0x3FFF_FFFF);
+    }
+
+    @Test(expected = AssertionError.class)
+    public void testGetCornerCase3() {
+        create(0xFF, 0xFF, 0xFF, 0xFF);
+        assertGet(0x0);
+    }
+
+    private void create(int... bytes) {
+        byte[] convBytes = new byte[bytes.length];
+        for (int i = 0; i < bytes.length; i++) {
+            convBytes[i] = (byte) bytes[i];
+        }
+        buf = new VariableLengthIntBuffer(convBytes);
+    }
+
+    private void assertGet(int expected) {
+        Assert.assertEquals(expected, buf.get());
+    }
+
+    private void assertBytes(int... expectedBytes) {
+        byte[] actualBytes = buf.getBytes();
+        Assert.assertEquals(expectedBytes.length, actualBytes.length);
+        for (int i = 0; i < expectedBytes.length; i++) {
+            Assert.assertTrue(actualBytes[i] == (byte) expectedBytes[i]);
+        }
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/PostOrderDeserializer.java	Wed Oct 02 15:33:08 2013 +0200
@@ -0,0 +1,366 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.nodes.serial;
+
+import java.lang.reflect.*;
+import java.util.*;
+
+import sun.misc.*;
+
+import com.oracle.truffle.api.*;
+import com.oracle.truffle.api.nodes.*;
+import com.oracle.truffle.api.nodes.NodeUtil.*;
+
+/**
+ * Experimental API. May change without notice.
+ */
+public final class PostOrderDeserializer {
+
+    private static final Unsafe unsafe = loadUnsafe();
+
+    private final SerializerConstantPool cp;
+
+    private final HierarchicalStack stack = new HierarchicalStack();
+
+    /**
+     * Constructs a new serializer using a custom {@link SerializerConstantPool} implementation. For
+     * the {@link SerializerConstantPool} implementation at least the following methods must be
+     * implemented:
+     * <ul>
+     * <li>{@link SerializerConstantPool#getInt(int)}</li>
+     * <li>{@link SerializerConstantPool#getClass(int)}</li>
+     * </ul>
+     */
+    public PostOrderDeserializer(SerializerConstantPool cp) {
+        this.cp = cp;
+    }
+
+    /**
+     * Deserializes the byte stream and returns the deserialized Truffle AST node.
+     * 
+     * @param bytes the trimmed byte array containing the serialized data
+     * @param expectedType the expected root node type. Throws an exception if the root node is not
+     *            assignable from this type.
+     * @return the deserialized Truffle AST represented by the root Node.
+     * 
+     * @throws UnsupportedConstantPoolTypeException thrown if a type is encountered that is not
+     *             supported by the constant pool implementation.
+     */
+    @SuppressWarnings("unchecked")
+    public <T extends Node> T deserialize(byte[] bytes, Class<T> expectedType) throws UnsupportedConstantPoolTypeException {
+        VariableLengthIntBuffer buffer = new VariableLengthIntBuffer(bytes);
+
+        while (buffer.hasRemaining()) {
+            int classCPI = buffer.get();
+            if (classCPI == VariableLengthIntBuffer.NULL) {
+                pushNode(null);
+            } else {
+                Class<?> clazz = cp.getClass(classCPI);
+                if (clazz.isArray()) {
+                    int lengthCPI = buffer.get();
+                    if (lengthCPI == VariableLengthIntBuffer.NULL) {
+                        pushArray(null);
+                    } else {
+                        pushArray((Node[]) Array.newInstance(clazz.getComponentType(), cp.getInt(lengthCPI)));
+                    }
+                } else {
+                    pushNode(invokeDeserialize(buffer, clazz.asSubclass(Node.class)));
+                }
+            }
+        }
+        T returnNode = (T) popNode(null, expectedType);
+
+        assert stack.dynamicStack.isEmpty();
+
+        return returnNode;
+    }
+
+    private void pushNode(Node node) {
+        stack.push(node);
+    }
+
+    private void pushArray(Node[] array) {
+        stack.pushStack(array);
+    }
+
+    private Node[] popArray(Node parent, Class<?> expectedType) {
+        Node[] array = (Node[]) stack.popStack();
+        if (array != null) {
+            assertType(array, expectedType);
+            for (int i = 0; i < array.length; i++) {
+                updateParent(parent, array[i]);
+            }
+        }
+        return array;
+    }
+
+    private Node popNode(Node parent, Class<?> expectedType) {
+        Object o = stack.pop();
+        assertType(o, expectedType);
+        updateParent(parent, (Node) o);
+        return (Node) o;
+    }
+
+    private static void assertType(Object o, Class<?> expectedType) {
+        if (o != null && !expectedType.isAssignableFrom(o.getClass())) {
+            throw new AssertionError("Expected element type '" + expectedType.getName() + "' but was '" + o.getClass().getName() + "'.");
+        }
+    }
+
+    private Node invokeDeserialize(VariableLengthIntBuffer buffer, Class<? extends Node> nodeClass) throws UnsupportedConstantPoolTypeException {
+        if (nodeClass == null) {
+            return null;
+        }
+
+        Object object;
+        try {
+            object = unsafe.allocateInstance(nodeClass);
+        } catch (InstantiationException e) {
+            throw new RuntimeException("Unable to allocate truffle node " + nodeClass, e);
+        }
+        if (!(object instanceof Node)) {
+            throw new RuntimeException("Class is not a truffle node " + nodeClass);
+        }
+
+        Node node = (Node) object;
+
+        NodeField[] nodeFields = NodeClass.get(nodeClass).getFields();
+        deserializeChildrenFields(node, nodeFields);
+        deserializeChildFields(node, nodeFields);
+        deserializeDataFields(buffer, node, nodeFields);
+
+        return node;
+    }
+
+    private void deserializeDataFields(VariableLengthIntBuffer buffer, Node nodeInstance, NodeField[] nodeFields) throws UnsupportedConstantPoolTypeException {
+        for (int i = 0; i < nodeFields.length; i++) {
+            NodeField field = nodeFields[i];
+            if (field.getKind() == NodeFieldKind.DATA) {
+                Class<?> fieldClass = field.getType();
+                long offset = field.getOffset();
+
+                // source sections are not serialized
+                // TODO add support for source sections
+                if (field.getType().isAssignableFrom(SourceSection.class)) {
+                    continue;
+                }
+
+                int cpi = buffer.get();
+                if (cpi == VariableLengthIntBuffer.NULL) {
+                    if (fieldClass == int.class) {
+                        unsafe.putInt(nodeInstance, offset, 0);
+                    } else if (fieldClass == long.class) {
+                        unsafe.putLong(nodeInstance, offset, 0L);
+                    } else if (fieldClass == float.class) {
+                        unsafe.putFloat(nodeInstance, offset, 0.0F);
+                    } else if (fieldClass == double.class) {
+                        unsafe.putDouble(nodeInstance, offset, 0.0D);
+                    } else if (fieldClass == byte.class) {
+                        unsafe.putByte(nodeInstance, offset, (byte) 0);
+                    } else if (fieldClass == short.class) {
+                        unsafe.putShort(nodeInstance, offset, (short) 0);
+                    } else if (fieldClass == char.class) {
+                        unsafe.putChar(nodeInstance, offset, (char) 0);
+                    } else if (fieldClass == boolean.class) {
+                        unsafe.putBoolean(nodeInstance, offset, false);
+                    } else {
+                        unsafe.putObject(nodeInstance, offset, null);
+                    }
+                } else {
+                    if (fieldClass == int.class) {
+                        unsafe.putInt(nodeInstance, offset, cp.getInt(cpi));
+                    } else if (fieldClass == long.class) {
+                        unsafe.putLong(nodeInstance, offset, cp.getLong(cpi));
+                    } else if (fieldClass == float.class) {
+                        unsafe.putFloat(nodeInstance, offset, cp.getFloat(cpi));
+                    } else if (fieldClass == double.class) {
+                        unsafe.putDouble(nodeInstance, offset, cp.getDouble(cpi));
+                    } else if (fieldClass == byte.class) {
+                        unsafe.putByte(nodeInstance, offset, (byte) cp.getInt(cpi));
+                    } else if (fieldClass == short.class) {
+                        unsafe.putShort(nodeInstance, offset, (short) cp.getInt(cpi));
+                    } else if (fieldClass == char.class) {
+                        unsafe.putChar(nodeInstance, offset, (char) cp.getInt(cpi));
+                    } else if (fieldClass == boolean.class) {
+                        unsafe.putBoolean(nodeInstance, offset, cp.getInt(cpi) == 1 ? true : false);
+                    } else if (fieldClass == Integer.class) {
+                        unsafe.putObject(nodeInstance, offset, cp.getInt(cpi));
+                    } else if (fieldClass == Long.class) {
+                        unsafe.putObject(nodeInstance, offset, cp.getLong(cpi));
+                    } else if (fieldClass == Float.class) {
+                        unsafe.putObject(nodeInstance, offset, cp.getFloat(cpi));
+                    } else if (fieldClass == Double.class) {
+                        unsafe.putObject(nodeInstance, offset, cp.getDouble(cpi));
+                    } else if (fieldClass == Byte.class) {
+                        unsafe.putObject(nodeInstance, offset, (byte) cp.getInt(cpi));
+                    } else if (fieldClass == Short.class) {
+                        unsafe.putObject(nodeInstance, offset, (short) cp.getInt(cpi));
+                    } else if (fieldClass == Character.class) {
+                        unsafe.putObject(nodeInstance, offset, (char) cp.getInt(cpi));
+                    } else if (fieldClass == Boolean.class) {
+                        unsafe.putObject(nodeInstance, offset, cp.getInt(cpi) == 1 ? Boolean.TRUE : Boolean.FALSE);
+                    } else {
+                        unsafe.putObject(nodeInstance, offset, cp.getObject(fieldClass, cpi));
+                    }
+                }
+            }
+        }
+    }
+
+    private void deserializeChildFields(Node parent, NodeField[] nodeFields) {
+        for (int i = nodeFields.length - 1; i >= 0; i--) {
+            NodeField field = nodeFields[i];
+            if (field.getKind() == NodeFieldKind.CHILD) {
+                unsafe.putObject(parent, field.getOffset(), popNode(parent, field.getType()));
+            }
+        }
+    }
+
+    private void deserializeChildrenFields(Node parent, NodeField[] nodeFields) {
+        for (int i = nodeFields.length - 1; i >= 0; i--) {
+            NodeField field = nodeFields[i];
+            if (field.getKind() == NodeFieldKind.CHILDREN) {
+                unsafe.putObject(parent, field.getOffset(), popArray(parent, field.getType()));
+            }
+        }
+    }
+
+    private static Node updateParent(Node parent, Node child) {
+        if (child != null) {
+            long parentOffset = NodeClass.get(child.getClass()).getParentOffset();
+            unsafe.putObject(child, parentOffset, parent);
+        }
+        return child;
+    }
+
+    private static Unsafe loadUnsafe() {
+        try {
+            return Unsafe.getUnsafe();
+        } catch (SecurityException e) {
+        }
+        try {
+            Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe");
+            theUnsafeInstance.setAccessible(true);
+            return (Unsafe) theUnsafeInstance.get(Unsafe.class);
+        } catch (Exception e) {
+            throw new RuntimeException("exception while trying to get Unsafe.theUnsafe via reflection:", e);
+        }
+    }
+
+    private static class HierarchicalStack {
+
+        private static final Object NULL_STACK = new Object();
+
+        private final List<Object> dynamicStack = new ArrayList<>();
+
+        void pushStack(Object[] array) {
+            if (array == null) {
+                dynamicStack.add(NULL_STACK);
+            } else {
+                dynamicStack.add(new FixedSizeNodeStack(array));
+            }
+        }
+
+        private FixedSizeNodeStack getTosStack() {
+            if (dynamicStack.isEmpty()) {
+                return null;
+            }
+            Object peekTos = dynamicStack.get(dynamicStack.size() - 1);
+            if (peekTos != null && peekTos.getClass() == FixedSizeNodeStack.class) {
+                return (FixedSizeNodeStack) peekTos;
+            }
+            return null;
+        }
+
+        Object[] popStack() {
+            Object tos = dynamicStack.remove(dynamicStack.size() - 1);
+            if (tos == NULL_STACK) {
+                return null;
+            }
+            return ((FixedSizeNodeStack) tos).getArray();
+        }
+
+        void push(Object o) {
+            FixedSizeNodeStack tosStack = getTosStack();
+            if (tosStack != null && !tosStack.isFull()) {
+                tosStack.push(o);
+            } else {
+                dynamicStack.add(o);
+            }
+        }
+
+        Object pop() {
+            FixedSizeNodeStack tosStack = getTosStack();
+            Object value;
+            if (tosStack != null) {
+                assert !tosStack.isEmpty();
+                value = tosStack.pop();
+            } else {
+                value = dynamicStack.remove(dynamicStack.size() - 1);
+            }
+            assert value != NULL_STACK;
+            return value;
+        }
+
+    }
+
+    private static class FixedSizeNodeStack {
+
+        private final Object[] array;
+
+        private int tos;
+
+        FixedSizeNodeStack(Object[] array) {
+            this.array = array;
+        }
+
+        boolean isFull() {
+            return tos == array.length;
+        }
+
+        boolean isEmpty() {
+            return tos == 0;
+        }
+
+        private void push(Object node) {
+            if (tos >= array.length) {
+                throw new ArrayIndexOutOfBoundsException();
+            }
+            unsafe.putObject(array, Unsafe.ARRAY_OBJECT_BASE_OFFSET + Unsafe.ARRAY_OBJECT_INDEX_SCALE * (long) (tos++), node);
+        }
+
+        private Object pop() {
+            if (tos <= 0) {
+                throw new ArrayIndexOutOfBoundsException();
+            }
+            return unsafe.getObject(array, Unsafe.ARRAY_OBJECT_BASE_OFFSET + Unsafe.ARRAY_OBJECT_INDEX_SCALE * (long) (--tos));
+        }
+
+        private Object[] getArray() {
+            return array;
+        }
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/PostOrderSerializer.java	Wed Oct 02 15:33:08 2013 +0200
@@ -0,0 +1,199 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.nodes.serial;
+
+import java.lang.reflect.*;
+import java.nio.*;
+
+import sun.misc.*;
+
+import com.oracle.truffle.api.*;
+import com.oracle.truffle.api.nodes.*;
+import com.oracle.truffle.api.nodes.NodeUtil.NodeClass;
+import com.oracle.truffle.api.nodes.NodeUtil.NodeField;
+import com.oracle.truffle.api.nodes.NodeUtil.NodeFieldKind;
+
+/**
+ * Experimental API. May change without notice.
+ */
+public final class PostOrderSerializer {
+
+    private static final Unsafe unsafe = loadUnsafe();
+
+    private final SerializerConstantPool cp;
+
+    /**
+     * Constructs a new deserializer using a custom {@link SerializerConstantPool} implementation.
+     * For the {@link SerializerConstantPool} implementation at least the following methods must be
+     * implemented:
+     * <ul>
+     * <li>{@link SerializerConstantPool#putInt(int)}</li>
+     * <li>{@link SerializerConstantPool#putClass(Class)}</li>
+     * </ul>
+     */
+    public PostOrderSerializer(SerializerConstantPool cp) {
+        this.cp = cp;
+    }
+
+    /**
+     * Serializes the node AST and returns the serialized data as byte array.
+     * 
+     * @param node the root node that represents the Truffle AST that should be serialized.
+     * @return a trimmed byte array that contains the serialized data.
+     * 
+     * @throws UnsupportedConstantPoolTypeException thrown if a type is encountered that is not
+     *             supported by the constant pool implementation.
+     */
+    public byte[] serialize(Node node) throws UnsupportedConstantPoolTypeException {
+        VariableLengthIntBuffer buffer = new VariableLengthIntBuffer(ByteBuffer.allocate(512));
+        serialize(buffer, node);
+        return buffer.getBytes();
+    }
+
+    private void serialize(VariableLengthIntBuffer buffer, Node node) throws UnsupportedConstantPoolTypeException {
+        if (node == null) {
+            buffer.put(VariableLengthIntBuffer.NULL);
+            return;
+        }
+        Class<? extends Node> nodeClass = node.getClass();
+
+        NodeField[] nodeFields = NodeClass.get(nodeClass).getFields();
+        serializeChildFields(buffer, node, nodeFields);
+        serializeChildrenFields(buffer, node, nodeFields);
+        buffer.put(cp.putClass(node.getClass()));
+        serializeDataFields(buffer, node, nodeFields);
+    }
+
+    private void serializeDataFields(VariableLengthIntBuffer buffer, Node node, NodeField[] nodeFields) throws UnsupportedConstantPoolTypeException {
+        for (int i = 0; i < nodeFields.length; i++) {
+            NodeField field = nodeFields[i];
+            if (field.getKind() == NodeFieldKind.DATA) {
+                Class<?> fieldClass = field.getType();
+                long offset = field.getOffset();
+                int cpi;
+
+                if (field.getType().isAssignableFrom(SourceSection.class)) {
+                    continue;
+                }
+
+                if (fieldClass == int.class) {
+                    cpi = cp.putInt(unsafe.getInt(node, offset));
+                } else if (fieldClass == long.class) {
+                    cpi = cp.putLong(unsafe.getLong(node, offset));
+                } else if (fieldClass == float.class) {
+                    cpi = cp.putFloat(unsafe.getFloat(node, offset));
+                } else if (fieldClass == double.class) {
+                    cpi = cp.putDouble(unsafe.getDouble(node, offset));
+                } else if (fieldClass == byte.class) {
+                    cpi = cp.putInt(unsafe.getByte(node, offset));
+                } else if (fieldClass == short.class) {
+                    cpi = cp.putInt(unsafe.getShort(node, offset));
+                } else if (fieldClass == char.class) {
+                    cpi = cp.putInt(unsafe.getChar(node, offset));
+                } else if (fieldClass == boolean.class) {
+                    cpi = cp.putInt(unsafe.getBoolean(node, offset) ? 1 : 0);
+                } else {
+                    Object value = unsafe.getObject(node, offset);
+                    if (value == null) {
+                        cpi = VariableLengthIntBuffer.NULL;
+                    } else if (fieldClass == Integer.class) {
+                        cpi = cp.putInt((Integer) value);
+                    } else if (fieldClass == Long.class) {
+                        cpi = cp.putLong((Long) value);
+                    } else if (fieldClass == Float.class) {
+                        cpi = cp.putFloat((Float) value);
+                    } else if (fieldClass == Double.class) {
+                        cpi = cp.putDouble((Double) value);
+                    } else if (fieldClass == Byte.class) {
+                        cpi = cp.putInt((Byte) value);
+                    } else if (fieldClass == Short.class) {
+                        cpi = cp.putInt((Short) value);
+                    } else if (fieldClass == Character.class) {
+                        cpi = cp.putInt((Character) value);
+                    } else if (fieldClass == Boolean.class) {
+                        cpi = cp.putInt((Boolean) value ? 1 : 0);
+                    } else {
+                        cpi = cp.putObject(fieldClass, value);
+                    }
+                }
+
+                buffer.put(cpi);
+            }
+        }
+    }
+
+    private void serializeChildrenFields(VariableLengthIntBuffer buffer, Node nodeInstance, NodeField[] nodeFields) throws UnsupportedConstantPoolTypeException {
+        for (int i = 0; i < nodeFields.length; i++) {
+            NodeField field = nodeFields[i];
+            if (field.getKind() == NodeFieldKind.CHILDREN) {
+                Object childArrayObject = unsafe.getObject(nodeInstance, field.getOffset());
+                if (childArrayObject != null && !(childArrayObject instanceof Node[])) {
+                    throw new AssertionError("Node children must be instanceof Node[]");
+                }
+
+                buffer.put(cp.putClass(field.getType()));
+
+                Node[] childArray = (Node[]) childArrayObject;
+                if (childArray == null) {
+                    buffer.put(VariableLengthIntBuffer.NULL);
+                } else {
+                    buffer.put(cp.putInt(childArray.length));
+
+                    for (int j = 0; j < childArray.length; j++) {
+                        serialize(buffer, childArray[j]);
+                    }
+                }
+            }
+        }
+    }
+
+    private void serializeChildFields(VariableLengthIntBuffer buffer, Node nodeInstance, NodeField[] nodeFields) throws UnsupportedConstantPoolTypeException {
+        for (int i = 0; i < nodeFields.length; i++) {
+            NodeField field = nodeFields[i];
+            if (field.getKind() == NodeFieldKind.CHILD) {
+                Object childObject = unsafe.getObject(nodeInstance, field.getOffset());
+                if (childObject != null && !(childObject instanceof Node)) {
+                    throw new AssertionError("Node children must be instanceof Node");
+                }
+                serialize(buffer, (Node) childObject);
+            }
+        }
+    }
+
+    private static Unsafe loadUnsafe() {
+        try {
+            return Unsafe.getUnsafe();
+        } catch (SecurityException e) {
+        }
+        try {
+            Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe");
+            theUnsafeInstance.setAccessible(true);
+            return (Unsafe) theUnsafeInstance.get(Unsafe.class);
+        } catch (Exception e) {
+            throw new RuntimeException("exception while trying to get Unsafe.theUnsafe via reflection:", e);
+        }
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/SerializerConstantPool.java	Wed Oct 02 15:33:08 2013 +0200
@@ -0,0 +1,152 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.nodes.serial;
+
+/**
+ * Experimental API. May change without notice. This interface is used as bridge between the
+ * {@link PostOrderDeserializer}, {@link PostOrderSerializer} and underlying constant pool
+ * implementation. A constant pool stores a value and returns an identifying index, with which the
+ * object can later be returned from the pool again. All methods of this class are optional and may
+ * throw a {@link UnsupportedOperationException}.
+ */
+public interface SerializerConstantPool {
+
+    /**
+     * Returns the constant pool index of a value that is not a java native type, a java
+     * native-wrapper class or a {@link Class} instance. The implementor should support all
+     * additional types that are necessary to serialize a truffle AST for a specific truffle
+     * implementation. If a type is not supported by this constant pool implementation a
+     * {@link UnsupportedConstantPoolTypeException} should be thrown.
+     * 
+     * @param clazz the {@link Class} of the value
+     * @param value the value to be stored. Must be at least a subclass of the given clazz.
+     * @return the constant pool index
+     * @throws UnsupportedConstantPoolTypeException if a type is not supported for persistence in
+     *             the constant pool.
+     */
+    int putObject(Class<?> clazz, Object value) throws UnsupportedConstantPoolTypeException;
+
+    /**
+     * Stores a value in the constant pool that is not a java native type, a java native-wrapper
+     * class or a {@link Class} instance. The implementor should support all additional types that
+     * are necessary to serialize a truffle AST for a specific truffle implementation. If a type is
+     * not supported by this constant pool implementation a
+     * {@link UnsupportedConstantPoolTypeException} should be thrown.
+     * 
+     * @param clazz the {@link Class} of the value in the constant pool.
+     * @param cpi the previously returned index
+     * @return the value stored inside the constant pool
+     * @throws UnsupportedConstantPoolTypeException if a type is not supported for persistence in
+     *             the constant pool.
+     * @throws IllegalArgumentException if the provided cpi is not a valid constant pool index.
+     */
+    Object getObject(Class<?> clazz, int cpi) throws UnsupportedConstantPoolTypeException;
+
+    /**
+     * Stores a Class instance in the constant pool and returns the constant pool index.
+     * 
+     * @param value the class to store
+     * @return the new or existing constant pool index of the Class
+     */
+    int putClass(Class<?> value);
+
+    /**
+     * Returns the {@link Class} instance to the given constant pool index.
+     * 
+     * @param cpi the constant pool index
+     * @return stored value
+     * @throws IllegalArgumentException if the constant pool indes is invalid.
+     */
+    Class<?> getClass(int cpi);
+
+    /**
+     * Stores an int value in the constant pool and returns the constant pool index.
+     * 
+     * @param value the value to store
+     * @return the new or existing constant pool index of the value
+     */
+    int putInt(int value);
+
+    /**
+     * Returns the stored int value to the given constant pool index from the constant pool.
+     * 
+     * @param cpi the constant pool index
+     * @return stored value
+     * @throws IllegalArgumentException if the constant pool index is invalid.
+     */
+    int getInt(int cpi);
+
+    /**
+     * Stores a long value in the constant pool and returns the constant pool index.
+     * 
+     * @param value the value to store
+     * @return the new or existing constant pool index of the value
+     */
+    int putLong(long value);
+
+    /**
+     * Returns the stored long value to the given constant pool index from the constant pool.
+     * 
+     * @param cpi the constant pool index
+     * @return the stored value
+     * @throws IllegalArgumentException if the constant pool index is invalid.
+     */
+    long getLong(int cpi);
+
+    /**
+     * Stores a double value in the constant pool and returns the constant pool index.
+     * 
+     * @param value the value to store
+     * @return the new or existing constant pool index of the value
+     */
+    int putDouble(double value);
+
+    /**
+     * Returns the stored double value to the given constant pool index from the constant pool.
+     * 
+     * @param cpi the constant pool index
+     * @return the stored value
+     * @throws IllegalArgumentException if the constant pool index is invalid.
+     */
+    double getDouble(int cpi);
+
+    /**
+     * Stores a float value in the constant pool and returns the constant pool index.
+     * 
+     * @param value the value to store
+     * @return the new or existing constant pool index of the value
+     */
+    int putFloat(float value);
+
+    /**
+     * Returns the stored float value to the given constant pool index from the constant pool.
+     * 
+     * @param cpi the constant pool index
+     * @return the stored value
+     * @throws IllegalArgumentException if the constant pool index is invalid.
+     */
+    float getFloat(int cpi);
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/UnsupportedConstantPoolTypeException.java	Wed Oct 02 15:33:08 2013 +0200
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.nodes.serial;
+
+/**
+ * Experimental API. May change without notice.
+ */
+public class UnsupportedConstantPoolTypeException extends RuntimeException {
+
+    private static final long serialVersionUID = 1L;
+
+    public UnsupportedConstantPoolTypeException() {
+        super();
+    }
+
+    public UnsupportedConstantPoolTypeException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    public UnsupportedConstantPoolTypeException(String message) {
+        super(message);
+    }
+
+    public UnsupportedConstantPoolTypeException(Throwable cause) {
+        super(cause);
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/VariableLengthIntBuffer.java	Wed Oct 02 15:33:08 2013 +0200
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.nodes.serial;
+
+import java.nio.*;
+
+/**
+ * Experimental API. May change without notice. Simple variable length unsigned int buffer backed by
+ * a byte buffer.
+ */
+public class VariableLengthIntBuffer {
+
+    public static final int NULL = -1;
+
+    private ByteBuffer buffer;
+
+    public VariableLengthIntBuffer(ByteBuffer buffer) {
+        this.buffer = buffer;
+    }
+
+    public VariableLengthIntBuffer(byte[] array) {
+        buffer = ByteBuffer.wrap(array);
+    }
+
+    /**
+     * Returns the backing byte buffer.
+     */
+    public ByteBuffer getBuffer() {
+        return buffer;
+    }
+
+    public byte[] getBytes() {
+        int pos = buffer.position();
+        byte[] bytes = new byte[buffer.position()];
+        buffer.rewind();
+        buffer.get(bytes);
+        buffer.position(pos);
+        return bytes;
+    }
+
+    public int get() {
+        byte peekByte = buffer.get(buffer.position());
+        if ((peekByte & 0x80) == 0) {
+            // single byte encoding with prefix 0 (range 127)
+            return buffer.get(); // no bit to be masked
+        } else {
+            if (peekByte == (byte) 0xFF) {
+                buffer.get(); // skip one byte
+                return NULL;
+            }
+            int result = buffer.getInt() & 0x7FFF_FFFF; // first bit masked out
+            assert (result & 0x4000_0000) == 0;
+            return result;
+        }
+    }
+
+    public void put(int i) {
+        ensureCapacity();
+        if (i == NULL) {
+            buffer.put((byte) 0xFF);
+        } else if ((i & 0xFFFF_FF80) == 0) { // 7 bits data
+            buffer.put((byte) i);
+        } else if ((i & 0xC000_0000) == 0) { // 32 bits data
+            buffer.putInt(i | 0x8000_0000); // append leading 1
+        } else {
+            throw new IllegalArgumentException("Integer out of encodeable " + i);
+        }
+    }
+
+    private void ensureCapacity() {
+        if (buffer.position() + 4 > buffer.capacity()) {
+            ByteBuffer newBuffer = ByteBuffer.allocate(buffer.capacity() * 2);
+
+            int pos = buffer.position();
+            buffer.rewind();
+            newBuffer.put(buffer);
+            newBuffer.position(pos);
+
+            buffer = newBuffer;
+        }
+    }
+
+    public boolean hasRemaining() {
+        return buffer.hasRemaining();
+    }
+
+}