[pypy-svn] r14392 - in pypy/dist/pypy: rpython translator/c

arigo at codespeak.net arigo at codespeak.net
Thu Jul 7 18:08:42 CEST 2005


Author: arigo
Date: Thu Jul  7 18:08:37 2005
New Revision: 14392

Added:
   pypy/dist/pypy/translator/c/fixedname.py   (contents, props changed)
Modified:
   pypy/dist/pypy/rpython/rtyper.py
   pypy/dist/pypy/translator/c/database.py
   pypy/dist/pypy/translator/c/g_exception.h
   pypy/dist/pypy/translator/c/genc.py
   pypy/dist/pypy/translator/c/node.py
Log:
Removed fixednames.  Replaced with translator/c/fixedname.py, which
explicitely asks for the name of each type, function and global object that
is needed by the hand-written C include files.  The generated C code will
contain typedef's and #define's to map from the generated names back to the
names expected by this hand-written code.


Modified: pypy/dist/pypy/rpython/rtyper.py
==============================================================================
--- pypy/dist/pypy/rpython/rtyper.py	(original)
+++ pypy/dist/pypy/rpython/rtyper.py	Thu Jul  7 18:08:37 2005
@@ -50,7 +50,6 @@
             r = self.getrepr(s_primitive)
             self.primitive_to_repr[r.lowleveltype] = r
         self.exceptiondata = ExceptionData(self)
-        self.fixednames = self.fixednames.copy()
 
     def getexceptiondata(self):
         return self.exceptiondata    # built at the end of specialize()
@@ -547,6 +546,3 @@
 from pypy.rpython import rlist, rstr, rtuple, rdict 
 from pypy.rpython import rclass, rbuiltin, rpbc, rspecialcase
 from pypy.rpython import rptr
-
-RPythonTyper.fixednames = {}
-RPythonTyper.fixednames[rstr.STR] = True

Modified: pypy/dist/pypy/translator/c/database.py
==============================================================================
--- pypy/dist/pypy/translator/c/database.py	(original)
+++ pypy/dist/pypy/translator/c/database.py	Thu Jul  7 18:08:37 2005
@@ -1,7 +1,6 @@
 from pypy.rpython.lltype import Primitive, Ptr, typeOf
 from pypy.rpython.lltype import Struct, Array, FuncType, PyObject, Void
 from pypy.rpython.lltype import ContainerType, pyobjectptr, OpaqueType, GcStruct
-from pypy.rpython.rmodel import getfunctionptr
 from pypy.objspace.flow.model import Constant
 from pypy.translator.c.primitive import PrimitiveName, PrimitiveType
 from pypy.translator.c.primitive import PrimitiveErrorValue
@@ -21,16 +20,6 @@
         self.containernodes = {}
         self.containerlist = []
         self.namespace = CNameManager()
-        if translator is not None and translator.rtyper is not None:
-            rtyper = translator.rtyper
-            for lltype, nameorflag in rtyper.fixednames.iteritems():
-                if nameorflag is True:
-                    self.namespace.make_reserved_names(lltype._name)
-                #else:
-                #    self.namespace.make_reserved_names(nameorflag)
-            self.fixednames = rtyper.fixednames
-        else:
-            self.fixednames = {}
         self.pyobjmaker = PyObjMaker(self.namespace, self.get, translator)
 
     def gettypedefnode(self, T, varlength=1):
@@ -170,44 +159,3 @@
             return exceptiondata.lltype_of_exception_value
         else:
             return Ptr(PyObject)
-
-    def pre_include_code_lines(self):
-        # generate some #defines that go before the #include to control
-        # what g_exception.h does
-        if self.translator is not None and self.translator.rtyper is not None:
-            exceptiondata = self.translator.rtyper.getexceptiondata()
-
-            TYPE = exceptiondata.lltype_of_exception_type
-            assert isinstance(TYPE, Ptr)
-            typename = self.gettype(TYPE)
-            yield '#define RPYTHON_EXCEPTION_VTABLE %s' % cdecl(typename, '')
-
-            TYPE = exceptiondata.lltype_of_exception_value
-            assert isinstance(TYPE, Ptr)
-            typename = self.gettype(TYPE)
-            yield '#define RPYTHON_EXCEPTION        %s' % cdecl(typename, '')
-
-            fnptr = getfunctionptr(self.translator,
-                                   exceptiondata.ll_exception_match)
-            fnname = self.get(fnptr)
-            yield '#define RPYTHON_EXCEPTION_MATCH  %s' % (fnname,)
-
-            fnptr = getfunctionptr(self.translator,
-                                   exceptiondata.ll_type_of_exc_inst)
-            fnname = self.get(fnptr)
-            yield '#define RPYTHON_TYPE_OF_EXC_INST %s' % (fnname,)
-
-            fnptr = getfunctionptr(self.translator,
-                                   exceptiondata.ll_pyexcclass2exc)
-            fnname = self.get(fnptr)
-            yield '#define RPYTHON_PYEXCCLASS2EXC   %s' % (fnname,)
-
-            for pyexccls in exceptiondata.standardexceptions:
-                exc_llvalue = exceptiondata.ll_pyexcclass2exc(
-                    pyobjectptr(pyexccls))
-                # strange naming here because the macro name must be
-                # a substring of PyExc_%s
-                yield '#define Exc_%s\t%s' % (
-                    pyexccls.__name__, self.get(exc_llvalue))
-
-            self.complete()   # because of the get() and gettype() above

Added: pypy/dist/pypy/translator/c/fixedname.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/c/fixedname.py	Thu Jul  7 18:08:37 2005
@@ -0,0 +1,67 @@
+import types
+from pypy.rpython.lltype import Ptr, pyobjectptr, LowLevelType
+from pypy.translator.c.support import cdecl
+from pypy.rpython.rmodel import getfunctionptr
+from pypy.rpython.rstr import STR
+
+
+def predeclare_common_types(rtyper):
+    # Common types
+    yield ('RPyString',                Ptr(STR))
+
+
+def predeclare_exception_data(rtyper):
+    # Exception-related types and constants
+    exceptiondata = rtyper.getexceptiondata()
+
+    yield ('RPYTHON_EXCEPTION_VTABLE', exceptiondata.lltype_of_exception_type)
+    yield ('RPYTHON_EXCEPTION',        exceptiondata.lltype_of_exception_value)
+
+    yield ('RPYTHON_EXCEPTION_MATCH',  exceptiondata.ll_exception_match)
+    yield ('RPYTHON_TYPE_OF_EXC_INST', exceptiondata.ll_type_of_exc_inst)
+    yield ('RPYTHON_PYEXCCLASS2EXC',   exceptiondata.ll_pyexcclass2exc)
+
+    for pyexccls in exceptiondata.standardexceptions:
+        exc_llvalue = exceptiondata.ll_pyexcclass2exc(pyobjectptr(pyexccls))
+        # strange naming here because the macro name must be
+        # a substring of PyExc_%s
+        yield ('Exc_%s' % pyexccls.__name__, exc_llvalue)
+
+
+def predeclare_all(rtyper):
+    for fn in [predeclare_common_types,
+               predeclare_exception_data
+               ]:
+        for t in fn(rtyper):
+            yield t
+
+# ____________________________________________________________
+
+def pre_include_code_lines(db, rtyper):
+    # generate some #defines that go before the #include to provide
+    # predeclared well-known names for constant objects, functions and
+    # types.  These names are then used by the #included files, like
+    # g_exception.h.
+
+    def predeclare(c_name, lowlevelobj):
+        llname = db.get(lowlevelobj)
+        assert '\n' not in llname
+        return '#define\t%s\t%s' % (c_name, llname)
+
+    def predeclarefn(c_name, ll_func):
+        return predeclare(c_name, getfunctionptr(db.translator, ll_func))
+
+    def predeclaretype(c_typename, lowleveltype):
+        assert isinstance(lowleveltype, Ptr)
+        typename = db.gettype(lowleveltype)
+        return 'typedef %s;' % cdecl(typename, c_typename)
+
+    for c_name, obj in predeclare_all(rtyper):
+        if isinstance(obj, LowLevelType):
+            yield predeclaretype(c_name, obj)
+        elif isinstance(obj, types.FunctionType):
+            yield predeclarefn(c_name, obj)
+        else:
+            yield predeclare(c_name, obj)
+
+    db.complete()   # because of the get() and gettype() above

Modified: pypy/dist/pypy/translator/c/g_exception.h
==============================================================================
--- pypy/dist/pypy/translator/c/g_exception.h	(original)
+++ pypy/dist/pypy/translator/c/g_exception.h	Thu Jul  7 18:08:37 2005
@@ -6,7 +6,7 @@
 
 
 /******************************************************************/
-#ifdef RPYTHON_EXCEPTION_VTABLE  /* RPython version of exceptions */
+#ifdef RPYTHON_EXCEPTION_MATCH   /* RPython version of exceptions */
 /******************************************************************/
 
 static RPYTHON_EXCEPTION_VTABLE	rpython_exc_type = NULL;
@@ -96,5 +96,5 @@
 		PyErr_SetString(Py##exc, msg)    /* pun */
 
 /******************************************************************/
-#endif                                /* RPYTHON_EXCEPTION_VTABLE */
+#endif                                 /* RPYTHON_EXCEPTION_MATCH */
 /******************************************************************/

Modified: pypy/dist/pypy/translator/c/genc.py
==============================================================================
--- pypy/dist/pypy/translator/c/genc.py	(original)
+++ pypy/dist/pypy/translator/c/genc.py	Thu Jul  7 18:08:37 2005
@@ -2,6 +2,7 @@
 import os, py
 from pypy.translator.c.node import PyObjectNode
 from pypy.translator.c.database import LowLevelDatabase
+from pypy.translator.c.fixedname import pre_include_code_lines
 from pypy.translator.gensupp import uniquemodulename
 from pypy.translator.tool.buildpyxmodule import make_module_from_c
 from pypy.rpython.lltype import pyobjectptr
@@ -50,8 +51,7 @@
 
 # ____________________________________________________________
 
-def gen_readable_parts_of_main_c_file(f, database):
-    lines = list(database.pre_include_code_lines())
+def gen_readable_parts_of_main_c_file(f, database, preimplementationlines=[]):
     #
     # All declarations
     #
@@ -77,7 +77,7 @@
     print >> f, '/***********************************************************/'
     print >> f, '/***  Implementations                                    ***/'
     print >> f
-    for line in lines:
+    for line in preimplementationlines:
         print >> f, line
     print >> f, '#include "g_include.h"'
     print >> f
@@ -117,11 +117,17 @@
     for include in includes:
         print >> f, '#include <%s>' % (include,)
 
+    if database.translator is None or database.translator.rtyper is None:
+        preimplementationlines = []
+    else:
+        preimplementationlines = list(
+            pre_include_code_lines(database, database.translator.rtyper))
+
     #
     # 1) All declarations
     # 2) Implementation of functions and global structures and arrays
     #
-    gen_readable_parts_of_main_c_file(f, database)
+    gen_readable_parts_of_main_c_file(f, database, preimplementationlines)
 
     #
     # Debugging info

Modified: pypy/dist/pypy/translator/c/node.py
==============================================================================
--- pypy/dist/pypy/translator/c/node.py	(original)
+++ pypy/dist/pypy/translator/c/node.py	Thu Jul  7 18:08:37 2005
@@ -40,10 +40,7 @@
         else:
             basename = db.gettypedefnode(STRUCT).name
             basename = '%s_len%d' % (basename, varlength)
-        if STRUCT in db.fixednames:
-            self.name = basename
-        else:
-            self.name = db.namespace.uniquename(basename)
+        self.name = db.namespace.uniquename(basename)
         self.dependencies = {}
         self.fields = []
         self.prefix = somelettersfrom(STRUCT._name) + '_'



More information about the Pypy-commit mailing list