[pypy-svn] r14394 - in pypy/dist/pypy/translator/llvm2: . test

rxe at codespeak.net rxe at codespeak.net
Thu Jul 7 18:57:55 CEST 2005


Author: rxe
Date: Thu Jul  7 18:57:54 2005
New Revision: 14394

Added:
   pypy/dist/pypy/translator/llvm2/test/test_lltype.py
Modified:
   pypy/dist/pypy/translator/llvm2/database.py
   pypy/dist/pypy/translator/llvm2/genllvm.py
   pypy/dist/pypy/translator/llvm2/test/test_extfunc.py
   pypy/dist/pypy/translator/llvm2/test/test_gc.py
Log:

* add new (and sane like) tests for lltypes
* refactor out genllvm function and add flag to avoid external functions being included
* add some basic support for unichars
* fix some typos in logging 



Modified: pypy/dist/pypy/translator/llvm2/database.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/database.py	(original)
+++ pypy/dist/pypy/translator/llvm2/database.py	Thu Jul  7 18:57:54 2005
@@ -13,6 +13,9 @@
                       lltype.Unsigned: "uint",
                       lltype.Bool: "bool",
                       lltype.Float: "double",
+                      # XXX Preliminary support for unicode, makes sense to
+                      # make this more configurable
+                      lltype.UniChar: "uint",
                       lltype.Void: "void"}
 
 class NormalizingDict(object): 

Modified: pypy/dist/pypy/translator/llvm2/genllvm.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/genllvm.py	(original)
+++ pypy/dist/pypy/translator/llvm2/genllvm.py	Thu Jul  7 18:57:54 2005
@@ -13,75 +13,101 @@
 from pypy.translator.llvm2.codewriter import CodeWriter
 from pypy.translator.backendoptimization import remove_void
 #from pypy.translator.backendoptimization import rename_extfunc_calls
-from pypy.translator.llvm2.extfunction import extdeclarations, extfunctions, gc_boehm, gc_disabled
+from pypy.translator.llvm2.extfunction import extdeclarations, \
+     extfunctions, gc_boehm, gc_disabled
+
 from pypy.translator.translator import Translator
 
 function_count = {}
 
-def genllvm(translator):
-    remove_void(translator)
-    #rename_extfunc_calls(translator)
-    func = translator.entrypoint
-
-    db = Database(translator)
-    ptr = getfunctionptr(translator, func)
-    c = inputconst(lltype.typeOf(ptr), ptr)
-    db.prepare_repr_arg(c)
-    assert c in db.obj2node
-    db.setup_all()
-    entrynode = db.obj2node[c]
-    codewriter = CodeWriter()
-    comment = codewriter.comment
-    nl = codewriter.newline
-    
-    nl(); comment("Type Declarations"); nl()
-    for typ_decl in db.getobjects():
-        typ_decl.writedatatypedecl(codewriter)
-
-    nl(); comment("Global Data") ; nl()
-    for typ_decl in db.getobjects():
-        typ_decl.writeglobalconstants(codewriter)
-
-    nl(); comment("Function Prototypes") ; nl()
-    for extdecl in extdeclarations.split('\n'):
-        codewriter.append(extdecl)
-    for typ_decl in db.getobjects():
-        typ_decl.writedecl(codewriter)
-
-    #import pdb ; pdb.set_trace()
-    nl(); comment("Function Implementation") 
-    codewriter.startimpl()
-    if use_boehm_gc:
-        gc_funcs = gc_boehm
-    else:
-        gc_funcs = gc_disabled
-    for extfunc in (gc_funcs + extfunctions).split('\n'):
-        codewriter.append(extfunc)
-    for typ_decl in db.getobjects():
-        typ_decl.writeimpl(codewriter)
-
-    comment("End of file") ; nl()
-
-    if func.func_name in function_count:
-        postfix = '_%d' % function_count[func.func_name]
-        function_count[func.func_name] += 1
-    else:
-        postfix = ''
-        function_count[func.func_name] = 1
-    
-    targetdir = udir
-    llvmsource = targetdir.join(func.func_name+postfix).new(ext='.ll')
-    content = str(codewriter) 
-    llvmsource.write(content) 
-    log.source(content)
-  
-    if not llvm_is_on_path(): 
-        py.test.skip("llvm not found")  # XXX not good to call py.test.skip here
-
-    pyxsource = llvmsource.new(basename=llvmsource.purebasename+'_wrapper'+postfix+'.pyx')
-    write_pyx_wrapper(entrynode, pyxsource)    
-    
-    return build_llvm_module.make_module_from_llvm(llvmsource, pyxsource)
+class GenLLVM(object):
+
+    def __init__(self, translator, embedexterns=True):
+        self.db = Database(translator)
+        self.translator = translator
+        self.embedexterns = embedexterns
+        # transformations
+        remove_void(translator)
+        #rename_extfunc_calls(translator)
+        translator.checkgraphs()
+
+    def compile(self, func=None):
+        if func is None:
+            func = self.translator.entrypoint
+        self.entrypoint = func
+        
+        ptr = getfunctionptr(self.translator, func)
+        c = inputconst(lltype.typeOf(ptr), ptr)
+        self.db.prepare_repr_arg(c)
+        assert c in self.db.obj2node
+        self.db.setup_all()
+        self.entrynode = self.db.obj2node[c]
+        codewriter = CodeWriter()
+        comment = codewriter.comment
+        nl = codewriter.newline
+
+        nl(); comment("Type Declarations"); nl()
+        for typ_decl in self.db.getobjects():
+            typ_decl.writedatatypedecl(codewriter)
+
+        nl(); comment("Global Data") ; nl()
+        for typ_decl in self.db.getobjects():
+            typ_decl.writeglobalconstants(codewriter)
+
+        nl(); comment("Function Prototypes") ; nl()
+        if self.embedexterns:
+            for extdecl in extdeclarations.split('\n'):
+                codewriter.append(extdecl)
+        for typ_decl in self.db.getobjects():
+            typ_decl.writedecl(codewriter)
+
+        #import pdb ; pdb.set_trace()
+        nl(); comment("Function Implementation") 
+        codewriter.startimpl()
+        if self.embedexterns:
+            for extfunc in extfunctions.split('\n'):
+                codewriter.append(extfunc)
+                
+        if use_boehm_gc:
+            gc_funcs = gc_boehm
+        else:
+            gc_funcs = gc_disabled    
+        for extfunc in gc_funcs.split('\n'):
+            codewriter.append(extfunc)
+            
+        for typ_decl in self.db.getobjects():
+            typ_decl.writeimpl(codewriter)
+
+        comment("End of file") ; nl()
+        self.content = str(codewriter)
+        return self.content
+
+    def create_module(self):
+        # hack to prevent running the same function twice in a test
+        func = self.entrypoint
+        if func.func_name in function_count:
+            postfix = '_%d' % function_count[func.func_name]
+            function_count[func.func_name] += 1
+        else:
+            postfix = ''
+            function_count[func.func_name] = 1
+
+        targetdir = udir
+        llvmsource = targetdir.join(func.func_name+postfix).new(ext='.ll')
+        llvmsource.write(self.content) 
+
+        if not llvm_is_on_path(): 
+            py.test.skip("llvm not found")  # XXX not good to call py.test.skip here
+
+        pyxsource = llvmsource.new(basename=llvmsource.purebasename+'_wrapper'+postfix+'.pyx')
+        write_pyx_wrapper(self.entrynode, pyxsource)    
+
+        return build_llvm_module.make_module_from_llvm(llvmsource, pyxsource)
+        
+def genllvm(translator, embedexterns=True):
+    gen = GenLLVM(translator, embedexterns=embedexterns)
+    log.source(gen.compile())
+    return gen.create_module()
 
 def llvm_is_on_path():
     try:
@@ -90,20 +116,20 @@
         return False 
     return True
 
-def compile_module(function, annotate, view=False):
+def compile_module(function, annotate, view=False, embedexterns=True):
     t = Translator(function)
     a = t.annotate(annotate)
     t.specialize()
     a.simplify()
     if view:
         t.view()
-    return genllvm(t)
+    return genllvm(t, embedexterns=embedexterns)
 
-def compile_function(function, annotate, view=False):
-    mod = compile_module(function, annotate, view)
+def compile_function(function, annotate, view=False, embedexterns=True):
+    mod = compile_module(function, annotate, view, embedexterns=embedexterns)
     return getattr(mod, function.func_name + "_wrapper")
 
-def compile_module_function(function, annotate, view=False):
-    mod = compile_module(function, annotate, view)
+def compile_module_function(function, annotate, view=False, embedexterns=True):
+    mod = compile_module(function, annotate, view, embedexterns=embedexterns)
     f = getattr(mod, function.func_name + "_wrapper")
     return mod, f

Modified: pypy/dist/pypy/translator/llvm2/test/test_extfunc.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/test/test_extfunc.py	(original)
+++ pypy/dist/pypy/translator/llvm2/test/test_extfunc.py	Thu Jul  7 18:57:54 2005
@@ -6,8 +6,8 @@
 from pypy.tool.udir import udir
 from pypy.translator.llvm2.genllvm import compile_function
 
-py.log.setconsumer("extfunc", py.log.STDOUT)
-py.log.setconsumer("extfunc database prepare", None)
+py.log.setconsumer("genllvm", py.log.STDOUT)
+py.log.setconsumer("genllvm database prepare", None)
 
 def test_external_function_ll_os_dup():
     import os

Modified: pypy/dist/pypy/translator/llvm2/test/test_gc.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/test/test_gc.py	(original)
+++ pypy/dist/pypy/translator/llvm2/test/test_gc.py	Thu Jul  7 18:57:54 2005
@@ -4,8 +4,8 @@
 from pypy.translator.llvm2.genllvm import use_boehm_gc
 from pypy.translator.llvm2.genllvm import compile_module_function
 
-py.log.setconsumer("test_gc", py.log.STDOUT)
-py.log.setconsumer("test_gc database prepare", None)
+py.log.setconsumer("genllvm", py.log.STDOUT)
+py.log.setconsumer("genllvm database prepare", None)
 
 def test_GC_malloc(): 
     if not use_boehm_gc:

Added: pypy/dist/pypy/translator/llvm2/test/test_lltype.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/llvm2/test/test_lltype.py	Thu Jul  7 18:57:54 2005
@@ -0,0 +1,60 @@
+import py
+
+from pypy.rpython import lltype
+
+from pypy.translator.llvm2.genllvm import compile_function
+from pypy.translator.llvm2 import database, codewriter
+
+py.log.setconsumer("genllvm", py.log.STDOUT)
+py.log.setconsumer("genllvm database prepare", None)
+
+P = lltype.GcStruct("s",
+                    ('signed', lltype.Signed),
+                    ('unsigned', lltype.Unsigned),
+                    ('float', lltype.Float),
+                    ('char', lltype.Char),
+                    ('bool', lltype.Bool),
+                    ('unichar', lltype.UniChar)
+                    )
+
+def test_struct1():
+    # struct of primitives
+    def simple1():
+        s = lltype.malloc(P)
+        return s.signed# + s.unsigned + s.float + s.char + s.bool + s.unichar
+    fn = compile_function(simple1, [], embedexterns=False)
+    assert fn() == 0
+
+# def test_struct2():
+#     S = lltype.Struct("s", ('v', lltype.Signed))
+#     S2 = lltype.GcStruct("s2", ('a', S), ('b', S))
+#     def simple2():
+#         s = lltype.malloc(S2)
+#         s.a.v = 6
+#         s.b.v = 12
+#         return s.a.v + s.b.v
+#     fn = compile_function(simple2, [], embedexterns=False, view=True)
+#     assert fn() == 18
+
+# def test_simple_struct():
+#     S0 = lltype.GcStruct("s0", ('a', lltype.Signed), ('b', lltype.Signed))
+#     c0 = lltype.malloc(S0)
+#     c0.a, c0.b = 1, 2
+#     def simple_struct():
+#         return c0.a + c0.b
+#     f = compile_function(simple_struct, [], embedexterns=False, view=True)
+#     assert f() == 3
+
+# def test_simple_struct2():
+#     S0 = lltype.GcStruct("s0", ('a', lltype.Char), ('b', lltype.Signed))
+#     def build():
+#         s0 = lltype.malloc(S0)
+#         s0.a = "l"
+#         s0.b = 2
+#         return s0
+#     c0 = build()
+#     def simple_struct2():
+#         return c0.a + c0.b
+#     f = compile_function(simple_struct, [], embedexterns=False, view=True)
+#     assert f() == 3
+



More information about the Pypy-commit mailing list