Mercurial > hg > graal-compiler
annotate graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/ThreadSafetyTest.java @ 15030:e098038093d5
AbstractBytecodeParser: move createTarget to GraphBuilderPhase.
author | Josef Eisl <josef.eisl@jku.at> |
---|---|
date | Mon, 07 Apr 2014 17:01:54 +0200 |
parents | 64dcb92ee75a |
children | f675818d9ad0 |
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.io.*; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
28 import java.util.*; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
29 import java.util.concurrent.*; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
30 import java.util.concurrent.atomic.*; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
31 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
32 import org.junit.*; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
33 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
34 import com.oracle.truffle.api.*; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
35 import com.oracle.truffle.api.frame.*; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
36 import com.oracle.truffle.api.nodes.*; |
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 /** |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
39 * 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
|
40 */ |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
41 public class ThreadSafetyTest { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
42 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
43 @Test |
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); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
53 callNode.setCallNode(runtime.createCallNode(target2)); |
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) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
71 PrintStream out = System.out; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
72 out.println(t); |
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 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
77 executorService.shutdown(); |
14879
69375786ef70
ThreadSafetyTest: increase timeout and check test termination with assertion
Andreas Woess <andreas.woess@jku.at>
parents:
14629
diff
changeset
|
78 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
|
79 assertTrue("test did not terminate", executorService.isTerminated()); |
14629
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
80 assertEquals(numberOfIterations, ai.get()); |
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 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
83 static class TestRootNode extends RootNode { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
84 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
85 @Child private ValueNode child; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
86 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
87 public TestRootNode(ValueNode child) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
88 super(null); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
89 this.child = child; |
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 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
92 @Override |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
93 public Object execute(VirtualFrame frame) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
94 return child.execute(frame); |
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 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
98 abstract static class ValueNode extends Node { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
99 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
100 public ValueNode() { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
101 super(null); |
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 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
104 abstract int execute(VirtualFrame frame); |
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 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
107 static class RewritingNode extends ValueNode { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
108 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
109 @Child private ValueNode child; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
110 private final Random random; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
111 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
112 public RewritingNode(ValueNode child) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
113 this(child, new Random()); |
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 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
116 public RewritingNode(ValueNode child, Random random) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
117 this.child = child; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
118 this.random = random; |
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 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
121 @Override |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
122 int execute(VirtualFrame frame) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
123 boolean replace = random.nextBoolean(); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
124 if (replace) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
125 ValueNode newNode = this.replace(new OtherRewritingNode(child, random)); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
126 return newNode.execute(frame); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
127 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
128 return 1 + child.execute(frame); |
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 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
132 static class OtherRewritingNode extends ValueNode { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
133 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
134 @Child private ValueNode child; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
135 private final Random random; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
136 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
137 public OtherRewritingNode(ValueNode child, Random random) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
138 this.child = child; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
139 this.random = random; |
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 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
142 @Override |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
143 int execute(VirtualFrame frame) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
144 boolean replace = random.nextBoolean(); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
145 if (replace) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
146 ValueNode newNode = this.replace(new RewritingNode(child, random)); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
147 return newNode.execute(frame); |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
148 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
149 return 1 + child.execute(frame); |
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 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
153 static class ConstNode extends ValueNode { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
154 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
155 private final int value; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
156 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
157 ConstNode(int value) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
158 this.value = value; |
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 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
161 @Override |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
162 int execute(VirtualFrame frame) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
163 return value; |
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 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
167 static class RecursiveCallNode extends ValueNode { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
168 @Child CallNode callNode; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
169 @Child private ValueNode valueNode; |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
170 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
171 RecursiveCallNode(ValueNode value) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
172 this.valueNode = value; |
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 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
175 @Override |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
176 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
|
177 int arg = (Integer) frame.getArguments()[0]; |
14629
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
178 if (arg > 0) { |
14991
64dcb92ee75a
Truffle: Change signature for Truffle calls from (PackedFrame, Arguments) to (Object[]).
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
14879
diff
changeset
|
179 return (int) callNode.call(new Object[]{(arg - 1)}); |
14629
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
180 } else { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
181 return valueNode.execute(frame); |
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 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
184 |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
185 void setCallNode(CallNode callNode) { |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
186 this.callNode = insert(callNode); |
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 } |
ba52fbec5b6c
Truffle: atomic node rewriting
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
189 } |