From e9be14ca0e18fbe8fe4b79af20288db8f9dcad4f Mon Sep 17 00:00:00 2001 From: Siu Kwan Lam Date: Wed, 23 Jan 2013 15:24:37 -0600 Subject: [PATCH] Refactor raw_svector_stream_helper --- newbinding/include/llvm_binding/extra.h | 26 +++++++++++++++++++++++-- newbinding/src/Module.py | 11 +++++------ newbinding/src/Type.py | 4 ++-- newbinding/src/raw_ostream.py | 9 ++------- newbinding/test2.py | 14 ++++++++----- 5 files changed, 42 insertions(+), 22 deletions(-) diff --git a/newbinding/include/llvm_binding/extra.h b/newbinding/include/llvm_binding/extra.h index 23548ff..417c0f8 100644 --- a/newbinding/include/llvm_binding/extra.h +++ b/newbinding/include/llvm_binding/extra.h @@ -1,15 +1,36 @@ #include #include +#include static -PyObject* small_vector_from_types(PyObject* self, PyObject* args) { +PyObject* make_raw_ostream_for_printing(PyObject* self, PyObject* args) { + using llvm::raw_svector_ostream_helper; + using llvm::raw_svector_ostream; + + if (!PyArg_ParseTuple(args, "")) { + return NULL; + } + raw_svector_ostream* RSOH = raw_svector_ostream_helper::create(); + return pycapsule_new(RSOH, "llvm::raw_ostream", + "llvm::raw_svector_ostream"); +} + +static +PyObject* make_small_vector_from_types(PyObject* self, PyObject* args) { using llvm::Type; using llvm::SmallVector_Type; + SmallVector_Type* SV = new SmallVector_Type; Py_ssize_t size = PyTuple_Size(args); for (Py_ssize_t i = 0; i < size; ++i) { PyObject* cap = PyTuple_GetItem(args, i); + if (!cap) { + return NULL; + } Type* type = (Type*)PyCapsule_GetPointer(cap, "llvm::Type"); + if (!type) { + return NULL; + } SV->push_back(type); } return pycapsule_new(SV, "llvm::SmallVector_Type"); @@ -17,7 +38,8 @@ PyObject* small_vector_from_types(PyObject* self, PyObject* args) { static PyMethodDef extra_methodtable[] = { #define method(func) { #func, (PyCFunction)func, METH_VARARGS, NULL } -method( small_vector_from_types ), +method( make_raw_ostream_for_printing ), +method( make_small_vector_from_types ), { NULL } #undef method }; diff --git a/newbinding/src/Module.py b/newbinding/src/Module.py index 5923859..67ac8b0 100644 --- a/newbinding/src/Module.py +++ b/newbinding/src/Module.py @@ -2,7 +2,9 @@ from binding import * from namespace import llvm from LLVMContext import LLVMContext from StringRef import StringRef -from raw_ostream import raw_svector_ostream_helper +from Constant import Constant +from DerivedTypes import FunctionType +from raw_ostream import raw_ostream from AssemblyAnnotationWriter import AssemblyAnnotationWriter # class Module @@ -34,13 +36,10 @@ setModuleInlineAsm = Module.method(Void, StringRef.From(str)) appendModuleInlineAsm = Module.method(Void, StringRef.From(str)) # Function Accessors -#getOrInsertFunction = Module.method(Constant, StringRef.From(str), FunctionType.Pointer) - - +getOrInsertFunction = Module.method(Constant.Pointer, StringRef.From(str), FunctionType.Pointer) # Utilities dump = Module.method(Void) -print_ = Module.method(Void, raw_svector_ostream_helper.Ref, - AssemblyAnnotationWriter.Pointer) +print_ = Module.method(Void, raw_ostream.Ref, AssemblyAnnotationWriter.Pointer) print_.realname = 'print' dropAllReferences = Module.method(Void) diff --git a/newbinding/src/Type.py b/newbinding/src/Type.py index 19f6d61..3703200 100644 --- a/newbinding/src/Type.py +++ b/newbinding/src/Type.py @@ -1,7 +1,7 @@ from binding import * from namespace import llvm from LLVMContext import LLVMContext -from raw_ostream import raw_svector_ostream_helper +from raw_ostream import raw_ostream Type = llvm.Class() Type.include.add('llvm/Type.h') @@ -13,7 +13,7 @@ PointerType = SequentialType.Subclass() getContext = Type.method(LLVMContext.Ref) dump = Type.method(Void) -print_ = Type.method(Void, raw_svector_ostream_helper.Ref) +print_ = Type.method(Void, raw_ostream.Ref) print_.realname = 'print' def type_checker(): diff --git a/newbinding/src/raw_ostream.py b/newbinding/src/raw_ostream.py index 0275db4..64e8046 100644 --- a/newbinding/src/raw_ostream.py +++ b/newbinding/src/raw_ostream.py @@ -5,13 +5,8 @@ from StringRef import StringRef raw_ostream = llvm.Class() raw_ostream.include.add("llvm/Support/raw_ostream.h") +delete = raw_ostream.delete() raw_svector_ostream = raw_ostream.Subclass() raw_svector_ostream.include.add("llvm/Support/raw_os_ostream.h") - -# extra class to help binding -raw_svector_ostream_helper = raw_svector_ostream.Subclass() -create = raw_svector_ostream_helper.staticmethod( - raw_svector_ostream_helper.Pointer) -delete = raw_svector_ostream_helper.delete() -str = raw_svector_ostream_helper.method(StringRef.To(str)) +str = raw_svector_ostream.method(StringRef.To(str)) diff --git a/newbinding/test2.py b/newbinding/test2.py index 6f087c2..b866f59 100644 --- a/newbinding/test2.py +++ b/newbinding/test2.py @@ -15,7 +15,7 @@ assert m.getPointerSize() == api.Module.PointerSize.AnyPointerSize m.dump() -os = api.raw_svector_ostream_helper.create() +os = api.make_raw_ostream_for_printing() m.print_(os, None) print os.str() @@ -29,10 +29,14 @@ fnty = api.FunctionType.get(int1ty, False) fnty.dump() types = [int1ty, api.Type.getIntNTy(context, 21)] -svt = api.small_vector_from_types(*types) +svt = api.make_small_vector_from_types(*types) fnty = api.FunctionType.get(int1ty, svt, False) -os2 = api.raw_svector_ostream_helper.create() -fnty.print_(os2) -print os2.str() +os = api.make_raw_ostream_for_printing() +fnty.print_(os) +print os.str() +fn = m.getOrInsertFunction("foo", fnty) +os = api.make_raw_ostream_for_printing() +fn.print_(os, None) +print os.str()