-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_bla.py
58 lines (49 loc) · 1.61 KB
/
run_bla.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from ctypes import CFUNCTYPE, c_int
import sys
from ir_bla import module
import llvmlite.binding as llvm
llvm.initialize()
llvm.initialize_native_target()
llvm.initialize_native_asmprinter()
def create_execution_engine():
"""
Create an ExecutionEngine suitable for JIT code generation on
the host CPU. The engine is reusable for an arbitrary number of
modules.
"""
# Create a target machine representing the host
target = llvm.Target.from_default_triple()
target_machine = target.create_target_machine()
# And an execution engine with an empty backing module
backing_mod = llvm.parse_assembly("")
engine = llvm.create_mcjit_compiler(backing_mod, target_machine)
return engine
def compile_ir(engine, module):
"""
Compile the LLVM IR string with the given engine.
The compiled module object is returned.
"""
# Create a LLVM module object from the IR
mod = llvm.parse_assembly(str(module))
mod.verify()
# Now add the module and make sure it is ready for execution
engine.add_module(mod)
engine.finalize_object()
return mod
engine = create_execution_engine()
mod = compile_ir(engine, module)
# Look up the function pointer (a Python int)
func_ptr = engine.get_function_address("main")
# Run the function via ctypes
cfunc = CFUNCTYPE(c_int)(func_ptr)
res = cfunc()
resPrint = bin(res)
def main():
input_filename = sys.argv[1]
output_filename = input_filename[:input_filename.rfind('.')] + '.run'
file_out = open(output_filename, mode='w')
file_out.write(resPrint[2:])
file_out.close()
print(resPrint[2:])
if __name__ == "__main__":
main()