diff --git a/newbinding/binding.py b/newbinding/binding.py index 1764559..3538c9a 100644 --- a/newbinding/binding.py +++ b/newbinding/binding.py @@ -272,28 +272,17 @@ class Method(object): writer.println() -class IterToList(Method): - def __init__(self, elemty, argtys, iterty, begin, end): - super(IterToList, self).__init__(PyObjectPtr, *argtys) - self.elemty = elemty - self.iterty = iterty - self.begin = begin - self.end = end +class CustomMethod(Method): + def __init__(self, methodname, retty, *argtys): + super(CustomMethod, self).__init__(retty, *argtys) + self.methodname = methodname def compile_cpp_body(self, writer, retty, argtys): args = writer.parse_arguments('args', ptr(self.parent), *argtys) - it = writer.method_call(self.begin, self.iterty, *args) - end = writer.method_call(self.end, self.iterty, *args) - con = writer.call('PyList_New', 'PyObject*', '0') - writer.die_if_false(con) - with writer.block('for (; %(it)s != %(end)s; ++%(it)s)' % locals()): - elem = writer.declare(self.elemty.fullname, '*%(it)s;' % locals()) - wrapped = self.elemty.wrap(writer, elem) - ok = writer.call('PyList_Append', 'int', con, wrapped) - with writer.block('if (%(ok)s == -1)' % locals()): - writer.return_value(None) - writer.return_value(con) + ret = writer.call(self.methodname, retty.fullname, *args) + writer.return_value(retty.wrap(writer, ret)) + class StaticMethod(Method): diff --git a/newbinding/codegen.py b/newbinding/codegen.py index 2a85abc..7652447 100644 --- a/newbinding/codegen.py +++ b/newbinding/codegen.py @@ -42,14 +42,14 @@ def parse_arguments(println, var, *args): return unwrapped -_re_mangle_pattern = re.compile(r'[ _<>\*&]') +_re_mangle_pattern = re.compile(r'[ _<>\*&,]') def mangle(name): def repl(m): s = m.group(0) if s in '<>*&': return '' - elif s in ' ': + elif s in ' ,': return '_' elif s in '_': return '__' diff --git a/newbinding/gen.py b/newbinding/gen.py index f1af18e..d9fc14f 100644 --- a/newbinding/gen.py +++ b/newbinding/gen.py @@ -41,7 +41,6 @@ def populate_headers(println): includes = [ 'llvm_binding/conversion.h', 'llvm_binding/binding.h', - 'llvm_binding/llvm_extra.h', 'llvm_binding/capsule_context.h', 'llvm_binding/extra.h', # extra submodule to add ] diff --git a/newbinding/include/llvm_binding/extra.h b/newbinding/include/llvm_binding/extra.h index 94887b3..639a87c 100644 --- a/newbinding/include/llvm_binding/extra.h +++ b/newbinding/include/llvm_binding/extra.h @@ -1,10 +1,43 @@ #include #include +#include #include + +namespace extra{ + using namespace llvm; + + class raw_svector_ostream_helper: public raw_svector_ostream { + SmallVectorImpl *SV; + public: + static + raw_svector_ostream_helper* create(){ + SmallVectorImpl* sv = new SmallVector(); + return new raw_svector_ostream_helper(sv); + } + + ~raw_svector_ostream_helper(){ + delete SV; + } + + protected: + + explicit + raw_svector_ostream_helper(SmallVectorImpl* sv) + : raw_svector_ostream(*sv), SV(sv) {} + + private: + // no copy + raw_svector_ostream_helper(const raw_svector_ostream_helper&); + // no assign + void operator = (const raw_svector_ostream_helper&); + }; + +} + static PyObject* make_raw_ostream_for_printing(PyObject* self, PyObject* args) { - using llvm::raw_svector_ostream_helper; + using extra::raw_svector_ostream_helper; using llvm::raw_svector_ostream; if (!PyArg_ParseTuple(args, "")) { @@ -18,7 +51,7 @@ PyObject* make_raw_ostream_for_printing(PyObject* self, PyObject* args) { static PyObject* make_small_vector_from_types(PyObject* self, PyObject* args) { using llvm::Type; - using llvm::SmallVector_Type; + typedef llvm::SmallVector SmallVector_Type; SmallVector_Type* SV = new SmallVector_Type; Py_ssize_t size = PyTuple_Size(args); @@ -33,7 +66,7 @@ PyObject* make_small_vector_from_types(PyObject* self, PyObject* args) { } SV->push_back(type); } - return pycapsule_new(SV, "llvm::SmallVector_Type"); + return pycapsule_new(SV, "llvm::SmallVector"); } static PyMethodDef extra_methodtable[] = { @@ -43,3 +76,17 @@ static PyMethodDef extra_methodtable[] = { #undef method { NULL } }; + + +//////////// + +PyObject* Value_use_iterator_to_list(llvm::Value* val){ + typedef llvm::Value::use_iterator iterator; + PyObject* list = PyList_New(0); + for (iterator it = val->use_begin(), end = val->use_end(); it != end; ++it) { + PyObject* cap = pycapsule_new(*it, "llvm::Value", "llvm::User"); + PyList_Append(list, cap); + } + return list; +} + diff --git a/newbinding/include/llvm_binding/llvm_extra.h b/newbinding/include/llvm_binding/llvm_extra.h deleted file mode 100644 index f920a47..0000000 --- a/newbinding/include/llvm_binding/llvm_extra.h +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -#include - -namespace llvm{ - -class Type; //forward declaration - -class raw_svector_ostream_helper: public raw_svector_ostream { - SmallVectorImpl *SV; -public: - static - raw_svector_ostream_helper* create(){ - SmallVectorImpl* sv = new SmallVector(); - return new raw_svector_ostream_helper(sv); - } - - ~raw_svector_ostream_helper(){ - delete SV; - } - -protected: - - explicit - raw_svector_ostream_helper(SmallVectorImpl* sv) - : raw_svector_ostream(*sv), SV(sv) {} - -private: - // no copy - raw_svector_ostream_helper(const raw_svector_ostream_helper&); - // no assign - void operator = (const raw_svector_ostream_helper&); -}; - -typedef SmallVector SmallVector_Type; - -} // end namespace llvm - diff --git a/newbinding/src/SmallVector.py b/newbinding/src/SmallVector.py index d00b294..55dadb1 100644 --- a/newbinding/src/SmallVector.py +++ b/newbinding/src/SmallVector.py @@ -3,4 +3,5 @@ from namespace import llvm @llvm.Class() class SmallVector_Type: + _realname_ = 'SmallVector' delete = Destructor() diff --git a/newbinding/src/Value.py b/newbinding/src/Value.py index a901946..3dd5644 100644 --- a/newbinding/src/Value.py +++ b/newbinding/src/Value.py @@ -32,8 +32,7 @@ class Value: replaceAllUsesWith = Method(Void, ptr(Value)) - list_use = IterToList(ptr(User), (), 'llvm::Value::use_iterator', - 'use_begin', 'use_end') + list_use = CustomMethod('Value_use_iterator_to_list', PyObjectPtr) hasOneUse = Method(cast(Bool, bool)) hasNUses = Method(cast(Bool, bool), cast(int, Unsigned))