[pypy-svn] r31494 - in pypy/dist/pypy/rpython: lltypesystem rctypes rctypes/test

arigo at codespeak.net arigo at codespeak.net
Tue Aug 22 18:41:14 CEST 2006


Author: arigo
Date: Tue Aug 22 18:41:10 2006
New Revision: 31494

Modified:
   pypy/dist/pypy/rpython/lltypesystem/lloperation.py
   pypy/dist/pypy/rpython/lltypesystem/opimpl.py
   pypy/dist/pypy/rpython/rctypes/avoid_p.py
   pypy/dist/pypy/rpython/rctypes/rvoid_p.py
   pypy/dist/pypy/rpython/rctypes/test/test_rvoid_p.py
Log:
(pedronis, arigo)

Support for c_void_p().value to cast a pointer or address to an int.
Started support for c_void_p(int), but it's a mess.  Delayed until we
really need it.


Modified: pypy/dist/pypy/rpython/lltypesystem/lloperation.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/lloperation.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/lloperation.py	Tue Aug 22 18:41:10 2006
@@ -340,6 +340,7 @@
     'cast_weakadr_to_ptr':  LLOp(canfold=True),
     'cast_weakadr_to_int':  LLOp(canfold=True),
     'cast_adr_to_int':      LLOp(canfold=True),
+    #'cast_int_to_adr':      LLOp(canfold=True),
 
     # __________ GC operations __________
 

Modified: pypy/dist/pypy/rpython/lltypesystem/opimpl.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/opimpl.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/opimpl.py	Tue Aug 22 18:41:10 2006
@@ -279,6 +279,10 @@
     checkadr(adr)
     return llmemory.cast_adr_to_int(adr)
 
+##def op_cast_int_to_adr(x):
+##    assert type(x) is int
+##    return llmemory.cast_int_to_adr(x)
+
 
 def op_unichar_eq(x, y):
     assert isinstance(x, unicode) and len(x) == 1

Modified: pypy/dist/pypy/rpython/rctypes/avoid_p.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/avoid_p.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/avoid_p.py	Tue Aug 22 18:41:10 2006
@@ -1,6 +1,6 @@
 from pypy.rpython.extregistry import ExtRegistryEntry
 from pypy.rpython.rctypes.implementation import CTypesCallEntry, CTypesObjEntry
-from pypy.annotation.model import SomeCTypesObject
+from pypy.annotation.model import SomeInteger, SomeCTypesObject
 
 from ctypes import c_void_p, c_int, POINTER, cast, c_char, c_char_p
 from pypy.rpython.rctypes.astringbuf import StringBufferType
@@ -17,6 +17,13 @@
         r_void_p = hop.r_result
         hop.exception_cannot_occur()
         v_result = r_void_p.allocate_instance(hop.llops)
+        if hop.args_r:
+            raise NotImplementedError("cast_int_to_adr")
+##            from pypy.rpython.lltypesystem import lltype, llmemory
+##            [v_intadr] = hop.inputargs(lltype.Signed)   # xxx id-sized
+##            v_adr = hop.genop('cast_int_to_adr', [v_intadr],
+##                              resulttype = llmemory.Address)
+##            r_void_p.setvalue(hop.llops, v_result, v_adr)
         return v_result
 
 
@@ -24,6 +31,10 @@
     "Annotation and rtyping of c_void_p instances."
     _type_ = c_void_p
 
+    def get_field_annotation(self, s_void_p, fieldname):
+        assert fieldname == "value"
+        return SomeInteger()      # xxx id-sized
+
     def get_repr(self, rtyper, s_void_p):
         from pypy.rpython.rctypes.rvoid_p import CVoidPRepr
         from pypy.rpython.lltypesystem import llmemory

Modified: pypy/dist/pypy/rpython/rctypes/rvoid_p.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rvoid_p.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rvoid_p.py	Tue Aug 22 18:41:10 2006
@@ -14,6 +14,16 @@
             return super(CVoidPRepr, self).convert_const(value)
         raise NotImplementedError("XXX constant pointer passed to void* arg")
 
+    def rtype_getattr(self, hop):
+        s_attr = hop.args_s[1]
+        assert s_attr.is_constant()
+        assert s_attr.const == 'value'
+        v_box = hop.inputarg(self, 0)
+        v_c_adr = self.getvalue(hop.llops, v_box)
+        hop.exception_cannot_occur()
+        return hop.genop('cast_adr_to_int', [v_c_adr],
+                         resulttype = lltype.Signed)
+
 
 class __extend__(pairtype(CCharPRepr, CVoidPRepr),
                  pairtype(PointerRepr, CVoidPRepr)):

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rvoid_p.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rvoid_p.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rvoid_p.py	Tue Aug 22 18:41:10 2006
@@ -5,6 +5,7 @@
 import py.test
 import pypy.rpython.rctypes.implementation
 from pypy.annotation.annrpython import RPythonAnnotator
+from pypy.annotation import model as annmodel
 from pypy.translator.translator import TranslationContext
 from pypy.translator.c.test.test_genc import compile
 from pypy import conftest
@@ -32,6 +33,26 @@
         if conftest.option.view:
             t.view()
 
+    def test_annotate_addr2int(self):
+        def fn():
+            x = c_int(12)
+            p1 = cast(pointer(x), c_void_p)
+            return p1.value
+
+        t = TranslationContext()
+        a = t.buildannotator()
+        s = a.build_types(fn, [])
+        assert isinstance(s, annmodel.SomeInteger)
+
+    def test_annotate_int2addr(self):
+        def fn():
+            return c_void_p(123)
+
+        t = TranslationContext()
+        a = t.buildannotator()
+        s = a.build_types(fn, [])
+        assert s.knowntype == c_void_p
+
 class Test_specialization:
     def test_specialize_c_void_p(self):
         def func():
@@ -82,6 +103,20 @@
 
         interpret(func, [65])
 
+    def test_specialize_addr2int(self):
+        def fn():
+            x = c_int(12)
+            p1 = cast(pointer(x), c_void_p)
+            return p1.value
+        res = interpret(fn, [])
+        assert lltype.typeOf(res) == lltype.Signed    # xxx
+
+##    def test_annotate_int2addr(self):   XXX cast_int_to_adr() not implemented
+##        def fn(n):
+##            return c_void_p(n)
+##        res = interpret(fn, [123])
+##        assert llmemory.cast_adr_to_int(res.c_data[0]) == 123
+
 class Test_compilation:
     def test_compile_c_char_p(self):
         def func():



More information about the Pypy-commit mailing list