Skip to content

replacer

Replacer

Replacer class

Warning: This class has been used in some experiments but will/should probably be removed because it does not belong to the project.

Source code in quokka/analysis/replacer.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class Replacer:
    """Replacer class

    Warning: This class has been used in some experiments but will/should probably be
    removed because it does not belong to the project.
    """

    ignored_registers: List[str]
    ignored_mnemonics: List[str]

    regs: RegType
    compared_mnemonics: List[str]
    calling_registers: List[RegType]

    replacement: Dict[RegType, RegType]

    @staticmethod
    def norm_mnemonic(mnemonic: str) -> str:
        """
        Norm a mnemonic (remove everything after '.').
        Examples:
            cmp.w -> CMP
            mov -> MOV

        Arguments:
            mnemonic: Mnemonic to norm

        Returns:
            Normed mnemonic
        """
        if "." in mnemonic:
            return mnemonic[: mnemonic.rfind(".")].upper()

        return mnemonic.upper()

    def get(self, item) -> RegType:
        assert self.replacement is not None
        if isinstance(item, RegType):
            return self.replacement.get(item, item)
        return item

    def is_ignored(self, item) -> bool:
        assert self.ignored_registers is not None
        return self.get(item) in self.ignored_registers

    def __call__(self, *args: Any, **kwargs: Any) -> RegType:
        return self.get(*args, **kwargs)

    def is_ignored_mnemonics(self, mnemonic: str) -> bool:
        assert self.ignored_mnemonics is not None
        return self.norm_mnemonic(mnemonic) in self.ignored_mnemonics

    def is_compared_mnemonic(self, mnemonic: str) -> bool:
        assert self.compared_mnemonics is not None
        return self.norm_mnemonic(mnemonic) in self.compared_mnemonics

    def calling_convention(self) -> List[RegType]:
        assert self.calling_registers is not None
        return self.calling_registers

    def argument_registers(self, platform: quokka.analysis.Platform) -> List:
        return []

norm_mnemonic(mnemonic) staticmethod

Norm a mnemonic (remove everything after '.'). Examples: cmp.w -> CMP mov -> MOV

Parameters:

Name Type Description Default
mnemonic str

Mnemonic to norm

required

Returns:

Type Description
str

Normed mnemonic

Source code in quokka/analysis/replacer.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@staticmethod
def norm_mnemonic(mnemonic: str) -> str:
    """
    Norm a mnemonic (remove everything after '.').
    Examples:
        cmp.w -> CMP
        mov -> MOV

    Arguments:
        mnemonic: Mnemonic to norm

    Returns:
        Normed mnemonic
    """
    if "." in mnemonic:
        return mnemonic[: mnemonic.rfind(".")].upper()

    return mnemonic.upper()