# Path: awake/regutil.py
# def joinRegisters(regs):
#     out = set(regs)
#     for big in ('BC', 'DE', 'HL'):
#         if splitRegister(big) <= out:
#             out -= splitRegister(big)
#             out.add(big)
#     return out
# 
# def splitRegisters(regs):
#     out = set()
#     for x in regs:
#         out |= splitRegister(x)
#     return out
# 
# ALL_REGS = set(['B', 'C', 'D', 'E', 'H', 'L', 'SP', 'A', 'mem', 'ROMBANK', 'FZ', 'FC', 'FN', 'FH'])


from awake.regutil import joinRegisters, splitRegisters, ALL_REGS

def unknownDependencySet():
    return DependencySet(ALL_REGS - set(['FZ', 'FC', 'FN', 'FH']), ALL_REGS - set(['ROMBANK']))

class DependencySet:
    def __init__(self, reads=None, writes=None):
        if reads:
            self.reads = reads
        else:
            self.reads = set()
        if writes:
            self.writes = writes
        else:
            self.writes = set()

    def __str__(self):
        return 'DependencySet({0}, {1})'.format(joinRegisters(self.reads), joinRegisters(self.writes))

    def onlyRegisters(self):
        return DependencySet(self.reads & ALL_REGS, self.writes & ALL_REGS)

def encodeDependencySet(depset):
    return ", ".join(str(x) for x in joinRegisters(depset.reads)) + " -> " + ", ".join(str(x) for x in joinRegisters(depset.writes))

def decodeDependencySet(text):
    if not text:
        return DependencySet()
    r, w = text.split("->")
    reads = set(x.strip() for x in r.split(","))
    writes = set(x.strip() for x in w.split(","))

# Path: awake/regutil.py
# def joinRegisters(regs):
#     out = set(regs)
#     for big in ('BC', 'DE', 'HL'):
#         if splitRegister(big) <= out:
#             out -= splitRegister(big)
#             out.add(big)
#     return out
# 
# def splitRegisters(regs):
#     out = set()
#     for x in regs:
#         out |= splitRegister(x)
#     return out
# 
# ALL_REGS = set(['B', 'C', 'D', 'E', 'H', 'L', 'SP', 'A', 'mem', 'ROMBANK', 'FZ', 'FC', 'FN', 'FH'])


# Path: awake/depend.py
from awake.regutil import joinRegisters, splitRegisters, ALL_REGS


def unknownDependencySet():
    return DependencySet(ALL_REGS - set(['FZ', 'FC', 'FN', 'FH']), ALL_REGS - set(['ROMBANK']))

class DependencySet:
    def __init__(self, reads=None, writes=None):
        if reads:
            self.reads = reads
        else:
            self.reads = set()
        if writes:
            self.writes = writes
        else:
            self.writes = set()

    def __str__(self):
        return 'DependencySet({0}, {1})'.format(joinRegisters(self.reads), joinRegisters(self.writes))

    def onlyRegisters(self):
        return DependencySet(self.reads & ALL_REGS, self.writes & ALL_REGS)

def encodeDependencySet(depset):
    return ", ".join(str(x) for x in joinRegisters(depset.reads)) + " -> " + ", ".join(str(x) for x in joinRegisters(depset.writes))

def decodeDependencySet(text):
    if not text:
        return DependencySet()
    r, w = text.split("->")
    reads = set(x.strip() for x in r.split(","))
    writes = set(x.strip() for x in w.split(","))

    return DependencySet(splitRegisters(reads), splitRegisters(writes))