Mercurial > hg > graal-jvmci-8
annotate graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/controlflow/SLRepeatingNode.java @ 16917:6af9d523222a
SL: use new LoopNode API.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Sat, 23 Aug 2014 19:31:44 +0200 |
parents | |
children | 87ea195b66ff |
rev | line source |
---|---|
16917
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
1 /* |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
4 * |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
7 * published by the Free Software Foundation. |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
8 * |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
13 * accompanied this code). |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
14 * |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
15 * You should have received a copy of the GNU General Public License version |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
18 * |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
21 * questions. |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
22 */ |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
23 package com.oracle.truffle.sl.nodes.controlflow; |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
24 |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
25 import com.oracle.truffle.api.dsl.*; |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
26 import com.oracle.truffle.api.frame.*; |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
27 import com.oracle.truffle.api.nodes.*; |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
28 import com.oracle.truffle.api.source.*; |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
29 import com.oracle.truffle.api.utilities.*; |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
30 import com.oracle.truffle.sl.nodes.*; |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
31 |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
32 public final class SLRepeatingNode extends Node implements RepeatingNode { |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
33 |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
34 /** |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
35 * The condition of the loop. This in a {@link SLExpressionNode} because we require a result |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
36 * value. We do not have a node type that can only return a {@code boolean} value, so |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
37 * {@link #evaluateCondition executing the condition} can lead to a type error. |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
38 */ |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
39 @Child private SLExpressionNode conditionNode; |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
40 |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
41 /** Statement (or {@link SLBlockNode block}) executed as long as the condition is true. */ |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
42 @Child private SLStatementNode bodyNode; |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
43 |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
44 /** |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
45 * Profiling information, collected by the interpreter, capturing whether a {@code continue} |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
46 * statement was used in this loop. This allows the compiler to generate better code for loops |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
47 * without a {@code continue}. |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
48 */ |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
49 private final BranchProfile continueTaken = new BranchProfile(); |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
50 private final BranchProfile breakTaken = new BranchProfile(); |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
51 |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
52 public SLRepeatingNode(SourceSection src, SLExpressionNode conditionNode, SLStatementNode bodyNode) { |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
53 super(src); |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
54 this.conditionNode = conditionNode; |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
55 this.bodyNode = bodyNode; |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
56 } |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
57 |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
58 public boolean executeRepeating(VirtualFrame frame) { |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
59 if (evaluateCondition(frame)) { |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
60 try { |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
61 /* Execute the loop body. */ |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
62 bodyNode.executeVoid(frame); |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
63 } catch (SLContinueException ex) { |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
64 /* In the interpreter, record profiling information that the loop uses continue. */ |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
65 continueTaken.enter(); |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
66 /* Fall through to next loop iteration. */ |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
67 } catch (SLBreakException ex) { |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
68 /* In the interpreter, record profiling information that the loop uses break. */ |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
69 breakTaken.enter(); |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
70 /* Done executing this loop, exit method to execute statement following the loop. */ |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
71 return false; |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
72 } |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
73 return true; |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
74 } else { |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
75 return false; |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
76 } |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
77 } |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
78 |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
79 private boolean evaluateCondition(VirtualFrame frame) { |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
80 try { |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
81 /* |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
82 * The condition must evaluate to a boolean value, so we call the boolean-specialized |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
83 * execute method. |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
84 */ |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
85 return (conditionNode.executeBoolean(frame)); |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
86 } catch (UnexpectedResultException ex) { |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
87 /* |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
88 * The condition evaluated to a non-boolean result. This is a type error in the SL |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
89 * program. We report it with the same exception that Truffle DSL generated nodes use to |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
90 * report type errors. |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
91 */ |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
92 throw new UnsupportedSpecializationException(this, new Node[]{conditionNode}, ex.getResult()); |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
93 } |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
94 } |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
95 |
6af9d523222a
SL: use new LoopNode API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
96 } |