# Path: core.py
# class REG(SimpleExpr):
# 
#     def __init__(self, name):
#         #assert isinstance(name, str)
#         self.name = name
#         self.signed = False
# 
#     def __repr__(self):
#         if self.simple_repr:
#             return self.__str__()
#         type = "REG_S" if self.signed else "REG"
#         return self.comment + type + "(%s)" % self.name
# 
#     def __str__(self):
#         cast = "(i32)" if self.signed else ""
#         return self.comment + cast + "$" + str(self.name)
# 
#     def __eq__(self, other):
#         return type(self) == type(other) and self.name == other.name
# 
#     def __lt__(self, other):
#         if type(self) != type(other):
#             return type_sort(type(self)) < type_sort(type(other))
# 
#         n1 = utils.natural_sort_key(self.name)
#         n2 = utils.natural_sort_key(other.name)
#         return n1 < n2
# 
#     def __contains__(self, other):
#         return self == other
# 
#     def __hash__(self):
#         return hash(self.name)
# 
#     def regs(self):
#         return {self}


from core import REG


def reg_range(prefix, first, last):
    return {REG("%s%d" % (prefix, x)) for x in range(first, last + 1)}


def reg_continuous_subrange(regs, ref_range):
    """Taking regs and ref_range as sorted reg lists, returns initial
    consecutive subrange of regs which is in ref_range. E.g. for
    regs={$a2, $a3, $a5}, ref_range=reg_range($a2, $a7), will return
    {$a2, $a3}. The idea is that ABIs usually call for cosecutive
    allocation of regs, so anything after 'hole' is spurious params /
    returns."""
    res = set()
    for r in sorted(ref_range):
        if r in regs:
            res.add(r)
        else:
            return res

# Path: core.py
# class REG(SimpleExpr):
# 
#     def __init__(self, name):
#         #assert isinstance(name, str)
#         self.name = name
#         self.signed = False
# 
#     def __repr__(self):
#         if self.simple_repr:
#             return self.__str__()
#         type = "REG_S" if self.signed else "REG"
#         return self.comment + type + "(%s)" % self.name
# 
#     def __str__(self):
#         cast = "(i32)" if self.signed else ""
#         return self.comment + cast + "$" + str(self.name)
# 
#     def __eq__(self, other):
#         return type(self) == type(other) and self.name == other.name
# 
#     def __lt__(self, other):
#         if type(self) != type(other):
#             return type_sort(type(self)) < type_sort(type(other))
# 
#         n1 = utils.natural_sort_key(self.name)
#         n2 = utils.natural_sort_key(other.name)
#         return n1 < n2
# 
#     def __contains__(self, other):
#         return self == other
# 
#     def __hash__(self):
#         return hash(self.name)
# 
#     def regs(self):
#         return {self}


# Path: archutils.py
from core import REG



def reg_range(prefix, first, last):
    return {REG("%s%d" % (prefix, x)) for x in range(first, last + 1)}


def reg_continuous_subrange(regs, ref_range):
    """Taking regs and ref_range as sorted reg lists, returns initial
    consecutive subrange of regs which is in ref_range. E.g. for
    regs={$a2, $a3, $a5}, ref_range=reg_range($a2, $a7), will return
    {$a2, $a3}. The idea is that ABIs usually call for cosecutive
    allocation of regs, so anything after 'hole' is spurious params /
    returns."""
    res = set()
    for r in sorted(ref_range):
        if r in regs:
            res.add(r)
        else:
            return res

    return res