From: <fwi...@us...> - 2009-10-08 03:30:28
|
Revision: 6848 http://jython.svn.sourceforge.net/jython/?rev=6848&view=rev Author: fwierzbicki Date: 2009-10-08 03:30:17 +0000 (Thu, 08 Oct 2009) Log Message: ----------- Coding standards / Cleanup. Modified Paths: -------------- trunk/jython/src/org/python/compiler/CodeCompiler.java Modified: trunk/jython/src/org/python/compiler/CodeCompiler.java =================================================================== --- trunk/jython/src/org/python/compiler/CodeCompiler.java 2009-10-08 02:33:20 UTC (rev 6847) +++ trunk/jython/src/org/python/compiler/CodeCompiler.java 2009-10-08 03:30:17 UTC (rev 6848) @@ -1,5 +1,4 @@ // Copyright (c) Corporation for National Research Initiatives - package org.python.compiler; import java.io.IOException; @@ -103,37 +102,30 @@ public class CodeCompiler extends Visitor implements Opcodes, ClassConstants { - static final Object Exit=new Integer(1); - static final Object NoExit=null; - - static final int GET=0; - static final int SET=1; - static final int DEL=2; - static final int AUGGET=3; - static final int AUGSET=4; - + static final Object Exit = new Integer(1); + static final Object NoExit = null; + static final int GET = 0; + static final int SET = 1; + static final int DEL = 2; + static final int AUGGET = 3; + static final int AUGSET = 4; Module module; ClassWriter cw; Code code; CodeCompiler mrefs; CompilerFlags cflags; - int temporary; expr_contextType augmode; int augtmp1; int augtmp2; int augtmp3; int augtmp4; - boolean fast_locals, print_results; - Map<String, SymInfo> tbl; ScopeInfo my_scope; - boolean optimizeGlobals = true; Vector<String> names; String className; - Stack<Label> continueLabels, breakLabels; Stack<ExceptionHandler> exceptionHandlers; Vector<Label> yields = new Vector<Label>(); @@ -147,9 +139,7 @@ * finally's all the exceptionHandlers are executed. */ public int bcfLevel = 0; - int yield_count = 0; - private Stack<String> stack = new Stack<String>(); public CodeCompiler(Module module, boolean print_results) { @@ -181,7 +171,7 @@ code.iconst(idx); code.putfield(p(PyFrame.class), "f_lasti", "I"); } - + private void loadf_back() throws Exception { code.getfield(p(PyFrame.class), "f_back", ci(PyFrame.class)); } @@ -235,40 +225,45 @@ code.astore(augtmp1); code.aload(augtmp1); - if (count >= 2) + if (count >= 2) { code.aload(augtmp2); - if (count >= 3) + } + if (count >= 3) { code.aload(augtmp3); - if (count >= 4) + } + if (count >= 4) { code.aload(augtmp4); + } } private void restoreAugTmps(PythonTree node, int count) throws Exception { - code.aload(augtmp1); - code.freeLocal(augtmp1); - if (count == 1) - return; - code.aload(augtmp2); - code.freeLocal(augtmp2); - if (count == 2) - return; - code.aload(augtmp3); - code.freeLocal(augtmp3); - if (count == 3) - return; - code.aload(augtmp4); - code.freeLocal(augtmp4); + code.aload(augtmp1); + code.freeLocal(augtmp1); + if (count == 1) { + return; + } + code.aload(augtmp2); + code.freeLocal(augtmp2); + if (count == 2) { + return; + } + code.aload(augtmp3); + code.freeLocal(augtmp3); + if (count == 3) { + return; + } + code.aload(augtmp4); + code.freeLocal(augtmp4); } static boolean checkOptimizeGlobals(boolean fast_locals, ScopeInfo scope) { - return fast_locals&&!scope.exec&&!scope.from_import_star; + return fast_locals && !scope.exec && !scope.from_import_star; } public void parse(mod node, Code code, - boolean fast_locals, String className, - boolean classBody, ScopeInfo scope, CompilerFlags cflags) - throws Exception - { + boolean fast_locals, String className, + boolean classBody, ScopeInfo scope, CompilerFlags cflags) + throws Exception { this.fast_locals = fast_locals; this.className = className; this.code = code; @@ -279,7 +274,7 @@ tbl = scope.tbl; optimizeGlobals = checkOptimizeGlobals(fast_locals, scope); - + if (scope.max_with_count > 0) { // allocate for all the with-exits we will have in the frame; // this allows yield and with to happily co-exist @@ -313,17 +308,15 @@ @Override public Object visitModule(org.python.antlr.ast.Module suite) - throws Exception - { + throws Exception { if (suite.getInternalBody().size() > 0 && - suite.getInternalBody().get(0) instanceof Expr && - ((Expr) suite.getInternalBody().get(0)).getInternalValue() instanceof Str) - { + suite.getInternalBody().get(0) instanceof Expr && + ((Expr) suite.getInternalBody().get(0)).getInternalValue() instanceof Str) { loadFrame(); code.ldc("__doc__"); visit(((Expr) suite.getInternalBody().get(0)).getInternalValue()); code.invokevirtual(p(PyFrame.class), "setglobal", sig(Void.TYPE, String.class, - PyObject.class)); + PyObject.class)); } traverse(suite); return null; @@ -333,9 +326,9 @@ public Object visitExpression(Expression node) throws Exception { if (my_scope.generator && node.getInternalBody() != null) { module.error("'return' with argument inside generator", - true, node); + true, node); } - return visitReturn(new Return(node,node.getInternalBody()), true); + return visitReturn(new Return(node, node.getInternalBody()), true); } public int makeArray(java.util.List<? extends PythonTree> nodes) throws Exception { @@ -343,10 +336,11 @@ // the caller is responsible for freeing. int n; - if (nodes == null) + if (nodes == null) { n = 0; - else + } else { n = nodes.size(); + } int array = code.getLocal(ci(PyObject[].class)); if (n == 0) { @@ -357,7 +351,7 @@ code.anewarray(p(PyObject.class)); code.astore(array); - for(int i=0; i<n; i++) { + for (int i = 0; i < n; i++) { visit(nodes.get(i)); code.aload(array); code.swap(); @@ -379,8 +373,7 @@ public void getDocString(java.util.List<stmt> suite) throws Exception { if (suite.size() > 0 && suite.get(0) instanceof Expr && - ((Expr) suite.get(0)).getInternalValue() instanceof Str) - { + ((Expr) suite.get(0)).getInternalValue() instanceof Str) { visit(((Expr) suite.get(0)).getInternalValue()); } else { code.aconst_null(); @@ -388,20 +381,24 @@ } public boolean makeClosure(ScopeInfo scope) throws Exception { - if (scope == null || scope.freevars == null) return false; + if (scope == null || scope.freevars == null) { + return false; + } int n = scope.freevars.size(); - if (n == 0) return false; + if (n == 0) { + return false; + } int tmp = code.getLocal(ci(PyObject[].class)); code.iconst(n); code.anewarray(p(PyObject.class)); code.astore(tmp); Map<String, SymInfo> upTbl = scope.up.tbl; - for(int i=0; i<n; i++) { + for (int i = 0; i < n; i++) { code.aload(tmp); code.iconst(i); loadFrame(); - for(int j = 1; j < scope.distance; j++) { + for (int j = 1; j < scope.distance; j++) { loadf_back(); } SymInfo symInfo = upTbl.get(scope.freevars.elementAt(i)); @@ -437,23 +434,23 @@ scope.setup_closure(); scope.dump(); - module.codeConstant(new Suite(node,node.getInternalBody()), name, true, - className, false, false, - node.getLine(), scope, cflags).get(code); + module.codeConstant(new Suite(node, node.getInternalBody()), name, true, + className, false, false, + node.getLine(), scope, cflags).get(code); getDocString(node.getInternalBody()); if (!makeClosure(scope)) { code.invokespecial(p(PyFunction.class), "<init>", sig(Void.TYPE, PyObject.class, - PyObject[].class, PyCode.class, PyObject.class)); + PyObject[].class, PyCode.class, PyObject.class)); } else { code.invokespecial(p(PyFunction.class), "<init>", sig(Void.TYPE, PyObject.class, - PyObject[].class, PyCode.class, PyObject.class, PyObject[].class)); + PyObject[].class, PyCode.class, PyObject.class, PyObject[].class)); } - + applyDecorators(node.getInternalDecorator_list()); - set(new Name(node,node.getInternalName(), expr_contextType.Store)); + set(new Name(node, node.getInternalName(), expr_contextType.Store)); return null; } @@ -461,14 +458,15 @@ if (decorators != null && !decorators.isEmpty()) { int res = storeTop(); for (expr decorator : decorators) { - visit(decorator); stackProduce(); + visit(decorator); + stackProduce(); } for (int i = decorators.size(); i > 0; i--) { stackConsume(); loadThreadState(); code.aload(res); code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, - ThreadState.class, PyObject.class)); + ThreadState.class, PyObject.class)); code.astore(res); } code.aload(res); @@ -490,7 +488,7 @@ } @Override - public Object visitAssign(Assign node) throws Exception { + public Object visitAssign(Assign node) throws Exception { setline(node); visit(node.getInternalValue()); if (node.getInternalTargets().size() == 1) { @@ -528,10 +526,10 @@ visit(node.getInternalValues().get(i)); if (node.getInternalNl() && i == node.getInternalValues().size() - 1) { code.invokestatic(p(Py.class), "println", sig(Void.TYPE, PyObject.class, - PyObject.class)); + PyObject.class)); } else { - code.invokestatic(p(Py.class), "printComma", sig(Void.TYPE, PyObject.class, - PyObject.class)); + code.invokestatic(p(Py.class), "printComma", sig(Void.TYPE, PyObject.class, + PyObject.class)); } } else { visit(node.getInternalValues().get(i)); @@ -539,7 +537,7 @@ code.invokestatic(p(Py.class), "println", sig(Void.TYPE, PyObject.class)); } else { code.invokestatic(p(Py.class), "printComma", sig(Void.TYPE, - PyObject.class)); + PyObject.class)); } } @@ -598,15 +596,15 @@ } int stackState = saveStack(); - + if (node.getInternalValue() != null) { visit(node.getInternalValue()); } else { getNone(); } - + setLastI(++yield_count); - + saveLocals(); code.areturn(); @@ -615,7 +613,7 @@ code.label(restart); restoreLocals(); restoreStack(stackState); - + loadFrame(); code.invokevirtual(p(PyFrame.class), "getGeneratorInput", sig(Object.class)); code.dup(); @@ -626,14 +624,14 @@ code.athrow(); code.label(done2); code.checkcast(p(PyObject.class)); - + return null; } - + private void stackProduce() { stackProduce(p(PyObject.class)); } - + private void stackProduce(String signature) { stack.push(signature); } @@ -641,7 +639,7 @@ private void stackConsume() { stackConsume(1); } - + private void stackConsume(int numItems) { for (int i = 0; i < numItems; i++) { stack.pop(); @@ -680,7 +678,7 @@ private void restoreStack(int array) throws Exception { if (stack.size() > 0) { - int i = stack.size() -1; + int i = stack.size() - 1; for (String signature : stack) { if (p(ThreadState.class).equals(signature)) { loadThreadState(); @@ -699,7 +697,7 @@ private void restoreLocals() throws Exception { endExceptionHandlers(); - + Vector<String> v = code.getActiveLocals(); loadFrame(); @@ -710,8 +708,9 @@ for (int i = 0; i < v.size(); i++) { String type = v.elementAt(i); - if (type == null) + if (type == null) { continue; + } code.aload(locals); code.iconst(i); code.aaload(); @@ -730,8 +729,7 @@ * variables without the verifier thinking we might jump out of our * handling with an exception. */ - private void endExceptionHandlers() - { + private void endExceptionHandlers() { Label end = new Label(); code.label(end); for (int i = 0; i < exceptionHandlers.size(); ++i) { @@ -740,8 +738,7 @@ } } - private void restartExceptionHandlers() - { + private void restartExceptionHandlers() { Label start = new Label(); code.label(start); for (int i = 0; i < exceptionHandlers.size(); ++i) { @@ -759,15 +756,17 @@ for (int i = 0; i < v.size(); i++) { String type = v.elementAt(i); - if (type == null) + if (type == null) { continue; + } code.aload(locals); code.iconst(i); //code.checkcast(code.pool.Class(p(Object.class))); if (i == 2222) { code.aconst_null(); - } else + } else { code.aload(i); + } code.aastore(); } @@ -777,7 +776,6 @@ code.freeLocal(locals); } - @Override public Object visitReturn(Return node) throws Exception { return visitReturn(node, false); @@ -790,9 +788,10 @@ } int tmp = 0; if (node.getInternalValue() != null) { - if (my_scope.generator) + if (my_scope.generator) { throw new ParseException("'return' with argument " + - "inside generator", node); + "inside generator", node); + } visit(node.getInternalValue()); tmp = code.getReturnLocal(); code.astore(tmp); @@ -813,9 +812,18 @@ @Override public Object visitRaise(Raise node) throws Exception { setline(node); - if (node.getInternalType() != null) { visit(node.getInternalType()); stackProduce(); } - if (node.getInternalInst() != null) { visit(node.getInternalInst()); stackProduce(); } - if (node.getInternalTback() != null) { visit(node.getInternalTback()); stackProduce(); } + if (node.getInternalType() != null) { + visit(node.getInternalType()); + stackProduce(); + } + if (node.getInternalInst() != null) { + visit(node.getInternalInst()); + stackProduce(); + } + if (node.getInternalTback() != null) { + visit(node.getInternalTback()); + stackProduce(); + } if (node.getInternalType() == null) { code.invokestatic(p(Py.class), "makeException", sig(PyException.class)); } else if (node.getInternalInst() == null) { @@ -824,11 +832,11 @@ } else if (node.getInternalTback() == null) { stackConsume(2); code.invokestatic(p(Py.class), "makeException", sig(PyException.class, PyObject.class, - PyObject.class)); + PyObject.class)); } else { stackConsume(3); code.invokestatic(p(Py.class), "makeException", sig(PyException.class, PyObject.class, - PyObject.class, PyObject.class)); + PyObject.class, PyObject.class)); } code.athrow(); return Exit; @@ -845,23 +853,23 @@ code.ldc(name); loadFrame(); code.invokestatic(p(imp.class), "importOneAs", sig(PyObject.class, String.class, - PyFrame.class)); + PyFrame.class)); } else { String name = a.getInternalName(); asname = name; - if (asname.indexOf('.') > 0) + if (asname.indexOf('.') > 0) { asname = asname.substring(0, asname.indexOf('.')); + } code.ldc(name); loadFrame(); code.invokestatic(p(imp.class), "importOne", sig(PyObject.class, String.class, - PyFrame.class)); + PyFrame.class)); } set(new Name(a, asname, expr_contextType.Store)); } return null; } - @Override public Object visitImportFrom(ImportFrom node) throws Exception { Future.checkFromFuture(node); // future stmt support @@ -890,18 +898,19 @@ "' because it is a nested function", true, node); } - + loadFrame(); code.invokestatic(p(imp.class), "importAll", sig(Void.TYPE, String.class, - PyFrame.class)); + PyFrame.class)); } else { java.util.List<String> fromNames = new ArrayList<String>();//[names.size()]; java.util.List<String> asnames = new ArrayList<String>();//[names.size()]; for (int i = 0; i < aliases.size(); i++) { fromNames.add(aliases.get(i).getInternalName()); asnames.add(aliases.get(i).getInternalAsname()); - if (asnames.get(i) == null) + if (asnames.get(i) == null) { asnames.set(i, fromNames.get(i)); + } } int strArray = makeStrings(code, fromNames); code.aload(strArray); @@ -919,7 +928,7 @@ code.iconst(node.getInternalLevel()); } code.invokestatic(p(imp.class), "importFrom", sig(PyObject[].class, String.class, - String[].class, PyFrame.class, Integer.TYPE)); + String[].class, PyFrame.class, Integer.TYPE)); int tmp = storeTop(); for (int i = 0; i < aliases.size(); i++) { code.aload(tmp); @@ -960,7 +969,7 @@ //do the real work here stackConsume(3); code.invokestatic(p(Py.class), "exec", sig(Void.TYPE, PyObject.class, PyObject.class, - PyObject.class)); + PyObject.class)); return null; } @@ -968,7 +977,7 @@ public Object visitAssert(Assert node) throws Exception { setline(node); Label end_of_assert = new Label(); - + /* First do an if __debug__: */ loadFrame(); emitGetGlobal("__debug__"); @@ -978,8 +987,8 @@ code.ifeq(end_of_assert); /* Now do the body of the assert. If PyObject.__nonzero__ is true, - then the assertion succeeded, the message portion should not be - processed. Otherwise, the message will be processed. */ + then the assertion succeeded, the message portion should not be + processed. Otherwise, the message will be processed. */ visit(node.getInternalTest()); code.invokevirtual(p(PyObject.class), "__nonzero__", sig(Boolean.TYPE)); @@ -987,24 +996,23 @@ code.ifne(end_of_assert); /* Visit the message part of the assertion, or pass Py.None */ - if( node.getInternalMsg() != null ) { - visit(node.getInternalMsg()); + if (node.getInternalMsg() != null) { + visit(node.getInternalMsg()); } else { - getNone(); + getNone(); } - + /* Push exception type onto stack(AssertionError) */ loadFrame(); emitGetGlobal("AssertionError"); - + code.swap(); // The type is the first argument, but the message could be a yield - + code.invokestatic(p(Py.class), "makeException", sig(PyException.class, PyObject.class, - PyObject.class)); - /* Raise assertion error. Only executes this logic if assertion - failed */ + PyObject.class)); + /* Raise assertion error. Only executes this logic if assertion failed */ code.athrow(); - + /* And finally set the label for the end of it all */ code.label(end_of_assert); @@ -1012,8 +1020,7 @@ } public Object doTest(Label end_of_if, If node, int index) - throws Exception - { + throws Exception { Label end_of_suite = new Label(); setline(node.getInternalTest()); @@ -1024,8 +1031,9 @@ Object exit = suite(node.getInternalBody()); - if (end_of_if != null && exit == null) + if (end_of_if != null && exit == null) { code.goto_(end_of_if); + } code.label(end_of_suite); @@ -1039,12 +1047,14 @@ @Override public Object visitIf(If node) throws Exception { Label end_of_if = null; - if (node.getInternalOrelse() != null) + if (node.getInternalOrelse() != null) { end_of_if = new Label(); + } Object exit = doTest(end_of_if, node, 0); - if (end_of_if != null) + if (end_of_if != null) { code.label(end_of_if); + } return exit; } @@ -1083,7 +1093,6 @@ bcfLevel = savebcf; } - @Override public Object visitWhile(While node) throws Exception { int savebcf = beginLoop(); @@ -1178,11 +1187,10 @@ } public void exceptionTest(int exc, Label end_of_exceptions, - TryExcept node, int index) - throws Exception - { + TryExcept node, int index) + throws Exception { for (int i = 0; i < node.getInternalHandlers().size(); i++) { - ExceptHandler handler = (ExceptHandler)node.getInternalHandlers().get(i); + ExceptHandler handler = (ExceptHandler) node.getInternalHandlers().get(i); //setline(name); Label end_of_self = new Label(); @@ -1192,12 +1200,12 @@ //get specific exception visit(handler.getInternalType()); code.invokevirtual(p(PyException.class), "match", sig(Boolean.TYPE, - PyObject.class)); + PyObject.class)); code.ifeq(end_of_self); } else { - if (i != node.getInternalHandlers().size()-1) { + if (i != node.getInternalHandlers().size() - 1) { throw new ParseException( - "default 'except:' must be last", handler); + "default 'except:' must be last", handler); } } @@ -1216,10 +1224,8 @@ code.athrow(); } - @Override - public Object visitTryFinally(TryFinally node) throws Exception - { + public Object visitTryFinally(TryFinally node) throws Exception { Label start = new Label(); Label end = new Label(); Label handlerStart = new Label(); @@ -1259,8 +1265,8 @@ code.aload(excLocal); loadFrame(); - 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.aload(excLocal); @@ -1284,19 +1290,19 @@ code.label(end); handler.exceptionEnds.addElement(end); // also exiting the try: portion of this particular finally - } + } if (handler.isFinallyHandler()) { handler.finalBody(this); } } - + private void reenterProtectedBody(ExceptionHandler handler) throws Exception { // restart exception coverage Label restart = new Label(); code.label(restart); handler.exceptionStarts.addElement(restart); } - + /** * Inline the finally handling code for levels down to the levelth parent * (0 means all). This takes care to avoid having more nested finallys @@ -1314,9 +1320,9 @@ ExceptionHandler handler = poppedHandlers.pop(); reenterProtectedBody(handler); exceptionHandlers.push(handler); - } - } - + } + } + @Override public Object visitTryExcept(TryExcept node) throws Exception { Label start = new Label(); @@ -1334,15 +1340,16 @@ code.label(end); handler.exceptionEnds.addElement(end); - if (exit == null) + if (exit == null) { code.goto_(handler_end); + } code.label(handler_start); loadFrame(); code.invokestatic(p(Py.class), "setException", sig(PyException.class, Throwable.class, - PyFrame.class)); + PyFrame.class)); int exc = code.getFinallyLocal(p(Throwable.class)); code.astore(exc); @@ -1373,10 +1380,11 @@ } public Object suite(java.util.List<stmt> stmts) throws Exception { - for(stmt s: stmts) { + for (stmt s : stmts) { Object exit = visit(s); - if (exit != null) + if (exit != null) { return Exit; + } } return null; } @@ -1389,12 +1397,12 @@ code.dup(); code.invokevirtual(p(PyObject.class), "__nonzero__", sig(Boolean.TYPE)); switch (node.getInternalOp()) { - case Or : - code.ifne(end); - break; - case And : - code.ifeq(end); - break; + case Or: + code.ifne(end); + break; + case And: + code.ifeq(end); + break; } code.pop(); visit(node.getInternalValues().get(i)); @@ -1403,7 +1411,6 @@ return null; } - @Override public Object visitCompare(Compare node) throws Exception { int last = code.getLocal(p(PyObject.class)); @@ -1414,7 +1421,7 @@ code.astore(last); int n = node.getInternalOps().size(); - for(int i = 0; i < n - 1; i++) { + for (int i = 0; i < n - 1; i++) { visit(node.getInternalComparators().get(i)); code.aload(last); code.swap(); @@ -1427,10 +1434,10 @@ code.ifeq(end); } - visit(node.getInternalComparators().get(n-1)); + visit(node.getInternalComparators().get(n - 1)); code.aload(last); code.swap(); - visitCmpop(node.getInternalOps().get(n-1)); + visitCmpop(node.getInternalOps().get(n - 1)); if (n > 1) { code.astore(result); @@ -1448,16 +1455,36 @@ public void visitCmpop(cmpopType op) throws Exception { String name = null; switch (op) { - case Eq: name = "_eq"; break; - case NotEq: name = "_ne"; break; - case Lt: name = "_lt"; break; - case LtE: name = "_le"; break; - case Gt: name = "_gt"; break; - case GtE: name = "_ge"; break; - case Is: name = "_is"; break; - case IsNot: name = "_isnot"; break; - case In: name = "_in"; break; - case NotIn: name = "_notin"; break; + case Eq: + name = "_eq"; + break; + case NotEq: + name = "_ne"; + break; + case Lt: + name = "_lt"; + break; + case LtE: + name = "_le"; + break; + case Gt: + name = "_gt"; + break; + case GtE: + name = "_ge"; + break; + case Is: + name = "_is"; + break; + case IsNot: + name = "_isnot"; + break; + case In: + name = "_in"; + break; + case NotIn: + name = "_notin"; + break; } code.invokevirtual(p(PyObject.class), name, sig(PyObject.class, PyObject.class)); } @@ -1470,18 +1497,42 @@ stackConsume(); String name = null; switch (node.getInternalOp()) { - case Add: name = "_add"; break; - case Sub: name = "_sub"; break; - case Mult: name = "_mul"; break; - case Div: name = "_div"; break; - case Mod: name = "_mod"; break; - case Pow: name = "_pow"; break; - case LShift: name = "_lshift"; break; - case RShift: name = "_rshift"; break; - case BitOr: name = "_or"; break; - case BitXor: name = "_xor"; break; - case BitAnd: name = "_and"; break; - case FloorDiv: name = "_floordiv"; break; + case Add: + name = "_add"; + break; + case Sub: + name = "_sub"; + break; + case Mult: + name = "_mul"; + break; + case Div: + name = "_div"; + break; + case Mod: + name = "_mod"; + break; + case Pow: + name = "_pow"; + break; + case LShift: + name = "_lshift"; + break; + case RShift: + name = "_rshift"; + break; + case BitOr: + name = "_or"; + break; + case BitXor: + name = "_xor"; + break; + case BitAnd: + name = "_and"; + break; + case FloorDiv: + name = "_floordiv"; + break; } if (node.getInternalOp() == operatorType.Div && module.getFutures().areDivisionOn()) { @@ -1490,16 +1541,24 @@ code.invokevirtual(p(PyObject.class), name, sig(PyObject.class, PyObject.class)); return null; } - + @Override public Object visitUnaryOp(UnaryOp node) throws Exception { visit(node.getInternalOperand()); String name = null; switch (node.getInternalOp()) { - case Invert: name = "__invert__"; break; - case Not: name = "__not__"; break; - case UAdd: name = "__pos__"; break; - case USub: name = "__neg__"; break; + case Invert: + name = "__invert__"; + break; + case Not: + name = "__not__"; + break; + case UAdd: + name = "__pos__"; + break; + case USub: + name = "__neg__"; + break; } code.invokevirtual(p(PyObject.class), name, sig(PyObject.class)); return null; @@ -1519,18 +1578,42 @@ code.swap(); String name = null; switch (node.getInternalOp()) { - case Add: name = "_iadd"; break; - case Sub: name = "_isub"; break; - case Mult: name = "_imul"; break; - case Div: name = "_idiv"; break; - case Mod: name = "_imod"; break; - case Pow: name = "_ipow"; break; - case LShift: name = "_ilshift"; break; - case RShift: name = "_irshift"; break; - case BitOr: name = "_ior"; break; - case BitXor: name = "_ixor"; break; - case BitAnd: name = "_iand"; break; - case FloorDiv: name = "_ifloordiv"; break; + case Add: + name = "_iadd"; + break; + case Sub: + name = "_isub"; + break; + case Mult: + name = "_imul"; + break; + case Div: + name = "_idiv"; + break; + case Mod: + name = "_imod"; + break; + case Pow: + name = "_ipow"; + break; + case LShift: + name = "_ilshift"; + break; + case RShift: + name = "_irshift"; + break; + case BitOr: + name = "_ior"; + break; + case BitXor: + name = "_ixor"; + break; + case BitAnd: + name = "_iand"; + break; + case FloorDiv: + name = "_ifloordiv"; + break; } if (node.getInternalOp() == operatorType.Div && module.getFutures().areDivisionOn()) { name = "_itruediv"; @@ -1546,10 +1629,8 @@ return null; } - public static int makeStrings(Code c, Collection<String> names) - throws IOException - { + throws IOException { if (names != null) { c.iconst(names.size()); } else { @@ -1570,68 +1651,76 @@ } return strings; } - + public Object invokeNoKeywords(Attribute node, java.util.List<expr> values) - throws Exception - { + throws Exception { String name = getName(node.getInternalAttr()); - visit(node.getInternalValue()); stackProduce(); + visit(node.getInternalValue()); + stackProduce(); code.ldc(name); code.invokevirtual(p(PyObject.class), "__getattr__", sig(PyObject.class, String.class)); - loadThreadState(); stackProduce(p(ThreadState.class)); + loadThreadState(); + stackProduce(p(ThreadState.class)); switch (values.size()) { - case 0: - stackConsume(2); // target + ts - code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, ThreadState.class)); - break; - case 1: - visit(values.get(0)); - stackConsume(2); // target + ts - code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, ThreadState.class, + case 0: + stackConsume(2); // target + ts + code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, + ThreadState.class)); + break; + case 1: + visit(values.get(0)); + stackConsume(2); // target + ts + code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, + ThreadState.class, PyObject.class)); + break; + case 2: + visit(values.get(0)); + stackProduce(); + visit(values.get(1)); + stackConsume(3); // target + ts + arguments + code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, + ThreadState.class, PyObject.class, PyObject.class)); + break; + case 3: + visit(values.get(0)); + stackProduce(); + visit(values.get(1)); + stackProduce(); + visit(values.get(2)); + stackConsume(4); // target + ts + arguments + code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, + ThreadState.class, PyObject.class, PyObject.class, PyObject.class)); + break; + case 4: + visit(values.get(0)); + stackProduce(); + visit(values.get(1)); + stackProduce(); + visit(values.get(2)); + stackProduce(); + visit(values.get(3)); + stackConsume(5); // target + ts + arguments + code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, + ThreadState.class, PyObject.class, PyObject.class, PyObject.class, PyObject.class)); - break; - case 2: - visit(values.get(0)); stackProduce(); - visit(values.get(1)); - stackConsume(3); // target + ts + arguments - code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, ThreadState.class, - PyObject.class, PyObject.class)); - break; - case 3: - visit(values.get(0)); stackProduce(); - visit(values.get(1)); stackProduce(); - visit(values.get(2)); - stackConsume(4); // target + ts + arguments - code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, ThreadState.class, - PyObject.class, PyObject.class, PyObject.class)); - break; - case 4: - visit(values.get(0)); stackProduce(); - visit(values.get(1)); stackProduce(); - visit(values.get(2)); stackProduce(); - visit(values.get(3)); - stackConsume(5); // target + ts + arguments - code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, ThreadState.class, - PyObject.class, PyObject.class, PyObject.class, PyObject.class)); - break; - default: - int argArray = makeArray(values); - code.aload(argArray); - code.freeLocal(argArray); - stackConsume(2); // target + ts - code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, ThreadState.class, - PyObject[].class)); - break; + break; + default: + int argArray = makeArray(values); + code.aload(argArray); + code.freeLocal(argArray); + stackConsume(2); // target + ts + code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, + ThreadState.class, PyObject[].class)); + break; } return null; } - @Override public Object visitCall(Call node) throws Exception { - java.util.List<String> keys = new ArrayList<String>();//[node.keywords.size()]; - java.util.List<expr> values = new ArrayList<expr>();//[node.args.size() + keys.size()]; + java.util.List<String> keys = new ArrayList<String>(); + java.util.List<expr> values = new ArrayList<expr>(); for (int i = 0; i < node.getInternalArgs().size(); i++) { values.add(node.getInternalArgs().get(i)); } @@ -1640,41 +1729,45 @@ values.add(node.getInternalKeywords().get(i).getInternalValue()); } - if ((node.getInternalKeywords() == null || node.getInternalKeywords().size() == 0)&& node.getInternalStarargs() == null && - node.getInternalKwargs() == null && node.getInternalFunc() instanceof Attribute) - { + if ((node.getInternalKeywords() == null || node.getInternalKeywords().size() == 0) && + node.getInternalStarargs() == null && node.getInternalKwargs() == null && + node.getInternalFunc() instanceof Attribute) { return invokeNoKeywords((Attribute) node.getInternalFunc(), values); } - visit(node.getInternalFunc()); stackProduce(); + visit(node.getInternalFunc()); + stackProduce(); if (node.getInternalStarargs() != null || node.getInternalKwargs() != null) { int argArray = makeArray(values); int strArray = makeStrings(code, keys); - if (node.getInternalStarargs() == null) + if (node.getInternalStarargs() == null) { code.aconst_null(); - else + } else { visit(node.getInternalStarargs()); + } stackProduce(); - if (node.getInternalKwargs() == null) + if (node.getInternalKwargs() == null) { code.aconst_null(); - else + } else { visit(node.getInternalKwargs()); + } stackProduce(); - + code.aload(argArray); code.aload(strArray); code.freeLocal(argArray); code.freeLocal(strArray); code.dup2_x2(); code.pop2(); - + stackConsume(3); // target + starargs + kwargs code.invokevirtual(p(PyObject.class), "_callextra", sig(PyObject.class, - PyObject[].class, String[].class, PyObject.class, PyObject.class)); + PyObject[].class, String[].class, PyObject.class, PyObject.class)); } else if (keys.size() > 0) { - loadThreadState(); stackProduce(p(ThreadState.class)); + loadThreadState(); + stackProduce(p(ThreadState.class)); int argArray = makeArray(values); int strArray = makeStrings(code, keys); code.aload(argArray); @@ -1683,60 +1776,66 @@ code.freeLocal(strArray); stackConsume(2); // target + ts code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, ThreadState.class, - PyObject[].class, String[].class)); + PyObject[].class, String[].class)); } else { - loadThreadState(); stackProduce(p(ThreadState.class)); + loadThreadState(); + stackProduce(p(ThreadState.class)); switch (values.size()) { - case 0: - stackConsume(2); // target + ts - code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, + case 0: + stackConsume(2); // target + ts + code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, ThreadState.class)); - break; - case 1: - visit(values.get(0)); - stackConsume(2); // target + ts - code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, + break; + case 1: + visit(values.get(0)); + stackConsume(2); // target + ts + code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, ThreadState.class, PyObject.class)); - break; - case 2: - visit(values.get(0)); stackProduce(); - visit(values.get(1)); - stackConsume(3); // target + ts + arguments - code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, + break; + case 2: + visit(values.get(0)); + stackProduce(); + visit(values.get(1)); + stackConsume(3); // target + ts + arguments + code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, ThreadState.class, PyObject.class, PyObject.class)); - break; - case 3: - visit(values.get(0)); stackProduce(); - visit(values.get(1)); stackProduce(); - visit(values.get(2)); - stackConsume(4); // target + ts + arguments - code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, + break; + case 3: + visit(values.get(0)); + stackProduce(); + visit(values.get(1)); + stackProduce(); + visit(values.get(2)); + stackConsume(4); // target + ts + arguments + code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, ThreadState.class, PyObject.class, PyObject.class, PyObject.class)); - break; - case 4: - visit(values.get(0)); stackProduce(); - visit(values.get(1)); stackProduce(); - visit(values.get(2)); stackProduce(); - visit(values.get(3)); - stackConsume(5); // target + ts + arguments - code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, + break; + case 4: + visit(values.get(0)); + stackProduce(); + visit(values.get(1)); + stackProduce(); + visit(values.get(2)); + stackProduce(); + visit(values.get(3)); + stackConsume(5); // target + ts + arguments + code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, ThreadState.class, PyObject.class, PyObject.class, PyObject.class, PyObject.class)); - break; - default: - int argArray = makeArray(values); - code.aload(argArray); - code.freeLocal(argArray); - stackConsume(2); // target + ts - code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, + break; + default: + int argArray = makeArray(values); + code.aload(argArray); + code.freeLocal(argArray); + stackConsume(2); // target + ts + code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, ThreadState.class, PyObject[].class)); - break; + break; } } return null; } - public Object Slice(Subscript node, Slice slice) throws Exception { expr_contextType ctx = node.getInternalCtx(); if (ctx == expr_contextType.AugStore && augmode == expr_contextType.Store) { @@ -1745,23 +1844,27 @@ } else { visit(node.getInternalValue()); stackProduce(); - if (slice.getInternalLower() != null) + if (slice.getInternalLower() != null) { visit(slice.getInternalLower()); - else + } else { code.aconst_null(); + } stackProduce(); - if (slice.getInternalUpper() != null) + if (slice.getInternalUpper() != null) { visit(slice.getInternalUpper()); - else + } else { code.aconst_null(); + } stackProduce(); - if (slice.getInternalStep() != null) + if (slice.getInternalStep() != null) { visit(slice.getInternalStep()); - else + } else { code.aconst_null(); + } stackProduce(); - if (node.getInternalCtx() == expr_contextType.AugStore && augmode == expr_contextType.Load) { + if (node.getInternalCtx() == expr_contextType.AugStore && + augmode == expr_contextType.Load) { saveAugTmps(node, 4); ctx = expr_contextType.Load; } @@ -1769,20 +1872,20 @@ } switch (ctx) { - case Del: - code.invokevirtual(p(PyObject.class), "__delslice__", sig(Void.TYPE, PyObject.class, + case Del: + code.invokevirtual(p(PyObject.class), "__delslice__", sig(Void.TYPE, PyObject.class, PyObject.class, PyObject.class)); - return null; - case Load: - code.invokevirtual(p(PyObject.class), "__getslice__", sig(PyObject.class, + return null; + case Load: + code.invokevirtual(p(PyObject.class), "__getslice__", sig(PyObject.class, PyObject.class, PyObject.class, PyObject.class)); - return null; - case Param: - case Store: - code.aload(temporary); - code.invokevirtual(p(PyObject.class), "__setslice__", sig(Void.TYPE, PyObject.class, + return null; + case Param: + case Store: + code.aload(temporary); + code.invokevirtual(p(PyObject.class), "__setslice__", sig(Void.TYPE, PyObject.class, PyObject.class, PyObject.class, PyObject.class)); - return null; + return null; } return null; @@ -1796,33 +1899,38 @@ int value = temporary; expr_contextType ctx = node.getInternalCtx(); - if (node.getInternalCtx() == expr_contextType.AugStore && augmode == expr_contextType.Store) { + if (node.getInternalCtx() == expr_contextType.AugStore && + augmode == expr_contextType.Store) { restoreAugTmps(node, 2); ctx = expr_contextType.Store; } else { - visit(node.getInternalValue()); stackProduce(); + visit(node.getInternalValue()); + stackProduce(); visit(node.getInternalSlice()); stackConsume(); - if (node.getInternalCtx() == expr_contextType.AugStore && augmode == expr_contextType.Load) { + if (node.getInternalCtx() == expr_contextType.AugStore && + augmode == expr_contextType.Load) { saveAugTmps(node, 2); ctx = expr_contextType.Load; } } switch (ctx) { - case Del: - code.invokevirtual(p(PyObject.class), "__delitem__", sig(Void.TYPE, PyObject.class)); - return null; - case Load: - code.invokevirtual(p(PyObject.class), "__getitem__", sig(PyObject.class, PyObject.class)); - return null; - case Param: - case Store: - code.aload(value); - code.invokevirtual(p(PyObject.class), "__setitem__", sig(Void.TYPE, PyObject.class, - PyObject.class)); - return null; + case Del: + code.invokevirtual(p(PyObject.class), "__delitem__", + sig(Void.TYPE, PyObject.class)); + return null; + case Load: + code.invokevirtual(p(PyObject.class), "__getitem__", + sig(PyObject.class, PyObject.class)); + return null; + case Param: + case Store: + code.aload(value); + code.invokevirtual(p(PyObject.class), "__setitem__", + sig(Void.TYPE, PyObject.class, PyObject.class)); + return null; } return null; } @@ -1848,32 +1956,35 @@ public Object visitAttribute(Attribute node) throws Exception { expr_contextType ctx = node.getInternalCtx(); - if (node.getInternalCtx() == expr_contextType.AugStore && augmode == expr_contextType.Store) { + if (node.getInternalCtx() == expr_contextType.AugStore && + augmode == expr_contextType.Store) { restoreAugTmps(node, 2); ctx = expr_contextType.Store; } else { visit(node.getInternalValue()); code.ldc(getName(node.getInternalAttr())); - if (node.getInternalCtx() == expr_contextType.AugStore && augmode == expr_contextType.Load) { + if (node.getInternalCtx() == expr_contextType.AugStore && + augmode == expr_contextType.Load) { saveAugTmps(node, 2); ctx = expr_contextType.Load; } } switch (ctx) { - case Del: - code.invokevirtual(p(PyObject.class), "__delattr__", sig(Void.TYPE, String.class)); - return null; - case Load: - code.invokevirtual(p(PyObject.class), "__getattr__", sig(PyObject.class, String.class)); - return null; - case Param: - case Store: - code.aload(temporary); - code.invokevirtual(p(PyObject.class), "__setattr__", sig(Void.TYPE, String.class, - PyObject.class)); - return null; + case Del: + code.invokevirtual(p(PyObject.class), "__delattr__", sig(Void.TYPE, String.class)); + return null; + case Load: + code.invokevirtual(p(PyObject.class), "__getattr__", + sig(PyObject.class, String.class)); + return null; + case Param: + case Store: + code.aload(temporary); + code.invokevirtual(p(PyObject.class), "__setattr__", + sig(Void.TYPE, String.class, PyObject.class)); + return null; } return null; } @@ -1881,8 +1992,8 @@ public Object seqSet(java.util.List<expr> nodes) throws Exception { code.aload(temporary); code.iconst(nodes.size()); - code.invokestatic(p(Py.class), "unpackSequence", sig(PyObject[].class, PyObject.class, - Integer.TYPE)); + code.invokestatic(p(Py.class), "unpackSequence", + sig(PyObject[].class, PyObject.class, Integer.TYPE)); int tmp = code.getLocal("[org/python/core/PyObject"); code.astore(tmp); @@ -1899,7 +2010,7 @@ } public Object seqDel(java.util.List<expr> nodes) throws Exception { - for (expr e: nodes) { + for (expr e : nodes) { visit(e); } return null; @@ -1907,12 +2018,13 @@ @Override public Object visitTuple(Tuple node) throws Exception { - /* if (mode ==AUGSET) - throw new ParseException( - "augmented assign to tuple not possible", node); */ - if (node.getInternalCtx() == expr_contextType.Store) return seqSet(node.getInternalElts()); - if (node.getInternalCtx() == expr_contextType.Del) return seqDel(node.getInternalElts()); - + if (node.getInternalCtx() == expr_contextType.Store) { + return seqSet(node.getInternalElts()); + } + if (node.getInternalCtx() == expr_contextType.Del) { + return seqDel(node.getInternalElts()); + } + int content = makeArray(node.getInternalElts()); code.new_(p(PyTuple.class)); @@ -1926,9 +2038,13 @@ @Override public Object visitList(List node) throws Exception { - if (node.getInternalCtx() == expr_contextType.Store) return seqSet(node.getInternalElts()); - if (node.getInternalCtx() == expr_contextType.Del) return seqDel(node.getInternalElts()); - + if (node.getInternalCtx() == expr_contextType.Store) { + return seqSet(node.getInternalElts()); + } + if (node.getInternalCtx() == expr_contextType.Del) { + return seqDel(node.getInternalElts()); + } + int content = makeArray(node.getInternalElts()); code.new_(p(PyList.class)); @@ -1951,26 +2067,28 @@ code.ldc("append"); code.invokevirtual(p(PyObject.class), "__getattr__", sig(PyObject.class, String.class)); - String tmp_append ="_[" + node.getLine() + "_" + node.getCharPositionInLine() + "]"; + String tmp_append = "_[" + node.getLine() + "_" + node.getCharPositionInLine() + "]"; set(new Name(node, tmp_append, expr_contextType.Store)); java.util.List<expr> args = new ArrayList<expr>(); args.add(node.getInternalElt()); - stmt n = new Expr(node, new Call(node, new Name(node, tmp_append, expr_contextType.Load), - args, - new ArrayList<keyword>(), null, null)); + stmt n = new Expr(node, new Call(node, new Name(node, tmp_append, expr_contextType.Load), + args, + new ArrayList<keyword>(), null, null)); for (int i = node.getInternalGenerators().size() - 1; i >= 0; i--) { comprehension lc = node.getInternalGenerators().get(i); for (int j = lc.getInternalIfs().size() - 1; j >= 0; j--) { java.util.List<stmt> body = new ArrayList<stmt>(); body.add(n); - n = new If(lc.getInternalIfs().get(j), lc.getInternalIfs().get(j), body, new ArrayList<stmt>()); + n = new If(lc.getInternalIfs().get(j), lc.getInternalIfs().get(j), body, + new ArrayList<stmt>()); } java.util.List<stmt> body = new ArrayList<stmt>(); body.add(n); - n = new For(lc,lc.getInternalTarget(), lc.getInternalIter(), body, new ArrayList<stmt>()); + n = new For(lc, lc.getInternalTarget(), lc.getInternalIter(), body, + new ArrayList<stmt>()); } visit(n); java.util.List<expr> targets = new ArrayList<expr>(); @@ -1988,7 +2106,7 @@ elts.add(node.getInternalValues().get(i)); } int content = makeArray(elts); - + code.new_(p(PyDictionary.class)); code.dup(); code.aload(content); @@ -2010,7 +2128,7 @@ //Add a return node onto the outside of suite; java.util.List<stmt> bod = new ArrayList<stmt>(); - bod.add(new Return(node,node.getInternalBody())); + bod.add(new Return(node, node.getInternalBody())); mod retSuite = new Suite(node, bod); setline(node); @@ -2020,32 +2138,31 @@ int defaultsArray = makeArray(scope.ac.getDefaults()); code.new_(p(PyFunction.class)); - + code.dup(); code.aload(defaultsArray); code.freeLocal(defaultsArray); loadFrame(); code.getfield(p(PyFrame.class), "f_globals", ci(PyObject.class)); - + code.swap(); scope.setup_closure(); scope.dump(); module.codeConstant(retSuite, name, true, className, - false, false, node.getLine(), scope, cflags).get(code); + false, false, node.getLine(), scope, cflags).get(code); if (!makeClosure(scope)) { code.invokespecial(p(PyFunction.class), "<init>", sig(Void.TYPE, PyObject.class, - PyObject[].class, PyCode.class)); + PyObject[].class, PyCode.class)); } else { code.invokespecial(p(PyFunction.class), "<init>", sig(Void.TYPE, PyObject.class, - PyObject[].class, Py... [truncated message content] |