2015-08-07 16:44:49 +08:00
|
|
|
import os, sys, tempfile
|
|
|
|
|
|
|
|
from pythonparser import diagnostic
|
2014-09-17 17:06:51 +08:00
|
|
|
|
2014-12-03 18:20:30 +08:00
|
|
|
from artiq.language.core import *
|
2015-04-22 01:12:01 +08:00
|
|
|
from artiq.language.units import ns
|
2014-12-03 18:20:30 +08:00
|
|
|
|
2015-08-07 16:44:49 +08:00
|
|
|
from artiq.compiler import Stitcher, Module
|
|
|
|
from artiq.compiler.targets import OR1KTarget
|
2014-09-17 17:06:51 +08:00
|
|
|
|
2015-08-07 16:44:49 +08:00
|
|
|
# Import for side effects (creating the exception classes).
|
|
|
|
from artiq.coredevice import exceptions
|
2014-09-17 17:06:51 +08:00
|
|
|
|
|
|
|
|
2015-08-07 16:44:49 +08:00
|
|
|
class CompileError(Exception):
|
2014-11-03 18:44:30 +08:00
|
|
|
pass
|
|
|
|
|
2015-07-14 04:08:20 +08:00
|
|
|
class Core:
|
|
|
|
def __init__(self, dmgr, ref_period=8*ns, external_clock=False):
|
|
|
|
self.comm = dmgr.get("comm")
|
|
|
|
self.ref_period = ref_period
|
|
|
|
self.external_clock = external_clock
|
2014-12-03 18:20:30 +08:00
|
|
|
|
2015-05-02 23:41:49 +08:00
|
|
|
self.first_run = True
|
2014-11-21 04:38:52 +08:00
|
|
|
self.core = self
|
2015-03-13 21:55:18 +08:00
|
|
|
self.comm.core = self
|
2014-09-17 17:06:51 +08:00
|
|
|
|
2015-08-07 16:44:49 +08:00
|
|
|
def compile(self, function, args, kwargs, with_attr_writeback=True):
|
|
|
|
try:
|
|
|
|
engine = diagnostic.Engine(all_errors_are_fatal=True)
|
2014-09-17 17:06:51 +08:00
|
|
|
|
2015-08-07 16:44:49 +08:00
|
|
|
stitcher = Stitcher(engine=engine)
|
|
|
|
stitcher.stitch_call(function, args, kwargs)
|
2014-09-17 17:06:51 +08:00
|
|
|
|
2015-08-09 02:47:20 +08:00
|
|
|
module = Module(stitcher)
|
|
|
|
target = OR1KTarget()
|
2014-09-17 17:06:51 +08:00
|
|
|
|
2015-08-09 02:47:20 +08:00
|
|
|
if os.getenv('ARTIQ_DUMP_IR'):
|
|
|
|
print("====== ARTIQ IR DUMP ======", file=sys.stderr)
|
|
|
|
for function in module.artiq_ir:
|
|
|
|
print(function, file=sys.stderr)
|
|
|
|
|
|
|
|
if os.getenv('ARTIQ_DUMP_LLVM'):
|
|
|
|
print("====== LLVM IR DUMP ======", file=sys.stderr)
|
|
|
|
print(module.build_llvm_ir(target), file=sys.stderr)
|
|
|
|
|
|
|
|
return target.compile_and_link([module]), stitcher.rpc_map
|
2015-08-07 16:44:49 +08:00
|
|
|
except diagnostic.Error as error:
|
|
|
|
print("\n".join(error.diagnostic.render(colored=True)), file=sys.stderr)
|
|
|
|
raise CompileError() from error
|
2014-10-29 17:09:45 +08:00
|
|
|
|
2015-08-07 16:44:49 +08:00
|
|
|
def run(self, function, args, kwargs):
|
2015-05-02 23:41:49 +08:00
|
|
|
if self.first_run:
|
|
|
|
self.comm.check_ident()
|
|
|
|
self.comm.switch_clock(self.external_clock)
|
2015-08-07 16:44:49 +08:00
|
|
|
self.first_run = False
|
|
|
|
|
|
|
|
kernel_library, rpc_map = self.compile(function, args, kwargs)
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.comm.load(kernel_library)
|
|
|
|
except Exception as error:
|
|
|
|
shlib_temp = tempfile.NamedTemporaryFile(suffix=".so", delete=False)
|
|
|
|
shlib_temp.write(kernel_library)
|
|
|
|
shlib_temp.close()
|
|
|
|
raise RuntimeError("shared library dumped to {}".format(shlib_temp.name)) from error
|
2015-05-02 23:41:49 +08:00
|
|
|
|
2015-08-07 16:44:49 +08:00
|
|
|
self.comm.run()
|
|
|
|
self.comm.serve(rpc_map)
|
2014-11-21 04:38:52 +08:00
|
|
|
|
2015-02-19 00:56:30 +08:00
|
|
|
@kernel
|
2015-07-02 04:22:53 +08:00
|
|
|
def get_rtio_counter_mu(self):
|
|
|
|
return syscall("rtio_get_counter")
|
2015-02-19 00:56:30 +08:00
|
|
|
|
2014-11-21 04:38:52 +08:00
|
|
|
@kernel
|
2015-05-03 20:42:42 +08:00
|
|
|
def break_realtime(self):
|
2015-07-02 04:22:53 +08:00
|
|
|
at_mu(syscall("rtio_get_counter") + 125000)
|