Mercurial > hg > graal-compiler
annotate graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/ThreadSafetyTest.java @ 19051:687479c0cd3e
Process inputs of ProxyNodes in the scheduler.
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Wed, 28 Jan 2015 15:07:54 +0100 |
parents | 6acf45a1e7e2 |
children |
rev | line source |
---|---|
14629
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
1 /* |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
2 * Copyright (c) 2014, 2014, Oracle and/or its affiliates. All rights reserved. |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
4 * |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
7 * published by the Free Software Foundation. |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
8 * |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
13 * accompanied this code). |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
14 * |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
15 * You should have received a copy of the GNU General Public License version |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
18 * |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
21 * questions. |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
22 */ |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
23 package com.oracle.truffle.api.test; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
24 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
25 import static org.junit.Assert.*; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
26 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
27 import java.util.*; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
28 import java.util.concurrent.*; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
29 import java.util.concurrent.atomic.*; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
30 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
31 import org.junit.*; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
32 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
33 import com.oracle.truffle.api.*; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
34 import com.oracle.truffle.api.frame.*; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
35 import com.oracle.truffle.api.nodes.*; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
36 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
37 /** |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
38 * Test node rewriting in a tree shared across multiple threads (run with -ea). |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
39 */ |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
40 public class ThreadSafetyTest { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
41 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
42 @Test |
16560
6acf45a1e7e2
disable ThreadSafetyTest
Lukas Stadler <lukas.stadler@oracle.com>
parents:
15096
diff
changeset
|
43 @Ignore("sporadic failures with \"expected:<1000000> but was:<999999>\"") |
14629
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
44 public void test() throws InterruptedException { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
45 TruffleRuntime runtime = Truffle.getRuntime(); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
46 TestRootNode rootNode1 = new TestRootNode(new RewritingNode(new RewritingNode(new RewritingNode(new RewritingNode(new RewritingNode(new ConstNode(42))))))); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
47 final CallTarget target1 = runtime.createCallTarget(rootNode1); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
48 NodeUtil.verify(rootNode1); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
49 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
50 RecursiveCallNode callNode = new RecursiveCallNode(new ConstNode(42)); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
51 TestRootNode rootNode2 = new TestRootNode(new RewritingNode(new RewritingNode(new RewritingNode(new RewritingNode(new RewritingNode(callNode)))))); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
52 final CallTarget target2 = runtime.createCallTarget(rootNode2); |
15093
5634b199c4da
Truffle: API-change: renamed CallNode to DirectCallNode and added IndirectCallNode.
Christian Humer <christian.humer@gmail.com>
parents:
15064
diff
changeset
|
53 callNode.setCallNode(runtime.createDirectCallNode(target2)); |
14629
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
54 NodeUtil.verify(rootNode2); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
55 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
56 testTarget(target1, 47, 1_000_000); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
57 testTarget(target2, 72, 1_000_000); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
58 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
59 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
60 private static void testTarget(final CallTarget target, final int expectedResult, final int numberOfIterations) throws InterruptedException { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
61 ExecutorService executorService = Executors.newFixedThreadPool(20); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
62 final AtomicInteger ai = new AtomicInteger(); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
63 for (int i = 0; i < numberOfIterations; i++) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
64 executorService.submit(new Runnable() { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
65 public void run() { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
66 try { |
14991
64dcb92ee75a
Truffle: Change signature for Truffle calls from (PackedFrame, Arguments) to (Object[]).
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
14879
diff
changeset
|
67 Object result = target.call(new Object[]{5}); |
14629
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
68 assertEquals(expectedResult, result); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
69 ai.incrementAndGet(); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
70 } catch (Throwable t) { |
15096
dcc0f31b2bfb
Truffle: Make ThreadSafetyTest scream louder if an assertion fails.
Christian Humer <christian.humer@gmail.com>
parents:
15093
diff
changeset
|
71 t.printStackTrace(System.out); |
14629
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
72 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
73 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
74 }); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
75 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
76 executorService.shutdown(); |
14879
69375786ef70
ThreadSafetyTest: increase timeout and check test termination with assertion
Andreas Woess <andreas.woess@jku.at>
parents:
14629
diff
changeset
|
77 executorService.awaitTermination(90, TimeUnit.SECONDS); |
69375786ef70
ThreadSafetyTest: increase timeout and check test termination with assertion
Andreas Woess <andreas.woess@jku.at>
parents:
14629
diff
changeset
|
78 assertTrue("test did not terminate", executorService.isTerminated()); |
14629
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
79 assertEquals(numberOfIterations, ai.get()); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
80 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
81 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
82 static class TestRootNode extends RootNode { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
83 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
84 @Child private ValueNode child; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
85 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
86 public TestRootNode(ValueNode child) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
87 super(null); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
88 this.child = child; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
89 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
90 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
91 @Override |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
92 public Object execute(VirtualFrame frame) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
93 return child.execute(frame); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
94 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
95 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
96 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
97 abstract static class ValueNode extends Node { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
98 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
99 public ValueNode() { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
100 super(null); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
101 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
102 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
103 abstract int execute(VirtualFrame frame); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
104 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
105 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
106 static class RewritingNode extends ValueNode { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
107 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
108 @Child private ValueNode child; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
109 private final Random random; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
110 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
111 public RewritingNode(ValueNode child) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
112 this(child, new Random()); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
113 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
114 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
115 public RewritingNode(ValueNode child, Random random) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
116 this.child = child; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
117 this.random = random; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
118 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
119 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
120 @Override |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
121 int execute(VirtualFrame frame) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
122 boolean replace = random.nextBoolean(); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
123 if (replace) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
124 ValueNode newNode = this.replace(new OtherRewritingNode(child, random)); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
125 return newNode.execute(frame); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
126 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
127 return 1 + child.execute(frame); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
128 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
129 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
130 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
131 static class OtherRewritingNode extends ValueNode { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
132 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
133 @Child private ValueNode child; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
134 private final Random random; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
135 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
136 public OtherRewritingNode(ValueNode child, Random random) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
137 this.child = child; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
138 this.random = random; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
139 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
140 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
141 @Override |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
142 int execute(VirtualFrame frame) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
143 boolean replace = random.nextBoolean(); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
144 if (replace) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
145 ValueNode newNode = this.replace(new RewritingNode(child, random)); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
146 return newNode.execute(frame); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
147 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
148 return 1 + child.execute(frame); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
149 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
150 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
151 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
152 static class ConstNode extends ValueNode { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
153 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
154 private final int value; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
155 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
156 ConstNode(int value) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
157 this.value = value; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
158 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
159 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
160 @Override |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
161 int execute(VirtualFrame frame) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
162 return value; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
163 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
164 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
165 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
166 static class RecursiveCallNode extends ValueNode { |
15093
5634b199c4da
Truffle: API-change: renamed CallNode to DirectCallNode and added IndirectCallNode.
Christian Humer <christian.humer@gmail.com>
parents:
15064
diff
changeset
|
167 @Child DirectCallNode callNode; |
14629
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
168 @Child private ValueNode valueNode; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
169 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
170 RecursiveCallNode(ValueNode value) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
171 this.valueNode = value; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
172 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
173 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
174 @Override |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
175 int execute(VirtualFrame frame) { |
14991
64dcb92ee75a
Truffle: Change signature for Truffle calls from (PackedFrame, Arguments) to (Object[]).
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
14879
diff
changeset
|
176 int arg = (Integer) frame.getArguments()[0]; |
14629
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
177 if (arg > 0) { |
15064
f675818d9ad0
new getStackTrace and getCurrentFrame functionality in TruffleRuntime
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14991
diff
changeset
|
178 return (int) callNode.call(frame, new Object[]{(arg - 1)}); |
14629
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
179 } else { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
180 return valueNode.execute(frame); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
181 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
182 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
183 |
15093
5634b199c4da
Truffle: API-change: renamed CallNode to DirectCallNode and added IndirectCallNode.
Christian Humer <christian.humer@gmail.com>
parents:
15064
diff
changeset
|
184 void setCallNode(DirectCallNode callNode) { |
14629
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
185 this.callNode = insert(callNode); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
186 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
187 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
188 } |