[pypy-svn] r34054 - in pypy/dist/pypy/jit/codegen/llvm: . lib test
ericvrp at codespeak.net
ericvrp at codespeak.net
Thu Nov 2 11:38:28 CET 2006
Author: ericvrp
Date: Thu Nov 2 11:38:26 2006
New Revision: 34054
Modified:
pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.cpp
pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.h
pypy/dist/pypy/jit/codegen/llvm/llvmjit.py
pypy/dist/pypy/jit/codegen/llvm/test/test_llvmjit.py
Log:
Add inmemory parsing of llvm sourcecode. next: remove the parsing of code from disc.
Modified: pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.cpp
==============================================================================
--- pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.cpp (original)
+++ pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.cpp Thu Nov 2 11:38:26 2006
@@ -23,16 +23,13 @@
ExecutionEngine* g_execution_engine;
-int compile(const char* filename) {
- std::string inputfile(filename);
+void restart() {
+ delete g_execution_engine;
+ g_execution_engine = NULL;
+}
- //from llvm-as.cpp
- Module* module(ParseAssemblyFile(inputfile + ".ll"));
- if (!module) {
- std::cerr << "Error: can not parse " << inputfile << ".ll\n" << std::flush;
- return false;
- }
+int add_module_to_execution_engine(Module* module) {
//std::ostream *Out = new std::ofstream((inputfile + ".bc").c_str(),
// std::ios::out | std::ios::trunc | std::ios::binary);
//WriteBytecodeToFile(module, *Out); //XXX what to do with the 3rd param (NoCompress)?
@@ -48,6 +45,30 @@
}
+int compile(const char* filename) {
+ std::string inputfile(filename);
+
+ Module* module(ParseAssemblyFile(inputfile + ".ll"));
+ if (!module) {
+ std::cerr << "Error: can not parse " << inputfile << ".ll\n" << std::flush;
+ return false;
+ }
+
+ return add_module_to_execution_engine(module);
+}
+
+
+int compile_src(const char* src) {
+ Module* module = ParseAssemblyString(src, new Module("llvmjit"));
+ if (!module) {
+ std::cerr << "Error: can not parse " << src << "\n" << std::flush;
+ return false;
+ }
+
+ return add_module_to_execution_engine(module);
+}
+
+
int execute(const char* funcname, int param) { //currently compiled=Module
int err = -1;
@@ -68,3 +89,4 @@
GenericValue gv = g_execution_engine->runFunction(func, args);
return gv.IntVal;
}
+
Modified: pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.h
==============================================================================
--- pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.h (original)
+++ pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.h Thu Nov 2 11:38:26 2006
@@ -4,8 +4,10 @@
extern "C" {
#endif
-int compile(const char* filename);
-int execute(const char* funcname, int param);
+void restart();
+int compile(const char* filename);
+int compile_src(const char* src);
+int execute(const char* funcname, int param);
#ifdef __cplusplus
}
Modified: pypy/dist/pypy/jit/codegen/llvm/llvmjit.py
==============================================================================
--- pypy/dist/pypy/jit/codegen/llvm/llvmjit.py (original)
+++ pypy/dist/pypy/jit/codegen/llvm/llvmjit.py Thu Nov 2 11:38:26 2006
@@ -21,10 +21,16 @@
llvmjit._FuncPtr = _FuncPtr
#exposed functions...
+restart = llvmjit.restart
+
compile = llvmjit.compile
compile.restype = c_int
compile.argtypes = [c_char_p]
+compile_src = llvmjit.compile_src
+compile_src.restype = c_int
+compile_src.argtypes = [c_char_p]
+
execute = llvmjit.execute
execute.restype = c_int
execute.argtypes = [c_char_p, c_int]
Modified: pypy/dist/pypy/jit/codegen/llvm/test/test_llvmjit.py
==============================================================================
--- pypy/dist/pypy/jit/codegen/llvm/test/test_llvmjit.py (original)
+++ pypy/dist/pypy/jit/codegen/llvm/test/test_llvmjit.py Thu Nov 2 11:38:26 2006
@@ -7,15 +7,38 @@
except OSError:
py.test.skip("libllvmjit not found (see ../README.TXT)")
+#helper data
curdir = dirname(__file__)
square = join(curdir, 'square')
mul2 = join(curdir, 'mul2')
+square_src = '''int %square(int %n) {
+block0:
+ %n2 = mul int %n, %n
+ ret int %n2
+}'''
+
+mul2_src = '''int %mul2(int %n) {
+block0:
+ %n2 = mul int %n, 2
+ ret int %n2
+}'''
+
+#helpers
def execute(filename, funcname, param):
assert llvmjit.compile(filename)
return llvmjit.execute(funcname, param)
-def test_execute_compile():
+def execute_src(src, funcname, param):
+ assert llvmjit.compile_src(src)
+ return llvmjit.execute(funcname, param)
+
+#tests...
+def test_restart():
+ llvmjit.restart()
+
+def test_execute_translation():
+ llvmjit.restart()
def f(x):
return execute(square, 'square', x + 5)
fn = compile(f, [int])
@@ -23,18 +46,37 @@
assert res == 36
def test_compile():
+ llvmjit.restart()
assert llvmjit.compile(square)
-def test_compiled():
+def test_execute():
+ llvmjit.restart()
assert execute(square, 'square', 4) == 4 * 4
-def test_compiled2():
+def test_execute_multiple():
+ llvmjit.restart()
llvmjit.compile(square)
llvmjit.compile(mul2)
for i in range(5):
assert llvmjit.execute('square', i) == i * i
assert llvmjit.execute('mul2', i) == i * 2
+def test_compile_src():
+ llvmjit.restart()
+ assert llvmjit.compile_src(square_src)
+
+def test_execute_src():
+ llvmjit.restart()
+ assert execute_src(square_src, 'square', 4) == 4 * 4
+
+def test_execute_multiple_src():
+ llvmjit.restart()
+ llvmjit.compile_src(square_src)
+ llvmjit.compile_src(mul2_src)
+ for i in range(5):
+ assert llvmjit.execute('square', i) == i * i
+ assert llvmjit.execute('mul2', i) == i * 2
+
def DONTtest_execute_accross_module():
pass
@@ -56,6 +98,3 @@
def DONTtest_layers_of_codegenerators(): #e.g. i386 code until function stabilizes then llvm
pass
-def DONTtest_inmemory_ll_and_bc_files():
- pass
-
More information about the Pypy-commit
mailing list