Initing with 0.1+some changes.

git-svn-id: http://llvm-py.googlecode.com/svn/trunk@2 8d1e9007-1d4e-0410-b67e-1979fd6579aa
This commit is contained in:
mdevan.foobar 2008-05-27 08:50:46 +00:00
commit 37f3a30a4a
13 changed files with 3593 additions and 0 deletions

19
CHANGELOG Normal file
View file

@ -0,0 +1,19 @@
0.1, 10-May-2008:
* Initial release.
0.2, ongoing:
* Independent package, need not be unpacked into llvm/bindings
* Modules can be dumped to a string
* Modules can be verified
* Module.global_variables, Module.functions, Function.args,
Function.basic_blocks and BasicBlock.instructions are
now proper iterators (generators in fact) than plain lists
* MemoryBuffer is done
* TypeHandle is done
* Unit tester added (but doesn't do much for now)

26
LICENSE Normal file
View file

@ -0,0 +1,26 @@
Copyright (c) 2008, Mahadevan R All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the Mahadevan R, llvm-py nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

37
README Normal file
View file

@ -0,0 +1,37 @@
llvm-py: Python Bindings for LLVM
---------------------------------
Home page:
----------
http://mdevan.nfshost.com/llvm-py.html
http://code.google.com/p/llvm-py/
Quickstart:
----------
1. Get 2.4svn version of LLVM, build it. 2.3 or older will *not* work.
LLVM need not be installed.
2. Unpack llvm-py, build and install:
$ tar jxvf llvm-py-0.2.tar.bz2
$ cd llvm-py-0.2
# if you've installed LLVM, and llvm-config is in the path
$ sudo python setup.py install
# if you've just built LLVM and not installed it, locate llvm-config,
# usually under ../llvm/Release/bin
$ sudo python setup.py install --llvm-config=/path/to/llvm-config
3. See examples under 'test' directory.
LICENSE:
--------
llvm-py is distributed under the new BSD license, which is similar to
the LLVM license itself. See the file called LICENSE for the full license
text.

0
llvm/__init__.py Normal file
View file

932
llvm/_core.c Normal file
View file

@ -0,0 +1,932 @@
/* our includes */
#include "wrap.h"
#include "extra.h"
/* libc includes */
#include <stdarg.h> /* for malloc(), free() */
/*===----------------------------------------------------------------------===*/
/* Modules */
/*===----------------------------------------------------------------------===*/
static PyObject *
_wLLVMModuleCreateWithName(PyObject *self, PyObject *args)
{
const char *s;
LLVMModuleRef module;
if (!PyArg_ParseTuple(args, "s", &s))
return NULL;
module = LLVMModuleCreateWithName(s);
return ctor_LLVMModuleRef(module);
}
_wrap_obj2str(LLVMGetDataLayout, LLVMModuleRef)
_wrap_objstr2none(LLVMSetDataLayout, LLVMModuleRef)
_wrap_obj2str(LLVMGetTarget, LLVMModuleRef)
_wrap_objstr2none(LLVMSetTarget, LLVMModuleRef)
_wrap_objstrobj2obj(LLVMAddTypeName, LLVMModuleRef, LLVMTypeRef, int)
_wrap_objstr2none(LLVMDeleteTypeName, LLVMModuleRef)
_wrap_obj2none(LLVMDumpModule, LLVMModuleRef)
_wrap_obj2none(LLVMDisposeModule, LLVMModuleRef)
_wrap_dumper(LLVMDumpModuleToString, LLVMModuleRef)
static PyObject *
_wLLVMVerifyModule(PyObject *self, PyObject *args)
{
PyObject *obj;
char *outmsg = 0;
PyObject *ret;
LLVMModuleRef m;
if (!PyArg_ParseTuple(args, "O", &obj))
return NULL;
m = (LLVMModuleRef) PyCObject_AsVoidPtr(obj);
(void) LLVMVerifyModule(m, LLVMReturnStatusAction, &outmsg);
if (outmsg) {
ret = PyString_FromString(outmsg);
LLVMDisposeMessage(outmsg);
} else {
ret = PyString_FromString("");
}
return ret;
}
/*===----------------------------------------------------------------------===*/
/* Types */
/*===----------------------------------------------------------------------===*/
/*===-- General ----------------------------------------------------------===*/
_wrap_obj2obj(LLVMGetTypeKind, LLVMTypeRef, int)
_wrap_dumper(LLVMDumpTypeToString, LLVMTypeRef)
/*===-- Integer types ----------------------------------------------------===*/
_wrap_none2obj(LLVMInt1Type, LLVMTypeRef)
_wrap_none2obj(LLVMInt8Type, LLVMTypeRef)
_wrap_none2obj(LLVMInt16Type, LLVMTypeRef)
_wrap_none2obj(LLVMInt32Type, LLVMTypeRef)
_wrap_none2obj(LLVMInt64Type, LLVMTypeRef)
_wrap_int2obj(LLVMIntType, LLVMTypeRef)
_wrap_obj2obj(LLVMGetIntTypeWidth, LLVMTypeRef, int)
/*===-- Floating-point types ---------------------------------------------===*/
_wrap_none2obj(LLVMFloatType, LLVMTypeRef)
_wrap_none2obj(LLVMDoubleType, LLVMTypeRef)
_wrap_none2obj(LLVMX86FP80Type, LLVMTypeRef)
_wrap_none2obj(LLVMFP128Type, LLVMTypeRef)
_wrap_none2obj(LLVMPPCFP128Type, LLVMTypeRef)
/*===-- Function types ---------------------------------------------------===*/
_wrap_objlistint2obj(LLVMFunctionType, LLVMTypeRef, LLVMTypeRef, LLVMTypeRef)
_wrap_obj2obj(LLVMIsFunctionVarArg, LLVMTypeRef, int)
_wrap_obj2obj(LLVMGetReturnType, LLVMTypeRef, LLVMTypeRef)
_wrap_obj2obj(LLVMCountParamTypes, LLVMTypeRef, int)
/* The LLVMGetParamTypes and LLVMGetStructElementTypes functions both
* have the same signatures. The following implementation takes advantage
* of this.
*/
typedef void (*obj2arr_fn_t)(LLVMTypeRef ty, LLVMTypeRef *outv);
typedef unsigned (*arrcnt_fn_t)(LLVMTypeRef ty);
static PyObject *
obj2arr(PyObject *self, PyObject *args, arrcnt_fn_t cntfunc, obj2arr_fn_t arrfunc)
{
LLVMTypeRef type, *param_types;
unsigned param_count;
PyObject *list;
/* get the function object ptr */
if (!(type = (LLVMTypeRef)get_object_arg(args)))
return NULL;
/* get param count */
param_count = cntfunc(type);
/* alloc enough space for all of them */
if (!(param_types = (LLVMTypeRef *)malloc(sizeof(LLVMTypeRef) * param_count)))
return PyErr_NoMemory();
/* call LLVM func */
arrfunc(type, param_types);
/* create a list from the array */
list = make_list_from_LLVMTypeRef_array(param_types, param_count);
/* free temp storage */
free(param_types);
return list;
}
static PyObject *
_wLLVMGetFunctionTypeParams(PyObject *self, PyObject *args)
{
return obj2arr(self, args, LLVMCountParamTypes, LLVMGetParamTypes);
}
/*===-- Struct types -----------------------------------------------------===*/
_wrap_listint2obj(LLVMStructType, LLVMTypeRef, LLVMTypeRef)
_wrap_obj2obj(LLVMCountStructElementTypes, LLVMTypeRef, int)
static PyObject *
_wLLVMGetStructElementTypes(PyObject *self, PyObject *args)
{
return obj2arr(self, args, LLVMCountStructElementTypes, LLVMGetStructElementTypes);
}
_wrap_obj2obj(LLVMIsPackedStruct, LLVMTypeRef, int)
/*===-- Array types ------------------------------------------------------===*/
_wrap_objint2obj(LLVMArrayType, LLVMTypeRef, LLVMTypeRef)
_wrap_obj2obj(LLVMGetElementType, LLVMTypeRef, LLVMTypeRef)
_wrap_obj2obj(LLVMGetArrayLength, LLVMTypeRef, int)
/*===-- Pointer types ----------------------------------------------------===*/
_wrap_objint2obj(LLVMPointerType, LLVMTypeRef, LLVMTypeRef)
_wrap_obj2obj(LLVMGetPointerAddressSpace, LLVMTypeRef, int)
/*===-- Vector type ------------------------------------------------------===*/
_wrap_objint2obj(LLVMVectorType, LLVMTypeRef, LLVMTypeRef)
_wrap_obj2obj(LLVMGetVectorSize, LLVMTypeRef, int)
/*===-- Other types ------------------------------------------------------===*/
_wrap_none2obj(LLVMVoidType, LLVMTypeRef)
_wrap_none2obj(LLVMLabelType, LLVMTypeRef)
_wrap_none2obj(LLVMOpaqueType, LLVMTypeRef)
/*===-- Type handles -----------------------------------------------------===*/
_wrap_obj2obj(LLVMCreateTypeHandle, LLVMTypeRef, LLVMTypeHandleRef)
_wrap_objobj2none(LLVMRefineType, LLVMTypeRef, LLVMTypeRef)
_wrap_obj2obj(LLVMResolveTypeHandle, LLVMTypeHandleRef, LLVMTypeRef)
_wrap_obj2none(LLVMDisposeTypeHandle, LLVMTypeHandleRef)
/*===----------------------------------------------------------------------===*/
/* Values */
/*===----------------------------------------------------------------------===*/
/* Operations on all values */
_wrap_obj2obj(LLVMTypeOf, LLVMValueRef, LLVMTypeRef)
_wrap_obj2str(LLVMGetValueName, LLVMValueRef)
_wrap_objstr2none(LLVMSetValueName, LLVMValueRef)
_wrap_obj2none(LLVMDumpValue, LLVMValueRef)
_wrap_dumper(LLVMDumpValueToString, LLVMValueRef)
/*===-- Constant Values --------------------------------------------------===*/
/* Operations on constants of any type */
_wrap_obj2obj(LLVMConstNull, LLVMTypeRef, LLVMValueRef)
_wrap_obj2obj(LLVMConstAllOnes, LLVMTypeRef, LLVMValueRef)
_wrap_obj2obj(LLVMGetUndef, LLVMTypeRef, LLVMValueRef)
_wrap_obj2obj(LLVMIsConstant, LLVMValueRef, int)
_wrap_obj2obj(LLVMIsNull, LLVMValueRef, int)
_wrap_obj2obj(LLVMIsUndef, LLVMValueRef, int)
/* Operations on scalar constants */
static PyObject *
_wLLVMConstInt(PyObject *self, PyObject *args)
{
PyObject *obj;
unsigned long long n;
int sign_extend;
LLVMTypeRef ty;
LLVMValueRef val;
if (!PyArg_ParseTuple(args, "OKi", &obj, &n, &sign_extend))
return NULL;
ty = (LLVMTypeRef)(PyCObject_AsVoidPtr(obj));
val = LLVMConstInt(ty, n, sign_extend);
return ctor_LLVMValueRef(val);
}
static PyObject *
_wLLVMConstReal(PyObject *self, PyObject *args)
{
PyObject *obj;
double d;
LLVMTypeRef ty;
LLVMValueRef val;
if (!PyArg_ParseTuple(args, "Od", &obj, &d))
return NULL;
ty = (LLVMTypeRef)(PyCObject_AsVoidPtr(obj));
val = LLVMConstReal(ty, d);
return ctor_LLVMValueRef(val);
}
_wrap_objstr2obj(LLVMConstRealOfString, LLVMTypeRef, LLVMValueRef)
/* Operations on composite constants */
static PyObject *
_wLLVMConstString(PyObject *self, PyObject *args)
{
const char *s;
int dont_null_terminate;
LLVMValueRef val;
if (!PyArg_ParseTuple(args, "si", &s, &dont_null_terminate))
return NULL;
val = LLVMConstString(s, strlen(s), dont_null_terminate);
return ctor_LLVMValueRef(val);
}
_wrap_objlist2obj(LLVMConstArray, LLVMTypeRef, LLVMValueRef, LLVMValueRef)
_wrap_listint2obj(LLVMConstStruct, LLVMValueRef, LLVMValueRef)
_wrap_list2obj(LLVMConstVector, LLVMValueRef, LLVMValueRef)
/* Constant expressions */
_wrap_obj2obj(LLVMSizeOf, LLVMTypeRef, LLVMValueRef)
_wrap_obj2obj(LLVMConstNeg, LLVMValueRef, LLVMValueRef)
_wrap_obj2obj(LLVMConstNot, LLVMValueRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstAdd, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstSub, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstMul, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstUDiv, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstSDiv, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstFDiv, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstURem, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstSRem, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstFRem, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstAnd, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstOr, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstXor, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_intobjobj2obj(LLVMConstICmp, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_intobjobj2obj(LLVMConstFCmp, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstShl, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstLShr, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstAShr, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objlist2obj(LLVMConstGEP, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstTrunc, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstSExt, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstZExt, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstFPTrunc, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstFPExt, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstUIToFP, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstSIToFP, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstFPToUI, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstFPToSI, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstPtrToInt, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstIntToPtr, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstBitCast, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobjobj2obj(LLVMConstSelect, LLVMValueRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobj2obj(LLVMConstExtractElement, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjobj2obj(LLVMConstInsertElement, LLVMValueRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjobj2obj(LLVMConstShuffleVector, LLVMValueRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
/*===----------------------------------------------------------------------===*/
/* Globals */
/*===----------------------------------------------------------------------===*/
/*===-- Globals ----------------------------------------------------------===*/
_wrap_obj2obj(LLVMGetGlobalParent, LLVMValueRef, LLVMModuleRef)
_wrap_obj2obj(LLVMIsDeclaration, LLVMValueRef, int)
_wrap_obj2obj(LLVMGetLinkage, LLVMValueRef, int)
_wrap_objint2none(LLVMSetLinkage, LLVMValueRef)
_wrap_obj2str(LLVMGetSection, LLVMValueRef)
_wrap_objstr2none(LLVMSetSection, LLVMValueRef)
_wrap_obj2obj(LLVMGetVisibility, LLVMValueRef, int)
_wrap_objint2none(LLVMSetVisibility, LLVMValueRef)
_wrap_obj2obj(LLVMGetAlignment, LLVMValueRef, int)
_wrap_objint2none(LLVMSetAlignment, LLVMValueRef)
/*===-- Global Variables -------------------------------------------------===*/
_wrap_objobjstr2obj(LLVMAddGlobal, LLVMModuleRef, LLVMTypeRef, LLVMValueRef)
_wrap_objstr2obj(LLVMGetNamedGlobal, LLVMModuleRef, LLVMValueRef)
_wrap_obj2obj(LLVMGetFirstGlobal, LLVMModuleRef, LLVMValueRef)
_wrap_obj2obj(LLVMGetNextGlobal, LLVMValueRef, LLVMValueRef)
_wrap_obj2none(LLVMDeleteGlobal, LLVMValueRef)
_wrap_obj2obj(LLVMHasInitializer, LLVMValueRef, int)
_wrap_obj2obj(LLVMGetInitializer, LLVMValueRef, LLVMValueRef)
_wrap_objobj2none(LLVMSetInitializer, LLVMValueRef, LLVMValueRef)
_wrap_objint2none(LLVMSetThreadLocal, LLVMValueRef)
_wrap_objint2none(LLVMSetGlobalConstant, LLVMValueRef)
_wrap_obj2obj(LLVMIsThreadLocal, LLVMValueRef, int)
_wrap_obj2obj(LLVMIsGlobalConstant, LLVMValueRef, int)
/*===-- Functions --------------------------------------------------------===*/
_wrap_objstrobj2obj(LLVMAddFunction, LLVMModuleRef, LLVMTypeRef, LLVMValueRef)
_wrap_objstr2obj(LLVMGetNamedFunction, LLVMModuleRef, LLVMValueRef)
_wrap_obj2obj(LLVMGetFirstFunction, LLVMModuleRef, LLVMValueRef)
_wrap_obj2obj(LLVMGetNextFunction, LLVMValueRef, LLVMValueRef)
_wrap_obj2none(LLVMDeleteFunction, LLVMValueRef)
_wrap_obj2obj(LLVMGetIntrinsicID, LLVMValueRef, int)
_wrap_obj2obj(LLVMGetFunctionCallConv, LLVMValueRef, int)
_wrap_objint2none(LLVMSetFunctionCallConv, LLVMValueRef)
_wrap_obj2str(LLVMGetCollector, LLVMValueRef)
_wrap_objstr2none(LLVMSetCollector, LLVMValueRef)
_wrap_objint2obj(LLVMVerifyFunction, LLVMValueRef, int)
/*===-- Arguments --------------------------------------------------------===*/
_wrap_obj2obj(LLVMCountParams, LLVMValueRef, int)
_wrap_obj2obj(LLVMGetFirstParam, LLVMValueRef, LLVMValueRef)
_wrap_obj2obj(LLVMGetNextParam, LLVMValueRef, LLVMValueRef)
_wrap_obj2obj(LLVMGetParamParent, LLVMValueRef, LLVMValueRef)
_wrap_objint2none(LLVMAddParamAttr, LLVMValueRef)
_wrap_objint2none(LLVMRemoveParamAttr, LLVMValueRef)
_wrap_objint2none(LLVMSetParamAlignment, LLVMValueRef)
/*===-- Basic Blocks -----------------------------------------------------===*/
_wrap_obj2obj(LLVMGetBasicBlockParent, LLVMBasicBlockRef, LLVMValueRef)
_wrap_obj2obj(LLVMCountBasicBlocks, LLVMValueRef, int)
_wrap_obj2obj(LLVMGetFirstBasicBlock, LLVMValueRef, LLVMBasicBlockRef)
_wrap_obj2obj(LLVMGetNextBasicBlock, LLVMBasicBlockRef, LLVMBasicBlockRef)
_wrap_obj2obj(LLVMGetEntryBasicBlock, LLVMValueRef, LLVMBasicBlockRef)
_wrap_objstr2obj(LLVMAppendBasicBlock, LLVMValueRef, LLVMBasicBlockRef)
_wrap_objstr2obj(LLVMInsertBasicBlock, LLVMBasicBlockRef, LLVMBasicBlockRef)
_wrap_obj2none(LLVMDeleteBasicBlock, LLVMBasicBlockRef)
/*===-- Instructions -----------------------------------------------------===*/
_wrap_obj2obj(LLVMGetInstructionParent, LLVMValueRef, LLVMBasicBlockRef)
_wrap_obj2obj(LLVMGetFirstInstruction, LLVMBasicBlockRef, LLVMValueRef)
_wrap_obj2obj(LLVMGetNextInstruction, LLVMValueRef, LLVMValueRef)
/*===-- Call Sites (Call or Invoke) --------------------------------------===*/
_wrap_objint2none(LLVMSetInstructionCallConv, LLVMValueRef)
_wrap_obj2obj(LLVMGetInstructionCallConv, LLVMValueRef, int)
_wrap_objintint2none(LLVMAddInstrParamAttr, LLVMValueRef)
_wrap_objintint2none(LLVMRemoveInstrParamAttr, LLVMValueRef)
_wrap_objintint2none(LLVMSetInstrParamAlignment, LLVMValueRef)
/*===-- PHI Nodes --------------------------------------------------------===*/
static void LLVMAddIncoming1(LLVMValueRef PhiNode, LLVMValueRef IncomingValue, LLVMBasicBlockRef IncomingBlock)
{
LLVMAddIncoming(PhiNode, &IncomingValue, &IncomingBlock, 1);
}
_wrap_objobjobj2none(LLVMAddIncoming1, LLVMValueRef, LLVMValueRef, LLVMBasicBlockRef)
_wrap_obj2obj(LLVMCountIncoming, LLVMValueRef, int)
_wrap_objint2obj(LLVMGetIncomingValue, LLVMValueRef, LLVMValueRef)
_wrap_objint2obj(LLVMGetIncomingBlock, LLVMValueRef, LLVMBasicBlockRef)
/*===-- Instruction builders ----------------------------------------------===*/
_wrap_none2obj(LLVMCreateBuilder, LLVMBuilderRef)
static PyObject *
_wLLVMPositionBuilder(PyObject *self, PyObject *args)
{
PyObject *obj1, *obj2, *obj3 = 0;
LLVMBuilderRef builder;
LLVMBasicBlockRef block;
LLVMValueRef instr = NULL;
if (!PyArg_ParseTuple(args, "OO|O", &obj1, &obj2, &obj3))
return NULL;
builder = (LLVMBuilderRef) (PyCObject_AsVoidPtr(obj1));
block = (LLVMBasicBlockRef)(PyCObject_AsVoidPtr(obj2));
if (obj3) /* optional */
instr = (LLVMValueRef)(PyCObject_AsVoidPtr(obj3));
LLVMPositionBuilder(builder, block, instr);
Py_RETURN_NONE;
}
_wrap_objobj2none(LLVMPositionBuilderBefore, LLVMBuilderRef, LLVMValueRef)
_wrap_objobj2none(LLVMPositionBuilderAtEnd, LLVMBuilderRef, LLVMBasicBlockRef)
_wrap_obj2obj(LLVMGetInsertBlock, LLVMBuilderRef, LLVMBasicBlockRef)
_wrap_obj2none(LLVMDisposeBuilder, LLVMBuilderRef)
/* Terminators */
_wrap_obj2obj(LLVMBuildRetVoid, LLVMBuilderRef, LLVMValueRef)
_wrap_objobj2obj(LLVMBuildRet, LLVMBuilderRef, LLVMValueRef, LLVMValueRef)
_wrap_objobj2obj(LLVMBuildBr, LLVMBuilderRef, LLVMBasicBlockRef, LLVMValueRef)
_wrap_objobjobjobj2obj(LLVMBuildCondBr, LLVMBuilderRef, LLVMValueRef, LLVMBasicBlockRef, LLVMBasicBlockRef, LLVMValueRef)
_wrap_objobjobjint2obj(LLVMBuildSwitch, LLVMBuilderRef, LLVMValueRef, LLVMBasicBlockRef, LLVMValueRef)
static PyObject *
_wLLVMBuildInvoke(PyObject *self, PyObject *args)
{
PyObject *obj1, *obj2, *obj3, *obj4, *obj5;
const char *name;
LLVMBuilderRef builder;
LLVMValueRef func;
LLVMValueRef *fnargs;
unsigned fnarg_count;
LLVMBasicBlockRef then_blk, catch_blk;
LLVMValueRef inst;
if (!PyArg_ParseTuple(args, "OOOOOs", &obj1, &obj2, &obj3, &obj4, &obj5, &name))
return NULL;
builder = (LLVMBuilderRef)(PyCObject_AsVoidPtr(obj1));
func = (LLVMValueRef)(PyCObject_AsVoidPtr(obj2));
fnarg_count = (unsigned) PyList_Size(obj3);
fnargs = (LLVMValueRef *)make_array_from_list(obj3, fnarg_count);
if (!fnargs)
return PyErr_NoMemory();
then_blk = (LLVMBasicBlockRef)(PyCObject_AsVoidPtr(obj4));
catch_blk = (LLVMBasicBlockRef)(PyCObject_AsVoidPtr(obj5));
inst = LLVMBuildInvoke(builder, func, fnargs, fnarg_count, then_blk, catch_blk, name);
free(fnargs);
return ctor_LLVMValueRef(inst);
}
_wrap_obj2obj(LLVMBuildUnwind, LLVMBuilderRef, LLVMValueRef)
_wrap_obj2obj(LLVMBuildUnreachable, LLVMBuilderRef, LLVMValueRef)
/* Add a case to the switch instruction */
_wrap_objobjobj2none(LLVMAddCase, LLVMValueRef, LLVMValueRef, LLVMBasicBlockRef)
/* Arithmetic */
_wrap_objobjobjstr2obj(LLVMBuildAdd, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildSub, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildMul, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildUDiv, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildSDiv, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildFDiv, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildURem, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildSRem, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildFRem, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildShl, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildLShr, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildAShr, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildAnd, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildOr, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildXor, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjstr2obj(LLVMBuildNeg, LLVMBuilderRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjstr2obj(LLVMBuildNot, LLVMBuilderRef, LLVMValueRef, LLVMValueRef)
/* Memory */
_wrap_objobjstr2obj(LLVMBuildMalloc, LLVMBuilderRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildArrayMalloc, LLVMBuilderRef, LLVMTypeRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjstr2obj(LLVMBuildAlloca, LLVMBuilderRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildArrayAlloca, LLVMBuilderRef, LLVMTypeRef, LLVMValueRef, LLVMValueRef)
_wrap_objobj2obj(LLVMBuildFree, LLVMBuilderRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjstr2obj(LLVMBuildLoad, LLVMBuilderRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjobj2obj(LLVMBuildStore, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjliststr2obj(LLVMBuildGEP, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
/* Casts */
_wrap_objobjobjstr2obj(LLVMBuildTrunc, LLVMBuilderRef, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildZExt, LLVMBuilderRef, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildSExt, LLVMBuilderRef, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildFPToUI, LLVMBuilderRef, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildFPToSI, LLVMBuilderRef, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildUIToFP, LLVMBuilderRef, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildSIToFP, LLVMBuilderRef, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildFPTrunc, LLVMBuilderRef, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildFPExt, LLVMBuilderRef, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildPtrToInt, LLVMBuilderRef, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildIntToPtr, LLVMBuilderRef, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildBitCast, LLVMBuilderRef, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
/* Comparisons */
_wrap_objintobjobjstr2obj(LLVMBuildICmp, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objintobjobjstr2obj(LLVMBuildFCmp, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
/* Miscellaneous instructions */
_wrap_objobjstr2obj(LLVMBuildPhi, LLVMBuilderRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobjliststr2obj(LLVMBuildCall, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjobjobjstr2obj(LLVMBuildSelect, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildVAArg, LLVMBuilderRef, LLVMValueRef, LLVMTypeRef, LLVMValueRef)
_wrap_objobjobjstr2obj(LLVMBuildExtractElement, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjobjobjstr2obj(LLVMBuildInsertElement, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
_wrap_objobjobjobjstr2obj(LLVMBuildShuffleVector, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef, LLVMValueRef)
/*===----------------------------------------------------------------------===*/
/* Modules Providers */
/*===----------------------------------------------------------------------===*/
_wrap_obj2obj(LLVMCreateModuleProviderForExistingModule, LLVMModuleRef, LLVMModuleProviderRef)
_wrap_obj2none(LLVMDisposeModuleProvider, LLVMModuleProviderRef)
/*===----------------------------------------------------------------------===*/
/* Memory Buffer */
/*===----------------------------------------------------------------------===*/
static PyObject *
_wLLVMCreateMemoryBufferWithContentsOfFile(PyObject *self, PyObject *args)
{
const char *path;
LLVMMemoryBufferRef ref;
char *outmsg;
PyObject *ret;
if (!PyArg_ParseTuple(args, "s", &path))
return NULL;
if (!LLVMCreateMemoryBufferWithContentsOfFile(path, &ref, &outmsg)) {
ret = ctor_LLVMMemoryBufferRef(ref);
} else {
ret = PyString_FromString(outmsg);
LLVMDisposeMessage(outmsg);
}
return ret;
}
static PyObject *
_wLLVMCreateMemoryBufferWithSTDIN(PyObject *self, PyObject *args)
{
LLVMMemoryBufferRef ref;
char *outmsg;
PyObject *ret;
if (!LLVMCreateMemoryBufferWithSTDIN(&ref, &outmsg)) {
ret = ctor_LLVMMemoryBufferRef(ref);
} else {
ret = PyString_FromString(outmsg);
LLVMDisposeMessage(outmsg);
}
return ret;
}
_wrap_obj2none(LLVMDisposeMemoryBuffer, LLVMMemoryBufferRef)
/*===----------------------------------------------------------------------===*/
/* Pass Manager */
/*===----------------------------------------------------------------------===*/
_wrap_none2obj(LLVMCreatePassManager, LLVMPassManagerRef)
_wrap_obj2obj(LLVMCreateFunctionPassManager, LLVMModuleProviderRef, LLVMPassManagerRef)
_wrap_objobj2obj(LLVMRunPassManager, LLVMPassManagerRef, LLVMModuleRef, int)
_wrap_obj2obj(LLVMInitializeFunctionPassManager, LLVMPassManagerRef, int)
_wrap_objobj2obj(LLVMRunFunctionPassManager, LLVMPassManagerRef, LLVMValueRef, int)
_wrap_obj2obj(LLVMFinalizeFunctionPassManager, LLVMPassManagerRef, int)
_wrap_obj2none(LLVMDisposePassManager, LLVMPassManagerRef)
/*===----------------------------------------------------------------------===*/
/* Python member method table */
/*===----------------------------------------------------------------------===*/
#define _method( func ) { # func , _w ## func , METH_VARARGS },
static PyMethodDef core_methods[] = {
/* Modules */
_method( LLVMModuleCreateWithName )
_method( LLVMGetDataLayout )
_method( LLVMSetDataLayout )
_method( LLVMGetTarget )
_method( LLVMSetTarget )
_method( LLVMAddTypeName )
_method( LLVMDeleteTypeName )
_method( LLVMDumpModule )
_method( LLVMDisposeModule )
_method( LLVMDumpModuleToString )
_method( LLVMVerifyModule )
/* Types */
/* General */
_method( LLVMGetTypeKind )
_method( LLVMDumpTypeToString )
/* Integer types */
_method( LLVMInt1Type )
_method( LLVMInt8Type )
_method( LLVMInt16Type )
_method( LLVMInt32Type )
_method( LLVMInt64Type )
_method( LLVMIntType )
_method( LLVMGetIntTypeWidth )
/* Floating-point types */
_method( LLVMFloatType )
_method( LLVMDoubleType )
_method( LLVMX86FP80Type )
_method( LLVMFP128Type )
_method( LLVMPPCFP128Type )
/* Function types */
_method( LLVMFunctionType )
_method( LLVMIsFunctionVarArg )
_method( LLVMGetReturnType )
_method( LLVMCountParamTypes )
_method( LLVMGetFunctionTypeParams )
/* Struct types */
_method( LLVMStructType )
_method( LLVMCountStructElementTypes )
_method( LLVMGetStructElementTypes )
_method( LLVMIsPackedStruct )
/* Array types */
_method( LLVMArrayType )
_method( LLVMGetElementType )
_method( LLVMGetArrayLength )
/* Pointer types */
_method( LLVMPointerType )
_method( LLVMGetPointerAddressSpace )
/* Vector type */
_method( LLVMVectorType )
_method( LLVMGetVectorSize )
/* Other types */
_method( LLVMVoidType )
_method( LLVMLabelType )
_method( LLVMOpaqueType )
/* Type handles */
_method( LLVMCreateTypeHandle )
_method( LLVMRefineType )
_method( LLVMResolveTypeHandle )
_method( LLVMDisposeTypeHandle )
/* Values */
/* Operations on all values */
_method( LLVMTypeOf )
_method( LLVMGetValueName )
_method( LLVMSetValueName )
_method( LLVMDumpValue )
_method( LLVMDumpValueToString )
/* Constant Values */
/* Operations on constants of any type */
_method( LLVMConstNull )
_method( LLVMConstAllOnes )
_method( LLVMGetUndef )
_method( LLVMIsConstant )
_method( LLVMIsNull )
_method( LLVMIsUndef )
/* Operations on scalar constants */
_method( LLVMConstInt )
_method( LLVMConstReal )
_method( LLVMConstRealOfString )
/* Operations on composite constants */
_method( LLVMConstString )
_method( LLVMConstArray )
_method( LLVMConstStruct )
_method( LLVMConstVector )
/* Constant expressions */
_method( LLVMSizeOf )
_method( LLVMConstNeg )
_method( LLVMConstNot )
_method( LLVMConstAdd )
_method( LLVMConstSub )
_method( LLVMConstMul )
_method( LLVMConstUDiv )
_method( LLVMConstSDiv )
_method( LLVMConstFDiv )
_method( LLVMConstURem )
_method( LLVMConstSRem )
_method( LLVMConstFRem )
_method( LLVMConstAnd )
_method( LLVMConstOr )
_method( LLVMConstXor )
_method( LLVMConstICmp )
_method( LLVMConstFCmp )
_method( LLVMConstShl )
_method( LLVMConstLShr )
_method( LLVMConstAShr )
_method( LLVMConstGEP )
_method( LLVMConstTrunc )
_method( LLVMConstSExt )
_method( LLVMConstZExt )
_method( LLVMConstFPTrunc )
_method( LLVMConstFPExt )
_method( LLVMConstUIToFP )
_method( LLVMConstSIToFP )
_method( LLVMConstFPToUI )
_method( LLVMConstFPToSI )
_method( LLVMConstPtrToInt )
_method( LLVMConstIntToPtr )
_method( LLVMConstBitCast )
_method( LLVMConstSelect )
_method( LLVMConstExtractElement )
_method( LLVMConstInsertElement )
_method( LLVMConstShuffleVector )
/* Globals */
/* Globals (general) */
_method( LLVMGetGlobalParent )
_method( LLVMIsDeclaration )
_method( LLVMGetLinkage )
_method( LLVMSetLinkage )
_method( LLVMGetSection )
_method( LLVMSetSection )
_method( LLVMGetVisibility )
_method( LLVMSetVisibility )
_method( LLVMGetAlignment )
_method( LLVMSetAlignment )
/* Global Variables */
_method( LLVMAddGlobal )
_method( LLVMGetNamedGlobal )
_method( LLVMGetFirstGlobal )
_method( LLVMGetNextGlobal )
_method( LLVMDeleteGlobal )
_method( LLVMHasInitializer )
_method( LLVMGetInitializer )
_method( LLVMSetInitializer )
_method( LLVMSetThreadLocal )
_method( LLVMSetGlobalConstant )
_method( LLVMIsThreadLocal )
_method( LLVMIsGlobalConstant )
/* Functions */
_method( LLVMAddFunction )
_method( LLVMGetNamedFunction )
_method( LLVMGetFirstFunction )
_method( LLVMGetNextFunction )
_method( LLVMDeleteFunction )
_method( LLVMGetIntrinsicID )
_method( LLVMGetFunctionCallConv )
_method( LLVMSetFunctionCallConv )
_method( LLVMGetCollector )
_method( LLVMSetCollector )
_method( LLVMVerifyFunction )
/* Arguments */
_method( LLVMCountParams )
_method( LLVMGetFirstParam )
_method( LLVMGetNextParam )
_method( LLVMGetParamParent )
_method( LLVMAddParamAttr )
_method( LLVMRemoveParamAttr )
_method( LLVMSetParamAlignment )
/* Basic Blocks */
_method( LLVMGetBasicBlockParent )
_method( LLVMCountBasicBlocks )
_method( LLVMGetFirstBasicBlock )
_method( LLVMGetNextBasicBlock )
_method( LLVMGetEntryBasicBlock )
_method( LLVMAppendBasicBlock )
_method( LLVMInsertBasicBlock )
_method( LLVMDeleteBasicBlock )
/* Instructions */
_method( LLVMGetInstructionParent )
_method( LLVMGetFirstInstruction )
_method( LLVMGetNextInstruction )
/* Call Sites (Call or Invoke) */
_method( LLVMSetInstructionCallConv )
_method( LLVMGetInstructionCallConv )
_method( LLVMAddInstrParamAttr )
_method( LLVMRemoveInstrParamAttr )
_method( LLVMSetInstrParamAlignment )
/* PHI Nodes */
_method( LLVMAddIncoming1 )
_method( LLVMCountIncoming )
_method( LLVMGetIncomingValue )
_method( LLVMGetIncomingBlock )
/* Instruction builders */
_method( LLVMCreateBuilder )
_method( LLVMPositionBuilder )
_method( LLVMPositionBuilderBefore )
_method( LLVMPositionBuilderAtEnd )
_method( LLVMGetInsertBlock )
_method( LLVMDisposeBuilder )
/* Terminators */
_method( LLVMBuildRetVoid )
_method( LLVMBuildRet )
_method( LLVMBuildBr )
_method( LLVMBuildCondBr )
_method( LLVMBuildSwitch )
_method( LLVMBuildInvoke )
_method( LLVMBuildUnwind )
_method( LLVMBuildUnreachable )
/* Add a case to the switch instruction */
_method( LLVMAddCase )
/* Arithmetic */
_method( LLVMBuildAdd )
_method( LLVMBuildSub )
_method( LLVMBuildMul )
_method( LLVMBuildUDiv )
_method( LLVMBuildSDiv )
_method( LLVMBuildFDiv )
_method( LLVMBuildURem )
_method( LLVMBuildSRem )
_method( LLVMBuildFRem )
_method( LLVMBuildShl )
_method( LLVMBuildLShr )
_method( LLVMBuildAShr )
_method( LLVMBuildAnd )
_method( LLVMBuildOr )
_method( LLVMBuildXor )
_method( LLVMBuildNeg )
_method( LLVMBuildNot )
/* Memory */
_method( LLVMBuildMalloc )
_method( LLVMBuildArrayMalloc )
_method( LLVMBuildAlloca )
_method( LLVMBuildArrayAlloca )
_method( LLVMBuildFree )
_method( LLVMBuildLoad )
_method( LLVMBuildStore )
_method( LLVMBuildGEP )
/* Casts */
_method( LLVMBuildTrunc )
_method( LLVMBuildZExt )
_method( LLVMBuildSExt )
_method( LLVMBuildFPToUI )
_method( LLVMBuildFPToSI )
_method( LLVMBuildUIToFP )
_method( LLVMBuildSIToFP )
_method( LLVMBuildFPTrunc )
_method( LLVMBuildFPExt )
_method( LLVMBuildPtrToInt )
_method( LLVMBuildIntToPtr )
_method( LLVMBuildBitCast )
/* Comparisons */
_method( LLVMBuildICmp )
_method( LLVMBuildFCmp )
/* Miscellaneous instructions */
_method( LLVMBuildPhi )
_method( LLVMBuildCall )
_method( LLVMBuildSelect )
_method( LLVMBuildVAArg )
_method( LLVMBuildExtractElement )
_method( LLVMBuildInsertElement )
_method( LLVMBuildShuffleVector )
/* Modules Providers */
_method( LLVMCreateModuleProviderForExistingModule )
_method( LLVMDisposeModuleProvider )
/* Memory Buffer */
_method( LLVMCreateMemoryBufferWithContentsOfFile )
_method( LLVMCreateMemoryBufferWithSTDIN )
_method( LLVMDisposeMemoryBuffer )
/* Pass Manager */
_method( LLVMCreatePassManager )
_method( LLVMCreateFunctionPassManager )
_method( LLVMRunPassManager )
_method( LLVMInitializeFunctionPassManager )
_method( LLVMRunFunctionPassManager )
_method( LLVMFinalizeFunctionPassManager )
_method( LLVMDisposePassManager )
{ NULL }
};
PyMODINIT_FUNC
init_core(void)
{
Py_InitModule("_core", core_methods);
}

1415
llvm/core.py Normal file

File diff suppressed because it is too large Load diff

43
llvm/extra.cpp Normal file
View file

@ -0,0 +1,43 @@
/**
* These are some "extra" functions not available in the standard LLVM-C
* bindings, but are required / good-to-have inorder to implement the
* Python bindings.
*/
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/GlobalVariable.h"
#include "llvm/TypeSymbolTable.h"
#include "llvm/ModuleProvider.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/CallSite.h"
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include "llvm-c/Core.h"
#include "llvm/Analysis/Verifier.h"
#include "extra.h"
using namespace llvm;
char *LLVMDumpModuleToString(LLVMModuleRef M) {
std::ostringstream buf;
unwrap(M)->print(buf);
return strdup(buf.str().c_str());
}
char *LLVMDumpTypeToString(LLVMTypeRef Ty) {
std::ostringstream buf;
unwrap(Ty)->print(buf);
return strdup(buf.str().c_str());
}
char *LLVMDumpValueToString(LLVMValueRef Val) {
std::ostringstream buf;
unwrap(Val)->print(buf);
return strdup(buf.str().c_str());
}

31
llvm/extra.h Normal file
View file

@ -0,0 +1,31 @@
/**
* These are some "extra" functions not available in the standard LLVM-C
* bindings, but are required / good-to-have inorder to implement the
* Python bindings.
*/
#ifndef LLVM_PY_EXTRA_H
#define LLVM_PY_EXTRA_H
#ifdef __cplusplus
extern "C" {
#endif
/** Dumps module contents to a string. See Module::print. Free the
returned string with LLVMDisposeMessage, after use. */
char *LLVMDumpModuleToString(LLVMModuleRef M);
/** Dumps type to a string. See Type::print. Free the returned string with
LLVMDisposeMessage, after use. */
char *LLVMDumpTypeToString(LLVMTypeRef Ty);
/** Dumps value to a string. See Value::print. Free the returned string with
LLVMDisposeMessage, after use. */
char *LLVMDumpValueToString(LLVMValueRef Val);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* LLVM_PY_EXTRA_H */

124
llvm/wrap.c Normal file
View file

@ -0,0 +1,124 @@
#include "wrap.h"
/*===----------------------------------------------------------------------===*/
/* Type ctor/dtor */
/*===----------------------------------------------------------------------===*/
PyObject *ctor_LLVMModuleRef(LLVMModuleRef p)
{
if (p)
return PyCObject_FromVoidPtr(p, NULL);
Py_RETURN_NONE; /* avoiding the `else' since we don't (want to) know what
the macro Py_RETURN_NONE expands to */
}
PyObject *ctor_LLVMTypeRef(LLVMTypeRef p)
{
if (p)
return PyCObject_FromVoidPtr(p, NULL);
Py_RETURN_NONE;
}
PyObject *ctor_LLVMValueRef(LLVMValueRef p)
{
if (p)
return PyCObject_FromVoidPtr(p, NULL);
Py_RETURN_NONE;
}
PyObject *ctor_LLVMTypeHandleRef(LLVMTypeHandleRef p)
{
if (p)
return PyCObject_FromVoidPtr(p, NULL);
Py_RETURN_NONE;
}
PyObject *ctor_LLVMBasicBlockRef(LLVMBasicBlockRef p)
{
if (p)
return PyCObject_FromVoidPtr(p, NULL);
Py_RETURN_NONE;
}
PyObject *ctor_LLVMBuilderRef(LLVMBuilderRef p)
{
if (p)
return PyCObject_FromVoidPtr(p, NULL);
Py_RETURN_NONE;
}
PyObject *ctor_LLVMModuleProviderRef(LLVMModuleProviderRef p)
{
if (p)
return PyCObject_FromVoidPtr(p, NULL);
Py_RETURN_NONE;
}
PyObject *ctor_LLVMMemoryBufferRef(LLVMMemoryBufferRef p)
{
if (p)
return PyCObject_FromVoidPtr(p, NULL);
Py_RETURN_NONE;
}
PyObject *ctor_LLVMPassManagerRef(LLVMPassManagerRef p)
{
if (p)
return PyCObject_FromVoidPtr(p, NULL);
Py_RETURN_NONE;
}
PyObject *ctor_int(int i)
{
return PyInt_FromLong(i);
}
/*===----------------------------------------------------------------------===*/
/* Helper functions */
/*===----------------------------------------------------------------------===*/
void *get_object_arg(PyObject *args)
{
PyObject *o;
if (!PyArg_ParseTuple(args, "O", &o))
return NULL;
return PyCObject_AsVoidPtr(o);
}
void **make_array_from_list(PyObject *list, int n)
{
int i;
void **arr;
arr = (void **)malloc(sizeof(void *) * n);
if (!arr)
return NULL;
for (i=0; i<n; i++) {
PyObject *e = PyList_GetItem(list, i);
arr[i] = PyCObject_AsVoidPtr(e);
}
return arr;
}
PyObject *make_list_from_LLVMTypeRef_array(LLVMTypeRef *p, unsigned n)
{
int i;
PyObject *list = PyList_New(n);
if (!list)
return NULL;
for (i=0; i<n; i++) {
PyObject *elem = ctor_LLVMTypeRef(p[i]);
PyList_SetItem(list, i, elem);
}
return list;
}

724
llvm/wrap.h Normal file
View file

@ -0,0 +1,724 @@
/**
* Functions and macros to aid in wrapping.
*/
#ifndef LLVM_PY_WRAP_H
#define LLVM_PY_WRAP_H
/* python includes */
#include <Python.h>
#include <structmember.h>
/* llvm includes */
#include <llvm-c/Core.h>
#include <llvm-c/Analysis.h>
/*===----------------------------------------------------------------------===*/
/* Type ctor/dtor */
/*===----------------------------------------------------------------------===*/
/* These are functions that can construct a PyCObject (a Python object that
* holds an opaque pointer) from any time. The naming convention is significant,
* this is assumed by the wrapper macros below. These are the equivalent of
* what would in C++ have been specializations of a template function ctor<T>
* for various types.
*/
/* LLVMModuleRef */
PyObject *ctor_LLVMModuleRef(LLVMModuleRef p);
/* LLVMTypeRef */
PyObject *ctor_LLVMTypeRef(LLVMTypeRef p);
/* LLVMValueRef */
PyObject *ctor_LLVMValueRef(LLVMValueRef p);
/* LLVMTypeHandleRef */
PyObject *ctor_LLVMTypeHandleRef(LLVMTypeHandleRef p);
/* LLVMBasicBlockRef */
PyObject *ctor_LLVMBasicBlockRef(LLVMBasicBlockRef p);
/* LLVMBuilderRef */
PyObject *ctor_LLVMBuilderRef(LLVMBuilderRef p);
/* LLVMModuleProviderRef */
PyObject *ctor_LLVMModuleProviderRef(LLVMModuleProviderRef p);
/* LLVMMemoryBufferRef */
PyObject *ctor_LLVMMemoryBufferRef(LLVMMemoryBufferRef p);
/* LLVMPassManagerRef */
PyObject *ctor_LLVMPassManagerRef(LLVMPassManagerRef p);
/* standard types */
PyObject *ctor_int(int i);
/*===----------------------------------------------------------------------===*/
/* Helper methods */
/*===----------------------------------------------------------------------===*/
/**
* Accept a single object argument of type PyCObject and return the
* opaque pointer contained in it.
*/
void *get_object_arg(PyObject *args);
/**
* Given a PyList object (list) having n (= PyList_Size(list)) elements,
* each list object being a PyCObject, return a malloc()-ed array of
* opaque pointers from the PyCObjects. The 'n' is passed explicitly
* since the caller will have to query it once anyway, and we can avoid
* a second call internally.
*/
void **make_array_from_list(PyObject *list, int n);
/**
* Given an array of LLVMTypeRef's, create a PyList object. Note that
* currently such an action is required only for LLVMTypeRef's, when
* it is required another type, this method has to be generalized.
*/
PyObject *make_list_from_LLVMTypeRef_array(LLVMTypeRef *p, unsigned n);
/*===----------------------------------------------------------------------===*/
/* Wrapper macros */
/*===----------------------------------------------------------------------===*/
/* The following wrapper macros define functions that wrap over LLVM-C APIs
* of various signatures. Though they look hairy, they all follow some
* conventions.
*/
/**
* Wrap LLVM functions of the type
* outtype func(intype1 arg1)
*/
#define _wrap_obj2obj(func, intype1, outtype) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
intype1 arg1; \
\
if (!(arg1 = ( intype1 )get_object_arg(args))) \
return NULL; \
\
return ctor_ ## outtype ( func (arg1)); \
}
/**
* Wrap LLVM functions of the type
* outtype func(int arg1)
*/
#define _wrap_int2obj(func, outtype) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
int arg1; \
\
if (!PyArg_ParseTuple(args, "i", &arg1)) \
return NULL; \
\
return ctor_ ## outtype ( func (arg1)); \
}
/**
* Wrap LLVM functions of the type
* outtype func(intype1 arg1, intype2 arg2)
*/
#define _wrap_objobj2obj(func, intype1, intype2, outtype) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
PyObject *obj1, *obj2; \
intype1 arg1; \
intype2 arg2; \
\
if (!PyArg_ParseTuple(args, "OO", &obj1, &obj2)) \
return NULL; \
\
arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \
arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \
\
return ctor_ ## outtype ( func (arg1, arg2)); \
}
/**
* Wrap LLVM functions of the type
* void func(intype1 arg1, intype2 arg2)
*/
#define _wrap_objobj2none(func, intype1, intype2) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
PyObject *obj1, *obj2; \
intype1 arg1; \
intype2 arg2; \
\
if (!PyArg_ParseTuple(args, "OO", &obj1, &obj2)) \
return NULL; \
\
arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \
arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \
\
func (arg1, arg2); \
Py_RETURN_NONE; \
}
/**
* Wrap LLVM functions of the type
* outtype func(intype1 arg1, <unsigned/signed int> arg2)
*/
#define _wrap_objint2obj(func, intype1, outtype) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
PyObject *obj1; \
intype1 arg1; \
unsigned int arg2; \
\
if (!PyArg_ParseTuple(args, "OI", &obj1, &arg2)) \
return NULL; \
\
arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \
\
return ctor_ ## outtype ( func (arg1, arg2)); \
}
/**
* Wrap LLVM functions of the type
* outtype func(intype1 arg1, intype2 arg2, intype3 arg3)
*/
#define _wrap_objobjobj2obj(func, intype1, intype2, intype3, outtype) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
PyObject *obj1, *obj2, *obj3; \
intype1 arg1; \
intype2 arg2; \
intype3 arg3; \
\
if (!PyArg_ParseTuple(args, "OOO", &obj1, &obj2, &obj3)) \
return NULL; \
\
arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \
arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \
arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \
\
return ctor_ ## outtype ( func (arg1, arg2, arg3)); \
}
/**
* Wrap LLVM functions of the type
* void func(intype1 arg1, intype2 arg2, intype3 arg3)
*/
#define _wrap_objobjobj2none(func, intype1, intype2, intype3) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
PyObject *obj1, *obj2, *obj3; \
intype1 arg1; \
intype2 arg2; \
intype3 arg3; \
\
if (!PyArg_ParseTuple(args, "OOO", &obj1, &obj2, &obj3)) \
return NULL; \
\
arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \
arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \
arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \
\
func (arg1, arg2, arg3); \
Py_RETURN_NONE; \
}
/**
* Wrap LLVM functions of the type
* outtype func(<unsigned/signed int> arg1, intype2 arg2, intype3 arg3)
*/
#define _wrap_intobjobj2obj(func, intype2, intype3, outtype) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
unsigned int arg1; \
PyObject *obj2, *obj3; \
intype2 arg2; \
intype3 arg3; \
\
if (!PyArg_ParseTuple(args, "IOO", &arg1, &obj2, &obj3)) \
return NULL; \
\
arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \
arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \
\
return ctor_ ## outtype ( func (arg1, arg2, arg3)); \
}
/**
* Wrap LLVM functions of the type
* outtype func()
*/
#define _wrap_none2obj(func, outtype) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
if (!PyArg_ParseTuple(args, "")) \
return NULL; \
\
return ctor_ ## outtype ( func ()); \
}
/**
* Wrap LLVM functions of the type
* const char *func(intype1 arg1)
*/
#define _wrap_obj2str(func, intype1) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
intype1 arg1; \
\
if (!(arg1 = ( intype1 )get_object_arg(args))) \
return NULL; \
\
return PyString_FromString( func (arg1)); \
}
/**
* Wrap LLVM functions of the type
* void func(intype1 arg1)
*/
#define _wrap_obj2none(func, intype1) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
intype1 arg1; \
\
if (!(arg1 = ( intype1 )get_object_arg(args))) \
return NULL; \
\
func (arg1); \
Py_RETURN_NONE; \
}
/**
* Wrap LLVM functions of the type
* void func(intype1 arg1, const char *arg2)
*/
#define _wrap_objstr2none(func, intype1) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
PyObject *obj1; \
const char *arg2; \
intype1 arg1; \
\
if (!PyArg_ParseTuple(args, "Os", &obj1, &arg2)) \
return NULL; \
\
arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \
\
func (arg1, arg2); \
Py_RETURN_NONE; \
}
/**
* Wrap LLVM functions of the type
* outtype func(intype1 arg1, const char *arg2)
*/
#define _wrap_objstr2obj(func, intype1, outtype) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
PyObject *obj1; \
const char *arg2; \
intype1 arg1; \
\
if (!PyArg_ParseTuple(args, "Os", &obj1, &arg2)) \
return NULL; \
\
arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \
\
return ctor_ ## outtype ( func (arg1, arg2)); \
}
/**
* Wrap LLVM functions of the type
* void func(intype1 arg1, <unsigned/signed int> arg2)
*/
#define _wrap_objint2none(func, intype1) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
PyObject *obj1; \
int arg2; \
intype1 arg1; \
\
if (!PyArg_ParseTuple(args, "Oi", &obj1, &arg2)) \
return NULL; \
\
arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \
\
func (arg1, arg2); \
Py_RETURN_NONE; \
}
/**
* Wrap LLVM functions of the type
* outtype func(intype1 arg1, intype2 arg2, const char *arg3)
*/
#define _wrap_objobjstr2obj(func, intype1, intype2, outtype) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
PyObject *obj1, *obj2; \
intype1 arg1; \
intype2 arg2; \
const char *arg3; \
\
if (!PyArg_ParseTuple(args, "OOs", &obj1, &obj2, &arg3)) \
return NULL; \
\
arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \
arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \
\
return ctor_ ## outtype ( func (arg1, arg2, arg3)) ;\
}
/**
* Wrap LLVM functions of the type
* void func(intype1 arg1, <unsigned/signed int> arg2, <unsigned/signed int> arg3)
*/
#define _wrap_objintint2none(func, intype1) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
PyObject *obj1; \
intype1 arg1; \
int arg2, arg3; \
\
if (!PyArg_ParseTuple(args, "Oii", &obj1, &arg2, &arg3)) \
return NULL; \
\
arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \
\
func (arg1, arg2, arg3); \
Py_RETURN_NONE; \
}
/**
* Wrap LLVM functions of the type
* outtype func(intype1 arg1, const char *arg2, intype3 arg3)
*/
#define _wrap_objstrobj2obj(func, intype1, intype3, outtype) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
PyObject *obj1, *obj3; \
intype1 arg1; \
const char *arg2; \
intype3 arg3; \
\
if (!PyArg_ParseTuple(args, "OsO", &obj1, &arg2, &obj3)) \
return NULL; \
\
arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \
arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \
\
return ctor_ ## outtype ( func (arg1, arg2, arg3)); \
}
/**
* Wrap LLVM functions of the type
* outtype func(intype1 arg1, intype2 arg2, intype3 arg3, const char *arg4)
*/
#define _wrap_objobjobjstr2obj(func, intype1, intype2, intype3, outtype) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
PyObject *obj1, *obj2, *obj3; \
intype1 arg1; \
intype2 arg2; \
intype3 arg3; \
const char *arg4; \
\
if (!PyArg_ParseTuple(args, "OOOs", &obj1, &obj2, &obj3, &arg4)) \
return NULL; \
\
arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \
arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \
arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \
\
return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4)); \
}
/**
* Wrap LLVM functions of the type
* outtype func(intype1 arg1, intype2 arg2, intype3 arg3, <unsigned/signed int> arg4)
*/
#define _wrap_objobjobjint2obj(func, intype1, intype2, intype3, outtype) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
PyObject *obj1, *obj2, *obj3; \
intype1 arg1; \
intype2 arg2; \
intype3 arg3; \
int arg4; \
\
if (!PyArg_ParseTuple(args, "OOOi", &obj1, &obj2, &obj3, &arg4)) \
return NULL; \
\
arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \
arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \
arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \
\
return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4)); \
}
/**
* Wrap LLVM functions of the type
* outtype func(intype1 arg1, intype2 arg2, intype3 arg3, intype arg4)
*/
#define _wrap_objobjobjobj2obj(func, intype1, intype2, intype3, intype4, outtype) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
PyObject *obj1, *obj2, *obj3, *obj4; \
intype1 arg1; \
intype2 arg2; \
intype3 arg3; \
intype4 arg4; \
\
if (!PyArg_ParseTuple(args, "OOOO", &obj1, &obj2, &obj3, &obj4)) \
return NULL; \
\
arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \
arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \
arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \
arg4 = ( intype4 ) PyCObject_AsVoidPtr(obj4); \
\
return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4)); \
}
/**
* Wrap LLVM functions of the type
* outtype func(intype1 arg1, intype2 arg2, intype3 arg3, intype arg4, const char *arg5)
*/
#define _wrap_objobjobjobjstr2obj(func, intype1, intype2, intype3, intype4, outtype) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
PyObject *obj1, *obj2, *obj3, *obj4; \
intype1 arg1; \
intype2 arg2; \
intype3 arg3; \
intype4 arg4; \
const char *arg5; \
\
if (!PyArg_ParseTuple(args, "OOOOs", &obj1, &obj2, &obj3, &obj4, &arg5)) \
return NULL; \
\
arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \
arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \
arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \
arg4 = ( intype4 ) PyCObject_AsVoidPtr(obj4); \
\
return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4, arg5)); \
}
/**
* Wrap LLVM functions of the type
* outtype func(intype1 arg1, int arg2, intype3 arg3, intype4 arg4, const char *arg5)
*/
#define _wrap_objintobjobjstr2obj(func, intype1, intype3, intype4, outtype) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
PyObject *obj1, *obj3, *obj4; \
intype1 arg1; \
int arg2; \
intype3 arg3; \
intype4 arg4; \
const char *arg5; \
\
if (!PyArg_ParseTuple(args, "OiOOs", &obj1, &arg2, &obj3, &obj4, &arg5)) \
return NULL; \
\
arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \
arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \
arg4 = ( intype4 ) PyCObject_AsVoidPtr(obj4); \
\
return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4, arg5)); \
}
/**
* Wrap LLVM functions of the type
* outtype func(intype1 arg1, intype2 *arg2v, unsigned arg2n)
* where arg2v is an array of intype2 elements, arg2n in length.
*/
#define _wrap_objlist2obj(func, intype1, intype2, outtype) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
PyObject *obj1, *obj2; \
intype1 arg1; \
intype2 *arg2v; \
unsigned arg2n; \
outtype ret; \
\
if (!PyArg_ParseTuple(args, "OO", &obj1, &obj2)) \
return NULL; \
\
arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \
arg2n = (unsigned) PyList_Size(obj2); \
if (!(arg2v = ( intype2 *)make_array_from_list(obj2, arg2n))) \
return PyErr_NoMemory(); \
\
ret = func (arg1, arg2v, arg2n); \
free(arg2v); \
return ctor_ ## outtype (ret); \
}
/**
* Wrap LLVM functions of the type
* outtype func(intype1 *arg1v, unsigned arg1n, int arg2)
* where arg1v is an array of intype1 elements, arg1n in length.
*/
#define _wrap_listint2obj(func, intype1, outtype) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
PyObject *obj1; \
int arg2; \
intype1 *arg1v; \
unsigned arg1n; \
outtype ret; \
\
if (!PyArg_ParseTuple(args, "Oi", &obj1, &arg2)) \
return NULL; \
\
arg1n = (unsigned) PyList_Size(obj1); \
if (!(arg1v = ( intype1 *)make_array_from_list(obj1, arg1n))) \
return PyErr_NoMemory(); \
\
ret = func (arg1v, arg1n, arg2); \
free(arg1v); \
return ctor_ ## outtype (ret); \
}
/**
* Wrap LLVM functions of the type
* outtype func(intype1 *arg1v, unsigned arg1n)
* where arg1v is an array of intype1 elements, arg1n in length.
*/
#define _wrap_list2obj(func, intype1, outtype) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
PyObject *obj1; \
intype1 *arg1v; \
unsigned arg1n; \
outtype ret; \
\
if (!PyArg_ParseTuple(args, "O", &obj1)) \
return NULL; \
\
arg1n = (unsigned) PyList_Size(obj1); \
if (!(arg1v = ( intype1 *)make_array_from_list(obj1, arg1n))) \
return PyErr_NoMemory(); \
\
ret = func (arg1v, arg1n); \
free(arg1v); \
return ctor_ ## outtype (ret); \
}
/**
* Wrap LLVM functions of the type
* outtype func(intype1 arg1, intype2 *arg2v, unsigned arg2n, int arg3)
* where arg2v is an array of intype2 elements, arg2n in length.
*/
#define _wrap_objlistint2obj(func, intype1, intype2, outtype) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
PyObject *obj1, *obj2; \
intype1 arg1; \
intype2 *arg2v; \
unsigned arg2n; \
int arg3; \
outtype ret; \
\
if (!PyArg_ParseTuple(args, "OOi", &obj1, &obj2, &arg3)) \
return NULL; \
\
arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \
arg2n = (unsigned) PyList_Size(obj2); \
if (!(arg2v = ( intype2 *)make_array_from_list(obj2, arg2n))) \
return PyErr_NoMemory(); \
\
ret = func (arg1, arg2v, arg2n, arg3); \
free(arg2v); \
return ctor_ ## outtype (ret); \
}
/**
* Wrap LLVM functions of the type
* outtype func(intype1 arg1, intype2 arg2, intype3 *arg3v, unsigned arg3n, const char *arg4)
* where arg3v is an array of intype3 elements, arg3n in length.
*/
#define _wrap_objobjliststr2obj(func, intype1, intype2, intype3, outtype) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
PyObject *obj1, *obj2, *obj3; \
const char *arg4; \
intype1 arg1; \
intype2 arg2; \
intype3 *arg3v; \
unsigned arg3n; \
outtype ret; \
\
if (!PyArg_ParseTuple(args, "OOOs", &obj1, &obj2, &obj3, &arg4))\
return NULL; \
\
arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \
arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \
arg3n = ( unsigned ) PyList_Size(obj3); \
if (!(arg3v = ( intype3 *)make_array_from_list(obj3, arg3n))) \
return PyErr_NoMemory(); \
\
ret = func (arg1, arg2, arg3v, arg3n, arg4); \
free(arg3v); \
return ctor_ ## outtype (ret); \
}
/**
* Wrap LLVM dump-to-string functions of the type
* char *func(intype1)
* where the return value has to be disposed after use by calling
* LLVMDisposeMessage.
*/
#define _wrap_dumper(func, intype1) \
static PyObject * \
_w ## func (PyObject *self, PyObject *args) \
{ \
intype1 arg1; \
char *val; \
PyObject *ret; \
\
if (!(arg1= ( intype1 ) get_object_arg(args))) \
return NULL; \
\
val = func (arg1); \
ret = PyString_FromString(val); \
LLVMDisposeMessage(val); \
return ret; \
}
#endif /* LLVM_PY_WRAP_H */

80
setup.py Normal file
View file

@ -0,0 +1,80 @@
#!/usr/bin/env python
import sys, os
from distutils.core import setup, Extension
def _run(cmd):
return os.popen(cmd).read().rstrip()
def get_libs(llvm_config, components):
return _run(llvm_config + ' --libs ' + ' '.join(components))
def get_llvm_config():
# get from command-line, or use default
lc = 'llvm-config'
for i in xrange(0, len(sys.argv)):
arg = sys.argv[i]
if arg.startswith('--llvm-config='):
del sys.argv[i]
lc = arg.split('=')[1]
# see if it works
version = _run(lc + ' --version')
if version == '':
return (lc, False) # didn't work
return (lc, True)
def call_setup(llvm_config):
incdir = _run(llvm_config + ' --includedir')
ldflags = _run(llvm_config + ' --ldflags')
libs_core = get_libs(llvm_config, ['core', 'analysis'])
ext_core = Extension(
'llvm._core',
['llvm/_core.c', 'llvm/wrap.c', 'llvm/extra.cpp'],
define_macros = [
('__STDC_LIMIT_MACROS', None),
('_GNU_SOURCE', None)],
include_dirs = [incdir],
extra_link_args = [ldflags + ' ' + libs_core])
setup(
name='llvm-py',
version='0.2',
description='Python Bindings for LLVM',
author='Mahadevan R',
author_email='mdevan.foobar@gmail.com',
url='http://code.google.com/p/llvm-py/',
packages=['llvm'],
py_modules = [ 'llvm.core' ],
ext_modules = [ ext_core ],)
def main():
# get llvm config
llvm_config, is_good = get_llvm_config()
if is_good:
print "Using llvm-config=" + llvm_config
else:
print "Cannot invoke llvm-config (tried '%s')." % llvm_config
print "Try again with --llvm-config=/path/to/llvm-config."
return 1
# setup
call_setup(llvm_config)
# done
return 0
ev = main()
sys.exit(ev)

43
test/example.py Normal file
View file

@ -0,0 +1,43 @@
#!/usr/bin/env python
from llvm.core import *
## create a module
module = Module.new("my_module")
## create a function type taking two doubles and returning a (32-bit) integer
ty_double = Type.double()
ty_int = Type.int()
ty_func = Type.function( ty_int, [ ty_double, ty_double ] )
## create a function of this type
func = Function.new( module, ty_func, "foobar" )
# name function args
func.args[0].name = "arg1"
func.args[1].name = "arg2"
## implement the function
# add a basic block
entry = func.append_basic_block("entry")
# create an llvm::IRBuilder
builder = Builder.new()
builder.position_at_end(entry)
# add two args into temp1
temp1 = builder.add(func.args[0], func.args[1], "temp1")
# sub `1' from that
one = Constant.real( ty_double, 1.0 )
temp2 = builder.sub(temp1, one, "temp2")
# convert to integer
temp3 = builder.fptoui(temp2, ty_int, "temp3")
# return it
builder.ret(temp3)
# dump the module to see the bc
module.dump()

119
test/test.py Normal file
View file

@ -0,0 +1,119 @@
#!/usr/bin/env python
# watch out for uncollected objects
import gc
import unittest, sys
from llvm.core import *
class TestModule(unittest.TestCase):
def setUp(self):
pass
def testmodule_and_moduleprovider(self):
"""Ownership issues between Module and ModuleProvider objects."""
def temp_mp(m):
mp = ModuleProvider.new(m)
del mp
def temp_m():
m = Module.new("temp_m")
return ModuleProvider.new(m)
m = Module.new("test1.1")
self.assertEqual(m.owner, None)
mp = ModuleProvider.new(m)
self.assertEqual(m.owner, mp)
mp_repr = repr(mp)
mp = None
self.assertNotEqual(m.owner, None)
self.assertEqual(repr(m.owner), mp_repr)
m = None
self.assertEqual(gc.garbage, [])
m2 = Module.new("test1.2")
temp_mp(m2)
del m2
self.assertEqual(gc.garbage, [])
mp3 = temp_m()
mp3 = None
m4 = Module.new("test1.4")
self.assertEqual(sys.getrefcount(m4), 1+1)
mp4 = ModuleProvider.new(m4)
self.assertEqual(sys.getrefcount(m4), 1+1)
self.assertEqual(sys.getrefcount(mp4), 2+1)
m4 = None
self.assertEqual(sys.getrefcount(mp4), 1+1)
mp4 = None
own_works = False
m5 = Module.new("test1.5")
mp5 = ModuleProvider.new(m5)
try:
m5._own(None)
except AssertionError:
own_works = True
self.assertEqual(own_works, True)
def testdata_layout(self):
"""Data layout property."""
m = Module.new("test2.1")
self.assertEqual(m.data_layout, '')
m.data_layout = 'some_value'
self.assertEqual(m.data_layout, 'some_value')
reqd = '; ModuleID = \'test2.1\'\ntarget datalayout = "some_value"\n'
self.assertEqual(str(m), reqd)
def testtarget(self):
"""Target property."""
m = Module.new("test3.1")
self.assertEqual(m.target, '')
m.target = 'some_value'
self.assertEqual(m.target, 'some_value')
reqd = '; ModuleID = \'test3.1\'\ntarget triple = "some_value"\n'
self.assertEqual(str(m), reqd)
def testtype_name(self):
"""Type names."""
m = Module.new("test4.1")
r = m.add_type_name("typename41", Type.int())
self.assertEqual(r, 0)
r = m.add_type_name("typename41", Type.int())
self.assertEqual(r, 1)
reqd = "; ModuleID = 'test4.1'\n\t%typename41 = type i32\n"
self.assertEqual(str(m), reqd)
r = m.delete_type_name("typename41")
reqd = "; ModuleID = 'test4.1'\n"
self.assertEqual(str(m), reqd)
r = m.delete_type_name("no such name") # nothing should happen
reqd = "; ModuleID = 'test4.1'\n"
self.assertEqual(str(m), reqd)
def testglobal_variable(self):
"""Global variables."""
m = Module.new("test5.1")
t = Type.int()
gv = m.add_global_variable(t, "gv")
print m
self.assertNotEqual(gv, None)
self.assertEqual(gv.name, "gv")
self.assertEqual(gv.type, t)
def main():
gc.set_debug(gc.DEBUG_LEAK)
# run tests
unittest.main()
# done
if gc.garbage:
print "garbage = ", gc.garbage
main()