[pypy-svn] r13168 - pypy/dist/pypy/translator/llvm

cfbolz at codespeak.net cfbolz at codespeak.net
Tue Jun 7 23:30:56 CEST 2005


Author: cfbolz
Date: Tue Jun  7 23:30:55 2005
New Revision: 13168

Modified:
   pypy/dist/pypy/translator/llvm/funcrepr.py
   pypy/dist/pypy/translator/llvm/genllvm.py
   pypy/dist/pypy/translator/llvm/lazyattribute.py
   pypy/dist/pypy/translator/llvm/representation.py
   pypy/dist/pypy/translator/llvm/reprmap.py
   pypy/dist/pypy/translator/llvm/typerepr.py
Log:
started refactoring genllvm. this is an intermediate checking.
It's a big mess now, lots of debug prints and comments. I'll clean this up
real soon now.



Modified: pypy/dist/pypy/translator/llvm/funcrepr.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/funcrepr.py	(original)
+++ pypy/dist/pypy/translator/llvm/funcrepr.py	Tue Jun  7 23:30:55 2005
@@ -11,15 +11,18 @@
 from pypy.objspace.flow.model import traverse, checkgraph
 from pypy.annotation import model as annmodel
 from pypy.annotation.builtin import BUILTIN_ANALYZERS
-from pypy.translator.llvm import llvmbc
-from pypy.translator.unsimplify import remove_double_links
 
+from pypy.rpython import lltype
+
+from pypy.translator.unsimplify import remove_double_links
+from pypy.translator.llvm import llvmbc
 from pypy.translator.llvm.representation import debug, LLVMRepr, CompileError
 from pypy.translator.llvm.representation import last_exception, last_exc_value
 from pypy.translator.llvm.representation import SimpleRepr
 from pypy.translator.llvm.typerepr import TypeRepr, PointerTypeRepr
 
-debug = False
+debug = True
+lazy_debug = True
 
 INTRINSIC_OPS = ["lt", "le", "eq", "ne", "gt", "ge", "is_", "is_true", "len",
                  "neg", "pos", "invert", "add", "sub", "mul", "truediv",
@@ -74,10 +77,18 @@
             l_args[1] = l_tmp
         lblock.call(l_target, l_args[0], l_args[1:])
 
+
 class FunctionRepr(LLVMRepr):
     l_functions = {}
     def get(obj, gen):
-        name = None
+        if isinstance(obj, lltype._func):
+            print "1a)"
+            if obj._callable not in FunctionRepr.l_functions:
+                print "1b)"
+                FunctionRepr.l_functions[obj._callable] = FunctionRepr(obj, gen)
+            return FunctionRepr.l_functions[obj._callable]
+        return None
+        name = None #ZZZZ this can probably be removed
         if (isinstance(obj, annmodel.SomePBC) and
             len(obj.prebuiltinstances) == 1 and
             isinstance(obj.prebuiltinstances.keys()[0], FunctionType)):
@@ -98,38 +109,47 @@
         return None
     get = staticmethod(get)
 
-    def __init__(self, name, function, gen):
+    def __init__(self, function, gen):
         if debug:
-            print "FunctionRepr: %s" % name
+            print "FunctionRepr: %s" % function
         self.gen = gen
         self.func = function
         self.translator = gen.translator
-        self.name = gen.get_global_tmp(name)
-        self.graph = self.translator.getflowgraph(self.func)
+        if debug: print "init 1a)"
+        #if debug: print 'QQQ',function.name,'QQQ'
+        #if debug: print "init 1b)"
+        print function, function.__class__
+        self.name = gen.get_global_tmp(function._name)
+        if debug: print "init 2)"
+        self.graph = function.graph
         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]
+        if debug: print "init 3)"
+        self.type = self.gen.get_repr(function._TYPE)
+        if debug: print "init 4)"
+        self.l_args = self.type.l_args
+        self.dependencies.add(self.type)
         self.dependencies.update(self.l_args)
+        if debug: print "init 8)"
         self.l_default_args = None
         remove_double_links(self.translator, self.graph)
+        print "init 9)"
         self.get_bbs()
+        print "init done"
 
-    lazy_attributes = ['llvm_func', 'lblocks']
+    lazy_attributes = ['llvm_func', 'lblocks'] 
 
     def setup(self):
+        print "setup"
         self.se = True
         self.lblocks = []
         self.build_bbs()
 
     def get_returntype():
-        return self.rettype.llvmname()
+        return self.type.l_returntype.typename()
 
     def get_bbs(self):
         def visit(node):
@@ -137,7 +157,7 @@
                 self.allblocks.append(node)
                 self.blocknum[node] = len(self.blocknum)
         traverse(visit, self.graph)
-        self.same_origin_block = [False] * len(self.allblocks)
+#        self.same_origin_block = [False] * len(self.allblocks)
 
     def build_bbs(self):
         for number, pyblock in enumerate(self.allblocks):
@@ -159,11 +179,11 @@
         self.lblocks.append(lblock)
 
     def llvmfuncdef(self):
-        s = "internal %s %s(" % (self.l_retvalue.llvmtype(), self.name)
+        s = "internal %s %s(" % (self.type.l_returntype.typename(), self.name)
         return s + ", ".join([a.typed_name() for a in self.l_args]) + ")"
 
     def rettype(self):
-        return self.l_retvalue.llvmtype()
+        return self.type.l_rettype.llvmname()
 
     def get_functions(self):
         return str(self.llvm_func)
@@ -460,7 +480,9 @@
     lazy_attributes = ['l_function', 'llvm_func', 'init_block', 'exceptblock']
 
     def setup(self):
-        self.l_function = self.gen.get_repr(self.function)
+        f = self.gen.rtyper.getfunctionptr(self.function)._obj
+        print "EntryFunctionRepr", f
+        self.l_function = self.gen.get_repr(f)
         self.dependencies.add(self.l_function)
         #XXX clean this up
         #create entry block
@@ -476,18 +498,19 @@
         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.l_retvalue.type,
+        l_ret = self.gen.get_local_tmp(self.l_function.type.l_returntype,
                                        self)
         real_entry.last_op = True
         self.l_function.op_simple_call(
-            l_ret, [self.function] + self.l_function.l_args, real_entry, self)
+            l_ret, [self.gen.rtyper.getfunctionptr(self.function)._obj] +
+            self.l_function.l_args, real_entry, self)
         self.llvm_func.basic_block(real_entry)
         #create the block that catches remaining unwinds and sets
         #pypy____uncaught_exception to 1
         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.l_retvalue.llvmtype()] * 2)
+\tret %s %%dummy_ret""" % tuple([self.l_function.type.l_returntype.typename()] * 2)
         self.exceptblock.instruction(ins)
         self.exceptblock.closed = True
         self.llvm_func.basic_block(self.exceptblock)
@@ -503,8 +526,8 @@
             rettype_c = C_SIMPLE_TYPES[a.binding(retv).__class__]
             self.void = False
         except KeyError:
-            l_retv = self.l_function.l_retvalue
-            if l_retv.llvmtype() == "%std.void*":
+            l_rett = self.l_function.type.l_returntype
+            if l_retv.llvmname() == "%std.void*":
                 rettype_c = "void"
                 self.void = True
             else:
@@ -516,7 +539,7 @@
         return fd
 
     def llvmfuncdef(self):
-        s = "%s %s(" % (self.l_function.l_retvalue.llvmtype(), self.name)
+        s = "%s %s(" % (self.l_function.type.l_returntype.typename(), self.name)
         s += ", ".join([a.typed_name() for a in self.l_function.l_args]) + ")"
         return s
 
@@ -549,7 +572,7 @@
         return self.pyrex_source
 
     def rettype(self):
-        return self.l_function.l_retvalue.llvmtype()
+        return self.l_function.type.l_returntype.typename()
 
     def get_functions(self):
         if not self.branch_added:
@@ -573,6 +596,7 @@
     # to the appropriate class
     # Should be replaced by function pointers
     def get(obj, gen):
+        return None #this function will go away, I just disable it for now
         if (isinstance(obj, annmodel.SomePBC) and
             len(obj.prebuiltinstances) > 1 and
             isinstance(obj.prebuiltinstances.keys()[0], FunctionType)):

Modified: pypy/dist/pypy/translator/llvm/genllvm.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/genllvm.py	(original)
+++ pypy/dist/pypy/translator/llvm/genllvm.py	Tue Jun  7 23:30:55 2005
@@ -77,7 +77,11 @@
                     pbcrepr]:
             self.repr_classes += [getattr(mod, s)
                                   for s in dir(mod) if "Repr" in s]
+        #if debug:
+        #    print 'LLVMGenerator before repr_classes=', len(self.repr_classes), self.repr_classes
         self.repr_classes = [c for c in self.repr_classes if hasattr(c, "get")]
+        #if debug:
+        #    print 'LLVMGenerator after  repr_classes=', len(self.repr_classes), self.repr_classes
         self.llvm_reprs = {}
         self.depth = 0
         self.entryname = self.translator.functions[0].__name__
@@ -85,10 +89,8 @@
         self.l_entrypoint = EntryFunctionRepr("%__entry__" + self.entryname,
                                               self.translator.functions[0],
                                               self)
-##         classrepr.create_builtin_exceptions(
-##             self, self.l_entrypoint.get_dependencies())
         self.local_counts[self.l_entrypoint] = 0
-        self.l_entrypoint.setup()
+        #self.l_entrypoint.setup()
 
     def compile(self, optimize=False):
         from pypy.tool.udir import udir
@@ -136,12 +138,8 @@
                 print "->exists already:", self.llvm_reprs[key]
             return self.llvm_reprs[key]
         elif isinstance(obj, Variable):
-            try:
-                obj.concretetype
-            except AttributeError:
-                self.rtyper.setconcretetype(obj)
             result = representation.VariableRepr(obj, self)
-            self.llvm_reprs[result] = result
+            self.llvm_reprs[key] = result
             self.depth -= 1
             return result
         elif isinstance(obj, lltype.Primitive):
@@ -151,8 +149,8 @@
             return result
         if isinstance(obj, Constant):
             try:
-                T = lltype.typeOf(obj)
-                if isinstance(T, lltype.Primitive):
+                concretetype = obj.concretetype
+                if isinstance(concretetype, lltype.Primitive):
                     result = reprmap.PRIMITIVE_REPRS[concretetype](obj, self)
                     self.llvm_reprs[key] = result
                     self.depth -= 1
@@ -161,14 +159,22 @@
             except AttributeError:
                 pass
         for cl in self.repr_classes:
+            if 0: #debug:
+                print 'try cl.get(obj, self) where cl=', cl
             try:
                 g = cl.get(obj, self)
+                #if debug:
+                #    print 'A) g=', g
             except AttributeError:
                 continue
+            #if debug:
+            #    print 'B) g=', g
             if g is not None:
                 self.llvm_reprs[key] = g
                 self.local_counts[g] = 0
                 self.depth -= 1
+                #if debug:
+                #    print 'C) should return here'
                 return g
         raise CompileError, "Can't get repr of %s, %s" % (obj, obj.__class__)
 
@@ -257,11 +263,3 @@
             remove_loops(l_dep, seen_repr.union(sets.Set([l_repr])))
     deps.difference_update(remove)
 
-## def f():
-##     l = [10,20,30]
-##     return l[2]
-
-## t = Translator(f)
-## a = t.annotate([])
-
-## flg = t.getflowgraph()

Modified: pypy/dist/pypy/translator/llvm/lazyattribute.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/lazyattribute.py	(original)
+++ pypy/dist/pypy/translator/llvm/lazyattribute.py	Tue Jun  7 23:30:55 2005
@@ -2,6 +2,8 @@
 
 import sets
 
+debug = True
+
 def create_property(cls, la, name):
     def get(self):
         self.setup()
@@ -27,7 +29,7 @@
             def setup(self):
                 if self.__setup_called__:
                     return
-                if getattr(cls.__module__, "lazy_debug", None):
+                if debug:
                     print "calling setup of class", name
                 self.__setup_called__ = True
                 self.gen.lazy_objects.discard(self)

Modified: pypy/dist/pypy/translator/llvm/representation.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/representation.py	(original)
+++ pypy/dist/pypy/translator/llvm/representation.py	Tue Jun  7 23:30:55 2005
@@ -55,30 +55,30 @@
     def setup(self):
         pass
     
-    def get_globals(self):
+    def get_globals(self): #this is called when the global definitions are collected
         return ""
 
-    def get_functions(self):
+    def get_functions(self): #this is called, when functions are collected
         return ""
 
-    def collect_init_code(self, lblock, l_func):
-        pass
+    def collect_init_code(self, lblock, l_func): #this collects init code
+        pass                                     #it is only executed once at module import time
 
-    def llvmname(self):
+    def llvmname(self):  #this is the name of the object in LLVM world: "5" for the int 5
         return self.name
 
-    def llvmtype(self):
+    def llvmtype(self):  #this is the name of the type of the object: "long" for and int
         return self.type.typename()
 
-    def llvmsize(self):
+    def llvmsize(self):  #this is the size in bytes -- not really used yet, but will be soon
         raise NotImplementedError, "This object has no size!"
 
-    def op(self, opname, l_target, args, lblock, l_func):
-        if hasattr(self, "type") and hasattr(self.type, "t_op"):
+    def op(self, opname, l_target, args, lblock, l_func): #This is used when code is generated:
+        if hasattr(self, "type") and hasattr(self.type, "t_op"): #I'll tell you about it soon
             return self.type.t_op(opname, l_target, args, lblock, l_func)
         raise CompileError, "op '%s' not supported" % opname
 
-    def typed_name(self):
+    def typed_name(self): #helper function: gives "int 5" for the int 5
         return self.llvmtype() + " " + self.llvmname()
 
     def get_dependencies(self):
@@ -99,7 +99,7 @@
         return str(self.value)
 
     def get_dependencies(self):
-        return [self.type]
+        return sets.Set([self.type])
 
 class UnsignedRepr(LLVMRepr):
     def __init__(self, value, gen):
@@ -113,7 +113,7 @@
         return str(self.value)
 
     def get_dependencies(self):
-        return [self.type]
+        return sets.Set([self.type])
 
 class BoolRepr(LLVMRepr):
     def __init__(self, value, gen):
@@ -127,7 +127,21 @@
         return str(self.value).lower()
         
     def get_dependencies(self):
-        return [self.type]
+        return sets.Set([self.type])
+
+class FloatRepr(LLVMRepr):
+    def __init__(self, value, gen):
+        if debug:
+            print "FloatRepr: %f" % value
+        self.value = float(value)
+        self.gen = gen
+        self.type = self.gen.get_repr(lltype.Float)
+
+    def llvmname(self):
+        return str(self.value).lower()
+        
+    def get_dependencies(self):
+        return sets.Set([self.type])
 
 class CharRepr(LLVMRepr):
     def __init__(self, value, gen):
@@ -146,7 +160,7 @@
             return '%d' % ord(value)
         
     def get_dependencies(self):
-        return [self.type]
+        return sets.Set([self.type])
 
        
 class SimpleRepr(LLVMRepr):

Modified: pypy/dist/pypy/translator/llvm/reprmap.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/reprmap.py	(original)
+++ pypy/dist/pypy/translator/llvm/reprmap.py	Tue Jun  7 23:30:55 2005
@@ -3,13 +3,14 @@
 from pypy.rpython import lltype
 
 from pypy.translator.llvm import representation, funcrepr, typerepr, seqrepr
-from pypy.translator.llvm import classrepr, pbcrepr
+from pypy.translator.llvm import classrepr, pbcrepr, pointerrepr
 
 PRIMITIVE_REPRS = {
     lltype.Signed: representation.SignedRepr,
     lltype.Unsigned: representation.UnsignedRepr,
     lltype.Char: representation.CharRepr,
-    lltype.Bool: representation.BoolRepr
+    lltype.Bool: representation.BoolRepr,
+    lltype.Float: representation.FloatRepr,
 }
     
 PRIMITIVE_TYPES = {
@@ -17,5 +18,10 @@
     lltype.Unsigned: typerepr.UnsignedTypeRepr,
     lltype.Char: typerepr.CharTypeRepr,
     lltype.Bool: typerepr.BoolTypeRepr,
+    lltype.Float: typerepr.FloatTypeRepr,
 }
     
+#def ptr_types(ptr):
+#    if isinstance(ptr._obj, lltype._func):
+#        return pointerrepr.FuncPointerRepr(ptr)
+        

Modified: pypy/dist/pypy/translator/llvm/typerepr.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/typerepr.py	(original)
+++ pypy/dist/pypy/translator/llvm/typerepr.py	Tue Jun  7 23:30:55 2005
@@ -15,13 +15,17 @@
 from pypy.translator.llvm.representation import debug, LLVMRepr, CompileError
 from pypy.translator.llvm.representation import LLVM_SIMPLE_TYPES
 
+from pypy.rpython import lltype
 
 import sys
+
 if 2147483647 == sys.maxint:
     BYTES_IN_INT = 4
 else:
     BYTES_IN_INT = 8
 
+lazy_debug = True
+debug = True
 
 class TypeRepr(LLVMRepr):
     def get(obj, gen):
@@ -137,6 +141,18 @@
     def llvmsize(self):
         return 1
 
+class FloatTypeRepr(TypeRepr):
+    def __init__(self, gen):
+        if debug:
+            print "FloatTypeRepr"
+        self.gen = gen
+
+    def typename(self):
+        return "double"
+
+    def llvmsize(self):
+        return 8
+
 class CharTypeRepr(TypeRepr):
     def __init__(self, gen):
         if debug:
@@ -149,6 +165,25 @@
     def llvmsize(self):
         return 1
 
+class FuncTypeRepr(TypeRepr):
+    def get(obj, gen):
+        if obj.__class__ is lltype.FuncType:
+            return FuncTypeRepr(obj, gen)
+    get = staticmethod(get)
+
+    def __init__(self, functype, gen):
+        if debug:
+            print "FuncTypeRepr: %s" % functype
+        self.gen = gen
+        self.functype = functype
+        self.l_returntype = self.gen.get_repr(functype.RESULT)
+        self.l_args = [self.gen.get_repr(arg) for arg in functype.ARGS]
+        self.dependencies = sets.Set(self.l_args + [self.l_returntype])
+
+    def typename(self):
+        args = ", ".join([l_arg.llvmname() for arg in self.l_args])
+        return "%s (%s)" % (self.l_returntype.llvmname(), args)
+        
 
 class StringTypeRepr(TypeRepr):
     def get(obj, gen):



More information about the Pypy-commit mailing list