Created
September 7, 2014 06:30
-
-
Save apparentlymart/a7d03c48289f4cdb529b to your computer and use it in GitHub Desktop.
Python C API from LLVM IR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%struct.pyobject = type { i32, i8* } | |
@hello = private unnamed_addr constant [16 x i8] c"Hello From LLVM\00", align 1 | |
@empty = private unnamed_addr constant [1 x i8] c"\00", align 1 | |
declare void @puts(i8*) | |
declare %struct.pyobject* @PyObject_CallFunction(%struct.pyobject*, i8*, ...) | |
declare %struct.pyobject* @PyObject_Repr(%struct.pyobject*) | |
define %struct.pyobject* @try_this(%struct.pyobject* %arg) { | |
; Say hello from LLVM land | |
call void (i8*)* @puts(i8* getelementptr inbounds ([16 x i8]* @hello, i32 0, i32 0)) | |
; Return value is the repr() of the argument. | |
%ret = call %struct.pyobject* (%struct.pyobject*)* @PyObject_Repr(%struct.pyobject* %arg) | |
; Can't do this yet since we need an LLVM version of Py_DECREF to use | |
; on the result. | |
; %dummy = call %struct.pyobject* (%struct.pyobject*, i8*, ...)* @PyObject_CallFunction(%struct.pyobject* %arg, i8* null) | |
ret %struct.pyobject* %ret | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
libfoo.so: foo.o | |
gcc foo.o -shared -o libfoo.so | |
foo.o: foo.ll | |
llc-3.3 -filetype=obj -relocation-model=pic foo.ll | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ctypes | |
def baz(): | |
print "Hello from back in Python land" | |
module = ctypes.cdll.LoadLibrary("./libtryllvm.so") | |
func = module.try_this | |
func.restype = ctypes.py_object | |
func.argtypes = (ctypes.py_object,) | |
# FIXME: Why does it segfault if we pass a list in here? | |
result = func(baz) | |
print repr(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment