Last active
February 10, 2017 01:00
-
-
Save scizzorz/8579c3deef24d91506405dd22c875ad7 to your computer and use it in GitHub Desktop.
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
; ModuleID = "test" | |
target triple = "x86_64-unknown-linux-gnu" | |
target datalayout = "" | |
declare i8* @malloc(i32) | |
declare void @"llvm.init.trampoline"(i8*, i8*, i8*) | |
declare i8* @"llvm.adjust.trampoline"(i8*) | |
declare void @printf(i8*, ...) | |
@format = global [4 x i8] c"%d\0A\00" | |
define i32 @loader(i32* nest %a) { | |
entry: | |
%res = load i32, i32* %a | |
ret i32 %res | |
} | |
define i32 @main(i32 %argc, i8** %argv) { | |
entry: | |
%env_raw_ptr = call i8* (i32) @malloc(i32 4) | |
%env_ptr = bitcast i8* %env_raw_ptr to i32* | |
store i32 5, i32* %env_ptr | |
%tramp_buf = call i8* (i32) @malloc(i32 72) | |
%raw_loader = bitcast i32 (i32*)* @loader to i8* | |
call void (i8*, i8*, i8*) @"llvm.init.trampoline"(i8* %tramp_buf, i8* %raw_loader, i8* %env_raw_ptr) | |
%adj_tramp_buf = call i8* (i8*) @"llvm.adjust.trampoline"(i8* %tramp_buf) | |
%bound_loader = bitcast i8* %adj_tramp_buf to i32 ()* | |
%format_ptr = getelementptr [4 x i8], [4 x i8]* @format, i32 0, i32 0 | |
%loader_ret = call i32 () %bound_loader() | |
; %loader_ret = call i32 (i32*) @loader(i32* %env_ptr) | |
call void(i8*, ...) @printf(i8* %format_ptr, i32 %loader_ret) | |
ret i32 0 | |
} |
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
from ctypes import CFUNCTYPE | |
from ctypes import byref | |
from ctypes import c_int | |
from ctypes import c_char_p | |
from ctypes import POINTER | |
import llvmlite.binding as llvm | |
with open('test.ll') as tmp: | |
llvm_ir = tmp.read() | |
llvm.initialize() | |
llvm.initialize_native_target() | |
llvm.initialize_native_asmprinter() | |
target = llvm.Target.from_default_triple() | |
target_machine = target.create_target_machine() | |
mod = llvm.parse_assembly(llvm_ir) | |
mod.verify() | |
engine = llvm.create_mcjit_compiler(mod, target_machine) | |
engine.finalize_object() | |
print('finalized') | |
main_ptr = engine.get_function_address('main') | |
main = CFUNCTYPE(c_int, c_int, POINTER(c_char_p))(main_ptr) | |
argc = c_int(1) | |
argv = c_char_p("test".encode("utf-8")) | |
res = main(argc, byref(argv)) | |
print(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment