[pypy-svn] r34055 - in pypy/dist/pypy/jit/codegen/llvm: . lib test

ericvrp at codespeak.net ericvrp at codespeak.net
Thu Nov 2 12:12:26 CET 2006


Author: ericvrp
Date: Thu Nov  2 12:12:22 2006
New Revision: 34055

Removed:
   pypy/dist/pypy/jit/codegen/llvm/test/mul2.ll
   pypy/dist/pypy/jit/codegen/llvm/test/square.ll
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:
* depricated llvm parsing/compiling from disc (inmemory only from now on)
* remove .ll sourcefiles
* added tests for llvmjit.restart and llvmjit.find_function



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 12:12:22 2006
@@ -23,14 +23,20 @@
 ExecutionEngine*    g_execution_engine;
 
 
-void restart() {
-    delete g_execution_engine;
+void    restart() {
+    delete g_execution_engine; //XXX test if this correctly cleans up including generated code
     g_execution_engine = NULL;
 }
 
 
-int add_module_to_execution_engine(Module* module) {
-    //std::ostream *Out = new std::ofstream((inputfile + ".bc").c_str(),
+int     compile(const char* llsource) {
+    Module*     module = ParseAssemblyString(llsource, new Module("llvmjit"));
+    if (!module) {
+        std::cerr << "Error: can not parse " << 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)?
 
@@ -45,48 +51,28 @@
 }
 
 
-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;
-    }
+void*   find_function(const char* name) {
+    if (!g_execution_engine)    return NULL; //note: decided not to be treated as an error
 
-    return add_module_to_execution_engine(module);
+    return g_execution_engine->FindFunctionNamed(name); //note: can be NULL
 }
 
 
-int execute(const char* funcname, int param) { //currently compiled=Module
-    int err = -1;
-
+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 err;
+        return -1;
+    }
+
+    if (!function) {
+        std::cerr << "Error: no function supplied to libllvmjit.execute(...)\n" << std::flush;
+        return -1;
     }
 
     std::vector<GenericValue> args;
     args.push_back((void*)param);
 
-    Function*   func = g_execution_engine->FindFunctionNamed(funcname);
-    if (!func) {
-        std::cerr << "Error: can not find function " << funcname << "\n" << std::flush;
-        return err;
-    }
-
-    GenericValue gv = g_execution_engine->runFunction(func, args);
+    GenericValue gv = g_execution_engine->runFunction((Function*)function, 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 12:12:22 2006
@@ -5,9 +5,9 @@
 #endif
 
 void    restart();
-int     compile(const char* filename);
-int     compile_src(const char* src);
-int     execute(const char* funcname, int param);
+int     compile(const char* llsource);
+void*   find_function(const char* funcname);
+int     execute(const void* function, 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 12:12:22 2006
@@ -27,10 +27,10 @@
 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]
+find_function = llvmjit.find_function
+find_function.restype  = c_void_p
+find_function.argtypes = [c_char_p]
 
 execute = llvmjit.execute
 execute.restype  = c_int
-execute.argtypes = [c_char_p, c_int]
+execute.argtypes = [c_void_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 12:12:22 2006
@@ -12,70 +12,67 @@
 square = join(curdir, 'square')
 mul2   = join(curdir, 'mul2')
 
-square_src = '''int %square(int %n) {
+llsquare = '''int %square(int %n) {
 block0:
     %n2 = mul int %n, %n
     ret int %n2
 }'''
 
-mul2_src = '''int %mul2(int %n) {
+llmul2 = '''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 execute_src(src, funcname, param):
-    assert llvmjit.compile_src(src)
-    return llvmjit.execute(funcname, param)
+def execute(llsource, function_name, param):
+    assert llvmjit.compile(llsource)
+    function = llvmjit.find_function(function_name)
+    assert function
+    return llvmjit.execute(function, param)
 
 #tests...
 def test_restart():
-    llvmjit.restart()
+    for i in range(3):
+        llvmjit.restart()
+        assert not llvmjit.find_function('square')
+        assert llvmjit.compile(llsquare)
+        assert llvmjit.find_function('square')
+
+def test_find_function():
+    for i in range(3):
+        llvmjit.restart()
+        assert not llvmjit.find_function('square')
+        assert not llvmjit.find_function('square')
+        assert llvmjit.compile(llsquare)
+        assert llvmjit.find_function('square')
+        assert llvmjit.find_function('square')
 
 def test_execute_translation():
     llvmjit.restart()
     def f(x):
-        return execute(square, 'square', x + 5)
+        return execute(llsquare, 'square', x + 5)
     fn = compile(f, [int])
     res = fn(1)
     assert res == 36
 
 def test_compile():
     llvmjit.restart()
-    assert llvmjit.compile(square)
+    assert llvmjit.compile(llsquare)
 
 def test_execute():
     llvmjit.restart()
-    assert execute(square, 'square', 4) == 4 * 4
+    assert execute(llsquare, 'square', 4) == 4 * 4
 
 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)
+    llvmjit.compile(llsquare)
+    llvmjit.compile(llmul2)
+    square = llvmjit.find_function('square')
+    mul2   = llvmjit.find_function('mul2')
     for i in range(5):
-        assert llvmjit.execute('square', i) == i * i
-        assert llvmjit.execute('mul2', i) == i * 2
+        assert llvmjit.execute(square, i) == i * i
+        assert llvmjit.execute(mul2  , i) == i * 2
 
 def DONTtest_execute_accross_module():
     pass
@@ -97,4 +94,3 @@
 
 def DONTtest_layers_of_codegenerators():    #e.g. i386 code until function stabilizes then llvm
     pass
-


More information about the pypy-svn mailing list