diff graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/controlflow/SLWhileNode.java @ 13836:64c77f0577bb

More documentation and improvements of Simple Language
author Christian Wimmer <christian.wimmer@oracle.com>
date Thu, 30 Jan 2014 17:53:27 -0800
parents b16ec83edc73
children f9b934e1e172
line wrap: on
line diff
--- a/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/controlflow/SLWhileNode.java	Thu Jan 30 17:52:24 2014 -0800
+++ b/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/controlflow/SLWhileNode.java	Thu Jan 30 17:53:27 2014 -0800
@@ -32,13 +32,29 @@
 @NodeInfo(shortName = "while")
 public class SLWhileNode extends SLStatementNode {
 
+    /**
+     * The condition of the loop. This in a {@link SLExpressionNode} because we require a result
+     * value. We do not have a node type that can only return a {@code boolean} value, so
+     * {@link #evaluateCondition executing the condition} can lead to a type error.
+     */
     @Child private SLExpressionNode conditionNode;
+
+    /** Statement (or {@SLBlockNode block}) executed as long as the condition is true. */
     @Child private SLStatementNode bodyNode;
 
+    /**
+     * Profiling information, collected by the interpreter, capturing whether a {@code continue}
+     * statement was used in this loop. This allows the compiler to generate better code for loops
+     * without a {@code continue}.
+     */
     private final BranchProfile continueTaken = new BranchProfile();
     private final BranchProfile breakTaken = new BranchProfile();
 
     public SLWhileNode(SLExpressionNode conditionNode, SLStatementNode bodyNode) {
+        /*
+         * It is a Truffle requirement to call adoptChild(), which performs all the necessary steps
+         * to add the new child to the node tree.
+         */
         this.conditionNode = adoptChild(conditionNode);
         this.bodyNode = adoptChild(bodyNode);
     }
@@ -49,28 +65,31 @@
         try {
             while (evaluateCondition(frame)) {
                 try {
+                    /* Execute the loop body. */
                     bodyNode.executeVoid(frame);
+
                     if (CompilerDirectives.inInterpreter()) {
+                        /* In the interpreter, profile the the number of loop iteration. */
                         count++;
                     }
                 } catch (SLContinueException ex) {
+                    /* In the interpreter, record profiling information that the loop uses continue. */
                     continueTaken.enter();
                     /* Fall through to next loop iteration. */
                 }
             }
         } catch (SLBreakException ex) {
+            /* In the interpreter, record profiling information that the loop uses break. */
             breakTaken.enter();
             /* Done executing this loop, exit method to execute statement following the loop. */
+
         } finally {
             if (CompilerDirectives.inInterpreter()) {
                 /*
-                 * Report the loop count to the Truffle system. It is used for compilation and
-                 * inlining decisions.
+                 * In the interpreter, report the loop count to the Truffle system. It is used for
+                 * compilation and inlining decisions.
                  */
-                RootNode root = getRootNode();
-                if (root.getCallTarget() instanceof LoopCountReceiver) {
-                    ((LoopCountReceiver) root.getCallTarget()).reportLoopCount(count);
-                }
+                getRootNode().reportLoopCount(count);
             }
         }
     }
@@ -78,8 +97,8 @@
     private boolean evaluateCondition(VirtualFrame frame) {
         try {
             /*
-             * The condition must evaluate to a boolean value, so we call boolean-specialized
-             * method.
+             * The condition must evaluate to a boolean value, so we call the boolean-specialized
+             * execute method.
              */
             return conditionNode.executeBoolean(frame);
         } catch (UnexpectedResultException ex) {