comparison truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/controlflow/SLIfNode.java @ 21951:9c8c0937da41

Moving all sources into truffle subdirectory
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 17 Jun 2015 10:58:08 +0200
parents graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/controlflow/SLIfNode.java@fa5e62620593
children c07e64ecb528
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
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 @NodeInfo(shortName = "if", description = "The node implementing a condional statement")
33 public final class SLIfNode extends SLStatementNode {
34
35 /**
36 * The condition of the {@code if}. This in a {@link SLExpressionNode} because we require a
37 * result value. We do not have a node type that can only return a {@code boolean} value, so
38 * {@link #evaluateCondition executing the condition} can lead to a type error.
39 */
40 @Child private SLExpressionNode conditionNode;
41
42 /** Statement (or {@link SLBlockNode block}) executed when the condition is true. */
43 @Child private SLStatementNode thenPartNode;
44
45 /** Statement (or {@link SLBlockNode block}) executed when the condition is false. */
46 @Child private SLStatementNode elsePartNode;
47
48 /**
49 * Profiling information, collected by the interpreter, capturing the profiling information of
50 * the condition. This allows the compiler to generate better code for conditions that are
51 * always true or always false. Additionally the {@link CountingConditionProfile} implementation
52 * (as opposed to {@link BinaryConditionProfile} implementation) transmits the probability of
53 * the condition to be true to the compiler.
54 */
55 private final ConditionProfile condition = ConditionProfile.createCountingProfile();
56
57 public SLIfNode(SourceSection src, SLExpressionNode conditionNode, SLStatementNode thenPartNode, SLStatementNode elsePartNode) {
58 super(src);
59 this.conditionNode = conditionNode;
60 this.thenPartNode = thenPartNode;
61 this.elsePartNode = elsePartNode;
62 }
63
64 @Override
65 public void executeVoid(VirtualFrame frame) {
66 /*
67 * In the interpreter, record profiling information that the condition was executed and with
68 * which outcome.
69 */
70 if (condition.profile(evaluateCondition(frame))) {
71 /* Execute the then-branch. */
72 thenPartNode.executeVoid(frame);
73 } else {
74 /* Execute the else-branch (which is optional according to the SL syntax). */
75 if (elsePartNode != null) {
76 elsePartNode.executeVoid(frame);
77 }
78 }
79 }
80
81 private boolean evaluateCondition(VirtualFrame frame) {
82 try {
83 /*
84 * The condition must evaluate to a boolean value, so we call the boolean-specialized
85 * execute method.
86 */
87 return conditionNode.executeBoolean(frame);
88 } catch (UnexpectedResultException ex) {
89 /*
90 * The condition evaluated to a non-boolean result. This is a type error in the SL
91 * program. We report it with the same exception that Truffle DSL generated nodes use to
92 * report type errors.
93 */
94 throw new UnsupportedSpecializationException(this, new Node[]{conditionNode}, ex.getResult());
95 }
96 }
97 }