From 6b88ffbaed4757f95814aa650c7297bd8e6d8f6a Mon Sep 17 00:00:00 2001 From: Siu Kwan Lam Date: Tue, 22 Jan 2013 21:23:20 -0600 Subject: [PATCH] Add custom binding to SmallVector --- newbinding/Makefile | 4 +++- newbinding/binding.py | 10 ++++++++++ newbinding/include/llvm_binding/extra.h | 24 +++++++++++++++++++++++- newbinding/src/DerivedTypes.py | 11 +++++++---- newbinding/test2.py | 10 ++++++++-- 5 files changed, 51 insertions(+), 8 deletions(-) diff --git a/newbinding/Makefile b/newbinding/Makefile index 3996d8b..6ae50be 100644 --- a/newbinding/Makefile +++ b/newbinding/Makefile @@ -1,5 +1,7 @@ PYTHON = python -PYMODS = raw_ostream Type DerivedTypes LLVMContext StringRef AssemblyAnnotationWriter Module + +PYMODS = raw_ostream SmallVector Type DerivedTypes StringRef +PYMODS += LLVMContext AssemblyAnnotationWriter Module all: _api.so _capsule.so diff --git a/newbinding/binding.py b/newbinding/binding.py index de68b55..adeb284 100644 --- a/newbinding/binding.py +++ b/newbinding/binding.py @@ -190,6 +190,16 @@ Bool = BuiltinType('bool') Unsigned = BuiltinType('unsigned') ConstStdString = BuiltinType('const std::string') +class PyObjectImpl(object): + name = 'PyObject*' + fullname = name + format = 'O' + + def unwrap(self, println, var): + return var + +PyObject = PyObjectImpl() + class Unwrapper(object): def __init__(self, cls, pytype): self.cls = cls diff --git a/newbinding/include/llvm_binding/extra.h b/newbinding/include/llvm_binding/extra.h index a7388a2..e78f0e2 100644 --- a/newbinding/include/llvm_binding/extra.h +++ b/newbinding/include/llvm_binding/extra.h @@ -4,6 +4,8 @@ namespace llvm{ +class Type; //forward declaration + class raw_svector_ostream_helper: public raw_svector_ostream { SmallVectorImpl *SV; public: @@ -30,5 +32,25 @@ private: void operator = (const raw_svector_ostream_helper&); }; -} + +class SmallVector_Type : public SmallVector { +public: + static + SmallVector_Type* fromPySequence(PyObject* obj) { + SmallVector_Type* SV = new SmallVector_Type; + Py_ssize_t sz = PySequence_Size(obj); + for (Py_ssize_t i = 0; i < sz; ++i) { + PyObject* item = PySequence_GetItem(obj, i); + PyObject* cap = PyObject_GetAttrString(item, "_ptr"); + Type* type = (Type*)PyCapsule_GetPointer(cap, "llvm::Type"); + SV->push_back(type); + Py_XDECREF(cap); + Py_XDECREF(item); + } + return SV; + } +}; + + +} // end namespace llvm diff --git a/newbinding/src/DerivedTypes.py b/newbinding/src/DerivedTypes.py index edf4297..d9dbb53 100644 --- a/newbinding/src/DerivedTypes.py +++ b/newbinding/src/DerivedTypes.py @@ -2,14 +2,17 @@ from binding import * from namespace import llvm from LLVMContext import LLVMContext from Type import Type +from SmallVector import SmallVector_Type FunctionType = Type.Subclass() FunctionType.include.add('llvm/DerivedTypes.h') -get = FunctionType.staticmultimethod([FunctionType.Pointer, - Type.Pointer, Bool.From(bool)],) -# (FunctionType.Pointer, -# Type.Pointer, )]) +_get_signatures = [(FunctionType.Pointer, + Type.Pointer, Bool.From(bool)), + (FunctionType.Pointer, + Type.Pointer, SmallVector_Type.Ref, Bool.From(bool))] + +get = FunctionType.staticmultimethod(*_get_signatures) isVarArg = FunctionType.method(Bool.To(bool)) getReturnType = FunctionType.method(Type.Pointer) diff --git a/newbinding/test2.py b/newbinding/test2.py index 0d64716..6cb8a06 100644 --- a/newbinding/test2.py +++ b/newbinding/test2.py @@ -1,5 +1,5 @@ import api -#api.capsule.set_debug(True) +api.capsule.set_debug(True) context = api.getGlobalContext() @@ -14,7 +14,6 @@ assert m.getPointerSize() == api.Module.PointerSize.AnyPointerSize m.dump() - os = api.raw_svector_ostream_helper.create() m.print_(os, None) print os.str() @@ -26,6 +25,13 @@ int1ty.dump() print int1ty.isIntegerTy(1) fnty = api.FunctionType.get(int1ty, False) +fnty.dump() +print + +types = [int1ty, api.Type.getIntNTy(context, 21)] +sv = api.SmallVector_Type.fromPySequence(types) + +fnty = api.FunctionType.get(int1ty, sv, False) os2 = api.raw_svector_ostream_helper.create() fnty.print_(os2)