[pypy-svn] r12918 - in pypy/dist/pypy: annotation rpython

arigo at codespeak.net arigo at codespeak.net
Tue May 31 14:37:45 CEST 2005


Author: arigo
Date: Tue May 31 14:37:45 2005
New Revision: 12918

Modified:
   pypy/dist/pypy/annotation/builtin.py
   pypy/dist/pypy/rpython/rbool.py
   pypy/dist/pypy/rpython/rbuiltin.py
   pypy/dist/pypy/rpython/rfloat.py
   pypy/dist/pypy/rpython/rint.py
Log:
Support for int(), float() and bool() built-in functions.


Modified: pypy/dist/pypy/annotation/builtin.py
==============================================================================
--- pypy/dist/pypy/annotation/builtin.py	(original)
+++ pypy/dist/pypy/annotation/builtin.py	Tue May 31 14:37:45 2005
@@ -18,34 +18,51 @@
 def immutablevalue(x):
     return getbookkeeper().immutablevalue(x)
 
+def constpropagate(func, args_s, s_result):
+    """Returns s_result unless all args are constants, in which case the
+    func() is called and a constant result is returned (it must be contained
+    in s_result).
+    """
+    args = []
+    for s in args_s:
+        if not s.is_constant():
+            return s_result
+        args.append(s.const)
+    realresult = func(*args)
+    s_realresult = immutablevalue(realresult)
+    if not s_result.contains(s_realresult):
+        raise Exception("%s%r returned %r, which is not contained in %s" % (
+            func, args, realresult, s_result))
+    return s_realresult
+
+# ____________________________________________________________
+
 def builtin_range(*args):
     return getbookkeeper().newlist(SomeInteger())  # XXX nonneg=...
 
 builtin_xrange = builtin_range # xxx for now allow it
 
 def builtin_bool(s_obj):
-    r = SomeBool()
-    if s_obj.is_constant():
-        r.const = bool(s_obj.const)
-    return r
+    return constpropagate(bool, [s_obj], SomeBool())
 
 def builtin_int(s_obj):
-    return SomeInteger()
+    return constpropagate(int, [s_obj], SomeInteger())
 
 def restricted_uint(s_obj):    # for r_uint
-    return SomeInteger(nonneg=True, unsigned=True)
+    return constpropagate(r_uint, [s_obj],
+                          SomeInteger(nonneg=True, unsigned=True))
 
 def builtin_float(s_obj):
-    return SomeFloat()
+    return constpropagate(float, [s_obj], SomeFloat())
 
 def builtin_long(s_obj):
-    return SomeObject()
+    return SomeObject()   # XXX go away
 
 def builtin_chr(s_int):
-    return SomeChar()
+    return constpropagate(chr, [s_int], SomeChar())
 
 def builtin_unichr(s_int):
-    return SomeUnicodeCodePoint()
+    return constpropagate(unichr, [s_int], SomeUnicodeCodePoint())
 
 def builtin_unicode(s_obj):
     raise TypeError, "unicode() calls should not happen at interp-level"

Modified: pypy/dist/pypy/rpython/rbool.py
==============================================================================
--- pypy/dist/pypy/rpython/rbool.py	(original)
+++ pypy/dist/pypy/rpython/rbool.py	Tue May 31 14:37:45 2005
@@ -29,3 +29,11 @@
     def rtype_is_true(_, hop):
         vlist = hop.inputargs(Bool)
         return vlist[0]
+
+    def rtype_int(_, hop):
+        vlist = hop.inputargs(Signed)
+        return vlist[0]
+
+    def rtype_float(_, hop):
+        vlist = hop.inputargs(Float)
+        return vlist[0]

Modified: pypy/dist/pypy/rpython/rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/rbuiltin.py	(original)
+++ pypy/dist/pypy/rpython/rbuiltin.py	Tue May 31 14:37:45 2005
@@ -1,5 +1,5 @@
 from pypy.annotation.pairtype import pair, pairtype
-from pypy.annotation.model import SomeBuiltin, SomeObject
+from pypy.annotation.model import SomeBuiltin, SomeObject, SomeString
 from pypy.rpython.lltype import malloc, typeOf, Void, Signed
 from pypy.rpython.rtyper import TyperError
 
@@ -45,6 +45,31 @@
 
 # ____________________________________________________________
 
+def rtype_builtin_bool(hop):
+    assert hop.nb_args == 1
+    return hop.args_s[0].rtype_is_true(hop)
+
+def rtype_builtin_int(hop):
+    if isinstance(hop.args_s[0], SomeString):
+        raise TyperError('int("string") not supported')
+    assert hop.nb_args == 1
+    return hop.args_s[0].rtype_int(hop)
+
+def rtype_builtin_float(hop):
+    assert hop.nb_args == 1
+    return hop.args_s[0].rtype_float(hop)
+
+
+# collect all functions
+import __builtin__
+BUILTIN_TYPER = {}
+for name, value in globals().items():
+    if name.startswith('rtype_builtin_'):
+        original = getattr(__builtin__, name[14:])
+        BUILTIN_TYPER[original] = value
+
+# annotation of low-level types
+
 def rtype_malloc(hop):
     assert hop.args_s[0].is_constant()
     if hop.nb_args == 1:
@@ -60,7 +85,5 @@
     return hop.inputconst(Void, hop.s_result.const)
 
 
-BUILTIN_TYPER = {
-    malloc: rtype_malloc,
-    typeOf: rtype_typeOf,
-    }
+BUILTIN_TYPER[malloc] = rtype_malloc
+BUILTIN_TYPER[typeOf] = rtype_typeOf

Modified: pypy/dist/pypy/rpython/rfloat.py
==============================================================================
--- pypy/dist/pypy/rpython/rfloat.py	(original)
+++ pypy/dist/pypy/rpython/rfloat.py	Tue May 31 14:37:45 2005
@@ -129,3 +129,9 @@
     def rtype_pos(_, hop):
         vlist = hop.inputargs(Float)
         return vlist[0]
+
+    def rtype_int(_, hop):
+        vlist = hop.inputargs(Float)
+        return hop.genop('cast_float_to_int', vlist, resulttype=Signed)
+
+    rtype_float = rtype_pos

Modified: pypy/dist/pypy/rpython/rint.py
==============================================================================
--- pypy/dist/pypy/rpython/rint.py	(original)
+++ pypy/dist/pypy/rpython/rint.py	Tue May 31 14:37:45 2005
@@ -197,6 +197,16 @@
             vlist = hop.inputargs(Signed)
         return vlist[0]
 
+    def rtype_int(s_int, hop):
+        if s_int.unsigned:
+            raise TyperError("use intmask() instead of int(r_uint(...))")
+        vlist = hop.inputargs(Signed)
+        return vlist[0]
+
+    def rtype_float(_, hop):
+        vlist = hop.inputargs(Float)
+        return vlist[0]
+
 #
 
 class __extend__(pairtype(SomeObject, SomeInteger)):



More information about the Pypy-commit mailing list