[pypy-svn] r11442 - in pypy/dist/pypy/translator/llvm: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Tue Apr 26 04:08:14 CEST 2005


Author: cfbolz
Date: Tue Apr 26 04:08:13 2005
New Revision: 11442

Added:
   pypy/dist/pypy/translator/llvm/lazyattribute.py
   pypy/dist/pypy/translator/llvm/test/test_lazyattribute.py
Modified:
   pypy/dist/pypy/translator/llvm/classrepr.py
   pypy/dist/pypy/translator/llvm/funcrepr.py
   pypy/dist/pypy/translator/llvm/genllvm.py
   pypy/dist/pypy/translator/llvm/list.c
   pypy/dist/pypy/translator/llvm/list_template.ll
   pypy/dist/pypy/translator/llvm/memorylayout.py
   pypy/dist/pypy/translator/llvm/pbcrepr.py
   pypy/dist/pypy/translator/llvm/representation.py
   pypy/dist/pypy/translator/llvm/seqrepr.py
   pypy/dist/pypy/translator/llvm/test/llvmsnippet.py
   pypy/dist/pypy/translator/llvm/test/test_genllvm.py
   pypy/dist/pypy/translator/llvm/typerepr.py
Log:
* Refactored most XXXRepr classes to make the instances more lazy via a (quite
  harmless) metaclass. Every class inheriting from LLVMRepr can define a
  classattribute 'lazy_attributes', which is a list of strings. For every
  string a property is bound to the corresponding attribute of the class. The
  get function of the property checks whether the setup method of the class
  has been called allready and does so if not.

* Various other fixes for small problems that were discovered while trying to
  get targetpypy1 to work. When I ran into my first SomeDict, I gave up.



Modified: pypy/dist/pypy/translator/llvm/classrepr.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/classrepr.py	(original)
+++ pypy/dist/pypy/translator/llvm/classrepr.py	Tue Apr 26 04:08:13 2005
@@ -51,14 +51,12 @@
             print self.name
         assert ".Exception.object" not in self.objectname
         self.dependencies = sets.Set()
-        self.setup_done = False
+
+    lazy_attributes = ['l_base', 'memlayout', 'definition', 'methods']
 
     def setup(self):
-        if self.setup_done:
-            return
-        self.setup_done = True
         if debug:
-            print "ClassRepr.setup()", id(self), hex(id(self)), self.setup_done
+            print "ClassRepr.setup()", id(self), hex(id(self))
             print len(ClassRepr.l_classes)
         gen = self.gen
         if self.classdef.basedef is not None: #get attributes from base classes
@@ -140,9 +138,10 @@
         lblock.malloc(l_target, self)
         self.memlayout.set(l_target, "__class__", self, lblock, l_func)
         init = None
-        for cls in self.classdef.getmro():
-            if "__init__" in cls.attrs:
-                init = cls.attrs["__init__"].getvalue()
+        for clsd in self.classdef.getmro():
+            if ("__init__" in clsd.cls.__dict__ and
+                clsd.cls.__module__ != "exceptions"):
+                init = clsd.cls.__dict__["__init__"]
                 break
         if init is not None:
             l_init = self.gen.get_repr(init)
@@ -244,6 +243,8 @@
         self.definition = s % (self.objectname, abs(id(exception)))
         self.dependencies = sets.Set()
 
+    lazy_attributes = ['l_base', 'memlayout']
+
     def setup(self):
         if len(self.exception.__bases__) != 0:
             self.l_base = self.gen.get_repr(self.exception.__bases__[0])
@@ -324,6 +325,8 @@
         self.dependencies = sets.Set([self.type])
         self.name = gen.get_global_tmp(obj.value.__class__.__name__ + ".inst")
 
+    lazy_attributes = ['l_attrib_values', 'definition']
+
     def setup(self):
         self.l_attrib_values = [self.type]
         for attr in self.type.memlayout.attrs[1:]:

Modified: pypy/dist/pypy/translator/llvm/funcrepr.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/funcrepr.py	(original)
+++ pypy/dist/pypy/translator/llvm/funcrepr.py	Tue Apr 26 04:08:13 2005
@@ -1,5 +1,5 @@
 import autopath
-import sets, StringIO
+import sets, inspect
 
 from types import FunctionType, MethodType
 
@@ -14,7 +14,7 @@
 from pypy.translator.llvm.representation import debug, LLVMRepr, CompileError
 from pypy.translator.llvm.typerepr import TypeRepr, PointerTypeRepr
 
-debug = False
+debug = True
 
 INTRINSIC_OPS = ["lt", "le", "eq", "ne", "gt", "ge", "is_", "is_true", "len",
                  "neg", "pos", "invert", "add", "sub", "mul", "truediv",
@@ -33,6 +33,13 @@
                   annmodel.SomeFloat: "double"}
 
 
+def is_annotated(func, gen):
+    if func not in gen.translator.flowgraphs:
+        return False
+    flg = gen.translator.getflowgraph(func)
+    startblock = flg.startblock
+    return startblock in gen.annotator.annotated
+
 class BuiltinFunctionRepr(LLVMRepr):
     def get(obj, gen):
         if (isinstance(obj, Constant) and
@@ -81,7 +88,7 @@
                 return FunctionRepr.l_functions[(obj, gen)]
             if name is None:
                 name = obj.__name__
-            l_func = FunctionRepr(gen.get_global_tmp(name), obj, gen)
+            l_func = FunctionRepr(name, obj, gen)
             FunctionRepr.l_functions[(obj, gen)] = l_func
             return l_func
         return None
@@ -93,28 +100,28 @@
         self.gen = gen
         self.func = function
         self.translator = gen.translator
-        self.name = name
+        self.name = gen.get_global_tmp(name)
         self.graph = self.translator.getflowgraph(self.func)
         self.annotator = gen.translator.annotator
         self.blocknum = {}
         self.allblocks = []
         self.pyrex_source = ""
         self.dependencies = sets.Set()
+        self.l_retvalue = self.gen.get_repr(
+            self.graph.returnblock.inputargs[0])
+        self.dependencies.add(self.l_retvalue)
+        self.l_args = [self.gen.get_repr(ar)
+                       for ar in self.graph.startblock.inputargs]
+        self.dependencies.update(self.l_args)
+        self.l_default_args = None
         remove_double_links(self.translator, self.graph)
         self.get_bbs()
-        self.se = False
-        self.lblocks = []
+
+    lazy_attributes = ['llvm_func', 'lblocks']
 
     def setup(self):
-        if self.se:
-            return
         self.se = True
-        self.l_args = [self.gen.get_repr(ar)
-                       for ar in self.graph.startblock.inputargs]
-        self.dependencies.update(self.l_args)
-        self.retvalue = self.gen.get_repr(self.graph.returnblock.inputargs[0])
-        self.dependencies.add(self.retvalue)
-        self.l_default_args = None
+        self.lblocks = []
         self.build_bbs()
 
     def get_returntype():
@@ -148,11 +155,11 @@
         self.lblocks.append(lblock)
 
     def llvmfuncdef(self):
-        s = "internal %s %s(" % (self.retvalue.llvmtype(), self.name)
+        s = "internal %s %s(" % (self.l_retvalue.llvmtype(), self.name)
         return s + ", ".join([a.typed_name() for a in self.l_args]) + ")"
 
     def rettype(self):
-        return self.retvalue.llvmtype()
+        return self.l_retvalue.llvmtype()
 
     def get_functions(self):
         return str(self.llvm_func)
@@ -164,7 +171,12 @@
     def op_simple_call(self, l_target, args, lblock, l_func):
         l_args = [self.gen.get_repr(arg) for arg in args]
         if len(l_args) - 1 < len(self.l_args):
-            assert self.func.func_defaults is not None
+            if self.func.func_defaults is None:
+                for l_a in l_args:
+                    print l_a, l_a.llvmname(), 
+                for l_a in self.l_args:
+                    print l_a, l_a.llvmname(),
+                assert self.func.func_defaults is not None
             if self.l_default_args is None:
                 self.l_default_args = [self.gen.get_repr(Constant(de))
                                        for de in self.func.func_defaults]
@@ -254,7 +266,6 @@
             l_func.dependencies.add(l_switch)
             self.lblock.cond_branch(l_switch, "%" + l_link2.toblock,
                                     "%" + l_link.toblock)
-        #1 / 0
 
 
 class ReturnBlockRepr(BlockRepr):
@@ -424,6 +435,8 @@
         self.dependencies = sets.Set()
         self.branch_added = False
 
+    lazy_attributes = ['l_function', 'llvm_func', 'init_block', 'exceptblock']
+
     def setup(self):
         self.l_function = self.gen.get_repr(self.function)
         self.dependencies.add(self.l_function)
@@ -441,7 +454,7 @@
         self.llvm_func.basic_block(self.init_block)
         #create the block that calls the "real" function
         real_entry = llvmbc.TryBasicBlock("real_entry", "retblock", "exc")
-        l_ret = self.gen.get_local_tmp(self.l_function.retvalue.type,
+        l_ret = self.gen.get_local_tmp(self.l_function.l_retvalue.type,
                                        self)
         real_entry.last_op = True
         self.l_function.op_simple_call(
@@ -452,7 +465,7 @@
         self.exceptblock = llvmbc.BasicBlock("exc")
         ins = """store int 1, int* %%pypy__uncaught_exception
 \t%%dummy_ret = cast int 0 to %s
-\tret %s %%dummy_ret""" % tuple([self.l_function.retvalue.llvmtype()] * 2)
+\tret %s %%dummy_ret""" % tuple([self.l_function.l_retvalue.llvmtype()] * 2)
         self.exceptblock.instruction(ins)
         self.exceptblock.closed = True
         self.llvm_func.basic_block(self.exceptblock)
@@ -472,7 +485,7 @@
         return fd
 
     def llvmfuncdef(self):
-        s = "%s %s(" % (self.l_function.retvalue.llvmtype(), self.name)
+        s = "%s %s(" % (self.l_function.l_retvalue.llvmtype(), self.name)
         s += ", ".join([a.typed_name() for a in self.l_function.l_args]) + ")"
         return s
 
@@ -501,7 +514,7 @@
         return self.pyrex_source
 
     def rettype(self):
-        return self.l_function.retvalue.llvmtype()
+        return self.l_function.l_retvalue.llvmtype()
 
     def get_functions(self):
         if not self.branch_added:
@@ -525,9 +538,9 @@
     # to the appropriate class
     # Should be replaced by function pointers
     def get(obj, gen):
-        if isinstance(obj, annmodel.SomePBC) and \
-                 len(obj.prebuiltinstances) > 1 and \
-                 isinstance(obj.prebuiltinstances.keys()[0], FunctionType):
+        if (isinstance(obj, annmodel.SomePBC) and
+            len(obj.prebuiltinstances) > 1 and
+            isinstance(obj.prebuiltinstances.keys()[0], FunctionType)):
             return VirtualMethodRepr(obj.prebuiltinstances, gen)
         return None
     get = staticmethod(get)
@@ -543,6 +556,9 @@
         self.attribute = self.funcs[0].__name__
         self.dependencies = sets.Set()
 
+    lazy_attributes = ['l_funcs', 'l_commonbase', 'l_classes', 'l_args',
+                       'type_numbers', 'llvm_func']
+
     def setup(self):
         self.l_commonbase = self.gen.get_repr(self.commonbase)
         self.l_classes = [self.l_commonbase] + \
@@ -552,19 +568,20 @@
         #find appropriate method for every class
         for l_cls in self.l_classes:
             for classdef in l_cls.classdef.getmro():
-                if classdef.cls.__dict__.has_key(self.attribute):
-                    self.l_funcs.append(self.gen.get_repr(
-                        classdef.cls.__dict__[self.attribute]))
-                    break
+                if (classdef.cls.__dict__.has_key(self.attribute)):
+                    func = classdef.cls.__dict__[self.attribute]
+                    if is_annotated(func, self.gen):
+                        self.l_funcs.append(self.gen.get_repr(func))
+                        break
             else:
                 raise CompileError, "Couldn't find method %s for %s" % \
                       (self.attribute, l_cls.classdef.cls)
         self.dependencies.update(self.l_funcs)
-        self.retvalue = self.l_funcs[0].retvalue
+        self.l_retvalue = self.l_funcs[0].l_retvalue
         self.type_numbers = [id(l_c) for l_c in self.l_classes]
         self.l_args = [self.gen.get_repr(ar)
                        for ar in self.l_funcs[0].graph.startblock.inputargs]
-        l_retvalue = self.retvalue
+        l_retvalue = self.l_retvalue
         self.dependencies.update(self.l_args)
         #create function
         #XXX pretty messy
@@ -628,12 +645,12 @@
         return str(self.llvm_func)
 
     def llvmfuncdef(self):
-        s = "internal %s %s(" % (self.l_funcs[0].retvalue.llvmtype(),
+        s = "internal %s %s(" % (self.l_funcs[0].l_retvalue.llvmtype(),
                                  self.name)
         return s + ", ".join([a.typed_name() for a in self.l_args]) + ")"
 
     def rettype(self):
-        return self.retvalue.llvmtype()
+        return self.l_retvalue.llvmtype()
 
 class BoundMethodRepr(LLVMRepr):
     def get(obj, gen):

Modified: pypy/dist/pypy/translator/llvm/genllvm.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/genllvm.py	(original)
+++ pypy/dist/pypy/translator/llvm/genllvm.py	Tue Apr 26 04:08:13 2005
@@ -5,7 +5,7 @@
 import autopath
 import sets, StringIO
 
-from pypy.objspace.flow.model import Constant
+from pypy.objspace.flow.model import Constant, last_exception, last_exc_value
 from pypy.annotation import model as annmodel
 from pypy.translator import transform
 from pypy.translator.translator import Translator
@@ -14,7 +14,6 @@
 from pypy.translator.test import snippet as test
 from pypy.translator.llvm.test import llvmsnippet as test2
 
-
 from pypy.translator.llvm import representation, funcrepr, typerepr, seqrepr
 from pypy.translator.llvm import classrepr, pbcrepr
 
@@ -22,7 +21,7 @@
 from pypy.translator.llvm.representation import CompileError
 from pypy.translator.llvm.funcrepr import EntryFunctionRepr
 
-debug = True
+debug = False
 
 
 def llvmcompile(transl, optimize=False):
@@ -64,6 +63,7 @@
         self.llvm_reprs = {}
         self.depth = 0
         self.entryname = self.translator.functions[0].__name__
+        self.lazy_objects = sets.Set()
         self.l_entrypoint = EntryFunctionRepr("%__entry__" + self.entryname,
                                               self.translator.functions[0],
                                               self)
@@ -89,6 +89,7 @@
 
     def get_global_tmp(self, used_by=None):
         used_by = (used_by or "unknown")
+        assert "%" not in used_by
         if used_by in self.global_counts:
             self.global_counts[used_by] += 1
             return "%%glb.%s.%i" % (used_by, self.global_counts[used_by])
@@ -103,7 +104,6 @@
 
     def get_repr(self, obj):
         self.depth += 1
-        flag = False
         if debug:
             print "  " * self.depth,
             print "looking for object", obj, type(obj).__name__,
@@ -118,31 +118,27 @@
             return self.llvm_reprs[get_key(obj)]
         for cl in self.repr_classes:
             try:
-                obj.__class__
+                g = cl.get(obj, self)
             except AttributeError:
-                obj.__class__ = None
-                flag = True
-            g = cl.get(obj, self)
+                continue
             if g is not None:
                 self.llvm_reprs[get_key(obj)] = g
                 self.local_counts[g] = 0
-                if debug:
-                    print "  " * self.depth,
-                    print "calling setup of %s, repr of %s" % (g, obj)
-                g.setup()
+##                 if not hasattr(g.__class__, "lazy_attributes"):
+##                     if debug:
+##                         print "  " * self.depth,
+##                         print "calling setup of %s, repr of %s" % (g, obj)
+##                     g.setup()
                 self.depth -= 1
-                if flag:
-                    del obj.__class__
                 return g
-        if flag:
-            del obj.__class__
         raise CompileError, "Can't get repr of %s, %s" % (obj, obj.__class__)
 
     def write(self, f):
-        init_block = self.l_entrypoint.init_block
+        self.unlazyify()
         seen_reprs = sets.Set()
         remove_loops(self.l_entrypoint, seen_reprs)
         seen_reprs = sets.Set()
+        init_block = self.l_entrypoint.init_block
         for l_repr in traverse_dependencies(self.l_entrypoint, seen_reprs):
             l_repr.collect_init_code(init_block, self.l_entrypoint)
         include_files = ["operations.ll", "class.ll"]
@@ -172,6 +168,16 @@
             if s != "":
                 f.write(s + "\n")
 
+    def unlazyify(self):
+        if debug:
+            print 
+            print "$$$$$$$$$$$$$$ unlazyify"
+        while len(self.lazy_objects):
+            obj = self.lazy_objects.pop()
+            if debug:
+                print obj
+            obj.setup()
+
     def __str__(self):
         f = StringIO.StringIO()
         self.write(f)

Added: pypy/dist/pypy/translator/llvm/lazyattribute.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/llvm/lazyattribute.py	Tue Apr 26 04:08:13 2005
@@ -0,0 +1,46 @@
+import autopath
+
+import sets
+
+def create_property(cls, la, name):
+    def get(self):
+        self.setup()
+        if la not in self.__dict__:
+            raise AttributeError, ("'%s' object has no attribute '%s'" %
+                                   (name, la))
+        return self.__dict__[la]
+
+    def set(self, value):
+        self.__dict__[la] = value
+
+    def del_(self):
+        if la not in self.__dict__:
+            raise AttributeError, ("'%s' object has no attribute '%s'" %
+                                   (name, la))
+        del self.__dict__[la]
+    setattr(cls, la, property(get, set, del_))
+
+class MetaLazyRepr(type):
+    def __init__(cls, name, bases, dct):
+        if "lazy_attributes" in dct:
+            csetup = cls.setup
+            def setup(self):
+                if self.__setup_called__:
+                    return
+                if getattr(cls.__module__, "lazy_debug", None):
+                    print "calling setup of class", name
+                self.__setup_called__ = True
+                self.gen.lazy_objects.discard(self)
+                ret = csetup(self)
+                return ret
+            cls.setup = setup
+            c__init__ = cls.__init__
+            def __init__(self, *args, **kwds):
+                ret = c__init__(self, *args, **kwds)
+                self.gen.lazy_objects.add(self)
+                self.__setup_called__ = False
+            cls.__init__ = __init__
+            for la in dct["lazy_attributes"]:
+                create_property(cls, la, name)
+        super(MetaLazyRepr, cls).__init__(name, bases, dct)
+

Modified: pypy/dist/pypy/translator/llvm/list.c
==============================================================================
--- pypy/dist/pypy/translator/llvm/list.c	(original)
+++ pypy/dist/pypy/translator/llvm/list.c	Tue Apr 26 04:08:13 2005
@@ -84,7 +84,7 @@
     nlist->data[0] = v1;
     nlist->data[1] = v2;
     nlist->data[2] = v3;
-    nlist->data[3] = v3;
+    nlist->data[3] = v4;
     return nlist;
 }
 

Modified: pypy/dist/pypy/translator/llvm/list_template.ll
==============================================================================
--- pypy/dist/pypy/translator/llvm/list_template.ll	(original)
+++ pypy/dist/pypy/translator/llvm/list_template.ll	Tue Apr 26 04:08:13 2005
@@ -113,7 +113,7 @@
 	%tmp.21 = getelementptr [4 x %(item)s]* %tmp.6, int 0, int 2
 	store %(item)s %v3, %(item)s* %tmp.21
 	%tmp.26 = getelementptr [4 x %(item)s]* %tmp.6, int 0, int 3
-	store %(item)s %v3, %(item)s* %tmp.26
+	store %(item)s %v4, %(item)s* %tmp.26
 	ret %std.list.%(name)s* %tmp.0
 }
 

Modified: pypy/dist/pypy/translator/llvm/memorylayout.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/memorylayout.py	(original)
+++ pypy/dist/pypy/translator/llvm/memorylayout.py	Tue Apr 26 04:08:13 2005
@@ -6,7 +6,7 @@
 from pypy.translator.llvm.representation import debug, LLVMRepr
 from pypy.translator.llvm.typerepr import TypeRepr, PointerTypeRepr
 
-debug = True
+debug = False
 
 class MemoryLayout(object):
     def __init__(self, attrs, l_types, gen):

Modified: pypy/dist/pypy/translator/llvm/pbcrepr.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/pbcrepr.py	(original)
+++ pypy/dist/pypy/translator/llvm/pbcrepr.py	Tue Apr 26 04:08:13 2005
@@ -38,6 +38,8 @@
             "pbc.%s" % obj.prebuiltinstances.keys()[0].__class__.__name__)
         self.objectname = self.name + ".object"
 
+    lazy_attributes = ['methods', 'memlayout', 'definition']
+
     def setup(self):
         bk = self.gen.annotator.bookkeeper
         access_sets = bk.pbc_maximal_access_sets
@@ -67,20 +69,18 @@
                s_value.knowntype in (FunctionType, MethodType):
                 if debug:
                     print "--> method"
-                func = objects[0].__class__.__dict__[attr]
-                self.methods[attr] = func
+                if attr in objects[0].__class__.__dict__:
+                    func = objects[0].__class__.__dict__[attr]
+                    self.methods[attr] = func
             else:
                 if debug:
                     print "--> value"
                 attribs.append(attr)
                 l_types.append(self.gen.get_repr(s_value))
         self.memlayout = MemoryLayout(attribs, l_types, self.gen)
-
-    def get_globals(self):
         self.definition = "%s = %s" % (self.name, self.memlayout.definition())
         s = "\n%s = internal global %%std.class {%%std.class* null, uint %i}"
-        s = s % (self.objectname, abs(id(self)))
-        return self.definition + s
+        self.definition += s % (self.objectname, abs(id(self)))
 
     def llvmtype(self):
         return "%std.class*"
@@ -100,7 +100,8 @@
                                l_func)
             return
         elif args[1].value in self.methods:
-            print l_target, l_target.llvmname()
+            if debug:
+                print l_target, l_target.llvmname()
             if not isinstance(l_target.type, BoundMethodRepr):
                 l_args0 = self.gen.get_repr(args[0])
                 l_func.dependencies.add(l_args0)
@@ -132,12 +133,13 @@
             print "PBCRepr: ", obj
         self.obj = obj
         self.gen = gen
-        self.dependencies = sets.Set()
+        self.type = self.gen.get_repr(self.gen.annotator.binding(self.obj))
+        self.dependencies = sets.Set([self.type])
         self.name = gen.get_global_tmp(obj.value.__class__.__name__ + ".inst")
 
+    lazy_attributes = ['l_attrib_values', 'definition']
+
     def setup(self):
-        self.type = self.gen.get_repr(self.gen.annotator.binding(self.obj))
-        self.dependencies.add(self.type)
         self.l_attrib_values = [self.type]
         for attr in self.type.memlayout.attrs[1:]:
             s_a = self.gen.get_repr(Constant(getattr(self.obj.value, attr)))
@@ -156,4 +158,3 @@
         else:
             raise AttributeError, ("PBCRepr instance has no attribute %s"
                                    % repr(name))
-

Modified: pypy/dist/pypy/translator/llvm/representation.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/representation.py	(original)
+++ pypy/dist/pypy/translator/llvm/representation.py	Tue Apr 26 04:08:13 2005
@@ -4,7 +4,7 @@
 from pypy.objspace.flow.model import Variable, Constant
 from pypy.objspace.flow.model import last_exception, last_exc_value
 from pypy.annotation import model as annmodel
-
+from pypy.translator.llvm.lazyattribute import MetaLazyRepr
 LLVM_SIMPLE_TYPES = {annmodel.SomeChar: "sbyte",
                      annmodel.SomeBool: "bool",
                      annmodel.SomeFloat: "double"}
@@ -17,6 +17,8 @@
 
 
 class LLVMRepr(object):
+    __metaclass__ = MetaLazyRepr
+
     def get(obj, gen):
         return None
     get = staticmethod(get)
@@ -57,16 +59,19 @@
 bool, char (string of length 1), last_exception, last_exc_value"""
 
     def get(obj, gen):
+        if obj is last_exception or (isinstance(obj, Constant) and
+                                     obj.value is last_exception):
+            return SimpleRepr("%std.class**",
+                              "%std.last_exception.type", gen)
+        if obj is last_exc_value or (isinstance(obj, Constant) and
+                                     obj.value is last_exc_value):
+            return SimpleRepr("%std.exception**",
+                              "%std.last_exception.value", gen)
         if isinstance(obj, Constant):
             type_ = gen.annotator.binding(obj)
             if type_.__class__ in LLVM_SIMPLE_TYPES:
                 llvmtype = LLVM_SIMPLE_TYPES[type_.__class__]
                 return SimpleRepr(llvmtype, repr(obj.value), gen)
-        if obj == last_exception:
-            return SimpleRepr("%std.class**", "%std.last_exception.type", gen)
-        if obj == last_exc_value:
-            return SimpleRepr("%std.exception**", "%std.last_exception.value",
-                              gen)
         return None
     get = staticmethod(get)
     
@@ -93,7 +98,10 @@
             return IntRepr(type_, obj, gen)
         if not isinstance(obj, Constant):
             return None
-        type_ = gen.annotator.binding(obj)
+        try:
+            type_ = gen.annotator.binding(obj)
+        except AssertionError:
+            return None
         if type_.__class__ == annmodel.SomeInteger:
             return IntRepr(type_, obj.value, gen)
     get = staticmethod(get)

Modified: pypy/dist/pypy/translator/llvm/seqrepr.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/seqrepr.py	(original)
+++ pypy/dist/pypy/translator/llvm/seqrepr.py	Tue Apr 26 04:08:13 2005
@@ -34,6 +34,8 @@
                            (self.type.typename()[:-1], len(self.list),
                             self.type.l_itemtype.typename())
 
+    lazy_attributes = ['l_items']
+
     def setup(self):
         self.l_items = [self.gen.get_repr(item) for item in self.list]
         self.dependencies.update(self.l_items)
@@ -153,11 +155,13 @@
         self.tuple = obj.value
         self.gen = gen
         self.dependencies = sets.Set()
+        self.glvar = self.gen.get_global_tmp(
+            repr(self.tuple).replace(" ", "").translate(gensupp.C_IDENTIFIER))
+
+    lazy_attributes = ['l_tuple', 'type']
 
     def setup(self):
         self.l_tuple = [self.gen.get_repr(l) for l in list(self.tuple)]
-        self.glvar = self.gen.get_global_tmp(
-            repr(self.tuple).replace(" ", "").translate(gensupp.C_IDENTIFIER))
         self.dependencies.update(self.l_tuple)
         self.type = self.gen.get_repr(self.gen.annotator.binding(self.const))
         self.dependencies.add(self.type)

Modified: pypy/dist/pypy/translator/llvm/test/llvmsnippet.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/test/llvmsnippet.py	(original)
+++ pypy/dist/pypy/translator/llvm/test/llvmsnippet.py	Tue Apr 26 04:08:13 2005
@@ -414,7 +414,6 @@
 pbc1.a.pbc = pbc1
 pbc1.a.a = range(4)
 
-def pbc_function2(i): #Circular dependencies: doesn't work at the moment
+def pbc_function2(i):
     a = CIRCULAR1()
-    b = CIRCULAR2()
     return a.get(i)

Modified: pypy/dist/pypy/translator/llvm/test/test_genllvm.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/test/test_genllvm.py	(original)
+++ pypy/dist/pypy/translator/llvm/test/test_genllvm.py	Tue Apr 26 04:08:13 2005
@@ -202,9 +202,9 @@
 
     def test_array_pop(self):
         f = compile_function(llvmsnippet.array_pop, [int])
-        assert f(0) == 5
-        assert f(1) == 6
-        assert f(2) == 7
+        assert f(0) == 6
+        assert f(1) == 7
+        assert f(2) == 8
 
     def test_access_global_array(self):
         f = compile_function(llvmsnippet.access_global_array, [int, int, int])
@@ -348,7 +348,7 @@
         assert f(2) == 6
         assert f(3) == 8
 
-    def DONOT_test_pbc_function2(self):
+    def test_pbc_function2(self):
         f = compile_function(llvmsnippet.pbc_function2, [int])
         assert f(0) == 13
         assert f(1) == 15

Added: pypy/dist/pypy/translator/llvm/test/test_lazyattribute.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/llvm/test/test_lazyattribute.py	Tue Apr 26 04:08:13 2005
@@ -0,0 +1,49 @@
+import autopath
+import py
+
+from pypy.translator.llvm.lazyattribute import *
+
+class LazyAttributes(object):
+    __metaclass__ = MetaLazyRepr
+    def __init__(self, gen):
+        self.gen = gen
+
+    lazy_attributes = ["test", "test1", "test2"]
+
+    def setup(self):
+        self.test = "asdf"
+        self.test1 = 23931
+        self.test2 = 2 ** 2233
+
+class PseudoGen(object):
+    def __init__(self):
+        self.lazy_objects = sets.Set()
+
+
+class TestLazy(object):
+    def setup_class(cls):
+        cls.gen = PseudoGen()
+        cls.la = LazyAttributes(cls.gen)
+
+    def test_registration(self):
+        assert not self.la.__setup_called__
+        assert self.la in self.gen.lazy_objects
+
+    def test_setup(self):
+        print self.la.test2
+        assert self.la not in self.gen.lazy_objects
+        assert self.la.__setup_called__
+        assert self.la.test == "asdf"
+        assert self.la.test1 == 23931
+        assert self.la.test2 == 2 ** 2233
+
+    def test_attributeness(self):
+        self.la.test = 23
+        assert self.la.test == 23
+        del self.la.test1
+        py.test.raises(AttributeError, "self.la.test1")
+        py.test.raises(AttributeError, "del self.la.test1")
+        
+    def test_type(self):
+        assert type(self.la) == LazyAttributes
+        assert type(type(self.la)) == MetaLazyRepr

Modified: pypy/dist/pypy/translator/llvm/typerepr.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/typerepr.py	(original)
+++ pypy/dist/pypy/translator/llvm/typerepr.py	Tue Apr 26 04:08:13 2005
@@ -69,8 +69,6 @@
             print "StringTypeRepr"
         self.gen = gen
         self.dependencies = sets.Set()
-
-    def setup(self):
         self.l_charlist = self.gen.get_repr(
             annmodel.SomeList(ListDef(None, annmodel.SomeChar())))
         self.dependencies.add(self.l_charlist)
@@ -142,9 +140,6 @@
         elif obj.__class__ is annmodel.SomeChar:
             l_repr = SimpleTypeRepr("sbyte", gen)
             return l_repr
-##         elif obj.__class__ is annmodel.SomePBC:
-##             if obj.knowntype == object or obj.knowntype == ClassType:
-##                 return SimpleTypeRepr("%std.class*", gen)
         elif obj.__class__ is annmodel.SomeObject and \
              hasattr(obj, "is_type_of"):
             return SimpleTypeRepr("%std.class*", gen)



More information about the Pypy-commit mailing list