Author: Maciej Fijalkowski <fij...@gmail.com> Branch: ffi-backend Changeset: r55795:3f14b7b04094 Date: 2012-06-24 15:00 +0200 http://bitbucket.org/pypy/pypy/changeset/3f14b7b04094/
Log: (fijal, arigo) start on raw_storage diff --git a/pypy/rlib/rawstorage.py b/pypy/rlib/rawstorage.py new file mode 100644 --- /dev/null +++ b/pypy/rlib/rawstorage.py @@ -0,0 +1,37 @@ + +from pypy.rpython.extregistry import ExtRegistryEntry +from pypy.rpython.lltypesystem import lltype, rffi, llmemory +from pypy.annotation import model as annmodel +from pypy.rlib.rgc import lltype_is_gc + +RAW_STORAGE = rffi.CCHARP.TO +RAW_STORAGE_PTR = rffi.CCHARP + +def alloc_raw_storage(size): + return lltype.malloc(RAW_STORAGE, size, flavor='raw') + +def raw_storage_setitem(storage, index, item): + # NOT_RPYTHON + TP = rffi.CArrayPtr(lltype.typeOf(item)) + rffi.cast(TP, rffi.ptradd(storage, index))[0] = item + +def free_raw_storage(storage): + lltype.free(storage, flavor='raw') + +class RawStorageSetitemEntry(ExtRegistryEntry): + _about_ = raw_storage_setitem + + def compute_result_annotation(self, s_storage, s_index, s_item): + assert annmodel.SomeInteger().contains(s_index) + + def specialize_call(self, hop): + assert not lltype_is_gc(hop.args_r[2].lowleveltype) + assert hop.args_r[0].lowleveltype == RAW_STORAGE_PTR + v_storage, v_index, v_item = hop.inputargs(hop.args_r[0], + lltype.Signed, + hop.args_r[2]) + c_typ = hop.inputconst(lltype.Void, hop.args_r[2].lowleveltype) + hop.exception_cannot_occur() + v_addr = hop.genop('cast_ptr_to_adr', [v_storage], + resulttype=llmemory.Address) + return hop.genop('raw_store', [v_addr, c_typ, v_index, v_item]) diff --git a/pypy/rlib/test/test_rawstorage.py b/pypy/rlib/test/test_rawstorage.py new file mode 100644 --- /dev/null +++ b/pypy/rlib/test/test_rawstorage.py @@ -0,0 +1,25 @@ + +from pypy.rpython.lltypesystem import rffi, lltype +from pypy.rlib.rawstorage import alloc_raw_storage, free_raw_storage,\ + raw_storage_setitem +from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin + +def raw_storage_getitem(storage, index, TP): + return rffi.cast(rffi.CArrayPtr(TP), rffi.ptradd(storage, index))[0] + +def test_untranslated_storage(): + r = alloc_raw_storage(15) + raw_storage_setitem(r, 3, 1<<30) + res = raw_storage_getitem(r, 3, lltype.Signed) + free_raw_storage(r) + assert res == 1<<30 + +class TestRawStorage(BaseRtypingTest, LLRtypeMixin): + def test_storage_int(self): + def f(i): + r = alloc_raw_storage(24) + raw_storage_setitem(r, 3, i) + return r + ll_r = self.interpret(f, [1<<30], malloc_check=False) + assert raw_storage_getitem(ll_r, 3, lltype.Signed) == 1 << 30 + free_raw_storage(ll_r) _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit