[pypy-svn] r34708 - in pypy/dist/pypy/jit/codegen/llvm: . lib test
ericvrp at codespeak.net
ericvrp at codespeak.net
Fri Nov 17 15:46:20 CET 2006
Author: ericvrp
Date: Fri Nov 17 15:46:17 2006
New Revision: 34708
Modified:
pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.cpp
pypy/dist/pypy/jit/codegen/llvm/llvmjit.py
pypy/dist/pypy/jit/codegen/llvm/test/test_llvmjit.py
Log:
pushed the llvm jit codegenerator a little...
* made call across sources testcase work
(by having execution context use a single module)
* removed some helper that would not translate
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 Fri Nov 17 15:46:17 2006
@@ -20,59 +20,45 @@
using namespace llvm;
-ExecutionEngine* g_execution_engine;
+Module* gp_module = new Module("llvmjit");
+ExecutionEngine* gp_execution_engine = ExecutionEngine::create(
+ new ExistingModuleProvider(gp_module), false);
void restart() {
- delete g_execution_engine; //XXX test if this correctly cleans up including generated code
- g_execution_engine = NULL;
+ delete gp_execution_engine; //XXX test if this correctly cleans up including generated code
+
+ gp_module = new Module("llvmjit");
+ gp_execution_engine = ExecutionEngine::create(new ExistingModuleProvider(gp_module), false);
}
int compile(const char* llsource) {
- Module* module = ParseAssemblyString(llsource, new Module("llvmjit"));
+ Module* module = ParseAssemblyString(llsource, gp_module);
if (!module) {
- std::cerr << "Error: can not parse " << llsource << "\n" << std::flush;
+ std::cerr << "Can not parse:\n" << llsource << "\n" << std::flush;
return false;
}
- //std::ostream *Out = new std::ofstream("temp-libllvmjit.bc",
- // std::ios::out | std::ios::trunc | std::ios::binary);
- //WriteBytecodeToFile(module, *Out); //XXX what to do with the 3rd param (NoCompress)?
-
- ModuleProvider* module_provider = new ExistingModuleProvider(module);
- if (!g_execution_engine) {
- g_execution_engine = ExecutionEngine::create(module_provider, false);
- } else {
- g_execution_engine->addModuleProvider(module_provider);
- }
-
return true;
}
void* find_function(const char* name) {
- if (!g_execution_engine) return NULL; //note: decided not to be treated as an error
-
- return g_execution_engine->FindFunctionNamed(name); //note: can be NULL
+ return gp_execution_engine->FindFunctionNamed(name); //note: can be NULL
}
int execute(const void* function, int param) { //XXX allow different function signatures
- if (!g_execution_engine) {
- std::cerr << "Error: no llvm code compiled yet!\n" << std::flush;
- return -1;
- }
-
if (!function) {
- std::cerr << "Error: no function supplied to libllvmjit.execute(...)\n" << std::flush;
+ std::cerr << "No function supplied to libllvmjit.execute(...)\n" << std::flush;
return -1;
}
std::vector<GenericValue> args;
args.push_back((void*)param);
- GenericValue gv = g_execution_engine->runFunction((Function*)function, args);
+ GenericValue gv = gp_execution_engine->runFunction((Function*)function, args);
return gv.IntVal;
}
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 Fri Nov 17 15:46:17 2006
@@ -56,11 +56,3 @@
execute.restype = c_int
execute.argtypes = [c_void_p, c_int]
-#helpers...
-class FindFunction(object):
- def __init__(self, function_name):
- self.function = find_function(function_name)
-
- def __call__(self, param): #XXX this does not seem to translate, how to do it instead?
- return execute(self.function, param)
-
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 Fri Nov 17 15:46:17 2006
@@ -27,7 +27,9 @@
ret int %n2
}'''
-llacross1 = '''declare int %across2(int %n2)
+llacross1 = '''declare int %across2(int)
+
+implementation
int %across1(int %n) {
block0:
@@ -42,7 +44,9 @@
ret int %n3
}'''
-llacross2 = '''declare int %across1(int %n2)
+llacross2 = '''declare int %across1(int %dsf)
+
+implementation
int %across2(int %n) {
block0:
@@ -60,10 +64,9 @@
#helpers
def execute(llsource, function_name, param):
assert llvmjit.compile(llsource)
- f = llvmjit.FindFunction(function_name)
- assert f.function
- return llvmjit.execute(f.function, param)
- #return function(param) #XXX this does not seem to translate, how to do it instead?
+ function = llvmjit.find_function(function_name)
+ assert function
+ return llvmjit.execute(function, param)
#tests...
def test_restart():
@@ -90,6 +93,10 @@
llvmjit.restart()
assert execute(llsquare, 'square', 4) == 4 * 4
+def test_execute_nothing():
+ llvmjit.restart()
+ assert llvmjit.execute(None, 4) == -1 #-1 == no function supplied
+
def test_execute_multiple():
llvmjit.restart()
llvmjit.compile(llsquare)
@@ -104,13 +111,13 @@
llvmjit.restart()
llvmjit.compile(llsquare)
llvmjit.compile(llmul2)
- square = llvmjit.FindFunction('square')
- mul2 = llvmjit.FindFunction('mul2')
+ square = llvmjit.find_function('square')
+ mul2 = llvmjit.find_function('mul2')
for i in range(5):
- assert square(i) == i * i
- assert mul2(i) == i * 2
+ assert llvmjit.execute(square, i) == i * i
+ assert llvmjit.execute(mul2 , i) == i * 2
-def DONTtest_execute_across_module():
+def test_execute_across_module():
def my_across1(n):
return n * 3
@@ -126,11 +133,11 @@
llvmjit.restart()
llvmjit.compile(llacross1)
llvmjit.compile(llacross2)
- across1to2 = llvmjit.FindFunction('across1to2')
- across2to1 = llvmjit.FindFunction('across2to1')
+ across1to2 = llvmjit.find_function('across1to2')
+ across2to1 = llvmjit.find_function('across2to1')
for i in range(5):
- assert across1to2(i) == my_across1to2(i)
- assert across2to1(i) == my_across2to1(i)
+ assert llvmjit.execute(across1to2, i) == my_across1to2(i)
+ assert llvmjit.execute(across2to1, i) == my_across2to1(i)
def DONTtest_modify_global_data():
More information about the Pypy-commit
mailing list