# HG changeset patch # User Michael Van De Vanter # Date 1447907912 28800 # Node ID 5573f12b94f8c4df81d26dde2637a7e66e8614f6 # Parent 57afe7055486cbcc75ce83e4e1ae4c6b40384b6b# Parent 37fabf84537a53b7994b9b8867bd3f4439dfc775 Merge diff -r 57afe7055486 -r 5573f12b94f8 mx.truffle/mx_truffle.py --- a/mx.truffle/mx_truffle.py Wed Nov 18 18:42:56 2015 -0800 +++ b/mx.truffle/mx_truffle.py Wed Nov 18 20:38:32 2015 -0800 @@ -44,6 +44,11 @@ vmArgs, slArgs = mx.extract_VM_args(args) mx.run_java(vmArgs + ['-cp', mx.classpath(["TRUFFLE_API", "com.oracle.truffle.sl"]), "com.oracle.truffle.sl.SLLanguage"] + slArgs) +def slcoverage(args): + """Demo: run an SL program with coverage counts printed when done""" + vmArgs, slArgs = mx.extract_VM_args(args) + mx.run_java(vmArgs + ['-cp', mx.classpath("com.oracle.truffle.sl.tools"), "com.oracle.truffle.sl.tools.SLCoverage"] + slArgs) + def sldebug(args): """run a simple command line debugger for the Simple Language""" vmArgs, slArgs = mx.extract_VM_args(args, useDoubleDash=True) @@ -58,4 +63,5 @@ mx.update_commands(_suite, { 'sl' : [sl, '[SL args|@VM options]'], 'sldebug' : [sldebug, '[SL args|@VM options]'], + 'slcoverage' : [slcoverage, '[SL args|@VM options]'], }) diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api.dsl/src/com/oracle/truffle/api/dsl/internal/SpecializationNode.java diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/PostOrderDeserializerTest.java --- a/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/PostOrderDeserializerTest.java Wed Nov 18 18:42:56 2015 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,188 +0,0 @@ -/* - * 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 com.oracle.truffle.api.nodes.Node; -import com.oracle.truffle.api.nodes.serial.PostOrderDeserializer; -import com.oracle.truffle.api.nodes.serial.VariableLengthIntBuffer; -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; -import java.nio.ByteBuffer; -import java.util.Iterator; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -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; - } - - expectedAst.adoptChildren(); - - Assert.assertNotNull(actualAst); - // fields are asserted using the corresponding equals implementation - Assert.assertEquals(expectedAst, actualAst); - - Iterable expectedChildIterator = expectedAst.getChildren(); - Iterator 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); - - } - } - -} diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/PostOrderSerializerTest.java --- a/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/PostOrderSerializerTest.java Wed Nov 18 18:42:56 2015 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,133 +0,0 @@ -/* - * 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 com.oracle.truffle.api.nodes.Node; -import com.oracle.truffle.api.nodes.serial.PostOrderSerializer; -import com.oracle.truffle.api.nodes.serial.VariableLengthIntBuffer; -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; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -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()); - } - -} diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/TestNodes.java --- a/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/TestNodes.java Wed Nov 18 18:42:56 2015 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,220 +0,0 @@ -/* - * 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 com.oracle.truffle.api.nodes.Node; -import java.util.Objects; - -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 = child; - } - - } - - static class NodeWithTwoChilds extends EmptyNode { - - @Child Node child1; - @Child Node child2; - - public NodeWithTwoChilds(Node child1, Node child2) { - this.child1 = child1; - this.child2 = 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 = child1; - this.child2 = child2; - this.child3 = child3; - } - - } - - static class NodeWithArray extends EmptyNode { - - @Children private final Node[] childNodes; - - NodeWithArray(Node[] children) { - this.childNodes = 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 = childs1; - this.childNodes2 = 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; - } - - } - -} diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/TestSerializerConstantPool.java --- a/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/TestSerializerConstantPool.java Wed Nov 18 18:42:56 2015 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,114 +0,0 @@ -/* - * 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 com.oracle.truffle.api.nodes.serial.SerializerConstantPool; -import com.oracle.truffle.api.nodes.serial.UnsupportedConstantPoolTypeException; -import java.util.HashMap; -import java.util.Map; - -class TestSerializerConstantPool implements SerializerConstantPool { - - private final Map int2object = new HashMap<>(); - private final Map 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); - } - -} diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/VariableLengthIntBufferTest.java --- a/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/VariableLengthIntBufferTest.java Wed Nov 18 18:42:56 2015 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,125 +0,0 @@ -/* - * 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 com.oracle.truffle.api.nodes.serial.VariableLengthIntBuffer; -import java.nio.ByteBuffer; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -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]); - } - } - -} diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/PolyglotEngine.java diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleLanguage.java --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleLanguage.java Wed Nov 18 18:42:56 2015 -0800 +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleLanguage.java Wed Nov 18 20:38:32 2015 -0800 @@ -389,6 +389,25 @@ } /** + * Evaluates source of (potentially different) language. The {@link Source#getMimeType() + * MIME type} is used to identify the {@link TruffleLanguage} to use to perform the + * {@link #parse(com.oracle.truffle.api.source.Source, com.oracle.truffle.api.nodes.Node, java.lang.String...)} + * . The names of arguments are parameters for the resulting {#link CallTarget} that allow + * the source to reference the actual parameters passed to + * {@link CallTarget#call(java.lang.Object...)}. + * + * @param source the source to evaluate + * @param argumentNames the names of {@link CallTarget#call(java.lang.Object...)} arguments + * that can be referenced from the source + * @return the call target representing the parsed result + * @throws IOException if the parsing or evaluation fails for some reason + */ + public CallTarget parse(Source source, String... argumentNames) throws IOException { + TruffleLanguage language = API.findLanguageImpl(vm, null, source.getMimeType()); + return language.parse(source, null, argumentNames); + } + + /** * Input associated with {@link com.oracle.truffle.api.vm.PolyglotEngine} this language is * being executed in. * @@ -495,6 +514,11 @@ } @Override + protected TruffleLanguage findLanguageImpl(Object known, Class languageClass, String mimeType) { + return super.findLanguageImpl(known, languageClass, mimeType); + } + + @Override protected Object languageGlobal(TruffleLanguage.Env env) { return env.langCtx.getLanguageGlobal(); } diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/impl/DefaultASTPrinter.java diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/GraphPrintVisitor.java --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/GraphPrintVisitor.java Wed Nov 18 18:42:56 2015 -0800 +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/GraphPrintVisitor.java Wed Nov 18 20:38:32 2015 -0800 @@ -120,11 +120,29 @@ } } - private static class Impl { + private interface Impl { + void writeStartDocument(); + + void writeEndDocument(); + + void writeStartElement(String name); + + void writeEndElement(); + + void writeAttribute(String name, String value); + + void writeCharacters(String text); + + void flush(); + + void close(); + } + + private static class XMLImpl implements Impl { private static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newInstance(); private final XMLStreamWriter xmlstream; - protected Impl(OutputStream outputStream) { + protected XMLImpl(OutputStream outputStream) { try { this.xmlstream = XML_OUTPUT_FACTORY.createXMLStreamWriter(outputStream); } catch (XMLStreamException | FactoryConfigurationError e) { @@ -203,11 +221,15 @@ public GraphPrintVisitor(OutputStream outputStream) { this.outputStream = outputStream; - this.xmlstream = new Impl(outputStream); + this.xmlstream = createImpl(outputStream); this.xmlstream.writeStartDocument(); this.xmlstream.writeStartElement("graphDocument"); } + private static Impl createImpl(OutputStream outputStream) { + return new XMLImpl(outputStream); + } + private void ensureOpen() { if (xmlstream == null) { throw new IllegalStateException("printer is closed"); @@ -373,11 +395,13 @@ public void printToNetwork(boolean ignoreErrors) { close(); - try (Socket socket = new Socket(GraphVisualizerAddress, GraphVisualizerPort); BufferedOutputStream os = new BufferedOutputStream(socket.getOutputStream(), 0x4000)) { - os.write(((ByteArrayOutputStream) outputStream).toByteArray()); - } catch (IOException e) { - if (!ignoreErrors) { - e.printStackTrace(); + if (outputStream instanceof ByteArrayOutputStream) { + try (Socket socket = new Socket(GraphVisualizerAddress, GraphVisualizerPort); BufferedOutputStream os = new BufferedOutputStream(socket.getOutputStream(), 0x4000)) { + os.write(((ByteArrayOutputStream) outputStream).toByteArray()); + } catch (IOException e) { + if (!ignoreErrors) { + e.printStackTrace(); + } } } } diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/Node.java diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeClass.java diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeFieldAccessor.java diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/RootNode.java --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/RootNode.java Wed Nov 18 18:42:56 2015 -0800 +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/RootNode.java Wed Nov 18 20:38:32 2015 -0800 @@ -24,6 +24,7 @@ */ package com.oracle.truffle.api.nodes; +import com.oracle.truffle.api.CompilerAsserts; import com.oracle.truffle.api.CompilerDirectives.CompilationFinal; import com.oracle.truffle.api.CompilerOptions; import com.oracle.truffle.api.ExecutionContext; @@ -106,6 +107,7 @@ * heuristics can use the loop count to guide compilation and inlining. */ public final void reportLoopCount(int count) { + CompilerAsserts.neverPartOfCompilation(); if (getCallTarget() instanceof LoopCountReceiver) { ((LoopCountReceiver) getCallTarget()).reportLoopCount(count); } diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/PostOrderDeserializer.java --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/PostOrderDeserializer.java Wed Nov 18 18:42:56 2015 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,390 +0,0 @@ -/* - * 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 com.oracle.truffle.api.nodes.Node; -import com.oracle.truffle.api.nodes.NodeClass; -import com.oracle.truffle.api.nodes.NodeFieldAccessor; -import com.oracle.truffle.api.nodes.NodeFieldAccessor.NodeFieldKind; -import com.oracle.truffle.api.source.SourceSection; -import java.lang.reflect.Array; -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.List; -import sun.misc.Unsafe; - -/** - * 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: - *
    - *
  • {@link SerializerConstantPool#getInt(int)}
  • - *
  • {@link SerializerConstantPool#getClass(int)}
  • - *
- */ - 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 deserialize(byte[] bytes, Class 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 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; - - NodeFieldAccessor[] nodeFields = NodeClass.get(nodeClass).getFields(); - deserializeChildrenFields(node, nodeFields); - deserializeChildFields(node, nodeFields); - deserializeDataFields(buffer, node, nodeFields); - - return node; - } - - private void deserializeDataFields(VariableLengthIntBuffer buffer, Node nodeInstance, NodeFieldAccessor[] nodeFields) throws UnsupportedConstantPoolTypeException { - for (int i = 0; i < nodeFields.length; i++) { - NodeFieldAccessor field = nodeFields[i]; - if (field.getKind() == NodeFieldKind.DATA) { - Class fieldClass = field.getType(); - long offset = getFieldOffset(field); - - // 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) { - deserializeDataFieldsLengthNull(nodeInstance, fieldClass, offset); - } else { - deserializeDataFieldsDefault(nodeInstance, fieldClass, offset, cpi); - } - } - } - } - - private void deserializeDataFieldsDefault(Node nodeInstance, Class fieldClass, long offset, int cpi) { - 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 static void deserializeDataFieldsLengthNull(Node nodeInstance, Class fieldClass, long offset) { - 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); - } - } - - private void deserializeChildFields(Node parent, NodeFieldAccessor[] nodeFields) { - for (int i = nodeFields.length - 1; i >= 0; i--) { - NodeFieldAccessor field = nodeFields[i]; - if (field.getKind() == NodeFieldKind.CHILD) { - unsafe.putObject(parent, getFieldOffset(field), popNode(parent, field.getType())); - } - } - } - - private void deserializeChildrenFields(Node parent, NodeFieldAccessor[] nodeFields) { - for (int i = nodeFields.length - 1; i >= 0; i--) { - NodeFieldAccessor field = nodeFields[i]; - if (field.getKind() == NodeFieldKind.CHILDREN) { - unsafe.putObject(parent, getFieldOffset(field), popArray(parent, field.getType())); - } - } - } - - private static Node updateParent(Node parent, Node child) { - if (child != null) { - NodeClass nodeClass = NodeClass.get(child.getClass()); - nodeClass.getNodeClassField().putObject(child, nodeClass); - nodeClass.getParentField().putObject(child, parent); - } - return child; - } - - static long getFieldOffset(NodeFieldAccessor field) { - if (field instanceof NodeFieldAccessor.AbstractUnsafeNodeFieldAccessor) { - return ((NodeFieldAccessor.AbstractUnsafeNodeFieldAccessor) field).getOffset(); - } else { - try { - Field reflectionField = field.getDeclaringClass().getDeclaredField(field.getName()); - return unsafe.objectFieldOffset(reflectionField); - } catch (NoSuchFieldException | SecurityException e) { - throw new RuntimeException(e); - } - } - } - - 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 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; - } - } - -} diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/PostOrderSerializer.java --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/PostOrderSerializer.java Wed Nov 18 18:42:56 2015 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,202 +0,0 @@ -/* - * 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 com.oracle.truffle.api.nodes.Node; -import com.oracle.truffle.api.nodes.NodeClass; -import com.oracle.truffle.api.nodes.NodeFieldAccessor; -import com.oracle.truffle.api.nodes.NodeFieldAccessor.NodeFieldKind; -import static com.oracle.truffle.api.nodes.serial.PostOrderDeserializer.getFieldOffset; -import com.oracle.truffle.api.source.SourceSection; -import java.lang.reflect.Field; -import java.nio.ByteBuffer; -import sun.misc.Unsafe; - -/** - * 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: - *
    - *
  • {@link SerializerConstantPool#putInt(int)}
  • - *
  • {@link SerializerConstantPool#putClass(Class)}
  • - *
- */ - 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 nodeClass = node.getClass(); - - NodeFieldAccessor[] 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, NodeFieldAccessor[] nodeFields) throws UnsupportedConstantPoolTypeException { - for (int i = 0; i < nodeFields.length; i++) { - NodeFieldAccessor field = nodeFields[i]; - if (field.getKind() == NodeFieldKind.DATA) { - Class fieldClass = field.getType(); - long offset = getFieldOffset(field); - 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 { - cpi = serializeDataFieldsObject(node, fieldClass, offset); - } - - buffer.put(cpi); - } - } - } - - private int serializeDataFieldsObject(Node node, Class fieldClass, long offset) { - Object value = unsafe.getObject(node, offset); - if (value == null) { - return VariableLengthIntBuffer.NULL; - } else if (fieldClass == Integer.class) { - return cp.putInt((Integer) value); - } else if (fieldClass == Long.class) { - return cp.putLong((Long) value); - } else if (fieldClass == Float.class) { - return cp.putFloat((Float) value); - } else if (fieldClass == Double.class) { - return cp.putDouble((Double) value); - } else if (fieldClass == Byte.class) { - return cp.putInt((Byte) value); - } else if (fieldClass == Short.class) { - return cp.putInt((Short) value); - } else if (fieldClass == Character.class) { - return cp.putInt((Character) value); - } else if (fieldClass == Boolean.class) { - return cp.putInt((Boolean) value ? 1 : 0); - } else { - return cp.putObject(fieldClass, value); - } - } - - private void serializeChildrenFields(VariableLengthIntBuffer buffer, Node nodeInstance, NodeFieldAccessor[] nodeFields) throws UnsupportedConstantPoolTypeException { - for (int i = 0; i < nodeFields.length; i++) { - NodeFieldAccessor field = nodeFields[i]; - if (field.getKind() == NodeFieldKind.CHILDREN) { - Object childArrayObject = unsafe.getObject(nodeInstance, getFieldOffset(field)); - 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, NodeFieldAccessor[] nodeFields) throws UnsupportedConstantPoolTypeException { - for (int i = 0; i < nodeFields.length; i++) { - NodeFieldAccessor field = nodeFields[i]; - if (field.getKind() == NodeFieldKind.CHILD) { - Object childObject = unsafe.getObject(nodeInstance, getFieldOffset(field)); - 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); - } - } - -} diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/SerializerConstantPool.java --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/SerializerConstantPool.java Wed Nov 18 18:42:56 2015 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,152 +0,0 @@ -/* - * 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); - -} diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/UnsupportedConstantPoolTypeException.java --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/UnsupportedConstantPoolTypeException.java Wed Nov 18 18:42:56 2015 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,50 +0,0 @@ -/* - * 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); - } - -} diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/VariableLengthIntBuffer.java --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/VariableLengthIntBuffer.java Wed Nov 18 18:42:56 2015 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,109 +0,0 @@ -/* - * 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.ByteBuffer; - -/** - * 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(); - } - -} diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/package-info.java --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/package-info.java Wed Nov 18 18:42:56 2015 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2015, 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. - */ - -/* - @ApiInfo( - group="Experimental" - ) - */ - -/** Persisting AST nodes. - */ -package com.oracle.truffle.api.nodes.serial; - diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/LineLocation.java --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/LineLocation.java Wed Nov 18 18:42:56 2015 -0800 +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/LineLocation.java Wed Nov 18 20:38:32 2015 -0800 @@ -45,7 +45,7 @@ /** * Gets the 1-based number of a line in the source. - * + * * @return value from 1 to infinity */ public int getLineNumber() { @@ -90,7 +90,15 @@ @Override public int compareTo(LineLocation o) { - final int sourceResult = this.getSource().getPath().compareTo(o.getSource().getPath()); + int sourceResult = 0; + final Source thisSource = this.getSource(); + final String thisPath = thisSource.getPath(); + if (thisPath == null) { + sourceResult = thisSource.getCode().compareTo(o.getSource().getCode()); + } else { + final String thatPath = o.getSource().getPath(); + sourceResult = thisPath.compareTo(thatPath); + } if (sourceResult != 0) { return sourceResult; } diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java Wed Nov 18 18:42:56 2015 -0800 +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java Wed Nov 18 20:38:32 2015 -0800 @@ -543,6 +543,7 @@ * @return newly created object representing the specified region */ public final SourceSection createSection(String identifier, int startLine, int startColumn, int charIndex, int length) { + checkRange(charIndex, length); return new SourceSection(null, this, identifier, startLine, startColumn, charIndex, length); } diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/SourceSection.java --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/SourceSection.java Wed Nov 18 18:42:56 2015 -0800 +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/SourceSection.java Wed Nov 18 20:38:32 2015 -0800 @@ -193,7 +193,7 @@ @Override public String toString() { - return getCode(); + return getShortDescription(); } @Override diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.object.basic/src/com/oracle/truffle/object/basic/ShapeBasic.java --- a/truffle/com.oracle.truffle.object.basic/src/com/oracle/truffle/object/basic/ShapeBasic.java Wed Nov 18 18:42:56 2015 -0800 +++ b/truffle/com.oracle.truffle.object.basic/src/com/oracle/truffle/object/basic/ShapeBasic.java Wed Nov 18 20:38:32 2015 -0800 @@ -24,7 +24,6 @@ import com.oracle.truffle.api.object.Layout; import com.oracle.truffle.api.object.ObjectType; -import com.oracle.truffle.api.object.Property; import com.oracle.truffle.object.PropertyMap; import com.oracle.truffle.object.ShapeImpl; import com.oracle.truffle.object.Transition; @@ -43,9 +42,4 @@ protected ShapeImpl createShape(Layout layout, Object sharedData, ShapeImpl parent, ObjectType objectType, PropertyMap propertyMap, Transition transition, Allocator allocator, int id) { return new ShapeBasic(layout, sharedData, parent, objectType, propertyMap, transition, allocator, id); } - - @Override - public ShapeImpl replaceProperty(Property oldProperty, Property newProperty) { - return directReplaceProperty(oldProperty, newProperty); - } } diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.object/src/com/oracle/truffle/object/LocationImpl.java --- a/truffle/com.oracle.truffle.object/src/com/oracle/truffle/object/LocationImpl.java Wed Nov 18 18:42:56 2015 -0800 +++ b/truffle/com.oracle.truffle.object/src/com/oracle/truffle/object/LocationImpl.java Wed Nov 18 20:38:32 2015 -0800 @@ -22,12 +22,9 @@ */ package com.oracle.truffle.object; -import com.oracle.truffle.api.object.BooleanLocation; -import com.oracle.truffle.api.object.DoubleLocation; import com.oracle.truffle.api.object.DynamicObject; import com.oracle.truffle.api.object.FinalLocationException; import com.oracle.truffle.api.object.IncompatibleLocationException; -import com.oracle.truffle.api.object.IntLocation; import com.oracle.truffle.api.object.Location; import com.oracle.truffle.api.object.LongLocation; import com.oracle.truffle.api.object.ObjectLocation; @@ -130,8 +127,7 @@ @Override public String toString() { String finalString = isFinal() ? "f" : ""; - String typeString = this instanceof IntLocation ? "i" : (this instanceof DoubleLocation ? "d" : (this instanceof BooleanLocation ? "b" - : (this instanceof TypedLocation ? ((TypedLocation) this).getType().getSimpleName() : "o"))); + String typeString = (this instanceof TypedLocation ? ((TypedLocation) this).getType().getSimpleName() : "Object"); return finalString + typeString + getWhereString(); } diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.object/src/com/oracle/truffle/object/ShapeImpl.java --- a/truffle/com.oracle.truffle.object/src/com/oracle/truffle/object/ShapeImpl.java Wed Nov 18 18:42:56 2015 -0800 +++ b/truffle/com.oracle.truffle.object/src/com/oracle/truffle/object/ShapeImpl.java Wed Nov 18 20:38:32 2015 -0800 @@ -313,10 +313,14 @@ } protected final ShapeImpl queryTransition(Transition transition) { + return queryTransition(transition, true); + } + + protected final ShapeImpl queryTransition(Transition transition, boolean ensureValid) { ShapeImpl cachedShape = this.getTransitionMapForRead().get(transition); if (cachedShape != null) { // Shape already exists? shapeCacheHitCount.inc(); - return layout.getStrategy().ensureValid(cachedShape); + return ensureValid ? layout.getStrategy().ensureValid(cachedShape) : cachedShape; } shapeCacheMissCount.inc(); @@ -334,7 +338,7 @@ public ShapeImpl addProperty(Property property) { assert isValid(); onPropertyTransition(property); - return addPropertyInternal(property); + return addPropertyInternal(property, true); } private void onPropertyTransition(Property property) { @@ -385,12 +389,12 @@ * * @see #addProperty(Property) */ - private ShapeImpl addPropertyInternal(Property prop) { + private ShapeImpl addPropertyInternal(Property prop, boolean ensureValid) { CompilerAsserts.neverPartOfCompilation(); assert prop.isShadow() || !(this.hasProperty(prop.getKey())) : "duplicate property " + prop.getKey(); AddPropertyTransition addTransition = new AddPropertyTransition(prop); - ShapeImpl cachedShape = queryTransition(addTransition); + ShapeImpl cachedShape = queryTransition(addTransition, ensureValid); if (cachedShape != null) { return cachedShape; } @@ -709,7 +713,7 @@ public final ShapeImpl applyTransition(Transition transition, boolean append) { if (transition instanceof AddPropertyTransition) { - return append ? append(((AddPropertyTransition) transition).getProperty()) : addProperty(((AddPropertyTransition) transition).getProperty()); + return append ? append(((AddPropertyTransition) transition).getProperty()) : addPropertyInternal(((AddPropertyTransition) transition).getProperty(), false); } else if (transition instanceof ObjectTypeTransition) { return changeType(((ObjectTypeTransition) transition).getObjectType()); } else if (transition instanceof ReservePrimitiveArrayTransition) { @@ -738,41 +742,7 @@ */ @Override public ShapeImpl replaceProperty(Property oldProperty, Property newProperty) { - return indirectReplaceProperty(oldProperty, newProperty); - } - - protected final ShapeImpl indirectReplaceProperty(Property oldProperty, Property newProperty) { - assert oldProperty.getKey().equals(newProperty.getKey()); - - Transition replacePropertyTransition = new Transition.IndirectReplacePropertyTransition(oldProperty, newProperty); - ShapeImpl cachedShape = queryTransition(replacePropertyTransition); - if (cachedShape != null) { - return cachedShape; - } - - ShapeImpl top = this; - List transitionList = new ArrayList<>(); - boolean found = false; - while (top != getRoot() && !found) { - Transition transition = top.getTransitionFromParent(); - transitionList.add(transition); - if (transition instanceof AddPropertyTransition && ((AddPropertyTransition) transition).getProperty().getKey().equals(newProperty.getKey())) { - found = true; - } - top = top.parent; - } - ShapeImpl newShape = top; - for (ListIterator iterator = transitionList.listIterator(transitionList.size()); iterator.hasPrevious();) { - Transition transition = iterator.previous(); - if (transition instanceof AddPropertyTransition && ((AddPropertyTransition) transition).getProperty().getKey().equals(newProperty.getKey())) { - newShape = newShape.addProperty(newProperty); - } else { - newShape = newShape.applyTransition(transition, false); - } - } - - addIndirectTransition(replacePropertyTransition, newShape); - return newShape; + return directReplaceProperty(oldProperty, newProperty); } protected final ShapeImpl directReplaceProperty(Property oldProperty, Property newProperty) { diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.object/src/com/oracle/truffle/object/Transition.java --- a/truffle/com.oracle.truffle.object/src/com/oracle/truffle/object/Transition.java Wed Nov 18 18:42:56 2015 -0800 +++ b/truffle/com.oracle.truffle.object/src/com/oracle/truffle/object/Transition.java Wed Nov 18 20:38:32 2015 -0800 @@ -22,9 +22,10 @@ */ package com.oracle.truffle.object; +import java.util.Objects; + import com.oracle.truffle.api.object.ObjectType; import com.oracle.truffle.api.object.Property; -import java.util.Objects; public abstract class Transition { @Override @@ -90,6 +91,11 @@ public boolean isDirect() { return true; } + + @Override + public String toString() { + return String.format("add(%s)", getProperty()); + } } public static final class RemovePropertyTransition extends PropertyTransition { @@ -101,6 +107,11 @@ public boolean isDirect() { return false; } + + @Override + public String toString() { + return String.format("remove(%s)", getProperty()); + } } public static final class ObjectTypeTransition extends Transition { @@ -131,6 +142,11 @@ public boolean isDirect() { return true; } + + @Override + public String toString() { + return String.format("objectType(%s)", getObjectType()); + } } public abstract static class AbstractReplacePropertyTransition extends PropertyTransition { @@ -161,6 +177,11 @@ result = prime * result + after.hashCode(); return result; } + + @Override + public String toString() { + return String.format("replace(%s,%s)", getPropertyBefore(), getPropertyAfter()); + } } public static final class IndirectReplacePropertyTransition extends AbstractReplacePropertyTransition { diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.object/src/com/oracle/truffle/object/debug/IGVShapeVisitor.java --- a/truffle/com.oracle.truffle.object/src/com/oracle/truffle/object/debug/IGVShapeVisitor.java Wed Nov 18 18:42:56 2015 -0800 +++ b/truffle/com.oracle.truffle.object/src/com/oracle/truffle/object/debug/IGVShapeVisitor.java Wed Nov 18 20:38:32 2015 -0800 @@ -57,7 +57,7 @@ Shape dst = entry.getValue(); ((ShapeImpl) dst).accept(IGVShapeVisitor.this); assert printer.visited(dst); - printer.connectNodes(s, dst, entry.getKey().getShortName()); + printer.connectNodes(s, dst, entry.getKey().toString()); } } } diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java --- a/truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java Wed Nov 18 18:42:56 2015 -0800 +++ b/truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java Wed Nov 18 20:38:32 2015 -0800 @@ -44,10 +44,8 @@ import com.oracle.truffle.api.vm.PolyglotEngine; import com.oracle.truffle.sl.test.instrument.InstrumentationTestMode; import com.oracle.truffle.tck.TruffleTCK; - +import org.junit.After; import static org.junit.Assert.assertTrue; - -import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -161,6 +159,16 @@ } @Override + protected String multiplyCode(String firstName, String secondName) { + // @formatter:off + return + "function multiply(" + firstName + ", " + secondName + ") {\n" + + " return " + firstName + " * " + secondName + ";\n" + + "}\n"; + // @formatter:on + } + + @Override protected String countInvocations() { return "count"; } @@ -170,6 +178,11 @@ return null; } + @Override + protected String evaluateSource() { + return "interopEval"; + } + // // Ignore tests working on floats and double // diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.sl.tools/src/com/oracle/truffle/sl/tools/SLCoverage.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/truffle/com.oracle.truffle.sl.tools/src/com/oracle/truffle/sl/tools/SLCoverage.java Wed Nov 18 20:38:32 2015 -0800 @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * The Universal Permissive License (UPL), Version 1.0 + * + * Subject to the condition set forth below, permission is hereby granted to any + * person obtaining a copy of this software, associated documentation and/or + * data (collectively the "Software"), free of charge and under any and all + * copyright rights in the Software, and any and all patent rights owned or + * freely licensable by each licensor hereunder covering either (i) the + * unmodified Software as contributed to or provided by such licensor, or (ii) + * the Larger Works (as defined below), to deal in both + * + * (a) the Software, and + * + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if + * one is included with the Software each a "Larger Work" to which the Software + * is contributed by such licensors), + * + * without restriction, including without limitation the rights to copy, create + * derivative works of, display, perform, and distribute the Software and make, + * use, sell, offer for sale, import, export, have made, and have sold the + * Software and the Larger Work(s), and to sublicense the foregoing rights on + * either these or other terms. + * + * This license is subject to the following condition: + * + * The above copyright notice and either this complete permission notice or at a + * minimum a reference to the UPL must be included in all copies or substantial + * portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.oracle.truffle.sl.tools; + +import java.io.IOException; +import java.lang.reflect.Field; + +import com.oracle.truffle.api.instrument.Instrumenter; +import com.oracle.truffle.api.source.Source; +import com.oracle.truffle.api.vm.PolyglotEngine; +import com.oracle.truffle.api.vm.PolyglotEngine.Value; +import com.oracle.truffle.tools.CoverageTracker; + +/** + * Temporary demonstration of code coverage counting. + *

+ * This reflective access will become unnecessary when tool-related PolyglotEngine APIs are in + * place. + */ +public class SLCoverage { + public static void main(String[] args) throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { + PolyglotEngine vm = PolyglotEngine.newBuilder().build(); + assert vm.getLanguages().containsKey("application/x-sl"); + final Field field = PolyglotEngine.class.getDeclaredField("instrumenter"); + field.setAccessible(true); + final Instrumenter instrumenter = (Instrumenter) field.get(vm); + final CoverageTracker coverageTracker = new CoverageTracker(); + instrumenter.install(coverageTracker); + + int repeats = 1; + if (args.length >= 2) { + repeats = Integer.parseInt(args[1]); + } + + if (args.length == 0) { + throw new IOException("No SL source file specified."); + } + final Source source = Source.fromFileName(args[0]); + + vm.eval(source); + Value main = vm.findGlobalSymbol("main"); + if (main == null) { + throw new IOException("No function main() defined in SL source file."); + } + while (repeats-- > 0) { + main.invoke(null); + } + coverageTracker.print(System.out); + } +} diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/SLLanguage.java --- a/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/SLLanguage.java Wed Nov 18 18:42:56 2015 -0800 +++ b/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/SLLanguage.java Wed Nov 18 20:38:32 2015 -0800 @@ -57,12 +57,16 @@ import com.oracle.truffle.api.dsl.NodeFactory; import com.oracle.truffle.api.dsl.UnsupportedSpecializationException; import com.oracle.truffle.api.frame.MaterializedFrame; +import com.oracle.truffle.api.frame.VirtualFrame; import com.oracle.truffle.api.instrument.Visualizer; import com.oracle.truffle.api.instrument.WrapperNode; +import com.oracle.truffle.api.interop.ForeignAccess; +import com.oracle.truffle.api.interop.Message; import com.oracle.truffle.api.nodes.GraphPrintVisitor; import com.oracle.truffle.api.nodes.Node; import com.oracle.truffle.api.nodes.NodeInfo; import com.oracle.truffle.api.nodes.NodeUtil; +import com.oracle.truffle.api.nodes.RootNode; import com.oracle.truffle.api.source.Source; import com.oracle.truffle.api.source.SourceSection; import com.oracle.truffle.api.vm.PolyglotEngine; @@ -199,7 +203,7 @@ protected SLContext createContext(Env env) { final BufferedReader in = new BufferedReader(new InputStreamReader(env.in())); final PrintWriter out = new PrintWriter(env.out(), true); - SLContext context = new SLContext(this, in, out); + SLContext context = new SLContext(this, env, in, out); for (NodeFactory builtin : builtins) { context.installBuiltin(builtin, true); } @@ -391,7 +395,7 @@ } @Override - protected CallTarget parse(Source code, Node node, String... argumentNames) throws IOException { + protected CallTarget parse(Source code, final Node node, String... argumentNames) throws IOException { final SLContext c = new SLContext(this); final Exception[] failed = {null}; try { @@ -400,10 +404,10 @@ } catch (Exception e) { failed[0] = e; } - return new CallTarget() { + RootNode rootNode = new RootNode(SLLanguage.class, null, null) { @TruffleBoundary @Override - public Object call(Object... arguments) { + public Object execute(VirtualFrame frame) { if (failed[0] instanceof RuntimeException) { throw (RuntimeException) failed[0]; } @@ -413,17 +417,26 @@ Node n = createFindContextNode(); SLContext fillIn = findContext(n); final SLFunctionRegistry functionRegistry = fillIn.getFunctionRegistry(); + int oneAndCnt = 0; + SLFunction oneAndOnly = null; for (SLFunction f : c.getFunctionRegistry().getFunctions()) { RootCallTarget callTarget = f.getCallTarget(); if (callTarget == null) { continue; } - functionRegistry.lookup(f.getName()); + oneAndOnly = functionRegistry.lookup(f.getName()); + oneAndCnt++; functionRegistry.register(f.getName(), (SLRootNode) f.getCallTarget().getRootNode()); } + Object[] arguments = frame.getArguments(); + if (oneAndCnt == 1 && (arguments.length > 0 || node != null)) { + Node callNode = Message.createExecute(arguments.length).createNode(); + return ForeignAccess.execute(callNode, frame, oneAndOnly, arguments); + } return null; } }; + return Truffle.getRuntime().createCallTarget(rootNode); } @Override diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/builtins/SLEvalBuiltin.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/builtins/SLEvalBuiltin.java Wed Nov 18 20:38:32 2015 -0800 @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * The Universal Permissive License (UPL), Version 1.0 + * + * Subject to the condition set forth below, permission is hereby granted to any + * person obtaining a copy of this software, associated documentation and/or + * data (collectively the "Software"), free of charge and under any and all + * copyright rights in the Software, and any and all patent rights owned or + * freely licensable by each licensor hereunder covering either (i) the + * unmodified Software as contributed to or provided by such licensor, or (ii) + * the Larger Works (as defined below), to deal in both + * + * (a) the Software, and + * + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if + * one is included with the Software each a "Larger Work" to which the Software + * is contributed by such licensors), + * + * without restriction, including without limitation the rights to copy, create + * derivative works of, display, perform, and distribute the Software and make, + * use, sell, offer for sale, import, export, have made, and have sold the + * Software and the Larger Work(s), and to sublicense the foregoing rights on + * either these or other terms. + * + * This license is subject to the following condition: + * + * The above copyright notice and either this complete permission notice or at a + * minimum a reference to the UPL must be included in all copies or substantial + * portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.oracle.truffle.sl.builtins; + +import com.oracle.truffle.api.dsl.Specialization; +import com.oracle.truffle.api.nodes.NodeInfo; +import com.oracle.truffle.api.source.Source; +import com.oracle.truffle.api.source.SourceSection; +import com.oracle.truffle.sl.SLLanguage; +import java.io.IOException; + +/** + * Builtin function to parse a text in other language. + */ +@NodeInfo(shortName = "interopEval") +public abstract class SLEvalBuiltin extends SLBuiltinNode { + + public SLEvalBuiltin() { + super(SourceSection.createUnavailable(SLLanguage.builtinKind, "interopEval")); + } + + @Specialization + public Object interopEval(String mimeType, String code) { + Source source = Source.fromText(code, "").withMimeType(mimeType); + try { + return getContext().evalAny(source); + } catch (IOException ex) { + throw new IllegalArgumentException(ex); + } + } +} diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/instrument/SLASTPrinter.java diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/runtime/SLContext.java --- a/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/runtime/SLContext.java Wed Nov 18 18:42:56 2015 -0800 +++ b/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/runtime/SLContext.java Wed Nov 18 20:38:32 2015 -0800 @@ -41,6 +41,7 @@ package com.oracle.truffle.sl.runtime; import com.oracle.truffle.api.ExecutionContext; +import com.oracle.truffle.api.TruffleLanguage; import com.oracle.truffle.api.dsl.NodeFactory; import com.oracle.truffle.api.frame.FrameDescriptor; import com.oracle.truffle.api.nodes.NodeInfo; @@ -54,6 +55,7 @@ import com.oracle.truffle.sl.builtins.SLAssertTrueBuiltinFactory; import com.oracle.truffle.sl.builtins.SLBuiltinNode; import com.oracle.truffle.sl.builtins.SLDefineFunctionBuiltinFactory; +import com.oracle.truffle.sl.builtins.SLEvalBuiltinFactory; import com.oracle.truffle.sl.builtins.SLHelloEqualsWorldBuiltinFactory; import com.oracle.truffle.sl.builtins.SLNanoTimeBuiltinFactory; import com.oracle.truffle.sl.builtins.SLNewObjectBuiltinFactory; @@ -69,6 +71,7 @@ import com.oracle.truffle.sl.parser.SLNodeFactory; import java.io.BufferedReader; +import java.io.IOException; import java.io.PrintWriter; import java.math.BigInteger; @@ -90,19 +93,21 @@ private final PrintWriter output; private final SLFunctionRegistry functionRegistry; private final Shape emptyShape; + private final TruffleLanguage.Env env; - public SLContext(SLLanguage language, BufferedReader input, PrintWriter output) { - this(language, input, output, true); + public SLContext(SLLanguage language, TruffleLanguage.Env env, BufferedReader input, PrintWriter output) { + this(language, env, input, output, true); } public SLContext(SLLanguage language) { - this(language, null, null, false); + this(language, null, null, null, false); } - private SLContext(SLLanguage language, BufferedReader input, PrintWriter output, boolean installBuiltins) { + private SLContext(SLLanguage language, TruffleLanguage.Env env, BufferedReader input, PrintWriter output, boolean installBuiltins) { this.language = language; this.input = input; this.output = output; + this.env = env; this.functionRegistry = new SLFunctionRegistry(); installBuiltins(installBuiltins); @@ -150,6 +155,7 @@ installBuiltin(SLAssertTrueBuiltinFactory.getInstance(), registerRootNodes); installBuiltin(SLAssertFalseBuiltinFactory.getInstance(), registerRootNodes); installBuiltin(SLNewObjectBuiltinFactory.getInstance(), registerRootNodes); + installBuiltin(SLEvalBuiltinFactory.getInstance(), registerRootNodes); } public void installBuiltin(NodeFactory factory, boolean registerRootNodes) { @@ -227,4 +233,8 @@ } return a; } + + public Object evalAny(Source source) throws IOException { + return env.parse(source).call(); + } } diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TckLanguage.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TckLanguage.java Wed Nov 18 20:38:32 2015 -0800 @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2015, 2015, 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.tck; + +import com.oracle.truffle.api.CallTarget; +import com.oracle.truffle.api.Truffle; +import com.oracle.truffle.api.TruffleLanguage; +import com.oracle.truffle.api.TruffleLanguage.Env; +import com.oracle.truffle.api.frame.MaterializedFrame; +import com.oracle.truffle.api.frame.VirtualFrame; +import com.oracle.truffle.api.instrument.Visualizer; +import com.oracle.truffle.api.instrument.WrapperNode; +import com.oracle.truffle.api.interop.ForeignAccess; +import com.oracle.truffle.api.interop.Message; +import com.oracle.truffle.api.interop.TruffleObject; +import com.oracle.truffle.api.nodes.Node; +import com.oracle.truffle.api.nodes.RootNode; +import com.oracle.truffle.api.source.Source; +import java.io.IOException; + +@TruffleLanguage.Registration(mimeType = "application/x-tck", name = "TCK", version = "1.0") +public final class TckLanguage extends TruffleLanguage { + public static final TckLanguage INSTANCE = new TckLanguage(); + + @Override + protected Env createContext(Env env) { + return env; + } + + @Override + protected CallTarget parse(Source code, Node context, String... argumentNames) throws IOException { + final RootNode root; + final String txt = code.getCode(); + if (txt.startsWith("TCK42:")) { + int nextColon = txt.indexOf(":", 6); + String mimeType = txt.substring(6, nextColon); + Source toParse = Source.fromText(txt.substring(nextColon + 1), "").withMimeType(mimeType); + root = new MultiplyNode(toParse); + } else { + final double value = Double.parseDouble(txt); + root = RootNode.createConstantNode(value); + } + return Truffle.getRuntime().createCallTarget(root); + } + + @Override + protected Object findExportedSymbol(Env context, String globalName, boolean onlyExplicit) { + return null; + } + + @Override + protected Object getLanguageGlobal(Env context) { + return null; + } + + @Override + protected boolean isObjectOfLanguage(Object object) { + return false; + } + + @Override + protected Visualizer getVisualizer() { + return null; + } + + @Override + protected boolean isInstrumentable(Node node) { + return false; + } + + @Override + protected WrapperNode createWrapperNode(Node node) { + throw new UnsupportedOperationException(); + } + + @Override + protected Object evalInContext(Source source, Node node, MaterializedFrame mFrame) throws IOException { + throw new IOException(); + } + + private static final class MultiplyNode extends RootNode implements TruffleObject, ForeignAccess.Factory { + private final Source code; + + public MultiplyNode(Source toParse) { + super(TckLanguage.class, null, null); + this.code = toParse; + } + + @Override + public Object execute(VirtualFrame frame) { + Env env = TckLanguage.INSTANCE.findContext(TckLanguage.INSTANCE.createFindContextNode()); + if (frame.getArguments().length == 0) { + return this; + } + try { + CallTarget call = env.parse(code, (String) frame.getArguments()[1], (String) frame.getArguments()[2]); + return call.call(6, 7); + } catch (IOException ex) { + throw new AssertionError("Cannot parse " + code, ex); + } + } + + @Override + public ForeignAccess getForeignAccess() { + return ForeignAccess.create(this); + } + + @Override + public boolean canHandle(TruffleObject obj) { + return obj instanceof MultiplyNode; + } + + @Override + public CallTarget accessMessage(Message tree) { + if (tree == Message.IS_EXECUTABLE) { + return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(Boolean.TRUE)); + } else if (Message.createExecute(2).equals(tree)) { + return Truffle.getRuntime().createCallTarget(this); + } else { + throw new IllegalArgumentException("" + tree); + } + } + + } +} diff -r 57afe7055486 -r 5573f12b94f8 truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java --- a/truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java Wed Nov 18 18:42:56 2015 -0800 +++ b/truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java Wed Nov 18 20:38:32 2015 -0800 @@ -33,13 +33,14 @@ import java.io.IOException; import java.lang.reflect.Field; import java.util.Random; +import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import org.junit.Test; /** * A collection of tests that can certify language implementation to be compliant with most recent @@ -157,6 +158,32 @@ } /** + * Name of a function to parse source written in some other language. When the function is + * executed, it expects two arguments. First one is MIME type identifying + * {@link TruffleLanguage} and the second one is the source code to parse in that language and + * execute it. The result of the execution is then returned back to the caller. + * + * @return name of globally exported symbol to invoke when one wants to execute some code + */ + protected String evaluateSource() { + throw new UnsupportedOperationException("evaluateSource() method not implemented"); + } + + /** + * Code snippet to multiplyCode two two variables. The test uses the snippet as a parameter to + * your language's + * {@link TruffleLanguage#parse(com.oracle.truffle.api.source.Source, com.oracle.truffle.api.nodes.Node, java.lang.String...)} + * method. + * + * @param firstName name of the first variable to multiplyCode + * @param secondName name of the second variable to multiplyCode + * @return code snippet that multiplies the two variables in your language + */ + protected String multiplyCode(String firstName, String secondName) { + throw new UnsupportedOperationException("multiply(String,String) method not implemeted!"); + } + + /** * Name of a function that counts number of its invocations in current {@link PolyglotEngine} * context. Your function should somehow keep a counter to remember number of its invocations * and always increment it. The first invocation should return 1, the second @@ -594,6 +621,34 @@ assertEquals("Global from the language same with Java obtained one", language.getGlobalObject().get(), global); } + @Test + public void testEvaluateSource() throws Exception { + Language language = vm().getLanguages().get(mimeType()); + assertNotNull("Langugage for " + mimeType() + " found", language); + + PolyglotEngine.Value function = vm().findGlobalSymbol(evaluateSource()); + assertNotNull(evaluateSource() + " found", function); + + double expect = Math.floor(RANDOM.nextDouble() * 100000.0) / 10.0; + Object parsed = function.invoke(null, "application/x-tck", "" + expect).get(); + assertTrue("Expecting numeric result, was:" + expect, parsed instanceof Number); + double value = ((Number) parsed).doubleValue(); + assertEquals("Gets the double", expect, value, 0.01); + } + + @Test + public void multiplyTwoVariables() throws Exception { + final String firstVar = "var" + (char) ('A' + RANDOM.nextInt(24)); + final String secondVar = "var" + (char) ('0' + RANDOM.nextInt(10)); + String mulCode = multiplyCode(firstVar, secondVar); + Source source = Source.fromText("TCK42:" + mimeType() + ":" + mulCode, "evaluate " + firstVar + " * " + secondVar).withMimeType("application/x-tck"); + final PolyglotEngine.Value evalSource = vm().eval(source); + final PolyglotEngine.Value invokeMul = evalSource.invoke(null, firstVar, secondVar); + Object result = invokeMul.get(); + assertTrue("Expecting numeric result, was:" + result, result instanceof Number); + assertEquals("Right value", 42, ((Number) result).intValue()); + } + private PolyglotEngine.Value findGlobalSymbol(String name) throws Exception { PolyglotEngine.Value s = vm().findGlobalSymbol(name); assert s != null : "Symbol " + name + " is not found!";