[pypy-svn] r7301 - in pypy/trunk/src: goal pypy/annotation pypy/translator

mwh at codespeak.net mwh at codespeak.net
Tue Nov 16 17:56:55 CET 2004


Author: mwh
Date: Tue Nov 16 17:56:54 2004
New Revision: 7301

Modified:
   pypy/trunk/src/goal/translate_pypy.py
   pypy/trunk/src/pypy/annotation/factory.py
   pypy/trunk/src/pypy/annotation/unaryop.py
   pypy/trunk/src/pypy/translator/annrpython.py
   pypy/trunk/src/pypy/translator/genc.py
   pypy/trunk/src/pypy/translator/translator.py
Log:
Hacks towards goal/translate_pypy.py.
Track which attributes are read from PreBuiltConstants (beware: new
jargon appears to be 'pbc' == 'PreBuiltConstant').
Support 'freezing' a translator, after which point no more flow graphs
will be built.
Support dumping a few more types (implementations of varying degrees 
of hackishness).


Modified: pypy/trunk/src/goal/translate_pypy.py
==============================================================================
--- pypy/trunk/src/goal/translate_pypy.py	(original)
+++ pypy/trunk/src/goal/translate_pypy.py	Tue Nov 16 17:56:54 2004
@@ -82,6 +82,7 @@
 
     try:
         analyse()
+        t.frozen = True
         print '-'*60
         print 'Generating C code...'
         t.ccompile()

Modified: pypy/trunk/src/pypy/annotation/factory.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/factory.py	(original)
+++ pypy/trunk/src/pypy/annotation/factory.py	Tue Nov 16 17:56:54 2004
@@ -38,6 +38,7 @@
         self.creationpoints = {} # map position-in-a-block to its Factory
         self.userclasses = {}    # map classes to ClassDefs
         self.userclasseslist = []# userclasses.keys() in creation order
+        self.attrs_read_from_constants = {}
 
     def enter(self, position_key):
         """Start of an operation.

Modified: pypy/trunk/src/pypy/annotation/unaryop.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/unaryop.py	(original)
+++ pypy/trunk/src/pypy/annotation/unaryop.py	Tue Nov 16 17:56:54 2004
@@ -9,6 +9,7 @@
 from pypy.annotation.model import SomeTuple, SomeImpossibleValue
 from pypy.annotation.model import SomeInstance, SomeBuiltin, SomeClass
 from pypy.annotation.model import SomeFunction, SomeMethod, SomeIterator
+from pypy.annotation.model import SomePrebuiltConstant
 from pypy.annotation.model import immutablevalue
 from pypy.annotation.model import unionof, set, setunion, missing_operation
 from pypy.annotation.factory import BlockedInference, getbookkeeper
@@ -179,3 +180,19 @@
             # call func(s_self, *arglist)
             results.append(factory.pycall(func, s_self, *args))
         return unionof(*results)
+
+class __extend__(SomePrebuiltConstant):
+    def getattr(pbc, s_attr):
+        assert s_attr.is_constant()
+        bookkeeper = getbookkeeper()
+        actuals = []
+        attr = s_attr.const
+        for c in pbc.prebuiltinstances:
+            bookkeeper.attrs_read_from_constants.setdefault(c, {})[attr] = True
+            if hasattr(c.value, attr):
+                actuals.append(immutablevalue(getattr(c.value, s_attr.const)))
+        return unionof(*actuals)
+
+    def setattr(pbc, s_attr, s_value):
+        raise Exception, "oops!"
+    

Modified: pypy/trunk/src/pypy/translator/annrpython.py
==============================================================================
--- pypy/trunk/src/pypy/translator/annrpython.py	(original)
+++ pypy/trunk/src/pypy/translator/annrpython.py	Tue Nov 16 17:56:54 2004
@@ -88,6 +88,9 @@
             self.bindings[v] = s_value
             yield v
 
+    def getpbcattrs(self, pbc):
+        c = Constant(pbc)
+        return self.bookkeeper.attrs_read_from_constants.get(c, {})
 
     #___ medium-level interface ____________________________
 

Modified: pypy/trunk/src/pypy/translator/genc.py
==============================================================================
--- pypy/trunk/src/pypy/translator/genc.py	(original)
+++ pypy/trunk/src/pypy/translator/genc.py	Tue Nov 16 17:56:54 2004
@@ -8,6 +8,7 @@
 from pypy.objspace.flow.model import FunctionGraph, Block, Link, last_exception
 from pypy.objspace.flow.model import traverse, uniqueitems, checkgraph
 from pypy.translator.simplify import remove_direct_loops
+from types import FunctionType
 
 # ____________________________________________________________
 
@@ -73,6 +74,24 @@
         self.seennames[name] = True
         return name
 
+    def nameof_object(self, value):
+        if type(value) is not object:
+            raise Exception, "nameof(%r)" % (value,)
+        name = self.uniquename('g_object')
+        self.globaldecl.append('static PyObject* %s;' % name)
+        self.initcode.append('INITCHK(%s = PyObject_CallFunction((PyObject*)&PyBaseObject_Type, ""))'%name)
+        return name
+
+    def nameof_module(self, value):
+##         assert not hasattr(value, "__file__") or \
+##                not (value.__file__.endswith('.pyc') or value.__file__.endswith('.py')), \
+##                "%r is not a builtin module (probably :)"%value
+        name = self.uniquename('mod%s'%value.__name__)
+        self.globaldecl.append('static PyObject* %s;' % name)
+        self.initcode.append('INITCHK(%s = PyImport_Import("%s"))'%(name, value.__name__))
+        return name
+        
+
     def nameof_int(self, value):
         if value >= 0:
             name = 'gint_%d' % value
@@ -103,6 +122,8 @@
         return name
 
     def nameof_function(self, func):
+        if self.translator.frozen:
+            assert func in self.translator.flowgraphs, func
         name = self.uniquename('gfunc_' + func.__name__)
         self.globaldecl.append('static PyObject* %s;' % name)
         self.initcode.append('INITCHK(%s = PyCFunction_New('
@@ -111,6 +132,20 @@
         self.pendingfunctions.append(func)
         return name
 
+    def nameof_staticmethod(self, sm):
+        # XXX XXX XXXX
+        func = sm.__get__(42.5)
+        if self.translator.frozen:
+            assert func in self.translator.flowgraphs, func
+            
+        name = self.uniquename('gsm_' + func.__name__)
+        self.globaldecl.append('static PyObject* %s;' % name)
+        self.initcode.append('INITCHK(%s = PyCFunction_New('
+                             '&ml_%s, NULL))' % (name, name))
+        self.pendingfunctions.append(func)
+        return name
+
+
     def nameof_instancemethod(self, meth):
         if meth.im_self is None:
             # no error checking here
@@ -125,7 +160,13 @@
                 'INITCHK(%s = gencfunc_descr_get(%s, %s, %s))'%(
                 us, func, ob, typ))
             return us
-                                   
+
+    def should_translate_attr(self, pbc, attr):
+        ann = self.translator.annotator
+        if ann is None:
+            return "good luck" # True
+        return attr in ann.getpbcattrs(pbc)
+
     def nameof_instance(self, instance):
         name = self.uniquename('ginst_' + instance.__class__.__name__)
         cls = self.nameof(instance.__class__)
@@ -133,8 +174,9 @@
             content = instance.__dict__.items()
             content.sort()
             for key, value in content:
-                yield 'INITCHK(SETUP_INSTANCE_ATTR(%s, "%s", %s))' % (
-                    name, key, self.nameof(value))
+                if self.should_translate_attr(instance, key):
+                    yield 'INITCHK(SETUP_INSTANCE_ATTR(%s, "%s", %s))' % (
+                        name, key, self.nameof(value))
         self.globaldecl.append('static PyObject* %s;' % name)
         self.initcode.append('INITCHK(SETUP_INSTANCE(%s, %s))' % (
             name, cls))
@@ -168,6 +210,9 @@
                         continue
                     # XXX some __NAMES__ are important... nicer solution sought
                     #raise Exception, "unexpected name %r in class %s"%(key, cls)
+                if isinstance(value, FunctionType) and value not in self.translator.flowgraphs and self.translator.frozen:
+                    continue
+                    
                 yield 'INITCHK(SETUP_CLASS_ATTR(%s, "%s", %s))' % (
                     name, key, self.nameof(value))
         self.globaldecl.append('static PyObject* %s;' % name)
@@ -277,6 +322,10 @@
             del g[:]
 
     def gen_cfunction(self, func):
+##         print 'gen_cfunction (%s:%d) %s' % (
+##             func.func_globals.get('__name__', '?'),
+##             func.func_code.co_firstlineno,
+##             func.__name__)
         f = self.f
         body = list(self.cfunction_body(func))
         self.gen_global_declarations()

Modified: pypy/trunk/src/pypy/translator/translator.py
==============================================================================
--- pypy/trunk/src/pypy/translator/translator.py	(original)
+++ pypy/trunk/src/pypy/translator/translator.py	Tue Nov 16 17:56:54 2004
@@ -57,6 +57,7 @@
         self.flowgraphs = {}  # {function: graph}
         self.functions = []   # the keys of self.flowgraphs, in creation order
         self.callgraph = {}   # {opaque_tag: (caller, callee)}
+        self.frozen = False   # when frozen, no more flowgraphs can be generated
         self.getflowgraph()
 
     def getflowgraph(self, func=None, called_by=None, call_tag=None):
@@ -70,6 +71,7 @@
                     func.func_globals.get('__name__', '?'),
                     func.func_code.co_firstlineno,
                     func.__name__)
+            assert not self.frozen
             space = FlowObjSpace()
             graph = space.build_flow(func)
             if self.simplifying:



More information about the Pypy-commit mailing list