comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/controlflow/SLWhileNode.java @ 13821:b16ec83edc73

Documentation and more refactoring of Simple Language
author Christian Wimmer <christian.wimmer@oracle.com>
date Wed, 29 Jan 2014 20:45:43 -0800
parents 7c418666c6c9
children 64c77f0577bb
comparison
equal deleted inserted replaced
13820:20e7727588e8 13821:b16ec83edc73
1 /* 1 /*
2 * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved. 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. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 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 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
20 * or visit www.oracle.com if you need additional information or have any 20 * or visit www.oracle.com if you need additional information or have any
21 * questions. 21 * questions.
22 */ 22 */
23 package com.oracle.truffle.sl.nodes.controlflow; 23 package com.oracle.truffle.sl.nodes.controlflow;
24 24
25 import com.oracle.truffle.api.*;
26 import com.oracle.truffle.api.dsl.*;
25 import com.oracle.truffle.api.frame.*; 27 import com.oracle.truffle.api.frame.*;
28 import com.oracle.truffle.api.nodes.*;
26 import com.oracle.truffle.api.utilities.*; 29 import com.oracle.truffle.api.utilities.*;
27 import com.oracle.truffle.sl.nodes.*; 30 import com.oracle.truffle.sl.nodes.*;
28 31
32 @NodeInfo(shortName = "while")
29 public class SLWhileNode extends SLStatementNode { 33 public class SLWhileNode extends SLStatementNode {
30 34
31 @Child private SLExpressionNode condition; 35 @Child private SLExpressionNode conditionNode;
32 @Child private SLStatementNode body; 36 @Child private SLStatementNode bodyNode;
33 37
34 private final BranchProfile continueTaken = new BranchProfile(); 38 private final BranchProfile continueTaken = new BranchProfile();
35 private final BranchProfile breakTaken = new BranchProfile(); 39 private final BranchProfile breakTaken = new BranchProfile();
36 40
37 public SLWhileNode(SLExpressionNode condition, SLStatementNode body) { 41 public SLWhileNode(SLExpressionNode conditionNode, SLStatementNode bodyNode) {
38 this.condition = adoptChild(condition); 42 this.conditionNode = adoptChild(conditionNode);
39 this.body = adoptChild(body); 43 this.bodyNode = adoptChild(bodyNode);
40 } 44 }
41 45
42 @Override 46 @Override
43 public void executeVoid(VirtualFrame frame) { 47 public void executeVoid(VirtualFrame frame) {
48 int count = 0;
44 try { 49 try {
45 while (condition.executeCondition(frame)) { 50 while (evaluateCondition(frame)) {
46 try { 51 try {
47 body.executeVoid(frame); 52 bodyNode.executeVoid(frame);
53 if (CompilerDirectives.inInterpreter()) {
54 count++;
55 }
48 } catch (SLContinueException ex) { 56 } catch (SLContinueException ex) {
49 continueTaken.enter(); 57 continueTaken.enter();
50 /* Fall through to next loop iteration. */ 58 /* Fall through to next loop iteration. */
51 } 59 }
52 } 60 }
53 } catch (SLBreakException ex) { 61 } catch (SLBreakException ex) {
54 breakTaken.enter(); 62 breakTaken.enter();
55 /* Done executing this loop, exit method to execute statement following the loop. */ 63 /* Done executing this loop, exit method to execute statement following the loop. */
64 } finally {
65 if (CompilerDirectives.inInterpreter()) {
66 /*
67 * Report the loop count to the Truffle system. It is used for compilation and
68 * inlining decisions.
69 */
70 RootNode root = getRootNode();
71 if (root.getCallTarget() instanceof LoopCountReceiver) {
72 ((LoopCountReceiver) root.getCallTarget()).reportLoopCount(count);
73 }
74 }
75 }
76 }
77
78 private boolean evaluateCondition(VirtualFrame frame) {
79 try {
80 /*
81 * The condition must evaluate to a boolean value, so we call boolean-specialized
82 * method.
83 */
84 return conditionNode.executeBoolean(frame);
85 } catch (UnexpectedResultException ex) {
86 /*
87 * The condition evaluated to a non-boolean result. This is a type error in the SL
88 * program. We report it with the same exception that Truffle DSL generated nodes use to
89 * report type errors.
90 */
91 throw new UnsupportedSpecializationException(this, ex.getResult());
56 } 92 }
57 } 93 }
58 } 94 }