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