[pypy-svn] r23577 - in pypy/dist/pypy/rpython: . lltypesystem memory memory/test
cfbolz at codespeak.net
cfbolz at codespeak.net
Tue Feb 21 23:16:12 CET 2006
Author: cfbolz
Date: Tue Feb 21 23:16:11 2006
New Revision: 23577
Modified:
pypy/dist/pypy/rpython/llinterp.py
pypy/dist/pypy/rpython/lltypesystem/lltype.py
pypy/dist/pypy/rpython/memory/gclltype.py
pypy/dist/pypy/rpython/memory/gcwrapper.py
pypy/dist/pypy/rpython/memory/lltypesimulation.py
pypy/dist/pypy/rpython/memory/test/test_llinterpsim.py
pypy/dist/pypy/rpython/memory/test/test_lltypesimulation.py
Log:
(pedronis, cfbolz):
first step in sanitizing the different _ptr/address implementations: delegate
to heap (former llt) in llinterpreter only for operations where this makes
sense.
Modified: pypy/dist/pypy/rpython/llinterp.py
==============================================================================
--- pypy/dist/pypy/rpython/llinterp.py (original)
+++ pypy/dist/pypy/rpython/llinterp.py Tue Feb 21 23:16:11 2006
@@ -30,17 +30,17 @@
class LLInterpreter(object):
""" low level interpreter working with concrete values. """
- def __init__(self, typer, lltype=lltype):
+ def __init__(self, typer, heap=lltype):
self.bindings = {}
self.typer = typer
- self.llt = lltype #module that contains the used lltype classes
+ self.heap = heap #module that provides malloc, etc for lltypes
self.active_frame = None
- # XXX hack hack hack: set gc to None because
+ # XXX hack: set gc to None because
# prepare_graphs_and_create_gc might already use the llinterpreter!
self.gc = None
- if hasattr(lltype, "prepare_graphs_and_create_gc"):
+ if hasattr(heap, "prepare_graphs_and_create_gc"):
flowgraphs = typer.annotator.translator.graphs
- self.gc = lltype.prepare_graphs_and_create_gc(self, flowgraphs)
+ self.gc = heap.prepare_graphs_and_create_gc(self, flowgraphs)
def eval_graph(self, graph, args=()):
llframe = LLFrame(graph, args, self)
@@ -102,6 +102,8 @@
'lt': True, 'le': True,
'eq': True, 'ne': True,
'is_true': True}
+def checkptr(ptr):
+ return isinstance(lltype.typeOf(ptr), lltype.Ptr)
class LLFrame(object):
def __init__(self, graph, args, llinterpreter, f_back=None):
@@ -109,7 +111,7 @@
self.graph = graph
self.args = args
self.llinterpreter = llinterpreter
- self.llt = llinterpreter.llt
+ self.heap = llinterpreter.heap
self.bindings = {}
self.f_back = f_back
self.curr_block = None
@@ -131,8 +133,8 @@
self.setvar(var, val)
def setvar(self, var, val):
- if var.concretetype is not self.llt.Void:
- assert self.llinterpreter.typer.type_system.isCompatibleType(self.llt.typeOf(val), var.concretetype)
+ if var.concretetype is not lltype.Void:
+ assert self.llinterpreter.typer.type_system.isCompatibleType(lltype.typeOf(val), var.concretetype)
assert isinstance(var, Variable)
self.bindings[var] = val
@@ -145,8 +147,8 @@
val = varorconst.value
except AttributeError:
val = self.bindings[varorconst]
- if varorconst.concretetype is not self.llt.Void:
- assert self.llinterpreter.typer.type_system.isCompatibleType(self.llt.typeOf(val), varorconst.concretetype)
+ if varorconst.concretetype is not lltype.Void:
+ assert self.llinterpreter.typer.type_system.isCompatibleType(lltype.typeOf(val), varorconst.concretetype)
return val
# _______________________________________________________
@@ -270,7 +272,7 @@
else:
exc_class = exc.__class__
evalue = self.op_direct_call(exdata.fn_pyexcclass2exc,
- self.llt.pyobjectptr(exc_class))
+ self.heap.pyobjectptr(exc_class))
etype = self.op_direct_call(exdata.fn_type_of_exc_inst, evalue)
raise LLException(etype, evalue, *extraargs)
@@ -283,12 +285,13 @@
def find_roots(self, roots):
#log.findroots(self.curr_block.inputargs)
+ PyObjPtr = lltype.Ptr(lltype.PyObject)
for arg in self.curr_block.inputargs:
if (isinstance(arg, Variable) and
- isinstance(self.getval(arg), self.llt._ptr)):
+ isinstance(getattr(arg, 'concretetype', PyObjPtr), lltype.Ptr)):
roots.append(self.getval(arg))
for op in self.curr_block.operations[:self.curr_operation_index]:
- if isinstance(self.getval(op.result), self.llt._ptr):
+ if isinstance(getattr(op.result, 'concretetype', PyObjPtr), lltype.Ptr):
roots.append(self.getval(op.result))
# __________________________________________________________
@@ -305,8 +308,8 @@
def op_setfield(self, obj, fieldname, fieldvalue):
# obj should be pointer
- FIELDTYPE = getattr(self.llt.typeOf(obj).TO, fieldname)
- if FIELDTYPE != self.llt.Void:
+ FIELDTYPE = getattr(lltype.typeOf(obj).TO, fieldname)
+ if FIELDTYPE != lltype.Void:
gc = self.llinterpreter.gc
if gc is None or not gc.needs_write_barrier(FIELDTYPE):
setattr(obj, fieldname, fieldvalue)
@@ -320,8 +323,8 @@
def op_setarrayitem(self, array, index, item):
# array should be a pointer
- ITEMTYPE = self.llt.typeOf(array).TO.OF
- if ITEMTYPE != self.llt.Void:
+ ITEMTYPE = lltype.typeOf(array).TO.OF
+ if ITEMTYPE != lltype.Void:
gc = self.llinterpreter.gc
if gc is None or not gc.needs_write_barrier(ITEMTYPE):
array[index] = item
@@ -361,7 +364,7 @@
result = self.op_direct_call(malloc, *args)
return self.llinterpreter.gc.adjust_result_malloc(result, obj)
else:
- return self.llt.malloc(obj)
+ return self.heap.malloc(obj)
def op_malloc_varsize(self, obj, size):
if self.llinterpreter.gc is not None:
@@ -371,45 +374,44 @@
return self.llinterpreter.gc.adjust_result_malloc(result, obj, size)
else:
try:
- return self.llt.malloc(obj, size)
+ return self.heap.malloc(obj, size)
except MemoryError:
self.make_llexception()
def op_flavored_malloc(self, flavor, obj):
assert isinstance(flavor, str)
if flavor == "stack":
- if isinstance(obj, self.llt.Struct) and obj._arrayfld is None:
- result = self.llt.malloc(obj)
+ if isinstance(obj, lltype.Struct) and obj._arrayfld is None:
+ result = self.heap.malloc(obj)
self.alloca_objects.append(result)
return result
else:
raise ValueError("cannot allocate variable-sized things on the stack")
- return self.llt.malloc(obj, flavor=flavor)
+ return self.heap.malloc(obj, flavor=flavor)
def op_flavored_free(self, flavor, obj):
assert isinstance(flavor, str)
- self.llt.free(obj, flavor=flavor)
+ self.heap.free(obj, flavor=flavor)
def op_getfield(self, obj, field):
- assert isinstance(obj, self.llt._ptr)
+ assert checkptr(obj)
result = getattr(obj, field)
# check the difference between op_getfield and op_getsubstruct:
# the former returns the real field, the latter a pointer to it
- assert self.llt.typeOf(result) == getattr(self.llt.typeOf(obj).TO,
- field)
+ assert lltype.typeOf(result) == getattr(lltype.typeOf(obj).TO, field)
return result
def op_getsubstruct(self, obj, field):
- assert isinstance(obj, self.llt._ptr)
+ assert checkptr(obj)
result = getattr(obj, field)
# check the difference between op_getfield and op_getsubstruct:
# the former returns the real field, the latter a pointer to it
- assert (self.llt.typeOf(result) ==
- self.llt.Ptr(getattr(self.llt.typeOf(obj).TO, field)))
+ assert (lltype.typeOf(result) ==
+ lltype.Ptr(getattr(lltype.typeOf(obj).TO, field)))
return result
def op_getarraysubstruct(self, array, index):
- assert isinstance(array, self.llt._ptr)
+ assert checkptr(array)
result = array[index]
return result
# the diff between op_getarrayitem and op_getarraysubstruct
@@ -417,43 +419,42 @@
def op_getarraysize(self, array):
#print array,type(array),dir(array)
- assert isinstance(self.llt.typeOf(array).TO, self.llt.Array)
+ assert isinstance(lltype.typeOf(array).TO, lltype.Array)
return len(array)
def op_cast_pointer(self, tp, obj):
# well, actually this is what's now in the globals.
- return self.llt.cast_pointer(tp, obj)
+ return lltype.cast_pointer(tp, obj)
def op_ptr_eq(self, ptr1, ptr2):
- assert isinstance(ptr1, self.llt._ptr)
- assert isinstance(ptr2, self.llt._ptr)
+ assert checkptr(ptr1)
+ assert checkptr(ptr2)
return ptr1 == ptr2
def op_ptr_ne(self, ptr1, ptr2):
- assert isinstance(ptr1, self.llt._ptr)
- assert isinstance(ptr2, self.llt._ptr)
+ assert checkptr(ptr1)
+ assert checkptr(ptr2)
return ptr1 != ptr2
def op_ptr_nonzero(self, ptr1):
- assert isinstance(ptr1, self.llt._ptr)
+ assert checkptr(ptr1)
return bool(ptr1)
def op_ptr_iszero(self, ptr1):
- assert isinstance(ptr1, self.llt._ptr)
+ assert checkptr(ptr1)
return not bool(ptr1)
def op_cast_ptr_to_int(self, ptr1):
- assert isinstance(ptr1, self.llt._ptr)
- assert isinstance(self.llt.typeOf(ptr1).TO, (self.llt.Array,
- self.llt.Struct))
- return self.llt.cast_ptr_to_int(ptr1)
+ assert checkptr(ptr1)
+ assert isinstance(lltype.typeOf(ptr1).TO, (lltype.Array, lltype.Struct))
+ return lltype.cast_ptr_to_int(ptr1)
def op_cast_ptr_to_adr(self, ptr):
- assert isinstance(ptr, self.llt._ptr)
+ assert checkptr(ptr)
return objectmodel.cast_ptr_to_adr(ptr)
def op_cast_adr_to_ptr(self, TYPE, adr):
- assert self.llt.typeOf(adr) == llmemory.Address
+ assert lltype.typeOf(adr) == llmemory.Address
return objectmodel.cast_adr_to_ptr(adr, TYPE)
def op_cast_int_to_float(self, i):
@@ -561,58 +562,58 @@
exec py.code.Source("""
def op_%(opname)s(self, *pyobjs):
for pyo in pyobjs:
- assert self.llt.typeOf(pyo) == self.llt.Ptr(self.llt.PyObject)
+ assert lltype.typeOf(pyo) == lltype.Ptr(lltype.PyObject)
func = opimpls[%(opname)r]
try:
pyo = func(*[pyo._obj.value for pyo in pyobjs])
except Exception:
self.make_llexception()
- return self.llt.pyobjectptr(pyo)
+ return self.heap.pyobjectptr(pyo)
""" % locals()).compile()
del opname
def op_simple_call(self, f, *args):
- assert self.llt.typeOf(f) == self.llt.Ptr(self.llt.PyObject)
+ assert lltype.typeOf(f) == lltype.Ptr(lltype.PyObject)
for pyo in args:
- assert self.llt.typeOf(pyo) == self.llt.Ptr(self.llt.PyObject)
+ assert lltype.typeOf(pyo) == lltype.Ptr(lltype.PyObject)
res = f._obj.value(*[pyo._obj.value for pyo in args])
- return self.llt.pyobjectptr(res)
+ return self.heap.pyobjectptr(res)
# __________________________________________________________
# operations on addresses
def op_raw_malloc(self, size):
- assert self.llt.typeOf(size) == self.llt.Signed
+ assert lltype.typeOf(size) == lltype.Signed
return lladdress.raw_malloc(size)
def op_raw_free(self, addr):
- assert self.llt.typeOf(addr) == llmemory.Address
+ assert lltype.typeOf(addr) == llmemory.Address
lladdress.raw_free(addr)
def op_raw_memcopy(self, fromaddr, toaddr, size):
- assert self.llt.typeOf(fromaddr) == llmemory.Address
- assert self.llt.typeOf(toaddr) == llmemory.Address
+ assert lltype.typeOf(fromaddr) == llmemory.Address
+ assert lltype.typeOf(toaddr) == llmemory.Address
lladdress.raw_memcopy(fromaddr, toaddr, size)
def op_raw_load(self, addr, typ, offset):
assert isinstance(addr, lladdress.address)
value = getattr(addr, str(typ).lower())[offset]
- assert self.llt.typeOf(value) == typ
+ assert lltype.typeOf(value) == typ
return value
def op_raw_store(self, addr, typ, offset, value):
assert isinstance(addr, lladdress.address)
- assert self.llt.typeOf(value) == typ
+ assert lltype.typeOf(value) == typ
getattr(addr, str(typ).lower())[offset] = value
def op_adr_add(self, addr, offset):
assert isinstance(addr, lladdress.address)
- assert self.llt.typeOf(offset) is self.llt.Signed
+ assert lltype.typeOf(offset) is lltype.Signed
return addr + offset
def op_adr_sub(self, addr, offset):
assert isinstance(addr, lladdress.address)
- assert self.llt.typeOf(offset) is self.llt.Signed
+ assert lltype.typeOf(offset) is lltype.Signed
return addr - offset
def op_adr_delta(self, addr1, addr2):
@@ -744,8 +745,8 @@
def op_oosetfield(self, inst, name, value):
assert isinstance(inst, ootype._instance)
assert isinstance(name, str)
- FIELDTYPE = self.llt.typeOf(inst)._field_type(name)
- if FIELDTYPE != self.llt.Void:
+ FIELDTYPE = lltype.typeOf(inst)._field_type(name)
+ if FIELDTYPE != lltype.Void:
setattr(inst, name, value)
def op_oogetfield(self, inst, name):
Modified: pypy/dist/pypy/rpython/lltypesystem/lltype.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/lltype.py (original)
+++ pypy/dist/pypy/rpython/lltypesystem/lltype.py Tue Feb 21 23:16:11 2006
@@ -567,34 +567,10 @@
return -u
def cast_pointer(PTRTYPE, ptr):
- if not isinstance(ptr, _ptr) or not isinstance(PTRTYPE, Ptr):
+ CURTYPE = typeOf(ptr)
+ if not isinstance(CURTYPE, Ptr) or not isinstance(PTRTYPE, Ptr):
raise TypeError, "can only cast pointers to other pointers"
- CURTYPE = ptr._TYPE
- down_or_up = castable(PTRTYPE, CURTYPE)
- if down_or_up == 0:
- return ptr
- if not ptr: # null pointer cast
- return PTRTYPE._defl()
- if down_or_up > 0:
- p = ptr
- while down_or_up:
- p = getattr(p, typeOf(p).TO._names[0])
- down_or_up -= 1
- return _ptr(PTRTYPE, p._obj)
- u = -down_or_up
- struc = ptr._obj
- while u:
- parent = struc._parentstructure()
- if parent is None:
- raise RuntimeError("widening to trash: %r" % ptr)
- PARENTTYPE = struc._parent_type
- if getattr(parent, PARENTTYPE._names[0]) is not struc:
- raise InvalidCast(CURTYPE, PTRTYPE) # xxx different exception perhaps?
- struc = parent
- u -= 1
- if PARENTTYPE != PTRTYPE.TO:
- raise TypeError("widening %r inside %r instead of %r" % (CURTYPE, PARENTTYPE, PTRTYPE.TO))
- return _ptr(PTRTYPE, struc)
+ return ptr._cast_to(PTRTYPE)
def _expose(val):
"""XXX A nice docstring here"""
@@ -787,6 +763,41 @@
__getstate__ = getstate_with_slots
__setstate__ = setstate_with_slots
+ def _cast_to(self, PTRTYPE):
+ CURTYPE = self._TYPE
+ down_or_up = castable(PTRTYPE, CURTYPE)
+ if down_or_up == 0:
+ return self
+ if not self: # null pointer cast
+ return PTRTYPE._defl()
+ if down_or_up > 0:
+ p = self
+ while down_or_up:
+ p = getattr(p, typeOf(p).TO._names[0])
+ down_or_up -= 1
+ return _ptr(PTRTYPE, p._obj)
+ u = -down_or_up
+ struc = self._obj
+ while u:
+ parent = struc._parentstructure()
+ if parent is None:
+ raise RuntimeError("widening to trash: %r" % self)
+ PARENTTYPE = struc._parent_type
+ if getattr(parent, PARENTTYPE._names[0]) is not struc:
+ raise InvalidCast(CURTYPE, PTRTYPE) # xxx different exception perhaps?
+ struc = parent
+ u -= 1
+ if PARENTTYPE != PTRTYPE.TO:
+ raise TypeError("widening %r inside %r instead of %r" % (CURTYPE, PARENTTYPE, PTRTYPE.TO))
+ return _ptr(PTRTYPE, struc)
+
+ def _cast_to_int(self):
+ obj = self._obj
+ while obj._parentstructure():
+ obj = obj._parentstructure()
+ return id(obj)
+
+
assert not '__dict__' in dir(_ptr)
class _parentable(object):
@@ -1043,11 +1054,7 @@
return _ptr(Ptr(PyObject), o)
def cast_ptr_to_int(ptr):
- obj = ptr._obj
- while obj._parentstructure():
- obj = obj._parentstructure()
- return id(obj)
-
+ return ptr._cast_to_int()
def attachRuntimeTypeInfo(GCSTRUCT, funcptr=None, destrptr=None):
if not isinstance(GCSTRUCT, GcStruct):
Modified: pypy/dist/pypy/rpython/memory/gclltype.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/gclltype.py (original)
+++ pypy/dist/pypy/rpython/memory/gclltype.py Tue Feb 21 23:16:11 2006
@@ -1,26 +1,13 @@
-from pypy.rpython.lltypesystem.lltype import \
- LowLevelType, ContainerType, Struct, GcStruct, \
- Array, GcArray, FuncType, OpaqueType, \
- RuntimeTypeInfo, PyObjectType, PyObject, \
- GC_CONTAINER, \
- Signed, Unsigned, Float, Char, Bool, Void, \
- UniChar, Ptr, typeOf, InvalidCast
from pypy.rpython.memory.convertlltype import FlowGraphConstantConverter
-from pypy.rpython.memory.lltypesimulation import cast_pointer, free
+from pypy.rpython.memory.lltypesimulation import free
from pypy.rpython.memory.lltypesimulation import simulatorptr as _ptr
from pypy.rpython.memory.lltypesimulation import malloc, functionptr, nullptr
-from pypy.rpython.memory.lltypesimulation import pyobjectptr, cast_ptr_to_int
+from pypy.rpython.memory.lltypesimulation import pyobjectptr
def notimplemented(*args, **kwargs):
raise NotImplemented
-# the following names might have to be imported from lltype as well
-# ForwardReference, GcForwardReference, castable, parentlink
-
-ForwardReference = GcForwardReference = castable = parentlink = notimplemented
-
-
# the following names from lltype will probably have to be implemented yet:
# opaqueptr, attachRuntimeTypeInfo, getRuntimeTypeInfo,
# runtime_type_info
Modified: pypy/dist/pypy/rpython/memory/gcwrapper.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/gcwrapper.py (original)
+++ pypy/dist/pypy/rpython/memory/gcwrapper.py Tue Feb 21 23:16:11 2006
@@ -195,7 +195,7 @@
return [typeid, size]
def get_funcptr_malloc(self):
- return self.llinterp.llt.functionptr(gc.gc_interface["malloc"], "malloc",
+ return self.llinterp.heap.functionptr(gc.gc_interface["malloc"], "malloc",
_callable=self.gc.malloc)
def adjust_result_malloc(self, address, TYPE, size=0):
@@ -224,7 +224,7 @@
def get_funcptr_write_barrier(self):
- return self.llinterp.llt.functionptr(gc.gc_interface["write_barrier"],
+ return self.llinterp.heap.functionptr(gc.gc_interface["write_barrier"],
"write_barrier",
_callable=self.gc.write_barrier)
@@ -315,7 +315,7 @@
return [self.gcptr, typeid, size]
def get_funcptr_malloc(self):
- return self.llinterp.llt.functionptr(gc.gc_interface["malloc"], "malloc",
+ return self.llinterp.heap.functionptr(gc.gc_interface["malloc"], "malloc",
_callable=self.gc.malloc,
graph=self.malloc_graph)
@@ -338,7 +338,7 @@
return self.gcptr, item._address, addr_to, obj._address
def get_funcptr_write_barrier(self):
- return self.llinterp.llt.functionptr(gc.gc_interface["write_barrier"],
+ return self.llinterp.heap.functionptr(gc.gc_interface["write_barrier"],
"write_barrier",
_callable=self.gc.write_barrier,
graph=self.write_barrier_graph)
Modified: pypy/dist/pypy/rpython/memory/lltypesimulation.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/lltypesimulation.py (original)
+++ pypy/dist/pypy/rpython/memory/lltypesimulation.py Tue Feb 21 23:16:11 2006
@@ -176,18 +176,15 @@
def __repr__(self):
return '<simulatorptr %s to %s>' % (self._TYPE.TO, self._address)
-
-def cast_pointer(PTRTYPE, ptr):
- if not isinstance(ptr, simulatorptr) or not isinstance(PTRTYPE, lltype.Ptr):
- raise TypeError, "can only cast pointers to other pointers"
- CURTYPE = ptr._TYPE
- down_or_up = lltype.castable(PTRTYPE, CURTYPE)
- if down_or_up == 0:
- return ptr
- # XXX the lltype.cast_pointer does a lot of checks here:
- # I can't think of a way to do that with simulatorptr.
- # I'm not sure whether this is the right way to go...
- return simulatorptr(PTRTYPE, ptr._address)
+ def _cast_to(self, PTRTYPE):
+ CURTYPE = self._TYPE
+ down_or_up = lltype.castable(PTRTYPE, CURTYPE)
+ if down_or_up == 0:
+ return self
+ return simulatorptr(PTRTYPE, self._address)
+
+ def _cast_to_int(self):
+ return self._address.intaddress
# for now use the simulators raw_malloc
def malloc(T, n=None, immortal=False, flavor='gc'):
@@ -232,5 +229,3 @@
addr = lladdress.get_address_of_object(lltype._pyobject(obj))
return simulatorptr(lltype.Ptr(lltype.PyObject), addr)
-def cast_ptr_to_int(ptr):
- return ptr._address.intaddress
Modified: pypy/dist/pypy/rpython/memory/test/test_llinterpsim.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/test/test_llinterpsim.py (original)
+++ pypy/dist/pypy/rpython/memory/test/test_llinterpsim.py Tue Feb 21 23:16:11 2006
@@ -40,7 +40,7 @@
t, typer, graph = gengraph(func, [annotation(x)
for x in values], viewbefore, policy)
- interp = LLInterpreter(typer, gclltype)
+ interp = LLInterpreter(typer, heap=gclltype)
_tcache[key] = (t, interp, graph)
# keep the cache small
_lastinterpreted.append(key)
Modified: pypy/dist/pypy/rpython/memory/test/test_lltypesimulation.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/test/test_lltypesimulation.py (original)
+++ pypy/dist/pypy/rpython/memory/test/test_lltypesimulation.py Tue Feb 21 23:16:11 2006
@@ -1,4 +1,5 @@
from pypy.rpython.memory.lltypesimulation import *
+from pypy.rpython.lltypesystem.lltype import cast_pointer, cast_ptr_to_int
py.log.setconsumer("lltypesim", None)
More information about the Pypy-commit
mailing list