1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi
from rpython.rtyper.test.test_llop import (BaseLLOpTest, str_gc_load,
newlist_and_gc_store)
from rpython.translator.c.test.test_genc import compile
class TestLLOp(BaseLLOpTest):
cache_load = {}
cache_store = {}
def gc_load_from_string(self, TYPE, buf, offset):
if TYPE not in self.cache_load:
assert isinstance(TYPE, lltype.Primitive)
if TYPE in (lltype.Float, lltype.SingleFloat):
TARGET_TYPE = lltype.Float
else:
TARGET_TYPE = lltype.Signed
def llf(buf, offset):
x = str_gc_load(TYPE, buf, offset)
return lltype.cast_primitive(TARGET_TYPE, x)
fn = compile(llf, [str, int])
self.cache_load[TYPE] = fn
#
fn = self.cache_load[TYPE]
x = fn(buf, offset)
return lltype.cast_primitive(TYPE, x)
def newlist_and_gc_store(self, TYPE, value, expected):
if TYPE not in self.cache_store:
assert isinstance(TYPE, lltype.Primitive)
if TYPE in (lltype.Float, lltype.SingleFloat):
argtype = float
else:
argtype = int
def llf(value):
value = lltype.cast_primitive(TYPE, value)
lst = newlist_and_gc_store(TYPE, value)
return ''.join(lst)
fn = compile(llf, [argtype])
self.cache_store[TYPE] = fn
#
fn = self.cache_store[TYPE]
got = fn(value)
assert got == expected
|