annotate graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/TruffleRuntimeTest.java @ 16992:5a1d764f6afc

Truffle: added support for Runtime#getCallTargets()
author Christian Humer <christian.humer@gmail.com>
date Thu, 28 Aug 2014 15:35:29 +0200
parents 5e3d1a68664e
children 7b45e33b7986
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
1 /*
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
4 *
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
8 *
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
13 * accompanied this code).
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
14 *
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
18 *
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
21 * questions.
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
22 */
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
23 package com.oracle.truffle.api.test;
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
24
16992
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
25 import static org.junit.Assert.*;
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
26
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
27 import java.util.*;
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
28
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
29 import org.junit.*;
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
30
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
31 import com.oracle.truffle.api.*;
16992
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
32 import com.oracle.truffle.api.frame.*;
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
33 import com.oracle.truffle.api.nodes.*;
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
34 import com.oracle.truffle.api.source.*;
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
35
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
36 /**
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
37 * <h3>Accessing the Truffle Runtime</h3>
16992
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
38 *
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
39 * <p>
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
40 * The Truffle runtime can be accessed at any point in time globally using the static method
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7267
diff changeset
41 * {@link Truffle#getRuntime()}. This method is guaranteed to return a non-null Truffle runtime
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7267
diff changeset
42 * object with an identifying name. A Java Virtual Machine implementation can chose to replace the
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7267
diff changeset
43 * default implementation of the {@link TruffleRuntime} interface with its own implementation for
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7267
diff changeset
44 * providing improved performance.
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
45 * </p>
16992
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
46 *
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
47 * <p>
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7267
diff changeset
48 * The next part of the Truffle API introduction is at
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7267
diff changeset
49 * {@link com.oracle.truffle.api.test.RootNodeTest}.
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
50 * </p>
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
51 */
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
52 public class TruffleRuntimeTest {
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
53
16992
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
54 private TruffleRuntime runtime;
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
55
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
56 @Before
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
57 public void setup() {
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
58 this.runtime = Truffle.getRuntime();
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
59 }
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
60
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
61 private static RootNode createTestRootNode() {
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
62 return new RootNode() {
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
63 @Override
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
64 public Object execute(VirtualFrame frame) {
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
65 return 42;
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
66 }
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
67 };
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
68 }
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
69
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
70 @Test
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
71 public void test() {
16992
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
72 assertNotNull(runtime);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
73 assertNotNull(runtime.getName());
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
74 }
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
75
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
76 @Test
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
77 public void testCreateCallTarget() {
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
78 RootNode rootNode = createTestRootNode();
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
79 RootCallTarget target = runtime.createCallTarget(rootNode);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
80 assertNotNull(target);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
81 assertEquals(target.call(), 42);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
82 assertSame(rootNode, target.getRootNode());
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
83 }
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
84
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
85 @Test
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
86 public void testGetCallTargets1() {
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
87 RootNode rootNode = createTestRootNode();
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
88 RootCallTarget target = runtime.createCallTarget(rootNode);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
89 assertTrue(runtime.getCallTargets().indexOf(target) != -1);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
90 }
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
91
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
92 @Test
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
93 public void testGetCallTargets2() {
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
94 RootNode rootNode = createTestRootNode();
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
95 RootCallTarget target1 = runtime.createCallTarget(rootNode);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
96 RootCallTarget target2 = runtime.createCallTarget(rootNode);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
97 assertTrue(runtime.getCallTargets().indexOf(target1) != -1);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
98 assertTrue(runtime.getCallTargets().indexOf(target2) != -1);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
99 }
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
100
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
101 /*
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
102 * This test case documents the use case for profilers and debuggers where they need to access
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
103 * multiple call targets for the same source section. This case may happen when the optimization
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
104 * system decides to duplicate call targets to achieve better performance.
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
105 */
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
106 @Test
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
107 public void testGetCallTargets3() {
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
108 Source source1 = Source.fromText("a\nb\n", "");
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
109 SourceSection sourceSection1 = source1.createSection("foo", 1);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
110 SourceSection sourceSection2 = source1.createSection("bar", 2);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
111
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
112 RootNode rootNode1 = createTestRootNode();
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
113 rootNode1.assignSourceSection(sourceSection1);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
114 RootNode rootNode2 = createTestRootNode();
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
115 rootNode2.assignSourceSection(sourceSection2);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
116 RootNode rootNode2Copy = NodeUtil.cloneNode(rootNode2);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
117
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
118 assertSame(rootNode2.getSourceSection(), rootNode2Copy.getSourceSection());
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
119
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
120 RootCallTarget target1 = runtime.createCallTarget(rootNode1);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
121 RootCallTarget target2 = runtime.createCallTarget(rootNode2);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
122 RootCallTarget target2Copy = runtime.createCallTarget(rootNode2Copy);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
123
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
124 Map<SourceSection, List<RootCallTarget>> groupedTargets = groupUniqueCallTargets();
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
125
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
126 List<RootCallTarget> targets1 = groupedTargets.get(sourceSection1);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
127 assertEquals(1, targets1.size());
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
128 assertEquals(target1, targets1.get(0));
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
129
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
130 List<RootCallTarget> targets2 = groupedTargets.get(sourceSection2);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
131 assertEquals(2, targets2.size());
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
132 // order of targets2 is not guaranteed
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
133 assertTrue(target2 == targets2.get(0) ^ target2Copy == targets2.get(0));
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
134 assertTrue(target2 == targets2.get(1) ^ target2Copy == targets2.get(1));
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
135 }
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
136
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
137 private static Map<SourceSection, List<RootCallTarget>> groupUniqueCallTargets() {
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
138 Map<SourceSection, List<RootCallTarget>> groupedTargets = new HashMap<>();
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
139 for (RootCallTarget target : Truffle.getRuntime().getCallTargets()) {
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
140 SourceSection section = target.getRootNode().getSourceSection();
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
141 if (section == null) {
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
142 // can not identify root node to a unique call target. Print warning?
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
143 continue;
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
144 }
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
145 List<RootCallTarget> targets = groupedTargets.get(section);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
146 if (targets == null) {
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
147 targets = new ArrayList<>();
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
148 groupedTargets.put(section, targets);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
149 }
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
150 targets.add(target);
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
151 }
5a1d764f6afc Truffle: added support for Runtime#getCallTargets()
Christian Humer <christian.humer@gmail.com>
parents: 7530
diff changeset
152 return groupedTargets;
7267
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
153 }
a4b84ba6dc2e Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
154 }