def test_constfold(): layoutbuilder = TypeLayoutBuilder(FakeGC) tid1 = layoutbuilder.get_type_id(GC_A) tid2 = layoutbuilder.get_type_id(GC_S3) class MockGC: def set_query_functions(self, is_varsize, has_gcptr_in_varsize, is_gcarrayofgcptr, *rest): self.is_varsize = is_varsize self.has_gcptr_in_varsize = has_gcptr_in_varsize self.is_gcarrayofgcptr = is_gcarrayofgcptr gc = MockGC() layoutbuilder.initialize_gc_query_function(gc) # def f(): return (1 * gc.is_varsize(tid1) + 10 * gc.has_gcptr_in_varsize(tid1) + 100 * gc.is_gcarrayofgcptr(tid1) + 1000 * gc.is_varsize(tid2) + 10000 * gc.has_gcptr_in_varsize(tid2) + 100000 * gc.is_gcarrayofgcptr(tid2)) interp, graph = get_interpreter(f, [], backendopt=True) assert interp.eval_graph(graph, []) == 11001 assert graph.startblock.exits[0].args == [Constant(11001, lltype.Signed)]
def test_constfold(): layoutbuilder = TypeLayoutBuilder(FakeGC) tid1 = layoutbuilder.get_type_id(GC_A) tid2 = layoutbuilder.get_type_id(GC_S3) class MockGC: def set_query_functions(self, is_varsize, has_gcptr_in_varsize, is_gcarrayofgcptr, *rest): self.is_varsize = is_varsize self.has_gcptr_in_varsize = has_gcptr_in_varsize self.is_gcarrayofgcptr = is_gcarrayofgcptr gc = MockGC() layoutbuilder.initialize_gc_query_function(gc) # def f(): return (1 * gc.is_varsize(tid1) + 10 * gc.has_gcptr_in_varsize(tid1) + 100 * gc.is_gcarrayofgcptr(tid1) + 1000 * gc.is_varsize(tid2) + 10000 * gc.has_gcptr_in_varsize(tid2) + 100000 * gc.is_gcarrayofgcptr(tid2)) interp, graph = get_interpreter(f, [], backendopt=True) assert interp.eval_graph(graph, []) == 11001 assert graph.startblock.exits[0].args == [Constant(11001, lltype.Signed)]
class BaseDirectGCTest(object): GC_PARAMS = {} def setup_method(self, meth): from rpython.config.translationoption import get_combined_translation_config config = get_combined_translation_config(translating=True).translation self.stackroots = [] GC_PARAMS = self.GC_PARAMS.copy() if hasattr(meth, 'GC_PARAMS'): GC_PARAMS.update(meth.GC_PARAMS) GC_PARAMS['translated_to_c'] = False self.gc = self.GCClass(config, **GC_PARAMS) self.gc.DEBUG = True self.rootwalker = DirectRootWalker(self) self.gc.set_root_walker(self.rootwalker) self.layoutbuilder = TypeLayoutBuilder(self.GCClass) self.get_type_id = self.layoutbuilder.get_type_id self.layoutbuilder.initialize_gc_query_function(self.gc) self.gc.setup() def consider_constant(self, p): obj = p._obj TYPE = lltype.typeOf(obj) self.layoutbuilder.consider_constant(TYPE, obj, self.gc) def write(self, p, fieldname, newvalue): if self.gc.needs_write_barrier: addr_struct = llmemory.cast_ptr_to_adr(p) self.gc.write_barrier(addr_struct) setattr(p, fieldname, newvalue) def writearray(self, p, index, newvalue): if self.gc.needs_write_barrier: addr_struct = llmemory.cast_ptr_to_adr(p) if hasattr(self.gc, 'write_barrier_from_array'): self.gc.write_barrier_from_array(addr_struct, index) else: self.gc.write_barrier(addr_struct) p[index] = newvalue def malloc(self, TYPE, n=None): addr = self.gc.malloc(self.get_type_id(TYPE), n) debug_print(self.gc) obj_ptr = llmemory.cast_adr_to_ptr(addr, lltype.Ptr(TYPE)) if not self.gc.malloc_zero_filled: zero_gc_pointers_inside(obj_ptr, TYPE) return obj_ptr
class BaseDirectGCTest(object): GC_PARAMS = {} def setup_method(self, meth): from rpython.config.translationoption import get_combined_translation_config config = get_combined_translation_config(translating=True).translation self.stackroots = [] GC_PARAMS = self.GC_PARAMS.copy() if hasattr(meth, 'GC_PARAMS'): GC_PARAMS.update(meth.GC_PARAMS) GC_PARAMS['translated_to_c'] = False self.gc = self.GCClass(config, **GC_PARAMS) self.gc.DEBUG = True self.rootwalker = DirectRootWalker(self) self.gc.set_root_walker(self.rootwalker) self.layoutbuilder = TypeLayoutBuilder(self.GCClass) self.get_type_id = self.layoutbuilder.get_type_id self.layoutbuilder.initialize_gc_query_function(self.gc) self.gc.setup() def consider_constant(self, p): obj = p._obj TYPE = lltype.typeOf(obj) self.layoutbuilder.consider_constant(TYPE, obj, self.gc) def write(self, p, fieldname, newvalue): if self.gc.needs_write_barrier: addr_struct = llmemory.cast_ptr_to_adr(p) self.gc.write_barrier(addr_struct) setattr(p, fieldname, newvalue) def writearray(self, p, index, newvalue): if self.gc.needs_write_barrier: addr_struct = llmemory.cast_ptr_to_adr(p) if hasattr(self.gc, 'write_barrier_from_array'): self.gc.write_barrier_from_array(addr_struct, index) else: self.gc.write_barrier(addr_struct) p[index] = newvalue def malloc(self, TYPE, n=None): addr = self.gc.malloc(self.get_type_id(TYPE), n) debug_print(self.gc) obj_ptr = llmemory.cast_adr_to_ptr(addr, lltype.Ptr(TYPE)) if not self.gc.malloc_zero_filled: zero_gc_pointers_inside(obj_ptr, TYPE) return obj_ptr