artiq/artiq/compiler/builtins.py

240 lines
5.8 KiB
Python
Raw Normal View History

"""
2015-06-24 16:46:15 +08:00
The :mod:`builtins` module contains the builtin Python
and ARTIQ types, such as int or float.
"""
from collections import OrderedDict
from . import types
# Types
class TNone(types.TMono):
def __init__(self):
super().__init__("NoneType")
class TBool(types.TMono):
def __init__(self):
super().__init__("bool")
@staticmethod
def zero():
return False
@staticmethod
def one():
return True
class TInt(types.TMono):
def __init__(self, width=None):
if width is None:
width = types.TVar()
super().__init__("int", {"width": width})
@staticmethod
def zero():
return 0
@staticmethod
def one():
return 1
class TFloat(types.TMono):
def __init__(self):
super().__init__("float")
@staticmethod
def zero():
return 0.0
@staticmethod
def one():
return 1.0
2015-07-21 19:27:48 +08:00
class TStr(types.TMono):
def __init__(self):
super().__init__("str")
class TList(types.TMono):
def __init__(self, elt=None):
if elt is None:
elt = types.TVar()
super().__init__("list", {"elt": elt})
2015-06-26 23:53:20 +08:00
class TRange(types.TMono):
def __init__(self, elt=None):
if elt is None:
elt = types.TVar()
super().__init__("range", {"elt": elt})
self.attributes = OrderedDict([
("start", elt),
("stop", elt),
("step", elt),
])
2015-06-26 23:53:20 +08:00
2015-06-29 03:48:15 +08:00
class TException(types.TMono):
# All exceptions share the same internal layout:
# * Pointer to the unique global with the name of the exception (str)
# (which also serves as the EHABI type_info).
# * File, line and column where it was raised (str, int, int).
# * Message, which can contain substitutions {0}, {1} and {2} (str).
# * Three 64-bit integers, parameterizing the message (int(width=64)).
# Keep this in sync with the function ARTIQIRGenerator.alloc_exn.
attributes = OrderedDict([
("__name__", TStr()),
("__file__", TStr()),
("__line__", TInt(types.TValue(32))),
("__col__", TInt(types.TValue(32))),
("__func__", TStr()),
("__message__", TStr()),
("__param0__", TInt(types.TValue(64))),
("__param1__", TInt(types.TValue(64))),
("__param2__", TInt(types.TValue(64))),
])
def __init__(self, name="Exception"):
super().__init__(name)
2015-06-26 16:16:08 +08:00
def fn_bool():
return types.TConstructor(TBool())
2015-06-26 16:16:08 +08:00
def fn_int():
return types.TConstructor(TInt())
2015-06-26 16:16:08 +08:00
def fn_float():
return types.TConstructor(TFloat())
2015-06-26 16:16:08 +08:00
2015-07-21 19:27:48 +08:00
def fn_str():
return types.TConstructor(TStr())
2015-07-21 19:27:48 +08:00
2015-06-26 16:16:08 +08:00
def fn_list():
return types.TConstructor(TList())
2015-06-26 16:16:08 +08:00
2015-06-29 03:48:15 +08:00
def fn_Exception():
return types.TExceptionConstructor(TException("Exception"))
2015-06-29 03:48:15 +08:00
def fn_IndexError():
return types.TExceptionConstructor(TException("IndexError"))
def fn_ValueError():
return types.TExceptionConstructor(TException("ValueError"))
def fn_ZeroDivisionError():
return types.TExceptionConstructor(TException("ZeroDivisionError"))
2015-06-26 23:53:20 +08:00
def fn_range():
2015-06-29 03:40:57 +08:00
return types.TBuiltinFunction("range")
2015-06-26 23:53:20 +08:00
def fn_len():
2015-06-29 03:40:57 +08:00
return types.TBuiltinFunction("len")
def fn_round():
2015-06-29 03:40:57 +08:00
return types.TBuiltinFunction("round")
2015-07-22 03:32:10 +08:00
def fn_print():
return types.TBuiltinFunction("print")
def fn_kernel():
return types.TBuiltinFunction("kernel")
def fn_parallel():
return types.TBuiltinFunction("parallel")
def fn_sequential():
return types.TBuiltinFunction("sequential")
def fn_now():
return types.TBuiltinFunction("now")
def fn_delay():
return types.TBuiltinFunction("delay")
def fn_at():
return types.TBuiltinFunction("at")
def fn_now_mu():
return types.TBuiltinFunction("now_mu")
def fn_delay_mu():
return types.TBuiltinFunction("delay_mu")
def fn_at_mu():
return types.TBuiltinFunction("at_mu")
def fn_mu_to_seconds():
return types.TBuiltinFunction("mu_to_seconds")
def fn_seconds_to_mu():
return types.TBuiltinFunction("seconds_to_mu")
# Accessors
2015-06-14 17:07:13 +08:00
def is_none(typ):
return types.is_mono(typ, "NoneType")
def is_bool(typ):
return types.is_mono(typ, "bool")
2015-06-13 18:45:09 +08:00
def is_int(typ, width=None):
2015-06-29 03:48:15 +08:00
if width is not None:
2015-08-09 07:17:19 +08:00
return types.is_mono(typ, "int", width=width)
2015-06-13 18:45:09 +08:00
else:
return types.is_mono(typ, "int")
2015-06-14 17:07:13 +08:00
def get_int_width(typ):
if is_int(typ):
2015-06-15 13:40:37 +08:00
return types.get_value(typ.find()["width"])
2015-06-14 17:07:13 +08:00
def is_float(typ):
return types.is_mono(typ, "float")
2015-07-21 19:27:48 +08:00
def is_str(typ):
return types.is_mono(typ, "str")
def is_numeric(typ):
2015-06-14 17:07:13 +08:00
typ = typ.find()
return isinstance(typ, types.TMono) and \
typ.name in ('int', 'float')
2015-06-14 17:07:13 +08:00
def is_list(typ, elt=None):
2015-06-29 03:48:15 +08:00
if elt is not None:
2015-08-09 07:17:19 +08:00
return types.is_mono(typ, "list", elt=elt)
2015-06-14 17:07:13 +08:00
else:
return types.is_mono(typ, "list")
2015-06-26 23:53:20 +08:00
def is_range(typ, elt=None):
2015-06-29 03:48:15 +08:00
if elt is not None:
2015-06-26 23:53:20 +08:00
return types.is_mono(typ, "range", {"elt": elt})
else:
return types.is_mono(typ, "range")
def is_exception(typ, name=None):
if name is None:
return isinstance(typ.find(), TException)
else:
return isinstance(typ.find(), TException) and \
typ.name == name
2015-07-17 21:05:02 +08:00
2015-06-26 23:53:20 +08:00
def is_iterable(typ):
typ = typ.find()
return isinstance(typ, types.TMono) and \
typ.name in ('list', 'range')
def get_iterable_elt(typ):
if is_iterable(typ):
2015-07-17 21:05:02 +08:00
return typ.find()["elt"].find()
2015-06-26 23:53:20 +08:00
2015-06-14 17:07:13 +08:00
def is_collection(typ):
typ = typ.find()
return isinstance(typ, types.TTuple) or \
types.is_mono(typ, "list")
2015-06-24 16:46:15 +08:00
2015-07-21 19:27:48 +08:00
def is_allocated(typ):
2015-08-19 13:44:09 +08:00
return not (is_none(typ) or is_bool(typ) or is_int(typ) or
is_float(typ) or is_range(typ) or
types._is_pointer(typ) or types.is_function(typ) or
2015-08-19 13:44:09 +08:00
types.is_c_function(typ) or types.is_rpc_function(typ) or
types.is_method(typ) or types.is_tuple(typ) or
types.is_value(typ))