[pypy-svn] r25337 - in pypy/dist/pypy/translator/llvm: llvmcapi/include llvmcapi/include/ExecutionEngine llvmcapi/include/VMCore llvmcapi/lib llvmcapi/lib/ExecutionEngine llvmcapi/lib/VMCore pythonllvm pythonllvm/test
ericvrp at codespeak.net
ericvrp at codespeak.net
Wed Apr 5 09:15:34 CEST 2006
Author: ericvrp
Date: Wed Apr 5 09:15:29 2006
New Revision: 25337
Added:
pypy/dist/pypy/translator/llvm/llvmcapi/include/VMCore/Type.h
pypy/dist/pypy/translator/llvm/llvmcapi/lib/VMCore/Type.cpp (contents, props changed)
Modified:
pypy/dist/pypy/translator/llvm/llvmcapi/include/ExecutionEngine/ExecutionEngine.h
pypy/dist/pypy/translator/llvm/llvmcapi/include/ExecutionEngine/GenericValue.h
pypy/dist/pypy/translator/llvm/llvmcapi/include/llvmcapi.h
pypy/dist/pypy/translator/llvm/llvmcapi/lib/ExecutionEngine/ExecutionEngine.cpp
pypy/dist/pypy/translator/llvm/llvmcapi/lib/llvmcapi.cpp
pypy/dist/pypy/translator/llvm/pythonllvm/create_llvmcapi.py
pypy/dist/pypy/translator/llvm/pythonllvm/helper.py
pypy/dist/pypy/translator/llvm/pythonllvm/llvmcapi.py
pypy/dist/pypy/translator/llvm/pythonllvm/pyllvm.py
pypy/dist/pypy/translator/llvm/pythonllvm/test/test_ee.py
Log:
most pythonllvm tests passing again
Modified: pypy/dist/pypy/translator/llvm/llvmcapi/include/ExecutionEngine/ExecutionEngine.h
==============================================================================
--- pypy/dist/pypy/translator/llvm/llvmcapi/include/ExecutionEngine/ExecutionEngine.h (original)
+++ pypy/dist/pypy/translator/llvm/llvmcapi/include/ExecutionEngine/ExecutionEngine.h Wed Apr 5 09:15:29 2006
@@ -14,7 +14,7 @@
void ExecutionEngine_freeMachineCodeForFunction(void* EE, void* F);
// return union and takes std::vector<GenericValue> actually
-int ExecutionEngine_runFunction(void* EE, void* F, int args_vector);
+long long ExecutionEngine_runFunction(void* EE, void* F, void* A);
#ifdef __cplusplus
};
Modified: pypy/dist/pypy/translator/llvm/llvmcapi/include/ExecutionEngine/GenericValue.h
==============================================================================
--- pypy/dist/pypy/translator/llvm/llvmcapi/include/ExecutionEngine/GenericValue.h (original)
+++ pypy/dist/pypy/translator/llvm/llvmcapi/include/ExecutionEngine/GenericValue.h Wed Apr 5 09:15:29 2006
@@ -5,6 +5,35 @@
extern "C" {
#endif
+//#include "llvm/ExecutionEngine/GenericValue.h"
+
+// taken from llvm/ExecutionEngine/GenericValue.h
+#define bool signed char // C has no boolean datatype
+#define uint64_t unsigned long long
+#define int64_t signed long long
+#define PointerTy void*
+
+// XXX GenericValue_ grew an underscore to avoid a conflict with llvm's GenericValue
+// which has additional stuff like constructors and the like which ctypes
+// codewriter does not swallow at the moment.
+// Idealy we would rewrite/extend codewriter (
+union GenericValue_ {
+ bool BoolVal;
+ unsigned char UByteVal;
+ signed char SByteVal;
+ unsigned short UShortVal;
+ signed short ShortVal;
+ unsigned int UIntVal;
+ signed int IntVal;
+ uint64_t ULongVal;
+ int64_t LongVal;
+ double DoubleVal;
+ float FloatVal;
+ struct { unsigned int first; unsigned int second; } UIntPairVal;
+ PointerTy PointerVal;
+ unsigned char Untyped[8];
+};
+
void* GenericValue__init__();
#ifdef __cplusplus
Added: pypy/dist/pypy/translator/llvm/llvmcapi/include/VMCore/Type.h
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/llvm/llvmcapi/include/VMCore/Type.h Wed Apr 5 09:15:29 2006
@@ -0,0 +1,40 @@
+#ifndef __TYPE_H__
+#define __TYPE_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// XXX can ctypes codewriter this directly from llvm/Type.h?
+enum TypeID {
+ // PrimitiveTypes .. make sure LastPrimitiveTyID stays up to date
+ VoidTyID = 0 , BoolTyID, // 0, 1: Basics...
+ UByteTyID , SByteTyID, // 2, 3: 8 bit types...
+ UShortTyID , ShortTyID, // 4, 5: 16 bit types...
+ UIntTyID , IntTyID, // 6, 7: 32 bit types...
+ ULongTyID , LongTyID, // 8, 9: 64 bit types...
+ FloatTyID , DoubleTyID, // 10,11: Floating point types...
+ LabelTyID , // 12 : Labels...
+
+ // Derived types... see DerivedTypes.h file...
+ // Make sure FirstDerivedTyID stays up to date!!!
+ FunctionTyID , StructTyID, // Functions... Structs...
+ ArrayTyID , PointerTyID, // Array... pointer...
+ OpaqueTyID, // Opaque type instances...
+ PackedTyID, // SIMD 'packed' format...
+ //...
+
+ NumTypeIDs, // Must remain as last defined ID
+ LastPrimitiveTyID = LabelTyID,
+ FirstDerivedTyID = FunctionTyID
+ };
+
+TypeID Type_getTypeID(void* T);
+const void* Type_getContainedType(void* T, int n); //return a Type*
+const char* Type_getDescription(void* T);
+
+#ifdef __cplusplus
+};
+#endif
+
+#endif
Modified: pypy/dist/pypy/translator/llvm/llvmcapi/include/llvmcapi.h
==============================================================================
--- pypy/dist/pypy/translator/llvm/llvmcapi/include/llvmcapi.h (original)
+++ pypy/dist/pypy/translator/llvm/llvmcapi/include/llvmcapi.h Wed Apr 5 09:15:29 2006
@@ -9,6 +9,7 @@
#include "VMCore/ModuleProvider.h"
#include "VMCore/Function.h"
#include "VMCore/DerivedTypes.h"
+#include "VMCore/Type.h"
#include "ExecutionEngine/ExecutionEngine.h"
#include "ExecutionEngine/GenericValue.h"
Modified: pypy/dist/pypy/translator/llvm/llvmcapi/lib/ExecutionEngine/ExecutionEngine.cpp
==============================================================================
--- pypy/dist/pypy/translator/llvm/llvmcapi/lib/ExecutionEngine/ExecutionEngine.cpp (original)
+++ pypy/dist/pypy/translator/llvm/llvmcapi/lib/ExecutionEngine/ExecutionEngine.cpp Wed Apr 5 09:15:29 2006
@@ -26,9 +26,18 @@
ee->freeMachineCodeForFunction(f);
}
-int ExecutionEngine_runFunction(void* EE, void* F, int args_vector) {
- ExecutionEngine* ee = (ExecutionEngine*)EE;
- Function* f = (Function*)F;
+long long ExecutionEngine_runFunction(void* EE, void* F, void* A) {
+ ExecutionEngine* ee = (ExecutionEngine*)EE;
+ Function* f = (Function*)F;
+ long long* pArgs = (long long*)A;
+
+ int n_params = f->getFunctionType()->getNumParams();
std::vector<GenericValue> args;
- return ee->runFunction(f, args).IntVal;
+ GenericValue gv;
+ for (int i=0;i < n_params;i++) {
+ gv.LongVal = pArgs[i];
+ args.push_back(gv);
+ }
+
+ return ee->runFunction(f, args).LongVal;
}
Added: pypy/dist/pypy/translator/llvm/llvmcapi/lib/VMCore/Type.cpp
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/llvm/llvmcapi/lib/VMCore/Type.cpp Wed Apr 5 09:15:29 2006
@@ -0,0 +1,18 @@
+#include "llvm/Type.h"
+
+using namespace llvm;
+
+TypeID Type_getTypeID(void* T) {
+ Type* t = (Type*)T;
+ return (TypeID)t->getTypeID();
+}
+
+const void* Type_getContainedType(void* T, int n) {
+ Type* t = (Type*)T;
+ return t->getContainedType(n);
+}
+
+const char* Type_getDescription(void* T) {
+ Type* t = (Type*)T;
+ return t->getDescription().c_str();
+}
Modified: pypy/dist/pypy/translator/llvm/llvmcapi/lib/llvmcapi.cpp
==============================================================================
--- pypy/dist/pypy/translator/llvm/llvmcapi/lib/llvmcapi.cpp (original)
+++ pypy/dist/pypy/translator/llvm/llvmcapi/lib/llvmcapi.cpp Wed Apr 5 09:15:29 2006
@@ -23,6 +23,7 @@
#include "VMCore/ModuleProvider.cpp"
#include "VMCore/Function.cpp"
#include "VMCore/DerivedTypes.cpp"
+#include "VMCore/Type.cpp"
#include "ExecutionEngine/ExecutionEngine.cpp"
#include "ExecutionEngine/GenericValue.cpp"
Modified: pypy/dist/pypy/translator/llvm/pythonllvm/create_llvmcapi.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/pythonllvm/create_llvmcapi.py (original)
+++ pypy/dist/pypy/translator/llvm/pythonllvm/create_llvmcapi.py Wed Apr 5 09:15:29 2006
@@ -4,11 +4,14 @@
WRAPDIR = "/Users/eric/projects/ctypes/ctypes/wrap" #XXX get rid of this hardcoded path
if os.path.exists(WRAPDIR + "/h2xml.py"):
- print "creating llvmcapi.py"
- os.system("python " + WRAPDIR + "/h2xml.py ../llvmcapi/include/llvmcapi.h -q -I . -o llvmcapi.xml")
+ print "h2xml.py"
+ os.system("python " + WRAPDIR + "/h2xml.py ../llvmcapi/include/llvmcapi.h -q -I. -I/opt/local/include -D_GNU_SOURCE -D__STDC_LIMIT_MACROS -o llvmcapi.xml")
+ print "xml2py.py"
os.system("python " + WRAPDIR + "/xml2py.py llvmcapi.xml -l ../llvmcapi/llvmcapi.so -o llvmcapi.tmp")
+ print "massage output"
os.system("sed -e s/from\ ctypes\ import/from\ cc\ import/ llvmcapi.tmp > llvmcapi.py")
+ print "cleanup some files"
os.system("rm -f llvmcapi.tmp llvmcapi.xml")
- print "done"
+ print "created llvmcapi.py"
else:
print "skipping llvmcapi.py creation"
Modified: pypy/dist/pypy/translator/llvm/pythonllvm/helper.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/pythonllvm/helper.py (original)
+++ pypy/dist/pypy/translator/llvm/pythonllvm/helper.py Wed Apr 5 09:15:29 2006
@@ -21,3 +21,41 @@
def __getattr__(self, name):
global_funcname = self.__class__.__name__ + "_" + name
return Method(self.instance, globals()[global_funcname])
+
+
+def to_llvm_value(pythonvalue, type_):
+ value = GenericValue_()
+ value.LongVal = 0
+
+ # XXX store in correct value.<type> here
+ value.IntVal = pythonvalue
+ return value
+
+
+def to_python_value(llvmvalue, type_):
+ value = GenericValue_()
+ value.LongVal = llvmvalue # XXX convert llvmvalue from long long
+ id_ = type_.getTypeID()
+ if id_ == PointerTyID and type_.getContainedType(0).getTypeID() == SByteTyID:
+ return STRING(value.PointerVal).value
+ elif id_ == VoidTyID:
+ return None
+ elif id_ == BoolTyID:
+ return value.BoolVal
+ elif id_ == UByteTyID:
+ return value.UByteVal
+ elif id_ == SByteTyID:
+ return value.SByteVal
+ elif id_ == UShortTyID:
+ return value.UShortVal
+ elif id_ == ShortTyID:
+ return value.ShortVal
+ elif id_ == UIntTyID:
+ return value.UIntVal
+ elif id_ == IntTyID:
+ return value.IntVal
+ elif id_ == ULongTyID:
+ return value.ULongVal
+ elif id_ == LongTyID:
+ return value.ULongVal
+ raise Exception("don't know how to convert llvmtype '%s' to python" % type_.getDescription())
Modified: pypy/dist/pypy/translator/llvm/pythonllvm/llvmcapi.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/pythonllvm/llvmcapi.py (original)
+++ pypy/dist/pypy/translator/llvm/pythonllvm/llvmcapi.py Wed Apr 5 09:15:29 2006
@@ -4,6 +4,28 @@
STRING = c_char_p
+UShortTyID = 4
+FirstDerivedTyID = 13
+FunctionTyID = 13
+ShortTyID = 5
+LabelTyID = 12
+UIntTyID = 6
+ArrayTyID = 15
+IntTyID = 7
+PointerTyID = 16
+ULongTyID = 8
+LongTyID = 9
+BoolTyID = 1
+StructTyID = 14
+PackedTyID = 18
+FloatTyID = 10
+NumTypeIDs = 19
+UByteTyID = 2
+VoidTyID = 0
+SByteTyID = 3
+OpaqueTyID = 17
+LastPrimitiveTyID = 12
+DoubleTyID = 11
def ExecutionEngine__create__(MP, ForceInterpreter):
# ../llvmcapi/include/ExecutionEngine/ExecutionEngine.h 12
@@ -23,14 +45,44 @@
ExecutionEngine_freeMachineCodeForFunction = cdecl(None, 'llvmcapi', [c_void_p, c_void_p]) (ExecutionEngine_freeMachineCodeForFunction)
-def ExecutionEngine_runFunction(EE, F, args_vector):
+def ExecutionEngine_runFunction(EE, F, A):
# ../llvmcapi/include/ExecutionEngine/ExecutionEngine.h 17
- return ExecutionEngine_runFunction._api_(EE, F, args_vector)
-ExecutionEngine_runFunction = cdecl(c_int, 'llvmcapi', [c_void_p, c_void_p, c_int]) (ExecutionEngine_runFunction)
+ return ExecutionEngine_runFunction._api_(EE, F, A)
+ExecutionEngine_runFunction = cdecl(c_longlong, 'llvmcapi', [c_void_p, c_void_p, c_void_p]) (ExecutionEngine_runFunction)
+# ../llvmcapi/include/ExecutionEngine/GenericValue.h 20
+class GenericValue_(Union):
+ pass
+# ../llvmcapi/include/ExecutionEngine/GenericValue.h 32
+class N13GenericValue_3DOLLAR_0E(Structure):
+ pass
+N13GenericValue_3DOLLAR_0E._fields_ = [
+ # ../llvmcapi/include/ExecutionEngine/GenericValue.h 32
+ ('first', c_uint),
+ ('second', c_uint),
+]
+assert sizeof(N13GenericValue_3DOLLAR_0E) == 8, sizeof(N13GenericValue_3DOLLAR_0E)
+GenericValue_._fields_ = [
+ # ../llvmcapi/include/ExecutionEngine/GenericValue.h 20
+ ('BoolVal', c_byte),
+ ('UByteVal', c_ubyte),
+ ('SByteVal', c_byte),
+ ('UShortVal', c_ushort),
+ ('ShortVal', c_short),
+ ('UIntVal', c_uint),
+ ('IntVal', c_int),
+ ('ULongVal', c_ulonglong),
+ ('LongVal', c_longlong),
+ ('DoubleVal', c_double),
+ ('FloatVal', c_float),
+ ('UIntPairVal', N13GenericValue_3DOLLAR_0E),
+ ('PointerVal', c_void_p),
+ ('Untyped', c_ubyte * 8),
+]
+assert sizeof(GenericValue_) == 8, sizeof(GenericValue_)
def GenericValue__init__():
- # ../llvmcapi/include/ExecutionEngine/GenericValue.h 8
+ # ../llvmcapi/include/ExecutionEngine/GenericValue.h 37
return GenericValue__init__._api_()
GenericValue__init__ = cdecl(c_void_p, 'llvmcapi', []) (GenericValue__init__)
@@ -143,8 +195,29 @@
ExistingModuleProvider__init__ = cdecl(c_void_p, 'llvmcapi', [c_void_p]) (ExistingModuleProvider__init__)
+# values for enumeration 'TypeID'
+TypeID = c_int # enum
+
+def Type_getTypeID(T):
+ # ../llvmcapi/include/VMCore/Type.h 32
+ return Type_getTypeID._api_(T)
+Type_getTypeID = cdecl(TypeID, 'llvmcapi', [c_void_p]) (Type_getTypeID)
+
+
+def Type_getContainedType(T, n):
+ # ../llvmcapi/include/VMCore/Type.h 33
+ return Type_getContainedType._api_(T, n)
+Type_getContainedType = cdecl(c_void_p, 'llvmcapi', [c_void_p, c_int]) (Type_getContainedType)
+
+
+def Type_getDescription(T):
+ # ../llvmcapi/include/VMCore/Type.h 34
+ return Type_getDescription._api_(T)
+Type_getDescription = cdecl(STRING, 'llvmcapi', [c_void_p]) (Type_getDescription)
+
+
def toggle_print_machineinstrs():
- # ../llvmcapi/include/llvmcapi.h 15
+ # ../llvmcapi/include/llvmcapi.h 16
return toggle_print_machineinstrs._api_()
toggle_print_machineinstrs = cdecl(None, 'llvmcapi', []) (toggle_print_machineinstrs)
Modified: pypy/dist/pypy/translator/llvm/pythonllvm/pyllvm.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/pythonllvm/pyllvm.py (original)
+++ pypy/dist/pypy/translator/llvm/pythonllvm/pyllvm.py Wed Apr 5 09:15:29 2006
@@ -25,7 +25,7 @@
MP = ExistingModuleProvider();
self.instance = ExecutionEngine__create__(MP.instance, ForceInterpreter)
global ee_hack
- ee_hack = self.instance
+ ee_hack = self.instance #XXX how to get to the executionengine from a function?
# XXX cast to actual Python Module (can't we automate this?)
def getModule(self):
@@ -34,7 +34,7 @@
return m
# helpers
- def parse(self, llcode):
+ def parse(self, llcode, fnname=None):
mod = self.getModule()
mod.ParseAssemblyString(llcode)
mod.verifyModule()
@@ -46,32 +46,29 @@
f.eraseFromParent()
-def to_llvm_value(pythonvalue, type_):
- # XXX use the GenericValue union instead
- return pythonvalue
-
-
-def to_python_value(llvmvalue, type_):
- # XXX use the GenericValue union instead
- return llvmvalue
-
-
class Function(Wrapper):
def __init__(self):
self.instance = Function__init__() #XXX this get annoying quickly
def __call__(self, *args):
- print 'calling %s(%s)' % ('Function', ','.join([str(arg) for arg in args]))
- ft = Function_getFunctionType(self.instance)
- argcount = FunctionType_getNumParams(ft)
- print 'argcount = ',argcount
+ #print 'calling %s(%s)' % ('Function', ','.join([str(arg) for arg in args]))
+ ft = self.getFunctionType()
+ argcount = ft.getNumParams()
+ #print 'argcount = ',argcounT
if argcount != len(args):
raise Exception("incorrect number of parameters")
- llvmvalues = GenericValue__init__()
+ ParamType = c_longlong * argcount
+ llvmvalues = ParamType()
for i, arg in enumerate(args):
- llvmvalues.append( to_llvm_value(arg, FunctionType_getParamType(ft, i)))
- llvmreturnvalue = ExecutionEngine_runFunction(ee_hack, self.instance, llvmvalues)
- return to_python_value(llvmreturnvalue, FunctionType_getReturnType(ft))
+ llvmvalues[i] = to_llvm_value(arg, ft.getParamType(i)).LongVal
+ pLlvmValues = cast(llvmvalues, c_void_p) #would like to cast to c_longlong_p
+ llvmreturnvalue = ExecutionEngine_runFunction(ee_hack, self.instance, pLlvmValues)
+ return to_python_value(llvmreturnvalue, ft.getReturnType())
+
+ def getFunctionType(self):
+ ft = object.__new__(FunctionType)
+ ft.instance = Function_getFunctionType(self.instance)
+ return ft
class GenericValue(Wrapper):
@@ -79,11 +76,26 @@
self.instance = GenericValue__init__()
-#class Instruction(Wrapper):
-# def __init__(self):
-# self.instance = Instruction__init__()
-#
-#
-#class BasicBlock(Wrapper):
-# def __init__(self):
-# self.instance = BasicBlock__init__()
+class Type(Wrapper):
+ def __init(self):
+ self.instance = Type__init__()
+
+ def getContainedType(self, n):
+ t = object.__new__(Type)
+ t.instance = Type_getContainedType(self.instance, n)
+ return t
+
+
+class FunctionType(Wrapper):
+ def __init(self):
+ self.instance = FunctionType__init__()
+
+ def getReturnType(self):
+ t = object.__new__(Type)
+ t.instance = FunctionType_getReturnType(self.instance)
+ return t
+
+ def getParamType(self, i):
+ t = object.__new__(Type)
+ t.instance = FunctionType_getParamType(self.instance, i)
+ return t
Modified: pypy/dist/pypy/translator/llvm/pythonllvm/test/test_ee.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/pythonllvm/test/test_ee.py (original)
+++ pypy/dist/pypy/translator/llvm/pythonllvm/test/test_ee.py Wed Apr 5 09:15:29 2006
@@ -6,7 +6,6 @@
from pypy.translator.llvm.pythonllvm import pyllvm
from pypy.translator.llvm.pythonllvm.test import ll_snippet
-py.test.skip("WIP")
def test_execution_engine():
ee = pyllvm.ExecutionEngine()
@@ -49,13 +48,16 @@
assert gethellostr() == "hello world\n"
def test_call_parse_twice():
+ py.test.skip("WIP")
ee = pyllvm.ExecutionEngine()
ee.parse(codepath.join("hello.s").read())
f = ee.getModule().getNamedFunction
- assert f("gethellostr")() == "hello world\n"
+ f1 = f("gethellostr")
+ assert f1() == "hello world\n"
ee.parse(codepath.join("addnumbers.s").read())
- assert f("add")(10, 32) == 42
- assert f("gethellostr")() == "hello world\n"
+ f2 = f("add")
+ assert f2(10, 32) == 42
+ assert f1() == "hello world\n"
py.test.raises(Exception, ee.parse)
py.test.raises(Exception, ee.parse, 1)
py.test.raises(Exception, ee.parse, "abc")
@@ -73,6 +75,7 @@
assert f("calc")(122) == 123
def test_replace_function():
+ py.test.skip("WIP")
"""similar to test_call_between_parsed_code with additional complexity
because we rebind the add1 function to another version after it the
first version already has been used."""
@@ -98,6 +101,7 @@
assert f("sub10_from_global_int_a")() == 82
def test_native_code(): #examine JIT generate native (assembly) code
+ py.test.skip("WIP")
pyllvm.toggle_print_machineinstrs()
ee = pyllvm.ExecutionEngine()
ee.parse(ll_snippet.calc)
@@ -107,6 +111,7 @@
pyllvm.toggle_print_machineinstrs()
def test_delete_function(): #this will only work if nothing uses Fn of course!
+ py.test.skip("WIP")
ee = pyllvm.ExecutionEngine()
mod = ee.getModule()
ee.parse(ll_snippet.calc)
More information about the Pypy-commit
mailing list