# Path: VM/util.py
# class Instruction(metaclass=InstructionMeta):
#     """
#     This class is here for convenience purposes only since it's much simpler (a.k.a. easier to type) to inherit from a class
#      than to use some weird metaclass. Also, all classes inheriting this one are automagically registered by the metaclass.
#     """
#     ...
# 
# def is_signed_out_of_range(num: int, size: int) -> bool:
#     """
#     Check if the signed number `num` is out of range for signed numbers of `size` byte length.
#     :param num:
#     :param size:
#     :return:
#     """
#     if size == 1:
#         return -128 <= num <= 127
#     elif size == 2:
#         return -32_768 <= num <= 32_767
#     elif size == 4:
#         return -2_147_483_648 <= num <= 2_147_483_647
# 
#     raise ValueError(f'Invalid number size: {size} not in (1, 2, 4)')
# 
# Path: VM/misc.py
# def parity(num: int) -> int:
#     """
#     Calculate the parity of the least significant byte of a number.
#     See: https://graphics.stanford.edu/~seander/bithacks.html#ParityWith64Bits
#     :param num: The byte to calculate the parity of.
#     :return:
#     """
# 
#     return ((((num & 0xFF) * 0x0101010101010101) & 0x8040201008040201) % 0x1FF) & 1
# 
# class Shift(enum.Enum):
#     C_ONE  = 1
#     C_CL   = 2
#     C_imm8 = 3
# 
#     SHL = 4
#     SHR = 5
#     SAR = 6
# 
# def MSB(num: int, size: int) -> int:
#     """
#     Get the most significant bit of a `size`-byte number `num`.
#     :param num: A number.
#     :param size: The number of bytes in this number.
#     :return:
#     """
# 
#     return (num >> (size * 8 - 1)) & 1
# 
# def LSB(num: int) -> int:
#     """
#     Get the least significant bit of a `size`-byte number `num`.
#     :param num: A number.
#     :param size: The number of bytes in this number.
#     :return:
#     """
# 
#     return num & 1


from ..util import Instruction, is_signed_out_of_range
from ..misc import parity, Shift, MSB, LSB
from functools import partialmethod as P
    from ..debug import debug_operand, debug_register_operand
import operator
    import logging
        else:
            raise RuntimeError('Invalid count')

        countMASK = 0x1F

        tmp_cnt = cnt & countMASK

        if tmp_cnt == 0:
            return True

        type, loc = RM

        dst = (type).get(loc, sz, operation == Shift.SAR)
        tmp_dst = dst

        while tmp_cnt != 0:
            if operation == Shift.SHL:
                # vm.reg.eflags_set(Reg32.CF, (dst >> (sz * 8)) & 1)
                vm.reg.eflags.CF = MSB(dst, sz)
                dst <<= 1
            else:
                # vm.reg.eflags_set(Reg32.CF, dst & 1)
                vm.reg.eflags.CF = LSB(dst)
                dst >>= 1

            tmp_cnt -= 1

        if cnt & countMASK == 1:
            if operation == Shift.SHL:
                vm.reg.eflags.OF = MSB(dst, sz) ^ vm.reg.eflags.CF

# Path: VM/util.py
# class Instruction(metaclass=InstructionMeta):
#     """
#     This class is here for convenience purposes only since it's much simpler (a.k.a. easier to type) to inherit from a class
#      than to use some weird metaclass. Also, all classes inheriting this one are automagically registered by the metaclass.
#     """
#     ...
# 
# def is_signed_out_of_range(num: int, size: int) -> bool:
#     """
#     Check if the signed number `num` is out of range for signed numbers of `size` byte length.
#     :param num:
#     :param size:
#     :return:
#     """
#     if size == 1:
#         return -128 <= num <= 127
#     elif size == 2:
#         return -32_768 <= num <= 32_767
#     elif size == 4:
#         return -2_147_483_648 <= num <= 2_147_483_647
# 
#     raise ValueError(f'Invalid number size: {size} not in (1, 2, 4)')
# 
# Path: VM/misc.py
# def parity(num: int) -> int:
#     """
#     Calculate the parity of the least significant byte of a number.
#     See: https://graphics.stanford.edu/~seander/bithacks.html#ParityWith64Bits
#     :param num: The byte to calculate the parity of.
#     :return:
#     """
# 
#     return ((((num & 0xFF) * 0x0101010101010101) & 0x8040201008040201) % 0x1FF) & 1
# 
# class Shift(enum.Enum):
#     C_ONE  = 1
#     C_CL   = 2
#     C_imm8 = 3
# 
#     SHL = 4
#     SHR = 5
#     SAR = 6
# 
# def MSB(num: int, size: int) -> int:
#     """
#     Get the most significant bit of a `size`-byte number `num`.
#     :param num: A number.
#     :param size: The number of bytes in this number.
#     :return:
#     """
# 
#     return (num >> (size * 8 - 1)) & 1
# 
# def LSB(num: int) -> int:
#     """
#     Get the least significant bit of a `size`-byte number `num`.
#     :param num: A number.
#     :param size: The number of bytes in this number.
#     :return:
#     """
# 
#     return num & 1


# Path: VM/instructions/bitwise.py
from ..util import Instruction, is_signed_out_of_range
from ..misc import parity, Shift, MSB, LSB
from functools import partialmethod as P
    from ..debug import debug_operand, debug_register_operand
import operator
    import logging

        else:
            raise RuntimeError('Invalid count')

        countMASK = 0x1F

        tmp_cnt = cnt & countMASK

        if tmp_cnt == 0:
            return True

        type, loc = RM

        dst = (type).get(loc, sz, operation == Shift.SAR)
        tmp_dst = dst

        while tmp_cnt != 0:
            if operation == Shift.SHL:
                # vm.reg.eflags_set(Reg32.CF, (dst >> (sz * 8)) & 1)
                vm.reg.eflags.CF = MSB(dst, sz)
                dst <<= 1
            else:
                # vm.reg.eflags_set(Reg32.CF, dst & 1)
                vm.reg.eflags.CF = LSB(dst)
                dst >>= 1

            tmp_cnt -= 1

        if cnt & countMASK == 1:
            if operation == Shift.SHL:
                vm.reg.eflags.OF = MSB(dst, sz) ^ vm.reg.eflags.CF

            elif operation == Shift.SAR: