[pypy-svn] r14355 - in pypy/dist/pypy: rpython translator/llvm2 translator/llvm2/test
ericvrp at codespeak.net
ericvrp at codespeak.net
Wed Jul 6 20:26:19 CEST 2005
Author: ericvrp
Date: Wed Jul 6 20:26:18 2005
New Revision: 14355
Modified:
pypy/dist/pypy/rpython/extfunctable.py
pypy/dist/pypy/translator/llvm2/extfunction.py
pypy/dist/pypy/translator/llvm2/test/test_genllvm.py
Log:
implemented time methods clock,time and sleep
Modified: pypy/dist/pypy/rpython/extfunctable.py
==============================================================================
--- pypy/dist/pypy/rpython/extfunctable.py (original)
+++ pypy/dist/pypy/rpython/extfunctable.py Wed Jul 6 20:26:18 2005
@@ -2,6 +2,7 @@
information table about external functions for annotation/ rtyping and backends
"""
import os
+import time
import types
class ExtFuncInfo:
@@ -48,10 +49,22 @@
def ll_os_dup(fd):
return 999
+def ll_time_time():
+ return time.time()
+
+def ll_time_clock():
+ return time.clock()
+
+def ll_time_sleep(t):
+ time.sleep(t)
+
# external function declarations
-declare(os.open , int , ll_os_open , False, 'C:open' )
-declare(os.read , str , ll_os_read , False, 'C:read' )
-declare(os.write , int , ll_os_write , False, 'C:write' )
-declare(os.close , None, ll_os_close , False, 'C:close' )
-declare(os.getcwd, str , ll_os_getcwd, False, 'C:getcwd')
-declare(os.dup , int , ll_os_dup , True , 'C:dup' )
+declare(os.open , int , ll_os_open , True ) #this is not annotatable actually, but llvm has an issue
+declare(os.read , str , ll_os_read , True )
+declare(os.write , int , ll_os_write , True )
+declare(os.close , lambda a: None, ll_os_close , True , 'C:close' )
+declare(os.getcwd , str , ll_os_getcwd , True )
+declare(os.dup , int , ll_os_dup , True , 'C:dup' )
+declare(time.time , float , ll_time_time , True )
+declare(time.clock, float , ll_time_clock, True )
+declare(time.sleep, lambda a: None, ll_time_sleep, True )
Modified: pypy/dist/pypy/translator/llvm2/extfunction.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/extfunction.py (original)
+++ pypy/dist/pypy/translator/llvm2/extfunction.py Wed Jul 6 20:26:18 2005
@@ -1,7 +1,34 @@
extdeclarations = """; External declarations
+
+; XXX these int's might need to be long's on 64 bit CPU's :(
+
+declare int %time(int*) ;void* actually
+declare int %clock()
+declare void %sleep(int)
+
; End of external declarations
"""
extfunctions = """; External functions (will be inlined by LLVM)
+
+double %ll_time_time() {
+ %v0 = call int %time(int* null)
+ %v1 = cast int %v0 to double
+ ret double %v1
+}
+
+double %ll_time_clock() {
+ %v0 = call int %clock()
+ %v1 = cast int %v0 to double
+ %v2 = div double %v1, 1000000.0 ;CLOCKS_PER_SEC
+ ret double %v2
+}
+
+void %ll_time_sleep__Float(double %f) {
+ %i = cast double %f to int
+ call void %sleep(int %i)
+ ret void
+}
+
; End of external functions
"""
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 Wed Jul 6 20:26:18 2005
@@ -58,6 +58,33 @@
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")
More information about the Pypy-commit
mailing list