def _enum(isa: Enum) -> int: asm = {} dsm = {} layout = {} free = [] used = set() i_map = {} for inst in isa.enumerate(): val = inst._value_ if isinstance(val, int): used.add(val) i_map[inst] = val else: assert isinstance(val, EnumMeta.Auto) free.append(inst) c = 0 while free: inst = free.pop() while c in used: c += 1 used.add(c) i_map[inst] = c width = max(used).bit_length() for inst in isa.enumerate(): layout[inst] = (0, width) opcode = i_map[inst] asm[inst] = opcode dsm[opcode] = inst def assembler(inst): return asm[inst] def disassembler(opcode): return dsm[opcode] return assembler, disassembler, width, layout
def _enum(isa: Enum): encoding = {} decoding = {} layout = {} free = [] used = set() i_map = {} for inst in isa.enumerate(): if isinstance(inst.value, int): used.add(inst.value) i_map[inst] = inst.value else: assert isinstance(inst.value, EnumMeta.Auto) free.append(inst) c = 0 while free: inst = free.pop() while c in used: c += 1 used.add(c) i_map[inst] = c width = max(used).bit_length() for inst in isa.enumerate(): layout[inst] = (0, width, None) opcode = BitVector[width](i_map[inst]) encoding[inst] = opcode decoding[opcode] = inst def assembler(inst): return encoding[inst] def disassembler(bv): return decoding[bv] return assembler, disassembler, width, layout