artiq/artiq/compiler/testbench/jit.py

38 lines
1.2 KiB
Python

import os, sys, fileinput, ctypes
from pythonparser import diagnostic
from llvmlite_artiq import binding as llvm
from .. import Module
llvm.initialize()
llvm.initialize_native_target()
llvm.initialize_native_asmprinter()
def main():
libartiq_personality = os.getenv('LIBARTIQ_PERSONALITY')
if libartiq_personality is not None:
llvm.load_library_permanently(libartiq_personality)
def process_diagnostic(diag):
print("\n".join(diag.render()))
if diag.level in ("fatal", "error"):
exit(1)
engine = diagnostic.Engine()
engine.process = process_diagnostic
source = "".join(fileinput.input())
source = source.replace("#ARTIQ#", "")
llmod = Module.from_string(source.expandtabs(), engine=engine).llvm_ir
lltarget = llvm.Target.from_default_triple()
llmachine = lltarget.create_target_machine()
llparsedmod = llvm.parse_assembly(str(llmod))
llparsedmod.verify()
lljit = llvm.create_mcjit_compiler(llparsedmod, llmachine)
lljit.finalize_object()
llmain = lljit.get_pointer_to_global(llparsedmod.get_function(llmod.name + ".__modinit__"))
ctypes.CFUNCTYPE(None)(llmain)()
if __name__ == "__main__":
main()