From: <zy...@us...> - 2009-01-29 03:45:03
|
Revision: 5998 http://jython.svn.sourceforge.net/jython/?rev=5998&view=rev Author: zyasoft Date: 2009-01-29 03:44:58 +0000 (Thu, 29 Jan 2009) Log Message: ----------- extract.py now creates modules where codeobjects respect their dependency relationships (via a tsort). Modified Paths: -------------- branches/pbcvm/Tools/pbcvm/extract.py branches/pbcvm/src/org/python/core/PyBytecode.java Modified: branches/pbcvm/Tools/pbcvm/extract.py =================================================================== --- branches/pbcvm/Tools/pbcvm/extract.py 2009-01-29 03:23:40 UTC (rev 5997) +++ branches/pbcvm/Tools/pbcvm/extract.py 2009-01-29 03:44:58 UTC (rev 5998) @@ -1,11 +1,11 @@ -"""Given a module, codegens a new module where all functions imported -from it (using __all__ ?) are replaced with functions tied to -PyBytecode, including references to code objs in co_consts. Other -objects are simply imported from the original object. Hopefully this -provides an opportunity to test something two different ways, which -seems nice.""" +"""Given a module, generates a new module where all functions that are + not builtin (including within classes) have their func_code pointed + to a PyBytecode constructor. This enables CPython to generate for + Jython the desired PBC. +""" import inspect +import networkx # add dependency to a setup script? probably overkill import sys from collections import defaultdict @@ -19,10 +19,9 @@ _codeobjs = {} _codeobjs_names = {} counters = defaultdict(int) +depend = networkx.DiGraph() +root = "root" -# XXX - need to capture with a toposort the dependencies of -# recursive code objects and emit in that order - def extract(mod): functionobjs = candidate_functions(mod) for name, f in functionobjs: @@ -32,12 +31,17 @@ for code, definition in _codeobjs.iteritems(): codes[_codeobjs_names[code]] = definition print "from %s import *" % mod.__name__ - print "from org.python.core import PyBytecode, PyFunction" + print "from org.python.core import PyBytecode" print print "_codeobjs = {}" print - for name, obj in sorted(codes.iteritems()): - print "_codeobjs[%r] = %s" % (name, obj) + + objs = networkx.topological_sort(depend) + for obj in objs: + if not inspect.iscode(obj): + continue + name = _codeobjs_names[obj] + print "_codeobjs[%r] = %s" % (name, _codeobjs[obj]) print for name, f in functionobjs: print "%s.func_code = _codeobjs[%r]" % (name, _codeobjs_names[f.func_code]) @@ -81,6 +85,7 @@ _codeobjs_names[code] = name # need to treat co_consts specially - maybe use pickling if repr is not suitable? values = [] + depend.add_edge(code, root) for attr in attrs: if attr == 'co_consts': co_consts = [] @@ -88,6 +93,7 @@ if inspect.iscode(const): print >> sys.stderr, "Extracting code const " + str(const) co_consts.append(extract_def(const)) + depend.add_edge(const, code) else: co_consts.append(repr(const)) values.append((attr, "["+', '.join(co_consts)+"]")) Modified: branches/pbcvm/src/org/python/core/PyBytecode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyBytecode.java 2009-01-29 03:23:40 UTC (rev 5997) +++ branches/pbcvm/src/org/python/core/PyBytecode.java 2009-01-29 03:44:58 UTC (rev 5998) @@ -27,10 +27,10 @@ // end debugging public final static int CO_MAXBLOCKS = 20; // same as in CPython - public final char[] co_code; // to avoid sign issues + public final char[] co_code; // widened to char to avoid signed byte issues public final PyObject[] co_consts; public final String[] co_names; - public final int co_stacksize; // ignored, probably shouldn't be + public final int co_stacksize; // XXX - use to convert PyStack to use PyObject[] instead of ArrayList<PyObject> public final PyObject[] co_lnotab; // ignored private final static int CALL_FLAG_VAR = 1; private final static int CALL_FLAG_KW = 2; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |