Remove IterToList, llvm_extra.h.

Add CustomMethod.
This commit is contained in:
Siu Kwan Lam 2013-01-28 15:53:01 -06:00
commit af7962a2ca
7 changed files with 61 additions and 64 deletions

View file

@ -272,27 +272,16 @@ class Method(object):
writer.println() writer.println()
class IterToList(Method): class CustomMethod(Method):
def __init__(self, elemty, argtys, iterty, begin, end): def __init__(self, methodname, retty, *argtys):
super(IterToList, self).__init__(PyObjectPtr, *argtys) super(CustomMethod, self).__init__(retty, *argtys)
self.elemty = elemty self.methodname = methodname
self.iterty = iterty
self.begin = begin
self.end = end
def compile_cpp_body(self, writer, retty, argtys): def compile_cpp_body(self, writer, retty, argtys):
args = writer.parse_arguments('args', ptr(self.parent), *argtys) args = writer.parse_arguments('args', ptr(self.parent), *argtys)
it = writer.method_call(self.begin, self.iterty, *args) ret = writer.call(self.methodname, retty.fullname, *args)
end = writer.method_call(self.end, self.iterty, *args) writer.return_value(retty.wrap(writer, ret))
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)
class StaticMethod(Method): class StaticMethod(Method):

View file

@ -42,14 +42,14 @@ def parse_arguments(println, var, *args):
return unwrapped return unwrapped
_re_mangle_pattern = re.compile(r'[ _<>\*&]') _re_mangle_pattern = re.compile(r'[ _<>\*&,]')
def mangle(name): def mangle(name):
def repl(m): def repl(m):
s = m.group(0) s = m.group(0)
if s in '<>*&': if s in '<>*&':
return '' return ''
elif s in ' ': elif s in ' ,':
return '_' return '_'
elif s in '_': elif s in '_':
return '__' return '__'

View file

@ -41,7 +41,6 @@ def populate_headers(println):
includes = [ includes = [
'llvm_binding/conversion.h', 'llvm_binding/conversion.h',
'llvm_binding/binding.h', 'llvm_binding/binding.h',
'llvm_binding/llvm_extra.h',
'llvm_binding/capsule_context.h', 'llvm_binding/capsule_context.h',
'llvm_binding/extra.h', # extra submodule to add 'llvm_binding/extra.h', # extra submodule to add
] ]

View file

@ -1,10 +1,43 @@
#include <Python.h> #include <Python.h>
#include <llvm/ADT/SmallVector.h> #include <llvm/ADT/SmallVector.h>
#include <llvm/Value.h>
#include <llvm/Support/raw_ostream.h> #include <llvm/Support/raw_ostream.h>
namespace extra{
using namespace llvm;
class raw_svector_ostream_helper: public raw_svector_ostream {
SmallVectorImpl<char> *SV;
public:
static
raw_svector_ostream_helper* create(){
SmallVectorImpl<char>* sv = new SmallVector<char, 16>();
return new raw_svector_ostream_helper(sv);
}
~raw_svector_ostream_helper(){
delete SV;
}
protected:
explicit
raw_svector_ostream_helper(SmallVectorImpl<char>* 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 static
PyObject* make_raw_ostream_for_printing(PyObject* self, PyObject* args) { 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; using llvm::raw_svector_ostream;
if (!PyArg_ParseTuple(args, "")) { if (!PyArg_ParseTuple(args, "")) {
@ -18,7 +51,7 @@ PyObject* make_raw_ostream_for_printing(PyObject* self, PyObject* args) {
static static
PyObject* make_small_vector_from_types(PyObject* self, PyObject* args) { PyObject* make_small_vector_from_types(PyObject* self, PyObject* args) {
using llvm::Type; using llvm::Type;
using llvm::SmallVector_Type; typedef llvm::SmallVector<llvm::Type*, 8> SmallVector_Type;
SmallVector_Type* SV = new SmallVector_Type; SmallVector_Type* SV = new SmallVector_Type;
Py_ssize_t size = PyTuple_Size(args); 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); SV->push_back(type);
} }
return pycapsule_new(SV, "llvm::SmallVector_Type"); return pycapsule_new(SV, "llvm::SmallVector<llvm::Type*,8>");
} }
static PyMethodDef extra_methodtable[] = { static PyMethodDef extra_methodtable[] = {
@ -43,3 +76,17 @@ static PyMethodDef extra_methodtable[] = {
#undef method #undef method
{ NULL } { 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;
}

View file

@ -1,38 +0,0 @@
#include <Python.h>
#include <llvm/ADT/SmallVector.h>
#include <llvm/Support/raw_ostream.h>
namespace llvm{
class Type; //forward declaration
class raw_svector_ostream_helper: public raw_svector_ostream {
SmallVectorImpl<char> *SV;
public:
static
raw_svector_ostream_helper* create(){
SmallVectorImpl<char>* sv = new SmallVector<char, 16>();
return new raw_svector_ostream_helper(sv);
}
~raw_svector_ostream_helper(){
delete SV;
}
protected:
explicit
raw_svector_ostream_helper(SmallVectorImpl<char>* 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<Type*, 8> SmallVector_Type;
} // end namespace llvm

View file

@ -3,4 +3,5 @@ from namespace import llvm
@llvm.Class() @llvm.Class()
class SmallVector_Type: class SmallVector_Type:
_realname_ = 'SmallVector<llvm::Type*,8>'
delete = Destructor() delete = Destructor()

View file

@ -32,8 +32,7 @@ class Value:
replaceAllUsesWith = Method(Void, ptr(Value)) replaceAllUsesWith = Method(Void, ptr(Value))
list_use = IterToList(ptr(User), (), 'llvm::Value::use_iterator', list_use = CustomMethod('Value_use_iterator_to_list', PyObjectPtr)
'use_begin', 'use_end')
hasOneUse = Method(cast(Bool, bool)) hasOneUse = Method(cast(Bool, bool))
hasNUses = Method(cast(Bool, bool), cast(int, Unsigned)) hasNUses = Method(cast(Bool, bool), cast(int, Unsigned))