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

ericvrp at codespeak.net ericvrp at codespeak.net
Thu Jul 7 15:48:21 CEST 2005


Author: ericvrp
Date: Thu Jul  7 15:48:19 2005
New Revision: 14381

Added:
   pypy/dist/pypy/translator/llvm2/test/test_extfunc.py
   pypy/dist/pypy/translator/llvm2/test/test_gc.py
Modified:
   pypy/dist/pypy/translator/llvm2/extfunction.py
   pypy/dist/pypy/translator/llvm2/funcnode.py
   pypy/dist/pypy/translator/llvm2/genllvm.py
   pypy/dist/pypy/translator/llvm2/test/test_genllvm.py
Log:
(hpk, ericvrp):

Added os.open,os.close test that uses some dirty charater to string conversions because PBC string are not working yet.
Added ll_file_open llvm implementation that convert structstrings to sbyte*.
Refactored tests into three seperate files.



Modified: pypy/dist/pypy/translator/llvm2/extfunction.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/extfunction.py	(original)
+++ pypy/dist/pypy/translator/llvm2/extfunction.py	Thu Jul  7 15:48:19 2005
@@ -6,12 +6,37 @@
 declare int %time(int*) ;void* actually
 declare int %clock()
 declare void %sleep(int)
+declare int %open(sbyte*, int)
+declare sbyte* %strncpy(sbyte*, sbyte*, int)
+
+%st.rpy_string.0 = type {int, {int, [0 x sbyte]}}
 
 ; End of external declarations
 
 """
 
-extfunctions = """; External functions (will be inlined by LLVM)
+extfunctions = """; Helper function to convert LLVM <-> C types
+
+sbyte* %cast(%st.rpy_string.0* %structstring) {
+    %reallengthptr = getelementptr %st.rpy_string.0* %structstring, int 0, uint 1, uint 0
+    %reallength = load int* %reallengthptr 
+    %length = add int %reallength, 1
+    %ulength = cast int %length to uint 
+    %dest = malloc sbyte, uint %ulength     ;XXX should actually call GC_malloc when available!
+    
+    %source1ptr = getelementptr %st.rpy_string.0* %structstring, int 0, uint 1, uint 1
+    %source1 = cast [0 x sbyte]* %source1ptr to sbyte* 
+    %dummy = call sbyte* %strncpy(sbyte* %dest, sbyte* %source1, int %reallength) 
+    
+    %zeropos1 = cast sbyte* %dest to int 
+    %zeropos2 = add int %zeropos1, %reallength 
+    %zerodest = cast int %zeropos2 to sbyte* 
+    store sbyte 0, sbyte* %zerodest 
+
+    ret sbyte* %dest    ;XXX alloca freed at end of function. this will crash!
+}
+
+; Wrapper functions that call external (C) functions
 
 double %ll_time_time() {
     %v0 = call int %time(int* null)
@@ -22,7 +47,8 @@
 double %ll_time_clock() {
     %v0 = call int %clock()
     %v1 = cast int %v0 to double
-    %v2 = div double %v1, 1000000.0    ;CLOCKS_PER_SEC
+    ; XXX how to get at the proper division constant per platform? 
+    %v2 = div double %v1, 1000000.0    ;CLOCKS_PER_SEC accrdoing to single unix spec
     ret double %v2
 }
 
@@ -32,5 +58,12 @@
     ret void
 }
 
+int %ll_os_open(%st.rpy_string.0* %structstring, int %mode) {
+    %dest = call sbyte* %cast(%st.rpy_string.0* %structstring)
+    %fd   = call int    %open(sbyte* %dest, int %mode)
+    free sbyte* %dest
+    ret int %fd 
+}
+
 ; End of external functions
 """

Modified: pypy/dist/pypy/translator/llvm2/funcnode.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/funcnode.py	(original)
+++ pypy/dist/pypy/translator/llvm2/funcnode.py	Thu Jul  7 15:48:19 2005
@@ -162,11 +162,11 @@
     fnmapping = {
         "%ll_os_dup": "%dup",
         "%ll_os_close": "%close",
-        "%ll_os_open": "%open",
+        #"%ll_os_open": "%open",
         }
 
     ignoreset = "%ll_time_time %ll_time_clock %ll_time_sleep " \
-                "%ll_os_read %ll_os_write".split() 
+                "%ll_os_read %ll_os_write %ll_os_open".split() 
 
     def __init__(self, db, value):
         self.db = db

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 15:48:19 2005
@@ -12,14 +12,15 @@
 from pypy.tool.udir import udir
 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.backendoptimization import rename_extfunc_calls
 from pypy.translator.llvm2.extfunction import extdeclarations, extfunctions
+from pypy.translator.translator import Translator
 
 function_count = {}
 
 def genllvm(translator):
     remove_void(translator)
-    rename_extfunc_calls(translator)
+    #rename_extfunc_calls(translator)
     func = translator.entrypoint
 
     db = Database(translator)
@@ -88,4 +89,21 @@
     except py.error.ENOENT: 
         return False 
     return True
-    
+
+def compile_module(function, annotate, view=False):
+    t = Translator(function)
+    a = t.annotate(annotate)
+    t.specialize()
+    a.simplify()
+    if view:
+        t.view()
+    return genllvm(t)
+
+def compile_function(function, annotate, view=False):
+    mod = compile_module(function, annotate, view)
+    return getattr(mod, function.func_name + "_wrapper")
+
+def compile_module_function(function, annotate, view=False):
+    mod = compile_module(function, annotate, view)
+    f = getattr(mod, function.func_name + "_wrapper")
+    return mod, f

Added: pypy/dist/pypy/translator/llvm2/test/test_extfunc.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/llvm2/test/test_extfunc.py	Thu Jul  7 15:48:19 2005
@@ -0,0 +1,66 @@
+from __future__ import division
+
+import sys
+import py
+
+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)
+
+def test_external_function_ll_os_dup():
+    import os
+    def fn():
+        return os.dup(0)
+    f = compile_function(fn, [])
+    assert os.path.sameopenfile(f(), fn())
+
+def test_external_function_ll_time_time():
+    import time
+    def fn():
+        return time.time()
+    f = compile_function(fn, [], view=False)
+    assert abs(f()-fn()) < 10.0
+
+def test_external_function_ll_time_clock():
+    import time
+    def fn():
+        return time.clock()
+    f = compile_function(fn, [], view=False)
+    assert abs(f()-fn()) < 10.0
+
+def test_external_function_ll_time_sleep():
+    import time
+    def fn(t):
+        time.sleep(t)
+        return 666
+    f = compile_function(fn, [float], view=False)
+    start_time = time.time()
+    delay_time = 2.0
+    d = f(delay_time)
+    duration = time.time() - start_time
+    assert duration >= delay_time - 0.5
+    assert duration <= delay_time + 0.5
+
+class TestOSLevelFunctions: 
+    def setup_method(self, method): 
+        path = udir.join("e")
+        self.path = path 
+        self.pathints = map(ord, path)
+        
+def test_os_file_ops_open_close(): 
+    # the test is overly complicated because
+    # we don't have prebuilt string constants yet 
+    import os
+    def openclose(a,b,c,d,e,f): 
+        s = chr(a) + chr(b) + chr(c) + chr(d) + chr(e) + chr(f)
+        fd = os.open(s, os.O_CREAT) 
+        os.close(fd)
+        return fd 
+
+    path = '/tmp/b'
+    f = compile_function(openclose, [int] * len(path))
+    os.unlink(path)
+    result = f(*map(ord, path))
+    assert os.path.exists(path)

Added: pypy/dist/pypy/translator/llvm2/test/test_gc.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/llvm2/test/test_gc.py	Thu Jul  7 15:48:19 2005
@@ -0,0 +1,31 @@
+import sys
+import py
+
+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)
+
+def test_GC_malloc(): 
+    if not use_boehm_gc:
+        py.test.skip("test_GC_malloc skipped because Boehm collector library was not found")
+        return
+    def tuple_getitem(n): 
+        x = 666
+        i = 0
+        while i < n:
+            l = (1,2,i,4,5,6,7,8,9,10,11)
+            x += l[2]
+            i += 1
+        return x
+    mod,f = compile_module_function(tuple_getitem, [int])
+    n = 5000
+    result = tuple_getitem(n)
+    assert f(n) == result
+    get_heap_size = getattr(mod, "GC_get_heap_size_wrapper")
+    heap_size_start = get_heap_size()
+    for i in range(0,25):
+        assert f(n) == result
+        heap_size_inc = get_heap_size() - heap_size_start
+        assert heap_size_inc < 500000

Modified: pypy/dist/pypy/translator/llvm2/test/test_genllvm.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/test/test_genllvm.py	(original)
+++ pypy/dist/pypy/translator/llvm2/test/test_genllvm.py	Thu Jul  7 15:48:19 2005
@@ -3,130 +3,19 @@
 import sys
 import py
 
-from pypy.translator.translator import Translator
-from pypy.translator.llvm2.genllvm import genllvm
-from pypy.translator.llvm2.genllvm import use_boehm_gc
-from pypy.translator.llvm2.test import llvmsnippet
-from pypy.objspace.flow.model import Constant, Variable
-
-from pypy.rpython.rtyper import RPythonTyper
 from pypy.rpython.rarithmetic import r_uint
+from pypy.translator.llvm2.genllvm import compile_function
 
 py.log.setconsumer("genllvm", py.log.STDOUT)
 py.log.setconsumer("genllvm database prepare", None)
 
-
-## def setup_module(mod):
-##     mod.llvm_found = is_on_path("llvm-as")
-
-def compile_module(function, annotate, view=False):
-    t = Translator(function)
-    a = t.annotate(annotate)
-    t.specialize()
-    a.simplify()
-    if view:
-        t.view()
-    return genllvm(t)
-
-def compile_function(function, annotate, view=False):
-    mod = compile_module(function, annotate, view)
-    return getattr(mod, function.func_name + "_wrapper")
-
-def compile_module_function(function, annotate, view=False):
-    mod = compile_module(function, annotate, view)
-    f = getattr(mod, function.func_name + "_wrapper")
-    return mod, f
-
-def test_external_function_ll_os_dup():
-    import os
-    def fn():
-        return os.dup(0)
-    f = compile_function(fn, [])
-    assert os.path.sameopenfile(f(), fn())
-
-def test_external_file_fns():
-    import os
-    def external_file_fns(intname, flags):
-        name = str(intname)
-        fd = os.open(name, flags)
-        #os.write(fd, name)
-        os.close(fd)
-        return fd
-    
-    f = compile_function(external_file_fns, [int, int])
-    assert isinstance(f(1209319, 64), int)
-    
-def DONTtest_external_function_ll_os_getcmd():
-    import os
-    def fn():
-        return os.getcwd()
-    f = compile_function(fn, [], view=True)
-    assert os.getcwd() == 'bla'
-
-def DONTtest_external_function_ll_os_open():
-    import os
-    def fn():
-        return os.open(0)
-    f = compile_function(fn, [], view=False)
-    assert os.path.sameopenfile(f(), fn())
-
-def test_external_function_ll_time_time():
-    import time
-    def fn():
-        return time.time()
-    f = compile_function(fn, [], view=False)
-    assert abs(f()-fn()) < 1.0
-
-def test_external_function_ll_time_clock():
-    import time
-    def fn():
-        return time.clock()
-    f = compile_function(fn, [], view=False)
-    assert abs(f()-fn()) < 1.0
-
-def test_external_function_ll_time_sleep():
-    import time
-    def fn(t):
-        time.sleep(t)
-        return 666
-    f = compile_function(fn, [float], view=False)
-    start_time = time.time()
-    delay_time = 2.0
-    d = f(delay_time)
-    duration = time.time() - start_time
-    assert duration >= delay_time - 0.5
-    assert duration <= delay_time + 0.5
-
-def test_GC_malloc(): 
-    if not use_boehm_gc:
-        py.test.skip("test_GC_malloc skipped because Boehm collector library was not found")
-        return
-    def tuple_getitem(n): 
-        x = 666
-        i = 0
-        while i < n:
-            l = (1,2,i,4,5,6,7,8,9,10,11)
-            x += l[2]
-            i += 1
-        return x
-    mod,f = compile_module_function(tuple_getitem, [int])
-    n = 5000
-    result = tuple_getitem(n)
-    assert f(n) == result
-    get_heap_size = getattr(mod, "GC_get_heap_size_wrapper")
-    heap_size_start = get_heap_size()
-    for i in range(0,25):
-        assert f(n) == result
-        heap_size_inc = get_heap_size() - heap_size_start
-        assert heap_size_inc < 500000
-
 def test_return1():
     def simple1():
         return 1
     f = compile_function(simple1, [])
     assert f() == 1
 
-def Xtest_simple_function_pointer(): 
+def DONTtest_simple_function_pointer(): 
     def f1(x): 
         return x + 1
     def f2(x): 
@@ -316,7 +205,7 @@
     assert f(-1) == 3
     assert f(0) == 5
 
-def Xtest_simple_chars():
+def DONTtest_simple_chars():
      def char_constant2(s):
          s = s + s + s
          return len(s + '.')
@@ -394,14 +283,14 @@
     assert f(5) == ord('5') + 2
         
 
-def Xtest_string_getitem1():
+def DONTtest_string_getitem1():
     l = "Hello, World"
     def string_getitem1(i): 
         return l[i]
-    f = compile_function(string_getitem1, [int], view=False)
+    f = compile_function(string_getitem1, [int], view=True)
     assert f(0) == ord("H")
 
-def DONOT_test_string_getitem2():
+def DONTtest_string_getitem2():
     def string_test(i): 
         l = "Hello, World"
         return l[i]
@@ -411,7 +300,7 @@
 class TestException(Exception):
     pass
 
-def DONOTtest_exception():
+def DONTtest_exception():
     def raise_(i):
         if i:
             raise TestException()



More information about the Pypy-commit mailing list