[pypy-svn] r15330 - in pypy/dist/pypy/translator/llvm2: module test

ericvrp at codespeak.net ericvrp at codespeak.net
Fri Jul 29 12:33:24 CEST 2005


Author: ericvrp
Date: Fri Jul 29 12:33:23 2005
New Revision: 15330

Added:
   pypy/dist/pypy/translator/llvm2/module/ll_math.py
   pypy/dist/pypy/translator/llvm2/module/ll_os.py
   pypy/dist/pypy/translator/llvm2/module/ll_os_path.py
   pypy/dist/pypy/translator/llvm2/module/ll_time.py
   pypy/dist/pypy/translator/llvm2/module/support.py
Modified:
   pypy/dist/pypy/translator/llvm2/module/extfunction.py
   pypy/dist/pypy/translator/llvm2/test/test_class.py
   pypy/dist/pypy/translator/llvm2/test/test_extfunc.py
   pypy/dist/pypy/translator/llvm2/test/test_genllvm.py
   pypy/dist/pypy/translator/llvm2/test/test_genllvm1.py
Log:
added simple os.math functions and tests


Modified: pypy/dist/pypy/translator/llvm2/module/extfunction.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/module/extfunction.py	(original)
+++ pypy/dist/pypy/translator/llvm2/module/extfunction.py	Fri Jul 29 12:33:23 2005
@@ -1,25 +1,16 @@
-extdeclarations =  """
+extdeclarations =  """;gc-type dependent mallocs
 declare sbyte* %gc_malloc(uint)
 declare sbyte* %gc_malloc_atomic(uint)
 
-declare int %time(int*) ;void* actually
-declare int %clock()
-declare void %sleep(int)
-declare int %dup(int)
-declare void %close(int)
-declare int %open(sbyte*, int, int)
-declare int %write(int, sbyte*, int)
-declare int %read(int, sbyte*, int)
-declare sbyte* %strncpy(sbyte*, sbyte*, int)
-
+;rpython stuff
 %structtype.rpy_string = type {int, {int, [0 x sbyte]}}
 
+;exception handling globals
 %last_exception_type  = global long 0
 %last_exception_value = global long 0
 """
 
-gc_boehm = """
-declare sbyte* %GC_malloc(uint)
+gc_boehm = """declare sbyte* %GC_malloc(uint)
 declare sbyte* %GC_malloc_atomic(uint)
 
 sbyte* %gc_malloc(uint %n) {
@@ -31,11 +22,9 @@
     %ptr = call sbyte* %GC_malloc_atomic(uint %n)
     ret sbyte* %ptr
 }
-
 """
 
-gc_disabled = """
-sbyte* %gc_malloc(uint %n) {
+gc_disabled = """sbyte* %gc_malloc(uint %n) {
     %ptr = malloc sbyte, uint %n
     ret sbyte* %ptr
 }
@@ -44,111 +33,16 @@
     %ptr = malloc sbyte, uint %n
     ret sbyte* %ptr
 }
-
 """
 
 extfunctions = {}   #dependencies, llvm-code
 
-extfunctions["%cast"] = ((), """
-sbyte* %cast(%structtype.rpy_string* %structstring) {
-    %reallengthptr = getelementptr %structtype.rpy_string* %structstring, int 0, uint 1, uint 0
-    %reallength = load int* %reallengthptr 
-    %length = add int %reallength, 1
-    %ulength = cast int %length to uint 
-    %dest = call sbyte* %gc_malloc_atomic(uint %ulength)
-
-    %source1ptr = getelementptr %structtype.rpy_string* %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
-}
-
-""")
-
-extfunctions["%ll_time_time"] = ((), """
-double %ll_time_time() {
-    %v0 = call int %time(int* null)
-    %v1 = cast int %v0 to double
-    ret double %v1
-}
-
-""")
-
-extfunctions["%ll_time_clock"] = ((), """
-double %ll_time_clock() {
-    %v0 = call int %clock()
-    %v1 = cast int %v0 to double
-    ; XXX how to get at the proper division (or any other) constant per platform? 
-    %v2 = div double %v1, 1000000.0    ;CLOCKS_PER_SEC accrdoing to single unix spec
-    ret double %v2
-}
-
-""")
-
-extfunctions["%ll_time_sleep"] = ((), """
-void %ll_time_sleep(double %f) {
-    %i = cast double %f to int
-    call void %sleep(int %i)
-    ret void
-}
-
-""")
-
-extfunctions["%ll_os_dup"] = ((), """
-int %ll_os_dup(int %fd) {
-    %ret = call int %dup(int %fd)
-    ret int %ret
-}
-
-""")
-
-extfunctions["%ll_os_close"] = ((), """
-void %ll_os_close(int %fd) {
-    call void %close(int %fd)
-    ret void
-}
-
-""")
-
-extfunctions["%ll_os_open"] = (("%cast",), """
-int %ll_os_open(%structtype.rpy_string* %structstring, int %flag, int %mode) {
-    %dest  = call sbyte* %cast(%structtype.rpy_string* %structstring)
-    %fd    = call int    %open(sbyte* %dest, int %flag, int %mode)
-    ret int %fd 
-}
-
-""")
-
-extfunctions["%ll_os_write"] = (("%cast",), """
-int %ll_os_write(int %fd, %structtype.rpy_string* %structstring) {
-    %reallengthptr = getelementptr %structtype.rpy_string* %structstring, int 0, uint 1, uint 0
-    %reallength    = load int* %reallengthptr 
-    %dest          = call sbyte* %cast(%structtype.rpy_string* %structstring)
-    %byteswritten  = call int    %write(int %fd, sbyte* %dest, int %reallength)
-    ret int %byteswritten
-}
-
-""")
-
-extfunctions["%ll_read_into"] = ((), """
-int %ll_read_into(int %fd, %structtype.rpy_string* %structstring) {
-    %reallengthptr = getelementptr %structtype.rpy_string* %structstring, int 0, uint 1, uint 0
-    %reallength    = load int* %reallengthptr 
-
-    %destptr   = getelementptr %structtype.rpy_string* %structstring, int 0, uint 1, uint 1
-    %dest      = cast [0 x sbyte]* %destptr to sbyte*
-
-    %bytesread = call int %read(int %fd, sbyte* %dest, int %reallength)
-    ret int %bytesread
-}
+import support, ll_os, ll_os_path, ll_time, ll_math
 
-""")
+for module in (support, ll_os, ll_os_path, ll_time, ll_math):
+    extdeclarations += module.extdeclarations
+    extfunctions.update(module.extfunctions)
+extdeclarations += '\n;application function prototypes'
 
 def dependencies(funcname, deplist):
     deplist.append(funcname)

Added: pypy/dist/pypy/translator/llvm2/module/ll_math.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/llvm2/module/ll_math.py	Fri Jul 29 12:33:23 2005
@@ -0,0 +1,37 @@
+extdeclarations = """
+;ll_math.py
+declare double %acos(double)
+declare double %asin(double)
+declare double %atan(double)
+declare double %ceil(double)
+declare double %cos(double)
+declare double %cosh(double)
+declare double %exp(double)
+declare double %fabs(double)
+declare double %floor(double)
+declare double %log(double)
+declare double %log10(double)
+declare double %sin(double)
+declare double %sinh(double)
+declare double %sqrt(double)
+declare double %tan(double)
+declare double %tanh(double)
+"""
+
+extfunctions = {}
+
+simple_math_functions = [
+    'acos', 'asin', 'atan', 'ceil', 'cos', 'cosh', 'exp', 'fabs',
+    'floor', 'log', 'log10', 'sin', 'sinh', 'sqrt', 'tan', 'tanh'
+    ]
+
+func_template = """
+double %%ll_math_%(func)s(double %%x) {
+    %%t = call double %%%(func)s(double %%x)
+    ret double %%t
+}
+
+"""
+
+for func in simple_math_functions:
+    extfunctions["%ll_math_" + func] = ((), func_template % locals())

Added: pypy/dist/pypy/translator/llvm2/module/ll_os.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/llvm2/module/ll_os.py	Fri Jul 29 12:33:23 2005
@@ -0,0 +1,61 @@
+extdeclarations = """
+;ll_os.py
+declare int %dup(int)
+declare void %close(int)
+declare int %open(sbyte*, int, int)
+declare int %write(int, sbyte*, int)
+declare int %read(int, sbyte*, int)
+declare sbyte* %strncpy(sbyte*, sbyte*, int)
+"""
+
+extfunctions = {}
+
+extfunctions["%ll_os_dup"] = ((), """
+int %ll_os_dup(int %fd) {
+    %ret = call int %dup(int %fd)
+    ret int %ret
+}
+
+""")
+
+extfunctions["%ll_os_close"] = ((), """
+void %ll_os_close(int %fd) {
+    call void %close(int %fd)
+    ret void
+}
+
+""")
+
+extfunctions["%ll_os_open"] = (("%cast",), """
+int %ll_os_open(%structtype.rpy_string* %structstring, int %flag, int %mode) {
+    %dest  = call sbyte* %cast(%structtype.rpy_string* %structstring)
+    %fd    = call int    %open(sbyte* %dest, int %flag, int %mode)
+    ret int %fd 
+}
+
+""")
+
+extfunctions["%ll_os_write"] = (("%cast",), """
+int %ll_os_write(int %fd, %structtype.rpy_string* %structstring) {
+    %reallengthptr = getelementptr %structtype.rpy_string* %structstring, int 0, uint 1, uint 0
+    %reallength    = load int* %reallengthptr 
+    %dest          = call sbyte* %cast(%structtype.rpy_string* %structstring)
+    %byteswritten  = call int    %write(int %fd, sbyte* %dest, int %reallength)
+    ret int %byteswritten
+}
+
+""")
+
+extfunctions["%ll_read_into"] = ((), """
+int %ll_read_into(int %fd, %structtype.rpy_string* %structstring) {
+    %reallengthptr = getelementptr %structtype.rpy_string* %structstring, int 0, uint 1, uint 0
+    %reallength    = load int* %reallengthptr 
+
+    %destptr   = getelementptr %structtype.rpy_string* %structstring, int 0, uint 1, uint 1
+    %dest      = cast [0 x sbyte]* %destptr to sbyte*
+
+    %bytesread = call int %read(int %fd, sbyte* %dest, int %reallength)
+    ret int %bytesread
+}
+
+""")

Added: pypy/dist/pypy/translator/llvm2/module/ll_os_path.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/llvm2/module/ll_os_path.py	Fri Jul 29 12:33:23 2005
@@ -0,0 +1,3 @@
+extdeclarations = ""
+
+extfunctions = {}

Added: pypy/dist/pypy/translator/llvm2/module/ll_time.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/llvm2/module/ll_time.py	Fri Jul 29 12:33:23 2005
@@ -0,0 +1,37 @@
+extdeclarations = '''
+;ll_time.py
+declare int %time(int*) ;void* actually
+declare int %clock()
+declare void %sleep(int)
+'''
+
+extfunctions = {}
+
+extfunctions["%ll_time_time"] = ((), """
+double %ll_time_time() {
+    %v0 = call int %time(int* null)
+    %v1 = cast int %v0 to double
+    ret double %v1
+}
+
+""")
+
+extfunctions["%ll_time_clock"] = ((), """
+double %ll_time_clock() {
+    %v0 = call int %clock()
+    %v1 = cast int %v0 to double
+    ; XXX how to get at the proper division (or any other) constant per platform?
+    %v2 = div double %v1, 1000000.0    ;CLOCKS_PER_SEC accrdoing to single unix spec
+    ret double %v2
+}
+
+""")
+
+extfunctions["%ll_time_sleep"] = ((), """
+void %ll_time_sleep(double %f) {
+    %i = cast double %f to int
+    call void %sleep(int %i)
+    ret void
+}
+
+""")

Added: pypy/dist/pypy/translator/llvm2/module/support.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/llvm2/module/support.py	Fri Jul 29 12:33:23 2005
@@ -0,0 +1,25 @@
+extdeclarations = ""
+
+extfunctions = {}
+
+extfunctions["%cast"] = ((), """
+sbyte* %cast(%structtype.rpy_string* %structstring) {
+    %reallengthptr = getelementptr %structtype.rpy_string* %structstring, int 0, uint 1, uint 0
+    %reallength = load int* %reallengthptr
+    %length = add int %reallength, 1
+    %ulength = cast int %length to uint
+    %dest = call sbyte* %gc_malloc_atomic(uint %ulength)
+
+    %source1ptr = getelementptr %structtype.rpy_string* %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
+}
+
+""")

Modified: pypy/dist/pypy/translator/llvm2/test/test_class.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/test/test_class.py	(original)
+++ pypy/dist/pypy/translator/llvm2/test/test_class.py	Fri Jul 29 12:33:23 2005
@@ -54,14 +54,14 @@
         assert f(False) == 2
 
     def test_global_instance(self):
-        py.test.skip("not working yet")
+        py.test.skip("modify global instance not working properly yet")
         f = compile_function(llvmsnippet.global_instance, [int])
         assert f(-1) == llvmsnippet.global_instance(-1)
         for i in range(20):
             assert f(i) == llvmsnippet.global_instance(i)
 
     def test_call_degrading_func(self):
-        py.test.skip("not working yet")
+        py.test.skip("call degrading not working yet")
         f = compile_function(llvmsnippet.call_degrading_func, [bool])
         assert f(True) == llvmsnippet.call_degrading_func(True)     #-36
         assert f(False) == llvmsnippet.call_degrading_func(False)   #14

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	Fri Jul 29 12:33:23 2005
@@ -91,3 +91,102 @@
     assert os.path.exists(path)
     assert open(path).read() == path * 3
     assert result is len(path) * 3
+
+# following from translator/c/test/test_extfunc.py Revision: 15320 (jul 29th 2005)
+
+def test_os_stat():
+    py.test.skip("ll_os_stat not implemented")
+    filename = str(py.magic.autopath())
+    def call_stat():
+        st = os.stat(filename)
+        return st
+    f = compile_function(call_stat, [])
+    result = f()
+    assert result[0] == os.stat(filename)[0]
+    assert result[1] == os.stat(filename)[1]
+    assert result[2] == os.stat(filename)[2]
+
+def test_os_fstat():
+    py.test.skip("ll_os_fstat not implemented")
+    filename = str(py.magic.autopath())
+    def call_fstat():
+        fd = os.open(filename, os.O_RDONLY, 0777)
+        st = os.fstat(fd)
+        os.close(fd)
+        return st
+    f = compile_function(call_fstat, [])
+    result = f()
+    assert result[0] == os.stat(filename)[0]
+    assert result[1] == os.stat(filename)[1]
+    assert result[2] == os.stat(filename)[2]
+
+def test_getcwd():
+    py.test.skip("ll_os_getcwd not implemented")
+    def does_stuff():
+        return os.getcwd()
+    f1 = compile_function(does_stuff, [])
+    res = f1()
+    assert res == os.getcwd()
+
+def test_math_frexp():
+    py.test.skip("ll_math_frexp not implemented")
+    from math import frexp
+    def fn(x):
+        return frexp(x)
+    f = compile_function(fn, [float])
+    assert f(10.123) == frexp(10.123)
+
+def test_math_modf():
+    py.test.skip("ll_math_modf not implemented (next)")
+    from math import modf
+    def fn(x):
+        return modf(x)
+    f = compile_function(fn, [float])
+    assert f(10.123) == modf(10.123)
+
+simple_math_functions = [
+    'acos', 'asin', 'atan', 'ceil', 'cos', 'cosh', 'exp', 'fabs',
+    'floor', 'log', 'log10', 'sin', 'sinh', 'sqrt', 'tan', 'tanh'
+    ]
+
+def math_function_test(funcname):
+    import random
+    import math
+    mathfn = getattr(math, funcname)
+    print funcname, 
+    def fn(x):
+        return mathfn(x)
+    f = compile_function(fn, [float])
+    for x in [0.12334, 0.3, 0.5, 0.9883]:
+        print x
+        assert f(x) == mathfn(x)
+
+def test_simple_math_functions():
+    for funcname in simple_math_functions:
+        yield math_function_test, funcname
+
+def test_os_path_exists():
+    py.test.skip("ll_os_stat not implemented")
+    tmpfile = str(udir.join('test_os_path_exists.TMP'))
+    def fn():
+        return os.path.exists(tmpfile)
+    f = compile_function(fn, [])
+    open(tmpfile, 'w').close()
+    assert f() == True
+    os.unlink(tmpfile)
+    assert f() == False
+
+def test_os_path_isdir():
+    py.test.skip("ll_os_stat not implemented")
+    directory = "./."
+    def fn():
+        return os.path.isdir(directory)
+    f = compile_function(fn, [])
+    assert f() == True
+    directory = "some/random/name"
+    def fn():
+        return os.path.isdir(directory)
+    f = compile_function(fn, [])
+    assert f() == False
+
+# end of tests taken from c backend

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	Fri Jul 29 12:33:23 2005
@@ -16,7 +16,7 @@
     assert f() == 1
 
 def test_simple_function_pointer(): 
-    py.test.skip("not working yet")
+    py.test.skip("function pointers not working yet")
     def f1(x): 
         return x + 1
     def f2(x): 

Modified: pypy/dist/pypy/translator/llvm2/test/test_genllvm1.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/test/test_genllvm1.py	(original)
+++ pypy/dist/pypy/translator/llvm2/test/test_genllvm1.py	Fri Jul 29 12:33:23 2005
@@ -89,7 +89,7 @@
         assert f(3) == 8
 
     def test_pbc_function2(self):
-        py.test.skip("not working yet")
+        py.test.skip("pbc circular not working yet")
         f = compile_function(llvmsnippet.pbc_function2, [int])
         assert f(0) == 13
         assert f(1) == 15



More information about the Pypy-commit mailing list