[pypy-svn] r6268 - pypy/branch/pypy-genc/translator

arigo at codespeak.net arigo at codespeak.net
Thu Sep 2 16:05:35 CEST 2004


Author: arigo
Date: Thu Sep  2 16:05:35 2004
New Revision: 6268

Modified:
   pypy/branch/pypy-genc/translator/genc.py
Log:
Precomputing constant PyIntObjects.


Modified: pypy/branch/pypy-genc/translator/genc.py
==============================================================================
--- pypy/branch/pypy-genc/translator/genc.py	(original)
+++ pypy/branch/pypy-genc/translator/genc.py	Thu Sep  2 16:05:35 2004
@@ -45,7 +45,8 @@
         self.genc = genc
         self.bindings = bindings
         self.r_constants = {}
-        self.globaldefs = []
+        self.globaldefs_decl = []
+        self.globaldefs_impl = []
         self.lloperations = {'convert': {}, 'release': {}}
         self.parse_operation_templates()
 
@@ -91,9 +92,12 @@
 
     def knownanswer(self, llname):
         if hasattr(llname, 'known_answer'):
-            if hasattr(llname, 'globaldef'):  # global definition required
-                self.consts_used.append(llname.globaldef)
-                del llname.globaldef
+            if hasattr(llname, 'globaldefs_decl'):  # global definition required
+                self.globaldefs_decl.extend(llname.globaldefs_decl)
+                del llname.globaldefs_decl
+            if hasattr(llname, 'globaldefs_impl'):  # global definition required
+                self.globaldefs_impl.extend(llname.globaldefs_impl)
+                del llname.globaldefs_impl
             return llname.known_answer
         else:
             return None
@@ -124,12 +128,17 @@
                 # can convert the constant to a C int
                 def writer(z):
                     return '%s = %d;' % (z, value)
-                writer.known_answer = [LLConst(self.R_INT, '%d' % value)]
                 conv[r, self.R_INT] = writer, False
+                writer.known_answer = [LLConst(self.R_INT, '%d' % value)]
                 # can convert the constant to a PyObject*
                 def writer(z, err):
                     return 'convert_io(%d, %s, %s)' % (value, z, err)
                 conv[r, self.R_OBJECT] = writer, True
+                llconst = LLConst('PyObject*', 'g_IntObject_%d' % value)
+                writer.known_answer = [llconst]
+                writer.globaldefs_decl = [llconst]
+                writer.globaldefs_impl = ['%s = PyInt_FromLong(%d);' %
+                                          (llconst.name, value)]
             elif isinstance(value, str):
                 # can convert the constant to a PyObject*
                 def writer(z, err):
@@ -255,7 +264,9 @@
 
         # footer
         print >> f, C_METHOD_TABLE % info
-        print >> f, C_FOOTER % info
+        print >> f, C_INIT_HEADER % info
+        self.gen_globaldefs()
+        print >> f, C_INIT_FOOTER % info
 
 
     def build_llfunctions(self):
@@ -303,13 +314,26 @@
         f = self.f
         llfunc = self.llfunctions[func]
 
+        # generate the body of the function
+        body = list(llfunc.ll_body())
+
+        # print the declaration of the new global constants needed by
+        # the current function
+        if self.typeset.globaldefs_decl:
+            if len(self.typeset.globaldefs_decl) > 1:
+                s = 's'
+            else:
+                s = ''
+            print >> f, '/* global constant%s */' % s
+            for llconst in self.typeset.globaldefs_decl:
+                print >> f, 'static %s %s;' % (llconst.type, llconst.name)
+            del self.typeset.globaldefs_decl[:]
+            print >> f
+
         # print header
         print >> f, self.cfunction_header(func)
         print >> f, '{'
 
-        # generate the body of the function
-        body = list(llfunc.ll_body())
-
         # collect and print all the local variables from the body
         llargs, rettype = llfunc.ll_header()
         lllocals = llargs[:]
@@ -348,6 +372,13 @@
                 print >> f
         print >> f, '}'
 
+    def gen_globaldefs(self):
+        # generate the global definitions
+        f = self.f
+        for code in self.typeset.globaldefs_impl:
+            for codeline in code.split('\n'):
+                print >> f, '\t' + codeline
+
 
 LL_ONLY_OPERATIONS = {
     'move': lambda x,y: '%s = %s;' % (y,x),
@@ -372,10 +403,11 @@
 \t{NULL, NULL}
 };'''
 
-C_FOOTER = '''
+C_INIT_HEADER = '''
 void init%(modname)s(void)
 {
-\tPy_InitModule("%(modname)s", %(modname)sMethods);
-}'''
+\tPy_InitModule("%(modname)s", %(modname)sMethods);'''
+
+C_INIT_FOOTER = '''}'''
 
 # ____________________________________________________________



More information about the Pypy-commit mailing list