From: <fwi...@us...> - 2009-10-07 19:17:06
|
Revision: 6846 http://jython.svn.sourceforge.net/jython/?rev=6846&view=rev Author: fwierzbicki Date: 2009-10-07 19:16:54 +0000 (Wed, 07 Oct 2009) Log Message: ----------- Make PyCodeConstant an actual constant. Move construction details into PyCodeConstant constructor. Hide more implementation details by making public fields package private. Modified Paths: -------------- trunk/jython/src/org/python/compiler/CodeCompiler.java trunk/jython/src/org/python/compiler/Module.java Modified: trunk/jython/src/org/python/compiler/CodeCompiler.java =================================================================== --- trunk/jython/src/org/python/compiler/CodeCompiler.java 2009-10-07 02:50:46 UTC (rev 6845) +++ trunk/jython/src/org/python/compiler/CodeCompiler.java 2009-10-07 19:16:54 UTC (rev 6846) @@ -103,20 +103,20 @@ public class CodeCompiler extends Visitor implements Opcodes, ClassConstants { - public static final Object Exit=new Integer(1); - public static final Object NoExit=null; + static final Object Exit=new Integer(1); + static final Object NoExit=null; - public static final int GET=0; - public static final int SET=1; - public static final int DEL=2; - public static final int AUGGET=3; - public static final int AUGSET=4; + 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; - public Module module; - public ClassWriter cw; - public Code code; - public CodeCompiler mrefs; - public CompilerFlags cflags; + Module module; + ClassWriter cw; + Code code; + CodeCompiler mrefs; + CompilerFlags cflags; int temporary; expr_contextType augmode; @@ -125,18 +125,18 @@ int augtmp3; int augtmp4; - public boolean fast_locals, print_results; + boolean fast_locals, print_results; - public Map<String, SymInfo> tbl; - public ScopeInfo my_scope; + Map<String, SymInfo> tbl; + ScopeInfo my_scope; boolean optimizeGlobals = true; - public Vector<String> names; - public String className; + Vector<String> names; + String className; - public Stack<Label> continueLabels, breakLabels; - public Stack<ExceptionHandler> exceptionHandlers; - public Vector<Label> yields = new Vector<Label>(); + Stack<Label> continueLabels, breakLabels; + Stack<ExceptionHandler> exceptionHandlers; + Vector<Label> yields = new Vector<Label>(); /* * break/continue finally's level. This is the lowest level in the @@ -258,8 +258,11 @@ return; code.aload(augtmp4); code.freeLocal(augtmp4); - } + } + static boolean checkOptimizeGlobals(boolean fast_locals, ScopeInfo scope) { + return fast_locals&&!scope.exec&&!scope.from_import_star; + } public void parse(mod node, Code code, boolean fast_locals, String className, @@ -275,7 +278,7 @@ names = scope.names; tbl = scope.tbl; - optimizeGlobals = fast_locals&&!scope.exec&&!scope.from_import_star; + optimizeGlobals = checkOptimizeGlobals(fast_locals, scope); if (scope.max_with_count > 0) { // allocate for all the with-exits we will have in the frame; Modified: trunk/jython/src/org/python/compiler/Module.java =================================================================== --- trunk/jython/src/org/python/compiler/Module.java 2009-10-07 02:50:46 UTC (rev 6845) +++ trunk/jython/src/org/python/compiler/Module.java 2009-10-07 19:16:54 UTC (rev 6846) @@ -243,24 +243,118 @@ class PyCodeConstant extends Constant implements ClassConstants, Opcodes { - String co_name; - int argcount; - List<String> names; - int id; - int co_firstlineno; - boolean arglist, keywordlist; - String fname; + final String co_name; + final int argcount; + final List<String> names; + final int id; + final int co_firstlineno; + final boolean arglist, keywordlist; + final String fname; // for nested scopes - List<String> cellvars; - List<String> freevars; - int jy_npurecell; + final List<String> cellvars; + final List<String> freevars; + final int jy_npurecell; - int moreflags; + final int moreflags; - PyCodeConstant() { + PyCodeConstant(mod tree, String name, boolean fast_locals, String + className, boolean classBody, boolean printResults, int + firstlineno, ScopeInfo scope, org.python.core.CompilerFlags cflags, + Module module) + throws Exception { + + this.co_name = name; + this.co_firstlineno = firstlineno; + this.module = module; + + //Needed so that moreflags can be final. + int _moreflags = 0; + + if (scope.ac != null) { + arglist = scope.ac.arglist; + keywordlist = scope.ac.keywordlist; + argcount = scope.ac.names.size(); + + //Do something to add init_code to tree + //XXX: not sure we should be modifying scope.ac in a PyCodeConstant + //constructor. + if (scope.ac.init_code.size() > 0) { + scope.ac.appendInitCode((Suite) tree); + } + } else { + arglist = false; + keywordlist = false; + argcount = 0; + } + + id = module.codes.size(); + + //Better names in the future? + if (isJavaIdentifier(name)) { + fname = name+"$"+id; + } else { + fname = "f$"+id; + } + //XXX: is fname needed at all, or should we just use "name"? + this.name = fname; + + // !classdef only + if (!classBody) { + names = toNameAr(scope.names,false); + } else { + names = null; + } + + cellvars = toNameAr(scope.cellvars, true); + freevars = toNameAr(scope.freevars, true); + jy_npurecell = scope.jy_npurecell; + + if (CodeCompiler.checkOptimizeGlobals(fast_locals, scope)) { + _moreflags |= org.python.core.CodeFlag.CO_OPTIMIZED.flag; + } + if (scope.generator) { + _moreflags |= org.python.core.CodeFlag.CO_GENERATOR.flag; + } + if (cflags != null) { + if (cflags.isFlagSet(CodeFlag.CO_GENERATOR_ALLOWED)) { + _moreflags |= org.python.core.CodeFlag.CO_GENERATOR_ALLOWED.flag; + } + if (cflags.isFlagSet(CodeFlag.CO_FUTURE_DIVISION)) { + _moreflags |= org.python.core.CodeFlag.CO_FUTURE_DIVISION.flag; + } + } + moreflags = _moreflags; } + //XXX: this can probably go away now that we can probably just copy the list. + private List<String> toNameAr(List<String> names,boolean nullok) { + int sz = names.size(); + if (sz == 0 && nullok) { + return null; + } + List<String> nameArray = new ArrayList<String>(); + nameArray.addAll(names); + return nameArray; + } + + private boolean isJavaIdentifier(String s) { + char[] chars = s.toCharArray(); + if (chars.length == 0) { + return false; + } + if (!Character.isJavaIdentifierStart(chars[0])) { + return false; + } + + for(int i=1; i<chars.length; i++) { + if (!Character.isJavaIdentifierPart(chars[i])) { + return false; + } + } + return true; + } + void get(Code c) throws IOException { c.getstatic(module.classfile.name, name, ci(PyCode.class)); } @@ -320,7 +414,7 @@ Constant filename; String sfilename; Constant mainCode; - public boolean linenumbers; + boolean linenumbers; Future futures; Hashtable<PythonTree,ScopeInfo> scopes; long mtime; @@ -391,34 +485,6 @@ } List<PyCodeConstant> codes; - private boolean isJavaIdentifier(String s) { - char[] chars = s.toCharArray(); - if (chars.length == 0) { - return false; - } - if (!Character.isJavaIdentifierStart(chars[0])) { - return false; - } - - for(int i=1; i<chars.length; i++) { - if (!Character.isJavaIdentifierPart(chars[i])) { - return false; - } - } - return true; - } - - //XXX: this can probably go away now that we can probably just copy the list. - private List<String> toNameAr(List<String> names,boolean nullok) { - int sz = names.size(); - if (sz == 0 && nullok) { - return null; - } - List<String> nameArray = new ArrayList<String>(); - nameArray.addAll(names); - return nameArray; - } - PyCodeConstant codeConstant(mod tree, String name, boolean fast_locals, String className, boolean classBody, boolean printResults, @@ -429,7 +495,6 @@ printResults,firstlineno,scope,null); } - PyCodeConstant codeConstant(mod tree, String name, boolean fast_locals, String className, boolean classBody, boolean printResults, @@ -438,34 +503,18 @@ org.python.core.CompilerFlags cflags) throws Exception { - PyCodeConstant code = new PyCodeConstant(); - ArgListCompiler ac = (scope != null)?scope.ac:null; + PyCodeConstant code = new PyCodeConstant(tree, name, fast_locals, + className, classBody, printResults, firstlineno, scope, cflags, + this); + codes.add(code); - if (ac != null) { - code.arglist = ac.arglist; - code.keywordlist = ac.keywordlist; - code.argcount = ac.names.size(); - } + CodeCompiler compiler = new CodeCompiler(this, printResults); - code.co_name = name; - code.co_firstlineno = firstlineno; - code.id = codes.size(); - - //Better names in the future? - if (isJavaIdentifier(name)) - code.fname = name+"$"+code.id; - else - code.fname = "f$"+code.id; - - codes.add(code); - Code c = classfile.addMethod( code.fname, sig(PyObject.class, PyFrame.class, ThreadState.class), ACC_PUBLIC); - CodeCompiler compiler = new CodeCompiler(this, printResults); - if (classBody) { // Set the class's __module__ to __name__. fails when there's no __name__ c.aload(1); @@ -485,10 +534,6 @@ Label start = new Label(); c.label(start); - //Do something to add init_code to tree - if (ac != null && ac.init_code.size() > 0) { - ac.appendInitCode((Suite) tree); - } int nparamcell = scope.jy_paramcells.size(); if(nparamcell > 0) { Map<String, SymInfo> tbl = scope.tbl; @@ -504,7 +549,6 @@ } compiler.parse(tree, c, fast_locals, className, classBody, scope, cflags); - // similar to visitResume code in pyasm.py if (scope.generator) { c.label(genswitch); @@ -520,30 +564,6 @@ c.tableswitch(0, yields.length - 1, start, yields); } - // !classdef only - if (!classBody) code.names = toNameAr(compiler.names,false); - - code.cellvars = toNameAr(scope.cellvars, true); - code.freevars = toNameAr(scope.freevars, true); - code.jy_npurecell = scope.jy_npurecell; - - if (compiler.optimizeGlobals) { - code.moreflags |= org.python.core.CodeFlag.CO_OPTIMIZED.flag; - } - if (compiler.my_scope.generator) { - code.moreflags |= org.python.core.CodeFlag.CO_GENERATOR.flag; - } - if (cflags != null) { - if (cflags.isFlagSet(CodeFlag.CO_GENERATOR_ALLOWED)) { - code.moreflags |= org.python.core.CodeFlag.CO_GENERATOR_ALLOWED.flag; - } - if (cflags.isFlagSet(CodeFlag.CO_FUTURE_DIVISION)) { - code.moreflags |= org.python.core.CodeFlag.CO_FUTURE_DIVISION.flag; - } - } - - code.module = this; - code.name = code.fname; return code; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |