artiq/artiq/compiler/testbench/jit.py

36 lines
1.1 KiB
Python
Raw Normal View History

import os, sys, fileinput, ctypes
2015-07-22 09:10:15 +08:00
from pythonparser import diagnostic
from llvmlite_artiq import binding as llvm
from ..module import Module, Source
2015-07-30 01:35:16 +08:00
from ..targets import NativeTarget
2015-07-22 09:10:15 +08:00
def main():
2016-02-03 04:57:25 +08:00
libartiq_support = os.getenv("LIBARTIQ_SUPPORT")
if libartiq_support is not None:
llvm.load_library_permanently(libartiq_support)
2015-07-22 09:10:15 +08:00
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#", "")
mod = Module(Source.from_string(source.expandtabs(), engine=engine))
2015-07-22 09:10:15 +08:00
2015-07-30 01:35:16 +08:00
target = NativeTarget()
llmod = mod.build_llvm_ir(target)
2015-07-22 09:10:15 +08:00
llparsedmod = llvm.parse_assembly(str(llmod))
llparsedmod.verify()
2015-07-30 01:35:16 +08:00
llmachine = llvm.Target.from_triple(target.triple).create_target_machine()
2015-07-22 09:10:15 +08:00
lljit = llvm.create_mcjit_compiler(llparsedmod, llmachine)
2016-03-31 16:16:03 +08:00
llmain = lljit.get_function_address(llmod.name + ".__modinit__")
ctypes.CFUNCTYPE(None)(llmain)()
2015-07-22 09:10:15 +08:00
if __name__ == "__main__":
main()