Commits

Shashank Bharadwaj committed 84be967

removed old code, need to do a pass of renaming, plus test_descr crashes need to investigate

Comments (0)

Files changed (1)

fix-asm-via-cfg.patch

  import org.python.antlr.ast.ExceptHandler;
  import org.python.antlr.ast.Exec;
  import org.python.antlr.ast.Expr;
-@@ -59,12 +63,15 @@
+@@ -59,12 +63,14 @@
  import org.python.antlr.ast.Str;
  import org.python.antlr.ast.Subscript;
  import org.python.antlr.ast.Suite;
  import org.python.antlr.ast.Tuple;
  import org.python.antlr.ast.UnaryOp;
  import org.python.antlr.ast.While;
- import org.python.antlr.ast.With;
+-import org.python.antlr.ast.With;
 +import org.python.antlr.ast.WithExit;
 +import org.python.antlr.ast.WithSpecialAssign;
  import org.python.antlr.ast.Yield;
  import org.python.antlr.ast.alias;
  import org.python.antlr.ast.cmpopType;
-@@ -627,8 +634,6 @@
+@@ -122,7 +128,6 @@
+     private boolean optimizeGlobals = true;
+     private String className;
+     private Stack<Label> continueLabels, breakLabels;
+-    private Stack<ExceptionHandler> exceptionHandlers;
+     private Vector<Label> yields = new Vector<Label>();
+ 
+     /*
+@@ -133,7 +138,6 @@
+      * PyCode, in other words: each 'function'.  When returning through
+      * finally's all the exceptionHandlers are executed.
+      */
+-    private int bcfLevel = 0;
+     private int yield_count = 0;
+     private Stack<String> stack = new Stack<String>();
+ 
+@@ -143,7 +147,6 @@
+ 
+         continueLabels = new Stack<Label>();
+         breakLabels = new Stack<Label>();
+-        exceptionHandlers = new Stack<ExceptionHandler>();
+     }
+ 
+     public void getNone() throws IOException {
+@@ -627,8 +630,6 @@
              throw new ParseException("'break' outside loop", node);
          }
  
          code.goto_(breakLabels.peek());
          return null;
      }
-@@ -640,8 +645,6 @@
+@@ -640,8 +641,6 @@
              throw new ParseException("'continue' not properly in loop", node);
          }
  
          code.goto_(continueLabels.peek());
          return Exit;
      }
-@@ -783,7 +786,7 @@
+@@ -783,26 +782,16 @@
      /**
       *  Close all the open exception handler ranges.  This should be paired
       *  with restartExceptionHandlers to delimit internal code that
       *  variables without the verifier thinking we might jump out of our
       *  handling with an exception.
       */
-@@ -901,7 +904,7 @@
+     private void endExceptionHandlers() {
+-        Label end = new Label();
+-        code.label(end);
+-        for (int i = 0; i < exceptionHandlers.size(); ++i) {
+-            ExceptionHandler handler = exceptionHandlers.elementAt(i);
+-            handler.exceptionEnds.addElement(end);
+-        }
++        // FIXME(shashank): move this functionality into IRBuilder
+     }
+ 
+     private void restartExceptionHandlers() {
+-        Label start = new Label();
+-        code.label(start);
+-        for (int i = 0; i < exceptionHandlers.size(); ++i) {
+-            ExceptionHandler handler = exceptionHandlers.elementAt(i);
+-            handler.exceptionStarts.addElement(start);
+-        }
++        // FIXME(shashank): move this functionality into IRBuilder
+     }
+ 
+     private void saveLocals() throws Exception {
+@@ -854,7 +843,6 @@
+             tmp = code.getReturnLocal();
+             code.astore(tmp);
+         }
+-        doFinallysDownTo(0);
+ 
+         setLastI(-1);
+ 
+@@ -901,7 +889,7 @@
      }
  
      /**
       */
      private void defaultImportLevel() {
      	// already prepared for a future change of DEFAULT_LEVEL
-@@ -909,9 +912,9 @@
+@@ -909,9 +897,9 @@
              code.iconst_0();
          } else {
              code.iconst_m1();
      @Override
      public Object visitImport(Import node) throws Exception {
          setline(node);
-@@ -1258,7 +1261,11 @@
+@@ -1151,15 +1139,12 @@
+     public int beginLoop() {
+         continueLabels.push(new Label());
+         breakLabels.push(new Label());
+-        int savebcf = bcfLevel;
+-        bcfLevel = exceptionHandlers.size();
+-        return savebcf;
++        return 0;
+     }
+ 
+     public void finishLoop(int savebcf) {
+         continueLabels.pop();
+         breakLabels.pop();
+-        bcfLevel = savebcf;
+     }
+ 
+     @Override
+@@ -1258,7 +1243,11 @@
      public void exceptionTest(int exc, Label end_of_exceptions,
              TryExcept node, int index)
              throws Exception {
              ExceptHandler handler = (ExceptHandler) node.getInternalHandlers().get(i);
  
              //setline(name);
-@@ -1282,6 +1289,11 @@
+@@ -1282,6 +1271,11 @@
                  code.aload(exc);
                  code.getfield(p(PyException.class), "value", ci(PyObject.class));
                  set(handler.getInternalName());
              }
  
              //do exception body
-@@ -1294,63 +1306,52 @@
+@@ -1294,127 +1288,88 @@
      }
  
      @Override
  
 -        code.label(start);
 -        inFinally.exceptionStarts.addElement(start);
--
++        suite(node.getInternalBody());
+ 
 -        ret = suite(node.getInternalBody());
 -
 -        code.label(end);
 -            inlineFinally(inFinally);
 -            code.goto_(finallyEnd);
 -        }
-+        suite(node.getInternalBody());
-+
 +        code.goto_(handler_end);
  
          // Handle any exceptions that get thrown in suite
  
 -        code.invokestatic(p(Py.class), "addTraceback",
 -                sig(Void.TYPE, Throwable.class, PyFrame.class));
--
++        code.invokestatic(p(Py.class), "addTraceback", sig(Void.TYPE, Throwable.class, PyFrame.class));
+ 
 -        inlineFinally(inFinally);
-+        code.invokestatic(p(Py.class), "addTraceback", sig(Void.TYPE, Throwable.class, PyFrame.class));
-+
 +        // Do finally body
 +        code.label(finally_body_label);
 +        suite(node.getInternalFinalbody());
          return null;
      }
  
-+
-     private void inlineFinally(ExceptionHandler handler) throws Exception {
-         if (!handler.bodyDone) {
-             // end the previous exception block so inlined finally code doesn't
-@@ -1393,28 +1394,42 @@
-     }
- 
-     @Override
+-    private void inlineFinally(ExceptionHandler handler) throws Exception {
+-        if (!handler.bodyDone) {
+-            // end the previous exception block so inlined finally code doesn't
+-            // get covered by our exception handler.
+-            Label end = new Label();
+-            code.label(end);
+-            handler.exceptionEnds.addElement(end);
+-            // also exiting the try: portion of this particular finally
+-        }
+-        if (handler.isFinallyHandler()) {
+-            handler.finalBody(this);
+-        }
++    @Override
 +    public Object visitExceptionRangeStart(ExceptionRangeStart node) throws Exception {
 +    	code.label(node.start);
 +    	return null;
-+    }
-+
+     }
+ 
+-    private void reenterProtectedBody(ExceptionHandler handler) throws Exception {
+-        // restart exception coverage
+-        Label restart = new Label();
+-        code.label(restart);
+-        handler.exceptionStarts.addElement(restart);
 +    @Override
 +    public Object visitExceptionRangeStop(ExceptionRangeStop node) throws Exception {
 +    	code.label(node.end);
 +    	return null;
-+    }
-+
+     }
+ 
+-    /**
+-     *  Inline the finally handling code for levels down to the levelth parent
+-     *  (0 means all).  This takes care to avoid having more nested finallys
+-     *  catch exceptions throw by the parent finally code.  This also pops off
+-     *  all the handlers above level temporarily.
+-     */
+-    private void doFinallysDownTo(int level) throws Exception {
+-        Stack<ExceptionHandler> poppedHandlers = new Stack<ExceptionHandler>();
+-        while (exceptionHandlers.size() > level) {
+-            ExceptionHandler handler = exceptionHandlers.pop();
+-            inlineFinally(handler);
+-            poppedHandlers.push(handler);
+-        }
+-        while (poppedHandlers.size() > 0) {
+-            ExceptionHandler handler = poppedHandlers.pop();
+-            reenterProtectedBody(handler);
+-            exceptionHandlers.push(handler);
+-        }
 +    @Override
 +    public Object visitHandlerStart(HandlerStart node) throws Exception {
 +    	// XXX:: visit the nodes' label or is there nothing to do?
 +    @Override
 +    public Object visitFinallyHandlerStart(FinallyStart node) throws Exception {
 +    	return super.visitHandlerStart(node);
-+    }
-+
-+    @Override
+     }
+ 
+     @Override
      public Object visitTryExcept(TryExcept node) throws Exception {
 -        Label start = new Label();
 -        Label end = new Label();
 -        Label handler_start = new Label();
 -        Label handler_end = new Label();
 -        ExceptionHandler handler = new ExceptionHandler();
--
++    	HandlerStart handlerBlock = (HandlerStart) node.getInternalHandlers().get(0);
++    	Label handler_start = handlerBlock.start;
++    	Label handler_end = handlerBlock.end;
+ 
 -        code.label(start);
 -        handler.exceptionStarts.addElement(start);
 -        exceptionHandlers.push(handler);
-+    	HandlerStart handlerBlock = (HandlerStart) node.getInternalHandlers().get(0);
-+    	Label handler_start = handlerBlock.start;
-+    	Label handler_end = handlerBlock.end;
-+
          //Do suite
          Object exit = suite(node.getInternalBody());
 -        exceptionHandlers.pop();
 -        code.label(end);
 -        handler.exceptionEnds.addElement(end);
--
+ 
 -        if (exit == null) {
-+
 +        if (exit == NoExit) {
              code.goto_(handler_end);
          }
          loadFrame();
  
          code.invokestatic(p(Py.class), "setException", sig(PyException.class, Throwable.class,
-@@ -1439,7 +1454,6 @@
+@@ -1439,7 +1394,6 @@
          }
  
          code.freeFinallyLocal(exc);
          return null;
      }
  
-@@ -1449,13 +1463,14 @@
+@@ -1449,13 +1403,14 @@
      }
  
      public Object suite(java.util.List<stmt> stmts) throws Exception {
      }
  
      @Override
-@@ -2263,7 +2278,7 @@
+@@ -2263,7 +2218,7 @@
              super(tree, value);
          }
      }
      @Override
      public Object visitLambda(Lambda node) throws Exception {
          String name = "<lambda>";
-@@ -2613,8 +2628,8 @@
-         return null;
+@@ -2614,125 +2569,63 @@
      }
  
--    @Override
+     @Override
 -    public Object visitWith(With node) throws Exception {
-+//    @Override
-+    public Object visitWithout(With node) throws Exception {
-         if (!module.getFutures().withStatementSupported()) {
-             throw new ParseException("'with' will become a reserved keyword in Python 2.6", node);
-         }
-@@ -2735,6 +2750,67 @@
-         handler.addExceptionHandlers(label_catch);
-         return null;
-     }
-+    
-+    @Override
+-        if (!module.getFutures().withStatementSupported()) {
+-            throw new ParseException("'with' will become a reserved keyword in Python 2.6", node);
 +    public Object visitWithExit(WithExit node) throws Exception {
 +        final Method __exit__ = Method.getMethod(
 +                "boolean __exit__ (org.python.core.ThreadState,org.python.core.PyException)");
 +                    __exit__.getName(), __exit__.getDescriptor());
 +            code.pop();
 +            break;
-+        }
+         }
+-
+-        final Label label_body_start = new Label();
+-        final Label label_body_end = new Label();
+-        final Label label_catch = new Label();
+-        final Label label_end = new Label();
+-
 +        return null;
 +    }
 +    
 +    @Override
 +    public Object visitWithSpecialAssign(WithSpecialAssign node) throws Exception {
-+        final Method contextGuard_getManager = Method.getMethod(
-+                "org.python.core.ContextManager getManager (org.python.core.PyObject)");
-+        final Method __enter__ = Method.getMethod(
-+                "org.python.core.PyObject __enter__ (org.python.core.ThreadState)");
-+
+         final Method contextGuard_getManager = Method.getMethod(
+                 "org.python.core.ContextManager getManager (org.python.core.PyObject)");
+         final Method __enter__ = Method.getMethod(
+                 "org.python.core.PyObject __enter__ (org.python.core.ThreadState)");
+-        final Method __exit__ = Method.getMethod(
+-                "boolean __exit__ (org.python.core.ThreadState,org.python.core.PyException)");
+ 
+-        // mgr = (EXPR)
+-        visit(node.getInternalContext_expr());
+-
+-        // wrap the manager with the ContextGuard (or get it directly if it
+-        // supports the ContextManager interface)
+-        code.invokestatic(Type.getType(ContextGuard.class).getInternalName(),
+-                contextGuard_getManager.getName(), contextGuard_getManager.getDescriptor());
+-        code.dup();
+-
+-
+-        final int mgr_tmp = code.getLocal(Type.getType(ContextManager.class).getInternalName());
+-        code.astore(mgr_tmp);
+-
+-        // value = mgr.__enter__()
+-        loadThreadState();
+-        code.invokeinterface(Type.getType(ContextManager.class).getInternalName(),
+-                __enter__.getName(), __enter__.getDescriptor());
+-        int value_tmp = code.getLocal(p(PyObject.class));
+-        code.astore(value_tmp);
+-
+-        // exc = True # not necessary, since we don't exec finally if exception
+-
+-        // FINALLY (preparation)
+-        // ordinarily with a finally, we need to duplicate the code. that's not the case
+-        // here
+-        // # The normal and non-local-goto cases are handled here
+-        // if exc: # implicit
+-        //     exit(None, None, None)
+-        ExceptionHandler normalExit = new ExceptionHandler() {
+-
+-            @Override
+-            public boolean isFinallyHandler() {
+-                return true;
+-            }
+-
+-            @Override
+-            public void finalBody(CodeCompiler compiler) throws Exception {
+-                compiler.code.aload(mgr_tmp);
+-                loadThreadState();
+-                compiler.code.aconst_null();
+-                compiler.code.invokeinterface(Type.getType(ContextManager.class).getInternalName(),
+-                        __exit__.getName(), __exit__.getDescriptor());
+-                compiler.code.pop();
+-            }
+-        };
+-        exceptionHandlers.push(normalExit);
+-
+-        // try-catch block here
+-        ExceptionHandler handler = new ExceptionHandler();
+-        exceptionHandlers.push(handler);
+-        handler.exceptionStarts.addElement(label_body_start);
+-
+-        // VAR = value  # Only if "as VAR" is present
+-        code.label(label_body_start);
+-        if (node.getInternalOptional_vars() != null) {
+-            set(node.getInternalOptional_vars(), value_tmp);
 +        switch (node.typeOfAssign) {
 +        case WithSpecialAssign.ENTER:
 +            visit(node.extraExpr);
 +                    contextGuard_getManager.getName(), contextGuard_getManager.getDescriptor());
 +            code.checkcast(p(PyObject.class));
 +            break;
-+        }
-+        return null;
-+    }
+         }
+-        code.freeLocal(value_tmp);
+-
+-        // BLOCK + FINALLY if non-local-goto
+-        Object blockResult = suite(node.getInternalBody());
+-        normalExit.bodyDone = true;
+-        exceptionHandlers.pop();
+-        exceptionHandlers.pop();
+-        code.label(label_body_end);
+-        handler.exceptionEnds.addElement(label_body_end);
+-
+-        // FINALLY if *not* non-local-goto
+-        if (blockResult == NoExit) {
+-            // BLOCK would have generated FINALLY for us if it exited (due to a break,
+-            // continue or return)
+-            inlineFinally(normalExit);
+-            code.goto_(label_end);
+-        }
+-
+-        // CATCH
+-        code.label(label_catch);
+-
+-        loadFrame();
+-        code.invokestatic(p(Py.class), "setException", sig(PyException.class, Throwable.class,
+-                PyFrame.class));
+-        code.aload(mgr_tmp);
+-        code.swap();
+-        loadThreadState();
+-        code.swap();
+-        code.invokeinterface(Type.getType(ContextManager.class).getInternalName(),
+-                __exit__.getName(), __exit__.getDescriptor());
+-
+-        // # The exceptional case is handled here
+-        // exc = False # implicit
+-        // if not exit(*sys.exc_info()):
+-        code.ifne(label_end);
+-        //    raise
+-        // # The exception is swallowed if exit() returns true
+-        code.invokestatic(p(Py.class), "makeException", sig(PyException.class));
+-        code.checkcast(p(Throwable.class));
+-        code.athrow();
+-
+-        code.label(label_end);
+-        code.freeLocal(mgr_tmp);
+-
+-        handler.addExceptionHandlers(label_catch);
+         return null;
+     }
  
-     @Override
-     protected Object unhandled_node(PythonTree node) throws Exception {
-@@ -2745,7 +2821,7 @@
-      *  Data about a given exception range whether a try:finally: or a
-      *  try:except:.  The finally needs to inline the finally block for
-      *  each exit of the try: section, so we carry around that data for it.
+@@ -2741,62 +2634,4 @@
+         throw new Exception("Unhandled node " + node);
+     }
+ 
+-    /**
+-     *  Data about a given exception range whether a try:finally: or a
+-     *  try:except:.  The finally needs to inline the finally block for
+-     *  each exit of the try: section, so we carry around that data for it.
 -     *  
-+     *
-      *  Both of these need to stop exception coverage of an area that is either
-      *  the inlined fin ally of a parent try:finally: or the reentry block after
-      *  a yield.  Thus we keep around a set of exception ranges that the
+-     *  Both of these need to stop exception coverage of an area that is either
+-     *  the inlined fin ally of a parent try:finally: or the reentry block after
+-     *  a yield.  Thus we keep around a set of exception ranges that the
+-     *  catch block will eventually handle.
+-     */
+-    class ExceptionHandler {
+-
+-        /**
+-         *  Each handler gets several exception ranges, this is because inlined
+-         *  finally exit code shouldn't be covered by the exception handler of
+-         *  that finally block.  Thus each time we inline the finally code, we
+-         *  stop one range and then enter a new one.
+-         *
+-         *  We also need to stop coverage for the recovery of the locals after
+-         *  a yield.
+-         */
+-        public Vector<Label> exceptionStarts = new Vector<Label>();
+-        public Vector<Label> exceptionEnds = new Vector<Label>();
+-        public boolean bodyDone = false;
+-        public PythonTree node = null;
+-
+-        public ExceptionHandler() {
+-        }
+-
+-        public ExceptionHandler(PythonTree n) {
+-            node = n;
+-        }
+-
+-        public boolean isFinallyHandler() {
+-            return node != null;
+-        }
+-
+-        public void addExceptionHandlers(Label handlerStart) throws Exception {
+-            for (int i = 0; i < exceptionStarts.size(); ++i) {
+-                Label start = exceptionStarts.elementAt(i);
+-                Label end = exceptionEnds.elementAt(i);
+-                //FIXME: not at all sure that getOffset() test is correct or necessary.
+-                if (start.getOffset() != end.getOffset()) {
+-                    code.trycatch(
+-                            exceptionStarts.elementAt(i),
+-                            exceptionEnds.elementAt(i),
+-                            handlerStart,
+-                            p(Throwable.class));
+-                }
+-            }
+-        }
+-
+-        public void finalBody(CodeCompiler compiler) throws Exception {
+-            if (node instanceof TryFinally) {
+-                suite(((TryFinally) node).getInternalFinalbody());
+-            }
+-        }
+-    }
+ }
 diff --git a/src/org/python/compiler/Future.java b/src/org/python/compiler/Future.java
 --- a/src/org/python/compiler/Future.java
 +++ b/src/org/python/compiler/Future.java