- llvmpy
- a jit python interpreter via llvm
- a wrapper interface between llvm c/c++ function calls and python interface
- flow
- wrapper c/c++ code to shared lib, such as pass, target, VM...
- explore the shared lib to python import path
- import it
- example
original c/c++ code
#include "dpi.h"
/* c_add */
int
c_add(int a, int b) {
return a+b;
}
wrapper it via c/c++ python extend
#include <Python/Python.h>
#include <numpy/arrayobject.h>
#include <stdio.h>
#include <stdlib.h>
#include "dpi.h"
/* wrapper add, ref count definition */
static PyObject *
dpi_add_wrapper(PyObject *self, PyObject *args)
{
int a, b, c;
// parse arguments
if (!PyArg_ParseTuple(args, "ii", &a, &b)) {
return NULL;
}
// run the actual function
c = c_add(a, b);
// build the result to a Python object.
return Py_BuildValue("i", c);
}
/* register methods */
static PyMethodDef DPIMethods[] =
{
{"dpi_add", dpi_add_wrapper, METH_VARARGS, "Calculate the sum of two integers."},
{NULL, NULL, 0, NULL}
};
register to python search path
from distutils.core import setup, Extension
# ref : http://docs.python.org/2/extending/building.html
# the c++ extension module
extension_mod = Extension("dpi", ["moduledpi.c", "dpi.c"], include_dirs=['/var/root/anaconda/lib/python2.7/site-packages/numpy/core/include'],
library_dirs=['/var/root/anaconda/lib/python2.7/site-packages/numpy/core/lib', '/var/root/anaconda/lib/python2.7/site-packages/numpy/lib'])
setup(name = "dpi", ext_modules=[extension_mod])
run it
>>> python
>>> import dpi
>>> dpi.dpi_add(1,2)
沒有留言:
張貼留言