[pypy-svn] r15308 - in pypy/dist/pypy: annotation interpreter rpython rpython/test translator/goal

arigo at codespeak.net arigo at codespeak.net
Fri Jul 29 01:21:53 CEST 2005


Author: arigo
Date: Fri Jul 29 01:21:45 2005
New Revision: 15308

Modified:
   pypy/dist/pypy/annotation/builtin.py
   pypy/dist/pypy/interpreter/baseobjspace.py
   pypy/dist/pypy/interpreter/pycompiler.py
   pypy/dist/pypy/rpython/objectmodel.py
   pypy/dist/pypy/rpython/rbuiltin.py
   pypy/dist/pypy/rpython/test/test_rbuiltin.py
   pypy/dist/pypy/translator/goal/targetpypymain.py
Log:
(pedronis, arigo)

* added an RPython helper function  we_are_translated()  which returns
  False when run on top of CPython, but True if the code has been translated.
  Using it is considered bad style or performance hacks.

* used this function in pycompiler.py to speed up (quite a
  lot) translate_pypy.py



Modified: pypy/dist/pypy/annotation/builtin.py
==============================================================================
--- pypy/dist/pypy/annotation/builtin.py	(original)
+++ pypy/dist/pypy/annotation/builtin.py	Fri Jul 29 01:21:45 2005
@@ -233,6 +233,9 @@
             clsdef = clsdef.commonbase(getbookkeeper().getclassdef(cls))
     return SomeInstance(clsdef)
 
+def robjmodel_we_are_translated():
+    return immutablevalue(True)
+
 
 ##def rarith_ovfcheck(s_obj):
 ##    if isinstance(s_obj, SomeInteger) and s_obj.unsigned:
@@ -269,6 +272,8 @@
 ##BUILTIN_ANALYZERS[pypy.rpython.rarithmetic.ovfcheck_lshift] = rarith_ovfcheck_lshift
 BUILTIN_ANALYZERS[pypy.rpython.rarithmetic.intmask] = rarith_intmask
 BUILTIN_ANALYZERS[pypy.rpython.objectmodel.instantiate] = robjmodel_instantiate
+BUILTIN_ANALYZERS[pypy.rpython.objectmodel.we_are_translated] = (
+    robjmodel_we_are_translated)
 
 BUILTIN_ANALYZERS[Exception.__init__.im_func] = exception_init
 BUILTIN_ANALYZERS[OSError.__init__.im_func] = exception_init

Modified: pypy/dist/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/dist/pypy/interpreter/baseobjspace.py	Fri Jul 29 01:21:45 2005
@@ -108,6 +108,7 @@
                  uselibfile=False,
                  parser="recparser", 
                  compiler="pyparse",
+                 translating=False,
                  **kw
                  ): 
         "NOT_RPYTHON: Basic initialization of objects."
@@ -121,6 +122,7 @@
         self.options.uselibfile = uselibfile or nofaking
         self.options.compiler = compiler 
         self.options.usemodules = usemodules 
+        self.options.translating = translating
         self.setoptions(**kw)
         self.initialize()
 

Modified: pypy/dist/pypy/interpreter/pycompiler.py
==============================================================================
--- pypy/dist/pypy/interpreter/pycompiler.py	(original)
+++ pypy/dist/pypy/interpreter/pycompiler.py	Fri Jul 29 01:21:45 2005
@@ -4,6 +4,7 @@
 """
 from codeop import PyCF_DONT_IMPLY_DEDENT
 from pypy.interpreter.error import OperationError
+from pypy.rpython.objectmodel import we_are_translated
 
 
 class AbstractCompiler:
@@ -266,6 +267,13 @@
 
     def compile_parse_result(self, parse_result, filename, mode):
         space = self.space
+        if space.options.translating and not we_are_translated():
+            # to avoid to spend too much time in the app-level compiler
+            # while translating PyPy, we can cheat here.  The annotator
+            # doesn't see this because it thinks that we_are_translated()
+            # returns True.
+            return PythonCompiler.compile_parse_result(self, parse_result,
+                                                       filename, mode)
         source_encoding, stack_element = parse_result
         w_nested_tuples = stack_element.as_w_tuple(space, lineno=True)
         if source_encoding is not None:

Modified: pypy/dist/pypy/rpython/objectmodel.py
==============================================================================
--- pypy/dist/pypy/rpython/objectmodel.py	(original)
+++ pypy/dist/pypy/rpython/objectmodel.py	Fri Jul 29 01:21:45 2005
@@ -12,3 +12,7 @@
         return object.__new__(cls)
     else:
         return new.instance(cls)
+
+def we_are_translated():
+    return False
+# annotation -> True

Modified: pypy/dist/pypy/rpython/rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/rbuiltin.py	(original)
+++ pypy/dist/pypy/rpython/rbuiltin.py	Fri Jul 29 01:21:45 2005
@@ -207,6 +207,9 @@
     klass = s_class.const
     return rclass.rtype_new_instance(hop.rtyper, klass, hop.llops)
 
+def rtype_we_are_translated(hop):
+    return hop.inputconst(lltype.Bool, True)
+
 
 # collect all functions
 import __builtin__
@@ -257,6 +260,7 @@
 BUILTIN_TYPER[rarithmetic.intmask] = rtype_intmask
 BUILTIN_TYPER[rarithmetic.r_uint] = rtype_r_uint
 BUILTIN_TYPER[objectmodel.instantiate] = rtype_instantiate
+BUILTIN_TYPER[objectmodel.we_are_translated] = rtype_we_are_translated
 
 from pypy.rpython import extfunctable
 

Modified: pypy/dist/pypy/rpython/test/test_rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rbuiltin.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rbuiltin.py	Fri Jul 29 01:21:45 2005
@@ -1,6 +1,6 @@
 from pypy.rpython.test.test_llinterp import interpret
 from pypy.rpython.test import test_llinterp
-from pypy.rpython.objectmodel import instantiate
+from pypy.rpython.objectmodel import instantiate, we_are_translated
 from pypy.rpython import lltype
 from pypy.objspace.flow import model as flowmodel
 from pypy.tool import udir
@@ -211,3 +211,8 @@
     res = interpret(f, [1])
     assert res is False    
 
+def test_we_are_translated():
+    def f():
+        return we_are_translated()
+    res = interpret(f, [])
+    assert res is True and f() is False

Modified: pypy/dist/pypy/translator/goal/targetpypymain.py
==============================================================================
--- pypy/dist/pypy/translator/goal/targetpypymain.py	(original)
+++ pypy/dist/pypy/translator/goal/targetpypymain.py	Fri Jul 29 01:21:45 2005
@@ -38,7 +38,9 @@
     # XXX why can't I enable this? crashes the annotator!
     gateway.ApplevelClass.use_geninterp = False
 
-    space = StdObjSpace(nofaking=True) 
+    space = StdObjSpace(nofaking=True,
+                        compiler="pyparseapp",
+                        translating=True)
 
     # manually imports app_main.py
     filename = os.path.join(this_dir, 'app_main.py')



More information about the Pypy-commit mailing list