Commit 0a808fc9 authored by John Zhang's avatar John Zhang

Refactored tests.

parent 8080b7a6
from test_milestones import get_fncptr from util import fncptr_from_c_script
def test_add(): def test_add():
fn = get_fncptr("test_add", "test_fnc") fn, _ = fncptr_from_c_script("test_add.c", "test_fnc")
assert fn() == 9 assert fn() == 9
def test_sub(): def test_sub():
fn = get_fncptr("test_sub", "test_fnc") fn, _ = fncptr_from_c_script("test_sub.c", "test_fnc")
assert fn() == 11 assert fn() == 11
def test_mul(): def test_mul():
fn = get_fncptr("test_mul", "test_fnc") fn, _ = fncptr_from_c_script("test_mul.c", "test_fnc")
assert fn() == 0xf6 assert fn() == 0xf6
def test_sdiv(): def test_sdiv():
fn = get_fncptr("test_sdiv", "test_fnc") fn, _ = fncptr_from_c_script("test_sdiv.c", "test_fnc")
assert fn() == 0xf4 assert fn() == 0xf4
def test_urem(): def test_urem():
fn = get_fncptr("test_urem", "test_fnc") fn, _ = fncptr_from_c_script("test_urem.c", "test_fnc")
assert fn() == 5 assert fn() == 5
def test_shl(): def test_shl():
fn = get_fncptr("test_shl", "test_fnc") fn, _ = fncptr_from_c_script("test_shl.c", "test_fnc")
assert fn() == 0x7e707560c92d5400 assert fn() == 0x7e707560c92d5400
def test_lshr(): def test_lshr():
fn = get_fncptr("test_lshr", "test_fnc") fn, _ = fncptr_from_c_script("test_lshr.c", "test_fnc")
assert fn() == 0x2367e707560c92 assert fn() == 0x2367e707560c92
def test_and(): def test_and():
fn = get_fncptr("test_and", "test_fnc") fn, _ = fncptr_from_c_script("test_and.c", "test_fnc")
assert fn() == 0x8588901c10004b14 assert fn() == 0x8588901c10004b14
def test_xor(): def test_xor():
fn = get_fncptr("test_xor", "test_fnc") fn, _ = fncptr_from_c_script("test_xor.c", "test_fnc")
assert fn() == 0x58376ec3e83fa0e1 assert fn() == 0x58376ec3e83fa0e1
from test_milestones import get_fncptr from util import fncptr_from_c_script
def test_eq_int(): def test_eq_int():
fn = get_fncptr("test_eq_int", "test_fnc") fn, _ = fncptr_from_c_script("test_eq_int.c", "test_fnc")
assert fn() == 0 assert fn() == 0
def test_eq_ref(): def test_eq_ref():
fn = get_fncptr("test_eq_ref", "test_fnc") fn, _ = fncptr_from_c_script("test_eq_ref.c", "test_fnc")
assert fn() == 0 assert fn() == 0
def test_ne_int(): def test_ne_int():
fn = get_fncptr("test_ne_int", "test_fnc") fn, _ = fncptr_from_c_script("test_ne_int.c", "test_fnc")
assert fn() == 1 assert fn() == 1
def test_ne_ref(): def test_ne_ref():
fn = get_fncptr("test_ne_ref", "test_fnc") fn, _ = fncptr_from_c_script("test_ne_ref.c", "test_fnc")
assert fn() == 1 assert fn() == 1
def test_sge(): def test_sge():
fn = get_fncptr("test_sge", "test_fnc") fn, _ = fncptr_from_c_script("test_sge.c", "test_fnc")
assert fn() == 1 assert fn() == 1
def test_sgt(): def test_sgt():
fn = get_fncptr("test_sgt", "test_fnc") fn, _ = fncptr_from_c_script("test_sgt.c", "test_fnc")
assert fn() == 0 assert fn() == 0
def test_sle(): def test_sle():
fn = get_fncptr("test_sle", "test_fnc") fn, _ = fncptr_from_c_script("test_sle.c", "test_fnc")
assert fn() == 1 assert fn() == 1
def test_slt(): def test_slt():
fn = get_fncptr("test_slt", "test_fnc") fn, _ = fncptr_from_c_script("test_slt.c", "test_fnc")
assert fn() == 0 assert fn() == 0
def test_ult(): def test_ult():
fn = get_fncptr("test_ult", "test_fnc") fn, _ = fncptr_from_c_script("test_ult.c", "test_fnc")
assert fn() == 0 assert fn() == 0
from test_milestones import get_fncptr, compile_lib from util import fncptr_from_c_script
import ctypes, ctypes.util import ctypes, ctypes.util
def test_branch(): def test_branch():
fn = get_fncptr("test_branch", "test_fnc") fn, _ = fncptr_from_c_script("test_branch.c", "test_fnc")
assert fn() == 30 assert fn() == 30
def test_branch2(): def test_branch2():
fn = get_fncptr("test_branch2", "test_fnc", [ctypes.c_byte]) fn, _ = fncptr_from_c_script("test_branch2.c", "test_fnc", [ctypes.c_byte])
assert fn(1) == 30 assert fn(1) == 30
assert fn(0) == 200 assert fn(0) == 200
def test_ccall(): def test_ccall():
fn = get_fncptr("test_ccall", "test_ccall", [ctypes.c_ulonglong]) fn, _ = fncptr_from_c_script("test_ccall.c", "test_ccall", [ctypes.c_ulonglong])
assert fn(0x7e707560c92d5400) == 0x7e707560c92d5400 assert fn(0x7e707560c92d5400) == 0x7e707560c92d5400
def test_extern_func(): def test_extern_func():
# libc = ctypes.CDLL(ctypes.util.find_library('c'), ctypes.RTLD_GLOBAL) # libc = ctypes.CDLL(ctypes.util.find_library('c'), ctypes.RTLD_GLOBAL)
fn = get_fncptr("test_extern_func", "test_write", [ctypes.c_void_p, ctypes.c_size_t], ctypes.c_int64) fn, _ = fncptr_from_c_script("test_extern_func.c", "test_write", [ctypes.c_void_p, ctypes.c_size_t], ctypes.c_int64)
buf = ctypes.create_string_buffer('hello world!\n') buf = ctypes.create_string_buffer('hello world!\n')
assert fn(ctypes.byref(buf), len(buf)) == len(buf) assert fn(ctypes.byref(buf), len(buf)) == len(buf)
from test_milestones import get_fncptr from util import fncptr_from_c_script
def test_trunc(): def test_trunc():
fn = get_fncptr("test_trunc", "test_fnc") fn, _ = fncptr_from_c_script("test_trunc.c", "test_fnc")
assert fn() == 0x58324b55 assert fn() == 0x58324b55
def test_sext(): def test_sext():
fn = get_fncptr("test_sext", "test_fnc") fn, _ = fncptr_from_c_script("test_sext.c", "test_fnc")
assert fn() == 0xffffffffa8324b55 assert fn() == 0xffffffffa8324b55
def test_zext(): def test_zext():
fn = get_fncptr("test_zext", "test_fnc") fn, _ = fncptr_from_c_script("test_zext.c", "test_fnc")
assert fn() == 0x00000000a8324b55 assert fn() == 0x00000000a8324b55
from test_milestones import get_fncptr from util import fncptr_from_c_script
import ctypes import ctypes
def test_uptr_bytestore_load(): def test_uptr_bytestore_load():
fn = get_fncptr("test_uptr_bytestore_load", "test_fnc", fn, _ = fncptr_from_c_script("test_uptr_bytestore_load.c", "test_fnc",
argtypes=[ctypes.POINTER(ctypes.c_uint32)], argtypes=[ctypes.POINTER(ctypes.c_uint32)],
restype=ctypes.c_uint32) restype=ctypes.c_uint32)
......
""" """
Harness JIT tests using py.test framework Harness JIT tests using py.test framework
""" """
import subprocess as subp from util import fncptr_from_c_script
import os, sys
import ctypes import ctypes
import py
CC = os.environ.get('CC', 'clang')
proj_dir = py.path.local(__file__).join('..', '..', '..')
test_jit_dir = proj_dir.join('tests', 'test_jit')
testsuite_dir = test_jit_dir.join('suite')
bin_dir = test_jit_dir.join('temp')
if not bin_dir.exists():
bin_dir.mkdir()
def compile_lib(testname):
src_c = testsuite_dir.join(testname + '.c')
bin_path = bin_dir.join(testname)
CFLAGS = [
"-std=c99",
"-I%(proj_dir)s/src/vm/api" % globals(),
"-L%(proj_dir)s/target/debug" % globals(),
"-lmu",
]
cmd = [CC] + CFLAGS + ['-o', str(bin_path)] + [str(src_c)]
# compile
p = subp.Popen(cmd, stdout=subp.PIPE, stderr=subp.PIPE, env=os.environ)
out, err = p.communicate()
if p.returncode != 0: # failed
sys.stdout.write(out + '\n')
sys.stderr.write(err + '\n')
raise subp.CalledProcessError(p.returncode, cmd)
os.environ['LD_LIBRARY_PATH'] = "%s:%s" % ("%(proj_dir)s/target/debug" % globals(),
os.environ['LD_LIBRARY_PATH'] if 'LD_LIBRARY_PATH' in os.environ else "")
# run
p = subp.Popen([str(bin_path)], stdout=subp.PIPE, stderr=subp.PIPE, cwd=str(bin_dir), env=os.environ)
out, err = p.communicate()
if p.returncode != 0: # failed
sys.stdout.write(out + '\n')
sys.stderr.write(err + '\n')
raise subp.CalledProcessError(p.returncode, bin_path)
return str(bin_dir.join('emit', testname + '.dylib'))
def get_fncptr(testname, entry_fnc, argtypes=[], restype=ctypes.c_ulonglong):
lib_path = compile_lib(testname)
lib = ctypes.CDLL(lib_path)
fncptr = getattr(lib, entry_fnc)
fncptr.restype = restype
fncptr.argtypes = argtypes
return fncptr
def test_constant_function(): def test_constant_function():
fn = get_fncptr("test_constfunc", 'test_fnc') fn, _ = fncptr_from_c_script("test_constfunc.c", 'test_fnc')
assert fn() == 0 assert fn() == 0
def test_milsum(): def test_milsum():
fn = get_fncptr("test_milsum", "milsum", [ctypes.c_ulonglong]) fn, _ = fncptr_from_c_script("test_milsum.c", "milsum", [ctypes.c_ulonglong])
assert fn(1000000) == 500000500000 assert fn(1000000) == 500000500000
def test_factorial(): def test_factorial():
fn = get_fncptr("test_fac", "fac", [ctypes.c_ulonglong]) fn, _ = fncptr_from_c_script("test_fac.c", "fac", [ctypes.c_ulonglong])
assert fn(20) == 2432902008176640000 assert fn(20) == 2432902008176640000
def test_fibonacci(): def test_fibonacci():
fn = get_fncptr("test_fib", "fib", [ctypes.c_ulonglong]) fn, _ = fncptr_from_c_script("test_fib.c", "fib", [ctypes.c_ulonglong])
assert fn(20) == 6765 assert fn(20) == 6765
def test_multifunc(): def test_multifunc():
fn = get_fncptr("test_multifunc", "entry") fn, _ = fncptr_from_c_script("test_multifunc.c", "entry")
assert fn() == 6765 assert fn() == 6765
from rpython.rtyper.lltypesystem import rffi, lltype from rpython.rtyper.lltypesystem import rffi, lltype
from rpython.rlib import rmu_fast as rmu from rpython.rlib import rmu_fast as rmu
from rpython.translator.interactive import Translation from util import fncptr_from_rpy_func, fncptr_from_py_script
import ctypes, sys import ctypes
from test_milestones import proj_dir
def getfncptr(entry_fnc, argtypes, **kwargs):
kwargs.setdefault('backend', 'mu')
kwargs.setdefault('muimpl', 'fast')
kwargs.setdefault('mucodegen', 'api')
kwargs.setdefault('mutestjit', True)
t = Translation(entry_fnc, argtypes, **kwargs)
if kwargs['backend'] == 'mu':
db, bdlgen, fnc_name = t.compile_mu()
bdlgen.mu.compile_to_sharedlib('libtesting.dylib', [])
lib = ctypes.CDLL('emit/libtesting.dylib')
fnp = getattr(lib, fnc_name)
return fnp
else:
libpath = t.compile_c()
return getattr(ctypes.CDLL(libpath.strpath), 'pypy_g_' + entry_fnc.__name__)
def test_add(): def test_add():
def add(a, b): def add(a, b):
return a + b return a + b
fn = getfncptr(add, [rffi.LONGLONG, rffi.LONGLONG]) fn, _ = fncptr_from_rpy_func(add, [rffi.LONGLONG, rffi.LONGLONG], rffi.LONGLONG)
assert fn(1, 2) == 3 assert fn(1, 2) == 3
...@@ -41,10 +21,13 @@ def test_find_min(): ...@@ -41,10 +21,13 @@ def test_find_min():
m = x m = x
return m return m
fnc = getfncptr(find_min, [rffi.CArrayPtr(rffi.LONGLONG), rffi.UINTPTR_T]) fnc, _ = fncptr_from_rpy_func(find_min, [rffi.CArrayPtr(rffi.LONGLONG), rffi.INTPTR_T], rffi.LONGLONG)
arr = (ctypes.c_longlong * 5)(23, 100, 0, 78, -5) with lltype.scoped_alloc(rffi.CArray(rffi.LONGLONG), 5) as arr:
assert fnc(ctypes.byref(arr), 5) == -5 lst = [23, 100, 0, 78, -5]
for i, n in enumerate(lst):
arr[i] = n
assert fnc(arr, 5) == -5
def rand_array_of(n): def rand_array_of(n):
...@@ -68,9 +51,7 @@ def test_arraysum(): ...@@ -68,9 +51,7 @@ def test_arraysum():
sum += arr[i] sum += arr[i]
return sum return sum
fnc = getfncptr(arraysum, [rffi.CArrayPtr(rffi.LONGLONG), rffi.SIZE_T]) fnc, _ = fncptr_from_rpy_func(arraysum, [rffi.CArrayPtr(rffi.LONGLONG), rffi.SIZE_T], rffi.LONGLONG)
# fnc = getfncptr(arraysum, [rffi.CArrayPtr(rffi.LONGLONG), rffi.SIZE_T],
# backend='c', jit=False, gc='none')
n = 1000000 n = 1000000
arr, lst = rand_array_of(n) arr, lst = rand_array_of(n)
...@@ -106,9 +87,7 @@ def test_quicksort(): ...@@ -106,9 +87,7 @@ def test_quicksort():
quicksort(arr, start, p - 1) quicksort(arr, start, p - 1)
quicksort(arr, p + 1, end) quicksort(arr, p + 1, end)
# fnc = getfncptr(quicksort, [rffi.CArrayPtr(rffi.LONGLONG), rffi.UINTPTR_T, rffi.UINTPTR_T], fnc, _ = fncptr_from_rpy_func(quicksort, [rffi.CArrayPtr(rffi.LONGLONG), rffi.UINTPTR_T, rffi.UINTPTR_T], lltype.Void)
# backend='c', jit=False, gc='none')
fnc = getfncptr(quicksort, [rffi.CArrayPtr(rffi.LONGLONG), rffi.UINTPTR_T, rffi.UINTPTR_T])
n = 1000000 n = 1000000
arr, lst = rand_array_of(n) arr, lst = rand_array_of(n)
...@@ -123,7 +102,7 @@ def test_quicksort(): ...@@ -123,7 +102,7 @@ def test_quicksort():
lst_s = sorted(lst) lst_s = sorted(lst)
for i in range(n): for i in range(n):
assert lst_s[i] == arr[i], "%d != %d" % (lst_s[i], arr[i]) assert lst_s[i] == arr[i], "%d != %d" % (lst_s[i], arr[i])
# +(+(uptr<int<64>> %find_min_0.blk2.rtn_5 #257) = SHIFTIREF PTR +(+(uptr<@hybSigned_0(hybrid)> %find_min_0.blk2.rtn_4 #255) = GETVARPARTIREF PTR +(uptr<@hybSigned_0(hybrid)> %find_min_0.blk2.xs_2 #251)) +(int<64> %find_min_0.blk2.next_0 #249)))
def test_linkedlist_reversal(): def test_linkedlist_reversal():
def reverse_linkedlist(head): def reverse_linkedlist(head):
...@@ -141,15 +120,7 @@ def test_linkedlist_reversal(): ...@@ -141,15 +120,7 @@ def test_linkedlist_reversal():
NodePtr = lltype.Ptr(Node) NodePtr = lltype.Ptr(Node)
Node.become(lltype.Struct("Node", ('val', rffi.CHAR), ('nxt', NodePtr))) Node.become(lltype.Struct("Node", ('val', rffi.CHAR), ('nxt', NodePtr)))
# RPython RFFI fnc, _ = fncptr_from_rpy_func(reverse_linkedlist, [NodePtr], NodePtr)
t = Translation(reverse_linkedlist, [NodePtr],
backend='mu', muimpl='fast', mucodegen='api', mutestjit=True)
db, bdlgen, fnc_name = t.compile_mu()
bdlgen.mu.compile_to_sharedlib('libtesting.dylib', [])
c_fnc = rffi.llexternal('reverse_linkedlist', [NodePtr], NodePtr,
compilation_info=rffi.ExternalCompilationInfo(libraries=['libtesting.dylib'],
library_dirs=['emit']), _nowrapper=True)
# linked list: a -> b -> c -> d # linked list: a -> b -> c -> d
with lltype.scoped_alloc(Node) as a: with lltype.scoped_alloc(Node) as a:
...@@ -165,49 +136,13 @@ def test_linkedlist_reversal(): ...@@ -165,49 +136,13 @@ def test_linkedlist_reversal():
d.val = 'd' d.val = 'd'
d.nxt = lltype.nullptr(Node) d.nxt = lltype.nullptr(Node)
h = c_fnc(a) h = fnc(a)
assert h.val == 'd' assert h.val == 'd'
assert h.nxt.val == 'c' assert h.nxt.val == 'c'
assert h.nxt.nxt.val == 'b' assert h.nxt.nxt.val == 'b'
assert h.nxt.nxt.nxt.val == 'a' assert h.nxt.nxt.nxt.val == 'a'
assert h.nxt.nxt.nxt.nxt == lltype.nullptr(Node) assert h.nxt.nxt.nxt.nxt == lltype.nullptr(Node)
# # ctypes
# fnc = getfncptr(reverse_linkedlist, [NodePtr])
#
# class cNode(ctypes.Structure):
# pass
# cNodePtr = ctypes.POINTER(cNode)
# cNode._fields_ = [('val', ctypes.c_char),
# ('nxt', cNodePtr)]
#
# # fnc.argtypes = [cNodePtr]
# # fnc.restype = [cNodePtr]
#
# # ctypes
# # linked list: a -> b -> c -> d
# a = cNode()
# a.val = 'a'
# b = cNode()
# b.val = 'b'
# a.nxt = ctypes.pointer(b)
# c = cNode()
# c.val = 'c'
# b.nxt = ctypes.pointer(c)
# d = cNode()
# d.val = 'd'
# c.nxt = ctypes.pointer(d)
# d.nxt = None
#
# h = fnc(a)
#
# assert h.val == 'd'
# assert h.nxt.val == 'c'
# assert h.nxt.nxt.val == 'b'
# assert h.nxt.nxt.nxt.val == 'a'
# assert h.nxt.nxt.nxt.nxt == None
def test_threadtran_fib(): def test_threadtran_fib():
def build_test_bundle(bldr, rmu): def build_test_bundle(bldr, rmu):
""" """
...@@ -307,16 +242,7 @@ def test_threadtran_fib(): ...@@ -307,16 +242,7 @@ def test_threadtran_fib():
"result_type": i64 "result_type": i64
} }
mu = rmu.MuVM() fnp, (mu, ctx, bldr) = fncptr_from_py_script(build_test_bundle, 'fib', [ctypes.c_longlong])
ctx = mu.new_context()
bldr = ctx.new_ir_builder()
id_dict = build_test_bundle(bldr, rmu)
bldr.load()
mu.compile_to_sharedlib('libtesting.dylib', [])
lib = ctypes.CDLL('emit/libtesting.dylib')
fnp = lib.fib
mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr)) mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr))
assert fnp(20) == 6765 assert fnp(20) == 6765
...@@ -386,19 +312,10 @@ def test_new(): ...@@ -386,19 +312,10 @@ def test_new():
} }
# load libmu before rffi so to load it with RTLD_GLOBAL # load libmu before rffi so to load it with RTLD_GLOBAL
libmu = ctypes.CDLL(proj_dir.join('target', 'debug', 'libmu.dylib').strpath, ctypes.RTLD_GLOBAL) from util import libmu_path
libmu = ctypes.CDLL(libmu_path.strpath, ctypes.RTLD_GLOBAL)
mu = rmu.MuVM()
ctx = mu.new_context()
bldr = ctx.new_ir_builder()
id_dict = build_test_bundle(bldr, rmu)
bldr.load()
mu.compile_to_sharedlib('libtesting.dylib', [])
fnp, (mu, ctx, bldr) = fncptr_from_py_script(build_test_bundle, 'test_fnc')
lib = ctypes.CDLL('emit/libtesting.dylib')
fnp = lib.test_fnc
mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr)) mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr))
assert fnp() == 1 assert fnp() == 1
...@@ -464,18 +381,10 @@ def test_new_cmpeq(): ...@@ -464,18 +381,10 @@ def test_new_cmpeq():
} }
# load libmu before rffi so to load it with RTLD_GLOBAL # load libmu before rffi so to load it with RTLD_GLOBAL
libmu = ctypes.CDLL(proj_dir.join('target', 'debug', 'libmu.dylib').strpath, ctypes.RTLD_GLOBAL) from util import libmu_path
libmu = ctypes.CDLL(libmu_path.strpath, ctypes.RTLD_GLOBAL)
mu = rmu.MuVM()
ctx = mu.new_context()
bldr = ctx.new_ir_builder()
id_dict = build_test_bundle(bldr, rmu)
bldr.load()
mu.compile_to_sharedlib('libtesting.dylib', [])
lib = ctypes.CDLL('emit/libtesting.dylib') fnp, (mu, ctx, bldr) = fncptr_from_py_script(build_test_bundle, 'test_fnc')
fnp = lib.test_fnc
mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr)) mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr))
assert fnp() == 0 assert fnp() == 0
......
import subprocess as subp
import os, sys
import ctypes
import py
CC = os.environ.get('CC', 'clang')
proj_dir = py.path.local(__file__).join('..', '..', '..')
test_jit_dir = proj_dir.join('tests', 'test_jit')
testsuite_dir = test_jit_dir.join('suite')
bin_dir = test_jit_dir.join('temp')
if not bin_dir.exists():
bin_dir.mkdir()
libmu_path = proj_dir.join('target', 'debug', 'libmu.dylib')
def compile_c_script(c_src_name):
testname = c_src_name[:-2]
src_c = testsuite_dir.join(c_src_name)
bin_path = bin_dir.join(testname)
CFLAGS = [
"-std=c11",
"-I%(proj_dir)s/src/vm/api" % globals(),
"-L" + libmu_path.dirname,
"-lmu",
]
cmd = [CC] + CFLAGS + ['-o', bin_path.strpath] + [src_c.strpath]
# compile
p = subp.Popen(cmd, stdout=subp.PIPE, stderr=subp.PIPE, env=os.environ)
out, err = p.communicate()
if p.returncode != 0: # failed
sys.stdout.write(out + '\n')
sys.stderr.write(err + '\n')
raise subp.CalledProcessError(p.returncode, cmd)
os.environ['LD_LIBRARY_PATH'] = "%s:%s" % ("%(proj_dir)s/target/debug" % globals(),
os.environ['LD_LIBRARY_PATH'] if 'LD_LIBRARY_PATH' in os.environ else "")
# run
p = subp.Popen([bin_path.strpath], stdout=subp.PIPE, stderr=subp.PIPE, cwd=str(bin_dir), env=os.environ)
out, err = p.communicate()
if p.returncode != 0: # failed
sys.stdout.write(out + '\n')
sys.stderr.write(err + '\n')
raise subp.CalledProcessError(p.returncode, bin_path)
return bin_dir.join('emit', '%(testname)s.dylib' % locals()).strpath
def fncptr_from_lib(lib, fnc_name, argtypes=[], restype=ctypes.c_longlong):
fnp = getattr(lib, fnc_name)
fnp.argtypes = argtypes
fnp.restype = restype
return fnp
def fncptr_from_c_script(c_src_name, name, argtypes=[], restype=ctypes.c_ulonglong):
lib_path = compile_c_script(c_src_name)
lib = ctypes.CDLL(lib_path)
return fncptr_from_lib(lib, name, argtypes, restype), lib
def fncptr_from_py_script(py_fnc, name, argtypes=[], restype=ctypes.c_longlong):
# NOTE: requires mu-client-pypy
from rpython.rlib import rmu_fast as rmu
mu = rmu.MuVM()
ctx = mu.new_context()
bldr = ctx.new_ir_builder()
id_dict = py_fnc(bldr, rmu)
bldr.load()
libname = 'lib%(name)s.dylib' % locals()
mu.compile_to_sharedlib(libname, [])
lib = ctypes.CDLL('emit/%(libname)s' % locals())
return fncptr_from_lib(lib, name, argtypes, restype), (mu, ctx, bldr)
def fncptr_from_rpy_func(rpy_fnc, llargtypes, llrestype, **kwargs):
# NOTE: requires mu-client-pypy
from rpython.rtyper.lltypesystem import rffi
from rpython.translator.interactive import Translation
kwargs.setdefault('backend', 'mu')
kwargs.setdefault('muimpl', 'fast')
kwargs.setdefault('mucodegen', 'api')
kwargs.setdefault('mutestjit', True)
t = Translation(rpy_fnc, llargtypes, **kwargs)
if kwargs['backend'] == 'mu':
db, bdlgen, fnc_name = t.compile_mu()
libname = 'lib%(fnc_name)s.dylib' % locals()
bdlgen.mu.compile_to_sharedlib(libname, [])
eci = rffi.ExternalCompilationInfo(libraries=[test_jit_dir.join('emit', libname).strpath])
extras = (db, bdlgen)
else:
libpath = t.compile_c()
fnc_name = 'pypy_g_' + rpy_fnc.__name__
eci = rffi.ExternalCompilationInfo(libraries=[libpath.strpath])
extras = None
return rffi.llexternal(fnc_name, llargtypes, llrestype, compilation_info=eci, _nowrapper=True), extras
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment