[pypy-svn] r34027 - in pypy/dist/pypy/jit/codegen/llvm: . lib test
ericvrp at codespeak.net
ericvrp at codespeak.net
Wed Nov 1 15:28:49 CET 2006
Author: ericvrp
Date: Wed Nov 1 15:28:48 2006
New Revision: 34027
Added:
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:
(mwh, ericvrp) added libllvmjit functions compile&execute.
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 Wed Nov 1 15:28:48 2006
@@ -2,6 +2,57 @@
#include "libllvmjit.h"
+#include "llvm/Module.h"
+#include "llvm/Assembly/Parser.h"
+#include "llvm/Bytecode/Writer.h"
+#include "llvm/Analysis/Verifier.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/SystemUtils.h"
+#include "llvm/System/Signals.h"
+#include "llvm/ModuleProvider.h"
+#include "llvm/ExecutionEngine/JIT.h"
+#include "llvm/ExecutionEngine/Interpreter.h"
+#include "llvm/ExecutionEngine/GenericValue.h"
+#include <fstream>
+#include <iostream>
+#include <memory>
+
+using namespace llvm;
+
+
int testme(int n) {
return n * 2;
}
+
+
+void* compile(const char* filename) {
+ std::string inputfile(filename);
+
+ //from llvm-as.cpp
+ Module* module(ParseAssemblyFile(inputfile + ".ll"));
+ if (!module) return NULL;
+
+ 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)?
+
+ return module;
+}
+
+
+int execute(void* compiled, const char* funcname, int param) { //currently compiled=Module
+ Module* module = (Module*)compiled;
+ if (!module) {
+ std::cerr << "Error: can not execute " << funcname << " in a non existing module\n" << std::flush;
+ return -1;
+ }
+
+ ExistingModuleProvider* module_provider = new ExistingModuleProvider(module);
+ ExecutionEngine* EE = ExecutionEngine::create(module_provider, false);
+
+ std::vector<GenericValue> args;
+ args.push_back((void*)param);
+ GenericValue gv = EE->runFunction(module->getNamedFunction(funcname), 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 Wed Nov 1 15:28:48 2006
@@ -5,6 +5,8 @@
#endif
int testme(int n);
+void* compile(const char* filename);
+int execute(void* compiled, 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 Wed Nov 1 15:28:48 2006
@@ -8,18 +8,27 @@
'''
from pypy.rpython.rctypes import implementation
-import ctypes
+from ctypes import _CFuncPtr, _FUNCFLAG_CDECL
+from ctypes import *
import os
path = os.path.join(os.path.dirname(__file__), 'libllvmjit.so')
-llvmjit = ctypes.cdll.LoadLibrary(os.path.abspath(path))
-class _FuncPtr(ctypes._CFuncPtr):
- _flags_ = ctypes._FUNCFLAG_CDECL
+llvmjit = cdll.LoadLibrary(os.path.abspath(path))
+class _FuncPtr(_CFuncPtr):
+ _flags_ = _FUNCFLAG_CDECL
# aaarghdistutilsunixaaargh (may need something different for standalone builds...)
libraries = (os.path.join(os.path.dirname(path), 'llvmjit'),)
llvmjit._FuncPtr = _FuncPtr
-#impls
+#exposed functions...
testme = llvmjit.testme
-testme.restype = ctypes.c_int
-testme.argtypes = [ctypes.c_int]
+testme.restype = c_int
+testme.argtypes = [c_int]
+
+compile = llvmjit.compile
+compile.restype = c_void_p
+compile.argtypes = [c_char_p]
+
+execute = llvmjit.execute
+execute.restype = c_int
+execute.argtypes = [c_void_p, c_char_p, c_int]
Added: pypy/dist/pypy/jit/codegen/llvm/test/square.ll
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/jit/codegen/llvm/test/square.ll Wed Nov 1 15:28:48 2006
@@ -0,0 +1,5 @@
+int %square(int %n) {
+block0:
+ %n2 = mul int %n, %n
+ ret int %n2
+}
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 Wed Nov 1 15:28:48 2006
@@ -1,10 +1,14 @@
import py
+from os.path import dirname, join
from pypy.translator.c.test.test_genc import compile
try:
from pypy.jit.codegen.llvm import llvmjit
except OSError:
- py.test.skip("llvmjit library not found (see ../README.TXT)")
+ py.test.skip("libllvmjit not found (see ../README.TXT)")
+
+curdir = dirname(__file__)
+square = join(curdir, 'square')
def test_testme():
assert llvmjit.testme(10) == 20
@@ -15,3 +19,11 @@
fn = compile(f, [int])
res = fn(1)
assert res == 42
+
+def test_compile():
+ assert llvmjit.compile(square)
+
+def test_compiled():
+ compiled = llvmjit.compile(square)
+ assert llvmjit.execute(compiled, 'square', 4) == 4 * 4
+
More information about the pypy-svn
mailing list