From 81b1b02b815ba013beac08745cd2d8cd9a63259f Mon Sep 17 00:00:00 2001
From: stepan <stepan.sindelar@oracle.com>
Date: Thu, 11 Jan 2018 14:52:07 +0100
Subject: [PATCH] Fix loops debugging.

---
 .../builtin/helpers/BrowserInteractNode.java  | 14 +++++++++++++
 .../nodes/builtin/helpers/DebugHandling.java  | 21 ++++++++++++-------
 mx.fastr/suite.py                             |  2 +-
 3 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/helpers/BrowserInteractNode.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/helpers/BrowserInteractNode.java
index c5507805b9..78e1c31a97 100644
--- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/helpers/BrowserInteractNode.java
+++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/helpers/BrowserInteractNode.java
@@ -131,6 +131,9 @@ public abstract class BrowserInteractNode extends Node {
                         break LW;
                     case "Q":
                         throw new JumpToTopLevelException();
+                    case "help":
+                        printHelp(ch);
+                        break;
                     case "where": {
                         if (currentCaller.getDepth() > 1) {
                             Object stack = Utils.createTraceback(0);
@@ -205,4 +208,15 @@ public abstract class BrowserInteractNode extends Node {
     private static String browserPrompt(int depth) {
         return "Browse[" + depth + "]> ";
     }
+
+    private static void printHelp(ConsoleIO out) {
+        out.println("n          next");
+        out.println("s          step into");
+        out.println("f          finish");
+        out.println("c or cont  continue");
+        out.println("Q          quit");
+        out.println("where      show stack");
+        out.println("help       show help");
+        out.println("<expr>     evaluate expression");
+    }
 }
diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/helpers/DebugHandling.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/helpers/DebugHandling.java
index be71516f86..dee1f1b1ac 100644
--- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/helpers/DebugHandling.java
+++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/helpers/DebugHandling.java
@@ -46,6 +46,7 @@ import com.oracle.truffle.r.nodes.control.AbstractLoopNode;
 import com.oracle.truffle.r.nodes.function.FunctionDefinitionNode;
 import com.oracle.truffle.r.nodes.instrumentation.RInstrumentation;
 import com.oracle.truffle.r.nodes.instrumentation.RSyntaxTags;
+import com.oracle.truffle.r.nodes.instrumentation.RSyntaxTags.LoopTag;
 import com.oracle.truffle.r.runtime.JumpToTopLevelException;
 import com.oracle.truffle.r.runtime.RArguments;
 import com.oracle.truffle.r.runtime.RDeparse;
@@ -515,7 +516,7 @@ public class DebugHandling {
         void setFinishing(AbstractLoopNode loopNode) {
             // Disable every statement listener except that for loopNode
             for (LoopStatementEventListener lser : loopStatementListeners) {
-                if (lser.getLoopNode() == loopNode) {
+                if (lser.handlesLoop(loopNode)) {
                     lser.setFinishing();
                 } else {
                     lser.disable();
@@ -708,24 +709,24 @@ public class DebugHandling {
          * The wrapper for the loop node is stable whereas the loop node itself will be replaced
          * with a specialized node.
          */
-        private final RSyntaxNode loopNode;
+        private final SourceSection loopSourceSection;
         private final FunctionStatementsEventListener fser;
 
         LoopStatementEventListener(FunctionDefinitionNode functionDefinitionNode, Object text, Object condition, RSyntaxNode loopNode, FunctionStatementsEventListener fser) {
             super(functionDefinitionNode, text, condition);
-            this.loopNode = loopNode;
+            this.loopSourceSection = loopNode.getSourceSection();
             this.fser = fser;
         }
 
         @Override
         public void onEnter(EventContext context, VirtualFrame frame) {
-            if (!disabled() && context.getInstrumentedNode() == loopNode) {
+            if (isEnabled(context)) {
                 super.onEnter(context, frame);
             }
         }
 
-        RSyntaxNode getLoopNode() {
-            return loopNode;
+        boolean handlesLoop(RSyntaxNode loop) {
+            return loopSourceSection != null && loopSourceSection.equals(loop.getSourceSection());
         }
 
         void setFinishing() {
@@ -734,7 +735,7 @@ public class DebugHandling {
 
         @Override
         public void onReturnExceptional(EventContext context, VirtualFrame frame, Throwable exception) {
-            if (!disabled() && context.getInstrumentedNode() == loopNode) {
+            if (isEnabled(context)) {
                 CompilerDirectives.transferToInterpreter();
                 returnCleanup();
             }
@@ -742,12 +743,16 @@ public class DebugHandling {
 
         @Override
         public void onReturnValue(EventContext context, VirtualFrame frame, Object result) {
-            if (!disabled() && context.getInstrumentedNode() == loopNode) {
+            if (isEnabled(context)) {
                 CompilerDirectives.transferToInterpreter();
                 returnCleanup();
             }
         }
 
+        private boolean isEnabled(EventContext ctx) {
+            return !disabled() && loopSourceSection != null && loopSourceSection.equals(ctx.getInstrumentedNode().getSourceSection());
+        }
+
         private void returnCleanup() {
             if (finishing) {
                 finishing = false;
diff --git a/mx.fastr/suite.py b/mx.fastr/suite.py
index 75301404a2..1300d03106 100644
--- a/mx.fastr/suite.py
+++ b/mx.fastr/suite.py
@@ -7,7 +7,7 @@ suite = {
             {
                "name" : "truffle",
                "subdir" : True,
-               "version" : "e140680ae7ebc4329e5cd96889258a75b6987dfe",
+               "version" : "b1c4af13e75d2dc839b5050ba020356ec1602788",
                "urls" : [
                     {"url" : "https://github.com/graalvm/graal", "kind" : "git"},
                     {"url" : "https://curio.ssw.jku.at/nexus/content/repositories/snapshots", "kind" : "binary"},
-- 
GitLab