[pypy-svn] r22929 - in pypy/dist/pypy: annotation rpython/rctypes rpython/rctypes/test
gromit at codespeak.net
gromit at codespeak.net
Wed Feb 1 20:12:47 CET 2006
Author: gromit
Date: Wed Feb 1 20:12:42 2006
New Revision: 22929
Modified:
pypy/dist/pypy/annotation/binaryop.py
pypy/dist/pypy/annotation/unaryop.py
pypy/dist/pypy/rpython/rctypes/implementation.py
pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py
Log:
CHG: Corrected the annotation to conform to the new design.
ADD: Added some more (and obviously wrong) specialization code.
Modified: pypy/dist/pypy/annotation/binaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/binaryop.py (original)
+++ pypy/dist/pypy/annotation/binaryop.py Wed Feb 1 20:12:42 2006
@@ -761,7 +761,7 @@
except AttributeError:
return SomeCTypesObject(
s_cto.knowntype._type_,
- memorystate=SomeCTypesObject.MEMORYALIAS)
+ memorystate=s_cto.memorystate)
class __extend__(pairtype(SomeCTypesObject, SomeSlice)):
def setitem((s_cto, s_slice), s_iterable):
Modified: pypy/dist/pypy/annotation/unaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/unaryop.py (original)
+++ pypy/dist/pypy/annotation/unaryop.py Wed Feb 1 20:12:42 2006
@@ -628,17 +628,21 @@
def getattr(cto, s_attr):
if s_attr.is_constant() and isinstance(s_attr.const, str):
attr = s_attr.const
- try:
+ # We reactivate the old contents field hack
+ if False:
+ try:
+ atype = cto.knowntype._fields_def_[attr]
+ except AttributeError:
+ # We are dereferencing a pointer by accessing its contents attribute
+ if s_attr.const == "contents":
+ return SomeCTypesObject(
+ cto.knowntype._type_, cto.MEMORYALIAS)
+ else:
+ raise AttributeError(
+ "%r object has no attribute %r" % (
+ cto.knowntype, s_attr.const))
+ else:
atype = cto.knowntype._fields_def_[attr]
- except AttributeError:
- # We are dereferencing a pointer by accessing its contents attribute
- if s_attr.const == "contents":
- return SomeCTypesObject(
- cto.knowntype._type_, SomeCTypesObject.MEMORYALIAS)
- else:
- raise AttributeError(
- "%r object has no attribute %r" % (
- cto.knowntype, s_attr.const))
try:
return atype.annotator_type
except AttributeError:
Modified: pypy/dist/pypy/rpython/rctypes/implementation.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/implementation.py (original)
+++ pypy/dist/pypy/rpython/rctypes/implementation.py Wed Feb 1 20:12:42 2006
@@ -15,6 +15,7 @@
Void
from pypy.rpython.rmodel import Repr, IntegerRepr, inputconst
from pypy.rpython.error import TyperError
+from pypy.annotation.pairtype import pairtype
# ctypes_annotation_list contains various attributes that
@@ -71,6 +72,8 @@
"""
Answer the annotation of the external function's result
"""
+ # TODO: Check whteher teh function returns a pointer
+ # an correct teh memory state appropriately
try:
return self.restype.annotator_type
except AttributeError:
@@ -174,18 +177,49 @@
def RPOINTER(cls):
answer = POINTER(cls)
+
def compute_result_annotation(cls, s_arg):
"""
Answer the result annotation of calling 'cls'.
"""
assert answer is cls
- return SomeCTypesObject(cls)
+ try:
+ memorystate = s_arg.memorystate
+ except AttributeError:
+ memorystate = None
+ return SomeCTypesObject(cls, memorystate)
answer.compute_result_annotation = classmethod(compute_result_annotation)
+
+ def createLowLevelRepresentation( rtyper, annotationObject ):
+ """
+ Create a lowlevel representation for the pointer.
+ """
+ return CtypesPointerRepresentation( rtyper, annotationObject )
+ answer.createLowLevelRepresentation = staticmethod(
+ createLowLevelRepresentation )
+
+ def specialize( cls, highLevelOperation ):
+ #d#print "specialize:", cls, highLevelOperation
+ ctypesStructureType = highLevelOperation.r_result.lowleveltype
+ answer = highLevelOperation.llops.genop(
+ "malloc", [ inputconst( Void, ctypesStructureType ) ],
+ highLevelOperation.r_result )
+ highLevelOperation.genop(
+ "setfield",
+ [ "contents",
+ highLevelOperation.inputarg( highLevelOperation.args_r[ 0 ], 0 ) ],
+ highLevelOperation.r_result )
+ return answer
+ answer.specialize = classmethod( specialize )
+
# We specialcased accessing pointers be getting their contents attribute
# because we can't use the memory state from 'cls'.
# So the obvious way to do it is obsolete (#o#).
- #o#answer._fields_def_ = {"contents": cls}
- answer.default_memorystate = SomeCTypesObject.MEMORYALIAS
+ answer._fields_def_ = {"contents": cls}
+
+ # XXX Think about that twice and think about obsoleting
+ # the obsoletion above
+ answer.default_memorystate = None
return answer
@@ -241,7 +275,7 @@
def __init__( self, rtyper, annotationObject ):
# XXX This .ll_type may not work for pointers or structures
- # conating structures
+ # containg structures
fields = [ ( name, ctypesType.ll_type )
for name, ctypesType in annotationObject.knowntype._fields_ ]
self.lowleveltype = Ptr(
@@ -249,16 +283,7 @@
'CtypesStructure_%s' %
annotationObject.knowntype.__name__, *fields ) )
-
-class CtypesMemoryOwningStructureRepresentation( AbstractCtypesStructureRepresentation ):
- """
- The lowlevel representation of a ctypes structure that owns its memory.
- """
-
def rtype_setattr( self, highLevelOperation ):
- #d#print highLevelOperation.args_v
- #d#print highLevelOperation.args_r
- #d#print highLevelOperation.args_s
highLevelOperation.genop(
"setfield",
highLevelOperation.inputargs(
@@ -272,6 +297,11 @@
highLevelOperation.r_result )
+class CtypesMemoryOwningStructureRepresentation( AbstractCtypesStructureRepresentation ):
+ """
+ The lowlevel representation of a ctypes structure that owns its memory.
+ """
+
class CtypesMemoryAliasStructureRepresentation( AbstractCtypesStructureRepresentation ):
"""
@@ -279,6 +309,28 @@
someone else's memory.
"""
+
+class CtypesPointerRepresentation( AbstractCtypesRepresentation ):
+ """
+ The lowlevel representation of a cytpes pointer.
+ """
+
+ def __init__( self, rtyper, annotationObject ):
+ self.lowleveltype = Ptr(
+ GcStruct(
+ 'CtypesPointer_%s' % annotationObject.knowntype.__name__,
+ ( "contents",
+ rtyper.getrepr(
+ annotationObject.knowntype._type_.compute_annotation() ).lowleveltype ) ) )
+
+ def rtype_getattr( self, highLevelOperation ):
+ return highLevelOperation.genop(
+ "getfield",
+ highLevelOperation.inputargs(
+ *highLevelOperation.args_r[ :2 ] ),
+ highLevelOperation.r_result )
+
+
class __extend__( SomeCTypesObject ):
def rtyper_makerepr( self, rtyper ):
return self.knowntype.createLowLevelRepresentation( rtyper, self )
@@ -286,3 +338,12 @@
def rtyper_makekey( self ):
return self.__class__, self.knowntype, self.memorystate
+
+class __extend__( pairtype( CtypesPointerRepresentation, IntegerRepr ) ):
+ def rtype_getitem( ( self, integer ), highLevelOperation ):
+ print "rtype_getitem:", integer
+ return highLevelOperation.genop(
+ "getfield",
+ "contents",
+ highLevelOperation.r_result )
+
Modified: pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py (original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py Wed Feb 1 20:12:42 2006
@@ -61,6 +61,7 @@
class tagpoint(Structure):
_fields_ = [("x", c_int),
("y", c_int)]
+
# compile and load our local test C file
compile_c_module([thisdir.join("_rctypes_test.c")], "_rctypes_test")
@@ -196,18 +197,25 @@
p = tagpoint()
p.x = 1
p.y = 2
-
return p.x
def _py_test_compile_struct( p, x, y ):
p.x = x
p.y = y
-
return p
def py_test_compile_struct( x, y ):
return _py_test_compile_struct( tagpoint(), x, y ).x
+def py_test_compile_pointer( x, y ):
+ return _py_test_compile_pointer( oppoint_type( tagpoint() ), x, y ).x
+
+def _py_test_compile_pointer( p, x, y ):
+ s = p.contents
+ s.x = x
+ s.y = y
+ return s
+
class Test_rctypes:
@@ -300,7 +308,7 @@
assert len(s.items) == 2
assert s.items[0].knowntype == int
assert s.items[1].knowntype == POINTER(tagpoint)
- assert s.items[1].memorystate == SomeCTypesObject.MEMORYALIAS
+ assert s.items[1].memorystate == SomeCTypesObject.OWNSMEMORY
#d#t.view()
def test_annotate_POINTER_dereference(self):
@@ -311,9 +319,9 @@
assert len(s.items) == 3
assert s.items[0].knowntype == int
assert s.items[1].knowntype == tagpoint
- assert s.items[1].memorystate == SomeCTypesObject.MEMORYALIAS
+ assert s.items[1].memorystate == SomeCTypesObject.OWNSMEMORY
assert s.items[2].knowntype == tagpoint
- assert s.items[2].memorystate == SomeCTypesObject.MEMORYALIAS
+ assert s.items[2].memorystate == SomeCTypesObject.OWNSMEMORY
#d#t.view()
def test_annotate_mixed_memorystate(self):
@@ -379,10 +387,26 @@
pass
def test_compile_struct(self):
- fn = compile(py_test_compile_struct, [int, int])
+ fn = compile( py_test_compile_struct, [ int, int ], False )
res = fn( 42, -42 )
assert res == 42
+ def test_specialize_POINTER_dereference(self):
+ t = TranslationContext()
+ a = t.buildannotator()
+ s = a.build_types(py_testfunc_POINTER_dereference, [tagpoint])
+ assert s.knowntype == tuple
+ try:
+ t.buildrtyper().specialize()
+ finally:
+ t.view()
+ pass
+
+ def x_test_compile_pointer(self):
+ fn = compile( py_test_compile_pointer, [ int, int ], True and False )
+ res = fn( -42, 42 )
+ assert res == -42
+
class Test_array:
More information about the Pypy-commit
mailing list