[pypy-commit] pypy dynamic-specialized-tuple: added bool support, also cleaned up code slightly
alex_gaynor
noreply at buildbot.pypy.org
Wed Mar 14 18:56:56 CET 2012
Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: dynamic-specialized-tuple
Changeset: r53562:b0a1e3ae8470
Date: 2012-03-14 10:56 -0700
http://bitbucket.org/pypy/pypy/changeset/b0a1e3ae8470/
Log: added bool support, also cleaned up code slightly
diff --git a/pypy/objspace/std/test/test_tupleobject.py b/pypy/objspace/std/test/test_tupleobject.py
--- a/pypy/objspace/std/test/test_tupleobject.py
+++ b/pypy/objspace/std/test/test_tupleobject.py
@@ -140,6 +140,8 @@
def test_bools(self):
t = (True, False)
assert self.get_specialization(t) == "bb"
+ assert t[0] is True
+ assert t[1] is False
def test_strs(self):
t = ("a", "b", "c")
diff --git a/pypy/objspace/std/tupletype.py b/pypy/objspace/std/tupletype.py
--- a/pypy/objspace/std/tupletype.py
+++ b/pypy/objspace/std/tupletype.py
@@ -4,7 +4,8 @@
from pypy.interpreter.baseobjspace import W_Root
from pypy.objspace.std.register_all import register_all
from pypy.objspace.std.stdtypedef import StdTypeDef, SMM
-from pypy.rlib.rerased_raw import UntypedStorage, INT, INSTANCE
+from pypy.rlib.rerased_raw import UntypedStorage, INT, BOOL, INSTANCE
+from pypy.rlib.unroll import unrolling_iterable
MAXIMUM_SPECIALIZED_SIZE = 8
@@ -16,24 +17,52 @@
make_tuple(space, w_tuple, list_w)
return w_tuple
+def _check_int(space, w_obj):
+ return space.is_w(space.type(w_obj), space.w_int)
+def _store_int(space, storage, idx, w_obj):
+ storage.setint(idx, space.int_w(w_obj))
+def _get_int(space, storage, idx):
+ return space.wrap(storage.getint(idx))
+
+def _check_bool(space, w_obj):
+ return space.is_w(space.type(w_obj), space.w_bool)
+def _store_bool(space, storage, idx, w_obj):
+ storage.setbool(idx, space.is_true(w_obj))
+def _get_bool(space, storage, idx):
+ return space.wrap(storage.getbool(idx))
+
+def _check_instance(space, w_obj):
+ return True
+def _store_instance(space, storage, idx, w_obj):
+ storage.setinstance(idx, w_obj)
+def _get_instance(space, storage, idx):
+ return storage.getinstance(idx, W_Root)
+
+SPECIALIZED_TYPES = unrolling_iterable([
+ (INT, _check_int, _store_int, _get_int),
+ (BOOL, _check_bool, _store_bool, _get_bool),
+ (INSTANCE, _check_instance, _store_instance, _get_instance)
+])
+
def get_char_from_obj(space, w_obj):
- if space.is_w(space.type(w_obj), space.w_int):
- return INT
- else:
- return INSTANCE
+ for char, check, store, read in SPECIALIZED_TYPES:
+ if check(space, w_obj):
+ return char
+ assert False
-def store_obj(space, storage, idx, w_obj):
- if space.is_w(space.type(w_obj), space.w_int):
- storage.setint(idx, space.int_w(w_obj))
- else:
- storage.setinstance(idx, w_obj)
+def store_obj(space, storage, shape_char, idx, w_obj):
+ for char, check, store, read in SPECIALIZED_TYPES:
+ if shape_char == char:
+ store(space, storage, idx, w_obj)
+ return
+ assert False
def read_obj(space, storage, idx):
- char = storage.getshape()[idx]
- if char == INT:
- return space.wrap(storage.getint(idx))
- else:
- return storage.getinstance(idx, W_Root)
+ shape_char = storage.getshape()[idx]
+ for char, check, store, read in SPECIALIZED_TYPES:
+ if shape_char == char:
+ return read(space, storage, idx)
+ assert False
def make_tuple(space, w_tuple, list_w):
from pypy.objspace.std.tupleobject import W_TupleObject
@@ -45,7 +74,7 @@
shape = space.str_w(space.new_interned_str("".join(shape_chars)))
storage = UntypedStorage(shape)
for i, w_item in enumerate(list_w):
- store_obj(space, storage, i, w_item)
+ store_obj(space, storage, shape[i], i, w_item)
W_TupleObject.__init__(w_tuple, storage)
return w_tuple
diff --git a/pypy/rlib/rerased_raw.py b/pypy/rlib/rerased_raw.py
--- a/pypy/rlib/rerased_raw.py
+++ b/pypy/rlib/rerased_raw.py
@@ -17,6 +17,7 @@
INT = "i"
+BOOL = "b"
FLOAT = "f"
INSTANCE = "o"
@@ -42,6 +43,17 @@
assert isinstance(v, int)
self.storage[idx] = v
+ def getbool(self, idx):
+ assert self.shape[idx] == BOOL
+ v = self.storage[idx]
+ assert isinstance(v, bool)
+ return v
+
+ def setbool(self, idx, v):
+ assert self.shape[idx] == BOOL
+ assert isinstance(v, bool)
+ self.storage[idx] = v
+
def getfloat(self, idx):
assert self.shape[idx] == FLOAT
v = self.storage[idx]
@@ -100,6 +112,14 @@
self._check_idx(s_idx)
assert annmodel.SomeInteger().contains(s_v)
+ def method_getbool(self, s_idx):
+ self._check_idx(s_idx)
+ return annmodel.SomeBool()
+
+ def method_setbool(self, s_idx, s_v):
+ self._check_idx(s_idx)
+ assert annmodel.SomeBool().contains(s_v)
+
def method_getfloat(self, s_idx):
self._check_idx(s_idx)
return annmodel.SomeFloat()
@@ -238,6 +258,15 @@
v_addr = hop.genop("force_cast", [v_value], resulttype=llmemory.Address)
self._write_index(hop, v_addr)
+ def rtype_method_getbool(self, hop):
+ v_addr = self._read_index(hop)
+ return hop.genop("force_cast", [v_addr], resulttype=lltype.Bool)
+
+ def rtype_method_setbool(self, hop):
+ v_value = hop.inputarg(lltype.Bool, arg=2)
+ v_addr = hop.genop("force_cast", [v_value], resulttype=llmemory.Address)
+ self._write_index(hop, v_addr)
+
def rtype_method_getfloat(self, hop):
v_value = self._read_index(hop)
return hop.genop("cast_adr_to_float", [v_value], resulttype=lltype.Float)
diff --git a/pypy/rlib/test/test_rerased_raw.py b/pypy/rlib/test/test_rerased_raw.py
--- a/pypy/rlib/test/test_rerased_raw.py
+++ b/pypy/rlib/test/test_rerased_raw.py
@@ -20,6 +20,12 @@
assert storage.getfloat(0) == 5.5
+def test_direct_bool():
+ storage = rerased_raw.UntypedStorage("bi")
+ storage.setbool(0, True)
+
+ assert storage.getbool(0) is True
+
def test_direct_instance():
class A(object):
def __init__(self, value):
@@ -49,6 +55,15 @@
res = self.interpret(f, [4])
assert res == 4
+ def test_bool(self):
+ def f(x):
+ storage = rerased_raw.UntypedStorage("b")
+ storage.setbool(0, x)
+ return storage.getbool(0)
+
+ res = self.interpret(f, [True])
+ assert res == True
+
def test_instance(self):
class A(object):
def __init__(self, v):
More information about the pypy-commit
mailing list