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

arigo at codespeak.net arigo at codespeak.net
Mon Sep 6 14:26:30 CEST 2004


Author: arigo
Date: Mon Sep  6 14:26:30 2004
New Revision: 6290

Modified:
   pypy/branch/pypy-genc/translator/genc.py
   pypy/branch/pypy-genc/translator/typer.py
Log:
Implement and use static global constants for constant PyObjects.


Modified: pypy/branch/pypy-genc/translator/genc.py
==============================================================================
--- pypy/branch/pypy-genc/translator/genc.py	(original)
+++ pypy/branch/pypy-genc/translator/genc.py	Mon Sep  6 14:26:30 2004
@@ -41,8 +41,6 @@
         self.genc = genc
         self.bindings = bindings
         self.r_constants = {}
-        self.globaldefs_decl = []
-        self.globaldefs_impl = []
         self.lloperations = {'convert': {}, 'release': {}}
         self.parse_operation_templates()
 
@@ -104,16 +102,7 @@
         return False
 
     def knownanswer(self, llname):
-        if hasattr(llname, 'known_answer'):
-            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
+        return getattr(llname, 'known_answer', None)
 
     # ____________________________________________________________
 
@@ -158,7 +147,7 @@
                     'g_StrObj_%s' % manglestr(value))
             elif value is None:
                 # can convert the constant to Py_None
-                self.can_convert_to_pyobj(r, 'Py_None')
+                self.can_convert_to_pyobj(r, None, 'Py_None')
             elif callable(value) and value in self.genc.llfunctions:
                 # another Python function: can be called with OP_SIMPLE_CALL
                 llfunc = self.genc.llfunctions[value]
@@ -205,20 +194,13 @@
                 print "// XXX not implemented: constant", key
             return r
 
-    def can_convert_to_pyobj(self, r, initexpr, globalname=None):
+    def can_convert_to_pyobj(self, r, initexpr, globalname):
         conv = self.lloperations['convert']
-        if globalname is not None:
-            def writer(z, err):
-                return 'if (!(%s = %s)) goto %s;' % (z, initexpr, err)
-            conv[r, self.R_OBJECT] = writer, True
-            llconst = LLConst('PyObject*', globalname)
-            writer.globaldefs_decl = [llconst]
-            writer.globaldefs_impl = ['%s = %s;' % (llconst.name, initexpr)]
-        else:
-            def writer(z):
-                return '%s = %s; Py_INCREF(%s);' % (z, initexpr, z)
-            conv[r, self.R_OBJECT] = writer, False
-            llconst = LLConst('PyObject*', initexpr)
+        def writer(z):
+            return '%s = %s; Py_INCREF(%s);' % (z, globalname, z)
+        conv[r, self.R_OBJECT] = writer, False
+        llconst = LLConst('PyObject*', globalname, initexpr,
+                          to_declare = bool(initexpr))
         writer.known_answer = [llconst]
 
     def parse_operation_templates(self):
@@ -283,6 +265,7 @@
         else:
             bindings = {}
         self.typeset = CTypeSet(self, bindings)
+        self.initializationcode = []
         self.llfunctions = {}
         self.build_llfunctions()
         self.gen_source()
@@ -318,7 +301,8 @@
         # footer
         print >> f, C_METHOD_TABLE % info
         print >> f, C_INIT_HEADER % info
-        self.gen_globaldefs()
+        for codeline in self.initializationcode:
+            print >> f, '\t' + codeline
         print >> f, C_INIT_FOOTER % info
 
 
@@ -372,15 +356,20 @@
 
         # 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[:]
+        to_declare = []
+        for line in body:
+            if isinstance(line, LLOp):
+                for a in line.args + getattr(line.name, 'known_answer', []):
+                    if isinstance(a, LLConst) and a.to_declare:
+                        to_declare.append(a)
+                        if a.initexpr:
+                            self.initializationcode.append('%s = %s;' % (
+                                a.name, a.initexpr))
+                        a.to_declare = False
+        if to_declare:
+            print >> f, '/* global constant%s */' % ('s'*(len(to_declare)>1))
+            for a in to_declare:
+                print >> f, 'static %s %s;' % (a.type, a.name)
             print >> f
 
         # print header
@@ -425,13 +414,6 @@
                 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),

Modified: pypy/branch/pypy-genc/translator/typer.py
==============================================================================
--- pypy/branch/pypy-genc/translator/typer.py	(original)
+++ pypy/branch/pypy-genc/translator/typer.py	Mon Sep  6 14:26:30 2004
@@ -14,7 +14,11 @@
         self.name = name
 
 class LLConst(LLVar):
-    "An LLVar whose name is abused to be a constant expression."
+    "A global, constant, preinitialized variable."
+    def __init__(self, type, name, initexpr=None, to_declare=False):
+        LLVar.__init__(self, type, name)
+        self.initexpr = initexpr
+        self.to_declare = to_declare
 
 class LLOp:
     "A low-level operation."



More information about the Pypy-commit mailing list